GO /****** Object: Table [dbo].[fex01] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[fex01]( [fea00c] [varchar](10) NOT NULL, [fea01c] [varchar](20) NOT NULL, [fea03d] [varchar](8) NULL, [fea04t] [varchar](6) NULL, [fea05c] [varchar](10) NULL, [fea06c] [varchar](10) NULL, [fea07i] [int] NULL, [fea91c] [varchar](10) NULL, [fea92d] [varchar](8) NULL, [fea93c] [varchar](10) NULL, [fea94d] [varchar](8) NULL, CONSTRAINT [PK_FEX01] PRIMARY KEY CLUSTERED ( [fea00c] ASC, [fea01c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[pt_master] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[pt_master]( [id] [numeric](18, 0) IDENTITY(1,1) NOT NULL, [pt_date] [varchar](8) NOT NULL, [out_no] [varchar](100) NULL, [in_no] [varchar](100) NULL, [out_comp] [varchar](10) NOT NULL, [in_comp] [varchar](10) NOT NULL, [out_check] [varchar](10) NULL, [out_checkdate] [varchar](8) NULL, [in_check] [varchar](10) NULL, [in_checkdate] [varchar](8) NULL, [state] [int] NOT NULL, CONSTRAINT [PK_pt_master] PRIMARY KEY CLUSTERED ( [id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gam04] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gam04]( [gad00c] [varchar](10) NOT NULL, [gad01c] [varchar](4) NOT NULL, [gad02c] [varchar](30) NULL, [gad03c] [varchar](20) NULL, [gad04c] [varchar](60) NULL, [gad05c] [varchar](20) NULL, [gad06c] [varchar](20) NULL, [gad07c] [varchar](60) NULL, [gad08c] [varchar](100) NULL, CONSTRAINT [PK_GAM04] PRIMARY KEY CLUSTERED ( [gad00c] ASC, [gad01c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gcm13] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gcm13]( [gco00c] [varchar](10) NOT NULL, [gco01c] [varchar](20) NOT NULL, [gco02i] [int] IDENTITY(1,1) NOT NULL, [gco03d] [varchar](8) NULL, [gco04t] [varchar](8) NULL, [gco05f] [float] NULL, [gco06f] [float] NULL, [gco07f] [float] NULL, [gco08f] [float] NULL, [gco09f] [float] NULL, [gco10i] [int] NULL, [gco11c] [varchar](20) NULL, [gco12f] [float] NULL, [gco13i] [int] NULL, [gco14c] [varchar](10) NULL, CONSTRAINT [PK_GCM13] PRIMARY KEY NONCLUSTERED ( [gco00c] ASC, [gco02i] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[wol] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[wol]( [wol00] [int] IDENTITY(1,1) NOT NULL, [wol01v] [varchar](20) NULL, [wol02v] [varchar](20) NULL, CONSTRAINT [PK_WOL] PRIMARY KEY CLUSTERED ( [wol00] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[ham08] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[ham08]( [hah00c] [varchar](10) NOT NULL, [hah01c] [varchar](10) NOT NULL, [hah02f] [float] NOT NULL, [hah03d] [varchar](8) NULL, [hah04c] [varchar](10) NULL, [hah05f] [float] NULL, [hah06c] [varchar](200) NULL, CONSTRAINT [PK_HAM08] PRIMARY KEY CLUSTERED ( [hah00c] ASC, [hah01c] ASC, [hah02f] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[glm10] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[glm10]( [gll00c] [varchar](10) NOT NULL, [gll01i] [int] IDENTITY(1,1) NOT NULL, [gll02d] [varchar](8) NULL, [gll03t] [varchar](6) NULL, [gll04t] [varchar](6) NULL, [gll05c] [varchar](20) NULL, [gll06c] [varchar](20) NULL, [gll07c] [varchar](20) NULL, CONSTRAINT [PK_GLM10] PRIMARY KEY CLUSTERED ( [gll00c] ASC, [gll01i] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[ing01] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[ing01]( [ina00c] [varchar](10) NOT NULL, [ina01c] [varchar](20) NOT NULL, [ina02d] [varchar](8) NOT NULL, [ina03c] [varchar](20) NOT NULL, [ina04c] [varchar](20) NULL, [ina05c] [varchar](1000) NULL, [ina06t] [varchar](6) NOT NULL, [ina91c] [varchar](10) NULL, [ina92d] [varchar](8) NULL, [ina93c] [varchar](10) NULL, [ina94d] [varchar](8) NULL, CONSTRAINT [PK_ING01] PRIMARY KEY CLUSTERED ( [ina00c] ASC, [ina01c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[card_fill_detail_by_payment] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[card_fill_detail_by_payment]( [idd] [int] IDENTITY(1,1) NOT NULL, [compid] [varchar](10) NOT NULL, [billtype] [varchar](10) NOT NULL, [billid] [varchar](20) NOT NULL, [personid] [varchar](20) NULL, [share_rate] [float] NULL, [paycode] [varchar](20) NULL, [payamt] [float] NULL, [filltype] [int] NULL, [cardid] [varchar](20) NULL, [cardtype] [varchar](20) NULL, [code] [varchar](20) NULL, [times] [float] NULL, [procid] [float] NULL, [perf] [float] NULL, [person_pos] [int] NULL, [comm] [float] NULL, [pctimes] [float] NULL, PRIMARY KEY CLUSTERED ( [idd] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gam05] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gam05]( [gae01c] [varchar](10) NOT NULL, [gae02c] [varchar](70) NULL, [gae03c] [varchar](40) NULL, [gae04c] [varchar](80) NULL, [gae05c] [varchar](80) NULL, [gae06c] [varchar](20) NULL, [gae07c] [varchar](20) NULL, [gae08c] [varchar](160) NULL, [gae09c] [varchar](160) NULL, [gae10c] [varchar](20) NULL, [gae11c] [varchar](50) NULL, [gae12c] [varchar](100) NULL, [gae13c] [varchar](10) NULL, [gae14c] [varchar](10) NULL, [gae15c] [varchar](20) NULL, [gae16c] [varchar](20) NULL, [gae17f] [float] NULL, [gae18f] [float] NULL, [gae19c] [varchar](60) NULL, [gae20c] [varchar](60) NULL, [gae21c] [varchar](20) NULL, [gae22p] [image] NULL, [gae23c] [varchar](100) NULL, [gae24c] [varchar](100) NULL, [gae25c] [varchar](100) NULL, [gae26c] [varchar](100) NULL, CONSTRAINT [PK_GAM05] PRIMARY KEY CLUSTERED ( [gae01c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_customer_etg] Script Date: 06/12/2017 06:19:54 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_customer_etg] ( @gct00c varchar(10), -- 公司别 @begindays int , -- 疏远天数(大于等于) @enddays int -- 疏远天数 (小于等于) ) as set nocount on set rowcount 0 begin begin try -- 最后一次来的日期 declare @begindate varchar(8),@enddate varchar(8) set @begindate = convert(varchar(8),dateadd(day,-@begindays,getdate()),112) if (@enddays = 0) begin set @enddate = convert(varchar(8),dateadd(day,-9999,getdate()),112) end else begin set @enddate = convert(varchar(8),dateadd(day,-@enddays,getdate()),112) end --会员卡基本信息 create table #gcm01 ( gca13d varchar(10) , --门店编号 gca01c varchar(20) --卡号 ) create clustered index idx_gcm01 on #gcm01(gca01c,gca13d) -- 会员卡消费历史信息 create table #gcm20 ( gct00c varchar(10), --门店编号 gct02c varchar(20), --卡号 gct04d varchar(8) --日期 ) create clustered index idx_gcm20_1 on #gcm20 (gct04d) -- 会员基本资料 create table #gbm01 ( gba00c varchar(10), -- 门店编号 gba23c varchar(20), -- 会员卡号 gba01c varchar(20), -- 会员编号 gba03c varchar(200), -- 会员姓名 gba07c varchar(20), -- 家庭Tele No. gba08c varchar(20), -- Mobile No. gba14i varchar(2), -- 性别 gba10c varchar(20) -- 当前负责业务 ) create clustered index idx_gbm01 on #gbm01(gba23c,gba00c) --查询归属当前门店的卡号 insert into #gcm01 (gca13d,gca01c) select gca00c,gca01c from gcm01 a with (nolock) where a.gca13d = @gct00c and a.gca08i in ( 4,5 ) --查询会员所有的消费历史信息 insert into #gcm20 (gct00c,gct02c,gct04d) select gct00c,gct02c,gct04d from gcm20 a with (nolock),#gcm01 b where a.gct02c = b.gca01c and gct04d between @enddate and @begindate and a.gct00c = b.gca13d -- 查询会员基本资料 insert into #gbm01 (gba00c,gba23c,gba01c,gba03c,gba07c,gba08c,gba14i,gba10c) select gba00c,gba23c,gba01c,gba03c,gba07c,gba08c,case when gba14i = 0 then '女' else '男' end gba14i,gba10c from gbm01 a with (nolock),#gcm01 b where a.gba23c = b.gca01c and a.gba00c = b.gca13d -- 计算结果 select gct00c,gct02c,b.gba01c,gct04d,datediff(d,gct04d,getdate()) diff,b.gba03c,b.gba07c,b.gba08c,b.gba14i,b.gba10c from ( select row_number() over (partition by gct02c order by gct04d desc) rn,gct00c,gct02c,gct04d from #gcm20 ) a, #gbm01 b where a.gct04d between @enddate and @begindate and a.rn = 1 and a.gct02c = b.gba23c and a.gct00c = b.gba00c order by gct04d desc --删除临时表 drop table #gcm01 drop table #gcm20 drop table #gbm01 end try begin catch drop table #gcm01 drop table #gcm20 drop table #gbm01 end catch end GO /****** Object: StoredProcedure [dbo].[upg_project_ratiowas] Script Date: 06/12/2017 06:20:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_project_ratiowas] ( @compid varchar(10), --门店 @fromdate varchar(10), --日期 @todate varchar(10), @fromdepart varchar(10), --部门 @todepart varchar(10), @type int --类型 0新增疗程,1消费疗程 ) as begin Create table #projectRatiowas ( compid varchar(10) not null, code varchar(30) null, [name] varchar(60) null, number float, jine float ) --会员卡冲值--疗程 gcm11 --会员卡销售--疗程 gnm03 if( @type = 0 ) begin insert into #projectRatiowas select compid,code,[name],number,jine from ( select a.gnc00c compid, a.gnc02c code, -- 项目编号 c.gda03c [name], --项目简称 sum(isnull(a.gnc11f,0)) number, -- 数量 sum(isnull(a.gnc05f,0)) jine -- 疗程金额 from gnm03 a with(nolock) inner join gnm01 b with(nolock) on b.gna00c = a.gnc00c and b.gna01c = a.gnc01c inner join gdm01 c with(nolock) on c.gda00c = a.gnc00c and c.gda01c = a.gnc02c where a.gnc00c in (select gaz02c from gam26 where gaz01c = @compid) and b.gna02d between @fromdate and @todate and (@fromdepart = '*' or c.gda02c between @fromdepart and @todepart) group by a.gnc00c,a.gnc02c,c.gda03c union all select a.gcm00c compid, a.gcm02c code, c.gda03c [name], --项目简称 sum(isnull(a.gcm11f,0)) number, -- 数量 sum(isnull(a.gcm05f,0)) jine from gcm11 a with(nolock) inner join gcm10 b with(nolock) on b.gcl00c = a.gcm00c and b.gcl01c = a.gcm01c and b.gcl06i = 0 inner join gdm01 c with(nolock) on c.gda00c = a.gcm00c and c.gda01c = a.gcm02c where a.gcm00c in (select gaz02c from gam26 where gaz01c = @compid) and b.gcl02d between @fromdate and @todate and (@fromdepart = '*' or c.gda02c between @fromdepart and @todepart) group by a.gcm00c,a.gcm02c, c.gda03c ) as Atable end else -- 消费疗程 begin insert into #projectRatiowas select compid,code,[name],number,jine from ( select a.ggb00c compid, a.ggb03c code, c.gda03c [name], --项目简称 sum(isnull(a.ggb05f,0)) number, sum(isnull(a.ggb11f,0)) jine from ggm02 a with(nolock) inner join ggm01 b with(nolock) on a.ggb00c = b.gga00c and a.ggb01c = b.gga01c inner join gdm01 c with(nolock) on c.gda00c = a.ggb00c and c.gda01c = a.ggb03c where a.ggb00c in (select gaz02c from gam26 where gaz01c = @compid) and isnull(a.ggb47i,-1) <> -1 and b.gga94d between @fromdate and @todate and (@fromdepart = '*' or c.gda02c between @fromdepart and @todepart) group by a.ggb00c,a.ggb03c,c.gda03c ) as Atable end select a.compid, a.code, a.name, a.number, a.jine, case b.totalnum when 0 then 0 else (a.number / b.totalnum) * 100 end rationum, --数量占比 case b.totaljine when 0 then 0 else (a.jine / b.totaljine) * 100 end ratiojine --金额占比 from #projectRatiowas a inner join (select compid,sum(number) totalnum,sum(jine) totaljine from #projectRatiowas group by compid ) as b on a.compid = b.compid drop table #projectRatiowas end GO /****** Object: Table [dbo].[gim03] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gim03]( [gic00c] [varchar](10) NOT NULL, [gic01c] [nvarchar](100) NOT NULL, [gic02c] [varchar](20) NOT NULL, [gic03c] [nvarchar](100) NOT NULL, [gic04f] [float] NULL, [gic05f] [float] NULL, CONSTRAINT [PK_GIM03] PRIMARY KEY CLUSTERED ( [gic00c] ASC, [gic01c] ASC, [gic02c] ASC, [gic03c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[ham09] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[ham09]( [hai00c] [varchar](10) NOT NULL, [hai01c] [varchar](20) NOT NULL, [hai02f] [float] NOT NULL, [hai03i] [int] NULL, [hai04c] [varchar](20) NULL, [hai05c] [varchar](5) NULL, [hai06f] [float] NULL, [hai07f] [float] NULL, CONSTRAINT [PK_HAM09] PRIMARY KEY CLUSTERED ( [hai00c] ASC, [hai01c] ASC, [hai02f] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_ymn_proc_consume_stat_outer] Script Date: 06/12/2017 06:20:53 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_ymn_proc_consume_stat_outer] ( @incompid varchar(10), -- 已经能支持各级别的公司 @fromdate varchar(10), -- 开始日期 @todate varchar(10) ) as begin create table #tbl_proc_consume ( seq int identity not null, --0 compid varchar(20) not null, --1 fromdate varchar(20) not null, --2 todate varchar(20) not null, --3 --前期 --美容 p_mr_s_times float null, --4 p_mr_s_amt float null, --5 p_mr_ll_times float null, --6 p_mr_ll_amt float null, --7 p_mr_xh_times float null, --8 p_mr_xh_amt float null, --9 p_mr_e_times float null, --10, 计算 p_mr_e_amt float null, --11, 计算 --美体 p_mt_s_times float null, --12 p_mt_s_amt float null, --13 p_mt_ll_times float null, p_mt_ll_amt float null, p_mt_xh_times float null, p_mt_xh_amt float null, p_mt_e_times float null, --, 计算 p_mt_e_amt float null, --, 计算 --合计 p_sum_s_times float null, --20 p_sum_s_amt float null, p_sum_ll_times float null, p_sum_ll_amt float null, p_sum_xh_times float null, p_sum_xh_amt float null, p_sum_e_times float null, p_sum_e_amt float null, --当期 --美容 c_mr_s_times float null, --28 c_mr_s_amt float null, c_mr_ll_times float null, c_mr_ll_amt float null, c_mr_xh_times float null, c_mr_xh_amt float null, c_mr_e_times float null, c_mr_e_amt float null, --美体 c_mt_s_times float null, --36 c_mt_s_amt float null, c_mt_ll_times float null, c_mt_ll_amt float null, c_mt_xh_times float null, c_mt_xh_amt float null, c_mt_e_times float null, c_mt_e_amt float null, --合计 c_sum_s_times float null, --44 c_sum_s_amt float null, --45 c_sum_ll_times float null, --46 c_sum_ll_amt float null, --47 c_sum_xh_times float null, --48 c_sum_xh_amt float null, --49 c_sum_e_times float null, --50 c_sum_e_amt float null, --51 primary key (seq)) --缓冲的表 create table #tbl_buffer( compid varchar(20) not null, s_times float null, --期初的次数 s_amt float null, --期初的金额 ll_times float null, --录入的次数 ll_amt float null, --录入的金额 xh_times float null, --消耗的次数 xh_amt float null, --消耗的金额 e_times float null, --期末的次数 e_amt float null, primary key(compid)) --期末的金额 create table #tbl_buffer_xh( compid varchar(20) not null, s_times float null, --期初的次数 s_amt float null, --期初的金额 ll_times float null, --录入的次数 ll_amt float null, --录入的金额 xh_times float null, --消耗的次数 xh_amt float null, --消耗的金额 e_times float null, --期末的次数 e_amt float null, --期末的金额 primary key(compid)) create table #tbl_buffer_ll( compid varchar(20) not null, s_times float null, --期初的次数 s_amt float null, --期初的金额 ll_times float null, --录入的次数 ll_amt float null, --录入的金额 xh_times float null, --消耗的次数 xh_amt float null, --消耗的金额 e_times float null, --期末的次数 e_amt float null, --期末的金额 primary key(compid)) --每个门店初始化一个记录 insert #tbl_proc_consume(compid, fromdate, todate) select gaz02c, @fromdate, @todate from gam26 z with(nolock) where z.gaz01c = @incompid --计算当前这个时间范围内消耗 --当期卡的处理 --当前的卡的美容期初的余量 delete #tbl_buffer_ll create table #gcm08_buf -- 会员卡--疗程变化历史 ( gch00c varchar(10) not null, --公司编号 gch01i int not null, -- 序号 gch02d varchar(8) null, -- 日期 gch03c varchar(20) not null, --卡号 gch04c varchar(20) not null, --疗程项目编号 gch05f float null, --购买次数 gch06f float null, --赠送次数 gch07f float null, --购买金额 gch08f float null, --使用次数 gch09f float null, --已经使用金额 gch10c varchar(20) null, --单据类别 gch11c varchar(20) null, --单号 gch12i int null, --价格序号 gch13f float null, --折扣 gch14f float null, --购买数量 gch15f float null, --单价 gch16i int null, --疗程中的序号 ) declare @PK_Sql nvarchar(max) set @PK_Sql = ' alter table #gcm08_buf add constraint PK_#gcm08_buf_' + convert(varchar(10), cast(floor(rand() * 1000000) as int)) + ' PRIMARY KEY NonCLUSTERED(gch01i)' exec sp_executesql @PK_Sql insert #gcm08_buf(gch00c, gch01i, gch02d, gch03c, gch04c, gch05f, gch06f, gch07f, gch08f, gch09f, gch10c, gch11c, gch12i, gch13f, gch14f, gch15f, gch16i) select gch00c, gch01i, gch02d, gch03c, gch04c, gch05f, gch06f, gch07f, gch08f, gch09f, gch10c, gch11c, gch12i, gch13f, gch14f, gch15f, gch16i from gcm08 a with (nolock), gam26 z with(nolock) where a.gch00c = z.gaz02c and z.gaz01c = @incompid and isnull(a.gch02d, '') <= @todate create clustered index idx_gcm08_buf_1 on #gcm08_buf(gch00c, gch02d) create nonclustered index idx_gcm08_buf_2 on #gcm08_buf(gch16i) insert #tbl_buffer_ll(compid, ll_times, ll_amt) select gch00c, sum(isnull(gch05f, 0)), sum(isnull(gch07f, 0)) from #gcm08_buf a, gdm01 c with(nolock) where isnull(a.gch02d, '')<@fromdate and a.gch00c = c.gda00c and a.gch04c = c.gda01c and c.gda13c = '233' group by gch00c delete #tbl_buffer_xh insert #tbl_buffer_xh(compid, xh_times, xh_amt) select gch00c, sum(isnull(gch08f, 0)), sum(isnull(gch09f, 0)) from #gcm08_buf a, gcm06 b with (nolock), gdm01 c with(nolock) where isnull(a.gch02d, '') < @fromdate and a.gch00c = b.gcf00c and isnull(a.gch16i, 0) = b.gcf23i and a.gch00c = c.gda00c and a.gch04c = c.gda01c and c.gda13c = '233' and isnull(b.gcf20i, 0) = 0 group by gch00c update a set a.c_mr_s_times = isnull(ll_times, 0), a.c_mr_s_amt = isnull(ll_amt, 0) from #tbl_proc_consume a, #tbl_buffer_ll b where a.compid = b.compid update a set a.c_mr_s_times = isnull(c_mr_s_times, 0) - isnull(xh_times, 0), a.c_mr_s_amt = isnull(c_mr_s_amt, 0) - isnull(xh_amt, 0) from #tbl_proc_consume a, #tbl_buffer_xh b where a.compid = b.compid --当前的卡的美体期初的余量 delete #tbl_buffer_ll insert #tbl_buffer_ll(compid, ll_times, ll_amt) select gch00c, sum(isnull(gch05f, 0)), sum(isnull(gch07f, 0)) from #gcm08_buf a, gdm01 c with(nolock) where isnull(a.gch02d, '')<@fromdate and a.gch00c = c.gda00c and a.gch04c = c.gda01c and c.gda13c <> '233' group by gch00c delete #tbl_buffer_xh insert #tbl_buffer_xh(compid, xh_times, xh_amt) select gch00c, sum(isnull(gch08f, 0)), sum(isnull(gch09f, 0)) from #gcm08_buf a, gcm06 b with (nolock), gdm01 c with(nolock) where isnull(a.gch02d, '')<@fromdate and a.gch00c = b.gcf00c and isnull(a.gch16i, 0) = b.gcf23i and a.gch00c = c.gda00c and a.gch04c = c.gda01c and c.gda13c <> '233' and isnull(b.gcf20i, 0) = 0 group by gch00c update a set a.c_mt_s_times = isnull(ll_times, 0), a.c_mt_s_amt = isnull(ll_amt, 0) from #tbl_proc_consume a, #tbl_buffer_ll b where a.compid = b.compid update a set a.c_mt_s_times = isnull(c_mt_s_times, 0) - isnull(xh_times, 0), a.c_mt_s_amt = isnull(c_mt_s_amt, 0) - isnull(xh_amt, 0) from #tbl_proc_consume a, #tbl_buffer_xh b where a.compid = b.compid --当期卡的,美容,当前的消耗, 以及输入 --只要在系统中销售的卡必然是当前卡 --当前卡,美容,录入 delete #tbl_buffer_ll insert #tbl_buffer_ll(compid, ll_times, ll_amt) select gch00c, sum(isnull(gch05f, 0)), sum(isnull(gch07f, 0)) from #gcm08_buf a, gdm01 c with(nolock) where isnull(a.gch02d, '') >= @fromdate and isnull(a.gch02d, '') <= @todate and a.gch00c = c.gda00c and a.gch04c = c.gda01c and c.gda13c = '233' group by gch00c --当前卡,美容,消耗 delete #tbl_buffer_xh insert #tbl_buffer_xh(compid, xh_times, xh_amt) select gch00c, sum(isnull(gch08f, 0)), sum(isnull(gch09f, 0)) from #gcm08_buf a, gcm06 b with (nolock), gdm01 c with(nolock) where isnull(a.gch02d, '') >= @fromdate and isnull(a.gch02d, '') <= @todate and a.gch00c = c.gda00c and a.gch04c = c.gda01c and c.gda13c = '233' and a.gch00c = b.gcf00c and isnull(a.gch16i, 0) = b.gcf23i and isnull(b.gcf20i, 0) = 0 group by gch00c --更新到主数据表中, 分别更新录入以及消耗 update a set a.c_mr_ll_times = isnull(b.ll_times, 0), a.c_mr_ll_amt = isnull(b.ll_amt, 0) from #tbl_proc_consume a, #tbl_buffer_ll b where a.compid = b.compid update a set a.c_mr_xh_times = isnull(b.xh_times, 0), a.c_mr_xh_amt = isnull(b.xh_amt, 0) from #tbl_proc_consume a, #tbl_buffer_xh b where a.compid = b.compid --当期卡的,美体,当前的消耗, 以及输入 delete #tbl_buffer_ll insert #tbl_buffer_ll(compid, ll_times, ll_amt) select gch00c, sum(isnull(gch05f, 0)), sum(isnull(gch07f, 0)) from #gcm08_buf a, gdm01 c with(nolock) where isnull(a.gch02d, '') >= @fromdate and isnull(a.gch02d, '') <= @todate and a.gch00c = c.gda00c and a.gch04c = c.gda01c and c.gda13c <> '233' group by gch00c --当前卡,美容,消耗 delete #tbl_buffer_xh insert #tbl_buffer_xh(compid, xh_times, xh_amt) select gch00c, sum(isnull(gch08f, 0)), sum(isnull(gch09f, 0)) from #gcm08_buf a, gcm06 b with (nolock), gdm01 c with(nolock) where isnull(a.gch02d, '') >= @fromdate and isnull(a.gch02d, '') <= @todate and a.gch00c = c.gda00c and a.gch04c = c.gda01c and c.gda13c <> '233' and a.gch00c = b.gcf00c and isnull(a.gch16i, 0) = b.gcf23i and isnull(b.gcf20i, 0) = 0 group by gch00c --更新到主数据表中, 分别更新录入以及消耗 update a set a.c_mt_ll_times = isnull(b.ll_times, 0), a.c_mt_ll_amt = isnull(b.ll_amt, 0) from #tbl_proc_consume a, #tbl_buffer_ll b where a.compid = b.compid update a set a.c_mt_xh_times = isnull(b.xh_times, 0), a.c_mt_xh_amt = isnull(b.xh_amt, 0) from #tbl_proc_consume a, #tbl_buffer_xh b where a.compid = b.compid --前期卡的处理 --前期卡的录入,另外需要计算消耗的量,这样就可以计算出来期初的量了 --计算美容的前期余额 delete #tbl_buffer_ll insert #tbl_buffer_ll(compid, ll_times, ll_amt) select gcr00c, sum(isnull(gcr07f, 0)), sum(isnull(gcr10f, 0)) from gcm16 a with(nolock), gam26 z with(nolock), gdm01 c with(nolock) where a.gcr00c = z.gaz02c and z.gaz01c = @incompid and isnull(a.gcr24d, '') < @fromdate and a.gcr00c = c.gda00c and a.gcr02c = c.gda01c and c.gda13c = '233' group by gcr00c delete #tbl_buffer_xh insert #tbl_buffer_xh(compid, xh_times, xh_amt) select gch00c, sum(isnull(gch08f, 0)), sum(isnull(gch09f, 0)) from #gcm08_buf a, gcm06 b with (nolock), gdm01 c with(nolock) where isnull(a.gch02d, '') < @fromdate and a.gch00c = b.gcf00c and isnull(a.gch16i, 0) = b.gcf23i and a.gch00c = c.gda00c and a.gch04c = c.gda01c and c.gda13c = '233' and isnull(b.gcf20i, 0) >= 1 group by gch00c update a set a.xh_times = isnull(b.xh_times, 0), a.xh_amt = isnull(b.xh_amt, 0) from #tbl_buffer_ll a, #tbl_buffer_xh b where a.compid = b.compid update a set a.p_mr_s_times = isnull(ll_times, 0) - isnull(xh_times, 0), a.p_mr_s_amt = isnull(ll_amt, 0) - isnull(xh_amt, 0) from #tbl_proc_consume a, #tbl_buffer_ll b where a.compid = b.compid --计算美体的前期余额 delete #tbl_buffer_ll insert #tbl_buffer_ll(compid, ll_times, ll_amt) select gcr00c, sum(isnull(gcr07f, 0)), sum(isnull(gcr10f, 0)) from gcm16 a with(nolock), gam26 z with(nolock), gdm01 c with(nolock) where a.gcr00c = z.gaz02c and z.gaz01c = @incompid and isnull(a.gcr24d, '')<@fromdate and a.gcr00c = c.gda00c and a.gcr02c = c.gda01c and c.gda13c <> '233' group by gcr00c delete #tbl_buffer_xh insert #tbl_buffer_xh(compid, xh_times, xh_amt) select gch00c, sum(isnull(gch08f, 0)), sum(isnull(gch09f, 0)) from #gcm08_buf a, gcm06 b with (nolock), gdm01 c with(nolock) where isnull(a.gch02d, '') < @fromdate and a.gch00c = b.gcf00c and isnull(a.gch16i, 0) = b.gcf23i and a.gch00c = c.gda00c and a.gch04c = c.gda01c and c.gda13c <> '233' and isnull(b.gcf20i, 0) >= 1 group by gch00c update a set a.xh_times = isnull(b.xh_times, 0), a.xh_amt = isnull(b.xh_amt, 0) from #tbl_buffer_ll a, #tbl_buffer_xh b where a.compid = b.compid update a set a.p_mt_s_times = isnull(ll_times, 0) - isnull(xh_times, 0), a.p_mt_s_amt = isnull(ll_amt, 0) - isnull(xh_amt, 0) from #tbl_proc_consume a, #tbl_buffer_ll b where a.compid = b.compid --前期卡的,美容,当前的消耗, 以及输入必须在开帐中取得 delete #tbl_buffer_xh insert #tbl_buffer_xh(compid, xh_times, xh_amt) select gch00c, sum(isnull(gch08f, 0)), sum(isnull(gch09f, 0)) from #gcm08_buf a, gcm06 b with (nolock), gdm01 c with(nolock) where isnull(a.gch02d, '') >= @fromdate and isnull(a.gch02d, '') <= @todate and a.gch00c = b.gcf00c and isnull(a.gch16i, 0) = b.gcf23i and a.gch00c = c.gda00c and a.gch04c = c.gda01c and c.gda13c = '233' and isnull(b.gcf20i, 0) >= 1 group by gch00c --更新到主数据表中 update a set a.p_mr_xh_times = isnull(b.xh_times, 0), a.p_mr_xh_amt = isnull(b.xh_amt, 0) from #tbl_proc_consume a, #tbl_buffer_xh b where a.compid = b.compid --前期卡的,美体,当前的消耗 delete #tbl_buffer_xh insert #tbl_buffer_xh(compid, xh_times, xh_amt) select gch00c, sum(isnull(gch08f, 0)), sum(isnull(gch09f, 0)) from #gcm08_buf a, gcm06 b with (nolock), gdm01 c with(nolock) where isnull(a.gch02d, '') >= @fromdate and isnull(a.gch02d, '') <= @todate and a.gch00c = b.gcf00c and isnull(a.gch16i, 0) = b.gcf23i and a.gch00c = c.gda00c and a.gch04c = c.gda01c and c.gda13c <> '233' and isnull(b.gcf20i, 0) >= 1 group by gch00c --更新到主数据表中 update a set a.p_mt_xh_times = isnull(b.xh_times, 0), a.p_mt_xh_amt = isnull(b.xh_amt, 0) from #tbl_proc_consume a, #tbl_buffer_xh b where a.compid = b.compid --需要处理前期卡的输入,需要到gcm16中统计 --美容前期卡的录入统计 delete #tbl_buffer_ll insert #tbl_buffer_ll(compid, ll_times, ll_amt) select gcr00c, sum(isnull(gcr07f, 0)), sum(isnull(gcr10f, 0)) from gcm16 a with(nolock), gam26 z with(nolock), gdm01 c with(nolock) where a.gcr00c = z.gaz02c and z.gaz01c = @incompid and isnull(a.gcr24d, '') >= @fromdate and isnull(a.gcr24d, '') <= @todate and a.gcr00c = c.gda00c and a.gcr02c = c.gda01c and c.gda13c = '233' group by gcr00c --更新到主数据表中 update a set a.p_mr_ll_times = isnull( b.ll_times, 0), a.p_mr_ll_amt = isnull(b.ll_amt, 0) from #tbl_proc_consume a, #tbl_buffer_ll b where a.compid = b.compid --美体前期卡的录入统计 delete #tbl_buffer_ll insert #tbl_buffer_ll(compid, ll_times, ll_amt) select gcr00c, sum(isnull(gcr07f, 0)), sum(isnull(gcr10f, 0)) from gcm16 a with(nolock), gam26 z with(nolock), gdm01 c with(nolock) where a.gcr00c = z.gaz02c and z.gaz01c = @incompid and isnull(a.gcr24d, '') >= @fromdate and isnull(a.gcr24d, '') <= @todate and a.gcr00c = c.gda00c and a.gcr02c = c.gda01c and c.gda13c <> '233' group by gcr00c --更新到主数据表中 update a set a.p_mt_ll_times = isnull(b.ll_times, 0), a.p_mt_ll_amt = isnull(b.ll_amt, 0) from #tbl_proc_consume a, #tbl_buffer_ll b where a.compid = b.compid --把表中的计算字段计算出来 --计算期末的余额 update a set a.c_mr_e_times = isnull(c_mr_s_times, 0) + isnull(c_mr_ll_times, 0) - isnull(c_mr_xh_times, 0), a.c_mr_e_amt = isnull(c_mr_s_amt, 0) + isnull(c_mr_ll_amt, 0) - isnull(c_mr_xh_amt, 0), a.c_mt_e_times = isnull(c_mt_s_times, 0) + isnull(c_mt_ll_times, 0) - isnull(c_mt_xh_times, 0), a.c_mt_e_amt = isnull(c_mt_s_amt, 0) + isnull(c_mt_ll_amt, 0) - isnull(c_mt_xh_amt, 0), a.p_mr_e_times = isnull(p_mr_s_times, 0) + isnull(p_mr_ll_times, 0) - isnull(p_mr_xh_times, 0), a.p_mr_e_amt = isnull(p_mr_s_amt, 0) + isnull(p_mr_ll_amt, 0) - isnull(p_mr_xh_amt, 0), a.p_mt_e_times = isnull(p_mt_s_times, 0) + isnull(p_mt_ll_times, 0) - isnull(p_mt_xh_times, 0), a.p_mt_e_amt = isnull(p_mt_s_amt, 0) + isnull(p_mt_ll_amt, 0) - isnull(p_mt_xh_amt, 0) from #tbl_proc_consume a --计算合计 update a set a.c_sum_ll_times = isnull(c_mr_ll_times, 0) + isnull(c_mt_ll_times, 0), a.c_sum_ll_amt = isnull(c_mr_ll_amt, 0) + isnull(c_mt_ll_amt, 0), a.c_sum_xh_times = isnull(c_mr_xh_times, 0) + isnull(c_mt_xh_times, 0), a.c_sum_xh_amt = isnull(c_mr_xh_amt, 0) + isnull(c_mt_xh_amt, 0), a.c_sum_s_times = isnull(c_mr_s_times, 0) + isnull(c_mt_s_times, 0), a.c_sum_s_amt = isnull(c_mr_s_amt, 0) + isnull(c_mt_s_amt, 0), a.c_sum_e_times = isnull(c_mr_e_times, 0) + isnull(c_mt_e_times, 0), a.c_sum_e_amt = isnull(c_mr_e_amt, 0) + isnull(c_mt_e_amt, 0), a.p_sum_ll_times = isnull(p_mr_ll_times, 0) + isnull(p_mt_ll_times, 0), a.p_sum_ll_amt = isnull(p_mr_ll_amt, 0) + isnull(p_mt_ll_amt, 0), a.p_sum_xh_times = isnull(p_mr_xh_times, 0) + isnull(p_mt_xh_times, 0), a.p_sum_xh_amt = isnull(p_mr_xh_amt, 0) + isnull(p_mt_xh_amt, 0), a.p_sum_s_times = isnull(p_mr_s_times, 0) + isnull(p_mt_s_times, 0), a.p_sum_s_amt = isnull(p_mr_s_amt, 0) + isnull(p_mt_s_amt, 0), a.p_sum_e_times = isnull(p_mr_e_times, 0) + isnull(p_mt_e_times, 0), a.p_sum_e_amt = isnull(p_mr_e_amt, 0) + isnull(p_mt_e_amt, 0) from #tbl_proc_consume a --前期卡的金额,无法为准 update a set a.p_mr_s_amt = 0, a.p_mt_s_amt = 0, a.p_mr_e_amt = 0, a.p_mt_e_amt = 0, a.p_sum_s_amt = 0, a.p_sum_e_amt = 0 from #tbl_proc_consume a --这些公司别中找出有之公司的公司别 create table #related_tbl( parentid varchar(20) not null, sonid varchar(20) not null, primary key(parentid, sonid)) insert #related_tbl(parentid, sonid) select z.gaz01c, z.gaz02c from #tbl_proc_consume a, gam26 z with(nolock) where a.compid = z.gaz01c and z.gaz01c <> z.gaz02c --里面可能包括非叶子的节点 delete a from #related_tbl a where sonid in ( select parent_id from b_perf) declare @cur_compid varchar(20) declare cur_each_comp cursor for select parentid from #related_tbl order by parentid desc open cur_each_comp fetch cur_each_comp into @cur_compid while @@fetch_status = 0 begin delete #tbl_proc_consume where compid = @cur_compid insert #tbl_proc_consume(compid, fromdate, todate, p_mr_s_times, p_mr_s_amt, p_mr_ll_times, p_mr_ll_amt, p_mr_xh_times, p_mr_xh_amt, p_mr_e_times, p_mr_e_amt, p_mt_s_times, p_mt_s_amt, p_mt_ll_times, p_mt_ll_amt, p_mt_xh_times, p_mt_xh_amt, p_mt_e_times, p_mt_e_amt, p_sum_s_times, p_sum_s_amt, p_sum_ll_times, p_sum_ll_amt, p_sum_xh_times, p_sum_xh_amt, p_sum_e_times, p_sum_e_amt, c_mr_s_times, c_mr_s_amt, c_mr_ll_times, c_mr_ll_amt, c_mr_xh_times, c_mr_xh_amt, c_mr_e_times, c_mr_e_amt, c_mt_s_times, c_mt_s_amt, c_mt_ll_times, c_mt_ll_amt, c_mt_xh_times, c_mt_xh_amt, c_mt_e_times, c_mt_e_amt, c_sum_s_times, c_sum_s_amt, c_sum_ll_times, c_sum_ll_amt, c_sum_xh_times, c_sum_xh_amt, c_sum_e_times, c_sum_e_amt) select @cur_compid, @fromdate, @todate, sum(b.p_mr_s_times), sum(b.p_mr_s_amt), sum(b.p_mr_ll_times), sum(b.p_mr_ll_amt), sum(b.p_mr_xh_times), sum(b.p_mr_xh_amt), sum(b.p_mr_e_times), sum(b.p_mr_e_amt), sum(b.p_mt_s_times), sum(b.p_mt_s_amt ), sum(b.p_mt_ll_times), sum(b.p_mt_ll_amt), sum(b.p_mt_xh_times), sum(b.p_mt_xh_amt), sum(b.p_mt_e_times ), sum(b.p_mt_e_amt ), sum(b.p_sum_s_times), sum(b.p_sum_s_amt), sum(b.p_sum_ll_times), sum(b.p_sum_ll_amt), sum(b.p_sum_xh_times), sum(b.p_sum_xh_amt ), sum(b.p_sum_e_times), sum(b.p_sum_e_amt), sum(b.c_mr_s_times ), sum(b.c_mr_s_amt ), sum(b.c_mr_ll_times), sum(b.c_mr_ll_amt), sum(b.c_mr_xh_times), sum(b.c_mr_xh_amt), sum(b.c_mr_e_times), sum(b.c_mr_e_amt), sum(b.c_mt_s_times), sum(b.c_mt_s_amt), sum(b.c_mt_ll_times), sum(b.c_mt_ll_amt), sum(b.c_mt_xh_times), sum(b.c_mt_xh_amt), sum(b.c_mt_e_times), sum(b.c_mt_e_amt ), sum(b.c_sum_s_times), sum(b.c_sum_s_amt), sum(b.c_sum_ll_times), sum(b.c_sum_ll_amt), sum(b.c_sum_xh_times), sum(b.c_sum_xh_amt), sum(b.c_sum_e_times), sum(b.c_sum_e_amt) from #tbl_proc_consume b, #related_tbl c where b.compid = c.sonid and c.parentid = @cur_compid fetch cur_each_comp into @cur_compid end close cur_each_comp deallocate cur_each_comp drop table #related_tbl select seq,isnull(compid,'') compid,isnull(fromdate,'') fromdate,isnull(todate,'') todate,isnull(p_mr_s_times,0) p_mr_s_times, isnull(p_mr_s_amt,0) p_mr_s_amt, isnull(p_mr_ll_times,0) p_mr_ll_times,isnull(p_mr_ll_amt,0) p_mr_ll_amt, isnull(p_mr_xh_times,0) p_mr_xh_times,isnull(p_mr_xh_amt,0) p_mr_xh_amt,isnull(p_mr_e_times,0) p_mr_e_times, isnull(p_mr_e_amt,0) p_mr_e_amt,isnull(p_mt_s_times,0) p_mt_s_times,isnull(p_mt_s_amt,0) p_mt_s_amt, isnull(p_mt_ll_times,0) p_mt_ll_times,isnull(p_mt_ll_amt,0) p_mt_ll_amt,isnull(p_mt_xh_times,0) p_mt_xh_times, isnull(p_mt_xh_amt,0) p_mt_xh_amt,isnull(p_mt_e_times,0) p_mt_e_times,isnull(p_mt_e_amt,0) p_mt_e_amt, isnull(p_sum_s_times,0) p_sum_s_times,isnull(p_sum_s_amt,0) p_sum_s_amt,isnull(p_sum_ll_times,0) p_sum_ll_times, isnull(p_sum_ll_amt,0) p_sum_ll_amt,isnull(p_sum_xh_times,0) p_sum_xh_times,isnull(p_sum_xh_amt,0) p_sum_xh_amt, isnull(p_sum_e_times,0) p_sum_e_times,isnull(p_sum_e_amt,0) p_sum_e_amt,isnull(c_mr_s_times,0) c_mr_s_times, isnull(c_mr_s_amt,0) c_mr_s_amt,isnull(c_mr_ll_times,0) c_mr_ll_times,isnull(c_mr_ll_amt,0) c_mr_ll_amt, isnull(c_mr_xh_times,0) c_mr_xh_times,isnull(c_mr_xh_amt,0) c_mr_xh_amt,isnull(c_mr_e_times,0) c_mr_e_times, isnull(c_mr_e_amt,0) c_mr_e_amt,isnull(c_mt_s_times,0) c_mt_s_times,isnull(c_mt_s_amt,0) c_mt_s_amt, isnull(c_mt_ll_times,0) c_mt_ll_times,isnull(c_mt_ll_amt,0) c_mt_ll_amt,isnull(c_mt_xh_times,0) c_mt_xh_times, isnull(c_mt_xh_amt,0) c_mt_xh_amt,isnull(c_mt_e_times,0) c_mt_e_times,isnull(c_mt_e_amt,0) c_mt_e_amt, isnull(c_sum_s_times,0) c_sum_s_times,isnull(c_sum_s_amt,0) c_sum_s_amt,isnull(c_sum_ll_times,0) c_sum_ll_times, isnull(c_sum_ll_amt,0) c_sum_ll_amt,isnull(c_sum_xh_times,0) c_sum_xh_times,isnull(c_sum_xh_amt,0) c_sum_xh_amt, isnull(c_sum_e_times,0) c_sum_e_times,isnull(c_sum_e_amt,0) c_sum_e_amt from #tbl_proc_consume order by seq drop table #gcm08_buf drop table #tbl_buffer drop table #tbl_buffer_xh drop table #tbl_buffer_ll drop table #tbl_proc_consume end GO /****** Object: StoredProcedure [dbo].[upg_customer_inout_stock] Script Date: 06/12/2017 06:19:55 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO Create Proc [dbo].[upg_customer_inout_stock] ( @compid varchar(10), @fromdate varchar(10), --日期 @todate varchar(10), --日期 @fromcardno varchar(60), --卡号 @tocardno varchar(60), --卡号 @fromproduct varchar(60), --产品 @toproduct varchar(60), --产品 @type int --类型 0为全部 1为领取 2为入库 ) as begin Create table #customr_inout ( billno varchar(60), --单号 cardno varchar(60), --卡号 cardname varchar(100), --姓名 status varchar(5), --状态 stock varchar(10), --仓库号 stockname varchar(30), --仓库名 product varchar(30), --产品号 productname varchar(100), --产品名 unit varchar(10), --单位 num float, --数量 price float, --单价 total float, --总额 date varchar(10) --日期 ) --领取 if( @type = 0 or @type = 1) begin insert into #customr_inout select a.gia01c, --单号 a.gia05c, --领料人员 gia05cName = (select gba03c from gbm01 where gba00c = a.gia00c and gba23c = a.gia05c), '领取' status, a.gia04c, --仓库代码 gia04cName = (select gad02c from gam04 where gad00c = a.gia00c and gad01c = a.gia04c), b.gib03c, --产品代码 gib03cName = (select gfa03c from gfm01 where gfa00c = a.gia00c and gfa01c = b.gib03c), --b.gib06c, --出库单位 gib06c = (select gsb03c from gsm02 where gsb00c = b.gib00c and gsb01c = 'K' and gsb02c = b.gib06c), b.gib07f, --(出库单位)数量 b.gib09f, --单价(出库单位) b.gib10f, --金额 a.gia94d --覆核日期 from gim02 b with(nolock) inner join gim01 a with(nolock) on a.gia00c = b.gib00c and a.gia01c = b.gib01c where a.gia00c = @compid and a.gia94d between @fromdate and @todate and a.gia09i = 4 and isnull(a.gia93c,'') <> '' and (@fromcardno = '*' or (a.gia05c between @fromcardno and @tocardno)) and (@fromproduct = '*' or (b.gib03c between @fromproduct and @toproduct)) order by gia94d desc end if( @type = 0 or @type = 2) begin insert into #customr_inout select a.gha01c, c.gia05c, --领料人员/门店代码 gia05cName = (select gba03c from gbm01 where gba00c = c.gia00c and gba23c = c.gia05c), '入库' status, a.gha04c, --仓库代码 gha04cName = (select gad02c from gam04 where gad00c = a.gha00c and gad01c = a.gha04c), b.ghb03c, --产品代码 gib03cName = (select gfa03c from gfm01 where gfa00c = b.ghb00c and gfa01c = b.ghb03c), --b.ghb06c, --进货单位 ghb06cName = (select gsb03c from gsm02 where gsb00c = b.ghb00c and gsb01c = 'K' and gsb02c = b.ghb06c), b.ghb07f, --(进货单位)数量 b.ghb10f, --单价 b.ghb11f, --金额 a.gha94d from ghm02 b with(nolock) inner join ghm01 a with(nolock) on a.gha00c = b.ghb00c and a.gha01c = b.ghb01c inner join gim01 c with(nolock) on c.gia00c = a.gha00c and c.gia11c = a.gha01c and gia09i = 4 and isnull(gia93c,'') <> '' where a.gha00c = @compid and a.gha94d between @fromdate and @todate and a.gha07i = 1 and isnull(a.gha93c,'') <> '' and (@fromcardno = '*' or (c.gia05c between @fromcardno and @tocardno)) and (@fromproduct = '*' or (b.ghb03c between @fromproduct and @toproduct)) order by gha94d desc end Create table #total_inout ( in_num float, in_jine float, out_num float, out_jine float ) insert into #total_inout(in_num,in_jine,out_num,out_jine) select sum(isnull(num,0)),sum(isnull(total,0)),0,0 from #customr_inout where status = '入库' update #total_inout set out_num = num, out_jine = jine from (select sum(isnull(num,0)) num,sum(isnull(total,0)) jine from #customr_inout where status = '领取') as Atable select billno,cardno,cardname,status,stock,stockname,product,productname,unit,num,price,total,date from #customr_inout union all select '合计' as billno, '' as cardno,'' as cardname,'' as status,'' as stock,'' stockname,'' product, '' productname,'' unit, isnull(out_num,0) - isnull(in_num,0) as num,0 price, isnull(out_jine,0) - isnull(in_jine,0) total,'' date from #total_inout drop table #customr_inout drop table #total_inout end GO /****** Object: Table [dbo].[glm12] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[glm12]( [gll00c] [varchar](10) NOT NULL, [gll01v] [varchar](20) NOT NULL, [gll02f] [float] NOT NULL, [gll03v] [varchar](20) NULL, [gll04v] [varchar](20) NULL, [gll05v] [varchar](20) NULL, [gll06v] [varchar](20) NULL, [gll07v] [varchar](20) NULL, [gll08v] [varchar](20) NULL, [gll09f] [float] NULL, [gll10v] [varchar](20) NULL, [gll11v] [varchar](240) NULL, [gll12i] [int] NULL, [gll13f] [float] NULL, [gll14f] [float] NULL, [gll15f] [float] NULL, [gll16i] [int] NULL, [gll17c] [varchar](5) NULL, [gll18c] [varchar](5) NULL, [gll19c] [varchar](5) NULL, CONSTRAINT [PK_GLM12] PRIMARY KEY CLUSTERED ( [gll00c] ASC, [gll01v] ASC, [gll02f] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_emergestat] Script Date: 06/12/2017 06:20:02 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_emergestat] ( @condition nvarchar(4000), @compid varchar(10) -- 公司别 ) as begin create table #month_cost ( compid varchar(10) null, orderid varchar(20) null, ddate varchar(8) null, itemid varchar(20) null, itemname varchar(80) null, outtype varchar(40) null, outclass varchar(4) null, unit varchar(8) null, mode int null, person varchar(20) null, totalqty float null, totalamt float null, seq int ) --add by lzh 20101228 create table #month_cost1 ( ddate varchar(8) null, itemid varchar(20) null, itemname varchar(80) null, outtype varchar(40) null, provider varchar(20) null, unit varchar(8) null, mode int null, person varchar(20) null, totalqty float null, totalamt float null, seq int ) declare @inserted nvarchar(100) declare @ended nvarchar(4000) set @inserted = N'insert #month_cost' set @ended = @inserted + @condition EXEC sp_executesql @ended insert #month_cost1 select ddate, itemid, itemname, case when outclass = '3' then '销货' else '耗用' end as outtype , gfa23c, unit, mode, person, totalqty, totalamt, seq from #month_cost, gfm01 where itemid = gfa01c and gfa00c = @compid and (outclass = '4' or outclass = '3') order by ddate, seq ---gim02中的(gib12c)供应商不为空 insert #month_cost1 select ddate, itemid, itemname, gsb03c, gib12c, unit, mode, person, totalqty, totalamt, seq from #month_cost, gim01, gim02, gsm02 where gsb01c = 'C2' and gsb00c = @compid and outtype = gsb02c and gib01c = gia01c and itemid = gib03c and gib01c = orderid and gib00c = @compid and outclass = '2' and gib12c is not null order by ddate, seq ---gim02中的(gib12c)供应商为空时(主要针对以前的产品)取产品资料中的(gfa23c)供应商 insert #month_cost1 select ddate, itemid, itemname, gsb03c, gfa23c, unit, mode, person, totalqty, totalamt, seq from #month_cost, gim01, gim02, gfm01, gsm02 where gsb01c = 'C2' and gsb00c = @compid and outtype = gsb02c and gib01c = gia01c and itemid = gib03c and itemid = gfa01c and gib03c = gfa01c and gib01c = orderid and gib00c = @compid and gfa00c = @compid and outclass = '2' and gib12c is null order by ddate, seq select ddate, itemid, itemname, outtype, provider, providername = gbt03c, unit, mode, person, totalqty, totalamt, seq from #month_cost1, gbm20 where gbt01c = provider and gbt00c = @compid order by ddate, seq drop table #month_cost drop table #month_cost1 end GO /****** Object: Table [dbo].[ing02] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[ing02]( [inb00c] [varchar](10) NOT NULL, [inb01c] [varchar](20) NOT NULL, [inb02c] [varchar](20) NOT NULL, [inb03f] [float] NOT NULL, [inb04c] [varchar](30) NOT NULL, [inb05f] [float] NOT NULL, [inb06f] [float] NOT NULL, [inb07c] [varchar](5) NULL, CONSTRAINT [PK_ING02] PRIMARY KEY CLUSTERED ( [inb00c] ASC, [inb01c] ASC, [inb02c] ASC, [inb04c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gcm14] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gcm14]( [gcp00c] [varchar](10) NOT NULL, [gcp01d] [varchar](8) NOT NULL, [gcp02c] [varchar](20) NULL, [gcp03d] [varchar](8) NULL ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[proc_set] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[proc_set]( [idd] [float] NOT NULL, [compid] [varchar](20) NOT NULL, [projid] [varchar](20) NOT NULL, [times] [float] NULL, [sendtimes] [float] NULL, [buyamt] [float] NULL, [saveamt] [float] NULL, [perf_mode] [int] NULL, [perf_set] [float] NULL, [comm_mode] [int] NULL, [comm_set] [float] NULL, [period] [float] NULL, [periodunit] [varchar](20) NULL, [unlimted] [int] NULL, [times_permonth] [int] NULL, [interval] [int] NULL, [state] [int] NULL, [memo] [varchar](120) NULL, [isdefault] [int] NULL, [free_perf_mode] [int] NULL, [free_perf_set] [float] NULL, [free_comm_mode] [int] NULL, [free_comm_set] [float] NULL, [fromdate] [varchar](8) NULL, [todate] [varchar](8) NULL, PRIMARY KEY CLUSTERED ( [compid] ASC, [projid] ASC, [idd] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[ham10] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[ham10]( [haj00c] [varchar](10) NOT NULL, [haj01c] [varchar](20) NOT NULL, [haj02p] [image] NULL, [haj03p] [image] NULL, [haj04p] [image] NULL, CONSTRAINT [PK_HAM10] PRIMARY KEY CLUSTERED ( [haj00c] ASC, [haj01c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gam06] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gam06]( [gaf00c] [varchar](10) NOT NULL, [gaf01c] [varchar](20) NOT NULL, [gaf02c] [varchar](5) NULL, [gaf03c] [varchar](20) NULL, [gaf04c] [varchar](5) NULL, [gaf05c] [varchar](20) NULL, [gaf06c] [varchar](20) NULL, [gaf07c] [varchar](20) NULL, [gaf08i] [int] NULL, [gaf09c] [varchar](5) NULL, CONSTRAINT [PK_GAM06] PRIMARY KEY CLUSTERED ( [gaf00c] ASC, [gaf01c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[P_tablePagination_total] Script Date: 06/12/2017 06:19:20 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create proc [dbo].[P_tablePagination_total] ( @tableName nvarchar(50), -- 表名 @sqlwhere nvarchar(2000) -- 查询语句 ) as begin declare @total varchar(10) declare @s nvarchar(max) set @s = 'select @total = count(1) from ' + @tableName + ' with(nolock) where 1 = 1 ' + @sqlwhere exec sp_executesql @s, N'@total varchar(20) output', @total output select @total end GO /****** Object: Table [dbo].[glm15] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[glm15]( [glp00c] [varchar](10) NOT NULL, [glp01c] [varchar](20) NOT NULL, [glp02f] [float] NOT NULL, [glp03c] [varchar](20) NULL, [glp04c] [varchar](5) NULL, [glp05f] [float] NULL, [glp06f] [float] NULL, [glp07f] [float] NULL, [glp08f] [float] NULL, [glp09c] [varchar](20) NULL, [glp10c] [varchar](20) NULL, [glp11c] [varchar](20) NULL, [glp12c] [varchar](20) NULL, [glp13i] [int] NULL, [glp14i] [int] NULL, CONSTRAINT [PK_glm15] PRIMARY KEY CLUSTERED ( [glp00c] ASC, [glp01c] ASC, [glp02f] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_cal_true_card_sale_amt] Script Date: 06/12/2017 06:19:35 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_cal_true_card_sale_amt] ( @compid varchar(10), -- 公司别 @pay_1 varchar(10), @pay_amt_1 float, @pay_2 varchar(10), @pay_amt_2 float, @pay_3 varchar(10), @pay_amt_3 float, @pay_4 varchar(10), @pay_amt_4 float, @trueamt float output ) as begin create table #tbl_pay_amt ( paymode varchar(10) not null, payamt float null, ex_rate float null, trueamt float null ) if isnull(@pay_1, '') <> '' begin insert #tbl_pay_amt(paymode, payamt) values(@pay_1, @pay_amt_1) end if isnull(@pay_2, '') <> '' begin insert #tbl_pay_amt(paymode, payamt) values(@pay_2, @pay_amt_2) end if isnull(@pay_3, '') <> '' begin insert #tbl_pay_amt(paymode, payamt) values(@pay_3, @pay_amt_3) end if isnull(@pay_4, '') <> '' begin insert #tbl_pay_amt(paymode, payamt) values(@pay_4, @pay_amt_4) end update a set a.ex_rate = b.gso05f from #tbl_pay_amt a, gsm15 b where a.paymode = b.gso02c and b.gso00c = @compid update a set a.trueamt = isnull(payamt, 0) * isnull(ex_rate, 0) from #tbl_pay_amt a select @trueamt = sum(isnull(trueamt, 0)) from #tbl_pay_amt drop table #tbl_pay_amt end GO /****** Object: Table [dbo].[calc_operator] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[calc_operator]( [id] [int] NOT NULL, [parent_id] [int] NULL, [calc_type] [int] NULL, [calc_name] [varchar](100) NULL, CONSTRAINT [PK_calc_operator] PRIMARY KEY CLUSTERED ( [id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_get_gfm01] Script Date: 06/12/2017 06:20:07 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_get_gfm01] ( @compid varchar(20), -- 公司代码 @gfa01c varchar(max) -- 产品编号集合 ) as set nocount on begin declare @str varchar(max) --存储当前已经出现过的产品编号 create table #gfm01 ( gfa01c varchar(20) ) create clustered index idx_gfm01_1 on #gfm01(gfa01c) set @str = N'insert into #gfm01 select gfa01c from gfm01 where gfa00c = '''+@compid+''' and gfa01c in('+''''+replace(@gfa01c,',',''',''')+''''+') and gfa10c <> 1 and gfa10c = 0' exec (@str) select a.gfa01c, gfa03c, gfa11f, gfa08c, (select gsb03c from gsm02 with(nolock) where gsb01c = 'K' and gsb02c = gfa07c and gsb00c = gfa00c) gfa07cName, gfa07c, gfa24c, (select gsb03c from gsm02 with(nolock) where gsb01c = 'F' and gsb02c = gfa05c and gsb00c = gfa00c) gfa05cName, gfa05c, gfa02c, (select gsb03c from gsm02 with(nolock) where gsb01c = 'Y' and gsb02c = gfa23c and gsb00c = gfa00c) gfa23cName, gfa23c, gfa12c, gfa47c = isnull(gfa47c, '0') from gfm01 a with(nolock) where gfa00c = @compid and gfa10c <> 1 and gfa10c = '0' and a.gfa01c not in (select gfa01c from #gfm01) drop table #gfm01 end GO /****** Object: StoredProcedure [dbo].[upg_cal_full_true_card_sale_amt] Script Date: 06/12/2017 06:19:32 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_cal_full_true_card_sale_amt] ( @compid varchar(10), -- 公司别 @billid varchar(20), -- 销售单号 @cardno varchar(20), -- 第一张卡 @trueamt float output ) as begin create table #tbl_pay_amt ( paymode varchar(10) not null, payamt float null, ) -- 售卡的第一支付 insert into #tbl_pay_amt(paymode, payamt) select gnb15c, gnb16f from gnm02 where gnb00c = @compid and gnb01c = @billid and gnb03c = @cardno and isnull(gnb15c, '') <> '' and isnull(gnb16f, 0) > 0 -- 售卡的第二支付 insert into #tbl_pay_amt(paymode, payamt) select gnb17c, gnb18f from gnm02 where gnb00c = @compid and gnb01c = @billid and gnb03c = @cardno and isnull(gnb17c, '') <> '' and isnull(gnb18f, 0) > 0 -- 售卡的第三支付 insert into #tbl_pay_amt(paymode, payamt) select gnb19c, gnb20f from gnm02 where gnb00c = @compid and gnb01c = @billid and gnb03c = @cardno and isnull(gnb19c, '') <> '' and isnull(gnb20f, 0) > 0 -- 售卡的第四支付 insert into #tbl_pay_amt(paymode, payamt) select gnb21c, gnb22f from gnm02 where gnb00c = @compid and gnb01c = @billid and gnb03c = @cardno and isnull(gnb21c, '') <> '' and isnull(gnb22f, 0) > 0 -- 疗程的第一支付 insert into #tbl_pay_amt(paymode, payamt) select gnc15c, gnc16f from gnm03 where gnc00c = @compid and gnc01c = @billid and isnull(gnc15c, '') <> '' and isnull(gnc16f, 0) > 0 -- 疗程的第二支付 insert into #tbl_pay_amt(paymode, payamt) select gnc17c, gnc18f from gnm03 where gnc00c = @compid and gnc01c = @billid and isnull(gnc17c, '') <> '' and isnull(gnc18f, 0) > 0 -- 疗程的第三支付 insert into #tbl_pay_amt(paymode, payamt) select gnc19c, gnc20f from gnm03 where gnc00c = @compid and gnc01c = @billid and isnull(gnc19c, '') <> '' and isnull(gnc20f, 0) > 0 -- 疗程的第四支付 insert into #tbl_pay_amt(paymode, payamt) select gnc23c, gnc24f from gnm03 where gnc00c = @compid and gnc01c = @billid and isnull(gnc23c, '') <> '' and isnull(gnc24f, 0) > 0 -- 套餐的第一支付 insert into #tbl_pay_amt(paymode, payamt) select gtf09c, gtf10f from gtc05 where gtf00c = @compid and gtf01c = @billid and isnull(gtf09c, '') <> '' and isnull(gtf10f, 0) > 0 -- 套餐的第二支付 insert into #tbl_pay_amt(paymode, payamt) select gtf11c, gtf12f from gtc05 where gtf00c = @compid and gtf01c = @billid and isnull(gtf11c, '') <> '' and isnull(gtf12f, 0) > 0 -- 套餐的第三支付 insert into #tbl_pay_amt(paymode, payamt) select gtf13c, gtf14f from gtc05 where gtf00c = @compid and gtf01c = @billid and isnull(gtf13c, '') <> '' and isnull(gtf14f, 0) > 0 -- 套餐的第四支付 insert into #tbl_pay_amt(paymode, payamt) select gtf15c, gtf16f from gtc05 where gtf00c = @compid and gtf01c = @billid and isnull(gtf15c, '') <> '' and isnull(gtf16f, 0) > 0 select @trueamt = sum(payamt * isnull(b.gso05f, 0)) from (select paymode, sum(payamt) payamt from #tbl_pay_amt group by paymode) T join gsm15 B on B.gso00c = @compid and T.paymode = B.gso02c set @trueamt = isnull(@trueamt, 0) drop table #tbl_pay_amt end GO /****** Object: Table [dbo].[gcm16] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gcm16]( [gcr00c] [varchar](10) NOT NULL, [gcr01c] [varchar](20) NOT NULL, [gcr02c] [varchar](20) NOT NULL, [gcr03f] [float] NULL, [gcr04f] [float] NULL, [gcr05f] [float] NULL, [gcr06f] [float] NULL, [gcr07f] [float] NULL, [gcr08f] [float] NULL, [gcr09f] [float] NULL, [gcr10f] [float] NULL, [gcr11d] [varchar](8) NULL, [gcr12d] [varchar](8) NULL, [gcr13i] [int] NULL, [gcr14f] [float] NULL, [gcr15c] [varchar](60) NULL, [gcr16i] [int] NULL, [gcr17i] [int] NOT NULL, [gcr18f] [float] NOT NULL, [gcr19i] [int] NULL, [gcr20i] [int] NULL, [gcr21i] [int] NULL, [gcr22i] [int] NULL, [gcr23i] [int] IDENTITY(1,1) NOT NULL, [gcr24d] [varchar](10) NULL, [gcr25c] [varchar](20) NULL, [gcr26i] [int] NULL, [gcr31i] [int] NULL, [gcr32i] [int] NULL, [gcr33c] [varchar](20) NULL, [gcr34c] [varchar](10) NULL, [gcr35c] [varchar](10) NULL, [gcr36f] [float] NULL, [gcr37i] [int] NULL, CONSTRAINT [PK_GCM16] PRIMARY KEY CLUSTERED ( [gcr00c] ASC, [gcr23i] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[proc_set_perf_comm] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[proc_set_perf_comm]( [compid] [varchar](20) NOT NULL, [projid] [varchar](20) NOT NULL, [projtype] [varchar](10) NOT NULL, [perf_mode] [int] NULL, [perf_set] [float] NULL, [comm_mode] [int] NULL, [comm_set] [float] NULL, [free_perf_mode] [int] NULL, [free_perf_set] [float] NULL, [free_comm_mode] [int] NULL, [free_comm_set] [float] NULL, PRIMARY KEY CLUSTERED ( [compid] ASC, [projid] ASC, [projtype] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gtc02] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gtc02]( [gtb00c] [varchar](10) NOT NULL, [gtb01c] [varchar](20) NOT NULL, [gtb02c] [varchar](20) NOT NULL, [gtb03f] [float] NULL, [gtb04f] [float] NULL, [gtb05c] [varchar](20) NULL, [gtb06i] [tinyint] NULL, [gtb07f] [float] NULL, [gtb08i] [tinyint] NULL, [gtb09f] [float] NULL, [gtb10i] [tinyint] NULL, [gtb11f] [float] NULL, [gtb12i] [tinyint] NULL, [gtb13f] [float] NULL, [gtb14i] [tinyint] NULL, [gtb15f] [float] NULL, [gtb16i] [tinyint] NULL, [gtb17f] [float] NULL, [gtb18i] [tinyint] NULL, [gtb19f] [float] NULL, [gtb20i] [tinyint] NULL, [gtb21f] [float] NULL, [gtb22i] [int] NULL, [gtb23c] [varchar](2) NULL, [gtb24c] [varchar](10) NULL, [gtb25c] [varchar](10) NULL, CONSTRAINT [PK_GTC02] PRIMARY KEY CLUSTERED ( [gtb00c] ASC, [gtb01c] ASC, [gtb02c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_foregift_report_ex] Script Date: 06/12/2017 06:20:03 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_foregift_report_ex] ( @compid varchar(10), @fromdate varchar(8), @todate varchar(8), @frommember varchar(20), @tomember varchar(20), @emp varchar(20), --保证金收取人 add by leiji on2012年10月31日 @paymode int ) -- 0 所有 1 -现金 2-银行卡 as begin declare @pay varchar(10) if @paymode = 0 select @pay = '*' else if @paymode = 1 select @pay = '1' else if @paymode = 2 select @pay = '6' create table #result ( idd int identity not null, -- 序号 seq int null, compid varchar(10) not null, memid varchar(20) not null, membername varchar(40) null, billno varchar(20) null, -- 收据编号 foregift float null, -- 保证金 foredate varchar(8) null, changeamt float null, -- 取用保证金 getamt float null, -- 退押金 gettype varchar(20) null, getddate varchar(8) null, remain float null, flag int null, paymode varchar(10) null, primary key(idd) ) create table #tbl_gbm17 ( memid varchar(20) not null, seq int not null, getamt float null, primary key(memid, seq) ) insert into #result(seq, compid, memid, membername, billno, foregift, foredate, changeamt, getamt, gettype, getddate, remain, flag, paymode) select gbl02f, gbl00c, gbl01c, gba03c, gbl14c, gbl04f, gbl05d, 0, 0, '', gbl08d, gbl11f, 0, gbl15c from gbm15 a with (nolock), gbm01 b with (nolock), gam26 z with (nolock) where gbl00c = z.gaz02c and z.gaz01c = @compid and (gbl06c = @emp or @emp = '*') and (gbl01c between @frommember and @tomember or @frommember = '*') and (gbl05d between @fromdate and @todate or gbl08d between @fromdate and @todate) and gbl00c = gba00c and gbl01c = gba01c and (@pay = '*' or (gbl15c = @pay)) --直接退得押金 insert #tbl_gbm17(memid, seq, getamt) select gbw01c, gbw02f, sum(gbw05f) from gbm17 a with (nolock), gbm15 b with (nolock) where a.gbw01c = b.gbl01c and a.gbw02f = b.gbl02f and (gbl01c between @frommember and @tomember or @frommember = '*') and (gbl05d between @fromdate and @todate or gbl08d between @fromdate and @todate) and gbw04c = 'BACK' group by gbw01c, gbw02f update a set a.getamt = b.getamt from #result a, #tbl_gbm17 b where a.seq = b.seq and a.memid = b.memid --取用的押金 delete #tbl_gbm17 insert #tbl_gbm17(memid, seq, getamt) select gbw01c, gbw02f, sum(gbw05f) from gbm17 a with (nolock), gbm15 b with (nolock) where a.gbw01c = b.gbl01c and a.gbw02f = b.gbl02f and (gbl01c between @frommember and @tomember or @frommember = '*') and (gbl05d between @fromdate and @todate or gbl08d between @fromdate and @todate) and gbw04c in ('gn', 'gz', 'gx') group by gbw01c, gbw02f update a set a.changeamt = b.getamt from #result a, #tbl_gbm17 b where a.seq = b.seq and a.memid = b.memid drop table #tbl_gbm17 insert into #result(compid, memid, membername, billno, foregift, foredate, changeamt, getamt, getddate, remain, flag, paymode) select compid, memid, membername, billno, sum(isnull(foregift, 0)), foredate, sum(isnull(changeamt, 0)), sum(isnull(getamt, 0)), getddate, sum(isnull(remain, 0)), 1, paymode from #result group by compid, memid, billno, foredate, getddate, membername, paymode delete #result where flag = 0 update a set a.gettype = '转销售' from #result a where isnull(changeamt, 0)>0 and isnull(getamt, 0) = 0 update a set a.gettype = '退回' from #result a where isnull(changeamt, 0) = 0 and isnull(getamt, 0)>0 update a set a.gettype = '转销售 + 退回' from #result a where isnull(changeamt, 0)>0 and isnull(getamt, 0)>0 select isnull(idd,'') idd, isnull(seq,'') seq, isnull(compid,'') compid, isnull(memid,'') memid, isnull(membername,'') membername, isnull(billno,'') billno, isnull(foregift,'') foregift, isnull(foredate,'') foredate, isnull(changeamt,'') changeamt, isnull(getamt,'') getamt, isnull(gettype,'') gettype, isnull(getddate,'') getddate, isnull(remain,'') remain, isnull(flag,'') flag, isnull(paymode,'') paymode from #result drop table #result end GO /****** Object: Table [dbo].[gnm01] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gnm01]( [gna00c] [varchar](10) NOT NULL, [gna01c] [varchar](20) NOT NULL, [gna02d] [varchar](8) NULL, [gna03c] [varchar](5) NULL, [gna04c] [varchar](20) NULL, [gna05c] [varchar](5) NULL, [gna06c] [varchar](30) NULL, [gna07c] [varchar](160) NULL, [gna08c] [varchar](6) NULL, [gna09c] [varchar](20) NULL, [gna10d] [varchar](8) NULL, [gna11c] [varchar](5) NULL, [gna12t] [varchar](6) NULL, [gna13c] [varchar](20) NULL, [gna14f] [float] NULL, [gna15c] [varchar](5) NULL, [gna16f] [float] NULL, [gna17f] [float] NULL, [gna18f] [float] NULL, [gna19f] [float] NULL, [gna20c] [varchar](10) NULL, [gna21c] [varchar](30) NULL, [gna22i] [int] NULL, [gna23d] [varchar](8) NULL, [gna24d] [varchar](8) NULL, [gna25c] [varchar](20) NULL, [gna26f] [float] NULL, [gna91c] [varchar](10) NULL, [gna92d] [varchar](8) NULL, [gna93c] [varchar](10) NULL, [gna94d] [varchar](8) NULL, [gna27f] [float] NULL, [gna28c] [varchar](10) NULL, [gna29f] [float] NULL, [gna30c] [varchar](10) NULL, [gna31f] [float] NULL, [gna32c] [varchar](10) NULL, [gna33f] [float] NULL, [gna34c] [varchar](10) NULL, [gna35f] [float] NULL, [gna36c] [varchar](10) NULL, [gna37f] [float] NULL, [gna38c] [varchar](10) NULL, [gna39f] [float] NULL, [gna40c] [varchar](10) NULL, [gna41f] [float] NULL, [gna42c] [varchar](10) NULL, [gna43f] [float] NULL, [gna44c] [varchar](10) NULL, [gna45f] [float] NULL, [gna46c] [varchar](10) NULL, [gna47f] [float] NULL, [gna48c] [varchar](10) NULL, [gna49f] [float] NULL, [gna50c] [varchar](10) NULL, [gna51f] [float] NULL, [gna52c] [varchar](10) NULL, [gna53f] [float] NULL, [gna54c] [varchar](10) NULL, [gna55f] [float] NULL, [gna56c] [varchar](10) NULL, [gna57f] [float] NULL, [gna58c] [varchar](10) NULL, [gna59f] [float] NULL, [gna60c] [varchar](10) NULL, [gna61f] [float] NULL, [gna62c] [varchar](10) NULL, [gna63f] [float] NULL, [gna64c] [varchar](10) NULL, [gna65f] [float] NULL, [gna66c] [varchar](20) NULL, [gna80d] [varchar](8) NULL, [gna67f] [float] NULL, [gna68f] [float] NULL, [gna69f] [float] NULL, [gna70f] [float] NULL, [gna71f] [float] NULL, [gna72f] [float] NULL, [gna73i] [int] NULL, [gna81c] [varchar](10) NULL, [gna82c] [varchar](10) NULL, [gna83c] [varchar](10) NULL, [gna85i] [int] NULL, [gna86f] [float] NULL, [gna87f] [float] NULL, [gna88f] [float] NULL, [gna89f] [float] NULL, [gna90f] [float] NULL, [gna91f] [float] NULL, [gna92f] [float] NULL, [gna99i] [int] NULL, [gna100c] [varchar](10) NULL, [gna101c] [varchar](10) NULL, [gna102c] [varchar](10) NULL, [gna103c] [varchar](10) NULL, [gna104c] [varchar](10) NULL, [gna105c] [varchar](10) NULL, [gna106c] [varchar](10) NULL, [gna107c] [varchar](20) NULL, [gna108c] [varchar](500) NULL, [gna109c] [varchar](50) NULL, [gna110c] [varchar](1) NULL, [gna111c] [varchar](1) NULL, [gna112c] [varchar](50) NULL, [gna113c] [varchar](2) NULL, CONSTRAINT [PK_INDEX_GNM01] PRIMARY KEY CLUSTERED ( [gna00c] ASC, [gna01c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_cal_full_true_card_charge_amt] Script Date: 06/12/2017 06:19:32 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_cal_full_true_card_charge_amt] ( @compid varchar(10), -- 公司别 @billid varchar(20), -- 销售单号 @trueamt float output ) as begin create table #tbl_pay_amt ( paymode varchar(10) not null, payamt float null, ) -- 充值的第一支付 insert into #tbl_pay_amt(paymode, payamt) select gcl67c, gcl68f from gcm10 where gcl00c = @compid and gcl01c = @billid and isnull(gcl67c, '') <> '' and isnull(gcl68f, 0) > 0 -- 充值的第二支付 insert into #tbl_pay_amt(paymode, payamt) select gcl69c, gcl70f from gcm10 where gcl00c = @compid and gcl01c = @billid and isnull(gcl69c, '') <> '' and isnull(gcl70f, 0) > 0 -- 充值的第三支付 insert into #tbl_pay_amt(paymode, payamt) select gcl71c, gcl72f from gcm10 where gcl00c = @compid and gcl01c = @billid and isnull(gcl71c, '') <> '' and isnull(gcl72f, 0) > 0 -- 充值的第四支付 insert into #tbl_pay_amt(paymode, payamt) select gcl73c, gcl74f from gcm10 where gcl00c = @compid and gcl01c = @billid and isnull(gcl73c, '') <> '' and isnull(gcl74f, 0) > 0 -- 疗程的第一支付 insert into #tbl_pay_amt(paymode, payamt) select gcm15c, gcm16f from gcm11 where gcm00c = @compid and gcm01c = @billid and isnull(gcm15c, '') <> '' and isnull(gcm16f, 0) > 0 -- 疗程的第二支付 insert into #tbl_pay_amt(paymode, payamt) select gcm17c, gcm18f from gcm11 where gcm00c = @compid and gcm01c = @billid and isnull(gcm17c, '') <> '' and isnull(gcm18f, 0) > 0 -- 疗程的第三支付 insert into #tbl_pay_amt(paymode, payamt) select gcm19c, gcm20f from gcm11 where gcm00c = @compid and gcm01c = @billid and isnull(gcm19c, '') <> '' and isnull(gcm20f, 0) > 0 -- 疗程的第四支付 insert into #tbl_pay_amt(paymode, payamt) select gcm23c, gcm24f from gcm11 where gcm00c = @compid and gcm01c = @billid and isnull(gcm23c, '') <> '' and isnull(gcm24f, 0) > 0 -- 套餐的第一支付 insert into #tbl_pay_amt(paymode, payamt) select gth09c, gth10f from gtc06 where gth00c = @compid and gth01c = @billid and isnull(gth09c, '') <> '' and isnull(gth10f, 0) > 0 -- 套餐的第二支付 insert into #tbl_pay_amt(paymode, payamt) select gth11c, gth12f from gtc06 where gth00c = @compid and gth01c = @billid and isnull(gth11c, '') <> '' and isnull(gth12f, 0) > 0 -- 套餐的第三支付 insert into #tbl_pay_amt(paymode, payamt) select gth13c, gth14f from gtc06 where gth00c = @compid and gth01c = @billid and isnull(gth13c, '') <> '' and isnull(gth14f, 0) > 0 -- 套餐的第四支付 insert into #tbl_pay_amt(paymode, payamt) select gth15c, gth16f from gtc06 where gth00c = @compid and gth01c = @billid and isnull(gth15c, '') <> '' and isnull(gth16f, 0) > 0 select @trueamt = sum(payamt * isnull(b.gso05f, 0)) from (select paymode, sum(payamt) payamt from #tbl_pay_amt group by paymode) T join gsm15 B on B.gso00c = @compid and T.paymode = B.gso02c set @trueamt = isnull(@trueamt, 0) drop table #tbl_pay_amt end GO /****** Object: Table [dbo].[calc_task] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[calc_task]( [task_id] [int] IDENTITY(1,1) NOT NULL, [task_comp] [varchar](10) NOT NULL, [calc_comp] [varchar](10) NOT NULL, [calc_type] [int] NOT NULL, [calc_time] [datetime] NULL, [fromdate] [varchar](8) NOT NULL, [todate] [varchar](8) NOT NULL, [success] [int] NULL, [consumed] [float] NULL, CONSTRAINT [PK_calc_task] PRIMARY KEY CLUSTERED ( [task_id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_recal_glm01_consume] Script Date: 06/12/2017 06:20:31 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_recal_glm01_consume] ( @compid varchar(10), @fromdate varchar(10), @todate varchar(10) ) as begin create table #tbl_glm01 ( compid varchar(10) not null, enterid varchar(20) not null, amt float null, ) declare @PK_Sql nvarchar(max) set @PK_Sql = ' alter table #tbl_glm01 add constraint PK_#tbl_glm01_' + convert(varchar(10), cast(floor(rand() * 1000000) as int)) + ' primary key clustered(compid, enterid)' exec sp_executesql @PK_Sql create table #tbl_ggm ( compid varchar(10) not null, enterid varchar(20) not null, amt float null, ) set @PK_Sql = ' alter table #tbl_ggm add constraint PK_#tbl_ggm_' + convert(varchar(10), cast(floor(rand() * 1000000) as int)) + ' primary key clustered(compid, enterid)' exec sp_executesql @PK_Sql insert #tbl_glm01(compid, enterid) select gla00c, gla01c from glm01 a with (nolock), gam26 z where gla00c = z.gaz02c and z.gaz01c = @compid and gla02d >= @fromdate and gla02d <= @todate insert #tbl_ggm(compid, enterid, amt) select gga00c, gga30c, sum(ggb11f) from ggm01 a with (nolock), ggm02 b with (nolock), gam26 z where a.gga00c = b.ggb00c and a.gga01c = b.ggb01c and a.gga00c = z.gaz02c and z.gaz01c = @compid and a.gga02d >= @fromdate and a.gga02d <= @todate group by gga00c, gga30c update a set a.amt = b.amt from #tbl_glm01 a, #tbl_ggm b where a.compid = b.compid and a.enterid = b.enterid delete #tbl_ggm insert #tbl_ggm(compid, enterid, amt) select gga00c, gga30c, sum(ggc11f) from ggm01 a with (nolock), ggm03 b with (nolock), gam26 z where a.gga00c = b.ggc00c and a.gga01c = b.ggc01c and a.gga00c = z.gaz02c and z.gaz01c = @compid and a.gga02d >= @fromdate and a.gga02d <= @todate group by gga00c, gga30c update a set a.amt = b.amt from #tbl_glm01 a, #tbl_ggm b where a.compid = b.compid and a.enterid = b.enterid update a set a.gla11f = b.amt from glm01 a, #tbl_glm01 b where a.gla00c = b.compid and a.gla01c = b.enterid drop table #tbl_ggm drop table #tbl_glm01 end GO /****** Object: Table [dbo].[gsm23] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[gsm23]( [q_index] [float] NOT NULL, [q_start] [float] NOT NULL, [q_end] [float] NOT NULL, [q_rate] [float] NOT NULL, [q_deduction] [float] NOT NULL ) ON [PRIMARY] GO /****** Object: StoredProcedure [dbo].[upg_get_card_snop] Script Date: 06/12/2017 06:20:05 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_get_card_snop] ( @compid varchar(10), -- 门店编号 @input varchar(100), -- 输入 @private int ) as begin if isnull(@input, '') = '' return create table #result ( gba00c varchar(10) null, -- 公司编号 gba01c varchar(20) null, -- 会员编号 gba03c varchar(40) null, -- 会员姓名 gba08c varchar(20) null, -- Mobile No. gba17d varchar(8) null, -- 出生日期 gba21c varchar(10) null, -- 农历生日 gba23c varchar(20) null, -- 会员卡号 gca02c varchar(5) null, -- 卡种 gca02cname varchar(80) null, -- 卡种名称 gba98c varchar(100) null, -- 会员助记码 gca08i int null, -- 状态 gca07d varchar(8) null, -- 有效日期 ) create clustered index idx_result_0001 on #result(gba00c,gca02c) create nonclustered index idx_result_0002 on #result(gba23c) if (@private <> 1 and @private <> 2) or isnull(@compid, '') = '' set @private = 0 if @private = 0 begin insert into #result(gba00c, gba01c, gba03c, gba08c, gba17d, gba21c, gba23c, gba98c, gca08i, gca07d) exec upg_get_card_snop_global @input end else if @private = 1 begin insert into #result(gba00c, gba01c, gba03c, gba08c, gba17d, gba21c, gba23c, gba98c, gca08i, gca07d) exec upg_get_card_snop_local @compid, @input end else if @private = 2 begin insert into #result(gba00c, gba01c, gba03c, gba08c, gba17d, gba21c, gba23c, gba98c, gca08i, gca07d) exec upg_get_card_snop_single @compid, @input end update a set a.gca02c = b.gca02c from #result a, gcm01 b where a.gba23c = b.gca01c and b.gca00c = b.gca13d update a set a.gca02cname = b.gak02c from #result a, gam10 b where a.gba00c = b.gak00c and a.gca02c = b.gak01c select gba00c, gba01c, gba03c, gba08c, gba17d, gba21c, gba23c, gba98c, gca02cname, gcc06f, gca08i, gca07d from #result left join (select distinct gcc00c, gcc01c, stuff((select ';' + rtrim(gsb03c + ':' + convert(nvarchar(100), gcc06f)) from gcm03 with(nolock) left join gsm02 with(nolock) on gsb01c = 'O' and gsb02c = gcc03i and gcc00c = gsb00c where k.gcc00c = gcc00c and k.gcc01c = gcc01c and gcc06f > 0 for xml path('')), 1, 1, '') gcc06f from gcm03 k with (nolock)) gcm03_1 on gcc00c = gba00c and gcc01c = gba23c where gca08i = 4 or gca08i = 5 or gca08i = 8 or gca08i = 7 drop table #result end GO /****** Object: Table [dbo].[gam07] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gam07]( [gag00c] [varchar](10) NOT NULL, [gag01c] [varchar](20) NOT NULL, [gag02c] [varchar](5) NOT NULL, [gag03c] [varchar](30) NULL, [gag04c] [varchar](30) NULL, [gag05c] [varchar](20) NULL, [gag06f] [float] NULL, [gag07f] [float] NULL, CONSTRAINT [PK_GAM07] PRIMARY KEY CLUSTERED ( [gag00c] ASC, [gag01c] ASC, [gag02c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gcm20] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gcm20]( [gct00c] [varchar](10) NOT NULL, [gct01i] [int] IDENTITY(1,1) NOT NULL, [gct02c] [varchar](20) NOT NULL, [gct03c] [varchar](20) NOT NULL, [gct04d] [varchar](8) NOT NULL, [gct05c] [varchar](5) NULL, [gct06c] [varchar](10) NULL, [gct07c] [varchar](20) NULL, [gct08c] [nvarchar](100) NULL, [gct09f] [float] NULL, [gct10f] [float] NULL, [gct11f] [float] NULL, [gct12f] [float] NULL, [gct13c] [varchar](10) NULL, [gct14c] [varchar](20) NULL, [gct15c] [varchar](20) NULL, [gct16c] [varchar](20) NULL, [gct17c] [varchar](20) NULL, [gct18c] [varchar](20) NULL, [gct19t] [varchar](10) NULL, [gct20c] [varchar](5) NULL, [gct21f] [float] NULL, CONSTRAINT [PK_GCM20] PRIMARY KEY NONCLUSTERED ( [gct00c] ASC, [gct01i] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gcm21] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gcm21]( [gcu00c] [varchar](10) NOT NULL, [gcu01c] [varchar](8) NOT NULL, [gcu02i] [int] NOT NULL, [gcu03c] [varchar](20) NOT NULL, [gcu04c] [varchar](60) NULL, [gcu05i] [int] NULL, [gcu06f] [float] NULL, [gcu07i] [int] NULL, [gcu08c] [varchar](10) NULL, [gcu09i] [int] NULL, [gcu10c] [varchar](19) NULL, CONSTRAINT [PK_GCM21] PRIMARY KEY CLUSTERED ( [gcu00c] ASC, [gcu01c] ASC, [gcu02i] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gsm03a] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gsm03a]( [gsc00c] [varchar](10) NOT NULL, [gsc01c] [varchar](20) NOT NULL, [gsc02c] [varchar](5) NOT NULL, [gsc03f] [float] NOT NULL, [gsc04c] [varchar](20) NULL, [gsc05f] [float] NULL, [gsc06f] [float] NULL, [gsc07c] [varchar](30) NULL, [gsc08d] [varchar](8) NULL, [gsc09t] [varchar](8) NULL, [gsc10c] [varchar](20) NULL, CONSTRAINT [PK_GSM03a] PRIMARY KEY NONCLUSTERED ( [gsc00c] ASC, [gsc01c] ASC, [gsc02c] ASC, [gsc03f] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_analyzer_emp] Script Date: 06/12/2017 06:19:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO Create procedure [dbo].[upg_analyzer_emp]( @compid varchar(20), @startTime varchar(20), @endTime varchar(20) ) as begin create table #emp_coll( compid varchar(20) null, empid varchar(20) null, empname varchar(50) null ) create table #member_emp( compid varchar(20) null, memberId varchar(20) null, empid varchar(20) null, empname varchar(50) null, gba99i int null, saleamt float null, chargeTotal float null, singleMemberAmt float null, validTotal float null, singleValidMemAmt float null ) create table #member_amt( memberId varchar(20) null, saleamt float null ) create table #member_emp_result( empid varchar(20) null, empname varchar(20) null, totalSale float null, chargeTotal float null, singleMemberAmt float null, validTotal float null, singleValidMemAmt float null ) if(@startTime = '' or @endTime = '') begin --求出本月第一天以及最后一天 set @startTime = CONVERT(CHAR(10),DATEADD(dd,-DAY(GETDATE())+1,GETDATE()),112) set @endTime = CONVERT(CHAR(10),DATEADD(ms,-3,DATEADD(mm,DATEDIFF(m,0,getdate())+1,0)),112) end insert into #emp_coll(compid,empid,empname) select haa00c,haa01c,haa02c from ham01 with(nolock) where haa00c = @compid insert into #member_emp(compid,memberId,empid,gba99i,empname) select gba00c,gba01c,gba10c,gba99i,empname from gbm01 with(nolock),#emp_coll with(nolock),gcm01 with(nolock) where gca00c = compid and gba00c = gca00c and gba01c = gca04c and gca00c = gca13d and gba10c = empid insert #member_amt select gcl03c,gsc05f from gcm10 with(nolock),gsm03 with(nolock) where gcl00c = gsc00c and gcl01c = gsc01c and gsc02c = 'gz' and gcl80d between @startTime and @endTime and gsc04c in ('1','6','ZFB','WX'); insert #member_amt select gna04c,gsc05f from gnm01 with(nolock),gsm03 with(nolock) where gna00c = gsc00c and gna01c = gsc01c and gsc02c = 'gn' and gna80d between @startTime and @endTime and gsc04c in ('1','6','ZFB','WX'); insert #member_amt select gga09c,gsc05f from ggm01 with(nolock),gsm03 with(nolock) where gga00c = gsc00c and gga01c = gsc01c and gsc02c = 'gx' and gga80d between @startTime and @endTime and gsc04c in ('1','6','ZFB','WX'); update a set a.saleamt = b.saleamt from #member_emp a,(select memberId,sum(saleamt) saleamt from #member_amt group by memberId) b where a.memberId = b.memberId insert into #member_emp_result(empid,empname,totalSale) select empid,empname,sum(saleamt) totalSale from #member_emp group by empid,empname update a set a.chargeTotal =b.chargeTotal from #member_emp_result a, (select empid,count(1) chargeTotal from #member_emp group by empid) b where a.empid = b.empid update #member_emp_result set singleMemberAmt = round((case when isnull(chargeTotal,0) = 0 then 0 else (totalSale/chargeTotal) end),0) declare @beginDate varchar(8),@endDate varchar(8) set @endDate = convert(varchar(8),getdate(),112); set @beginDate = convert(varchar(8),dateadd(month,-3,getdate()),112); update a set a.validTotal = c.ccount from #member_emp_result a, (select empid,count(1) ccount from (select empid,gct02c from gcm20 with(nolock),#member_emp where memberId = gct02c and gct00c = compid and gct04d between @beginDate and @endDate and isnull(gba99i,1) = 1 group by empid,gct02c) b group by empid) c where a.empid = c.empid print @beginDate print @endDate update #member_emp_result set singleValidMemAmt = round((case when isnull(validTotal,0) = 0 then 0 else (totalSale/validTotal) end),0) insert into #member_emp_result select empid,empname,0,0,0,0,0 from #emp_coll where empid not in (select empid from #member_emp_result) select empid,empname empName,isnull(round(totalSale,0),0) totalSale,isnull(round(chargeTotal,0),0) chargeTotal,isnull(round(singleMemberAmt,0),0) singleMemberAmt,isnull(round(validTotal,0),0) validTotal,isnull(round(singleValidMemAmt,0),0) singleValidMemAmt from #member_emp_result drop table #emp_coll drop table #member_emp drop table #member_amt drop table #member_emp_result end GO /****** Object: Table [dbo].[gnm02] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gnm02]( [gnb00c] [varchar](10) NOT NULL, [gnb01c] [varchar](20) NOT NULL, [gnb02c] [varchar](5) NULL, [gnb03c] [varchar](20) NOT NULL, [gnb04i] [int] NULL, [gnb05f] [float] NULL, [gnb06f] [float] NULL, [gnb07f] [float] NULL, [gnb08f] [float] NULL, [gnb09i] [int] NULL, [gnb10i] [int] NULL, [gnb11f] [float] NULL, [gnb12f] [float] NULL, [gnb13i] [int] NULL, [gnb15c] [varchar](5) NULL, [gnb16f] [float] NULL, [gnb17c] [varchar](5) NULL, [gnb18f] [float] NULL, [gnb19c] [varchar](5) NULL, [gnb20f] [float] NULL, [gnb21c] [varchar](5) NULL, [gnb22f] [float] NULL, [gnb23c] [varchar](10) NULL, [gnb24f] [float] NULL, CONSTRAINT [PK_INDEX_GNM02] PRIMARY KEY NONCLUSTERED ( [gnb00c] ASC, [gnb01c] ASC, [gnb03c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[time_add_plus] Script Date: 06/12/2017 06:19:21 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[time_add_plus] @starttime varchar(20), @endtime varchar(20), @resulttime varchar(20) output as begin declare @start int declare @end int declare @starthour int declare @endhour int declare @startminute int declare @endminute int if (isnull(@starttime, '') = '' or isnull(@starttime, '') = '') begin set @resulttime = '000000' return end if (@starttime = '000000') begin set @resulttime = @endtime return end if (@endtime = '000000') begin set @resulttime = @starttime return end set @starthour = cast(@starttime as int)/10000 set @startminute = cast(@starttime as int)/100 set @endhour = cast(@endtime as int)/10000 set @endminute = cast(@endtime as int)/100 set @start = @startminute - @starthour*100 set @end = @endminute - @endhour*100 set @start = @start + @end set @starthour = @starthour + @endhour set @starthour = @starthour + @start/60 set @start = @start%60 if LEN (cast(@starthour as varchar(2)))<2 begin set @resulttime = '0' + cast(@starthour as varchar(2)) end else begin set @resulttime = cast(@starthour as varchar(2)) end if LEN (cast(@start as varchar(2)))<2 begin set @resulttime = @resulttime + '0' + cast(@start as varchar(2)) end else begin set @resulttime = @resulttime + cast(@start as varchar(2)) end set @resulttime = @resulttime + '00' end GO /****** Object: Table [dbo].[calc_work_detail] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[calc_work_detail]( [person_id] [varchar](20) NOT NULL, [action_id] [tinyint] NOT NULL, [srvdate] [varchar](10) NOT NULL, [code] [varchar](20) NOT NULL, [name] [varchar](200) NULL, [payway] [varchar](20) NULL, [amt] [float] NULL, [quan] [float] NULL, [cost] [float] NULL, [comm] [float] NULL, [amt2] [float] NULL, [amt3] [float] NULL, [cardrate] [float] NULL, [stdprice] [float] NULL, [prj_type] [varchar](20) NULL, [share_rate] [float] NULL, [cls_flag] [int] NULL, [gender] [int] NULL, [billid] [varchar](20) NULL, [paycode] [varchar](20) NULL, [ex_rate] [float] NULL, [ggm02_seq] [float] NULL, [room_id] [varchar](20) NULL, [compid] [varchar](10) NULL, [cardid] [varchar](20) NULL, [memid] [varchar](40) NULL ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[ham11] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[ham11]( [hak00c] [varchar](10) NOT NULL, [hak01c] [varchar](20) NOT NULL, [hak02c] [varchar](20) NOT NULL, [hak03c] [varchar](40) NULL, CONSTRAINT [PK_HAM11] PRIMARY KEY CLUSTERED ( [hak00c] ASC, [hak01c] ASC, [hak02c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gtc04] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gtc04]( [gte00c] [varchar](10) NOT NULL, [gte01c] [varchar](20) NOT NULL, [gte02c] [varchar](20) NOT NULL, [gte03c] [varchar](20) NOT NULL, [gte04f] [float] NULL, [gte05f] [float] NULL, [gte06f] [float] NULL, [gte08c] [varchar](20) NULL, [gte09i] [int] IDENTITY(1,1) NOT NULL, [gte10c] [varchar](20) NULL, [gte11c] [varchar](50) NULL, [gte12i] [int] NULL, [gte13i] [int] NULL, [gte14c] [varchar](2) NULL, [gte15d] [varchar](10) NULL, [gte16c] [varchar](10) NULL, [gte17c] [varchar](10) NULL, CONSTRAINT [PK_GTC04] PRIMARY KEY CLUSTERED ( [gte00c] ASC, [gte01c] ASC, [gte09i] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[ham13] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[ham13]( [ham01c] [varchar](10) NOT NULL, [ham02i] [int] IDENTITY(1,1) NOT NULL, [ham03c] [varchar](20) NULL, [ham04c] [varchar](20) NULL, [ham05c] [varchar](10) NULL, [ham06c] [varchar](10) NULL, [ham07c] [varchar](80) NULL, [ham08c] [varchar](80) NULL, CONSTRAINT [PK_HAM13] PRIMARY KEY CLUSTERED ( [ham02i] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gam08] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gam08]( [gah00c] [varchar](10) NOT NULL, [gah01c] [varchar](5) NULL, [gah02c] [varchar](20) NOT NULL, [gah03d] [varchar](8) NULL, [gah04d] [varchar](8) NULL, [gah05c] [varchar](10) NULL, [gah06i] [int] NULL, [gah07c] [varchar](5) NULL, [gah08c] [varchar](20) NULL, [gah09d] [varchar](8) NULL, [gah10c] [varchar](20) NULL, [gah11c] [varchar](20) NULL, [gah12f] [float] NULL, [gah13c] [varchar](5) NULL, [gah14c] [varchar](20) NULL, [gah15d] [varchar](8) NULL, [gah16c] [varchar](20) NULL, [gah17c] [varchar](20) NULL, [gah18f] [float] NULL, [gah19i] [int] NULL, [gah20c] [varchar](10) NULL, [gah21c] [varchar](10) NULL, [gah22f] [float] NULL, [gah23c] [varchar](10) NULL, [gah24f] [float] NULL, [gah25c] [varchar](10) NULL, [gah26f] [float] NULL, [gah27c] [varchar](10) NULL, [gah28f] [float] NULL, [gah29c] [varchar](200) NULL, CONSTRAINT [PK_GAM08] PRIMARY KEY CLUSTERED ( [gah00c] ASC, [gah02c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_getmember_info_byemp] Script Date: 06/12/2017 06:20:10 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE procedure [dbo].[upg_getmember_info_byemp]( @compid varchar(20), @empid varchar(20), @startTime varchar(20), @endTime varchar(20) ) as begin create table #member_result( compid varchar(20) null, memberid varchar(100) null, membername varchar(100) null, mobile varchar(100) null, storeamt float null, --储值金额 surplus float null, --剩余疗程数 totalrechargeamt float null, --累计充值金额 totalconsumamt float null, --累计消费金额 monthchargeamt float null, --本月累计充值金额 monthconsumamt float null, --本月累计消费金额 firsttime varchar(20) null, --首次来店时间 lastconsumtime varchar(20) null --最后消费时间 ) create table #member_amt( memberId varchar(20) null, datestr varchar(20) null, saleamt float null ) insert #member_amt select gcl03c,gcl80d,gsc05f from gcm10 with(nolock),gsm03 with(nolock) where gcl00c = gsc00c and gcl01c = gsc01c and gsc02c = 'gz' and gcl80d between @startTime and @endTime and gsc04c in ('1','6','ZFB','WX'); insert #member_amt select gna04c,gna80d,gsc05f from gnm01 with(nolock),gsm03 with(nolock) where gna00c = gsc00c and gna01c = gsc01c and gsc02c = 'gn' and gna80d between @startTime and @endTime and gsc04c in ('1','6','ZFB','WX'); insert #member_amt select gga09c,gga80d,gsc05f from ggm01 with(nolock),gsm03 with(nolock) where gga00c = gsc00c and gga01c = gsc01c and gsc02c = 'gx' and gga80d between @startTime and @endTime and gsc04c in ('1','6','ZFB','WX'); insert into #member_result(compid,memberid,membername,mobile) select gba00c,gba01c,gba03c,gba08c from gbm01 with(nolock),gcm01 with(nolock) where gba00c = gca00c and gba01c = gca04c and gca00c = gca13d and gba10c = @empid and gba00c = @compid update b set b.storeamt = a.gcc06f from gcm03 a with(nolock),#member_result b where gcc00c = compid and memberid = gcc01c and gcc03i = 2 update d set d.surplus = c.gcf07f from #member_result d,(select gcf01c,sum(isnull(gcf07f,0)) gcf07f from gcm06 a with(nolock),#member_result b where gcf00c = compid and memberid = gcf01c and isnull(gcf07f,0) > 0 group by gcf01c) c where d.memberid = gcf01c update d set d.totalrechargeamt = c.saleamt from #member_result d,(select memberId,sum(saleamt) saleamt from #member_amt group by memberId) c where d.memberid = c.memberId update d set d.totalconsumamt = c.gct12f from #member_result d,(select gct02c,sum(isnull(gct12f,0)) gct12f from gcm20 with(nolock),#member_result b where gct00c = compid and memberid = gct02c and gct13c in ('gx') group by gct02c) c where d.memberid = gct02c if(@startTime = '' or @endTime = '') begin --求出本月第一天以及最后一天 set @startTime = CONVERT(CHAR(10),DATEADD(dd,-DAY(GETDATE())+1,GETDATE()),112) set @endTime = CONVERT(CHAR(10),DATEADD(ms,-3,DATEADD(mm,DATEDIFF(m,0,getdate())+1,0)),112) end update d set d.monthchargeamt = c.saleamt from #member_result d,(select memberId,sum(saleamt) saleamt from #member_amt where datestr between @startTime and @endTime group by memberId) c where d.memberid = c.memberId update d set d.monthconsumamt = c.gct12f from #member_result d,(select gct02c,sum(isnull(gct12f,0)) gct12f from gcm20 with(nolock),#member_result b where gct00c = compid and memberid = gct02c and gct13c in ('gx') and gct04d between @startTime and @endTime group by gct02c) c where d.memberid = gct02c update d set d.firsttime = c.gct04d from #member_result d,(select gct02c,min(gct04d) gct04d from gcm20 with(nolock),#member_result b where gct00c = compid and memberid = gct02c group by gct02c) c where d.memberid = gct02c update d set d.lastconsumtime = c.gct04d from #member_result d,(select gct02c,max(gct04d) gct04d from gcm20 with(nolock),#member_result b where gct00c = compid and memberid = gct02c and gct13c in ('gx') group by gct02c) c where d.memberid = gct02c select compid,memberid,membername,mobile, isnull(round(storeamt,0),0) storeamt,isnull(round(surplus,0),0) surplus,isnull(round(totalrechargeamt,0),0) totalrechargeamt,isnull(round(monthchargeamt,0),0) monthrechargeamt, isnull(round(totalconsumamt,0),0) totalconsumamt,isnull(round(monthconsumamt,0),0) monthconsumamt,firsttime,lastconsumtime from #member_result drop table #member_result drop table #member_amt end GO /****** Object: StoredProcedure [dbo].[time_sub_plus] Script Date: 06/12/2017 06:19:21 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[time_sub_plus] @starttime varchar(6), @endtime varchar(6), @resulttime varchar(6) output as begin declare @start int declare @end int declare @starthour int declare @endhour int declare @startminute int declare @endminute int if (isnull(@starttime, '') = '' or isnull(@starttime, '') = '') begin set @resulttime = '000000' return end if (@starttime = '000000') begin set @resulttime = @endtime return end if (@endtime = '000000') begin set @resulttime = @starttime return end if (@starttime<@endtime) begin set @resulttime = @starttime set @starttime = @endtime set @endtime = @resulttime end set @starthour = cast(@starttime as int)/10000 set @startminute = cast(@starttime as int)/100 set @endhour = cast(@endtime as int)/10000 set @endminute = cast(@endtime as int)/100 set @start = @startminute - @starthour*100 set @end = @endminute - @endhour*100 if (@start<@end) begin if (@starthour!= 0) set @starthour = @starthour - 1 else begin set @starthour = @starthour + 24 -1 end set @start = @start + 60 - @end end else set @start = @start - @end if (@starthour<@endhour) set @starthour = @starthour + 24 - @endhour else set @starthour = @starthour - @endhour if LEN (cast(@starthour as varchar(2)))<2 begin set @resulttime = '0' + cast(@starthour as varchar(2)) end else begin set @resulttime = cast(@starthour as varchar(2)) end if LEN (cast(@start as varchar(2)))<2 begin set @resulttime = @resulttime + '0' + cast(@start as varchar(2)) end else begin set @resulttime = @resulttime + cast(@start as varchar(2)) end set @resulttime = @resulttime + '00' end GO /****** Object: Table [dbo].[gcm22] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gcm22]( [gcp00c] [varchar](10) NOT NULL, [gcp01c] [varchar](20) NOT NULL, [gcp02f] [float] NOT NULL, [gcp03c] [varchar](20) NULL, [gcp04c] [varchar](20) NULL, [gcp05c] [varchar](5) NULL, [gcp06f] [float] NULL, [gcp07f] [float] NULL, CONSTRAINT [PK_GCM22] PRIMARY KEY CLUSTERED ( [gcp00c] ASC, [gcp01c] ASC, [gcp02f] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gam08p] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gam08p]( [gah00c] [varchar](10) NOT NULL, [gah02c] [varchar](20) NOT NULL, [gah02p] [image] NULL, [gah03c] [varchar](2) NULL, [gah04c] [numeric](19, 0) IDENTITY(1,1) NOT NULL, CONSTRAINT [PK_GAM08P] PRIMARY KEY CLUSTERED ( [gah04c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[recover_ggm01] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[recover_ggm01]( [id] [int] NOT NULL, [gga00c] [varchar](10) NOT NULL, [gga01c] [varchar](20) NOT NULL, [gga02d] [varchar](8) NULL, [gga03t] [varchar](6) NULL, [gga04t] [varchar](6) NULL, [gga05c] [varchar](20) NULL, [gga06c] [varchar](40) NULL, [gga07c] [varchar](10) NULL, [gga08c] [varchar](20) NULL, [gga09c] [varchar](20) NULL, [gga10c] [varchar](5) NULL, [gga11f] [float] NULL, [gga12c] [varchar](5) NULL, [gga13f] [float] NULL, [gga14f] [float] NULL, [gga15f] [float] NULL, [gga16f] [float] NULL, [gga17f] [float] NULL, [gga18f] [float] NULL, [gga19f] [float] NULL, [gga20c] [varchar](5) NULL, [gga21c] [varchar](20) NULL, [gga22c] [varchar](5) NULL, [gga23c] [varchar](5) NULL, [gga24c] [varchar](20) NULL, [gga25c] [varchar](20) NULL, [gga26f] [float] NULL, [gga27i] [int] NULL, [gga28c] [varchar](10) NULL, [gga29c] [varchar](5) NULL, [gga30c] [varchar](20) NULL, [gga31i] [int] NULL, [gga91c] [varchar](10) NULL, [gga92d] [varchar](8) NULL, [gga93c] [varchar](10) NULL, [gga94d] [varchar](8) NULL, [gga95c] [varchar](10) NULL, [gga96d] [varchar](8) NULL, [gga80d] [varchar](8) NULL, [gga81i] [int] NULL, [gga99i] [int] NULL, [gga82c] [varchar](2000) NULL, [gga83d] [datetime] NULL, [gga84c] [varchar](50) NULL, [memo] [varchar](400) NULL, [gga98c] [varchar](10) NULL, [gga82i] [int] NOT NULL, CONSTRAINT [PK_recover_GGM01] PRIMARY KEY NONCLUSTERED ( [id] ASC, [gga00c] ASC, [gga01c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_day_totalaccount] Script Date: 06/12/2017 06:19:57 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_day_totalaccount] ( @incompid varchar(10), -- 公司编号 @from varchar(8), -- date from @to varchar(8), -- date to @inc_expense int, -- 是否包括费用支出 @staff varchar(20) ) as--WUXM, 2002/08/12 begin declare @compid varchar(6) declare @type varchar(5) select @compid = @incompid if @compid = '001' select @compid = '%' create table #detail_account ( id int identity not null, type varchar(5) null, --note class note varchar(20) null, --note id class varchar(5) null, --note class cname varchar(40) null, --class name date varchar(8) null, --单据 date time varchar(6) null, --时间 brief varchar(40) null, --摘要 get float null default 0, --借方金额 out float null default 0, --贷方金额 balance float null default 0, --余额 primary key (id), ) create table #account_sum --返回的各种付款方式的加总表 ( id int identity not null, type varchar(5) null, --note class tname varchar(40) null, --type name get float null default 0, --借方金额 out float null default 0, --贷方金额 balance float null default 0, --余额 primary key (id), ) -- begin transaction p1 insert #account_sum(type, tname, get, out, balance) select gsb02c, gsb03c, 0, 0, 0 from gsm02 with (nolock) where gsb00c = @compid and gsb01c = '3' if @@error <> 0 goto error_handler declare cur_get_each_type cursor for select type from #account_sum open cur_get_each_type fetch cur_get_each_type into @type while @@fetch_status = 0 begin insert #detail_account(date, time, cname, note, brief, get, out, balance) exec upg_day_account @incompid, @type, @from, @to, @inc_expense, @staff, '*' update #detail_account set type = @type where isnull(type, '') = '' fetch cur_get_each_type into @type end close cur_get_each_type deallocate cur_get_each_type -- 把temp表汇总后更新account表 update #account_sum set get = (select sum(get) from #detail_account where #account_sum.type = #detail_account.type and isnull(#detail_account.type, '')<>'' group by #detail_account.type) if @@error <> 0 goto error_handler update #account_sum set out = (select sum(out) from #detail_account where #account_sum.type = #detail_account.type and isnull(#detail_account.type, '')<>'' group by #detail_account.type) if @@error <> 0 goto error_handler update #account_sum set balance = isnull(get, 0)-isnull(out, 0) if @@error <> 0 goto error_handler update #account_sum set get = 0 where isnull(get, 0) = 0 update #account_sum set out = 0 where isnull(out, 0) = 0 update #account_sum set balance = 0 where isnull(balance, 0) = 0 select type, tname, get, out, balance from #account_sum order by type --commit transaction p1 drop table #account_sum drop table #detail_account return error_handler: --rollback transaction p1 drop table #account_sum drop table #detail_account end GO /****** Object: Table [dbo].[gam10] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gam10]( [gak00c] [varchar](10) NOT NULL, [gak01c] [varchar](5) NOT NULL, [gak02c] [varchar](40) NULL, [gak03i] [int] NULL, [gak04i] [int] NULL, [gak05i] [int] NULL, [gak06i] [int] NULL, [gak07f] [float] NULL, [gak08c] [varchar](5) NULL, [gak09f] [float] NULL, [gak10f] [float] NULL, [gak11f] [float] NULL, [gak12f] [float] NULL, [gak13f] [float] NULL, [gak14i] [int] NULL, [gak15i] [int] NULL, [gak16f] [float] NULL, [gak17i] [tinyint] NULL, [gak18f] [float] NULL, [gak19f] [float] NULL, [gak20i] [tinyint] NULL, [gak21f] [float] NULL, [gak22f] [float] NULL, [gak23i] [int] NULL, [gak24f] [float] NULL, [gak25i] [tinyint] NULL, [gak26f] [float] NULL, [gak27i] [tinyint] NULL, [gak28f] [float] NULL, [gak29i] [int] NULL, [gak30i] [int] NULL, [gak31i] [float] NULL, [gak32f] [float] NULL, [gak33f] [float] NULL, [gak34f] [float] NULL, [gak35i] [int] NULL, [gak36f] [float] NULL, [gak37i] [int] NULL, [gak38f] [float] NULL, [gak40f] [float] NULL, [gak41i] [int] NULL, [gak42i] [int] NULL, [gak43f] [float] NULL, [gak44i] [int] NULL, [gak45f] [float] NULL, [gak46f] [float] NULL, [gak49i] [int] NULL, [gak50i] [int] NULL, [gak51i] [int] NULL, [gak73f] [float] NULL, [gak74i] [int] NULL, [gak75i] [int] NULL, [gak76i] [int] NULL, [gak77f] [float] NULL, [gak78i] [int] NULL, [gak79i] [int] NULL, [gak80i] [int] NULL, [gak81f] [float] NULL, [gak82c] [varchar](5) NULL, CONSTRAINT [PK_GAM10] PRIMARY KEY CLUSTERED ( [gak00c] ASC, [gak01c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gfm06p] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gfm06p]( [gff00c] [varchar](10) NOT NULL, [gff01c] [varchar](20) NOT NULL, [gff02p] [image] NULL, [gff03c] [varchar](2) NULL, [gff04c] [numeric](19, 0) IDENTITY(1,1) NOT NULL, CONSTRAINT [PK_GFM06P] PRIMARY KEY CLUSTERED ( [gff04c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gcm24] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gcm24]( [gcx00c] [varchar](10) NOT NULL, [gcx01c] [varchar](10) NOT NULL, [gcx02c] [varchar](20) NOT NULL, [gcx03d] [varchar](10) NULL, [gcx04c] [varchar](20) NULL, [gcx05c] [varchar](20) NULL, [gcx06c] [varchar](10) NULL, [gcx07c] [varchar](20) NULL, [gcx08f] [float] NULL, [gcx09c] [varchar](20) NULL, [gcx10f] [float] NULL, [gcx11c] [varchar](20) NULL, [gcx12f] [float] NULL, [gcx13f] [float] NULL, [gcx14f] [float] NULL, [gcx15f] [float] NULL, [gcx16f] [float] NULL, [gcx17f] [float] NULL, [gcx18f] [float] NULL, [gcx19f] [float] NULL, [gcx20f] [float] NULL, [gcx21i] [int] IDENTITY(1,1) NOT NULL, [gcx22i] [int] NULL, [gcx99i] [int] NULL, CONSTRAINT [PK_GCM24] PRIMARY KEY CLUSTERED ( [gcx00c] ASC, [gcx01c] ASC, [gcx02c] ASC, [gcx21i] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gtc05] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gtc05]( [gtf00c] [varchar](10) NOT NULL, [gtf01c] [varchar](20) NOT NULL, [gtf02c] [varchar](20) NOT NULL, [gtf03f] [float] NULL, [gtf04f] [float] NULL, [gtf05f] [float] NULL, [gtf06f] [float] NULL, [gtf07f] [float] NULL, [gtf08i] [int] NULL, [gtf09c] [varchar](5) NULL, [gtf10f] [float] NULL, [gtf11c] [varchar](5) NULL, [gtf12f] [float] NULL, [gtf13c] [varchar](5) NULL, [gtf14f] [float] NULL, [gtf15c] [varchar](5) NULL, [gtf16f] [float] NULL, [gtf17i] [int] NULL, [gtf18f] [float] NULL, [gtf25i] [int] NULL, [gtf26c] [varchar](2) NULL, [gtf19c] [int] NULL, [gtf20c] [varchar](20) NULL, [gtf21d] [varchar](20) NULL, [gtf22f] [float] NULL, [gtf23c] [varchar](20) NULL, [gtf24f] [float] NULL, CONSTRAINT [PK_GTC05] PRIMARY KEY CLUSTERED ( [gtf00c] ASC, [gtf01c] ASC, [gtf02c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[ham16] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[ham16]( [haq00c] [varchar](10) NOT NULL, [haq01c] [varchar](20) NOT NULL, [haq02f] [float] NOT NULL, [haq03f] [float] NULL, [haq04f] [float] NULL, [haq05i] [tinyint] NULL, [haq06f] [float] NULL, [haq08c] [varchar](5) NULL, CONSTRAINT [PK_HAM16] PRIMARY KEY CLUSTERED ( [haq00c] ASC, [haq01c] ASC, [haq02f] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gom01] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gom01]( [goa00c] [varchar](10) NOT NULL, [goa01c] [varchar](20) NOT NULL, [goa02d] [varchar](8) NULL, [goa03c] [varchar](20) NULL, [goa04c] [varchar](10) NULL, [goa05c] [varchar](10) NULL, [goa06c] [varchar](40) NULL, [goa07c] [varchar](40) NULL, [goa08c] [varchar](40) NULL, [goa09c] [varchar](40) NULL, [goa10i] [int] NULL, [goa11i] [int] NULL, [goa12d] [varchar](8) NULL, [goa13d] [varchar](8) NULL, [goa91c] [varchar](10) NULL, [goa92d] [varchar](8) NULL, [goa93c] [varchar](10) NULL, [goa94d] [varchar](8) NULL, CONSTRAINT [PK_goM01] PRIMARY KEY CLUSTERED ( [goa00c] ASC, [goa01c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_analyse_profit_depart] Script Date: 06/12/2017 06:19:26 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_analyse_profit_depart] ( @compid varchar(10), @fromdate varchar(10), @todate varchar(10), @flag int, -- flag 0: 计算员工提成 1: 不计算员工提成 @staff varchar(20), -- 收银人员 @fromdepart varchar(10), -- add by lyj 2007-09-06 @todepart varchar(10), @calmode int ) as begin exec upg_analyse_profit_inter_depart @compid, @fromdate, @todate, @flag, @staff, @fromdepart, @todepart, @calmode --add by lyj 2007-09-06 end GO /****** Object: Table [dbo].[gam10p] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gam10p]( [gak00c] [varchar](10) NOT NULL, [gak01c] [varchar](20) NOT NULL, [gak02p] [image] NULL, [gak03c] [varchar](2) NULL, [gak04c] [numeric](19, 0) IDENTITY(1,1) NOT NULL, CONSTRAINT [PK_GAM10P1] PRIMARY KEY CLUSTERED ( [gak04c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_staff_workeffectiveness] Script Date: 06/12/2017 06:20:42 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_staff_workeffectiveness] ( @compid varchar(10), -- 已经能支持各级别的公司 @fromdate varchar(10), -- 开始日期 @todate varchar(10), -- 结束日期 @fromstaff varchar(10), -- 员工开编号 @tostaff varchar(10), -- 员工结束编号 @sellgoodstime float, -- 销售一件产品消耗的时间 --分钟 @sellcardtime float -- 卖出一张卡消耗的时间 --分钟 ) as begin create table #workeffectiveness ( compid varchar(20) not null, -- 公司别 empid varchar(10) not null, -- 员工编号 empname varchar(20) null, -- 员工姓名 dutyid varchar(5) null, -- 员工职务代码 dutyname varchar(30) null, -- 员工职务名称 goodcount float null, -- 销售产品数量 goodamt float null, -- 销售产品金额 treatmentcount float null, -- 销售项目个数 treatmentamt float null, -- 销售项目金额 sellcardcount float null, -- 买卡数量 sellcardamt float null, -- 买卡金额 workdatecount int null, -- 正常上班的天数 takestimes float null, -- 总的耗用时间 sumamt float null, -- 总的销售额 ) create table #stafftakestimeandamt ( compid varchar(20) not null, -- 公司别 empid varchar(20) not null, -- 员工编号 takestime float null, -- 消耗总时间 sumamt float null, -- 销售总金额 ) create table #onestaffworkhistory -- 每个员工每个相同工作统计 ( compid varchar(20) not null, -- 公司别 empid varchar(10) not null, -- 员工编号 worktype varchar(10) null, -- 代码的类别 1-卡销售 2-疗程销售 3-项目 4-产品销售 5-卡充值 6-疗程充值 typeid varchar(20) null, -- 代码编号 sellcount float null, -- 数量 sellamt float null, -- 金额 taketime float null, -- 消耗时间 ) ---------------------------获得统计员工的基本信息--------------------------------------------- insert #workeffectiveness(compid, empid, empname, dutyid) select haa00c, haa01c, case haa27c when '3' then '(*)' else '' end + haa02c, haa25c from ham01 a with(nolock), gam26 z with(nolock) where haa00c = z.gaz02c and z.gaz01c = @compid and (haa01c between @fromstaff and @tostaff or @fromstaff = '*') update a set dutyname = b.gsb03c from #workeffectiveness a, gsm02 b with(nolock) where b.gsb00c = a.compid and b.gsb01c = 'R' and b.gsb02c = isnull(a.dutyid, '') ----------------------------获得有效的消费记录--------------------------------------- insert #onestaffworkhistory(compid, empid, worktype, typeid, sellcount, sellamt) select a.gct00c, c.empid, a.gct06c, a.gct07c, sum(a.gct11f), sum(a.gct12f) from gcm20 a with (nolock), gam26 z with(nolock), #workeffectiveness c where gct00c = z.gaz02c and z.gaz01c = @compid and gct04d between @fromdate and @todate and a.gct00c = c.compid and (gct15c = c.empid or gct16c = c.empid or gct17c = c.empid or gct18c = c.empid ) and (a.gct06c >= '1' and a.gct06c <= '6') group by a.gct00c, c.compid, c.empid, gct06c, gct07c order by c.empid -----------------------获得每个项目所需的时间------------------------------------------ update #onestaffworkhistory set taketime = case when worktype = '1' or worktype = '2' or worktype = '5' or worktype = '6' then sellcount*@sellcardtime when worktype = '3' then sellcount * (select (cast(isnull(gda21t, '0') as int) / 10000 * 60) + ((cast(isnull(gda21t, '0') as int) - cast(isnull(gda21t, '0') as int) / 10000 * 10000) / 100) from gdm01 with(nolock) where gda00c = compid and gda01c = typeid) when worktype = '4' then sellcount * @sellgoodstime else 0 end ---------------------获得员工的总时间与总金额-------------------------------- insert #stafftakestimeandamt(compid, empid, takestime, sumamt) select compid, empid, sum(taketime), sum(sellamt) from #onestaffworkhistory group by compid, empid order by empid -------------------更新每个员工的总时间和总金额---------------------------- update a set a.takestimes = b.takestime, a.sumamt = b.sumamt from #workeffectiveness a, #stafftakestimeandamt b where a.compid = b.compid and a.empid = b.empid ----------------------更新每个员工的物品, 项目, 与买卡的统计数据------------------------------------------- update a set goodcount = (select sum(sellcount) from #onestaffworkhistory b where a.compid = b.compid and a.empid = b.empid and worktype = '4'), goodamt = (select sum(sellamt) from #onestaffworkhistory b where a.compid = b.compid and a.empid = b.empid and worktype = '4'), treatmentcount = (select sum(sellcount) from #onestaffworkhistory b where a.compid = b.compid and a.empid = b.empid and worktype = '3'), treatmentamt = (select sum(sellamt) from #onestaffworkhistory b where a.compid = b.compid and a.empid = b.empid and worktype = '3'), sellcardcount = (select sum(sellcount) from #onestaffworkhistory b where a.compid = b.compid and a.empid = b.empid and (worktype = '1' or worktype = '2' or worktype = '5' or worktype = '6')), sellcardamt = (select sum(sellamt) from #onestaffworkhistory b where a.compid = b.compid and a.empid = b.empid and (worktype = '1' or worktype = '2' or worktype = '5' or worktype = '6')) from #workeffectiveness a ------------------更新每个员工的排班天数--------------------------------- update a set workdatecount = (select count(hda02d) from hdm01 b where a.compid = b.hda00c and a.empid = b.hda01c and hda02d between @fromdate and @todate) from #workeffectiveness a select isnull(compid,'') compid, isnull(empid,'') empid, isnull(empname,'') empname, isnull(dutyid,'') dutyid, isnull(dutyname,'') dutyname, isnull(goodcount,0) goodcount, isnull(goodamt,0) goodamt, isnull(treatmentcount,0) treatmentcount, isnull(treatmentamt,0) treatmentamt, isnull(sellcardcount,0) sellcardcount, isnull(sellcardamt,0) sellcardamt, isnull(workdatecount,0) workdatecount, isnull(takestimes,0) takestimes, isnull(sumamt,0) sumamt from #workeffectiveness order by takestimes desc drop table #workeffectiveness drop table #stafftakestimeandamt drop table #onestaffworkhistory end GO /****** Object: StoredProcedure [dbo].[upg_detal_perf_total] Script Date: 06/12/2017 06:20:00 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_detal_perf_total] ( @fromdate varchar(10), @todate varchar(10) ) as begin create table #pay_cash ( compid varchar(10) not null,--公司编号 billid varchar(20) null, --单据 paytype varchar(10) null,--单据类型 paycode varchar(10) null,--支付方式 amt float null,--支付金额 ) create table #client ( compid varchar(10) not null, billid varchar(20) null, num float null, sex int null, ntype int null, ) create table #resut_tbl ( compid varchar(10) not null,--公司编号 compname varchar(50) null,--公司名称 totalamt float null,--总收入 zcamt float null,--支出金额 cashamt float null,--现金 backamt float null,--银行卡 wxamt float null,--微信支付 aliamt float null,--支付宝支付 mtamt float null,--美团支付 dzamt float null,--大众点评 zpamt float null,--支票 sumamt float null,--收入合计 -- cash_perf_mf float null,--现金美发 -- cash_perf_mr float null,--现金美容 -- card_perf_mf float null,--划卡美发 -- card_perf_mr float null,--划卡美容 cash_mf float null,--现金美发 cash_mr float null,--现金美容 card_mf float null,--划卡美发 card_mr float null,--划卡美容 dy_mf float null,--抵用券美发 dy_mr float null,--抵用券美容 kcard_mf float null,--开卡美发 kcard_mr float null,--开卡美容 wm_mf float null,--外卖美发 wm_mr float null,--外卖美容 zd_mf float null,--指定美发 zd_mr float null,--指定美容 man_mf float null,--美发男客 man_mr float null,--美容男客 momen_mf float null,--美发女客 momen_mr float null,--美容女客 man_mf_mr float null,--美容美发男客 momen_mf_mr float null,--美容美发女客 mf_total float null,--美发总客数 mr_total float null,--美容总客数 mr_mf_total float null,--美容美发总客数 totalclient float null,--总客数 tk_mf float null,--退卡美发 tk_mr float null,--退卡美容 dy_mf_count float null,--抵用券美发数量 dy_mf_amt float null,--抵用券美发业绩 dy_mr_count float null,--抵用券美容数量 dy_mr_amt float null,--抵用券美容业绩 jf_mf_count float null,--积分美发数量 jf_mf_amt float null,--积分美发业绩 jf_mr_count float null,--积分美容数量 jf_mr_amt float null,--积分美容业绩 cardcashamt_mf float null,--现金办卡业绩 cardcashamt_mr float null,--现金办卡业绩 def_mf float null, --欠款美发 def_mr float null, --欠款美容 cash_wm_mf float null,--现金外面美发 cash_wm_mf_count float null,--现金美发外卖数量 cash_wm_mr float null,--现金外面美容 cash_wm_mr_count float null,--现金美容外卖数量 card_wm_mf float null,--划卡外面美发 card_wm_mf_count float null,--划卡美发外卖数量 card_wm_mr float null,--划卡外面美容 card_wm_mr_count float null,--划卡美容外卖数量 cash_other_mf float null,--现金外面美发 cash_other_mf_count float null,--现金美发外卖数量 cash_other_mr float null,--现金外面美容 cash_other_mr_count float null,--现金美容外卖数量 card_other_mf float null,--划卡外面美发 card_other_mf_count float null,--划卡美发外卖数量 card_other_mr float null,--划卡外面美容 card_other_mr_count float null,--划卡美容外卖数量 total_mf_count float null,--美发总客数 total_mf_amt float null,--美发总业绩 total_mr_count float null,--美容总客数 total_mr_amt float null,--美容总业绩 ) create table #tbl_result ( compid varchar(10) not null,--公司编号 cash_mf float null,--现金美发 cash_mr float null,--现金美容 card_mf float null,--划卡美发 card_mr float null,--划卡美容 tk_mf float null,--退卡美发 tk_mr float null,--退卡美容 dy_mf float null,--抵用券美发 dy_mr float null,--抵用券美容 kcard_mf float null,--开卡美发 kcard_mr float null,--开卡美容 wm_mf float null,--外卖美发 wm_mr float null,--外卖美容 zd_mf float null,--指定美发 zd_mr float null,--指定美容 man_mf float null,--美发男客 man_mr float null,--美容男客 momen_mf float null,--美发女客 momen_mr float null,--美容女客 man_mf_mr float null,--美容美发男客 momen_mf_mr float null,--美容美发女客 dy_mf_count float null,--抵用券数量 dy_mf_amt float null,--抵用券业绩 dy_mr_count float null,--抵用券美容数量 dy_mr_amt float null,--抵用券美容业绩 jf_mf_count float null,--积分美发数量 jf_mf_amt float null,--积分美发业绩 jf_mr_count float null,--积分美容数量 jf_mr_amt float null,--积分美容业绩 cardcashamt_mf float null,--美发业绩 cardcashamt_mr float null,--美容业绩 def_mf float null, --欠款美发 def_mr float null, --欠款美容 cash_wm_mf float null,--现金外面美发 cash_wm_mf_count float null,--现金美发外卖数量 cash_wm_mr float null,--现金外面美容 cash_wm_mr_count float null,--现金美容外卖数量 card_wm_mf float null,--划卡外面美发 card_wm_mf_count float null,--划卡美发外卖数量 card_wm_mr float null,--划卡外面美容 card_wm_mr_count float null,--划卡美容外卖数量 cash_other_mf float null,--现金外面美发 cash_other_mf_count float null,--现金美发外卖数量 cash_other_mr float null,--现金外面美容 cash_other_mr_count float null,--现金美容外卖数量 card_other_mf float null,--划卡外面美发 card_other_mf_count float null,--划卡美发外卖数量 card_other_mr float null,--划卡外面美容 card_other_mr_count float null,--划卡美容外卖数量 ) create table #ggm01_ggm02_temp ( gga00c varchar(10) not null, gga01c varchar(20) not null, ggb02f float not null, ggb03c varchar(20) null, ggb05f float null, ggb11f float null, gga14f float null, --客数 ggb12c varchar(20) null, ggb13c varchar(20) null, ggb14c varchar(20) null, ggb15c varchar(20) null, ggb16c varchar(20) null, ggb19c varchar(20) null, gga31i int null, ggb81c varchar(20) null, prjtype varchar(20) null, ggb27c varchar(10) null, ntype int null, ) create table #ggm01_ggm03_temp ( gga00c varchar(10) not null, gga01c varchar(20) not null, ggc02f float not null, ggc03c varchar(20) null, ggc05f float null, ggc11f float null, ggc12c varchar(20) null, ggc15c varchar(20) null, ggc17c varchar(20) null, ggc21f float null, ggc23f float null, ggc25f float null, ggc27c varchar(10) null, ggc81c varchar(20) null, ggc82c varchar(20) null, ggc83c varchar(20) null, goodtype varchar(20) null, ) create table #gnm01_temp ( gna00c varchar(10) not null, gna01c varchar(20) not null, gna20c varchar(20) null, gna28c varchar(20) null, gna30c varchar(20) null, gna32c varchar(20) null, gna34c varchar(20) null, gna36c varchar(20) null, gna38c varchar(20) null, gna40c varchar(20) null, gna42c varchar(20) null, gna44c varchar(20) null, gna81c varchar(20) null, gna82c varchar(20) null, gna83c varchar(20) null, gna100c varchar(20) null, gna101c varchar(20) null, gna102c varchar(20) null, gna103c varchar(20) null, gna104c varchar(20) null, gna105c varchar(20) null, gna106c varchar(20) null, gna27f float null, gna29f float null, gna31f float null, gna33f float null, gna35f float null, gna37f float null, gna39f float null, gna41f float null, gna43f float null, gna45f float null, billcash float null,--现金总额 billcard float null,--划卡卡总额 billdy float null,--抵用券总额 billsumamt float null,--单据总金额 billdef float null,--欠款金额 ) create table #gcm10_temp ( gcl00c varchar(10) not null, gcl01c varchar(20) not null, gcl09c varchar(20) null, gcl17c varchar(20) null, gcl19c varchar(20) null, gcl22c varchar(20) null, gcl24c varchar(20) null, gcl26c varchar(20) null, gcl28c varchar(20) null, gcl30c varchar(20) null, gcl32c varchar(20) null, gcl34c varchar(20) null, gcl16f float null, gcl18f float null, gcl20f float null, gcl23f float null, gcl25f float null, gcl27f float null, gcl29f float null, gcl31f float null, gcl33f float null, gcl35f float null, gcl81c varchar(20) null, gcl82c varchar(20) null, gcl83c varchar(20) null, gcl100c varchar(20) null, gcl101c varchar(20) null, gcl102c varchar(20) null, gcl103c varchar(20) null, gcl104c varchar(20) null, gcl105c varchar(20) null, gcl106c varchar(20) null, billcash float null,--现金总额 billcard float null,--划卡总额 billdy float null,--抵用券总额 billsumamt float null,--单据总金额 billdef float null,--欠款金额 ) create table #tbl_item ( compid varchar(20) not null, item varchar(20) null, item_cashcount float null, item_cashamt float null, item_cardcount float null, item_cardamt float null, ) insert into #resut_tbl(compid,compname) select gae01c,gae03c from gam05 insert into #gcm10_temp(gcl00c,gcl01c,gcl09c,gcl17c,gcl19c,gcl22c,gcl24c,gcl26c,gcl28c,gcl30c,gcl32c,gcl34c, gcl16f,gcl18f,gcl20f,gcl23f,gcl25f,gcl27f,gcl29f,gcl31f,gcl33f,gcl35f,gcl81c,gcl82c,gcl83c,gcl100c, gcl101c,gcl102c,gcl103c,gcl104c,gcl105c,gcl106c) select gcl00c,gcl01c,gcl09c,gcl17c,gcl19c,gcl22c,gcl24c,gcl26c,gcl28c,gcl30c,gcl32c,gcl34c, gcl16f,gcl18f,gcl20f,gcl23f,gcl25f,gcl27f,gcl29f,gcl31f,gcl33f,gcl35f,gcl81c,gcl82c,gcl83c,gcl100c, gcl101c,gcl102c,gcl103c,gcl104c,gcl105c,gcl106c from gcm10 with(nolock) where gcl80d between @fromdate and @todate and gcl06i in(0,6,1) insert into #ggm01_ggm02_temp(gga00c,gga01c,ggb02f,ggb03c,ggb05f,ggb11f,ggb81c,prjtype,ggb27c,ggb12c,ggb13c,ggb14c,ggb15c,ggb16c,ggb19c,gga14f,gga31i) select gga00c,gga01c,ggb02f,ggb03c,ggb05f,ggb11f,ggb81c,gda13c,ggb27c,ggb12c,ggb13c,ggb14c,ggb15c,ggb16c,ggb19c,gga14f,gga31i from ggm01 with(nolock),ggm02 with(nolock),gdm01 with(nolock) where gga80d between @fromdate and @todate and gga00c=ggb00c and gga01c=ggb01c and ggb00c=gda00c and ggb03c=gda01c insert into #ggm01_ggm03_temp(gga00c,gga01c,ggc02f,ggc03c,ggc05f,ggc11f,ggc12c,ggc15c,ggc17c,ggc81c,ggc82c,ggc83c,goodtype,ggc21f,ggc23f,ggc25f,ggc27c) select gga00c,gga01c,ggc02f,ggc03c,ggc05f,ggc11f,ggc12c,ggc15c,ggc17c,ggc81c,ggc82c,ggc83c,gfa05c,ggc21f,ggc23f,ggc25f,ggc27c from ggm01 with(nolock),ggm03 with(nolock),gfm01 with(nolock) where gga80d between @fromdate and @todate and gga00c=ggc00c and gga01c=ggc01c and ggc00c=gfa00c and ggc03c=gfa01c update a set ggc81c=haa06c from #ggm01_ggm03_temp a,ham01 where gga00c=haa00c and ggc12c=haa01c and isnull(ggc12c,'')<>'' and isnull(ggc81c,'')='' update a set ggc82c=haa06c from #ggm01_ggm03_temp a,ham01 where gga00c=haa00c and ggc15c=haa01c and isnull(ggc15c,'')<>'' and isnull(ggc82c,'')='' update a set ggc83c=haa06c from #ggm01_ggm03_temp a,ham01 where gga00c=haa00c and ggc17c=haa01c and isnull(ggc17c,'')<>'' and isnull(ggc83c,'')='' --print '333333333333' insert into #gnm01_temp(gna00c,gna01c,gna20c,gna28c,gna30c,gna32c,gna34c,gna36c,gna38c,gna40c,gna42c,gna44c,gna81c, gna82c,gna83c,gna100c,gna101c,gna102c,gna103c,gna104c,gna105c,gna106c,gna27f,gna29f,gna31f,gna33f,gna35f,gna37f,gna39f,gna41f, gna43f,gna45f) select gna00c,gna01c,gna20c,gna28c,gna30c,gna32c,gna34c,gna36c,gna38c,gna40c,gna42c,gna44c,gna81c, gna82c,gna83c,gna100c,gna101c,gna102c,gna103c,gna104c,gna105c,gna106c,gna27f,gna29f,gna31f,gna33f,gna35f,gna37f,gna39f,gna41f, gna43f,gna45f from gnm01 with(nolock) where gna80d between @fromdate and @todate --print '333333333333' update a set a.billcash=(select sum(gsc05f) from gsm03 where gsc00c=gcl00c and gsc01c=gcl01c and gsc02c='gz' and gsc04c in('1','6','$','$1','$2')) from #gcm10_temp a --update a set a.billcard=(select sum(gsc05f) from gsm03 where gsc00c=gcl00c and gsc01c=gcl01c and gsc02c='gz' and gsc04c in ('#','A','4','9')) from #gcm10_temp a update a set a.billdy=(select sum(gsc05f) from gsm03 where gsc00c=gcl00c and gsc01c=gcl01c and gsc02c='gz' and gsc04c='3') from #gcm10_temp a update a set a.billsumamt=(select sum(gsc05f) from gsm03 where gsc00c=gcl00c and gsc01c=gcl01c and gsc02c='gz' and gsc04c<>'5') from #gcm10_temp a update a set a.billdef=(select sum(gsc05f) from gsm03 where gsc00c=gcl00c and gsc01c=gcl01c and gsc02c='gz' and gsc04c='5') from #gcm10_temp a update a set a.billcash=(select sum(gsc05f) from gsm03 where gsc00c=gna00c and gsc01c=gna01c and gsc02c='gn' and gsc04c in('1','6','$','$1','$2')) from #gnm01_temp a --update a set a.billcard=(select sum(gsc05f) from gsm03 where gsc00c=gna00c and gsc01c=gna01c and gsc02c='gn' and gsc04c in ('#','A','4','9')) from #gnm01_temp a update a set a.billdy=(select sum(gsc05f) from gsm03 where gsc00c=gna00c and gsc01c=gna01c and gsc02c='gn' and gsc04c='3') from #gnm01_temp a update a set a.billsumamt=(select sum(gsc05f) from gsm03 where gsc00c=gna00c and gsc01c=gna01c and gsc02c='gn' and gsc04c<>'5') from #gnm01_temp a update a set a.billdef=(select sum(gsc05f) from gsm03 where gsc00c=gna00c and gsc01c=gna01c and gsc02c='gn' and gsc04c='5') from #gnm01_temp a update #ggm01_ggm02_temp set ntype=case when prjtype between '01' and '09' then 1 else 2 end insert into #pay_cash(compid,billid,paytype,paycode,amt) select gga00c,gga01c,'gx',ggb27c,ggb11f from #ggm01_ggm02_temp where ggb27c in ('1','6','$','$1','$2','$3','$4') insert into #pay_cash(compid,billid,paytype,paycode,amt) select gga00c,gga01c,'gx',ggc27c,ggc11f from #ggm01_ggm03_temp where ggc27c in ('1','6','$','$1','$2','$3','$4') insert into #pay_cash(compid,billid,paytype,paycode,amt) select gsc00c,gsc01c,gsc02c,gsc04c,gsc05f from #gnm01_temp with(nolock),gsm03 with(nolock) where gsc02c='gn' and gna00c=gsc00c and gna01c=gsc01c and gsc04c in ('1','6','$','$1','$2','$3','$4') insert into #pay_cash(compid,billid,paytype,paycode,amt) select gsc00c,gsc01c,gsc02c,gsc04c,gsc05f from #gcm10_temp with(nolock),gsm03 with(nolock) where gsc02c='gz' and gcl00c=gsc00c and gcl01c=gsc01c and gsc04c in ('1','6','$','$1','$2','$3','$4') insert into #pay_cash(compid,billid,paytype,paycode,amt) select gsc00c,gsc01c,gsc02c,gsc04c,gsc05f from gem01 with(nolock),gsm03 with(nolock) where gea80d between @fromdate and @todate and gsc02c='ge' and gea00c=gsc00c and gea01c=gsc01c and gsc04c in ('1','6','$','$1','$2','$3','$4') update a set a.cashamt=b.amt from #resut_tbl a,(select compid,paycode,sum(amt) amt from #pay_cash group by compid,paycode) b where a.compid=b.compid and b.paycode='1' update a set a.backamt=b.amt from #resut_tbl a,(select compid,paycode,sum(amt) amt from #pay_cash group by compid,paycode) b where a.compid=b.compid and b.paycode='6' update a set a.zpamt=b.amt from #resut_tbl a,(select compid,paycode,sum(amt) amt from #pay_cash group by compid,paycode) b where a.compid=b.compid and b.paycode='$' update a set a.wxamt=b.amt from #resut_tbl a,(select compid,paycode,sum(amt) amt from #pay_cash group by compid,paycode) b where a.compid=b.compid and b.paycode='$1' update a set a.aliamt=b.amt from #resut_tbl a,(select compid,paycode,sum(amt) amt from #pay_cash group by compid,paycode) b where a.compid=b.compid and b.paycode='$2' update a set a.dzamt=b.amt from #resut_tbl a,(select compid,paycode,sum(amt) amt from #pay_cash group by compid,paycode) b where a.compid=b.compid and b.paycode='$4' update a set a.mtamt=b.amt from #resut_tbl a,(select compid,paycode,sum(amt) amt from #pay_cash group by compid,paycode) b where a.compid=b.compid and b.paycode='$3' update #resut_tbl set sumamt=isnull(cashamt,0)+isnull(backamt,0)+isnull(zpamt,0)+isnull(wxamt,0)+isnull(aliamt,0)+isnull(dzamt,0)+isnull(mtamt,0) --edit by zhangfu 20160504 update a set a.zcamt=b.amt from #resut_tbl a,(select gqb00c,sum(gqb05f) amt from gqm01 with(nolock),gqm02 with(nolock) where gqa80d between @fromdate and @todate and gqa00c=gqb00c and gqa01c=gqb01c and gqa03c='gv' group by gqb00c) b where a.compid=b.gqb00c update #resut_tbl set totalamt=isnull(sumamt,0)-isnull(zcamt,0)--+isnull(aliamt,0)+isnull(wxamt,0) insert into #tbl_result(compid,cash_mf,cash_mr) select gga00c,case when isnull(ggb81c,'004')<>'003' then ggb11f else 0 end, case when isnull(ggb81c,'004')='003' then ggb11f else 0 end from #ggm01_ggm02_temp where ggb27c in('1','6','$','$1','$2','$3','$4') --insert into #tbl_result(compid,cash_mf,cash_mr) --select gga00c,case when prjtype between '01' and '09' then ggb11f else 0 end, -- case when prjtype between '10' and '29' then ggb11f else 0 end -- from #ggm01_ggm02_temp --where ggb27c in('1','6','$') -- and isnull(ggb81c,'')='' insert into #tbl_result(compid,card_mf,card_mr) select gga00c,case when isnull(ggb81c,'004')<>'003' then ggb11f else 0 end, case when isnull(ggb81c,'004')='003' then ggb11f else 0 end from #ggm01_ggm02_temp where ggb27c in('A','#','4','9') --insert into #tbl_result(compid,card_mf,card_mr) --select gga00c,case when prjtype between '01' and '09' then ggb11f else 0 end, -- case when prjtype between '10' and '29' then ggb11f else 0 end -- from #ggm01_ggm02_temp --where ggb27c in('A','#','4','9') -- and isnull(ggb81c,'')='' insert into #tbl_result(compid,dy_mf,dy_mr) select gga00c,case when isnull(ggb81c,'004')<>'003' then ggb11f else 0 end, case when isnull(ggb81c,'004')='003' then ggb11f else 0 end from #ggm01_ggm02_temp where ggb27c='3' --insert into #tbl_result(compid,dy_mf,dy_mr) --select gga00c,case when prjtype between '01' and '09' then ggb11f else 0 end, -- case when prjtype between '10' and '29' then ggb11f else 0 end -- from #ggm01_ggm02_temp --where ggb27c='3' -- and isnull(ggb81c,'')<>'' insert into #tbl_result(compid,dy_mf,dy_mr) select gga00c,case when isnull(ggc81c,'004')<>'003' then ggc21f else 0 end, case when isnull(ggc81c,'004')='003' then ggc21f else 0 end from #ggm01_ggm03_temp where ggc27c='3' insert into #tbl_result(compid,cash_wm_mf,cash_wm_mr,cash_wm_mf_count,cash_wm_mr_count) select gga00c,case when isnull(ggc81c,'004')<>'003' then ggc21f else 0 end, case when isnull(ggc81c,'004')='003' then ggc21f else 0 end, case when isnull(ggc81c,'004')<>'003' then isnull(ggc21f,0)/case when isnull(ggc11f,0)=0 then 1 else ggc11f end else 0 end, case when isnull(ggc81c,'004')='003' then isnull(ggc21f,0)/case when isnull(ggc11f,0)=0 then 1 else ggc11f end else 0 end from #ggm01_ggm03_temp where ggc27c in('1','6','$','$1','$2','$3','$4') insert into #tbl_result(compid,card_wm_mf,card_wm_mr,card_wm_mf_count,card_wm_mr_count) select gga00c,case when isnull(ggc81c,'004')<>'003' then ggc21f else 0 end, case when isnull(ggc81c,'004')='003' then ggc21f else 0 end, case when isnull(ggc81c,'004')<>'003' then isnull(ggc21f,0)/case when isnull(ggc11f,0)=0 then 1 else ggc11f end else 0 end, case when isnull(ggc81c,'004')='003' then isnull(ggc21f,0)/case when isnull(ggc11f,0)=0 then 1 else ggc11f end else 0 end from #ggm01_ggm03_temp where ggc27c in('A','#','4','9') insert into #tbl_result(compid,card_mf,card_mr) select gga00c,case when isnull(ggc81c,'004')<>'003' then ggc21f else 0 end, case when isnull(ggc81c,'004')='003' then ggc21f else 0 end from #ggm01_ggm03_temp where ggc27c in('A','#','4','9') ------------------------------------------------------------- insert into #tbl_result(compid,cash_wm_mf,cash_wm_mr,cash_wm_mf_count,cash_wm_mr_count) select gga00c,case when isnull(ggc82c,'004')<>'003' then ggc23f else 0 end, case when isnull(ggc82c,'004')='003' then ggc23f else 0 end, case when isnull(ggc82c,'004')<>'003' then isnull(ggc23f,0)/case when isnull(ggc11f,0)=0 then 1 else ggc11f end else 0 end, case when isnull(ggc82c,'004')='003' then isnull(ggc23f,0)/case when isnull(ggc11f,0)=0 then 1 else ggc11f end else 0 end from #ggm01_ggm03_temp where ggc27c in ('1','6','$','$1','$2','$3','$4') insert into #tbl_result(compid,card_wm_mf,card_wm_mr,card_wm_mf_count,card_wm_mr_count) select gga00c,case when isnull(ggc82c,'004')<>'003' then ggc23f else 0 end, case when isnull(ggc82c,'004')='003' then ggc23f else 0 end, case when isnull(ggc82c,'004')<>'003' then isnull(ggc23f,0)/case when isnull(ggc11f,0)=0 then 1 else ggc11f end else 0 end, case when isnull(ggc82c,'004')='003' then isnull(ggc23f,0)/case when isnull(ggc11f,0)=0 then 1 else ggc11f end else 0 end from #ggm01_ggm03_temp where ggc27c in('A','#','4','9') insert into #tbl_result(compid,card_mf,card_mr) select gga00c,case when isnull(ggc82c,'004')<>'003' then ggc23f else 0 end, case when isnull(ggc82c,'004')='003' then ggc23f else 0 end from #ggm01_ggm03_temp where ggc27c in ('A','#','4','9') insert into #tbl_result(compid,dy_mf,dy_mr) select gga00c,case when isnull(ggc82c,'004')<>'003' then ggc23f else 0 end, case when isnull(ggc82c,'004')='003' then ggc23f else 0 end from #ggm01_ggm03_temp where ggc27c='3' -------------------------------------------------------------- insert into #tbl_result(compid,dy_mf,dy_mr) select gga00c,case when isnull(ggc83c,'004')<>'003' then ggc25f else 0 end, case when isnull(ggc83c,'004')='003' then ggc25f else 0 end from #ggm01_ggm03_temp where ggc27c='3' insert into #tbl_result(compid,card_mf,card_mr) select gga00c,case when isnull(ggc83c,'004')<>'003' then ggc25f else 0 end, case when isnull(ggc83c,'004')='003' then ggc25f else 0 end from #ggm01_ggm03_temp where ggc27c in ('A','#','4','9') insert into #tbl_result(compid,cash_wm_mf,cash_wm_mr,cash_wm_mf_count,cash_wm_mr_count) select gga00c,case when isnull(ggc83c,'004')<>'003' then ggc25f else 0 end, case when isnull(ggc83c,'004')='003' then ggc25f else 0 end, case when isnull(ggc83c,'004')<>'003' then isnull(ggc25f,0)/case when isnull(ggc11f,0)=0 then 1 else ggc11f end else 0 end, case when isnull(ggc83c,'004')='003' then isnull(ggc25f,0)/case when isnull(ggc11f,0)=0 then 1 else ggc11f end else 0 end from #ggm01_ggm03_temp where ggc27c in ('1','6','$','$1','$2','$3','$4') --and isnull(ggc83c,'')<>'' insert into #tbl_result(compid,card_wm_mf,card_wm_mr,card_wm_mf_count,card_wm_mr_count) select gga00c,case when isnull(ggc83c,'004')<>'003' then ggc25f else 0 end, case when isnull(ggc83c,'004')='003' then ggc25f else 0 end, case when isnull(ggc83c,'004')<>'003' then isnull(ggc25f,0)/case when isnull(ggc11f,0)=0 then 1 else ggc11f end else 0 end, case when isnull(ggc83c,'004')='003' then isnull(ggc25f,0)/case when isnull(ggc11f,0)=0 then 1 else ggc11f end else 0 end from #ggm01_ggm03_temp where ggc27c in('A','#','4','9') --and isnull(ggc83c,'')<>'' ---------------------------------------------------------------- ---------------------------------------------------------------- insert into #tbl_result(compid,dy_mf,dy_mr) select gga00c,case when isnull(ggc83c,'004')<>'003' then ggc11f else 0 end, case when isnull(ggc83c,'004')='003' then ggc11f else 0 end from #ggm01_ggm03_temp where ggc27c='3' and isnull(ggc21f,0)+isnull(ggc23f,0)+isnull(ggc25f,0)=0 insert into #tbl_result(compid,card_mf,card_mr) select gga00c,case when isnull(ggc83c,'004')<>'003' then ggc11f else 0 end, case when isnull(ggc83c,'004')='003' then ggc11f else 0 end from #ggm01_ggm03_temp where ggc27c in ('A','#','4','9') and isnull(ggc21f,0)+isnull(ggc23f,0)+isnull(ggc25f,0)=0 insert into #tbl_result(compid,cash_wm_mf,cash_wm_mr,cash_wm_mf_count,cash_wm_mr_count) select gga00c,case when isnull(ggc83c,'004')<>'003' then ggc11f else 0 end, case when isnull(ggc83c,'004')='003' then ggc11f else 0 end, case when isnull(ggc83c,'004')<>'003' then ggc05f else 0 end, case when isnull(ggc83c,'004')='003' then ggc05f else 0 end from #ggm01_ggm03_temp where ggc27c in ('1','6','$','$1','$2','$3','$4') and isnull(ggc21f,0)+isnull(ggc23f,0)+isnull(ggc25f,0)=0 --and isnull(ggc83c,'')<>'' insert into #tbl_result(compid,card_wm_mf,card_wm_mr,card_wm_mf_count,card_wm_mr_count) select gga00c,case when isnull(ggc83c,'004')<>'003' then ggc11f else 0 end, case when isnull(ggc83c,'004')='003' then ggc11f else 0 end, case when isnull(ggc83c,'004')<>'003' then ggc05f else 0 end, case when isnull(ggc83c,'004')='003' then ggc05f else 0 end from #ggm01_ggm03_temp where ggc27c in('A','#','4','9') and isnull(ggc21f,0)+isnull(ggc23f,0)+isnull(ggc25f,0)=0 --insert into #tbl_result(compid,cash_wm_mf,cash_wm_mr,cash_wm_mf_count,cash_wm_mr_count) --select gga00c,case when goodtype=1 then ggc11f else 0 end, -- case when goodtype in ('2','3','4') then ggc11f else 0 end, -- case when goodtype=1 then ggc05f else 0 end, -- case when goodtype in ('2','3','4') then ggc05f else 0 end --from #ggm01_ggm03_temp --where isnull(ggc81c,'')='' -- and isnull(ggc82c,'')='' -- and isnull(ggc83c,'')='' -- and ggc27c in('1','6','$') -- insert into #tbl_result(compid,card_wm_mf,card_wm_mr,card_wm_mf_count,card_wm_mr_count) --select gga00c,case when goodtype=1 then ggc11f else 0 end, -- case when goodtype in ('2','3','4') then ggc11f else 0 end, -- case when goodtype=1 then ggc05f else 0 end, -- case when goodtype in ('2','3','4') then ggc05f else 0 end --from #ggm01_ggm03_temp --where isnull(ggc81c,'')='' -- and isnull(ggc82c,'')='' -- and isnull(ggc83c,'')='' -- and ggc27c in ('A','#','4','9') --insert into #tbl_result(compid,card_mf,card_mr) --select gga00c,case when goodtype=1 then ggc11f else 0 end, -- case when goodtype in ('2','3','4') then ggc11f else 0 end --from #ggm01_ggm03_temp --where isnull(ggc81c,'')='' -- and isnull(ggc82c,'')='' -- and isnull(ggc83c,'')='' -- and ggc27c in ('A','#','4','9') --insert into #tbl_result(compid,dy_mf,dy_mr) --select gga00c,case when goodtype=1 then ggc11f else 0 end, -- case when goodtype in ('2','3','4') then ggc11f else 0 end --from #ggm01_ggm03_temp --where isnull(ggc81c,'')='' -- and isnull(ggc82c,'')='' -- and isnull(ggc83c,'')='' -- and ggc27c='3' ----------------------------卖卡第一销售 insert into #tbl_result(compid,cardcashamt_mf,cardcashamt_mr) select gna00c,case when isnull(gna81c,'004')<>'003' then isnull(gna27f,0)*isnull(billcash,0) else 0 end, case when isnull(gna81c,'004')='003' then isnull(gna27f,0)*isnull(billcash,0) else 0 end from #gnm01_temp --where isnull(gna81c,'')<>'' insert into #tbl_result(compid,card_mf,card_mr) select gna00c,case when isnull(gna81c,'004')<>'003' then isnull(gna27f,0)*isnull(billcard,0) else 0 end, case when isnull(gna81c,'004')='003' then isnull(gna27f,0)*isnull(billcard,0) else 0 end from #gnm01_temp --where isnull(gna81c,'')<>'' insert into #tbl_result(compid,dy_mf,dy_mr) select gna00c,case when isnull(gna81c,'004')<>'003' then isnull(gna27f,0)*isnull(billdy,0) else 0 end, case when isnull(gna81c,'004')='003' then isnull(gna27f,0)*isnull(billdy,0) else 0 end from #gnm01_temp --where isnull(gna81c,'')<>'' insert into #tbl_result(compid,def_mf,def_mr) select gna00c,case when isnull(gna81c,'004')<>'003' then isnull(gna27f,0)*isnull(billdef,0) else 0 end, case when isnull(gna81c,'004')='003' then isnull(gna27f,0)*isnull(billdef,0) else 0 end from #gnm01_temp --where isnull(gna81c,'')<>'' ---------------------------------------------------卖卡第2销售 insert into #tbl_result(compid,cardcashamt_mf,cardcashamt_mr) select gna00c,case when isnull(gna82c,'004')<>'003' then isnull(gna29f,0)*isnull(billcash,0) else 0 end, case when isnull(gna82c,'004')='003' then isnull(gna29f,0)*isnull(billcash,0) else 0 end from #gnm01_temp --where isnull(gna82c,'')<>'' insert into #tbl_result(compid,card_mf,card_mr) select gna00c,case when isnull(gna82c,'004')<>'003' then isnull(gna29f,0)*isnull(billcard,0) else 0 end, case when isnull(gna82c,'004')='003' then isnull(gna29f,0)*isnull(billcard,0) else 0 end from #gnm01_temp --where isnull(gna82c,'')<>'' insert into #tbl_result(compid,dy_mf,dy_mr) select gna00c,case when isnull(gna82c,'004')<>'003' then isnull(gna29f,0)*isnull(billdy,0) else 0 end, case when isnull(gna82c,'004')='003' then isnull(gna29f,0)*isnull(billdy,0) else 0 end from #gnm01_temp --where isnull(gna82c,'')<>'' insert into #tbl_result(compid,def_mf,def_mr) select gna00c,case when isnull(gna82c,'004')<>'003' then isnull(gna29f,0)*isnull(billdef,0) else 0 end, case when isnull(gna82c,'004')='003' then isnull(gna29f,0)*isnull(billdef,0) else 0 end from #gnm01_temp --where isnull(gna82c,'')<>'' -----------------------------------------------卖卡第3销售 insert into #tbl_result(compid,cardcashamt_mf,cardcashamt_mr) select gna00c,case when isnull(gna83c,'004')<>'003' then isnull(gna31f,0)*isnull(billcash,0) else 0 end, case when isnull(gna83c,'004')='003' then isnull(gna31f,0)*isnull(billcash,0) else 0 end from #gnm01_temp --where isnull(gna83c,'')<>'' insert into #tbl_result(compid,card_mf,card_mr) select gna00c,case when isnull(gna83c,'004')<>'003' then isnull(gna31f,0)*isnull(billcard,0) else 0 end, case when isnull(gna83c,'004')='003' then isnull(gna31f,0)*isnull(billcard,0) else 0 end from #gnm01_temp --where isnull(gna83c,'')<>'' insert into #tbl_result(compid,dy_mf,dy_mr) select gna00c,case when isnull(gna83c,'004')<>'003' then isnull(gna31f,0)*isnull(billdy,0) else 0 end, case when isnull(gna83c,'004')='003' then isnull(gna31f,0)*isnull(billdy,0) else 0 end from #gnm01_temp --where isnull(gna83c,'')<>'' insert into #tbl_result(compid,def_mf,def_mr) select gna00c,case when isnull(gna83c,'004')<>'003' then isnull(gna31f,0)*isnull(billdef,0) else 0 end, case when isnull(gna83c,'004')='003' then isnull(gna31f,0)*isnull(billdef,0) else 0 end from #gnm01_temp --where isnull(gna83c,'')<>'' -----------------------------------------------卖卡第4销售 insert into #tbl_result(compid,cardcashamt_mf,cardcashamt_mr) select gna00c,case when isnull(gna100c,'004')<>'003' then isnull(gna33f,0)*isnull(billcash,0) else 0 end, case when isnull(gna100c,'004')='003' then isnull(gna33f,0)*isnull(billcash,0) else 0 end from #gnm01_temp --where isnull(gna100c,'')<>'' insert into #tbl_result(compid,card_mf,card_mr) select gna00c,case when isnull(gna100c,'004')<>'003' then isnull(gna33f,0)*isnull(billcard,0) else 0 end, case when isnull(gna100c,'004')='003' then isnull(gna33f,0)*isnull(billcard,0) else 0 end from #gnm01_temp --where isnull(gna100c,'')<>'' insert into #tbl_result(compid,dy_mf,dy_mr) select gna00c,case when isnull(gna100c,'004')<>'003' then isnull(gna33f,0)*isnull(billdy,0) else 0 end, case when isnull(gna100c,'004')='003' then isnull(gna33f,0)*isnull(billdy,0) else 0 end from #gnm01_temp --where isnull(gna100c,'')<>'' insert into #tbl_result(compid,def_mf,def_mr) select gna00c,case when isnull(gna100c,'004')<>'003' then isnull(gna33f,0)*isnull(billdef,0) else 0 end, case when isnull(gna100c,'004')='003' then isnull(gna33f,0)*isnull(billdef,0) else 0 end from #gnm01_temp --where isnull(gna100c,'')<>'' -----------------------------------------------卖卡第5销售 insert into #tbl_result(compid,cardcashamt_mf,cardcashamt_mr) select gna00c,case when isnull(gna101c,'004')<>'003' then isnull(gna35f,0)*isnull(billcash,0) else 0 end, case when isnull(gna101c,'004')='003' then isnull(gna35f,0)*isnull(billcash,0) else 0 end from #gnm01_temp --where isnull(gna101c,'')<>'' insert into #tbl_result(compid,card_mf,card_mr) select gna00c,case when isnull(gna101c,'004')<>'003' then isnull(gna35f,0)*isnull(billcard,0) else 0 end, case when isnull(gna101c,'004')='003' then isnull(gna35f,0)*isnull(billcard,0) else 0 end from #gnm01_temp --where isnull(gna101c,'')<>'' insert into #tbl_result(compid,dy_mf,dy_mr) select gna00c,case when isnull(gna101c,'004')<>'003' then isnull(gna35f,0)*isnull(billdy,0) else 0 end, case when isnull(gna101c,'004')='003' then isnull(gna35f,0)*isnull(billdy,0) else 0 end from #gnm01_temp --where isnull(gna101c,'')<>'' insert into #tbl_result(compid,def_mf,def_mr) select gna00c,case when isnull(gna101c,'004')<>'003' then isnull(gna35f,0)*isnull(billdef,0) else 0 end, case when isnull(gna101c,'004')='003' then isnull(gna35f,0)*isnull(billdef,0) else 0 end from #gnm01_temp --where isnull(gna101c,'')<>'' -----------------------------------------------卖卡第6销售 insert into #tbl_result(compid,cardcashamt_mf,cardcashamt_mr) select gna00c,case when isnull(gna102c,'004')<>'003' then isnull(gna37f,0)*isnull(billcash,0) else 0 end, case when isnull(gna102c,'004')='003' then isnull(gna37f,0)*isnull(billcash,0) else 0 end from #gnm01_temp --where isnull(gna102c,'')<>'' insert into #tbl_result(compid,card_mf,card_mr) select gna00c,case when isnull(gna102c,'004')<>'003' then isnull(gna37f,0)*isnull(billcard,0) else 0 end, case when isnull(gna102c,'004')='003' then isnull(gna37f,0)*isnull(billcard,0) else 0 end from #gnm01_temp --where isnull(gna102c,'')<>'' insert into #tbl_result(compid,dy_mf,dy_mr) select gna00c,case when isnull(gna102c,'004')<>'003' then isnull(gna37f,0)*isnull(billdy,0) else 0 end, case when isnull(gna102c,'004')='003' then isnull(gna37f,0)*isnull(billdy,0) else 0 end from #gnm01_temp --where isnull(gna102c,'')<>'' insert into #tbl_result(compid,def_mf,def_mr) select gna00c,case when isnull(gna102c,'004')<>'003' then isnull(gna37f,0)*isnull(billdef,0) else 0 end, case when isnull(gna102c,'004')='003' then isnull(gna37f,0)*isnull(billdef,0) else 0 end from #gnm01_temp --where isnull(gna102c,'')<>'' -----------------------------------------------卖卡第7销售 insert into #tbl_result(compid,cardcashamt_mf,cardcashamt_mr) select gna00c,case when isnull(gna103c,'004')<>'003' then isnull(gna39f,0)*isnull(billcash,0) else 0 end, case when isnull(gna103c,'004')='003' then isnull(gna39f,0)*isnull(billcash,0) else 0 end from #gnm01_temp --where isnull(gna103c,'')<>'' insert into #tbl_result(compid,card_mf,card_mr) select gna00c,case when isnull(gna103c,'004')<>'003' then isnull(gna39f,0)*isnull(billcard,0) else 0 end, case when isnull(gna103c,'004')='003' then isnull(gna39f,0)*isnull(billcard,0) else 0 end from #gnm01_temp --where isnull(gna103c,'')<>'' insert into #tbl_result(compid,dy_mf,dy_mr) select gna00c,case when isnull(gna103c,'004')<>'003' then isnull(gna39f,0)*isnull(billdy,0) else 0 end, case when isnull(gna103c,'004')='003' then isnull(gna39f,0)*isnull(billdy,0) else 0 end from #gnm01_temp --where isnull(gna103c,'')<>'' insert into #tbl_result(compid,def_mf,def_mr) select gna00c,case when isnull(gna103c,'004')<>'003' then isnull(gna39f,0)*isnull(billdef,0) else 0 end, case when isnull(gna103c,'004')='003' then isnull(gna39f,0)*isnull(billdef,0) else 0 end from #gnm01_temp --where isnull(gna102c,'')<>'' -----------------------------------------------卖卡第8销售 insert into #tbl_result(compid,cardcashamt_mf,cardcashamt_mr) select gna00c,case when isnull(gna104c,'004')<>'003' then isnull(gna41f,0)*isnull(billcash,0) else 0 end, case when isnull(gna104c,'004')='003' then isnull(gna41f,0)*isnull(billcash,0) else 0 end from #gnm01_temp --where isnull(gna104c,'')<>'' insert into #tbl_result(compid,card_mf,card_mr) select gna00c,case when isnull(gna104c,'004')<>'003' then isnull(gna41f,0)*isnull(billcard,0) else 0 end, case when isnull(gna104c,'004')='003' then isnull(gna41f,0)*isnull(billcard,0) else 0 end from #gnm01_temp --where isnull(gna104c,'')<>'' insert into #tbl_result(compid,dy_mf,dy_mr) select gna00c,case when isnull(gna104c,'004')<>'003' then isnull(gna41f,0)*isnull(billdy,0) else 0 end, case when isnull(gna104c,'004')='003' then isnull(gna41f,0)*isnull(billdy,0) else 0 end from #gnm01_temp --where isnull(gna104c,'')<>'' insert into #tbl_result(compid,def_mf,def_mr) select gna00c,case when isnull(gna104c,'004')<>'003' then isnull(gna41f,0)*isnull(billdef,0) else 0 end, case when isnull(gna104c,'004')='003' then isnull(gna41f,0)*isnull(billdef,0) else 0 end from #gnm01_temp --where isnull(gna104c,'')<>'' -----------------------------------------------卖卡第9销售 insert into #tbl_result(compid,cardcashamt_mf,cardcashamt_mr) select gna00c,case when isnull(gna105c,'004')<>'003' then isnull(gna43f,0)*isnull(billcash,0) else 0 end, case when isnull(gna105c,'004')='003' then isnull(gna43f,0)*isnull(billcash,0) else 0 end from #gnm01_temp --where isnull(gna105c,'')<>'' insert into #tbl_result(compid,card_mf,card_mr) select gna00c,case when isnull(gna105c,'004')<>'003' then isnull(gna43f,0)*isnull(billcard,0) else 0 end, case when isnull(gna105c,'004')='003' then isnull(gna43f,0)*isnull(billcard,0) else 0 end from #gnm01_temp --where isnull(gna105c,'')<>'' insert into #tbl_result(compid,dy_mf,dy_mr) select gna00c,case when isnull(gna105c,'004')<>'003' then isnull(gna43f,0)*isnull(billdy,0) else 0 end, case when isnull(gna105c,'004')='003' then isnull(gna43f,0)*isnull(billdy,0) else 0 end from #gnm01_temp --where isnull(gna105c,'')<>'' insert into #tbl_result(compid,def_mf,def_mr) select gna00c,case when isnull(gna105c,'004')<>'003' then isnull(gna43f,0)*isnull(billdef,0) else 0 end, case when isnull(gna105c,'004')='003' then isnull(gna43f,0)*isnull(billdef,0) else 0 end from #gnm01_temp --where isnull(gna105c,'')<>'' -----------------------------------------------卖卡第10销售 insert into #tbl_result(compid,cardcashamt_mf,cardcashamt_mr) select gna00c,case when isnull(gna106c,'004')<>'003' then isnull(gna45f,0)*isnull(billcash,0) else 0 end, case when isnull(gna106c,'004')='003' then isnull(gna45f,0)*isnull(billcash,0) else 0 end from #gnm01_temp --where isnull(gna106c,'')<>'' insert into #tbl_result(compid,card_mf,card_mr) select gna00c,case when isnull(gna106c,'004')<>'003' then isnull(gna45f,0)*isnull(billcard,0) else 0 end, case when isnull(gna106c,'004')='003' then isnull(gna45f,0)*isnull(billcard,0) else 0 end from #gnm01_temp --where isnull(gna106c,'')<>'' insert into #tbl_result(compid,dy_mf,dy_mr) select gna00c,case when isnull(gna106c,'004')<>'003' then isnull(gna45f,0)*isnull(billdy,0) else 0 end, case when isnull(gna106c,'004')='003' then isnull(gna45f,0)*isnull(billdy,0) else 0 end from #gnm01_temp --where isnull(gna106c,'')<>'' insert into #tbl_result(compid,def_mf,def_mr) select gna00c,case when isnull(gna106c,'004')<>'003' then isnull(gna45f,0)*isnull(billdef,0) else 0 end, case when isnull(gna106c,'004')='003' then isnull(gna45f,0)*isnull(billdef,0) else 0 end from #gnm01_temp --where isnull(gna106c,'')<>'' -----------------------------------------------充值第1销售 insert into #tbl_result(compid,cardcashamt_mf,cardcashamt_mr) select gcl00c,case when isnull(gcl81c,'004')<>'003' then isnull(gcl16f,0)*isnull(billcash,0) else 0 end, case when isnull(gcl81c,'004')='003' then isnull(gcl16f,0)*isnull(billcash,0) else 0 end from #gcm10_temp --where isnull(gcl81c,'')<>'' insert into #tbl_result(compid,card_mf,card_mr) select gcl00c,case when isnull(gcl81c,'004')<>'003' then isnull(gcl16f,0)*isnull(billcard,0) else 0 end, case when isnull(gcl81c,'004')='003' then isnull(gcl16f,0)*isnull(billcard,0) else 0 end from #gcm10_temp --where isnull(gcl81c,'')<>'' insert into #tbl_result(compid,dy_mf,dy_mr) select gcl00c,case when isnull(gcl81c,'004')<>'003' then isnull(gcl16f,0)*isnull(billdy,0) else 0 end, case when isnull(gcl81c,'004')='003' then isnull(gcl16f,0)*isnull(billdy,0) else 0 end from #gcm10_temp --where isnull(gcl81c,'')<>'' insert into #tbl_result(compid,def_mf,def_mr) select gcl00c,case when isnull(gcl81c,'004')<>'003' then isnull(gcl16f,0)*isnull(billdef,0) else 0 end, case when isnull(gcl81c,'004')='003' then isnull(gcl16f,0)*isnull(billdef,0) else 0 end from #gcm10_temp --where isnull(gcl81c,'')<>'' -----------------------------------------------充值第2销售 insert into #tbl_result(compid,cardcashamt_mf,cardcashamt_mr) select gcl00c,case when isnull(gcl82c,'004')<>'003' then isnull(gcl18f,0)*isnull(billcash,0) else 0 end, case when isnull(gcl82c,'004')='003' then isnull(gcl18f,0)*isnull(billcash,0) else 0 end from #gcm10_temp --where isnull(gcl82c,'')<>'' insert into #tbl_result(compid,card_mf,card_mr) select gcl00c,case when isnull(gcl82c,'004')<>'003' then isnull(gcl18f,0)*isnull(billcard,0) else 0 end, case when isnull(gcl82c,'004')='003' then isnull(gcl18f,0)*isnull(billcard,0) else 0 end from #gcm10_temp --where isnull(gcl82c,'')<>'' insert into #tbl_result(compid,dy_mf,dy_mr) select gcl00c,case when isnull(gcl82c,'004')<>'003' then isnull(gcl18f,0)*isnull(billdy,0) else 0 end, case when isnull(gcl82c,'004')='003' then isnull(gcl18f,0)*isnull(billdy,0) else 0 end from #gcm10_temp --where isnull(gcl82c,'')<>'' insert into #tbl_result(compid,def_mf,def_mr) select gcl00c,case when isnull(gcl82c,'004')<>'003' then isnull(gcl18f,0)*isnull(billdef,0) else 0 end, case when isnull(gcl82c,'004')='003' then isnull(gcl18f,0)*isnull(billdef,0) else 0 end from #gcm10_temp --where isnull(gcl82c,'')<>'' -----------------------------------------------充值第3销售 insert into #tbl_result(compid,cardcashamt_mf,cardcashamt_mr) select gcl00c,case when isnull(gcl83c,'004')<>'003' then isnull(gcl20f,0)*isnull(billcash,0) else 0 end, case when isnull(gcl83c,'004')='003' then isnull(gcl20f,0)*isnull(billcash,0) else 0 end from #gcm10_temp --where isnull(gcl83c,'')<>'' insert into #tbl_result(compid,card_mf,card_mr) select gcl00c,case when isnull(gcl83c,'004')<>'003' then isnull(gcl20f,0)*isnull(billcard,0) else 0 end, case when isnull(gcl83c,'004')='003' then isnull(gcl20f,0)*isnull(billcard,0) else 0 end from #gcm10_temp --where isnull(gcl83c,'')<>'' insert into #tbl_result(compid,dy_mf,dy_mr) select gcl00c,case when isnull(gcl83c,'004')<>'003' then isnull(gcl20f,0)*isnull(billdy,0) else 0 end, case when isnull(gcl83c,'004')='003' then isnull(gcl20f,0)*isnull(billdy,0) else 0 end from #gcm10_temp --where isnull(gcl83c,'')<>'' insert into #tbl_result(compid,def_mf,def_mr) select gcl00c,case when isnull(gcl83c,'004')<>'003' then isnull(gcl20f,0)*isnull(billdef,0) else 0 end, case when isnull(gcl83c,'004')='003' then isnull(gcl20f,0)*isnull(billdef,0) else 0 end from #gcm10_temp --where isnull(gcl83c,'')<>'' -----------------------------------------------充值第4销售 insert into #tbl_result(compid,cardcashamt_mf,cardcashamt_mr) select gcl00c,case when isnull(gcl100c,'004')<>'003' then isnull(gcl23f,0)*isnull(billcash,0) else 0 end, case when isnull(gcl100c,'004')='003' then isnull(gcl23f,0)*isnull(billcash,0) else 0 end from #gcm10_temp --where isnull(gcl100c,'')<>'' insert into #tbl_result(compid,card_mf,card_mr) select gcl00c,case when isnull(gcl100c,'004')<>'003' then isnull(gcl23f,0)*isnull(billcard,0) else 0 end, case when isnull(gcl100c,'004')='003' then isnull(gcl23f,0)*isnull(billcard,0) else 0 end from #gcm10_temp --where isnull(gcl100c,'')<>'' insert into #tbl_result(compid,dy_mf,dy_mr) select gcl00c,case when isnull(gcl100c,'004')<>'003' then isnull(gcl23f,0)*isnull(billdy,0) else 0 end, case when isnull(gcl100c,'004')='003' then isnull(gcl23f,0)*isnull(billdy,0) else 0 end from #gcm10_temp --where isnull(gcl100c,'')<>'' insert into #tbl_result(compid,def_mf,def_mr) select gcl00c,case when isnull(gcl100c,'004')<>'003' then isnull(gcl23f,0)*isnull(billdef,0) else 0 end, case when isnull(gcl100c,'004')='003' then isnull(gcl23f,0)*isnull(billdef,0) else 0 end from #gcm10_temp --where isnull(gcl100c,'')<>'' -----------------------------------------------充值第5销售 insert into #tbl_result(compid,cardcashamt_mf,cardcashamt_mr) select gcl00c,case when isnull(gcl101c,'004')<>'003' then isnull(gcl25f,0)*isnull(billcash,0) else 0 end, case when isnull(gcl101c,'004')='003' then isnull(gcl25f,0)*isnull(billcash,0) else 0 end from #gcm10_temp --where isnull(gcl101c,'')<>'' insert into #tbl_result(compid,card_mf,card_mr) select gcl00c,case when isnull(gcl101c,'004')<>'003' then isnull(gcl25f,0)*isnull(billcard,0) else 0 end, case when isnull(gcl101c,'004')='003' then isnull(gcl25f,0)*isnull(billcard,0) else 0 end from #gcm10_temp --where isnull(gcl101c,'')<>'' insert into #tbl_result(compid,dy_mf,dy_mr) select gcl00c,case when isnull(gcl101c,'004')<>'003' then isnull(gcl25f,0)*isnull(billdy,0) else 0 end, case when isnull(gcl101c,'004')='003' then isnull(gcl25f,0)*isnull(billdy,0) else 0 end from #gcm10_temp --where isnull(gcl101c,'')<>'' insert into #tbl_result(compid,def_mf,def_mr) select gcl00c,case when isnull(gcl101c,'004')<>'003' then isnull(gcl25f,0)*isnull(billdef,0) else 0 end, case when isnull(gcl101c,'004')='003' then isnull(gcl25f,0)*isnull(billdef,0) else 0 end from #gcm10_temp --where isnull(gcl101c,'')<>'' -----------------------------------------------充值第6销售 insert into #tbl_result(compid,cardcashamt_mf,cardcashamt_mr) select gcl00c,case when isnull(gcl102c,'004')<>'003' then isnull(gcl27f,0)*isnull(billcash,0) else 0 end, case when isnull(gcl102c,'004')='003' then isnull(gcl27f,0)*isnull(billcash,0) else 0 end from #gcm10_temp --where isnull(gcl102c,'')<>'' insert into #tbl_result(compid,card_mf,card_mr) select gcl00c,case when isnull(gcl102c,'004')<>'003' then isnull(gcl27f,0)*isnull(billcard,0) else 0 end, case when isnull(gcl102c,'004')='003' then isnull(gcl27f,0)*isnull(billcard,0) else 0 end from #gcm10_temp --where isnull(gcl102c,'')<>'' insert into #tbl_result(compid,dy_mf,dy_mr) select gcl00c,case when isnull(gcl102c,'004')<>'003' then isnull(gcl27f,0)*isnull(billdy,0) else 0 end, case when isnull(gcl102c,'004')='003' then isnull(gcl27f,0)*isnull(billdy,0) else 0 end from #gcm10_temp --where isnull(gcl102c,'')<>'' insert into #tbl_result(compid,def_mf,def_mr) select gcl00c,case when isnull(gcl102c,'004')<>'003' then isnull(gcl27f,0)*isnull(billdef,0) else 0 end, case when isnull(gcl102c,'004')='003' then isnull(gcl27f,0)*isnull(billdef,0) else 0 end from #gcm10_temp --where isnull(gcl102c,'')<>'' -----------------------------------------------充值第7销售 insert into #tbl_result(compid,cardcashamt_mf,cardcashamt_mr) select gcl00c,case when isnull(gcl103c,'004')<>'003' then isnull(gcl29f,0)*isnull(billcash,0) else 0 end, case when isnull(gcl103c,'004')='003' then isnull(gcl29f,0)*isnull(billcash,0) else 0 end from #gcm10_temp --where isnull(gcl103c,'')<>'' insert into #tbl_result(compid,card_mf,card_mr) select gcl00c,case when isnull(gcl103c,'004')<>'003' then isnull(gcl29f,0)*isnull(billcard,0) else 0 end, case when isnull(gcl103c,'004')='003' then isnull(gcl29f,0)*isnull(billcard,0) else 0 end from #gcm10_temp --where isnull(gcl103c,'')<>'' insert into #tbl_result(compid,dy_mf,dy_mr) select gcl00c,case when isnull(gcl103c,'004')<>'003' then isnull(gcl29f,0)*isnull(billdy,0) else 0 end, case when isnull(gcl103c,'004')='003' then isnull(gcl29f,0)*isnull(billdy,0) else 0 end from #gcm10_temp --where isnull(gcl103c,'')<>'' insert into #tbl_result(compid,def_mf,def_mr) select gcl00c,case when isnull(gcl103c,'004')<>'003' then isnull(gcl29f,0)*isnull(billdef,0) else 0 end, case when isnull(gcl103c,'004')='003' then isnull(gcl29f,0)*isnull(billdef,0) else 0 end from #gcm10_temp --where isnull(gcl103c,'')<>'' -----------------------------------------------充值第8销售 insert into #tbl_result(compid,cardcashamt_mf,cardcashamt_mr) select gcl00c,case when isnull(gcl104c,'004')<>'003' then isnull(gcl31f,0)*isnull(billcash,0) else 0 end, case when isnull(gcl104c,'004')='003' then isnull(gcl31f,0)*isnull(billcash,0) else 0 end from #gcm10_temp --where isnull(gcl104c,'')<>'' insert into #tbl_result(compid,card_mf,card_mr) select gcl00c,case when isnull(gcl104c,'004')<>'003' then isnull(gcl31f,0)*isnull(billcard,0) else 0 end, case when isnull(gcl104c,'004')='003' then isnull(gcl31f,0)*isnull(billcard,0) else 0 end from #gcm10_temp --where isnull(gcl104c,'')<>'' insert into #tbl_result(compid,dy_mf,dy_mr) select gcl00c,case when isnull(gcl104c,'004')<>'003' then isnull(gcl31f,0)*isnull(billdy,0) else 0 end, case when isnull(gcl104c,'004')='003' then isnull(gcl31f,0)*isnull(billdy,0) else 0 end from #gcm10_temp --where isnull(gcl104c,'')<>'' insert into #tbl_result(compid,def_mf,def_mr) select gcl00c,case when isnull(gcl104c,'004')<>'003' then isnull(gcl31f,0)*isnull(billdef,0) else 0 end, case when isnull(gcl104c,'004')='003' then isnull(gcl31f,0)*isnull(billdef,0) else 0 end from #gcm10_temp --where isnull(gcl104c,'')<>'' -----------------------------------------------充值第9销售 insert into #tbl_result(compid,cardcashamt_mf,cardcashamt_mr) select gcl00c,case when isnull(gcl105c,'004')<>'003' then isnull(gcl33f,0)*isnull(billcash,0) else 0 end, case when isnull(gcl105c,'004')='003' then isnull(gcl33f,0)*isnull(billcash,0) else 0 end from #gcm10_temp --where isnull(gcl105c,'')<>'' insert into #tbl_result(compid,card_mf,card_mr) select gcl00c,case when isnull(gcl105c,'004')<>'003' then isnull(gcl33f,0)*isnull(billcard,0) else 0 end, case when isnull(gcl105c,'004')='003' then isnull(gcl33f,0)*isnull(billcard,0) else 0 end from #gcm10_temp --where isnull(gcl105c,'')<>'' insert into #tbl_result(compid,dy_mf,dy_mr) select gcl00c,case when isnull(gcl105c,'004')<>'003' then isnull(gcl33f,0)*isnull(billdy,0) else 0 end, case when isnull(gcl105c,'004')='003' then isnull(gcl33f,0)*isnull(billdy,0) else 0 end from #gcm10_temp --where isnull(gcl105c,'')<>'' insert into #tbl_result(compid,def_mf,def_mr) select gcl00c,case when isnull(gcl105c,'004')<>'003' then isnull(gcl33f,0)*isnull(billdef,0) else 0 end, case when isnull(gcl105c,'004')='003' then isnull(gcl33f,0)*isnull(billdef,0) else 0 end from #gcm10_temp --where isnull(gcl105c,'')<>'' -----------------------------------------------充值第10销售 insert into #tbl_result(compid,cardcashamt_mf,cardcashamt_mr) select gcl00c,case when isnull(gcl106c,'004')<>'003' then isnull(gcl35f,0)*isnull(billcash,0) else 0 end, case when isnull(gcl106c,'004')='003' then isnull(gcl35f,0)*isnull(billcash,0) else 0 end from #gcm10_temp --where isnull(gcl106c,'')<>'' insert into #tbl_result(compid,card_mf,card_mr) select gcl00c,case when isnull(gcl106c,'004')<>'003' then isnull(gcl35f,0)*isnull(billcard,0) else 0 end, case when isnull(gcl106c,'004')='003' then isnull(gcl35f,0)*isnull(billcard,0) else 0 end from #gcm10_temp --where isnull(gcl106c,'')<>'' insert into #tbl_result(compid,dy_mf,dy_mr) select gcl00c,case when isnull(gcl106c,'004')<>'003' then isnull(gcl35f,0)*isnull(billdy,0) else 0 end, case when isnull(gcl106c,'004')='003' then isnull(gcl35f,0)*isnull(billdy,0) else 0 end from #gcm10_temp --where isnull(gcl106c,'')<>'' insert into #tbl_result(compid,def_mf,def_mr) select gcl00c,case when isnull(gcl106c,'004')<>'003' then isnull(gcl35f,0)*isnull(billdef,0) else 0 end, case when isnull(gcl106c,'004')='003' then isnull(gcl35f,0)*isnull(billdef,0) else 0 end from #gcm10_temp --where isnull(gcl106c,'')<>'' -----------------------------------------------外卖美容美发 insert into #tbl_result(compid,wm_mf,wm_mr) select gga00c,case when isnull(ggc81c,'004')<>'003' then ggc21f else 0 end, case when isnull(ggc81c,'004')='003' then ggc21f else 0 end from #ggm01_ggm03_temp --where isnull(ggc81c,'')<>'' insert into #tbl_result(compid,wm_mf,wm_mr) select gga00c,case when isnull(ggc82c,'004')<>'003' then ggc23f else 0 end, case when isnull(ggc82c,'004')='003' then ggc23f else 0 end from #ggm01_ggm03_temp --where isnull(ggc82c,'')<>'' insert into #tbl_result(compid,wm_mf,wm_mr) select gga00c,case when isnull(ggc83c,'004')<>'003' then ggc25f else 0 end, case when isnull(ggc83c,'004')='003' then ggc25f else 0 end from #ggm01_ggm03_temp --where isnull(ggc83c,'')<>'' --insert into #tbl_result(compid,wm_mf,wm_mr) --select gga00c,case when goodtype=1 then ggc11f else 0 end, -- case when goodtype in ('2','3','4') then ggc11f else 0 end --from #ggm01_ggm03_temp --where isnull(ggc81c,'')='' -- and isnull(ggc82c,'')='' -- and isnull(ggc83c,'')='' -----------------------------------------------指定美容美发 --一张单子里面只要出现一个指定。那就算这个项目是指定项目 insert into #tbl_result(compid,zd_mf,zd_mr) select gga00c,case when isnull(ggb81c,'004')<>'003' then ggb11f else 0 end, case when isnull(ggb81c,'004')='003' then ggb11f else 0 end from #ggm01_ggm02_temp where ((isnull(ggb12c,'')<>'' and ggb15c='1') or (isnull(ggb13c,'')<>'' and ggb16c='') or (isnull(ggb14c,'')<>'' and ggb19c='1')) --insert into #tbl_result(compid,zd_mf,zd_mr) --select gga00c,case when prjtype between '01' and '09' then ggb11f else 0 end, -- case when prjtype between '10' and '29' then ggb11f else 0 end -- from #ggm01_ggm02_temp --where ((isnull(ggb12c,'')<>'' and ggb15c='1') or (isnull(ggb13c,'')<>'' and ggb16c='') or (isnull(ggb14c,'')<>'' and ggb19c='1')) -------------------------总客数 --单据中只要有美容的就算美容 --insert into #tbl_result(compid,man_mf,momen_mf) insert into #client(compid,billid,num,sex,ntype) select gga00c,gga01c,gga14f,gga31i,ntype from #ggm01_ggm02_temp with(nolock) group by gga00c,gga01c,gga14f,gga31i,ntype insert into #tbl_result(compid,man_mf_mr,momen_mf_mr) select compid,man_mf_mr,momen_mf_mr from (select compid,billid,man_mf_mr=case when sex=1 then num else 0 end, momen_mf_mr=case when sex=0 then num else 0 end,count(ntype) ctype from #client group by compid,billid,sex,num having count(ntype)=2) a delete a from #client a,(select compid,billid,man_mf_mr=case when sex=1 then num else 0 end, momen_mf_mr=case when sex=0 then num else 0 end, count(ntype) ntype from #client group by compid,billid,sex,num having count(ntype)=2) b where a.compid=b.compid and a.billid=b.billid insert into #tbl_result(compid,man_mf,momen_mf) select compid,man_mf,momen_mf from (select compid,billid,man_mf=case when sex=1 then num else 0 end, momen_mf=case when sex=0 then num else 0 end, ntype from #client where ntype=1 group by compid,billid,sex,num,ntype) a insert into #tbl_result(compid,man_mr,momen_mr) select compid,man_mr,momen_mr from (select compid,billid,man_mr=case when sex=1 then num else 0 end, momen_mr=case when sex=0 then num else 0 end, ntype from #client where ntype=2 group by compid,billid,sex,num,ntype) a ---开卡美容美发 ---卖卡第1销售 insert into #tbl_result(compid,kcard_mf,kcard_mr) select gna00c,case when isnull(gna81c,'004')<>'003' then isnull(gna27f,0)*isnull(billsumamt,0) else 0 end, case when isnull(gna81c,'004')='003' then isnull(gna27f,0)*isnull(billsumamt,0) else 0 end from #gnm01_temp --where isnull(gna81c,'')<>'' ---卖卡第2销售 insert into #tbl_result(compid,kcard_mf,kcard_mr) select gna00c,case when isnull(gna82c,'004')<>'003' then isnull(gna29f,0)*isnull(billsumamt,0) else 0 end, case when isnull(gna82c,'004')='003' then isnull(gna29f,0)*isnull(billsumamt,0) else 0 end from #gnm01_temp --where isnull(gna82c,'')<>'' ---卖卡第3销售 insert into #tbl_result(compid,kcard_mf,kcard_mr) select gna00c,case when isnull(gna83c,'004')<>'003' then isnull(gna31f,0)*isnull(billsumamt,0) else 0 end, case when isnull(gna83c,'004')='003' then isnull(gna31f,0)*isnull(billsumamt,0) else 0 end from #gnm01_temp --where isnull(gna83c,'')<>'' ---卖卡第4销售 insert into #tbl_result(compid,kcard_mf,kcard_mr) select gna00c,case when isnull(gna100c,'004')<>'003' then isnull(gna33f,0)*isnull(billsumamt,0) else 0 end, case when isnull(gna100c,'004')='003' then isnull(gna33f,0)*isnull(billsumamt,0) else 0 end from #gnm01_temp --where isnull(gna100c,'')<>'' ---卖卡第5销售 insert into #tbl_result(compid,kcard_mf,kcard_mr) select gna00c,case when isnull(gna101c,'004')<>'003' then isnull(gna35f,0)*isnull(billsumamt,0) else 0 end, case when isnull(gna101c,'004')='003' then isnull(gna35f,0)*isnull(billsumamt,0) else 0 end from #gnm01_temp --where isnull(gna101c,'')<>'' ---卖卡第6销售 insert into #tbl_result(compid,kcard_mf,kcard_mr) select gna00c,case when isnull(gna102c,'004')<>'003' then isnull(gna37f,0)*isnull(billsumamt,0) else 0 end, case when isnull(gna102c,'004')='003' then isnull(gna37f,0)*isnull(billsumamt,0) else 0 end from #gnm01_temp --where isnull(gna102c,'')<>'' ---卖卡第7销售 insert into #tbl_result(compid,kcard_mf,kcard_mr) select gna00c,case when isnull(gna103c,'004')<>'003' then isnull(gna39f,0)*isnull(billsumamt,0) else 0 end, case when isnull(gna103c,'004')='003' then isnull(gna39f,0)*isnull(billsumamt,0) else 0 end from #gnm01_temp --where isnull(gna103c,'')<>'' ---卖卡第8销售 insert into #tbl_result(compid,kcard_mf,kcard_mr) select gna00c,case when isnull(gna104c,'004')<>'003' then isnull(gna41f,0)*isnull(billsumamt,0) else 0 end, case when isnull(gna104c,'004')='003' then isnull(gna41f,0)*isnull(billsumamt,0) else 0 end from #gnm01_temp --where isnull(gna104c,'')<>'' ---卖卡第9销售 insert into #tbl_result(compid,kcard_mf,kcard_mr) select gna00c,case when isnull(gna105c,'004')<>'003' then isnull(gna43f,0)*isnull(billsumamt,0) else 0 end, case when isnull(gna105c,'004')='003' then isnull(gna43f,0)*isnull(billsumamt,0) else 0 end from #gnm01_temp --where isnull(gna105c,'')<>'' ---卖卡第10销售 insert into #tbl_result(compid,kcard_mf,kcard_mr) select gna00c,case when isnull(gna106c,'004')<>'003' then isnull(gna45f,0)*isnull(billsumamt,0) else 0 end, case when isnull(gna106c,'004')='003' then isnull(gna45f,0)*isnull(billsumamt,0) else 0 end from #gnm01_temp --where isnull(gna106c,'')<>'' ---充值第1销售 insert into #tbl_result(compid,kcard_mf,kcard_mr) select gcl00c,case when isnull(gcl81c,'004')<>'003' then isnull(gcl16f,0)*isnull(billsumamt,0) else 0 end, case when isnull(gcl81c,'004')='003' then isnull(gcl16f,0)*isnull(billsumamt,0) else 0 end from #gcm10_temp --where isnull(gcl81c,'')<>'' ---充值第2销售 insert into #tbl_result(compid,kcard_mf,kcard_mr) select gcl00c,case when isnull(gcl82c,'004')<>'003' then isnull(gcl18f,0)*isnull(billsumamt,0) else 0 end, case when isnull(gcl82c,'004')='003' then isnull(gcl18f,0)*isnull(billsumamt,0) else 0 end from #gcm10_temp --where isnull(gcl82c,'')<>'' ---充值第3销售 insert into #tbl_result(compid,kcard_mf,kcard_mr) select gcl00c,case when isnull(gcl83c,'004')<>'003' then isnull(gcl20f,0)*isnull(billsumamt,0) else 0 end, case when isnull(gcl83c,'004')='003' then isnull(gcl20f,0)*isnull(billsumamt,0) else 0 end from #gcm10_temp --where isnull(gcl83c,'')<>'' ---充值第4销售 insert into #tbl_result(compid,kcard_mf,kcard_mr) select gcl00c,case when isnull(gcl100c,'004')<>'003' then isnull(gcl23f,0)*isnull(billsumamt,0) else 0 end, case when isnull(gcl100c,'004')='003' then isnull(gcl23f,0)*isnull(billsumamt,0) else 0 end from #gcm10_temp --where isnull(gcl100c,'')<>'' ---充值第5销售 insert into #tbl_result(compid,kcard_mf,kcard_mr) select gcl00c,case when isnull(gcl101c,'004')<>'003' then isnull(gcl25f,0)*isnull(billsumamt,0) else 0 end, case when isnull(gcl101c,'004')='003' then isnull(gcl25f,0)*isnull(billsumamt,0) else 0 end from #gcm10_temp --where isnull(gcl101c,'')<>'' ---充值第6销售 insert into #tbl_result(compid,kcard_mf,kcard_mr) select gcl00c,case when isnull(gcl102c,'004')<>'003' then isnull(gcl27f,0)*isnull(billsumamt,0) else 0 end, case when isnull(gcl102c,'004')='003' then isnull(gcl27f,0)*isnull(billsumamt,0) else 0 end from #gcm10_temp --where isnull(gcl102c,'')<>'' ---充值第7销售 insert into #tbl_result(compid,kcard_mf,kcard_mr) select gcl00c,case when isnull(gcl103c,'004')<>'003' then isnull(gcl29f,0)*isnull(billsumamt,0) else 0 end, case when isnull(gcl103c,'004')='003' then isnull(gcl29f,0)*isnull(billsumamt,0) else 0 end from #gcm10_temp --where isnull(gcl103c,'')<>'' ---充值第8销售 insert into #tbl_result(compid,kcard_mf,kcard_mr) select gcl00c,case when isnull(gcl104c,'004')<>'003' then isnull(gcl31f,0)*isnull(billsumamt,0) else 0 end, case when isnull(gcl104c,'004')='003' then isnull(gcl31f,0)*isnull(billsumamt,0) else 0 end from #gcm10_temp --where isnull(gcl104c,'')<>'' ---充值第9销售 insert into #tbl_result(compid,kcard_mf,kcard_mr) select gcl00c,case when isnull(gcl105c,'004')<>'003' then isnull(gcl33f,0)*isnull(billsumamt,0) else 0 end, case when isnull(gcl105c,'004')='003' then isnull(gcl33f,0)*isnull(billsumamt,0) else 0 end from #gcm10_temp --where isnull(gcl105c,'')<>'' ---充值第10销售 insert into #tbl_result(compid,kcard_mf,kcard_mr) select gcl00c,case when isnull(gcl106c,'004')<>'003' then isnull(gcl35f,0)*isnull(billsumamt,0) else 0 end, case when isnull(gcl106c,'004')='003' then isnull(gcl35f,0)*isnull(billsumamt,0) else 0 end from #gcm10_temp where isnull(gcl106c,'')<>'' ------------------------------------统计退卡 delete #gcm10_temp insert into #gcm10_temp(gcl00c,gcl01c,gcl09c,gcl17c,gcl19c,gcl22c,gcl24c,gcl26c,gcl28c,gcl30c,gcl32c,gcl34c, gcl16f,gcl18f,gcl20f,gcl23f,gcl25f,gcl27f,gcl29f,gcl31f,gcl33f,gcl35f,gcl81c,gcl82c,gcl83c,gcl100c, gcl101c,gcl102c,gcl103c,gcl104c,gcl105c,gcl106c) select gcl00c,gcl01c,gcl09c,gcl17c,gcl19c,gcl22c,gcl24c,gcl26c,gcl28c,gcl30c,gcl32c,gcl34c, gcl16f,gcl18f,gcl20f,gcl23f,gcl25f,gcl27f,gcl29f,gcl31f,gcl33f,gcl35f,gcl81c,gcl82c,gcl83c,gcl100c, gcl101c,gcl102c,gcl103c,gcl104c,gcl105c,gcl106c from gcm10 with(nolock) where gcl80d between @fromdate and @todate and gcl06i=1 update a set a.billsumamt=(select sum(gsc05f) from gsm03 where gsc00c=gcl00c and gsc01c=gcl01c and gsc02c='gz' and gsc04c<>'5') from #gcm10_temp a ---充值第1销售 insert into #tbl_result(compid,tk_mf,tk_mr) select gcl00c,case when isnull(gcl81c,'004')<>'003' then isnull(gcl16f,0)*isnull(billsumamt,0) else 0 end, case when isnull(gcl81c,'004')='003' then isnull(gcl16f,0)*isnull(billsumamt,0) else 0 end from #gcm10_temp --where isnull(gcl81c,'')<>'' ---充值第2销售 insert into #tbl_result(compid,tk_mf,tk_mr) select gcl00c,case when isnull(gcl82c,'004')<>'003' then isnull(gcl18f,0)*isnull(billsumamt,0) else 0 end, case when isnull(gcl82c,'004')='003' then isnull(gcl18f,0)*isnull(billsumamt,0) else 0 end from #gcm10_temp --where isnull(gcl82c,'')<>'' ---充值第3销售 insert into #tbl_result(compid,tk_mf,tk_mr) select gcl00c,case when isnull(gcl83c,'004')<>'003' then isnull(gcl20f,0)*isnull(billsumamt,0) else 0 end, case when isnull(gcl83c,'004')='003' then isnull(gcl20f,0)*isnull(billsumamt,0) else 0 end from #gcm10_temp --where isnull(gcl83c,'')<>'' ---充值第4销售 insert into #tbl_result(compid,tk_mf,tk_mr) select gcl00c,case when isnull(gcl100c,'004')<>'003' then isnull(gcl23f,0)*isnull(billsumamt,0) else 0 end, case when isnull(gcl100c,'004')='003' then isnull(gcl23f,0)*isnull(billsumamt,0) else 0 end from #gcm10_temp --where isnull(gcl100c,'')<>'' ---充值第5销售 insert into #tbl_result(compid,tk_mf,tk_mr) select gcl00c,case when isnull(gcl101c,'004')<>'003' then isnull(gcl25f,0)*isnull(billsumamt,0) else 0 end, case when isnull(gcl101c,'004')='003' then isnull(gcl25f,0)*isnull(billsumamt,0) else 0 end from #gcm10_temp --where isnull(gcl101c,'')<>'' ---充值第6销售 insert into #tbl_result(compid,tk_mf,tk_mr) select gcl00c,case when isnull(gcl102c,'004')<>'003' then isnull(gcl27f,0)*isnull(billsumamt,0) else 0 end, case when isnull(gcl102c,'004')='003' then isnull(gcl27f,0)*isnull(billsumamt,0) else 0 end from #gcm10_temp --where isnull(gcl102c,'')<>'' ---充值第7销售 insert into #tbl_result(compid,tk_mf,tk_mr) select gcl00c,case when isnull(gcl103c,'004')<>'003' then isnull(gcl29f,0)*isnull(billsumamt,0) else 0 end, case when isnull(gcl103c,'004')='003' then isnull(gcl29f,0)*isnull(billsumamt,0) else 0 end from #gcm10_temp --where isnull(gcl103c,'')<>'' ---充值第8销售 insert into #tbl_result(compid,tk_mf,tk_mr) select gcl00c,case when isnull(gcl104c,'004')<>'003' then isnull(gcl31f,0)*isnull(billsumamt,0) else 0 end, case when isnull(gcl104c,'004')='003' then isnull(gcl31f,0)*isnull(billsumamt,0) else 0 end from #gcm10_temp --where isnull(gcl104c,'')<>'' ---充值第9销售 insert into #tbl_result(compid,tk_mf,tk_mr) select gcl00c,case when isnull(gcl105c,'004')<>'003' then isnull(gcl33f,0)*isnull(billsumamt,0) else 0 end, case when isnull(gcl105c,'004')='003' then isnull(gcl33f,0)*isnull(billsumamt,0) else 0 end from #gcm10_temp --where isnull(gcl105c,'')<>'' ---充值第10销售 insert into #tbl_result(compid,tk_mf,tk_mr) select gcl00c,case when isnull(gcl106c,'004')<>'003' then isnull(gcl35f,0)*isnull(billsumamt,0) else 0 end, case when isnull(gcl106c,'004')='003' then isnull(gcl35f,0)*isnull(billsumamt,0) else 0 end from #gcm10_temp --where isnull(gcl106c,'')<>'' insert into #tbl_result(compid,dy_mf_count,dy_mf_amt) select gga00c,sum(ggb05f),sum(ggb11f) from #ggm01_ggm02_temp where ggb27c='3' and isnull(ggb81c,'004')<>'003' group by gga00c,prjtype insert into #tbl_result(compid,dy_mr_count,dy_mr_amt) select gga00c,sum(ggb05f),sum(ggb11f) from #ggm01_ggm02_temp where ggb27c='3' and ggb81c='003' group by gga00c,prjtype insert into #tbl_result(compid,jf_mf_count,jf_mf_amt) select gga00c,sum(ggb05f),sum(ggb11f) from #ggm01_ggm02_temp where ggb27c='7' and isnull(ggb81c,'004')<>'003' group by gga00c,prjtype insert into #tbl_result(compid,jf_mr_count,jf_mr_amt) select gga00c,sum(ggb05f),sum(ggb11f) from #ggm01_ggm02_temp where ggb27c='7' and ggb81c='003' group by gga00c,prjtype --select * from #ggm01_ggm02_temp where gga00c = '013' --项目分类统计 insert into #tbl_item(compid,item,item_cashcount,item_cashamt) select gga00c,prjtype,sum(ggb05f),sum(ggb11f) from #ggm01_ggm02_temp where ggb27c in('1','6','$','$1','$2','$3','$4') group by gga00c,prjtype insert into #tbl_item(compid,item,item_cardcount,item_cardamt) select gga00c,prjtype,sum(ggb05f),sum(ggb11f) from #ggm01_ggm02_temp where ggb27c in('#','A','4','9') group by gga00c,prjtype --计算欠款业绩 insert into #tbl_result(compid,def_mf,def_mr) select gga00c,case when isnull(ggb81c,'004')<>'003' then ggb11f else 0 end, case when isnull(ggb81c,'004')='003' then ggb11f else 0 end from #ggm01_ggm02_temp where ggb27c='5' --and isnull(ggb81c,'')<>'' insert into #tbl_result(compid,def_mf,def_mr) select gga00c,case when isnull(ggc81c,'004')<>'003' then ggc21f else 0 end, case when isnull(ggc81c,'004')='003' then ggc21f else 0 end from #ggm01_ggm03_temp where ggc27c='5' and isnull(ggc81c,'')<>'' insert into #tbl_result(compid,def_mf,def_mr) select gga00c,case when isnull(ggc82c,'004')<>'003' then ggc23f else 0 end, case when isnull(ggc82c,'004')='003' then ggc23f else 0 end from #ggm01_ggm03_temp where ggc27c='5' and isnull(ggc82c,'')<>'' insert into #tbl_result(compid,def_mf,def_mr) select gga00c,case when isnull(ggc83c,'004')<>'003' then ggc25f else 0 end, case when isnull(ggc83c,'004')='003' then ggc25f else 0 end from #ggm01_ggm03_temp where ggc27c='5' and isnull(ggc83c,'')<>'' ------------------------------其他项目的分类 insert into #tbl_result(compid,cash_other_mf,cash_other_mf_count,cash_other_mr,cash_other_mr_count) select gga00c,case when isnull(ggb81c,'004')<>'003' then ggb11f else 0 end, case when isnull(ggb81c,'004')<>'003' then ggb05f else 0 end, case when ggb81c='003' then ggb11f else 0 end, case when ggb81c='003' then ggb05f else 0 end from #ggm01_ggm02_temp where ggb27c in('1','6','$','$1','$2','$3','$4') and prjtype='30' insert into #tbl_result(compid,card_other_mf,card_other_mf_count,card_other_mr,card_other_mr_count) select gga00c,case when isnull(ggb81c,'004')<>'003' then ggb11f else 0 end, case when isnull(ggb81c,'004')<>'003' then ggb05f else 0 end, case when ggb81c='003' then ggb11f else 0 end, case when ggb81c='003' then ggb05f else 0 end from #ggm01_ggm02_temp where ggb27c in('#','A','4','9') and prjtype='30' --insert into #tbl_item(compid,item,cashcount,cashamt) update a set a.cash_mf=b.cash_mf,a.cash_mr=b.cash_mr,a.card_mf=b.card_mf,a.card_mr=b.card_mr, a.dy_mf=b.dy_mf,a.dy_mr=b.dy_mr,a.kcard_mf=b.kcard_mf,a.kcard_mr=b.kcard_mr, a.wm_mf=b.wm_mf,a.wm_mr=b.wm_mr,a.zd_mf=b.zd_mf,a.zd_mr=b.zd_mr,a.man_mf=b.man_mf,a.man_mr=b.man_mr, a.momen_mf=b.momen_mf,a.momen_mr=b.momen_mr,a.tk_mf=b.tk_mf,a.tk_mr=b.tk_mr,a.dy_mf_count=b.dy_mf_count, a.dy_mf_amt=b.dy_mf_amt,a.jf_mf_count=b.jf_mf_count,a.jf_mf_amt=b.jf_mf_amt,a.cardcashamt_mf=b.cardcashamt_mf, a.cardcashamt_mr=b.cardcashamt_mr,a.cash_wm_mf=b.cash_wm_mf,a.cash_wm_mr=b.cash_wm_mr, a.man_mf_mr=b.man_mf_mr,a.momen_mf_mr=b.momen_mf_mr,a.cash_other_mf=b.cash_other_mf, a.cash_other_mf_count=b.cash_other_mf_count,a.cash_other_mr=b.cash_other_mr, a.cash_other_mr_count=b.cash_other_mr_count,a.card_other_mf=b.card_other_mf, a.card_other_mf_count=b.card_other_mf_count,a.card_other_mr=b.card_other_mr, a.card_other_mr_count=b.card_other_mr_count,a.cash_wm_mf_count=b.cash_wm_mf_count, a.cash_wm_mr_count=b.cash_wm_mr_count,a.card_wm_mf=b.card_wm_mf,a.card_wm_mf_count=b.card_wm_mf_count, a.card_wm_mr=b.card_wm_mr,a.card_wm_mr_count=b.card_wm_mr_count,a.dy_mr_count=b.dy_mr_count, a.jf_mr_count=b.jf_mr_count,a.jf_mr_amt=b.jf_mr_amt,a.def_mf=b.def_mf,a.def_mr=b.def_mr from #resut_tbl a, (select compid,isnull(sum(cash_mf),0)+isnull(sum(cash_wm_mf),0)+isnull(sum(cardcashamt_mf),0) cash_mf,isnull(sum(cash_mr),0)+isnull(sum(cash_wm_mr),0)+isnull(sum(cardcashamt_mr),0) cash_mr,sum(card_mf) card_mf,sum(card_mr) card_mr, sum(dy_mf) dy_mf,sum(dy_mr) dy_mr,sum(kcard_mf) kcard_mf,sum(kcard_mr) kcard_mr,sum(wm_mf) wm_mf, sum(wm_mr) wm_mr,sum(zd_mf) zd_mf,sum(zd_mr) zd_mr,sum(man_mf) man_mf,sum(man_mr) man_mr, sum(momen_mf) momen_mf,sum(momen_mr) momen_mr,sum(tk_mf) tk_mf,sum(tk_mr) tk_mr,sum(dy_mf_count) dy_mf_count, sum(dy_mf_amt) dy_mf_amt,sum(jf_mf_count) jf_mf_count,sum(jf_mf_amt) jf_mf_amt,sum(cardcashamt_mf) cardcashamt_mf,sum(cardcashamt_mr) cardcashamt_mr, sum(cash_wm_mf) cash_wm_mf,sum(cash_wm_mr) cash_wm_mr,sum(man_mf_mr) man_mf_mr,sum(momen_mf_mr) momen_mf_mr, sum(cash_other_mf) cash_other_mf,sum(cash_other_mf_count) cash_other_mf_count,sum(cash_other_mr) cash_other_mr, sum(cash_other_mr_count) cash_other_mr_count,sum(card_other_mf) card_other_mf,sum(card_other_mf_count) card_other_mf_count, sum(card_other_mr) card_other_mr,sum(card_other_mr_count) card_other_mr_count,sum(cash_wm_mf_count) cash_wm_mf_count, sum(cash_wm_mr_count) cash_wm_mr_count,sum(card_wm_mf) card_wm_mf,sum(card_wm_mf_count) card_wm_mf_count,sum(card_wm_mr) card_wm_mr, sum(card_wm_mr_count) card_wm_mr_count,sum(dy_mr_count) dy_mr_count,sum(dy_mr_amt) dy_mr_amt, sum(jf_mr_count) jf_mr_count,sum(jf_mr_amt) jf_mr_amt,sum(def_mf) def_mf,sum(def_mr) def_mr from #tbl_result group by compid) b where a.compid=b.compid update #resut_tbl set mf_total=isnull(man_mf,0)+isnull(momen_mf,0), mr_total=isnull(man_mr,0)+isnull(momen_mr,0), mr_mf_total=isnull(momen_mf_mr,0)+isnull(man_mf_mr,0), totalclient=isnull(man_mf,0)+isnull(momen_mf,0)+isnull(man_mr,0)+isnull(momen_mr,0)+isnull(momen_mf_mr,0)+isnull(man_mf_mr,0) update #resut_tbl set total_mf_amt=isnull(cardcashamt_mf,0)+isnull(cash_wm_mf,0)--+isnull(cash_other_mf,0) update #resut_tbl set total_mr_amt=isnull(cardcashamt_mr,0)+isnull(cash_wm_mr,0)--+isnull(cash_other_mr,0) update #resut_tbl set total_mf_count=isnull(cash_wm_mf_count,0)--+isnull(cash_other_mf_count,0) update #resut_tbl set total_mr_count=isnull(cash_wm_mr_count,0)--+isnull(cash_other_mr_count,0) update a set total_mf_amt=isnull(total_mf_amt,0)+isnull(b.cashamt,0),total_mf_count=isnull(total_mf_count,0)+isnull(b.cashcount,0) from #resut_tbl a,(select gga00c,sum(ggb05f) cashcount,sum(ggb11f) cashamt from #ggm01_ggm02_temp where ggb27c in('1','6','$','$1','$2','$3','$4') and isnull(ggb81c,'004')<>'003' group by gga00c ) b where a.compid=b.gga00c update a set total_mr_amt=isnull(total_mr_amt,0)+isnull(b.cashamt,0),total_mr_count=isnull(total_mr_count,0)+isnull(b.cashcount,0) from #resut_tbl a,(select gga00c,sum(ggb05f) cashcount,sum(ggb11f) cashamt from #ggm01_ggm02_temp where ggb27c in('1','6','$','$1','$2','$3','$4') and isnull(ggb81c,'004')='003' group by gga00c) b where a.compid=b.gga00c select isnull(cash_mf,0)+isnull(cash_mr,0),* from #resut_tbl a,(select compid,item,sum(item_cashamt) item_cashamt,sum(item_cashcount) item_cashcount, sum(item_cardcount) item_cardcount,sum(item_cardamt)item_cardamt from #tbl_item group by compid,item) b where a.compid=b.compid order by sumamt desc end GO /****** Object: Table [dbo].[recover_ggm02] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[recover_ggm02]( [id] [int] NOT NULL, [ggb00c] [varchar](10) NOT NULL, [ggb01c] [varchar](20) NOT NULL, [ggb02f] [float] NOT NULL, [ggb03c] [varchar](20) NULL, [ggb04c] [varchar](5) NULL, [ggb05f] [float] NULL, [ggb06c] [varchar](5) NULL, [ggb07f] [float] NULL, [ggb08f] [float] NULL, [ggb09f] [float] NULL, [ggb10f] [float] NULL, [ggb11f] [float] NULL, [ggb12c] [varchar](20) NULL, [ggb13c] [varchar](20) NULL, [ggb14c] [varchar](20) NULL, [ggb15c] [varchar](20) NULL, [ggb16c] [varchar](20) NULL, [ggb17f] [float] NULL, [ggb18f] [float] NULL, [ggb19c] [varchar](20) NULL, [ggb20f] [float] NULL, [ggb21f] [float] NULL, [ggb22f] [float] NULL, [ggb23c] [varchar](128) NULL, [ggb24f] [float] NULL, [ggb25c] [varchar](20) NULL, [ggb26c] [varchar](20) NULL, [ggb27c] [varchar](5) NULL, [ggb28f] [float] NULL, [ggb29f] [float] NULL, [ggb30f] [float] NULL, [ggb31c] [varchar](20) NULL, [ggb32c] [varchar](20) NULL, [ggb33f] [float] NULL, [ggb34f] [float] NULL, [ggb35f] [float] NULL, [ggb81c] [varchar](10) NULL, [ggb36f] [float] NULL, [ggb37f] [float] NULL, [ggb38f] [float] NULL, [ggb39f] [float] NULL, [ggb40i] [int] NULL, [ggb41d] [varchar](20) NULL, [ggb42c] [varchar](20) NULL, [ggb43c] [varchar](6) NULL, [ggb44c] [varchar](6) NULL, [ggb45i] [int] NULL, [ggb46f] [float] NULL, [ggb47i] [int] NULL, [ggb48i] [int] NULL, [memo] [varchar](50) NULL, [ggb82i] [int] NULL, [ggb83f] [float] NULL, CONSTRAINT [PK_recover_GGM02] PRIMARY KEY CLUSTERED ( [id] ASC, [ggb00c] ASC, [ggb01c] ASC, [ggb02f] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gcm25] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gcm25]( [gcy01c] [varchar](20) NOT NULL, [gcy02c] [varchar](10) NOT NULL, CONSTRAINT [PK_GCM25] PRIMARY KEY CLUSTERED ( [gcy01c] ASC, [gcy02c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[ham10p] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[ham10p]( [haj00c] [varchar](10) NOT NULL, [haj01c] [varchar](20) NOT NULL, [haj02p] [image] NULL, [haj03c] [varchar](2) NOT NULL, [haj04c] [numeric](19, 0) IDENTITY(1,1) NOT NULL, CONSTRAINT [PK_HAM10P] PRIMARY KEY CLUSTERED ( [haj04c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gtc06] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gtc06]( [gth00c] [varchar](10) NOT NULL, [gth01c] [varchar](20) NOT NULL, [gth02c] [varchar](20) NOT NULL, [gth03f] [float] NULL, [gth04f] [float] NULL, [gth05f] [float] NULL, [gth06f] [float] NULL, [gth07f] [float] NULL, [gth08i] [int] NULL, [gth09c] [varchar](5) NULL, [gth10f] [float] NULL, [gth11c] [varchar](5) NULL, [gth12f] [float] NULL, [gth13c] [varchar](5) NULL, [gth14f] [float] NULL, [gth15c] [varchar](5) NULL, [gth16f] [float] NULL, [gth17i] [int] NULL, [gth18f] [float] NULL, [gth19c] [varchar](50) NULL, [gth26i] [int] NULL, [gth27c] [varchar](2) NULL, [gth20c] [int] NULL, [gth21c] [varchar](20) NULL, [gth22d] [varchar](20) NULL, [gth23f] [float] NULL, [gth24c] [varchar](20) NULL, [gth25f] [float] NULL, CONSTRAINT [PK_GTC06] PRIMARY KEY CLUSTERED ( [gth00c] ASC, [gth01c] ASC, [gth02c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_cal_operation_result_new_lm2] Script Date: 06/12/2017 06:19:33 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_cal_operation_result_new_lm2] ( @compid varchar(10), -- 公司别 @fromdate varchar(10), -- 开始日期 @todate varchar(10), -- 截至日期 @fromstaff varchar(20), -- 员工代码 @tostaff varchar(20), -- 员工代码 @amttype int, -- 0-营业额 1-虚业绩 2-数量 3-实业绩 @printcom varchar(4) -- 打印提成 substring(, 1, 1) --总提成, substring(, 2, 1) - 项目, substring(, 3, 1) -产品 substring(, 4, 1) -卡 ) as begin create table #work_detail ( id int identity , -- 流水好 person_id varchar(20) not null, -- 员工代码 action_id tinyint not null, -- 类别,1-卡销售 2-指定大工,3-轮班大工,4-补工大工, -- 5-指定中工,6-轮班中工 -- 7-指定小工, 8-轮班小工,9-产品销售 10,11-四工 -- 0 卡充值 srvdate varchar(10) not null, --日期 code varchar(20) not null, --项目代码,或是卡号,产品码 name varchar(200) null, --名称 payway varchar(20) null, --支付方式 amt float null, --营业金额 quan float null, --数量 cost float null, --成本 comm float null, --提成 amt2 float null, --虚业绩 amt3 float null, --真实业绩 cardrate float null, --转卡比率 --当买卡或充值的时候,这个地方当作疗程账户金额用 stdprice float null, --标准单价 prj_type varchar(20) null, --项目类别 share_rate float null default 1, --分享比率 cls_flag int null, -- 1:项目 2:产品 3:卡 gender int null, --性别 0:女 1:男 billid varchar(20) null, --单号 paycode varchar(20) null, --支付代码 ex_rate float null, --实际营业额折算比率 ggm02_seq float null, --ggm02中的序列号,与单号一起来标志一个项目 room_id varchar(20) null, --房间号,只在spa或足浴版本需要 compid varchar(10) null, --公司别 spid int null, --连接号 cardid varchar(20) null, --会员卡号 memid varchar(40) null, --会员编号 comp_ascription varchar(10) null,-- 作业公司 ) declare @printtotalcom varchar(1) --打印总提成 declare @printitemcom varchar(1) --打印项目提成 declare @printgoodscom varchar(1) --打印产品提成 declare @printcardcom varchar(1) --打印卡提成 set @printtotalcom = substring(@printcom, 1, 1) set @printitemcom = substring(@printcom, 2, 1) set @printgoodscom = substring(@printcom, 3, 1) set @printcardcom = substring(@printcom, 4, 1) --提成 if @printtotalcom = '1' or @printitemcom = '1' or @printgoodscom = '1' or @printcardcom = '1' begin create table #com ( staffno varchar(20) not null, com float null ) declare @spid int select @spid = @@spid + 1000 insert #com(staffno, com) exec upg_personal_statistics @compid, @fromdate, @todate, @fromstaff, @tostaff, 1, 1, 1, 2, @spid insert #work_detail(person_id,action_id,srvdate,code ,name,payway, amt,quan,cost,comm,amt2,amt3,cardrate,stdprice,prj_type,share_rate, cls_flag,gender,billid,paycode,ex_rate,ggm02_seq,room_id,compid, spid,cardid,memid ,comp_ascription ) select person_id,action_id,srvdate,code ,name,payway, amt,quan,cost,comm,amt2,amt3,cardrate,stdprice,prj_type,share_rate, cls_flag,gender,billid,paycode,ex_rate,ggm02_seq,room_id,compid, spid,cardid,memid ,comp_ascription from work_detail with (nolock) where spid = @spid delete #com drop table #com end --start计算 门店总业绩 add by lm 2016.01.06 create table #comp_amt ( compid varchar(20) not null,--公司别 billid varchar(20) not null,--单据编号 code varchar(20) null,--项目类别 codetype varchar(20) null,--项目统计分类 paycode varchar(20) null,--支付方式 amt float default 0 null,--营业额 amt2 float default 0 null,--虚业绩(营业额折算金额) num float default 0 null --数量 ) create table #comp_cashamt ( compid varchar(20) not null,--公司别 code varchar(20) null,--项目类别 codetype varchar(20) null,--项目统计分类 paycode varchar(20) null,--支付方式 cashamt float default 0 null,--营业额 cashamt2 float default 0 null, --虚业绩(营业额折算金额) cashnum float default 0 null --数量 ) create table #comp_cardamt ( compid varchar(20) not null,--公司别 code varchar(20) null,--项目类别 codetype varchar(20) null,--项目统计分类 paycode varchar(20) null,--支付方式 cardamt float default 0 null,--营业额 cardamt2 float default 0 null,--虚业绩(营业额折算金额) cardnum float default 0 null --数量 ) create table #comp_otheramt ( compid varchar(20) not null,--公司别 code varchar(20) null,--项目类别 codetype varchar(20) null,--项目统计分类 paycode varchar(20) null,--支付方式 otheramt float default 0 null,--营业额 otheramt2 float default 0 null,--虚业绩(营业额折算金额) othernum float default 0 null,--数量 ) create table #comp_sum_amt ( compid varchar(20) not null,--公司别 codetype varchar(20) null,--项目统计分类 cashamt float default 0 null,--营业额 cashamt2 float default 0 null,--虚业绩(营业额折算金额) cashnum float default 0 null,--数量 cardamt float default 0 null,--营业额 cardamt2 float default 0 null,--虚业绩(营业额折算金额) cardnum float default 0 null,--数量 otheramt float default 0 null,--营业额 otheramt2 float default 0 null,--虚业绩(营业额折算金额) othernum float default 0 null,--数量 cashsumamt float default 0 null,--营业额 cardsumamt float default 0 null --营业额 ) insert #comp_amt(billid, compid, code, paycode, amt, amt2, num) select distinct billid, compid, code, paycode,isnull(a.amt,0), isnull(a.amt,0) * isnull(b.gso03i,0),quan from #work_detail a with(nolock), gsm15 b with(nolock) where spid = @spid and b.gso00c = @compid and a.paycode = b.gso02c and a.compid = @compid and a.comp_ascription = @compid and a.action_id not in (0,1,9) update a set a.codetype = b.gda13c from #comp_amt a, gdm01 b with(nolock), gsm02 c with(nolock) where a.compid = b.gda00c and a.code = b.gda01c and b.gda13c = c.gsb02c and b.gda00c = c.gsb00c and c.gsb01c = 'AJ' -- 项目 insert #comp_cashamt(compid, codetype, cashamt, cashamt2, cashnum) select compid, codetype, sum(amt), sum(amt2),sum(num) from #comp_amt where paycode in (select pay from pay_classify where category = '1' and comp = @compid) group by compid, codetype insert #comp_cardamt(compid, codetype, cardamt, cardamt2, cardnum) select compid, codetype, sum(amt), sum(amt2),sum(num) from #comp_amt where paycode in (select pay from pay_classify where category = '2' and comp = @compid) group by compid, codetype insert #comp_otheramt(compid, codetype, otheramt, otheramt2, othernum) select compid, codetype, sum(amt), sum(amt2),sum(num) from #comp_amt where paycode not in(select pay from pay_classify where category = '1' and comp = @compid) and paycode not in(select pay from pay_classify where category = '2' and comp = @compid) group by compid, codetype insert #comp_sum_amt (compid, codetype) select distinct compid, codetype from #comp_amt group by compid, codetype update a set a.cashamt = b.cashamt, a.cashamt2 = b.cashamt2, a.cashnum = b.cashnum from #comp_sum_amt a, #comp_cashamt b where a.codetype = b.codetype and a.compid = b.compid update a set a.cardamt = b.cardamt, a.cardamt2 = b.cardamt2, a.cardnum = b.cardnum from #comp_sum_amt a, #comp_cardamt b where a.codetype = b.codetype and a.compid = b.compid update a set a.otheramt = b.otheramt, a.otheramt2 = b.otheramt2, a.othernum = b.othernum from #comp_sum_amt a, #comp_otheramt b where a.codetype = b.codetype and a.compid = b.compid -- 产品 delete from #comp_amt delete from #comp_cardamt delete from #comp_cashamt delete from #comp_otheramt insert #comp_amt(billid, compid, code, paycode, amt, amt2, num) select distinct billid, compid, code, paycode,isnull(a.amt,0), isnull(a.amt,0) * isnull(b.gso03i,0),quan from #work_detail a , gsm15 b with(nolock) where spid = @spid and b.gso00c = @compid and a.paycode = b.gso02c and a.compid = @compid and a.comp_ascription = @compid and a.action_id = 9 insert #comp_sum_amt (compid, codetype, cashamt, cashamt2, cashnum ) select compid, 'goods', sum(amt), sum(amt2),sum(num) from #comp_amt group by compid insert #comp_cashamt(compid, codetype, cashamt, cashamt2, cashnum) select compid, 'goods', sum(amt), sum(amt2),sum(num) from #comp_amt where paycode in (select pay from pay_classify where category = '1' and comp = @compid) group by compid insert #comp_cardamt(compid, codetype, cardamt, cardamt2, cardnum) select compid, 'goods', sum(amt), sum(amt2),sum(num) from #comp_amt where paycode in (select pay from pay_classify where category = '2' and comp = @compid) group by compid insert #comp_otheramt(compid, codetype, otheramt, otheramt2, othernum) select compid, 'goods', sum(amt), sum(amt2),sum(num) from #comp_amt where paycode not in(select pay from pay_classify where category = '1' and comp = @compid) and paycode not in(select pay from pay_classify where category = '2' and comp = @compid) group by compid update a set a.cashamt = b.cashamt, a.cashamt2 = b.cashamt2, a.cashnum = b.cashnum from #comp_sum_amt a, #comp_cashamt b where a.codetype = b.codetype and a.compid = b.compid update a set a.cardamt = b.cardamt, a.cardamt2 = b.cardamt2, a.cardnum = b.cardnum from #comp_sum_amt a, #comp_cardamt b where a.codetype = b.codetype and a.compid = b.compid update a set a.otheramt = b.otheramt, a.otheramt2 = b.otheramt2, a.othernum = b.othernum from #comp_sum_amt a, #comp_otheramt b where a.codetype = b.codetype and a.compid = b.compid --充值 delete from #comp_amt insert #comp_amt(billid, compid, code, paycode, amt, amt2, num) select distinct billid, compid, code, paycode,isnull(a.amt,0), isnull(a.amt,0) * isnull(b.gso03i,0),quan from #work_detail a , gsm15 b with(nolock) where spid = @spid and b.gso00c = @compid and a.paycode = b.gso02c and a.compid = @compid and a.comp_ascription = @compid and a.action_id = 0 insert #comp_sum_amt (compid, codetype, cashamt, cashamt2, cashnum ) select compid, 'sellcard', sum(amt), sum(amt2),sum(num) from #comp_amt group by compid --卖卡 delete from #comp_amt insert #comp_amt(billid, compid, code, paycode, amt, amt2, num) select distinct billid, compid, code, paycode,isnull(a.amt,0), isnull(a.amt,0) * isnull(b.gso03i,0),quan from #work_detail a, gsm15 b with(nolock) where spid = @spid and b.gso00c = @compid and a.paycode = b.gso02c and a.compid = @compid and a.comp_ascription = @compid and a.action_id = 1 insert #comp_sum_amt (compid, codetype, cardamt, cardamt2, cardnum ) select compid, 'recharge', sum(amt), sum(amt2),sum(num) from #comp_amt group by compid --总现金,总卡付 delete from #comp_amt insert #comp_amt(billid, compid, code, paycode, amt, amt2, num) select distinct billid, compid, code, paycode,isnull(a.amt,0), isnull(a.amt,0) * isnull(b.gso03i,0),quan from #work_detail a , gsm15 b with(nolock) where spid = @spid and b.gso00c = @compid and a.paycode = b.gso02c and a.compid = @compid and a.comp_ascription = @compid insert #comp_sum_amt (compid, codetype, cardamt, cardamt2, cardnum ) select compid, 'sumcashamt', sum(amt), sum(amt2),sum(num) from #comp_amt where paycode in (select pay from pay_classify where category = '1' and comp = @compid) group by compid insert #comp_sum_amt (compid, codetype, cardamt, cardamt2, cardnum ) select compid, 'sumcardamt', sum(amt), sum(amt2),sum(num) from #comp_amt where paycode in (select pay from pay_classify where category = '2' and comp = @compid) group by compid if @amttype in (0, 2) begin select isnull(codetype,'') codetype, isnull(cashamt,'') cashamt, isnull(cashnum,'') cashnum, isnull(cardamt,'') cardamt, isnull(cardnum,'') cardnum, isnull(otheramt,'') otheramt, isnull(othernum,'') othernum from #comp_sum_amt end else begin select isnull(codetype,'') codetype, isnull(cashamt2,'') cashamt, isnull(cashnum,'') cashnum, isnull(cardamt2,'') cardamt, isnull(cardnum,'') cardnum, isnull(otheramt2,'') otheramt, isnull(othernum,'') othernum from #comp_sum_amt end drop table #comp_sum_amt drop table #comp_amt drop table #work_detail drop table #comp_cashamt drop table #comp_cardamt drop table #comp_otheramt end GO /****** Object: StoredProcedure [dbo].[upg_get_billaccount] Script Date: 06/12/2017 06:20:04 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_get_billaccount] ( @compid varchar(10),--门店号 @fromdate varchar(20),--开始时间 @todate varchar(20),--结束时间 @billtype varchar(5),--单据类型 @paycode varchar(5),--单据支付方式 @serviceName varchar(20),--服务人员 @condition varchar(20),--查询条件值 @pageindex int = 1 --页码 ) as begin set @serviceName = '%'+ISNULL(@serviceName,'')+'%' set @condition = '%'+ISNULL(@condition,'')+'%' declare @pagesize int set @pagesize = 10 create table #gnm01_gnm02 ( gna00c varchar(10) NOT NULL, --公司编号 gna01c varchar(20) NOT NULL, --销售单号 gna02d varchar(8) NULL , --销售日期 gna80d varchar(8) NULL , --帐务日期 ,added by MZH, 2005/08/16 gnb03c varchar(20) NOT NULL, --会员卡号 gnb05f float NULL, --标准金额 gnb06f float NULL, --折让 gnb07f float NULL, --折让金额 gnb08f float NULL, --卡销售金额(包括疗程套餐的销售金额) gna20c varchar(10) NULL, --负责业务 name1 varchar(20) NULL default '', --姓名 gna28c varchar(10) NULL, --第二卖卡人 name2 varchar(20) NULL default '', --姓名2 gna30c varchar(10) NULL, --第三卖卡人 name3 varchar(20) NULL default '', --姓名3 gna32c varchar(10) NULL, --第四卖卡人 name4 varchar(20) NULL default '', --姓名4 gna91c varchar(10) NULL --登陆人员代码 ) create table #ggm01_ggm02 ( gga00c varchar(10) NOT NULL, --公司编号 gga01c varchar(20) NOT NULL, --消费单号 gga02d varchar(8) NULL , --消费日期 gga80d varchar(8) NULL , --帐务日期 ,added by MZH, 2005/08/15 gga03t varchar(6) NULL , --消费开始时间 gga04t varchar(6) NULL , --消费结束时间 gga05c varchar(20) NULL , --会员代码,927版本 gga06c varchar(20) NULL , --客户姓名,如果是会员则是会员姓名 gga07c varchar(10) NULL , --会员卡类别(消费时候) gga08c varchar(20) NULL , --匙牌号 gga09c varchar(20) NULL , --会员卡号 ggb02f float NOT NULL, --序号 ggb03c varchar(20) NULL, --项目代码 ggb04c varchar(5) NULL, --单位 ggb05f float NULL, --数量 ggb08f float NULL, --标准单价 ggb09f float NULL, --折让 ggb10f float NULL, --折让单价 ggb11f float NULL, --金额 ggb12c varchar(20) NULL, --大工代码 name1 varchar(20) NULL default '', --姓名1 ggb13c varchar(20) NULL, --中工代码 name2 varchar(20) NULL default '', --姓名2 ggb14c varchar(20) NULL, --小工代码 name3 varchar(20) NULL default '', --姓名3 ggb26c varchar(20) NULL, --四工代码 name4 varchar(20) NULL default '', --姓名4 ggb27c varchar(5) NULL, --项目支付方式 Added by WUXM 2005/11/09 gga91c varchar(10) NULL --登陆人员代码 ) create table #ggm01_ggm03 ( gga00c varchar(10) NOT NULL, --公司编号 gga01c varchar(20) NOT NULL, --消费单号 gga02d varchar(8) NULL , --消费日期 gga80d varchar(8) NULL , --帐务日期 ,added by MZH, 2005/08/15 gga03t varchar(6) NULL , --消费开始时间 gga04t varchar(6) NULL , --消费结束时间 gga05c varchar(20) NULL , --会员代码,927版本 gga06c varchar(20) NULL , --客户姓名,如果是会员则是会员姓名 gga07c varchar(10) NULL , --会员卡类别(消费时候) gga08c varchar(20) NULL , --匙牌号 gga09c varchar(20) NULL , --会员卡号 ggc02f float NOT NULL, --序号 ggc03c varchar(20) NULL, --产品代码 ggc04c varchar(5) NULL, --单位 ggc05f float NULL, --数量 ggc08f float NULL, --标准单价 ggc09f float NULL, --折让 ggc10f float NULL, --折让单价 ggc11f float NULL, --金额 ggc12c varchar(20) NULL, --产品销售人员 name1 varchar(20) NULL default '', --姓名1 ggc15c varchar(20) NULL, --第二人 name2 varchar(20) NULL default '', --姓名2 ggc17c varchar(20) NULL, --第三人 name3 varchar(20) NULL default '', --姓名3 ggc27c varchar(5) NULL, --产品支付方式 gga91c varchar(10) NULL --登陆人员代码 ) create table #gcm10 ( gcl00c varchar(10) NOT NULL, --公司编号 gcl01c varchar(20) NOT NULL, --异动单号 gcl02d varchar(8) NOT NULL, --异动日期 gcl03c varchar(20) NULL, --会员卡号 gcl04c varchar(5) NULL, --帐号类别(1.电子钱包 2.储值帐户) gcl05c varchar(20) NULL, --是否为重新购卡 1 gcl06i int NULL, --异动类别( 0充值 ,1取款 5欠款 6还款) gcl07f float NULL, --异动金额(总收款金额),含套餐,含疗程 gcl08f float NULL, --异动前余额 gcl09c varchar(20) NULL, --销售人员 name1 varchar(20) NULL default '', --姓名1 gcl17c varchar(20) null, --第二销售 name2 varchar(20) NULL default '', --姓名2 gcl19c varchar(20) null, --第三负责人 name3 varchar(20) NULL default '', --姓名3 gcl22c varchar(20) null, --第4负责人 name4 varchar(20) NULL default '', --姓名4 gcl80d varchar(8) NULL, --帐务日期 added by MZH, 2005/08/16 gcl91c varchar(10) NULL --登陆人员代码 ) create table #gem01 ( gea00c varchar(10) NOT NULL, --公司编号 gea01c varchar(20) NOT NULL, --异动单号 gea02i int NOT NULL, --异动类别( 开卡, 卡种变更, 延期 ,批量延期,退卡) gea03d varchar(8) NULL , --异动日期 gea04c varchar(20) NULL , --会员卡号 gea12f float NULL , --收费总额,收款总额 gea80d varchar(8) NULL , --帐务日期 ,added by MZH, 2005/08/19 gea91c varchar(10) NULL --登陆人员代码 ) create table #gsm03 ( gsc00c varchar(10) NOT NULL, --公司编号 gsc01c varchar(20) NOT NULL, --单据编号 gsc02c varchar(5) NOT NULL, --单据类别 gsc03f float NOT NULL, --序号 gsc04c varchar(5) NULL, --支付方式 gsc05f float NULL, --支付金额 gsc06c varchar(100) NULL, --备注说明 gsc07f float NULL, --预留-标志单据是否已经付款了(1.付款 0.未付款) gsc08c varchar(20) NULL, --收银钥匙牌号,卡销售会员卡号 gsc09c varchar(20) NULL, --账户异动单号 ,Added by WUXM, 2004/04/10 gsc10d varchar(8) NULL --单据帐务日期 ) create table #billItem ( compid varchar(10) not null, billid varchar(20) not null, billtype varchar(5) not null, billamount float null ) create table #result ( compid varchar(10) not null, --公司编号 compname varchar(70) null, --公司名称 billid varchar(20) not null, --单据编号 billtype varchar(5) not null, --单据类别 totalamount float null, --总金额 date varchar(8) null, --单据时间 accountdate varchar(8) null, --账务日期 prjcode varchar(20) null, --代码 prjname varchar(30) null, --名称 type varchar(2) null, --类型 1:项目 2:产品 paycode varchar(5) null, --支付方式 payamount float null, --支付金额 cardid varchar(20) null, --会员卡号 mobile varchar(20) null, --手机号码 memberid varchar(20) null, --会员编号 membername varchar(40) null, --会员姓名 userid varchar(10) null, --登录人员代码 person1 varchar(20) null, --服务人员1 name1 varchar(20) null default '', --名称 person2 varchar(20) null, --服务人员2 name2 varchar(20) null default '', --名称2 person3 varchar(20) null, --服务人员3 name3 varchar(20) null default '', --名称3 person4 varchar(20) null, --服务人员4 name4 varchar(20) null default '', --名称4 paycode1 varchar(5) null, --支付方式1 payname1 varchar(20) null, --支付方式名称 amount1 float null, --金额 paycode2 varchar(5) null, --支付方式2 payname2 varchar(20) null, --支付方式名称 amount2 float null, --金额 paycode3 varchar(5) null, --支付方式3 payname3 varchar(20) null, --支付方式名称 amount3 float null, --金额 paycode4 varchar(5) null, --支付方式4 payname4 varchar(20) null, --支付方式名称 amount4 float null, --金额 paycode5 varchar(5) null, --支付方式5 payname5 varchar(20) null, --支付方式名称 amount5 float null, --金额 paycode6 varchar(5) null, --支付方式6 payname6 varchar(20) null, --支付方式名称 amount6 float null, --金额 paycode7 varchar(5) null, --支付方式7 payname7 varchar(20) null, --支付方式名称 amount7 float null, --金额 paycode8 varchar(5) null, --支付方式8 payname8 varchar(20) null, --支付方式名称 amount8 float null, --金额 paycode9 varchar(5) null, --支付方式9 payname9 varchar(20) null, --支付方式名称 amount9 float null --金额 ) create table #tb_result ( idd int identity(1,1) primary key not null, compid varchar(10) not null, --公司编号 compname varchar(70) null, --公司名称 billid varchar(20) not null, --单据编号 billtype varchar(5) not null, --单据类别 totalamount float null, --总金额 date varchar(8) null, --单据时间 accountdate varchar(8) null, --账务日期 cardid varchar(20) null, --会员卡号 mobile varchar(20) null, --手机号码 memberid varchar(20) null, --会员编号 membername varchar(40) null, --会员姓名 userid varchar(10) null, --登录人员代码 person1 varchar(20) null, --服务人员1 name1 varchar(20) null default '', --名称 person2 varchar(20) null, --服务人员2 name2 varchar(20) null default '', --名称2 person3 varchar(20) null, --服务人员3 name3 varchar(20) null default '', --名称3 person4 varchar(20) null, --服务人员4 name4 varchar(20) null default '', --名称4 paycode1 varchar(5) null, --支付方式1 payname1 varchar(20) null, --支付方式名称 amount1 float null, --金额 paycode2 varchar(5) null, --支付方式2 payname2 varchar(20) null, --支付方式名称 amount2 float null, --金额 paycode3 varchar(5) null, --支付方式3 payname3 varchar(20) null, --支付方式名称 amount3 float null, --金额 paycode4 varchar(5) null, --支付方式4 payname4 varchar(20) null, --支付方式名称 amount4 float null, --金额 paycode5 varchar(5) null, --支付方式5 payname5 varchar(20) null, --支付方式名称 amount5 float null, --金额 paycode6 varchar(5) null, --支付方式6 payname6 varchar(20) null, --支付方式名称 amount6 float null, --金额 paycode7 varchar(5) null, --支付方式7 payname7 varchar(20) null, --支付方式名称 amount7 float null, --金额 paycode8 varchar(5) null, --支付方式8 payname8 varchar(20) null, --支付方式名称 amount8 float null, --金额 paycode9 varchar(5) null, --支付方式9 payname9 varchar(20) null, --支付方式名称 amount9 float null --金额 ) if(@billtype='gn') --卖卡 begin insert into #gnm01_gnm02(gna00c,gna01c,gna02d,gna80d,gnb03c,gnb05f,gnb06f,gnb07f,gnb08f,gna20c,gna28c,gna30c,gna32c,gna91c) select a.gna00c,a.gna01c,a.gna02d,a.gna80d,b.gnb03c,b.gnb05f,b.gnb06f,b.gnb07f,b.gnb08f,a.gna20c,a.gna28c,a.gna30c,a.gna32c,a.gna91c from gnm01 a with(NOLOCK),gnm02 b with(NOLOCK) where a.gna00c = @compid and a.gna80d >= @fromdate and a.gna80d <= @todate and a.gna00c = b.gnb00c and a.gna01c = b.gnb01c insert into #gsm03(gsc00c,gsc01c,gsc02c,gsc03f,gsc04c,gsc05f,gsc06c,gsc07f,gsc08c,gsc09c,gsc10d) select gsc00c,gsc01c,gsc02c,gsc03f,gsc04c,gsc05f,gsc06c,gsc07f,gsc08c,gsc09c,gsc10d from gsm03 with(NOLOCK) where gsc00c = @compid and gsc10d >= @fromdate and gsc10d <= @todate and gsc02c = 'gn' insert into #billItem(compid,billid,billtype,billamount) select gsc00c,gsc01c,gsc02c,sum(gsc05f) from #gsm03 group by gsc00c,gsc01c,gsc02c update a set name1 = b.haa02c from #gnm01_gnm02 a,ham01 b where a.gna20c = b.haa01c and a.gna00c = b.haa00c update a set name2 = b.haa02c from #gnm01_gnm02 a,ham01 b where a.gna28c = b.haa01c and a.gna00c = b.haa00c update a set name3 = b.haa02c from #gnm01_gnm02 a,ham01 b where a.gna30c = b.haa01c and a.gna00c = b.haa00c update a set name4 = b.haa02c from #gnm01_gnm02 a,ham01 b where a.gna32c = b.haa01c and a.gna00c = b.haa00c insert into #result(compid,billid,billtype,date,accountdate,paycode,payamount,cardid,person1,name1,person2,name2,person3,name3,person4,name4,userid) select a.gna00c,a.gna01c,'gn',a.gna02d,a.gna80d,b.gsc04c,b.gsc05f,a.gnb03c,a.gna20c,a.name1,a.gna28c,a.name2,a.gna30c,a.name3,a.gna32c,a.name4,a.gna91c from #gnm01_gnm02 a,#gsm03 b where a.gna00c = b.gsc00c and a.gna01c = b.gsc01c and (a.name1 like @serviceName or a.name2 like @serviceName or a.name3 like @serviceName or a.name4 like @serviceName) end else if(@billtype='gx') --收银 begin insert into #ggm01_ggm02(gga00c,gga01c,gga02d,gga80d,gga03t,gga04t,gga05c,gga06c,gga07c,gga08c,gga09c,ggb02f,ggb03c,ggb04c,ggb05f,ggb08f,ggb09f,ggb10f,ggb11f,ggb12c,ggb13c,ggb14c,ggb26c,ggb27c,gga91c) select a.gga00c,a.gga01c,a.gga02d,a.gga80d,a.gga03t,a.gga04t,a.gga05c,a.gga06c,a.gga07c,a.gga08c,a.gga09c,b.ggb02f,b.ggb03c,b.ggb04c,b.ggb05f,b.ggb08f, b.ggb09f,b.ggb10f,b.ggb11f,b.ggb12c,b.ggb13c,b.ggb14c,b.ggb26c,b.ggb27c,a.gga91c from ggm01 a with(NOLOCK),ggm02 b with(NOLOCK) where a.gga00c = b.ggb00c and a.gga01c = b.ggb01c and a.gga00c = @compid and a.gga80d >= @fromdate and a.gga80d <= @todate update a set name1 = b.haa02c from #ggm01_ggm02 a,ham01 b where a.gga00c = b.haa00c and a.ggb12c = b.haa01c update a set name2 = b.haa02c from #ggm01_ggm02 a,ham01 b where a.gga00c = b.haa00c and a.ggb13c = b.haa01c update a set name3 = b.haa02c from #ggm01_ggm02 a,ham01 b where a.gga00c = b.haa00c and a.ggb14c = b.haa01c update a set name4 = b.haa02c from #ggm01_ggm02 a,ham01 b where a.gga00c = b.haa00c and a.ggb26c = b.haa01c insert into #ggm01_ggm03(gga00c,gga01c,gga02d,gga80d,gga03t,gga04t,gga05c,gga06c,gga07c,gga08c,gga09c,ggc02f,ggc03c,ggc04c,ggc05f,ggc08f,ggc09f,ggc10f,ggc11f,ggc12c,ggc15c,ggc17c,ggc27c,gga91c) select a.gga00c,a.gga01c,a.gga02d,a.gga80d,a.gga03t,a.gga04t,a.gga05c,a.gga06c,a.gga07c,a.gga08c,a.gga09c,b.ggc02f,b.ggc03c,b.ggc04c,b.ggc05f,b.ggc08f, b.ggc09f,b.ggc10f,b.ggc11f,b.ggc12c,b.ggc15c,b.ggc17c,b.ggc27c,a.gga91c from ggm01 a with(NOLOCK),ggm03 b with(NOLOCK) where a.gga00c = b.ggc00c and a.gga01c = b.ggc01c and a.gga00c = @compid and a.gga80d >= @fromdate and a.gga80d <= @todate update a set name1 = b.haa02c from #ggm01_ggm03 a,ham01 b where a.gga00c = b.haa00c and a.ggc12c = b.haa01c update a set name2 = b.haa02c from #ggm01_ggm03 a,ham01 b where a.gga00c = b.haa00c and a.ggc15c = b.haa01c update a set name3 = b.haa02c from #ggm01_ggm03 a,ham01 b where a.gga00c = b.haa00c and a.ggc17c = b.haa01c insert into #gsm03(gsc00c,gsc01c,gsc02c,gsc03f,gsc04c,gsc05f,gsc06c,gsc07f,gsc08c,gsc09c,gsc10d) select gsc00c,gsc01c,gsc02c,gsc03f,gsc04c,gsc05f,gsc06c,gsc07f,gsc08c,gsc09c,gsc10d from gsm03 with(NOLOCK) where gsc00c = @compid and gsc10d >= @fromdate and gsc10d <= @todate and gsc02c = 'gx' insert into #billItem(compid,billid,billtype,billamount) select gsc00c,gsc01c,gsc02c,sum(gsc05f) from #gsm03 group by gsc00c,gsc01c,gsc02c insert into #result(compid,billid,billtype,date,accountdate,prjcode,prjname,type,paycode,payamount,cardid,userid) select a.gga00c,a.gga01c,'gx',a.gga02d,a.gga80d,a.ggb03c,c.gda03c,'1',b.gsc04c,b.gsc05f,a.gga08c,a.gga91c from #ggm01_ggm02 a,#gsm03 b,gdm01 c with(NOLOCK) where a.gga00c = b.gsc00c and a.gga01c = b.gsc01c and a.gga00c = c.gda00c and a.ggb03c = c.gda01c and (a.name1 like @serviceName or a.name2 like @serviceName or a.name3 like @serviceName or a.name4 like @serviceName) insert into #result(compid,billid,billtype,date,accountdate,prjcode,prjname,type,paycode,payamount,cardid,userid) select a.gga00c,a.gga01c,'gx',a.gga02d,a.gga80d,a.ggc03c,c.gfa03c,'2',b.gsc04c,b.gsc05f,a.gga08c,a.gga91c from #ggm01_ggm03 a,#gsm03 b,gfm01 c with(NOLOCK) where a.gga00c = b.gsc00c and a.gga01c = b.gsc01c and a.gga00c = c.gfa00c and a.ggc03c = c.gfa01c and (a.name1 like @serviceName or a.name2 like @serviceName or a.name3 like @serviceName) end else if(@billtype='gz') --充值 begin insert into #gcm10(gcl00c,gcl01c,gcl02d,gcl03c,gcl04c,gcl05c,gcl06i,gcl07f,gcl08f,gcl09c,gcl17c,gcl19c,gcl22c,gcl80d,gcl91c) select a.gcl00c,a.gcl01c,a.gcl02d,a.gcl03c,a.gcl04c,a.gcl05c,a.gcl06i,a.gcl07f,a.gcl08f,a.gcl09c,a.gcl17c,a.gcl19c,a.gcl22c,a.gcl80d,a.gcl91c from gcm10 a with(NOLOCK) where a.gcl00c = @compid and a.gcl80d >= @fromdate and a.gcl80d <= @todate update a set name1 = b.haa02c from #gcm10 a,ham01 b where a.gcl00c = b.haa00c and a.gcl09c = b.haa01c update a set name2 = b.haa02c from #gcm10 a,ham01 b where a.gcl00c = b.haa00c and a.gcl17c = b.haa01c update a set name3 = b.haa02c from #gcm10 a,ham01 b where a.gcl00c = b.haa00c and a.gcl19c = b.haa01c update a set name4 = b.haa02c from #gcm10 a,ham01 b where a.gcl00c = b.haa00c and a.gcl22c = b.haa01c insert into #gsm03(gsc00c,gsc01c,gsc02c,gsc03f,gsc04c,gsc05f,gsc06c,gsc07f,gsc08c,gsc10d) select gsc00c,gsc01c,gsc02c,gsc03f,gsc04c,gsc05f,gsc06c,gsc07f,gsc08c,gsc10d from gsm03 with(NOLOCK) where gsc00c = @compid and gsc10d >= @fromdate and gsc10d <= @todate and gsc02c = 'gz' insert into #billItem(compid,billid,billtype,billamount) select gsc00c,gsc01c,gsc02c,sum(gsc05f) from #gsm03 group by gsc00c,gsc01c,gsc02c insert into #result(compid,billid,billtype,date,accountdate,paycode,payamount,cardid,userid,person1,name1,person2,name2,person3,name3,person4,name4) select a.gcl00c,a.gcl01c,'gz',gcl02d,gcl80d,gsc04c,gsc05f,a.gcl03c,a.gcl91c,a.gcl09c,a.name1,a.gcl17c,a.name2,a.gcl19c,a.name3,a.gcl22c,a.name4 from #gcm10 a,#gsm03 b where a.gcl00c = b.gsc00c and a.gcl01c = b.gsc01c and (a.name1 like @serviceName or a.name2 like @serviceName or a.name3 like @serviceName or a.name4 like @serviceName) end else if(@billtype='ge') --退卡 begin insert into #gem01(gea00c,gea01c,gea02i,gea03d,gea04c,gea12f,gea80d,gea91c) select gea00c,gea01c,gea02i,gea03d,gea04c,gea12f,gea80d,gea91c from gem01 with(NOLOCK) where gea00c = @compid and gea80d >= @fromdate and gea80d <= @todate insert into #gsm03(gsc00c,gsc01c,gsc02c,gsc03f,gsc04c,gsc05f,gsc06c,gsc07f,gsc08c,gsc10d) select gsc00c,gsc01c,gsc02c,gsc03f,gsc04c,gsc05f,gsc06c,gsc07f,gsc08c,gsc10d from gsm03 with(NOLOCK) where gsc00c = @compid and gsc10d >= @fromdate and gsc10d <= @todate and gsc02c = 'ge' insert into #billItem(compid,billid,billtype,billamount) select gsc00c,gsc01c,gsc02c,sum(gsc05f) from #gsm03 group by gsc00c,gsc01c,gsc02c insert into #result(compid,billid,billtype,date,accountdate,paycode,payamount,cardid,userid) select a.gea00c,a.gea01c,'ge',a.gea03d,a.gea80d,b.gsc04c,b.gsc05f,a.gea04c,a.gea91c from #gem01 a,#gsm03 b where a.gea00c = b.gsc00c and a.gea01c = b.gsc01c end else --全部 begin insert into #gnm01_gnm02(gna00c,gna01c,gna02d,gna80d,gnb03c,gnb05f,gnb06f,gnb07f,gnb08f,gna20c,gna28c,gna30c,gna32c,gna91c) select a.gna00c,a.gna01c,a.gna02d,a.gna80d,b.gnb03c,b.gnb05f,b.gnb06f,b.gnb07f,b.gnb08f,a.gna20c,a.gna28c,a.gna30c,a.gna32c,a.gna91c from gnm01 a with(NOLOCK),gnm02 b with(NOLOCK) where a.gna00c = @compid and a.gna80d >= @fromdate and a.gna80d <= @todate and a.gna00c = b.gnb00c and a.gna01c = b.gnb01c insert into #ggm01_ggm02(gga00c,gga01c,gga02d,gga80d,gga03t,gga04t,gga05c,gga06c,gga07c,gga08c,gga09c,ggb02f,ggb03c,ggb04c,ggb05f,ggb08f,ggb09f,ggb10f,ggb11f,ggb12c,ggb13c,ggb14c,ggb26c,ggb27c,gga91c) select a.gga00c,a.gga01c,a.gga02d,a.gga80d,a.gga03t,a.gga04t,a.gga05c,a.gga06c,a.gga07c,a.gga08c,a.gga09c,b.ggb02f,b.ggb03c,b.ggb04c,b.ggb05f,b.ggb08f,b.ggb09f,b.ggb10f,b.ggb11f,b.ggb12c,b.ggb13c,b.ggb14c,b.ggb26c,b.ggb27c,a.gga91c from ggm01 a with(NOLOCK),ggm02 b with(NOLOCK) where a.gga00c = b.ggb00c and a.gga01c = b.ggb01c and a.gga00c = @compid and a.gga80d >= @fromdate and a.gga80d <= @todate insert into #ggm01_ggm03(gga00c,gga01c,gga02d,gga80d,gga03t,gga04t,gga05c,gga06c,gga07c,gga08c,gga09c,ggc02f,ggc03c,ggc04c,ggc05f,ggc08f,ggc09f,ggc10f,ggc11f,ggc12c,ggc15c,ggc17c,ggc27c,gga91c) select a.gga00c,a.gga01c,a.gga02d,a.gga80d,a.gga03t,a.gga04t,a.gga05c,a.gga06c,a.gga07c,a.gga08c,a.gga09c,b.ggc02f,b.ggc03c,b.ggc04c,b.ggc05f,b.ggc08f,b.ggc09f,b.ggc10f,b.ggc11f,b.ggc12c,b.ggc15c,b.ggc17c,b.ggc27c,a.gga91c from ggm01 a with(NOLOCK),ggm03 b with(NOLOCK) where a.gga00c = b.ggc00c and a.gga01c = b.ggc01c and a.gga00c = @compid and a.gga80d >= @fromdate and a.gga80d <= @todate insert into #gcm10(gcl00c,gcl01c,gcl02d,gcl03c,gcl04c,gcl05c,gcl06i,gcl07f,gcl08f,gcl09c,gcl17c,gcl19c,gcl22c,gcl80d,gcl91c) select a.gcl00c,a.gcl01c,a.gcl02d,a.gcl03c,a.gcl04c,a.gcl05c,a.gcl06i,a.gcl07f,a.gcl08f,a.gcl09c,a.gcl17c,a.gcl19c,a.gcl22c,a.gcl80d,a.gcl91c from gcm10 a with(NOLOCK) where a.gcl00c = @compid and a.gcl80d >= @fromdate and a.gcl80d <= @todate insert into #gem01(gea00c,gea01c,gea02i,gea03d,gea04c,gea12f,gea80d,gea91c) select gea00c,gea01c,gea02i,gea03d,gea04c,gea12f,gea80d,gea91c from gem01 with(NOLOCK) where gea00c = @compid and gea80d >= @fromdate and gea80d <= @todate --卖卡 update a set name1 = b.haa02c from #gnm01_gnm02 a,ham01 b where a.gna20c = b.haa01c and a.gna00c = b.haa00c update a set name2 = b.haa02c from #gnm01_gnm02 a,ham01 b where a.gna28c = b.haa01c and a.gna00c = b.haa00c update a set name3 = b.haa02c from #gnm01_gnm02 a,ham01 b where a.gna30c = b.haa01c and a.gna00c = b.haa00c update a set name4 = b.haa02c from #gnm01_gnm02 a,ham01 b where a.gna32c = b.haa01c and a.gna00c = b.haa00c --收银 update a set name1 = b.haa02c from #ggm01_ggm02 a,ham01 b where a.gga00c = b.haa00c and a.ggb12c = b.haa01c update a set name2 = b.haa02c from #ggm01_ggm02 a,ham01 b where a.gga00c = b.haa00c and a.ggb13c = b.haa01c update a set name3 = b.haa02c from #ggm01_ggm02 a,ham01 b where a.gga00c = b.haa00c and a.ggb14c = b.haa01c update a set name4 = b.haa02c from #ggm01_ggm02 a,ham01 b where a.gga00c = b.haa00c and a.ggb26c = b.haa01c update a set name1 = b.haa02c from #ggm01_ggm03 a,ham01 b where a.gga00c = b.haa00c and a.ggc12c = b.haa01c update a set name2 = b.haa02c from #ggm01_ggm03 a,ham01 b where a.gga00c = b.haa00c and a.ggc15c = b.haa01c update a set name3 = b.haa02c from #ggm01_ggm03 a,ham01 b where a.gga00c = b.haa00c and a.ggc17c = b.haa01c --充值 update a set name1 = b.haa02c from #gcm10 a,ham01 b where a.gcl00c = b.haa00c and a.gcl09c = b.haa01c update a set name2 = b.haa02c from #gcm10 a,ham01 b where a.gcl00c = b.haa00c and a.gcl17c = b.haa01c update a set name3 = b.haa02c from #gcm10 a,ham01 b where a.gcl00c = b.haa00c and a.gcl19c = b.haa01c update a set name4 = b.haa02c from #gcm10 a,ham01 b where a.gcl00c = b.haa00c and a.gcl22c = b.haa01c insert into #gsm03(gsc00c,gsc01c,gsc02c,gsc03f,gsc04c,gsc05f,gsc06c,gsc07f,gsc08c,gsc09c,gsc10d) select gsc00c,gsc01c,gsc02c,gsc03f,gsc04c,gsc05f,gsc06c,gsc07f,gsc08c,gsc09c,gsc10d from gsm03 with(NOLOCK) where gsc00c = @compid and gsc10d >= @fromdate and gsc10d <= @todate insert into #billItem(compid,billid,billtype,billamount) select gsc00c,gsc01c,gsc02c,sum(gsc05f) from #gsm03 group by gsc00c,gsc01c,gsc02c insert into #result(compid,billid,billtype,date,accountdate,paycode,payamount,cardid,userid,person1,name1,person2,name2,person3,name3,person4,name4) select a.gna00c,a.gna01c,'gn',a.gna02d,a.gna80d,b.gsc04c,b.gsc05f,a.gnb03c,a.gna91c,a.gna20c,a.name1,a.gna28c,a.name2,a.gna30c,a.name3,a.gna32c,a.name4 from #gnm01_gnm02 a,#gsm03 b where a.gna00c = b.gsc00c and a.gna01c = b.gsc01c and b.gsc02c = 'gn' and (a.name1 like @serviceName or a.name2 like @serviceName or a.name3 like @serviceName or a.name4 like @serviceName) insert into #result(compid,billid,billtype,date,accountdate,prjcode,prjname,type,paycode,payamount,cardid,userid) select a.gga00c,a.gga01c,'gx',a.gga02d,a.gga80d,a.ggb03c,c.gda03c,'1',b.gsc04c,b.gsc05f,a.gga08c,a.gga91c from #ggm01_ggm02 a,#gsm03 b,gdm01 c with(NOLOCK) where a.gga00c = b.gsc00c and a.gga01c = b.gsc01c and a.gga00c = c.gda00c and a.ggb03c = c.gda01c and b.gsc02c = 'gx' and (a.name1 like @serviceName or a.name2 like @serviceName or a.name3 like @serviceName or a.name4 like @serviceName) insert into #result(compid,billid,billtype,date,accountdate,prjcode,prjname,type,paycode,payamount,cardid,userid) select a.gga00c,a.gga01c,'gx',a.gga02d,a.gga80d,a.ggc03c,c.gfa03c,'2',b.gsc04c,b.gsc05f,a.gga08c,a.gga91c from #ggm01_ggm03 a,#gsm03 b,gfm01 c with(NOLOCK) where a.gga00c = b.gsc00c and a.gga01c = b.gsc01c and a.gga00c = c.gfa00c and a.ggc03c = c.gfa01c and b.gsc02c = 'gx' and (a.name1 like @serviceName or a.name2 like @serviceName or a.name3 like @serviceName ) insert into #result(compid,billid,billtype,date,accountdate,paycode,payamount,cardid,userid,person1,name1,person2,name2,person3,name3,person4,name4) select a.gcl00c,a.gcl01c,'gz',gcl02d,gcl80d,gsc04c,gsc05f,a.gcl03c,a.gcl91c,a.gcl09c,a.name1,a.gcl17c,a.name2,a.gcl19c,a.name3,a.gcl22c,a.name4 from #gcm10 a,#gsm03 b where a.gcl00c = b.gsc00c and a.gcl01c = b.gsc01c and b.gsc02c = 'gz' and (a.name1 like @serviceName or a.name2 like @serviceName or a.name3 like @serviceName or a.name4 like @serviceName) insert into #result(compid,billid,billtype,date,accountdate,paycode,payamount,cardid,userid) select a.gea00c,a.gea01c,'ge',a.gea03d,a.gea80d,b.gsc04c,b.gsc05f,a.gea04c,a.gea91c from #gem01 a,#gsm03 b where a.gea00c = b.gsc00c and a.gea01c = b.gsc01c and b.gsc02c = 'ge' end update a set a.mobile = c.gba08c,a.memberid = c.gba01c,a.membername = c.gba03c from #result a,gcm01 b with(NOLOCK),gbm01 c with(NOLOCK) where a.cardid = b.gca01c and b.gca00c = b.gca13d and b.gca00c = c.gba00c and b.gca04c = c.gba01c update a set a.totalamount = b.billamount from #result a,#billItem b where a.compid = b.compid and a.billid = b.billid and a.billtype = b.billtype update a set a.compname = b.gae02c from #result a,gam05 b where a.compid = b.gae01c --支付方式1 update a set a.paycode1 = b.gsc04c,a.payname1 = c.gsb03c,a.amount1 = b.gsc05f from #result a,#gsm03 b,gsm02 c where a.compid = b.gsc00c and a.billid = b.gsc01c and a.billtype = b.gsc02c and b.gsc03f = 0 and b.gsc00c = c.gsb00c and c.gsb01c = '3' and b.gsc04c = c.gsb02c --支付方式2 update a set a.paycode2 = b.gsc04c,a.payname2 = c.gsb03c,a.amount2 = b.gsc05f from #result a,#gsm03 b,gsm02 c where a.compid = b.gsc00c and a.billid = b.gsc01c and a.billtype = b.gsc02c and b.gsc03f = 1 and b.gsc00c = c.gsb00c and c.gsb01c = '3' and b.gsc04c = c.gsb02c --支付方式3 update a set a.paycode3 = b.gsc04c,a.payname3 = c.gsb03c,a.amount3 = b.gsc05f from #result a,#gsm03 b,gsm02 c where a.compid = b.gsc00c and a.billid = b.gsc01c and a.billtype = b.gsc02c and b.gsc03f = 2 and b.gsc00c = c.gsb00c and c.gsb01c = '3' and b.gsc04c = c.gsb02c --支付方式4 update a set a.paycode4 = b.gsc04c,a.payname4 = c.gsb03c,a.amount4 = b.gsc05f from #result a,#gsm03 b,gsm02 c where a.compid = b.gsc00c and a.billid = b.gsc01c and a.billtype = b.gsc02c and b.gsc03f = 3 and b.gsc00c = c.gsb00c and c.gsb01c = '3' and b.gsc04c = c.gsb02c --支付方式5 update a set a.paycode5 = b.gsc04c,a.payname5 = c.gsb03c,a.amount5 = b.gsc05f from #result a,#gsm03 b,gsm02 c where a.compid = b.gsc00c and a.billid = b.gsc01c and a.billtype = b.gsc02c and b.gsc03f = 4 and b.gsc00c = c.gsb00c and c.gsb01c = '3' and b.gsc04c = c.gsb02c --支付方式6 update a set a.paycode6 = b.gsc04c,a.payname6 = c.gsb03c,a.amount6 = b.gsc05f from #result a,#gsm03 b,gsm02 c where a.compid = b.gsc00c and a.billid = b.gsc01c and a.billtype = b.gsc02c and b.gsc03f = 5 and b.gsc00c = c.gsb00c and c.gsb01c = '3' and b.gsc04c = c.gsb02c --支付方式7 update a set a.paycode7 = b.gsc04c,a.payname7 = c.gsb03c,a.amount7 = b.gsc05f from #result a,#gsm03 b,gsm02 c where a.compid = b.gsc00c and a.billid = b.gsc01c and a.billtype = b.gsc02c and b.gsc03f = 6 and b.gsc00c = c.gsb00c and c.gsb01c = '3' and b.gsc04c = c.gsb02c --支付方式8 update a set a.paycode8 = b.gsc04c,a.payname8 = c.gsb03c,a.amount8 = b.gsc05f from #result a,#gsm03 b,gsm02 c where a.compid = b.gsc00c and a.billid = b.gsc01c and a.billtype = b.gsc02c and b.gsc03f = 7 and b.gsc00c = c.gsb00c and c.gsb01c = '3' and b.gsc04c = c.gsb02c --支付方式9 update a set a.paycode9 = b.gsc04c,a.payname9 = c.gsb03c,a.amount9 = b.gsc05f from #result a,#gsm03 b,gsm02 c where a.compid = b.gsc00c and a.billid = b.gsc01c and a.billtype = b.gsc02c and b.gsc03f = 8 and b.gsc00c = c.gsb00c and c.gsb01c = '3' and b.gsc04c = c.gsb02c declare @sql nvarchar(4000) if(ISNULL(@paycode,'')='') begin if(@condition <> '%%') begin set @sql = ' insert into #tb_result(compid,compname,billid,billtype,date,accountdate,cardid,mobile,memberid,membername,totalamount,userid,person1,name1,person2,name2,person3,name3,person4,name4, paycode1,payname1,amount1,paycode2,payname2,amount2,paycode3,payname3,amount3,paycode4,payname4,amount4,paycode5,payname5,amount5,paycode6,payname6,amount6, paycode7,payname7,amount7,paycode8,payname8,amount8,paycode9,payname9,amount9) select compid,compname,billid,billtype,date,accountdate,cardid,mobile,memberid,membername,totalamount,userid,person1,name1,person2,name2,person3,name3,person4,name4, paycode1,payname1,amount1,paycode2,payname2,amount2,paycode3,payname3,amount3,paycode4,payname4,amount4,paycode5,payname5,amount5,paycode6,payname6,amount6, paycode7,payname7,amount7,paycode8,payname8,amount8,paycode9,payname9,amount9 from #result where mobile like ''' + @condition + '''' + ' or payamount like ''' + @condition + '''' + ' or cardid like ''' + @condition + '''' + ' or memberid like ''' + @condition + '''' + ' or membername like ''' + @condition + '''' + ' or billid like ''' + @condition + '''' + ' or prjcode like ''' + @condition + '''' + ' or prjname like ''' + @condition + '''' + ' group by compid,compname,billid,billtype,date,accountdate,cardid,mobile,memberid,membername,totalamount,userid,person1,name1,person2,name2,person3,name3,person4,name4, paycode1,payname1,amount1,paycode2,payname2,amount2,paycode3,payname3,amount3,paycode4,payname4,amount4,paycode5,payname5,amount5,paycode6,payname6,amount6, paycode7,payname7,amount7,paycode8,payname8,amount8,paycode9,payname9,amount9 order by date desc ' end else begin set @sql = ' insert into #tb_result(compid,compname,billid,billtype,date,accountdate,cardid,mobile,memberid,membername,totalamount,userid,person1,name1,person2,name2,person3,name3,person4,name4, paycode1,payname1,amount1,paycode2,payname2,amount2,paycode3,payname3,amount3,paycode4,payname4,amount4,paycode5,payname5,amount5,paycode6,payname6,amount6, paycode7,payname7,amount7,paycode8,payname8,amount8,paycode9,payname9,amount9) select compid,compname,billid,billtype,date,accountdate,cardid,mobile,memberid,membername,totalamount,userid,person1,name1,person2,name2,person3,name3,person4,name4, paycode1,payname1,amount1,paycode2,payname2,amount2,paycode3,payname3,amount3,paycode4,payname4,amount4,paycode5,payname5,amount5,paycode6,payname6,amount6, paycode7,payname7,amount7,paycode8,payname8,amount8,paycode9,payname9,amount9 from #result group by compid,compname,billid,billtype,date,accountdate,cardid,mobile,memberid,membername,totalamount,userid,person1,name1,person2,name2,person3,name3,person4,name4, paycode1,payname1,amount1,paycode2,payname2,amount2,paycode3,payname3,amount3,paycode4,payname4,amount4,paycode5,payname5,amount5,paycode6,payname6,amount6, paycode7,payname7,amount7,paycode8,payname8,amount8,paycode9,payname9,amount9 order by date desc ' end end else begin if(@condition <> '%%') begin set @sql = 'insert into #tb_result(compid,compname,billid,billtype,date,accountdate,cardid,mobile,memberid,membername,totalamount,userid,person1,name1,person2,name2,person3,name3,person4,name4, paycode1,payname1,amount1,paycode2,payname2,amount2,paycode3,payname3,amount3,paycode4,payname4,amount4,paycode5,payname5,amount5,paycode6,payname6,amount6, paycode7,payname7,amount7,paycode8,payname8,amount8,paycode9,payname9,amount9) select compid,compname,billid,billtype,date,accountdate,cardid,mobile,memberid,membername,totalamount,userid,person1,name1,person2,name2,person3,name3,person4,name4, paycode1,payname1,amount1,paycode2,payname2,amount2,paycode3,payname3,amount3,paycode4,payname4,amount4,paycode5,payname5,amount5,paycode6,payname6,amount6, paycode7,payname7,amount7,paycode8,payname8,amount8,paycode9,payname9,amount9 from #result where paycode= ''' + @paycode + '''' + ' and (mobile like ''' + @condition + '''' + ' or payamount like ''' + @condition + '''' + ' or cardid like ''' + @condition + '''' + ' or memberid like ''' + @condition + '''' + ' or membername like ''' + @condition + '''' + ' or billid like ''' + @condition + '''' + ' or prjcode like ''' + @condition + '''' + ' or prjname like ''' + @condition + + '''' + ') group by compid,compname,billid,billtype,date,accountdate,cardid,mobile,memberid,membername,totalamount,userid,person1,name1,person2,name2,person3,name3,person4,name4, paycode1,payname1,amount1,paycode2,payname2,amount2,paycode3,payname3,amount3,paycode4,payname4,amount4,paycode5,payname5,amount5,paycode6,payname6,amount6, paycode7,payname7,amount7,paycode8,payname8,amount8,paycode9,payname9,amount9 order by date desc ' end else begin set @sql = 'insert into #tb_result(compid,compname,billid,billtype,date,accountdate,cardid,mobile,memberid,membername,totalamount,userid,person1,name1,person2,name2,person3,name3,person4,name4, paycode1,payname1,amount1,paycode2,payname2,amount2,paycode3,payname3,amount3,paycode4,payname4,amount4,paycode5,payname5,amount5,paycode6,payname6,amount6, paycode7,payname7,amount7,paycode8,payname8,amount8,paycode9,payname9,amount9) select compid,compname,billid,billtype,date,accountdate,cardid,mobile,memberid,membername,totalamount,userid,person1,name1,person2,name2,person3,name3,person4,name4, paycode1,payname1,amount1,paycode2,payname2,amount2,paycode3,payname3,amount3,paycode4,payname4,amount4,paycode5,payname5,amount5,paycode6,payname6,amount6, paycode7,payname7,amount7,paycode8,payname8,amount8,paycode9,payname9,amount9 from #result where paycode= ''' + @paycode + '''' + ' group by compid,compname,billid,billtype,date,accountdate,cardid,mobile,memberid,membername,totalamount,userid,person1,name1,person2,name2,person3,name3,person4,name4, paycode1,payname1,amount1,paycode2,payname2,amount2,paycode3,payname3,amount3,paycode4,payname4,amount4,paycode5,payname5,amount5,paycode6,payname6,amount6, paycode7,payname7,amount7,paycode8,payname8,amount8,paycode9,payname9,amount9 order by date desc ' end end exec sp_executesql @sql select compid,compname,billid,billtype,date,accountdate,cardid,mobile,memberid,membername,totalamount,userid,person1,name1,person2,name2,person3,name3,person4,name4, paycode1,payname1,amount1,paycode2,payname2,amount2,paycode3,payname3,amount3,paycode4,payname4,amount4,paycode5,payname5,amount5,paycode6,payname6,amount6, paycode7,payname7,amount7,paycode8,payname8,amount8,paycode9,payname9,amount9 from #tb_result where idd >= @pagesize*(@pageindex-1)+1 and idd <= @pagesize*@pageindex drop table #tb_result drop table #result drop table #billItem drop table #gsm03 drop table #gcm10 drop table #ggm01_ggm03 drop table #ggm01_ggm02 drop table #gnm01_gnm02 end GO /****** Object: Table [dbo].[gom02] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gom02]( [gob00c] [varchar](10) NOT NULL, [gob01c] [varchar](20) NOT NULL, [gob02f] [float] NULL, [gob03c] [varchar](5) NOT NULL, [gob04c] [varchar](5) NOT NULL, [gob05f] [float] NULL, [gob06c] [varchar](40) NULL, CONSTRAINT [PK_goM02] PRIMARY KEY CLUSTERED ( [gob00c] ASC, [gob01c] ASC, [gob03c] ASC, [gob04c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_search_stock] Script Date: 06/12/2017 06:20:39 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_search_stock] @compid varchar(10), @date varchar(8), @quan float --安全余额 as begin create table #result ( goodsno varchar(20) null, --产品编号 goodsname varchar(80) null, --产品名称 garageno varchar(20) null, -- 仓库编号 garagename varchar(80) null, --仓库名称 goodstype varchar(80) null, --产品类型 price float null, --单价 unit varchar(10) null, --单位 quantity float null, --数量 amt float null, stattype varchar(20) null ) insert #result(goodsno, goodsname, garageno, garagename, goodstype, price, unit, quantity, amt, stattype) exec upg_stock_statistic @compid, @date, '*', '', '*', '', 1, @quan, '*', '*', '', 0 select gau00c = @compid, gau03c = goodsno, gau17c = goodsname, gau18c = unit, gau06f = quantity from #result where quantity <= @quan delete #result drop table #result end GO /****** Object: Table [dbo].[ham17] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[ham17]( [har00c] [varchar](10) NOT NULL, [har01c] [varchar](20) NOT NULL, [har02c] [varchar](8) NOT NULL, [har03f] [float] NULL, [har04c] [varchar](60) NULL, [har05i] [int] NULL, CONSTRAINT [PK_HAM17] PRIMARY KEY CLUSTERED ( [har00c] ASC, [har01c] ASC, [har02c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gam11] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gam11]( [gal00c] [varchar](10) NOT NULL, [gal01c] [varchar](5) NOT NULL, [gal02f] [float] NOT NULL, [gal03c] [varchar](5) NOT NULL, [gal04c] [varchar](5) NULL, CONSTRAINT [PK_GAM11] PRIMARY KEY CLUSTERED ( [gal00c] ASC, [gal01c] ASC, [gal02f] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[glm08] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[glm08]( [gli00c] [varchar](10) NOT NULL, [gli01i] [int] IDENTITY(1,1) NOT NULL, [gli02d] [varchar](8) NULL, [gli03t] [varchar](6) NULL, [gli04i] [int] NULL, [gli05c] [varchar](20) NULL, [gli06i] [int] NULL, [gli07c] [varchar](20) NULL, [gli08i] [int] NULL, [gli09c] [varchar](20) NULL, [gli10c] [varchar](20) NULL, CONSTRAINT [PK_GLM08] PRIMARY KEY CLUSTERED ( [gli00c] ASC, [gli01i] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[ham06] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[ham06]( [haf00c] [varchar](10) NOT NULL, [haf01c] [varchar](10) NOT NULL, [haf02c] [varchar](20) NOT NULL, [haf03f] [float] NULL, [haf04c] [varchar](5) NULL, CONSTRAINT [PK_HAM06] PRIMARY KEY CLUSTERED ( [haf00c] ASC, [haf01c] ASC, [haf02c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[wgl] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[wgl]( [wgl00] [varchar](10) NOT NULL, [wgl01v] [varchar](10) NOT NULL, [wgl02v] [varchar](20) NOT NULL, CONSTRAINT [PK_WGL] PRIMARY KEY CLUSTERED ( [wgl00] ASC, [wgl01v] ASC, [wgl02v] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_cal_mr_perf] Script Date: 06/12/2017 06:19:32 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_cal_mr_perf] ( @compid varchar(10), @fromdate varchar(10), @todate varchar(10) ) as begin create table #shop_salon_perf ( seq int identity not null, compid varchar(10) not null, fromdate varchar(10) not null, todate varchar(10) not null, --售前 p_cust_num float null, --售前人数, 只要是来过的都算不管是否已经购买了卡, 统计入院单 p_succ_num float null, --成交人数 p_succ_ratio float null, --成交率 p_this_goods_num float null, --当次办产品零售人数 p_this_mr_num float null, --当次办美容项目卡人数 --ok p_th_one_num float null, --特护单次人数 p_this_thc_num float null, --当次办特护卡人数 p_sec_card_num float null, --二次回卡人数 p_a_good_amt float null, --售后人员完成产品业绩 p_a_mr_card_amt float null, --售后人员完成美容项目卡业绩 p_a_mt_amt float null, --售后人员完成美体业绩 p_a_yq_amt float null, --售后人员完成仪器业绩 p_a_sum float null, --小计 p_mt_good_amt float null, --产品零售业绩 --ok p_mt_mr_card_amt float null, --办美容项目卡业绩 --ok --美体业绩 p_mt_one_amt float null, --单次美体业绩 --ok p_mt_one_yq_amt float null, --单次仪器业绩 --ok p_mt_card_amt float null, --美体办卡业绩 --ok p_mt_yq_card float null, --仪器办卡业绩 --ok p_mt_sum float null, --小计 --ok p_mt_sum = p_mt_one_amt + p_mt_one_yq_amt + p_mt_card_amt + p_mt_yq_card p_avg float null, --平均值 p_sum float null, --业绩合计 --ok p_sum = p_a_sum + p_mt_good_amt + p_mt_mr_card_amt + p_mt_sum --售后 a_cust_gold_d_num float null, -- 金卡及以下 a_cust_wgold_u_num float null, --白金及以上 a_cust_sum_num float null, --小计 a_succ_gold_d_num float null, -- 成交人数 金卡及以下 a_succ_vip_num float null, -- 贵宾及以上 a_succ_sun_num float null, -- 小计 a_succ_ratio float null, -- 成交率 --美容业绩 a_mr_good_amt float null, --产品业绩 --ok a_mr_card_amt float null, --美容项目卡业绩 --ok a_mr_sum float null, --小计 --ok a_mr_sum = a_mr_good_amt + a_mr_card_amt a_mr_avg float null, --平均值 --美体业绩 a_mt_srv_num float null, --服务人次 a_mt_act_num float null, --实际人数 a_mt_succ_num float null, --成交人数 a_mt_ratio float null, --成交率 a_mt_good_amt float null, --产品业绩 --ok, 总是为0,分不开 a_mt_amt float null, --美体业绩 --ok a_mt_yq_amt float null, --仪器业绩 --ok a_mt_sum float null, --小计 -- ok a_mt_sum = a_mt_good_amt + a_mt_amt + a_mt_yq_amt a_mt_avg float null, --平均值 a_amt_sum float null, --售后业绩合计 a_amt_sum = a_mr_sum + a_mt_sum --嘉宾 j_srv_num float null, -- 业绩 人数 j_succ_num float null, --成交人数 j_succ_ratio float null, --成交率 j_good_amt float null, --产品业绩 --ok j_mr_card_amt float null, --美容项目卡业绩 --ok j_mt_amt float null, --美体业绩 --ok j_yq_amt float null, --仪器业绩 --ok j_sum float null, --小计 --ok j_sum = j_good_amt + j_mr_card_amt + j_mt_amt + j_yq_amt j_avg float null, --平均值 --合计 t_good_amt float null, --当日产品业绩 -- ok t_good_amt = p_a_good_amt + p_mt_good_amt + a_mr_good_amt + a_mt_good_amt + j_good_amt t_mr_card_amt float null, --当日美容项目卡业绩 --ok t_mr_card_amt = p_a_mr_card_amt + p_mt_mr_card_amt + a_mr_card_amt + j_mr_card_amt t_mt_amt float null, --当日美体业绩 --ok t_mt_amt = p_a_mt_amt + p_mt_one_amt + p_mt_card_amt + a_mt_amt + j_mt_amt t_yq_amt float null, --当日仪器业绩 --ok t_yq_amt = p_a_yq_amt + p_mt_one_yq_amt + p_mt_yq_card + a_mt_yq_amt + j_yq_amt t_sum float null, --当日总业绩合计 --ok t_sum = t_good_amt + t_mr_card_amt + t_mt_amt + t_yq_amt t_month_total float null, --累计 t_other_revenue float null, --其它收入 t_st_mon_yj_amt float null, --当月押金期初金额 t_yj_amt float null, --当日保证金收入 t_yj_used float null, --当日保证金转销售 t_yj_back float null, --当日保证金退出 t_yj_net float null, --当日保证金净金额 t_yj_mon_end_amt float null, --当月押金末存金额 t_bank_saving float null, --当日银行存入金额 t_pos float null, --当日刷卡金额 t_remain float null, --当日存留金额 t_check_amt float null, --当日支票金额 t_act_saving float null, --当日业绩实际总存款 t_revenue float null, --当日总收入 t_check float null, --检查 ) declare @PK_Sql nvarchar(max) set @PK_Sql = ' alter table #shop_salon_perf add constraint PK_#shop_salon_perf_' + convert(varchar(10), cast(floor(rand() * 1000000) as int)) + ' PRIMARY KEY(seq)' exec sp_executesql @PK_Sql insert #shop_salon_perf(compid, fromdate, todate) select gaz02c, @fromdate, @todate from gam26 where gaz01c = @compid --每个院的用于统计的一个缓冲报表 create table #tbl_shop_stat( compid varchar(20) not null, value float null, num float null, type int null) insert #tbl_shop_stat(compid, value) select gla00c, count(*) from glm01 a with (nolock), gam26 z where a.gla00c = z.gaz02c and z.gaz01c = @compid and a.gla09i = 0 and a.gla80d >= @fromdate and a.gla80d <= @todate group by gla00c update a set a.p_cust_num = b.value from #shop_salon_perf a, #tbl_shop_stat b where a.compid = b.compid delete #tbl_shop_stat --美容卡项目人数,业绩 insert #tbl_shop_stat(type, compid, num, value) select gcw99i, gcw00c, count(*), sum(gcw13f) from gcm23 a, gam26 z where gcw03d >= @fromdate and gcw03d <= @todate and gcw00c = z.gaz02c and z.gaz01c = @compid group by gcw99i, gcw00c --售前 update a set a.p_this_mr_num = b.num, a.p_mt_mr_card_amt = b.value from #shop_salon_perf a, #tbl_shop_stat b where a.compid = b.compid and b.type = 1 --售后 update a set a.a_mr_card_amt = b.value from #shop_salon_perf a, #tbl_shop_stat b where a.compid = b.compid and b.type = 2 --嘉宾 update a set a.j_mr_card_amt = b.value from #shop_salon_perf a, #tbl_shop_stat b where a.compid = b.compid and b.type = 3 --美体项目人数,业绩 delete #tbl_shop_stat insert #tbl_shop_stat(type, compid, num, value) select gcw99i, gcw00c, count(*), sum(gcw15f) from gcm23 a, gam26 z where gcw03d >= @fromdate and gcw03d <= @todate and gcw00c = z.gaz02c and z.gaz01c = @compid group by gcw99i, gcw00c --售前 update a set a.p_mt_card_amt = b.value from #shop_salon_perf a, #tbl_shop_stat b where a.compid = b.compid and b.type = 1 --售后 update a set a.a_mt_amt = b.value from #shop_salon_perf a, #tbl_shop_stat b where a.compid = b.compid and b.type = 2 --嘉宾 update a set a.j_mt_amt = b.value from #shop_salon_perf a, #tbl_shop_stat b where a.compid = b.compid and b.type = 3 --仪器项目人数,业绩 delete #tbl_shop_stat insert #tbl_shop_stat(type, compid, num, value) select gcw99i, gcw00c, count(*), sum(gcw17f) from gcm23 a, gam26 z where gcw03d >= @fromdate and gcw03d <= @todate and gcw00c = z.gaz02c and z.gaz01c = @compid group by gcw99i, gcw00c --售前 update a set a.p_mt_yq_card = b.value from #shop_salon_perf a, #tbl_shop_stat b where a.compid = b.compid and b.type = 1 --售后 update a set a.a_mt_yq_amt = b.value from #shop_salon_perf a, #tbl_shop_stat b where a.compid = b.compid and b.type = 2 --嘉宾 update a set a.j_yq_amt = b.value from #shop_salon_perf a, #tbl_shop_stat b where a.compid = b.compid and b.type = 3 --统计产品 delete #tbl_shop_stat insert #tbl_shop_stat(type, compid, value) select gcx99i, gcx00c, sum(gcx19f) from gcm24 a, gam26 z where a.gcx00c = z.gaz02c and z.gaz01c = @compid and a.gcx03d >= @fromdate and a.gcx03d <= @todate group by gcx99i, gcx00c --售前 update a set a.p_mt_good_amt = b.value from #shop_salon_perf a, #tbl_shop_stat b where a.compid = b.compid and b.type = 1 --售后 update a set a.a_mr_good_amt = b.value from #shop_salon_perf a, #tbl_shop_stat b where a.compid = b.compid and b.type = 2 --嘉宾 update a set a.j_good_amt = b.value from #shop_salon_perf a, #tbl_shop_stat b where a.compid = b.compid and b.type = 3 --计算单次业绩 delete #tbl_shop_stat --美体单次业绩 insert #tbl_shop_stat(type, compid, value) select gcx99i, gcx00c, sum(gcx15f) from gcm24 a, gam26 z where a.gcx00c = z.gaz02c and z.gaz01c = @compid and a.gcx03d >= @fromdate and a.gcx03d <= @todate group by gcx99i, gcx00c --售前 update a set a.p_mt_one_amt = b.value from #shop_salon_perf a, #tbl_shop_stat b where a.compid = b.compid and b.type = 1 -- delete #tbl_shop_stat --仪器单次业绩 insert #tbl_shop_stat(type, compid, value) select gcx99i, gcx00c, sum(gcx17f) from gcm24 a, gam26 z where a.gcx00c = z.gaz02c and z.gaz01c = @compid and a.gcx03d >= @fromdate and a.gcx03d <= @todate group by gcx99i, gcx00c --售前 update a set a.p_mt_one_yq_amt = b.value from #shop_salon_perf a, #tbl_shop_stat b where a.compid = b.compid and b.type = 1 update a set a.p_mt_sum = isnull(p_mt_one_amt, 0) + isnull(p_mt_one_yq_amt, 0) + isnull(p_mt_card_amt, 0) + isnull(p_mt_yq_card, 0), a.a_mr_sum = isnull(a_mr_good_amt, 0) + isnull(a_mr_card_amt, 0) from #shop_salon_perf a update a set a.a_amt_sum = isnull(a_mr_sum, 0) + isnull(a_mt_sum, 0) from #shop_salon_perf a update a set a.p_sum = isnull(p_a_sum, 0) + isnull(p_mt_good_amt, 0) + isnull(p_mt_mr_card_amt, 0) + isnull(p_mt_sum, 0), a_mt_sum = isnull(a_mt_good_amt, 0) + isnull(a_mt_amt, 0) + isnull(a_mt_yq_amt, 0), j_sum = isnull(j_good_amt, 0) + isnull(j_mr_card_amt, 0) + isnull(j_mt_amt, 0) + isnull(j_yq_amt, 0) from #shop_salon_perf a update a set a.t_good_amt = isnull(p_a_good_amt, 0) + isnull(p_mt_good_amt, 0) + isnull(a_mr_good_amt, 0) + isnull(a_mt_good_amt, 0) + isnull(j_good_amt, 0), a.t_mr_card_amt = isnull(p_a_mr_card_amt, 0) + isnull(p_mt_mr_card_amt, 0) + isnull(a_mr_card_amt, 0) + isnull(j_mr_card_amt, 0), a.t_mt_amt = isnull(p_a_mt_amt, 0) + isnull(p_mt_one_amt, 0) + isnull(p_mt_card_amt, 0) + isnull(a_mt_amt, 0) + isnull(j_mt_amt, 0), a.t_yq_amt = isnull(p_a_yq_amt, 0) + isnull(p_mt_one_yq_amt, 0) + isnull(p_mt_yq_card, 0) + isnull(a_mt_yq_amt, 0) + isnull(j_yq_amt, 0) from #shop_salon_perf a update a set a.t_sum = t_good_amt + t_mr_card_amt + t_mt_amt + t_yq_amt from #shop_salon_perf a drop table #tbl_shop_stat select * from #shop_salon_perf drop table #shop_salon_perf end GO /****** Object: StoredProcedure [dbo].[P_tablePagination_page] Script Date: 06/12/2017 06:19:20 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create proc [dbo].[P_tablePagination_page] ( @page nvarchar(20), -- 当前页 @pagesize nvarchar(20), -- 每页条数 @columns nvarchar(2000), -- 列 @rank nvarchar(50), -- 指定排序的列 @tableName nvarchar(50), -- 表名 @sqlwhere nvarchar(2000), -- 查询语句 @desc nvarchar(20) -- 排序方式 ) as begin declare @count nvarchar(20) declare @joinwhere nvarchar(2000) declare @sql nvarchar(max) set @sql = 'select top ' + @pagesize + ' ' + @columns + ' from (select top ' + convert(nvarchar(50), convert(int, @pagesize) * convert(int, @page)) + ' ' declare @au_id char(20) set rowcount 0 set @joinwhere = '' select * into #mytemp from INFORMATIon_SCHEMA.KEY_COLUMN_USAGE where table_NAME = '' + @tableName + '' set rowcount 1 select @au_id = COLUMN_NAME from #mytemp set @count = 0 while @@rowcount <> 0 begin set rowcount 0 set @count = @count + 1 set @sql = @sql + ' ' + @au_id + ' as KeyColumn' + @count + ', ' set @joinwhere = @joinwhere + ' b.' + @au_id + ' = a.KeyColumn' + @count + ' and' delete #mytemp where COLUMN_NAME = @au_id set rowcount 1 select @au_id = COLUMN_NAME from #mytemp end set rowcount 0 drop table #mytemp if (@count > 0) begin set @joinwhere = substring(@joinwhere, 0, len(@joinwhere) - 3) end set @sql = @sql + ' Row_Number() over(order by ' + @rank + ' ' + @desc + ') as RowNumber from ' + @tableName + ' with(nolock) where 1 = 1 ' + @sqlwhere + ' ) a, ' + @tableName + ' b where 1 = 1 ' + @sqlwhere if (@count > 0) begin set @sql = @sql + ' and ' + @joinwhere end set @sql = @sql + ' and RowNumber >= ' + convert(nvarchar(50), (convert(int, @page) - 1) * Convert(int, @pagesize) + 1) + ' order by a.RowNumber asc' exec sp_executesql @sql end GO /****** Object: Table [dbo].[gcm15] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gcm15]( [gcq00c] [varchar](10) NOT NULL, [gcq01d] [varchar](8) NOT NULL, [gcq02c] [varchar](20) NULL, [gcq03i] [int] NULL, [gcq04c] [varchar](100) NULL, [gcq05c] [varchar](100) NULL, [gcq06c] [varchar](20) NULL, [gcq07i] [int] NULL, [gcq08c] [varchar](100) NULL, [gcq09c] [varchar](100) NULL, [gcq10c] [varchar](20) NULL, [gcq11i] [int] NULL, [gcq12c] [varchar](100) NULL, [gcq13c] [varchar](100) NULL, [gcq14c] [varchar](20) NULL, [gcq15i] [int] NULL, [gcq16c] [varchar](100) NULL, [gcq17c] [varchar](100) NULL, [gcq18c] [varchar](20) NULL, [gcq19i] [int] NULL, [gcq20c] [varchar](100) NULL, [gcq21c] [varchar](100) NULL, [gcq22c] [varchar](20) NULL, [gcq23i] [int] NULL, [gcq24c] [varchar](100) NULL, [gcq25c] [varchar](100) NULL, CONSTRAINT [PK_GCM15] PRIMARY KEY CLUSTERED ( [gcq00c] ASC, [gcq01d] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gtc01] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gtc01]( [gta00c] [varchar](10) NOT NULL, [gta01c] [varchar](20) NOT NULL, [gta02c] [varchar](30) NULL, [gta03f] [float] NULL, [gta04c] [varchar](2000) NULL, [gta05i] [tinyint] NULL, [gta06f] [float] NULL, [gta07i] [tinyint] NULL, [gta08f] [float] NULL, [gta09i] [int] NULL, [gta10i] [int] NULL, [gta10d] [datetime] NULL, [gta11i] [int] NULL, [gta12c] [varchar](2) NULL, [gta13c] [varchar](10) NULL, [gta14c] [varchar](10) NULL, CONSTRAINT [PK_GTC01] PRIMARY KEY CLUSTERED ( [gta00c] ASC, [gta01c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gam05p] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gam05p]( [gae00cp] [varchar](10) NOT NULL, [gae01cp] [varchar](1) NOT NULL, [gae02cp] [varchar](100) NULL, [gae03cp] [varchar](500) NULL, [gae04p] [image] NULL, [gae05cp] [numeric](19, 0) IDENTITY(1,1) NOT NULL, [gae06cp] [varchar](2) NULL, CONSTRAINT [PK_GAM05P] PRIMARY KEY CLUSTERED ( [gae05cp] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_stat_acct_history_ex] Script Date: 06/12/2017 06:20:42 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_stat_acct_history_ex] ( @compid varchar(10), @fromcard varchar(max), @tocard varchar(max), @fromdate varchar(20), @todate varchar(20), @flag int -- 1-一个账户一个余额 2-详细的每条纪录 ) as begin declare @incompid varchar(10) select @incompid = @compid declare @compcode varchar(20) declare @PK_Sql nvarchar(max) select @fromcard = replace(@fromcard,',',''',''') if @compid = '001' or @compid = '*' or @compid = '%' begin select @compcode = '%' select @incompid = '001' end else begin select @compcode = @compid end if @fromdate = '*' or @fromdate = '' begin select @fromdate = '19900101' select @todate = '20901231' end create table #tbl_history ( seq int identity not null, cardid varchar(20) not null, nname varchar(40) null, acctno varchar(20) null, ddate varchar(10) null, mode int null, lastamt float null, amt float null, billtype varchar(20) null, billid varchar(20) null, remain float null, total_fill float null, total_consume float null, primary key(seq) ) create table #tbl_stat ( cardid varchar(20) not null, acctno varchar(20) not null, amt float null ) if @fromcard = '*' begin insert #tbl_history(cardid, nname, acctno, ddate, mode, lastamt, amt, billtype, billid) select gcd01c, '', gcd02i, gcd09d, gcd05i, gcd10f, gcd06f, gcd07c, gcd08c from gcm04 a with (nolock), gcm01 b with (nolock), gam26 z with(nolock) where a.gcd00c = z.gaz02c and z.gaz01c = @incompid and a.gcd00c = b.gca00c and a.gcd01c = b.gca01c and a.gcd09d >= @fromdate and a.gcd09d <= @todate order by gcd01c, gcd02i, gcd04f end else begin set @PK_Sql = 'insert #tbl_history(cardid, nname, acctno, ddate, mode, lastamt, amt, billtype, billid) select gcd01c, '''', gcd02i, gcd09d, gcd05i, gcd10f, gcd06f, gcd07c, gcd08c from gcm04 a with (nolock), gcm01 b with (nolock), gam26 z with(nolock) where a.gcd00c = z.gaz02c and z.gaz01c = '''+@incompid +''' and a.gcd00c = b.gca00c and a.gcd01c = b.gca01c and a.gcd09d >= '''+@fromdate +''' and a.gcd09d <= '''+@todate +''' and a.gcd01c in ('''+@fromcard +''') and a.gcd01c <> '''' order by gcd01c, gcd02i, gcd04f' exec sp_executesql @PK_Sql end delete #tbl_history where isnull(amt, 0) = 0 if @fromcard = '*' or @fromcard = '' begin insert #tbl_history(cardid, nname, acctno, ddate, mode, lastamt, remain, billtype, billid) select gcc01c, '', gcc03i, '', 0, 0, gcc06f, '', '' from gcm03 a with (nolock), gcm01 b with (nolock) where gcc00c = gca13d and gca00c = gca13d and gcc01c = gca01c and gcc01c <> '' end else begin set @PK_Sql = 'insert #tbl_history(cardid, nname, acctno, ddate, mode, lastamt, remain, billtype, billid) select gcc01c, '''', gcc03i, '''', 0, 0, gcc06f, '''', '''' from gcm03 a with (nolock), gcm01 b with (nolock) where gcc00c = gca13d and gca00c = gca13d and gcc01c = gca01c and gca01c in ('''+ @fromcard +''') and gca01c <> ''''' exec sp_executesql @PK_Sql end -- 统计累计充值以及累计消费 insert #tbl_stat(cardid, acctno, amt) select cardid, acctno, sum(isnull(amt, 0)) from #tbl_history where (mode = 0 or mode = 6) and isnull(ddate, '') <> '' group by cardid, acctno update a set a.total_fill = isnull(b.amt, 0) from #tbl_history a, #tbl_stat b where a.cardid = b.cardid and a.acctno = b.acctno and isnull(ddate, '') = '' delete #tbl_stat insert #tbl_stat(cardid, acctno, amt) select cardid, acctno, sum(isnull(amt, 0)) from #tbl_history where (mode = 5 or mode = 1) and isnull(ddate, '') <> '' group by cardid, acctno update a set a.total_fill = isnull(a.total_fill, 0) - isnull(b.amt, 0) from #tbl_history a, #tbl_stat b where a.cardid = b.cardid and a.acctno = b.acctno and isnull(ddate, '') = '' delete #tbl_stat insert #tbl_stat(cardid, acctno, amt) select cardid, acctno, sum(isnull(amt, 0)) from #tbl_history where mode = 2 and isnull(ddate, '') <> '' group by cardid, acctno update a set a.total_consume = isnull(b.amt, 0) from #tbl_history a, #tbl_stat b where a.cardid = b.cardid and a.acctno = b.acctno and isnull(ddate, '') = '' drop table #tbl_stat update a set a.nname = b.gba03c from #tbl_history a, gbm01 b with(nolock) where a.cardid = b.gba23c and a.ddate = '' update #tbl_history set billtype = '收银单' where billtype = 'gx' update #tbl_history set billtype = '账户异动' where billtype = 'gz' update #tbl_history set billtype = '卡销售' where billtype = 'gn' update #tbl_history set billtype = '卡异动' where billtype = 'ge' if @flag = 2 -- 每个账户的详细变化历史,也包括一条汇总的纪录 select seq,cardid, isnull(nname,'') nname, isnull(acctno,'') acctno, isnull(ddate,'') ddate, isnull(mode,'') mode, isnull(lastamt,0) lastamt, isnull(amt,0) amt, isnull(billtype,'') billtype, isnull(billid,'') billid, isnull(remain,0) remain, isnull(total_fill,0) total_fill, isnull(total_consume,0) total_consume from #tbl_history order by cardid, acctno, ddate else -- 每个账户一个余额 select seq,cardid, isnull(nname,'') nname, isnull(acctno,'') acctno, isnull(ddate,'') ddate, isnull(mode,'') mode, isnull(lastamt,0) lastamt, isnull(amt,0) amt, isnull(billtype,'') billtype, isnull(billid,'') billid, isnull(remain,0) remain, isnull(total_fill,0) total_fill, isnull(total_consume,0) total_consume from #tbl_history where isnull(ddate, '') = '' order by cardid, acctno, ddate drop table #tbl_history end GO /****** Object: Table [dbo].[gnm03] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gnm03]( [gnc00c] [varchar](10) NOT NULL, [gnc01c] [varchar](20) NOT NULL, [gnc02c] [varchar](20) NOT NULL, [gnc03f] [float] NULL, [gnc04f] [float] NULL, [gnc05f] [float] NULL, [gnc06d] [varchar](8) NULL, [gnc07i] [int] NULL, [gnc08f] [float] NULL, [gnc09i] [int] NOT NULL, [gnc10f] [float] NOT NULL, [gnc11f] [float] NULL, [gnc12i] [int] NULL, [gnc13c] [varchar](1000) NULL, [gnc15c] [varchar](5) NULL, [gnc16f] [float] NULL, [gnc17c] [varchar](5) NULL, [gnc18f] [float] NULL, [gnc19c] [varchar](5) NULL, [gnc20f] [float] NULL, [gnc21f] [float] NULL, [gnc22i] [int] NULL, [gnc23c] [varchar](5) NULL, [gnc24f] [float] NULL, [gnc25i] [int] NULL, [gnc26i] [int] NULL, [gnc27f] [float] NULL, [gnc28c] [nvarchar](2000) NULL, [gnc29i] [int] IDENTITY(1,1) NOT NULL, [gnc30c] [int] NULL, [gnc31c] [varchar](20) NULL, [gnc32d] [varchar](20) NULL, [gnc33f] [float] NULL, [gnc34c] [varchar](20) NULL, [gnc35f] [float] NULL, CONSTRAINT [PK_GNM03] UNIQUE CLUSTERED ( [gnc00c] ASC, [gnc01c] ASC, [gnc02c] ASC, [gnc29i] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_date_plus] Script Date: 06/12/2017 06:19:56 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_date_plus] @fromdate varchar(8), @addday float, @result varchar(8) output as begin declare @tmpdate datetime declare @year varchar(4) --年份 declare @month varchar(2)--月份 declare @day varchar(2) --日份 set @tmpdate = dateadd(day, @addday, convert(datetime, @fromdate)) set @year = year(@tmpdate) set @month = month(@tmpdate) set @day = day(@tmpdate) if (len(@month) = 1) set @month = '0' + @month if (len(@day) = 1) set @day = '0' + @day set @result = @year + @month + @day end GO /****** Object: Table [dbo].[recover_ggm01_master] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[recover_ggm01_master]( [id] [int] IDENTITY(1,1) NOT NULL, [comp] [varchar](10) NOT NULL, [old_bill] [varchar](20) NOT NULL, [new_bill] [varchar](20) NULL, [del_op] [varchar](10) NOT NULL, [del_time] [datetime] NOT NULL, [new_op] [varchar](10) NULL, [new_time] [datetime] NULL, [new] [int] NULL ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[ham14] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[ham14]( [han00c] [varchar](10) NOT NULL, [han01c] [varchar](10) NOT NULL, [han02f] [float] NOT NULL, [han03f] [float] NULL, [han04f] [float] NULL, [han05i] [int] NULL, [han06f] [float] NULL, [han07c] [varchar](40) NULL, CONSTRAINT [PK_HAM14] PRIMARY KEY CLUSTERED ( [han00c] ASC, [han01c] ASC, [han02f] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gam09] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gam09]( [gaj00c] [varchar](10) NOT NULL, [gaj01c] [varchar](5) NOT NULL, [gaj02c] [varchar](20) NOT NULL, [gaj03i] [int] NULL, [gaj04c] [varchar](20) NULL, CONSTRAINT [PK_GAM09] PRIMARY KEY CLUSTERED ( [gaj00c] ASC, [gaj01c] ASC, [gaj02c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gtc08] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gtc08]( [gtj00c] [varchar](10) NOT NULL, [gtj01c] [varchar](20) NOT NULL, [gtj02c] [varchar](20) NOT NULL, [gtj03c] [varchar](20) NOT NULL, [gtj04f] [float] NULL, [gtj05f] [float] NULL, [gtj06f] [float] NULL, [gtj07c] [varchar](20) NULL, [gtj09i] [int] IDENTITY(1,1) NOT NULL, [gtj10c] [varchar](20) NULL, [gtj11c] [varchar](50) NULL, [gtj12i] [int] NULL, CONSTRAINT [PK_GTC08] PRIMARY KEY CLUSTERED ( [gtj00c] ASC, [gtj01c] ASC, [gtj09i] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_batch_give_right] Script Date: 06/12/2017 06:19:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_batch_give_right]( @fromuserid varchar(20), @touserid varchar(20), @right varchar(100), @value varchar(20)) as begin create table #tbl_tmp_userid( userid varchar(20)) insert #tbl_tmp_userid(userid) select gaa01c from gam01 where (gaa01c >= @fromuserid and gaa01c <= @touserid) or ('*' = @fromuserid) --删除重复的 delete a from gam02 a, #tbl_tmp_userid b where a.gab02c = '4' and a.gab03c = @right and a.gab01c = b.userid insert gam02(gab01c, gab02c, gab03c, gab04c) select userid, '4', @right, @value from #tbl_tmp_userid drop table #tbl_tmp_userid end GO /****** Object: Table [dbo].[gdm03p] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gdm03p]( [gdc00c] [varchar](10) NOT NULL, [gdc01c] [varchar](20) NOT NULL, [gdc02p] [image] NULL, [gdc03c] [varchar](2) NULL, [gdc04c] [numeric](19, 0) IDENTITY(1,1) NOT NULL, CONSTRAINT [PK_GDM03P] PRIMARY KEY CLUSTERED ( [gdc04c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[ham15] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[ham15]( [hap00c] [varchar](10) NOT NULL, [hap01c] [varchar](10) NOT NULL, [hap02c] [varchar](10) NOT NULL, [hap03i] [tinyint] NULL, [hap04f] [float] NULL, [hap05i] [tinyint] NULL, [hap06f] [float] NULL, [hap07i] [tinyint] NULL, [hap08f] [float] NULL, [hap09i] [tinyint] NULL, [hap10f] [float] NULL, [hap11i] [tinyint] NULL, [hap12f] [float] NULL, [hap13i] [tinyint] NULL, [hap14f] [float] NULL, [hap15i] [tinyint] NULL, [hap16f] [float] NULL, [hap17i] [tinyint] NULL, [hap18f] [float] NULL, [hap19i] [tinyint] NULL, [hap20f] [float] NULL, [hap21i] [tinyint] NULL, [hap22f] [float] NULL, [hap23i] [tinyint] NULL, [hap24f] [float] NULL, [hap25i] [tinyint] NULL, [hap26f] [float] NULL, [hap37i] [tinyint] NULL, [hap38f] [float] NULL, [hap39i] [tinyint] NULL, [hap40f] [float] NULL, [hap41i] [tinyint] NULL, [hap42f] [float] NULL, [hap43i] [tinyint] NULL, [hap44f] [float] NULL, [hap45i] [tinyint] NULL, [hap46f] [float] NULL, [hap47i] [tinyint] NULL, [hap48f] [float] NULL, [hap49i] [tinyint] NULL, [hap50f] [float] NULL, [hap51i] [tinyint] NULL, [hap52f] [float] NULL, [hap53c] [varchar](10) NULL, CONSTRAINT [PK_HAM15] PRIMARY KEY CLUSTERED ( [hap00c] ASC, [hap01c] ASC, [hap02c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_payway_inventory] Script Date: 06/12/2017 06:20:26 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE PROCEDURE [dbo].[upg_payway_inventory] ( @compid varchar(10), @fromdate varchar(10), @todate varchar(10) ) AS BEGIN CREATE TABLE #ptable ( payway varchar(10) not null, pname varchar(40) null, amt float null, amt_srv float null, amt_good float null, amt_card float null ) INSERT #ptable(payway,pname) SELECT gsb02c,gsb03c FROM gsm02 WITH (nolock) WHERE gsb00c=@compid AND gsb01c='3' --项目 UPDATE #ptable SET amt_srv=a.amt FROM (SELECT ggb27c AS pay, SUM(ggb11f) AS amt FROM ggm02 WITH (nolock), ggm01 WITH (nolock) WHERE ggb00c=@compid AND gga00c=ggb00c AND gga01c=ggb01c AND gga02d BETWEEN @fromdate AND @todate GROUP BY ggb27c) AS a WHERE a.pay=payway --产品 UPDATE #ptable SET amt_good=a2.amt FROM (SELECT ggc27c AS pay, SUM(ggc11f) AS amt FROM ggm03 c WITH (nolock),ggm01 g WITH (nolock) WHERE ggc00c=@compid AND gga00c=ggc00c AND gga01c=ggc01c AND gga02d BETWEEN @fromdate AND @todate GROUP BY ggc27c) AS a2 WHERE a2.pay=payway --卖卡充值 UPDATE #ptable SET amt_card=a3.amt FROM (SELECT gsc04c AS pay, SUM(gsc05f) AS amt FROM gsm03 WITH (nolock) WHERE gsc02c in ('gn','gz') AND gsc00c=@compid AND gsc10d BETWEEN @fromdate AND @todate GROUP BY gsc04c) AS a3 WHERE a3.pay=payway --总计 UPDATE p SET p.amt=ISNULL(p.amt_srv, 0)+ISNULL(p.amt_good, 0)+ISNULL(p.amt_card, 0) FROM #ptable p SELECT * FROM #ptable DROP TABLE #ptable END GO /****** Object: Table [dbo].[gnm04] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gnm04]( [gnd00c] [varchar](10) NOT NULL, [gnd01c] [varchar](20) NOT NULL, [gnd02c] [varchar](20) NOT NULL, [gnd03f] [float] NULL, [gnd04f] [float] NULL, [gnd05f] [float] NULL, CONSTRAINT [PK_GNM04] PRIMARY KEY CLUSTERED ( [gnd00c] ASC, [gnd01c] ASC, [gnd02c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gcm23] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gcm23]( [gcw00c] [varchar](10) NOT NULL, [gcw01c] [varchar](10) NOT NULL, [gcw02c] [varchar](20) NOT NULL, [gcw03d] [varchar](10) NULL, [gcw04c] [varchar](20) NULL, [gcw05c] [varchar](20) NULL, [gcw06c] [varchar](10) NULL, [gcw07c] [varchar](20) NULL, [gcw08f] [float] NULL, [gcw09c] [varchar](20) NULL, [gcw10f] [float] NULL, [gcw11c] [varchar](20) NULL, [gcw12f] [float] NULL, [gcw13f] [float] NULL, [gcw14f] [float] NULL, [gcw15f] [float] NULL, [gcw16f] [float] NULL, [gcw17f] [float] NULL, [gcw18f] [float] NULL, [gcw99i] [int] NULL, CONSTRAINT [PK_GCM23] PRIMARY KEY CLUSTERED ( [gcw00c] ASC, [gcw01c] ASC, [gcw02c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[hbm01] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[hbm01]( [hba00c] [varchar](10) NOT NULL, [hba01c] [varchar](5) NOT NULL, [hba02c] [varchar](5) NOT NULL, [hba03c] [varchar](10) NOT NULL, [hba04c] [varchar](10) NULL, [hba05f] [float] NULL, [hba06c] [varchar](5) NULL, CONSTRAINT [PK_HBM01] PRIMARY KEY CLUSTERED ( [hba00c] ASC, [hba01c] ASC, [hba02c] ASC, [hba03c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_get_happy_birthday_customers] Script Date: 06/12/2017 06:20:07 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_get_happy_birthday_customers]( @compid varchar(10), @fromdate varchar(4), @todate varchar(4), @gregorian int, @include_sons int) as begin create table #tbl_members( compid varchar(20) not null, memid varchar(20) not null, cardid varchar(20) null, nname varchar(40) null, birth varchar(20) null, tel varchar(60) null, mobile varchar(60) null, addr varchar(160) null, birth_y varchar(20) null) create table #tbl_members_result( memid varchar(20) not null, cardid varchar(20) null, nname varchar(40) null, birth varchar(20) null, tel varchar(60) null, mobile varchar(60) null, addr varchar(160) null, birth_y varchar(20) null) if @include_sons = 0 begin --没有卡会员的日期范围内的生日 if @gregorian = 0 begin insert #tbl_members(compid, memid, cardid, nname, birth, birth_y, tel, mobile, addr) select gba00c, gba01c, '', gba03c, gba17d, gba21c, gba07c, gba08c, gba05c from gbm01 where gba00c = @compid and (substring(gba21c, 5, 4) >= @fromdate and substring(gba21c, 5, 4) <= @todate) and isnull(gba23c, '') = '' end else begin insert #tbl_members(compid, memid, cardid, nname, birth, birth_y, tel, mobile, addr) select gba00c, gba01c, '', gba03c, gba17d, gba21c, gba07c, gba08c, gba05c from gbm01 where gba00c = @compid and (substring(gba17d, 5, 4) >= @fromdate and substring(gba17d, 5, 4) <= @todate) and isnull(gba23c, '') = '' end if @gregorian = 0 begin --本公司自己的会员卡 insert #tbl_members(compid, memid, cardid, nname, birth, birth_y, tel, mobile, addr) select gba00c, gba01c, gba23c, gba03c, gba17d, gba21c, gba07c, gba08c, gba05c from gbm01 a, gcm01 b where gba00c = @compid and (substring(gba21c, 5, 4) >= @fromdate and substring(gba21c, 5, 4) <= @todate) and isnull(gba23c, '')<>'' and a.gba00c = b.gca00c and a.gba23c = b.gca01c and b.gca00c = b.gca13d end else begin insert #tbl_members(compid, memid, cardid, nname, birth, birth_y, tel, mobile, addr) select gba00c, gba01c, gba23c, gba03c, gba17d, gba21c, gba07c, gba08c, gba05c from gbm01 a, gcm01 b where gba00c = @compid and (substring(gba17d, 5, 4) >= @fromdate and substring(gba17d, 5, 4) <= @todate) and isnull(gba23c, '')<>'' and a.gba00c = b.gca00c and a.gba23c = b.gca01c and b.gca00c = b.gca13d end end else begin --包括自己下面的所有公司 --没有卡会员的日期范围内的生日 if @gregorian = 0 begin insert #tbl_members(compid, memid, cardid, nname, birth, birth_y, tel, mobile, addr) select gba00c, gba01c, '', gba03c, gba17d, gba21c, gba07c, gba08c, gba05c from gbm01 a, gam26 z where gba00c = z.gaz02c and z.gaz01c = @compid and (substring(gba21c, 5, 4) >= @fromdate and substring(gba21c, 5, 4) <= @todate) and isnull(gba23c, '') = '' --本公司自己的会员卡 insert #tbl_members(compid, memid, cardid, nname, birth, birth_y, tel, mobile, addr) select gba00c, gba01c, gba23c, gba03c, gba17d, gba21c, gba07c, gba08c, gba05c from gbm01 a, gcm01 b, gam26 z where a.gba00c = z.gaz02c and z.gaz01c = @compid and (substring(gba21c, 5, 4) >= @fromdate and substring(gba21c, 5, 4) <= @todate) and isnull(gba23c, '')<>'' and a.gba00c = b.gca00c and a.gba23c = b.gca01c and b.gca00c = b.gca13d end else begin insert #tbl_members(compid, memid, cardid, nname, birth, birth_y, tel, mobile, addr) select gba00c, gba01c, '', gba03c, gba17d, gba21c, gba07c, gba08c, gba05c from gbm01 a, gam26 z where gba00c = z.gaz02c and z.gaz01c = @compid and (substring(gba17d, 5, 4) >= @fromdate and substring(gba17d, 5, 4) <= @todate) and isnull(gba23c, '') = '' --本公司自己的会员卡 insert #tbl_members(compid, memid, cardid, nname, birth, birth_y, tel, mobile, addr) select gba00c, gba01c, gba23c, gba03c, gba17d, gba21c, gba07c, gba08c, gba05c from gbm01 a, gcm01 b, gam26 z where a.gba00c = z.gaz02c and z.gaz01c = @compid and (substring(gba17d, 5, 4) >= @fromdate and substring(gba17d, 5, 4) <= @todate) and isnull(gba23c, '')<>'' and a.gba00c = b.gca00c and a.gba23c = b.gca01c and b.gca00c = b.gca13d end end insert #tbl_members_result(memid, cardid, nname, birth, birth_y, tel, mobile, addr) select distinct memid, cardid, nname, birth, birth_y, tel, mobile, addr from #tbl_members if @gregorian = 0 select memid, cardid, nname, birth, birth_y, tel, mobile, addr from #tbl_members_result order by substring(birth_y, 5, 4), memid else select memid, cardid, nname, birth, birth_y, tel, mobile, addr from #tbl_members_result order by substring(birth, 5, 4), memid drop table #tbl_members drop table #tbl_members_result end GO /****** Object: Table [dbo].[gpm01] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gpm01]( [gpa00c] [varchar](10) NOT NULL, [gpa01c] [varchar](20) NOT NULL, [gpa02d] [varchar](8) NULL, [gpa03c] [varchar](5) NULL, [gpa04c] [varchar](20) NULL, [gpa05c] [varchar](5) NULL, [gpa06c] [varchar](20) NULL, [gpa07c] [varchar](10) NULL, [gpa91c] [varchar](10) NULL, [gpa92d] [varchar](8) NULL, [gpa93c] [varchar](10) NULL, [gpa94d] [varchar](8) NULL, CONSTRAINT [PK_GPM01] PRIMARY KEY CLUSTERED ( [gpa00c] ASC, [gpa01c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gam12] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gam12]( [gam01c] [varchar](10) NOT NULL, [gam02c] [varchar](20) NULL, [gam03c] [varchar](5) NOT NULL, [gam04c] [varchar](10) NOT NULL, [gam05c] [varchar](20) NULL, CONSTRAINT [PK_GAM12] PRIMARY KEY CLUSTERED ( [gam01c] ASC, [gam03c] ASC, [gam04c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_select_member_treatment_detail] Script Date: 06/12/2017 06:20:40 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_select_member_treatment_detail] ( @compid varchar(10),--公司编号 @cardid varchar(20),--卡号 @fromdate varchar(8),--开始时间 @todate varchar(8)--结束时间 ) as begin create table #result ( compId varchar(10) not null,--公司 cardId varchar(20) not null,--卡号 projectId varchar(20) not null,--项目编号 projectName varchar(100) null,--项目名称 times float null,--次数, overlytimes float null,--叠加次数 timesLess float null,--剩余次数 moneyLess float null,--剩余金额 amtOpenDate varchar(8) null,--账户开立日 staffcode varchar(20) null,--员工编号 staffName varchar(20) null,--员工名称 timesBuy float null,--购买次数 timesGive float null,--赠送次数 moneyBuy float null,--已使用+剩余 timesConsume float null,--已使用次数 moneyCourse float null--已使用金额 ) create table #gcm06 ( gcf00c varchar(10) not null, --门店编号 gcf01c varchar(20) not null, --卡号 gcf02c varchar(20) not null, --项目编号 gcf03f float null, --次数 gcf04f float null, --赠送次数 gcf05f float null, --总次数 gcf06f float null, --已经使用次数 gcf07f float null, --剩余次数 gcf08f float null, --疗程金额 gcf09f float null, --已经使用金额 gcf10f float null, --剩余金额 gcf11d varchar(8) null, --账户开立日 gcf12d varchar(8) null, --账户截至日 gcf13i int null, --疗程间隔 gcf14f float null, --一个月可使用的次数 0--代表不控制 直到这个疗程用完其他float就是一个月可以使用的次数 gcf15c varchar(60) null, --输入的时候的备注 gcf16i int null, --是否已经审核 gcf17i int not null, --价格序号 gcf18f float not null, --折扣 gcf19i int null, --是否停用 2停用 1正常 3买产品赠送项目 gcf20i int null, --标志 是否前期卡 gcf21i int null, --是否赠送 gcf22i int null, --永久免费 gcf23i int not null, --序号 gcf24i int null, --开帐中的序号(gcm16.gcr23i) gcf25f float null, --虚假次数(只有增加部分) gcf26c nvarchar(2000) null, --疗程备注 gcf27i int not null default 0 ,--过期疗程是否处理,0表示没处理 gcf28i bit not null default 0, --是否已退 primary key(gcf00c,gcf01c,gcf23i) ) insert into #gcm06(gcf00c,gcf01c,gcf02c,gcf03f,gcf04f,gcf05f,gcf06f,gcf07f,gcf08f,gcf09f,gcf10f,gcf11d,gcf12d,gcf13i,gcf14f,gcf15c,gcf16i,gcf17i,gcf18f,gcf19i,gcf20i,gcf21i,gcf22i,gcf23i,gcf24i,gcf25f,gcf26c,gcf27i,gcf28i) select gcf00c,gcf01c,gcf02c,gcf03f,gcf04f,gcf05f,gcf06f,gcf07f,gcf08f,gcf09f,gcf10f,gcf11d,gcf12d,gcf13i,gcf14f,gcf15c,gcf16i,gcf17i,gcf18f,gcf19i,gcf20i,gcf21i,gcf22i,gcf23i,gcf24i,gcf25f,gcf26c,gcf27i,gcf28i from gcm06 with(nolock) where gcf00c = @compid and gcf01c = @cardid and isnull(gcf07f,0) > 0 create table #gnm01_gnm02 ( gna00c varchar(10) not null, --门店编号 gna01c varchar(20) not null, --销售单号 gna02d varchar(8) null, --销售日期 gna03c varchar(5) null, --证件类别 gna04c varchar(20) null, --会员编号(自动寻找到的匹配) gna05c varchar(5) null, --客户性质 gna06c varchar(30) null, --客户名称 gna07c varchar(160)null, --备注 gna20c varchar(10) null, --负责业务 gnb02c varchar(5) null, --卡种 gnb03c varchar(20) not null, --会员卡号 gnb04i int null, --折让类别(分为 1-一般折让,2-续卡优惠价,3-内部优惠价) gnb05f float null, --标准金额 ) insert into #gnm01_gnm02(gna00c,gna01c,gna02d,gna03c,gna04c,gna05c,gna06c,gna07c,gna20c,gnb02c,gnb03c,gnb04i,gnb05f) select gna00c,gna01c,gna02d,gna03c,gna04c,gna05c,gna06c,gna07c,gna20c,gnb02c,gnb03c,gnb04i,gnb05f from gnm01 with(nolock),gnm02 with(nolock) where gna00c = @compid and gnb00c = @compid and gnb03c = @cardid and gna01c = gnb01c insert into #result(compId,cardId,projectId,projectName,times,timesLess,moneyLess,amtOpenDate) select gcf00c,gcf01c,gcf02c,gda03c,sum(gcf03f),sum(gcf07f),sum(gcf10f),gcf11d from ( select gcf00c,gcf01c,gcf02c,gda03c,gcf03f,gcf07f,gcf10f,gcf11d from #gcm06 LEFT JOIN gdm01 with(nolock) ON gcf02c = gda01c AND gcf00c = gda00c where gda00c = @compid ) t_treat_1 group by gcf00c,gcf01c,gcf02c,gda03c,gcf11d update #result set staffcode = gna20c from #result ,#gnm01_gnm02 where compId = gna00c AND cardId = gnb03c update #result set staffName = isnull(haa02c,'') from #result ,ham01 with(nolock) where haa00c = @compid and staffcode = haa01c update a set a.timesBuy = t_treat_2.timesBuy, a.timesGive = t_treat_2.timesGive, a.moneyBuy = t_treat_2.moneyBuy, a.timesConsume = t_treat_2.timesConsume , a.moneyCourse = t_treat_2.moneyCourse from #result a, (select gcf00c,gcf01c,gcf02c,sum(isnull(gcf03f,0))timesBuy,sum(isnull(gcf04f,0))timesGive, sum(isnull(gcf09f,0)+isnull(gcf10f,0))moneyBuy,sum(isnull(gcf06f,0))timesConsume,sum(isnull(gcf09f,0)) moneyCourse from #gcm06 where (gcf11d >= @fromdate or isnull(@fromdate,'*') = '*' ) and (gcf11d <= @todate or isnull(@todate,'*')='*') group by gcf00c,gcf01c,gcf02c )t_treat_2 where a.cardId = t_treat_2.gcf01c and a.projectId = t_treat_2.gcf02c create table #gcm20 ( gct00c varchar(10) not null, --门店编号 gct01i int not null, --序号 gct02c varchar(20) not null, --卡号 gct03c varchar(20) not null, --会员编号 gct04d varchar(8 ) not null, --日期 gct05c varchar(5) null, --异动标志,账户的异动方式 gct06c varchar(10) null, --代码的类别--1-卡销售 2-疗程销售 3-项目 4-产品销售 - 5卡充值 - 6疗程充值 gct07c varchar(20) null, --代码 gct08c varchar(100) null, --名称 gct09f float null, --标准单价 gct10f float null, --折扣单价 gct11f float null, --数量 gct12f float null, --价格 gct13c varchar(10) null, --单据类别 gct14c varchar(20) null, --单据单号 gct15c varchar(20) null, --员工1 gct16c varchar(20) null, --员工2 gct17c varchar(20) null, --员工3 gct18c varchar(20) null, --员工4 gct19t varchar(10) null, --时间 gct20c varchar(5) null, --支付方式 gct21f float null, --项目消费序号 primary key(gct00c,gct01i) ) insert into #gcm20(gct00c,gct01i,gct02c,gct03c,gct04d,gct05c,gct06c,gct07c,gct08c,gct09f,gct10f,gct11f,gct12f,gct13c,gct14c,gct15c,gct16c,gct17c,gct18c,gct19t,gct20c,gct21f) select gct00c,gct01i,gct02c,gct03c,gct04d,gct05c,gct06c,gct07c,gct08c,gct09f,gct10f,gct11f,gct12f,gct13c,gct14c,gct15c,gct16c,gct17c,gct18c,gct19t,gct20c,gct21f from gcm20 with(nolock) where gct00c = @compid and gct02c = @cardid and gct13c in('gn','gz') and gct06c in('2','6') update a set a.overlytimes = b.cnt from #result a,(select gct02c,gct07c,count(*) cnt from #gcm20 group by gct02c,gct07c)b where a.cardId = b.gct02c and a.projectId = b.gct07c select compId,cardId,projectId,projectName,times,timesLess,moneyLess,amtOpenDate,staffName,timesBuy,timesGive,moneyBuy,timesConsume,moneyCourse from #result drop table #gcm06 drop table #result end GO /****** Object: Table [dbo].[recover_ggm05] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[recover_ggm05]( [id] [int] NOT NULL, [gge00c] [varchar](10) NOT NULL, [gge01c] [varchar](20) NOT NULL, [gge02f] [float] NOT NULL, [gge03c] [varchar](20) NULL, [gge04c] [varchar](5) NULL, [gge05f] [float] NULL, [gge06c] [varchar](5) NULL, [gge07f] [float] NULL, [gge08f] [float] NULL, [gge09f] [float] NULL, [gge10f] [float] NULL, [gge11f] [float] NULL, [gge12c] [varchar](20) NULL, [gge13f] [float] NULL, [gge14c] [varchar](20) NULL, [gge15c] [varchar](20) NULL, [gge16f] [float] NULL, [gge81c] [varchar](10) NULL, [gge17c] [varchar](20) NULL, [gge18i] [int] NULL, CONSTRAINT [PK_revocer_GGM05] PRIMARY KEY CLUSTERED ( [id] ASC, [gge00c] ASC, [gge01c] ASC, [gge02f] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[openpolicy_pay_together] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[openpolicy_pay_together]( [id] [int] IDENTITY(1,1) NOT NULL, [compid] [varchar](20) NOT NULL, [operationdate] [varchar](8) NOT NULL, [operationtime] [varchar](8) NULL, [rootcard] [varchar](20) NULL, [handcard] [varchar](20) NULL, [billflag] [int] NOT NULL, CONSTRAINT [PK_openpolicy_pay_together] PRIMARY KEY CLUSTERED ( [id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_ymn_sale_consume_detail] Script Date: 06/12/2017 06:20:53 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_ymn_sale_consume_detail] ( @compid varchar(10), @fromprjid varchar(10), -- 项目编号 @toprjid varchar(10), @fromgoodid varchar(10), @togoodid varchar(10), -- 产品编号 @fromdate varchar(10), -- 20091001 @todate varchar(10) -- 20091031 ) as begin declare @opendate varchar(20) select @opendate = '20090801' create table #good_buf ( idd int identity not null, compid varchar(10) null, cardid varchar(20) not null, goodid varchar(20) not null, goodname varchar(60) null, num float null, price float null, amt float null, ) declare @PK_Sql nvarchar(max) set @PK_Sql = ' alter table #good_buf add constraint PK_#good_buf_' + convert(varchar(10), cast(floor(rand() * 1000000) as int)) + ' primary key(idd)' exec sp_executesql @PK_Sql -- 疗程购买 create table #proc_buf_buy ( idd int identity not null, compid varchar(10) null, cardid varchar(20) not null, itemid varchar(20) not null, num float null, amt float null, procseq int not null, -- 疗程序号 ddate varchar(20) not null, type int null, -- 1疗程 2套餐 ) set @PK_Sql = ' alter table #proc_buf_buy add constraint PK_#proc_buf_buy_' + convert(varchar(10), cast(floor(rand() * 1000000) as int)) + ' primary key nonclustered(idd)' exec sp_executesql @PK_Sql create clustered index idx_proc_buf_buy_02 on #proc_buf_buy(cardid, itemid, procseq) --疗程消耗 create table #proc_buf_con ( idd int identity not null, cardid varchar(20) not null, itemid varchar(20) not null, num float null, amt float null, procseq int not null, -- 疗程序号 ddate varchar(20) not null, flag int null, -- 1 是购买的卡项的消费 type int null, -- 1疗程 2套餐 ) set @PK_Sql = ' alter table #proc_buf_con add constraint PK_#proc_buf_con_' + convert(varchar(10), cast(floor(rand() * 1000000) as int)) + ' primary key nonclustered(idd)' exec sp_executesql @PK_Sql create clustered index idx_proc_buf_con_02 on #proc_buf_con(cardid, itemid, procseq) -- 产品销售的 insert #good_buf(compid, cardid, goodid, num, price, amt) select gga00c, gga09c, ggc03c, sum(ggc05f), ggc10f, sum(ggc11f) from ggm03 b with (nolock), ggm01 a with (nolock), gam26 z with (nolock) where b.ggc00c = a.gga00c and b.ggc01c = a.gga01c and a.gga80d >= @fromdate and a.gga80d <= @todate and a.gga00c = z.gaz02c and z.gaz01c = @compid -- and (ggc27c = '1' or ggc27c = '6' or ggc27c = '$') group by gga00c, gga09c, ggc03c, ggc10f order by gga09c, ggc03c -- 统计8余份开始20090801开始到现在的所有的销售的卡 -- 包括本月购买的卡,从日期上可以判断出来 insert #proc_buf_buy(compid, cardid, itemid, num, amt, ddate, procseq, type) select gch00c, gch03c, gch04c, isnull(gch05f, 0) + isnull(gch06f, 0), amt = gch07f, --modify by lm 20150806 (不需要判断支付方式) gch02d, isnull(gch16i, 0), 1 from gcm08 a with (nolock), gam26 z with (nolock) where a.gch02d >= @opendate and a.gch02d <= @todate and a.gch00c = z.gaz02c and z.gaz01c = @compid and isnull(gch05f, 0) + isnull(gch06f, 0) > 0 -- 购买套餐 insert #proc_buf_buy(compid, cardid, itemid, num, amt, ddate, procseq, type) select gti00c, gti03c, gti05c, isnull(gti06f, 0), 0, gti02d, 0, 2 from gtc07 a with (nolock), gam26 z with (nolock) where a.gti02d >= @opendate and a.gti02d <= @todate and a.gti00c = z.gaz02c and z.gaz01c = @compid and isnull(gti06f, 0) > 0 and gti12i = 2 -- 统计8余份到现在的所有消耗,必须删除不是8月份开始的那些卡的消耗 insert #proc_buf_con(cardid, itemid, num, amt, ddate, procseq, type) select gch03c, gch04c, gch08f, gch09f, gch02d, isnull(gch16i, 0), 1 from gcm08 a with (nolock), gam26 z with (nolock) where a.gch02d >= @opendate and a.gch02d <= @todate and a.gch00c = z.gaz02c and z.gaz01c = @compid and isnull(gch08f, 0) > 0 -- 消耗套餐 insert #proc_buf_con(cardid, itemid, num, amt, ddate, procseq, type) select gti03c, gti05c, gti07f, 0, gti02d, 0, 2 from gtc07 a with (nolock), gam26 z with (nolock) where a.gti02d >= @opendate and a.gti02d <= @todate and a.gti00c = z.gaz02c and z.gaz01c = @compid and isnull(gti07f, 0) > 0 -- 需要把原来的卡项给删除掉 update a set a.flag = 1 from #proc_buf_con a, #proc_buf_buy b where a.cardid = b.cardid and a.procseq = b.procseq and a.itemid = b.itemid and a.type = b.type delete a from #proc_buf_con a where isnull(flag, 0) = 0 -- 结果 create table #stat_result ( idd int identity not null, compid varchar(10) null, cardid varchar(20) null, nname varchar(60) null, itemid varchar(20) null, itemname varchar(60) null, gna66c varchar(60) null, -- 会员手机号 finalconsumption varchar(60), -- 最后一次消费时间 Consumptiondays varchar(60), -- 未消费天数 procseq int null, start_num float null, start_amt float null, buy_num float null, buy_amt float null, con_num float null, con_amt float null, end_num float null, end_amt float null, g_compid varchar(10) null, g_cardid varchar(20) null, g_nname varchar(60) null, g_goodid varchar(20) null, g_goodname varchar(60) null, g_num float null, g_price float null, g_amt float null, ) set @PK_Sql = ' alter table #stat_result add constraint PK_#stat_result_' + convert(varchar(10), cast(floor(rand() * 1000000) as int)) + ' primary key clustered(idd)' exec sp_executesql @PK_Sql create table #stat_result_buy ( idd int identity not null, cardid varchar(20) not null, itemid varchar(20) null, procseq int null, num float null, amt float null, ) set @PK_Sql = ' alter table #stat_result_buy add constraint PK_#stat_result_buy_' + convert(varchar(10), cast(floor(rand() * 1000000) as int)) + ' primary key nonclustered(idd)' exec sp_executesql @PK_Sql create clustered index idx_stat_result_buy on #stat_result_buy(cardid, itemid, procseq) create table #stat_result_con ( idd int identity not null, cardid varchar(20) not null, itemid varchar(20) null, procseq int null, num float null, amt float null, ) set @PK_Sql = ' alter table #stat_result_con add constraint PK_#stat_result_con_' + convert(varchar(10), cast(floor(rand() * 1000000) as int)) + ' primary key nonclustered(idd)' exec sp_executesql @PK_Sql create clustered index idx_stat_result_con on #stat_result_con(cardid, itemid, procseq) -- 初始化结果表 insert #stat_result(compid, cardid, itemid) select distinct compid, cardid, itemid from #proc_buf_buy -- 计算期初 insert #stat_result_buy(cardid, itemid, num, amt) select cardid, itemid, sum(num), sum(amt) from #proc_buf_buy where ddate < @fromdate group by cardid, itemid insert #stat_result_con(cardid, itemid, num, amt) select cardid, itemid, sum(num), sum(amt) from #proc_buf_con where ddate < @fromdate group by cardid, itemid update a set a.start_num = b.num, a.start_amt = b.amt from #stat_result a, #stat_result_buy b where a.cardid = b.cardid and a.itemid = b.itemid -- update a --modify by lm 期初只算买疗程,不算消耗 -- set a.start_num = a.start_num , -- a.start_amt = a.start_amt --from #stat_result a, #stat_result_con b --where a.cardid = b.cardid --and a.itemid = b.itemid -- 计算期末 delete #stat_result_buy insert #stat_result_buy(cardid, itemid, num, amt) select cardid, itemid, sum(num), sum(amt) from #proc_buf_buy group by cardid, itemid delete #stat_result_con insert #stat_result_con(cardid, itemid, num, amt) select cardid, itemid, sum(num), sum(amt) from #proc_buf_con group by cardid, itemid update a set a.end_num = b.num, a.end_amt = b.amt from #stat_result a, #stat_result_buy b where a.cardid = b.cardid and a.itemid = b.itemid update a set a.end_num = a.end_num - b.num, a.end_amt = a.end_amt - b.amt from #stat_result a, #stat_result_con b where a.cardid = b.cardid and a.itemid = b.itemid -- 发生额 delete #stat_result_buy insert #stat_result_buy(cardid, itemid, num, amt) select cardid, itemid, sum(num), sum(amt) from #proc_buf_buy where ddate >= @fromdate group by cardid, itemid delete #stat_result_con insert #stat_result_con(cardid, itemid, num, amt) select cardid, itemid, sum(num), sum(amt) from #proc_buf_con where ddate >= @fromdate group by cardid, itemid update a set a.buy_num = b.num, a.buy_amt = b.amt from #stat_result a, #stat_result_buy b where a.cardid = b.cardid and a.itemid = b.itemid update a set a.con_num = b.num, a.con_amt = b.amt from #stat_result a, #stat_result_con b where a.cardid = b.cardid and a.itemid = b.itemid -- 更新顾客名字 --把产品合并进来 update a set a.g_compid = b.compid, a.g_goodid = b.goodid, a.g_cardid = b.cardid, a.g_num = b.num, a.g_price = b.price, a.g_amt = b.amt from #stat_result a, #good_buf b where a.idd = b.idd delete a from #good_buf a, #stat_result b where a.idd = b.idd -- 可能还有多余的,需要插入 insert #stat_result(g_compid, g_cardid, g_goodid, g_num, g_price, g_amt) select compid, cardid, goodid, num, price, amt from #good_buf order by cardid, goodid update a set a.nname = b.gba03c from #stat_result a, gbm01 b with (nolock) where a.cardid = b.gba23c and a.compid = b.gba00c update a set a.g_nname = b.gba03c from #stat_result a, gbm01 b with (nolock) where a.g_cardid = b.gba23c and a.g_compid = b.gba00c update a set a.g_goodname = b.gfa03c from #stat_result a, gfm01 b with (nolock) where a.g_goodid = b.gfa01c and a.g_compid = b.gfa00c update a set a.itemname = b.gda03c from #stat_result a, gdm01 b with (nolock) where a.itemid = b.gda01c and a.compid = b.gda00c -- 增加字段--客户手机号和客户最后一次消费时间 -- 会员手机号 update a set a.gna66c = b.gna66c from #stat_result a, gnm01 b with (nolock) where a.cardid = b.gna04c and a.compid = b.gna00c -- 最后一次消费时间 update a set a.finalconsumption = b.finalconsumption from #stat_result a left join(select max(gct04d) finalconsumption, gct00c, gct02c from gcm20 with (nolock) group by gct00c, gct02c) b on a.cardid = b.gct02c and a.compid = b.gct00c -- 未消费天数 update a set a.Consumptiondays = case when a.finalconsumption is null then null else datediff(day, a.finalconsumption, getdate()) end from #stat_result a -- 项目编号和产品编号筛选 if (@fromprjid = '*'and @toprjid = '*'and @fromgoodid = '*' and @togoodid = '*') begin select idd, isnull(compid,'') compid,isnull(cardid,'') cardid,isnull(nname,'') nname,isnull(itemid,'') itemid,isnull(itemname,'') itemname, isnull(gna66c,'') gna66c,isnull(finalconsumption,'') finalconsumption,isnull(Consumptiondays,'') Consumptiondays, isnull(procseq,0) procseq,isnull(start_num,0) start_num, isnull(start_amt,0) start_amt,isnull(buy_num,0) buy_num, isnull(buy_amt,0) buy_amt,isnull(con_num,0) con_num,isnull(con_amt,0) con_amt,isnull(end_num,0) end_num,isnull(end_amt,'') end_amt, isnull(g_compid,'') g_compid,isnull(g_cardid,'') g_cardid,isnull(g_nname,'') g_nname,isnull(g_goodid,'') g_goodid, isnull(g_goodname,'') g_goodname,isnull(g_num,0) g_num,isnull(g_price,0) g_price,isnull(g_amt,0) g_amt from #stat_result order by idd, g_cardid end else if (@fromprjid = '*' and @toprjid = '*'and (@fromgoodid != '*'or @togoodid != '*')) begin select idd, isnull(compid,'') compid,isnull(cardid,'') cardid,isnull(nname,'') nname,isnull(itemid,'') itemid,isnull(itemname,'') itemname, isnull(gna66c,'') gna66c,isnull(finalconsumption,'') finalconsumption,isnull(Consumptiondays,'') Consumptiondays, isnull(procseq,0) procseq,isnull(start_num,0) start_num, isnull(start_amt,0) start_amt,isnull(buy_num,0) buy_num, isnull(buy_amt,0) buy_amt,isnull(con_num,0) con_num,isnull(con_amt,0) con_amt,isnull(end_num,0) end_num,isnull(end_amt,'') end_amt, isnull(g_compid,'') g_compid,isnull(g_cardid,'') g_cardid,isnull(g_nname,'') g_nname,isnull(g_goodid,'') g_goodid, isnull(g_goodname,'') g_goodname,isnull(g_num,0) g_num,isnull(g_price,0) g_price,isnull(g_amt,0) g_amt from #stat_result where g_goodid >= @fromgoodid and g_goodid <= @togoodid order by idd, g_cardid end else if ((@fromprjid != '*' or @toprjid != '*')and @fromgoodid = '*' and @togoodid = '*') begin select idd, isnull(compid,'') compid,isnull(cardid,'') cardid,isnull(nname,'') nname,isnull(itemid,'') itemid,isnull(itemname,'') itemname, isnull(gna66c,'') gna66c,isnull(finalconsumption,'') finalconsumption,isnull(Consumptiondays,'') Consumptiondays, isnull(procseq,0) procseq,isnull(start_num,0) start_num, isnull(start_amt,0) start_amt,isnull(buy_num,0) buy_num, isnull(buy_amt,0) buy_amt,isnull(con_num,0) con_num,isnull(con_amt,0) con_amt,isnull(end_num,0) end_num,isnull(end_amt,'') end_amt, isnull(g_compid,'') g_compid,isnull(g_cardid,'') g_cardid,isnull(g_nname,'') g_nname,isnull(g_goodid,'') g_goodid, isnull(g_goodname,'') g_goodname,isnull(g_num,0) g_num,isnull(g_price,0) g_price,isnull(g_amt,0) g_amt from #stat_result where itemid >= @fromprjid and itemid <= @toprjid order by idd, g_cardid end else begin select idd, isnull(compid,'') compid,isnull(cardid,'') cardid,isnull(nname,'') nname,isnull(itemid,'') itemid,isnull(itemname,'') itemname, isnull(gna66c,'') gna66c,isnull(finalconsumption,'') finalconsumption,isnull(Consumptiondays,'') Consumptiondays, isnull(procseq,0) procseq,isnull(start_num,0) start_num, isnull(start_amt,0) start_amt,isnull(buy_num,0) buy_num, isnull(buy_amt,0) buy_amt,isnull(con_num,0) con_num,isnull(con_amt,0) con_amt,isnull(end_num,0) end_num,isnull(end_amt,'') end_amt, isnull(g_compid,'') g_compid,isnull(g_cardid,'') g_cardid,isnull(g_nname,'') g_nname,isnull(g_goodid,'') g_goodid, isnull(g_goodname,'') g_goodname,isnull(g_num,0) g_num,isnull(g_price,0) g_price,isnull(g_amt,0) g_amt from #stat_result where itemid >= @fromprjid and itemid <= @toprjid and g_goodid >= @fromgoodid and g_goodid <= @togoodid order by idd, g_cardid end drop table #good_buf drop table #stat_result_buy drop table #stat_result_con drop table #stat_result drop table #proc_buf_buy drop table #proc_buf_con end GO /****** Object: Table [dbo].[gam17] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gam17]( [gao00c] [varchar](10) NOT NULL, [gao01i] [int] IDENTITY(1,1) NOT NULL, [gao02c] [varchar](10) NULL, [gao03c] [varchar](128) NULL, [gao04d] [varchar](8) NULL, [gao05t] [varchar](6) NULL, [gao06c] [varchar](20) NULL, [gao07c] [varchar](20) NULL, [gco08d] [varchar](8) NULL, [gco09c] [varchar](20) NULL, [gco10c] [varchar](20) NULL, CONSTRAINT [PK_GAM17] PRIMARY KEY CLUSTERED ( [gao00c] ASC, [gao01i] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gam29] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gam29]( [gax00c] [varchar](10) NOT NULL, [gax01c] [varchar](20) NOT NULL, [gax02i] [int] NOT NULL, [gax03i] [int] NULL, CONSTRAINT [PK_GAM29] PRIMARY KEY CLUSTERED ( [gax00c] ASC, [gax01c] ASC, [gax02i] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gdm07] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gdm07]( [gdg00c] [varchar](10) NOT NULL, [gdg01c] [varchar](5) NOT NULL, [gdg02c] [varchar](5) NOT NULL, [gdg03c] [varchar](5) NOT NULL, [gdg04f] [float] NULL, [gdg05f] [float] NULL, [gdg06d] [varchar](8) NULL, [gdg07d] [varchar](8) NULL, [gdg08i] [int] NULL, [gdg09i] [int] NULL, [gdg10f] [float] NOT NULL, [gdg11i] [int] NULL, [gdg12f] [float] NULL, CONSTRAINT [PK_GDM07] PRIMARY KEY CLUSTERED ( [gdg00c] ASC, [gdg01c] ASC, [gdg02c] ASC, [gdg10f] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[member_enter_sms] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[member_enter_sms]( [compid] [varchar](20) NOT NULL, [cardid] [varchar](50) NOT NULL, [enterdate] [varchar](10) NOT NULL, [sendsms] [int] NULL, CONSTRAINT [PK_member_enter_sms] PRIMARY KEY CLUSTERED ( [compid] ASC, [cardid] ASC, [enterdate] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[workdetail_by_empl] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[workdetail_by_empl]( [id] [int] IDENTITY(1,1) NOT NULL, [person_id] [varchar](20) NOT NULL, [name] [varchar](40) NULL, [amt_c] [float] NULL, [amt_1] [float] NULL, [amt_12] [float] NULL, [amt_13] [float] NULL, [amt_2] [float] NULL, [amt_21] [float] NULL, [amt_3] [float] NULL, [amt_31] [float] NULL, [amt_4] [float] NULL, [amt_41] [float] NULL, [amt_g] [float] NULL, [amtrate_1] [float] NULL, [comm] [float] NULL, [total_srv] [float] NULL, [total_perf] [float] NULL, [total_cash] [float] NULL, [total_cash_c] [float] NULL, [bonus] [float] NULL, [total_srv_cash] [float] NULL, [total_srv_card] [float] NULL, [num_fix] [float] NULL, [num_nofix] [float] NULL, [amt_c_cash] [float] NULL, [oper_amt] [float] NULL, [total_vperf] [float] NULL, [total_tperf] [float] NULL, [quan] [float] NULL, [f_oper_amt] [float] NULL, [f_total_vperf] [float] NULL, [f_total_tperf] [float] NULL, [f_comm] [float] NULL, [f_quan] [float] NULL, [t_oper_amt] [float] NULL, [t_total_vperf] [float] NULL, [t_total_tperf] [float] NULL, [t_comm] [float] NULL, [t_quan] [float] NULL, [compid] [varchar](10) NULL, [fromdate] [varchar](10) NULL, [todate] [varchar](10) NULL, [spid] [int] NULL, [cust_num] [float] NULL, [cust_comm] [float] NULL, [consume_cost] [float] NULL, [comm_prj] [float] NULL, [comm_good] [float] NULL, [comm_card] [float] NULL, [amt_1_14] [float] NULL, [amt_1_15] [float] NULL, [amt_2_14] [float] NULL, [amt_2_15] [float] NULL, [amt_3_14] [float] NULL, [amt_3_15] [float] NULL, [amt_4_14] [float] NULL, [amt_4_15] [float] NULL, [num_in] [float] NULL, [num_outer] [float] NULL, CONSTRAINT [PK_workdetail_by_empl] PRIMARY KEY NONCLUSTERED ( [id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_stores_prjcostanalysis] Script Date: 06/12/2017 06:20:46 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_stores_prjcostanalysis] @compid varchar(10), -- 已经能支持各级别的公司 @fromprjid varchar(20), -- 开始项目编号 @toprjid varchar(20), -- 结束项目编号 @fromdate varchar(10), -- 开始日期 @todate varchar(10) -- 结束日期 as begin create table #prjcost --项目成本分析表 ( projectid varchar(20) not null, -- 项目编号 projectname varchar(20) null, -- 项目名称 sellprice float null, -- 销售价格(单) payway varchar(5) null, -- 支付方式 sumcountforway float null, -- 支付方式对应的销售次数 sumamtforway float null, -- 支付方式对应的销售金 sellcount float null, -- 次数 sellamt float null, -- 销售总金额(疗程账户,储值账户,现金,银行卡支付) consumamt float null, -- 消耗成本(产品消耗) staff_integral float null, -- 员工积分 integral_royalty float null, -- 积分提成 staff_prj_royalty float null, -- 员工项目提成 grossprofit float null, -- 毛利润 ) create table #sellproject -- 项目消费 ( compid varchar(10) not null, -- 公司别 sellbillid varchar(20) not null, -- 消费单号 porjectid varchar(20) not null, -- 项目编号 sellno float null, -- 销售项目序号 sellprice float null, -- 销售单价 sellscore float null, -- 销售项目次数 sellamt float null, -- 销售金额 payway varchar(5) null, -- 支付方式 staff_integral float null, -- 员工积分 royaltyflag int null, -- 项目提成 1-有 0-无 royalty1flag int null, -- 项目提成一 1-有 0-无 royalty2flag int null, -- 项目提成二 1-有 0-无 integraltype varchar(20) null, -- 积分提成类型 1-点 2-轮 royaltytype varchar(20) null, -- 项目提成类型 1-点 2-轮 royalty1type varchar(20) null, -- 项目提成一类型 1-点 2-轮 royalty2type varchar(20) null, -- 项目提成二类型 1-点 2-轮 integralrate float null, -- 积分提成比率 royaltyrate float null, -- 项目提成比率 royalty1rate float null, -- 项目提成一比率 royalty2rate float null, -- 项目提成二比率 consumamt float null, -- 产品消耗 ) create table #prjtoconsum --项目消费及对应的产品消耗 ( compid varchar(10) not null, -- 公司别 sellbillid varchar(20) not null, -- 消费单号 sellno float null, -- 项目编号 goodid varchar(20) null, -- 产品编号 goodcount float null, -- 产品数量 consumamt float null, -- 产品消耗 ) ----------------------------获得相应时间范围内项目-------------------------------------- insert #sellproject(compid, sellbillid, porjectid, sellno, sellprice, sellscore, sellamt, payway, staff_integral, integraltype, royaltytype, royalty1type, royalty2type, royaltyflag, royalty1flag, royalty2flag) select ggb00c, ggb01c, ggb03c, ggb02f, ggb08f, ggb05f, ggb11f, ggb27c, ggb20f, ggb15c, ggb16c, ggb19c, ggb25c, case when isnull(ggb13c, '') = '' then 0 else 1 end, case when isnull(ggb14c, '') = '' then 0 else 1 end, case when isnull(ggb26c, '') = '' then 0 else 1 end from ggm01 a with (nolock), ggm02 b with (nolock), gam26 z where a.gga00c = z.gaz02c and z.gaz01c = @compid and a.gga02d between @fromdate and @todate and a.gga00c = b.ggb00c and a.gga01c = b.ggb01c and (b.ggb03c between @fromprjid and @toprjid or @fromprjid = '*') -------------------------------获得相应项目消耗的产品数量---------------------------------------------- insert #prjtoconsum(compid, sellbillid, sellno, goodid, goodcount) select gge00c, gge01c, gge16f, gge03c, gge05f from #sellproject a, ggm05 b where b.gge01c = a.sellbillid and b.gge16f = a.sellno and b.gge00c = a.compid --------------------获得相应项目消耗的成本--------------------------- update a set a.consumamt = (select a.goodcount*b.gfa15f from gfm01 b where a.goodid = b.gfa01c and a.compid = b.gfa00c) from #prjtoconsum a -----------------------更新项目消费表对应的相互成本----------------------------- update a set a.consumamt = (select sum(b.consumamt) from #prjtoconsum b where a.compid = b.compid and a.sellbillid = b.sellbillid and a.sellno = b.sellno) from #sellproject a ------------------------获得每个项目的积分提成与项目提成比率(1--点 2--轮)--------------------------------------------- update a set a.integralrate = (select case when a.integraltype = '1' then hea03f when a.integraltype = '2' then hea09f else 0 end from hem01 where hea01c = a.porjectid and hea18i = '2' and hea00c = a.compid and (isnull(hea34c, '*') = '*' or hea34c = a.payway)), a.royaltyrate = (select case when a.royaltytype = '1' then hea05f when a.royaltytype = '2' then hea15f else 0 end from hem01 where hea01c = a.porjectid and hea18i = '2' and hea00c = a.compid and (isnull(hea34c, '*') = '*' or hea34c = a.payway)), a.royalty1rate = (select case when a.royalty1type = '1' then hea07f when a.royalty2type = '2' then hea17f else 0 end from hem01 where hea01c = a.porjectid and hea18i = '2' and hea00c = a.compid and (isnull(hea34c, '*') = '*' or hea34c = a.payway)), a.royalty2rate = (select case when a.royalty1type = '1' then hea26f when a.royalty2type = '2' then hea28f else 0 end from hem01 where hea01c = a.porjectid and hea18i = '2' and hea00c = a.compid and (isnull(hea34c, '*') = '*' or hea34c = a.payway)) from #sellproject a -----------------------更新项目消费表(只有疗程账户,储值账户,现金,银行卡支付 才有销售价和项目提成)------------------------ update #sellproject set royaltyrate = 0, royalty1rate = 0, royalty2rate = 0, royaltyflag = 0, royalty1flag = 0, royalty2flag = 0 where (payway<>'9' and payway<>'4' and payway<>'1' and payway<>'6') -----------------------------数据支付方式汇总(按项目编号与项目单价)---------------------------------- insert #prjcost(projectid, sellprice, payway, sumcountforway, sumamtforway, sellcount, sellamt, consumamt, staff_integral, integral_royalty, staff_prj_royalty) select a.porjectid, a.sellprice, a.payway, sum(a.sellscore), sum(a.sellamt), sum(a.sellscore), sum(a.sellamt), sum(a.consumamt), sum(a.staff_integral), sum(a.staff_integral*isnull(a.integralrate, 0)), sum(a.sellamt*a.royaltyrate*royaltyflag + a.sellamt*a.royalty1rate*royalty1flag + a.sellamt*a.royalty2rate*royalty2flag) from #sellproject a group by a.porjectid, a.sellprice, a.payway ------------------------------更新项目名称----------------------------------- update a set a.projectname = (select gda03c from gdm01 where gda00c = @compid and gda01c = a.projectid) from #prjcost a ----------------------------根据价格汇总----------------------------------------- declare @projectid varchar(20) -- 项目编号 declare @sellprice float -- 销售单价 declare @sumsellcount float -- 总销售次数 declare @sumsellamt float -- 总销售价格 declare @sumconsumamt float -- 总消耗成本 declare @sumstaff_integral float -- 总消费积分 declare @sumintegral_royalty float -- 总积分提成 declare @sumstaff_prj_royalty float -- 总项目提成 declare @sumgrossprofit float -- 总毛利润 ------------------------------将相同项目编号便且相同价格的项目统计------------------------------------------------- declare cur_prjcost cursor for select projectid, sellprice, sum(sellcount), sum(case when payway = '9' or payway = '4'or payway = '1' or payway = '6' then sellamt else 0 end), sum(consumamt), sum(staff_integral), sum(integral_royalty), sum(staff_prj_royalty), sum(grossprofit) from #prjcost group by projectid, sellprice open cur_prjcost fetch cur_prjcost into @projectid, @sellprice, @sumsellcount, @sumsellamt, @sumconsumamt, @sumstaff_integral, @sumintegral_royalty, @sumstaff_prj_royalty, @sumgrossprofit while @@fetch_status = 0 begin update #prjcost set sellcount = @sumsellcount, sellamt = @sumsellamt, consumamt = @sumconsumamt, staff_integral = @sumstaff_integral, integral_royalty = @sumintegral_royalty, staff_prj_royalty = @sumstaff_prj_royalty, grossprofit = @sumgrossprofit where projectid = @projectid and sellprice = @sellprice fetch cur_prjcost into @projectid, @sellprice, @sumsellcount, @sumsellamt, @sumconsumamt, @sumstaff_integral, @sumintegral_royalty, @sumstaff_prj_royalty, @sumgrossprofit end close cur_prjcost deallocate cur_prjcost -----------------------------计算毛利润-------------------------------------- update #prjcost set grossprofit = (isnull(sellamt, 0)-isnull(consumamt, 0) -isnull(integral_royalty, 0)-isnull(staff_prj_royalty, 0)) select * from #prjcost drop table #prjcost drop table #sellproject drop table #prjtoconsum end GO /****** Object: Table [dbo].[gsm02_3_role] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gsm02_3_role]( [gsa01c_role] [varchar](10) NOT NULL, [gsa02c_role] [varchar](10) NOT NULL, [gsa03i_role] [int] NULL, [gsa04i_role] [int] NULL, [gsa05i_role] [int] NULL, [gsa06i_role] [int] NULL, [gsa07i_role] [int] NULL, [gsa08i_role] [int] NULL, CONSTRAINT [PK_GSM02_3_role] PRIMARY KEY CLUSTERED ( [gsa01c_role] ASC, [gsa02c_role] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gam18] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gam18]( [gap00c] [varchar](10) NOT NULL, [gap01c] [varchar](5) NOT NULL, [gap02c] [varchar](20) NOT NULL, [gap03f] [float] NULL, CONSTRAINT [PK_GAM18] PRIMARY KEY CLUSTERED ( [gap00c] ASC, [gap01c] ASC, [gap02c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[FormCustom] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[FormCustom]( [id] [int] IDENTITY(1,1) NOT NULL, [UserId] [nvarchar](50) NULL, [ShopId] [nvarchar](50) NULL, [CustomControlName] [nvarchar](100) NULL, [ShowColumns] [nvarchar](max) NULL, [HiddenColumns] [nvarchar](max) NULL, [Userfield1] [nvarchar](50) NULL, [Userfield2] [nvarchar](50) NULL, [Userfield3] [nvarchar](50) NULL, [Userfield4] [nvarchar](50) NULL, [Userfield5] [nvarchar](50) NULL, CONSTRAINT [PK_FormCustom] PRIMARY KEY CLUSTERED ( [id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] GO /****** Object: Table [dbo].[hcm03] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[hcm03]( [hcc00c] [varchar](10) NOT NULL, [hcc01c] [varchar](20) NOT NULL, [hcc02c] [varchar](10) NOT NULL, [hcc03c] [varchar](5) NOT NULL, [hcc04c] [varchar](5) NOT NULL, [hcc05c] [varchar](10) NULL, [hcc06f] [float] NULL, [hcc07c] [varchar](5) NULL, [hcc08f] [varchar](5) NULL, [hcc09f] [float] NULL, CONSTRAINT [PK_HCM03] PRIMARY KEY CLUSTERED ( [hcc00c] ASC, [hcc01c] ASC, [hcc02c] ASC, [hcc03c] ASC, [hcc04c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[hem03] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[hem03]( [hec00c] [varchar](10) NOT NULL, [hec01c] [varchar](20) NOT NULL, [hec02f] [float] NOT NULL, [hec03f] [float] NULL, [hec04f] [float] NULL, [hec05i] [tinyint] NULL, [hec06f] [float] NULL, [hec07c] [varchar](20) NULL, [hec08i] [tinyint] NULL, [hec09f] [float] NULL, [hec10c] [varchar](5) NULL, CONSTRAINT [PK_HEM03] PRIMARY KEY CLUSTERED ( [hec00c] ASC, [hec01c] ASC, [hec02f] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[batch_insert_gam07] Script Date: 06/12/2017 06:19:19 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[batch_insert_gam07] ( @compid varchar(10), @strPrefix varchar(20), @strSuffix varchar(20), @nbegin int, @nend int, @nLen int, @strPrjType varchar(10), @strFull varchar(40), @strSml varchar(40), @f_mz float ) as begin declare @strpre varchar(40) declare @n int set @n = 0 set @strpre = '0' while (@n < @nLen) begin set @strpre = @strpre + '0' set @n = @n + 1 end create table #tbl_gam07 ( gag00c varchar(10) not null, --公司编号 gag01c varchar(20) not null, --赠券编号 gag02c varchar(5) not null, --赠券类别 gag03c varchar(30) null, --赠券密码 gag05c varchar(20) null, --简单密码 gag06f float null --面值 ) while @nbegin <= @nend begin insert into #tbl_gam07(gag00c, gag01c, gag02c, gag03c, gag05c, gag06f) select @compid, @strPrefix + right(@strpre + convert(varchar(20), @nbegin), @nLen) + @strSuffix, @strPrjType, @strFull, @strSml, @f_mz set @nbegin = @nbegin + 1 end create clustered index tbl_idx_gam07 on #tbl_gam07(gag01c) insert into gam07(gag00c, gag01c, gag02c, gag03c, gag05c, gag06f) select * from #tbl_gam07 a where not exists(select 1 from gam07 b where a.gag01c = b.gag01c) drop table #tbl_gam07 end GO /****** Object: Table [dbo].[gsm06] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gsm06]( [gsf01c] [varchar](10) NOT NULL, [gsf02c] [varchar](10) NOT NULL, [gsf03c] [varchar](40) NULL, [gsf04c] [varchar](40) NOT NULL, [gsf05i] [int] NULL, [gsf06c] [varchar](10) NULL, CONSTRAINT [PK_GSM06] PRIMARY KEY CLUSTERED ( [gsf01c] ASC, [gsf02c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_wait_queue_of_open_bill] Script Date: 06/12/2017 06:20:52 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_wait_queue_of_open_bill] ( @compid varchar(10), @datefrom varchar(20), -- 开始时间 20130522000000 @dateto varchar(20) -- 结束时间 20130523245959 ) as -- 员工对应等候客户开单信息查询 begin create table #waitqueue --员工等候排序 ( compid varchar(20) not null, billid varchar(20) not null, --开单单号 person varchar(20) not null, --开单人 flagid int not null, --标识序号 datetime varchar(20) null, --开单日期 memtype int null, --开单类型 0-会员;1-散客 memcard varchar(20) null, --开单类型为会员,则填会员卡号 billflag int null, --标志列 0-开单 1-已经结账(不可修改了) remark varchar(1000) null, --开单备注信息,方便快速查询(以前用的) keyid bigint null, --开单钥匙牌号 gender int null, --性别 0:女 1:男 ranking int null, --排名 project varchar(20) null, --项目 empid varchar(20) null, --员工编号 empname varchar(20) null, --员工姓名 idd int identity not null, ) declare @PK_Sql nvarchar(max) set @PK_Sql = ' alter table #waitqueue add constraint PK_#waitqueue_' + convert(varchar(10), cast(floor(rand() * 1000000) as int)) + ' primary key (idd) ' exec sp_executesql @PK_Sql create table #empinfo --需要展示的员工 ( haa00c varchar(10) null, --公司编号 haa01c varchar(20) null, --员工编号 haa02c varchar(20) null, --员工姓名 idd int identity not null, ) set @PK_Sql = ' alter table #empinfo add constraint PK_#empinfo_' + convert(varchar(10), cast(floor(rand() * 1000000) as int)) + ' primary key (idd) ' exec sp_executesql @PK_Sql create table #emprank -- 明细排序 ( ranking int identity not null, billid varchar(20) null, --开单单号 flagid int not null, --标识序号 idd int null, --序号 datetime varchar(20) null, --开单日期 empid varchar(20) null --员工编号 ) -- 插入要显示的员工 insert #empinfo(haa00c,haa01c,haa02c) select haa00c,haa01c,haa02c from ham01 with(nolock) where haa00c=@compid and haa27c='2' and haa47i=1 -- 插入大工 insert #waitqueue(compid,billid,flagid,person,datetime,memtype,memcard,billflag,remark,keyid,gender,project,empid,empname ) select a.compid,a.billid,flagid,person,datetime,memtype,memcard,a.billflag,remark,keyid,gender,project,b.empa,c.haa02c from openpolicy_master a with(nolock) ,openpolicy_detail b with(nolock),#empinfo c where a.compid=b.compid and a.billid=b.billid and b.compid=c.haa00c and b.empa=c.haa01c and datetime <= @dateto and datetime >= @datefrom and a.compid = @compid and a.billflag in(0,3) -- 插入中工 insert #waitqueue(compid,billid,flagid,person,datetime,memtype,memcard,billflag,remark,keyid,gender,project,empid,empname ) select a.compid,a.billid,flagid,person,datetime,memtype,memcard,a.billflag,remark,keyid,gender,project,b.empb,c.haa02c from openpolicy_master a with(nolock) ,openpolicy_detail b with(nolock),#empinfo c where a.compid=b.compid and a.billid=b.billid and b.compid=c.haa00c and b.empb=c.haa01c and datetime <=@dateto and datetime >=@datefrom and a.compid = @compid and a.billflag in(0,3) -- 插入小工 insert #waitqueue(compid,billid,flagid,person,datetime,memtype,memcard,billflag,remark,keyid,gender,project,empid,empname ) select a.compid,a.billid,flagid,person,datetime,memtype,memcard,a.billflag,remark,keyid,gender,project,b.empc,c.haa02c from openpolicy_master a with(nolock) ,openpolicy_detail b with(nolock),#empinfo c where a.compid=b.compid and a.billid=b.billid and b.compid=c.haa00c and b.empc=c.haa01c and datetime <= @dateto and datetime >= @datefrom and a.compid = @compid and a.billflag in(0,3) -- 插入四工 insert #waitqueue(compid,billid,flagid,person,datetime,memtype,memcard,billflag,remark,keyid,gender,project,empid,empname ) select a.compid,a.billid,flagid,person,datetime,memtype,memcard,a.billflag,remark,keyid,gender,project,b.empd,c.haa02c from openpolicy_master a with(nolock) ,openpolicy_detail b with(nolock),#empinfo c where a.compid=b.compid and a.billid=b.billid and b.compid=c.haa00c and b.empd=c.haa01c and datetime <= @dateto and datetime >= @datefrom and a.compid = @compid and a.billflag in(0,3) --处理每个员工的客户顺序 declare @empid varchar(20) declare cur_get_each_emp cursor for select haa01c from #empinfo open cur_get_each_emp fetch cur_get_each_emp into @empid while @@fetch_status=0 begin truncate table #emprank insert #emprank(billid,flagid,idd,datetime,empid) select billid,flagid,idd,datetime,empid from #waitqueue where empid=@empid order by datetime,flagid update a set a.ranking = b.ranking from #waitqueue a, #emprank b where a.billid=b.billid and a.datetime=b.datetime and a.empid=@empid and a.flagid=b.flagid and a.idd=b.idd fetch cur_get_each_emp into @empid end close cur_get_each_emp deallocate cur_get_each_emp select * from #waitqueue order by empid,datetime,flagid drop table #waitqueue drop table #empinfo drop table #emprank end GO /****** Object: Table [dbo].[gam23] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gam23]( [gau00c] [varchar](10) NOT NULL, [gau01c] [varchar](5) NOT NULL, [gau02i] [int] IDENTITY(1,1) NOT NULL, [gau03c] [varchar](20) NULL, [gau04c] [varchar](20) NULL, [gau05d] [varchar](8) NULL, [gau06f] [float] NULL, [gau07c] [varchar](20) NULL, [gau08c] [varchar](20) NULL, [gau09c] [varchar](160) NULL, [gau10t] [varchar](6) NULL, [gau11c] [varchar](30) NULL, [gau12c] [varchar](30) NULL, [gau13c] [varchar](30) NULL, [gau14c] [varchar](128) NULL, [gau15c] [varchar](10) NULL, [gau16d] [varchar](8) NULL, [gau17i] [int] NULL, [gau17c] [varchar](80) NULL, [gau18c] [varchar](10) NULL, CONSTRAINT [PK_GAM23] PRIMARY KEY CLUSTERED ( [gau00c] ASC, [gau01c] ASC, [gau02i] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: UserDefinedFunction [dbo].[fun_getFirstPY] Script Date: 06/12/2017 06:20:57 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create function [dbo].[fun_getFirstPY](@word nvarchar(1)) returns nvarchar(1) as begin declare @code int set @code = unicode(@word) if @code < 19968 or @code > 40869 return '' if @word <= N'驁' collate Chinese_PRC_CS_as_KS_WS return 'A' if @word <= N'簿' collate Chinese_PRC_CS_as_KS_WS return 'B' if @word <= N'錯' collate Chinese_PRC_CS_as_KS_WS return 'C' if @word <= N'鵽' collate Chinese_PRC_CS_as_KS_WS return 'D' if @word <= N'樲' collate Chinese_PRC_CS_as_KS_WS return 'E' if @word <= N'鰒' collate Chinese_PRC_CS_as_KS_WS return 'F' if @word <= N'腂' collate Chinese_PRC_CS_as_KS_WS return 'G' if @word <= N'夻' collate Chinese_PRC_CS_as_KS_WS return 'H' if @word <= N'攈' collate Chinese_PRC_CS_as_KS_WS return 'J' if @word <= N'穒' collate Chinese_PRC_CS_as_KS_WS return 'K' if @word <= N'鱳' collate Chinese_PRC_CS_as_KS_WS return 'L' if @word <= N'旀' collate Chinese_PRC_CS_as_KS_WS return 'M' if @word <= N'桛' collate Chinese_PRC_CS_as_KS_WS return 'N' if @word <= N'漚' collate Chinese_PRC_CS_as_KS_WS return 'O' if @word <= N'曝' collate Chinese_PRC_CS_as_KS_WS return 'P' if @word <= N'囕' collate Chinese_PRC_CS_as_KS_WS return 'Q' if @word <= N'鶸' collate Chinese_PRC_CS_as_KS_WS return 'R' if @word <= N'蜶' collate Chinese_PRC_CS_as_KS_WS return 'S' if @word <= N'籜' collate Chinese_PRC_CS_as_KS_WS return 'T' if @word <= N'鶩' collate Chinese_PRC_CS_as_KS_WS return 'W' if @word <= N'鑂' collate Chinese_PRC_CS_as_KS_WS return 'X' if @word <= N'韻' collate Chinese_PRC_CS_as_KS_WS return 'Y' if @word <= N'咗' collate Chinese_PRC_CS_as_KS_WS return 'Z' return '' end GO /****** Object: Table [dbo].[gdm20] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gdm20]( [gdt00c] [varchar](10) NOT NULL, [gdt01c] [varchar](20) NOT NULL, [gdt02c] [varchar](40) NULL, [gdt03i] [int] NULL, [gdt04d] [varchar](10) NULL, [gdt05d] [varchar](10) NULL, [gdt06i] [int] NULL, [gdt91c] [varchar](10) NULL, [gdt92d] [varchar](10) NULL, [gdt93c] [varchar](10) NULL, [gdt94d] [varchar](10) NULL, CONSTRAINT [PK_GDM20] PRIMARY KEY NONCLUSTERED ( [gdt00c] ASC, [gdt01c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_analyzer_customer_order] Script Date: 06/12/2017 06:19:27 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_analyzer_customer_order] ( @compid varchar(10), @class varchar(10), @frommemid varchar(20), @tomemid varchar(20), @fromdate varchar(20), @todate varchar(20), @state varchar(200), -- 会员卡状态 @acon_amt float, -- 大于消费金额 @alastdate varchar(8), -- 某个日期之后 @at_times float, -- 大于消费次数 @aav_amt float, -- 大于平均消费金额 @aless_remain float, -- 小于某个储值余额 @abig_remain float, -- 大于某个储值余额 @addtoSMS int, @ordertype int ) as begin create table #mem_result_2 ( idd int not null, compid varchar(10), memid varchar(20) not null, -- Member编号 m_name varchar(40) null, -- 姓名 cardid varchar(20) null, -- 卡号 gender int null, -- 性别 0:女 mobile varchar(20) null, -- 手机 tel varchar(20) null, -- 电话 state varchar(10) null, -- Member卡状态 con_amt float null, -- Consumption金额 lastdate varchar(8) null, -- 最后Consumption日期 lastcall varchar(8) null, -- 最后回访日期 t_times float null, -- 用于比较的Consumption次数 t_compare float null, -- 总共来的次数 av_ytimes float null, -- 每年来的次数 av_times float null, -- 每月来的次数 av_wtimes float null, -- 每周来的次数 months float null, -- 客人来了多少个月了,客龄 av_amt float null, -- 每次Consumption的平均金额 start_date varchar(20) null, -- 开始日期 first_date varchar(20) null, -- 第一次来店日期 comp_date varchar(20) null, -- 开始比较的日期 dur_years float null, -- 统计期间来了多少个年了 dur_months float null, -- 统计期间来了多少个月了 dur_weeks float null, -- 统计期间来了多少个周了 remain01 float null, -- 余额 remain02 float null, -- 余额 remain03 float null, -- 余额 remain04 float null, -- 余额 remain05 float null, -- 余额 total_fill_amt float null, -- 累计Charge金额 memaddr varchar(160) null, -- 会员地址 postcode varchar(20) null, -- 邮编 principle varchar(20) null, -- 负责人 total_salecard_amt float null, -- 累计卡销售金额 primary key(idd) ) insert #mem_result_2(idd, compid, memid, m_name, cardid, gender, mobile, tel, state, con_amt, lastdate, lastcall, t_times, t_compare, av_ytimes, av_times, av_wtimes, months, av_amt, start_date, first_date, comp_date, dur_years, dur_months, dur_weeks, remain01, remain02, remain03, remain04, remain05, total_fill_amt, memaddr, postcode, principle, total_salecard_amt) exec upg_analyzer_customer_ex @compid, @class, @frommemid, @tomemid, @fromdate, @todate, @fromdate, @todate, @state, @acon_amt, @alastdate, @at_times, 0, @aav_amt, @aless_remain, @abig_remain, @addtoSMS, 3, 0 select idd, memid, m_name, cardid, gender, mobile, tel, state, con_amt, lastdate, lastcall, t_times, av_times, months, av_amt, start_date, first_date, dur_months, remain01, remain02, remain03, remain04, remain05, total_fill_amt, memaddr, postcode, principle, total_salecard_amt from #mem_result_2 order by memid drop table #mem_result_2 end GO /****** Object: StoredProcedure [dbo].[upg_query_ggpic_list1] Script Date: 06/12/2017 06:20:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_query_ggpic_list1] ( @compid varchar(10), --登陆公司id @topshow int, -- 表示前几条数据 @show int, -- 查询首页大图,0查询所有不包含topshow,1只查显示的,2包括无图的列表 @stype varchar(1) -- *所有;0,产品;1,项目;2,会员卡;3,员工;4,门店介绍;5,抵用券; ) as begin create table #tbl_data_tem( id int identity not null, compid varchar(20) not null, wpid varchar(20) not null, -- 产品、项目、员工、会员卡编号 pname varchar(60) null, --名称 punit varchar(500) null, --销售单位 pprice float null, --外购价格 pclass varchar(20) null, --统计分类 picid bigint null, -- 对应图片id pstatus varchar(10) null, -- 图片状态,0,首页大图不显示,1,首页大图显示,2,小图首页显示,3,小图普通 pstype varchar(10) null, -- 图片类型:0,产品;1,项目;2,会员卡;3,员工;4,门店介绍;5,抵用券; primary key(id) ) --防止项目与产品等的编号有重复,而导致错误的更新图片编号, create table #tbl_data_tem1( compid varchar(20) not null, wpid varchar(20) not null, -- 产品、项目、员工、会员卡编号 pname varchar(60) null, --名称 punit varchar(500) null, --销售单位 pprice float null, --外购价格 pclass varchar(20) null, --统计分类 picid bigint null, -- 对应图片id pstatus varchar(10) null, pstype varchar(10) null, primary key(compid,wpid) ) -- 产品 if(@stype='*' or @stype='0') begin -- 查出所有该门店的 insert #tbl_data_tem1(compid,wpid,pname,punit,pprice,pclass,pstype) select f.gfa00c as compid,f.gfa01c as wpid,f.gfa03c as pname,f.gfa07c as punit,f.gfa71f as pprice,f.gfa24c as pclass,'0' as pstype from gfm01 f with (nolock) where f.gfa00c=@compid and f.gfa70i=1--是否允许外购 and isnull(f.gfa10c,'0')='0' and (isnull(f.gfa47c,'0')='0' or f.gfa47c='')--是否停用 --更新本门店的图片id update a set a.picid=(select top 1 isnull(p.gff04c,0) from gfm06p p with (nolock) where wpid=p.gff01c and compid=p.gff00c and datalength(p.gff02p)>20 and p.gff00c=@compid and (p.gff03c=1 or p.gff03c=0) order by p.gff04c desc) from #tbl_data_tem1 a --没有图片的,从001门店更新 update a set a.picid=(select top 1 isnull(p.gff04c,0) from gfm06p p with (nolock) where wpid=p.gff01c and datalength(p.gff02p)>20 and p.gff00c='001' and (p.gff03c=1 or p.gff03c=0) order by p.gff04c desc) from #tbl_data_tem1 a where isnull(picid,0)=0 if(@show<>2) begin --删除没有图片的 delete from #tbl_data_tem1 where isnull(picid,0)=0 end --更新图片状态 update a set a.pstatus=(select p.gff03c from gfm06p p with (nolock) where p.gff04c=picid) from #tbl_data_tem1 a insert #tbl_data_tem select * from #tbl_data_tem1 delete from #tbl_data_tem1 end -- 项目 if(@stype='*' or @stype='1') begin -- 查出所有该门店的项目 insert #tbl_data_tem1(compid,wpid,pname,punit,pprice,pclass,pstype) select d.gda00c as compid,d.gda01c as wpid,d.gda03c as pname,d.gda09c as punit,d.gda10f as pprice,d.gda13c as pclass,'1' as pstype from gdm01 d with (nolock) where d.gda00c=@compid --更新本门店的图片id update a set a.picid=(select top 1 isnull(p.gdc04c,0) from gdm03p p with (nolock) where wpid=p.gdc01c and compid=p.gdc00c and datalength(p.gdc02p)>20 and p.gdc00c=@compid and (p.gdc03c=1 or p.gdc03c=0) order by p.gdc04c desc) from #tbl_data_tem1 a --没有图片的,从001门店更新 update a set a.picid=(select top 1 isnull(p.gdc04c,0) from gdm03p p with (nolock) where wpid=p.gdc01c and datalength(p.gdc02p)>20 and p.gdc00c='001' and (p.gdc03c=1 or p.gdc03c=0) order by p.gdc04c desc) from #tbl_data_tem1 a where isnull(picid,0)=0 if(@show<>2) begin --删除没有图片的 delete from #tbl_data_tem1 where isnull(picid,0)=0 end --更新图片状态 update a set a.pstatus=(select p.gdc03c from gdm03p p with (nolock) where p.gdc04c=picid) from #tbl_data_tem1 a insert #tbl_data_tem select * from #tbl_data_tem1 delete from #tbl_data_tem1 end -- 抵用券 if(@stype='*' or @stype='5') begin -- 查出所有该门店的 insert #tbl_data_tem1(compid,wpid,pname,punit,pprice,pclass,pstype) select d.gah00c as compid,d.gah02c as wpid,'' as pname,'' as punit,d.gah12f as pprice,d.gah01c as pclass,'5' as pstype from gam08 d with (nolock) where d.gah00c=@compid --更新本门店的图片id update a set a.picid=(select top 1 isnull(p.gah04c,0) from gam08p p with (nolock) where wpid=p.gah02c and compid=p.gah00c and datalength(p.gah02p)>20 and p.gah00c=@compid and (p.gah03c=1 or p.gah03c=0) order by p.gah04c desc) from #tbl_data_tem1 a --没有图片的,从001门店更新 update a set a.picid=(select top 1 isnull(p.gah04c,0) from gam08p p with (nolock) where wpid=p.gah02c and datalength(p.gah02p)>20 and p.gah00c='001' and (p.gah03c=1 or p.gah03c=0) order by p.gah04c desc) from #tbl_data_tem1 a where isnull(picid,0)=0 if(@show<>2) begin --删除没有图片的 delete from #tbl_data_tem1 where isnull(picid,0)=0 end --更新图片状态 update a set a.pstatus=(select p.gah03c from gam08p p with (nolock) where p.gah04c=picid) from #tbl_data_tem1 a insert #tbl_data_tem select * from #tbl_data_tem1 end if(@show=1) begin Exec ('select top ' + @topshow + ' compid,wpid,pname,punit,pprice,pclass,picid,pstatus,pstype from #tbl_data_tem where pstatus=1') end else begin select compid,wpid,pname,punit,pprice,pclass,picid,pstatus,pstype from #tbl_data_tem end drop table #tbl_data_tem1 drop table #tbl_data_tem end GO /****** Object: Table [dbo].[gam27] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gam27]( [compid] [varchar](10) NOT NULL, [cardtype] [varchar](10) NOT NULL, [descompid] [varchar](10) NOT NULL, [ispermit] [int] NULL, CONSTRAINT [PK_gam27] PRIMARY KEY CLUSTERED ( [compid] ASC, [cardtype] ASC, [descompid] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[personprecept] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[personprecept]( [ppt00c] [varchar](10) NOT NULL, [ppt01i] [int] IDENTITY(1,1) NOT NULL, [ppt02c] [varchar](20) NOT NULL, [ppt03c] [varchar](20) NOT NULL, [ppt04i] [int] NOT NULL, [ppt05d] [datetime] NOT NULL, [ppt05c] [varchar](100) NULL, [ppt06c] [varchar](100) NULL, [ppt07c] [varchar](100) NULL, [ppt08c] [varchar](100) NULL, [ppt09c] [varchar](100) NULL, [ppt10c] [varchar](100) NULL, [ppt11i] [int] NOT NULL, CONSTRAINT [PK_PERSONPRECEPT] PRIMARY KEY CLUSTERED ( [ppt00c] ASC, [ppt01i] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gsm10] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gsm10]( [gsj00c] [varchar](10) NOT NULL, [gsj01f] [float] NOT NULL, [gsj02f] [float] NOT NULL, [gsj03f] [float] NULL, [gsj04f] [float] NULL, CONSTRAINT [PK_GSM10] PRIMARY KEY CLUSTERED ( [gsj00c] ASC, [gsj01f] ASC, [gsj02f] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gdn01] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gdn01]( [gdn00c] [varchar](10) NOT NULL, [gdn01c] [varchar](20) NOT NULL, [gdn02c] [varchar](20) NOT NULL, CONSTRAINT [PK_GDN01] PRIMARY KEY CLUSTERED ( [gdn00c] ASC, [gdn01c] ASC, [gdn02c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_analyzer_customer_order_regdate] Script Date: 06/12/2017 06:19:27 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_analyzer_customer_order_regdate] ( @compid varchar(10), @class varchar(10), @memfromdate varchar(10), @memtodate varchar(10), @frommemid varchar(20), @tomemid varchar(20), @fromdate varchar(20), @todate varchar(20), @state varchar(200), -- 会员卡状态 @acon_amt float, -- 大于消费金额 @alastdate varchar(8), -- 某个日期之后 @at_times float, -- 大于消费次数 @aav_amt float, -- 大于平均消费金额 @aless_remain float, -- 小于某个储值余额 @abig_remain float, -- 大于某个储值余额 @addtoSMS int, @ordertype int, @only_card_pay int ) as begin create table #mem_result ( idd int not null, compid varchar(10), memid varchar(20) not null, -- Member编号 m_name varchar(40) null, -- 姓名 cardid varchar(20) null, -- 卡号 gender int null, -- 性别 0:女 mobile varchar(20) null, -- 手机 tel varchar(20) null, -- 电话 birthday varchar(8) null, -- 会员生日 state varchar(10) null, -- Member卡状态 con_amt float null, -- Consumption金额 lastdate varchar(8) null, -- 最后Consumption日期 lastcall varchar(8) null, -- 最后回访日期 t_times float null, -- 用于比较的Consumption次数 t_compare float null, -- 总共来的次数 av_ytimes float null, -- 每年来的次数 av_times float null, -- 每月来的次数 av_wtimes float null, -- 每周来的次数 months float null, -- 客人来了多少个月了,客龄 av_amt float null, -- 每次Consumption的平均金额 start_date varchar(20) null, -- 开始日期 first_date varchar(20) null, -- 第一次来店日期 comp_date varchar(20) null, -- 开始比较的日期 dur_years float null, -- 统计期间来了多少个年了 dur_months float null, -- 统计期间来了多少个月了 dur_weeks float null, -- 统计期间来了多少个周了 remain01 float null, -- 余额 remain02 float null, -- 余额 remain03 float null, -- 余额 remain04 float null, -- 余额 remain05 float null, -- 余额 total_fill_amt float null, -- 累计Charge金额 memaddr varchar(160) null, -- 会员地址 postcode varchar(20) null, -- 邮编 principle varchar(20) null, -- 负责人 total_salecard_amt float null, -- 累计卡销售金额 regdate varchar(20) null, -- 登记日期 memlevel varchar(300) null, --会员等级 curmonth_amt float null, --本月消费 premonth_amt float null, --上月消费 primary key(idd) ) insert #mem_result(idd, compid, memid, m_name, cardid, gender, mobile, tel, birthday, state, con_amt, lastdate, lastcall, t_times, t_compare, av_ytimes, av_times, av_wtimes, months, av_amt, start_date, first_date, comp_date, dur_years, dur_months, dur_weeks, remain01, remain02, remain03, remain04, remain05, total_fill_amt, memaddr, postcode, principle, total_salecard_amt) exec upg_analyzer_customer_ex @compid, @class, @frommemid, @tomemid, @memfromdate, @memtodate, @fromdate, @todate, @state, @acon_amt, @alastdate, @at_times, 0, @aav_amt, @aless_remain, @abig_remain, @addtoSMS, 3, @only_card_pay --会员资料统计表,需要 create table #gbm18 ( compid varchar(10) not null, memid varchar(30) not null, levelid int null, leveltext varchar(300) ) insert into #gbm18(compid,memid,levelid,leveltext) select a.gbr00c,a.gbr01c,b.gsb02c,b.gsb03c from gbm18 a,(select gsb00c,gsb01c,gsb02c,gsb03c from gsm02 where gsb00c = @compid and gsb01c = 'DJ' ) b where a.gbr00c = b.gsb00c and a.gbr04i = b.gsb02c and a.gbr00c = @compid create table #member_level ( compid varchar(10) not null, memid varchar(30) not null, leveltext varchar(300) ) insert into #member_level(compid,memid,leveltext) select compid,memId,leveltext = stuff((select ',' + leveltext from #gbm18 b where a.compid = b.compid and a.memId = b.memId for xml path('')),1,1,'') from #gbm18 a group by compid,memId update a set a.memlevel = b.leveltext from #mem_result a,#member_level b where a.compid = b.compid and a.memid = b.memid declare @startdate_currmonth varchar(10),@enddate_currmonth varchar(10), @startdate_premonth varchar(10),@enddate_premonth varchar(10) select @startdate_currmonth = convert(varchar(10),DATEADD(month,-1,GETDATE()-DATEPART(day,GETDATE())+1),112) select @enddate_currmonth = convert(varchar(10),DATEADD(day,-1,GETDATE()-DATEPART(day,GETDATE())+1),112) select @startdate_premonth = CONVERT(varchar(10),GETDATE()-DATEPART(day,GETDATE())+1,112) select @enddate_premonth = convert(varchar(10),DATEADD(day,-1,DATEADD(month,1,GETDATE()-DATEPART(day,GETDATE())+1)),112) create table #member_consume ( compid varchar(10) not null, memid varchar(30) not null, currmonth_amt float null, premonth_amt float null ) insert into #member_consume(compid,memid,currmonth_amt) select gct00c,gct03c,sum(gct12f) from gcm20 where gct00c = @compid and gct04d >= @startdate_currmonth and gct04d <= @enddate_currmonth and gct06c in('3','4') group by gct00c,gct03c insert into #member_consume(compid,memid,premonth_amt) select gct00c,gct03c,sum(gct12f) from gcm20 where gct00c = @compid and gct04d >= @startdate_premonth and gct04d <= @enddate_premonth and gct06c in('3','4') group by gct00c,gct03c update a set a.curmonth_amt = b.currmonth_amt,a.premonth_amt = b.premonth_amt from #mem_result a,(select compid,memid,sum(currmonth_amt) currmonth_amt,sum(premonth_amt) premonth_amt from #member_consume group by compid,memid) b where a.compid = b.compid and a.memid = b.memid select idd, compid, memid, m_name, cardid, gender, mobile, tel, birthday, state, con_amt, lastdate, lastcall, t_times, av_times, months, av_amt, start_date, first_date, dur_months, remain01, remain02, remain03, remain04, remain05, total_fill_amt, memaddr, postcode, principle, total_salecard_amt,memlevel,curmonth_amt,premonth_amt from #mem_result a order by memid drop table #member_consume drop table #member_level drop table #gbm18 drop table #mem_result end GO /****** Object: Table [dbo].[pay_classify] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[pay_classify]( [comp] [varchar](10) NOT NULL, [pay] [varchar](5) NOT NULL, [category] [varchar](5) NULL, PRIMARY KEY CLUSTERED ( [comp] ASC, [pay] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_query_diff_list] Script Date: 06/12/2017 06:20:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_query_diff_list] ( @compid varchar(10), -- 登陆公司id @stype int, -- 获取类型,0,产品;1,项目;2,会员卡;3,员工;4,门店介绍;5,抵用券; @classid varchar(10), -- 类别 @tophow int, -- 表示前几条数据,0表示不限制条数 @killnopic int -- 只显示有图的,0否1是,=2只显示首页的广告图,不等于2表示显示首页小图和普通图,应首页小图和普通图是通用的 ) as begin create table #tbl_data_tem( compid varchar(20) not null, wpid varchar(20) not null, -- 产品、项目、员工、会员卡编号 pname varchar(60) null, --名称 punit varchar(500) null, --销售单位 pprice float null, --外购价格 pclass varchar(40) null, --统计分类 picid bigint null, -- 对应图片id pstatus varchar(2) null,--//图片状态,0,首页大图不显示,1,首页大图显示,2,小图首页显示,3,小图普通 pstype varchar(2) null,--//图片类型:0,产品;1,项目;2,会员卡;3,员工;4,门店介绍;5,抵用券;6,广告图; primary key(compid,wpid) ) --产品 if(@stype=0) begin if(@classid='' or @classid='*') begin insert #tbl_data_tem(compid,wpid,pname,punit,pprice,pclass) select f.gfa00c as compid,f.gfa01c as wpid,f.gfa03c as pname,f.gfa07c as punit,f.gfa71f as pprice,f.gfa24c as pclass from gfm01 f with (nolock) where f.gfa00c=@compid and f.gfa70i=1--是否允许外购 and isnull(f.gfa10c,'0')='0' and (isnull(f.gfa47c,'0')='0' or f.gfa47c='')--是否停用 end else begin insert #tbl_data_tem(compid,wpid,pname,punit,pprice,pclass) select f.gfa00c as compid,f.gfa01c as wpid,f.gfa03c as pname,f.gfa07c as punit,f.gfa71f as pprice,f.gfa24c as pclass from gfm01 f with (nolock) where f.gfa00c=@compid and f.gfa70i=1--是否允许外购 and isnull(f.gfa10c,'0')='0' and (isnull(f.gfa47c,'0')='0' or f.gfa47c='')--是否停用 and f.gfa24c=@classid end --更新本门店的图片id update a set a.picid=(select top 1 isnull(p.gff04c,0) from gfm06p p with (nolock) where wpid=p.gff01c and compid=p.gff00c and datalength(p.gff02p)>20 and p.gff00c=@compid and (isnull(p.gff03c,3)=3 or isnull(p.gff03c,3)=2) order by p.gff04c desc) from #tbl_data_tem a --没有图片的,从001门店更新 update a set a.picid=(select top 1 isnull(p.gff04c,0) from gfm06p p with (nolock) where wpid=p.gff01c and datalength(p.gff02p)>20 and p.gff00c='001' and (isnull(p.gff03c,3)=3 or isnull(p.gff03c,3)=2) order by p.gff04c desc) from #tbl_data_tem a where isnull(picid,0)=0 --更新类别成中文名字 update a set a.pclass=(select g.gsb03c from gsm02 g with (nolock) where g.gsb02c=pclass and g.gsb00c=compid and g.gsb01c='AL') from #tbl_data_tem a --更新图片状态 update a set a.pstatus=(select isnull(p.gff03c,3) from gfm06p p with (nolock) where p.gff04c=picid) from #tbl_data_tem a end --项目 if(@stype=1) begin if(@classid='' or @classid='*') begin insert #tbl_data_tem(compid,wpid,pname,punit,pprice,pclass) select d.gda00c as compid,d.gda01c as wpid,d.gda03c as pname,d.gda09c as punit,d.gda10f as pprice,d.gda13c as pclass from gdm01 d with (nolock) where d.gda00c=@compid and (isnull(d.gda14i,1)=1 or d.gda14i=1) --2停用,1正常 and (isnull(d.gda38i,1)=1 or d.gda38i=1) --需要在终端中显示,0 不显示,1显示 end else begin insert #tbl_data_tem(compid,wpid,pname,punit,pprice,pclass) select d.gda00c as compid,d.gda01c as wpid,d.gda03c as pname,d.gda09c as punit,d.gda10f as pprice,d.gda13c as pclass from gdm01 d with (nolock) where d.gda00c=@compid and d.gda13c=@classid and (isnull(d.gda14i,1)=1 or d.gda14i=1) --2停用,1正常 and (isnull(d.gda38i,1)=1 or d.gda38i=1) --需要在终端中显示,0 不显示,1显示 end --更新本门店的图片id update a set a.picid=(select top 1 isnull(p.gdc04c,0) from gdm03p p with (nolock) where wpid=p.gdc01c and compid=p.gdc00c and datalength(p.gdc02p)>20 and p.gdc00c=@compid and (isnull(p.gdc03c,3)=3 or isnull(p.gdc03c,3)=2) order by p.gdc04c desc) from #tbl_data_tem a --没有图片的,从001门店更新 update a set a.picid=(select top 1 isnull(p.gdc04c,0) from gdm03p p with (nolock) where wpid=p.gdc01c and datalength(p.gdc02p)>20 and p.gdc00c='001' and (isnull(p.gdc03c,3)=3 or isnull(p.gdc03c,3)=2) order by p.gdc04c desc) from #tbl_data_tem a where isnull(picid,0)=0 --更新类别成中文名字 update a set a.pclass=(select g.gsb03c from gsm02 g with (nolock) where g.gsb02c=pclass and g.gsb00c=compid and g.gsb01c='AJ') from #tbl_data_tem a --更新图片状态 update a set a.pstatus=(select isnull(p.gdc03c,3) from gdm03p p with (nolock) where p.gdc04c=picid) from #tbl_data_tem a end --会员卡 if(@stype=2) begin insert #tbl_data_tem(compid,wpid,pname,punit,pprice,pclass) select d.gak00c as compid,d.gak01c as wpid,d.gak02c as pname,d.gak09f as punit,d.gak16f as pprice,d.gak03i as pclass from gam10 d with (nolock) where d.gak00c=@compid --更新本门店的图片id update a set a.picid=(select top 1 isnull(p.gak04c,0) from gam10p p with (nolock) where wpid=p.gak01c and compid=p.gak00c and datalength(p.gak02p)>20 and p.gak00c=@compid and (isnull(p.gak03c,3)=3 or isnull(p.gak03c,3)=2) order by p.gak04c desc) from #tbl_data_tem a --没有图片的,从001门店更新 update a set a.picid=(select top 1 isnull(p.gak04c,0) from gam10p p with (nolock) where wpid=p.gak01c and datalength(p.gak02p)>20 and p.gak00c='001' and (isnull(p.gak03c,3)=3 or isnull(p.gak03c,3)=2) order by p.gak04c desc) from #tbl_data_tem a where isnull(picid,0)=0 end --员工头像 if(@stype=3) begin if(@classid='' or @classid='*') begin insert #tbl_data_tem(compid,wpid,pname,punit,pprice,pclass) select d.haa00c as compid,d.haa01c as wpid,d.haa02c as pname,d.haa99c as punit,'0' as pprice,d.haa25c as pclass from ham01 d with (nolock) where d.haa00c=@compid end else begin insert #tbl_data_tem(compid,wpid,pname,punit,pprice,pclass) select d.haa00c as compid,d.haa01c as wpid,d.haa02c as pname,d.haa99c as punit,'0' as pprice,d.haa25c as pclass from ham01 d with (nolock) where d.haa00c=@compid and d.haa25c=@classid end --更新本门店的图片id update a set a.picid=(select top 1 isnull(p.haj04c,0) from ham10p p with (nolock) where wpid=p.haj01c and compid=p.haj00c and datalength(p.haj02p)>20 and p.haj00c=@compid order by p.haj04c desc) from #tbl_data_tem a --更新类别成中文名字 update a set a.pclass=(select g.gsb03c from gsm02 g with (nolock) where g.gsb02c=pclass and g.gsb00c=compid and g.gsb01c='R') from #tbl_data_tem a end --门店介绍 if(@stype=4) begin --插入本门店的5副图片 insert #tbl_data_tem(compid,wpid,pname,punit,pprice,pclass,picid) select gae00cp as compid,gae01cp as wpid,gae02cp as pname, punit=(case gae01cp when '0' then (select gsd05c= dbo.fun_combine_gsm04_gsd05c(gsd00c,gsd01c,gsd02c,gsd03c) from gsm04 where gsd00c=@compid and gsd02c='ga' and gsd03c='6' group by gsd00c,gsd01c,gsd02c,gsd03c) else gae03cp end), '0' as pprice,'' as pclass,gae05cp as picid from gam05p with (nolock) where gae00cp=@compid and gae01cp in (0,1,2,3,4) --不足5副从001插入 insert #tbl_data_tem(compid,wpid,pname,punit,pprice,pclass,picid) select @compid as compid,gae01cp as wpid,gae02cp as pname,'' as punit,'0' as pprice,'' as pclass,gae05cp as picid from gam05p with (nolock) where gae00cp='001' and gae01cp<>5 and gae01cp not in (select wpid from #tbl_data_tem) --没有图片的,从001门店更新 update a set a.picid=(select top 1 isnull(p.gae05cp,0) from gam05p p with (nolock) where wpid=p.gae01cp and datalength(p.gae04p)>20 and p.gae00cp='001' order by p.gae01cp asc) from #tbl_data_tem a where isnull(picid,0)=0 end --品牌展示图 if(@stype=7) begin create table #tbl_data_tem1( compid varchar(20) not null, wpid varchar(20) not null, -- 产品、项目、员工、会员卡编号 pname varchar(60) null, --名称 punit varchar(500) null, --销售单位 pprice float null, --外购价格 pclass varchar(40) null, --统计分类 picid bigint not null, -- 对应图片id pstatus varchar(2) null,--//图片状态,0,首页大图不显示,1,首页大图显示,2,小图首页显示,3,小图普通 pstype varchar(2) null,--//图片类型:0,产品;1,项目;2,会员卡;3,员工;4,门店介绍;5,抵用券;6,广告图; primary key(picid) ) insert #tbl_data_tem1(compid,wpid,pname,punit,pprice,pclass,picid,pstatus,pstype) select gae00cp as compid,gae01cp as wpid,gae02cp as pname,'' as punit,'0' as pprice,'' as pclass,gae05cp as picid,isnull(gae06cp,0) as pstatus,'4' as pstype from gam05p with (nolock) where gae01cp=5 and datalength(gae04p)>20 and (isnull(gae06cp,0)=0 or isnull(gae06cp,0)=1) --gae00cp=@compid and --暂时不要门店限制 end --抵用券 if(@stype=5) begin insert #tbl_data_tem(compid,wpid,pname,punit,pprice,pclass) select d.gah00c as compid,d.gah02c as wpid,'' as pname,'' as punit,d.gah12f as pprice,d.gah01c as pclass from gam08 d with (nolock) where d.gah00c=@compid --更新本门店的图片id update a set a.picid=(select top 1 isnull(p.gah04c,0) from gam08p p with (nolock) where wpid=p.gah02c and compid=p.gah00c and datalength(p.gah02p)>20 and p.gah00c=@compid and (isnull(p.gah03c,3)=3 or p.gah03c=2) order by p.gah04c desc) from #tbl_data_tem a --没有图片的,从001门店更新 update a set a.picid=(select top 1 isnull(p.gah04c,0) from gam08p p with (nolock) where wpid=p.gah02c and datalength(p.gah02p)>20 and p.gah00c='001' and (isnull(p.gah03c,3)=3 or p.gah03c=2) order by p.gah04c desc) from #tbl_data_tem a where isnull(picid,0)=0 --更新类别成中文名字 update a set a.pclass=(select g.gsb03c from gsm02 g with (nolock) where g.gsb02c=pclass and g.gsb00c=compid and g.gsb01c='D') from #tbl_data_tem a --更新图片状态 update a set a.pstatus=(select isnull(p.gah03c,3) from gam08p p with (nolock) where p.gah04c=picid) from #tbl_data_tem a end if(@stype=7) begin if(@killnopic=1)--此时0代表显示所有,1代表只显示需要显示的 begin delete #tbl_data_tem1 where isnull(pstatus,0)<>1 end if(@tophow>0) begin Exec ('select top ' + @tophow + ' compid,wpid,pname,punit,pprice,pclass,picid,pstatus,pstype from #tbl_data_tem1') end else begin select compid,wpid,pname,punit,pprice,pclass,picid,pstatus,pstype from #tbl_data_tem1 end drop table #tbl_data_tem1 end else begin --更新类型 update a set a.pstype=@stype from #tbl_data_tem a if(@killnopic=1) begin delete #tbl_data_tem where isnull(picid,0)=0 end if(@stype=0 or @stype=1 or @stype=5) begin if(@killnopic=2) begin delete #tbl_data_tem where isnull(pstatus,0)<>2 end end if(@tophow>0) begin Exec ('select top ' + @tophow + ' compid,wpid,pname,punit,pprice,pclass,picid,pstatus,pstype from #tbl_data_tem') end else begin select compid,wpid,pname,punit,pprice,pclass,picid,pstatus,pstype from #tbl_data_tem end drop table #tbl_data_tem end end GO /****** Object: Table [dbo].[hem08] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[hem08]( [hei00c] [varchar](10) NOT NULL, [hei01c] [varchar](20) NOT NULL, [hei02c] [varchar](20) NOT NULL, [hei03d] [varchar](8) NULL, [hei04c] [varchar](20) NOT NULL, [hei05f] [float] NULL, [hei06f] [float] NULL, [hei07f] [float] NULL, [hei08f] [float] NULL, [hei09f] [float] NULL, [hei10f] [float] NULL, [hei11i] [int] NULL, [hei12f] [float] NULL, [hei13i] [int] NULL, [hei14i] [int] IDENTITY(1,1) NOT NULL, CONSTRAINT [PK_HEM08] PRIMARY KEY CLUSTERED ( [hei00c] ASC, [hei14i] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gsm11] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gsm11]( [gsk01i] [int] IDENTITY(1,1) NOT NULL, [gsk02c] [varchar](2) NOT NULL, [gsk03c] [varchar](60) NULL, CONSTRAINT [PK_GSM11] PRIMARY KEY CLUSTERED ( [gsk01i] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: UserDefinedFunction [dbo].[fun_split] Script Date: 06/12/2017 06:20:57 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create function [dbo].[fun_split] ( @sourcesql varchar(1000), -- 要分隔的字符串 @strseprate varchar(10) -- 分隔符 ) returns @temp table(line varchar(1000)) as -- 字符串拆分函数 begin declare @i int set @sourcesql = rtrim(ltrim(@sourcesql)) set @i = charindex(@strseprate,@sourcesql) while @i >= 1 begin if len(left(@sourcesql,@i-1))>0 begin insert @temp values(left(@sourcesql,@i-1)) end set @sourcesql=substring(@sourcesql,@i+len(@strseprate),len(@sourcesql)-@i) set @i=charindex(@strseprate,@sourcesql) end if @sourcesql <> '' insert @temp values(@sourcesql) return end GO /****** Object: Table [dbo].[gam28] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gam28]( [compid] [varchar](10) NOT NULL, [cardtype] [varchar](20) NOT NULL, [prjtype] [varchar](5) NOT NULL, [ratio] [float] NOT NULL, CONSTRAINT [PK_GAM28] PRIMARY KEY CLUSTERED ( [compid] ASC, [cardtype] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_getcurrdate] Script Date: 06/12/2017 06:20:10 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_getcurrdate] ( @curdate varchar(20) output, @curtime varchar(20) output ) as begin declare @ddatetime datetime set @ddatetime = getdate() select @curdate = substring(convert(varchar(20), @ddatetime, 102), 1, 4) + substring(convert(varchar(20), @ddatetime, 102), 6, 2) + substring(convert(varchar(20), @ddatetime, 102), 9, 2) select @curtime = substring(convert(varchar(20), @ddatetime, 108), 1, 2) + substring(convert(varchar(20), @ddatetime, 108), 4, 2) + substring(convert(varchar(20), @ddatetime, 108), 7, 2) end GO /****** Object: Table [dbo].[sysoperationlog] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[sysoperationlog]( [log_id] [int] IDENTITY(1,1) NOT NULL, [userid] [varchar](10) NOT NULL, [program] [varchar](10) NOT NULL, [operation] [varchar](1) NOT NULL, [operationdate] [varchar](8) NOT NULL, [operationtime] [varchar](8) NOT NULL, [origatedate] [varchar](8) NOT NULL, [compid] [varchar](10) NOT NULL, [keyvalue1] [varchar](160) NULL, [keyvalue2] [varchar](160) NULL, [keyvalue3] [varchar](160) NULL, [keyvalue4] [varchar](160) NULL, CONSTRAINT [Pk_sysoperationlog_id] PRIMARY KEY CLUSTERED ( [log_id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gsm15] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gsm15]( [gso00c] [varchar](10) NOT NULL, [gso01c] [varchar](10) NOT NULL, [gso02c] [varchar](5) NOT NULL, [gso03i] [float] NULL, [gso04f] [float] NULL, [gso05f] [float] NULL, [gso06f] [float] NULL, [gso07f] [float] NULL, [gso08f] [float] NULL, [gso09f] [float] NULL, [gso10f] [float] NULL, CONSTRAINT [PK_GSM15] PRIMARY KEY CLUSTERED ( [gso00c] ASC, [gso01c] ASC, [gso02c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gbm03] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gbm03]( [gbc00c] [varchar](10) NOT NULL, [gbc01c] [varchar](20) NOT NULL, [gbc02f] [varchar](20) NOT NULL, [gbc03c] [varchar](5) NULL, [gbc04c] [varchar](20) NULL, [gbc05i] [int] NULL, CONSTRAINT [PK_GBM03] PRIMARY KEY CLUSTERED ( [gbc00c] ASC, [gbc01c] ASC, [gbc02f] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[aloneproductprecept] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[aloneproductprecept]( [gap00c] [varchar](10) NOT NULL, [gap01c] [varchar](20) NOT NULL, [gap02c] [varchar](20) NOT NULL, [gap03i] [int] NULL, [gap04f] [float] NULL, [gap05c] [varchar](100) NULL, [gap06c] [varchar](100) NULL, [gap07c] [varchar](100) NULL, [gap08c] [varchar](100) NULL, [gap09c] [varchar](100) NULL, CONSTRAINT [PK_ALONEPRODUCTPRECEPT] PRIMARY KEY CLUSTERED ( [gap00c] ASC, [gap01c] ASC, [gap02c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_client_return_info] Script Date: 06/12/2017 06:19:42 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_client_return_info] ( @compid varchar(10), @fromdate varchar(8), @todate varchar(8), @goodis varchar(5), @rettype int ) as begin create table #gbm10 ( gbj00c varchar(10) not null, -- 公司编号 gbj01i int identity not null, -- 流水号 gbj02c varchar(20) null, -- 消费单号 gbj03f float null, -- 序号 gbj04c varchar(20) null, -- 会员编号 gbj05c varchar(20) null, -- 项目编号 gbj06c varchar(20) null, -- 联系方式固定电话 gbj07d varchar(8) null, -- 服务日期 gbj08d varchar(8) null, -- 应该回访日期 gbj09c varchar(10) null, -- 回访员工编号 gbj10i int null, -- 是否已经回访 gbj11c varchar(5) null, -- 访问结果 gbj12c varchar(80) null, -- 访问结果描述 gbj13i int null, -- 类别 1:会员 2:散客 gbj14c varchar(20) null, -- 姓名 gbj15c varchar(20) null, -- 手机 gbj16i int null, -- 类别 0项目 1产品 gbj17c varchar(500) null, -- 名称 gbj18c varchar(20) null, --会员卡号 gbj19c varchar(20) null, --满意程度 gbj20c varchar(20) null, --服务人员1编号 gbj21c varchar(20) null, --服务人员1名称 gbj22c varchar(20) null, --服务人员2编号 gbj23c varchar(20) null, --服务人员2名称 gbj24c varchar(20) null, --服务人员3编号 gbj25c varchar(20) null, --服务人员3名称 gbj26c varchar(20) null, --服务人员4编号 gbj27c varchar(20) null, --服务人员4名称 gbj28f float null, --价格 ) declare @PK_Sql nvarchar(max) set @PK_Sql = ' alter table #gbm10 add constraint PK_#gbm10_' + convert(varchar(10), cast(floor(rand() * 1000000) as int)) + ' primary key clustered(gbj00c, gbj01i)' exec sp_executesql @PK_Sql ---项目回访 insert into #gbm10(gbj00c, gbj02c, gbj03f, gbj04c, gbj05c, gbj06c, gbj07d, gbj08d, gbj09c, gbj10i, gbj11c, gbj12c, gbj13i, gbj14c, gbj15c, gbj16i, gbj17c) select gbj00c, gbj02c, gbj03f, gbj04c, gbj05c, gbj06c, gbj07d, gbj08d, gbj09c, gbj10i, gbj11c, gbj12c, gbj13i, gbj14c, gbj15c, gbj16i, gbj17c from gbm10 a, gam26 z where gbj00c = z.gaz02c and z.gaz01c = @compid and gbj08d between @fromdate and @todate and (gbj11c = @goodis or @goodis = '*') and gbj10i = 1 update a set gbj20c = ggb12c, gbj22c = ggb13c, gbj24c = ggb14c, gbj26c = ggb26c, gbj28f = ggb11f from #gbm10 a, ggm01, ggm02 where gbj00c = gga00c and gbj02c = gga01c and gga00c = ggb00c and gga01c = ggb01c and ggb03c = gbj05c update a set gbj20c = ggc12c, gbj22c = ggc15c, gbj24c = ggc17c, gbj28f = ggc11f from #gbm10 a, ggm01, ggm03 where gbj00c = gga00c and gbj02c = gga01c and gga00c = ggc00c and gga01c = ggc01c and ggc03c = gbj05c update a set gbj18c = gca01c from #gbm10 a, gcm01 where gbj00c = gca00c and gbj04c = gca04c and gca00c = gca13d and substring(gbj04c, 1, 2)<>'散客' update a set gbj19c = gsb03c from #gbm10 a, gsm02 where gbj00c = gsb00c and gsb01c = 'A1' and gsb02c = gbj11c update a set gbj21c = haa02c from #gbm10 a, ham01 where gbj00c = haa00c and gbj20c = haa01c update a set gbj23c = haa02c from #gbm10 a, ham01 where gbj00c = haa00c and gbj22c = haa01c update a set gbj25c = haa02c from #gbm10 a, ham01 where gbj00c = haa00c and gbj24c = haa01c update a set gbj27c = haa02c from #gbm10 a, ham01 where gbj00c = haa00c and gbj26c = haa01c if @rettype = 1 begin select * from #gbm10 end else begin declare @stasum float create table #gbm10_per ( compid varchar(10) not null, statype varchar(10) not null, staname varchar(50) null, fcount float null, sumcount float null ) create table #gbm10_sum ( compid varchar(10) not null, statype varchar(10) null, stacount float null, ) insert into #gbm10_per(compid, statype, staname) select gsb00c, gsb02c, gsb03c from gsm02 where gsb00c = @compid and gsb01c = 'A1' insert into #gbm10_sum(compid, statype, stacount) select gbj00c, gbj11c, count(gbj11c) from #gbm10 where isnull(gbj11c, '')<>'' group by gbj00c, gbj11c select @stasum = count(gbj11c) from #gbm10 where isnull(gbj11c, '')<>'' update a set a.fcount = b.stacount from #gbm10_per a, #gbm10_sum b where a.statype = b.statype update a set a.sumcount = case when isnull(fcount, '')<>'' then fcount/@stasum else 0 end from #gbm10_per a, #gbm10_sum b where a.statype = b.statype select * from #gbm10_per end drop table #gbm10 end GO /****** Object: Table [dbo].[gsm16] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gsm16]( [gsp00c] [varchar](10) NOT NULL, [gsp01c] [varchar](5) NOT NULL, [gsp02c] [varchar](5) NOT NULL, [gsp03i] [float] NULL, CONSTRAINT [PK_GSM16] PRIMARY KEY CLUSTERED ( [gsp00c] ASC, [gsp01c] ASC, [gsp02c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[sysstruct] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[sysstruct]( [version] [varchar](10) NOT NULL, [system] [varchar](10) NOT NULL, [parentid] [int] NOT NULL, [modulegroupid] [tinyint] NOT NULL, [classid] [tinyint] NULL, [orderid] [tinyint] NULL, [name] [varchar](40) NOT NULL, [e_name] [varchar](40) NULL, CONSTRAINT [Pk_sysstruct_id] PRIMARY KEY CLUSTERED ( [version] ASC, [parentid] ASC, [modulegroupid] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[myfolder] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[myfolder]( [userid] [varchar](10) NOT NULL, [program] [varchar](10) NOT NULL, [origatedate] [varchar](8) NOT NULL, [compid] [varchar](10) NOT NULL, [keyvalue1] [varchar](20) NOT NULL, [keyvalue2] [varchar](20) NOT NULL, [keyvalue3] [varchar](20) NOT NULL, [keyvalue4] [varchar](20) NOT NULL, CONSTRAINT [Pk_myfolder_id] PRIMARY KEY CLUSTERED ( [userid] ASC, [compid] ASC, [program] ASC, [keyvalue1] ASC, [keyvalue2] ASC, [keyvalue3] ASC, [keyvalue4] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_cardinfo_combo_treatment_lm1_gtc08] Script Date: 06/12/2017 06:19:38 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_cardinfo_combo_treatment_lm1_gtc08] ( @compid varchar(10), --门店编号 @flag char(2), --0:疗程,1:套餐 @cardnofrom varchar(10), --会员卡号 @cardnoto varchar(20), @combofrom varchar(20), --疗程编号 @comboto varchar(20), @treatmentfrom varchar(20), --套餐编号 @treatmentto varchar(20) ) as begin --创建临时表 -套餐产品 create table #gtc08 ( gtj00c varchar(10) not null, --门店编号 gtj01c varchar(20) not null, --卡号 gtj01cName varchar(40) null, --会员姓名 gtj02c varchar(20) not null, --套餐编号 gtj02cName varchar(40) null, --套餐名称 gtj03c varchar(20) not null, --产品编号 gtj03cName varchar(40) null, --产品名称 gtj04f float null, --总次数 gtj05f float null, --已经使用次数 gtj06f float null, --剩余次数 flag int not null --区分套餐中的项目,产品 ) -- 插入数据 insert #gtc08 (gtj00c, gtj01c, gtj02c, gtj03c, gtj04f, gtj05f, gtj06f, flag) select gtj00c, gtj01c, UPPER(gtj02c), gtj03c, sum(gtj04f), sum(gtj05f), sum(gtj06f), '0' from gtc08 with (nolock) where gtj00c = @compid and ((gtj01c >= @cardnofrom and gtj01c <= @cardnoto) or (@cardnofrom = '*')) and ((gtj02c <= @treatmentfrom and gtj02c >= @treatmentto) or (@treatmentfrom = '*')) and gtj12i = 0 group by gtj00c, gtj01c, gtj02c, gtj03c --更改会员姓名 update a set a.gtj01cName = b.gba03c from #gtc08 a, gbm01 b with (nolock) where a.gtj00c = b.gba00c and a.gtj01c = b.gba23c --更改套餐名称 update a set a.gtj02cName = b.gta02c from #gtc08 a, gtc01 b with (nolock) where a.gtj00c = b.gta00c and a.gtj02c = b.gta01c --更改产品名称 update a set a.gtj03cName = b.gfa03c from #gtc08 a, gfm01 b with(nolock) where a.gtj00c = b.gfa00c and a.gtj03c = b.gfa01c select isnull(gtj00c,'') gtj00c, isnull(gtj01c,'') gtj01c, isnull(gtj01cName,'') gtj01cName, isnull(gtj02c,'') gtj02c, isnull(gtj02cName,'') gtj02cName, isnull(gtj03c,'') gtj03c, isnull(gtj03cName,'') gtj03cName, isnull(gtj04f,0) gtj04f, isnull(gtj05f,0) gtj05f, isnull(gtj06f,0) gtj06f, isnull(flag,0) flag from #gtc08 order by gtj02c, gtj01c, gtj00c --套餐产品 drop table #gtc08 end GO /****** Object: Table [dbo].[gum01] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gum01]( [gua00c] [varchar](10) NOT NULL, [gua01c] [varchar](10) NOT NULL, [gua02c] [varchar](20) NOT NULL, [gua03f] [float] NULL, [gua04f] [float] NULL, [gua05d] [varchar](8) NULL, [gua06c] [varchar](20) NULL, [gua91c] [varchar](10) NULL, [gua92d] [varchar](8) NULL, CONSTRAINT [pk_gum01] PRIMARY KEY CLUSTERED ( [gua00c] ASC, [gua01c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gbm15] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gbm15]( [gbl00c] [varchar](10) NOT NULL, [gbl01c] [varchar](20) NOT NULL, [gbl02f] [float] NOT NULL, [gbl03c] [varchar](5) NULL, [gbl04f] [float] NULL, [gbl05d] [varchar](8) NULL, [gbl06c] [varchar](20) NULL, [gbl07f] [float] NULL, [gbl08d] [varchar](8) NULL, [gbl09c] [varchar](5) NULL, [gbl10c] [varchar](20) NULL, [gbl11f] [float] NULL, [gbl12i] [int] NULL, [gbl13c] [varchar](40) NULL, [gbl14c] [varchar](20) NULL, [gbl15c] [varchar](5) NULL, [gbl16c] [varchar](5) NULL, [gbl17t] [varchar](6) NULL, [gbl18t] [varchar](6) NULL, [gbl19c] [varchar](20) NULL, [gbl20c] [varchar](20) NULL, [gbl21c] [varchar](20) NULL, [gbl22c] [varchar](20) NULL, [gbl23c] [varchar](20) NULL, [gbl24c] [varchar](20) NULL, [gbl25c] [varchar](20) NULL, [gbl26f] [float] NULL, [gbl27f] [float] NULL, [gbl28f] [float] NULL, CONSTRAINT [PK_GBM15] PRIMARY KEY CLUSTERED ( [gbl00c] ASC, [gbl01c] ASC, [gbl02f] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[openpolicy_prod_del] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[openpolicy_prod_del]( [compid] [varchar](20) NOT NULL, [billid] [varchar](20) NOT NULL, [flagid] [int] NOT NULL, [project] [varchar](20) NULL, [servicer] [varchar](8) NULL, [price] [float] NULL, [number] [float] NULL, [amount] [float] NULL, [empa] [varchar](20) NULL, [empb] [varchar](20) NULL, [empc] [varchar](20) NULL, [userid] [varchar](20) NULL, [ddate] [varchar](10) NULL, [ttime] [varchar](10) NULL, [idd] [int] IDENTITY(1,1) NOT NULL, CONSTRAINT [PK_OPENPOLICY_PROD_DEL] PRIMARY KEY CLUSTERED ( [idd] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_cal_all_employees_produce_vperf] Script Date: 06/12/2017 06:19:31 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_cal_all_employees_produce_vperf] ( @quan float, -- 数量 @standardprice float, -- 标准单价, @in_cost float, -- 总进货成本, 不需要再考虑数量 @std_cost float, -- 总销售成本, 不需要再考虑数量 @price float, -- 实际单价 @amt float, -- 该产品的金额, 已经包含了数量的考虑 @ex_rate float, -- 实际营业额折算比率(很多卡在销售的时候已经赠送了金额) @mode int, -- 业绩的计算方式 @setvalue float, -- 业绩比率与金额 @ratio float, -- 分享比率 @vperf float output -- 虚业绩 ) as begin declare @basicamt float select @ratio = isnull(@ratio, 0) if @mode = 1 select @basicamt = @setvalue * @quan else if @mode = 2 select @basicamt = @amt * @setvalue else if @mode = 5 select @basicamt = @standardprice * @quan * @setvalue else if @mode = 9 select @basicamt = @amt * @ex_rate * @setvalue else if @mode = 10 -- 营业额的折扣折算,在营业的基础上再计算上项目的折扣 select @basicamt = @amt * @setvalue * (@amt / (@standardprice * @quan)) else if @mode = 11 -- 实际营业额再折扣 select @basicamt = @amt * @ex_rate * @setvalue * (@amt / (@standardprice * @quan)) else if @mode = 20 -- 标准价格 - 进货成本 select @basicamt = (isnull(@standardprice, 0) * isnull(@quan, 1) - isnull(@in_cost, 0)) * @setvalue else if @mode = 21 -- 营业额 - 进货成本 select @basicamt = (@amt - isnull(@in_cost, 0)) * @setvalue else if @mode = 22 -- 标准价格 - 销售成本 select @basicamt = (isnull(@standardprice, 0) * isnull(@quan, 1) - isnull(@std_cost, 0)) * @setvalue else if @mode = 23 -- 营业额 - 销售成本 select @basicamt = (@amt - isnull(@std_cost, 0)) * @setvalue select @vperf = @basicamt * @ratio end GO /****** Object: Table [dbo].[gbm20] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gbm20]( [gbt00c] [varchar](10) NOT NULL, [gbt01c] [varchar](20) NOT NULL, [gbt02c] [varchar](40) NULL, [gbt03c] [varchar](30) NULL, [gbt04c] [varchar](40) NULL, [gbt05c] [varchar](20) NULL, [gbt06i] [int] NULL, [gbt07c] [varchar](20) NULL, [gbt08c] [varchar](20) NULL, [gbt09c] [varchar](20) NULL, [gbt10c] [varchar](20) NULL, [gbt11c] [varchar](20) NULL, [gbt12c] [varchar](20) NULL, [gbt13c] [varchar](80) NULL, [gbt14c] [varchar](80) NULL, [gbt15c] [varchar](20) NULL, [gbt16c] [varchar](40) NULL, [gbt17c] [varchar](40) NULL, [gbt18d] [datetime] NULL, [gbt18c] [varchar](20) NULL, CONSTRAINT [PK_GBM20] PRIMARY KEY CLUSTERED ( [gbt00c] ASC, [gbt01c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[satisfaction] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[satisfaction]( [sf00c] [varchar](10) NOT NULL, [sf01c] [varchar](10) NOT NULL, [sf02c] [varchar](10) NULL, CONSTRAINT [PK_Satisfaction] PRIMARY KEY CLUSTERED ( [sf00c] ASC, [sf01c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_cal_all_employees_good_vperf] Script Date: 06/12/2017 06:19:31 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_cal_all_employees_good_vperf]( @compid varchar(10), -- 公司别 @good varchar(20), -- 产品别 @quan float, -- 数量 @standardprice float, -- 标准单价, @in_cost float, -- 总进货成本, 不需要再考虑数量 @std_cost float, -- 总销售成本, 不需要再考虑数量 @price float, -- 实际单价 @amt float, -- 该产品的金额, 已经包含了数量的考虑 @ex_rate float, -- 实际营业额折算比率(很多卡在销售的时候已经赠送了金额) @ex_rate_points float, -- 积分的折算比率 @mode int, -- 业绩的计算方式 @setvalue float, -- 业绩比率与金额 @ratio_1 float, -- 分享比率1 @ratio_2 float, -- 分享比率2 @ratio_3 float, -- 分享比率3 @vperf_1 float output, -- 大工虚业绩 @vperf_2 float output, -- 中工虚业绩 @vperf_3 float output, -- 小工虚业绩 @paymode varchar(5), -- 支付方式 @p_mode int, -- 积分的计算方式 @p_setvalue float, -- 积分比率与金额 @p_points float output) -- 积分 as begin declare @basicamt float select @ratio_1 = isnull(@ratio_1, 0), @ratio_2 = isnull(@ratio_2, 0), @ratio_3 = isnull(@ratio_3, 0) if @mode = 1 select @basicamt = @setvalue * @quan else if @mode = 2 select @basicamt = @amt * @setvalue else if @mode = 5 select @basicamt = @standardprice * @quan * @setvalue else if @mode = 9 select @basicamt = @amt * @ex_rate * @setvalue else if @mode = 10 -- 营业额的折扣折算,在营业的基础上再计算上项目的折扣 select @basicamt = @amt * @setvalue * (@amt/(@standardprice * @quan)) else if @mode = 11 -- 实际营业额再折扣 select @basicamt = @amt * @ex_rate * @setvalue* (@amt/(@standardprice * @quan)) else if @mode = 20 -- 标准价格 - 进货成本 select @basicamt = (isnull(@standardprice, 0) * isnull(@quan, 1)- isnull(@in_cost, 0)) * @setvalue else if @mode = 21 -- 营业额 - 进货成本 select @basicamt = (@amt -isnull(@in_cost, 0)) * @setvalue else if @mode = 22 -- 标准价格 - 销售成本 select @basicamt = (isnull(@standardprice, 0) * isnull(@quan, 1) - isnull(@std_cost, 0)) * @setvalue else if @mode = 23 -- 营业额 - 销售成本 select @basicamt = (@amt - isnull(@std_cost, 0) ) * @setvalue select @vperf_1 = @basicamt * @ratio_1, @vperf_2 = @basicamt * @ratio_2, @vperf_3 = @basicamt * @ratio_3 if isnull(@p_mode, 0) = 0 select @p_points = 0 else if @p_mode = 1 select @p_points = @p_setvalue * @quan * @ex_rate_points else if @p_mode = 2 select @p_points = @amt * @p_setvalue * @ex_rate_points else if @p_mode = 5 select @p_points = @standardprice * @quan * @p_setvalue * @ex_rate_points else if @p_mode = 9 select @p_points = @amt * @ex_rate * @p_setvalue * @ex_rate_points end GO /****** Object: Table [dbo].[gmm12] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gmm12]( [gml00c] [varchar](10) NOT NULL, [gml01c] [varchar](20) NOT NULL, [gml02f] [float] NOT NULL, [gml03d] [varchar](8) NULL, [gml04d] [varchar](8) NULL, [gml05c] [varchar](20) NULL, [gml06c] [varchar](40) NULL, CONSTRAINT [PK_GMM12] PRIMARY KEY CLUSTERED ( [gml00c] ASC, [gml01c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_avaliable_combo_good] Script Date: 06/12/2017 06:19:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_avaliable_combo_good]( @compid varchar(10), @cardid varchar(20)) as begin create table #gtc08_buf ( gtj00c varchar(10) not null, --公司编号 gtj01c varchar(20) not null, --卡号 gtj02c varchar(20) not null, --套餐编号 gtj03c varchar(20) not null, --产品编号 gtj04f float null, --总次数 gtj05f float null, --已经使用次数 gtj06f float null, --剩余次数 gtj07c varchar(20) null, --替换产品编号 gtj09i int not null, --序号 gtj10d varchar(10) null, --购买日期 primary key clustered(gtj00c, gtj01c, gtj09i) ) insert #gtc08_buf(gtj00c, gtj01c, gtj02c, gtj03c, gtj04f, gtj05f, gtj06f, gtj07c, gtj09i, gtj10d) select gtj00c, gtj01c, gtj02c, gtj03c, gtj04f, gtj05f, gtj06f, gtj07c, gtj09i, '' from gtc08 with(nolock) where gtj00c = @compid and gtj01c = @cardid and isnull(gtj06f, 0)>0 -- 剩余次数大于0 and gtj07c = gtj03c -- 主项目 --把非主项目也加入可以选择 insert #gtc08_buf(gtj00c, gtj01c, gtj02c, gtj03c, gtj04f, gtj05f, gtj06f, gtj07c, gtj09i, gtj10d) select a.gtj00c, a.gtj01c, a.gtj02c, a.gtj03c, a.gtj04f, a.gtj05f, 0, a.gtj07c, a.gtj09i, '' from gtc08 a with(nolock), #gtc08_buf b where a.gtj07c<>a.gtj03c and a.gtj00c = b.gtj00c and a.gtj01c = b.gtj01c and a.gtj02c = b.gtj02c and a.gtj07c = b.gtj03c select gtj00c, gtj01c, gtj02c, gtj03c, gtj04f, gtj05f, gtj06f, gtj07c, gtj09i, gtj10d, ischeck = 0, gfa03c from #gtc08_buf a, gfm01 b where a.gtj00c = b.gfa00c and a.gtj03c = b.gfa01c drop table #gtc08_buf end GO /****** Object: Table [dbo].[gwm01] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gwm01]( [idd] [int] IDENTITY(1,1) NOT NULL, [gwa00c] [varchar](10) NOT NULL, [gwa01d] [varchar](8) NOT NULL, [gwa02t] [varchar](6) NOT NULL, [gwa02i] [bit] NOT NULL, [gwa03t] [varchar](6) NOT NULL, [gwa03i] [bit] NOT NULL, [gwa04c] [varchar](20) NULL, [gwa05c] [varchar](60) NULL, [gwa06c] [varchar](20) NULL, [gwa07c] [varchar](20) NULL, [gwa08i] [int] NULL, [gwa09i] [int] NULL, [gwa10i] [int] NULL, [gwa11i] [int] NULL, [gwa12c] [varchar](20) NULL, [gwa13c] [varchar](20) NULL, [gwa14c] [varchar](20) NULL, [gwa15c] [varchar](20) NULL, [gwa16c] [varchar](60) NULL, [gwa17c] [varchar](20) NULL, [gwa18c] [varchar](20) NULL, [gwa19c] [varchar](20) NULL, [gwa20c] [varchar](20) NULL, [gwa21c] [varchar](20) NULL, [gwa22c] [varchar](60) NULL, [gwa23i] [int] NULL, [status] [int] NULL, [readflag] [int] NULL, [source] [int] NULL, [llid] [varchar](50) NULL, CONSTRAINT [PK_GWM01] PRIMARY KEY CLUSTERED ( [idd] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gxm02] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gxm02]( [gxb00c] [varchar](10) NOT NULL, [gxb01f] [float] NOT NULL, [gxb02c] [varchar](5) NULL, [gxb03c] [varchar](1000) NULL, [gxb04i] [int] NULL, CONSTRAINT [PK_GXM02] PRIMARY KEY CLUSTERED ( [gxb00c] ASC, [gxb01f] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_query_gmb18R] Script Date: 06/12/2017 06:20:30 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create proc [dbo].[upg_query_gmb18R] ( @compid varchar(10), @custumer varchar(60), @fromdate varchar(10), @todate varchar(10) ) as begin create table #table_ggm01 ( compid varchar(10) not null, -- 门店编号 billno varchar(30) not null, -- 消费单号 custumer varchar(60) null, -- 会员卡号 date varchar(8) null, -- 消费日期 project varchar(500) null, -- 所做项目 jine float null, -- 金额 comm varchar(5000) null, -- 沟通记录 staff varchar(30) null, -- 操作人 staffName varchar(60) null, -- 操作人姓名 ) --收银 insert into #table_ggm01(compid,billno,custumer,jine) select gga00c,gga01c,gga05c,gga16f from ggm01 where gga00c = @compid and gga05c = @custumer and (@fromdate = '' or gga02d between @fromdate and @todate) --加上所做的项目 declare @ggb00c varchar(10) declare @ggb01c varchar(30) declare @ggb03c varchar(30) declare @ggb03cName varchar(60) declare ggm02_cursor cursor for select ggb00c, ggb01c, ggb03c, ggb03cName = (select gda03c from gdm01 where gda00c = ggb00c and gda01c = ggb03c) from ggm02 inner join #table_ggm01 on ggb00c = compid and ggb01c = billno open ggm02_cursor fetch next from ggm02_cursor into @ggb00c,@ggb01c,@ggb03c,@ggb03cName while (@@fetch_status = 0) begin update #table_ggm01 set project = isnull(project,'') + @ggb03c +'('+@ggb03cName+') ' where compid = @ggb00c and billno = @ggb01c fetch next from ggm02_cursor into @ggb00c,@ggb01c,@ggb03c,@ggb03cName end close ggm02_cursor deallocate ggm02_cursor update a set comm = b.gbf03c, staff = b.gbf05c, staffName = (select haa02c from ham01 where haa00c = gbf00c and haa01c = gbf05c), date = b.gbf04c from #table_ggm01 a, gbm06 b where a.compid = b.gbf00c and a.billno = b.gbf06c and a.custumer = b.gbf01c select isnull(compid,'') compid, isnull(billno,'') billno, isnull(custumer,'') custumer, isnull(date,'') date, isnull(project,'') project, isnull(jine,'') jine, isnull(comm,'') comm, isnull(staff,'') staff, isnull(staffName,'') staffName from #table_ggm01 order by billno desc drop table #table_ggm01 end GO /****** Object: Table [dbo].[ggm07] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[ggm07]( [ggg00c] [varchar](10) NOT NULL, [ggg01c] [varchar](20) NOT NULL, [ggg02c] [varchar](20) NOT NULL, [ggg03c] [varchar](5) NULL, [ggg04f] [float] NULL, [ggg05f] [float] NULL, [ggg06f] [float] NULL, [ggg07f] [float] NULL, CONSTRAINT [PK_GGM07] PRIMARY KEY CLUSTERED ( [ggg00c] ASC, [ggg01c] ASC, [ggg02c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[SP_TranLog] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[SP_TranLog]( [SPName] [varchar](100) NULL, [SPTime] [datetime] NOT NULL, [SPParameter] [varchar](max) NULL ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gicm03] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gicm03]( [gicc00c] [varchar](10) NOT NULL, [gicc01v] [varchar](20) NOT NULL, [gicc02v] [varchar](20) NOT NULL, [gicc03v] [varchar](20) NULL, [gicc04i] [int] NULL, [gicc05f] [float] NULL, [gicc06v] [varchar](20) NULL, [gicc07t] [varchar](6) NULL, [gicc08d] [varchar](8) NULL, [gicc09v] [varchar](100) NULL, CONSTRAINT [PK_GICM03] PRIMARY KEY CLUSTERED ( [gicc00c] ASC, [gicc01v] ASC, [gicc02v] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gim01] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gim01]( [gia00c] [varchar](10) NOT NULL, [gia01c] [nvarchar](100) NOT NULL, [gia03d] [varchar](8) NULL, [gia04c] [varchar](4) NULL, [gia05c] [varchar](20) NULL, [gia06c] [varchar](10) NULL, [gia07i] [int] NULL, [gia91c] [varchar](10) NULL, [gia92d] [varchar](8) NULL, [gia93c] [varchar](10) NULL, [gia94d] [varchar](8) NULL, [gia08t] [varchar](6) NULL, [gia09i] [tinyint] NULL, [gia10c] [varchar](200) NULL, [gia11c] [varchar](20) NULL, CONSTRAINT [PK_GIM01] PRIMARY KEY CLUSTERED ( [gia00c] ASC, [gia01c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[DistributeOrCopyLookup] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[DistributeOrCopyLookup]( [DisplayName] [varchar](100) NOT NULL, [tableName] [varchar](20) NOT NULL ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_grade_gmb18i_query] Script Date: 06/12/2017 06:20:12 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create proc [dbo].[upg_grade_gmb18i_query] ( @companyId varchar(30), --公司编号 @xmdl varchar(30), --项目大类(部门) @fromjuli int, --多久没来 @tojuli int, --多久没来 @fromcs int, --来店次数 @tocs int, --来店次数 @fromjine float, --平均消费金额 @tojine float, --平均消费金额 @fromdate varchar(10), --消费时间 @todate varchar(10), --消费时间 @fromxfnl float, --消费能力 @toxfnl float, --消费能力 @fromlcsale float, --购买能力(疗程销售) @tolcsale float, --购买能力(疗程销售) @frommaxjine float, --单笔消费 @tomaxjine float, --单笔消费 @frompinlu float, --来店频率(消费频率) @topinlu float --来店频率(消费频率) ) as begin create table #tb_query_gmb18i ( gba00c varchar(10), -- 公司编号 gba01c varchar(30), -- 会员编号 gba03c varchar(60), -- 会员姓名 gna26f float, -- 办卡(卡销售) gcl07f float, -- 卡充值金额 lcxfnl float, -- 疗程消费金额(消费能力) lcsale float, -- 购买疗程(办卡) lccs float, -- 疗程总次数 lczs float, -- 疗程赠送次数 lcxhcs float, -- 疗程消耗次数 lcxhjine float, -- 疗程消耗金额 lcfromtime varchar(10), -- 疗程第一次消费日期 lctotime varchar(10), -- 疗程最后一次消费日期 jine float, -- 消费总金额 maxjine float, -- 最大单笔消费金额 juli float, -- 多久没来 cishu float, -- 消费总次数 pinlu float, -- 消费频率 pjxh float, -- 平均消费金额 ggc11f float, --外卖品 startime varchar(30), --第一次消费时间 endtime varchar(30), --最近消费时间 gba08c varchar(30), -- Mobile No. gca01c varchar(30) --会员卡号 ) insert into #tb_query_gmb18i select gba00c, -- 公司编号 gba01c, -- 会员编号 gba03c, -- 会员姓名 gna26f, --办卡(卡销售) gcl07f, --卡充值金额 lcxfnl, --疗程消费 0 as lcsale, 0 as lccs, 0 as lczs, lcxhcs, --疗程消耗次数 lcxhjine, --疗程消耗金额 lcfromtime, --疗程第一次消费日期 lctotime, --疗程最后一次消费日期 convert(decimal(19,2),jine) jine, --其他消费金额 maxjine, --最大单笔消费金额 juli, -- 多久没来 1 cishu, -- 消费总次数 1 pinlu, --消费频率 0 pjxh, ggc11f, --外卖品 startime, --第一次消费时间 endtime, --最近消费时间 gba08c, -- Mobile No. gca01c --会员卡号 from gbm01 --会员基本资料主档 left join ( select ggb00c compyId, gga05c membid, -- 会员代码, sum(isnull(ggb11f,'')) jine, -- 消费总金额 max(isnull(ggb11f,0)) maxjine, --最大单笔消费金额 min(gga94d) startime, -- 开始消费日期 max(gga94d) endtime, -- 最后消费日期 -datediff(dd,getdate(),convert(datetime,max(gga94d))) juli --多久没来店 from ggm02 inner join ggm01 on ggb00c = gga00c and ggb01c = gga01c inner join gdm01 on gdm01.gda00c = ggm02.ggb00c And gdm01.gda01c = ggm02.ggb03c where (@fromdate = '' or (gga94d between @fromdate and @todate)) and (@xmdl = '' or gda02c = @xmdl) --(项目大类)部门 group by ggb00c, gga05c ) aTable on gba00c = compyId and gba01c = membid left join ( select gch00c, -- 门店编号 gch03c, -- 卡号 sum(isnull(gch08f,0)) lcxhcs, --消耗次数 sum(isnull(gch09f,0)) lcxhjine, --消耗金额 min(gch02d) lcfromtime, -- 第一次疗程消费日期 max(gch02d) lctotime, -- 最后一次疗程消费日期 sum(isnull(gch18f,0)+isnull(gch20f,0)+isnull(gch22f,0)) lcxfnl --疗程消费金额 from gcm08 inner join gdm01 on gch00c = gda00c and gch04c = gda01c where gch10c = 'gx' --疗程消费 and (@xmdl = '' or gda02c = @xmdl) --(项目大类)部门 and (@fromdate = '' or (gch02d between @fromdate and @todate)) group by gch00c,gch03c ) fTable on gch00c = compyId and gch03c = membid left join gcm01 --会员卡 on gba00c = gca00c and gba23c = gca01c left join ( select gcl00c, --公司编号 gca04c member, --会员编号 sum(gcl65f) gcl07f --金额 from gcm10 left join gcm01 on gcl00c = gca00c and gcl03c = gca01c where gcl06i = 0 and gcl04c = '2' --仅包含储值账户 and (@fromdate = '' or (gcl02d between @fromdate and @todate)) group by gcl00c,gca04c ) cTable --卡充值 on gba00c = gcl00c and gba01c = member left join ( select ggc00c, gga05c, --会员代码 sum(ggc11f) ggc11f from ggm03 left join ggm01 on gga00c = ggc00c and gga01c = ggc01c where (@fromdate = '' or (gga94d between @fromdate and @todate)) group by ggc00c,gga05c ) dTable --外卖品 on gba00c = ggc00c and gba01c = gga05c left join ( select gna00c, gna04c, sum(gna26f) gna26f from gnm01 where (@fromdate = '' or (gna02d between @fromdate and @todate)) group by gna00c,gna04c ) eTable --卡销售(办卡) on gba00c = gna00c and gba01c = gna04c where gba00c in (select gaz02c from gam26 where gaz01c = @companyId ) --公司编号 and ( @fromjuli = -1 or isnull(juli,0) between @fromjuli and @tojuli ) --距离多久没来 and ( @frommaxjine = -1 or isnull(maxjine,0) between @frommaxjine and @tomaxjine) --单笔消费金额 and ( @fromxfnl = -1 or isnull(lcxfnl,0) between @fromxfnl and @toxfnl) --疗程消费能力 --来店次数 declare @compid_cs int update a set a.cishu = cs from #tb_query_gmb18i a, (select gga00c, gga05c, count(*) cs from(select gga00c, gga05c, gga02d from ggm01 where gga00c = @companyId and (@fromdate = '' or (gga02d between @fromdate and @todate)) group by gga00c,gga05c,gga02d ) as a_table group by gga00c,gga05c) as b where a.gba00c = b.gga00c and a.gba01c = gga05c -- 疗程销售 update a set a.lcsale = isnull(a.lcsale,0) + isnull(b.jine,0) from #tb_query_gmb18i a,( select gna00c, gna04c, -- 会员编号 sum(isnull(gnc05f,0)) jine --金额 from gnm03 inner join gnm01 on gna00c = gnc00c and gna01c = gnc01c where gnc00c in (select gaz02c from gam26 where gaz01c = @companyId) and (@fromdate = '' or ( gna02d between @fromdate and @todate)) group by gna00c,gna04c ) b where a.gba00c = b.gna00c and a.gba01c = b.gna04c --疗程销售 update a set a.lcsale = isnull(a.lcsale,0) + isnull(b.jine,0) from #tb_query_gmb18i a, ( select gcl00c, gcl03c, sum(isnull(gcm05f,0)) jine --金额 from gcm11 inner join gcm10 on gcl00c = gcm00c and gcl01c = gcm01c where gcm00c in (select gaz02c from gam26 where gaz01c = @companyId) and gcl06i = 0 and (@fromdate = '' or (gcl02d between @fromdate and @todate)) group by gcl00c,gcl03c ) b where a.gba00c = b.gcl00c and a.gba01c = b.gcl03c --疗程次数 update a set a.lccs = b.lccs, a.lczs = b.lczs from #tb_query_gmb18i a,( select gch00c, -- 门店编号 gch03c, -- 卡号 sum(isnull(gch05f,0))+ sum(isnull(gch06f,0)) lccs, --总次数 sum(isnull(gch06f,0)) lczs --赠送次数 from gcm08 inner join gdm01 on gch00c = gda00c and gch04c = gda01c where gch10c in ('gn','gz') --疗程消费 and (@xmdl = '' or gda02c = @xmdl) --(项目大类)部门 and (@fromdate = '' or (gch02d between @fromdate and @todate)) group by gch00c,gch03c ) b where a.gba00c = b.gch00c and a.gba01c = b.gch03c --平均消费 update #tb_query_gmb18i set pjxh = case cishu when 0 then 0 else convert(decimal(19,2),jine /cishu) end --消费频率 update #tb_query_gmb18i set pinlu = CEILING(round(convert(decimal(19,2), datediff(dd,startime,endtime))/cishu,1)) where datediff(dd,startime,endtime) > 0 select gba00c, -- 公司编号 gba01c, -- 会员编号 gba03c, -- 会员姓名 isnull(gna26f,0) as gna26f, --办卡(卡销售) isnull(gcl07f,0) as gcl07f, --卡充值金额 isnull(lcxfnl,0) as lcxfnl, --疗程消费 isnull(lcsale,0) as lcsale, isnull(lccs,0) as lccs, isnull(lczs,0) as lczs, isnull(lcxhcs,0) as lcxhcs, --疗程消耗次数 isnull(lcxhjine,0) as lcxhjine, --疗程消耗金额 lcfromtime, --疗程第一次消费日期 lctotime, --疗程最后一次消费日期 convert(decimal(19,2),isnull(jine,0)) jine, --其他消费金额 isnull(maxjine,0) as maxjine, --最大单笔消费金额 isnull(juli,0) as juli, -- 多久没来 isnull(cishu,0) as cishu, -- 消费总次数 isnull(pinlu,0) as pinlu, --消费频率 --convert(decimal(19,2),pjxh) pjxh, -- 平均消费金额(不准确) isnull(pjxh,0) as pjxh, isnull(ggc11f,0) as ggc11f, --外卖品 startime, --第一次消费时间 endtime, --最近消费时间 gba08c, -- Mobile No. gca01c --会员卡号 from #tb_query_gmb18i where ( @fromcs = -1 or isnull(cishu,0) between @fromcs and @tocs ) --来店次数 and ( @fromlcsale = -1 or (isnull(lcsale,0) between @fromlcsale and @tolcsale)) --购买能力(疗程销售) and ( @frompinlu =-1 or(isnull(pinlu,0) between @frompinlu and @topinlu)) --来店频率(消费频率) and ( @fromjine = -1 or isnull(pjxh,0) between @fromjine and @tojine ) --平均消费金额 drop table #tb_query_gmb18i end GO /****** Object: Table [dbo].[gkm01] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gkm01]( [gka00c] [varchar](10) NOT NULL, [gka01c] [nvarchar](100) NOT NULL, [gka02d] [varchar](8) NULL, [gka03c] [varchar](4) NULL, [gka04c] [varchar](10) NULL, [gka91c] [varchar](10) NULL, [gka92d] [varchar](8) NULL, [gka93c] [varchar](10) NULL, [gka94d] [varchar](8) NULL, [gka05t] [varchar](6) NULL, [gka06c] [varchar](20) NULL, [gka07c] [varchar](20) NULL, CONSTRAINT [PK_GKM01] PRIMARY KEY CLUSTERED ( [gka00c] ASC, [gka01c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_conv_procamt_card] Script Date: 06/12/2017 06:19:46 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_conv_procamt_card] ( @compid varchar(10), @fromdate varchar(20), @todate varchar(20) ) as begin create table #sale_bill_one_card ( billid varchar(20) not null, cardid varchar(20) not null, sumamt float null, primary key(billid) ) create table #sale_bill_one_card_2 ( billid varchar(20) not null, cardid varchar(20) not null, sumamt float null, primary key(billid) ) create table #sale_bill_proc_amt ( billid varchar(20) not null, sumamt float null, primary key(billid) ) -- 一个卡可能被销售多次 insert #sale_bill_one_card_2(billid, cardid) select gnb01c, min(gnb03c) from gnm01 a with (nolock), gnm02 b with (nolock) where a.gna00c = b.gnb00c and a.gna01c = b.gnb01c and a.gna80d >= @fromdate and a.gna80d <= @todate and a.gna00c = @compid and isnull(gnb01c, '') <> '' group by gnb01c insert #sale_bill_one_card(billid, cardid) select min(billid), cardid from #sale_bill_one_card_2 group by cardid drop table #sale_bill_one_card_2 insert #sale_bill_proc_amt(billid, sumamt) select gnc01c, sum(gnc05f) from gnm01 a with (nolock), gnm03 b with (nolock) where a.gna00c = b.gnc00c and a.gna01c = b.gnc01c and gna80d >= @fromdate and gna80d <= @todate and a.gna00c = @compid and isnull(gnc01c, '') <> '' group by gnc01c update a set a.sumamt = isnull(b.sumamt, 0) from #sale_bill_one_card a, #sale_bill_proc_amt b where a.billid = b.billid if exists(select 1 from tempdb..sysobjects where type = 'U' and name like '#card_proc_amt%') begin insert #card_proc_amt(cardid, sumamt) select cardid, isnull(sumamt, 0) from #sale_bill_one_card end else begin select cardid, sumamt from #sale_bill_one_card end drop table #sale_bill_one_card drop table #sale_bill_proc_amt end GO /****** Object: Table [dbo].[gim20] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gim20]( [gim00c] [varchar](10) NOT NULL, [gim01c] [varchar](20) NOT NULL, [gim02c] [varchar](20) NULL, [gim06c] [varchar](1) NULL, [gim07c] [varchar](20) NULL, [gim08d] [varchar](8) NULL, [gim09d] [varchar](8) NULL, [gim10t] [varchar](8) NULL, [gim11c] [varchar](10) NULL, [gim12c] [varchar](50) NULL, [gim13c] [varchar](20) NULL, [gim14d] [varchar](8) NULL, [gim15d] [datetime] NULL, CONSTRAINT [PK_GIM20] PRIMARY KEY CLUSTERED ( [gim00c] ASC, [gim01c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_staff_card_analysis] Script Date: 06/12/2017 06:20:41 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_staff_card_analysis] ( @compid varchar(10), -- 门店编号 @startdate varchar(10), @enddate varchar(10) ) as begin begin try create table #empl_stat_outer ( id int identity, -- 流水号 person_id varchar(20) not null, -- 员工代码 comp_ascription varchar(10) null, -- 公司别(员工) action_id tinyint not null, -- 类别, 1-卡销售 2-指定大工, 3-轮班大工, 4-补工大工, -- 5-指定中工, 6-轮班中工 -- 7-指定小工, 8-轮班小工, 9-产品销售 10,11-四工 -- 0 卡充值 srvdate varchar(10) not null, -- 日期 code varchar(20) not null, -- 项目代码, 或是卡号, 产品码 name varchar(200) null, -- 名称 payway varchar(40) null, -- 支付方式 amt float null, -- 营业金额 quan float null, -- 数量 cost float null, -- 成本 comm float null, -- 提成 amt2 float null, -- 虚业绩 amt3 float null, -- 真实业绩 cardrate float null, -- 转卡比率 stdprice float null, -- 标准单价 prj_type varchar(20) null, -- 项目类别 share_rate float default 1 null, --分享比率 cls_flag int null, -- 1:项目 2:产品 3:卡 gender int null, -- 性别 0:女 1:男 billid varchar(20) null, -- 单号 paycode varchar(20) null, -- 支付代码 ex_rate float null, -- 实际营业额折算比率 ggm02_seq float null, -- ggm02中的序列号, 与单号一起来标志一个项目 room_id varchar(20) null, -- 房间号,只在spa或足浴版本需要 cardid varchar(20) null, -- 会员卡号 memid varchar(40) null, -- 会员编号 sumMoney float null, -- 会员卡销售总金额 compid varchar(20) null ) create clustered index idx_empl_stat_outer_01 on #empl_stat_outer(person_id, prj_type) declare @spid int select @spid = @@spid insert #empl_stat_outer(person_id, comp_ascription, action_id, srvdate, code, name, payway, amt, quan, cost, comm, amt2, amt3, cardrate, stdprice, prj_type, share_rate, cls_flag, gender, billid, paycode, ex_rate, ggm02_seq, room_id, cardid, memid) exec upg_personal_statistics @compid,@startdate,@enddate,'*','*',1,1,1,1,@spid --类别(产品销售在外): 1: 0 卡充值 1-卡销售, 其他为2 select * from gam10 select comp_ascription as compid, person_id as person, [name] = (select haa02c from ham01 where haa00c = comp_ascription and haa01c = person_id), [action] as status, [type], case [type] when '其他' then '其他' else (select isnull(gak02c,[type]) from gam10 where gak00c = comp_ascription and gak01c = [type]) end typename, sum(amt2) amt from (select comp_ascription, person_id, --员工 case action_id when 1 then 1 when 0 then 1 else 2 end as [action], case isnull(memid,'') when '' then '其他' else (select gca02c from gcm01 where gca01c = cardid) end [type], isnull(amt2,0) as amt2 from #empl_stat_outer where paycode in (select pay from pay_classify where category = '1' and comp = comp_ascription) and action_id <> 9 --9-产品销售 and isnull(amt2,0) <> 0 and isnull(memid,'') <> '' ) as Atable group by comp_ascription,person_id,[action],[type] order by person_id,[type] end try begin catch drop table #empl_stat_outer end catch drop table #empl_stat_outer end GO /****** Object: Table [dbo].[gkm02] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gkm02]( [gkb00c] [varchar](10) NOT NULL, [gkb01c] [nvarchar](100) NOT NULL, [gkb02f] [float] NOT NULL, [gkb03c] [varchar](20) NULL, [gkb04c] [varchar](5) NULL, [gkb05f] [float] NULL, [gkb06f] [float] NULL, [gkb07f] [float] NULL, CONSTRAINT [PK_GKM02] PRIMARY KEY CLUSTERED ( [gkb00c] ASC, [gkb01c] ASC, [gkb02f] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_query_everydayAlert] Script Date: 06/12/2017 06:20:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_query_everydayAlert] ( @companyId varchar(30), --公司编号 @Department varchar(30), --部门 @chk0 varchar(2), --排除死亡客户 4 @chk1 varchar(2) --排除休眠客户 3 ) as begin create table #gam23_alert --公告发布和各种提醒内容 ( gau00c varchar(10), --发送到那个公司 gau01c varchar(5), gau03c varchar(20), --会员卡号/03-04-员工编号/10-单据号/11--产品编号 gau04c varchar(20), --会员姓名/员工姓名 juli int , --多久没来 gau05d varchar(8) , --日期 gau06f float , --06-疗程剩余次数/05-卡剩余金额/11-产品数量 gau07c varchar(20), --家庭电话 gau08c varchar(20), --移动电话 gau09c varchar(160), --地址 gau10t varchar(6) , --预约时间/发送时间 gau11c varchar(30), --预约项目1/gx-项目编号/gx-产品编号 gau12c varchar(30), --预约项目2 gau13c varchar(30), --预约项目3 fzren1 varchar(20), -- 负责人1 fzrename1 varchar(20), -- 负责人姓名1 dj1 int, -- 会员等级1 djcode1 varchar(20), -- 会员等级代码1 Depart1 varchar(20), -- 部门编号1 cardsection1 varchar(20), -- 会员归属部门1 fzren2 varchar(20), -- 负责人2 fzrename2 varchar(20), -- 负责人姓名2 dj2 int, -- 会员等级2 djcode2 varchar(20), -- 会员等级代码2 Depart2 varchar(20), -- 部门编号2 cardsection2 varchar(20), -- 会员归属部门2 fzren3 varchar(20), -- 负责人3 fzrename3 varchar(20), -- 负责人姓名3 dj3 int, -- 会员等级3 djcode3 varchar(20), -- 会员等级代码3 Depart3 varchar(20), -- 部门编号3 cardsection3 varchar(20), -- 会员归属部门3 fzren4 varchar(20), -- 负责人4 fzrename4 varchar(20), -- 负责人姓名4 dj4 int, -- 会员等级4 djcode4 varchar(20), -- 会员等级代码4 Depart4 varchar(20), -- 部门编号4 cardsection4 varchar(20), -- 会员归属部门4 ) insert into #gam23_alert(gau00c,gau01c, gau03c, gau04c, gau05d, gau06f, gau07c, gau08c, gau09c,gau10t,gau11c, gau12c, gau13c) select gau00c,gau01c, gau03c, gau04c, gau05d, convert(decimal(18,2),gau06f), gau07c, gau08c, gau09c,gau10t,gau11c, gau12c, gau13c from gam23 where gau00c = @companyId --in (select gaz02c from gam26 where gaz01c = '001') and gau01c in ('5','7','8') create table #ggm01_alert ( gga00c varchar(20), gga05c varchar(30), juli int ) insert into #ggm01_alert select gga00c, gga05c, --会员卡号 -datediff(dd,getdate(),convert(datetime,max(gga94d))) juli --多久没来店 from ggm01 where gga00c = @companyId --in (select gaz02c from gam26 where gaz01c = @companyId) and gga05c in (select gau03c from #gam23_alert) and isnull(gga94d,'') != '' group by gga00c,gga05c update a set a.juli = b.juli from #gam23_alert a,#ggm01_alert b where gau00c = gga00c and gau03c = gga05c --更新会员等级,负责人 declare @gbr02c varchar(30) -- 所属部门 declare @intSn int set @intSn = 1 declare alter_cursor cursor for select gbr02c from gbm18 inner join #gam23_alert on gbr00c = gau00c and gbr01c = gau03c group by gbr02c open alter_cursor fetch next from alter_cursor into @gbr02c while(@@fetch_status = 0) begin if( @intSn = 1) begin update a set Depart1 = b.gbr02c, fzren1 = b.gbr03c, dj1 = b.gbr04i, cardsection1 = (select gsb03c from gsm02 where gsb00c = b.gbr00c and gsb01c = 'U' and gsb02c = b.gbr02c), fzrename1 = (select haa02c from ham01 where haa00c = b.gbr00c and haa01c = b.gbr03c), --负责人名称 djcode1 = (select gsb03c from gsm02 where gsb00c = b.gbr00c and gsb01c = 'DJ' and gsb02c = b.gbr04i) --会员等级 from #gam23_alert a, gbm18 b where a.gau00c = b.gbr00c and a.gau03c = b.gbr01c and b.gbr02c = @gbr02c end else if(@intSn = 2) begin update a set Depart2 = b.gbr02c, fzren2 = b.gbr03c, dj2 = b.gbr04i, cardsection2 = (select gsb03c from gsm02 where gsb00c = b.gbr00c and gsb01c = 'U' and gsb02c = b.gbr02c), fzrename2 = (select haa02c from ham01 where haa00c = b.gbr00c and haa01c = b.gbr03c), --负责人名称 djcode2 = (select gsb03c from gsm02 where gsb00c = b.gbr00c and gsb01c = 'DJ' and gsb02c = b.gbr04i) --会员等级 from #gam23_alert a, gbm18 b where a.gau00c = b.gbr00c and a.gau03c = b.gbr01c and b.gbr02c = @gbr02c end else if(@intSn = 3) begin update a set Depart3 = b.gbr02c, fzren3 = b.gbr03c, dj3 = b.gbr04i, cardsection3 = (select gsb03c from gsm02 where gsb00c = b.gbr00c and gsb01c = 'U' and gsb02c = b.gbr02c), fzrename3 = (select haa02c from ham01 where haa00c = b.gbr00c and haa01c = b.gbr03c), --负责人名称 djcode3 = (select gsb03c from gsm02 where gsb00c = b.gbr00c and gsb01c = 'DJ' and gsb02c = b.gbr04i) --会员等级 from #gam23_alert a, gbm18 b where a.gau00c = b.gbr00c and a.gau03c = b.gbr01c and b.gbr02c = @gbr02c end else if(@intSn = 4) begin update a set Depart4 = b.gbr02c, fzren4 = b.gbr03c, dj4 = b.gbr04i, cardsection4 = (select gsb03c from gsm02 where gsb00c = b.gbr00c and gsb01c = 'U' and gsb02c = b.gbr02c), fzrename4 = (select haa02c from ham01 where haa00c = b.gbr00c and haa01c = b.gbr03c), --负责人名称 djcode4 = (select gsb03c from gsm02 where gsb00c = b.gbr00c and gsb01c = 'DJ' and gsb02c = b.gbr04i) --会员等级 from #gam23_alert a, gbm18 b where a.gau00c = b.gbr00c and a.gau03c = b.gbr01c and b.gbr02c = @gbr02c end set @intSn = @intSn + 1 fetch next from alter_cursor into @gbr02c end close alter_cursor deallocate alter_cursor --结果 select * from #gam23_alert where (@Department = '' or ( isnull(Depart1,'') = @Department and (@chk0 = '' or isnull(dj1,'') != @chk0) and (@chk1 = '' or isnull(dj1,'') != @chk1) )) or (@Department = '' or ( isnull(Depart2,'') = @Department and (@chk0 = '' or isnull(dj2,'') != @chk0) and (@chk1 = '' or isnull(dj2,'') != @chk1) )) or (@Department = '' or ( isnull(Depart3,'') = @Department and (@chk0 = '' or isnull(dj3,'') != @chk0) and (@chk1 = '' or isnull(dj3,'') != @chk1) )) or (@Department = '' or ( isnull(Depart4,'') = @Department and (@chk0 = '' or isnull(dj4,'') != @chk0) and (@chk1 = '' or isnull(dj4,'') != @chk1) )) drop table #gam23_alert end GO /****** Object: Table [dbo].[gcm06_log] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gcm06_log]( [gcf00c] [varchar](10) NOT NULL, [gcf01c] [varchar](20) NOT NULL, [gcf02c] [varchar](20) NOT NULL, [gcf03f] [float] NULL, [gcf04f] [float] NULL, [gcf05f] [float] NULL, [gcf06f] [float] NULL, [gcf07f] [float] NULL, [gcf08f] [float] NULL, [gcf09f] [float] NULL, [gcf10f] [float] NULL, [gcf11d] [varchar](8) NULL, [gcf12d] [varchar](8) NULL, [gcf13i] [int] NULL, [gcf14f] [float] NULL, [gcf15c] [varchar](60) NULL, [gcf16i] [int] NULL, [gcf17i] [int] NOT NULL, [gcf18f] [float] NOT NULL, [gcf19i] [int] NULL, [gcf20i] [int] NULL, [gcf21i] [int] NULL, [gcf22i] [int] NULL, [gcf23i] [int] NOT NULL, [gcf24i] [int] NULL, [gcf25f] [float] NULL, [idd] [int] IDENTITY(1,1) NOT NULL, [compid] [varchar](10) NULL, [userid] [varchar](20) NULL, [ddate] [varchar](10) NULL, [ttime] [varchar](10) NULL, CONSTRAINT [PK_GCM06_LOG] PRIMARY KEY NONCLUSTERED ( [idd] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gicm10] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gicm10]( [gicj00c] [varchar](10) NOT NULL, [gicj01v] [varchar](20) NOT NULL, [gicj02v] [varchar](20) NOT NULL, [gicj03t] [varchar](6) NULL, [gicj04t] [varchar](6) NULL, [gicj05d] [varchar](8) NULL, [gicj06d] [varchar](8) NULL, [gicj07v] [varchar](20) NULL, [gicj08t] [varchar](6) NULL, [gicj09d] [varchar](8) NULL, [gicj10v] [varchar](240) NULL, [gicj11i] [int] IDENTITY(1,1) NOT NULL, [gicj12t] [varchar](6) NULL, [gicj13c] [varchar](20) NULL, CONSTRAINT [PK_GICM10] PRIMARY KEY CLUSTERED ( [gicj00c] ASC, [gicj11i] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gzm03] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gzm03]( [gzc00c] [varchar](10) NOT NULL, [gzc02d] [varchar](8) NOT NULL, [gzc03c] [varchar](30) NULL, [gzc04d] [varchar](8) NULL, [gzc05t] [varchar](6) NULL, CONSTRAINT [PK_gzm03] PRIMARY KEY CLUSTERED ( [gzc00c] ASC, [gzc02d] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_member_operation_details_group] Script Date: 06/12/2017 06:20:24 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_member_operation_details_group] ( @compid varchar(10), @memid varchar(20), @fromdate varchar(20), @todate varchar(20) ) as begin declare @incompid varchar(10) select @incompid = @compid if @compid = '001' select @compid = '%' if @fromdate = '' or @fromdate = '*' begin select @fromdate = '19900101' select @todate = '20901231' end create table #details ( seq int identity not null, action varchar(4) not null, item varchar(20) not null, name varchar(100) null, quan float null, -- 标准价格 amt float null, primary key(seq) ) if (@memid <> '') begin insert #details(action, item, name, quan, amt) select gct06c, gct07c, gct08c, sum(gct11f), sum(gct12f) from gcm20 with (nolock), gam26 z where gct03c = @memid and gct00c = z.gaz02c and z.gaz01c = @incompid and gct04d >= @fromdate and gct04d <= @todate group by gct06c, gct07c, gct08c end update #details set action = '产品' where action = '4' update #details set action = '消费' where action = '3' select * from #details order by seq drop table #details end GO /****** Object: Table [dbo].[gcm05] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gcm05]( [gce00c] [varchar](10) NOT NULL, [gce01c] [varchar](20) NOT NULL, [gce02c] [varchar](20) NOT NULL, [gce03i] [int] NULL, [gce04c] [varchar](20) NULL, CONSTRAINT [PK_GCM05] PRIMARY KEY CLUSTERED ( [gce00c] ASC, [gce01c] ASC, [gce02c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_cal_staff_consume_cost] Script Date: 06/12/2017 06:19:34 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_cal_staff_consume_cost] ( @compid varchar(10), @staff_id varchar(20), @fromdate varchar(10), @todate varchar(10), @consume_cost float output ) as begin select @consume_cost = 0 create table #tbl_staff_cost ( goodid varchar(20) not null, quan float null, cost float null, primary key(goodid) ) insert #tbl_staff_cost(goodid, quan) select gge03c, sum(isnull(gge05f, 0)) gge05f from ggm05 where gge12c = @staff_id and gge01c in (select gga01c from ggm01 with(nolock) where gga00c in (select gaz02c from gam26 where gaz01c = @compid) and gga02d >= @fromdate and gga02d <= @todate) group by gge03c update a set a.cost = isnull(a.quan, 0) * isnull(gfa15f, 0) from #tbl_staff_cost a, gfm01 b where a.goodid = b.gfa01c and b.gfa00c = @compid select @consume_cost = sum(isnull(cost, 0)) from #tbl_staff_cost drop table #tbl_staff_cost end GO /****** Object: Table [dbo].[wip] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[wip]( [wip00] [int] IDENTITY(1,1) NOT NULL, [wip01v] [varchar](50) NULL, [wip02v] [text] NULL, [wip03v] [varchar](20) NULL, [wip04v] [varchar](20) NULL, [wip05i] [int] NULL, [wip06i] [int] NULL, [wip07i] [int] NULL, [wip08v] [varchar](20) NULL, [wip09v] [varchar](20) NULL, [wip10v] [varchar](20) NULL, CONSTRAINT [PK_WIP] PRIMARY KEY CLUSTERED ( [wip00] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[glm03] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[glm03]( [glc00c] [varchar](10) NOT NULL, [glc01c] [varchar](20) NOT NULL, [glc02c] [varchar](20) NOT NULL, [glc03f] [float] NULL, [glc04f] [float] NULL, [glc05i] [int] NULL, [glc06f] [float] NULL, CONSTRAINT [PK_GLM03] PRIMARY KEY CLUSTERED ( [glc00c] ASC, [glc01c] ASC, [glc02c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[loop_event_log] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[loop_event_log]( [compid] [varchar](10) NOT NULL, [idd] [int] IDENTITY(1,1) NOT NULL, [cardid] [varchar](20) NOT NULL, [acctid] [int] NULL, [startflag] [int] NULL, [ddate] [varchar](10) NULL, [ttime] [varchar](10) NULL, [seqid] [int] NULL, [mmonth] [varchar](10) NULL, [addflag] [int] NULL, [amt] [float] NULL, [billtype] [varchar](10) NULL, [billid] [varchar](20) NULL, PRIMARY KEY CLUSTERED ( [idd] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gcm12] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gcm12]( [gcn00c] [varchar](10) NOT NULL, [gcn01c] [varchar](20) NOT NULL, [gcn02c] [varchar](50) NULL, [gcn03c] [varchar](200) NULL, [gcn04c] [varchar](20) NULL, [gcn05c] [varchar](10) NULL, [gcn06i] [tinyint] NULL, [gcn07c] [varchar](20) NULL, [gcn08c] [varchar](160) NULL, [gcn09f] [float] NULL, [gcn10i] [int] NULL, [gcn11i] [int] NULL, [gcn12d] [varchar](8) NULL, [gcn13d] [varchar](8) NULL, [gcn14i] [int] NULL, [gcn15c] [varchar](20) NULL, [gcn16f] [float] NULL, [gcn17c] [varchar](10) NULL, [gcn18f] [float] NULL, [gcn19f] [float] NULL, [gcn20f] [float] NULL, [gcn21f] [float] NULL, [gcn22i] [int] NULL, [gcn23c] [varchar](30) NULL, [gcn24i] [int] NULL, [gcn25c] [varchar](60) NULL, [gcn26f] [float] NULL, [gcn27c] [varchar](60) NULL, [gcn28c] [varchar](60) NULL, [gcn29c] [varchar](60) NULL, [gcn30c] [varchar](60) NULL, [gcn31c] [varchar](60) NULL, [gcn32c] [varchar](60) NULL, [gcn33c] [varchar](20) NULL, [gcn34c] [varchar](10) NULL, [gcn35c] [varchar](10) NULL, [gcn36i] [int] NULL, [gcn37c] [varchar](1000) NULL, [gcn38f] [float] NULL, [gcn39f] [float] NULL, [gcn40c] [varchar](60) NULL, [gcn41c] [varchar](60) NULL, CONSTRAINT [PK_GCM12] PRIMARY KEY CLUSTERED ( [gcn00c] ASC, [gcn01c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_stock_due_statistics] Script Date: 06/12/2017 06:20:45 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_stock_due_statistics] ( @compid varchar(10), --Company ID @date varchar(10), --Date @duedays varchar(20), --Due days @goodsfrom varchar(20), --goods No. for 'from' @goodsto varchar(20), --goods No. for 'to' @storagefrom varchar(20), --Storage No. for 'from' @storageto varchar(20) --Storage No. for 'to' ) as begin create table #duestock ( idd int identity(1, 1) not null, --Record ID goodsno varchar(20) null, --goods No. goodsname varchar(80) null, --goods name storageno varchar(20) null, --Storage No. storagename varchar(20) null, --Storage Name goodstype varchar(40) null, --goods type instorepatch varchar(20) null, --Bill No. of instore as patch No. curquantity float null, --Current quantity of the goods unit varchar(10) null, --goods' unit stayperiod int null, --Stay period of the goods duedate varchar(10) null, --Due date of the goods remaindays int null --Remain days away from stay period ) if isnull(@goodsfrom, '') = '' set @goodsfrom = '*' if isnull(@storagefrom, '') = '' set @storagefrom = '*' if isnull(@duedays, '') = '' or isnull(@duedays, '') = '*' begin insert into #duestock(goodsno, goodsname, storageno, storagename, goodstype, instorepatch, curquantity, unit, stayperiod, duedate, remaindays) select ghc01c, gfa03c, ghc06c, gad02c, gsb03c, ghc02c, ghc05f, gfa07c, gfa28i, ghc04d, datediff(dd, cast(@date as datetime), cast(ghc04d as datetime)) from ghm03, gam04, gsm02, gfm01 where ghc00c = gad00c and ghc00c = gsb00c and ghc00c = gfa00c and ghc00c = @compid and ghc01c = gfa01c and ghc06c = gad01c and gfa05c = gsb02c and gsb01c = 'F' and ((ghc01c between @goodsfrom and @goodsto) or(@goodsfrom = '*')) and ((ghc06c between @storagefrom and @storageto) or (@storagefrom = '*')) order by ghc04d asc end else begin insert into #duestock(goodsno, goodsname, storageno, storagename, goodstype, instorepatch, curquantity, unit, stayperiod, duedate, remaindays) select ghc01c, gfa03c, ghc06c, gad02c, gsb03c, ghc02c, ghc05f, gfa07c, gfa28i, ghc04d, datediff(dd, cast(@date as datetime), cast(ghc04d as datetime)) from ghm03, gam04, gsm02, gfm01 where ghc00c = gad00c and ghc00c = gsb00c and ghc00c = gfa00c and ghc00c = @compid and ghc01c = gfa01c and ghc06c = gad01c and gfa05c = gsb02c and gsb01c = 'F' and ((ghc01c between @goodsfrom and @goodsto) or(@goodsfrom = '*')) and ((ghc06c between @storagefrom and @storageto) or (@storagefrom = '*')) and ghc04d <= convert(varchar(8), dateadd(dd, cast(isnull(@duedays, 0) as int), cast(@date as datetime)), 112) order by ghc04d asc end update #duestock set unit = gsb03c from #duestock, gsm02 where gsb00c = @compid and gsb01c = 'K' and unit = gsb02c select isnull(idd,'') idd, isnull(goodsno,'') goodsno, isnull(goodsname,'') goodsname, isnull(storageno,'') storageno, isnull(storagename,'') storagename, isnull(goodstype,'') goodstype, isnull(instorepatch,'') instorepatch, isnull(curquantity,'') curquantity, isnull(unit,'') unit, isnull(stayperiod,'') stayperiod, isnull(duedate,'') duedate, isnull(remaindays,'') remaindays from #duestock drop table #duestock end GO /****** Object: Table [dbo].[wft] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[wft]( [wft00] [int] IDENTITY(1,1) NOT NULL, [wft01v] [varchar](50) NULL, [wft02v] [varchar](200) NULL, [wft03b] [image] NULL, [wft04v] [varchar](20) NULL, [wft05v] [varchar](20) NULL, [wft06v] [varchar](20) NULL, [wft07v] [varchar](20) NULL, [wft08v] [varchar](20) NULL, [wft09v] [varchar](20) NULL, [wft10v] [varchar](20) NULL, CONSTRAINT [PK_WFT] PRIMARY KEY CLUSTERED ( [wft00] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[ham07] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[ham07]( [hag00c] [varchar](10) NOT NULL, [hag01c] [varchar](10) NOT NULL, [hag02f] [float] NOT NULL, [hag03c] [varchar](30) NULL, [hag04f] [float] NULL, [hag05d] [varchar](8) NULL, [hag06f] [float] NULL, [hag07d] [varchar](8) NULL, [hag08f] [float] NULL, CONSTRAINT [PK_HAM07] PRIMARY KEY CLUSTERED ( [hag00c] ASC, [hag01c] ASC, [hag02f] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_init_gicm13] Script Date: 06/12/2017 06:20:14 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_init_gicm13] ( @compid varchar(10), @curdate varchar(10) ) as begin create table #gicm13_buf ( gick00c varchar(10) not null, -- 公司编号 gick01i int identity(1, 1) not null, gick02i int not null, -- 自动手动添加还是从考勤载入 0--自己手动添加 1--从考勤载入 gick03c varchar(20) null, -- 班次 gick04c varchar(20) null, -- 员工编号 gick05c varchar(20) null, -- 员工姓名 gick06i int null, -- 性别 0-女 1-男 gick07d varchar(8) null, -- 日期 gick08i int null -- 轮排顺序 ) insert #gicm13_buf(gick00c, gick02i, gick03c, gick04c, gick05c, gick06i, gick07d, gick08i) select @compid, 1, '', hda01c, haa02c, haa05i, @curdate, 0 from hdm01 a, ham01 b where hda00c = @compid and hda02d = @curdate and a.hda00c = b.haa00c and a.hda01c = b.haa01c order by hda18c, hda03t -- 删除已经在gicm13中存在的 delete a from #gicm13_buf a, gicm13 b where a.gick00c = b.gick00c and a.gick07d = b.gick07d and a.gick04c = b.gick04c insert gicm13(gick00c, gick02i, gick03c, gick04c, gick05c, gick06i, gick07d, gick08i) select gick00c, gick02i, gick03c, gick04c, gick05c, gick06i, gick07d, gick08i from #gicm13_buf order by gick01i drop table #gicm13_buf end GO /****** Object: Table [dbo].[glm09] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[glm09]( [glk00c] [varchar](10) NOT NULL, [glk01c] [varchar](20) NOT NULL, [glk02i] [int] IDENTITY(1,1) NOT NULL, [glk03d] [varchar](8) NULL, [glk04t] [varchar](6) NULL, [glk05c] [varchar](20) NULL, [glk06c] [varchar](20) NULL, [glk07f] [float] NULL, [glk08c] [varchar](20) NULL, [glk09c] [varchar](20) NULL, [glk10c] [varchar](20) NULL, [glk11i] [int] NULL, CONSTRAINT [PK_GLM09] PRIMARY KEY CLUSTERED ( [glk00c] ASC, [glk01c] ASC, [glk02i] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[fex02] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[fex02]( [feb00c] [varchar](10) NOT NULL, [feb01c] [varchar](20) NOT NULL, [feb02f] [float] NOT NULL, [feb03c] [varchar](20) NULL, [feb04c] [varchar](5) NULL, [feb05f] [float] NULL, [feb06i] [int] NULL, [feb07c] [varchar](20) NULL, [feb08f] [float] NULL, [feb09f] [float] NULL, [feb10f] [float] NULL, CONSTRAINT [PK_FEX02] PRIMARY KEY CLUSTERED ( [feb00c] ASC, [feb01c] ASC, [feb02f] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[empworks] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[empworks]( [compid] [varchar](10) NOT NULL, [empid] [varchar](20) NOT NULL, [wid] [varchar](20) NOT NULL, [wtitle] [varchar](100) NULL, [wdesc] [varchar](500) NULL, [wimg] [image] NULL, [wdate] [varchar](8) NULL, [wtime] [varchar](6) NULL, CONSTRAINT [PK_empworks] PRIMARY KEY CLUSTERED ( [compid] ASC, [empid] ASC, [wid] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[s3_cardtype_itemtype_rate] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[s3_cardtype_itemtype_rate]( [compid] [varchar](10) NOT NULL, [cardtype] [varchar](20) NOT NULL, [kind] [int] NOT NULL, [itemtype] [varchar](20) NOT NULL, [rate] [float] NULL, CONSTRAINT [PK_CardType_ItemType] PRIMARY KEY CLUSTERED ( [compid] ASC, [cardtype] ASC, [kind] ASC, [itemtype] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gtc03] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gtc03]( [gtd00c] [varchar](10) NOT NULL, [gtd01c] [varchar](20) NOT NULL, [gtd02c] [varchar](20) NOT NULL, [gtd03f] [float] NULL, [gtd04f] [float] NULL, [gtd05c] [varchar](20) NULL, CONSTRAINT [PK_GTC03] PRIMARY KEY CLUSTERED ( [gtd00c] ASC, [gtd01c] ASC, [gtd02c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[ham12] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[ham12]( [hal01c] [varchar](20) NOT NULL, [hal02i] [int] IDENTITY(1,1) NOT NULL, [hal03c] [varchar](10) NULL, [hal04c] [varchar](20) NULL, [hal05c] [varchar](5) NULL, [hal06c] [varchar](10) NULL, [hal07c] [varchar](20) NULL, [hal08c] [varchar](5) NULL, [hal09c] [varchar](80) NULL, CONSTRAINT [PK_HAM12] PRIMARY KEY NONCLUSTERED ( [hal02i] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gnm01_SendAcct] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gnm01_SendAcct]( [gns00c] [varchar](10) NOT NULL, [gns01c] [varchar](20) NOT NULL, [gns02c] [varchar](20) NOT NULL, [gns03c] [varchar](20) NOT NULL, [gns04f] [float] NULL, CONSTRAINT [PK_gnm01_SendAcct] PRIMARY KEY CLUSTERED ( [gns00c] ASC, [gns01c] ASC, [gns02c] ASC, [gns03c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[calc_instant] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[calc_instant]( [inst_id] [int] IDENTITY(1,1) NOT NULL, [inst_comp] [varchar](10) NOT NULL, [calc_type] [int] NOT NULL, [calc_comp] [varchar](10) NOT NULL, [calc_time] [datetime] NOT NULL, [fromdate] [varchar](8) NOT NULL, [todate] [varchar](8) NOT NULL, [success] [int] NULL, [consumed] [float] NULL, CONSTRAINT [PK_calc_instant] PRIMARY KEY CLUSTERED ( [inst_id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_consume_statistic_gdbs] Script Date: 06/12/2017 06:19:46 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO Create procedure [dbo].[upg_consume_statistic_gdbs] ( @compid varchar(10), --门店编号 @fromdate varchar(8), --开始日期 @todate varchar(8), --结束日期 @frombill varchar(20), --开始单号 @tobill varchar(20), --结束单号 @fromcard varchar(20), --开始卡号 @tocard varchar(20) --结束卡号 ) as begin begin try create table #result -- 卖卡_充值_消费临时明细表 ( id int identity not null, compid varchar(10) null, -- 门店 customer varchar(10) null, -- 新老客 checkdate varchar(10) null, -- 消费日期 checktime varchar(8) null, -- 消费时间 cardno varchar(20) null, -- 会员卡编号 cardtypecode varchar(100) null, -- 卡类别代码 m_name varchar(100) null, -- 会员名称 gender varchar(10) null, -- 性别 billid varchar(20) null, -- 单号 billtype varchar(10) null, -- 单据别 type_gz_gn varchar(5) null, -- 单据类型(gn,gz,gx) gn卖卡 gz充值 gx消费 projectcode varchar(20) null, -- 项目编号 projectname varchar(100) null, -- 项目名称 projectclass varchar(50) null, -- 项目类别 department1 varchar(100) null, -- 部门1 department2 varchar(100) null, -- 部门2 department3 varchar(100) null, -- 部门3 department4 varchar(100) null, -- 部门4 types1 varchar(20) null, -- 大工类型点/轮 employee1 varchar(20) null, -- 大工姓名 ratio1 float null, -- 大工 分享比率 项目给死 1 num1 float null, -- 数量 types2 varchar(20) null, -- 中工类型点/轮 employee2 varchar(20) null, -- 中工姓名 ratio2 float null, -- 中工 分享比率 项目给死 1 num2 float null, -- 数量 types3 varchar(20) null, -- 小工类型点/轮 employee3 varchar(20) null, -- 小工姓名 ratio3 float null, -- 小工 分享比率 项目给死 1 num3 float null, -- 数量 types4 varchar(20) null, -- 四工类型点/轮 employee4 varchar(20) null, -- 四工姓名 ratio4 float null, -- 四工 分享比率 项目给死 1 num4 float null, -- 数量 pay_# float null, -- 支付 # pay_$ float null, -- 支付 $ pay_$1 float null, -- 支付 $1 pay_$2 float null, -- 支付 $2 pay_$3 float null, -- 支付 $3 pay_$4 float null, -- 支付 $4 pay_$5 float null, -- 支付 $5 pay_$6 float null, -- 支付 $6 pay_$7 float null, -- 支付 $7 pay_$8 float null, -- 支付 $8 [pay_&] float null, -- 支付 & pay_0 float null, -- 支付 0 pay_1 float null, -- 支付 1 pay_2 float null, -- 支付 2 pay_3 float null, -- 支付 3 pay_4 float null, -- 支付 4 pay_5 float null, -- 支付 5 pay_6 float null, -- 支付 6 pay_7 float null, -- 支付 7 pay_8 float null, -- 支付 8 pay_9 float null, -- 支付 9 pay_A float null, -- 支付 A pay_B float null, -- 支付 B pay_C float null, -- 支付 C pay_D float null, -- 支付 D pay_E float null, -- 支付 E pay_WX float null, -- 支付 WX pay_ZF float null, -- 支付 ZF pay_ZFB float null, -- 支付 ZFB pay_lv varchar(1000) null, -- 支付方式明细 price float null, -- 单价 quantity varchar(200) null, -- 数量占比 summoney float null, -- 总金额 traaccount varchar(100) null, -- 异动账户 trasummoney float null, -- 异动金额 mark varchar(2000) null, -- 备注 primary key (id) ) create nonclustered index idx_result on #result(compid,billid) -- 项目大类 gsm02 create table #gsm02 ( gsb00c varchar(10) not null, -- 门店编号 gsb01c varchar(10) not null, -- 类别 gsb02c varchar(5) not null, -- 类别字段代码值 gsb03c varchar(40) null -- 类别字段显示Text ) create clustered index idx_gsm02 on #gsm02(gsb00c,gsb01c, gsb02c) insert into #gsm02(gsb00c,gsb01c,gsb02c,gsb03c) select gsb00c,gsb01c,gsb02c,gsb03c from gsm02 with(nolock) where gsb00c = @compid and gsb01c in('AJ','U','L','B','O','3','AL') -- 支付方式账户 create table #gsm14 ( gsn01c varchar(5) not null, -- 支付方式 gsn02c varchar(20) null, -- 支付方式中文名 gsn03c varchar(50) null -- 账户 ) insert #gsm14(gsn01c,gsn03c) select gsn01c,gsb03c from gsm14 a with(nolock) left join #gsm02 b on a.gsn01c = b.gsb02c and b.gsb01c = 'O' -- 更新支付方式中文名 update a set a.gsn02c = gsb03c from #gsm14 a,#gsm02 b where a.gsn01c = b.gsb02c and b.gsb01c = '3' create table #ham01 ( haa00c varchar(10) not null, --门店编号 haa01c varchar(10) not null, --员工编号 haa02c varchar(20) null, --员工姓名 haa06c varchar(10) null --部门编号 ) create clustered index idx_ham01 on #ham01(haa00c,haa01c) insert into #ham01(haa00c,haa01c,haa02c,haa06c) select haa00c,haa01c,haa02c,haa06c from ham01 a with(nolock) where a.haa00c = @compid create table #ggm02 ( ggb00c varchar(10) not null, --门店编号 ggb01c varchar(20) not null, --消费单号 ggb02f float not null, --序号 [type] varchar(10) not null, -- 消费类型 ggb03c varchar(20) null, --项目代码 ggb12c varchar(20) null, --大工代码 ggb13c varchar(20) null, --中工代码 ggb14c varchar(20) null, --小工代码 ggb26c varchar(20) null, --四工代码 ggb12c_name varchar(50) null, --大工姓名 ggb13c_name varchar(50) null, --中工姓名 ggb14c_name varchar(50) null, --小工姓名 ggb26c_name varchar(50) null, --四工姓名 department1 varchar(100) null, -- 部门1 department2 varchar(100) null, -- 部门2 department3 varchar(100) null, -- 部门3 department4 varchar(100) null, -- 部门4 bmlx varchar(100) null, -- 员工类型 ggb15c varchar(20) null, --大工类型 ggb16c varchar(20) null, --中工类型 ggb19c varchar(20) null, --小工类型 ggb25c varchar(20) null, --四工类型 dmployeetype1 varchar(100) null, -- 员工类型 dmployeetype2 varchar(100) null, -- 员工类型 dmployeetype3 varchar(100) null, -- 员工类型 dmployeetype4 varchar(100) null, -- 员工类型 pay_# float null, -- 支付 # pay_$ float null, -- 支付 $ pay_$1 float null, -- 支付 $1 pay_$2 float null, -- 支付 $2 pay_$3 float null, -- 支付 $3 pay_$4 float null, -- 支付 $4 pay_$5 float null, -- 支付 $5 pay_$6 float null, -- 支付 $6 pay_$7 float null, -- 支付 $7 pay_$8 float null, -- 支付 $8 [pay_&] float null, -- 支付 & pay_0 float null, -- 支付 0 pay_1 float null, -- 支付 1 pay_2 float null, -- 支付 2 pay_3 float null, -- 支付 3 pay_4 float null, -- 支付 4 pay_5 float null, -- 支付 5 pay_6 float null, -- 支付 6 pay_7 float null, -- 支付 7 pay_8 float null, -- 支付 8 pay_9 float null, -- 支付 9 pay_A float null, -- 支付 A pay_B float null, -- 支付 B pay_C float null, -- 支付 C pay_D float null, -- 支付 D pay_E float null, -- 支付 E pay_WX float null, -- 支付 WX pay_ZF float null, -- 支付 ZF pay_ZFB float null -- 支付 ZFB ) -- 项目消费 insert #ggm02(ggb00c,ggb01c,ggb02f,[type],ggb03c,ggb12c,ggb13c,ggb14c,ggb26c,ggb15c,ggb16c,ggb19c,ggb25c) select ggb00c,ggb01c,ggb02f,'gx',ggb03c,ggb12c,ggb13c,ggb14c,ggb26c,ggb15c,ggb16c,ggb19c,ggb25c from ggm01 a with(nolock),ggm02 b with(nolock) where a.gga00c = b.ggb00c and a.gga01c = b.ggb01c and a.gga00c = @compid and (isnull(@frombill,'*') = '*' or a.gga01c between @frombill and @tobill) and (isnull(@fromcard,'*') = '*' or a.gga09c between @fromcard and @tocard) and a.gga02d between @fromdate and @todate -- 产品消费 insert #ggm02(ggb00c,ggb01c,ggb02f,[type],ggb03c,ggb12c,ggb13c,ggb14c,ggb26c) select ggc00c,ggc01c,ggc02f,'cp',ggc03c,ggc12c,ggc15c,ggc17c,'' from ggm01 a with(nolock),ggm03 b with(nolock) where a.gga00c = b.ggc00c and a.gga01c = b.ggc01c and a.gga00c = @compid and (isnull(@frombill,'*') = '*' or a.gga01c between @frombill and @tobill) and (isnull(@fromcard,'*') = '*' or a.gga09c between @fromcard and @tocard) and a.gga02d between @fromdate and @todate -- 卖卡的 gn insert #ggm02(ggb00c,ggb01c,ggb02f,[type],ggb03c,ggb12c,ggb13c,ggb14c,ggb26c,ggb15c,ggb16c,ggb19c,ggb25c) select gna00c,gna01c,0,'gn','',gna20c,gna28c,gna30c,gna32c,'','','','' from gnm01 a with(nolock),gnm02 b with(nolock) where a.gna00c = b.gnb00c and a.gna01c = b.gnb01c and a.gna00c = @compid and (isnull(@frombill,'*') = '*' or a.gna01c between @frombill and @tobill) and (isnull(@fromcard,'*') = '*' or b.gnb03c between @fromcard and @tocard) and a.gna02d between @fromdate and @todate -- 卖卡套餐 gntc insert #ggm02(ggb00c,ggb01c,ggb02f,[type],ggb03c,ggb12c,ggb13c,ggb14c,ggb26c,ggb15c,ggb16c,ggb19c,ggb25c) select gna00c,gna01c,0,'gntc',gtf02c,gna20c,gna28c,gna30c,gna32c,'','','','' from gnm01 a with(nolock),gtc05 b with(nolock),gnm02 c with(nolock) where a.gna00c = b.gtf00c and a.gna01c = b.gtf01c and a.gna00c = c.gnb00c and a.gna01c = c.gnb01c and a.gna00c = @compid and (isnull(@frombill,'*') = '*' or a.gna01c between @frombill and @tobill) and (isnull(@fromcard,'*') = '*' or c.gnb03c between @fromcard and @tocard) and a.gna02d between @fromdate and @todate -- 卖卡套餐 gntc insert #ggm02(ggb00c,ggb01c,ggb02f,[type],ggb03c,ggb12c,ggb13c,ggb14c,ggb26c,ggb15c,ggb16c,ggb19c,ggb25c) select gna00c,gna01c,0,'gnlc',gnc02c,gna20c,gna28c,gna30c,gna32c,'','','','' from gnm01 a with(nolock),gnm02 c with(nolock),gnm03 d with(nolock) where a.gna00c = c.gnb00c and a.gna01c = c.gnb01c and a.gna00c = d.gnc00c and a.gna01c = d.gnc01c and a.gna00c = @compid and (isnull(@frombill,'*') = '*' or a.gna01c between @frombill and @tobill) and (isnull(@fromcard,'*') = '*' or c.gnb03c between @fromcard and @tocard) and a.gna02d between @fromdate and @todate -- 充值的 gz insert #ggm02(ggb00c,ggb01c,ggb02f,[type],ggb03c,ggb12c,ggb13c,ggb14c,ggb26c,ggb15c,ggb16c,ggb19c,ggb25c) select gcl00c,gcl01c,0,'gz','',gcl09c,gcl17c,gcl19c,gcl22c,'','','','' from gcm10 a with(nolock) where a.gcl00c = @compid and gcl06i = 0 and (isnull(@frombill,'*') = '*' or a.gcl01c between @frombill and @tobill) and (isnull(@fromcard,'*') = '*' or a.gcl03c between @fromcard and @tocard) and a.gcl02d between @fromdate and @todate --充值套餐 gztc insert #ggm02(ggb00c,ggb01c,ggb02f,[type],ggb03c,ggb12c,ggb13c,ggb14c,ggb26c,ggb15c,ggb16c,ggb19c,ggb25c) select distinct gcl00c,gcl01c,0,'gztc',gth02c,gcl09c,gcl17c,gcl19c,gcl22c,'','','','' from gcm10 a with(nolock),gtc06 b with(nolock) where a.gcl00c = b.gth00c and a.gcl01c = b.gth01c and a.gcl00c = @compid and gcl06i = 0 and (isnull(@frombill,'*') = '*' or a.gcl01c between @frombill and @tobill) and (isnull(@fromcard,'*') = '*' or a.gcl03c between @fromcard and @tocard) and a.gcl02d between @fromdate and @todate -- 取款的 qk insert #ggm02(ggb00c,ggb01c,ggb02f,[type],ggb03c,ggb12c,ggb13c,ggb14c,ggb26c,ggb15c,ggb16c,ggb19c,ggb25c) select gcl00c,gcl01c,0,'qk','',gcl09c,gcl17c,gcl19c,gcl22c,'','','','' from gcm10 a with(nolock) where a.gcl00c = @compid and gcl06i = 1 and (isnull(@frombill,'*') = '*' or a.gcl01c between @frombill and @tobill) and (isnull(@fromcard,'*') = '*' or a.gcl03c between @fromcard and @tocard) and a.gcl02d between @fromdate and @todate -- 还款的 hk insert #ggm02(ggb00c,ggb01c,ggb02f,[type],ggb03c,ggb12c,ggb13c,ggb14c,ggb26c,ggb15c,ggb16c,ggb19c,ggb25c) select gcl00c,gcl01c,0,'hk','',gcl09c,gcl17c,gcl19c,gcl22c,'','','','' from gcm10 a with(nolock) where a.gcl00c = @compid and gcl06i = 6 and (isnull(@frombill,'*') = '*' or a.gcl01c between @frombill and @tobill) and (isnull(@fromcard,'*') = '*' or a.gcl03c between @fromcard and @tocard) and a.gcl02d between @fromdate and @todate and a.gcl04c <> '888' -- 项目还款的 xmhk insert #ggm02(ggb00c,ggb01c,ggb02f,[type],ggb03c,ggb12c,ggb13c,ggb14c,ggb26c,ggb15c,ggb16c,ggb19c,ggb25c) select distinct gcl00c,gcl01c,0,'xmhk','',gcl09c,gcl17c,gcl19c,gcl22c,'','','','' from gcm10 a with(nolock),ggm02 b with(nolock) where a.gcl00c = b.ggb00c and a.gcl01c = b.ggb42c and a.gcl00c = @compid and a.gcl06i = 6 and (isnull(@frombill,'*') = '*' or a.gcl01c between @frombill and @tobill) and (isnull(@fromcard,'*') = '*' or a.gcl03c between @fromcard and @tocard) and a.gcl02d between @fromdate and @todate and a.gcl04c = '888' -- 产品还款的 cphk insert #ggm02(ggb00c,ggb01c,ggb02f,[type],ggb03c,ggb12c,ggb13c,ggb14c,ggb26c,ggb15c,ggb16c,ggb19c,ggb25c) select distinct gcl00c,gcl01c,0,'cphk','',gcl09c,gcl17c,gcl19c,gcl22c,'','','','' from gcm10 a with(nolock),ggm03 b with(nolock) where a.gcl00c = b.ggc00c and a.gcl01c = b.ggc34c and a.gcl00c = @compid and a.gcl06i = 6 and (isnull(@frombill,'*') = '*' or a.gcl01c between @frombill and @tobill) and (isnull(@fromcard,'*') = '*' or a.gcl03c between @fromcard and @tocard) and a.gcl02d between @fromdate and @todate and a.gcl04c = '888' -- 疗程的 insert #ggm02(ggb00c,ggb01c,ggb02f,[type],ggb03c,ggb12c,ggb13c,ggb14c,ggb26c,ggb15c,ggb16c,ggb19c,ggb25c) select gcm00c,gcm01c,isnull(gcm25i,0),'lc',gcm02c,gcl09c,gcl17c,gcl19c,gcl22c,'','','','' from gcm10 a with(nolock),gcm11 b with(nolock) where a.gcl00c = b.gcm00c and a.gcl01c = b.gcm01c and a.gcl00c = @compid and gcl06i = 0 and (isnull(@frombill,'*') = '*' or a.gcl01c between @frombill and @tobill) and (isnull(@fromcard,'*') = '*' or a.gcl03c between @fromcard and @tocard) and a.gcl02d between @fromdate and @todate -- 翻译员工姓名 和 部门编号 create table #temp ( bz float not null, -- 标志 1 大工 2 中工 3 小工 4 四工 ggb00c varchar(10) not null, -- 门店编号 ggb01c varchar(20) not null, -- 消费单号 ratio varchar(20) null, -- 员工编号 name varchar(50) null, -- 员工姓名 department varchar(100) null, -- 部门 dmployeetype varchar(100) null -- 员工类型 ) create nonclustered index idx_temp on #temp(bz,ggb00c,ggb01c,ratio) insert #temp(bz,ggb00c,ggb01c,ratio,name,department,dmployeetype) select 1 bz,ggb00c,ggb01c, ggb12c,haa02c,gsb03c,(select gsb03c from #gsm02 d where d.gsb00c = a.ggb00c and d.gsb02c = a.ggb15c and d.gsb01c = 'B') from #ggm02 a,#ham01 b,gsm02 c with(nolock) where a.ggb00c = b.haa00c and a.ggb12c = b.haa01c and b.haa00c = c.gsb00c and b.haa06c = c.gsb02c and c.gsb01c = 'U' union all --中工 select 2 bz,ggb00c,ggb01c, ggb13c,haa02c,gsb03c,(select gsb03c from #gsm02 d where d.gsb00c = a.ggb00c and d.gsb02c = a.ggb16c and d.gsb01c = 'B') from #ggm02 a,#ham01 b,gsm02 c with(nolock) where a.ggb00c = b.haa00c and a.ggb13c = b.haa01c and b.haa00c = c.gsb00c and b.haa06c = c.gsb02c and c.gsb01c = 'U' union all --小工 select 3 bz,ggb00c,ggb01c, ggb14c,haa02c,gsb03c,(select gsb03c from #gsm02 d where d.gsb00c = a.ggb00c and d.gsb02c = a.ggb19c and d.gsb01c = 'B') from #ggm02 a,#ham01 b,gsm02 c with(nolock) where a.ggb00c = b.haa00c and a.ggb14c = b.haa01c and b.haa00c = c.gsb00c and b.haa06c = c.gsb02c and c.gsb01c = 'U' union all --四工 select 4 bz,ggb00c,ggb01c, ggb26c,haa02c,gsb03c,(select gsb03c from #gsm02 d where d.gsb00c = a.ggb00c and d.gsb02c = a.ggb25c and d.gsb01c = 'B') from #ggm02 a,#ham01 b,gsm02 c with(nolock) where a.ggb00c = b.haa00c and a.ggb26c = b.haa01c and b.haa00c = c.gsb00c and b.haa06c = c.gsb02c and c.gsb01c = 'U' -- 更新大工员工姓名 和 部门编号 update a set a.ggb12c_name = b.name, a.department1 = b.department, a.dmployeetype1 = b.dmployeetype from #ggm02 a,#temp b where a.ggb00c = b.ggb00c and a.ggb01c = b.ggb01c and a.ggb12c = b.ratio and b.bz = 1 -- 更新中工员工姓名 和 部门编号 update a set a.ggb13c_name = b.name, a.department2 = b.department, a.dmployeetype2 = b.dmployeetype from #ggm02 a,#temp b where a.ggb00c = b.ggb00c and a.ggb01c = b.ggb01c and a.ggb13c = b.ratio and b.bz = 2 -- 更新小工员工姓名 和 部门编号 update a set a.ggb14c_name = b.name, a.department3 = b.department, a.dmployeetype3 = b.dmployeetype from #ggm02 a,#temp b where a.ggb00c = b.ggb00c and a.ggb01c = b.ggb01c and a.ggb14c = b.ratio and b.bz = 3 -- 更新四工员工姓名 和 部门编号 update a set a.ggb26c_name = b.name, a.department4 = b.department, a.dmployeetype4 = b.dmployeetype from #ggm02 a,#temp b where a.ggb00c = b.ggb00c and a.ggb01c = b.ggb01c and a.ggb26c = b.ratio and b.bz = 4 --支付明细表 create table #pays ( compid varchar(10) not null, -- 门店编号 ggb01c varchar(20) not null, -- 消费单号 ggb03c varchar(20) null, -- 项目代码 ggb02f float null, --序号 [type] varchar(10) null, -- 消费类型 pay varchar(10) null, -- 支付方式 amount float null -- 支付金额 ) create nonclustered index idx_pays on #pays(compid,ggb01c,ggb03c,pay) -- 项目的 insert #pays(compid,ggb01c,ggb03c,ggb02f,[type],pay,amount) select ggb00c,ggb01c,ggb03c,ggb02f,'gx',ggb27c,ggb11f from ggm01 a with(nolock),ggm02 b with(nolock) where a.gga00c = b.ggb00c and a.gga01c = b.ggb01c and a.gga00c = @compid and (isnull(@frombill,'*') = '*' or a.gga01c between @frombill and @tobill) and (isnull(@fromcard,'*') = '*' or a.gga09c between @fromcard and @tocard) and gga02d between @fromdate and @todate --产品 insert #pays(compid,ggb01c,ggb03c,ggb02f,[type],pay,amount) select ggc00c,ggc01c,ggc03c,ggc02f,'cp',ggc27c,ggc11f from ggm01 a with(nolock),ggm03 b with(nolock) where a.gga00c = b.ggc00c and a.gga01c = b.ggc01c and a.gga00c = @compid and (isnull(@frombill,'*') = '*' or a.gga01c between @frombill and @tobill) and (isnull(@fromcard,'*') = '*' or a.gga09c between @fromcard and @tocard) and gga02d between @fromdate and @todate -- -- 卖卡 -- insert #pays(compid,ggb01c,ggb03c,[type],pay,amount) -- select gna00c,gna01c,'','gn',gsc04c,gsc05f -- from gnm01 a with(nolock),gnm02 b with(nolock),gsm03 c with(nolock) -- where a.gna00c = b.gnb00c -- and a.gna01c = b.gnb01c -- and a.gna00c = @compid -- and b.gnb00c = c.gsc00c -- and b.gnb01c = c.gsc01c -- and c.gsc02c = 'gn' -- and (isnull(@frombill,'*') = '*' or a.gna01c between @frombill and @tobill) -- and (isnull(@fromcard,'*') = '*' or b.gnb03c between @fromcard and @tocard) -- and gna02d between @fromdate and @todate --卖卡的金额 insert #pays(compid,ggb01c,ggb03c,[type],pay,amount) select gnb00c,gnb01c,'','gn',gnb15c,gnb16f from gnm01 a with(nolock),gnm02 b with(nolock) where a.gna00c = b.gnb00c and a.gna01c = b.gnb01c and a.gna00c = @compid and (isnull(@frombill,'*') = '*' or a.gna01c between @frombill and @tobill) and (isnull(@fromcard,'*') = '*' or b.gnb03c between @fromcard and @tocard) and gna02d between @fromdate and @todate and isnull(b.gnb15c,'') <> '' union all select gnb00c,gnb01c,'','gn',gnb17c,gnb18f from gnm01 a with(nolock),gnm02 b with(nolock) where a.gna00c = b.gnb00c and a.gna01c = b.gnb01c and a.gna00c = @compid and (isnull(@frombill,'*') = '*' or a.gna01c between @frombill and @tobill) and (isnull(@fromcard,'*') = '*' or b.gnb03c between @fromcard and @tocard) and gna02d between @fromdate and @todate and isnull(b.gnb17c,'') <> '' union all select gnb00c,gnb01c,'','gn',gnb19c,gnb20f from gnm01 a with(nolock),gnm02 b with(nolock) where a.gna00c = b.gnb00c and a.gna01c = b.gnb01c and a.gna00c = @compid and (isnull(@frombill,'*') = '*' or a.gna01c between @frombill and @tobill) and (isnull(@fromcard,'*') = '*' or b.gnb03c between @fromcard and @tocard) and gna02d between @fromdate and @todate and isnull(b.gnb19c,'') <> '' union all select gnb00c,gnb01c,'','gn',gnb21c,gnb22f from gnm01 a with(nolock),gnm02 b with(nolock) where a.gna00c = b.gnb00c and a.gna01c = b.gnb01c and a.gna00c = @compid and (isnull(@frombill,'*') = '*' or a.gna01c between @frombill and @tobill) and (isnull(@fromcard,'*') = '*' or b.gnb03c between @fromcard and @tocard) and gna02d between @fromdate and @todate and isnull(b.gnb21c,'') <> '' -- 卖卡 套餐 gn insert #pays(compid,ggb01c,ggb03c,[type],pay,amount) select distinct gna00c,gna01c,gtf02c,'gntc',gtf09c,gtf10f from gnm01 a with(nolock),gtc05 b with(nolock),gnm02 c with(nolock) where a.gna00c = b.gtf00c and a.gna01c = b.gtf01c and a.gna00c = c.gnb00c and a.gna01c = c.gnb01c and a.gna00c = @compid and (isnull(@frombill,'*') = '*' or a.gna01c between @frombill and @tobill) and (isnull(@fromcard,'*') = '*' or c.gnb03c between @fromcard and @tocard) and gna02d between @fromdate and @todate and isnull(b.gtf09c,'') <> '' union all select gna00c,gna01c,gtf02c,'gntc',gtf11c,gtf12f from gnm01 a with(nolock),gtc05 b with(nolock),gnm02 c with(nolock) where a.gna00c = b.gtf00c and a.gna01c = b.gtf01c and a.gna00c = c.gnb00c and a.gna01c = c.gnb01c and a.gna00c = @compid and (isnull(@frombill,'*') = '*' or a.gna01c between @frombill and @tobill) and (isnull(@fromcard,'*') = '*' or c.gnb03c between @fromcard and @tocard) and gna02d between @fromdate and @todate and isnull(b.gtf11c,'') <> '' union all select gna00c,gna01c,gtf02c,'gntc',gtf13c,gtf14f from gnm01 a with(nolock),gtc05 b with(nolock),gnm02 c with(nolock) where a.gna00c = b.gtf00c and a.gna01c = b.gtf01c and a.gna00c = c.gnb00c and a.gna01c = c.gnb01c and a.gna00c = @compid and (isnull(@frombill,'*') = '*' or a.gna01c between @frombill and @tobill) and (isnull(@fromcard,'*') = '*' or c.gnb03c between @fromcard and @tocard) and gna02d between @fromdate and @todate and isnull(b.gtf13c,'') <> '' union all select gna00c,gna01c,gtf02c,'gntc',gtf15c,gtf16f from gnm01 a with(nolock),gtc05 b with(nolock),gnm02 c with(nolock) where a.gna00c = b.gtf00c and a.gna01c = b.gtf01c and a.gna00c = c.gnb00c and a.gna01c = c.gnb01c and a.gna00c = @compid and (isnull(@frombill,'*') = '*' or a.gna01c between @frombill and @tobill) and (isnull(@fromcard,'*') = '*' or c.gnb03c between @fromcard and @tocard) and gna02d between @fromdate and @todate and isnull(b.gtf15c,'') <> '' -- 卖卡 疗程 gnlc insert #pays(compid,ggb01c,ggb03c,[type],pay,amount) select distinct gna00c,gna01c,gnc02c,'gnlc',gnc15c,gnc16f from gnm01 a with(nolock),gnm02 c with(nolock),gnm03 d with(nolock) where a.gna00c = c.gnb00c and a.gna01c = c.gnb01c and a.gna00c = @compid and a.gna00c = d.gnc00c and a.gna01c = d.gnc01c and (isnull(@frombill,'*') = '*' or a.gna01c between @frombill and @tobill) and (isnull(@fromcard,'*') = '*' or c.gnb03c between @fromcard and @tocard) and gna02d between @fromdate and @todate and isnull(d.gnc15c,'') <> '' union all select gna00c,gna01c,gnc02c,'gnlc',gnc17c,gnc18f from gnm01 a with(nolock),gnm02 c with(nolock),gnm03 d with(nolock) where a.gna00c = c.gnb00c and a.gna01c = c.gnb01c and a.gna00c = d.gnc00c and a.gna01c = d.gnc01c and a.gna00c = @compid and (isnull(@frombill,'*') = '*' or a.gna01c between @frombill and @tobill) and (isnull(@fromcard,'*') = '*' or c.gnb03c between @fromcard and @tocard) and gna02d between @fromdate and @todate and isnull(d.gnc17c,'') <> '' union all select gna00c,gna01c,gnc02c,'gnlc',gnc19c,gnc20f from gnm01 a with(nolock),gnm02 c with(nolock),gnm03 d with(nolock) where a.gna00c = c.gnb00c and a.gna01c = c.gnb01c and a.gna00c = d.gnc00c and a.gna01c = d.gnc01c and a.gna00c = @compid and (isnull(@frombill,'*') = '*' or a.gna01c between @frombill and @tobill) and (isnull(@fromcard,'*') = '*' or c.gnb03c between @fromcard and @tocard) and gna02d between @fromdate and @todate and isnull(d.gnc19c,'') <> '' union all select gna00c,gna01c,gnc02c,'gnlc',gnc23c,gnc24f from gnm01 a with(nolock),gnm02 c with(nolock),gnm03 d with(nolock) where a.gna00c = c.gnb00c and a.gna01c = c.gnb01c and a.gna00c = d.gnc00c and a.gna01c = d.gnc01c and a.gna00c = @compid and (isnull(@frombill,'*') = '*' or a.gna01c between @frombill and @tobill) and (isnull(@fromcard,'*') = '*' or c.gnb03c between @fromcard and @tocard) and gna02d between @fromdate and @todate and isnull(d.gnc23c,'') <> '' -- 充值 gz insert #pays(compid,ggb01c,ggb03c,[type],pay,amount) select gcl00c,gcl01c,'','gz',gcl67c,gcl68f from gcm10 a with(nolock) where a.gcl00c = @compid and a.gcl06i = 0 and (isnull(@frombill,'*') = '*' or a.gcl01c between @frombill and @tobill) and (isnull(@fromcard,'*') = '*' or a.gcl03c between @fromcard and @tocard) and a.gcl02d between @fromdate and @todate and isnull(a.gcl67c,'') <> '' union all select gcl00c,gcl01c,'','gz',gcl69c,gcl70f from gcm10 a with(nolock) where a.gcl00c = @compid and a.gcl06i = 0 and (isnull(@frombill,'*') = '*' or a.gcl01c between @frombill and @tobill) and (isnull(@fromcard,'*') = '*' or a.gcl03c between @fromcard and @tocard) and a.gcl02d between @fromdate and @todate and isnull(a.gcl69c,'') <> '' union all select gcl00c,gcl01c,'','gz',gcl71c,gcl72f from gcm10 a with(nolock) where a.gcl00c = @compid and a.gcl06i = 0 and (isnull(@frombill,'*') = '*' or a.gcl01c between @frombill and @tobill) and (isnull(@fromcard,'*') = '*' or a.gcl03c between @fromcard and @tocard) and a.gcl02d between @fromdate and @todate and isnull(a.gcl71c,'') <> '' union all select gcl00c,gcl01c,'','gz',gcl73c,gcl74f from gcm10 a with(nolock) where a.gcl00c = @compid and a.gcl06i = 0 and (isnull(@frombill,'*') = '*' or a.gcl01c between @frombill and @tobill) and (isnull(@fromcard,'*') = '*' or a.gcl03c between @fromcard and @tocard) and a.gcl02d between @fromdate and @todate and isnull(a.gcl73c,'') <> '' -- 充值 套餐 gztc insert #pays(compid,ggb01c,ggb03c,[type],pay,amount) select gcl00c,gcl01c,gth02c,'gztc',gth09c,gth10f from gcm10 a with(nolock),gtc06 b with(nolock) where a.gcl00c = b.gth00c and a.gcl01c = b.gth01c and a.gcl00c = @compid and a.gcl06i = 0 and (isnull(@frombill,'*') = '*' or a.gcl01c between @frombill and @tobill) and (isnull(@fromcard,'*') = '*' or a.gcl03c between @fromcard and @tocard) and a.gcl02d between @fromdate and @todate and isnull(b.gth09c,'') <> '' union all select gcl00c,gcl01c,gth02c,'gztc',gth11c,gth12f from gcm10 a with(nolock),gtc06 b with(nolock) where a.gcl00c = b.gth00c and a.gcl01c = b.gth01c and a.gcl00c = @compid and a.gcl06i = 0 and (isnull(@frombill,'*') = '*' or a.gcl01c between @frombill and @tobill) and (isnull(@fromcard,'*') = '*' or a.gcl03c between @fromcard and @tocard) and a.gcl02d between @fromdate and @todate and isnull(b.gth11c,'') <> '' union all select gcl00c,gcl01c,gth02c,'gztc',gth13c,gth14f from gcm10 a with(nolock),gtc06 b with(nolock) where a.gcl00c = b.gth00c and a.gcl01c = b.gth01c and a.gcl00c = @compid and a.gcl06i = 0 and (isnull(@frombill,'*') = '*' or a.gcl01c between @frombill and @tobill) and (isnull(@fromcard,'*') = '*' or a.gcl03c between @fromcard and @tocard) and a.gcl02d between @fromdate and @todate and isnull(b.gth13c,'') <> '' union all select gcl00c,gcl01c,gth02c,'gztc',gth15c,gth16f from gcm10 a with(nolock),gtc06 b with(nolock) where a.gcl00c = b.gth00c and a.gcl01c = b.gth01c and a.gcl00c = @compid and a.gcl06i = 0 and (isnull(@frombill,'*') = '*' or a.gcl01c between @frombill and @tobill) and (isnull(@fromcard,'*') = '*' or a.gcl03c between @fromcard and @tocard) and a.gcl02d between @fromdate and @todate and isnull(b.gth15c,'') <> '' -- 取款 qk insert #pays(compid,ggb01c,ggb03c,[type],pay,amount) select gcl00c,gcl01c,'','qk',gcl67c,gcl68f from gcm10 a with(nolock) where a.gcl00c = @compid and a.gcl06i = 1 and (isnull(@frombill,'*') = '*' or a.gcl01c between @frombill and @tobill) and (isnull(@fromcard,'*') = '*' or a.gcl03c between @fromcard and @tocard) and a.gcl02d between @fromdate and @todate and isnull(a.gcl67c,'') <> '' union all select gcl00c,gcl01c,'','qk',gcl69c,gcl70f from gcm10 a with(nolock) where a.gcl00c = @compid and a.gcl06i = 1 and (isnull(@frombill,'*') = '*' or a.gcl01c between @frombill and @tobill) and (isnull(@fromcard,'*') = '*' or a.gcl03c between @fromcard and @tocard) and a.gcl02d between @fromdate and @todate and isnull(a.gcl69c,'') <> '' union all select gcl00c,gcl01c,'','qk',gcl71c,gcl72f from gcm10 a with(nolock) where a.gcl00c = @compid and a.gcl06i = 1 and (isnull(@frombill,'*') = '*' or a.gcl01c between @frombill and @tobill) and (isnull(@fromcard,'*') = '*' or a.gcl03c between @fromcard and @tocard) and a.gcl02d between @fromdate and @todate and isnull(a.gcl71c,'') <> '' union all select gcl00c,gcl01c,'','qk',gcl73c,gcl74f from gcm10 a with(nolock) where a.gcl00c = @compid and a.gcl06i = 1 and (isnull(@frombill,'*') = '*' or a.gcl01c between @frombill and @tobill) and (isnull(@fromcard,'*') = '*' or a.gcl03c between @fromcard and @tocard) and a.gcl02d between @fromdate and @todate and isnull(a.gcl73c,'') <> '' -- 还款 hk insert #pays(compid,ggb01c,ggb03c,[type],pay,amount) select gcl00c,gcl01c,'','hk',gcl67c,gcl68f from gcm10 a with(nolock) where a.gcl00c = @compid and a.gcl06i = 6 and (isnull(@frombill,'*') = '*' or a.gcl01c between @frombill and @tobill) and (isnull(@fromcard,'*') = '*' or a.gcl03c between @fromcard and @tocard) and a.gcl02d between @fromdate and @todate and isnull(a.gcl67c,'') <> '' and gcl04c <> '888' union all select gcl00c,gcl01c,'','hk',gcl69c,gcl70f from gcm10 a with(nolock) where a.gcl00c = @compid and a.gcl06i = 6 and (isnull(@frombill,'*') = '*' or a.gcl01c between @frombill and @tobill) and (isnull(@fromcard,'*') = '*' or a.gcl03c between @fromcard and @tocard) and a.gcl02d between @fromdate and @todate and isnull(a.gcl69c,'') <> '' and gcl04c <> '888' union all select gcl00c,gcl01c,'','hk',gcl71c,gcl72f from gcm10 a with(nolock) where a.gcl00c = @compid and a.gcl06i = 6 and (isnull(@frombill,'*') = '*' or a.gcl01c between @frombill and @tobill) and (isnull(@fromcard,'*') = '*' or a.gcl03c between @fromcard and @tocard) and a.gcl02d between @fromdate and @todate and isnull(a.gcl71c,'') <> '' and gcl04c <> '888' union all select gcl00c,gcl01c,'','hk',gcl73c,gcl74f from gcm10 a with(nolock) where a.gcl00c = @compid and a.gcl06i = 6 and (isnull(@frombill,'*') = '*' or a.gcl01c between @frombill and @tobill) and (isnull(@fromcard,'*') = '*' or a.gcl03c between @fromcard and @tocard) and a.gcl02d between @fromdate and @todate and isnull(a.gcl73c,'') <> '' and gcl04c <> '888' -- 项目还款 xmhk insert #pays(compid,ggb01c,ggb03c,[type],pay,amount) select distinct gcl00c,gcl01c,'','xmhk',gcl67c,gcl68f from gcm10 a with(nolock),ggm02 b with(nolock) where a.gcl00c = b.ggb00c and a.gcl01c = b.ggb42c and a.gcl00c = @compid and a.gcl06i = 6 and (isnull(@frombill,'*') = '*' or a.gcl01c between @frombill and @tobill) and (isnull(@fromcard,'*') = '*' or a.gcl03c between @fromcard and @tocard) and a.gcl02d between @fromdate and @todate and isnull(a.gcl67c,'') <> '' and gcl04c = '888' union all select distinct gcl00c,gcl01c,'','xmhk',gcl69c,gcl70f from gcm10 a with(nolock),ggm02 b with(nolock) where a.gcl00c = b.ggb00c and a.gcl01c = b.ggb42c and a.gcl00c = @compid and a.gcl06i = 6 and (isnull(@frombill,'*') = '*' or a.gcl01c between @frombill and @tobill) and (isnull(@fromcard,'*') = '*' or a.gcl03c between @fromcard and @tocard) and a.gcl02d between @fromdate and @todate and isnull(a.gcl69c,'') <> '' and gcl04c = '888' union all select distinct gcl00c,gcl01c,'','xmhk',gcl71c,gcl72f from gcm10 a with(nolock),ggm02 b with(nolock) where a.gcl00c = b.ggb00c and a.gcl01c = b.ggb42c and a.gcl00c = @compid and a.gcl06i = 6 and (isnull(@frombill,'*') = '*' or a.gcl01c between @frombill and @tobill) and (isnull(@fromcard,'*') = '*' or a.gcl03c between @fromcard and @tocard) and a.gcl02d between @fromdate and @todate and isnull(a.gcl71c,'') <> '' and gcl04c = '888' union all select distinct gcl00c,gcl01c,'','xmhk',gcl73c,gcl74f from gcm10 a with(nolock),ggm02 b with(nolock) where a.gcl00c = b.ggb00c and a.gcl01c = b.ggb42c and a.gcl00c = @compid and a.gcl06i = 6 and (isnull(@frombill,'*') = '*' or a.gcl01c between @frombill and @tobill) and (isnull(@fromcard,'*') = '*' or a.gcl03c between @fromcard and @tocard) and a.gcl02d between @fromdate and @todate and isnull(a.gcl73c,'') <> '' and gcl04c = '888' -- 产品还款 cphk insert #pays(compid,ggb01c,ggb03c,[type],pay,amount) select distinct gcl00c,gcl01c,'','cphk',gcl67c,gcl68f from gcm10 a with(nolock),ggm03 b with(nolock) where a.gcl00c = b.ggc00c and a.gcl01c = b.ggc34c and a.gcl00c = @compid and a.gcl06i = 6 and (isnull(@frombill,'*') = '*' or a.gcl01c between @frombill and @tobill) and (isnull(@fromcard,'*') = '*' or a.gcl03c between @fromcard and @tocard) and a.gcl02d between @fromdate and @todate and isnull(a.gcl67c,'') <> '' and gcl04c = '888' union all select distinct gcl00c,gcl01c,'','cphk',gcl69c,gcl70f from gcm10 a with(nolock),ggm03 b with(nolock) where a.gcl00c = b.ggc00c and a.gcl01c = b.ggc34c and a.gcl00c = @compid and a.gcl06i = 6 and (isnull(@frombill,'*') = '*' or a.gcl01c between @frombill and @tobill) and (isnull(@fromcard,'*') = '*' or a.gcl03c between @fromcard and @tocard) and a.gcl02d between @fromdate and @todate and isnull(a.gcl69c,'') <> '' and gcl04c = '888' union all select distinct gcl00c,gcl01c,'','cphk',gcl71c,gcl72f from gcm10 a with(nolock),ggm03 b with(nolock) where a.gcl00c = b.ggc00c and a.gcl01c = b.ggc34c and a.gcl00c = @compid and a.gcl06i = 6 and (isnull(@frombill,'*') = '*' or a.gcl01c between @frombill and @tobill) and (isnull(@fromcard,'*') = '*' or a.gcl03c between @fromcard and @tocard) and a.gcl02d between @fromdate and @todate and isnull(a.gcl71c,'') <> '' and gcl04c = '888' union all select distinct gcl00c,gcl01c,'','cphk',gcl73c,gcl74f from gcm10 a with(nolock),ggm03 b with(nolock) where a.gcl00c = b.ggc00c and a.gcl01c = b.ggc34c and a.gcl00c = @compid and a.gcl06i = 6 and (isnull(@frombill,'*') = '*' or a.gcl01c between @frombill and @tobill) and (isnull(@fromcard,'*') = '*' or a.gcl03c between @fromcard and @tocard) and a.gcl02d between @fromdate and @todate and isnull(a.gcl73c,'') <> '' and gcl04c = '888' -- 疗程 lc insert #pays(compid,ggb01c,ggb03c,ggb02f,[type],pay,amount) select gcm00c,gcm01c,gcm02c,gcm25i,'lc',gcm15c,gcm16f from gcm10 a with(nolock),gcm11 b with(nolock) where a.gcl00c = b.gcm00c and a.gcl01c = b.gcm01c and a.gcl00c = @compid and a.gcl06i = 0 and (isnull(@frombill,'*') = '*' or a.gcl01c between @frombill and @tobill) and (isnull(@fromcard,'*') = '*' or a.gcl03c between @fromcard and @tocard) and a.gcl02d between @fromdate and @todate and isnull(b.gcm15c,'') <> '' union all select gcm00c,gcm01c,gcm02c,gcm25i,'lc',gcm17c,gcm18f from gcm10 a with(nolock),gcm11 b with(nolock) where a.gcl00c = b.gcm00c and a.gcl01c = b.gcm01c and a.gcl00c = @compid and a.gcl06i = 0 and (isnull(@frombill,'*') = '*' or a.gcl01c between @frombill and @tobill) and (isnull(@fromcard,'*') = '*' or a.gcl03c between @fromcard and @tocard) and a.gcl02d between @fromdate and @todate and isnull(b.gcm17c,'') <> '' union all select gcm00c,gcm01c,gcm02c,gcm25i,'lc',gcm19c,gcm20f from gcm10 a with(nolock),gcm11 b with(nolock) where a.gcl00c = b.gcm00c and a.gcl01c = b.gcm01c and a.gcl00c = @compid and a.gcl06i = 0 and (isnull(@frombill,'*') = '*' or a.gcl01c between @frombill and @tobill) and (isnull(@fromcard,'*') = '*' or a.gcl03c between @fromcard and @tocard) and a.gcl02d between @fromdate and @todate and isnull(b.gcm19c,'') <> '' union all select gcm00c,gcm01c,gcm02c,gcm25i,'lc',gcm23c,gcm24f from gcm10 a with(nolock),gcm11 b with(nolock) where a.gcl00c = b.gcm00c and a.gcl01c = b.gcm01c and a.gcl00c = @compid and a.gcl06i = 0 and (isnull(@frombill,'*') = '*' or a.gcl01c between @frombill and @tobill) and (isnull(@fromcard,'*') = '*' or a.gcl03c between @fromcard and @tocard) and a.gcl02d between @fromdate and @todate and isnull(b.gcm23c,'') <> '' create table #pays_result ( compid varchar(10) not null, -- 门店编号 ggb01c varchar(20) not null, -- 消费单号 ggb02f float null, --序号 ggb03c varchar(20) null, -- 项目代码 bz varchar(10) not null, --标识 pay_# float null, -- 支付 # pay_$ float null, -- 支付 $ pay_$1 float null, -- 支付 $1 pay_$2 float null, -- 支付 $2 pay_$3 float null, -- 支付 $3 pay_$4 float null, -- 支付 $4 pay_$5 float null, -- 支付 $5 pay_$6 float null, -- 支付 $6 pay_$7 float null, -- 支付 $7 pay_$8 float null, -- 支付 $8 [pay_&] float null, -- 支付 & pay_0 float null, -- 支付 0 pay_1 float null, -- 支付 1 pay_2 float null, -- 支付 2 pay_3 float null, -- 支付 3 pay_4 float null, -- 支付 4 pay_5 float null, -- 支付 5 pay_6 float null, -- 支付 6 pay_7 float null, -- 支付 7 pay_8 float null, -- 支付 8 pay_9 float null, -- 支付 9 pay_A float null, -- 支付 A pay_B float null, -- 支付 B pay_C float null, -- 支付 C pay_D float null, -- 支付 D pay_E float null, -- 支付 E pay_WX float null, -- 支付 WX pay_ZF float null, -- 支付 ZF pay_ZFB float null -- 支付 ZFB ) insert into #pays_result(compid,ggb01c,ggb02f,ggb03c,bz,pay_#,pay_$,pay_$1,pay_$2,pay_$3,pay_$4,pay_$5,pay_$6,pay_$7,pay_$8,[pay_&],pay_0,pay_1,pay_2,pay_3,pay_4,pay_5,pay_6,pay_7,pay_8,pay_9,pay_A,pay_B,pay_C,pay_D,pay_E,pay_WX,pay_ZF,pay_ZFB) select compid,a.ggb01c,ggb02f,ggb03c,bz,sum(pay_#),sum(pay_$),sum(pay_$1),sum(pay_$2),sum(pay_$3),sum(pay_$4),sum(pay_$5),sum(pay_$6),sum(pay_$7),sum(pay_$8), sum([pay_&]),sum(pay_0),sum(pay_1),sum(pay_2),sum(pay_3),sum(pay_4),sum(pay_5),sum(pay_6),sum(pay_7),sum(pay_8),sum(pay_9), sum(pay_A),sum(pay_B),sum(pay_C),sum(pay_D),sum(pay_E),sum(pay_WX),sum(pay_ZF),sum(pay_ZFB) from ( select compid,b.ggb01c,b.ggb02f,b.ggb03c,'gx' bz,pay_# = (case when b.pay = '#' then convert(float,isnull(amount,0)) else 0 end), pay_$ = (case when b.pay = '$' then isnull(amount,0) else 0 end), pay_$1 = (case when b.pay = '$1' then isnull(amount,0) else 0 end), pay_$2 = (case when b.pay = '$2' then isnull(amount,0) else 0 end), pay_$3 = (case when b.pay = '$3' then isnull(amount,0) else 0 end), pay_$4 = (case when b.pay = '$4' then isnull(amount,0) else 0 end), pay_$5 = (case when b.pay = '$5' then isnull(amount,0) else 0 end), pay_$6 = (case when b.pay = '$6' then isnull(amount,0) else 0 end), pay_$7 = (case when b.pay = '$7' then isnull(amount,0) else 0 end), pay_$8 = (case when b.pay = '$8' then isnull(amount,0) else 0 end), [pay_&] = (case when b.pay = '&' then isnull(amount,0) else 0 end), pay_0 = (case when b.pay = '0' then isnull(amount,0) else 0 end), pay_1 = (case when b.pay = '1' then isnull(amount,0) else 0 end), pay_2 = (case when b.pay = '2' then isnull(amount,0) else 0 end), pay_3 = (case when b.pay = '3' then isnull(amount,0) else 0 end), pay_4 = (case when b.pay = '4' then isnull(amount,0) else 0 end), pay_5 = (case when b.pay = '5' then isnull(amount,0) else 0 end), pay_6 = (case when b.pay = '6' then isnull(amount,0) else 0 end), pay_7 = (case when b.pay = '7' then isnull(amount,0) else 0 end), pay_8 = (case when b.pay = '8' then isnull(amount,0) else 0 end), pay_9 = (case when b.pay = '9' then isnull(amount,0) else 0 end), pay_A = (case when b.pay = 'A' then isnull(amount,0) else 0 end), pay_B = (case when b.pay = 'B' then isnull(amount,0) else 0 end), pay_C = (case when b.pay = 'C' then isnull(amount,0) else 0 end), pay_D = (case when b.pay = 'D' then isnull(amount,0) else 0 end), pay_E = (case when b.pay = 'E' then isnull(amount,0) else 0 end), pay_WX = (case when b.pay = 'WX' then isnull(amount,0) else 0 end), pay_ZF = (case when b.pay = 'ZF' then isnull(amount,0) else 0 end), pay_ZFB = (case when b.pay = 'ZFB' then isnull(amount,0) else 0 end) from #ggm02 a,#pays b where a.ggb00c = b.compid and a.ggb01c = b.ggb01c and a.ggb03c = b.ggb03c and a.ggb02f = b.ggb02f and b.[type] = 'gx' union all select compid,b.ggb01c,b.ggb02f,b.ggb03c,'cp' bz,pay_# = (case when b.pay = '#' then convert(float,isnull(amount,0)) else 0 end), pay_$ = (case when b.pay = '$' then isnull(amount,0) else 0 end), pay_$1 = (case when b.pay = '$1' then isnull(amount,0) else 0 end), pay_$2 = (case when b.pay = '$2' then isnull(amount,0) else 0 end), pay_$3 = (case when b.pay = '$3' then isnull(amount,0) else 0 end), pay_$4 = (case when b.pay = '$4' then isnull(amount,0) else 0 end), pay_$5 = (case when b.pay = '$5' then isnull(amount,0) else 0 end), pay_$6 = (case when b.pay = '$6' then isnull(amount,0) else 0 end), pay_$7 = (case when b.pay = '$7' then isnull(amount,0) else 0 end), pay_$8 = (case when b.pay = '$8' then isnull(amount,0) else 0 end), [pay_&] = (case when b.pay = '&' then isnull(amount,0) else 0 end), pay_0 = (case when b.pay = '0' then isnull(amount,0) else 0 end), pay_1 = (case when b.pay = '1' then isnull(amount,0) else 0 end), pay_2 = (case when b.pay = '2' then isnull(amount,0) else 0 end), pay_3 = (case when b.pay = '3' then isnull(amount,0) else 0 end), pay_4 = (case when b.pay = '4' then isnull(amount,0) else 0 end), pay_5 = (case when b.pay = '5' then isnull(amount,0) else 0 end), pay_6 = (case when b.pay = '6' then isnull(amount,0) else 0 end), pay_7 = (case when b.pay = '7' then isnull(amount,0) else 0 end), pay_8 = (case when b.pay = '8' then isnull(amount,0) else 0 end), pay_9 = (case when b.pay = '9' then isnull(amount,0) else 0 end), pay_A = (case when b.pay = 'A' then isnull(amount,0) else 0 end), pay_B = (case when b.pay = 'B' then isnull(amount,0) else 0 end), pay_C = (case when b.pay = 'C' then isnull(amount,0) else 0 end), pay_D = (case when b.pay = 'D' then isnull(amount,0) else 0 end), pay_E = (case when b.pay = 'E' then isnull(amount,0) else 0 end), pay_WX = (case when b.pay = 'WX' then isnull(amount,0) else 0 end), pay_ZF = (case when b.pay = 'ZF' then isnull(amount,0) else 0 end), pay_ZFB = (case when b.pay = 'ZFB' then isnull(amount,0) else 0 end) from #ggm02 a,#pays b where a.ggb00c = b.compid and a.ggb01c = b.ggb01c and a.ggb03c = b.ggb03c and a.ggb02f = b.ggb02f and b.[type] = 'cp' union all select compid,a.ggb01c,b.ggb02f,b.ggb03c,'gn',pay_# = (case when b.pay = '#' then isnull(amount,0) else 0 end), pay_$ = (case when b.pay = '$' then isnull(amount,0) else 0 end), pay_$1 = (case when b.pay = '$1' then isnull(amount,0) else 0 end), pay_$2 = (case when b.pay = '$2' then isnull(amount,0) else 0 end), pay_$3 = (case when b.pay = '$3' then isnull(amount,0) else 0 end), pay_$4 = (case when b.pay = '$4' then isnull(amount,0) else 0 end), pay_$5 = (case when b.pay = '$5' then isnull(amount,0) else 0 end), pay_$6 = (case when b.pay = '$6' then isnull(amount,0) else 0 end), pay_$7 = (case when b.pay = '$7' then isnull(amount,0) else 0 end), pay_$8 = (case when b.pay = '$8' then isnull(amount,0) else 0 end), [pay_&] = (case when b.pay = '&' then isnull(amount,0) else 0 end), pay_0 = (case when b.pay = '0' then isnull(amount,0) else 0 end), pay_1 = (case when b.pay = '1' then isnull(amount,0) else 0 end), pay_2 = (case when b.pay = '2' then isnull(amount,0) else 0 end), pay_3 = (case when b.pay = '3' then isnull(amount,0) else 0 end), pay_4 = (case when b.pay = '4' then isnull(amount,0) else 0 end), pay_5 = (case when b.pay = '5' then isnull(amount,0) else 0 end), pay_6 = (case when b.pay = '6' then isnull(amount,0) else 0 end), pay_7 = (case when b.pay = '7' then isnull(amount,0) else 0 end), pay_8 = (case when b.pay = '8' then isnull(amount,0) else 0 end), pay_9 = (case when b.pay = '9' then isnull(amount,0) else 0 end), pay_A = (case when b.pay = 'A' then isnull(amount,0) else 0 end), pay_B = (case when b.pay = 'B' then isnull(amount,0) else 0 end), pay_C = (case when b.pay = 'C' then isnull(amount,0) else 0 end), pay_D = (case when b.pay = 'D' then isnull(amount,0) else 0 end), pay_E = (case when b.pay = 'E' then isnull(amount,0) else 0 end), pay_WX = (case when b.pay = 'WX' then isnull(amount,0) else 0 end), pay_ZF = (case when b.pay = 'ZF' then isnull(amount,0) else 0 end), pay_ZFB = (case when b.pay = 'ZFB' then isnull(amount,0) else 0 end) from #ggm02 a,#pays b where a.ggb00c = b.compid and a.ggb01c = b.ggb01c and a.ggb03c = b.ggb03c and a.[type] = b.[type] and a.[type] = 'gn' union all select compid,a.ggb01c,b.ggb02f,b.ggb03c,'gz',pay_# = (case when b.pay = '#' then isnull(amount,0) else 0 end), pay_$ = (case when b.pay = '$' then isnull(amount,0) else 0 end), pay_$1 = (case when b.pay = '$1' then isnull(amount,0) else 0 end), pay_$2 = (case when b.pay = '$2' then isnull(amount,0) else 0 end), pay_$3 = (case when b.pay = '$3' then isnull(amount,0) else 0 end), pay_$4 = (case when b.pay = '$4' then isnull(amount,0) else 0 end), pay_$5 = (case when b.pay = '$5' then isnull(amount,0) else 0 end), pay_$6 = (case when b.pay = '$6' then isnull(amount,0) else 0 end), pay_$7 = (case when b.pay = '$7' then isnull(amount,0) else 0 end), pay_$8 = (case when b.pay = '$8' then isnull(amount,0) else 0 end), [pay_&] = (case when b.pay = '&' then isnull(amount,0) else 0 end), pay_0 = (case when b.pay = '0' then isnull(amount,0) else 0 end), pay_1 = (case when b.pay = '1' then isnull(amount,0) else 0 end), pay_2 = (case when b.pay = '2' then isnull(amount,0) else 0 end), pay_3 = (case when b.pay = '3' then isnull(amount,0) else 0 end), pay_4 = (case when b.pay = '4' then isnull(amount,0) else 0 end), pay_5 = (case when b.pay = '5' then isnull(amount,0) else 0 end), pay_6 = (case when b.pay = '6' then isnull(amount,0) else 0 end), pay_7 = (case when b.pay = '7' then isnull(amount,0) else 0 end), pay_8 = (case when b.pay = '8' then isnull(amount,0) else 0 end), pay_9 = (case when b.pay = '9' then isnull(amount,0) else 0 end), pay_A = (case when b.pay = 'A' then isnull(amount,0) else 0 end), pay_B = (case when b.pay = 'B' then isnull(amount,0) else 0 end), pay_C = (case when b.pay = 'C' then isnull(amount,0) else 0 end), pay_D = (case when b.pay = 'D' then isnull(amount,0) else 0 end), pay_E = (case when b.pay = 'E' then isnull(amount,0) else 0 end), pay_WX = (case when b.pay = 'WX' then isnull(amount,0) else 0 end), pay_ZF = (case when b.pay = 'ZF' then isnull(amount,0) else 0 end), pay_ZFB = (case when b.pay = 'ZFB' then isnull(amount,0) else 0 end) from #ggm02 a,#pays b where a.ggb00c = b.compid and a.ggb01c = b.ggb01c and a.[type] = b.[type] and a.[type] = 'gz' union all select compid,a.ggb01c,b.ggb02f,b.ggb03c,'qk',pay_# = (case when b.pay = '#' then isnull(amount,0) else 0 end), pay_$ = (case when b.pay = '$' then isnull(amount,0) else 0 end), pay_$1 = (case when b.pay = '$1' then isnull(amount,0) else 0 end), pay_$2 = (case when b.pay = '$2' then isnull(amount,0) else 0 end), pay_$3 = (case when b.pay = '$3' then isnull(amount,0) else 0 end), pay_$4 = (case when b.pay = '$4' then isnull(amount,0) else 0 end), pay_$5 = (case when b.pay = '$5' then isnull(amount,0) else 0 end), pay_$6 = (case when b.pay = '$6' then isnull(amount,0) else 0 end), pay_$7 = (case when b.pay = '$7' then isnull(amount,0) else 0 end), pay_$8 = (case when b.pay = '$8' then isnull(amount,0) else 0 end), [pay_&] = (case when b.pay = '&' then isnull(amount,0) else 0 end), pay_0 = (case when b.pay = '0' then isnull(amount,0) else 0 end), pay_1 = (case when b.pay = '1' then isnull(amount,0) else 0 end), pay_2 = (case when b.pay = '2' then isnull(amount,0) else 0 end), pay_3 = (case when b.pay = '3' then isnull(amount,0) else 0 end), pay_4 = (case when b.pay = '4' then isnull(amount,0) else 0 end), pay_5 = (case when b.pay = '5' then isnull(amount,0) else 0 end), pay_6 = (case when b.pay = '6' then isnull(amount,0) else 0 end), pay_7 = (case when b.pay = '7' then isnull(amount,0) else 0 end), pay_8 = (case when b.pay = '8' then isnull(amount,0) else 0 end), pay_9 = (case when b.pay = '9' then isnull(amount,0) else 0 end), pay_A = (case when b.pay = 'A' then isnull(amount,0) else 0 end), pay_B = (case when b.pay = 'B' then isnull(amount,0) else 0 end), pay_C = (case when b.pay = 'C' then isnull(amount,0) else 0 end), pay_D = (case when b.pay = 'D' then isnull(amount,0) else 0 end), pay_E = (case when b.pay = 'E' then isnull(amount,0) else 0 end), pay_WX = (case when b.pay = 'WX' then isnull(amount,0) else 0 end), pay_ZF = (case when b.pay = 'ZF' then isnull(amount,0) else 0 end), pay_ZFB = (case when b.pay = 'ZFB' then isnull(amount,0) else 0 end) from #ggm02 a,#pays b where a.ggb00c = b.compid and a.ggb01c = b.ggb01c and a.[type] = b.[type] and a.[type] = 'qk' union all select compid,a.ggb01c,b.ggb02f,b.ggb03c,'hk',pay_# = (case when b.pay = '#' then isnull(amount,0) else 0 end), pay_$ = (case when b.pay = '$' then isnull(amount,0) else 0 end), pay_$1 = (case when b.pay = '$1' then isnull(amount,0) else 0 end), pay_$2 = (case when b.pay = '$2' then isnull(amount,0) else 0 end), pay_$3 = (case when b.pay = '$3' then isnull(amount,0) else 0 end), pay_$4 = (case when b.pay = '$4' then isnull(amount,0) else 0 end), pay_$5 = (case when b.pay = '$5' then isnull(amount,0) else 0 end), pay_$6 = (case when b.pay = '$6' then isnull(amount,0) else 0 end), pay_$7 = (case when b.pay = '$7' then isnull(amount,0) else 0 end), pay_$8 = (case when b.pay = '$8' then isnull(amount,0) else 0 end), [pay_&] = (case when b.pay = '&' then isnull(amount,0) else 0 end), pay_0 = (case when b.pay = '0' then isnull(amount,0) else 0 end), pay_1 = (case when b.pay = '1' then isnull(amount,0) else 0 end), pay_2 = (case when b.pay = '2' then isnull(amount,0) else 0 end), pay_3 = (case when b.pay = '3' then isnull(amount,0) else 0 end), pay_4 = (case when b.pay = '4' then isnull(amount,0) else 0 end), pay_5 = (case when b.pay = '5' then isnull(amount,0) else 0 end), pay_6 = (case when b.pay = '6' then isnull(amount,0) else 0 end), pay_7 = (case when b.pay = '7' then isnull(amount,0) else 0 end), pay_8 = (case when b.pay = '8' then isnull(amount,0) else 0 end), pay_9 = (case when b.pay = '9' then isnull(amount,0) else 0 end), pay_A = (case when b.pay = 'A' then isnull(amount,0) else 0 end), pay_B = (case when b.pay = 'B' then isnull(amount,0) else 0 end), pay_C = (case when b.pay = 'C' then isnull(amount,0) else 0 end), pay_D = (case when b.pay = 'D' then isnull(amount,0) else 0 end), pay_E = (case when b.pay = 'E' then isnull(amount,0) else 0 end), pay_WX = (case when b.pay = 'WX' then isnull(amount,0) else 0 end), pay_ZF = (case when b.pay = 'ZF' then isnull(amount,0) else 0 end), pay_ZFB = (case when b.pay = 'ZFB' then isnull(amount,0) else 0 end) from #ggm02 a,#pays b where a.ggb00c = b.compid and a.ggb01c = b.ggb01c and a.[type] = b.[type] and a.[type] = 'hk' union all select compid,a.ggb01c,b.ggb02f,b.ggb03c,'xmhk',pay_# = (case when b.pay = '#' then isnull(amount,0) else 0 end), pay_$ = (case when b.pay = '$' then isnull(amount,0) else 0 end), pay_$1 = (case when b.pay = '$1' then isnull(amount,0) else 0 end), pay_$2 = (case when b.pay = '$2' then isnull(amount,0) else 0 end), pay_$3 = (case when b.pay = '$3' then isnull(amount,0) else 0 end), pay_$4 = (case when b.pay = '$4' then isnull(amount,0) else 0 end), pay_$5 = (case when b.pay = '$5' then isnull(amount,0) else 0 end), pay_$6 = (case when b.pay = '$6' then isnull(amount,0) else 0 end), pay_$7 = (case when b.pay = '$7' then isnull(amount,0) else 0 end), pay_$8 = (case when b.pay = '$8' then isnull(amount,0) else 0 end), [pay_&] = (case when b.pay = '&' then isnull(amount,0) else 0 end), pay_0 = (case when b.pay = '0' then isnull(amount,0) else 0 end), pay_1 = (case when b.pay = '1' then isnull(amount,0) else 0 end), pay_2 = (case when b.pay = '2' then isnull(amount,0) else 0 end), pay_3 = (case when b.pay = '3' then isnull(amount,0) else 0 end), pay_4 = (case when b.pay = '4' then isnull(amount,0) else 0 end), pay_5 = (case when b.pay = '5' then isnull(amount,0) else 0 end), pay_6 = (case when b.pay = '6' then isnull(amount,0) else 0 end), pay_7 = (case when b.pay = '7' then isnull(amount,0) else 0 end), pay_8 = (case when b.pay = '8' then isnull(amount,0) else 0 end), pay_9 = (case when b.pay = '9' then isnull(amount,0) else 0 end), pay_A = (case when b.pay = 'A' then isnull(amount,0) else 0 end), pay_B = (case when b.pay = 'B' then isnull(amount,0) else 0 end), pay_C = (case when b.pay = 'C' then isnull(amount,0) else 0 end), pay_D = (case when b.pay = 'D' then isnull(amount,0) else 0 end), pay_E = (case when b.pay = 'E' then isnull(amount,0) else 0 end), pay_WX = (case when b.pay = 'WX' then isnull(amount,0) else 0 end), pay_ZF = (case when b.pay = 'ZF' then isnull(amount,0) else 0 end), pay_ZFB = (case when b.pay = 'ZFB' then isnull(amount,0) else 0 end) from #ggm02 a,#pays b where a.ggb00c = b.compid and a.ggb01c = b.ggb01c and a.[type] = b.[type] and a.[type] = 'xmhk' union all select compid,a.ggb01c,b.ggb02f,b.ggb03c,'cphk',pay_# = (case when b.pay = '#' then isnull(amount,0) else 0 end), pay_$ = (case when b.pay = '$' then isnull(amount,0) else 0 end), pay_$1 = (case when b.pay = '$1' then isnull(amount,0) else 0 end), pay_$2 = (case when b.pay = '$2' then isnull(amount,0) else 0 end), pay_$3 = (case when b.pay = '$3' then isnull(amount,0) else 0 end), pay_$4 = (case when b.pay = '$4' then isnull(amount,0) else 0 end), pay_$5 = (case when b.pay = '$5' then isnull(amount,0) else 0 end), pay_$6 = (case when b.pay = '$6' then isnull(amount,0) else 0 end), pay_$7 = (case when b.pay = '$7' then isnull(amount,0) else 0 end), pay_$8 = (case when b.pay = '$8' then isnull(amount,0) else 0 end), [pay_&] = (case when b.pay = '&' then isnull(amount,0) else 0 end), pay_0 = (case when b.pay = '0' then isnull(amount,0) else 0 end), pay_1 = (case when b.pay = '1' then isnull(amount,0) else 0 end), pay_2 = (case when b.pay = '2' then isnull(amount,0) else 0 end), pay_3 = (case when b.pay = '3' then isnull(amount,0) else 0 end), pay_4 = (case when b.pay = '4' then isnull(amount,0) else 0 end), pay_5 = (case when b.pay = '5' then isnull(amount,0) else 0 end), pay_6 = (case when b.pay = '6' then isnull(amount,0) else 0 end), pay_7 = (case when b.pay = '7' then isnull(amount,0) else 0 end), pay_8 = (case when b.pay = '8' then isnull(amount,0) else 0 end), pay_9 = (case when b.pay = '9' then isnull(amount,0) else 0 end), pay_A = (case when b.pay = 'A' then isnull(amount,0) else 0 end), pay_B = (case when b.pay = 'B' then isnull(amount,0) else 0 end), pay_C = (case when b.pay = 'C' then isnull(amount,0) else 0 end), pay_D = (case when b.pay = 'D' then isnull(amount,0) else 0 end), pay_E = (case when b.pay = 'E' then isnull(amount,0) else 0 end), pay_WX = (case when b.pay = 'WX' then isnull(amount,0) else 0 end), pay_ZF = (case when b.pay = 'ZF' then isnull(amount,0) else 0 end), pay_ZFB = (case when b.pay = 'ZFB' then isnull(amount,0) else 0 end) from #ggm02 a,#pays b where a.ggb00c = b.compid and a.ggb01c = b.ggb01c and a.[type] = b.[type] and a.[type] = 'cphk' union all select compid,a.ggb01c,b.ggb02f,b.ggb03c,'lc',pay_# = (case when b.pay = '#' then isnull(amount,0) else 0 end), pay_$ = (case when b.pay = '$' then isnull(amount,0) else 0 end), pay_$1 = (case when b.pay = '$1' then isnull(amount,0) else 0 end), pay_$2 = (case when b.pay = '$2' then isnull(amount,0) else 0 end), pay_$3 = (case when b.pay = '$3' then isnull(amount,0) else 0 end), pay_$4 = (case when b.pay = '$4' then isnull(amount,0) else 0 end), pay_$5 = (case when b.pay = '$5' then isnull(amount,0) else 0 end), pay_$6 = (case when b.pay = '$6' then isnull(amount,0) else 0 end), pay_$7 = (case when b.pay = '$7' then isnull(amount,0) else 0 end), pay_$8 = (case when b.pay = '$8' then isnull(amount,0) else 0 end), [pay_&] = (case when b.pay = '&' then isnull(amount,0) else 0 end), pay_0 = (case when b.pay = '0' then isnull(amount,0) else 0 end), pay_1 = (case when b.pay = '1' then isnull(amount,0) else 0 end), pay_2 = (case when b.pay = '2' then isnull(amount,0) else 0 end), pay_3 = (case when b.pay = '3' then isnull(amount,0) else 0 end), pay_4 = (case when b.pay = '4' then isnull(amount,0) else 0 end), pay_5 = (case when b.pay = '5' then isnull(amount,0) else 0 end), pay_6 = (case when b.pay = '6' then isnull(amount,0) else 0 end), pay_7 = (case when b.pay = '7' then isnull(amount,0) else 0 end), pay_8 = (case when b.pay = '8' then isnull(amount,0) else 0 end), pay_9 = (case when b.pay = '9' then isnull(amount,0) else 0 end), pay_A = (case when b.pay = 'A' then isnull(amount,0) else 0 end), pay_B = (case when b.pay = 'B' then isnull(amount,0) else 0 end), pay_C = (case when b.pay = 'C' then isnull(amount,0) else 0 end), pay_D = (case when b.pay = 'D' then isnull(amount,0) else 0 end), pay_E = (case when b.pay = 'E' then isnull(amount,0) else 0 end), pay_WX = (case when b.pay = 'WX' then isnull(amount,0) else 0 end), pay_ZF = (case when b.pay = 'ZF' then isnull(amount,0) else 0 end), pay_ZFB = (case when b.pay = 'ZFB' then isnull(amount,0) else 0 end) from #ggm02 a,#pays b where a.ggb00c = b.compid and a.ggb01c = b.ggb01c and a.ggb02f = b.ggb02f and a.[type] = b.[type] and a.ggb03c = b.ggb03c and a.[type] = 'lc' union all select compid,a.ggb01c,b.ggb02f,b.ggb03c,'gztc',pay_# = (case when b.pay = '#' then isnull(amount,0) else 0 end), pay_$ = (case when b.pay = '$' then isnull(amount,0) else 0 end), pay_$1 = (case when b.pay = '$1' then isnull(amount,0) else 0 end), pay_$2 = (case when b.pay = '$2' then isnull(amount,0) else 0 end), pay_$3 = (case when b.pay = '$3' then isnull(amount,0) else 0 end), pay_$4 = (case when b.pay = '$4' then isnull(amount,0) else 0 end), pay_$5 = (case when b.pay = '$5' then isnull(amount,0) else 0 end), pay_$6 = (case when b.pay = '$6' then isnull(amount,0) else 0 end), pay_$7 = (case when b.pay = '$7' then isnull(amount,0) else 0 end), pay_$8 = (case when b.pay = '$8' then isnull(amount,0) else 0 end), [pay_&] = (case when b.pay = '&' then isnull(amount,0) else 0 end), pay_0 = (case when b.pay = '0' then isnull(amount,0) else 0 end), pay_1 = (case when b.pay = '1' then isnull(amount,0) else 0 end), pay_2 = (case when b.pay = '2' then isnull(amount,0) else 0 end), pay_3 = (case when b.pay = '3' then isnull(amount,0) else 0 end), pay_4 = (case when b.pay = '4' then isnull(amount,0) else 0 end), pay_5 = (case when b.pay = '5' then isnull(amount,0) else 0 end), pay_6 = (case when b.pay = '6' then isnull(amount,0) else 0 end), pay_7 = (case when b.pay = '7' then isnull(amount,0) else 0 end), pay_8 = (case when b.pay = '8' then isnull(amount,0) else 0 end), pay_9 = (case when b.pay = '9' then isnull(amount,0) else 0 end), pay_A = (case when b.pay = 'A' then isnull(amount,0) else 0 end), pay_B = (case when b.pay = 'B' then isnull(amount,0) else 0 end), pay_C = (case when b.pay = 'C' then isnull(amount,0) else 0 end), pay_D = (case when b.pay = 'D' then isnull(amount,0) else 0 end), pay_E = (case when b.pay = 'E' then isnull(amount,0) else 0 end), pay_WX = (case when b.pay = 'WX' then isnull(amount,0) else 0 end), pay_ZF = (case when b.pay = 'ZF' then isnull(amount,0) else 0 end), pay_ZFB = (case when b.pay = 'ZFB' then isnull(amount,0) else 0 end) from #ggm02 a,#pays b where a.ggb00c = b.compid and a.ggb01c = b.ggb01c and a.[type] = b.[type] and a.ggb03c = b.ggb03c and a.[type] = 'gztc' union all select compid,a.ggb01c,b.ggb02f,b.ggb03c,'gntc',pay_# = (case when b.pay = '#' then isnull(amount,0) else 0 end), pay_$ = (case when b.pay = '$' then isnull(amount,0) else 0 end), pay_$1 = (case when b.pay = '$1' then isnull(amount,0) else 0 end), pay_$2 = (case when b.pay = '$2' then isnull(amount,0) else 0 end), pay_$3 = (case when b.pay = '$3' then isnull(amount,0) else 0 end), pay_$4 = (case when b.pay = '$4' then isnull(amount,0) else 0 end), pay_$5 = (case when b.pay = '$5' then isnull(amount,0) else 0 end), pay_$6 = (case when b.pay = '$6' then isnull(amount,0) else 0 end), pay_$7 = (case when b.pay = '$7' then isnull(amount,0) else 0 end), pay_$8 = (case when b.pay = '$8' then isnull(amount,0) else 0 end), [pay_&] = (case when b.pay = '&' then isnull(amount,0) else 0 end), pay_0 = (case when b.pay = '0' then isnull(amount,0) else 0 end), pay_1 = (case when b.pay = '1' then isnull(amount,0) else 0 end), pay_2 = (case when b.pay = '2' then isnull(amount,0) else 0 end), pay_3 = (case when b.pay = '3' then isnull(amount,0) else 0 end), pay_4 = (case when b.pay = '4' then isnull(amount,0) else 0 end), pay_5 = (case when b.pay = '5' then isnull(amount,0) else 0 end), pay_6 = (case when b.pay = '6' then isnull(amount,0) else 0 end), pay_7 = (case when b.pay = '7' then isnull(amount,0) else 0 end), pay_8 = (case when b.pay = '8' then isnull(amount,0) else 0 end), pay_9 = (case when b.pay = '9' then isnull(amount,0) else 0 end), pay_A = (case when b.pay = 'A' then isnull(amount,0) else 0 end), pay_B = (case when b.pay = 'B' then isnull(amount,0) else 0 end), pay_C = (case when b.pay = 'C' then isnull(amount,0) else 0 end), pay_D = (case when b.pay = 'D' then isnull(amount,0) else 0 end), pay_E = (case when b.pay = 'E' then isnull(amount,0) else 0 end), pay_WX = (case when b.pay = 'WX' then isnull(amount,0) else 0 end), pay_ZF = (case when b.pay = 'ZF' then isnull(amount,0) else 0 end), pay_ZFB = (case when b.pay = 'ZFB' then isnull(amount,0) else 0 end) from #ggm02 a,#pays b where a.ggb00c = b.compid and a.ggb01c = b.ggb01c and a.[type] = b.[type] and a.ggb03c = b.ggb03c and a.[type] = 'gntc' union all select compid,a.ggb01c,b.ggb02f,b.ggb03c,'gnlc',pay_# = (case when b.pay = '#' then isnull(amount,0) else 0 end), pay_$ = (case when b.pay = '$' then isnull(amount,0) else 0 end), pay_$1 = (case when b.pay = '$1' then isnull(amount,0) else 0 end), pay_$2 = (case when b.pay = '$2' then isnull(amount,0) else 0 end), pay_$3 = (case when b.pay = '$3' then isnull(amount,0) else 0 end), pay_$4 = (case when b.pay = '$4' then isnull(amount,0) else 0 end), pay_$5 = (case when b.pay = '$5' then isnull(amount,0) else 0 end), pay_$6 = (case when b.pay = '$6' then isnull(amount,0) else 0 end), pay_$7 = (case when b.pay = '$7' then isnull(amount,0) else 0 end), pay_$8 = (case when b.pay = '$8' then isnull(amount,0) else 0 end), [pay_&] = (case when b.pay = '&' then isnull(amount,0) else 0 end), pay_0 = (case when b.pay = '0' then isnull(amount,0) else 0 end), pay_1 = (case when b.pay = '1' then isnull(amount,0) else 0 end), pay_2 = (case when b.pay = '2' then isnull(amount,0) else 0 end), pay_3 = (case when b.pay = '3' then isnull(amount,0) else 0 end), pay_4 = (case when b.pay = '4' then isnull(amount,0) else 0 end), pay_5 = (case when b.pay = '5' then isnull(amount,0) else 0 end), pay_6 = (case when b.pay = '6' then isnull(amount,0) else 0 end), pay_7 = (case when b.pay = '7' then isnull(amount,0) else 0 end), pay_8 = (case when b.pay = '8' then isnull(amount,0) else 0 end), pay_9 = (case when b.pay = '9' then isnull(amount,0) else 0 end), pay_A = (case when b.pay = 'A' then isnull(amount,0) else 0 end), pay_B = (case when b.pay = 'B' then isnull(amount,0) else 0 end), pay_C = (case when b.pay = 'C' then isnull(amount,0) else 0 end), pay_D = (case when b.pay = 'D' then isnull(amount,0) else 0 end), pay_E = (case when b.pay = 'E' then isnull(amount,0) else 0 end), pay_WX = (case when b.pay = 'WX' then isnull(amount,0) else 0 end), pay_ZF = (case when b.pay = 'ZF' then isnull(amount,0) else 0 end), pay_ZFB = (case when b.pay = 'ZFB' then isnull(amount,0) else 0 end) from #ggm02 a,#pays b where a.ggb00c = b.compid and a.ggb01c = b.ggb01c and a.[type] = b.[type] and a.ggb03c = b.ggb03c and a.[type] = 'gnlc' ) a group by compid,a.ggb01c,ggb02f,ggb03c,bz update a set pay_# = b.pay_#, pay_$ = b.pay_$, pay_$1 = b.pay_$1, pay_$2 = b.pay_$2, pay_$3 = b.pay_$3, pay_$4 = b.pay_$4, pay_$5 = b.pay_$5, pay_$6 = b.pay_$6, pay_$7 = b.pay_$7, pay_$8 = b.pay_$8, [pay_&] = b.[pay_&], pay_0 = b.pay_0, pay_1 = b.pay_1, pay_2 = b.pay_2, pay_3 = b.pay_3, pay_4 = b.pay_4, pay_5 = b.pay_5, pay_6 = b.pay_6, pay_7 = b.pay_7, pay_8 = b.pay_8, pay_9 = b.pay_9, pay_A = b.pay_A, pay_B = b.pay_B, pay_C = b.pay_C, pay_D = b.pay_D, pay_E = b.pay_E, pay_WX = b.pay_WX, pay_ZF = b.pay_ZF, pay_ZFB = b.pay_ZFB from #ggm02 a,#pays_result b where a.ggb00c = b.compid and a.ggb01c = b.ggb01c and a.ggb03c = b.ggb03c and a.ggb02f = b.ggb02f and a.[type] = b.bz and a.[type] = 'gx' update a set pay_# = b.pay_#, pay_$ = b.pay_$, pay_$1 = b.pay_$1, pay_$2 = b.pay_$2, pay_$3 = b.pay_$3, pay_$4 = b.pay_$4, pay_$5 = b.pay_$5, pay_$6 = b.pay_$6, pay_$7 = b.pay_$7, pay_$8 = b.pay_$8, [pay_&] = b.[pay_&], pay_0 = b.pay_0, pay_1 = b.pay_1, pay_2 = b.pay_2, pay_3 = b.pay_3, pay_4 = b.pay_4, pay_5 = b.pay_5, pay_6 = b.pay_6, pay_7 = b.pay_7, pay_8 = b.pay_8, pay_9 = b.pay_9, pay_A = b.pay_A, pay_B = b.pay_B, pay_C = b.pay_C, pay_D = b.pay_D, pay_E = b.pay_E, pay_WX = b.pay_WX, pay_ZF = b.pay_ZF, pay_ZFB = b.pay_ZFB from #ggm02 a,#pays_result b where a.ggb00c = b.compid and a.ggb01c = b.ggb01c and a.ggb03c = b.ggb03c and a.ggb02f = b.ggb02f and a.[type] = b.bz and a.[type] = 'cp' update a set pay_# = b.pay_#, pay_$ = b.pay_$, pay_$1 = b.pay_$1, pay_$2 = b.pay_$2, pay_$3 = b.pay_$3, pay_$4 = b.pay_$4, pay_$5 = b.pay_$5, pay_$6 = b.pay_$6, pay_$7 = b.pay_$7, pay_$8 = b.pay_$8, [pay_&] = b.[pay_&], pay_0 = b.pay_0, pay_1 = b.pay_1, pay_2 = b.pay_2, pay_3 = b.pay_3, pay_4 = b.pay_4, pay_5 = b.pay_5, pay_6 = b.pay_6, pay_7 = b.pay_7, pay_8 = b.pay_8, pay_9 = b.pay_9, pay_A = b.pay_A, pay_B = b.pay_B, pay_C = b.pay_C, pay_D = b.pay_D, pay_E = b.pay_E, pay_WX = b.pay_WX, pay_ZF = b.pay_ZF, pay_ZFB = b.pay_ZFB from #ggm02 a,#pays_result b where a.ggb00c = b.compid and a.ggb01c = b.ggb01c and a.ggb03c = b.ggb03c and a.[type] = b.bz and a.[type] = 'gn' --充值 update a set pay_# = b.pay_#, pay_$ = b.pay_$, pay_$1 = b.pay_$1, pay_$2 = b.pay_$2, pay_$3 = b.pay_$3, pay_$4 = b.pay_$4, pay_$5 = b.pay_$5, pay_$6 = b.pay_$6, pay_$7 = b.pay_$7, pay_$8 = b.pay_$8, [pay_&] = b.[pay_&], pay_0 = b.pay_0, pay_1 = b.pay_1, pay_2 = b.pay_2, pay_3 = b.pay_3, pay_4 = b.pay_4, pay_5 = b.pay_5, pay_6 = b.pay_6, pay_7 = b.pay_7, pay_8 = b.pay_8, pay_9 = b.pay_9, pay_A = b.pay_A, pay_B = b.pay_B, pay_C = b.pay_C, pay_D = b.pay_D, pay_E = b.pay_E, pay_WX = b.pay_WX, pay_ZF = b.pay_ZF, pay_ZFB = b.pay_ZFB from #ggm02 a,#pays_result b where a.ggb00c = b.compid and a.ggb01c = b.ggb01c and a.[type] = b.bz and a.[type] = 'gz' --取款 update a set pay_# = b.pay_#, pay_$ = b.pay_$, pay_$1 = b.pay_$1, pay_$2 = b.pay_$2, pay_$3 = b.pay_$3, pay_$4 = b.pay_$4, pay_$5 = b.pay_$5, pay_$6 = b.pay_$6, pay_$7 = b.pay_$7, pay_$8 = b.pay_$8, [pay_&] = b.[pay_&], pay_0 = b.pay_0, pay_1 = b.pay_1, pay_2 = b.pay_2, pay_3 = b.pay_3, pay_4 = b.pay_4, pay_5 = b.pay_5, pay_6 = b.pay_6, pay_7 = b.pay_7, pay_8 = b.pay_8, pay_9 = b.pay_9, pay_A = b.pay_A, pay_B = b.pay_B, pay_C = b.pay_C, pay_D = b.pay_D, pay_E = b.pay_E, pay_WX = b.pay_WX, pay_ZF = b.pay_ZF, pay_ZFB = b.pay_ZFB from #ggm02 a,#pays_result b where a.ggb00c = b.compid and a.ggb01c = b.ggb01c and a.[type] = b.bz and a.[type] = 'qk' --还款 update a set pay_# = b.pay_#, pay_$ = b.pay_$, pay_$1 = b.pay_$1, pay_$2 = b.pay_$2, pay_$3 = b.pay_$3, pay_$4 = b.pay_$4, pay_$5 = b.pay_$5, pay_$6 = b.pay_$6, pay_$7 = b.pay_$7, pay_$8 = b.pay_$8, [pay_&] = b.[pay_&], pay_0 = b.pay_0, pay_1 = b.pay_1, pay_2 = b.pay_2, pay_3 = b.pay_3, pay_4 = b.pay_4, pay_5 = b.pay_5, pay_6 = b.pay_6, pay_7 = b.pay_7, pay_8 = b.pay_8, pay_9 = b.pay_9, pay_A = b.pay_A, pay_B = b.pay_B, pay_C = b.pay_C, pay_D = b.pay_D, pay_E = b.pay_E, pay_WX = b.pay_WX, pay_ZF = b.pay_ZF, pay_ZFB = b.pay_ZFB from #ggm02 a,#pays_result b where a.ggb00c = b.compid and a.ggb01c = b.ggb01c and a.[type] = b.bz and a.[type] = 'hk' --项目还款 update a set pay_# = b.pay_#, pay_$ = b.pay_$, pay_$1 = b.pay_$1, pay_$2 = b.pay_$2, pay_$3 = b.pay_$3, pay_$4 = b.pay_$4, pay_$5 = b.pay_$5, pay_$6 = b.pay_$6, pay_$7 = b.pay_$7, pay_$8 = b.pay_$8, [pay_&] = b.[pay_&], pay_0 = b.pay_0, pay_1 = b.pay_1, pay_2 = b.pay_2, pay_3 = b.pay_3, pay_4 = b.pay_4, pay_5 = b.pay_5, pay_6 = b.pay_6, pay_7 = b.pay_7, pay_8 = b.pay_8, pay_9 = b.pay_9, pay_A = b.pay_A, pay_B = b.pay_B, pay_C = b.pay_C, pay_D = b.pay_D, pay_E = b.pay_E, pay_WX = b.pay_WX, pay_ZF = b.pay_ZF, pay_ZFB = b.pay_ZFB from #ggm02 a,#pays_result b where a.ggb00c = b.compid and a.ggb01c = b.ggb01c and a.[type] = b.bz and a.[type] = 'xmhk' --产品还款 update a set pay_# = b.pay_#, pay_$ = b.pay_$, pay_$1 = b.pay_$1, pay_$2 = b.pay_$2, pay_$3 = b.pay_$3, pay_$4 = b.pay_$4, pay_$5 = b.pay_$5, pay_$6 = b.pay_$6, pay_$7 = b.pay_$7, pay_$8 = b.pay_$8, [pay_&] = b.[pay_&], pay_0 = b.pay_0, pay_1 = b.pay_1, pay_2 = b.pay_2, pay_3 = b.pay_3, pay_4 = b.pay_4, pay_5 = b.pay_5, pay_6 = b.pay_6, pay_7 = b.pay_7, pay_8 = b.pay_8, pay_9 = b.pay_9, pay_A = b.pay_A, pay_B = b.pay_B, pay_C = b.pay_C, pay_D = b.pay_D, pay_E = b.pay_E, pay_WX = b.pay_WX, pay_ZF = b.pay_ZF, pay_ZFB = b.pay_ZFB from #ggm02 a,#pays_result b where a.ggb00c = b.compid and a.ggb01c = b.ggb01c and a.[type] = b.bz and a.[type] = 'cphk' --疗程 update a set pay_# = b.pay_#, pay_$ = b.pay_$, pay_$1 = b.pay_$1, pay_$2 = b.pay_$2, pay_$3 = b.pay_$3, pay_$4 = b.pay_$4, pay_$5 = b.pay_$5, pay_$6 = b.pay_$6, pay_$7 = b.pay_$7, pay_$8 = b.pay_$8, [pay_&] = b.[pay_&], pay_0 = b.pay_0, pay_1 = b.pay_1, pay_2 = b.pay_2, pay_3 = b.pay_3, pay_4 = b.pay_4, pay_5 = b.pay_5, pay_6 = b.pay_6, pay_7 = b.pay_7, pay_8 = b.pay_8, pay_9 = b.pay_9, pay_A = b.pay_A, pay_B = b.pay_B, pay_C = b.pay_C, pay_D = b.pay_D, pay_E = b.pay_E, pay_WX = b.pay_WX, pay_ZF = b.pay_ZF, pay_ZFB = b.pay_ZFB from #ggm02 a,#pays_result b where a.ggb00c = b.compid and a.ggb01c = b.ggb01c and a.ggb02f = b.ggb02f and a.ggb03c = b.ggb03c and a.[type] = b.bz and a.[type] = 'lc' --充值套餐 update a set pay_# = b.pay_#, pay_$ = b.pay_$, pay_$1 = b.pay_$1, pay_$2 = b.pay_$2, pay_$3 = b.pay_$3, pay_$4 = b.pay_$4, pay_$5 = b.pay_$5, pay_$6 = b.pay_$6, pay_$7 = b.pay_$7, pay_$8 = b.pay_$8, [pay_&] = b.[pay_&], pay_0 = b.pay_0, pay_1 = b.pay_1, pay_2 = b.pay_2, pay_3 = b.pay_3, pay_4 = b.pay_4, pay_5 = b.pay_5, pay_6 = b.pay_6, pay_7 = b.pay_7, pay_8 = b.pay_8, pay_9 = b.pay_9, pay_A = b.pay_A, pay_B = b.pay_B, pay_C = b.pay_C, pay_D = b.pay_D, pay_E = b.pay_E, pay_WX = b.pay_WX, pay_ZF = b.pay_ZF, pay_ZFB = b.pay_ZFB from #ggm02 a,#pays_result b where a.ggb00c = b.compid and a.ggb01c = b.ggb01c and a.ggb03c = b.ggb03c and a.[type] = b.bz and a.[type] = 'gztc' --卖卡套餐 update a set pay_# = b.pay_#, pay_$ = b.pay_$, pay_$1 = b.pay_$1, pay_$2 = b.pay_$2, pay_$3 = b.pay_$3, pay_$4 = b.pay_$4, pay_$5 = b.pay_$5, pay_$6 = b.pay_$6, pay_$7 = b.pay_$7, pay_$8 = b.pay_$8, [pay_&] = b.[pay_&], pay_0 = b.pay_0, pay_1 = b.pay_1, pay_2 = b.pay_2, pay_3 = b.pay_3, pay_4 = b.pay_4, pay_5 = b.pay_5, pay_6 = b.pay_6, pay_7 = b.pay_7, pay_8 = b.pay_8, pay_9 = b.pay_9, pay_A = b.pay_A, pay_B = b.pay_B, pay_C = b.pay_C, pay_D = b.pay_D, pay_E = b.pay_E, pay_WX = b.pay_WX, pay_ZF = b.pay_ZF, pay_ZFB = b.pay_ZFB from #ggm02 a,#pays_result b where a.ggb00c = b.compid and a.ggb01c = b.ggb01c and a.ggb03c = b.ggb03c and a.[type] = b.bz and a.[type] = 'gntc' --卖卡疗程 update a set pay_# = b.pay_#, pay_$ = b.pay_$, pay_$1 = b.pay_$1, pay_$2 = b.pay_$2, pay_$3 = b.pay_$3, pay_$4 = b.pay_$4, pay_$5 = b.pay_$5, pay_$6 = b.pay_$6, pay_$7 = b.pay_$7, pay_$8 = b.pay_$8, [pay_&] = b.[pay_&], pay_0 = b.pay_0, pay_1 = b.pay_1, pay_2 = b.pay_2, pay_3 = b.pay_3, pay_4 = b.pay_4, pay_5 = b.pay_5, pay_6 = b.pay_6, pay_7 = b.pay_7, pay_8 = b.pay_8, pay_9 = b.pay_9, pay_A = b.pay_A, pay_B = b.pay_B, pay_C = b.pay_C, pay_D = b.pay_D, pay_E = b.pay_E, pay_WX = b.pay_WX, pay_ZF = b.pay_ZF, pay_ZFB = b.pay_ZFB from #ggm02 a,#pays_result b where a.ggb00c = b.compid and a.ggb01c = b.ggb01c and a.ggb03c = b.ggb03c and a.[type] = b.bz and a.[type] = 'gnlc' -- 项目消费(非散客) insert into #result(compid,customer,checkdate,checktime,cardno,cardtypecode,m_name,gender,billid,billtype,type_gz_gn,projectcode,projectname,projectclass,department1,department2,department3, department4,types1,employee1,ratio1,num1,types2,employee2,ratio2,num2,types3,employee3,ratio3,num3,types4,employee4,ratio4,num4, pay_#,pay_$,pay_$1,pay_$2,pay_$3,pay_$4,pay_$5,pay_$6,pay_$7,pay_$8,[pay_&],pay_0,pay_1,pay_2,pay_3,pay_4,pay_5,pay_6,pay_7,pay_8,pay_9,pay_A,pay_B,pay_C,pay_D,pay_E,pay_WX,pay_ZF,pay_ZFB,pay_lv, price,quantity,summoney,traaccount,trasummoney,mark ) select gga00c,case when isnull(gga13f,0) = 0 then '新客' else '老客' end,gga02d,substring(gga03t,1,2)+':'+substring(gga03t,3,2)+':'+substring(gga03t,5,2),gga09c,gca02c,gga06c, case when isnull(gga31i,0) = 0 then '女' else '男' end,gga01c,'消费','gx',b.ggb03c,gda03c,gsb03c,department1,department2,department3,department4, dmployeetype1,ggb12c_name,case when isnull(dmployeetype1,'') = '' then 0 else 1 end,case when isnull(dmployeetype1,'') = '' then 0 else ggb05f end, dmployeetype2,ggb13c_name,case when isnull(dmployeetype2,'') = '' then 0 else 1 end,case when isnull(dmployeetype2,'') = '' then 0 else ggb05f end, dmployeetype3,ggb14c_name,case when isnull(dmployeetype3,'') = '' then 0 else 1 end,case when isnull(dmployeetype3,'') = '' then 0 else ggb05f end, dmployeetype4,ggb26c_name,case when isnull(dmployeetype4,'') = '' then 0 else 1 end,case when isnull(dmployeetype4,'') = '' then 0 else ggb05f end, pay_#,pay_$,pay_$1,pay_$2,pay_$3,pay_$4,pay_$5,pay_$6,pay_$7,pay_$8,[pay_&],pay_0,pay_1,pay_2,pay_3,pay_4,pay_5,pay_6,pay_7,pay_8,pay_9,pay_A,pay_B,pay_C,pay_D,pay_E,pay_WX,pay_ZF,pay_ZFB, case when isnull(ggb11f,0) = 0 then gsn02c+':0' else gsn02c+':'+ convert(varchar(20),ggb11f) end, ggb10f,case when isnull(ggb11f,0) = 0 then gsn02c+':0' else gsn02c+':'+convert(varchar(20),convert(decimal(10,2),ggb11f/gga16f)) end,gga16f,gsn03c,-1*ggb11f,b.memo from ggm01 a with (nolock),ggm02 b with(nolock),gcm01 c with(nolock),gdm01 e with(nolock),#gsm02 f with(nolock),#ggm02 g,#gsm14 h where a.gga00c = b.ggb00c and a.gga01c = b.ggb01c and a.gga09c = c.gca01c and c.gca02c <> 'Z' and b.ggb00c = e.gda00c and b.ggb03c = e.gda01c and e.gda00c = f.gsb00c and e.gda13c = f.gsb02c and f.gsb01c = 'AJ' and b.ggb00c = g.ggb00c and b.ggb01c = g.ggb01c and b.ggb02f = g.ggb02f and b.ggb03c = g.ggb03c and g.[type] = 'gx' and b.ggb27c = h.gsn01c and a.gga00c = @compid and (isnull(@frombill,'*') = '*' or a.gga01c between @frombill and @tobill) and (isnull(@fromcard,'*') = '*' or a.gga09c between @fromcard and @tocard) and gga02d between @fromdate and @todate -- 项目消费(非散客) insert into #result(compid,customer,checkdate,checktime,cardno,cardtypecode,m_name,gender,billid,billtype,type_gz_gn,projectcode,projectname,projectclass,department1,department2,department3, department4,types1,employee1,ratio1,num1,types2,employee2,ratio2,num2,types3,employee3,ratio3,num3,types4,employee4,ratio4,num4, pay_#,pay_$,pay_$1,pay_$2,pay_$3,pay_$4,pay_$5,pay_$6,pay_$7,pay_$8,[pay_&],pay_0,pay_1,pay_2,pay_3,pay_4,pay_5,pay_6,pay_7,pay_8,pay_9,pay_A,pay_B,pay_C,pay_D,pay_E,pay_WX,pay_ZF,pay_ZFB,pay_lv, price,quantity,summoney,traaccount,trasummoney,mark ) select gga00c,case when isnull(gga13f,0) = 0 then '新客' else '老客' end,gga02d,substring(gga03t,1,2)+':'+substring(gga03t,3,2)+':'+substring(gga03t,5,2),gga09c,gca02c,gga06c, case when isnull(gga31i,0) = 0 then '女' else '男' end,gga01c,'消费','gx',b.ggb03c,gda03c,gsb03c,department1,department2,department3,department4, dmployeetype1,ggb12c_name,case when isnull(dmployeetype1,'') = '' then 0 else 1 end,case when isnull(dmployeetype1,'') = '' then 0 else ggb05f end, dmployeetype2,ggb13c_name,case when isnull(dmployeetype2,'') = '' then 0 else 1 end,case when isnull(dmployeetype2,'') = '' then 0 else ggb05f end, dmployeetype3,ggb14c_name,case when isnull(dmployeetype3,'') = '' then 0 else 1 end,case when isnull(dmployeetype3,'') = '' then 0 else ggb05f end, dmployeetype4,ggb26c_name,case when isnull(dmployeetype4,'') = '' then 0 else 1 end,case when isnull(dmployeetype4,'') = '' then 0 else ggb05f end, pay_#,pay_$,pay_$1,pay_$2,pay_$3,pay_$4,pay_$5,pay_$6,pay_$7,pay_$8,[pay_&],pay_0,pay_1,pay_2,pay_3,pay_4,pay_5,pay_6,pay_7,pay_8,pay_9,pay_A,pay_B,pay_C,pay_D,pay_E,pay_WX,pay_ZF,pay_ZFB, case when isnull(ggb11f,0) = 0 then gsn02c+':0' else gsn02c+':'+ convert(varchar(20),ggb11f) end, ggb10f,case when isnull(ggb11f,0) = 0 then gsn02c+':0' else gsn02c+':'+convert(varchar(20),convert(decimal(10,2),ggb11f/gga16f)) end,gga16f,gsn03c,-1*ggb11f,b.memo from ggm01 a with (nolock),ggm02 b with(nolock),gcm01 c with(nolock),gdm01 e with(nolock),#gsm02 f with(nolock),#ggm02 g,#gsm14 h where a.gga00c = b.ggb00c and a.gga01c = b.ggb01c and a.gga09c = c.gca01c and a.gga00c = c.gca00c and c.gca02c = 'Z' and b.ggb00c = e.gda00c and b.ggb03c = e.gda01c and e.gda00c = f.gsb00c and e.gda13c = f.gsb02c and f.gsb01c = 'AJ' and b.ggb00c = g.ggb00c and b.ggb01c = g.ggb01c and b.ggb02f = g.ggb02f and b.ggb03c = g.ggb03c and g.[type] = 'gx' and b.ggb27c = h.gsn01c and a.gga00c = @compid and (isnull(@frombill,'*') = '*' or a.gga01c between @frombill and @tobill) and (isnull(@fromcard,'*') = '*' or a.gga09c between @fromcard and @tocard) and gga02d between @fromdate and @todate -- 产品消费(非散客) insert into #result(compid,customer,checkdate,checktime,cardno,cardtypecode,m_name,gender,billid,billtype,type_gz_gn,projectcode,projectname,projectclass,department1,department2,department3, department4,types1,employee1,ratio1,num1,types2,employee2,ratio2,num2,types3,employee3,ratio3,num3,types4,employee4,ratio4,num4, pay_#,pay_$,pay_$1,pay_$2,pay_$3,pay_$4,pay_$5,pay_$6,pay_$7,pay_$8,[pay_&],pay_0,pay_1,pay_2,pay_3,pay_4,pay_5,pay_6,pay_7,pay_8,pay_9,pay_A,pay_B,pay_C,pay_D,pay_E,pay_WX,pay_ZF,pay_ZFB,pay_lv, price,quantity,summoney,traaccount,trasummoney,mark ) select gga00c,case when isnull(gga13f,0) = 0 then '新客' else '老客' end,gga02d,substring(gga03t,1,2)+':'+substring(gga03t,3,2)+':'+substring(gga03t,5,2),gga09c,gca02c,gga06c, case when isnull(gga31i,0) = 0 then '女' else '男' end,gga01c,'产品','cp',b.ggc03c,gfa03c,gsb03c,department1,department2,department3,department4, dmployeetype1,ggb12c_name,case when isnull(ggb12c_name,'') = '' then 0 else ggc14f end,case when isnull(ggb12c_name,'') = '' then 0 else ggc05f end, dmployeetype2,ggb13c_name,case when isnull(ggb13c_name,'') = '' then 0 else ggc16f end,case when isnull(ggb13c_name,'') = '' then 0 else ggc05f end, dmployeetype3,ggb14c_name,case when isnull(ggb14c_name,'') = '' then 0 else ggc18f end,case when isnull(ggb14c_name,'') = '' then 0 else ggc05f end, dmployeetype4,ggb26c_name,case when isnull(ggb26c_name,'') = '' then 0 else 1 end,case when isnull(ggb26c_name,'') = '' then 0 else ggc05f end, pay_#,pay_$,pay_$1,pay_$2,pay_$3,pay_$4,pay_$5,pay_$6,pay_$7,pay_$8,[pay_&],pay_0,pay_1,pay_2,pay_3,pay_4,pay_5,pay_6,pay_7,pay_8,pay_9,pay_A,pay_B,pay_C,pay_D,pay_E,pay_WX,pay_ZF,pay_ZFB, case when isnull(ggc11f,0) = 0 then gsn02c+':0' else gsn02c+':'+ convert(varchar(20),ggc11f) end, ggc10f,case when isnull(ggc11f,0) = 0 then gsn02c+':0' else gsn02c+':'+convert(varchar(20),convert(decimal(10,2),ggc11f/gga16f)) end,gga16f,gsn03c,-1*ggc11f, case when isnull(b.ggc36i,0) = 0 then '带走' when isnull(b.ggc36i,0) = 1 then '寄存' when isnull(b.ggc36i,0) = 2 then '欠货' else '' end from ggm01 a with (nolock),ggm03 b with(nolock),gcm01 c with(nolock),gfm01 e with(nolock),#gsm02 f with(nolock),#ggm02 g,#gsm14 h where a.gga00c = b.ggc00c and a.gga01c = b.ggc01c and a.gga09c = c.gca01c and c.gca02c <> 'Z' and b.ggc00c = e.gfa00c and b.ggc03c = e.gfa01c and e.gfa00c = f.gsb00c and e.gfa24c = f.gsb02c and f.gsb01c = 'AL' and b.ggc00c = g.ggb00c and b.ggc01c = g.ggb01c and b.ggc02f = g.ggb02f and b.ggc03c = g.ggb03c and g.[type] = 'cp' and b.ggc27c = h.gsn01c and a.gga00c = @compid and (isnull(@frombill,'*') = '*' or a.gga01c between @frombill and @tobill) and (isnull(@fromcard,'*') = '*' or a.gga09c between @fromcard and @tocard) and gga02d between @fromdate and @todate -- 产品消费(散客) insert into #result(compid,customer,checkdate,checktime,cardno,cardtypecode,m_name,gender,billid,billtype,type_gz_gn,projectcode,projectname,projectclass,department1,department2,department3, department4,types1,employee1,ratio1,num1,types2,employee2,ratio2,num2,types3,employee3,ratio3,num3,types4,employee4,ratio4,num4, pay_#,pay_$,pay_$1,pay_$2,pay_$3,pay_$4,pay_$5,pay_$6,pay_$7,pay_$8,[pay_&],pay_0,pay_1,pay_2,pay_3,pay_4,pay_5,pay_6,pay_7,pay_8,pay_9,pay_A,pay_B,pay_C,pay_D,pay_E,pay_WX,pay_ZF,pay_ZFB,pay_lv, price,quantity,summoney,traaccount,trasummoney,mark ) select gga00c,case when isnull(gga13f,0) = 0 then '新客' else '老客' end,gga02d,substring(gga03t,1,2)+':'+substring(gga03t,3,2)+':'+substring(gga03t,5,2),gga09c,gca02c,gga06c, case when isnull(gga31i,0) = 0 then '女' else '男' end,gga01c,'产品','cp',b.ggc03c,gfa03c,gsb03c,department1,department2,department3,department4, dmployeetype1,ggb12c_name,case when isnull(ggb12c_name,'') = '' then 0 else ggc14f end,case when isnull(ggb12c_name,'') = '' then 0 else ggc05f end, dmployeetype2,ggb13c_name,case when isnull(ggb13c_name,'') = '' then 0 else ggc16f end,case when isnull(ggb13c_name,'') = '' then 0 else ggc05f end, dmployeetype3,ggb14c_name,case when isnull(ggb14c_name,'') = '' then 0 else ggc18f end,case when isnull(ggb14c_name,'') = '' then 0 else ggc05f end, dmployeetype4,ggb26c_name,case when isnull(ggb26c_name,'') = '' then 0 else 1 end,case when isnull(ggb26c_name,'') = '' then 0 else ggc05f end, pay_#,pay_$,pay_$1,pay_$2,pay_$3,pay_$4,pay_$5,pay_$6,pay_$7,pay_$8,[pay_&],pay_0,pay_1,pay_2,pay_3,pay_4,pay_5,pay_6,pay_7,pay_8,pay_9,pay_A,pay_B,pay_C,pay_D,pay_E,pay_WX,pay_ZF,pay_ZFB, case when isnull(ggc11f,0) = 0 then gsn02c+':0' else gsn02c+':'+ convert(varchar(20),ggc11f) end, ggc10f,case when isnull(ggc11f,0) = 0 then gsn02c+':0' else gsn02c+':'+convert(varchar(20),convert(decimal(10,2),ggc11f/gga16f)) end,gga16f,gsn03c,-1*ggc11f, case when isnull(b.ggc36i,0) = 0 then '带走' when isnull(b.ggc36i,0) = 1 then '寄存' when isnull(b.ggc36i,0) = 2 then '欠货' else '' end from ggm01 a with (nolock),ggm03 b with(nolock),gcm01 c with(nolock),gfm01 e with(nolock),#gsm02 f with(nolock),#ggm02 g,#gsm14 h where a.gga00c = b.ggc00c and a.gga01c = b.ggc01c and a.gga09c = c.gca01c and a.gga00c = c.gca00c and c.gca02c = 'Z' and b.ggc00c = e.gfa00c and b.ggc03c = e.gfa01c and e.gfa00c = f.gsb00c and e.gfa24c = f.gsb02c and f.gsb01c = 'AL' and b.ggc00c = g.ggb00c and b.ggc01c = g.ggb01c and b.ggc02f = g.ggb02f and b.ggc03c = g.ggb03c and g.[type] = 'cp' and b.ggc27c = h.gsn01c and a.gga00c = @compid and (isnull(@frombill,'*') = '*' or a.gga01c between @frombill and @tobill) and (isnull(@fromcard,'*') = '*' or a.gga09c between @fromcard and @tocard) and gga02d between @fromdate and @todate -- 卖卡 insert into #result(compid,customer,checkdate,checktime,cardno,cardtypecode,m_name,gender,billid,billtype,type_gz_gn,projectcode,projectname,projectclass,department1,department2,department3, department4,types1,employee1,ratio1,num1,types2,employee2,ratio2,num2,types3,employee3,ratio3,num3,types4,employee4,ratio4,num4, pay_#,pay_$,pay_$1,pay_$2,pay_$3,pay_$4,pay_$5,pay_$6,pay_$7,pay_$8,[pay_&],pay_0,pay_1,pay_2,pay_3,pay_4,pay_5,pay_6,pay_7,pay_8,pay_9,pay_A,pay_B,pay_C,pay_D,pay_E,pay_WX,pay_ZF,pay_ZFB,pay_lv, price,quantity,summoney,traaccount,trasummoney,mark ) select gna00c,'新客',gna02d,substring(gna12t,1,2)+':'+substring(gna12t,3,2)+':'+substring(gna12t,5,2),gnb03c,gnb02c,gna06c, case when isnull(gna85i,0) = 0 then '女' else '男' end,gna01c,'卖卡','gn','','','',department1,department2,department3,department4, dmployeetype1,ggb12c_name,case when isnull(ggb12c_name,'') = '' then 0 else gna27f end,case when isnull(ggb12c_name,'') = '' then 0 else 1 end, dmployeetype2,ggb13c_name,case when isnull(ggb13c_name,'') = '' then 0 else gna29f end,case when isnull(ggb13c_name,'') = '' then 0 else 1 end, dmployeetype3,ggb14c_name,case when isnull(ggb14c_name,'') = '' then 0 else gna31f end,case when isnull(ggb14c_name,'') = '' then 0 else 1 end, dmployeetype4,ggb26c_name,case when isnull(ggb26c_name,'') = '' then 0 else gna33f end,case when isnull(ggb26c_name,'') = '' then 0 else 1 end, pay_#,pay_$,pay_$1,pay_$2,pay_$3,pay_$4,pay_$5,pay_$6,pay_$7,pay_$8,[pay_&],pay_0,pay_1,pay_2,pay_3,pay_4,pay_5,pay_6,pay_7,pay_8,pay_9,pay_A,pay_B,pay_C,pay_D,pay_E,pay_WX,pay_ZF,pay_ZFB, case when pay_# = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '#')+ ':'+ convert(varchar(10),pay_#)+'; ' end + case when pay_$ = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$')+ ':'+ convert(varchar(10),pay_$)+'; ' end + case when pay_$1 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$1')+ ':'+ convert(varchar(10),pay_$1)+'; ' end + case when pay_$2 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$2')+ ':'+ convert(varchar(10),pay_$2)+'; ' end + case when pay_$3 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$3')+ ':'+ convert(varchar(10),pay_$3)+'; ' end + case when pay_$4 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$4')+ ':'+ convert(varchar(10),pay_$4)+'; ' end + case when pay_$5 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$5')+ ':'+ convert(varchar(10),pay_$5)+'; ' end + case when pay_$6 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$6')+ ':'+ convert(varchar(10),pay_$6)+'; ' end + case when pay_$7 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$7')+ ':'+ convert(varchar(10),pay_$7)+'; ' end + case when pay_$8 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$8')+ ':'+ convert(varchar(10),pay_$8)+'; ' end + case when pay_0 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '0')+ ':'+ convert(varchar(10),pay_0)+'; ' end + case when pay_1 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '1')+ ':'+ convert(varchar(10),pay_1)+'; ' end + case when pay_2 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '2')+ ':'+ convert(varchar(10),pay_2)+'; ' end + case when pay_3 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '3')+ ':'+ convert(varchar(10),pay_3)+'; ' end + case when pay_4 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '4')+ ':'+ convert(varchar(10),pay_4)+'; ' end + case when pay_5 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '5')+ ':'+ convert(varchar(10),pay_5)+'; ' end + case when pay_6 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '6')+ ':'+ convert(varchar(10),pay_6)+'; ' end + case when pay_7 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '7')+ ':'+ convert(varchar(10),pay_7)+'; ' end + case when pay_8 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '8')+ ':'+ convert(varchar(10),pay_8)+'; ' end + case when pay_9 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '9')+ ':'+ convert(varchar(10),pay_9)+'; ' end + case when pay_A = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'A')+ ':'+ convert(varchar(10),pay_A)+'; ' end + case when pay_B = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'B')+ ':'+ convert(varchar(10),pay_B)+'; ' end + case when pay_C = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'C')+ ':'+ convert(varchar(10),pay_C)+'; ' end + case when pay_D = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'D')+ ':'+ convert(varchar(10),pay_D)+'; ' end + case when pay_E = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'E')+ ':'+ convert(varchar(10),pay_E)+'; ' end + case when pay_WX = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'WX')+ ':'+ convert(varchar(10),pay_WX) end + case when pay_ZF = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'ZF')+ ':'+ convert(varchar(10),pay_ZF) end + case when pay_ZFB = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'ZFB')+ ':'+ convert(varchar(10),pay_ZFB) end , b.gnb08f, case when pay_# = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '#')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_#/gnb08f))+'; ' end + case when pay_$ = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$/gnb08f))+'; ' end + case when pay_$1 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$1')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$1/gnb08f))+'; ' end + case when pay_$2 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$2')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$2/gnb08f))+'; ' end + case when pay_$3 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$3')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$3/gnb08f))+'; ' end + case when pay_$4 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$4')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$4/gnb08f))+'; ' end + case when pay_$5 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$5')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$5/gnb08f))+'; ' end + case when pay_$6 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$6')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$6/gnb08f))+'; ' end + case when pay_$7 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$7')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$7/gnb08f))+'; ' end + case when pay_$8 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$8')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$8/gnb08f))+'; ' end + case when pay_0 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '0')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_0/gnb08f))+'; ' end + case when pay_1 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '1')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_1/gnb08f))+'; ' end + case when pay_2 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '2')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_2/gnb08f))+'; ' end + case when pay_3 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '3')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_3/gnb08f))+'; ' end + case when pay_4 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '4')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_4/gnb08f))+'; ' end + case when pay_5 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '5')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_5/gnb08f))+'; ' end + case when pay_6 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '6')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_6/gnb08f))+'; ' end + case when pay_7 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '7')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_7/gnb08f))+'; ' end + case when pay_8 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '8')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_8/gnb08f))+'; ' end + case when pay_9 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '9')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_9/gnb08f))+'; ' end + case when pay_A = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'A')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_A/gnb08f))+'; ' end + case when pay_B = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'B')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_B/gnb08f))+'; ' end + case when pay_C = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'C')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_C/gnb08f))+'; ' end + case when pay_D = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'D')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_D/gnb08f))+'; ' end + case when pay_E = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'E')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_E/gnb08f))+'; ' end + case when pay_WX = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'WX')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_WX/gnb08f)) end + case when pay_ZF = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'ZF')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_ZF/gnb08f)) end + case when pay_ZFB = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'ZFB')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_ZFB/gnb08f)) end ,gnb11f,gsb03c,gnb11f,gna07c from gnm01 a with (nolock),gnm02 b with(nolock),#ggm02 g,#gsm02 h where a.gna00c = b.gnb00c and a.gna01c = b.gnb01c and b.gnb00c = g.ggb00c and b.gnb01c = g.ggb01c and g.[type] = 'gn' and b.gnb13i = h.gsb02c and h.gsb01c = 'O' and a.gna00c = @compid and (isnull(@frombill,'*') = '*' or a.gna01c between @frombill and @tobill) and (isnull(@fromcard,'*') = '*' or b.gnb03c between @fromcard and @tocard) and gna02d between @fromdate and @todate -- 卖卡 insert into #result(compid,customer,checkdate,checktime,cardno,cardtypecode,m_name,gender,billid,billtype,type_gz_gn,projectcode,projectname,projectclass,department1,department2,department3, department4,types1,employee1,ratio1,num1,types2,employee2,ratio2,num2,types3,employee3,ratio3,num3,types4,employee4,ratio4,num4, pay_#,pay_$,pay_$1,pay_$2,pay_$3,pay_$4,pay_$5,pay_$6,pay_$7,pay_$8,[pay_&],pay_0,pay_1,pay_2,pay_3,pay_4,pay_5,pay_6,pay_7,pay_8,pay_9,pay_A,pay_B,pay_C,pay_D,pay_E,pay_WX,pay_ZF,pay_ZFB,pay_lv, price,quantity,summoney,traaccount,trasummoney,mark ) select gna00c,'新客',gna02d,substring(gna12t,1,2)+':'+substring(gna12t,3,2)+':'+substring(gna12t,5,2),gnb03c,gnb02c,gna06c, case when isnull(gna85i,0) = 0 then '女' else '男' end,gna01c,'卖卡','gntc',ggb03c,gta02c,'',department1,department2,department3,department4, dmployeetype1,ggb12c_name,case when isnull(ggb12c_name,'') = '' then 0 else gna27f end,case when isnull(ggb12c_name,'') = '' then 0 else 1 end, dmployeetype2,ggb13c_name,case when isnull(ggb13c_name,'') = '' then 0 else gna29f end,case when isnull(ggb13c_name,'') = '' then 0 else 1 end, dmployeetype3,ggb14c_name,case when isnull(ggb14c_name,'') = '' then 0 else gna31f end,case when isnull(ggb14c_name,'') = '' then 0 else 1 end, dmployeetype4,ggb26c_name,case when isnull(ggb26c_name,'') = '' then 0 else gna33f end,case when isnull(ggb26c_name,'') = '' then 0 else 1 end, pay_#,pay_$,pay_$1,pay_$2,pay_$3,pay_$4,pay_$5,pay_$6,pay_$7,pay_$8,[pay_&],pay_0,pay_1,pay_2,pay_3,pay_4,pay_5,pay_6,pay_7,pay_8,pay_9,pay_A,pay_B,pay_C,pay_D,pay_E,pay_WX,pay_ZF,pay_ZFB, case when pay_# = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '#')+ ':'+ convert(varchar(10),pay_#)+'; ' end + case when pay_$ = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$')+ ':'+ convert(varchar(10),pay_$)+'; ' end + case when pay_$1 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$1')+ ':'+ convert(varchar(10),pay_$1)+'; ' end + case when pay_$2 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$2')+ ':'+ convert(varchar(10),pay_$2)+'; ' end + case when pay_$3 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$3')+ ':'+ convert(varchar(10),pay_$3)+'; ' end + case when pay_$4 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$4')+ ':'+ convert(varchar(10),pay_$4)+'; ' end + case when pay_$5 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$5')+ ':'+ convert(varchar(10),pay_$5)+'; ' end + case when pay_$6 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$6')+ ':'+ convert(varchar(10),pay_$6)+'; ' end + case when pay_$7 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$7')+ ':'+ convert(varchar(10),pay_$7)+'; ' end + case when pay_$8 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$8')+ ':'+ convert(varchar(10),pay_$8)+'; ' end + case when pay_0 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '0')+ ':'+ convert(varchar(10),pay_0)+'; ' end + case when pay_1 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '1')+ ':'+ convert(varchar(10),pay_1)+'; ' end + case when pay_2 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '2')+ ':'+ convert(varchar(10),pay_2)+'; ' end + case when pay_3 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '3')+ ':'+ convert(varchar(10),pay_3)+'; ' end + case when pay_4 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '4')+ ':'+ convert(varchar(10),pay_4)+'; ' end + case when pay_5 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '5')+ ':'+ convert(varchar(10),pay_5)+'; ' end + case when pay_6 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '6')+ ':'+ convert(varchar(10),pay_6)+'; ' end + case when pay_7 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '7')+ ':'+ convert(varchar(10),pay_7)+'; ' end + case when pay_8 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '8')+ ':'+ convert(varchar(10),pay_8)+'; ' end + case when pay_9 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '9')+ ':'+ convert(varchar(10),pay_9)+'; ' end + case when pay_A = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'A')+ ':'+ convert(varchar(10),pay_A)+'; ' end + case when pay_B = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'B')+ ':'+ convert(varchar(10),pay_B)+'; ' end + case when pay_C = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'C')+ ':'+ convert(varchar(10),pay_C)+'; ' end + case when pay_D = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'D')+ ':'+ convert(varchar(10),pay_D)+'; ' end + case when pay_E = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'E')+ ':'+ convert(varchar(10),pay_E)+'; ' end + case when pay_WX = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'WX')+ ':'+ convert(varchar(10),pay_WX) end + case when pay_ZF = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'ZF')+ ':'+ convert(varchar(10),pay_ZF) end + case when pay_ZFB = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'ZFB')+ ':'+ convert(varchar(10),pay_ZFB) end , b.gnb08f, case when pay_# = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '#')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_#/gnb08f))+'; ' end + case when pay_$ = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$/gnb08f))+'; ' end + case when pay_$1 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$1')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$1/gnb08f))+'; ' end + case when pay_$2 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$2')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$2/gnb08f))+'; ' end + case when pay_$3 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$3')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$3/gnb08f))+'; ' end + case when pay_$4 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$4')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$4/gnb08f))+'; ' end + case when pay_$5 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$5')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$5/gnb08f))+'; ' end + case when pay_$6 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$6')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$6/gnb08f))+'; ' end + case when pay_$7 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$7')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$7/gnb08f))+'; ' end + case when pay_$8 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$8')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$8/gnb08f))+'; ' end + case when pay_0 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '0')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_0/gnb08f))+'; ' end + case when pay_1 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '1')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_1/gnb08f))+'; ' end + case when pay_2 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '2')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_2/gnb08f))+'; ' end + case when pay_3 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '3')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_3/gnb08f))+'; ' end + case when pay_4 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '4')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_4/gnb08f))+'; ' end + case when pay_5 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '5')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_5/gnb08f))+'; ' end + case when pay_6 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '6')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_6/gnb08f))+'; ' end + case when pay_7 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '7')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_7/gnb08f))+'; ' end + case when pay_8 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '8')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_8/gnb08f))+'; ' end + case when pay_9 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '9')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_9/gnb08f))+'; ' end + case when pay_A = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'A')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_A/gnb08f))+'; ' end + case when pay_B = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'B')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_B/gnb08f))+'; ' end + case when pay_C = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'C')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_C/gnb08f))+'; ' end + case when pay_D = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'D')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_D/gnb08f))+'; ' end + case when pay_E = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'E')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_E/gnb08f))+'; ' end + case when pay_WX = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'WX')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_WX/gnb08f)) end + case when pay_ZF = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'ZF')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_ZF/gnb08f)) end + case when pay_ZFB = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'ZFB')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_ZFB/gnb08f)) end ,gnb08f,gsb03c,gnb08f,gna07c from gnm01 a with (nolock),gnm02 b with(nolock),#ggm02 g,#gsm02 h,gtc01 k with(nolock) where a.gna00c = b.gnb00c and a.gna01c = b.gnb01c and b.gnb00c = g.ggb00c and b.gnb01c = g.ggb01c and g.[type] = 'gntc' and b.gnb13i = h.gsb02c and h.gsb01c = 'O' and g.ggb00c = k.gta00c and g.ggb03c = k.gta01c and a.gna00c = @compid and (isnull(@frombill,'*') = '*' or a.gna01c between @frombill and @tobill) and (isnull(@fromcard,'*') = '*' or b.gnb03c between @fromcard and @tocard) and gna02d between @fromdate and @todate -- 卖卡 insert into #result(compid,customer,checkdate,checktime,cardno,cardtypecode,m_name,gender,billid,billtype,type_gz_gn,projectcode,projectname,projectclass,department1,department2,department3, department4,types1,employee1,ratio1,num1,types2,employee2,ratio2,num2,types3,employee3,ratio3,num3,types4,employee4,ratio4,num4, pay_#,pay_$,pay_$1,pay_$2,pay_$3,pay_$4,pay_$5,pay_$6,pay_$7,pay_$8,[pay_&],pay_0,pay_1,pay_2,pay_3,pay_4,pay_5,pay_6,pay_7,pay_8,pay_9,pay_A,pay_B,pay_C,pay_D,pay_E,pay_WX,pay_ZF,pay_ZFB,pay_lv, price,quantity,summoney,traaccount,trasummoney,mark ) select gna00c,'新客',gna02d,substring(gna12t,1,2)+':'+substring(gna12t,3,2)+':'+substring(gna12t,5,2),gnb03c,gnb02c,gna06c, case when isnull(gna85i,0) = 0 then '女' else '男' end,gna01c,'卖卡','gnlc',ggb03c,gda03c,'',department1,department2,department3,department4, dmployeetype1,ggb12c_name,case when isnull(ggb12c_name,'') = '' then 0 else gna27f end,case when isnull(ggb12c_name,'') = '' then 0 else 1 end, dmployeetype2,ggb13c_name,case when isnull(ggb13c_name,'') = '' then 0 else gna29f end,case when isnull(ggb13c_name,'') = '' then 0 else 1 end, dmployeetype3,ggb14c_name,case when isnull(ggb14c_name,'') = '' then 0 else gna31f end,case when isnull(ggb14c_name,'') = '' then 0 else 1 end, dmployeetype4,ggb26c_name,case when isnull(ggb26c_name,'') = '' then 0 else gna33f end,case when isnull(ggb26c_name,'') = '' then 0 else 1 end, pay_#,pay_$,pay_$1,pay_$2,pay_$3,pay_$4,pay_$5,pay_$6,pay_$7,pay_$8,[pay_&],pay_0,pay_1,pay_2,pay_3,pay_4,pay_5,pay_6,pay_7,pay_8,pay_9,pay_A,pay_B,pay_C,pay_D,pay_E,pay_WX,pay_ZF,pay_ZFB, case when pay_# = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '#')+ ':'+ convert(varchar(10),pay_#)+'; ' end + case when pay_$ = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$')+ ':'+ convert(varchar(10),pay_$)+'; ' end + case when pay_$1 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$1')+ ':'+ convert(varchar(10),pay_$1)+'; ' end + case when pay_$2 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$2')+ ':'+ convert(varchar(10),pay_$2)+'; ' end + case when pay_$3 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$3')+ ':'+ convert(varchar(10),pay_$3)+'; ' end + case when pay_$4 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$4')+ ':'+ convert(varchar(10),pay_$4)+'; ' end + case when pay_$5 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$5')+ ':'+ convert(varchar(10),pay_$5)+'; ' end + case when pay_$6 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$6')+ ':'+ convert(varchar(10),pay_$6)+'; ' end + case when pay_$7 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$7')+ ':'+ convert(varchar(10),pay_$7)+'; ' end + case when pay_$8 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$8')+ ':'+ convert(varchar(10),pay_$8)+'; ' end + case when pay_0 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '0')+ ':'+ convert(varchar(10),pay_0)+'; ' end + case when pay_1 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '1')+ ':'+ convert(varchar(10),pay_1)+'; ' end + case when pay_2 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '2')+ ':'+ convert(varchar(10),pay_2)+'; ' end + case when pay_3 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '3')+ ':'+ convert(varchar(10),pay_3)+'; ' end + case when pay_4 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '4')+ ':'+ convert(varchar(10),pay_4)+'; ' end + case when pay_5 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '5')+ ':'+ convert(varchar(10),pay_5)+'; ' end + case when pay_6 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '6')+ ':'+ convert(varchar(10),pay_6)+'; ' end + case when pay_7 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '7')+ ':'+ convert(varchar(10),pay_7)+'; ' end + case when pay_8 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '8')+ ':'+ convert(varchar(10),pay_8)+'; ' end + case when pay_9 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '9')+ ':'+ convert(varchar(10),pay_9)+'; ' end + case when pay_A = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'A')+ ':'+ convert(varchar(10),pay_A)+'; ' end + case when pay_B = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'B')+ ':'+ convert(varchar(10),pay_B)+'; ' end + case when pay_C = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'C')+ ':'+ convert(varchar(10),pay_C)+'; ' end + case when pay_D = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'D')+ ':'+ convert(varchar(10),pay_D)+'; ' end + case when pay_E = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'E')+ ':'+ convert(varchar(10),pay_E)+'; ' end + case when pay_WX = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'WX')+ ':'+ convert(varchar(10),pay_WX) end + case when pay_ZF = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'ZF')+ ':'+ convert(varchar(10),pay_ZF) end + case when pay_ZFB = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'ZFB')+ ':'+ convert(varchar(10),pay_ZFB) end , b.gnb08f, case when pay_# = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '#')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_#/gnb08f))+'; ' end + case when pay_$ = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$/gnb08f))+'; ' end + case when pay_$1 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$1')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$1/gnb08f))+'; ' end + case when pay_$2 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$2')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$2/gnb08f))+'; ' end + case when pay_$3 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$3')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$3/gnb08f))+'; ' end + case when pay_$4 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$4')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$4/gnb08f))+'; ' end + case when pay_$5 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$5')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$5/gnb08f))+'; ' end + case when pay_$6 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$6')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$6/gnb08f))+'; ' end + case when pay_$7 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$7')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$7/gnb08f))+'; ' end + case when pay_$8 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$8')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$8/gnb08f))+'; ' end + case when pay_0 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '0')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_0/gnb08f))+'; ' end + case when pay_1 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '1')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_1/gnb08f))+'; ' end + case when pay_2 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '2')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_2/gnb08f))+'; ' end + case when pay_3 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '3')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_3/gnb08f))+'; ' end + case when pay_4 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '4')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_4/gnb08f))+'; ' end + case when pay_5 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '5')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_5/gnb08f))+'; ' end + case when pay_6 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '6')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_6/gnb08f))+'; ' end + case when pay_7 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '7')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_7/gnb08f))+'; ' end + case when pay_8 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '8')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_8/gnb08f))+'; ' end + case when pay_9 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '9')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_9/gnb08f))+'; ' end + case when pay_A = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'A')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_A/gnb08f))+'; ' end + case when pay_B = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'B')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_B/gnb08f))+'; ' end + case when pay_C = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'C')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_C/gnb08f))+'; ' end + case when pay_D = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'D')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_D/gnb08f))+'; ' end + case when pay_E = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'E')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_E/gnb08f))+'; ' end + case when pay_WX = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'WX')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_WX/gnb08f)) end + case when pay_ZF = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'ZF')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_ZF/gnb08f)) end + case when pay_ZFB = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'ZFB')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_ZFB/gnb08f)) end ,gnb08f,gsb03c,gnb08f,gna07c from gnm01 a with (nolock),gnm02 b with(nolock),#ggm02 g,#gsm02 h,gdm01 k with(nolock) where a.gna00c = b.gnb00c and a.gna01c = b.gnb01c and b.gnb00c = g.ggb00c and b.gnb01c = g.ggb01c and g.[type] = 'gnlc' and b.gnb13i = h.gsb02c and h.gsb01c = 'O' and g.ggb00c = k.gda00c and g.ggb03c = k.gda01c and a.gna00c = @compid and (isnull(@frombill,'*') = '*' or a.gna01c between @frombill and @tobill) and (isnull(@fromcard,'*') = '*' or b.gnb03c between @fromcard and @tocard) and gna02d between @fromdate and @todate -- 充值 insert into #result(compid,customer,checkdate,checktime,cardno,cardtypecode,m_name,gender,billid,billtype,type_gz_gn,projectcode,projectname,projectclass,department1,department2,department3, department4,types1,employee1,ratio1,num1,types2,employee2,ratio2,num2,types3,employee3,ratio3,num3,types4,employee4,ratio4,num4, pay_#,pay_$,pay_$1,pay_$2,pay_$3,pay_$4,pay_$5,pay_$6,pay_$7,pay_$8,[pay_&],pay_0,pay_1,pay_2,pay_3,pay_4,pay_5,pay_6,pay_7,pay_8,pay_9,pay_A,pay_B,pay_C,pay_D,pay_E,pay_WX,pay_ZF,pay_ZFB,pay_lv, price,quantity,summoney,traaccount,trasummoney,mark ) select gcl00c,'老客',gcl02d,substring(gcl56t,1,2)+':'+substring(gcl56t,3,2)+':'+substring(gcl56t,5,2),gcl03c,gca02c,gba03c, case when isnull(gba14i,0) = 0 then '女' else '男' end,gcl01c,'充值','gz','','','',department1,department2,department3,department4, dmployeetype1,ggb12c_name,case when isnull(ggb12c_name,'') = '' then 0 else gcl16f end,case when isnull(ggb12c_name,'') = '' then 0 else 1 end, dmployeetype2,ggb13c_name,case when isnull(ggb13c_name,'') = '' then 0 else gcl18f end,case when isnull(ggb13c_name,'') = '' then 0 else 1 end, dmployeetype3,ggb14c_name,case when isnull(ggb14c_name,'') = '' then 0 else gcl20f end,case when isnull(ggb14c_name,'') = '' then 0 else 1 end, dmployeetype4,ggb26c_name,case when isnull(ggb26c_name,'') = '' then 0 else gcl23f end,case when isnull(ggb26c_name,'') = '' then 0 else 1 end, pay_#,pay_$,pay_$1,pay_$2,pay_$3,pay_$4,pay_$5,pay_$6,pay_$7,pay_$8,[pay_&],pay_0,pay_1,pay_2,pay_3,pay_4,pay_5,pay_6,pay_7,pay_8,pay_9,pay_A,pay_B,pay_C,pay_D,pay_E,pay_WX,pay_ZF,pay_ZFB, case when pay_# = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '#')+ ':'+ convert(varchar(10),pay_#)+'; ' end + case when pay_$ = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$')+ ':'+ convert(varchar(10),pay_$)+'; ' end + case when pay_$1 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$1')+ ':'+ convert(varchar(10),pay_$1)+'; ' end + case when pay_$2 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$2')+ ':'+ convert(varchar(10),pay_$2)+'; ' end + case when pay_$3 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$3')+ ':'+ convert(varchar(10),pay_$3)+'; ' end + case when pay_$4 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$4')+ ':'+ convert(varchar(10),pay_$4)+'; ' end + case when pay_$5 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$5')+ ':'+ convert(varchar(10),pay_$5)+'; ' end + case when pay_$6 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$6')+ ':'+ convert(varchar(10),pay_$6)+'; ' end + case when pay_$7 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$7')+ ':'+ convert(varchar(10),pay_$7)+'; ' end + case when pay_$8 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$8')+ ':'+ convert(varchar(10),pay_$8)+'; ' end + case when pay_0 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '0')+ ':'+ convert(varchar(10),pay_0)+'; ' end + case when pay_1 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '1')+ ':'+ convert(varchar(10),pay_1)+'; ' end + case when pay_2 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '2')+ ':'+ convert(varchar(10),pay_2)+'; ' end + case when pay_3 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '3')+ ':'+ convert(varchar(10),pay_3)+'; ' end + case when pay_4 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '4')+ ':'+ convert(varchar(10),pay_4)+'; ' end + case when pay_5 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '5')+ ':'+ convert(varchar(10),pay_5)+'; ' end + case when pay_6 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '6')+ ':'+ convert(varchar(10),pay_6)+'; ' end + case when pay_7 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '7')+ ':'+ convert(varchar(10),pay_7)+'; ' end + case when pay_8 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '8')+ ':'+ convert(varchar(10),pay_8)+'; ' end + case when pay_9 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '9')+ ':'+ convert(varchar(10),pay_9)+'; ' end + case when pay_A = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'A')+ ':'+ convert(varchar(10),pay_A)+'; ' end + case when pay_B = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'B')+ ':'+ convert(varchar(10),pay_B)+'; ' end + case when pay_C = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'C')+ ':'+ convert(varchar(10),pay_C)+'; ' end + case when pay_D = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'D')+ ':'+ convert(varchar(10),pay_D)+'; ' end + case when pay_E = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'E')+ ':'+ convert(varchar(10),pay_E)+'; ' end + case when pay_WX = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'WX')+ ':'+ convert(varchar(10),pay_WX) end + case when pay_ZF = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'ZF')+ ':'+ convert(varchar(10),pay_ZF) end + case when pay_ZFB = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'ZFB')+ ':'+ convert(varchar(10),pay_ZFB) end , a.gcl07f, case when pay_# = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '#')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_#/gcl07f))+'; ' end + case when pay_$ = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$/gcl07f))+'; ' end + case when pay_$1 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$1')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$1/gcl07f))+'; ' end + case when pay_$2 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$2')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$2/gcl07f))+'; ' end + case when pay_$3 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$3')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$3/gcl07f))+'; ' end + case when pay_$4 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$4')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$4/gcl07f))+'; ' end + case when pay_$5 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$5')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$5/gcl07f))+'; ' end + case when pay_$6 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$6')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$6/gcl07f))+'; ' end + case when pay_$7 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$7')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$7/gcl07f))+'; ' end + case when pay_$8 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$8')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$8/gcl07f))+'; ' end + case when pay_0 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '0')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_0/gcl07f))+'; ' end + case when pay_1 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '1')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_1/gcl07f))+'; ' end + case when pay_2 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '2')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_2/gcl07f))+'; ' end + case when pay_3 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '3')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_3/gcl07f))+'; ' end + case when pay_4 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '4')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_4/gcl07f))+'; ' end + case when pay_5 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '5')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_5/gcl07f))+'; ' end + case when pay_6 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '6')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_6/gcl07f))+'; ' end + case when pay_7 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '7')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_7/gcl07f))+'; ' end + case when pay_8 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '8')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_8/gcl07f))+'; ' end + case when pay_9 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '9')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_9/gcl07f))+'; ' end + case when pay_A = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'A')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_A/gcl07f))+'; ' end + case when pay_B = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'B')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_B/gcl07f))+'; ' end + case when pay_C = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'C')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_C/gcl07f))+'; ' end + case when pay_D = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'D')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_D/gcl07f))+'; ' end + case when pay_E = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'E')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_E/gcl07f))+'; ' end + case when pay_WX = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'WX')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_WX/gcl07f)) end + case when pay_ZF = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'ZF')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_ZF/gcl07f)) end + case when pay_ZFB = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'ZFB')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_ZFB/gcl07f)) end ,gcl07f,gsb03c,gcl07f,isnull(gcl57c,'') from gcm10 a with (nolock),#ggm02 b ,gcm01 c with(nolock),gbm01 d with(nolock),#gsm02 e where gcl00c = b.ggb00c and gcl01c = b.ggb01c and a.gcl06i = 0 and b.[type] = 'gz' and a.gcl03c = c.gca01c and c.gca13d = d.gba00c and c.gca04c = d.gba01c and a.gcl04c = e.gsb02c and e.gsb01c = 'O' and a.gcl00c = @compid and (isnull(@frombill,'*') = '*' or a.gcl01c between @frombill and @tobill) and (isnull(@fromcard,'*') = '*' or a.gcl03c between @fromcard and @tocard) and gcl02d between @fromdate and @todate -- 充值 insert into #result(compid,customer,checkdate,checktime,cardno,cardtypecode,m_name,gender,billid,billtype,type_gz_gn,projectcode,projectname,projectclass,department1,department2,department3, department4,types1,employee1,ratio1,num1,types2,employee2,ratio2,num2,types3,employee3,ratio3,num3,types4,employee4,ratio4,num4, pay_#,pay_$,pay_$1,pay_$2,pay_$3,pay_$4,pay_$5,pay_$6,pay_$7,pay_$8,[pay_&],pay_0,pay_1,pay_2,pay_3,pay_4,pay_5,pay_6,pay_7,pay_8,pay_9,pay_A,pay_B,pay_C,pay_D,pay_E,pay_WX,pay_ZF,pay_ZFB,pay_lv, price,quantity,summoney,traaccount,trasummoney,mark ) select gcl00c,'老客',gcl02d,substring(gcl56t,1,2)+':'+substring(gcl56t,3,2)+':'+substring(gcl56t,5,2),gcl03c,gca02c,gba03c, case when isnull(gba14i,0) = 0 then '女' else '男' end,gcl01c,'套餐','tc',ggb03c,gta02c,'',department1,department2,department3,department4, dmployeetype1,ggb12c_name,case when isnull(ggb12c_name,'') = '' then 0 else gcl16f end,case when isnull(ggb12c_name,'') = '' then 0 else 1 end, dmployeetype2,ggb13c_name,case when isnull(ggb13c_name,'') = '' then 0 else gcl18f end,case when isnull(ggb13c_name,'') = '' then 0 else 1 end, dmployeetype3,ggb14c_name,case when isnull(ggb14c_name,'') = '' then 0 else gcl20f end,case when isnull(ggb14c_name,'') = '' then 0 else 1 end, dmployeetype4,ggb26c_name,case when isnull(ggb26c_name,'') = '' then 0 else gcl23f end,case when isnull(ggb26c_name,'') = '' then 0 else 1 end, pay_#,pay_$,pay_$1,pay_$2,pay_$3,pay_$4,pay_$5,pay_$6,pay_$7,pay_$8,[pay_&],pay_0,pay_1,pay_2,pay_3,pay_4,pay_5,pay_6,pay_7,pay_8,pay_9,pay_A,pay_B,pay_C,pay_D,pay_E,pay_WX,pay_ZF,pay_ZFB, case when pay_# = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '#')+ ':'+ convert(varchar(10),pay_#)+'; ' end + case when pay_$ = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$')+ ':'+ convert(varchar(10),pay_$)+'; ' end + case when pay_$1 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$1')+ ':'+ convert(varchar(10),pay_$1)+'; ' end + case when pay_$2 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$2')+ ':'+ convert(varchar(10),pay_$2)+'; ' end + case when pay_$3 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$3')+ ':'+ convert(varchar(10),pay_$3)+'; ' end + case when pay_$4 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$4')+ ':'+ convert(varchar(10),pay_$4)+'; ' end + case when pay_$5 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$5')+ ':'+ convert(varchar(10),pay_$5)+'; ' end + case when pay_$6 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$6')+ ':'+ convert(varchar(10),pay_$6)+'; ' end + case when pay_$7 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$7')+ ':'+ convert(varchar(10),pay_$7)+'; ' end + case when pay_$8 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$8')+ ':'+ convert(varchar(10),pay_$8)+'; ' end + case when pay_0 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '0')+ ':'+ convert(varchar(10),pay_0)+'; ' end + case when pay_1 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '1')+ ':'+ convert(varchar(10),pay_1)+'; ' end + case when pay_2 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '2')+ ':'+ convert(varchar(10),pay_2)+'; ' end + case when pay_3 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '3')+ ':'+ convert(varchar(10),pay_3)+'; ' end + case when pay_4 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '4')+ ':'+ convert(varchar(10),pay_4)+'; ' end + case when pay_5 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '5')+ ':'+ convert(varchar(10),pay_5)+'; ' end + case when pay_6 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '6')+ ':'+ convert(varchar(10),pay_6)+'; ' end + case when pay_7 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '7')+ ':'+ convert(varchar(10),pay_7)+'; ' end + case when pay_8 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '8')+ ':'+ convert(varchar(10),pay_8)+'; ' end + case when pay_9 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '9')+ ':'+ convert(varchar(10),pay_9)+'; ' end + case when pay_A = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'A')+ ':'+ convert(varchar(10),pay_A)+'; ' end + case when pay_B = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'B')+ ':'+ convert(varchar(10),pay_B)+'; ' end + case when pay_C = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'C')+ ':'+ convert(varchar(10),pay_C)+'; ' end + case when pay_D = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'D')+ ':'+ convert(varchar(10),pay_D)+'; ' end + case when pay_E = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'E')+ ':'+ convert(varchar(10),pay_E)+'; ' end + case when pay_WX = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'WX')+ ':'+ convert(varchar(10),pay_WX) end + case when pay_ZF = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'ZF')+ ':'+ convert(varchar(10),pay_ZF) end + case when pay_ZFB = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'ZFB')+ ':'+ convert(varchar(10),pay_ZFB) end , a.gcl07f, case when pay_# = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '#')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_#/gcl07f))+'; ' end + case when pay_$ = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$/gcl07f))+'; ' end + case when pay_$1 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$1')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$1/gcl07f))+'; ' end + case when pay_$2 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$2')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$2/gcl07f))+'; ' end + case when pay_$3 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$3')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$3/gcl07f))+'; ' end + case when pay_$4 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$4')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$4/gcl07f))+'; ' end + case when pay_$5 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$5')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$5/gcl07f))+'; ' end + case when pay_$6 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$6')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$6/gcl07f))+'; ' end + case when pay_$7 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$7')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$7/gcl07f))+'; ' end + case when pay_$8 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$8')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$8/gcl07f))+'; ' end + case when pay_0 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '0')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_0/gcl07f))+'; ' end + case when pay_1 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '1')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_1/gcl07f))+'; ' end + case when pay_2 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '2')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_2/gcl07f))+'; ' end + case when pay_3 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '3')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_3/gcl07f))+'; ' end + case when pay_4 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '4')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_4/gcl07f))+'; ' end + case when pay_5 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '5')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_5/gcl07f))+'; ' end + case when pay_6 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '6')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_6/gcl07f))+'; ' end + case when pay_7 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '7')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_7/gcl07f))+'; ' end + case when pay_8 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '8')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_8/gcl07f))+'; ' end + case when pay_9 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '9')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_9/gcl07f))+'; ' end + case when pay_A = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'A')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_A/gcl07f))+'; ' end + case when pay_B = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'B')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_B/gcl07f))+'; ' end + case when pay_C = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'C')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_C/gcl07f))+'; ' end + case when pay_D = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'D')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_D/gcl07f))+'; ' end + case when pay_E = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'E')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_E/gcl07f))+'; ' end + case when pay_WX = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'WX')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_WX/gcl07f)) end + case when pay_ZF = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'ZF')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_ZF/gcl07f)) end + case when pay_ZFB = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'ZFB')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_ZFB/gcl07f)) end ,gcl07f,gsb03c,gcl07f,isnull(gcl57c,'') from gcm10 a with (nolock),#ggm02 b ,gcm01 c with(nolock),gbm01 d with(nolock),#gsm02 e,gtc01 g where gcl00c = b.ggb00c and gcl01c = b.ggb01c and a.gcl06i = 0 and b.[type] = 'gztc' and b.ggb00c = g.gta00c and b.ggb03c = g.gta01c and a.gcl03c = c.gca01c and c.gca13d = d.gba00c and c.gca04c = d.gba01c and a.gcl04c = e.gsb02c and e.gsb01c = 'O' and a.gcl00c = @compid and (isnull(@frombill,'*') = '*' or a.gcl01c between @frombill and @tobill) and (isnull(@fromcard,'*') = '*' or a.gcl03c between @fromcard and @tocard) and gcl02d between @fromdate and @todate -- 取款 insert into #result(compid,customer,checkdate,checktime,cardno,cardtypecode,m_name,gender,billid,billtype,type_gz_gn,projectcode,projectname,projectclass,department1,department2,department3, department4,types1,employee1,ratio1,num1,types2,employee2,ratio2,num2,types3,employee3,ratio3,num3,types4,employee4,ratio4,num4, pay_#,pay_$,pay_$1,pay_$2,pay_$3,pay_$4,pay_$5,pay_$6,pay_$7,pay_$8,[pay_&],pay_0,pay_1,pay_2,pay_3,pay_4,pay_5,pay_6,pay_7,pay_8,pay_9,pay_A,pay_B,pay_C,pay_D,pay_E,pay_WX,pay_ZF,pay_ZFB,pay_lv, price,quantity,summoney,traaccount,trasummoney,mark ) select gcl00c,'老客',gcl02d,substring(gcl56t,1,2)+':'+substring(gcl56t,3,2)+':'+substring(gcl56t,5,2),gcl03c,gca02c,gba03c, case when isnull(gba14i,0) = 0 then '女' else '男' end,gcl01c,'取款','qk','','','',department1,department2,department3,department4, dmployeetype1,ggb12c_name,case when isnull(ggb12c_name,'') = '' then 0 else gcl16f end,case when isnull(ggb12c_name,'') = '' then 0 else 1 end, dmployeetype2,ggb13c_name,case when isnull(ggb13c_name,'') = '' then 0 else gcl18f end,case when isnull(ggb13c_name,'') = '' then 0 else 1 end, dmployeetype3,ggb14c_name,case when isnull(ggb14c_name,'') = '' then 0 else gcl20f end,case when isnull(ggb14c_name,'') = '' then 0 else 1 end, dmployeetype4,ggb26c_name,case when isnull(ggb26c_name,'') = '' then 0 else gcl23f end,case when isnull(ggb26c_name,'') = '' then 0 else 1 end, pay_#,pay_$,pay_$1,pay_$2,pay_$3,pay_$4,pay_$5,pay_$6,pay_$7,pay_$8,[pay_&],pay_0,pay_1,pay_2,pay_3,pay_4,pay_5,pay_6,pay_7,pay_8,pay_9,pay_A,pay_B,pay_C,pay_D,pay_E,pay_WX,pay_ZF,pay_ZFB, case when pay_# = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '#')+ ':'+ convert(varchar(10),pay_#)+'; ' end + case when pay_$ = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$')+ ':'+ convert(varchar(10),pay_$)+'; ' end + case when pay_$1 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$1')+ ':'+ convert(varchar(10),pay_$1)+'; ' end + case when pay_$2 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$2')+ ':'+ convert(varchar(10),pay_$2)+'; ' end + case when pay_$3 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$3')+ ':'+ convert(varchar(10),pay_$3)+'; ' end + case when pay_$4 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$4')+ ':'+ convert(varchar(10),pay_$4)+'; ' end + case when pay_$5 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$5')+ ':'+ convert(varchar(10),pay_$5)+'; ' end + case when pay_$6 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$6')+ ':'+ convert(varchar(10),pay_$6)+'; ' end + case when pay_$7 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$7')+ ':'+ convert(varchar(10),pay_$7)+'; ' end + case when pay_$8 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$8')+ ':'+ convert(varchar(10),pay_$8)+'; ' end + case when pay_0 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '0')+ ':'+ convert(varchar(10),pay_0)+'; ' end + case when pay_1 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '1')+ ':'+ convert(varchar(10),pay_1)+'; ' end + case when pay_2 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '2')+ ':'+ convert(varchar(10),pay_2)+'; ' end + case when pay_3 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '3')+ ':'+ convert(varchar(10),pay_3)+'; ' end + case when pay_4 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '4')+ ':'+ convert(varchar(10),pay_4)+'; ' end + case when pay_5 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '5')+ ':'+ convert(varchar(10),pay_5)+'; ' end + case when pay_6 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '6')+ ':'+ convert(varchar(10),pay_6)+'; ' end + case when pay_7 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '7')+ ':'+ convert(varchar(10),pay_7)+'; ' end + case when pay_8 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '8')+ ':'+ convert(varchar(10),pay_8)+'; ' end + case when pay_9 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '9')+ ':'+ convert(varchar(10),pay_9)+'; ' end + case when pay_A = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'A')+ ':'+ convert(varchar(10),pay_A)+'; ' end + case when pay_B = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'B')+ ':'+ convert(varchar(10),pay_B)+'; ' end + case when pay_C = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'C')+ ':'+ convert(varchar(10),pay_C)+'; ' end + case when pay_D = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'D')+ ':'+ convert(varchar(10),pay_D)+'; ' end + case when pay_E = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'E')+ ':'+ convert(varchar(10),pay_E)+'; ' end + case when pay_WX = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'WX')+ ':'+ convert(varchar(10),pay_WX) end + case when pay_ZF = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'ZF')+ ':'+ convert(varchar(10),pay_ZF) end + case when pay_ZFB = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'ZFB')+ ':'+ convert(varchar(10),pay_ZFB) end , a.gcl07f, case when pay_# = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '#')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_#/gcl07f))+'; ' end + case when pay_$ = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$/gcl07f))+'; ' end + case when pay_$1 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$1')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$1/gcl07f))+'; ' end + case when pay_$2 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$2')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$2/gcl07f))+'; ' end + case when pay_$3 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$3')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$3/gcl07f))+'; ' end + case when pay_$4 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$4')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$4/gcl07f))+'; ' end + case when pay_$5 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$5')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$5/gcl07f))+'; ' end + case when pay_$6 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$6')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$6/gcl07f))+'; ' end + case when pay_$7 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$7')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$7/gcl07f))+'; ' end + case when pay_$8 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$8')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$8/gcl07f))+'; ' end + case when pay_0 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '0')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_0/gcl07f))+'; ' end + case when pay_1 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '1')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_1/gcl07f))+'; ' end + case when pay_2 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '2')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_2/gcl07f))+'; ' end + case when pay_3 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '3')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_3/gcl07f))+'; ' end + case when pay_4 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '4')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_4/gcl07f))+'; ' end + case when pay_5 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '5')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_5/gcl07f))+'; ' end + case when pay_6 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '6')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_6/gcl07f))+'; ' end + case when pay_7 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '7')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_7/gcl07f))+'; ' end + case when pay_8 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '8')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_8/gcl07f))+'; ' end + case when pay_9 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '9')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_9/gcl07f))+'; ' end + case when pay_A = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'A')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_A/gcl07f))+'; ' end + case when pay_B = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'B')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_B/gcl07f))+'; ' end + case when pay_C = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'C')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_C/gcl07f))+'; ' end + case when pay_D = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'D')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_D/gcl07f))+'; ' end + case when pay_E = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'E')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_E/gcl07f))+'; ' end + case when pay_WX = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'WX')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_WX/gcl07f)) end + case when pay_ZF = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'ZF')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_ZF/gcl07f)) end + case when pay_ZFB = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'ZFB')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_ZFB/gcl07f)) end ,gcl07f,gsb03c,gcl07f,isnull(gcl57c,'') from gcm10 a with (nolock),#ggm02 b ,gcm01 c with(nolock),gbm01 d with(nolock),#gsm02 e where gcl00c = b.ggb00c and gcl01c = b.ggb01c and a.gcl06i = 0 and b.[type] = 'qk' and a.gcl03c = c.gca01c and c.gca13d = d.gba00c and c.gca04c = d.gba01c and a.gcl04c = e.gsb02c and e.gsb01c = 'O' and a.gcl00c = @compid and (isnull(@frombill,'*') = '*' or a.gcl01c between @frombill and @tobill) and (isnull(@fromcard,'*') = '*' or a.gcl03c between @fromcard and @tocard) and gcl02d between @fromdate and @todate -- 还款 insert into #result(compid,customer,checkdate,checktime,cardno,cardtypecode,m_name,gender,billid,billtype,type_gz_gn,projectcode,projectname,projectclass,department1,department2,department3, department4,types1,employee1,ratio1,num1,types2,employee2,ratio2,num2,types3,employee3,ratio3,num3,types4,employee4,ratio4,num4, pay_#,pay_$,pay_$1,pay_$2,pay_$3,pay_$4,pay_$5,pay_$6,pay_$7,pay_$8,[pay_&],pay_0,pay_1,pay_2,pay_3,pay_4,pay_5,pay_6,pay_7,pay_8,pay_9,pay_A,pay_B,pay_C,pay_D,pay_E,pay_WX,pay_ZF,pay_ZFB,pay_lv, price,quantity,summoney,traaccount,trasummoney,mark ) select gcl00c,'老客',gcl02d,substring(gcl56t,1,2)+':'+substring(gcl56t,3,2)+':'+substring(gcl56t,5,2),gcl03c,gca02c,gba03c, case when isnull(gba14i,0) = 0 then '女' else '男' end,gcl01c,'还款','hk','','','',department1,department2,department3,department4, dmployeetype1,ggb12c_name,case when isnull(ggb12c_name,'') = '' then 0 else gcl16f end,case when isnull(ggb12c_name,'') = '' then 0 else 1 end, dmployeetype2,ggb13c_name,case when isnull(ggb13c_name,'') = '' then 0 else gcl18f end,case when isnull(ggb13c_name,'') = '' then 0 else 1 end, dmployeetype3,ggb14c_name,case when isnull(ggb14c_name,'') = '' then 0 else gcl20f end,case when isnull(ggb14c_name,'') = '' then 0 else 1 end, dmployeetype4,ggb26c_name,case when isnull(ggb26c_name,'') = '' then 0 else gcl23f end,case when isnull(ggb26c_name,'') = '' then 0 else 1 end, pay_#,pay_$,pay_$1,pay_$2,pay_$3,pay_$4,pay_$5,pay_$6,pay_$7,pay_$8,[pay_&],pay_0,pay_1,pay_2,pay_3,pay_4,pay_5,pay_6,pay_7,pay_8,pay_9,pay_A,pay_B,pay_C,pay_D,pay_E,pay_WX,pay_ZF,pay_ZFB, case when pay_# = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '#')+ ':'+ convert(varchar(10),pay_#)+'; ' end + case when pay_$ = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$')+ ':'+ convert(varchar(10),pay_$)+'; ' end + case when pay_$1 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$1')+ ':'+ convert(varchar(10),pay_$1)+'; ' end + case when pay_$2 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$2')+ ':'+ convert(varchar(10),pay_$2)+'; ' end + case when pay_$3 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$3')+ ':'+ convert(varchar(10),pay_$3)+'; ' end + case when pay_$4 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$4')+ ':'+ convert(varchar(10),pay_$4)+'; ' end + case when pay_$5 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$5')+ ':'+ convert(varchar(10),pay_$5)+'; ' end + case when pay_$6 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$6')+ ':'+ convert(varchar(10),pay_$6)+'; ' end + case when pay_$7 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$7')+ ':'+ convert(varchar(10),pay_$7)+'; ' end + case when pay_$8 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$8')+ ':'+ convert(varchar(10),pay_$8)+'; ' end + case when pay_0 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '0')+ ':'+ convert(varchar(10),pay_0)+'; ' end + case when pay_1 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '1')+ ':'+ convert(varchar(10),pay_1)+'; ' end + case when pay_2 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '2')+ ':'+ convert(varchar(10),pay_2)+'; ' end + case when pay_3 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '3')+ ':'+ convert(varchar(10),pay_3)+'; ' end + case when pay_4 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '4')+ ':'+ convert(varchar(10),pay_4)+'; ' end + case when pay_5 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '5')+ ':'+ convert(varchar(10),pay_5)+'; ' end + case when pay_6 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '6')+ ':'+ convert(varchar(10),pay_6)+'; ' end + case when pay_7 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '7')+ ':'+ convert(varchar(10),pay_7)+'; ' end + case when pay_8 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '8')+ ':'+ convert(varchar(10),pay_8)+'; ' end + case when pay_9 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '9')+ ':'+ convert(varchar(10),pay_9)+'; ' end + case when pay_A = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'A')+ ':'+ convert(varchar(10),pay_A)+'; ' end + case when pay_B = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'B')+ ':'+ convert(varchar(10),pay_B)+'; ' end + case when pay_C = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'C')+ ':'+ convert(varchar(10),pay_C)+'; ' end + case when pay_D = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'D')+ ':'+ convert(varchar(10),pay_D)+'; ' end + case when pay_E = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'E')+ ':'+ convert(varchar(10),pay_E)+'; ' end + case when pay_WX = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'WX')+ ':'+ convert(varchar(10),pay_WX) end + case when pay_ZF = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'ZF')+ ':'+ convert(varchar(10),pay_ZF) end + case when pay_ZFB = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'ZFB')+ ':'+ convert(varchar(10),pay_ZFB) end , a.gcl07f, case when pay_# = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '#')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_#/gcl07f))+'; ' end + case when pay_$ = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$/gcl07f))+'; ' end + case when pay_$1 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$1')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$1/gcl07f))+'; ' end + case when pay_$2 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$2')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$2/gcl07f))+'; ' end + case when pay_$3 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$3')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$3/gcl07f))+'; ' end + case when pay_$4 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$4')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$4/gcl07f))+'; ' end + case when pay_$5 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$5')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$5/gcl07f))+'; ' end + case when pay_$6 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$6')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$6/gcl07f))+'; ' end + case when pay_$7 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$7')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$7/gcl07f))+'; ' end + case when pay_$8 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$8')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$8/gcl07f))+'; ' end + case when pay_0 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '0')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_0/gcl07f))+'; ' end + case when pay_1 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '1')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_1/gcl07f))+'; ' end + case when pay_2 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '2')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_2/gcl07f))+'; ' end + case when pay_3 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '3')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_3/gcl07f))+'; ' end + case when pay_4 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '4')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_4/gcl07f))+'; ' end + case when pay_5 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '5')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_5/gcl07f))+'; ' end + case when pay_6 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '6')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_6/gcl07f))+'; ' end + case when pay_7 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '7')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_7/gcl07f))+'; ' end + case when pay_8 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '8')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_8/gcl07f))+'; ' end + case when pay_9 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '9')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_9/gcl07f))+'; ' end + case when pay_A = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'A')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_A/gcl07f))+'; ' end + case when pay_B = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'B')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_B/gcl07f))+'; ' end + case when pay_C = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'C')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_C/gcl07f))+'; ' end + case when pay_D = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'D')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_D/gcl07f))+'; ' end + case when pay_E = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'E')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_E/gcl07f))+'; ' end + case when pay_WX = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'WX')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_WX/gcl07f)) end + case when pay_ZF = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'ZF')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_ZF/gcl07f)) end + case when pay_ZFB = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'ZFB')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_ZFB/gcl07f)) end ,gcl07f,gsb03c,gcl07f,isnull(gcl57c,'') from gcm10 a with (nolock),#ggm02 b ,gcm01 c with(nolock),gbm01 d with(nolock),#gsm02 e where gcl00c = b.ggb00c and gcl01c = b.ggb01c and a.gcl06i = 6 and b.[type] = 'hk' and a.gcl03c = c.gca01c and c.gca13d = d.gba00c and c.gca04c = d.gba01c and a.gcl04c = e.gsb02c and e.gsb01c = 'O' and a.gcl00c = @compid and (isnull(@frombill,'*') = '*' or a.gcl01c between @frombill and @tobill) and (isnull(@fromcard,'*') = '*' or a.gcl03c between @fromcard and @tocard) and gcl02d between @fromdate and @todate -- 项目还款 insert into #result(compid,customer,checkdate,checktime,cardno,cardtypecode,m_name,gender,billid,billtype,type_gz_gn,projectcode,projectname,projectclass,department1,department2,department3, department4,types1,employee1,ratio1,num1,types2,employee2,ratio2,num2,types3,employee3,ratio3,num3,types4,employee4,ratio4,num4, pay_#,pay_$,pay_$1,pay_$2,pay_$3,pay_$4,pay_$5,pay_$6,pay_$7,pay_$8,[pay_&],pay_0,pay_1,pay_2,pay_3,pay_4,pay_5,pay_6,pay_7,pay_8,pay_9,pay_A,pay_B,pay_C,pay_D,pay_E,pay_WX,pay_ZF,pay_ZFB,pay_lv, price,quantity,summoney,traaccount,trasummoney,mark ) select gcl00c,'老客',gcl02d,substring(gcl56t,1,2)+':'+substring(gcl56t,3,2)+':'+substring(gcl56t,5,2),gcl03c,gca02c,gba03c, case when isnull(gba14i,0) = 0 then '女' else '男' end,gcl01c,'项目还款','xmhk','','','',department1,department2,department3,department4, dmployeetype1,ggb12c_name,case when isnull(ggb12c_name,'') = '' then 0 else gcl16f end,case when isnull(ggb12c_name,'') = '' then 0 else 1 end, dmployeetype2,ggb13c_name,case when isnull(ggb13c_name,'') = '' then 0 else gcl18f end,case when isnull(ggb13c_name,'') = '' then 0 else 1 end, dmployeetype3,ggb14c_name,case when isnull(ggb14c_name,'') = '' then 0 else gcl20f end,case when isnull(ggb14c_name,'') = '' then 0 else 1 end, dmployeetype4,ggb26c_name,case when isnull(ggb26c_name,'') = '' then 0 else gcl23f end,case when isnull(ggb26c_name,'') = '' then 0 else 1 end, pay_#,pay_$,pay_$1,pay_$2,pay_$3,pay_$4,pay_$5,pay_$6,pay_$7,pay_$8,[pay_&],pay_0,pay_1,pay_2,pay_3,pay_4,pay_5,pay_6,pay_7,pay_8,pay_9,pay_A,pay_B,pay_C,pay_D,pay_E,pay_WX,pay_ZF,pay_ZFB, case when pay_# = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '#')+ ':'+ convert(varchar(10),pay_#)+'; ' end + case when pay_$ = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$')+ ':'+ convert(varchar(10),pay_$)+'; ' end + case when pay_$1 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$1')+ ':'+ convert(varchar(10),pay_$1)+'; ' end + case when pay_$2 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$2')+ ':'+ convert(varchar(10),pay_$2)+'; ' end + case when pay_$3 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$3')+ ':'+ convert(varchar(10),pay_$3)+'; ' end + case when pay_$4 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$4')+ ':'+ convert(varchar(10),pay_$4)+'; ' end + case when pay_$5 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$5')+ ':'+ convert(varchar(10),pay_$5)+'; ' end + case when pay_$6 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$6')+ ':'+ convert(varchar(10),pay_$6)+'; ' end + case when pay_$7 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$7')+ ':'+ convert(varchar(10),pay_$7)+'; ' end + case when pay_$8 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$8')+ ':'+ convert(varchar(10),pay_$8)+'; ' end + case when pay_0 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '0')+ ':'+ convert(varchar(10),pay_0)+'; ' end + case when pay_1 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '1')+ ':'+ convert(varchar(10),pay_1)+'; ' end + case when pay_2 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '2')+ ':'+ convert(varchar(10),pay_2)+'; ' end + case when pay_3 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '3')+ ':'+ convert(varchar(10),pay_3)+'; ' end + case when pay_4 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '4')+ ':'+ convert(varchar(10),pay_4)+'; ' end + case when pay_5 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '5')+ ':'+ convert(varchar(10),pay_5)+'; ' end + case when pay_6 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '6')+ ':'+ convert(varchar(10),pay_6)+'; ' end + case when pay_7 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '7')+ ':'+ convert(varchar(10),pay_7)+'; ' end + case when pay_8 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '8')+ ':'+ convert(varchar(10),pay_8)+'; ' end + case when pay_9 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '9')+ ':'+ convert(varchar(10),pay_9)+'; ' end + case when pay_A = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'A')+ ':'+ convert(varchar(10),pay_A)+'; ' end + case when pay_B = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'B')+ ':'+ convert(varchar(10),pay_B)+'; ' end + case when pay_C = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'C')+ ':'+ convert(varchar(10),pay_C)+'; ' end + case when pay_D = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'D')+ ':'+ convert(varchar(10),pay_D)+'; ' end + case when pay_E = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'E')+ ':'+ convert(varchar(10),pay_E)+'; ' end + case when pay_WX = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'WX')+ ':'+ convert(varchar(10),pay_WX) end + case when pay_ZF = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'ZF')+ ':'+ convert(varchar(10),pay_ZF) end + case when pay_ZFB = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'ZFB')+ ':'+ convert(varchar(10),pay_ZFB) end , a.gcl07f, case when pay_# = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '#')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_#/gcl07f))+'; ' end + case when pay_$ = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$/gcl07f))+'; ' end + case when pay_$1 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$1')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$1/gcl07f))+'; ' end + case when pay_$2 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$2')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$2/gcl07f))+'; ' end + case when pay_$3 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$3')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$3/gcl07f))+'; ' end + case when pay_$4 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$4')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$4/gcl07f))+'; ' end + case when pay_$5 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$5')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$5/gcl07f))+'; ' end + case when pay_$6 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$6')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$6/gcl07f))+'; ' end + case when pay_$7 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$7')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$7/gcl07f))+'; ' end + case when pay_$8 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$8')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$8/gcl07f))+'; ' end + case when pay_0 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '0')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_0/gcl07f))+'; ' end + case when pay_1 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '1')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_1/gcl07f))+'; ' end + case when pay_2 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '2')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_2/gcl07f))+'; ' end + case when pay_3 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '3')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_3/gcl07f))+'; ' end + case when pay_4 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '4')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_4/gcl07f))+'; ' end + case when pay_5 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '5')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_5/gcl07f))+'; ' end + case when pay_6 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '6')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_6/gcl07f))+'; ' end + case when pay_7 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '7')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_7/gcl07f))+'; ' end + case when pay_8 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '8')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_8/gcl07f))+'; ' end + case when pay_9 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '9')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_9/gcl07f))+'; ' end + case when pay_A = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'A')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_A/gcl07f))+'; ' end + case when pay_B = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'B')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_B/gcl07f))+'; ' end + case when pay_C = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'C')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_C/gcl07f))+'; ' end + case when pay_D = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'D')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_D/gcl07f))+'; ' end + case when pay_E = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'E')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_E/gcl07f))+'; ' end + case when pay_WX = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'WX')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_WX/gcl07f)) end + case when pay_ZF = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'ZF')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_ZF/gcl07f)) end + case when pay_ZFB = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'ZFB')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_ZFB/gcl07f)) end ,gcl07f,gsb03c,gcl07f,isnull(gcl57c,'') from gcm10 a with (nolock),#ggm02 b ,gcm01 c with(nolock),gbm01 d with(nolock),#gsm02 e where gcl00c = b.ggb00c and gcl01c = b.ggb01c and a.gcl06i = 6 and b.[type] = 'xmhk' and a.gcl03c = c.gca01c and c.gca13d = d.gba00c and c.gca04c = d.gba01c and a.gcl04c = e.gsb02c and e.gsb01c = 'O' and a.gcl00c = @compid and (isnull(@frombill,'*') = '*' or a.gcl01c between @frombill and @tobill) and (isnull(@fromcard,'*') = '*' or a.gcl03c between @fromcard and @tocard) and gcl02d between @fromdate and @todate -- 项目还款 insert into #result(compid,customer,checkdate,checktime,cardno,cardtypecode,m_name,gender,billid,billtype,type_gz_gn,projectcode,projectname,projectclass,department1,department2,department3, department4,types1,employee1,ratio1,num1,types2,employee2,ratio2,num2,types3,employee3,ratio3,num3,types4,employee4,ratio4,num4, pay_#,pay_$,pay_$1,pay_$2,pay_$3,pay_$4,pay_$5,pay_$6,pay_$7,pay_$8,[pay_&],pay_0,pay_1,pay_2,pay_3,pay_4,pay_5,pay_6,pay_7,pay_8,pay_9,pay_A,pay_B,pay_C,pay_D,pay_E,pay_WX,pay_ZF,pay_ZFB,pay_lv, price,quantity,summoney,traaccount,trasummoney,mark ) select gcl00c,'老客',gcl02d,substring(gcl56t,1,2)+':'+substring(gcl56t,3,2)+':'+substring(gcl56t,5,2),gcl03c,gca02c,gba03c, case when isnull(gba14i,0) = 0 then '女' else '男' end,gcl01c,'产品还款','cphk','','','',department1,department2,department3,department4, dmployeetype1,ggb12c_name,case when isnull(ggb12c_name,'') = '' then 0 else gcl16f end,case when isnull(ggb12c_name,'') = '' then 0 else 1 end, dmployeetype2,ggb13c_name,case when isnull(ggb13c_name,'') = '' then 0 else gcl18f end,case when isnull(ggb13c_name,'') = '' then 0 else 1 end, dmployeetype3,ggb14c_name,case when isnull(ggb14c_name,'') = '' then 0 else gcl20f end,case when isnull(ggb14c_name,'') = '' then 0 else 1 end, dmployeetype4,ggb26c_name,case when isnull(ggb26c_name,'') = '' then 0 else gcl23f end,case when isnull(ggb26c_name,'') = '' then 0 else 1 end, pay_#,pay_$,pay_$1,pay_$2,pay_$3,pay_$4,pay_$5,pay_$6,pay_$7,pay_$8,[pay_&],pay_0,pay_1,pay_2,pay_3,pay_4,pay_5,pay_6,pay_7,pay_8,pay_9,pay_A,pay_B,pay_C,pay_D,pay_E,pay_WX,pay_ZF,pay_ZFB, case when pay_# = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '#')+ ':'+ convert(varchar(10),pay_#)+'; ' end + case when pay_$ = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$')+ ':'+ convert(varchar(10),pay_$)+'; ' end + case when pay_$1 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$1')+ ':'+ convert(varchar(10),pay_$1)+'; ' end + case when pay_$2 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$2')+ ':'+ convert(varchar(10),pay_$2)+'; ' end + case when pay_$3 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$3')+ ':'+ convert(varchar(10),pay_$3)+'; ' end + case when pay_$4 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$4')+ ':'+ convert(varchar(10),pay_$4)+'; ' end + case when pay_$5 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$5')+ ':'+ convert(varchar(10),pay_$5)+'; ' end + case when pay_$6 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$6')+ ':'+ convert(varchar(10),pay_$6)+'; ' end + case when pay_$7 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$7')+ ':'+ convert(varchar(10),pay_$7)+'; ' end + case when pay_$8 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$8')+ ':'+ convert(varchar(10),pay_$8)+'; ' end + case when pay_0 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '0')+ ':'+ convert(varchar(10),pay_0)+'; ' end + case when pay_1 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '1')+ ':'+ convert(varchar(10),pay_1)+'; ' end + case when pay_2 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '2')+ ':'+ convert(varchar(10),pay_2)+'; ' end + case when pay_3 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '3')+ ':'+ convert(varchar(10),pay_3)+'; ' end + case when pay_4 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '4')+ ':'+ convert(varchar(10),pay_4)+'; ' end + case when pay_5 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '5')+ ':'+ convert(varchar(10),pay_5)+'; ' end + case when pay_6 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '6')+ ':'+ convert(varchar(10),pay_6)+'; ' end + case when pay_7 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '7')+ ':'+ convert(varchar(10),pay_7)+'; ' end + case when pay_8 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '8')+ ':'+ convert(varchar(10),pay_8)+'; ' end + case when pay_9 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '9')+ ':'+ convert(varchar(10),pay_9)+'; ' end + case when pay_A = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'A')+ ':'+ convert(varchar(10),pay_A)+'; ' end + case when pay_B = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'B')+ ':'+ convert(varchar(10),pay_B)+'; ' end + case when pay_C = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'C')+ ':'+ convert(varchar(10),pay_C)+'; ' end + case when pay_D = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'D')+ ':'+ convert(varchar(10),pay_D)+'; ' end + case when pay_E = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'E')+ ':'+ convert(varchar(10),pay_E)+'; ' end + case when pay_WX = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'WX')+ ':'+ convert(varchar(10),pay_WX) end + case when pay_ZF = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'ZF')+ ':'+ convert(varchar(10),pay_ZF) end + case when pay_ZFB = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'ZFB')+ ':'+ convert(varchar(10),pay_ZFB) end , a.gcl07f, case when pay_# = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '#')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_#/gcl07f))+'; ' end + case when pay_$ = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$/gcl07f))+'; ' end + case when pay_$1 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$1')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$1/gcl07f))+'; ' end + case when pay_$2 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$2')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$2/gcl07f))+'; ' end + case when pay_$3 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$3')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$3/gcl07f))+'; ' end + case when pay_$4 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$4')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$4/gcl07f))+'; ' end + case when pay_$5 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$5')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$5/gcl07f))+'; ' end + case when pay_$6 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$6')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$6/gcl07f))+'; ' end + case when pay_$7 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$7')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$7/gcl07f))+'; ' end + case when pay_$8 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$8')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$8/gcl07f))+'; ' end + case when pay_0 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '0')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_0/gcl07f))+'; ' end + case when pay_1 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '1')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_1/gcl07f))+'; ' end + case when pay_2 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '2')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_2/gcl07f))+'; ' end + case when pay_3 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '3')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_3/gcl07f))+'; ' end + case when pay_4 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '4')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_4/gcl07f))+'; ' end + case when pay_5 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '5')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_5/gcl07f))+'; ' end + case when pay_6 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '6')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_6/gcl07f))+'; ' end + case when pay_7 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '7')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_7/gcl07f))+'; ' end + case when pay_8 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '8')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_8/gcl07f))+'; ' end + case when pay_9 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '9')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_9/gcl07f))+'; ' end + case when pay_A = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'A')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_A/gcl07f))+'; ' end + case when pay_B = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'B')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_B/gcl07f))+'; ' end + case when pay_C = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'C')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_C/gcl07f))+'; ' end + case when pay_D = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'D')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_D/gcl07f))+'; ' end + case when pay_E = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'E')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_E/gcl07f))+'; ' end + case when pay_WX = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'WX')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_WX/gcl07f)) end + case when pay_ZF = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'ZF')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_ZF/gcl07f)) end + case when pay_ZFB = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'ZFB')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_ZFB/gcl07f)) end ,gcl07f,gsb03c,gcl07f,isnull(gcl57c,'') from gcm10 a with (nolock),#ggm02 b ,gcm01 c with(nolock),gbm01 d with(nolock),#gsm02 e where gcl00c = b.ggb00c and gcl01c = b.ggb01c and a.gcl06i = 6 and b.[type] = 'cphk' and a.gcl03c = c.gca01c and c.gca13d = d.gba00c and c.gca04c = d.gba01c and a.gcl04c = e.gsb02c and e.gsb01c = 'O' and a.gcl00c = @compid and (isnull(@frombill,'*') = '*' or a.gcl01c between @frombill and @tobill) and (isnull(@fromcard,'*') = '*' or a.gcl03c between @fromcard and @tocard) and gcl02d between @fromdate and @todate -- 疗程 insert into #result(compid,customer,checkdate,checktime,cardno,cardtypecode,m_name,gender,billid,billtype,type_gz_gn,projectcode,projectname,projectclass,department1,department2,department3, department4,types1,employee1,ratio1,num1,types2,employee2,ratio2,num2,types3,employee3,ratio3,num3,types4,employee4,ratio4,num4, pay_#,pay_$,pay_$1,pay_$2,pay_$3,pay_$4,pay_$5,pay_$6,pay_$7,pay_$8,[pay_&],pay_0,pay_1,pay_2,pay_3,pay_4,pay_5,pay_6,pay_7,pay_8,pay_9,pay_A,pay_B,pay_C,pay_D,pay_E,pay_WX,pay_ZF,pay_ZFB,pay_lv, price,quantity,summoney,traaccount,trasummoney,mark ) select gcl00c,'老客',gcl02d,substring(gcl56t,1,2)+':'+substring(gcl56t,3,2)+':'+substring(gcl56t,5,2),gcl03c,gca02c,gba03c, case when isnull(gba14i,0) = 0 then '女' else '男' end,gcl01c,'疗程','lc',ggb03c,gda03c,'',department1,department2,department3,department4, dmployeetype1,ggb12c_name,case when isnull(ggb12c_name,'') = '' then 0 else gcl16f end,case when isnull(ggb12c_name,'') = '' then 0 else 1 end, dmployeetype2,ggb13c_name,case when isnull(ggb13c_name,'') = '' then 0 else gcl18f end,case when isnull(ggb13c_name,'') = '' then 0 else 1 end, dmployeetype3,ggb14c_name,case when isnull(ggb14c_name,'') = '' then 0 else gcl20f end,case when isnull(ggb14c_name,'') = '' then 0 else 1 end, dmployeetype4,ggb26c_name,case when isnull(ggb26c_name,'') = '' then 0 else gcl23f end,case when isnull(ggb26c_name,'') = '' then 0 else 1 end, pay_#,pay_$,pay_$1,pay_$2,pay_$3,pay_$4,pay_$5,pay_$6,pay_$7,pay_$8,[pay_&],pay_0,pay_1,pay_2,pay_3,pay_4,pay_5,pay_6,pay_7,pay_8,pay_9,pay_A,pay_B,pay_C,pay_D,pay_E,pay_WX,pay_ZF,pay_ZFB, case when pay_# = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '#')+ ':'+ convert(varchar(10),pay_#)+'; ' end + case when pay_$ = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$')+ ':'+ convert(varchar(10),pay_$)+'; ' end + case when pay_$1 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$1')+ ':'+ convert(varchar(10),pay_$1)+'; ' end + case when pay_$2 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$2')+ ':'+ convert(varchar(10),pay_$2)+'; ' end + case when pay_$3 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$3')+ ':'+ convert(varchar(10),pay_$3)+'; ' end + case when pay_$4 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$4')+ ':'+ convert(varchar(10),pay_$4)+'; ' end + case when pay_$5 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$5')+ ':'+ convert(varchar(10),pay_$5)+'; ' end + case when pay_$6 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$6')+ ':'+ convert(varchar(10),pay_$6)+'; ' end + case when pay_$7 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$7')+ ':'+ convert(varchar(10),pay_$7)+'; ' end + case when pay_$8 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$8')+ ':'+ convert(varchar(10),pay_$8)+'; ' end + case when pay_0 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '0')+ ':'+ convert(varchar(10),pay_0)+'; ' end + case when pay_1 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '1')+ ':'+ convert(varchar(10),pay_1)+'; ' end + case when pay_2 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '2')+ ':'+ convert(varchar(10),pay_2)+'; ' end + case when pay_3 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '3')+ ':'+ convert(varchar(10),pay_3)+'; ' end + case when pay_4 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '4')+ ':'+ convert(varchar(10),pay_4)+'; ' end + case when pay_5 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '5')+ ':'+ convert(varchar(10),pay_5)+'; ' end + case when pay_6 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '6')+ ':'+ convert(varchar(10),pay_6)+'; ' end + case when pay_7 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '7')+ ':'+ convert(varchar(10),pay_7)+'; ' end + case when pay_8 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '8')+ ':'+ convert(varchar(10),pay_8)+'; ' end + case when pay_9 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '9')+ ':'+ convert(varchar(10),pay_9)+'; ' end + case when pay_A = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'A')+ ':'+ convert(varchar(10),pay_A)+'; ' end + case when pay_B = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'B')+ ':'+ convert(varchar(10),pay_B)+'; ' end + case when pay_C = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'C')+ ':'+ convert(varchar(10),pay_C)+'; ' end + case when pay_D = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'D')+ ':'+ convert(varchar(10),pay_D)+'; ' end + case when pay_E = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'E')+ ':'+ convert(varchar(10),pay_E)+'; ' end + case when pay_WX = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'WX')+ ':'+ convert(varchar(10),pay_WX) end + case when pay_ZF = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'ZF')+ ':'+ convert(varchar(10),pay_ZF) end + case when pay_ZFB = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'ZFB')+ ':'+ convert(varchar(10),pay_ZFB) end , a.gcl07f, case when pay_# = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '#')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_#/gcl07f))+'; ' end + case when pay_$ = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$/gcl07f))+'; ' end + case when pay_$1 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$1')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$1/gcl07f))+'; ' end + case when pay_$2 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$2')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$2/gcl07f))+'; ' end + case when pay_$3 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$3')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$3/gcl07f))+'; ' end + case when pay_$4 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$4')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$4/gcl07f))+'; ' end + case when pay_$5 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$5')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$5/gcl07f))+'; ' end + case when pay_$6 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$6')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$6/gcl07f))+'; ' end + case when pay_$7 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$7')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$7/gcl07f))+'; ' end + case when pay_$8 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '$8')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_$8/gcl07f))+'; ' end + case when pay_0 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '0')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_0/gcl07f))+'; ' end + case when pay_1 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '1')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_1/gcl07f))+'; ' end + case when pay_2 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '2')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_2/gcl07f))+'; ' end + case when pay_3 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '3')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_3/gcl07f))+'; ' end + case when pay_4 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '4')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_4/gcl07f))+'; ' end + case when pay_5 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '5')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_5/gcl07f))+'; ' end + case when pay_6 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '6')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_6/gcl07f))+'; ' end + case when pay_7 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '7')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_7/gcl07f))+'; ' end + case when pay_8 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '8')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_8/gcl07f))+'; ' end + case when pay_9 = 0 then '' else (select gsn02c from #gsm14 where gsn01c = '9')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_9/gcl07f))+'; ' end + case when pay_A = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'A')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_A/gcl07f))+'; ' end + case when pay_B = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'B')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_B/gcl07f))+'; ' end + case when pay_C = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'C')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_C/gcl07f))+'; ' end + case when pay_D = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'D')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_D/gcl07f))+'; ' end + case when pay_E = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'E')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_E/gcl07f))+'; ' end + case when pay_WX = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'WX')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_WX/gcl07f)) end + case when pay_ZF = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'ZF')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_ZF/gcl07f)) end + case when pay_ZFB = 0 then '' else (select gsn02c from #gsm14 where gsn01c = 'ZFB')+ ':'+ convert(varchar(10),convert(decimal(10,2),pay_ZFB/gcl07f)) end ,gcl07f,gsb03c,gcl07f,isnull(gcl57c,'') from gcm10 a with (nolock),#ggm02 b ,gcm01 c with(nolock),gbm01 d with(nolock),#gsm02 e,gdm01 g with(nolock) where gcl00c = b.ggb00c and gcl01c = b.ggb01c and a.gcl06i = 0 and b.[type] = 'lc' and b.ggb00c = g.gda00c and b.ggb03c = g.gda01c and a.gcl03c = c.gca01c and c.gca13d = d.gba00c and c.gca04c = d.gba01c and a.gcl04c = e.gsb02c and e.gsb01c = 'O' and a.gcl00c = @compid and (isnull(@frombill,'*') = '*' or a.gcl01c between @frombill and @tobill) and (isnull(@fromcard,'*') = '*' or a.gcl03c between @fromcard and @tocard) and gcl02d between @fromdate and @todate select row_number() over (order by type_gz_gn,checkdate) id , compid , -- 门店 customer, -- 新老客 checkdate , -- 消费日期 checktime , -- 消费时间 cardno , -- 会员卡编号 cardtypecode , -- 卡类别代码 m_name , -- 会员名称 gender , -- 性别 billid , -- 单号 billtype , -- 单据别 projectcode , -- 项目编号 projectname , -- 项目名称 projectclass , -- 项目类别 isnull(department1,'') department1 , -- 部门1 isnull(department2,'') department2 , -- 部门2 isnull(department3,'') department3 , -- 部门3 isnull(department4,'') department4 , -- 部门4 isnull(types1,'') types1 , -- 大工类型点/轮 isnull(employee1,'')employee1 , -- 大工姓名 isnull(ratio1,0) ratio1 , -- 大工 分享比率 项目给死 1 isnull(num1,0)num1 , -- 数量 isnull(types2,'') types2 , -- 中工类型点/轮 isnull(employee2,'')employee2 , -- 中工姓名 isnull(ratio2,0) ratio2 , -- 中工 分享比率 项目给死 1 isnull(num2,0) num2 , -- 数量 isnull(types3,'') types3 , -- 小工类型点/轮 isnull(employee3,'') employee3 , -- 小工姓名 isnull( ratio3,0)ratio3 , -- 小工 分享比率 项目给死 1 isnull(num3,0) num3 , -- 数量 isnull(types4,'') types4 , -- 四工类型点/轮 isnull(employee4,'')employee4 , -- 四工姓名 isnull(ratio4,0) ratio4 , -- 四工 分享比率 项目给死 1 isnull(num4,0) num4 , -- 数量 isnull(pay_#,0)pay_# , -- 支付 # isnull(pay_$,0)pay_$ , -- 支付 $ isnull(pay_$1,0)pay_$1 , -- 支付 $1 isnull(pay_$2,0)pay_$2 , -- 支付 $2 isnull(pay_$3,0)pay_$3 , -- 支付 $3 isnull(pay_$4,0)pay_$4 , -- 支付 $4 isnull(pay_$5,0)pay_$5 , -- 支付 $5 isnull(pay_$6,0)pay_$6 , -- 支付 $6 isnull(pay_$7,0)pay_$7 , -- 支付 $7 isnull(pay_$8,0)pay_$8 , -- 支付 $8 isnull([pay_&],0)[pay_&] , -- 支付 & isnull(pay_0,0)pay_0 , -- 支付 0 isnull(pay_1,0)pay_1 , -- 支付 1 isnull(pay_2,0)pay_2 , -- 支付 2 isnull(pay_3,0)pay_3 , -- 支付 3 isnull(pay_4,0)pay_4 , -- 支付 4 isnull(pay_5,0)pay_5 , -- 支付 5 isnull(pay_6,0)pay_6 , -- 支付 6 isnull(pay_7,0)pay_7 , -- 支付 7 isnull(pay_8,0)pay_8 , -- 支付 8 isnull(pay_9,0)pay_9 , -- 支付 9 isnull(pay_A,0)pay_A , -- 支付 A isnull(pay_B,0)pay_B , -- 支付 B isnull(pay_C,0)pay_C , -- 支付 C isnull(pay_D,0)pay_D , -- 支付 D isnull(pay_E,0)pay_E , -- 支付 E isnull(pay_WX,0)pay_WX , -- 支付 WX isnull(pay_ZF,0)pay_ZF , -- 支付 ZF isnull(pay_ZFB,0)pay_ZFB , -- 支付 ZFB isnull(pay_lv,0)pay_lv , -- 支付方式明细 isnull(price,0) price , -- 单价 isnull(quantity,0)quantity , -- 数量占比 isnull(summoney,0)summoney , -- 总金额 isnull(traaccount,'')traaccount , -- 异动账户 isnull(trasummoney,0)trasummoney , -- 异动金额 isnull(mark,'')mark -- 备注 from #result drop table #result drop table #gsm02 drop table #gsm14 drop table #ggm02 drop table #ham01 drop table #temp drop table #pays drop table #pays_result end try begin catch drop table #result drop table #gsm02 drop table #gsm14 drop table #ggm02 drop table #ham01 drop table #temp drop table #pays drop table #pays_result end catch end GO /****** Object: Table [dbo].[kam01] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[kam01]( [kaa00c] [varchar](10) NOT NULL, [kaa01c] [varchar](5) NOT NULL, [kaa02c] [varchar](40) NULL, [kaa03f] [float] NULL, [kaa04f] [float] NULL, [kaa05f] [float] NULL, [kaa06c] [varchar](5) NULL, CONSTRAINT [PK_kam01] PRIMARY KEY CLUSTERED ( [kaa00c] ASC, [kaa01c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_analyzer_customer_order_loyal] Script Date: 06/12/2017 06:19:27 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_analyzer_customer_order_loyal] ( @compid varchar(10) ) as begin create table #mem_table ( compid varchar(10) not null,--门店号 cardid varchar(20) not null,--卡号 memid varchar(20) not null,--会员编号 memname varchar(300) null,--会员名称 mobile varchar(20) null,--手机号 balanceamt float null,--储值余额 treatmentcount int null,--疗程次数 rechargeamt float null,--累计充值金额 consumeamt float null,--消费金额 incompdate varchar(8) null,--首次来店时间 consumedate varchar(8) null,--最后消费时间 empid varchar(10) null,--负责员工 empname varchar(20) null,--员工名称 level int null,--0:近一个月内 1:近一到3个月 2:近3个月到半年 3:近半年到一年 4:近一年以上 5:无效会员 isinvalid int null,--0:无效 1:有效 ) insert into #mem_table(compid,cardid,memid,memname,mobile,incompdate,empid,isinvalid) select gca00c,gca01c,gca04c,gba03c,gba08c,gca05d,gba10c,gba99i from gcm01 a with(nolock), gbm01 b with(nolock) where a.gca00c = b.gba00c and a.gca04c = b.gba01c and a.gca00c = @compid and a.gca04c <> '散客' update a set a.empname = b.haa02c from #mem_table a, ham01 b where a.compid = b.haa00c and a.empid = b.haa01c --需要清除被删除的员工数据 update a set a.empid = NULL, a.empname = NULL from #mem_table a where not exists(select 1 from ham01 where haa00c = a.compid and haa01c = a.empid) create table #gcm20 ( gct00c varchar(10) not null, --门店编号 gct01i int not null, --序号 gct02c varchar(20) not null, --卡号 gct03c varchar(20) not null, --会员编号 gct04d varchar(8 ) not null, --日期 gct05c varchar(5) null, --异动标志,账户的异动方式 gct06c varchar(10) null, --代码的类别--1-卡销售 2-疗程销售 3-项目 4-产品销售 - 5卡充值 - 6疗程充值 gct07c varchar(20) null, --代码 gct08c varchar(100) null, --名称 gct09f float null, --标准单价 gct10f float null, --折扣单价 gct11f float null, --数量 gct12f float null, --价格 gct13c varchar(10) null, --单据类别 gct14c varchar(20) null, --单据单号 gct15c varchar(20) null, --员工1 gct16c varchar(20) null, --员工2 gct17c varchar(20) null, --员工3 gct18c varchar(20) null, --员工4 gct19t varchar(10) null, --时间 gct20c varchar(5) null, --支付方式d gct21f float null, --项目消费序号 ) insert into #gcm20(gct00c,gct01i,gct02c,gct03c,gct04d,gct05c,gct06c,gct07c,gct08c,gct09f,gct10f,gct11f,gct12f,gct13c,gct14c,gct15c,gct16c,gct17c,gct18c,gct19t,gct20c,gct21f) select gct00c,gct01i,gct02c,gct03c,gct04d,gct05c,gct06c,gct07c,gct08c,gct09f,gct10f,gct11f,gct12f,gct13c,gct14c,gct15c,gct16c,gct17c,gct18c,gct19t,gct20c,gct21f from gcm20 a,#mem_table b where a.gct13c in ('gx','gn','gz') and a.gct02c = b.cardid update a set a.balanceamt = isnull(b.gcc06f,0) from #mem_table a, gcm03 b with(nolock) where b.gcc00c = @compid and b.gcc03i = 2 and a.compid = b.gcc00c and a.cardid = b.gcc01c update a set a.treatmentcount = isnull(b.treatmentcount,0) from #mem_table a, (select gcf00c,gcf01c,sum(gcf07f) treatmentcount from gcm06 with(nolock) where gcf00c = @compid group by gcf00c,gcf01c) b where a.compid = b.gcf00c and a.cardid = b.gcf01c update a set a.rechargeamt = isnull(b.rechargeamt,0) from #mem_table a, (select gcd00c,gcd01c,sum(gcd06f) rechargeamt from gcm04 with(nolock) where gcd00c = @compid and gcd07c = 'gz' group by gcd00c,gcd01c) b where a.compid = b.gcd00c and a.cardid = b.gcd01c update a set a.consumeamt = isnull(amt,0),a.consumedate = b.lastdate from #mem_table a,(select gct02c,sum(gct12f) amt,max(gct04d) lastdate from #gcm20 group by gct02c) b where a.cardid = b.gct02c declare @date1 varchar(8),@date2 varchar(8), @date3 varchar(8),@date4 varchar(8), @date5 varchar(8) set @date1 = convert(varchar(8),getdate(),112) set @date2 = convert(varchar(8),dateadd(month,-1,getdate()),112) set @date3 = convert(varchar(8),dateadd(month,-3,getdate()),112) set @date4 = convert(varchar(8),dateadd(month,-6,getdate()),112) set @date5 = convert(varchar(8),dateadd(month,-12,getdate()),112) update a set level = 0 from #mem_table a where a.consumedate > @date2 and a.consumedate <= @date1 -- 一个月以内 and isnull(a.isinvalid,1) = 1 update a set level = 1 from #mem_table a where a.consumedate >= @date3 and a.consumedate <= @date2 -- 3个月以内 and isnull(a.isinvalid,1) = 1 update a set level = 2 from #mem_table a where a.consumedate >= @date4 and a.consumedate < @date3 -- 3个月到半年之间 and isnull(a.isinvalid,1) = 1 update a set level = 3 from #mem_table a where a.consumedate >= @date5 and a.consumedate < @date4 -- 半年到一年之间 and isnull(a.isinvalid,1) = 1 update a set level = 4 from #mem_table a where a.consumedate < @date5 --一年以上 and isnull(a.isinvalid,1) = 1 update a set a.level = 5 from #mem_table a where a.isinvalid = 0 select * from #mem_table order by cardid drop table #mem_table drop table #gcm20 end GO /****** Object: Table [dbo].[gam14] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gam14]( [gal00c] [varchar](10) NOT NULL, [gal01c] [varchar](5) NOT NULL, [gal02i] [int] NOT NULL, [gal03f] [float] NULL, [gal04i] [int] NULL, [gal05i] [int] NULL, [gal06i] [int] NULL, CONSTRAINT [PK_GAM14] PRIMARY KEY CLUSTERED ( [gal00c] ASC, [gal01c] ASC, [gal02i] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gbm19] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gbm19]( [gbs00c] [varchar](10) NOT NULL, [gbs01d] [varchar](10) NOT NULL, [gbs02c] [varchar](30) NOT NULL, [gbs03c] [varchar](30) NOT NULL, [gbs04i] [int] NOT NULL, [gbs05f] [float] NULL, [gbs06f] [float] NULL, [gbs07f] [float] NULL, [gbs08f] [float] NULL, [gbs09c] [varchar](30) NULL, [gbs10i] [int] NULL, [gbs11i] [int] NULL, CONSTRAINT [PK_GBM19] PRIMARY KEY NONCLUSTERED ( [gbs00c] ASC, [gbs01d] ASC, [gbs02c] ASC, [gbs03c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_stat_prj] Script Date: 06/12/2017 06:20:44 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_stat_prj] ( @compid varchar(10), -- 公司别 @fromdate varchar(8), -- 开始日期 @todate varchar(8), -- 截至日期 @type int, -- 1: 统计项目数量 2:统计项目金额, --3:统计产品销售数量, 4:统计产品销售金额; 5: 产品销售 + 消耗数量; 6:产品销售 + 消耗金额 @top int, -- 前几名 @kind varchar(20) ) -- 项目统计类别 as begin declare @incompid varchar(10) select @incompid = @compid if @compid = '001' select @compid = '%' create table #result( idd int identity not null, item varchar(20) not null, item_name varchar(60) null, value float null, primary key(idd)) create table #result2( idd int identity not null, item varchar(20) not null, item_name varchar(60) null, value float null, primary key(idd)) create table #result3( idd int identity not null, item varchar(20) not null, item_name varchar(60) null, value float null, primary key(idd)) if @type = 1 begin insert #result(item, item_name, value) select item = gma03c, item_name = gda03c, value = sum(gma05f) from gmm01 a with (nolock), gdm01 c, gam26 z where a.gma01d >= @fromdate and a.gma01d <= @todate and a.gma00c = z.gaz02c and z.gaz01c = @incompid and a.gma00c = c.gda00c and a.gma03c = c.gda01c and a.gma02i = 1 and (@kind = '*' or c.gda13c = @kind) group by gma03c, gda03c end else if (@type = 2) begin insert #result(item, item_name, value) select item = gma03c, item_name = gda03c, value = sum(gma06f) from gmm01 a with (nolock), gdm01 c, gam26 z where a.gma01d >= @fromdate and a.gma01d <= @todate and a.gma00c = z.gaz02c and z.gaz01c = @incompid and a.gma00c = c.gda00c and a.gma03c = c.gda01c and a.gma02i = 1 and (@kind = '*' or c.gda13c = @kind) group by gma03c, gda03c end else if (@type = 3 or @type = 5) begin insert #result(item, item_name, value) select item = gma03c, item_name = gfa03c, value = sum(gma05f) from gmm01 a with (nolock), gfm01 c, gam26 z where a.gma01d >= @fromdate and a.gma01d <= @todate and a.gma00c = z.gaz02c and z.gaz01c = @incompid and a.gma00c = c.gfa00c and a.gma03c = c.gfa01c and a.gma02i = 2 and (@kind = '*' or c.gfa24c = @kind) group by gma03c, gfa03c if (@type = 5) begin insert #result(item, item_name, value) select item = gge03c, item_name = gfa03c, value = sum(gge05f/isnull(gfa09f, 1)) from ggm05 b with (nolock), ggm01 a with (nolock), gfm01 c, gam26 z where b.gge00c = a.gga00c and b.gge01c = a.gga01c and a.gga02d >= @fromdate and a.gga02d <= @todate and b.gge00c = z.gaz02c and z.gaz01c = @incompid and b.gge00c = c.gfa00c and b.gge03c = c.gfa01c and (@kind = '*' or c.gfa24c = @kind) group by gge03c, gfa03c end end else if (@type = 4 or @type = 6) begin insert #result(item, item_name, value) select item = gma03c, item_name = gfa03c, value = sum(gma06f) from gmm01 a, gfm01 c, gam26 z where a.gma01d >= @fromdate and a.gma01d <= @todate and a.gma00c = z.gaz02c and z.gaz01c = @incompid and a.gma00c = c.gfa00c and a.gma03c = c.gfa01c and a.gma02i = 2 and (@kind = '*' or c.gfa24c = @kind) group by gma03c, gfa03c if (@type = 6) begin insert #result(item, item_name, value) select item = gge03c, item_name = gfa03c, value = sum(gfa11f*(gge05f/isnull(gfa09f, 1))) from ggm05 b with (nolock), ggm01 a with (nolock), gfm01 c, gam26 z where b.gge00c = a.gga00c and b.gge01c = a.gga01c and a.gga02d >= @fromdate and a.gga02d <= @todate and b.gge00c = z.gaz02c and z.gaz01c = @incompid and b.gge00c = c.gfa00c and b.gge03c = c.gfa01c and (@kind = '*' or c.gfa24c = @kind) group by gge03c, gfa03c end end insert #result2(item, item_name, value) select item, item_name, sum(value) from #result group by item, item_name insert #result3(item, item_name, value) select item, item_name, value from #result2 order by value desc delete #result3 where idd>@top select idd, isnull(item,'') item, isnull(item_name,'') item_name, isnull(value,0) value from #result3 order by value desc drop table #result drop table #result2 drop table #result3 end GO /****** Object: StoredProcedure [dbo].[upg_billid_search_3g] Script Date: 06/12/2017 06:19:30 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_billid_search_3g] ( @compid varchar(10), -- 公司别 @fromdate varchar(20), -- 单号起始 @todate varchar(20), @key bigint, -- 钥匙号 @remark varchar(1000) -- 备注 ) as -- 前台终端未结帐或未结束服务单据查询,处理了备注信息 begin declare @billflag int select @billflag = 0 create table #tbl_master ( compid varchar(20) not null, billid varchar(20) not null, person varchar(20) null, ddate varchar(16) null, ttime varchar(12) null, memtype int null, -- 开单类型 0-会员;1-散客 memcard varchar(20) null, -- 开单类型为会员,则填会员卡号 billflag int null, -- 标志列 0-开单 1-已经结账(不可修改了) remark varchar(1000) null, -- 开单备注信息, 方便快速查询(以前用的) keyid bigint null, -- 开单钥匙牌号 gender int null, -- 性别 0:女 1:男 ggm_billid varchar(20) null, -- 收银单号 selected int null, idd int identity not null, enddate varchar(10) null, --最后更新日期 endtime varchar(20) null, --最后更新时间 primary key(idd) ) insert #tbl_master(compid, billid, person, ddate, ttime, memtype, memcard, billflag, keyid, gender, selected, remark, enddate, endtime) select compid, billid, person, ddate = substring(datetime, 1, 8), ttime = substring(datetime, 9, 6), memtype, memcard, billflag, keyid, gender, selected = 0, remark, ddate = substring(endtime,1,8), ttime = substring(endtime,9,6) from openpolicy_master where billflag in(0,3) and compid = @compid and substring(datetime, 1, 8) >= @fromdate and substring(datetime, 1, 8) <= @todate and (keyid = @key or @key = 0) create table #tbl_bill_srv ( compid varchar(10) not null, billid varchar(20) not null, srvcode varchar(20) null, srvname varchar(30) null, primary key(compid, billid) ) insert #tbl_bill_srv(compid, billid, srvcode) select a.compid, a.billid, min(b.empa) as empa from #tbl_master a, openpolicy_detail b where a.compid = b.compid and a.billid = b.billid group by a.compid, a.billid update a set a.srvname = b.haa02c from #tbl_bill_srv a, ham01 b where a.compid = b.haa00c and a.srvcode = b.haa01c -- 先显示发型师名字 update a set a.remark = b.srvname + '(' + b.srvcode + '):' + isnull(gba03c, '') + ':' + isnull(a.memcard, '') from #tbl_master a, #tbl_bill_srv b, gbm01 c where a.compid = b.compid and a.billid = b.billid and a.compid = c.gba00c and c.gba23c = a.memcard and a.memtype = 0 -- 散客 update a set a.remark = b.srvname + '(' + b.srvcode + '):' + isnull(a.remark, '') from #tbl_master a, #tbl_bill_srv b where a.compid = b.compid and a.billid = b.billid and a.memtype = 1 -- 显示客户的名字 drop table #tbl_bill_srv if @remark = '' or @remark = '*' begin select compid, billid, person, ddate, ttime, memtype, memcard, billflag, gender, keyid, selected, remark, enddate, endtime from #tbl_master end else begin select @remark = '%' + @remark + '%' select compid, billid, person, ddate, ttime, memtype, memcard, billflag, gender, keyid, selected, remark, enddate, endtime from #tbl_master left join gcm01 on gca01c = memcard and gca13d = gca00c where (memcard like @remark or gca30c like @remark) end drop table #tbl_master end GO /****** Object: Table [dbo].[gtc10] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gtc10]( [gtj00c] [varchar](10) NOT NULL, [gtj01c] [varchar](20) NOT NULL, [gtj02c] [varchar](20) NOT NULL, [gtj03c] [varchar](20) NOT NULL, [gtj04c] [varchar](20) NOT NULL, [gtj05f] [float] NULL, [gtj06f] [float] NULL, [gtj07f] [float] NULL, [gtj08c] [varchar](20) NULL, [gtj09f] [float] NULL, [gtj10i] [int] NULL, CONSTRAINT [PK_GTC10] PRIMARY KEY CLUSTERED ( [gtj00c] ASC, [gtj01c] ASC, [gtj02c] ASC, [gtj03c] ASC, [gtj04c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[openpolicy_pay_together_del] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[openpolicy_pay_together_del]( [id] [int] NOT NULL, [compid] [varchar](20) NOT NULL, [operationdate] [varchar](8) NOT NULL, [operationtime] [varchar](8) NULL, [rootcard] [varchar](20) NULL, [handcard] [varchar](20) NULL, [userid] [varchar](20) NULL, [ddate] [varchar](10) NULL, [ttime] [varchar](10) NULL, [idd] [int] IDENTITY(1,1) NOT NULL, CONSTRAINT [PK_OPENPOLICY_PAY_TOGETHER_DEL] PRIMARY KEY CLUSTERED ( [idd] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[hcm01_operation] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[hcm01_operation]( [id] [numeric](18, 0) IDENTITY(1,1) NOT NULL, [hcId] [numeric](18, 0) NOT NULL, [operation] [int] NOT NULL, [operator] [varchar](10) NOT NULL, [operateTime] [datetime] NOT NULL, [description] [varchar](1000) NULL, [state] [int] NOT NULL, CONSTRAINT [PK_hcm01_operation] PRIMARY KEY CLUSTERED ( [id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[bk_channel_pic] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[bk_channel_pic]( [itemno] [varchar](20) NOT NULL, [img] [image] NULL, [type] [varchar](2) NULL, [idd] [numeric](19, 0) IDENTITY(1,1) NOT NULL, CONSTRAINT [PK_bk_channel_pic] PRIMARY KEY CLUSTERED ( [idd] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_member_operation_details_3g] Script Date: 06/12/2017 06:20:23 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_member_operation_details_3g] ( @compid varchar(10), @cardno varchar(20), @fromdate varchar(8), @todate varchar(8), @orderby int -- 0 asc 1 desc ) as -- 查询一个会员的详细历史,增加满意度评价 begin create table #details ( seq int not null, date varchar(8) not null, action varchar(10) not null, item varchar(20) not null, name varchar(100) null, memo varchar(1000) null, -- 备注 price float null, -- 标准价格 amt float null, person1 varchar(20) null, person2 varchar(20) null, person3 varchar(20) null, person4 varchar(20) null, compid varchar(10) null, billid varchar(20) null, type varchar(10) null, fillmode varchar(10) null, quan float null, ttime varchar(20) null, paycode varchar(5) null, pingjia int null, gct21f float null, compname varchar(40) null, primary key(seq) ) insert #details(seq, date, action, item, name, memo, price, amt, person1, person2, person3, person4, compid, billid, type, fillmode, quan, ttime, paycode, gct21f, compname) exec upg_member_operation_details @compid, @cardno, @fromdate, @todate, @orderby alter table #details drop column memo alter table #details drop column gct21f update a set pingjia = (case when (isnull(ggf02c, '') <> '' and isnull(ggf02c, 'null') <> 'null') then ggf04i when (isnull(ggf05c, '') <> '' and isnull(ggf05c, 'null') <> 'null') then ggf07i when (isnull(ggf08c, '') <> '' and isnull(ggf08c, 'null') <> 'null') then ggf10i when (isnull(ggf20c, '') <> '' and isnull(ggf20c, 'null') <> 'null') then ggf22i end) from #details a, ggm06 g where a.compid = g.ggf00c and a.billid = g.ggf01c if @orderby = 0 begin select * from #details order by date asc, ttime asc, seq asc end else begin select * from #details order by date desc, ttime desc, seq desc end drop table #details end GO /****** Object: Table [dbo].[gqm02] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gqm02]( [gqb00c] [varchar](10) NOT NULL, [gqb01c] [varchar](20) NOT NULL, [gqb02f] [float] NOT NULL, [gqb03c] [varchar](5) NOT NULL, [gqb04c] [varchar](600) NULL, [gqb05f] [float] NULL, [gqb06c] [varchar](10) NULL, [gqb07c] [varchar](10) NULL, [gqb08c] [varchar](10) NULL, [gqb09c] [varchar](10) NULL, [gqb10c] [varchar](10) NULL, [gqb11f] [float] NULL, [gqb12c] [varchar](5) NULL, [gqb13f] [float] NULL, [gqb14c] [varchar](5) NULL, [gqb15f] [float] NULL, [gqb16f] [float] NULL, [gqb17f] [float] NULL, [gqb18f] [float] NULL, [gqb19i] [int] NULL, CONSTRAINT [PK_GQM02] PRIMARY KEY CLUSTERED ( [gqb00c] ASC, [gqb01c] ASC, [gqb02f] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gdm03] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gdm03]( [gdc00c] [varchar](10) NOT NULL, [gdc01c] [varchar](20) NOT NULL, [gdc02p] [image] NULL, [gdc03p] [image] NULL, [gdc04p] [image] NULL, CONSTRAINT [PK_GDM03] PRIMARY KEY CLUSTERED ( [gdc00c] ASC, [gdc01c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_cal_next_month] Script Date: 06/12/2017 06:19:33 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_cal_next_month](@curmonth varchar(20) output) as begin declare @priormonth varchar(20) declare @todate varchar(20) declare @date_todate datetime declare @cal_date varchar(20) select @cal_date = @curmonth + '01' select @date_todate = dateadd(month, 1, @cal_date) select @todate = substring(convert(varchar(20), @date_todate, 102), 1, 4) + substring(convert(varchar(20), @date_todate, 102), 6, 2) + substring(convert(varchar(20), @date_todate, 102), 9, 2) select @curmonth = substring(@todate, 1, 6) end GO /****** Object: Table [dbo].[gsm03] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gsm03]( [gsc00c] [varchar](10) NOT NULL, [gsc01c] [varchar](20) NOT NULL, [gsc02c] [varchar](5) NOT NULL, [gsc03f] [float] NOT NULL, [gsc04c] [varchar](5) NULL, [gsc05f] [float] NULL, [gsc06c] [nvarchar](500) NULL, [gsc07f] [float] NULL, [gsc08c] [varchar](20) NULL, [gsc09c] [varchar](20) NULL, [gsc10d] [varchar](8) NULL, [gsc11c] [varchar](20) NULL, [gsc12i] [int] NULL, CONSTRAINT [PK_GSM03] PRIMARY KEY NONCLUSTERED ( [gsc00c] ASC, [gsc01c] ASC, [gsc02c] ASC, [gsc03f] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_compare_compid_project] Script Date: 06/12/2017 06:19:43 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_compare_compid_project] ( @compid nvarchar(2000), -- 公司别 @fromdate varchar(10), -- 开始日期 @todate varchar(10), -- 截至日期 @fromproject varchar(20), -- 开始项目类别 @toproject varchar(20), -- 结束项目类别 @amttype int -- 0-营业额 1-虚业绩 ) as begin declare @sql nvarchar(4000) create table #st_results ( compid varchar(10) null, date varchar(8) null, type varchar(20) null, cnt float null, amt float null, price float null, ) -- 不同统计类的 if @amttype = 0 begin set @sql = '' set @sql = N'insert #st_results(compid, date, type, cnt, amt) select a.ggb00c, substring(gga80d, 1, 6), gda13c, cnt = sum(ggb05f), amt = sum(isnull(a.ggb11f, 0)) from ggm02 a, gdm01 b, ggm01 c where a.ggb00c = b.gda00c and a.ggb03c = b.gda01c and a.ggb00c in ' + @compid + ' and a.ggb00c = c.gga00c and a.ggb01c = c.gga01c and substring(c.gga80d, 1, 6) >= ' + quotename(@fromdate, '''') + ' and substring(c.gga80d, 1, 6) <= ' + quotename(@todate, '''') + ' and isnull(gda13c, '''')<>'''' and (gda13c between ' + quotename(@fromproject, '''') + ' and ' + quotename(@toproject, '''') + ' or cast(' + quotename(@fromproject, '''') + ' as varchar(20)) = ''*'') group by a.ggb00c, substring(gga80d, 1, 6), gda13c' execute sp_executesql @sql end else if @amttype = 1 begin set @sql = '' set @sql = N'insert #st_results(compid, date, type, cnt, amt) select a.ggb00c, substring(c.gga80d, 1, 6), gda13c, cnt = sum(ggb05f), amt = sum(isnull(a.ggb20f, 0) + isnull(a.ggb21f, 0) + isnull(a.ggb22f, 0) + isnull(a.ggb35f, 0) ) from ggm02 a, gdm01 b, ggm01 c where a.ggb00c = b.gda00c and a.ggb03c = b.gda01c and a.ggb00c in' + @compid + ' and a.ggb00c = c.gga00c and a.ggb01c = c.gga01c and substring(c.gga80d, 1, 6) >= ' + quotename(@fromdate, '''') + ' and substring(c.gga80d, 1, 6) <= ' + quotename(@todate, '''') + ' and isnull(gda13c, '''')<>'''' and (gda13c between ' + quotename(@fromproject, '''') + ' and ' + quotename(@toproject, '''') + 'or cast(' + quotename(@fromproject, '''') + 'as varchar(20)) = ''*'') group by a.ggb00c, substring(gga80d, 1, 6), gda13c' execute sp_executesql @sql end update #st_results set price = isnull(amt, 0)/isnull(cnt, 1) select * from #st_results end GO /****** Object: Table [dbo].[gzm07] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gzm07]( [gzg00c] [varchar](10) NOT NULL, [gzg01c] [varchar](5) NOT NULL, [gzg02c] [varchar](20) NOT NULL, [gzg03c] [float] NOT NULL, [gzg04c] [varchar](20) NOT NULL, [gzg05c] [float] NOT NULL, CONSTRAINT [PK_GZM07] PRIMARY KEY CLUSTERED ( [gzg00c] ASC, [gzg01c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gam20] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gam20]( [gar00c] [varchar](10) NOT NULL, [gar01i] [int] IDENTITY(1,1) NOT NULL, [gar02i] [int] NOT NULL, [gar03c] [varchar](20) NULL, [gar04c] [text] NULL, CONSTRAINT [PK_GAM20] PRIMARY KEY NONCLUSTERED ( [gar00c] ASC, [gar01i] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_payment_dropdown] Script Date: 06/12/2017 06:20:25 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_payment_dropdown] ( @compid varchar(10), @cardid varchar(20) ) as begin create table #tbl_card_pay ( code varchar(10) not null, nname varchar(30) null, ddate varchar(30) null ) insert #tbl_card_pay(code, nname, ddate) select distinct a.gsn01c, c.gsb03c, '' from gsm14 a, gcm03 b with (nolock), gsm02 c, gcm01 d with (nolock) where gcc00c = gca00c and d.gca01c = b.gcc01c and d.gca13d = d.gca00c and b.gcc01c = @cardid and (gsn02c = gcc03i or isnull(gsn02c, '') = '' or isnull(gsn02c, '') = '999') and b.gcc00c = c.gsb00c and a.gsn01c = c.gsb02c and c.gsb01c = '3' update a set a.ddate = '(' + b.gcc09d + ',' + convert(varchar(20), b.gcc06f) + ')' from #tbl_card_pay a, gcm03 b, gsm14 c, gcm01 d where a.code = c.gsn01c and c.gsn02c = b.gcc03i and b.gcc00c = d.gca00c and d.gca01c = b.gcc01c and d.gca00c = d.gca13d and b.gcc01c = @cardid select code, nname + ddate from #tbl_card_pay drop table #tbl_card_pay end GO /****** Object: StoredProcedure [dbo].[get_month_days] Script Date: 06/12/2017 06:19:20 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[get_month_days] ( @year int, @month int, @day int output ) as declare @leap int --1 is leap 0 is not leap begin if (@year/4 = 0 and @year%100<>0 or @year/400 = 0) set @leap = 1 if (@month = 1) set @day = 31 if (@month = 2) begin if (@leap = 1) set @day = 28 else set @day = 29 end if (@month = 3) set @day = 31 if (@month = 4) set @day = 30 if (@month = 5) set @day = 31 if (@month = 6) set @day = 30 if (@month = 7) set @day = 31 if (@month = 8) set @day = 31 if (@month = 9) set @day = 30 if (@month = 10) set @day = 31 if (@month = 11) set @day = 30 if (@month = 12) set @day = 31 end GO /****** Object: Table [dbo].[gdm10] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gdm10]( [gdj00c] [varchar](10) NOT NULL, [gdj01c] [varchar](5) NOT NULL, [gdj02c] [varchar](20) NOT NULL, [gdj03c] [varchar](20) NULL, [gdj04i] [int] NOT NULL, [gdj05i] [int] NULL, [gdj06c] [varchar](60) NULL, [gdj07c] [varchar](20) NULL, [gdj08i] [int] NULL, CONSTRAINT [PK_GDM10] PRIMARY KEY NONCLUSTERED ( [gdj00c] ASC, [gdj01c] ASC, [gdj02c] ASC, [gdj04i] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[potential_member_enter_sms] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[potential_member_enter_sms]( [compid] [varchar](20) NOT NULL, [mobile] [varchar](50) NOT NULL, [enterdate] [varchar](10) NOT NULL, [sendsms] [int] NULL, CONSTRAINT [PK_potential_member_enter_sms] PRIMARY KEY CLUSTERED ( [compid] ASC, [mobile] ASC, [enterdate] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gtc08_log] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gtc08_log]( [gtj00c] [varchar](10) NOT NULL, [gtj01c] [varchar](20) NOT NULL, [gtj02c] [varchar](20) NOT NULL, [gtj03c] [varchar](20) NOT NULL, [gtj04f] [float] NULL, [gtj05f] [float] NULL, [gtj06f] [float] NULL, [gtj07c] [varchar](20) NULL, [gtj09i] [int] NULL, [gtj10c] [varchar](20) NULL, [gtj11c] [varchar](50) NULL, [idd] [int] IDENTITY(1,1) NOT NULL, [compid] [varchar](10) NULL, [userid] [varchar](20) NULL, [ddate] [varchar](10) NULL, [ttime] [varchar](10) NULL, CONSTRAINT [PK_GTC08_LOG] PRIMARY KEY CLUSTERED ( [idd] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[ControlConfiguration] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[ControlConfiguration]( [id] [int] IDENTITY(1,1) NOT NULL, [UserId] [nvarchar](50) NULL, [ShopId] [nvarchar](50) NULL, [ControlName] [nvarchar](100) NULL, [Columns] [nvarchar](max) NULL, [Summary] [nvarchar](max) NULL, [Userfield1] [nvarchar](max) NULL, [Userfield2] [image] NULL, [Userfield3] [nvarchar](500) NULL, [Userfield4] [nvarchar](500) NULL, [Userfield5] [nvarchar](500) NULL, CONSTRAINT [PK_ControlConfiguration] PRIMARY KEY CLUSTERED ( [id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] GO /****** Object: Table [dbo].[gdm21] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gdm21]( [gdu00c] [varchar](10) NOT NULL, [gdu01c] [varchar](20) NOT NULL, [gdu02f] [float] NOT NULL, [gdu03i] [int] NOT NULL, [gdu04c] [varchar](20) NOT NULL, [gdu05f] [float] NOT NULL, [gdu06f] [float] NULL, [gdu07i] [int] NULL, [gdu08i] [int] NULL, [gdu09i] [int] NULL, [gdu10i] [int] NULL, [gdu11i] [int] NULL, [gdu12f] [float] NULL, [gdu13i] [int] NULL, [gdu14i] [int] NULL, [gdu15i] [int] NULL, [gdu16i] [int] NULL, [gdu17i] [int] NULL, [gdu18f] [float] NULL, [gdu19c] [varchar](60) NULL, [gdu20c] [varchar](20) NULL, [gdu21c] [varchar](20) NULL, [gdu22c] [varchar](20) NULL, CONSTRAINT [PK_GDM21] PRIMARY KEY NONCLUSTERED ( [gdu00c] ASC, [gdu01c] ASC, [gdu02f] ASC, [gdu03i] ASC, [gdu04c] ASC, [gdu05f] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[hem05] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[hem05]( [hee00c] [varchar](10) NOT NULL, [hee01c] [varchar](20) NOT NULL, [hee02f] [float] NOT NULL, [hee03f] [float] NULL, [hee04f] [float] NULL, [hee05i] [tinyint] NULL, [hee06f] [float] NULL, [hee07c] [varchar](20) NULL, [hee08c] [varchar](5) NULL, CONSTRAINT [PK_HEM05] PRIMARY KEY CLUSTERED ( [hee00c] ASC, [hee01c] ASC, [hee02f] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[get_new_ids] Script Date: 06/12/2017 06:19:20 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[get_new_ids]( @Length int, --日期后面的编号长度 @Column nvarchar(20), --字段名称 @tableName nvarchar(20), --表名称 @Company nvarchar(20), --门店编号 @Return nvarchar(20) output --输出变量 ) as begin declare @sql nvarchar(2000) set @sql = ' declare @nowdate nvarchar(10) declare @mincoding nvarchar(20) declare @coding int declare @bianhao varchar(20) set @nowdate = convert(nvarchar(10), getdate(), 120) set @nowdate = replace(@nowdate, ''-'', '''') set @nowdate = substring(@nowdate, 3, 8) set @mincoding = @nowdate + replicate(''0'', @length-1) + ''0'' select @coding = isnull(max(convert(int, substring(' + @Column + ', 11, len(' + @Column + ')-7))), ''0000'') from ' + @tableName + ' where ' + @Column + ' like @Company + @nowdate + ''%'' set @coding = @coding + 1 set @bianhao = replicate(''0'', @Length-len(@coding)) + convert(varchar, @coding) set @Return = @Company + @nowdate + @bianhao select @return ' exec sp_executesql @sql, N'@Length int, @Company nvarchar(20), @Return nvarchar(20) output', @Length, @Company, @Return output end GO /****** Object: Table [dbo].[gsm08] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gsm08]( [gsh00c] [varchar](10) NOT NULL, [gsh01c] [varchar](5) NOT NULL, [gsh02c] [varchar](20) NULL, [gsh03f] [float] NULL, [gsh04c] [varchar](10) NULL, [gsh05c] [varchar](10) NULL, [gsh06i] [int] NULL, [gsh07i] [int] NULL, CONSTRAINT [PK_GSM08] PRIMARY KEY CLUSTERED ( [gsh01c] ASC, [gsh00c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_share_combo_amtrate] Script Date: 06/12/2017 06:20:41 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_share_combo_amtrate] ( @compid varchar(10), @billtype varchar(10), @billid varchar(20) ) as begin create table #gtc09_buf ( gti03c varchar(20) not null, -- 套餐编号 gti04c varchar(20) not null, -- 项目编号 gti05f float null, -- 项目总次数 gti09f float null, -- 套餐中金额比率 stdamt float null ) create clustered index idx_gtc09_buf on #gtc09_buf(gti04c) create table #gtc10_buf ( gtj03c varchar(20) not null, -- 套餐编号 gtj04c varchar(20) not null, -- 产品编号 gtj05f float null, -- 总次数 gtj09f float null, -- 套餐中金额比率 stdamt float null ) create clustered index idx_gtc10_buf on #gtc10_buf(gtj04c) insert #gtc09_buf(gti03c, gti04c, gti05f, gti09f) select gti03c, gti04c, gti05f, gti09f from gtc09 with(nolock) where gti00c = @compid and gti01c = @billtype and gti02c = @billid and gti04c = gti08c insert #gtc10_buf(gtj03c, gtj04c, gtj05f, gtj09f) select gtj03c, gtj04c, gtj05f, gtj09f from gtc10 where gtj00c = @compid and gtj01c = @billtype and gtj02c = @billid and gtj04c = gtj08c update a set a.stdamt = b.gtb04f * a.gti05f from #gtc09_buf a, gtc02 b where a.gti04c = b.gtb02c and a.gti03c = b.gtb01c and b.gtb00c = @compid update a set a.stdamt = b.gtd04f * a.gtj05f from #gtc10_buf a, gtc03 b where a.gtj04c = b.gtd02c and a.gtj03c = b.gtd01c and b.gtd00c = @compid create table #comp_amt ( comboid varchar(20) not null, amt float null ) create clustered index idx_comp_amt on #comp_amt(comboid) create table #comp_amt_result ( comboid varchar(20) not null, amt float null ) create clustered index idx_comp_amt_result on #comp_amt_result(comboid) insert #comp_amt(comboid, amt) select gti03c, sum(isnull(stdamt, 0)) from #gtc09_buf group by gti03c insert #comp_amt(comboid, amt) select gtj03c, sum(isnull(stdamt, 0)) from #gtc10_buf group by gtj03c insert #comp_amt_result(comboid, amt) select comboid, sum(amt) from #comp_amt group by comboid -- 有可能套餐的金额合计是0,也就是每个项目都没有设置标准价格,则评分 update a set a.stdamt = 1 * a.gti05f from #gtc09_buf a, #comp_amt_result b where a.gti03c = b.comboid and isnull(b.amt, 0) = 0 update a set a.stdamt = 1 * a.gtj05f from #gtc10_buf a, #comp_amt_result b where a.gtj03c = b.comboid and isnull(b.amt, 0) = 0 -- 重新计算累计的 delete #comp_amt delete #comp_amt_result insert #comp_amt(comboid, amt) select gti03c, sum(isnull(stdamt, 0)) from #gtc09_buf group by gti03c insert #comp_amt(comboid, amt) select gtj03c, sum(isnull(stdamt, 0)) from #gtc10_buf group by gtj03c insert #comp_amt_result(comboid, amt) select comboid, sum(amt) from #comp_amt group by comboid drop table #comp_amt update a set a.gti09f = stdamt / b.amt from #gtc09_buf a, #comp_amt_result b where isnull(stdamt, 0) <> 0 and a.gti03c = b.comboid update a set a.gtj09f = stdamt / b.amt from #gtc10_buf a, #comp_amt_result b where isnull(stdamt, 0) <> 0 and a.gtj03c = b.comboid drop table #comp_amt_result update a set a.gti09f = b.gti09f from gtc09 a, #gtc09_buf b where a.gti03c = b.gti03c and a.gti04c = b.gti04c and a.gti00c = @compid and a.gti01c = @billtype and a.gti02c = @billid update a set a.gtj09f = b.gtj09f from gtc10 a, #gtc10_buf b where a.gtj03c = b.gtj03c and a.gtj04c = b.gtj04c and a.gtj00c = @compid and a.gtj01c = @billtype and a.gtj02c = @billid drop table #gtc09_buf drop table #gtc10_buf end GO /****** Object: Table [dbo].[appointment_perf_ratio] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[appointment_perf_ratio]( [compid] [varchar](20) NOT NULL, [projid] [varchar](20) NOT NULL, [projtype] [varchar](10) NOT NULL, [ratio] [float] NULL, PRIMARY KEY CLUSTERED ( [compid] ASC, [projid] ASC, [projtype] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: UserDefinedFunction [dbo].[fun_divide_default] Script Date: 06/12/2017 06:20:57 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create function [dbo].[fun_divide_default](@divisor float, @dividend float, @default float) returns float as begin declare @outs float if (@dividend = 0) begin set @outs = @default end else begin set @outs = @divisor / @dividend end return @outs end GO /****** Object: Table [dbo].[helpfile] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[helpfile]( [program] [varchar](10) NOT NULL, [moduletype] [tinyint] NULL, [filename] [varchar](255) NULL, CONSTRAINT [Pk_helpfile_id] PRIMARY KEY CLUSTERED ( [program] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gsm13] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gsm13]( [gsm01i] [int] IDENTITY(1,1) NOT NULL, [gsm02i] [int] NULL, [gsm03c] [varchar](20) NULL, [gsm04c] [varchar](10) NULL, [gsm05c] [varchar](2048) NULL, CONSTRAINT [PK_GSM13] PRIMARY KEY CLUSTERED ( [gsm01i] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[update_log] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[update_log]( [upd00c] [varchar](10) NOT NULL, [upd01c] [varchar](50) NOT NULL, [upd02d] [datetime] NOT NULL, [upd03c] [varchar](100) NULL, [upd04c] [varchar](100) NULL, [upd05c] [varchar](100) NULL, [upd06c] [varchar](100) NULL, [upd07c] [varchar](100) NULL, [upd08c] [varchar](100) NULL, [upd09c] [varchar](100) NULL, [upd10c] [varchar](100) NULL, [upd11c] [varchar](50) NOT NULL, [upd12c] [varchar](50) NULL, [upd13c] [varchar](50) NULL ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_query_diff_list_page] Script Date: 06/12/2017 06:20:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_query_diff_list_page] ( @compid varchar(10), -- 登陆公司id @stype int, -- 获取类型,0,产品;1,项目; @classid varchar(10), -- 类别 @page int, -- 表示第几页 @pagesize int, -- 每页多少条数据 @killnopic int -- 只显示有图的,0否1是 ) as begin create table #tbl_data_tem ( pid int identity not null, compid varchar(20) not null, wpid varchar(20) not null, -- 产品、项目、员工、会员卡编号 pname varchar(60) null, --名称 punit varchar(500) null, --销售单位 pprice float null, --外购价格 pclass varchar(40) null, --统计分类 picid bigint null, -- 对应图片id pstatus varchar(2) null,--//图片状态,0,首页大图不显示,1,首页大图显示,2,小图首页显示,3,小图普通 pstype varchar(2) null,--//图片类型:0,产品;1,项目;2,会员卡;3,员工;4,门店介绍;5,抵用券;6,广告图; primary key(pid) ) --产品 if (@stype = 0) begin if(@classid='' or @classid='*') begin insert #tbl_data_tem(compid,wpid,pname,punit,pprice,pclass) select f.gfa00c as compid,f.gfa01c as wpid,f.gfa03c as pname,f.gfa07c as punit,f.gfa71f as pprice,f.gfa24c as pclass from gfm01 f with (nolock) where f.gfa00c=@compid and f.gfa70i=1--是否允许外购 and isnull(f.gfa10c,'0')='0' and (isnull(f.gfa47c,'0')='0' or f.gfa47c='')--是否停用 order by f.gfa01c end else begin insert #tbl_data_tem(compid,wpid,pname,punit,pprice,pclass) select f.gfa00c as compid,f.gfa01c as wpid,f.gfa03c as pname,f.gfa07c as punit,f.gfa71f as pprice,f.gfa24c as pclass from gfm01 f with (nolock) where f.gfa00c=@compid and f.gfa70i=1--是否允许外购 and isnull(f.gfa10c,'0')='0' and (isnull(f.gfa47c,'0')='0' or f.gfa47c='')--是否停用 and f.gfa24c=@classid order by f.gfa01c end --更新本门店的图片id update a set a.picid=(select top 1 isnull(p.gff04c,0) from gfm06p p with (nolock) where wpid=p.gff01c and compid=p.gff00c and datalength(p.gff02p)>20 and p.gff00c=@compid and (isnull(p.gff03c,3)=3 or isnull(p.gff03c,3)=2) order by p.gff04c desc) from #tbl_data_tem a --没有图片的,从001门店更新 update a set a.picid=(select top 1 isnull(p.gff04c,0) from gfm06p p with (nolock) where wpid=p.gff01c and datalength(p.gff02p)>20 and p.gff00c='001' and (isnull(p.gff03c,3)=3 or isnull(p.gff03c,3)=2) order by p.gff04c desc) from #tbl_data_tem a where isnull(picid,0)=0 --更新类别成中文名字 update a set a.pclass=(select g.gsb03c from gsm02 g with (nolock) where g.gsb02c=pclass and g.gsb00c=compid and g.gsb01c='AL') from #tbl_data_tem a --更新图片状态 update a set a.pstatus=(select isnull(p.gff03c,3) from gfm06p p with (nolock) where p.gff04c=picid) from #tbl_data_tem a end --项目 if(@stype=1) begin if(@classid='' or @classid='*') begin insert #tbl_data_tem(compid,wpid,pname,punit,pprice,pclass) select d.gda00c as compid,d.gda01c as wpid,d.gda03c as pname,d.gda09c as punit,d.gda10f as pprice,d.gda13c as pclass from gdm01 d with (nolock) where d.gda00c=@compid and (isnull(d.gda14i,1)=1 or d.gda14i=1) --2停用,1正常 and (isnull(d.gda38i,1)=1 or d.gda38i=1) --需要在终端中显示,0 不显示,1显示 order by d.gda01c end else begin insert #tbl_data_tem(compid,wpid,pname,punit,pprice,pclass) select d.gda00c as compid,d.gda01c as wpid,d.gda03c as pname,d.gda09c as punit,d.gda10f as pprice,d.gda13c as pclass from gdm01 d with (nolock) where d.gda00c=@compid and d.gda13c=@classid and (isnull(d.gda14i,1)=1 or d.gda14i=1) --2停用,1正常 and (isnull(d.gda38i,1)=1 or d.gda38i=1) --需要在终端中显示,0 不显示,1显示 order by d.gda01c end --更新本门店的图片id update a set a.picid=(select top 1 isnull(p.gdc04c,0) from gdm03p p with (nolock) where wpid=p.gdc01c and compid=p.gdc00c and datalength(p.gdc02p)>20 and p.gdc00c=@compid and (isnull(p.gdc03c,3)=3 or isnull(p.gdc03c,3)=2) order by p.gdc04c desc) from #tbl_data_tem a --没有图片的,从001门店更新 update a set a.picid=(select top 1 isnull(p.gdc04c,0) from gdm03p p with (nolock) where wpid=p.gdc01c and datalength(p.gdc02p)>20 and p.gdc00c='001' and (isnull(p.gdc03c,3)=3 or isnull(p.gdc03c,3)=2) order by p.gdc04c desc) from #tbl_data_tem a where isnull(picid,0)=0 --更新类别成中文名字 update a set a.pclass=(select g.gsb03c from gsm02 g with (nolock) where g.gsb02c=pclass and g.gsb00c=compid and g.gsb01c='AJ') from #tbl_data_tem a --更新图片状态 update a set a.pstatus=(select isnull(p.gdc03c,3) from gdm03p p with (nolock) where p.gdc04c=picid) from #tbl_data_tem a end if(@killnopic=1) begin delete #tbl_data_tem where isnull(picid,0)=0 end --更新类型 update a set a.pstype=@stype from #tbl_data_tem a if(@page=1) begin Exec ('SELECT TOP ' + @pagesize + 'compid,wpid,pname,punit,pprice,pclass,picid,pstatus,pstype FROM #tbl_data_tem order by pid') end else begin declare @otatol int select @otatol=(@page-1)*@pagesize Exec ('SELECT TOP ' + @pagesize + 'compid,wpid,pname,punit,pprice,pclass,picid,pstatus,pstype FROM #tbl_data_tem ' + ' WHERE pid > (SELECT MAX(pid) FROM (SELECT TOP ' + @otatol + ' pid FROM #tbl_data_tem order by pid) AS T) ' + ' order by pid') end drop table #tbl_data_tem end GO /****** Object: Table [dbo].[cardprecept] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[cardprecept]( [car00c] [varchar](10) NOT NULL, [car01c] [varchar](20) NOT NULL, [car02c] [varchar](20) NOT NULL, [car03i] [int] NULL, [car04f] [float] NULL, [car05i] [int] NULL, [car06f] [float] NULL, [car07i] [int] NULL, [car08f] [float] NULL, [car09i] [int] NULL, [car10f] [float] NULL, [car11f] [float] NULL, [car12f] [float] NULL, [car13f] [float] NULL, [car14f] [float] NULL, [car15f] [float] NULL, [car16c] [varchar](100) NULL, [car17c] [varchar](100) NULL, [car18c] [varchar](100) NULL, [car19c] [varchar](100) NULL, [car20c] [varchar](100) NULL, CONSTRAINT [PK_CARDPRECEPT] PRIMARY KEY CLUSTERED ( [car00c] ASC, [car01c] ASC, [car02c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_get_account_remain] Script Date: 06/12/2017 06:20:04 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_get_account_remain] ( @compid varchar(10), @accttype varchar(20), -- 账户代码 @cardtype varchar(20), -- 卡类别 @fromcardid varchar(20), @tocardid varchar(20) ) as begin if @accttype = '*' select @accttype = '0' create table #cardinfo ( compid varchar(20) not null,--门店编号 cardid varchar(20) not null,--会员卡号 memname varchar(100) null,--会员卡姓名 mobile varchar(20) null,--手机号 sex int null, remain float null,--账户余额 cardtype varchar(8) not null,--卡类别代码 cardtypename varchar(40) null,--卡类别名称 accttype varchar(8) not null --账户类别 ) insert #cardinfo(compid, cardid, cardtype, remain, accttype) select gca00c, gca01c, gca02c, gcc06f, gcc03i from gcm01 a with (nolock) ,gcm03 b with (nolock), gam26 z where a.gca00c = b.gcc00c and a.gca01c = b.gcc01c and a.gca00c = z.gaz02c and z.gaz01c = @compid and a.gca00c = a.gca13d and (@cardtype = '*' or (gca02c = @cardtype)) and (@fromcardid = '*' or (gca01c >= @fromcardid and gca01c <= @tocardid)) and ((@accttype = '0') or (b.gcc03i = convert(int, @accttype))) and (gca08i = 4 or gca08i = 5 or gca08i = 10) --修改会员手机号,会员卡姓名 update a set a.mobile = c.gba08c, a.memname = c.gba03c, a.sex = c.gba14i from #cardinfo a, gbm01 c with (nolock) where a.compid = c.gba00c and a.cardid = c.gba23c --修改会员卡类别名称 update a set a.cardtypename = d.gak02c from #cardinfo a,gam10 d with (nolock) where a.compid = d.gak00c and a.cardtype = d.gak01c select isnull(cardid,'') cardid, isnull(memname,'') memname, isnull(mobile,'') mobile,isnull(sex,0) sex, isnull(remain,'') remain, isnull(cardtype,'') cardtype, isnull(cardtypename,0) cardtypename,isnull(accttype,0) accttype from #cardinfo order by cardid drop table #cardinfo end GO /****** Object: Table [dbo].[gem03] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gem03]( [gec00c] [varchar](10) NOT NULL, [gec01c] [varchar](20) NOT NULL, [gec02c] [varchar](20) NOT NULL, [gec03c] [varchar](5) NULL, [gec04c] [varchar](20) NULL, CONSTRAINT [PK_GEM03] PRIMARY KEY CLUSTERED ( [gec00c] ASC, [gec01c] ASC, [gec02c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gma06r_cursor] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gma06r_cursor]( [spid] [int] NOT NULL, [memid] [varchar](20) NOT NULL, [cardid] [varchar](20) NOT NULL, CONSTRAINT [PK_gma06r_cursor] PRIMARY KEY NONCLUSTERED ( [spid] ASC, [memid] ASC, [cardid] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_search_pjpd_page] Script Date: 06/12/2017 06:20:39 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_search_pjpd_page] ( @compid varchar(10), -- 登陆公司id @stype int, -- 获取类型,0,产品;1,项目; @classid varchar(2), -- 类别 @keyword varchar(20), -- 查找关键字 @page int, -- 表示第几页 @pagesize int, -- 每页多少条数据 @killnopic int -- 只显示有图的,0否1是 ) as begin create table #tbl_data_tem( pid int identity not null, compid varchar(20) not null, wpid varchar(20) not null, -- 产品、项目、员工、会员卡编号 pname varchar(60) null, --名称 punit varchar(500) null, --销售单位 pprice float null, --外购价格 pclass varchar(40) null, --统计分类 picid bigint null, -- 对应图片id pstatus varchar(2) null,--//图片状态,0,首页大图不显示,1,首页大图显示,2,小图首页显示,3,小图普通 pstype varchar(2) null,--//图片类型:0,产品;1,项目;2,会员卡;3,员工;4,门店介绍;5,抵用券;6,广告图; primary key(pid) ) declare @keyword1 varchar(30) select @keyword1='%'+@keyword+'%' --产品 if(@stype=0) begin if(@classid='' or @classid='*') begin insert #tbl_data_tem(compid,wpid,pname,punit,pprice,pclass) select f.gfa00c as compid,f.gfa01c as wpid,f.gfa03c as pname,f.gfa07c as punit,f.gfa71f as pprice,f.gfa24c as pclass from gfm01 f with (nolock) where f.gfa00c=@compid and f.gfa70i=1 --是否允许外购 and isnull(f.gfa10c,'0')='0' and isnull(f.gfa47c,'0')='0' --是否停用 and f.gfa03c like @keyword1 -- 名称关键字查找 order by f.gfa01c end else begin insert #tbl_data_tem(compid,wpid,pname,punit,pprice,pclass) select f.gfa00c as compid,f.gfa01c as wpid,f.gfa03c as pname,f.gfa07c as punit,f.gfa71f as pprice,f.gfa24c as pclass from gfm01 f with (nolock) where f.gfa00c=@compid and f.gfa70i=1 --是否允许外购 and isnull(f.gfa10c,'0')='0' and isnull(f.gfa47c,'0')='0' --是否停用 and f.gfa24c=@classid and f.gfa03c like @keyword1 -- 名称关键字查找 order by f.gfa01c end --更新本门店的图片id update a set a.picid=(select top 1 isnull(p.gff04c,0) from gfm06p p with (nolock) where wpid=p.gff01c and compid=p.gff00c and datalength(p.gff02p)>20 and p.gff00c=@compid and (isnull(p.gff03c,3)=3 or isnull(p.gff03c,3)=2) order by p.gff04c desc) from #tbl_data_tem a --没有图片的,从001门店更新 update a set a.picid=(select top 1 isnull(p.gff04c,0) from gfm06p p with (nolock) where wpid=p.gff01c and datalength(p.gff02p)>20 and p.gff00c='001' and (isnull(p.gff03c,3)=3 or isnull(p.gff03c,3)=2) order by p.gff04c desc) from #tbl_data_tem a where isnull(picid,0)=0 --更新类别成中文名字 update a set a.pclass=(select g.gsb03c from gsm02 g with (nolock) where g.gsb02c=pclass and g.gsb00c=compid and g.gsb01c='AL') from #tbl_data_tem a --更新图片状态 update a set a.pstatus=(select isnull(p.gff03c,3) from gfm06p p with (nolock) where p.gff04c=picid) from #tbl_data_tem a end --项目 if(@stype=1) begin if(@classid='' or @classid='*') begin insert #tbl_data_tem(compid,wpid,pname,punit,pprice,pclass) select d.gda00c as compid,d.gda01c as wpid,d.gda03c as pname,d.gda09c as punit,d.gda10f as pprice,d.gda13c as pclass from gdm01 d with (nolock) where d.gda00c=@compid and (isnull(d.gda14i,1)=1 or d.gda14i=1) --2停用,1正常 and (isnull(d.gda38i,1)=1 or d.gda38i=1) --需要在终端中显示,0 不显示,1显示 and d.gda03c like @keyword1 -- 名称关键字查找 order by d.gda01c end else begin insert #tbl_data_tem(compid,wpid,pname,punit,pprice,pclass) select d.gda00c as compid,d.gda01c as wpid,d.gda03c as pname,d.gda09c as punit,d.gda10f as pprice,d.gda13c as pclass from gdm01 d with (nolock) where d.gda00c=@compid and d.gda13c=@classid and (isnull(d.gda14i,1)=1 or d.gda14i=1) --2停用,1正常 and (isnull(d.gda38i,1)=1 or d.gda38i=1) --需要在终端中显示,0 不显示,1显示 and d.gda03c like @keyword1 -- 名称关键字查找 order by d.gda01c end --更新本门店的图片id update a set a.picid=(select top 1 isnull(p.gdc04c,0) from gdm03p p with (nolock) where wpid=p.gdc01c and compid=p.gdc00c and datalength(p.gdc02p)>20 and p.gdc00c=@compid and (isnull(p.gdc03c,3)=3 or isnull(p.gdc03c,3)=2) order by p.gdc04c desc) from #tbl_data_tem a --没有图片的,从001门店更新 update a set a.picid=(select top 1 isnull(p.gdc04c,0) from gdm03p p with (nolock) where wpid=p.gdc01c and datalength(p.gdc02p)>20 and p.gdc00c='001' and (isnull(p.gdc03c,3)=3 or isnull(p.gdc03c,3)=2) order by p.gdc04c desc) from #tbl_data_tem a where isnull(picid,0)=0 --更新类别成中文名字 update a set a.pclass=(select g.gsb03c from gsm02 g with (nolock) where g.gsb02c=pclass and g.gsb00c=compid and g.gsb01c='AJ') from #tbl_data_tem a --更新图片状态 update a set a.pstatus=(select isnull(p.gdc03c,3) from gdm03p p with (nolock) where p.gdc04c=picid) from #tbl_data_tem a end if(@killnopic=1) begin delete #tbl_data_tem where isnull(picid,0)=0 end --更新类型 update a set a.pstype=@stype from #tbl_data_tem a if(@page=1) begin Exec ('SELECT TOP ' + @pagesize + 'compid,wpid,pname,punit,pprice,pclass,picid,pstatus,pstype FROM #tbl_data_tem order by pid') end else begin declare @otatol int select @otatol=(@page-1)*@pagesize Exec ('SELECT TOP ' + @pagesize + 'compid,wpid,pname,punit,pprice,pclass,picid,pstatus,pstype FROM #tbl_data_tem ' + ' WHERE pid > (SELECT MAX(pid) FROM (SELECT TOP ' + @otatol + ' pid FROM #tbl_data_tem order by pid) AS T) ' + ' order by pid') end drop table #tbl_data_tem end GO /****** Object: Table [dbo].[gfm04] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gfm04]( [gfd00c] [varchar](10) NOT NULL, [gfd01c] [varchar](2) NOT NULL, [gfd02c] [nvarchar](100) NOT NULL, [gfd03f] [float] NOT NULL, [gfd04c] [varchar](30) NOT NULL, [gfd05f] [float] NULL, [gfd06f] [float] NULL, [gfd07c] [varchar](8) NULL, [gfd08c] [varchar](4) NULL, [gfd09f] [float] NULL, [gfd10d] [varchar](8) NULL, [gfd11c] [varchar](8) NULL, [gfd12f] [float] NULL, [gfd13f] [float] NULL, [gfd14i] [int] NULL, [gfd15c] [varchar](20) NULL, CONSTRAINT [PK_GFM04] PRIMARY KEY CLUSTERED ( [gfd00c] ASC, [gfd01c] ASC, [gfd02c] ASC, [gfd03f] ASC, [gfd04c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_calc_customercout] Script Date: 06/12/2017 06:19:36 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_calc_customercout] @compid varchar(10), --公司别 @key varchar(20) --手牌号码 as begin create table #result ( billkey varchar(20) not null ) if (isnull(@key, '') = '')--@key为空的时候表明这个入店单号没有进行委托结账 begin --插入购买产品的 insert into #result(billkey) select distinct glk01c from glm09, glm07 where glk11i = 0 and glk05c = glg03c and glk00c = glg00c and glk01c = glg01c and glg12i = 0 and glg13i = 1 and glg00c = @compid --插入做项目的 insert into #result(billkey) select distinct glg01c from glm07 where glg13i = 1 and glg12i = 0 and glg00c = @compid and glg01c not in(select distinct billkey from #result) --结果 select count(*) as customercount from #result end else begin --插入购买产品的 insert into #result(billkey) select distinct glk01c from glm09, glm07 where glk11i = 0 and glk05c = glg03c and glk00c = glg00c and glk01c = glg01c and glg12i = 0 and glg13i = 1 and glg00c = @compid --插入做项目的 create table #key ( [key] varchar(5) not null ) insert #key([key]) select gli05c from glm08 where gli00c = @compid and isnull(gli08i, 0) = 0 and gli07c = @key insert #key([key]) values(@key) insert into #result(billkey) select distinct glg01c from glm07 where glg13i = 1 and glg12i = 0 and glg00c = @compid and glg03c in (select [key] from #key) and glg01c not in(select distinct billkey from #result) --结果 select count(*) as customercount from #result end end GO /****** Object: StoredProcedure [dbo].[upg_get_member_enter_statistics] Script Date: 06/12/2017 06:20:08 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_get_member_enter_statistics] ( @compid varchar(10), -- 公司 @startDate varchar(10), -- 起始日期 @endDate varchar(10) -- 终止日期 ) as begin create table #result ( compid varchar(20) null, -- 门店编码 cardid varchar(50) null, -- 会员卡号 membername varchar(50) null, -- 会员名 owecompid varchar(20) null, -- 卡归属门店编码 mobile varchar(20) null, -- 门店编码 sendsms int null, -- 是否已发短信 lastenter datetime null, -- 最后进店时间 times int null -- 进店次数 ) insert into #result(compid, cardid, times, lastenter) select distinct compid, cardid, count(1), max(createdate) from shop_remind where compid = @compid and convert(varchar(10), createdate, 112) between @startDate and @endDate group by compid, cardid update a set a.owecompid = b.gca13d, a.mobile = c.gba08c, a.membername = c.gba03c from #result a, gcm01 b with(nolock), gbm01 c with(nolock) where a.cardid = b.gca01c and b.gca00c = b.gca13d and b.gca00c = c.gba00c and b.gca04c = c.gba01c update a set a.sendsms = b.sendsms from #result a, member_enter_sms b with(nolock) where a.compid = b.compid and a.cardid = b.cardid and b.enterdate between @startDate and @endDate select compid, cardid, membername, owecompid, mobile, sendsms, times, lastenter from #result order by lastenter desc drop table #result end GO /****** Object: Table [dbo].[gbm06] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gbm06]( [gbf00c] [varchar](10) NOT NULL, [gbf01c] [varchar](20) NOT NULL, [gbf02f] [float] NOT NULL, [gbf03c] [nvarchar](500) NULL, [gbf04c] [varchar](8) NULL, [gbf05c] [varchar](10) NULL, [gbf06c] [varchar](20) NULL, CONSTRAINT [PK_GBM06] PRIMARY KEY CLUSTERED ( [gbf00c] ASC, [gbf01c] ASC, [gbf02f] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[conditionghb02r] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[conditionghb02r]( [ghb00c] [varchar](10) NOT NULL, [ghb01c] [varchar](30) NOT NULL, [ghb02i] [int] NOT NULL, [ghb03c] [text] NULL, [ghb04c] [varchar](5) NOT NULL, CONSTRAINT [PK_CONDITIONGHB02R] PRIMARY KEY CLUSTERED ( [ghb00c] ASC, [ghb01c] ASC, [ghb02i] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_items_push] Script Date: 06/12/2017 06:20:17 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_items_push] ( @compid varchar(10), -- 公司别 @fromdate varchar(10), -- 开始日期 @todate varchar(10), -- 截至日期 @fromempl varchar(20), -- 查询开始人员 @toempl varchar(20), -- 查询截至人员 @inc_card int, -- 卡销售 @inc_service int, -- 服务 @inc_goods int, -- 是否包括产品销售 @return_type int, @paymode varchar(20), @spid int ) -- return 数据的类型, 1-明细数据员工业绩统计报表 -- 2-每人一个提成数据, 用于计算员工薪资 -- 3-业绩排行-虚业绩, 4-业绩排行-营业金额 -- 5-业绩排行-实业绩 -- 7-每人按项目分组 6-总提成 as begin create table #empl_stat_outer ( id int identity , -- 流水好 person_id varchar(20) not null, -- 员工代码 comp_ascription varchar(10) null, -- 公司别(员工) action_id tinyint not null, -- 类别, 1-卡销售 2-指定大工, 3-轮班大工, 4-补工大工, -- 5-指定中工, 6-轮班中工 -- 7-指定小工, 8-轮班小工, 9-产品销售 10,11-四工 -- 0 卡充值 srvdate varchar(10) not null, --日期 code varchar(20) not null, --项目代码, 或是卡号, 产品码 name varchar(40) null, --名称 payway varchar(20) null, --支付方式 amt float null, --营业金额 quan float null, --数量 cost float null, --成本 comm float null, --提成 amt2 float null, --虚业绩 amt3 float null, --真实业绩 cardrate float null, --转卡比率 stdprice float null, --标准单价 prj_type varchar(20) null, --项目类别 share_rate float null default 1, --分享比率 cls_flag int null, -- 1:项目 2:产品 3:卡 gender int null, --性别 0:女 1:男 billid varchar(20) null, --单号 paycode varchar(20) null, --支付代码 ex_rate float null, --实际营业额折算比率 ggm02_seq float null, --ggm02中的序列号, 与单号一起来标志一个项目 room_id varchar(20) null, --房间号,只在spa或足浴版本需要 cardid varchar(20) null, --会员卡号 memid varchar(20) null, --会员编号 sumMoney float null, -- 会员卡销售总金额 compid varchar(20) null ) create clustered index idx_empl_stat_outer_01 on #empl_stat_outer(person_id, prj_type) insert #empl_stat_outer(person_id, comp_ascription, action_id, srvdate, code, name, payway, amt, quan, cost, comm, amt2, amt3, cardrate, stdprice, prj_type, share_rate, cls_flag, gender, billid, paycode, ex_rate, ggm02_seq, room_id, cardid, memid) exec upg_personal_statistics @compid, @fromdate, @todate, @fromempl, @toempl, @inc_card, @inc_service, @inc_goods, @return_type, @spid create table #items_push ( id int identity(1, 1) not null, person_id varchar(20) not null, ---员工编号 itemsid varchar(20) not null, ---项目编号或产品编号,卡号 itemsName varchar(30) null, ---名称 itype float null, ---类型:1.表示项目,2.表示产品,3.卡销售,4.卡充值 itemsNum float null, --数量 itemsMoney float null, ---金额 performance float null, ---业绩 zItems float null, ---指定数 zmoney float null, ---指定金额 lItem float null, ---轮牌数 lmoney float null, ---轮牌金额 push float null, ---提成 clientSum float null, ---总客数 clientz float null, ---指定客数 clientl float null, ---轮牌客数 ) declare @PK_Sql nvarchar(max) set @PK_Sql = ' alter table #items_push add constraint PK_#items_push_' + convert(varchar(10), cast(floor(rand() * 1000000) as int)) + ' PRIMARY KEY(id, person_id, itemsid) ' exec sp_executesql @PK_Sql ----用来计算客数 create table #client ( id int identity not null, clientbill varchar(20) not null, appoint float, ring float, ) set @PK_Sql = ' alter table #client add constraint PK_#client_' + convert(varchar(10), cast(floor(rand() * 1000000) as int)) + ' PRIMARY KEY(id, clientbill)' exec sp_executesql @PK_Sql create table #client1 ( id int identity not null, clientbill varchar(20) not null, appoint float, ring float, ) set @PK_Sql = ' alter table #client1 add constraint PK_#client1_' + convert(varchar(10), cast(floor(rand() * 1000000) as int)) + ' PRIMARY KEY(id, clientbill) ' exec sp_executesql @PK_Sql create table #itemSum ( id int identity(1, 1) not null, person_id varchar(20) not null, itemid varchar(20) not null, lsum float null, lmoney float null, ---轮牌金额 zsum float null, zmoney float null, ---指定金额 ) set @PK_Sql = ' alter table #itemSum add constraint PK_#itemSum_' + convert(varchar(10), cast(floor(rand() * 1000000) as int)) + ' PRIMARY KEY (id, person_id, itemid) ' exec sp_executesql @PK_Sql create table #itemSum1 ( id int identity(1, 1) not null, person_id varchar(20)not null, itemid varchar(20) not null, lsum float null, lmoney float null, ---轮牌金额 zsum float null, zmoney float null, ---指定金额 ) set @PK_Sql = ' alter table #itemSum1 add constraint PK_#itemSum1_' + convert(varchar(10), cast(floor(rand() * 1000000) as int)) + ' PRIMARY KEY(id, person_id, itemid) ' exec sp_executesql @PK_Sql declare @clientSum float ---总客数 declare @clientz float ---指定客数 declare @clientl float ---轮牌客数 insert into #client(clientbill, appoint, ring) select distinct billid, 1, 0 from #empl_stat_outer where action_id in(2, 5, 7) group by billid insert into #client(clientbill, ring, appoint) select distinct billid, 1, 0 from #empl_stat_outer where action_id in(3, 6, 8) group by billid update a set a.ring = b.ring from #client a, #client b where a.clientbill = b.clientbill and a.id<>b.id and isnull(a.ring, 0) = 0 update a set a.appoint = b.appoint from #client a, #client b where a.clientbill = b.clientbill and a.id<>b.id and isnull(a.appoint, 0) = 0 delete a from #client a, #client b where a.clientbill = b.clientbill and a.id>b.id update #client set ring = 0 where appoint<>0 select @clientSum = count(clientbill) from #client select @clientz = sum(appoint) from #client select @clientl = sum(ring) from #client insert into #items_push(person_id, itemsid, itemsName, itype, itemsNum, itemsMoney, performance, push, zItems, lItem, clientSum, clientz, clientl) select person_id, code, max(name), 1, isnull(sum(quan), 0), isnull(sum(amt), 0), isnull(sum(amt3), 0), isnull(sum(comm), 0), 0, 0, 0, 0, 0 from #empl_stat_outer where person_id between @fromempl and @toempl and action_id in(2, 3, 5, 6, 7, 8) group by code, person_id insert #itemSum(person_id, itemid, lsum, zsum, lmoney, zmoney) select person_id, code, isnull(count(action_id), 0), 0, isnull(sum(amt), 0), 0 from #empl_stat_outer where person_id between @fromempl and @toempl and action_id in (3, 6, 8) group by code, person_id insert #itemSum(person_id, itemid, zsum, lsum, lmoney, zmoney) select person_id, code, isnull(count(action_id), 0), 0, 0, isnull(sum(amt), 0) from #empl_stat_outer where person_id between @fromempl and @toempl and action_id in (2, 5, 7) group by code, person_id insert into #itemSum1(person_id, itemid, zsum, lsum, lmoney, zmoney) select person_id, itemid, isnull(sum(zsum), 0), isnull(sum(lsum), 0), isnull(sum(lmoney), 0), isnull(sum(zmoney), 0) from #itemSum group by person_id, itemid delete #itemSum insert into #itemSum(person_id, itemid, zsum, lsum, lmoney, zmoney) select person_id, itemid, zsum, lsum, lmoney, zmoney from #itemSum1 update a set a.lItem = isnull(b.lsum, 0), a.zItems = isnull(b.zsum, 0), a.lmoney = isnull(b.lmoney, 0), a.zmoney = isnull(b.zmoney, 0) from #items_push a, #itemSum b where a.itemsid = b.itemid and a.person_id = b.person_id update #items_push set clientSum = @clientSum, clientz = @clientz, clientL = @clientL insert into #items_push(person_id, itemsid, itemsName, itype, itemsNum, itemsMoney, performance, push, zItems, lItem, clientSum, clientz, clientl) select person_id, code, max(name), 2, isnull(sum(quan), 0), isnull(sum(amt), 0), isnull(sum(amt3), 0), isnull(sum(comm), 0), 0, 0, 0, 0, 0 from #empl_stat_outer where person_id between @fromempl and @toempl and action_id = 9 group by code, person_id insert into #items_push(person_id, itemsid, itemsName, itype, itemsNum, itemsMoney, performance, push, zItems, lItem, clientSum, clientz, clientl) select person_id, '', max(name), 3, isnull(sum(quan), 0), isnull(sum(amt), 0), isnull(sum(amt3), 0), isnull(sum(comm), 0), 0, 0, 0, 0, 0 from #empl_stat_outer where person_id between @fromempl and @toempl and action_id = 1 group by person_id, prj_type insert into #items_push(person_id, itemsid, itemsName, itype, itemsNum, itemsMoney, performance, push, zItems, lItem, clientSum, clientz, clientl) select person_id, max(code), max(name), 4, isnull(sum(quan), 0), isnull(sum(amt), 0), isnull(sum(amt3), 0), isnull(sum(comm), 0), 0, 0, 0, 0, 0 from #empl_stat_outer where person_id between @fromempl and @toempl and action_id = 0 group by person_id, prj_type select * from #items_push order by itype drop table #empl_stat_outer drop table #items_push drop table #itemSum drop table #client drop table #itemSum1 drop table #client1 end GO /****** Object: Table [dbo].[modulestruct] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[modulestruct]( [version] [varchar](10) NOT NULL, [system] [varchar](10) NOT NULL, [modulegroupid] [tinyint] NOT NULL, [parentid] [int] NOT NULL, [moduleid] [int] NOT NULL, [program] [varchar](10) NOT NULL, [name] [varchar](40) NULL, [e_name] [varchar](40) NULL, [classid] [int] NULL, [moduletype] [tinyint] NOT NULL, [orderid] [int] NULL, [imageindex] [int] NULL, [userright] [varchar](20) NULL, [pageright] [varchar](20) NULL, [otherright] [varchar](20) NULL, [report] [varchar](200) NULL, [relativate] [varchar](200) NULL, [specialright] [varchar](200) NULL, [e_specialright] [varchar](200) NULL, [IsReport] [int] NULL, CONSTRAINT [Pk_modulestruct_id] PRIMARY KEY CLUSTERED ( [version] ASC, [system] ASC, [parentid] ASC, [modulegroupid] ASC, [moduleid] ASC, [moduletype] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_create_person_stat] Script Date: 06/12/2017 06:19:54 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_create_person_stat] ( @compid varchar(10), @fromdate varchar(10), @todate varchar(10) ) as begin create table #tbl_buf_person ( compid varchar(10) not null, billid varchar(20) not null, personid varchar(20) not null, type varchar(10) null, idd int identity not null, primary key(idd) ) create table #tbl_ggm07 ( compid varchar(10) not null, billid varchar(20) not null, personid varchar(20) not null, type varchar(10) null, idd int identity not null, primary key(idd) ) insert #tbl_buf_person(compid, billid, personid, type) select ggb00c, ggb01c, ggb12c, ggb15c from ggm01 a with(nolock), ggm02 b with (nolock) where a.gga00c = b.ggb00c and a.gga01c = b.ggb01c and a.gga00c = @compid and a.gga02d >= @fromdate and a.gga02d <= @todate and isnull(ggb12c, '')<>'' insert #tbl_buf_person(compid, billid, personid, type) select ggb00c, ggb01c, ggb13c, ggb16c from ggm01 a with(nolock), ggm02 b with (nolock) where a.gga00c = b.ggb00c and a.gga01c = b.ggb01c and a.gga00c = @compid and a.gga02d >= @fromdate and a.gga02d <= @todate and isnull(ggb13c, '')<>'' insert #tbl_buf_person(compid, billid, personid, type) select ggb00c, ggb01c, ggb14c, ggb19c from ggm01 a with(nolock), ggm02 b with (nolock) where a.gga00c = b.ggb00c and a.gga01c = b.ggb01c and a.gga00c = @compid and a.gga02d >= @fromdate and a.gga02d <= @todate and isnull(ggb14c, '')<>'' insert #tbl_buf_person(compid, billid, personid, type) select ggb00c, ggb01c, ggb32c, ggb31c from ggm01 a with(nolock), ggm02 b with (nolock) where a.gga00c = b.ggb00c and a.gga01c = b.ggb01c and a.gga00c = @compid and a.gga02d >= @fromdate and a.gga02d <= @todate and isnull(ggb32c, '')<>'' insert #tbl_ggm07(compid, billid, personid) select distinct compid, billid, personid from #tbl_buf_person update a set a.type = '1' from #tbl_ggm07 a, #tbl_buf_person b where a.compid = b.compid and a.billid = b.billid and a.personid = b.personid and b.type = '1' update a set a.type = '2' from #tbl_ggm07 a where isnull(a.type, '') = '' delete a from ggm07 a, #tbl_ggm07 b where a.ggg00c = b.compid and a.ggg01c = b.billid and a.ggg02c = b.personid insert ggm07(ggg00c, ggg01c, ggg02c, ggg03c) select compid, billid, personid, type from #tbl_ggm07 drop table #tbl_buf_person drop table #tbl_ggm07 end GO /****** Object: Table [dbo].[gsm28] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gsm28]( [gsq00i] [int] IDENTITY(1,1) NOT NULL, [gsq01c] [varchar](120) NULL, CONSTRAINT [PH_GSM28] PRIMARY KEY CLUSTERED ( [gsq00i] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_analyzer_customer_order_regdate_lm2] Script Date: 06/12/2017 06:19:27 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_analyzer_customer_order_regdate_lm2] ( @compid varchar(10), @class varchar(10), @memfromdate varchar(10), @memtodate varchar(10), @frommemid varchar(20), @tomemid varchar(20), @fromdate varchar(20), @todate varchar(20), @state varchar(200), -- 会员卡状态 @acon_amt float, -- 大于消费金额 @alastdate varchar(8), -- 某个日期之后 @at_times float, -- 大于消费次数 @aav_amt float, -- 大于平均消费金额 @aless_remain float, -- 小于某个储值余额 @abig_remain float, -- 大于某个储值余额 @addtoSMS int, @ordertype int, @only_card_pay int ) as begin create table #mem_result_2 ( idd int null, compid varchar(10), memid varchar(20) not null, -- Member编号 m_name varchar(40) null, -- 姓名 cardid varchar(20) null, -- 卡号 gender int null, -- 性别 0:女 mobile varchar(20) null, -- 手机 tel varchar(20) null, -- 电话 birthday varchar(8) null, -- 会员生日 state varchar(10) null, -- Member卡状态 con_amt float null, -- Consumption金额 consjamt float null, -- 实际消费金额 lastdate varchar(8) null, -- 最后Consumption日期 lastcall varchar(8) null, -- 最后回访日期 t_times float null, -- 用于比较的Consumption次数 t_compare float null, -- 总共来的次数 av_ytimes float null, -- 每年来的次数 av_times float null, -- 每月来的次数 av_wtimes float null, -- 每周来的次数 months float null, -- 客人来了多少个月了,客龄 av_amt float null, -- 每次Consumption的平均金额 start_date varchar(20) null, -- 开始日期 first_date varchar(20) null, -- 第一次来店日期 comp_date varchar(20) null, -- 开始比较的日期 dur_years float null, -- 统计期间来了多少个年了 dur_months float null, -- 统计期间来了多少个月了 dur_weeks float null, -- 统计期间来了多少个周了 remain01 float null, -- 余额 remain02 float null, -- 余额 remain03 float null, -- 余额 remain04 float null, -- 余额 remain05 float null, -- 余额 total_fill_amt float null, -- 累计Charge金额 memaddr varchar(160) null, -- 会员地址 postcode varchar(20) null, -- 邮编 principle varchar(20) null, -- 负责人 total_salecard_amt float null, -- 累计卡销售金额 regdate varchar(20) null, -- 登记日期 -- primary key(memid) ) create table #gcm03 ( gcc00c varchar(10) not null, --门店编号 gcc01c varchar(20) not null, --卡号 gcc03i int not null, --账户类别 gcc03iname varchar(40) null, --账户名称 gcc06f float null --馀额 ) create table #sum_gcm03 ( gcc03i int not null, --账户类别 gcc03iname varchar(40) null, --账户名称 gcc06f float null --馀额 ) insert #mem_result_2(idd, compid, memid, m_name, cardid, gender, mobile, tel, birthday, state, con_amt,consjamt, lastdate, lastcall, t_times, t_compare, av_ytimes, av_times, av_wtimes, months, av_amt, start_date, first_date, comp_date, dur_years, dur_months, dur_weeks, remain01, remain02, remain03, remain04, remain05, total_fill_amt, memaddr, postcode, principle, total_salecard_amt) exec upg_analyzer_customer_ex @compid, @class, @frommemid, @tomemid, @memfromdate, @memtodate, @fromdate, @todate, @state, @acon_amt, @alastdate, @at_times, 0, @aav_amt,@aless_remain, @abig_remain, @addtoSMS, 3, @only_card_pay --会员资料统计表,需要 insert #gcm03(gcc00c,gcc01c, gcc03i,gcc06f) select gcc00c,gcc01c, gcc03i,gcc06f from gcm03 a with(nolock), #mem_result_2 b, gam26 z with(nolock) where a.gcc00c = b.compid and a.gcc01c = b.cardid and a.gcc00c = z.gaz02c and z.gaz01c = @compid insert #sum_gcm03(gcc03i, gcc06f) select gcc03i, sum(gcc06f) from #gcm03 group by gcc03i update a set a.gcc03iname = b.gsb03c from #sum_gcm03 a,gsm02 b with(nolock) where a.gcc03i = b.gsb02c and b.gsb01c = 'O' and b.gsb00c = @compid select isnull(idd,'') idd, isnull(compid,'') compid, isnull(memid,'') memid, isnull(m_name,'') m_name, isnull(cardid,'') cardid, isnull(gender,0) gender, isnull(mobile,'') mobile, isnull(tel,'') tel, isnull(birthday,'') birthday, isnull(state,'') state, isnull(con_amt,0) con_amt,isnull(consjamt,0)consjamt, isnull(lastdate,'') lastdate, isnull(lastcall,'') lastcall, isnull(t_times,0) t_times, isnull(t_compare,0) t_compare, isnull(av_ytimes,0) av_ytimes, isnull(av_times,0) av_times, isnull(av_wtimes,0) av_wtimes, isnull(months,0) months, isnull(av_amt,0) av_amt, isnull(start_date,'') start_date, isnull(first_date,'') first_date, isnull(comp_date,'') comp_date, isnull(dur_years,0) dur_years, isnull(dur_months,0) dur_months, isnull(dur_weeks,0) dur_weeks, isnull(remain01,0) remain01, isnull(remain02,0) remain02, isnull(remain03,0) remain03, isnull(remain04,0) remain04, isnull(remain05,0) remain05, isnull(total_fill_amt,0) total_fill_amt, isnull(memaddr,'') memaddr, isnull(postcode,'') postcode, isnull(principle,'') principle, isnull(total_salecard_amt,0) total_salecard_amt, isnull(regdate,'') regdate from #mem_result_2 union all select 0 idd, 'lm' compid, '' memid, isnull(gcc03iname,'') m_name, '' cardid, 0 gender, '' mobile, '' tel, '' birthday, '' state, isnull(gcc06f,0) con_amt,0 consjamt, '' lastdate, '' lastcall, 0 t_times, 0 t_compare, 0 av_ytimes, 0 av_times, 0 av_wtimes, 0 months, 0 av_amt, '' start_date, '' first_date, '' comp_date, 0 dur_years, 0 dur_months, 0 dur_weeks, 0 remain01, 0 remain02, 0 remain03, 0 remain04, 0 remain05, 0 total_fill_amt, '' memaddr, '' postcode, '' principle, 0 total_salecard_amt, '' regdate from #sum_gcm03 drop table #gcm03 drop table #sum_gcm03 drop table #mem_result_2 end GO /****** Object: Table [dbo].[gfm08] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gfm08]( [gfh00c] [varchar](10) NOT NULL, [gfh01c] [varchar](20) NOT NULL, [gfh02c] [varchar](10) NOT NULL, [gfh03f] [float] NULL, [gfh04d] [varchar](10) NULL, [gfh05t] [varchar](10) NULL, [gfh06f] [float] NULL, [gfh07f] [float] NULL, [gfh08i] [int] NULL, CONSTRAINT [PK_GFM08] PRIMARY KEY CLUSTERED ( [gfh00c] ASC, [gfh01c] ASC, [gfh02c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[wizardrelamodules] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[wizardrelamodules]( [modulegroup] [varchar](10) NULL, [system] [varchar](10) NOT NULL, [wizardprogram] [varchar](10) NOT NULL, [program] [varchar](10) NOT NULL, [caption] [varchar](100) NULL, [moduletype] [int] NOT NULL, [norder] [int] NULL, CONSTRAINT [Pk_wizardrelamodules_id] PRIMARY KEY CLUSTERED ( [system] ASC, [wizardprogram] ASC, [moduletype] ASC, [program] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gbm10] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gbm10]( [gbj00c] [varchar](10) NOT NULL, [gbj01i] [int] IDENTITY(1,1) NOT NULL, [gbj02c] [varchar](20) NULL, [gbj03f] [float] NULL, [gbj04c] [varchar](20) NULL, [gbj05c] [varchar](20) NULL, [gbj06c] [varchar](20) NULL, [gbj07d] [varchar](8) NULL, [gbj08d] [varchar](8) NULL, [gbj09c] [varchar](10) NULL, [gbj10i] [int] NULL, [gbj11c] [varchar](5) NULL, [gbj12c] [nvarchar](500) NULL, [gbj13i] [int] NULL, [gbj14c] [varchar](20) NULL, [gbj15c] [varchar](20) NULL, [gbj16i] [int] NULL, [gbj17c] [varchar](500) NULL, [gbj18c] [varchar](10) NULL, [gbj19c] [varchar](5) NULL, [gbj20c] [varchar](5) NULL, [gbj21c] [varchar](10) NULL, [gbj22c] [varchar](5) NULL, [gbj23c] [varchar](5) NULL, [gbj24c] [varchar](10) NULL, [gbj25c] [varchar](5) NULL, [gbj26c] [varchar](5) NULL, [gbj27c] [varchar](10) NULL, [gbj28c] [varchar](5) NULL, [gbj29c] [varchar](5) NULL, [gbj30c] [varchar](10) NULL, [gbj31c] [varchar](5) NULL, [gbj32c] [varchar](10) NULL, [gbj33c] [varchar](5) NULL, [gbj34c] [varchar](5) NULL, [gbj35c] [varchar](500) NULL, CONSTRAINT [PK_GBM10] PRIMARY KEY CLUSTERED ( [gbj00c] ASC, [gbj01i] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[flagkey] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[flagkey]( [compid] [varchar](20) NOT NULL, [keyid] [bigint] NOT NULL, [iuse] [int] NOT NULL, CONSTRAINT [PK_flagkey] PRIMARY KEY CLUSTERED ( [compid] ASC, [keyid] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_cardinfo_combo_treatment_lm0] Script Date: 06/12/2017 06:19:38 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_cardinfo_combo_treatment_lm0] ( @compid varchar(10), --门店编号 @flag char(2), --0:疗程,1:套餐 @cardnofrom varchar(10), --会员卡号 @cardnoto varchar(20), @combofrom varchar(20), --疗程编号 @comboto varchar(20), @treatmentfrom varchar(20), --套餐编号 @treatmentto varchar(20) ) as begin --begin会员疗程 create table #gcm06 -- 会员卡--疗程 ( gcf00c varchar(10) not null, --门店编号 gcf01c varchar(20) not null, --卡号 gcf01cName varchar(40) null, --会员卡姓名 gcf02c varchar(20) not null, --项目编号 gcf02cName varchar(40) null, --项目名称 mobileno varchar(20) null, --手机号码 gcf03f float null, --次数 gcf04f float null, --赠送次数 gcf05f float null, --总次数 gcf06f float null, --已经使用次数 gcf07f float null, --剩余次数 gcf08f float null, --疗程金额 gcf09f float null, --已经使用金额 gcf10f float null --剩余金额 -- gcf23i int not null --序号 ) -- 插入数据 insert #gcm06 (gcf00c, gcf02c, gcf01c, gcf03f, gcf04f, gcf05f, gcf08f, gcf06f, gcf09f, gcf07f, gcf10f ) select gcf00c, gcf02c, gcf01c, sum(gcf03f), sum(gcf04f), sum(gcf05f), sum(gcf08f), sum(gcf06f), sum(gcf09f), sum(gcf07f), sum(gcf10f) from gcm06 with(nolock) where gcf00c = @compid and ((@cardnofrom = '*') or (gcf01c between @cardnofrom and @cardnoto)) and ((@combofrom = '*') or (gcf02c between @combofrom and @comboto)) and gcf27i = 0 and gcf28i = 0 group by gcf00c, gcf02c, gcf01c -- 更改项目名称 update a set a.gcf02cName = b.gda03c from #gcm06 a, gdm01 b with (nolock) where a.gcf02c = b.gda01c and b.gda00c = @compid --更改手机号码,会员姓名 update a set a.mobileno = b.gba08c, a.gcf01cName = b.gba03c from #gcm06 a, gbm01 b with (nolock) where a.gcf00c = b.gba00c and a.gcf01c = b.gba23c select isnull(gcf02c,'') gcf02c, isnull(gcf02cName,'') gcf02cName, isnull(gcf01c,'') gcf01c, isnull(gcf01cName,'') gcf01cName, isnull(mobileno,'') mobileno, isnull(gcf03f,0) gcf03f, isnull(gcf04f,0) gcf04f, isnull(gcf05f,0) gcf05f, isnull(gcf08f,0) gcf08f, isnull(gcf06f,0) gcf06f, isnull(gcf09f,0) gcf09f, isnull(gcf07f,0) gcf07f, isnull(gcf10f,0) gcf10f --疗程 from #gcm06 order by gcf02c, gcf01c, gcf00c drop table #gcm06 end GO /****** Object: Table [dbo].[lrm02] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[lrm02]( [grb00c] [varchar](10) NOT NULL, [grb01c] [varchar](20) NOT NULL, [grb02f] [float] NOT NULL, [grb03c] [varchar](20) NULL, [grb04f] [float] NULL, [grb05c] [varchar](5) NULL, [grb06f] [float] NULL, [grb07f] [float] NULL, [grb08f] [float] NULL, [grb09i] [int] NULL, [grb10c] [varchar](10) NULL, [grb11f] [float] NULL, [grb12f] [float] NULL, [grb13f] [float] NULL, [grb14f] [float] NULL, [grb15c] [varchar](20) NULL, [grb16f] [float] NULL, [grb17f] [float] NULL, [grb18f] [float] NULL, [grb19f] [float] NULL, [grb20i] [int] NULL, [grb21c] [varchar](20) NULL, [grb22f] [float] NULL, CONSTRAINT [pk_lrm02] PRIMARY KEY CLUSTERED ( [grb00c] ASC, [grb01c] ASC, [grb02f] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_report_gwm01r] Script Date: 06/12/2017 06:20:36 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO Create Proc [dbo].[upg_report_gwm01r] ( @companyid varchar(10), --门店 @fromdate varchar(10), --起始日期 @todate varchar(10), --结束日期 @gda02c varchar(10), --项目大类 @gda01c varchar(10), --项目简称 @rad int, --客户分类 -1不分类,0会员非会员,1男女 ,2 A类,B类,C类,3 叠加项目 @fromcs int, --来店次数 (@rad 选择了 2才有效) @tocs int, --来店次数 @jine float, --平均消费金额(@rad 选择了 2才有效) @chk int --客户明细 -1不展示 0展示明细 ) As begin Create table #kfxhtable ( gga00c varchar(60), --公司编号 gga01c varchar(60), --消费单号 gga05c varchar(60), --会员代码 gga06c varchar(60), --客户姓名 gga07c varchar(60), --会员卡类别 gga13f float, --是否为老客 gga14f float, --客人数量 gga31i int, --顾客性别 gga16f float, --总金额 gda01c varchar(60), --项目代码, gda03c varchar(60), --项目简称, gda04c varchar(60), --项目类别, gda02c varchar(60), --部门 gda02cName varchar(60), --部门名称 ggb12c varchar(60), --大工 ggb13c varchar(60), --中工 ggb14c varchar(60), --小工 ggb06c varchar(10), --价格序号(0标准价,1为体验价,2之后为项目) ggb47i int, --疗程支付的时候的疗程ID gga94d varchar(30), --覆核日期, gga80d varchar(30) --帐务日期 ) insert into #kfxhtable select gga00c, gga01c, gga05c, gga06c, gga07c, gga13f, gga14f, gga31i, ggb11f, --项目金额 gda01c, gda03c, gda04c, gda02c, gda02cName = (select top 1 gsb03c from gsm02 with(nolock) where gsb01c = 'U' and gsb00c = gga00c and gsb02c = gda02c), ggb12c, ggb13c, ggb14c, ggb06c, ggb47i, gga94d, gga80d from ggm01 with(nolock) left join ggm02 with(nolock) on ggm01.gga00c = ggm02.ggb00c And ggm01.gga01c = ggm02.ggb01c inner join gdm01 with(nolock) on gdm01.gda00c = ggm02.ggb00c And gdm01.gda01c = ggm02.ggb03c where (@companyid = '' or gga00c in (select gaz02c from gam26 with(nolock) where gaz01c = @companyid ) ) And gga94d between @fromdate and @todate if(@gda02c = '' And @rad = -1 And @chk = -1) --客户大类消费分析 (大类不选,项目数据从大类来,则项目也选不上,则认为是分析大类消费) begin select gda02c, max(gda02cName) gda02cName, count(gga01c) gga01c, sum(gga16f) gga16f from #kfxhtable group by gda02c union all select '合计:','', sum(gga01c) gga01c, sum(gga16f) gga16f from ( select gda02c, max(gda02cName) gda02cName, count(gga01c) gga01c, sum(gga16f) gga16f from #kfxhtable group by gda02c ) aTable end else if(@gda02c != '' And @rad = -1 And @chk = -1) --客户大类-> 统计分类 消费分析 (选了大类,则认为是分析到项目简称) begin select gda01c, max(gda03c) gda03c, max(gda02cName) gda02cName, count(gga01c) gga01c, sum(gga16f) gga16f from #kfxhtable where gda02c = @gda02c and (gda01c = @gda01c or @gda01c = '') group by gda01c union all select '合计:','','', sum(gga01c) gga01c, sum(gga16f) gga16f from ( select gda01c, max(gda03c) gda03c, max(gda02cName) gda02cName, count(gga01c) gga01c, sum(gga16f) gga16f from #kfxhtable where gda02c = @gda02c and (gda01c = @gda01c or @gda01c = '') group by gda01c ) aTable end else if(@rad != -1 And @chk = -1) begin if(@rad = 0 ) --会员非会员分类 begin select '非会员' fenlei, count(gga01c) gga01c, sum(gga16f) gga16f from #kfxhtable where gga07c = 'Z' and (gda02c = @gda02c or @gda02c = '') and (gda01c = @gda01c or @gda01c = '') union all select '会员' fenlei, count(gga01c) gga01c, sum(gga16f) gga16f from #kfxhtable where gga07c != 'Z' and (gda02c = @gda02c or @gda02c = '') and (gda01c = @gda01c or @gda01c = '') union all select '合计:', count(gga01c) gga01c, sum(gga16f) gga16f from #kfxhtable where (gda02c = @gda02c or @gda02c = '') and (gda01c = @gda01c or @gda01c = '') end else if(@rad = 1) --男女分类 begin select case gga31i when 1 then '男' when 0 then '女' end fenlei, count(gga01c) gga01c, sum(gga16f) gga16f from #kfxhtable where (gda02c = @gda02c or @gda02c = '') And (gda01c = @gda01c or @gda01c = '') group by gga31i union all select '合计:', sum(gga01c) gga01c, sum(gga16f) gga16f from ( select case gga31i when 1 then '男' when 0 then '女' end sex, count(gga01c) gga01c, sum(gga16f) gga16f from #kfxhtable where (gda02c = @gda02c or @gda02c = '') and (gda01c = @gda01c or @gda01c = '') group by gga31i ) aTable end else if( @rad = 2 ) --A,B,C类 begin select gga05c, -- 会员代码, max(gga06c) gga06c, -- 客户姓名, count(gga01c) gga01c, --客单数 sum(gga16f) gga16f, --金额 Convert(decimal(18,2),sum(gga16f)/count(gga01c)) pjxh, --平均消费金额 case max(gga31i) when 1 then '男' when 0 then '女' end gga31i, max(gba17d) gba17d, -- 会员生日, max(gba08c) gba08c, -- 会员电话, max(gba06c) gba06c, -- 微信, max(gba05c) gba05c, -- 地址, '来店' + Convert(varchar(10), @fromcs) + '至' + Convert(varchar(10),@tocs) +'次,平均消费' + Convert(varchar(10),@jine) + '元' leixing from #kfxhtable left join gbm01 with(nolock) on gba00c = gga00c And gba01c = gga05c where gga07c!='Z' and (gda02c = @gda02c or @gda02c = '') and (gda01c = @gda01c or @gda01c = '') group by gga05c having count(gga01c) >= @fromcs and count(gga01c) <= @tocs and sum(gga16f)/count(gga01c) >= @jine union all select '合计:','', sum(gga01c) gga01c, sum(gga16f) gga16f, Convert(decimal(18,2),sum(gga16f)/sum(gga01c)) pjxh, '','','','','','' from ( select gga05c, -- 会员代码, count(gga01c) gga01c, sum(gga16f) gga16f, --金额 sum(gga16f)/count(gga01c) pjxh from #kfxhtable where gga07c!='Z' and (gda02c = @gda02c or @gda02c = '') and (gda01c = @gda01c or @gda01c = '') group by gga05c having count(gga01c) >= @fromcs and count(gga01c) <= @tocs and sum(gga16f)/count(gga01c) >= @jine ) aTable end else if( @rad = 3 ) --叠加项目 begin select gga05c, --会员代码, case max(gga07c) when 'Z' then '非会员' else '会员' end gga07c, --是否会员, max(gga06c) gga06c, --客户代码, case max(gga31i) when 0 then '女' else '男' end gga31i, --顾客性别, count(gga01c) gga01c, -- 叠加数目, sum(gga16f) gga16f --总金额 from #kfxhtable where ggb47i != -1 and (gda02c = @gda02c or @gda02c = '') and (gda01c = @gda01c or @gda01c = '') group by gga05c union all select '合计:','','','', sum(gga01c) gga01c, sum(gga16f) gga16f from ( select gga05c, count(gga01c) gga01c, sum(gga16f) gga16f from #kfxhtable where ggb47i != -1 and (gda02c = @gda02c or @gda02c = '') and (gda01c = @gda01c or @gda01c = '') group by gga05c ) aTable end end else if(@chk = 0) --展示客户明细数据 begin if( @rad = 2 ) --A 类客户消费明细 begin select gda02cName, -- 大类, gda03c, -- 项目简称, '来店' + Convert(varchar(10), @fromcs) + '至' + Convert(varchar(10),@tocs) +'次,平均消费' + Convert(varchar(10),@jine) + '元' gga07c, case gga31i when 1 then '男' when 0 then '女' end gga31i, gga01c, -- 消费单号, gga05c, -- 会员代码, gga06c, -- 客户姓名, gba17d, -- 会员生日, gba08c, -- 会员电话, gba06c, -- 微信, gba05c, -- 地址, gga13f, -- 是否为老客, gga14f, --客人数量 gga16f, --金额 gga94d --日期 from #kfxhtable left join gbm01 with(nolock) on gba00c = gga00c And gba01c = gga05c where (gda02c = @gda02c or @gda02c = '') and (gda01c = @gda01c or @gda01c = '') and gga05c in ( select gga05c from #kfxhtable where gga07c!='Z' and (gda02c = @gda02c or @gda02c = '') and (gda01c = @gda01c or @gda01c = '') group by gga05c having count(gga01c) >= @fromcs and count(gga01c) <= @tocs and sum(gga16f)/count(gga01c) >= @jine ) union all select '合计:', '' gda03c, '' gga07c, '' gga31i, convert(varchar(20),count(gga01c)) gga01c, '' gga05c, '' gga06c, '' gba17d, '' gba08c, '' gba06c, '' gba05c, 0 gga13f, sum(gga14f) gga14f, sum(gga16f) gga16f, '' gga94d from #kfxhtable left join gbm01 with(nolock) on gba00c = gga00c And gba01c = gga05c where (gda02c = @gda02c or @gda02c = '') and (gda01c = @gda01c or @gda01c = '') and gga05c in ( select gga05c from #kfxhtable where gga07c!='Z' and (gda02c = @gda02c or @gda02c = '') and (gda01c = @gda01c or @gda01c = '') group by gga05c having count(gga01c) >= @fromcs and count(gga01c) <= @tocs and sum(gga16f)/count(gga01c) >= @jine ) end else if( @rad = 3) --叠加项目 begin select gga01c, --消费单号, case gga07c when 'z' then '非会员' else '会员' end gga07c, --是否会员, gga05c, --会员代码, gga06c, --客户代码, case gga31i when 0 then '女' else '男' end gga31i ,--顾客性别, gda01c, --项目代码, gga01c, --叠加数目, gba17d, --会员生日, gba08c, --会员电话, gba06c, --微信, gba05c, --地址, gga13f, --是否为老客, gga14f, --客人数量 gga16f, --金额 gga94d --日期 from #kfxhtable left join gbm01 with(nolock) on gba00c = gga00c and gba01c = gga05c where ggb47i != -1 and (gda02c = @gda02c or @gda02c = '') and (gda01c = @gda01c or @gda01c = '') union all select '合计:', '' gga07c, '' gga05c, '' gga06c, '' gga31i, '' gda01c, '' gga01c, '' gba17d, '' gba08c, '' gba06c, '' gba05c, 0 gga13f, sum(gga14f) gga14f, sum(gga16f) gga16f, '' gga94d from #kfxhtable left join gbm01 on gba00c = gga00c and gba01c = gga05c where ggb47i != -1 and (gda02c = @gda02c or @gda02c = '') and (gda01c = @gda01c or @gda01c = '') end else begin select gda02cName, -- 大类, gda03c, -- 项目简称, case gga07c when 'Z' then '非会员' else '会员' end gga07c, case gga31i when 1 then '男' when 0 then '女' end gga31i, gga01c, -- 消费单号, gga05c, -- 会员代码, gga06c, -- 客户姓名, gba17d, -- 会员生日, gba08c, -- 会员电话, gba06c, -- 微信, gba05c, -- 地址, gga13f, -- 是否为老客, gga14f, --客人数量 gga16f, --金额 gga94d --日期 from #kfxhtable left join gbm01 with(nolock) on gba00c = gga00c And gba01c = gga05c where (gda02c = @gda02c or @gda02c = '') and (gda01c = @gda01c or @gda01c = '') union all select '合计:', '' gda03c, '' gga07c, '' gga31i, '' gga01c, '' gga05c, '' gga06c, '' gba17d, '' gba08c, '' gba06c, '' gba05c, 0 gga13f, sum(gga14f) gga14f, sum(gga16f) gga16f, '' gga94d from #kfxhtable left join gbm01 on gba00c = gga00c And gba01c = gga05c where (gda02c = @gda02c or @gda02c = '') and (gda01c = @gda01c or @gda01c = '') end end drop table #kfxhtable end GO /****** Object: Table [dbo].[gbm13] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gbm13]( [gbm00c] [varchar](10) NOT NULL, [gbm01c] [varchar](20) NOT NULL, [gbm02c] [varchar](10) NULL, [gbm03c] [varchar](20) NULL, [gbm04c] [varchar](20) NULL, [gbm05c] [varchar](20) NULL, [gbm06c] [varchar](20) NULL, [gbm07c] [varchar](20) NULL, [gbm08c] [varchar](20) NULL, [gbm09c] [varchar](20) NULL, [gbm10c] [varchar](60) NULL, [gbm11c] [varchar](20) NULL, [gbm12c] [varchar](20) NULL, [gbm13c] [varchar](20) NULL, [gbm14c] [varchar](20) NULL, [gbm15c] [varchar](20) NULL, [gbm16c] [varchar](20) NULL, [gbm17c] [varchar](60) NULL, [gbm18c] [varchar](20) NULL, [gbm19c] [varchar](20) NULL, [gbm20c] [varchar](20) NULL, [gbm21c] [varchar](20) NULL, [gbm22c] [varchar](20) NULL, [gbm23c] [varchar](20) NULL, [gbm24c] [varchar](60) NULL, [gbm25c] [varchar](60) NULL, [gbm26c] [varchar](60) NULL, [gbm27c] [varchar](60) NULL, [gbm28c] [varchar](60) NULL, [gbm29c] [varchar](60) NULL, [gbm30c] [varchar](20) NULL, [gbm31c] [varchar](20) NULL, [gbm32c] [varchar](20) NULL, [gbm33c] [varchar](20) NULL, [gbm34c] [varchar](20) NULL, [gbm35c] [varchar](60) NULL, [gbm36c] [varchar](20) NULL, [gbm37c] [varchar](60) NULL, [gbm38p] [image] NULL, [gbm39p] [image] NULL, [gbm40i] [int] IDENTITY(1,1) NOT NULL, [gbm41c] [varchar](20) NULL, [gbm42c] [varchar](20) NULL, [gbm43c] [varchar](60) NULL, [gbm44c] [varchar](20) NULL, [gbm45c] [varchar](20) NULL, [gbm46c] [varchar](20) NULL, [gbm47c] [varchar](20) NULL, [gbm48c] [varchar](20) NULL, [gbm49c] [varchar](20) NULL, [gbm50c] [varchar](20) NULL, [gbm51c] [varchar](20) NULL, [gbm52c] [varchar](20) NULL, [gbm53c] [varchar](20) NULL, [gbm54c] [varchar](60) NULL, CONSTRAINT [PK_GBM13] PRIMARY KEY CLUSTERED ( [gbm00c] ASC, [gbm01c] ASC, [gbm40i] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[openpolicy_master_del] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[openpolicy_master_del]( [compid] [varchar](20) NOT NULL, [billid] [varchar](20) NOT NULL, [person] [varchar](20) NOT NULL, [datetime] [varchar](20) NULL, [memtype] [int] NULL, [memcard] [varchar](20) NULL, [billflag] [int] NULL, [remark] [varchar](1000) NULL, [keyid] [bigint] NULL, [gender] [int] NULL, [userid] [varchar](20) NULL, [ddate] [varchar](10) NULL, [ttime] [varchar](10) NULL, [idd] [int] IDENTITY(1,1) NOT NULL, CONSTRAINT [PK_OPENPOLICY_MASTER_DEL] PRIMARY KEY CLUSTERED ( [idd] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gsm03_wechatpay] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gsm03_wechatpay]( [gsc00c_wechatpay] [varchar](10) NOT NULL, [gsc01c_wechatpay] [varchar](20) NOT NULL, [gsc02c_wechatpay] [varchar](5) NOT NULL, [gsc03c_wechatpay] [varchar](8) NULL, [gsc04c_wechatpay] [varchar](10) NULL, [gsc05d_wechatpay] [varchar](20) NULL, [gsc06c_wechatpay] [varchar](50) NULL, [gsc07f_wechatpay] [float] NULL, [gsc08i_wechatpay] [int] NULL, [gsc09c_wechatpay] [varchar](2000) NULL, [gsc10c_wechatpay] [varchar](20) NULL, [gsc11c_wechatpay] [varchar](100) NULL, CONSTRAINT [PK_gsm03_wechatpay] PRIMARY KEY NONCLUSTERED ( [gsc00c_wechatpay] ASC, [gsc01c_wechatpay] ASC, [gsc02c_wechatpay] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_punishment] Script Date: 06/12/2017 06:20:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_punishment] ( @compid varchar(10), @fromdate varchar(10), @todate varchar(10), @fromemp varchar(10), @toemp varchar(10), @type varchar(10) ) as begin create table #ham08 ( hah01c varchar(10) not null, -- 员工代码 empname varchar(20) null, -- 员工姓名 hah03d varchar(8) null, -- 日期 hah04c varchar(10) null, -- 罚款类别 itemname varchar(40) null, -- 类别名称 hah05f float null, -- 罚款金额 hah06c varchar(200) null -- 备注 ) insert #ham08 (hah01c, empname, hah03d, hah04c, itemname, hah05f, hah06c) select hah01c, haa02c, hah03d, hah04c, gsb03c, hah05f, hah06c from ham08 a with(nolock), gsm02 b with(nolock), ham01 c with(nolock) where (hah03d between @fromdate and @todate or @fromdate = '*') and (hah01c between @fromemp and @toemp or @fromemp = '*') and a.hah00c = @compid and a.hah00c = b.gsb00c and a.hah00c = c.haa00c --公司别 and a.hah01c = c.haa01c --员工编号 and a.hah04c = b.gsb02c --类别 and b.gsb01c = 'AI' and (b.gsb02c = @type or @type = '*') select isnull(hah01c,'') hah01c, isnull(empname,'') empname, isnull(hah03d,'') hah03d, isnull(hah04c,'') hah04c, isnull(itemname,'') itemname, isnull(hah05f,0) hah05f, isnull(hah06c,'') hah06c from #ham08 order by hah01c, hah03d drop table #ham08 end GO /****** Object: StoredProcedure [dbo].[upg_report_gwm02r] Script Date: 06/12/2017 06:20:36 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO Create Proc [dbo].[upg_report_gwm02r] ( @companyid varchar(10), --门店 @fromdate varchar(10), --起始日期 @todate varchar(10), --结束日期 @fromyg varchar(60), --起始员工 @toyg varchar(60), --结束员工 @gda02c varchar(10), --项目大类 @gda01c varchar(10), --项目简称 @rad int, --业绩分类 -1不分类,0会员非会员,1男女 ,2 体验项目, 3 叠加项目, 4 A类,B类,C类 @fromcs int, --来店次数 (@rad 选择了 4才有效) @tocs int, --来店次数 @jine float, --平均消费金额(@rad 选择了 4才有效) @chk int --明细 -1 不展示 0 展示明细 ) As begin Create table #kfxhtable ( gga00c varchar(60), --公司编号 gga00cName varchar(60), --公司名称 gga01c varchar(60), --消费单号 gga05c varchar(60), --会员代码 gga06c varchar(60), --客户姓名 gga07c varchar(60), --会员卡类别 gga13f float, --是否为老客 gga14f float, --客人数量 gga31i int, --顾客性别 gga16f float, --总金额 gda01c varchar(60), --项目代码, gda03c varchar(60), --项目简称, gda04c varchar(60), --项目类别, gda04Name varchar(60), --项目类名 gda02c varchar(10), --部门 gda02cName varchar(60), --部门名称 ggb12c varchar(60), --大工 ggb13c varchar(60), --中工 ggb14c varchar(60), --小工 ggb26c varchar(60), --四工 ggb36f float, -- 大工实业绩, ggb37f float, -- 中工实业绩, ggb38f float, -- 小工实业绩, ggb39f float, -- 四工实业绩 ggb45i int, --价格序号(0标准价,1为体验价,2之后为项目) ggb47i int, --疗程支付的时候的疗程ID gga94d varchar(30), --覆核日期, gga80d varchar(30) --帐务日期 ) insert into #kfxhtable select gga00c, gga00cName = (select gae02c from gam05 with(nolock) where gae01c = gga00c), gga01c, gga05c, gga06c, gga07c, gga13f, gga14f, gga31i, gga16f, gda01c, gda03c, gda04c, gda04Name = (select gsb03c from gsm02 with(nolock) where gsb01c = 'L' And gsb00c = gdm01.gda00c And gsb02c = gdm01.gda04c), gda02c, gda02cName = (select top 1 gsb03c from gsm02 with(nolock) where gsb01c='U' and gsb00c = gga00c and gsb02c = gda02c), ggb12c, ggb13c, ggb14c, ggb26c, ggb36f, -- 大工实业绩, ggb37f, -- 中工实业绩, ggb38f, -- 小工实业绩, ggb39f, -- 四工实业绩 ggb45i, --价格序号(0标准价,1为体验价,2之后为项目) ggb47i, --疗程ID gga94d, --覆核日期, gga80d --帐务日期 from ggm01 with(nolock) left join ggm02 with(nolock) on ggm01.gga00c = ggm02.ggb00c And ggm01.gga01c = ggm02.ggb01c inner join gdm01 with(nolock) on gdm01.gda00c = ggm02.ggb00c And gdm01.gda01c = ggm02.ggb03c where (gga00c in (select gaz02c from gam26 with(nolock) where gaz01c = @companyid ) Or @companyid = '' ) and gga94d between @fromdate and @todate create table #lsTable ( yg varchar(20), --员工 ygName varchar(20), --员工名称 --add by lm 2015-10-13 gda02c varchar(60), --部门代码 gda02cName varchar(60), --部门名称 gda01c varchar(60), --项目代码 gda03c varchar(60), --项目名称 gga01c int, --客单数 jine float, --业绩 lx varchar(60) --类型 ) if( @gda02c = '' and @rad = -1 and @chk = -1) --员工大类销售业绩 begin insert into #lsTable(yg,gda02c,gda02cName,gga01c,jine,lx) select ggb12c yg, gda02c, max(gda02cName) gda02cName, count(gga01c) gga01c, -- 客单数, sum(ggb36f) jine, -- 业绩 '大类' lx from #kfxhtable where ggb36f!=0 and (@fromyg = '' or (ggb12c >= @fromyg and ggb12c <= @toyg)) group by ggb12c,gda02c union all select ggb13c yg, -- 中工, gda02c, -- 部门代码, max(gda02cName) gda02cName, count(gga01c) gga01c, -- 客单数, sum(ggb37f) jine, -- 业绩 '大类' lx from #kfxhtable where ggb37f!=0 and (@fromyg = '' or (ggb13c >= @fromyg and ggb13c <= @toyg)) group by ggb13c,gda02c union all select ggb14c yg, gda02c, max(gda02cName) gda02cName, count(gga01c) gga01c, -- 客单数, sum(ggb38f) jine, -- 业绩 '大类' lx from #kfxhtable where ggb38f!=0 and (@fromyg = '' or (ggb14c >= @fromyg and ggb14c <= @toyg)) group by ggb14c,gda02c union all select ggb26c yg, gda02c, max(gda02cName) gda02cName, count(gga01c) gga01c, -- 客单数, sum(ggb39f) jine, -- 业绩 '大类' lx from #kfxhtable where ggb39f != 0 and (@fromyg = '' or (ggb26c >= @fromyg and ggb26c <= @toyg)) group by ggb26c,gda02c --更改员工名称 add by lm 2015-10-13 update a set a.ygName = b.haa02c from #lsTable a, ham01 b with(nolock) where a.yg = b.haa01c and b.haa00c = @companyid --员工大类销售业绩 结果 select isnull(yg,'') yg, -- 员工, isnull(ygName,'') ygName, isnull(gda02c,'') gda02c, -- 部门代码, isnull(max(gda02cName),'') gda02cName, isnull(sum(gga01c),0) gga01c, -- 客单数, isnull(sum(jine),0) jine -- 业绩 from #lsTable group by yg, ygName, gda02c union all select '合计:','','','', isnull(sum(gga01c),0) gga01c, isnull(sum(jine),0) jine from #lsTable end else if( @gda02c != '' And @rad = -1 and @chk = -1 ) --项目分类 业绩 begin insert into #lsTable select ggb12c yg, gda02c, max(gda02cName) gda02cName, gda01c, max(gda03c) gda03c, count(gga01c) gga01c, -- 客单数, sum(ggb36f) jine, -- 业绩 '项目分类' lx from #kfxhtable where ggb36f!=0 and (gda02c = @gda02c or @gda02c = '') and (gda01c = @gda01c or @gda01c = '') and (@fromyg = '' or (ggb12c >= @fromyg and ggb12c <= @toyg)) group by ggb12c,gda02c,gda01c union all select ggb13c yg, -- 中工, gda02c, -- 部门代码, max(gda02cName) gda02cName, gda01c, max(gda03c) gda03c, count(gga01c) gga01c, -- 客单数, sum(ggb37f) jine, -- 业绩 '项目分类' lx from #kfxhtable where ggb37f != 0 and (gda02c = @gda02c or @gda02c = '') and (gda01c = @gda01c or @gda01c = '') and (@fromyg = '' or (ggb13c >= @fromyg and ggb13c <= @toyg)) group by ggb13c,gda02c,gda01c union all select ggb14c yg, gda02c, max(gda02cName) gda02cName, gda01c, max(gda03c) gda03c, count(gga01c) gga01c, -- 客单数, sum(ggb38f) jine, -- 业绩 '项目分类' lx from #kfxhtable where ggb38f != 0 and (gda02c = @gda02c or @gda02c = '') and (gda01c = @gda01c or @gda01c = '') and (@fromyg = '' or (ggb14c >= @fromyg and ggb14c <= @toyg)) group by ggb14c,gda02c,gda01c union all select ggb26c yg, gda02c, max(gda02cName) gda02cName, gda01c, max(gda03c) gda03c, count(gga01c) gga01c, -- 客单数, sum(ggb39f) jine, -- 业绩 '项目分类' lx from #kfxhtable where ggb39f != 0 and (gda02c = @gda02c or @gda02c = '') and (gda01c = @gda01c or @gda01c = '') and (@fromyg = '' or (ggb26c >= @fromyg and ggb26c <= @toyg)) group by ggb26c,gda02c,gda01c --更改员工名称 add by lm 2015-10-13 update a set a.ygName = b.haa02c from #lsTable a, ham01 b with(nolock) where a.yg = b.haa01c and b.haa00c = @companyid --员工大类销售业绩 结果 select isnull(yg,'') yg, -- 员工, isnull(ygName,'') ygName, isnull(gda02c,'') gda02c, -- 部门代码, isnull(max(gda02cName),'') gda02cName, isnull(gda01c,'') gda01c, isnull(max(gda03c),'') gda03c, isnull(sum(gga01c),0) gga01c, -- 客单数, isnull(sum(jine),0) jine -- 业绩 from #lsTable group by yg, ygName ,gda02c, gda01c union all select '合计:','','','','','', isnull(sum(gga01c),0) gga01c, isnull(sum(jine),0) jine from #lsTable end else if( @chk = -1 and @rad != -1 ) ---1不分类,0会员非会员,1男女 ,2 体验项目, 3 叠加项目 begin if( @rad = 0) --会员非会员比例业绩 begin insert into #lsTable(yg,gga01c,jine,lx) select ggb12c yg, count(gga01c) gga01c, sum(ggb36f) jine, '会员' lx from #kfxhtable where gga07c != 'Z' and ggb36f != 0 and (gda02c = @gda02c or @gda02c = '') and (gda01c = @gda01c or @gda01c = '') and (@fromyg = '' or (ggb12c >= @fromyg and ggb12c <= @toyg)) group by ggb12c union all select ggb12c yg, count(gga01c) gga01c, sum(ggb36f) jine, '非会员' lx from #kfxhtable where gga07c = 'Z' and ggb36f != 0 and (gda02c = @gda02c or @gda02c = '') and (gda01c = @gda01c or @gda01c = '') and (@fromyg = '' or (ggb12c >= @fromyg and ggb12c <= @toyg)) group by ggb12c union all select ggb13c yg, count(gga01c) gga01c, sum(ggb37f) jine, '会员' lx from #kfxhtable where gga07c != 'Z' and ggb37f != 0 and (gda02c = @gda02c or @gda02c = '') and (gda01c = @gda01c or @gda01c = '') and (@fromyg = '' or (ggb13c >= @fromyg and ggb13c <= @toyg)) group by ggb13c union all select ggb13c yg, count(gga01c) gga01c, sum(ggb37f) jine, '非会员' lx from #kfxhtable where gga07c = 'Z' and ggb37f != 0 and (gda02c = @gda02c or @gda02c = '') and (gda01c = @gda01c or @gda01c = '') and (@fromyg = '' or (ggb13c >= @fromyg and ggb13c <= @toyg)) group by ggb13c union all select ggb14c yg, count(gga01c) gga01c, sum(ggb38f) jine, '会员' lx from #kfxhtable where gga07c != 'Z' and ggb38f != 0 and (gda02c = @gda02c or @gda02c = '') and (gda01c = @gda01c or @gda01c = '') and (@fromyg = '' or (ggb14c >= @fromyg and ggb14c <= @toyg)) group by ggb14c union all select ggb14c yg, count(gga01c) gga01c, sum(ggb38f) jine, '非会员' lx from #kfxhtable where gga07c = 'Z' and ggb38f != 0 and (gda02c = @gda02c or @gda02c = '') and (gda01c = @gda01c or @gda01c = '') and (@fromyg = '' or (ggb14c >= @fromyg and ggb14c <= @toyg)) group by ggb14c union all select ggb26c yg, count(gga01c) gga01c, sum(ggb39f) jine, '会员' lx from #kfxhtable where gga07c != 'Z' and ggb39f != 0 and (gda02c = @gda02c or @gda02c = '') and (gda01c = @gda01c or @gda01c = '') and (@fromyg = '' or (ggb26c >= @fromyg and ggb26c <= @toyg)) group by ggb26c union all select ggb26c yg, count(gga01c) gga01c, sum(ggb39f) jine, '非会员' lx from #kfxhtable where gga07c = 'Z' and ggb39f != 0 and (gda02c = @gda02c or @gda02c = '') and (gda01c = @gda01c or @gda01c = '') and (@fromyg = '' or (ggb26c >= @fromyg and ggb26c <= @toyg)) group by ggb26c --更改员工名称 add by lm 2015-10-13 update a set a.ygName = b.haa02c from #lsTable a, ham01 b with(nolock) where a.yg = b.haa01c and b.haa00c = @companyid --员工会员非会员业绩 结果 select isnull(lx,'') lx, isnull(yg,'') yg, -- 员工, isnull(ygName,'') ygName, isnull(sum(gga01c),0) gga01c, -- 客单数, isnull(sum(jine),0) jine -- 业绩 from #lsTable group by yg, ygName, lx union all select '合计:','','', isnull(sum(gga01c),0) gga01c, -- 客单数, isnull(sum(jine),0) jine from #lsTable end else if( @rad = 1 ) --客户性别销售业绩分析 begin insert into #lsTable(yg,gga01c,jine,lx) select ggb12c yg, count(gga01c) gga01c, sum(ggb36f) jine, case gga31i when 0 then '男' when 1 then '女' end lx from #kfxhtable where ggb36f != 0 and (gda02c = @gda02c or @gda02c = '') and (gda01c = @gda01c or @gda01c = '') and (@fromyg = '' or (ggb12c >= @fromyg and ggb12c <= @toyg)) group by ggb12c,gga31i union all select ggb13c yg, count(gga01c) gga01c, sum(ggb37f) jine, case gga31i when 0 then '男' when 1 then '女' end lx from #kfxhtable where ggb37f != 0 and (gda02c = @gda02c or @gda02c = '') and (gda01c = @gda01c or @gda01c = '') and (@fromyg = '' or (ggb13c >= @fromyg and ggb13c <= @toyg)) group by ggb13c,gga31i union all select ggb14c yg, count(gga01c) gga01c, sum(ggb38f) jine, case gga31i when 0 then '男' when 1 then '女' end lx from #kfxhtable where ggb38f != 0 and (gda02c = @gda02c or @gda02c = '') and (gda01c = @gda01c or @gda01c = '') and (@fromyg = '' or (ggb14c >= @fromyg and ggb14c <= @toyg)) group by ggb14c,gga31i union all select ggb26c yg, count(gga01c) gga01c, sum(ggb39f) jine, case gga31i when 0 then '男' when 1 then '女' end lx from #kfxhtable where ggb39f != 0 and (gda02c = @gda02c or @gda02c = '') and (gda01c = @gda01c or @gda01c = '') and (@fromyg = '' or (ggb26c >= @fromyg and ggb26c <= @toyg)) group by ggb26c,gga31i --更改员工名称 add by lm 2015-10-13 update a set a.ygName = b.haa02c from #lsTable a, ham01 b with(nolock) where a.yg = b.haa01c and b.haa00c = @companyid --员工 客户性别销售业绩 结果 select isnull(lx,'') lx, isnull(yg,'') yg, -- 员工, isnull(ygName,'') ygName, isnull(sum(gga01c),0) gga01c, -- 客单数, isnull(sum(jine),0) jine -- 业绩 from #lsTable group by yg, ygName, lx union all select '合计:','','', isnull(sum(gga01c),0) gga01c, -- 客单数, isnull(sum(jine),0) jine from #lsTable end else if( @rad = 2) --体验项目业绩 ggb06c, --价格序号(0标准价,1为体验价,2之后为项目) begin insert into #lsTable(yg,gga01c,jine,lx) select ggb12c yg, count(gga01c) gga01c, sum(ggb36f) jine, '体验项目' lx from #kfxhtable where ggb36f != 0 and ggb45i = 1 and (gda02c = @gda02c or @gda02c = '') and (gda01c = @gda01c or @gda01c = '') and (@fromyg = '' or (ggb12c >= @fromyg and ggb12c <= @toyg)) group by ggb12c union all select ggb13c yg, count(gga01c) gga01c, sum(ggb37f) jine, '体验项目' lx from #kfxhtable where ggb37f != 0 and ggb45i = 1 and (gda02c = @gda02c or @gda02c = '') and (gda01c = @gda01c or @gda01c = '') and (@fromyg = '' or (ggb12c >= @fromyg and ggb12c <= @toyg)) group by ggb13c union all select ggb14c yg, count(gga01c) gga01c, sum(ggb38f) jine, '体验项目' lx from #kfxhtable where ggb38f != 0 and ggb45i = 1 and (gda02c = @gda02c or @gda02c = '') and (gda01c = @gda01c or @gda01c = '') and (@fromyg = '' or (ggb14c >= @fromyg and ggb14c <= @toyg)) group by ggb14c union all select ggb26c yg, count(gga01c) gga01c, sum(ggb39f) jine, '体验项目' lx from #kfxhtable where ggb39f != 0 and ggb45i = 1 and (gda02c = @gda02c or @gda02c = '') and (gda01c = @gda01c or @gda01c = '') and (@fromyg = '' or (ggb26c >= @fromyg and ggb26c <= @toyg)) group by ggb26c --更改员工名称 add by lm 2015-10-13 update a set a.ygName = b.haa02c from #lsTable a, ham01 b with(nolock) where a.yg = b.haa01c and b.haa00c = @companyid --体验项目业绩 结果 select isnull(lx,'') lx, isnull(yg,'') yg, -- 员工, isnull(ygName,'') ygName, isnull(sum(gga01c),0) gga01c, -- 客单数, isnull(sum(jine),0) jine -- 业绩 from #lsTable group by yg, ygName, lx union all select '合计:','','', isnull(sum(gga01c),0) gga01c, -- 客单数, isnull(sum(jine),0) jine from #lsTable end else if( @rad = 3 ) -- 叠加项目 begin insert into #lsTable(yg,gga01c,jine,lx) select ggb12c yg, count(gga01c) gga01c, sum(ggb36f) jine, '叠加项目' lx from #kfxhtable where ggb47i != -1 and ggb36f != 0 and (gda02c = @gda02c or @gda02c = '') and (gda01c = @gda01c or @gda01c = '') and (@fromyg = '' or (ggb12c >= @fromyg and ggb12c <= @toyg)) group by ggb12c union all select ggb13c yg, count(gga01c) gga01c, sum(ggb37f) jine, '叠加项目' lx from #kfxhtable where ggb47i != -1 and ggb37f != 0 and (gda02c = @gda02c or @gda02c = '') and (gda01c = @gda01c or @gda01c = '') and (@fromyg = '' or (ggb13c >= @fromyg and ggb13c <= @toyg)) group by ggb13c union all select ggb14c yg, count(gga01c) gga01c, sum(ggb38f) jine, '叠加项目' lx from #kfxhtable where ggb47i != -1 and ggb38f != 0 and (gda02c = @gda02c or @gda02c = '') and (gda01c = @gda01c or @gda01c = '') and (@fromyg = '' or (ggb14c >= @fromyg and ggb14c <= @toyg)) group by ggb14c union all select ggb26c yg, count(gga01c) gga01c, sum(ggb39f) jine, '叠加项目' lx from #kfxhtable where ggb47i != -1 and ggb39f != 0 and (gda02c = @gda02c or @gda02c = '') and (gda01c = @gda01c or @gda01c = '') and (@fromyg = '' or (ggb26c >= @fromyg and ggb26c <= @toyg)) group by ggb26c --更改员工名称 add by lm 2015-10-13 update a set a.ygName = b.haa02c from #lsTable a, ham01 b with(nolock) where a.yg = b.haa01c and b.haa00c = @companyid --叠加项目 结果 select isnull(lx,'') lx, isnull(yg,'') yg, -- 员工, isnull(ygName,'') ygName, isnull(sum(gga01c),0) gga01c, -- 客单数, isnull(sum(jine),0) jine -- 业绩 from #lsTable group by yg, ygName, lx union all select '合计:','','', isnull(sum(gga01c),0) gga01c, -- 客单数, isnull(sum(jine),0) jine from #lsTable end else if( @rad = 4 ) -- A类,B 类,C 类 begin insert into #lsTable(yg,gga01c,jine,lx) select ggb12c yg, count(gga01c) gga01c, sum(ggb36f) jine, '来店' + Convert(varchar(10), @fromcs) + '至' + Convert(varchar(10),@tocs) + '次,平均消费' + Convert(varchar(10),@jine) + '元' lx from #kfxhtable where ggb36f != 0 and (gda02c = @gda02c or @gda02c = '') and (gda01c = @gda01c or @gda01c = '') and (@fromyg = '' or (ggb12c >= @fromyg and ggb12c <= @toyg)) and gga05c in ( select gga05c from #kfxhtable where gga07c != 'Z' group by gga05c having count(gga01c) >= @fromcs and count(gga01c) <= @tocs and sum(gga16f)/count(gga01c)> @jine ) group by ggb12c union all select ggb13c yg, count(gga01c) gga01c, sum(ggb37f) jine, '来店' + Convert(varchar(10), @fromcs) + '至' + Convert(varchar(10),@tocs) + '次,平均消费' + Convert(varchar(10),@jine) + '元' lx from #kfxhtable where ggb37f != 0 and (gda02c = @gda02c or @gda02c = '') and (gda01c = @gda01c or @gda01c = '') and (@fromyg = '' or (ggb13c >= @fromyg and ggb13c <= @toyg)) and gga05c in ( select gga05c from #kfxhtable where gga07c != 'Z' group by gga05c having count(gga01c) >= @fromcs and count(gga01c) <= @tocs and sum(gga16f)/count(gga01c)> @jine ) group by ggb13c union all select ggb14c yg, count(gga01c) gga01c, sum(ggb38f) jine, '来店' + Convert(varchar(10), @fromcs) + '至' + Convert(varchar(10),@tocs) + '次,平均消费' + Convert(varchar(10),@jine) + '元' lx from #kfxhtable where ggb38f != 0 and (gda02c = @gda02c or @gda02c = '') and (gda01c = @gda01c or @gda01c = '') and (@fromyg = '' or (ggb14c >= @fromyg and ggb14c <= @toyg)) and gga05c in ( select gga05c from #kfxhtable where gga07c != 'Z' group by gga05c having count(gga01c) >= @fromcs and count(gga01c) <= @tocs and sum(gga16f)/count(gga01c)> @jine ) group by ggb14c union all select ggb26c yg, count(gga01c) gga01c, sum(ggb39f) jine, '来店' + Convert(varchar(10), @fromcs) + '至' + Convert(varchar(10),@tocs) + '次,平均消费' + Convert(varchar(10),@jine) + '元' lx from #kfxhtable where ggb39f != 0 and (gda02c = @gda02c or @gda02c = '') and (gda01c = @gda01c or @gda01c = '') and (@fromyg = '' or (ggb26c >= @fromyg and ggb26c <= @toyg)) and gga05c in ( select gga05c from #kfxhtable where gga07c != 'Z' group by gga05c having count(gga01c) >= @fromcs and count(gga01c) <= @tocs and sum(gga16f)/count(gga01c)> @jine ) group by ggb26c --更改员工名称 add by lm 2015-10-13 update a set a.ygName = b.haa02c from #lsTable a, ham01 b with(nolock) where a.yg = b.haa01c and b.haa00c = @companyid --A类,B 类,C 类 结果 select isnull(lx,'') lx, isnull(yg,'') yg, -- 员工, isnull(ygName,'') ygName, isnull(sum(gga01c),0) gga01c, -- 客单数, isnull(sum(jine),0) jine -- 业绩 from #lsTable group by yg, ygName, lx union all select '合计:','','', isnull(sum(gga01c),0) gga01c, -- 客单数, isnull(sum(jine),0) jine from #lsTable end end else if( @chk = 0 ) begin if( @rad = 2 ) --体验项目 begin select isnull(ggb12c,'') ggb12c, --员工 isnull(gda02c,'') gda02c, --部门代码 isnull(gda02cName,'') gda02cName, --部门名称 isnull(gga01c,'') gga01c, --消费单号, isnull(gga05c,'') gga05c, --会员代码 isnull((case gga07c when 'Z' then '非会员' else '会员' end),'') gga07c, --会员卡类别 isnull(gga06c,'') gga06c, --客户姓名 isnull(gga14f,0) gga14f, --客人数量 isnull((case gga31i when 0 then '男' when 1 then '女' end),'') gga31i, --顾客性别 isnull(gga13f,'') gga13f, --是否为老客 isnull(gba17d,'') gba17d, -- 会员生日, isnull(gba08c,'') gba08c, -- 会员电话, isnull(gba06c,'') gba06c, -- 微信, isnull(gba05c,'') gba05c, -- 地址, isnull(gga16f,0) gga16f, --消费金额 isnull(gda01c,'') gda01c, --项目代码 isnull(gda03c,'') gda03c, --项目简称 isnull(gda04c,'') gda04c, --项目类别 isnull(gda04Name,'') gda04Name, --项目类名 isnull(ggb45i,'') ggb45i, --价格序号(0标准价,1为体验价,2之后为项目) isnull(ggb36f,'') ggb36f --业绩 from #kfxhtable left join gbm01 with(nolock) on gba00c = gga00c and gba01c = gga05c where ggb36f != 0 and ggb45i = 1 and (gda02c = @gda02c or @gda02c = '') and (gda01c = @gda01c or @gda01c = '') and (@fromyg = '' or (ggb12c >= @fromyg and ggb12c <= @toyg)) union all select isnull(ggb13c,'') ggb13c, --中工 isnull(gda02c,'') gda02c, --部门代码 isnull(gda02cName,'') gda02cName, --部门名称 isnull(gga01c,'') gga01c, --消费单号, isnull(gga05c,'') gga05c, --会员代码 isnull((case gga07c when 'Z' then '非会员' else '会员' end),'') gga07c, --会员卡类别 isnull(gga06c,'') gga06c, --客户姓名 isnull(gga14f,0) gga14f, --客人数量 isnull((case gga31i when 0 then '男' when 1 then '女' end),'') gga31i, --顾客性别 isnull(gga13f,'') gga13f, --是否为老客 isnull(gba17d,'') gba17d, -- 会员生日, isnull(gba08c,'') gba08c, -- 会员电话, isnull(gba06c,'') gba06c, -- 微信, isnull(gba05c,'') gba05c, -- 地址, isnull(gga16f,0) gga16f, --消费金额 isnull(gda01c,'') gda01c, --项目代码 isnull(gda03c,'') gda03c, --项目简称 isnull(gda04c,'') gda04c, --项目类别 isnull(gda04Name,'') gda04Name, --项目类名 isnull(ggb45i,'') ggb45i, --价格序号(0标准价,1为体验价,2之后为项目) isnull(ggb37f,'') ggb37f --业绩 from #kfxhtable left join gbm01 with(nolock) on gba00c = gga00c and gba01c = gga05c where ggb37f != 0 and ggb45i = 1 and (gda02c = @gda02c or @gda02c = '') and (gda01c = @gda01c or @gda01c = '') and (@fromyg = '' or (ggb13c >= @fromyg and ggb13c <= @toyg)) union all select isnull(ggb14c,'') ggb14c, isnull(gda02c,'') gda02c, --部门代码 isnull(gda02cName,'') gda02cName, --部门名称 isnull(gga01c,'') gga01c, --消费单号, isnull(gga05c,'') gga05c, --会员代码 isnull((case gga07c when 'Z' then '非会员' else '会员' end),'') gga07c, --会员卡类别 isnull(gga06c,'') gga06c, --客户姓名 isnull(gga14f,0) gga14f, --客人数量 isnull((case gga31i when 0 then '男' when 1 then '女' end),'') gga31i, --顾客性别 isnull(gga13f,'') gga13f, --是否为老客 isnull(gba17d,'') gba17d, -- 会员生日, isnull(gba08c,'') gba08c, -- 会员电话, isnull(gba06c,'') gba06c, -- 微信, isnull(gba05c,'') gba05c, -- 地址, isnull(gga16f,0) gga16f, --消费金额 isnull(gda01c,'') gda01c, --项目代码 isnull(gda03c,'') gda03c, --项目简称 isnull(gda04c,'') gda04c, --项目类别 isnull(gda04Name,'') gda04Name, --项目类名 isnull(ggb45i,'') ggb45i, --价格序号(0标准价,1为体验价,2之后为项目) isnull(ggb38f,'') ggb38f --业绩 from #kfxhtable left join gbm01 with(nolock) on gba00c = gga00c and gba01c = gga05c where ggb38f != 0 and ggb45i = 1 and (gda02c = @gda02c or @gda02c = '') and (gda01c = @gda01c or @gda01c = '') and (@fromyg = '' or (ggb14c >= @fromyg and ggb14c <= @toyg)) union all select isnull(ggb26c,'') ggb26c, isnull(gda02c,'') gda02c, --部门代码 isnull(gda02cName,'') gda02cName, --部门名称 isnull(gga01c,'') gga01c, --消费单号, isnull(gga05c,'') gga05c, --会员代码 isnull((case gga07c when 'Z' then '非会员' else '会员' end),'') gga07c, --会员卡类别 isnull(gga06c,'') gga06c, --客户姓名 isnull(gga14f,0) gga14f, --客人数量 isnull((case gga31i when 0 then '男' when 1 then '女' end),'') gga31i, --顾客性别 isnull(gga13f,'') gga13f, --是否为老客 isnull(gba17d,'') gba17d, -- 会员生日, isnull(gba08c,'') gba08c, -- 会员电话, isnull(gba06c,'') gba06c, -- 微信, isnull(gba05c,'') gba05c, -- 地址, isnull(gga16f,0) gga16f, --消费金额 isnull(gda01c,'') gda01c, --项目代码 isnull(gda03c,'') gda03c, --项目简称 isnull(gda04c,'') gda04c, --项目类别 isnull(gda04Name,'') gda04Name, --项目类名 isnull(ggb45i,'') ggb45i, --价格序号(0标准价,1为体验价,2之后为项目) isnull(ggb39f,'') ggb39f --业绩 from #kfxhtable left join gbm01 with(nolock) on gba00c = gga00c and gba01c = gga05c where ggb39f != 0 and ggb45i = 1 and (gda02c = @gda02c or @gda02c = '') and (gda01c = @gda01c or @gda01c = '') and (@fromyg = '' or (ggb26c >= @fromyg and ggb26c <= @toyg)) end --end 体验项目 else if( @rad = 3 ) --叠加项目 begin select isnull(ggb12c,'') ggb12c, --员工 isnull(gda02c,'') gda02c, --部门代码 isnull(gda02cName,'') gda02cName, --部门名称 isnull(gga01c,'') gga01c, --消费单号, isnull(gga05c,'') gga05c, --会员代码 isnull((case gga07c when 'Z' then '非会员' else '会员' end),'') gga07c, --会员卡类别 isnull(gga06c,'') gga06c, --客户姓名 isnull(gga14f,0) gga14f, --客人数量 isnull((case gga31i when 0 then '男' when 1 then '女' end),'') gga31i, --顾客性别 isnull(gga13f,'') gga13f, --是否为老客 isnull(gba17d,'') gba17d, -- 会员生日, isnull(gba08c,'') gba08c, -- 会员电话, isnull(gba06c,'') gba06c, -- 微信, isnull(gba05c,'') gba05c, -- 地址, isnull(gga16f,0) gga16f, --消费金额 isnull(gda01c,'') gda01c, --项目代码 isnull(gda03c,'') gda03c, --项目简称 isnull(gda04c,'') gda04c, --项目类别 isnull(gda04Name,'') gda04Name, --项目类名 isnull(ggb45i,'') ggb45i, --价格序号(0标准价,1为体验价,2之后为项目) isnull(ggb36f,'') ggb36f --业绩 from #kfxhtable left join gbm01 with(nolock) on gba00c = gga00c and gba01c = gga05c where ggb36f != 0 and ggb47i != -1 and (gda02c = @gda02c or @gda02c = '') and (gda01c = @gda01c or @gda01c = '') and (@fromyg = '' or (ggb12c >= @fromyg and ggb12c <= @toyg)) union all select isnull(ggb13c,'') ggb13c, --中工 isnull(gda02c,'') gda02c, --部门代码 isnull(gda02cName,'') gda02cName, --部门名称 isnull(gga01c,'') gga01c, --消费单号, isnull(gga05c,'') gga05c, --会员代码 isnull((case gga07c when 'Z' then '非会员' else '会员' end),'') gga07c, --会员卡类别 isnull(gga06c,'') gga06c, --客户姓名 isnull(gga14f,0) gga14f, --客人数量 isnull((case gga31i when 0 then '男' when 1 then '女' end),'') gga31i, --顾客性别 isnull(gga13f,'') gga13f, --是否为老客 isnull(gba17d,'') gba17d, -- 会员生日, isnull(gba08c,'') gba08c, -- 会员电话, isnull(gba06c,'') gba06c, -- 微信, isnull(gba05c,'') gba05c, -- 地址, isnull(gga16f,0) gga16f, --消费金额 isnull(gda01c,'') gda01c, --项目代码 isnull(gda03c,'') gda03c, --项目简称 isnull(gda04c,'') gda04c, --项目类别 isnull(gda04Name,'') gda04Name, --项目类名 isnull(ggb45i,'') ggb45i, --价格序号(0标准价,1为体验价,2之后为项目) isnull(ggb37f,'') ggb37f --业绩 from #kfxhtable left join gbm01 with(nolock) on gba00c = gga00c and gba01c = gga05c where ggb37f != 0 and ggb47i != -1 and (gda02c = @gda02c or @gda02c = '') and (gda01c = @gda01c or @gda01c = '') and (@fromyg = '' or (ggb13c >= @fromyg and ggb13c <= @toyg)) union all select isnull(ggb14c,'') ggb14c, isnull(gda02c,'') gda02c, --部门代码 isnull(gda02cName,'') gda02cName, --部门名称 isnull(gga01c,'') gga01c, --消费单号, isnull(gga05c,'') gga05c, --会员代码 isnull((case gga07c when 'Z' then '非会员' else '会员' end),'') gga07c, --会员卡类别 isnull(gga06c,'') gga06c, --客户姓名 isnull(gga14f,0) gga14f, --客人数量 isnull((case gga31i when 0 then '男' when 1 then '女' end),'') gga31i, --顾客性别 isnull(gga13f,'') gga13f, --是否为老客 isnull(gba17d,'') gba17d, -- 会员生日, isnull(gba08c,'') gba08c, -- 会员电话, isnull(gba06c,'') gba06c, -- 微信, isnull(gba05c,'') gba05c, -- 地址, isnull(gga16f,0) gga16f, --消费金额 isnull(gda01c,'') gda01c, --项目代码 isnull(gda03c,'') gda03c, --项目简称 isnull(gda04c,'') gda04c, --项目类别 isnull(gda04Name,'') gda04Name, --项目类名 isnull(ggb45i,'') ggb45i, --价格序号(0标准价,1为体验价,2之后为项目) isnull(ggb38f,'') ggb38f --业绩 from #kfxhtable left join gbm01 with(nolock) on gba00c = gga00c and gba01c = gga05c where ggb38f != 0 and ggb47i != -1 and (gda02c = @gda02c or @gda02c = '') and (gda01c = @gda01c or @gda01c = '') and (@fromyg = '' or (ggb14c >= @fromyg and ggb14c <= @toyg)) union all select isnull(ggb26c,'') ggb26c, isnull(gda02c,'') gda02c, --部门代码 isnull(gda02cName,'') gda02cName, --部门名称 isnull(gga01c,'') gga01c, --消费单号, isnull(gga05c,'') gga05c, --会员代码 isnull((case gga07c when 'Z' then '非会员' else '会员' end),'') gga07c, --会员卡类别 isnull(gga06c,'') gga06c, --客户姓名 isnull(gga14f,0) gga14f, --客人数量 isnull((case gga31i when 0 then '男' when 1 then '女' end),'') gga31i, --顾客性别 isnull(gga13f,'') gga13f, --是否为老客 isnull(gba17d,'') gba17d, -- 会员生日, isnull(gba08c,'') gba08c, -- 会员电话, isnull(gba06c,'') gba06c, -- 微信, isnull(gba05c,'') gba05c, -- 地址, isnull(gga16f,0) gga16f, --消费金额 isnull(gda01c,'') gda01c, --项目代码 isnull(gda03c,'') gda03c, --项目简称 isnull(gda04c,'') gda04c, --项目类别 isnull(gda04Name,'') gda04Name, --项目类名 isnull(ggb45i,'') ggb45i, --价格序号(0标准价,1为体验价,2之后为项目) isnull(ggb39f,'') ggb39f --业绩 from #kfxhtable left join gbm01 with(nolock) on gba00c = gga00c and gba01c = gga05c where ggb39f != 0 and ggb47i != -1 and (gda02c = @gda02c or @gda02c = '') and (gda01c = @gda01c or @gda01c = '') and (@fromyg = '' or (ggb26c >= @fromyg and ggb26c <= @toyg)) end --end 叠加项目 else if( @rad = 4 ) -- A类,B类,C类 begin select isnull(ggb12c,'') ggb12c, --员工 isnull(gda02c,'') gda02c, --部门代码 isnull(gda02cName,'') gda02cName, --部门名称 isnull(gga01c,'') gga01c, --消费单号, isnull(gga05c,'') gga05c, --会员代码 '来店' + Convert(varchar(10), @fromcs) + '至' + Convert(varchar(10),@tocs) + '次,平均消费' + Convert(varchar(10),@jine) + '元' gga07c, --类别 isnull(gga06c,'') gga06c, --客户姓名 isnull(gga14f,0) gga14f, --客人数量 isnull((case gga31i when 0 then '男' when 1 then '女' end),'') gga31i, --顾客性别 isnull(gga13f,'') gga13f, --是否为老客 isnull(gba17d,'') gba17d, -- 会员生日, isnull(gba08c,'') gba08c, -- 会员电话, isnull(gba06c,'') gba06c, -- 微信, isnull(gba05c,'') gba05c, -- 地址, isnull(gga16f,0) gga16f, --消费金额 isnull(gda01c,'') gda01c, --项目代码 isnull(gda03c,'') gda03c, --项目简称 isnull(gda04c,'') gda04c, --项目类别 isnull(gda04Name,'') gda04Name, --项目类名 isnull(ggb45i,'') ggb45i, --价格序号(0标准价,1为体验价,2之后为项目) isnull(ggb36f,'') ggb36f --业绩 from #kfxhtable left join gbm01 with(nolock) on gba00c = gga00c and gba01c = gga05c where ggb36f != 0 and (gda02c = @gda02c or @gda02c = '') and (gda01c = @gda01c or @gda01c = '') and (@fromyg = '' or (ggb12c >= @fromyg and ggb12c <= @toyg)) and gga05c in ( select gga05c from #kfxhtable where gga07c != 'Z' group by gga05c having count(gga01c) >= @fromcs and count(gga01c) <= @tocs and sum(gga16f)/count(gga01c)> @jine ) union all select isnull(ggb13c,'') ggb13c, isnull(gda02c,'') gda02c, --部门代码 isnull(gda02cName,'') gda02cName, --部门名称 isnull(gga01c,'') gga01c, --消费单号, isnull(gga05c,'') gga05c, --会员代码 '来店' + Convert(varchar(10), @fromcs) + '至' + Convert(varchar(10),@tocs) + '次,平均消费' + Convert(varchar(10),@jine) + '元' gga07c, --类别 isnull(gga06c,'') gga06c, --客户姓名 isnull(gga14f,0) gga14f, --客人数量 isnull((case gga31i when 0 then '男' when 1 then '女' end),'') gga31i, --顾客性别 isnull(gga13f,'') gga13f, --是否为老客 isnull(gba17d,'') gba17d, -- 会员生日, isnull(gba08c,'') gba08c, -- 会员电话, isnull(gba06c,'') gba06c, -- 微信, isnull(gba05c,'') gba05c, -- 地址, isnull(gga16f,0) gga16f, --消费金额 isnull(gda01c,'') gda01c, --项目代码 isnull(gda03c,'') gda03c, --项目简称 isnull(gda04c,'') gda04c, --项目类别 isnull(gda04Name,'') gda04Name, --项目类名 isnull(ggb45i,'') ggb45i, --价格序号(0标准价,1为体验价,2之后为项目) isnull(ggb37f,'') ggb37f --业绩 from #kfxhtable left join gbm01 with(nolock) on gba00c = gga00c and gba01c = gga05c where ggb37f != 0 and (gda02c = @gda02c or @gda02c = '') and (gda01c = @gda01c or @gda01c = '') and (@fromyg = '' or (ggb13c >= @fromyg and ggb13c <= @toyg)) and gga05c in ( select gga05c from #kfxhtable where gga07c!='Z' group by gga05c having count(gga01c) >= @fromcs and count(gga01c) <= @tocs and sum(gga16f)/count(gga01c)> @jine ) union all select isnull(ggb14c,'') ggb14c, isnull(gda02c,'') gda02c, --部门代码 isnull(gda02cName,'') gda02cName, --部门名称 isnull(gga01c,'') gga01c, --消费单号, isnull(gga05c,'') gga05c, --会员代码 '来店' + Convert(varchar(10), @fromcs) + '至' + Convert(varchar(10),@tocs) + '次,平均消费' + Convert(varchar(10),@jine) + '元' gga07c, --类别 isnull(gga06c,'') gga06c, --客户姓名 isnull(gga14f,0) gga14f, --客人数量 isnull((case gga31i when 0 then '男' when 1 then '女' end),'') gga31i, --顾客性别 isnull(gga13f,'') gga13f, --是否为老客 isnull(gba17d,'') gba17d, -- 会员生日, isnull(gba08c,'') gba08c, -- 会员电话, isnull(gba06c,'') gba06c, -- 微信, isnull(gba05c,'') gba05c, -- 地址, isnull(gga16f,0) gga16f, --消费金额 isnull(gda01c,'') gda01c, --项目代码 isnull(gda03c,'') gda03c, --项目简称 isnull(gda04c,'') gda04c, --项目类别 isnull(gda04Name,'') gda04Name, --项目类名 isnull(ggb45i,'') ggb45i, --价格序号(0标准价,1为体验价,2之后为项目) isnull(ggb38f,'') ggb38f --业绩 from #kfxhtable left join gbm01 with(nolock) on gba00c = gga00c and gba01c = gga05c where ggb38f != 0 and (gda02c = @gda02c or @gda02c = '') and (gda01c = @gda01c or @gda01c = '') and (@fromyg = '' or (ggb14c >= @fromyg and ggb14c <= @toyg)) and gga05c in ( select gga05c from #kfxhtable where gga07c!='Z' group by gga05c having count(gga01c) >= @fromcs and count(gga01c) <= @tocs and sum(gga16f)/count(gga01c)> @jine ) union all select isnull(ggb26c,'') ggb26c, isnull(gda02c,'') gda02c, --部门代码 isnull(gda02cName,'') gda02cName, --部门名称 isnull(gga01c,'') gga01c, --消费单号, isnull(gga05c,'') gga05c, --会员代码 '来店' + Convert(varchar(10), @fromcs) + '至' + Convert(varchar(10),@tocs) + '次,平均消费' + Convert(varchar(10),@jine) + '元' gga07c, --类别 isnull(gga06c,'') gga06c, --客户姓名 isnull(gga14f,0) gga14f, --客人数量 isnull((case gga31i when 0 then '男' when 1 then '女' end),'') gga31i, --顾客性别 isnull(gga13f,'') gga13f, --是否为老客 isnull(gba17d,'') gba17d, -- 会员生日, isnull(gba08c,'') gba08c, -- 会员电话, isnull(gba06c,'') gba06c, -- 微信, isnull(gba05c,'') gba05c, -- 地址, isnull(gga16f,0) gga16f, --消费金额 isnull(gda01c,'') gda01c, --项目代码 isnull(gda03c,'') gda03c, --项目简称 isnull(gda04c,'') gda04c, --项目类别 isnull(gda04Name,'') gda04Name, --项目类名 isnull(ggb45i,'') ggb45i, --价格序号(0标准价,1为体验价,2之后为项目) isnull(ggb39f,'') ggb39f --业绩 from #kfxhtable left join gbm01 with(nolock) on gba00c = gga00c and gba01c = gga05c where ggb39f != 0 and (gda02c = @gda02c or @gda02c = '') and (gda01c = @gda01c or @gda01c = '') and (@fromyg = '' or (ggb26c >= @fromyg and ggb26c <= @toyg)) and gga05c in ( select gga05c from #kfxhtable where gga07c!='Z' group by gga05c having count(gga01c) >= @fromcs and count(gga01c) <= @tocs and sum(gga16f)/count(gga01c)> @jine ) end --end A类,B类,C类 else begin --其他begin select isnull(ggb12c,'') ggb12c, --员工 isnull(gda02c,'') gda02c, --部门代码 isnull(gda02cName,'') gda02cName, --部门名称 isnull(gga01c,'') gga01c, --消费单号, isnull(gga05c,'') gga05c, --会员代码 isnull((case gga07c when 'Z' then '非会员' else '会员' end),'') gga07c, --会员卡类别 isnull(gga06c,'') gga06c, --客户姓名 isnull(gga14f,0) gga14f, --客人数量 isnull((case gga31i when 0 then '男' when 1 then '女' end),'') gga31i, --顾客性别 isnull(gga13f,'') gga13f, --是否为老客 isnull(gba17d,'') gba17d, -- 会员生日, isnull(gba08c,'') gba08c, -- 会员电话, isnull(gba06c,'') gba06c, -- 微信, isnull(gba05c,'') gba05c, -- 地址, isnull(gga16f,0) gga16f, --消费金额 isnull(gda01c,'') gda01c, --项目代码 isnull(gda03c,'') gda03c, --项目简称 isnull(gda04c,'') gda04c, --项目类别 isnull(gda04Name,'') gda04Name, --项目类名 isnull(ggb45i,'') ggb45i, --价格序号(0标准价,1为体验价,2之后为项目) isnull(ggb36f,'') ggb36f --业绩 from #kfxhtable left join gbm01 with(nolock) on gba00c = gga00c and gba01c = gga05c where ggb36f != 0 and (gda02c = @gda02c or @gda02c = '') and (gda01c = @gda01c or @gda01c = '') and (@fromyg = '' or (ggb12c >= @fromyg and ggb12c <= @toyg)) union all select isnull(ggb13c,'') ggb13c, --员工 isnull(gda02c,'') gda02c, --部门代码 isnull(gda02cName,'') gda02cName, --部门名称 isnull(gga01c,'') gga01c, --消费单号, isnull(gga05c,'') gga05c, --会员代码 isnull((case gga07c when 'Z' then '非会员' else '会员' end),'') gga07c, --会员卡类别 isnull(gga06c,'') gga06c, --客户姓名 isnull(gga14f,0) gga14f, --客人数量 isnull((case gga31i when 0 then '男' when 1 then '女' end),'') gga31i, --顾客性别 isnull(gga13f,'') gga13f, --是否为老客 isnull(gba17d,'') gba17d, -- 会员生日, isnull(gba08c,'') gba08c, -- 会员电话, isnull(gba06c,'') gba06c, -- 微信, isnull(gba05c,'') gba05c, -- 地址, isnull(gga16f,0) gga16f, --消费金额 isnull(gda01c,'') gda01c, --项目代码 isnull(gda03c,'') gda03c, --项目简称 isnull(gda04c,'') gda04c, --项目类别 isnull(gda04Name,'') gda04Name, --项目类名 isnull(ggb45i,'') ggb45i, --价格序号(0标准价,1为体验价,2之后为项目) isnull(ggb37f,'') ggb37f --业绩 from #kfxhtable left join gbm01 with(nolock) on gba00c = gga00c and gba01c = gga05c where ggb37f != 0 and (gda02c = @gda02c or @gda02c = '') and (gda01c = @gda01c or @gda01c = '') and (@fromyg = '' or (ggb13c >= @fromyg and ggb13c <= @toyg)) union all select isnull(ggb14c,'') ggb14c, --员工 isnull(gda02c,'') gda02c, --部门代码 isnull(gda02cName,'') gda02cName, --部门名称 isnull(gga01c,'') gga01c, --消费单号, isnull(gga05c,'') gga05c, --会员代码 isnull((case gga07c when 'Z' then '非会员' else '会员' end),'') gga07c, --会员卡类别 isnull(gga06c,'') gga06c, --客户姓名 isnull(gga14f,0) gga14f, --客人数量 isnull((case gga31i when 0 then '男' when 1 then '女' end),'') gga31i, --顾客性别 isnull(gga13f,'') gga13f, --是否为老客 isnull(gba17d,'') gba17d, -- 会员生日, isnull(gba08c,'') gba08c, -- 会员电话, isnull(gba06c,'') gba06c, -- 微信, isnull(gba05c,'') gba05c, -- 地址, isnull(gga16f,0) gga16f, --消费金额 isnull(gda01c,'') gda01c, --项目代码 isnull(gda03c,'') gda03c, --项目简称 isnull(gda04c,'') gda04c, --项目类别 isnull(gda04Name,'') gda04Name, --项目类名 isnull(ggb45i,'') ggb45i, --价格序号(0标准价,1为体验价,2之后为项目) isnull(ggb38f,'') ggb38f --业绩 from #kfxhtable left join gbm01 with(nolock) on gba00c = gga00c and gba01c = gga05c where ggb38f != 0 and (gda02c = @gda02c or @gda02c = '') and (gda01c = @gda01c or @gda01c = '') and (@fromyg = '' or (ggb14c >= @fromyg and ggb14c <= @toyg)) union all select isnull(ggb26c,'') ggb26c, --员工 isnull(gda02c,'') gda02c, --部门代码 isnull(gda02cName,'') gda02cName, --部门名称 isnull(gga01c,'') gga01c, --消费单号, isnull(gga05c,'') gga05c, --会员代码 isnull((case gga07c when 'Z' then '非会员' else '会员' end),'') gga07c, --会员卡类别 isnull(gga06c,'') gga06c, --客户姓名 isnull(gga14f,0) gga14f, --客人数量 isnull((case gga31i when 0 then '男' when 1 then '女' end),'') gga31i, --顾客性别 isnull(gga13f,'') gga13f, --是否为老客 isnull(gba17d,'') gba17d, -- 会员生日, isnull(gba08c,'') gba08c, -- 会员电话, isnull(gba06c,'') gba06c, -- 微信, isnull(gba05c,'') gba05c, -- 地址, isnull(gga16f,0) gga16f, --消费金额 isnull(gda01c,'') gda01c, --项目代码 isnull(gda03c,'') gda03c, --项目简称 isnull(gda04c,'') gda04c, --项目类别 isnull(gda04Name,'') gda04Name, --项目类名 isnull(ggb45i,'') ggb45i, --价格序号(0标准价,1为体验价,2之后为项目) isnull(ggb39f,'') ggb39f --业绩 from #kfxhtable left join gbm01 with(nolock) on gba00c = gga00c and gba01c = gga05c where ggb39f != 0 and (gda02c = @gda02c or @gda02c = '') and (gda01c = @gda01c or @gda01c = '') and (@fromyg = '' or (ggb26c >= @fromyg and ggb26c <= @toyg)) end --end 其他 end drop table #kfxhtable drop table #lsTable end GO /****** Object: Table [dbo].[fieldname] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[fieldname]( [tblname] [char](20) NOT NULL, [fldname] [char](20) NOT NULL, [fldengdesc] [char](2000) NULL, [fldchndesc] [char](2000) NULL, [fldtype] [char](1) NULL, [fldlength] [int] NULL, [flddivlength] [int] NULL, [isvisible] [char](2) NULL, CONSTRAINT [pk_fieldname] PRIMARY KEY CLUSTERED ( [tblname] ASC, [fldname] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gbm22] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gbm22]( [gbv00c] [varchar](10) NOT NULL, [gbv01c] [varchar](20) NOT NULL, [gbv02f] [float] NOT NULL, [gbv03c] [varchar](20) NULL, [gbv04c] [varchar](20) NULL, [gbv06c] [varchar](10) NOT NULL, [gbv07c] [varchar](20) NULL, [gbv08c] [varchar](20) NULL, [gbv09c] [varchar](20) NULL, [gbv10c] [varchar](20) NULL, [gbv11c] [varchar](20) NULL, [gbv12c] [varchar](20) NULL, [gbv13c] [varchar](80) NULL, [gbv14c] [varchar](80) NULL, [gbv15c] [varchar](80) NULL, CONSTRAINT [PK_GBM22] PRIMARY KEY CLUSTERED ( [gbv00c] ASC, [gbv01c] ASC, [gbv02f] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[tbl_profit_day_buf] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[tbl_profit_day_buf]( [idd] [int] IDENTITY(1,1) NOT NULL, [compid] [varchar](10) NULL, [fromdate] [varchar](10) NULL, [todate] [varchar](10) NULL, [flag] [int] NULL, [staff] [varchar](20) NULL, [fromdepart] [varchar](10) NULL, [todepart] [varchar](10) NULL, [calmode] [int] NULL, [code] [varchar](20) NOT NULL, [itemname] [varchar](60) NULL, [is_add] [int] NULL, [amt] [float] NULL, [is_leaf] [int] NULL, [payment] [varchar](10) NULL, [kind] [int] NULL, PRIMARY KEY CLUSTERED ( [idd] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gicm12] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gicm12]( [gicl00c] [varchar](10) NOT NULL, [gicl01v] [varchar](20) NOT NULL, [gicl02i] [int] IDENTITY(1,1) NOT NULL, [gicl03v] [varchar](20) NULL, [gicl04v] [varchar](20) NULL, [gicl05v] [varchar](20) NULL, [gicl06v] [varchar](20) NULL, [gicl07v] [varchar](20) NULL, [gicl08v] [varchar](20) NULL, [gicl09i] [varchar](20) NULL, [gicl10v] [varchar](20) NULL, [gicl11v] [varchar](240) NULL, CONSTRAINT [PK_GICM12] PRIMARY KEY CLUSTERED ( [gicl00c] ASC, [gicl01v] ASC, [gicl02i] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_get_happy_birth_members] Script Date: 06/12/2017 06:20:07 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_get_happy_birth_members] ( @compid varchar(10), @fromdate varchar(4), @todate varchar(4), @fromdate_y varchar(4), @todate_y varchar(4), @include_sons int, @chk0 int, --死亡客户 4 @chk1 int --休眠客户 3 ) as begin if @fromdate = '' select @fromdate = '0101' if @todate = '' select @todate = '1231' if @fromdate_y = '' select @fromdate_y = '0101' if @todate_y = '' select @todate_y = '1231' create table #tbl_members ( compid varchar(20) not null, memid varchar(20) not null, cardid varchar(20) null, nname varchar(40) null, birth varchar(20) null, tel varchar(60) null, mobile varchar(60) null, addr varchar(160) null, birth_y varchar(20) null ) create table #tbl_members_result ( compid varchar(20) not null, memid varchar(20) not null, cardid varchar(20) null, nname varchar(40) null, birth varchar(20) null, tel varchar(60) null, mobile varchar(60) null, addr varchar(160) null, birth_y varchar(20) null, fzren1 varchar(20), -- 负责人1 fzrename1 varchar(20), -- 负责人姓名1 dj1 int, -- 会员等级1 djcode1 varchar(20), -- 会员等级代码1 Depart1 varchar(20), -- 部门编号1 cardsection1 varchar(20), -- 会员归属部门1 fzren2 varchar(20), -- 负责人2 fzrename2 varchar(20), -- 负责人姓名2 dj2 int, -- 会员等级2 djcode2 varchar(20), -- 会员等级代码2 Depart2 varchar(20), -- 部门编号2 cardsection2 varchar(20), -- 会员归属部门2 fzren3 varchar(20), -- 负责人3 fzrename3 varchar(20), -- 负责人姓名3 dj3 int, -- 会员等级3 djcode3 varchar(20), -- 会员等级代码3 Depart3 varchar(20), -- 部门编号3 cardsection3 varchar(20), -- 会员归属部门3 fzren4 varchar(20), -- 负责人4 fzrename4 varchar(20), -- 负责人姓名4 dj4 int, -- 会员等级4 djcode4 varchar(20), -- 会员等级代码4 Depart4 varchar(20), -- 部门编号4 cardsection4 varchar(20), -- 会员归属部门4 ) if @include_sons = 0 begin -- 没有卡会员的日期范围内的生日 insert #tbl_members(compid, memid, cardid, nname, birth, birth_y, tel, mobile, addr) select gba00c, gba01c, '', gba03c, gba17d, gba21c, gba07c, gba08c, gba05c from gbm01 where gba00c = @compid and ((substring(gba17d, 5, 4) >= @fromdate and substring(gba17d, 5, 4) <= @todate) or (substring(gba21c, 5, 4) >= @fromdate_y and substring(gba21c, 5, 4) <= @todate_y)) and isnull(gba23c, '') = '' --本公司自己的会员卡 insert #tbl_members(compid, memid, cardid, nname, birth, birth_y, tel, mobile, addr) select gba00c, gba01c, gba23c, gba03c, gba17d, gba21c, gba07c, gba08c, gba05c from gbm01 a, gcm01 b where gba00c = @compid and ((substring(gba17d, 5, 4) >= @fromdate and substring(gba17d, 5, 4) <= @todate) or (substring(gba21c, 5, 4) >= @fromdate_y and substring(gba21c, 5, 4) <= @todate_y)) and isnull(gba23c, '')<>'' and a.gba00c = b.gca00c and a.gba23c = b.gca01c and b.gca00c = b.gca13d end else begin --包括自己下面的所有公司 --没有卡会员的日期范围内的生日 insert #tbl_members(compid, memid, cardid, nname, birth, birth_y, tel, mobile, addr) select gba00c, gba01c, '', gba03c, gba17d, gba21c, gba07c, gba08c, gba05c from gbm01 a, gam26 z where gba00c = z.gaz02c and z.gaz01c = @compid and ((substring(gba17d, 5, 4) >= @fromdate and substring(gba17d, 5, 4) <= @todate) or (substring(gba21c, 5, 4) >= @fromdate_y and substring(gba21c, 5, 4) <= @todate_y)) and isnull(gba23c, '') = '' --本公司自己的会员卡 insert #tbl_members(compid, memid, cardid, nname, birth, birth_y, tel, mobile, addr) select gba00c, gba01c, gba23c, gba03c, gba17d, gba21c, gba07c, gba08c, gba05c from gbm01 a, gcm01 b, gam26 z where a.gba00c = z.gaz02c and z.gaz01c = @compid and ((substring(gba17d, 5, 4) >= @fromdate and substring(gba17d, 5, 4) <= @todate) or (substring(gba21c, 5, 4) >= @fromdate_y and substring(gba21c, 5, 4) <= @todate_y)) and isnull(gba23c, '')<>'' and a.gba00c = b.gca00c and a.gba23c = b.gca01c and b.gca00c = b.gca13d end insert #tbl_members_result(compid,memid, cardid, nname, birth, birth_y, tel, mobile, addr) select distinct compid,memid, cardid, nname, birth, birth_y, tel, mobile, addr from #tbl_members --更新会员等级,负责人 declare @gbr02c varchar(30) -- 所属部门 declare @intSn int set @intSn = 1 declare gbm18_cursor cursor for select gbr02c from gbm18 inner join #tbl_members_result on gbr00c = compid and gbr01c = memid group by gbr02c open gbm18_cursor fetch next from gbm18_cursor into @gbr02c while(@@fetch_status = 0) begin if( @intSn = 1) begin update a set Depart1 = b.gbr02c, fzren1 = b.gbr03c, dj1 = b.gbr04i, cardsection1 = (select gsb03c from gsm02 where gsb00c = b.gbr00c and gsb01c = 'U' and gsb02c = b.gbr02c), fzrename1 = (select haa02c from ham01 where haa00c = b.gbr00c and haa01c = b.gbr03c), --负责人名称 djcode1 = (select gsb03c from gsm02 where gsb00c = b.gbr00c and gsb01c = 'DJ' and gsb02c = b.gbr04i) --会员等级 from #tbl_members_result a, gbm18 b where a.compid = b.gbr00c and a.memid = b.gbr01c and b.gbr02c = @gbr02c end else if(@intSn = 2) begin update a set Depart2 = b.gbr02c, fzren2 = b.gbr03c, dj2 = b.gbr04i, cardsection2 = (select gsb03c from gsm02 where gsb00c = b.gbr00c and gsb01c = 'U' and gsb02c = b.gbr02c), fzrename2 = (select haa02c from ham01 where haa00c = b.gbr00c and haa01c = b.gbr03c), --负责人名称 djcode2 = (select gsb03c from gsm02 where gsb00c = b.gbr00c and gsb01c = 'DJ' and gsb02c = b.gbr04i) --会员等级 from #tbl_members_result a, gbm18 b where a.compid = b.gbr00c and a.memid = b.gbr01c and b.gbr02c = @gbr02c end else if(@intSn = 3) begin update a set Depart3 = b.gbr02c, fzren3 = b.gbr03c, dj3 = b.gbr04i, cardsection3 = (select gsb03c from gsm02 where gsb00c = b.gbr00c and gsb01c = 'U' and gsb02c = b.gbr02c), fzrename3 = (select haa02c from ham01 where haa00c = b.gbr00c and haa01c = b.gbr03c), --负责人名称 djcode3 = (select gsb03c from gsm02 where gsb00c = b.gbr00c and gsb01c = 'DJ' and gsb02c = b.gbr04i) --会员等级 from #tbl_members_result a, gbm18 b where a.compid = b.gbr00c and a.memid = b.gbr01c and b.gbr02c = @gbr02c end else if(@intSn = 4) begin update a set Depart4 = b.gbr02c, fzren4 = b.gbr03c, dj4 = b.gbr04i, cardsection4 = (select gsb03c from gsm02 where gsb00c = b.gbr00c and gsb01c = 'U' and gsb02c = b.gbr02c), fzrename4 = (select haa02c from ham01 where haa00c = b.gbr00c and haa01c = b.gbr03c), --负责人名称 djcode4 = (select gsb03c from gsm02 where gsb00c = b.gbr00c and gsb01c = 'DJ' and gsb02c = b.gbr04i) --会员等级 from #tbl_members_result a, gbm18 b where a.compid = b.gbr00c and a.memid = b.gbr01c and b.gbr02c = @gbr02c end set @intSn = @intSn + 1 fetch next from gbm18_cursor into @gbr02c end close gbm18_cursor deallocate gbm18_cursor select * from #tbl_members_result where (@chk0 = '' or (isnull(dj1,'') != @chk0 or isnull(dj2,'') != @chk0 or isnull(dj3,'') != @chk0 or isnull(dj4,'') != @chk0)) and (@chk1 = '' or (isnull(dj1,'') != @chk1 or isnull(dj2,'') != @chk1 or isnull(dj3,'') != @chk1 or isnull(dj4,'') != @chk1)) drop table #tbl_members drop table #tbl_members_result end GO /****** Object: StoredProcedure [dbo].[upg_statis_stat] Script Date: 06/12/2017 06:20:44 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_statis_stat] ( @compid varchar(10), @fromdate varchar(20), @todate varchar(20), @fromstaff varchar(20), @tostaff varchar(20) ) as begin create table #tbl_staff_stat ( staff varchar(20) not null, value float null ) create table #tbl_one_eval ( staff varchar(20) not null, num_1 float null, -- 满意的人数 ratio_1 float null, -- 满意的比率 num_2 float null, -- 一般满意的人数 ratio_2 float null, -- 一般满意的比率 num_3 float null, -- 不满意的人数 ratio_3 float null -- 不满意的比率 ) create table #tbl_one_num ( staff varchar(20) not null, lvl int null, -- 级别 num float null ) insert #tbl_staff_stat(staff, value) select ggf02c, ggf04i from ggm06 a, ggm01 b where a.ggf00c = b.gga00c and a.ggf01c = b.gga01c and b.gga02d >= @fromdate and b.gga02d <= @todate and a.ggf00c = @compid and a.ggf02c >= @fromstaff and a.ggf02c <= @tostaff insert #tbl_staff_stat(staff, value) select ggf05c, ggf07i from ggm06 a, ggm01 b where a.ggf00c = b.gga00c and a.ggf01c = b.gga01c and b.gga02d >= @fromdate and b.gga02d <= @todate and a.ggf00c = @compid and a.ggf05c >= @fromstaff and a.ggf05c <= @tostaff insert #tbl_staff_stat(staff, value) select ggf08c, ggf10i from ggm06 a, ggm01 b where a.ggf00c = b.gga00c and a.ggf01c = b.gga01c and b.gga02d >= @fromdate and b.gga02d <= @todate and a.ggf00c = @compid and a.ggf08c >= @fromstaff and a.ggf08c <= @tostaff insert #tbl_staff_stat(staff, value) select ggf11c, ggf13i from ggm06 a, ggm01 b where a.ggf00c = b.gga00c and a.ggf01c = b.gga01c and b.gga02d >= @fromdate and b.gga02d <= @todate and a.ggf00c = @compid and a.ggf11c >= @fromstaff and a.ggf11c <= @tostaff insert #tbl_staff_stat(staff, value) select ggf14c, ggf16i from ggm06 a, ggm01 b where a.ggf00c = b.gga00c and a.ggf01c = b.gga01c and b.gga02d >= @fromdate and b.gga02d <= @todate and a.ggf00c = @compid and a.ggf14c >= @fromstaff and a.ggf14c <= @tostaff insert #tbl_staff_stat(staff, value) select ggf17c, ggf19i from ggm06 a, ggm01 b where a.ggf00c = b.gga00c and a.ggf01c = b.gga01c and b.gga02d >= @fromdate and b.gga02d <= @todate and a.ggf00c = @compid and a.ggf17c >= @fromstaff and a.ggf17c <= @tostaff insert #tbl_one_eval(staff, num_1, ratio_1, num_2, ratio_2, num_3, ratio_3) select staff, 0, 0, 0, 0, 0, 0 from #tbl_staff_stat group by staff --满意的个数 insert #tbl_one_num(staff, lvl, num) select staff, value, count(*) from #tbl_staff_stat where value = 1 group by staff, value update a set a.num_1 = b.num from #tbl_one_eval a, #tbl_one_num b where a.staff = b.staff --一般满意的个数 delete #tbl_one_num insert #tbl_one_num(staff, lvl, num) select staff, value, count(*) from #tbl_staff_stat where value = 2 group by staff, value update a set a.num_2 = b.num from #tbl_one_eval a, #tbl_one_num b where a.staff = b.staff --不满意的个数 delete #tbl_one_num insert #tbl_one_num(staff, lvl, num) select staff, value, count(*) from #tbl_staff_stat where value >= 3 and value <= 6 group by staff, value update a set a.num_3 = b.num from #tbl_one_eval a, #tbl_one_num b where a.staff = b.staff update #tbl_one_eval set ratio_1 = case when (isnull(num_1, 0) + isnull(num_2, 0) + isnull(num_3, 0)) = 0 then 0 else num_1/(isnull(num_1, 0) + isnull(num_2, 0) + isnull(num_3, 0)) end, ratio_2 = case when (isnull(num_1, 0) + isnull(num_2, 0) + isnull(num_3, 0)) = 0 then 0 else num_2/(isnull(num_1, 0) + isnull(num_2, 0) + isnull(num_3, 0)) end, ratio_3 = case when (isnull(num_1, 0) + isnull(num_2, 0) + isnull(num_3, 0)) = 0 then 0 else num_3/(isnull(num_1, 0) + isnull(num_2, 0) + isnull(num_3, 0)) end select * from #tbl_one_eval drop table #tbl_one_num drop table #tbl_staff_stat drop table #tbl_one_eval end GO /****** Object: Table [dbo].[ghm02] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[ghm02]( [ghb00c] [varchar](10) NOT NULL, [ghb01c] [varchar](100) NOT NULL, [ghb02f] [float] NOT NULL, [ghb03c] [varchar](20) NULL, [ghb04c] [varchar](5) NULL, [ghb05f] [float] NULL, [ghb06c] [varchar](5) NULL, [ghb07f] [float] NULL, [ghb08c] [varchar](5) NULL, [ghb09f] [float] NULL, [ghb10f] [float] NULL, [ghb11f] [float] NULL, [ghb12c] [varchar](20) NULL, [ghb13f] [float] NULL, [ghb14f] [float] NULL, [ghb15d] [varchar](8) NULL, CONSTRAINT [PK_GHM021] PRIMARY KEY CLUSTERED ( [ghb00c] ASC, [ghb01c] ASC, [ghb02f] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_cardinfo_combo_treatment] Script Date: 06/12/2017 06:19:37 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_cardinfo_combo_treatment] ( @compid varchar(10), --门店编号 @flag char(2), --0:疗程,1:套餐 @cardnofrom varchar(10), --会员卡号 @cardnoto varchar(20), @combofrom varchar(20), --疗程编号 @comboto varchar(20), @treatmentfrom varchar(20), --套餐编号 @treatmentto varchar(20) ) as begin --创建临时表 create table sumcardinfo ( cardcount float null, --卡数 combo_treatment float null --疗程或套餐数量 ) declare @cardcount float declare @combo_treatment float if(@flag = 0) begin --begin会员疗程 create table #gcm06 -- 会员卡--疗程 ( gcf00c varchar(10) not null, --门店编号 gcf01c varchar(20) not null, --卡号 gcf01cName varchar(40) null, --会员卡姓名 gcf02c varchar(20) not null, --项目编号 gcf02cName varchar(40) null, --项目名称 mobileno varchar(20) null, --手机号码 gcf03f float null, --次数 gcf04f float null, --赠送次数 gcf05f float null, --总次数 gcf06f float null, --已经使用次数 gcf07f float null, --剩余次数 gcf08f float null, --疗程金额 gcf09f float null, --已经使用金额 gcf10f float null --剩余金额 -- gcf23i int not null --序号 ) -- 插入数据 insert #gcm06 (gcf00c, gcf02c, gcf01c, gcf03f, gcf04f, gcf05f, gcf08f, gcf06f, gcf09f, gcf07f, gcf10f ) select gcf00c, gcf02c, gcf01c, sum(gcf03f), sum(gcf04f), sum(gcf05f), sum(gcf08f), sum(gcf06f), sum(gcf09f), sum(gcf07f), sum(gcf10f) from gcm06 with(nolock) where gcf00c = @compid and ((gcf01c >= @cardnofrom and gcf01c <= @cardnoto) or (@cardnofrom = '*')) and ((gcf02c <= @combofrom and gcf02c >= @comboto) or (@combofrom = '*')) and gcf27i = 0 and gcf28i = 0 group by gcf00c, gcf02c, gcf01c -- 更改项目名称 update a set a.gcf02cName = b.gda03c from #gcm06 a, gdm01 b with (nolock) where a.gcf02c = b.gda01c and b.gda00c = @compid --更改手机号码,会员姓名 update a set a.mobileno = b.gba08c, a.gcf01cName = b.gba03c from #gcm06 a, gbm01 b with (nolock) where a.gcf00c = b.gba00c and a.gcf01c = b.gba23c select gcf02c, gcf02cName, gcf01c, gcf01cName, mobileno, gcf03f, gcf04f, gcf05f, gcf08f, gcf06f, gcf09f, gcf07f, gcf10f --疗程 from #gcm06 order by gcf02c, gcf01c, gcf00c --赋值 select @combo_treatment = count(distinct gcf02c) from #gcm06 select @cardcount = count(distinct gcf01c) from #gcm06 insert sumcardinfo (cardcount, combo_treatment) values (@cardcount, @combo_treatment) drop table #gcm06 --end end else begin --begin会员套餐 --创建临时表 -套餐项目 create table #gtc04 ( gte00c varchar(10) not null, --门店编号 gte01c varchar(20) not null, --卡号 gte01cName varchar(40) null, --会员姓名 gte02c varchar(20) not null, --套餐编号 gte02cName varchar(40) null, --套餐名称 gte03c varchar(20) not null, --项目编号 gte03cName varchar(40) null, --项目名称 gte04f float null, --项目总次数 gte05f float null, --消费次数 gte06f float null, --剩余次数 gte08c varchar(20) null, --主项目 --gte09i int identity not null, --序号 flag int not null --区分套餐中的项目,产品 ) -- 插入数据 insert #gtc04(gte00c, gte01c, gte02c, gte03c, gte04f, gte05f, gte06f, gte08c, flag) select gte00c, gte01c, UPPER(gte02c), gte03c, sum(gte04f), sum(gte05f), sum(gte06f), gte08c, '1' from gtc04 with (nolock) where gte00c = @compid and ((gte01c >= @cardnofrom and gte01c <= @cardnoto) or(@cardnofrom = '*')) and ((gte02c >= @treatmentfrom and gte02c <= @treatmentto) or (@treatmentfrom = '*')) group by gte00c, gte01c, gte02c, gte03c, gte08c --更改会员姓名 update a set a.gte01cName = b.gba03c from #gtc04 a, gbm01 b with (nolock) where a.gte00c = b.gba00c and a.gte01c = b.gba23c --更改套餐名称 update a set a.gte02cName = b.gta02c from #gtc04 a, gtc01 b with (nolock) where a.gte00c = b.gta00c and a.gte02c = b.gta01c -- 更改项目名称 update a set a.gte03cName = b.gda03c from #gtc04 a, gdm01 b with (nolock) where a.gte03c = b.gda01c and b.gda00c = @compid --创建临时表 -套餐产品 create table #gtc08 ( gtj00c varchar(10) not null, --门店编号 gtj01c varchar(20) not null, --卡号 gtj01cName varchar(40) null, --会员姓名 gtj02c varchar(20) not null, --套餐编号 gtj02cName varchar(40) null, --套餐名称 gtj03c varchar(20) not null, --产品编号 gtj03cName varchar(40) null, --产品名称 gtj04f float null, --总次数 gtj05f float null, --已经使用次数 gtj06f float null, --剩余次数 flag int not null --区分套餐中的项目,产品 ) -- 插入数据 insert #gtc08 (gtj00c, gtj01c, gtj02c, gtj03c, gtj04f, gtj05f, gtj06f, flag) select gtj00c, gtj01c, UPPER(gtj02c), gtj03c, sum(gtj04f), sum(gtj05f), sum(gtj06f), '0' from gtc08 with (nolock) where gtj00c = @compid and ((gtj01c >= @cardnofrom and gtj01c <= @cardnoto) or (@cardnofrom = '*')) and ((gtj02c <= @treatmentfrom and gtj02c >= @treatmentto) or (@treatmentfrom = '*')) group by gtj00c, gtj01c, gtj02c, gtj03c --更改会员姓名 update a set a.gtj01cName = b.gba03c from #gtc08 a, gbm01 b with (nolock) where a.gtj00c = b.gba00c and a.gtj01c = b.gba23c --更改套餐名称 update a set a.gtj02cName = b.gta02c from #gtc08 a, gtc01 b with (nolock) where a.gtj00c = b.gta00c and a.gtj02c = b.gta01c --更改产品名称 update a set a.gtj03cName = b.gfa03c from #gtc08 a, gfm01 b with(nolock) where a.gtj00c = b.gfa00c and a.gtj03c = b.gfa01c --end select gte00c, gte01c, gte01cName, gte02c, gte02cName, gte03c, gte03cName, gte04f, gte05f, gte06f, gte08c, flag from #gtc04 order by gte02c, gte01c, gte00c --套餐项目 select gtj00c, gtj01c, gtj01cName, gtj02c, gtj02cName, gtj03c, gtj03cName, gtj04f, gtj05f, gtj06f, flag from #gtc08 order by gtj02c, gtj01c, gtj00c --套餐产品 --赋值 select @combo_treatment = count(distinct combo_treatment) from ( select gte02c as combo_treatment from #gtc04 union all select gtj02c as combo_treatment from #gtc08 ) c select @cardcount = count(distinct cardcount) from ( select gte01c as cardcount from #gtc04 union all select gtj01c as cardcount from #gtc08 ) d insert sumcardinfo (cardcount, combo_treatment) values (@cardcount, @combo_treatment) drop table #gtc08 drop table #gtc04 end select cardcount,combo_treatment from sumcardinfo drop table sumcardinfo end GO /****** Object: Table [dbo].[lrm03] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[lrm03]( [grc00c] [varchar](10) NOT NULL, [grc01c] [varchar](10) NOT NULL, [grc02c] [varchar](20) NOT NULL, [grc03c] [varchar](20) NOT NULL, [grc04f] [float] NULL, [grc05c] [varchar](10) NULL, [grc06f] [float] NULL, [grc07i] [int] NULL, [grc08i] [int] NULL, [grc09f] [float] NULL, [grc10c] [varchar](20) NOT NULL, CONSTRAINT [pk_lrm03] PRIMARY KEY CLUSTERED ( [grc00c] ASC, [grc01c] ASC, [grc02c] ASC, [grc03c] ASC, [grc10c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gxm04_log] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gxm04_log]( [gxd04c] [varchar](200) NULL, [gxd06c] [varchar](1000) NULL, [compid] [varchar](10) NULL, [userid] [varchar](20) NULL, [ddate] [varchar](10) NULL, [ttime] [varchar](10) NULL, [idd] [int] IDENTITY(1,1) NOT NULL, CONSTRAINT [PK_GXM04_LOG] PRIMARY KEY CLUSTERED ( [idd] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_spreader_verf] Script Date: 06/12/2017 06:20:41 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_spreader_verf] ( @compid varchar(10), @fromdate varchar(8), @todate varchar(8), @fromstaff varchar(20), @tostaff varchar(20), @includeother int -- 是否包括其他店的业绩 ) as begin create table #staff ( nowcompid varchar(10) null, -- 公司别 nowfromstaff varchar(20) null, -- 员工编号 nowtostaff varchar(20) null, -- 结束编号 ) create table #result ( nowcompid varchar(10) null, billno varchar(20) null, staffno varchar(20) null, memberno varchar(20) null, accountdate varchar(8) null, introcount float null, --推广人数 dealcount float null, --成交人员 suncount float null, --总业绩 billtype varchar(5) null, ) create table #master ( compid varchar(10) null, --公司别 flag int null, --0--是主档 1--明细 没有任何意义的就是一个标志位 [no] varchar(20) null, --编号 flag = 0 的时候是员工编号 1的时候是具体的单号 [name] varchar(40) null, --名称 flag = 0 的时候是员工名称 1的时候是会员名称 [date] varchar(8) null, --flag = 0的时候这个值是空值 1的时候是单据的账务日期 introcount float null, --推广人数 dealcount float null, --成交人数 verpcount float null, --总业绩 staffno varchar(20) null, --员工编号 没有任何意义就是为了排序用的 ) if (isnull(@fromstaff, '*') = '*') begin insert #staff(nowcompid, nowfromstaff, nowtostaff) select gaz02c, min(haa01c), max(haa01c) from gam26 with(nolock), ham01 with(nolock) where gaz02c = haa00c and gaz01c = @compid group by gaz02c end else begin insert #staff(nowcompid, nowfromstaff, nowtostaff) values(@compid, @fromstaff, @tostaff) end declare @nowcompid varchar(10) declare @nowfromstaff varchar(20) declare @nowtostaff varchar(20) declare cursor_01 cursor for select nowcompid, nowfromstaff, nowtostaff from #staff open cursor_01 fetch cursor_01 into @nowcompid, @nowfromstaff, @nowtostaff while @@fetch_status = 0 begin --首先插入做项目的推广,但是没有业绩的 --注意如果推广的时候购买的产品 那么这个产品应该算业绩的 insert into #result(nowcompid, billno, staffno, memberno, accountdate, introcount, dealcount, suncount, billtype) select gga00c, gga01c, gll06v, gga08c, gga80d, gga14f, 0, 0, 'gx' from ggm01 with (nolock), glm01 with (nolock), glm12 with(nolock) where gga30c = gla01c and gla00c = gga00c and gll01v = gla01c and gll00c = gla00c and gga00c = @nowcompid and (gll06v between @nowfromstaff and @nowtostaff) and (gga80d between @fromdate and @todate) --传入成交人数 和业绩 insert into #result(nowcompid, billno, staffno, memberno, accountdate, introcount, dealcount, suncount, billtype) select gna00c, gna01c, gna30c, gna13c, gna80d, 0, 1, gna71f, 'gn' from gnm01 with(nolock) where gna00c = @compid and (gna30c between @nowfromstaff and @nowtostaff) and (gna80d between @fromdate and @todate) fetch cursor_01 into @nowcompid, @nowfromstaff, @nowtostaff end close cursor_01 deallocate cursor_01 ---更新推广的时候购买产品的业绩第一销售 update a set a.suncount = b.ggc21f from #result a, ggm03 b with(nolock) where a.nowcompid = b.ggc00c and a.billno = b.ggc01c and a.billtype = 'gx' and a.staffno = b.ggc12c ---更新推广的时候购买产品的业绩第二销售 update a set a.suncount = b.ggc23f from #result a, ggm03 b with(nolock) where a.nowcompid = b.ggc00c and a.billno = b.ggc01c and a.billtype = 'gx' and a.staffno = b.ggc15c ---更新推广的时候购买产品的业绩第三销售 update a set a.suncount = b.ggc25f from #result a, ggm03 b with(nolock) where a.nowcompid = b.ggc00c and a.billno = b.ggc01c and a.billtype = 'gx' and a.staffno = b.ggc17c --insert主档 insert into #master(compid, flag, [no], [name], [date], introcount, dealcount, verpcount, staffno) select nowcompid, 0, staffno, '', '', sum(isnull(introcount, 0)), sum(isnull(dealcount, 0)), sum(isnull(suncount, 0)), staffno from #result group by nowcompid, staffno --跟新主档的员工名称 update a set a.name = haa02c from #master a, ham01 b with(nolock) where a.compid = b.haa00c and a.no = b.haa01c and a.flag = 0 --insert detail info insert into #master(compid, flag, [no], [name], [date], introcount, dealcount, verpcount, staffno) select nowcompid, 1, billno, memberno, accountdate, sum(isnull(introcount, 0)), sum(isnull(dealcount, 0)), sum(isnull(suncount, 0)), staffno from #result group by nowcompid, billno, memberno, memberno, billtype, accountdate, staffno update a set a.name = b.gba03c from #master a, gbm01 b with(nolock) where a.compid = b.gba00c and a.name = b.gba23c and a.flag = 1 select isnull(compid,'') compid, isnull(flag,0) flag, isnull([no],'') [no], isnull([name],'') [name], isnull([date] ,'') [date] , isnull(introcount,0) introcount, isnull(dealcount,0) dealcount, isnull(verpcount,0) verpcount, isnull(staffno,'') staffno from #master order by compid, staffno, flag end GO /****** Object: Table [dbo].[gcm03_log] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gcm03_log]( [gcc00c] [varchar](10) NOT NULL, [gcc01c] [varchar](20) NOT NULL, [gcc02f] [float] NOT NULL, [gcc03i] [int] NOT NULL, [gcc04c] [varchar](20) NULL, [gcc05c] [varchar](5) NULL, [gcc06f] [float] NULL, [gcc07f] [float] NULL, [gcc08c] [varchar](20) NULL, [gcc09d] [varchar](20) NULL, [gcc10f] [float] NULL, [gcc11c] [varchar](60) NULL, [idd] [int] IDENTITY(1,1) NOT NULL, [compid] [varchar](10) NULL, [userid] [varchar](20) NULL, [ddate] [varchar](10) NULL, [ttime] [varchar](10) NULL, CONSTRAINT [PK_GCM03_LOG] PRIMARY KEY CLUSTERED ( [idd] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gjm01] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gjm01]( [gja00c] [varchar](10) NOT NULL, [gja01c] [varchar](20) NOT NULL, [gja02d] [varchar](8) NULL, [gja03c] [varchar](10) NULL, [gja04c] [varchar](20) NULL, [gja05c] [varchar](20) NULL, [gja06f] [float] NULL, [gja07f] [float] NULL, [gja08c] [varchar](4) NULL, [gja09c] [varchar](5) NULL, [gja10c] [varchar](20) NULL, [gja91c] [varchar](10) NULL, [gja92d] [varchar](8) NULL, [gja93c] [varchar](10) NULL, [gja94d] [varchar](8) NULL, CONSTRAINT [PK_GJM01] PRIMARY KEY CLUSTERED ( [gja00c] ASC, [gja01c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gzm01] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gzm01]( [gza00c] [varchar](10) NOT NULL, [gza01c] [varchar](5) NOT NULL, [gza02c] [varchar](5) NOT NULL, [gza03c] [varchar](5) NULL, [gza04f] [float] NULL, [gza05f] [float] NULL, [gza06i] [int] NULL, [gza07i] [int] NULL, CONSTRAINT [PK_gzm01] PRIMARY KEY CLUSTERED ( [gza00c] ASC, [gza01c] ASC, [gza02c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_date_analyse] Script Date: 06/12/2017 06:19:56 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_date_analyse] ( @flag varchar(5), -- 按何种方式统计分析(0.按日期方式 1.按周次方式分析) @compid varchar(10), @condition nvarchar(1024) -- 条件执行语句(前台组织好的) ) as--WUXM, 2004/12/26 begin create table #struct ( id int identity not null, dateno varchar(8) null, --日期或是周次 sum9 float null, --人数(before 9:00) sum12 float null, --人数(9:00~12:00) sum14 float null, --人数(12:00~14:00) sum17 float null, --人数(14:00~17:00) sum20 float null, --人数(17:00~20:00) sum21 float null, --人数(20:00~21:30) sum213 float null, --人数(21:30 after) primary key (id), ) declare @update nvarchar(1024) declare @select nvarchar(1024) declare @sql nvarchar(2048) if @flag = '0' begin declare @from nvarchar(1024) declare @to nvarchar(1024) select @sql = 'insert #struct(dateno) select distinct gla02d from glm01 a with (nolock), gam26 z with(nolock) where isnull(gla03t, '''')<> ''''' + @condition + ' group by gla02d' EXEC sp_executesql @sql --add @compid by lyj for mr_97 2007/4/27 select @select = N'(select sum(gla08i) from glm01 a with (nolock), gam26 z with(nolock) where gla02d = dateno and a.gla00c = z.gaz02c and z.gaz01c = ''' + @compid + ''' and (substring(gla03t, 1, 3) >= @ff and substring(gla03t, 1, 3)<@tt) )' set @from = N'000' set @to = N'090' set @update = N'update #struct set sum9 = ' set @sql = @update + @select EXEC sp_executesql @sql, N'@ff varchar(10), @tt varchar(10)', @from, @to set @from = N'090' set @to = N'120' set @update = N'update #struct set sum12 = ' set @sql = @update + @select EXEC sp_executesql @sql, N'@ff varchar(10), @tt varchar(10)', @from, @to set @from = N'120' set @to = N'140' set @update = N'update #struct set sum14 = ' set @sql = @update + @select EXEC sp_executesql @sql, N'@ff varchar(10), @tt varchar(10)', @from, @to set @from = N'140' set @to = N'170' set @update = N'update #struct set sum17 = ' set @sql = @update + @select EXEC sp_executesql @sql, N'@ff varchar(10), @tt varchar(10)', @from, @to set @from = N'170' set @to = N'200' set @update = N'update #struct set sum20 = ' set @sql = @update + @select EXEC sp_executesql @sql, N'@ff varchar(10), @tt varchar(10)', @from, @to set @from = N'200' set @to = N'213' set @update = N'update #struct set sum21 = ' set @sql = @update + @select EXEC sp_executesql @sql, N'@ff varchar(10), @tt varchar(10)', @from, @to set @from = N'213' set @to = N'240' set @update = N'update #struct set sum213 = ' set @sql = @update + @select EXEC sp_executesql @sql, N'@ff varchar(10), @tt varchar(10)', @from, @to end else begin insert #struct(dateno) --插入周次的数字 select '1' union select'2' union select '3' union select '4' union select '5' union select '6' union select '7' --add + @condition by lyj for mr_97 2007/4/27 select @select = N'( select sum(gla08i) from glm01 a with (nolock), gam26 z with(nolock) where datepart(weekday, gla02d) = dateno and gla00c = z.gaz02c and z.gaz01c = ''' + @compid + ''' and ( substring(gla03t, 1, 3) >= @ff and substring(gla03t, 1, 3)<@tt)' + @condition + ')' set @from = N'000' set @to = N'090' set @update = N'update #struct set sum9 = ' set @sql = @update + @select EXEC sp_executesql @sql, N'@ff varchar(10), @tt varchar(10)', @from, @to set @from = N'090' set @to = N'120' set @update = N'update #struct set sum12 = ' set @sql = @update + @select EXEC sp_executesql @sql, N'@ff varchar(10), @tt varchar(10)', @from, @to set @from = N'120' set @to = N'140' set @update = N'update #struct set sum14 = ' set @sql = @update + @select EXEC sp_executesql @sql, N'@ff varchar(10), @tt varchar(10)', @from, @to set @from = N'140' set @to = N'170' set @update = N'update #struct set sum17 = ' set @sql = @update + @select EXEC sp_executesql @sql, N'@ff varchar(10), @tt varchar(10)', @from, @to set @from = N'170' set @to = N'200' set @update = N'update #struct set sum20 = ' set @sql = @update + @select EXEC sp_executesql @sql, N'@ff varchar(10), @tt varchar(10)', @from, @to set @from = N'200' set @to = N'213' set @update = N'update #struct set sum21 = ' set @sql = @update + @select EXEC sp_executesql @sql, N'@ff varchar(10), @tt varchar(10)', @from, @to set @from = N'213' set @to = N'240' set @update = N'update #struct set sum213 = ' set @sql = @update + @select EXEC sp_executesql @sql, N'@ff varchar(10), @tt varchar(10)', @from, @to update #struct set dateno = '8' where dateno = '1' update #struct set dateno = dateno-1 end select isnull(dateno,'') dateno, isnull(sum9,0) sum9, isnull(sum12,0) sum12, isnull(sum14,0) sum14, isnull(sum17,0) sum17, isnull(sum20,0) sum20, isnull(sum21,0) sum21, isnull(sum213,0) sum213 from #struct order by dateno drop table #struct end GO /****** Object: Table [dbo].[spm03] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[spm03]( [id] [int] IDENTITY(1,1) NOT NULL, [cname] [varchar](100) NOT NULL, [remark] [varchar](4000) NULL, [spa02i] [tinyint] NULL, [spa03f] [float] NULL, [spa08i] [tinyint] NULL, [spa09f] [float] NULL, [spa10i] [tinyint] NULL, [spa11f] [float] NULL, [spa04i] [tinyint] NULL, [spa05f] [float] NULL, [spa14i] [tinyint] NULL, [spa15f] [float] NULL, [spa20i] [tinyint] NULL, [spa21f] [float] NULL, [spa06i] [tinyint] NULL, [spa07f] [float] NULL, [spa16i] [tinyint] NULL, [spa17f] [float] NULL, [spa22i] [tinyint] NULL, [spa23f] [float] NULL, [spa25i] [tinyint] NULL, [spa26f] [float] NULL, [spa27i] [tinyint] NULL, [spa28f] [float] NULL, [spb02i] [tinyint] NULL, [spb03f] [float] NULL, [spb08i] [tinyint] NULL, [spb09f] [float] NULL, [spb10i] [tinyint] NULL, [spb11f] [float] NULL, [spb04i] [tinyint] NULL, [spb05f] [float] NULL, [spb14i] [tinyint] NULL, [spb15f] [float] NULL, [spb20i] [tinyint] NULL, [spb21f] [float] NULL, [spb06i] [tinyint] NULL, [spb07f] [float] NULL, [spb16i] [tinyint] NULL, [spb17f] [float] NULL, [spb22i] [tinyint] NULL, [spb23f] [float] NULL, [spb25i] [tinyint] NULL, [spb26f] [float] NULL, [spb27i] [tinyint] NULL, [spb28f] [float] NULL, [spa19i] [int] NULL, [spa12f] [float] NULL, [spa13f] [float] NULL, [spa24f] [float] NULL, [spa29i] [int] NULL, [spa30i] [int] NULL, [spa31i] [int] NULL, [spa32i] [int] NULL, CONSTRAINT [PK_spm03] PRIMARY KEY CLUSTERED ( [id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gicm08] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gicm08]( [gich00c] [varchar](10) NOT NULL, [gich01v] [varchar](20) NOT NULL, [gich02v] [varchar](20) NOT NULL, [gich03i] [int] NULL, [gich04t] [varchar](6) NULL, [gich05t] [varchar](6) NULL, [gich06d] [varchar](8) NULL, [gich07i] [int] NULL, [gich08c] [varchar](20) NULL, CONSTRAINT [PK_GICM08] PRIMARY KEY CLUSTERED ( [gich00c] ASC, [gich01v] ASC, [gich02v] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[glm01] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[glm01]( [gla00c] [varchar](10) NOT NULL, [gla01c] [varchar](20) NOT NULL, [gla02d] [varchar](8) NULL, [gla03t] [varchar](6) NULL, [gla04t] [varchar](6) NULL, [gla05c] [nvarchar](50) NULL, [gla06c] [varchar](20) NULL, [gla07c] [varchar](5) NULL, [gla08i] [int] NULL, [gla09i] [int] NULL, [gla10i] [int] NULL, [gla11f] [float] NULL, [gla12f] [float] NULL, [gla13d] [varchar](8) NULL, [gla14c] [varchar](30) NULL, [gla15i] [int] NULL, [gla16c] [varchar](20) NULL, [gla17c] [varchar](5) NULL, [gla18c] [varchar](20) NULL, [gla19f] [float] NULL, [gla20f] [float] NULL, [gla21c] [varchar](20) NULL, [gla22i] [int] NULL, [gla23i] [int] NULL, [gla24c] [varchar](5) NULL, [gla25i] [int] NULL, [gla26i] [int] NULL, [gla27c] [varchar](20) NULL, [gla28f] [float] NULL, [gla29f] [float] NULL, [gla30f] [float] NULL, [gla31f] [float] NULL, [gla32f] [float] NULL, [gla33f] [float] NULL, [gla34i] [int] NULL, [gla35f] [float] NULL, [gla36f] [float] NULL, [gla91c] [varchar](10) NULL, [gla92d] [varchar](8) NULL, [gla93c] [varchar](10) NULL, [gla94d] [varchar](8) NULL, [gla95c] [varchar](10) NULL, [gla96d] [varchar](8) NULL, [gla80d] [varchar](8) NULL, [gla99i] [int] NULL, CONSTRAINT [PK_GLM01] PRIMARY KEY NONCLUSTERED ( [gla00c] ASC, [gla01c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_create_gbm12] Script Date: 06/12/2017 06:19:51 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_create_gbm12] ( @compid varchar(10), -- 公司别 @memid varchar(20) ) as begin create table #gbm12 -- 会员基本资料--自定义值 ( gbl00c varchar(10) not null, -- 公司别 gbl01c varchar(20) not null, -- 会员编号 gbl02c varchar(10) not null, -- 字段编号 gbl03c varchar(30) null, -- 字符值 gbl04i int null, -- 数字值 gbl05c varchar(30) null -- 字段描述 ) insert #gbm12(gbl00c, gbl01c, gbl02c, gbl03c, gbl04i, gbl05c) select @compid, @memid, gbk01c, '', 0, gbk02c from gbm11 delete a from #gbm12 a, gbm12 b where a.gbl00c = b.gbl00c and a.gbl01c = b.gbl01c and a.gbl02c = b.gbl02c insert gbm12(gbl00c, gbl01c, gbl02c, gbl03c, gbl04i, gbl05c) select gbl00c, gbl01c, gbl02c, gbl03c, gbl04i, gbl05c from #gbm12 drop table #gbm12 end GO /****** Object: StoredProcedure [dbo].[upg_card_treatment_comp] Script Date: 06/12/2017 06:19:37 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_card_treatment_comp] ( @compid varchar(20), --门店编号 @chk0 varchar(2), --排除死亡客户 4 @chk1 varchar(2), --排除休眠客户 3 @datefrom varchar(8), --日期 @dateto varchar(8), --日期 @Department varchar(30), --部门 @isChkDepart int --来店次数是否关联部门(1为关联) ) as begin create table #result ( compid varchar(20), -- 门店编号 cardid varchar(20), -- 会员卡号 cardno varchar(20), -- 会员编号 cardname varchar(20), -- 会员卡姓名 unfinishnum int , -- 应完成数 finishnum int , -- 已完成数 gbs04i int, -- 完成状态 juli float, -- 多久没来 laidian float, --最近来店 cishu int, --来店次数 sex varchar(2) , -- 会员性别 birthdate varchar(8) , -- 出生日期 familytel varchar(20), -- 家庭电话 mobiletel varchar(20), -- 手机号码 companytel varchar(20), -- 公司电话 address varchar(40), -- 联系地址 fzren1 varchar(20), -- 负责人1 fzrename1 varchar(20), -- 负责人姓名1 dj1 int, -- 会员等级1 djcode1 varchar(20), -- 会员等级代码1 Depart1 varchar(20), -- 部门编号1 cardsection1 varchar(20), -- 会员归属部门1 fzren2 varchar(20), -- 负责人2 fzrename2 varchar(20), -- 负责人姓名2 dj2 int, -- 会员等级2 djcode2 varchar(20), -- 会员等级代码2 Depart2 varchar(20), -- 部门编号2 cardsection2 varchar(20), -- 会员归属部门2 fzren3 varchar(20), -- 负责人3 fzrename3 varchar(20), -- 负责人姓名3 dj3 int, -- 会员等级3 djcode3 varchar(20), -- 会员等级代码3 Depart3 varchar(20), -- 部门编号3 cardsection3 varchar(20), -- 会员归属部门3 fzren4 varchar(20), -- 负责人4 fzrename4 varchar(20), -- 负责人姓名4 dj4 int, -- 会员等级4 djcode4 varchar(20), -- 会员等级代码4 Depart4 varchar(20), -- 部门编号4 cardsection4 varchar(20), -- 会员归属部门4 ) insert into #result(compid,cardid,cardno,cardname,sex,birthdate,familytel,mobiletel,companytel,address) select gba00c, gba23c, --会员卡号 gba01c, -- 会员编号 gba03c, case gba14i when 0 then '女' when 1 then '男' else '未知' end sex, --性别 gba17d, --出生日期 gba07c, gba08c, gba13c, --公司电话 gba05c --地址 from gbm01 where gba00c in (select gaz02c from gam26 where gaz01c = @compid) and gba23c in ( select gbs02c from gbm19 where gbs00c in (select gaz02c from gam26 where gaz01c = @compid) and (@Department = '' or gbs03c in (select gda01c from gdm01 where gda00c = @compid and gda02c = @Department) ) --部门判断 and gbs01d between @datefrom and @dateto group by gbs02c ) declare @nowdate varchar(10) --当天时间 Select @nowdate = CONVERT(varchar(10), GETDATE(), 112) -- --更新 多久没来 -- update a -- set a.juli = b.gbs10i -- from #result a, gbm19 b -- where a.compid = b.gbs00c -- and a.cardid = b.gbs02c Create table #ggm01_num ( gga00c varchar(10), gga09c varchar(60), --会员卡号 gga94d varchar(10), ggb03c varchar(60), ggb47i int, ) insert into #ggm01_num select gga00c,gga09c, gga94d,ggb03c,ggb47i from ggm01 inner join ggm02 on gga00c = ggb00c and gga01c = ggb01c where gga00c in (select gaz02c from gam26 where gaz01c = @compid) and gga94d between @datefrom and @dateto delete from #ggm01_num where isnull(gga94d,'') = '' --来店次数cishu update a set a.cishu = isnull(b.cishu,0) from #result a, ( select gga00c,gga09c, count(gga94d) cishu from (select gga00c, gga09c, --会员卡号 gga94d from #ggm01_num where ggb47i <> -1 and (@isChkDepart = 0 or (@isChkDepart = 1 and ggb03c in (select gda01c from gdm01 where gda00c = @compid And gda02c = @Department ))) group by gga00c,gga09c,gga94d )as Atable group by gga00c,gga09c ) as b where a.compid = b.gga00c and a.cardid = b.gga09c Create table #gbm19 ( gbs00c varchar(10) not null, -- 门店编号 gbs01d varchar(10) not null, -- 时间 gbs02c varchar(30) not null, -- 会员卡号 gbs03c varchar(30) not null, -- 项目编号 gbs04i int not null, -- 完成状态(0未完成,1已完成,2未预约完成) gbs05f float null, -- 疗程总次数 gbs06f float null, -- 已用次数 gbs07f float null, -- 剩余次数 gbs08f float null, -- 当前完成次数 gbs09c varchar(30) null, -- 消费单号 gbs10i int null, -- 多久没来 gbs11i int null -- 最近来店 ) insert into #gbm19 select gbm19.* from gbm19 inner join gdm01 on gbs00c = gda00c and gbs03c = gda01c where gbs00c in (select gaz02c from gam26 where gaz01c = @compid) and gbs01d between @datefrom and @dateto and gbs04i <> 0 --更新完成数 update a set unfinishnum = num, -- 应完成数 finishnum = gbs08f, -- 已完成数 juli = gbs10i, -- 多久没来 laidian = gbs11i, -- 最近来店 gbs04i = status from #result a,( select gbs00c, gbs02c, max(gbs04i) status, count(gbs00c) num, sum(gbs08f) gbs08f, min(gbs10i) gbs10i, min(gbs11i) gbs11i from #gbm19 where @Department = '' or gbs03c in (select gda01c from gdm01 where gda00c = @compid and gda02c = @Department) group by gbs00c,gbs02c ) b where a.compid = b.gbs00c and a.cardid = b.gbs02c update a set a.juli = b.juli from #result a,(select gga00c,gga09c,-datediff(dd,@dateto,convert(datetime, max(gga94d))) as juli from #ggm01_num where ggb47i <> -1 group by gga00c,gga09c) b where a.compid = b.gga00c and a.cardid = b.gga09c update a set a.laidian = b.laidian from #result a,(select gga00c,gga09c,-datediff(dd,@dateto,convert(datetime, max(gga94d))) as laidian from #ggm01_num group by gga00c,gga09c) b where a.compid = b.gga00c and a.cardid = b.gga09c delete from #result where finishnum = 0 and isnull(juli,'') = '' -- delete from #result where isnull(cishu,0) = 0 and (juli < 7 or (juli > 7 and (convert(int,juli) % 7) !=0)) -- -- delete from #result where isnull(juli,'') = '' --更新会员等级,负责人 declare @gbr02c varchar(30) -- 所属部门 declare @intSn int set @intSn = 1 declare gbm18_cursor cursor for select gbr02c from gbm18 inner join #result on gbr00c = compid and gbr01c = cardno group by gbr02c open gbm18_cursor fetch next from gbm18_cursor into @gbr02c while(@@fetch_status = 0) begin if( @intSn = 1) begin update a set Depart1 = b.gbr02c, fzren1 = b.gbr03c, dj1 = b.gbr04i, cardsection1 = (select gsb03c from gsm02 where gsb00c = b.gbr00c and gsb01c = 'U' and gsb02c = b.gbr02c), fzrename1 = (select haa02c from ham01 where haa00c = b.gbr00c and haa01c = b.gbr03c), --负责人名称 djcode1 = (select gsb03c from gsm02 where gsb00c = b.gbr00c and gsb01c = 'DJ' and gsb02c = b.gbr04i) --会员等级 from #result a, gbm18 b where a.compid = b.gbr00c and a.cardno = b.gbr01c and b.gbr02c = @gbr02c end else if(@intSn = 2) begin update a set Depart2 = b.gbr02c, fzren2 = b.gbr03c, dj2 = b.gbr04i, cardsection2 = (select gsb03c from gsm02 where gsb00c = b.gbr00c and gsb01c = 'U' and gsb02c = b.gbr02c), fzrename2 = (select haa02c from ham01 where haa00c = b.gbr00c and haa01c = b.gbr03c), --负责人名称 djcode2 = (select gsb03c from gsm02 where gsb00c = b.gbr00c and gsb01c = 'DJ' and gsb02c = b.gbr04i) --会员等级 from #result a, gbm18 b where a.compid = b.gbr00c and a.cardno = b.gbr01c and b.gbr02c = @gbr02c end else if(@intSn = 3) begin update a set Depart3 = b.gbr02c, fzren3 = b.gbr03c, dj3 = b.gbr04i, cardsection3 = (select gsb03c from gsm02 where gsb00c = b.gbr00c and gsb01c = 'U' and gsb02c = b.gbr02c), fzrename3 = (select haa02c from ham01 where haa00c = b.gbr00c and haa01c = b.gbr03c), --负责人名称 djcode3 = (select gsb03c from gsm02 where gsb00c = b.gbr00c and gsb01c = 'DJ' and gsb02c = b.gbr04i) --会员等级 from #result a, gbm18 b where a.compid = b.gbr00c and a.cardno = b.gbr01c and b.gbr02c = @gbr02c end else if(@intSn = 4) begin update a set Depart4 = b.gbr02c, fzren4 = b.gbr03c, dj4 = b.gbr04i, cardsection4 = (select gsb03c from gsm02 where gsb00c = b.gbr00c and gsb01c = 'U' and gsb02c = b.gbr02c), fzrename4 = (select haa02c from ham01 where haa00c = b.gbr00c and haa01c = b.gbr03c), --负责人名称 djcode4 = (select gsb03c from gsm02 where gsb00c = b.gbr00c and gsb01c = 'DJ' and gsb02c = b.gbr04i) --会员等级 from #result a, gbm18 b where a.compid = b.gbr00c and a.cardno = b.gbr01c and b.gbr02c = @gbr02c end set @intSn = @intSn + 1 fetch next from gbm18_cursor into @gbr02c end close gbm18_cursor deallocate gbm18_cursor select compid, case isnull(gba08c,'') when '' then max(gcf01c) else gba08c end gba08c, -- 手机号码 max(gcf01c) gcf01c, -- 会员卡号 max(cardno) cardno, -- 会员编号 max(cardname) cardname, -- 会员卡姓名 sum(isnull(unfinishnum,0)) unfinishnum, sum(isnull(finishnum,0)) finishnum, --case sum(isnull(finishnum,0)) when 0 then '未完成' else '已完成' end gbs04i, -- 完成状态(0未完成,1已完成,2未预约完成) case max(gbs04i) when 1 then '已完成' when 2 then '未预约完成' else '未完成' end gbs04i, -- 完成状态(0未完成,1已完成,2未预约完成) min(juli) juli, min(laidian) laidian, max(cishu) cishu, '' remark, --备注 max(sex) sex, max(birthdate) birthdate, max(familytel) familytel, max(companytel) companytel, max(address) address, max(fzren1) fzren1, max(fzrename1) fzrename1, max(dj1) dj1, max(djcode1) djcode1, max(Depart1) Depart1, max(cardsection1) cardsection1, max(fzren2) fzren2, max(fzrename2) fzrename2, max(dj2) dj2, max(djcode2) djcode2, max(Depart2) Depart2, max(cardsection2) cardsection2, max(fzren3) fzren3, max(fzrename3) fzrename3, max(dj3) dj3, max(djcode3) djcode3, max(Depart3) Depart3, max(cardsection3) cardsection3, max(fzren3) fzren4, max(fzrename3) fzrename4, max(dj3) dj4, max(djcode3) djcode4, max(Depart3) Depart4, max(cardsection3) cardsection4 from ( select compid, cardid as gcf01c, -- 会员卡号 cardno, cardname, unfinishnum, finishnum, gbs04i, -- 完成状态 juli, isnull(cishu,0) cishu, isnull(laidian,0) laidian, sex, birthdate, familytel, case isnull(mobiletel,'') when '' then cardid else mobiletel end as gba08c, -- 手机号码 companytel, address, fzren1, fzrename1, dj1, djcode1, Depart1, cardsection1, fzren2, fzrename2, dj2, djcode2, Depart2, cardsection2, fzren3, fzrename3, dj3, djcode3, Depart3, cardsection3, fzren4, fzrename4, dj4, djcode4, Depart4, cardsection4 from #result where (@Department = '' or ( isnull(Depart1,'') = @Department and (@chk0 = '' or isnull(dj1,'') != @chk0) and (@chk1 = '' or isnull(dj1,'') != @chk1) )) or (@Department = '' or ( isnull(Depart2,'') = @Department and (@chk0 = '' or isnull(dj2,'') != @chk0) and (@chk1 = '' or isnull(dj2,'') != @chk1) )) or (@Department = '' or ( isnull(Depart3,'') = @Department and (@chk0 = '' or isnull(dj3,'') != @chk0) and (@chk1 = '' or isnull(dj3,'') != @chk1) )) )as Atable group by compid,gba08c drop table #gbm19 drop table #result drop table #ggm01_num end GO /****** Object: Table [dbo].[b_perf] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[b_perf]( [idd] [int] IDENTITY(1,1) NOT NULL, [mmonth] [varchar](20) NOT NULL, [bran_id] [varchar](20) NOT NULL, [perf_amt] [float] NULL, [lvl] [int] NULL, [parent_id] [varchar](20) NULL, [six_amt] [float] NULL, [last_amt] [float] NULL, [all_amt] [float] NULL, [son_amt] [float] NULL, [gson_amt] [float] NULL, [ggson_amt] [float] NULL, [son_amt_1] [float] NULL, [gson_amt_1] [float] NULL, [ggson_amt_1] [float] NULL, [son_amt_2] [float] NULL, [gson_amt_2] [float] NULL, [ggson_amt_2] [float] NULL, [bonus_1] [float] NULL, [bonus_2] [float] NULL, [bonus_3] [float] NULL, [bonus_4] [float] NULL, [quart_amt] [float] NULL, [year_amt] [float] NULL, [stype] [int] NULL, CONSTRAINT [pk_b_perf] PRIMARY KEY NONCLUSTERED ( [idd] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_select_CourseSubscription] Script Date: 06/12/2017 06:20:39 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_select_CourseSubscription] ( @compid varchar(20),--门店编号 @chk0 int, --排除死亡客户 4 @chk1 int, --排除休眠客户 3 @Department varchar(30), --部门 @day int --提前天数 ) as begin declare @nowdate varchar(10) --当天时间 Select @nowdate = CONVERT(varchar(10), GETDATE(), 112) create table #gcm06_date ( gcf00c varchar(10) not null, --门店编号 gcf01c varchar(20) not null, --卡号 gcf02c varchar(20) not null, --项目编号 gcf05f float null, --总次数 gcf06f float null, --已经使用次数 gcf07f float null, --剩余次数 gcf13i int null, --疗程间隔 gcf19i int null, --是否停用 2停用 1正常 3买产品赠送项目 gcf22i int null, --永久免费 startdate varchar(10) null, -- 开始疗程日期 laidian varchar(10) null, --最近来店 tel varchar(30) null, --手机号 ) --会员卡--疗程 insert into #gcm06_date(gcf00c,gcf01c,gcf02c,gcf05f,gcf06f,gcf07f,gcf13i,gcf19i,gcf22i,startdate,laidian) select gcf00c,gcf01c,gcf02c,sum(isnull(gcf05f,0)),sum(isnull(gcf06f,0)),sum(isnull(gcf07f,0)),min(isnull(gcf13i,7)),min(isnull(gcf19i,0)),min(isnull(gcf22i,0)),'','' from gcm06 where gcf00c = @compid and gcf19i = 1 ----是否停用 2停用 1正常 3买产品赠送项目 and gcf07f > 0 group by gcf00c,gcf01c,gcf02c --更新手机号 update a set tel = b.gba08c from #gcm06_date a,( select gba08c, --手机号 gba23c, --卡号 gba00c --门店 from gbm01 where gba23c in (select gcf01c from #gcm06_date) and isnull(gba08c,'') != '' ) as b where a.gcf00c = b.gba00c and a.gcf01c = b.gba23c --更新疗程日期 --取该项目最后一次疗程日期 update a set a.startdate = b.gga94d from #gcm06_date a,( select gga00c, gga09c, --会员卡号 max(gga94d) gga94d from ggm01 inner join ggm02 on gga00c = ggb00c and gga01c = ggb01c where gga00c = @compid and ggb47i <> -1 group by gga00c,gga09c ) as b where gcf00c = gga00c and gcf01c = gga09c and isnull(tel,'') = '' update a set a.startdate = b.gga94d from #gcm06_date a,( select gga00c, gba08c, --手机号 max(gga94d) gga94d from ggm01 inner join ggm02 on gga00c = ggb00c and gga01c = ggb01c inner join gbm01 on gba00c = gga00c and gga09c = gba23c where gga00c = @compid and ggb47i <> -1 group by gga00c,gba08c ) as b where gcf00c = gga00c and isnull(tel,'') != '' and tel = gba08c --最近来店 update a set a.laidian = b.gga94d from #gcm06_date a,( select gga00c, gba08c, --手机号 max(gga94d) gga94d from ggm01 inner join ggm02 on gga00c = ggb00c and gga01c = ggb01c inner join gbm01 on gba00c = gga00c and gga09c = gba23c where gga00c = @compid and isnull(gga94d,'') <> '' group by gga00c,gba08c ) as b where gcf00c = gga00c and isnull(tel,'') != '' and tel = gba08c --清掉没有日期的数据 delete from #gcm06_date where isnull(startdate,'') = '' or -datediff(dd,@nowdate,convert(datetime,startdate)) = 0 Create table #gbm19_cs ( gbs00c varchar(10) not null, -- 门店编号 gbs01d varchar(10) not null, -- 时间 gbs02c varchar(30) not null, -- 会员卡号 gbs03c varchar(30) not null, -- 项目编号 gbs04i int not null, -- 完成状态(0未完成,1已完成,2未预约完成) gbs05f float null, -- 疗程总次数 gbs06f float null, -- 已用次数 gbs07f float null, -- 剩余次数 gbs08f float null, -- 当前完成次数 gbs09c varchar(30) null, -- 消费单号 gbs10i int null, --多久没来(依疗程来计算) gbs11i int null --最近来店 constraint PK_GBM19 primary key nonclustered(gbs00c,gbs01d,gbs02c,gbs03c) ) -- 更新 insert into #gbm19_cs(gbs00c,gbs01d,gbs02c,gbs03c,gbs04i,gbs05f,gbs06f,gbs07f,gbs08f,gbs09c,gbs10i,gbs11i) select gcf00c, @nowdate, gcf01c, gcf02c, 0, gcf05f, gcf06f, gcf07f, 0, '', -datediff(dd,@nowdate,convert(datetime,startdate)), case isnull(laidian,'') when '' then -datediff(dd,@nowdate,convert(datetime,startdate)) else -datediff(dd,@nowdate,convert(datetime,laidian)) end from #gcm06_date where -datediff(dd,@nowdate,convert(datetime,startdate)) % (7 - @day) = 0 create table #result ( compid varchar(20), -- 门店编号 cardid varchar(20), -- 会员卡号 cardno varchar(20), -- 会员编号 cardname varchar(20), -- 会员卡姓名 --unfinishnum int , -- 应完成数 -- finishnum int , -- 已完成数 -- gbs04i int, -- 完成状态 juli float, -- 多久没来 laidian float, --最近来店 --cishu int, --来店次数 sex varchar(2) , -- 会员性别 birthdate varchar(8) , -- 出生日期 familytel varchar(20), -- 家庭电话 mobiletel varchar(20), -- 手机号码 companytel varchar(20), -- 公司电话 address varchar(40), -- 联系地址 fzren1 varchar(20), -- 负责人1 fzrename1 varchar(20), -- 负责人姓名1 dj1 int, -- 会员等级1 djcode1 varchar(20), -- 会员等级代码1 Depart1 varchar(20), -- 部门编号1 cardsection1 varchar(20), -- 会员归属部门1 fzren2 varchar(20), -- 负责人2 fzrename2 varchar(20), -- 负责人姓名2 dj2 int, -- 会员等级2 djcode2 varchar(20), -- 会员等级代码2 Depart2 varchar(20), -- 部门编号2 cardsection2 varchar(20), -- 会员归属部门2 fzren3 varchar(20), -- 负责人3 fzrename3 varchar(20), -- 负责人姓名3 dj3 int, -- 会员等级3 djcode3 varchar(20), -- 会员等级代码3 Depart3 varchar(20), -- 部门编号3 cardsection3 varchar(20), -- 会员归属部门3 fzren4 varchar(20), -- 负责人4 fzrename4 varchar(20), -- 负责人姓名4 dj4 int, -- 会员等级4 djcode4 varchar(20), -- 会员等级代码4 Depart4 varchar(20), -- 部门编号4 cardsection4 varchar(20), -- 会员归属部门4 ) insert into #result(compid,cardid,cardno,cardname,sex,birthdate,familytel,mobiletel,companytel,address) select gba00c, gba23c, --会员卡号 gba01c, -- 会员编号 gba03c, case gba14i when 0 then '女' when 1 then '男' else '未知' end sex, --性别 gba17d, --出生日期 gba07c, gba08c, gba13c, --公司电话 gba05c --地址 from gbm01 where gba00c = @compid and gba23c in ( select gbs02c from gbm19 where gbs00c in (select gaz02c from gam26 where gaz01c = @compid) and (@Department = '' or gbs03c in (select gda01c from gdm01 where gda00c = @compid and gda02c = @Department) ) --部门判断 group by gbs02c ) --清除已经在今日提醒中存在的 delete from #result where cardid in (select gbs02c from gbm19 where gbs00c = compid and gbs01d = @nowdate) --更新完成数 update a set juli = gbs10i, -- 多久没来 laidian = gbs11i -- 最近来店 from #result a,( select gbs00c, gbs02c, min(gbs10i) gbs10i, min(gbs11i) gbs11i from #gbm19_cs where @Department = '' or gbs03c in (select gda01c from gdm01 where gda00c = @compid and gda02c = @Department) group by gbs00c,gbs02c ) b where a.compid = b.gbs00c and a.cardid = b.gbs02c delete from #result where isnull(juli,'') = '' --更新会员等级,负责人 declare @gbr02c varchar(30) -- 所属部门 declare @intSn int set @intSn = 1 declare gbm18_cursor cursor for select gbr02c from gbm18 inner join #result on gbr00c = compid and gbr01c = cardno group by gbr02c open gbm18_cursor fetch next from gbm18_cursor into @gbr02c while(@@fetch_status = 0) begin if( @intSn = 1) begin update a set Depart1 = b.gbr02c, fzren1 = b.gbr03c, dj1 = b.gbr04i, cardsection1 = (select gsb03c from gsm02 where gsb00c = b.gbr00c and gsb01c = 'U' and gsb02c = b.gbr02c), fzrename1 = (select haa02c from ham01 where haa00c = b.gbr00c and haa01c = b.gbr03c), --负责人名称 djcode1 = (select gsb03c from gsm02 where gsb00c = b.gbr00c and gsb01c = 'DJ' and gsb02c = b.gbr04i) --会员等级 from #result a, gbm18 b where a.compid = b.gbr00c and a.cardno = b.gbr01c and b.gbr02c = @gbr02c end else if(@intSn = 2) begin update a set Depart2 = b.gbr02c, fzren2 = b.gbr03c, dj2 = b.gbr04i, cardsection2 = (select gsb03c from gsm02 where gsb00c = b.gbr00c and gsb01c = 'U' and gsb02c = b.gbr02c), fzrename2 = (select haa02c from ham01 where haa00c = b.gbr00c and haa01c = b.gbr03c), --负责人名称 djcode2 = (select gsb03c from gsm02 where gsb00c = b.gbr00c and gsb01c = 'DJ' and gsb02c = b.gbr04i) --会员等级 from #result a, gbm18 b where a.compid = b.gbr00c and a.cardno = b.gbr01c and b.gbr02c = @gbr02c end else if(@intSn = 3) begin update a set Depart3 = b.gbr02c, fzren3 = b.gbr03c, dj3 = b.gbr04i, cardsection3 = (select gsb03c from gsm02 where gsb00c = b.gbr00c and gsb01c = 'U' and gsb02c = b.gbr02c), fzrename3 = (select haa02c from ham01 where haa00c = b.gbr00c and haa01c = b.gbr03c), --负责人名称 djcode3 = (select gsb03c from gsm02 where gsb00c = b.gbr00c and gsb01c = 'DJ' and gsb02c = b.gbr04i) --会员等级 from #result a, gbm18 b where a.compid = b.gbr00c and a.cardno = b.gbr01c and b.gbr02c = @gbr02c end else if(@intSn = 4) begin update a set Depart4 = b.gbr02c, fzren4 = b.gbr03c, dj4 = b.gbr04i, cardsection4 = (select gsb03c from gsm02 where gsb00c = b.gbr00c and gsb01c = 'U' and gsb02c = b.gbr02c), fzrename4 = (select haa02c from ham01 where haa00c = b.gbr00c and haa01c = b.gbr03c), --负责人名称 djcode4 = (select gsb03c from gsm02 where gsb00c = b.gbr00c and gsb01c = 'DJ' and gsb02c = b.gbr04i) --会员等级 from #result a, gbm18 b where a.compid = b.gbr00c and a.cardno = b.gbr01c and b.gbr02c = @gbr02c end set @intSn = @intSn + 1 fetch next from gbm18_cursor into @gbr02c end close gbm18_cursor deallocate gbm18_cursor select compid, case isnull(gba08c,'') when '' then max(gcf01c) else gba08c end gba08c, -- 手机号码 max(gcf01c) gcf01c, -- 会员卡号 max(cardno) cardno, -- 会员编号 max(cardname) cardname, -- 会员卡姓名 min(juli) juli, min(laidian) laidian, max(sex) sex, max(birthdate) birthdate, max(familytel) familytel, max(companytel) companytel, max(address) address, max(fzren1) fzren1, max(fzrename1) fzrename1, max(dj1) dj1, max(djcode1) djcode1, max(Depart1) Depart1, max(cardsection1) cardsection1, max(fzren2) fzren2, max(fzrename2) fzrename2, max(dj2) dj2, max(djcode2) djcode2, max(Depart2) Depart2, max(cardsection2) cardsection2, max(fzren3) fzren3, max(fzrename3) fzrename3, max(dj3) dj3, max(djcode3) djcode3, max(Depart3) Depart3, max(cardsection3) cardsection3, max(fzren3) fzren4, max(fzrename3) fzrename4, max(dj3) dj4, max(djcode3) djcode4, max(Depart3) Depart4, max(cardsection3) cardsection4 from ( select compid, cardid as gcf01c, -- 会员卡号 cardno, cardname, juli, isnull(laidian,0) laidian, sex, birthdate, familytel, case isnull(mobiletel,'') when '' then cardid else mobiletel end as gba08c, -- 手机号码 companytel, address, fzren1, fzrename1, dj1, djcode1, Depart1, cardsection1, fzren2, fzrename2, dj2, djcode2, Depart2, cardsection2, fzren3, fzrename3, dj3, djcode3, Depart3, cardsection3, fzren4, fzrename4, dj4, djcode4, Depart4, cardsection4 from #result where (@Department = '' or ( isnull(Depart1,'') = @Department and (@chk0 = '' or isnull(dj1,'') != @chk0) and (@chk1 = '' or isnull(dj1,'') != @chk1) )) or (@Department = '' or ( isnull(Depart2,'') = @Department and (@chk0 = '' or isnull(dj2,'') != @chk0) and (@chk1 = '' or isnull(dj2,'') != @chk1) )) or (@Department = '' or ( isnull(Depart3,'') = @Department and (@chk0 = '' or isnull(dj3,'') != @chk0) and (@chk1 = '' or isnull(dj3,'') != @chk1) )) )as Atable group by compid,gba08c drop table #gbm19_cs drop table #result end GO /****** Object: Table [dbo].[loop_event_desc] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[loop_event_desc]( [compid] [varchar](10) NOT NULL, [idd] [int] IDENTITY(1,1) NOT NULL, [cardid] [varchar](20) NOT NULL, [startflag] [int] NULL, [periodnum] [int] NULL, [periodunit] [int] NULL, [loop_times] [int] NULL, [addflag] [int] NULL, [peramt] [float] NULL, [acctid] [int] NULL, [endflag] [int] NULL, [startdate] [varchar](20) NULL, [done_times] [int] NULL, [billtype] [varchar](10) NULL, [billid] [varchar](20) NULL, PRIMARY KEY CLUSTERED ( [idd] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gim22] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gim22]( [id] [numeric](18, 0) IDENTITY(1,1) NOT NULL, [did] [int] NOT NULL, [gio00c] [varchar](10) NOT NULL, [gio01c] [varchar](20) NOT NULL, [gio02c] [varchar](20) NULL, [gio05c] [varchar](20) NOT NULL, [gio06f] [float] NOT NULL, [gio07f] [float] NULL, [gio08c] [varchar](20) NULL, [gio09f] [float] NULL, [gio10f] [float] NULL, [gio11c] [varchar](1) NULL, [gio12c] [varchar](50) NULL, [gio13c] [varchar](50) NULL, [gio14c] [varchar](50) NULL, [gio15c] [varchar](50) NULL, [gio16c] [varchar](50) NULL, [gio17d] [varchar](8) NULL, [gio18t] [varchar](6) NULL, [gio19c] [varchar](10) NULL, [gio20c] [varchar](20) NULL, CONSTRAINT [PK_GIM22] PRIMARY KEY CLUSTERED ( [id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gzm05] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gzm05]( [gze00c] [varchar](10) NOT NULL, [gze01c] [varchar](20) NOT NULL, [gze02f] [float] NOT NULL, [gze03f] [float] NULL, [gze04i] [int] NULL, [gze05c] [varchar](8) NULL, [gze06c] [varchar](8) NULL, CONSTRAINT [PK_GZM05] PRIMARY KEY CLUSTERED ( [gze00c] ASC, [gze01c] ASC, [gze02f] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[suggest] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[suggest]( [sid] [numeric](19, 0) IDENTITY(1,1) NOT NULL, [stype] [varchar](1) NULL, [sfromid] [varchar](20) NULL, [sfromname] [varchar](20) NULL, [sfromphone] [varchar](20) NULL, [sfromdate] [datetime] NULL, [sfromcomp] [varchar](10) NULL, [stocomp] [varchar](10) NULL, [stoempid] [varchar](20) NULL, [stoempname] [varchar](20) NULL, [stodesc] [varchar](500) NULL, [sreplydesc] [varchar](500) NULL, [sreplyid] [varchar](20) NULL, [sreplyname] [varchar](20) NULL, [sreplydate] [datetime] NULL, CONSTRAINT [PK_suggest] PRIMARY KEY CLUSTERED ( [sid] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gtc07] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gtc07]( [gti00c] [varchar](10) NOT NULL, [gti01i] [int] IDENTITY(1,1) NOT NULL, [gti02d] [varchar](8) NOT NULL, [gti03c] [varchar](20) NULL, [gti04c] [varchar](20) NULL, [gti05c] [varchar](20) NULL, [gti06f] [float] NULL, [gti07f] [float] NULL, [gti08c] [varchar](20) NULL, [gti09c] [varchar](20) NULL, [gti10f] [float] NULL, [gti11f] [float] NULL, [gti12i] [int] NULL, [gti13c] [varchar](20) NULL, [gti14i] [int] NULL, [gti17c] [varchar](5) NULL, [gti18f] [float] NULL, [gti19c] [varchar](5) NULL, [gti20f] [float] NULL, [gti21c] [varchar](5) NULL, [gti22f] [float] NULL, [gti23c] [varchar](5) NULL, [gti24f] [float] NULL, CONSTRAINT [PK_GTC07] PRIMARY KEY CLUSTERED ( [gti00c] ASC, [gti01i] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[recover_ggm03] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[recover_ggm03]( [id] [int] NOT NULL, [ggc00c] [varchar](10) NOT NULL, [ggc01c] [varchar](20) NOT NULL, [ggc02f] [float] NOT NULL, [ggc03c] [varchar](20) NULL, [ggc04c] [varchar](5) NULL, [ggc05f] [float] NULL, [ggc06c] [varchar](5) NULL, [ggc07f] [float] NULL, [ggc08f] [float] NULL, [ggc09f] [float] NULL, [ggc10f] [float] NULL, [ggc11f] [float] NULL, [ggc12c] [varchar](20) NULL, [ggc13f] [float] NULL, [ggc14f] [float] NULL, [ggc15c] [varchar](20) NULL, [ggc16f] [float] NULL, [ggc17c] [varchar](20) NULL, [ggc18f] [float] NULL, [ggc19c] [varchar](128) NULL, [ggc20f] [float] NULL, [ggc21f] [float] NULL, [ggc22f] [float] NULL, [ggc23f] [float] NULL, [ggc24f] [float] NULL, [ggc25f] [float] NULL, [ggc26f] [float] NULL, [ggc27c] [varchar](5) NULL, [ggc28f] [float] NULL, [ggc29f] [float] NULL, [ggc30f] [float] NULL, [ggc31c] [varchar](20) NULL, [ggc32i] [int] NULL, [ggc33d] [varchar](20) NULL, [ggc34c] [varchar](20) NULL, [ggc35i] [int] NULL, [ggc36i] [int] NULL, [ggc37f] [float] NULL, [ggc38f] [float] NULL, [ggc81c] [varchar](10) NULL, [ggc82c] [varchar](10) NULL, [ggc83c] [varchar](10) NULL, [ggc48i] [int] NULL, [ggc49f] [float] NULL, [ggc82i] [int] NULL, [ggc83f] [float] NULL, CONSTRAINT [PK_recover_GGM03] PRIMARY KEY CLUSTERED ( [id] ASC, [ggc00c] ASC, [ggc01c] ASC, [ggc02f] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gdm01] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gdm01]( [gda00c] [varchar](10) NOT NULL, [gda01c] [varchar](20) NOT NULL, [gda02c] [varchar](30) NULL, [gda03c] [nvarchar](100) NULL, [gda04c] [varchar](5) NULL, [gda05i] [int] NULL, [gda06c] [varchar](10) NULL, [gda07c] [char](1) NULL, [gda08c] [varchar](40) NULL, [gda09c] [varchar](5) NULL, [gda10f] [float] NULL, [gda11f] [float] NULL, [gda12f] [float] NULL, [gda13c] [varchar](10) NULL, [gda14i] [int] NULL, [gda15c] [varchar](128) NULL, [gda16i] [int] NULL, [gda17i] [tinyint] NULL, [gda18f] [float] NULL, [gda19i] [int] NULL, [gda20i] [int] NULL, [gda21t] [varchar](20) NULL, [gda22f] [float] NULL, [gda23f] [float] NULL, [gda24f] [float] NULL, [gda25f] [float] NULL, [gda26f] [float] NULL, [gda27f] [float] NULL, [gda28f] [float] NULL, [gda29f] [float] NULL, [gda30f] [float] NULL, [gda31f] [float] NULL, [gda32f] [float] NULL, [gda33f] [float] NULL, [gda34i] [float] NULL, [gda35i] [float] NULL, [gda36i] [float] NULL, [gda37i] [float] NULL, [gda38c] [varchar](200) NULL, [gda38i] [int] NULL, [gda39d] [datetime] NULL, [gda11i] [int] NULL, [gda98c] [varchar](100) NULL, [gda99c] [varchar](500) NULL, [gda40i] [int] NULL, CONSTRAINT [PK_GDM01] PRIMARY KEY CLUSTERED ( [gda00c] ASC, [gda01c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[T_ggm_delete_error] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[T_ggm_delete_error]( [compid] [varchar](50) NOT NULL, [billid] [varchar](20) NOT NULL, [cardid] [varchar](20) NOT NULL, [errorcode] [varchar](50) NOT NULL, [step] [varchar](1000) NOT NULL ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_searchgood_saleinfo] Script Date: 06/12/2017 06:20:39 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_searchgood_saleinfo] @compid varchar(10), @fromgoods varchar(20), @togoods varchar(20), @fromdate varchar(8), @todate varchar(8) as begin --因为报表中出库是按照出库类型来划分的,所以出库的地方没有从gfm04中取数据了 --从最原始的单据去的数据了 --并且4-耗用 5-卡销售套餐 6-充值套餐 都计算在销售里面了 declare @curryear varchar(4) set @curryear = substring(@fromdate, 1, 4) create table #allgoods --所有产品的信息 ( id int identity not null, compid varchar(10) null, --公司别 goodsno varchar(20) null, --产品编号 billno varchar(20) null, --单号 type varchar(2) null, --类型 入库 销售 quantity float null, date varchar(8) null, ) create nonclustered index goodsno on #allgoods(compid, goodsno, billno, type) create table #yeargood --所有产品的年初库存 ( id int identity not null, compid varchar(10) null, goodsno varchar(20) null, --产品编号 type varchar(2) null, --类型 入库 销售 quantity float null, date varchar(8) null, ) create table #monthgood --所以产品的月初库存 ( id int identity not null, compid varchar(10) null, --公司别 goodsno varchar(20) null, --产品编号 type varchar(2) null, --类型 入库 销售 quantity float null, --数量 date varchar(8) null, --日期 ) create table #alllessgood --所有缺货 ( id int identity not null, compid varchar(10) null, goodsno varchar(20) null, quantity float null, date varchar(8) null, billno varchar(20) null, ) create table #sumlessgood ( id int identity not null, compid varchar(10) null, goodsno varchar(20) null, quantity float null, ) create table #getgood --已经领取的货物 ( id int identity not null, compid varchar(10) null, goodsno varchar(20) null, quantity float null, date varchar(8) null, ) create table #result ( goodsno varchar(20) not null, --产品编号 yearsum float null, --年初库存 monthsum float null, --月初库存 incurrday float null, --当天入库 incurrmonth float null, --当月入库 insum float null, --累计 salecurrday float null, --当天销售 salecurrmonth float null, --当月销售 salesum float null, --累计 saleless float null, --缺货 endsum float null, --期末库存 factsum float null, --实际数量 ) insert into #allgoods(compid, goodsno, type, quantity, date, billno) select gfd00c, gfd04c, gfd01c, gfd05f, gfd07c, gfd02c from gfm04 with (nolock) where gfd00c = @compid and (gfd04c between @fromgoods and @togoods or @fromgoods = '*') and substring(gfd07c, 1, 4) = @curryear and isnull(gfd01c, '')<>'2' ----把这个产品的年初库存insert insert into #yeargood(compid, goodsno, type, quantity, date) select a.compid, a.goodsno, a.type, a.quantity, substring(a.date, 1, 4) from #allgoods a where substring(a.date, 1, 4) = @curryear group by a.compid, a.goodsno, a.type, substring(a.date, 1, 4), a.quantity, a.billno having a.billno = (select top 1 b.billno from #allgoods b where a.compid = b.compid and a.type = b.type and a.goodsno = b.goodsno and substring(a.date, 1, 4) = substring(b.date, 1, 4)) order by a.compid, a.goodsno, a.type, substring(a.date, 1, 4), a.quantity, a.billno --把产品的月初库存 insert into #monthgood(compid, goodsno, type, quantity, date) select a.compid, a.goodsno, a.type, a.quantity, substring(a.date, 1, 6) from #allgoods a where substring(a.date, 1, 6) = @fromdate group by a.compid, a.goodsno, a.type, substring(a.date, 1, 6), a.quantity, a.billno having a.billno = (select top 1 b.billno from #allgoods b where a.compid = b.compid and a.type = b.type and a.goodsno = b.goodsno and substring(a.date, 1, 6) = substring(b.date, 1, 6)) order by a.compid, a.goodsno, a.type, substring(a.date, 1, 6), a.quantity, a.billno --插入所以缺货的单子 insert into #alllessgood(compid, goodsno, quantity, date, billno) select ggc00c, ggc03c, ggc05f, gga80d, gga01c from ggm01, ggm03 where gga00c = ggc00c and gga01c = ggc01c and gga00c = @compid and (ggc03c between @fromgoods and @togoods or @fromgoods = '*') and gga80d between @fromdate and @todate and gga80d <= @todate and ggc36i = 2 insert into #sumlessgood(compid, goodsno, quantity) select compid, goodsno, sum(quantity) from #alllessgood group by compid, goodsno insert into #getgood(compid, goodsno, quantity) select a.compid, a.goodsno, sum(gip09i) from #alllessgood a, gim23, gim25 where a.compid = gim00c and a.billno = gim12c and gim11i = 1 and gip00c = gim00c and gip010c = gim010c and (gip04i = 1 or gip04i = 2) group by a.compid, a.goodsno update b set b.quantity = isnull(b.quantity, 0) - isnull(a.quantity, 0) from #sumlessgood b left outer join #getgood a on b.compid = a.compid and b.goodsno = a.goodsno insert into #result(goodsno, yearsum, monthsum) select distinct a.goodsno, a.quantity, b.quantity from #yeargood a left outer join #monthgood b on a.compid = b.compid and a.goodsno = b.goodsno group by a.goodsno, a.quantity, b.quantity --跟新当天入库 update a set a.incurrday = (select sum(b.quantity) from #allgoods b where a.goodsno = b.goodsno and b.type = '1' and b.date = @fromdate group by b.goodsno ) from #result a --跟新当月入库 update a set a.incurrmonth = (select sum(b.quantity) from #allgoods b where a.goodsno = b.goodsno and substring(b.date, 1, 6) = substring(@fromdate, 1, 6) and (b.type = '1') group by b.goodsno ) from #result a --跟新累计入库 update a set a.insum = (select sum(b.quantity)from #allgoods b where a.goodsno = b.goodsno and b.date <= @fromdate and (b.type = '1') group by b.goodsno ) from #result a --跟新当天销售 update a set a.salecurrday = (select sum(b.quantity) from #allgoods b where a.goodsno = b.goodsno and (b.type = '3' or b.type = '4' or b.type = '5' or b.type = '6') and b.date = @fromdate group by b.goodsno ) from #result a --跟新当月销售 update a set a.salecurrmonth = (select sum(b.quantity) from #allgoods b where a.goodsno = b.goodsno and substring(b.date, 1, 6) = substring(@fromdate, 1, 6) and (b.type = '3' or b.type = '4' or b.type = '5' or b.type = '6') group by b.goodsno ) from #result a --跟新累计销售 update a set a.salesum = (select sum(b.quantity) from #allgoods b where a.goodsno = b.goodsno and (b.type = '3' or b.type = '4' or b.type = '5' or b.type = '6') group by b.goodsno) from #result a --跟新缺货 update a set a.saleless = b.quantity from #result a, #sumlessgood b where b.compid = @compid and a.goodsno = b.goodsno --跟新期末库存 update a set a.endsum = (select sum(b.quantity)from #allgoods b where a.goodsno = b.goodsno and substring(b.date, 1, 4) = substring(@fromdate, 1, 4) group by b.goodsno ) from #result a select * from #result end GO /****** Object: Table [dbo].[gicm13] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gicm13]( [gick00c] [varchar](10) NOT NULL, [gick01i] [int] IDENTITY(1,1) NOT NULL, [gick02i] [int] NOT NULL, [gick03c] [varchar](20) NULL, [gick04c] [varchar](20) NULL, [gick05c] [varchar](20) NULL, [gick06i] [int] NULL, [gick07d] [varchar](8) NULL, [gick08i] [int] NULL, CONSTRAINT [PK_GICM13] PRIMARY KEY CLUSTERED ( [gick00c] ASC, [gick01i] ASC, [gick02i] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gam13] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gam13]( [gan01c] [varchar](5) NOT NULL, [gan02c] [varchar](10) NOT NULL, [gan03c] [char](1) NULL, [gan04c] [char](1) NULL, [gan05c] [char](1) NULL, [gan06c] [char](1) NULL, [gan07c] [char](1) NULL, [gan08c] [char](1) NULL, [gan09c] [char](1) NULL, [gan10c] [char](1) NULL, [gan11c] [varchar](10) NOT NULL, [gan12c] [char](1) NULL, [gan13c] [char](1) NULL, [gan14c] [char](1) NULL, [gan15c] [char](1) NULL, [gan16c] [char](1) NULL, [gan19c] [char](1) NULL, CONSTRAINT [PK_GAM13] PRIMARY KEY CLUSTERED ( [gan01c] ASC, [gan02c] ASC, [gan11c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gpm02] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gpm02]( [gpb00c] [varchar](10) NOT NULL, [gpb01c] [varchar](20) NOT NULL, [gpb02f] [float] NULL, [gpb03c] [varchar](5) NOT NULL, [gpb04c] [varchar](20) NULL, [gpb05c] [varchar](40) NULL, CONSTRAINT [PK_GPM02] PRIMARY KEY CLUSTERED ( [gpb00c] ASC, [gpb01c] ASC, [gpb03c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[sp_queryInfo] Script Date: 06/12/2017 06:19:21 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[sp_queryInfo] @condition varchar(100) as begin declare @queryInfoSql varchar(200) set @queryInfoSql = N'select * from wip ' + @condition execute sp_executesql @queryInfoSql end GO /****** Object: Table [dbo].[hbm02] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[hbm02]( [hbb00c] [varchar](10) NOT NULL, [hbb01c] [varchar](10) NOT NULL, [hbb02c] [varchar](5) NOT NULL, [hbb03c] [varchar](5) NOT NULL, [hbb04c] [varchar](10) NULL, [hbb05f] [float] NULL, [hbb06c] [varchar](5) NULL, CONSTRAINT [PK_HBM02] PRIMARY KEY CLUSTERED ( [hbb00c] ASC, [hbb01c] ASC, [hbb02c] ASC, [hbb03c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gbm18] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gbm18]( [gbr00c] [varchar](10) NOT NULL, [gbr01c] [varchar](30) NOT NULL, [gbr02c] [varchar](10) NOT NULL, [gbr03c] [varchar](10) NULL, [gbr04i] [int] NULL, [gbr05i] [int] NULL, [gbr06c] [varchar](300) NULL, [gbr90c] [varchar](10) NULL, [gbr91d] [varchar](10) NULL, CONSTRAINT [PK_GBM18] PRIMARY KEY NONCLUSTERED ( [gbr00c] ASC, [gbr01c] ASC, [gbr02c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gtc09] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gtc09]( [gti00c] [varchar](10) NOT NULL, [gti01c] [varchar](20) NOT NULL, [gti02c] [varchar](20) NOT NULL, [gti03c] [varchar](20) NOT NULL, [gti04c] [varchar](20) NOT NULL, [gti05f] [float] NULL, [gti06f] [float] NULL, [gti07f] [float] NULL, [gti08c] [varchar](20) NULL, [gti09f] [float] NULL, [gti10i] [int] NULL, [gti11i] [int] NULL, [gti12c] [varchar](2) NULL, CONSTRAINT [PK_GTC09] PRIMARY KEY CLUSTERED ( [gti00c] ASC, [gti01c] ASC, [gti02c] ASC, [gti03c] ASC, [gti04c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[hcm01] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[hcm01]( [hca00c] [varchar](10) NOT NULL, [hca01c] [varchar](20) NOT NULL, [hca02d] [varchar](8) NULL, [hca03c] [varchar](6) NULL, [hca04i] [int] NULL, [hca05d] [varchar](8) NULL, [hca06d] [varchar](8) NULL, [hca07i] [int] NULL, [hca08i] [int] NULL, [hca09i] [int] NULL, [hca10f] [float] NULL, [hca11f] [float] NULL, [hca91c] [varchar](10) NULL, [hca92d] [varchar](8) NULL, [hca93c] [varchar](10) NULL, [hca94d] [varchar](8) NULL, [id] [numeric](18, 0) IDENTITY(1,1) NOT NULL, CONSTRAINT [PK_HCM01] PRIMARY KEY CLUSTERED ( [hca00c] ASC, [hca01c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gdm02] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gdm02]( [gdb00c] [varchar](10) NOT NULL, [gdb01c] [varchar](20) NOT NULL, [gdb02d] [varchar](8) NOT NULL, [gdb03t] [varchar](6) NOT NULL, [gdb04t] [varchar](6) NOT NULL, [gdb05c] [varchar](5) NOT NULL, [gdb06c] [varchar](20) NULL, [gdb07c] [varchar](20) NULL, [gdb08c] [varchar](20) NULL, CONSTRAINT [PK_GDM02] PRIMARY KEY CLUSTERED ( [gdb00c] ASC, [gdb01c] ASC, [gdb02d] ASC, [gdb03t] ASC, [gdb04t] ASC, [gdb05c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[work_detail] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[work_detail]( [id] [int] IDENTITY(1,1) NOT NULL, [person_id] [varchar](20) NOT NULL, [action_id] [tinyint] NOT NULL, [srvdate] [varchar](10) NOT NULL, [code] [varchar](20) NOT NULL, [name] [varchar](200) NULL, [payway] [varchar](60) NULL, [amt] [float] NULL, [quan] [float] NULL, [cost] [float] NULL, [comm] [float] NULL, [amt2] [float] NULL, [amt3] [float] NULL, [cardrate] [float] NULL, [stdprice] [float] NULL, [prj_type] [varchar](20) NULL, [share_rate] [float] NULL, [cls_flag] [int] NULL, [gender] [int] NULL, [billid] [varchar](20) NULL, [paycode] [varchar](20) NULL, [ex_rate] [float] NULL, [ggm02_seq] [float] NULL, [room_id] [varchar](20) NULL, [compid] [varchar](10) NULL, [spid] [int] NULL, [cardid] [varchar](60) NULL, [memid] [varchar](60) NULL, [comp_ascription] [varchar](10) NULL, CONSTRAINT [PK_work_detail] PRIMARY KEY NONCLUSTERED ( [id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_decisionAnalysis_bydays] Script Date: 06/12/2017 06:19:57 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_decisionAnalysis_bydays] ( @analysis_object int, @analysis_indicator int, @analysis_subject varchar(10), @analysis_keti varchar(10), @fromdate varchar(8), @todate varchar(8), @compid varchar(10) ) as begin create table #project_yeji_month --项目单月业绩 ( idd int identity(1, 1) not null, compid varchar(10) not null, projectid varchar(20) not null, amount float null, quantity float null, ymonth varchar(8) null --年月 ) create table #project_class_yeji_month --项目类别单月业绩 ( idd int identity(1, 1) not null, compid varchar(10) not null, project_class varchar(10) not null, amount float null, ymonth varchar(8) null ) create table #card_yeji_month --卡销售充值单月业绩 ( idd int identity(1, 1) not null, compid varchar(10) not null, amount float null, ymonth varchar(8) null ) create table #card_class_yeji_month --卡类别销售充值单月业绩 ( idd int identity(1, 1) not null, compid varchar(10) not null, card_class varchar(10) not null, amount float null, ymonth varchar(8) null ) create table #commodity_yeji_month --产品单月销售业绩 ( idd int identity(1, 1) not null, compid varchar(10) not null, amount float null, quantity float null, ymonth varchar(8) null ) create table #total_yeji_month --店单月总业绩 ( idd int identity(1, 1) not null, compid varchar(10) not null, amount float null, ymonth varchar(8) null ) if (@analysis_object = 0) --分析对象为‘公司/店’ begin if (@analysis_indicator = 0)--指标为总业绩 begin create table #temp1 ( idd int identity(1, 1) not null, compid varchar(10) not null, amount float null, ymonth varchar(8) null ) insert into #temp1(compid, amount, ymonth) select gga00c, sum(gga16f), gga80d from ggm01 where gga00c = @analysis_subject and gga80d >= @fromdate and gga80d <= @todate group by gga00c, gga80d insert into #temp1(compid, amount, ymonth) select gna00c, sum(gnb08f), gna80d from gnm01, gnm02 where gna00c = gnb00c and gna01c = gnb01c and gna00c = @analysis_subject and gna80d >= @fromdate and gna80d <= @todate group by gna00c, gna80d insert into #temp1(compid, amount, ymonth) select gcl00c, sum(gcl07f), gcl80d from gcm10 where gcl00c = @analysis_subject and gcl06i = 0 and gcl80d >= @fromdate and gcl80d <= @todate group by gcl00c, gcl80d insert into #total_yeji_month(compid, amount, ymonth) select #temp1.compid, sum(#temp1.amount), #temp1.ymonth from #temp1 group by #temp1.compid, #temp1.ymonth drop table #temp1 select amount, ymonth from #total_yeji_month order by ymonth asc delete #total_yeji_month end else if (@analysis_indicator = 1)--指标为项目业绩 begin insert into #project_yeji_month(compid, projectid, amount, quantity, ymonth) select gga00c, ggb03c, sum(ggb11f), sum(ggb05f), gga80d from ggm01 with (nolock), ggm02 with (nolock) where gga00c = ggb00c and gga01c = ggb01c and gga00c = @analysis_subject and ggb03c = @analysis_keti and gga80d >= @fromdate and gga80d <= @todate group by gga00c, ggb03c, gga80d select amount, ymonth from #project_yeji_month order by ymonth asc delete #project_yeji_month end else if (@analysis_indicator = 2)--指标为某类项目业绩 begin insert into #project_class_yeji_month(compid, project_class, amount, ymonth) select gga00c, gda13c, sum(ggb11f), gga80d from ggm01, ggm02 with (nolock), gdm01 with (nolock) where gga00c = ggb00c and gga00c = gda00c and gga01c = ggb01c and ggb03c = gda01c and gga00c = @analysis_subject and gda13c = @analysis_keti and gga80d >= @fromdate and gga80d <= @todate group by gga00c, gda13c, gga80d select amount, ymonth from #project_class_yeji_month order by ymonth asc delete #project_class_yeji_month end else if (@analysis_indicator = 3)--指标为卡销售充值业绩 begin create table #temp2 ( idd int identity(1, 1) not null, compid varchar(10) not null, amount float null, ymonth varchar(8) null ) insert into #temp2(compid, amount, ymonth) select gna00c, sum(gnb08f), gna80d from gnm01, gnm02 where gna00c = gnb00c and gna01c = gnb01c and gna00c = @analysis_subject and gna80d >= @fromdate and gna80d <= @todate group by gna00c, gna80d insert into #temp2(compid, amount, ymonth) select gcl00c, sum(gcl07f), gcl80d from gcm10 where gcl00c = @analysis_subject and gcl06i = 0 and gcl80d >= @fromdate and gcl80d <= @todate group by gcl00c, gcl80d insert into #card_yeji_month(compid, amount, ymonth) select #temp2.compid, sum(#temp2.amount), #temp2.ymonth from #temp2 group by #temp2.compid, #temp2.ymonth drop table #temp2 select amount, ymonth from #card_yeji_month order by ymonth asc delete #card_yeji_month end else if (@analysis_indicator = 4)--指标为某类卡销售充值业绩 begin create table #temp3 ( idd int identity(1, 1) not null, compid varchar(10) not null, card_class varchar(10) not null, amount float null, ymonth varchar(8) null ) insert into #temp3(compid, card_class, amount, ymonth) select gna00c, gnb02c, sum(gnb08f), gna80d from gnm01, gnm02 where gna00c = gnb00c and gna01c = gnb01c and gna00c = @analysis_subject and gnb02c = @analysis_keti and gna80d >= @fromdate and gna80d <= @todate group by gna00c, gnb02c, gna80d insert into #temp3(compid, card_class, amount, ymonth) select gcl00c, gca02c, sum(gcl07f), gcl80d from gcm10, gcm01 where gcl00c = gca00c and gcl03c = gca01c and gca02c = @analysis_keti and gcl00c = @analysis_subject and gcl06i = 0 and gcl80d >= @fromdate and gcl80d <= @todate group by gcl00c, gca02c, gcl80d insert into #card_class_yeji_month(compid, card_class, amount, ymonth) select #temp3.compid, #temp3.card_class, sum(#temp3.amount), #temp3.ymonth from #temp3 group by #temp3.compid, #temp3.card_class, #temp3.ymonth drop table #temp3 select amount, ymonth from #card_class_yeji_month order by ymonth asc delete #card_class_yeji_month end else if (@analysis_indicator = 5)--指标为产品销售业绩 begin insert into #commodity_yeji_month(compid, amount, quantity, ymonth) select gga00c, sum(ggc11f), sum(ggc05f), gga80d from ggm01, ggm03 where gga00c = ggc00c and gga01c = ggc01c and gga00c = @analysis_subject and gga80d >= @fromdate and gga80d <= @todate group by gga00c, gga80d select amount, ymonth from #commodity_yeji_month order by ymonth asc delete #commodity_yeji_month end end else if (@analysis_object = 1)--分析对象为‘员工’ begin if (@analysis_indicator = 0)--指标为总业绩 begin create table #temp4 ( idd int identity(1, 1) not null, compid varchar(10) not null, amount float null, ymonth varchar(8) null ) insert into #temp4(compid, amount, ymonth) select gga00c, sum(case when isnull(ggb12c, '') = @analysis_subject then ggb20f when isnull(ggb13c, '') = @analysis_subject then ggb21f when isnull(ggb14c, '') = @analysis_subject then ggb22f when isnull(ggb26c, '') = @analysis_subject then ggb35f end), gga80d from ggm01 with (nolock), ggm02 with (nolock) where gga00c = ggb00c and gga01c = ggb01c and gga00c = @compid and gga80d >= @fromdate and gga80d <= @todate and (isnull(ggb12c, '') = @analysis_subject or isnull(ggb13c, '') = @analysis_subject or isnull(ggb14c, '') = @analysis_subject or isnull(ggb26c, '') = @analysis_subject) group by gga00c, gga80d insert into #temp4(compid, amount, ymonth) select gna00c, sum(gnb08f*(case when isnull(gna20c, '') = @analysis_subject then gna27f when isnull(gna28c, '') = @analysis_subject then gna29f when isnull(gna30c, '') = @analysis_subject then gna31f end)), gna80d from gnm01, gnm02 where gna00c = gnb00c and gna01c = gnb01c and gna00c = @compid and gna80d >= @fromdate and gna80d <= @todate and (isnull(gna20c, '') = @analysis_subject or isnull(gna28c, '') = @analysis_subject or isnull(gna30c, '') = @analysis_subject) group by gna00c, gna80d insert into #temp4(compid, amount, ymonth) select gcl00c, sum(gcl07f*(case when isnull(gcl09c, '') = @analysis_subject then gcl16f when isnull(gcl17c, '') = @analysis_subject then gcl18f when isnull(gcl19c, '') = @analysis_subject then gcl20f end)), gcl80d from gcm10 where gcl00c = @compid and gcl06i = 0 and gcl80d >= @fromdate and gcl80d <= @todate and (isnull(gcl09c, '') = @analysis_subject or isnull(gcl17c, '') = @analysis_subject or isnull(gcl19c, '') = @analysis_subject) group by gcl00c, gcl80d insert into #temp4(compid, amount, ymonth) select gga00c, sum(ggc11f*(case when isnull(ggc12c, '') = @analysis_subject then ggc14f when isnull(ggc15c, '') = @analysis_subject then ggc16f when isnull(ggc17c, '') = @analysis_subject then ggc18f end)), gga80d from ggm01, ggm03 where gga00c = ggc00c and gga01c = ggc01c and gga00c = @compid and gga80d >= @fromdate and gga80d <= @todate and (isnull(ggc12c, '') = @analysis_subject or isnull(ggc15c, '') = @analysis_subject or isnull(ggc17c, '') = @analysis_subject) group by gga00c, gga80d insert into #total_yeji_month(compid, amount, ymonth) select #temp4.compid, sum(#temp4.amount), #temp4.ymonth from #temp4 group by #temp4.compid, #temp4.ymonth drop table #temp4 select amount, ymonth from #total_yeji_month order by ymonth asc delete #total_yeji_month end else if (@analysis_indicator = 1)--指标为项目业绩 begin insert into #project_yeji_month(compid, projectid, amount, quantity, ymonth) select gga00c, ggb03c, sum(case when isnull(ggb12c, '') = @analysis_subject then ggb20f when isnull(ggb13c, '') = @analysis_subject then ggb21f when isnull(ggb14c, '') = @analysis_subject then ggb22f when isnull(ggb26c, '') = @analysis_subject then ggb35f end), sum(ggb05f), gga80d from ggm01 with (nolock), ggm02 with (nolock) where gga00c = ggb00c and gga01c = ggb01c and gga00c = @compid and ggb03c = @analysis_keti and gga80d >= @fromdate and gga80d <= @todate and (isnull(ggb12c, '') = @analysis_subject or isnull(ggb13c, '') = @analysis_subject or isnull(ggb14c, '') = @analysis_subject or isnull(ggb26c, '') = @analysis_subject) group by gga00c, ggb03c, gga80d select amount, ymonth from #project_yeji_month order by ymonth asc delete #project_yeji_month end else if (@analysis_indicator = 2)--指标为某类项目业绩 begin insert into #project_class_yeji_month(compid, project_class, amount, ymonth) select gga00c, gda13c, sum(case when isnull(ggb12c, '') = @analysis_subject then ggb20f when isnull(ggb13c, '') = @analysis_subject then ggb21f when isnull(ggb14c, '') = @analysis_subject then ggb22f when isnull(ggb26c, '') = @analysis_subject then ggb35f end), gga80d from ggm01 with (nolock), ggm02 with (nolock), gdm01 with (nolock) where gga00c = ggb00c and gga00c = gda00c and gga01c = ggb01c and ggb03c = gda01c and gga00c = @compid and gda13c = @analysis_keti and gga80d >= @fromdate and gga80d <= @todate and (isnull(ggb12c, '') = @analysis_subject or isnull(ggb13c, '') = @analysis_subject or isnull(ggb14c, '') = @analysis_subject or isnull(ggb26c, '') = @analysis_subject) group by gga00c, gda13c, gga80d select amount, ymonth from #project_class_yeji_month order by ymonth asc delete #project_class_yeji_month end else if (@analysis_indicator = 3)--指标为卡销售充值业绩 begin create table #temp5 ( idd int identity(1, 1) not null, compid varchar(10) not null, amount float null, ymonth varchar(8) null ) insert into #temp5(compid, amount, ymonth) select gna00c, sum(gnb08f*(case when isnull(gna20c, '') = @analysis_subject then gna27f when isnull(gna28c, '') = @analysis_subject then gna29f when isnull(gna30c, '') = @analysis_subject then gna31f end)), gna80d from gnm01, gnm02 where gna00c = gnb00c and gna01c = gnb01c and gna00c = @compid and gna80d >= @fromdate and gna80d <= @todate and (isnull(gna20c, '') = @analysis_subject or isnull(gna28c, '') = @analysis_subject or isnull(gna30c, '') = @analysis_subject) group by gna00c, gna80d insert into #temp5(compid, amount, ymonth) select gcl00c, sum(gcl07f*(case when isnull(gcl09c, '') = @analysis_subject then gcl16f when isnull(gcl17c, '') = @analysis_subject then gcl18f when isnull(gcl19c, '') = @analysis_subject then gcl20f end)), gcl80d from gcm10 where gcl00c = @compid and gcl06i = 0 and gcl80d >= @fromdate and gcl80d <= @todate and (isnull(gcl09c, '') = @analysis_subject or isnull(gcl17c, '') = @analysis_subject or isnull(gcl19c, '') = @analysis_subject) group by gcl00c, gcl80d insert into #card_yeji_month(compid, amount, ymonth) select #temp5.compid, sum(#temp5.amount), #temp5.ymonth from #temp5 group by #temp5.compid, #temp5.ymonth drop table #temp5 select amount, ymonth from #card_yeji_month order by ymonth asc delete #card_yeji_month end else if (@analysis_indicator = 4)--指标为某类卡销售充值业绩 begin create table #temp6 ( idd int identity(1, 1) not null, compid varchar(10) not null, card_class varchar(10) not null, amount float null, ymonth varchar(8) null ) insert into #temp6(compid, card_class, amount, ymonth) select gna00c, gnb02c, sum(gnb08f*(case when isnull(gna20c, '') = @analysis_subject then gna27f when isnull(gna28c, '') = @analysis_subject then gna29f when isnull(gna30c, '') = @analysis_subject then gna31f end)), gna80d from gnm01, gnm02 where gna00c = gnb00c and gna01c = gnb01c and gna00c = @compid and gnb02c = @analysis_keti and gna80d >= @fromdate and gna80d <= @todate and (isnull(gna20c, '') = @analysis_subject or isnull(gna28c, '') = @analysis_subject or isnull(gna30c, '') = @analysis_subject) group by gna00c, gnb02c, gna80d insert into #temp6(compid, card_class, amount, ymonth) select gcl00c, gca02c, sum(gcl07f*(case when isnull(gcl09c, '') = @analysis_subject then gcl16f when isnull(gcl17c, '') = @analysis_subject then gcl18f when isnull(gcl19c, '') = @analysis_subject then gcl20f end)), gcl80d from gcm10, gcm01 where gcl00c = gca00c and gcl03c = gca01c and gca02c = @analysis_keti and gcl00c = @compid and gcl06i = 0 and gcl80d >= @fromdate and gcl80d <= @todate and (isnull(gcl09c, '') = @analysis_subject or isnull(gcl17c, '') = @analysis_subject or isnull(gcl19c, '') = @analysis_subject) group by gcl00c, gca02c, gcl80d insert into #card_class_yeji_month(compid, card_class, amount, ymonth) select #temp6.compid, #temp6.card_class, sum(#temp6.amount), #temp6.ymonth from #temp6 group by #temp6.compid, #temp6.card_class, #temp6.ymonth drop table #temp6 select amount, ymonth from #card_class_yeji_month order by ymonth asc delete #card_class_yeji_month end else if (@analysis_indicator = 5)--指标为产品销售业绩 begin insert into #commodity_yeji_month(compid, amount, quantity, ymonth) select gga00c, sum(ggc11f*(case when isnull(ggc12c, '') = @analysis_subject then ggc14f when isnull(ggc15c, '') = @analysis_subject then ggc16f when isnull(ggc17c, '') = @analysis_subject then ggc18f end)), sum(ggc05f), gga80d from ggm01, ggm03 where gga00c = ggc00c and gga01c = ggc01c and gga00c = @compid and gga80d >= @fromdate and gga80d <= @todate and (isnull(ggc12c, '') = @analysis_subject or isnull(ggc15c, '') = @analysis_subject or isnull(ggc17c, '') = @analysis_subject) group by gga00c, gga80d select amount, ymonth from #commodity_yeji_month order by ymonth asc delete #commodity_yeji_month end end else if (@analysis_object = 2)--分析对象为‘项目’ begin if (@analysis_indicator = 0) begin insert into #project_yeji_month(compid, projectid, amount, quantity, ymonth) select gga00c, ggb03c, sum(ggb11f), sum(ggb05f), gga80d from ggm01 with (nolock), ggm02 with (nolock) where gga00c = ggb00c and gga01c = ggb01c and gga00c = @compid and ggb03c = @analysis_subject and gga80d >= @fromdate and gga80d <= @todate group by gga00c, ggb03c, gga80d select amount, ymonth from #project_yeji_month order by ymonth asc delete #project_yeji_month end end else if (@analysis_object = 3)--分析对象为‘项目类别’ begin if (@analysis_indicator = 0) begin insert into #project_class_yeji_month(compid, project_class, amount, ymonth) select gga00c, gda13c, sum(ggb11f), gga80d from ggm01 with (nolock), ggm02 with (nolock), gdm01 with (nolock) where gga00c = ggb00c and gga00c = gda00c and gga01c = ggb01c and ggb03c = gda01c and gga00c = @compid and gda13c = @analysis_subject and gga80d >= @fromdate and gga80d <= @todate group by gga00c, gda13c, gga80d select amount, ymonth from #project_class_yeji_month order by ymonth asc delete #project_class_yeji_month end end drop table #project_yeji_month drop table #project_class_yeji_month drop table #card_yeji_month drop table #card_class_yeji_month drop table #commodity_yeji_month drop table #total_yeji_month end GO /****** Object: Table [dbo].[grade_setting] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[grade_setting]( [rsb00c] [varchar](10) NOT NULL, [rsb01c] [varchar](10) NOT NULL, [rsb02i] [int] NOT NULL, [rsb03c] [varchar](30) NOT NULL, [rsb04i] [int] NULL, [rsb05i] [int] NULL, [rsb06i] [int] NULL, [rsb07d] [varchar](20) NULL, CONSTRAINT [PK_GRADE_SETING] PRIMARY KEY CLUSTERED ( [rsb00c] ASC, [rsb01c] ASC, [rsb02i] ASC, [rsb03c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gqm03] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gqm03]( [gqc00c] [varchar](10) NOT NULL, [gqc01d] [varchar](10) NOT NULL, [gqc02f] [float] NULL, [gqc03f] [float] NULL, [gqc04c] [varchar](60) NULL, [gqc05f] [float] NULL, [gqc06f] [float] NULL, [gqc07c] [varchar](60) NULL, [gqc08f] [float] NULL, [gqc09c] [varchar](60) NULL, [gqc10f] [float] NULL, [gqc11c] [varchar](60) NULL, [gqc12f] [float] NULL, [gqc13f] [float] NULL, [gqc14c] [varchar](20) NULL, [gqc15d] [varchar](10) NULL, [gqc16t] [varchar](10) NULL, [gqc17f] [float] NULL, [gqc18c] [varchar](60) NULL, CONSTRAINT [PK_GQM03] PRIMARY KEY CLUSTERED ( [gqc00c] ASC, [gqc01d] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_decisionAnalysis] Script Date: 06/12/2017 06:19:57 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_decisionAnalysis] ( @analysis_object int, @analysis_indicator int, @analysis_subject varchar(10), @analysis_keti varchar(10), @fromdate varchar(8), @todate varchar(8), @compid varchar(10) ) as begin create table #project_yeji_month --项目单月业绩 ( idd int identity(1, 1) not null, compid varchar(10) not null, projectid varchar(20) not null, amount float null, quantity float null, ymonth varchar(8) null --年月 ) create table #project_class_yeji_month --项目类别单月业绩 ( idd int identity(1, 1) not null, compid varchar(10) not null, project_class varchar(10) not null, amount float null, ymonth varchar(8) null ) create table #card_yeji_month --卡销售充值单月业绩 ( idd int identity(1, 1) not null, compid varchar(10) not null, amount float null, ymonth varchar(8) null ) create table #card_class_yeji_month --卡类别销售充值单月业绩 ( idd int identity(1, 1) not null, compid varchar(10) not null, card_class varchar(10) not null, amount float null, ymonth varchar(8) null ) create table #commodity_yeji_month --产品单月销售业绩 ( idd int identity(1, 1) not null, compid varchar(10) not null, amount float null, quantity float null, ymonth varchar(8) null ) create table #total_yeji_month --店单月总业绩 ( idd int identity(1, 1) not null, compid varchar(10) not null, amount float null, ymonth varchar(8) null ) if (@analysis_object = 0) --分析对象为‘公司/店’ begin if (@analysis_indicator = 0)--指标为总业绩 begin create table #temp1 ( idd int identity(1, 1) not null, compid varchar(10) not null, amount float null, ymonth varchar(8) null ) insert into #temp1(compid, amount, ymonth) select gga00c, sum(gga16f), left(gga80d, 6) from ggm01 where gga00c = @analysis_subject and left(gga80d, 6) >= @fromdate and left(gga80d, 6) <= @todate group by gga00c, left(gga80d, 6) insert into #temp1(compid, amount, ymonth) select gna00c, sum(gnb08f), left(gna80d, 6) from gnm01, gnm02 where gna00c = gnb00c and gna01c = gnb01c and gna00c = @analysis_subject and left(gna80d, 6) >= @fromdate and left(gna80d, 6) <= @todate group by gna00c, left(gna80d, 6) insert into #temp1(compid, amount, ymonth) select gcl00c, sum(gcl07f), left(gcl80d, 6) from gcm10 where gcl00c = @analysis_subject and gcl06i = 0 and left(gcl80d, 6) >= @fromdate and left(gcl80d, 6) <= @todate group by gcl00c, left(gcl80d, 6) insert into #total_yeji_month(compid, amount, ymonth) select #temp1.compid, sum(#temp1.amount), #temp1.ymonth from #temp1 group by #temp1.compid, #temp1.ymonth drop table #temp1 select amount, ymonth from #total_yeji_month order by ymonth asc delete #total_yeji_month end else if (@analysis_indicator = 1)--指标为项目业绩 begin insert into #project_yeji_month(compid, projectid, amount, quantity, ymonth) select gga00c, ggb03c, sum(ggb11f), sum(ggb05f), left(gga80d, 6) from ggm01 with (nolock), ggm02 with (nolock) where gga00c = ggb00c and gga01c = ggb01c and gga00c = @analysis_subject and ggb03c = @analysis_keti and left(gga80d, 6) >= @fromdate and left(gga80d, 6) <= @todate group by gga00c, ggb03c, left(gga80d, 6) select amount, ymonth from #project_yeji_month order by ymonth asc delete #project_yeji_month end else if (@analysis_indicator = 2)--指标为某类项目业绩 begin insert into #project_class_yeji_month(compid, project_class, amount, ymonth) select gga00c, gda13c, sum(ggb11f), left(gga80d, 6) from ggm01 with (nolock), ggm02 with (nolock), gdm01 with (nolock) where gga00c = ggb00c and gga00c = gda00c and gga01c = ggb01c and ggb03c = gda01c and gga00c = @analysis_subject and gda13c = @analysis_keti and left(gga80d, 6) >= @fromdate and left(gga80d, 6) <= @todate group by gga00c, gda13c, left(gga80d, 6) select amount, ymonth from #project_class_yeji_month order by ymonth asc delete #project_class_yeji_month end else if (@analysis_indicator = 3)--指标为卡销售充值业绩 begin create table #temp2 ( idd int identity(1, 1) not null, compid varchar(10) not null, amount float null, ymonth varchar(8) null ) insert into #temp2(compid, amount, ymonth) select gna00c, sum(gnb08f), left(gna80d, 6) from gnm01, gnm02 where gna00c = gnb00c and gna01c = gnb01c and gna00c = @analysis_subject and left(gna80d, 6) >= @fromdate and left(gna80d, 6) <= @todate group by gna00c, left(gna80d, 6) insert into #temp2(compid, amount, ymonth) select gcl00c, sum(gcl07f), left(gcl80d, 6) from gcm10 where gcl00c = @analysis_subject and gcl06i = 0 and left(gcl80d, 6) >= @fromdate and left(gcl80d, 6) <= @todate group by gcl00c, left(gcl80d, 6) insert into #card_yeji_month(compid, amount, ymonth) select #temp2.compid, sum(#temp2.amount), #temp2.ymonth from #temp2 group by #temp2.compid, #temp2.ymonth drop table #temp2 select amount, ymonth from #card_yeji_month order by ymonth asc delete #card_yeji_month end else if (@analysis_indicator = 4)--指标为某类卡销售充值业绩 begin create table #temp3 ( idd int identity(1, 1) not null, compid varchar(10) not null, card_class varchar(10) not null, amount float null, ymonth varchar(8) null ) insert into #temp3(compid, card_class, amount, ymonth) select gna00c, gnb02c, sum(gnb08f), left(gna80d, 6) from gnm01, gnm02 where gna00c = gnb00c and gna01c = gnb01c and gna00c = @analysis_subject and gnb02c = @analysis_keti and left(gna80d, 6) >= @fromdate and left(gna80d, 6) <= @todate group by gna00c, gnb02c, left(gna80d, 6) insert into #temp3(compid, card_class, amount, ymonth) select gcl00c, gca02c, sum(gcl07f), left(gcl80d, 6) from gcm10, gcm01 where gcl00c = gca00c and gcl03c = gca01c and gca02c = @analysis_keti and gcl00c = @analysis_subject and gcl06i = 0 and left(gcl80d, 6) >= @fromdate and left(gcl80d, 6) <= @todate group by gcl00c, gca02c, left(gcl80d, 6) insert into #card_class_yeji_month(compid, card_class, amount, ymonth) select #temp3.compid, #temp3.card_class, sum(#temp3.amount), #temp3.ymonth from #temp3 group by #temp3.compid, #temp3.card_class, #temp3.ymonth drop table #temp3 select amount, ymonth from #card_class_yeji_month order by ymonth asc delete #card_class_yeji_month end else if (@analysis_indicator = 5)--指标为产品销售业绩 begin insert into #commodity_yeji_month(compid, amount, quantity, ymonth) select gga00c, sum(ggc11f), sum(ggc05f), left(gga80d, 6) from ggm01, ggm03 where gga00c = ggc00c and gga01c = ggc01c and gga00c = @analysis_subject and left(gga80d, 6) >= @fromdate and left(gga80d, 6) <= @todate group by gga00c, left(gga80d, 6) select amount, ymonth from #commodity_yeji_month order by ymonth asc delete #commodity_yeji_month end end else if (@analysis_object = 1)--分析对象为‘员工’ begin if (@analysis_indicator = 0)--指标为总业绩 begin create table #temp4 ( idd int identity(1, 1) not null, compid varchar(10) not null, amount float null, ymonth varchar(8) null ) insert into #temp4(compid, amount, ymonth) select gga00c, sum(case when isnull(ggb12c, '') = @analysis_subject then ggb20f when isnull(ggb13c, '') = @analysis_subject then ggb21f when isnull(ggb14c, '') = @analysis_subject then ggb22f when isnull(ggb26c, '') = @analysis_subject then ggb35f end), left(gga80d, 6) from ggm01 with (nolock), ggm02 with (nolock) where gga00c = ggb00c and gga01c = ggb01c and gga00c = @compid and left(gga80d, 6) >= @fromdate and left(gga80d, 6) <= @todate and (isnull(ggb12c, '') = @analysis_subject or isnull(ggb13c, '') = @analysis_subject or isnull(ggb14c, '') = @analysis_subject or isnull(ggb26c, '') = @analysis_subject) group by gga00c, left(gga80d, 6) insert into #temp4(compid, amount, ymonth) select gna00c, sum(gnb08f*(case when isnull(gna20c, '') = @analysis_subject then gna27f when isnull(gna28c, '') = @analysis_subject then gna29f when isnull(gna30c, '') = @analysis_subject then gna31f end)), left(gna80d, 6) from gnm01, gnm02 where gna00c = gnb00c and gna01c = gnb01c and gna00c = @compid and left(gna80d, 6) >= @fromdate and left(gna80d, 6) <= @todate and (isnull(gna20c, '') = @analysis_subject or isnull(gna28c, '') = @analysis_subject or isnull(gna30c, '') = @analysis_subject) group by gna00c, left(gna80d, 6) insert into #temp4(compid, amount, ymonth) select gcl00c, sum(gcl07f*(case when isnull(gcl09c, '') = @analysis_subject then gcl16f when isnull(gcl17c, '') = @analysis_subject then gcl18f when isnull(gcl19c, '') = @analysis_subject then gcl20f end)), left(gcl80d, 6) from gcm10 where gcl00c = @compid and gcl06i = 0 and left(gcl80d, 6) >= @fromdate and left(gcl80d, 6) <= @todate and (isnull(gcl09c, '') = @analysis_subject or isnull(gcl17c, '') = @analysis_subject or isnull(gcl19c, '') = @analysis_subject) group by gcl00c, left(gcl80d, 6) insert into #temp4(compid, amount, ymonth) select gga00c, sum(ggc11f*(case when isnull(ggc12c, '') = @analysis_subject then ggc14f when isnull(ggc15c, '') = @analysis_subject then ggc16f when isnull(ggc17c, '') = @analysis_subject then ggc18f end)), left(gga80d, 6) from ggm01, ggm03 where gga00c = ggc00c and gga01c = ggc01c and gga00c = @compid and left(gga80d, 6) >= @fromdate and left(gga80d, 6) <= @todate and (isnull(ggc12c, '') = @analysis_subject or isnull(ggc15c, '') = @analysis_subject or isnull(ggc17c, '') = @analysis_subject) group by gga00c, left(gga80d, 6) insert into #total_yeji_month(compid, amount, ymonth) select #temp4.compid, sum(#temp4.amount), #temp4.ymonth from #temp4 group by #temp4.compid, #temp4.ymonth drop table #temp4 select amount, ymonth from #total_yeji_month order by ymonth asc delete #total_yeji_month end else if (@analysis_indicator = 1)--指标为项目业绩 begin insert into #project_yeji_month(compid, projectid, amount, quantity, ymonth) select gga00c, ggb03c, sum(case when isnull(ggb12c, '') = @analysis_subject then ggb20f when isnull(ggb13c, '') = @analysis_subject then ggb21f when isnull(ggb14c, '') = @analysis_subject then ggb22f when isnull(ggb26c, '') = @analysis_subject then ggb35f end), sum(ggb05f), left(gga80d, 6) from ggm01 with (nolock), ggm02 with (nolock) where gga00c = ggb00c and gga01c = ggb01c and gga00c = @compid and ggb03c = @analysis_keti and left(gga80d, 6) >= @fromdate and left(gga80d, 6) <= @todate and (isnull(ggb12c, '') = @analysis_subject or isnull(ggb13c, '') = @analysis_subject or isnull(ggb14c, '') = @analysis_subject or isnull(ggb26c, '') = @analysis_subject) group by gga00c, ggb03c, left(gga80d, 6) select amount, ymonth from #project_yeji_month order by ymonth asc delete #project_yeji_month end else if (@analysis_indicator = 2)--指标为某类项目业绩 begin insert into #project_class_yeji_month(compid, project_class, amount, ymonth) select gga00c, gda13c, sum(case when isnull(ggb12c, '') = @analysis_subject then ggb20f when isnull(ggb13c, '') = @analysis_subject then ggb21f when isnull(ggb14c, '') = @analysis_subject then ggb22f when isnull(ggb26c, '') = @analysis_subject then ggb35f end), left(gga80d, 6) from ggm01 with (nolock), ggm02 with (nolock), gdm01 with (nolock) where gga00c = ggb00c and gga00c = gda00c and gga01c = ggb01c and ggb03c = gda01c and gga00c = @compid and gda13c = @analysis_keti and left(gga80d, 6) >= @fromdate and left(gga80d, 6) <= @todate and (isnull(ggb12c, '') = @analysis_subject or isnull(ggb13c, '') = @analysis_subject or isnull(ggb14c, '') = @analysis_subject or isnull(ggb26c, '') = @analysis_subject) group by gga00c, gda13c, left(gga80d, 6) select amount, ymonth from #project_class_yeji_month order by ymonth asc delete #project_class_yeji_month end else if (@analysis_indicator = 3)--指标为卡销售充值业绩 begin create table #temp5 ( idd int identity(1, 1) not null, compid varchar(10) not null, amount float null, ymonth varchar(8) null ) insert into #temp5(compid, amount, ymonth) select gna00c, sum(gnb08f*(case when isnull(gna20c, '') = @analysis_subject then gna27f when isnull(gna28c, '') = @analysis_subject then gna29f when isnull(gna30c, '') = @analysis_subject then gna31f end)), left(gna80d, 6) from gnm01, gnm02 where gna00c = gnb00c and gna01c = gnb01c and gna00c = @compid and left(gna80d, 6) >= @fromdate and left(gna80d, 6) <= @todate and (isnull(gna20c, '') = @analysis_subject or isnull(gna28c, '') = @analysis_subject or isnull(gna30c, '') = @analysis_subject) group by gna00c, left(gna80d, 6) insert into #temp5(compid, amount, ymonth) select gcl00c, sum(gcl07f*(case when isnull(gcl09c, '') = @analysis_subject then gcl16f when isnull(gcl17c, '') = @analysis_subject then gcl18f when isnull(gcl19c, '') = @analysis_subject then gcl20f end)), left(gcl80d, 6) from gcm10 where gcl00c = @compid and gcl06i = 0 and left(gcl80d, 6) >= @fromdate and left(gcl80d, 6) <= @todate and (isnull(gcl09c, '') = @analysis_subject or isnull(gcl17c, '') = @analysis_subject or isnull(gcl19c, '') = @analysis_subject) group by gcl00c, left(gcl80d, 6) insert into #card_yeji_month(compid, amount, ymonth) select #temp5.compid, sum(#temp5.amount), #temp5.ymonth from #temp5 group by #temp5.compid, #temp5.ymonth drop table #temp5 select amount, ymonth from #card_yeji_month order by ymonth asc delete #card_yeji_month end else if (@analysis_indicator = 4)--指标为某类卡销售充值业绩 begin create table #temp6 ( idd int identity(1, 1) not null, compid varchar(10) not null, card_class varchar(10) not null, amount float null, ymonth varchar(8) null ) insert into #temp6(compid, card_class, amount, ymonth) select gna00c, gnb02c, sum(gnb08f*(case when isnull(gna20c, '') = @analysis_subject then gna27f when isnull(gna28c, '') = @analysis_subject then gna29f when isnull(gna30c, '') = @analysis_subject then gna31f end)), left(gna80d, 6) from gnm01, gnm02 where gna00c = gnb00c and gna01c = gnb01c and gna00c = @compid and gnb02c = @analysis_keti and left(gna80d, 6) >= @fromdate and left(gna80d, 6) <= @todate and (isnull(gna20c, '') = @analysis_subject or isnull(gna28c, '') = @analysis_subject or isnull(gna30c, '') = @analysis_subject) group by gna00c, gnb02c, left(gna80d, 6) insert into #temp6(compid, card_class, amount, ymonth) select gcl00c, gca02c, sum(gcl07f*(case when isnull(gcl09c, '') = @analysis_subject then gcl16f when isnull(gcl17c, '') = @analysis_subject then gcl18f when isnull(gcl19c, '') = @analysis_subject then gcl20f end)), left(gcl80d, 6) from gcm10, gcm01 where gcl00c = gca00c and gcl03c = gca01c and gca02c = @analysis_keti and gcl00c = @compid and gcl06i = 0 and left(gcl80d, 6) >= @fromdate and left(gcl80d, 6) <= @todate and (isnull(gcl09c, '') = @analysis_subject or isnull(gcl17c, '') = @analysis_subject or isnull(gcl19c, '') = @analysis_subject) group by gcl00c, gca02c, left(gcl80d, 6) insert into #card_class_yeji_month(compid, card_class, amount, ymonth) select #temp6.compid, #temp6.card_class, sum(#temp6.amount), #temp6.ymonth from #temp6 group by #temp6.compid, #temp6.card_class, #temp6.ymonth drop table #temp6 select amount, ymonth from #card_class_yeji_month order by ymonth asc delete #card_class_yeji_month end else if (@analysis_indicator = 5)--指标为产品销售业绩 begin insert into #commodity_yeji_month(compid, amount, quantity, ymonth) select gga00c, sum(ggc11f*(case when isnull(ggc12c, '') = @analysis_subject then ggc14f when isnull(ggc15c, '') = @analysis_subject then ggc16f when isnull(ggc17c, '') = @analysis_subject then ggc18f end)), sum(ggc05f), left(gga80d, 6) from ggm01, ggm03 where gga00c = ggc00c and gga01c = ggc01c and gga00c = @compid and left(gga80d, 6) >= @fromdate and left(gga80d, 6) <= @todate and (isnull(ggc12c, '') = @analysis_subject or isnull(ggc15c, '') = @analysis_subject or isnull(ggc17c, '') = @analysis_subject) group by gga00c, left(gga80d, 6) select amount, ymonth from #commodity_yeji_month order by ymonth asc delete #commodity_yeji_month end end else if (@analysis_object = 2)--分析对象为‘项目’ begin if (@analysis_indicator = 0) begin insert into #project_yeji_month(compid, projectid, amount, quantity, ymonth) select gga00c, ggb03c, sum(ggb11f), sum(ggb05f), left(gga80d, 6) from ggm01 with (nolock), ggm02 with (nolock) where gga00c = ggb00c and gga01c = ggb01c and gga00c = @compid and ggb03c = @analysis_subject and left(gga80d, 6) >= @fromdate and left(gga80d, 6) <= @todate group by gga00c, ggb03c, left(gga80d, 6) select amount, ymonth from #project_yeji_month order by ymonth asc delete #project_yeji_month end end else if (@analysis_object = 3)--分析对象为‘项目类别’ begin if (@analysis_indicator = 0) begin insert into #project_class_yeji_month(compid, project_class, amount, ymonth) select gga00c, gda13c, sum(ggb11f), left(gga80d, 6) from ggm01 with (nolock), ggm02 with (nolock), gdm01 with (nolock) where gga00c = ggb00c and gga00c = gda00c and gga01c = ggb01c and ggb03c = gda01c and gga00c = @compid and gda13c = @analysis_subject and left(gga80d, 6) >= @fromdate and left(gga80d, 6) <= @todate group by gga00c, gda13c, left(gga80d, 6) select amount, ymonth from #project_class_yeji_month order by ymonth asc delete #project_class_yeji_month end end drop table #project_yeji_month drop table #project_class_yeji_month drop table #card_yeji_month drop table #card_class_yeji_month drop table #commodity_yeji_month drop table #total_yeji_month end GO /****** Object: Table [dbo].[gdm04] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gdm04]( [gdd00c] [varchar](10) NOT NULL, [gdd01c] [varchar](20) NOT NULL, [gdd02c] [varchar](20) NOT NULL, [gdd03c] [varchar](20) NULL, [gdd04c] [varchar](5) NULL, [gdd05f] [float] NULL, [gdd06f] [float] NULL, [gdd07f] [float] NULL, [gdd08i] [int] NULL, CONSTRAINT [PK_GDM04] PRIMARY KEY CLUSTERED ( [gdd00c] ASC, [gdd01c] ASC, [gdd02c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_goods_waste_analysis] Script Date: 06/12/2017 06:20:12 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_goods_waste_analysis] ( @compid varchar(10), -- 公司别 @fromdate varchar(8), -- 开始日期 @todate varchar(8), -- 截至日期 @type int, -- 1: 以浪费数量比率排序, 2:浪费金额比率排序 --3: 分析一个员工的浪费;4:统计项目的浪费;5:统计产品的浪费;6 分析会员产品消耗 @cost_type int -- 成本计算方式, 1-标准价格 2 - 销售成本 3-消耗成本 ) as begin create table #stat_goods_waste ( idd int identity not null, -- 序号 billdate varchar(8) not null, -- 日期 billid varchar(20) null, -- 单号 item varchar(20) null, -- 项目代码 itemname varchar(40) null, -- 项目名称 good varchar(20) null, -- 产品代码 goodname varchar(40) null, -- 产品名称 staff varchar(20) null, -- 员工代码 staffname varchar(40) null, -- 员工姓名 std_quan float null, -- 标准消耗数量 std_amt float null, -- 标准消耗金额 fct_quan float null, -- 实际消耗数量 fct_amt float null, -- 实际消耗金额 quan_rate float null, -- 数量比率 amt_rate float null, -- 金额比率 unit varchar(20) null, -- 单位 setgoodid varchar(20) null, --设置产品编码 setgoodname varchar(40) null, --设置的产品名称 cardid varchar(60) null, --卡号 [name] varchar(100) null -- 姓名 primary key(idd) ) insert #stat_goods_waste(billdate, billid, item, itemname, good, goodname, staff, staffname, fct_quan, unit, setgoodid,cardid,[name]) select a.gga02d, a.gga01c, b.gge14c, c.gda03c, b.gge03c, d.gfa03c, b.gge12c, e.haa02c, b.gge05f, isnull(gfa08c, gfa07c), gge17c,a.gga05c,gga06c from ggm05 b with (nolock), ggm01 a with (nolock), gdm01 c with (nolock), gfm01 d with (nolock), ham01 e with (nolock) where b.gge00c = a.gga00c and b.gge01c = a.gga01c and b.gge00c = c.gda00c and b.gge14c = c.gda01c and b.gge00c = d.gfa00c and b.gge03c = d.gfa01c and a.gga80d >= @fromdate and a.gga80d <= @todate and a.gga00c = @compid and b.gge00c = e.haa00c and b.gge12c = e.haa01c delete #stat_goods_waste where isnull(fct_quan, 0) = 0 --更新标准数量 update a set std_quan = b.gfe03f from #stat_goods_waste a, gfm05 b with (nolock) where b.gfe00c = @compid and a.item = b.gfe01c and a.setgoodid = b.gfe02c --更新标准金额,以产品的标准价 --更新实际金额 if (@cost_type = 1) begin --标准定价 update a set a.std_amt = (case when isnull(gfa09f, 1) = 0 then 0 else a.std_quan * (isnull(b.gfa11f, 0)/isnull(gfa09f, 1))end), a.fct_amt = (case when isnull(gfa09f, 1) = 0 then 0 else a.fct_quan * (isnull(b.gfa11f, 0)/isnull(gfa09f, 1))end) from #stat_goods_waste a, gfm01 b with (nolock) where b.gfa00c = @compid and a.good = b.gfa01c end else if (@cost_type = 2) begin --销售成本来计算 update a set a.std_amt = (case when isnull(gfa09f, 1) = 0 then 0 else a.std_quan * (isnull(b.gfa19f, 0)/isnull(gfa09f, 1))end), a.fct_amt = (case when isnull(gfa09f, 1) = 0 then 0 else a.fct_quan * (isnull(b.gfa19f, 0)/isnull(gfa09f, 1))end) from #stat_goods_waste a, gfm01 b with (nolock) where b.gfa00c = @compid and a.good = b.gfa01c end else if (@cost_type = 3) begin --消耗成本 update a set a.std_amt = a.std_quan * (isnull(b.gfa15f, 0)), a.fct_amt = a.fct_quan * (isnull(b.gfa15f, 0)) from #stat_goods_waste a, gfm01 b with (nolock) where b.gfa00c = @compid and a.good = b.gfa01c end --计算出比率 update a set quan_rate = (case when std_quan = 0 then 0 else (fct_quan - std_quan)/std_quan end), amt_rate = (case when std_amt = 0 then 0 else (fct_amt - std_amt)/std_amt end) from #stat_goods_waste a where isnull(std_quan, 0) <> 0 and isnull(std_amt, 0) <> 0 if (@type = 1) select idd,isnull(billdate,'') billdate, isnull(billid,'') billid,isnull(item,'') item,isnull(itemname,'') itemname, isnull(good,'') good,isnull(goodname,'') goodname,isnull(staff,'') staff,isnull(staffname,'') staffname, isnull(std_quan,0) std_quan,isnull(std_amt,0) std_amt,isnull(fct_quan,0) fct_quan,isnull(fct_amt,0) fct_amt, isnull(quan_rate,0) quan_rate,isnull(amt_rate,0) amt_rate,isnull(unit,'') unit,isnull(setgoodid,'') setgoodid, isnull(setgoodname,'') setgoodname from #stat_goods_waste order by item else if (@type = 2) select idd,isnull(billdate,'') billdate, isnull(billid,'') billid,isnull(item,'') item,isnull(itemname,'') itemname, isnull(good,'') good,isnull(goodname,'') goodname,isnull(staff,'') staff,isnull(staffname,'') staffname, isnull(std_quan,0) std_quan,isnull(std_amt,0) std_amt,isnull(fct_quan,0) fct_quan,isnull(fct_amt,0) fct_amt, isnull(quan_rate,0) quan_rate,isnull(amt_rate,0) amt_rate,isnull(unit,'') unit,isnull(setgoodid,'') setgoodid, isnull(setgoodname,'') setgoodname from #stat_goods_waste order by amt_rate desc else if (@type = 3) begin select isnull(good,'') good, isnull(goodname,'') goodname, isnull(staff,'') staff, isnull(staffname,'') staffname, std_quan = isnull(sum(std_quan),0), std_amt = isnull(sum(std_amt),0), fct_quan = isnull(sum(fct_quan),0), fct_amt = isnull(sum(fct_amt),0), quan_rate = isnull((case when sum(std_quan) = 0 then 0 else (sum(fct_quan)-sum(std_quan))/isnull(sum(std_quan), 99999)end),0), amt_rate = isnull((case when sum(std_amt) = 0 then 0 else (sum(fct_amt)-sum(std_amt))/isnull(sum(std_amt), 99999)end),0), isnull(unit,'') unit from #stat_goods_waste group by good, goodname, staff, staffname, unit order by staff end else if (@type = 4) begin select isnull(item,'') item, isnull(itemname,'') itemname, std_quan = isnull(sum(std_quan),0), std_amt = isnull(sum(std_amt),0), fct_quan = isnull(sum(fct_quan),0), fct_amt = isnull(sum(fct_amt),0), quan_rate = isnull((case when sum(std_quan) = 0 then 0 else (sum(fct_quan)-sum(std_quan))/isnull(sum(std_quan), 99999) end),0), amt_rate = isnull((case when sum(std_amt) = 0 then 0 else (sum(fct_amt)-sum(std_amt))/isnull(sum(std_amt), 99999) end),0), isnull(unit,'') unit from #stat_goods_waste group by item, itemname, unit order by amt_rate desc end else if (@type = 5) begin select isnull(good,'') good, isnull(goodname,'') goodname, isnull(billdate,'') billdate,isnull(billid,'') billid, isnull(staff,'') staff, isnull(staffname,'') staffname, std_quan = isnull(sum(std_quan),0), std_amt = isnull(sum(std_amt),0), fct_quan = isnull(sum(fct_quan),0), fct_amt = isnull(sum(fct_amt),0), quan_rate = isnull((case when sum(std_quan) = 0 then 0 else (sum(fct_quan)-sum(std_quan))/isnull(sum(std_quan), 99999) end),0), amt_rate = isnull((case when sum(std_amt) = 0 then 0 else (sum(fct_amt)-sum(std_amt))/isnull(sum(std_amt), 99999) end),0), isnull(unit,'') unit from #stat_goods_waste group by good, goodname, unit, billdate, billid, staff, staffname order by good desc end else if (@type = 6) begin select isnull(cardid,'') cardid, isnull(good,'') good, isnull(goodname,'') goodname, isnull(unit,'') unit, std_quan = isnull(sum(std_quan),0), std_amt = isnull(sum(std_amt),0), fct_quan = isnull(sum(fct_quan),0), fct_amt = isnull(sum(fct_amt),0), quan_rate = isnull((case when sum(std_quan) = 0 then 0 else (sum(fct_quan)-sum(std_quan))/isnull(sum(std_quan), 99999) end),0), amt_rate = isnull((case when sum(std_amt) = 0 then 0 else (sum(fct_amt)-sum(std_amt))/isnull(sum(std_amt), 99999) end),0), isnull(max([name]),'') [name] from #stat_goods_waste where rtrim(cardid) <> '散客' group by good, goodname, unit,cardid order by good desc end drop table #stat_goods_waste end GO /****** Object: Table [dbo].[PNIC_Account] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[PNIC_Account]( [pna00c] [varchar](10) NOT NULL, [pna01c] [varchar](5) NOT NULL, [pna02i] [int] NOT NULL, CONSTRAINT [PK_PNIC_Account] PRIMARY KEY CLUSTERED ( [pna00c] ASC, [pna01c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[hcm02] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[hcm02]( [hcb00c] [varchar](10) NOT NULL, [hcb01c] [varchar](20) NOT NULL, [hcb02c] [varchar](10) NOT NULL, [hcb03f] [float] NULL, [hcb04f] [float] NULL, [hcb05f] [float] NULL, [hcb06f] [float] NULL, [hcb07i] [int] NULL, [hcb08i] [int] NULL, [id] [numeric](18, 0) IDENTITY(1,1) NOT NULL, CONSTRAINT [PK_HCM02] PRIMARY KEY CLUSTERED ( [hcb00c] ASC, [hcb01c] ASC, [hcb02c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_search_yesterday_unclose] Script Date: 06/12/2017 06:20:39 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_search_yesterday_unclose]( @compid varchar(10), @curdate varchar(10)) as begin return end GO /****** Object: StoredProcedure [dbo].[upg_analyzer_customer_order_regdate_ex] Script Date: 06/12/2017 06:19:27 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_analyzer_customer_order_regdate_ex] ( @compid varchar(10), @class varchar(10), @memfromdate varchar(10), @memtodate varchar(10), @frommemid varchar(20), @tomemid varchar(20), @fromdate varchar(20), @todate varchar(20), @state varchar(200), -- 会员卡状态 @acon_amt float, -- 大于消费金额 @alastdate varchar(8), -- 某个日期之后 @at_times float, -- 大于消费次数 @aav_amt float, -- 大于平均消费金额 @aless_remain float, -- 小于某个储值余额 @abig_remain float, -- 大于某个储值余额 @addtoSMS int, @ordertype int, @only_card_pay int ) as begin create table #mem_result ( idd int not null, compid varchar(10), memid varchar(20) not null, -- Member编号 m_name varchar(40) null, -- 姓名 cardid varchar(20) null, -- 卡号 gender int null, -- 性别 0:女 mobile varchar(20) null, -- 手机 tel varchar(20) null, -- 电话 birthday varchar(8) null, -- 会员生日 state varchar(10) null, -- Member卡状态 con_amt float null, -- Consumption金额 lastdate varchar(8) null, -- 最后Consumption日期 lastcall varchar(8) null, -- 最后回访日期 t_times float null, -- 用于比较的Consumption次数 t_compare float null, -- 总共来的次数 av_ytimes float null, -- 每年来的次数 av_times float null, -- 每月来的次数 av_wtimes float null, -- 每周来的次数 months float null, -- 客人来了多少个月了,客龄 av_amt float null, -- 每次Consumption的平均金额 start_date varchar(20) null, -- 开始日期 first_date varchar(20) null, -- 第一次来店日期 comp_date varchar(20) null, -- 开始比较的日期 dur_years float null, -- 统计期间来了多少个年了 dur_months float null, -- 统计期间来了多少个月了 dur_weeks float null, -- 统计期间来了多少个周了 remain01 float null, -- 余额 remain02 float null, -- 余额 remain03 float null, -- 余额 remain04 float null, -- 余额 remain05 float null, -- 余额 total_fill_amt float null, -- 累计Charge金额 memaddr varchar(160) null, -- 会员地址 postcode varchar(20) null, -- 邮编 principle varchar(20) null, -- 负责人 total_salecard_amt float null, -- 累计卡销售金额 regdate varchar(20) null, -- 登记日期 memlevel varchar(300) null, --会员等级 curmonth_amt float null, --本月消费 premonth_amt float null, --上月消费 primary key(idd) ) insert #mem_result(idd, compid, memid, m_name, cardid, gender, mobile, tel, birthday, state, con_amt, lastdate, lastcall, t_times, t_compare, av_ytimes, av_times, av_wtimes, months, av_amt, start_date, first_date, comp_date, dur_years, dur_months, dur_weeks, remain01, remain02, remain03, remain04, remain05, total_fill_amt, memaddr, postcode, principle, total_salecard_amt) exec upg_analyzer_customer_ex_GLZX @compid, @class, @frommemid, @tomemid, @memfromdate, @memtodate, @fromdate, @todate, @state, @acon_amt, @alastdate, @at_times, 0, @aav_amt, @aless_remain, @abig_remain, @addtoSMS, 3, @only_card_pay --会员资料统计表,需要 create table #gbm18 ( compid varchar(10) not null, memid varchar(30) not null, levelid int null, leveltext varchar(300) ) insert into #gbm18(compid,memid,levelid,leveltext) select a.gbr00c,a.gbr01c,b.gsb02c,b.gsb03c from gbm18 a,(select gsb00c,gsb01c,gsb02c,gsb03c from gsm02 where gsb00c = @compid and gsb01c = 'DJ' ) b where a.gbr00c = b.gsb00c and a.gbr04i = b.gsb02c and a.gbr00c = @compid create table #member_level ( compid varchar(10) not null, memid varchar(30) not null, leveltext varchar(300) ) insert into #member_level(compid,memid,leveltext) select compid,memId,leveltext = stuff((select ',' + leveltext from #gbm18 b where a.compid = b.compid and a.memId = b.memId for xml path('')),1,1,'') from #gbm18 a group by compid,memId update a set a.memlevel = b.leveltext from #mem_result a,#member_level b where a.compid = b.compid and a.memid = b.memid declare @startdate_currmonth varchar(10),@enddate_currmonth varchar(10), @startdate_premonth varchar(10),@enddate_premonth varchar(10) select @startdate_currmonth = convert(varchar(10),DATEADD(month,-1,GETDATE()-DATEPART(day,GETDATE())+1),112) select @enddate_currmonth = convert(varchar(10),DATEADD(day,-1,GETDATE()-DATEPART(day,GETDATE())+1),112) select @startdate_premonth = CONVERT(varchar(10),GETDATE()-DATEPART(day,GETDATE())+1,112) select @enddate_premonth = convert(varchar(10),DATEADD(day,-1,DATEADD(month,1,GETDATE()-DATEPART(day,GETDATE())+1)),112) create table #member_consume ( compid varchar(10) not null, memid varchar(30) not null, currmonth_amt float null, premonth_amt float null ) insert into #member_consume(compid,memid,currmonth_amt) select gct00c,gct03c,sum(gct12f) from gcm20 where gct00c = @compid and gct04d >= @startdate_currmonth and gct04d <= @enddate_currmonth and gct06c in('3','4') group by gct00c,gct03c insert into #member_consume(compid,memid,premonth_amt) select gct00c,gct03c,sum(gct12f) from gcm20 where gct00c = @compid and gct04d >= @startdate_premonth and gct04d <= @enddate_premonth and gct06c in('3','4') group by gct00c,gct03c update a set a.curmonth_amt = b.currmonth_amt,a.premonth_amt = b.premonth_amt from #mem_result a,(select compid,memid,sum(currmonth_amt) currmonth_amt,sum(premonth_amt) premonth_amt from #member_consume group by compid,memid) b where a.compid = b.compid and a.memid = b.memid select idd, compid, memid, m_name, cardid, gender, mobile, tel, birthday, state, con_amt, lastdate, lastcall, t_times, av_times, months, av_amt, start_date, first_date, dur_months, remain01, remain02, remain03, remain04, remain05, total_fill_amt, memaddr, postcode, principle, total_salecard_amt,memlevel,curmonth_amt,premonth_amt from #mem_result a order by memid drop table #member_consume drop table #member_level drop table #gbm18 drop table #mem_result end GO /****** Object: Table [dbo].[gqm01] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gqm01]( [gqa00c] [varchar](10) NOT NULL, [gqa01c] [varchar](20) NOT NULL, [gqa02d] [varchar](8) NULL, [gqa03c] [varchar](5) NULL, [gqa04c] [varchar](5) NULL, [gqa05f] [float] NULL, [gqa06f] [float] NULL, [gqa07c] [varchar](10) NULL, [gqa08d] [varchar](8) NULL, [gqa09i] [int] NULL, [gqa10c] [varchar](5) NULL, [gqa11c] [varchar](5) NULL, [gqa12c] [varchar](20) NULL, [gqa13i] [int] NULL, [gqa14d] [datetime] NULL, [gqa91c] [varchar](10) NULL, [gqa92d] [varchar](8) NULL, [gqa93c] [varchar](10) NULL, [gqa94d] [varchar](8) NULL, [gqa95c] [varchar](10) NULL, [gqa96d] [varchar](8) NULL, [gqa13t] [varchar](6) NULL, [gqa80d] [varchar](8) NULL, [gqa81c] [varchar](10) NULL, CONSTRAINT [PK_GQM01] PRIMARY KEY CLUSTERED ( [gqa00c] ASC, [gqa01c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[from_Setting] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[from_Setting]( [fra01c] [varchar](30) NULL, [fra02c] [varchar](60) NULL, [fra03c] [varchar](30) NULL, [fra04c] [varchar](60) NULL, [fra05c] [char](1) NULL, [fra06i] [int] NULL ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gsm01] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gsm01]( [gsa00c] [varchar](10) NOT NULL, [gsa01c] [varchar](8) NOT NULL, [gsa02c] [varchar](160) NULL, [gsa03f] [float] NULL, [gsa04c] [varchar](800) NULL, CONSTRAINT [PK_GSM01] PRIMARY KEY CLUSTERED ( [gsa00c] ASC, [gsa01c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[lag01] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[lag01]( [lga00c] [varchar](10) NOT NULL, [lga01c] [varchar](20) NOT NULL, [lga02c] [varchar](20) NULL, [lga03c] [varchar](20) NULL, [lga04c] [varchar](20) NULL, [lga05d] [varchar](8) NULL, [lga06f] [float] NULL, [lga07c] [varchar](20) NULL, [lga08d] [varchar](8) NULL, [lga09c] [varchar](20) NULL, [lga10d] [varchar](8) NULL, [lga11i] [int] NULL, [lga12c] [varchar](120) NULL, [lga13f] [float] NULL, CONSTRAINT [PK_LAG01] PRIMARY KEY CLUSTERED ( [lga00c] ASC, [lga01c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gdm05] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gdm05]( [gde00c] [varchar](10) NOT NULL, [gde01c] [varchar](20) NOT NULL, [gde02c] [varchar](20) NOT NULL, [gde03c] [varchar](20) NULL, [gde04c] [varchar](5) NULL, [gde05f] [float] NULL, [gde06f] [float] NULL, [gde07f] [float] NULL, [gde08i] [int] NULL, CONSTRAINT [PK_GDM05] PRIMARY KEY CLUSTERED ( [gde00c] ASC, [gde01c] ASC, [gde02c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_get_changed_cards] Script Date: 06/12/2017 06:20:06 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_get_changed_cards] ( @compid varchar(10), -- 公司别 @fromdate varchar(8), -- 开始日期 @todate varchar(8) -- 截止日期 ) as begin create table #changed_cards ( cardid varchar(20) not null, isnew int null, memid varchar(20) null ) insert #changed_cards(cardid, isnew) select distinct gcb01c, 1 from gcm02, gam26 z where gcb00c = z.gaz02c and z.gaz01c = @compid and gcb07d >= @fromdate and gcb07d <= @todate insert #changed_cards(cardid, isnew) select distinct gcd01c, 0 from gcm04 with (nolock), gam26 z where gcd00c = z.gaz02c and z.gaz01c = @compid and gcd09d >= @fromdate and gcd09d <= @todate and gcd01c not in (select cardid from #changed_cards) update a set a.memid = b.gca04c from #changed_cards a, gcm01 b, gam26 z where a.cardid = b.gca01c and b.gca00c = z.gaz02c and z.gaz01c = @compid select distinct cardid, isnew, memid from #changed_cards drop table #changed_cards end GO /****** Object: Table [dbo].[PrintableAccount] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[PrintableAccount]( [paa00c] [varchar](10) NOT NULL, [paa01c] [varchar](5) NOT NULL, [paa02i] [int] NOT NULL, CONSTRAINT [PK_PrintableAccount] PRIMARY KEY CLUSTERED ( [paa00c] ASC, [paa01c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gam15] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gam15]( [gao00c] [varchar](10) NOT NULL, [gao01c] [varchar](5) NOT NULL, [gao02f] [float] NOT NULL, [gao03f] [float] NOT NULL, [gao04c] [varchar](5) NULL, [gao05c] [varchar](5) NULL, [gao06f] [float] NULL, [gao07f] [float] NULL, [gao08f] [float] NULL, [gao10i] [int] NULL, [gao09c] [varchar](5) NULL, [gao11f] [float] NULL, CONSTRAINT [PK_GAM15] PRIMARY KEY CLUSTERED ( [gao00c] ASC, [gao01c] ASC, [gao02f] ASC, [gao03f] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[workdetail_by_item] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[workdetail_by_item]( [idd] [int] IDENTITY(1,1) NOT NULL, [person_id] [varchar](20) NOT NULL, [cls_flag] [int] NULL, [action_id] [tinyint] NOT NULL, [code] [varchar](20) NOT NULL, [name] [varchar](40) NULL, [amt] [float] NULL, [pt_amt] [float] NULL, [rt_amt] [float] NULL, [amt_f] [float] NULL, [amt_m] [float] NULL, [quan] [float] NULL, [pt_quan] [float] NULL, [rt_quan] [float] NULL, [quan_f] [float] NULL, [quan_m] [float] NULL, [amt2] [float] NULL, [pt_amt2] [float] NULL, [rt_amt2] [float] NULL, [cost] [float] NULL, [pt_cost] [float] NULL, [rt_cost] [float] NULL, [amt3] [float] NULL, [pt_amt3] [float] NULL, [rt_amt3] [float] NULL, [comm] [float] NULL, [pt_comm] [float] NULL, [rt_comm] [float] NULL, [prj_amt_rt] [float] NULL, [compid] [varchar](10) NULL, [fromdate] [varchar](10) NULL, [todate] [varchar](10) NULL, [spid] [int] NULL, CONSTRAINT [PK_workdetail_by_item] PRIMARY KEY NONCLUSTERED ( [idd] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_stat_sent_srv] Script Date: 06/12/2017 06:20:44 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_stat_sent_srv] ( @compid varchar(10), @fromdate varchar(20), @todate varchar(20), @flag int -- 1 明细 2 合计 ) as begin -- 缓冲gcm08 create table #gcm08_buf -- 会员卡--疗程变化历史 ( gch00c varchar(10) not null, --公司编号 gch01i int not null, -- 序号 gch02d varchar(8) null, -- 日期 gch03c varchar(20) not null, --卡号 gch04c varchar(20) not null, --疗程项目编号 gch05f float null, --购买次数 gch06f float null, --赠送次数 gch07f float null, --购买金额 gch08f float null, --使用次数 gch09f float null, --已经使用金额 gch10c varchar(20) null, --单据类别 gch11c varchar(20) null, --单号 gch12i int null, --价格序号 gch13f float null, --折扣 gch14f float null, --购买数量 gch15f float null, --单价 gch16i int null, --疗程中的序号 gch17c varchar(5) null, --支付方式1 gch18f float null, --支付方式1金额 gch19c varchar(5) null, --支付方式2 gch20f float null, --支付方式2金额 gch21c varchar(5) null, --支付方式3 gch22f float null, --支付方式3金额 gch23f float null, --前次次数 gch24c varchar(20) null, --代码(项目或产品) gch25i int null, --类型 1项目 2产品 gch26f float null, --序列号(ggm02或ggm03的流水号) gch27c varchar(5) null, --支付方式4 gch28f float null, --支付方式4金额 ) declare @PK_Sql nvarchar(max) set @PK_Sql = ' alter table #gcm08_buf add constraint PK_#gcm08_buf_' + convert(varchar(10), cast(floor(rand() * 1000000) as int)) + ' PRIMARY KEY NonCLUSTERED(gch01i)' exec sp_executesql @PK_Sql create clustered index idx_gcm08_buf on #gcm08_buf(gch00c, gch02d) insert #gcm08_buf(gch00c, gch01i, gch02d, gch03c, gch04c, gch05f, gch06f, gch07f, gch08f, gch09f, gch10c, gch11c, gch12i, gch13f, gch14f, gch15f, gch16i, gch17c, gch18f, gch19c, gch20f, gch21c, gch22f, gch23f, gch24c, gch25i, gch26f, gch27c, gch28f) select gch00c, gch01i, gch02d, gch03c, gch04c, gch05f, gch06f, gch07f, gch08f, gch09f, gch10c, gch11c, gch12i, gch13f, gch14f, gch15f, gch16i, gch17c, gch18f, gch19c, gch20f, gch21c, gch22f, gch23f, gch24c, gch25i, gch26f, gch27c, gch28f from gcm08 a with (nolock), gam26 z with (nolock) where a.gch00c = z.gaz02c and z.gaz01c = @compid and gch02d >= @fromdate and gch02d <= @todate create table #tbl_sent_record( idd int identity not null, memid varchar(20) null, memname varchar(20) null, cardid varchar(20) not null, goodid varchar(20) null, goodname varchar(60) null, itemid varchar(20) null, itemname varchar(60) null, buy_times float null, -- 购买次数 srv_times float null, -- 累计消费次数或每次消费的次数 remain_times float null, -- 总剩余次数 或当次剩余次数 srv_date varchar(20) null, -- 消费日期 compid varchar(10) null, compname varchar(60) null, procseq int null, ) set @PK_Sql = ' alter table #tbl_sent_record add constraint PK_#tbl_sent_record_' + convert(varchar(10), cast(floor(rand() * 1000000) as int)) + ' primary key nonclustered(idd)' exec sp_executesql @PK_Sql create clustered INDEX IDX_tbl_sent_record_01 on #tbl_sent_record(cardid, procseq) insert #tbl_sent_record(compid, cardid, goodid, itemid, buy_times, procseq, srv_date) select gch00c, gch03c, gch24c, gch04c, gch06f*gch14f, gch16i, gch02d from #gcm08_buf a with (nolock) where a.gch10c = 'gx' and isnull(gch25i, 0) = 2 --需要更新消费的次数,剩余的次数 update a set a.srv_times = b.gcf06f, a.remain_times = b.gcf07f from #tbl_sent_record a, gcm06 b with (nolock), gcm01 c with (nolock) where a.cardid = b.gcf01c and a.procseq = b.gcf23i and b.gcf00c = c.gca00c and b.gcf01c = c.gca01c and c.gca00c = c.gca13d --把消费的明细转入 insert #tbl_sent_record(compid, cardid, goodid, itemid, srv_times, procseq, srv_date) select gch00c, gch03c, '', gch04c, gch08f, gch16i, gch02d from #gcm08_buf a with (nolock), #tbl_sent_record b where a.gch10c = 'gx' and gch03c = b.cardid and gch04c = b.itemid and gch16i = b.procseq and isnull(gch08f, 0)>0 update a set a.goodname = b.gfa03c from #tbl_sent_record a, gfm01 b with (nolock) where a.compid = b.gfa00c and a.goodid = b.gfa01c and isnull(goodid, '')<>'' update a set a.itemname = b.gda03c from #tbl_sent_record a, gdm01 b with (nolock) where a.compid = b.gda00c and a.itemid = b.gda01c and isnull(itemid, '')<>'' update a set a.memid = b.gba01c, a.memname = b.gba03c from #tbl_sent_record a, gbm01 b with (nolock) where a.cardid = b.gba23c and a.compid = b.gba00c update a set a.compname = b.gae03c from #tbl_sent_record a, gam05 b with (nolock) where a.compid = b.gae01c if @flag = 1 begin select idd, isnull(memid ,'') memid,isnull(memname ,'') memname,isnull(cardid ,'') cardid,isnull(goodid ,'') goodid, isnull(goodname ,'') goodname,isnull(itemid ,'') itemid,isnull(itemname ,'') itemname,isnull(buy_times ,0) buy_times, isnull(srv_times ,0) srv_times,isnull(remain_times ,0) remain_times,isnull(srv_date ,'') srv_date,isnull(compid ,'') compid, isnull(compname ,'') compname,isnull(procseq ,'') procseq from #tbl_sent_record order by cardid, itemid, procseq end else begin select isnull(compid,'') compid, isnull(compname,'') compname, isnull(goodid,'') goodid, isnull(goodname,'') goodname, buy_times = isnull(sum(buy_times),0), srv_times = isnull(sum(srv_times),0), remain_times = isnull(sum(remain_times),0) from #tbl_sent_record where isnull(goodid, '') <> '' group by compid, compname, goodid, goodname order by compid, compname, goodid, goodname end drop table #tbl_sent_record drop table #gcm08_buf end GO /****** Object: StoredProcedure [dbo].[upg_get_changed_cards_cnt] Script Date: 06/12/2017 06:20:06 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_get_changed_cards_cnt]( @compid varchar(10), -- 公司别 @fromdate varchar(8), -- 开始日期 @todate varchar(8)) -- 截止日期 as begin create table #changed_cards( cardid varchar(20) not null, isnew int null, memid varchar(20) null) insert #changed_cards(cardid, isnew) select distinct gcb01c, 1 from gcm02, gam26 z where gcb00c = z.gaz02c and z.gaz01c = @compid and gcb07d >= @fromdate and gcb07d <= @todate insert #changed_cards(cardid, isnew) select distinct gcd01c, 0 from gcm04 with (nolock), gam26 z where gcd00c = z.gaz02c and z.gaz01c = @compid and gcd09d >= @fromdate and gcd09d <= @todate and gcd01c not in (select cardid from #changed_cards) update a set a.memid = b.gca04c from #changed_cards a, gcm01 b, gam26 z where a.cardid = b.gca01c and b.gca00c = z.gaz02c and z.gaz01c = @compid select count(*) from #changed_cards drop table #changed_cards end GO /****** Object: Table [dbo].[gam16] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gam16]( [gan00c] [varchar](10) NOT NULL, [gan01c] [varchar](5) NOT NULL, [gan02i] [int] NOT NULL, [gan03f] [float] NULL, CONSTRAINT [PK_GAM16] PRIMARY KEY CLUSTERED ( [gan00c] ASC, [gan01c] ASC, [gan02i] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gsm02] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gsm02]( [gsb00c] [varchar](10) NOT NULL, [gsb01c] [varchar](10) NOT NULL, [gsb02c] [varchar](5) NOT NULL, [gsb03c] [varchar](40) NULL, [gsb04c] [varchar](40) NOT NULL, [gsb05i] [int] NULL, [gsb06i] [int] NULL, CONSTRAINT [PK_GSM02] PRIMARY KEY CLUSTERED ( [gsb00c] ASC, [gsb01c] ASC, [gsb02c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[lag02] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[lag02]( [lgb00c] [varchar](10) NOT NULL, [lgb01c] [varchar](20) NOT NULL, [lgb02i] [int] NOT NULL, [lgb03c] [varchar](20) NULL, [lgb04c] [varchar](20) NULL, [lgb05f] [float] NULL, [lgb06f] [float] NULL, [lgb07f] [float] NULL, [lgb08i] [int] NULL, [lgb09c] [varchar](200) NULL, CONSTRAINT [PK_LAG02] PRIMARY KEY CLUSTERED ( [lgb00c] ASC, [lgb01c] ASC, [lgb02i] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gdm06] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gdm06]( [gdf00c] [varchar](10) NOT NULL, [gdf01c] [varchar](5) NOT NULL, [gdf02c] [varchar](5) NOT NULL, [gdf03i] [int] NULL, [gdf04i] [int] NULL, [gdf05i] [int] NULL, [gdf06f] [float] NULL, [gdf07f] [float] NULL, [gdf08f] [float] NULL, [gdf09f] [float] NULL, [gdf10f] [float] NULL, [gdf11f] [float] NULL, [gdf12f] [float] NULL, CONSTRAINT [PK_GDM06] PRIMARY KEY CLUSTERED ( [gdf00c] ASC, [gdf01c] ASC, [gdf02c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[workdetail_by_kind] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[workdetail_by_kind]( [person_id] [varchar](20) NOT NULL, [cls_flag] [int] NOT NULL, [prj_type] [varchar](20) NOT NULL, [oper_amt] [float] NULL, [total_vperf] [float] NULL, [total_tperf] [float] NULL, [comm] [float] NULL, [quan] [float] NULL, [f_oper_amt] [float] NULL, [f_total_vperf] [float] NULL, [f_total_tperf] [float] NULL, [f_comm] [float] NULL, [f_quan] [float] NULL, [t_oper_amt] [float] NULL, [t_total_vperf] [float] NULL, [t_total_tperf] [float] NULL, [t_comm] [float] NULL, [t_quan] [float] NULL, [compid] [varchar](10) NULL, [fromdate] [varchar](10) NULL, [todate] [varchar](10) NULL, [spid] [int] NOT NULL, [total_f_quan] [float] NULL, [total_cust_num] [float] NULL, CONSTRAINT [PK_workdetail_by_kind] PRIMARY KEY CLUSTERED ( [spid] ASC, [person_id] ASC, [cls_flag] ASC, [prj_type] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[hcm02_operation] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[hcm02_operation]( [id] [numeric](18, 0) IDENTITY(1,1) NOT NULL, [hcId] [numeric](18, 0) NOT NULL, [operation] [int] NOT NULL, [operator] [varchar](10) NOT NULL, [operateTime] [datetime] NOT NULL, [description] [varchar](1000) NOT NULL, [state] [int] NOT NULL, CONSTRAINT [PK_hcm02_operation] PRIMARY KEY CLUSTERED ( [id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_ymn_get_notuse_proc] Script Date: 06/12/2017 06:20:52 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_ymn_get_notuse_proc] ( @compid varchar(20), -- 公司代码 @fromdate varchar(10), -- 开始日期 @howlongdays int, -- 多少日期范围内不会到期 @pricetype int -- 价格序号 ) as begin declare @dt_end_date datetime declare @str_end_date varchar(20) select @dt_end_date = convert(datetime, @fromdate) select @dt_end_date = dateadd(day, @howlongdays, @dt_end_date) select @str_end_date = substring(convert(varchar(20), @dt_end_date, 102), 1, 4) + substring(convert(varchar(20), @dt_end_date, 102), 6, 2) + substring(convert(varchar(20), @dt_end_date, 102), 9, 2) --计算截至日期 create table #gcm06_buf -- 会员卡--疗程 ( gcf00c varchar(10) not null, --公司编号 gcf01c varchar(20) not null, --卡号 gcf02c varchar(20) not null, --项目编号 gcf03f float null, --次数 gcf04f float null, --赠送次数 gcf05f float null, --总次数 gcf06f float null, --已经使用次数 gcf07f float null, --剩余次数 gcf08f float null, --疗程金额 gcf09f float null, --已经使用金额 gcf10f float null, --剩余金额 gcf11d varchar(8) null, --账户开立日 gcf12d varchar(8) null, --账户截至日 gcf13i int null, --疗程间隔 gcf14f float null, --一个月可使用的次数 0--代表不控制 直到这个疗程用完其他float就是一个月可以使用的次数 gcf15c varchar(60) null, --输入的时候的备注 gcf16i int null, --是否已经审核 gcf17i int not null, --价格序号 gcf18f float not null, --折扣 gcf19i int null, --是否停用 2停用 1正常 3买产品赠送项目 gcf20i int null, --标志 是否前期卡 gcf21i int null, --是否赠送 gcf22i int null, --永久免费 gcf23i int not null, --序号 gcf24i int null, --开帐中的序号(gcm16.gcr23i) gcf25f float null, --虚假次数(只有增加部分) stdcnt float null, --销售的标准次数 lvlid varchar(20) null, --卡类别 issent int null, ) declare @PK_Sql nvarchar(max) set @PK_Sql = ' alter table #gcm06_buf add constraint PK_#gcm06_buf_' + convert(varchar(10), cast(floor(rand() * 1000000) as int)) + ' PRIMARY KEY NonCLUSTERED(gcf23i, gcf00c, gcf01c)' exec sp_executesql @PK_Sql create clustered index idx_gcm06_buf_1 on #gcm06_buf(gcf00c, gcf01c) create table #tbl_result ( compid varchar(20) not null, compname varchar(40) null, memid varchar(20) null, cardid varchar(20) null, memname varchar(30) null, zjidd varchar(40) null, --证件编号 lvlid varchar(20) null, --卡类别 lvlname varchar(40) null, prjid varchar(20) null, prjname varchar(40) null, source varchar(40) null, regdate varchar(20) null, cardnum float null, stdcnt float null, times float null, issent int null, idd int identity not null, ) set @PK_Sql = ' alter table #tbl_result add constraint PK_#tbl_result_' + convert(varchar(10), cast(floor(rand() * 1000000) as int)) + ' PRIMARY KEY NonCLUSTERED(idd)' exec sp_executesql @PK_Sql create clustered index idx_result_buf on #tbl_result(cardid, prjid) insert #gcm06_buf(gcf00c, gcf01c, gcf02c, gcf03f, gcf04f, gcf05f, gcf06f, gcf07f, gcf08f, gcf09f, gcf10f, gcf11d, gcf12d, gcf13i, gcf14f, gcf15c, gcf16i, gcf17i, gcf18f, gcf19i, gcf20i, gcf21i, gcf22i, gcf23i, gcf24i, gcf25f, lvlid) select gcf00c, gcf01c, gcf02c, gcf03f, gcf04f, gcf05f, gcf06f, gcf07f, gcf08f, gcf09f, gcf10f, gcf11d, gcf12d, gcf13i, gcf14f, gcf15c, gcf16i, gcf17i, gcf18f, gcf19i, gcf20i, gcf21i, gcf22i, gcf23i, gcf24i, gcf25f, gca02c from gcm06 f with (nolock), gcm01 a with (nolock), gam26 z where f.gcf00c = a.gca00c and f.gcf01c = a.gca01c and a.gca00c = a.gca13d and isnull(gcf12d, '20901231')>@str_end_date and a.gca00c = z.gaz02c and z.gaz01c = @compid -- 限制公司 and isnull(gcf06f, 0) = 0 -- 没有使用过 and isnull(gcf17i, 0) = @pricetype -- 价格序号 update a set a.issent = 1 from #gcm06_buf a where isnull(gcf19i, 0) = 3 or isnull(gcf21i, 0) = 1 update a set a.issent = 1 from #gcm06_buf a where isnull(gcf04f, 0) = isnull(gcf05f, 0) and isnull(gcf04f, 0)>0 if @pricetype = 1 begin update a set a.stdcnt = b.gda22f from #gcm06_buf a, gdm01 b where a.gcf00c = b.gda00c and a.gcf02c = b.gda01c end else if @pricetype = 2 begin update a set a.stdcnt = b.gda24f from #gcm06_buf a, gdm01 b where a.gcf00c = b.gda00c and a.gcf02c = b.gda01c end else if @pricetype = 3 begin update a set a.stdcnt = b.gda26f from #gcm06_buf a, gdm01 b where a.gcf00c = b.gda00c and a.gcf02c = b.gda01c end else if @pricetype = 4 begin update a set a.stdcnt = b.gda28f from #gcm06_buf a, gdm01 b where a.gcf00c = b.gda00c and a.gcf02c = b.gda01c end --删除剩余次数小于标准次数的 delete #gcm06_buf where isnull(gcf07f, 0)convert(decimal(20, 0), cardnum) update a set compname = b.gae03c from #tbl_result a, gam05 b where a.compid = b.gae01c update a set a.memid = b.gba01c, memname = b.gba03c, zjidd = gba16c from #tbl_result a, gbm01 b where a.compid = b.gba00c and a.cardid = b.gba23c update a set a.prjname = gda03c from #tbl_result a, gdm01 b where a.compid = b.gda00c and a.prjid = b.gda01c update a set a.lvlname = b.gak02c from #tbl_result a, gam10 b where a.compid = b.gak00c and a.lvlid = b.gak01c select * from #tbl_result drop table #gcm06_buf drop table #tbl_result end GO /****** Object: Table [dbo].[SHOP_REMIND] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[SHOP_REMIND]( [ID] [int] IDENTITY(1,1) NOT NULL, [COMPID] [varchar](20) NULL, [CARDID] [varchar](50) NULL, [MEMBERNAME] [varchar](50) NULL, [STATUS] [int] NULL, [MESSAGE] [varchar](50) NULL, [createDATE] [datetime] NULL, CONSTRAINT [PK_SHOP_REMIND] PRIMARY KEY CLUSTERED ( [ID] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[potential_member] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[potential_member]( [mobile] [varchar](20) NOT NULL, [ismember] [int] NULL, [bememberdate] [datetime] NULL, CONSTRAINT [PK_potential_member] PRIMARY KEY CLUSTERED ( [mobile] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gtc04_log] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gtc04_log]( [gte00c] [varchar](10) NOT NULL, [gte01c] [varchar](20) NOT NULL, [gte02c] [varchar](20) NOT NULL, [gte03c] [varchar](20) NOT NULL, [gte04f] [float] NULL, [gte05f] [float] NULL, [gte06f] [float] NULL, [gte08c] [varchar](20) NULL, [gte09i] [int] NULL, [gte10c] [varchar](20) NULL, [gte11c] [varchar](50) NULL, [idd] [int] IDENTITY(1,1) NOT NULL, [compid] [varchar](10) NULL, [userid] [varchar](20) NULL, [ddate] [varchar](10) NULL, [ttime] [varchar](10) NULL, CONSTRAINT [PK_GTC04_LOG] PRIMARY KEY CLUSTERED ( [idd] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gsm02_3] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gsm02_3]( [gsa01c] [varchar](10) NOT NULL, [gsa02c] [varchar](10) NOT NULL, [gsa03i] [int] NULL, [gsa04i] [int] NULL, [gsa05i] [int] NULL, [gsa06i] [int] NULL, [gsa07i] [int] NULL, [gsa08i] [int] NULL, CONSTRAINT [PK_GSM02_3] PRIMARY KEY CLUSTERED ( [gsa01c] ASC, [gsa02c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_cal_operation_result_depart] Script Date: 06/12/2017 06:19:33 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO --modify by lyj for 列印类型是数量的时候 项目的数量列印不正确/2007-08-15 create procedure [dbo].[upg_cal_operation_result_depart] ( @compid varchar(10), -- 公司别 @fromdate varchar(10), -- 开始日期 @todate varchar(10), -- 截至日期 @fromstaff varchar(20), -- 员工代码 @tostaff varchar(20), -- 员工代码 @fromdepart varchar(10), --部门 @todepart varchar(10), @amttype int ) -- 0 营业额 1 虚业绩 2-数量 as begin declare @printtype int --列印类型,add by lyj 保存@amttype的值/2007-08-15 set @printtype = @amttype declare @t_cnt float -- 总客数 declare @t_amt float -- 总业绩 declare @t_f_cnt float declare @t_f_amt float declare @t_n_cnt float declare @t_n_amt float declare @card_total_amt float declare @tmp_amt float declare @female_count float -- 女客 declare @male_count float -- 男客 declare @old_amt float declare @new_amt float declare @old_count float declare @new_count float declare @old_amt_tmp float declare @new_amt_tmp float if isnull(@fromstaff, '') = '*' select @fromstaff = '' --如果为数量, 也按营业额来生成报表 if @amttype!= 1 set @amttype = 0 create table #st_results_2( idd int identity not null, type varchar(10) not null, cnt float null, amt float null, price float null, primary key(idd)) create table #st_results( idd int identity not null, type varchar(10) not null, cnt float null, amt float null, price float null, cnt_f float null, -- 指定的数量 amt_f float null, -- 指定的金额 cnt_t float null, -- 轮班的数量 amt_t float null, -- 轮班的金额 tamt float null, -- 真实营业额 primary key(idd)) --add by lyj 添加列印是否打印新老客数 create table #st_bills_old ( idd int identity not null, billid varchar(20) not null, ) --add by lyj 添加列印是否打印新老客数 create table #st_bills_new ( idd int identity not null, billid varchar(20) not null, ) create table #st_bills( idd int identity not null, billid varchar(20) not null, -- 单号 srv_type varchar(10) null, gender int null, -- 性别 custtype int null, --0-新客 1--老客 primary key(idd)) --指定的单据 create table #st_bills_fix( idd int identity not null, billid varchar(20) not null, -- 单号 primary key(idd)) --不指定的单据 create table #st_bills_nofix( idd int identity not null, billid varchar(20) not null, -- 单号 primary key(idd)) create table #tbl_gnm01_gnm02( gna20c varchar(10) null, --负责业务 gna27f float null, --第一分享比率 gna28c varchar(10) null, --第二卖卡人 gna29f float null, --分享比率 gna30c varchar(10) null, --第三卖卡人 gna31f float null, --分享比率 gna67f float null, --第一销售虚业绩 Added by WUXM 2005/11/04 gna69f float null, --第二销售虚业绩 Added by WUXM 2005/11/04 gna71f float null, --第三销售虚业绩 Added by WUXM 2005/11/04 gna81c varchar(10) null, --部门编号 gna82c varchar(10) null, --部门编号2 gna83c varchar(10) null, --部门编号3 gnb08f float null --卡销售金额(包括疗程套餐的销售金额) ) create clustered index idx_gnm01_gnm02_01 on #tbl_gnm01_gnm02(gna81c) create nonclustered index idx_gnm01_gnm02_02 on #tbl_gnm01_gnm02(gna82c) create nonclustered index idx_gnm01_gnm02_03 on #tbl_gnm01_gnm02(gna83c) insert #tbl_gnm01_gnm02(gna20c, gna27f, gna28c, gna29f, gna30c, gna31f, gna67f, gna69f, gna71f, gna81c, gna82c, gna83c, gnb08f) select gna20c, gna27f, gna28c, gna29f, gna30c, gna31f, gna67f, gna69f, gna71f, gna81c, gna82c, gna83c, gnb08f from gnm01 a with (nolock), gnm02 b with (nolock) where b.gnb00c = a.gna00c and b.gnb01c = a.gna01c and a.gna00c = @compid and a.gna80d >= @fromdate and a.gna80d <= @todate create table #tbl_gcm10( gcl06i int null, --异动类别( 0充值, 1取款 5欠款 6还款) gcl07f float null, --异动金额(总收款金额), 含套餐,含疗程 gcl09c varchar(20) null, --销售人员 gcl16f float null, --第一销售分享比率 gcl17c varchar(20) null, --第二销售 gcl18f float null, --分享比率 gcl19c varchar(20) null, --第三负责人 gcl20f float null, --分享比率 gcl81c varchar(10) null, --部门编号 gcl82c varchar(10) null, --部门编号2 gcl83c varchar(10) null, --部门编号3 gcl58f float null, --第一销售虚业绩 Added by WUXM 2005/11/08 gcl60f float null, --第二销售虚业绩 Added by WUXM 2005/11/08 gcl62f float null --第三销售虚业绩 Added by WUXM 2005/11/08 ) create clustered index idx_gcm10_buf_01 on #tbl_gcm10(gcl81c) create nonclustered index idx_gcm10_buf_02 on #tbl_gcm10(gcl82c) create nonclustered index idx_gcm10_buf_03 on #tbl_gcm10(gcl83c) insert #tbl_gcm10(gcl06i, gcl07f, gcl09c, gcl16f, gcl17c, gcl18f, gcl19c, gcl20f, gcl81c, gcl82c, gcl83c, gcl58f, gcl60f, gcl62f) select gcl06i, gcl07f, gcl09c, gcl16f, gcl17c, gcl18f, gcl19c, gcl20f, gcl81c, gcl82c, gcl83c, gcl58f, gcl60f, gcl62f from gcm10 a with (nolock) where gcl00c = @compid and gcl80d >= @fromdate and gcl80d <= @todate --所有这段时间来得客户-- --按照部门统计的时候这个区分总客数区分不了,所以这里就不区分了 insert #st_bills(billid, gender, custtype) select gga01c, isnull(gga31i, 0), isnull(gga13f, 0) from ggm01 with (nolock) where gga00c = @compid and gga80d >= @fromdate and gga80d <= @todate -- 指定的总单据 if @fromstaff = '' begin --所有员工的指定的单据 insert #st_bills_fix(billid) select distinct ggb01c from ggm02 b with (nolock), ggm01 a with (nolock) where a.gga00c = b.ggb00c and a.gga01c = b.ggb01c and a.gga00c = @compid and a.gga80d >= @fromdate and a.gga80d <= @todate and isnull(b.ggb15c, '') = '1' --and (b.ggb81c between @fromdepart and @todepart or @fromdepart = '*')--add by lyj for department end else begin insert #st_bills_fix(billid) select distinct ggb01c from ggm02 b with (nolock), ggm01 a with (nolock) where a.gga00c = b.ggb00c and a.gga01c = b.ggb01c and a.gga00c = @compid and a.gga80d >= @fromdate and a.gga80d <= @todate --and (b.ggb81c between @fromdepart and @todepart or @fromdepart = '*')--add by lyj for department and ((isnull(b.ggb15c, '') = '1' and ggb12c >= @fromstaff and ggb12c <= @tostaff) or (isnull(b.ggb16c, '') = '1' and ggb13c >= @fromstaff and ggb13c <= @tostaff) or (isnull(b.ggb19c, '') = '1' and ggb14c >= @fromstaff and ggb14c <= @tostaff) or (isnull(b.ggb25c, '') = '1' and ggb26c >= @fromstaff and ggb26c <= @tostaff)) end --统计不指定的单据 if isnull(@fromstaff, '')<>'' begin --一个员工的不指定的单据 insert #st_bills_nofix(billid) select distinct ggb01c from ggm02 b with (nolock), ggm01 a with (nolock) where a.gga00c = b.ggb00c and a.gga01c = b.ggb01c and a.gga00c = @compid and a.gga80d >= @fromdate and a.gga80d <= @todate --and (b.ggb81c between @fromdepart and @todepart or @fromdepart = '*')--add by lyj for department and ((isnull(b.ggb15c, '') = '2' and ggb12c >= @fromstaff and ggb12c <= @tostaff) or (isnull(b.ggb16c, '') = '2' and ggb13c >= @fromstaff and ggb13c <= @tostaff) or (isnull(b.ggb19c, '') = '2' and ggb14c >= @fromstaff and ggb14c <= @tostaff) or (isnull(b.ggb25c, '') = '2' and ggb26c >= @fromstaff and ggb26c <= @tostaff)) end -- 新客数 if @fromstaff = '' begin --所有员工的 新客数 insert #st_bills_new(billid) select distinct ggb01c from ggm02 b, #st_bills a where b.ggb00c = @compid and a.billid = b.ggb01c and custtype = 0 --and (b.ggb81c between @fromdepart and @todepart or @fromdepart = '*')--add by lyj for department end else begin insert #st_bills_new(billid) select distinct ggb01c from ggm02 b, #st_bills a where b.ggb00c = @compid and a.billid = b.ggb01c and ((ggb12c >= @fromstaff and ggb12c <= @tostaff) or (ggb13c >= @fromstaff and ggb13c <= @tostaff) or (ggb14c >= @fromstaff and ggb14c <= @tostaff) or (ggb26c >= @fromstaff and ggb26c <= @tostaff)) --and (b.ggb81c between @fromdepart and @todepart or @fromdepart = '*')--add by lyj for department and custtype = 0 end -- 老客数 if @fromstaff = '' begin --所有员工的老客数 insert #st_bills_old(billid) select distinct ggb01c from ggm02 b, #st_bills a where b.ggb00c = @compid and a.billid = b.ggb01c and custtype = 1 --and (b.ggb81c between @fromdepart and @todepart or @fromdepart = '*')--add by lyj for department end else begin insert #st_bills_old(billid) select distinct ggb01c from ggm02 b, #st_bills a where b.ggb00c = @compid and a.billid = b.ggb01c and ((ggb12c >= @fromstaff and ggb12c <= @tostaff) or(ggb13c >= @fromstaff and ggb13c <= @tostaff) or (ggb14c >= @fromstaff and ggb14c <= @tostaff) or (ggb26c >= @fromstaff and ggb26c <= @tostaff)) and custtype = 1 --and (b.ggb81c between @fromdepart and @todepart or @fromdepart = '*')--add by lyj for department end if @fromstaff = '' begin --不区分员工 update a set a.srv_type = '1' from #st_bills a, #st_bills_fix b where a.billid = b.billid update a set a.srv_type = '2' from #st_bills a where isnull(srv_type, '') = '' --add by lyj 对于轮班总的单据,不可以这么insert的 要重新的select查询出结果然后再insert insert #st_bills_nofix(billid) select distinct ggb01c from ggm02 b with (nolock), ggm01 a with (nolock) where a.gga00c = b.ggb00c and a.gga01c = b.ggb01c and a.gga00c = @compid and a.gga80d >= @fromdate and a.gga80d <= @todate --and (b.ggb81c between @fromdepart and @todepart or @fromdepart = '*')--add by lyj for department and ((isnull(b.ggb15c, '') = '2') or (isnull(b.ggb16c, '') = '2' and isnull(b.ggb15c, '')<>'1') or (isnull(b.ggb19c, '') = '2' and isnull(b.ggb15c, '')<>'1' and isnull(b.ggb16c, '')<>'1') or (isnull(b.ggb25c, '') = '2' and isnull(b.ggb15c, '')<>'1' and isnull(b.ggb16c, '')<>'1' and isnull(b.ggb19c, '')<>'1')) and gga01c not in(select billid from #st_bills_fix) insert #st_bills_nofix(billid) select distinct ggc01c from ggm03 b with (nolock), ggm01 a with (nolock), #st_bills_nofix c where a.gga00c = b.ggc00c and a.gga01c = b.ggc01c and a.gga00c = @compid and a.gga80d between @fromdate and @todate --and (b.ggc81c between @fromdepart and @todepart or @fromdepart = '*') and gga01c<>c.billid end else begin delete a from #st_bills_nofix a, #st_bills_fix b where a.billid = b.billid update a set a.srv_type = '1' from #st_bills a, #st_bills_fix b where a.billid = b.billid update a set a.srv_type = '2' from #st_bills a, #st_bills_nofix b where a.billid = b.billid end delete #st_bills where isnull(srv_type, '') = '' --add by lyj 指定的客数和男女数量按部门统计不了,所以还是按照原来的全部 --指定总客数 select @t_f_cnt = count(*) from #st_bills where srv_type = '1' --统计男女的数量 select @female_count = count(*) from #st_bills where gender = 0 select @male_count = count(*) from #st_bills where gender = 1 --add by lyj 添加新老客数 select @old_count = count(*) from #st_bills_old select @new_count = count(*) from #st_bills_new if @fromstaff = '' begin if @amttype = 0 begin --指定客业绩 select @t_f_amt = sum(a.ggb11f) from ggm02 a, #st_bills_fix b where a.ggb01c = b.billid and a.ggb00c = @compid --and (a.ggb81c between @fromdepart and @todepart or @fromdepart = '*')--add by lyj for department --不定客业绩 select @t_n_amt = sum(a.ggb11f) from ggm02 a, #st_bills_nofix b where a.ggb01c = b.billid and a.ggb00c = @compid --and (a.ggb81c between @fromdepart and @todepart or @fromdepart = '*')--add by lyj for department --add by lyj 新客 select @new_amt = sum(a.ggb11f) from ggm02 a, #st_bills_new b where a.ggb01c = b.billid and a.ggb00c = @compid --and (a.ggb81c between @fromdepart and @todepart or @fromdepart = '*')--add by lyj for department --add by lyj 老客 select @old_amt = sum(a.ggb11f) from ggm02 a, #st_bills_old b where a.ggb01c = b.billid and a.ggb00c = @compid --and (a.ggb81c between @fromdepart and @todepart or @fromdepart = '*')--add by lyj for department end else if @amttype = 1 begin --指定客业绩 select @t_f_amt = sum(isnull(a.ggb20f, 0) + isnull(a.ggb21f, 0) + isnull(a.ggb22f, 0) + isnull(a.ggb35f, 0) ) from ggm02 a, #st_bills_fix b where a.ggb01c = b.billid and a.ggb00c = @compid --and (a.ggb81c between @fromdepart and @todepart or @fromdepart = '*')--add by lyj for department --不定客业绩 select @t_n_amt = sum(isnull(a.ggb20f, 0) + isnull(a.ggb21f, 0) + isnull(a.ggb22f, 0) + isnull(a.ggb35f, 0) ) from ggm02 a, #st_bills_nofix b where a.ggb01c = b.billid and a.ggb00c = @compid --and (a.ggb81c between @fromdepart and @todepart or @fromdepart = '*')--add by lyj for department end else if @amttype = 3 begin --新客 select @new_amt = sum(isnull(a.ggb36f, 0) + isnull(a.ggb37f, 0) + isnull(a.ggb38f, 0) + isnull(a.ggb39f, 0) ) from ggm02 a, #st_bills_new b where a.ggb01c = b.billid and a.ggb00c = @compid ---and (a.ggb81c between @fromdepart and @todepart or @fromdepart = '*')--add by lyj for department --老客 select @old_amt = sum(isnull(a.ggb36f, 0) + isnull(a.ggb37f, 0) + isnull(a.ggb38f, 0) + isnull(a.ggb39f, 0) ) from ggm02 a, #st_bills_old b where a.ggb01c = b.billid and a.ggb00c = @compid --and (a.ggb81c between @fromdepart and @todepart or @fromdepart = '*')--add by lyj for department --指定客业绩 select @t_f_amt = sum(isnull(a.ggb36f, 0) + isnull(a.ggb37f, 0) + isnull(a.ggb38f, 0) + isnull(a.ggb39f, 0) ) from ggm02 a, #st_bills_fix b where a.ggb01c = b.billid and a.ggb00c = @compid --and (a.ggb81c between @fromdepart and @todepart or @fromdepart = '*')--add by lyj for department --不定客业绩 select @t_n_amt = sum(isnull(a.ggb36f, 0) + isnull(a.ggb37f, 0) + isnull(a.ggb38f, 0) + isnull(a.ggb39f, 0) ) from ggm02 a, #st_bills_nofix b where a.ggb01c = b.billid and a.ggb00c = @compid --and (a.ggb81c between @fromdepart and @todepart or @fromdepart = '*')--add by lyj for department end end else begin --指定客业绩 if @amttype = 0 begin select @t_f_amt = sum(a.ggb11f) from ggm02 a, #st_bills_fix b where a.ggb01c = b.billid and a.ggb00c = @compid and ((ggb12c >= @fromstaff and ggb12c <= @tostaff) or (ggb13c >= @fromstaff and ggb13c <= @tostaff) or (ggb14c >= @fromstaff and ggb14c <= @tostaff) or (ggb26c >= @fromstaff and ggb26c <= @tostaff)) --and (a.ggb81c between @fromdepart and @todepart or @fromdepart = '*')--add by lyj for department --不定客业绩 select @t_n_amt = sum(a.ggb11f) from ggm02 a, #st_bills_nofix b where a.ggb01c = b.billid and a.ggb00c = @compid and ((ggb12c >= @fromstaff and ggb12c <= @tostaff) or (ggb13c >= @fromstaff and ggb13c <= @tostaff) or (ggb14c >= @fromstaff and ggb14c <= @tostaff) or (ggb26c >= @fromstaff and ggb26c <= @tostaff)) --and (a.ggb81c between @fromdepart and @todepart or @fromdepart = '*')--add by lyj for department ----------新客 select @new_amt = sum(a.ggb11f) from ggm02 a, #st_bills_new b where a.ggb01c = b.billid and a.ggb00c = @compid and ((ggb12c >= @fromstaff and ggb12c <= @tostaff) or (ggb13c >= @fromstaff and ggb13c <= @tostaff) or (ggb14c >= @fromstaff and ggb14c <= @tostaff) or (ggb26c >= @fromstaff and ggb26c <= @tostaff)) --and (a.ggb81c between @fromdepart and @todepart or @fromdepart = '*')--add by lyj for department --老客 select @old_amt = sum(a.ggb11f) from ggm02 a, #st_bills_old b where a.ggb01c = b.billid and a.ggb00c = @compid and ((ggb12c >= @fromstaff and ggb12c <= @tostaff) or (ggb13c >= @fromstaff and ggb13c <= @tostaff) or (ggb14c >= @fromstaff and ggb14c <= @tostaff) or (ggb26c >= @fromstaff and ggb26c <= @tostaff) or (ggb26c >= @fromstaff and ggb26c <= @tostaff)) --and (a.ggb81c between @fromdepart and @todepart or @fromdepart = '*')--add by lyj for department end else if @amttype = 1 begin --指定客业绩 --大工 declare @t_f_amt_tmp float select @t_f_amt_tmp = 0 select @t_f_amt_tmp = sum(isnull(a.ggb20f, 0)) from ggm02 a, #st_bills_fix b where a.ggb01c = b.billid and a.ggb00c = @compid and ((ggb12c >= @fromstaff and ggb12c <= @tostaff)) select @t_f_amt = isnull(@t_f_amt, 0) + isnull(@t_f_amt_tmp, 0) --中工业绩 select @t_f_amt_tmp = 0 select @t_f_amt_tmp = sum(isnull(a.ggb21f, 0)) from ggm02 a, #st_bills_fix b where a.ggb01c = b.billid and a.ggb00c = @compid and ((ggb13c >= @fromstaff and ggb13c <= @tostaff)) select @t_f_amt = isnull(@t_f_amt, 0) + isnull(@t_f_amt_tmp, 0) --小工业绩 select @t_f_amt_tmp = 0 select @t_f_amt_tmp = sum(isnull(a.ggb22f, 0)) from ggm02 a, #st_bills_fix b where a.ggb01c = b.billid and a.ggb00c = @compid and ((ggb14c >= @fromstaff and ggb14c <= @tostaff)) select @t_f_amt = isnull(@t_f_amt, 0) + isnull(@t_f_amt_tmp, 0) --四工业绩 select @t_f_amt_tmp = 0 select @t_f_amt_tmp = sum(isnull(a.ggb35f, 0)) from ggm02 a, #st_bills_fix b where a.ggb01c = b.billid and a.ggb00c = @compid and ((ggb26c >= @fromstaff and ggb26c <= @tostaff)) select @t_f_amt = isnull(@t_f_amt, 0) + isnull(@t_f_amt_tmp, 0) --不指定客业绩 declare @t_n_amt_tmp float --大工业绩 select @t_n_amt_tmp = 0 select @t_n_amt_tmp = sum(isnull(a.ggb20f, 0)) from ggm02 a, #st_bills_nofix b where a.ggb01c = b.billid and a.ggb00c = @compid and ((ggb12c >= @fromstaff and ggb12c <= @tostaff)) select @t_n_amt = isnull(@t_n_amt, 0) + isnull(@t_n_amt_tmp, 0) --中工业绩 select @t_n_amt_tmp = 0 select @t_n_amt_tmp = sum(isnull(a.ggb21f, 0)) from ggm02 a, #st_bills_nofix b where a.ggb01c = b.billid and a.ggb00c = @compid and ((ggb13c >= @fromstaff and ggb13c <= @tostaff)) select @t_n_amt = isnull(@t_n_amt, 0) + isnull(@t_n_amt_tmp, 0) --小工业绩 select @t_n_amt_tmp = 0 select @t_n_amt_tmp = sum(isnull(a.ggb22f, 0)) from ggm02 a, #st_bills_nofix b where a.ggb01c = b.billid and a.ggb00c = @compid and ((ggb14c >= @fromstaff and ggb14c <= @tostaff)) select @t_n_amt = isnull(@t_n_amt, 0) + isnull(@t_n_amt_tmp, 0) --四工业绩 select @t_n_amt_tmp = 0 select @t_n_amt_tmp = sum(isnull(a.ggb35f, 0)) from ggm02 a, #st_bills_nofix b where a.ggb01c = b.billid and a.ggb00c = @compid and ((ggb26c >= @fromstaff and ggb26c <= @tostaff)) select @t_n_amt = isnull(@t_n_amt, 0) + isnull(@t_n_amt_tmp, 0) --add by lyj --新客 --大工业绩 select @new_amt_tmp = 0 select @new_amt_tmp = sum(isnull(a.ggb20f, 0)) from ggm02 a, #st_bills_new b where a.ggb01c = b.billid and a.ggb00c = @compid and ((ggb12c >= @fromstaff and ggb12c <= @tostaff)) --and (a.ggb81c between @fromdepart and @todepart or @fromdepart = '*')--add by lyj for department select @new_amt = isnull(@new_amt, 0) + isnull(@new_amt_tmp, 0) --中工业绩 select @new_amt_tmp = 0 select @new_amt_tmp = sum(isnull(a.ggb21f, 0)) from ggm02 a, #st_bills_new b where a.ggb01c = b.billid and a.ggb00c = @compid and ((ggb13c >= @fromstaff and ggb13c <= @tostaff)) --and (a.ggb81c between @fromdepart and @todepart or @fromdepart = '*')--add by lyj for department select @new_amt = isnull(@new_amt, 0) + isnull(@new_amt_tmp, 0) --小工业绩 select @new_amt_tmp = 0 select @new_amt_tmp = sum(isnull(a.ggb22f, 0)) from ggm02 a, #st_bills_new b where a.ggb01c = b.billid and a.ggb00c = @compid and ((ggb14c >= @fromstaff and ggb14c <= @tostaff)) --and (a.ggb81c between @fromdepart and @todepart or @fromdepart = '*')--add by lyj for department select @new_amt = isnull(@t_n_amt, 0) + isnull(@new_amt_tmp, 0) --四工业绩 select @new_amt_tmp = 0 select @new_amt_tmp = sum(isnull(a.ggb35f, 0)) from ggm02 a, #st_bills_new b where a.ggb01c = b.billid and a.ggb00c = @compid and ((ggb26c >= @fromstaff and ggb26c <= @tostaff)) --and (a.ggb81c between @fromdepart and @todepart or @fromdepart = '*')--add by lyj for department select @new_amt = isnull(@t_n_amt, 0) + isnull(@new_amt_tmp, 0) --老客数 --大工业绩 select @old_amt_tmp = 0 select @old_amt_tmp = sum(isnull(a.ggb20f, 0)) from ggm02 a, #st_bills_old b where a.ggb01c = b.billid and a.ggb00c = @compid and ((ggb12c >= @fromstaff and ggb12c <= @tostaff)) --and (a.ggb81c between @fromdepart and @todepart or @fromdepart = '*')--add by lyj for department select @old_amt = isnull(@old_amt, 0) + isnull(@old_amt_tmp, 0) --中工业绩 select @old_amt_tmp = 0 select @old_amt_tmp = sum(isnull(a.ggb21f, 0)) from ggm02 a, #st_bills_old b where a.ggb01c = b.billid and a.ggb00c = @compid and ((ggb13c >= @fromstaff and ggb13c <= @tostaff)) --and (a.ggb81c between @fromdepart and @todepart or @fromdepart = '*')--add by lyj for department select @old_amt = isnull(@t_n_amt, 0) + isnull(@old_amt_tmp, 0) --小工业绩 select @old_amt_tmp = 0 select @old_amt_tmp = sum(isnull(a.ggb22f, 0)) from ggm02 a, #st_bills_old b where a.ggb01c = b.billid and a.ggb00c = @compid and ((ggb14c >= @fromstaff and ggb14c <= @tostaff)) --and (a.ggb81c between @fromdepart and @todepart or @fromdepart = '*')--add by lyj for department select @old_amt = isnull(@t_n_amt, 0) + isnull(@old_amt_tmp, 0) --四工业绩 select @old_amt_tmp = 0 select @old_amt_tmp = sum(isnull(a.ggb35f, 0)) from ggm02 a, #st_bills_old b where a.ggb01c = b.billid and a.ggb00c = @compid and ((ggb26c >= @fromstaff and ggb26c <= @tostaff)) --and (a.ggb81c between @fromdepart and @todepart or @fromdepart = '*')--add by lyj for department select @old_amt = isnull(@t_n_amt, 0) + isnull(@old_amt_tmp, 0) end else if @amttype = 3 begin --计算实业绩 --指定客业绩 --大工 select @t_f_amt_tmp = 0 select @t_f_amt_tmp = sum(isnull(a.ggb36f, 0)) from ggm02 a, #st_bills_fix b where a.ggb01c = b.billid and a.ggb00c = @compid and ((ggb12c >= @fromstaff and ggb12c <= @tostaff)) --and (a.ggb81c between @fromdepart and @todepart or @fromdepart = '*')--add by lyj for department select @t_f_amt = isnull(@t_f_amt, 0) + isnull(@t_f_amt_tmp, 0) --中工业绩 select @t_f_amt_tmp = 0 select @t_f_amt_tmp = sum(isnull(a.ggb37f, 0)) from ggm02 a, #st_bills_fix b where a.ggb01c = b.billid and a.ggb00c = @compid and ((ggb13c >= @fromstaff and ggb13c <= @tostaff)) --and (a.ggb81c between @fromdepart and @todepart or @fromdepart = '*')--add by lyj for department select @t_f_amt = isnull(@t_f_amt, 0) + isnull(@t_f_amt_tmp, 0) --小工业绩 select @t_f_amt_tmp = 0 select @t_f_amt_tmp = sum(isnull(a.ggb38f, 0)) from ggm02 a, #st_bills_fix b where a.ggb01c = b.billid and a.ggb00c = @compid and ((ggb14c >= @fromstaff and ggb14c <= @tostaff)) --and (a.ggb81c between @fromdepart and @todepart or @fromdepart = '*')--add by lyj for department select @t_f_amt = isnull(@t_f_amt, 0) + isnull(@t_f_amt_tmp, 0) --四工业绩 select @t_f_amt_tmp = 0 select @t_f_amt_tmp = sum(isnull(a.ggb39f, 0)) from ggm02 a, #st_bills_fix b where a.ggb01c = b.billid and a.ggb00c = @compid and ((ggb26c >= @fromstaff and ggb26c <= @tostaff)) --and (a.ggb81c between @fromdepart and @todepart or @fromdepart = '*')--add by lyj for department select @t_f_amt = isnull(@t_f_amt, 0) + isnull(@t_f_amt_tmp, 0) --不指定客业绩 --大工业绩 select @t_n_amt_tmp = 0 select @t_n_amt_tmp = sum(isnull(a.ggb36f, 0)) from ggm02 a, #st_bills_nofix b where a.ggb01c = b.billid and a.ggb00c = @compid and ((ggb12c >= @fromstaff and ggb12c <= @tostaff)) --and (a.ggb81c between @fromdepart and @todepart or @fromdepart = '*')--add by lyj for department select @t_n_amt = isnull(@t_n_amt, 0) + isnull(@t_n_amt_tmp, 0) --中工业绩 select @t_n_amt_tmp = 0 select @t_n_amt_tmp = sum(isnull(a.ggb37f, 0)) from ggm02 a, #st_bills_nofix b where a.ggb01c = b.billid and a.ggb00c = @compid and ((ggb13c >= @fromstaff and ggb13c <= @tostaff)) --and (a.ggb81c between @fromdepart and @todepart or @fromdepart = '*')--add by lyj for department select @t_n_amt = isnull(@t_n_amt, 0) + isnull(@t_n_amt_tmp, 0) --小工业绩 select @t_n_amt_tmp = 0 select @t_n_amt_tmp = sum(isnull(a.ggb38f, 0)) from ggm02 a, #st_bills_nofix b where a.ggb01c = b.billid and a.ggb00c = @compid and ((ggb14c >= @fromstaff and ggb14c <= @tostaff)) --and (a.ggb81c between @fromdepart and @todepart or @fromdepart = '*')--add by lyj for department select @t_n_amt = isnull(@t_n_amt, 0) + isnull(@t_n_amt_tmp, 0) --四工业绩 select @t_n_amt_tmp = 0 select @t_n_amt_tmp = sum(isnull(a.ggb39f, 0)) from ggm02 a, #st_bills_nofix b where a.ggb01c = b.billid and a.ggb00c = @compid and ((ggb26c >= @fromstaff and ggb26c <= @tostaff)) --and (a.ggb81c between @fromdepart and @todepart or @fromdepart = '*')--add by lyj for department select @t_n_amt = isnull(@t_n_amt, 0) + isnull(@t_n_amt_tmp, 0) --add by lyj --新客 --大工业绩 select @new_amt_tmp = 0 select @new_amt_tmp = sum(isnull(a.ggb36f, 0)) from ggm02 a, #st_bills_new b where a.ggb01c = b.billid and a.ggb00c = @compid and ((ggb12c >= @fromstaff and ggb12c <= @tostaff)) --and (a.ggb81c between @fromdepart and @todepart or @fromdepart = '*')--add by lyj for department select @new_amt = isnull(@new_amt, 0) + isnull(@new_amt_tmp, 0) --中工业绩 select @new_amt_tmp = 0 select @new_amt_tmp = sum(isnull(a.ggb37f, 0)) from ggm02 a, #st_bills_new b where a.ggb01c = b.billid and a.ggb00c = @compid and ((ggb13c >= @fromstaff and ggb13c <= @tostaff)) --and (a.ggb81c between @fromdepart and @todepart or @fromdepart = '*')--add by lyj for department select @new_amt = isnull(@new_amt, 0) + isnull(@new_amt_tmp, 0) --小工业绩 select @new_amt_tmp = 0 select @new_amt_tmp = sum(isnull(a.ggb38f, 0)) from ggm02 a, #st_bills_new b where a.ggb01c = b.billid and a.ggb00c = @compid and ((ggb14c >= @fromstaff and ggb14c <= @tostaff)) --and (a.ggb81c between @fromdepart and @todepart or @fromdepart = '*')--add by lyj for department select @new_amt = isnull(@t_n_amt, 0) + isnull(@new_amt_tmp, 0) --四工业绩 select @new_amt_tmp = 0 select @new_amt_tmp = sum(isnull(a.ggb39f, 0)) from ggm02 a, #st_bills_new b where a.ggb01c = b.billid and a.ggb00c = @compid and ((ggb26c >= @fromstaff and ggb26c <= @tostaff)) --and (a.ggb81c between @fromdepart and @todepart or @fromdepart = '*')--add by lyj for department select @new_amt = isnull(@t_n_amt, 0) + isnull(@new_amt_tmp, 0) --add by lyj 老客数 --大工业绩 select @old_amt_tmp = 0 select @old_amt_tmp = sum(isnull(a.ggb36f, 0)) from ggm02 a, #st_bills_old b where a.ggb01c = b.billid and a.ggb00c = @compid and ((ggb12c >= @fromstaff and ggb12c <= @tostaff)) --and (a.ggb81c between @fromdepart and @todepart or @fromdepart = '*')--add by lyj for department select @old_amt = isnull(@old_amt, 0) + isnull(@old_amt_tmp, 0) --中工业绩 select @old_amt_tmp = 0 select @old_amt_tmp = sum(isnull(a.ggb37f, 0)) from ggm02 a, #st_bills_old b where a.ggb01c = b.billid and a.ggb00c = @compid and ((ggb13c >= @fromstaff and ggb13c <= @tostaff)) --and (a.ggb81c between @fromdepart and @todepart or @fromdepart = '*')--add by lyj for department select @old_amt = isnull(@t_n_amt, 0) + isnull(@old_amt_tmp, 0) --小工业绩 select @old_amt_tmp = 0 select @old_amt_tmp = sum(isnull(a.ggb38f, 0)) from ggm02 a, #st_bills_old b where a.ggb01c = b.billid and a.ggb00c = @compid and ((ggb14c >= @fromstaff and ggb14c <= @tostaff)) --and (a.ggb81c between @fromdepart and @todepart or @fromdepart = '*')--add by lyj for department select @old_amt = isnull(@t_n_amt, 0) + isnull(@old_amt_tmp, 0) --四工业绩 select @old_amt_tmp = 0 select @old_amt_tmp = sum(isnull(a.ggb39f, 0)) from ggm02 a, #st_bills_old b where a.ggb01c = b.billid and a.ggb00c = @compid and ((ggb26c >= @fromstaff and ggb26c <= @tostaff)) --and (a.ggb81c between @fromdepart and @todepart or @fromdepart = '*')--add by lyj for department select @old_amt = isnull(@t_n_amt, 0) + isnull(@old_amt_tmp, 0) --end end end --不定客数 select @t_n_cnt = count(*) from #st_bills where srv_type = '2' --合计总的客数与总的业绩 select @t_cnt = isnull(@t_f_cnt, 0) + isnull(@t_n_cnt, 0), @t_amt = isnull(@t_f_amt, 0) + isnull(@t_n_amt, 0) --插入到返回结果的表中 --总的客数,总的金额 insert #st_results(type, cnt, amt) values('0-0', @t_cnt, @t_amt) --指定的客数,总的金额 insert #st_results(type, cnt, amt) values ('0-1', @t_f_cnt, @t_f_amt) --不指定的客数,总的金额 insert #st_results(type, cnt, amt) values('0-2', @t_n_cnt, @t_n_amt) --男女客数量 insert #st_results(type, cnt, amt) values('A-1', @female_count, @female_count) insert #st_results(type, cnt, amt) values('A-2', @male_count, @male_count) --add by lyj for 添加列印新老客数 declare @price float if (isnull(@new_count, 0) = 0) set @price = 0 else set @price = @new_amt/@new_count insert #st_results(type, cnt, amt, price) values('N-1', @new_count, @new_amt, @price) insert #st_results(type, cnt, amt, price) values('N-2', @old_count, @old_amt, @price) --不同统计类的 if @fromstaff = '' begin if (@amttype = 0 and @printtype = 0)--modify by lyj/2007-08-15 begin insert #st_results(type, cnt, amt, tamt) select s_type = gda13c, cnt = sum(ggb05f), amt = sum(isnull(a.ggb11f, 0)), tamt = sum(ggb11f*isnull(ggb34f, 1)) from ggm02 a with (nolock), gdm01 b, ggm01 c with (nolock) where a.ggb00c = b.gda00c and a.ggb03c = b.gda01c and a.ggb00c = @compid and a.ggb00c = c.gga00c and a.ggb01c = c.gga01c and c.gga80d >= @fromdate and c.gga80d <= @todate and isnull(gda13c, '')<>'' --and (a.ggb81c between @fromdepart and @todepart or @fromdepart = '*') group by gda13c end else if (@printtype = 2)--modify by lyj/2007-08-15 begin insert #st_results(type, cnt, amt, tamt) select s_type = gda13c, cnt = sum(ggb05f), amt = sum(isnull(a.ggb11f, 0)), tamt = sum(ggb11f*isnull(ggb34f, 1)) from ggm02 a with (nolock), gdm01 b, ggm01 c with (nolock) where a.ggb00c = b.gda00c and a.ggb03c = b.gda01c and a.ggb00c = @compid and a.ggb00c = c.gga00c and a.ggb01c = c.gga01c and c.gga80d >= @fromdate and c.gga80d <= @todate and isnull(gda13c, '')<>'' --and (a.ggb81c between @fromdepart and @todepart or @fromdepart = '*') group by gda13c end else if @amttype = 1 begin insert #st_results(type, cnt, amt) select s_type = gda13c, cnt = sum(ggb05f), amt = sum(isnull(a.ggb20f, 0) + isnull(a.ggb21f, 0) + isnull(a.ggb22f, 0) + isnull(a.ggb35f, 0) ) from ggm02 a with (nolock), gdm01 b, ggm01 c with (nolock) where a.ggb00c = b.gda00c and a.ggb03c = b.gda01c and a.ggb00c = @compid and a.ggb00c = c.gga00c and a.ggb01c = c.gga01c and c.gga80d >= @fromdate and c.gga80d <= @todate and isnull(gda13c, '')<>'' --and (a.ggb81c between @fromdepart and @todepart or @fromdepart = '*') group by gda13c end end else begin --一个员工范围的 if (@amttype = 0 and @printtype = 0)--modify by lyj/2007-08-15 begin insert #st_results(type, cnt, amt, tamt) select s_type = gda13c, cnt = sum(ggb05f), amt = sum(ggb11f), tamt = sum(ggb11f*isnull(ggb34f, 1)) from ggm02 a with (nolock), gdm01 b, ggm01 c with (nolock) where a.ggb00c = b.gda00c and a.ggb03c = b.gda01c and a.ggb00c = @compid and a.ggb00c = c.gga00c and a.ggb01c = c.gga01c and c.gga80d >= @fromdate and c.gga80d <= @todate and isnull(gda13c, '')<>'' and ((ggb12c >= @fromstaff and ggb12c <= @tostaff) or (ggb13c >= @fromstaff and ggb13c <= @tostaff) or (ggb14c >= @fromstaff and ggb14c <= @tostaff) or (ggb26c >= @fromstaff and ggb26c <= @tostaff)) --and (a.ggb81c between @fromdepart and @todepart or @fromdepart = '*') group by gda13c end else if (@printtype = 2)--modify by lyj/2007-08-15 begin insert #st_results(type, cnt, amt, tamt) select s_type = gda13c, cnt = sum(ggb05f), amt = sum(ggb11f), tamt = sum(ggb11f*isnull(ggb34f, 1)) from ggm02 a with (nolock), gdm01 b, ggm01 c with (nolock) where a.ggb00c = b.gda00c and a.ggb03c = b.gda01c and a.ggb00c = @compid and a.ggb00c = c.gga00c and a.ggb01c = c.gga01c and c.gga80d >= @fromdate and c.gga80d <= @todate and isnull(gda13c, '')<>'' and ((ggb12c >= @fromstaff and ggb12c <= @tostaff) or (ggb13c >= @fromstaff and ggb13c <= @tostaff) or (ggb14c >= @fromstaff and ggb14c <= @tostaff) or (ggb26c >= @fromstaff and ggb26c <= @tostaff)) --and (a.ggb81c between @fromdepart and @todepart or @fromdepart = '*') group by gda13c end else if @amttype = 1 begin --大工虚业绩 insert #st_results_2(type, cnt, amt) select s_type = gda13c, cnt = sum(ggb05f), amt = sum(isnull(ggb20f, 0)) from ggm02 a with (nolock), gdm01 b, ggm01 c with (nolock) where a.ggb00c = b.gda00c and a.ggb03c = b.gda01c and a.ggb00c = @compid and a.ggb00c = c.gga00c and a.ggb01c = c.gga01c and c.gga80d >= @fromdate and c.gga80d <= @todate and isnull(gda13c, '')<>'' and ((ggb12c >= @fromstaff and ggb12c <= @tostaff)) --and (a.ggb81c between @fromdepart and @todepart or @fromdepart = '*') group by gda13c --中工虚业绩 insert #st_results_2(type, cnt, amt) select s_type = gda13c, cnt = sum(ggb05f), amt = sum(isnull(ggb21f, 0)) from ggm02 a with (nolock), gdm01 b, ggm01 c with (nolock) where a.ggb00c = b.gda00c and a.ggb03c = b.gda01c and a.ggb00c = @compid and a.ggb00c = c.gga00c and a.ggb01c = c.gga01c and c.gga80d >= @fromdate and c.gga80d <= @todate and isnull(gda13c, '')<>'' and ((ggb13c >= @fromstaff and ggb13c <= @tostaff)) --and (a.ggb81c between @fromdepart and @todepart or @fromdepart = '*') group by gda13c --小工虚业绩 insert #st_results_2(type, cnt, amt) select s_type = gda13c, cnt = sum(ggb05f), amt = sum(isnull(ggb22f, 0)) from ggm02 a with (nolock), gdm01 b, ggm01 c with (nolock) where a.ggb00c = b.gda00c and a.ggb03c = b.gda01c and a.ggb00c = @compid and a.ggb00c = c.gga00c and a.ggb01c = c.gga01c and c.gga80d >= @fromdate and c.gga80d <= @todate and isnull(gda13c, '')<>'' and ((ggb14c >= @fromstaff and ggb14c <= @tostaff)) --and (a.ggb81c between @fromdepart and @todepart or @fromdepart = '*') group by gda13c --四工虚业绩 insert #st_results_2(type, cnt, amt) select s_type = gda13c, cnt = sum(ggb05f), amt = sum(isnull(ggb35f, 0)) from ggm02 a with (nolock), gdm01 b, ggm01 c with (nolock) where a.ggb00c = b.gda00c and a.ggb03c = b.gda01c and a.ggb00c = @compid and a.ggb00c = c.gga00c and a.ggb01c = c.gga01c and c.gga80d >= @fromdate and c.gga80d <= @todate and isnull(gda13c, '')<>'' and ((ggb26c >= @fromstaff and ggb26c <= @tostaff)) --and (a.ggb81c between @fromdepart and @todepart or @fromdepart = '*') group by gda13c insert #st_results(type, cnt, amt) select type, sum(isnull(cnt, 0)), sum(isnull(amt, 0)) from #st_results_2 group by type end end update #st_results set price = isnull(amt, 0)/isnull(cnt, 1) where cnt <> 0.0 update #st_results set price = 0 where cnt = 0.0 select @card_total_amt = 0 --卡销售 if isnull(@fromstaff, '') = '' or isnull(@fromstaff, '') = '*' begin if (@amttype<>1 and @amttype<>3) begin select @card_total_amt = 0 select @card_total_amt = sum(gnb08f*isnull(gna27f, 0)) from #tbl_gnm01_gnm02 a --where (a.gna81c between @fromdepart and @todepart or @fromdepart = '*') select @card_total_amt = isnull(@card_total_amt, 0) + sum(gnb08f*isnull(gna29f, 0)) from #tbl_gnm01_gnm02 a --where (a.gna82c between @fromdepart and @todepart or @fromdepart = '*') select @card_total_amt = isnull(@card_total_amt, 0) + sum(gnb08f*isnull(gna31f, 0)) from #tbl_gnm01_gnm02 a --where (a.gna83c between @fromdepart and @todepart or @fromdepart = '*') insert #st_results(type, amt) values('卡销售', @card_total_amt) --加上充值金额 select @tmp_amt = 0 select @tmp_amt = sum(gcl07f*isnull(gcl16f, 0)) from #tbl_gcm10 b where (gcl06i = 0 or gcl06i = 6) --and (gcl81c between @fromdepart and @todepart or @fromdepart = '*') select @tmp_amt = isnull(@tmp_amt, 0) + sum(gcl07f*isnull(gcl18f, 0)) from #tbl_gcm10 b where (gcl06i = 0 or gcl06i = 6) --and (gcl82c between @fromdepart and @todepart or @fromdepart = '*') select @tmp_amt = isnull(@tmp_amt, 0) + sum(gcl07f*isnull(gcl20f, 0)) from #tbl_gcm10 b where (gcl06i = 0 or gcl06i = 6) --and (gcl83c between @fromdepart and @todepart or @fromdepart = '*') select @card_total_amt = isnull(@card_total_amt, 0) + isnull(@tmp_amt, 0) select @tmp_amt = 0 select @tmp_amt = sum(gcl07f*isnull(gcl16f, 0)) from #tbl_gcm10 b where gcl06i = 5 --and (gcl81c between @fromdepart and @todepart or @fromdepart = '*') select @tmp_amt = isnull(@tmp_amt, 0) + sum(gcl07f*isnull(gcl18f, 0)) from #tbl_gcm10 b where gcl06i = 5 --and (gcl82c between @fromdepart and @todepart or @fromdepart = '*') select @tmp_amt = isnull(@tmp_amt, 0) + sum(gcl07f*isnull(gcl20f, 0)) from #tbl_gcm10 b where gcl06i = 5 --and (gcl83c between @fromdepart and @todepart or @fromdepart = '*') select @card_total_amt = isnull(@card_total_amt, 0) - isnull(@tmp_amt, 0) insert #st_results(type, amt) values('99-0', @card_total_amt) end else begin --因为按照部门统计的时候,这个单子归属部门是第一销售的所以,不需要统计第一销售的业绩 select @card_total_amt = 0 select @card_total_amt = isnull(@card_total_amt, 0) + sum(isnull(gna67f, 0)) from #tbl_gnm01_gnm02 a --where (a.gna81c between @fromdepart and @todepart or @fromdepart = '*') select @card_total_amt = isnull(@card_total_amt, 0) + sum(isnull(gna69f, 0)) from #tbl_gnm01_gnm02 a --where (a.gna82c between @fromdepart and @todepart or @fromdepart = '*') select @card_total_amt = isnull(@card_total_amt, 0) + sum(isnull(gna71f, 0)) from #tbl_gnm01_gnm02 a --where (a.gna83c between @fromdepart and @todepart or @fromdepart = '*') insert #st_results(type, amt) values('卡销售', @card_total_amt) --加上充值金额 select @tmp_amt = 0 select @tmp_amt = isnull(@tmp_amt, 0) + sum(isnull(gcl58f, 0)) from #tbl_gcm10 b where (gcl06i = 0 or gcl06i = 6 or gcl06i = 5) --and (gcl81c between @fromdepart and @todepart or @fromdepart = '*') select @tmp_amt = isnull(@tmp_amt, 0) + sum(isnull(gcl60f, 0)) from #tbl_gcm10 b where (gcl06i = 0 or gcl06i = 6 or gcl06i = 5) --and (gcl82c between @fromdepart and @todepart or @fromdepart = '*') select @tmp_amt = isnull(@tmp_amt, 0) + sum(isnull(gcl62f, 0)) from #tbl_gcm10 b where (gcl06i = 0 or gcl06i = 6 or gcl06i = 5) --and (gcl83c between @fromdepart and @todepart or @fromdepart = '*') select @card_total_amt = isnull(@card_total_amt, 0) + isnull(@tmp_amt, 0) insert #st_results(type, amt) values('99-0', @card_total_amt) end --因为要分部门统计 所以这个要分卡统计,现在分成#st_bills_fix 和#st_bills_nofix declare @tmpamt01 float select @tmpamt01 = sum(gsc05f) from gsm03 a, #st_bills_fix b, pay_classify g where a.gsc01c = b.billid and a.gsc02c = 'gx' and a.gsc00c = @compid and g.comp = @compid and g.pay = a.gsc04c and g.category = 2 select @tmpamt01 = isnull(@tmpamt01, 0) + sum(gsc05f) from gsm03 a, #st_bills_nofix b, pay_classify g where a.gsc01c = b.billid and a.gsc02c = 'gx' and a.gsc00c = @compid and g.comp = @compid and g.pay = a.gsc04c and g.category = 2 insert #st_results(type, amt) values('99-1', @tmpamt01) --end --外卖 insert #st_results(type, amt) select '99-2', sum(ggc11f) from ggm03 b with (nolock), ggm01 a with (nolock) where a.gga00c = b.ggc00c and a.gga01c = b.ggc01c and a.gga80d >= @fromdate and a.gga80d <= @todate and a.gga00c = @compid --and (b.ggc81c between @fromdepart and @todepart or @fromdepart = '*') --add by lyj for depart end else begin -- 员工 --卖卡 if (@amttype<>1 and @amttype<>3) begin select @card_total_amt = 0 select @card_total_amt = isnull(@card_total_amt, 0) + sum(gnb08f*isnull(gna27f, 0)) from #tbl_gnm01_gnm02 a where (a.gna20c >= @fromstaff and a.gna20c <= @tostaff) --and (a.gna81c between @fromdepart and @todepart or @fromdepart = '*') select @card_total_amt = isnull(@card_total_amt, 0) + sum(gnb08f*isnull(gna29f, 0)) from #tbl_gnm01_gnm02 a where (a.gna28c >= @fromstaff and a.gna28c <= @tostaff) --and (a.gna82c between @fromdepart and @todepart or @fromdepart = '*') select @card_total_amt = isnull(@card_total_amt, 0) + sum(gnb08f*isnull(gna31f, 0)) from #tbl_gnm01_gnm02 a where (a.gna30c >= @fromstaff and a.gna30c <= @tostaff) --and (a.gna83c between @fromdepart and @todepart or @fromdepart = '*') --加上充值金额 select @tmp_amt = 0 select @tmp_amt = isnull(@tmp_amt, 0) + sum(gcl07f*isnull(gcl16f, 0)) from #tbl_gcm10 b where (gcl06i = 0 or gcl06i = 6) and (gcl09c >= @fromstaff and gcl09c <= @tostaff) --and (gcl81c between @fromdepart and @todepart or @fromdepart = '*') select @tmp_amt = isnull(@tmp_amt, 0) + sum(gcl07f*isnull(gcl18f, 0)) from #tbl_gcm10 b where (gcl06i = 0 or gcl06i = 6) and (gcl17c >= @fromstaff and gcl17c <= @tostaff) --and (gcl82c between @fromdepart and @todepart or @fromdepart = '*') select @tmp_amt = isnull(@tmp_amt, 0) + sum(gcl07f*isnull(gcl20f, 0)) from #tbl_gcm10 b where (gcl06i = 0 or gcl06i = 6) and (gcl19c >= @fromstaff and gcl19c <= @tostaff) --and (gcl83c between @fromdepart and @todepart or @fromdepart = '*') select @card_total_amt = isnull(@card_total_amt, 0) + isnull(@tmp_amt, 0) select @tmp_amt = 0 select @tmp_amt = isnull(@tmp_amt, 0) + sum(gcl07f*isnull(gcl16f, 0)) from #tbl_gcm10 b where (gcl06i = 5) and (gcl09c >= @fromstaff and gcl09c <= @tostaff) --and (gcl81c between @fromdepart and @todepart or @fromdepart = '*') select @tmp_amt = isnull(@tmp_amt, 0) + sum(gcl07f*isnull(gcl18f, 0)) from #tbl_gcm10 b where (gcl06i = 5) and (gcl17c >= @fromstaff and gcl17c <= @tostaff) --and (gcl82c between @fromdepart and @todepart or @fromdepart = '*') select @tmp_amt = isnull(@tmp_amt, 0) + sum(gcl07f*isnull(gcl20f, 0)) from #tbl_gcm10 b where (gcl06i = 5) and (gcl19c >= @fromstaff and gcl19c <= @tostaff) --and (gcl83c between @fromdepart and @todepart or @fromdepart = '*') select @card_total_amt = isnull(@card_total_amt, 0) + isnull(@tmp_amt, 0) insert #st_results(type, amt) values('99-0', @card_total_amt) end else begin select @card_total_amt = 0 select @card_total_amt = isnull(@card_total_amt, 0) + sum(isnull(gna67f, 0)) from #tbl_gnm01_gnm02 a where (a.gna20c >= @fromstaff and a.gna20c <= @tostaff) --and (a.gna81c between @fromdepart and @todepart or @fromdepart = '*') --add by lyj for depart select @card_total_amt = isnull(@card_total_amt, 0) + sum(isnull(gna69f, 0)) from #tbl_gnm01_gnm02 a where (a.gna28c >= @fromstaff and a.gna28c <= @tostaff) --and (a.gna82c between @fromdepart and @todepart or @fromdepart = '*') --add by lyj for depart select @card_total_amt = isnull(@card_total_amt, 0) + sum(isnull(gna71f, 0)) from #tbl_gnm01_gnm02 a where (a.gna30c >= @fromstaff and a.gna30c <= @tostaff) --and (a.gna83c between @fromdepart and @todepart or @fromdepart = '*') --add by lyj for depart select @tmp_amt = 0 select @tmp_amt = isnull(@tmp_amt, 0) + sum(isnull(gcl58f, 0)) from #tbl_gcm10 b where (gcl06i = 0 or gcl06i = 6 or gcl06i = 5) --and (gcl81c between @fromdepart and @todepart or @fromdepart = '*') and (gcl09c >= @fromstaff and gcl09c <= @tostaff) select @tmp_amt = isnull(@tmp_amt, 0) + sum(isnull(gcl60f, 0)) from #tbl_gcm10 b where (gcl06i = 0 or gcl06i = 6 or gcl06i = 5) --and (gcl82c between @fromdepart and @todepart or @fromdepart = '*') and (gcl17c >= @fromstaff and gcl17c <= @tostaff) select @tmp_amt = isnull(@tmp_amt, 0) + sum(isnull(gcl62f, 0)) from #tbl_gcm10 b where (gcl06i = 0 or gcl06i = 6 or gcl06i = 5) --and (gcl83c between @fromdepart and @todepart or @fromdepart = '*') and (gcl19c >= @fromstaff and gcl19c <= @tostaff) select @card_total_amt = isnull(@card_total_amt, 0) + isnull(@tmp_amt, 0) insert #st_results(type, amt) values('99-0', @card_total_amt) end --add by lyj 现在要分开统计 因为#st_bills 没有过滤部门 --所以要分成 #st_bills_fix 和#st_bills_nofix统计 declare @tmpamt02 float select @tmpamt02 = sum(gsc05f) from gsm03 a, #st_bills_fix b, pay_classify g where a.gsc01c = b.billid and a.gsc02c = 'gx' and a.gsc00c = @compid and g.comp = @compid and g.pay = a.gsc04c and g.category = 2 select @tmpamt02 = isnull(@tmpamt02, 0) + sum(gsc05f) from gsm03 a, #st_bills_nofix b, pay_classify g where a.gsc01c = b.billid and a.gsc02c = 'gx' and a.gsc00c = @compid and g.comp = @compid and g.pay = a.gsc04c and g.category = 2 insert #st_results(type, amt) values('99-1', @tmpamt02) --end --外卖 insert #st_results(type, amt) select '99-2', sum(ggc11f) from ggm03 b with (nolock), ggm01 a with (nolock) where a.gga00c = b.ggc00c and a.gga01c = b.ggc01c and a.gga80d >= @fromdate and a.gga80d <= @todate and a.gga00c = @compid and ((ggc12c >= @fromstaff and ggc12c <= @tostaff) or (ggc15c >= @fromstaff and ggc15c <= @tostaff) or (ggc17c >= @fromstaff and ggc17c <= @tostaff)) --and (b.ggc81c between @fromdepart and @todepart or @fromdepart = '*')--add by lyj for depart end select week = case (datepart(dw, cast(@fromdate as datetime))) when 1 then '星期日' when 2 then '星期一' when 3 then '星期二' when 4 then '星期三' when 5 then '星期四' when 6 then '星期五' when 7 then '星期六' end, type, cnt, amt, price, cnt_f, amt_f, cnt_t, amt_t, tamt from #st_results order by type drop table #st_bills drop table #st_bills_fix drop table #st_bills_nofix drop table #st_results drop table #st_results_2 drop table #tbl_gnm01_gnm02 drop table #tbl_gcm10 end GO /****** Object: Table [dbo].[gzm06] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gzm06]( [gzf00c] [varchar](10) NOT NULL, [gzf01c] [varchar](5) NOT NULL, [gzf02c] [varchar](5) NOT NULL, [gzf03f] [float] NOT NULL, [gzf04f] [float] NULL, [gzf05f] [float] NULL, [gzf06i] [int] NULL, [gzf07i] [int] NULL, CONSTRAINT [PK_GZM06] PRIMARY KEY CLUSTERED ( [gzf00c] ASC, [gzf01c] ASC, [gzf02c] ASC, [gzf03f] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_compare_compid] Script Date: 06/12/2017 06:19:43 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_compare_compid] ( @compid nvarchar(2000), -- 公司别 @fromdate varchar(10), -- 开始日期 @todate varchar(10), -- 截至日期 @amttype int -- 0-营业额 1-虚业绩 ) as begin declare @sql nvarchar(4000) create table #st_results ( idd int identity not null, compid varchar(10) not null, type varchar(10) not null, date varchar(8) default '' null, billid varchar(20) default '' null, total_cnt float default 0 null, -- 总数 total_amt float default 0 null, -- 总业绩 unit_price float default 0 null, -- 客单价 fix_cnt float default 0 null, -- 指定客数 notfix_cnt float default 0 null, -- 不指定客数 fix_amt float default 0 null, -- 指定业绩 notfix_amt float default 0 null, -- 不指定业绩 card_total_amt float default 0 null, -- 售卡金额 use_card_amt float default 0 null, -- 用卡数 take_way float default 0 null, -- 外卖 female_count float default 0 null, -- 女客 male_count float default 0 null, -- 男客 primary key(idd) ) create table #st_results2 ( idd int identity not null, compid varchar(10) not null, type varchar(10) not null, printdate varchar(20) default '' null, date varchar(20) default '' null, billid varchar(20) default '' null, total_cnt float default 0 null, --总数 total_amt float default 0 null, --总业绩 unit_price float default 0 null, --客单价 fix_cnt float default 0 null, --指定客数 notfix_cnt float default 0 null, --不指定客数 fix_amt float default 0 null, --指定业绩 notfix_amt float default 0 null, --不指定业绩 card_total_amt float default 0 null, --售卡金额 use_card_amt float default 0 null, --用卡数 take_way float default 0 null, --外卖 female_count float default 0 null, -- 女客 male_count float default 0 null, -- 男客 rate float default 0 null, --增长率 primary key(idd) ) create table #st_bills ( idd int identity not null, compid varchar(10) not null, -- 公司 date varchar(8) not null, -- 日期 billid varchar(20) not null, -- 单号 srv_type varchar(10) null, -- 1-指定单据 2-非指定单据 gender int null, -- 性别 primary key(idd) ) --指定的单据 create table #st_bills_fix( idd int identity not null, compid varchar(10) not null, date varchar(8) not null, billid varchar(20) not null, -- 单号 primary key(idd)) --所有这段时间来得客户 set @sql = N'insert #st_bills(compid, billid, date, gender) select gga00c, gga01c, substring(gga80d, 1, 6), isnull(gga31i, 0) from ggm01 where gga00c in' + @compid + ' and substring(gga80d, 1, 6) >= ' + @fromdate + ' and substring(gga80d, 1, 6) <= ' + @todate execute sp_executesql @sql set @sql = '' --所有员工的指定的单据 set @sql = N'insert #st_bills_fix(compid, date, billid) select distinct ggb00c, substring(gga80d, 1, 6), ggb01c from ggm02 b with (nolock), ggm01 a with (nolock) where a.gga00c = b.ggb00c and a.gga01c = b.ggb01c and a.gga00c in' + @compid + ' and substring(a.gga80d, 1, 6) >= ' + @fromdate + ' and substring(a.gga80d, 1, 6) <= ' + @todate + ' and isnull(b.ggb15c, '''') = ''1''' execute sp_executesql @sql --更新指定的单据 update a set a.srv_type = '1' from #st_bills a, #st_bills_fix b where a.billid = b.billid and a.compid = b.compid --更新不指定的单据 update a set a.srv_type = '2' from #st_bills a where isnull(srv_type, '') = '' insert #st_results(compid, date, type) select distinct compid, date, 'T_T' from #st_bills group by compid, date if @amttype = 0 --营业额显示 begin --指定客业绩 update c set c.fix_amt = (select sum(isnull(a.ggb11f, 0)) from ggm02 a, #st_bills b where a.ggb00c = b.compid and a.ggb00c = c.compid and a.ggb01c = b.billid and b.srv_type = '1' and b.date = c.date group by compid, date) from #st_results c --不定客业绩 update c set c.notfix_amt = (select sum(isnull(a.ggb11f, 0)) from ggm02 a, #st_bills b where a.ggb00c = b.compid and a.ggb00c = c.compid and a.ggb01c = b.billid and b.srv_type = '2' and b.date = c.date group by compid, date) from #st_results c end else if @amttype = 1 --虚业绩显示 begin --指定客业绩 update c set c.fix_amt = (select sum(isnull(a.ggb20f, 0) + isnull(a.ggb21f, 0) + isnull(a.ggb22f, 0) + isnull(a.ggb35f, 0)) from ggm02 a, #st_bills b where a.ggb00c = b.compid and a.ggb00c = c.compid and a.ggb01c = b.billid and b.srv_type = '1'and b.date = c.date group by compid, date) from #st_results c --不定客业绩 update c set c.notfix_amt = (select sum(isnull(a.ggb20f, 0) + isnull(a.ggb21f, 0) + isnull(a.ggb22f, 0) + isnull(a.ggb35f, 0)) from ggm02 a, #st_bills b where a.ggb00c = b.compid and a.ggb00c = c.compid and a.ggb01c = b.billid and b.srv_type = '2' and b.date = c.date group by compid, date) from #st_results c end --指定总客数 update b set b.fix_cnt = (select count(*) from #st_bills a where a.srv_type = '1' and a.compid = b.compid and a.date = b.date group by a.compid, date) from #st_results b --不指定客数 update b set b.notfix_cnt = (select count(*) from #st_bills a where a.srv_type = '2' and a.compid = b.compid and a.date = b.date group by a.compid, date) from #st_results b --更新男女的数量 update b set b.male_count = (select count(*) from #st_bills a where a.gender = 1 and a.compid = b.compid and a.date = b.date group by a.compid, date, a.gender) from #st_results b update b set b.female_count = (select count(*) from #st_bills a where a.gender = 1 and a.compid = b.compid and a.date = b.date group by a.compid, date, a.gender) from #st_results b --总的客数 update b set b.total_cnt = isnull(b.fix_cnt, 0) + isnull(b.notfix_cnt, 0) from #st_results b --总的业绩 update b set b.total_amt = isnull(b.fix_amt, 0) + isnull(b.notfix_amt, 0) from #st_results b --客单价 update b set b.unit_price = isnull(total_amt, 0)/isnull(total_cnt, 1) from #st_results b --卡销售 update c set c.card_total_amt = (select sum(isnull(gnb08f, 0)) from gnm02, gnm01 where gnb00c = gna00c and gnb01c = gna01c and gna00c = c.compid and gna80d >= @fromdate and gna80d <= @todate and substring(gna80d, 1, 6) = c.date) from #st_results c --加上充值金额 update c set c.card_total_amt = c.card_total_amt + (select sum(gcl07f) from gcm10 where gcl00c = c.compid and gcl80d >= @fromdate and gcl80d <= @todate and (gcl06i = 0 or gcl06i = 6) and substring(gcl80d, 1, 6) = c.date) from #st_results c update c set c.card_total_amt = c.card_total_amt-(select sum(gcl07f) from gcm10 where gcl00c = c.compid and gcl80d >= @fromdate and gcl80d <= @todate and gcl06i = 5 and substring(gcl80d, 1, 6) = c.date) from #st_results c --用卡 update c set c.use_card_amt = (select sum(gsc05f) from gsm03, ggm01 where gsc00c = gga00c and gsc01c = gga01c and gsc02c = 'gx' and (gsc04c = '#' or gsc04c = '4' or gsc04c >= '9') and gga80d >= @fromdate and gga80d <= @todate and gga00c = c.compid and substring(gga80d, 1, 6) = c.date) from #st_results c --外卖 update c set c.take_way = ( select sum(ggc11f) from ggm03, ggm01 where gga00c = ggc00c and gga01c = ggc01c and gga80d >= @fromdate and gga80d <= @todate and gga00c = c.compid and substring(gga80d, 1, 6) = c.date) from #st_results c -------整理报表--插入差额 和月份平局值------------------- ------------cursor--------------------- declare @compidex varchar(10) declare @date varchar(8) declare @type varchar(20) declare @beforedate varchar(20) declare @currdate varchar(20) declare @beforecompid varchar(20) declare @currcompid varchar(10) declare @total_cnt float declare @total_amt float declare @unit_price float declare @fix_cnt float declare @notfix_cnt float declare @fix_amt float declare @notfix_amt float declare @card_total_amt float declare @use_card_amt float declare @take_way float declare @female_count float declare @male_count float declare @days int --这个月有多少天 declare @count int --月份之差是2个月的时候就算差额 declare @idd int set @count = 0 declare insert_foreach cursor for select compid, date, type from #st_results order by compid, date open insert_foreach fetch insert_foreach into @compidex, @date, @type while @@fetch_status = 0 begin if (@count<2) begin insert #st_results2(compid, type, printdate, date, billid, total_cnt, total_amt, unit_price, fix_cnt, notfix_cnt, fix_amt , notfix_amt, card_total_amt, use_card_amt, take_way, female_count, male_count) select compid, 'T', substring(date, 5, 2) + '月份总额', date, billid, isnull(total_cnt, 0), isnull(total_amt, 0), isnull(unit_price, 0), isnull(fix_cnt, 0), isnull(notfix_cnt, 0), isnull(fix_amt, 0), isnull(notfix_amt, 0), isnull(card_total_amt, 0), isnull(use_card_amt, 0), isnull(take_way, 0), isnull(female_count, 0), isnull(male_count, 0) from #st_results a where a.compid = @compidex and a.date = @date select @currcompid = compid, @currdate = date, @total_cnt = isnull(total_cnt, 0), @total_amt = isnull(total_amt, 0), @unit_price = isnull(unit_price, 0), @fix_cnt = isnull(fix_cnt, 0), @notfix_cnt = isnull(notfix_cnt, 0), @fix_amt = isnull(fix_amt, 0), @notfix_amt = isnull(notfix_amt, 0), @card_total_amt = isnull(card_total_amt, 0), @use_card_amt = isnull(use_card_amt, 0), @take_way = isnull(take_way, 0), @female_count = isnull(female_count, 0), @male_count = isnull(male_count, 0) from #st_results a where a.compid = @compidex and a.date = @date if (@compidex!= @beforecompid) set @count = 1 else begin set @count = @count + 1 end end else if (@count = 2) begin if (@currcompid = @beforecompid) begin --更新增长率 select @idd = scope_identity() update b set b.rate = (select rate = case when @total_amt = 0 then 0 else (@total_amt-isnull(total_amt, 0))/isnull(@total_amt, 1) end from #st_results a where a.compid = @currcompid and a.date = @beforedate) from #st_results2 b where idd = @idd --插入总差额 insert #st_results2(compid, type, printdate, date, billid, total_cnt, total_amt, unit_price, fix_cnt, notfix_cnt, fix_amt , notfix_amt, card_total_amt, use_card_amt, take_way, female_count, male_count) select @currcompid, 'T_T', '差额', date + @date, billid, @total_cnt-isnull(total_cnt, 0), @total_amt-isnull(total_amt, 0), @unit_price-isnull(unit_price, 0), @fix_cnt-isnull(fix_cnt, 0), @notfix_cnt-isnull(notfix_cnt, 0), @fix_amt-isnull(fix_amt, 0), @notfix_amt-isnull(notfix_amt, 0), @card_total_amt-isnull(card_total_amt, 0), @use_card_amt-isnull(use_card_amt, 0), @take_way-isnull(take_way, 0), @female_count-isnull(female_count, 0), @male_count-isnull(male_count, 0) from #st_results a where a.compid = @currcompid and a.date = @beforedate declare @year int declare @month int set @year = substring(@beforedate, 1, 4) set @month = substring(@beforedate, 5, 2) execute get_month_days @year, @month, @days output --插入月平均 insert #st_results2(compid, type, printdate, date, billid, total_cnt, total_amt, unit_price, fix_cnt, notfix_cnt, fix_amt , notfix_amt, card_total_amt, use_card_amt, take_way, female_count, male_count) select @beforecompid, 'T_T_T', substring(date, 5, 2) + '月平均', date, billid, round(isnull(total_cnt, 0)/@days, 8), round(isnull(total_amt, 0)/@days, 8), round(isnull(unit_price, 0)/@days, 8), round(isnull(fix_cnt, 0)/@days, 8), round(isnull(notfix_cnt, 0)/@days, 8), round(isnull(fix_amt, 0)/@days, 8), round(isnull(notfix_amt, 0)/@days, 8), round(isnull(card_total_amt, 0)/@days, 8), round(isnull(use_card_amt, 0)/@days, 8), round(isnull(take_way, 0)/@days, 8), round(isnull(female_count, 0)/@days, 8), round(isnull(male_count, 0)/@days, 8) from #st_results a where a.compid = @beforecompid and a.date = @beforedate insert #st_results2(compid, type, printdate, date, billid, total_cnt, total_amt, unit_price, fix_cnt, notfix_cnt, fix_amt , notfix_amt, card_total_amt, use_card_amt, take_way, female_count, male_count) select @currcompid, 'T_T_T', substring(date, 5, 2) + '月平均', date, billid, isnull(total_cnt, 0)/@days, isnull(total_amt, 0)/@days, isnull(unit_price, 0)/@days, isnull(fix_cnt, 0)/@days, isnull(notfix_cnt, 0)/@days, isnull(fix_amt, 0)/@days , isnull(notfix_amt, 0)/@days, isnull(card_total_amt, 0)/@days, isnull(use_card_amt, 0)/@days, isnull(take_way, 0)/@days, isnull(female_count, 0)/@days, isnull(male_count, 0)/@days from #st_results a where a.compid = @currcompid and a.date = @currdate --更新增长率 select @idd = scope_identity() update b set b.rate = (select rate = case when b.total_amt = 0 then 0 else (b.total_amt-isnull(a.total_amt, 0))/isnull(b.total_amt, 1) end from #st_results2 a where a.idd = (@idd-1)) from #st_results2 b where b.idd = @idd insert #st_results2(compid, type, printdate, date, billid, total_cnt, total_amt, unit_price, fix_cnt, notfix_cnt, fix_amt , notfix_amt, card_total_amt, use_card_amt, take_way, female_count, male_count) select @currcompid, 'T_T_T_T', '平均差额', date + @date, billid, (@total_cnt-isnull(total_cnt, 0))/@days, (@total_amt-isnull(total_amt, 0))/@days, (@unit_price-isnull(unit_price, 0))/@days, (@fix_cnt-isnull(fix_cnt, 0))/@days, (@notfix_cnt-isnull(notfix_cnt, 0))/@days, (@fix_amt-isnull(fix_amt, 0))/@days, (@notfix_amt-isnull(notfix_amt, 0))/@days, (@card_total_amt-isnull(card_total_amt, 0))/@days, (@use_card_amt-isnull(use_card_amt, 0))/@days, (@take_way-isnull(take_way, 0))/@days, (@female_count-isnull(female_count, 0))/@days, @male_count-isnull(male_count, 0) from #st_results a where a.compid = @currcompid and a.date = @beforedate set @count = 0 insert #st_results2(compid, type, printdate, date, billid, total_cnt, total_amt, unit_price, fix_cnt, notfix_cnt, fix_amt , notfix_amt, card_total_amt, use_card_amt, take_way, female_count, male_count) select compid, 'T', substring(date, 5, 2) + '月份总额', date, billid, round(isnull(total_cnt, 0), 8), round(isnull(total_amt, 0), 8), round(isnull(unit_price, 0), 8), round(isnull(fix_cnt, 0), 8), round(isnull(notfix_cnt, 0), 8), round(isnull(fix_amt, 0), 8), round(isnull(notfix_amt, 0), 8), round(isnull(card_total_amt, 0), 8), round(isnull(use_card_amt, 0), 8), round(isnull(take_way, 0), 8), round(isnull(female_count, 0), 8), round(isnull(male_count, 0), 8) from #st_results a where a.compid = @compidex and a.date = @date select @currcompid = compid, @currdate = date, @total_cnt = isnull(total_cnt, 0), @total_amt = isnull(total_amt, 0), @unit_price = isnull(unit_price, 0), @fix_cnt = isnull(fix_cnt, 0), @notfix_cnt = isnull(notfix_cnt, 0), @fix_amt = isnull(fix_amt, 0), @notfix_amt = isnull(notfix_amt, 0), @card_total_amt = isnull(card_total_amt, 0), @use_card_amt = isnull(use_card_amt, 0), @take_way = isnull(take_way, 0), @female_count = isnull(female_count, 0), @male_count = isnull(male_count, 0) from #st_results a where a.compid = @compidex and a.date = @date set @count = 1 end else begin set @count = 1 end end if (@count = 1) begin set @beforecompid = @compidex set @beforedate = @date end fetch insert_foreach into @compidex, @date, @type end if (@count = 2) begin if (@currcompid = @beforecompid) begin --更新增长率 select @idd = scope_identity() update b set b.rate = (select rate = case when @total_amt = 0 then 0 else (@total_amt-isnull(total_amt, 0))/isnull(@total_amt, 1) end from #st_results a where a.compid = @currcompid and a.date = @beforedate) from #st_results2 b where idd = @idd insert #st_results2(compid, type, printdate, date, billid, total_cnt, total_amt, unit_price, fix_cnt, notfix_cnt, fix_amt , notfix_amt, card_total_amt, use_card_amt, take_way, female_count, male_count) select @currcompid, 'T_T', '差额', date + @date, billid, @total_cnt-isnull(total_cnt, 0), @total_amt-isnull(total_amt, 0), @unit_price-isnull(unit_price, 0), @fix_cnt-isnull(fix_cnt, 0), @notfix_cnt-isnull(notfix_cnt, 0), @fix_amt-isnull(fix_amt, 0), @notfix_amt-isnull(notfix_amt, 0), @card_total_amt-isnull(card_total_amt, 0), @use_card_amt-isnull(use_card_amt, 0), @take_way-isnull(take_way, 0), @female_count-isnull(female_count, 0), @male_count-isnull(male_count, 0) from #st_results a where a.compid = @currcompid and a.date = @beforedate set @year = substring(@beforedate, 1, 4) set @month = substring(@beforedate, 5, 2) execute get_month_days @year, @month, @days output insert #st_results2(compid, type, printdate, date, billid, total_cnt, total_amt, unit_price, fix_cnt, notfix_cnt, fix_amt , notfix_amt, card_total_amt, use_card_amt, take_way, female_count, male_count) select @beforecompid, 'T_T_T', substring(date, 5, 2) + '月平均', date, billid, round(isnull(total_cnt, 0)/@days, 8), round(isnull(total_amt, 0)/@days, 8), round(isnull(unit_price, 0)/@days, 8), round(isnull(fix_cnt, 0)/@days, 8), round(isnull(notfix_cnt, 0)/@days, 8), round(isnull(fix_amt, 0)/@days, 8), round(isnull(notfix_amt, 0)/@days, 8), round(isnull(card_total_amt, 0)/@days, 8), round(isnull(use_card_amt, 0)/@days, 8), round(isnull(take_way, 0)/@days, 8), round(isnull(female_count, 0)/@days, 8), round(isnull(male_count, 0)/@days, 8) from #st_results a where a.compid = @beforecompid and a.date = @beforedate insert #st_results2(compid, type, printdate, date, billid, total_cnt, total_amt, unit_price, fix_cnt, notfix_cnt, fix_amt , notfix_amt, card_total_amt, use_card_amt, take_way, female_count, male_count) select @currcompid, 'T_T_T', substring(date, 5, 2) + '月平均', date, billid, round(isnull(total_cnt, 0)/@days, 8), round(isnull(total_amt, 0)/@days, 8), round(isnull(unit_price, 0)/@days, 8), round(isnull(fix_cnt, 0)/@days, 8), round(isnull(notfix_cnt, 0)/@days, 8), round(isnull(fix_amt, 0)/@days, 8), round(isnull(notfix_amt, 0)/@days, 8), round(isnull(card_total_amt, 0)/@days, 8), round(isnull(use_card_amt, 0)/@days, 8), round(isnull(take_way, 0)/@days, 8), round(isnull(female_count, 0)/@days, 8), round(isnull(male_count, 0)/@days, 8) from #st_results a where a.compid = @currcompid and a.date = @currdate --更新增长率 select @idd = scope_identity() update b set b.rate = (select rate = case when b.total_amt = 0 then 0 else (b.total_amt-isnull(a.total_amt, 0))/isnull(b.total_amt, 1) end from #st_results2 a where a.idd = (@idd-1)) from #st_results2 b where b.idd = @idd insert #st_results2(compid, type, printdate, date, billid, total_cnt, total_amt, unit_price, fix_cnt, notfix_cnt, fix_amt , notfix_amt, card_total_amt, use_card_amt, take_way, female_count, male_count) select @currcompid, 'T_T_T_T', '平均差额', date + @date, billid, (@total_cnt-isnull(total_cnt, 0))/@days, (@total_amt-isnull(total_amt, 0))/@days, (@unit_price-isnull(unit_price, 0))/@days, (@fix_cnt-isnull(fix_cnt, 0))/@days, (@notfix_cnt-isnull(notfix_cnt, 0))/@days, (@fix_amt-isnull(fix_amt, 0))/@days, (@notfix_amt-isnull(notfix_amt, 0))/@days, (@card_total_amt-isnull(card_total_amt, 0))/@days, (@use_card_amt-isnull(use_card_amt, 0))/@days, (@take_way-isnull(take_way, 0))/@days, (@female_count-isnull(female_count, 0))/@days, @male_count-isnull(male_count, 0) from #st_results a where a.compid = @currcompid and a.date = @beforedate end end close insert_foreach deallocate insert_foreach select * from #st_results2 order by idd end GO /****** Object: Table [dbo].[gdm09] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gdm09]( [gdi00c] [varchar](10) NOT NULL, [gdi01c] [varchar](20) NOT NULL, [gdi02c] [varchar](5) NOT NULL, [gdi03f] [float] NOT NULL, [gdi04t] [varchar](6) NOT NULL, [gdi05t] [varchar](6) NOT NULL, [gdi06f] [float] NULL, [gdi07i] [int] NOT NULL, [gdi08i] [int] NULL, [gdi09f] [float] NULL, CONSTRAINT [PK_GDM09] PRIMARY KEY CLUSTERED ( [gdi00c] ASC, [gdi01c] ASC, [gdi02c] ASC, [gdi03f] ASC, [gdi04t] ASC, [gdi05t] ASC, [gdi07i] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_bed_status] Script Date: 06/12/2017 06:19:30 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_bed_status] @compid varchar(10), @fromroomno varchar(20), @toroomno varchar(20), @date varchar(8), @fromtime varchar(6), @totime varchar(6), @status int --0--所有 1--空闲 2--预约 3--忙碌 as begin if (isnull(@fromroomno, '') = '') set @fromroomno = '*' --只精确到分钟 set @fromtime = substring(@fromtime, 1, 4) set @totime = substring(@totime, 1, 4) create table #result ( compid varchar(10) not null, roomno varchar(20) not null, roomname varchar(20) null, bedno varchar(20) null, status int null, --0--空闲 1--是使用 2--预约 ) create table #book --这个时间所有有预约的房间号码 ( compid varchar(10) not null, roomno varchar(20) not null, roomname varchar(20) null, bedno varchar(20) null, status int null, ) insert #book(compid, roomno, roomname, bedno, status) select gwa00c, gwa17c, gicb02v, gwa18c, 2 from gwm01, gicm02 where gwa00c = @compid and gwa01d = @date and gwa00c = gicb00c and gwa17c = gicb01v and (substring(gwa02t, 1, 4) between @fromtime and @totime or substring(gwa03t, 1, 4) between @fromtime and @totime) and gwa11i = 0 if (@status = 0) begin insert #result(compid, roomno, roomname, bedno, status) select gich00c, gich01v, gicb02v, gich02v, gich03i from gicm08, gicm02 where gicb00c = gich00c and gicb01v = gich01v and gicb00c = @compid end if (@status = 1) begin --所有空闲的房间 insert #result(compid, roomno, roomname, bedno, status) select gich00c, gich01v, gicb02v, gich02v, gich03i from gicm08, gicm02 where gicb00c = gich00c and gicb01v = gich01v and gicb00c = @compid and (gich03i = 0 or gich03i = 1 and (substring(gich04t, 1, 4)>@totime or substring(gich05t, 1, 4)<@fromtime)) --删除有预约的房间 delete a from #result a, #book b where a.compid = b.compid and a.roomno = b.roomno and a.bedno = b.bedno end if (@status = 2) begin select * from #book where roomno between @fromroomno and @toroomno or @fromroomno = '*' return end if (@status = 3) begin insert #result(compid, roomno, roomname, bedno, status) select gich00c, gich01v, gicb02v, gich02v, gich03i from gicm08, gicm02 where gicb00c = gich00c and gicb01v = gich01v and gicb00c = @compid and gich03i = 1 end select * from #result where roomno between @fromroomno and @toroomno or @fromroomno = '*' end GO /****** Object: Table [dbo].[potential_member_enter] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[potential_member_enter]( [mobile] [varchar](20) NOT NULL, [comp] [varchar](10) NULL, [entertime] [datetime] NULL ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gam19] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gam19]( [gaq00c] [varchar](10) NOT NULL, [gaq01i] [int] IDENTITY(1,1) NOT NULL, [gaq02c] [varchar](10) NULL, [gaq03c] [varchar](128) NULL, [gaq04d] [varchar](8) NULL, [gaq05t] [varchar](6) NULL, [gaq06c] [varchar](20) NOT NULL, [gaq07c] [varchar](20) NULL, [gaq08i] [int] NULL, [gaq09i] [int] NULL, [gcq10d] [varchar](8) NULL, [gcq11c] [varchar](20) NULL, [gcq12c] [varchar](20) NULL, CONSTRAINT [PK_GAM19] PRIMARY KEY CLUSTERED ( [gaq00c] ASC, [gaq06c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gdm08] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gdm08]( [gdh00c] [varchar](10) NOT NULL, [gdh01c] [varchar](20) NOT NULL, [gdh02c] [varchar](5) NOT NULL, [gdh03f] [float] NOT NULL, [gdh04d] [varchar](8) NULL, [gdh05d] [varchar](8) NULL, [gdh06i] [int] NULL, [gdh07i] [int] NULL, [gdh08f] [float] NULL, [gdh09i] [int] NULL, [gdh10f] [float] NULL, [gdh11c] [varchar](5) NULL, CONSTRAINT [PK_GDM08] PRIMARY KEY CLUSTERED ( [gdh00c] ASC, [gdh01c] ASC, [gdh02c] ASC, [gdh03f] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[hdm01] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[hdm01]( [hda00c] [varchar](10) NOT NULL, [hda01c] [varchar](10) NOT NULL, [hda02d] [varchar](8) NOT NULL, [hda03t] [varchar](6) NULL, [hda04t] [varchar](6) NULL, [hda05c] [varchar](5) NULL, [hda06d] [varchar](8) NULL, [hda07c] [varchar](5) NULL, [hda08i] [varchar](6) NULL, [hda09i] [varchar](6) NULL, [hda91c] [varchar](10) NULL, [hda92d] [varchar](8) NULL, [hda93c] [varchar](10) NULL, [hda94d] [varchar](8) NULL, [hda10t] [varchar](6) NULL, [hda12t] [varchar](6) NULL, [hda13t] [varchar](6) NULL, [hda14t] [varchar](6) NULL, [hda15t] [varchar](6) NULL, [hda16t] [varchar](6) NULL, [hda17c] [varchar](5) NULL, [hda18c] [varchar](20) NOT NULL, [hda19c] [nvarchar](200) NULL, CONSTRAINT [PK_HDM01] PRIMARY KEY CLUSTERED ( [hda00c] ASC, [hda01c] ASC, [hda02d] ASC, [hda18c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[hem01] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[hem01]( [hea00c] [varchar](10) NOT NULL, [hea01c] [varchar](20) NOT NULL, [hea02i] [tinyint] NULL, [hea03f] [float] NULL, [hea04i] [tinyint] NULL, [hea05f] [float] NULL, [hea06i] [tinyint] NULL, [hea07f] [float] NULL, [hea08i] [tinyint] NULL, [hea09f] [float] NULL, [hea10i] [tinyint] NULL, [hea11f] [float] NULL, [hea12f] [float] NULL, [hea13f] [float] NULL, [hea14i] [tinyint] NULL, [hea15f] [float] NULL, [hea16i] [tinyint] NULL, [hea17f] [float] NULL, [hea18i] [int] NOT NULL, [hea19i] [int] NULL, [hea20i] [tinyint] NULL, [hea21f] [float] NULL, [hea22i] [tinyint] NULL, [hea23f] [float] NULL, [hea24f] [float] NULL, [hea25i] [tinyint] NULL, [hea26f] [float] NULL, [hea27i] [tinyint] NULL, [hea28f] [float] NULL, [hea29i] [int] NULL, [hea30i] [int] NULL, [hea31i] [int] NULL, [hea32i] [int] NULL, [hea33c] [varchar](20) NOT NULL, [hea34c] [varchar](10) NOT NULL, [hea35i] [tinyint] NULL, [hea36f] [float] NULL, [hea37i] [tinyint] NULL, [hea38f] [float] NULL, [hea39i] [tinyint] NULL, [hea40f] [float] NULL, [hea41i] [tinyint] NULL, [hea42f] [float] NULL, [hea43i] [tinyint] NULL, [hea44f] [float] NULL, [hea45i] [tinyint] NULL, [hea46f] [float] NULL, [hea47i] [tinyint] NULL, [hea48f] [float] NULL, [hea49i] [tinyint] NULL, [hea50f] [float] NULL, [hea51i] [int] NULL, CONSTRAINT [PK_HEM01] PRIMARY KEY CLUSTERED ( [hea00c] ASC, [hea01c] ASC, [hea18i] ASC, [hea33c] ASC, [hea34c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gsm04] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gsm04]( [gsd00c] [varchar](10) NOT NULL, [gsd01c] [varchar](20) NOT NULL, [gsd02c] [varchar](5) NOT NULL, [gsd03c] [char](1) NOT NULL, [gsd04f] [float] NOT NULL, [gsd05c] [varchar](80) NULL, [gsd06f] [float] NULL, CONSTRAINT [PK_GSM04] PRIMARY KEY CLUSTERED ( [gsd00c] ASC, [gsd01c] ASC, [gsd02c] ASC, [gsd03c] ASC, [gsd04f] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[member_enter_parameter] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[member_enter_parameter]( [mep00c] [varchar](10) NOT NULL, [mep01i] [int] NULL, [mep02c] [varchar](400) NULL, [mep03i] [int] NULL, [mep04c] [varchar](400) NULL, [mep05i] [int] NULL, [mep06i] [int] NULL, [mep07i] [int] NULL, CONSTRAINT [PK_member_enter_parameter] PRIMARY KEY CLUSTERED ( [mep00c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_check_stock] Script Date: 06/12/2017 06:19:41 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_check_stock] ( @compid varchar(10), @datefrom varchar(8), @dateto varchar(8), @cidfrom varchar(20), @cidto varchar(20), @type varchar(5), @stockid varchar(4) ) as begin create table #tbl_pd ( comp varchar(10) not null, cid varchar(20) not null, cname varchar(200), pnum float, difnum float, cprice float, cmoney float, cunit varchar(5) null, offset float, ) create table #gkm02 -- 产品盘点单--盘点产品明细 ( gkb00c varchar(10) not null, --门店编号 gkb01c varchar(100) not null, --盘点单号 gkb02f float not null, --序号 gkb03c varchar(20) null, --产品代码 gkb04c varchar(5) null, --基本单位 gkb05f float null, --盘点数量 gkb06f float null, --差异数量 gkb07f float null, --现库存量 price float null, --匹配单位 primary key (gkb00c,gkb01c,gkb02f) ) --declare @PK_Sql nvarchar(max) -- -- set @PK_Sql = ' alter table #tbl_pd add constraint PK_#tbl_pd_' -- + convert(varchar(10), cast(floor(rand() * 1000000) as int)) -- + ' primary key clustered(comp, cid)' -- -- exec sp_executesql @PK_Sql insert #gkm02(gkb00c,gkb01c,gkb02f,gkb03c,gkb04c,gkb05f,gkb06f,gkb07f) select gkb00c,gkb01c,gkb02f,gkb03c,gkb04c,gkb05f,gkb06f,gkb07f from gkm02 with(nolock), gkm01 with(nolock), gfm01 with(nolock) where gka00c = gkb00c and gka01c = gkb01c and gkb00c = gfa00c and gkb03c = gfa01c and gka00c = @compid and gka02d between @datefrom and @dateto and ((@cidfrom = '*') or (gkb03c between @cidfrom and @cidto)) and ((@type = '') or (gfa05c = @type)) and ((@stockid = '') or (gka03c = @stockid)) -- 标准单位 update a set a.price = b.gfa11f from #gkm02 a, gfm01 b where a.gkb00c = b.gfa00c and a.gkb03c = b.gfa01c and a.gkb04c = b.gfa07c and isnull(price,0) = 0 -- 销售单位 update a set a.price = b.gfa11f/b.gfa09f from #gkm02 a, gfm01 b where a.gkb00c = b.gfa00c and a.gkb03c = b.gfa01c and a.gkb04c = b.gfa08c and isnull(price,0) = 0 --进货单位 update a set a.price = b.gfa21f * b.gfa11f from #gkm02 a, gfm01 b where a.gkb00c = b.gfa00c and a.gkb03c = b.gfa01c and a.gkb04c = b.gfa20c and isnull(price,0) = 0 insert into #tbl_pd(comp, cid, cname, pnum, difnum, cprice, cmoney, cunit) select gkb00c, gkb03c, gfa03c, sum(isnull(gkb05f, 0)), sum(isnull(gkb06f, 0)), max(isnull(gfa11f, 0)), isnull(sum(gkb06f*price), 0), gkb04c from #gkm02, gfm01 where gkb00c = gfa00c and gkb03c = gfa01c group by gkb03c, gkb00c, gfa03c, gkb04c select isnull(comp,'') comp, isnull(cid,'') cid, isnull(cname,'') cname, isnull((pnum ),'') pnum, isnull((difnum ),'') difnum, isnull(cprice,'') cprice, isnull((cmoney ),'') cmoney, isnull(b.gsb03c,'') cunit from #tbl_pd a join gsm02 b on a.cunit = b.gsb02c and b.gsb00c = a.comp and gsb01c = 'K' drop table #gkm02 drop table #tbl_pd end GO /****** Object: Table [dbo].[gdm11] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gdm11]( [gdk00c] [varchar](10) NOT NULL, [gdk01c] [varchar](5) NOT NULL, [gdk02c] [varchar](20) NOT NULL, [gdk03i] [int] NOT NULL, [gdk04c] [varchar](5) NOT NULL, [gdk05f] [float] NULL, CONSTRAINT [PK_GDM11] PRIMARY KEY NONCLUSTERED ( [gdk00c] ASC, [gdk01c] ASC, [gdk02c] ASC, [gdk03i] ASC, [gdk04c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[hem02] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[hem02]( [heb00c] [varchar](10) NOT NULL, [heb01c] [varchar](20) NOT NULL, [heb02c] [varchar](10) NOT NULL, [heb03i] [tinyint] NULL, [heb04f] [float] NULL, [heb05i] [tinyint] NULL, [heb06f] [float] NULL, [heb07i] [tinyint] NULL, [heb08f] [float] NULL, [heb09i] [tinyint] NULL, [heb10f] [float] NULL, [heb11i] [tinyint] NULL, [heb12f] [float] NULL, [heb13i] [tinyint] NULL, [heb14f] [float] NULL, [heb15i] [tinyint] NULL, [heb16f] [float] NULL, [heb17i] [tinyint] NULL, [heb18f] [float] NULL, [heb19i] [tinyint] NULL, [heb20f] [float] NULL, [heb21i] [tinyint] NULL, [heb22f] [float] NULL, [heb23i] [tinyint] NULL, [heb24f] [float] NULL, CONSTRAINT [PK_HEM02] PRIMARY KEY CLUSTERED ( [heb00c] ASC, [heb01c] ASC, [heb02c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gsm05] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gsm05]( [gse00c] [varchar](10) NOT NULL, [gse01c] [varchar](5) NOT NULL, [gse02c] [char](1) NOT NULL, [gse03f] [float] NOT NULL, [gse04c] [varchar](80) NULL, [gse05f] [float] NULL, CONSTRAINT [PK_GSM05] PRIMARY KEY CLUSTERED ( [gse00c] ASC, [gse01c] ASC, [gse02c] ASC, [gse03f] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[SYS_Log] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[SYS_Log]( [Id] [int] IDENTITY(1,1) NOT NULL, [StoreNumber] [nvarchar](10) NULL, [UserID] [nvarchar](10) NULL, [LogType] [nvarchar](50) NULL, [Date] [datetime] NULL, [message] [text] NULL, [position] [text] NULL, CONSTRAINT [Pk_SYS_Log_id] PRIMARY KEY CLUSTERED ( [Id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] GO /****** Object: Table [dbo].[gam21] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gam21]( [gas01c] [varchar](20) NOT NULL, [gas02c] [varchar](20) NOT NULL, [gas03c] [varchar](160) NULL, CONSTRAINT [PK_GAM21] PRIMARY KEY CLUSTERED ( [gas01c] ASC, [gas02c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gam22] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gam22]( [gat01c] [varchar](20) NOT NULL, [gat02c] [varchar](20) NOT NULL, CONSTRAINT [PK_GAM22] PRIMARY KEY CLUSTERED ( [gat01c] ASC, [gat02c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[staff_task] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[staff_task]( [id] [int] IDENTITY(1,1) NOT NULL, [compid] [varchar](10) NOT NULL, [cardid] [varchar](30) NULL, [mobile] [varchar](20) NULL, [memberid] [varchar](20) NOT NULL, [memname] [varchar](50) NULL, [birthday] [varchar](15) NULL, [notcomedays] [float] NULL, [itemcode] [varchar](30) NULL, [itemname] [varchar](50) NULL, [istreatment] [float] NULL, [date] [varchar](8) NULL, [cardamount] [float] NULL, [rechargeamount] [float] NULL, [consumeamount] [float] NULL, [remaincount] [float] NULL, [remaintreatamt] [float] NULL, [expiredate] [varchar](8) NULL, [goodsid] [varchar](30) NULL, [goodsname] [varchar](50) NULL, [goodsqty] [float] NULL, [goodsunit] [varchar](30) NULL, [personcode] [varchar](20) NULL, [personname] [varchar](30) NULL, [taskdate] [varchar](8) NULL, [taskstatus] [float] NULL, [feedbackcontent] [text] NULL, [feedbackdate] [varchar](20) NULL, [itemtype] [int] NULL, PRIMARY KEY CLUSTERED ( [id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_fic_card] Script Date: 06/12/2017 06:20:03 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_fic_card] ( @compid varchar(10), @fromdate varchar(8), @todate varchar(8), @payment varchar(10), @fromCashier varchar(20), @toCashier varchar(20), @displaytype int, @carding int ) as begin create table #tbl_unsold ( cashier varchar(20) null, --收银人员 ddate varchar(8) null, --消费日期 ttime varchar(8) null, --消费时间 cardno varchar(20) null, --会员卡号 accttype varchar(40)null, --账户类型 cardname varchar(40)null, --会员姓名 cmoney float --消费金额 ) if @displaytype = 1 begin insert #tbl_unsold(cashier, ddate, ttime, cardno, accttype, cardname, cmoney) select '', gco03d, gco04t, gco01c, gsb03c, gba03c, gco06f from gcm13 with(nolock), gsm02 with(nolock), gbm01 with(nolock) where gco00c = gsb00c and isnull(gco06f, 0)>0 and gsb01c = 'O' and gsb02c = cast(gco10i as varchar(2)) and gco00c = gba00c and gco01c = gba23c and gco00c = @compid and gco03d between @fromdate and @todate and ((@payment = '*')or (gsb02c = @payment)) and (isnull(gco13i, 0) = @carding or 2 = @carding) end else begin insert #tbl_unsold(cashier, ddate, ttime, cardno, accttype, cardname, cmoney) select gcd16c, gcd09d, substring(gcd08c, 9, 6), gcd01c, gsb03c, gba03c, gcd06f from gcm04 with(nolock), gsm02 with(nolock), gbm01 with(nolock) where gcd00c = gsb00c and gsb01c = 'O' and gsb02c = cast(gcd02i as varchar(2)) and gcd00c = gba00c and gcd01c = gba23c and gcd07c = 'gx' and gcd05i = 2 and gcd00c = @compid and ((@fromCashier = '*') or (gcd16c between @fromCashier and @toCashier)) and gcd09d between @fromdate and @todate and ((@payment = '*') or (gsb02c = @payment)) and (isnull(gcd12i, 0) = @carding or 2 = @carding) end select isnull(cashier,'') cashier, isnull(ddate,'') ddate, isnull(ttime,'') ttime, isnull(cardno,'') cardno, isnull(accttype,'') accttype, isnull(cardname,'') cardname, isnull(cmoney,0) cmoney from #tbl_unsold order by cashier drop table #tbl_unsold end GO /****** Object: Table [dbo].[gdm12] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gdm12]( [gdl00c] [varchar](10) NOT NULL, [gdl01c] [varchar](5) NOT NULL, [gdl02c] [varchar](20) NOT NULL, [gdl03i] [int] NOT NULL, [gdl04c] [varchar](20) NOT NULL, [gdl05f] [float] NULL, CONSTRAINT [PK_GDM12] PRIMARY KEY NONCLUSTERED ( [gdl00c] ASC, [gdl01c] ASC, [gdl02c] ASC, [gdl03i] ASC, [gdl04c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[insert_log] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[insert_log]( [ins00c] [varchar](10) NOT NULL, [ins01c] [varchar](50) NOT NULL, [ins02d] [datetime] NOT NULL, [ins03c] [varchar](100) NULL, [ins04c] [varchar](100) NULL, [ins05c] [varchar](100) NULL, [ins06c] [varchar](100) NULL, [ins07c] [varchar](100) NULL, [ins08c] [varchar](100) NULL, [ins09c] [varchar](100) NULL, [ins10c] [varchar](100) NULL ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[precept_log] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[precept_log]( [pre00c] [varchar](10) NOT NULL, [pre01c] [varchar](20) NOT NULL, [pre02c] [varchar](50) NULL, [pre03d] [varchar](8) NULL, [pre04d] [varchar](8) NULL, [pre05i] [int] NULL, [pre06i] [int] NULL, [pre07i] [int] NULL, [pre08f] [float] NULL, [pre09f] [float] NULL, [pre10d] [datetime] NOT NULL, [pre10c] [varchar](100) NULL, [pre11c] [varchar](100) NULL, [pre12c] [varchar](100) NULL, [pre13c] [varchar](100) NULL, [pre14c] [varchar](100) NULL, [pre15c] [varchar](100) NULL, CONSTRAINT [PK_PRECEPT_LOG] PRIMARY KEY CLUSTERED ( [pre00c] ASC, [pre01c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[hem04] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[hem04]( [hed00c] [varchar](10) NOT NULL, [hed01c] [varchar](20) NOT NULL, [hed02f] [float] NOT NULL, [hed03f] [float] NULL, [hed04f] [float] NULL, [hed05i] [tinyint] NULL, [hed06f] [float] NULL, [hed07c] [varchar](20) NULL, [hed08c] [varchar](5) NULL, CONSTRAINT [PK_HEM04] PRIMARY KEY CLUSTERED ( [hed00c] ASC, [hed01c] ASC, [hed02f] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gsm07] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gsm07]( [gsg00c] [varchar](10) NOT NULL, [gsg01c] [varchar](20) NOT NULL, [gsg02c] [varchar](10) NOT NULL, [gsg03c] [varchar](20) NOT NULL, [gsg04c] [varchar](255) NULL, CONSTRAINT [PK_GSM07] PRIMARY KEY CLUSTERED ( [gsg00c] ASC, [gsg01c] ASC, [gsg02c] ASC, [gsg03c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[delete_log] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[delete_log]( [del00c] [varchar](10) NOT NULL, [del01c] [varchar](50) NOT NULL, [del02d] [datetime] NOT NULL, [del03c] [varchar](100) NULL, [del04c] [varchar](100) NULL, [del05c] [varchar](100) NULL, [del06c] [varchar](100) NULL, [del07c] [varchar](100) NULL, [del08c] [varchar](100) NULL, [del09c] [varchar](100) NULL, [del10c] [varchar](100) NULL ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gam24] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gam24]( [gav00c] [varchar](10) NOT NULL, [gav01c] [varchar](5) NOT NULL, [gav02c] [varchar](20) NOT NULL, [gav03f] [float] NULL, [gav04f] [float] NULL, [gav05f] [float] NULL, [gav06f] [float] NULL, [gav07i] [int] NULL, [gav08f] [float] NULL, CONSTRAINT [PK_GAM24] PRIMARY KEY CLUSTERED ( [gav00c] ASC, [gav01c] ASC, [gav02c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: UserDefinedFunction [dbo].[fun_divide] Script Date: 06/12/2017 06:20:57 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create function [dbo].[fun_divide](@divisor float, @dividend float) returns float as begin declare @outs float if (@dividend = 0) begin set @outs = null end else begin set @outs = @divisor / @dividend end return @outs end GO /****** Object: Table [dbo].[gam25] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gam25]( [gay00c] [char](10) NOT NULL, [gay01c] [varchar](20) NOT NULL, [gay02c] [varchar](20) NOT NULL, [gay03f] [float] NULL, [gay04f] [float] NULL, [gay05f] [float] NULL, CONSTRAINT [PK_GAM25] PRIMARY KEY CLUSTERED ( [gay00c] ASC, [gay01c] ASC, [gay02c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: UserDefinedFunction [dbo].[fun_AsCharCount] Script Date: 06/12/2017 06:20:57 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create function [dbo].[fun_AsCharCount](@value varchar(max), @char varchar(1)) returns int as begin declare @result int set @result = 0 if isnull(@value, '') = '' return @result declare @index int set @index = 1 while @index <= len(@value) begin if substring(@value, @index, 1) = @char set @result = @result + 1 set @index = @index + 1 end return @result end GO /****** Object: Table [dbo].[precept] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[precept]( [pre00c] [varchar](10) NOT NULL, [pre01c] [varchar](20) NOT NULL, [pre02c] [varchar](50) NULL, [pre03i] [int] NULL, [pre04i] [int] NULL, [pre05i] [int] NULL, [pre06i] [int] NULL, [pre07f] [float] NULL, [pre08f] [float] NULL, [pre09i] [int] NULL, [pre10f] [float] NULL, [pre11f] [float] NULL, [pre12i] [int] NULL, [pre13i] [int] NULL, [pre14i] [int] NULL, [pre15d] [datetime] NOT NULL, [pre16c] [varchar](100) NULL, [pre17c] [varchar](100) NULL, [pre18c] [varchar](100) NULL, [pre19c] [varchar](100) NULL, [pre20c] [varchar](100) NULL, [pre21c] [varchar](100) NULL, [pre22i] [int] NULL, CONSTRAINT [PK_PRECEPT] PRIMARY KEY CLUSTERED ( [pre00c] ASC, [pre01c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[P_tablePagination] Script Date: 06/12/2017 06:19:20 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create proc [dbo].[P_tablePagination] ( @page nvarchar(20), -- 当前页 @pagesize nvarchar(20), -- 每页条数 @columns nvarchar(2000), -- 列 @rank nvarchar(50), -- 指定排序的列 @tableName nvarchar(50), -- 表名 @sqlwhere nvarchar(2000), -- 查询语句 @desc nvarchar(20), -- 排序方式 @total nvarchar(20) output ) as begin declare @count nvarchar(20) declare @joinwhere nvarchar(2000) declare @s nvarchar(max) set @s = 'select @total = count(1) from ' + @tableName + ' with(nolock) where 1 = 1 ' + @sqlwhere exec sp_executesql @s, N'@total varchar(20) output', @total output select @total declare @sql nvarchar(max) set @sql = 'select top ' + @pagesize + ' ' + @columns + ' from (select top ' + convert(nvarchar(50), convert(int, @pagesize) * convert(int, @page)) + ' ' declare @au_id char(20) set rowcount 0 set @joinwhere = '' select * into #mytemp from INFORMATIon_SCHEMA.KEY_COLUMN_USAGE where table_NAME = '' + @tableName + '' set rowcount 1 select @au_id = COLUMN_NAME from #mytemp set @count = 0 while @@rowcount <> 0 begin set rowcount 0 set @count = @count + 1 set @sql = @sql + ' ' + @au_id + ' as KeyColumn' + @count + ', ' set @joinwhere = @joinwhere + ' b.' + @au_id + ' = a.KeyColumn' + @count + ' and' delete #mytemp where COLUMN_NAME = @au_id set rowcount 1 select @au_id = COLUMN_NAME from #mytemp end set rowcount 0 drop table #mytemp if (@count > 0) begin set @joinwhere = substring(@joinwhere, 0, len(@joinwhere) - 3) end set @sql = @sql + ' Row_Number() over(order by ' + @rank + ' ' + @desc + ') as RowNumber from ' + @tableName + ' with(nolock) where 1 = 1 ' + @sqlwhere + ' ) a, ' + @tableName + ' b where 1 = 1 ' + @sqlwhere if (@count > 0) begin set @sql = @sql + ' and ' + @joinwhere end set @sql = @sql + ' and RowNumber >= ' + convert(nvarchar(50), (convert(int, @page) - 1) * Convert(int, @pagesize) + 1) + ' order by a.RowNumber asc' exec sp_executesql @sql end GO /****** Object: Table [dbo].[gdm22] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gdm22]( [gdv00c] [varchar](10) NOT NULL, [gdv01c] [varchar](20) NOT NULL, [gdv02f] [float] NOT NULL, [gdv03f] [float] NOT NULL, [gdv04i] [int] NOT NULL, [gdv05c] [varchar](20) NOT NULL, [gdv06f] [float] NULL, [gdv07c] [varchar](5) NULL, [gdv08c] [varchar](20) NULL, [gdv09c] [varchar](60) NULL, CONSTRAINT [PK_GDM22] PRIMARY KEY NONCLUSTERED ( [gdv00c] ASC, [gdv01c] ASC, [gdv02f] ASC, [gdv03f] ASC, [gdv04i] ASC, [gdv05c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_stat_card_consume] Script Date: 06/12/2017 06:20:43 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_stat_card_consume] ( @compid varchar(10), @fromdate varchar(10), @todate varchar(10), @frommemid varchar(20), @tomemid varchar(20), @only_card_pay int ) as begin create table #tbl_card_consume ( memid varchar(20) not null, amt float null, mindate varchar(10) null, maxdate varchar(10) null, times int null ) if @only_card_pay = 1 begin -- 包括所有的跨店消费 insert #tbl_card_consume(memid, amt, mindate, maxdate) select gct02c, sum(gct12f), min(isnull(gct04d, '20000101')), max(isnull(gct04d, '20000101')) from gcm20 a with (nolock) where gct04d >= @fromdate and gct04d <= @todate and ((gct02c >= @frommemid and gct03c <= @tomemid) or ('*' = @frommemid)) and (gct20c = '4' or gct20c = '9' or gct20c = '#' or (gct20c >= 'A' and gct20c < 'Z')) and (gct06c = '4' or gct06c = '3') and gct20c <> 'WX' and gct20c <> 'ZFB' group by gct02c end else begin insert #tbl_card_consume(memid, amt, mindate, maxdate) select gct02c, sum(gct12f), min(isnull(gct04d, '20000101')), max(isnull(gct04d, '20000101')) from gcm20 a with (nolock) where gct04d >= @fromdate and gct04d <= @todate and ((gct02c >= @frommemid and gct03c <= @tomemid) or ('*' = @frommemid)) and (gct06c = '4' or gct06c = '3') group by gct02c end -- 再统计来得次数 create table #tbl_consume_times ( memid varchar(20) not null, billid varchar(20) not null ) if @only_card_pay = 1 begin insert #tbl_consume_times(memid, billid) select distinct gct02c, gct14c from gcm20 a with (nolock) where gct04d >= @fromdate and gct04d <= @todate and ((gct02c >= @frommemid and gct03c <= @tomemid) or ('*' = @frommemid)) and (gct20c = '4' or gct20c = '9' or gct20c = '#' or (gct20c >= 'A' and gct20c < 'Z')) and (gct06c = '4' or gct06c = '3') and gct20c <> 'WX' and gct20c <> 'ZFB' end else begin insert #tbl_consume_times(memid, billid) select distinct gct02c, gct14c from gcm20 a with (nolock) where gct04d >= @fromdate and gct04d <= @todate and ((gct02c >= @frommemid and gct03c <= @tomemid) or ('*' = @frommemid)) and (gct06c = '4' or gct06c = '3') end create table #tbl_consume_times_result ( memid varchar(20) not null, times int null ) insert #tbl_consume_times_result(memid, times) select memid, count(*) from #tbl_consume_times group by memid update a set a.times = b.times from #tbl_card_consume a, #tbl_consume_times_result b where a.memid = b.memid drop table #tbl_consume_times drop table #tbl_consume_times_result -- 可能存在累计消费为<0的情况,则在会员统计的时候就不出现了 -- 所以需要把累计消费为<0的修改为0 update a set a.amt = 0 from #tbl_card_consume a where isnull(amt, 0) < 0 if exists(select 1 from tempdb..sysobjects where name like '%#sum_tmp%') insert #sum_tmp(vid, vsum, v_2, v_1, f_1) select memid, amt, mindate, maxdate, isnull(times, 0) from #tbl_card_consume else begin select * from #tbl_card_consume end drop table #tbl_card_consume end GO /****** Object: Table [dbo].[hem06] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[hem06]( [hef00c] [varchar](10) NOT NULL, [hef01c] [varchar](20) NOT NULL, [hef02f] [float] NOT NULL, [hef03f] [float] NULL, [hef04f] [float] NULL, [hef05i] [tinyint] NULL, [hef06f] [float] NULL, [hef08c] [varchar](5) NULL, CONSTRAINT [PK_HEM06] PRIMARY KEY CLUSTERED ( [hef00c] ASC, [hef01c] ASC, [hef02f] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[P_tablePagination_Find] Script Date: 06/12/2017 06:19:20 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create proc [dbo].[P_tablePagination_Find]( @page nvarchar(20), --当前页 @pagesize nvarchar(20), --每页条数 @columns nvarchar(2000), --列 @rank nvarchar(50), --指定排序的列 @tableName nvarchar(50), --表名 @sqlwhere nvarchar(2000), --查询语句 @desc nvarchar(20), --排序方式 @total nvarchar(20) output ) as begin declare @count nvarchar(20) declare @s nvarchar(max) set @s = 'select @total = count(1) from ( select * from (select ' + @columns + ' from ' + @tableName + ' with(nolock))t where 1 = 1 ' + @sqlwhere + ') tables ' exec sp_executesql @s, N'@total varchar(20) output', @total output select @total set @count = @pagesize*(@page-1) declare @sql nvarchar(max) set @sql = 'select top ' + @pagesize + ' * from (select Row_Number() over(order by ' + @rank + ' ' + @desc + ') as RowNumber, * from (select * from (select ' + @columns + ' from ' + @tableName + ' with(nolock)) tables where 1 = 1 ' + @sqlwhere + ') tableName )a where RowNumber>' + @count + ' ' exec sp_executesql @sql end GO /****** Object: Table [dbo].[gsm09] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gsm09]( [gsi00c] [varchar](10) NOT NULL, [gsi01c] [varchar](8) NOT NULL, [gsi02c] [varchar](5) NOT NULL, [gsi03f] [float] NULL, CONSTRAINT [PK_GSM09] PRIMARY KEY CLUSTERED ( [gsi00c] ASC, [gsi01c] ASC, [gsi02c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: UserDefinedFunction [dbo].[fun_IntToHexString] Script Date: 06/12/2017 06:20:57 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create function [dbo].[fun_IntToHexString](@value int) returns varchar(20) as begin declare @result varchar(20) set @result = '' if isnull(@value, -1) < 0 return @result if @value = 0 return '0' declare @d int while @value > 0 begin set @d = @value % 16 set @value = @value / 16 if @d > 9 set @result = char(ascii('A') + @d - 10) + @result else set @result = convert(varchar(1), @d) + @result end return @result end GO /****** Object: Table [dbo].[gam26] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gam26]( [gaz01c] [varchar](10) NOT NULL, [gaz02c] [varchar](10) NOT NULL, CONSTRAINT [PK_GAM26] PRIMARY KEY CLUSTERED ( [gaz01c] ASC, [gaz02c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: UserDefinedFunction [dbo].[fun_IntToFixLengthString] Script Date: 06/12/2017 06:20:57 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create function [dbo].[fun_IntToFixLengthString](@value int, @fix int) returns varchar(100) as begin declare @value_str varchar(100) set @value_str = convert(varchar(100), @value) if isnull(@fix, -1) < 0 return @value_str if isnull(@value, -1) < 0 return @value_str declare @result varchar(100) set @result = @value_str declare @d int set @d = len(@value_str) if @d > @fix return substring(@value_str, @d - @fix + 1, @fix) while @d < @fix begin set @result = '0' + @result set @d = @d + 1 end return @result end GO /****** Object: Table [dbo].[gdm23] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gdm23]( [gdw00c] [varchar](10) NOT NULL, [gdw01c] [varchar](20) NOT NULL, [gdw02f] [float] NOT NULL, [gdw03f] [float] NOT NULL, [gdw04i] [int] NOT NULL, [gdw05c] [varchar](20) NOT NULL, [gdw06f] [float] NULL, [gdw07c] [varchar](20) NULL, [gdw08c] [varchar](60) NULL, CONSTRAINT [PK_GDM23] PRIMARY KEY NONCLUSTERED ( [gdw00c] ASC, [gdw01c] ASC, [gdw02f] ASC, [gdw03f] ASC, [gdw04i] ASC, [gdw05c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_analyzer_customer] Script Date: 06/12/2017 06:19:26 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_analyzer_customer] ( @compid varchar(10), @class varchar(10), @frommemid varchar(20), @tomemid varchar(20), @fromdate varchar(20), @todate varchar(20), @state varchar(200), -- Member卡状态 @acon_amt float, -- 大于Consumption金额 @alastdate varchar(8), -- 某个日期之后 @at_times float, -- 大于Consumption次数 @aav_amt float, -- 大于平均Consumption金额 @aless_remain float, -- 小于某个储值余额 @abig_remain float, -- 大于某个储值余额 @addtoSMS int ) as begin create table #mem_result_3 ( idd int not null, compid varchar(10), memid varchar(20) not null, -- Member编号 m_name varchar(40) null, -- 姓名 cardid varchar(20) null, -- 卡号 gender int null, -- 性别 0:女 mobile varchar(20) null, -- 手机 tel varchar(20) null, -- 电话 state varchar(10) null, -- Member卡状态 con_amt float null, -- Consumption金额 lastdate varchar(8) null, -- 最后Consumption日期 lastcall varchar(8) null, -- 最后回访日期 t_times float null, -- 用于比较的Consumption次数 t_compare float null, -- 总共来的次数 av_ytimes float null, -- 每年来的次数 av_times float null, -- 每月来的次数 av_wtimes float null, -- 每周来的次数 months float null, -- 客人来了多少个月了,客龄 av_amt float null, -- 每次Consumption的平均金额 start_date varchar(20) null, -- 开始日期 first_date varchar(20) null, -- 第一次来店日期 comp_date varchar(20) null, -- 开始比较的日期 dur_years float null, -- 统计期间来了多少个年了 dur_months float null, -- 统计期间来了多少个月了 dur_weeks float null, -- 统计期间来了多少个周了 remain01 float null, -- 余额 remain02 float null, -- 余额 remain03 float null, -- 余额 remain04 float null, -- 余额 remain05 float null, -- 余额 total_fill_amt float null, -- 累计Charge金额 memaddr varchar(160) null, -- 会员地址 postcode varchar(20) null, -- 邮编 principle varchar(20) null, -- 负责人 total_salecard_amt float null, -- 累计卡销售金额 primary key(memid) ) insert #mem_result_3(idd, compid, memid, m_name, cardid, gender, mobile, tel, state, con_amt, lastdate, lastcall, t_times, t_compare, av_ytimes, av_times, av_wtimes, months, av_amt, start_date, first_date, comp_date, dur_years, dur_months, dur_weeks, remain01, remain02, remain03, remain04, remain05, total_fill_amt, memaddr, postcode, principle, total_salecard_amt) exec upg_analyzer_customer_ex @compid, @class, @frommemid, @tomemid, @fromdate, @todate, @fromdate, @todate, @state, @acon_amt, @alastdate, @at_times, 0, @aav_amt, @aless_remain, @abig_remain, @addtoSMS, 3, 0 select idd, memid, m_name, cardid, gender, mobile, tel, state, con_amt, lastdate, lastcall, t_times, av_times, months, av_amt, start_date, first_date, dur_months, remain01, remain02, remain03, remain04, remain05, total_fill_amt, total_salecard_amt from #mem_result_3 drop table #mem_result_3 end GO /****** Object: Table [dbo].[pay_category] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[pay_category]( [code] [varchar](5) NOT NULL, [caption] [varchar](40) NOT NULL, PRIMARY KEY CLUSTERED ( [code] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_query_ggpic_list] Script Date: 06/12/2017 06:20:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_query_ggpic_list] ( @compid varchar(10), -- 登陆公司id @topshow int, -- 表示前几条数据 @show int -- 查询首页大图,0查询所有不包含topshow,1只查显示的, ) as begin create table #tbl_data_tem ( id int identity not null, compid varchar(20) not null, wpid varchar(20) not null, -- 产品、项目、员工、会员卡编号 pname varchar(60) null, -- 名称 punit varchar(500) null, -- 销售单位 pprice float null, -- 外购价格 pclass varchar(20) null, -- 统计分类 picid bigint null, -- 对应图片id pstatus varchar(10) null, -- 图片状态,0,首页大图不显示,1,首页大图显示,2,小图首页显示,3,小图普通 pstype varchar(10) null, -- 图片类型:0,产品;1,项目;2,会员卡;3,员工;4,门店介绍;5,抵用券; primary key(id) ) --防止项目与产品等的编号有重复,而导致错误的更新图片编号, create table #tbl_data_tem1( compid varchar(20) not null, wpid varchar(20) not null, -- 产品、项目、员工、会员卡编号 pname varchar(60) null, --名称 punit varchar(500) null, --销售单位 pprice float null, --外购价格 pclass varchar(20) null, --统计分类 picid bigint null, -- 对应图片id pstatus varchar(10) null, pstype varchar(10) null, primary key(compid,wpid) ) -- 产品 -- 查出所有该门店的 insert #tbl_data_tem1(compid,wpid,pname,punit,pprice,pclass,pstype) select f.gfa00c as compid,f.gfa01c as wpid,f.gfa03c as pname,f.gfa07c as punit,f.gfa71f as pprice,f.gfa24c as pclass,'0' as pstype from gfm01 f with (nolock) where f.gfa00c=@compid and f.gfa70i=1--是否允许外购 and isnull(f.gfa10c,'0')='0' and (isnull(f.gfa47c,'0')='0' or f.gfa47c='')--是否停用 --更新本门店的图片id update a set a.picid=(select top 1 isnull(p.gff04c,0) from gfm06p p with (nolock) where wpid=p.gff01c and compid=p.gff00c and datalength(p.gff02p)>20 and p.gff00c=@compid and (p.gff03c=1 or p.gff03c=0) order by p.gff04c desc) from #tbl_data_tem1 a --没有图片的,从001门店更新 update a set a.picid=(select top 1 isnull(p.gff04c,0) from gfm06p p with (nolock) where wpid=p.gff01c and datalength(p.gff02p)>20 and p.gff00c='001' and (p.gff03c=1 or p.gff03c=0) order by p.gff04c desc) from #tbl_data_tem1 a where isnull(picid,0)=0 --删除没有图片的 delete from #tbl_data_tem1 where isnull(picid,0)=0 --更新图片状态 update a set a.pstatus=(select p.gff03c from gfm06p p with (nolock) where p.gff04c=picid) from #tbl_data_tem1 a insert #tbl_data_tem select * from #tbl_data_tem1 delete from #tbl_data_tem1 -- 项目 -- 查出所有该门店的项目 insert #tbl_data_tem1(compid,wpid,pname,punit,pprice,pclass,pstype) select d.gda00c as compid,d.gda01c as wpid,d.gda03c as pname,d.gda09c as punit,d.gda10f as pprice,d.gda13c as pclass,'1' as pstype from gdm01 d with (nolock) where d.gda00c=@compid --更新本门店的图片id update a set a.picid=(select top 1 isnull(p.gdc04c,0) from gdm03p p with (nolock) where wpid=p.gdc01c and compid=p.gdc00c and datalength(p.gdc02p)>20 and p.gdc00c=@compid and (p.gdc03c=1 or p.gdc03c=0) order by p.gdc04c desc) from #tbl_data_tem1 a --没有图片的,从001门店更新 update a set a.picid=(select top 1 isnull(p.gdc04c,0) from gdm03p p with (nolock) where wpid=p.gdc01c and datalength(p.gdc02p)>20 and p.gdc00c='001' and (p.gdc03c=1 or p.gdc03c=0) order by p.gdc04c desc) from #tbl_data_tem1 a where isnull(picid,0)=0 --删除没有图片的 delete from #tbl_data_tem1 where isnull(picid,0)=0 --更新图片状态 update a set a.pstatus=(select p.gdc03c from gdm03p p with (nolock) where p.gdc04c=picid) from #tbl_data_tem1 a insert #tbl_data_tem select * from #tbl_data_tem1 delete from #tbl_data_tem1 -- 抵用券 -- 查出所有该门店的 insert #tbl_data_tem1(compid,wpid,pname,punit,pprice,pclass,pstype) select d.gah00c as compid,d.gah02c as wpid,'' as pname,'' as punit,d.gah12f as pprice,d.gah01c as pclass,'5' as pstype from gam08 d with (nolock) where d.gah00c=@compid --更新本门店的图片id update a set a.picid=(select top 1 isnull(p.gah04c,0) from gam08p p with (nolock) where wpid=p.gah02c and compid=p.gah00c and datalength(p.gah02p)>20 and p.gah00c=@compid and (p.gah03c=1 or p.gah03c=0) order by p.gah04c desc) from #tbl_data_tem1 a --没有图片的,从001门店更新 update a set a.picid=(select top 1 isnull(p.gah04c,0) from gam08p p with (nolock) where wpid=p.gah02c and datalength(p.gah02p)>20 and p.gah00c='001' and (p.gah03c=1 or p.gah03c=0) order by p.gah04c desc) from #tbl_data_tem1 a where isnull(picid,0)=0 --删除没有图片的 delete from #tbl_data_tem1 where isnull(picid,0)=0 --更新图片状态 update a set a.pstatus=(select p.gah03c from gam08p p with (nolock) where p.gah04c=picid) from #tbl_data_tem1 a insert #tbl_data_tem select * from #tbl_data_tem1 if(@show=1) begin Exec ('select top ' + @topshow + ' compid,wpid,pname,punit,pprice,pclass,picid,pstatus,pstype from #tbl_data_tem where pstatus=1') end else begin select compid,wpid,pname,punit,pprice,pclass,picid,pstatus,pstype from #tbl_data_tem end drop table #tbl_data_tem1 drop table #tbl_data_tem end GO /****** Object: UserDefinedFunction [dbo].[f_splitstr] Script Date: 06/12/2017 06:20:56 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create function [dbo].[f_splitstr](@SourceSql varchar(8000),@StrSeprate varchar(100)) returns @temp table(F1 varchar(100)) as begin declare @ch as varchar(100) set @SourceSql=@SourceSql+@StrSeprate while(@SourceSql<>'') begin set @ch=left(@SourceSql,charindex(',',@SourceSql,1)-1) insert @temp values(@ch) set @SourceSql=stuff(@SourceSql,1,charindex(',',@SourceSql,1),'') end return end GO /****** Object: StoredProcedure [dbo].[upg_point_details] Script Date: 06/12/2017 06:20:27 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_point_details] ( @compid varchar(10), -- 公司编号 @cardcls varchar(5), -- 卡类别 @cardfrom varchar(20), -- 开始卡号 @cardto varchar(20), -- 结束卡号 @datefrom varchar(8), -- 开始日期 @dateto varchar(8), -- 结束日期 @type int -- 按单据统计-0, 按卡统计-1 ) as begin if isnull(@cardfrom, '') = '' set @cardfrom = '*' if isnull(@datefrom, '') = '' set @datefrom = '*' if isnull(@cardcls, '') = '' set @cardcls = '*' create table #gcm04 ( _gcd00c varchar(10) not null, _gcd01c varchar(20) not null, -- 卡号 _gcd05i int null, -- 异动类别 _gcd06f float null, -- 异动金额 _gcd07c varchar(5) null, -- 异动单据类别 _gcd08c varchar(20) null, -- 异动单号 _gcd09d varchar(8) null -- 异动日期 ) create table #point_stat ( id int identity not null, billdate varchar(8) null, -- 异动日期 billcompid varchar(10) null, -- 消费公司 billtype varchar(10) null, -- 单据类型 billid varchar(20) null, -- 异动单号 cardid varchar(30) not null, -- 卡号 men varchar(40) null, -- 客户姓名 cardcompid varchar(10) null, -- 归属公司 cardcls varchar(40) null, -- 卡类别 在会员统计时使用 beforeamt float null, -- 前期余额 在根据会员卡统计的时候使用 inamt float null, -- 存入金额 outamt float null, -- 取出金额 amt float null -- 最终余额 会员卡统计时用 ) insert #gcm04(_gcd00c, _gcd01c, _gcd05i, _gcd06f, _gcd07c, _gcd08c, _gcd09d) select gcd00c, gcd01c, gcd05i, gcd06f, gcd07c, gcd08c, gcd09d from gcm04 with (nolock), gcm01 with (nolock), gam26 z with (nolock) where gcd02i = 3 and gca00c = gca13d and gca01c = gcd01c and gca13d = gaz02c and gcd06f <> 0 and gaz01c = @compid and (gca02c = @cardcls or @cardcls = '*') and ((gcd09d >= @datefrom and gcd09d <= @dateto) or @datefrom = '*') and ((gca01c >= @cardfrom and gca01c <= @cardto) or @cardfrom = '*') if @type = 0 -- 根据单据统计 begin -- 存入 insert #point_stat(billdate, billcompid, billtype, billid, cardid, men, inamt, cardcompid) select _gcd09d, _gcd00c, _gcd07c, _gcd08c, gca01c, gba03c, _gcd06f, gca00c from gcm01 with (nolock), gbm01 with (nolock), #gcm04 where gca00c = gba00c and gca00c = gca13d and gca01c = _gcd01c and gca04c = gba01c and (_gcd05i = 0 or _gcd05i = 3) -- 取出 insert #point_stat(billdate, billcompid, billid, billtype, cardid, men, outamt, cardcompid) select _gcd09d, _gcd00c, _gcd08c, _gcd07c, gca01c, gba03c, _gcd06f, gca00c from gcm01 with (nolock), gbm01 with (nolock), #gcm04 where gca00c = gba00c and gca00c = gca13d and gca01c = _gcd01c and gca04c = gba01c and (_gcd05i = 1 or _gcd05i = 2 or _gcd05i = 5) update #point_stat set outamt = 0 where outamt is null update #point_stat set inamt = 0 where inamt is null end else if @type = 1 --根据卡统计 begin -- 范围内的卡 姓名 类别 积分余额 insert #point_stat(cardid, men, cardcompid, cardcls, amt) select gca01c, gba03c, gca00c, gca02c, gcc06f from gcm01 with (nolock), gbm01 with (nolock), gcm03 with (nolock) where gcc03i = 3 and gca00c = gba00c and gca00c = gcc00c and gca00c = gca13d and gca04c = gba01c and gca01c = gcc01c and gca01c in (select _gcd01c from #gcm04) update #point_stat set cardcls = gak02c from #point_stat, gam10 with (nolock) where gak00c = cardcompid and gak01c = cardcls create table #inpoint ( gcd01c_i varchar(20) not null, gcd06f_i float null ) create table #outpoint ( gcd01c_o varchar(20) not null, gcd06f_o float null ) -- 每张卡的 存入总额 insert #inpoint(gcd01c_i, gcd06f_i) select _gcd01c, sum(_gcd06f) from #gcm04 where (_gcd05i = 0 or _gcd05i = 3 ) group by _gcd01c update #point_stat set inamt = gcd06f_i from #inpoint, #point_stat where cardid = gcd01c_i -- 每张卡的 取出总额 insert #outpoint(gcd01c_o, gcd06f_o) select _gcd01c, sum(_gcd06f) from #gcm04 where (_gcd05i = 1 or _gcd05i = 2 or _gcd05i = 5) group by _gcd01c update #point_stat set outamt = gcd06f_o from #outpoint, #point_stat where cardid = gcd01c_o update b -- 把前期的存入总额 更新到 #point_stat set b.beforeamt = isnull(a.gcd06f_i, 0) from (select gcd01c, sum(gcd06f) as gcd06f_i from gcm04 with(nolock), #point_stat where gcd02i = 3 and gcd01c = cardid and (gcd05i = 0 or gcd05i = 3 ) and gcd09d < @datefrom group by gcd01c) as a, #point_stat as b where gcd01c = cardid update b -- 用前期的存入总额- 前期的取出总额 更新到 #point_stat set b.beforeamt = isnull(b.beforeamt, 0) - isnull(a.gcd06f_o, 0) from (select gcd01c, sum(gcd06f) as gcd06f_o from gcm04 with(nolock), #point_stat where gcd02i = 3 and gcd01c = cardid and (gcd05i = 1 or gcd05i = 2 or gcd05i = 5) and gcd09d < @datefrom group by gcd01c) as a, #point_stat as b where gcd01c = cardid update #point_stat set beforeamt = 0 where beforeamt is null update #point_stat set outamt = 0 where outamt is null update #point_stat set inamt = 0 where inamt is null drop table #outpoint drop table #inpoint end select left(billdate, 4) + '-' + substring(billdate, 5, 2) + '-' + right(billdate, 2) as billdate, '(' + billcompid + ')' + billid as billid, billtype, cardid + '(' + isnull(men, '') + ')' as card, cardcls, beforeamt, inamt, outamt, amt from #point_stat order by billdate, billcompid, billtype, billid, card drop table #point_stat drop table #gcm04 end GO /****** Object: Table [dbo].[hem07] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[hem07]( [heg00c] [varchar](10) NOT NULL, [heg01c] [varchar](20) NOT NULL, [heg02c] [varchar](5) NOT NULL, [heg03i] [float] NULL, [heg04f] [float] NULL, [heg05i] [float] NULL, [heg06f] [float] NULL, [heg07i] [float] NULL, [heg08f] [float] NULL, [heg09i] [float] NULL, [heg10f] [float] NULL, [heg11f] [float] NULL, CONSTRAINT [PK_HEM07] PRIMARY KEY CLUSTERED ( [heg00c] ASC, [heg01c] ASC, [heg02c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gem01] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gem01]( [gea00c] [char](10) NOT NULL, [gea01c] [varchar](20) NOT NULL, [gea02i] [int] NOT NULL, [gea03d] [varchar](8) NULL, [gea04c] [varchar](20) NULL, [gea05i] [int] NULL, [gea06i] [int] NULL, [gea07c] [varchar](20) NULL, [gea08c] [varchar](20) NULL, [gea09d] [varchar](8) NULL, [gea10c] [varchar](20) NULL, [gea11d] [varchar](8) NULL, [gea12f] [float] NULL, [gea13c] [varchar](20) NULL, [gea14i] [int] NULL, [gea15d] [varchar](8) NULL, [gea16d] [varchar](8) NULL, [gea17c] [varchar](20) NULL, [gea18f] [float] NULL, [gea19c] [varchar](5) NULL, [gea20f] [float] NULL, [gea21c] [varchar](20) NULL, [gea21cinid] [varchar](20) NULL, [gea22t] [varchar](6) NULL, [gea22i] [int] NULL, [gea22c] [varchar](5) NULL, [gea23f] [float] NULL, [gea24c] [varchar](5) NULL, [gea26c] [varchar](5) NULL, [gea23c] [varchar](120) NULL, [gea24f] [float] NULL, [gea25f] [float] NULL, [gea26f] [float] NULL, [gea27f] [float] NULL, [gea28c] [nvarchar](20) NULL, [gea28cinid] [varchar](20) NULL, [gea29f] [float] NULL, [gea30c] [varchar](20) NULL, [gea30cinid] [varchar](20) NULL, [gea31f] [float] NULL, [gea32c] [varchar](20) NULL, [gea32cinid] [varchar](20) NULL, [gea33f] [float] NULL, [gea34c] [varchar](20) NULL, [gea34cinid] [varchar](20) NULL, [gea35f] [float] NULL, [gea36c] [varchar](20) NULL, [gea36cinid] [varchar](20) NULL, [gea37f] [float] NULL, [gea38c] [varchar](20) NULL, [gea38cinid] [varchar](20) NULL, [gea39f] [float] NULL, [gea40c] [varchar](20) NULL, [gea40cinid] [varchar](20) NULL, [gea41f] [float] NULL, [gea42c] [varchar](20) NULL, [gea42cinid] [varchar](20) NULL, [gea43f] [float] NULL, [gea44c] [varchar](20) NULL, [gea44cinid] [varchar](20) NULL, [gea45f] [float] NULL, [gea46i] [int] NULL, [gea47f] [float] NULL, [gea48f] [float] NULL, [gea91c] [nvarchar](20) NULL, [gea92d] [varchar](8) NULL, [gea93c] [varchar](10) NULL, [gea94d] [varchar](8) NULL, [gea95c] [varchar](10) NULL, [gea96d] [varchar](8) NULL, [gea80d] [varchar](8) NULL, [gea81c] [varchar](10) NULL, [gea98i] [int] NULL, [gea97i] [int] NULL, [gea99i] [int] NULL, [gea98c] [varchar](5) NULL, [gea04c1] [varchar](20) NULL, [gea82c] [varchar](10) NULL, [gea83c] [varchar](10) NULL, [gea84c] [varchar](10) NULL, [gea85c] [varchar](10) NULL, [gea86c] [varchar](10) NULL, [gea87c] [varchar](10) NULL, [gea88c] [varchar](10) NULL, [gea89c] [varchar](10) NULL, [gea90c] [varchar](10) NULL, CONSTRAINT [PK_GEM01] PRIMARY KEY CLUSTERED ( [gea00c] ASC, [gea01c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gbm23] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gbm23]( [gbw00c] [varchar](10) NOT NULL, [gbw01c] [varchar](60) NOT NULL, [gbw02d] [varchar](10) NOT NULL, [gbw03c] [varchar](10) NULL, [gbw04f] [float] NULL, [gbw05f] [float] NULL, [gbw90d] [varchar](10) NULL, [gbw91c] [varchar](30) NULL, CONSTRAINT [PK_GBM23] PRIMARY KEY NONCLUSTERED ( [gbw00c] ASC, [gbw01c] ASC, [gbw02d] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[lrm01] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[lrm01]( [gra00c] [varchar](10) NOT NULL, [gra01c] [varchar](20) NOT NULL, [gra02d] [varchar](8) NULL, [gra03c] [varchar](20) NULL, [gra04c] [varchar](10) NULL, [gra05i] [int] NULL, [gra06t] [varchar](8) NULL, [gra07f] [float] NULL, [gra08d] [varchar](8) NULL, [gra09c] [varchar](20) NULL, [gra10d] [varchar](20) NULL, [gra11t] [varchar](6) NULL, [gra12c] [varchar](20) NULL, [gra13c] [varchar](20) NULL, [gra14f] [float] NULL, [gra15f] [float] NULL, [gra16c] [varchar](30) NULL, [gra17c] [varchar](300) NULL, [gra18i] [int] NULL, [gra19c] [varchar](20) NULL, [gra20c] [varchar](20) NULL, [gra21f] [float] NULL, [gra22f] [float] NULL, [gra23i] [int] NULL, [gra91c] [varchar](10) NULL, [gra92d] [varchar](8) NULL, [gra93c] [varchar](10) NULL, [gra94d] [varchar](8) NULL, [gra95c] [varchar](10) NULL, [gra96d] [varchar](8) NULL, [gra97t] [varchar](8) NULL, CONSTRAINT [PK_LRM01] PRIMARY KEY CLUSTERED ( [gra00c] ASC, [gra01c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[hem09] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[hem09]( [hej00c] [varchar](10) NOT NULL, [hej01c] [varchar](20) NOT NULL, [hej02c] [varchar](20) NOT NULL, [hej03d] [varchar](8) NULL, [hej04c] [varchar](20) NOT NULL, [hej05i] [int] NOT NULL, [hej06c] [varchar](20) NOT NULL, [hej07f] [float] NULL, [hej08f] [float] NULL, [hej09f] [float] NULL, [hej10i] [int] IDENTITY(1,1) NOT NULL, [hej11i] [int] NULL, CONSTRAINT [PK_HEM09] PRIMARY KEY CLUSTERED ( [hej00c] ASC, [hej10i] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_auto_key] Script Date: 06/12/2017 06:19:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_auto_key]( @compid varchar(10), --公司 @date varchar(8), --日期 @tablename varchar(20), --表名 @compfiled varchar(20), --公司字段 @keyfiled varchar(20), --单号字段 @fmtstr varchar(20), --默认单号格式 @retkey varchar(20)output) --返回 as begin declare @keystr varchar(20) declare @keynum varchar(20) declare @defhead varchar(20) declare @fmtnumlen int select @defhead = '' --公司 if substring(@fmtstr, 1, 1) = 'C' begin select @defhead = @defhead + @compid select @fmtstr = substring(@fmtstr, 2, len(@fmtstr)-1) end --年 if substring(@fmtstr, 1, 2) = 'YY' begin select @defhead = @defhead + substring(@date, 3, 2) select @fmtstr = substring(@fmtstr, 3, len(@fmtstr)-2) end --月 if substring(@fmtstr, 1, 2) = 'MM' begin select @defhead = @defhead + substring(@date, 5, 2) select @fmtstr = substring(@fmtstr, 3, len(@fmtstr)-2) end --日 if substring(@fmtstr, 1, 2) = 'DD' begin select @defhead = @defhead + substring(@date, 7, 4) select @fmtstr = substring(@fmtstr, 3, len(@fmtstr)-2) end --剩下的就是序号长度 select @fmtnumlen = len(@fmtstr) if @fmtnumlen = 0 begin set @fmtnumlen = 1 end declare @sql varchar(8000) select @sql = ' select max (' + @keyfiled + ')' + ' from ' + @tablename + ' where ' + @compfiled + ' = ''' + @compid + '''' + ' and ' + @keyfiled + ' like ''' + @defhead + '%''' + ' and len(' + @keyfiled + ') >= ' + ' (select max(len( ' + @keyfiled + ' )) ' + ' from ' + @tablename + '' + ' where ' + @compfiled + ' = ''' + @compid + '''' + ' and ' + @keyfiled + ' like ''' + @defhead + '%'')' declare @currmaxid varchar(20) create table #id (keyid varchar(20) null) insert #id exec (@sql) select @currmaxid = keyid from #id drop table #id --计算单号序号 declare @maxseq varchar(20) declare @tmp varchar(20) declare @len int declare @i int declare @one varchar(1) select @tmp = '' select @i = 1 select @maxseq = substring(@currmaxid, len(@defhead), len(@currmaxid) + 1) select @len = len(@maxseq) while(@i <= @len) begin select @one = substring(@maxseq, @i, 1) if @one < '0' begin select @tmp = @tmp + '0' end else if @one > '9' begin select @tmp = @tmp + '9' select @tmp = (@tmp + 1) end else begin select @tmp = @tmp + @one end select @i = @i + 1 end if @fmtnumlen < len((@tmp + 1)) begin set @fmtnumlen = len((@tmp + 1)) end select @keynum = right('00000000000000000000' + convert(varchar(20), (@tmp + 1)), @fmtnumlen) ---end 计算序号--- if isnull(@currmaxid, '') = '' begin select @keystr = @defhead + right('00000000000000000001', @fmtnumlen) end else begin select @keystr = @defhead + right('00000000000000000000' + @keynum, len(@currmaxid)) end select @retkey = @keystr end GO /****** Object: Table [dbo].[gsm12] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gsm12]( [gsl01i] [int] IDENTITY(1,1) NOT NULL, [gsl02c] [varchar](10) NOT NULL, [gsl03c] [varchar](20) NULL, [gsl04c] [varchar](10) NULL, CONSTRAINT [PK_GSM12] PRIMARY KEY CLUSTERED ( [gsl01i] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_cal_next_day] Script Date: 06/12/2017 06:19:33 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_cal_next_day] ( @fromdate varchar(20), @unit varchar(10), @period float, @todate varchar(20) output ) as begin declare @days int declare @date_todate datetime if @unit = '1' select @date_todate = dateadd(year, @period, @fromdate) else if @unit = '2' select @date_todate = dateadd(month, @period, @fromdate) else if @unit = '3' select @date_todate = dateadd(day, @period, @fromdate) else if @unit = '4' select @date_todate = dateadd(month, 3 * @period, @fromdate) select @todate = substring(convert(varchar(20), @date_todate, 102), 1, 4) + substring(convert(varchar(20), @date_todate, 102), 6, 2) + substring(convert(varchar(20), @date_todate, 102), 9, 2) end GO /****** Object: Table [dbo].[productprecept] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[productprecept]( [pro00c] [varchar](10) NOT NULL, [pro01c] [varchar](20) NOT NULL, [pro02c] [varchar](20) NOT NULL, [pro03i] [int] NULL, [pro04f] [float] NULL, [pro05i] [int] NULL, [pro06f] [float] NULL, [pro07f] [float] NULL, [pro08c] [varchar](100) NULL, [pro09c] [varchar](100) NULL, [pro10c] [varchar](100) NOT NULL, [pro11c] [varchar](100) NULL, [pro12c] [varchar](100) NULL, [pro13i] [int] NULL, CONSTRAINT [PK_PRODUCTPRECEPT] PRIMARY KEY CLUSTERED ( [pro00c] ASC, [pro01c] ASC, [pro02c] ASC, [pro10c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gem02] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gem02]( [geb00c] [varchar](10) NOT NULL, [geb01c] [varchar](20) NOT NULL, [geb02f] [float] NOT NULL, [geb03c] [varchar](5) NULL, [geb04c] [varchar](5) NULL, [geb05f] [float] NULL, [geb06f] [float] NULL, [geb07f] [float] NULL, [geb08c] [varchar](30) NULL, [geb09i] [int] NULL, [geb10f] [float] NULL, CONSTRAINT [PK_GEM02] PRIMARY KEY CLUSTERED ( [geb00c] ASC, [geb01c] ASC, [geb02f] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gbm01] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gbm01]( [gba00c] [varchar](10) NOT NULL, [gba01c] [varchar](20) NOT NULL, [gba02c] [varchar](30) NULL, [gba03c] [varchar](200) NULL, [gba04c] [varchar](1) NULL, [gba05c] [varchar](160) NULL, [gba06c] [varchar](50) NULL, [gba07c] [varchar](20) NULL, [gba08c] [varchar](20) NULL, [gba09c] [varchar](20) NULL, [gba10c] [varchar](20) NULL, [gba11c] [varchar](100) NULL, [gba12c] [varchar](6) NULL, [gba13c] [varchar](20) NULL, [gba14i] [int] NULL, [gba15c] [varchar](5) NULL, [gba16c] [varchar](20) NULL, [gba17d] [varchar](8) NULL, [gba18c] [varchar](5) NULL, [gba19c] [varchar](5) NULL, [gba20c] [varchar](5) NULL, [gba21c] [varchar](10) NULL, [gba22c] [varchar](5) NULL, [gba23c] [varchar](20) NULL, [gba24c] [varchar](10) NULL, [gba25c] [varchar](30) NULL, [gba26c] [varchar](30) NULL, [gba27c] [varchar](30) NULL, [gba28i] [int] NULL, [gba29c] [varchar](30) NULL, [gba30c] [varchar](30) NULL, [gba31c] [varchar](30) NULL, [gba32c] [varchar](20) NULL, [gba33c] [varchar](60) NULL, [gba34c] [varchar](10) NULL, [gba35c] [varchar](10) NULL, [gba36c] [varchar](20) NULL, [gba37c] [varchar](20) NULL, [gba38c] [varchar](20) NULL, [gba39i] [int] NULL, [gba40c] [varchar](20) NULL, [gba41d] [datetime] NULL, [gba42c] [varchar](50) NULL, [gba43c] [varchar](1) NULL, [gba90c] [varchar](20) NULL, [gba92c] [varchar](100) NULL, [gba98c] [varchar](100) NULL, [gba99i] [int] NULL, CONSTRAINT [PK_GBM01] PRIMARY KEY CLUSTERED ( [gba00c] ASC, [gba01c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_get_overtime] Script Date: 06/12/2017 06:20:08 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_get_overtime] ( @beginTime varchar(20), -- 开始时间 @endTime varchar(20) -- 截止时间 ) as--WUXM 2004/12/30 begin declare @begin datetime declare @end datetime if isnull(@beginTime, '') = '' return 0 select @begin = substring(@beginTime, 1, 4) + '/' + substring(@beginTime, 5, 2) + '/' + substring(@beginTime, 7, 2) + ' ' + substring(@beginTime, 9, 2) + ':' + substring(@beginTime, 11, 2) + ':' + substring(@beginTime, 13, 2) if isnull(@endTime, '') = '' select @end = getdate() else begin select @end = substring(@endTime, 1, 4) + '/' + substring(@endTime, 5, 2) + '/' + substring(@endTime, 7, 2) + ' ' + substring(@endTime, 9, 2) + ':' + substring(@endTime, 11, 2) + ':' + substring(@endTime, 13, 2) end select datediff(n, @begin, @end) end GO /****** Object: Table [dbo].[keydefinedic] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[keydefinedic]( [program] [varchar](10) NOT NULL, [keyType] [varchar](10) NOT NULL, [keyName] [varchar](20) NOT NULL, [keyCaption] [varchar](50) NULL, CONSTRAINT [Pk_keydefinedic_id] PRIMARY KEY CLUSTERED ( [program] ASC, [keyType] ASC, [keyName] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_get_account_total_remain] Script Date: 06/12/2017 06:20:04 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_get_account_total_remain] ( @compid varchar(10), @accttype varchar(20), -- 账户代码 @cardtype varchar(20), -- 卡类别 @fromcardid varchar(20), @tocardid varchar(20) ) as begin if @accttype = '*' select @accttype = '0' create table #tbl_gcm03_buf ( cardid varchar(20) not null, remain float null, cardtype varchar(20) not null, accttype int not null, primary key(cardtype, accttype, cardid) ) insert #tbl_gcm03_buf(cardid, remain, cardtype, accttype) select cardid = gca01c, remain = gcc06f, cardtype = gca02c, accttype = gcc03i from gcm01 a with (nolock), gcm03 b with (nolock), gam26 z where a.gca00c = b.gcc00c and a.gca01c = b.gcc01c and a.gca00c = a.gca13d and ((@accttype = '0') or (b.gcc03i = convert(int, @accttype))) and (@fromcardid = '*' or (gca01c >= @fromcardid and gca01c <= @tocardid)) and (@cardtype = '*' or (gca02c = @cardtype)) and a.gca00c = z.gaz02c and z.gaz01c = @compid and (gca08i = 4 or gca08i = 5 or gca08i = 10) order by gca01c delete #tbl_gcm03_buf where cardid like '散客%' select isnull(cardtype,'') cardtype, isnull(accttype,'') accttype, remain = isnull(sum(remain),0) from #tbl_gcm03_buf group by cardtype, accttype order by cardtype, accttype drop table #tbl_gcm03_buf end GO /****** Object: Table [dbo].[gsm14] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gsm14]( [gsn01c] [varchar](5) NOT NULL, [gsn02c] [varchar](5) NULL, CONSTRAINT [PK_GSM14] PRIMARY KEY CLUSTERED ( [gsn01c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_account_analysis] Script Date: 06/12/2017 06:19:22 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_account_analysis] ( @compid varchar(10), --公司编号 @depid varchar(10), --部门编号 @accttype varchar(5), --帐户类别 @cardtype varchar(5), --卡类别 @cardfrom varchar(20), --开始卡号 @cardto varchar(20), --结束卡号 @datefrom varchar(8), --开始日期 @dateto varchar(8), --结束日期 @salesman varchar(20), --业务负责人, 已经不用 @printdetail int --是否打印统计明细:0-不打印,1-打印 -- 不用 ) as begin if isnull(@cardfrom, '') = '' set @cardfrom = '*' if isnull(@depid, '') = '' set @depid = '*' if isnull(@datefrom, '') = '' set @datefrom = '*' if isnull(@salesman, '') = '' set @salesman = '*' if isnull(@cardtype, '') = '' set @cardtype = '*' if isnull(@accttype, '') = '*' set @accttype = '0' create table #detail ( depid varchar(10) null, --部门编号 depname varchar(40) null, --部门名称 cardid varchar(20) null, --卡号 memname varchar(40) null, --会员名字 account_type int null, --帐户类别 account_name varchar(40) null, --帐户名称 chg_type int null, --异动类别(方向) chg_name varchar(40) null, --异动名称 amt float null, --异动金额 chg_date varchar(8) null, --异动日期 billtype varchar(10) null, --单据类别 billid varchar(20) null, --单据号 chg_src int null, --异动来源:0-卡销售,1-卡充值,2-收银,3-其它 chg_srcname varchar(10) null, detail_amt float null, --详细异动金额 prjid varchar(20) null, --项目编号 prjname varchar(40) null, --项目名称 compid varchar(10) null, --单据归属公司编号 memo varchar(400) null, --备注信息 ) create table #temp_gcm04 ( _gcd00c char(10) not null, --会馆编号 _gcd01c varchar(20) not null, --卡号 _gcd02i int not null, --帐号类别(1-电子钱包, 2-储值帐号) _gcd03c varchar(20) null, --帐号(暂不用) _gcd04f float not null, --序号 _gcd05i int null, --异动类别(0- 充值, 1-取款 2-消费, 3-转入, 4-转出) _gcd06f float null, --异动金额 _gcd07c varchar(5) null, --异动单据类别 _gcd08c varchar(20) null, --异动单号 _gcd09d varchar(8) null, --异动日期 primary key clustered(_gcd00c, _gcd01c, _gcd02i, _gcd04f) ) --统计本公司的所有会员卡 --把gcm04导入buffer表中 insert into #temp_gcm04(_gcd00c, _gcd01c, _gcd02i, _gcd03c, _gcd04f, _gcd05i, _gcd06f, _gcd07c, _gcd08c, _gcd09d) select gcd00c, gcd01c, gcd02i, gcd03c, gcd04f, gcd05i, gcd06f, gcd07c, gcd08c, gcd09d from gcm04 a with (nolock), gam26 z where a.gcd00c = z.gaz02c and z.gaz01c = @compid and (@accttype = '0' or gcd02i = cast(@accttype as int)) and (gcd01c between @cardfrom and @cardto or @cardfrom = '*') and gcd09d >= @datefrom and gcd09d <= @dateto insert into #detail(compid, depid, cardid, account_type, chg_type, amt, chg_date, billid, billtype) select _gcd00c, '', _gcd01c, _gcd02i, _gcd05i, _gcd06f, _gcd09d, _gcd08c, _gcd07c from #temp_gcm04 a, gcm01 b with (nolock) where a._gcd01c = b.gca01c and b.gca00c = b.gca13d and (@cardtype = '*' or (b.gca02c = @cardtype)) --把备注写进来 declare @cur_compid varchar(10) declare @memo varchar(250) declare @billid varchar(20) declare @billtype varchar(10) declare cur_each_gcm04 cursor for select compid, billtype, billid, memo from #detail for update of memo open cur_each_gcm04 fetch cur_each_gcm04 into @cur_compid, @billtype, @billid, @memo while @@fetch_status = 0 begin exec upg_get_bill_memo @cur_compid, @billtype, @billid, @memo output update #detail set memo = @memo where current of cur_each_gcm04 fetch cur_each_gcm04 into @cur_compid, @billtype, @billid, @memo end close cur_each_gcm04 deallocate cur_each_gcm04 update #detail set depname = gsb03c from #detail, gsm02 where gsb00c = compid and gsb01c = 'U' and gsb02c = depid update #detail set account_name = gsb03c from #detail, gsm02 where gsb00c = compid and gsb01c = 'O' and gsb02c = account_type update #detail set chg_name = gsb03c from #detail, gsm02 where gsb00c = compid and gsb01c = 'A8' and gsb02c = chg_type update #detail set prjname = gda03c from #detail, gdm01 where gda00c = compid and gda01c = prjid update a set a.memname = b.gba03c from #detail a, gbm01 b where a.cardid = b.gba23c and isnull(b.gba23c, '')<>'' and b.gba00c = a.compid update #detail set chg_srcname = case when chg_src = 0 then '卡销售' when chg_src = 1 then '卡充值' when chg_src = 2 then '收银' else '其它' end from #detail delete #detail where isnull(amt, 0) = 0 delete #detail where isnull(detail_amt, 0) = 0 and @printdetail = 1 select isnull(depid,'') depid ,isnull(depname,'') depname,isnull(cardid,'') cardid ,isnull(memname,'') memname , isnull(account_type,'') account_type ,isnull(account_name,'') account_name,isnull(chg_type,'') chg_type , isnull(chg_name,'') chg_name ,isnull(amt,'') amt ,isnull(chg_date,'') chg_date ,isnull(billtype,'') billtype , isnull(billid,'') billid,isnull(chg_src,0) chg_src ,isnull(chg_srcname,'') chg_srcname ,isnull(detail_amt,0) detail_amt , isnull(prjid,'') prjid ,isnull(prjname,'') prjname,isnull(compid,'') compid ,isnull(memo,'') memo from #detail where isnull(amt, 0) <> 0 order by depid asc, cardid asc, chg_date asc drop table #temp_gcm04 drop table #detail end GO /****** Object: Table [dbo].[gbm02] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gbm02]( [gbb00c] [varchar](10) NOT NULL, [gbb01c] [varchar](20) NOT NULL, [gbb02c] [varchar](20) NOT NULL, [gbb03c] [varchar](5) NULL, CONSTRAINT [PK_GBM02] PRIMARY KEY CLUSTERED ( [gbb00c] ASC, [gbb01c] ASC, [gbb02c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[alonecardprecept] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[alonecardprecept]( [gac00c] [varchar](10) NOT NULL, [gac01c] [varchar](20) NOT NULL, [gac02c] [varchar](20) NOT NULL, [gac03i] [int] NULL, [gac04f] [float] NULL, [gac05i] [int] NULL, [gac06f] [float] NULL, [gac07i] [int] NULL, [gac08f] [float] NULL, [gac09i] [int] NULL, [gac10f] [float] NULL, [gac11f] [float] NULL, [gac12f] [float] NULL, [gac13f] [float] NULL, [gac14f] [float] NULL, [gac15f] [float] NULL, [gac16c] [varchar](100) NULL, [gac17c] [varchar](100) NULL, [gac18c] [varchar](100) NULL, [gac19c] [varchar](100) NULL, [gac20c] [varchar](100) NULL, CONSTRAINT [PK_ALONECARDPRECEPT_New] PRIMARY KEY CLUSTERED ( [gac00c] ASC, [gac01c] ASC, [gac02c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_get_proc_lst] Script Date: 06/12/2017 06:20:08 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_get_proc_lst] ( @compid varchar(10), @cardtype varchar(20), @fromcardid varchar(20), @tocardid varchar(20) ) as begin create table #tbl_proc_lis ( idd int identity not null, itemid varchar(20) not null, itemname varchar(60) null, totaltimes float null, usedtimes float null, times float null, totalamt float null, usedamt float null, amt float null, primary key(idd) ) insert #tbl_proc_lis(itemid, totaltimes, usedtimes, times, totalamt, usedamt, amt) select gcf02c, sum(isnull(gcf03f, 0) + isnull(gcf04f, 0)), sum(gcf06f), sum(gcf07f), sum(gcf08f), sum(gcf09f), sum(gcf10f) from gcm06 a with (nolock), gcm01 b with (nolock), gam26 z where a.gcf00c = b.gca00c and a.gcf01c = b.gca01c and a.gcf00c = z.gaz02c and z.gaz01c = @compid and b.gca00c = b.gca13d and (@cardtype = '*' or (@cardtype = gca02c)) and (@fromcardid = '*' or (gca01c >= @fromcardid and gca01c <= @tocardid)) and (gca08i = 4 or gca08i = 5 or gca08i = 10) group by gcf02c order by gcf02c update a set a.itemname = b.gda03c from #tbl_proc_lis a, gdm01 b where a.itemid = b.gda01c and b.gda00c = @compid select idd, isnull(itemid,'') itemid,isnull(itemname,'') itemname,isnull(totaltimes,0) totaltimes,isnull(usedtimes,0) usedtimes, isnull(times,0) times,isnull(totalamt,0) totalamt,isnull(usedamt,0) usedamt,isnull(amt,0) amt from #tbl_proc_lis drop table #tbl_proc_lis end GO /****** Object: Table [dbo].[gfm01] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gfm01]( [gfa00c] [varchar](10) NOT NULL, [gfa01c] [varchar](20) NOT NULL, [gfa02c] [varchar](20) NULL, [gfa03c] [nvarchar](100) NULL, [gfa04d] [varchar](8) NULL, [gfa05c] [varchar](5) NULL, [gfa06c] [varchar](10) NULL, [gfa07c] [varchar](5) NULL, [gfa08c] [varchar](5) NULL, [gfa09f] [float] NULL, [gfa10c] [varchar](5) NULL, [gfa11f] [float] NULL, [gfa12c] [varchar](40) NULL, [gfa13i] [tinyint] NULL, [gfa14f] [float] NULL, [gfa15f] [float] NULL, [gfa16i] [tinyint] NULL, [gfa17f] [float] NULL, [gfa18f] [float] NULL, [gfa19f] [float] NULL, [gfa20c] [varchar](5) NULL, [gfa21f] [float] NULL, [gfa22f] [float] NULL, [gfa23c] [varchar](20) NULL, [gfa24c] [varchar](10) NULL, [gfa25i] [tinyint] NULL, [gfa26f] [float] NULL, [gfa27i] [int] NULL, [gfa28i] [int] NULL, [gfa29f] [float] NULL, [gfa30f] [float] NULL, [gfa31f] [float] NULL, [gfa32f] [float] NULL, [gfa33c] [varchar](5) NULL, [gfa34f] [float] NULL, [gfa35f] [float] NULL, [gfa36c] [varchar](5) NULL, [gfa37i] [int] NULL, [gfa38i] [int] NULL, [gfa39f] [float] NULL, [gfa40f] [float] NULL, [gfa41c] [varchar](5) NULL, [gfa42f] [float] NULL, [gfa43f] [float] NULL, [gfa44c] [varchar](5) NULL, [gfa45i] [int] NULL, [gfa46c] [varchar](20) NULL, [gfa47c] [varchar](5) NULL, [gfa97c] [varchar](20) NULL, [gfa98c] [varchar](200) NULL, [gfa99d] [datetime] NULL, [gfa39i] [int] NULL, [gfa48f] [float] NULL, [gfa49f] [float] NULL, [gfa50i] [int] NULL, [gfa51c] [varchar](20) NULL, [gfa52c] [varchar](500) NULL, [gfa70i] [int] NULL, [gfa71f] [float] NULL, [gfa72i] [int] NULL, CONSTRAINT [PK_GFM01] PRIMARY KEY CLUSTERED ( [gfa00c] ASC, [gfa01c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_arrange_transform] Script Date: 06/12/2017 06:19:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_arrange_transform] ( @compid varchar(10), -- 公司编号 @personno varchar(10), -- 员工编号 @begintime varchar(6), -- 上班时间 @endtime varchar(6), -- 下班时间 @datefrom varchar(8), -- 开始日期 @dateto varchar(8), -- 结束日期 @return varchar(2) -- 返回类型 ) as --WUXM 2005/07/15 功能:按对应时间把一行记录转换成多行记录 begin create table #arrange --排班表 ( id int identity not null, compid varchar(10) null, -- 公司编号 personno varchar(10) null, -- 员工号 date varchar(8) null, -- 日期 onduty varchar(6) null, -- 应上班时间 offduty varchar(6) null, -- 应下班时间 primary key(id) ) declare @datetmp datetime declare @strtmp varchar(20) while (select datediff(day, cast(@datefrom as datetime), cast(@dateto as datetime))) >= 0 begin insert #arrange(compid, personno, date, onduty, offduty) values(@compid, @personno, @datefrom, @begintime, @endtime) set @datetmp = dateadd(day, 1, cast(@datefrom as datetime)) set @strtmp = convert(varchar(10), cast(@datetmp as datetime), 120) set @datefrom = substring(@strtmp, 1, 4) + substring(@strtmp, 6, 2) + substring(@strtmp, 9, 2) end if (isnull(@return, '') = '1') select compid, personno, date, onduty, offduty from #arrange if (isnull(@return, '') = '2') select personno, date from #arrange drop table #arrange end GO /****** Object: Table [dbo].[gfm02] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gfm02]( [gfb00c] [varchar](10) NOT NULL, [gfb01c] [varchar](30) NOT NULL, [gfb02c] [varchar](6) NOT NULL, [gfb03c] [varchar](2) NOT NULL, [gfb04c] [varchar](4) NOT NULL, [gfb05c] [varchar](10) NULL, [gfb06f] [float] NULL, [gfb07f] [float] NULL, CONSTRAINT [PK_GFM02] PRIMARY KEY CLUSTERED ( [gfb00c] ASC, [gfb01c] ASC, [gfb02c] ASC, [gfb03c] ASC, [gfb04c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gbm24] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gbm24]( [gbx00c] [varchar](10) NOT NULL, [gbx01d] [varchar](10) NOT NULL, [gbx02f] [float] NULL, [gbx03f] [float] NULL, [gbx90d] [varchar](10) NULL, [gbx91c] [varchar](30) NULL, CONSTRAINT [PK_GBM24] PRIMARY KEY NONCLUSTERED ( [gbx00c] ASC, [gbx01d] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[personprecept_log] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[personprecept_log]( [ppl00c] [varchar](10) NOT NULL, [ppl01i] [int] IDENTITY(1,1) NOT NULL, [ppl02c] [varchar](20) NOT NULL, [ppl03c] [varchar](20) NOT NULL, [ppl04d] [varchar](8) NOT NULL, [ppl05d] [varchar](8) NOT NULL, [ppl06c] [varchar](8) NOT NULL, [ppl07d] [datetime] NOT NULL, [ppl08c] [varchar](100) NULL, [ppl09c] [varchar](100) NULL, [ppl10c] [varchar](100) NULL, [ppl11c] [varchar](100) NULL, [ppl12c] [varchar](100) NULL, [ppl13c] [varchar](2000) NULL, CONSTRAINT [PK_PERSONPRECEPT_LOG] PRIMARY KEY CLUSTERED ( [ppl00c] ASC, [ppl01i] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_delete_gsm26_gsm27] Script Date: 06/12/2017 06:19:59 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_delete_gsm26_gsm27] @compid varchar(10), @type varchar(5), --F L AL AJ @typeno varchar(5), --0-1-2-3 @classno varchar(5) --编号 as begin create table #delete ( classno varchar(5) null, idd int ) --如果他有儿子,则查询出其所有儿子 insert #delete(idd, classno) select gsq01i, gsq04c from gsm27 where gsq00c = @compid and gsq02i = @typeno and gsq03c = @classno delete b from gsm27 b, #delete where b.gsq00c = @compid and gsq02i = @typeno and gsq03c = classno delete c from gsm26 c, #delete where c.gsp00c = @compid and gsp02i = @typeno and gsp03c = classno end GO /****** Object: StoredProcedure [dbo].[comm_exec_sql] Script Date: 06/12/2017 06:19:19 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[comm_exec_sql] ( @condition ntext ) as begin exec sp_executesql @condition end GO /****** Object: StoredProcedure [dbo].[upg_arrearage_statistic] Script Date: 06/12/2017 06:19:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_arrearage_statistic] ( @compid varchar(10), -- 公司编号 @ddate varchar(20), -- 查询日期 @cardfrom varchar(20), -- 会员卡号 @cardto varchar(20) ) as --WUXM 2005/07/20 modified 2005/08/15欠款没有考虑账户异动的情况 begin -- 欠款客户以及欠款项目 create table #arrearage ( idd int identity not null, comp varchar(10) null, -- 消费门店 billno varchar(20) null, -- 消费单号 cardno varchar(20) null, -- 卡号 client varchar(40) null, -- 会员名称 ddate varchar(8) null, -- 日期 dtime varchar(6) null, -- 时间 sale varchar(20) null, -- 销售人员 project varchar(max) null, -- 欠费项目 mmoney float null, -- 欠款金额 account varchar(5) null, -- 账户类型 primary key(idd) ) -- 客户还款记录 create table #payoff ( idd int identity not null, payno varchar(20) null, -- 还款单号 cardno varchar(20) null, -- 卡号 ddate varchar(8) null, -- 还款日期 dtime varchar(6) null, -- 时间 pay float null, -- 还款金额 payacc varchar(5) null, -- 还款账户 primary key(idd) ) -- 返回报表的内容 create table #output ( idd int identity not null, comp varchar(10) null, -- 消费门店 billno varchar(100) null, -- 消费单号 cardno varchar(20) null, -- 会员卡号 client varchar(40) null, -- 会员姓名 project varchar(max) null, -- 欠费项目 arrearage float null, -- 欠费金额 payno varchar(100) null, -- 还款单号 account varchar(20) null, -- 账户类别 paymmoney float null, -- 还款金额 stillarre float null, -- 仍欠款 ddate varchar(8) null, -- 欠款日期 primary key(idd) ) create table #card_key ( comp varchar(10) null, -- 归属门店 cardno varchar(20) null, -- 会员卡号 memberno varchar(40) null, -- 会员编号 membername varchar(40) null -- 会员姓名 ) if (isnull(@cardfrom, '') = '*' or isnull(@cardfrom, '') = '') begin insert into #card_key(comp, cardno, memberno) select gca00c, gca01c, gca04c from gcm01 a with(nolock), gam26 z with(nolock) where a.gca00c = a.gca13d and a.gca00c = z.gaz02c and z.gaz01c = @compid and charindex(N'散客', a.gca01c) = 0 end else begin insert into #card_key(comp, cardno, memberno) select gca00c, gca01c, gca04c from gcm01 a with(nolock), gam26 z with(nolock) where a.gca00c = a.gca13d and a.gca00c = z.gaz02c and z.gaz01c = @compid and charindex(N'散客', a.gca01c) = 0 and a.gca01c >= @cardfrom and a.gca01c <= @cardto end update a set a.membername = b.gba03c from #card_key a, gbm01 b with(nolock) where a.comp = b.gba00c and a.memberno = b.gba01c declare @billno varchar(20) -- 消费单号 declare @mmoney float -- 欠款金额 declare @account varchar(5) -- 欠款账户 declare @billtype varchar(20) -- 单据类别 declare @curcompid varchar(10) -- 当前公司代码 -- 插入购买会员卡欠款信息 insert #arrearage(comp, billno, cardno, client, ddate, sale, project, mmoney, account, dtime) select distinct gcl00c, gcl12c, gcl03c, membername, gcl02d, gcl09c, '买卡' + '(' + gcl09c + ')', gcl07f, gcl04c, gcl56t from gcm10 with(nolock), #card_key where gcl03c = cardno and gcl06i = 5 and (gcl80d <= @ddate) and gcl11c = 'gn' -- 项目或产品消费欠账信息, 项目、产品消耗、购买产品三个明细可有可无 declare cur_client_consume cursor for select gcl00c, gcl12c, gcl07f, gcl04c, gcl11c from gcm10 with(nolock), #card_key where gcl06i = 5 and gcl07f > 0 and (gcl80d <= @ddate) and (gcl11c = 'gx'or gcl11c = 'gl') and gcl03c = cardno open cur_client_consume fetch next from cur_client_consume into @curcompid, @billno, @mmoney, @account, @billtype while @@fetch_status = 0 begin insert #arrearage(comp, billno, cardno, client, ddate, sale, project, mmoney, account, dtime) exec upg_consume_info @curcompid, @billtype, @billno, @mmoney, @account fetch next from cur_client_consume into @curcompid, @billno, @mmoney, @account, @billtype end close cur_client_consume deallocate cur_client_consume -- 账户异动中的欠款 insert #arrearage(comp, billno, cardno, client, ddate, sale, project, mmoney, account, dtime) select distinct gcl00c, gcl01c, gcl03c, membername, gcl02d, gcl09c, '充值' + '(' + gcl09c + ')', gcl07f, gcl04c, gcl56t from gcm10 a with (nolock), #card_key b where a.gcl03c = b.cardno and a.gcl06i = 5 and (isnull(gcl11c, 'gz') = 'gz' or gcl11c = '') and (gcl80d <= @ddate) -- 客户充帐信息 insert #payoff(payno, cardno, ddate, pay, payacc, dtime) select distinct gcl01c, gcl03c, gcl02d, gcl07f, gcl04c, gcl56t from gcm10 with (nolock), #card_key b where gcl06i = 6 and gcl07f > 0 and (gcl80d <= @ddate) and gcl03c = b.cardno -- 把在gcm03中没有欠款的卡全部去掉 -- 先找出来所有有欠款的卡 /* create table #tbl_def_cards ( cardid varchar(20) not null, primary key(cardid) ) insert #tbl_def_cards(cardid) select distinct gcc01c from gcm03 c with (nolock), gcm01 a with (nolock), gam26 z with(nolock) where c.gcc00c = a.gca00c and c.gcc01c = a.gca01c and a.gca00c = a.gca13d and a.gca00c = z.gaz02c and z.gaz01c = @compid and isnull(gcc10f, 0) > 0 delete a from #arrearage a where a.cardno not in (select cardid from #tbl_def_cards) delete a from #payoff a where a.cardno not in (select cardid from #tbl_def_cards) drop table #tbl_def_cards */ declare @bill varchar(20) declare @cardno varchar(20) declare @arrearage float declare @payno varchar(20) declare @pay float declare @card varchar(20) declare @mmoneya float declare @mmoneyb float declare @paynob varchar(20) declare @billa varchar(20) declare @carda varchar(20) declare @idd1 int declare @idd2 int declare @idda int declare @loop int select @loop = 1 declare @cur1count int declare @cur2count int declare @acct1 varchar(5) --欠款账户 declare @acct2 varchar(5) --还款账户 declare @ceshi int set @ceshi = 0 -- 取出欠款客户内容 declare cur_find_arrearage cursor for select idd, billno, cardno, mmoney, account from #arrearage where isnull(cardno, '') <> '' order by cardno, account, ddate, dtime open cur_find_arrearage fetch next from cur_find_arrearage into @idd1, @bill, @cardno, @arrearage, @acct1 select @cur1count = @@cursor_rows -- 取出冲账客户内容 declare cur_acculate_pay cursor for select idd, payno, cardno, pay, payacc from #payoff where isnull(cardno, '') <> '' order by cardno, payacc, ddate, dtime open cur_acculate_pay fetch next from cur_acculate_pay into @idd2, @payno, @card, @pay, @acct2 select @cur2count = @@cursor_rows set @ceshi = @ceshi +1 -- 只有欠账的情况 if (@cur1count > 0 and @cur2count = 0) begin insert #output(comp, billno, cardno, client, project, arrearage, payno, account, paymmoney, stillarre, ddate) select comp, billno, cardno, client, project, mmoney, '', account, 0, mmoney, ddate from #arrearage end -- 既有欠账,又有冲账的情况 if (@cur1count > 0 and @cur2count > 0) begin while @loop = 1 begin if (@cardno < @card) --没有还款的客户,直接插入到输入表,两个cursor分别向下走 begin insert #output(comp, billno, cardno, client, project, arrearage, payno, account, paymmoney, stillarre, ddate) select comp, @bill, @cardno, client, project, @arrearage, '', account, 0, @arrearage, ddate from #arrearage where cardno = @cardno and idd = @idd1 fetch next from cur_find_arrearage into @idd1, @bill, @cardno, @arrearage, @acct1 if @@fetch_status <> 0 select @loop = 0 continue end else if(@cardno = @card) begin -- 卡号相同并且账户相同 if (@acct1 = @acct2) begin -- 欠款 = 还款,直接将欠款记录抵消,欠款和还款表都移到下一条 if (@arrearage = @pay) begin fetch next from cur_find_arrearage into @idd1, @bill, @cardno, @arrearage, @acct1 if @@fetch_status <> 0 select @loop = 0 fetch next from cur_acculate_pay into @idd2, @payno, @card, @pay, @acct2 if @@fetch_status <> 0 begin select @payno = '', @card = 'ZZZZZ', @pay = 0, @acct2 = 'Z' end continue end -- 欠款>还款,看一下还款的下一条记录是不是仍是该卡号,并且是该账户,如果是的话,累加还款金额,如果不是的话,insert到输入表中 if (@arrearage > @pay) begin select @mmoneyb = @pay select @paynob = @payno fetch next from cur_acculate_pay into @idd2, @payno, @card, @pay, @acct2 if @@fetch_status <> 0 select @loop = 0 if @@fetch_status <> 0 begin select @payno = '', @card = 'ZZZZZ', @pay = 0, @acct2 = 'Z' end -- 卡号相同 if (@card = @cardno) begin if (@acct1 = @acct2) begin select @pay = @pay + @mmoneyb end else begin -- 是该卡号另外一个账户的还款, 输出这笔欠款和还款的资料 insert #output(comp, billno, cardno, client, project, arrearage, payno, account, paymmoney, stillarre, ddate) select comp, @bill, @cardno, client, project, @arrearage, @paynob, account, @mmoneyb, @arrearage-@mmoneyb, ddate from #arrearage where cardno = @cardno and idd = @idd1 fetch next from cur_find_arrearage into @idd1, @bill, @cardno, @arrearage, @acct1 if @@fetch_status <> 0 select @loop = 0 continue end end -- 另外一张卡号, 或没有还钱信息 if ((@card > @cardno) or (@card = 'ZZZZZ')) begin insert #output(comp, billno, cardno, client, project, arrearage, payno, account, paymmoney, stillarre, ddate) select comp, @bill, @cardno, client, project, @arrearage, @paynob, account, @mmoneyb, @arrearage-@mmoneyb, ddate from #arrearage where cardno = @cardno and idd = @idd1 fetch next from cur_find_arrearage into @idd1, @bill, @cardno, @arrearage, @acct1 if @@fetch_status <> 0 select @loop = 0 else select @loop = 1 continue end end -- 欠款小于还款, 看一下该会员的该账户是不是仍有还款, 如果有的话, 累加, 没有输出 if (@arrearage < @pay) begin select @mmoneya = @arrearage fetch next from cur_find_arrearage into @idd1, @bill, @cardno, @arrearage, @acct1 if @@fetch_status <> 0 select @loop = 0 if (@cardno = @card) begin if (@acct1 = @acct2) select @arrearage = @arrearage + @mmoneya else begin fetch next from cur_acculate_pay into @idd2, @payno, @card, @pay, @acct2 end if @@fetch_status <> 0 select @payno = '', @card = 'ZZZZZ', @pay = 0, @acct2 = 'Z' --select @loop = 0 continue end if (@cardno > @card) begin fetch next from cur_acculate_pay into @idd2, @payno, @card, @pay, @acct2 end if @@fetch_status <> 0 select @payno = '', @card = 'ZZZZZ', @pay = 0, @acct2 = 'Z' --select @loop = 0 continue end end -- 卡号相同, 但账户号不同, 直接将欠款信息输出 else begin if (@acct1 > @acct2) begin select @idda = @idd1 select @billa = @bill select @carda = @cardno select @mmoneya = @arrearage fetch next from cur_find_arrearage into @idd1, @bill, @cardno, @arrearage, @acct1 if @@fetch_status <> 0 select @loop = 0 if (@cardno > @card) begin insert #output(comp, billno, cardno, client, project, arrearage, payno, account, paymmoney, stillarre, ddate) select comp, @billa, @carda, client, project, @mmoneya, '', account, 0, @mmoneya, ddate from #arrearage where cardno = @carda and idd = @idda end continue end if (@acct1 < @acct2) begin insert #output(comp, billno, cardno, client, project, arrearage, payno, account, paymmoney, stillarre, ddate) select comp, @bill, @cardno, client, project, @arrearage, '', account, 0, @arrearage, ddate from #arrearage where cardno = @cardno and idd = @idd1 fetch next from cur_find_arrearage into @idd1, @bill, @cardno, @arrearage, @acct1 if @@fetch_status <> 0 select @loop = 0 continue end end end if (@cardno > @card) begin fetch next from cur_acculate_pay into @idd2, @payno, @card, @pay, @acct2 if @@fetch_status <> 0 select @loop = 0 if @@fetch_status <> 0 select @payno = '', @card = 'ZZZZZ', @pay = 0, @acct2 = 'Z' continue end end end close cur_acculate_pay deallocate cur_acculate_pay close cur_find_arrearage deallocate cur_find_arrearage select isnull(comp,'') comp, isnull(billno,'') billno, isnull(cardno,'') cardno, isnull(client,'') client,isnull(project,'') project, isnull(arrearage,0) arrearage, isnull(payno,'') payno,isnull(account,'') account, paymoney = isnull(paymmoney,0) , isnull(stillarre,'') stillarre, isnull(ddate,'') ddate from #output where isnull(cardno, '') <> '' order by cardno drop table #arrearage drop table #payoff drop table #output drop table #card_key end GO /****** Object: StoredProcedure [dbo].[upg_analyzer_customer_ranking] Script Date: 06/12/2017 06:19:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_analyzer_customer_ranking] ( @fromdate varchar(20), -- 消费开始日期 @todate varchar(20), -- 消费截至日期 @top int, -- 获取前多少名的数据 @order int -- 0-消费频率 1-消费金额 ) as begin if isnull(@top, 0) <= 0 set @top = 10 if isnull(@order, -1) <> 1 set @order = 0 create table #tbl_card_consume ( cardid varchar(20) not null, amt float null, times float null ) insert #tbl_card_consume(cardid, amt) select gct02c, sum(gct12f) from gcm20 with(nolock) where gct04d >= @fromdate and gct04d <= @todate and (gct06c = '4' or gct06c = '3') and isnull(gct02c, '') <> '' group by gct02c update a set a.times = b.times from #tbl_card_consume a, (select gct02c as cardid, count(gct14c) as times from (select distinct gct02c, gct14c from gcm20 with(nolock) where gct04d >= @fromdate and gct04d <= @todate and (gct06c = '4' or gct06c = '3')) F group by gct02c) b where a.cardid = b.cardid update #tbl_card_consume set amt = 0 where isnull(amt, -1) < 0 declare @PK_Sql nvarchar(max) set @PK_Sql = 'select top ' + convert(varchar(50), @top) + ' gca13d as compid, gca04c as memid, gba03c as m_name, gba14i as gender, gba08c as mobile, times as times, amt as amt from (select gca13d, gca04c, gba03c, gba14i, gba08c, sum(times) as times, sum(amt) as amt from gcm01 e with (nolock), gbm01 b with (nolock), #tbl_card_consume c where e.gca00c = e.gca13d and substring(e.gca01c, 1, 2) <> ''散客'' and e.gca00c = b.gba00c and e.gca04c = b.gba01c and e.gca01c = c.cardid group by e.gca13d, e.gca04c, b.gba03c, b.gba14i, b.gba08c) as T ' if @order = 0 set @PK_Sql = @PK_Sql + ' order by T.times desc ' else set @PK_Sql = @PK_Sql + ' order by T.amt desc ' exec sp_executesql @PK_Sql drop table #tbl_card_consume end GO /****** Object: Table [dbo].[gbm04] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gbm04]( [gbd00c] [varchar](10) NOT NULL, [gbd01c] [varchar](20) NOT NULL, [gbd02p] [image] NULL, CONSTRAINT [PK_GBM04] PRIMARY KEY CLUSTERED ( [gbd00c] ASC, [gbd01c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[myfavorite] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[myfavorite]( [sbg00c] [int] NOT NULL, [sbg01c] [varchar](10) NOT NULL, [sbg02c] [varchar](20) NOT NULL, [sbg03c] [int] NOT NULL, [sbg04c] [varchar](100) NULL, [sbg05c] [varchar](200) NULL, [sbg06c] [varchar](1000) NULL ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_init_control_set] Script Date: 06/12/2017 06:20:13 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_init_control_set] as begin create table #data_control ( compid varchar(10) not null, -- 公司别 datatype varchar(20) not null, -- 资料类型 control_lvl int null, -- 控制级别(与父亲之间的关系) 1-严格听总部 2-复制总部,保留自己 3--独立 primary key(compid, datatype) ) insert #data_control(compid, datatype, control_lvl) select gae01c, 'gdm01', 3 from gam05 where gae01c <> '001' insert #data_control(compid, datatype, control_lvl) select gae01c, 'gfm01', 3 from gam05 where gae01c <> '001' insert #data_control(compid, datatype, control_lvl) select gae01c, 'gam10', 3 from gam05 where gae01c <> '001' insert #data_control(compid, datatype, control_lvl) select gae01c, 'gsm02', 3 from gam05 where gae01c <> '001' insert #data_control(compid, datatype, control_lvl) select gae01c, 'gsm01', 3 from gam05 where gae01c<>'001' insert #data_control(compid, datatype, control_lvl) select gae01c, 'gtc01', 3 from gam05 where gae01c<>'001' insert #data_control(compid, datatype, control_lvl) select gae01c, 'hem01', 3 from gam05 where gae01c<>'001' insert #data_control(compid, datatype, control_lvl) select gae01c, 'gdm06', 3 from gam05 where gae01c<>'001' insert #data_control(compid, datatype, control_lvl) select gae01c, 'gdm20', 3 from gam05 where gae01c<>'001' insert #data_control(compid, datatype, control_lvl) select gae01c, 'gfm05', 3 from gam05 where gae01c<>'001' insert #data_control(compid, datatype, control_lvl) select gae01c, 'gbm20', 3 from gam05 where gae01c<>'001' insert #data_control(compid, datatype, control_lvl) select gae01c, 'gam04', 3 from gam05 where gae01c<>'001' insert #data_control(compid, datatype, control_lvl) select gae01c, 'gzm05', 3 from gam05 where gae01c<>'001' insert #data_control(compid, datatype, control_lvl) select gae01c, 'hbm01', 3 from gam05 where gae01c<>'001' insert #data_control(compid, datatype, control_lvl) select gae01c, 'gxm02', 3 from gam05 where gae01c<>'001' insert #data_control(compid, datatype, control_lvl) select gae01c, 'gsm15', 3 from gam05 where gae01c<>'001' insert #data_control(compid, datatype, control_lvl) select gae01c, 'satisfaction', 3 from gam05 --where gae01c<>'001' insert #data_control(compid, datatype, control_lvl) select gae01c, 'gdn01', 3 from gam05 where gae01c<>'001' insert #data_control(compid, datatype, control_lvl) select gae01c, 'personprecept', 3 from gam05 where gae01c<>'001' delete a from #data_control a, basic_data_control b where a.compid = b.compid and a.datatype = b.datatype insert basic_data_control(compid, datatype, control_lvl) select compid, datatype, control_lvl from #data_control drop table #data_control end GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gsm17] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gsm17]( [gsq00c] [varchar](10) NOT NULL, [gsq01c] [varchar](5) NOT NULL, [gsq02c] [varchar](5) NOT NULL, [gsq03i] [float] NULL, CONSTRAINT [PK_GSM17] PRIMARY KEY CLUSTERED ( [gsq00c] ASC, [gsq01c] ASC, [gsq02c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[comm_exec_sql_1] Script Date: 06/12/2017 06:19:19 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[comm_exec_sql_1] ( @condition ntext ) as begin exec sp_executesql @condition end GO /****** Object: Table [dbo].[gfm03] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gfm03]( [gfc00c] [varchar](10) NOT NULL, [gfc01c] [varchar](2) NOT NULL, [gfc02c] [nvarchar](100) NOT NULL, [gfc03c] [varchar](8) NULL, [gfc040c] [char](1) NULL, [gfc041c] [varchar](10) NULL, [gfc05c] [varchar](4) NULL, [gfc06i] [int] NULL, [gfc07c] [varchar](20) NULL, [gfc08i] [int] NULL, [gfc09t] [varchar](10) NULL, CONSTRAINT [PK_GFM03] PRIMARY KEY CLUSTERED ( [gfc00c] ASC, [gfc01c] ASC, [gfc02c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_analyzer_customer_range] Script Date: 06/12/2017 06:19:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_analyzer_customer_range] ( @comp varchar(10), -- 门店 @fromdate varchar(20), -- 消费开始日期 @todate varchar(20), -- 消费截至日期 @max float, -- 最大金额 @min float, -- 最小金额 @pay varchar(1000) -- 不参与计算的支付方式 ) as begin if isnull(@max, 0) <= 0 set @max = 9999999999 if isnull(@min, -1) <= 0 set @min = 0 if isnull(@pay, '') = '' set @pay = '''''' create table #tbl_card_consume ( cardid varchar(20) not null, amt float null, times float null ) declare @PK_Sql nvarchar(max) set @PK_Sql = 'insert #tbl_card_consume(cardid, amt) select gct02c, sum(gct12f) from gcm20 with(nolock), gcm01 with(nolock) where gct04d >= ''' + @fromdate + ''' and gct04d <= ''' + @todate + ''' and (gct06c = ''4'' or gct06c = ''3'') and isnull(gct02c, '''') <> '''' and gct02c = gca01c and gca00c = gca13d and gca00c = ''' + @comp + ''' and gct20c not in(' + @pay + ') group by gct02c ' print @PK_Sql exec sp_executesql @PK_Sql set @PK_Sql = ' update a set a.times = b.times from #tbl_card_consume a, (select gct02c as cardid, count(gct14c) as times from (select distinct gct02c, gct14c from gcm20 with(nolock), gcm01 with(nolock) where gct04d >= ''' + @fromdate + ''' and gct04d <= ''' + @todate + ''' and (gct06c = ''4'' or gct06c = ''3'') and isnull(gct02c, '''') <> '''' and gct02c = gca01c and gca00c = gca13d and gca00c = ''' + @comp + ''' and gct20c not in(' + @pay + ')) F group by gct02c) b where a.cardid = b.cardid ' print @PK_Sql exec sp_executesql @PK_Sql update #tbl_card_consume set amt = 0 where isnull(amt, -1) < 0 select gca13d as compid, gca04c as memid, gba03c as m_name, gba14i as gender, gba08c as mobile, times as times, amt as amt from (select gca13d, gca04c, gba03c, gba14i, gba08c, sum(times) as times, sum(amt) as amt from gcm01 e with (nolock), gbm01 b with (nolock), #tbl_card_consume c where e.gca00c = e.gca13d and substring(e.gca01c, 1, 2) <> '散客' and e.gca00c = b.gba00c and e.gca04c = b.gba01c and e.gca01c = c.cardid group by e.gca13d, e.gca04c, b.gba03c, b.gba14i, b.gba08c) as T where T.amt between @min and @max order by T.amt desc drop table #tbl_card_consume end GO /****** Object: Table [dbo].[gbm05] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gbm05]( [gbe00c] [varchar](10) NOT NULL, [gbe01c] [varchar](20) NOT NULL, [gbe02f] [float] NOT NULL, [gbe03c] [varchar](20) NULL, [gbe04f] [float] NULL, [gbe05c] [varchar](20) NULL, [gbe06c] [varchar](20) NULL, [gbe07c] [varchar](20) NULL, [gbe08c] [varchar](10) NULL, [gbe09c] [varchar](256) NULL, [gbe10p] [image] NULL, [gbe11c] [varchar](200) NULL, CONSTRAINT [PK_GBM05] PRIMARY KEY CLUSTERED ( [gbe00c] ASC, [gbe01c] ASC, [gbe02f] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[bkregister] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[bkregister]( [pccode] [varchar](40) NOT NULL, [regcode] [varchar](40) NULL, [regdate] [varchar](8) NULL, CONSTRAINT [PK_BKREGISTER] PRIMARY KEY CLUSTERED ( [pccode] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[wizardmodules] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[wizardmodules]( [modulegroup] [varchar](10) NULL, [system] [varchar](10) NOT NULL, [wizardprogram] [varchar](10) NULL, [program] [varchar](10) NOT NULL, [caption] [varchar](100) NULL, [moduletype] [int] NULL, [description] [text] NULL, [over_top] [int] NULL, [over_left] [int] NULL, [over_right] [int] NULL, [over_bottom] [int] NULL, [paint_top] [int] NULL, [paint_left] [int] NULL, [paint_right] [int] NULL, [paint_bottom] [int] NULL, CONSTRAINT [Pk_wizardmodules] PRIMARY KEY CLUSTERED ( [system] ASC, [program] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_report_gwm01r_lm] Script Date: 06/12/2017 06:20:36 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO Create Proc [dbo].[upg_report_gwm01r_lm] ( @companyid varchar(10), --门店 @fromdate varchar(10), --起始日期 @todate varchar(10), --结束日期 @gda02c varchar(10), --项目大类 @gda01c varchar(10), --项目简称 @rad int, --客户分类 -1不分类,0会员非会员,1男女 ,2 A类,B类,C类,3 叠加项目 @fromcs int, --来店次数 (@rad 选择了 2才有效) @tocs int, --来店次数 @jine float, --平均消费金额(@rad 选择了 2才有效) @chk int --客户明细 -1不展示 0展示明细 ) As begin Create table #kfxhtable ( gga00c varchar(60), --公司编号 gga01c varchar(60), --消费单号 gga05c varchar(60), --会员代码 gga06c varchar(60), --客户姓名 gga07c varchar(60), --会员卡类别 gga13f float, --是否为老客 gga14f float, --客人数量 gga31i int, --顾客性别 gga16f float, --总金额 gda01c varchar(60), --项目代码, gda03c varchar(60), --项目简称, gda04c varchar(60), --项目类别, gda02c varchar(60), --部门 gda02cName varchar(60), --部门名称 ggb12c varchar(60), --大工 ggb13c varchar(60), --中工 ggb14c varchar(60), --小工 ggb06c varchar(10), --价格序号(0标准价,1为体验价,2之后为项目) ggb47i int, --疗程支付的时候的疗程ID gga94d varchar(30), --覆核日期, gga80d varchar(30), --帐务日期 gda13c varchar(10) ) insert into #kfxhtable select gga00c, gga01c, gga05c, gga06c, gga07c, gga13f, gga14f, gga31i, ggb11f, --项目金额 gda01c, gda03c, gda04c, gda02c, gda02cName = (select top 1 gsb03c from gsm02 with(nolock) where gsb01c = 'U' and gsb00c = gga00c and gsb02c = gda02c), ggb12c, ggb13c, ggb14c, ggb06c, ggb47i, gga94d, gga80d, gda13c from ggm01 with(nolock) left join ggm02 with(nolock) on ggm01.gga00c = ggm02.ggb00c And ggm01.gga01c = ggm02.ggb01c inner join gdm01 with(nolock) on gdm01.gda00c = ggm02.ggb00c And gdm01.gda01c = ggm02.ggb03c where (@companyid = '' or gga00c in (select gaz02c from gam26 with(nolock) where gaz01c = @companyid ) ) And gga94d between @fromdate and @todate if(@gda02c = '' And @rad = -1 And @chk = -1) --客户大类消费分析 (大类不选,项目数据从大类来,则项目也选不上,则认为是分析大类消费) begin select isnull(gda02c,'') gda02c, isnull(gda02cName,'') gda02cName, isnull(count(gga01c),0) gga01c, isnull(sum(gga16f),0) gga16f from #kfxhtable group by gda02c, gda02cName union all select '合计:','', isnull(sum(gga01c),0) gga01c, isnull(sum(gga16f),0) gga16f from ( select gda02c, max(gda02cName) gda02cName, count(gga01c) gga01c, sum(gga16f) gga16f from #kfxhtable group by gda02c, gda02cName ) aTable end else if(@gda02c != '' And @rad = -1 And @chk = -1) --客户大类-> 统计分类 消费分析 (选了大类,则认为是分析到项目简称) begin select isnull(gda01c,'') gda01c, isnull(gda03c,'') gda03c, isnull(gda02cName,'') gda02cName, count(gga01c) gga01c, sum(gga16f) gga16f from #kfxhtable where gda02c = @gda02c and (gda13c = @gda01c or @gda01c = '') group by gda01c, gda03c, gda02cName union all select '合计:','','', isnull(sum(gga01c),0) gga01c, isnull(sum(gga16f),0) gga16f from ( select gda01c, max(gda03c) gda03c, max(gda02cName) gda02cName, count(gga01c) gga01c, sum(gga16f) gga16f from #kfxhtable where gda02c = @gda02c and (gda13c = @gda01c or @gda01c = '') group by gda01c,gda02cName ) aTable end else if(@rad != -1 And @chk = -1) begin if(@rad = 0 ) --会员非会员分类 begin select '非会员' fenlei, isnull(count(gga01c),0) gga01c, isnull(sum(gga16f),0) gga16f from #kfxhtable where gga07c = 'Z' and (gda02c = @gda02c or @gda02c = '') and (gda13c = @gda01c or @gda01c = '') union all select '会员' fenlei, count(gga01c) gga01c, sum(gga16f) gga16f from #kfxhtable where gga07c != 'Z' and (gda02c = @gda02c or @gda02c = '') and (gda13c = @gda01c or @gda01c = '') union all select '合计:', count(gga01c) gga01c, sum(gga16f) gga16f from #kfxhtable where (gda02c = @gda02c or @gda02c = '') and (gda13c = @gda01c or @gda01c = '') end else if(@rad = 1) --男女分类 begin select case gga31i when 1 then '男' when 0 then '女' end fenlei, count(gga01c) gga01c, sum(gga16f) gga16f from #kfxhtable where (gda02c = @gda02c or @gda02c = '') And (gda13c = @gda01c or @gda01c = '') group by gga31i union all select '合计:', sum(gga01c) gga01c, sum(gga16f) gga16f from ( select case gga31i when 1 then '男' when 0 then '女' end sex, count(gga01c) gga01c, sum(gga16f) gga16f from #kfxhtable where (gda02c = @gda02c or @gda02c = '') and (gda13c = @gda01c or @gda01c = '') group by gga31i ) aTable end else if( @rad = 2 ) --A,B,C类 begin select isnull(gga05c,'') gga05c, -- 会员代码, isnull(gga06c,'') gga06c, -- 客户姓名, count(gga01c) gga01c, --客单数 sum(gga16f) gga16f, --金额 Convert(decimal(18,2),sum(gga16f)/count(gga01c)) pjxh, --平均消费金额 case max(gga31i) when 1 then '男' when 0 then '女' end gga31i, isnull(gba17d,'') gba17d, -- 会员生日, isnull(gba08c,'') gba08c, -- 会员电话, isnull(gba06c,'') gba06c, -- 微信, isnull(gba05c,'') gba05c, -- 地址, '来店' + Convert(varchar(10), @fromcs) + '至' + Convert(varchar(10),@tocs) +'次,平均消费' + Convert(varchar(10),@jine) + '元' leixing from #kfxhtable left join gbm01 with(nolock) on gba00c = gga00c And gba01c = gga05c where gga07c!='Z' and (gda02c = @gda02c or @gda02c = '') and (gda13c = @gda01c or @gda01c = '') group by gga05c,gga06c,gba17d,gba08c,gba06c,gba05c having count(gga01c) >= @fromcs and count(gga01c) <= @tocs and sum(gga16f)/count(gga01c) >= @jine union all select '合计:','', sum(gga01c) gga01c, sum(gga16f) gga16f, Convert(decimal(18,2),sum(gga16f)/sum(gga01c)) pjxh, '','','','','','' from ( select gga05c, -- 会员代码, count(gga01c) gga01c, sum(gga16f) gga16f, --金额 sum(gga16f)/count(gga01c) pjxh from #kfxhtable where gga07c!='Z' and (gda02c = @gda02c or @gda02c = '') and (gda13c = @gda01c or @gda01c = '') group by gga05c having count(gga01c) >= @fromcs and count(gga01c) <= @tocs and sum(gga16f)/count(gga01c) >= @jine ) aTable end else if( @rad = 3 ) --叠加项目 begin select isnull(gga05c,'') gga05c, --会员代码, case max(gga07c) when 'Z' then '非会员' else '会员' end gga07c, --是否会员, isnull(gga06c,'') gga06c, --客户代码, case max(gga31i) when 0 then '女' else '男' end gga31i, --顾客性别, count(gga01c) gga01c, -- 叠加数目, sum(gga16f) gga16f --总金额 from #kfxhtable where ggb47i != -1 and (gda02c = @gda02c or @gda02c = '') and (gda13c = @gda01c or @gda01c = '') group by gga05c,gga06c union all select '合计:','','','', sum(gga01c) gga01c, sum(gga16f) gga16f from ( select gga05c, count(gga01c) gga01c, sum(gga16f) gga16f from #kfxhtable where ggb47i != -1 and (gda02c = @gda02c or @gda02c = '') and (gda13c = @gda01c or @gda01c = '') group by gga05c ) aTable end end else if(@chk = 0) --展示客户明细数据 begin if( @rad = 2 ) --A 类客户消费明细 begin select isnull(gda02cName,'') gda02cName, -- 大类, isnull(gda03c,'') gda03c, -- 项目简称, '来店' + Convert(varchar(10), @fromcs) + '至' + Convert(varchar(10),@tocs) +'次,平均消费' + Convert(varchar(10),@jine) + '元' gga07c, case gga31i when 1 then '男' when 0 then '女' end gga31i, isnull(gga01c,'') gga01c, -- 消费单号, isnull(gga05c,'') gga05c, -- 会员代码, isnull(gga06c,'') gga06c, -- 客户姓名, isnull(gba17d,'') gba17d, -- 会员生日, isnull(gba08c,'') gba08c, -- 会员电话, isnull(gba06c,'') gba06c, -- 微信, isnull(gba05c,'') gba05c, -- 地址, isnull(gga13f,0) gga13f, -- 是否为老客, isnull(gga14f,0) gga14f, --客人数量 isnull(gga16f,0) gga16f, --金额 isnull(gga94d,'') gga94d --日期 from #kfxhtable left join gbm01 with(nolock) on gba00c = gga00c And gba01c = gga05c where (gda02c = @gda02c or @gda02c = '') and (gda13c = @gda01c or @gda01c = '') and gga05c in ( select gga05c from #kfxhtable where gga07c!='Z' and (gda02c = @gda02c or @gda02c = '') and (gda13c = @gda01c or @gda01c = '') group by gga05c having count(gga01c) >= @fromcs and count(gga01c) <= @tocs and sum(gga16f)/count(gga01c) >= @jine ) union all select '合计:', '' gda03c, '' gga07c, '' gga31i, convert(varchar(20),count(gga01c)) gga01c, '' gga05c, '' gga06c, '' gba17d, '' gba08c, '' gba06c, '' gba05c, 0 gga13f, sum(gga14f) gga14f, sum(gga16f) gga16f, '' gga94d from #kfxhtable left join gbm01 with(nolock) on gba00c = gga00c And gba01c = gga05c where (gda02c = @gda02c or @gda02c = '') and (gda13c = @gda01c or @gda01c = '') and gga05c in ( select gga05c from #kfxhtable where gga07c!='Z' and (gda02c = @gda02c or @gda02c = '') and (gda13c = @gda01c or @gda01c = '') group by gga05c having count(gga01c) >= @fromcs and count(gga01c) <= @tocs and sum(gga16f)/count(gga01c) >= @jine ) end else if( @rad = 3) --叠加项目 begin select isnull(gga01c,'') gga01c, --消费单号, case gga07c when 'z' then '非会员' else '会员' end gga07c, --是否会员, isnull(gga05c,'') gga05c, --会员代码, isnull(gga06c,'') gga06c, --客户代码, case gga31i when 0 then '女' else '男' end gga31i ,--顾客性别, isnull(gda01c,'') gda01c, --项目代码, isnull(gga01c,'') gga01c, --叠加数目, isnull(gba17d,'') gba17d, --会员生日, isnull(gba08c,'') gba08c, --会员电话, isnull(gba06c,'') gba06c, --微信, isnull(gba05c,'') gba05c, --地址, isnull(gga13f,0) gga13f, --是否为老客, isnull(gga14f,0) gga14f, --客人数量 isnull(gga16f,0) gga16f, --金额 isnull(gga94d,'') gga94d --日期 from #kfxhtable left join gbm01 with(nolock) on gba00c = gga00c and gba01c = gga05c where ggb47i != -1 and (gda02c = @gda02c or @gda02c = '') and (gda13c = @gda01c or @gda01c = '') union all select '合计:', '' gga07c, '' gga05c, '' gga06c, '' gga31i, '' gda01c, '' gga01c, '' gba17d, '' gba08c, '' gba06c, '' gba05c, 0 gga13f, sum(gga14f) gga14f, sum(gga16f) gga16f, '' gga94d from #kfxhtable left join gbm01 on gba00c = gga00c and gba01c = gga05c where ggb47i != -1 and (gda02c = @gda02c or @gda02c = '') and (gda13c = @gda01c or @gda01c = '') end else begin select isnull(gda02cName,'') gda02cName, -- 大类, isnull(gda03c,'') gda03c, -- 项目简称, case gga07c when 'Z' then '非会员' else '会员' end gga07c, case gga31i when 1 then '男' when 0 then '女' end gga31i, isnull(gga01c,'') gga01c, -- 消费单号, isnull(gga05c,'') gga05c, -- 会员代码, isnull(gga06c,'') gga06c, -- 客户姓名, isnull(gba17d,'') gba17d, -- 会员生日, isnull(gba08c,'') gba08c, -- 会员电话, isnull(gba06c,'') gba06c, -- 微信, isnull(gba05c,'') gba05c, -- 地址, isnull(gga13f,0) gga13f, -- 是否为老客, isnull(gga14f,0) gga14f, --客人数量 isnull(gga16f,0) gga16f, --金额 isnull(gga94d,'') gga94d --日期 from #kfxhtable left join gbm01 with(nolock) on gba00c = gga00c And gba01c = gga05c where (gda02c = @gda02c or @gda02c = '') and (gda13c = @gda01c or @gda01c = '') union all select '合计:', '' gda03c, '' gga07c, '' gga31i, '' gga01c, '' gga05c, '' gga06c, '' gba17d, '' gba08c, '' gba06c, '' gba05c, 0 gga13f, sum(gga14f) gga14f, sum(gga16f) gga16f, '' gga94d from #kfxhtable left join gbm01 on gba00c = gga00c And gba01c = gga05c where (gda02c = @gda02c or @gda02c = '') and (gda13c = @gda01c or @gda01c = '') end end drop table #kfxhtable end GO /****** Object: Table [dbo].[gsm18] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gsm18]( [gsr00c] [varchar](10) NOT NULL, [gsr01i] [int] IDENTITY(1,1) NOT NULL, [gsr02d] [varchar](8) NULL, [gsr03c] [varchar](8) NULL, [gsr04c] [varchar](20) NULL, [gsr05i] [int] NULL, CONSTRAINT [PK_GSM18] PRIMARY KEY NONCLUSTERED ( [gsr00c] ASC, [gsr01i] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gsm24] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gsm24]( [gsn01i] [int] IDENTITY(1,1) NOT NULL, [gsn02c] [varchar](10) NOT NULL, [gsn03c] [varchar](200) NULL, [gsn04c] [varchar](200) NULL, [gsn05i] [int] NULL, CONSTRAINT [PK_GSM24] PRIMARY KEY CLUSTERED ( [gsn01i] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_get_upgrade_discount] Script Date: 06/12/2017 06:20:09 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_get_upgrade_discount] ( @compid varchar(10), @lvl int, @discount float output ) as begin if @lvl = 0 select @discount = 1 else if @lvl = 1 select @discount = 1 else if @lvl = 2 select @discount = 1 else if @lvl >= 3 select @discount = 1 end GO /****** Object: Table [dbo].[power_processes] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[power_processes]( [power_application] [char](50) NOT NULL, [power_hostname] [char](50) NOT NULL, [power_modulegroup] [char](10) NOT NULL, [power_system] [char](20) NOT NULL, [power_program] [char](20) NOT NULL, [usercode] [varchar](20) NULL, CONSTRAINT [PK_power_processes] PRIMARY KEY CLUSTERED ( [power_application] ASC, [power_hostname] ASC, [power_modulegroup] ASC, [power_system] ASC, [power_program] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gfm05] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gfm05]( [gfe00c] [varchar](10) NOT NULL, [gfe01c] [varchar](20) NOT NULL, [gfe02c] [varchar](30) NOT NULL, [gfe03f] [float] NULL, [gfe04i] [int] NULL, CONSTRAINT [PK_GFM05] PRIMARY KEY CLUSTERED ( [gfe00c] ASC, [gfe01c] ASC, [gfe02c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_projectbook_statistic] Script Date: 06/12/2017 06:20:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_projectbook_statistic] ( @compid varchar(10), -- 公司别 @fromman varchar(20), -- 员工编号 @toman varchar(20), @fromdate varchar(8), -- 开始日期 @todate varchar(8), -- 结束日期 @apo int -- 预约来源 ) as --WUXM 2005/08/24 begin create table #ap_result ( gwa06c varchar(100) null, -- 员工编号 gwa01d varchar(8) null, -- 预约日期 gwa02t varchar(6) null, -- 结束时间 gwa03t varchar(6) null, -- 结束时间 gwa04c varchar(20) null, -- 会员卡号 gwa05c varchar(20) null, -- 会员姓名 gda03c varchar(100) null, -- 预约项目简称 gwa07c varchar(20) null, -- 联系电话 gsb03c varchar(40) null -- 预约来源 ) if @fromman = '' or @fromman = '*' begin insert into #ap_result(gwa06c, gwa01d, gwa02t, gwa03t, gwa04c, gwa05c, gda03c, gwa07c, gsb03c) select isnull(gwa06c, ''), gwa01d, gwa02t, gwa03t, gwa04c, gwa05c, gwa13c, gwa07c, isnull(source, 3) from gwm01 with(nolock) where gwa00c = @compid and gwa01d >= @fromdate and gwa01d <= @todate and (@apo = -1 or isnull(source, 3) = @apo) and isnull(gwa13c, '') <> '' and isnull(status, 1) = 1 insert into #ap_result(gwa06c, gwa01d, gwa02t, gwa03t, gwa04c, gwa05c, gda03c, gwa07c, gsb03c) select isnull(gwa06c, ''), gwa01d, gwa02t, gwa03t, gwa04c, gwa05c, gwa14c, gwa07c, isnull(source, 3) from gwm01 with(nolock) where gwa00c = @compid and gwa01d >= @fromdate and gwa01d <= @todate and (@apo = -1 or isnull(source, 3) = @apo) and isnull(gwa14c, '') <> '' and isnull(status, 1) = 1 insert into #ap_result(gwa06c, gwa01d, gwa02t, gwa03t, gwa04c, gwa05c, gda03c, gwa07c, gsb03c) select isnull(gwa06c, ''), gwa01d, gwa02t, gwa03t, gwa04c, gwa05c, gwa15c, gwa07c, isnull(source, 3) from gwm01 with(nolock) where gwa00c = @compid and gwa01d >= @fromdate and gwa01d <= @todate and (@apo = -1 or isnull(source, 3) = @apo) and isnull(gwa15c, '') <> '' and isnull(status, 1) = 1 insert into #ap_result(gwa06c, gwa01d, gwa02t, gwa03t, gwa04c, gwa05c, gda03c, gwa07c, gsb03c) select isnull(gwa06c, ''), gwa01d, gwa02t, gwa03t, gwa04c, gwa05c, gwa19c, gwa07c, isnull(source, 3) from gwm01 with(nolock) where gwa00c = @compid and gwa01d >= @fromdate and gwa01d <= @todate and (@apo = -1 or isnull(source, 3) = @apo) and isnull(gwa19c, '') <> '' and isnull(status, 1) = 1 insert into #ap_result(gwa06c, gwa01d, gwa02t, gwa03t, gwa04c, gwa05c, gda03c, gwa07c, gsb03c) select isnull(gwa06c, ''), gwa01d, gwa02t, gwa03t, gwa04c, gwa05c, gwa20c, gwa07c, isnull(source, 3) from gwm01 with(nolock) where gwa00c = @compid and gwa01d >= @fromdate and gwa01d <= @todate and (@apo = -1 or isnull(source, 3) = @apo) and isnull(gwa20c, '') <> '' and isnull(status, 1) = 1 insert into #ap_result(gwa06c, gwa01d, gwa02t, gwa03t, gwa04c, gwa05c, gda03c, gwa07c, gsb03c) select isnull(gwa06c, ''), gwa01d, gwa02t, gwa03t, gwa04c, gwa05c, gwa21c, gwa07c, isnull(source, 3) from gwm01 with(nolock) where gwa00c = @compid and gwa01d >= @fromdate and gwa01d <= @todate and (@apo = -1 or isnull(source, 3) = @apo) and isnull(gwa21c, '') <> '' and isnull(status, 1) = 1 end else begin insert into #ap_result(gwa06c, gwa01d, gwa02t, gwa03t, gwa04c, gwa05c, gda03c, gwa07c, gsb03c) select isnull(gwa06c, ''), gwa01d, gwa02t, gwa03t, gwa04c, gwa05c, gwa13c, gwa07c, isnull(source, 3) from gwm01 with(nolock) where gwa00c = @compid and gwa01d >= @fromdate and gwa01d <= @todate and gwa06c >= @fromman and gwa06c <= @toman and (@apo = -1 or isnull(source, 3) = @apo) and isnull(gwa13c, '') <> '' and isnull(status, 1) = 1 insert into #ap_result(gwa06c, gwa01d, gwa02t, gwa03t, gwa04c, gwa05c, gda03c, gwa07c, gsb03c) select isnull(gwa06c, ''), gwa01d, gwa02t, gwa03t, gwa04c, gwa05c, gwa14c, gwa07c, isnull(source, 3) from gwm01 with(nolock) where gwa00c = @compid and gwa01d >= @fromdate and gwa01d <= @todate and gwa06c >= @fromman and gwa06c <= @toman and (@apo = -1 or isnull(source, 3) = @apo) and isnull(gwa14c, '') <> '' and isnull(status, 1) = 1 insert into #ap_result(gwa06c, gwa01d, gwa02t, gwa03t, gwa04c, gwa05c, gda03c, gwa07c, gsb03c) select isnull(gwa06c, ''), gwa01d, gwa02t, gwa03t, gwa04c, gwa05c, gwa15c, gwa07c, isnull(source, 3) from gwm01 with(nolock) where gwa00c = @compid and gwa01d >= @fromdate and gwa01d <= @todate and gwa06c >= @fromman and gwa06c <= @toman and (@apo = -1 or isnull(source, 3) = @apo) and isnull(gwa15c, '') <> '' and isnull(status, 1) = 1 insert into #ap_result(gwa06c, gwa01d, gwa02t, gwa03t, gwa04c, gwa05c, gda03c, gwa07c, gsb03c) select isnull(gwa06c, ''), gwa01d, gwa02t, gwa03t, gwa04c, gwa05c, gwa19c, gwa07c, isnull(source, 3) from gwm01 with(nolock) where gwa00c = @compid and gwa01d >= @fromdate and gwa01d <= @todate and gwa06c >= @fromman and gwa06c <= @toman and (@apo = -1 or isnull(source, 3) = @apo) and isnull(gwa19c, '') <> '' and isnull(status, 1) = 1 insert into #ap_result(gwa06c, gwa01d, gwa02t, gwa03t, gwa04c, gwa05c, gda03c, gwa07c, gsb03c) select isnull(gwa06c, ''), gwa01d, gwa02t, gwa03t, gwa04c, gwa05c, gwa20c, gwa07c, isnull(source, 3) from gwm01 with(nolock) where gwa00c = @compid and gwa01d >= @fromdate and gwa01d <= @todate and gwa06c >= @fromman and gwa06c <= @toman and (@apo = -1 or isnull(source, 3) = @apo) and isnull(gwa20c, '') <> '' and isnull(status, 1) = 1 insert into #ap_result(gwa06c, gwa01d, gwa02t, gwa03t, gwa04c, gwa05c, gda03c, gwa07c, gsb03c) select isnull(gwa06c, ''), gwa01d, gwa02t, gwa03t, gwa04c, gwa05c, gwa21c, gwa07c, isnull(source, 3) from gwm01 with(nolock) where gwa00c = @compid and gwa01d >= @fromdate and gwa01d <= @todate and gwa06c >= @fromman and gwa06c <= @toman and (@apo = -1 or isnull(source, 3) = @apo) and isnull(gwa21c, '') <> '' and isnull(status, 1) = 1 end update a set a.gwa06c = a.gwa06c + '(' + c.haa02c + ')' from #ap_result a, ham01 c with(nolock) where c.haa00c = @compid and a.gwa06c = c.haa01c update a set a.gda03c = b.gda03c from #ap_result a, gdm01 b with(nolock) where b.gda00c = @compid and a.gda03c = b.gda01c update a set a.gsb03c = d.gsb03c from #ap_result a, gsm02 d with(nolock) where d.gsb00c = @compid and d.gsb01c = 'APO' and d.gsb02c = a.gsb03c select isnull(gwa06c,'') gwa06c, isnull(gwa01d,'') gwa01d, isnull(gwa02t,'') gwa02t, isnull(gwa03t,'') gwa03t, isnull(gwa04c,'') gwa04c, isnull(gwa05c,'') gwa05c, isnull(gda03c,'') gda03c, isnull(gwa07c,'') gwa07c, isnull(gsb03c,'') gsb03c from #ap_result drop table #ap_result end GO /****** Object: StoredProcedure [dbo].[upg_get_potential_member_enter_statistics] Script Date: 06/12/2017 06:20:08 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_get_potential_member_enter_statistics] ( @compid varchar(10), -- 公司 @startDate varchar(10), -- 起始日期 @endDate varchar(10) -- 终止日期 ) as begin -- 创建临时表 create table #result ( compid varchar(20) null, -- 门店编码 cardid varchar(50) null, -- 会员卡号 membername varchar(50) null, -- 会员名 owecompid varchar(20) null, -- 卡归属门店编码 mobile varchar(20) null, -- 手机号码 sendsms int null, -- 是否已发短信 ismember int null, -- 是否是会员 bememberdate datetime null, -- 成为会员的日期 lastenter datetime null, -- 最后进店时间 times int null -- 进店次数 ) insert into #result(compid, mobile, times, lastenter) select comp, mobile, count(1), max(entertime) from potential_member_enter where comp = @compid and convert(varchar(10), entertime, 112) between @startDate and @endDate group by comp, mobile update a set a.ismember = b.ismember, a.bememberdate = b.bememberdate from #result a, potential_member b with(nolock) where a.mobile = b.mobile update a set a.owecompid = b.gca13d, a.cardid = b.gca01c, a.membername = c.gba03c from #result a, gcm01 b with(nolock), gbm01 c with(nolock) where a.mobile = c.gba08c and b.gca00c = b.gca13d and b.gca00c = c.gba00c and b.gca01c = c.gba23c update a set a.sendsms = b.sendsms from #result a, potential_member_enter_sms b with(nolock) where a.compid = b.compid and a.mobile = b.mobile and b.enterdate between @startDate and @endDate select compid, cardid, membername, owecompid, mobile, sendsms, ismember, bememberdate, lastenter from #result order by lastenter desc drop table #result end GO /****** Object: Table [dbo].[gbm07] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gbm07]( [gbg00c] [varchar](10) NOT NULL, [gbg01c] [varchar](20) NOT NULL, [gbg02f] [float] NOT NULL, [gbg03c] [varchar](40) NULL, [gbg04c] [varchar](8) NULL, [gbg05c] [varchar](10) NULL, CONSTRAINT [PK_GBM07] PRIMARY KEY CLUSTERED ( [gbg00c] ASC, [gbg01c] ASC, [gbg02f] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[openpolicy_master] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[openpolicy_master]( [compid] [varchar](20) NOT NULL, [billid] [varchar](20) NOT NULL, [person] [varchar](20) NOT NULL, [datetime] [varchar](20) NULL, [memtype] [int] NULL, [memcard] [varchar](20) NULL, [billflag] [int] NULL, [remark] [varchar](1000) NULL, [keyid] [bigint] NOT NULL, [gender] [int] NULL, [ggm_billid] [varchar](20) NULL, [endtime] [varchar](20) NULL, [ina01c] [varchar](20) NULL, CONSTRAINT [PK_OPENPOLICY_MASTER] PRIMARY KEY CLUSTERED ( [compid] ASC, [billid] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_client_return_info_ex] Script Date: 06/12/2017 06:19:42 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_client_return_info_ex] ( @compid varchar(10), @fromdate varchar(8), @todate varchar(8), @goodis varchar(5), @rettype int ) as begin create table #gbm10 ( gbj00c varchar(10) not null, -- 公司编号 gbj01i int identity not null, -- 流水号 gbj02c varchar(20) null, -- 消费单号 gbj03f float null, -- 序号 gbj04c varchar(20) null, -- 会员编号 gbj05c varchar(20) null, -- 项目编号 gbj06c varchar(20) null, -- 联系方式固定电话 gbj07d varchar(8) null, -- 服务日期 gbj08d varchar(8) null, -- 应该回访日期 gbj09c varchar(10) null, -- 回访员工编号 gbj10i int null, -- 是否已经回访 gbj11c varchar(5) null, -- 访问结果 gbj12c varchar(500) null, -- 访问结果描述 gbj13i int null, -- 类别 1:会员 2:散客 gbj14c varchar(20) null, -- 姓名 gbj15c varchar(20) null, -- 手机 gbj16i int null, -- 类别 0项目 1产品 gbj17c varchar(300) null, -- 名称 gbj18c1 varchar(20) null, --会员卡号 gbj18c varchar(20) null, --大工 gbj19c varchar(50) null, --服务满意度 gbj20c varchar(50) null, --技术满意度 gbj21c varchar(20) null, --中工 gbj22c varchar(50) null, --服务满意度 gbj23c varchar(50) null, --技术满意度 gbj24c varchar(20) null, --小工 gbj25c varchar(50) null, --服务满意度 gbj26c varchar(50) null, --技术满意度 gbj27c varchar(20) null, --四工 gbj28c varchar(50) null, --服务满意度 gbj29c varchar(50) null, --技术满意度 gbj30c varchar(200) null, --前台 gbj31c varchar(50) null, --服务满意度 gbj32c varchar(200) null, --门店 gbj33c varchar(50) null, --服务满意度 gbj34c varchar(50) null, --技术满意度 gbj28f float null, --价格 ) declare @PK_Sql nvarchar(max) set @PK_Sql = ' alter table #gbm10 add constraint PK_#gbm10_' + convert(varchar(10), cast(floor(rand() * 1000000) as int)) + ' primary key clustered(gbj00c, gbj01i)' exec sp_executesql @PK_Sql ---项目回访 insert into #gbm10(gbj00c, gbj02c, gbj03f, gbj04c, gbj05c, gbj06c, gbj07d, gbj08d, gbj09c, gbj10i, gbj11c, gbj12c, gbj13i, gbj14c, gbj15c, gbj16i, gbj17c, gbj18c, gbj19c, gbj20c, gbj21c, gbj22c, gbj23c, gbj24c, gbj25c, gbj26c, gbj27c, gbj28c, gbj29c, gbj30c, gbj31c, gbj32c, gbj33c, gbj34c) select gbj00c, gbj02c, gbj03f, gbj04c, gbj05c, gbj06c, gbj07d, gbj08d, gbj09c, gbj10i, gbj11c, gbj12c, gbj13i, gbj14c, gbj15c, gbj16i, gbj17c, (select top 1 haa01c + '-' + haa02c from ham01 where haa00c = gbj00c and haa01c = gbj18c), (select top 1 gsb03c from gsm02 where gsb01c = 'A1' and gsb00c = gbj00c and gsb02c = gbj19c and (gsb02c = @goodis or @goodis = '*')), (select top 1 gsb03c from gsm02 where gsb01c = 'A1' and gsb00c = gbj00c and gsb02c = gbj20c and (gsb02c = @goodis or @goodis = '*')), (select top 1 haa01c + '-' + haa02c from ham01 where haa00c = gbj00c and haa01c = gbj21c), (select top 1 gsb03c from gsm02 where gsb01c = 'A1' and gsb00c = gbj00c and gsb02c = gbj22c and (gsb02c = @goodis or @goodis = '*')), (select top 1 gsb03c from gsm02 where gsb01c = 'A1' and gsb00c = gbj00c and gsb02c = gbj23c and (gsb02c = @goodis or @goodis = '*')), (select top 1 haa01c + '-' + haa02c from ham01 where haa00c = gbj00c and haa01c = gbj24c), (select top 1 gsb03c from gsm02 where gsb01c = 'A1' and gsb00c = gbj00c and gsb02c = gbj25c and (gsb02c = @goodis or @goodis = '*')), (select top 1 gsb03c from gsm02 where gsb01c = 'A1' and gsb00c = gbj00c and gsb02c = gbj26c and (gsb02c = @goodis or @goodis = '*')), (select top 1 haa01c + '-' + haa02c from ham01 where haa00c = gbj00c and haa01c = gbj27c), (select top 1 gsb03c from gsm02 where gsb01c = 'A1' and gsb00c = gbj00c and gsb02c = gbj28c and (gsb02c = @goodis or @goodis = '*')), (select top 1 gsb03c from gsm02 where gsb01c = 'A1' and gsb00c = gbj00c and gsb02c = gbj29c and (gsb02c = @goodis or @goodis = '*')), (select top 1 gaa01c + '-' + gaa06c from gam01 where gaa01c = gbj30c), (select top 1 gsb03c from gsm02 where gsb01c = 'A1' and gsb00c = gbj00c and gsb02c = gbj31c and (gsb02c = @goodis or @goodis = '*')), (select top 1 gae01c + '-' + gae03c from gam05 where gae01c = gbj32c), (select top 1 gsb03c from gsm02 where gsb01c = 'A1' and gsb00c = gbj00c and gsb02c = gbj33c and (gsb02c = @goodis or @goodis = '*')), (select top 1 gsb03c from gsm02 where gsb01c = 'A1' and gsb00c = gbj00c and gsb02c = gbj34c and (gsb02c = @goodis or @goodis = '*')) from gbm10 a, gam26 z where gbj00c = z.gaz02c and z.gaz01c = @compid and gbj08d between @fromdate and @todate and gbj10i = 1 update a set gbj18c1 = gca01c from #gbm10 a, gcm01 where gbj00c = gca00c and gbj04c = gca04c and gca00c = gca13d and substring(gbj04c, 1, 2)<>'散客' if @rettype = 1 begin select * from #gbm10 where gbj19c<>'' or gbj20c<>'' or gbj22c<>'' or gbj23c<>'' or gbj25c<>'' or gbj26c<>'' or gbj28c<>'' or gbj29c<>'' or gbj31c<>'' or gbj33c<>'' or gbj34c<>'' end else begin declare @stasum float declare @stasum0 float declare @stasum1 float declare @stasum2 float declare @stasum3 float declare @stasum4 float create table #gbm10_per ( compid varchar(10) not null, statype varchar(500) not null, staclass varchar(50) null, --0 服务人员服务态度 1 服务人员技术 2 收银人员服务态度 3 门店服务态度 4 门店技术 staname varchar(50) null, fcount float null, sumcount float null ) create table #gbm10_sum ( compid varchar(10) not null, statype varchar(500) null, stacount float null, staclass varchar(50) null--0 服务人员服务态度 1 服务人员技术 2 收银人员服务态度 3 门店服务态度 4 门店技术 ) insert into #gbm10_per(compid, statype, staname, staclass) select gsb00c, gsb02c, gsb03c, '0' from gsm02 where gsb00c = @compid and gsb01c = 'A1' insert into #gbm10_per(compid, statype, staname, staclass) select gsb00c, gsb02c, gsb03c, '1' from gsm02 where gsb00c = @compid and gsb01c = 'A1' insert into #gbm10_per(compid, statype, staname, staclass) select gsb00c, gsb02c, gsb03c, '2' from gsm02 where gsb00c = @compid and gsb01c = 'A1' insert into #gbm10_per(compid, statype, staname, staclass) select gsb00c, gsb02c, gsb03c, '3' from gsm02 where gsb00c = @compid and gsb01c = 'A1' insert into #gbm10_per(compid, statype, staname, staclass) select gsb00c, gsb02c, gsb03c, '4' from gsm02 where gsb00c = @compid and gsb01c = 'A1' --大工服务 insert into #gbm10_sum(compid, statype, stacount, staclass) select gbj00c, gbj19c, count(gbj19c), '0' from #gbm10 where isnull(gbj19c, '')<>'' group by gbj00c, gbj19c --中工服务 insert into #gbm10_sum(compid, statype, stacount, staclass) select gbj00c, gbj22c, count(gbj22c), '0' from #gbm10 where isnull(gbj22c, '')<>'' group by gbj00c, gbj22c --小工服务 insert into #gbm10_sum(compid, statype, stacount, staclass) select gbj00c, gbj25c, count(gbj25c), '0' from #gbm10 where isnull(gbj25c, '')<>'' group by gbj00c, gbj25c --四工服务 insert into #gbm10_sum(compid, statype, stacount, staclass) select gbj00c, gbj28c, count(gbj28c), '0' from #gbm10 where isnull(gbj28c, '')<>'' group by gbj00c, gbj28c --大工技术 insert into #gbm10_sum(compid, statype, stacount, staclass) select gbj00c, gbj20c, count(gbj20c), '1' from #gbm10 where isnull(gbj20c, '')<>'' group by gbj00c, gbj20c --中工技术 insert into #gbm10_sum(compid, statype, stacount, staclass) select gbj00c, gbj23c, count(gbj23c), '1' from #gbm10 where isnull(gbj23c, '')<>'' group by gbj00c, gbj23c --小工技术 insert into #gbm10_sum(compid, statype, stacount, staclass) select gbj00c, gbj26c, count(gbj26c), '1' from #gbm10 where isnull(gbj26c, '')<>'' group by gbj00c, gbj26c --四工技术 insert into #gbm10_sum(compid, statype, stacount, staclass) select gbj00c, gbj29c, count(gbj29c), '1' from #gbm10 where isnull(gbj29c, '')<>'' group by gbj00c, gbj29c --收银员服务 insert into #gbm10_sum(compid, statype, stacount, staclass) select gbj00c, gbj31c, count(gbj31c), '2' from #gbm10 where isnull(gbj31c, '')<>'' group by gbj00c, gbj31c --门店服务 insert into #gbm10_sum(compid, statype, stacount, staclass) select gbj00c, gbj33c, count(gbj33c), '3' from #gbm10 where isnull(gbj33c, '')<>'' group by gbj00c, gbj33c --门店技术 insert into #gbm10_sum(compid, statype, stacount, staclass) select gbj00c, gbj34c, count(gbj34c), '4' from #gbm10 where isnull(gbj34c, '')<>'' group by gbj00c, gbj34c update a set a.fcount = isnull(b.stacount, 0) from #gbm10_per a, (select compid, statype, sum(stacount) stacount, staclass from #gbm10_sum group by compid, statype, staclass) b where a.staname = b.statype and a.staclass = b.staclass select @stasum = sum(isnull(fcount, 0)) from #gbm10_per select compid, statype, staname, sum(convert(decimal(18, 2), isnull(fcount, 0)))fcount, (case when isnull(@stasum, 0) = 0 then 0 else convert(decimal(18, 2), sum(isnull(fcount, 0))/@stasum) end )sumcount from #gbm10_per group by compid, statype, staname end drop table #gbm10 end GO /****** Object: Table [dbo].[gsm26] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gsm26]( [gsp00c] [varchar](10) NOT NULL, [gsp01i] [int] IDENTITY(1,1) NOT NULL, [gsp02i] [int] NULL, [gsp03c] [varchar](5) NULL, [gsp04c] [varchar](40) NULL, [gsp05c] [varchar](5) NULL, [gsp06c] [varchar](40) NULL, CONSTRAINT [PK_GSM26] PRIMARY KEY CLUSTERED ( [gsp00c] ASC, [gsp01i] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[openpolicy_detail] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[openpolicy_detail]( [compid] [varchar](20) NOT NULL, [billid] [varchar](20) NOT NULL, [flagid] [int] NOT NULL, [project] [varchar](20) NULL, [servicer] [varchar](8) NULL, [price] [float] NULL, [number] [float] NULL, [amount] [float] NULL, [empa] [varchar](20) NULL, [empb] [varchar](20) NULL, [empc] [varchar](20) NULL, [empaflag] [int] NULL, [empbflag] [int] NULL, [empcflag] [int] NULL, [empd] [varchar](20) NULL, [empdflag] [int] NULL, [discount] [float] NULL, [paycode] [varchar](20) NULL, CONSTRAINT [PK_OPENPOLICY_detail] PRIMARY KEY CLUSTERED ( [billid] ASC, [flagid] ASC, [compid] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gfm06] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gfm06]( [gff00c] [varchar](10) NOT NULL, [gff01c] [varchar](20) NOT NULL, [gff02p] [image] NULL, [gff03p] [image] NULL, [gff04p] [image] NULL, CONSTRAINT [PK_GFM06] PRIMARY KEY CLUSTERED ( [gff00c] ASC, [gff01c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gbm08] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gbm08]( [gbh00c] [varchar](10) NOT NULL, [gbh01c] [varchar](20) NOT NULL, [gbh02f] [float] NOT NULL, [gbh03c] [varchar](10) NULL, [gbh04c] [varchar](10) NULL, [gbh05c] [varchar](500) NULL, CONSTRAINT [PK_GBM08] PRIMARY KEY CLUSTERED ( [gbh00c] ASC, [gbh01c] ASC, [gbh02f] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_getTimeout] Script Date: 06/12/2017 06:20:10 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_getTimeout] ( @begintime varchar(20), --开始时间 @endtiem varchar(20), --结束时间 @strTimeout varchar(20) output, --时间间隔 @relt int output ) as begin declare @fromtime varchar(20) declare @totime varchar(20) declare @strtime int declare @hous varchar(10) declare @min varchar(10) select @fromtime = substring(@begintime, 1, 4) + '/' + substring(@begintime, 5, 2) + '/' + substring(@begintime, 7, 2) + ' ' + substring(@begintime, 9, 2) + ':' + substring(@begintime, 11, 2) + ':' + substring(@begintime, 13, 2) select @totime = substring(@endtiem, 1, 4) + '/' + substring(@endtiem, 5, 2) + '/' + substring(@endtiem, 7, 2) + ' ' + substring(@endtiem, 9, 2) + ':' + substring(@endtiem, 11, 2) + ':' + substring(@endtiem, 13, 2) set @strtime = cast(datediff(n, @fromtime, @totime) as int) if (@strtime>0) begin set @relt = 1 end else begin set @relt = 0 end set @hous = cast(abs(@strtime/60) as varchar) set @min = cast(abs(@strtime%60) as varchar) if (len(@hous) <= 1) begin set @hous = '0' + @hous end if (len(@min) <= 1) begin set @min = '0' + @min end set @strTimeout = @hous + @min + '00' end GO /****** Object: Table [dbo].[gsm27] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gsm27]( [gsq00c] [varchar](10) NOT NULL, [gsq01i] [int] IDENTITY(1,1) NOT NULL, [gsq02i] [int] NULL, [gsq03c] [varchar](5) NULL, [gsq04c] [varchar](5) NULL, [gsq05c] [varchar](5) NULL, CONSTRAINT [PH_GSM27] PRIMARY KEY CLUSTERED ( [gsq00c] ASC, [gsq01i] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gfm07] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gfm07]( [gfg00c] [varchar](10) NOT NULL, [gfg01c] [varchar](20) NOT NULL, [gfg02f] [float] NOT NULL, [gfg03f] [float] NULL, [gfg04c] [varchar](80) NULL, CONSTRAINT [PK_GFM07] PRIMARY KEY CLUSTERED ( [gfg00c] ASC, [gfg01c] ASC, [gfg02f] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_managerfree_statistic] Script Date: 06/12/2017 06:20:23 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_managerfree_statistic] ( @compid varchar(10), --公司别 @datefrom varchar(8), --开始时间 @dateto varchar(8) --结束日期 ) as --WUXM 2005/09/05 begin create table #cardsale ( idd int identity not null, billno varchar(20) null, --消费单号 date varchar(8) null, --帐务日期 card varchar(30) null, --会员卡号 type varchar(20) null, --消费类别 operator varchar(10) null, --操作人 money float null, --签单金额 remark varchar(400) null, --签单备注 billdate varchar(20) null, -- 单据日期 primary key(idd) ) --会员卡帐务管理 insert #cardsale(billno, date, card, type, operator, money, remark, billdate) select gcl01c, gcl80d, gcl03c, '会员卡充值', gcl91c, gsc05f, gsc06c, gcl02d from gcm10 a with (nolock), gsm03 b with (nolock) where gcl00c = @compid and gcl80d >= @datefrom and gcl80d <= @dateto and a.gcl00c = b.gsc00c and a.gcl01c = b.gsc01c and b.gsc02c = 'gz' and b.gsc04c = '8' --收银管理 insert #cardsale(billno, date, card, type, operator, money, remark, billdate) select gga01c, gga80d, gga09c, '收银', gga91c, gsc05f, gsc06c, gga02d from ggm01 with (nolock), gsm03 with (nolock) where gga00c = gsc00c and gga01c = gsc01c and gga00c = @compid and gsc04c = '8' and gga80d >= @datefrom and gga80d <= @dateto and gsc02c = 'gx' insert #cardsale(billno, date, card, type, operator, money, remark, billdate) select gla01c, gla80d, gla06c, '出院收银', gla95c, gsc05f, gsc06c, gla96d from glm01 with (nolock), gsm03 with (nolock) where gla00c = gsc00c and gla01c = gsc01c and gla00c = @compid and gsc04c = '8' and gla80d >= @datefrom and gla80d <= @dateto and gsc02c = 'gl' --会员卡销售 insert #cardsale(billno, date, card, type, operator, money, remark, billdate) exec upg_membercard_unite @compid, @datefrom, @dateto select isnull(billno,'') billno, isnull(date,'') date, isnull(card,'') card, isnull(type,'') type, isnull(operator,'') operator, isnull(money,'') money, isnull(remark,'') remark, isnull(billdate,'') billdate from #cardsale drop table #cardsale end GO /****** Object: Table [dbo].[gbm09] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gbm09]( [gbi00c] [varchar](10) NOT NULL, [gbi01c] [varchar](20) NOT NULL, [gbi02f] [float] NOT NULL, [gbi03d] [varchar](8) NULL, [gbi04c] [varchar](10) NULL, [gbi05c] [varchar](40) NULL, [gbi06p] [image] NULL, CONSTRAINT [PK_GBM09] PRIMARY KEY CLUSTERED ( [gbi00c] ASC, [gbi01c] ASC, [gbi02f] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[openpolicy_prod] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[openpolicy_prod]( [compid] [varchar](20) NOT NULL, [billid] [varchar](20) NOT NULL, [flagid] [int] NOT NULL, [project] [varchar](20) NULL, [servicer] [varchar](8) NULL, [price] [float] NULL, [number] [float] NULL, [amount] [float] NULL, [empa] [varchar](20) NULL, [empb] [varchar](20) NULL, [empc] [varchar](20) NULL, [discount] [float] NULL, [paycode] [varchar](20) NULL, [empa_sratio] [float] NULL, [empb_sratio] [float] NULL, [empc_sratio] [float] NULL, CONSTRAINT [PK_OPENPOLICY_prod] PRIMARY KEY CLUSTERED ( [billid] ASC, [flagid] ASC, [compid] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_analyzer_customer_order_regdate_lm1] Script Date: 06/12/2017 06:19:27 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_analyzer_customer_order_regdate_lm1] ( @compid varchar(10), @class varchar(10), @memfromdate varchar(10), @memtodate varchar(10), @frommemid varchar(20), @tomemid varchar(20), @fromdate varchar(20), @todate varchar(20), @state varchar(200), -- 会员卡状态 @acon_amt float, -- 大于消费金额 @alastdate varchar(8), -- 某个日期之后 @at_times float, -- 大于消费次数 @aav_amt float, -- 大于平均消费金额 @aless_remain float, -- 小于某个储值余额 @abig_remain float, -- 大于某个储值余额 @addtoSMS int, @ordertype int, @only_card_pay int ) as begin create table #mem_result_2 ( idd int not null, compid varchar(10), memid varchar(20) not null, -- Member编号 m_name varchar(40) null, -- 姓名 cardid varchar(20) null, -- 卡号 gender int null, -- 性别 0:女 mobile varchar(20) null, -- 手机 tel varchar(20) null, -- 电话 birthday varchar(8) null, -- 会员生日 state varchar(10) null, -- Member卡状态 con_amt float null, -- Consumption金额 consjamt float null, -- 实际消费金额 lastdate varchar(8) null, -- 最后Consumption日期 lastcall varchar(8) null, -- 最后回访日期 t_times float null, -- 用于比较的Consumption次数 t_compare float null, -- 总共来的次数 av_ytimes float null, -- 每年来的次数 av_times float null, -- 每月来的次数 av_wtimes float null, -- 每周来的次数 months float null, -- 客人来了多少个月了,客龄 av_amt float null, -- 每次Consumption的平均金额 start_date varchar(20) null, -- 开始日期 first_date varchar(20) null, -- 第一次来店日期 comp_date varchar(20) null, -- 开始比较的日期 dur_years float null, -- 统计期间来了多少个年了 dur_months float null, -- 统计期间来了多少个月了 dur_weeks float null, -- 统计期间来了多少个周了 remain01 float null, -- 余额 remain02 float null, -- 余额 remain03 float null, -- 余额 remain04 float null, -- 余额 remain05 float null, -- 余额 total_fill_amt float null, -- 累计Charge金额 memaddr varchar(160) null, -- 会员地址 postcode varchar(20) null, -- 邮编 principle varchar(20) null, -- 负责人 total_salecard_amt float null, -- 累计卡销售金额 regdate varchar(20) null, -- 登记日期 primary key(idd) ) insert #mem_result_2(idd, compid, memid, m_name, cardid, gender, mobile, tel, birthday, state, con_amt, consjamt, lastdate, lastcall, t_times, t_compare, av_ytimes, av_times, av_wtimes, months, av_amt, start_date, first_date, comp_date, dur_years, dur_months, dur_weeks, remain01, remain02, remain03, remain04, remain05, total_fill_amt, memaddr, postcode, principle, total_salecard_amt) exec upg_analyzer_customer_ex @compid, @class, @frommemid, @tomemid, @memfromdate, @memtodate, @fromdate, @todate, @state, @acon_amt, @alastdate, @at_times, 0, @aav_amt, @aless_remain, @abig_remain, @addtoSMS, 3, @only_card_pay --会员资料统计表,需要 select isnull(idd,'') idd, isnull(compid,'') compid, isnull(memid,'') memid, isnull(m_name,'') m_name, isnull(cardid,'') cardid, isnull(gender,0) gender, isnull(mobile,'') mobile, isnull(tel,'') tel, isnull(birthday,'') birthday, isnull(state,'') state, isnull(con_amt,0) con_amt,isnull(consjamt,0)consjamt, isnull(lastdate,'') lastdate, isnull(lastcall,'') lastcall, isnull(t_times,0) t_times, isnull(av_times,0) av_times, isnull(months,0) months, isnull(av_amt,0) av_amt, isnull(start_date,'') start_date, isnull(first_date,'') first_date, isnull(dur_months,0) dur_months, isnull(remain01,0) remain01, isnull(remain02,0) remain02, isnull(remain03,0) remain03, isnull(remain04,0) remain04, isnull(remain05,0) remain05, isnull(total_fill_amt,0) total_fill_amt, isnull(memaddr,'') memaddr, isnull(postcode,'') postcode, isnull(principle,'') principle, isnull(total_salecard_amt,0) total_salecard_amt from #mem_result_2 a order by memid drop table #mem_result_2 end GO /****** Object: StoredProcedure [dbo].[upg_cardinfo_combo_treatment_lm0_sum] Script Date: 06/12/2017 06:19:38 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_cardinfo_combo_treatment_lm0_sum] ( @compid varchar(10), --门店编号 @flag char(2), --0:疗程,1:套餐 @cardnofrom varchar(10), --会员卡号 @cardnoto varchar(20), @combofrom varchar(20), --疗程编号 @comboto varchar(20), @treatmentfrom varchar(20), --套餐编号 @treatmentto varchar(20) ) as begin --创建临时表 create table sumcardinfo ( cardcount float null, --卡数 combo_treatment float null --疗程或套餐数量 ) declare @cardcount float declare @combo_treatment float --begin会员疗程 create table #gcm06 -- 会员卡--疗程 ( gcf00c varchar(10) not null, --门店编号 gcf01c varchar(20) not null, --卡号 gcf01cName varchar(40) null, --会员卡姓名 gcf02c varchar(20) not null, --项目编号 gcf02cName varchar(40) null, --项目名称 mobileno varchar(20) null, --手机号码 gcf03f float null, --次数 gcf04f float null, --赠送次数 gcf05f float null, --总次数 gcf06f float null, --已经使用次数 gcf07f float null, --剩余次数 gcf08f float null, --疗程金额 gcf09f float null, --已经使用金额 gcf10f float null --剩余金额 -- gcf23i int not null --序号 ) -- 插入数据 insert #gcm06 (gcf00c, gcf02c, gcf01c, gcf03f, gcf04f, gcf05f, gcf08f, gcf06f, gcf09f, gcf07f, gcf10f ) select gcf00c, gcf02c, gcf01c, sum(gcf03f), sum(gcf04f), sum(gcf05f), sum(gcf08f), sum(gcf06f), sum(gcf09f), sum(gcf07f), sum(gcf10f) from gcm06 with(nolock) where gcf00c = @compid and ((gcf01c >= @cardnofrom and gcf01c <= @cardnoto) or (@cardnofrom = '*')) and ((gcf02c <= @combofrom and gcf02c >= @comboto) or (@combofrom = '*')) and gcf27i = 0 and gcf28i = 0 group by gcf00c, gcf02c, gcf01c --赋值 select @combo_treatment = count(distinct gcf02c) from #gcm06 select @cardcount = count(distinct gcf01c) from #gcm06 insert sumcardinfo (cardcount, combo_treatment) values (@cardcount, @combo_treatment) select isnull(cardcount,'') cardcount,isnull(combo_treatment,'') combo_treatment from sumcardinfo drop table #gcm06 drop table sumcardinfo end GO /****** Object: Table [dbo].[ggm01] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[ggm01]( [gga00c] [varchar](10) NOT NULL, [gga01c] [varchar](20) NOT NULL, [gga02d] [varchar](8) NULL, [gga03t] [varchar](6) NULL, [gga04t] [varchar](6) NULL, [gga05c] [varchar](20) NULL, [gga06c] [varchar](40) NULL, [gga07c] [varchar](10) NULL, [gga08c] [varchar](20) NULL, [gga09c] [varchar](20) NULL, [gga10c] [varchar](5) NULL, [gga11f] [float] NULL, [gga12c] [varchar](5) NULL, [gga13f] [float] NULL, [gga14f] [float] NULL, [gga15f] [float] NULL, [gga16f] [float] NULL, [gga17f] [float] NULL, [gga18f] [float] NULL, [gga19f] [float] NULL, [gga20c] [varchar](5) NULL, [gga21c] [varchar](20) NULL, [gga22c] [varchar](5) NULL, [gga23c] [varchar](5) NULL, [gga24c] [varchar](20) NULL, [gga25c] [varchar](20) NULL, [gga26f] [float] NULL, [gga27i] [int] NULL, [gga28c] [varchar](10) NULL, [gga29c] [varchar](5) NULL, [gga30c] [varchar](20) NULL, [gga31i] [int] NULL, [gga32c] [varchar](10) NULL, [gga91c] [varchar](10) NULL, [gga92d] [varchar](8) NULL, [gga93c] [varchar](10) NULL, [gga94d] [varchar](8) NULL, [gga95c] [varchar](10) NULL, [gga96d] [varchar](8) NULL, [gga80d] [varchar](8) NULL, [gga81i] [int] NULL, [gga99i] [int] NULL, [gga82c] [varchar](2000) NULL, [gga83d] [datetime] NULL, [gga84c] [varchar](50) NULL, [memo] [varchar](400) NULL, [gga98c] [varchar](10) NULL, [gga82i] [int] NOT NULL, [gga83i] [int] NULL, [gga33i] [int] NULL, [gga34c] [varchar](60) NULL, [gga35c] [varchar](20) NULL, [gga36i] [int] NULL, CONSTRAINT [PK_GGM01] PRIMARY KEY NONCLUSTERED ( [gga00c] ASC, [gga01c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gsm29] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gsm29]( [gsr00c] [varchar](10) NOT NULL, [gsr01c] [varchar](5) NOT NULL, [gsr02c] [varchar](5) NULL, [gsr03i] [int] NOT NULL, CONSTRAINT [PH_GSM29] PRIMARY KEY CLUSTERED ( [gsr00c] ASC, [gsr01c] ASC, [gsr03i] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gbm11] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gbm11]( [gbk01c] [varchar](10) NOT NULL, [gbk02c] [varchar](30) NULL, CONSTRAINT [PK_GBM11] PRIMARY KEY CLUSTERED ( [gbk01c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[commonproject] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[commonproject]( [compid] [varchar](20) NOT NULL, [proid] [varchar](20) NOT NULL, CONSTRAINT [PK_commonproject] PRIMARY KEY CLUSTERED ( [compid] ASC, [proid] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_cardinfo_combo_treatment_lm1_gtc04] Script Date: 06/12/2017 06:19:38 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_cardinfo_combo_treatment_lm1_gtc04] ( @compid varchar(10), --门店编号 @flag char(2), --0:疗程,1:套餐 @cardnofrom varchar(10), --会员卡号 @cardnoto varchar(20), @combofrom varchar(20), --疗程编号 @comboto varchar(20), @treatmentfrom varchar(20), --套餐编号 @treatmentto varchar(20) ) as begin --创建临时表 -套餐项目 create table #gtc04 ( gte00c varchar(10) not null, --门店编号 gte01c varchar(20) not null, --卡号 gte01cName varchar(40) null, --会员姓名 gte02c varchar(20) not null, --套餐编号 gte02cName varchar(40) null, --套餐名称 gte03c varchar(20) not null, --项目编号 gte03cName varchar(40) null, --项目名称 gte04f float null, --项目总次数 gte05f float null, --消费次数 gte06f float null, --剩余次数 gte08c varchar(20) null, --主项目 --gte09i int identity not null, --序号 flag int not null --区分套餐中的项目,产品 ) -- 插入数据 insert #gtc04(gte00c, gte01c, gte02c, gte03c, gte04f, gte05f, gte06f, gte08c, flag) select gte00c, gte01c, UPPER(gte02c), gte03c, sum(gte04f), sum(gte05f), sum(gte06f), gte08c, '1' from gtc04 with (nolock) where gte00c = @compid and ((gte01c >= @cardnofrom and gte01c <= @cardnoto) or(@cardnofrom = '*')) and ((gte02c >= @treatmentfrom and gte02c <= @treatmentto) or (@treatmentfrom = '*')) and gte12i = 0 group by gte00c, gte01c, gte02c, gte03c, gte08c --更改会员姓名 update a set a.gte01cName = b.gba03c from #gtc04 a, gbm01 b with (nolock) where a.gte00c = b.gba00c and a.gte01c = b.gba23c --更改套餐名称 update a set a.gte02cName = b.gta02c from #gtc04 a, gtc01 b with (nolock) where a.gte00c = b.gta00c and a.gte02c = b.gta01c -- 更改项目名称 update a set a.gte03cName = b.gda03c from #gtc04 a, gdm01 b with (nolock) where a.gte03c = b.gda01c and b.gda00c = @compid select isnull(gte00c,'') gte00c, isnull(gte01c,'') gte01c, isnull(gte01cName,'') gte01cName, isnull(gte02c,'') gte02c, isnull(gte02cName,'') gte02cName, isnull(gte03c,'') gte03c, isnull(gte03cName,'') gte03cName, isnull(gte04f,0) gte04f, isnull(gte05f,0) gte05f, isnull(gte06f,0) gte06f, isnull(gte08c,'') gte08c, isnull(flag,0) flag from #gtc04 order by gte02c, gte01c, gte00c --套餐项目 drop table #gtc04 end GO /****** Object: Table [dbo].[gbm12] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gbm12]( [gbl00c] [varchar](10) NOT NULL, [gbl01c] [varchar](20) NOT NULL, [gbl02c] [varchar](10) NOT NULL, [gbl03c] [varchar](30) NULL, [gbl04i] [int] NULL, [gbl05c] [varchar](30) NULL, CONSTRAINT [PK_GBM12] PRIMARY KEY CLUSTERED ( [gbl00c] ASC, [gbl01c] ASC, [gbl02c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[imgshow] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[imgshow]( [compid] [varchar](20) NOT NULL, [imgkind] [int] NOT NULL, [imgid] [varchar](20) NOT NULL, CONSTRAINT [PK_imgshow] PRIMARY KEY CLUSTERED ( [compid] ASC, [imgkind] ASC, [imgid] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_paymode_to_java] Script Date: 06/12/2017 06:20:25 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_paymode_to_java] ( @paycode nvarchar(500), @paymode nvarchar(500) output ) as begin set @paymode = @paycode if(charindex('WWQ',@paycode) > 0) set @paymode = replace(@paycode,'WWQ','#') else if(charindex('WWR',@paycode) > 0) set @paymode = replace(@paycode,'WWR','$') else if(charindex('WWE',@paycode) > 0) set @paymode = replace(@paycode,'WWE','&') end GO /****** Object: StoredProcedure [dbo].[upg_cardinfo_combo_treatment_lm1_sum] Script Date: 06/12/2017 06:19:38 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_cardinfo_combo_treatment_lm1_sum] ( @compid varchar(10), --门店编号 @flag char(2), --0:疗程,1:套餐 @cardnofrom varchar(10), --会员卡号 @cardnoto varchar(20), @combofrom varchar(20), --疗程编号 @comboto varchar(20), @treatmentfrom varchar(20), --套餐编号 @treatmentto varchar(20) ) as begin --创建临时表 create table sumcardinfo ( cardcount float null, --卡数 combo_treatment float null --疗程或套餐数量 ) declare @cardcount float declare @combo_treatment float --创建临时表 -套餐项目 create table #gtc04 ( gte00c varchar(10) not null, --门店编号 gte01c varchar(20) not null, --卡号 gte01cName varchar(40) null, --会员姓名 gte02c varchar(20) not null, --套餐编号 gte02cName varchar(40) null, --套餐名称 gte03c varchar(20) not null, --项目编号 gte03cName varchar(40) null, --项目名称 gte04f float null, --项目总次数 gte05f float null, --消费次数 gte06f float null, --剩余次数 gte08c varchar(20) null, --主项目 --gte09i int identity not null, --序号 flag int not null --区分套餐中的项目,产品 ) -- 插入数据 insert #gtc04(gte00c, gte01c, gte02c, gte03c, gte04f, gte05f, gte06f, gte08c, flag) select gte00c, gte01c, UPPER(gte02c), gte03c, sum(gte04f), sum(gte05f), sum(gte06f), gte08c, '1' from gtc04 with (nolock) where gte00c = @compid and ((gte01c >= @cardnofrom and gte01c <= @cardnoto) or(@cardnofrom = '*')) and ((gte02c >= @treatmentfrom and gte02c <= @treatmentto) or (@treatmentfrom = '*')) group by gte00c, gte01c, gte02c, gte03c, gte08c --创建临时表 -套餐产品 create table #gtc08 ( gtj00c varchar(10) not null, --门店编号 gtj01c varchar(20) not null, --卡号 gtj01cName varchar(40) null, --会员姓名 gtj02c varchar(20) not null, --套餐编号 gtj02cName varchar(40) null, --套餐名称 gtj03c varchar(20) not null, --产品编号 gtj03cName varchar(40) null, --产品名称 gtj04f float null, --总次数 gtj05f float null, --已经使用次数 gtj06f float null, --剩余次数 flag int not null --区分套餐中的项目,产品 ) -- 插入数据 insert #gtc08 (gtj00c, gtj01c, gtj02c, gtj03c, gtj04f, gtj05f, gtj06f, flag) select gtj00c, gtj01c, UPPER(gtj02c), gtj03c, sum(gtj04f), sum(gtj05f), sum(gtj06f), '0' from gtc08 with (nolock) where gtj00c = @compid and ((gtj01c >= @cardnofrom and gtj01c <= @cardnoto) or (@cardnofrom = '*')) and ((gtj02c <= @treatmentfrom and gtj02c >= @treatmentto) or (@treatmentfrom = '*')) group by gtj00c, gtj01c, gtj02c, gtj03c --赋值 select @combo_treatment = count(distinct combo_treatment) from ( select gte02c as combo_treatment from #gtc04 union all select gtj02c as combo_treatment from #gtc08 ) c select @cardcount = count(distinct cardcount) from ( select gte01c as cardcount from #gtc04 union all select gtj01c as cardcount from #gtc08 ) d insert sumcardinfo (cardcount, combo_treatment) values (@cardcount, @combo_treatment) drop table #gtc08 drop table #gtc04 select isnull(cardcount,'') cardcount,isnull(combo_treatment,'') combo_treatment from sumcardinfo drop table sumcardinfo end GO /****** Object: Table [dbo].[gsm03_alipay] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gsm03_alipay]( [gsc00c_alipay] [varchar](10) NOT NULL, [gsc01c_alipay] [varchar](20) NOT NULL, [gsc02c_alipay] [varchar](5) NOT NULL, [gsc03c_alipay] [varchar](60) NULL, [gsc04c_alipay] [varchar](10) NULL, [gsc05d_alipay] [datetime] NULL, [gsc06c_alipay] [varchar](100) NULL, [gsc07f_alipay] [float] NULL, [gsc08i_alipay] [int] NULL, [gsc09c_alipay] [varchar](10) NULL, [gsc10d_alipay] [datetime] NULL, [gsc11c_alipay] [varchar](2000) NULL, CONSTRAINT [PK_gsm03_alipay] PRIMARY KEY NONCLUSTERED ( [gsc00c_alipay] ASC, [gsc01c_alipay] ASC, [gsc02c_alipay] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[ltm01] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[ltm01]( [gta00c] [varchar](10) NOT NULL, [gta01c] [varchar](20) NOT NULL, [gta02d] [varchar](8) NULL, [gta03c] [varchar](10) NULL, [gta04c] [varchar](20) NULL, [gta05d] [varchar](8) NULL, [gta06i] [int] NULL, [gta07c] [varchar](20) NULL, [gta08d] [varchar](8) NULL, [gta09c] [varchar](20) NULL, [gta10c] [varchar](20) NULL, [gta11d] [varchar](8) NULL, [gta12c] [varchar](10) NULL, [gta13c] [varchar](80) NULL, [gta14c] [varchar](20) NULL, [gta15c] [varchar](10) NULL, [gta16t] [varchar](6) NULL, [gta17f] [float] NULL, [gta18f] [float] NULL, [gta19c] [varchar](300) NULL, [gta20i] [int] NULL, [gta21c] [varchar](60) NULL, [gta22c] [varchar](60) NULL, [gta91c] [varchar](10) NULL, [gta92d] [varchar](8) NULL, [gta93c] [varchar](10) NULL, [gta94d] [varchar](8) NULL, CONSTRAINT [pk_ltm01] PRIMARY KEY CLUSTERED ( [gta00c] ASC, [gta01c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[verinformation] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[verinformation]( [system] [varchar](10) NOT NULL, [program] [varchar](10) NULL, [version] [varchar](100) NOT NULL, [note] [varchar](200) NULL, [verdate] [varchar](10) NULL, [verflag] [int] NOT NULL, CONSTRAINT [Pk_verinformaion_id] PRIMARY KEY CLUSTERED ( [system] ASC, [version] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gum02] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gum02]( [gub00c] [varchar](10) NOT NULL, [gub01c] [varchar](20) NOT NULL, [gub02d] [varchar](8) NULL, [gub03c] [varchar](20) NULL, [gub04i] [int] NULL, [gub05c] [varchar](80) NULL, [gub06f] [float] NULL, [gub07f] [float] NULL, [gub08f] [float] NULL, [gub09t] [varchar](6) NULL, [gub10c] [varchar](10) NULL, [gub91c] [varchar](10) NULL, [gub92d] [varchar](8) NULL, [gub93c] [varchar](10) NULL, [gub94d] [varchar](8) NULL, [gub11c] [varchar](10) NULL, [gub12c] [varchar](3) NULL, CONSTRAINT [pk_gum02] PRIMARY KEY CLUSTERED ( [gub00c] ASC, [gub01c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[messages] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[messages]( [type] [varchar](20) NULL, [id] [int] NOT NULL, [content] [varchar](200) NULL, CONSTRAINT [Pk_sysmessages_id] PRIMARY KEY CLUSTERED ( [id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gum03] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gum03]( [guc00c] [varchar](10) NOT NULL, [guc01c] [varchar](20) NOT NULL, [guc02c] [varchar](20) NOT NULL, [guc03i] [int] NULL, [guc04f] [float] NULL, [guc05f] [float] NULL, [guc06c] [varchar](10) NULL, [guc91c] [varchar](10) NULL, [guc92d] [varchar](8) NULL, [guc93c] [varchar](10) NULL, [guc94d] [varchar](8) NULL, CONSTRAINT [pk_gum03] PRIMARY KEY CLUSTERED ( [guc00c] ASC, [guc01c] ASC, [guc02c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_getproject_com] Script Date: 06/12/2017 06:20:10 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_getproject_com] ( @compid varchar(10), @selfcombo varchar(20), @introcombo varchar(20), @iself int, @iintro int ) as begin create table #result ( type int null, projectno varchar(20) null, projectname varchar(20) null, projectcount float null, subprojectno varchar(20) null, ) if (isnull(@iself, 0) = 1) begin insert into #result(type, projectno, projectcount, subprojectno, projectname) select 1, gtb02c, gtb03f, gtb05c, substring(gda03c, 1, 20) from gtc02, gdm01 where gtb00c = gda00c and gtb02c = gda01c and gtb00c = @compid and gtb01c = @selfcombo end if (isnull(@iintro, 0) = 1) begin insert into #result(type, projectno, projectcount, subprojectno, projectname) select 2, gtb02c, gtb03f, gtb05c, substring(gda03c, 1, 20) from gtc02, gdm01 where gtb00c = gda00c and gtb02c = gda01c and gtb00c = @compid and gtb01c = @introcombo end select * from #result delete #result drop table #result end GO /****** Object: Table [dbo].[gbm14] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gbm14]( [gbn00c] [varchar](10) NOT NULL, [gbn01c] [varchar](20) NOT NULL, [gbn02c] [varchar](10) NULL, [gbn03c] [varchar](20) NULL, [gbn04c] [varchar](20) NULL, [gbn05c] [varchar](20) NULL, [gbn06c] [varchar](20) NULL, [gbn07c] [varchar](20) NULL, [gbn08c] [varchar](20) NULL, [gbn09c] [varchar](20) NULL, [gbn10c] [varchar](60) NULL, [gbn11c] [varchar](20) NULL, [gbn12c] [varchar](20) NULL, [gbn13c] [varchar](20) NULL, [gbn14c] [varchar](20) NULL, [gbn15c] [varchar](20) NULL, [gbn16c] [varchar](20) NULL, [gbn17c] [varchar](20) NULL, [gbn18c] [varchar](60) NULL, [gbn19c] [varchar](20) NULL, [gbn20c] [varchar](20) NULL, [gbn21c] [varchar](20) NULL, [gbn22c] [varchar](20) NULL, [gbn23c] [varchar](60) NULL, [gbn24c] [varchar](60) NULL, [gbn25c] [varchar](60) NULL, [gbn26c] [varchar](60) NULL, [gbn27c] [varchar](20) NULL, [gbn28c] [varchar](20) NULL, [gbn29c] [varchar](20) NULL, [gbn30c] [varchar](20) NULL, [gbn31c] [varchar](20) NULL, [gbn32c] [varchar](20) NULL, [gbn33c] [varchar](20) NULL, [gbn34c] [varchar](20) NULL, [gbn35c] [varchar](20) NULL, [gbn36c] [varchar](20) NULL, [gbn37c] [varchar](20) NULL, [gbn38c] [varchar](20) NULL, [gbn39c] [varchar](20) NULL, [gbn40c] [varchar](20) NULL, [gbn41c] [varchar](20) NULL, [gbn42c] [varchar](20) NULL, [gbn43c] [varchar](20) NULL, [gbn44c] [varchar](20) NULL, [gbn45c] [varchar](20) NULL, [gbn46c] [varchar](20) NULL, [gbn47c] [varchar](20) NULL, [gbn48c] [varchar](20) NULL, [gbn49c] [varchar](20) NULL, [gbn50c] [varchar](60) NULL, [gbn51p] [image] NULL, [gbn52p] [image] NULL, [gbn53i] [int] IDENTITY(1,1) NOT NULL, [gbn54c] [varchar](20) NULL, [gbn55c] [varchar](20) NULL, [gbn56c] [varchar](20) NULL, [gbn57c] [varchar](20) NULL, [gbn58c] [varchar](20) NULL, [gbn59c] [varchar](20) NULL, [gbn60c] [varchar](20) NULL, [gbn61c] [varchar](20) NULL, [gbn62c] [varchar](20) NULL, [gbn63c] [varchar](20) NULL, [gbn64c] [varchar](20) NULL, [gbn65c] [varchar](20) NULL, [gbn66c] [varchar](20) NULL, [gbn67c] [varchar](20) NULL, [gbn68c] [varchar](20) NULL, [gbn69c] [varchar](20) NULL, [gbn70c] [varchar](20) NULL, [gbn71c] [varchar](20) NULL, [gbn72c] [varchar](20) NULL, [gbn73c] [varchar](20) NULL, [gbn74c] [varchar](20) NULL, [gbn75c] [varchar](20) NULL, [gbn76c] [varchar](20) NULL, [gbn77c] [varchar](20) NULL, [gbn78c] [varchar](20) NULL, [gbn79c] [varchar](20) NULL, [gbn80c] [varchar](20) NULL, [gbn81c] [varchar](20) NULL, [gbn82c] [varchar](20) NULL, [gbn83c] [varchar](20) NULL, [gbn84c] [varchar](20) NULL, [gbn85c] [varchar](20) NULL, [gbn86c] [varchar](20) NULL, [gbn87c] [varchar](20) NULL, [gbn88c] [varchar](20) NULL, [gbn89c] [varchar](20) NULL, [gbn90c] [varchar](20) NULL, [gbn91c] [varchar](20) NULL, [gbn92c] [varchar](20) NULL, [gbn93c] [varchar](20) NULL, [gbn94c] [varchar](20) NULL, [gbn95c] [varchar](20) NULL, [gbn96c] [varchar](20) NULL, [gbn97c] [varchar](20) NULL, [gbn98c] [varchar](20) NULL, [gbn99c] [varchar](20) NULL, [gbn100c] [varchar](20) NULL, [gbn101c] [varchar](20) NULL, [gbn102c] [varchar](20) NULL, [gbn103c] [varchar](20) NULL, [gbn104c] [varchar](20) NULL, [gbn105c] [varchar](20) NULL, [gbn106c] [varchar](20) NULL, [gbn107c] [varchar](20) NULL, [gbn108c] [varchar](20) NULL, [gbn109c] [varchar](20) NULL, [gbn110c] [varchar](20) NULL, [gbn111c] [varchar](20) NULL, [gbn112c] [varchar](20) NULL, [gbn113c] [varchar](20) NULL, [gbn114c] [varchar](20) NULL, [gbn115c] [varchar](20) NULL, [gbn116c] [varchar](20) NULL, [gbn117c] [varchar](20) NULL, [gbn118c] [varchar](20) NULL, [gbn119c] [varchar](20) NULL, [gbn120c] [varchar](20) NULL, CONSTRAINT [PK_GBM14] PRIMARY KEY CLUSTERED ( [gbn00c] ASC, [gbn01c] ASC, [gbn53i] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[openpolicy_detail_del] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[openpolicy_detail_del]( [compid] [varchar](20) NOT NULL, [billid] [varchar](20) NOT NULL, [flagid] [int] NOT NULL, [project] [varchar](20) NULL, [servicer] [varchar](8) NULL, [price] [float] NULL, [number] [float] NULL, [amount] [float] NULL, [empa] [varchar](20) NULL, [empb] [varchar](20) NULL, [empc] [varchar](20) NULL, [empaflag] [int] NULL, [empbflag] [int] NULL, [empcflag] [int] NULL, [empd] [varchar](20) NULL, [empdflag] [int] NULL, [userid] [varchar](20) NULL, [ddate] [varchar](10) NULL, [ttime] [varchar](10) NULL, [idd] [int] IDENTITY(1,1) NOT NULL, CONSTRAINT [PK_OPENPOLICY_DETAIL_DEL] PRIMARY KEY CLUSTERED ( [idd] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_cal_all_employees_produce_point] Script Date: 06/12/2017 06:19:31 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_cal_all_employees_produce_point] ( @quan float, -- 数量 @standardprice float, -- 标准单价, @amt float, -- 该产品的金额, 已经包含了数量的考虑 @ex_rate float, -- 实际营业额折算比率(很多卡在销售的时候已经赠送了金额) @ex_rate_points float, -- 积分的折算比率 @p_mode int, -- 积分的计算方式 @p_setvalue float, -- 积分比率与金额 @p_points float output -- 积分 ) as begin if isnull(@p_mode, 0) = 0 select @p_points = 0 else if @p_mode = 1 select @p_points = @p_setvalue * @quan * @ex_rate_points else if @p_mode = 2 select @p_points = @amt * @p_setvalue * @ex_rate_points else if @p_mode = 5 select @p_points = @standardprice * @quan * @p_setvalue * @ex_rate_points else if @p_mode = 9 select @p_points = @amt * @ex_rate * @p_setvalue * @ex_rate_points end GO /****** Object: StoredProcedure [dbo].[upg_getgood_com] Script Date: 06/12/2017 06:20:10 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_getgood_com] ( @compid varchar(10), @selfcombo varchar(20), @introcombo varchar(20), @iself int, @iintro int ) as begin create table #result ( type int null, goodsno varchar(20) null, goodsname varchar(80) null, goodscount float null, unit varchar(20) null, ) if (isnull(@iself, 0) = 1) begin insert into #result(type, goodsno, goodscount, unit, goodsname) select 1, gtd02c , gtd03f, gfa07c, substring(gfa03c, 1, 20) from gtc03, gfm01 where gtd00c = gfa00c and gtd02c = gfa01c and gtd00c = @compid and gtd01c = @selfcombo end if (isnull(@iintro, 0) = 1) begin insert into #result(type, goodsno, goodscount, unit, goodsname) select 2, gtd02c , gtd03f, gfa07c, substring(gfa03c, 1, 20) from gtc03, gfm01 where gtd00c = gfa00c and gtd02c = gfa01c and gtd00c = @compid and gtd01c = @introcombo end select * from #result drop table #result end GO /****** Object: Table [dbo].[ggm02] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[ggm02]( [ggb00c] [varchar](10) NOT NULL, [ggb01c] [varchar](20) NOT NULL, [ggb02f] [float] NOT NULL, [ggb03c] [varchar](20) NULL, [ggb04c] [varchar](5) NULL, [ggb05f] [float] NULL, [ggb06c] [varchar](5) NULL, [ggb07f] [float] NULL, [ggb08f] [float] NULL, [ggb09f] [float] NULL, [ggb10f] [float] NULL, [ggb11f] [float] NULL, [ggb12c] [varchar](20) NULL, [ggb13c] [varchar](20) NULL, [ggb14c] [varchar](20) NULL, [ggb15c] [varchar](20) NULL, [ggb16c] [varchar](20) NULL, [ggb17f] [float] NULL, [ggb18f] [float] NULL, [ggb19c] [varchar](20) NULL, [ggb20f] [float] NULL, [ggb21f] [float] NULL, [ggb22f] [float] NULL, [ggb23c] [varchar](128) NULL, [ggb24f] [float] NULL, [ggb25c] [varchar](20) NULL, [ggb26c] [varchar](20) NULL, [ggb27c] [varchar](5) NULL, [ggb28f] [float] NULL, [ggb29f] [float] NULL, [ggb30f] [float] NULL, [ggb31c] [varchar](20) NULL, [ggb32c] [varchar](20) NULL, [ggb33f] [float] NULL, [ggb34f] [float] NULL, [ggb35f] [float] NULL, [ggb81c] [varchar](10) NULL, [ggb36f] [float] NULL, [ggb37f] [float] NULL, [ggb38f] [float] NULL, [ggb39f] [float] NULL, [ggb40i] [int] NULL, [ggb41d] [varchar](20) NULL, [ggb42c] [varchar](20) NULL, [ggb43c] [varchar](6) NULL, [ggb44c] [varchar](6) NULL, [ggb45i] [int] NULL, [ggb46f] [float] NULL, [ggb47i] [int] NULL, [ggb48i] [int] NULL, [memo] [varchar](50) NULL, [ggb82i] [int] NULL, [ggb83f] [float] NULL, CONSTRAINT [PK_GGM02] PRIMARY KEY CLUSTERED ( [ggb00c] ASC, [ggb01c] ASC, [ggb02f] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gum04] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gum04]( [gud00c] [varchar](10) NOT NULL, [gud01c] [varchar](20) NOT NULL, [gud02d] [varchar](8) NULL, [gud03c] [varchar](10) NULL ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_cal_vperf] Script Date: 06/12/2017 06:19:35 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_cal_vperf] ( @type int, -- 类型 -- 1: 固定金额; 2:实际价格的比率;5: 标准单价的比率;6: 扣其他一人,7: 扣其他第二人;8: 扣其他所有人 @setvalue float, -- 设定值 @quan float, -- 数量 @standardprice float, -- 标准价 @price float, -- 实际单价 @amt float, -- 项目总金额 @ex_rate float, -- 折算比率,以营业额为基础 @vperf_other_1 float, -- 其他一人的虚业绩 @vperf_other_2 float, -- 其他另一人的虚业绩 @vperf_4 float, -- 四工业绩 @vperf float output ) as begin if @type = 1 select @vperf = @quan * @setvalue else if @type = 2 select @vperf = @amt * @setvalue else if @type = 5 select @vperf = @standardprice * @quan * @setvalue else if @type = 6 -- 营业额扣小工 begin if (@setvalue > 1) begin select @vperf = @setvalue - @vperf_other_1 end else begin select @vperf = @amt * @setvalue - @vperf_other_1 end end else if @type = 7-- 营业额扣中工 begin if @setvalue > 1 begin select @vperf = @setvalue - @vperf_other_2 end else begin select @vperf = @amt * @setvalue - @vperf_other_2 end end else if @type = 8-- 营业额扣中小工 begin if @setvalue > 1 begin select @vperf = @setvalue - (@vperf_other_1 + @vperf_other_2) end else begin select @vperf = @amt * @setvalue - (@vperf_other_1 + @vperf_other_2) end end else if @type = 62 -- 营业额先扣小工 begin if (@setvalue > 1) begin select @vperf = @setvalue - @vperf_other_1 end else begin select @vperf = (@amt - @vperf_other_1) * @setvalue end end else if @type = 72-- 营业额先扣中工 begin if @setvalue > 1 begin select @vperf = @setvalue - @vperf_other_2 end else begin select @vperf = (@amt - @vperf_other_2) * @setvalue end end else if @type = 92-- 实际营业额先扣小工 begin if @setvalue >1 begin select @vperf = @setvalue*@ex_rate - @vperf_other_2 end else begin select @vperf = (@amt*@ex_rate - @vperf_other_2)* @setvalue end end else if @type = 82-- 营业额先扣中小工 begin if @setvalue > 1 begin select @vperf = @setvalue - (@vperf_other_1 + @vperf_other_2) end else begin select @vperf = (@amt - (@vperf_other_1 + @vperf_other_2)) * @setvalue end end else if @type = 61 -- 实际营业额扣小工 begin if (@setvalue > 1) begin select @vperf = @setvalue * @ex_rate - @vperf_other_1 end else begin select @vperf = @amt * @setvalue * @ex_rate - @vperf_other_1 end end else if @type = 71 -- 实际营业额扣中工 begin if @setvalue > 1 begin select @vperf = @setvalue * @ex_rate - @vperf_other_2 end else begin select @vperf = @amt * @setvalue * @ex_rate - @vperf_other_2 end end else if @type = 81-- 实际营业额扣中小工 begin if @setvalue > 1 begin select @vperf = @setvalue * @ex_rate - (@vperf_other_1 + @vperf_other_2) end else begin select @vperf = @amt * @setvalue * @ex_rate - (@vperf_other_1 + @vperf_other_2) end end else if @type = 9 -- 按折算后项目的金额(本身卡销售的时候已经赠送了金额) begin select @vperf = @amt * @ex_rate * @setvalue end else if @type = 10 -- 营业额的折扣折算,在营业的基础上再计算上项目的折扣 select @vperf = @amt * @setvalue * (@amt / (@standardprice * @quan)) else if @type = 11 -- 实际营业额再折扣 begin select @vperf = @amt * @ex_rate * @setvalue * (@amt / (@standardprice * @quan)) end else if @type = 15-- 实际营业额扣四工 begin if @setvalue > 1 begin select @vperf = @setvalue * @ex_rate - (@vperf_4) end else begin select @vperf = @amt * @setvalue * @ex_rate - (@vperf_4) end end set @vperf = isnull(@vperf, 0) -- 当@type = 0的时候, 这句是必须的, 补工的时候@type = 0 end GO /****** Object: Table [dbo].[sysparams] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[sysparams]( [id] [varchar](20) NOT NULL, [stringvalue] [varchar](80) NULL, [floatvalue] [float] NULL, [remark] [varchar](80) NULL, CONSTRAINT [Pk_sysparams_id] PRIMARY KEY CLUSTERED ( [id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[ggm03] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[ggm03]( [ggc00c] [varchar](10) NOT NULL, [ggc01c] [varchar](20) NOT NULL, [ggc02f] [float] NOT NULL, [ggc03c] [varchar](20) NULL, [ggc04c] [varchar](5) NULL, [ggc05f] [float] NULL, [ggc06c] [varchar](5) NULL, [ggc07f] [float] NULL, [ggc08f] [float] NULL, [ggc09f] [float] NULL, [ggc10f] [float] NULL, [ggc11f] [float] NULL, [ggc12c] [varchar](20) NULL, [ggc13f] [float] NULL, [ggc14f] [float] NULL, [ggc15c] [varchar](20) NULL, [ggc16f] [float] NULL, [ggc17c] [varchar](20) NULL, [ggc18f] [float] NULL, [ggc19c] [varchar](128) NULL, [ggc20f] [float] NULL, [ggc21f] [float] NULL, [ggc22f] [float] NULL, [ggc23f] [float] NULL, [ggc24f] [float] NULL, [ggc25f] [float] NULL, [ggc26f] [float] NULL, [ggc27c] [varchar](5) NULL, [ggc28f] [float] NULL, [ggc29f] [float] NULL, [ggc30f] [float] NULL, [ggc31c] [varchar](20) NULL, [ggc32i] [int] NULL, [ggc33d] [varchar](20) NULL, [ggc34c] [varchar](20) NULL, [ggc35i] [int] NULL, [ggc36i] [int] NULL, [ggc37f] [float] NULL, [ggc38f] [float] NULL, [ggc81c] [varchar](10) NULL, [ggc82c] [varchar](10) NULL, [ggc83c] [varchar](10) NULL, [ggc48i] [int] NULL, [ggc49f] [float] NULL, [ggc82i] [int] NULL, [ggc83f] [float] NULL, CONSTRAINT [PK_GGM03] PRIMARY KEY CLUSTERED ( [ggc00c] ASC, [ggc01c] ASC, [ggc02f] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_cal_all_employees_good_vperf_ex] Script Date: 06/12/2017 06:19:31 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_cal_all_employees_good_vperf_ex] ( @compid varchar(10), -- 公司别 @good varchar(20), -- 产品别 @quan float, -- 数量 @standardprice float, -- 标准单价, @in_cost float, -- 总进货成本, 不需要再考虑数量 @std_cost float, -- 总销售成本, 不需要再考虑数量 @price float, -- 实际单价 @amt float, -- 该产品的金额, 已经包含了数量的考虑 @ex_rate float, -- 实际营业额折算比率(很多卡在销售的时候已经赠送了金额) @ex_rate_points float, -- 积分的折算比率 @mode int, -- 业绩的计算方式 @setvalue float, -- 业绩比率与金额 @ratio float, -- 分享比率 @vperf float output, -- 虚业绩 @paymode varchar(5), -- 支付方式 @p_mode int, -- 积分的计算方式 @p_setvalue float, -- 积分比率与金额 @p_points float output -- 积分 ) as begin declare @basicamt float select @ratio = isnull(@ratio, 0) if @mode = 1 select @basicamt = @setvalue * @quan else if @mode = 2 select @basicamt = @amt * @setvalue else if @mode = 5 select @basicamt = @standardprice * @quan * @setvalue else if @mode = 9 select @basicamt = @amt * @ex_rate * @setvalue else if @mode = 10 -- 营业额的折扣折算,在营业的基础上再计算上项目的折扣 select @basicamt = @amt * @setvalue * (@amt / (@standardprice * @quan)) else if @mode = 11 -- 实际营业额再折扣 select @basicamt = @amt * @ex_rate * @setvalue * (@amt / (@standardprice * @quan)) else if @mode = 20 -- 标准价格 - 进货成本 select @basicamt = (isnull(@standardprice, 0) * isnull(@quan, 1) - isnull(@in_cost, 0)) * @setvalue else if @mode = 21 -- 营业额 - 进货成本 select @basicamt = (@amt - isnull(@in_cost, 0)) * @setvalue else if @mode = 22 -- 标准价格 - 销售成本 select @basicamt = (isnull(@standardprice, 0) * isnull(@quan, 1) - isnull(@std_cost, 0)) * @setvalue else if @mode = 23 -- 营业额 - 销售成本 select @basicamt = (@amt - isnull(@std_cost, 0)) * @setvalue select @vperf = @basicamt * @ratio if isnull(@p_mode, 0) = 0 select @p_points = 0 else if @p_mode = 1 select @p_points = @p_setvalue * @quan * @ex_rate_points else if @p_mode = 2 select @p_points = @amt * @p_setvalue * @ex_rate_points else if @p_mode = 5 select @p_points = @standardprice * @quan * @p_setvalue * @ex_rate_points else if @p_mode = 9 select @p_points = @amt * @ex_rate * @p_setvalue * @ex_rate_points end GO /****** Object: Table [dbo].[tablename] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[tablename]( [tblname] [char](20) NOT NULL, [tblengdesc] [char](50) NULL, [tblchndesc] [char](50) NULL, [isvisible] [char](2) NULL, CONSTRAINT [pk_tablename] PRIMARY KEY CLUSTERED ( [tblname] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_autocreate_gsm15_bypay] Script Date: 06/12/2017 06:19:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_autocreate_gsm15_bypay] ( @compid varchar(10)) -- 公司别 as begin create table #gsm15_2 ( gso00c varchar(10) not null, -- 公司别 gso01c varchar(10) not null, -- 分类码, 3-支付方式 gso02c varchar(5) not null, -- 支付方式类别代码 gso03i float null, -- 实际营业额折算比率 gso04f float null, -- 积分折算比率 gso05f float null, -- 买卡业绩折算比率 gso06f float null, -- 产品实际营业额折算比率 gso07f float null, -- 作为营业收入的比率 gso08f float null, -- 计算劳动业绩的比率 gso09f float null, -- 项目劳动业绩的比率 gso10f float null, -- 产品劳动业绩的比率 primary key (gso00c, gso01c, gso02c) ) insert #gsm15_2(gso00c, gso01c, gso02c, gso03i, gso04f, gso05f, gso06f, gso07f, gso08f, gso09f, gso10f) select gsb00c, gsb01c, gsb02c, 1, 1, 1, 1, 1, 1, 1, 1 from gsm02 where gsb00c = @compid and gsb01c = '3' update #gsm15_2 set gso03i = 0, gso04f = 0, gso05f = 0, gso06f = 0, gso07f = 0, gso08f = 0, gso09f = 0, gso10f = 0 where gso02c = '5' --签单挂账 update #gsm15_2 set gso03i = 0, gso04f = 0, gso05f = 0, gso06f = 0, gso07f = 0, gso08f = 0, gso09f = 0, gso10f = 0 where gso02c = '0' --内调 update #gsm15_2 set gso03i = 0, gso04f = 0, gso05f = 0, gso06f = 0, gso07f = 0, gso08f = 0, gso09f = 0, gso10f = 0 where gso02c like '$%' -- delete a from #gsm15_2 a, gsm15 b where a.gso00c = b.gso00c and a.gso01c = b.gso01c and a.gso02c = b.gso02c insert gsm15(gso00c, gso01c, gso02c, gso03i, gso04f, gso05f, gso06f, gso07f, gso08f, gso09f, gso10f) select gso00c, gso01c, gso02c, gso03i, gso04f, gso05f, gso06f, gso07f, gso08f, gso09f, gso10f from #gsm15_2 drop table #gsm15_2 end GO /****** Object: Table [dbo].[gbm16] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gbm16]( [gbp00c] [varchar](10) NOT NULL, [gbp01c] [varchar](20) NOT NULL, [gbp02c] [varchar](20) NOT NULL, [gbp03i] [int] NULL, [gbp04c] [varchar](30) NULL, CONSTRAINT [PK_GBM16] PRIMARY KEY CLUSTERED ( [gbp00c] ASC, [gbp01c] ASC, [gbp02c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[ggm05] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[ggm05]( [gge00c] [varchar](10) NOT NULL, [gge01c] [varchar](20) NOT NULL, [gge02f] [float] NOT NULL, [gge03c] [varchar](20) NULL, [gge04c] [varchar](5) NULL, [gge05f] [float] NULL, [gge06c] [varchar](5) NULL, [gge07f] [float] NULL, [gge08f] [float] NULL, [gge09f] [float] NULL, [gge10f] [float] NULL, [gge11f] [float] NULL, [gge12c] [varchar](20) NULL, [gge13f] [float] NULL, [gge14c] [varchar](20) NULL, [gge15c] [varchar](20) NULL, [gge16f] [float] NULL, [gge81c] [varchar](10) NULL, [gge17c] [varchar](20) NULL, [gge18i] [int] NULL, CONSTRAINT [PK_GGM05] PRIMARY KEY CLUSTERED ( [gge00c] ASC, [gge01c] ASC, [gge02f] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[basic_data_control] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[basic_data_control]( [compid] [varchar](10) NOT NULL, [datatype] [varchar](20) NOT NULL, [control_lvl] [int] NULL, CONSTRAINT [Pk_basic_data_control] PRIMARY KEY CLUSTERED ( [compid] ASC, [datatype] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[basic_data_type] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[basic_data_type]( [datatype] [varchar](20) NOT NULL, [description] [varchar](200) NOT NULL, CONSTRAINT [Pk_basic_data_type] PRIMARY KEY CLUSTERED ( [datatype] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gbm17] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gbm17]( [gbw00c] [varchar](10) NOT NULL, [gbw01c] [varchar](20) NOT NULL, [gbw02f] [float] NOT NULL, [gbw03c] [varchar](20) NOT NULL, [gbw04c] [varchar](5) NOT NULL, [gbw05f] [float] NULL, [gbw06d] [varchar](8) NULL, [gbw07c] [varchar](20) NULL, CONSTRAINT [PK_GBM17] PRIMARY KEY CLUSTERED ( [gbw00c] ASC, [gbw01c] ASC, [gbw02f] ASC, [gbw03c] ASC, [gbw04c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gxm01] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gxm01]( [gxa00c] [varchar](10) NOT NULL, [gxa01f] [float] NOT NULL, [gxa02c] [varchar](20) NULL, [gxa03c] [varchar](20) NULL, [gxa04c] [varchar](10) NULL, CONSTRAINT [PK_GXM01] PRIMARY KEY CLUSTERED ( [gxa00c] ASC, [gxa01f] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[ggm06] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[ggm06]( [ggf00c] [varchar](10) NOT NULL, [ggf01c] [varchar](20) NOT NULL, [ggf02c] [varchar](20) NULL, [ggf03c] [varchar](60) NULL, [ggf04i] [int] NULL, [ggf05c] [varchar](20) NULL, [ggf06c] [varchar](60) NULL, [ggf07i] [int] NULL, [ggf08c] [varchar](20) NULL, [ggf09c] [varchar](60) NULL, [ggf10i] [int] NULL, [ggf11c] [varchar](20) NULL, [ggf12c] [varchar](60) NULL, [ggf13i] [int] NULL, [ggf14c] [varchar](20) NULL, [ggf15c] [varchar](60) NULL, [ggf16i] [int] NULL, [ggf17c] [varchar](20) NULL, [ggf18c] [varchar](60) NULL, [ggf19i] [int] NULL, [ggf20c] [varchar](20) NULL, [ggf21c] [varchar](60) NULL, [ggf22i] [int] NULL, [ggf23c] [varchar](20) NULL, [ggf24c] [varchar](60) NULL, [ggf25i] [int] NULL, CONSTRAINT [PK_GGM06] PRIMARY KEY CLUSTERED ( [ggf00c] ASC, [ggf01c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gmm01] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gmm01]( [gma00c] [varchar](10) NOT NULL, [gma01d] [varchar](8) NOT NULL, [gma02i] [int] NOT NULL, [gma03c] [varchar](20) NOT NULL, [gma04c] [varchar](5) NOT NULL, [gma05f] [float] NULL, [gma06f] [float] NULL, [gma07f] [float] NULL, CONSTRAINT [PK_GMM01] PRIMARY KEY CLUSTERED ( [gma00c] ASC, [gma01d] ASC, [gma02i] ASC, [gma03c] ASC, [gma04c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_stat_card_sale_groupby_cardtype] Script Date: 06/12/2017 06:20:43 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_stat_card_sale_groupby_cardtype] ( @incompid varchar(10), -- 公司别 @fromdate varchar(10), -- 开始日期 @todate varchar(10), -- 截至日期 @fromcardtype varchar(10), -- 卡类别 @tocardtype varchar(10), -- 卡类别 @fromcardid varchar(20), -- 开始卡号 @tocardid varchar(20), -- 截至卡号 @includeproc int ) as begin declare @proc_acct int -- 疗程账户 select @proc_acct = 4 if @includeproc = 0 begin select @proc_acct = 2 -- 这样就除了疗程账户了 end if @fromdate = '*' or @fromdate = '' begin select @fromdate = '19000101', @todate = '20901231' end create table #tbl_cards ( cardid varchar(20) null ) create table #tbl_card_sale_detail_buf ( compid varchar(20) not null, -- 公司别 ddate varchar(10) not null, -- 日期 ttime varchar(10) not null, -- 时间 billid varchar(20) not null, -- 单号 billtype varchar(20) not null, -- 单别 mode int null, -- 方式 0 会员卡销售 1 充 2 还款 3 取款 4欠款 saleamt float null, accamt float null, -- 账户销售金额 因为需要调用日报表 而日报表里面里面增加了账户销售 savedamt float null, procamt float null, -- 疗程金额 卡销售日报表里 增加了 疗程金额 cardid varchar(20) null, cardtype varchar(10) null, acctype varchar (40) null, -- 账户类别 卡销售日报表里 增加了 账户类别 memname varchar(80) null, memphone varchar(20) null, sales varchar(40) null, salesid1 varchar(100) null, salesid1_p float null, -- 第一分享比率 ADD BY LEIJI on 2012-7-26 salesid2 varchar(100) null, salesid2_p float null, -- 第二分享比率 salesid3 varchar(100) null, salesid3_p float null, -- 第三分享比率 memo varchar(60) null, upgradeflag int null, oldcardtype varchar(20) null, sendacct varchar(200) null, -- 赠送账户 sendamt varchar(200) null -- 赠送金额 ) create clustered index idx_card_sale_detail_buf on #tbl_card_sale_detail_buf(compid, billtype, billid) create nonclustered index idx_card_sale_detail_buf_002 on #tbl_card_sale_detail_buf(compid, cardid) create table #tbl_card_sale_group ( cardtype varchar(20) not null, cnt float null, amt float null, -- 卡销售金额 savedamt float null, -- 储值金额 consume float null, -- 这个时间之内的消费 own_consume float null, -- 归属本公司的卡的消费 remain float null -- 只能显示目前余额(只包括本公司的卡) ) create clustered index idx_card_sale_group on #tbl_card_sale_group(cardtype) create table #tbl_card_consume ( cardtype varchar(20) not null, amt float null ) create clustered index idx_card_consume on #tbl_card_consume(cardtype) create table #card_type_remain ( cardtype varchar(20) not null, remain float null ) create clustered index idx_type_remain on #card_type_remain(cardtype) insert #tbl_card_sale_detail_buf(compid, ddate, ttime, billid, billtype, mode, saleamt, accamt, savedamt, procamt, cardid, cardtype, acctype, memname, memphone, sales, salesid1, salesid1_p, salesid2, salesid2_p, salesid3, salesid3_p, memo, upgradeflag, oldcardtype, sendacct, sendamt) exec upg_stat_card_sale @incompid, @fromdate, @todate, @fromcardtype, @tocardtype, '*', '*', @fromcardid, @tocardid, 0, @includeproc, '*' -- *代表所有的会员卡销售 insert #tbl_cards(cardid) select distinct cardid from #tbl_card_sale_detail_buf insert #tbl_card_sale_group(cardtype, cnt, amt, savedamt) select cardtype, cnt = sum(1), amt = sum(saleamt), sum(savedamt) from #tbl_card_sale_detail_buf group by cardtype -- 统计这个时间之内的消费累计, 从gcm04中统计 insert #tbl_card_consume(cardtype, amt) select b.gca02c, sum(a.gcd06f) from gcm04 a with (nolock), gcm01 b with (nolock), #tbl_cards c where a.gcd01c = b.gca01c and a.gcd09d >= @fromdate and a.gcd09d <= @todate and ((a.gcd01c >= @fromcardid and a.gcd01c <= @tocardid) or (@fromcardid = '*')) and ((b.gca02c >= @fromcardtype and b.gca02c <= @tocardtype) or (@fromcardtype = '*')) and (a.gcd05i = 2) and ((a.gcd02i = 2 or gcd02i = @proc_acct) or (gcd02i >= 5 and gcd02i < 888) ) and (gca08i = 4 or gca08i = 5 or gca08i = 10) and gca00c = gca13d and gca01c = c.cardid group by b.gca02c update a set a.consume = b.amt from #tbl_card_sale_group a, #tbl_card_consume b where a.cardtype = b.cardtype -- 统计归属卡的消费 delete #tbl_card_consume insert #tbl_card_consume(cardtype, amt) select b.gca02c, sum(a.gcd06f) from gcm04 a with (nolock), gcm01 b with (nolock), #tbl_cards c, gam26 z with (nolock) where a.gcd01c = b.gca01c and a.gcd09d >= @fromdate and a.gcd09d <= @todate and ((a.gcd01c >= @fromcardid and a.gcd01c <= @tocardid) or (@fromcardid = '*')) and ((b.gca02c >= @fromcardtype and b.gca02c <= @tocardtype) or (@fromcardtype = '*')) and (a.gcd05i = 2) and ((a.gcd02i = 2 or gcd02i = @proc_acct) or (gcd02i >= 5 and gcd02i < 888) ) and (gca08i = 4 or gca08i = 5 or gca08i = 10) and gca00c = gca13d and gca01c = c.cardid and gca00c = z.gaz02c and z.gaz01c = @incompid group by b.gca02c update a set a.own_consume = b.amt from #tbl_card_sale_group a, #tbl_card_consume b where a.cardtype = b.cardtype -- 更新最新的卡余额, 只统计归属为本公司的卡的最新余额 -- 需要先合计卡的各账户的余额 insert #card_type_remain(cardtype, remain) select gca02c, sum(gcc06f) from gcm01 a with (nolock), gcm03 b with (nolock), #tbl_cards c, gam26 z with (nolock) where a.gca00c = b.gcc00c and a.gca01c = b.gcc01c and ((a.gca01c >= @fromcardid and a.gca01c <= @tocardid) or (@fromcardid = '*')) and ((a.gca02c >= @fromcardtype and a.gca02c <= @tocardtype) or (@fromcardtype = '*')) and ((b.gcc03i = 2 or b.gcc03i = @proc_acct) or (b.gcc03i >= 5 and b.gcc03i < 888)) and gca00c = gca13d and (gca08i = 4 or gca08i = 5 or gca08i = 10) and a.gca01c = c.cardid and a.gca00c = z.gaz02c and z.gaz01c = @incompid group by gca02c update a set a.remain = b.remain from #tbl_card_sale_group a, #card_type_remain b where a.cardtype = b.cardtype insert #tbl_card_sale_group(cardtype) select gak01c from gam10 with (nolock) where gak00c = @incompid and gak01c not in(select cardtype from #tbl_card_sale_group) select isnull(cardtype,'') cardtype, isnull(cnt,0) cnt, isnull(amt,0) amt, isnull(savedamt,0) savedamt, isnull(consume,0) consume, isnull(own_consume,0) own_consume, isnull(remain,0) remain from #tbl_card_sale_group drop table #tbl_card_sale_detail_buf drop table #tbl_card_sale_group drop table #tbl_card_consume drop table #card_type_remain drop table #tbl_cards end GO /****** Object: StoredProcedure [dbo].[upg_recal_proc_amt] Script Date: 06/12/2017 06:20:31 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_recal_proc_amt] ( @compid varchar(10) ) as begin declare @newacct int select @newacct = 4 -- 每个卡的疗程总金额 create table #tbl_proc_amt ( cardno varchar(20) not null, seq float null, amt float null, primary key(cardno) ) -- 可用的序号 create table #tbl_gcm03_seq ( cardno varchar(20) not null, seq float null, primary key(cardno) ) -- 统计每个卡的所有疗程金额之和 insert #tbl_proc_amt(cardno, amt) select gcf01c, sum(gcf10f) from gcm06 with (nolock) where gcf00c = @compid and isnull(gcf01c, '') <> '' group by gcf01c -- 可能不存在这个账户 -- 先更新存在的账户,然后删除temp table update a set a.gcc06f = isnull(amt, 0) from gcm03 a, #tbl_proc_amt b where a.gcc00c = @compid and a.gcc01c = b.cardno and a.gcc03i = @newacct -- 已经存在的先删除 delete b from #tbl_proc_amt b, gcm03 a where a.gcc00c = @compid and a.gcc01c = b.cardno and a.gcc03i = @newacct -- 先得到每个会员卡可用的序号 insert #tbl_gcm03_seq(cardno, seq) select gcc01c, max(gcc02f) + 1 from gcm03 a with (nolock), #tbl_proc_amt b where a.gcc00c = @compid and a.gcc01c = b.cardno group by gcc00c, gcc01c update a set a.seq = b.seq from #tbl_proc_amt a, #tbl_gcm03_seq b where a.cardno = b.cardno insert gcm03(gcc00c, gcc01c, gcc02f, gcc03i, gcc04c, gcc05c, gcc06f) select @compid, cardno, seq, @newacct, '', 'RMB', amt from #tbl_proc_amt drop table #tbl_gcm03_seq drop table #tbl_proc_amt end GO /****** Object: StoredProcedure [dbo].[upg_calc_instant_history] Script Date: 06/12/2017 06:19:36 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_calc_instant_history] ( @comp varchar(10) ) as begin create table #result ( calc_type int not null, -- 算子类型 calc_comp varchar(10) not null, -- 计算门店 calc_time datetime not null, -- 计算开始时间 fromdate varchar(8) not null, -- 开始日期 todate varchar(8) not null, -- 截止日期 success int null, -- 成功标记(0-失败 1-成功 null-未执行) consumed float null, -- 计算用时 ) create table #operator_id ( calc_type int ) insert into #operator_id(calc_type) select calc_type from calc_operator -- 创建循环变量 declare @HasRecord int set @HasRecord = 1 declare @current_calc_type int while(@HasRecord = 1) begin if exists(select 1 from #operator_id) begin set @HasRecord = 1 select top 1 @current_calc_type = calc_type from #operator_id insert into #result(calc_type, calc_comp, calc_time, fromdate, todate, success, consumed) select top 20 calc_type, calc_comp, calc_time, fromdate, todate, success, consumed from calc_instant where inst_comp = @comp and calc_type = @current_calc_type order by calc_time desc -- 清除已处理的门店 delete #operator_id where calc_type = @current_calc_type end else begin set @HasRecord = 0 end end select calc_type, calc_comp, calc_time, fromdate, todate, success, consumed from #result order by calc_time desc drop table #operator_id drop table #result end GO /****** Object: StoredProcedure [dbo].[upg_avaliable_combo_items] Script Date: 06/12/2017 06:19:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_avaliable_combo_items]( @compid varchar(10), @cardid varchar(20)) as begin create table #gtc04_buf ( gte00c varchar(10) not null, --公司编号 gte01c varchar(20) not null, --卡号 gte02c varchar(20) not null, --套餐编号 gte03c varchar(20) not null, --项目编号 gte04f float null, --项目总次数 gte05f float null, --已经使用次数 gte06f float null, --剩余次数 gte08c varchar(20) null, --主项目 gte09i int not null, --序号 gte10d varchar(10) null, --购买日期 primary key clustered(gte00c, gte01c, gte09i) ) insert #gtc04_buf(gte00c, gte01c, gte02c, gte03c, gte04f, gte05f, gte06f, gte08c, gte09i, gte10d) select gte00c, gte01c, gte02c, gte03c, gte04f, gte05f, gte06f, gte08c, gte09i, '' from gtc04 with(nolock) where gte00c = @compid and gte01c = @cardid and isnull(gte06f, 0)>0 -- 剩余次数大于0 and gte08c = gte03c -- 主项目 --把非主项目也加入可以选择 insert #gtc04_buf(gte00c, gte01c, gte02c, gte03c, gte04f, gte05f, gte06f, gte08c, gte09i, gte10d) select a.gte00c, a.gte01c, a.gte02c, a.gte03c, a.gte04f, a.gte05f, 0, a.gte08c, a.gte09i, '' from gtc04 a with(nolock), #gtc04_buf b where a.gte08c<>a.gte03c and a.gte00c = b.gte00c and a.gte01c = b.gte01c and a.gte02c = b.gte02c and a.gte08c = b.gte03c select gte00c, gte01c, gte02c, gte03c, gte04f, gte05f, gte06f, gte08c, gte09i, gte10d, ischeck = 0, gda03c from #gtc04_buf a, gdm01 b where a.gte00c = b.gda00c and a.gte03c = b.gda01c drop table #gtc04_buf end GO /****** Object: Table [dbo].[gxm03] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gxm03]( [gxc00c] [varchar](10) NOT NULL, [gxc01f] [float] NOT NULL, [gxc02f] [float] NOT NULL, [gxc03c] [varchar](2) NOT NULL, [gxc04c] [varchar](2) NULL, [gxc05d] [varchar](8) NULL, [gxc06t] [varchar](8) NULL, [gxc07d] [varchar](8) NULL, [gxc08t] [varchar](8) NULL, [gxc09c] [varchar](20) NULL, [gxc10c] [varchar](1000) NULL, [gxc14c] [varchar](5) NULL, [gxc11c] [nvarchar](20) NULL, [gxc12c] [varchar](10) NULL, [gxc13c] [varchar](512) NULL, CONSTRAINT [PK_GXM03] PRIMARY KEY CLUSTERED ( [gxc00c] ASC, [gxc01f] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_cal_prj_buy_comsume_remain] Script Date: 06/12/2017 06:19:34 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_cal_prj_buy_comsume_remain] ( @compid varchar(10), -- 公司别 @todate varchar(20) -- 截止日期 ) as begin create table #prj_stock ( prjid varchar(20) not null, -- 疗程代码 pricetype int not null, -- 价格序号 discount float not null, -- 折扣 cardnum float null, -- 购买的数量 prjnum float null, -- 疗程的数量 prjamt float null, -- 疗程金额 price float null, -- 单价 primary key(prjid, pricetype, discount) ) create table #prj_stock_buf1 ( type int not null, -- 类别 前期 1 当期 2 prjid varchar(20) not null, -- 疗程代码 pricetype int not null, -- 价格序号 discount float not null, -- 折扣 cardnum float null, -- 购买的数量 prjnum float null, -- 疗程的数量 prjamt float null, -- 疗程金额 price float null, -- 单价 primary key(type, prjid, pricetype, discount) ) create table #prj_consume ( idd int identity not null, prjid varchar(20) not null, -- 疗程代码 pricetype int not null, -- 价格序号 discount float not null, -- 折扣 price float null, -- 单价 prjnum float null, -- 疗程的数量 prjamt float null, -- 疗程金额 primary key(idd) ) -- 这个时间点范围内销售的疗程 insert #prj_stock_buf1(type, prjid, pricetype, discount, cardnum, prjnum, prjamt) select 2, isnull(gch04c, ''), isnull(gch12i, 0), isnull(gch13f, 1), tims = sum(gch14f), num = sum(gch05f + isnull(gch06f, 0)), amt = sum(gch07f) from gcm08 a with (nolock), gam26 z with (nolock) where (gch10c = 'gn' or gch10c = 'gz' or gch10c = 'gx') and gch00c = z.gaz02c and z.gaz01c = @compid and gch02d <= @todate and isnull(gch05f, 0) + isnull(gch06f, 0) > 0 group by isnull(gch04c, ''), isnull(gch12i, 0), isnull(gch13f, 1) order by isnull(gch04c, ''), isnull(gch12i, 0), isnull(gch13f, 1) -- 需要把前期的卡统计进去 insert #prj_stock_buf1(type, prjid, pricetype, discount, cardnum, prjnum, prjamt) select 1, isnull(gcr02c, ''), isnull(gcr17i, 0), isnull(gcr18f, 1), tims = count(*), num = sum(gcr07f), amt = sum(gcr10f) from gcm16 a with (nolock), gam26 z with (nolock) where gcr00c = z.gaz02c and z.gaz01c = @compid and gcr24d <= @todate group by isnull(gcr02c, ''), isnull(gcr17i, 0), isnull(gcr18f, 1) order by isnull(gcr02c, ''), isnull(gcr17i, 0), isnull(gcr18f, 1) insert #prj_stock(prjid, pricetype, discount, cardnum, prjnum, prjamt) select prjid, pricetype, discount, sum(cardnum), sum(prjnum), sum(prjamt) from #prj_stock_buf1 group by prjid, pricetype, discount order by prjid, pricetype, discount drop table #prj_stock_buf1 -- 这个时间范围内消耗的疗程 insert #prj_consume(prjid, pricetype, discount, prjnum, prjamt) select isnull(gch04c, ''), isnull(gch12i, 0), isnull(gch13f, 1), sum(gch08f), sum(gch09f) from gcm08 a with (nolock), gam26 z with (nolock) where gch10c = 'gx' and gch00c = z.gaz02c and z.gaz01c = @compid and gch02d <= @todate group by isnull(gch04c, ''), isnull(gch12i, 0), isnull(gch13f, 1) -- 计算疗程的库存 update a set a.prjnum = a.prjnum - b.prjnum, a.prjamt = a.prjamt - b.prjamt from #prj_stock a, #prj_consume b where a.prjid = b.prjid and a.pricetype = b.pricetype and a.discount = b.discount select prjid, isnull(pricetype, 0), discount, prjnum, prjamt from #prj_stock drop table #prj_stock drop table #prj_consume end GO /****** Object: Table [dbo].[ghm01] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[ghm01]( [gha00c] [varchar](10) NOT NULL, [gha01c] [nvarchar](100) NOT NULL, [gha03d] [varchar](8) NULL, [gha04c] [varchar](4) NULL, [gha05c] [varchar](10) NULL, [gha06c] [varchar](10) NULL, [gha07i] [int] NULL, [gha08c] [varchar](20) NULL, [gha91c] [varchar](10) NULL, [gha92d] [varchar](8) NULL, [gha93c] [varchar](10) NULL, [gha94d] [varchar](8) NULL, [gha09t] [varchar](6) NULL, [gha10i] [int] NULL, [gha11c] [varchar](200) NULL, [gha12c] [varchar](20) NULL, CONSTRAINT [PK_GHM01] PRIMARY KEY CLUSTERED ( [gha00c] ASC, [gha01c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gbm21] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gbm21]( [gbu00c] [varchar](10) NOT NULL, [gbu01c] [varchar](20) NOT NULL, [gbu02c] [varchar](20) NOT NULL, [gbu03f] [float] NULL, [gbu05f] [float] NULL, [gbu06d] [varchar](10) NULL, [gbu07f] [float] NULL, [gbu08c] [varchar](80) NULL, CONSTRAINT [PK_GBM21] PRIMARY KEY CLUSTERED ( [gbu00c] ASC, [gbu01c] ASC, [gbu02c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[sms_fee_control] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[sms_fee_control]( [compid] [varchar](10) NOT NULL, [mmonth] [varchar](10) NOT NULL, [done] [int] NULL, [caldate] [varchar](10) NULL, CONSTRAINT [PK_FEE_CONTROL] PRIMARY KEY CLUSTERED ( [compid] ASC, [mmonth] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[ltm02] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[ltm02]( [gtb00c] [varchar](10) NOT NULL, [gtb01c] [varchar](20) NOT NULL, [gtb02f] [float] NOT NULL, [gtb03c] [varchar](20) NULL, [gtb04f] [float] NULL, [gtb05c] [varchar](20) NULL, [gtb06f] [float] NULL, [gtb07f] [float] NULL, [gtb08f] [float] NULL, [gtb09f] [float] NULL, [gtb10f] [float] NULL, [gtb11f] [float] NULL, [gtb12f] [float] NULL, [gtb13i] [int] NULL, [gtb14f] [float] NULL, CONSTRAINT [pk_ltm02] PRIMARY KEY CLUSTERED ( [gtb00c] ASC, [gtb01c] ASC, [gtb02f] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gicm01] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gicm01]( [gica00c] [varchar](10) NOT NULL, [gica01v] [varchar](20) NOT NULL, [gica02v] [varchar](20) NULL, [gica03v] [varchar](5) NULL, [gica04v] [varchar](20) NULL, [gica05v] [varchar](20) NULL, [gica06v] [varchar](10) NULL, [gica07i] [int] NULL, [gica08i] [int] NULL, [gica09v] [varchar](20) NULL, [gica10v] [varchar](20) NULL, [gica11v] [varchar](20) NULL, [gica12i] [int] NULL, [gica13v] [varchar](20) NULL, [gica14v] [varchar](20) NULL, [gica15v] [varchar](20) NULL, [gica16v] [varchar](20) NULL, [gica17v] [varchar](20) NULL, [gica18v] [varchar](20) NULL, [gica19t] [varchar](6) NOT NULL, [gica20t] [varchar](6) NOT NULL, [gica21d] [varchar](8) NOT NULL, [gica22v] [varchar](20) NULL, [gica23i] [int] NULL, [gica24t] [varchar](6) NULL, [gica25d] [varchar](8) NULL, [gica26v] [varchar](20) NULL, [gica27t] [varchar](6) NULL, [gica28d] [varchar](8) NULL, [gica29f] [float] NULL, [gica30v] [varchar](5) NULL, [gica31v] [varchar](240) NULL, CONSTRAINT [PK_GICM01] PRIMARY KEY CLUSTERED ( [gica00c] ASC, [gica01v] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upd_getStoreChain] Script Date: 06/12/2017 06:19:22 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upd_getStoreChain] ( @compId varchar(10) --公司编号 ) as begin create table #clain_comp ( compId varchar(20) not null ) create table #clain_old_comp ( compId varchar(20) not null ) create table #clain_result ( compId varchar(20) not null, compName varchar(40) null, compLevel int null, compParent varchar(20) not null, ) declare @count int insert into #clain_result(compId, compName, compLevel, compParent) select @compId, b.gae03c, a.lvl, '' from b_perf a, gam05 b where a.bran_id = @compId and a.bran_id = b.gae01c insert into #clain_comp (compId) select @compId select @count = count(1) from #clain_comp while(@count > 0) begin insert into #clain_result(compId, compName, compLevel, compParent) select bran_id, b.gae03c, a.lvl, a.parent_id from b_perf a, gam05 b where a.parent_id in (select compId from #clain_comp) and a.bran_id = b.gae01c delete from #clain_old_comp insert into #clain_old_comp (compId) select compId from #clain_comp delete from #clain_comp insert into #clain_comp (compId) select a.bran_id from b_perf a where a.parent_id in (select compId from #clain_old_comp) select @count = count(1) from #clain_comp end drop table #clain_comp drop table #clain_old_comp select compId, compName, compLevel, compParent from #clain_result drop table #clain_result end GO /****** Object: StoredProcedure [dbo].[upg_cal_prj_buy_comsume_happen] Script Date: 06/12/2017 06:19:33 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_cal_prj_buy_comsume_happen] ( @compid varchar(10), -- 公司别 @fromdate varchar(20), -- 开始日期 @todate varchar(20) -- 截止日期 ) as begin create table #prj_stock ( prjid varchar(20) not null, --疗程代码 pricetype int not null, --价格序号 discount float not null, --折扣 cardnum float null, --购买的数量 prjnum float null, --疗程的数量 prjamt float null, --疗程金额 price float null, --单价 c_prjnum float null, --消耗疗程的数量 c_prjamt float null --消耗疗程金额 ) create clustered index prj_stock_idx_03 on #prj_stock(prjid, pricetype, discount) create table #prj_consume( idd int identity not null, prjid varchar(20) not null, --疗程代码 pricetype int not null, --价格序号 discount float not null, --折扣 prjnum float null, --疗程的数量 prjamt float null, --疗程金额 ) declare @PK_Sql nvarchar(max) set @PK_Sql = ' alter table #prj_consume add constraint PK_#prj_consume_' + convert(varchar(10), cast(floor(rand() * 1000000) as int)) + ' PRIMARY KEY NonCLUSTERED(idd)' exec sp_executesql @PK_Sql create clustered index prj_consume_idx_03 on #prj_consume(prjid, pricetype, discount) -- 这个时间点范围内销售的疗程 insert #prj_stock(prjid, pricetype, discount, cardnum, prjnum, prjamt) select isnull(gch04c, ''), isnull(gch12i, 0), isnull(gch13f, 1), tims = sum(gch14f), num = sum(gch05f + isnull(gch06f, 0)), amt = sum(gch07f) from gcm08 a with (nolock), gam26 z with (nolock) where (gch10c = 'gn' or gch10c = 'gz' or gch10c = 'gx') and gch00c = z.gaz02c and z.gaz01c = @compid and gch02d >= @fromdate and gch02d <= @todate and isnull(gch14f, 0)>0 and isnull(gch05f, 0) + isnull(gch06f, 0)>0 group by isnull(gch04c, ''), isnull(gch12i, 0), isnull(gch13f, 1) order by isnull(gch04c, ''), isnull(gch12i, 0), isnull(gch13f, 1) --这个时间范围内消耗的疗程 insert #prj_consume(prjid, pricetype, discount, prjnum, prjamt) select isnull(gch04c, ''), isnull(gch12i, 0), isnull(gch13f, 1), sum(gch08f), sum(gch09f) from gcm08 a with (nolock), gam26 z with (nolock) where gch10c = 'gx' and gch00c = z.gaz02c and z.gaz01c = @compid and gch02d >= @fromdate and gch02d <= @todate group by isnull(gch04c, ''), isnull(gch12i, 0), isnull(gch13f, 1) --插入一个表中 update a set a.c_prjnum = b.prjnum, a.c_prjamt = b.prjamt from #prj_stock a, #prj_consume b where a.prjid = b.prjid and a.pricetype = b.pricetype and a.discount = b.discount --可能在这个时间范围内没有购买,但有消耗,所以可能没有update到,需要插入 delete a from #prj_consume a, #prj_stock b where a.prjid = b.prjid and a.pricetype = b.pricetype and a.discount = b.discount insert #prj_stock(prjid, pricetype, discount, c_prjnum, c_prjamt) select prjid, isnull(pricetype, 0), discount, prjnum, prjamt from #prj_consume select prjid, pricetype, discount, cardnum, prjnum, prjamt, c_prjnum, c_prjamt from #prj_stock drop table #prj_stock drop table #prj_consume end GO /****** Object: StoredProcedure [dbo].[upg_quick_ownership_confirm] Script Date: 06/12/2017 06:20:30 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_quick_ownership_confirm] ( @currentCompId varchar(10) --公司 ) as begin -- 创建临时表 create table #quick_ownership_confirm_comp( compId varchar(10) not null ) insert into #quick_ownership_confirm_comp (compId) select gsa00c from gsm01 with (nolock) where gsa01c = 'SP143' and gsa03f = 0 and gsa00c <> @currentCompId create table #quick_ownership_confirm_employee( employeeId varchar(10) not null ) insert into #quick_ownership_confirm_employee (employeeId) select haa01c from ham01 with (nolock) where haa00c = @currentCompId and haa00c = ownhaa00c and haa01c = ownhaa01c -- 创建循环变量 declare @HasRecord int set @HasRecord = 1 declare @currentEmployeeId varchar(10) while(@HasRecord = 1) begin if exists ( select 1 from #quick_ownership_confirm_employee) begin set @HasRecord = 1 select top 1 @currentEmployeeId = employeeId from #quick_ownership_confirm_employee update ham01 set ownhaa00c = @currentCompId, ownhaa01c = @currentEmployeeId where haa01c = @currentEmployeeId and haa00c in (select compId from #quick_ownership_confirm_comp) -- 清除已处理的门店 delete #quick_ownership_confirm_employee where employeeId = @currentEmployeeId end else begin set @HasRecord = 0 end end drop table #quick_ownership_confirm_comp drop table #quick_ownership_confirm_employee end GO /****** Object: Table [dbo].[gxm04] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gxm04]( [gxd00c] [varchar](10) NOT NULL, [gxd01f] [float] NOT NULL, [gxd02d] [varchar](8) NOT NULL, [gxd03t] [varchar](8) NULL, [gxd04c] [varchar](20) NULL, [gxd05c] [varchar](20) NULL, [gxd06c] [varchar](1000) NULL, CONSTRAINT [PK_GXM04] PRIMARY KEY CLUSTERED ( [gxd00c] ASC, [gxd01f] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_cal_prj_buy_comsume_report] Script Date: 06/12/2017 06:19:34 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_cal_prj_buy_comsume_report] ( @compid varchar(10), -- 公司别 @fromdate varchar(20), -- 开始日期 @todate varchar(20), -- 截止日期 @fromprj varchar(20), @toprj varchar(20) ) as begin create table #prj_stock ( prjid varchar(20) not null, -- 疗程代码 pricetype int not null, -- 价格序号 discount float not null, -- 折扣 s_prjnum float null, -- 期初疗程的数量 s_prjamt float null, -- 期初疗程金额 cardnum float null, -- 购买的数量 prjnum float null, -- 疗程的数量 prjamt float null, -- 疗程金额 c_prjnum float null, -- 消耗疗程的数量 c_prjamt float null, -- 消耗疗程金额 e_prjnum float null, -- 期末疗程的数量 e_prjamt float null, -- 期末疗程金额 timeprice float null, -- 单次的金额 cardprice float null, -- 单卡的金额 primary key(prjid, pricetype, discount) ) create table #prj_stock_buf ( prjid varchar(20) not null, -- 疗程代码 pricetype int not null, -- 价格序号 discount float not null, -- 折扣 s_prjnum float null, -- 期初疗程的数量 s_prjamt float null, -- 期初疗程金额 cardnum float null, -- 购买的数量 prjnum float null, -- 疗程的数量 prjamt float null, -- 疗程金额 c_prjnum float null, -- 消耗疗程的数量 c_prjamt float null, -- 消耗疗程金额 e_prjnum float null, -- 期末疗程的数量 e_prjamt float null -- 期末疗程金额 ) create clustered index prj_stock_buf_idx_03 on #prj_stock_buf(prjid, pricetype, discount) -- 先计算期末,应该疗程最多 insert #prj_stock(prjid, pricetype, discount, e_prjnum, e_prjamt) exec upg_cal_prj_buy_comsume_remain @compid, @todate declare @strtmp varchar(20) declare @start_date varchar(20) set @strtmp = convert(varchar(10), dateadd(day, -1, @fromdate), 120) set @start_date = substring(@strtmp, 1, 4) + substring(@strtmp, 6, 2) + substring(@strtmp, 9, 2) insert #prj_stock_buf(prjid, pricetype, discount, s_prjnum, s_prjamt) exec upg_cal_prj_buy_comsume_remain @compid, @start_date update a set a.s_prjnum = b.s_prjnum, a.s_prjamt = b.s_prjamt from #prj_stock a, #prj_stock_buf b where a.prjid = b.prjid and a.pricetype = b.pricetype and a.discount = b.discount delete #prj_stock_buf insert #prj_stock_buf(prjid, pricetype, discount, cardnum, prjnum, prjamt, c_prjnum, c_prjamt) exec upg_cal_prj_buy_comsume_happen @compid, @fromdate, @todate update a set a.cardnum = b.cardnum, a.prjnum = b.prjnum, a.prjamt = b.prjamt, a.c_prjnum = b.c_prjnum, a.c_prjamt = b.c_prjamt from #prj_stock a, #prj_stock_buf b where a.prjid = b.prjid and a.pricetype = b.pricetype and a.discount = b.discount --计算单次消费的金额 --计算单卡的金额 update #prj_stock set cardprice = prjamt/isnull(cardnum, 1) update #prj_stock set timeprice = prjamt/isnull(prjnum, 1) if @fromprj = '*' or @fromprj = '' begin select isnull(prjid,'') prjid, isnull(pricetype,0) pricetype,isnull(discount,0) discount,isnull(s_prjnum,0) s_prjnum, isnull(s_prjamt,0) s_prjamt,isnull(cardnum,0) cardnum,isnull(prjnum,0) prjnum,isnull(prjamt,0) prjamt, isnull(c_prjnum,0) c_prjnum,isnull(c_prjamt,0) c_prjamt,isnull(e_prjnum,0) e_prjnum,isnull(e_prjamt,0) e_prjamt, isnull(timeprice,0) timeprice,isnull(cardprice,0) cardprice from #prj_stock end else begin select isnull(prjid,'') prjid, isnull(pricetype,0) pricetype,isnull(discount,0) discount,isnull(s_prjnum,0) s_prjnum, isnull(s_prjamt,0) s_prjamt,isnull(cardnum,0) cardnum,isnull(prjnum,0) prjnum,isnull(prjamt,0) prjamt, isnull(c_prjnum,0) c_prjnum,isnull(c_prjamt,0) c_prjamt,isnull(e_prjnum,0) e_prjnum,isnull(e_prjamt,0) e_prjamt, isnull(timeprice,0) timeprice,isnull(cardprice,0) cardprice from #prj_stock where prjid >= @fromprj and prjid <= @toprj end drop table #prj_stock_buf drop table #prj_stock end GO /****** Object: Table [dbo].[gcm01] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gcm01]( [gca00c] [varchar](10) NOT NULL, [gca01c] [varchar](20) NOT NULL, [gca02c] [varchar](5) NOT NULL, [gca03i] [int] NULL, [gca04c] [varchar](20) NULL, [gca05d] [varchar](8) NULL, [gca06d] [varchar](8) NULL, [gca07d] [varchar](8) NULL, [gca08i] [int] NULL, [gca09c] [varchar](5) NULL, [gca10f] [float] NULL, [gca11c] [varchar](20) NULL, [gca12c] [varchar](20) NULL, [gca13d] [varchar](10) NULL, [gca14i] [int] NULL, [gca15i] [int] NULL, [gca16d] [varchar](8) NULL, [gca17d] [varchar](8) NULL, [gca18c] [varchar](20) NULL, [gca19f] [float] NULL, [gca20c] [varchar](10) NULL, [gca21d] [varchar](8) NULL, [gca22d] [varchar](8) NULL, [gca23i] [int] NULL, [gca24f] [float] NULL, [gca25f] [float] NULL, [gca26f] [float] NULL, [gca27c] [varchar](3000) NULL, [gca28i] [int] NULL, [gca29f] [float] NULL, [gca30d] [nvarchar](14) NULL, [gca99c] [nvarchar](50) NULL, [gca30c] [varchar](20) NULL, [gca31c] [varchar](30) NULL, CONSTRAINT [PK_GCM01] PRIMARY KEY CLUSTERED ( [gca01c] ASC, [gca00c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_stat_acct_history] Script Date: 06/12/2017 06:20:42 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_stat_acct_history] ( @compid varchar(10), @fromcard varchar(20), @tocard varchar(20), @fromdate varchar(20), @todate varchar(20), @flag int -- 1-一个账户一个余额 2-详细的每条纪录 ) as begin declare @incompid varchar(10) select @incompid = @compid declare @compcode varchar(20) if @compid = '001' or @compid = '*' or @compid = '%' begin select @compcode = '%' select @incompid = '001' end else begin select @compcode = @compid end if @fromdate = '*' or @fromdate = '' begin select @fromdate = '19900101' select @todate = '20901231' end create table #tbl_history ( seq int identity not null, cardid varchar(20) not null, nname varchar(40) null, acctno varchar(20) null, ddate varchar(10) null, mode int null, lastamt float null, amt float null, billtype varchar(20) null, billid varchar(20) null, remain float null, total_fill float null, total_consume float null, primary key(seq) ) create table #tbl_stat ( cardid varchar(20) not null, acctno varchar(20) not null, amt float null ) if @fromcard = '*' begin insert #tbl_history(cardid, nname, acctno, ddate, mode, lastamt, amt, billtype, billid) select gcd01c, '', gcd02i, gcd09d, gcd05i, gcd10f, gcd06f, gcd07c, gcd08c from gcm04 a with (nolock), gcm01 b with (nolock), gam26 z with(nolock) where a.gcd00c = z.gaz02c and z.gaz01c = @incompid and a.gcd00c = b.gca00c and a.gcd01c = b.gca01c and a.gcd09d >= @fromdate and a.gcd09d <= @todate order by gcd01c, gcd02i, gcd04f end else begin insert #tbl_history(cardid, nname, acctno, ddate, mode, lastamt, amt, billtype, billid) select gcd01c, '', gcd02i, gcd09d, gcd05i, gcd10f, gcd06f, gcd07c, gcd08c from gcm04 a with (nolock), gcm01 b with (nolock), gam26 z with(nolock) where a.gcd00c = z.gaz02c and z.gaz01c = @incompid and a.gcd00c = b.gca00c and a.gcd01c = b.gca01c and a.gcd09d >= @fromdate and a.gcd09d <= @todate and a.gcd01c >= @fromcard and a.gcd01c <= @tocard and a.gcd01c <> '' order by gcd01c, gcd02i, gcd04f end delete #tbl_history where isnull(amt, 0) = 0 if @fromcard = '*' or @fromcard = '' begin insert #tbl_history(cardid, nname, acctno, ddate, mode, lastamt, remain, billtype, billid) select gcc01c, '', gcc03i, '', 0, 0, gcc06f, '', '' from gcm03 a with (nolock), gcm01 b with (nolock) where gcc00c = gca13d and gca00c = gca13d and gcc01c = gca01c and gcc01c <> '' end else begin insert #tbl_history(cardid, nname, acctno, ddate, mode, lastamt, remain, billtype, billid) select gcc01c, '', gcc03i, '', 0, 0, gcc06f, '', '' from gcm03 a with (nolock), gcm01 b with (nolock) where gcc00c = gca13d and gca00c = gca13d and gcc01c = gca01c and gca01c >= @fromcard and gca01c <= @tocard and gca01c <> '' end -- 统计累计充值以及累计消费 insert #tbl_stat(cardid, acctno, amt) select cardid, acctno, sum(isnull(amt, 0)) from #tbl_history where (mode = 0 or mode = 6) and isnull(ddate, '') <> '' group by cardid, acctno update a set a.total_fill = isnull(b.amt, 0) from #tbl_history a, #tbl_stat b where a.cardid = b.cardid and a.acctno = b.acctno and isnull(ddate, '') = '' delete #tbl_stat insert #tbl_stat(cardid, acctno, amt) select cardid, acctno, sum(isnull(amt, 0)) from #tbl_history where (mode = 5 or mode = 1) and isnull(ddate, '') <> '' group by cardid, acctno update a set a.total_fill = isnull(a.total_fill, 0) - isnull(b.amt, 0) from #tbl_history a, #tbl_stat b where a.cardid = b.cardid and a.acctno = b.acctno and isnull(ddate, '') = '' delete #tbl_stat insert #tbl_stat(cardid, acctno, amt) select cardid, acctno, sum(isnull(amt, 0)) from #tbl_history where mode = 2 and isnull(ddate, '') <> '' group by cardid, acctno update a set a.total_consume = isnull(b.amt, 0) from #tbl_history a, #tbl_stat b where a.cardid = b.cardid and a.acctno = b.acctno and isnull(ddate, '') = '' drop table #tbl_stat update a set a.nname = b.gba03c from #tbl_history a, gbm01 b with(nolock) where a.cardid = b.gba23c and a.ddate = '' update #tbl_history set billtype = '收银单' where billtype = 'gx' update #tbl_history set billtype = '账户异动' where billtype = 'gz' update #tbl_history set billtype = '卡销售' where billtype = 'gn' update #tbl_history set billtype = '卡异动' where billtype = 'ge' if @flag = 2 -- 每个账户的详细变化历史,也包括一条汇总的纪录 select seq,cardid, isnull(nname,'') nname, isnull(acctno,'') acctno, isnull(ddate,'') ddate, isnull(mode,'') mode, isnull(lastamt,0) lastamt, isnull(amt,0) amt, isnull(billtype,'') billtype, isnull(billid,'') billid, isnull(remain,0) remain, isnull(total_fill,0) total_fill, isnull(total_consume,0) total_consume from #tbl_history order by cardid, acctno, ddate else -- 每个账户一个余额 select seq,cardid, isnull(nname,'') nname, isnull(acctno,'') acctno, isnull(ddate,'') ddate, isnull(mode,'') mode, isnull(lastamt,0) lastamt, isnull(amt,0) amt, isnull(billtype,'') billtype, isnull(billid,'') billid, isnull(remain,0) remain, isnull(total_fill,0) total_fill, isnull(total_consume,0) total_consume from #tbl_history where isnull(ddate, '') = '' order by cardid, acctno, ddate drop table #tbl_history end GO /****** Object: Table [dbo].[accountspendlimit] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[accountspendlimit]( [gal00c] [varchar](10) NOT NULL, [gal01c] [varchar](20) NOT NULL, [gal02f] [float] NULL, [gal03c] [varchar](100) NULL, [gal04c] [varchar](100) NULL, [gal05c] [varchar](100) NULL, [gal06c] [varchar](100) NULL, [gal07c] [varchar](100) NULL, CONSTRAINT [PK_ACCOUNTSPENDLIMIT] PRIMARY KEY CLUSTERED ( [gal00c] ASC, [gal01c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gicm02] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gicm02]( [gicb00c] [varchar](10) NOT NULL, [gicb01v] [varchar](20) NOT NULL, [gicb02v] [varchar](20) NULL, [gicb03i] [int] NULL, [gicb04i] [int] NULL, [gicb05i] [int] NULL, [gicb06v] [varchar](20) NULL, [gicb07v] [varchar](20) NULL, [gicb08f] [float] NULL, [gicb09v] [varchar](20) NULL, [gicb10t] [varchar](6) NULL, [gicb11d] [varchar](8) NULL, [gicb12v] [varchar](100) NULL, [gicb13c] [varchar](20) NULL, CONSTRAINT [PK_GICM02] PRIMARY KEY CLUSTERED ( [gicb00c] ASC, [gicb01v] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_foregift_report] Script Date: 06/12/2017 06:20:03 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_foregift_report] ( @compid varchar(10), @fromdate varchar(8), @todate varchar(8), @frommember varchar(20), @tomember varchar(20), @emp varchar(20), --保证金收取人 add by leiji on2012年10月31日 @paymode int ) -- 0 所有 1 -现金 2-银行卡 as begin declare @pay varchar(10) if @paymode = 0 select @pay = '*' else if @paymode = 1 select @pay = '1' else if @paymode = 2 select @pay = '6' -- create table #result -- ( -- idd int identity not null, -- 序号 -- seq int null, -- compid varchar(10) not null, -- memid varchar(20) not null, -- membername varchar(40) null, -- billno varchar(20) null, -- 收据编号 -- foregift float null, -- 保证金 -- foredate varchar(8) null, -- changeamt float null, -- 取用保证金 -- getamt float null, -- 退押金 -- gettype varchar(20) null, -- getddate varchar(8) null, -- remain float null, -- flag int null, -- paymode varchar(10) null, -- primary key(idd) -- ) -- -- create table #tbl_gbm17 -- ( -- memid varchar(20) not null, -- seq int not null, -- getamt float null, -- primary key(memid, seq) -- ) -- -- insert into #result(seq, compid, memid, membername, billno, foregift, foredate, changeamt, getamt, gettype, getddate, remain, flag, paymode) -- select gbl02f, gbl00c, gbl01c, gba03c, gbl14c, gbl04f, gbl05d, 0, 0, '', gbl08d, gbl11f, 0, gbl15c -- from gbm15 a with (nolock), gbm01 b with (nolock), gam26 z with (nolock) -- where gbl00c = z.gaz02c -- and z.gaz01c = @compid -- and (gbl06c = @emp or @emp = '*') -- and (gbl01c between @frommember and @tomember or @frommember = '*') -- and (gbl05d between @fromdate and @todate or gbl08d between @fromdate and @todate) -- and gbl00c = gba00c -- and gbl01c = gba01c -- and (@pay = '*' or (gbl15c = @pay)) -- -- --直接退得押金 -- insert #tbl_gbm17(memid, seq, getamt) -- select gbw01c, gbw02f, sum(gbw05f) -- from gbm17 a with (nolock), gbm15 b with (nolock) -- where a.gbw01c = b.gbl01c -- and a.gbw02f = b.gbl02f -- and (gbl01c between @frommember and @tomember or @frommember = '*') -- and (gbl05d between @fromdate and @todate or gbl08d between @fromdate and @todate) -- and gbw04c = 'BACK' -- group by gbw01c, gbw02f -- -- update a -- set a.getamt = b.getamt -- from #result a, #tbl_gbm17 b -- where a.seq = b.seq -- and a.memid = b.memid -- -- --取用的押金 -- delete #tbl_gbm17 -- -- insert #tbl_gbm17(memid, seq, getamt) -- select gbw01c, gbw02f, sum(gbw05f) -- from gbm17 a with (nolock), gbm15 b with (nolock) -- where a.gbw01c = b.gbl01c -- and a.gbw02f = b.gbl02f -- and (gbl01c between @frommember and @tomember or @frommember = '*') -- and (gbl05d between @fromdate and @todate or gbl08d between @fromdate and @todate) -- and gbw04c in ('gn', 'gz', 'gx') -- group by gbw01c, gbw02f -- -- update a -- set a.changeamt = b.getamt -- from #result a, #tbl_gbm17 b -- where a.seq = b.seq -- and a.memid = b.memid -- -- drop table #tbl_gbm17 -- -- insert into #result(compid, memid, membername, billno, foregift, foredate, changeamt, getamt, getddate, remain, flag, paymode) -- select compid, memid, membername, billno, sum(isnull(foregift, 0)), foredate, sum(isnull(changeamt, 0)), sum(isnull(getamt, 0)), getddate, sum(isnull(remain, 0)), 1, paymode -- from #result -- group by compid, memid, billno, foredate, getddate, membername, paymode -- -- delete #result where flag = 0 -- -- update a set a.gettype = '转销售' -- from #result a -- where isnull(changeamt, 0)>0 and isnull(getamt, 0) = 0 -- -- update a set a.gettype = '退回' -- from #result a -- where isnull(changeamt, 0) = 0 and isnull(getamt, 0)>0 -- -- update a set a.gettype = '转销售 + 退回' -- from #result a -- where isnull(changeamt, 0)>0 and isnull(getamt, 0)>0 -- -- select -- isnull(idd,'') idd, -- isnull(seq,'') seq, -- isnull(compid,'') compid, -- isnull(memid,'') memid, -- isnull(membername,'') membername, -- isnull(billno,'') billno, -- isnull(foregift,'') foregift, -- isnull(foredate,'') foredate, -- isnull(changeamt,'') changeamt, -- isnull(getamt,'') getamt, -- isnull(gettype,'') gettype, -- isnull(getddate,'') getddate, -- isnull(remain,'') remain, -- isnull(flag,'') flag, -- isnull(paymode,'') paymode -- from #result create table #result ( bz int, memid varchar(20) not null, idd varchar(20) not null, -- 押金序号 total float null ) insert #result(bz,memid,idd,total) --保证金 select 1 bz,gbl01c,gbl02f,sum(gbl04f) from gbm15 a with (nolock), gbm01 b with (nolock), gam26 z with (nolock) where gbl00c = z.gaz02c and z.gaz01c = @compid and (gbl06c = @emp or @emp = '*') and (gbl01c between @frommember and @tomember or @frommember = '*') and (gbl05d between @fromdate and @todate ) and gbl00c = gba00c and gbl01c = gba01c and (@pay = '*' or (gbl15c = @pay)) group by gbl01c,gbl02f union all --剩余保证金 select 2 bz,gbl01c,gbl02f,sum(gbl11f) from gbm15 a with (nolock), gbm01 b with (nolock), gam26 z with (nolock) where gbl00c = z.gaz02c and z.gaz01c = @compid and (gbl06c = @emp or @emp = '*') and (gbl01c between @frommember and @tomember or @frommember = '*') and (gbl05d between @fromdate and @todate ) and gbl00c = gba00c and gbl01c = gba01c and (@pay = '*' or (gbl15c = @pay)) group by gbl01c,gbl02f union all --转销售 select 3 bz,gbw01c, gbw02f, sum(gbw05f) from gbm17 a with (nolock), gbm15 b with (nolock),gam26 z with (nolock) where gbw00c = z.gaz02c and z.gaz01c = @compid and a.gbw01c = b.gbl01c and a.gbw02f = b.gbl02f and (gbl01c between @frommember and @tomember or @frommember = '*') and (gbw06d between @fromdate and @todate) and gbw04c in ('gn', 'gz', 'gx') group by gbw01c, gbw02f union all --退押金 select 4 bz,gbw01c, gbw02f, sum(gbw05f) from gbm17 a with (nolock), gbm15 b with (nolock),gam26 z with (nolock) where gbw00c = z.gaz02c and z.gaz01c = @compid and a.gbw01c = b.gbl01c and a.gbw02f = b.gbl02f and (gbl01c between @frommember and @tomember or @frommember = '*') and (gbw06d between @fromdate and @todate) and gbw04c = 'BACK' group by gbw01c, gbw02f select sum(case when bz = 1 then total else 0 end) foregift, sum(case when bz = 2 then total else 0 end) remain, sum(case when bz = 3 then total else 0 end) changeamt, sum(case when bz = 4 then total else 0 end) getamt from #result drop table #result end GO /****** Object: StoredProcedure [dbo].[upg_query_gmb18i] Script Date: 06/12/2017 06:20:30 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create proc [dbo].[upg_query_gmb18i] ( @companyId varchar(30), --公司编号 @xmdl varchar(30), --项目大类(部门) @tjfl varchar(500), --项目分类(按统计分类) @fzren varchar(30), --负责人 @fwren varchar(30), --服务人 @fromjuli int, --多久没来 @tojuli int, --多久没来 @fromcs int, --来店次数 @tocs int, --来店次数 @fromjine float, --平均消费金额 @tojine float, --平均消费金额 @kfdj varchar(60), --客户等级 @kfqz varchar(60), --客户发展潜质 @frommember varchar(30), --会员编号 @tomember varchar(30), --会员编号 @fromdate varchar(10), --消费时间 @todate varchar(10), --消费时间 @fromxfnl float, --消费能力 @toxfnl float, --消费能力 @fromlcsale float, --购买能力(疗程销售) @tolcsale float, --购买能力(疗程销售) @frommaxjine float, --单笔消费 @tomaxjine float, --单笔消费 @frompinlu float, --来店频率(消费频率) @topinlu float, --来店频率(消费频率) @chksex int, -- -1 全部,0 女,1 男 @chkFun int, -- -1 全部,0 未婚,1 已婚 @chkDj int, -- -1 未选择,0 选择 @chkFZren int, -- -1 未选择,0 选择 @chkNolc int -- -1 未选择,0 选择 ) as begin create table #tb_query_gmb18i ( chk int, gba00c varchar(10), -- 公司编号 gba01c varchar(30), -- 会员编号 gba03c varchar(60), -- 会员姓名 gba14iName varchar(10), -- 性别 gba17d varchar(30), -- 出生日期 gba39iName varchar(10), -- 婚 gna26f float, -- 办卡(卡销售) gcl07f float, -- 卡充值金额 lcxfnl float, -- 疗程消费金额(消费能力) lcsale float, -- 购买疗程(办卡) lccs float, -- 疗程总次数 lczs float, -- 疗程赠送次数 lcxhcs float, -- 疗程消耗次数 lcxhjine float, -- 疗程消耗金额 lcfromtime varchar(10), -- 疗程第一次消费日期 lctotime varchar(10), -- 疗程最后一次消费日期 jine float, -- 消费总金额 maxjine float, -- 最大单笔消费金额 juli float, -- 多久没来 cishu float, -- 消费总次数 pinlu float, -- 消费频率 pjxh float, -- 平均消费金额 ggc11f float, --外卖品 startime varchar(30), --第一次消费时间 endtime varchar(30), --最近消费时间 gba08c varchar(30), -- Mobile No. gca01c varchar(30), --会员卡号 gca06d varchar(30), --开卡日期 gca06dName varchar(30), --开卡年限 gbr02c varchar(30), --所属部门1 gbr02cName varchar(30), --部门名称1 gbr03c varchar(30), --负责人1 gbr03cName varchar(30), --负责人名称1 gbr04i int, gbr04iName varchar(60), --会员等级1 gbr05i int, gbr05iName varchar(60), --发展潜质1 gbr02c1 varchar(30), --所属部门2 gbr02cName1 varchar(30), --部门名称2 gbr03c1 varchar(30), --负责人2 gbr03cName1 varchar(30), --负责人名称2 gbr04i1 int, gbr04iName1 varchar(60), --会员等级2 gbr05i1 int, gbr05iName1 varchar(60), --发展潜质2 gbr02c2 varchar(30), --所属部门3 gbr02cName2 varchar(30), --部门名称3 gbr03c2 varchar(30), --负责人3 gbr03cName2 varchar(30), --负责人名称3 gbr04i2 int, gbr04iName2 varchar(60), --会员等级3 gbr05i2 int, gbr05iName2 varchar(60), --发展潜质3 gbr02c3 varchar(30), --所属部门4 gbr02cName3 varchar(30), --部门名称4 gbr03c3 varchar(30), --负责人4 gbr03cName3 varchar(30), --负责人名称4 gbr04i3 int, gbr04iName3 varchar(60), --会员等级4 gbr05i3 int, gbr05iName3 varchar(60), --发展潜质4 gba41d varchar(30), -- 会员创建时间 gba25c varchar(60), -- 居住区域 gba05c varchar(300) -- 联络地址 ) create nonclustered index tb_query_gmb18i_idx_01 ON #tb_query_gmb18i(gba00c,gba01c) insert into #tb_query_gmb18i select 0 chk, gba00c, -- 公司编号 gba01c, -- 会员编号 gba03c, -- 会员姓名 case gba14i when 0 then '女' when 1 then '男' else '未知' end gba14iName, -- 性别 gba17d, -- 出生日期 case gba39i when 0 then '未婚' when 1 then '已婚' else '不详' end gba39iName, -- 婚 gna26f, --办卡(卡销售) gcl07f, --卡充值金额 lcxfnl, --疗程消费 0 as lcsale, 0 as lccs, 0 as lczs, lcxhcs, --疗程消耗次数 lcxhjine, --疗程消耗金额 lcfromtime, --疗程第一次消费日期 lctotime, --疗程最后一次消费日期 convert(decimal(19,2),jine) jine, --其他消费金额 maxjine, --最大单笔消费金额 juli, -- 多久没来 1 cishu, -- 消费总次数 1 pinlu, --消费频率 --convert(decimal(19,2),pjxh) pjxh, -- 平均消费金额(不准确) 0 pjxh, ggc11f, --外卖品 startime, --第一次消费时间 endtime, --最近消费时间 gba08c, -- Mobile No. gca01c, --会员卡号 gca06d, --开卡日期 case isdate(gca06d) when 1 then - datediff(yy,getdate(),convert(datetime,gca06d)) else 0 end gca06dName, --开卡年限 '' gbr02c, --所属部门1 '' gbr02cName, --部门名称1 '' gbr03c, --负责人1 '' gbr03cName, --负责人名称1 -1 gbr04i, '' gbr04iName, --会员等级1 -1 gbr05i, '' gbr05iName, --发展潜质1 '' gbr02c1, --所属部门2 '' gbr02cName1, --部门名称2 '' gbr03c1, --负责人2 '' gbr03cName1, --负责人名称2 -1 gbr04i1, '' gbr04iName1, --会员等级2 -1 gbr05i1, '' gbr05iName1, --发展潜质2 '' gbr02c2, --所属部门3 '' gbr02cName2, --部门名称3 '' gbr03c2, --负责人3 '' gbr03cName2, --负责人名称3 -1 gbr04i2, '' gbr04iName2, --会员等级3 -1 gbr05i2, '' gbr05iName2, --发展潜质3 '' gbr02c3, --所属部门4 '' gbr02cName3, --部门名称4 '' gbr03c3, --负责人4 '' gbr03cName3, --负责人名称4 -1 gbr04i3, '' gbr04iName3, --会员等级4 -1 gbr05i3, '' gbr05iName3, --发展潜质4 gba41d, -- 会员创建时间 gba25c, -- 居住区域 gba05c -- 联络地址 from gbm01 with(nolock) --会员基本资料主档 left join ( select ggb00c compyId, gga05c membid, -- 会员代码, sum(ggb11f) jine, -- 消费总金额 max(ggb11f) maxjine, --最大单笔消费金额 min(gga94d) startime, -- 开始消费日期 max(gga94d) endtime, -- 最后消费日期 -datediff(dd,getdate(),convert(datetime,max(gga94d))) juli --多久没来店 from ggm02 with(nolock) inner join ggm01 with(nolock) on ggb00c = gga00c and ggb01c = gga01c inner join gdm01 with(nolock) on gdm01.gda00c = ggm02.ggb00c And gdm01.gda01c = ggm02.ggb03c where (@fromdate = '' or (gga94d between @fromdate and @todate)) and (@xmdl = '' or gda02c = @xmdl) --(项目大类)部门 and (@tjfl = '' or gda13c in (select F1 from f_splitstr(@tjfl,',')) ) --统计分类 and (@frommember = '' or (gga05c between @frommember and @tomember) ) --会员编号 group by ggb00c, gga05c ) aTable on gba00c = compyId and gba01c = membid left join gcm01 with(nolock) --会员卡 on gba00c = gca00c and gba23c = gca01c left join ( select gch00c, -- 门店编号 gch03c, -- 卡号 sum(isnull(gch08f,0)) lcxhcs, --消耗次数 sum(isnull(gch09f,0)) lcxhjine, --消耗金额 min(gch02d) lcfromtime, -- 第一次疗程消费日期 max(gch02d) lctotime, -- 最后一次疗程消费日期 sum(isnull(gch18f,0)+isnull(gch20f,0)+isnull(gch22f,0)) lcxfnl --疗程消费金额 from gcm08 with(nolock) inner join gdm01 with(nolock) on gch00c = gda00c and gch04c = gda01c where gch10c = 'gx' --疗程消费 and (@xmdl = '' or gda02c = @xmdl) --(项目大类)部门 and (@tjfl = '' or gda13c in (select F1 from f_splitstr(@tjfl,','))) --统计分类 and (@fromdate = '' or (gch02d between @fromdate and @todate)) --and (@frommember = '' or (gch03c between @frommember and @tomember)) --会员编号 group by gch00c,gch03c ) fTable on gch00c = compyId and gch03c = gca01c left join ( select gcl00c, --公司编号 gca04c member, --会员编号 sum(gcl65f) gcl07f --金额 from gcm10 with(nolock) left join gcm01 with(nolock) on gcl00c = gca00c and gcl03c = gca01c where gcl06i = 0 and gcl04c = '2' --仅包含储值账户 and (@fromdate = '' or (gcl02d between @fromdate and @todate)) and (@frommember = '' or (gca04c between @frommember and @tomember)) --会员编号 group by gcl00c,gca04c ) cTable --卡充值 on gba00c = gcl00c and gba01c = member left join ( select ggc00c, gga05c, --会员代码 sum(ggc11f) ggc11f from ggm03 with(nolock) left join ggm01 with(nolock) on gga00c = ggc00c and gga01c = ggc01c where (@fromdate = '' or (gga94d between @fromdate and @todate)) and (@frommember = '' or (gga05c between @frommember and @tomember)) --会员编号 group by ggc00c,gga05c ) dTable --外卖品 on gba00c = ggc00c and gba01c = gga05c left join ( select gna00c, gna04c, sum(gna26f) gna26f from gnm01 with(nolock) where (@fromdate = '' or (gna02d between @fromdate and @todate)) and (@frommember = '' or (gna04c between @frommember and @tomember)) --会员编号 group by gna00c,gna04c ) eTable --卡销售(办卡) on gba00c = gna00c and gba01c = gna04c where gba00c in (select gaz02c from gam26 where gaz01c = @companyId ) --公司编号 and ( @frommember = '' or (gba01c between @frommember and @tomember) ) --会员编号 and ( @chksex = -1 or gba14i = @chksex ) --性别 and ( @chkFun = -1 or gba39i = @chkFun ) --婚否 and ( @fromjuli = -1 or isnull(juli,0) between @fromjuli and @tojuli ) --距离多久没来 and ( @frommaxjine = -1 or isnull(maxjine,0) between @frommaxjine and @tomaxjine) --单笔消费金额 and ( @fromxfnl = -1 or isnull(lcxfnl,0) between @fromxfnl and @toxfnl) --疗程消费能力 and ( @fwren = '' or gba01c in( --服务员工 select gga05c from ggm01 left join ggm02 on ggm01.gga00c = ggm02.ggb00c and ggm01.gga01c = ggm02.ggb01c where ggb12c = @fwren --大工 or ggb13c = @fwren --中工 or ggb14c = @fwren --小工 or ggb26c = @fwren ) --四工 ) -- delete #tb_query_gmb18i where juli is null --来店次数 declare @compid_cs int update a set a.cishu = cs from #tb_query_gmb18i a, (select gga00c, gga05c, count(*) cs from(select gga00c, gga05c, gga02d from ggm01 with(nolock) where gga00c = @companyId and (@frommember = '' or (gga05c between @frommember and @tomember)) and (@fromdate = '' or (gga02d between @fromdate and @todate)) group by gga00c,gga05c,gga02d ) as a_table group by gga00c,gga05c) as b where a.gba00c = b.gga00c and a.gba01c = gga05c -- 疗程销售 update a set a.lcsale = isnull(a.lcsale,0) + isnull(b.jine,0) from #tb_query_gmb18i a,( select gna00c, gna04c, -- 会员编号 sum(isnull(gnc05f,0)) jine --金额 from gnm03 with(nolock) inner join gnm01 with(nolock) on gna00c = gnc00c and gna01c = gnc01c where gnc00c in (select gaz02c from gam26 where gaz01c = @companyId) and (@fromdate = '' or ( gna02d between @fromdate and @todate)) and (@frommember = '' or (gna04c between @frommember and @tomember)) --会员编号 and gnc02c in (select gda01c from gdm01 where gda00c in (select gaz02c from gam26 where gaz01c = @companyId) and (@xmdl = '' or gda02c = @xmdl) --(项目大类)部门 and (@tjfl = '' or gda13c in (select F1 from f_splitstr(@tjfl,',')) ) --统计分类 ) group by gna00c,gna04c ) b where a.gba00c = b.gna00c and a.gba01c = b.gna04c --疗程销售 update a set a.lcsale = isnull(a.lcsale,0) + isnull(b.jine,0) from #tb_query_gmb18i a, ( select gcl00c, gcl03c, sum(isnull(gcm05f,0)) jine --金额 from gcm11 with(nolock) inner join gcm10 with(nolock) on gcl00c = gcm00c and gcl01c = gcm01c where gcm00c in (select gaz02c from gam26 where gaz01c = @companyId) and gcl06i = 0 and (@fromdate = '' or (gcl02d between @fromdate and @todate)) --and (@frommember = '' or (gcl03c between @frommember and @tomember)) and gcm02c in (select gda01c from gdm01 where gda00c in (select gaz02c from gam26 where gaz01c = @companyId) and (@xmdl = '' or gda02c = @xmdl) --(项目大类)部门 and (@tjfl = '' or gda13c in (select F1 from f_splitstr(@tjfl,',')) ) --统计分类 ) group by gcl00c,gcl03c ) b where a.gba00c = b.gcl00c and a.gca01c = b.gcl03c --疗程次数 update a set a.lccs = b.lccs, a.lczs = b.lczs from #tb_query_gmb18i a,( select gch00c, -- 门店编号 gch03c, -- 卡号 sum(isnull(gch05f,0))+ sum(isnull(gch06f,0)) lccs, --总次数 sum(isnull(gch06f,0)) lczs --赠送次数 from gcm08 with(nolock) inner join gdm01 with(nolock) on gch00c = gda00c and gch04c = gda01c where gch10c in ('gn','gz') --疗程消费 and (@xmdl = '' or gda02c = @xmdl) --(项目大类)部门 and (@tjfl = '' or gda13c in (select F1 from f_splitstr(@tjfl,','))) --统计分类 and (@fromdate = '' or (gch02d between @fromdate and @todate)) group by gch00c,gch03c ) b where a.gba00c = b.gch00c and a.gca01c = b.gch03c --平均消费 update #tb_query_gmb18i set pjxh = case cishu when 0 then 0 else convert(decimal(19,2),jine /cishu) end --消费频率 update #tb_query_gmb18i set pinlu = CEILING(round(convert(decimal(19,2), datediff(dd,startime,endtime))/cishu,1)) where datediff(dd,startime,endtime) > 0 declare @gbr02c varchar(30) -- 所属部门 declare @intSN int set @intSN = 1 declare gbm18_cursor cursor for select gbr02c from gbm18 where gbr00c in (select gaz02c from gam26 where gaz01c = @companyId ) and gbr01c in (select gba01c from #tb_query_gmb18i) group by gbr02c open gbm18_cursor fetch next from gbm18_cursor into @gbr02c while(@@fetch_status = 0) begin if(@intSN = 1) begin update a set gbr02c = b.gbr02c, gbr03c = b.gbr03c, gbr04i = b.gbr04i, gbr05i = b.gbr05i, gbr02cName = (select gsb03c from gsm02 where gsb00c = b.gbr00c and gsb01c = 'U' and gsb02c = b.gbr02c), gbr03cName = (select haa02c from ham01 where haa00c = b.gbr00c and haa01c = b.gbr03c), --负责人名称 gbr04iName = (select gsb03c from gsm02 where gsb00c = b.gbr00c and gsb01c = 'DJ' and gsb02c = b.gbr04i), --会员等级 gbr05iName = (select gsb03c from gsm02 where gsb00c = b.gbr00c and gsb01c = 'QZ' and gsb02c = b.gbr05i) --发展潜质 from #tb_query_gmb18i a, gbm18 b where a.gba00c = b.gbr00c and a.gba01c = b.gbr01c and b.gbr02c = @gbr02c end else if (@intSN = 2) begin update a set gbr02c1 = b.gbr02c, gbr03c1 = b.gbr03c, gbr04i1 = b.gbr04i, gbr05i1 = b.gbr05i, gbr02cName1 = (select gsb03c from gsm02 where gsb00c = b.gbr00c and gsb01c = 'U' and gsb02c = b.gbr02c), gbr03cName1 = (select haa02c from ham01 where haa00c = b.gbr00c and haa01c = b.gbr03c), --负责人名称 gbr04iName1 = (select gsb03c from gsm02 where gsb00c = b.gbr00c and gsb01c = 'DJ' and gsb02c = b.gbr04i), --会员等级 gbr05iName1 = (select gsb03c from gsm02 where gsb00c = b.gbr00c and gsb01c = 'QZ' and gsb02c = b.gbr05i) --发展潜质 from #tb_query_gmb18i a, gbm18 b where a.gba00c = b.gbr00c and a.gba01c = b.gbr01c and b.gbr02c = @gbr02c end else if(@intSN = 3) begin update a set gbr02c2 = b.gbr02c, gbr03c2 = b.gbr03c, gbr04i2 = b.gbr04i, gbr05i2 = b.gbr05i, gbr02cName2 = (select gsb03c from gsm02 where gsb00c = b.gbr00c and gsb01c = 'U' and gsb02c = b.gbr02c), gbr03cName2 = (select haa02c from ham01 where haa00c = b.gbr00c and haa01c = b.gbr03c), --负责人名称 gbr04iName2 = (select gsb03c from gsm02 where gsb00c = b.gbr00c and gsb01c = 'DJ' and gsb02c = b.gbr04i), --会员等级 gbr05iName2 = (select gsb03c from gsm02 where gsb00c = b.gbr00c and gsb01c = 'QZ' and gsb02c = b.gbr05i) --发展潜质 from #tb_query_gmb18i a, gbm18 b where a.gba00c = b.gbr00c and a.gba01c = b.gbr01c and b.gbr02c = @gbr02c end else if(@intSN = 4) begin update a set gbr02c3 = b.gbr02c, gbr03c3 = b.gbr03c, gbr04i3 = b.gbr04i, gbr05i3 = b.gbr05i, gbr02cName3 = (select gsb03c from gsm02 where gsb00c = b.gbr00c and gsb01c = 'U' and gsb02c = b.gbr02c), gbr03cName3 = (select haa02c from ham01 where haa00c = b.gbr00c and haa01c = b.gbr03c), --负责人名称 gbr04iName3 = (select gsb03c from gsm02 where gsb00c = b.gbr00c and gsb01c = 'DJ' and gsb02c = b.gbr04i), --会员等级 gbr05iName3 = (select gsb03c from gsm02 where gsb00c = b.gbr00c and gsb01c = 'QZ' and gsb02c = b.gbr05i) --发展潜质 from #tb_query_gmb18i a, gbm18 b where a.gba00c = b.gbr00c and a.gba01c = b.gbr01c and b.gbr02c = @gbr02c end set @intSN = @intSN+1 fetch next from gbm18_cursor into @gbr02c end close gbm18_cursor deallocate gbm18_cursor select * from #tb_query_gmb18i where ( @chkDj = -1 or (@chkDj != -1 and gbr04i is null)) --未设置等级 and ( @chkFZren = -1 or (@chkFZren != -1 and isnull(gbr03c,'') = '' and isnull(gbr03c1,'') = '' and isnull(gbr03c2,'') = '' )) --未安排负责人 and ( @fromcs = -1 or isnull(cishu,0) between @fromcs and @tocs ) --来店次数 and ( @fzren = '' or (gbr03c = @fzren or gbr03c1 = @fzren or gbr03c2 = @fzren )) --负责人 and ( @kfdj = '' or ( gbr04i in (select F1 from f_splitstr(@kfdj,',')) or gbr04i1 in (select F1 from f_splitstr(@kfdj,',')) or gbr04i2 in (select F1 from f_splitstr(@kfdj,',')) )) --会员等级 and ( @kfqz = '' or ( gbr05i in (select F1 from f_splitstr(@kfqz,',')) or gbr05i1 in (select F1 from f_splitstr(@kfqz,',')) or gbr05i2 in (select F1 from f_splitstr(@kfqz,',')) )) --发展潜质 and ( @fromlcsale = -1 or (isnull(lcsale,0) between @fromlcsale and @tolcsale)) --购买能力(疗程销售) and ( @frompinlu =-1 or(isnull(pinlu,0) between @frompinlu and @topinlu)) --来店频率(消费频率) and ( (@chkNolc = -1 and isnull(lcsale,0) > 0 ) or (@chkNolc = 0 and isnull(lcsale,0) = 0)) --无疗程购买 and ( @fromjine = -1 or isnull(pjxh,0) between @fromjine and @tojine ) --平均消费金额 drop table #tb_query_gmb18i end GO /****** Object: StoredProcedure [dbo].[upg_get_unattendance] Script Date: 06/12/2017 06:20:09 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_get_unattendance] ( @CompId varchar(10), @Days int, @IsArrange int ) as begin create table #Hdm01Temp ( haa01c varchar(10) not null, gicj02v varchar(20) null, hda02d varchar(8) not null ) declare @currentData varchar(10) declare @index int set @index = 1 while (@index <= @Days) begin set @currentData = getdate() - @index set @currentData = substring(@currentData, 7, 4) + substring(@currentData, 1, 2) + (case when substring(@currentData, 4, 1) = '' then '0' else substring(@currentData, 4, 1) end) + substring(@currentData, 5, 1) if (@IsArrange = 1) begin insert into #Hdm01Temp(haa01c, gicj02v, hda02d) select gicj01v, gicj02v, @currentData from gicm10 where gicj00c = @CompId and gicj01v not in (select hda01c from hdm01 where hda00c = @CompId and hda02d = @currentData) and gicj05d = @currentData end else begin insert into #Hdm01Temp (haa01c, hda02d) select haa01c, @currentData from ham01 where haa00c = @CompId and haa27c = '2' and haa01c not in (select hda01c from hdm01 where hda00c = @CompId and hda02d = @currentData) end set @index = @index + 1 end select haa01c as name, gicj02v as class, hda02d as date from #Hdm01Temp drop table #Hdm01Temp end GO /****** Object: Table [dbo].[gxm05] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gxm05]( [gxe00c] [varchar](10) NOT NULL, [gxe01f] [float] NOT NULL, [gxe02d] [varchar](8) NULL, [gxe03t] [varchar](8) NULL, [gxe04c] [varchar](20) NULL, [gxe05c] [varchar](20) NULL, [gxe06c] [varchar](1000) NULL, [gxe07c] [varchar](2) NULL, [gxe08d] [varchar](8) NULL, [gxe09t] [varchar](8) NULL, CONSTRAINT [PK_GXM05] PRIMARY KEY CLUSTERED ( [gxe00c] ASC, [gxe01f] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[ghm03] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[ghm03]( [ghc00c] [varchar](10) NOT NULL, [ghc01c] [varchar](20) NOT NULL, [ghc02c] [nvarchar](100) NOT NULL, [ghc03d] [varchar](8) NULL, [ghc04d] [varchar](8) NULL, [ghc05f] [float] NULL, [ghc06c] [varchar](4) NULL, [ghc07d] [varchar](8) NULL, CONSTRAINT [PK_GHM03] PRIMARY KEY CLUSTERED ( [ghc00c] ASC, [ghc01c] ASC, [ghc02c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gxm06] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gxm06]( [gxf00c] [varchar](10) NOT NULL, [gxf01c] [varchar](20) NOT NULL, [gxf02f] [float] NOT NULL, [gxf03c] [varchar](20) NULL, [gxf04c] [varchar](20) NULL, CONSTRAINT [PK_GXM06] PRIMARY KEY CLUSTERED ( [gxf00c] ASC, [gxf01c] ASC, [gxf02f] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gcm02] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gcm02]( [gcb00c] [varchar](10) NOT NULL, [gcb01c] [varchar](20) NOT NULL, [gcb02f] [float] NOT NULL, [gcb03i] [int] NULL, [gcb04c] [varchar](20) NULL, [gcb05i] [int] NULL, [gcb06i] [int] NULL, [gcb07d] [varchar](8) NULL, [gcb08c] [varchar](20) NULL, CONSTRAINT [PK_GCM02] PRIMARY KEY CLUSTERED ( [gcb00c] ASC, [gcb01c] ASC, [gcb02f] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gcm03] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gcm03]( [gcc00c] [varchar](10) NOT NULL, [gcc01c] [varchar](20) NOT NULL, [gcc02f] [float] NOT NULL, [gcc03i] [int] NOT NULL, [gcc04c] [varchar](20) NULL, [gcc05c] [varchar](5) NULL, [gcc06f] [float] NULL, [gcc07f] [float] NULL, [gcc08c] [varchar](20) NULL, [gcc09d] [varchar](20) NULL, [gcc10f] [float] NULL, [gcc11c] [varchar](60) NULL, [gcc12f] [float] NULL, CONSTRAINT [PK_GCM03] PRIMARY KEY CLUSTERED ( [gcc00c] ASC, [gcc01c] ASC, [gcc03i] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gicm06] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gicm06]( [gicf00c] [varchar](10) NOT NULL, [gicf01v] [varchar](20) NOT NULL, [gicf02v] [varchar](20) NOT NULL, [gicf03i] [int] NULL, [gicf04t] [varchar](6) NULL, [gicf05t] [varchar](6) NULL, [gicf06d] [varchar](8) NULL, [gicf07v] [varchar](20) NULL, CONSTRAINT [PK_GICM06] PRIMARY KEY CLUSTERED ( [gicf00c] ASC, [gicf01v] ASC, [gicf02v] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[spm01] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[spm01]( [id] [int] IDENTITY(1,1) NOT NULL, [cname] [varchar](100) NOT NULL, [remark] [varchar](4000) NULL, CONSTRAINT [PK_spm01] PRIMARY KEY CLUSTERED ( [id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_get_card_happen_amt] Script Date: 06/12/2017 06:20:05 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_get_card_happen_amt] ( @belongcompid varchar(20), -- 本公司的卡,包括在别的店的充值以及消费,否则与利润分析表等对不起来 @cardtype varchar(20), @fromdate varchar(10), @todate varchar(10), @includeproc int ) as begin create table #gcm04_buf -- 会员卡帐户异动历史 ( idd int identity not null, gcd00c varchar(10) not null, --公司编号 gcd01c varchar(20) not null, --卡号 gcd02i int not null, --帐号类别(1-电子钱包, 2-储值帐号) gcd05i int null, --异动类别(0- 充值, 1-取款 2-消费, 3-转入, 4-转出) gcd06f float null, --异动金额 gcd07c varchar(5) null, --异动单据类别 gcd09d varchar(8) null, --异动日期 ) declare @PK_Sql nvarchar(max) set @PK_Sql = ' alter table #gcm04_buf add constraint PK_#gcm04_buf_' + convert(varchar(10), cast(floor(rand() * 1000000) as int)) + ' PRIMARY KEY NonCLUSTERED(idd)' exec sp_executesql @PK_Sql create clustered index gcm04_buf_idx_01 on #gcm04_buf(gcd00c, gcd09d, gcd05i) --本公司以及下属公司的消费记录, 不管卡的归属公司,也就是可能包括别的门店的会员卡 --不包括在别的公司的充值与消费 --需要包括所有的消费以及充值 insert #gcm04_buf(gcd00c, gcd01c, gcd02i, gcd05i, gcd06f, gcd09d, gcd07c) select gcd00c, gcd01c, gcd02i, gcd05i, gcd06f, gcd09d, gcd07c from gcm04 a with (nolock) where a.gcd09d <= @todate and a.gcd09d >= @fromdate and a.gcd02i<>1 and a.gcd02i<>3 and isnull(a.gcd08c, '')<>'' declare @procacct int select @procacct = 4 if @includeproc <> 1 begin delete #gcm04_buf where gcd02i = @procacct end create table #tbl_cardtype_happen( cardtype varchar(20) not null, in_amt float null, --充值金额, 本卡全充 in_amt_2 float null, --开账进来的 in_amt_other float null, --跨店充值(别的公司的卡在本店的充值) 外卡本店充值 out_amt float null, --消费金额, 本卡全消 out_amt_other float null, --跨店消费(别的公司的卡在本店的消费) 外卡本店消费 in_amt_bw float null, --本卡外店充值 out_amt_bw float null, --本卡外店消费 in_amt_bb float null, --本卡本店充值 out_amt_bb float null, --本卡本店消费 primary key clustered(cardtype) ) create table #tbl_cardtype_buf( cardtype varchar(20) not null, amt float null, primary key clustered(cardtype) ) --这个公司的所有类别 insert #tbl_cardtype_happen(cardtype, in_amt, out_amt) select distinct gak01c, 0, 0 from gam10 a with (nolock), gam26 z with (nolock) where a.gak00c = z.gaz02c and z.gaz01c = @belongcompid --归属公司为本公司的卡, 在本公司存的款 delete #tbl_cardtype_buf --需要扣掉欠款部分 --在所有门店归属本公司的卡充值 insert #tbl_cardtype_buf(cardtype, amt) select gca02c, sum(case when gcd05i = 5 then 0-gcd06f when gcd05i = 1 then 0-gcd06f else gcd06f end) from #gcm04_buf b with (nolock), gcm01 a with (nolock) where a.gca01c = b.gcd01c and a.gca00c = a.gca13d and a.gca00c = @belongcompid and (b.gcd05i = 0 or b.gcd05i = 5 or b.gcd05i = 6 or b.gcd05i = 7) --and (a.gca08i = 4 or a.gca08i = 5 or a.gca08i = 10) group by gca02c update a set a.in_amt = b.amt from #tbl_cardtype_happen a, #tbl_cardtype_buf b where a.cardtype = b.cardtype --跨店充值(外卡本店充值) --跨店充值: 归属公司非本公司的会员卡在本店的充值金额 delete #tbl_cardtype_buf insert #tbl_cardtype_buf(cardtype, amt) select gca02c, sum(case when gcd05i = 5 then 0-gcd06f when gcd05i = 1 then 0-gcd06f else gcd06f end) from #gcm04_buf b with (nolock), gcm01 a with (nolock) where a.gca01c = b.gcd01c and a.gca00c = a.gca13d and a.gca13d <> @belongcompid and b.gcd00c = @belongcompid and (b.gcd05i = 0 or b.gcd05i = 5 or b.gcd05i = 6 or b.gcd05i = 1) group by gca02c update a set a.in_amt_other = b.amt from #tbl_cardtype_happen a, #tbl_cardtype_buf b where a.cardtype = b.cardtype --本卡外充 delete #tbl_cardtype_buf insert #tbl_cardtype_buf(cardtype, amt) select gca02c, sum(case when gcd05i = 5 then 0-gcd06f when gcd05i = 1 then 0-gcd06f else gcd06f end) from #gcm04_buf b with (nolock), gcm01 a with (nolock) where a.gca01c = b.gcd01c and a.gca00c = a.gca13d and a.gca13d = @belongcompid and b.gcd00c <> @belongcompid and (b.gcd05i = 0 or b.gcd05i = 5 or b.gcd05i = 6 or b.gcd05i = 1) group by gca02c update a set a.in_amt_bw = b.amt from #tbl_cardtype_happen a, #tbl_cardtype_buf b where a.cardtype = b.cardtype --本卡本充 delete #tbl_cardtype_buf insert #tbl_cardtype_buf(cardtype, amt) select gca02c, sum(case when gcd05i = 5 then 0-gcd06f when gcd05i = 1 then 0-gcd06f else gcd06f end) from #gcm04_buf b with (nolock), gcm01 a with (nolock) where a.gca01c = b.gcd01c and a.gca00c = a.gca13d and a.gca13d = @belongcompid and b.gcd00c = @belongcompid and (b.gcd05i = 0 or b.gcd05i = 5 or b.gcd05i = 6 or b.gcd05i = 1) group by gca02c update a set a.in_amt_bb = b.amt from #tbl_cardtype_happen a, #tbl_cardtype_buf b where a.cardtype = b.cardtype --开账进来的卡 delete #tbl_cardtype_buf --归属公司为本公司的卡,在所有门店的开张进来 insert #tbl_cardtype_buf(cardtype, amt) select gca02c, sum(gcd06f) from #gcm04_buf b with (nolock), gcm01 a with (nolock) where a.gca01c = b.gcd01c and a.gca00c = a.gca13d and a.gca13d = @belongcompid and b.gcd05i = 0 and isnull(gcd07c, '') = '' group by gca02c update a set a.in_amt_2 = b.amt from #tbl_cardtype_happen a, #tbl_cardtype_buf b where a.cardtype = b.cardtype --期间消费: 统计的是在统计期间内归属公司为本公司的卡在本公司的消费 --本店的卡在所有门店的消费,否则期末余额是不会对上利润分析表 delete #tbl_cardtype_buf insert #tbl_cardtype_buf(cardtype, amt) select gca02c, sum(gcd06f) from #gcm04_buf b with (nolock), gcm01 a with (nolock) where a.gca01c = b.gcd01c and a.gca00c = a.gca13d and a.gca13d = @belongcompid and (b.gcd05i = 2 or b.gcd05i = 1) group by gca02c update a set a.out_amt = b.amt from #tbl_cardtype_happen a, #tbl_cardtype_buf b where a.cardtype = b.cardtype --跨店消费, 外卡本店消费,别的公司的卡在本店的消费 delete #tbl_cardtype_buf insert #tbl_cardtype_buf(cardtype, amt) select gca02c, sum(gcd06f) from #gcm04_buf b with (nolock), gcm01 a with (nolock) where a.gca01c = b.gcd01c and a.gca00c = a.gca13d and b.gcd00c = @belongcompid and a.gca13d <> @belongcompid and (b.gcd05i = 2) group by gca02c --可能在gcm01中存在的卡类别已经在gam10中不存在了 update a set a.out_amt_other = b.amt from #tbl_cardtype_happen a, #tbl_cardtype_buf b where a.cardtype = b.cardtype --本卡外销费 delete #tbl_cardtype_buf insert #tbl_cardtype_buf(cardtype, amt) select gca02c, sum(gcd06f) from #gcm04_buf b with (nolock), gcm01 a with (nolock) where a.gca01c = b.gcd01c and a.gca00c = a.gca13d and b.gcd00c <> @belongcompid and a.gca13d = @belongcompid and (b.gcd05i = 2) group by gca02c update a set a.out_amt_bw = b.amt from #tbl_cardtype_happen a, #tbl_cardtype_buf b where a.cardtype = b.cardtype --本卡本销费 delete #tbl_cardtype_buf insert #tbl_cardtype_buf(cardtype, amt) select gca02c, sum(gcd06f) from #gcm04_buf b with (nolock), gcm01 a with (nolock) where a.gca01c = b.gcd01c and a.gca00c = a.gca13d and b.gcd00c = @belongcompid and a.gca13d = @belongcompid and (b.gcd05i = 2) group by gca02c update a set a.out_amt_bb = b.amt from #tbl_cardtype_happen a, #tbl_cardtype_buf b where a.cardtype = b.cardtype select cardtype, in_amt, in_amt_2, in_amt_other, out_amt, out_amt_other, in_amt_bw, out_amt_bw, in_amt_bb, out_amt_bb from #tbl_cardtype_happen where ((@cardtype = '*') or (cardtype = @cardtype)) drop table #tbl_cardtype_buf drop table #tbl_cardtype_happen drop table #gcm04_buf end GO /****** Object: Table [dbo].[gym01] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gym01]( [gya00c] [char](10) NOT NULL, [gya01c] [varchar](20) NOT NULL, [gya02f] [float] NOT NULL, [gya03f] [float] NULL, [gya04f] [float] NULL, [gya05i] [int] NULL, [gya06i] [int] NULL, CONSTRAINT [PK_GYM01] PRIMARY KEY CLUSTERED ( [gya00c] ASC, [gya01c] ASC, [gya02f] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_get_card_remain_amt] Script Date: 06/12/2017 06:20:05 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_get_card_remain_amt] ( @compid varchar(20), -- 本公司卡的消费统计,包括在别的店的充值以及消费 @cardtype varchar(20), @fromdate varchar(10), @todate varchar(10), @includeproc int ) as begin create table #tbl_cardtype_happen ( cardtype varchar(20) not null, in_amt float null, in_amt_2 float null, -- 开账进来的 in_amt_other float null, -- 跨店充值(别的公司的卡在本店的充值) out_amt float null, out_amt_other float null, -- 跨店消费(别的公司的卡在本店的消费) in_amt_bw float null, -- 本卡外店充值 out_amt_bw float null, -- 本卡外店消费 in_amt_bb float null, -- 本卡本店充值 out_amt_bb float null, -- 本卡本店消费 primary key clustered(cardtype) ) create table #tbl_remain_stat ( cardtype varchar(20) not null, remain_start float null, in_amt float null, in_amt_2 float null, -- 开账进来的 in_amt_other float null, -- 跨店充值(别的公司的卡在本店的充值), 外卡本充 out_amt float null, out_amt_other float null, -- 跨店消费(别的公司的卡在本店的消费), 外卡本消 in_amt_bw float null, -- 本卡外店充值 out_amt_bw float null, -- 本卡外店消费 in_amt_bb float null, -- 本卡本店充值 out_amt_bb float null, -- 本卡本店消费 remain_end float null, primary key clustered(cardtype) ) -- 这个公司的所有类别 insert #tbl_remain_stat(cardtype, remain_start, in_amt, out_amt, remain_end, in_amt_bw, out_amt_bw, in_amt_bb, out_amt_bb) select distinct gak01c, 0, 0, 0, 0, 0, 0, 0, 0 from gam10 a with(nolock), gam26 z with(nolock) where a.gak00c = z.gaz02c and z.gaz01c = @compid and (@cardtype = '*' or gak01c = @cardtype) declare @prior_fromdate varchar(20) declare @ddatetime datetime set @ddatetime = dateadd(day, -1, convert(datetime, @fromdate)) select @prior_fromdate = substring(convert(varchar(20), @ddatetime, 102), 1, 4) + substring(convert(varchar(20), @ddatetime, 102), 6, 2) + substring(convert(varchar(20), @ddatetime, 102), 9, 2) create table #tbl_cardtype_buf ( cardtype varchar(20) not null, amt float null, primary key clustered(cardtype) ) create table #gcm04_buf -- 会员卡帐户异动历史 ( idd int identity not null, gcd00c varchar(10) not null, --公司编号 gcd01c varchar(20) not null, --卡号 gcd02i int not null, --帐号类别(1-电子钱包, 2-储值帐号) gcd05i int null, --异动类别(0- 充值, 1-取款 2-消费, 3-转入, 4-转出) gcd06f float null, --异动金额 gcd07c varchar(5) null, --异动单据类别 gcd09d varchar(8) null, --异动日期 ) create clustered index gcm04_buf_idx_01 on #gcm04_buf(gcd00c, gcd09d, gcd05i) insert #gcm04_buf(gcd00c, gcd01c, gcd02i, gcd05i, gcd06f, gcd09d, gcd07c) select gcd00c, gcd01c, gcd02i, gcd05i, gcd06f, gcd09d, gcd07c from gcm04 a with (nolock) where a.gcd00c = @compid and a.gcd09d <= @prior_fromdate and a.gcd09d >= '20000101' and a.gcd02i <> 1 and a.gcd02i <> 3 --and (@includeproc <> 1 and a.gcd02i <> 4) and ((isnull(@includeproc,0) =0 and a.gcd02i <> 4 ) or (isnull(@includeproc,0) = 1)) and isnull(a.gcd08c, '')<>'' --这个公司的所有类别 insert #tbl_cardtype_happen(cardtype, in_amt, out_amt) select distinct gak01c, 0, 0 from gam10 a with (nolock), gam26 z with (nolock) where a.gak00c = z.gaz02c and z.gaz01c = @compid and (@cardtype = '*' or gak01c = @cardtype) insert #tbl_cardtype_buf(cardtype, amt) select gca02c, sum(case when gcd05i = 5 then 0-gcd06f when gcd05i = 1 then 0-gcd06f else gcd06f end) from #gcm04_buf b with (nolock), gcm01 a with (nolock) where a.gca01c = b.gcd01c and a.gca00c = a.gca13d and a.gca00c = @compid and (b.gcd05i = 0 or b.gcd05i = 5 or b.gcd05i = 6 ) group by gca02c update a set a.in_amt = b.amt from #tbl_cardtype_happen a, #tbl_cardtype_buf b where a.cardtype = b.cardtype delete #tbl_cardtype_buf insert #tbl_cardtype_buf(cardtype, amt) select gca02c, sum(gcd06f) from #gcm04_buf b with (nolock), gcm01 a with (nolock) where a.gca01c = b.gcd01c and a.gca00c = a.gca13d and a.gca13d = @compid and (b.gcd05i = 2 or b.gcd05i = 1) group by gca02c update a set a.out_amt = b.amt from #tbl_cardtype_happen a, #tbl_cardtype_buf b where a.cardtype = b.cardtype --期初 update a set a.remain_start = isnull(b.in_amt, 0) - isnull(b.out_amt, 0) from #tbl_remain_stat a, #tbl_cardtype_happen b where a.cardtype = b.cardtype --清除临时表 drop table #tbl_cardtype_buf drop table #gcm04_buf delete #tbl_cardtype_happen insert #tbl_cardtype_happen(cardtype, in_amt, in_amt_2, in_amt_other, out_amt, out_amt_other, in_amt_bw, out_amt_bw, in_amt_bb, out_amt_bb) exec upg_get_card_happen_amt @compid, @cardtype, @fromdate, @todate, @includeproc update a set a.in_amt = isnull(b.in_amt, 0), a.out_amt = isnull(b.out_amt, 0), a.in_amt_2 = isnull(b.in_amt_2, 0), a.in_amt_other = isnull(b.in_amt_other, 0), a.out_amt_other = isnull(b.out_amt_other, 0), a.in_amt_bw = isnull(b.in_amt_bw, 0), a.out_amt_bw = isnull(b.out_amt_bw, 0), a.in_amt_bb = isnull(b.in_amt_bb, 0), a.out_amt_bb = isnull(b.out_amt_bb, 0) from #tbl_remain_stat a, #tbl_cardtype_happen b where a.cardtype = b.cardtype update a set a.remain_end = isnull(a.remain_start, 0) + isnull(a.in_amt, 0) - isnull(a.out_amt, 0) from #tbl_remain_stat a select isnull(cardtype,'') cardtype, isnull(remain_start,0) remain_start, isnull(in_amt,0) in_amt, isnull(in_amt_2,0) in_amt_2, isnull(in_amt_other,0) in_amt_other, isnull(out_amt,0) out_amt, isnull(out_amt_other,0) out_amt_other, isnull(in_amt_bw,0) in_amt_bw, isnull(out_amt_bw,0) out_amt_bw, isnull(in_amt_bb,0) in_amt_bb, isnull(out_amt_bb,0) out_amt_bb, isnull(remain_end,0) remain_end from #tbl_remain_stat drop table #tbl_cardtype_happen drop table #tbl_remain_stat end GO /****** Object: Table [dbo].[gim02] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gim02]( [gib00c] [varchar](10) NOT NULL, [gib01c] [nvarchar](100) NOT NULL, [gib02f] [float] NOT NULL, [gib03c] [varchar](20) NULL, [gib04c] [varchar](5) NULL, [gib05f] [float] NULL, [gib06c] [varchar](5) NULL, [gib07f] [float] NULL, [gib08f] [float] NULL, [gib09f] [float] NULL, [gib10f] [float] NULL, [gib11f] [float] NULL, [gib12c] [varchar](20) NULL, CONSTRAINT [PK_GIM02] PRIMARY KEY CLUSTERED ( [gib00c] ASC, [gib01c] ASC, [gib02f] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_date_statistic] Script Date: 06/12/2017 06:19:56 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_date_statistic] ( @compid varchar(10), -- 公司编号 @from varchar(10), -- 日期 from @to varchar(10) -- 日期 to ) as--WUXM, 2004/12/25 begin declare @incompid varchar(10) select @incompid = @compid create table #struct ( id int identity not null, datatime varchar(2) null, -- 统计时间段 class varchar(20) null, -- 会员卡类别编号 customer varchar(5) null, -- 客户类别 enternum int null, -- 入院人数 outnum int null, -- 出馆人数 primary key (id), ) create table #return ( id int identity not null, datatime varchar(2) null, --统计时间段 class varchar(20) null, --会员卡类别编号 customer varchar(5) null, --客户类别 enternum int null, --入院人数 outnum int null, --出馆人数 primary key (id), ) --壁球入院的临时表 create table #temp ( id int identity not null, datatime varchar(2) null, class varchar(20) null, customer varchar(20) null, enternum int null, outnum int null, primary key (id), ) if isnull(@from, '') = '' or isnull(@from, '') = '*' begin -- 统计入院人数并放入临时表中#struct insert #struct(datatime, class, customer, enternum) select substring(gla03t, 1, 2), gla06c, gla07c, sum(gla08i) from glm01 a with (nolock), gam26 z with (nolock) where gla00c = z.gaz02c and z.gaz01c = @incompid and isnull(gla03t, '')<> '' group by substring(gla03t, 1, 2), gla06c, gla07c --- 找出出馆人数 insert #temp(datatime, class, customer, outnum) select substring(gla04t, 1, 2), gla06c, gla07c, sum(gla08i) from glm01 a with (nolock), gam26 z with (nolock) where gla00c = z.gaz02c and z.gaz01c = @incompid and isnull(gla95c, '')<>'' and isnull(gla04t, '')<> '' group by substring(gla04t, 1, 2), gla06c, gla07c --- 根据出馆临时表更新出馆人数 update t set t.outnum = isnull(t.outnum, 0) + a.outnum from #temp a, #struct t where a.datatime = t.datatime and a.class = t.class --清空出馆的临时表 truncate table #temp --删除临时表#temp drop table #temp end else begin ---统计入院人数并放入临时表中#struct insert #struct(datatime, class, customer, enternum) select substring(gla03t, 1, 2), gla06c, gla07c, sum(gla08i) from glm01 a with (nolock), gam26 z with (nolock) where gla00c = z.gaz02c and z.gaz01c = @incompid and gla02d <= @to and gla02d >= @from and isnull(gla03t, '')<> '' group by substring(gla03t, 1, 2), gla06c, gla07c --- 找出出馆人数 insert #temp(datatime, class, customer, outnum) select substring(gla04t, 1, 2), gla06c, gla07c, sum(gla08i) from glm01 a with (nolock), gam26 z with (nolock) where gla00c = z.gaz02c and z.gaz01c = @incompid and gla02d <= @to and gla02d >= @from and isnull(gla95c, '')<>'' and isnull(gla04t, '')<> '' group by substring(gla04t, 1, 2), gla06c, gla07c --- 根据出馆临时表更新出馆人数 update t set t.outnum = isnull(t.outnum, 0) + a.outnum from #temp a, #struct t where a.datatime = t.datatime and a.class = t.class --清空出馆的临时表 truncate table #temp --删除临时表#temp drop table #temp end update t set t.class = gca02c from gcm01 a, #struct t where a.gca01c = t.class insert #return(datatime, class, customer, enternum, outnum) select datatime, class, customer, sum(enternum), sum(outnum) from #struct group by datatime, class, customer select isnull(datatime,'') datatime, isnull(class,'') class, isnull(customer,'') customer, isnull(enternum,0) enternum, isnull(outnum,0) outnum from #return order by datatime drop table #struct drop table #return end GO /****** Object: Table [dbo].[ggm04] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[ggm04]( [ggd01c] [varchar](100) NOT NULL, [ggd02c] [varchar](5) NOT NULL, [ggd03c] [varchar](20) NULL, [ggd04c] [varchar](100) NULL, [ggd05c] [varchar](5) NULL, [ggd06f] [float] NULL, [ggd07f] [float] NULL, [ggd08i] [int] NULL, [ggd09c] [varchar](30) NULL, [ggd90d] [varchar](10) NULL, [ggd91t] [varchar](10) NULL, CONSTRAINT [PK_GGM04] PRIMARY KEY NONCLUSTERED ( [ggd01c] ASC, [ggd02c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[spm02] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[spm02]( [pid] [int] NOT NULL, [pay] [varchar](10) NOT NULL, [did] [int] NOT NULL, [remark] [varchar](4000) NULL, CONSTRAINT [PK_spm02] PRIMARY KEY CLUSTERED ( [pid] ASC, [pay] ASC, [did] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gicm07] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gicm07]( [gicg00c] [varchar](10) NOT NULL, [gicg01v] [varchar](20) NOT NULL, [gicg02i] [int] NULL, [gicg03t] [varchar](6) NULL, [gicg04t] [varchar](6) NULL, [gicg05d] [varchar](8) NULL, [gicg06v] [varchar](20) NULL, CONSTRAINT [PK_GICM07] PRIMARY KEY CLUSTERED ( [gicg00c] ASC, [gicg01v] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[lvm01] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[lvm01]( [gva00c] [varchar](10) NOT NULL, [gva01c] [varchar](20) NOT NULL, [gva02d] [varchar](8) NULL, [gva03c] [varchar](10) NULL, [gva04c] [varchar](10) NULL, [gva05c] [varchar](20) NULL, [gva06c] [varchar](20) NULL, [gva07i] [int] NULL, [gva08c] [varchar](20) NULL, [gva09i] [int] NULL, [gva10t] [varchar](6) NULL, [gva11c] [varchar](60) NULL, [gva93c] [varchar](10) NULL, [gva94d] [varchar](8) NULL, CONSTRAINT [pk_lvm01] PRIMARY KEY CLUSTERED ( [gva00c] ASC, [gva01c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gcm03_log_gcc12f] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gcm03_log_gcc12f]( [glc00c] [varchar](10) NOT NULL, [glc01c] [varchar](20) NOT NULL, [glc02f] [int] IDENTITY(1,1) NOT NULL, [glc03i] [int] NOT NULL, [glc04f] [float] NOT NULL, [glc05d] [varchar](20) NOT NULL, CONSTRAINT [PK_GCM03_LOG_GCC12F] PRIMARY KEY CLUSTERED ( [glc02f] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_stock_get] Script Date: 06/12/2017 06:20:45 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_stock_get] ( @compid varchar(10), @fromdate varchar(8), @todate varchar(8), @fromgoods varchar(20), @togoods varchar(20), @fromcardno varchar(20), @tocardno varchar(20), @rpttype varchar(1), @type int -- 0所有1寄存 2欠货 ) as begin declare @txt varchar(20) declare @strtext varchar(1) if @type = 0 select @txt = '寄存/欠货' else if @type = 1 select @txt = '寄存' else if @type = 2 select @txt = '欠货' else if @type = 3 select @txt = '领取' create table #result ( compid varchar(10) null, -- 门店 goodsno varchar(20) null, -- 产品编号 goodsname varchar(80) null, -- 产品名称 cardno varchar(20) null, -- 会员编号 cardname varchar(20) null, -- 会员姓名 ddate varchar(10) null, -- 日期 billno varchar(20) null, -- 寄存单号 quantity float null, -- 寄存数量 getquantity float null, -- 领取数量 remainquantity float null, -- 剩余数量 emplyeeone varchar(20) null, -- 寄存负责人 orderState int null, -- 单据类型 ) --整理数据 modified by maql 20100427 增加连锁查询. -- Modify by liyh 20101118 if (@type = 0) begin insert #result(compid, goodsno, cardno, ddate, billno, quantity, getquantity, remainquantity, emplyeeone, orderState) select z.gaz01c, gio05c, gim02c, gim08d, gim01c, gio07f, gio09f, gio10f, gin06c, gim06c from gim20 a, gam26 z, gim21 b, gim22 c where a.gim00c = z.gaz02c and (z.gaz01c = @compid or @compid = '*') and (a.gim06c = '1' or a.gim06c = '3') and a.gim00c = b.gin00c and a.gim01c = b.gin01c and c.did = b.id and (gio05c between @fromgoods and @togoods or @fromgoods = '*') and (gim02c between @fromcardno and @tocardno or @fromcardno = '*') and (gim08d between @fromdate and @todate) end else if (@type = 1) begin insert #result(compid, goodsno, cardno, ddate, billno, quantity, getquantity, remainquantity, emplyeeone, orderState) select z.gaz01c, gio05c, gim02c, gim08d, gim01c, gio07f, gio09f, gio10f, gin06c, gim06c from gim20 a, gam26 z, gim21 b, gim22 c where a.gim00c = z.gaz02c and (z.gaz01c = @compid or @compid = '*') and (a.gim06c = '1') and a.gim00c = b.gin00c and a.gim01c = b.gin01c and c.did = b.id and (gio05c between @fromgoods and @togoods or @fromgoods = '*') and (gim02c between @fromcardno and @tocardno or @fromcardno = '*') and (gim08d between @fromdate and @todate) end else if (@type = 2) begin insert #result(compid, goodsno, cardno, ddate, billno, quantity, getquantity, remainquantity, emplyeeone, orderState) select z.gaz01c, gio05c, gim02c, gim08d, gim01c, gio07f, gio09f, gio10f, gin06c, gim06c from gim20 a, gam26 z, gim21 b, gim22 c where a.gim00c = z.gaz02c and (z.gaz01c = @compid or @compid = '*') and (a.gim06c = '3') and a.gim00c = b.gin00c and a.gim01c = b.gin01c and c.did = b.id and (gio05c between @fromgoods and @togoods or @fromgoods = '*') and (gim02c between @fromcardno and @tocardno or @fromcardno = '*') and (gim08d between @fromdate and @todate) end else if (@type = 3) begin insert #result(compid, goodsno, cardno, ddate, billno, quantity, getquantity, remainquantity, emplyeeone, orderState) select z.gaz01c, gio05c, gim02c, gim08d, gim01c, gio07f, gio09f, gio10f, gin06c, gim06c from gim20 a, gam26 z, gim21 b, gim22 c where a.gim00c = z.gaz02c and (z.gaz01c = @compid or @compid = '*') and (a.gim06c = '2') and a.gim00c = b.gin00c and a.gim01c = b.gin01c and b.take_from_id = c.did and (gio05c between @fromgoods and @togoods or @fromgoods = '*') and (gim02c between @fromcardno and @tocardno or @fromcardno = '*') and (gim08d between @fromdate and @todate) end update a set a.emplyeeone = (select haa02c from ham01 where haa01c = a.emplyeeone and haa00c = @compid and haa27c <> '3') from #result a update a set a.goodsname = (select gfa03c from gfm01 where gfa01c = a.goodsno and gfa00c = @compid) from #result a update a set a.cardname = (select gba03c from gbm01 where gba01c = a.cardno and gba00c = @compid) from #result a --整理报表 create table #result_tmp_2 ( flag int identity(1, 1) not null, compid varchar(10) null, goodsno varchar(20) null, goodsname varchar(80) null, cardno varchar(20) null, cardname varchar(20) null, ddate varchar(10) null, billno varchar(20) null, quantity float null, getquantity float null, remainquantity float null, emplyeeone varchar(20) null, sumquantity float null, sumgetquantity float null, sumremainquantity float null, flag1 int null, flag2 int null, flag3 int null, flag4 int null, flag5 int null, orderState int null, ) if (@rpttype = '0') begin insert #result_tmp_2(compid, goodsno, goodsname, cardno, cardname, ddate, billno, quantity, getquantity, remainquantity, emplyeeone, sumquantity, sumgetquantity, sumremainquantity, flag1, flag2, flag3, flag4, flag5, orderState) select compid, goodsno, goodsname, cardno, cardname, substring(ddate, 1, 4) + '-' + substring(ddate, 5, 2) + '-' + substring(ddate, 7, 2), billno, quantity, getquantity, remainquantity, emplyeeone, 0, 0, 0, 0, 0, 0, 0, 0, orderState from #result order by goodsno, cardno, ddate, billno, remainquantity desc update a set a.flag1 = a.flag - (select top 1 b.flag from #result_tmp_2 b where a.goodsno = b.goodsno order by b.flag) + 1 from #result_tmp_2 a update a set a.flag2 = a.flag1 - (select top 1 b.flag1 from #result_tmp_2 b where a.goodsno = b.goodsno and a.cardno = b.cardno order by b.flag1) + 1 from #result_tmp_2 a update a set a.flag3 = a.flag2-(select top 1 b.flag2 from #result_tmp_2 b where a.goodsno = b.goodsno and a.cardno = b.cardno and a.ddate = b.ddate order by b.flag2) + 1 from #result_tmp_2 a update a set a.flag4 = a.flag3-(select top 1 b.flag3 from #result_tmp_2 b where a.goodsno = b.goodsno and a.cardno = b.cardno and a.ddate = b.ddate and a.billno = b.billno order by b.flag3) + 1 from #result_tmp_2 a update a set a.flag5 = a.flag4-(select top 1 b.flag4 from #result_tmp_2 b where a.goodsno = b.goodsno and a.cardno = b.cardno and a.ddate = b.ddate and a.billno = b.billno order by b.flag4) + 1 from #result_tmp_2 a update a set a.sumquantity = (select sum(isnull(b.quantity, 0)) from #result_tmp_2 b where a.goodsno = b.goodsno group by b.goodsno) from #result_tmp_2 a update a set a.sumgetquantity = (select sum(isnull(b.getquantity, 0)) from #result_tmp_2 b where a.goodsno = b.goodsno group by b.goodsno) from #result_tmp_2 a update a set a.sumremainquantity = isnull(a.sumquantity, 0) - isnull(a.sumgetquantity, 0) from #result_tmp_2 a update a set a.goodsno = '', a.goodsname = '' from #result_tmp_2 a where a.flag1<>1 update a set a.cardno = '', a.cardname = '' from #result_tmp_2 a where a.flag2<>1 update a set a.ddate = '' from #result_tmp_2 a where a.flag3<>1 update a set a.billno = '' from #result_tmp_2 a where a.flag4<>1 select field1 = isnull(goodsno,''), field2 = isnull(goodsname,''), field3 = isnull(cardno,''), field4 = isnull(cardname,''), field5 = isnull(ddate,''), field6 = isnull(billno,''), field7 = @txt, field8 = isnull(quantity, 0), field9 = isnull(getquantity, 0), field10 = isnull(remainquantity, 0), field11 = isnull(emplyeeone, ''), field15 = isnull(sumquantity, 0), field16 = isnull(sumgetquantity, 0), field17 = isnull(sumremainquantity, 0), isnull(orderState,'') orderState from #result_tmp_2 end if (@rpttype = '1') begin insert #result_tmp_2(compid, goodsno, goodsname, cardno, cardname, ddate, billno, quantity, getquantity, remainquantity, emplyeeone, sumquantity, sumgetquantity, sumremainquantity, flag1, flag2, flag3, flag4, flag5, orderState) select compid, goodsno, goodsname, cardno, cardname, substring(ddate, 1, 4) + '-' + substring(ddate, 5, 2) + '-' + substring(ddate, 7, 2), billno, quantity, getquantity, remainquantity, emplyeeone, 0, 0, 0, 0, 0, 0, 0, 0, orderState from #result order by cardno, ddate, billno, goodsno, remainquantity desc update a set a.flag1 = a.flag-(select top 1 b.flag from #result_tmp_2 b where a.cardno = b.cardno order by b.flag) + 1 from #result_tmp_2 a update a set a.flag2 = a.flag1-(select top 1 b.flag1 from #result_tmp_2 b where a.cardno = b.cardno and a.ddate = b.ddate order by b.flag1) + 1 from #result_tmp_2 a update a set a.flag3 = a.flag2-(select top 1 b.flag2 from #result_tmp_2 b where a.cardno = b.cardno and a.ddate = b.ddate and a.billno = b.billno order by b.flag2) + 1 from #result_tmp_2 a update a set a.flag4 = a.flag3-(select top 1 b.flag3 from #result_tmp_2 b where a.cardno = b.cardno and a.ddate = b.ddate and a.billno = b.billno order by b.flag3) + 1 from #result_tmp_2 a update a set a.flag5 = a.flag4-(select top 1 b.flag4 from #result_tmp_2 b where a.cardno = b.cardno and a.ddate = b.ddate and a.billno = b.billno and a.goodsno = b.goodsno order by b.flag4) + 1 from #result_tmp_2 a update a set a.sumquantity = (select sum(isnull(b.quantity, 0)) from #result_tmp_2 b where a.cardno = b.cardno group by b.cardno) from #result_tmp_2 a update a set a.sumgetquantity = (select sum(isnull(b.getquantity, 0)) from #result_tmp_2 b where a.cardno = b.cardno group by b.cardno) from #result_tmp_2 a update a set a.sumremainquantity = isnull(a.sumquantity, 0) - isnull(a.sumgetquantity, 0) from #result_tmp_2 a update a set a.cardno = '', a.cardname = '' from #result_tmp_2 a where a.flag1<>1 update a set a.ddate = '' from #result_tmp_2 a where a.flag2<>1 update a set a.billno = '' from #result_tmp_2 a where a.flag3<>1 update a set a.goodsno = '', a.goodsname = ''from #result_tmp_2 a where a.flag5<>1 select field1 = isnull(cardno,''), field2 = isnull(cardname,''), field3 = isnull(ddate,''), field4 = isnull(billno,''), field5 = @txt, field6 = isnull(goodsno,''), field7 = isnull(goodsname,''), field8 = isnull(quantity, 0), field9 = isnull(getquantity, 0), field10 = isnull(remainquantity, 0), field11 = isnull(emplyeeone, ''), field15 = isnull(sumquantity, 0), field16 = isnull(sumgetquantity, 0), field17 = isnull(sumremainquantity, 0), isnull(orderState,'') orderState from #result_tmp_2 end delete #result drop table #result delete #result_tmp_2 drop table #result_tmp_2 end GO /****** Object: Table [dbo].[gjm02] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gjm02]( [gjb00c] [varchar](10) NOT NULL, [gjb01c] [varchar](20) NOT NULL, [gjb02f] [float] NOT NULL, [gjb03c] [varchar](20) NULL, [gjb04c] [varchar](5) NULL, [gjb05f] [float] NULL, [gjb06c] [varchar](5) NULL, [gjb07f] [float] NULL, [gjb08f] [float] NULL, [gjb09f] [float] NULL, [gjb10f] [float] NULL, [gjb11f] [float] NULL, CONSTRAINT [PK_GJM02] PRIMARY KEY CLUSTERED ( [gjb00c] ASC, [gjb01c] ASC, [gjb02f] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[data_recover] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[data_recover]( [rdd00c] [varchar](10) NOT NULL, [rdd01c] [varchar](50) NOT NULL, [rdd02c] [varchar](20) NOT NULL, CONSTRAINT [PK_data_recover] PRIMARY KEY CLUSTERED ( [rdd00c] ASC, [rdd01c] ASC, [rdd02c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_calculate_prod_price_mv] Script Date: 06/12/2017 06:19:37 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_calculate_prod_price_mv] ( @compid varchar(10), -- 公司编号 @prod varchar(20), -- 产品代码 @currdate varchar(8), -- 复核日期 @price float out ) as-- WUXM 2004/12/6 PM 21:31 begin declare @count float set @count = 0 declare @amt float set @amt = 0 set @price = 0 create table #gfm04 ( gfd05f float, gfd13f float, inout int, number int ) create clustered index idx_gfm04_tmp_01 on #gfm04(number) insert into #gfm04(number, gfd05f, gfd13f, inout) select row_number() over(order by gfd07c, gfd01c, gfc09t) as number, gfd05f, gfd13f, (case when gfd01c = 1 then 1 else -1 end) as inout from gfm04 a with (nolock) join gfm03 b with (nolock) on a.gfd00c = b.gfc00c and a.gfd01c = b.gfc01c and a.gfd02c = b.gfc02c where gfd00c = @compid and gfd04c = @prod and gfd07c <= @currdate and isnull(gfd05f, 0) > 0 declare @i int set @i = 1 declare @max int select @max = max(number) from #gfm04 while @i <= @max begin select @count = @count + gfd05f * inout, @amt = @amt + (case when inout = 1 then gfd13f else gfd05f * @price * -1 end), @price = (case when inout = 1 then dbo.fun_divide_default(@amt, @count, @price) else @price end) from #gfm04 where number = @i set @i = @i + 1 end drop table #gfm04 end GO /****** Object: Table [dbo].[gicm09] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gicm09]( [gici00c] [varchar](10) NOT NULL, [gici01v] [varchar](20) NOT NULL, [gici02v] [varchar](20) NULL, [gici03i] [int] NULL, [gici04t] [varchar](6) NULL, [gici05t] [varchar](6) NULL, [gici06d] [varchar](8) NULL, [gici07i] [int] NULL, [gici08c] [varchar](20) NULL, [gici09c] [varchar](20) NULL, CONSTRAINT [PK_GICM09] PRIMARY KEY CLUSTERED ( [gici00c] ASC, [gici01v] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gzm02] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gzm02]( [gzb01c] [varchar](5) NOT NULL, [gzb02c] [varchar](10) NOT NULL, [gzb03c] [char](1) NULL, [gzb04c] [char](1) NULL, [gzb05c] [char](1) NULL, [gzb06c] [char](1) NULL, [gzb07c] [char](1) NULL, [gzb08c] [char](1) NULL, [gzb09c] [char](1) NULL, [gzb10c] [char](1) NULL, [gzb11c] [varchar](10) NOT NULL, [gzb12c] [char](1) NULL, [gzb13c] [char](1) NULL, [gzb14c] [char](1) NULL, [gzb15c] [char](1) NULL, [gzb16c] [char](1) NULL, [gzb19c] [char](1) NULL, CONSTRAINT [PK_gzm02] PRIMARY KEY CLUSTERED ( [gzb01c] ASC, [gzb02c] ASC, [gzb11c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gcm04] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gcm04]( [gcd00c] [varchar](10) NOT NULL, [gcd01c] [varchar](20) NOT NULL, [gcd02i] [int] NOT NULL, [gcd03c] [varchar](20) NULL, [gcd04f] [float] NOT NULL, [gcd05i] [int] NULL, [gcd06f] [float] NULL, [gcd07c] [varchar](5) NULL, [gcd08c] [varchar](20) NULL, [gcd09d] [varchar](8) NULL, [gcd10f] [float] NULL, [gcd11c] [varchar](20) NULL, [gcd12i] [int] NULL, [gcd13t] [varchar](10) NULL, [gcd14c] [varchar](120) NULL, [gcd15f] [float] NULL, [gcd16c] [varchar](20) NULL, [gcd17i] [int] NULL, [gcd18c] [varchar](20) NULL, CONSTRAINT [PK_GCM04] PRIMARY KEY CLUSTERED ( [gcd00c] ASC, [gcd01c] ASC, [gcd02i] ASC, [gcd04f] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gicm11] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gicm11]( [gick00c] [varchar](10) NOT NULL, [gick01v] [varchar](20) NOT NULL, [gick02v] [varchar](20) NULL, [gick03t] [varchar](6) NULL, [gick04t] [varchar](6) NULL, [gick05v] [varchar](20) NULL, [gick06t] [varchar](6) NULL, [gick07d] [varchar](8) NULL, [gick08v] [varchar](100) NULL, [gick09t] [varchar](6) NULL, CONSTRAINT [PK_GICM11] PRIMARY KEY CLUSTERED ( [gick00c] ASC, [gick01v] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gzm04] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gzm04]( [gzd00c] [varchar](10) NOT NULL, [gzd01d] [varchar](8) NOT NULL, [gzd02i] [int] IDENTITY(1,1) NOT NULL, [gzd03c] [varchar](50) NULL, [gzd04f] [float] NULL, [gzd05c] [varchar](300) NULL, CONSTRAINT [PK_gzm04] PRIMARY KEY CLUSTERED ( [gzd00c] ASC, [gzd01d] ASC, [gzd02i] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gim21] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gim21]( [gin00c] [varchar](10) NOT NULL, [gin01c] [varchar](20) NOT NULL, [gin02c] [varchar](20) NOT NULL, [gin03f] [float] NOT NULL, [gin04f] [float] NULL, [gin05c] [varchar](20) NULL, [gin06c] [varchar](20) NULL, [gin07f] [varchar](50) NULL, [gin08c] [varchar](50) NULL, [gin09c] [varchar](50) NULL, [gin10c] [varchar](50) NULL, [gin11c] [varchar](50) NULL, [id] [int] IDENTITY(1,1) NOT NULL, [take_from_id] [int] NULL, CONSTRAINT [PK_GIM21] PRIMARY KEY CLUSTERED ( [gin00c] ASC, [gin01c] ASC, [gin02c] ASC, [gin03f] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_gbm19_after] Script Date: 06/12/2017 06:20:03 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_gbm19_after] ( @company varchar(10), --门店编号 @cardId varchar(10), @startDate varchar(10), @endDate varchar(10) ) as begin create table #gcm06_date ( gcf00c varchar(10) not null, --门店编号 gcf01c varchar(20) not null, --卡号 gcf02c varchar(20) not null, --项目编号 gcf05f float null, --总次数 gcf06f float null, --已经使用次数 gcf07f float null, --剩余次数 gcf13i int null, --疗程间隔 gcf19i int null, --是否停用 2停用 1正常 3买产品赠送项目 gcf22i int null, --永久免费 startdate varchar(10) null -- 开始疗程日期 ) --会员卡--疗程 insert into #gcm06_date(gcf00c,gcf01c,gcf02c,gcf05f,gcf06f,gcf07f,gcf13i,gcf19i,gcf22i,startdate) select gcf00c,gcf01c,gcf02c,sum(gcf05f),sum(gcf06f),sum(gcf07f),min(gcf13i),min(gcf19i),min(gcf22i),'' from gcm06 where gcf00c = @company and gcf01c = @cardId and gcf19i = 1 ----是否停用 2停用 1正常 3买产品赠送项目 and gcf07f > 0 group by gcf00c,gcf01c,gcf02c --更新疗程日期 --取该项目第一次疗程日期 update a set a.startdate = b.gga94d from #gcm06_date a,( select gga00c, gga09c, --会员卡号 ggb03c, -- 项目编号 min(gga94d) gga94d from ggm01 inner join ggm02 on gga00c = ggb00c and gga01c = ggb01c where gga09c = @cardId and ggb47i <> -1 group by gga00c,gga09c,ggb03c ) as b where gcf00c = gga00c and gcf01c = gga09c and gcf02c = ggb03c --清掉没有日期的数据 delete from #gcm06_date where isnull(startdate,'') = '' or isnull(gcf13i,0) = 0 Create table #gbm19_date ( gbs00c varchar(10) not null, -- 门店编号 gbs01d varchar(10) not null, -- 时间 gbs02c varchar(30) not null, -- 会员卡号 gbs03c varchar(30) not null, -- 项目编号 gbs04i int not null, -- 完成状态(0未完成,1已完成,2未预约完成) gbs05f float null, -- 疗程总次数 gbs06f float null, -- 已用次数 gbs07f float null, -- 剩余次数 gbs08f float null, -- 当前完成次数 gbs09c varchar(30) null -- 消费单号 constraint PK_GBM19 primary key nonclustered(gbs00c,gbs01d,gbs02c,gbs03c) ) declare @datetime int select @datetime = convert(int,@startDate) while @datetime <= convert(int,@endDate) begin -- 更新今日疗程提醒表 insert into #gbm19_date(gbs00c,gbs01d,gbs02c,gbs03c,gbs04i,gbs05f,gbs06f,gbs07f,gbs08f,gbs09c) select gcf00c, convert(varchar(10),@datetime), gcf01c, gcf02c, 0, gcf05f, gcf06f, gcf07f, 0, '' from #gcm06_date where (isnull(gcf13i,0) != 0 And -datediff(dd,Convert(varchar(10),@datetime),convert(datetime,startdate)) % gcf13i = 0) select @datetime = @datetime + 1 end select gbs01d,gbs02c,gbs03c, (select gda03c from gdm01 where gda00c = gbs00c and gda01c = gbs03c) as gbs03cName, case gbs04i when 1 then '已完成' when 2 then '未预约完成' else '未完成' end gbs04i, gbs05f,gbs06f,gbs07f,gbs08f,gbs09c from #gbm19_date order by gbs01d asc drop table #gcm06_date drop table #gbm19_date end GO /****** Object: Table [dbo].[gcm06] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gcm06]( [gcf00c] [varchar](10) NOT NULL, [gcf01c] [varchar](20) NOT NULL, [gcf02c] [varchar](20) NOT NULL, [gcf03f] [float] NULL, [gcf04f] [float] NULL, [gcf05f] [float] NULL, [gcf06f] [float] NULL, [gcf07f] [float] NULL, [gcf08f] [float] NULL, [gcf09f] [float] NULL, [gcf10f] [float] NULL, [gcf11d] [varchar](8) NULL, [gcf12d] [varchar](8) NULL, [gcf13i] [int] NULL, [gcf14f] [float] NULL, [gcf15c] [varchar](60) NULL, [gcf16i] [int] NULL, [gcf17i] [int] NOT NULL, [gcf18f] [float] NOT NULL, [gcf19i] [int] NULL, [gcf20i] [int] NULL, [gcf21i] [int] NULL, [gcf22i] [int] NULL, [gcf23i] [int] IDENTITY(1,1) NOT NULL, [gcf24i] [int] NULL, [gcf25f] [float] NULL, [gcf26c] [nvarchar](2000) NULL, [gcf27i] [int] NOT NULL, [gcf28i] [bit] NOT NULL, CONSTRAINT [PK_GCM06] PRIMARY KEY NONCLUSTERED ( [gcf23i] ASC, [gcf00c] ASC, [gcf01c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[glm02] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[glm02]( [glb00c] [varchar](10) NOT NULL, [glb01c] [varchar](20) NOT NULL, [glb02c] [varchar](5) NOT NULL, [glb03c] [varchar](20) NOT NULL, [glb04c] [varchar](20) NOT NULL, CONSTRAINT [PK_GLM02] PRIMARY KEY CLUSTERED ( [glb00c] ASC, [glb01c] ASC, [glb02c] ASC, [glb03c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[lvm02] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[lvm02]( [gvb00c] [varchar](10) NOT NULL, [gvb01c] [varchar](20) NOT NULL, [gvb02f] [float] NOT NULL, [gvb03c] [varchar](20) NULL, [gvb04f] [float] NULL, [gvb05f] [float] NULL, [gvb06f] [float] NULL, [gvb07f] [float] NULL, [gvb08f] [float] NULL, [gvb09c] [varchar](10) NULL, [gvb10f] [float] NULL, [gvb11f] [float] NULL, [gvb12f] [float] NULL, [gvb13i] [int] NULL, CONSTRAINT [pk_lvm02] PRIMARY KEY CLUSTERED ( [gvb00c] ASC, [gvb01c] ASC, [gvb02f] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_select_CourseSubscription_nos] Script Date: 06/12/2017 06:20:39 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_select_CourseSubscription_nos] ( @compid varchar(20),--门店编号 @chk0 int, --排除死亡客户 4 @chk1 int, --排除休眠客户 3 @Department varchar(30), --部门 @day int --提前天数 ) as begin declare @nowdate varchar(10) --当天时间 Select @nowdate = CONVERT(varchar(10), GETDATE(), 112) create table #gcm06_date ( gcf00c varchar(10) not null, --门店编号 gcf01c varchar(20) not null, --卡号 gcf02c varchar(20) not null, --项目编号 gcf05f float null, --总次数 gcf06f float null, --已经使用次数 gcf07f float null, --剩余次数 gcf13i int null, --疗程间隔 gcf19i int null, --是否停用 2停用 1正常 3买产品赠送项目 gcf22i int null, --永久免费 startdate varchar(10) null, -- 开始疗程日期 laidian varchar(10) null, --最近来店 tel varchar(30) null, --手机号 ) --会员卡--疗程 insert into #gcm06_date(gcf00c,gcf01c,gcf02c,gcf05f,gcf06f,gcf07f,gcf13i,gcf19i,gcf22i,startdate,laidian) select gcf00c,gcf01c,gcf02c,sum(isnull(gcf05f,0)),sum(isnull(gcf06f,0)),sum(isnull(gcf07f,0)),min(isnull(gcf13i,7)),min(isnull(gcf19i,0)),min(isnull(gcf22i,0)),'','' from gcm06 where gcf00c = @compid and gcf19i = 1 ----是否停用 2停用 1正常 3买产品赠送项目 and gcf07f > 0 group by gcf00c,gcf01c,gcf02c --更新手机号 update a set tel = b.gba08c from #gcm06_date a,( select gba08c, --手机号 gba23c, --卡号 gba00c --门店 from gbm01 where gba23c in (select gcf01c from #gcm06_date) and isnull(gba08c,'') != '' ) as b where a.gcf00c = b.gba00c and a.gcf01c = b.gba23c --更新疗程日期 --取该项目最后一次疗程日期 update a set a.startdate = b.gga94d from #gcm06_date a,( select gga00c, gga09c, --会员卡号 ggb03c, max(gga94d) gga94d from ggm01 inner join ggm02 on gga00c = ggb00c and gga01c = ggb01c where gga00c = @compid and ggb47i <> -1 group by gga00c,gga09c,ggb03c ) as b where gcf00c = gga00c and gcf01c = gga09c and gcf02c = ggb03c and isnull(tel,'') = '' update a set a.startdate = b.gga94d from #gcm06_date a,( select gga00c, gba08c, --手机号 ggb03c, max(gga94d) gga94d from ggm01 inner join ggm02 on gga00c = ggb00c and gga01c = ggb01c inner join gbm01 on gba00c = gga00c and gga09c = gba23c where gga00c = @compid and ggb47i <> -1 group by gga00c,gba08c,ggb03c ) as b where gcf00c = gga00c and gcf02c = ggb03c and isnull(tel,'') != '' and tel = gba08c --最近来店 update a set a.laidian = b.gga94d from #gcm06_date a,( select gga00c, gba08c, --手机号 ggb03c, max(gga94d) gga94d from ggm01 inner join ggm02 on gga00c = ggb00c and gga01c = ggb01c inner join gbm01 on gba00c = gga00c and gga09c = gba23c where gga00c = @compid and isnull(gga94d,'') <> '' group by gga00c,gba08c,ggb03c ) as b where gcf00c = gga00c and gcf02c = ggb03c and isnull(tel,'') != '' and tel = gba08c --清掉没有日期的数据 delete from #gcm06_date where isnull(startdate,'') = '' or -datediff(dd,@nowdate,convert(datetime,startdate)) = 0 Create table #gbm19_cs ( gbs00c varchar(10) not null, -- 门店编号 gbs01d varchar(10) not null, -- 时间 gbs02c varchar(30) not null, -- 会员卡号 gbs03c varchar(30) not null, -- 项目编号 gbs04i int not null, -- 完成状态(0未完成,1已完成,2未预约完成) gbs05f float null, -- 疗程总次数 gbs06f float null, -- 已用次数 gbs07f float null, -- 剩余次数 gbs08f float null, -- 当前完成次数 gbs09c varchar(30) null, -- 消费单号 gbs10i int null, --多久没来(依疗程来计算) gbs11i int null --最近来店 constraint PK_GBM19 primary key nonclustered(gbs00c,gbs01d,gbs02c,gbs03c) ) -- 更新 insert into #gbm19_cs(gbs00c,gbs01d,gbs02c,gbs03c,gbs04i,gbs05f,gbs06f,gbs07f,gbs08f,gbs09c,gbs10i,gbs11i) select gcf00c, @nowdate, gcf01c, gcf02c, 0, gcf05f, gcf06f, gcf07f, 0, '', -datediff(dd,@nowdate,convert(datetime,startdate)), case isnull(laidian,'') when '' then -datediff(dd,@nowdate,convert(datetime,startdate)) else -datediff(dd,@nowdate,convert(datetime,laidian)) end from #gcm06_date where -datediff(dd,@nowdate,convert(datetime,startdate)) % (7 - @day) = 0 create table #result ( compid varchar(20), -- 门店编号 cardid varchar(20), -- 会员卡号 cardno varchar(20), -- 会员编号 cardname varchar(20), -- 会员卡姓名 --unfinishnum int , -- 应完成数 -- finishnum int , -- 已完成数 -- gbs04i int, -- 完成状态 juli float, -- 多久没来 laidian float, --最近来店 --cishu int, --来店次数 sex varchar(2) , -- 会员性别 birthdate varchar(8) , -- 出生日期 familytel varchar(20), -- 家庭电话 mobiletel varchar(20), -- 手机号码 companytel varchar(20), -- 公司电话 address varchar(40), -- 联系地址 fzren1 varchar(20), -- 负责人1 fzrename1 varchar(20), -- 负责人姓名1 dj1 int, -- 会员等级1 djcode1 varchar(20), -- 会员等级代码1 Depart1 varchar(20), -- 部门编号1 cardsection1 varchar(20), -- 会员归属部门1 fzren2 varchar(20), -- 负责人2 fzrename2 varchar(20), -- 负责人姓名2 dj2 int, -- 会员等级2 djcode2 varchar(20), -- 会员等级代码2 Depart2 varchar(20), -- 部门编号2 cardsection2 varchar(20), -- 会员归属部门2 fzren3 varchar(20), -- 负责人3 fzrename3 varchar(20), -- 负责人姓名3 dj3 int, -- 会员等级3 djcode3 varchar(20), -- 会员等级代码3 Depart3 varchar(20), -- 部门编号3 cardsection3 varchar(20), -- 会员归属部门3 fzren4 varchar(20), -- 负责人4 fzrename4 varchar(20), -- 负责人姓名4 dj4 int, -- 会员等级4 djcode4 varchar(20), -- 会员等级代码4 Depart4 varchar(20), -- 部门编号4 cardsection4 varchar(20), -- 会员归属部门4 ) insert into #result(compid,cardid,cardno,cardname,sex,birthdate,familytel,mobiletel,companytel,address) select gba00c, gba23c, --会员卡号 gba01c, -- 会员编号 gba03c, case gba14i when 0 then '女' when 1 then '男' else '未知' end sex, --性别 gba17d, --出生日期 gba07c, gba08c, gba13c, --公司电话 gba05c --地址 from gbm01 where gba00c = @compid and gba23c in ( select gbs02c from gbm19 where gbs00c in (select gaz02c from gam26 where gaz01c = @compid) and (@Department = '' or gbs03c in (select gda01c from gdm01 where gda00c = @compid and gda02c = @Department) ) --部门判断 group by gbs02c ) --清除已经在今日提醒中存在的 delete from #result where cardid in (select gbs02c from gbm19 where gbs00c = compid and gbs01d = @nowdate) --更新完成数 update a set juli = gbs10i, -- 多久没来 laidian = gbs11i -- 最近来店 from #result a,( select gbs00c, gbs02c, min(gbs10i) gbs10i, min(gbs11i) gbs11i from #gbm19_cs where @Department = '' or gbs03c in (select gda01c from gdm01 where gda00c = @compid and gda02c = @Department) group by gbs00c,gbs02c ) b where a.compid = b.gbs00c and a.cardid = b.gbs02c delete from #result where isnull(juli,'') = '' --更新会员等级,负责人 declare @gbr02c varchar(30) -- 所属部门 declare @intSn int set @intSn = 1 declare gbm18_cursor cursor for select gbr02c from gbm18 inner join #result on gbr00c = compid and gbr01c = cardno group by gbr02c open gbm18_cursor fetch next from gbm18_cursor into @gbr02c while(@@fetch_status = 0) begin if( @intSn = 1) begin update a set Depart1 = b.gbr02c, fzren1 = b.gbr03c, dj1 = b.gbr04i, cardsection1 = (select gsb03c from gsm02 where gsb00c = b.gbr00c and gsb01c = 'U' and gsb02c = b.gbr02c), fzrename1 = (select haa02c from ham01 where haa00c = b.gbr00c and haa01c = b.gbr03c), --负责人名称 djcode1 = (select gsb03c from gsm02 where gsb00c = b.gbr00c and gsb01c = 'DJ' and gsb02c = b.gbr04i) --会员等级 from #result a, gbm18 b where a.compid = b.gbr00c and a.cardno = b.gbr01c and b.gbr02c = @gbr02c end else if(@intSn = 2) begin update a set Depart2 = b.gbr02c, fzren2 = b.gbr03c, dj2 = b.gbr04i, cardsection2 = (select gsb03c from gsm02 where gsb00c = b.gbr00c and gsb01c = 'U' and gsb02c = b.gbr02c), fzrename2 = (select haa02c from ham01 where haa00c = b.gbr00c and haa01c = b.gbr03c), --负责人名称 djcode2 = (select gsb03c from gsm02 where gsb00c = b.gbr00c and gsb01c = 'DJ' and gsb02c = b.gbr04i) --会员等级 from #result a, gbm18 b where a.compid = b.gbr00c and a.cardno = b.gbr01c and b.gbr02c = @gbr02c end else if(@intSn = 3) begin update a set Depart3 = b.gbr02c, fzren3 = b.gbr03c, dj3 = b.gbr04i, cardsection3 = (select gsb03c from gsm02 where gsb00c = b.gbr00c and gsb01c = 'U' and gsb02c = b.gbr02c), fzrename3 = (select haa02c from ham01 where haa00c = b.gbr00c and haa01c = b.gbr03c), --负责人名称 djcode3 = (select gsb03c from gsm02 where gsb00c = b.gbr00c and gsb01c = 'DJ' and gsb02c = b.gbr04i) --会员等级 from #result a, gbm18 b where a.compid = b.gbr00c and a.cardno = b.gbr01c and b.gbr02c = @gbr02c end else if(@intSn = 4) begin update a set Depart4 = b.gbr02c, fzren4 = b.gbr03c, dj4 = b.gbr04i, cardsection4 = (select gsb03c from gsm02 where gsb00c = b.gbr00c and gsb01c = 'U' and gsb02c = b.gbr02c), fzrename4 = (select haa02c from ham01 where haa00c = b.gbr00c and haa01c = b.gbr03c), --负责人名称 djcode4 = (select gsb03c from gsm02 where gsb00c = b.gbr00c and gsb01c = 'DJ' and gsb02c = b.gbr04i) --会员等级 from #result a, gbm18 b where a.compid = b.gbr00c and a.cardno = b.gbr01c and b.gbr02c = @gbr02c end set @intSn = @intSn + 1 fetch next from gbm18_cursor into @gbr02c end close gbm18_cursor deallocate gbm18_cursor select compid, case isnull(gba08c,'') when '' then max(gcf01c) else gba08c end gba08c, -- 手机号码 max(gcf01c) gcf01c, -- 会员卡号 max(cardno) cardno, -- 会员编号 max(cardname) cardname, -- 会员卡姓名 min(juli) juli, min(laidian) laidian, max(sex) sex, max(birthdate) birthdate, max(familytel) familytel, max(companytel) companytel, max(address) address, max(fzren1) fzren1, max(fzrename1) fzrename1, max(dj1) dj1, max(djcode1) djcode1, max(Depart1) Depart1, max(cardsection1) cardsection1, max(fzren2) fzren2, max(fzrename2) fzrename2, max(dj2) dj2, max(djcode2) djcode2, max(Depart2) Depart2, max(cardsection2) cardsection2, max(fzren3) fzren3, max(fzrename3) fzrename3, max(dj3) dj3, max(djcode3) djcode3, max(Depart3) Depart3, max(cardsection3) cardsection3, max(fzren3) fzren4, max(fzrename3) fzrename4, max(dj3) dj4, max(djcode3) djcode4, max(Depart3) Depart4, max(cardsection3) cardsection4 from ( select compid, cardid as gcf01c, -- 会员卡号 cardno, cardname, juli, isnull(laidian,0) laidian, sex, birthdate, familytel, case isnull(mobiletel,'') when '' then cardid else mobiletel end as gba08c, -- 手机号码 companytel, address, fzren1, fzrename1, dj1, djcode1, Depart1, cardsection1, fzren2, fzrename2, dj2, djcode2, Depart2, cardsection2, fzren3, fzrename3, dj3, djcode3, Depart3, cardsection3, fzren4, fzrename4, dj4, djcode4, Depart4, cardsection4 from #result where (@Department = '' or ( isnull(Depart1,'') = @Department and (@chk0 = '' or isnull(dj1,'') != @chk0) and (@chk1 = '' or isnull(dj1,'') != @chk1) )) or (@Department = '' or ( isnull(Depart2,'') = @Department and (@chk0 = '' or isnull(dj2,'') != @chk0) and (@chk1 = '' or isnull(dj2,'') != @chk1) )) or (@Department = '' or ( isnull(Depart3,'') = @Department and (@chk0 = '' or isnull(dj3,'') != @chk0) and (@chk1 = '' or isnull(dj3,'') != @chk1) )) )as Atable group by compid,gba08c drop table #gbm19_cs drop table #result end GO /****** Object: Table [dbo].[ham01] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[ham01]( [haa00c] [varchar](10) NOT NULL, [haa01c] [varchar](10) NOT NULL, [haa02c] [varchar](20) NULL, [haa03c] [varchar](20) NULL, [haa04c] [varchar](20) NULL, [haa05i] [int] NULL, [haa06c] [varchar](10) NULL, [haa07c] [varchar](5) NULL, [haa08d] [varchar](8) NULL, [haa09d] [varchar](8) NULL, [haa10c] [varchar](5) NULL, [haa11d] [varchar](8) NULL, [haa12c] [varchar](20) NULL, [haa13c] [varchar](5) NULL, [haa14d] [varchar](8) NULL, [haa15f] [float] NULL, [haa16f] [float] NULL, [haa17c] [varchar](10) NULL, [haa18c] [varchar](160) NULL, [haa19c] [varchar](20) NULL, [haa20c] [varchar](20) NULL, [haa21c] [varchar](20) NULL, [haa22c] [varchar](20) NULL, [haa23c] [varchar](20) NULL, [haa24c] [varchar](100) NULL, [haa25c] [varchar](5) NULL, [haa26c] [varchar](20) NULL, [haa27c] [varchar](1) NULL, [haa28i] [int] NULL, [haa29f] [float] NULL, [haa30f] [float] NULL, [haa31f] [float] NULL, [haa32c] [varchar](500) NULL, [haa91c] [varchar](10) NULL, [haa92d] [varchar](8) NULL, [haa33c] [varchar](30) NULL, [haa34c] [varchar](20) NULL, [haa35c] [varchar](30) NULL, [haa36c] [varchar](30) NULL, [haa37c] [varchar](160) NULL, [haa38c] [varchar](20) NULL, [haa39i] [int] NULL, [haa40i] [int] NULL, [haa41i] [int] NULL, [haa42f] [float] NULL, [haa43f] [float] NULL, [haa44c] [varchar](30) NULL, [haa45d] [datetime] NULL, [haa46i] [int] NULL, [haa47i] [int] NULL, [haa49i] [int] NULL, [haa90c] [varchar](100) NULL, [haa99c] [varchar](1000) NULL, [ownhaa01c] [varchar](10) NOT NULL, [ownhaa00c] [varchar](10) NOT NULL, [haa50c] [varchar](60) NULL, [haa51c] [varchar](100) NULL, [haa52c] [varchar](500) NULL, CONSTRAINT [PK_HAM01] PRIMARY KEY CLUSTERED ( [haa00c] ASC, [haa01c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_query_everydayAlert_02] Script Date: 06/12/2017 06:20:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_query_everydayAlert_02] ( @companyId varchar(30), --公司编号 @Department varchar(30), --部门 @chk0 varchar(2), --排除死亡客户 4 @chk1 varchar(2), --排除休眠客户 3 @mark varchar(2), --标识 0:系统自查询 1:自查询 @expired varchar(2), --包含过期数据 0:不包含 1:包含 @days int --将要过期的天数 ) as begin create table #gam23_today ( gau00c varchar(10), --发送到那个公司 gau01c varchar(5) , gau03c varchar(20), --疗程编号 gau11c varchar(30), --项目编号 gau11cName varchar(60), --项目名称 gau04c varchar(20), --会员姓名 cardid varchar(60), --会员卡号 juli int , --多久没来 gau05d varchar(8) , --日期 gau06f float , --06-疗程剩余次数/05-卡剩余金额/11-产品数量 gau07c varchar(20), --家庭电话 gau08c varchar(20), --移动电话 gau09c varchar(160), --地址 fzren1 varchar(20), -- 负责人1 fzrename1 varchar(20), -- 负责人姓名1 dj1 int, -- 会员等级1 djcode1 varchar(20), -- 会员等级代码1 Depart1 varchar(20), -- 部门编号1 cardsection1 varchar(20), -- 会员归属部门1 fzren2 varchar(20), -- 负责人2 fzrename2 varchar(20), -- 负责人姓名2 dj2 int, -- 会员等级2 djcode2 varchar(20), -- 会员等级代码2 Depart2 varchar(20), -- 部门编号2 cardsection2 varchar(20), -- 会员归属部门2 fzren3 varchar(20), -- 负责人3 fzrename3 varchar(20), -- 负责人姓名3 dj3 int, -- 会员等级3 djcode3 varchar(20), -- 会员等级代码3 Depart3 varchar(20), -- 部门编号3 cardsection3 varchar(20), -- 会员归属部门3 fzren4 varchar(20), -- 负责人4 fzrename4 varchar(20), -- 负责人姓名4 dj4 int, -- 会员等级4 djcode4 varchar(20), -- 会员等级代码4 Depart4 varchar(20), -- 部门编号4 cardsection4 varchar(20), -- 会员归属部门4 ) --系统首次加载 或者是自查询且查询天数为0 时 取系统默认配置天数 查询 gam23 公告发布和各种提醒内容表 if( @mark = '0') begin insert into #gam23_today(gau00c,gau01c,gau03c,gau11c,gau11cName,gau04c,gau05d,gau06f,gau08c,gau07c,gau09c) select gau00c, gau01c,gau03c, gau11c, gau11cName = (select gda03c from gdm01 where gda00c = gau00c And gda01c = gau11c), --项目名称 gau04c, gau05d, gau06f, gau08c, --移动电话 gau07c, gau09c from gam23 where gau00c in (select gaz02c from gam26 where gaz01c = @companyId) and gau01c in ('6','21') and ( @Department = '' or gau11c in (select gda01c from gdm01 where gda00c = @companyId and gda02c = @Department) ) --部门判断 end --自查询 else begin insert into #gam23_today(gau00c,gau01c,gau03c,gau11c,gau11cName,gau04c,gau05d,gau06f,gau08c,gau07c,gau09c) select distinct gcf00c gau00c, '21' gau01c, gcf01c gau03c, gcf02c gau11c, gau11cName = (select gda03c from gdm01 where gda00c = gcf00c And gda01c = gcf02c), --项目名称 cast(gba03c as varchar(20)) gau04c, gcf12d gau05d, gcf10f gau06f, gba08c gau08c, gba07c gau07c, gba05c gau09c from gcm06 a with (nolock), gbm01 b with (nolock),gam26 c with (nolock),gdm01 d with (nolock) where a.gcf00c = b.gba00c and a.gcf01c = b.gba23c and a.gcf19i<>2 and a.gcf27i = 0 and (a.gcf00c = @companyId or @companyId = '001') and a.gcf12d = dateadd(day,@days,convert(varchar(12),getdate(),112)) and a.gcf28i = 0 and b.gba00c = c.gaz02c and c.gaz01c = @companyId and ( @Department = '' or gcf02c in (select gda01c from gdm01 where gda00c = @companyId and gda02c = @Department) ) --部门判断 end --包含已过期 if (@expired = '1') begin insert into #gam23_today(gau00c,gau01c,gau03c,gau11c,gau11cName,gau04c,gau05d,gau06f,gau08c,gau07c,gau09c) select distinct gcf00c gau00c, '21' gau01c, gcf01c gau03c, gcf02c gau11c, --gau11cName = (select gda03c from gdm01 where gda00c = gcf00c And gda01c = gcf02c), --项目名称 d.gda03c gau11cName, cast(gba03c as varchar(20)) gau04c, gcf12d gau05d, gcf10f gau06f, gba08c gau08c, gba07c gau07c, gba05c gau09c from gcm06 a with (nolock), gbm01 b with (nolock),gam26 c with (nolock),gdm01 d with (nolock) where a.gcf00c = b.gba00c and a.gcf01c = b.gba23c and a.gcf19i<>2 and a.gcf27i = 0 and (a.gcf00c = @companyId or @companyId = '001') and a.gcf12d < convert(varchar(12),getdate(),112) and a.gcf28i = 0 and b.gba00c = c.gaz02c and c.gaz01c = @companyId and d.gda00c = @companyId and d.gda01c = a.gcf02c and ( @Department = '' or gcf02c in (select gda01c from gdm01 where gda00c = @companyId and gda02c = @Department) ) --部门判断 end --更新 多久没来 Update a set a.juli = b.juli, a.cardid = b.gga05c from #gam23_today a,(select ggb00c,gba08c,min(juli) juli,max(gga05c) gga05c from(select ggb00c, gga05c, --会员卡号 -datediff(dd,getdate(),convert(datetime,max(gga94d))) juli --多久没来店 from ggm02 inner join ggm01 on ggb00c = gga00c and ggb01c = gga01c inner join gdm01 on gdm01.gda00c = ggm02.ggb00c And gdm01.gda01c = ggm02.ggb03c where ggb00c in (select gaz02c from gam26 where gaz01c = @companyId) group by ggb00c,gga05c ) as a inner join (select gba00c, gba08c, -- 手机 gba23c -- 会员卡号 from gbm01 where gba00c in (select gaz02c from gam26 where gaz01c = @companyId) and gba08c in (select gau08c from #gam23_today) ) as b on a.ggb00c = b.gba00c and a.gga05c = b.gba23c group by ggb00c,gba08c ) b where gau00c = ggb00c and gau08c = gba08c --更新会员等级,负责人 declare @gbr02c varchar(30) -- 所属部门 declare @intSn int set @intSn = 1 declare gam23_cursor cursor for select gbr02c from gbm18 inner join #gam23_today on gbr00c = gau00c and gbr01c = cardid group by gbr02c open gam23_cursor fetch next from gam23_cursor into @gbr02c while(@@fetch_status = 0) begin if( @intSn = 1) begin update a set Depart1 = b.gbr02c, fzren1 = b.gbr03c, dj1 = b.gbr04i, cardsection1 = (select gsb03c from gsm02 where gsb00c = b.gbr00c and gsb01c = 'U' and gsb02c = b.gbr02c), fzrename1 = (select haa02c from ham01 where haa00c = b.gbr00c and haa01c = b.gbr03c), --负责人名称 djcode1 = (select gsb03c from gsm02 where gsb00c = b.gbr00c and gsb01c = 'DJ' and gsb02c = b.gbr04i) --会员等级 from #gam23_today a, gbm18 b where a.gau00c = b.gbr00c and a.cardid = b.gbr01c and b.gbr02c = @gbr02c end else if(@intSn = 2) begin update a set Depart2 = b.gbr02c, fzren2 = b.gbr03c, dj2 = b.gbr04i, cardsection2 =(select gsb03c from gsm02 where gsb00c = b.gbr00c and gsb01c = 'U' and gsb02c = b.gbr02c), fzrename2 = (select haa02c from ham01 where haa00c = b.gbr00c and haa01c = b.gbr03c), --负责人名称 djcode2 = (select gsb03c from gsm02 where gsb00c = b.gbr00c and gsb01c = 'DJ' and gsb02c = b.gbr04i) --会员等级 from #gam23_today a, gbm18 b where a.gau00c = b.gbr00c and a.cardid = b.gbr01c and b.gbr02c = @gbr02c end else if(@intSn = 3) begin update a set Depart3 = b.gbr02c, fzren3 = b.gbr03c, dj3 = b.gbr04i, cardsection3 = (select gsb03c from gsm02 where gsb00c = b.gbr00c and gsb01c = 'U' and gsb02c = b.gbr02c), fzrename3 = (select haa02c from ham01 where haa00c = b.gbr00c and haa01c = b.gbr03c), --负责人名称 djcode3 = (select gsb03c from gsm02 where gsb00c = b.gbr00c and gsb01c = 'DJ' and gsb02c = b.gbr04i) --会员等级 from #gam23_today a, gbm18 b where a.gau00c = b.gbr00c and a.cardid = b.gbr01c and b.gbr02c = @gbr02c end else if(@intSn = 4) begin update a set Depart4 = b.gbr02c, fzren4 = b.gbr03c, dj4 = b.gbr04i, cardsection4 = (select gsb03c from gsm02 where gsb00c = b.gbr00c and gsb01c = 'U' and gsb02c = b.gbr02c), fzrename4 = (select haa02c from ham01 where haa00c = b.gbr00c and haa01c = b.gbr03c), --负责人名称 djcode4 = (select gsb03c from gsm02 where gsb00c = b.gbr00c and gsb01c = 'DJ' and gsb02c = b.gbr04i) --会员等级 from #gam23_today a, gbm18 b where a.gau00c = b.gbr00c and a.cardid = b.gbr01c and b.gbr02c = @gbr02c end set @intSn = @intSn + 1 fetch next from gam23_cursor into @gbr02c end close gam23_cursor deallocate gam23_cursor select * from #gam23_today where (@Department = '' or ( isnull(Depart1,'') = @Department and (@chk0 = '' or isnull(dj1,'') != @chk0) and (@chk1 = '' or isnull(dj1,'') != @chk1) )) or (@Department = '' or ( isnull(Depart2,'') = @Department and (@chk0 = '' or isnull(dj2,'') != @chk0) and (@chk1 = '' or isnull(dj2,'') != @chk1) )) or (@Department = '' or ( isnull(Depart3,'') = @Department and (@chk0 = '' or isnull(dj3,'') != @chk0) and (@chk1 = '' or isnull(dj3,'') != @chk1) )) or (@Department = '' or ( isnull(Depart4,'') = @Department and (@chk0 = '' or isnull(dj4,'') != @chk0) and (@chk1 = '' or isnull(dj4,'') != @chk1) )) order by gau00c asc,gau05d desc drop table #gam23_today end GO /****** Object: Table [dbo].[remind_setting] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[remind_setting]( [rsa00c] [varchar](10) NOT NULL, [rsa01c] [varchar](20) NOT NULL, [rsa02i] [int] NULL, [rsa03f] [float] NULL, [rsa04f] [float] NULL, [rsa05f] [float] NULL, [rsa06f] [float] NULL, [rsa10c] [varchar](500) NULL, CONSTRAINT [PK_remind_setting] PRIMARY KEY CLUSTERED ( [rsa00c] ASC, [rsa01c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gcm07] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gcm07]( [gcg00c] [varchar](10) NOT NULL, [gcg01c] [varchar](20) NOT NULL, [gcg02c] [varchar](20) NOT NULL, [gcg03f] [float] NULL, [gcg04f] [float] NULL, [gcg05f] [float] NULL, [gcg06f] [float] NULL, [gcg07f] [float] NULL, [gcg08f] [float] NULL, [gcg09f] [float] NULL, [gcg10f] [float] NULL, CONSTRAINT [PK_GCM07] PRIMARY KEY CLUSTERED ( [gcg00c] ASC, [gcg01c] ASC, [gcg02c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[ham02] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[ham02]( [hab00c] [varchar](10) NOT NULL, [hab01c] [varchar](10) NOT NULL, [hab02f] [float] NOT NULL, [hab03c] [varchar](20) NULL, [hab04c] [varchar](20) NULL, [hab05d] [varchar](8) NULL, [hab06d] [varchar](8) NULL, [hab07c] [varchar](5) NULL, [hab08i] [int] NULL, [hab09c] [varchar](10) NULL, [hab10c] [varchar](40) NULL, CONSTRAINT [PK_HAM02] PRIMARY KEY CLUSTERED ( [hab00c] ASC, [hab01c] ASC, [hab02f] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gcm08] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gcm08]( [gch00c] [varchar](10) NOT NULL, [gch01i] [int] IDENTITY(1,1) NOT NULL, [gch02d] [varchar](8) NULL, [gch03c] [varchar](20) NOT NULL, [gch04c] [varchar](20) NOT NULL, [gch05f] [float] NULL, [gch06f] [float] NULL, [gch07f] [float] NULL, [gch08f] [float] NULL, [gch09f] [float] NULL, [gch10c] [varchar](20) NULL, [gch11c] [varchar](20) NULL, [gch12i] [int] NULL, [gch13f] [float] NULL, [gch14f] [float] NULL, [gch15f] [float] NULL, [gch16i] [int] NULL, [gch17c] [varchar](5) NULL, [gch18f] [float] NULL, [gch19c] [varchar](5) NULL, [gch20f] [float] NULL, [gch21c] [varchar](5) NULL, [gch22f] [float] NULL, [gch23f] [float] NULL, [gch24c] [varchar](20) NULL, [gch25i] [int] NULL, [gch26f] [float] NULL, [gch27c] [varchar](5) NULL, [gch28f] [float] NULL, [gch29c] [nvarchar](2000) NULL, CONSTRAINT [PK_GCM08] PRIMARY KEY NONCLUSTERED ( [gch01i] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_member_compose_analyze] Script Date: 06/12/2017 06:20:23 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_member_compose_analyze] ( @gaz01c varchar(20), @fromdate varchar(10), @todate varchar(20) ) as-- WUXM 2002/02/4 AM 11:20 begin create table #temp_1 ( ageperiod char(1) null, --年龄层次 mannum float null --男子人数 ) create table #temp_2 ( ageperiod char(1) null, --年龄层次 womannum float null --女子人数 ) create table #temp_3 ( ageperiod char(1) null, --年龄层次 mannum float null, --男子人数 womannum float null, --女子人数 manpercent float null, --男子比例 womanpercent float null --女子比例 ) Create table #table_gbm01 ( sn int, coun int, yy int ) insert into #table_gbm01(sn,coun,yy) select gba14i as sn,count(*) as coun,datediff(year, gba17d, getdate()) as yy from gbm01,gcm01 where gba00c = gca00c and gba01c = gca04c and gba00c in (select gaz02c from gam26 where gaz01c = @gaz01c) and gca06d between @fromdate and @todate and isdate(gba17d) = 1 group by gba14i,gba17d insert into #temp_1(ageperiod, mannum) select '1',coun from #table_gbm01 where sn = 1 and yy <=20 insert into #temp_1(ageperiod, mannum) select '2',coun from #table_gbm01 where sn = 1 and yy > 20 and yy <=30 insert into #temp_1(ageperiod, mannum) select '3',coun from #table_gbm01 where sn = 1 and yy > 30 and yy <= 40 insert into #temp_1(ageperiod, mannum) select '4',coun from #table_gbm01 where sn = 1 and yy > 40 and yy <= 50 insert into #temp_1(ageperiod, mannum) select '5',coun from #table_gbm01 where sn = 1 and yy > 50 --写入#temp_2 insert into #temp_2(ageperiod, womannum) select '1',coun from #table_gbm01 where sn = 0 and yy <=20 insert into #temp_2(ageperiod, womannum) select '2',coun from #table_gbm01 where sn = 0 and yy > 20 and yy <=30 insert into #temp_2(ageperiod, womannum) select '3',coun from #table_gbm01 where sn = 0 and yy > 30 and yy <= 40 insert into #temp_2(ageperiod, womannum) select '4',coun from #table_gbm01 where sn = 0 and yy > 40 and yy <= 50 insert into #temp_2(ageperiod, womannum) select '5',coun from #table_gbm01 where sn = 0 and yy > 50 declare @mannum float declare @womannum float declare @totalnum float select @mannum = sum(mannum) from #temp_1 --对男子人数进行加总 select @womannum = sum(womannum) from #temp_2 --对女子人数进行加总 set @totalnum = isnull(@mannum,0) + isnull(@womannum,0) --得到总人数 if @totalnum >0 begin if exists(select 1 from #temp_1) begin insert into #temp_3(ageperiod, mannum, manpercent) select ageperiod, mannum, round(isnull(mannum,0)/@totalnum, 5) from #temp_1 --先写入男子人数及比例 update #temp_3 set #temp_3.womannum = #temp_2.womannum, #temp_3.womanpercent = round(isnull(#temp_2.womannum,0)/@totalnum, 5) from #temp_2, #temp_3 where #temp_3.ageperiod = #temp_2.ageperiod --再写入女子人数及比例 end else begin insert into #temp_3(ageperiod, womannum, womanpercent) select ageperiod, womannum, round(isnull(womannum,0)/@totalnum, 5) from #temp_2 update #temp_3 set #temp_3.mannum = #temp_1.mannum, #temp_3.manpercent = round(isnull(#temp_1.mannum,0)/@totalnum, 5) from #temp_1, #temp_3 where #temp_3.ageperiod = #temp_1.ageperiod end end --返回结果集 select --isnull(datatime, '') datatime, isnull(ageperiod, '') ageperiod, isnull(mannum, 0) mannum, isnull(womannum, 0) womannum, isnull(manpercent, 0) manpercent, isnull(womanpercent, 0) womanpercent from #temp_3 order by ageperiod asc drop table #temp_1 drop table #temp_2 drop table #temp_3 drop table #table_gbm01 end GO /****** Object: Table [dbo].[wmc] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[wmc]( [wmc00] [int] IDENTITY(1,1) NOT NULL, [wmc01v] [varchar](200) NULL, [wmc02v] [varchar](20) NULL, [wmc03v] [varchar](20) NULL, [wmc04v] [varchar](20) NULL, [wmc05v] [varchar](20) NULL, [wmc06v] [varchar](20) NULL, [wmc07i] [int] NULL, CONSTRAINT [PK_WMC] PRIMARY KEY CLUSTERED ( [wmc00] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[glm04] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[glm04]( [gld00c] [varchar](10) NOT NULL, [gld01c] [varchar](20) NOT NULL, [gld02c] [varchar](2) NOT NULL, [gld03f] [float] NOT NULL, [gld04c] [varchar](40) NULL, [gld05c] [varchar](20) NULL, CONSTRAINT [PK_GLM04] PRIMARY KEY CLUSTERED ( [gld00c] ASC, [gld01c] ASC, [gld02c] ASC, [gld03f] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[ham03] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[ham03]( [hac00c] [varchar](10) NOT NULL, [hac01c] [varchar](10) NOT NULL, [hac02f] [float] NOT NULL, [hac03c] [varchar](20) NULL, [hac04c] [varchar](20) NULL, [hac05d] [varchar](8) NULL, [hac06d] [varchar](8) NULL, [hac07i] [int] NULL, [hac08c] [varchar](20) NULL, [hac09f] [float] NULL, [hac10c] [varchar](200) NULL, CONSTRAINT [PK_HAM03] PRIMARY KEY CLUSTERED ( [hac00c] ASC, [hac01c] ASC, [hac02f] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gam01] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gam01]( [gaa01c] [varchar](10) NOT NULL, [gaa02c] [varchar](60) NULL, [gaa03c] [varchar](5) NULL, [gaa04c] [varchar](10) NULL, [gaa05c] [varchar](20) NULL, [gaa06c] [varchar](20) NULL, CONSTRAINT [PK_GAM01] PRIMARY KEY CLUSTERED ( [gaa01c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[wem] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[wem]( [wem00] [int] NOT NULL, [wem01v] [varchar](50) NULL, [wem02v] [text] NULL, [wem03v] [varchar](200) NULL, [wem04v] [varchar](20) NULL, [wem05v] [varchar](20) NULL, [wem06v] [varchar](20) NULL, [wem07i] [int] NULL, [wem08v] [varchar](50) NULL, [wem09b] [image] NULL, [wem10v] [varchar](20) NULL, [wem11v] [varchar](20) NULL, CONSTRAINT [PK_WEM] PRIMARY KEY CLUSTERED ( [wem00] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[glm05] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[glm05]( [gle00c] [varchar](10) NOT NULL, [gle01c] [varchar](20) NOT NULL, [gle02c] [varchar](2) NOT NULL, [gle03f] [float] NOT NULL, [gle04c] [varchar](40) NULL, CONSTRAINT [PK_GLM05] PRIMARY KEY CLUSTERED ( [gle00c] ASC, [gle01c] ASC, [gle02c] ASC, [gle03f] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gcm10] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gcm10]( [gcl00c] [varchar](10) NOT NULL, [gcl01c] [varchar](20) NOT NULL, [gcl02d] [varchar](8) NOT NULL, [gcl03c] [varchar](20) NULL, [gcl04c] [varchar](5) NULL, [gcl05c] [varchar](20) NULL, [gcl06i] [int] NULL, [gcl07f] [float] NULL, [gcl08f] [float] NULL, [gcl09c] [varchar](20) NULL, [gcl10c] [varchar](3) NULL, [gcl11c] [varchar](2) NULL, [gcl12c] [varchar](20) NULL, [gcl13f] [float] NULL, [gcl91c] [varchar](10) NULL, [gcl92d] [varchar](8) NULL, [gcl93c] [varchar](10) NULL, [gcl94d] [varchar](8) NULL, [gcl14i] [int] NULL, [gcl15c] [varchar](5) NULL, [gcl16f] [float] NULL, [gcl17c] [varchar](20) NULL, [gcl18f] [float] NULL, [gcl19c] [varchar](20) NULL, [gcl20f] [float] NULL, [gcl21d] [varchar](8) NULL, [gcl22c] [varchar](20) NULL, [gcl23f] [float] NULL, [gcl24c] [varchar](20) NULL, [gcl25f] [float] NULL, [gcl26c] [varchar](20) NULL, [gcl27f] [float] NULL, [gcl28c] [varchar](20) NULL, [gcl29f] [float] NULL, [gcl30c] [varchar](20) NULL, [gcl31f] [float] NULL, [gcl32c] [varchar](20) NULL, [gcl33f] [float] NULL, [gcl34c] [varchar](20) NULL, [gcl35f] [float] NULL, [gcl36c] [varchar](20) NULL, [gcl37f] [float] NULL, [gcl38c] [varchar](20) NULL, [gcl39f] [float] NULL, [gcl40c] [varchar](20) NULL, [gcl41f] [float] NULL, [gcl42c] [varchar](20) NULL, [gcl43f] [float] NULL, [gcl44c] [varchar](20) NULL, [gcl45f] [float] NULL, [gcl46c] [varchar](20) NULL, [gcl47f] [float] NULL, [gcl48c] [varchar](20) NULL, [gcl49f] [float] NULL, [gcl50c] [varchar](20) NULL, [gcl51f] [float] NULL, [gcl52c] [varchar](20) NULL, [gcl53f] [float] NULL, [gcl54c] [varchar](20) NULL, [gcl55f] [float] NULL, [gcl56t] [varchar](6) NULL, [gcl80d] [varchar](8) NULL, [gcl57c] [varchar](120) NULL, [gcl58f] [float] NULL, [gcl59f] [float] NULL, [gcl60f] [float] NULL, [gcl61f] [float] NULL, [gcl62f] [float] NULL, [gcl63f] [float] NULL, [gcl64f] [float] NULL, [gcl65f] [float] NULL, [gcl66f] [float] NULL, [gcl67c] [varchar](5) NULL, [gcl68f] [float] NULL, [gcl69c] [varchar](5) NULL, [gcl70f] [float] NULL, [gcl71c] [varchar](5) NULL, [gcl72f] [float] NULL, [gcl73c] [varchar](5) NULL, [gcl74f] [float] NULL, [gcl81c] [varchar](10) NULL, [gcl82c] [varchar](10) NULL, [gcl83c] [varchar](10) NULL, [gcl83f] [float] NULL, [gcl84f] [float] NULL, [gcl85f] [float] NULL, [gcl86f] [float] NULL, [gcl87f] [float] NULL, [gcl88f] [float] NULL, [gcl89f] [float] NULL, [gcl99i] [int] NULL, [gcl100c] [varchar](10) NULL, [gcl101c] [varchar](10) NULL, [gcl102c] [varchar](10) NULL, [gcl103c] [varchar](10) NULL, [gcl104c] [varchar](10) NULL, [gcl105c] [varchar](10) NULL, [gcl106c] [varchar](10) NULL, [gcl107c] [varchar](10) NULL, [gcl108f] [float] NULL, [gcl109c] [varchar](50) NULL, [gcl110c] [varchar](10) NULL, [gcl111i] [int] NULL, [gcl112i] [float] NULL, CONSTRAINT [PK_GCM10] PRIMARY KEY NONCLUSTERED ( [gcl00c] ASC, [gcl01c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[glm06] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[glm06]( [glf00c] [varchar](10) NOT NULL, [glf01c] [varchar](20) NOT NULL, [glf02c] [varchar](20) NULL, [glf03v] [varchar](20) NOT NULL, [glf04v] [varchar](20) NOT NULL, [glf05i] [int] NULL, [glf06t] [varchar](6) NULL, [glf07t] [varchar](6) NULL, [glf08d] [varchar](8) NULL, [glf09c] [varchar](20) NULL, [glf10c] [varchar](20) NULL, [glf11c] [varchar](20) NULL, CONSTRAINT [PK_GLM06] PRIMARY KEY CLUSTERED ( [glf00c] ASC, [glf01c] ASC, [glf03v] ASC, [glf04v] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_searchgood_outinfo] Script Date: 06/12/2017 06:20:39 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_searchgood_outinfo] @compid varchar(10), @fromgoods varchar(20), @togoods varchar(20), @fromdate varchar(8), @todate varchar(8) as begin create table #allout ( id int identity not null, compid varchar(10) null, --公司别 goodsno varchar(20) null, --产品编号 type int null, --出库类型(1- 正常出库 2- 盘点冲抵 3-供应商退货) quantity float null, --出库数量 date varchar(8) null, --出库日期 ) create table #outresult ( id int identity not null, compid varchar(10) null, --公司别 goodsno varchar(20) null, --产品编号 type int null, --出库类型(1- 正常出库 2- 盘点冲抵 3-供应商退货) currday float null, --当天出库 currmonth float null, --当月出库 currsum float null, --累计 allday float null, --小记部分的当天出库 allmonth float null, --小记部分的当月出库 allsum float null, --小记部分的累计 quantity float null, --出库数量 date varchar(8) null, --出库日期 ) ---一下是处理出库了---因为处理累计的部分所以过滤条件必须按照年来过滤 insert into #allout(compid, goodsno, quantity, date, type) select gia00c, gib03c, gib07f, gia03d, gia07i from gim01 with (nolock), gim02 with (nolock) where gia00c = gib00c and gia01c = gib01c and (gib03c between @fromgoods and @togoods or @fromgoods = '*') and gia00c = @compid and substring(gia03d, 1, 4) = substring(@fromdate, 1, 4) --插入当天库存 insert into #outresult(compid, goodsno, type, currday) select compid, goodsno, type, sum(quantity) from #allout where date = @fromdate group by compid, goodsno, type --跟新当月库存 update a set a.currmonth = (select sum(b.quantity) from #allout b where a.goodsno = b.goodsno and a.compid = b.compid and a.type = b.type and substring(a.date, 1, 4) = substring(@fromdate, 1, 6) group by compid, goodsno, type, date ) from #outresult a --跟新累计 update a set a.currsum = (select sum(b.quantity) from #allout b where a.goodsno = b.goodsno and a.type = b.type and a.date <= @todate group by compid, goodsno, type) from #outresult a --跟新小记的当天出库 update a set a.allday = (select sum(b.currday) from #outresult b where a.goodsno = b.goodsno group by goodsno) from #outresult a --跟新小记的当天出库 update a set a.allmonth = (select sum(b.currmonth) from #outresult b where a.goodsno = b.goodsno group by goodsno) from #outresult a --跟新小记的当天出库 update a set a.allsum = (select sum(b.currsum) from #outresult b where a.goodsno = b.goodsno group by goodsno) from #outresult a select * from #outresult order by goodsno, type end GO /****** Object: Table [dbo].[gam02] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gam02]( [gab01c] [varchar](10) NOT NULL, [gab02c] [varchar](5) NOT NULL, [gab03c] [varchar](20) NOT NULL, [gab04c] [varchar](20) NULL, [gab06c] [char](1) NULL, CONSTRAINT [PK_GAM02] PRIMARY KEY CLUSTERED ( [gab01c] ASC, [gab02c] ASC, [gab03c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[wmb] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[wmb]( [wmb00] [int] IDENTITY(1,1) NOT NULL, [wmb01v] [varchar](20) NULL, [wmb02i] [int] NULL, [wmb03i] [int] NULL, [wmb04i] [int] NULL, [wmb05v] [varchar](20) NULL, CONSTRAINT [PK_WMB] PRIMARY KEY CLUSTERED ( [wmb00] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[lym01] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[lym01]( [gya00c] [varchar](10) NOT NULL, [gya01c] [varchar](20) NOT NULL, [gya02d] [varchar](8) NULL, [gya03c] [varchar](300) NULL, [gya04i] [int] NULL, [gya05t] [varchar](6) NULL, [gya06c] [varchar](40) NULL, [gya07f] [float] NULL, [gya08f] [float] NULL, [gya91c] [varchar](10) NULL, [gya92d] [varchar](8) NULL, [gya93c] [varchar](10) NULL, [gya94d] [varchar](8) NULL, CONSTRAINT [PK_LYM01] PRIMARY KEY CLUSTERED ( [gya00c] ASC, [gya01c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[ham04] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[ham04]( [had00c] [varchar](10) NOT NULL, [had01c] [varchar](10) NOT NULL, [had02c] [varchar](5) NOT NULL, [had03d] [varchar](8) NULL, [had04d] [varchar](8) NULL, [had05i] [int] NULL, [had06c] [varchar](20) NULL, [had07c] [varchar](20) NULL, CONSTRAINT [PK_HAM04] PRIMARY KEY CLUSTERED ( [had00c] ASC, [had01c] ASC, [had02c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[lym02] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[lym02]( [gyb00c] [varchar](10) NOT NULL, [gyb01c] [varchar](20) NOT NULL, [gyb02f] [float] NOT NULL, [gyb03c] [varchar](20) NULL, [gyb04c] [varchar](5) NULL, [gyb05f] [float] NULL, [gyb06c] [varchar](5) NULL, [gyb07f] [float] NULL, [gyb08f] [float] NULL, [gyb09f] [float] NULL, [gyb10f] [float] NULL, [gyb11f] [float] NULL, [gyb12f] [float] NULL, [gyb13i] [int] NULL, CONSTRAINT [PK_LYM02] PRIMARY KEY CLUSTERED ( [gyb00c] ASC, [gyb01c] ASC, [gyb02f] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gcm11] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gcm11]( [gcm00c] [varchar](10) NOT NULL, [gcm01c] [varchar](20) NOT NULL, [gcm02c] [varchar](20) NOT NULL, [gcm03f] [float] NULL, [gcm04f] [float] NULL, [gcm05f] [float] NULL, [gcm06d] [varchar](8) NULL, [gcm07i] [int] NULL, [gcm08f] [float] NULL, [gcm09i] [int] NOT NULL, [gcm10f] [float] NOT NULL, [gcm11f] [float] NULL, [gcm12i] [int] NULL, [gcm13c] [varchar](1000) NULL, [gcm14i] [int] IDENTITY(1,1) NOT NULL, [gcm15c] [varchar](5) NULL, [gcm16f] [float] NULL, [gcm17c] [varchar](5) NULL, [gcm18f] [float] NULL, [gcm19c] [varchar](5) NULL, [gcm20f] [float] NULL, [gcm21f] [float] NULL, [gcm22i] [int] NULL, [gcm23c] [varchar](5) NULL, [gcm24f] [float] NULL, [gcm25i] [int] NULL, [gcm26i] [int] NULL, [gcm27f] [float] NULL, [gcm28c] [nvarchar](2000) NULL, [gcm29c] [varchar](20) NULL, [gcm30c] [varchar](20) NULL, [gcm31d] [int] NULL, [gcm32f] [float] NULL, [gcm33c] [varchar](20) NULL, [gcm33f] [float] NULL, CONSTRAINT [PK_GCM11] PRIMARY KEY CLUSTERED ( [gcm00c] ASC, [gcm14i] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[pt_operation] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[pt_operation]( [id] [numeric](18, 0) IDENTITY(1,1) NOT NULL, [ptId] [numeric](18, 0) NOT NULL, [compId] [varchar](10) NOT NULL, [operation] [int] NOT NULL, [operator] [varchar](10) NOT NULL, [operateTime] [datetime] NOT NULL, [description] [varchar](1000) NOT NULL, [state] [int] NOT NULL, CONSTRAINT [PK_pt_operation] PRIMARY KEY CLUSTERED ( [id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[glm07] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[glm07]( [glg00c] [varchar](10) NOT NULL, [glg01c] [varchar](20) NOT NULL, [glg02i] [float] NOT NULL, [glg03c] [varchar](20) NULL, [glg04v] [varchar](20) NULL, [glg05v] [varchar](20) NULL, [glg06c] [varchar](20) NULL, [glg07f] [float] NULL, [glg08t] [varchar](6) NULL, [glg09t] [varchar](6) NULL, [glg10d] [varchar](8) NULL, [glg11c] [varchar](20) NULL, [glg12i] [int] NULL, [glg13i] [int] NULL, [glg14i] [int] NULL, [glg15c] [varchar](10) NULL, [glg16f] [float] NULL, [glg17t] [varchar](20) NULL, [glg18t] [varchar](20) NULL, [glg19c] [varchar](20) NULL, [glg20c] [varchar](20) NULL, [glg21c] [varchar](20) NULL, [glg22i] [int] NULL, [glg23i] [int] NULL, [glg24i] [int] NULL, CONSTRAINT [PK_GLM07] PRIMARY KEY CLUSTERED ( [glg00c] ASC, [glg01c] ASC, [glg02i] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[gam03] Script Date: 06/12/2017 06:16:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[gam03]( [gac01c] [varchar](10) NOT NULL, [gac02c] [varchar](10) NOT NULL, [gac03c] [char](1) NULL, [gac04c] [char](1) NULL, [gac05c] [char](1) NULL, [gac06c] [char](1) NULL, [gac07c] [char](1) NULL, [gac08c] [char](1) NULL, [gac09c] [char](1) NULL, [gac10c] [char](1) NULL, [gac11c] [varchar](10) NOT NULL, [gac12c] [char](1) NULL, [gac13c] [char](1) NULL, [gac14c] [char](1) NULL, [gac15c] [char](1) NULL, [gac16c] [char](1) NULL, [gac17c] [char](1) NOT NULL, [gac18i] [int] NOT NULL, [gac19c] [char](1) NOT NULL, CONSTRAINT [PK_GAM03] PRIMARY KEY CLUSTERED ( [gac01c] ASC, [gac11c] ASC, [gac02c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[wgp] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[wgp]( [wgp00] [varchar](10) NOT NULL, [wgp01v] [varchar](20) NULL, [wgp02v] [varchar](200) NULL, [wgp03v] [varchar](20) NULL, [wgp04v] [varchar](20) NULL, [wgp05v] [varchar](20) NULL, CONSTRAINT [PK_WGP] PRIMARY KEY CLUSTERED ( [wgp00] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: Table [dbo].[ham05] Script Date: 06/12/2017 06:16:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[ham05]( [hae00c] [varchar](10) NOT NULL, [hae01c] [varchar](10) NOT NULL, [hae02c] [varchar](8) NOT NULL, [hae03c] [varchar](20) NULL, [hae04f] [float] NULL, [hae05c] [varchar](20) NULL, [hae06c] [varchar](20) NULL, CONSTRAINT [PK_HAM05] PRIMARY KEY CLUSTERED ( [hae00c] ASC, [hae01c] ASC, [hae02c] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO /****** Object: StoredProcedure [dbo].[upg_rename_memid] Script Date: 06/12/2017 06:20:36 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_rename_memid] ( @oldmemid varchar(20), @newmemid varchar(20), @userid varchar(20), @curdate varchar(10), @curtime varchar(10) ) as begin declare @cardid varchar(20) select @cardid = isnull(gba23c, '') from gbm01 where gba01c = @oldmemid and isnull(gba23c, '') <> '' select @cardid = isnull(@cardid, '') insert sysoperationlog(userid, program, operation, operationdate, operationtime, origatedate, compid, keyvalue1, keyvalue2, keyvalue3) values(@userid, 'GSB06I', 'M', @curdate, @curtime, @curdate, '001', @oldmemid, @newmemid, 'upg_rename_memid') if @cardid <> '' begin update gbm01 set gba01c = @newmemid where gba01c = @oldmemid and isnull(gba23c, '') = @cardid end else begin update gbm01 set gba01c = @newmemid where gba01c = @oldmemid end update gbm04 set gbd01c = @newmemid where gbd01c = @oldmemid update gbm05 set gbe01c = @newmemid where gbe01c = @oldmemid update gbm06 set gbf01c = @newmemid where gbf01c = @oldmemid update gbm07 set gbg01c = @newmemid where gbg01c = @oldmemid update gbm08 set gbh01c = @newmemid where gbh01c = @oldmemid update gbm09 set gbi01c = @newmemid where gbi01c = @oldmemid update gbm10 set gbj04c = @newmemid where gbj04c = @oldmemid update gbm12 set gbl01c = @newmemid where gbl01c = @oldmemid update gbm13 set gbm01c = @newmemid where gbm01c = @oldmemid update gbm14 set gbn01c = @newmemid where gbn01c = @oldmemid update gbm15 set gbl01c = @newmemid where gbl01c = @oldmemid if @cardid <> '' begin update gcm01 set gca04c = @newmemid where gca04c = @oldmemid and isnull(gca01c, '') = @cardid update glm01 set gla27c = @newmemid where gla27c = @oldmemid and isnull(gla06c, '') = @cardid update ggm01 set gga05c = @newmemid where gga05c = @oldmemid and isnull(gga09c, '') = @cardid update gnm01 set gna04c = @newmemid where gna04c = @oldmemid and isnull(gna13c, '') = @cardid update gcm12 set gcn15c = @newmemid where gcn15c = @oldmemid and isnull(gcn01c, '') = @cardid end else begin update gcm01 set gca04c = @newmemid where gca04c = @oldmemid update glm01 set gla27c = @newmemid where gla27c = @oldmemid update ggm01 set gga05c = @newmemid where gga05c = @oldmemid update gnm01 set gna04c = @newmemid where gna04c = @oldmemid update gcm12 set gcn15c = @newmemid where gcn15c = @oldmemid end end GO /****** Object: StoredProcedure [dbo].[upg_delete_all_gcm12] Script Date: 06/12/2017 06:19:58 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_delete_all_gcm12] ( @compid varchar(10), @userid varchar(20), @curdate varchar(10), @curtime varchar(10) ) as begin insert sysoperationlog(userid, program, operation, operationdate, operationtime, origatedate, compid, keyvalue1, keyvalue2, keyvalue3, keyvalue4) values(@userid, 'GSB22I', 'D', @curdate, @curtime, @curdate, @compid, 'ALL', '', 'delete_all_gcm12', '删除所有开帐资料') delete gcm12 where gcn00c = @compid delete gcm16 where gcr00c = @compid end GO /****** Object: StoredProcedure [dbo].[upg_init_customers_gcm16] Script Date: 06/12/2017 06:20:13 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_init_customers_gcm16] ( @compid varchar(10) -- 公司别 ) as begin declare @procacct int select @procacct = 4 create table #tbl_card_proc_amt ( compid varchar(10) not null, -- 公司别 cardid varchar(20) not null, -- 会员卡号 amt float null, primary key(compid, cardid) ) -- 只处理gcm12资料已经为开帐的情况 insert #tbl_card_proc_amt(compid, cardid, amt) select gcr00c, gcr01c, sum(gcr10f) from gcm16 a, gcm12 b where gcr00c = @compid and isnull(gcr26i, 0) = 0 -- 还没有开帐的 and isnull(gcr31i, 0) = 1 -- 需要开帐的 and a.gcr00c = b.gcn00c and a.gcr01c = b.gcn01c and b.gcn24i = 1 group by gcr00c, gcr01c insert gcm06(gcf00c, gcf01c, gcf02c, gcf03f, gcf04f, gcf05f, gcf06f, gcf07f, gcf08f, gcf09f, gcf10f, gcf11d, gcf12d, gcf13i, gcf14f, gcf15c, gcf16i, gcf17i, gcf18f, gcf19i, gcf20i, gcf21i, gcf22i, gcf24i) select gcr00c, gcr01c, gcr02c, gcr03f, gcr04f, gcr05f, gcr06f, gcr07f, gcr08f, gcr09f, gcr10f, gcr11d, gcr12d, gcr13i, gcr14f, gcr15c, gcr16i, gcr17i, gcr18f, isnull(gcr19i, 1), isnull(gcr20i, 1), isnull(gcr21i, 0), isnull(gcr22i, 0), gcr23i from gcm16 a, gcm12 b where gcr00c = @compid and isnull(gcr26i, 0) = 0 -- 还没有开帐的 and isnull(gcr31i, 0) = 1 -- 需要开帐的 and a.gcr00c = b.gcn00c and a.gcr01c = b.gcn01c and b.gcn24i = 1 update a set a.gcr26i = 1 from gcm16 a, gcm12 b where gcr00c = @compid and isnull(gcr26i, 0) = 0 -- 还没有开帐的 and isnull(gcr31i, 0) = 1 -- 需要开帐的 and a.gcr00c = b.gcn00c and a.gcr01c = b.gcn01c and b.gcn24i = 1 update a set a.gcc06f = isnull(a.gcc06f, 0) + isnull(b.amt, 0) from gcm03 a, #tbl_card_proc_amt b where a.gcc00c = b.compid and a.gcc01c = b.cardid and a.gcc03i = @procacct drop table #tbl_card_proc_amt end GO /****** Object: StoredProcedure [dbo].[sp_sendOrgMail] Script Date: 06/12/2017 06:19:21 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[sp_sendOrgMail] @orgType int, @level1Org varchar(50), @level2Org varchar(50), @subject varchar(50), @content varchar(5000), @sender varchar(20), @sendTime varchar(20), @createTime varchar(20), @fjFileName varchar(50), @senderCompID varchar(20) as begin declare @count int select @count = count(*) from wem declare @receiver varchar(100) if (@orgType = 1) begin select @receiver = 'Company - ' + @level1Org end else if (@orgType = 2) begin select @receiver = 'Company - ' + @level1Org + '(' + @level2Org + ')' end else if (@orgType = 3) begin select @receiver = 'Group - ' + @level1Org end insert into wem(wem00, wem01v, wem02v, wem03v, wem04v, wem05v, wem06v, wem07i, wem08v, wem10v, wem11v) values(@count, @subject, @content, @receiver, @sender, @sendTime, @createTime, 1, @fjFileName, '', @senderCompID) --保存到发件人的发件箱 insert into wmb(wmb01v, wmb02i, wmb03i, wmb05v) values(@sender, @count, 2, @senderCompID) declare @empid varchar(10) --保存到组织中每个成员的收件箱 --向某公司(公司)发送群邮件 if (@orgType = 1) begin declare cur_ham01 cursor for select haa01c from ham01 where haa00c = @level1Org open cur_ham01 fetch next from cur_ham01 into @empid while @@fetch_status = 0 begin insert into wmb(wmb01v, wmb02i, wmb03i, wmb04i, wmb05v) values(@empid, @count, 1, 0, @level1Org) fetch next from cur_ham01 into @empid end close cur_ham01 deallocate cur_ham01 end --向某部门发送群邮件 else if (@orgType = 2) begin declare cur_ham01_dp cursor for select haa01c from ham01 where haa00c = @level1Org and haa06c = @level2Org open cur_ham01_dp fetch next from cur_ham01_dp into @empid while @@fetch_status = 0 begin insert into wmb(wmb01v, wmb02i, wmb03i, wmb04i, wmb05v) values(@empid, @count, 1, 0, @level1Org) fetch next from cur_ham01_dp into @empid end close cur_ham01_dp deallocate cur_ham01_dp end --向某群组织发送群邮件 else if (@orgType = 3) begin declare @compid varchar(20) declare cur_wgl cursor for select wgl01v, wgl02v from wgl where wgl00 = @level1Org open cur_wgl fetch next from cur_wgl into @empid, @compid while @@fetch_status = 0 begin insert into wmb(wmb01v, wmb02i, wmb03i, wmb04i, wmb05v) values(@empid, @count, 1, 0, @compid) fetch next from cur_wgl into @empid end close cur_wgl deallocate cur_wgl end select @count as count end GO /****** Object: StoredProcedure [dbo].[upg_synch_staff] Script Date: 06/12/2017 06:20:46 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_synch_staff] ( @compid varchar(10), -- 门店 @employee varchar(20) -- 工号 ) as begin declare @owncompid varchar(10) declare @ownemployee varchar(20) declare @isDimission varchar(10) select @owncompid = ownhaa00c, @ownemployee = ownhaa01c, @isDimission = haa27c from ham01 where haa00c = @compid and haa01c = @employee create table #employ_keys ( compid varchar(10), -- 门店 employee varchar(20) -- 工号 ) insert into #employ_keys(compid, employee) select haa00c, haa01c from ham01 where ownhaa00c = @owncompid and ownhaa01c = @ownemployee and haa00c <> @compid -- 创建循环变量 declare @HasRecord int set @HasRecord = 1 declare @currentcompid varchar(10) declare @currentemployee varchar(20) while(@HasRecord = 1) begin if exists ( select 1 from #employ_keys) begin set @HasRecord = 1 select top 1 @currentcompid = compid, @currentemployee = employee from #employ_keys -- 员工基本资料主档 update a set a.haa02c = b.haa02c, a.haa03c = b.haa03c, a.haa04c = b.haa04c, a.haa05i = b.haa05i, a.haa08d = b.haa08d, a.haa09d = b.haa09d, a.haa10c = b.haa10c, a.haa11d = b.haa11d, a.haa12c = b.haa12c, a.haa13c = b.haa13c, a.haa14d = b.haa14d, a.haa15f = b.haa15f, a.haa16f = b.haa16f, a.haa17c = b.haa17c, a.haa18c = b.haa18c, a.haa19c = b.haa19c, a.haa20c = b.haa20c, a.haa21c = b.haa21c, a.haa22c = b.haa22c, a.haa23c = b.haa23c, a.haa24c = b.haa24c, a.haa25c = b.haa25c, a.haa35c = b.haa35c, a.haa36c = b.haa36c, a.haa37c = b.haa37c, a.haa38c = b.haa38c, a.haa44c = b.haa44c, a.haa99c = b.haa99c, a.haa27c = (case when @isDimission = '3' then '3' else a.haa27c end) from ham01 a, ham01 b where a.haa00c = @currentcompid and a.haa01c = @currentemployee and b.haa00c = @compid and b.haa01c = @employee -- 员工资料--学历 delete ham02 where hab00c = @currentcompid and hab01c = @currentemployee insert ham02(hab00c, hab01c, hab02f, hab03c, hab04c, hab05d, hab06d, hab07c, hab08i, hab09c, hab10c) select @currentcompid, @currentemployee, hab02f, hab03c, hab04c, hab05d, hab06d, hab07c, hab08i, hab09c, hab10c from ham02 where hab00c = @compid and hab01c = @employee -- 员工资料--经历 delete ham03 where hac00c = @currentcompid and hac01c = @currentemployee insert ham03(hac00c, hac01c, hac02f, hac03c, hac04c, hac05d, hac06d, hac07i, hac08c, hac09f, hac10c) select @currentcompid, @currentemployee, hac02f, hac03c, hac04c, hac05d, hac06d, hac07i, hac08c, hac09f, hac10c from ham03 where hac00c = @compid and hac01c = @employee -- 员工资料--专长 delete ham04 where had00c = @currentcompid and had01c = @currentemployee insert ham04(had00c, had01c, had02c, had03d, had04d, had05i, had06c, had07c) select @currentcompid, @currentemployee, had02c, had03d, had04d, had05i, had06c, had07c from ham04 where had00c = @compid and had01c = @employee -- 教练资料--擅长项目 delete ham06 where haf00c = @currentcompid and haf01c = @currentemployee insert ham06(haf00c, haf01c, haf02c, haf03f, haf04c) select @currentcompid, @currentemployee, haf02c, haf03f, haf04c from ham06 where haf00c = @compid and haf01c = @employee -- 员工--擅长项目 delete ham11 where hak00c = @currentcompid and hak01c = @currentemployee insert ham11(hak00c, hak01c, hak02c, hak03c) select @currentcompid, @currentemployee, hak02c, hak03c from ham11 where hak00c = @compid and hak01c = @employee -- 清除已处理的门店 delete #employ_keys where compid = @currentcompid and employee = @currentemployee end else begin set @HasRecord = 0 end end drop table #employ_keys end GO /****** Object: StoredProcedure [dbo].[upg_dispatch_staff] Script Date: 06/12/2017 06:20:01 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_dispatch_staff] ( @incompid varchar(10), @oldstaff varchar(20), @newcompid varchar(10), @newstaff varchar(20), @instaffid varchar(20), --add by lyj 内部管理编号 @oldpostion varchar(20), --add by lyj 老职位 @newpostion varchar(20), --add by lyj 新职位 @remark varchar(80) --派遣说明 ) as begin update ham01 set haa27c = '1' where haa00c = @incompid and haa01c = @oldstaff if (@incompid = @newcompid and @oldstaff = @newstaff) return if exists(select 1 from ham01 where haa00c = @newcompid and haa01c = @newstaff) begin insert ham12(hal01c, hal03c, hal04c, hal05c, hal06c, hal07c, hal08c, hal09c) values (@instaffid, @incompid, @oldstaff, @oldpostion, @newcompid, @newstaff, @newpostion, @remark) update ham01 set haa27c = '2' where haa00c = @newcompid and haa01c = @newstaff return end -- 员工基本资料主档 insert ham01(haa00c, haa01c, haa02c, haa03c, haa04c, haa05i, haa06c, haa07c, haa08d, haa09d, haa10c, haa11d, haa12c, haa13c, haa14d, haa15f, haa16f, haa17c, haa18c, haa19c, haa20c, haa21c, haa22c, haa23c, haa24c, haa25c, haa26c, haa27c, haa28i, haa29f, haa30f, haa31f, haa32c, haa91c, haa92d, haa33c, haa34c, haa35c, haa36c, haa37c, haa38c, haa39i, haa40i, haa41i, haa42f, haa43f, haa44c, haa46i, haa90c, ownhaa00c, ownhaa01c,haa50c,haa51c) select @newcompid, @newstaff, haa02c, haa03c, haa04c, haa05i, haa06c, haa07c, haa08d, haa09d, haa10c, haa11d, haa12c, haa13c, haa14d, haa15f, haa16f, haa17c, haa18c, haa19c, haa20c, haa21c, haa22c, haa23c, haa24c, @newpostion, haa26c, '2', haa28i, haa29f, haa30f, haa31f, haa32c, haa91c, haa92d, haa33c, haa34c, haa35c, haa36c, haa37c, haa38c, haa39i, haa40i, haa41i, haa42f, haa43f, haa44c, haa46i, haa90c, ownhaa00c, ownhaa01c,haa50c,haa51c from ham01 where haa00c = @incompid and haa01c = @oldstaff -- 员工资料--学历 insert ham02(hab00c, hab01c, hab02f, hab03c, hab04c, hab05d, hab06d, hab07c, hab08i, hab09c, hab10c) select @newcompid, @newstaff, hab02f, hab03c, hab04c, hab05d, hab06d, hab07c, hab08i, hab09c, hab10c from ham02 where hab00c = @incompid and hab01c = @oldstaff -- 员工资料--经历 insert ham03(hac00c, hac01c, hac02f, hac03c, hac04c, hac05d, hac06d, hac07i, hac08c, hac09f, hac10c) select @newcompid, @newstaff, hac02f, hac03c, hac04c, hac05d, hac06d, hac07i, hac08c, hac09f, hac10c from ham03 where hac00c = @incompid and hac01c = @oldstaff -- 员工资料--专长 insert ham04(had00c, had01c, had02c, had03d, had04d, had05i, had06c, had07c) select @newcompid, @newstaff, had02c, had03d, had04d, had05i, had06c, had07c from ham04 where had00c = @incompid and had01c = @oldstaff -- 教练资料--擅长项目 insert ham06(haf00c, haf01c, haf02c, haf03f, haf04c) select @newcompid, @newstaff, haf02c, haf03f, haf04c from ham06 where haf00c = @incompid and haf01c = @oldstaff -- 员工--擅长项目 insert ham11(hak00c, hak01c, hak02c, hak03c) select @newcompid, @newstaff, hak02c, hak03c from ham11 where hak00c = @incompid and hak01c = @oldstaff -- 派遣员工的时候加入派遣历史 insert ham12(hal01c, hal03c, hal04c, hal05c, hal06c, hal07c, hal08c, hal09c) values (@instaffid, @incompid, @oldstaff, @oldpostion, @newcompid, @newstaff, @newpostion, @remark) end GO /****** Object: StoredProcedure [dbo].[upg_rename_personno] Script Date: 06/12/2017 06:20:36 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_rename_personno] ( @compid varchar(10), --公司编号 @oldno varchar(10), --旧员工编号 @newno varchar(10), @userid varchar(20), @curdate varchar(10), @curtime varchar(10) ) as --WUXM 2005/07/21 begin insert sysoperationlog(userid, program, operation, operationdate, operationtime, origatedate, compid, keyvalue1, keyvalue2, keyvalue3) values(@userid, 'GSA09I', 'M', @curdate, @curtime, @curdate, @compid, @oldno, @newno, 'upg_rename_person') declare @owncompid varchar(10) declare @ownemployee varchar(20) select @owncompid = ownhaa00c, @ownemployee = ownhaa01c from ham01 where haa00c = @compid and haa01c = @oldno create table #employ_keys ( compid varchar(10), -- 门店 employee varchar(20) -- 工号 ) insert into #employ_keys(compid, employee) select haa00c, haa01c from ham01 where ownhaa00c = @owncompid and ownhaa01c = @ownemployee --where haa00c = @compid and haa01c = @oldno -- 创建循环变量 declare @HasRecord int set @HasRecord = 1 declare @currentcompid varchar(10) declare @currentemployee varchar(20) while(@HasRecord = 1) begin if exists ( select 1 from #employ_keys) begin set @HasRecord = 1 select top 1 @currentcompid = compid, @currentemployee = employee from #employ_keys --员工基本主档资料 update a set a.haa01c = @newno, a.ownhaa01c = @newno from ham01 a where a.haa01c = @currentemployee and a.haa00c = @currentcompid --员工资料--学历 update a set a.hab01c = @newno from ham02 a where a.hab01c = @currentemployee and a.hab00c = @currentcompid --员工资料--经历 update a set a.hac01c = @newno from ham03 a where a.hac01c = @currentemployee and a.hac00c = @currentcompid --员工资料--专长 update a set a.had01c = @newno from ham04 a where a.had01c = @currentemployee and a.had00c = @currentcompid --员工资料--考核 update a set a.hae01c = @newno from ham05 a where a.hae01c = @currentemployee and a.hae00c = @currentcompid --教练资料 update a set a.haf01c = @newno from ham06 a where a.haf01c = @currentemployee and a.haf00c = @currentcompid --add by lyj 同时修改介绍人 update a set a.ham01c = @newno from ham13 a where a.ham01c = @currentemployee --员工薪资项目设定 update a set a.hbb01c = @newno from hbm02 a where a.hbb01c = @currentemployee and a.hbb00c = @currentcompid --薪资计算--员工发薪总额 update a set a.hcb02c = @newno from hcm02 a where a.hcb02c = @currentemployee and a.hcb00c = @currentcompid --薪资计算--员工薪资明细 update a set a.hcc02c = @newno from hcm03 a where a.hcc02c = @currentemployee and a.hcc00c = @currentcompid --员工实际出勤 update a set a.hda01c = @newno from hdm01 a where a.hda01c = @currentemployee and a.hda00c = @currentcompid --员工项目提成 update a set a.heb01c = @newno from hem02 a where a.heb01c = @currentemployee and a.heb00c = @currentcompid --累计业绩提成 update a set a.hec01c = @newno from hem03 a where a.hec01c = @currentemployee and a.hec00c = @currentcompid --产品累计提成 update a set a.hed01c = @newno from hem04 a where a.hed01c = @currentemployee and a.hed00c = @currentcompid --总业绩提成 update a set a.hef01c = @newno from hem06 a where a.hef01c = @currentemployee and a.hef00c = @currentcompid update a set a.heg01c = @newno from hem07 a where a.heg01c = @currentemployee and a.heg00c = @currentcompid --会员基本资料--回访记录 update a set a.gbj09c = @newno from gbm10 a where a.gbj09c = @currentemployee and a.gbj00c = @currentcompid --员工基本资料--员工借物 update a set a.hag01c = @newno from ham07 a where a.hag01c = @currentemployee and a.hag00c = @currentcompid --员工基本资料--员工惩罚 update a set a.hah01c = @newno from ham08 a where a.hah01c = @currentemployee and a.hah00c = @currentcompid --员工账户 update a set a.hai01c = @newno from ham09 a where a.hai01c = @currentemployee and a.hai00c = @currentcompid --卡累计提成 update a set a.hee01c = @newno from hem05 a where a.hee01c = @currentemployee and a.hee00c = @currentcompid --员工当前状态表 update a set a.gicg01v = @newno from gicm07 a where a.gicg01v = @currentemployee and a.gicg00c = @currentcompid --员工状态临时表 update a set a.gici01v = @newno from gicm09 a where a.gici01v = @currentemployee and a.gici00c = @currentcompid --员工排班表 update a set a.gicj01v = @newno from gicm10 a where a.gicj01v = @currentemployee and a.gicj00c = @currentcompid update a set gick04c = @newno from gicm13 a where a.gick04c = @currentemployee and a.gick00c = @currentcompid --会员基本资料--现在的感受 update a set a.gbf05c = @newno from gbm06 a where a.gbf05c = @currentemployee and a.gbf00c = @currentcompid --会员基本资料—-明天的美容希望 update a set a.gbg05c = @newno from gbm07 a where a.gbg05c = @currentemployee and a.gbg00c = @currentcompid --赠券资料档 update a set a.gah05c = @newno from gam08 a where a.gah05c = @currentemployee and a.gah00c = @currentcompid --会员基本资料 --负责销售 update a set a.gba10c = @newno from gbm01 a where a.gba10c = @currentemployee and a.gba00c = @currentcompid --会员卡基本资料主档 update a set a.gca20c = @newno from gcm01 a where a.gca20c = @currentemployee and a.gca00c = @currentcompid --账户异动单--第一销售 update a set a.gcl09c = @newno from gcm10 a where a.gcl09c = @currentemployee and a.gcl00c = @currentcompid --账户异动单--第二销售 update a set a.gcl17c = @newno from gcm10 a where a.gcl17c = @currentemployee and a.gcl00c = @currentcompid --账户异动单--第三销售 update a set a.gcl19c = @newno from gcm10 a where a.gcl19c = @currentemployee and a.gcl00c = @currentcompid --项目资料主档--当前负责教练 update a set a.gda13c = @newno from gdm01 a where a.gda13c = @currentemployee and a.gda00c = @currentcompid --会员卡异动单--销售人员 update a set a.gea21c = @newno from gem01 a where a.gea21c = @currentemployee and a.gea00c = @currentcompid --产品资料档--负责采购 update a set a.gfa06c = @newno from gfm01 a where a.gfa06c = @currentemployee and a.gfa00c = @currentcompid --项目消费--消耗明细 update a set a.ggb12c = @newno from ggm02 a where a.ggb12c = @currentemployee and a.ggb00c = @currentcompid update a set a.ggb13c = @newno from ggm02 a where a.ggb13c = @currentemployee and a.ggb00c = @currentcompid update a set a.ggb14c = @newno from ggm02 a where a.ggb14c = @currentemployee and a.ggb00c = @currentcompid --服务项目4工 --add by lyj 添加第四销售人员 update a set a.ggb26c = @newno from ggm02 a where a.ggb26c = @currentemployee and a.ggb00c = @currentcompid --项目消费--产品明细--第一人 update a set a.ggc12c = @newno from ggm03 a where a.ggc12c = @currentemployee and a.ggc00c = @currentcompid --项目消费--产品明细--第二人 update a set a.ggc15c = @newno from ggm03 a where a.ggc15c = @currentemployee and a.ggc00c = @currentcompid --项目消费--产品明细--第三人 update a set a.ggc17c = @newno from ggm03 a where a.ggc17c = @currentemployee and a.ggc00c = @currentcompid --ggm05 update a set a.gge12c = @newno from ggm05 a where a.gge12c = @currentemployee and a.gge00c = @currentcompid --产品入库--采购人员 update a set a.gha05c = @newno from ghm01 a where a.gha05c = @currentemployee and a.gha00c = @currentcompid --产品出库单--领料人员 update a set a.gia05c = @newno from gim01 a where a.gia05c = @currentemployee and a.gia00c = @currentcompid --产品盘点--盘点人员代码 update a set a.gka04c = @newno from gkm01 a where a.gka04c = @currentemployee and a.gka00c = @currentcompid --会员卡销售单--负责业务 update a set a.gna20c = @newno from gnm01 a where a.gna20c = @currentemployee and a.gna00c = @currentcompid --会员卡销售单--第二卖卡人 update a set a.gna28c = @newno from gnm01 a where a.gna28c = @currentemployee and a.gna00c = @currentcompid --会员卡销售单--第三卖卡人 update a set a.gna28c = @newno from gnm01 a where a.gna28c = @currentemployee and a.gna00c = @currentcompid --体质测试单--教练 update a set a.goa04c = @newno from gom01 a where a.goa04c = @currentemployee and a.goa00c = @currentcompid --体质测试单--测试人员 update a set a.goa05c = @newno from gom01 a where a.goa05c = @currentemployee and a.goa00c = @currentcompid --客户调查单 update a set a.gpa07c = @newno from gpm01 a where a.gpa07c = @currentemployee and a.gpa00c = @currentcompid --收款费用单 update a set a.gqa07c = @newno from gqm01 a where a.gqa07c = @currentemployee and a.gqa00c = @currentcompid --收款费用明细项目金额--第一提成 update a set a.gqb06c = @newno from gqm02 a where a.gqb06c = @currentemployee and a.gqb00c = @currentcompid --收款费用明细项目金额--第二提成 update a set a.gqb07c = @newno from gqm02 a where a.gqb07c = @currentemployee and a.gqb00c = @currentcompid --收款费用明细项目金额--第三提成 update a set a.gqb08c = @newno from gqm02 a where a.gqb08c = @currentemployee and a.gqb00c = @currentcompid --收款费用明细项目金额--第四提成 update a set a.gqb09c = @newno from gqm02 a where a.gqb09c = @currentemployee and a.gqb00c = @currentcompid --收款费用明细项目金额--第五提成 update a set a.gqb10c = @newno from gqm02 a where a.gqb10c = @currentemployee and a.gqb00c = @currentcompid --预约单表明细表--大工号 update a set a.gicl04v = @newno from gicm12 a where a.gicl04v = @currentemployee and a.gicl00c = @currentcompid --预约单表明细表--中工号 update a set a.gicl05v = @newno from gicm12 a where a.gicl05v = @currentemployee and a.gicl00c = @currentcompid --预约单表明细表--小工号 update a set a.gicl06v = @newno from gicm12 a where a.gicl06v = @currentemployee and a.gicl00c = @currentcompid --入院预开单明细--大工号 update a set a.gll04v = @newno from glm12 a where a.gll04v = @currentemployee and a.gll00c = @currentcompid --入院预开单明细--中工号 update a set a.gll05v = @newno from glm12 a where a.gll05v = @currentemployee and a.gll00c = @currentcompid --入院预开单明细--小工号 update a set a.gll06v = @newno from glm12 a where a.gll06v = @currentemployee and a.gll00c = @currentcompid update a set gct15c = @newno from gcm20 a where a.gct15c = @currentemployee and a.gct00c = @currentcompid update a set gct16c = @newno from gcm20 a where a.gct16c = @currentemployee and a.gct00c = @currentcompid update a set gct17c = @newno from gcm20 a where a.gct17c = @currentemployee and a.gct00c = @currentcompid update a set gct18c = @newno from gcm20 a where a.gct18c = @currentemployee and a.gct00c = @currentcompid --add by lyj for 伊美娜2008-03-20 --员工账户 update a set a.han01c = @newno from ham14 a where a.han01c = @currentemployee and a.han00c = @currentcompid update a set a.hap01c = @newno from ham15 a where a.hap01c = @currentemployee and a.hap00c = @currentcompid update a set a.haq01c = @newno from ham16 a where a.haq01c = @currentemployee and a.haq00c = @currentcompid -- 清除已处理的门店 delete #employ_keys where compid = @currentcompid and employee = @currentemployee end else begin set @HasRecord = 0 end end drop table #employ_keys end GO /****** Object: StoredProcedure [dbo].[upg_clear_glm06_history_data] Script Date: 06/12/2017 06:19:41 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_clear_glm06_history_data] ( @compid varchar(10), @enddate varchar(10) ) as begin -- 日期之间的临时传递全部删除 delete glm06 where glf00c = @compid and glf08d <= @enddate -- 日期之前的所有开单结账掉 update glm07 set glg12i = 1, glg13i = 0, glg23i = 0 where glg00c = @compid and glg10d <= @enddate -- 日期之前的所有委托关闭 update glm08 set gli08i = 1 where gli00c = @compid and gli02d <= @enddate -- 日期之间的所有产品购买结账掉 update glm09 set glk11i = 1 where glk00c = @compid and glk03d <= @enddate -- 已经结账的不能为选择待结账 update glm07 set glg13i = 0 where glg00c = @compid and isnull(glg12i, 0) = 1 end GO /****** Object: StoredProcedure [dbo].[upg_updata_bill_state_when_glm07] Script Date: 06/12/2017 06:20:48 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_updata_bill_state_when_glm07] ( @compid varchar(10), @date varchar(8), @curtime varchar(10), @key varchar(20), @enterno varchar(20) ) as begin delete a from gam06 a, glm07 b where a.gaf00c = b.glg00c and a.gaf01c = b.glg03c and glg00c = @compid and glg13i = 1 and glg12i = 0 if (isnull(@key, '') = '') begin update a set a.glk11i = 1 from glm09 a, glm07 b where glk00c = glg00c and glg10d = glk03d and glk11i = 0 and glg12i = 0 and glg13i = 1 and glg03c = glk05c and glg00c = @compid and glg10d = @date update a set a.gli08i = 1 from glm08 a, glm07 b where glg00c = gli00c and glg10d = gli02d and gli08i = 0 and glg13i = 1 and glg12i = 0 and gli05c = glg03c and glg00c = @compid and glg10d = @date --先把时间超过结帐时间的时间全部设置为结帐的时间 update a set a.glg08t = @curtime from glm07 a where glg00c = @compid and glg13i = 1 and glg12i = 0 and glg08t>@curtime update a set a.glg09t = @curtime from glm07 a where glg00c = @compid and glg13i = 1 and glg12i = 0 and glg09t>@curtime update glm07 set glg13i = 0, glg12i = 1 where glg00c = @compid and glg13i = 1 and glg12i = 0 end else begin create table #related_key ( keyno varchar(20) not null, enterno varchar(20) null ) insert #related_key(keyno, enterno) select gli05c, gli09c from glm08 where gli00c = @compid and isnull(gli08i, 0) = 0 and gli07c = @key and (@enterno = '' or @enterno = gli10c) insert #related_key(keyno, enterno) values(@key, @enterno) update a set a.glk11i = 1 from glm09 a, #related_key c where a.glk00c = @compid and a.glk01c = c.enterno and a.glk11i = 0 and a.glk05c = c.keyno update a set a.gli08i = 1 from glm08 a, #related_key c where a.gli00c = @compid and a.gli10c = c.enterno and a.gli07c = c.keyno and a.gli08i = 0 --更新时间 update a set a.glg08t = @curtime from glm07 a, #related_key b where a.glg00c = @compid and a.glg13i = 1 and a.glg12i = 0 and a.glg03c = b.keyno and glg08t>@curtime update a set a.glg09t = @curtime from glm07 a, #related_key b where a.glg00c = @compid and a.glg13i = 1 and a.glg12i = 0 and a.glg03c = b.keyno and glg09t>@curtime update a set glg13i = 0, glg12i = 1 from glm07 a, #related_key b where a.glg00c = @compid and a.glg03c = b.keyno and a.glg01c = b.enterno and a.glg13i = 1 and a.glg12i = 0 drop table #related_key end end GO /****** Object: StoredProcedure [dbo].[upg_unconfirm_be_paid] Script Date: 06/12/2017 06:20:48 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_unconfirm_be_paid] ( @compid varchar(10), @own_billid varchar(20), -- 付钱的人 @cust_billid varchar(20) -- 客人,委托别人付钱的人 ) as begin delete glm08 where gli00c = @compid and gli09c = @cust_billid update glm07 set glg13i = 0, glg23i = 0 where glg00c = @compid and glg01c = @cust_billid end GO /****** Object: StoredProcedure [dbo].[upg_select_cashkey] Script Date: 06/12/2017 06:20:39 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_select_cashkey] ( @compid varchar(10), @key varchar(20), @enterno varchar(20) ) as begin if not exists(select * from glm08 with (nolock) where gli00c = @compid and isnull(gli08i, 0) = 0 and gli07c = @key) begin -- 没有委托信息,直接返回 select * from glm07 where isnull(glg13i, 0) = 1 and isnull(glg12i, 0) = 0 and glg00c = @compid and glg03c = @key and (@enterno = '' or glg01c = @enterno) end else begin -- 因为有委托,所以把其他的找出来 create table #related_key ( keyno varchar(20) not null, enterno varchar(20) null -- 单号 ) insert #related_key(keyno, enterno) select gli05c, gli09c from glm08 where gli00c = @compid and isnull(gli08i, 0) = 0 and gli07c = @key insert #related_key(keyno, enterno) values(@key, @enterno) if isnull(@enterno, '') = '' begin select * from glm07 where isnull(glg13i, 0) = 1 and isnull(glg12i, 0) = 0 and glg00c = @compid and glg03c in (select keyno from #related_key) end else begin select a.* from glm07 a, #related_key b where isnull(glg13i, 0) = 1 and isnull(glg12i, 0) = 0 and glg00c = @compid and a.glg03c = b.keyno and a.glg01c = b.enterno end drop table #related_key end end GO /****** Object: StoredProcedure [dbo].[upg_get_free_staff] Script Date: 06/12/2017 06:20:07 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_get_free_staff] ( @compid varchar(10), @curdate varchar(10) ) as begin -- 需要从轮班表gicm13中取得,如果gicm13还是为空,则先初始化这个表 exec upg_init_gicm13 @compid, @curdate create table #tbl_hdm01 ( seq int identity not null, staffid varchar(20) not null ) create table #tbl_inbusy ( staff01 varchar(20) null, staff02 varchar(20) null, staff03 varchar(20) null ) insert #tbl_hdm01(staffid) select gick04c from gicm13 where gick00c = @compid and gick07d = @curdate order by gick08i, gick01i insert #tbl_inbusy(staff01, staff02, staff03) select gll04v, gll05v, gll06v from glm01 a with (nolock), glm12 b with (nolock) where a.gla00c = b.gll00c and a.gla01c = b.gll01v and b.gll00c = @compid and a.gla23i = 0 -- 没有结帐的服务人员 and a.gla02d = @curdate delete a from #tbl_hdm01 a, #tbl_inbusy b where a.staffid = b.staff01 delete a from #tbl_hdm01 a, #tbl_inbusy b where a.staffid = b.staff02 delete a from #tbl_hdm01 a, #tbl_inbusy b where a.staffid = b.staff03 -- 假设已经在gicm09中已经处理过,这必须在这个表中是空闲的 delete a from #tbl_hdm01 a where staffid not in (select gici01v from gicm09 where gici00c = @compid and gici03i = 0) select staffid from #tbl_hdm01 order by seq drop table #tbl_hdm01 drop table #tbl_inbusy end GO /****** Object: StoredProcedure [dbo].[upg_rename_goods] Script Date: 06/12/2017 06:20:35 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_rename_goods] ( @oldgoodid varchar(20), @newgoodid varchar(20), @userid varchar(20), @curdate varchar(10), @curtime varchar(10) ) as begin if exists(select 1 from gfm01 where gfa01c = @newgoodid) return if not exists(select 1 from gfm01 where gfa01c = @oldgoodid) return insert sysoperationlog(userid, program, operation, operationdate, operationtime, origatedate, compid, keyvalue1, keyvalue2, keyvalue3) values(@userid, 'GSB04I', 'M', @curdate, @curtime, @curdate, '001', @oldgoodid, @newgoodid, 'upg_rename_goods') -- 产品资料档 update gfm01 set gfa01c = @newgoodid where gfa01c = @oldgoodid -- 库存月异动统计档 update gfm02 set gfb01c = @newgoodid where gfb01c = @oldgoodid -- 库存日异动统计明细档 update gfm04 set gfd04c = @newgoodid where gfd04c = @oldgoodid -- 产品消耗设定 update gfm05 set gfe02c = @newgoodid where gfe02c = @oldgoodid -- 产品图片 update gfm06 set gff01c = @newgoodid where gff01c = @oldgoodid -- 产品功效说明 update gfm07 set gfg01c = @newgoodid where gfg01c = @oldgoodid -- 产品基本资料--月加权成本 update gfm08 set gfh01c = @newgoodid where gfh01c = @oldgoodid -- 产品入库单--入库产品明细 update ghm02 set ghb03c = @newgoodid where ghb03c = @oldgoodid -- 产品入库批次管理 update ghm03 set ghc01c = @newgoodid where ghc01c = @oldgoodid -- 产品出库单--出库产品明细 update gim02 set gib03c = @newgoodid where gib03c = @oldgoodid -- 产品盘点单--产品明细 update gkm02 set gkb03c = @newgoodid where gkb03c = @oldgoodid -- 预购买产品明细 update glm15 set glp03c = @newgoodid where glp03c = @oldgoodid -- 项目--产品明细 update gdm04 set gdd02c = @newgoodid where gdd02c = @oldgoodid -- 供应商产品信息 update gbm21 set gbu02c = @newgoodid where gbu02c = @oldgoodid -- 项目消费--产品明细 update ggm03 set ggc03c = @newgoodid where ggc03c = @oldgoodid -- 服务项目消耗--产品明细 update ggm05 set gge03c = @newgoodid where gge03c = @oldgoodid -- 购买产品的明细 update glm09 set glk06c = @newgoodid where glk06c = @oldgoodid -- 会员卡销售--赠送产品 update gnm04 set gnd02c = @newgoodid where gnd02c = @oldgoodid -- -- 订单明细表 -- update grm02 -- set grb03c = @newgoodid -- where grb03c = @oldgoodid -- -- -- 欠货登记表 -- update grm03 -- set grc03c = @newgoodid -- where grc03c = @oldgoodid -- 发货申请明细表 -- update gtm02 -- set gtb03c = @newgoodid -- where gtb03c = @oldgoodid -- 收货单明细表 -- update gvm02 -- set gvb03c = @newgoodid -- where gvb03c = @oldgoodid -- 会员卡消费历史 update gcm20 set gct07c = @newgoodid where gct07c = @oldgoodid and (gct06c = '4') -- 套餐设定-产品明细 update gtc03 set gtd02c = @newgoodid where gtd02c = @oldgoodid update gtc03 set gtd05c = @newgoodid where gtd05c = @oldgoodid -- 会员卡资料-套餐物品 update gtc08 set gtj03c = @newgoodid where gtj03c = @oldgoodid update gtc08 set gtj07c = @newgoodid where gtj07c = @oldgoodid -- 会员卡-套餐变化历史 update gtc07 set gti05c = @newgoodid where gti05c = @oldgoodid and gti12i = 2 update gtc07 set gti13c = @newgoodid where gti13c = @oldgoodid and gti12i = 2 -- 卡销售充值-套餐物品 update gtc10 set gtj04c = @newgoodid where gtj04c = @oldgoodid update gtc10 set gtj08c = @newgoodid where gtj08c = @oldgoodid -- 寄存单的detail update gim21 set gin02c = @newgoodid where gin02c = @oldgoodid -- 寄存领取历史 update gim22 set gio05c = @newgoodid where gio05c = @oldgoodid -- 会员卡--暂存产品 update gcm07 set gcg02c = @newgoodid where gcg02c = @oldgoodid -- 替换套餐的历史记录 update hem09 set hej06c = @newgoodid where hej06c = @oldgoodid and hej05i = 2 -- 缺货记录 update gcm22 set gcp03c = @newgoodid where gcp03c = @oldgoodid -- 产品业绩提成设置 update productprecept set pro01c = @newgoodid where pro01c = @oldgoodid -- 单独产品提成设置 update aloneproductprecept set gap01c = @newgoodid where gap01c = @oldgoodid end GO /****** Object: StoredProcedure [dbo].[upg_rename_goods_comp] Script Date: 06/12/2017 06:20:35 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_rename_goods_comp] ( @compid varchar(10), @oldgoodid varchar(20), @newgoodid varchar(20), @userid varchar(20), @curdate varchar(10), @curtime varchar(10) ) as begin if exists(select 1 from gfm01 a, gam26 z where a.gfa00c = z.gaz02c and z.gaz01c = @compid and gfa01c = @newgoodid) return if not exists(select 1 from gfm01 a, gam26 z where a.gfa00c = z.gaz02c and z.gaz01c = @compid and gfa01c = @oldgoodid) return insert sysoperationlog(userid, program, operation, operationdate, operationtime, origatedate, compid, keyvalue1, keyvalue2, keyvalue3) values(@userid, 'GSB04I', 'M', @curdate, @curtime, @curdate, '001', @oldgoodid, @newgoodid, 'upg_rename_goods_comp') -- 产品资料档 update a set a.gfa01c = @newgoodid from gfm01 a, gam26 z where a.gfa01c = @oldgoodid and a.gfa00c = z.gaz02c and z.gaz01c = @compid -- 库存月异动统计档 update a set gfb01c = @newgoodid from gfm02 a, gam26 z where a.gfb01c = @oldgoodid and a.gfb00c = z.gaz02c and z.gaz01c = @compid -- 库存日异动统计明细档 update a set gfd04c = @newgoodid from gfm04 a, gam26 z where a.gfd04c = @oldgoodid and a.gfd00c = z.gaz02c and z.gaz01c = @compid -- 产品消耗设定 update a set gfe02c = @newgoodid from gfm05 a, gam26 z where gfe02c = @oldgoodid and gfe00c = z.gaz02c and z.gaz01c = @compid -- 产品图片 update a set gff01c = @newgoodid from gfm06 a, gam26 z where a.gff01c = @oldgoodid and a.gff00c = z.gaz02c and z.gaz01c = @compid -- 产品功效说明 update a set gfg01c = @newgoodid from gfm07 a, gam26 z where gfg01c = @oldgoodid and gfg00c = z.gaz02c and z.gaz01c = @compid -- 产品基本资料--月加权成本 update a set gfh01c = @newgoodid from gfm08 a, gam26 z where gfh01c = @oldgoodid and gfh00c = z.gaz02c and z.gaz01c = @compid -- 产品入库单--入库产品明细 update a set ghb03c = @newgoodid from ghm02 a, gam26 z where ghb03c = @oldgoodid and ghb00c = z.gaz02c and z.gaz01c = @compid -- 产品入库批次管理 update a set ghc01c = @newgoodid from ghm03 a, gam26 z where ghc01c = @oldgoodid and ghc00c = z.gaz02c and z.gaz01c = @compid -- 产品出库单--出库产品明细 update a set gib03c = @newgoodid from gim02 a, gam26 z where gib03c = @oldgoodid and gib00c = z.gaz02c and z.gaz01c = @compid -- 预购买产品明细 update a set glp03c = @newgoodid from glm15 a, gam26 z where glp03c = @oldgoodid and glp00c = z.gaz02c and z.gaz01c = @compid -- 项目--产品明细 update a set gdd02c = @newgoodid from gdm04 a, gam26 z where gdd02c = @oldgoodid and gdd00c = z.gaz02c and z.gaz01c = @compid -- 供应商产品信息 update a set gbu02c = @newgoodid from gbm21 a, gam26 z where gbu02c = @oldgoodid and gbu00c = z.gaz02c and z.gaz01c = @compid -- 项目消费--产品明细 update a set ggc03c = @newgoodid from ggm03 a, gam26 z where ggc03c = @oldgoodid and ggc00c = z.gaz02c and z.gaz01c = @compid -- 服务项目消耗--产品明细 update a set gge03c = @newgoodid from ggm05 a, gam26 z where gge03c = @oldgoodid and gge00c = z.gaz02c and z.gaz01c = @compid -- 产品盘点单--产品明细 update a set gkb03c = @newgoodid from gkm02 a, gam26 z where gkb03c = @oldgoodid and gkb00c = z.gaz02c and z.gaz01c = @compid -- 购买产品的明细 update a set glk06c = @newgoodid from glm09 a, gam26 z where glk06c = @oldgoodid and glk00c = z.gaz02c and z.gaz01c = @compid -- 会员卡销售--赠送产品 update a set gnd02c = @newgoodid from gnm04 a, gam26 z where gnd02c = @oldgoodid and gnd00c = z.gaz02c and z.gaz01c = @compid -- -- 订单明细表 -- update a -- set grb03c = @newgoodid -- from grm02 a, gam26 z -- where grb03c = @oldgoodid -- and grb00c = z.gaz02c -- and z.gaz01c = @compid -- -- -- 欠货登记表 -- update a -- set grc03c = @newgoodid -- from grm03 a, gam26 z -- where grc03c = @oldgoodid -- and grc00c = z.gaz02c -- and z.gaz01c = @compid -- 发货申请明细表 -- update a -- set gtb03c = @newgoodid -- from gtm02 a, gam26 z -- where gtb03c = @oldgoodid -- and gtb00c = z.gaz02c -- and z.gaz01c = @compid -- 收货单明细表 -- update a -- set gvb03c = @newgoodid -- from gvm02 a, gam26 z -- where gvb03c = @oldgoodid -- and gvb00c = z.gaz02c -- and z.gaz01c = @compid -- 会员卡消费历史 update a set gct07c = @newgoodid from gcm20 a, gam26 z where gct07c = @oldgoodid and (gct06c = '4') and gct00c = z.gaz02c and z.gaz01c = @compid -- 套餐设定-产品明细 update a set gtd02c = @newgoodid from gtc03 a, gam26 z where gtd02c = @oldgoodid and gtd00c = z.gaz02c and z.gaz01c = @compid update a set gtd05c = @newgoodid from gtc03 a, gam26 z where gtd05c = @oldgoodid and gtd00c = z.gaz02c and z.gaz01c = @compid -- 会员卡资料-套餐物品 update a set gtj03c = @newgoodid from gtc08 a, gam26 z where gtj03c = @oldgoodid and gtj00c = z.gaz02c and z.gaz01c = @compid update a set gtj07c = @newgoodid from gtc08 a, gam26 z where gtj07c = @oldgoodid and gtj00c = z.gaz02c and z.gaz01c = @compid -- 会员卡-套餐变化历史 update a set gti05c = @newgoodid from gtc07 a, gam26 z where gti05c = @oldgoodid and gti12i = 2 and gti00c = z.gaz02c and z.gaz01c = @compid update a set a.gti13c = @newgoodid from gtc07 a, gam26 z where a.gti13c = @oldgoodid and a.gti12i = 2 and a.gti00c = z.gaz02c and z.gaz01c = @compid -- 卡销售充值-套餐物品 update a set gtj04c = @newgoodid from gtc10 a, gam26 z where gtj04c = @oldgoodid and gtj00c = z.gaz02c and z.gaz01c = @compid update a set gtj08c = @newgoodid from gtc10 a, gam26 z where gtj08c = @oldgoodid and gtj00c = z.gaz02c and z.gaz01c = @compid -- 寄存单的detail update a set gin02c = @newgoodid from gim21 a, gam26 z where gin02c = @oldgoodid and gin00c = z.gaz02c and z.gaz01c = @compid -- 寄存领取历史 update a set gio05c = @newgoodid from gim22 a, gam26 z where gio05c = @oldgoodid and gio00c = z.gaz02c and z.gaz01c = @compid -- 会员卡--暂存产品 update a set gcg02c = @newgoodid from gcm07 a, gam26 z where gcg02c = @oldgoodid and gcg00c = z.gaz02c and z.gaz01c = @compid -- 替换套餐的历史记录 update a set hej06c = @newgoodid from hem09 a, gam26 z where hej06c = @oldgoodid and hej05i = 2 and hej00c = z.gaz02c and z.gaz01c = @compid -- 缺货记录 update a set gcp03c = @newgoodid from gcm22 a, gam26 z where gcp03c = @oldgoodid and gcp00c = z.gaz02c and z.gaz01c = @compid -- 产品业绩提成设置 update a set a.pro01c = @newgoodid from productprecept a, gam26 z where a.pro01c = @oldgoodid and a.pro00c = z.gaz02c and z.gaz01c = @compid -- 单独产品提成设置 update a set a.gap01c = @newgoodid from aloneproductprecept a, gam26 z where a.gap01c = @oldgoodid and a.gap00c = z.gaz02c and z.gaz01c = @compid end GO /****** Object: StoredProcedure [dbo].[upg_m_dispatch_gam10] Script Date: 06/12/2017 06:20:18 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_m_dispatch_gam10] ( @fromcompid varchar(10), -- 从哪一个公司发布 @tocompid varchar(10), -- 目的公司 @mandatory varchar(2) -- 是否强制统一 ) as begin create table #gam10_1 -- 会员卡类别设定 ( gak00c varchar(10) not null, --公司编号 gak01c varchar(5) not null, --类别代码 gak02c varchar(40) null, --类别名称 gak03i int null, --大类(1-储值卡, 2- 资格卡, 3-计次卡, 4-单次卡) gak04i int null, --最多会员数(0 代表不限量) gak05i int null, --单次使用人数(0 代表不限量) gak06i int null, --最大使用人次(0 代表不限次数) gak07f float null, --有效期限 gak08c varchar(5) null, --期限单位 gak09f float null, --标准售价 gak10f float null, --自动升级条件-时间(月) gak11f float null, --自动升级条件-买卡额 gak12f float null, --自动升级条件-单次消费额 gak13f float null, --自动升级条件-累计消费额 gak14i int null, --账户类别(储值金额的存放处) gak15i int null, --级别(数字高代表级别高) gak16f float null, --储值金额 , Added by WUXM, 2004/02/16 gak17i tinyint null, --卡销售提成方式 gak18f float null, --卡销售金额或比率 gak19f float null, --成本 gak20i tinyint null, --卡销售业绩方式 gak21f float null, --卡销售金额或比率 gak22f float null, --项目消费实际营业额折算比率 gak23i int null, --储值类型,0-金额,1-点数 gak24f float null, --产品实际营业额折算比率 gak25i tinyint null, --卡充值业绩方式 gak26f float null, --卡充值金额或比率 gak27i tinyint null, --卡充值提成方式 gak28f float null, --卡充值金额或比率 gak29i int null, --卡介质类型 0磁卡 1IC卡, gak30i int null, --赠送积分的种类 gak31i float null, --赠送积分的值 如果为固定金额的那么此值就为赠送积分的值,如果是标准/实际值的比例那么在计算的时候就必须除上100; gak32f float null, --冻结比率 gak33f float null, --项目消费积分折算比率 gak34f float null, --产品消费积分折算比率 gak35i int null, --第二账户类别 gak36f float null, --第二账户金额 gak37i int null, --第三账户类别 gak38f float null, --第三账户金额 gak40f float null, --最低冲值额度 gak41i int null, --停止销售 1停止 gak42i int null, --停止充值 1停止 gak43f float null, --安全余额 Add by maql 2010/07/07 gak44i int null, --跨店消费标志 1 所有店 2 部分店 3 本店 gak45f float null, --业绩这算比率,项目 gak46f float null, --业绩这算比率,产品 gak49i int null, --赠送储值的模式 1 立即 2 分期 gak50i int null, --分期的期数 gak51i int null, --是否需要月末清零 gak73f float null, --默认充值金额 gak74i int null, -- 已过期会员卡能不能消费 1-不能消费 2-能消费不打折 默认为1-不能消费 gak75i int null, -- 已过期会员卡能不能充值 1-不能充值 2-能充值 默认为1-不能充值 gak76i int null, -- 是否禁用积分(0-不禁用 1-禁用 默认0-不禁用) gak77f float null, -- 最低卖卡金额 gak78i int default(0) null, -- 项目消费是否计算储值账户 gak79i int default(0) null, -- 产品是否计算储值账户 gak80i int default(0) null, -- 是否启用会员价 gak81f float null, -- 账户有期限 gak82c varchar(5) default(1) null, --账户有效期限单位 ) declare @PK_Sql nvarchar(max) set @PK_Sql = ' alter table #gam10_1 add constraint PK_#gam10_1_' + convert(varchar(10), cast(floor(rand() * 1000000) as int)) + ' primary key clustered(gak00c, gak01c)' exec sp_executesql @PK_Sql insert #gam10_1(gak00c, gak01c, gak02c, gak03i, gak04i, gak05i, gak06i, gak07f, gak08c, gak09f, gak10f, gak11f, gak12f, gak13f, gak14i, gak15i, gak16f, gak17i, gak18f, gak19f, gak20i, gak21f, gak22f, gak23i, gak24f, gak25i, gak26f, gak27i, gak28f, gak29i, gak30i, gak31i, gak32f, gak33f, gak34f, gak35i, gak36f, gak37i, gak38f, gak40f, gak41i, gak42i, gak43f, gak44i, gak45f, gak46f, gak49i, gak50i, gak51i, gak73f, gak74i, gak75i, gak76i, gak77f,gak78i,gak79i,gak80i,gak81f,gak82c) select gak00c, gak01c, gak02c, gak03i, gak04i, gak05i, gak06i, gak07f, gak08c, gak09f, gak10f, gak11f, gak12f, gak13f, gak14i, gak15i, gak16f, gak17i, gak18f, gak19f, gak20i, gak21f, gak22f, gak23i, gak24f, gak25i, gak26f, gak27i, gak28f, gak29i, gak30i, gak31i, gak32f, gak33f, gak34f, gak35i, gak36f, gak37i, gak38f, gak40f, gak41i, gak42i, gak43f, gak44i, gak45f, gak46f, gak49i, gak50i, gak51i, gak73f, gak74i, gak75i, gak76i, gak77f,gak78i,gak79i,gak80i,gak81f,gak82c from gam10 where gak00c = @fromcompid if @mandatory = '1' begin delete gam10 where gak00c = @tocompid end else begin delete a from #gam10_1 a, gam10 b where a.gak00c = @fromcompid and a.gak01c = b.gak01c and b.gak00c = @tocompid end insert gam10(gak00c, gak01c, gak02c, gak03i, gak04i, gak05i, gak06i, gak07f, gak08c, gak09f, gak10f, gak11f, gak12f, gak13f, gak14i, gak15i, gak16f, gak17i, gak18f, gak19f, gak20i, gak21f, gak22f, gak23i, gak24f, gak25i, gak26f, gak27i, gak28f, gak29i, gak30i, gak31i, gak32f, gak33f, gak34f, gak35i, gak36f, gak37i, gak38f, gak40f, gak41i, gak42i, gak43f, gak44i, gak45f, gak46f, gak49i, gak50i, gak51i, gak73f, gak74i, gak75i, gak76i, gak77f,gak78i,gak79i,gak80i,gak81f,gak82c) select @tocompid, gak01c, gak02c, gak03i, gak04i, gak05i, gak06i, gak07f, gak08c, gak09f, gak10f, gak11f, gak12f, gak13f, gak14i, gak15i, gak16f, gak17i, gak18f, gak19f, gak20i, gak21f, gak22f, gak23i, gak24f, gak25i, gak26f, gak27i, gak28f, gak29i, gak30i, gak31i, gak32f, gak33f, gak34f, gak35i, gak36f, gak37i, gak38f, gak40f, gak41i, gak42i, gak43f, gak44i, gak45f, gak46f, gak49i, gak50i, gak51i, gak73f, gak74i, gak75i, gak76i, gak77f,gak78i,gak79i,gak80i,gak81f,gak82c from #gam10_1 where gak00c = @fromcompid drop table #gam10_1 delete gam11 where gal00c = @tocompid insert gam11(gal00c, gal01c, gal02f, gal03c, gal04c) select @tocompid, gal01c, gal02f, gal03c, gal04c from gam11 where gal00c = @fromcompid delete gam15 where gao00c = @tocompid insert gam15(gao00c, gao01c, gao02f, gao03f, gao04c, gao05c, gao06f, gao07f, gao08f, gao10i, gao09c, gao11f) select @tocompid, gao01c, gao02f, gao03f, gao04c, gao05c, gao06f, gao07f, gao08f, gao10i, gao09c, gao11f from gam15 where gao00c = @fromcompid delete gam18 where gap00c = @tocompid insert gam18(gap00c, gap01c, gap02c, gap03f) select @tocompid, gap01c, gap02c, gap03f from gam18 where gap00c = @fromcompid delete gam24 where gav00c = @tocompid insert gam24(gav00c, gav01c, gav02c, gav03f, gav04f, gav05f, gav06f, gav07i, gav08f) select @tocompid, gav01c, gav02c, gav03f, gav04f, gav05f, gav06f, gav07i, gav08f from gam24 where gav00c = @fromcompid delete gam25 where gay00c = @tocompid insert gam25(gay00c, gay01c, gay02c, gay03f, gay04f, gay05f) select @tocompid, gay01c, gay02c, gay03f, gay04f, gay05f from gam25 where gay00c = @fromcompid delete gam09 where gaj00c = @tocompid insert gam09(gaj00c, gaj01c, gaj02c, gaj03i, gaj04c) select @tocompid, gaj01c, gaj02c, gaj03i, gaj04c from gam09 where gaj00c = @fromcompid -- Add by maql 20100602 升级控制 delete gzm01 where gza00c = @tocompid insert gzm01(gza00c, gza01c, gza02c, gza03c, gza04f, gza05f, gza06i, gza07i) select @tocompid, gza01c, gza02c, gza03c, gza04f, gza05f, gza06i, gza07i from gzm01 where gza00c = @fromcompid --赠送储值控制 --added by cyr 20120912 delete gzm05 where gze00c = @tocompid insert gzm05(gze00c, gze01c, gze02f, gze03f, gze04i) select @tocompid, gze01c, gze02f, gze03f, gze04i from gzm05 where gze00c = @fromcompid delete gam27 where compid = @tocompid insert gam27(compid, cardtype, descompid, ispermit) select @tocompid, cardtype, descompid, ispermit from gam27 where compid = @fromcompid delete s3_cardtype_itemtype_rate where compid = @tocompid insert s3_cardtype_itemtype_rate(compid, cardtype, kind, itemtype, rate) select @tocompid, cardtype, kind, itemtype, rate from s3_cardtype_itemtype_rate where compid = @fromcompid end GO /****** Object: StoredProcedure [dbo].[upg_get_cardtype_itemtype_rate] Script Date: 06/12/2017 06:20:06 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_get_cardtype_itemtype_rate] ( @compid varchar(10), -- 公司别 @cardtype varchar(20), @itemid varchar(20), @paycode varchar(20), -- 1 项目 2 产品 @kind varchar(20), -- 1 项目 2 产品 @rate float output ) as begin if @paycode <> '4' begin select @rate = 1 return end declare @itemtype varchar(20) -- 项目折扣类别 if isnull(@kind, '1') = '1' begin set @kind = '1' select @itemtype = gda04c from gdm01 with (nolock) where gda00c = @compid and gda01c = @itemid end else begin set @kind = '2' select @itemtype = gfa05c from gfm01 with (nolock) where gfa00c = @compid and gfa01c = @itemid end select @rate = rate from s3_cardtype_itemtype_rate a where a.compid = @compid and a.cardtype = @cardtype and a.itemtype = @itemtype and a.kind = @kind select @rate = isnull(@rate, 1) end GO /****** Object: StoredProcedure [dbo].[upg_m_dispatch_gam04] Script Date: 06/12/2017 06:20:18 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_m_dispatch_gam04] ( @fromcompid varchar(10), -- 从哪一个公司发布 @tocompid varchar(10), -- 目的公司 @mandatory varchar(2) -- 是否强制统一 ) as begin if @fromcompid = @tocompid return -- Add by maql 20100506 declare @curdate varchar(20) declare @curtime varchar(20) exec upg_getcurrdate @curdate output, @curtime output create table #gam04_01 -- 产品资料档 ( gad00c varchar(10) not null, -- 公司别 gad01c varchar(4) not null, -- 仓库编号 gad02c varchar(10) null, -- 仓库名称 gad03c varchar(20) null, -- 区域别 gad04c varchar(10) null, -- 仓管联络人 gad05c varchar(20) null, -- 联络电话 gad06c varchar(20) null, -- 传真 gad07c varchar(40) null, -- 仓库地址 gad08c varchar(20) null, -- 备注 ) declare @PK_Sql nvarchar(max) set @PK_Sql = ' alter table #gam04_01 add constraint PK_#gam04_01_' + convert(varchar(10), cast(floor(rand() * 1000000) as int)) + ' primary key clustered(gad00c, gad01c)' exec sp_executesql @PK_Sql insert #gam04_01(gad00c, gad01c, gad02c, gad03c, gad04c, gad05c, gad06c, gad07c, gad08c) select gad00c, gad01c, gad02c, gad03c, gad04c, gad05c, gad06c, gad07c, gad08c from gam04 where gad00c = @fromcompid if @mandatory = '1' begin delete gam04 where gad00c = @tocompid end else begin delete a from #gam04_01 a, gam04 b where a.gad00c = @fromcompid and a.gad01c = b.gad01c and b.gad00c = @tocompid end insert gam04(gad00c, gad01c, gad02c, gad03c, gad04c, gad05c, gad06c, gad07c, gad08c) select @tocompid, gad01c, gad02c, gad03c, gad04c, gad05c, gad06c, gad07c, gad08c from #gam04_01 where gad00c = @fromcompid -- 写入sysoperationlog Add by maql 20100506 insert sysoperationlog(userid, program, operation, operationdate, operationtime, origatedate, compid, keyvalue1, keyvalue2, keyvalue3) values('system', 'GSB04I', 'M', @curdate, @curtime, @curdate, @fromcompid, @tocompid, @mandatory, 'upg_m_dispatch_gam04') drop table #gam04_01 end GO /****** Object: StoredProcedure [dbo].[upg_trans_history] Script Date: 06/12/2017 06:20:47 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_trans_history] ( @oldcardid varchar(20), @newcardid varchar(20) ) as begin declare @sp039 varchar(20) select @sp039 = gsa02c from gsm01 where gsa00c = '001' and gsa01c = 'SP039' declare @gcm04_old_max int, @gcm04_old_min int declare @gcm04_new_max int, @gcm04_new_min int delete gcm04 where (gcd01c = @newcardid or gcd01c = @oldcardid) and isnull(gcd06f, 0) = 0 select @gcm04_old_max = max(gcd04f), @gcm04_old_min = min(gcd04f) from gcm04 with (nolock) where gcd01c = @oldcardid select @gcm04_new_max = max(gcd04f), @gcm04_new_min = min(gcd04f) from gcm04 with (nolock) where gcd01c = @newcardid select @gcm04_new_max = isnull(@gcm04_new_max, 0), @gcm04_new_min = isnull(@gcm04_new_min, 0) -- 把老的以及新的卡的gcm04的序号都调整为正数 if isnull(@gcm04_old_min, 0) < 0 begin update gcm04 set gcd04f = gcd04f - @gcm04_old_min where gcd01c = @oldcardid end if isnull(@gcm04_new_min, 0) < 0 begin update gcm04 set gcd04f = gcd04f - @gcm04_new_min where gcd01c = @newcardid end -- 然后取新的卡号的最大序号 select @gcm04_new_max = max(gcd04f) from gcm04 with (nolock) where gcd01c = @newcardid select @gcm04_new_max = isnull(@gcm04_new_max, 0), @gcm04_new_min = isnull(@gcm04_new_min, 0) update gcm04 set gcd01c = @newcardid, gcd04f = gcd04f + @gcm04_new_max + 1 where gcd01c = @oldcardid update gcm10 set gcl03c = @newcardid where gcl03c = @oldcardid update hem08 set hei02c = @newcardid where hei02c = @oldcardid if @sp039 = '1' -- 会员卡号与会员编号同步 begin update glm01 set gla06c = @newcardid, gla16c = @newcardid, gla27c = @newcardid where gla06c = @oldcardid update ggm01 set gga08c = @newcardid, gga09c = @newcardid, gga05c = @newcardid where gga08c = @oldcardid update gnm01 set gna13c = @newcardid, gna04c = @newcardid where gna13c = @oldcardid update gnm01 set gna09c = @newcardid where gna09c = @oldcardid delete gcm20 where gct02c = @newcardid update gcm20 set gct02c = @newcardid, gct03c = @newcardid where gct02c = @oldcardid update gem01 set gea07c = @newcardid, gea10c = @newcardid where gea07c = @oldcardid end else begin update glm01 set gla06c = @newcardid, gla16c = @newcardid where gla06c = @oldcardid update ggm01 set gga08c = @newcardid, gga09c = @newcardid where gga08c = @oldcardid update gnm01 set gna13c = @newcardid where gna13c = @oldcardid update gnm01 set gna09c = @newcardid where gna09c = @oldcardid delete gcm20 where gct02c = @newcardid update gcm20 set gct02c = @newcardid where gct02c = @oldcardid update gem01 set gea07c = @newcardid where gea07c = @oldcardid end update gsm03 set gsc08c = @newcardid where gsc08c = @oldcardid update gnm02 set gnb03c = @newcardid where gnb03c = @oldcardid update gcm13 set gco01c = @newcardid where gco01c = @oldcardid end GO /****** Object: StoredProcedure [dbo].[upg_delete_data_other] Script Date: 06/12/2017 06:19:58 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_delete_data_other]( @compid varchar(10), @from varchar(8), @to varchar(8)) as begin delete glm01 where gla00c<>@compid and gla02d >= @from and gla02d <= @to delete ggm01 where gga00c<>@compid and gga02d >= @from and gga02d <= @to delete gnm01 where gna00c<>@compid and gna02d >= @from and gna02d <= @to delete gcm10 where gcl00c<>@compid and gcl02d >= @from and gcl02d <= @to delete ham01 where haa00c<>@compid delete gdm01 where gda00c<>@compid delete gfm01 where gfa00c<>@compid delete gcm01 where gca13d<>gca00c and gca00c<>@compid delete gbm01 where gba00c<>@compid delete gcm13 where gco00c<>@compid delete gcm04 where gcd00c<>@compid end GO /****** Object: StoredProcedure [dbo].[upg_updateiccard_consumestat] Script Date: 06/12/2017 06:20:51 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_updateiccard_consumestat] ( @compid varchar(10), @billid varchar(20), @cardno varchar(20), @flag int ) as begin declare @amt float--储值账户 select @amt = isnull(gsc05f, 0) from gsm03 where gsc00c = @compid and gsc01c = @billid and gsc02c = 'gx' and gsc04c = '4' declare @seq int if @flag = 1 begin select @seq = gco02i from gcm13 where gco00c = @compid and gco01c = @cardno and isnull(gco13i, 0) = 0 and gco06f = @amt and gco10i = 2 end else begin select @seq = gco02i from gcm13 where gco00c = @compid and gco01c = @cardno and isnull(gco13i, 0) = 1 and gco06f = @amt and gco10i = 2 end if (@flag = 1) begin update gcm13 set gco13i = 1 where gco02i = @seq and gco00c = @compid end else begin update gcm13 set gco13i = 0 where gco02i = @seq and gco00c = @compid end end GO /****** Object: StoredProcedure [dbo].[upg_analyse_profit_inter_depart_emp_nochain] Script Date: 06/12/2017 06:19:26 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_analyse_profit_inter_depart_emp_nochain] ( @compid varchar(10), @fromdate varchar(10), @todate varchar(10), @flag int, -- flag 0:计算员工提成 1:不计算员工提成 @staff varchar(20), -- 收银人员 @fromdepart varchar(10), -- 部门 --add by lyj 2007-09-06 @todepart varchar(10), @calmode int ) as begin declare @amt float declare @amt2 float declare @amt3 float declare @itemtype varchar(10) declare @itemname varchar(60) declare @id int --变量 declare @code_no varchar(10) declare @isadd_0 float declare @isadd_3 float declare @isadd_$ float declare @incompid varchar(10) select @incompid = @compid declare @add_card_sale int declare @add_card_svr int -- 卡付是否作为收入 declare @sp215f float --充值疗程储值方式支付是否作为卡付实际营业额 declare @gak24f float declare @gak22f float select @sp215f = gsa03f from gsm01 with (nolock) where gsa00c = @compid and gsa01c = 'SP212' if @calmode = 2 begin declare @sp115f float select @sp115f = gsa03f from gsm01 with (nolock) where gsa00c = @compid and gsa01c = 'SP115' if @sp115f = 0 select @calmode = 0 else select @calmode = 1 end if @calmode = 0 begin select @add_card_sale = 1 select @add_card_svr = 0 end else begin select @add_card_sale = 0 select @add_card_svr = 1 end select @isadd_0 = gso07f from gsm15 with (nolock) where gso00c = @incompid and gso02c = '0' select @isadd_3 = gso07f from gsm15 with (nolock) where gso00c = @incompid and gso02c = '3' select @isadd_$ = gso07f from gsm15 with (nolock) where gso00c = @incompid and gso02c = '$' select @isadd_0 = isnull(@isadd_0, 0) select @isadd_3 = isnull(@isadd_3, 0) select @isadd_$ = isnull(@isadd_$, 0) select @incompid = @compid if @staff = '*' or @staff = '' begin select @staff = '%' end declare @sp055f float select @sp055f = gsa03f from gsm01 with (nolock) where gsa00c = @incompid and gsa01c = 'SP055' declare @sp048 varchar(10) select @sp048 = gsa02c from gsm01 with (nolock) where gsa00c = @incompid and gsa01c = 'SP048' create table #customed_proj_count--总客数 add by lm 2016.06.21 ( customed_num float null, empno varchar(20) not null, compid varchar(20) not null primary key (compid,empno) ) create table #emp_info --add by lm 2016.06.21 ( empno varchar(20) not null, empname varchar(20) null, compid varchar(20) not null primary key (compid, empno) ) insert #emp_info(compid, empno,empname) select gsb00c, gsb02c,gsb03c from gsm02 a with(nolock) where a.gsb00c = @compid and a.gsb01c = 'U' and ((a.gsb02c between @fromdepart and @todepart )or @fromdepart = '*') create table #gsm03_tmp -- 单据--支付明细 ( gsc00c varchar(20) not null, -- 公司编号 gsc01c varchar(20) not null, -- 单据编号 gsc02c varchar(5) not null, -- 单据类别 gsc04c varchar(5) not null, -- 支付方式 gsc05f float not null, -- 支付金额 gsc10d varchar(8) not null, -- 单据帐务日期 gsc11c varchar(20) not null, -- 登陆人 gsc12i int not null, -- 方式 empno varchar(20) null, -- 部门编号 isnotcard int null default 0 -- 标志是否为卡销售的,为了区分充值时候的产品或项目的欠款与还款 ) create nonclustered index gsm03_tmp_idx_02 on #gsm03_tmp(gsc02c) create clustered index gsm03_tmp_idx_01 on #gsm03_tmp(gsc04c) create nonclustered index gsm03_tmp_idx_03 on #gsm03_tmp(gsc00c, empno) --modify by lm 2015-10-14 create table #gsm03_tmp_gem01 -- 竞争转卡--支付明细 ( gsc00c varchar(20) not null, -- 公司编号 gsc01c varchar(20) not null, -- 单据编号 gsc02c varchar(5) not null, -- 单据类别 gsc04c varchar(5) not null, -- 支付方式 gsc05f float not null, -- 支付金额 gsc10d varchar(8) not null, -- 单据帐务日期 gsc11c varchar(20) not null, -- 登陆人 gsc12i int not null, -- 方式 empno varchar(20) null, -- 部门编号 isnotcard int null default 0 -- 标志是否为卡销售的,为了区分充值时候的产品或项目的欠款与还款 ) -- create clustered index gem01_tmp_idx_01 on #gsm03_tmp_gem01(gsc00c, empno) --因为项目或产品的欠款也是纪录在会员账户管理中,而且还款也是纪录在其中 --所以必须把这些特殊的gcm10标记出来,否则在统计会员卡销售的时候就不准 -- modify by lyj for add department 2007-09-06 -- 因为这个涉及到一个单据有几种支付方式的问题,所以只有先把gnm01 gcm10, ggm02, ggm03 gqm01, gem01 插入到 -- 临时表,然后和#gsm03_tmp_01 通过单号和公司别,单据类型来链接, 这样才能把gnm01 gcm10 gem01, gqm01 中的支付方式给分开 -- 但是对于ggm02 和ggm03 还要判断ggm02,ggm03中支付方式和支付明细中的支付方式相同才能把支付方式分开,所以现在 -- 按部门统计的时候支付方式必须用项目支付. -- 把值插入到 #gsm03_tmp declare @SP031 float --是否为多账单统一结帐 select @SP031 = gsa03f from gsm01 with (nolock) where gsa00c = @incompid and gsa01c = 'SP031' create table #dept_bills ( flag int identity (1, 1) not null, compid varchar(10) null, billtype varchar(5) null, billno varchar(20) null, payway varchar(5) null, payamt float null, -- 当从ggm02, ggm03中取数据的时候此值要填写上 ratio float null, -- 并不是整个单据都属于一个部门的,是部分 empno varchar(20) null, -- 部门编号 ) -- 并且在gsm03和ggm02, ggm03链接的时候, #gsm03_tmp 中的值应该是payamt 而不是gsm03中的gsc05f create clustered index departresult_01 on #dept_bills(billtype, billno, payway) create table #dept_bills_dept ( flag int identity (1, 1) not null, compid varchar(10) null, billtype varchar(5) null, billno varchar(20) null, payway varchar(5) null, payamt float null, -- 当从ggm02, ggm03中取数据的时候此值要填写上 empno varchar(20) null, -- 部门编号 ratio float null -- 并不是整个单据都属于一个部门的,是部分 ) create clustered index idx_dept_bills_dept on #dept_bills_dept(billtype, billno, payway) create table #tbl_gnm01 ( gna00c varchar(10) not null, gna01c varchar(20) not null, gna27f float null, --第一分享比率 gna29f float null, --分享比率2 gna31f float null, --分享比率3 gna33f float null, --分享比率4 gna35f float null, --分享比率5 gna37f float null, --分享比率6 gna39f float null, --分享比率7 gna41f float null, --分享比率8 gna43f float null, --分享比率9 gna45f float null, --分享比率10 gna81c varchar(10) null, --部门编号 gna82c varchar(10) null, --部门编号2 gna83c varchar(10) null, --部门编号3 gna100c varchar(10) null, --部门编号4 Add by maql 20100705 gna101c varchar(10) null, --部门编号5 Add by maql 20100705 gna102c varchar(10) null, --部门编号6 Add by maql 20100705 gna103c varchar(10) null, --部门编号7 Add by maql 20100705 gna104c varchar(10) null, --部门编号8 Add by maql 20100705 gna105c varchar(10) null, --部门编号9 Add by maql 20100705 gna106c varchar(10) null, --部门编号10 Add by maql 20100705 gna67f float null, gna69f float null, gna71f float null, gna86f float null, gna87f float null, gna88f float null, gna89f float null, gna90f float null, gna91f float null, gna92f float null, bill_total_amt float null, gna26f float null, gnb08f float null, ) create clustered index idx_tbl_gnm01 on #tbl_gnm01(gna00c, gna01c) create nonclustered index idx_tbl_gnm01_02 on #tbl_gnm01(gna81c) create nonclustered index idx_tbl_gnm01_03 on #tbl_gnm01(gna82c) create nonclustered index idx_tbl_gnm01_04 on #tbl_gnm01(gna83c) create nonclustered index idx_tbl_gnm01_05 on #tbl_gnm01(gna100c) create nonclustered index idx_tbl_gnm01_06 on #tbl_gnm01(gna101c) create nonclustered index idx_tbl_gnm01_07 on #tbl_gnm01(gna102c) create nonclustered index idx_tbl_gnm01_08 on #tbl_gnm01(gna103c) create nonclustered index idx_tbl_gnm01_09 on #tbl_gnm01(gna104c) create nonclustered index idx_tbl_gnm01_10 on #tbl_gnm01(gna105c) create nonclustered index idx_tbl_gnm01_11 on #tbl_gnm01(gna106c) create table #tbl_gcm10 ( gcl00c varchar(10) not null, --公司编号 gcl01c varchar(20) not null, --异动单号 gcl16f float null, --第一销售分享比率 gcl17c varchar(20) null, --第二销售 gcl18f float null, --分享比率2 gcl19c varchar(20) null, --第三负责人 gcl20f float null, --分享比率3 gcl23f float null, --分享比率4 gcl25f float null, --分享比率5 gcl27f float null, --分享比率6 gcl29f float null, --分享比率7 gcl31f float null, --分享比率8 gcl33f float null, --分享比率9 gcl35f float null, --分享比率10 gcl81c varchar(10) null, --部门编号 gcl82c varchar(10) null, --部门编号2 gcl83c varchar(10) null, --部门编号3 gcl100c varchar(10) null, --部门编号4 gcl101c varchar(10) null, --部门编号5 gcl102c varchar(10) null, --部门编号6 gcl103c varchar(10) null, --部门编号7 gcl104c varchar(10) null, --部门编号8 gcl105c varchar(10) null, --部门编号9 gcl106c varchar(10) null, --部门编号10 gcl58f float null, gcl60f float null, gcl62f float null, gcl83f float null, gcl84f float null, gcl85f float null, gcl86f float null, gcl87f float null, gcl88f float null, gcl89f float null, gcl07f float null, bill_total_amt float null ) create clustered index idx_tbl_gcm10 on #tbl_gcm10(gcl00c, gcl01c) create nonclustered index idx_tbl_gcm10_02 on #tbl_gcm10(gcl81c) create nonclustered index idx_tbl_gcm10_03 on #tbl_gcm10(gcl82c) create nonclustered index idx_tbl_gcm10_04 on #tbl_gcm10(gcl83c) create nonclustered index idx_tbl_gcm10_05 on #tbl_gcm10(gcl100c) create nonclustered index idx_tbl_gcm10_06 on #tbl_gcm10(gcl101c) create nonclustered index idx_tbl_gcm10_07 on #tbl_gcm10(gcl102c) create nonclustered index idx_tbl_gcm10_08 on #tbl_gcm10(gcl103c) create nonclustered index idx_tbl_gcm10_09 on #tbl_gcm10(gcl104c) create nonclustered index idx_tbl_gcm10_10 on #tbl_gcm10(gcl105c) create nonclustered index idx_tbl_gcm10_11 on #tbl_gcm10(gcl106c) insert #tbl_gnm01(gna00c, gna01c, gna27f, gna29f, gna31f, gna33f, gna35f, gna37f, gna39f, gna41f, gna43f, gna45f, gna81c, gna82c, gna83c, gna100c, gna101c, gna102c, gna103c, gna104c, gna105c, gna106c, gna67f, gna69f, gna71f, gna86f, gna87f, gna88f, gna89f, gna90f, gna91f, gna92f, gna26f, gnb08f, bill_total_amt) select gna00c, gna01c, gna27f, gna29f, gna31f, gna33f, gna35f, gna37f, gna39f, gna41f, gna43f, gna45f, gna81c, gna82c, gna83c, gna100c, gna101c, gna102c, gna103c, gna104c, gna105c, gna106c, gna67f, gna69f, gna71f, gna86f, gna87f, gna88f, gna89f, gna90f, gna91f, gna92f, gna26f, gnb08f, isnull(gna67f, 0) + isnull(gna69f, 0) + isnull(gna71f, 0) + isnull(gna86f, 0) + isnull(gna87f, 0) + isnull(gna88f, 0) + isnull(gna89f, 0) + isnull(gna90f, 0) + isnull(gna91f, 0) + isnull(gna92f, 0) from gnm01 a with (nolock), gnm02 c with (nolock) where a.gna00c = @incompid and a.gna00c = c.gnb00c and a.gna01c = c.gnb01c and gna80d between @fromdate and @todate and gna93c like @staff insert #tbl_gcm10(gcl00c, gcl01c, gcl16f, gcl17c, gcl18f, gcl19c, gcl20f, gcl23f, gcl25f, gcl27f, gcl29f, gcl31f, gcl33f, gcl35f, gcl81c, gcl82c, gcl83c, gcl100c, gcl101c, gcl102c, gcl103c, gcl104c, gcl105c, gcl106c, gcl58f, gcl60f, gcl62f, gcl83f, gcl84f, gcl85f, gcl86f, gcl87f, gcl88f, gcl89f, gcl07f, bill_total_amt) select gcl00c, gcl01c, gcl16f, gcl17c, gcl18f, gcl19c, gcl20f, gcl23f, gcl25f, gcl27f, gcl29f, gcl31f, gcl33f, gcl35f, gcl81c, gcl82c, gcl83c, gcl100c, gcl101c, gcl102c, gcl103c, gcl104c, gcl105c, gcl106c, gcl58f, gcl60f, gcl62f, gcl83f, gcl84f, gcl85f, gcl86f, gcl87f, gcl88f, gcl89f, gcl07f, isnull(gcl58f, 0) + isnull(gcl60f, 0) + isnull(gcl62f, 0) + isnull(gcl83f, 0) + isnull(gcl84f, 0) + isnull(gcl85f, 0) + isnull(gcl86f, 0) + isnull(gcl87f, 0) + isnull(gcl88f, 0) + isnull(gcl89f, 0) from gcm10 a with (nolock) where gcl00c = @incompid and gcl80d between @fromdate and @todate and gcl93c like @staff --为了提高性能先把此值插入到临时表 create table #gsm03_tmp_01 -- 单据--支付明细 ( gsc00c varchar(10) not null, --公司 gsc01c varchar(20) not null, --单据编号 gsc02c varchar(5) not null, --单据类别 gsc04c varchar(5) not null, --支付方式 gsc05f float not null, --支付金额 gsc10d varchar(8) not null, --单据帐务日期 gsc11c varchar(20) not null, --登陆人 gsc12i int not null --方式 ) create nonclustered index gsm03_tmp_01_01 on #gsm03_tmp_01(gsc02c) create nonclustered index gsm03_tmp_01_02 on #gsm03_tmp_01(gsc01c) -------把gsm03里面符合条件的先insert到临时表中 if (@compid = '001' or @compid = '*') begin insert #gsm03_tmp_01(gsc00c, gsc01c, gsc02c, gsc04c, gsc05f, gsc10d, gsc11c, gsc12i) select gsc00c, gsc01c, gsc02c, gsc04c, isnull(gsc05f, 0), gsc10d, gsc11c, isnull(gsc12i, -1) from gsm03 with (nolock) where gsc10d >= @fromdate and gsc10d <= @todate and gsc11c like @staff end else begin insert #gsm03_tmp_01(gsc00c, gsc01c, gsc02c, gsc04c, gsc05f, gsc10d, gsc11c, gsc12i) select gsc00c, gsc01c, gsc02c, gsc04c, isnull(gsc05f, 0), gsc10d, gsc11c, isnull(gsc12i, -1) from gsm03 with (nolock) where gsc00c = @compid and gsc10d >= @fromdate and gsc10d <= @todate and gsc11c like @staff end create table #tbl_bill_total ( compid varchar(20) not null, billid varchar(20) not null, amt float null, primary key(compid, billid)) if (@compid = '001' or @compid = '*') begin insert #tbl_bill_total(compid, billid, amt) select gnb00c, gnb01c, sum(gnb08f) from gnm01 c with (nolock), gnm02 a with (nolock) where c.gna80d >= @fromdate and c.gna80d <= @todate and c.gna00c = a.gnb00c and c.gna01c = a.gnb01c group by gnb00c, gnb01c end else begin insert #tbl_bill_total(compid, billid, amt) select gnb00c, gnb01c, sum(gnb08f) from gnm01 c with (nolock), gnm02 a with (nolock) where a.gnb00c = @compid and c.gna80d >= @fromdate and c.gna80d <= @todate and c.gna00c = a.gnb00c and c.gna01c = a.gnb01c group by gnb00c, gnb01c end ---在营业数据插入到#departsesult临时表 insert #dept_bills_dept(compid, billtype, billno, ratio, payway, payamt, empno) select gna00c, 'gn', gna01c, isnull(gna27f, 0), gsc04c, case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gna67f,0)*b.gsc05f / bill_total_amt end , gna81c from #tbl_gnm01 a, #gsm03_tmp_01 b where a.gna00c = b.gsc00c and a.gna01c = b.gsc01c and b.gsc02c = 'gn' and (isnull(gna81c, '') between @fromdepart and @todepart ) --add by lyj 2007-09-06 insert #dept_bills_dept(compid, billtype, billno, ratio, payway, payamt, empno) select gna00c, 'gn', gna01c, isnull(gna29f, 0), gsc04c, case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gna69f,0)*b.gsc05f / bill_total_amt end, gna82c from #tbl_gnm01 a, #gsm03_tmp_01 b where a.gna00c = b.gsc00c and a.gna01c = b.gsc01c and b.gsc02c = 'gn' and (isnull(gna82c, '') between @fromdepart and @todepart ) --add by lyj 2007-09-06 insert #dept_bills_dept(compid, billtype, billno, ratio, payway, payamt, empno) select gna00c, 'gn', gna01c, isnull(gna31f, 0),gsc04c, case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gna71f,0)*b.gsc05f / bill_total_amt end, gna83c from #tbl_gnm01 a, #gsm03_tmp_01 b where a.gna00c = b.gsc00c and a.gna01c = b.gsc01c and b.gsc02c = 'gn' and (isnull(gna83c, '') between @fromdepart and @todepart ) --add by lyj 2007-09-06 insert #dept_bills_dept(compid, billtype, billno, ratio, payway, payamt, empno) select gna00c, 'gn', gna01c, isnull(gna33f, 0), gsc04c, case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gna86f,0)*b.gsc05f / bill_total_amt end, gna100c from #tbl_gnm01 a, #gsm03_tmp_01 b where a.gna00c = b.gsc00c and a.gna01c = b.gsc01c and b.gsc02c = 'gn' and (isnull(gna100c, '') between @fromdepart and @todepart ) --add by maql 20100705 insert #dept_bills_dept(compid, billtype, billno, ratio, payway, payamt, empno) select gna00c, 'gn', gna01c, isnull(gna35f, 0), gsc04c, case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gna87f,0)*b.gsc05f / bill_total_amt end, gna101c from #tbl_gnm01 a, #gsm03_tmp_01 b where a.gna00c = b.gsc00c and a.gna01c = b.gsc01c and b.gsc02c = 'gn' and (isnull(gna101c, '') between @fromdepart and @todepart ) --add by maql 20100705 insert #dept_bills_dept(compid, billtype, billno, ratio, payway, payamt, empno) select gna00c, 'gn', gna01c, isnull(gna37f, 0), gsc04c, case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gna88f,0)*b.gsc05f / bill_total_amt end , gna102c from #tbl_gnm01 a, #gsm03_tmp_01 b where a.gna00c = b.gsc00c and a.gna01c = b.gsc01c and b.gsc02c = 'gn' and (isnull(gna102c, '') between @fromdepart and @todepart ) --add by maql 20100705 insert #dept_bills_dept(compid, billtype, billno, ratio, payway, payamt, empno) select gna00c, 'gn', gna01c, isnull(gna39f, 0), gsc04c, case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gna89f,0)*b.gsc05f / bill_total_amt end , gna103c from #tbl_gnm01 a, #gsm03_tmp_01 b where a.gna00c = b.gsc00c and a.gna01c = b.gsc01c and b.gsc02c = 'gn' and (isnull(gna103c, '') between @fromdepart and @todepart ) --add by maql 20100705 insert #dept_bills_dept(compid, billtype, billno, ratio, payway, payamt, empno) select gna00c, 'gn', gna01c, isnull(gna41f, 0), gsc04c, case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gna90f,0)*b.gsc05f / bill_total_amt end, gna104c from #tbl_gnm01 a, #gsm03_tmp_01 b where a.gna00c = b.gsc00c and a.gna01c = b.gsc01c and b.gsc02c = 'gn' and (isnull(gna104c, '') between @fromdepart and @todepart ) --add by maql 20100705 insert #dept_bills_dept(compid, billtype, billno, ratio, payway, payamt, empno) select gna00c, 'gn', gna01c, isnull(gna43f, 0), gsc04c, case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gna91f,0)*b.gsc05f / bill_total_amt end, gna105c from #tbl_gnm01 a, #gsm03_tmp_01 b where a.gna00c = b.gsc00c and a.gna01c = b.gsc01c and b.gsc02c = 'gn' and (isnull(gna105c, '') between @fromdepart and @todepart ) --add by maql 20100705 insert #dept_bills_dept(compid, billtype, billno, ratio, payway, payamt, empno) select gna00c, 'gn', gna01c, isnull(gna45f, 0), gsc04c, case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gna92f,0)*b.gsc05f / bill_total_amt end, gna106c from #tbl_gnm01 a, #gsm03_tmp_01 b where a.gna00c = b.gsc00c and a.gna01c = b.gsc01c and b.gsc02c = 'gn' and (isnull(gna106c, '') between @fromdepart and @todepart ) --add by maql 20100705 insert into #dept_bills(compid, billtype, billno, ratio, payway, payamt, empno) select compid, billtype, billno, sum(ratio), payway, sum(payamt),empno from #dept_bills_dept a group by a.compid, a.billtype, a.billno, a.payway, empno drop table #tbl_gnm01 -- insert gcm10 truncate table #dept_bills_dept -- 第一销售 insert #dept_bills_dept(compid, billtype, billno, ratio, payway, payamt, empno ) select gcl00c, 'gz', gcl01c, isnull(gcl16f, 0), gsc04c, case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gcl58f,0) * b.gsc05f / bill_total_amt end , gcl81c from #tbl_gcm10 a, #gsm03_tmp_01 b where a.gcl00c = b.gsc00c and a.gcl01c = b.gsc01c and b.gsc02c = 'gz' and (isnull(gcl81c, '') between @fromdepart and @todepart ) --add by lyj 2007-09-06 insert #dept_bills_dept(compid, billtype, billno, ratio, payway, payamt, empno) select gcl00c, 'gz', gcl01c, isnull(gcl18f, 0), gsc04c, case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gcl60f,0) * b.gsc05f / bill_total_amt end , gcl82c from #tbl_gcm10 a, #gsm03_tmp_01 b where a.gcl00c = b.gsc00c and a.gcl01c = b.gsc01c and b.gsc02c = 'gz' and (isnull(gcl82c, '') between @fromdepart and @todepart ) --add by lyj 2007-09-06 insert #dept_bills_dept(compid, billtype, billno, ratio, payway, payamt, empno) select gcl00c, 'gz', gcl01c, isnull(gcl20f, 0), gsc04c, case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gcl62f,0) * b.gsc05f / bill_total_amt end , gcl83c from #tbl_gcm10 a, #gsm03_tmp_01 b where a.gcl00c = b.gsc00c and a.gcl01c = b.gsc01c and b.gsc02c = 'gz' and (isnull(gcl83c, '') between @fromdepart and @todepart ) --add by lyj 2007-09-06 insert #dept_bills_dept(compid, billtype, billno, ratio, payway, payamt, empno) select gcl00c, 'gz', gcl01c, isnull(gcl23f, 0), gsc04c, case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gcl83f,0) * b.gsc05f / bill_total_amt end, gcl100c from #tbl_gcm10 a, #gsm03_tmp_01 b where a.gcl00c = b.gsc00c and a.gcl01c = b.gsc01c and b.gsc02c = 'gz' and (isnull(gcl100c, '') between @fromdepart and @todepart ) --add by maql 20100705 insert #dept_bills_dept(compid, billtype, billno, ratio, payway, payamt, empno) select gcl00c, 'gz', gcl01c, isnull(gcl25f, 0), gsc04c, case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gcl84f,0) * b.gsc05f / bill_total_amt end, gcl101c from #tbl_gcm10 a, #gsm03_tmp_01 b where a.gcl00c = b.gsc00c and a.gcl01c = b.gsc01c and b.gsc02c = 'gz' and (isnull(gcl101c, '') between @fromdepart and @todepart ) --add by maql 20100705 insert #dept_bills_dept(compid, billtype, billno, ratio, payway, payamt, empno) select gcl00c, 'gz', gcl01c, isnull(gcl27f, 0), gsc04c, case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gcl85f,0) * b.gsc05f / bill_total_amt end, gcl102c from #tbl_gcm10 a, #gsm03_tmp_01 b where a.gcl00c = b.gsc00c and a.gcl01c = b.gsc01c and b.gsc02c = 'gz' and (isnull(gcl102c, '') between @fromdepart and @todepart ) --add by maql 20100705 insert #dept_bills_dept(compid, billtype, billno, ratio, payway, payamt, empno) select gcl00c, 'gz', gcl01c, isnull(gcl29f, 0), gsc04c, case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gcl86f,0) * b.gsc05f / bill_total_amt end, gcl103c from #tbl_gcm10 a, #gsm03_tmp_01 b where a.gcl00c = b.gsc00c and a.gcl01c = b.gsc01c and b.gsc02c = 'gz' and (isnull(gcl103c, '') between @fromdepart and @todepart ) --add by maql 20100705 insert #dept_bills_dept(compid, billtype, billno, ratio, payway, payamt, empno) select gcl00c, 'gz', gcl01c, isnull(gcl31f, 0), gsc04c, case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gcl87f,0) * b.gsc05f / bill_total_amt end, gcl104c from #tbl_gcm10 a, #gsm03_tmp_01 b where a.gcl00c = b.gsc00c and a.gcl01c = b.gsc01c and b.gsc02c = 'gz' and (isnull(gcl104c, '') between @fromdepart and @todepart ) --add by maql 20100705 insert #dept_bills_dept(compid, billtype, billno, ratio, payway, payamt, empno) select gcl00c, 'gz', gcl01c, isnull(gcl33f, 0), gsc04c, case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gcl88f,0) * b.gsc05f / bill_total_amt end, gcl105c from #tbl_gcm10 a, #gsm03_tmp_01 b where a.gcl00c = b.gsc00c and a.gcl01c = b.gsc01c and b.gsc02c = 'gz' and (isnull(gcl105c, '') between @fromdepart and @todepart ) --add by maql 20100705 insert #dept_bills_dept(compid, billtype, billno, ratio, payway, payamt, empno) select gcl00c, 'gz', gcl01c, isnull(gcl35f, 0), gsc04c, case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gcl89f,0) * b.gsc05f / bill_total_amt end, gcl106c from #tbl_gcm10 a, #gsm03_tmp_01 b where a.gcl00c = b.gsc00c and a.gcl01c = b.gsc01c and b.gsc02c = 'gz' and (isnull(gcl106c, '') between @fromdepart and @todepart ) --add by maql 20100705 insert #dept_bills(compid, billtype, billno, ratio, payway, payamt, empno) select compid, billtype, billno, sum(ratio), payway, sum(payamt), empno from #dept_bills_dept a group by a.compid, a.billtype, a.billno, a.payway, empno drop table #tbl_gcm10 drop table #dept_bills_dept if (@SP031 = '1') --收银的时候直接结帐 begin insert #dept_bills(compid, billtype, billno, payway, payamt, empno) select ggb00c, 'gx', ggb01c, ggb27c, isnull(ggb11f, 0),ggb81c from ggm02 with (nolock), ggm01 with (nolock) where gga00c = ggb00c and gga01c = ggb01c and gga00c = @incompid and gga80d between @fromdate and @todate and (isnull(ggb81c, '') between @fromdepart and @todepart ) --add by lyj 2007-09-06 and gga93c like @staff insert #customed_proj_count (compid, customed_num, empno) select gga00c, sum(isnull(ggb05f,0)),ggb81c from ggm02 with (nolock), ggm01 with (nolock) where gga00c = ggb00c and gga01c = ggb01c and gga00c = @incompid and gga80d between @fromdate and @todate and (isnull(ggb81c, '') between @fromdepart and @todepart ) --add by lyj 2007-09-06 and gga93c like @staff group by ggb81c,gga00c insert #dept_bills(compid, billtype, billno, payway, payamt, empno) select ggc00c, 'gx', ggc01c, ggc27c, case when @sp048 = '2' then ggc21f else isnull(ggc11f, 0) * ggc14f end, ggc81c from ggm03 with (nolock), ggm01 with (nolock) where ggc00c = gga00c and gga01c = ggc01c and gga00c = @incompid and gga80d between @fromdate and @todate and (isnull(ggc81c, '') between @fromdepart and @todepart ) --add by lyj 2007-09-06 and gga93c like @staff union all select ggc00c, 'gx', ggc01c, ggc27c, case when @sp048 = '2' then ggc23f else isnull(ggc11f, 0) * ggc16f end, ggc82c from ggm03 with (nolock), ggm01 with (nolock) where ggc00c = gga00c and gga01c = ggc01c and gga00c = @incompid and gga80d between @fromdate and @todate and (isnull(ggc82c, '') between @fromdepart and @todepart ) --add by lyj 2007-09-06 and gga93c like @staff union all select ggc00c, 'gx', ggc01c, ggc27c, case when @sp048 = '2' then ggc25f else isnull(ggc11f, 0) * ggc18f end, ggc83c from ggm03 with (nolock), ggm01 with (nolock) where ggc00c = gga00c and gga01c = ggc01c and gga00c = @incompid and gga80d between @fromdate and @todate and (isnull(ggc83c, '') between @fromdepart and @todepart ) --add by lyj 2007-09-06 and gga93c like @staff end else if (@SP031 = '2')--多账单统一结帐 begin insert #dept_bills(compid, billtype, billno, payway, payamt, empno) select ggb00c, 'gl', gga30c, ggb27c, isnull(ggb11f, 0), ggb81c from ggm02 with (nolock), ggm01 with (nolock) where gga00c = ggb00c and gga01c = ggb01c and ggb00c = @incompid and gga80d between @fromdate and @todate and (isnull(ggb81c, '') between @fromdepart and @todepart ) --add by lyj 2007-09-06 and gga93c like @staff insert #customed_proj_count (customed_num, compid, empno) select sum(isnull(ggb05f,0)),gga00c, ggb81c from ggm02 with (nolock), ggm01 with (nolock) where gga00c = ggb00c and gga01c = ggb01c and ggb00c = @incompid and gga80d between @fromdate and @todate and (isnull(ggb81c, '') between @fromdepart and @todepart ) --add by lyj 2007-09-06 and gga93c like @staff group by gga00c, ggb81c --insert ggm03 insert #dept_bills(compid, billtype, billno, payway, payamt, empno) select ggc00c, 'gl', gga30c, ggc27c, case when @sp048 = '2' then ggc21f else isnull(ggc11f, 0) * ggc14f end, ggc81c from ggm03 with (nolock), ggm01 with (nolock) where ggc00c = gga00c and gga01c = ggc01c and gga00c = @incompid and gga80d between @fromdate and @todate and (isnull(ggc81c, '') between @fromdepart and @todepart ) --add by lyj 2007-09-06 and gga93c like @staff union all select ggc00c, 'gl', gga30c, ggc27c, case when @sp048 = '2' then ggc23f else isnull(ggc11f, 0) * ggc16f end, ggc82c from ggm03 with (nolock), ggm01 with (nolock) where ggc00c = gga00c and gga01c = ggc01c and gga00c = @incompid and gga80d between @fromdate and @todate and (isnull(ggc82c, '') between @fromdepart and @todepart ) --add by lyj 2007-09-06 and gga93c like @staff union all select ggc00c, 'gl', gga30c, ggc27c, case when @sp048 = '2' then ggc25f else isnull(ggc11f, 0) * ggc18f end, ggc83c from ggm03 with (nolock), ggm01 with (nolock) where ggc00c = gga00c and gga01c = ggc01c and gga00c = @incompid and gga80d between @fromdate and @todate and (isnull(ggc83c, '') between @fromdepart and @todepart ) --add by lyj 2007-09-06 and gga93c like @staff end --insert gqm01 --费用登记 insert #dept_bills(compid, billtype, billno, empno) select gqa00c, 'gv', gqa01c, gqa81c from gqm01 with (nolock) where gqa00c = @incompid and gqa80d between @fromdate and @todate and (isnull(gqa81c, '') between @fromdepart and @todepart ) --add by lyj 2007-09-06 and gqa07c like @staff -------把#gsm03_tmp_01和#departsesult里面符合条件的先insert到#gsm03_tmp临时表中 insert #gsm03_tmp(gsc00c, gsc01c, gsc02c, gsc04c, gsc05f, gsc10d, gsc11c, gsc12i, empno) select gsc00c, gsc01c, billtype, payway, isnull(payamt, 0), gsc10d, gsc11c, isnull(gsc12i, -1),empno from #dept_bills a, #gsm03_tmp_01 b where b.gsc01c = a.billno and b.gsc00c = a.compid and b.gsc02c = a.billtype and b.gsc04c = a.payway and b.gsc11c like @staff --Start竞争转卡业绩 add by lm 2015-10-13 create table #gem01_tmp -- 会员卡--异动单 ( gea00c char(10) not null, --门店编号 gea01c varchar(20) not null, --异动单号 gea07c varchar(20) null, --异动後卡号(只对 -卡种变更 有效) gea21c varchar(20) null, --销售人员(对 -卡种变更 有效) gea27f float null, --第一销售业绩 gea28c varchar(20) null, --第二销售(支付方式4) gea29f float null, --第二销售业绩 gea30c varchar(20) null, --第3销售 gea31f float null, --第3销售业绩 gea32c varchar(20) null, --第4销售 gea33f float null, --第4销售业绩 gea34c varchar(20) null, --第5销售 gea35f float null, --第5销售业绩 gea36c varchar(20) null, --第6销售 gea37f float null, --第6销售业绩 gea38c varchar(20) null, --第7销售 gea39f float null, --第7销售业绩 gea40c varchar(20) null, --第8销售 gea41f float null, --第8销售业绩 gea42c varchar(20) null, --第9销售 gea43f float null, --第9销售业绩 gea44c varchar(20) null, --第10销售 gea45f float null, --第10销售业绩 geb09i int null, --收支方向(1-收取 ,2 -支付) gea12f float null, --收费总额 geb10f float null, --金额 gea81c varchar(20) null, --部门1 gea82c varchar(20) null, --部门2 gea83c varchar(20) null, --部门3 gea84c varchar(20) null, --部门4 gea85c varchar(20) null, --部门5 gea86c varchar(20) null, --部门6 gea87c varchar(20) null, --部门7 gea88c varchar(20) null, --部门8 gea89c varchar(20) null, --部门9 gea90c varchar(20) null, --部门10 bill_total_amt float null -- 总业绩 ) insert #gem01_tmp(gea00c, gea01c, gea07c, gea21c, gea27f, gea28c, gea29f, gea30c, gea31f, gea32c, gea33f, gea34c, gea35f, gea36c, gea37f, gea38c, gea39f, gea40c, gea41f, gea42c, gea43f, gea44c, gea45f, gea12f,gea81c,gea82c,gea83c,gea84c,gea85c,gea86c,gea87c,gea88c,gea89c,gea90c,bill_total_amt) select gea00c, gea01c, gea07c, gea21c, gea27f, gea28c, gea29f, gea30c, gea31f, gea32c, gea33f, gea34c, gea35f, gea36c, gea37f, gea38c, gea39f, gea40c, gea41f, gea42c, gea43f, gea44c, gea45f, gea12f,gea81c,gea82c,gea83c,gea84c,gea85c,gea86c,gea87c,gea88c,gea89c,gea90c, isnull(gea27f,0)+isnull(gea29f,0)+isnull(gea31f,0)+isnull(gea33f,0)+isnull(gea35f,0)+isnull(gea37f,0)+isnull(gea39f,0)+isnull(gea41f,0)+isnull(gea43f,0)+isnull(gea45f,0) from gem01 a with(nolock) where a.gea00c = @compid and gea80d between @fromdate and @todate --第一销售 insert #gsm03_tmp_gem01(gsc00c, gsc01c, gsc02c, gsc04c, gsc05f, gsc10d, gsc11c, gsc12i, empno) select gea00c, gea01c, 'ge', gsc04c, case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gea27f,0) * b.gsc05f / bill_total_amt end, gsc10d, gsc11c, gsc12i, gea81c from #gem01_tmp a with (nolock), #gsm03_tmp_01 b where a.gea00c = b.gsc00c and a.gea01c = b.gsc01c and b.gsc02c = 'ge' and ((gea81c between @fromdepart and @todepart) or @fromdepart = '*') and isnull(gea21c,'') <> '' --第二销售 insert #gsm03_tmp_gem01(gsc00c, gsc01c, gsc02c, gsc04c, gsc05f, gsc10d, gsc11c, gsc12i, empno) select gea00c, gea01c, 'ge', gsc04c, case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gea29f,0) * b.gsc05f / bill_total_amt end, gsc10d, gsc11c, gsc12i, gea82c from #gem01_tmp a with (nolock), #gsm03_tmp_01 b where a.gea00c = b.gsc00c and a.gea01c = b.gsc01c and b.gsc02c = 'ge' and ((gea82c between @fromdepart and @todepart) or @fromdepart = '*') and isnull(gea28c,'') <> '' --第三销售 insert #gsm03_tmp_gem01(gsc00c, gsc01c, gsc02c, gsc04c, gsc05f, gsc10d, gsc11c, gsc12i, empno) select gea00c, gea01c, 'ge', gsc04c, case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gea31f,0) * b.gsc05f / bill_total_amt end, gsc10d, gsc11c, gsc12i, gea83c from #gem01_tmp a with (nolock), #gsm03_tmp_01 b where a.gea00c = b.gsc00c and a.gea01c = b.gsc01c and b.gsc02c = 'ge' and ((gea83c between @fromdepart and @todepart) or @fromdepart = '*') and isnull(gea30c,'') <> '' --第四销售 insert #gsm03_tmp_gem01(gsc00c, gsc01c, gsc02c, gsc04c, gsc05f, gsc10d, gsc11c, gsc12i, empno) select gea00c, gea01c, 'ge', gsc04c, case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gea33f,0) * b.gsc05f / bill_total_amt end, gsc10d, gsc11c, gsc12i, gea84c from #gem01_tmp a with (nolock), #gsm03_tmp_01 b where a.gea00c = b.gsc00c and a.gea01c = b.gsc01c and b.gsc02c = 'ge' and ((gea84c between @fromdepart and @todepart) or @fromdepart = '*') and isnull(gea32c,'') <> '' --第五销售 insert #gsm03_tmp_gem01(gsc00c, gsc01c, gsc02c, gsc04c, gsc05f, gsc10d, gsc11c, gsc12i, empno) select gea00c, gea01c, 'ge', gsc04c, case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gea35f,0) * b.gsc05f / bill_total_amt end,gsc10d, gsc11c, gsc12i, gea85c from #gem01_tmp a with (nolock), #gsm03_tmp_01 b where a.gea00c = b.gsc00c and a.gea01c = b.gsc01c and b.gsc02c = 'ge' and ((gea85c between @fromdepart and @todepart) or @fromdepart = '*') and isnull(gea34c,'') <> '' --第六销售 insert #gsm03_tmp_gem01(gsc00c, gsc01c, gsc02c, gsc04c, gsc05f, gsc10d, gsc11c, gsc12i, empno) select gea00c, gea01c, 'ge', gsc04c, case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gea37f,0) * b.gsc05f / bill_total_amt end, gsc10d, gsc11c, gsc12i, gea86c from #gem01_tmp a with (nolock), #gsm03_tmp_01 b where a.gea00c = b.gsc00c and a.gea01c = b.gsc01c and b.gsc02c = 'ge' and ((gea86c between @fromdepart and @todepart) or @fromdepart = '*') and isnull(gea36c,'') <> '' --第七销售 insert #gsm03_tmp_gem01(gsc00c, gsc01c, gsc02c, gsc04c, gsc05f, gsc10d, gsc11c, gsc12i, empno) select gea00c, gea01c, 'ge', gsc04c, case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gea39f,0) * b.gsc05f / bill_total_amt end, gsc10d, gsc11c, gsc12i, gea87c from #gem01_tmp a with (nolock), #gsm03_tmp_01 b, ham01 c with(nolock) where a.gea00c = b.gsc00c and a.gea01c = b.gsc01c and b.gsc02c = 'ge' and ((gea87c between @fromdepart and @todepart) or @fromdepart = '*') and isnull(gea38c,'') <> '' --第八销售 insert #gsm03_tmp_gem01(gsc00c, gsc01c, gsc02c, gsc04c, gsc05f, gsc10d, gsc11c, gsc12i, empno) select gea00c, gea01c, 'ge', gsc04c, case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gea41f,0) * b.gsc05f / bill_total_amt end, gsc10d, gsc11c, gsc12i,gea88c from #gem01_tmp a with (nolock), #gsm03_tmp_01 b where a.gea00c = b.gsc00c and a.gea01c = b.gsc01c and b.gsc02c = 'ge' and ((gea88c between @fromdepart and @todepart) or @fromdepart = '*') and isnull(gea40c,'') <> '' --第九销售 insert #gsm03_tmp_gem01(gsc00c, gsc01c, gsc02c, gsc04c, gsc05f, gsc10d, gsc11c, gsc12i, empno) select gea00c, gea01c, 'ge', gsc04c, case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gea43f,0) * b.gsc05f / bill_total_amt end, gsc10d, gsc11c, gsc12i,gea89c from #gem01_tmp a with (nolock), #gsm03_tmp_01 b where a.gea00c = b.gsc00c and a.gea01c = b.gsc01c and b.gsc02c = 'ge' and ((gea89c between @fromdepart and @todepart) or @fromdepart = '*') and isnull(gea42c,'') <> '' --第十销售 insert #gsm03_tmp_gem01(gsc00c, gsc01c, gsc02c, gsc04c, gsc05f, gsc10d, gsc11c, gsc12i, empno) select gea00c, gea01c, 'ge', gsc04c, case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gea45f,0) * b.gsc05f / bill_total_amt end, gsc10d, gsc11c, gsc12i,gea90c from #gem01_tmp a with (nolock), #gsm03_tmp_01 b where a.gea00c = b.gsc00c and a.gea01c = b.gsc01c and b.gsc02c = 'ge' and ((gea90c between @fromdepart and @todepart) or @fromdepart = '*') and isnull(gea44c,'') <> '' insert #gsm03_tmp(gsc00c,gsc01c, gsc02c, gsc04c, gsc05f, gsc10d, gsc11c, gsc12i, empno) select gsc00c, gsc01c, gsc02c, gsc04c, sum(gsc05f), gsc10d, gsc11c, gsc12i, empno from #gsm03_tmp_gem01 group by gsc00c, gsc01c, gsc02c, gsc04c, gsc10d, gsc11c, gsc12i, empno --End --删除临时表 drop table #gsm03_tmp_01 drop table #gsm03_tmp_gem01 --#gsm03_tmp --统计的中间结果 create table #tbl_stat_fill ( idd int identity not null, compid varchar(20) not null, empno varchar(20) not null, paymode varchar(10) not null, -- 支付方式 mode int null, -- 卡销售9, 0-存款 1-取款 5-欠款 6还款 amt float null default 0, -- 合计的金额 primary key(idd) ) --create nonclustered index idx_tbl_stat_fill on #tbl_stat_fill(compid, empno, paymode) create table #tbl_profit ( idd int identity not null, code varchar(20) not null, compid varchar(20) not null, empno varchar(20) not null, itemname varchar(60) not null, is_add int not null, amt float null default 0, is_leaf int not null, payment varchar(10) null, -- 支付方式 kind int null, -- 0 充值 1卡销售 9产品销售 2 服务 -1 取款 8 保证金收入 6 还款 primary key(idd) ) --create nonclustered index idx_tbl_profit on #tbl_profit(compid, empno, payment) insert #tbl_stat_fill(paymode, mode, amt,compid, empno) select gsc04c, 9, isnull(sum(isnull(a.gsc05f, 0)), 0),gsc00c, empno from #gsm03_tmp a where a.gsc02c = 'gn' group by gsc04c, gsc00c, empno declare @comp varchar(20) declare @empno varchar(20) declare tbl_profit_gn cursor for select compid, empno from #emp_info open tbl_profit_gn fetch tbl_profit_gn into @comp, @empno while @@fetch_status = 0 begin declare @customed_proj_count float select @customed_proj_count = customed_num from #customed_proj_count where compid = @comp and empno = @empno insert #tbl_profit(compid, empno, code, itemname, is_add, is_leaf, amt) values(@comp, @empno, 'HB', '总客数', 0, 1, @customed_proj_count) insert #tbl_profit(compid, empno, code, itemname, is_add, is_leaf, amt) values(@comp, @empno, '11', '收入', 1, 0, 0) insert #tbl_profit(compid, empno, code, itemname, is_add, is_leaf, amt, payment, kind) values(@comp, @empno, '1101', '卡销售收入', @add_card_sale, 0, 0, '', 1) insert #tbl_profit(compid, empno, code, itemname, is_add, is_leaf, payment, kind) values(@comp, @empno, '110101', '卡销售收入(扣欠款)-现金', @add_card_sale, 1, '1', 1) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '110101' and b.paymode = '1' and a.compid = @comp and a.empno = @empno insert #tbl_profit(compid, empno, code, itemname, is_add, is_leaf, payment, kind) values(@comp, @empno, '110102', '卡销售收入(扣欠款)-银行卡', @add_card_sale, 1, '6', 1) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '110102' and b.paymode = '6' and a.compid = @comp and a.empno = @empno insert #tbl_profit(compid,empno, code, itemname, is_add, is_leaf, payment, kind) values(@comp, @empno, '1101Z1', '卡销售收入(扣欠款)-支付宝', @add_card_sale, 1, 'ZFB', 1) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '1101Z1' and b.paymode = 'ZFB' and a.compid = @comp and a.empno = @empno insert #tbl_profit(compid,empno, code, itemname, is_add, is_leaf, payment, kind) values(@comp, @empno, '1101W1', '卡销售收入(扣欠款)-微信', @add_card_sale, 1, 'WX', 1) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '1101W1' and b.paymode = 'WX' and a.compid = @comp and a.empno = @empno insert #tbl_profit(compid,empno, code, itemname, is_add, is_leaf) values(@comp, @empno, '110103', '卡销售收入-买卡欠款', 0, 1) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '110103' and b.paymode = '5' and a.compid = @comp and a.empno = @empno fetch tbl_profit_gn into @comp, @empno end close tbl_profit_gn deallocate tbl_profit_gn -- 如下为账户异动的处理 insert #tbl_stat_fill(paymode, mode, amt,compid, empno) select gsc04c, gsc12i, isnull(sum(isnull(a.gsc05f, 0)), 0),gsc00c, empno from #gsm03_tmp a where a.gsc02c = 'gz' group by gsc04c, gsc12i,gsc00c, empno declare tbl_profit_gz cursor for select compid, empno from #emp_info open tbl_profit_gz fetch tbl_profit_gz into @comp, @empno while @@fetch_status = 0 begin -- 卡充值 insert #tbl_profit(compid, empno, code, itemname, is_add, is_leaf, payment, kind) values(@comp, @empno, '110104', '卡销售收入-充值-现金(扣欠款)', @add_card_sale, 1, '1', 0) -- 卡充值-现金 update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '110104' and b.paymode = '1' and b.mode = 0 and a.compid = @comp and a.empno = @empno insert #tbl_profit(compid, empno, code, itemname, is_add, is_leaf, payment, kind) values(@comp, @empno, '110105', '卡销售收入-充值-银行卡', @add_card_sale, 1, '6', 0) -- 银行卡支付 update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '110105' and b.paymode = '6' and b.mode = 0 and a.compid = @comp and a.empno = @empno -- 考虑银行卡取款 update a set a.amt = isnull(a.amt, 0) + isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '110105' and b.paymode = '6' and b.mode = 1 and a.compid = @comp and a.empno = @empno insert #tbl_profit(compid, empno, code, itemname, is_add, is_leaf, payment, kind) values(@comp, @empno, '1101Z2', '卡销售收入-充值-支付宝', @add_card_sale, 1, 'ZFB', 0) -- 支付宝支付 update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '1101Z2' and b.paymode = 'ZFB' and b.mode = 0 and a.compid = @comp and a.empno = @empno -- 考虑支付宝取款 update a set a.amt = isnull(a.amt, 0) + isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '1101Z2' and b.paymode = 'ZFB' and b.mode = 1 and a.compid = @comp and a.empno = @empno insert #tbl_profit(compid, empno, code, itemname, is_add, is_leaf, payment, kind) values(@comp, @empno, '1101W2', '卡销售收入-充值-微信', @add_card_sale, 1, 'WX', 0) -- 支付宝支付 update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '1101W2' and b.paymode = 'WX' and b.mode = 0 and a.compid = @comp and a.empno = @empno -- 考虑支付宝取款 update a set a.amt = isnull(a.amt, 0) + isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '1101W2' and b.paymode = 'WX' and b.mode = 1 and a.compid = @comp and a.empno = @empno insert #tbl_profit(compid, empno, code, itemname, is_add, is_leaf) values(@comp, @empno, '110106', '卡销售收入-充值欠款', 0, 1) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '110106' and b.paymode = '5' and b.mode = 0 and a.compid = @comp and a.empno = @empno -- 卡还款 insert #tbl_profit(compid, empno, code, itemname, is_add, is_leaf, payment, kind) values(@comp, @empno, '110107', '卡销售收入-还款-现金', @add_card_sale, 1, '1', 6) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '110107' and b.paymode = '1' and b.mode = 6 and a.compid = @comp and a.empno = @empno insert #tbl_profit(compid, empno, code, itemname, is_add, is_leaf, payment, kind) values(@comp, @empno, '110108', '卡销售收入-还款-银行卡', @add_card_sale, 1, '6', 6) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '110108' and b.paymode = '6' and b.mode = 6 and a.compid = @comp and a.empno = @empno insert #tbl_profit(compid, empno, code, itemname, is_add, is_leaf, payment, kind) values(@comp, @empno, '1101Z3', '卡销售收入-还款-支付宝', @add_card_sale, 1, 'ZFB', 6) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '1101Z3' and b.paymode = 'ZFB' and b.mode = 6 and a.compid = @comp and a.empno = @empno insert #tbl_profit(compid, empno, code, itemname, is_add, is_leaf, payment, kind) values(@comp, @empno, '1101W3', '卡销售收入-还款-微信', @add_card_sale, 1, 'WX', 6) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '1101W3' and b.paymode = 'WX' and b.mode = 6 and a.compid = @comp and a.empno = @empno -- 保证金转销售 declare @draw_preamt float select @draw_preamt = sum(amt) from #tbl_stat_fill where paymode = '&' insert #tbl_profit(compid, empno, code, itemname, is_add, is_leaf, amt, payment, kind) values(@comp, @empno, '110109', '卡销售收入-保证金转销售', 0, 1, isnull(@draw_preamt, 0), '&', 0) -- 经理签单 select @itemtype = '8' select @itemname = gsb03c from gsm02 with (nolock) where gsb01c = '3' and gsb02c = @itemtype and gsb00c = @comp insert #tbl_profit(compid, empno, code, itemname, is_add, is_leaf, payment, kind) values(@comp, @empno, '110110', '卡销售收入(扣欠款)-' + isnull(@itemname, ''), 0, 1, @itemtype, 1) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '110110' and b.paymode = @itemtype and b.mode = 9 and a.compid = @comp and a.empno = @empno insert #tbl_profit(compid,empno,code, itemname, is_add, is_leaf, payment, kind) values(@comp, @empno, '110111', '卡销售收入-充值-' + isnull(@itemname, ''), 0, 1, @itemtype, 0) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '110111' and b.paymode = @itemtype and b.mode = 0 and a.compid = @comp and a.empno = @empno insert #tbl_profit(compid, empno, code, itemname, is_add, is_leaf, payment, kind) values(@comp, @empno, '110112', '卡销售收入-还款-' + isnull(@itemname, ''), 0, 1, @itemtype, 6) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '110112' and b.paymode = @itemtype and b.mode = 6 and a.compid = @comp and a.empno = @empno -- 储值账户二, 按理应该不会有, 但yongqi作为转账了, 记得为0的删除就可以 select @itemtype = '9' select @itemname = gsb03c from gsm02 with (nolock) where gsb01c = '3' and gsb02c = @itemtype and gsb00c = @comp insert #tbl_profit(compid, empno, code, itemname, is_add, is_leaf, payment, kind) values(@comp, @empno, '110115', '卡销售收入(扣欠款)-' + isnull(@itemname, ''), 0, 1, @itemtype, 1) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '110115' and b.paymode = @itemtype and b.mode = 9 and a.compid = @comp and a.empno = @empno -- 转账方式下的充值,也应该扣除欠款部分 insert #tbl_profit(compid, empno, code, itemname, is_add, is_leaf, payment, kind) values(@comp, @empno,'110116', '卡销售收入-充值-' + isnull(@itemname, '') + '(扣欠款)', 0, 1, @itemtype, 0) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '110116' and b.paymode = @itemtype and b.mode = 0 and a.compid = @comp and a.empno = @empno insert #tbl_profit(compid, empno, code, itemname, is_add, is_leaf, payment, kind) values(@comp, @empno, '110117', '卡销售收入-还款-' + isnull(@itemname, ''), 0, 1, @itemtype, 6) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '110117' and b.paymode = @itemtype and b.mode = 6 and a.compid = @comp and a.empno = @empno -- 会员卡异动收费, 就算是现金 insert #tbl_profit(compid, empno, code, itemname, is_add, is_leaf, payment, kind) values(@comp, @empno, '110118', '卡销售收入-异动收费', @add_card_sale, 1, '1', 0) update #tbl_profit set amt = isnull(@amt, 0) - isnull(@amt2, 0) where code = '110118' and compid = @comp and empno = @empno -- 现金取款, 取款kind定义为-1 insert #tbl_profit(compid, empno, code, itemname, is_add, is_leaf, payment, kind) values(@comp, @empno, '110119', '卡销售收入-取款-现金', @add_card_sale, 1, '1', -1) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '110119' and b.paymode = '1' and b.mode = 1 and a.compid = @comp and a.empno = @empno -- 内调, 记得为0的删除就可以 select @itemtype = '0' select @itemname = gsb03c from gsm02 with (nolock) where gsb01c = '3' and gsb02c = @itemtype and gsb00c = @comp insert #tbl_profit(compid, empno, code, itemname, is_add, is_leaf, payment, kind) values(@comp, @empno, '110120', '卡销售收入(扣欠款)-' + isnull(@itemname, ''), @isadd_0 * @add_card_sale, 1, @itemtype, 1) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '110120' and b.paymode = @itemtype and b.mode = 9 and a.compid = @comp and a.empno = @empno insert #tbl_profit(compid, empno, code, itemname, is_add, is_leaf, payment, kind) values(@comp, @empno, '110121', '卡销售收入-充值-' + isnull(@itemname, ''), @isadd_0 * @add_card_sale, 1, @itemtype, 0) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '110121' and b.paymode = @itemtype and b.mode = 0 and a.compid = @comp and a.empno = @empno insert #tbl_profit(compid, empno, code, itemname, is_add, is_leaf, payment, kind) values(@comp, @empno, '110122', '卡销售收入-还款-' + isnull(@itemname, ''), @isadd_0 * @add_card_sale, 1, @itemtype, 6) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '110122' and b.paymode = @itemtype and b.mode = 6 and a.compid = @comp and a.empno = @empno insert #tbl_profit(compid, empno, code, itemname, is_add, is_leaf, payment, kind) values(@comp, @empno, '110123', '卡销售收入-取款-' + isnull(@itemname, ''), @isadd_0 * @add_card_sale, 1, @itemtype, -1) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '110123' and b.paymode = @itemtype and b.mode = 1 and a.compid = @comp and a.empno = @empno -- 抵用券, 记得为0的删除就可以 select @itemtype = '3' select @itemname = gsb03c from gsm02 with (nolock) where gsb01c = '3' and gsb02c = @itemtype and gsb00c = @comp insert #tbl_profit(compid, empno, code, itemname, is_add, is_leaf, payment, kind) values(@comp, @empno, '110125', '卡销售收入(扣欠款)-' + isnull(@itemname, ''), @isadd_3 * @add_card_sale, 1, @itemtype, 1) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '110125' and b.paymode = @itemtype and b.mode = 9 and a.compid = @comp and a.empno = @empno insert #tbl_profit(compid, empno, code, itemname, is_add, is_leaf, payment, kind) values(@comp, @empno, '110126', '卡销售收入-充值-' + isnull(@itemname, ''), @isadd_3 * @add_card_sale, 1, @itemtype, 0) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '110126' and b.paymode = @itemtype and b.mode = 0 and a.compid = @comp and a.empno = @empno insert #tbl_profit(compid, empno, code, itemname, is_add, is_leaf, payment, kind) values(@comp, @empno, '110127', '卡销售收入-还款-' + isnull(@itemname, ''), @isadd_3 * @add_card_sale, 1, @itemtype, 6) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '110127' and b.paymode = @itemtype and b.mode = 6 and a.compid = @comp and a.empno = @empno -- 抵用券B, 记得为0的删除就可以 select @itemtype = '$' select @itemname = gsb03c from gsm02 with (nolock) where gsb01c = '3' and gsb02c = @itemtype and gsb00c = @comp insert #tbl_profit(compid, empno, code, itemname, is_add, is_leaf, payment, kind) values(@comp, @empno, '110130', '卡销售收入(扣欠款)-' + isnull(@itemname, ''), @isadd_$ * @add_card_sale, 1, @itemtype, 1) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '110130' and b.paymode = @itemtype and b.mode = 9 and a.compid = @comp and a.empno = @empno insert #tbl_profit(compid, empno, code, itemname, is_add, is_leaf, payment, kind) values(@comp, @empno, '110131', '卡销售收入-充值-' + isnull(@itemname, ''), @isadd_$ * @add_card_sale, 1, @itemtype, 0) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '110131' and b.paymode = @itemtype and b.mode = 0 and a.compid = @comp and a.empno = @empno insert #tbl_profit(compid, empno, code, itemname, is_add, is_leaf, payment, kind) values(@comp, @empno, '110132', '卡销售收入-还款-' + isnull(@itemname, ''), @isadd_$ * @add_card_sale, 1, @itemtype, 6) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '110132' and b.paymode = @itemtype and b.mode = 6 and a.compid = @comp and a.empno = @empno -- 自己增加的$开头的支付方式 declare @pay_$ varchar(10) declare @code_char varchar(10) declare @isadd_ur float -- 子定义的支付方式是为计入营业收入 declare @code_int int select @code_int = 110150 declare cur_each_pay cursor for select gsb02c from gsm02 with (nolock) where gsb00c = @comp and gsb01c = '3' and gsb02c like '$%' and len(gsb02c) > 1 order by gsb02c open cur_each_pay fetch cur_each_pay into @pay_$ while @@fetch_status = 0 begin select @code_char = convert(varchar(10), @code_int) select @itemtype = @pay_$ select @itemname = gsb03c from gsm02 with (nolock) where gsb01c = '3' and gsb02c = @itemtype and gsb00c = @comp select @isadd_ur = gso07f from gsm15 with (nolock) where gso00c = @comp and gso02c = @pay_$ select @isadd_ur = isnull(@isadd_ur, 1) -- 默认算收入 insert #tbl_profit(compid, empno, code, itemname, is_add, is_leaf, payment, kind) values(@comp, @empno, @code_char, '卡销售收入(扣欠款)-' + isnull(@itemname, ''), @isadd_ur * @add_card_sale, 1, @pay_$, 1) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = @code_char and b.paymode = @itemtype and b.mode = 9 and a.compid = @comp and a.empno = @empno select @code_int = @code_int + 1 select @code_char = convert(varchar(10), @code_int) insert #tbl_profit(compid, empno, code, itemname, is_add, is_leaf, payment, kind) values(@comp, @empno, @code_char, '卡销售收入-充值-' + isnull(@itemname, ''), @isadd_ur * @add_card_sale, 1, @pay_$, 0) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = @code_char and b.paymode = @itemtype and b.mode = 0 and a.compid = @comp and a.empno = @empno select @code_int = @code_int + 1 select @code_char = convert(varchar(10), @code_int) insert #tbl_profit(compid, empno, code, itemname, is_add, is_leaf, payment, kind) values(@comp, @empno, @code_char, '卡销售收入-还款-' + isnull(@itemname, ''), @isadd_ur * @add_card_sale, 1, @pay_$, 6) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = @code_char and b.paymode = @itemtype and b.mode = 6 and a.compid = @comp and a.empno = @empno select @code_int = @code_int + 1 select @code_char = convert(varchar(10), @code_int) fetch cur_each_pay into @pay_$ end close cur_each_pay deallocate cur_each_pay -- 积分买卡,只有在充值的时候可能存在,也无法作为卡销售的收入 select @itemtype = '7' select @itemname = gsb03c from gsm02 with (nolock) where gsb01c = '3' and gsb02c = @itemtype and gsb00c = @comp insert #tbl_profit(compid,empno,code, itemname, is_add, is_leaf, payment, kind) values(@comp,@empno,'110133', '卡销售收入-充值-' + isnull(@itemname, ''), 0, 1, @itemtype, 0) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '110133' and b.paymode = @itemtype and b.mode = 0 and a.compid = @comp and a.empno = @empno --end fetch tbl_profit_gz into @comp, @empno end close tbl_profit_gz deallocate tbl_profit_gz --end -- 卡的变更等收入,也属于卡的销售收入 truncate table #tbl_stat_fill ---------start-----2016.06.24 insert #tbl_stat_fill(paymode, amt,compid, empno) select gsc04c, isnull(sum(isnull(a.gsc05f, 0)), 0),gsc00c, empno from #gsm03_tmp a where a.gsc02c = 'ge' group by gsc04c,gsc00c, empno create table #gsm02 ( pay varchar(10) not null, payname varchar(40) null ) declare tbl_profit_ge cursor for select compid, empno from #emp_info open tbl_profit_ge fetch tbl_profit_ge into @comp, @empno while @@fetch_status = 0 begin insert #tbl_profit(compid,empno,code, itemname, is_add, is_leaf, payment, kind) values(@comp,@empno,'110140', '卡销售收入-变更-现金', @add_card_sale, 1, '1', 0) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '110140' and b.paymode = '1' and a.compid = @comp and a.empno = @empno insert #tbl_profit(compid,empno, code, itemname, is_add, is_leaf, payment, kind) values(@comp,@empno,'110141', '卡销售收入-变更-银行卡', @add_card_sale, 1, '6', 0) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '110141' and b.paymode = '6' and a.compid = @comp and a.empno = @empno insert #tbl_profit(compid,empno, code, itemname, is_add, is_leaf, payment, kind) values(@comp,@empno,'1101Z4', '卡销售收入-变更-支付宝', @add_card_sale, 1, 'ZFB', 0) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '1101Z4' and b.paymode = 'ZFB' and a.compid = @comp and a.empno = @empno insert #tbl_profit(compid,empno,code, itemname, is_add, is_leaf, payment, kind) values(@comp,@empno,'110145', '卡销售收入-变更-欠款', @add_card_sale, 0, '5', 0) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '110145' and b.paymode = '5' and a.compid = @comp and a.empno = @empno insert #tbl_profit(compid,empno, code, itemname, is_add, is_leaf, payment, kind) values(@comp,@empno,'1101W4', '卡销售收入-变更-微信', @add_card_sale, 1, 'WX', 0) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '1101W4' and b.paymode = 'WX' and a.compid = @comp and a.empno = @empno --Start 卡种变更所有支付方式 add by lm 2015.12.28 set @id = 0 set @code_no = '' delete from #gsm02 insert #gsm02 (pay, payname) select gsb02c, gsb03c from gsm02 a with(nolock) where a.gsb00c = @comp and a.gsb01c = '3' and a.gsb02c not in ('ZFB', 'WX', '1', '6', '5') while (@id = 0) begin if exists(select 1 from #gsm02) begin select top 1 @itemtype = pay, @itemname = payname from #gsm02 select @code_no = '110140' + @itemtype insert #tbl_profit(compid,empno,code, itemname, is_add, is_leaf, payment, kind) values(@comp,@empno,@code_no, '卡销售收入-变更-' + isnull(@itemname, ''), @add_card_sale, 1, @itemtype, 0) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = @code_no and b.paymode = @itemtype and a.compid = @comp and a.empno = @empno set @code_no = '' set @id = 0 delete from #gsm02 where pay = @itemtype end else begin set @id = 1 end end -- 统计卡销售收入 select @amt = 0 if @calmode = 0 begin select @amt = sum(isnull(amt * is_add, 0)) from #tbl_profit where code like '1101%' and is_leaf = 1 and compid = @comp and empno = @empno end else begin select @amt = sum(isnull(amt, 0)) from #tbl_profit where code like '1101%' and is_leaf = 1 and (isnull(payment, '') <> '&' and isnull(payment, '') <> '7' and isnull(payment, '') <> '') and compid = @comp and empno = @empno end update #tbl_profit set amt = isnull(@amt, 0) where code = '1101' and compid = @comp and empno = @empno fetch tbl_profit_ge into @comp, @empno end close tbl_profit_ge deallocate tbl_profit_ge truncate table #tbl_stat_fill -- 服务收入, 包括产品部分 create table #tbl_stat_fill_2 ( idd int identity not null, compid varchar(20) not null, empno varchar(20) not null, paymode varchar(10) null, mode int null, amt float null, primary key(idd) ) create nonclustered index tbl_stat_fill_2_idx_01 on #tbl_stat_fill_2(compid, empno) insert #tbl_stat_fill_2(paymode, mode, amt,compid, empno) select gsc04c, 0, isnull(sum(isnull(a.gsc05f, 0)), 0),gsc00c, empno from #gsm03_tmp a where a.gsc02c = 'gx' group by gsc04c,gsc00c, empno insert #tbl_stat_fill_2(paymode, mode, amt, compid, empno) select gsc04c, 1, isnull(sum(isnull(a.gsc05f, 0)), 0),gsc00c, empno from #gsm03_tmp a where a.gsc02c = 'gl' group by gsc04c,gsc00c, empno insert #tbl_stat_fill(paymode, mode, amt, compid, empno) select isnull(paymode, ''), 0, sum(isnull(amt, 0)), compid, empno from #tbl_stat_fill_2 group by isnull(paymode, ''), compid, empno drop table #tbl_stat_fill_2 -- 必须要的数据 -- 为了提高性能,把需要用倒的ggm01数据转移到临时表中 (1) create table #ggm01_1 ( gga00c varchar(20) not null, gga01c varchar(20) not null, gga93c varchar(20) null, gga07c varchar(10) null, --会员卡类别 ggb34f float null, --实际营业额折算率 ggc30f float null --实际营业额折算比率 primary key(gga00c, gga01c) ) insert #ggm01_1(gga00c, gga01c,gga93c, gga07c) select gga00c, gga01c, gga93c,gga07c from ggm01 a with (nolock) where gga00c = @incompid and gga80d >= @fromdate and gga80d <= @todate -- 把明细的服务项目的卡付计算出来 create table #tbl_card_pay ( paycode varchar(20) not null, compid varchar(20) not null, empno varchar(20) not null, acctid int null, amt float null, code varchar(20) null, pay_name varchar(30) null, --primary key(paycode) ) declare tbl_profit_gx_gl cursor for select compid, empno from #emp_info open tbl_profit_gx_gl fetch tbl_profit_gx_gl into @comp, @empno while @@fetch_status = 0 begin insert #tbl_profit(compid, empno,code, itemname, is_add, is_leaf) values(@comp,@empno,'1102', '服务收入(包括产品销售)', 1, 0) insert #tbl_profit(compid, empno,code, itemname, is_add, is_leaf, payment, kind) values(@comp,@empno,'110201', '服务收入-现金', 1, 1, '1', 2) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '110201' and b.paymode = '1' and a.compid = @comp and a.empno= @empno insert #tbl_profit(compid, empno,code, itemname, is_add, is_leaf, payment, kind) values(@comp,@empno,'110202', '服务收入-银行卡', 1, 1, '6', 2) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '110202' and b.paymode = '6' and a.compid = @comp and a.empno= @empno insert #tbl_profit(compid, empno,code, itemname, is_add, is_leaf, payment, kind) values(@comp,@empno,'1102Z1', '服务收入-支付宝', 1, 1, 'ZFB', 2) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '1102Z1' and b.paymode = 'ZFB' and a.compid = @comp and a.empno= @empno insert #tbl_profit(compid, empno,code, itemname, is_add, is_leaf, payment, kind) values(@comp,@empno,'1102W1', '服务收入-微信', 1, 1, 'WX', 2) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '1102W1' and b.paymode = 'WX' and a.compid = @comp and a.empno= @empno -- 经理签单 select @itemtype = '8' select @itemname = gsb03c from gsm02 with (nolock) where gsb01c = '3' and gsb02c = @itemtype and gsb00c = @comp insert #tbl_profit(compid, empno,code, itemname, is_add, is_leaf, payment, kind) values(@comp,@empno,'110203', '服务收入-' + isnull(@itemname, ''), 0, 1, @itemtype, 2) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '110203' and b.paymode = @itemtype and a.compid = @comp and a.empno= @empno -- 抵用券 select @itemtype = '3' select @itemname = gsb03c from gsm02 with (nolock) where gsb01c = '3' and gsb02c = @itemtype and gsb00c = @comp insert #tbl_profit(compid, empno,code, itemname, is_add, is_leaf, payment, kind) values(@comp,@empno,'110204', '服务收入-' + isnull(@itemname, ''), @isadd_3, 1, @itemtype, 2) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '110204' and b.paymode = @itemtype and a.compid = @comp and a.empno= @empno -- 第二抵用券 select @itemtype = '$' select @itemname = gsb03c from gsm02 with (nolock) where gsb01c = '3' and gsb02c = @itemtype and gsb00c = @comp insert #tbl_profit(compid, empno,code, itemname, is_add, is_leaf, payment, kind) values(@comp,@empno,'110208', '服务收入-' + isnull(@itemname, ''), @isadd_$, 1, @itemtype, 2) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '110208' and b.paymode = @itemtype and a.compid = @comp and a.empno= @empno select @code_int = 110250 declare cur_each_pay_2 cursor for select gsb02c from gsm02 with (nolock) where gsb00c = @comp and gsb01c = '3' and gsb02c like '$%' and len(gsb02c) > 1 order by gsb02c open cur_each_pay_2 fetch cur_each_pay_2 into @pay_$ while @@fetch_status = 0 begin select @code_char = convert(varchar(10), @code_int) select @itemtype = @pay_$ select @itemname = gsb03c from gsm02 with (nolock) where gsb01c = '3' and gsb02c = @itemtype and gsb00c = @comp select @isadd_ur = gso07f from gsm15 with (nolock) where gso00c = @incompid and gso02c = @pay_$ select @isadd_ur = isnull(@isadd_ur, 1) -- 默认算收入 insert #tbl_profit(compid, empno,code, itemname, is_add, is_leaf, payment, kind) values(@comp,@empno,@code_char, '服务收入-' + isnull(@itemname, ''), @isadd_ur, 1, @pay_$, 2) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = @code_char and b.paymode = @itemtype and a.compid = @comp and a.empno= @empno select @code_int = @code_int + 1 select @code_char = convert(varchar(10), @code_int) fetch cur_each_pay_2 into @pay_$ end close cur_each_pay_2 deallocate cur_each_pay_2 -- 内调 select @itemtype = '0' select @itemname = gsb03c from gsm02 with (nolock) where gsb01c = '3' and gsb02c = @itemtype and gsb00c = @comp insert #tbl_profit(compid, empno,code, itemname, is_add, is_leaf, payment, kind) values(@comp,@empno,'110209', '服务收入-' + isnull(@itemname, ''), @isadd_0, 1, @itemtype, 2) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '110209' and b.paymode = @itemtype and a.compid = @comp and a.empno= @empno insert #tbl_profit(compid, empno,code, itemname, is_add, is_leaf) values(@comp,@empno,'110207', '服务收入-欠款', 0, 1) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '110207' and b.paymode = '5' and a.compid = @comp and a.empno= @empno -- 服务收入-取用储值 insert #tbl_profit(compid, empno,code, itemname, is_add, is_leaf, payment, kind) values(@comp,@empno,'110205', '服务收入-取用储值', @add_card_svr, 0, 'CARD', 2) truncate table #tbl_card_pay insert #tbl_card_pay(compid,empno,paycode, amt) select compid,empno,paymode, sum(amt) from #tbl_stat_fill where (paymode = '#' or paymode = '4' or paymode = '9' or (paymode >= 'A' and paymode <= 'Z')) -- 不包括积分 and paymode not in ('WX','ZFB') group by compid,empno,paymode update a set a.acctid = b.gsn02c from #tbl_card_pay a, gsm14 b with (nolock) where a.paycode = b.gsn01c and a.compid = @comp and a.empno = @empno update a set a.code = right('00' + paycode, 2) from #tbl_card_pay a where a.compid = @comp and a.empno = @empno update a set a.pay_name = b.gsb03c from #tbl_card_pay a, gsm02 b with (nolock) where a.paycode = b.gsb02c and gsb00c = @comp and gsb01c = '3' --修复 卡收入 没有值 根据部门重复 显示 卡值 insert #tbl_profit(compid,empno,code, itemname, is_add, is_leaf, payment, kind, amt) select @comp,@empno,'110205' + isnull(code, '00'), '服务收入-取用储值-' + pay_name, @add_card_svr, 1, paycode, 2, amt from #tbl_card_pay where compid = @comp and empno = @empno -- 积分需要单独列出来 declare @act varchar(20) declare @act_name varchar(50) select @act = '7' -- 积分的支付 select @act_name = gsb03c from gsm02 with (nolock) where gsb00c = @comp and gsb01c = '3' and gsb02c = @act insert #tbl_profit(compid,empno,code, itemname, is_add, is_leaf, payment, kind) values(@comp,@empno,'11020507', '服务收入-积分支付-' + isnull(@act_name, ''), 0, 1, @act, 2) -- 积分永远不作为服务收入 is_add = 0 update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '11020507' and b.paymode = @act and a.compid = @comp and a.empno = @empno -- 合计所有取用储值, 把积分支付单独扣除 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit where (code like '110205%' and code <> '11020507') and is_leaf = 1 and compid = @comp and empno = @empno update #tbl_profit set amt = isnull(@amt, 0) where code = '110205' and compid = @comp and empno = @empno -- START -- 统计因为项目或产品销售的欠款的还款收入,只统计现金还款及银行卡的还款 -- 服务中欠款的可能行很小,所以默认为产品的还款 select @amt = 0 select @amt = isnull(sum(isnull(a.gsc05f, 0)), 0) from #gsm03_tmp a where a.gsc02c = 'gz' and isnull(a.isnotcard, 0) = 1 and gsc04c = '1' and gsc12i = 6 and gsc00c = @comp and empno = @empno insert #tbl_profit(compid,empno,code, itemname, is_add, is_leaf, amt, payment, kind) values(@comp,@empno,'110230', '服务收入-还款-现金', 1, 1, @amt, '1', 2) select @amt = 0 select @amt = isnull(sum(isnull(a.gsc05f, 0)), 0) from #gsm03_tmp a where a.gsc02c = 'gz' and isnull(a.isnotcard, 0) = 1 and gsc04c = '6' and gsc12i = 6 and gsc00c = @comp and empno = @empno insert #tbl_profit(compid,empno,code, itemname, is_add, is_leaf, amt, payment, kind) values(@comp,@empno,'110231', '服务收入-还款-银行卡', 1, 1, @amt, '6', 2) select @amt = 0 select @amt = isnull(sum(isnull(a.gsc05f, 0)), 0) from #gsm03_tmp a where a.gsc02c = 'gz' and isnull(a.isnotcard, 0) = 1 and gsc04c = 'ZFB' and gsc12i = 6 and gsc00c = @comp and empno = @empno insert #tbl_profit(compid,empno,code, itemname, is_add, is_leaf, amt, payment, kind) values(@comp,@empno,'1102Z3', '服务收入-还款-支付宝', 1, 1, @amt, 'ZFB', 2) select @amt = 0 select @amt = isnull(sum(isnull(a.gsc05f, 0)), 0) from #gsm03_tmp a where a.gsc02c = 'gz' and isnull(a.isnotcard, 0) = 1 and gsc04c = 'WX' and gsc12i = 6 and gsc00c = @comp and empno = @empno insert #tbl_profit(compid,empno,code, itemname, is_add, is_leaf, amt, payment, kind) values(@comp,@empno,'1102W3', '服务收入-还款-微信', 1, 1, @amt, 'WX', 2) --Start 统计还款所有支付方式 add by lm 2015.12.28 set @id = 0 set @code_no = '' create table #gsm02_1 ( pay varchar(10) not null, payname varchar(40) null ) insert #gsm02_1 (pay, payname) select gsb02c, gsb03c from gsm02 a with(nolock) where a.gsb00c = @comp and a.gsb01c = '3' and a.gsb02c not in ('1','6','ZFB','WX') while (@id = 0) begin if exists(select 1 from #gsm02_1) begin select top 1 @itemtype = pay, @itemname = payname from #gsm02_1 select @code_no = '110231' + @itemtype select @amt = 0 select @amt = isnull(sum(isnull(a.gsc05f, 0)), 0) from #gsm03_tmp a where a.gsc02c = 'gz' and isnull(a.isnotcard, 0) = 1 and gsc04c = @itemtype and gsc12i = 6 and gsc00c = @comp and empno = @empno select @isadd_ur = gso07f from gsm15 with(nolock) where gso00c = @comp and gso02c = @itemtype select @isadd_ur = isnull(@isadd_ur, 1) -- 默认算收入 insert #tbl_profit(compid,empno,code, itemname, is_add, is_leaf, amt, payment, kind) values(@comp,@empno,@code_no, '服务收入-还款(只计算还款)-' + @itemname, @isadd_ur, 1, @amt, @itemtype, 6) set @code_no = '' set @id = 0 delete from #gsm02_1 where pay = @itemtype end else begin set @id = 1 end end drop table #gsm02_1 -- end --start 在按项目支付的情况下,合计卡付的实际营业额 if (isnull(@sp055f, 0) = 0) begin insert #tbl_profit(compid, empno, code, itemname, is_add, is_leaf, payment, kind) values(@compid, @empno, '110206', '服务收入-卡付实际营业额', 0, 0, '', 2) if(@sp215f = 1) --add by lm 2015-09-16 begin select @amt = 0 select @amt = sum(isnull(isnull(ggb11f, 0) * isnull(gso03i, 1), 0)) --* isnull(a.ggb34f, 1) from ggm02 b with (nolock), #ggm01_1 a, gsm15 c with (nolock) where b.ggb00c = a.gga00c and b.ggb01c = a.gga01c and b.ggb00c = c.gso00c and b.ggb27c = c.gso02c and c.gso01c = '3' and c.gso09f = 1 and b.ggb81c = @empno and a.gga00c = @comp and ((ggb27c >= 'A' and ggb27c <= 'Z') or ggb27c = '#') and ggb27c not in ('WX','ZFB') select @gak22f = 0 select @gak22f = sum(isnull(isnull(ggb11f, 0) * isnull(gso03i, 1) * isnull(gak22f, 1), 0)) from ggm02 b with (nolock), #ggm01_1 a, gsm15 c with (nolock), gam10 d with(nolock) where b.ggb00c = a.gga00c and b.ggb01c = a.gga01c and b.ggb00c = c.gso00c and b.ggb27c = c.gso02c and d.gak00c = a.gga00c and d.gak01c = a.gga07c and b.ggb81c = @empno and a.gga00c = @comp and c.gso01c = '3' and c.gso09f = 1 and ggb27c = '4' --第一销售 select @amt2 = 0 select @amt2 = sum(isnull(isnull(ggc11f, 0) * isnull(gso06f, 1) * isnull(a.ggc30f, 1), 0)) from ggm03 b with (nolock), #ggm01_1 a, gsm15 c with (nolock) where b.ggc00c = a.gga00c and b.ggc01c = a.gga01c and b.ggc00c = c.gso00c and b.ggc27c = c.gso02c and b.ggc81c = @empno and a.gga00c = @comp and c.gso01c = '3' and c.gso10f = 1 and ((ggc27c >= 'A' and ggc27c <= 'Z') or ggc27c = '#') and ggc27c not in ('WX','ZFB') select @gak24f = 0 select @gak24f = sum(isnull(isnull(ggc11f, 0) * isnull(gso06f, 1) * isnull(gak24f, 1), 0)) from ggm03 b with (nolock), #ggm01_1 a, gsm15 c with (nolock),gam10 d with(nolock) where b.ggc00c = a.gga00c and b.ggc01c = a.gga01c and b.ggc00c = c.gso00c and b.ggc27c = c.gso02c and d.gak00c = a.gga00c and d.gak01c = a.gga07c and b.ggc81c = @empno and a.gga00c = @comp and c.gso01c = '3' and c.gso10f = 1 and ggc27c = '4' --第二销售 select @amt2 = isnull(@amt2,0 ) + sum(isnull(isnull(ggc11f, 0) * isnull(gso06f, 1) * isnull(a.ggc30f, 1), 0)) from ggm03 b with (nolock), #ggm01_1 a, gsm15 c with (nolock) where b.ggc00c = a.gga00c and b.ggc01c = a.gga01c and b.ggc00c = c.gso00c and b.ggc27c = c.gso02c and b.ggc82c = @empno and a.gga00c = @comp and c.gso01c = '3' and c.gso10f = 1 and ((ggc27c >= 'A' and ggc27c <= 'Z') or ggc27c = '#') and ggc27c not in ('WX','ZFB') select @gak24f = isnull(@gak24f,0) + sum(isnull(isnull(ggc11f, 0) * isnull(gso06f, 1) * isnull(gak24f, 1), 0)) from ggm03 b with (nolock), #ggm01_1 a, gsm15 c with (nolock),gam10 d with(nolock) where b.ggc00c = a.gga00c and b.ggc01c = a.gga01c and b.ggc00c = c.gso00c and b.ggc27c = c.gso02c and d.gak00c = a.gga00c and d.gak01c = a.gga07c and b.ggc82c = @empno and a.gga00c = @comp and c.gso01c = '3' and c.gso10f = 1 and ggc27c = '4' --第三销售 select @amt2 = isnull(@amt2,0) + sum(isnull(isnull(ggc11f, 0) * isnull(gso06f, 1) * isnull(a.ggc30f, 1), 0)) from ggm03 b with (nolock), #ggm01_1 a, gsm15 c with (nolock) where b.ggc00c = a.gga00c and b.ggc01c = a.gga01c and b.ggc00c = c.gso00c and b.ggc27c = c.gso02c and b.ggc83c = @empno and a.gga00c = @comp and c.gso01c = '3' and c.gso10f = 1 and ((ggc27c >= 'A' and ggc27c <= 'Z') or ggc27c = '#') and ggc27c not in ('WX','ZFB') select @gak24f = isnull(@gak24f,0) + sum(isnull(isnull(ggc11f, 0) * isnull(gso06f, 1) * isnull(gak24f, 1), 0)) from ggm03 b with (nolock), #ggm01_1 a, gsm15 c with (nolock),gam10 d with(nolock) where b.ggc00c = a.gga00c and b.ggc01c = a.gga01c and b.ggc00c = c.gso00c and b.ggc27c = c.gso02c and d.gak00c = a.gga00c and d.gak01c = a.gga07c and b.ggc83c = @empno and a.gga00c = @comp and c.gso01c = '3' and c.gso10f = 1 and ggc27c = '4' select @amt2 = isnull(@amt2,0) +isnull(@gak24f,0) select @amt =isnull(@amt,0) +isnull(@gak22f,0) end else begin create table #gcm11 --会员疗程充值 ( gcm00c varchar(10) not null, --门店编号 gcm01c varchar(20) not null --异动单号 ) create table #gcm11_card --会员疗程充值(卡付) ( gcm00c varchar(10) not null, --门店编号 gcm01c varchar(20) not null, --异动单号 cardamt float null ) insert #gcm11(gcm00c, gcm01c) select distinct gcm00c,gcm01c from gcm11 a with(nolock), #dept_bills b where a.gcm00c = b.compid and a.gcm01c = b.billno and (b.billtype = 'gz') and b.payway in (select pay from pay_classify where comp = @incompid and category = '2' ) insert #gcm11_card(gcm00c, gcm01c, cardamt) select gcm00c, gcm01c, isnull(gcm16f * ratio,0) from gcm11 a with(nolock), #dept_bills b where a.gcm00c = b.compid and a.gcm01c = b.billno and b.billtype = 'gz' and a.gcm15c = b.payway and a.gcm15c in (select pay from pay_classify where comp = @incompid and category = '2') and isnull(a.gcm15c,'') <> '' union all select gcm00c, gcm01c, isnull(gcm18f * ratio,0) from gcm11 a with(nolock), #dept_bills b where a.gcm00c = b.compid and a.gcm01c = b.billno and b.billtype = 'gz' and a.gcm17c = b.payway and a.gcm17c in (select pay from pay_classify where comp = @incompid and category = '2') and isnull(a.gcm17c,'') <> '' union all select gcm00c, gcm01c, isnull(gcm20f * ratio,0) from gcm11 a with(nolock), #dept_bills b where a.gcm00c = b.compid and a.gcm01c = b.billno and b.billtype = 'gz' and a.gcm19c = b.payway and a.gcm19c in (select pay from pay_classify where comp = @incompid and category = '2') and isnull(a.gcm19c,'') <> '' select @amt = 0 select @amt = sum(isnull(isnull(ggb11f, 0) * isnull(gso03i, 1), 0)) --* isnull(a.ggb34f, 1) from ggm02 b with (nolock), #ggm01_1 a, gsm15 c with (nolock) where b.ggb00c = a.gga00c and b.ggb01c = a.gga01c and b.ggb00c = c.gso00c and b.ggb27c = c.gso02c and c.gso01c = '3' and c.gso09f = 1 and b.ggb81c = @empno and a.gga00c = @comp and ((ggb27c >= 'A' and ggb27c <= 'Z') or ggb27c = '#') and ggb27c not in ('WX','ZFB') select @gak22f = 0 select @gak22f = sum(isnull(isnull(ggb11f, 0) * isnull(gso03i, 1) * isnull(gak22f, 1), 0)) from ggm02 b with (nolock), #ggm01_1 a, gsm15 c with (nolock), gam10 d with(nolock) where b.ggb00c = a.gga00c and b.ggb01c = a.gga01c and b.ggb00c = c.gso00c and b.ggb27c = c.gso02c and d.gak00c = a.gga00c and d.gak01c = a.gga07c and b.ggb81c = @empno and a.gga00c = @comp and c.gso01c = '3' and c.gso09f = 1 and ggb27c = '4' --第一销售 select @amt2 = 0 select @amt2 = sum(isnull(isnull(ggc11f, 0) * isnull(gso06f, 1) * isnull(a.ggc30f, 1), 0)) from ggm03 b with (nolock), #ggm01_1 a, gsm15 c with (nolock) where b.ggc00c = a.gga00c and b.ggc01c = a.gga01c and b.ggc00c = c.gso00c and b.ggc27c = c.gso02c and b.ggc81c = @empno and a.gga00c = @comp and c.gso01c = '3' and c.gso10f = 1 and ((ggc27c >= 'A' and ggc27c <= 'Z') or ggc27c = '#') and ggc27c not in ('WX','ZFB') select @gak24f = 0 select @gak24f = sum(isnull(isnull(ggc11f, 0) * isnull(gso06f, 1) * isnull(gak24f, 1), 0)) from ggm03 b with (nolock), #ggm01_1 a, gsm15 c with (nolock),gam10 d with(nolock) where b.ggc00c = a.gga00c and b.ggc01c = a.gga01c and b.ggc00c = c.gso00c and b.ggc27c = c.gso02c and d.gak00c = a.gga00c and d.gak01c = a.gga07c and b.ggc81c = @empno and a.gga00c = @comp and c.gso01c = '3' and c.gso10f = 1 and ggc27c = '4' --第二销售 select @amt2 = isnull(@amt2,0 ) + sum(isnull(isnull(ggc11f, 0) * isnull(gso06f, 1) * isnull(a.ggc30f, 1), 0)) from ggm03 b with (nolock), #ggm01_1 a, gsm15 c with (nolock) where b.ggc00c = a.gga00c and b.ggc01c = a.gga01c and b.ggc00c = c.gso00c and b.ggc27c = c.gso02c and b.ggc82c = @empno and a.gga00c = @comp and c.gso01c = '3' and c.gso10f = 1 and ((ggc27c >= 'A' and ggc27c <= 'Z') or ggc27c = '#') and ggc27c not in ('WX','ZFB') select @gak24f = isnull(@gak24f,0) + sum(isnull(isnull(ggc11f, 0) * isnull(gso06f, 1) * isnull(gak24f, 1), 0)) from ggm03 b with (nolock), #ggm01_1 a, gsm15 c with (nolock),gam10 d with(nolock) where b.ggc00c = a.gga00c and b.ggc01c = a.gga01c and b.ggc00c = c.gso00c and b.ggc27c = c.gso02c and d.gak00c = a.gga00c and d.gak01c = a.gga07c and b.ggc82c = @empno and a.gga00c = @comp and c.gso01c = '3' and c.gso10f = 1 and ggc27c = '4' --第三销售 select @amt2 = isnull(@amt2,0) + sum(isnull(isnull(ggc11f, 0) * isnull(gso06f, 1) * isnull(a.ggc30f, 1), 0)) from ggm03 b with (nolock), #ggm01_1 a, gsm15 c with (nolock) where b.ggc00c = a.gga00c and b.ggc01c = a.gga01c and b.ggc00c = c.gso00c and b.ggc27c = c.gso02c and b.ggc83c = @empno and a.gga00c = @comp and c.gso01c = '3' and c.gso10f = 1 and ((ggc27c >= 'A' and ggc27c <= 'Z') or ggc27c = '#') and ggc27c not in ('WX','ZFB') select @gak24f = isnull(@gak24f,0) + sum(isnull(isnull(ggc11f, 0) * isnull(gso06f, 1) * isnull(gak24f, 1), 0)) from ggm03 b with (nolock), #ggm01_1 a, gsm15 c with (nolock),gam10 d with(nolock) where b.ggc00c = a.gga00c and b.ggc01c = a.gga01c and b.ggc00c = c.gso00c and b.ggc27c = c.gso02c and d.gak00c = a.gga00c and d.gak01c = a.gga07c and b.ggc83c = @empno and a.gga00c = @comp and c.gso01c = '3' and c.gso10f = 1 and ggc27c = '4' select @amt2 = isnull(@amt2,0) +isnull(@gak24f,0) select @amt =isnull(@amt,0) +isnull(@gak22f,0) select @amt3 = 0 select @amt3 = sum(cardamt) from #gcm11_card drop table #gcm11_card drop table #gcm11 end update #tbl_profit set amt = isnull(@amt, 0) + isnull(@amt2, 0) + isnull(@amt3, 0) where code = '110206' and compid = @comp and empno = @empno insert #tbl_profit(compid, empno, code, itemname, is_add, is_leaf, payment, kind) values(@comp,@empno, '11AA01', '服务收入-卡付实际劳动业绩', 0, 0, '', 2) select @amt = 0 select @amt = sum(isnull(isnull(ggb11f, 0) * isnull(gso03i, 1), 0)) --* isnull(a.ggb34f, 1) from ggm02 b with (nolock), #ggm01_1 a, gsm15 c with (nolock) where b.ggb00c = a.gga00c and b.ggb01c = a.gga01c and b.ggb00c = c.gso00c and b.ggb27c = c.gso02c and c.gso01c = '3' and c.gso09f = 1 and b.ggb81c = @empno and a.gga00c = @comp and ((ggb27c >= 'A' and ggb27c <= 'Z') or ggb27c = '#') and ggb27c not in ('WX','ZFB') select @gak22f = 0 select @gak22f = sum(isnull(isnull(ggb11f, 0) * isnull(gso03i, 1) * isnull(gak22f, 1), 0)) from ggm02 b with (nolock), #ggm01_1 a, gsm15 c with (nolock), gam10 d with(nolock) where b.ggb00c = a.gga00c and b.ggb01c = a.gga01c and b.ggb00c = c.gso00c and b.ggb27c = c.gso02c and d.gak00c = a.gga00c and d.gak01c = a.gga07c and b.ggb81c = @empno and a.gga00c = @comp and c.gso01c = '3' and c.gso09f = 1 and ggb27c = '4' --第一销售 select @amt2 = 0 select @amt2 = sum(isnull(isnull(ggc11f, 0) * isnull(gso06f, 1) * isnull(a.ggc30f, 1), 0)) from ggm03 b with (nolock), #ggm01_1 a, gsm15 c with (nolock) where b.ggc00c = a.gga00c and b.ggc01c = a.gga01c and b.ggc00c = c.gso00c and b.ggc27c = c.gso02c and b.ggc81c = @empno and a.gga00c = @comp and c.gso01c = '3' and c.gso10f = 1 and ((ggc27c >= 'A' and ggc27c <= 'Z') or ggc27c = '#') and ggc27c not in ('WX','ZFB') select @gak24f = 0 select @gak24f = sum(isnull(isnull(ggc11f, 0) * isnull(gso06f, 1) * isnull(gak24f, 1), 0)) from ggm03 b with (nolock), #ggm01_1 a, gsm15 c with (nolock),gam10 d with(nolock) where b.ggc00c = a.gga00c and b.ggc01c = a.gga01c and b.ggc00c = c.gso00c and b.ggc27c = c.gso02c and d.gak00c = a.gga00c and d.gak01c = a.gga07c and b.ggc81c = @empno and a.gga00c = @comp and c.gso01c = '3' and c.gso10f = 1 and ggc27c = '4' --第二销售 select @amt2 = isnull(@amt2,0 ) + sum(isnull(isnull(ggc11f, 0) * isnull(gso06f, 1) * isnull(a.ggc30f, 1), 0)) from ggm03 b with (nolock), #ggm01_1 a, gsm15 c with (nolock) where b.ggc00c = a.gga00c and b.ggc01c = a.gga01c and b.ggc00c = c.gso00c and b.ggc27c = c.gso02c and b.ggc82c = @empno and a.gga00c = @comp and c.gso01c = '3' and c.gso10f = 1 and ((ggc27c >= 'A' and ggc27c <= 'Z') or ggc27c = '#') and ggc27c not in ('WX','ZFB') select @gak24f = isnull(@gak24f,0) + sum(isnull(isnull(ggc11f, 0) * isnull(gso06f, 1) * isnull(gak24f, 1), 0)) from ggm03 b with (nolock), #ggm01_1 a, gsm15 c with (nolock),gam10 d with(nolock) where b.ggc00c = a.gga00c and b.ggc01c = a.gga01c and b.ggc00c = c.gso00c and b.ggc27c = c.gso02c and d.gak00c = a.gga00c and d.gak01c = a.gga07c and b.ggc82c = @empno and a.gga00c = @comp and c.gso01c = '3' and c.gso10f = 1 and ggc27c = '4' --第三销售 select @amt2 = isnull(@amt2,0) + sum(isnull(isnull(ggc11f, 0) * isnull(gso06f, 1) * isnull(a.ggc30f, 1), 0)) from ggm03 b with (nolock), #ggm01_1 a, gsm15 c with (nolock) where b.ggc00c = a.gga00c and b.ggc01c = a.gga01c and b.ggc00c = c.gso00c and b.ggc27c = c.gso02c and b.ggc83c = @empno and a.gga00c = @comp and c.gso01c = '3' and c.gso10f = 1 and ((ggc27c >= 'A' and ggc27c <= 'Z') or ggc27c = '#') and ggc27c not in ('WX','ZFB') select @gak24f = isnull(@gak24f,0) + sum(isnull(isnull(ggc11f, 0) * isnull(gso06f, 1) * isnull(gak24f, 1), 0)) from ggm03 b with (nolock), #ggm01_1 a, gsm15 c with (nolock),gam10 d with(nolock) where b.ggc00c = a.gga00c and b.ggc01c = a.gga01c and b.ggc00c = c.gso00c and b.ggc27c = c.gso02c and d.gak00c = a.gga00c and d.gak01c = a.gga07c and b.ggc83c = @empno and a.gga00c = @comp and c.gso01c = '3' and c.gso10f = 1 and ggc27c = '4' select @amt2 = isnull(@amt2,0) +isnull(@gak24f,0) select @amt =isnull(@amt,0) +isnull(@gak22f,0) update #tbl_profit set amt = isnull(@amt, 0) + isnull(@amt2, 0) where code = '11AA01' and compid = @comp and empno = @empno end -- 合计服务收入 select @amt = 0 select @amt = sum(isnull(amt * is_add, 0)) from #tbl_profit where code like '1102%' and is_leaf = 1 and compid = @comp and empno = @empno update #tbl_profit set amt = isnull(@amt, 0) where code = '1102' and compid = @comp and empno = @empno -- 统计收入项目 select @amt = 0 select @amt = sum(isnull(amt * is_add, 0)) from #tbl_profit where code like '11%' and is_leaf = 1 and compid = @comp and empno = @empno update #tbl_profit set amt = isnull(@amt, 0) where code = '11' and compid = @comp and empno = @empno fetch tbl_profit_gx_gl into @comp, @empno end close tbl_profit_gx_gl deallocate tbl_profit_gx_gl -- 单独计算产品的销售以及各种付款方式的产品总销售 truncate table #tbl_stat_fill insert #tbl_stat_fill(paymode, mode, amt, compid, empno) select ggc27c, 0, sum(isnull(case when @sp048 = '2' then ggc21f else isnull(ggc11f, 0) * ggc14f end , 0)),gga00c, ggc81c from ggm03 a with (nolock), #ggm01_1 b where a.ggc00c = b.gga00c and a.ggc01c = b.gga01c and b.gga93c like @staff and (a.ggc81c between @fromdepart and @todepart or @fromdepart = '*') group by ggc27c,gga00c, ggc81c union all select ggc27c, 0, sum(isnull(case when @sp048 = '2' then ggc23f else isnull(ggc11f, 0) * ggc16f end, 0)),gga00c, ggc82c from ggm03 a with (nolock), #ggm01_1 b where a.ggc00c = b.gga00c and a.ggc01c = b.gga01c and b.gga93c like @staff and (a.ggc82c between @fromdepart and @todepart or @fromdepart = '*') group by ggc27c,gga00c, ggc82c union all select ggc27c, 0, sum(isnull(case when @sp048 = '2' then ggc25f else isnull(ggc11f, 0) * ggc18f end, 0)),gga00c, ggc83c from ggm03 a with (nolock), #ggm01_1 b where a.ggc00c = b.gga00c and a.ggc01c = b.gga01c and b.gga93c like @staff and (a.ggc83c between @fromdepart and @todepart or @fromdepart = '*') group by ggc27c,gga00c, ggc83c -- 把产品卡付的每个账户也细分出来 create table #tbl_card_pay_good ( paycode varchar(20) not null, compid varchar(20) not null, empno varchar(20) not null, acctid int null, amt float null, code varchar(20) null, pay_name varchar(30) null, primary key(compid,empno,paycode) ) declare tbl_stat_fill_goods cursor for select compid, empno from #emp_info open tbl_stat_fill_goods fetch tbl_stat_fill_goods into @comp, @empno while @@fetch_status = 0 begin insert #tbl_profit(compid,empno,code, itemname, is_add, is_leaf) values(@comp,@empno,'1103', '产品销售收入', 0, 1) select @amt = 0 select @amt = sum(isnull(a.amt, 0)) from #tbl_stat_fill a where a.compid = @comp and a.empno = @empno update #tbl_profit set amt = isnull(@amt, 0) where code = '1103' and compid = @comp and empno = @empno if (isnull(@sp055f, 0) = 0) -- 按项目支付 begin -- 产品销售-现金 insert #tbl_profit(compid,empno,code, itemname, is_add, is_leaf, payment, kind) values(@comp, @empno,'110301', '产品销售收入-现金', 0, 1, '1', 9) -- 合计产品现金 select @amt = 0 select @amt = sum(isnull(b.amt, 0)) from #tbl_stat_fill b where b.paymode = '1' and b.compid = @comp and b.empno = @empno --更新现金产品 update a set a.amt = @amt from #tbl_profit a where a.code = '110301' and a.compid = @comp and a.empno = @empno -- update a -- set a.amt = isnull(b.amt, 0) -- from #tbl_profit a, #tbl_stat_fill b -- where a.compid = b.compid -- and a.empno = b.empno -- and a.code = '110301' -- and b.paymode = '1' -- and a.compid = @comp -- and a.empno = @empno -- 产品销售-卡 insert #tbl_profit(compid,empno,code, itemname, is_add, is_leaf, payment, kind) values(@comp,@empno,'110302', '产品销售收入-卡付', @add_card_svr, 1, 'CARD', 9) -- 很多种卡付的情况, 把积分支付的情况排除掉,单独列出来 select @amt = 0 select @amt = sum(isnull(b.amt, 0)) from #tbl_stat_fill b, gsm14 c with (nolock) where b.paymode = c.gsn01c and isnull(c.gsn02c, '') <> '' and isnull(c.gsn02c, '') <> '999' and isnull(c.gsn02c, '') <> '3' -- 积分支付 and b.compid = @comp and b.empno = @empno update a set a.amt = isnull(@amt, 0) from #tbl_profit a where a.code = '110302' and a.compid = @comp and a.empno = @empno -- 把产品卡付的每个账户也细分出来 truncate table #tbl_card_pay_good insert #tbl_card_pay_good(compid,empno,paycode, amt) select compid,empno,paymode, sum(amt) from #tbl_stat_fill where (paymode = '#' or paymode = '4' or paymode = '9' or (paymode >= 'A' and paymode <= 'Z')) -- 不包括积分 and paymode not in ('WX','ZFB') group by compid,empno,paymode update a set a.acctid = b.gsn02c from #tbl_card_pay_good a, gsm14 b with (nolock) where a.paycode = b.gsn01c and a.compid = @comp and a.empno = @empno update a set a.code = right('00' + paycode, 2) from #tbl_card_pay_good a where a.compid = @comp and a.empno = @empno update a set a.pay_name = b.gsb03c from #tbl_card_pay_good a, gsm02 b with (nolock) where a.paycode = b.gsb02c and gsb00c = @comp and gsb01c = '3' and a.compid = @comp and a.empno = @empno insert #tbl_profit(compid,empno,code, itemname, is_add, is_leaf, payment, kind, amt) select @comp,@empno,'110302' + isnull(code, '00'), '产品收入-取用储值-' + isnull(pay_name,''), 0, 1, paycode, 2, amt from #tbl_card_pay_good -- 产品销售-抵用券 select @itemname = gsb03c from gsm02 with (nolock) where gsb01c = '3' and gsb02c = '3' and gsb00c = @incompid insert #tbl_profit(compid,empno,code, itemname, is_add, is_leaf, payment, kind) values(@comp,@empno,'110303', '产品销售收入-' + isnull(@itemname, ''), 0, 1, '3', 9) -- 合计产品销售收入 抵用券 select @amt = 0 select @amt = sum(isnull(b.amt, 0)) from #tbl_stat_fill b where b.paymode = '3' and b.compid = @comp and b.empno = @empno --修改 抵用券 合计产品销售收入 抵用券 update a set a.amt = @amt from #tbl_profit a where a.code = '110303' and a.compid = @comp and a.empno = @empno -- 产品销售-抵用券2 select @itemname = gsb03c from gsm02 with (nolock) where gsb01c = '3' and gsb02c = '$' and gsb00c = @comp insert #tbl_profit(compid,empno,code, itemname, is_add, is_leaf, payment, kind) values(@comp,@empno,'110304', '产品销售收入-' + isnull(@itemname, ''), 0, 1, '$', 9) -- 合计产品销售收入 抵用券B select @amt = 0 select @amt = sum(isnull(b.amt, 0)) from #tbl_stat_fill b where b.paymode = '$' and b.compid = @comp and b.empno = @empno -- 修改 合计产品销售收入 抵用券B update a set a.amt = @amt from #tbl_profit a where a.code = '110304' and a.compid = @comp and a.empno = @empno -- 新增加的$开头的支付方式的产品支付 select @code_int = 110350 declare cur_each_pay_3 cursor for select gsb02c from gsm02 with (nolock) where gsb00c = @comp and gsb01c = '3' and gsb02c like '$%' and len(gsb02c) > 1 order by gsb02c open cur_each_pay_3 fetch cur_each_pay_3 into @pay_$ while @@fetch_status = 0 begin select @code_char = convert(varchar(10), @code_int) select @itemname = gsb03c from gsm02 with (nolock) where gsb01c = '3' and gsb02c = @pay_$ and gsb00c = @comp insert #tbl_profit(compid,empno,code, itemname, is_add, is_leaf, payment, kind) values(@comp,@empno,@code_char, '产品销售收入-' + isnull(@itemname, ''), 0, 1, @pay_$, 9) -- 合计产品销售收入 其他以$ 开头的 支付方式 select @amt = 0 select @amt = sum(isnull(b.amt, 0)) from #tbl_stat_fill b where b.paymode = @pay_$ and b.compid = @comp and b.empno = @empno --更新其他 以 $ 开头的支付方式 update a set a.amt = @amt from #tbl_profit a where a.code = @code_char and a.compid = @comp and a.empno = @empno select @code_int = @code_int + 1 select @code_char = convert(varchar(10), @code_int) fetch cur_each_pay_3 into @pay_$ end close cur_each_pay_3 deallocate cur_each_pay_3 -- 产品销售-经理签单,已经在服务收入中包括了 select @itemname = gsb03c from gsm02 with (nolock) where gsb01c = '3' and gsb02c = '8' and gsb00c = @comp insert #tbl_profit(compid,empno,code, itemname, is_add, is_leaf, payment, kind) values(@comp,@empno,'110305', '产品销售收入-' + isnull(@itemname, ''), 0, 1, '8', 9) -- 合计产品销售收入经理签单 select @amt = 0 select @amt = sum(isnull(b.amt, 0)) from #tbl_stat_fill b where b.paymode = '8' and b.compid = @comp and b.empno = @empno -- 更新 合计产品销售收入经理签单 update a set a.amt = @amt from #tbl_profit a where a.code = '110305' and a.compid = @comp and a.empno = @empno -- 产品销售-银行卡 select @itemname = gsb03c from gsm02 with (nolock) where gsb01c = '3' and gsb02c = '6' and gsb00c = @comp insert #tbl_profit(compid,empno,code, itemname, is_add, is_leaf, payment, kind) values(@comp,@empno,'110306', '产品销售收入-' + isnull(@itemname, ''), 0, 1, '6', 9) -- 合计产品销售收入银行卡支付 select @amt = 0 select @amt = sum(isnull(b.amt, 0)) from #tbl_stat_fill b where b.paymode = '6' and b.compid = @comp and b.empno = @empno -- 更新 合计产品销售收入银行卡支付 update a set a.amt = @amt from #tbl_profit a where a.code = '110306' and a.compid = @comp and a.empno = @empno -- 产品销售-支付宝 select @itemname = gsb03c from gsm02 with (nolock) where gsb01c = '3' and gsb02c = 'ZFB' and gsb00c = @comp insert #tbl_profit(compid,empno,code, itemname, is_add, is_leaf, payment, kind) values(@comp,@empno,'1103Z1', '产品销售收入-' + isnull(@itemname, ''), 0, 1, 'ZFB', 9) -- 合计产品销售收入-支付宝 select @amt = 0 select @amt = sum(isnull(b.amt, 0)) from #tbl_stat_fill b where b.paymode = 'ZFB' and b.compid = @comp and b.empno = @empno -- 更新 合计产品销售收入-支付宝 update a set a.amt = @amt from #tbl_profit a where a.code = '1103Z1' and a.compid = @comp and a.empno = @empno insert #tbl_profit(compid,empno,code, itemname, is_add, is_leaf, payment, kind) values(@comp,@empno,'1103W1', '产品销售收入-微信', 0, 1, 'WX', 9) -- 合计产品销售收入-微信 select @amt = 0 select @amt = sum(isnull(b.amt, 0)) from #tbl_stat_fill b where b.paymode = 'WX' and b.compid = @comp and b.empno = @empno --更新 合计产品销售收入-微信 update a set a.amt = @amt from #tbl_profit a where a.code = '1103W1' and a.compid = @comp and a.empno = @empno -- 产品销售-欠款 insert #tbl_profit(compid,empno,code, itemname, is_add, is_leaf) values(@comp,@empno,'110307', '产品销售收入-欠款', 0, 1) -- 合计产品销售收入-欠款 select @amt = 0 select @amt = sum(isnull(b.amt, 0)) from #tbl_stat_fill b where b.paymode = '5' and b.compid = @comp and b.empno = @empno --更新 合计产品销售收入-欠款 update a set a.amt = @amt from #tbl_profit a where a.code = '110307' and a.compid = @comp and a.empno = @empno -- 产品销售内调 select @itemname = gsb03c from gsm02 with (nolock) where gsb01c = '3' and gsb02c = '0' and gsb00c = @comp insert #tbl_profit(compid,empno,code, itemname, is_add, is_leaf, payment, kind) values(@comp,@empno,'110308', '产品销售收入-' + isnull(@itemname, ''), 0, 1, '0', 9) -- 合计产品销售收入-内调 select @amt = 0 select @amt = sum(isnull(b.amt, 0)) from #tbl_stat_fill b where b.paymode = '0' and b.compid = @comp and b.empno = @empno --更新 合计产品销售收入-内调 update a set a.amt = @amt from #tbl_profit a where a.code = '110308' and a.compid = @comp and a.empno = @empno -- 产品销售 积分支付 select @itemname = gsb03c from gsm02 with (nolock) where gsb01c = '3' and gsb02c = '7' and gsb00c = @comp insert #tbl_profit(compid,empno,code, itemname, is_add, is_leaf, payment, kind) ---- 积分永远不作为服务收入 values(@comp,@empno,'110309', '产品销售收入-' + isnull(@itemname, ''), 0, 1, '7', 9) -- 合计产品销售收入-积分 select @amt = 0 select @amt = sum(isnull(b.amt, 0)) from #tbl_stat_fill b where b.paymode = '7' and b.compid = @comp and b.empno = @empno --更新 合计产品销售收入-积分 update a set a.amt = @amt from #tbl_profit a where a.code = '110309' and a.compid = @comp and a.empno = @empno -- 产品欠款的还款 select @amt = 0 select @amt = isnull(sum(isnull(a.gsc05f, 0)), 0) from #gsm03_tmp a where a.gsc02c = 'gz' and isnull(a.isnotcard, 0) = 1 and gsc04c = '1' and gsc12i = 6 and a.gsc00c = @comp and a.empno = @empno insert #tbl_profit(compid, empno,code, itemname, is_add, is_leaf, amt, payment, kind) values(@comp,@empno,'110310', '产品销售收入-还款-现金', 0, 1, @amt, '1', 9) select @amt = 0 select @amt = isnull(sum(isnull(a.gsc05f, 0)), 0) from #gsm03_tmp a where a.gsc02c = 'gz' and isnull(a.isnotcard, 0) = 1 and gsc04c = '6' and gsc12i = 6 and a.gsc00c = @comp and a.empno = @empno insert #tbl_profit(compid, empno,code, itemname, is_add, is_leaf, amt, payment, kind) values(@comp,@empno,'110311', '产品销售收入-还款-银行卡', 0, 1, @amt, '6', 9) select @amt = 0 select @amt = isnull(sum(isnull(a.gsc05f, 0)), 0) from #gsm03_tmp a where a.gsc02c = 'gz' and isnull(a.isnotcard, 0) = 1 and gsc04c = 'ZFB' and gsc12i = 6 and a.gsc00c = @comp and a.empno = @empno insert #tbl_profit(compid, empno,code, itemname, is_add, is_leaf, amt, payment, kind) values(@comp,@empno,'1103Z2', '产品销售收入-还款-支付宝', 0, 1, @amt, 'ZFB', 9) select @amt = 0 select @amt = isnull(sum(isnull(a.gsc05f, 0)), 0) from #gsm03_tmp a where a.gsc02c = 'gz' and isnull(a.isnotcard, 0) = 1 and gsc04c = 'WX' and gsc12i = 6 and a.gsc00c = @comp and a.empno = @empno insert #tbl_profit(compid,empno,code, itemname, is_add, is_leaf, amt, payment, kind) values(@comp,@empno,'1103W2', '产品销售收入-还款-微信', 0, 1, @amt, 'WX', 9) end fetch tbl_stat_fill_goods into @comp, @empno end close tbl_stat_fill_goods deallocate tbl_stat_fill_goods create table #group_fee ( idd int identity not null, code varchar(20) not null, compid varchar(20) not null, empno varchar(20) not null, type varchar(20) not null, name varchar(30) null, amt float null, type2 int null, primary key(idd) ) create nonclustered index index_group_fee on #group_fee(compid,empno) declare tbl_profit_other cursor for select compid, empno from #emp_info open tbl_profit_other fetch tbl_profit_other into @comp, @empno while @@fetch_status = 0 begin --费用 insert #tbl_profit(compid,empno,code, itemname, is_add, is_leaf) values(@comp,@empno,'12', '费用', -1, 0) --其他收入 Huzy 2012/05/31 insert #tbl_profit(compid,empno,code, itemname, is_add, is_leaf) values(@comp,@empno,'18', '其他收入', 1, 0) --虚拟收入 insert #tbl_profit(compid,empno,code, itemname, is_add, is_leaf) values(@comp,@empno,'n1', '虚拟收入', 1, 0) --其他收入 insert #tbl_profit(compid,empno,code, itemname, is_add, is_leaf) values(@comp,@empno,'n2', '虚拟支出', 1, 0) truncate table #group_fee insert #group_fee(compid,empno,code, type, amt) select gqa00c,gqa81c,isnull(gqb03c, ''), isnull(gqa03c, ''), isnull(sum(isnull(gqb05f, 0)), 0) from gqm02 b with (nolock), gqm01 a with (nolock) where b.gqb00c = a.gqa00c and b.gqb01c = a.gqa01c and a.gqa00c = @comp and a.gqa80d >= @fromdate and a.gqa80d <= @todate and a.gqa95c like @staff and (a.gqa81c between @fromdepart and @todepart or @fromdepart = '*')--add @fromdepart by lyj 2007-09-06 and isnull(b.gqb19i, 0) = 0 --是否是虚拟款 group by gqa00c,gqa81c,isnull(gqb03c, ''), isnull(gqa03c, ''), isnull(gqb19i, 0) insert #group_fee(compid,empno,code, type, amt, type2) select gqa00c,gqa81c,isnull(gqb03c, ''), isnull(gqa03c, ''), isnull(sum(isnull(gqb05f, 0)), 0), isnull(b.gqb19i, 0) from gqm02 b with (nolock), gqm01 a with (nolock) where b.gqb00c = a.gqa00c and b.gqb01c = a.gqa01c and a.gqa00c = @comp and a.gqa80d >= @fromdate and a.gqa80d <= @todate and a.gqa95c like @staff and (a.gqa81c between @fromdepart and @todepart or @fromdepart = '*')--add @fromdepart by lyj 2007-09-06 and isnull(b.gqb19i, 0) = 1 --是否是虚拟款 group by gqa00c,gqa81c,isnull(gqb03c, ''), isnull(gqa03c, ''), isnull(b.gqb19i, 0) update a set a.name = b.gsb03c from #group_fee a, gsm02 b with (nolock) where b.gsb00c = @comp and b.gsb01c = 'N' and b.gsb02c = a.code and a.type = 'gv' and compid = @comp and empno = @empno update a set a.name = b.gsb03c from #group_fee a, gsm02 b with (nolock) where b.gsb00c = @comp and b.gsb01c = 'A5' and b.gsb02c = a.code and a.type = 'gp' and compid = @comp and empno = @empno update a set a.code = '12' + right(('000' + convert(varchar(10), a.idd)), 2) from #group_fee a where a.type = 'gv' and type2 is null and compid = @comp and empno = @empno update a set a.code = '18' + right(('000' + convert(varchar(10), a.idd)), 2) from #group_fee a where a.type = 'gp' and type2 is null and compid = @comp and empno = @empno --虚拟收入 update a set a.code = 'n1' + right(('000' + convert(varchar(10), a.idd)), 2) from #group_fee a where a.type = 'gp' and type2 = 1 and compid = @comp and empno = @empno --虚拟支出 update a set a.code = 'n2' + right(('000' + convert(varchar(10), a.idd)), 2) from #group_fee a where a.type = 'gv' and type2 = 1 and compid = @comp and empno = @empno insert #tbl_profit(compid,empno,code, itemname, amt, is_add, is_leaf) select compid,empno,code, isnull(name, ''), amt, case when type = 'gv' then -1 when type = 'gp' then 1 else 0 end, 1 from #group_fee where compid = @comp and empno = @empno order by type --费用-房租 Added by WUXM 2006/02/09 declare @rent float declare @range int declare @flow int declare @code varchar(20) declare @before varchar(10) declare @after varchar(10) declare @cur varchar(10) select @before = substring(convert(varchar(10), dateadd(day, -1, cast(@fromdate as datetime)), 120), 6, 2) select @after = substring(convert(varchar(10), dateadd(day, 1, cast(@todate as datetime)), 120), 6, 2) select @cur = substring(convert(varchar(10), cast(@fromdate as datetime), 120), 6, 2) if (@before = '12' and @cur = '01' and @after = '02') select @rent = sum(isnull(gae18f, 0)) from gam05 with (nolock) where gae01c = @comp else if (@before = '11' and @cur = '12' and @after = '01') select @rent = sum(isnull(gae18f, 0)) from gam05 with (nolock) where gae01c = @comp else if ((@before < @cur) and (@cur < @after)) --从月初到月末的统计 begin select @rent = sum(isnull(gae18f, 0)) from gam05 with (nolock) where gae01c = @comp end else begin select @range = datediff(day, cast(@fromdate as datetime), cast(@todate as datetime)) select @rent = sum(isnull(gae18f, 0)) * (@range + 1) / 30 from gam05 with (nolock) where gae01c = @comp end select @code = '1299' if (@rent > 0) begin insert #tbl_profit(compid,empno,code, itemname, amt, is_add, is_leaf) values (@comp,@empno,@code, '房租', @rent, -1, 1) end select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit where code like '12%' and is_leaf = 1 and compid = @comp and empno = @empno update #tbl_profit set amt = isnull(@amt, 0) where code = '12' and compid = @comp and empno = @empno select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit where code like '18%' and is_leaf = 1 and compid = @comp and empno = @empno update #tbl_profit set amt = isnull(@amt, 0) where code = '18' and compid = @comp and empno = @empno select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit where code like 'n1%' and is_leaf = 1 and compid = @comp and empno = @empno update #tbl_profit set amt = isnull(@amt, 0) where code = 'n1' and compid = @comp and empno = @empno select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit where code like 'n2%' and is_leaf = 1 and compid = @comp and empno = @empno update #tbl_profit set amt = isnull(@amt, 0) where code = 'n2' and compid = @comp and empno = @empno --成本 insert #tbl_profit(compid,empno,code, itemname, is_add, is_leaf) values(@comp,@empno,'13', '成本', -1, 0) select @amt = 0 select @amt = sum(isnull(hah05f, 0)) from ham08 with (nolock) where hah00c = @comp and hah03d >= @fromdate and hah03d <= @todate insert #tbl_profit(compid,empno,code, itemname, amt, is_add, is_leaf) values(@comp,@empno,'1301', '成本-日常奖罚', @amt, -1, 1) --计算利润 select @amt = 0 select @amt = sum(isnull(amt*is_add, 0)) from #tbl_profit where len(code) = 2 and compid = @comp and empno = @empno insert #tbl_profit(compid,empno,code, itemname, amt, is_add, is_leaf) values(@comp,@empno,'19', '利润', isnull(@amt, 0), 0, 1) --单位面积利润率 declare @area float select @area = sum(isnull(gae17f, 0)) from gam05 with (nolock) where gae01c = @comp if (@area > 0) begin insert #tbl_profit(compid,empno,code, itemname, amt, is_add, is_leaf) values (@comp,@empno,20, '单位面积利润', @amt/@area, 0, 1) end --储值余额 select @amt = 0 select @amt = sum(isnull(gcc06f, 0)) from gcm03 with (nolock), gcm01 with (nolock) where gcc00c = @comp and (gcc03i <> 1 and gcc03i <> 0 and gcc03i <> 3 and gcc03i < 888) and gca08i <> 1 and gcc00c = gca00c and gca01c = gcc01c and gcc00c = gca13d insert #tbl_profit(compid,empno,code, itemname, amt, is_add, is_leaf) values(@comp,@empno,'14', '储值余额', isnull(@amt, 0), 0, 1) declare @pointssum float select @pointssum = 0 select @pointssum = sum(isnull(gcd06f, 0)) from gcm04 with (nolock) where gcd09d >= @fromdate and gcd09d <= @todate and gcd02i = 3 and gcd00c = @comp and gcd05i = 0 insert #tbl_profit(compid,empno,code, itemname, amt, is_add, is_leaf) values(@comp,@empno,'15', '赠送积分', isnull(@pointssum, 0), 0, 1) fetch tbl_profit_other into @comp, @empno end close tbl_profit_other deallocate tbl_profit_other --删除第二级为0的 delete #tbl_profit where isnull(amt, 0) = 0 and len(code) > 4 drop table #gsm03_tmp select compid, empno, idd, code, itemname, is_add, amt, is_leaf, payment, kind from #tbl_profit --where compid = '001' order by compid ,empno,code -- if ((@staff = '' or @staff = '*' or @staff = '%') and @flag = 1 and @fromdate = @todate) -- begin -- delete gcm21 -- where gcu00c = @incompid and gcu01c = @todate -- -- insert gcm21(gcu00c, gcu01c, gcu02i, gcu03c, gcu04c, gcu05i, gcu06f, gcu07i, gcu08c, gcu09i) -- select @incompid, @todate, idd, code, itemname, is_add, isnull(amt, 0), is_leaf, payment, kind -- from #tbl_profit -- end --删除为了性能必须要得ggm01的缓冲表 drop table #gem01_tmp drop table #tbl_profit drop table #tbl_stat_fill drop table #emp_info drop table #customed_proj_count drop table #group_fee drop table #ggm01_1 drop table #tbl_card_pay_good drop table #tbl_card_pay drop table #gsm02 drop table #dept_bills drop table #tbl_bill_total end GO /****** Object: StoredProcedure [dbo].[upg_analyse_profit_inter_depart] Script Date: 06/12/2017 06:19:26 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_analyse_profit_inter_depart] ( @compid varchar(10), @fromdate varchar(10), @todate varchar(10), @flag int, -- flag 0:计算员工提成 1:不计算员工提成 @staff varchar(20), -- 收银人员 @fromdepart varchar(10), -- 部门 --add by lyj 2007-09-06 @todepart varchar(10), @calmode int ) as begin declare @amt float declare @amt2 float declare @amt3 float declare @itemtype varchar(10) declare @itemname varchar(60) declare @id int --变量 declare @code_no varchar(10) declare @isadd_0 float declare @isadd_3 float declare @isadd_$ float declare @incompid varchar(10) select @incompid = @compid declare @add_card_sale int declare @add_card_svr int -- 卡付是否作为收入 declare @sp215f float --充值疗程储值方式支付是否作为卡付实际营业额 declare @gak24f float declare @gak22f float select @sp215f = gsa03f from gsm01 with (nolock) where gsa00c = @compid and gsa01c = 'SP212' if @calmode = 2 begin declare @sp115f float select @sp115f = gsa03f from gsm01 with (nolock) where gsa00c = @compid and gsa01c = 'SP115' if @sp115f = 0 select @calmode = 0 else select @calmode = 1 end if @calmode = 0 begin select @add_card_sale = 1 select @add_card_svr = 0 end else begin select @add_card_sale = 0 select @add_card_svr = 1 end select @isadd_0 = gso07f from gsm15 with (nolock) where gso00c = @incompid and gso02c = '0' select @isadd_3 = gso07f from gsm15 with (nolock) where gso00c = @incompid and gso02c = '3' select @isadd_$ = gso07f from gsm15 with (nolock) where gso00c = @incompid and gso02c = '$' select @isadd_0 = isnull(@isadd_0, 0) select @isadd_3 = isnull(@isadd_3, 0) select @isadd_$ = isnull(@isadd_$, 0) select @incompid = @compid if @staff = '*' or @staff = '' begin select @staff = '%' end declare @sp055f float select @sp055f = gsa03f from gsm01 with (nolock) where gsa00c = @incompid and gsa01c = 'SP055' declare @sp048 varchar(10) select @sp048 = gsa02c from gsm01 with (nolock) where gsa00c = @incompid and gsa01c = 'SP048' create table #gsm03_tmp -- 单据--支付明细 ( gsc00c varchar(10) null, --公司别 gsc01c varchar(20) not null, -- 单据编号 gsc02c varchar(5) not null, -- 单据类别 gsc04c varchar(5) not null, -- 支付方式 gsc05f float not null, -- 支付金额 gsc10d varchar(8) not null, -- 单据帐务日期 gsc11c varchar(20) not null, -- 登陆人 gsc12i int not null, -- 方式 isnotcard int null default 0 -- 标志是否为卡销售的,为了区分充值时候的产品或项目的欠款与还款 ) --modify by lm 2015-10-14 create table #gsm03_tmp_gem01 -- 竞争转卡--支付明细 ( gsc00c varchar(20) null, -- 门店编号 gsc01c varchar(20) not null, -- 单据编号 gsc02c varchar(5) not null, -- 单据类别 gsc04c varchar(5) not null, -- 支付方式 gsc05f float not null, -- 支付金额 gsc10d varchar(8) not null, -- 单据帐务日期 gsc11c varchar(20) not null, -- 登陆人 gsc12i int not null, -- 方式 isnotcard int null default 0 -- 标志是否为卡销售的,为了区分充值时候的产品或项目的欠款与还款 ) create nonclustered index gsm03_tmp_idx_02 on #gsm03_tmp(gsc02c) create clustered index gsm03_tmp_idx_01 on #gsm03_tmp(gsc04c) --因为项目或产品的欠款也是纪录在会员账户管理中,而且还款也是纪录在其中 --所以必须把这些特殊的gcm10标记出来,否则在统计会员卡销售的时候就不准 -- modify by lyj for add department 2007-09-06 -- 因为这个涉及到一个单据有几种支付方式的问题,所以只有先把gnm01 gcm10, ggm02, ggm03 gqm01, gem01 插入到 -- 临时表,然后和#gsm03_tmp_01 通过单号和公司别,单据类型来链接, 这样才能把gnm01 gcm10 gem01, gqm01 中的支付方式给分开 -- 但是对于ggm02 和ggm03 还要判断ggm02,ggm03中支付方式和支付明细中的支付方式相同才能把支付方式分开,所以现在 -- 按部门统计的时候支付方式必须用项目支付. -- 把值插入到 #gsm03_tmp declare @SP031 float --是否为多账单统一结帐 select @SP031 = gsa03f from gsm01 with (nolock) where gsa00c = @incompid and gsa01c = 'SP031' create table #dept_bills ( flag int identity (1, 1) not null, compid varchar(10) null, billtype varchar(5) null, billno varchar(20) null, payway varchar(5) null, payamt float null, -- 当从ggm02, ggm03中取数据的时候此值要填写上 ratio float null -- 并不是整个单据都属于一个部门的,是部分 ) -- 并且在gsm03和ggm02, ggm03链接的时候, #gsm03_tmp 中的值应该是payamt 而不是gsm03中的gsc05f create clustered index departresult_01 on #dept_bills(billtype, billno, payway) create table #dept_bills_dept ( flag int identity (1, 1) not null, compid varchar(10) null, billtype varchar(5) null, billno varchar(20) null, payway varchar(5) null, payamt float null, -- 当从ggm02, ggm03中取数据的时候此值要填写上 ratio float null -- 并不是整个单据都属于一个部门的,是部分 ) create clustered index idx_dept_bills_dept on #dept_bills_dept(compid, billtype, billno, payway) create table #tbl_gnm01 ( gna00c varchar(10) not null, gna01c varchar(20) not null, gna27f float null, --第一分享比率 gna29f float null, --分享比率2 gna31f float null, --分享比率3 gna33f float null, --分享比率4 gna35f float null, --分享比率5 gna37f float null, --分享比率6 gna39f float null, --分享比率7 gna41f float null, --分享比率8 gna43f float null, --分享比率9 gna45f float null, --分享比率10 gna81c varchar(10) null, --部门编号 gna82c varchar(10) null, --部门编号2 gna83c varchar(10) null, --部门编号3 gna100c varchar(10) null, --部门编号4 Add by maql 20100705 gna101c varchar(10) null, --部门编号5 Add by maql 20100705 gna102c varchar(10) null, --部门编号6 Add by maql 20100705 gna103c varchar(10) null, --部门编号7 Add by maql 20100705 gna104c varchar(10) null, --部门编号8 Add by maql 20100705 gna105c varchar(10) null, --部门编号9 Add by maql 20100705 gna106c varchar(10) null, --部门编号10 Add by maql 20100705 gna67f float null, gna69f float null, gna71f float null, gna86f float null, gna87f float null, gna88f float null, gna89f float null, gna90f float null, gna91f float null, gna92f float null, bill_total_amt float null, gna26f float null, gnb08f float null, ) create clustered index idx_tbl_gnm01 on #tbl_gnm01(gna00c, gna01c) create nonclustered index idx_tbl_gnm01_02 on #tbl_gnm01(gna81c) create nonclustered index idx_tbl_gnm01_03 on #tbl_gnm01(gna82c) create nonclustered index idx_tbl_gnm01_04 on #tbl_gnm01(gna83c) create nonclustered index idx_tbl_gnm01_05 on #tbl_gnm01(gna100c) create nonclustered index idx_tbl_gnm01_06 on #tbl_gnm01(gna101c) create nonclustered index idx_tbl_gnm01_07 on #tbl_gnm01(gna102c) create nonclustered index idx_tbl_gnm01_08 on #tbl_gnm01(gna103c) create nonclustered index idx_tbl_gnm01_09 on #tbl_gnm01(gna104c) create nonclustered index idx_tbl_gnm01_10 on #tbl_gnm01(gna105c) create nonclustered index idx_tbl_gnm01_11 on #tbl_gnm01(gna106c) create table #tbl_gcm10 ( gcl00c varchar(10) not null, --公司编号 gcl01c varchar(20) not null, --异动单号 gcl16f float null, --第一销售分享比率 gcl17c varchar(20) null, --第二销售 gcl18f float null, --分享比率2 gcl19c varchar(20) null, --第三负责人 gcl20f float null, --分享比率3 gcl23f float null, --分享比率4 gcl25f float null, --分享比率5 gcl27f float null, --分享比率6 gcl29f float null, --分享比率7 gcl31f float null, --分享比率8 gcl33f float null, --分享比率9 gcl35f float null, --分享比率10 gcl81c varchar(10) null, --部门编号 gcl82c varchar(10) null, --部门编号2 gcl83c varchar(10) null, --部门编号3 gcl100c varchar(10) null, --部门编号4 gcl101c varchar(10) null, --部门编号5 gcl102c varchar(10) null, --部门编号6 gcl103c varchar(10) null, --部门编号7 gcl104c varchar(10) null, --部门编号8 gcl105c varchar(10) null, --部门编号9 gcl106c varchar(10) null, --部门编号10 gcl58f float null, gcl60f float null, gcl62f float null, gcl83f float null, gcl84f float null, gcl85f float null, gcl86f float null, gcl87f float null, gcl88f float null, gcl89f float null, gcl07f float null, bill_total_amt float null ) create clustered index idx_tbl_gcm10 on #tbl_gcm10(gcl00c, gcl01c) create nonclustered index idx_tbl_gcm10_02 on #tbl_gcm10(gcl81c) create nonclustered index idx_tbl_gcm10_03 on #tbl_gcm10(gcl82c) create nonclustered index idx_tbl_gcm10_04 on #tbl_gcm10(gcl83c) create nonclustered index idx_tbl_gcm10_05 on #tbl_gcm10(gcl100c) create nonclustered index idx_tbl_gcm10_06 on #tbl_gcm10(gcl101c) create nonclustered index idx_tbl_gcm10_07 on #tbl_gcm10(gcl102c) create nonclustered index idx_tbl_gcm10_08 on #tbl_gcm10(gcl103c) create nonclustered index idx_tbl_gcm10_09 on #tbl_gcm10(gcl104c) create nonclustered index idx_tbl_gcm10_10 on #tbl_gcm10(gcl105c) create nonclustered index idx_tbl_gcm10_11 on #tbl_gcm10(gcl106c) insert #tbl_gnm01(gna00c, gna01c, gna27f, gna29f, gna31f, gna33f, gna35f, gna37f, gna39f, gna41f, gna43f, gna45f, gna81c, gna82c, gna83c, gna100c, gna101c, gna102c, gna103c, gna104c, gna105c, gna106c, gna67f, gna69f, gna71f, gna86f, gna87f, gna88f, gna89f, gna90f, gna91f, gna92f, gna26f, gnb08f, bill_total_amt) select gna00c, gna01c, gna27f, gna29f, gna31f, gna33f, gna35f, gna37f, gna39f, gna41f, gna43f, gna45f, gna81c, gna82c, gna83c, gna100c, gna101c, gna102c, gna103c, gna104c, gna105c, gna106c, gna67f, gna69f, gna71f, gna86f, gna87f, gna88f, gna89f, gna90f, gna91f, gna92f, gna26f, gnb08f, isnull(gna67f, 0) + isnull(gna69f, 0) + isnull(gna71f, 0) + isnull(gna86f, 0) + isnull(gna87f, 0) + isnull(gna88f, 0) + isnull(gna89f, 0) + isnull(gna90f, 0) + isnull(gna91f, 0) + isnull(gna92f, 0) from gnm01 a with (nolock), gam26 z with (nolock), gnm02 c with (nolock) where a.gna00c = z.gaz02c and a.gna00c = c.gnb00c and a.gna01c = c.gnb01c and z.gaz01c = @incompid and gna80d between @fromdate and @todate and gna93c like @staff insert #tbl_gcm10(gcl00c, gcl01c, gcl16f, gcl17c, gcl18f, gcl19c, gcl20f, gcl23f, gcl25f, gcl27f, gcl29f, gcl31f, gcl33f, gcl35f, gcl81c, gcl82c, gcl83c, gcl100c, gcl101c, gcl102c, gcl103c, gcl104c, gcl105c, gcl106c, gcl58f, gcl60f, gcl62f, gcl83f, gcl84f, gcl85f, gcl86f, gcl87f, gcl88f, gcl89f, gcl07f, bill_total_amt) select gcl00c, gcl01c, gcl16f, gcl17c, gcl18f, gcl19c, gcl20f, gcl23f, gcl25f, gcl27f, gcl29f, gcl31f, gcl33f, gcl35f, gcl81c, gcl82c, gcl83c, gcl100c, gcl101c, gcl102c, gcl103c, gcl104c, gcl105c, gcl106c, gcl58f, gcl60f, gcl62f, gcl83f, gcl84f, gcl85f, gcl86f, gcl87f, gcl88f, gcl89f, gcl07f, isnull(gcl58f, 0) + isnull(gcl60f, 0) + isnull(gcl62f, 0) + isnull(gcl83f, 0) + isnull(gcl84f, 0) + isnull(gcl85f, 0) + isnull(gcl86f, 0) + isnull(gcl87f, 0) + isnull(gcl88f, 0) + isnull(gcl89f, 0) from gcm10 a with (nolock), gam26 z with (nolock) where gcl00c = z.gaz02c and z.gaz01c = @incompid and gcl80d between @fromdate and @todate and gcl93c like @staff --把还款类型为888 的查询保存下来 create table #gcm10_888 ( compid varchar(10) not null, billid varchar(20) not null, primary key(compid, billid) ) insert #gcm10_888(compid, billid) select gcl00c,gcl01c from gcm10 a with (nolock),gam26 b with (nolock) where a.gcl00c = b.gaz02c and b.gaz01c = @incompid and gcl80d between @fromdate and @todate and a.gcl04c = '888' and gcl93c like @staff --为了提高性能先把此值插入到临时表 create table #gsm03_tmp_01 -- 单据--支付明细 ( gsc00c varchar(10) not null, --公司 gsc01c varchar(20) not null, --单据编号 gsc02c varchar(5) not null, --单据类别 gsc04c varchar(5) not null, --支付方式 gsc05f float not null, --支付金额 gsc10d varchar(8) not null, --单据帐务日期 gsc11c varchar(20) not null, --登陆人 gsc12i int not null, --方式 gsc05ftotal float null ) create nonclustered index gsm03_tmp_01_01 on #gsm03_tmp_01(gsc02c) create nonclustered index gsm03_tmp_01_02 on #gsm03_tmp_01(gsc01c) -------把gsm03里面符合条件的先insert到临时表中 if (@compid = '001' or @compid = '*') begin select a.gea00c,a.gea01c,sum(gsc05f) gsc05ftotal into #result1 from gem01 a with(nolock),gsm03 b with(nolock) where a.gea00c = b.gsc00c and a.gea01c = b.gsc01c --and a.gea00c = @compid and b.gsc10d >= @fromdate and b.gsc10d <= @todate and b.gsc02c = 'ge' group by gea00c,gea01c insert #gsm03_tmp_01(a.gsc00c, gsc01c, a.gsc02c, gsc04c, gsc05f, gsc10d, gsc11c, gsc12i,gsc05ftotal) select gsc00c, gsc01c, gsc02c, gsc04c, isnull(gsc05f, 0), gsc10d, gsc11c, isnull(gsc12i, -1),gsc05ftotal from gsm03 a with (nolock) left join #result1 b on b.gea00c = a.gsc00c and b.gea01c = a.gsc01c and a.gsc02c = 'ge' where gsc10d >= @fromdate and gsc10d <= @todate and gsc11c like @staff drop table #result1 end else begin select a.gea00c,a.gea01c,sum(gsc05f) gsc05ftotal into #result from gem01 a with(nolock),gsm03 b with(nolock) where a.gea00c = b.gsc00c and a.gea01c = b.gsc01c and a.gea00c = @compid and b.gsc10d >= @fromdate and b.gsc10d <= @todate and b.gsc02c = 'ge' group by gea00c,gea01c insert #gsm03_tmp_01(gsc00c, gsc01c, gsc02c, gsc04c, gsc05f, gsc10d, gsc11c, gsc12i,gsc05ftotal) select gsc00c, gsc01c, gsc02c, gsc04c, isnull(gsc05f, 0), gsc10d, gsc11c, isnull(gsc12i, -1),isnull(gsc05ftotal,0)gsc05ftotal from gsm03 a with (nolock) left join #result b on b.gea00c = a.gsc00c and b.gea01c = a.gsc01c and a.gsc02c = 'ge' where gsc00c = @compid and gsc10d >= @fromdate and gsc10d <= @todate and gsc11c like @staff drop table #result end create table #tbl_bill_total ( compid varchar(20) not null, billid varchar(20) not null, amt float null, primary key(compid, billid)) if (@compid = '001' or @compid = '*') begin insert #tbl_bill_total(compid, billid, amt) select gnb00c, gnb01c, sum(gnb08f) from gnm01 c with (nolock), gnm02 a with (nolock) where c.gna80d >= @fromdate and c.gna80d <= @todate and c.gna00c = a.gnb00c and c.gna01c = a.gnb01c group by gnb00c, gnb01c end else begin insert #tbl_bill_total(compid, billid, amt) select gnb00c, gnb01c, sum(gnb08f) from gnm01 c with (nolock), gnm02 a with (nolock) where a.gnb00c = @compid and c.gna80d >= @fromdate and c.gna80d <= @todate and c.gna00c = a.gnb00c and c.gna01c = a.gnb01c group by gnb00c, gnb01c end ---在营业数据插入到#departsesult临时表 insert #dept_bills_dept(compid, billtype, billno, ratio, payway, payamt) select gna00c, 'gn', gna01c, isnull(gna27f, 0), gsc04c, case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gna67f,0) * gsc05f / bill_total_amt end from #tbl_gnm01 a, #gsm03_tmp_01 b where a.gna00c = b.gsc00c and a.gna01c = b.gsc01c and b.gsc02c = 'gn' and (isnull(gna81c, '') between @fromdepart and @todepart or @fromdepart = '*') --add by lyj 2007-09-06 insert #dept_bills_dept(compid, billtype, billno, ratio, payway, payamt) select gna00c, 'gn', gna01c, isnull(gna29f, 0), gsc04c, case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gna69f,0) * gsc05f / bill_total_amt end from #tbl_gnm01 a, #gsm03_tmp_01 b where a.gna00c = b.gsc00c and a.gna01c = b.gsc01c and b.gsc02c = 'gn' and (isnull(gna82c, '') between @fromdepart and @todepart or @fromdepart = '*') --add by lyj 2007-09-06 insert #dept_bills_dept(compid, billtype, billno, ratio, payway, payamt) select gna00c, 'gn', gna01c, isnull(gna31f, 0), gsc04c, case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gna71f,0) * gsc05f / bill_total_amt end from #tbl_gnm01 a, #gsm03_tmp_01 b where a.gna00c = b.gsc00c and a.gna01c = b.gsc01c and b.gsc02c = 'gn' and (isnull(gna83c, '') between @fromdepart and @todepart or @fromdepart = '*') --add by lyj 2007-09-06 insert #dept_bills_dept(compid, billtype, billno, ratio, payway, payamt) select gna00c, 'gn', gna01c, isnull(gna33f, 0), gsc04c, case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gna86f,0) * gsc05f / bill_total_amt end from #tbl_gnm01 a, #gsm03_tmp_01 b where a.gna00c = b.gsc00c and a.gna01c = b.gsc01c and b.gsc02c = 'gn' and (isnull(gna100c, '') between @fromdepart and @todepart or @fromdepart = '*') --add by maql 20100705 insert #dept_bills_dept(compid, billtype, billno, ratio, payway, payamt) select gna00c, 'gn', gna01c, isnull(gna35f, 0), gsc04c, case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gna87f,0) * gsc05f / bill_total_amt end from #tbl_gnm01 a, #gsm03_tmp_01 b where a.gna00c = b.gsc00c and a.gna01c = b.gsc01c and b.gsc02c = 'gn' and (isnull(gna101c, '') between @fromdepart and @todepart or @fromdepart = '*') --add by maql 20100705 insert #dept_bills_dept(compid, billtype, billno, ratio, payway, payamt) select gna00c, 'gn', gna01c, isnull(gna37f, 0), gsc04c, case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gna88f,0) * gsc05f / bill_total_amt end from #tbl_gnm01 a, #gsm03_tmp_01 b where a.gna00c = b.gsc00c and a.gna01c = b.gsc01c and b.gsc02c = 'gn' and (isnull(gna102c, '') between @fromdepart and @todepart or @fromdepart = '*') --add by maql 20100705 insert #dept_bills_dept(compid, billtype, billno, ratio, payway, payamt) select gna00c, 'gn', gna01c, isnull(gna39f, 0), gsc04c,case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gna89f,0) * gsc05f / bill_total_amt end from #tbl_gnm01 a, #gsm03_tmp_01 b where a.gna00c = b.gsc00c and a.gna01c = b.gsc01c and b.gsc02c = 'gn' and (isnull(gna103c, '') between @fromdepart and @todepart or @fromdepart = '*') --add by maql 20100705 insert #dept_bills_dept(compid, billtype, billno, ratio, payway, payamt) select gna00c, 'gn', gna01c, isnull(gna41f, 0), gsc04c, case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gna90f,0) * gsc05f / bill_total_amt end from #tbl_gnm01 a, #gsm03_tmp_01 b where a.gna00c = b.gsc00c and a.gna01c = b.gsc01c and b.gsc02c = 'gn' and (isnull(gna104c, '') between @fromdepart and @todepart or @fromdepart = '*') --add by maql 20100705 insert #dept_bills_dept(compid, billtype, billno, ratio, payway, payamt) select gna00c, 'gn', gna01c, isnull(gna43f, 0), gsc04c, case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gna91f,0) * gsc05f / bill_total_amt end from #tbl_gnm01 a, #gsm03_tmp_01 b where a.gna00c = b.gsc00c and a.gna01c = b.gsc01c and b.gsc02c = 'gn' and (isnull(gna105c, '') between @fromdepart and @todepart or @fromdepart = '*') --add by maql 20100705 insert #dept_bills_dept(compid, billtype, billno, ratio, payway, payamt) select gna00c, 'gn', gna01c, isnull(gna45f, 0), gsc04c, case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gna92f,0) * gsc05f / bill_total_amt end from #tbl_gnm01 a, #gsm03_tmp_01 b where a.gna00c = b.gsc00c and a.gna01c = b.gsc01c and b.gsc02c = 'gn' and (isnull(gna106c, '') between @fromdepart and @todepart or @fromdepart = '*') --add by maql 20100705 insert into #dept_bills(compid, billtype, billno, ratio, payway, payamt) select compid, billtype, billno, sum(ratio), payway, sum(payamt) from #dept_bills_dept a group by a.compid, a.billtype, a.billno, a.payway drop table #tbl_gnm01 -- insert gcm10 delete #dept_bills_dept -- 第一销售 insert #dept_bills_dept(compid, billtype, billno, ratio, payway, payamt) select gcl00c, 'gz', gcl01c, isnull(gcl16f, 0), gsc04c, case when @sp048 = '2' then gcl58f else (case when isnull(bill_total_amt,0) = 0 then 0 else (isnull(gcl58f, 0) * gsc05f)/ bill_total_amt end) end from #tbl_gcm10 a, #gsm03_tmp_01 b where a.gcl00c = b.gsc00c and a.gcl01c = b.gsc01c and b.gsc02c = 'gz' and (isnull(gcl81c, '') between @fromdepart and @todepart or @fromdepart = '*') --add by lyj 2007-09-06 insert #dept_bills_dept(compid, billtype, billno, ratio, payway, payamt) select gcl00c, 'gz', gcl01c, isnull(gcl18f, 0), gsc04c, case when @sp048 = '2' then gcl60f else (case when isnull(bill_total_amt,0) = 0 then 0 else (isnull(gcl60f, 0) * gsc05f)/ bill_total_amt end)end from #tbl_gcm10 a, #gsm03_tmp_01 b where a.gcl00c = b.gsc00c and a.gcl01c = b.gsc01c and b.gsc02c = 'gz' and (isnull(gcl82c, '') between @fromdepart and @todepart or @fromdepart = '*') --add by lyj 2007-09-06 insert #dept_bills_dept(compid, billtype, billno, ratio, payway, payamt) select gcl00c, 'gz', gcl01c, isnull(gcl20f, 0), gsc04c, case when @sp048 = '2' then gcl62f else (case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gcl62f, 0) * gsc05f/ bill_total_amt end) end from #tbl_gcm10 a, #gsm03_tmp_01 b where a.gcl00c = b.gsc00c and a.gcl01c = b.gsc01c and b.gsc02c = 'gz' and (isnull(gcl83c, '') between @fromdepart and @todepart or @fromdepart = '*') --add by lyj 2007-09-06 insert #dept_bills_dept(compid, billtype, billno, ratio, payway, payamt) select gcl00c, 'gz', gcl01c, isnull(gcl23f, 0), gsc04c, case when @sp048 = '2' then gcl83f else (case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gcl83f, 0) * gsc05f/bill_total_amt end) end from #tbl_gcm10 a, #gsm03_tmp_01 b where a.gcl00c = b.gsc00c and a.gcl01c = b.gsc01c and b.gsc02c = 'gz' and (isnull(gcl100c, '') between @fromdepart and @todepart or @fromdepart = '*') --add by maql 20100705 insert #dept_bills_dept(compid, billtype, billno, ratio, payway, payamt) select gcl00c, 'gz', gcl01c, isnull(gcl25f, 0), gsc04c, case when @sp048 = '2' then gcl84f else (case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gcl84f, 0) * gsc05f/bill_total_amt end) end from #tbl_gcm10 a, #gsm03_tmp_01 b where a.gcl00c = b.gsc00c and a.gcl01c = b.gsc01c and b.gsc02c = 'gz' and (isnull(gcl101c, '') between @fromdepart and @todepart or @fromdepart = '*') --add by maql 20100705 insert #dept_bills_dept(compid, billtype, billno, ratio, payway, payamt) select gcl00c, 'gz', gcl01c, isnull(gcl27f, 0), gsc04c, case when @sp048 = '2' then gcl85f else (case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gcl85f, 0) * gsc05f/bill_total_amt end) end from #tbl_gcm10 a, #gsm03_tmp_01 b where a.gcl00c = b.gsc00c and a.gcl01c = b.gsc01c and b.gsc02c = 'gz' and (isnull(gcl102c, '') between @fromdepart and @todepart or @fromdepart = '*') --add by maql 20100705 insert #dept_bills_dept(compid, billtype, billno, ratio, payway, payamt) select gcl00c, 'gz', gcl01c, isnull(gcl29f, 0), gsc04c, case when @sp048 = '2' then gcl86f else (case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gcl86f, 0) * gsc05f/bill_total_amt end) end from #tbl_gcm10 a, #gsm03_tmp_01 b where a.gcl00c = b.gsc00c and a.gcl01c = b.gsc01c and b.gsc02c = 'gz' and (isnull(gcl103c, '') between @fromdepart and @todepart or @fromdepart = '*') --add by maql 20100705 insert #dept_bills_dept(compid, billtype, billno, ratio, payway, payamt) select gcl00c, 'gz', gcl01c, isnull(gcl31f, 0), gsc04c, case when @sp048 = '2' then gcl87f else (case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gcl87f, 0) * gsc05f/bill_total_amt end) end from #tbl_gcm10 a, #gsm03_tmp_01 b where a.gcl00c = b.gsc00c and a.gcl01c = b.gsc01c and b.gsc02c = 'gz' and (isnull(gcl104c, '') between @fromdepart and @todepart or @fromdepart = '*') --add by maql 20100705 insert #dept_bills_dept(compid, billtype, billno, ratio, payway, payamt) select gcl00c, 'gz', gcl01c, isnull(gcl33f, 0), gsc04c, case when @sp048 = '2' then gcl88f else (case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gcl88f, 0) * gsc05f/bill_total_amt end) end from #tbl_gcm10 a, #gsm03_tmp_01 b where a.gcl00c = b.gsc00c and a.gcl01c = b.gsc01c and b.gsc02c = 'gz' and (isnull(gcl105c, '') between @fromdepart and @todepart or @fromdepart = '*') --add by maql 20100705 insert #dept_bills_dept(compid, billtype, billno, ratio, payway, payamt) select gcl00c, 'gz', gcl01c, isnull(gcl35f, 0), gsc04c, case when @sp048 = '2' then gcl89f else (case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gcl89f, 0) * gsc05f/bill_total_amt end) end from #tbl_gcm10 a, #gsm03_tmp_01 b where a.gcl00c = b.gsc00c and a.gcl01c = b.gsc01c and b.gsc02c = 'gz' and (isnull(gcl106c, '') between @fromdepart and @todepart or @fromdepart = '*') --add by maql 20100705 insert #dept_bills(compid, billtype, billno, ratio, payway, payamt) select compid, billtype, billno, sum(ratio), payway, sum(payamt) from #dept_bills_dept a group by a.compid, a.billtype, a.billno, a.payway drop table #tbl_gcm10 drop table #dept_bills_dept declare @customed_proj_count float if (@SP031 = '1') --收银的时候直接结帐 begin insert #dept_bills(compid, billtype, billno, payway, payamt) select ggb00c, 'gx', ggb01c, ggb27c, isnull(ggb11f, 0) from ggm02 with (nolock), ggm01 with (nolock), gam26 z with (nolock) where gga00c = ggb00c and gga01c = ggb01c and gga00c = z.gaz02c and z.gaz01c = @incompid and gga80d between @fromdate and @todate and (isnull(ggb81c, '') between @fromdepart and @todepart or @fromdepart = '*') --add by lyj 2007-09-06 and gga93c like @staff select @customed_proj_count = sum(isnull(ggb05f,0)) from ggm02 with (nolock), ggm01 with (nolock), gam26 z with (nolock) where gga00c = ggb00c and gga01c = ggb01c and gga00c = z.gaz02c and z.gaz01c = @incompid and gga80d between @fromdate and @todate and (isnull(ggb81c, '') between @fromdepart and @todepart or @fromdepart = '*') --add by lyj 2007-09-06 and gga93c like @staff insert #dept_bills(compid, billtype, billno, payway, payamt) select ggc00c, 'gx', ggc01c, ggc27c, case when @sp048 = '2' then ggc21f else isnull(ggc11f, 0) * ggc14f end from ggm03 with (nolock), ggm01 with (nolock), gam26 z with (nolock) where ggc00c = gga00c and gga01c = ggc01c and gga00c = z.gaz02c and z.gaz01c = @incompid and gga80d between @fromdate and @todate and (isnull(ggc81c, '') between @fromdepart and @todepart or @fromdepart = '*') --add by lyj 2007-09-06 and gga93c like @staff union all select ggc00c, 'gx', ggc01c, ggc27c, case when @sp048 = '2' then ggc23f else isnull(ggc11f, 0) * ggc16f end from ggm03 with (nolock), ggm01 with (nolock), gam26 z with (nolock) where ggc00c = gga00c and gga01c = ggc01c and gga00c = z.gaz02c and z.gaz01c = @incompid and gga80d between @fromdate and @todate and (isnull(ggc82c, '') between @fromdepart and @todepart or @fromdepart = '*') --add by lyj 2007-09-06 and gga93c like @staff union all select ggc00c, 'gx', ggc01c, ggc27c, case when @sp048 = '2' then ggc25f else isnull(ggc11f, 0) * ggc18f end from ggm03 with (nolock), ggm01 with (nolock), gam26 z with (nolock) where ggc00c = gga00c and gga01c = ggc01c and gga00c = z.gaz02c and z.gaz01c = @incompid and gga80d between @fromdate and @todate and (isnull(ggc83c, '') between @fromdepart and @todepart or @fromdepart = '*') --add by lyj 2007-09-06 and gga93c like @staff end else if (@SP031 = '2')--多账单统一结帐 begin insert #dept_bills(compid, billtype, billno, payway, payamt) select ggb00c, 'gl', gga30c, ggb27c, isnull(ggb11f, 0) from ggm02 with (nolock), ggm01 with (nolock), gam26 z with (nolock) where gga00c = ggb00c and gga01c = ggb01c and ggb00c = z.gaz02c and z.gaz01c = @incompid and gga80d between @fromdate and @todate and (isnull(ggb81c, '') between @fromdepart and @todepart or @fromdepart = '*') --add by lyj 2007-09-06 and gga93c like @staff select @customed_proj_count = sum(isnull(ggb05f,0)) from ggm02 with (nolock), ggm01 with (nolock), gam26 z with (nolock) where gga00c = ggb00c and gga01c = ggb01c and ggb00c = z.gaz02c and z.gaz01c = @incompid and gga80d between @fromdate and @todate and (isnull(ggb81c, '') between @fromdepart and @todepart or @fromdepart = '*') --add by lyj 2007-09-06 and gga93c like @staff --insert ggm03 insert #dept_bills(compid, billtype, billno, payway, payamt) select ggc00c, 'gl', gga30c, ggc27c, case when @sp048 = '2' then ggc21f else isnull(ggc11f, 0) * ggc14f end from ggm03 with (nolock), ggm01 with (nolock), gam26 z with (nolock) where ggc00c = gga00c and gga01c = ggc01c and gga00c = z.gaz02c and z.gaz01c = @incompid and gga80d between @fromdate and @todate and (isnull(ggc81c, '') between @fromdepart and @todepart or @fromdepart = '*') --add by lyj 2007-09-06 and gga93c like @staff union all select ggc00c, 'gl', gga30c, ggc27c, case when @sp048 = '2' then ggc23f else isnull(ggc11f, 0) * ggc16f end from ggm03 with (nolock), ggm01 with (nolock), gam26 z with (nolock) where ggc00c = gga00c and gga01c = ggc01c and gga00c = z.gaz02c and z.gaz01c = @incompid and gga80d between @fromdate and @todate and (isnull(ggc82c, '') between @fromdepart and @todepart or @fromdepart = '*') --add by lyj 2007-09-06 and gga93c like @staff union all select ggc00c, 'gl', gga30c, ggc27c, case when @sp048 = '2' then ggc25f else isnull(ggc11f, 0) * ggc18f end from ggm03 with (nolock), ggm01 with (nolock), gam26 z with (nolock) where ggc00c = gga00c and gga01c = ggc01c and gga00c = z.gaz02c and z.gaz01c = @incompid and gga80d between @fromdate and @todate and (isnull(ggc83c, '') between @fromdepart and @todepart or @fromdepart = '*') --add by lyj 2007-09-06 and gga93c like @staff end --insert gqm01 --费用登记 insert #dept_bills(compid, billtype, billno) select gqa00c, 'gv', gqa01c from gqm01 with (nolock), gam26 z with (nolock) where gqa00c = z.gaz02c and z.gaz01c = @incompid and gqa80d between @fromdate and @todate and (isnull(gqa10c, '') between @fromdepart and @todepart or @fromdepart = '*') --add by lyj 2007-09-06 and gqa07c like @staff --insert gem01 --会籍变更里面的会员卡异动 -- insert #dept_bills(compid, billtype, billno) -- select gea00c, 'ge', gea01c -- from gem01 with (nolock), gam26 z with (nolock) -- where gea00c = z.gaz02c -- and z.gaz01c = @incompid -- and gea80d between @fromdate and @todate -- and (isnull(gea81c, '') between @fromdepart and @todepart or @fromdepart = '*') --add by lyj 2007-09-06 -- and gea91c like @staff -------把#gsm03_tmp_01和#departsesult里面符合条件的先insert到#gsm03_tmp临时表中 insert #gsm03_tmp(gsc00c,gsc01c, gsc02c, gsc04c, gsc05f, gsc10d, gsc11c, gsc12i) select compid,gsc01c, billtype, payway, isnull(payamt, 0), gsc10d, gsc11c, isnull(gsc12i, -1) from #dept_bills a, #gsm03_tmp_01 b where b.gsc01c = a.billno and b.gsc00c = a.compid and b.gsc02c = a.billtype and b.gsc04c = a.payway and b.gsc11c like @staff --Start竞争转卡业绩 add by lm 2015-10-13 create table #gem01_tmp -- 会员卡--异动单 ( gea00c char(10) not null, --门店编号 gea01c varchar(20) not null, --异动单号 gea07c varchar(20) null, --异动後卡号(只对 -卡种变更 有效) gea21c varchar(20) null, --销售人员(对 -卡种变更 有效) gea27f float null, --第一销售业绩 gea28c varchar(20) null, --第二销售(支付方式4) gea29f float null, --第二销售业绩 gea30c varchar(20) null, --第3销售 gea31f float null, --第3销售业绩 gea32c varchar(20) null, --第4销售 gea33f float null, --第4销售业绩 gea34c varchar(20) null, --第5销售 gea35f float null, --第5销售业绩 gea36c varchar(20) null, --第6销售 gea37f float null, --第6销售业绩 gea38c varchar(20) null, --第7销售 gea39f float null, --第7销售业绩 gea40c varchar(20) null, --第8销售 gea41f float null, --第8销售业绩 gea42c varchar(20) null, --第9销售 gea43f float null, --第9销售业绩 gea44c varchar(20) null, --第10销售 gea45f float null, --第10销售业绩 geb09i int null, --收支方向(1-收取 ,2 -支付) gea12f float null, --收费总额 geb10f float null, --金额 gea81c varchar(20) null, --部门1 gea82c varchar(20) null, --部门2 gea83c varchar(20) null, --部门3 gea84c varchar(20) null, --部门4 gea85c varchar(20) null, --部门5 gea86c varchar(20) null, --部门6 gea87c varchar(20) null, --部门7 gea88c varchar(20) null, --部门8 gea89c varchar(20) null, --部门9 gea90c varchar(20) null, --部门10 bill_total_amt float null -- 总业绩 ) insert #gem01_tmp(gea00c, gea01c, gea07c, gea21c, gea27f, gea28c, gea29f, gea30c, gea31f, gea32c, gea33f, gea34c, gea35f, gea36c, gea37f, gea38c, gea39f, gea40c, gea41f, gea42c, gea43f, gea44c, gea45f, gea12f,gea81c,gea82c,gea83c,gea84c,gea85c,gea86c,gea87c,gea88c,gea89c,gea90c,bill_total_amt) select gea00c, gea01c, gea07c, gea21c, gea27f, gea28c, gea29f, gea30c, gea31f, gea32c, gea33f, gea34c, gea35f, gea36c, gea37f, gea38c, gea39f, gea40c, gea41f, gea42c, gea43f, gea44c, gea45f, gea12f,gea81c,gea82c,gea83c,gea84c,gea85c,gea86c,gea87c,gea88c,gea89c,gea90c, isnull(gea27f,0)+isnull(gea29f,0)+isnull(gea31f,0)+isnull(gea33f,0)+isnull(gea35f,0)+isnull(gea37f,0)+isnull(gea39f,0)+isnull(gea41f,0)+isnull(gea43f,0)+isnull(gea45f,0) from gem01 a with(nolock), gam26 z with (nolock) where a.gea00c = z.gaz02c and z.gaz01c = @compid and gea80d between @fromdate and @todate --第一销售 insert #gsm03_tmp_gem01(gsc01c, gsc02c, gsc04c, gsc05f, gsc10d, gsc11c, gsc12i) select gea01c, 'ge', gsc04c, case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gea27f,0) * b.gsc05f / bill_total_amt end gea27f, gsc10d, gsc11c, gsc12i from #gem01_tmp a with (nolock), #gsm03_tmp_01 b where a.gea00c = b.gsc00c and a.gea01c = b.gsc01c and b.gsc02c = 'ge' and ((gea81c between @fromdepart and @todepart) or @fromdepart = '*') --add by lm 2015-10-13 and isnull(gea21c,'') <> '' --第二销售 insert #gsm03_tmp_gem01(gsc01c, gsc02c, gsc04c, gsc05f, gsc10d, gsc11c, gsc12i) select gea01c, 'ge', gsc04c, case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gea29f,0) * b.gsc05f / bill_total_amt end gea29f, gsc10d, gsc11c, gsc12i from #gem01_tmp a with (nolock), #gsm03_tmp_01 b where a.gea00c = b.gsc00c and a.gea01c = b.gsc01c and b.gsc02c = 'ge' and ((gea82c between @fromdepart and @todepart) or @fromdepart = '*') and isnull(gea28c,'') <> '' --第三销售 insert #gsm03_tmp_gem01(gsc01c, gsc02c, gsc04c, gsc05f, gsc10d, gsc11c, gsc12i) select gea01c, 'ge', gsc04c, case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gea31f,0) * b.gsc05f / bill_total_amt end, gsc10d, gsc11c, gsc12i from #gem01_tmp a with (nolock), #gsm03_tmp_01 b where a.gea00c = b.gsc00c and a.gea01c = b.gsc01c and b.gsc02c = 'ge' and ((gea83c between @fromdepart and @todepart) or @fromdepart = '*') and isnull(gea30c,'') <> '' --第四销售 insert #gsm03_tmp_gem01(gsc01c, gsc02c, gsc04c, gsc05f, gsc10d, gsc11c, gsc12i) select gea01c, 'ge', gsc04c, case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gea33f,0) * b.gsc05f / bill_total_amt end, gsc10d, gsc11c, gsc12i from #gem01_tmp a with (nolock), #gsm03_tmp_01 b where a.gea00c = b.gsc00c and a.gea01c = b.gsc01c and b.gsc02c = 'ge' and ((gea84c between @fromdepart and @todepart) or @fromdepart = '*') and isnull(gea32c,'') <> '' --第五销售 insert #gsm03_tmp_gem01(gsc01c, gsc02c, gsc04c, gsc05f, gsc10d, gsc11c, gsc12i) select gea01c, 'ge', gsc04c, case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gea35f,0) * b.gsc05f / bill_total_amt end, gsc10d, gsc11c, gsc12i from #gem01_tmp a with (nolock), #gsm03_tmp_01 b where a.gea00c = b.gsc00c and a.gea01c = b.gsc01c and b.gsc02c = 'ge' and ((gea85c between @fromdepart and @todepart) or @fromdepart = '*') and isnull(gea34c,'') <> '' --第六销售 insert #gsm03_tmp_gem01(gsc01c, gsc02c, gsc04c, gsc05f, gsc10d, gsc11c, gsc12i) select gea01c, 'ge', gsc04c, case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gea37f,0) * b.gsc05f / bill_total_amt end, gsc10d, gsc11c, gsc12i from #gem01_tmp a with (nolock), #gsm03_tmp_01 b where a.gea00c = b.gsc00c and a.gea01c = b.gsc01c and b.gsc02c = 'ge' and ((gea86c between @fromdepart and @todepart) or @fromdepart = '*') and isnull(gea36c,'') <> '' --第七销售 insert #gsm03_tmp_gem01(gsc01c, gsc02c, gsc04c, gsc05f, gsc10d, gsc11c, gsc12i) select gea01c, 'ge', gsc04c, case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gea39f,0) * b.gsc05f / bill_total_amt end, gsc10d, gsc11c, gsc12i from #gem01_tmp a with (nolock), #gsm03_tmp_01 b where a.gea00c = b.gsc00c and a.gea01c = b.gsc01c and b.gsc02c = 'ge' and ((gea87c between @fromdepart and @todepart) or @fromdepart = '*') and isnull(gea38c,'') <> '' --第八销售 insert #gsm03_tmp_gem01(gsc01c, gsc02c, gsc04c, gsc05f, gsc10d, gsc11c, gsc12i) select gea01c, 'ge', gsc04c, case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gea41f,0) * b.gsc05f / bill_total_amt end, gsc10d, gsc11c, gsc12i from #gem01_tmp a with (nolock), #gsm03_tmp_01 b where a.gea00c = b.gsc00c and a.gea01c = b.gsc01c and b.gsc02c = 'ge' and ((gea88c between @fromdepart and @todepart) or @fromdepart = '*') and isnull(gea40c,'') <> '' --第九销售 insert #gsm03_tmp_gem01(gsc01c, gsc02c, gsc04c, gsc05f, gsc10d, gsc11c, gsc12i) select gea01c, 'ge', gsc04c, case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gea43f,0) * b.gsc05f / bill_total_amt end, gsc10d, gsc11c, gsc12i from #gem01_tmp a with (nolock), #gsm03_tmp_01 b where a.gea00c = b.gsc00c and a.gea01c = b.gsc01c and b.gsc02c = 'ge' and ((gea89c between @fromdepart and @todepart) or @fromdepart = '*') and isnull(gea42c,'') <> '' --第十销售 insert #gsm03_tmp_gem01(gsc01c, gsc02c, gsc04c, gsc05f, gsc10d, gsc11c, gsc12i) select gea01c, 'ge', gsc04c, case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gea45f,0) * b.gsc05f / bill_total_amt end, gsc10d, gsc11c, gsc12i from #gem01_tmp a with (nolock), #gsm03_tmp_01 b where a.gea00c = b.gsc00c and a.gea01c = b.gsc01c and b.gsc02c = 'ge' and ((gea89c between @fromdepart and @todepart) or @fromdepart = '*') and isnull(gea44c,'') <> '' insert #gsm03_tmp(gsc01c, gsc02c, gsc04c, gsc05f, gsc10d, gsc11c, gsc12i) select gsc01c, gsc02c, gsc04c, sum(gsc05f), gsc10d, gsc11c, gsc12i from #gsm03_tmp_gem01 group by gsc01c, gsc02c, gsc04c, gsc10d, gsc11c, gsc12i --End --删除临时表 drop table #gsm03_tmp_01 drop table #gsm03_tmp_gem01 --#gsm03_tmp --统计的中间结果 create table #tbl_stat_fill ( idd int identity not null, paymode varchar(10) not null, -- 支付方式 mode int null, -- 卡销售9, 0-存款 1-取款 5-欠款 6还款 amt float null default 0, -- 合计的金额 primary key(idd) ) create table #tbl_profit ( idd int identity not null, code varchar(20) not null, itemname varchar(60) not null, is_add int not null, amt float null default 0, is_leaf int not null, payment varchar(10) null, -- 支付方式 kind int null, -- 0 充值 1卡销售 9产品销售 2 服务 -1 取款 8 保证金收入 6 还款 primary key(idd) ) insert #tbl_stat_fill(paymode, mode, amt) select gsc04c, 9, isnull(sum(isnull(a.gsc05f, 0)), 0) from #gsm03_tmp a where a.gsc02c = 'gn' group by gsc04c insert #tbl_profit(code, itemname, is_add, is_leaf, amt) values('HB', '总客数', 0, 1, @customed_proj_count) insert #tbl_profit(code, itemname, is_add, is_leaf, amt) values('11', '收入', 1, 0, 0) insert #tbl_profit(code, itemname, is_add, is_leaf, amt, payment, kind) values('1101', '卡销售收入', @add_card_sale, 0, 0, '', 1) insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('110101', '卡销售收入(扣欠款)-现金', @add_card_sale, 1, '1', 1) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '110101' and b.paymode = '1' insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('110102', '卡销售收入(扣欠款)-银行卡', @add_card_sale, 1, '6', 1) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '110102' and b.paymode = '6' insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('1101Z1', '卡销售收入(扣欠款)-支付宝', @add_card_sale, 1, 'ZFB', 1) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '1101Z1' and b.paymode = 'ZFB' insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('1101W1', '卡销售收入(扣欠款)-微信', @add_card_sale, 1, 'WX', 1) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '1101W1' and b.paymode = 'WX' insert #tbl_profit(code, itemname, is_add, is_leaf) values('110103', '卡销售收入-买卡欠款', 0, 1) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '110103' and b.paymode = '5' update a set a.isnotcard = 1 from #gsm03_tmp a, #gcm10_888 b where a.gsc00c = b.compid and a.gsc01c = b.billid -- 如下为账户异动的处理 insert #tbl_stat_fill(paymode, mode, amt) select gsc04c, gsc12i, isnull(sum(isnull(a.gsc05f, 0)), 0) from #gsm03_tmp a where a.gsc02c = 'gz' and isnull(a.isnotcard, 0) = 0 group by gsc04c, gsc12i -- 卡充值 insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('110104', '卡销售收入-充值-现金(扣欠款)', @add_card_sale, 1, '1', 0) -- 卡充值-现金 update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '110104' and b.paymode = '1' and b.mode = 0 insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('110105', '卡销售收入-充值-银行卡', @add_card_sale, 1, '6', 0) -- 银行卡支付 update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '110105' and b.paymode = '6' and b.mode = 0 -- 考虑银行卡取款 update a set a.amt = isnull(a.amt, 0) + isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '110105' and b.paymode = '6' and b.mode = 1 insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('1101Z2', '卡销售收入-充值-支付宝', @add_card_sale, 1, 'ZFB', 0) -- 支付宝支付 update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '1101Z2' and b.paymode = 'ZFB' and b.mode = 0 -- 考虑支付宝取款 update a set a.amt = isnull(a.amt, 0) + isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '1101Z2' and b.paymode = 'ZFB' and b.mode = 1 insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('1101W2', '卡销售收入-充值-微信', @add_card_sale, 1, 'WX', 0) -- 支付宝支付 update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '1101W2' and b.paymode = 'WX' and b.mode = 0 -- 考虑支付宝取款 update a set a.amt = isnull(a.amt, 0) + isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '1101W2' and b.paymode = 'WX' and b.mode = 1 insert #tbl_profit(code, itemname, is_add, is_leaf) values('110106', '卡销售收入-充值欠款', 0, 1) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '110106' and b.paymode = '5' and b.mode = 0 -- 卡还款 insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('110107', '卡销售收入-还款-现金', @add_card_sale, 1, '1', 6) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '110107' and b.paymode = '1' and b.mode = 6 insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('110108', '卡销售收入-还款-银行卡', @add_card_sale, 1, '6', 6) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '110108' and b.paymode = '6' and b.mode = 6 insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('1101Z3', '卡销售收入-还款-支付宝', @add_card_sale, 1, 'ZFB', 6) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '1101Z3' and b.paymode = 'ZFB' and b.mode = 6 insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('1101W3', '卡销售收入-还款-微信', @add_card_sale, 1, 'WX', 6) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '1101W3' and b.paymode = 'WX' and b.mode = 6 -- 保证金转销售 declare @draw_preamt float select @draw_preamt = sum(amt) from #tbl_stat_fill where paymode = '&' insert #tbl_profit(code, itemname, is_add, is_leaf, amt, payment, kind) values('110109', '卡销售收入-保证金转销售', 0, 1, isnull(@draw_preamt, 0), '&', 0) -- 经理签单 select @itemtype = '8' select @itemname = gsb03c from gsm02 with (nolock) where gsb01c = '3' and gsb02c = @itemtype and gsb00c = @incompid insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('110110', '卡销售收入(扣欠款)-' + isnull(@itemname, ''), 0, 1, @itemtype, 1) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '110110' and b.paymode = @itemtype and b.mode = 9 insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('110111', '卡销售收入-充值-' + isnull(@itemname, ''), 0, 1, @itemtype, 0) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '110111' and b.paymode = @itemtype and b.mode = 0 insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('110112', '卡销售收入-还款-' + isnull(@itemname, ''), 0, 1, @itemtype, 6) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '110112' and b.paymode = @itemtype and b.mode = 6 -- 储值账户二, 按理应该不会有, 但yongqi作为转账了, 记得为0的删除就可以 select @itemtype = '9' select @itemname = gsb03c from gsm02 with (nolock) where gsb01c = '3' and gsb02c = @itemtype and gsb00c = @incompid insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('110115', '卡销售收入(扣欠款)-' + isnull(@itemname, ''), 0, 1, @itemtype, 1) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '110115' and b.paymode = @itemtype and b.mode = 9 -- 转账方式下的充值,也应该扣除欠款部分 insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('110116', '卡销售收入-充值-' + isnull(@itemname, '') + '(扣欠款)', 0, 1, @itemtype, 0) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '110116' and b.paymode = @itemtype and b.mode = 0 insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('110117', '卡销售收入-还款-' + isnull(@itemname, ''), 0, 1, @itemtype, 6) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '110117' and b.paymode = @itemtype and b.mode = 6 -- 会员卡异动收费, 就算是现金 insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('110118', '卡销售收入-异动收费', @add_card_sale, 1, '1', 0) --Start 异动收费 add by lm 20151014 --清除数据 delete from #gem01_tmp insert #gem01_tmp(gea00c, gea01c, gea07c, gea21c, gea27f, gea28c, gea29f, gea30c, gea31f, gea32c, gea33f, gea34c, gea35f, gea36c, gea37f, gea38c, gea39f, gea40c, gea41f, gea42c, gea43f, gea44c, gea45f, geb09i,gea81c,gea82c,gea83c,gea84c,gea85c,gea86c,gea87c,gea88c,gea89c,gea90c,bill_total_amt) select gea00c, gea01c, gea07c, gea21c, gea27f, gea28c, gea29f, gea30c, gea31f, gea32c, gea33f, gea34c, gea35f, gea36c, gea37f, gea38c, gea39f, gea40c, gea41f, gea42c, gea43f, gea44c, gea45f, geb09i,gea81c,gea82c,gea83c,gea84c,gea85c,gea86c,gea87c,gea88c,gea89c,gea90c, isnull(gea27f,0)+isnull(gea29f,0)+isnull(gea31f,0)+isnull(gea33f,0)+isnull(gea35f,0)+isnull(gea37f,0)+isnull(gea39f,0)+isnull(gea41f,0)+isnull(gea43f,0)+isnull(gea45f,0) from gem02 a with(nolock), gem01 b with(nolock),gam26 z with (nolock) where a.geb00c = b.gea00c and a.geb01c = b.gea01c and a.geb00c = z.gaz02c and z.gaz01c = @compid and gea80d between @fromdate and @todate --第一销售 select @amt = 0 select @amt = isnull(gea27f, 0) from #gem01_tmp a with (nolock) where a.geb09i = 1 and ((gea81c between @fromdepart and @todepart )or @fromdepart = '*') --add by lm 2015-10-13 and isnull(gea21c,'') <> '' --第二销售 select @amt = @amt + isnull(gea29f, 0) from #gem01_tmp a with (nolock) where a.geb09i = 1 and ((gea82c between @fromdepart and @todepart )or @fromdepart = '*') --add by lm 2015-10-13 and isnull(gea28c,'') <> '' --第三销售 select @amt = @amt + isnull(gea31f, 0) from #gem01_tmp a with (nolock) where a.geb09i = 1 and ((gea83c between @fromdepart and @todepart )or @fromdepart = '*') --add by lm 2015-10-13 and isnull(gea30c,'') <> '' --第四销售 select @amt = @amt + isnull(gea33f, 0) from #gem01_tmp a with (nolock) where a.geb09i = 1 and ((gea84c between @fromdepart and @todepart )or @fromdepart = '*') --add by lm 2015-10-13 and isnull(gea32c,'') <> '' --第五销售 select @amt = @amt + isnull(gea35f, 0) from #gem01_tmp a with (nolock) where a.geb09i = 1 and ((gea85c between @fromdepart and @todepart )or @fromdepart = '*') --add by lm 2015-10-13 and isnull(gea34c,'') <> '' --第六销售 select @amt = @amt + isnull(gea37f, 0) from #gem01_tmp a with (nolock) where a.geb09i = 1 and ((gea86c between @fromdepart and @todepart )or @fromdepart = '*') --add by lm 2015-10-13 and isnull(gea36c,'') <> '' --第七销售 select @amt = @amt + isnull(gea39f, 0) from #gem01_tmp a with (nolock) where a.geb09i = 1 and ((gea87c between @fromdepart and @todepart )or @fromdepart = '*') and isnull(gea38c,'') <> '' --第八销售 select @amt = @amt + isnull(gea41f, 0) from #gem01_tmp a with (nolock) where a.geb09i = 1 and ((gea88c between @fromdepart and @todepart )or @fromdepart = '*') and isnull(gea40c,'') <> '' --第九销售 select @amt = @amt + isnull(gea43f, 0) from #gem01_tmp a with (nolock) where a.geb09i = 1 and ((gea89c between @fromdepart and @todepart )or @fromdepart = '*') and isnull(gea42c,'') <> '' --第十销售 select @amt = @amt + isnull(gea45f, 0) from #gem01_tmp a with (nolock) where a.geb09i = 1 and ((gea90c between @fromdepart and @todepart )or @fromdepart = '*') and isnull(gea44c,'') <> '' --第一销售 select @amt2 = isnull(gea27f, 0) from #gem01_tmp a with (nolock) where a.geb09i = 1 and ((gea81c between @fromdepart and @todepart )or @fromdepart = '*') and isnull(gea21c,'') <> '' --第二销售 select @amt2 = @amt2 + isnull(gea29f, 0) from #gem01_tmp a with (nolock) where a.geb09i = 1 and ((gea82c between @fromdepart and @todepart )or @fromdepart = '*') and isnull(gea28c,'') <> '' --第三销售 select @amt2 = @amt2 + isnull(gea31f, 0) from #gem01_tmp a with (nolock) where a.geb09i = 1 and ((gea83c between @fromdepart and @todepart )or @fromdepart = '*') and isnull(gea30c,'') <> '' --第四销售 select @amt2 = @amt2 + isnull(gea33f, 0) from #gem01_tmp a with (nolock) where a.geb09i = 1 and ((gea84c between @fromdepart and @todepart )or @fromdepart = '*') and isnull(gea32c,'') <> '' --第五销售 select @amt2 = @amt2 + isnull(gea35f, 0) from #gem01_tmp a with (nolock) where a.geb09i = 1 and ((gea85c between @fromdepart and @todepart )or @fromdepart = '*') and isnull(gea34c,'') <> '' --第六销售 select @amt2 = @amt2 + isnull(gea37f, 0) from #gem01_tmp a with (nolock) where a.geb09i = 1 and ((gea86c between @fromdepart and @todepart )or @fromdepart = '*') and isnull(gea36c,'') <> '' --第七销售 select @amt2 = @amt2 + isnull(gea39f, 0) from #gem01_tmp a with (nolock) where a.geb09i = 1 and ((gea87c between @fromdepart and @todepart )or @fromdepart = '*') and isnull(gea38c,'') <> '' --第八销售 select @amt2 = @amt2 + isnull(gea41f, 0) from #gem01_tmp a with (nolock) where a.geb09i = 1 and ((gea88c between @fromdepart and @todepart )or @fromdepart = '*') and isnull(gea40c,'') <> '' --第九销售 select @amt2 = @amt2 + isnull(gea43f, 0) from #gem01_tmp a with (nolock) where a.geb09i = 1 and ((gea89c between @fromdepart and @todepart )or @fromdepart = '*') and isnull(gea42c,'') <> '' --第十销售 select @amt2 = @amt2 + isnull(gea45f, 0) from #gem01_tmp a with (nolock) where a.geb09i = 1 and ((gea90c between @fromdepart and @todepart )or @fromdepart = '*') and isnull(gea44c,'') <> '' --End -- select @amt2 = sum(isnull(a.geb10f, 0)) -- from gem02 a with (nolock), gem01 b with (nolock), gam26 c with (nolock) -- where a.geb00c = c.gaz02c -- and a.geb00c = b.gea00c -- and a.geb01c = b.gea01c -- and b.gea80d >= @fromdate -- and b.gea80d <= @todate -- and geb09i = 2 -- and b.gea95c like @staff -- and c.gaz01c = @incompid -- and (@fromdepart = '*' or b.gea81c between @fromdepart and @todepart) update #tbl_profit set amt = isnull(@amt, 0) - isnull(@amt2, 0) where code = '110118' -- 现金取款, 取款kind定义为-1 insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('110119', '卡销售收入-取款-现金', @add_card_sale, 1, '1', -1) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '110119' and b.paymode = '1' and b.mode = 1 -- 内调, 记得为0的删除就可以 select @itemtype = '0' select @itemname = gsb03c from gsm02 with (nolock) where gsb01c = '3' and gsb02c = @itemtype and gsb00c = @incompid insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('110120', '卡销售收入(扣欠款)-' + isnull(@itemname, ''), @isadd_0 * @add_card_sale, 1, @itemtype, 1) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '110120' and b.paymode = @itemtype and b.mode = 9 insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('110121', '卡销售收入-充值-' + isnull(@itemname, ''), @isadd_0 * @add_card_sale, 1, @itemtype, 0) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '110121' and b.paymode = @itemtype and b.mode = 0 insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('110122', '卡销售收入-还款-' + isnull(@itemname, ''), @isadd_0 * @add_card_sale, 1, @itemtype, 6) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '110122' and b.paymode = @itemtype and b.mode = 6 insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('110123', '卡销售收入-取款-' + isnull(@itemname, ''), @isadd_0 * @add_card_sale, 1, @itemtype, -1) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '110123' and b.paymode = @itemtype and b.mode = 1 --start 取款其他支付方式 add by lm 2016.06.22 select @amt = 0 select @amt = sum(amt) from #tbl_stat_fill where mode = 1 and paymode <> '1' and paymode <> '0' insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind, amt) values('drawother', '卡销售收入-取款-其他支付方式', @isadd_0 * @add_card_sale, 1, @itemtype, -1, @amt) --end -- 抵用券, 记得为0的删除就可以 select @itemtype = '3' select @itemname = gsb03c from gsm02 with (nolock) where gsb01c = '3' and gsb02c = @itemtype and gsb00c = @incompid insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('110125', '卡销售收入(扣欠款)-' + isnull(@itemname, ''), @isadd_3 * @add_card_sale, 1, @itemtype, 1) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '110125' and b.paymode = @itemtype and b.mode = 9 insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('110126', '卡销售收入-充值-' + isnull(@itemname, ''), @isadd_3 * @add_card_sale, 1, @itemtype, 0) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '110126' and b.paymode = @itemtype and b.mode = 0 insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('110127', '卡销售收入-还款-' + isnull(@itemname, ''), @isadd_3 * @add_card_sale, 1, @itemtype, 6) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '110127' and b.paymode = @itemtype and b.mode = 6 -- 抵用券B, 记得为0的删除就可以 select @itemtype = '$' select @itemname = gsb03c from gsm02 with (nolock) where gsb01c = '3' and gsb02c = @itemtype and gsb00c = @incompid insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('110130', '卡销售收入(扣欠款)-' + isnull(@itemname, ''), @isadd_$ * @add_card_sale, 1, @itemtype, 1) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '110130' and b.paymode = @itemtype and b.mode = 9 insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('110131', '卡销售收入-充值-' + isnull(@itemname, ''), @isadd_$ * @add_card_sale, 1, @itemtype, 0) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '110131' and b.paymode = @itemtype and b.mode = 0 insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('110132', '卡销售收入-还款-' + isnull(@itemname, ''), @isadd_$ * @add_card_sale, 1, @itemtype, 6) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '110132' and b.paymode = @itemtype and b.mode = 6 -- 自己增加的$开头的支付方式 declare @pay_$ varchar(10) declare @code_char varchar(10) declare @isadd_ur float -- 子定义的支付方式是为计入营业收入 declare @code_int int select @code_int = 110150 declare cur_each_pay cursor for select gsb02c from gsm02 with (nolock) where gsb00c = @incompid and gsb01c = '3' and gsb02c like '$%' and len(gsb02c) > 1 order by gsb02c open cur_each_pay fetch cur_each_pay into @pay_$ while @@fetch_status = 0 begin select @code_char = convert(varchar(10), @code_int) select @itemtype = @pay_$ select @itemname = gsb03c from gsm02 with (nolock) where gsb01c = '3' and gsb02c = @itemtype and gsb00c = @incompid select @isadd_ur = gso07f from gsm15 with (nolock) where gso00c = @incompid and gso02c = @pay_$ select @isadd_ur = isnull(@isadd_ur, 1) -- 默认算收入 insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values(@code_char, '卡销售收入(扣欠款)-' + isnull(@itemname, ''), @isadd_ur * @add_card_sale, 1, @pay_$, 1) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = @code_char and b.paymode = @itemtype and b.mode = 9 select @code_int = @code_int + 1 select @code_char = convert(varchar(10), @code_int) insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values(@code_char, '卡销售收入-充值-' + isnull(@itemname, ''), @isadd_ur * @add_card_sale, 1, @pay_$, 0) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = @code_char and b.paymode = @itemtype and b.mode = 0 select @code_int = @code_int + 1 select @code_char = convert(varchar(10), @code_int) insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values(@code_char, '卡销售收入-还款-' + isnull(@itemname, ''), @isadd_ur * @add_card_sale, 1, @pay_$, 6) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = @code_char and b.paymode = @itemtype and b.mode = 6 select @code_int = @code_int + 1 select @code_char = convert(varchar(10), @code_int) fetch cur_each_pay into @pay_$ end close cur_each_pay deallocate cur_each_pay -- 积分买卡,只有在充值的时候可能存在,也无法作为卡销售的收入 select @itemtype = '7' select @itemname = gsb03c from gsm02 with (nolock) where gsb01c = '3' and gsb02c = @itemtype and gsb00c = @incompid insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('110133', '卡销售收入-充值-' + isnull(@itemname, ''), 0, 1, @itemtype, 0) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '110133' and b.paymode = @itemtype and b.mode = 0 -- 卡的变更等收入,也属于卡的销售收入 delete #tbl_stat_fill insert #tbl_stat_fill(paymode, amt) select gsc04c, isnull(sum(isnull(a.gsc05f, 0)), 0) from #gsm03_tmp a where a.gsc02c = 'ge' group by gsc04c insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('110140', '卡销售收入-变更-现金', @add_card_sale, 1, '1', 0) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '110140' and b.paymode = '1' insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('110141', '卡销售收入-变更-银行卡', @add_card_sale, 1, '6', 0) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '110141' and b.paymode = '6' insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('1101Z4', '卡销售收入-变更-支付宝', @add_card_sale, 1, 'ZFB', 0) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '1101Z4' and b.paymode = 'ZFB' insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('110145', '卡销售收入-变更-欠款', @add_card_sale, 0, '5', 0) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '110145' and b.paymode = '5' insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('1101W4', '卡销售收入-变更-微信', @add_card_sale, 1, 'WX', 0) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '1101W4' and b.paymode = 'WX' --Start 卡种变更所有支付方式 add by lm 2015.12.28 set @id = 0 set @code_no = '' create table #gsm02 ( pay varchar(10) not null, payname varchar(40) null ) insert #gsm02 (pay, payname) select gsb02c, gsb03c from gsm02 a with(nolock) where a.gsb00c = @incompid and a.gsb01c = '3' and a.gsb02c not in ('ZFB', 'WX', '1', '6', '5') while (@id = 0) begin if exists(select 1 from #gsm02) begin select top 1 @itemtype = pay, @itemname = payname from #gsm02 select @code_no = '110140' + @itemtype insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values(@code_no, '卡销售收入-变更-' + isnull(@itemname, ''), @add_card_sale, 1, @itemtype, 0) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = @code_no and b.paymode = @itemtype set @code_no = '' set @id = 0 delete from #gsm02 where pay = @itemtype end else begin set @id = 1 end end drop table #gsm02 -- 统计卡销售收入 select @amt = 0 if @calmode = 0 begin select @amt = sum(isnull(amt * is_add, 0)) from #tbl_profit where code like '1101%' and is_leaf = 1 end else begin select @amt = sum(isnull(amt, 0)) from #tbl_profit where code like '1101%' and is_leaf = 1 and (isnull(payment, '') <> '&' and isnull(payment, '') <> '7' and isnull(payment, '') <> '') end update #tbl_profit set amt = isnull(@amt, 0) where code = '1101' delete #tbl_stat_fill -- 服务收入, 包括产品部分 create table #tbl_stat_fill_2 ( idd int identity not null, paymode varchar(10) null, mode int null, amt float null, primary key(idd) ) insert #tbl_stat_fill_2(paymode, mode, amt) select gsc04c, 0, isnull(sum(isnull(a.gsc05f, 0)), 0) from #gsm03_tmp a where a.gsc02c = 'gx' group by gsc04c insert #tbl_stat_fill_2(paymode, mode, amt) select gsc04c, 1, isnull(sum(isnull(a.gsc05f, 0)), 0) from #gsm03_tmp a where a.gsc02c = 'gl' group by gsc04c insert #tbl_stat_fill(paymode, mode, amt) select isnull(paymode, ''), 0, sum(isnull(amt, 0)) from #tbl_stat_fill_2 group by isnull(paymode, '') drop table #tbl_stat_fill_2 insert #tbl_profit(code, itemname, is_add, is_leaf) values('1102', '服务收入(包括产品销售)', 1, 0) insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('110201', '服务收入-现金', 1, 1, '1', 2) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '110201' and b.paymode = '1' insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('110202', '服务收入-银行卡', 1, 1, '6', 2) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '110202' and b.paymode = '6' insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('1102Z1', '服务收入-支付宝', 1, 1, 'ZFB', 2) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '1102Z1' and b.paymode = 'ZFB' insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('1102W1', '服务收入-微信', 1, 1, 'WX', 2) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '1102W1' and b.paymode = 'WX' -- 经理签单 select @itemtype = '8' select @itemname = gsb03c from gsm02 with (nolock) where gsb01c = '3' and gsb02c = @itemtype and gsb00c = @incompid insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('110203', '服务收入-' + isnull(@itemname, ''), 0, 1, @itemtype, 2) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '110203' and b.paymode = @itemtype -- 抵用券 select @itemtype = '3' select @itemname = gsb03c from gsm02 with (nolock) where gsb01c = '3' and gsb02c = @itemtype and gsb00c = @incompid insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('110204', '服务收入-' + isnull(@itemname, ''), @isadd_3, 1, @itemtype, 2) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '110204' and b.paymode = @itemtype -- 第二抵用券 select @itemtype = '$' select @itemname = gsb03c from gsm02 with (nolock) where gsb01c = '3' and gsb02c = @itemtype and gsb00c = @incompid insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('110208', '服务收入-' + isnull(@itemname, ''), @isadd_$, 1, @itemtype, 2) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '110208' and b.paymode = @itemtype select @code_int = 110250 declare cur_each_pay_2 cursor for select gsb02c from gsm02 with (nolock) where gsb00c = @incompid and gsb01c = '3' and gsb02c like '$%' and len(gsb02c) > 1 order by gsb02c open cur_each_pay_2 fetch cur_each_pay_2 into @pay_$ while @@fetch_status = 0 begin select @code_char = convert(varchar(10), @code_int) select @itemtype = @pay_$ select @itemname = gsb03c from gsm02 with (nolock) where gsb01c = '3' and gsb02c = @itemtype and gsb00c = @incompid select @isadd_ur = gso07f from gsm15 with (nolock) where gso00c = @incompid and gso02c = @pay_$ select @isadd_ur = isnull(@isadd_ur, 1) -- 默认算收入 insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values(@code_char, '服务收入-' + isnull(@itemname, ''), @isadd_ur, 1, @pay_$, 2) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = @code_char and b.paymode = @itemtype select @code_int = @code_int + 1 select @code_char = convert(varchar(10), @code_int) fetch cur_each_pay_2 into @pay_$ end close cur_each_pay_2 deallocate cur_each_pay_2 -- 内调 select @itemtype = '0' select @itemname = gsb03c from gsm02 with (nolock) where gsb01c = '3' and gsb02c = @itemtype and gsb00c = @incompid insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('110209', '服务收入-' + isnull(@itemname, ''), @isadd_0, 1, @itemtype, 2) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '110209' and b.paymode = @itemtype insert #tbl_profit(code, itemname, is_add, is_leaf) values('110207', '服务收入-欠款', 0, 1) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '110207' and b.paymode = '5' -- 服务收入-取用储值 insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('110205', '服务收入-取用储值', @add_card_svr, 0, 'CARD', 2) -- 把明细的服务项目的卡付计算出来 create table #tbl_card_pay ( paycode varchar(20) not null, acctid int null, amt float null, code varchar(20) null, pay_name varchar(30) null, primary key(paycode) ) insert #tbl_card_pay(paycode, amt) select paymode, sum(amt) from #tbl_stat_fill where (paymode = '#' or paymode = '2' or paymode = '4' or paymode = '9' or (paymode >= 'A' and paymode <= 'Z')) -- 不包括积分 and paymode not in ('WX','ZFB') group by paymode update a set a.acctid = b.gsn02c from #tbl_card_pay a, gsm14 b with (nolock) where a.paycode = b.gsn01c update a set a.code = right('00' + paycode, 2) from #tbl_card_pay a update a set a.pay_name = b.gsb03c from #tbl_card_pay a, gsm02 b with (nolock) where a.paycode = b.gsb02c and gsb00c = @incompid and gsb01c = '3' insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind, amt) select '110205' + isnull(code, '00'), '服务收入-取用储值-' + pay_name, @add_card_svr, 1, paycode, 2, amt from #tbl_card_pay drop table #tbl_card_pay -- 积分需要单独列出来 declare @act varchar(20) declare @act_name varchar(50) select @act = '7' -- 积分的支付 select @act_name = gsb03c from gsm02 with (nolock) where gsb00c = @incompid and gsb01c = '3' and gsb02c = @act insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('11020507', '服务收入-积分支付-' + isnull(@act_name, ''), 0, 1, @act, 2) -- 积分永远不作为服务收入 is_add = 0 update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '11020507' and b.paymode = @act -- 合计所有取用储值, 把积分支付单独扣除 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit where (code like '110205%' and code <> '11020507') and is_leaf = 1 update #tbl_profit set amt = isnull(@amt, 0) where code = '110205' -- START -- 统计因为项目或产品销售的欠款的还款收入,只统计现金还款及银行卡的还款 -- 服务中欠款的可能行很小,所以默认为产品的还款 select @amt = 0 select @amt = isnull(sum(isnull(a.gsc05f, 0)), 0) from #gsm03_tmp a where a.gsc02c = 'gz' and isnull(a.isnotcard, 0) = 1 and gsc04c = '1' and gsc12i = 6 insert #tbl_profit(code, itemname, is_add, is_leaf, amt, payment, kind) values('110230', '服务收入-还款-现金', 1, 1, @amt, '1', 2) select @amt = 0 select @amt = isnull(sum(isnull(a.gsc05f, 0)), 0) from #gsm03_tmp a where a.gsc02c = 'gz' and isnull(a.isnotcard, 0) = 1 and gsc04c = '6' and gsc12i = 6 insert #tbl_profit(code, itemname, is_add, is_leaf, amt, payment, kind) values('110231', '服务收入-还款-银行卡', 1, 1, @amt, '6', 2) select @amt = 0 select @amt = isnull(sum(isnull(a.gsc05f, 0)), 0) from #gsm03_tmp a where a.gsc02c = 'gz' and isnull(a.isnotcard, 0) = 1 and gsc04c = 'ZFB' and gsc12i = 6 insert #tbl_profit(code, itemname, is_add, is_leaf, amt, payment, kind) values('1102Z3', '服务收入-还款-支付宝', 1, 1, @amt, 'ZFB', 2) select @amt = 0 select @amt = isnull(sum(isnull(a.gsc05f, 0)), 0) from #gsm03_tmp a where a.gsc02c = 'gz' and isnull(a.isnotcard, 0) = 1 and gsc04c = 'WX' and gsc12i = 6 insert #tbl_profit(code, itemname, is_add, is_leaf, amt, payment, kind) values('1102W3', '服务收入-还款-微信', 1, 1, @amt, 'WX', 2) --Start 统计还款所有支付方式 add by lm 2015.12.28 set @id = 0 set @code_no = '' create table #gsm02_1 ( pay varchar(10) not null, payname varchar(40) null ) insert #gsm02_1 (pay, payname) select gsb02c, gsb03c from gsm02 a with(nolock) where a.gsb00c = @incompid and a.gsb01c = '3' and a.gsb02c not in ('1','6','ZFB','WX') while (@id = 0) begin if exists(select 1 from #gsm02_1) begin select top 1 @itemtype = pay, @itemname = payname from #gsm02_1 select @code_no = '110231' + @itemtype select @amt = 0 select @amt = isnull(sum(isnull(a.gsc05f, 0)), 0) from #gsm03_tmp a where a.gsc02c = 'gz' and isnull(a.isnotcard, 0) = 1 and gsc04c = @itemtype and gsc12i = 6 select @isadd_ur = gso07f from gsm15 with(nolock) where gso00c = @incompid and gso02c = @itemtype select @isadd_ur = isnull(@isadd_ur, 1) -- 默认算收入 insert #tbl_profit(code, itemname, is_add, is_leaf, amt, payment, kind) values(@code_no, '服务收入-还款(只计算还款)-' + @itemname, @isadd_ur, 1, @amt, @itemtype, 6) set @code_no = '' set @id = 0 delete from #gsm02_1 where pay = @itemtype end else begin set @id = 1 end end drop table #gsm02_1 -- end -- 必须要的数据 -- 这个地方不需要判断是哪个部门的因为可以在下面的链接中在重新判断 -- 为了提高性能,把需要用倒的ggm01数据转移到临时表中 create table #ggm01_1 ( gga00c varchar(20) not null, gga01c varchar(20) not null, gga93c varchar(20) null, gga07c varchar(10) null, --会员卡类别 ggb34f float null, --实际营业额折算率 ggc30f float null --实际营业额折算比率 primary key(gga00c, gga01c) ) insert #ggm01_1(gga00c, gga01c, gga93c, gga07c) select gga00c, gga01c, gga93c, gga07c from ggm01 a with (nolock), gam26 z with(nolock) where gga00c = z.gaz02c and gga80d >= @fromdate and gga80d <= @todate and z.gaz01c = @incompid update a set a.ggb34f = b.gak22f, a.ggc30f = b.gak24f from #ggm01_1 a, gam10 b with(nolock) where b.gak00c = a.gga00c and b.gak01c = a.gga07c update a set a.ggb34f = 1 from #ggm01_1 a, ggm02 b with(nolock) where b.ggb00c = a.gga00c and b.ggb01c = a.gga01c and b.ggb27c <> '4' update a set a.ggc30f = 1 from #ggm01_1 a, ggm03 b with(nolock) where b.ggc00c = a.gga00c and b.ggc01c = a.gga01c and b.ggc27c <> '4' -- 在按项目支付的情况下,合计卡付的实际营业额 if (isnull(@sp055f, 0) = 0) begin insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('110206', '服务收入-卡付实际营业额', 0, 0, '', 2) if(@sp215f = 1) --add by lm 2015-09-16 begin select @amt = 0 select @amt = sum(isnull(isnull(ggb11f, 0) * isnull(gso03i, 1), 0)) --* isnull(a.ggb34f, 1) from ggm02 b with (nolock), #ggm01_1 a, gsm15 c with (nolock) where b.ggb00c = a.gga00c and b.ggb01c = a.gga01c and b.ggb00c = c.gso00c and b.ggb27c = c.gso02c and c.gso01c = '3' and c.gso09f = 1 and (b.ggb81c between @fromdepart and @todepart or (@fromdepart='*')) and ((ggb27c >= 'A' and ggb27c <= 'Z') or ggb27c = '#' or ggb27c = '9' or ggb27c = '2' ) and ggb27c not in ('WX','ZFB') select @gak22f = 0 select @gak22f = sum(isnull(isnull(ggb11f, 0) * isnull(gso03i, 1) * isnull(gak22f, 1), 0)) from ggm02 b with (nolock), #ggm01_1 a, gsm15 c with (nolock), gam10 d with(nolock) where b.ggb00c = a.gga00c and b.ggb01c = a.gga01c and b.ggb00c = c.gso00c and b.ggb27c = c.gso02c and d.gak00c = a.gga00c and d.gak01c = a.gga07c and (b.ggb81c between @fromdepart and @todepart or (@fromdepart='*')) and c.gso01c = '3' and c.gso09f = 1 and ggb27c = '4' --第一销售 select @amt2 = 0 select @amt2 = sum(isnull(isnull(ggc11f, 0) * isnull(gso06f, 1) * isnull(a.ggc30f, 1) * isnull(ggc14f,0), 0)) from ggm03 b with (nolock), #ggm01_1 a, gsm15 c with (nolock) where b.ggc00c = a.gga00c and b.ggc01c = a.gga01c and b.ggc00c = c.gso00c and b.ggc27c = c.gso02c and (b.ggc81c between @fromdepart and @todepart or (@fromdepart='*')) and c.gso01c = '3' and c.gso10f = 1 and ((ggc27c >= 'A' and ggc27c <= 'Z') or ggc27c = '#' or ggc27c = '9' or ggc27c = '2' ) and ggc27c not in ('WX','ZFB') select @gak24f = 0 select @gak24f = sum(isnull(isnull(ggc11f, 0) * isnull(gso06f, 1) * isnull(gak24f, 1) * isnull(ggc14f,0), 0)) from ggm03 b with (nolock), #ggm01_1 a, gsm15 c with (nolock),gam10 d with(nolock) where b.ggc00c = a.gga00c and b.ggc01c = a.gga01c and b.ggc00c = c.gso00c and b.ggc27c = c.gso02c and d.gak00c = a.gga00c and d.gak01c = a.gga07c and (b.ggc81c between @fromdepart and @todepart or (@fromdepart='*')) and c.gso01c = '3' and c.gso10f = 1 and ggc27c = '4' --第二销售 select @amt2 = isnull(@amt2,0 ) + sum(isnull(isnull(ggc11f, 0) * isnull(gso06f, 1) * isnull(a.ggc30f, 1) * isnull(ggc16f,0), 0)) from ggm03 b with (nolock), #ggm01_1 a, gsm15 c with (nolock) where b.ggc00c = a.gga00c and b.ggc01c = a.gga01c and b.ggc00c = c.gso00c and b.ggc27c = c.gso02c and (b.ggc82c between @fromdepart and @todepart or (@fromdepart='*')) and c.gso01c = '3' and c.gso10f = 1 and ((ggc27c >= 'A' and ggc27c <= 'Z') or ggc27c = '#' or ggc27c = '9' or ggc27c = '2' ) and ggc27c not in ('WX','ZFB') select @gak24f = isnull(@gak24f,0) + isnull(sum(isnull(isnull(ggc11f, 0) * isnull(gso06f, 1) * isnull(gak24f, 1) * isnull(ggc16f,0), 0)),0) from ggm03 b with (nolock), #ggm01_1 a, gsm15 c with (nolock),gam10 d with(nolock) where b.ggc00c = a.gga00c and b.ggc01c = a.gga01c and b.ggc00c = c.gso00c and b.ggc27c = c.gso02c and d.gak00c = a.gga00c and d.gak01c = a.gga07c and (b.ggc82c between @fromdepart and @todepart or (@fromdepart='*')) and c.gso01c = '3' and c.gso10f = 1 and ggc27c = '4' --第三销售 select @amt2 = isnull(@amt2,0) + sum(isnull(isnull(ggc11f, 0) * isnull(gso06f, 1) * isnull(a.ggc30f, 1) * isnull(ggc18f,0), 0)) from ggm03 b with (nolock), #ggm01_1 a, gsm15 c with (nolock) where b.ggc00c = a.gga00c and b.ggc01c = a.gga01c and b.ggc00c = c.gso00c and b.ggc27c = c.gso02c and (b.ggc83c between @fromdepart and @todepart or (@fromdepart='*')) and c.gso01c = '3' and c.gso10f = 1 and ((ggc27c >= 'A' and ggc27c <= 'Z') or ggc27c = '#' or ggc27c = '9' or ggc27c = '2' ) and ggc27c not in ('WX','ZFB') select @gak24f = isnull(@gak24f,0) + isnull(sum(isnull(isnull(ggc11f, 0) * isnull(gso06f, 1) * isnull(gak24f, 1) * isnull(ggc18f,0), 0)),0) from ggm03 b with (nolock), #ggm01_1 a, gsm15 c with (nolock),gam10 d with(nolock) where b.ggc00c = a.gga00c and b.ggc01c = a.gga01c and b.ggc00c = c.gso00c and b.ggc27c = c.gso02c and d.gak00c = a.gga00c and d.gak01c = a.gga07c and (b.ggc83c between @fromdepart and @todepart or (@fromdepart='*')) and c.gso01c = '3' and c.gso10f = 1 and ggc27c = '4' select @amt2 = isnull(@amt2,0) +isnull(@gak24f,0) select @amt =isnull(@amt,0) +isnull(@gak22f,0) end else begin create table #gcm11 --会员疗程充值 ( gcm00c varchar(10) not null, --门店编号 gcm01c varchar(20) not null --异动单号 ) create table #gcm11_card --会员疗程充值(卡付) ( gcm00c varchar(10) not null, --门店编号 gcm01c varchar(20) not null, --异动单号 cardamt float null ) insert #gcm11(gcm00c, gcm01c) select distinct gcm00c,gcm01c from gcm11 a with(nolock), #dept_bills b --, gam26 z with(nolock) where a.gcm00c = b.compid and a.gcm01c = b.billno -- and a.gcm00c = z.gaz02c -- and z.gaz01c = @incompid and (b.billtype = 'gz') and b.payway in (select pay from pay_classify where comp = @incompid and category = '2' ) insert #gcm11_card(gcm00c, gcm01c, cardamt) select gcm00c, gcm01c, isnull(gcm16f * ratio,0) from gcm11 a with(nolock), #dept_bills b where a.gcm00c = b.compid and a.gcm01c = b.billno and b.billtype = 'gz' and a.gcm15c = b.payway and a.gcm15c in (select pay from pay_classify where comp = @incompid and category = '2') and isnull(a.gcm15c,'') <> '' union all select gcm00c, gcm01c, isnull(gcm18f * ratio,0) from gcm11 a with(nolock), #dept_bills b where a.gcm00c = b.compid and a.gcm01c = b.billno and b.billtype = 'gz' and a.gcm17c = b.payway and a.gcm17c in (select pay from pay_classify where comp = @incompid and category = '2') and isnull(a.gcm17c,'') <> '' union all select gcm00c, gcm01c, isnull(gcm20f * ratio,0) from gcm11 a with(nolock), #dept_bills b where a.gcm00c = b.compid and a.gcm01c = b.billno and b.billtype = 'gz' and a.gcm19c = b.payway and a.gcm19c in (select pay from pay_classify where comp = @incompid and category = '2') and isnull(a.gcm19c,'') <> '' select @amt = 0 select @amt = sum(isnull(isnull(ggb11f, 0) * isnull(gso03i, 1), 0)) --* isnull(a.ggb34f, 1) from ggm02 b with (nolock), #ggm01_1 a, gsm15 c with (nolock) where b.ggb00c = a.gga00c and b.ggb01c = a.gga01c and b.ggb00c = c.gso00c and b.ggb27c = c.gso02c and c.gso01c = '3' and c.gso09f = 1 and (b.ggb81c between @fromdepart and @todepart or (@fromdepart='*')) and ((ggb27c >= 'A' and ggb27c <= 'Z') or ggb27c = '#' or ggb27c = '2' ) and ggb27c not in ('WX','ZFB') select @gak22f = 0 select @gak22f = sum(isnull(isnull(ggb11f, 0) * isnull(gso03i, 1) * isnull(gak22f, 1), 0)) from ggm02 b with (nolock), #ggm01_1 a, gsm15 c with (nolock), gam10 d with(nolock) where b.ggb00c = a.gga00c and b.ggb01c = a.gga01c and b.ggb00c = c.gso00c and b.ggb27c = c.gso02c and d.gak00c = a.gga00c and d.gak01c = a.gga07c and (b.ggb81c between @fromdepart and @todepart or (@fromdepart='*')) and c.gso01c = '3' and c.gso09f = 1 and ggb27c = '4' --第一销售 select @amt2 = 0 select @amt2 = sum(isnull(isnull(ggc11f, 0) * isnull(gso06f, 1) * isnull(a.ggc30f, 1), 0)) from ggm03 b with (nolock), #ggm01_1 a, gsm15 c with (nolock) where b.ggc00c = a.gga00c and b.ggc01c = a.gga01c and b.ggc00c = c.gso00c and b.ggc27c = c.gso02c and (b.ggc81c between @fromdepart and @todepart or (@fromdepart='*')) and c.gso01c = '3' and c.gso10f = 1 and ((ggc27c >= 'A' and ggc27c <= 'Z') or ggc27c = '#' or ggc27c = '2' ) and ggc27c not in ('WX','ZFB') select @gak24f = 0 select @gak24f = sum(isnull(isnull(ggc11f, 0) * isnull(gso06f, 1) * isnull(gak24f, 1), 0)) from ggm03 b with (nolock), #ggm01_1 a, gsm15 c with (nolock),gam10 d with(nolock) where b.ggc00c = a.gga00c and b.ggc01c = a.gga01c and b.ggc00c = c.gso00c and b.ggc27c = c.gso02c and d.gak00c = a.gga00c and d.gak01c = a.gga07c and (b.ggc81c between @fromdepart and @todepart or (@fromdepart='*')) and c.gso01c = '3' and c.gso10f = 1 and ggc27c = '4' --第二销售 select @amt2 = isnull(@amt2,0 ) + sum(isnull(isnull(ggc11f, 0) * isnull(gso06f, 1) * isnull(a.ggc30f, 1), 0)) from ggm03 b with (nolock), #ggm01_1 a, gsm15 c with (nolock) where b.ggc00c = a.gga00c and b.ggc01c = a.gga01c and b.ggc00c = c.gso00c and b.ggc27c = c.gso02c and (b.ggc82c between @fromdepart and @todepart or (@fromdepart='*')) and c.gso01c = '3' and c.gso10f = 1 and ((ggc27c >= 'A' and ggc27c <= 'Z') or ggc27c = '#' or ggc27c = '2' ) and ggc27c not in ('WX','ZFB') select @gak24f = isnull(@gak24f,0) + sum(isnull(isnull(ggc11f, 0) * isnull(gso06f, 1) * isnull(gak24f, 1), 0)) from ggm03 b with (nolock), #ggm01_1 a, gsm15 c with (nolock),gam10 d with(nolock) where b.ggc00c = a.gga00c and b.ggc01c = a.gga01c and b.ggc00c = c.gso00c and b.ggc27c = c.gso02c and d.gak00c = a.gga00c and d.gak01c = a.gga07c and (b.ggc82c between @fromdepart and @todepart or (@fromdepart='*')) and c.gso01c = '3' and c.gso10f = 1 and ggc27c = '4' --第三销售 select @amt2 = isnull(@amt2,0) + sum(isnull(isnull(ggc11f, 0) * isnull(gso06f, 1) * isnull(a.ggc30f, 1), 0)) from ggm03 b with (nolock), #ggm01_1 a, gsm15 c with (nolock) where b.ggc00c = a.gga00c and b.ggc01c = a.gga01c and b.ggc00c = c.gso00c and b.ggc27c = c.gso02c and (b.ggc83c between @fromdepart and @todepart or (@fromdepart='*')) and c.gso01c = '3' and c.gso10f = 1 and ((ggc27c >= 'A' and ggc27c <= 'Z') or ggc27c = '#' or ggc27c = '2' ) and ggc27c not in ('WX','ZFB') select @gak24f = isnull(@gak24f,0) + sum(isnull(isnull(ggc11f, 0) * isnull(gso06f, 1) * isnull(gak24f, 1), 0)) from ggm03 b with (nolock), #ggm01_1 a, gsm15 c with (nolock),gam10 d with(nolock) where b.ggc00c = a.gga00c and b.ggc01c = a.gga01c and b.ggc00c = c.gso00c and b.ggc27c = c.gso02c and d.gak00c = a.gga00c and d.gak01c = a.gga07c and (b.ggc83c between @fromdepart and @todepart or (@fromdepart='*')) and c.gso01c = '3' and c.gso10f = 1 and ggc27c = '4' select @amt2 = isnull(@amt2,0) +isnull(@gak24f,0) select @amt =isnull(@amt,0) +isnull(@gak22f,0) select @amt3 = 0 select @amt3 = sum(cardamt) from #gcm11_card drop table #gcm11_card drop table #gcm11 end update #tbl_profit set amt = isnull(@amt, 0) + isnull(@amt2, 0) + isnull(@amt3, 0) where code = '110206' insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('11AA01', '服务收入-卡付实际劳动业绩', 0, 0, '', 2) select @amt = 0 select @amt = sum(isnull(isnull(ggb11f, 0) * isnull(gso03i, 1), 0)) --* isnull(a.ggb34f, 1) from ggm02 b with (nolock), #ggm01_1 a, gsm15 c with (nolock) where b.ggb00c = a.gga00c and b.ggb01c = a.gga01c and b.ggb00c = c.gso00c and b.ggb27c = c.gso02c and c.gso01c = '3' and c.gso09f = 1 and (b.ggb81c between @fromdepart and @todepart or (@fromdepart='*')) and ((ggb27c >= 'A' and ggb27c <= 'Z') or ggb27c = '#' or ggb27c = '2' ) and ggb27c not in ('WX','ZFB') select @gak22f = 0 select @gak22f = sum(isnull(isnull(ggb11f, 0) * isnull(gso03i, 1) * isnull(gak22f, 1), 0)) from ggm02 b with (nolock), #ggm01_1 a, gsm15 c with (nolock), gam10 d with(nolock) where b.ggb00c = a.gga00c and b.ggb01c = a.gga01c and b.ggb00c = c.gso00c and b.ggb27c = c.gso02c and d.gak00c = a.gga00c and d.gak01c = a.gga07c and (b.ggb81c between @fromdepart and @todepart or (@fromdepart='*')) and c.gso01c = '3' and c.gso09f = 1 and ggb27c = '4' --第一销售 select @amt2 = 0 select @amt2 = sum(isnull(isnull(ggc11f, 0) * isnull(gso06f, 1) * isnull(a.ggc30f, 1), 0)) from ggm03 b with (nolock), #ggm01_1 a, gsm15 c with (nolock) where b.ggc00c = a.gga00c and b.ggc01c = a.gga01c and b.ggc00c = c.gso00c and b.ggc27c = c.gso02c and (b.ggc81c between @fromdepart and @todepart or (@fromdepart='*')) and c.gso01c = '3' and c.gso10f = 1 and ((ggc27c >= 'A' and ggc27c <= 'Z') or ggc27c = '#' or ggc27c = '2' ) and ggc27c not in ('WX','ZFB') select @gak24f = 0 select @gak24f = sum(isnull(isnull(ggc11f, 0) * isnull(gso06f, 1) * isnull(gak24f, 1), 0)) from ggm03 b with (nolock), #ggm01_1 a, gsm15 c with (nolock),gam10 d with(nolock) where b.ggc00c = a.gga00c and b.ggc01c = a.gga01c and b.ggc00c = c.gso00c and b.ggc27c = c.gso02c and d.gak00c = a.gga00c and d.gak01c = a.gga07c and (b.ggc81c between @fromdepart and @todepart or (@fromdepart='*')) and c.gso01c = '3' and c.gso10f = 1 and ggc27c = '4' --第二销售 select @amt2 = isnull(@amt2,0 ) + sum(isnull(isnull(ggc11f, 0) * isnull(gso06f, 1) * isnull(a.ggc30f, 1), 0)) from ggm03 b with (nolock), #ggm01_1 a, gsm15 c with (nolock) where b.ggc00c = a.gga00c and b.ggc01c = a.gga01c and b.ggc00c = c.gso00c and b.ggc27c = c.gso02c and (b.ggc82c between @fromdepart and @todepart or (@fromdepart='*')) and c.gso01c = '3' and c.gso10f = 1 and ((ggc27c >= 'A' and ggc27c <= 'Z') or ggc27c = '#' or ggc27c = '2' ) and ggc27c not in ('WX','ZFB') select @gak24f = isnull(@gak24f,0) + sum(isnull(isnull(ggc11f, 0) * isnull(gso06f, 1) * isnull(gak24f, 1), 0)) from ggm03 b with (nolock), #ggm01_1 a, gsm15 c with (nolock),gam10 d with(nolock) where b.ggc00c = a.gga00c and b.ggc01c = a.gga01c and b.ggc00c = c.gso00c and b.ggc27c = c.gso02c and d.gak00c = a.gga00c and d.gak01c = a.gga07c and (b.ggc82c between @fromdepart and @todepart or (@fromdepart='*')) and c.gso01c = '3' and c.gso10f = 1 and ggc27c = '4' --第三销售 select @amt2 = isnull(@amt2,0) + sum(isnull(isnull(ggc11f, 0) * isnull(gso06f, 1) * isnull(a.ggc30f, 1), 0)) from ggm03 b with (nolock), #ggm01_1 a, gsm15 c with (nolock) where b.ggc00c = a.gga00c and b.ggc01c = a.gga01c and b.ggc00c = c.gso00c and b.ggc27c = c.gso02c and (b.ggc83c between @fromdepart and @todepart or (@fromdepart='*')) and c.gso01c = '3' and c.gso10f = 1 and ((ggc27c >= 'A' and ggc27c <= 'Z') or ggc27c = '#' or ggc27c = '2' ) and ggc27c not in ('WX','ZFB') select @gak24f = isnull(@gak24f,0) + sum(isnull(isnull(ggc11f, 0) * isnull(gso06f, 1) * isnull(gak24f, 1), 0)) from ggm03 b with (nolock), #ggm01_1 a, gsm15 c with (nolock),gam10 d with(nolock) where b.ggc00c = a.gga00c and b.ggc01c = a.gga01c and b.ggc00c = c.gso00c and b.ggc27c = c.gso02c and d.gak00c = a.gga00c and d.gak01c = a.gga07c and (b.ggc83c between @fromdepart and @todepart or (@fromdepart='*')) and c.gso01c = '3' and c.gso10f = 1 and ggc27c = '4' select @amt2 = isnull(@amt2,0) +isnull(@gak24f,0) select @amt =isnull(@amt,0) +isnull(@gak22f,0) update #tbl_profit set amt = isnull(@amt, 0) + isnull(@amt2, 0) where code = '11AA01' end -- 合计服务收入 select @amt = 0 select @amt = sum(isnull(amt * is_add, 0)) from #tbl_profit where code like '1102%' and is_leaf = 1 update #tbl_profit set amt = isnull(@amt, 0) where code = '1102' -- 统计收入项目 select @amt = 0 select @amt = sum(isnull(amt * is_add, 0)) from #tbl_profit where code like '11%' and is_leaf = 1 update #tbl_profit set amt = isnull(@amt, 0) where code = '11' -- 单独计算产品的销售以及各种付款方式的产品总销售 delete #tbl_stat_fill insert #tbl_stat_fill(paymode, mode, amt) select ggc27c, 0, sum(isnull(case when @sp048 = '2' then ggc21f else isnull(ggc11f, 0) * ggc14f end , 0)) from ggm03 a with (nolock), #ggm01_1 b where a.ggc00c = b.gga00c and a.ggc01c = b.gga01c and b.gga93c like @staff and (a.ggc81c between @fromdepart and @todepart or @fromdepart = '*') group by ggc27c, ggc81c union all select ggc27c, 0, sum(isnull(case when @sp048 = '2' then ggc23f else isnull(ggc11f, 0) * ggc16f end, 0)) from ggm03 a with (nolock), #ggm01_1 b where a.ggc00c = b.gga00c and a.ggc01c = b.gga01c and b.gga93c like @staff and (a.ggc82c between @fromdepart and @todepart or @fromdepart = '*') group by ggc27c, ggc82c union all select ggc27c, 0, sum(isnull(case when @sp048 = '2' then ggc25f else isnull(ggc11f, 0) * ggc18f end, 0)) from ggm03 a with (nolock), #ggm01_1 b where a.ggc00c = b.gga00c and a.ggc01c = b.gga01c and b.gga93c like @staff and (a.ggc83c between @fromdepart and @todepart or @fromdepart = '*') group by ggc27c, ggc83c insert #tbl_profit(code, itemname, is_add, is_leaf) values('1103', '产品销售收入', 0, 1) select @amt = 0 select @amt = sum(isnull(a.amt, 0)) from #tbl_stat_fill a update #tbl_profit set amt = isnull(@amt, 0) where code = '1103' if (isnull(@sp055f, 0) = 0) -- 按项目支付 begin -- 产品销售-现金 insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('110301', '产品销售收入-现金', 0, 1, '1', 9) select @amt = 0 select @amt = sum(isnull(a.amt, 0)) from #tbl_stat_fill a where paymode = '1' update a set a.amt = @amt from #tbl_profit a where a.code = '110301' -- 产品销售-卡 insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('110302', '产品销售收入-卡付', @add_card_svr, 1, 'CARD', 9) -- 很多种卡付的情况, 把积分支付的情况排除掉,单独列出来 select @amt = 0 select @amt = sum(isnull(b.amt, 0)) from #tbl_stat_fill b, gsm14 c with (nolock) where b.paymode = c.gsn01c and isnull(c.gsn02c, '') <> '' and isnull(c.gsn02c, '') <> '999' and isnull(c.gsn02c, '') <> '3' -- 积分支付 update a set a.amt = isnull(@amt, 0) from #tbl_profit a where a.code = '110302' -- 把产品卡付的每个账户也细分出来 create table #tbl_card_pay_good ( paycode varchar(20) not null, acctid int null, amt float null, code varchar(20) null, pay_name varchar(30) null, primary key(paycode) ) insert #tbl_card_pay_good(paycode, amt) select paymode, sum(amt) from #tbl_stat_fill where (paymode = '#' or paymode = '4' or paymode = '2' or paymode = '9' or (paymode >= 'A' and paymode <= 'Z')) -- 不包括积分 and paymode not in ('WX','ZFB') group by paymode update a set a.acctid = b.gsn02c from #tbl_card_pay_good a, gsm14 b with (nolock) where a.paycode = b.gsn01c update a set a.code = right('00' + paycode, 2) from #tbl_card_pay_good a update a set a.pay_name = b.gsb03c from #tbl_card_pay_good a, gsm02 b with (nolock) where a.paycode = b.gsb02c and gsb00c = @incompid and gsb01c = '3' insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind, amt) select '110302' + isnull(code, '00'), '产品收入-取用储值-' + pay_name, 0, 1, paycode, 2, amt from #tbl_card_pay_good drop table #tbl_card_pay_good -- 产品销售-抵用券 select @itemname = gsb03c from gsm02 with (nolock) where gsb01c = '3' and gsb02c = '3' and gsb00c = @incompid insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('110303', '产品销售收入-' + isnull(@itemname, ''), 0, 1, '3', 9) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '110303' and b.paymode = '3' -- 产品销售-抵用券2 select @itemname = gsb03c from gsm02 with (nolock) where gsb01c = '3' and gsb02c = '$' and gsb00c = @incompid insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('110304', '产品销售收入-' + isnull(@itemname, ''), 0, 1, '$', 9) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '110304' and b.paymode = '$' -- 新增加的$开头的支付方式的产品支付 select @code_int = 110350 declare cur_each_pay_3 cursor for select gsb02c from gsm02 with (nolock) where gsb00c = @incompid and gsb01c = '3' and gsb02c like '$%' and len(gsb02c) > 1 order by gsb02c open cur_each_pay_3 fetch cur_each_pay_3 into @pay_$ while @@fetch_status = 0 begin select @code_char = convert(varchar(10), @code_int) select @itemname = gsb03c from gsm02 with (nolock) where gsb01c = '3' and gsb02c = @pay_$ and gsb00c = @incompid insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values(@code_char, '产品销售收入-' + isnull(@itemname, ''), 0, 1, @pay_$, 9) -- 产品销售- 支付方式带 $ 的 金额合计 select @amt = 0 select @amt = sum(isnull(a.amt, 0)) from #tbl_stat_fill a where paymode = @pay_$ update a set a.amt = @amt from #tbl_profit a where a.code = @code_char select @code_int = @code_int + 1 select @code_char = convert(varchar(10), @code_int) fetch cur_each_pay_3 into @pay_$ end close cur_each_pay_3 deallocate cur_each_pay_3 -- 产品销售-经理签单,已经在服务收入中包括了 select @itemname = gsb03c from gsm02 with (nolock) where gsb01c = '3' and gsb02c = '8' and gsb00c = @incompid insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('110305', '产品销售收入-' + isnull(@itemname, ''), 0, 1, '8', 9) -- 产品销售-经理签单 金额合计 select @amt = 0 select @amt = sum(isnull(a.amt, 0)) from #tbl_stat_fill a where paymode = '8' -- 更新 产品销售-经理签单 金额合计 update a set a.amt = @amt from #tbl_profit a where a.code = '110305' -- 产品销售-银行卡 select @itemname = gsb03c from gsm02 with (nolock) where gsb01c = '3' and gsb02c = '6' and gsb00c = @incompid insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('110306', '产品销售收入-' + isnull(@itemname, ''), 0, 1, '6', 9) -- 产品销售-银行卡 金额合计 select @amt = 0 select @amt = sum(isnull(a.amt, 0)) from #tbl_stat_fill a where paymode = '6' -- 更新 产品销售-银行卡 金额 update a set a.amt = @amt from #tbl_profit a where a.code = '110306' -- 产品销售-支付宝 select @itemname = gsb03c from gsm02 with (nolock) where gsb01c = '3' and gsb02c = 'ZFB' and gsb00c = @incompid insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('1103Z1', '产品销售收入-' + isnull(@itemname, ''), 0, 1, 'ZFB', 9) -- 产品销售-支付宝 金额合计 select @amt = 0 select @amt = sum(isnull(a.amt, 0)) from #tbl_stat_fill a where paymode = 'ZFB' -- 更新产品销售-支付宝 金额合计 update a set a.amt = @amt from #tbl_profit a where a.code = '1103Z1' insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('1103W1', '产品销售收入-微信', 0, 1, 'WX', 9) -- 产品销售-微信 金额合计 select @amt = 0 select @amt = sum(isnull(a.amt, 0)) from #tbl_stat_fill a where paymode = 'WX' update a set a.amt = @amt from #tbl_profit a where a.code = '1103W1' -- 产品销售-欠款 insert #tbl_profit(code, itemname, is_add, is_leaf) values('110307', '产品销售收入-欠款', 0, 1) -- 产品销售-欠款 金额合计 select @amt = 0 select @amt = sum(isnull(a.amt, 0)) from #tbl_stat_fill a where paymode = '5' -- 更新 产品销售-欠款 金额合计 update a set a.amt = @amt from #tbl_profit a where a.code = '110307' -- 产品销售内调 select @itemname = gsb03c from gsm02 with (nolock) where gsb01c = '3' and gsb02c = '0' and gsb00c = @incompid insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('110308', '产品销售收入-' + isnull(@itemname, ''), 0, 1, '0', 9) -- 产品销售-内调 金额合计 select @amt = 0 select @amt = sum(isnull(a.amt, 0)) from #tbl_stat_fill a where paymode = '0' -- 更新 产品销售-内调 金额合计 update a set a.amt = @amt from #tbl_profit a where a.code = '110308' -- 产品销售 积分支付 select @itemname = gsb03c from gsm02 with (nolock) where gsb01c = '3' and gsb02c = '7' and gsb00c = @incompid insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) ---- 积分永远不作为服务收入 values('110309', '产品销售收入-' + isnull(@itemname, ''), 0, 1, '7', 9) -- 产品销售-积分 金额合计 select @amt = 0 select @amt = sum(isnull(a.amt, 0)) from #tbl_stat_fill a where paymode = '7' --更新 产品销售-积分 金额合计 update a set a.amt = @amt from #tbl_profit a where a.code = '110309' -- 产品欠款的还款 select @amt = 0 select @amt = isnull(sum(isnull(a.gsc05f, 0)), 0) from #gsm03_tmp a where a.gsc02c = 'gz' and isnull(a.isnotcard, 0) = 1 and gsc04c = '1' and gsc12i = 6 insert #tbl_profit(code, itemname, is_add, is_leaf, amt, payment, kind) values('110310', '产品销售收入-还款-现金', 0, 1, @amt, '1', 9) select @amt = 0 select @amt = isnull(sum(isnull(a.gsc05f, 0)), 0) from #gsm03_tmp a where a.gsc02c = 'gz' and isnull(a.isnotcard, 0) = 1 and gsc04c = '6' and gsc12i = 6 insert #tbl_profit(code, itemname, is_add, is_leaf, amt, payment, kind) values('110311', '产品销售收入-还款-银行卡', 0, 1, @amt, '6', 9) select @amt = 0 select @amt = isnull(sum(isnull(a.gsc05f, 0)), 0) from #gsm03_tmp a where a.gsc02c = 'gz' and isnull(a.isnotcard, 0) = 1 and gsc04c = 'ZFB' and gsc12i = 6 insert #tbl_profit(code, itemname, is_add, is_leaf, amt, payment, kind) values('1103Z2', '产品销售收入-还款-支付宝', 0, 1, @amt, 'ZFB', 9) select @amt = 0 select @amt = isnull(sum(isnull(a.gsc05f, 0)), 0) from #gsm03_tmp a where a.gsc02c = 'gz' and isnull(a.isnotcard, 0) = 1 and gsc04c = 'WX' and gsc12i = 6 insert #tbl_profit(code, itemname, is_add, is_leaf, amt, payment, kind) values('1103W2', '产品销售收入-还款-微信', 0, 1, @amt, 'WX', 9) end --费用 insert #tbl_profit(code, itemname, is_add, is_leaf) values('12', '费用', -1, 0) --其他收入 Huzy 2012/05/31 insert #tbl_profit(code, itemname, is_add, is_leaf) values('18', '其他收入', 1, 0) --虚拟收入 insert #tbl_profit(code, itemname, is_add, is_leaf) values('n1', '虚拟收入', 1, 0) --其他收入 insert #tbl_profit(code, itemname, is_add, is_leaf) values('n2', '虚拟支出', 1, 0) create table #group_fee ( idd int identity not null, code varchar(20) not null, type varchar(20) not null, name varchar(30) null, amt float null, type2 int null, primary key(idd) ) insert #group_fee(code, type, amt) select isnull(gqb03c, ''), isnull(gqa03c, ''), isnull(sum(isnull(gqb05f, 0)), 0) from gqm02 b with (nolock), gqm01 a with (nolock), gam26 z with (nolock) where b.gqb00c = a.gqa00c and b.gqb01c = a.gqa01c and a.gqa00c = z.gaz02c and z.gaz01c = @incompid and a.gqa80d >= @fromdate and a.gqa80d <= @todate and a.gqa95c like @staff and (a.gqa10c between @fromdepart and @todepart or @fromdepart = '*')--add @fromdepart by lyj 2007-09-06 and isnull(b.gqb19i, 0) = 0 --是否是虚拟款 group by isnull(gqb03c, ''), isnull(gqa03c, ''), isnull(gqb19i, 0) insert #group_fee(code, type, amt, type2) select isnull(gqb03c, ''), isnull(gqa03c, ''), isnull(sum(isnull(gqb05f, 0)), 0), isnull(b.gqb19i, 0) from gqm02 b with (nolock), gqm01 a with (nolock), gam26 z with (nolock) where b.gqb00c = a.gqa00c and b.gqb01c = a.gqa01c and a.gqa00c = z.gaz02c and z.gaz01c = @incompid and a.gqa80d >= @fromdate and a.gqa80d <= @todate and a.gqa95c like @staff and (a.gqa10c between @fromdepart and @todepart or @fromdepart = '*')--add @fromdepart by lyj 2007-09-06 and isnull(b.gqb19i, 0) = 1 --是否是虚拟款 group by isnull(gqb03c, ''), isnull(gqa03c, ''), isnull(b.gqb19i, 0) update a set a.name = b.gsb03c from #group_fee a, gsm02 b with (nolock) where b.gsb00c = @incompid and b.gsb01c = 'N' and b.gsb02c = a.code and a.type = 'gv' update a set a.name = b.gsb03c from #group_fee a, gsm02 b with (nolock) where b.gsb00c = @incompid and b.gsb01c = 'A5' and b.gsb02c = a.code and a.type = 'gp' update a set a.code = '12' + right(('000' + convert(varchar(10), a.idd)), 2) from #group_fee a where a.type = 'gv' and type2 is null update a set a.code = '18' + right(('000' + convert(varchar(10), a.idd)), 2) from #group_fee a where a.type = 'gp' and type2 is null --虚拟收入 update a set a.code = 'n1' + right(('000' + convert(varchar(10), a.idd)), 2) from #group_fee a where a.type = 'gp' and type2 = 1 --虚拟支出 update a set a.code = 'n2' + right(('000' + convert(varchar(10), a.idd)), 2) from #group_fee a where a.type = 'gv' and type2 = 1 insert #tbl_profit(code, itemname, amt, is_add, is_leaf) select code, isnull(name, ''), amt, case when type = 'gv' then -1 when type = 'gp' then 1 else 0 end, 1 from #group_fee order by type --费用-房租 Added by WUXM 2006/02/09 declare @rent float declare @range int declare @flow int declare @code varchar(20) declare @before varchar(10) declare @after varchar(10) declare @cur varchar(10) select @before = substring(convert(varchar(10), dateadd(day, -1, cast(@fromdate as datetime)), 120), 6, 2) select @after = substring(convert(varchar(10), dateadd(day, 1, cast(@todate as datetime)), 120), 6, 2) select @cur = substring(convert(varchar(10), cast(@fromdate as datetime), 120), 6, 2) if (@before = '12' and @cur = '01' and @after = '02') select @rent = sum(isnull(gae18f, 0)) from gam05 with (nolock), gam26 z with (nolock) where gae01c = z.gaz02c and z.gaz01c = @incompid else if (@before = '11' and @cur = '12' and @after = '01') select @rent = sum(isnull(gae18f, 0)) from gam05 with (nolock), gam26 z with (nolock) where gae01c = z.gaz02c and z.gaz01c = @incompid else if ((@before < @cur) and (@cur < @after)) --从月初到月末的统计 begin select @rent = sum(isnull(gae18f, 0)) from gam05 with (nolock), gam26 z with (nolock) where gae01c = z.gaz02c and z.gaz01c = @incompid end else begin select @range = datediff(day, cast(@fromdate as datetime), cast(@todate as datetime)) select @rent = sum(isnull(gae18f, 0)) * (@range + 1) / 30 from gam05 with (nolock), gam26 z with (nolock) where gae01c = z.gaz02c and z.gaz01c = @incompid end select @code = '1299' if (@rent > 0) begin insert #tbl_profit(code, itemname, amt, is_add, is_leaf) values (@code, '房租', @rent, -1, 1) end drop table #group_fee select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit where code like '12%' and is_leaf = 1 update #tbl_profit set amt = isnull(@amt, 0) where code = '12' select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit where code like '18%' and is_leaf = 1 update #tbl_profit set amt = isnull(@amt, 0) where code = '18' select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit where code like 'n1%' and is_leaf = 1 update #tbl_profit set amt = isnull(@amt, 0) where code = 'n1' select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit where code like 'n2%' and is_leaf = 1 update #tbl_profit set amt = isnull(@amt, 0) where code = 'n2' --成本 insert #tbl_profit(code, itemname, is_add, is_leaf) values('13', '成本', -1, 0) select @amt = 0 select @amt = sum(isnull(hah05f, 0)) from ham08 with (nolock), gam26 z with (nolock) where hah00c = z.gaz02c and z.gaz01c = @incompid and hah03d >= @fromdate and hah03d <= @todate insert #tbl_profit(code, itemname, amt, is_add, is_leaf) values('1301', '成本-日常奖罚', @amt, -1, 1) --计算总提成 declare @spid int select @spid = @@spid if @flag = 0 begin create table #tmp_rst(amt float null) insert #tmp_rst(amt) exec upg_personal_statistics @incompid, @fromdate, @todate, '*', '', 1, 1, 1, 6, @spid -- 6:所有员工提成总数据 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tmp_rst drop table #tmp_rst insert #tbl_profit(code, itemname, amt, is_add, is_leaf) values('1302', '成本-员工提成', @amt, -1, 1) end select @amt = 0 select @amt = sum(isnull(ggc05f, 0)*isnull(gfa19f, 0)) from ggm03 a with (nolock), #ggm01_1 b, gfm01 c with (nolock) where a.ggc00c = b.gga00c and a.ggc01c = b.gga01c and a.ggc00c = c.gfa00c and a.ggc03c = c.gfa01c and (a.ggc81c between @fromdepart and @todepart or @fromdepart = '*') select @amt = isnull(@amt, 0) + isnull(sum(isnull(gge05f, 0)*isnull(gfa15f, 0)), 0) from ggm05 a with (nolock), #ggm01_1 b, gfm01 c with (nolock) where a.gge00c = b.gga00c and a.gge01c = b.gga01c and a.gge00c = c.gfa00c and a.gge03c = c.gfa01c and (a.gge81c between @fromdepart and @todepart or @fromdepart = '*') insert #tbl_profit(code, itemname, amt, is_add, is_leaf) values('1303', '成本-产品', isnull(@amt, 0), -1, 1) --成本-固定工资 select @amt = 0 select @amt = sum(isnull(hbb05f, 0)) from hbm02 with (nolock), gam26 z with (nolock) where hbb00c = z.gaz02c and z.gaz01c = @incompid and hbb04c = '1' if (@before = '12' and @cur = '01' and @after = '02') select @amt = @amt else if (@before = '11' and @cur = '12' and @after = '01') select @amt = @amt else if ((@before < @cur) and (@cur < @after)) --从月初到月末的统计 select @amt = @amt else select @amt = @amt * (@range + 1)/30 insert #tbl_profit(code, itemname, amt, is_add, is_leaf) values('1304', '成本-固定工资', @amt, -1, 1) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit where code like '13%' and is_leaf = 1 update #tbl_profit set amt = isnull(@amt, 0) where code = '13' --删除为了性能必须要得ggm01的缓冲表 drop table #ggm01_1 --计算利润 select @amt = 0 select @amt = sum(isnull(amt*is_add, 0)) from #tbl_profit where len(code) = 2 insert #tbl_profit(code, itemname, amt, is_add, is_leaf) values('19', '利润', isnull(@amt, 0), 0, 1) --单位面积利润率 declare @area float select @area = sum(isnull(gae17f, 0)) from gam05 with (nolock), gam26 z with (nolock) where gae01c = z.gaz02c and z.gaz01c = @incompid if (@area > 0) begin insert #tbl_profit(code, itemname, amt, is_add, is_leaf) values (20, '单位面积利润', @amt/@area, 0, 1) end --储值余额 select @amt = 0 select @amt = sum(isnull(gcc06f, 0)) from gcm03 with (nolock), gcm01 with (nolock), gam26 z with (nolock) where gcc00c = z.gaz02c and z.gaz01c = @incompid and (gcc03i <> 1 and gcc03i <> 0 and gcc03i <> 3 and gcc03i < 888) and gca08i <> 1 and gcc00c = gca00c and gca01c = gcc01c and gcc00c = gca13d insert #tbl_profit(code, itemname, amt, is_add, is_leaf) values('14', '储值余额', isnull(@amt, 0), 0, 1) declare @pointssum float select @pointssum = 0 select @pointssum = sum(isnull(gcd06f, 0)) from gcm04 with (nolock), gam26 z with (nolock) where gcd09d >= @fromdate and gcd09d <= @todate and gcd02i = 3 and gcd00c = z.gaz02c and z.gaz01c = @incompid and gcd05i = 0 insert #tbl_profit(code, itemname, amt, is_add, is_leaf) values('15', '赠送积分', isnull(@pointssum, 0), 0, 1) --删除第二级为0的 delete #tbl_profit where isnull(amt, 0) = 0 and len(code) > 4 drop table #gsm03_tmp select * from #tbl_profit ----分部门时,不缓存gcm21数据 -- if ((@staff = '' or @staff = '*' or @staff = '%') and @flag = 1 and @fromdate = @todate) -- begin -- delete gcm21 -- where gcu00c = @incompid and gcu01c = @todate -- -- insert gcm21(gcu00c, gcu01c, gcu02i, gcu03c, gcu04c, gcu05i, gcu06f, gcu07i, gcu08c, gcu09i) -- select @incompid, @todate, idd, code, itemname, is_add, isnull(amt, 0), is_leaf, payment, kind -- from #tbl_profit -- end drop table #gem01_tmp drop table #tbl_profit drop table #tbl_stat_fill drop table #dept_bills drop table #tbl_bill_total drop table #gcm10_888 end GO /****** Object: StoredProcedure [dbo].[upg_analyse_profit_inter_depart_emp] Script Date: 06/12/2017 06:19:26 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_analyse_profit_inter_depart_emp] ( @compid varchar(10), @fromdate varchar(10), @todate varchar(10), @flag int, -- flag 0:计算员工提成 1:不计算员工提成 @staff varchar(20), -- 收银人员 @fromdepart varchar(10), -- 部门 --add by lyj 2007-09-06 @todepart varchar(10), @calmode int ) as begin declare @amt float declare @amt2 float declare @amt3 float declare @itemtype varchar(10) declare @itemname varchar(60) declare @id int --变量 declare @code_no varchar(10) declare @isadd_0 float declare @isadd_3 float declare @isadd_$ float declare @incompid varchar(10) select @incompid = @compid declare @add_card_sale int declare @add_card_svr int -- 卡付是否作为收入 declare @sp215f float --充值疗程储值方式支付是否作为卡付实际营业额 declare @gak24f float declare @gak22f float select @sp215f = gsa03f from gsm01 with (nolock) where gsa00c = @compid and gsa01c = 'SP212' if @calmode = 2 begin declare @sp115f float select @sp115f = gsa03f from gsm01 with (nolock) where gsa00c = @compid and gsa01c = 'SP115' if @sp115f = 0 select @calmode = 0 else select @calmode = 1 end if @calmode = 0 begin select @add_card_sale = 1 select @add_card_svr = 0 end else begin select @add_card_sale = 0 select @add_card_svr = 1 end select @isadd_0 = gso07f from gsm15 with (nolock) where gso00c = @incompid and gso02c = '0' select @isadd_3 = gso07f from gsm15 with (nolock) where gso00c = @incompid and gso02c = '3' select @isadd_$ = gso07f from gsm15 with (nolock) where gso00c = @incompid and gso02c = '$' select @isadd_0 = isnull(@isadd_0, 0) select @isadd_3 = isnull(@isadd_3, 0) select @isadd_$ = isnull(@isadd_$, 0) select @incompid = @compid if @staff = '*' or @staff = '' begin select @staff = '%' end declare @sp055f float select @sp055f = gsa03f from gsm01 with (nolock) where gsa00c = @incompid and gsa01c = 'SP055' declare @sp048 varchar(10) select @sp048 = gsa02c from gsm01 with (nolock) where gsa00c = @incompid and gsa01c = 'SP048' create table #customed_proj_count--总客数 add by lm 2016.06.21 ( customed_num float null, empno varchar(20) not null, compid varchar(20) not null primary key (compid,empno) ) create table #emp_info --add by lm 2016.06.21 ( empno varchar(20) not null, empname varchar(20) null, compid varchar(20) not null primary key (compid, empno) ) insert #emp_info(compid, empno,empname) select gsb00c, gsb02c,gsb03c from gsm02 a with(nolock), gam26 z with(nolock) where a.gsb00c = z.gaz02c and a.gsb01c = 'U' and z.gaz01c = @compid and ((a.gsb02c between @fromdepart and @todepart )or @fromdepart = '*') create table #gsm03_tmp -- 单据--支付明细 ( gsc00c varchar(20) not null, -- 公司编号 gsc01c varchar(20) not null, -- 单据编号 gsc02c varchar(5) not null, -- 单据类别 gsc04c varchar(5) not null, -- 支付方式 gsc05f float not null, -- 支付金额 gsc10d varchar(8) not null, -- 单据帐务日期 gsc11c varchar(20) not null, -- 登陆人 gsc12i int not null, -- 方式 empno varchar(20) null, -- 部门编号 isnotcard int null default 0 -- 标志是否为卡销售的,为了区分充值时候的产品或项目的欠款与还款 ) create nonclustered index gsm03_tmp_idx_02 on #gsm03_tmp(gsc02c) create clustered index gsm03_tmp_idx_01 on #gsm03_tmp(gsc04c) create nonclustered index gsm03_tmp_idx_03 on #gsm03_tmp(gsc00c, empno) --modify by lm 2015-10-14 create table #gsm03_tmp_gem01 -- 竞争转卡--支付明细 ( gsc00c varchar(20) not null, -- 公司编号 gsc01c varchar(20) not null, -- 单据编号 gsc02c varchar(5) not null, -- 单据类别 gsc04c varchar(5) not null, -- 支付方式 gsc05f float not null, -- 支付金额 gsc10d varchar(8) not null, -- 单据帐务日期 gsc11c varchar(20) not null, -- 登陆人 gsc12i int not null, -- 方式 empno varchar(20) null, -- 部门编号 isnotcard int null default 0 -- 标志是否为卡销售的,为了区分充值时候的产品或项目的欠款与还款 ) -- create clustered index gem01_tmp_idx_01 on #gsm03_tmp_gem01(gsc00c, empno) --因为项目或产品的欠款也是纪录在会员账户管理中,而且还款也是纪录在其中 --所以必须把这些特殊的gcm10标记出来,否则在统计会员卡销售的时候就不准 -- modify by lyj for add department 2007-09-06 -- 因为这个涉及到一个单据有几种支付方式的问题,所以只有先把gnm01 gcm10, ggm02, ggm03 gqm01, gem01 插入到 -- 临时表,然后和#gsm03_tmp_01 通过单号和公司别,单据类型来链接, 这样才能把gnm01 gcm10 gem01, gqm01 中的支付方式给分开 -- 但是对于ggm02 和ggm03 还要判断ggm02,ggm03中支付方式和支付明细中的支付方式相同才能把支付方式分开,所以现在 -- 按部门统计的时候支付方式必须用项目支付. -- 把值插入到 #gsm03_tmp declare @SP031 float --是否为多账单统一结帐 select @SP031 = gsa03f from gsm01 with (nolock) where gsa00c = @incompid and gsa01c = 'SP031' create table #dept_bills ( flag int identity (1, 1) not null, compid varchar(10) null, billtype varchar(5) null, billno varchar(20) null, payway varchar(5) null, payamt float null, -- 当从ggm02, ggm03中取数据的时候此值要填写上 ratio float null, -- 并不是整个单据都属于一个部门的,是部分 empno varchar(20) null, -- 部门编号 ) -- 并且在gsm03和ggm02, ggm03链接的时候, #gsm03_tmp 中的值应该是payamt 而不是gsm03中的gsc05f create clustered index departresult_01 on #dept_bills(billtype, billno, payway) create table #dept_bills_dept ( flag int identity (1, 1) not null, compid varchar(10) null, billtype varchar(5) null, billno varchar(20) null, payway varchar(5) null, payamt float null, -- 当从ggm02, ggm03中取数据的时候此值要填写上 empno varchar(20) null, -- 部门编号 ratio float null -- 并不是整个单据都属于一个部门的,是部分 ) create clustered index idx_dept_bills_dept on #dept_bills_dept(billtype, billno, payway) create table #tbl_gnm01 ( gna00c varchar(10) not null, gna01c varchar(20) not null, gna27f float null, --第一分享比率 gna29f float null, --分享比率2 gna31f float null, --分享比率3 gna33f float null, --分享比率4 gna35f float null, --分享比率5 gna37f float null, --分享比率6 gna39f float null, --分享比率7 gna41f float null, --分享比率8 gna43f float null, --分享比率9 gna45f float null, --分享比率10 gna81c varchar(10) null, --部门编号 gna82c varchar(10) null, --部门编号2 gna83c varchar(10) null, --部门编号3 gna100c varchar(10) null, --部门编号4 Add by maql 20100705 gna101c varchar(10) null, --部门编号5 Add by maql 20100705 gna102c varchar(10) null, --部门编号6 Add by maql 20100705 gna103c varchar(10) null, --部门编号7 Add by maql 20100705 gna104c varchar(10) null, --部门编号8 Add by maql 20100705 gna105c varchar(10) null, --部门编号9 Add by maql 20100705 gna106c varchar(10) null, --部门编号10 Add by maql 20100705 gna67f float null, gna69f float null, gna71f float null, gna86f float null, gna87f float null, gna88f float null, gna89f float null, gna90f float null, gna91f float null, gna92f float null, bill_total_amt float null, gna26f float null, gnb08f float null, ) create clustered index idx_tbl_gnm01 on #tbl_gnm01(gna00c, gna01c) create nonclustered index idx_tbl_gnm01_02 on #tbl_gnm01(gna81c) create nonclustered index idx_tbl_gnm01_03 on #tbl_gnm01(gna82c) create nonclustered index idx_tbl_gnm01_04 on #tbl_gnm01(gna83c) create nonclustered index idx_tbl_gnm01_05 on #tbl_gnm01(gna100c) create nonclustered index idx_tbl_gnm01_06 on #tbl_gnm01(gna101c) create nonclustered index idx_tbl_gnm01_07 on #tbl_gnm01(gna102c) create nonclustered index idx_tbl_gnm01_08 on #tbl_gnm01(gna103c) create nonclustered index idx_tbl_gnm01_09 on #tbl_gnm01(gna104c) create nonclustered index idx_tbl_gnm01_10 on #tbl_gnm01(gna105c) create nonclustered index idx_tbl_gnm01_11 on #tbl_gnm01(gna106c) create table #tbl_gcm10 ( gcl00c varchar(10) not null, --公司编号 gcl01c varchar(20) not null, --异动单号 gcl16f float null, --第一销售分享比率 gcl17c varchar(20) null, --第二销售 gcl18f float null, --分享比率2 gcl19c varchar(20) null, --第三负责人 gcl20f float null, --分享比率3 gcl23f float null, --分享比率4 gcl25f float null, --分享比率5 gcl27f float null, --分享比率6 gcl29f float null, --分享比率7 gcl31f float null, --分享比率8 gcl33f float null, --分享比率9 gcl35f float null, --分享比率10 gcl81c varchar(10) null, --部门编号 gcl82c varchar(10) null, --部门编号2 gcl83c varchar(10) null, --部门编号3 gcl100c varchar(10) null, --部门编号4 gcl101c varchar(10) null, --部门编号5 gcl102c varchar(10) null, --部门编号6 gcl103c varchar(10) null, --部门编号7 gcl104c varchar(10) null, --部门编号8 gcl105c varchar(10) null, --部门编号9 gcl106c varchar(10) null, --部门编号10 gcl58f float null, gcl60f float null, gcl62f float null, gcl83f float null, gcl84f float null, gcl85f float null, gcl86f float null, gcl87f float null, gcl88f float null, gcl89f float null, gcl07f float null, bill_total_amt float null ) create clustered index idx_tbl_gcm10 on #tbl_gcm10(gcl00c, gcl01c) create nonclustered index idx_tbl_gcm10_02 on #tbl_gcm10(gcl81c) create nonclustered index idx_tbl_gcm10_03 on #tbl_gcm10(gcl82c) create nonclustered index idx_tbl_gcm10_04 on #tbl_gcm10(gcl83c) create nonclustered index idx_tbl_gcm10_05 on #tbl_gcm10(gcl100c) create nonclustered index idx_tbl_gcm10_06 on #tbl_gcm10(gcl101c) create nonclustered index idx_tbl_gcm10_07 on #tbl_gcm10(gcl102c) create nonclustered index idx_tbl_gcm10_08 on #tbl_gcm10(gcl103c) create nonclustered index idx_tbl_gcm10_09 on #tbl_gcm10(gcl104c) create nonclustered index idx_tbl_gcm10_10 on #tbl_gcm10(gcl105c) create nonclustered index idx_tbl_gcm10_11 on #tbl_gcm10(gcl106c) insert #tbl_gnm01(gna00c, gna01c, gna27f, gna29f, gna31f, gna33f, gna35f, gna37f, gna39f, gna41f, gna43f, gna45f, gna81c, gna82c, gna83c, gna100c, gna101c, gna102c, gna103c, gna104c, gna105c, gna106c, gna67f, gna69f, gna71f, gna86f, gna87f, gna88f, gna89f, gna90f, gna91f, gna92f, gna26f, gnb08f, bill_total_amt) select gna00c, gna01c, gna27f, gna29f, gna31f, gna33f, gna35f, gna37f, gna39f, gna41f, gna43f, gna45f, gna81c, gna82c, gna83c, gna100c, gna101c, gna102c, gna103c, gna104c, gna105c, gna106c, gna67f, gna69f, gna71f, gna86f, gna87f, gna88f, gna89f, gna90f, gna91f, gna92f, gna26f, gnb08f, isnull(gna67f, 0) + isnull(gna69f, 0) + isnull(gna71f, 0) + isnull(gna86f, 0) + isnull(gna87f, 0) + isnull(gna88f, 0) + isnull(gna89f, 0) + isnull(gna90f, 0) + isnull(gna91f, 0) + isnull(gna92f, 0) from gnm01 a with (nolock), gam26 z with (nolock), gnm02 c with (nolock) where a.gna00c = z.gaz02c and a.gna00c = c.gnb00c and a.gna01c = c.gnb01c and z.gaz01c = @incompid and gna80d between @fromdate and @todate and gna93c like @staff insert #tbl_gcm10(gcl00c, gcl01c, gcl16f, gcl17c, gcl18f, gcl19c, gcl20f, gcl23f, gcl25f, gcl27f, gcl29f, gcl31f, gcl33f, gcl35f, gcl81c, gcl82c, gcl83c, gcl100c, gcl101c, gcl102c, gcl103c, gcl104c, gcl105c, gcl106c, gcl58f, gcl60f, gcl62f, gcl83f, gcl84f, gcl85f, gcl86f, gcl87f, gcl88f, gcl89f, gcl07f, bill_total_amt) select gcl00c, gcl01c, gcl16f, gcl17c, gcl18f, gcl19c, gcl20f, gcl23f, gcl25f, gcl27f, gcl29f, gcl31f, gcl33f, gcl35f, gcl81c, gcl82c, gcl83c, gcl100c, gcl101c, gcl102c, gcl103c, gcl104c, gcl105c, gcl106c, gcl58f, gcl60f, gcl62f, gcl83f, gcl84f, gcl85f, gcl86f, gcl87f, gcl88f, gcl89f, gcl07f, isnull(gcl58f, 0) + isnull(gcl60f, 0) + isnull(gcl62f, 0) + isnull(gcl83f, 0) + isnull(gcl84f, 0) + isnull(gcl85f, 0) + isnull(gcl86f, 0) + isnull(gcl87f, 0) + isnull(gcl88f, 0) + isnull(gcl89f, 0) from gcm10 a with (nolock), gam26 z with (nolock) where gcl00c = z.gaz02c and z.gaz01c = @incompid and gcl80d between @fromdate and @todate and gcl93c like @staff --为了提高性能先把此值插入到临时表 create table #gsm03_tmp_01 -- 单据--支付明细 ( gsc00c varchar(10) not null, --公司 gsc01c varchar(20) not null, --单据编号 gsc02c varchar(5) not null, --单据类别 gsc04c varchar(5) not null, --支付方式 gsc05f float not null, --支付金额 gsc10d varchar(8) not null, --单据帐务日期 gsc11c varchar(20) not null, --登陆人 gsc12i int not null --方式 ) create nonclustered index gsm03_tmp_01_01 on #gsm03_tmp_01(gsc02c) create nonclustered index gsm03_tmp_01_02 on #gsm03_tmp_01(gsc01c) -------把gsm03里面符合条件的先insert到临时表中 if (@compid = '001' or @compid = '*') begin insert #gsm03_tmp_01(gsc00c, gsc01c, gsc02c, gsc04c, gsc05f, gsc10d, gsc11c, gsc12i) select gsc00c, gsc01c, gsc02c, gsc04c, isnull(gsc05f, 0), gsc10d, gsc11c, isnull(gsc12i, -1) from gsm03 with (nolock) where gsc10d >= @fromdate and gsc10d <= @todate and gsc11c like @staff end else begin insert #gsm03_tmp_01(gsc00c, gsc01c, gsc02c, gsc04c, gsc05f, gsc10d, gsc11c, gsc12i) select gsc00c, gsc01c, gsc02c, gsc04c, isnull(gsc05f, 0), gsc10d, gsc11c, isnull(gsc12i, -1) from gsm03 with (nolock) where gsc00c = @compid and gsc10d >= @fromdate and gsc10d <= @todate and gsc11c like @staff end create table #tbl_bill_total ( compid varchar(20) not null, billid varchar(20) not null, amt float null, primary key(compid, billid)) if (@compid = '001' or @compid = '*') begin insert #tbl_bill_total(compid, billid, amt) select gnb00c, gnb01c, sum(gnb08f) from gnm01 c with (nolock), gnm02 a with (nolock) where c.gna80d >= @fromdate and c.gna80d <= @todate and c.gna00c = a.gnb00c and c.gna01c = a.gnb01c group by gnb00c, gnb01c end else begin insert #tbl_bill_total(compid, billid, amt) select gnb00c, gnb01c, sum(gnb08f) from gnm01 c with (nolock), gnm02 a with (nolock) where a.gnb00c = @compid and c.gna80d >= @fromdate and c.gna80d <= @todate and c.gna00c = a.gnb00c and c.gna01c = a.gnb01c group by gnb00c, gnb01c end ---在营业数据插入到#departsesult临时表 insert #dept_bills_dept(compid, billtype, billno, ratio, payway, payamt, empno) select gna00c, 'gn', gna01c, isnull(gna27f, 0), gsc04c, case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gna67f,0)*b.gsc05f / bill_total_amt end , gna81c from #tbl_gnm01 a, #gsm03_tmp_01 b where a.gna00c = b.gsc00c and a.gna01c = b.gsc01c and b.gsc02c = 'gn' and (isnull(gna81c, '') between @fromdepart and @todepart ) --add by lyj 2007-09-06 insert #dept_bills_dept(compid, billtype, billno, ratio, payway, payamt, empno) select gna00c, 'gn', gna01c, isnull(gna29f, 0), gsc04c, case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gna69f,0)*b.gsc05f / bill_total_amt end, gna82c from #tbl_gnm01 a, #gsm03_tmp_01 b where a.gna00c = b.gsc00c and a.gna01c = b.gsc01c and b.gsc02c = 'gn' and (isnull(gna82c, '') between @fromdepart and @todepart ) --add by lyj 2007-09-06 insert #dept_bills_dept(compid, billtype, billno, ratio, payway, payamt, empno) select gna00c, 'gn', gna01c, isnull(gna31f, 0),gsc04c, case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gna71f,0)*b.gsc05f / bill_total_amt end, gna83c from #tbl_gnm01 a, #gsm03_tmp_01 b where a.gna00c = b.gsc00c and a.gna01c = b.gsc01c and b.gsc02c = 'gn' and (isnull(gna83c, '') between @fromdepart and @todepart ) --add by lyj 2007-09-06 insert #dept_bills_dept(compid, billtype, billno, ratio, payway, payamt, empno) select gna00c, 'gn', gna01c, isnull(gna33f, 0), gsc04c, case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gna86f,0)*b.gsc05f / bill_total_amt end, gna100c from #tbl_gnm01 a, #gsm03_tmp_01 b where a.gna00c = b.gsc00c and a.gna01c = b.gsc01c and b.gsc02c = 'gn' and (isnull(gna100c, '') between @fromdepart and @todepart ) --add by maql 20100705 insert #dept_bills_dept(compid, billtype, billno, ratio, payway, payamt, empno) select gna00c, 'gn', gna01c, isnull(gna35f, 0), gsc04c, case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gna87f,0)*b.gsc05f / bill_total_amt end, gna101c from #tbl_gnm01 a, #gsm03_tmp_01 b where a.gna00c = b.gsc00c and a.gna01c = b.gsc01c and b.gsc02c = 'gn' and (isnull(gna101c, '') between @fromdepart and @todepart ) --add by maql 20100705 insert #dept_bills_dept(compid, billtype, billno, ratio, payway, payamt, empno) select gna00c, 'gn', gna01c, isnull(gna37f, 0), gsc04c, case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gna88f,0)*b.gsc05f / bill_total_amt end , gna102c from #tbl_gnm01 a, #gsm03_tmp_01 b where a.gna00c = b.gsc00c and a.gna01c = b.gsc01c and b.gsc02c = 'gn' and (isnull(gna102c, '') between @fromdepart and @todepart ) --add by maql 20100705 insert #dept_bills_dept(compid, billtype, billno, ratio, payway, payamt, empno) select gna00c, 'gn', gna01c, isnull(gna39f, 0), gsc04c, case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gna89f,0)*b.gsc05f / bill_total_amt end , gna103c from #tbl_gnm01 a, #gsm03_tmp_01 b where a.gna00c = b.gsc00c and a.gna01c = b.gsc01c and b.gsc02c = 'gn' and (isnull(gna103c, '') between @fromdepart and @todepart ) --add by maql 20100705 insert #dept_bills_dept(compid, billtype, billno, ratio, payway, payamt, empno) select gna00c, 'gn', gna01c, isnull(gna41f, 0), gsc04c, case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gna90f,0)*b.gsc05f / bill_total_amt end, gna104c from #tbl_gnm01 a, #gsm03_tmp_01 b where a.gna00c = b.gsc00c and a.gna01c = b.gsc01c and b.gsc02c = 'gn' and (isnull(gna104c, '') between @fromdepart and @todepart ) --add by maql 20100705 insert #dept_bills_dept(compid, billtype, billno, ratio, payway, payamt, empno) select gna00c, 'gn', gna01c, isnull(gna43f, 0), gsc04c, case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gna91f,0)*b.gsc05f / bill_total_amt end, gna105c from #tbl_gnm01 a, #gsm03_tmp_01 b where a.gna00c = b.gsc00c and a.gna01c = b.gsc01c and b.gsc02c = 'gn' and (isnull(gna105c, '') between @fromdepart and @todepart ) --add by maql 20100705 insert #dept_bills_dept(compid, billtype, billno, ratio, payway, payamt, empno) select gna00c, 'gn', gna01c, isnull(gna45f, 0), gsc04c, case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gna92f,0)*b.gsc05f / bill_total_amt end, gna106c from #tbl_gnm01 a, #gsm03_tmp_01 b where a.gna00c = b.gsc00c and a.gna01c = b.gsc01c and b.gsc02c = 'gn' and (isnull(gna106c, '') between @fromdepart and @todepart ) --add by maql 20100705 insert into #dept_bills(compid, billtype, billno, ratio, payway, payamt, empno) select compid, billtype, billno, sum(ratio), payway, sum(payamt),empno from #dept_bills_dept a group by a.compid, a.billtype, a.billno, a.payway, empno drop table #tbl_gnm01 -- insert gcm10 truncate table #dept_bills_dept -- 第一销售 insert #dept_bills_dept(compid, billtype, billno, ratio, payway, payamt, empno ) select gcl00c, 'gz', gcl01c, isnull(gcl16f, 0), gsc04c, case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gcl58f,0) * b.gsc05f / bill_total_amt end , gcl81c from #tbl_gcm10 a, #gsm03_tmp_01 b where a.gcl00c = b.gsc00c and a.gcl01c = b.gsc01c and b.gsc02c = 'gz' and (isnull(gcl81c, '') between @fromdepart and @todepart ) --add by lyj 2007-09-06 insert #dept_bills_dept(compid, billtype, billno, ratio, payway, payamt, empno) select gcl00c, 'gz', gcl01c, isnull(gcl18f, 0), gsc04c, case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gcl60f,0) * b.gsc05f / bill_total_amt end , gcl82c from #tbl_gcm10 a, #gsm03_tmp_01 b where a.gcl00c = b.gsc00c and a.gcl01c = b.gsc01c and b.gsc02c = 'gz' and (isnull(gcl82c, '') between @fromdepart and @todepart ) --add by lyj 2007-09-06 insert #dept_bills_dept(compid, billtype, billno, ratio, payway, payamt, empno) select gcl00c, 'gz', gcl01c, isnull(gcl20f, 0), gsc04c, case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gcl62f,0) * b.gsc05f / bill_total_amt end , gcl83c from #tbl_gcm10 a, #gsm03_tmp_01 b where a.gcl00c = b.gsc00c and a.gcl01c = b.gsc01c and b.gsc02c = 'gz' and (isnull(gcl83c, '') between @fromdepart and @todepart ) --add by lyj 2007-09-06 insert #dept_bills_dept(compid, billtype, billno, ratio, payway, payamt, empno) select gcl00c, 'gz', gcl01c, isnull(gcl23f, 0), gsc04c, case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gcl83f,0) * b.gsc05f / bill_total_amt end, gcl100c from #tbl_gcm10 a, #gsm03_tmp_01 b where a.gcl00c = b.gsc00c and a.gcl01c = b.gsc01c and b.gsc02c = 'gz' and (isnull(gcl100c, '') between @fromdepart and @todepart ) --add by maql 20100705 insert #dept_bills_dept(compid, billtype, billno, ratio, payway, payamt, empno) select gcl00c, 'gz', gcl01c, isnull(gcl25f, 0), gsc04c, case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gcl84f,0) * b.gsc05f / bill_total_amt end, gcl101c from #tbl_gcm10 a, #gsm03_tmp_01 b where a.gcl00c = b.gsc00c and a.gcl01c = b.gsc01c and b.gsc02c = 'gz' and (isnull(gcl101c, '') between @fromdepart and @todepart ) --add by maql 20100705 insert #dept_bills_dept(compid, billtype, billno, ratio, payway, payamt, empno) select gcl00c, 'gz', gcl01c, isnull(gcl27f, 0), gsc04c, case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gcl85f,0) * b.gsc05f / bill_total_amt end, gcl102c from #tbl_gcm10 a, #gsm03_tmp_01 b where a.gcl00c = b.gsc00c and a.gcl01c = b.gsc01c and b.gsc02c = 'gz' and (isnull(gcl102c, '') between @fromdepart and @todepart ) --add by maql 20100705 insert #dept_bills_dept(compid, billtype, billno, ratio, payway, payamt, empno) select gcl00c, 'gz', gcl01c, isnull(gcl29f, 0), gsc04c, case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gcl86f,0) * b.gsc05f / bill_total_amt end, gcl103c from #tbl_gcm10 a, #gsm03_tmp_01 b where a.gcl00c = b.gsc00c and a.gcl01c = b.gsc01c and b.gsc02c = 'gz' and (isnull(gcl103c, '') between @fromdepart and @todepart ) --add by maql 20100705 insert #dept_bills_dept(compid, billtype, billno, ratio, payway, payamt, empno) select gcl00c, 'gz', gcl01c, isnull(gcl31f, 0), gsc04c, case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gcl87f,0) * b.gsc05f / bill_total_amt end, gcl104c from #tbl_gcm10 a, #gsm03_tmp_01 b where a.gcl00c = b.gsc00c and a.gcl01c = b.gsc01c and b.gsc02c = 'gz' and (isnull(gcl104c, '') between @fromdepart and @todepart ) --add by maql 20100705 insert #dept_bills_dept(compid, billtype, billno, ratio, payway, payamt, empno) select gcl00c, 'gz', gcl01c, isnull(gcl33f, 0), gsc04c, case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gcl88f,0) * b.gsc05f / bill_total_amt end, gcl105c from #tbl_gcm10 a, #gsm03_tmp_01 b where a.gcl00c = b.gsc00c and a.gcl01c = b.gsc01c and b.gsc02c = 'gz' and (isnull(gcl105c, '') between @fromdepart and @todepart ) --add by maql 20100705 insert #dept_bills_dept(compid, billtype, billno, ratio, payway, payamt, empno) select gcl00c, 'gz', gcl01c, isnull(gcl35f, 0), gsc04c, case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gcl89f,0) * b.gsc05f / bill_total_amt end, gcl106c from #tbl_gcm10 a, #gsm03_tmp_01 b where a.gcl00c = b.gsc00c and a.gcl01c = b.gsc01c and b.gsc02c = 'gz' and (isnull(gcl106c, '') between @fromdepart and @todepart ) --add by maql 20100705 insert #dept_bills(compid, billtype, billno, ratio, payway, payamt, empno) select compid, billtype, billno, sum(ratio), payway, sum(payamt), empno from #dept_bills_dept a group by a.compid, a.billtype, a.billno, a.payway, empno drop table #tbl_gcm10 drop table #dept_bills_dept if (@SP031 = '1') --收银的时候直接结帐 begin insert #dept_bills(compid, billtype, billno, payway, payamt, empno) select ggb00c, 'gx', ggb01c, ggb27c, isnull(ggb11f, 0),ggb81c from ggm02 with (nolock), ggm01 with (nolock), gam26 z with (nolock) where gga00c = ggb00c and gga01c = ggb01c and gga00c = z.gaz02c and z.gaz01c = @incompid and gga80d between @fromdate and @todate and (isnull(ggb81c, '') between @fromdepart and @todepart ) --add by lyj 2007-09-06 and gga93c like @staff insert #customed_proj_count (compid, customed_num, empno) select gga00c, sum(isnull(ggb05f,0)),ggb81c from ggm02 with (nolock), ggm01 with (nolock), gam26 z with (nolock) where gga00c = ggb00c and gga01c = ggb01c and gga00c = z.gaz02c and z.gaz01c = @incompid and gga80d between @fromdate and @todate and (isnull(ggb81c, '') between @fromdepart and @todepart ) --add by lyj 2007-09-06 and gga93c like @staff group by ggb81c,gga00c insert #dept_bills(compid, billtype, billno, payway, payamt, empno) select ggc00c, 'gx', ggc01c, ggc27c, case when @sp048 = '2' then ggc21f else isnull(ggc11f, 0) * ggc14f end, ggc81c from ggm03 with (nolock), ggm01 with (nolock), gam26 z with (nolock) where ggc00c = gga00c and gga01c = ggc01c and gga00c = z.gaz02c and z.gaz01c = @incompid and gga80d between @fromdate and @todate and (isnull(ggc81c, '') between @fromdepart and @todepart ) --add by lyj 2007-09-06 and gga93c like @staff union all select ggc00c, 'gx', ggc01c, ggc27c, case when @sp048 = '2' then ggc23f else isnull(ggc11f, 0) * ggc16f end, ggc82c from ggm03 with (nolock), ggm01 with (nolock), gam26 z with (nolock) where ggc00c = gga00c and gga01c = ggc01c and gga00c = z.gaz02c and z.gaz01c = @incompid and gga80d between @fromdate and @todate and (isnull(ggc82c, '') between @fromdepart and @todepart ) --add by lyj 2007-09-06 and gga93c like @staff union all select ggc00c, 'gx', ggc01c, ggc27c, case when @sp048 = '2' then ggc25f else isnull(ggc11f, 0) * ggc18f end, ggc83c from ggm03 with (nolock), ggm01 with (nolock), gam26 z with (nolock) where ggc00c = gga00c and gga01c = ggc01c and gga00c = z.gaz02c and z.gaz01c = @incompid and gga80d between @fromdate and @todate and (isnull(ggc83c, '') between @fromdepart and @todepart ) --add by lyj 2007-09-06 and gga93c like @staff end else if (@SP031 = '2')--多账单统一结帐 begin insert #dept_bills(compid, billtype, billno, payway, payamt, empno) select ggb00c, 'gl', gga30c, ggb27c, isnull(ggb11f, 0), ggb81c from ggm02 with (nolock), ggm01 with (nolock), gam26 z with (nolock) where gga00c = ggb00c and gga01c = ggb01c and ggb00c = z.gaz02c and z.gaz01c = @incompid and gga80d between @fromdate and @todate and (isnull(ggb81c, '') between @fromdepart and @todepart ) --add by lyj 2007-09-06 and gga93c like @staff insert #customed_proj_count (customed_num, compid, empno) select sum(isnull(ggb05f,0)),gga00c, ggb81c from ggm02 with (nolock), ggm01 with (nolock), gam26 z with (nolock) where gga00c = ggb00c and gga01c = ggb01c and ggb00c = z.gaz02c and z.gaz01c = @incompid and gga80d between @fromdate and @todate and (isnull(ggb81c, '') between @fromdepart and @todepart ) --add by lyj 2007-09-06 and gga93c like @staff group by gga00c, ggb81c --insert ggm03 insert #dept_bills(compid, billtype, billno, payway, payamt, empno) select ggc00c, 'gl', gga30c, ggc27c, case when @sp048 = '2' then ggc21f else isnull(ggc11f, 0) * ggc14f end, ggc81c from ggm03 with (nolock), ggm01 with (nolock), gam26 z with (nolock) where ggc00c = gga00c and gga01c = ggc01c and gga00c = z.gaz02c and z.gaz01c = @incompid and gga80d between @fromdate and @todate and (isnull(ggc81c, '') between @fromdepart and @todepart ) --add by lyj 2007-09-06 and gga93c like @staff union all select ggc00c, 'gl', gga30c, ggc27c, case when @sp048 = '2' then ggc23f else isnull(ggc11f, 0) * ggc16f end, ggc82c from ggm03 with (nolock), ggm01 with (nolock), gam26 z with (nolock) where ggc00c = gga00c and gga01c = ggc01c and gga00c = z.gaz02c and z.gaz01c = @incompid and gga80d between @fromdate and @todate and (isnull(ggc82c, '') between @fromdepart and @todepart ) --add by lyj 2007-09-06 and gga93c like @staff union all select ggc00c, 'gl', gga30c, ggc27c, case when @sp048 = '2' then ggc25f else isnull(ggc11f, 0) * ggc18f end, ggc83c from ggm03 with (nolock), ggm01 with (nolock), gam26 z with (nolock) where ggc00c = gga00c and gga01c = ggc01c and gga00c = z.gaz02c and z.gaz01c = @incompid and gga80d between @fromdate and @todate and (isnull(ggc83c, '') between @fromdepart and @todepart ) --add by lyj 2007-09-06 and gga93c like @staff end --insert gqm01 --费用登记 insert #dept_bills(compid, billtype, billno, empno) select gqa00c, 'gv', gqa01c, gqa81c from gqm01 with (nolock), gam26 z with (nolock) where gqa00c = z.gaz02c and z.gaz01c = @incompid and gqa80d between @fromdate and @todate and (isnull(gqa81c, '') between @fromdepart and @todepart ) --add by lyj 2007-09-06 and gqa07c like @staff --insert gem01 --会籍变更里面的会员卡异动 -- insert #dept_bills(compid, billtype, billno) -- select gea00c, 'ge', gea01c -- from gem01 with (nolock), gam26 z with (nolock) -- where gea00c = z.gaz02c -- and z.gaz01c = @incompid -- and gea80d between @fromdate and @todate -- and (isnull(gea81c, '') between @fromdepart and @todepart or @fromdepart = '*') --add by lyj 2007-09-06 -- and gea91c like @staff -------把#gsm03_tmp_01和#departsesult里面符合条件的先insert到#gsm03_tmp临时表中 insert #gsm03_tmp(gsc00c, gsc01c, gsc02c, gsc04c, gsc05f, gsc10d, gsc11c, gsc12i, empno) select gsc00c, gsc01c, billtype, payway, isnull(payamt, 0), gsc10d, gsc11c, isnull(gsc12i, -1),empno from #dept_bills a, #gsm03_tmp_01 b where b.gsc01c = a.billno and b.gsc00c = a.compid and b.gsc02c = a.billtype and b.gsc04c = a.payway and b.gsc11c like @staff --Start竞争转卡业绩 add by lm 2015-10-13 create table #gem01_tmp -- 会员卡--异动单 ( gea00c char(10) not null, --门店编号 gea01c varchar(20) not null, --异动单号 gea07c varchar(20) null, --异动後卡号(只对 -卡种变更 有效) gea21c varchar(20) null, --销售人员(对 -卡种变更 有效) gea27f float null, --第一销售业绩 gea28c varchar(20) null, --第二销售(支付方式4) gea29f float null, --第二销售业绩 gea30c varchar(20) null, --第3销售 gea31f float null, --第3销售业绩 gea32c varchar(20) null, --第4销售 gea33f float null, --第4销售业绩 gea34c varchar(20) null, --第5销售 gea35f float null, --第5销售业绩 gea36c varchar(20) null, --第6销售 gea37f float null, --第6销售业绩 gea38c varchar(20) null, --第7销售 gea39f float null, --第7销售业绩 gea40c varchar(20) null, --第8销售 gea41f float null, --第8销售业绩 gea42c varchar(20) null, --第9销售 gea43f float null, --第9销售业绩 gea44c varchar(20) null, --第10销售 gea45f float null, --第10销售业绩 geb09i int null, --收支方向(1-收取 ,2 -支付) gea12f float null, --收费总额 geb10f float null, --金额 gea81c varchar(20) null, --部门1 gea82c varchar(20) null, --部门2 gea83c varchar(20) null, --部门3 gea84c varchar(20) null, --部门4 gea85c varchar(20) null, --部门5 gea86c varchar(20) null, --部门6 gea87c varchar(20) null, --部门7 gea88c varchar(20) null, --部门8 gea89c varchar(20) null, --部门9 gea90c varchar(20) null, --部门10 bill_total_amt float null -- 总业绩 ) insert #gem01_tmp(gea00c, gea01c, gea07c, gea21c, gea27f, gea28c, gea29f, gea30c, gea31f, gea32c, gea33f, gea34c, gea35f, gea36c, gea37f, gea38c, gea39f, gea40c, gea41f, gea42c, gea43f, gea44c, gea45f, gea12f,gea81c,gea82c,gea83c,gea84c,gea85c,gea86c,gea87c,gea88c,gea89c,gea90c,bill_total_amt) select gea00c, gea01c, gea07c, gea21c, gea27f, gea28c, gea29f, gea30c, gea31f, gea32c, gea33f, gea34c, gea35f, gea36c, gea37f, gea38c, gea39f, gea40c, gea41f, gea42c, gea43f, gea44c, gea45f, gea12f,gea81c,gea82c,gea83c,gea84c,gea85c,gea86c,gea87c,gea88c,gea89c,gea90c, isnull(gea27f,0)+isnull(gea29f,0)+isnull(gea31f,0)+isnull(gea33f,0)+isnull(gea35f,0)+isnull(gea37f,0)+isnull(gea39f,0)+isnull(gea41f,0)+isnull(gea43f,0)+isnull(gea45f,0) from gem01 a with(nolock), gam26 z with (nolock) where a.gea00c = z.gaz02c and z.gaz01c = @compid and gea80d between @fromdate and @todate --第一销售 insert #gsm03_tmp_gem01(gsc00c, gsc01c, gsc02c, gsc04c, gsc05f, gsc10d, gsc11c, gsc12i, empno) select gea00c, gea01c, 'ge', gsc04c, case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gea27f,0) * b.gsc05f / bill_total_amt end, gsc10d, gsc11c, gsc12i, gea81c from #gem01_tmp a with (nolock), #gsm03_tmp_01 b where a.gea00c = b.gsc00c and a.gea01c = b.gsc01c and b.gsc02c = 'ge' and ((gea81c between @fromdepart and @todepart) or @fromdepart = '*') and isnull(gea21c,'') <> '' --第二销售 insert #gsm03_tmp_gem01(gsc00c, gsc01c, gsc02c, gsc04c, gsc05f, gsc10d, gsc11c, gsc12i, empno) select gea00c, gea01c, 'ge', gsc04c, case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gea29f,0) * b.gsc05f / bill_total_amt end, gsc10d, gsc11c, gsc12i, gea82c from #gem01_tmp a with (nolock), #gsm03_tmp_01 b where a.gea00c = b.gsc00c and a.gea01c = b.gsc01c and b.gsc02c = 'ge' and ((gea82c between @fromdepart and @todepart) or @fromdepart = '*') and isnull(gea28c,'') <> '' --第三销售 insert #gsm03_tmp_gem01(gsc00c, gsc01c, gsc02c, gsc04c, gsc05f, gsc10d, gsc11c, gsc12i, empno) select gea00c, gea01c, 'ge', gsc04c, case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gea31f,0) * b.gsc05f / bill_total_amt end, gsc10d, gsc11c, gsc12i, gea83c from #gem01_tmp a with (nolock), #gsm03_tmp_01 b where a.gea00c = b.gsc00c and a.gea01c = b.gsc01c and b.gsc02c = 'ge' and ((gea83c between @fromdepart and @todepart) or @fromdepart = '*') and isnull(gea30c,'') <> '' --第四销售 insert #gsm03_tmp_gem01(gsc00c, gsc01c, gsc02c, gsc04c, gsc05f, gsc10d, gsc11c, gsc12i, empno) select gea00c, gea01c, 'ge', gsc04c, case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gea33f,0) * b.gsc05f / bill_total_amt end, gsc10d, gsc11c, gsc12i, gea84c from #gem01_tmp a with (nolock), #gsm03_tmp_01 b where a.gea00c = b.gsc00c and a.gea01c = b.gsc01c and b.gsc02c = 'ge' and ((gea84c between @fromdepart and @todepart) or @fromdepart = '*') and isnull(gea32c,'') <> '' --第五销售 insert #gsm03_tmp_gem01(gsc00c, gsc01c, gsc02c, gsc04c, gsc05f, gsc10d, gsc11c, gsc12i, empno) select gea00c, gea01c, 'ge', gsc04c, case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gea35f,0) * b.gsc05f / bill_total_amt end,gsc10d, gsc11c, gsc12i, gea85c from #gem01_tmp a with (nolock), #gsm03_tmp_01 b where a.gea00c = b.gsc00c and a.gea01c = b.gsc01c and b.gsc02c = 'ge' and ((gea85c between @fromdepart and @todepart) or @fromdepart = '*') and isnull(gea34c,'') <> '' --第六销售 insert #gsm03_tmp_gem01(gsc00c, gsc01c, gsc02c, gsc04c, gsc05f, gsc10d, gsc11c, gsc12i, empno) select gea00c, gea01c, 'ge', gsc04c, case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gea37f,0) * b.gsc05f / bill_total_amt end, gsc10d, gsc11c, gsc12i, gea86c from #gem01_tmp a with (nolock), #gsm03_tmp_01 b where a.gea00c = b.gsc00c and a.gea01c = b.gsc01c and b.gsc02c = 'ge' and ((gea86c between @fromdepart and @todepart) or @fromdepart = '*') and isnull(gea36c,'') <> '' --第七销售 insert #gsm03_tmp_gem01(gsc00c, gsc01c, gsc02c, gsc04c, gsc05f, gsc10d, gsc11c, gsc12i, empno) select gea00c, gea01c, 'ge', gsc04c, case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gea39f,0) * b.gsc05f / bill_total_amt end, gsc10d, gsc11c, gsc12i, gea87c from #gem01_tmp a with (nolock), #gsm03_tmp_01 b where a.gea00c = b.gsc00c and a.gea01c = b.gsc01c and b.gsc02c = 'ge' and ((gea87c between @fromdepart and @todepart) or @fromdepart = '*') and isnull(gea38c,'') <> '' --第八销售 insert #gsm03_tmp_gem01(gsc00c, gsc01c, gsc02c, gsc04c, gsc05f, gsc10d, gsc11c, gsc12i, empno) select gea00c, gea01c, 'ge', gsc04c, case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gea41f,0) * b.gsc05f / bill_total_amt end, gsc10d, gsc11c, gsc12i,gea88c from #gem01_tmp a with (nolock), #gsm03_tmp_01 b where a.gea00c = b.gsc00c and a.gea01c = b.gsc01c and b.gsc02c = 'ge' and ((gea88c between @fromdepart and @todepart) or @fromdepart = '*') and isnull(gea40c,'') <> '' --第九销售 insert #gsm03_tmp_gem01(gsc00c, gsc01c, gsc02c, gsc04c, gsc05f, gsc10d, gsc11c, gsc12i, empno) select gea00c, gea01c, 'ge', gsc04c, case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gea43f,0) * b.gsc05f / bill_total_amt end, gsc10d, gsc11c, gsc12i,gea89c from #gem01_tmp a with (nolock), #gsm03_tmp_01 b where a.gea00c = b.gsc00c and a.gea01c = b.gsc01c and b.gsc02c = 'ge' and ((gea89c between @fromdepart and @todepart) or @fromdepart = '*') and isnull(gea42c,'') <> '' --第十销售 insert #gsm03_tmp_gem01(gsc00c, gsc01c, gsc02c, gsc04c, gsc05f, gsc10d, gsc11c, gsc12i, empno) select gea00c, gea01c, 'ge', gsc04c, case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gea45f,0) * b.gsc05f / bill_total_amt end, gsc10d, gsc11c, gsc12i,gea90c from #gem01_tmp a with (nolock), #gsm03_tmp_01 b where a.gea00c = b.gsc00c and a.gea01c = b.gsc01c and b.gsc02c = 'ge' and ((gea90c between @fromdepart and @todepart) or @fromdepart = '*') and isnull(gea44c,'') <> '' insert #gsm03_tmp(gsc00c,gsc01c, gsc02c, gsc04c, gsc05f, gsc10d, gsc11c, gsc12i, empno) select gsc00c, gsc01c, gsc02c, gsc04c, sum(gsc05f), gsc10d, gsc11c, gsc12i, empno from #gsm03_tmp_gem01 group by gsc00c, gsc01c, gsc02c, gsc04c, gsc10d, gsc11c, gsc12i, empno --End --删除临时表 drop table #gsm03_tmp_01 drop table #gsm03_tmp_gem01 --#gsm03_tmp --统计的中间结果 create table #tbl_stat_fill ( idd int identity not null, compid varchar(20) not null, empno varchar(20) not null, paymode varchar(10) not null, -- 支付方式 mode int null, -- 卡销售9, 0-存款 1-取款 5-欠款 6还款 amt float null default 0, -- 合计的金额 primary key(idd) ) --create nonclustered index idx_tbl_stat_fill on #tbl_stat_fill(compid, empno, paymode) create table #tbl_profit ( idd int identity not null, code varchar(20) not null, compid varchar(20) not null, empno varchar(20) not null, itemname varchar(60) not null, is_add int not null, amt float null default 0, is_leaf int not null, payment varchar(10) null, -- 支付方式 kind int null, -- 0 充值 1卡销售 9产品销售 2 服务 -1 取款 8 保证金收入 6 还款 primary key(idd) ) --create nonclustered index idx_tbl_profit on #tbl_profit(compid, empno, payment) insert #tbl_stat_fill(paymode, mode, amt,compid, empno) select gsc04c, 9, isnull(sum(isnull(a.gsc05f, 0)), 0),gsc00c, empno from #gsm03_tmp a where a.gsc02c = 'gn' group by gsc04c, gsc00c, empno declare @comp varchar(20) declare @empno varchar(20) declare tbl_profit_gn cursor for select compid, empno from #emp_info open tbl_profit_gn fetch tbl_profit_gn into @comp, @empno while @@fetch_status = 0 begin declare @customed_proj_count float select @customed_proj_count = customed_num from #customed_proj_count where compid = @comp and empno = @empno insert #tbl_profit(compid, empno, code, itemname, is_add, is_leaf, amt) values(@comp, @empno, 'HB', '总客数', 0, 1, @customed_proj_count) insert #tbl_profit(compid, empno, code, itemname, is_add, is_leaf, amt) values(@comp, @empno, '11', '收入', 1, 0, 0) insert #tbl_profit(compid, empno, code, itemname, is_add, is_leaf, amt, payment, kind) values(@comp, @empno, '1101', '卡销售收入', @add_card_sale, 0, 0, '', 1) insert #tbl_profit(compid, empno, code, itemname, is_add, is_leaf, payment, kind) values(@comp, @empno, '110101', '卡销售收入(扣欠款)-现金', @add_card_sale, 1, '1', 1) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '110101' and b.paymode = '1' and a.compid = @comp and a.empno = @empno insert #tbl_profit(compid, empno, code, itemname, is_add, is_leaf, payment, kind) values(@comp, @empno, '110102', '卡销售收入(扣欠款)-银行卡', @add_card_sale, 1, '6', 1) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '110102' and b.paymode = '6' and a.compid = @comp and a.empno = @empno insert #tbl_profit(compid,empno, code, itemname, is_add, is_leaf, payment, kind) values(@comp, @empno, '1101Z1', '卡销售收入(扣欠款)-支付宝', @add_card_sale, 1, 'ZFB', 1) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '1101Z1' and b.paymode = 'ZFB' and a.compid = @comp and a.empno = @empno insert #tbl_profit(compid,empno, code, itemname, is_add, is_leaf, payment, kind) values(@comp, @empno, '1101W1', '卡销售收入(扣欠款)-微信', @add_card_sale, 1, 'WX', 1) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '1101W1' and b.paymode = 'WX' and a.compid = @comp and a.empno = @empno insert #tbl_profit(compid,empno, code, itemname, is_add, is_leaf) values(@comp, @empno, '110103', '卡销售收入-买卡欠款', 0, 1) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '110103' and b.paymode = '5' and a.compid = @comp and a.empno = @empno fetch tbl_profit_gn into @comp, @empno end close tbl_profit_gn deallocate tbl_profit_gn -- 如下为账户异动的处理 insert #tbl_stat_fill(paymode, mode, amt,compid, empno) select gsc04c, gsc12i, isnull(sum(isnull(a.gsc05f, 0)), 0),gsc00c, empno from #gsm03_tmp a where a.gsc02c = 'gz' group by gsc04c, gsc12i,gsc00c, empno declare tbl_profit_gz cursor for select compid, empno from #emp_info open tbl_profit_gz fetch tbl_profit_gz into @comp, @empno while @@fetch_status = 0 begin -- 卡充值 insert #tbl_profit(compid, empno, code, itemname, is_add, is_leaf, payment, kind) values(@comp, @empno, '110104', '卡销售收入-充值-现金(扣欠款)', @add_card_sale, 1, '1', 0) -- 卡充值-现金 update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '110104' and b.paymode = '1' and b.mode = 0 and a.compid = @comp and a.empno = @empno insert #tbl_profit(compid, empno, code, itemname, is_add, is_leaf, payment, kind) values(@comp, @empno, '110105', '卡销售收入-充值-银行卡', @add_card_sale, 1, '6', 0) -- 银行卡支付 update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '110105' and b.paymode = '6' and b.mode = 0 and a.compid = @comp and a.empno = @empno -- 考虑银行卡取款 update a set a.amt = isnull(a.amt, 0) + isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '110105' and b.paymode = '6' and b.mode = 1 and a.compid = @comp and a.empno = @empno insert #tbl_profit(compid, empno, code, itemname, is_add, is_leaf, payment, kind) values(@comp, @empno, '1101Z2', '卡销售收入-充值-支付宝', @add_card_sale, 1, 'ZFB', 0) -- 支付宝支付 update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '1101Z2' and b.paymode = 'ZFB' and b.mode = 0 and a.compid = @comp and a.empno = @empno -- 考虑支付宝取款 update a set a.amt = isnull(a.amt, 0) + isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '1101Z2' and b.paymode = 'ZFB' and b.mode = 1 and a.compid = @comp and a.empno = @empno insert #tbl_profit(compid, empno, code, itemname, is_add, is_leaf, payment, kind) values(@comp, @empno, '1101W2', '卡销售收入-充值-微信', @add_card_sale, 1, 'WX', 0) -- 支付宝支付 update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '1101W2' and b.paymode = 'WX' and b.mode = 0 and a.compid = @comp and a.empno = @empno -- 考虑支付宝取款 update a set a.amt = isnull(a.amt, 0) + isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '1101W2' and b.paymode = 'WX' and b.mode = 1 and a.compid = @comp and a.empno = @empno insert #tbl_profit(compid, empno, code, itemname, is_add, is_leaf) values(@comp, @empno, '110106', '卡销售收入-充值欠款', 0, 1) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '110106' and b.paymode = '5' and b.mode = 0 and a.compid = @comp and a.empno = @empno -- 卡还款 insert #tbl_profit(compid, empno, code, itemname, is_add, is_leaf, payment, kind) values(@comp, @empno, '110107', '卡销售收入-还款-现金', @add_card_sale, 1, '1', 6) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '110107' and b.paymode = '1' and b.mode = 6 and a.compid = @comp and a.empno = @empno insert #tbl_profit(compid, empno, code, itemname, is_add, is_leaf, payment, kind) values(@comp, @empno, '110108', '卡销售收入-还款-银行卡', @add_card_sale, 1, '6', 6) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '110108' and b.paymode = '6' and b.mode = 6 and a.compid = @comp and a.empno = @empno insert #tbl_profit(compid, empno, code, itemname, is_add, is_leaf, payment, kind) values(@comp, @empno, '1101Z3', '卡销售收入-还款-支付宝', @add_card_sale, 1, 'ZFB', 6) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '1101Z3' and b.paymode = 'ZFB' and b.mode = 6 and a.compid = @comp and a.empno = @empno insert #tbl_profit(compid, empno, code, itemname, is_add, is_leaf, payment, kind) values(@comp, @empno, '1101W3', '卡销售收入-还款-微信', @add_card_sale, 1, 'WX', 6) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '1101W3' and b.paymode = 'WX' and b.mode = 6 and a.compid = @comp and a.empno = @empno -- 保证金转销售 declare @draw_preamt float select @draw_preamt = sum(amt) from #tbl_stat_fill where paymode = '&' insert #tbl_profit(compid, empno, code, itemname, is_add, is_leaf, amt, payment, kind) values(@comp, @empno, '110109', '卡销售收入-保证金转销售', 0, 1, isnull(@draw_preamt, 0), '&', 0) -- 经理签单 select @itemtype = '8' select @itemname = gsb03c from gsm02 with (nolock) where gsb01c = '3' and gsb02c = @itemtype and gsb00c = @comp insert #tbl_profit(compid, empno, code, itemname, is_add, is_leaf, payment, kind) values(@comp, @empno, '110110', '卡销售收入(扣欠款)-' + isnull(@itemname, ''), 0, 1, @itemtype, 1) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '110110' and b.paymode = @itemtype and b.mode = 9 and a.compid = @comp and a.empno = @empno insert #tbl_profit(compid,empno,code, itemname, is_add, is_leaf, payment, kind) values(@comp, @empno, '110111', '卡销售收入-充值-' + isnull(@itemname, ''), 0, 1, @itemtype, 0) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '110111' and b.paymode = @itemtype and b.mode = 0 and a.compid = @comp and a.empno = @empno insert #tbl_profit(compid, empno, code, itemname, is_add, is_leaf, payment, kind) values(@comp, @empno, '110112', '卡销售收入-还款-' + isnull(@itemname, ''), 0, 1, @itemtype, 6) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '110112' and b.paymode = @itemtype and b.mode = 6 and a.compid = @comp and a.empno = @empno -- 储值账户二, 按理应该不会有, 但yongqi作为转账了, 记得为0的删除就可以 select @itemtype = '9' select @itemname = gsb03c from gsm02 with (nolock) where gsb01c = '3' and gsb02c = @itemtype and gsb00c = @comp insert #tbl_profit(compid, empno, code, itemname, is_add, is_leaf, payment, kind) values(@comp, @empno, '110115', '卡销售收入(扣欠款)-' + isnull(@itemname, ''), 0, 1, @itemtype, 1) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '110115' and b.paymode = @itemtype and b.mode = 9 and a.compid = @comp and a.empno = @empno -- 转账方式下的充值,也应该扣除欠款部分 insert #tbl_profit(compid, empno, code, itemname, is_add, is_leaf, payment, kind) values(@comp, @empno,'110116', '卡销售收入-充值-' + isnull(@itemname, '') + '(扣欠款)', 0, 1, @itemtype, 0) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '110116' and b.paymode = @itemtype and b.mode = 0 and a.compid = @comp and a.empno = @empno insert #tbl_profit(compid, empno, code, itemname, is_add, is_leaf, payment, kind) values(@comp, @empno, '110117', '卡销售收入-还款-' + isnull(@itemname, ''), 0, 1, @itemtype, 6) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '110117' and b.paymode = @itemtype and b.mode = 6 and a.compid = @comp and a.empno = @empno -- 会员卡异动收费, 就算是现金 insert #tbl_profit(compid, empno, code, itemname, is_add, is_leaf, payment, kind) values(@comp, @empno, '110118', '卡销售收入-异动收费', @add_card_sale, 1, '1', 0) update #tbl_profit set amt = isnull(@amt, 0) - isnull(@amt2, 0) where code = '110118' and compid = @comp and empno = @empno -- 现金取款, 取款kind定义为-1 insert #tbl_profit(compid, empno, code, itemname, is_add, is_leaf, payment, kind) values(@comp, @empno, '110119', '卡销售收入-取款-现金', @add_card_sale, 1, '1', -1) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '110119' and b.paymode = '1' and b.mode = 1 and a.compid = @comp and a.empno = @empno -- 内调, 记得为0的删除就可以 select @itemtype = '0' select @itemname = gsb03c from gsm02 with (nolock) where gsb01c = '3' and gsb02c = @itemtype and gsb00c = @comp insert #tbl_profit(compid, empno, code, itemname, is_add, is_leaf, payment, kind) values(@comp, @empno, '110120', '卡销售收入(扣欠款)-' + isnull(@itemname, ''), @isadd_0 * @add_card_sale, 1, @itemtype, 1) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '110120' and b.paymode = @itemtype and b.mode = 9 and a.compid = @comp and a.empno = @empno insert #tbl_profit(compid, empno, code, itemname, is_add, is_leaf, payment, kind) values(@comp, @empno, '110121', '卡销售收入-充值-' + isnull(@itemname, ''), @isadd_0 * @add_card_sale, 1, @itemtype, 0) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '110121' and b.paymode = @itemtype and b.mode = 0 and a.compid = @comp and a.empno = @empno insert #tbl_profit(compid, empno, code, itemname, is_add, is_leaf, payment, kind) values(@comp, @empno, '110122', '卡销售收入-还款-' + isnull(@itemname, ''), @isadd_0 * @add_card_sale, 1, @itemtype, 6) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '110122' and b.paymode = @itemtype and b.mode = 6 and a.compid = @comp and a.empno = @empno insert #tbl_profit(compid, empno, code, itemname, is_add, is_leaf, payment, kind) values(@comp, @empno, '110123', '卡销售收入-取款-' + isnull(@itemname, ''), @isadd_0 * @add_card_sale, 1, @itemtype, -1) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '110123' and b.paymode = @itemtype and b.mode = 1 and a.compid = @comp and a.empno = @empno -- 抵用券, 记得为0的删除就可以 select @itemtype = '3' select @itemname = gsb03c from gsm02 with (nolock) where gsb01c = '3' and gsb02c = @itemtype and gsb00c = @comp insert #tbl_profit(compid, empno, code, itemname, is_add, is_leaf, payment, kind) values(@comp, @empno, '110125', '卡销售收入(扣欠款)-' + isnull(@itemname, ''), @isadd_3 * @add_card_sale, 1, @itemtype, 1) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '110125' and b.paymode = @itemtype and b.mode = 9 and a.compid = @comp and a.empno = @empno insert #tbl_profit(compid, empno, code, itemname, is_add, is_leaf, payment, kind) values(@comp, @empno, '110126', '卡销售收入-充值-' + isnull(@itemname, ''), @isadd_3 * @add_card_sale, 1, @itemtype, 0) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '110126' and b.paymode = @itemtype and b.mode = 0 and a.compid = @comp and a.empno = @empno insert #tbl_profit(compid, empno, code, itemname, is_add, is_leaf, payment, kind) values(@comp, @empno, '110127', '卡销售收入-还款-' + isnull(@itemname, ''), @isadd_3 * @add_card_sale, 1, @itemtype, 6) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '110127' and b.paymode = @itemtype and b.mode = 6 and a.compid = @comp and a.empno = @empno -- 抵用券B, 记得为0的删除就可以 select @itemtype = '$' select @itemname = gsb03c from gsm02 with (nolock) where gsb01c = '3' and gsb02c = @itemtype and gsb00c = @comp insert #tbl_profit(compid, empno, code, itemname, is_add, is_leaf, payment, kind) values(@comp, @empno, '110130', '卡销售收入(扣欠款)-' + isnull(@itemname, ''), @isadd_$ * @add_card_sale, 1, @itemtype, 1) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '110130' and b.paymode = @itemtype and b.mode = 9 and a.compid = @comp and a.empno = @empno insert #tbl_profit(compid, empno, code, itemname, is_add, is_leaf, payment, kind) values(@comp, @empno, '110131', '卡销售收入-充值-' + isnull(@itemname, ''), @isadd_$ * @add_card_sale, 1, @itemtype, 0) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '110131' and b.paymode = @itemtype and b.mode = 0 and a.compid = @comp and a.empno = @empno insert #tbl_profit(compid, empno, code, itemname, is_add, is_leaf, payment, kind) values(@comp, @empno, '110132', '卡销售收入-还款-' + isnull(@itemname, ''), @isadd_$ * @add_card_sale, 1, @itemtype, 6) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '110132' and b.paymode = @itemtype and b.mode = 6 and a.compid = @comp and a.empno = @empno -- 自己增加的$开头的支付方式 declare @pay_$ varchar(10) declare @code_char varchar(10) declare @isadd_ur float -- 子定义的支付方式是为计入营业收入 declare @code_int int select @code_int = 110150 declare cur_each_pay cursor for select gsb02c from gsm02 with (nolock) where gsb00c = @comp and gsb01c = '3' and gsb02c like '$%' and len(gsb02c) > 1 order by gsb02c open cur_each_pay fetch cur_each_pay into @pay_$ while @@fetch_status = 0 begin select @code_char = convert(varchar(10), @code_int) select @itemtype = @pay_$ select @itemname = gsb03c from gsm02 with (nolock) where gsb01c = '3' and gsb02c = @itemtype and gsb00c = @comp select @isadd_ur = gso07f from gsm15 with (nolock) where gso00c = @comp and gso02c = @pay_$ select @isadd_ur = isnull(@isadd_ur, 1) -- 默认算收入 insert #tbl_profit(compid, empno, code, itemname, is_add, is_leaf, payment, kind) values(@comp, @empno, @code_char, '卡销售收入(扣欠款)-' + isnull(@itemname, ''), @isadd_ur * @add_card_sale, 1, @pay_$, 1) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = @code_char and b.paymode = @itemtype and b.mode = 9 and a.compid = @comp and a.empno = @empno select @code_int = @code_int + 1 select @code_char = convert(varchar(10), @code_int) insert #tbl_profit(compid, empno, code, itemname, is_add, is_leaf, payment, kind) values(@comp, @empno, @code_char, '卡销售收入-充值-' + isnull(@itemname, ''), @isadd_ur * @add_card_sale, 1, @pay_$, 0) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = @code_char and b.paymode = @itemtype and b.mode = 0 and a.compid = @comp and a.empno = @empno select @code_int = @code_int + 1 select @code_char = convert(varchar(10), @code_int) insert #tbl_profit(compid, empno, code, itemname, is_add, is_leaf, payment, kind) values(@comp, @empno, @code_char, '卡销售收入-还款-' + isnull(@itemname, ''), @isadd_ur * @add_card_sale, 1, @pay_$, 6) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = @code_char and b.paymode = @itemtype and b.mode = 6 and a.compid = @comp and a.empno = @empno select @code_int = @code_int + 1 select @code_char = convert(varchar(10), @code_int) fetch cur_each_pay into @pay_$ end close cur_each_pay deallocate cur_each_pay -- 积分买卡,只有在充值的时候可能存在,也无法作为卡销售的收入 select @itemtype = '7' select @itemname = gsb03c from gsm02 with (nolock) where gsb01c = '3' and gsb02c = @itemtype and gsb00c = @comp insert #tbl_profit(compid,empno,code, itemname, is_add, is_leaf, payment, kind) values(@comp,@empno,'110133', '卡销售收入-充值-' + isnull(@itemname, ''), 0, 1, @itemtype, 0) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '110133' and b.paymode = @itemtype and b.mode = 0 and a.compid = @comp and a.empno = @empno --end fetch tbl_profit_gz into @comp, @empno end close tbl_profit_gz deallocate tbl_profit_gz --end -- 卡的变更等收入,也属于卡的销售收入 truncate table #tbl_stat_fill ---------start-----2016.06.24 insert #tbl_stat_fill(paymode, amt,compid, empno) select gsc04c, isnull(sum(isnull(a.gsc05f, 0)), 0),gsc00c, empno from #gsm03_tmp a where a.gsc02c = 'ge' group by gsc04c,gsc00c, empno create table #gsm02 ( pay varchar(10) not null, payname varchar(40) null ) declare tbl_profit_ge cursor for select compid, empno from #emp_info open tbl_profit_ge fetch tbl_profit_ge into @comp, @empno while @@fetch_status = 0 begin insert #tbl_profit(compid,empno,code, itemname, is_add, is_leaf, payment, kind) values(@comp,@empno,'110140', '卡销售收入-变更-现金', @add_card_sale, 1, '1', 0) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '110140' and b.paymode = '1' and a.compid = @comp and a.empno = @empno insert #tbl_profit(compid,empno, code, itemname, is_add, is_leaf, payment, kind) values(@comp,@empno,'110141', '卡销售收入-变更-银行卡', @add_card_sale, 1, '6', 0) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '110141' and b.paymode = '6' and a.compid = @comp and a.empno = @empno insert #tbl_profit(compid,empno, code, itemname, is_add, is_leaf, payment, kind) values(@comp,@empno,'1101Z4', '卡销售收入-变更-支付宝', @add_card_sale, 1, 'ZFB', 0) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '1101Z4' and b.paymode = 'ZFB' and a.compid = @comp and a.empno = @empno insert #tbl_profit(compid,empno,code, itemname, is_add, is_leaf, payment, kind) values(@comp,@empno,'110145', '卡销售收入-变更-欠款', @add_card_sale, 0, '5', 0) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '110145' and b.paymode = '5' and a.compid = @comp and a.empno = @empno insert #tbl_profit(compid,empno, code, itemname, is_add, is_leaf, payment, kind) values(@comp,@empno,'1101W4', '卡销售收入-变更-微信', @add_card_sale, 1, 'WX', 0) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '1101W4' and b.paymode = 'WX' and a.compid = @comp and a.empno = @empno --Start 卡种变更所有支付方式 add by lm 2015.12.28 set @id = 0 set @code_no = '' delete from #gsm02 insert #gsm02 (pay, payname) select gsb02c, gsb03c from gsm02 a with(nolock) where a.gsb00c = @comp and a.gsb01c = '3' and a.gsb02c not in ('ZFB', 'WX', '1', '6', '5') while (@id = 0) begin if exists(select 1 from #gsm02) begin select top 1 @itemtype = pay, @itemname = payname from #gsm02 select @code_no = '110140' + @itemtype insert #tbl_profit(compid,empno,code, itemname, is_add, is_leaf, payment, kind) values(@comp,@empno,@code_no, '卡销售收入-变更-' + isnull(@itemname, ''), @add_card_sale, 1, @itemtype, 0) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = @code_no and b.paymode = @itemtype and a.compid = @comp and a.empno = @empno set @code_no = '' set @id = 0 delete from #gsm02 where pay = @itemtype end else begin set @id = 1 end end -- 统计卡销售收入 select @amt = 0 if @calmode = 0 begin select @amt = sum(isnull(amt * is_add, 0)) from #tbl_profit where code like '1101%' and is_leaf = 1 and compid = @comp and empno = @empno end else begin select @amt = sum(isnull(amt, 0)) from #tbl_profit where code like '1101%' and is_leaf = 1 and (isnull(payment, '') <> '&' and isnull(payment, '') <> '7' and isnull(payment, '') <> '') and compid = @comp and empno = @empno end update #tbl_profit set amt = isnull(@amt, 0) where code = '1101' and compid = @comp and empno = @empno fetch tbl_profit_ge into @comp, @empno end close tbl_profit_ge deallocate tbl_profit_ge truncate table #tbl_stat_fill -- 服务收入, 包括产品部分 create table #tbl_stat_fill_2 ( idd int identity not null, compid varchar(20) not null, empno varchar(20) not null, paymode varchar(10) null, mode int null, amt float null, primary key(idd) ) create nonclustered index tbl_stat_fill_2_idx_01 on #tbl_stat_fill_2(compid, empno) insert #tbl_stat_fill_2(paymode, mode, amt,compid, empno) select gsc04c, 0, isnull(sum(isnull(a.gsc05f, 0)), 0),gsc00c, empno from #gsm03_tmp a where a.gsc02c = 'gx' group by gsc04c,gsc00c, empno insert #tbl_stat_fill_2(paymode, mode, amt, compid, empno) select gsc04c, 1, isnull(sum(isnull(a.gsc05f, 0)), 0),gsc00c, empno from #gsm03_tmp a where a.gsc02c = 'gl' group by gsc04c,gsc00c, empno insert #tbl_stat_fill(paymode, mode, amt, compid, empno) select isnull(paymode, ''), 0, sum(isnull(amt, 0)), compid, empno from #tbl_stat_fill_2 group by isnull(paymode, ''), compid, empno drop table #tbl_stat_fill_2 -- 必须要的数据 -- 为了提高性能,把需要用倒的ggm01数据转移到临时表中 (1) create table #ggm01_1 ( gga00c varchar(20) not null, gga01c varchar(20) not null, gga93c varchar(20) null, gga07c varchar(10) null, --会员卡类别 ggb34f float null, --实际营业额折算率 ggc30f float null --实际营业额折算比率 primary key(gga00c, gga01c) ) insert #ggm01_1(gga00c, gga01c,gga93c, gga07c) select gga00c, gga01c, gga93c,gga07c from ggm01 a with (nolock), gam26 z with(nolock) where gga00c = z.gaz02c and gga80d >= @fromdate and gga80d <= @todate and z.gaz01c = @incompid -- 把明细的服务项目的卡付计算出来 create table #tbl_card_pay ( paycode varchar(20) not null, compid varchar(20) not null, empno varchar(20) not null, acctid int null, amt float null, code varchar(20) null, pay_name varchar(30) null, --primary key(paycode) ) declare tbl_profit_gx_gl cursor for select compid, empno from #emp_info open tbl_profit_gx_gl fetch tbl_profit_gx_gl into @comp, @empno while @@fetch_status = 0 begin insert #tbl_profit(compid, empno,code, itemname, is_add, is_leaf) values(@comp,@empno,'1102', '服务收入(包括产品销售)', 1, 0) insert #tbl_profit(compid, empno,code, itemname, is_add, is_leaf, payment, kind) values(@comp,@empno,'110201', '服务收入-现金', 1, 1, '1', 2) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '110201' and b.paymode = '1' and a.compid = @comp and a.empno= @empno insert #tbl_profit(compid, empno,code, itemname, is_add, is_leaf, payment, kind) values(@comp,@empno,'110202', '服务收入-银行卡', 1, 1, '6', 2) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '110202' and b.paymode = '6' and a.compid = @comp and a.empno= @empno insert #tbl_profit(compid, empno,code, itemname, is_add, is_leaf, payment, kind) values(@comp,@empno,'1102Z1', '服务收入-支付宝', 1, 1, 'ZFB', 2) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '1102Z1' and b.paymode = 'ZFB' and a.compid = @comp and a.empno= @empno insert #tbl_profit(compid, empno,code, itemname, is_add, is_leaf, payment, kind) values(@comp,@empno,'1102W1', '服务收入-微信', 1, 1, 'WX', 2) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '1102W1' and b.paymode = 'WX' and a.compid = @comp and a.empno= @empno -- 经理签单 select @itemtype = '8' select @itemname = gsb03c from gsm02 with (nolock) where gsb01c = '3' and gsb02c = @itemtype and gsb00c = @comp insert #tbl_profit(compid, empno,code, itemname, is_add, is_leaf, payment, kind) values(@comp,@empno,'110203', '服务收入-' + isnull(@itemname, ''), 0, 1, @itemtype, 2) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '110203' and b.paymode = @itemtype and a.compid = @comp and a.empno= @empno -- 抵用券 select @itemtype = '3' select @itemname = gsb03c from gsm02 with (nolock) where gsb01c = '3' and gsb02c = @itemtype and gsb00c = @comp insert #tbl_profit(compid, empno,code, itemname, is_add, is_leaf, payment, kind) values(@comp,@empno,'110204', '服务收入-' + isnull(@itemname, ''), @isadd_3, 1, @itemtype, 2) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '110204' and b.paymode = @itemtype and a.compid = @comp and a.empno= @empno -- 第二抵用券 select @itemtype = '$' select @itemname = gsb03c from gsm02 with (nolock) where gsb01c = '3' and gsb02c = @itemtype and gsb00c = @comp insert #tbl_profit(compid, empno,code, itemname, is_add, is_leaf, payment, kind) values(@comp,@empno,'110208', '服务收入-' + isnull(@itemname, ''), @isadd_$, 1, @itemtype, 2) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '110208' and b.paymode = @itemtype and a.compid = @comp and a.empno= @empno select @code_int = 110250 declare cur_each_pay_2 cursor for select gsb02c from gsm02 with (nolock) where gsb00c = @comp and gsb01c = '3' and gsb02c like '$%' and len(gsb02c) > 1 order by gsb02c open cur_each_pay_2 fetch cur_each_pay_2 into @pay_$ while @@fetch_status = 0 begin select @code_char = convert(varchar(10), @code_int) select @itemtype = @pay_$ select @itemname = gsb03c from gsm02 with (nolock) where gsb01c = '3' and gsb02c = @itemtype and gsb00c = @comp select @isadd_ur = gso07f from gsm15 with (nolock) where gso00c = @incompid and gso02c = @pay_$ select @isadd_ur = isnull(@isadd_ur, 1) -- 默认算收入 insert #tbl_profit(compid, empno,code, itemname, is_add, is_leaf, payment, kind) values(@comp,@empno,@code_char, '服务收入-' + isnull(@itemname, ''), @isadd_ur, 1, @pay_$, 2) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = @code_char and b.paymode = @itemtype and a.compid = @comp and a.empno= @empno select @code_int = @code_int + 1 select @code_char = convert(varchar(10), @code_int) fetch cur_each_pay_2 into @pay_$ end close cur_each_pay_2 deallocate cur_each_pay_2 -- 内调 select @itemtype = '0' select @itemname = gsb03c from gsm02 with (nolock) where gsb01c = '3' and gsb02c = @itemtype and gsb00c = @comp insert #tbl_profit(compid, empno,code, itemname, is_add, is_leaf, payment, kind) values(@comp,@empno,'110209', '服务收入-' + isnull(@itemname, ''), @isadd_0, 1, @itemtype, 2) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '110209' and b.paymode = @itemtype and a.compid = @comp and a.empno= @empno insert #tbl_profit(compid, empno,code, itemname, is_add, is_leaf) values(@comp,@empno,'110207', '服务收入-欠款', 0, 1) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '110207' and b.paymode = '5' and a.compid = @comp and a.empno= @empno -- 服务收入-取用储值 insert #tbl_profit(compid, empno,code, itemname, is_add, is_leaf, payment, kind) values(@comp,@empno,'110205', '服务收入-取用储值', @add_card_svr, 0, 'CARD', 2) truncate table #tbl_card_pay insert #tbl_card_pay(compid,empno,paycode, amt) select compid,empno,paymode, sum(amt) from #tbl_stat_fill where (paymode = '#' or paymode = '4' or paymode = '9' or (paymode >= 'A' and paymode <= 'Z')) -- 不包括积分 and paymode not in ('WX','ZFB') group by compid,empno,paymode update a set a.acctid = b.gsn02c from #tbl_card_pay a, gsm14 b with (nolock) where a.paycode = b.gsn01c and a.compid = @comp and a.empno = @empno update a set a.code = right('00' + paycode, 2) from #tbl_card_pay a where a.compid = @comp and a.empno = @empno update a set a.pay_name = b.gsb03c from #tbl_card_pay a, gsm02 b with (nolock) where a.paycode = b.gsb02c and gsb00c = @comp and gsb01c = '3' --修复 卡收入 没有值 根据部门重复 显示 卡值 insert #tbl_profit(compid,empno,code, itemname, is_add, is_leaf, payment, kind, amt) select @comp,@empno,'110205' + isnull(code, '00'), '服务收入-取用储值-' + pay_name, @add_card_svr, 1, paycode, 2, amt from #tbl_card_pay where compid = @comp and empno = @empno -- 积分需要单独列出来 declare @act varchar(20) declare @act_name varchar(50) select @act = '7' -- 积分的支付 select @act_name = gsb03c from gsm02 with (nolock) where gsb00c = @comp and gsb01c = '3' and gsb02c = @act insert #tbl_profit(compid,empno,code, itemname, is_add, is_leaf, payment, kind) values(@comp,@empno,'11020507', '服务收入-积分支付-' + isnull(@act_name, ''), 0, 1, @act, 2) -- 积分永远不作为服务收入 is_add = 0 update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.compid = b.compid and a.empno = b.empno and a.code = '11020507' and b.paymode = @act and a.compid = @comp and a.empno = @empno -- 合计所有取用储值, 把积分支付单独扣除 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit where (code like '110205%' and code <> '11020507') and is_leaf = 1 and compid = @comp and empno = @empno update #tbl_profit set amt = isnull(@amt, 0) where code = '110205' and compid = @comp and empno = @empno -- START -- 统计因为项目或产品销售的欠款的还款收入,只统计现金还款及银行卡的还款 -- 服务中欠款的可能行很小,所以默认为产品的还款 select @amt = 0 select @amt = isnull(sum(isnull(a.gsc05f, 0)), 0) from #gsm03_tmp a where a.gsc02c = 'gz' and isnull(a.isnotcard, 0) = 1 and gsc04c = '1' and gsc12i = 6 and gsc00c = @comp and empno = @empno insert #tbl_profit(compid,empno,code, itemname, is_add, is_leaf, amt, payment, kind) values(@comp,@empno,'110230', '服务收入-还款-现金', 1, 1, @amt, '1', 2) select @amt = 0 select @amt = isnull(sum(isnull(a.gsc05f, 0)), 0) from #gsm03_tmp a where a.gsc02c = 'gz' and isnull(a.isnotcard, 0) = 1 and gsc04c = '6' and gsc12i = 6 and gsc00c = @comp and empno = @empno insert #tbl_profit(compid,empno,code, itemname, is_add, is_leaf, amt, payment, kind) values(@comp,@empno,'110231', '服务收入-还款-银行卡', 1, 1, @amt, '6', 2) select @amt = 0 select @amt = isnull(sum(isnull(a.gsc05f, 0)), 0) from #gsm03_tmp a where a.gsc02c = 'gz' and isnull(a.isnotcard, 0) = 1 and gsc04c = 'ZFB' and gsc12i = 6 and gsc00c = @comp and empno = @empno insert #tbl_profit(compid,empno,code, itemname, is_add, is_leaf, amt, payment, kind) values(@comp,@empno,'1102Z3', '服务收入-还款-支付宝', 1, 1, @amt, 'ZFB', 2) select @amt = 0 select @amt = isnull(sum(isnull(a.gsc05f, 0)), 0) from #gsm03_tmp a where a.gsc02c = 'gz' and isnull(a.isnotcard, 0) = 1 and gsc04c = 'WX' and gsc12i = 6 and gsc00c = @comp and empno = @empno insert #tbl_profit(compid,empno,code, itemname, is_add, is_leaf, amt, payment, kind) values(@comp,@empno,'1102W3', '服务收入-还款-微信', 1, 1, @amt, 'WX', 2) --Start 统计还款所有支付方式 add by lm 2015.12.28 set @id = 0 set @code_no = '' create table #gsm02_1 ( pay varchar(10) not null, payname varchar(40) null ) insert #gsm02_1 (pay, payname) select gsb02c, gsb03c from gsm02 a with(nolock) where a.gsb00c = @comp and a.gsb01c = '3' and a.gsb02c not in ('1','6','ZFB','WX') while (@id = 0) begin if exists(select 1 from #gsm02_1) begin select top 1 @itemtype = pay, @itemname = payname from #gsm02_1 select @code_no = '110231' + @itemtype select @amt = 0 select @amt = isnull(sum(isnull(a.gsc05f, 0)), 0) from #gsm03_tmp a where a.gsc02c = 'gz' and isnull(a.isnotcard, 0) = 1 and gsc04c = @itemtype and gsc12i = 6 and gsc00c = @comp and empno = @empno select @isadd_ur = gso07f from gsm15 with(nolock) where gso00c = @comp and gso02c = @itemtype select @isadd_ur = isnull(@isadd_ur, 1) -- 默认算收入 insert #tbl_profit(compid,empno,code, itemname, is_add, is_leaf, amt, payment, kind) values(@comp,@empno,@code_no, '服务收入-还款(只计算还款)-' + @itemname, @isadd_ur, 1, @amt, @itemtype, 6) set @code_no = '' set @id = 0 delete from #gsm02_1 where pay = @itemtype end else begin set @id = 1 end end drop table #gsm02_1 -- end --start 在按项目支付的情况下,合计卡付的实际营业额 if (isnull(@sp055f, 0) = 0) begin insert #tbl_profit(compid, empno, code, itemname, is_add, is_leaf, payment, kind) values(@compid, @empno, '110206', '服务收入-卡付实际营业额', 0, 0, '', 2) if(@sp215f = 1) --add by lm 2015-09-16 begin select @amt = 0 select @amt = sum(isnull(isnull(ggb11f, 0) * isnull(gso03i, 1), 0)) --* isnull(a.ggb34f, 1) from ggm02 b with (nolock), #ggm01_1 a, gsm15 c with (nolock) where b.ggb00c = a.gga00c and b.ggb01c = a.gga01c and b.ggb00c = c.gso00c and b.ggb27c = c.gso02c and c.gso01c = '3' and c.gso09f = 1 and b.ggb81c = @empno and a.gga00c = @comp and ((ggb27c >= 'A' and ggb27c <= 'Z') or ggb27c = '#' or ggb27c = '2' or ggb27c = '9') and ggb27c not in ('WX','ZFB') select @gak22f = 0 select @gak22f = sum(isnull(isnull(ggb11f, 0) * isnull(gso03i, 1) * isnull(gak22f, 1), 0)) from ggm02 b with (nolock), #ggm01_1 a, gsm15 c with (nolock), gam10 d with(nolock) where b.ggb00c = a.gga00c and b.ggb01c = a.gga01c and b.ggb00c = c.gso00c and b.ggb27c = c.gso02c and d.gak00c = a.gga00c and d.gak01c = a.gga07c and b.ggb81c = @empno and a.gga00c = @comp and c.gso01c = '3' and c.gso09f = 1 and ggb27c = '4' --第一销售 select @amt2 = 0 select @amt2 = sum(isnull(isnull(ggc11f, 0) * isnull(gso06f, 1) * isnull(a.ggc30f, 1), 0)) from ggm03 b with (nolock), #ggm01_1 a, gsm15 c with (nolock) where b.ggc00c = a.gga00c and b.ggc01c = a.gga01c and b.ggc00c = c.gso00c and b.ggc27c = c.gso02c and b.ggc81c = @empno and a.gga00c = @comp and c.gso01c = '3' and c.gso10f = 1 and ((ggc27c >= 'A' and ggc27c <= 'Z') or ggc27c = '#' or ggc27c = '2' or ggc27c = '9') and ggc27c not in ('WX','ZFB') select @gak24f = 0 select @gak24f = sum(isnull(isnull(ggc11f, 0) * isnull(gso06f, 1) * isnull(gak24f, 1), 0)) from ggm03 b with (nolock), #ggm01_1 a, gsm15 c with (nolock),gam10 d with(nolock) where b.ggc00c = a.gga00c and b.ggc01c = a.gga01c and b.ggc00c = c.gso00c and b.ggc27c = c.gso02c and d.gak00c = a.gga00c and d.gak01c = a.gga07c and b.ggc81c = @empno and a.gga00c = @comp and c.gso01c = '3' and c.gso10f = 1 and ggc27c = '4' --第二销售 select @amt2 = isnull(@amt2,0 ) + sum(isnull(isnull(ggc11f, 0) * isnull(gso06f, 1) * isnull(a.ggc30f, 1), 0)) from ggm03 b with (nolock), #ggm01_1 a, gsm15 c with (nolock) where b.ggc00c = a.gga00c and b.ggc01c = a.gga01c and b.ggc00c = c.gso00c and b.ggc27c = c.gso02c and b.ggc82c = @empno and a.gga00c = @comp and c.gso01c = '3' and c.gso10f = 1 and ((ggc27c >= 'A' and ggc27c <= 'Z') or ggc27c = '#' or ggc27c = '2' or ggc27c = '9') and ggc27c not in ('WX','ZFB') select @gak24f = isnull(@gak24f,0) + sum(isnull(isnull(ggc11f, 0) * isnull(gso06f, 1) * isnull(gak24f, 1), 0)) from ggm03 b with (nolock), #ggm01_1 a, gsm15 c with (nolock),gam10 d with(nolock) where b.ggc00c = a.gga00c and b.ggc01c = a.gga01c and b.ggc00c = c.gso00c and b.ggc27c = c.gso02c and d.gak00c = a.gga00c and d.gak01c = a.gga07c and b.ggc82c = @empno and a.gga00c = @comp and c.gso01c = '3' and c.gso10f = 1 and ggc27c = '4' --第三销售 select @amt2 = isnull(@amt2,0) + sum(isnull(isnull(ggc11f, 0) * isnull(gso06f, 1) * isnull(a.ggc30f, 1), 0)) from ggm03 b with (nolock), #ggm01_1 a, gsm15 c with (nolock) where b.ggc00c = a.gga00c and b.ggc01c = a.gga01c and b.ggc00c = c.gso00c and b.ggc27c = c.gso02c and b.ggc83c = @empno and a.gga00c = @comp and c.gso01c = '3' and c.gso10f = 1 and ((ggc27c >= 'A' and ggc27c <= 'Z') or ggc27c = '#' or ggc27c = '2' or ggc27c = '9') and ggc27c not in ('WX','ZFB') select @gak24f = isnull(@gak24f,0) + sum(isnull(isnull(ggc11f, 0) * isnull(gso06f, 1) * isnull(gak24f, 1), 0)) from ggm03 b with (nolock), #ggm01_1 a, gsm15 c with (nolock),gam10 d with(nolock) where b.ggc00c = a.gga00c and b.ggc01c = a.gga01c and b.ggc00c = c.gso00c and b.ggc27c = c.gso02c and d.gak00c = a.gga00c and d.gak01c = a.gga07c and b.ggc83c = @empno and a.gga00c = @comp and c.gso01c = '3' and c.gso10f = 1 and ggc27c = '4' select @amt2 = isnull(@amt2,0) +isnull(@gak24f,0) select @amt =isnull(@amt,0) +isnull(@gak22f,0) end else begin create table #gcm11 --会员疗程充值 ( gcm00c varchar(10) not null, --门店编号 gcm01c varchar(20) not null --异动单号 ) create table #gcm11_card --会员疗程充值(卡付) ( gcm00c varchar(10) not null, --门店编号 gcm01c varchar(20) not null, --异动单号 cardamt float null ) insert #gcm11(gcm00c, gcm01c) select distinct gcm00c,gcm01c from gcm11 a with(nolock), #dept_bills b --, gam26 z with(nolock) where a.gcm00c = b.compid and a.gcm01c = b.billno -- and a.gcm00c = z.gaz02c -- and z.gaz01c = @incompid and (b.billtype = 'gz') and b.payway in (select pay from pay_classify where comp = @incompid and category = '2' ) insert #gcm11_card(gcm00c, gcm01c, cardamt) select gcm00c, gcm01c, isnull(gcm16f * ratio,0) from gcm11 a with(nolock), #dept_bills b where a.gcm00c = b.compid and a.gcm01c = b.billno and b.billtype = 'gz' and a.gcm15c = b.payway and a.gcm15c in (select pay from pay_classify where comp = @incompid and category = '2') and isnull(a.gcm15c,'') <> '' union all select gcm00c, gcm01c, isnull(gcm18f * ratio,0) from gcm11 a with(nolock), #dept_bills b where a.gcm00c = b.compid and a.gcm01c = b.billno and b.billtype = 'gz' and a.gcm17c = b.payway and a.gcm17c in (select pay from pay_classify where comp = @incompid and category = '2') and isnull(a.gcm17c,'') <> '' union all select gcm00c, gcm01c, isnull(gcm20f * ratio,0) from gcm11 a with(nolock), #dept_bills b where a.gcm00c = b.compid and a.gcm01c = b.billno and b.billtype = 'gz' and a.gcm19c = b.payway and a.gcm19c in (select pay from pay_classify where comp = @incompid and category = '2') and isnull(a.gcm19c,'') <> '' select @amt = 0 select @amt = sum(isnull(isnull(ggb11f, 0) * isnull(gso03i, 1), 0)) --* isnull(a.ggb34f, 1) from ggm02 b with (nolock), #ggm01_1 a, gsm15 c with (nolock) where b.ggb00c = a.gga00c and b.ggb01c = a.gga01c and b.ggb00c = c.gso00c and b.ggb27c = c.gso02c and c.gso01c = '3' and c.gso09f = 1 and b.ggb81c = @empno and a.gga00c = @comp and ((ggb27c >= 'A' and ggb27c <= 'Z') or ggb27c = '#' or ggb27c = '2' or ggb27c = '9') and ggb27c not in ('WX','ZFB') select @gak22f = 0 select @gak22f = sum(isnull(isnull(ggb11f, 0) * isnull(gso03i, 1) * isnull(gak22f, 1), 0)) from ggm02 b with (nolock), #ggm01_1 a, gsm15 c with (nolock), gam10 d with(nolock) where b.ggb00c = a.gga00c and b.ggb01c = a.gga01c and b.ggb00c = c.gso00c and b.ggb27c = c.gso02c and d.gak00c = a.gga00c and d.gak01c = a.gga07c and b.ggb81c = @empno and a.gga00c = @comp and c.gso01c = '3' and c.gso09f = 1 and ggb27c = '4' --第一销售 select @amt2 = 0 select @amt2 = sum(isnull(isnull(ggc11f, 0) * isnull(gso06f, 1) * isnull(a.ggc30f, 1), 0)) from ggm03 b with (nolock), #ggm01_1 a, gsm15 c with (nolock) where b.ggc00c = a.gga00c and b.ggc01c = a.gga01c and b.ggc00c = c.gso00c and b.ggc27c = c.gso02c and b.ggc81c = @empno and a.gga00c = @comp and c.gso01c = '3' and c.gso10f = 1 and ((ggc27c >= 'A' and ggc27c <= 'Z') or ggc27c = '#' or ggb27c='2' or ggb27c='9') and ggc27c not in ('WX','ZFB') select @gak24f = 0 select @gak24f = sum(isnull(isnull(ggc11f, 0) * isnull(gso06f, 1) * isnull(gak24f, 1), 0)) from ggm03 b with (nolock), #ggm01_1 a, gsm15 c with (nolock),gam10 d with(nolock) where b.ggc00c = a.gga00c and b.ggc01c = a.gga01c and b.ggc00c = c.gso00c and b.ggc27c = c.gso02c and d.gak00c = a.gga00c and d.gak01c = a.gga07c and b.ggc81c = @empno and a.gga00c = @comp and c.gso01c = '3' and c.gso10f = 1 and ggc27c = '4' --第二销售 select @amt2 = isnull(@amt2,0 ) + sum(isnull(isnull(ggc11f, 0) * isnull(gso06f, 1) * isnull(a.ggc30f, 1), 0)) from ggm03 b with (nolock), #ggm01_1 a, gsm15 c with (nolock) where b.ggc00c = a.gga00c and b.ggc01c = a.gga01c and b.ggc00c = c.gso00c and b.ggc27c = c.gso02c and b.ggc82c = @empno and a.gga00c = @comp and c.gso01c = '3' and c.gso10f = 1 and ((ggc27c >= 'A' and ggc27c <= 'Z') or ggc27c = '#' or ggc27c = '2' or ggc27c = '9') and ggc27c not in ('WX','ZFB') select @gak24f = isnull(@gak24f,0) + sum(isnull(isnull(ggc11f, 0) * isnull(gso06f, 1) * isnull(gak24f, 1), 0)) from ggm03 b with (nolock), #ggm01_1 a, gsm15 c with (nolock),gam10 d with(nolock) where b.ggc00c = a.gga00c and b.ggc01c = a.gga01c and b.ggc00c = c.gso00c and b.ggc27c = c.gso02c and d.gak00c = a.gga00c and d.gak01c = a.gga07c and b.ggc82c = @empno and a.gga00c = @comp and c.gso01c = '3' and c.gso10f = 1 and ggc27c = '4' --第三销售 select @amt2 = isnull(@amt2,0) + sum(isnull(isnull(ggc11f, 0) * isnull(gso06f, 1) * isnull(a.ggc30f, 1), 0)) from ggm03 b with (nolock), #ggm01_1 a, gsm15 c with (nolock) where b.ggc00c = a.gga00c and b.ggc01c = a.gga01c and b.ggc00c = c.gso00c and b.ggc27c = c.gso02c and b.ggc83c = @empno and a.gga00c = @comp and c.gso01c = '3' and c.gso10f = 1 and ((ggc27c >= 'A' and ggc27c <= 'Z') or ggc27c = '#' or ggc27c = '2' or ggc27c = '9') and ggc27c not in ('WX','ZFB') select @gak24f = isnull(@gak24f,0) + sum(isnull(isnull(ggc11f, 0) * isnull(gso06f, 1) * isnull(gak24f, 1), 0)) from ggm03 b with (nolock), #ggm01_1 a, gsm15 c with (nolock),gam10 d with(nolock) where b.ggc00c = a.gga00c and b.ggc01c = a.gga01c and b.ggc00c = c.gso00c and b.ggc27c = c.gso02c and d.gak00c = a.gga00c and d.gak01c = a.gga07c and b.ggc83c = @empno and a.gga00c = @comp and c.gso01c = '3' and c.gso10f = 1 and ggc27c = '4' select @amt2 = isnull(@amt2,0) +isnull(@gak24f,0) select @amt =isnull(@amt,0) +isnull(@gak22f,0) select @amt3 = 0 select @amt3 = sum(cardamt) from #gcm11_card drop table #gcm11_card drop table #gcm11 end update #tbl_profit set amt = isnull(@amt, 0) + isnull(@amt2, 0) + isnull(@amt3, 0) where code = '110206' and compid = @comp and empno = @empno insert #tbl_profit(compid, empno, code, itemname, is_add, is_leaf, payment, kind) values(@comp,@empno, '11AA01', '服务收入-卡付实际劳动业绩', 0, 0, '', 2) select @amt = 0 select @amt = sum(isnull(isnull(ggb11f, 0) * isnull(gso03i, 1), 0)) --* isnull(a.ggb34f, 1) from ggm02 b with (nolock), #ggm01_1 a, gsm15 c with (nolock) where b.ggb00c = a.gga00c and b.ggb01c = a.gga01c and b.ggb00c = c.gso00c and b.ggb27c = c.gso02c and c.gso01c = '3' and c.gso09f = 1 and b.ggb81c = @empno and a.gga00c = @comp and ((ggb27c >= 'A' and ggb27c <= 'Z') or ggb27c = '#') and ggb27c not in ('WX','ZFB') select @gak22f = 0 select @gak22f = sum(isnull(isnull(ggb11f, 0) * isnull(gso03i, 1) * isnull(gak22f, 1), 0)) from ggm02 b with (nolock), #ggm01_1 a, gsm15 c with (nolock), gam10 d with(nolock) where b.ggb00c = a.gga00c and b.ggb01c = a.gga01c and b.ggb00c = c.gso00c and b.ggb27c = c.gso02c and d.gak00c = a.gga00c and d.gak01c = a.gga07c and b.ggb81c = @empno and a.gga00c = @comp and c.gso01c = '3' and c.gso09f = 1 and ggb27c = '4' --第一销售 select @amt2 = 0 select @amt2 = sum(isnull(isnull(ggc11f, 0) * isnull(gso06f, 1) * isnull(a.ggc30f, 1), 0)) from ggm03 b with (nolock), #ggm01_1 a, gsm15 c with (nolock) where b.ggc00c = a.gga00c and b.ggc01c = a.gga01c and b.ggc00c = c.gso00c and b.ggc27c = c.gso02c and b.ggc81c = @empno and a.gga00c = @comp and c.gso01c = '3' and c.gso10f = 1 and ((ggc27c >= 'A' and ggc27c <= 'Z') or ggc27c = '#') and ggc27c not in ('WX','ZFB') select @gak24f = 0 select @gak24f = sum(isnull(isnull(ggc11f, 0) * isnull(gso06f, 1) * isnull(gak24f, 1), 0)) from ggm03 b with (nolock), #ggm01_1 a, gsm15 c with (nolock),gam10 d with(nolock) where b.ggc00c = a.gga00c and b.ggc01c = a.gga01c and b.ggc00c = c.gso00c and b.ggc27c = c.gso02c and d.gak00c = a.gga00c and d.gak01c = a.gga07c and b.ggc81c = @empno and a.gga00c = @comp and c.gso01c = '3' and c.gso10f = 1 and ggc27c = '4' --第二销售 select @amt2 = isnull(@amt2,0 ) + sum(isnull(isnull(ggc11f, 0) * isnull(gso06f, 1) * isnull(a.ggc30f, 1), 0)) from ggm03 b with (nolock), #ggm01_1 a, gsm15 c with (nolock) where b.ggc00c = a.gga00c and b.ggc01c = a.gga01c and b.ggc00c = c.gso00c and b.ggc27c = c.gso02c and b.ggc82c = @empno and a.gga00c = @comp and c.gso01c = '3' and c.gso10f = 1 and ((ggc27c >= 'A' and ggc27c <= 'Z') or ggc27c = '#') and ggc27c not in ('WX','ZFB') select @gak24f = isnull(@gak24f,0) + sum(isnull(isnull(ggc11f, 0) * isnull(gso06f, 1) * isnull(gak24f, 1), 0)) from ggm03 b with (nolock), #ggm01_1 a, gsm15 c with (nolock),gam10 d with(nolock) where b.ggc00c = a.gga00c and b.ggc01c = a.gga01c and b.ggc00c = c.gso00c and b.ggc27c = c.gso02c and d.gak00c = a.gga00c and d.gak01c = a.gga07c and b.ggc82c = @empno and a.gga00c = @comp and c.gso01c = '3' and c.gso10f = 1 and ggc27c = '4' --第三销售 select @amt2 = isnull(@amt2,0) + sum(isnull(isnull(ggc11f, 0) * isnull(gso06f, 1) * isnull(a.ggc30f, 1), 0)) from ggm03 b with (nolock), #ggm01_1 a, gsm15 c with (nolock) where b.ggc00c = a.gga00c and b.ggc01c = a.gga01c and b.ggc00c = c.gso00c and b.ggc27c = c.gso02c and b.ggc83c = @empno and a.gga00c = @comp and c.gso01c = '3' and c.gso10f = 1 and ((ggc27c >= 'A' and ggc27c <= 'Z') or ggc27c = '#') and ggc27c not in ('WX','ZFB') select @gak24f = isnull(@gak24f,0) + sum(isnull(isnull(ggc11f, 0) * isnull(gso06f, 1) * isnull(gak24f, 1), 0)) from ggm03 b with (nolock), #ggm01_1 a, gsm15 c with (nolock),gam10 d with(nolock) where b.ggc00c = a.gga00c and b.ggc01c = a.gga01c and b.ggc00c = c.gso00c and b.ggc27c = c.gso02c and d.gak00c = a.gga00c and d.gak01c = a.gga07c and b.ggc83c = @empno and a.gga00c = @comp and c.gso01c = '3' and c.gso10f = 1 and ggc27c = '4' select @amt2 = isnull(@amt2,0) +isnull(@gak24f,0) select @amt =isnull(@amt,0) +isnull(@gak22f,0) update #tbl_profit set amt = isnull(@amt, 0) + isnull(@amt2, 0) where code = '11AA01' and compid = @comp and empno = @empno end -- 合计服务收入 select @amt = 0 select @amt = sum(isnull(amt * is_add, 0)) from #tbl_profit where code like '1102%' and is_leaf = 1 and compid = @comp and empno = @empno update #tbl_profit set amt = isnull(@amt, 0) where code = '1102' and compid = @comp and empno = @empno -- 统计收入项目 select @amt = 0 select @amt = sum(isnull(amt * is_add, 0)) from #tbl_profit where code like '11%' and is_leaf = 1 and compid = @comp and empno = @empno update #tbl_profit set amt = isnull(@amt, 0) where code = '11' and compid = @comp and empno = @empno fetch tbl_profit_gx_gl into @comp, @empno end close tbl_profit_gx_gl deallocate tbl_profit_gx_gl -- 单独计算产品的销售以及各种付款方式的产品总销售 truncate table #tbl_stat_fill insert #tbl_stat_fill(paymode, mode, amt, compid, empno) select ggc27c, 0, sum(isnull(case when @sp048 = '2' then ggc21f else isnull(ggc11f, 0) * ggc14f end , 0)),gga00c, ggc81c from ggm03 a with (nolock), #ggm01_1 b where a.ggc00c = b.gga00c and a.ggc01c = b.gga01c and b.gga93c like @staff and (a.ggc81c between @fromdepart and @todepart or @fromdepart = '*') group by ggc27c,gga00c, ggc81c union all select ggc27c, 0, sum(isnull(case when @sp048 = '2' then ggc23f else isnull(ggc11f, 0) * ggc16f end, 0)),gga00c, ggc82c from ggm03 a with (nolock), #ggm01_1 b where a.ggc00c = b.gga00c and a.ggc01c = b.gga01c and b.gga93c like @staff and (a.ggc82c between @fromdepart and @todepart or @fromdepart = '*') group by ggc27c,gga00c, ggc82c union all select ggc27c, 0, sum(isnull(case when @sp048 = '2' then ggc25f else isnull(ggc11f, 0) * ggc18f end, 0)),gga00c, ggc83c from ggm03 a with (nolock), #ggm01_1 b where a.ggc00c = b.gga00c and a.ggc01c = b.gga01c and b.gga93c like @staff and (a.ggc83c between @fromdepart and @todepart or @fromdepart = '*') group by ggc27c,gga00c, ggc83c -- 把产品卡付的每个账户也细分出来 create table #tbl_card_pay_good ( paycode varchar(20) not null, compid varchar(20) not null, empno varchar(20) not null, acctid int null, amt float null, code varchar(20) null, pay_name varchar(30) null, primary key(compid,empno,paycode) ) declare tbl_stat_fill_goods cursor for select compid, empno from #emp_info open tbl_stat_fill_goods fetch tbl_stat_fill_goods into @comp, @empno while @@fetch_status = 0 begin insert #tbl_profit(compid,empno,code, itemname, is_add, is_leaf) values(@comp,@empno,'1103', '产品销售收入', 0, 1) select @amt = 0 select @amt = sum(isnull(a.amt, 0)) from #tbl_stat_fill a where a.compid = @comp and a.empno = @empno update #tbl_profit set amt = isnull(@amt, 0) where code = '1103' and compid = @comp and empno = @empno if (isnull(@sp055f, 0) = 0) -- 按项目支付 begin -- 产品销售-现金 insert #tbl_profit(compid,empno,code, itemname, is_add, is_leaf, payment, kind) values(@comp, @empno,'110301', '产品销售收入-现金', 0, 1, '1', 9) -- 合计产品现金 select @amt = 0 select @amt = sum(isnull(b.amt, 0)) from #tbl_stat_fill b where b.paymode = '1' and b.compid = @comp and b.empno = @empno --更新现金产品 update a set a.amt = @amt from #tbl_profit a where a.code = '110301' and a.compid = @comp and a.empno = @empno -- update a -- set a.amt = isnull(b.amt, 0) -- from #tbl_profit a, #tbl_stat_fill b -- where a.compid = b.compid -- and a.empno = b.empno -- and a.code = '110301' -- and b.paymode = '1' -- and a.compid = @comp -- and a.empno = @empno -- 产品销售-卡 insert #tbl_profit(compid,empno,code, itemname, is_add, is_leaf, payment, kind) values(@comp,@empno,'110302', '产品销售收入-卡付', @add_card_svr, 1, 'CARD', 9) -- 很多种卡付的情况, 把积分支付的情况排除掉,单独列出来 select @amt = 0 select @amt = sum(isnull(b.amt, 0)) from #tbl_stat_fill b, gsm14 c with (nolock) where b.paymode = c.gsn01c and isnull(c.gsn02c, '') <> '' and isnull(c.gsn02c, '') <> '999' and isnull(c.gsn02c, '') <> '3' -- 积分支付 and b.compid = @comp and b.empno = @empno update a set a.amt = isnull(@amt, 0) from #tbl_profit a where a.code = '110302' and a.compid = @comp and a.empno = @empno -- 把产品卡付的每个账户也细分出来 truncate table #tbl_card_pay_good insert #tbl_card_pay_good(compid,empno,paycode, amt) select compid,empno,paymode, sum(amt) from #tbl_stat_fill where (paymode = '#' or paymode = '4' or paymode = '9' or (paymode >= 'A' and paymode <= 'Z')) -- 不包括积分 and paymode not in ('WX','ZFB') group by compid,empno,paymode update a set a.acctid = b.gsn02c from #tbl_card_pay_good a, gsm14 b with (nolock) where a.paycode = b.gsn01c and a.compid = @comp and a.empno = @empno update a set a.code = right('00' + paycode, 2) from #tbl_card_pay_good a where a.compid = @comp and a.empno = @empno update a set a.pay_name = b.gsb03c from #tbl_card_pay_good a, gsm02 b with (nolock) where a.paycode = b.gsb02c and gsb00c = @comp and gsb01c = '3' and a.compid = @comp and a.empno = @empno insert #tbl_profit(compid,empno,code, itemname, is_add, is_leaf, payment, kind, amt) select @comp,@empno,'110302' + isnull(code, '00'), '产品收入-取用储值-' + isnull(pay_name,''), 0, 1, paycode, 2, amt from #tbl_card_pay_good -- 产品销售-抵用券 select @itemname = gsb03c from gsm02 with (nolock) where gsb01c = '3' and gsb02c = '3' and gsb00c = @incompid insert #tbl_profit(compid,empno,code, itemname, is_add, is_leaf, payment, kind) values(@comp,@empno,'110303', '产品销售收入-' + isnull(@itemname, ''), 0, 1, '3', 9) -- 合计产品销售收入 抵用券 select @amt = 0 select @amt = sum(isnull(b.amt, 0)) from #tbl_stat_fill b where b.paymode = '3' and b.compid = @comp and b.empno = @empno --修改 抵用券 合计产品销售收入 抵用券 update a set a.amt = @amt from #tbl_profit a where a.code = '110303' and a.compid = @comp and a.empno = @empno -- 产品销售-抵用券2 select @itemname = gsb03c from gsm02 with (nolock) where gsb01c = '3' and gsb02c = '$' and gsb00c = @comp insert #tbl_profit(compid,empno,code, itemname, is_add, is_leaf, payment, kind) values(@comp,@empno,'110304', '产品销售收入-' + isnull(@itemname, ''), 0, 1, '$', 9) -- 合计产品销售收入 抵用券B select @amt = 0 select @amt = sum(isnull(b.amt, 0)) from #tbl_stat_fill b where b.paymode = '$' and b.compid = @comp and b.empno = @empno -- 修改 合计产品销售收入 抵用券B update a set a.amt = @amt from #tbl_profit a where a.code = '110304' and a.compid = @comp and a.empno = @empno -- 新增加的$开头的支付方式的产品支付 select @code_int = 110350 declare cur_each_pay_3 cursor for select gsb02c from gsm02 with (nolock) where gsb00c = @comp and gsb01c = '3' and gsb02c like '$%' and len(gsb02c) > 1 order by gsb02c open cur_each_pay_3 fetch cur_each_pay_3 into @pay_$ while @@fetch_status = 0 begin select @code_char = convert(varchar(10), @code_int) select @itemname = gsb03c from gsm02 with (nolock) where gsb01c = '3' and gsb02c = @pay_$ and gsb00c = @comp insert #tbl_profit(compid,empno,code, itemname, is_add, is_leaf, payment, kind) values(@comp,@empno,@code_char, '产品销售收入-' + isnull(@itemname, ''), 0, 1, @pay_$, 9) -- 合计产品销售收入 其他以$ 开头的 支付方式 select @amt = 0 select @amt = sum(isnull(b.amt, 0)) from #tbl_stat_fill b where b.paymode = @pay_$ and b.compid = @comp and b.empno = @empno --更新其他 以 $ 开头的支付方式 update a set a.amt = @amt from #tbl_profit a where a.code = @code_char and a.compid = @comp and a.empno = @empno select @code_int = @code_int + 1 select @code_char = convert(varchar(10), @code_int) fetch cur_each_pay_3 into @pay_$ end close cur_each_pay_3 deallocate cur_each_pay_3 -- 产品销售-经理签单,已经在服务收入中包括了 select @itemname = gsb03c from gsm02 with (nolock) where gsb01c = '3' and gsb02c = '8' and gsb00c = @comp insert #tbl_profit(compid,empno,code, itemname, is_add, is_leaf, payment, kind) values(@comp,@empno,'110305', '产品销售收入-' + isnull(@itemname, ''), 0, 1, '8', 9) -- 合计产品销售收入经理签单 select @amt = 0 select @amt = sum(isnull(b.amt, 0)) from #tbl_stat_fill b where b.paymode = '8' and b.compid = @comp and b.empno = @empno -- 更新 合计产品销售收入经理签单 update a set a.amt = @amt from #tbl_profit a where a.code = '110305' and a.compid = @comp and a.empno = @empno -- 产品销售-银行卡 select @itemname = gsb03c from gsm02 with (nolock) where gsb01c = '3' and gsb02c = '6' and gsb00c = @comp insert #tbl_profit(compid,empno,code, itemname, is_add, is_leaf, payment, kind) values(@comp,@empno,'110306', '产品销售收入-' + isnull(@itemname, ''), 0, 1, '6', 9) -- 合计产品销售收入银行卡支付 select @amt = 0 select @amt = sum(isnull(b.amt, 0)) from #tbl_stat_fill b where b.paymode = '6' and b.compid = @comp and b.empno = @empno -- 更新 合计产品销售收入银行卡支付 update a set a.amt = @amt from #tbl_profit a where a.code = '110306' and a.compid = @comp and a.empno = @empno -- 产品销售-支付宝 select @itemname = gsb03c from gsm02 with (nolock) where gsb01c = '3' and gsb02c = 'ZFB' and gsb00c = @comp insert #tbl_profit(compid,empno,code, itemname, is_add, is_leaf, payment, kind) values(@comp,@empno,'1103Z1', '产品销售收入-' + isnull(@itemname, ''), 0, 1, 'ZFB', 9) -- 合计产品销售收入-支付宝 select @amt = 0 select @amt = sum(isnull(b.amt, 0)) from #tbl_stat_fill b where b.paymode = 'ZFB' and b.compid = @comp and b.empno = @empno -- 更新 合计产品销售收入-支付宝 update a set a.amt = @amt from #tbl_profit a where a.code = '1103Z1' and a.compid = @comp and a.empno = @empno insert #tbl_profit(compid,empno,code, itemname, is_add, is_leaf, payment, kind) values(@comp,@empno,'1103W1', '产品销售收入-微信', 0, 1, 'WX', 9) -- 合计产品销售收入-微信 select @amt = 0 select @amt = sum(isnull(b.amt, 0)) from #tbl_stat_fill b where b.paymode = 'WX' and b.compid = @comp and b.empno = @empno --更新 合计产品销售收入-微信 update a set a.amt = @amt from #tbl_profit a where a.code = '1103W1' and a.compid = @comp and a.empno = @empno -- 产品销售-欠款 insert #tbl_profit(compid,empno,code, itemname, is_add, is_leaf) values(@comp,@empno,'110307', '产品销售收入-欠款', 0, 1) -- 合计产品销售收入-欠款 select @amt = 0 select @amt = sum(isnull(b.amt, 0)) from #tbl_stat_fill b where b.paymode = '5' and b.compid = @comp and b.empno = @empno --更新 合计产品销售收入-欠款 update a set a.amt = @amt from #tbl_profit a where a.code = '110307' and a.compid = @comp and a.empno = @empno -- 产品销售内调 select @itemname = gsb03c from gsm02 with (nolock) where gsb01c = '3' and gsb02c = '0' and gsb00c = @comp insert #tbl_profit(compid,empno,code, itemname, is_add, is_leaf, payment, kind) values(@comp,@empno,'110308', '产品销售收入-' + isnull(@itemname, ''), 0, 1, '0', 9) -- 合计产品销售收入-内调 select @amt = 0 select @amt = sum(isnull(b.amt, 0)) from #tbl_stat_fill b where b.paymode = '0' and b.compid = @comp and b.empno = @empno --更新 合计产品销售收入-内调 update a set a.amt = @amt from #tbl_profit a where a.code = '110308' and a.compid = @comp and a.empno = @empno -- 产品销售 积分支付 select @itemname = gsb03c from gsm02 with (nolock) where gsb01c = '3' and gsb02c = '7' and gsb00c = @comp insert #tbl_profit(compid,empno,code, itemname, is_add, is_leaf, payment, kind) ---- 积分永远不作为服务收入 values(@comp,@empno,'110309', '产品销售收入-' + isnull(@itemname, ''), 0, 1, '7', 9) -- 合计产品销售收入-积分 select @amt = 0 select @amt = sum(isnull(b.amt, 0)) from #tbl_stat_fill b where b.paymode = '7' and b.compid = @comp and b.empno = @empno --更新 合计产品销售收入-积分 update a set a.amt = @amt from #tbl_profit a where a.code = '110309' and a.compid = @comp and a.empno = @empno -- 产品欠款的还款 select @amt = 0 select @amt = isnull(sum(isnull(a.gsc05f, 0)), 0) from #gsm03_tmp a where a.gsc02c = 'gz' and isnull(a.isnotcard, 0) = 1 and gsc04c = '1' and gsc12i = 6 and a.gsc00c = @comp and a.empno = @empno insert #tbl_profit(compid, empno,code, itemname, is_add, is_leaf, amt, payment, kind) values(@comp,@empno,'110310', '产品销售收入-还款-现金', 0, 1, @amt, '1', 9) select @amt = 0 select @amt = isnull(sum(isnull(a.gsc05f, 0)), 0) from #gsm03_tmp a where a.gsc02c = 'gz' and isnull(a.isnotcard, 0) = 1 and gsc04c = '6' and gsc12i = 6 and a.gsc00c = @comp and a.empno = @empno insert #tbl_profit(compid, empno,code, itemname, is_add, is_leaf, amt, payment, kind) values(@comp,@empno,'110311', '产品销售收入-还款-银行卡', 0, 1, @amt, '6', 9) select @amt = 0 select @amt = isnull(sum(isnull(a.gsc05f, 0)), 0) from #gsm03_tmp a where a.gsc02c = 'gz' and isnull(a.isnotcard, 0) = 1 and gsc04c = 'ZFB' and gsc12i = 6 and a.gsc00c = @comp and a.empno = @empno insert #tbl_profit(compid, empno,code, itemname, is_add, is_leaf, amt, payment, kind) values(@comp,@empno,'1103Z2', '产品销售收入-还款-支付宝', 0, 1, @amt, 'ZFB', 9) select @amt = 0 select @amt = isnull(sum(isnull(a.gsc05f, 0)), 0) from #gsm03_tmp a where a.gsc02c = 'gz' and isnull(a.isnotcard, 0) = 1 and gsc04c = 'WX' and gsc12i = 6 and a.gsc00c = @comp and a.empno = @empno insert #tbl_profit(compid,empno,code, itemname, is_add, is_leaf, amt, payment, kind) values(@comp,@empno,'1103W2', '产品销售收入-还款-微信', 0, 1, @amt, 'WX', 9) end fetch tbl_stat_fill_goods into @comp, @empno end close tbl_stat_fill_goods deallocate tbl_stat_fill_goods create table #group_fee ( idd int identity not null, code varchar(20) not null, compid varchar(20) not null, empno varchar(20) not null, type varchar(20) not null, name varchar(30) null, amt float null, type2 int null, primary key(idd) ) create nonclustered index index_group_fee on #group_fee(compid,empno) declare tbl_profit_other cursor for select compid, empno from #emp_info open tbl_profit_other fetch tbl_profit_other into @comp, @empno while @@fetch_status = 0 begin --费用 insert #tbl_profit(compid,empno,code, itemname, is_add, is_leaf) values(@comp,@empno,'12', '费用', -1, 0) --其他收入 Huzy 2012/05/31 insert #tbl_profit(compid,empno,code, itemname, is_add, is_leaf) values(@comp,@empno,'18', '其他收入', 1, 0) --虚拟收入 insert #tbl_profit(compid,empno,code, itemname, is_add, is_leaf) values(@comp,@empno,'n1', '虚拟收入', 1, 0) --其他收入 insert #tbl_profit(compid,empno,code, itemname, is_add, is_leaf) values(@comp,@empno,'n2', '虚拟支出', 1, 0) truncate table #group_fee insert #group_fee(compid,empno,code, type, amt) select gqa00c,gqa81c,isnull(gqb03c, ''), isnull(gqa03c, ''), isnull(sum(isnull(gqb05f, 0)), 0) from gqm02 b with (nolock), gqm01 a with (nolock), gam26 z with (nolock) where b.gqb00c = a.gqa00c and b.gqb01c = a.gqa01c and a.gqa00c = z.gaz02c and z.gaz01c = @comp and a.gqa80d >= @fromdate and a.gqa80d <= @todate and a.gqa95c like @staff and (a.gqa81c between @fromdepart and @todepart or @fromdepart = '*')--add @fromdepart by lyj 2007-09-06 and isnull(b.gqb19i, 0) = 0 --是否是虚拟款 group by gqa00c,gqa81c,isnull(gqb03c, ''), isnull(gqa03c, ''), isnull(gqb19i, 0) insert #group_fee(compid,empno,code, type, amt, type2) select gqa00c,gqa81c,isnull(gqb03c, ''), isnull(gqa03c, ''), isnull(sum(isnull(gqb05f, 0)), 0), isnull(b.gqb19i, 0) from gqm02 b with (nolock), gqm01 a with (nolock), gam26 z with (nolock) where b.gqb00c = a.gqa00c and b.gqb01c = a.gqa01c and a.gqa00c = z.gaz02c and z.gaz01c = @comp and a.gqa80d >= @fromdate and a.gqa80d <= @todate and a.gqa95c like @staff and (a.gqa81c between @fromdepart and @todepart or @fromdepart = '*')--add @fromdepart by lyj 2007-09-06 and isnull(b.gqb19i, 0) = 1 --是否是虚拟款 group by gqa00c,gqa81c,isnull(gqb03c, ''), isnull(gqa03c, ''), isnull(b.gqb19i, 0) update a set a.name = b.gsb03c from #group_fee a, gsm02 b with (nolock) where b.gsb00c = @comp and b.gsb01c = 'N' and b.gsb02c = a.code and a.type = 'gv' and compid = @comp and empno = @empno update a set a.name = b.gsb03c from #group_fee a, gsm02 b with (nolock) where b.gsb00c = @comp and b.gsb01c = 'A5' and b.gsb02c = a.code and a.type = 'gp' and compid = @comp and empno = @empno update a set a.code = '12' + right(('000' + convert(varchar(10), a.idd)), 2) from #group_fee a where a.type = 'gv' and type2 is null and compid = @comp and empno = @empno update a set a.code = '18' + right(('000' + convert(varchar(10), a.idd)), 2) from #group_fee a where a.type = 'gp' and type2 is null and compid = @comp and empno = @empno --虚拟收入 update a set a.code = 'n1' + right(('000' + convert(varchar(10), a.idd)), 2) from #group_fee a where a.type = 'gp' and type2 = 1 and compid = @comp and empno = @empno --虚拟支出 update a set a.code = 'n2' + right(('000' + convert(varchar(10), a.idd)), 2) from #group_fee a where a.type = 'gv' and type2 = 1 and compid = @comp and empno = @empno insert #tbl_profit(compid,empno,code, itemname, amt, is_add, is_leaf) select compid,empno,code, isnull(name, ''), amt, case when type = 'gv' then -1 when type = 'gp' then 1 else 0 end, 1 from #group_fee where compid = @comp and empno = @empno order by type --费用-房租 Added by WUXM 2006/02/09 declare @rent float declare @range int declare @flow int declare @code varchar(20) declare @before varchar(10) declare @after varchar(10) declare @cur varchar(10) select @before = substring(convert(varchar(10), dateadd(day, -1, cast(@fromdate as datetime)), 120), 6, 2) select @after = substring(convert(varchar(10), dateadd(day, 1, cast(@todate as datetime)), 120), 6, 2) select @cur = substring(convert(varchar(10), cast(@fromdate as datetime), 120), 6, 2) if (@before = '12' and @cur = '01' and @after = '02') select @rent = sum(isnull(gae18f, 0)) from gam05 with (nolock), gam26 z with (nolock) where gae01c = z.gaz02c and z.gaz01c = @comp else if (@before = '11' and @cur = '12' and @after = '01') select @rent = sum(isnull(gae18f, 0)) from gam05 with (nolock), gam26 z with (nolock) where gae01c = z.gaz02c and z.gaz01c = @comp else if ((@before < @cur) and (@cur < @after)) --从月初到月末的统计 begin select @rent = sum(isnull(gae18f, 0)) from gam05 with (nolock), gam26 z with (nolock) where gae01c = z.gaz02c and z.gaz01c = @comp end else begin select @range = datediff(day, cast(@fromdate as datetime), cast(@todate as datetime)) select @rent = sum(isnull(gae18f, 0)) * (@range + 1) / 30 from gam05 with (nolock), gam26 z with (nolock) where gae01c = z.gaz02c and z.gaz01c = @comp end select @code = '1299' if (@rent > 0) begin insert #tbl_profit(compid,empno,code, itemname, amt, is_add, is_leaf) values (@comp,@empno,@code, '房租', @rent, -1, 1) end select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit where code like '12%' and is_leaf = 1 and compid = @comp and empno = @empno update #tbl_profit set amt = isnull(@amt, 0) where code = '12' and compid = @comp and empno = @empno select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit where code like '18%' and is_leaf = 1 and compid = @comp and empno = @empno update #tbl_profit set amt = isnull(@amt, 0) where code = '18' and compid = @comp and empno = @empno select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit where code like 'n1%' and is_leaf = 1 and compid = @comp and empno = @empno update #tbl_profit set amt = isnull(@amt, 0) where code = 'n1' and compid = @comp and empno = @empno select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit where code like 'n2%' and is_leaf = 1 and compid = @comp and empno = @empno update #tbl_profit set amt = isnull(@amt, 0) where code = 'n2' and compid = @comp and empno = @empno --成本 insert #tbl_profit(compid,empno,code, itemname, is_add, is_leaf) values(@comp,@empno,'13', '成本', -1, 0) select @amt = 0 select @amt = sum(isnull(hah05f, 0)) from ham08 with (nolock), gam26 z with (nolock) where hah00c = z.gaz02c and z.gaz01c = @comp and hah03d >= @fromdate and hah03d <= @todate insert #tbl_profit(compid,empno,code, itemname, amt, is_add, is_leaf) values(@comp,@empno,'1301', '成本-日常奖罚', @amt, -1, 1) -- --计算总提成 -- declare @spid int -- select @spid = @@spid -- if @flag = 0 -- begin -- create table #tmp_rst(amt float null) -- insert #tmp_rst(amt) -- exec upg_personal_statistics @incompid, @fromdate, @todate, '*', '', 1, 1, 1, 6, @spid -- 6:所有员工提成总数据 -- -- select @amt = 0 -- select @amt = sum(isnull(amt, 0)) from #tmp_rst -- drop table #tmp_rst -- -- insert #tbl_profit(code, itemname, amt, is_add, is_leaf) -- values('1302', '成本-员工提成', @amt, -1, 1) -- end -- select @amt = 0 -- select @amt = sum(isnull(ggc05f, 0)*isnull(gfa19f, 0)) -- from ggm03 a with (nolock), #ggm01_1 b, gfm01 c with (nolock) -- where a.ggc00c = b.gga00c -- and a.ggc01c = b.gga01c -- and a.ggc00c = c.gfa00c -- and a.ggc03c = c.gfa01c -- and (a.ggc81c between @fromdepart and @todepart or @fromdepart = '*') -- -- select @amt = isnull(@amt, 0) + isnull(sum(isnull(gge05f, 0)*isnull(gfa15f, 0)), 0) -- from ggm05 a with (nolock), #ggm01_1 b, gfm01 c with (nolock) -- where a.gge00c = b.gga00c -- and a.gge01c = b.gga01c -- and a.gge00c = c.gfa00c -- and a.gge03c = c.gfa01c -- and (a.gge81c between @fromdepart and @todepart or @fromdepart = '*') -- -- insert #tbl_profit(code, itemname, amt, is_add, is_leaf) -- values('1303', '成本-产品', isnull(@amt, 0), -1, 1) -- --成本-固定工资 -- select @amt = 0 -- select @amt = sum(isnull(hbb05f, 0)) -- from hbm02 with (nolock), gam26 z with (nolock) -- where hbb00c = z.gaz02c -- and z.gaz01c = @incompid and hbb04c = '1' -- if (@before = '12' and @cur = '01' and @after = '02') -- select @amt = @amt -- else if (@before = '11' and @cur = '12' and @after = '01') -- select @amt = @amt -- else if ((@before < @cur) and (@cur < @after)) --从月初到月末的统计 -- select @amt = @amt -- else -- select @amt = @amt * (@range + 1)/30 -- -- insert #tbl_profit(code, itemname, amt, is_add, is_leaf) -- values('1304', '成本-固定工资', @amt, -1, 1) -- select @amt = 0 -- select @amt = sum(isnull(amt, 0)) from #tbl_profit where code like '13%' and is_leaf = 1 -- -- update #tbl_profit set amt = isnull(@amt, 0) where code = '13' --删除为了性能必须要得ggm01的缓冲表 --计算利润 select @amt = 0 select @amt = sum(isnull(amt*is_add, 0)) from #tbl_profit where len(code) = 2 and compid = @comp and empno = @empno insert #tbl_profit(compid,empno,code, itemname, amt, is_add, is_leaf) values(@comp,@empno,'19', '利润', isnull(@amt, 0), 0, 1) --单位面积利润率 declare @area float select @area = sum(isnull(gae17f, 0)) from gam05 with (nolock), gam26 z with (nolock) where gae01c = z.gaz02c and z.gaz01c = @comp if (@area > 0) begin insert #tbl_profit(compid,empno,code, itemname, amt, is_add, is_leaf) values (@comp,@empno,20, '单位面积利润', @amt/@area, 0, 1) end --储值余额 select @amt = 0 select @amt = sum(isnull(gcc06f, 0)) from gcm03 with (nolock), gcm01 with (nolock), gam26 z with (nolock) where gcc00c = z.gaz02c and z.gaz01c = @comp and (gcc03i <> 1 and gcc03i <> 0 and gcc03i <> 3 and gcc03i < 888) and gca08i <> 1 and gcc00c = gca00c and gca01c = gcc01c and gcc00c = gca13d insert #tbl_profit(compid,empno,code, itemname, amt, is_add, is_leaf) values(@comp,@empno,'14', '储值余额', isnull(@amt, 0), 0, 1) declare @pointssum float select @pointssum = 0 select @pointssum = sum(isnull(gcd06f, 0)) from gcm04 with (nolock), gam26 z with (nolock) where gcd09d >= @fromdate and gcd09d <= @todate and gcd02i = 3 and gcd00c = z.gaz02c and z.gaz01c = @comp and gcd05i = 0 insert #tbl_profit(compid,empno,code, itemname, amt, is_add, is_leaf) values(@comp,@empno,'15', '赠送积分', isnull(@pointssum, 0), 0, 1) fetch tbl_profit_other into @comp, @empno end close tbl_profit_other deallocate tbl_profit_other --删除第二级为0的 delete #tbl_profit where isnull(amt, 0) = 0 and len(code) > 4 drop table #gsm03_tmp select compid, empno, idd, code, itemname, is_add, amt, is_leaf, payment, kind from #tbl_profit --where compid = '001' order by compid ,empno,code -- if ((@staff = '' or @staff = '*' or @staff = '%') and @flag = 1 and @fromdate = @todate) -- begin -- delete gcm21 -- where gcu00c = @incompid and gcu01c = @todate -- -- insert gcm21(gcu00c, gcu01c, gcu02i, gcu03c, gcu04c, gcu05i, gcu06f, gcu07i, gcu08c, gcu09i) -- select @incompid, @todate, idd, code, itemname, is_add, isnull(amt, 0), is_leaf, payment, kind -- from #tbl_profit -- end --删除为了性能必须要得ggm01的缓冲表 drop table #gem01_tmp drop table #tbl_profit drop table #tbl_stat_fill drop table #emp_info drop table #customed_proj_count drop table #group_fee drop table #ggm01_1 drop table #tbl_card_pay_good drop table #tbl_card_pay drop table #gsm02 drop table #dept_bills drop table #tbl_bill_total end GO /****** Object: StoredProcedure [dbo].[upg_analyse_profit_inter] Script Date: 06/12/2017 06:19:26 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_analyse_profit_inter] ( @compid varchar(10), -- 已经支持各级别的公司 @fromdate varchar(10), @todate varchar(10), @flag int, -- flag 0-计算员工提成 1-不计算员工提成 @staff varchar(20), -- 收银人员 @calmode int -- 计算利润的模式 0-卡销售作为收入 1-劳动业绩作为收入 2-综合小单的计算方式 ) as begin declare @amt float declare @amt2 float declare @amt3 float declare @incompid varchar(10) declare @itemtype varchar(10) declare @itemname varchar(60) declare @isadd_0 float declare @isadd_3 float declare @isadd_$ float declare @add_card_sale int declare @add_card_svr int -- 卡付是否作为收入 declare @NoCheckAmount float declare @sp215f float --充值疗程储值方式支付是否作为卡付实际营业额 declare @gak24f float declare @gak22f float select @sp215f = gsa03f from gsm01 with (nolock) where gsa00c = @compid and gsa01c = 'SP212' if @calmode = 2 begin declare @sp115f float select @sp115f = gsa03f from gsm01 with (nolock) where gsa00c = @compid and gsa01c = 'SP115' if @sp115f = 0 select @calmode = 0 else select @calmode = 1 end if @calmode = 0 begin select @add_card_sale = 1 select @add_card_svr = 0 end else begin select @add_card_sale = 0 select @add_card_svr = 1 end select @incompid = @compid select @isadd_0 = gso07f from gsm15 with (nolock) where gso00c = @incompid and gso02c = '0' select @isadd_3 = gso07f from gsm15 with (nolock) where gso00c = @incompid and gso02c = '3' select @isadd_$ = gso07f from gsm15 with (nolock) where gso00c = @incompid and gso02c = '$' select @isadd_0 = isnull(@isadd_0, 0) select @isadd_3 = isnull(@isadd_3, 0) select @isadd_$ = isnull(@isadd_$, 0) if @staff = '*' or @staff = '' begin select @staff = '%' end if @compid = '001' select @compid = '%' declare @sp055f float select @sp055f = gsa03f from gsm01 with (nolock) where gsa00c = @incompid and gsa01c = 'SP055' create table #gsm03_tmp -- 单据--支付明细 ( gsc00c varchar(10) not null, --公司别 gsc01c varchar(20) not null, --单据编号 gsc02c varchar(5) not null, --单据类别 gsc04c varchar(5) not null, --支付方式 gsc05f float null, --支付金额 gsc10d varchar(8) null, --单据帐务日期 gsc11c varchar(20) null, --登陆人 gsc12i int null, --方式 isnotcard int null default 0 --标志是否为卡销售的,为了区分充值时候的产品或项目的欠款与还款 ) create nonclustered index gsm03_tmp_idx_02 on #gsm03_tmp(gsc02c) create clustered index gsm03_tmp_idx_01 on #gsm03_tmp(gsc04c) --因为项目或产品的欠款也是纪录在会员账户管理中,而且还款也是纪录在其中 --所以必须把这些特殊的gcm10标记出来,否则在统计会员卡销售的时候就不准 create table #gcm10_888 ( compid varchar(10) not null, billid varchar(20) not null, primary key(compid, billid) ) if (@incompid = '001') begin insert #gsm03_tmp(gsc00c, gsc01c, gsc02c, gsc04c, gsc05f, gsc10d, gsc11c, gsc12i) select gsc00c, gsc01c, gsc02c, isnull(gsc04c, ''), isnull(gsc05f, 0), isnull(gsc10d, ''), isnull(gsc11c, ''), isnull(gsc12i, -1) from gsm03 with (nolock) where gsc10d >= @fromdate and gsc10d <= @todate and gsc11c like @staff insert #gcm10_888(compid, billid) select gcl00c, gcl01c from gcm10 a with (nolock) where gcl80d >= @fromdate and gcl80d <= @todate and gcl93c like @staff and a.gcl04c = '888' --合计未复核的金额 select @NoCheckAmount = ISnull(SUM(ISnull(gsc05f, 0)), 0) from gsm03 a with ( nolock ) INNER JOIN ggm01 b with(nolock) on a.gsc00c = b.gga00c and a.gsc01c = b.gga01c where gsc10d >= @fromdate and gsc10d <= @todate and gsc11c like @staff and ISnull(b.gga93c, '') = '' and ISnull(b.gga94d, '') = '' end else begin -- 创建临时表 create table #temp_comp ( compId varchar(10) not null ) insert into #temp_comp(compId) select gaz02c from gam26 with (nolock) where gaz01c = @incompid -- 创建循环变量 declare @HasRecord int set @HasRecord = 1 declare @currentCompId varchar(10) while(@HasRecord = 1) begin if exists(select 1 from #temp_comp) begin set @HasRecord = 1 select top 1 @currentCompId = compId from #temp_comp insert #gsm03_tmp(gsc00c, gsc01c, gsc02c, gsc04c, gsc05f, gsc10d, gsc11c, gsc12i) select gsc00c, gsc01c, gsc02c, isnull(gsc04c, ''), isnull(gsc05f, 0), isnull(gsc10d, ''), isnull(gsc11c, ''), isnull(gsc12i, -1) from gsm03 a with (nolock) where gsc00c = @currentCompId and gsc10d >= @fromdate and gsc10d <= @todate and gsc11c like @staff insert #gcm10_888(compid, billid) select gcl00c, gcl01c from gcm10 a with (nolock) where gcl00c = @currentCompId and gcl80d >= @fromdate and gcl80d <= @todate and a.gcl04c = '888' and gcl93c like @staff -- 合计未复核的金额 select @NoCheckAmount = ISnull(SUM(ISnull(gsc05f, 0)), 0) from gsm03 a with ( nolock ), ggm01 b with(nolock) where gsc00c = @currentCompId and gsc10d >= @fromdate and gsc10d <= @todate and a.gsc00c = b.gga00c and a.gsc01c = b.gga01c and ISnull(b.gga93c, '') = '' and ISnull(b.gga94d, '') = '' and gsc11c LIKE @staff -- 清除已处理的门店 delete #temp_comp where compId = @currentCompId end else begin set @HasRecord = 0 end end drop table #temp_comp end --统计的中间结果 create table #tbl_stat_fill ( idd int identity not null, paymode varchar(10) not null, -- 支付方式 mode int null, -- 卡销售9, 0-存款 1-取款 5-欠款 6还款 amt float null default 0, -- 合计的金额 primary key(idd) ) create table #tbl_profit ( idd int identity not null, code varchar(30) not null, itemname varchar(80) null, is_add int not null, amt float null default 0, is_leaf int not null, payment varchar(60) null, -- 支付方式 kind int null, -- 0 充值 1卡销售 9产品销售 2 服务 -1 取款 8 保证金收入 6 还款 primary key(idd) ) insert #tbl_stat_fill(paymode, mode, amt) select gsc04c, 9, isnull(sum(isnull(a.gsc05f, 0)), 0) from #gsm03_tmp a where a.gsc02c = 'gn' group by gsc04c declare @customed_proj_count float declare @SP031 float --是否为多账单统一结帐 select @SP031 = gsa03f from gsm01 with(nolock) where gsa00c = @incompid and gsa01c = 'SP031' if (@SP031 = '1') --收银的时候直接结帐 begin select @customed_proj_count = sum(isnull(ggb05f,0)) from ggm02 with (nolock), ggm01 with (nolock), gam26 z with(nolock) where gga00c = ggb00c and gga01c = ggb01c and gga00c = z.gaz02c and z.gaz01c = @incompid and gga80d between @fromdate and @todate and gga93c like @staff end else if (@SP031 = '2')--多账单统一结帐 begin select @customed_proj_count = sum(isnull(ggb05f,0)) from ggm02 with (nolock), ggm01 with (nolock), gam26 z with(nolock) where gga00c = ggb00c and gga01c = ggb01c and ggb00c = z.gaz02c and z.gaz01c = @incompid and gga80d between @fromdate and @todate and gga93c like @staff end insert #tbl_profit(code, itemname, is_add, is_leaf, amt) values('HB', '总客数', 0, 1, @customed_proj_count) insert #tbl_profit(code, itemname, is_add, is_leaf, amt) values('11', '收入', 1, 0, 0) insert #tbl_profit(code, itemname, is_add, is_leaf, amt, payment, kind) values('1101', '卡销售收入', @add_card_sale, 0, 0, '', 1) insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('110101', '卡销售收入(扣欠款)-现金', @add_card_sale, 1, '1', 1) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '110101' and b.paymode = '1' insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('110102', '卡销售收入(扣欠款)-银行卡', @add_card_sale, 1, '6', 1) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '110102' and b.paymode = '6' insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('1101Z1', '卡销售收入(扣欠款)-支付宝', @add_card_sale, 1, 'ZFB', 1) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '1101Z1' and b.paymode = 'ZFB' insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('1101W1', '卡销售收入(扣欠款)-微信', @add_card_sale, 1, 'WX', 1) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '1101W1' and b.paymode = 'WX' insert #tbl_profit(code, itemname, is_add, is_leaf) values('110103', '卡销售收入-买卡欠款', 0, 1) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '110103' and b.paymode = '5' -- 如下为账户异动的处理, 必须去掉那些项目或产品销售的欠款与还款, 需要根据单号来去掉 update a set a.isnotcard = 1 from #gsm03_tmp a, #gcm10_888 b where a.gsc00c = b.compid and a.gsc01c = b.billid insert #tbl_stat_fill(paymode, mode, amt) select gsc04c, gsc12i, isnull(sum(isnull(a.gsc05f, 0)), 0) from #gsm03_tmp a where a.gsc02c = 'gz' and isnull(a.isnotcard, 0) = 0 group by gsc04c, gsc12i -- 卡充值 insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('110104', '卡销售收入-充值-现金(扣欠款)', @add_card_sale, 1, '1', 0) -- 卡充值-现金 update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '110104' and b.paymode = '1' and b.mode = 0 insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('110105', '卡销售收入-充值-银行卡', @add_card_sale, 1, '6', 0) -- 银行卡支付 update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '110105' and b.paymode = '6' and b.mode = 0 -- 考虑银行卡取款 update a set a.amt = isnull(a.amt, 0) + isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '110105' and b.paymode = '6' and b.mode = 1 insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('1101Z2', '卡销售收入-充值-支付宝', @add_card_sale, 1, 'ZFB', 0) -- 支付宝支付 update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '1101Z2' and b.paymode = 'ZFB' and b.mode = 0 -- 考虑支付宝取款 update a set a.amt = isnull(a.amt, 0) + isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '1101Z2' and b.paymode = 'ZFB' and b.mode = 1 insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('1101W2', '卡销售收入-充值-微信', @add_card_sale, 1, 'WX', 0) -- 支付宝支付 update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '1101W2' and b.paymode = 'WX' and b.mode = 0 -- 考虑支付宝取款 update a set a.amt = isnull(a.amt, 0) + isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '1101W2' and b.paymode = 'WX' and b.mode = 1 insert #tbl_profit(code, itemname, is_add, is_leaf) values('110106', '卡销售收入-充值欠款', 0, 1) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '110106' and b.paymode = '5' and b.mode = 0 -- 卡还款 insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('110107', '卡销售收入-还款-现金', @add_card_sale, 1, '1', 6) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '110107' and b.paymode = '1' and b.mode = 6 insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('110108', '卡销售收入-还款-银行卡', @add_card_sale, 1, '6', 6) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '110108' and b.paymode = '6' and b.mode = 6 insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('1101Z3', '卡销售收入-还款-支付宝', @add_card_sale, 1, 'ZFB', 6) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '1101Z3' and b.paymode = 'ZFB' and b.mode = 6 insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('1101W3', '卡销售收入-还款-微信', @add_card_sale, 1, 'WX', 6) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '1101W3' and b.paymode = 'WX' and b.mode = 6 -- 保证金转销售 declare @draw_preamt float select @draw_preamt = sum(amt) from #tbl_stat_fill where paymode = '&' insert #tbl_profit(code, itemname, is_add, is_leaf, amt, payment, kind) values('110109', '卡销售收入-保证金转销售', 0, 1, isnull(@draw_preamt, 0), '&', 0) -- 经理签单 select @itemtype = '8' select @itemname = gsb03c from gsm02 with(nolock) where gsb01c = '3' and gsb02c = @itemtype and gsb00c = @incompid insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('110110', '卡销售收入(扣欠款)-' + isnull(@itemname, ''), 0, 1, @itemtype, 1) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '110110' and b.paymode = @itemtype and b.mode = 9 insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('110111', '卡销售收入-充值-' + isnull(@itemname, ''), 0, 1, @itemtype, 0) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '110111' and b.paymode = @itemtype and b.mode = 0 insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('110112', '卡销售收入-还款-' + isnull(@itemname, ''), 0, 1, @itemtype, 6) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '110112' and b.paymode = @itemtype and b.mode = 6 -- 储值账户二, 按理应该不会有, 但yongqi作为转账了, 记得为0的删除就可以 select @itemtype = '9' select @itemname = gsb03c from gsm02 with(nolock) where gsb01c = '3' and gsb02c = @itemtype and gsb00c = @incompid insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('110115', '卡销售收入(扣欠款)-' + isnull(@itemname, ''), 0, 1, @itemtype, 1) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '110115' and b.paymode = @itemtype and b.mode = 9 -- 转账方式下的充值,也应该扣除欠款部分 insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('110116', '卡销售收入-充值-' + isnull(@itemname, '') + '(扣欠款)', 0, 1, @itemtype, 0) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '110116' and b.paymode = @itemtype and b.mode = 0 insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('110117', '卡销售收入-还款-' + isnull(@itemname, ''), 0, 1, @itemtype, 6) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '110117' and b.paymode = @itemtype and b.mode = 6 -- 会员卡异动收费, 就算是现金 insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('110118', '卡销售收入-异动收费', @add_card_sale, 1, '1', 0) select @amt = 0 select @amt = sum(isnull(a.geb10f, 0)) from gem02 a with(nolock), gem01 b with(nolock), gam26 c with(nolock) where a.geb00c = c.gaz02c and a.geb00c = b.gea00c and a.geb01c = b.gea01c and b.gea80d >= @fromdate and b.gea80d <= @todate and geb09i = 1 and b.gea95c like @staff and c.gaz01c = @incompid select @amt2 = sum(isnull(a.geb10f, 0)) from gem02 a with(nolock), gem01 b with(nolock), gam26 c with(nolock) where a.geb00c = c.gaz02c and a.geb00c = b.gea00c and a.geb01c = b.gea01c and b.gea80d >= @fromdate and b.gea80d <= @todate and geb09i = 2 and b.gea95c like @staff and c.gaz01c = @incompid update #tbl_profit set amt = isnull(@amt, 0) - isnull(@amt2, 0) where code = '110118' -- 现金取款, 取款kind定义为-1 insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('110119', '卡销售收入-取款-现金', @add_card_sale, 1, '1', -1) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '110119' and b.paymode = '1' and b.mode = 1 -- 内调, 记得为0的删除就可以 select @itemtype = '0' select @itemname = gsb03c from gsm02 with(nolock) where gsb01c = '3' and gsb02c = @itemtype and gsb00c = @incompid insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('110120', '卡销售收入(扣欠款)-' + isnull(@itemname, ''), @isadd_0 * @add_card_sale, 1, @itemtype, 1) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '110120' and b.paymode = @itemtype and b.mode = 9 insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('110121', '卡销售收入-充值-' + isnull(@itemname, ''), @isadd_0 * @add_card_sale, 1, @itemtype, 0) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '110121' and b.paymode = @itemtype and b.mode = 0 insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('110122', '卡销售收入-还款-' + isnull(@itemname, ''), @isadd_0 * @add_card_sale, 1, @itemtype, 6) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '110122' and b.paymode = @itemtype and b.mode = 6 insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('110123', '卡销售收入-取款-' + isnull(@itemname, ''), @isadd_0 * @add_card_sale, 1, @itemtype, -1) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '110123' and b.paymode = @itemtype and b.mode = 1 --start 取款其他支付方式 add by lm 2016.06.22 select @amt = 0 select @amt = sum(amt) from #tbl_stat_fill where mode = 1 and paymode <> '1' and paymode <> '0' insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind, amt) values('drawother', '卡销售收入-取款-其他支付方式', @isadd_0 * @add_card_sale, 1, @itemtype, -1, @amt) --end -- 抵用券, 记得为0的删除就可以 select @itemtype = '3' select @itemname = gsb03c from gsm02 with(nolock) where gsb01c = '3' and gsb02c = @itemtype and gsb00c = @incompid insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('110125', '卡销售收入(扣欠款)-' + isnull(@itemname, ''), @isadd_3 * @add_card_sale, 1, @itemtype, 1) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '110125' and b.paymode = @itemtype and b.mode = 9 insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('110126', '卡销售收入-充值-' + isnull(@itemname, ''), @isadd_3 * @add_card_sale, 1, @itemtype, 0) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '110126' and b.paymode = @itemtype and b.mode = 0 insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('110127', '卡销售收入-还款-' + isnull(@itemname, ''), @isadd_3 * @add_card_sale, 1, @itemtype, 6) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '110127' and b.paymode = @itemtype and b.mode = 6 -- 抵用券B, 记得为0的删除就可以 select @itemtype = '$' select @itemname = gsb03c from gsm02 with(nolock) where gsb01c = '3' and gsb02c = @itemtype and gsb00c = @incompid insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('110130', '卡销售收入(扣欠款)-' + isnull(@itemname, ''), @isadd_$ * @add_card_sale, 1, @itemtype, 1) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '110130' and b.paymode = @itemtype and b.mode = 9 insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('110131', '卡销售收入-充值-' + isnull(@itemname, ''), @isadd_$ * @add_card_sale, 1, @itemtype, 0) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '110131' and b.paymode = @itemtype and b.mode = 0 insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('110132', '卡销售收入-还款-' + isnull(@itemname, ''), @isadd_$ * @add_card_sale, 1, @itemtype, 6) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '110132' and b.paymode = @itemtype and b.mode = 6 -- 自己增加的$开头的支付方式 declare @pay_$ varchar(10) declare @code_char varchar(10) declare @isadd_ur float -- 子定义的支付方式是为计入营业收入 declare @code_int int select @code_int = 110150 declare cur_each_pay cursor for select gsb02c from gsm02 with(nolock) where gsb00c = @incompid and gsb01c = '3' and gsb02c like '$%' and len(gsb02c) > 1 order by gsb02c open cur_each_pay fetch cur_each_pay into @pay_$ while @@fetch_status = 0 begin select @code_char = convert(varchar(10), @code_int) select @itemtype = @pay_$ select @itemname = gsb03c from gsm02 with(nolock) where gsb01c = '3' and gsb02c = @itemtype and gsb00c = @incompid select @isadd_ur = gso07f from gsm15 with(nolock) where gso00c = @incompid and gso02c = @pay_$ select @isadd_ur = isnull(@isadd_ur, 1) -- 默认算收入 insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values(@code_char, '卡销售收入(扣欠款)-' + isnull(@itemname, ''), @isadd_ur * @add_card_sale, 1, @pay_$, 1) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = @code_char and b.paymode = @itemtype and b.mode = 9 select @code_int = @code_int + 1 select @code_char = convert(varchar(10), @code_int) insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values(@code_char, '卡销售收入-充值-' + isnull(@itemname, ''), @isadd_ur * @add_card_sale, 1, @pay_$, 0) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = @code_char and b.paymode = @itemtype and b.mode = 0 select @code_int = @code_int + 1 select @code_char = convert(varchar(10), @code_int) insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values(@code_char, '卡销售收入-还款-' + isnull(@itemname, ''), @isadd_ur * @add_card_sale, 1, @pay_$, 6) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = @code_char and b.paymode = @itemtype and b.mode = 6 select @code_int = @code_int + 1 select @code_char = convert(varchar(10), @code_int) -- 备用支付方式取款, 取款kind定义为-1 insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values(@code_char, '卡销售收入-取款-' + isnull(@itemname, ''), @isadd_ur * @add_card_sale, 1, @pay_$, -1) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = @code_char and b.paymode = @itemtype and b.mode = 1 select @code_int = @code_int + 1 select @code_char = convert(varchar(10), @code_int) fetch cur_each_pay into @pay_$ end close cur_each_pay deallocate cur_each_pay -- 积分买卡,只有在充值的时候可能存在,也无法作为卡销售的收入 select @itemtype = '7' select @itemname = gsb03c from gsm02 with(nolock) where gsb01c = '3' and gsb02c = @itemtype and gsb00c = @incompid insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('110133', '卡销售收入-充值-' + isnull(@itemname, ''), 0, 1, @itemtype, 0) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '110133' and b.paymode = @itemtype and b.mode = 0 -- 卡的变更等收入,也属于卡的销售收入 delete #tbl_stat_fill insert #tbl_stat_fill(paymode, amt) select gsc04c, isnull(sum(isnull(a.gsc05f, 0)), 0) from #gsm03_tmp a where a.gsc02c = 'ge' group by gsc04c insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('110140', '卡销售收入-变更-现金', @add_card_sale, 1, '1', 0) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '110140' and b.paymode = '1' insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('110141', '卡销售收入-变更-银行卡', @add_card_sale, 1, '6', 0) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '110141' and b.paymode = '6' insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('1101Z4', '卡销售收入-变更-支付宝', @add_card_sale, 1, 'ZFB', 0) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '1101Z4' and b.paymode = 'ZFB' insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('110145', '卡销售收入-变更-欠款', @add_card_sale, 0, '5', 0) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '110145' and b.paymode = '5' insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('1101W4', '卡销售收入-变更-微信', @add_card_sale, 1, 'WX', 0) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '1101W4' and b.paymode = 'WX' --Start 卡种变更所有支付方式 add by lm 2015.12.28 declare @id int declare @code_no varchar(10) set @id = 0 create table #gsm02 ( pay varchar(10) not null, payname varchar(40) null ) insert #gsm02 (pay, payname) select gsb02c, gsb03c from gsm02 a with(nolock), #tbl_stat_fill b, gsm15 c with (nolock) where a.gsb00c = @incompid and a.gsb02c = b.paymode and a.gsb00c = c.gso00c and a.gsb02c = c.gso02c and c.gso07f = 1 and a.gsb01c = '3' and a.gsb02c not in ('ZFB', 'WX', '1', '6', '5') while (@id = 0) begin if exists(select 1 from #gsm02) begin select top 1 @itemtype = pay, @itemname = payname from #gsm02 select @code_no = '11014' + @itemtype insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values(@code_no, '卡销售收入-变更-' + isnull(@itemname, ''), @add_card_sale, 1, @itemtype, 0) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = @code_no and b.paymode = @itemtype set @code_no = '' set @id = 0 delete from #gsm02 where pay = @itemtype end else begin set @id = 1 end end drop table #gsm02 --END -- 统计卡销售收入 select @amt = 0 if @calmode = 0 begin select @amt = sum(isnull(amt * is_add, 0)) from #tbl_profit where code like '1101%' and is_leaf = 1 end else begin select @amt = sum(isnull(amt, 0)) from #tbl_profit where code like '1101%' and is_leaf = 1 and (isnull(payment, '') <> '&' and isnull(payment, '') <> '7' and isnull(payment, '') <> '') end update #tbl_profit set amt = isnull(@amt, 0) where code = '1101' delete #tbl_stat_fill -- 服务收入 create table #tbl_stat_fill_2 ( idd int identity not null, paymode varchar(10) null, mode int null, amt float null, primary key(idd) ) insert #tbl_stat_fill_2(paymode, mode, amt) select gsc04c, 0, isnull(sum(isnull(a.gsc05f, 0)), 0) from #gsm03_tmp a where a.gsc02c = 'gx' group by gsc04c insert #tbl_stat_fill_2(paymode, mode, amt) select gsc04c, 1, isnull(sum(isnull(a.gsc05f, 0)), 0) from #gsm03_tmp a where a.gsc02c = 'gl' group by gsc04c insert #tbl_stat_fill(paymode, mode, amt) select isnull(paymode, ''), 0, sum(isnull(amt, 0)) from #tbl_stat_fill_2 group by isnull(paymode, '') drop table #tbl_stat_fill_2 insert #tbl_profit(code, itemname, is_add, is_leaf) values('1102', '服务收入(包括产品销售)', 1, 0) insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('110201', '服务收入-现金', 1, 1, '1', 2) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '110201' and b.paymode = '1' and a.kind = 2 insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('110202', '服务收入-银行卡', 1, 1, '6', 2) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '110202' and b.paymode = '6' and a.kind = 2 insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('1102Z1', '服务收入-支付宝', 1, 1, 'ZFB', 2) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '1102Z1' and b.paymode = 'ZFB' insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('1102W1', '服务收入-微信', 1, 1, 'WX', 2) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '1102W1' and b.paymode = 'WX' -- 经理签单 select @itemtype = '8' select @itemname = gsb03c from gsm02 with(nolock) where gsb01c = '3' and gsb02c = @itemtype and gsb00c = @incompid insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('110203', '服务收入-' + isnull(@itemname, ''), 0, 1, @itemtype, 2) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '110203' and b.paymode = @itemtype -- 抵用券 select @itemtype = '3' select @itemname = gsb03c from gsm02 with(nolock) where gsb01c = '3' and gsb02c = @itemtype and gsb00c = @incompid insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('110204', '服务收入-' + isnull(@itemname, ''), @isadd_3, 1, @itemtype, 2) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '110204' and b.paymode = @itemtype -- 第二抵用券 select @itemtype = '$' select @itemname = gsb03c from gsm02 with(nolock) where gsb01c = '3' and gsb02c = @itemtype and gsb00c = @incompid insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('110208', '服务收入-' + isnull(@itemname, ''), @isadd_$, 1, @itemtype, 2) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '110208' and b.paymode = @itemtype select @code_int = 110250 declare cur_each_pay_2 cursor for select gsb02c from gsm02 with(nolock) where gsb00c = @incompid and gsb01c = '3' and gsb02c like '$%' and len(gsb02c) > 1 order by gsb02c open cur_each_pay_2 fetch cur_each_pay_2 into @pay_$ while @@fetch_status = 0 begin select @code_char = convert(varchar(10), @code_int) select @itemtype = @pay_$ select @itemname = gsb03c from gsm02 with(nolock) where gsb01c = '3' and gsb02c = @itemtype and gsb00c = @incompid select @isadd_ur = gso07f from gsm15 with(nolock) where gso00c = @incompid and gso02c = @pay_$ select @isadd_ur = isnull(@isadd_ur, 1) -- 默认算收入 insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values(@code_char, '服务收入-' + isnull(@itemname, ''), @isadd_ur, 1, @pay_$, 2) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = @code_char and b.paymode = @itemtype select @code_int = @code_int + 1 select @code_char = convert(varchar(10), @code_int) fetch cur_each_pay_2 into @pay_$ end close cur_each_pay_2 deallocate cur_each_pay_2 -- 内调 select @itemtype = '0' select @itemname = gsb03c from gsm02 with(nolock) where gsb01c = '3' and gsb02c = @itemtype and gsb00c = @incompid insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('110209', '服务收入-' + isnull(@itemname, ''), @isadd_0, 1, @itemtype, 2) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '110209' and b.paymode = @itemtype insert #tbl_profit(code, itemname, is_add, is_leaf) values('110207', '服务收入-欠款', 0, 1) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '110207' and b.paymode = '5' -- 服务收入-取用储值 insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('110205', '服务收入-取用储值', 0, 0, 'CARD', 2) -- 把明细的服务项目的卡付计算出来 create table #tbl_card_pay ( paycode varchar(20) not null, acctid int null, amt float null, code varchar(20) null, pay_name varchar(30) null, primary key(paycode) ) insert #tbl_card_pay(paycode, amt) select paymode, sum(amt) from #tbl_stat_fill where (paymode = '#' or paymode='2' or paymode = '4' or paymode = '9' or (paymode >= 'A' and paymode <= 'Z')) -- 不包括积分 and paymode not in ('WX', 'ZFB') group by paymode update a set a.acctid = b.gsn02c from #tbl_card_pay a, gsm14 b with(nolock) where a.paycode = b.gsn01c update a set a.code = right('00' + paycode, 2) from #tbl_card_pay a update a set a.pay_name = b.gsb03c from #tbl_card_pay a, gsm02 b with(nolock) where a.paycode = b.gsb02c and gsb00c = @incompid and gsb01c = '3' insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind, amt) select '110205' + isnull(code, '00'), '服务收入-取用储值-' + pay_name, @add_card_svr, 1, paycode, 2, amt from #tbl_card_pay drop table #tbl_card_pay -- 积分需要单独列出来 declare @act varchar(20) declare @act_name varchar(50) select @act = '7' -- 积分的支付 select @act_name = gsb03c from gsm02 with (nolock) where gsb00c = @incompid and gsb01c = '3' and gsb02c = @act insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('11020507', '服务收入-积分支付-' + isnull(@act_name, ''), 0, 1, @act, 2) -- 积分永远不作为服务收入 is_add = 0 update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '11020507' and b.paymode = @act -- 合计所有取用储值, 把积分支付单独扣除 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit where (code like '110205%' and code <> '11020507') and is_leaf = 1 update #tbl_profit set amt = isnull(@amt, 0) where code = '110205' -- START -- 统计因为项目或产品销售的欠款的还款收入,只统计现金还款及银行卡的还款 -- 服务中欠款的可能行很小,所以默认为产品的还款 select @amt = 0 select @amt = isnull(sum(isnull(a.gsc05f, 0)), 0) from #gsm03_tmp a where a.gsc02c = 'gz' and isnull(a.isnotcard, 0) = 1 and a.gsc04c = '1' and gsc12i = 6 insert #tbl_profit(code, itemname, is_add, is_leaf, amt, payment, kind) values('110230', '服务收入-还款-现金', 1, 1, @amt, '1', 2) select @amt = 0 select @amt = isnull(sum(isnull(a.gsc05f, 0)), 0) from #gsm03_tmp a where a.gsc02c = 'gz' and isnull(a.isnotcard, 0) = 1 and gsc04c = '6' and gsc12i = 6 insert #tbl_profit(code, itemname, is_add, is_leaf, amt, payment, kind) values('110231', '服务收入-还款-银行卡', 1, 1, @amt, '6', 2) select @amt = 0 select @amt = isnull(sum(isnull(a.gsc05f, 0)), 0) from #gsm03_tmp a where a.gsc02c = 'gz' and isnull(a.isnotcard, 0) = 1 and gsc04c = 'ZFB' and gsc12i = 6 insert #tbl_profit(code, itemname, is_add, is_leaf, amt, payment, kind) values('1102Z2', '服务收入-还款-支付宝', 1, 1, @amt, 'ZFB', 2) select @amt = 0 select @amt = isnull(sum(isnull(a.gsc05f, 0)), 0) from #gsm03_tmp a where a.gsc02c = 'gz' and isnull(a.isnotcard, 0) = 1 and gsc04c = 'WX' and gsc12i = 6 insert #tbl_profit(code, itemname, is_add, is_leaf, amt, payment, kind) values('1102W2', '服务收入-还款-微信', 1, 1, @amt, 'WX', 2) --Start 统计还款所有支付方式 add by lm 2015.12.28 set @id = 0 set @code_no = '' create table #gsm02_1 ( pay varchar(10) not null, payname varchar(40) null ) insert #gsm02_1 (pay, payname) select gsb02c, gsb03c from gsm02 a with(nolock) where a.gsb00c = @incompid and a.gsb01c = '3' and a.gsb02c not in ('1','6','ZFB','WX','5') while (@id = 0) begin if exists(select 1 from #gsm02_1) begin select top 1 @itemtype = pay, @itemname = payname from #gsm02_1 select @code_no = '110231' + @itemtype select @amt = 0 select @amt = isnull(sum(isnull(a.gsc05f, 0)), 0) from #gsm03_tmp a where a.gsc02c = 'gz' and isnull(a.isnotcard, 0) = 1 and gsc04c = @itemtype and gsc12i = 6 select @isadd_ur = gso07f from gsm15 with(nolock) where gso00c = @incompid and gso02c = @itemtype select @isadd_ur = isnull(@isadd_ur, 1) -- 默认算收入 insert #tbl_profit(code, itemname, is_add, is_leaf, amt, payment, kind) values(@code_no, '服务收入-还款(只计算还款)-' + @itemname, @isadd_ur, 1, @amt, @itemtype, 6) set @code_no = '' set @id = 0 delete from #gsm02_1 where pay = @itemtype end else begin set @id = 1 end end drop table #gsm02_1 -- end -- end -- 必须要的数据 -- 为了提高性能,把需要用倒的ggm01数据转移到临时表中 create table #ggm01_1 ( gga00c varchar(20) not null, gga01c varchar(20) not null, gga93c varchar(20) null, gga07c varchar(10) null, --会员卡类别 ggb34f float null, --实际营业额折算率 ggc30f float null --实际营业额折算比率 primary key(gga00c, gga01c) ) insert #ggm01_1(gga00c, gga01c, gga93c, gga07c) select gga00c, gga01c, gga93c, gga07c from ggm01 a with (nolock), gam26 z with(nolock) where gga00c = z.gaz02c and gga80d >= @fromdate and gga80d <= @todate and z.gaz01c = @incompid -- 在按项目支付的情况下,合计卡付的实际营业额 if (isnull(@sp055f, 0) = 0) begin insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('110206', '服务收入-卡付实际营业额', 0, 0, '', 2) if(@sp215f = 1) --add by lm 2015-09-16 begin select @amt = 0 select @amt = sum(isnull(isnull(ggb11f, 0) * isnull(gso03i, 1), 0)) --* isnull(a.ggb34f, 1) from ggm02 b with (nolock), #ggm01_1 a, gsm15 c with (nolock) where b.ggb00c = a.gga00c and b.ggb01c = a.gga01c and b.ggb00c = c.gso00c and b.ggb27c = c.gso02c and c.gso01c = '3' and c.gso09f = 1 and (ggb27c = '9' or ggb27c = '2' or (ggb27c >= 'A' and ggb27c <= 'Z') or ggb27c = '#') and ggb27c not in ('WX','ZFB') select @gak22f = 0 select @gak22f = sum(isnull(isnull(ggb11f, 0) * isnull(gso03i, 1) * isnull(b.ggb34f, 1), 0)) -- * isnull(gak22f, 1) from ggm02 b with (nolock), #ggm01_1 a, gsm15 c with (nolock), gam10 d with(nolock) where b.ggb00c = a.gga00c and b.ggb01c = a.gga01c and b.ggb00c = c.gso00c and b.ggb27c = c.gso02c and d.gak00c = a.gga00c and d.gak01c = a.gga07c and c.gso01c = '3' and c.gso09f = 1 and ggb27c = '4' select @amt2 = 0 select @amt2 = sum(isnull(isnull(ggc11f, 0) * isnull(gso06f, 1), 0)) from ggm03 b with (nolock), #ggm01_1 a, gsm15 c with (nolock) where b.ggc00c = a.gga00c and b.ggc01c = a.gga01c and b.ggc00c = c.gso00c and b.ggc27c = c.gso02c and c.gso01c = '3' and c.gso10f = 1 and (ggc27c = '9' or ggc27c = '2' or (ggc27c >= 'A' and ggc27c <= 'Z') or ggc27c = '#') and ggc27c not in ('WX','ZFB') select @gak24f = 0 select @gak24f = sum(isnull(isnull(ggc11f, 0) * isnull(gso06f, 1) * isnull(b.ggc30f, 1), 0)) --* isnull(gak24f, 1) from ggm03 b with (nolock), #ggm01_1 a, gsm15 c with (nolock),gam10 d with(nolock) where b.ggc00c = a.gga00c and b.ggc01c = a.gga01c and b.ggc00c = c.gso00c and b.ggc27c = c.gso02c and d.gak00c = a.gga00c and d.gak01c = a.gga07c and c.gso01c = '3' and c.gso10f = 1 and ggc27c = '4' select @amt2 = isnull(@amt2,0) +isnull(@gak24f,0) select @amt =isnull(@amt,0) +isnull(@gak22f,0) end else begin create table #gcm11 --会员疗程充值 ( gcm00c varchar(10) not null, --门店编号 gcm01c varchar(20) not null --异动单号 ) create table #gcm11_card --会员疗程充值(卡付) ( gcm00c varchar(10) not null, --门店编号 gcm01c varchar(20) not null, --异动单号 cardamt float null ) insert #gcm11(gcm00c, gcm01c) select distinct gcm00c,gcm01c from gcm11 a with(nolock), #gsm03_tmp b --, gam26 z with(nolock) where a.gcm00c = b.gsc00c and a.gcm01c = b.gsc01c -- and a.gcm00c = z.gaz02c -- and z.gaz01c = @incompid and (b.gsc02c = 'gz') and b.gsc04c in (select pay from pay_classify where comp = @incompid and category = '2' ) insert #gcm11_card(gcm00c, gcm01c, cardamt) select gcm00c, gcm01c, isnull(gcm16f,0) from gcm11 a with(nolock), #gsm03_tmp b where a.gcm00c = b.gsc00c and a.gcm01c = b.gsc01c and b.gsc02c = 'gz' and a.gcm15c = b.gsc04c and a.gcm15c in (select pay from pay_classify where comp = @incompid and category = '2') and isnull(a.gcm15c,'') <> '' union all select gcm00c, gcm01c, isnull(gcm18f ,0) from gcm11 a with(nolock), #gsm03_tmp b where a.gcm00c = b.gsc00c and a.gcm01c = b.gsc01c and b.gsc02c = 'gz' and a.gcm17c = b.gsc04c and a.gcm17c in (select pay from pay_classify where comp = @incompid and category = '2') and isnull(a.gcm17c,'') <> '' union all select gcm00c, gcm01c, isnull(gcm20f,0) from gcm11 a with(nolock), #gsm03_tmp b where a.gcm00c = b.gsc00c and a.gcm01c = b.gsc01c and b.gsc02c = 'gz' and a.gcm19c = b.gsc04c and a.gcm19c in (select pay from pay_classify where comp = @incompid and category = '2') and isnull(a.gcm19c,'') <> '' select @amt = 0 select @amt = sum(isnull(isnull(ggb11f, 0) * isnull(b.ggb34f, 1) * isnull(gso03i, 1), 0)) --* isnull(a.ggb34f, 1) from ggm02 b with (nolock), #ggm01_1 a, gsm15 c with (nolock) where b.ggb00c = a.gga00c and b.ggb01c = a.gga01c and b.ggb00c = c.gso00c and b.ggb27c = c.gso02c and c.gso01c = '3' and c.gso09f = 1 and ( ggb27c = '9' or ggb27c = '2' or (ggb27c >= 'A' and ggb27c <= 'Z') or ggb27c = '#') and ggb27c not in ('WX','ZFB') select @gak22f = 0 select @gak22f = sum(isnull(isnull(ggb11f, 0) * isnull(gso03i, 1) * isnull(gak22f,1), 0)) -- * isnull(gak22f, 1) from ggm02 b with (nolock), #ggm01_1 a, gsm15 c with (nolock), gam10 d with(nolock) where b.ggb00c = a.gga00c and b.ggb01c = a.gga01c and b.ggb00c = c.gso00c and b.ggb27c = c.gso02c and d.gak00c = a.gga00c and d.gak01c = a.gga07c and c.gso01c = '3' and c.gso09f = 1 and ggb27c = '4' select @amt2 = 0 select @amt2 = sum(isnull(isnull(ggc11f, 0) * isnull(gso06f, 1) * isnull(b.ggc30f, 1), 0)) from ggm03 b with (nolock), #ggm01_1 a, gsm15 c with (nolock) where b.ggc00c = a.gga00c and b.ggc01c = a.gga01c and b.ggc00c = c.gso00c and b.ggc27c = c.gso02c and c.gso01c = '3' and c.gso10f = 1 and (ggc27c = '9' or ggc27c = '2' or (ggc27c >= 'A' and ggc27c <= 'Z') or ggc27c = '#') and ggc27c not in ('WX','ZFB') select @gak24f = 0 select @gak24f = sum(isnull(isnull(ggc11f, 0) * isnull(gso06f, 1) * isnull(gak24f, 1), 0)) -- * isnull(gak24f, 1) from ggm03 b with (nolock), #ggm01_1 a, gsm15 c with (nolock),gam10 d with(nolock) where b.ggc00c = a.gga00c and b.ggc01c = a.gga01c and b.ggc00c = c.gso00c and b.ggc27c = c.gso02c and d.gak00c = a.gga00c and d.gak01c = a.gga07c and c.gso01c = '3' and c.gso10f = 1 and ggc27c = '4' select @amt2 = isnull(@amt2,0) +isnull(@gak24f,0) select @amt =isnull(@amt,0) +isnull(@gak22f,0) select @amt3 = 0 select @amt3 = sum(cardamt) from #gcm11_card drop table #gcm11_card drop table #gcm11 end update #tbl_profit set amt = isnull(@amt, 0) + isnull(@amt2, 0) + isnull(@amt3, 0) where code = '110206' insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('11AA01', '服务收入-卡付实际劳动业绩', 0, 0, '', 2) -- select @amt = 0 -- select @amt = sum(isnull(isnull(ggb11f, 0) * isnull(ggb34f, 1) * isnull(gso09f, 0), 0)) -- from ggm02 b with (nolock), #ggm01_1 a, gsm15 c with (nolock) -- where b.ggb00c = a.gga00c -- and b.ggb01c = a.gga01c -- and (ggb27c = '4' or ggb27c = '9' or (ggb27c >= 'A' and ggb27c <= 'Z') or ggb27c = '#') -- and c.gso00c = b.ggb00c -- and b.ggb27c = c.gso02c -- -- select @amt2 = 0 -- select @amt2 = sum(isnull(isnull(ggc11f, 0) * isnull(ggc30f, 1) * isnull(gso10f, 0), 0)) -- from ggm03 b with (nolock), #ggm01_1 a, gsm15 c with (nolock) -- where b.ggc00c = a.gga00c -- and b.ggc01c = a.gga01c -- and (ggc27c = '4' or ggc27c = '9' or (ggc27c >= 'A' and ggc27c <= 'Z') or ggc27c = '#') -- and c.gso00c = b.ggc00c -- and b.ggc27c = c.gso02c select @amt = 0 select @amt = sum(isnull(isnull(ggb11f, 0) * isnull(gso03i, 1)* isnull(a.ggb34f, 1), 0)) --* isnull(a.ggb34f, 1) from ggm02 b with (nolock), #ggm01_1 a, gsm15 c with (nolock) where b.ggb00c = a.gga00c and b.ggb01c = a.gga01c and b.ggb00c = c.gso00c and b.ggb27c = c.gso02c and c.gso01c = '3' and c.gso09f = 1 and (ggb27c = '9' or ggb27c = '2' or (ggb27c >= 'A' and ggb27c <= 'Z') or ggb27c = '#') and ggb27c not in ('WX','ZFB') select @gak22f = 0 select @gak22f = sum(isnull(isnull(ggb11f, 0) * isnull(gso03i, 1) * isnull(gak22f, 1), 0)) from ggm02 b with (nolock), #ggm01_1 a, gsm15 c with (nolock), gam10 d with(nolock) where b.ggb00c = a.gga00c and b.ggb01c = a.gga01c and b.ggb00c = c.gso00c and b.ggb27c = c.gso02c and d.gak00c = a.gga00c and d.gak01c = a.gga07c and c.gso01c = '3' and c.gso09f = 1 and ggb27c = '4' select @amt2 = 0 select @amt2 = sum(isnull(isnull(ggc11f, 0) * isnull(gso06f, 1) * isnull(a.ggc30f, 1), 0)) from ggm03 b with (nolock), #ggm01_1 a, gsm15 c with (nolock) where b.ggc00c = a.gga00c and b.ggc01c = a.gga01c and b.ggc00c = c.gso00c and b.ggc27c = c.gso02c and c.gso01c = '3' and c.gso10f = 1 and (ggc27c = '9' or ggc27c = '2' or (ggc27c >= 'A' and ggc27c <= 'Z') or ggc27c = '#') and ggc27c not in ('WX','ZFB') select @gak24f = 0 select @gak24f = sum(isnull(isnull(ggc11f, 0) * isnull(gso06f, 1) * isnull(gak24f, 1), 0)) from ggm03 b with (nolock), #ggm01_1 a, gsm15 c with (nolock),gam10 d with(nolock) where b.ggc00c = a.gga00c and b.ggc01c = a.gga01c and b.ggc00c = c.gso00c and b.ggc27c = c.gso02c and d.gak00c = a.gga00c and d.gak01c = a.gga07c and c.gso01c = '3' and c.gso10f = 1 and ggc27c = '4' select @amt2 = isnull(@amt2,0) +isnull(@gak24f,0) select @amt =isnull(@amt,0) +isnull(@gak22f,0) update #tbl_profit set amt = isnull(@amt, 0) + isnull(@amt2, 0) where code = '11AA01' end -- 合计服务收入 select @amt = 0 select @amt = sum(isnull(amt * is_add, 0)) from #tbl_profit where code like '1102%' and is_leaf = 1 update #tbl_profit set amt = isnull(@amt, 0) where code = '1102' -- 保证金的收入 declare @pre_amt float select @pre_amt = sum(gbl04f) from gbm15 a with(nolock), gam26 z with(nolock) where a.gbl00c = z.gaz02c and z.gaz01c = @incompid and gbl05d >= @fromdate and gbl05d <= @todate insert #tbl_profit(code, itemname, is_add, is_leaf, amt, payment, kind) values('1104', '保证金收入', 1, 1, isnull(@pre_amt, 0), '1', 8) -- 统计收入项目 select @amt = 0 select @amt = sum(isnull(amt * is_add, 0)) from #tbl_profit where code like '11%' and is_leaf = 1 update #tbl_profit set amt = isnull(@amt, 0) where code = '11' -- 单独计算产品的销售以及各种付款方式的产品总销售 delete #tbl_stat_fill insert #tbl_stat_fill(paymode, mode, amt) select ggc27c, 0, sum(isnull(a.ggc11f, 0)) from ggm03 a with (nolock), #ggm01_1 b where a.ggc00c = b.gga00c and a.ggc01c = b.gga01c and b.gga93c like @staff group by ggc27c insert #tbl_profit(code, itemname, is_add, is_leaf) values('1103', '产品销售收入', 0, 1) select @amt = 0 select @amt = sum(isnull(a.amt, 0)) from #tbl_stat_fill a update #tbl_profit set amt = isnull(@amt, 0) where code = '1103' if (isnull(@sp055f, 0) = 0) -- 按项目支付 begin -- 产品销售-现金 insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('110301', '产品销售收入-现金', 0, 1, '1', 9) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '110301' and b.paymode = '1' -- 产品销售-卡 insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('110302', '产品销售收入-卡付', @add_card_svr, 1, 'CARD', 9) -- 很多种卡付的情况, 把积分支付的情况排除掉,单独列出来 select @amt = 0 select @amt = sum(isnull(b.amt, 0)) from #tbl_stat_fill b, gsm14 c with(nolock) where b.paymode = c.gsn01c and isnull(c.gsn02c, '') <> '' and isnull(c.gsn02c, '') <> '999' and isnull(c.gsn02c, '') <> '3' -- 积分支付 update a set a.amt = isnull(@amt, 0) from #tbl_profit a where a.code = '110302' -- 把产品卡付的每个账户也细分出来 create table #tbl_card_pay_good ( paycode varchar(20) not null, acctid int null, amt float null, code varchar(20) null, pay_name varchar(30) null, primary key(paycode) ) insert #tbl_card_pay_good(paycode, amt) select paymode, sum(amt) from #tbl_stat_fill where (paymode = '#' or paymode = '4' or paymode = '2' or paymode = '9' or (paymode >= 'A' and paymode <= 'Z')) -- 不包括积分 and paymode not in ('WX', 'ZFB') group by paymode update a set a.acctid = b.gsn02c from #tbl_card_pay_good a, gsm14 b with(nolock) where a.paycode = b.gsn01c update a set a.code = right('00' + paycode, 2) from #tbl_card_pay_good a update a set a.pay_name = b.gsb03c from #tbl_card_pay_good a, gsm02 b with(nolock) where a.paycode = b.gsb02c and gsb00c = @incompid and gsb01c = '3' insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind, amt) select '110302' + isnull(code, '00'), '产品收入-取用储值-' + pay_name, 0, 1, paycode, 2, amt from #tbl_card_pay_good drop table #tbl_card_pay_good -- 产品销售-抵用券 select @itemname = gsb03c from gsm02 with(nolock) where gsb01c = '3' and gsb02c = '3' and gsb00c = @incompid insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('110303', '产品销售收入-' + isnull(@itemname, ''), 0, 1, '3', 9) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '110303' and b.paymode = '3' -- 产品销售-抵用券2 select @itemname = gsb03c from gsm02 with(nolock) where gsb01c = '3' and gsb02c = '$' and gsb00c = @incompid insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('110304', '产品销售收入-' + isnull(@itemname, ''), 0, 1, '$', 9) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '110304' and b.paymode = '$' -- 新增加的$开头的支付方式的产品支付 select @code_int = 110350 declare cur_each_pay_3 cursor for select gsb02c from gsm02 with(nolock) where gsb00c = @incompid and gsb01c = '3' and gsb02c like '$%' and len(gsb02c) > 1 order by gsb02c open cur_each_pay_3 fetch cur_each_pay_3 into @pay_$ while @@fetch_status = 0 begin select @code_char = convert(varchar(10), @code_int) select @itemname = gsb03c from gsm02 with(nolock) where gsb01c = '3' and gsb02c = @pay_$ and gsb00c = @incompid insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values(@code_char, '产品销售收入-' + isnull(@itemname, ''), 0, 1, @pay_$, 9) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = @code_char and b.paymode = @pay_$ select @code_int = @code_int + 1 select @code_char = convert(varchar(10), @code_int) fetch cur_each_pay_3 into @pay_$ end close cur_each_pay_3 deallocate cur_each_pay_3 -- 产品销售-经理签单,已经在服务收入中包括了 select @itemname = gsb03c from gsm02 with(nolock) where gsb01c = '3' and gsb02c = '8' and gsb00c = @incompid insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('110305', '产品销售收入-' + isnull(@itemname, ''), 0, 1, '8', 9) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '110305' and b.paymode = '8' -- 产品销售-银行卡 select @itemname = gsb03c from gsm02 with(nolock) where gsb01c = '3' and gsb02c = '6' and gsb00c = @incompid insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('110306', '产品销售收入-' + isnull(@itemname, ''), 0, 1, '6', 9) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '110306' and b.paymode = '6' -- 产品销售-支付宝 select @itemname = gsb03c from gsm02 with(nolock) where gsb01c = '3' and gsb02c = 'ZFB' and gsb00c = @incompid insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('1103Z1', '产品销售收入-' + isnull(@itemname, ''), 0, 1, 'ZFB', 9) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '1103Z1' and b.paymode = 'ZFB' insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('1103W1', '产品销售收入-微信', 0, 1, 'WX', 9) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '1103W1' and b.paymode = 'WX' -- 产品销售-欠款 insert #tbl_profit(code, itemname, is_add, is_leaf) values('110307', '产品销售收入-欠款', 0, 1) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '110307' and b.paymode = '5' -- 产品销售内调 select @itemname = gsb03c from gsm02 with(nolock) where gsb01c = '3' and gsb02c = '0' and gsb00c = @incompid insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) values('110308', '产品销售收入-' + isnull(@itemname, ''), 0, 1, '0', 9) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '110308' and b.paymode = '0' -- 产品销售 积分支付 select @itemname = gsb03c from gsm02 with(nolock) where gsb01c = '3' and gsb02c = '7' and gsb00c = @incompid insert #tbl_profit(code, itemname, is_add, is_leaf, payment, kind) ---- 积分永远不作为服务收入 values('110309', '产品销售收入-' + isnull(@itemname, ''), 0, 1, '7', 9) update a set a.amt = isnull(b.amt, 0) from #tbl_profit a, #tbl_stat_fill b where a.code = '110309' and b.paymode = '7' -- 产品欠款的还款 select @amt = 0 select @amt = isnull(sum(isnull(a.gsc05f, 0)), 0) from #gsm03_tmp a where a.gsc02c = 'gz' and isnull(a.isnotcard, 0) = 1 and a.gsc04c = '1' and gsc12i = 6 insert #tbl_profit(code, itemname, is_add, is_leaf, amt, payment, kind) values('110310', '产品销售收入-还款-现金', 0, 1, @amt, '1', 9) select @amt = 0 select @amt = isnull(sum(isnull(a.gsc05f, 0)), 0) from #gsm03_tmp a where a.gsc02c = 'gz' and isnull(a.isnotcard, 0) = 1 and gsc04c = '6' and gsc12i = 6 insert #tbl_profit(code, itemname, is_add, is_leaf, amt, payment, kind) values('110311', '产品销售收入-还款-银行卡', 0, 1, @amt, '6', 9) select @amt = 0 select @amt = isnull(sum(isnull(a.gsc05f, 0)), 0) from #gsm03_tmp a where a.gsc02c = 'gz' and isnull(a.isnotcard, 0) = 1 and gsc04c = 'ZFB' and gsc12i = 6 insert #tbl_profit(code, itemname, is_add, is_leaf, amt, payment, kind) values('1103Z2', '产品销售收入-还款-支付宝', 0, 1, @amt, 'ZFB', 9) select @amt = 0 select @amt = isnull(sum(isnull(a.gsc05f, 0)), 0) from #gsm03_tmp a where a.gsc02c = 'gz' and isnull(a.isnotcard, 0) = 1 and gsc04c = 'WX' and gsc12i = 6 insert #tbl_profit(code, itemname, is_add, is_leaf, amt, payment, kind) values('1103W2', '产品销售收入-还款-微信', 0, 1, @amt, 'WX', 9) end --加上加号标志 update #tbl_profit set itemname = itemname + '( + )' where is_add = 1 and is_leaf = 1 --费用 insert #tbl_profit(code, itemname, is_add, is_leaf) values('12', '费用', -1, 0) --其他收入 Huzy 2012/05/31 insert #tbl_profit(code, itemname, is_add, is_leaf) values('18', '其他收入', 1, 0) --虚拟收入 insert #tbl_profit(code, itemname, is_add, is_leaf) values('n1', '虚拟收入', 1, 0) --其他收入 insert #tbl_profit(code, itemname, is_add, is_leaf) values('n2', '虚拟支出', -1, 0) create table #group_fee ( idd int identity not null, code varchar(20) not null, type varchar(20) not null, -- 其他收入 gp /费用 gv name varchar(40) null, -- MODIFY BY LIMIN 2015/02/05 amt float null, type2 int null, primary key(idd) ) -- 计算总的 insert #group_fee(code, type, amt) select isnull(gqb03c, ''), isnull(gqa03c, ''), isnull(sum(isnull(gqb05f, 0)), 0) from gqm02 b with(nolock), gqm01 a with(nolock), gam26 c with(nolock) where b.gqb00c = a.gqa00c and b.gqb01c = a.gqa01c and a.gqa00c = c.gaz02c and a.gqa80d >= @fromdate and a.gqa80d <= @todate and a.gqa95c like @staff and c.gaz01c = @incompid and isnull(gqb19i, 0) = 0 -- 是否为虚拟款 group by isnull(gqb03c, ''), isnull(gqa03c, '') order by isnull(gqa03c, '')desc, isnull(gqb03c, '') --计算虚拟 insert #group_fee(code, type, amt, type2) select isnull(gqb03c, ''), isnull(gqa03c, ''), isnull(sum(isnull(gqb05f, 0)), 0), isnull(gqb19i, 0) from gqm02 b with(nolock), gqm01 a with(nolock), gam26 c with(nolock) where b.gqb00c = a.gqa00c and b.gqb01c = a.gqa01c and a.gqa00c = c.gaz02c and a.gqa80d >= @fromdate and a.gqa80d <= @todate and a.gqa95c like @staff and c.gaz01c = @incompid and isnull(gqb19i, 0) = 1--是否为虚拟款 group by isnull(gqb03c, ''), isnull(gqa03c, ''), isnull(gqb19i, 0) order by isnull(gqa03c, '')desc, isnull(gqb03c, '') update a set a.name = b.gsb03c from #group_fee a, gsm02 b with(nolock) where b.gsb00c = @incompid and b.gsb01c = 'N' and b.gsb02c = a.code and a.type = 'gv' update a set a.name = b.gsb03c from #group_fee a, gsm02 b with(nolock) where b.gsb00c = @incompid and b.gsb01c = 'A5' and b.gsb02c = a.code and a.type = 'gp' update a set a.code = '12' + right(('000' + convert(varchar(10), a.idd)), 2) from #group_fee a where a.type = 'gv' and type2 is null update a set a.code = '18' + right(('000' + convert(varchar(10), a.idd)), 2) from #group_fee a where a.type = 'gp' and type2 is null -- 虚拟收入 update a set a.code = 'n1' + right(('000' + convert(varchar(10), a.idd)), 2) from #group_fee a where a.type = 'gp' and type2 = 1 -- 虚拟支出 update a set a.code = 'n2' + right(('000' + convert(varchar(10), a.idd)), 2) from #group_fee a where a.type = 'gv' and type2 = 1 insert #tbl_profit(code, itemname, amt, is_add, is_leaf) select code, isnull(name, ''), amt, case when type = 'gv' then -1 when type = 'gp' then 1 else 0 end, 1 from #group_fee order by type --费用-房租 Added by WUXM 2006/02/09 declare @rent float declare @range int declare @flow int declare @code varchar(20) declare @before varchar(10) declare @after varchar(10) declare @cur varchar(10) select @before = substring(convert(varchar(10), dateadd(day, -1, cast(@fromdate as datetime)), 120), 6, 2) select @after = substring(convert(varchar(10), dateadd(day, 1, cast(@todate as datetime)), 120), 6, 2) select @cur = substring(convert(varchar(10), cast(@fromdate as datetime), 120), 6, 2) if (@before = '12' and @cur = '01' and @after = '02') begin select @rent = sum(isnull(gae18f, 0)) from gam05 a with(nolock), gam26 z with(nolock) where a.gae01c = z.gaz02c and z.gaz01c = @incompid end else if (@before = '11' and @cur = '12' and @after = '01') begin select @rent = sum(isnull(gae18f, 0)) from gam05 a with(nolock), gam26 z with(nolock) where a.gae01c = z.gaz02c and z.gaz01c = @incompid end else if ((@before < @cur) and (@cur < @after)) --从月初到月末的统计 begin select @rent = sum(isnull(gae18f, 0)) from gam05 a with(nolock), gam26 z with(nolock) where a.gae01c = z.gaz02c and z.gaz01c = @incompid end else begin select @range = datediff(day, cast(@fromdate as datetime), cast(@todate as datetime)) select @rent = sum(isnull(gae18f, 0))*(@range + 1)/30 from gam05 a with(nolock), gam26 z with(nolock) where a.gae01c = z.gaz02c and z.gaz01c = @incompid end select @code = '1299' -- + right('000' + convert(varchar(10), @flow), 2) if (@rent > 0) begin insert #tbl_profit(code, itemname, amt, is_add, is_leaf) values (@code, '房租', @rent, -1, 1) end drop table #group_fee select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit where code like '12%' and is_leaf = 1 update #tbl_profit set amt = isnull(@amt, 0) where code = '12' select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit where code like '18%' and is_leaf = 1 update #tbl_profit set amt = isnull(@amt, 0) where code = '18' select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit where code like 'n1%' and is_leaf = 1 update #tbl_profit set amt = isnull(@amt, 0) where code = 'n1' select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit where code like 'n2%' and is_leaf = 1 update #tbl_profit set amt = isnull(@amt, 0) where code = 'n2' --成本 insert #tbl_profit(code, itemname, is_add, is_leaf) values('13', '成本', -1, 0) select @amt = 0 select @amt = sum(isnull(hah05f, 0)) from ham08 a with(nolock), gam26 z with(nolock) where a.hah00c = z.gaz02c and z.gaz01c = @incompid and hah03d >= @fromdate and hah03d <= @todate insert #tbl_profit(code, itemname, amt, is_add, is_leaf) values('1301', '成本-日常奖罚', @amt, -1, 1) --计算总提成 declare @spid int select @spid = @@spid if @flag = 0 begin create table #tmp_rst(amt float null) insert #tmp_rst(amt) exec upg_personal_statistics @incompid, @fromdate, @todate, '*', '', 1, 1, 1, 6, @spid -- 6:所有员工提成总数据 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tmp_rst drop table #tmp_rst insert #tbl_profit(code, itemname, amt, is_add, is_leaf) values('1302', '成本-员工提成', @amt, -1, 1) end select @amt = 0 select @amt = sum(isnull(ggc05f, 0)*isnull(gfa19f, 0)) from ggm03 a with (nolock), #ggm01_1 b, gfm01 c with(nolock) where a.ggc00c = b.gga00c and a.ggc01c = b.gga01c and a.ggc00c = c.gfa00c and a.ggc03c = c.gfa01c select @amt = isnull(@amt, 0) + isnull(sum(isnull(gge05f, 0)*isnull(gfa15f, 0)), 0) from ggm05 a with (nolock), #ggm01_1 b, gfm01 c with(nolock) where a.gge00c = b.gga00c and a.gge01c = b.gga01c and a.gge00c = c.gfa00c and a.gge03c = c.gfa01c insert #tbl_profit(code, itemname, amt, is_add, is_leaf) values('1303', '成本-产品', isnull(@amt, 0), -1, 1) --成本-固定工资 select @amt = 0 select @amt = sum(isnull(hbb05f, 0)) from hbm02 a with(nolock), gam26 z with(nolock), ham01 c with(nolock) where a.hbb00c = z.gaz02c and z.gaz01c = @incompid and hbb04c = '1' and hbb03c = '001' --基本工资项目 and hbb00c = haa00c and hbb01c = haa01c and haa27c = '2' if (@before = '12' and @cur = '01' and @after = '02') select @amt = @amt else if (@before = '11' and @cur = '12' and @after = '01') select @amt = @amt else if ((@before < @cur) and (@cur < @after)) --从月初到月末的统计 select @amt = @amt else select @amt = @amt * (@range + 1)/30 select @amt = convert(numeric(20, 5), @amt) insert #tbl_profit(code, itemname, amt, is_add, is_leaf) values('1304', '成本-固定工资', @amt, -1, 1) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit where code like '13%' and is_leaf = 1 update #tbl_profit set amt = isnull(@amt, 0) where code = '13' --删除为了性能必须要得ggm01的缓冲表 drop table #ggm01_1 --计算利润 select @amt = 0 select @amt = sum(isnull(amt*is_add, 0)) from #tbl_profit where len(code) = 2 insert #tbl_profit(code, itemname, amt, is_add, is_leaf) values('19', '利润', isnull(@amt, 0), 0, 1) --单位面积利润率 declare @area float select @area = sum(isnull(gae17f, 0)) from gam05 a with(nolock), gam26 z with(nolock) where a.gae01c = z.gaz02c and z.gaz01c = @incompid if (@area > 0) begin insert #tbl_profit(code, itemname, amt, is_add, is_leaf) values (20, '单位面积利润', @amt/@area, 0, 1) end --储值余额 declare @card_total_num float select @amt = 0 select @amt = sum(isnull(gcc06f, 0)) from gcm03 a with (nolock), gcm01 b with (nolock), gam26 z with(nolock) where a.gcc00c = z.gaz02c and z.gaz01c = @incompid and (gcc03i <> 1 and gcc03i<>0 and gcc03i <> 3 and gcc03i<888) and (gca08i = 4 or gca08i = 5 or gca08i = 10) and gca00c = gcc00c and gca01c = gcc01c and gca00c = gca13d select @card_total_num = 0 select @card_total_num = count(*) from gcm01 a with (nolock), gam26 z with(nolock) where a.gca00c = z.gaz02c and z.gaz01c = @incompid and (gca08i = 4 or gca08i = 5 or gca08i = 10) and a.gca00c = a.gca13d and gca01c not like '散客%' insert #tbl_profit(code, itemname, amt, is_add, is_leaf) values('14', '账户总余额(' + convert(varchar(20), @card_total_num) + '张卡)', isnull(@amt, 0), 0, 1) --统计过期卡的余额,不包括积分 declare @invaldcard_remain float select @invaldcard_remain = 0 select @invaldcard_remain = sum(isnull(gcc06f, 0)) from gcm03 a with (nolock), gcm01 b with (nolock), gam26 z with(nolock) where a.gcc00c = z.gaz02c and z.gaz01c = @incompid and (gcc03i <> 1 and gcc03i<>0 and gcc03i <> 3 and gcc03i<888) and (gca08i = 8) and gca00c = gcc00c and gca01c = gcc01c and gca00c = gca13d insert #tbl_profit(code, itemname, amt, is_add, is_leaf) values('15', '过期卡余额', isnull(@invaldcard_remain, 0), 0, 1) declare @pointssum float select @pointssum = 0 select @pointssum = sum(isnull(gcd06f, 0)) from gcm04 a with(nolock), gam26 z with(nolock) where gcd09d >= @fromdate and gcd09d <= @todate and gcd02i = 3 and a.gcd00c = z.gaz02c and z.gaz01c = @incompid and gcd05i = 0 insert #tbl_profit(code, itemname, amt, is_add, is_leaf) values('16', '赠送积分', isnull(@pointssum, 0), 0, 1) -----Add by leiji on 2012-7-18 [赠送账户金额] declare @sp147 varchar(160) select @sp147 = gsa02c from gsm01 with (nolock) where gsa00c = @incompid and gsa01c = 'SP147' select @pointssum = 0 select @pointssum = sum(isnull(gcc06f, 0)) from gcm03 a with (nolock), gcm01 b with (nolock), gam26 z with(nolock) where a.gcc00c = z.gaz02c and z.gaz01c = @incompid and gcc03i = @sp147 and (gca08i = 4 or gca08i = 5 or gca08i = 10) and gca00c = gcc00c and gca01c = gcc01c and gca00c = gca13d insert #tbl_profit(code, itemname, amt, is_add, is_leaf) values('17', '赠送账户金额', isnull(@pointssum, 0), 0, 1) --删除第二级为0的 delete #tbl_profit where isnull(amt, 0) = 0 and len(code) > 4 drop table #gsm03_tmp --储值账户减掉未复核的金额 UPDATE #tbl_profit SET amt = ( amt - @NoCheckAmount ) where code = '11020504' select isnull(idd,'') idd, isnull(code,'') code, isnull(itemname,'') itemname, isnull(is_add,'') is_add, isnull(amt,'') amt, isnull(is_leaf,'') is_leaf, isnull(payment,'') payment, isnull(kind,'') kind from #tbl_profit order by code if ((@staff = '' or @staff = '*' or @staff = '%') and @flag = 1 and @fromdate = @todate) begin delete gcm21 where gcu00c = @incompid and gcu01c = @todate --gcm21的每个公司的统计数据都已经包括了下面的所有数据 insert gcm21(gcu00c, gcu01c, gcu02i, gcu03c, gcu04c, gcu05i, gcu06f, gcu07i, gcu08c, gcu09i) select @incompid, @todate, idd, code, itemname, is_add, isnull(amt, 0), isnull(is_leaf,''), isnull(payment,''), isnull(kind,'') from #tbl_profit end drop table #gcm10_888 -- 服务或产品欠款还款的纪录 drop table #tbl_profit drop table #tbl_stat_fill end GO /****** Object: StoredProcedure [dbo].[upg_ing01_confirm] Script Date: 06/12/2017 06:20:12 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_ing01_confirm] ( @compid varchar(10), -- 公司编号 @billno varchar(20), -- 配料单号 @billdate varchar(8), -- 单据日期 @checkman varchar(10), -- 复核人 @currdate varchar(8), -- 复核日期 @flag int -- 标志位 1-复核 0-取消复核 ) as begin set nocount on -- 消耗是否直接扣库存 declare @sp208_3 float select @sp208_3 = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP208' declare @sp208_2 varchar(10) select @sp208_2 = gsa02c from gsm01 where gsa00c = @compid and gsa01c = 'SP208' select @sp208_2 = isnull(@sp208_2, '01') declare @has_goods int if @sp208_3 <> 1 return 0 declare @ina03c varchar(20) select @ina03c = ina03c from ing01 where ina00c = @compid and ina01c = @billno -- 当前的事务数 declare @__transaction_count int set @__transaction_count = @@trancount -- try...catch处理 begin try -- a.开启事务,或者设置事务保存点 if @__transaction_count = 0 begin tran else save tran __tran_SavePoint declare @curdate_tmp varchar(20) declare @curtime varchar(20) exec upg_getcurrdate @curdate_tmp output, @curtime output declare @__sp_re int if @flag = 1 --复核 begin insert gfm03(gfc00c, gfc01c, gfc02c, gfc03c, gfc040c, gfc041c, gfc05c, gfc06i, gfc08i, gfc07c, gfc09t) select ina00c, '8', ina01c, ina02d, '3', @sp208_2, @sp208_2, 1, 1, ina91c, ina06t from ing01 with (nolock) where ina00c = @compid and ina01c = @billno insert gfm04(gfd00c, gfd01c, gfd02c, gfd03f, gfd04c, gfd05f, gfd06f, gfd07c, gfd08c, gfd11c, gfd12f, gfd13f) select inb00c, '8', inb01c, row_number() over(order by inb00c, inb01c, inb02c, inb04c), inb04c, isnull(inb05f / isnull(case when isnull(gfa09f, 0) = 0 then 1 else gfa09f end, 1), 0), isnull(inb06f, 0) * (case when isnull(gfa09f, 0) = 0 then 1 else gfa09f end), @billdate, @sp208_2, inb07c, isnull(inb05f, 0), isnull(inb05f, 0) * isnull(inb06f, 0) from ing02 with (nolock), gfm01 where inb00c = @compid and inb01c = @billno and inb00c = gfa00c and inb04c = gfa01c update gf set gf.gfb06f = isnull(gf.gfb06f, 0) + isnull(inb05f / isnull(case when isnull(gfa09f, 0) = 0 then 1 else gfa09f end, 1), 0) from gfm02 gf, ing02 gi, gfm01 c where gi.inb00c = @compid and gi.inb01c = @billno and gfb00c = inb00c and inb04c = gfb01c and gfb02c = substring(@billdate, 1, 6) and gfb04c = @sp208_2 and gfb03c = '2' and inb00c = gfa00c and inb04c = gfa01c insert into gfm02(gfb00c, gfb01c, gfb02c, gfb03c, gfb04c, gfb06f) select inb00c, inb04c, substring(@billdate, 1, 6), '2', @sp208_2, isnull(inb05f / isnull(case when isnull(gfa09f, 0) = 0 then 1 else gfa09f end, 1), 0) from ing02 with (nolock), gfm01 c where inb00c = @compid and inb01c = @billno and inb04c not in (select gfb01c from gfm02 where gfb00c = inb00c and gfb01c = inb04c and gfb02c = substring(@billdate, 1, 6) and gfb03c = '2' and gfb04c = @sp208_2 ) and inb00c = gfa00c and inb04c = gfa01c update ing01 set ina93c = @checkman, ina94d = @currdate where ina00c = @compid and ina01c = @billno insert sysoperationlog(userid, program, operation, operationdate, operationtime, origatedate, compid, keyvalue1, keyvalue2, keyvalue3) values(@checkman, 'GIA07I', 'M', @currdate, @curtime, @currdate, @compid, @billno, @sp208_2, '复核') end else if @flag = 0 --取消复核 begin delete from gfm03 where gfc00c = @compid and gfc01c = '8' and gfc02c = @billno delete from gfm04 where gfd00c = @compid and gfd01c = '8' and gfd02c = @billno update gf set gf.gfb06f = isnull(gf.gfb06f, 0) - isnull(inb05f / isnull(case when isnull(gfa09f, 0) = 0 then 1 else gfa09f end, 1), 0) from gfm02 gf, ing02 gi, gfm01 c where gi.inb00c = @compid and gi.inb01c = @billno and gfb00c = inb00c and inb04c = gfb01c and gfb02c = substring(@billdate, 1, 6) and gfb04c = @sp208_2 and gfb03c = '2' and inb00c = gfa00c and inb04c = gfa01c update ing01 set ina93c = '', ina94d = '' where ina00c = @compid and ina01c = @billno insert sysoperationlog(userid, program, operation, operationdate, operationtime, origatedate, compid, keyvalue1, keyvalue2, keyvalue3) values(@checkman, 'GIA07I', 'M', @currdate, @curtime, @currdate, @compid, @billno, @sp208_2, '取消复核') end if @__sp_re <> 0 raiserror('has some error', 16, 1) -- 提交事务 -- 有可提交的事务, 并且事务是在当前模块中开启的情况下, 才提交事务 if xact_state() = 1 and @__transaction_count = 0 commit -- 为了防止TRY 中有遗漏的事务处理, 可以在TRY 模拟的结束部分做最终的判断 if @__transaction_count = 0 begin if xact_state() = -1 rollback tran else begin while @@trancount > 0 commit tran end end end try begin catch -- 在CATCH 模块, 应该首先处理事务 if xact_state() <> 0 begin if @__transaction_count = 0 rollback tran -- xact_state 为-1 时, 不能回滚到事务保存点, 这种情况留给外层调用者做统一的事务回滚 -- 通过@@trancount > @__transaction_count 的判断, 即使在TRY 模块中没有设置事务保存点的情况下跳到此步骤, 也不会出错 else if xact_state() = 1 and @@trancount > @__transaction_count rollback tran __tran_SavePoint end declare @SP_Parameters varchar(max) set @SP_Parameters = '@compid = ' + isnull(@compid, '') + '@billno = ' + isnull(@billno, '') + '@billdate = ' + isnull(@billdate, '') + '@checkman = ' + isnull(@checkman, '') + '@currdate = ' + isnull(@currdate, '') + '@flag = ' + (case when @flag is null then '' else convert(varchar(100), @flag) end) insert into SP_TranLog(SPName, SPTime, SPParameter) values('upg_ing01_confirm', getdate(), @SP_Parameters) return -1 end catch end GO /****** Object: StoredProcedure [dbo].[upg_prepare_fill_card_detail] Script Date: 06/12/2017 06:20:27 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_prepare_fill_card_detail] ( @compid varchar(10), -- 公司编号 @billtype varchar(5), -- 单据类别 'gn' = 会员卡销售 'gz' = 会员帐户管理 @billid varchar(20) -- 单号 ) as begin delete card_fill_detail_by_payment where billtype = @billtype and billid = @billid and compid = @compid declare @cardid varchar(20) -- 会员卡号 if @billtype = 'gn' begin select @cardid = gna13c from gnm01 with (nolock) where gna00c = @compid and gna01c = @billid end else if @billtype = 'gz' begin select @cardid = gcl03c from gcm10 with (nolock) where gcl00c = @compid and gcl01c = @billid end declare @loop_cnt int select @loop_cnt = 1 declare @filltype int -- 1-卡充值 2-疗程 3-套餐 -- 支付的缓冲表 create table #gsm03_detail ( idd int identity not null, -- 序号 cardid varchar(20) null, -- 会员卡号 code varchar(20) null, -- 代码 卡代码, 套餐代码, 疗程代码 paycode varchar(20) not null, -- 支付代码 payamt float null, -- 支付金额 cardtype varchar(20) null, -- 卡类别 times float null, -- 疗程或套餐的次数 procid float null, -- 疗程的价格序号 pctimes float null, -- 疗程或套餐的实际次数 primary key(idd) ) while @loop_cnt <= 3 begin -- 把支付方式准备 delete #gsm03_detail if @billtype = 'gn' begin if @loop_cnt = 1 begin insert #gsm03_detail(cardid, code, paycode, payamt, times, cardtype) select gnb03c, gnb03c, gnb15c, isnull(gnb16f, 0), isnull(gnb16f, 0) / isnull(gnb07f, 1), isnull(gnb02c,'') from gnm02 a with (nolock) where a.gnb00c = @compid and a.gnb01c = @billid and isnull(gnb15c, '') <> '' insert #gsm03_detail(cardid, code, paycode, payamt, times, cardtype) select gnb03c, gnb03c, gnb17c, isnull(gnb18f, 0), isnull(gnb18f, 0) / isnull(gnb07f, 1), isnull(gnb02c,'') from gnm02 a with (nolock) where a.gnb00c = @compid and a.gnb01c = @billid and isnull(gnb17c, '') <> '' insert #gsm03_detail(cardid, code, paycode, payamt, times, cardtype) select gnb03c, gnb03c, gnb19c, isnull(gnb20f, 0), isnull(gnb20f, 0) / isnull(gnb07f, 1), isnull(gnb02c,'') from gnm02 a with (nolock) where a.gnb00c = @compid and a.gnb01c = @billid and isnull(gnb19c, '') <> '' insert #gsm03_detail(cardid, code, paycode, payamt, times, cardtype) select gnb03c, gnb03c, gnb21c, isnull(gnb22f, 0), isnull(gnb22f, 0) / isnull(gnb07f, 1), isnull(gnb02c,'') from gnm02 a with (nolock) where a.gnb00c = @compid and a.gnb01c = @billid and isnull(gnb21c, '') <> '' end else if @loop_cnt = 2 -- 疗程支付 begin insert #gsm03_detail(cardid, code, paycode, payamt, times, procid, pctimes) select @cardid, gnc02c, gnc15c, isnull(gnc16f, 0), gnc03f, gnc09i, dbo.fun_divide_default(isnull(gnc16f, 0), isnull(gnc16f, 0) + isnull(gnc18f, 0) + isnull(gnc20f, 0) + isnull(gnc24f, 0), 0) from gnm03 a with (nolock) where a.gnc00c = @compid and a.gnc01c = @billid and isnull(gnc15c, '') <> '' insert #gsm03_detail(cardid, code, paycode, payamt, times, procid, pctimes) select @cardid, gnc02c, gnc17c, isnull(gnc18f, 0), gnc03f, gnc09i, dbo.fun_divide_default(isnull(gnc18f, 0), isnull(gnc16f, 0) + isnull(gnc18f, 0) + isnull(gnc20f, 0) + isnull(gnc24f, 0), 0) from gnm03 a with (nolock) where a.gnc00c = @compid and a.gnc01c = @billid and isnull(gnc17c, '') <> '' insert #gsm03_detail(cardid, code, paycode, payamt, times, procid, pctimes) select @cardid, gnc02c, gnc19c, isnull(gnc20f, 0), gnc03f, gnc09i, dbo.fun_divide_default(isnull(gnc20f, 0), isnull(gnc16f, 0) + isnull(gnc18f, 0) + isnull(gnc20f, 0) + isnull(gnc24f, 0), 0) from gnm03 a with (nolock) where a.gnc00c = @compid and a.gnc01c = @billid and isnull(gnc19c, '') <> '' insert #gsm03_detail(cardid, code, paycode, payamt, times, procid, pctimes) select @cardid, gnc02c, gnc23c, isnull(gnc24f, 0), gnc03f, gnc09i, dbo.fun_divide_default(isnull(gnc24f, 0), isnull(gnc16f, 0) + isnull(gnc18f, 0) + isnull(gnc20f, 0) + isnull(gnc24f, 0), 0) from gnm03 a with (nolock) where a.gnc00c = @compid and a.gnc01c = @billid and isnull(gnc23c, '') <> '' -- --减掉欠款的(统一做现金支付方式算) -- insert #gsm03_detail(cardid, code, paycode, payamt, times, procid, pctimes) -- select @cardid, gnc02c, '1', (0-isnull(gnc33f, 0)), gnc03f, gnc09i, dbo.fun_divide_default(isnull(gnc16f, 0), isnull(gnc16f, 0) + isnull(gnc18f, 0) + isnull(gnc20f, 0) + isnull(gnc24f, 0), 0) -- from gnm03 a with (nolock) -- where a.gnc00c = @compid -- and a.gnc01c = @billid -- and isnull(gnc15c, '') <> '' end else if @loop_cnt = 3 -- 套餐支付 begin insert #gsm03_detail(cardid, code, paycode, payamt, times, pctimes) select @cardid, gtf02c, gtf09c, isnull(gtf10f, 0), gtf04f, dbo.fun_divide_default(isnull(gtf10f, 0), isnull(gtf10f, 0) + isnull(gtf12f, 0) + isnull(gtf14f, 0) + isnull(gtf16f, 0), 0) from gtc05 a with (nolock) where a.gtf00c = @compid and a.gtf01c = @billid and isnull(gtf09c, '') <> '' insert #gsm03_detail(cardid, code, paycode, payamt, times, pctimes) select @cardid, gtf02c, gtf11c, isnull(gtf12f, 0), gtf04f, dbo.fun_divide_default(isnull(gtf12f, 0), isnull(gtf10f, 0) + isnull(gtf12f, 0) + isnull(gtf14f, 0) + isnull(gtf16f, 0), 0) from gtc05 a with (nolock) where a.gtf00c = @compid and a.gtf01c = @billid and isnull(gtf11c, '') <> '' insert #gsm03_detail(cardid, code, paycode, payamt, times, pctimes) select @cardid, gtf02c, gtf13c, isnull(gtf14f, 0), gtf04f, dbo.fun_divide_default(isnull(gtf14f, 0), isnull(gtf10f, 0) + isnull(gtf12f, 0) + isnull(gtf14f, 0) + isnull(gtf16f, 0), 0) from gtc05 a with (nolock) where a.gtf00c = @compid and a.gtf01c = @billid and isnull(gtf13c, '') <> '' insert #gsm03_detail(cardid, code, paycode, payamt, times, pctimes) select @cardid, gtf02c, gtf15c, isnull(gtf16f, 0), gtf04f, dbo.fun_divide_default(isnull(gtf16f, 0), isnull(gtf10f, 0) + isnull(gtf12f, 0) + isnull(gtf14f, 0) + isnull(gtf16f, 0), 0) from gtc05 a with (nolock) where a.gtf00c = @compid and a.gtf01c = @billid and isnull(gtf15c, '') <> '' -- --减掉欠款的(统一做现金支付方式算) -- insert #gsm03_detail(cardid, code, paycode, payamt, times, pctimes) -- select @cardid, gtf02c, '1', (0-isnull(gtf22f, 0)), gtf04f, dbo.fun_divide_default(isnull(gtf10f, 0), isnull(gtf10f, 0) + isnull(gtf12f, 0) + isnull(gtf14f, 0) + isnull(gtf16f, 0), 0) -- from gtc05 a with (nolock) -- where a.gtf00c = @compid -- and a.gtf01c = @billid -- and isnull(gtf09c, '') <> '' end end else if @billtype = 'gz' begin if @loop_cnt = 1 -- 充值部分 begin insert #gsm03_detail(cardid, code, paycode, payamt, times, cardtype) --add by lm 2015-10-10(cardtype) select gcl03c, gcl03c, gcl67c, isnull(gcl68f, 0), isnull(gcl68f, 0) / isnull(gcl53f, 1), isnull(gcl15c,'') from gcm10 a with (nolock) where a.gcl00c = @compid and a.gcl01c = @billid and isnull(gcl67c, '') <> '' and isnull(gcl112i,0) = 0 insert #gsm03_detail(cardid, code, paycode, payamt, times, cardtype)--add by lm 2015-10-10(cardtype) select gcl03c, gcl03c, gcl69c, isnull(gcl70f, 0), isnull(gcl70f, 0) / isnull(gcl53f, 1), isnull(gcl15c,'') from gcm10 a with (nolock) where a.gcl00c = @compid and a.gcl01c = @billid and isnull(gcl69c, '') <> '' and isnull(gcl112i,0) = 0 insert #gsm03_detail(cardid, code, paycode, payamt, times, cardtype)--add by lm 2015-10-10(cardtype) select gcl03c, gcl03c, gcl71c, isnull(gcl72f, 0), isnull(gcl72f, 0) / isnull(gcl53f, 1), isnull(gcl15c,'') from gcm10 a with (nolock) where a.gcl00c = @compid and a.gcl01c = @billid and isnull(gcl71c, '') <> '' and isnull(gcl112i,0) = 0 insert #gsm03_detail(cardid, code, paycode, payamt, times, cardtype)--add by lm 2015-10-10(cardtype) select gcl03c, gcl03c, gcl73c, isnull(gcl74f, 0), isnull(gcl74f, 0) / isnull(gcl53f, 1), isnull(gcl15c,'') from gcm10 a with (nolock) where a.gcl00c = @compid and a.gcl01c = @billid and isnull(gcl73c, '') <> '' and isnull(gcl112i,0) = 0 end else if @loop_cnt = 2 -- 疗程支付 begin insert #gsm03_detail(cardid, code, paycode, payamt, times, procid, pctimes) select @cardid, gcm02c, gcm15c, isnull(gcm16f, 0), gcm03f, gcm09i, dbo.fun_divide_default(isnull(gcm16f, 0), isnull(gcm16f, 0) + isnull(gcm18f, 0) + isnull(gcm20f, 0) + isnull(gcm24f, 0), 0) from gcm11 a with (nolock) where a.gcm00c = @compid and a.gcm01c = @billid and isnull(gcm15c, '') <> '' insert #gsm03_detail(cardid, code, paycode, payamt, times, procid, pctimes) select @cardid, gcm02c, gcm17c, isnull(gcm18f, 0), gcm03f, gcm09i, dbo.fun_divide_default(isnull(gcm18f, 0), isnull(gcm16f, 0) + isnull(gcm18f, 0) + isnull(gcm20f, 0) + isnull(gcm24f, 0), 0) from gcm11 a with (nolock) where a.gcm00c = @compid and a.gcm01c = @billid and isnull(gcm17c, '') <> '' insert #gsm03_detail(cardid, code, paycode, payamt, times, procid, pctimes) select @cardid, gcm02c, gcm19c, isnull(gcm20f, 0), gcm03f, gcm09i, dbo.fun_divide_default(isnull(gcm20f, 0), isnull(gcm16f, 0) + isnull(gcm18f, 0) + isnull(gcm20f, 0) + isnull(gcm24f, 0), 0) from gcm11 a with (nolock) where a.gcm00c = @compid and a.gcm01c = @billid and isnull(gcm19c, '') <> '' insert #gsm03_detail(cardid, code, paycode, payamt, times, procid, pctimes) select @cardid, gcm02c, gcm23c, isnull(gcm24f, 0), gcm03f, gcm09i, dbo.fun_divide_default(isnull(gcm24f, 0), isnull(gcm16f, 0) + isnull(gcm18f, 0) + isnull(gcm20f, 0) + isnull(gcm24f, 0), 0) from gcm11 a with (nolock) where a.gcm00c = @compid and a.gcm01c = @billid and isnull(gcm23c, '') <> '' -- --减掉欠款的(统一做现金支付方式算) -- insert #gsm03_detail(cardid, code, paycode, payamt, times, procid, pctimes) -- select @cardid, gcm02c, '1', (0-isnull(gcm32f, 0)), gcm03f, gcm09i, dbo.fun_divide_default(isnull(gcm16f, 0), isnull(gcm16f, 0) + isnull(gcm18f, 0) + isnull(gcm20f, 0) + isnull(gcm24f, 0), 0) -- from gcm11 a with (nolock) -- where a.gcm00c = @compid -- and a.gcm01c = @billid -- and isnull(gcm15c, '') <> '' --还款 insert #gsm03_detail(cardid, code, paycode, payamt, times, procid, pctimes) select @cardid, gcm02c, gsc04c, isnull(gcm33f, 0), gcm03f, gcm09i, dbo.fun_divide_default(isnull(gcm16f, 0), isnull(gcm16f, 0) + isnull(gcm18f, 0) + isnull(gcm20f, 0) + isnull(gcm24f, 0), 0) from gcm11 a with (nolock), gsm03 b with(nolock) where a.gcm00c = b.gsc00c and a.gcm30c = b.gsc01c and b.gsc02c = 'gz' and a.gcm00c = @compid and a.gcm30c = @billid and isnull(gcm33f, 0) > 0 insert #gsm03_detail(cardid, code, paycode, payamt, times, procid, pctimes) select @cardid, gnc02c, gsc04c, isnull(gnc35f, 0), gnc03f, gnc09i, dbo.fun_divide_default(isnull(gnc16f, 0), isnull(gnc16f, 0) + isnull(gnc18f, 0) + isnull(gnc20f, 0) + isnull(gnc24f, 0), 0) from gnm03 a with (nolock), gsm03 b with(nolock) where a.gnc00c = b.gsc00c and a.gnc31c = b.gsc01c and b.gsc02c = 'gz' and a.gnc00c = @compid and a.gnc31c = @billid and isnull(gnc35f, 0) > 0 end else if @loop_cnt = 3 -- 套餐支付 begin insert #gsm03_detail(cardid, code, paycode, payamt, times, pctimes) select @cardid, gth02c, gth09c, isnull(gth10f, 0), gth04f, dbo.fun_divide_default(isnull(gth10f, 0), isnull(gth10f, 0) + isnull(gth12f, 0) + isnull(gth14f, 0) + isnull(gth16f, 0), 0) from gtc06 a with (nolock) where a.gth00c = @compid and a.gth01c = @billid and isnull(gth09c, '') <> '' insert #gsm03_detail(cardid, code, paycode, payamt, times, pctimes) select @cardid, gth02c, gth11c, isnull(gth12f, 0), gth04f, dbo.fun_divide_default(isnull(gth12f, 0), isnull(gth10f, 0) + isnull(gth12f, 0) + isnull(gth14f, 0) + isnull(gth16f, 0), 0) from gtc06 a with (nolock) where a.gth00c = @compid and a.gth01c = @billid and isnull(gth11c, '') <> '' insert #gsm03_detail(cardid, code, paycode, payamt, times, pctimes) select @cardid, gth02c, gth13c, isnull(gth14f, 0), gth04f, dbo.fun_divide_default(isnull(gth14f, 0), isnull(gth10f, 0) + isnull(gth12f, 0) + isnull(gth14f, 0) + isnull(gth16f, 0), 0) from gtc06 a with (nolock) where a.gth00c = @compid and a.gth01c = @billid and isnull(gth13c, '') <> '' insert #gsm03_detail(cardid, code, paycode, payamt, times, pctimes) select @cardid, gth02c, gth15c, isnull(gth16f, 0), gth04f, dbo.fun_divide_default(isnull(gth16f, 0), isnull(gth10f, 0) + isnull(gth12f, 0) + isnull(gth14f, 0) + isnull(gth16f, 0), 0) from gtc06 a with (nolock) where a.gth00c = @compid and a.gth01c = @billid and isnull(gth15c, '') <> '' -- -- --减掉欠款的(统一做现金支付方式算) -- insert #gsm03_detail(cardid, code, paycode, payamt, times, pctimes) -- select @cardid, gth02c, '1', (0 -isnull(gth23f, 0)), gth04f, dbo.fun_divide_default(isnull(gth10f, 0), isnull(gth10f, 0) + isnull(gth12f, 0) + isnull(gth14f, 0) + isnull(gth16f, 0), 0) -- from gtc06 a with (nolock) -- where a.gth00c = @compid -- and a.gth01c = @billid -- and isnull(gth09c, '') <> '' --还款 insert #gsm03_detail(cardid, code, paycode, payamt, times, pctimes) select @cardid, gth02c, gsc04c, isnull(gth25f, 0), gth04f, dbo.fun_divide_default(isnull(gth10f, 0), isnull(gth10f, 0) + isnull(gth12f, 0) + isnull(gth14f, 0) + isnull(gth16f, 0), 0) from gtc06 a with (nolock), gsm03 b with(nolock) where a.gth00c = b.gsc00c and a.gth21c = b.gsc01c and b.gsc02c = 'gz' and a.gth00c = @compid and a.gth21c = @billid and isnull(gth25f, 0) > 0 insert #gsm03_detail(cardid, code, paycode, payamt, times, pctimes) select @cardid, gtf02c, gsc04c, isnull(gtf24f, 0), gtf04f, dbo.fun_divide_default(isnull(gtf10f, 0), isnull(gtf10f, 0) + isnull(gtf12f, 0) + isnull(gtf14f, 0) + isnull(gtf16f, 0), 0) from gtc05 a with (nolock), gsm03 b with(nolock) where a.gtf00c = b.gsc00c and a.gtf20c = b.gsc01c and b.gsc02c = 'gz' and a.gtf00c = @compid and a.gtf20c = @billid and isnull(gtf24f, 0) > 0 end end update a set a.cardtype = b.gca02c from #gsm03_detail a, gcm01 b where a.cardid = b.gca01c and b.gca00c = b.gca13d and isnull(a.cardtype,'') = '' --add by lm 2015-10-10(cardtype) if @billtype = 'gn' -- 卡销售部分 begin --第一销售人员 insert card_fill_detail_by_payment(compid, cardtype, cardid, billtype, billid, personid, share_rate, paycode, payamt, filltype, code, times, person_pos, procid, pctimes) select gna00c, cardtype, cardid, 'gn', gna01c, gna20c, isnull(gna27f, 1), paycode, payamt, @loop_cnt, code, times, 1, procid, pctimes from gnm01 a with (nolock), #gsm03_detail c where a.gna00c = @compid and a.gna01c = @billid and isnull(gna20c, '')<>'' --第二销售人员 insert card_fill_detail_by_payment(compid, cardtype, cardid, billtype, billid, personid, share_rate, paycode, payamt, filltype, code, times, person_pos, procid, pctimes) select gna00c, cardtype, cardid, 'gn', gna01c, gna28c, isnull(gna29f, 0), paycode, payamt, @loop_cnt, code, times, 2, procid, pctimes from gnm01 a with (nolock), #gsm03_detail c where a.gna00c = @compid and a.gna01c = @billid and isnull(gna28c, '')<>'' --第三销售人员 insert card_fill_detail_by_payment(compid, cardtype, cardid, billtype, billid, personid, share_rate, paycode, payamt, filltype, code, times, person_pos, procid, pctimes) select gna00c, cardtype, cardid, 'gn', gna01c, gna30c, isnull(gna31f, 0), paycode, payamt, @loop_cnt, code, times, 3, procid, pctimes from gnm01 a with (nolock), #gsm03_detail c where a.gna00c = @compid and a.gna01c = @billid and isnull(gna30c, '')<>'' --第四销售人员 insert card_fill_detail_by_payment(compid, cardtype, cardid, billtype, billid, personid, share_rate, paycode, payamt, filltype, code, times, person_pos, procid, pctimes) select gna00c, cardtype, cardid, 'gn', gna01c, gna32c, isnull(gna33f, 0), paycode, payamt, @loop_cnt, code, times, 4, procid, pctimes from gnm01 a with (nolock), #gsm03_detail c where a.gna00c = @compid and a.gna01c = @billid and isnull(gna32c, '')<>'' --第五销售人员 insert card_fill_detail_by_payment(compid, cardtype, cardid, billtype, billid, personid, share_rate, paycode, payamt, filltype, code, times, person_pos, procid, pctimes) select gna00c, cardtype, cardid, 'gn', gna01c, gna34c, isnull(gna35f, 0), paycode, payamt, @loop_cnt, code, times, 5, procid, pctimes from gnm01 a with (nolock), #gsm03_detail c where a.gna00c = @compid and a.gna01c = @billid and isnull(gna34c, '')<>'' --第六销售人员 insert card_fill_detail_by_payment(compid, cardtype, cardid, billtype, billid, personid, share_rate, paycode, payamt, filltype, code, times, person_pos, procid, pctimes) select gna00c, cardtype, cardid, 'gn', gna01c, gna36c, isnull(gna37f, 0), paycode, payamt, @loop_cnt, code, times, 6, procid, pctimes from gnm01 a with (nolock), #gsm03_detail c where a.gna00c = @compid and a.gna01c = @billid and isnull(gna36c, '')<>'' --第七销售人员 insert card_fill_detail_by_payment(compid, cardtype, cardid, billtype, billid, personid, share_rate, paycode, payamt, filltype, code, times, person_pos, procid, pctimes) select gna00c, cardtype, cardid, 'gn', gna01c, gna38c, isnull(gna39f, 0), paycode, payamt, @loop_cnt, code, times, 7, procid, pctimes from gnm01 a with (nolock), #gsm03_detail c where a.gna00c = @compid and a.gna01c = @billid and isnull(gna38c, '')<>'' --第八销售人员 insert card_fill_detail_by_payment(compid, cardtype, cardid, billtype, billid, personid, share_rate, paycode, payamt, filltype, code, times, person_pos, procid, pctimes) select gna00c, cardtype, cardid, 'gn', gna01c, gna40c, isnull(gna41f, 0), paycode, payamt, @loop_cnt, code, times, 8, procid, pctimes from gnm01 a with (nolock), #gsm03_detail c where a.gna00c = @compid and a.gna01c = @billid and isnull(gna40c, '')<>'' --第九销售人员 insert card_fill_detail_by_payment(compid, cardtype, cardid, billtype, billid, personid, share_rate, paycode, payamt, filltype, code, times, person_pos, procid, pctimes) select gna00c, cardtype, cardid, 'gn', gna01c, gna42c, isnull(gna43f, 0), paycode, payamt, @loop_cnt, code, times, 9, procid, pctimes from gnm01 a with (nolock), #gsm03_detail c where a.gna00c = @compid and a.gna01c = @billid and isnull(gna42c, '')<>'' --第十销售人员 insert card_fill_detail_by_payment(compid, cardtype, cardid, billtype, billid, personid, share_rate, paycode, payamt, filltype, code, times, person_pos, procid, pctimes) select gna00c, cardtype, cardid, 'gn', gna01c, gna44c, isnull(gna45f, 0), paycode, payamt, @loop_cnt, code, times, 10, procid, pctimes from gnm01 a with (nolock), #gsm03_detail c where a.gna00c = @compid and a.gna01c = @billid and isnull(gna44c, '')<>'' end else if @billtype = 'gz' begin --第一销售人员 insert card_fill_detail_by_payment(compid, cardtype, cardid, billtype, billid, personid, share_rate, paycode, payamt, filltype, code, times, person_pos, procid, pctimes) select gcl00c, cardtype, cardid, 'gz', gcl01c, gcl09c, isnull(gcl16f, 1), paycode, payamt, @loop_cnt, code, times, 1, procid, pctimes from gcm10 a with (nolock), #gsm03_detail c where a.gcl00c = @compid and a.gcl01c = @billid and isnull(gcl09c, '')<>'' --第二销售人员 insert card_fill_detail_by_payment(compid, cardtype, cardid, billtype, billid, personid, share_rate, paycode, payamt, filltype, code, times, person_pos, procid, pctimes) select gcl00c, cardtype, cardid, 'gz', gcl01c, gcl17c, isnull(gcl18f, 0), paycode, payamt, @loop_cnt, code, times, 2, procid, pctimes from gcm10 a with (nolock), #gsm03_detail c where a.gcl00c = @compid and a.gcl01c = @billid and isnull(gcl17c, '')<>'' --第三销售人员 insert card_fill_detail_by_payment(compid, cardtype, cardid, billtype, billid, personid, share_rate, paycode, payamt, filltype, code, times, person_pos, procid, pctimes) select gcl00c, cardtype, cardid, 'gz', gcl01c, gcl19c, isnull(gcl20f, 0), paycode, payamt, @loop_cnt, code, times, 3, procid, pctimes from gcm10 a with (nolock), #gsm03_detail c where a.gcl00c = @compid and a.gcl01c = @billid and isnull(gcl19c, '')<>'' --第四销售人员 insert card_fill_detail_by_payment(compid, cardtype, cardid, billtype, billid, personid, share_rate, paycode, payamt, filltype, code, times, person_pos, procid, pctimes) select gcl00c, cardtype, cardid, 'gz', gcl01c, gcl22c, isnull(gcl23f, 0), paycode, payamt, @loop_cnt, code, times, 4, procid, pctimes from gcm10 a with (nolock), #gsm03_detail c where a.gcl00c = @compid and a.gcl01c = @billid and isnull(gcl22c, '')<>'' --第五销售人员 insert card_fill_detail_by_payment(compid, cardtype, cardid, billtype, billid, personid, share_rate, paycode, payamt, filltype, code, times, person_pos, procid, pctimes) select gcl00c, cardtype, cardid, 'gz', gcl01c, gcl24c, isnull(gcl25f, 0), paycode, payamt, @loop_cnt, code, times, 5, procid, pctimes from gcm10 a with (nolock), #gsm03_detail c where a.gcl00c = @compid and a.gcl01c = @billid and isnull(gcl24c, '')<>'' --第六销售人员 insert card_fill_detail_by_payment(compid, cardtype, cardid, billtype, billid, personid, share_rate, paycode, payamt, filltype, code, times, person_pos, procid, pctimes) select gcl00c, cardtype, cardid, 'gz', gcl01c, gcl26c, isnull(gcl27f, 0), paycode, payamt, @loop_cnt, code, times, 6, procid, pctimes from gcm10 a with (nolock), #gsm03_detail c where a.gcl00c = @compid and a.gcl01c = @billid and isnull(gcl26c, '')<>'' --第七销售人员 insert card_fill_detail_by_payment(compid, cardtype, cardid, billtype, billid, personid, share_rate, paycode, payamt, filltype, code, times, person_pos, procid, pctimes) select gcl00c, cardtype, cardid, 'gz', gcl01c, gcl28c, isnull(gcl29f, 0), paycode, payamt, @loop_cnt, code, times, 7, procid, pctimes from gcm10 a with (nolock), #gsm03_detail c where a.gcl00c = @compid and a.gcl01c = @billid and isnull(gcl28c, '')<>'' --第八销售人员 insert card_fill_detail_by_payment(compid, cardtype, cardid, billtype, billid, personid, share_rate, paycode, payamt, filltype, code, times, person_pos, procid, pctimes) select gcl00c, cardtype, cardid, 'gz', gcl01c, gcl30c, isnull(gcl31f, 0), paycode, payamt, @loop_cnt, code, times, 8, procid, pctimes from gcm10 a with (nolock), #gsm03_detail c where a.gcl00c = @compid and a.gcl01c = @billid and isnull(gcl30c, '')<>'' --第九销售人员 insert card_fill_detail_by_payment(compid, cardtype, cardid, billtype, billid, personid, share_rate, paycode, payamt, filltype, code, times, person_pos, procid, pctimes) select gcl00c, cardtype, cardid, 'gz', gcl01c, gcl32c, isnull(gcl33f, 0), paycode, payamt, @loop_cnt, code, times, 9, procid, pctimes from gcm10 a with (nolock), #gsm03_detail c where a.gcl00c = @compid and a.gcl01c = @billid and isnull(gcl32c, '')<>'' --第十销售人员 insert card_fill_detail_by_payment(compid, cardtype, cardid, billtype, billid, personid, share_rate, paycode, payamt, filltype, code, times, person_pos, procid, pctimes) select gcl00c, cardtype, cardid, 'gz', gcl01c, gcl34c, isnull(gcl35f, 0), paycode, payamt, @loop_cnt, code, times, 10, procid, pctimes from gcm10 a with (nolock), #gsm03_detail c where a.gcl00c = @compid and a.gcl01c = @billid and isnull(gcl34c, '')<>'' end select @loop_cnt = @loop_cnt + 1 end drop table #gsm03_detail end GO /****** Object: StoredProcedure [dbo].[upg_add_paycode] Script Date: 06/12/2017 06:19:24 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_add_paycode] ( @paycode varchar(5), -- 支付方式编码 @payname varchar(40) -- 支付方式名称 ) as begin if (isnull(@paycode, '') = '') return insert into gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) select gae01c, '3', @paycode, @payname, '收费方式', 2 from gam05 where gae01c not in (select gsb00c from gsm02 where gsb01c = '3' and gsb02c = @paycode) insert into gsm02_3_role(gsa01c_role, gsa02c_role, gsa03i_role, gsa04i_role, gsa05i_role, gsa06i_role, gsa07i_role, gsa08i_role) select T.gsa01c_role, @paycode, 1, 1, 1, 1, 1, 1 from (select distinct gsa01c_role from gsm02_3_role) T where T.gsa01c_role not in(select distinct gsa01c_role from gsm02_3_role where gsa02c_role = @paycode) insert into gsm02_3(gsa01c, gsa02c, gsa03i, gsa04i, gsa05i, gsa06i, gsa07i, gsa08i) select T.gsa01c, @paycode, 1, 1, 1, 1, 1, 1 from (select distinct gsa01c from gsm02_3) T where T.gsa01c not in(select distinct gsa01c from gsm02_3 where gsa02c = @paycode) end GO /****** Object: StoredProcedure [dbo].[upg_report_gwn04r_lm1] Script Date: 06/12/2017 06:20:37 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO Create proc [dbo].[upg_report_gwn04r_lm1] ( @fromdate varchar(10), --起始日期 @todate varchar(10), --结束日期 @companyid varchar(10), --门店 @gda02c varchar(10) --项目大类 ) as begin if( @companyid = '' and @gda02c = '' ) begin --门店占比 select compid + '[' + (select gae02c from gam05 with(nolock) where gae01c = compid) + ']' [name], isnull(sum(amt3),'') amt3 from work_detail with(nolock) where cls_flag = 1 and srvdate between @fromdate and @todate and amt3 > 0 group by compid end else if( @companyid != '' and @gda02c = '' ) begin --项目(部门) select gda02c + '['+ (select top 1 gsb03c from gsm02 with(nolock) where gsb01c='U' and gsb00c = compid and gsb02c = gda02c) +']' [name], isnull(sum(amt3),'') amt3 from work_detail with(nolock) inner join gdm01 with(nolock) on gda00c = compid and code = gda01c where cls_flag = 1 and compid = @companyid and srvdate between @fromdate and @todate and amt3 > 0 group by gda02c, compid end else if( @companyid != '' and @gda02c != '' ) begin --小类 select code + '[' + name + ']' [name], isnull(sum(amt3),'') amt3 from work_detail with(nolock) where cls_flag = 1 and compid = @companyid and code in ( select gda01c from gdm01 with(nolock) where gda00c = compid and gda02c = @gda02c ) and srvdate between @fromdate and @todate and amt3 > 0 group by code,name end end GO /****** Object: StoredProcedure [dbo].[upg_cal_profit_all] Script Date: 06/12/2017 06:19:34 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_cal_profit_all] ( @fromdate varchar(10), @todate varchar(10), @calmode int ) as begin declare @comp_cnt int select @comp_cnt = count(*) from gam05 if isnull(@comp_cnt, 1) = 1 begin exec upg_cal_profit '001', @fromdate, @todate, @calmode return end declare @maxAQ int select @maxAQ = max(gsb02c) from gsm02 where gsb00c = '001' and gsb01c = 'AQ' set @maxAQ = isnull(@maxAQ, 4) declare @compid varchar(10) declare cur_each_comp cursor for select gae01c from gam05 a, b_perf b where a.gae01c = b.bran_id and b.lvl = @maxAQ order by gae01c open cur_each_comp fetch cur_each_comp into @compid while @@fetch_status = 0 begin exec upg_cal_profit @compid, @fromdate, @todate, @calmode fetch cur_each_comp into @compid end close cur_each_comp deallocate cur_each_comp -- 处理非叶子节点的数据, START declare @each_parent varchar(20) begin create table #used_compid ( compid varchar(20) not null, isleaf int null ) insert #used_compid(compid, isleaf) select distinct gaz02c, 0 from gam26 where gaz01c = '001' update a set a.isleaf = 1 from #used_compid a, b_perf b where a.compid = b.bran_id and b.lvl = @maxAQ create table #notleaf_sons ( parantid varchar(20) not null, sonid varchar(20) not null ) insert #notleaf_sons(parantid, sonid) select gaz01c, gaz02c from gam26 z, #used_compid b, #used_compid c where z.gaz01c = b.compid and b.isleaf = 0 and gaz01c <> gaz02c and z.gaz02c = c.compid and c.isleaf = 1 create table #gcm21_buf ( -- 帐历史 gcu00c varchar(10) not null, --公司别 gcu01c varchar(8) not null, --日期 gcu02i int identity not null, --序号 gcu03c varchar(20) not null, --代码 gcu04c varchar(60) null, --名称 gcu05i int null, --加减标志 gcu06f float null, --金额 gcu07i int null, --是否为叶子 gcu08c varchar(10) null, --支付方式 gcu09i int null, --类别 0 充值 1卡销售 9产品销售 2 服务 -1 取款 8 保证金收入 6还款 ) declare cur_each_parent_compid cursor for select compid from #used_compid where isleaf = 0 order by compid open cur_each_parent_compid fetch cur_each_parent_compid into @each_parent while @@fetch_status = 0 begin delete gcm21 where gcu00c = @each_parent and gcu01c >= @fromdate and gcu01c <= @todate insert #gcm21_buf(gcu00c, gcu01c, gcu03c, gcu04c, gcu05i, gcu06f, gcu07i, gcu08c, gcu09i) select @each_parent, gcu01c, gcu03c, gcu04c, gcu05i, sum(gcu06f), gcu07i, gcu08c, gcu09i from gcm21 a, #notleaf_sons b where a.gcu00c = b.sonid and b.parantid = @each_parent and a.gcu01c >= @fromdate and a.gcu01c <= @todate group by gcu01c, gcu03c, gcu04c, gcu05i, gcu07i, gcu08c, gcu09i order by gcu01c, gcu03c fetch cur_each_parent_compid into @each_parent end close cur_each_parent_compid deallocate cur_each_parent_compid insert gcm21(gcu00c, gcu01c, gcu02i, gcu03c, gcu04c, gcu05i, gcu06f, gcu07i, gcu08c, gcu09i) select gcu00c, gcu01c, gcu02i, gcu03c, gcu04c, gcu05i, gcu06f, gcu07i, gcu08c, gcu09i from #gcm21_buf order by gcu00c, gcu01c, gcu02i drop table #gcm21_buf end --处理非叶子节点的数据, end end GO /****** Object: StoredProcedure [dbo].[upg_all_comp_pk] Script Date: 06/12/2017 06:19:24 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_all_comp_pk] ( @compid varchar(10), --登陆公司id @fromdate varchar(10), @todate varchar(10) ) as begin create table #tbl_profit_day ( idd int not null, code varchar(20) not null, itemname varchar(60) null, is_add int null, amt float null, is_leaf int null, payment varchar(10) null, -- 支付方式 kind int null -- 0-充值 1-卡销售 9-产品销售 2-服务 -1-取款 8-保证金收入 6-还款 ) create table #stat_byday ( comp varchar(10) not null, compname varchar(40) null, revene float null, primary key(comp) ) declare @maxAQ int select @maxAQ = max(gsb02c) from gsm02 where gsb00c = @compid and gsb01c = 'AQ' set @maxAQ = isnull(@maxAQ, 4) declare @amt float declare @comp varchar(10) declare cur_get_compid cursor for select gae01c from gam05 open cur_get_compid fetch cur_get_compid into @comp while @@fetch_status = 0 begin truncate table #tbl_profit_day insert #tbl_profit_day(idd, code, itemname, is_add, amt, is_leaf, payment, kind) exec upg_analyse_profit @comp, @fromdate, @todate, 1, '*', 0 if exists (select count(lvl) from b_perf where lvl = @maxAQ) begin delete from #stat_byday where comp in (select bran_id from b_perf where lvl <> @maxAQ) end insert #stat_byday(comp) values(@comp) -- 收入都同利润分析表中的收入 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '11' -- 收入 update #stat_byday set revene = @amt where comp = @comp fetch cur_get_compid into @comp end close cur_get_compid deallocate cur_get_compid update #stat_byday set compname = (select top 1 gae03c from gam05 with(nolock) where gae01c = comp ) declare @yeji float select @yeji = revene from #stat_byday with(nolock) where comp = @compid declare @mingci int select @mingci = count(revene) from #stat_byday with(nolock) where revene >= @yeji -- 创建临时表 create table #stat_bymingci ( mingci int identity, -- 流水号 comp varchar(10) null, compname varchar(40) null, revene float null, ) declare @PK_Sql nvarchar(max) set @PK_Sql = ' alter table #stat_bymingci add constraint PK_#stat_bymingci_' + convert(varchar(10), cast(floor(rand() * 1000000) as int)) + ' PRIMARY KEY NonCLUSTERED(mingci) ' exec sp_executesql @PK_Sql insert #stat_bymingci(comp, compname, revene) select comp, compname, revene from #stat_byday with(nolock) where revene > 0 order by revene desc select mingci, comp, compname, revene from #stat_bymingci with(nolock) -- where (mingci <= 10 or (mingci >= @mingci - 5 and mingci <= @mingci + 5)) drop table #stat_byday drop table #tbl_profit_day end GO /****** Object: StoredProcedure [dbo].[upg_all_stock_statistic] Script Date: 06/12/2017 06:19:25 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_all_stock_statistic] ( @compfrom varchar(10), -- 公司编号 @compto varchar(10), -- 公司编号 @date varchar(10), -- 日期 @goodsfrom varchar(20), -- 产品编号from @goodsto varchar(20), -- 产品编号to @goodstype varchar(10), -- 产品类型 add 产品类型 modified by maql 2010/03/16 @garagefrom varchar(20), -- 仓库编号from @garageto varchar(20), -- 仓库编号to @quan float, -- 安全库存量 @provider varchar(20), -- 供应商 @sorttype int, -- 排序类别 @printmode int -- 是否打印零库存产品 1-打印 0-不打印 ) as --WUXM 2006/02/08 begin create table #stock ( id int identity not null, comp varchar(10) null, -- 公司编号 goodsno varchar(20) null, -- 产品编号 goodsname varchar(80) null, -- 产品名称 garageno varchar(20) null, -- 仓库编号 garagename varchar(20) null, -- 仓库名称 goodstype varchar(20) null, -- 产品类型 goodstype_key varchar(20) null, -- 产品类型 price float null, -- 销售价格 inprice float null, -- 销售价格 unit varchar(20) null, -- 单位 quantity float null, -- 数量 amt float null, -- 金额 provider varchar(20) null, -- 供应商 cost float null, -- 成本单价 amt_cost float null, -- 成本 primary key (id) ) create table #output ( goodsno varchar(20) not null, -- 产品编号 quantity float null, -- 数量 amt float null, -- 单位 primary key(goodsno) ) declare @comp varchar(10) declare @id int -- 所有公司 if (isnull(@compfrom, '') = '*' or isnull(@compfrom, '') = '') begin declare cur_get_compid cursor for select gae01c from gam05 open cur_get_compid fetch cur_get_compid into @comp while @@fetch_status = 0 begin insert #stock(comp, goodsno, goodsname, garageno, garagename, goodstype, goodstype_key, price, inprice, unit, quantity, amt, provider, cost, amt_cost) exec upg_stock_statistic_for_single @comp, @date, @goodsfrom, @goodsto, @garagefrom, @garageto fetch cur_get_compid into @comp end close cur_get_compid deallocate cur_get_compid end -- 单个公司 else begin declare cur_get_compid_2 cursor for select gae01c from gam05 where gae01c >= @compfrom and gae01c <= @compto open cur_get_compid_2 fetch cur_get_compid_2 into @compfrom while @@fetch_status = 0 begin insert #stock(comp, goodsno, goodsname, garageno, garagename, goodstype, goodstype_key, price, inprice, unit, quantity, amt, provider, cost, amt_cost) exec upg_stock_statistic_for_single @compfrom, @date, @goodsfrom, @goodsto, @garagefrom, @garageto fetch cur_get_compid_2 into @compfrom end close cur_get_compid_2 deallocate cur_get_compid_2 end if (@printmode = 0) delete #stock where isnull(quantity, 0) = 0 if (isnull(@provider, '') = '' or isnull(@provider, '') = '*') --所有供应商的产品 begin if (@sorttype = 0) begin select isnull(comp,'') comp, isnull(goodsno,'') goodsno, isnull(goodsname,'') goodsname, isnull(garageno,'') garageno, isnull(garagename,'') garagename,isnull(goodstype,'') goodstype, isnull(price,0) price,isnull(unit,'') unit,n_quan = isnull(quantity,0), c_quan = convert(varchar(20), isnull(quantity,0)), isnull(amt,0) amt,isnull(cost,0),cost, isnull(amt_cost,0) amt_cost from #stock where(goodstype_key = @goodstype or isnull(@goodstype, '*') = '*') --添加产品类型条件 modified by maql 2010/03/16 order by goodsno, comp end else if (@sorttype = 1) begin select isnull(comp,'') comp, isnull(goodsno,'') goodsno, isnull(goodsname,'') goodsname, isnull(garageno,'') garageno, isnull(garagename,'') garagename,isnull(goodstype,'') goodstype, isnull(price,0) price,isnull(unit,'') unit,n_quan = isnull(quantity,0), c_quan = convert(varchar(20), isnull(quantity,0)), isnull(amt,0) amt,isnull(cost,0),cost, isnull(amt_cost,0) amt_cost from #stock where(goodstype_key = @goodstype or isnull(@goodstype, '*') = '*') --添加产品类型条件 modified by maql 2010/03/16 order by goodstype end else if (@sorttype = 2) begin select isnull(comp,'') comp, isnull(goodsno,'') goodsno, isnull(goodsname,'') goodsname, isnull(garageno,'') garageno, isnull(garagename,'') garagename,isnull(goodstype,'') goodstype, isnull(price,0) price,isnull(unit,'') unit,n_quan = isnull(quantity,0), c_quan = convert(varchar(20), isnull(quantity,0)), isnull(amt,0) amt,isnull(cost,0),cost, isnull(amt_cost,0) amt_cost from #stock where(goodstype_key = @goodstype or isnull(@goodstype, '*') = '*') --添加产品类型条件 modified by maql 2010/03/16 order by provider end end else --指定供应商的产品 begin if (@sorttype = 0) begin select isnull(comp,'') comp, isnull(goodsno,'') goodsno, isnull(goodsname,'') goodsname, isnull(garageno,'') garageno, isnull(garagename,'') garagename,isnull(goodstype,'') goodstype, isnull(price,0) price,isnull(unit,'') unit,n_quan = isnull(quantity,0), c_quan = convert(varchar(20), isnull(quantity,0)), isnull(amt,0) amt,isnull(cost,0),cost, isnull(amt_cost,0) amt_cost from #stock where(goodstype_key = @goodstype or isnull(@goodstype, '*') = '*') --添加产品类型条件 modified by maql 2010/03/16 and provider = @provider order by goodsno, comp end else if (@sorttype = 1) begin select isnull(comp,'') comp, isnull(goodsno,'') goodsno, isnull(goodsname,'') goodsname,isnull(garageno,'') garageno, isnull(garagename,'') garagename,isnull(goodstype,'') goodstype, isnull(price,0) price,isnull(unit,'') unit,n_quan = isnull(quantity,0), c_quan = convert(varchar(20), isnull(quantity,0)), isnull(amt,0) amt,isnull(cost,0),cost, isnull(amt_cost,0) amt_cost from #stock where(goodstype_key = @goodstype or isnull(@goodstype, '*') = '*') --添加产品类型条件 modified by maql 2010/03/16 and provider = @provider order by goodstype end else if (@sorttype = 2) begin select isnull(comp,'') comp, isnull(goodsno,'') goodsno, isnull(goodsname,'') goodsname, isnull(garageno,'') garageno, isnull(garagename,'') garagename,isnull(goodstype,'') goodstype, isnull(price,0) price,isnull(unit,'') unit,n_quan = isnull(quantity,0), c_quan = convert(varchar(20), isnull(quantity,0)), isnull(amt,0) amt,isnull(cost,0),cost, isnull(amt_cost,0) amt_cost from #stock where(goodstype_key = @goodstype or isnull(@goodstype, '*') = '*') --添加产品类型条件 modified by maql 2010/03/16 and provider = @provider order by provider end end drop table #stock drop table #output end GO /****** Object: StoredProcedure [dbo].[upg_all_goods_sale_report] Script Date: 06/12/2017 06:19:25 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_all_goods_sale_report] ( @fromcompid varchar(10), -- from公司别 @tocompid varchar(10), -- to公司别 @flag int, -- 1-销售 2-消耗, 3-全部 @condition1 varchar(1024), @condition2 varchar(1024), @catelogy varchar(10), -- 产品类别 @kind int -- 1-不同价格的产品单独显示, 2-同个产品分组合计 ) as begin declare @sql nvarchar(1024) if @fromcompid = '*' begin select @fromcompid = min(gae01c) from gam05 select @tocompid = max(gae01c) from gam05 end create table #good_result ( idd int identity not null, compid varchar(10) not null, catelogy varchar(10) null, item varchar(20) not null, itemname varchar(100) null, costprice float null, --进价 unitprice float null, --标准价格 saleprice float null, --销售价格 quan float null, --数量 saler varchar(10) null, --销售人员 saledate varchar(8) null, --销售日期 saletime varchar(6) null, --销售时间 到分钟 fact_quan float null, unit varchar(10) null, costamt float null, --成本总金额 amt float null, --金额 type int null, -- 1: 销售 2:消耗 paymode varchar(10) null, -- 支付方式 primary key(idd) ) if @flag = 1 or @flag = 3 begin select @sql = 'insert #good_result(compid, item, costprice, saleprice, quan, saler, saledate, saletime, costamt, amt, type, paymode) select ggc00c, ggc03c, isnull(gfa22f, 0), ggc10f, ggc05f, ggc12c, gga02d, gla03t, isnull(gfa22f, 0)*isnull(ggc05f, 0), ggc11f, 1, ggc27c from ggm03 b with (nolock), ggm01 a with (nolock), glm01 c with (nolock), gfm01 d where a.gga00c = b.ggc00c and a.gga01c = b.ggc01c and a.gga00c = c.gla00c and c.gla01c = a.gga30c and a.gga00c = d.gfa00c and b.ggc03c = d.gfa01c and isnull(ggc03c, '''')<>'''' and a.gga00c >= ''' + @fromcompid + ''' and a.gga00c <= ''' + @tocompid + '''' if @condition1<>'' select @sql = @sql + ' and ' + @condition1; exec sp_executesql @sql end if @flag = 2 or @flag = 3 begin select @sql = 'insert #good_result(compid, item, costprice, saleprice, quan, saler, saledate, saletime, costamt, amt, type) select gge00c, gge03c, isnull(gfa22f, 0), gge10f, gge05f, gge12c, gga02d, gla03t, isnull(gfa22f, 0)*(gge05f/isnull(gfa09f, 0)), gge11f, 2 from ggm05 b with (nolock), ggm01 a with (nolock), glm01 c with (nolock), gfm01 d where a.gga00c = b.gge00c and a.gga01c = b.gge01c and a.gga00c = c.gla00c and c.gla01c = a.gga30c and a.gga00c = d.gfa00c and b.gge03c = d.gfa01c and isnull(gge03c, '''')<>'''' and a.gga00c >= ''' + @fromcompid + ''' and a.gga00c <= ''' + @tocompid + '''' if @condition2<>'' select @sql = @sql + ' and ' + @condition2; exec sp_executesql @sql end update a set a.fact_quan = a.quan from #good_result a where a.type = 1 update a set a.fact_quan = dbo.fun_divide(a.quan, isnull(b.gfa09f, 1)), a.amt = 0 from #good_result a, gfm01 b where a.type = 2 and b.gfa00c = a.compid and b.gfa01c = a.item update a set a.itemname = b.gfa03c, a.unit = b.gfa07c, a.catelogy = b.gfa05c from #good_result a, gfm01 b where a.item = b.gfa01c and b.gfa00c = a.compid update a set a.unit = b.gsb03c from #good_result a, gsm02 b where a.unit = b.gsb02c and b.gsb00c = a.compid and b.gsb01c = 'K' if @kind = 1 begin if @catelogy = '' or @catelogy = '*' begin select isnull(compid,'') compid, isnull(item,'') item, isnull(itemname,'') itemname, costprice = isnull(costprice, 0), isnull(saleprice,'') saleprice, isnull(fact_quan,'') fact_quan, saler = isnull(saler, ''), saledate = isnull(convert(varchar(10), cast(saledate as datetime), 120),''), saletime = isnull(substring(saletime, 1, 2) + ':' + substring(saletime, 3, 2),''), isnull(unit,'') unit, isnull(costamt,'') costamt, isnull(amt,'') amt, isnull(paymode,'') paymode from #good_result end else begin select isnull(compid,'') compid, isnull(item,'') item, isnull(itemname,'') itemname, costprice = isnull(costprice, 0), isnull(saleprice,'') saleprice, isnull(fact_quan,'') fact_quan, saler = isnull(saler, ''), saledate = isnull(convert(varchar(10), cast(saledate as datetime), 120),''), saletime = isnull(substring(saletime, 1, 2) + ':' + substring(saletime, 3, 2),''), isnull(unit,'') unit, isnull(costamt,'') costamt, isnull(amt,'') amt, isnull(paymode,'') paymode from #good_result where catelogy = @catelogy end end else if @kind = 2 begin if @catelogy = '' or @catelogy = '*' begin select isnull(compid,'') compid, isnull(item,'') item, isnull(itemname,'') itemname, costprice = isnull(dbo.fun_divide(sum(costamt), sum(fact_quan)),0), saleprice = isnull(dbo.fun_divide(sum(amt), sum(fact_quan)),0), fact_quan = isnull(sum(fact_quan),0), isnull(unit,'') unit, costamt = isnull(sum(costamt),0), amt = isnull(sum(amt),0) from #good_result group by item, itemname, compid, unit order by item, compid end else begin select isnull(compid,'') compid, isnull(item,'') item, isnull(itemname,'') itemname, costprice = isnull(dbo.fun_divide(sum(costamt), sum(fact_quan)),0), saleprice = isnull(dbo.fun_divide(sum(amt), sum(fact_quan)),0), fact_quan = isnull(sum(fact_quan),0), isnull(unit,'') unit, costamt = isnull(sum(costamt),0), amt = isnull(sum(amt),0) from #good_result where catelogy = @catelogy group by item, itemname, compid, unit order by item, compid end end drop table #good_result end GO /****** Object: StoredProcedure [dbo].[upg_cs_update_to_level_2] Script Date: 06/12/2017 06:19:54 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_cs_update_to_level_2] ( @comp varchar(10) ) as begin begin transaction begin try if not exists(select 1 from gam05 where gae01c = @comp) return update b_perf set lvl = 2, parent_id = '001' where bran_id = @comp delete gam26 where gaz02c = @comp insert gam26(gaz01c, gaz02c) values('001', @comp) end try begin catch if @@TRANCOUNT > 0 begin rollback transaction declare @SP_Parameters varchar(max) set @SP_Parameters = '@comp = ' + isnull(@comp, '') insert into SP_TranLog (SPName ,SPTime ,SPParameter) values ( 'upg_cs_update_to_level_2' ,getdate() ,@SP_Parameters) raiserror ('transaction is failed.', 16, 1) WITH NOWAIT end end catch if @@TRANCOUNT > 0 begin commit transaction end end GO /****** Object: StoredProcedure [dbo].[upg_add_compidtosatisfaction] Script Date: 06/12/2017 06:19:23 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_add_compidtosatisfaction] as begin declare @gae01c varchar(10) declare cursor_satisfaction cursor for select gae01c from gam05 open cursor_satisfaction fetch next from cursor_satisfaction into @gae01c while(@@fetch_status = 0) begin fetch next from cursor_satisfaction into @gae01c end if(not exists(select 1 from satisfaction where sf00c = @gae01c)) begin insert into satisfaction(sf00c,sf01c,sf02c) select gae01c,sf01c,sf02c from ( select @gae01c as gae01c,'1' as sf01c,'4' as sf02c union all select @gae01c as gae01c,'2' as sf01c,'4' as sf02c union all select @gae01c as gae01c,'3' as sf01c,'3' as sf02c union all select @gae01c as gae01c,'4' as sf01c,'2' as sf02c union all select @gae01c as gae01c,'5' as sf01c,'1' as sf02c ) as AA end close cursor_satisfaction deallocate cursor_satisfaction end GO /****** Object: StoredProcedure [dbo].[upg_dispatch_user] Script Date: 06/12/2017 06:20:01 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_dispatch_user] ( @fromcompid varchar(10), @fromuser varchar(20), @tocompid varchar(20), @del_self int ) as begin declare @tocompid_code varchar(10) declare @tocompid_true varchar(10) select @fromcompid = gaa04c from gam01 where gaa01c = @fromuser if @tocompid = '*' begin select @tocompid_code = '%' end else select @tocompid_code = @tocompid declare cur_each_comp cursor for select gae01c from gam05 where gae01c <> @fromcompid and gae01c like @tocompid_code open cur_each_comp fetch cur_each_comp into @tocompid_true while @@fetch_status = 0 begin delete gam02 where gab01c = @fromuser and gab02c = '2' and gab03c = @tocompid_true insert gam02(gab01c, gab02c, gab03c, gab04c, gab06c) values(@fromuser, '2', @tocompid_true, '', '') fetch cur_each_comp into @tocompid_true end close cur_each_comp deallocate cur_each_comp if @del_self = 1 begin delete gam02 where gab01c = @fromuser and gab02c = '2' and gab03c = @fromcompid end if @del_self = 1 begin update gam01 set gaa04c = @tocompid_true where gaa01c = @fromuser end end GO /****** Object: StoredProcedure [dbo].[upg_stores_currentsellsumcount] Script Date: 06/12/2017 06:20:45 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_stores_currentsellsumcount] ( @compid varchar(10), -- 已经能支持各级别的公司 @fromdate varchar(10), -- 开始日期 @todate varchar(10) -- 结束日期 ) as begin create table #currentsell --当前购买量统计表 ( compid varchar(10) not null, -- 公司别 compname varchar(40) null, -- 公司名称 goodssumcount float null, -- 物品购买数量 goodssumamt float null, -- 物品购买金额 beautysumcount float null, -- 美容疗程购买数量 beautysumamt float null, -- 美容疗程购买金额 bodysumcount float null, -- 美体疗程购买数量 bodysumamt float null, -- 美体疗程购买金额 instrumentsumcount float null, -- 仪器购买数量 instrumentsumamt float null, -- 仪器购买金额 writedownsamt float null -- 划简金额 ) create table #gmm01x -- 日结表 -- 项目/产品 ( gma00c varchar(10) not null, -- 公司编号 gma01d varchar(8) not null, -- 日期 gma02i int not null, -- 类别 1 -项目 2产品 3疗程 4-会员卡买卡 5-会员卡升级 gma03c varchar(20) null, -- 项目代码/产品代码 gma04c varchar(5) null, -- 支付方式 gma05f float null, -- 数量 gma06f float null, -- 金额 gma07f float null --数量(疗程的时候是疗程次数, 产品数量, 充值卡数量) ) create clustered index idx_gmm01x on #gmm01x(gma02i, gma03c) declare @payment_zp varchar(10) --支票的支付方式 declare @is_revenue varchar(10) --判断是否为收入项目 select @is_revenue = gsa02c from gsm01 with(nolock) where gsa00c = @compid and gsa01c = 'SP063' if @is_revenue = '1' begin select @payment_zp = '$' end else begin select @payment_zp = 'ZZ' -- 无此支付方式 end -------------------------获得当期的数据-------------------------------- insert #gmm01x(gma00c, gma01d, gma02i, gma03c, gma04c, gma05f, gma06f, gma07f) select gma00c, gma01d, gma02i, gma03c, gma04c, gma05f, gma06f, gma07f from gmm01 a with (nolock), gam26 z with(nolock) where a.gma00c = z.gaz02c and z.gaz01c = @compid and a.gma01d between @fromdate and @todate and a.gma02i in (1, 2, 3, 4, 5, 6) and gma04c in ('1', '6', 'ZFB', '&', 'WX', @payment_zp) and isnull(gma03c, '')<>'' order by gma00c --把项目代码转为项目类别代码 update a set a.gma03c = isnull(b.gda13c, b.gda04c) from #gmm01x a, gdm01 b with(nolock) where a.gma02i in (1, 3, 6) and a.gma03c = b.gda01c and a.gma00c = b.gda00c declare @count float -- 计数器 set @count = 0 declare @compidofmid varchar(10) -- 判断不同的公司别 set @compidofmid = '' declare @chain_compid varchar(10) -- 连锁结构公司别 declare @typeofproj int -- 类别编号 1 -项目 2产品 3疗程 4-会员卡买卡 5-会员卡升级 declare @noofyreatment varchar(20) -- 疗程代码 233 - 美容 231-232 - 仪器 其他 - 美体 declare @sumaccount float -- 总数量 declare @sumamt float -- 总金额 declare @compname varchar(40) -- 门店名称 declare @sumaccount_good float -- 物品总数量 declare @sumamt_good float -- 物品总金额 declare @sumaccount_beauty float -- 美容总数量 declare @sumamt_beauty float -- 美容总金额 declare @sumaccount_body float -- 美体总数量 declare @sumamt_body float -- 美体总金额 declare @sumaccount_instrument float -- 仪器总数量 declare @sumamt_instrument float -- 仪器总金额 declare @sumat_writedown float -- 划简金额 set @sumaccount_good = 0 set @sumamt_good = 0 set @sumaccount_beauty = 0 set @sumamt_beauty = 0 set @sumaccount_body = 0 set @sumamt_body = 0 set @sumaccount_instrument = 0 set @sumamt_instrument = 0 set @sumat_writedown = 0 declare cur_gmm01x cursor for select gma00c, gma02i, gma03c, sum(gma07f), sum(gma06f) from #gmm01x group by gma00c, gma02i, gma03c order by gma00c, gma02i, gma03c open cur_gmm01x fetch cur_gmm01x into @chain_compid, @typeofproj, @noofyreatment, @sumaccount, @sumamt while @@fetch_status = 0 begin if (@compidofmid <> @chain_compid and @count != 0) begin insert #currentsell(compid, compname, goodssumcount, goodssumamt, beautysumcount, beautysumamt, bodysumcount, bodysumamt, instrumentsumcount, instrumentsumamt, writedownsamt) values(@compidofmid, '', @sumaccount_good, @sumamt_good, @sumaccount_beauty, @sumamt_beauty, @sumaccount_body, @sumamt_body, @sumaccount_instrument, @sumamt_instrument, @sumat_writedown) set @sumaccount_good = 0 set @sumamt_good = 0 set @sumaccount_beauty = 0 set @sumamt_beauty = 0 set @sumaccount_body = 0 set @sumamt_body = 0 set @sumaccount_instrument = 0 set @sumamt_instrument = 0 set @sumat_writedown = 0 end set @compidofmid = @chain_compid if (@typeofproj = 2) --产品 begin set @sumaccount_good = isnull(@sumaccount_good, 0) + isnull(@sumaccount, 0) set @sumamt_good = isnull(@sumamt_good, 0) + isnull(@sumamt, 0) end if (@typeofproj = 4 or @typeofproj = 5) --划减金额 begin set @sumat_writedown = isnull(@sumat_writedown, 0) + isnull(@sumamt, 0) end if (@typeofproj = 1 or @typeofproj = 3 or @typeofproj = 6) --疗程 begin if (@noofyreatment = '233') --美容 begin set @sumaccount_beauty = isnull(@sumaccount_beauty, 0) + isnull(@sumaccount, 0) set @sumamt_beauty = isnull(@sumamt_beauty, 0) + isnull(@sumamt, 0) end else if (@noofyreatment = '231' or @noofyreatment = '232' or @noofyreatment = '234') --仪器 begin set @sumaccount_instrument = isnull(@sumaccount_instrument, 0) + isnull(@sumaccount, 0) set @sumamt_instrument = isnull(@sumamt_instrument, 0) + isnull(@sumamt, 0) end else --美体 begin set @sumaccount_body = isnull(@sumaccount_body, 0) + isnull(@sumaccount, 0) set @sumamt_body = isnull(@sumamt_body, 0) + isnull(@sumamt, 0) end end set @count = @count + 1 fetch cur_gmm01x into @chain_compid, @typeofproj, @noofyreatment, @sumaccount, @sumamt end close cur_gmm01x deallocate cur_gmm01x if (@compidofmid <> '') begin select top 1 @compname = gae03c from gam05 with(nolock) where gae01c = @compidofmid insert #currentsell(compid, compname, goodssumcount, goodssumamt, beautysumcount, beautysumamt, bodysumcount, bodysumamt, instrumentsumcount, instrumentsumamt, writedownsamt) values(@compidofmid, @compname, @sumaccount_good, @sumamt_good, @sumaccount_beauty, @sumamt_beauty, @sumaccount_body, @sumamt_body, @sumaccount_instrument, @sumamt_instrument, @sumat_writedown) end drop table #gmm01x --因为需要考虑赠送的储值, 所以只能从gcm04中取 create table #tbl_acct_save( compid varchar(20) not null, saveamt float null) insert #tbl_acct_save(compid, saveamt) select gcd00c, sum(gcd06f) from gcm04 a with (nolock), gam26 z with(nolock) where (gcd02i = 2 or (gcd02i >= 5 and gcd02i <= 8)) and (gcd05i = 0 or gcd05i = 1) and a.gcd00c = z.gaz02c and z.gaz01c = @compid and a.gcd09d >= @fromdate and a.gcd09d <= @todate group by gcd00c update a set a.writedownsamt = b.saveamt from #currentsell a, #tbl_acct_save b where a.compid = b.compid drop table #tbl_acct_save declare @maxAQ int select @maxAQ = max(gsb02c) from gsm02 with(nolock) where gsb00c = @compid and gsb01c = 'AQ' set @maxAQ = isnull(@maxAQ, 4) --处理非叶子节点的数据, START declare @each_parent varchar(20) begin create table #used_compid( compid varchar(20) not null, isleaf int null) insert #used_compid(compid, isleaf) select distinct gaz02c, 0 from gam26 with(nolock) where gaz01c = @compid update a set a.isleaf = 1 from #used_compid a, b_perf b where a.compid = b.bran_id and b.lvl = @maxAQ create table #notleaf_sons( parantid varchar(20) not null, sonid varchar(20) not null) insert #notleaf_sons(parantid, sonid) select gaz01c, gaz02c from gam26 z with(nolock), #used_compid b, #used_compid c where z.gaz01c = b.compid and b.isleaf = 0 and gaz01c <> gaz02c and z.gaz02c = c.compid and c.isleaf = 1 declare cur_each_parent_compid cursor for select compid from #used_compid where isleaf = 0 open cur_each_parent_compid fetch cur_each_parent_compid into @each_parent while @@fetch_status = 0 begin delete #currentsell where compid = @each_parent insert #currentsell(compid, compname, goodssumcount, goodssumamt, beautysumcount, beautysumamt, bodysumcount, bodysumamt, instrumentsumcount, instrumentsumamt, writedownsamt) select @each_parent, '', sum(goodssumcount), sum(goodssumamt), sum(beautysumcount), sum(beautysumamt), sum(bodysumcount), sum(bodysumamt), sum(instrumentsumcount), sum(instrumentsumamt), sum(writedownsamt) from #currentsell a, #notleaf_sons b where a.compid = b.sonid and b.parantid = @each_parent fetch cur_each_parent_compid into @each_parent end close cur_each_parent_compid deallocate cur_each_parent_compid end --处理非叶子节点的数据, end update a set a.compname = gae03c from #currentsell a, gam05 b with(nolock) where a.compid = b.gae01c select isnull(compid,'') compid, isnull(compname,'') compname,isnull(goodssumcount,0) goodssumcount, isnull(goodssumamt,0) goodssumamt, isnull(beautysumcount,0) beautysumcount,isnull(beautysumamt,0) beautysumamt,isnull(bodysumcount,0) bodysumcount, isnull(bodysumamt,0) bodysumamt,isnull(instrumentsumcount,0) instrumentsumcount,isnull(instrumentsumamt,0) instrumentsumamt, isnull(writedownsamt,0) compname from #currentsell order by compid drop table #currentsell end GO /****** Object: StoredProcedure [dbo].[upg_analyse_profit] Script Date: 06/12/2017 06:19:25 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_analyse_profit] ( @compid varchar(10), -- 已经支持各级别的公司 @fromdate varchar(10), @todate varchar(10), @flag int, -- flag 0-计算员工提成 1-不计算员工提成 @staff varchar(20), -- 收银人员 @calmode int -- 计算利润的模式 0-卡销售收入作为模式 1-劳动业绩作为收入 ) as begin declare @yesterday datetime select @yesterday = getdate() - 1 declare @curdate varchar(20) select @curdate = substring(convert(varchar(20), @yesterday, 102), 1, 4) + substring(convert(varchar(20), @yesterday, 102), 6, 2) + substring(convert(varchar(20), @yesterday, 102), 9, 2) --3记入收入;控制多少个店才隔天产生帐 declare @sp062f float select @sp062f = gsa03f from gsm01 with(nolock) where gsa00c = @compid and gsa01c = 'SP062' declare @compcount int select @compcount = count(*) from gam05 with(nolock) -- gcm21中的统计数据已经包括了下面的所有子公司了 if (@compcount > @sp062f and @flag = 1 and (@staff = '' or @staff = '*' or @staff = '%') and (@todate <= @curdate)) begin create table #gcm21 ( idd int identity not null, code varchar(20) not null, itemname varchar(60) not null, is_add int not null, amt float default 0 null, is_leaf int not null, payment varchar(10) null, -- 支付方式 kind int null, -- 0充值 1卡销售 9产品销售 2服务 -1取款 8保证金收入 6还款 ) declare @add_card_sale int declare @add_card_svr int -- 卡付是否作为收入 if @calmode = 2 begin declare @sp115f float select @sp115f = gsa03f from gsm01 with (nolock) where gsa00c = @compid and gsa01c = 'SP115' if @sp115f = 0 select @calmode = 0 else select @calmode = 1 end if @calmode = 0 begin select @add_card_sale = 1 select @add_card_svr = 0 end else begin select @add_card_sale = 0 select @add_card_svr = 1 end if ((@compid = '001' or @compid = '' or @compid = '%' or @compid = '*')) begin insert into #gcm21(code, itemname, is_add, amt, is_leaf, payment, kind) select code = gcu03c, name = gcu04c, is_add = max(gcu05i), amt = sum(isnull(gcu06f,0)), is_leaf = gcu07i, payment = gcu08c, kind = gcu09i from gcm21 a with(nolock) where a.gcu01c >= @fromdate and a.gcu01c <= @todate and a.gcu00c = '001' group by gcu03c, gcu04c, gcu07i, gcu08c, gcu09i end else begin declare @maxAQ int select @maxAQ = max(gsb02c) from gsm02 with(nolock) where gsb00c = '001' and gsb01c = 'AQ' set @maxAQ = isnull(@maxAQ, 4) insert into #gcm21(code, itemname, is_add, amt, is_leaf, payment, kind) select code = gcu03c, name = gcu04c, is_add = max(gcu05i), amt = sum(isnull(gcu06f, 0)), is_leaf = gcu07i, payment = gcu08c, kind = gcu09i from gcm21 a with(nolock), gam26 z with(nolock), b_perf c with(nolock) where a.gcu01c >= @fromdate and a.gcu01c <= @todate and a.gcu00c = z.gaz02c and z.gaz01c = @compid and c.bran_id = z.gaz02c and c.lvl = @maxAQ group by gcu03c, gcu04c, gcu07i, gcu08c, gcu09i end update #gcm21 set is_add = @add_card_sale where code like '1101%' update #gcm21 set is_add = 0 where code in('110103', '110106', '110109', '110110', '110111', '110112', '110115', '110116', '110117', '110133') declare @isadd_0 float select @isadd_0 = gso07f from gsm15 with (nolock) where gso00c = @compid and gso02c = '0' select @isadd_0 = isnull(@isadd_0, 0) update #gcm21 set is_add = @isadd_0 * @add_card_sale where code in('110120', '110121', '110122', '110123') declare @isadd_3 float select @isadd_3 = gso07f from gsm15 with (nolock) where gso00c = @compid and gso02c = '3' select @isadd_3 = isnull(@isadd_3, 0) update #gcm21 set is_add = @isadd_3 * @add_card_sale where code in('110125', '110126', '110127') declare @isadd_$ float select @isadd_$ = gso07f from gsm15 with (nolock) where gso00c = @compid and gso02c = '$' select @isadd_$ = isnull(@isadd_$, 0) update #gcm21 set is_add = @isadd_$ * @add_card_sale where code in('110130', '110131', '110132') declare @pay_$ varchar(10) declare @isadd_ur float -- 子定义的支付方式是为计入营业收入 declare @code_int int select @code_int = 110150 declare cur_each_pay cursor for select gsb02c from gsm02 with(nolock) where gsb00c = @compid and gsb01c = '3' and gsb02c like '$%' and len(gsb02c) > 1 order by gsb02c open cur_each_pay fetch cur_each_pay into @pay_$ while @@fetch_status = 0 begin select @isadd_ur = gso07f from gsm15 with(nolock) where gso00c = @compid and gso02c = @pay_$ select @isadd_ur = isnull(@isadd_ur, 1) -- 默认算收入 update #gcm21 set is_add = @isadd_ur * @add_card_sale where code = convert(varchar(10), @code_int) or code = convert(varchar(10), @code_int + 1) or code = convert(varchar(10), @code_int + 2) or code = convert(varchar(10), @code_int + 3) -- modify by lm 2016.05.12 select @code_int = @code_int + 4 -- modify by lm 2016.05.12 fetch cur_each_pay into @pay_$ end close cur_each_pay deallocate cur_each_pay -- 统计卡销售收入 declare @amt float select @amt = 0 if @calmode = 0 begin select @amt = sum(isnull(amt * is_add, 0)) from #gcm21 where code like '1101%' and is_leaf = 1 end else begin select @amt = sum(isnull(amt, 0)) from #gcm21 where code like '1101%' and is_leaf = 1 and (isnull(payment, '') <> '&' and isnull(payment, '') <> '7' and isnull(payment, '') <> '') end update #gcm21 set amt = isnull(@amt, 0) where code = '1101' --这代码为之前的,后期测试是正确的 lucz 2015.10.17 update #gcm21 set is_add = @add_card_svr where code = '110302' or code in (select '110205' + right('00' + gsn01c, 2) from gsm14 c with(nolock) where isnull(c.gsn02c, '') <> '' and isnull(c.gsn02c, '') <> '999' and isnull(c.gsn02c, '') <> '3' ) /* --这是后面更新的,结果是 110302(产品销售收入-卡付)为负数,导致总收入不正确 lucz 2015.10.17 update #gcm21 set is_add = @add_card_svr - 1 where code = '110302' update #gcm21 set is_add = @add_card_svr where code in (select '110205' + right('00' + gsn01c, 2) from gsm14 c with(nolock) where isnull(c.gsn02c, '') <> '' and isnull(c.gsn02c, '') <> '999' and isnull(c.gsn02c, '') <> '3' ) */ select @amt = 0 select @amt = sum(isnull(amt * is_add, 0)) from #gcm21 where code like '11%' and is_leaf = 1 update #gcm21 set amt = isnull(@amt, 0) where code = '11' select idd, code, itemname, is_add, amt, is_leaf, payment, kind from #gcm21 drop table #gcm21 end else begin exec upg_analyse_profit_inter @compid, @fromdate, @todate, @flag, @staff, @calmode end end GO /****** Object: StoredProcedure [dbo].[upg_all_comp_pk_new] Script Date: 06/12/2017 06:19:25 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_all_comp_pk_new] ( @compid varchar(10), --登陆公司id @userid varchar(10), --登陆者id @fromdate varchar(10), @todate varchar(10) ) as begin create table #tbl_profit_day( idd int not null, code varchar(20) not null, itemname varchar(60) null, is_add int null, amt float null, is_leaf int null, payment varchar(10) null, -- 支付方式 kind int null -- 0 充值 1卡销售 9产品销售 2 服务 -1 取款 8 保证金收入 6还款 ) create table #stat_byday( comp varchar(10) not null, compname varchar(40) null, revene float null, primary key(comp) ) declare @amt float declare @comp varchar(10) declare cur_get_compid cursor for select gae01c from gam05 declare @maxAQ int select @maxAQ = max(gsb02c) from gsm02 where gsb00c = @compid and gsb01c = 'AQ' set @maxAQ = isnull(@maxAQ, 4) open cur_get_compid fetch cur_get_compid into @comp while @@fetch_status = 0 begin truncate table #tbl_profit_day insert #tbl_profit_day(idd, code, itemname, is_add, amt, is_leaf, payment, kind) exec upg_analyse_profit @comp, @fromdate, @todate, 1, '*', 0 -- 是否子门店 if exists (select count(lvl) from b_perf where lvl = @maxAQ) begin delete from #stat_byday where comp in (select bran_id from b_perf where lvl<>@maxAQ) end -- 删除不是自己的管理门店 delete from #stat_byday where comp not in (select gab03c from gam02 where gab02c = '2' and gab01c = @userid) insert #stat_byday(comp) values(@comp) --收入都同利润分析表中的收入 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '11' -- 收入 update #stat_byday set revene = @amt where comp = @comp fetch cur_get_compid into @comp end close cur_get_compid deallocate cur_get_compid update #stat_byday set compname = (select top 1 gae03c from gam05 with(nolock) where gae01c = comp ) declare @yeji float declare @mingci int select @yeji = revene from #stat_byday with(nolock) where comp = @compid select @mingci = count(revene) from #stat_byday with(nolock) where revene >= @yeji --创建临时表 create table #stat_bymingci( mingci int identity, -- 流水号 comp varchar(10) null, compname varchar(40) null, revene float null, ) declare @PK_Sql nvarchar(max) set @PK_Sql = ' alter table #stat_bymingci add constraint PK_#stat_bymingci_' + convert(varchar(10), cast(floor(rand() * 1000000) as int)) + ' PRIMARY KEY NonCLUSTERED(mingci)' exec sp_executesql @PK_Sql insert #stat_bymingci(comp, compname, revene) select comp, compname, revene from #stat_byday with(nolock) where revene>0 order by revene desc select mingci, comp, compname, revene from #stat_bymingci with(nolock) --where (mingci <= 10 or ( mingci >= @mingci-5 and mingci <= @mingci + 5)) drop table #stat_byday drop table #tbl_profit_day end GO /****** Object: StoredProcedure [dbo].[upg_ones_stock_statistic] Script Date: 06/12/2017 06:20:25 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_ones_stock_statistic] ( @compid varchar(10), -- 公司编号 @date varchar(10), -- 日期 @goodsfrom varchar(20), -- 产品编号from @goodsto varchar(20), -- 产品编号to @garagefrom varchar(20), -- 仓库编号from @garageto varchar(20), -- 仓库编号to @quan float, -- 安全库存量 @provider varchar(20), -- 供应商 @sorttype int, -- 排序类别 @printmode int -- 是否打印零库存产品 1-打印 0-不打印 ) as begin create table #stock ( id int identity not null, comp varchar(10) null, -- 公司编号 goodsno varchar(20) null, -- 产品编号 goodsname varchar(80) null, -- 产品名称 garageno varchar(20) null, -- 仓库编号 garagename varchar(20) null, -- 仓库名称 goodstype varchar(20) null, -- 产品类型 goodstype_key varchar(20) null, -- 产品类型 price float null, -- 销售价格 inprice float null, -- 销售价格 unit varchar(20) null, -- 单位 quantity float null, -- 数量 amt float null, -- 金额 provider varchar(20) null, -- 供应商 cost float null, -- 成本单价 amt_cost float null, -- 成本 primary key (id) ) create table #gfm01 ( comp varchar(10) null, -- 公司编号 goodsno varchar(20) null, -- 产品编号 goodsname varchar(80) null, -- 产品名称 garageno varchar(20) null, -- 仓库编号 garagename varchar(20) null, -- 仓库名称 goodstype varchar(20) null, -- 产品类型 goodstype_key varchar(20) null, -- 产品类型 price float null, -- 销售价格 inprice float null, -- 销售价格 unit varchar(20) null, -- 单位 quantity float null, -- 数量 amt float null, -- 金额 provider varchar(20) null, -- 供应商 cost float null, -- 成本单价 amt_cost float null, -- 成本 ) create table #output ( goodsno varchar(20) not null, -- 产品编号 quantity float null, -- 数量 amt float null, -- 单位 primary key(goodsno) ) declare @comp varchar(10) declare @id int -- 看公司的下属门店 declare cur_get_compid_2 cursor for select gae01c from gam05 a, gam26 z where a.gae01c = z.gaz02c and z.gaz01c = @compid open cur_get_compid_2 fetch cur_get_compid_2 into @comp while @@fetch_status = 0 begin insert #stock(comp, goodsno, goodsname, garageno, garagename, goodstype, goodstype_key, price, inprice, unit, quantity, amt, provider, cost, amt_cost) exec upg_stock_statistic_for_single @comp, @date, @goodsfrom, @goodsto, @garagefrom, @garageto fetch cur_get_compid_2 into @comp end close cur_get_compid_2 deallocate cur_get_compid_2 if (@printmode = 0) begin delete #stock where isnull(quantity, 0) = 0 end else begin --start打印库存为0的产品资料 insert #gfm01(comp, goodsno) select gfa00c, gfa01c from gfm01 a with (nolock), gam26 z with(nolock) where a.gfa00c = gaz02c and z.gaz02c = @compid delete a from #gfm01 a, #stock b where a.comp = b.comp and a.goodsno = b.goodsno update a set a.amt = 0, a.price = isnull(b.gfa11f, 0), a.inprice = isnull(b.gfa22f, 0), a.goodsname = b.gfa03c, a.goodstype = b.gfa05c, a.goodstype_key = b.gfa24c, a.unit = b.gfa07c, a.provider = b.gfa23c from #gfm01 a, gfm01 b where a.goodsno = b.gfa01c and b.gfa00c = @compid declare @SP054f float -- 成本计算方式 select @SP054f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP054' -- 需要修改成本 if @SP054f = 2 -- 月加权 begin update a set a.cost = b.gfh03f, a.amt_cost = isnull(b.gfh03f, 0) * a.quantity from #gfm01 a, gfm08 b with(nolock) where a.goodsno = b.gfh01c and b.gfh00c = @compid and b.gfh02c = substring(@date, 1, 6) end else begin update a set a.cost = b.gfa22f, a.amt_cost = isnull(b.gfa22f, 0) * a.quantity from #gfm01 a, gfm01 b with(nolock) where a.goodsno = b.gfa01c and b.gfa00c = @compid end update a set a.unit = b.gsb03c from #gfm01 a, gsm02 b where b.gsb00c = @compid and b.gsb01c = 'K' and a.unit = b.gsb02c update a set a.garagename = b.gad02c from #gfm01 a, gam04 b where b.gad00c = @compid and b.gad01c = a.garageno update a set a.goodstype = b.gsb03c from #gfm01 a, gsm02 b where b.gsb00c = @compid and b.gsb01c = 'F' and a.goodstype = b.gsb02c insert #stock(comp, goodsno, goodsname, garageno, garagename, goodstype, goodstype_key, price, inprice, unit, quantity, amt, provider, cost, amt_cost) select comp, goodsno, goodsname, garageno, garagename, goodstype, goodstype_key, price, inprice, unit, quantity, amt, provider, cost, amt_cost from #gfm01 --end end if (isnull(@provider, '') = '' or isnull(@provider, '') = '*') --所有供应商的产品 begin select isnull(comp,'')comp,isnull(goodsno,'')goodsno, isnull(goodsname,'')goodsname, isnull(goodstype,'')goodstype, isnull(price,0)price, n_quan = cast(sum(quantity) as varchar), isnull(unit,'')unit, amt = isnull(sum(amt),0), isnull(cost,0)cost, amt_cost = isnull(sum(amt_cost),0) from #stock group by comp, goodsno, goodsname, goodstype, price, unit, cost order by goodsno, comp end else --指定供应商的产品 begin select isnull(comp,'') comp, isnull(goodsno,'') goodsno, isnull(goodsname,'') goodsname, isnull(goodstype,'') goodstype, isnull(price,'') price, n_quan = isnull(cast(sum(quantity) as varchar),''), isnull(unit,'') unit, amt = isnull(sum(amt),''), isnull(cost,'') cost, amt_cost = isnull(sum(amt_cost),'') from #stock where provider = @provider group by comp, goodsno, goodsname, goodstype, price, unit, cost order by goodsno, comp end drop table #stock drop table #output drop table #gfm01 end GO /****** Object: StoredProcedure [dbo].[upg_adjust_gicm13_turn] Script Date: 06/12/2017 06:19:24 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_adjust_gicm13_turn] ( @compid varchar(10), @billid varchar(20), @curdate varchar(10) -- 入院单号 ) as begin declare @staff01 varchar(20), @staff02 varchar(20), @staff03 varchar(20) declare @turn01 varchar(20), @turn02 varchar(20), @turn03 varchar(20) declare cur_each_glm12 cursor for select gll04v, gll05v, gll06v, gll17c, gll18c, gll19c from glm12 where gll00c = @compid and gll01v = @billid open cur_each_glm12 fetch cur_each_glm12 into @staff01, @staff02, @staff03, @turn01, @turn02, @turn03 while @@fetch_status = 0 begin if isnull(@turn01, '2') = '2' and isnull(@staff01, '') <> '' begin update gicm13 set gick08i = isnull(gick08i, 0) + 1 where gick00c = @compid and gick07d = @curdate and gick04c = @staff01 end if isnull(@turn02, '2') = '2' and isnull(@staff02, '') <> '' begin update gicm13 set gick08i = isnull(gick08i, 0) + 1 where gick00c = @compid and gick07d = @curdate and gick04c = @staff02 end if isnull(@turn03, '2') = '2' and isnull(@staff03, '') <> '' begin update gicm13 set gick08i = isnull(gick08i, 0) + 1 where gick00c = @compid and gick07d = @curdate and gick04c = @staff03 end fetch cur_each_glm12 into @staff01, @staff02, @staff03, @turn01, @turn02, @turn03 end close cur_each_glm12 deallocate cur_each_glm12 end GO /****** Object: StoredProcedure [dbo].[upg_rename_item_comp] Script Date: 06/12/2017 06:20:35 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_rename_item_comp] ( @compid varchar(10), @olditemid varchar(20), @newitemid varchar(20), @userid varchar(20), @curdate varchar(10), @curtime varchar(10) ) as begin if exists(select 1 from gdm01 where gda01c = @newitemid and gda00c = @compid) return if not exists(select 1 from gdm01 where gda01c = @olditemid and gda00c = @compid) return insert sysoperationlog(userid, program, operation, operationdate, operationtime, origatedate, compid, keyvalue1, keyvalue2, keyvalue3) values(@userid, 'GSB02I', 'M', @curdate, @curtime, @curdate, '001', @olditemid, @newitemid, 'upg_rename_item_comp') update a set gda01c = @newitemid from gdm01 a, gam26 z where gda01c = @olditemid and gda00c = z.gaz02c and z.gaz01c = @compid update a set gbj05c = @newitemid from gbm10 a, gam26 z where gbj05c = @olditemid and gbj00c = z.gaz02c and z.gaz01c = @compid update a set gdc01c = @newitemid from gdm03 a, gam26 z where gdc01c = @olditemid and gdc00c = z.gaz02c and z.gaz01c = @compid update a set gfe01c = @newitemid from gfm05 a, gam26 z where gfe01c = @olditemid and gfe00c = z.gaz02c and z.gaz01c = @compid update a set ggb03c = @newitemid from ggm02 a, gam26 z where ggb03c = @olditemid and ggb00c = z.gaz02c and z.gaz01c = @compid update a set gge14c = @newitemid from ggm05 a, gam26 z where gge14c = @olditemid and gge00c = z.gaz02c and z.gaz01c = @compid update a set gld05c = @newitemid from glm04 a, gam26 z where gld05c = @olditemid and gld00c = z.gaz02c and z.gaz01c = @compid update a set hak02c = @newitemid from ham11 a, gam26 z where hak02c = @olditemid and hak00c = z.gaz02c and z.gaz01c = @compid update a set hea01c = @newitemid from hem01 a, gam26 z where hea01c = @olditemid and hea00c = z.gaz02c and z.gaz01c = @compid update a set gcf02c = @newitemid from gcm06 a, gam26 z where gcf02c = @olditemid and gcf00c = z.gaz02c and z.gaz01c = @compid update a set gnc02c = @newitemid from gnm03 a, gam26 z where gnc02c = @olditemid and gnc00c = z.gaz02c and z.gaz01c = @compid update a set gcm02c = @newitemid from gcm11 a, gam26 z where gcm02c = @olditemid and gcm00c = z.gaz02c and z.gaz01c = @compid update a set heb02c = @newitemid from hem02 a, gam26 z where heb02c = @olditemid and heb00c = z.gaz02c and z.gaz01c = @compid update a set gll03v = @newitemid from glm12 a, gam26 z where gll03v = @olditemid and gll00c = z.gaz02c and z.gaz01c = @compid update a set gct07c = @newitemid from gcm20 a, gam26 z where gct07c = @olditemid and (gct06c = '2' or gct06c = '3' or gct06c = '6') and gct00c = z.gaz02c and z.gaz01c = @compid update a set gch04c = @newitemid from gcm08 a, gam26 z where gch04c = @olditemid and gch00c = z.gaz02c and z.gaz01c = @compid update a set gap02c = @newitemid from gam18 a, gam26 z where gap02c = @olditemid and gap00c = z.gaz02c and z.gaz01c = @compid update a set gwa13c = @newitemid from gwm01 a, gam26 z where gwa13c = @olditemid and gwa00c = z.gaz02c and z.gaz01c = @compid update a set gwa14c = @newitemid from gwm01 a, gam26 z where gwa14c = @olditemid and gwa00c = z.gaz02c and z.gaz01c = @compid update a set gwa15c = @newitemid from gwm01 a, gam26 z where gwa15c = @olditemid and gwa00c = z.gaz02c and z.gaz01c = @compid --套餐的项目 update a set gtb02c = @newitemid from gtc02 a, gam26 z where gtb02c = @olditemid and gtb00c = z.gaz02c and z.gaz01c = @compid update a set gtb05c = @newitemid from gtc02 a, gam26 z where gtb05c = @olditemid and gtb00c = z.gaz02c and z.gaz01c = @compid update a set gte03c = @newitemid from gtc04 a, gam26 z where gte03c = @olditemid and gte00c = z.gaz02c and z.gaz01c = @compid update a set gte08c = @newitemid from gtc04 a, gam26 z where gte08c = @olditemid and gte00c = z.gaz02c and z.gaz01c = @compid update a set gti05c = @newitemid from gtc07 a, gam26 z where gti05c = @olditemid and gti12i = 1 and gti00c = z.gaz02c and z.gaz01c = @compid update a set gti13c = @newitemid from gtc07 a, gam26 z where gti13c = @olditemid and gti12i = 1 and gti00c = z.gaz02c and z.gaz01c = @compid update a set gti04c = @newitemid from gtc09 a, gam26 z where gti04c = @olditemid and gti00c = z.gaz02c and z.gaz01c = @compid update a set gti08c = @newitemid from gtc09 a, gam26 z where gti08c = @olditemid and gti00c = z.gaz02c and z.gaz01c = @compid end GO /****** Object: StoredProcedure [dbo].[upg_rename_item] Script Date: 06/12/2017 06:20:35 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_rename_item]( @olditemid varchar(20), @newitemid varchar(20), @userid varchar(20), @curdate varchar(10), @curtime varchar(10)) as begin if exists(select 1 from gdm01 where gda01c = @newitemid) return if not exists(select 1 from gdm01 where gda01c = @olditemid) return insert sysoperationlog(userid, program, operation, operationdate, operationtime, origatedate, compid, keyvalue1, keyvalue2, keyvalue3) values(@userid, 'GSB02I', 'M', @curdate, @curtime, @curdate, '001', @olditemid, @newitemid, 'upg_rename_item') update gdm01 set gda01c = @newitemid where gda01c = @olditemid update gbm10 set gbj05c = @newitemid where gbj05c = @olditemid update gdm03 set gdc01c = @newitemid where gdc01c = @olditemid update gfm05 set gfe01c = @newitemid where gfe01c = @olditemid update ggm02 set ggb03c = @newitemid where ggb03c = @olditemid update ggm05 set gge14c = @newitemid where gge14c = @olditemid update glm04 set gld05c = @newitemid where gld05c = @olditemid update ham11 set hak02c = @newitemid where hak02c = @olditemid update hem01 set hea01c = @newitemid where hea01c = @olditemid update gcm06 set gcf02c = @newitemid where gcf02c = @olditemid update gnm03 set gnc02c = @newitemid where gnc02c = @olditemid update gcm11 set gcm02c = @newitemid where gcm02c = @olditemid update hem02 set heb02c = @newitemid where heb02c = @olditemid update glm12 set gll03v = @newitemid where gll03v = @olditemid update gcm20 set gct07c = @newitemid where gct07c = @olditemid and (gct06c = '2' or gct06c = '3' or gct06c = '6') update gcm08 set gch04c = @newitemid where gch04c = @olditemid update gam18 set gap02c = @newitemid where gap02c = @olditemid update gwm01 set gwa13c = @newitemid where gwa13c = @olditemid update gwm01 set gwa14c = @newitemid where gwa14c = @olditemid update gwm01 set gwa15c = @newitemid where gwa15c = @olditemid --套餐的项目 update gtc02 set gtb02c = @newitemid where gtb02c = @olditemid update gtc02 set gtb05c = @newitemid where gtb05c = @olditemid update gtc04 set gte03c = @newitemid where gte03c = @olditemid update gtc04 set gte08c = @newitemid where gte08c = @olditemid update gtc07 set gti05c = @newitemid where gti05c = @olditemid and gti12i = 1 update gtc07 set gti13c = @newitemid where gti13c = @olditemid and gti12i = 1 update gtc09 set gti04c = @newitemid where gti04c = @olditemid update gtc09 set gti08c = @newitemid where gti08c = @olditemid --Add by maql update gcm16 set gcr02c = @newitemid where gcr02c = @olditemid update hem08 set hei04c = @newitemid where hei04c = @olditemid update hem09 set hej06c = @newitemid where hej06c = @olditemid and hej05i = 1 end GO /****** Object: StoredProcedure [dbo].[upg_cal_cardsale_comm] Script Date: 06/12/2017 06:19:31 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_cal_cardsale_comm] ( @compid varchar(10), -- 公司别 @cardtype varchar(20), -- 卡类别 @code varchar(20), -- 代码 会员卡号 疗程项目 套餐代码 @standardprice float, -- 标准单价 @total float, -- 总金额, 实际金额 @vperf float, -- 虚业绩,应该已经计算好 @times float, -- 疗程数量, 套餐数量, 卡数量 @pctimes float, -- 疗程或套餐的实际次数 @procid float, -- 疗程的价格序号 @personid varchar(200), -- 人员 @flag int, -- 类别, 0-卡充值 1-卡销售 2-疗程 3-套餐 @comm float output, -- 佣金 @trueperf float output, -- 真实业绩 @share_rate float, -- 分享比率 @paycode varchar(10) -- 支付方式 ) as--wuxm, 2004/06/12 begin -- 注意: 卡的成本总是为0 -- 服务项目, 产品的成本已经计算好 declare @type int declare @setvalue float -- 提成方案代码 declare @perf_code varchar(20) select @perf_code = ppt02c from personprecept where ppt00c = @compid and ppt03c = @personid and isnull(ppt04i, 0) = 1 and isnull(ppt11i, 0) = 0 -- 提成方式(1店总业绩 2个人总业绩 3项目/产品/卡分别算 ) declare @pre03i int -- 卡提成计算方式(当提成方式为3时有效:1单独算 2项目总累计 3按类别累计) declare @pre14i int select @pre03i = pre03i, @pre14i = pre14i from precept where pre00c = @compid and pre01c = @perf_code and isnull(pre22i, 0) = 0 select @comm = 0 declare @single int set @single = 0 if @pre03i = 3 and @pre14i = 1 set @single = 1 -- 会员卡销售 if @flag = 1 -- 卡销售 begin if @single = 1 -- 按项目单独算提成 begin -- 提成设置,提成方式,提成金额或比率 select @type = car05i, @setvalue = car06f from cardprecept with (nolock) where car00c = @compid and car01c = @cardtype and car02c = @perf_code end else begin -- 计算累计提成 -- 每项计算提成设置为0 select @type = 1, @setvalue = 0 end end else if @flag = 0 begin if @single = 1 -- 按项目单独算提成 begin -- 提成设置,提成方式,提成金额或比率 select @type = car09i, @setvalue = car10f from cardprecept with (nolock) where car00c = @compid and car01c = @cardtype and car02c = @perf_code end else begin -- 计算累计提成 -- 每项计算提成设置为0 select @type = 1, @setvalue = 0 end end else if @flag = 2 begin if @single = 1 begin declare @free_mode int declare @gda13c varchar(10) if @times = 0 begin select @free_mode = free_comm_mode, @setvalue = free_comm_set from proc_set with (nolock) where compid = @compid and projid = @code and idd = @procid -- 获取疗程默认业绩/提成(按项目) if isnull(@free_mode, 0) = 0 begin select @free_mode = free_comm_mode, @setvalue = free_comm_set from proc_set_perf_comm with (nolock) where compid = @compid and projid = @code end -- 获取疗程默认业绩/提成(按项目统计类别) if isnull(@free_mode, 0) = 0 begin select @gda13c = gda13c from gdm01 where gda00c = @compid and gda01c = @code select @free_mode = free_comm_mode, @setvalue = free_comm_set from proc_set_perf_comm with (nolock) where compid = @compid and projid = '' and projtype = @gda13c end -- 获取疗程默认业绩/提成(默认) if isnull(@free_mode, 0) = 0 begin select @free_mode = free_comm_mode, @setvalue = free_comm_set from proc_set_perf_comm with (nolock) where compid = @compid and projid = '' and projtype = '' end end if isnull(@free_mode, 0) = 1 begin set @type = 100 end else begin declare @proc_times float select @type = comm_mode, @setvalue = comm_set, @proc_times = times from proc_set with (nolock) where compid = @compid and projid = @code and idd = @procid -- 固定提成比率 if @type = 1 begin if @proc_times > 0 set @setvalue = @setvalue / @proc_times end -- 获取疗程默认业绩/提成(按项目) if isnull(@type, '') = '' begin select @type = comm_mode, @setvalue = comm_set from proc_set_perf_comm with (nolock) where compid = @compid and projid = @code end -- 获取疗程默认业绩/提成(按项目统计类别) if isnull(@type, '') = '' begin select @gda13c = gda13c from gdm01 where gda00c = @compid and gda01c = @code select @type = comm_mode, @setvalue = comm_set from proc_set_perf_comm with (nolock) where compid = @compid and projid = '' and projtype = @gda13c end -- 获取疗程默认业绩/提成(默认) if isnull(@type, '') = '' begin select @type = comm_mode, @setvalue = comm_set from proc_set_perf_comm with (nolock) where compid = @compid and projid = '' and projtype = '' end end --如果没有设置疗程的业绩, 那么用卡充值的业绩比率 if isnull(@type, '') = '' begin -- 提成设置,提成方式,提成金额或比率 select @type = car09i, @setvalue = car10f from cardprecept with (nolock) where car00c = @compid and car01c = @cardtype and car02c = @perf_code end select @type = isnull(@type, 2), @setvalue = isnull(@setvalue, 0) end else begin -- 计算累计提成 -- 每项计算提成设置为0 select @type = 1, @setvalue = 0 end end else if @flag = 3 begin if @single = 1 begin select @type = gta05i, @setvalue = gta06f from gtc01 with (nolock) where gta00c = @compid and gta01c = @code select @type = isnull(@type, 2), @setvalue = isnull(@setvalue, 0) end else begin -- 计算累计提成 -- 每项计算提成设置为0 select @type = 1, @setvalue = 0 end end select @setvalue = isnull(@setvalue, 0) -- 计算真实业绩 -- 卡销售与充值 select @trueperf = @vperf if isnull(@type, 0) = 0 return -- 计算提成 if @type = 1 -- 固定金额 begin if(@flag = 2 or @flag =3) --疗程或套餐增加支付方式平分 add by lm 2016.02.25 select @comm = @setvalue * @times * @share_rate * @pctimes else select @comm = @setvalue * @times * @share_rate end else if @type = 100 -- 固定金额 begin if isnull(@free_mode, 0) = 1 --疗程 永久免费 lucz select @comm = @setvalue * @share_rate * @pctimes else select @comm = @setvalue * @share_rate end else if @type = 2 -- 营业额比率 select @comm = @total * @setvalue * @share_rate else if @type = 3 -- 实业绩比率 select @comm = @trueperf * @setvalue else if @type = 4 or @type = 41 -- 虚业绩比率, 实际虚业绩 select @comm = @vperf * @setvalue else if @type = 5 -- 标准价比率 select @comm = @standardprice * @times * @setvalue * @share_rate else if @type = 6 -- 折算虚业绩比率, 考虑实际营业额的比率 select @comm = @vperf * @setvalue else if @type = 10 -- 营业额再折扣 begin if @standardprice * @times > 0 begin select @comm = @total * @setvalue * @share_rate * (@total / (@standardprice * @times )) end else begin select @comm = 0 end end end GO /****** Object: StoredProcedure [dbo].[upg_m_dispatch_proc_set] Script Date: 06/12/2017 06:20:22 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_m_dispatch_proc_set] ( @fromcompid varchar(10), -- 从哪一个公司发布 @tocompid varchar(10), -- 目的公司 @mandatory varchar(2) -- 是否强制统一 ) as begin if @fromcompid = @tocompid return if @mandatory <> '1' and @mandatory <> '0' return if @mandatory = '1' begin delete proc_set where compid = @tocompid insert proc_set(idd, compid, projid, times, sendtimes, buyamt, saveamt, perf_mode, perf_set, comm_mode, comm_set, period, periodunit, unlimted, times_permonth, interval, state, memo, isdefault, free_perf_mode, free_perf_set, free_comm_mode, free_comm_set) select idd, @tocompid, projid, times, sendtimes, buyamt, saveamt, perf_mode, perf_set, comm_mode, comm_set, period, periodunit, unlimted, times_permonth, interval, state, memo, isdefault, free_perf_mode, free_perf_set, free_comm_mode, free_comm_set from proc_set where compid = @fromcompid return end create table #proc_set_dispatch_key -- 疗程资料关键字 ( idd float not null, -- 序号 compid varchar(20) not null, -- 公司别 projid varchar(20) not null, -- 疗程代码 ) declare @PK_Sql nvarchar(max) set @PK_Sql = ' alter table #proc_set_dispatch_key add constraint PK_#proc_set_dispatch_key_' + convert(varchar(10), cast(floor(rand() * 1000000) as int)) + ' primary key clustered(idd, compid, projid)' exec sp_executesql @PK_Sql insert into #proc_set_dispatch_key(idd, compid, projid) select idd, compid, projid from proc_set where compid = @fromcompid declare @current_idd float -- 序号 declare @current_projid varchar(20) -- 疗程代码 -- 创建循环变量 declare @HasRecord int set @HasRecord = 1 declare @Index int set @Index = -1 while(@HasRecord = 1) begin if exists ( select 1 from #proc_set_dispatch_key) begin set @HasRecord = 1 select top 1 @current_idd = idd, @current_projid = projid from #proc_set_dispatch_key if not exists (select 1 from proc_set where idd = @current_idd and compid = @tocompid and projid = @current_projid) begin insert proc_set(idd, compid, projid, times, sendtimes, buyamt, saveamt, perf_mode, perf_set, comm_mode, comm_set, period, periodunit, unlimted, times_permonth, interval, state, memo, isdefault, free_perf_mode, free_perf_set, free_comm_mode, free_comm_set) select idd, @tocompid, projid, times, sendtimes, buyamt, saveamt, perf_mode, perf_set, comm_mode, comm_set, period, periodunit, unlimted, times_permonth, interval, state, memo, isdefault, free_perf_mode, free_perf_set, free_comm_mode, free_comm_set from proc_set where compid = @fromcompid and projid = @current_projid and idd = @current_idd end delete from #proc_set_dispatch_key where idd = @current_idd and projid = @current_projid end else begin set @HasRecord = 0 end end drop table #proc_set_dispatch_key end GO /****** Object: StoredProcedure [dbo].[upg_m_dispatch_proc_set_single] Script Date: 06/12/2017 06:20:22 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_m_dispatch_proc_set_single] ( @fromcompid varchar(10), -- 从哪一个公司发布 @tocompid varchar(10), -- 目的公司 @mandatory varchar(2), -- 是否强制统一 @projid varchar(20) -- 项目编号 ) as begin if @fromcompid = @tocompid return if @mandatory <> '1' and @mandatory <> '0' return if @mandatory = '1' begin delete proc_set where compid = @tocompid and projid = @projid end create table #proc_set_dispatch_single_key -- 疗程资料关键字 ( idd float not null, -- 序号 compid varchar(20) not null, -- 公司别 projid varchar(20) not null, -- 疗程代码 ) declare @PK_Sql nvarchar(max) set @PK_Sql = ' alter table #proc_set_dispatch_single_key add constraint PK_#proc_set_dispatch_single_key_' + convert(varchar(10), cast(floor(rand() * 1000000) as int)) + ' primary key clustered(idd, compid, projid)' exec sp_executesql @PK_Sql insert into #proc_set_dispatch_single_key(idd, compid, projid) select idd, compid, projid from proc_set where compid = @fromcompid and projid = @projid declare @current_idd float -- 序号 declare @current_projid varchar(20) -- 疗程代码 -- 创建循环变量 declare @HasRecord int set @HasRecord = 1 declare @Index int set @Index = -1 while(@HasRecord = 1) begin if exists ( select 1 from #proc_set_dispatch_single_key) begin set @HasRecord = 1 select top 1 @current_idd = idd, @current_projid = projid from #proc_set_dispatch_single_key if not exists (select 1 from proc_set where idd = @current_idd and compid = @tocompid and projid = @current_projid) begin insert proc_set(idd, compid, projid, times, sendtimes, buyamt, saveamt, perf_mode, perf_set, comm_mode, comm_set, period, periodunit, unlimted, times_permonth, interval, state, memo, isdefault, free_perf_mode, free_perf_set, free_comm_mode, free_comm_set) select idd, @tocompid, projid, times, sendtimes, buyamt, saveamt, perf_mode, perf_set, comm_mode, comm_set, period, periodunit, unlimted, times_permonth, interval, state, memo, isdefault, free_perf_mode, free_perf_set, free_comm_mode, free_comm_set from proc_set where compid = @fromcompid and projid = @current_projid and idd = @current_idd end delete from #proc_set_dispatch_single_key where idd = @current_idd and projid = @current_projid end else begin set @HasRecord = 0 end end drop table #proc_set_dispatch_single_key end GO /****** Object: StoredProcedure [dbo].[upg_account_fix_create_gcm15] Script Date: 06/12/2017 06:19:23 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_account_fix_create_gcm15] ( @compid varchar(10), --公司别 @fixdate varchar(8), --封账日期 @userid varchar(20), --操作人 @curdate varchar(8) --操作日期 ) as begin create table #document ( idd int identity not null, compid varchar(20) not null, curdate varchar(8) null, total int null, --单据张数 fromno varchar(100) null, --开始单据号 tono varchar(100) null, --结束单据号 primary key(idd) ) --收银 insert into #document(compid, curdate, total, fromno, tono) select gcq00c, gcq01d, count(*), min(gga01c), max(gga01c) from ggm01 with (nolock), gcm15 b, gam26 z where gga00c = gcq00c and gga80d = gcq01d and gga00c = z.gaz02c and z.gaz01c = @compid group by gcq00c, gcq01d update a set a.gcq02c = '收银单', a.gcq03i = total, a.gcq04c = fromno, a.gcq05c = tono from gcm15 a, #document b where a.gcq01d = b.curdate and a.gcq00c = b.compid --会员卡销售 delete from #document insert into #document(compid, curdate, total, fromno, tono) select gcq00c, gcq01d, count(*), min(gna01c), max(gna01c) from gnm01 with (nolock), gcm15 b, gam26 z where gna00c = gcq00c and gna80d = gcq01d and gna00c = z.gaz02c and z.gaz01c = @compid group by gcq00c, gcq01d update a set a.gcq06c = '卡销售单', a.gcq07i = total, a.gcq08c = fromno, a.gcq09c = tono from gcm15 a, #document b where a.gcq01d = b.curdate and a.gcq00c = b.compid --会员帐务异动 delete from #document insert into #document(compid, curdate, total, fromno, tono) select gcq00c, gcq01d, count(*), min(gcl01c), max(gcl01c) from gcm10 with (nolock), gcm15 b, gam26 z where gcl00c = gcq00c and gcl80d = gcq01d and gcl00c = z.gaz02c and z.gaz01c = @compid group by gcq00c, gcq01d update a set a.gcq10c = '账务异动', a.gcq11i = total, a.gcq12c = fromno, a.gcq13c = tono from gcm15 a, #document b where a.gcq01d = b.curdate and a.gcq00c = b.compid --费用登记 delete from #document insert into #document(compid, curdate, total, fromno, tono) select gcq00c, gcq01d, count(*), min(gqa01c), max(gqa01c) from gqm01, gcm15 b, gam26 z where gqa00c = gcq00c and gqa03c = 'gv'--单据类别 (支出:gv) and gqa80d = gcq01d and gqa00c = z.gaz02c and z.gaz01c = @compid group by gcq00c, gcq01d update a set a.gcq14c = '费用登记', a.gcq15i = total, a.gcq16c = fromno, a.gcq17c = tono from gcm15 a, #document b where a.gcq01d = b.curdate and a.gcq00c = b.compid --入库单 delete from #document insert into #document(compid, curdate, total, fromno, tono) select gha00c, gha03d, count(*), min(gha01c), max(gha01c) from ghm01 with (nolock), gcm15 b, gam26 z where gha00c = gcq00c and gha03d = gcq01d and gha00c = z.gaz02c and z.gaz01c = @compid group by gha00c, gha03d update a set a.gcq18c = '入库单', a.gcq19i = total, a.gcq20c = fromno, a.gcq21c = tono from gcm15 a, #document b where a.gcq01d = b.curdate and a.gcq00c = b.compid --出库单 delete from #document insert into #document(compid, curdate, total, fromno, tono) select gia00c, gia03d, count(*), min(gia01c), max(gia01c) from gim01 with (nolock), gcm15 b, gam26 z where gia00c = gcq00c and gia03d = gcq01d and gia00c = z.gaz02c and z.gaz01c = @compid group by gia00c, gia03d update a set a.gcq22c = '出库单', a.gcq23i = total, a.gcq24c = fromno, a.gcq25c = tono from gcm15 a, #document b where a.gcq01d = b.curdate and a.gcq00c = b.compid --更新当天没有的记录 update a set a.gcq02c = '收银单', a.gcq03i = 0 from gcm15 a, gam26 z where isnull(a.gcq02c, '') = '' and gcq00c = z.gaz02c and z.gaz01c = @compid update a set a.gcq06c = '卡销售单', a.gcq07i = 0 from gcm15 a, gam26 z where isnull(a.gcq06c, '') = '' and gcq00c = z.gaz02c and z.gaz01c = @compid update a set a.gcq10c = '账务异动', a.gcq11i = 0 from gcm15 a, gam26 z where isnull(a.gcq10c, '') = '' and gcq00c = z.gaz02c and z.gaz01c = @compid update a set a.gcq14c = '费用登记', a.gcq15i = 0 from gcm15 a, gam26 z where isnull(a.gcq14c, '') = '' and gcq00c = z.gaz02c and z.gaz01c = @compid update a set a.gcq18c = '入库单', a.gcq19i = 0 from gcm15 a, gam26 z where isnull(a.gcq18c, '') = '' and gcq00c = z.gaz02c and z.gaz01c = @compid update a set a.gcq22c = '出库单', a.gcq23i = 0 from gcm15 a, gam26 z where isnull(a.gcq22c, '') = '' and gcq00c = z.gaz02c and z.gaz01c = @compid drop table #document end GO /****** Object: StoredProcedure [dbo].[upg_combo_sale_stat] Script Date: 06/12/2017 06:19:42 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_combo_sale_stat] ( @compid varchar(10), -- 公司别 @fromdate varchar(20), -- 开始日期 @todate varchar(20), -- 截止日期 @fromcombo varchar(20), -- 疗程项目 @tocombo varchar(20), -- 疗程项目 @fromcard varchar(20), -- 会员卡号 @tocard varchar(20) -- 会员卡号 ) as begin declare @incompid varchar(10) select @incompid = @compid declare @payment_zp varchar(10) --支票的支付方式 declare @is_revenue varchar(10) --判断是否为收入项目 select @is_revenue = gsa02c from gsm01 where gsa00c = @compid and gsa01c = 'SP063' if @is_revenue = '1' begin select @payment_zp = '$' end else begin select @payment_zp = 'ZZ' -- 无此支付方式 end create table #tbl_proc_sale ( idd int identity not null, compid varchar(10) not null, procid varchar(20) not null, --套餐ID cardid varchar(20) not null, ddate varchar(8) null, btimes float null, stimes float null, procamt float null, --套餐金额 billtype varchar(20) null, billid varchar(20) null, srv1 varchar(20) null, srv2 varchar(20) null, srv3 varchar(20) null, mem_name varchar(40) null, -- 会员姓名 cardnum float null, payment_way1 varchar(5) null, --支付方式1 payment_name1 varchar(40) null, --支付名称1 payment_way2 varchar(5) null, --支付方式2 payment_name2 varchar(40) null, --支付名称2 payment_way3 varchar(5) null, --支付方式3 payment_name3 varchar(40) null, --支付名称3 payment_way4 varchar(5) null, --支付方式4 payment_name4 varchar(40) null, ---支付名称4 ) declare @PK_Sql nvarchar(max) set @PK_Sql = ' alter table #tbl_proc_sale add constraint PK_#tbl_proc_sale_' + convert(varchar(10), cast(floor(rand() * 1000000) as int)) + ' primary key nonclustered(idd)' exec sp_executesql @PK_Sql create clustered index idx_proc_sale_01 on #tbl_proc_sale(compid, billtype, billid, procid) if @fromdate = '*' or @fromdate = '' begin select @fromdate = '19900101', @todate = '20901231' end if @fromcombo = '*' or @fromcombo = '' begin select @fromcombo = min(gta01c), @tocombo = max(gta01c) from gtc01 where gta00c = @compid end insert #tbl_proc_sale(compid, procid, cardid, ddate, btimes, stimes, procamt, billtype, billid, cardnum, payment_way1, payment_way2, payment_way3, payment_way4) select distinct compid = gti00c, procid = gti04c, cardid = gti03c, ddate = gti02d, btimes = 0, stimes = 0, procamt = 0, billtype = gti08c, billid = gti09c, case when gti08c = 'pj' then 0 else 1 end, payment_way1 = gti17c, payment_way2 = gti19c, payment_way3 = gti21c, payment_way4 = gti23c from gtc07 a with (nolock), gam26 z with (nolock) where gti00c = z.gaz02c and z.gaz01c = @compid and gti02d >= @fromdate and gti02d <= @todate and gti08c in('gn', 'gz', 'pj') --modified by maql 20100909 退套餐生成的单据类别是pj 在此加上pj and gti04c >= @fromcombo and gti04c <= @tocombo and isnull(gti17c,'') <> '' order by gti04c, gti02d insert #tbl_proc_sale(compid, procid, cardid, ddate, btimes, stimes, procamt, billtype, billid, cardnum, payment_way1, payment_way2, payment_way3, payment_way4) select distinct compid = gti00c, procid = gti04c, cardid = gti03c, ddate = gti02d, btimes = 0, stimes = 0, procamt = 0, billtype = gti08c, billid = gti09c, case when gti08c = 'pj' then 0 else 1 end, payment_way1 = gti19c, payment_way2 = gti19c, payment_way3 = gti21c, payment_way4 = gti23c from gtc07 a with (nolock), gam26 z with (nolock) where gti00c = z.gaz02c and z.gaz01c = @compid and gti02d >= @fromdate and gti02d <= @todate and gti08c in('gn', 'gz', 'pj') --modified by maql 20100909 退套餐生成的单据类别是pj 在此加上pj and gti04c >= @fromcombo and gti04c <= @tocombo and isnull(gti19c,'') <> '' order by gti04c, gti02d insert #tbl_proc_sale(compid, procid, cardid, ddate, btimes, stimes, procamt, billtype, billid, cardnum, payment_way1, payment_way2, payment_way3, payment_way4) select distinct compid = gti00c, procid = gti04c, cardid = gti03c, ddate = gti02d, btimes = 0, stimes = 0, procamt = 0, billtype = gti08c, billid = gti09c, case when gti08c = 'pj' then 0 else 1 end, payment_way1 = gti21c, payment_way2 = gti19c, payment_way3 = gti21c, payment_way4 = gti23c from gtc07 a with (nolock), gam26 z with (nolock) where gti00c = z.gaz02c and z.gaz01c = @compid and gti02d >= @fromdate and gti02d <= @todate and gti08c in('gn', 'gz', 'pj') --modified by maql 20100909 退套餐生成的单据类别是pj 在此加上pj and gti04c >= @fromcombo and gti04c <= @tocombo and isnull(gti21c,'') <> '' order by gti04c, gti02d insert #tbl_proc_sale(compid, procid, cardid, ddate, btimes, stimes, procamt, billtype, billid, cardnum, payment_way1, payment_way2, payment_way3, payment_way4) select distinct compid = gti00c, procid = gti04c, cardid = gti03c, ddate = gti02d, btimes = 0, stimes = 0, procamt = 0, billtype = gti08c, billid = gti09c, case when gti08c = 'pj' then 0 else 1 end, payment_way1 = gti23c, payment_way2 = gti19c, payment_way3 = gti21c, payment_way4 = gti23c from gtc07 a with (nolock), gam26 z with (nolock) where gti00c = z.gaz02c and z.gaz01c = @compid and gti02d >= @fromdate and gti02d <= @todate and gti08c in('gn', 'gz', 'pj') --modified by maql 20100909 退套餐生成的单据类别是pj 在此加上pj and gti04c >= @fromcombo and gti04c <= @tocombo and isnull(gti23c,'') <> '' order by gti04c, gti02d declare @onlycash float select @onlycash = gsa03f from gsm01 with (nolock) where gsa00c = @compid and gsa01c = 'SP139' if @onlycash = 2 -- 所有支付方式 begin update a set a.btimes = round(isnull(gtf10f,0)/isnull(gtf05f,1),2), a.procamt = isnull(gtf10f,0) from #tbl_proc_sale a, gtc05 b with (nolock) where a.compid = b.gtf00c and a.billid = b.gtf01c and a.procid = b.gtf02c and a.payment_way1 = gtf09c and isnull(gtf09c,'') <> '' and a.billtype = 'gn' update a set a.btimes = round(isnull(gtf12f,0)/isnull(gtf05f,1),2), a.procamt = isnull(gtf12f,0) from #tbl_proc_sale a, gtc05 b with (nolock) where a.compid = b.gtf00c and a.billid = b.gtf01c and a.procid = b.gtf02c and a.payment_way1 = gtf11c and isnull(gtf11c,'') <> '' and a.billtype = 'gn' update a set a.btimes = round(isnull(gtf14f,0)/isnull(gtf05f,1),2), a.procamt = isnull(gtf14f,0) from #tbl_proc_sale a, gtc05 b with (nolock) where a.compid = b.gtf00c and a.billid = b.gtf01c and a.procid = b.gtf02c and a.payment_way1 = gtf13c and isnull(gtf13c,'') <> '' and a.billtype = 'gn' update a set a.btimes = round(isnull(gtf16f,0)/isnull(gtf05f,1),2), a.procamt = isnull(gtf16f,0) from #tbl_proc_sale a, gtc05 b with (nolock) where a.compid = b.gtf00c and a.billid = b.gtf01c and a.procid = b.gtf02c and a.payment_way1 = gtf15c and isnull(gtf15c,'') <> '' and a.billtype = 'gn' --关联充值单,如果不是取款 金额为正数 update a set a.btimes = round(isnull(gth10f,0)/isnull(gth05f,1),2), a.procamt = isnull(gth10f,0) from #tbl_proc_sale a, gtc06 b with (nolock),gcm10 c with(nolock) where a.compid = b.gth00c and a.billid = b.gth01c and a.compid = c.gcl00c and a.billid = c.gcl01c and c.gcl06i <> 1 and a.billtype = 'gz' and a.payment_way1 = gth09c and isnull(gth09c,'') <> '' and a.procid = b.gth02c --关联充值单,如果不是取款 金额为正数 update a set a.btimes = round(isnull(gth12f,0)/isnull(gth05f,1),2), a.procamt = isnull(gth12f,0) from #tbl_proc_sale a, gtc06 b with (nolock),gcm10 c with(nolock) where a.compid = b.gth00c and a.billid = b.gth01c and a.compid = c.gcl00c and a.billid = c.gcl01c and c.gcl06i <> 1 and a.billtype = 'gz' and a.payment_way1 = gth11c and isnull(gth11c,'') <> '' and a.procid = b.gth02c --关联充值单,如果不是取款 金额为正数 update a set a.btimes = round(isnull(gth14f,0)/isnull(gth05f,1),2), a.procamt = isnull(gth14f,0) from #tbl_proc_sale a, gtc06 b with (nolock),gcm10 c with(nolock) where a.compid = b.gth00c and a.billid = b.gth01c and a.compid = c.gcl00c and a.billid = c.gcl01c and c.gcl06i <> 1 and a.billtype = 'gz' and a.payment_way1 = gth13c and isnull(gth13c,'') <> '' and a.procid = b.gth02c --关联充值单,如果不是取款 金额为正数 update a set a.btimes = round(isnull(gth16f,0)/isnull(gth05f,1),2), a.procamt = isnull(gth16f,0) from #tbl_proc_sale a, gtc06 b with (nolock),gcm10 c with(nolock) where a.compid = b.gth00c and a.billid = b.gth01c and a.compid = c.gcl00c and a.billid = c.gcl01c and c.gcl06i <> 1 and a.billtype = 'gz' and a.payment_way1 = gth15c and isnull(gth15c,'') <> '' and a.procid = b.gth02c --关联充值单,如果是取款 金额为负数 update a set a.btimes = -1 * b.gth04f, a.procamt = -1 * gth05f from #tbl_proc_sale a, gtc06 b with (nolock),gcm10 c with(nolock) where a.compid = b.gth00c and a.billid = b.gth01c and a.compid = c.gcl00c and a.billid = c.gcl01c and c.gcl06i = 1 and a.billtype = 'gz' and a.procid = b.gth02c end else -- 只有现金类 begin update a set a.btimes = b.gtf04f, a.procamt = (case when gtf09c in ('1', '6', 'ZFB', '&', 'WX', @payment_zp) then gtf10f else 0 end + case when gtf11c in ('1', '6', 'ZFB', '&', 'WX', @payment_zp) then gtf12f else 0 end + case when gtf13c in ('1', '6', 'ZFB', '&', 'WX', @payment_zp) then gtf14f else 0 end + case when gtf15c in ('1', '6', 'ZFB', '&', 'WX', @payment_zp) then gtf16f else 0 end) from #tbl_proc_sale a, gtc05 b with (nolock) where a.compid = b.gtf00c and a.billid = b.gtf01c and a.procid = b.gtf02c and a.billtype = 'gn' update a set a.btimes = b.gth04f, a.procamt = (case when gth09c in ('1', '6', 'ZFB', '&', 'WX', @payment_zp) then gth10f else 0 end + case when gth11c in ('1', '6', 'ZFB', '&', 'WX', @payment_zp) then gth12f else 0 end + case when gth13c in ('1', '6', 'ZFB', '&', 'WX', @payment_zp) then gth14f else 0 end + case when gth15c in ('1', '6', 'ZFB', '&', 'WX', @payment_zp) then gth16f else 0 end) from #tbl_proc_sale a, gtc06 b with (nolock) where a.compid = b.gth00c and a.billid = b.gth01c and a.billtype = 'gz' and a.procid = b.gth02c end update a set a.srv1 = b.gna20c, a.srv2 = b.gna28c, a.srv3 = b.gna30c from #tbl_proc_sale a, gnm01 b with (nolock) where a.compid = b.gna00c and a.billid = b.gna01c and a.billtype = 'gn' update a set a.srv1 = b.gcl09c, a.srv2 = b.gcl17c, a.srv3 = b.gcl19c from #tbl_proc_sale a, gcm10 b with (nolock) where a.compid = b.gcl00c and a.billid = b.gcl01c and a.billtype = 'gz' update a set a.srv1 = b.gcl09c, a.srv2 = b.gcl17c, a.srv3 = b.gcl19c from #tbl_proc_sale a, gcm10 b with (nolock) where a.compid = b.gcl00c and a.billid = b.gcl01c and a.billtype = 'pj' -- Add by maql 20100909 退套餐生成的单据类别是pj update a set a.billtype = b.gsf03c from #tbl_proc_sale a, gsm06 b with (nolock) where a.billtype = b.gsf02c and b.gsf01c = '1' create table #hem09 --替换套餐的历史记录 ( hej00c varchar(10) not null, --公司 hej01c varchar(20) not null, --单号 hej02c varchar(20) not null, --卡号 hej04c varchar(20) not null, --套餐编号 hej07f float null, --更换的金额 hej08f float null, --更换的次数 ) insert #hem09(hej00c, hej01c, hej02c, hej04c, hej08f, hej07f) select hej00c, hej01c, hej02c, hej04c, min(hej08f) hej08f, sum(hej07f)hej07f from hem09 with (nolock) where hej00c = @compid and ((hej03d >= @fromdate and hej03d <= @todate) or hej03d = '*') and ((hej04c >= @fromcombo and hej04c <= @tocombo) or hej04c = '*') group by hej00c, hej01c, hej02c, hej04c update a set a.btimes = -b.hej08f, a.procamt = -b.hej07f from #tbl_proc_sale a, #hem09 b where a.compid = b.hej00c and a.billid = b.hej01c and a.cardid = b.hej02c and a.procid = b.hej04c and a.billtype = 'pj' --Add by maql 20100909 update a set a.payment_way1 = '#' from #tbl_proc_sale a where a.billtype = 'pj' -- Add by maql 20100909 update a set a.billtype = '更换套餐' from #tbl_proc_sale a where a.billtype = 'pj' -- Add by maql 20100909 update a set a.mem_name = b.gba03c from #tbl_proc_sale a, gbm01 b with (nolock) where a.cardid = b.gba23c and b.gba00c = a.compid update a set a.payment_name1 = b.gsb03c from #tbl_proc_sale a, gsm02 b with (nolock) where a.compid = b.gsb00c and a.payment_way1 = b.gsb02c and b.gsb01c = '3' update a set a.payment_name2 = b.gsb03c from #tbl_proc_sale a, gsm02 b with (nolock) where a.compid = b.gsb00c and a.payment_way2 = b.gsb02c and b.gsb01c = '3' update a set a.payment_name3 = b.gsb03c from #tbl_proc_sale a, gsm02 b with (nolock) where a.compid = b.gsb00c and a.payment_way3 = b.gsb02c and b.gsb01c = '3' update a set a.payment_name4 = b.gsb03c from #tbl_proc_sale a, gsm02 b with (nolock) where a.compid = b.gsb00c and a.payment_way4 = b.gsb02c and b.gsb01c = '3' -- 四个 update a set a.payment_name1 = isnull(payment_name1,isnull(payment_name2,isnull(payment_name3,isnull(payment_name4,'')))) , a.payment_way1 = isnull(payment_way1,isnull(payment_way2,isnull(payment_way3,isnull(payment_way4,'')))) from #tbl_proc_sale a select isnull(idd,'') idd,isnull(compid,'') compid,isnull(procid,'') procid,isnull(cardid,'') cardid,isnull(ddate,'') ddate, isnull(btimes,0) btimes,isnull(stimes,0) stimes,isnull(procamt,0) procamt,isnull(billtype,'') billtype,isnull(billid,'') billid, isnull(srv1,'') srv1,isnull(srv2,'') srv2,isnull(srv3,'') srv3,isnull(mem_name,'') mem_name,isnull(cardnum,0) cardnum, isnull(payment_way1,'') payment_way1,isnull(payment_name1,'') payment_name1,isnull(payment_way2,'') payment_way2, isnull(payment_name2,'') payment_name2,isnull(payment_way3,'') payment_way3,isnull(payment_name3,'') payment_name3, isnull(payment_way4,'') payment_way4,isnull(payment_name4,'') payment_name4 from #tbl_proc_sale where cardid between @fromcard and @tocard or @fromcard='*' --modify by limin 2014/12/11 union all select '' idd,'' compid,'' procid,'' cardid,'' ddate, 0 btimes,0 stimes,sum(isnull(procamt,0)) procamt,'' billtype,'paysum' billid, '' srv1,'' srv2,'' srv3,'' mem_name,0 cardnum, '' payment_way1,isnull(payment_name1,'')payment_name1,'' payment_way2, '' payment_name2,'' payment_way3,'' payment_name3, '' payment_way4,'' payment_name4 from #tbl_proc_sale where cardid between @fromcard and @tocard or @fromcard='*' group by payment_way1,payment_name1 order by procid, ddate drop table #tbl_proc_sale drop table #hem09 end GO /****** Object: StoredProcedure [dbo].[upg_query_gtc01] Script Date: 06/12/2017 06:20:30 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_query_gtc01] ( @compid varchar(10) ) as begin declare @SP217_2 varchar(2) --套餐是否严格控制有效时长 declare @SP217_3 varchar(2) --套餐是否严格控制有效时间范围 select @SP217_2 = gsa02c, @SP217_3 = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP217' if (@SP217_2 = '0' and @SP217_3 = '0') begin select gta00c, gta01c, gta02c, gta03f, gta04c, gta05i, gta06f, gta07i, gta08f, gta09i, gta10i, gta10d, gta11i, gta12c, gta13c, gta14c from gtc01 with(nolock) where gta00c = @compid and isnull(gta10i,0) <> 1 end else begin --获取当前日期 declare @curdate varchar(20), @curtime varchar(20) exec upg_getcurrdate @curdate output, @curtime output --2个权限都有,则取第1个 if((@SP217_2 = '1' and @SP217_3 = '0') or (@SP217_2 = '1' and @SP217_3 = '1')) begin select gta00c, gta01c, gta02c, gta03f, gta04c, gta05i, gta06f, gta07i, gta08f, gta09i, gta10i, gta10d, gta11i, gta12c, gta13c, gta14c from ( select gta00c, gta01c, gta02c, gta03f, gta04c, gta05i, gta06f, gta07i, gta08f, gta09i, gta10i, gta10d, gta11i, gta12c, gta13c, gta14c, convert(varchar(10),(case gta12c when '1' then dateadd(year,gta11i,gta10d) --年 when '2' then dateadd(month,gta11i,gta10d) --月 when '3' then dateadd(day,gta11i,gta10d) --日 when '4' then dateadd(month,gta11i * 3 ,gta10d) --季度 end),112) as dtime from gtc01 with(nolock) where gta00c = @compid and isnull(gta10i,0) <> 1 ) as a where dtime >= @curdate end if(@SP217_2 = '0' and @SP217_3 = '1' ) begin select gta00c, gta01c, gta02c, gta03f, gta04c, gta05i, gta06f, gta07i, gta08f, gta09i, gta10i, gta10d, gta11i, gta12c, gta13c, gta14c from gtc01 with(nolock) where gta00c = @compid and isnull(gta10i,0) <> 1 and gta13c <= @curdate and gta14c >= @curdate end end end GO /****** Object: StoredProcedure [dbo].[upg_create_gcm20_one_card] Script Date: 06/12/2017 06:19:52 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_create_gcm20_one_card] ( @cardno varchar(20) -- 会员卡号 ) as begin if substring(@cardno, 1, 2) = '散客' return if not exists (select 1 from gcm01 where gca04c = @cardno and gca00c = gca13d) return delete gcm20 where gct02c = @cardno -- 卡销售 insert gcm20(gct00c, gct02c, gct03c, gct04d, gct05c, gct06c, gct07c, gct08c, gct09f, gct10f, gct11f, gct12f, gct13c, gct14c, gct15c, gct16c, gct17c, gct18c, gct19t, gct20c) select gna00c, gnb03c, gna04c, gna02d, '', '1', gnb02c, gak02c, gnb05f, gnb07f, 1, gnb07f, 'gn', gna01c, gna20c, gna28c, gna30c, gna32c, gna12t, gnb15c from gnm01 a with (nolock), gnm02 b with (nolock), gam10 c where a.gna00c = b.gnb00c and a.gna01c = b.gnb01c and b.gnb00c = c.gak00c and b.gnb02c = c.gak01c and b.gnb03c = @cardno and isnull(gnb07f, 0)>0 insert gcm20(gct00c, gct02c, gct03c, gct04d, gct05c, gct06c, gct07c, gct08c, gct09f, gct10f, gct11f, gct12f, gct13c, gct14c, gct15c, gct16c, gct17c, gct18c, gct19t, gct20c) select gna00c, gnb03c, gna04c, gna02d, '', '2', gnc02c, gda03c, gda10f, gnc05f/(case isnull(gnc03f, 1) + isnull(gnc04f, 0) when 0 then 1 else isnull(gnc03f, 1) + isnull(gnc04f, 0) end), isnull(gnc03f, 0) + isnull(gnc04f, 0), gnc05f, 'gn', gna01c, gna20c, gna28c, gna30c, gna32c, gna12t, gnc15c from gnm01 a with (nolock), gnm03 b with (nolock), gdm01 c, gnm02 d with (nolock) where gna00c = gnc00c and a.gna01c = b.gnc01c and b.gnc00c = c.gda00c and b.gnc02c = c.gda01c and b.gnc00c = d.gnb00c and b.gnc01c = d.gnb01c and d.gnb03c = @cardno insert gcm20(gct00c, gct02c, gct03c, gct04d, gct05c, gct06c, gct07c, gct08c, gct09f, gct10f, gct11f, gct12f, gct13c, gct14c, gct15c, gct16c, gct17c, gct19t, gct20c) select gtf00c, gna13c, isnull(gna04c, ''), gna02d, '', '7', gtf02c, gta02c, gtf06f, gtf05f, 1, gtf05f, 'gn', gtf01c, gna20c, gna28c, gna30c, gna12t, gtf09c from gtc05 a with (nolock), gtc01 b, gnm01 c with (nolock) where a.gtf00c = b.gta00c and a.gtf02c = b.gta01c and a.gtf00c = c.gna00c and a.gtf01c = c.gna01c and c.gna13c = @cardno -- 充值 insert gcm20(gct00c, gct02c, gct03c, gct04d, gct05c, gct06c, gct07c, gct08c, gct09f, gct10f, gct11f, gct12f, gct13c, gct14c, gct15c, gct16c, gct17c, gct18c, gct19t, gct20c) select gcl00c, gcl03c, '', gcl02d, convert(varchar(10), gcl06i), '5', '', gsb03c, gcl07f-isnull(gcl66f, 0)-isnull(gcl51f, 0), gcl07f-isnull(gcl66f, 0)-isnull(gcl51f, 0), 1, gcl07f-isnull(gcl66f, 0)-isnull(gcl51f, 0), 'gz', gcl01c, gcl09c , gcl17c, gcl19c, gcl22c, gcl56t, gcl67c from gcm10 a with (nolock), gsm02 b where a.gcl00c = b.gsb00c and b.gsb01c = 'A8' and a.gcl06i = convert(int, b.gsb02c) and isnull(gcl94d, '') <> '' and a.gcl03c = @cardno and (gcl07f - isnull(gcl66f, 0) - isnull(gcl51f, 0) > 0 or gcl14i = 1 or isnull(gcl65f, 0) > 0) insert gcm20(gct00c, gct02c, gct03c, gct04d, gct05c, gct06c, gct07c, gct08c, gct09f, gct10f, gct11f, gct12f, gct13c, gct14c, gct15c, gct16c, gct17c, gct18c, gct19t, gct20c) select gcl00c, gcl03c, '', gcl02d, convert(varchar(10), gcl06i), '6', gcm02c, gda03c, gda10f, gcm05f/(case isnull(gcm03f, 1) + isnull(gcm04f, 0) when 0 then 1 else isnull(gcm03f, 1) + isnull(gcm04f, 0) end ), isnull(gcm03f, 0) + isnull(gcm04f, 0), gcm05f, 'gz', gcl01c, gcl09c, gcl17c, gcl19c, gcl22c, gcl56t, gcm15c from gcm10 a with (nolock), gcm11 b with (nolock), gdm01 c where a.gcl00c = b.gcm00c and a.gcl01c = b.gcm01c and b.gcm00c = c.gda00c and b.gcm02c = c.gda01c and a.gcl03c = @cardno insert gcm20(gct00c, gct02c, gct03c, gct04d, gct05c, gct06c, gct07c, gct08c, gct09f, gct10f, gct11f, gct12f, gct13c, gct14c, gct15c, gct16c, gct17c, gct19t, gct20c) select gth00c, gcl03c, isnull(gca04c, ''), gcl02d, '', '8', gth02c, gta02c, gth06f, gth05f, 1, gth05f, 'gz', gth01c, gcl09c, gcl17c, gcl19c, gcl56t, gth09c from gtc06 a with (nolock), gtc01 b, gcm10 c with (nolock), gcm01 d with (nolock) where a.gth00c = b.gta00c and a.gth02c = b.gta01c and a.gth00c = c.gcl00c and a.gth01c = c.gcl01c -- and c.gcl00c = d.gca00c and c.gcl03c = d.gca01c and c.gcl03c = @cardno -- 收银 -- 项目 -- 有卡会员部分 insert gcm20(gct00c, gct02c, gct03c, gct04d, gct05c, gct06c, gct07c, gct08c, gct09f, gct10f, gct11f, gct12f, gct13c, gct14c, gct15c, gct16c, gct17c, gct18c, gct19t, gct20c, gct21f) select gga00c, gga09c, gga05c, gga02d, '', '3', ggb03c, gda03c, ggb08f, ggb10f, ggb05f, ggb11f, 'gx', gga01c, ggb12c, ggb13c, ggb14c, ggb26c, gga04t, ggb27c, ggb02f from ggm01 a with (nolock), ggm02 b with (nolock), gdm01 c where a.gga00c = b.ggb00c and a.gga01c = b.ggb01c and b.ggb00c = c.gda00c and b.ggb03c = c.gda01c and a.gga09c = @cardno -- 产品 insert gcm20(gct00c, gct02c, gct03c, gct04d, gct05c, gct06c, gct07c, gct08c, gct09f, gct10f, gct11f, gct12f, gct13c, gct14c, gct15c, gct16c, gct17c, gct18c, gct19t, gct20c) select gga00c, gga09c, gga05c, gga02d, '', '4', ggc03c, gfa03c, ggc08f, ggc10f, ggc05f, ggc11f, 'gx', gga01c, ggc12c, ggc15c, ggc17c, '', gga04t, ggc27c from ggm01 a with (nolock), ggm03 b with (nolock), gfm01 c where a.gga00c = b.ggc00c and a.gga01c = b.ggc01c and b.ggc00c = c.gfa00c and b.ggc03c = c.gfa01c and a.gga09c = @cardno update a set a.gct03c = isnull(b.gca04c, b.gca01c) from gcm20 a, gcm01 b where a.gct00c = b.gca00c and a.gct02c = b.gca01c and a.gct02c = @cardno end GO /****** Object: StoredProcedure [dbo].[upg_get_bill_memo] Script Date: 06/12/2017 06:20:04 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_get_bill_memo] ( @compid varchar(20), @billtype varchar(10), @billid varchar(20), @memo varchar(250) output ) as begin declare @nname varchar(60) if @billtype = 'gx' begin -- 项目以及产品 declare cur_item_001 cursor for select distinct gda03c from ggm02 a with (nolock), gdm01 b where a.ggb00c = b.gda00c and a.ggb03c = b.gda01c and a.ggb00c = @compid and a.ggb01c = @billid open cur_item_001 fetch cur_item_001 into @nname while @@fetch_status = 0 begin select @memo = isnull(@memo, '') + isnull(@nname, '') + ', ' fetch cur_item_001 into @nname end close cur_item_001 deallocate cur_item_001 declare cur_good_001 cursor for select distinct gfa03c from ggm03 a with (nolock), gfm01 b where a.ggc00c = b.gfa00c and a.ggc03c = b.gfa01c and a.ggc00c = @compid and a.ggc01c = @billid open cur_good_001 fetch cur_good_001 into @nname while @@fetch_status = 0 begin select @memo = isnull(@memo, '') + isnull(@nname, '') + ', ' fetch cur_good_001 into @nname end close cur_good_001 deallocate cur_good_001 end else if @billtype = 'gn' begin -- 疗程套餐 declare cur_proc_001 cursor for select distinct gda03c from gnm03 a with (nolock), gdm01 b where a.gnc00c = b.gda00c and a.gnc02c = b.gda01c and a.gnc00c = @compid and a.gnc01c = @billid open cur_proc_001 fetch cur_proc_001 into @nname while @@fetch_status = 0 begin select @memo = isnull(@memo, '') + isnull(@nname, '') + ', ' fetch cur_proc_001 into @nname end close cur_proc_001 deallocate cur_proc_001 --套餐 declare cur_combo_001 cursor for select distinct gta02c from gtc05 a with (nolock), gtc01 b where a.gtf00c = b.gta00c and a.gtf02c = b.gta01c and a.gtf00c = @compid and a.gtf01c = @billid open cur_combo_001 fetch cur_combo_001 into @nname while @@fetch_status = 0 begin select @memo = isnull(@memo, '') + isnull(@nname, '') + ', ' fetch cur_combo_001 into @nname end close cur_combo_001 deallocate cur_combo_001 end else if @billtype = 'gz' begin -- 疗程 declare cur_proc_002 cursor for select distinct gda03c from gcm11 a with (nolock), gdm01 b where a.gcm00c = b.gda00c and a.gcm02c = b.gda01c and a.gcm00c = @compid and a.gcm01c = @billid open cur_proc_002 fetch cur_proc_002 into @nname while @@fetch_status = 0 begin select @memo = isnull(@memo, '') + isnull(@nname, '') + ', ' fetch cur_proc_002 into @nname end close cur_proc_002 deallocate cur_proc_002 --套餐 declare cur_combo_002 cursor for select distinct gta02c from gtc06 a with (nolock), gtc01 b where a.gth00c = b.gta00c and a.gth02c = b.gta01c and a.gth00c = @compid and a.gth01c = @billid open cur_combo_002 fetch cur_combo_002 into @nname while @@fetch_status = 0 begin select @memo = isnull(@memo, '') + isnull(@nname, '') + ', ' fetch cur_combo_002 into @nname end close cur_combo_002 deallocate cur_combo_002 end end GO /****** Object: StoredProcedure [dbo].[upg_create_gcm20_from_one_bill] Script Date: 06/12/2017 06:19:51 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_create_gcm20_from_one_bill] ( @compid varchar(10), @billtype varchar(10), @billid varchar(20), @isconfirm int -- 是否为复核 1复核 0取消复核 ) as begin if ((@isconfirm = 0) or (@isconfirm = 1 and @billtype = 'gx')) begin delete gcm20 where gct00c = @compid and gct13c = @billtype and gct14c = @billid end if (@isconfirm = 0) return -- 卡销售 if (@billtype = 'gn') begin insert gcm20(gct00c, gct02c, gct03c, gct04d, gct05c, gct06c, gct07c, gct08c, gct09f, gct10f, gct11f, gct12f, gct13c, gct14c, gct15c, gct16c, gct17c, gct18c, gct19t, gct20c) select gna00c, gnb03c, isnull(gca04c, ''), gna02d, '', '1', gnb02c, gak02c, gnb05f, gnb07f, 1, gnb07f, 'gn', gna01c, gna20c, gna28c, gna30c, gna32c, gna12t, gnb15c from gnm01 a with (nolock), gnm02 b with (nolock), gam10 c with (nolock), gcm01 d with (nolock) where a.gna00c = b.gnb00c and a.gna01c = b.gnb01c and b.gnb00c = c.gak00c and b.gnb02c = c.gak01c and d.gca00c = @compid and d.gca01c = b.gnb03c and a.gna00c = @compid and a.gna01c = @billid and isnull(gnb07f, 0) > 0 -- 美容院版本才有疗程 insert gcm20(gct00c, gct02c, gct03c, gct04d, gct05c, gct06c, gct07c, gct08c, gct09f, gct10f,gct11f,gct12f, gct13c, gct14c, gct15c, gct16c, gct17c, gct18c, gct19t, gct20c) select gna00c, gnb03c, isnull(gna04c, ''), gna02d, '', '2', gnc02c, gda03c, gda10f, dbo.fun_divide_default(gnc05f, (isnull(gnc03f, 1) + isnull(gnc04f, 0)), 0), isnull(gnc03f, 0) + isnull(gnc04f, 0), gnc05f, 'gn', gna01c, gna20c, gna28c, gna30c, gna32c, gna12t, gnc15c from gnm01 a with (nolock), gnm03 b with (nolock), gdm01 c with (nolock), gnm02 d with (nolock) where gna00c = gnc00c and a.gna01c = b.gnc01c and b.gnc00c *= c.gda00c and b.gnc02c *= c.gda01c and b.gnc00c = d.gnb00c and b.gnc01c = d.gnb01c and a.gna00c = @compid and a.gna01c = @billid insert gcm20(gct00c, gct02c, gct03c, gct04d, gct05c, gct06c, gct07c, gct08c, gct09f, gct10f, gct11f, gct12f, gct13c, gct14c, gct15c, gct16c, gct17c, gct19t, gct20c) select gtf00c, gna13c, isnull(gna04c,''), gna02d, '', '7', gtf02c, gta02c, gtf06f, gtf05f, 1, gtf05f, 'gn', gtf01c, gna20c, gna28c, gna30c, gna12t, gtf09c from gtc05 a with (nolock), gtc01 b with (nolock), gnm01 c with (nolock) where a.gtf00c = b.gta00c and a.gtf02c = b.gta01c and a.gtf00c = c.gna00c and a.gtf01c = c.gna01c and c.gna00c = @compid and c.gna01c = @billid end --充值 if (@billtype = 'gz') begin insert gcm20(gct00c, gct02c, gct03c, gct04d, gct05c, gct06c, gct07c, gct08c, gct09f, gct10f, gct11f, gct12f, gct13c, gct14c, gct15c, gct16c, gct17c, gct18c, gct19t, gct20c) select gcl00c, gcl03c, '', gcl02d, convert(varchar(10), gcl06i), '5', '', gsb03c, gcl07f-isnull(gcl66f, 0)-isnull(gcl51f, 0), gcl07f-isnull(gcl66f, 0)-isnull(gcl51f, 0), 1, gcl07f-isnull(gcl66f, 0)-isnull(gcl51f, 0), 'gz', gcl01c, gcl09c, gcl17c, gcl19c, gcl22c, gcl56t, gcl67c from gcm10 a with (nolock), gsm02 b with (nolock) where a.gcl00c = b.gsb00c and b.gsb01c = 'A8' and a.gcl06i = convert(int, b.gsb02c) and isnull(gcl94d, '')<>'' and a.gcl00c = @compid and a.gcl01c = @billid and (gcl07f-isnull(gcl66f, 0)-isnull(gcl51f, 0)>0 or gcl14i = 1 or isnull(gcl65f, 0)>0) insert gcm20(gct00c, gct02c, gct03c, gct04d, gct05c, gct06c, gct07c, gct08c, gct09f, gct10f, gct11f, gct12f, gct13c, gct14c, gct15c, gct16c, gct17c, gct18c, gct19t, gct20c) select gcl00c, gcl03c, '', gcl02d, convert(varchar(10), gcl06i), '6', gcm02c, gda03c, gda10f, dbo.fun_divide_default(gcm05f, (isnull(gcm03f, 1) + isnull(gcm04f, 0)), 0), isnull(gcm03f, 0) + isnull(gcm04f, 0), gcm05f, 'gz', gcl01c, gcl09c, gcl17c, gcl19c, gcl22c, gcl56t, gcm15c from gcm10 a with (nolock), gcm11 b with (nolock), gdm01 c with (nolock) where a.gcl00c = b.gcm00c and a.gcl01c = b.gcm01c and b.gcm00c *= c.gda00c and b.gcm02c *= c.gda01c and a.gcl00c = @compid and a.gcl01c = @billid insert gcm20(gct00c, gct02c, gct03c, gct04d, gct05c, gct06c, gct07c, gct08c, gct09f, gct10f, gct11f, gct12f, gct13c, gct14c, gct15c, gct16c, gct17c, gct19t, gct20c) select gth00c, gcl03c, isnull(gca04c,gcl03c), gcl02d, '', '8', gth02c, gta02c, gth06f, gth05f, 1, gth05f, 'gz', gth01c, gcl09c, gcl17c, gcl19c, gcl56t, gth09c from gtc06 a with (nolock), gtc01 b with (nolock), gcm10 c with (nolock), gcm01 d with (nolock) where a.gth00c = b.gta00c and a.gth02c = b.gta01c and a.gth00c = c.gcl00c and a.gth01c = c.gcl01c --and c.gcl00c = d.gca00c and c.gcl03c = d.gca01c and c.gcl00c = @compid and c.gcl01c = @billid end if (@billtype = 'gx' or @billtype = 'gl') begin --收银 --项目 declare @cardid varchar(30) declare @enterid varchar(20) declare @glm_billid varchar(20) declare @gsa03f float select @gsa03f = isnull(gsa03f, 0) from gsm01 with (nolock) where gsa01c = 'SP031' and gsa00c = @compid if (@gsa03f = 2 and @billtype = 'gx') or (@gsa03f<>2 and @billtype = 'gl') return create table #ggm_billid(billid varchar(20) not null) if (@billtype = 'gx') begin select @cardid = gga09c, @enterid = gga30c from ggm01 with (nolock) where gga00c = @compid and gga01c = @billid --insert #ggm_billid(billid) values(@billid) end else if (@billtype = 'gl') begin select @cardid = gla06c, @enterid = gla01c from glm01 with (nolock) where gla00c = @compid and gla01c = @billid insert #ggm_billid(billid) select gga01c from ggm01 with (nolock) where gga00c = @compid and gga30c = @billid end if substring(@cardid, 1, 2)<>'散客' begin insert gcm20(gct00c, gct02c, gct03c, gct04d, gct05c, gct06c, gct07c, gct08c, gct09f, gct10f, gct11f, gct12f, gct13c, gct14c, gct15c, gct16c, gct17c, gct18c, gct19t, gct20c, gct21f) select gga00c, gga09c, isnull(gga05c,gga09c), gga02d, '', '3', ggb03c, gda03c, ggb08f, ggb10f, ggb05f, ggb11f, 'gx', @billid, ggb12c, ggb13c, ggb14c, ggb26c, gga04t, ggb27c, ggb02f from ggm01 a with (nolock) inner join ggm02 b with (nolock) on a.gga00c = b.ggb00c and a.gga01c = b.ggb01c left join gdm01 c with (nolock) on b.ggb00c = c.gda00c and b.ggb03c = c.gda01c where a.gga00c = @compid and a.gga01c = @billid --产品 insert gcm20(gct00c, gct02c, gct03c, gct04d, gct05c, gct06c, gct07c, gct08c, gct09f, gct10f, gct11f, gct12f, gct13c, gct14c, gct15c, gct16c, gct17c, gct18c, gct19t, gct20c) select gga00c, gga09c, isnull(gga05c,gga09c), gga02d, '', '4', ggc03c, gfa03c, ggc08f, ggc10f, ggc05f, ggc11f, 'gx', @billid, ggc12c, ggc15c, ggc17c, '', gga04t, ggc27c from ggm01 a with (nolock) inner join ggm03 b with (nolock) on a.gga00c = b.ggc00c and a.gga01c = b.ggc01c left join gfm01 c with (nolock) on b.ggc00c = c.gfa00c and b.ggc03c = c.gfa01c where a.gga00c = @compid and a.gga01c = @billid end else -- 散客,但有会员号 begin declare @memid varchar(20) select @memid = gla27c from glm01 b with (nolock) where gla00c = @compid and gla01c = @enterid and isnull(gla27c, '')<>'' and substring(gla06c, 1, 2) = '散客' and substring(gla27c, 1, 2)<>'散客' if isnull(@memid, '')<>'' begin --项目 insert gcm20(gct00c, gct02c, gct03c, gct04d, gct05c, gct06c, gct07c, gct08c, gct09f, gct10f, gct11f, gct12f, gct13c, gct14c, gct15c, gct16c, gct17c, gct18c, gct19t, gct20c, gct21f) select gga00c, '', @memid, gga02d, '', '3', ggb03c, gda03c, ggb08f, ggb10f, ggb05f, ggb11f, 'gx', d.billid, ggb12c, ggb13c, ggb14c, ggb26c, gga04t, ggb27c, ggb02f from ggm01 a with (nolock), ggm02 b with (nolock), gdm01 c with (nolock), #ggm_billid d where a.gga00c = b.ggb00c and a.gga01c = b.ggb01c and b.ggb00c *= c.gda00c and b.ggb03c *= c.gda01c and a.gga00c = @compid and a.gga01c = d.billid --产品购买 insert gcm20(gct00c, gct02c, gct03c, gct04d, gct05c, gct06c, gct07c, gct08c, gct09f, gct10f, gct11f, gct12f, gct13c, gct14c, gct15c, gct16c, gct17c, gct18c, gct19t, gct20c) select gga00c, '', @memid, gga02d, '', '4', ggc03c, gfa03c, ggc08f, ggc10f, ggc05f, ggc11f, 'gx', d.billid, ggc12c, ggc15c, ggc17c, '', gga04t, ggc27c from ggm01 a with (nolock), ggm03 b with (nolock), gfm01 c with (nolock), #ggm_billid d where a.gga00c = b.ggc00c and a.gga01c = b.ggc01c and b.ggc00c *= c.gfa00c and b.ggc03c *= c.gfa01c and a.gga00c = @compid and a.gga01c = d.billid end end drop table #ggm_billid end end GO /****** Object: StoredProcedure [dbo].[upg_create_gcm20_from_raw_bills] Script Date: 06/12/2017 06:19:52 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_create_gcm20_from_raw_bills] ( @fromdate varchar(8), @todate varchar(8) ) as begin delete gcm20 where gct04d >= @fromdate and gct04d <= @todate -- 卡销售 insert gcm20(gct00c, gct02c, gct03c, gct04d, gct05c, gct06c, gct07c, gct08c, gct09f, gct10f, gct11f, gct12f, gct13c, gct14c, gct15c, gct16c, gct17c, gct18c, gct19t, gct20c) select gna00c, gnb03c, gna04c, gna02d, '', '1', gnb02c, gak02c, gnb05f, gnb07f, 1, gnb07f, 'gn', gna01c, gna20c, gna28c, gna30c, gna32c, gna12t, gnb15c from gnm01 a with (nolock), gnm02 b with (nolock), gam10 c where a.gna00c = b.gnb00c and a.gna01c = b.gnb01c and b.gnb00c = c.gak00c and b.gnb02c = c.gak01c and a.gna02d >= @fromdate and a.gna02d <= @todate and isnull(gnb07f, 0)>0 --美容院版本才有疗程 insert gcm20(gct00c, gct02c, gct03c, gct04d, gct05c, gct06c, gct07c, gct08c, gct09f, gct10f, gct11f, gct12f, gct13c, gct14c, gct15c, gct16c, gct17c, gct18c, gct19t, gct20c) select gna00c, gnb03c, gna04c, gna02d, '', '2', gnc02c, gda03c, gda10f, gnc05f/(case isnull(gnc03f, 1) + isnull(gnc04f, 0) when 0 then 1 else isnull(gnc03f, 1) + isnull(gnc04f, 0) end), isnull(gnc03f, 0) + isnull(gnc04f, 0), gnc05f, 'gn', gna01c, gna20c, gna28c, gna30c, gna32c, gna12t, gnc15c from gnm01 a with (nolock), gnm03 b with (nolock), gdm01 c, gnm02 d with (nolock) where gna00c = gnc00c and a.gna01c = b.gnc01c and b.gnc00c = c.gda00c and b.gnc02c = c.gda01c and b.gnc00c = d.gnb00c and b.gnc01c = d.gnb01c and a.gna02d >= @fromdate and a.gna02d <= @todate insert gcm20(gct00c, gct02c, gct03c, gct04d, gct05c, gct06c, gct07c, gct08c, gct09f, gct10f, gct11f, gct12f, gct13c, gct14c, gct15c, gct16c, gct17c, gct19t, gct20c) select gtf00c, gna13c, isnull(gna04c, ''), gna02d, '', '7', gtf02c, gta02c, gtf06f, gtf05f, 1, gtf05f, 'gn', gtf01c, gna20c, gna28c, gna30c, gna12t, gtf09c from gtc05 a with (nolock), gtc01 b, gnm01 c with (nolock) where a.gtf00c = b.gta00c and a.gtf02c = b.gta01c and a.gtf00c = c.gna00c and a.gtf01c = c.gna01c and c.gna02d >= @fromdate and c.gna02d <= @todate --充值 insert gcm20(gct00c, gct02c, gct03c, gct04d, gct05c, gct06c, gct07c, gct08c, gct09f, gct10f, gct11f, gct12f, gct13c, gct14c, gct15c, gct16c, gct17c, gct18c, gct19t, gct20c) select gcl00c, gcl03c, '', gcl02d, convert(varchar(10), gcl06i), '5', '', gsb03c, gcl07f-isnull(gcl66f, 0)-isnull(gcl51f, 0), gcl07f-isnull(gcl66f, 0)-isnull(gcl51f, 0), 1, gcl07f-isnull(gcl66f, 0)-isnull(gcl51f, 0), 'gz', gcl01c, gcl09c, gcl17c, gcl19c, gcl22c, gcl56t, gcl67c from gcm10 a with (nolock), gsm02 b where a.gcl00c = b.gsb00c and b.gsb01c = 'A8' and a.gcl06i = convert(int, b.gsb02c) and isnull(gcl94d, '')<>'' and a.gcl02d >= @fromdate and a.gcl02d <= @todate and (gcl07f-isnull(gcl66f, 0)-isnull(gcl51f, 0)>0 or gcl14i = 1 or isnull(gcl65f, 0)>0) --美容院版本才有疗程 insert gcm20(gct00c, gct02c, gct03c, gct04d, gct05c, gct06c, gct07c, gct08c, gct09f, gct10f, gct11f, gct12f, gct13c, gct14c, gct15c, gct16c, gct17c, gct18c, gct19t, gct20c) select gcl00c, gcl03c, '', gcl02d, convert(varchar(10), gcl06i), '6', gcm02c, gda03c, gda10f, gcm05f/(case isnull(gcm03f, 1) + isnull(gcm04f, 0) when 0 then 1 else isnull(gcm03f, 1) + isnull(gcm04f, 0) end ), isnull(gcm03f, 0) + isnull(gcm04f, 0), gcm05f, 'gz', gcl01c, gcl09c, gcl17c, gcl19c, gcl22c, gcl56t, gcm15c from gcm10 a with (nolock), gcm11 b with (nolock), gdm01 c where a.gcl00c = b.gcm00c and a.gcl01c = b.gcm01c and b.gcm00c = c.gda00c and b.gcm02c = c.gda01c and a.gcl02d >= @fromdate and a.gcl02d <= @todate insert gcm20(gct00c, gct02c, gct03c, gct04d, gct05c, gct06c, gct07c, gct08c, gct09f, gct10f, gct11f, gct12f, gct13c, gct14c, gct15c, gct16c, gct17c, gct19t, gct20c) select gth00c, gcl03c, isnull(gca04c, ''), gcl02d, '', '8', gth02c, gta02c, gth06f, gth05f, 1, gth05f, 'gz', gth01c, gcl09c, gcl17c, gcl19c, gcl56t, gth09c from gtc06 a with (nolock), gtc01 b, gcm10 c with (nolock), gcm01 d with (nolock) where a.gth00c = b.gta00c and a.gth02c = b.gta01c and a.gth00c = c.gcl00c and a.gth01c = c.gcl01c and c.gcl00c = d.gca00c and c.gcl03c = d.gca01c and c.gcl02d >= @fromdate and c.gcl02d <= @todate --收银 --项目 --有卡会员部分 insert gcm20(gct00c, gct02c, gct03c, gct04d, gct05c, gct06c, gct07c, gct08c, gct09f, gct10f, gct11f, gct12f, gct13c, gct14c, gct15c, gct16c, gct17c, gct18c, gct19t, gct20c, gct21f) select gga00c, gga09c, gga05c, gga02d, '', '3', ggb03c, gda03c, ggb08f, ggb10f, ggb05f, ggb11f, 'gx', gga01c, ggb12c, ggb13c, ggb14c, ggb26c, gga04t, ggb27c, ggb02f from ggm01 a with (nolock), ggm02 b with (nolock), gdm01 c where a.gga00c = b.ggb00c and a.gga01c = b.ggb01c and b.ggb00c = c.gda00c and b.ggb03c = c.gda01c and a.gga02d >= @fromdate and a.gga02d <= @todate and substring(gga09c, 1, 2)<>'散客' --产品 insert gcm20(gct00c, gct02c, gct03c, gct04d, gct05c, gct06c, gct07c, gct08c, gct09f, gct10f, gct11f, gct12f, gct13c, gct14c, gct15c, gct16c, gct17c, gct18c, gct19t, gct20c) select gga00c, gga09c, gga05c, gga02d, '', '4', ggc03c, gfa03c, ggc08f, ggc10f, ggc05f, ggc11f, 'gx', gga01c, ggc12c, ggc15c, ggc17c, '', gga04t, ggc27c from ggm01 a with (nolock), ggm03 b with (nolock), gfm01 c where a.gga00c = b.ggc00c and a.gga01c = b.ggc01c and b.ggc00c = c.gfa00c and b.ggc03c = c.gfa01c and a.gga02d >= @fromdate and a.gga02d <= @todate and substring(gga09c, 1, 2)<>'散客' update a set a.gct03c = isnull(b.gca04c, b.gca01c) from gcm20 a, gcm01 b where a.gct00c = b.gca00c and a.gct02c = b.gca01c and a.gct04d >= @fromdate and a.gct04d <= @todate and substring(gct02c, 1, 2)<>'散客' --下面处理所有没有卡号但有会员编号的消费 --顾问版本没有此消费, 也就是是否有入院 declare @sp005 varchar(10) select @sp005 = gsa02c from gsm01 where gsa00c = '001' and gsa01c = 'SP005' if @sp005 = '1' begin create table #related_ggm01( idd int identity not null, billid varchar(20) not null, memid varchar(20) null, compid varchar(10) null, primary key nonclustered(idd)) create clustered index idx_related_ggm01_01 on #related_ggm01(compid, billid) --先查询出这个会员相关的所有消费单 insert #related_ggm01(billid, memid, compid) select gga01c, isnull(gla27c, ''), gla00c from ggm01 a with (nolock), glm01 b with (nolock) where a.gga00c = b.gla00c and a.gga30c = b.gla01c and a.gga02d >= @fromdate and a.gga02d <= @todate and isnull(gla27c, '')<>'' and substring(gla06c, 1, 2) = '散客' and substring(gla27c, 1, 2)<>'散客' --收银 --项目 insert gcm20(gct00c, gct02c, gct03c, gct04d, gct05c, gct06c, gct07c, gct08c, gct09f, gct10f, gct11f, gct12f, gct13c, gct14c, gct15c, gct16c, gct17c, gct18c, gct19t, gct20c, gct21f) select gga00c, '', memid, gga02d, '', '3', ggb03c, gda03c, ggb08f, ggb10f, ggb05f, ggb11f, 'gx', gga01c, ggb12c, ggb13c, ggb14c, ggb26c, gga04t, ggb27c, ggb02f from ggm01 a with (nolock), ggm02 b with (nolock), gdm01 c, #related_ggm01 r where a.gga00c = b.ggb00c and a.gga01c = b.ggb01c and b.ggb00c = c.gda00c and b.ggb03c = c.gda01c and r.compid = a.gga00c and r.billid = a.gga01c --产品购买 insert gcm20(gct00c, gct02c, gct03c, gct04d, gct05c, gct06c, gct07c, gct08c, gct09f, gct10f, gct11f, gct12f, gct13c, gct14c, gct15c, gct16c, gct17c, gct18c, gct19t, gct20c) select gga00c, '', memid, gga02d, '', '4', ggc03c, gfa03c, ggc08f, ggc10f, ggc05f, ggc11f, 'gx', gga01c, ggc12c, ggc15c, ggc17c, '', gga04t, ggc27c from ggm01 a with (nolock), ggm03 b with (nolock), gfm01 c, #related_ggm01 r where a.gga00c = b.ggc00c and a.gga01c = b.ggc01c and b.ggc00c = c.gfa00c and b.ggc03c = c.gfa01c and r.compid = a.gga00c and r.billid = a.gga01c drop table #related_ggm01 end end GO /****** Object: StoredProcedure [dbo].[upg_m_dispatch_gtc01] Script Date: 06/12/2017 06:20:20 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_m_dispatch_gtc01] ( @fromcompid varchar(10), -- 从哪一个公司发布 @tocompid varchar(10), -- 目的公司 @mandatory varchar(2) -- 是否强制统一 ) as begin if @fromcompid = @tocompid return create table #gtc01_01 -- 套餐资料主档 ( gta00c varchar(10) not null, -- 公司编号 gta01c varchar(20) not null, -- 套餐编号 gta02c varchar(30) null, -- 套餐名称 gta03f float null, -- 标准价格 gta04c varchar(2000) null, -- 套餐简介 gta05i tinyint null, -- 套餐销售提成方式 gta06f float null, -- 套餐销售提成比率 gta07i tinyint null, -- 套餐销售业绩方式 gta08f float null, -- 套餐销售金额或比率 gta09i int null, -- 是否需要消耗产品 gta10i int null, -- 是否停用 1停止 0正常 gta10d datetime null, -- 创建时间 gta11i int null, --有效期限 gta12c varchar(2) null, --期限单位 gta13c varchar(10) null, --有效起始日期 权限SP217-3 gta14c varchar(10) null --有效截止日期 权限SP217-3 ) declare @PK_Sql nvarchar(max) set @PK_Sql = ' alter table #gtc01_01 add constraint PK_#gtc01_01_' + convert(varchar(10), cast(floor(rand() * 1000000) as int)) + ' primary key clustered(gta00c, gta01c)' exec sp_executesql @PK_Sql insert #gtc01_01(gta00c, gta01c, gta02c, gta03f, gta04c, gta05i, gta06f, gta07i, gta08f, gta09i, gta10i, gta10d,gta11i,gta12c,gta13c,gta14c) select gta00c, gta01c, gta02c, gta03f, gta04c, gta05i, gta06f, gta07i, gta08f, gta09i, gta10i, gta10d,gta11i,gta12c,gta13c,gta14c from gtc01 where gta00c = @fromcompid if @mandatory = '1' begin delete gtc01 where gta00c = @tocompid end else begin delete a from #gtc01_01 a, gtc01 b where a.gta00c = @fromcompid and a.gta01c = b.gta01c and b.gta00c = @tocompid end insert gtc01(gta00c, gta01c, gta02c, gta03f, gta04c, gta05i, gta06f, gta07i, gta08f, gta09i, gta10i, gta10d,gta11i,gta12c,gta13c,gta14c) select @tocompid, gta01c, gta02c, gta03f, gta04c, gta05i, gta06f, gta07i, gta08f, gta09i, gta10i, gta10d,gta11i,gta12c,gta13c,gta14c from #gtc01_01 where gta00c = @fromcompid -- modify by lyj 2008-05-30 在插入的时候必须只添加新增的明细 insert gtc02(gtb00c, gtb01c, gtb02c, gtb03f, gtb04f, gtb05c, gtb06i, gtb07f, gtb08i, gtb09f, gtb10i, gtb11f, gtb12i, gtb13f, gtb14i, gtb15f, gtb16i, gtb17f, gtb18i, gtb19f, gtb20i, gtb21f,gtb22i,gtb23c,gtb24c,gtb25c) select @tocompid, a.gtb01c, a.gtb02c, a.gtb03f, a.gtb04f, a.gtb05c, a.gtb06i, a.gtb07f, a.gtb08i, a.gtb09f, a.gtb10i, a.gtb11f, a.gtb12i, a.gtb13f, a.gtb14i, a.gtb15f, a.gtb16i, a.gtb17f, a.gtb18i, a.gtb19f, a.gtb20i, a.gtb21f,a.gtb22i,a.gtb23c,a.gtb24c,a.gtb25c from gtc02 a where a.gtb00c = @fromcompid and a.gtb02c not in(select b.gtb02c from gtc02 b where b.gtb00c = @tocompid and a.gtb01c = b.gtb01c) -- modify by lyj 2008-05-30 在插入的时候必须只添加新增的明细 insert gtc03(gtd00c, gtd01c, gtd02c, gtd03f, gtd04f, gtd05c) select @tocompid, gtd01c, gtd02c, gtd03f, gtd04f, gtd05c from gtc03 a where a.gtd00c = @fromcompid and a.gtd02c not in (select b.gtd02c from gtc03 b where b.gtd00c = @tocompid and a.gtd01c = b.gtd01c) drop table #gtc01_01 end GO /****** Object: StoredProcedure [dbo].[upg_m_dispatch_gtc01_single] Script Date: 06/12/2017 06:20:20 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_m_dispatch_gtc01_single] ( @fromcompid varchar(10), -- 从哪一个公司发布 @tocompid varchar(10), -- 目的公司 @mandatory varchar(2), -- 是否强制统一 @gta01c varchar(20) -- 套餐编号 ) as begin if @fromcompid = @tocompid return if @mandatory <> '1' and @mandatory <> '0' return if @mandatory = '0' begin if exists (select 1 from gtc01 where gta00c = @tocompid and gta01c = @gta01c) return end delete gtc01 where gta00c = @tocompid and gta01c = @gta01c insert gtc01(gta00c, gta01c, gta02c, gta03f, gta04c, gta05i, gta06f, gta07i, gta08f, gta09i, gta10i, gta10d,gta11i,gta12c,gta13c,gta14c) select @tocompid, gta01c, gta02c, gta03f, gta04c, gta05i, gta06f, gta07i, gta08f, gta09i, gta10i, gta10d,gta11i,gta12c,gta13c,gta14c from gtc01 where gta00c = @fromcompid and gta01c = @gta01c -- modify by lyj 2008-05-30 在插入的时候必须只添加新增的明细 insert gtc02(gtb00c, gtb01c, gtb02c, gtb03f, gtb04f, gtb05c, gtb06i, gtb07f, gtb08i, gtb09f, gtb10i, gtb11f, gtb12i, gtb13f, gtb14i, gtb15f, gtb16i, gtb17f, gtb18i, gtb19f, gtb20i, gtb21f,gtb22i,gtb23c,gtb24c,gtb25c) select @tocompid, a.gtb01c, a.gtb02c, a.gtb03f, a.gtb04f, a.gtb05c, a.gtb06i, a.gtb07f, a.gtb08i, a.gtb09f, a.gtb10i, a.gtb11f, a.gtb12i, a.gtb13f, a.gtb14i, a.gtb15f, a.gtb16i, a.gtb17f, a.gtb18i, a.gtb19f, a.gtb20i, a.gtb21f,a.gtb22i,a.gtb23c,a.gtb24c,a.gtb25c from gtc02 a where a.gtb00c = @fromcompid and gtb01c = @gta01c -- modify by lyj 2008-05-30 在插入的时候必须只添加新增的明细 insert gtc03(gtd00c, gtd01c, gtd02c, gtd03f, gtd04f, gtd05c) select @tocompid, gtd01c, gtd02c, gtd03f, gtd04f, gtd05c from gtc03 a where a.gtd00c = @fromcompid and a.gtd01c = @gta01c end GO /****** Object: StoredProcedure [dbo].[upg_amend_prod_unit] Script Date: 06/12/2017 06:19:25 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_amend_prod_unit] ( @compid varchar(10) -- 门店编号 ) as begin update a set a.gcp05c = b.gsb02c from gcm22 a join gsm02 b on a.gcp00c = @compid and isnull(a.gcp05c, '') <> '' and b.gsb00c = @compid and b.gsb01c = 'K' and a.gcp05c = b.gsb02c update a set a.gdd04c = b.gsb02c from gdm04 a join gsm02 b on a.gdd00c = @compid and isnull(a.gdd04c, '') <> '' and b.gsb00c = @compid and b.gsb01c = 'K' and a.gdd04c = b.gsb02c update a set a.gdv07c = b.gsb02c from gdm22 a join gsm02 b on a.gdv00c = @compid and isnull(a.gdv07c, '') <> '' and b.gsb00c = @compid and b.gsb01c = 'K' and a.gdv07c = b.gsb02c update a set a.gfa07c = b.gsb02c from gfm01 a join gsm02 b on a.gfa00c = @compid and isnull(a.gfa07c, '') <> '' and b.gsb00c = @compid and b.gsb01c = 'K' and a.gfa07c = b.gsb02c update a set a.gfa08c = b.gsb02c from gfm01 a join gsm02 b on a.gfa00c = @compid and isnull(a.gfa08c, '') <> '' and b.gsb00c = @compid and b.gsb01c = 'K' and a.gfa08c = b.gsb02c update a set a.gfa20c = b.gsb02c from gfm01 a join gsm02 b on a.gfa00c = @compid and isnull(a.gfa20c, '') <> '' and b.gsb00c = @compid and b.gsb01c = 'K' and a.gfa20c = b.gsb02c update a set a.gfa33c = b.gsb02c from gfm01 a join gsm02 b on a.gfa00c = @compid and isnull(a.gfa33c, '') <> '' and b.gsb00c = @compid and b.gsb01c = 'K' and a.gfa33c = b.gsb02c update a set a.gfa36c = b.gsb02c from gfm01 a join gsm02 b on a.gfa00c = @compid and isnull(a.gfa36c, '') <> '' and b.gsb00c = @compid and b.gsb01c = 'K' and a.gfa36c = b.gsb02c update a set a.gfa41c = b.gsb02c from gfm01 a join gsm02 b on a.gfa00c = @compid and isnull(a.gfa41c, '') <> '' and b.gsb00c = @compid and b.gsb01c = 'K' and a.gfa41c = b.gsb02c update a set a.gfa44c = b.gsb02c from gfm01 a join gsm02 b on a.gfa00c = @compid and isnull(a.gfa44c, '') <> '' and b.gsb00c = @compid and b.gsb01c = 'K' and a.gfa44c = b.gsb02c update a set a.gfd11c = b.gsb02c from gfm04 a join gsm02 b on a.gfd00c = @compid and isnull(a.gfd11c, '') <> '' and b.gsb00c = @compid and b.gsb01c = 'K' and a.gfd11c = b.gsb02c update a set a.ggc04c = b.gsb02c from ggm03 a join gsm02 b on a.ggc00c = @compid and isnull(a.ggc04c, '') <> '' and b.gsb00c = @compid and b.gsb01c = 'K' and a.ggc04c = b.gsb02c update a set a.gge04c = b.gsb02c from ggm05 a join gsm02 b on a.gge00c = @compid and isnull(a.gge04c, '') <> '' and b.gsb00c = @compid and b.gsb01c = 'K' and a.gge04c = b.gsb02c update a set a.ghb04c = b.gsb02c from ghm02 a join gsm02 b on a.ghb00c = @compid and isnull(a.ghb04c, '') <> '' and b.gsb00c = @compid and b.gsb01c = 'K' and a.ghb04c = b.gsb02c update a set a.ghb06c = b.gsb02c from ghm02 a join gsm02 b on a.ghb00c = @compid and isnull(a.ghb06c, '') <> '' and b.gsb00c = @compid and b.gsb01c = 'K' and a.ghb06c = b.gsb02c update a set a.gib04c = b.gsb02c from gim02 a join gsm02 b on a.gib00c = @compid and isnull(a.gib04c, '') <> '' and b.gsb00c = @compid and b.gsb01c = 'K' and a.gib04c = b.gsb02c update a set a.gib06c = b.gsb02c from gim02 a join gsm02 b on a.gib00c = @compid and isnull(a.gib06c, '') <> '' and b.gsb00c = @compid and b.gsb01c = 'K' and a.gib06c = b.gsb02c update a set a.gjb04c = b.gsb02c from gjm02 a join gsm02 b on a.gjb00c = @compid and isnull(a.gjb04c, '') <> '' and b.gsb00c = @compid and b.gsb01c = 'K' and a.gjb04c = b.gsb02c update a set a.gkb04c = b.gsb02c from gkm02 a join gsm02 b on a.gkb00c = @compid and isnull(a.gkb04c, '') <> '' and b.gsb00c = @compid and b.gsb01c = 'K' and a.gkb04c = b.gsb02c update a set a.glp04c = b.gsb02c from glm15 a join gsm02 b on a.glp00c = @compid and isnull(a.glp04c, '') <> '' and b.gsb00c = @compid and b.gsb01c = 'K' and a.glp04c = b.gsb02c -- -- update a -- set a.grb05c = b.gsb02c -- from grm02 a join gsm02 b -- on a.grb00c = @compid and isnull(a.grb05c, '') <> '' and b.gsb00c = @compid and b.gsb01c = 'K' and a.grb05c = b.gsb02c -- -- update a -- set a.grc05c = b.gsb02c -- from grm03 a join gsm02 b -- on a.grc00c = @compid and isnull(a.grc05c, '') <> '' and b.gsb00c = @compid and b.gsb01c = 'K' and a.grc05c = b.gsb02c -- update a -- set a.gtb05c = b.gsb02c -- from gtm02 a join gsm02 b -- on a.gtb00c = @compid and isnull(a.gtb05c, '') <> '' and b.gsb00c = @compid and b.gsb01c = 'K' and a.gtb05c = b.gsb02c -- -- update a -- set a.gvb09c = b.gsb02c -- from gvm02 a join gsm02 b -- on a.gvb00c = @compid and isnull(a.gvb09c, '') <> '' and b.gsb00c = @compid and b.gsb01c = 'K' and a.gvb09c = b.gsb02c update a set a.gin05c = b.gsb02c from gim21 a join gsm02 b on a.gin00c = @compid and isnull(a.gin05c, '') <> '' and b.gsb00c = @compid and b.gsb01c = 'K' and a.gin05c = b.gsb02c update a set a.gin08c = b.gsb02c from gim21 a join gsm02 b on a.gin00c = @compid and isnull(a.gin08c, '') <> '' and b.gsb00c = @compid and b.gsb01c = 'K' and a.gin08c = b.gsb02c update a set a.gio08c = b.gsb02c from gim22 a join gsm02 b on a.gio00c = @compid and isnull(a.gio08c, '') <> '' and b.gsb00c = @compid and b.gsb01c = 'K' and a.gio08c = b.gsb02c update a set a.gio12c = b.gsb02c from gim22 a join gsm02 b on a.gio00c = @compid and isnull(a.gio12c, '') <> '' and b.gsb00c = @compid and b.gsb01c = 'K' and a.gio12c = b.gsb02c end GO /****** Object: StoredProcedure [dbo].[upg_m_dispatch_proc_set_perf_comm] Script Date: 06/12/2017 06:20:22 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_m_dispatch_proc_set_perf_comm] ( @fromcompid varchar(10), -- 从哪一个公司发布 @tocompid varchar(10), -- 目的公司 @mandatory varchar(2) -- 是否强制统一 ) as begin if @fromcompid = @tocompid return if @mandatory <> '1' and @mandatory <> '0' return if @mandatory = '1' begin delete proc_set_perf_comm where compid = @tocompid insert proc_set_perf_comm(compid, projid, projtype, perf_mode, perf_set, comm_mode, comm_set, free_perf_mode, free_perf_set, free_comm_mode, free_comm_set) select @tocompid, projid, projtype, perf_mode, perf_set, comm_mode, comm_set, free_perf_mode, free_perf_set, free_comm_mode, free_comm_set from proc_set_perf_comm where compid = @fromcompid return end create table #proc_set_dispatch_single_key -- 疗程资料关键字 ( projid varchar(20) not null, -- 疗程代码 projtype varchar(10) not null -- 统计分类(统计), 对应于gdm01.gda13c ) declare @PK_Sql nvarchar(max) set @PK_Sql = ' alter table #proc_set_dispatch_single_key add constraint PK_#proc_set_dispatch_single_key_' + convert(varchar(10), cast(floor(rand() * 1000000) as int)) + ' primary key clustered(projid, projtype)' exec sp_executesql @PK_Sql insert into #proc_set_dispatch_single_key(projid, projtype) select projid, projtype from proc_set_perf_comm where compid = @fromcompid declare @current_projtype varchar(10) -- 统计分类(统计), 对应于gdm01.gda13c declare @current_projid varchar(20) -- 疗程代码 -- 创建循环变量 declare @HasRecord int set @HasRecord = 1 declare @Index int set @Index = -1 while(@HasRecord = 1) begin if exists ( select 1 from #proc_set_dispatch_key) begin set @HasRecord = 1 select top 1 @current_projtype = projtype, @current_projid = projid from #proc_set_dispatch_single_key if not exists (select 1 from proc_set_perf_comm where compid = @tocompid and projid = @current_projid and projtype = @current_projtype) begin insert proc_set_perf_comm(compid, projid, projtype, perf_mode, perf_set, comm_mode, comm_set, free_perf_mode, free_perf_set, free_comm_mode, free_comm_set) select @tocompid, projid, projtype, perf_mode, perf_set, comm_mode, comm_set, free_perf_mode, free_perf_set, free_comm_mode, free_comm_set from proc_set_perf_comm where compid = @fromcompid and projid = @current_projid and projtype = @current_projtype end delete from #proc_set_dispatch_single_key where projtype = @current_projtype and projid = @current_projid end else begin set @HasRecord = 0 end end drop table #proc_set_dispatch_key end GO /****** Object: StoredProcedure [dbo].[upg_m_dispatch_proc_set_perf_comm_single] Script Date: 06/12/2017 06:20:22 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_m_dispatch_proc_set_perf_comm_single] ( @fromcompid varchar(10), -- 从哪一个公司发布 @tocompid varchar(10), -- 目的公司 @mandatory varchar(2) -- 是否强制统一 ) as begin if @fromcompid = @tocompid return if @mandatory <> '1' and @mandatory <> '0' return if @mandatory = '1' begin delete proc_set_perf_comm where compid = @tocompid end create table #proc_set_dispatch_single_key -- 疗程资料关键字 ( projid varchar(20) not null, -- 疗程代码 projtype varchar(10) not null -- 统计分类(统计), 对应于gdm01.gda13c ) declare @PK_Sql nvarchar(max) set @PK_Sql = ' alter table #proc_set_dispatch_single_key add constraint PK_#proc_set_dispatch_single_key_' + convert(varchar(10), cast(floor(rand() * 1000000) as int)) + ' primary key clustered(projid, projtype)' exec sp_executesql @PK_Sql insert into #proc_set_dispatch_single_key(projid, projtype) select projid, projtype from proc_set_perf_comm where compid = @fromcompid declare @current_projtype varchar(10) -- 统计分类(统计), 对应于gdm01.gda13c declare @current_projid varchar(20) -- 疗程代码 -- 创建循环变量 declare @HasRecord int set @HasRecord = 1 declare @Index int set @Index = -1 while(@HasRecord = 1) begin if exists ( select 1 from #proc_set_dispatch_single_key) begin set @HasRecord = 1 select top 1 @current_projtype = projtype, @current_projid = projid from #proc_set_dispatch_single_key if not exists (select 1 from proc_set_perf_comm where compid = @tocompid and projid = @current_projid and projtype = @current_projtype) begin insert proc_set_perf_comm(compid, projid, projtype, perf_mode, perf_set, comm_mode, comm_set, free_perf_mode, free_perf_set, free_comm_mode, free_comm_set) select @tocompid, projid, projtype, perf_mode, perf_set, comm_mode, comm_set, free_perf_mode, free_perf_set, free_comm_mode, free_comm_set from proc_set_perf_comm where compid = @fromcompid and projid = @current_projid and projtype = @current_projtype end delete from #proc_set_dispatch_single_key where projtype = @current_projtype and projid = @current_projid end else begin set @HasRecord = 0 end end drop table #proc_set_dispatch_single_key end GO /****** Object: StoredProcedure [dbo].[upg_unclose_glm01_part] Script Date: 06/12/2017 06:20:48 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_unclose_glm01_part] ( @compid varchar(10), @billid varchar(20), @keyid varchar(20) ) as begin update glm01 set gla95c = '', gla96d = '', gla80d = '', gla04t = '', gla20f = 0, gla23i = 0 where gla00c = @compid and gla01c = @billid update glm03 set glc06f = 0, glc05i = 0 where glc00c = @compid and glc01c = @billid if not exists(select 1 from gam06 where gaf00c = @compid and (gaf07c = @billid or gaf01c = @keyid)) begin insert gam06(gaf00c, gaf01c, gaf02c, gaf03c, gaf07c, gaf08i, gaf09c, gaf05c, gaf04c) select gla00c, gla16c, gla07c, gla06c, gla01c, 1, '', gla02d, substring(gla03t, 1, 4) from glm01 with (nolock) where gla00c = @compid and gla01c = @billid end end GO /****** Object: StoredProcedure [dbo].[upg_query_keys] Script Date: 06/12/2017 06:20:30 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_query_keys] ( @compid varchar(10), -- 公司别 @flag int -- 1-未使用,2-正在使用中 ) as begin if @flag = 2 begin select keyno = gaf01c, cardno = gaf03c, billid = gaf07c from gam06 where gaf00c = @compid end else begin declare @i int declare @max_num int select @max_num = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP032' if @max_num = 0 select @max_num = 200 create table #notused_keys( keyno varchar(20)) select @i = 1 while @i<@max_num + 1 begin insert #notused_keys(keyno) values(convert(varchar(20), @i)) select @i = @i + 1 end delete a from #notused_keys a where a.keyno in (select gaf01c from gam06 where gaf00c = @compid) select keyno = keyno, cardno = convert(varchar(20), '未使用'), billid = convert(varchar(20), 0) from #notused_keys drop table #notused_keys end end GO /****** Object: StoredProcedure [dbo].[upg_delete_data] Script Date: 06/12/2017 06:19:58 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_delete_data] ( @compid varchar(10), @from varchar(8), @to varchar(8) ) as begin --设定一下标志是数据删除的开始,这个时候不要去触发那些处理的事务 update gsm01 set gsa02c = 'START' where gsa00c = @compid and gsa01c = 'SP001' delete glm01 where gla00c = @compid and gla02d >= @from and gla02d <= @to delete ggm01 where gga00c = @compid and gga02d >= @from and gga02d <= @to delete gnm01 where gna00c = @compid and gna02d >= @from and gna02d <= @to delete gcm10 where gcl00c = @compid and gcl02d >= @from and gcl02d <= @to --删除收银资料的时候,需要将所有会员赶出去 --不在入院中客户全部赶出去 delete gam06 where gaf00c = @compid and gaf01c not in(select gla16c from glm01 where gla23i = 0 and gla00c = @compid) --设定一下标志是数据删除的结束,这个时候不要去触发那些处理的事务 update gsm01 set gsa02c = 'end' where gsa00c = @compid and gsa01c = 'SP001' end GO /****** Object: StoredProcedure [dbo].[upg_confirm_glm01] Script Date: 06/12/2017 06:19:45 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_confirm_glm01] @compid varchar(15), -- @noteId varchar(20), -- @UserId varchar(10), -- @Date varchar(20), -- @Time varchar(6), -- @confirm int, -- @validate varchar(10) --保留期间的会员卡入院需要修改的有效截止期 as--WUXM, 2004/12/05 begin ----------会员卡-- 与 钥匙牌的关联----------------------------- declare @cardno varchar(20) --判断会员卡的类型,为储值卡时才递减次数,计次卡不递减 select @cardno = gla06c from glm01 with (nolock) where gla00c = @compid and gla01c = @noteId if @confirm = 1 begin -- the master key -- 将主钥匙牌插入glm03 中 -- the correlation key no insert glm03(glc00c, glc01c, glc02c, glc03f, glc04f, glc05i) select gla00c, gla01c, gla16c, 0, 0, 0 from glm01 with (nolock) where gla00c = @compid and gla01c = @noteId -- 建立钥匙牌 与 会员卡的关联 delete a from gam06 a, glm01 b where a.gaf00c = b.gla00c and a.gaf01c = b.gla16c and b.gla00c = @compid and b.gla01c = @noteId insert gam06(gaf00c, gaf01c, gaf02c, gaf03c, gaf04c, gaf05c, gaf06c, gaf07c, gaf08i, gaf09c) select gla00c, glc02c, gla07c, gla06c, substring(gla03t, 1, 4), gla02d, gla21c, gla01c, 1, isnull(gla24c, '') from glm01 with (nolock), glm03 with (nolock) where glc00c = gla00c and glc01c = gla01c and gla00c = @compid and gla01c = @noteId end else -- begin -- 从glm03 中 删除 主钥匙牌 delete c from glm03 c, glm01 a where gla00c = glc00c and gla01c = glc01c and glc02c = gla16c and glc00c = @compid and glc01c = @noteId --取消 钥匙牌 与 会员卡的关联 delete gam06 where gaf00c = @compid and gaf07c = @noteId end ----set the flag of confirm-------------------------- if @confirm = 1 begin update glm01 set gla93c = @UserId, gla94d = @Date, gla03t = @Time where gla00c = @compid and gla01c = @noteId end else begin update glm01 set gla93c = '', gla94d = '' where gla00c = @compid and gla01c = @noteId end end GO /****** Object: StoredProcedure [dbo].[upg_union_keys] Script Date: 06/12/2017 06:20:48 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_union_keys] ( @compid varchar(10), @masterkey varchar(20), @secondkey varchar(20) ) as begin declare @enterno_master varchar(20) declare @enterno_second varchar(20) if @masterkey = @secondkey return -- 取得主Key所对应的入馆单号 select @enterno_master = gaf07c from gam06 where gaf00c = @compid and gaf01c = @masterkey select @enterno_second = gaf07c from gam06 where gaf00c = @compid and gaf01c = @secondkey if isnull(@enterno_master, '') = '' or isnull(@enterno_second, '') = '' or @enterno_master = @enterno_second return update glm03 set glc01c = @enterno_master where glc01c = @enterno_second and glc00c = @compid and glc05i = 0 update glm02 set glb01c = @enterno_master where glb01c = @enterno_second and glb00c = @compid update glm01 set gla23i = 1 where gla00c = @compid and gla01c = @enterno_second update a set a.gla08i = a.gla08i + b.gla08i, a.gla11f = a.gla11f + isnull(b.gla11f, 0) from glm01 a, glm01 b where a.gla00c = b.gla00c and a.gla01c = @enterno_master and b.gla01c = @enterno_second update ggm01 set gga30c = @enterno_master where gga00c = @compid and gga30c = @enterno_second update gam06 set gaf07c = @enterno_master, gaf06c = '并入' + @masterkey where gaf00c = @compid and gaf07c = @enterno_second update glm01 set gla08i = 0 where gla00c = @compid and gla01c = @enterno_second end GO /****** Object: StoredProcedure [dbo].[upg_cancel_union_keys] Script Date: 06/12/2017 06:19:37 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_cancel_union_keys] ( @compid varchar(10), @masterkey varchar(20), @secondkey varchar(20) ) as begin declare @enterno_master varchar(20) declare @enterno_second varchar(20) if @masterkey = @secondkey return -- 取得主Key所对应的入馆单号 select @enterno_master = gaf07c from gam06 where gaf00c = @compid and gaf01c = @masterkey select @enterno_second = gla01c from glm01 with (nolock) where gla00c = @compid and gla08i = 0 and gla16c = @secondkey if isnull(@enterno_master, '') = '' or isnull(@enterno_second, '') = '' or @enterno_master = @enterno_second return update glm03 set glc01c = @enterno_second where glc01c = @enterno_master and glc00c = @compid and glc05i = 0 and glc02c = @secondkey update glm02 set glb01c = @enterno_second where glb01c = @enterno_master and glb00c = @compid and glb04c = @secondkey update glm01 set gla23i = 0, gla08i = 1 where gla00c = @compid and gla01c = @enterno_second --求出这个钥匙牌消费金额 declare @amt float select @amt = glc03f from glm03 where glc00c = @compid and glc01c = @enterno_second and glc02c = @secondkey update a set a.gla08i = a.gla08i - 1, a.gla11f = a.gla11f - @amt from glm01 a where a.gla00c = @compid and a.gla01c = @enterno_master update a set a.gla08i = 1, a.gla11f = @amt from glm01 a where a.gla00c = @compid and a.gla01c = @enterno_second update gam06 set gaf07c = @enterno_second, gaf06c = '' where gaf00c = @compid and gaf07c = @enterno_master and gaf01c = @secondkey update ggm01 set gga30c = @enterno_second where gga00c = @compid and gga08c = @secondkey and gga30c = @enterno_master end GO /****** Object: StoredProcedure [dbo].[upg_change_card_when_in] Script Date: 06/12/2017 06:19:39 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_change_card_when_in] ( @compid varchar(10), @enterno varchar(20), @cardid varchar(20), @ret int output ) as begin declare @memid varchar(20) declare @cardtype varchar(20) declare @rate float declare @remain float, @remain_1 float, @remain_3 float, @remain_4 float, @remain_5 float -- 判断这个卡是否已经在gam06等待结帐,如果是,则退出 if exists(select 1 from gam06 where gaf01c = @cardid and gaf07c <> @enterno and gaf00c = @compid) begin select @ret = -1 return end else begin if not exists(select 1 from gam06 where gaf07c = @enterno and gaf00c = @compid) begin -- 本入院单已经出院结帐 select @ret = -2 return end else begin select @ret = 1 end end select @memid = gba01c from gbm01 where gba00c = @compid and gba23c = @cardid select @remain = gcc06f from gcm03 with (nolock) where gcc00c = @compid and gcc01c = @cardid and gcc03i = 2 select @remain_1 = gcc06f from gcm03 with (nolock) where gcc00c = @compid and gcc01c = @cardid and gcc03i = 1 select @remain_3 = gcc06f from gcm03 with (nolock) where gcc00c = @compid and gcc01c = @cardid and gcc03i = 3 select @remain_4 = gcc06f from gcm03 with (nolock) where gcc00c = @compid and gcc01c = @cardid and gcc03i = 4 select @remain_5 = gcc06f from gcm03 with (nolock) where gcc00c = @compid and gcc01c = @cardid and gcc03i = 5 update glm01 set gla06c = @cardid, gla16c = @cardid, gla27c = @memid, gla29f = @remain_1, gla30f = @remain, gla31f = @remain_3, gla32f = @remain_4, gla33f = @remain_5 where gla00c = @compid and gla01c = @enterno update glm02 set glb04c = @cardid where glb00c = @compid and glb01c = @enterno -- 如果原来有三个钥匙牌号,这样更新就重复了 declare @gcl03f_amt float select @gcl03f_amt = sum(isnull(glc03f, 0)) from glm03 where glc00c = @compid and glc01c = @enterno delete glm03 where glc00c = @compid and glc01c = @enterno insert glm03(glc00c, glc01c, glc02c, glc03f, glc04f, glc05i, glc06f) values(@compid, @enterno, @cardid, @gcl03f_amt, 0, 0, 0) delete gam06 where gaf00c = @compid and gaf07c = @enterno declare @curdate varchar(20), @curtime varchar(20) exec upg_getcurrdate @curdate output, @curtime output insert gam06(gaf00c, gaf01c, gaf03c, gaf07c, gaf04c, gaf05c) values(@compid, @cardid, @cardid, @enterno, substring(@curtime, 1, 4), @curdate) end GO /****** Object: StoredProcedure [dbo].[upg_create_card_relation] Script Date: 06/12/2017 06:19:50 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_create_card_relation]( @compid varchar(10), --公司编号, @cardid varchar(20)) as begin declare @row_cnt int -- 行数 declare @itemid varchar(20) -- 项目编号 declare @pricetype int -- 价格序号 declare @discount float -- 折扣 declare @proc_id int -- 疗程序号 declare @memo varchar(60) --备注 declare @open_proc_id int -- 开账序号 declare @remain_times float --只要把没有开账序号的而且是前期卡的 declare cur_each_gcm06_prior cursor for select gcf02c, gcf17i, gcf18f, gcf23i, gcf15c, gcf07f from gcm06 where gcf00c = @compid and gcf01c = @cardid and isnull(gcf20i, 0) = 1 -- 前期卡 and isnull(gcf24i, 0) = 0 -- 没有开账序号的 for update of gcf24i open cur_each_gcm06_prior fetch cur_each_gcm06_prior into @itemid, @pricetype, @discount, @proc_id, @memo, @remain_times while @@fetch_status = 0 begin --判断在开账资料中符合条件的是否只有一条,如果只有1条,则容易处理 select @row_cnt = 0 select @row_cnt = count(*) from gcm16 where gcr00c = @compid and gcr01c = @cardid and gcr02c = @itemid and gcr17i = @pricetype and gcr18f = @discount if @row_cnt = 1 begin --设置gcm16中的疗程序号 update a set a.gcr37i = @proc_id from gcm16 a where gcr00c = @compid and gcr01c = @cardid and gcr02c = @itemid and gcr17i = @pricetype and gcr18f = @discount and isnull(gcr37i, 0) = 0 select @open_proc_id = gcr23i from gcm16 where gcr00c = @compid and gcr01c = @cardid and gcr02c = @itemid and gcr17i = @pricetype and gcr18f = @discount update gcm06 set gcf24i = @open_proc_id where current of cur_each_gcm06_prior end else if @row_cnt>1 begin --如果超过了2条可能的开账记录 --寻找最符合条件的 select @open_proc_id = 0 if isnull(@memo, '')<>'' begin select @open_proc_id = gcr23i from gcm16 where gcr00c = @compid and gcr01c = @cardid and gcr02c = @itemid and gcr17i = @pricetype and gcr18f = @discount and isnull(gcr15c, '') = @memo and isnull(gcr37i, 0) = 0 end if isnull(@open_proc_id, 0) = 0 begin --寻找次数一样的 select @open_proc_id = min(gcr23i) from gcm16 where gcr00c = @compid and gcr01c = @cardid and gcr02c = @itemid and gcr17i = @pricetype and gcr18f = @discount and isnull(gcr37i, 0) = 0 and isnull(gcr07f, 0) = @remain_times end if isnull(@open_proc_id, 0) = 0 begin --寻找次数超过剩余的 select @open_proc_id = min(gcr23i) from gcm16 where gcr00c = @compid and gcr01c = @cardid and gcr02c = @itemid and gcr17i = @pricetype and gcr18f = @discount and isnull(gcr37i, 0) = 0 and isnull(gcr07f, 0)>@remain_times end if isnull(@open_proc_id, 0) = 0 begin --没有找到合适的,寻找最新的 select @open_proc_id = min(gcr23i) from gcm16 where gcr00c = @compid and gcr01c = @cardid and gcr02c = @itemid and gcr17i = @pricetype and gcr18f = @discount and isnull(gcr37i, 0) = 0 end --设置gcm16中的疗程序号 update a set a.gcr37i = @proc_id from gcm16 a where gcr00c = @compid and gcr01c = @cardid and gcr02c = @itemid and gcr17i = @pricetype and gcr18f = @discount and isnull(gcr37i, 0) = 0 and gcr23i = @open_proc_id update gcm06 set gcf24i = @open_proc_id where current of cur_each_gcm06_prior end fetch cur_each_gcm06_prior into @itemid, @pricetype, @discount, @proc_id, @memo, @remain_times end close cur_each_gcm06_prior deallocate cur_each_gcm06_prior end GO /****** Object: StoredProcedure [dbo].[upg_cal_all_employees_vperf] Script Date: 06/12/2017 06:19:31 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_cal_all_employees_vperf] ( @compid varchar(10), -- 公司别 @cardid varchar(20), -- 会员卡号 @item varchar(20), -- 项目别 @quan float, -- 数量 @standardprice float, -- 标准单价, @price float, -- 实际单价 @amt float, -- 该项目的金额 @ex_rate float, -- 折算营业额比率(很多卡在销售的时候已经赠送了金额) @ex_rate_points float, -- 积分的折算比率 @perf_rate_prj_gam10 float, -- 会员卡类别业绩折算比率 @mode_1 varchar(20), -- 大工类型 @mode_2 varchar(20), -- 中工类型 @mode_3 varchar(20), -- 小工类型 @mode_4 varchar(20), -- 四工类型 @staff_1 varchar(20), -- 大工代码 @staff_2 varchar(20), -- 中工代码 @staff_3 varchar(20), -- 小工代码 @staff_4 varchar(20), -- 四工代码 @vperf_1 float output, -- 大工虚业绩 @vperf_2 float output, -- 中工虚业绩 @vperf_3 float output, -- 小工虚业绩 @vperf_4 float output, -- 四工虚业绩 @paymode varchar(5), -- 支付方式 @p_mode int, -- 积分的计算方式 @p_setvalue float, -- 积分比率与金额 @p_points float output, -- 积分 @fa_type1 varchar(20), -- 方案代码1 @fa_type2 varchar(20), -- 方案代码2 @fa_type3 varchar(20), -- 方案代码3 @fa_type4 varchar(20) -- 方案代码4 ) as begin declare @type int declare @type_11 int, @type_12 int, @type_13 int, @type_14 int, @type_15 int declare @type_21 int, @type_22 int, @type_23 int, @type_24 int, @type_25 int declare @type_31 int, @type_32 int, @type_33 int, @type_34 int, @type_35 int declare @type_41 int, @type_42 int, @type_43 int, @type_44 int, @type_45 int declare @setvalue_11 float, @setvalue_12 float, @setvalue_13 float , @setvalue_14 float , @setvalue_15 float declare @setvalue_21 float, @setvalue_22 float, @setvalue_23 float , @setvalue_24 float , @setvalue_25 float declare @setvalue_31 float, @setvalue_32 float, @setvalue_33 float , @setvalue_34 float , @setvalue_35 float declare @setvalue_41 float, @setvalue_42 float, @setvalue_43 float , @setvalue_44 float , @setvalue_45 float declare @setvalue float declare @person_num int set @person_num = 1 -- 大工 select @type_11 = isnull(hea02i, 2), -- 指定大工方式 @setvalue_11 = isnull(hea03f, 1), -- 指定大工比率 @type_12 = isnull(hea08i, 2), @setvalue_12 = isnull(hea09f, 1), -- 轮班大工 @type_13 = isnull(hea10i, 2), @setvalue_13 = isnull(hea11f, 1), -- 补工大工 @type_14 = isnull(hea35i, 2), @setvalue_14 = isnull(hea36f, 1), -- 内创大工 @type_15 = isnull(hea43i, 2), @setvalue_15 = isnull(hea44f, 1) -- 内创大工 from --modify by lm 2015-08-31 ( select isnull(hea02i, 2) as hea02i, isnull(hea03f, 1) as hea03f, isnull(hea08i, 2) as hea08i, isnull(hea09f, 1) as hea09f, isnull(hea10i, 2) as hea10i, isnull(hea11f, 1) as hea11f, isnull(hea35i, 2) as hea35i, isnull(hea36f, 1) as hea36f, isnull(hea43i, 2) as hea43i, isnull(hea44f, 1) as hea44f from hem01 where hea00c = @compid and hea01c = @item and hea18i = 1 and hea33c = @fa_type1 and isnull(hea34c, '*') = '*' union all select isnull(hea02i, 2) as hea02i, isnull(hea03f, 1) as hea03f, isnull(hea08i, 2) as hea08i, isnull(hea09f, 1) as hea09f, isnull(hea10i, 2) as hea10i, isnull(hea11f, 1) as hea11f, isnull(hea35i, 2) as hea35i, isnull(hea36f, 1) as hea36f, isnull(hea43i, 2) as hea43i, isnull(hea44f, 1) as hea44f from hem01 where hea00c = @compid and hea01c = @item and hea18i = 1 and hea33c = @fa_type1 and hea34c = @paymode ) a -- 中工 select @type_21 = isnull(hea04i, 2), @setvalue_21 = isnull(hea05f, 1), -- 指定中工 @type_22 = isnull(hea14i, 2), @setvalue_22 = isnull(hea15f, 1), -- 轮班中工 @type_23 = isnull(hea20i, 2), @setvalue_23 = isnull(hea21f, 1), -- 补工中工 @type_24 = isnull(hea37i, 2), @setvalue_24 = isnull(hea38f, 1), -- 内创中工 @type_25 = isnull(hea45i, 2), @setvalue_25 = isnull(hea46f, 1) -- 内创中工 from -- modify by lm 2015-08-31 ( select isnull(hea04i, 2) as hea04i, isnull(hea05f, 1) as hea05f, isnull(hea14i, 2) as hea14i, isnull(hea15f, 1) as hea15f, isnull(hea20i, 2) as hea20i, isnull(hea21f, 1) as hea21f, isnull(hea37i, 2) as hea37i, isnull(hea38f, 1) as hea38f, isnull(hea45i, 2) as hea45i, isnull(hea46f, 1) as hea46f from hem01 where hea00c = @compid and hea01c = @item and hea18i = 1 and hea33c = @fa_type2 and isnull(hea34c, '*') = '*' union all select isnull(hea04i, 2) as hea04i, isnull(hea05f, 1) as hea05f, isnull(hea14i, 2) as hea14i, isnull(hea15f, 1) as hea15f, isnull(hea20i, 2) as hea20i, isnull(hea21f, 1) as hea21f, isnull(hea37i, 2) as hea37i, isnull(hea38f, 1) as hea38f, isnull(hea45i, 2) as hea45i, isnull(hea46f, 1) as hea46f from hem01 where hea00c = @compid and hea01c = @item and hea18i = 1 and hea33c = @fa_type2 and hea34c = @paymode ) a -- 小工 select @type_31 = isnull(hea06i, 2), @setvalue_31 = isnull(hea07f, 1), -- 指定小工 @type_32 = isnull(hea16i, 2), @setvalue_32 = isnull(hea17f, 1), -- 轮班小工 @type_33 = isnull(hea22i, 2), @setvalue_33 = isnull(hea23f, 1), -- 补工小工 @type_34 = isnull(hea39i, 2), @setvalue_34 = isnull(hea40f, 1), -- 内创小工 @type_35 = isnull(hea47i, 2), @setvalue_35 = isnull(hea48f, 1) -- 内创小工 from -- modify by lm 2015-08-31 ( select isnull(hea06i, 2) as hea06i, isnull(hea07f, 1) as hea07f, isnull(hea16i, 2) as hea16i, isnull(hea17f, 1) as hea17f, isnull(hea22i, 2) as hea22i, isnull(hea23f, 1) as hea23f, isnull(hea39i, 2) as hea39i, isnull(hea40f, 1) as hea40f, isnull(hea47i, 2) as hea47i, isnull(hea48f, 1) as hea48f from hem01 where hea00c = @compid and hea01c = @item and hea18i = 1 and hea33c = @fa_type3 and isnull(hea34c, '*') = '*' union all select isnull(hea06i, 2) as hea06i, isnull(hea07f, 1) as hea07f, isnull(hea16i, 2) as hea16i, isnull(hea17f, 1) as hea17f, isnull(hea22i, 2) as hea22i, isnull(hea23f, 1) as hea23f, isnull(hea39i, 2) as hea39i, isnull(hea40f, 1) as hea40f, isnull(hea47i, 2) as hea47i, isnull(hea48f, 1) as hea48f from hem01 where hea00c = @compid and hea01c = @item and hea18i = 1 and hea33c = @fa_type3 and hea34c = @paymode ) a -- 4工 select @type_41 = isnull(hea25i, 2), @setvalue_41 = isnull(hea26f, 1), -- 指定4工 @type_42 = isnull(hea27i, 2), @setvalue_42 = isnull(hea28f, 1), -- 轮班4工 @type_44 = isnull(hea41i, 2), @setvalue_44 = isnull(hea42f, 1), -- 内创4工 @type_45 = isnull(hea49i, 2), @setvalue_45 = isnull(hea50f, 1) -- 内创4工 from --modify by lm 2015-08-31 ( select isnull(hea25i, 2) as hea25i, isnull(hea26f, 1) as hea26f, isnull(hea27i, 2) as hea27i, isnull(hea28f, 1) as hea28f, isnull(hea41i, 2) as hea41i, isnull(hea42f, 1) as hea42f, isnull(hea49i, 2) as hea49i, isnull(hea50f, 1) as hea50f from hem01 where hea00c = @compid and hea01c = @item and hea18i = 1 and hea33c = @fa_type4 and isnull(hea34c, '*') = '*' union all select isnull(hea25i, 2) as hea25i, isnull(hea26f, 1) as hea26f, isnull(hea27i, 2) as hea27i, isnull(hea28f, 1) as hea28f, isnull(hea41i, 2) as hea41i, isnull(hea42f, 1) as hea42f, isnull(hea49i, 2) as hea49i, isnull(hea50f, 1) as hea50f from hem01 where hea00c = @compid and hea01c = @item and hea18i = 1 and hea33c = @fa_type4 and hea34c = @paymode ) a -- 套餐 if @paymode = '#' begin -- 套餐项目的核算金额 declare @combo_hs_amt float select @type_11 = isnull(gtb06i, @type_11), @setvalue_11 = isnull(gtb07f, @setvalue_11), @type_12 = isnull(gtb08i, @type_12), @setvalue_12 = isnull(gtb09f, @setvalue_12), @type_21 = isnull(gtb10i, @type_21), @setvalue_21 = isnull(gtb11f, @setvalue_21), @type_22 = isnull(gtb12i, @type_22), @setvalue_22 = isnull(gtb13f, @setvalue_22), @type_31 = isnull(gtb14i, @type_31), @setvalue_31 = isnull(gtb15f, @setvalue_31), @type_32 = isnull(gtb16i, @type_32), @setvalue_32 = isnull(gtb17f, @setvalue_32), @type_41 = isnull(gtb18i, @type_41), @setvalue_41 = isnull(gtb19f, @setvalue_41), @type_42 = isnull(gtb20i, @type_42), @setvalue_42 = isnull(gtb21f, @setvalue_42), @combo_hs_amt = isnull(gtb04f, 0) from gtc02 a, gtc04 b where a.gtb01c = b.gte02c and a.gtb02c = b.gte03c and a.gtb02c = @item and b.gte01c = @cardid and a.gtb00c = @compid -- 如果是套餐,则营业额必然为0,所以就直接取核算金额 --select @amt = isnull(@combo_hs_amt, 0) -- xyq add 20160907 套餐支付的时候,把使用核算金额来计算积分的方式改为用实际支付额 ggm02 ggb11f select @amt = isnull(@amt, 0) end -- 计算四工 if isnull(@staff_4, '') <> '' begin if @mode_4 = '1' select @type = @type_41, @setvalue = @setvalue_41 else if @mode_4 = '2' select @type = @type_42, @setvalue = @setvalue_42 else if @mode_4 = '14' select @type = @type_44, @setvalue = @setvalue_44 else if @mode_4 = '15' select @type = @type_45, @setvalue = @setvalue_45 else select @type = @type_43, @setvalue = @setvalue_43 if (@type = '99') begin set @person_num = @person_num + 1 end exec upg_cal_vperf @type, @setvalue, @quan, @standardprice, @price, @amt, @ex_rate, 0, 0, 0, @vperf_4 output set @vperf_4 = isnull(@vperf_4, 0) * isnull(@perf_rate_prj_gam10, 0) end else begin select @vperf_4 = 0 end -- 计算小工 if isnull(@staff_3, '') <> '' begin if @mode_3 = '1' select @type = @type_31, @setvalue = @setvalue_31 else if @mode_3 = '2' select @type = @type_32, @setvalue = @setvalue_32 else if @mode_3 = '14' select @type = @type_34, @setvalue = @setvalue_34 else if @mode_3 = '15' select @type = @type_35, @setvalue = @setvalue_35 else select @type = @type_33, @setvalue = @setvalue_33 if (@type = '99') begin set @person_num = @person_num + 1 end exec upg_cal_vperf @type, @setvalue, @quan, @standardprice, @price, @amt, @ex_rate, 0, 0, @vperf_4, @vperf_3 output set @vperf_3 = isnull(@vperf_3, 0) * isnull(@perf_rate_prj_gam10, 0) end else begin select @vperf_3 = 0 end -- 计算中工 if isnull(@staff_2, '') <> '' begin if @mode_2 = '1' select @type = @type_21, @setvalue = @setvalue_21 else if @mode_2 = '2' select @type = @type_22, @setvalue = @setvalue_22 else if @mode_2 = '14' select @type = @type_24, @setvalue = @setvalue_24 else if @mode_2 = '15' select @type = @type_25, @setvalue = @setvalue_25 else select @type = @type_23, @setvalue = @setvalue_23 if (@type = '99') begin set @person_num = @person_num + 1 end exec upg_cal_vperf @type, @setvalue, @quan, @standardprice, @price, @amt, @ex_rate, 0, @vperf_3, @vperf_4, @vperf_2 output set @vperf_2 = isnull(@vperf_2, 0) * isnull(@perf_rate_prj_gam10, 0) end else begin select @vperf_2 = 0 end -- 计算大工 if isnull(@staff_1, '') <> '' begin if @mode_1 = '1' select @type = @type_11, @setvalue = @setvalue_11 else if @mode_1 = '2' select @type = @type_12, @setvalue = @setvalue_12 else if @mode_1 = '14' select @type = @type_14, @setvalue = @setvalue_14 else if @mode_1 = '15' select @type = @type_15, @setvalue = @setvalue_15 else select @type = @type_13, @setvalue = @setvalue_13 exec upg_cal_vperf @type, @setvalue, @quan, @standardprice, @price, @amt, @ex_rate, @vperf_2, @vperf_3, @vperf_4, @vperf_1 output set @vperf_1 = isnull(@vperf_1, 0) * isnull(@perf_rate_prj_gam10, 0) end else begin select @vperf_1 = 0 end if isnull(@p_mode, 0) = 0 select @p_points = 0 else if @p_mode = 1 select @p_points = @p_setvalue * @quan * @ex_rate_points else if @p_mode = 2 select @p_points = @amt * @p_setvalue * @ex_rate_points else if @p_mode = 5 select @p_points = @standardprice * @quan * @p_setvalue * @ex_rate_points else if @p_mode = 9 select @p_points = @amt * @ex_rate * @p_setvalue * @ex_rate_points -- 如果是设置平分的业绩 declare @before_vperf_1 float -- 没有分之前大工的业绩 select @before_vperf_1 = isnull(@vperf_1, 0) -- 四工的平分业绩 if isnull(@staff_4, '') <> '' and (@type_41 = '99' or @type_42 = '99' or @type_44 = '99' or @type_45 = '99') begin select @vperf_4 = isnull(@before_vperf_1, 0) / @person_num select @vperf_1 = isnull(@vperf_1, 0) - isnull(@vperf_4, 0) end -- 小工的平分业绩 if isnull(@staff_3, '') <> '' and (@type_31 = '99' or @type_32 = '99' or @type_33 = '99' or @type_34 = '99' or @type_35 = '99') begin select @vperf_3 = isnull(@before_vperf_1, 0) / @person_num select @vperf_1 = isnull(@vperf_1, 0) - isnull(@vperf_3, 0) end -- 中工的平分业绩 if isnull(@staff_2, '') <> '' and (@type_21 = '99' or @type_22 = '99' or @type_23 = '99' or @type_24 = '99' or @type_25 = '99') begin select @vperf_2 = isnull(@before_vperf_1, 0) / @person_num select @vperf_1 = isnull(@vperf_1, 0) - isnull(@vperf_2, 0) end end GO /****** Object: StoredProcedure [dbo].[upg_give_card_combo] Script Date: 06/12/2017 06:20:11 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_give_card_combo] ( @compid varchar(10), -- 公司别 @billtype varchar(20), -- 类别 @billid varchar(20), -- 单号 @comboid varchar(20), -- 套餐编号 @cardid varchar(20), -- 会员卡号 @confirm int ) as begin -- 根据卡号获取归属公司: declare @ownercompid nvarchar(10) select @ownercompid = min(gca13d) from gcm01 where gca01c = @cardid and gca00c = gca13d create table #prj_buf ( idd varchar(20) not null, -- 项目编号 times float null, -- 总次数 mainidd varchar(20) null, -- 替换项目编号 comboid int null, -- 序号 gti11i int null, --有效期限 gti12c varchar(2) null --期限单位 ) declare @confirmdate varchar(10) insert #prj_buf(idd, times, mainidd, comboid,gti11i,gti12c) select gti04c, gti05f, gti08c, gti10i,gti11i,gti12c from gtc09 where gti00c = @compid and gti01c = @billtype and gti02c = @billid and gti03c = @comboid -- 创建循环变量 declare @HasRecord int set @HasRecord = 1 declare @current_idd varchar(20) declare @current_times float declare @current_mainidd varchar(20) declare @current_comboid int declare @gti11i int declare @gti12c varchar(2) while(@HasRecord = 1) begin if exists(select 1 from #prj_buf) begin set @HasRecord = 1 select top 1 @current_idd = idd, @current_times = times, @current_mainidd = mainidd, @current_comboid = comboid, @gti11i = gti11i, @gti12c = gti12c from #prj_buf if @confirm = 1 begin --gn 卖卡 gz 充值 if(@billtype='gn') select @confirmdate = gna02d from gnm01 where gna00c = @compid and gna01c = @billid else if (@billtype='gz' ) select @confirmdate = gcl02d from gcm10 where gcl00c = @compid and gcl01c = @billid if isnull(@current_comboid, -1) <= 0 begin insert gtc04(gte00c, gte01c, gte02c, gte03c, gte04f, gte05f, gte06f, gte08c, gte10c, gte11c,gte13i,gte14c,gte15d) select @ownercompid, @cardid, @comboid, @current_idd, @current_times, 0, @current_times, @current_mainidd, @billtype, @billid,@gti11i ,@gti12c , @confirmdate --套餐是否严格控制有效时间范围 0否,1是 declare @sp217 varchar(2) select @sp217 = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP217' if( @sp217 = 1) begin update a set a.gte16c = b.gtb24c, --有效起始日期 a.gte17c = b.gtb25c --有效起始日期 from gtc04 a with(nolock), gtc02 b where a.gte00c = b.gtb00c and a.gte02c = b.gtb01c and a.gte03c = b.gtb02c and a.gte00c = @ownercompid and a.gte01c = @cardid and a.gte02c = @comboid and a.gte03c = @current_idd end set @current_comboid = @@identity update a set a.gti10i = @current_comboid from gtc09 a where a.gti00c = @compid and a.gti01c = @billtype and a.gti02c = @billid and a.gti03c = @comboid and a.gti04c = @current_idd end else begin update a set a.gte04f = isnull(a.gte04f, 0) + @current_times, a.gte06f = isnull(a.gte06f, 0) + @current_times, a. gte13i = @gti11i, --有效期限 a. gte14c = @gti12c, --期限单位 a.gte15d = @confirmdate --最后更新日期 from gtc04 a where a.gte00c = @ownercompid and a.gte01c = @cardid and a.gte02c = @comboid and a.gte03c = @current_idd and a.gte09i = @current_comboid and a.gte10c = @billtype and a.gte11c = @billid end end else begin update a set a.gte04f = isnull(a.gte04f, 0) - @current_times, a.gte06f = isnull(a.gte06f, 0) - @current_times from gtc04 a where a.gte00c = @ownercompid and a.gte01c = @cardid and a.gte02c = @comboid and a.gte03c = @current_idd and a.gte09i = @current_comboid and a.gte10c = @billtype and a.gte11c = @billid end delete #prj_buf where idd = @current_idd end else begin set @HasRecord = 0 end end drop table #prj_buf create table #good_buf ( idd varchar(20) not null, -- 物品编号 times float null, -- 总次数 mainidd varchar(20) null, -- 替换物品编号 comboid int null -- 序号 ) insert #good_buf(idd, times, mainidd, comboid) select gtj04c, gtj05f, gtj08c, gtj10i from gtc10 where gtj00c = @compid and gtj01c = @billtype and gtj02c = @billid and gtj03c = @comboid set @HasRecord = 1 while(@HasRecord = 1) begin if exists(select 1 from #good_buf) begin set @HasRecord = 1 select top 1 @current_idd = idd, @current_times = times, @current_mainidd = mainidd, @current_comboid = comboid from #good_buf if @confirm = 1 begin if isnull(@current_comboid, -1) <= 0 begin insert gtc08(gtj00c, gtj01c, gtj02c, gtj03c, gtj04f, gtj05f, gtj06f, gtj07c, gtj10c, gtj11c) select @ownercompid, @cardid, @comboid, @current_idd, @current_times, 0, @current_times, @current_mainidd, @billtype, @billid set @current_comboid = @@identity update a set a.gtj10i = @current_comboid from gtc10 a where a.gtj00c = @compid and a.gtj01c = @billtype and a.gtj02c = @billid and a.gtj03c = @comboid and a.gtj04c = @current_idd end else begin update a set a.gtj04f = isnull(a.gtj04f, 0) + @current_times, a.gtj06f = isnull(a.gtj06f, 0) + @current_times from gtc08 a where a.gtj00c = @ownercompid and a.gtj01c = @cardid and a.gtj02c = @comboid and a.gtj03c = @current_idd and a.gtj09i = @current_comboid and a.gtj10c = @billtype and a.gtj11c = @billid end end else begin update a set a.gtj04f = isnull(a.gtj04f, 0) - @current_times, a.gtj06f = isnull(a.gtj06f, 0) - @current_times from gtc08 a where a.gtj00c = @ownercompid and a.gtj01c = @cardid and a.gtj02c = @comboid and a.gtj03c = @current_idd and a.gtj09i = @current_comboid and a.gtj10c = @billtype and a.gtj11c = @billid end delete #good_buf where idd = @current_idd end else begin set @HasRecord = 0 end end drop table #good_buf end GO /****** Object: StoredProcedure [dbo].[upg_check_confirm_status] Script Date: 06/12/2017 06:19:40 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_check_confirm_status] ( @compid varchar(10), @billtype varchar(5), @fromdate varchar(10), @todate varchar(10) ) as begin declare @billcnt int select @billcnt = 0 if @billtype = 'gx' begin select @billcnt = count(*) from ggm01 a with (nolock), gam26 z where a.gga00c = z.gaz02c and z.gaz01c = @compid and a.gga02d >= @fromdate and a.gga02d <= @todate and (isnull(a.gga93c, '') = '' or isnull(a.gga94d,'') = '') --isnull(a.gga80d, '') = '') end else if (@billtype = 'gn') begin select @billcnt = count(*) from gnm01 a with (nolock), gam26 z where a.gna00c = z.gaz02c and z.gaz01c = @compid and a.gna02d >= @fromdate and a.gna02d <= @todate and (isnull(a.gna93c, '') = '' or isnull(a.gna94d,'') = '') --isnull(a.gna80d, '') = '') end else if (@billtype = 'gz') begin select @billcnt = count(*) from gcm10 a with (nolock), gam26 z where a.gcl00c = z.gaz02c and z.gaz01c = @compid and a.gcl02d >= @fromdate and a.gcl02d <= @todate and (isnull(a.gcl93c, '') = '' or isnull(a.gcl94d,'') = '') --isnull(a.gcl80d, '') = '') end else if (@billtype = 'gh') begin select @billcnt = count(*) from ghm01 a, gam26 z where a.gha00c = z.gaz02c and z.gaz01c = @compid and a.gha03d >= @fromdate and a.gha03d <= @todate and (isnull(a.gha93c, '') = '' or isnull(a.gha94d, '') = '') end else if (@billtype = 'gi') begin select @billcnt = count(*) from gim01 a, gam26 z where a.gia00c = z.gaz02c and z.gaz01c = @compid and a.gia03d >= @fromdate and a.gia03d <= @todate and (isnull(a.gia93c, '') = '' or isnull(a.gia94d, '') = '') end select @billcnt as cnt end GO /****** Object: StoredProcedure [dbo].[upg_refresh_stock] Script Date: 06/12/2017 06:20:34 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_refresh_stock] ( @compid varchar(10), -- 公司编号 @date varchar(10), -- 日期 @billno varchar(20), -- 销货单号(收银单号,会员卡销售单号,会员账户异动单号) @garage varchar(20), -- 仓库编号 @flag int, -- 是否复核(1.复核,2.取消复核) @type int -- 3-收银时候销售与耗用 4-会员卡销售套餐 5-会员账户管理套餐 ) as--WUXM, 2004/07/10 set nocount on set rowcount 0 set transaction isolation level read uncommitted begin -- 销售是否直接扣库存 declare @reducedirectly_s float select @reducedirectly_s = gsa03f from gsm01 with (nolock) where gsa00c = @compid and gsa01c = 'SP014' -- 消耗是否直接扣库存 declare @reducedirectly_u float select @reducedirectly_u = gsa03f from gsm01 with (nolock) where gsa00c = @compid and gsa01c = 'SP016' declare @use_stockid varchar(10) select @use_stockid = gsa02c from gsm01 with (nolock) where gsa00c = @compid and gsa01c = 'SP045' select @use_stockid = isnull(@use_stockid, '01') declare @sale_stockid varchar(10) select @sale_stockid = gsa02c from gsm01 with (nolock) where gsa00c = @compid and gsa01c = 'SP044' select @sale_stockid = isnull(@sale_stockid, '01') declare @has_ggm03 int declare @has_ggm05 int declare @has_gtc05 int declare @has_gtc06 int if (@type = 4 or @type = 5) begin create table #gfm04_buf -- 库存日异动统计明细档(所有异动单据明细 update) ( gfd00c varchar(10) not null, -- 公司别 gfd01c varchar(2) not null, -- 异动别 (1-入库, 2-出库, 3-销货, 4-耗用 5-卡销售套餐 6-充值套餐) gfd02c varchar(20) not null, -- 异动单号 gfd03f int identity not null, -- 输入流水号 gfd04c varchar(30) not null, -- 产品代号 gfd05f float null, -- 标准单位数量 gfd06f float null, -- 标准单位单价 gfd07c varchar(8) null, -- 异动日期 gfd08c varchar(4) null, -- 仓库代号 gfd09f float null, -- 税额 (delete) gfd10d varchar(8) null, -- 生产日期 gfd11c varchar(8) null, -- 入库或出库单位 gfd12f float null, -- 入库单位或出库单位数量 gfd13f float null, -- 金额 gfd14i int null, -- 类型 1 员工 2 门店 gfd15c varchar(20) null -- 代码(员工代码或门店代码) ) --新增的聚集索引和 非聚集索引 create clustered index idx_gfm04_buf on #gfm04_buf(gfd00c,gfd01c,gfd02c,gfd04c) create nonclustered index idx_gfm04_buf_01 on #gfm04_buf(gfd03f) end if (@type = 3) begin -- 收银时候销售产品 if (@reducedirectly_s = 1) begin if @flag = 1 begin -- modify by lyj for 伊美娜 2008-04-09因为现在产品有几种领取方式所以这个这地方应该加判断 -- 其中isnull中的9 没有任何意义只是作为isnull判断的 if exists(select 1 from ggm03 with (nolock) where ggc00c = @compid and ggc01c = @billno and (isnull(ggc36i, 9) = 0 or isnull(ggc36i, 9) = 1) and ggc27c <> '#') select @has_ggm03 = 1 else select @has_ggm03 = 0 -- 新增库存日异动的表头档 -- modify by lyj 对于表头档的仓库为系统参数设置的仓库 if (@has_ggm03 = 1) begin delete from gfm03 where gfc00c = @compid and gfc01c = '3' and gfc02c = @billno delete from gfm04 where gfd00c = @compid and gfd01c = '3' and gfd02c = @billno insert gfm03(gfc00c, gfc01c, gfc02c, gfc03c, gfc040c, gfc041c, gfc05c, gfc06i, gfc08i, gfc07c, gfc09t) select gga00c, '3', gga01c, gga02d, '3', @sale_stockid, @sale_stockid, 1, 1, gga93c, gga04t from ggm01 with (nolock) where gga00c = @compid and gga01c = @billno -- 新增库存日异动的明细档 -- modify by lyj 对于明细档的要具体的仓库了 insert gfm04(gfd00c, gfd01c, gfd02c, gfd03f, gfd04c, gfd05f, gfd06f, gfd07c, gfd08c, gfd11c, gfd12f, gfd13f, gfd14i, gfd15c) select ggc00c, '3', ggc01c, ggc02f, ggc03c, isnull(ggc37f, 0), isnull(ggc38f, 0), @date, ggc31c = case when isnull(ggc31c, '') <> '' then ggc31c else @sale_stockid end, ggc04c, isnull(ggc05f, 0), isnull(ggc11f, 0), 1, ggc12c from ggm03 with (nolock) where ggc00c = @compid and ggc01c = @billno and (isnull(ggc36i, 9) = 0 or isnull(ggc36i, 9) = 1) -- 带走,寄存扣库存,但欠货不扣库存 and ggc27c <> '#' -- modify by lyj for 伊美娜 2008-04-09因为现在产品有几种领取方式所以这个这地方应该加判断 -- 其中isnull中的9 没有任何意义只是作为isnull判断的 end end else begin delete from gfm03 where gfc00c = @compid and gfc01c = '3' and gfc02c = @billno delete from gfm04 where gfd00c = @compid and gfd01c = '3' and gfd02c = @billno end end if @reducedirectly_u = 1 begin -- 扣除消耗 if @flag = 1 -- 复核 begin if exists(select 1 from ggm05 with (nolock) where gge00c = @compid and gge01c = @billno and isnull(gge18i, 0) <> 1) select @has_ggm05 = 1 else select @has_ggm05 = 0 -- 新增库存日异动的表头档 -- modify by lyj 对于表头档的仓库为系统参数设置的仓库 if (@has_ggm05 = 1) begin delete from gfm03 where gfc00c = @compid and gfc01c = '4' and gfc02c = @billno delete from gfm04 where gfd00c = @compid and gfd01c = '4' and gfd02c = @billno insert gfm03(gfc00c, gfc01c, gfc02c, gfc03c, gfc040c, gfc041c, gfc05c, gfc06i, gfc08i, gfc07c, gfc09t) select gga00c, '4', gga01c, gga02d, '3', @use_stockid, @use_stockid, 1, 1, gga93c, gga04t from ggm01 with (nolock) where gga00c = @compid and gga01c = @billno -- 新增库存日异动的明细档 -- modify by lyj 对于明细档的要具体的仓库了 insert gfm04(gfd00c, gfd01c, gfd02c, gfd03f, gfd04c, gfd05f, gfd06f, gfd07c, gfd08c, gfd11c, gfd12f, gfd13f) select gge00c, '4', gge01c, gge02f, gge03c, isnull(gge05f / isnull(case when isnull(gfa09f, 0) = 0 then 1 else gfa09f end, 1), 0), isnull(gge10f, 0), @date, gge15c = case when isnull(gge15c, '') <> '' then gge15c else @use_stockid end, gge04c, isnull(gge05f, 0), 0 from ggm05 with (nolock), gfm01 with (nolock) where gge00c = @compid and gge01c = @billno and gge00c = gfa00c and gge03c = gfa01c and isnull(gge18i, 0) <> 1 end end else begin -- 取消复核 delete from gfm03 where gfc00c = @compid and gfc01c = '4' and gfc02c = @billno delete from gfm04 where gfd00c = @compid and gfd01c = '4' and gfd02c = @billno end end end else if (@type = 4 and @reducedirectly_s = 1) begin if @flag = 1 -- 复核 begin -- 会员卡销售的时候的套餐的产品出货 if exists(select 1 from gtc05 a with (nolock), gtc03 b with (nolock) where a.gtf00c = @compid and gtf01c = @billno and a.gtf00c = b.gtd00c and a.gtf02c = b.gtd01c) select @has_gtc05 = 1 else select @has_gtc05 = 0 -- 新增库存日异动的表头档 if (@has_gtc05 = 1) begin if not exists(select 1 from gfm03 with (nolock) where gfc00c = @compid and gfc01c = '6' and gfc02c = @billno) begin insert gfm03(gfc00c, gfc01c, gfc02c, gfc03c, gfc040c, gfc041c, gfc05c, gfc06i, gfc08i, gfc07c, gfc09t) select gna00c, '5', gna01c, gna02d, '3', @use_stockid, @use_stockid, 1, 1, gna93c, gna12t from gnm01 with (nolock) where gna00c = @compid and gna01c = @billno end -- 新增库存日异动的明细档 -- 出库的金额就是产品占套餐的金额比率*套餐的金额,必须先计算出比率 insert #gfm04_buf(gfd00c, gfd01c, gfd02c, gfd04c, gfd05f, gfd06f, gfd07c, gfd08c, gfd11c, gfd12f, gfd13f, gfd14i, gfd15c) select gtf00c, '5', gtf01c, gtj04c, gtj05f, dbo.fun_divide(gtj09f * gtf05f, gtj05f), @date, @use_stockid, gfa07c, gtj05f, gtj09f * gtf05f, 1, gna20c from gtc05 a with (nolock), gtc10 b with (nolock), gfm01 c with (nolock), gnm01 d with (nolock) where a.gtf00c = @compid and a.gtf01c = @billno and a.gtf00c = b.gtj00c and a.gtf01c = b.gtj02c -- 单号 and a.gtf02c = b.gtj03c --套餐编号 and b.gtj01c = 'gn' -- 类型 and b.gtj00c = c.gfa00c and b.gtj04c = c.gfa01c -- 产品编号 and a.gtf00c = d.gna00c and a.gtf01c = d.gna01c -- 客户可能购买多个套餐,产品重复,所以必须加到原来的地方 update a set a.gfd05f = a.gfd05f + isnull(b.gfd05f, 0), a.gfd12f = a.gfd12f + isnull(b.gfd12f, 0), a.gfd13f = a.gfd13f + isnull(b.gfd13f, 0) from gfm04 a, #gfm04_buf b where a.gfd00c = b.gfd00c and a.gfd01c = b.gfd01c and a.gfd02c = b.gfd02c and a.gfd03f = b.gfd03f and a.gfd04c = b.gfd04c delete a from #gfm04_buf a, gfm04 b where a.gfd00c = b.gfd00c and a.gfd01c = b.gfd01c and a.gfd02c = b.gfd02c and a.gfd03f = b.gfd03f and a.gfd04c = b.gfd04c insert gfm04(gfd00c, gfd01c, gfd02c, gfd03f, gfd04c, gfd05f, gfd06f, gfd07c, gfd08c, gfd11c, gfd12f, gfd13f, gfd14i, gfd15c) select gfd00c, gfd01c, gfd02c, gfd03f, gfd04c, gfd05f, gfd06f, gfd07c, gfd08c, gfd11c, gfd12f, gfd13f, gfd14i, gfd15c from #gfm04_buf end end else begin -- 取消复核 delete from gfm03 where gfc00c = @compid and gfc01c = '5' and gfc02c = @billno delete from gfm04 where gfd00c = @compid and gfd01c = '5' and gfd02c = @billno end end else if (@type = 5 and @reducedirectly_s = 1) begin if @flag = 1 -- 复核 begin -- 会员账户管理购买套餐的产品出货 if exists(select 1 from gtc06 a with (nolock), gtc03 b with (nolock) where a.gth00c = @compid and gth01c = @billno and a.gth00c = b.gtd00c and a.gth02c = b.gtd01c) select @has_gtc06 = 1 else select @has_gtc06 = 0 -- 新增库存日异动的表头档 if (@has_gtc06 = 1) begin -- 可能客户购买了多个套餐,则gfm03已经存在,不需要再增加了 if not exists(select 1 from gfm03 with (nolock) where gfc00c = @compid and gfc01c = '6' and gfc02c = @billno) begin insert gfm03(gfc00c, gfc01c, gfc02c, gfc03c, gfc040c, gfc041c, gfc05c, gfc06i, gfc08i, gfc07c, gfc09t) select gcl00c, '6', gcl01c, gcl02d, '3', @use_stockid, @use_stockid, 1, 1, gcl93c, gcl56t from gcm10 with (nolock) where gcl00c = @compid and gcl01c = @billno end -- 新增库存日异动的明细档 -- 出库的金额就是产品占套餐的金额比率*套餐的金额,必须先计算出比率 insert #gfm04_buf(gfd00c, gfd01c, gfd02c, gfd04c, gfd05f, gfd06f, gfd07c, gfd08c, gfd11c, gfd12f, gfd13f, gfd14i, gfd15c) select gth00c, '6', gth01c, gtj04c, gtj05f, dbo.fun_divide(gtj09f * gth05f, gtj05f), @date, @use_stockid, gfa07c, gtj05f, gtj09f * gth05f, 1, gcl09c from gtc06 a with (nolock), gtc10 b with (nolock), gfm01 c with (nolock), gcm10 d with (nolock) where a.gth00c = @compid and a.gth01c = @billno and a.gth00c = b.gtj00c --公司 and a.gth01c = b.gtj02c -- 单号 and a.gth02c = b.gtj03c --套餐编号 and b.gtj01c = 'gz' and b.gtj00c = c.gfa00c and b.gtj04c = c.gfa01c and a.gth00c = d.gcl00c and a.gth01c = d.gcl01c -- 客户可能购买多个套餐,产品重复,所以必须加到原来的地方 update a set a.gfd05f = a.gfd05f + isnull(b.gfd05f, 0), a.gfd12f = a.gfd12f + isnull(b.gfd12f, 0), a.gfd13f = a.gfd13f + isnull(b.gfd13f, 0) from gfm04 a, #gfm04_buf b where a.gfd00c = b.gfd00c and a.gfd01c = b.gfd01c and a.gfd02c = b.gfd02c and a.gfd03f = b.gfd03f and a.gfd04c = b.gfd04c delete a from #gfm04_buf a, gfm04 b where a.gfd00c = b.gfd00c and a.gfd01c = b.gfd01c and a.gfd02c = b.gfd02c and a.gfd03f = b.gfd03f and a.gfd04c = b.gfd04c insert gfm04(gfd00c, gfd01c, gfd02c, gfd03f, gfd04c, gfd05f, gfd06f, gfd07c, gfd08c, gfd11c, gfd12f, gfd13f, gfd14i, gfd15c) select gfd00c, gfd01c, gfd02c, gfd03f, gfd04c, gfd05f, gfd06f, gfd07c, gfd08c, gfd11c, gfd12f, gfd13f, gfd14i, gfd15c from #gfm04_buf end end else begin -- 取消复核 delete from gfm03 where gfc00c = @compid and gfc01c = '6' and gfc02c = @billno delete from gfm04 where gfd00c = @compid and gfd01c = '6' and gfd02c = @billno end end if (@type = 4 or @type = 5) begin truncate table #gfm04_buf drop table #gfm04_buf end end GO /****** Object: StoredProcedure [dbo].[upg_delete_bill] Script Date: 06/12/2017 06:19:58 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_delete_bill] ( @compid varchar(10), @billtype varchar(10), @billid varchar(20), @userid varchar(20), @curdate varchar(10), @curtime varchar(10) ) as begin if @billtype = 'gx' begin insert sysoperationlog(userid, program, operation, operationdate, operationtime, origatedate, compid, keyvalue1, keyvalue2, keyvalue3, keyvalue4) values(@userid, 'GIA04I', 'D', @curdate, @curtime, @curdate, @compid, @billid, '', 'upg_delete_bill', '') delete ggm01 where gga00c = @compid and gga01c = @billid end else if @billtype = 'gn' begin insert sysoperationlog(userid, program, operation, operationdate, operationtime, origatedate, compid, keyvalue1, keyvalue2, keyvalue3, keyvalue4) values(@userid, 'GMA01I', 'D', @curdate, @curtime, @curdate, @compid, @billid, '', 'upg_delete_bill', '') delete gnm01 where gna00c = @compid and gna01c = @billid end else if @billtype = 'gz' begin insert sysoperationlog(userid, program, operation, operationdate, operationtime, origatedate, compid, keyvalue1, keyvalue2, keyvalue3, keyvalue4) values(@userid, 'GMC01I', 'D', @curdate, @curtime, @curdate, @compid, @billid, '', 'upg_delete_bill', '') delete gcm10 where gcl00c = @compid and gcl01c = @billid delete gcm08 where gch00c = @compid and gch11c = @billid end else if @billtype = 'ge' begin insert sysoperationlog(userid, program, operation, operationdate, operationtime, origatedate, compid, keyvalue1, keyvalue2, keyvalue3, keyvalue4) values(@userid, 'GMB03I', 'D', @curdate, @curtime, @curdate, @compid, @billid, '', 'upg_delete_bill', '') delete gem01 where gea00c = @compid and gea01c = @billid end else if @billtype = 'gl' begin insert sysoperationlog(userid, program, operation, operationdate, operationtime, origatedate, compid, keyvalue1, keyvalue2, keyvalue3, keyvalue4) values(@userid, 'GMA02I', 'D', @curdate, @curtime, @curdate, @compid, @billid, '', 'upg_delete_bill', '') delete glm01 where gla00c = @compid and gla01c = @billid end end GO /****** Object: StoredProcedure [dbo].[upg_cal_consumeamt_afterfill] Script Date: 06/12/2017 06:19:32 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_cal_consumeamt_afterfill] ( @compid varchar(10), -- 公司编号 @billtype varchar(10), -- 类型 @billid varchar(20), -- 单号 @amttype varchar(20), -- 扣除的金额类型 25-当天消费 26-当天实际营业额 27-低于一个设置的销卡额,实际营业额 @amt float output ) as begin declare @startdate varchar(20) declare @starttime varchar(20) declare @cardid varchar(20) declare @amt_good float set @amt_good = 0 select @amt = 0 if @billtype = 'gn' begin select @startdate = gna02d, @starttime = gna12t, @cardid = gna13c from gnm01 with (nolock), gam26 with (nolock) where gna00c = gaz02c and gna01c = @billid and gaz01c = @compid end else if @billtype = 'gz' begin select @startdate = gcl02d, @starttime = gcl56t, @cardid = gcl03c from gcm10 with (nolock), gam26 with (nolock) where gcl00c = gaz02c and gaz01c = @compid and gcl01c = @billid end if @amttype = '25' begin select @amt = sum(gcd06f) from gcm04 with (nolock) where gcd01c = @cardid and gcd07c = 'gx' and gcd09d = @startdate and gcd13t >= @starttime and (gcd02i = 2 or gcd02i >= 4) end else if @amttype = '26' or @amttype = '27' or @amttype = '28' begin select @amt = isnull(sum(b.ggb11f * b.ggb34f), 0) from ggm01 a with (nolock), ggm02 b with (nolock) where a.gga09c = @cardid and a.gga02d = @startdate and a.gga04t >= @starttime and (b.ggb27c = '4'or b.ggb27c >= '9') and a.gga00c = b.ggb00c and a.gga01c = b.ggb01c select @amt_good = isnull(sum(b.ggc11f * b.ggc30f), 0) from ggm01 a with (nolock), ggm03 b with (nolock) where a.gga09c = @cardid and a.gga02d = @startdate and a.gga04t >= @starttime and (b.ggc27c = '4' or b.ggc27c >= '9') and a.gga00c = b.ggc00c and a.gga01c = b.ggc01c set @amt = @amt + @amt_good end end GO /****** Object: StoredProcedure [dbo].[upg_select_gmb18i] Script Date: 06/12/2017 06:20:39 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create proc [dbo].[upg_select_gmb18i] ( @companyId varchar(60), --门店编号 @memberNo varchar(60), --会员编号 @fromdate varchar(10), --消费时间 @todate varchar(10), --消费时间 @xmdl varchar(30), --项目大类(部门) @tjfl varchar(30), --项目分类(按统计分类) @fwren varchar(30) --服务人 ) as begin --项目消费 select ggb01c, --消费单号 ggb03c, --项目代码 ggb03cName = ( select gda03c from gdm01 where gda00c = ggb00c and gda01c = ggb03c), ggb04c, --单位 ggb05f, --数量 gga94d, --开单时间 ggb08f, --标准单价 ggb09f, --折让 ggb10f, --折让单价 ggb11f, --金额 ggb12c + '-' + (select haa02c from ham01 where haa00c = ggb00c and haa01c = ggb12c) ggb12c, --大工代码 ggb20f, --大工虚业绩 ggb13c + '-' + (select haa02c from ham01 where haa00c = ggb00c and haa01c = ggb13c) ggb13c, --中工代码 ggb21f, --中工虚业绩 ggb14c + '-' + (select haa02c from ham01 where haa00c = ggb00c and haa01c = ggb14c) ggb14c, --小工代码 ggb22f, --小工虚业绩 ggb26c + '-' + (select haa02c from ham01 where haa00c = ggb00c and haa01c = ggb26c) ggb26c, --四工代码 ggb35f, --四工虚业绩 ggm01.memo --备注 from ggm02 with(nolock) inner join ggm01 with(nolock) on ggb00c = gga00c and ggb01c = gga01c inner join gdm01 with(nolock) on gdm01.gda00c = ggm02.ggb00c And gdm01.gda01c = ggm02.ggb03c where gga00c = @companyId and gga05c = @memberNo and (@fromdate = '' or ( gga94d between @fromdate and @todate )) and (@xmdl = '' or gda02c = @xmdl) --(项目大类)部门 and (@tjfl = '' or gda13c in (select F1 from f_splitstr(@tjfl,',')) ) and (@fwren = '' or (ggb12c = @fwren or ggb13c = @fwren or ggb14c = @fwren or ggb26c = @fwren) ) order by ggb01c desc --卡充值 select gcl01c, --异动单号 gcl65f, --异动金额 gcl09c+ '-' + (select haa02c from ham01 where haa00c = gcl00c and haa01c = gcl09c) gcl09c, --销售人员 gcl58f, --第一销售虚业绩 gcl17c+ '-' + (select haa02c from ham01 where haa00c = gcl00c and haa01c = gcl17c) gcl17c, --第二销售 gcl60f, --第二销售虚业绩 gcl19c+ '-' + (select haa02c from ham01 where haa00c = gcl00c and haa01c = gcl19c) gcl19c, --第三负责人 gcl62f, --第三销售虚业绩 gcl22c+ '-' + (select haa02c from ham01 where haa00c = gcl00c and haa01c = gcl22c) gcl22c, --第4负责人 gcl83f, --第四人员 gcl02d --异动日期 from gcm10 with(nolock) where gcl06i = 0 --充值 and gcl04c = '2' --仅包含储值账户 and gcl00c = @companyId and gcl03c = @memberNo and (@fromdate = '' or ( gcl02d between @fromdate and @todate )) order by gcl01c desc --产品消费 select ggc01c, --消费单号 ggc03c, --产品代码 ggc03cName =(select gfa03c from gfm01 where gfa00c = ggc00c and gfa01c = ggc03c), ggc04c, --单位 ggc04cName = (select gsb03c from gsm02 where gsb00c = ggc00c and gsb01c = 'K' and gsb02c = ggc04c ), ggc05f, --数量 ggc08f, --标准单价 ggc09f, --折让 ggc10f, --折让单价 ggc11f, --金额 ggc12c + '-' + (select haa02c from ham01 where haa00c = ggc00c and haa01c = ggc12c) ggc12c, --产品销售人员 ggc21f, --第一销售虚业绩 ggc15c + '-' + (select haa02c from ham01 where haa00c = ggc00c and haa01c = ggc15c) ggc15c, --第二人 ggc23f, --第二销售虚业绩 ggc17c + '-' + (select haa02c from ham01 where haa00c = ggc00c and haa01c = ggc17c) ggc17c, --第三人 ggc25f, --第三销售虚业绩 --ggc36i, --方式 0带走 1寄存 2欠货 gga94d --开单时间 from ggm03 with(nolock) inner join ggm01 on ggc00c = gga00c and ggc01c = gga01c where ggc00c = @companyId and gga05c = @memberNo and (@fromdate = '' or ( gga94d between @fromdate and @todate )) order by ggc01c desc --办卡 select gna01c, --销售单号 gna21c, --发票号码 gna26f, --实收总额 gna23d, --开卡起始日 gna24d, --开卡截止日 --gna09c, --介绍会员 gna20c + '-' + (select haa02c from ham01 where haa00c = gna00c and haa01c = gna20c) gna20c, --负责业务 gna67f, --第一销售虚业绩 gna28c + '-' + (select haa02c from ham01 where haa00c = gna00c and haa01c = gna28c) gna28c, --第二卖卡人 gna69f, --第二销售虚业绩 gna30c + '-' + (select haa02c from ham01 where haa00c = gna00c and haa01c = gna30c) gna30c, --第三卖卡人 gna71f, --第三销售虚业绩 gna32c + '-' + (select haa02c from ham01 where haa00c = gna00c and haa01c = gna32c) gna32c, --第4卖卡人 gna86f, --第四人员业绩 gna02d --销售日期 from gnm01 with(nolock) where gna00c = @companyId and gna04c = @memberNo and (@fromdate = '' or ( gna02d between @fromdate and @todate )) order by gna01c desc end GO /****** Object: StoredProcedure [dbo].[upg_updategnm01_accountdate] Script Date: 06/12/2017 06:20:51 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_updategnm01_accountdate] ( @compid varchar(10), --公司编号 @consumeid varchar(20) --消费单号 ) as --WUXM 2005/08/16 begin declare @begtime varchar(20) --工作日开始时间 declare @endtime varchar(20) --工作日结束时间,表示凌晨时间,如果为000000,则表示帐务日期和实际日期一致 declare @time varchar(6) --实际结帐时间 declare @date varchar(8) --实际日期 declare @staff varchar(20) --登陆人 declare @acctdate varchar(20) -- 账户日期 declare @cardid varchar(20) select @begtime = gsa02c from gsm01 with(nolock) where gsa00c = @compid and gsa01c = 'SP050' select @endtime = gsa02c from gsm01 with(nolock) where gsa00c = @compid and gsa01c = 'SP051' declare @curtime varchar(8) declare @curdate varchar(10) select @curtime = gna12t, @curdate = gna02d, @staff = gna93c, @cardid = gna13c from gnm01 with(nolock) where gna00c = @compid and gna01c = @consumeid select @time = @curtime select @date = @curdate declare @newdate varchar(8) declare @strdate varchar(10) --帐务日期比正常日期短 if (@endtime > @begtime and @time > @endtime) begin select @strdate = convert(varchar(10), dateadd(day, 1, cast(@date as datetime)), 120) select @newdate = substring(@strdate, 1, 4) + substring(@strdate, 6, 2) + substring(@strdate, 9, 2) update a set a.gna80d = @newdate from gnm01 a where gna00c = @compid and gna01c = @consumeid select @acctdate = @newdate end --帐务日期比正常日期长 else if (@endtime <= @begtime and @time <= @endtime) begin select @strdate = convert(varchar(10), dateadd(day, -1, cast(@date as datetime)), 120) select @newdate = substring(@strdate, 1, 4) + substring(@strdate, 6, 2) + substring(@strdate, 9, 2) update a set a.gna80d = @newdate from gnm01 a where gna00c = @compid and gna01c = @consumeid select @acctdate = @newdate end --帐务日期和正常日期一致 else begin update a set a.gna80d = @date from gnm01 a where a.gna00c = @compid and a.gna01c = @consumeid select @acctdate = @date end --更新gsm03新增加的字段 update a set a.gsc10d = @acctdate, a.gsc11c = @staff, gsc08c = @cardid from gsm03 a where gsc00c = @compid and gsc01c = @consumeid and gsc02c = 'gn' end GO /****** Object: StoredProcedure [dbo].[upg_modify_billdate] Script Date: 06/12/2017 06:20:24 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_modify_billdate] ( @compid varchar(10), @billtype varchar(10), @billid varchar(20), @billdate varchar(20), @userid varchar(20), @curdate varchar(10), @curtime varchar(10) ) as begin if @billtype = 'gx' begin insert sysoperationlog(userid, program, operation, operationdate, operationtime, origatedate, compid, keyvalue1, keyvalue2, keyvalue3, keyvalue4) values(@userid, 'GIA04I', 'M', @curdate, @curtime, @curdate, @compid, @billid, @billdate, 'modify_billdate', '') update ggm01 set gga02d = @billdate, gga80d = @billdate, gga94d = @billdate where gga00c = @compid and gga01c = @billid update gcm08 set gch02d = @billdate where gch00c = @compid and gch11c = @billid and gch10c = 'gx' update gtc07 set gti02d = @billdate where gti00c = @compid and gti09c = @billid and gti08c = 'gx' end else if @billtype = 'gn' begin insert sysoperationlog(userid, program, operation, operationdate, operationtime, origatedate, compid, keyvalue1, keyvalue2, keyvalue3, keyvalue4) values(@userid, 'GMA01I', 'M', @curdate, @curtime, @curdate, @compid, @billid, @billdate, 'modify_billdate', '') update gnm01 set gna02d = @billdate, gna80d = @billdate, gna94d = @billdate where gna00c = @compid and gna01c = @billid update gcm08 set gch02d = @billdate where gch00c = @compid and gch11c = @billid and gch10c = 'gn' update gtc07 set gti02d = @billdate where gti00c = @compid and gti09c = @billid and gti08c = 'gn' end else if @billtype = 'gz' begin insert sysoperationlog(userid, program, operation, operationdate, operationtime, origatedate, compid, keyvalue1, keyvalue2, keyvalue3, keyvalue4) values(@userid, 'GMC01I', 'M', @curdate, @curtime, @curdate, @compid, @billid, @billdate, 'modify_billdate', '') update gcm10 set gcl02d = @billdate, gcl80d = @billdate, gcl94d = @billdate where gcl00c = @compid and gcl01c = @billid update gcm08 set gch02d = @billdate where gch00c = @compid and gch11c = @billid and gch10c = 'gz' update gtc07 set gti02d = @billdate where gti00c = @compid and gti09c = @billid and gti08c = 'gz' end else if @billtype = 'ge' begin insert sysoperationlog(userid, program, operation, operationdate, operationtime, origatedate, compid, keyvalue1, keyvalue2, keyvalue3, keyvalue4) values(@userid, 'GMB03I', 'M', @curdate, @curtime, @curdate, @compid, @billid, @billdate, 'modify_billdate', '') update gem01 set gea03d = @billdate, gea80d = @billdate, gea96d = @billdate where gea00c = @compid and gea01c = @billid end else if @billtype = 'gl' begin insert sysoperationlog(userid, program, operation, operationdate, operationtime, origatedate, compid, keyvalue1, keyvalue2, keyvalue3, keyvalue4) values(@userid, 'GMA02I', 'M', @curdate, @curtime, @curdate, @compid, @billid, @billdate, 'modify_billdate', '') update glm01 set gla02d = @billdate, gla80d = @billdate, gla96d = @billdate where gla00c = @compid and gla01c = @billid end update gsm03 set gsc10d = @billdate where gsc02c = @billtype and gsc01c = @billid and gsc00c = @compid end GO /****** Object: StoredProcedure [dbo].[upg_cal_total_sum_consume] Script Date: 06/12/2017 06:19:35 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_cal_total_sum_consume] ( @compid varchar(10), -- 公司别 @cardid varchar(20), -- 会员卡号 @period int, -- 前面多少天 @todate varchar(10), -- 截止哪个日期 @total_sum_consume float output ) as begin declare @judgekind int declare @lastsaledate varchar(20) select @lastsaledate = max(gcb07d) from gcm02 with (nolock) where gcb03i = 11 and gcb01c = @cardid if (isnull(@lastsaledate, '') = '') begin select @lastsaledate = max(gna02d)from gnm01 with (nolock) where gna13c = @cardid and gna00c = @compid end -- 判断最后一次升级到现在是否在2月之内 if (datediff(day, @lastsaledate, @todate)<@period) select @judgekind = 1 else select @judgekind = 2 if @judgekind = 1 -- 按累极设定的月份内 begin select @total_sum_consume = sum(isnull(gsc05f, 0)) from gsm03 a with (nolock) where a.gsc00c = @compid and a.gsc08c = @cardid and (gsc02c = 'gx') and datediff(day, gsc10d, @todate)<@period and gsc10d <= @todate and (gsc04c = '1' or gsc04c = '6') end else if @judgekind = 2 --按当月计算 begin select @total_sum_consume = sum(isnull(gsc05f, 0)) from gsm03 a with (nolock) where a.gsc00c = @compid and a.gsc08c = @cardid and (gsc02c = 'gx') and substring(gsc10d, 1, 6) = substring(@todate, 1, 6) and gsc10d <= @todate and (gsc04c = '1' or gsc04c = '6') end end GO /****** Object: StoredProcedure [dbo].[upg_cal_total_card_sale] Script Date: 06/12/2017 06:19:35 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_cal_total_card_sale] ( @compid varchar(10), -- 公司别 @cardid varchar(20), -- 会员卡号 @period int, -- 天 @todate varchar(10), -- 截止哪个日期 @total_sale_card float output ) as begin declare @judgekind int declare @lastsaledate varchar(20) --取最后一次升级的日期 select @lastsaledate = max(gcl02d) from gcm10 with (nolock) where gcl14i = 1 and gcl03c = @cardid and gcl00c = @compid if (isnull(@lastsaledate, '') = '') begin select @lastsaledate = max(gna02d)from gnm01 with (nolock) where gna13c = @cardid and gna00c = @compid end if isnull( @lastsaledate, '') = '' begin select @lastsaledate = '20080701' end --判断最后一次升级到现在是否在2月之内 if (datediff(day, @lastsaledate, @todate) >= @period) select @judgekind = 1 --两个月之内都没有升级过了 else select @judgekind = 2 --已经升级过了,则判断当月 if @judgekind = 1 -- 没有升级过,按设定的累计 begin select @total_sale_card = sum(isnull(gsc05f, 0)) from gsm03 a with (nolock) where a.gsc00c = @compid and a.gsc08c = @cardid and (gsc02c = 'gn' or gsc02c = 'gz') and datediff(day, gsc10d, @todate)<@period and gsc10d <= @todate and (gsc04c = '1' or gsc04c = '6' or gsc04c = '&') end else if @judgekind = 2 --按当月计算 begin select @total_sale_card = sum(isnull(gsc05f, 0)) from gsm03 a with (nolock) where a.gsc00c = @compid and a.gsc08c = @cardid and (gsc02c = 'gn' or gsc02c = 'gz') and substring(gsc10d, 1, 6) = substring(@todate, 1, 6) and gsc10d <= @todate and (gsc04c = '1' or gsc04c = '6' or gsc04c = '&') end end GO /****** Object: StoredProcedure [dbo].[upg_copy_accountdate] Script Date: 06/12/2017 06:19:46 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_copy_accountdate] as --WUXM 2005/08/28 begin -- 会员账户异动 update a set a.gcl80d = gcl02d from gcm10 a where isnull(gcl80d, '') = '' and isnull(gcl94d, '') <> '' update a set a.gsc10d = b.gcl80d, a.gsc11c = b.gcl93c, a.gsc12i = b.gcl06i from gsm03 a, gcm10 b where a.gsc00c = b.gcl00c and a.gsc01c = b.gcl01c and a.gsc02c = 'gz' and isnull(a.gsc10d, '') = '' and isnull(b.gcl80d, '') <> '' -- 收银 update a set a.gga80d = gga02d from ggm01 a where isnull(gga80d, '') = '' and isnull(gga94d, '') <> '' update a set a.gsc10d = b.gga80d, a.gsc11c = b.gga93c from gsm03 a, ggm01 b where a.gsc00c = b.gga00c and a.gsc01c = b.gga01c and a.gsc02c = 'gx' and isnull(a.gsc10d, '') = '' and isnull(b.gga80d, '')<>'' --会员卡销售 update a set a.gna80d = gna02d from gnm01 a where isnull(gna80d, '') = '' and isnull(gna94d, '')<>'' update a set a.gsc10d = b.gna80d, a.gsc11c = b.gna93c from gsm03 a, gnm01 b where a.gsc00c = b.gna00c and a.gsc01c = b.gna01c and a.gsc02c = 'gn' and isnull(a.gsc10d, '') = '' and isnull(b.gna80d, '')<>'' --会籍管理 update a set a.gea80d = gea03d from gem01 a where isnull(gea80d, '') = '' and isnull(gea94d, '')<>'' update a set a.gsc10d = b.gea80d, a.gsc11c = b.gea93c from gsm03 a, gem01 b where a.gsc00c = b.gea00c and a.gsc01c = b.gea01c and a.gsc02c = 'ge' and isnull(a.gsc10d, '') = '' and isnull(b.gea80d, '')<>'' --费用登记 update a set a.gqa80d = gqa02d from gqm01 a where isnull(gqa80d, '') = '' and isnull(gqa96d, '')<>'' --会员入院 update a set a.gla80d = gla02d from glm01 a where isnull(gla80d, '') = '' and isnull(gla96d, '')<>'' update a set a.gsc10d = b.gla80d, a.gsc11c = b.gla95c from gsm03 a, glm01 b where a.gsc00c = b.gla00c and a.gsc01c = b.gla01c and a.gsc02c = 'gl' and isnull(a.gsc10d, '') = '' and isnull(b.gla80d, '')<>'' end GO /****** Object: StoredProcedure [dbo].[upg_confirm_unconfirm_bills] Script Date: 06/12/2017 06:19:45 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_confirm_unconfirm_bills](@compid varchar(10)) as begin update ggm01 set gga94d = gga02d, gga80d = gga02d, gga93c = 'ADMIN' where isnull(gga94d, '') = '' and gga00c = @compid update gcm10 set gcl94d = gcl02d, gcl80d = gcl02d, gcl93c = 'ADMIN' where isnull(gcl94d, '') = '' and gcl00c = @compid update gnm01 set gna94d = gna02d, gna80d = gna02d, gna93c = 'ADMIN' where isnull(gna94d, '') = '' and gna00c = @compid end GO /****** Object: StoredProcedure [dbo].[upg_reset_project] Script Date: 06/12/2017 06:20:37 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_reset_project] ( @compid varchar(10), @cardno varchar(20) ) as begin declare @SP055 varchar(5) --支付方式 单据支付还是项目支付 declare @SP057 varchar(5) --疗程放在什么账户中 declare @SP031 varchar(5) --收银结帐 多账单统一出院 declare @payway varchar(5) ---项目的支付方式,用什么账户支付的 select @SP031 = cast(gsa03f as varchar(5)) from gsm01 where gsa01c = 'SP031' select @SP055 = cast(gsa03f as varchar(5)) from gsm01 where gsa01c = 'SP055' select @SP057 = gsa02c from gsm01 where gsa01c = 'SP057' select @payway = '9' delete gcm08 where gch03c = @cardno --买卡的时候没有没有购买疗程 insert gcm08(gch00c, gch02d, gch03c, gch04c, gch05f, gch06f, gch07f, gch08f, gch09f, gch10c, gch11c, gch12i, gch13f, gch14f, gch15f, gch17c, gch18f, gch19c, gch20f, gch21c, gch22f, gch27c, gch28f, gch16i) select gna00c, gna80d, gnb03c, gnc02c, isnull(gnc03f, 0), isnull(gnc04f, 0), isnull(gnc05f, 0), 0, 0, 'gn', gna01c, gnc09i, gnc10f, gnc11f, gnc05f/(case when isnull(gnc03f, 1) + isnull(gnc04f, 0) = 0 then 1 else isnull(gnc03f, 1) + isnull(gnc04f, 0) end), gnc15c, gnc16f, gnc17c, gnc18f, gnc19c, gnc20f, gnc23c, gnc24f, gnc25i from gnm01 with (nolock), gnm02 with (nolock), gnm03 with (nolock) where gna00c = gnc00c and gna01c = gnc01c and gna00c = gnb00c and gna01c = gnb01c and gnb03c = @cardno and isnull(gna80d, '')<>'' --异动的时候 insert gcm08(gch00c, gch02d, gch03c, gch04c, gch05f, gch06f, gch07f, gch08f, gch09f, gch10c, gch11c, gch12i, gch13f, gch14f, gch15f, gch17c, gch18f, gch19c, gch20f, gch21c, gch22f, gch27c, gch28f, gch16i) select gcl00c, gcl80d, gcl03c, gcm02c, gcm03f, gcm04f, gcm05f, 0, 0, 'gz', gcl01c, gcm09i, gcm10f, gcm11f, gcm05f/(case when isnull(gcm03f, 1) + isnull(gcm04f, 0) = 0 then 1 else isnull(gcm03f, 1) + isnull(gcm04f, 0) end), gcm15c, gcm16f, gcm17c, gcm18f, gcm19c, gcm20f, gcm23c, gcm24f, gcm25i from gcm10 with (nolock), gcm11 with (nolock) where gcl00c = gcm00c and gcm01c = gcl01c and gcl03c = @cardno and isnull(gcl80d, '')<>'' and gcl06i = 0 --取款 insert gcm08(gch00c, gch02d, gch03c, gch04c, gch05f, gch06f, gch07f, gch08f, gch09f, gch10c, gch11c, gch12i, gch13f, gch14f, gch15f, gch17c, gch18f, gch19c, gch20f, gch21c, gch22f, gch27c, gch28f, gch16i) select gcl00c, gcl80d, gcl03c, gcm02c, 0-gcm03f, 0-gcm04f, 0-gcm05f, 0, 0, 'gz', gcl01c, gcm09i, gcm10f, 0-gcm11f, gcm05f/(case when isnull(gcm03f, 1) + isnull(gcm04f, 0) = 0 then 1 else isnull(gcm03f, 1) + isnull(gcm04f, 0) end), gcm15c, gcm16f, gcm17c, gcm18f, gcm19c, gcm20f, gcm23c, gcm24f, gcm25i from gcm10 with (nolock), gcm11 with (nolock) where gcl00c = gcm00c and gcm01c = gcl01c and gcl03c = @cardno and isnull(gcl80d, '')<>'' and gcl06i = 1 --处理换疗程的情况 insert gcm08(gch00c, gch02d, gch03c, gch04c, gch05f, gch06f, gch07f, gch08f, gch09f, gch10c, gch11c, gch12i, gch13f, gch14f, gch15f, gch16i, gch17c, gch18f) select hei00c, hei03d, hei02c, hei04c, 0, 0, 0, hei06f, hei07f, 'pj', hei01c, hei11i, hei12f, 0, hei07f/(case when isnull(hei06f, 1) = 0 then 0 else isnull(hei06f, 1) end), hei13i, '9', hei07f from hem08 with (nolock) where hei02c = @cardno ---收银的时候 if (@SP055 = '0')--项目支付的时候 begin --当时用的哪个疗程支付应该被记录下来,否则无法重复生成, debug insert gcm08(gch00c, gch02d, gch03c, gch04c, gch05f, gch06f, gch07f, gch08f, gch09f, gch10c, gch11c, gch12i, gch13f, gch15f, gch17c, gch18f, gch16i) select gga00c, gga80d, gga08c, ggb03c, 0, 0, 0, ggb05f, ggb10f, 'gx', gga01c, ggb45i, ggb46f, ggb11f/(case when isnull(ggb05f, 1) = 0 then 1 else isnull(ggb05f, 1) end), ggb27c, ggb11f, ggb47i from ggm01 with (nolock), ggm02 with (nolock) where gga00c = ggb00c and gga01c = ggb01c and gga08c = @cardno and ggb27c = @payway and isnull(gga80d, '')<>'' end if (@SP055 = '1') --支付明细中只要有用疗程支付,那么就需要转换 begin if (@SP031 = '1')--收银结帐 begin insert gcm08(gch00c, gch02d, gch03c, gch04c, gch05f, gch06f, gch07f, gch08f, gch09f, gch10c, gch11c, gch16i) select distinct gga00c, gga80d, gga08c, ggb03c, 0, 0, 0, ggb05f, ggb11f, 'gx', gga01c, ggb47i from ggm01 with (nolock), ggm02 with (nolock), gsm03 with (nolock), gcm06 with (nolock) where gga00c = ggb00c and gga01c = ggb01c and gga00c = gsc00c and gga01c = gsc01c and gga00c = gcf00c and gga08c = gcf01c and ggb03c = gcf02c and gsc02c = 'gx' and gsc04c = @payway and gga08c = @cardno and isnull(gga80d, '')<>'' end else if (@SP031 = '2')--多张单统一出院 begin insert gcm08(gch00c, gch02d, gch03c, gch04c, gch05f, gch06f, gch07f, gch08f, gch09f, gch10c, gch11c, gch16i) select distinct gla00c, gla80d, gla06c, ggb03c, 0, 0, 0, ggb05f, ggb11f, 'gl', gla01c, ggb47i from ggm01 with (nolock), ggm02 with (nolock), gsm03 with (nolock), gcm06 with (nolock), glm01 with (nolock) where gga00c = ggb00c and gga01c = ggb01c and gga00c = gla00c and gga30c = gla01c and gsc01c = gga00c and gsc01c = gla01c and gsc02c = 'gl' and gga00c = gcf00c and gla06c = gcf01c and ggb03c = gcf02c and gsc04c = @payway and gla06c = @cardno and isnull(gla80d, '')<>'' end end end GO /****** Object: StoredProcedure [dbo].[upg_modify_comp] Script Date: 06/12/2017 06:20:24 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_modify_comp] ( @oldcomp varchar(10), @newcomp varchar(10) ) as begin update glm01 set gla00c = @newcomp where gla00c = @oldcomp update ggm01 set gga00c = @newcomp where gga00c = @oldcomp update ggm02 set ggb00c = @newcomp where ggb00c = @oldcomp update ggm03 set ggc00c = @newcomp where ggc00c = @oldcomp update gsm03 set gsc00c = @newcomp where gsc00c = @oldcomp update gnm01 set gna00c = @newcomp where gna00c = @oldcomp update gnm02 set gnb00c = @newcomp where gnb00c = @oldcomp update gnm03 set gnc00c = @newcomp where gnc00c = @oldcomp update gcm10 set gcl00c = @newcomp where gcl00c = @oldcomp end GO /****** Object: StoredProcedure [dbo].[upg_data_recover_gtc09_gtc10] Script Date: 06/12/2017 06:19:55 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_data_recover_gtc09_gtc10] as begin -- 创建临时表 create table #data_recover_comp ( compId varchar(10) not null ) insert into #data_recover_comp(compId) select gae01c from gam05 with (nolock) -- 创建循环变量 declare @HasRecord int set @HasRecord = 1 declare @currentCompId varchar(10) while(@HasRecord = 1) begin if exists (select 1 from #data_recover_comp) begin set @HasRecord = 1 select top 1 @currentCompId = compId from #data_recover_comp update a set a.gti10i = b.gti14i from gtc09 a, gtc07 b, gnm01 c where a.gti01c = b.gti08c and a.gti01c = 'gn' and a.gti00c = c.gna00c and a.gti02c = c.gna01c and b.gti03c = c.gna13c and a.gti00c = b.gti00c and a.gti02c = b.gti09c and a.gti03c = b.gti04c and a.gti04c = b.gti05c and b.gti12i = 1 and isnull(a.gti10i, -1) = -1 update a set a.gti10i = b.gte09i from gtc09 a, (select gte01c, gte02c, gte03c, gte10c, gte11c, max(gte09i) as gte09i from gtc04 group by gte01c, gte02c, gte03c, gte10c, gte11c) b, gnm01 c where a.gti01c = b.gte10c and a.gti02c = b.gte11c and a.gti01c = 'gn' and a.gti00c = c.gna00c and a.gti02c = c.gna01c and b.gte01c = c.gna13c and a.gti03c = b.gte02c and a.gti04c = b.gte03c and isnull(a.gti10i, -1) = -1 update a set a.gti10i = b.gti14i from gtc09 a, gtc07 b, gcm10 c where a.gti01c = b.gti08c and a.gti01c = 'gz' and a.gti00c = c.gcl00c and a.gti00c = b.gti00c and a.gti02c = c.gcl01c and b.gti03c = c.gcl03c and a.gti02c = b.gti09c and a.gti03c = b.gti04c and a.gti04c = b.gti05c and b.gti12i = 1 and c.gcl06i = 0 and isnull(a.gti10i, -1) = -1 update a set a.gti10i = b.gte09i from gtc09 a, (select gte01c, gte02c, gte03c, gte10c, gte11c, max(gte09i) as gte09i from gtc04 group by gte01c, gte02c, gte03c, gte10c, gte11c) b, gcm10 c where a.gti01c = b.gte10c and a.gti02c = b.gte11c and a.gti01c = 'gz' and a.gti00c = c.gcl00c and a.gti02c = c.gcl01c and b.gte01c = c.gcl03c and a.gti03c = b.gte02c and a.gti04c = b.gte03c and c.gcl06i = 0 and isnull(a.gti10i, -1) = -1 update a set a.gtj10i = b.gti14i from gtc10 a, gtc07 b, gnm01 c where a.gtj01c = b.gti08c and a.gtj01c = 'gn' and a.gtj00c = c.gna00c and a.gtj02c = c.gna01c and b.gti03c = c.gna13c and a.gtj00c = b.gti00c and a.gtj02c = b.gti09c and a.gtj03c = b.gti04c and a.gtj04c = b.gti05c and b.gti12i = 2 and isnull(a.gtj10i, -1) = -1 update a set a.gtj10i = b.gtj09i from gtc10 a, (select gtj01c, gtj02c, gtj03c, gtj10c, gtj11c, max(gtj09i) as gtj09i from gtc08 group by gtj01c, gtj02c, gtj03c, gtj10c, gtj11c) b, gnm01 c where a.gtj01c = b.gtj10c and a.gtj02c = b.gtj11c and a.gtj01c = 'gn' and a.gtj00c = c.gna00c and a.gtj02c = c.gna01c and b.gtj01c = c.gna13c and a.gtj03c = b.gtj02c and a.gtj04c = b.gtj03c and isnull(a.gtj10i, -1) = -1 update a set a.gtj10i = b.gti14i from gtc10 a, gtc07 b, gcm10 c where a.gtj01c = b.gti08c and a.gtj01c = 'gz' and a.gtj00c = c.gcl00c and a.gtj00c = b.gti00c and a.gtj02c = c.gcl01c and b.gti03c = c.gcl03c and a.gtj02c = b.gti09c and a.gtj03c = b.gti04c and a.gtj04c = b.gti05c and b.gti12i = 2 and c.gcl06i = 0 and isnull(a.gtj10i, -1) = -1 update a set a.gtj10i = b.gtj09i from gtc10 a, (select gtj01c, gtj02c, gtj03c, gtj10c, gtj11c, max(gtj09i) as gtj09i from gtc08 group by gtj01c, gtj02c, gtj03c, gtj10c, gtj11c) b, gcm10 c where a.gtj01c = b.gtj10c and a.gtj02c = b.gtj11c and a.gtj01c = 'gz' and a.gtj00c = c.gcl00c and a.gtj02c = c.gcl01c and b.gtj01c = c.gcl03c and a.gtj03c = b.gtj02c and a.gtj04c = b.gtj03c and c.gcl06i = 0 and isnull(a.gtj10i, -1) = -1 -- 清除已处理的门店 delete #data_recover_comp where compId = @currentCompId end else begin set @HasRecord = 0 end end drop table #data_recover_comp end GO /****** Object: StoredProcedure [dbo].[upg_delete_unconfirm_bills] Script Date: 06/12/2017 06:20:00 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_delete_unconfirm_bills](@compid varchar(10)) as begin delete a from ggm01 a, gam26 z where a.gga00c = z.gaz02c and z.gaz01c = @compid and (isnull(gga94d, '') = '' or isnull(a.gga93c, '') = '') --isnull(gga80d, '') = '') delete a from gcm10 a, gam26 z where a.gcl00c = z.gaz02c and z.gaz01c = @compid and (isnull(gcl94d, '') = '' or isnull(a.gcl93c, '') = '') --isnull(gcl80d, '') = '') delete a from gnm01 a, gam26 z where a.gna00c = z.gaz02c and z.gaz01c = @compid and (isnull(gna94d, '') = '' or isnull(a.gna93c,'') = '') --isnull(gna80d, '') = '') end GO /****** Object: StoredProcedure [dbo].[upg_membercard_unite] Script Date: 06/12/2017 06:20:24 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_membercard_unite] ( @compid varchar(10), -- 公司别 @datefrom varchar(8), -- 开始时间 @dateto varchar(8) -- 结束日期 ) as --WUXM 2005/09/05 begin create table #cardsale ( idd int identity not null, billno varchar(20) null, -- 消费单号 date varchar(8) null, -- 消费日期 card varchar(30) null, -- 会员卡号 type varchar(20) null, -- 消费类别 operator varchar(10) null, -- 操作人 money float null, -- 签单进额 remark varchar(400) null, -- 签单备注 billdate varchar(20) null, -- 单据日期 primary key(idd) ) declare @billno varchar(20) -- 消费单号 declare @card varchar(20) -- 会员卡号 insert #cardsale(billno, date, type, operator, money, remark, billdate) select distinct gna01c, gna80d, '会员卡销售', gna91c, gsc05f, gsc06c, gna02d from gnm01 with(nolock), gnm02 with(nolock), gsm03 with(nolock) where gna00c = gnb00c and gna01c = gnb01c and gna00c = gsc00c and gna01c = gsc01c and gsc04c = '8' and gna00c = @compid and gna80d >= @datefrom and gna80d <= @dateto and gsc02c = 'gn' declare cur_member_unite cursor for select distinct gna01c from gnm01 with(nolock), gnm02 with(nolock), gsm03 with(nolock) where gna00c = gnb00c and gna01c = gnb01c and gna00c = gsc00c and gna01c = gsc01c and gsc04c = '8' and gna00c = @compid and gna80d >= @datefrom and gna80d <= @dateto and gsc02c = 'gn' open cur_member_unite fetch next from cur_member_unite into @billno while @@fetch_status = 0 begin select @card = gnb03c from gnm02 with(nolock) where gnb00c = @compid and gnb01c = @billno if @@rowcount = 1 begin update a set a.card = @card from #cardsale a where billno = @billno end else begin declare cur_card_no cursor for select gnb03c from gnm02 with(nolock) where gnb00c = @compid and gnb01c = @billno declare @cardno varchar(30) select @cardno = '' open cur_card_no fetch next from cur_card_no into @card while @@fetch_status = 0 begin select @cardno = @cardno + ',' + @card fetch next from cur_card_no into @card end update a set a.card = substring(@cardno, 2, len(@cardno)) from #cardsale a where billno = @billno close cur_card_no deallocate cur_card_no end fetch next from cur_member_unite into @billno end close cur_member_unite deallocate cur_member_unite select billno, date, card, type, operator, money, remark, billdate from #cardsale drop table #cardsale end GO /****** Object: StoredProcedure [dbo].[upg_set_confirm_state] Script Date: 06/12/2017 06:20:40 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_set_confirm_state] ( @compid varchar(10), @billtype varchar(10), @billid varchar(20), @confirmer varchar(20), @confirmdate varchar(10), @cardid varchar(20) ) as begin if @billtype = 'gn' begin update gnm01 set gna93c = @confirmer, gna94d = @confirmdate, gna13c = @cardid where gna00c = @compid and gna01c = @billid --直接销售开卡的情况下设置会员编号 declare @autoopen float select @autoopen = gsa03f from gsm01 with(nolock) where gsa00c = @compid and gsa01c = 'SP036' if @autoopen = 1 begin update gnm01 set gna04c = @cardid where gna00c = @compid and gna01c = @billid and isnull(gna04c, '') = '' end end else if @billtype = 'gz' begin update gcm10 set gcl93c = @confirmer, gcl94d = @confirmdate where gcl00c = @compid and gcl01c = @billid end end GO /****** Object: StoredProcedure [dbo].[upg_correct_memberid] Script Date: 06/12/2017 06:19:49 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_correct_memberid] ( @cardid varchar(20) -- 卡号 ) as begin if not exists(select 1 from gcm01 with (nolock) where gca01c = @cardid and gca00c = gca13d ) return declare @memid varchar(20) select @memid = isnull(gca04c, '') from gcm01 with (nolock) where gca00c = gca13d and gca01c = @cardid if (@memid <> '' and @memid <> @cardid) begin update gcm01 set gca04c = @cardid where gca01c = @cardid end else if (@memid = '') begin select @memid = @cardid update gcm01 set gca04c = @cardid where gca01c = @cardid end if not exists(select 1 from gbm01 with(nolock) where gba01c = @cardid) begin update gbm01 set gba01c = @cardid, gba23c = @cardid where gba23c = @cardid or gba23c = @memid or gba01c = @cardid or gba23c = @memid update gbm09 set gbi01c = @cardid where gbi01c = @memid update gnm01 set gna04c = @cardid where gna13c = @cardid -- 会员卡销售中可能记录了 这个会员编号 end else begin update gbm01 set gba23c = @cardid where gba01c = @cardid if (@memid <> @cardid) begin update gbm01 set gba23c = '' where gba01c = @memid end end update gbm15 set gbl01c = @cardid where gbl01c = @memid update gbm17 set gbw01c = @cardid where gbw01c = @memid end GO /****** Object: StoredProcedure [dbo].[upg_ymn_divide_bill] Script Date: 06/12/2017 06:20:52 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_ymn_divide_bill]( @compid varchar(10), -- 公司别 @fromdate varchar(10), @todate varchar(10)) as begin declare @billtype varchar(10) delete a from gcm23 a, gam26 z where a.gcw00c = z.gaz02c and z.gaz01c = @compid and gcw03d >= @fromdate and gcw03d <= @todate create table #prjtype_sum( compid varchar(10) not null, billid varchar(20) not null, prjtype varchar(20) not null, amt float null, num float null) select @billtype = 'gn' begin insert gcm23(gcw00c, gcw01c, gcw02c, gcw03d, gcw04c, gcw05c, gcw07c, gcw08f, gcw09c, gcw10f, gcw11c, gcw12f, gcw99i) select gna00c, @billtype, gna01c, gna80d, gna13c, gna04c, gna20c, gna27f, gna28c, gna29f, gna30c, gna31f, gna99i from gnm01 a with (nolock), gam26 z where gna00c = z.gaz02c and z.gaz01c = @compid and gna80d >= @fromdate and gna80d <= @todate --更新会员卡类别 update a set a.gcw06c = b.gnb02c from gcm23 a, gnm02 b, gam26 z where a.gcw00c = b.gnb00c and a.gcw01c = @billtype and a.gcw02c = b.gnb01c and a.gcw04c = b.gnb03c and gcw03d >= @fromdate and gcw03d <= @todate and a.gcw00c = z.gaz02c and z.gaz01c = @compid --更新可能为空的会员编号 update a set a.gcw05c = b.gca04c from gcm23 a, gcm01 b, gam26 z where a.gcw00c = b.gca00c and a.gcw04c = b.gca01c and a.gcw01c = @billtype and gcw03d >= @fromdate and gcw03d <= @todate and isnull(gcw05c, '') = '' and a.gcw00c = z.gaz02c and z.gaz01c = @compid --更新到项目类别中的业绩 --美容 insert #prjtype_sum(compid, billid, prjtype, amt, num) select gnc00c, gnc01c, 'mr', sum(gnc05f), sum(gnc03f) from gnm03 c with (nolock), gnm01 a with (nolock), gdm01 b, gam26 z where c.gnc00c = a.gna00c and c.gnc01c = a.gna01c and a.gna80d >= @fromdate and a.gna80d <= @todate and c.gnc02c = b.gda01c and c.gnc00c = b.gda00c and (gda13c = '233') and gnc00c = z.gaz02c and z.gaz01c = @compid group by gnc00c, gnc01c update a set a.gcw13f = b.amt, a.gcw14f = b.num from gcm23 a, #prjtype_sum b where a.gcw01c = @billtype and a.gcw00c = b.compid and a.gcw02c = b.billid and a.gcw03d >= @fromdate and a.gcw03d <= @todate --仪器 delete #prjtype_sum insert #prjtype_sum(compid, billid, prjtype, amt, num) select gnc00c, gnc01c, 'yq', sum(gnc05f), sum(gnc03f) from gnm03 c with (nolock), gnm01 a with (nolock), gdm01 b, gam26 z where c.gnc00c = a.gna00c and c.gnc01c = a.gna01c and a.gna80d >= @fromdate and a.gna80d <= @todate and c.gnc02c = b.gda01c and c.gnc00c = b.gda00c and (gda13c = '231' or gda13c = '232' or gda13c = '234') and gnc00c = z.gaz02c and z.gaz01c = @compid group by gnc00c, gnc01c update a set a.gcw17f = b.amt, a.gcw18f = b.num from gcm23 a, #prjtype_sum b where a.gcw01c = @billtype and a.gcw00c = b.compid and a.gcw02c = b.billid and a.gcw03d >= @fromdate and a.gcw03d <= @todate --美体 delete #prjtype_sum insert #prjtype_sum(compid, billid, prjtype, amt, num) select gnc00c, gnc01c, 'mt', sum(gnc05f), sum(gnc03f) from gnm03 c with (nolock), gnm01 a with (nolock), gdm01 b, gam26 z where c.gnc00c = a.gna00c and c.gnc01c = a.gna01c and a.gna80d >= @fromdate and a.gna80d <= @todate and c.gnc02c = b.gda01c and c.gnc00c = b.gda00c and (gda13c = '228' or gda13c = '229' or gda13c = '230') and gnc00c = z.gaz02c and z.gaz01c = @compid group by gnc00c, gnc01c update a set a.gcw15f = b.amt, a.gcw16f = b.num from gcm23 a, #prjtype_sum b where a.gcw01c = @billtype and a.gcw00c = b.compid and a.gcw02c = b.billid and a.gcw03d >= @fromdate and a.gcw03d <= @todate end select @billtype = 'gz' begin insert gcm23(gcw00c, gcw01c, gcw02c, gcw03d, gcw04c, gcw05c, gcw07c, gcw08f, gcw09c, gcw10f, gcw11c, gcw12f, gcw06c, gcw99i) select gcl00c, @billtype, gcl01c, gcl80d, gcl03c, '', gcl09c, gcl16f, gcl17c, gcl18f, gcl19c, gcl20f, gcl52c, gcl99i from gcm10 a with (nolock), gam26 z where gcl00c = z.gaz02c and z.gaz01c = @compid and gcl80d >= @fromdate and gcl80d <= @todate --更新会员号 update a set a.gcw05c = b.gca04c from gcm23 a, gcm01 b, gam26 z where a.gcw00c = b.gca00c and a.gcw04c = b.gca01c and a.gcw01c = @billtype and gcw03d >= @fromdate and gcw03d <= @todate and a.gcw00c = z.gaz02c and z.gaz01c = @compid --更新可能出现的会员卡类别为空的情况 update a set a.gcw06c = b.gca02c from gcm23 a, gcm01 b, gam26 z where a.gcw00c = b.gca00c and a.gcw04c = b.gca01c and a.gcw01c = @billtype and gcw03d >= @fromdate and gcw03d <= @todate and a.gcw00c = z.gaz02c and z.gaz01c = @compid and isnull(gcw06c, '') = '' --更新到项目类别中的业绩 --美容 delete #prjtype_sum insert #prjtype_sum(compid, billid, prjtype, amt, num) select gcm00c, gcm01c, 'mr', sum(gcm05f), sum(gcm03f) from gcm11 c, gcm10 a with (nolock), gdm01 b, gam26 z where c.gcm00c = a.gcl00c and c.gcm01c = a.gcl01c and a.gcl80d >= @fromdate and a.gcl80d <= @todate and c.gcm02c = b.gda01c and c.gcm00c = b.gda00c and (gda13c = '233') and gcm00c = z.gaz02c and z.gaz01c = @compid group by gcm00c, gcm01c update a set a.gcw13f = b.amt, a.gcw14f = b.num from gcm23 a, #prjtype_sum b where a.gcw01c = @billtype and a.gcw00c = b.compid and a.gcw02c = b.billid and a.gcw03d >= @fromdate and a.gcw03d <= @todate --仪器 delete #prjtype_sum insert #prjtype_sum(compid, billid, prjtype, amt, num) select gcm00c, gcm01c, 'yq', sum(gcm05f), sum(gcm03f) from gcm11 c with (nolock), gcm10 a with (nolock), gdm01 b, gam26 z where c.gcm00c = a.gcl00c and c.gcm01c = a.gcl01c and a.gcl80d >= @fromdate and a.gcl80d <= @todate and c.gcm02c = b.gda01c and c.gcm00c = b.gda00c and (gda13c = '231' or gda13c = '232' or gda13c = '234') and gcm00c = z.gaz02c and z.gaz01c = @compid group by gcm00c, gcm01c update a set a.gcw17f = b.amt, a.gcw18f = b.num from gcm23 a, #prjtype_sum b where a.gcw01c = @billtype and a.gcw00c = b.compid and a.gcw02c = b.billid and a.gcw03d >= @fromdate and a.gcw03d <= @todate --美体 delete #prjtype_sum insert #prjtype_sum(compid, billid, prjtype, amt, num) select gcm00c, gcm01c, 'mt', sum(gcm05f), sum(gcm03f) from gcm11 c with (nolock), gcm10 a with (nolock), gdm01 b, gam26 z where c.gcm00c = a.gcl00c and c.gcm01c = a.gcl01c and a.gcl80d >= @fromdate and a.gcl80d <= @todate and c.gcm02c = b.gda01c and c.gcm00c = b.gda00c and (gda13c = '228' or gda13c = '229' or gda13c = '230') and gcm00c = z.gaz02c and z.gaz01c = @compid group by gcm00c, gcm01c update a set a.gcw15f = b.amt, a.gcw16f = b.num from gcm23 a, #prjtype_sum b where a.gcw01c = @billtype and a.gcw00c = b.compid and a.gcw02c = b.billid and a.gcw03d >= @fromdate and a.gcw03d <= @todate end drop table #prjtype_sum end GO /****** Object: StoredProcedure [dbo].[upg_reset_project_datascope] Script Date: 06/12/2017 06:20:37 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_reset_project_datascope] ( @compid varchar(10), --考虑倒历史的正确性,必须所有公司 @fromdate varchar(10), @todate varchar(10) ) as begin declare @SP055 varchar(5) -- 支付方式 单据支付还是项目支付 declare @SP057 varchar(5) -- 疗程放在什么账户中 declare @SP031 varchar(5) -- 收银结帐 多账单统一出院 declare @payway varchar(5) -- 项目的支付方式,用什么账户支付的 select @SP031 = cast(gsa03f as varchar(5)) from gsm01 where gsa01c = 'SP031' select @SP055 = cast(gsa03f as varchar(5)) from gsm01 where gsa01c = 'SP055' select @SP057 = gsa02c from gsm01 where gsa01c = 'SP057' select @payway = '9' delete a from gcm08 a where a.gch02d >= @fromdate and a.gch02d <= @todate -- 买卡的时候没有没有购买疗程 insert gcm08(gch00c, gch02d, gch03c, gch04c, gch05f, gch06f, gch07f, gch08f, gch09f, gch10c, gch11c, gch12i, gch13f, gch14f, gch15f, gch17c, gch18f, gch19c, gch20f, gch21c, gch22f, gch27c, gch28f, gch16i) select gna00c, gna80d, gnb03c, gnc02c, isnull(gnc03f, 0), isnull(gnc04f, 0), isnull(gnc05f, 0), 0, 0, 'gn', gna01c, gnc09i, gnc10f, gnc11f, gnc05f/(case when isnull(gnc03f, 1) + isnull(gnc04f, 0) = 0 then 1 else isnull(gnc03f, 1) + isnull(gnc04f, 0) end), gnc15c, gnc16f, gnc17c, gnc18f, gnc19c, gnc20f, gnc23c, gnc24f, gnc25i from gnm01 with (nolock), gnm02 with (nolock), gnm03 with (nolock) where gna00c = gnc00c and gna01c = gnc01c and gna00c = gnb00c and gna01c = gnb01c and gna80d >= @fromdate and gna80d <= @todate and isnull(gnb03c, '') <> '' -- 异动的时候 insert gcm08(gch00c, gch02d, gch03c, gch04c, gch05f, gch06f, gch07f, gch08f, gch09f, gch10c, gch11c, gch12i, gch13f, gch14f, gch15f, gch17c, gch18f, gch19c, gch20f, gch21c, gch22f, gch27c, gch28f, gch16i) select gcl00c, gcl80d, gcl03c, gcm02c, gcm03f, gcm04f, gcm05f, 0, 0, 'gz', gcl01c, gcm09i, gcm10f, gcm11f, gcm05f/(case when isnull(gcm03f, 1) + isnull(gcm04f, 0) = 0 then 1 else isnull(gcm03f, 1) + isnull(gcm04f, 0) end), gcm15c, gcm16f, gcm17c, gcm18f, gcm19c, gcm20f, gcm23c, gcm24f, gcm25i from gcm10 with (nolock), gcm11 with (nolock) where gcl00c = gcm00c and gcm01c = gcl01c and gcl80d >= @fromdate and gcl80d <= @todate and gcl06i = 0 and isnull(gcl03c, '') <> '' -- 取款 insert gcm08(gch00c, gch02d, gch03c, gch04c, gch05f, gch06f, gch07f, gch08f, gch09f, gch10c, gch11c, gch12i, gch13f, gch14f, gch15f, gch17c, gch18f, gch19c, gch20f, gch21c, gch22f, gch27c, gch28f, gch16i) select gcl00c, gcl80d, gcl03c, gcm02c, 0-gcm03f, 0-gcm04f, 0-gcm05f, 0, 0, 'gz', gcl01c, gcm09i, gcm10f, 0-gcm11f, gcm05f/(case when isnull(gcm03f, 1) + isnull(gcm04f, 0) = 0 then 1 else isnull(gcm03f, 1) + isnull(gcm04f, 0) end), gcm15c, gcm16f, gcm17c, gcm18f, gcm19c, gcm20f, gcm23c, gcm24f, gcm25i from gcm10 with (nolock), gcm11 with (nolock) where gcl00c = gcm00c and gcm01c = gcl01c and gcl80d >= @fromdate and gcl80d <= @todate and gcl06i = 1 -- 处理换疗程的,卡数为0,次数扣,金额扣 insert gcm08(gch00c, gch02d, gch03c, gch04c, gch05f, gch06f, gch07f, gch08f, gch09f, gch10c, gch11c, gch12i, gch13f, gch14f, gch15f, gch16i, gch17c, gch18f) select hei00c, hei03d, hei02c, hei04c, 0, 0, 0, hei06f, hei07f, 'pj', hei01c, hei11i, hei12f, 0, hei07f/(case when isnull(hei06f, 1) = 0 then 1 else isnull(hei06f, 1) end), hei13i, '9', hei07f from hem08 with (nolock) where hei03d >= @fromdate and hei03d <= @todate and isnull(hei02c, '') <> '' -- 收银的时候 if (@SP055 = '0')--项目支付的时候 begin --当时用的哪个疗程支付应该被记录下来,否则无法重复生成, debug insert gcm08(gch00c, gch02d, gch03c, gch04c, gch05f, gch06f, gch07f, gch08f, gch09f, gch10c, gch11c, gch12i, gch13f, gch15f, gch16i, gch17c, gch18f) select gga00c, gga80d, gga08c, ggb03c, 0, 0, 0, ggb05f, ggb10f, 'gx', gga01c, ggb45i, ggb46f, ggb11f/(case when isnull(ggb05f, 1) = 0 then 1 else isnull(ggb05f, 1) end), ggb47i, ggb27c, ggb11f from ggm01 with (nolock), ggm02 with (nolock) where gga00c = ggb00c and gga01c = ggb01c and gga80d >= @fromdate and gga80d <= @todate and ggb27c = @payway and isnull(gga08c, '') <> '' end if (@SP055 = '1') --支付明细中只要有用疗程支付,那么就需要转换 begin if (@SP031 = '1')--收银结帐 begin insert gcm08(gch00c, gch02d, gch03c, gch04c, gch05f, gch06f, gch07f, gch08f, gch09f, gch10c, gch11c, gch16i) select distinct gga00c, gga80d, gga08c, ggb03c, 0, 0, 0, ggb05f, ggb11f, 'gx', gga01c, ggb47i from ggm01 with (nolock), ggm02 with (nolock), gsm03 with (nolock), gcm06 with (nolock) where gga00c = ggb00c and gga01c = ggb01c and gga00c = gsc00c and gga01c = gsc01c and gga00c = gcf00c and gga08c = gcf01c and ggb03c = gcf02c and gsc02c = 'gx' and gsc04c = @payway and gga80d >= @fromdate and gga80d <= @todate end else if (@SP031 = '2')--多张单统一出院 begin insert gcm08(gch00c, gch02d, gch03c, gch04c, gch05f, gch06f, gch07f, gch08f, gch09f, gch10c, gch11c, gch16i) select distinct gla00c, gla80d, gla06c, ggb03c, 0, 0, 0, ggb05f, ggb11f, 'gl', gla01c, ggb47i from ggm01 with (nolock), ggm02 with (nolock), gsm03 with (nolock), gcm06 with (nolock), glm01 with (nolock) where gga00c = ggb00c and gga01c = ggb01c and gga00c = gla00c and gga30c = gla01c and gsc01c = gga00c and gsc01c = gla01c and gsc02c = 'gl' and gga00c = gcf00c and gla06c = gcf01c and ggb03c = gcf02c and gsc04c = @payway and gla80d >= @fromdate and gla80d <= @todate end end end GO /****** Object: StoredProcedure [dbo].[upg_cal_operation_result] Script Date: 06/12/2017 06:19:33 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_cal_operation_result] ( @compid varchar(10), -- 公司别 @fromdate varchar(10), -- 开始日期 @todate varchar(10), -- 截至日期 @fromstaff varchar(20), -- 员工代码 @tostaff varchar(20), -- 员工代码 @amttype int -- 0-营业额 1-虚业绩 2-数量 3-实业绩 ) as begin declare @printtype int --列印类型,add by lyj 保存@amttype的值/2007-08-15 set @printtype = @amttype declare @t_cnt float -- 总客数 declare @t_amt float -- 总业绩 declare @t_f_cnt float declare @t_f_amt float declare @t_n_cnt float declare @t_n_amt float declare @card_total_amt float declare @tmp_amt float declare @female_count float -- 女客 declare @male_count float -- 男客 declare @t_f_amt_tmp float declare @t_n_amt_tmp float declare @old_amt float --add by lyj for 新老客数数量 declare @new_amt float --add by lyj for 新老客数数量 declare @old_count float --add by lyj for 新老客数金额 declare @new_count float --add by lyj for 新老客数金额 declare @old_amt_tmp float --add by lyj for 新老客数和金额 declare @new_amt_tmp float --add by lyj for 新老客数和金额 if isnull(@fromstaff, '') = '*' select @fromstaff = '' -- 如果为数量, 也按营业额来生成报表 if @amttype = 2 set @amttype = 0 create table #st_results_2 ( idd int identity not null, type varchar(10) not null, cnt float null, amt float null, price float null, primary key(idd) ) create table #st_results ( idd int identity not null, type varchar(10) not null, cnt float null, amt float null, price float null, cnt_f float null, -- 指定的数量 amt_f float null, -- 指定的金额 cnt_t float null, -- 轮班的数量 amt_t float null, -- 轮班的金额 tamt float null, -- 真实营业额 primary key(idd) ) -- 用于临时保存指定或轮班的统计数据,需要更新到#st_results create table #st_results_fixorno ( idd int identity not null, type varchar(10) not null, cnt float null, amt float null, price float null, primary key(idd) ) create table #st_results_fixorno_buf ( idd int identity not null, type varchar(10) not null, cnt_t float null, amt_t float null, cnt_f float null, amt_f float null, primary key(idd) ) create table #st_results_fixorno_grp ( idd int identity not null, type varchar(10) not null, cnt_t float null, amt_t float null, cnt_f float null, amt_f float null, primary key(idd) ) create table #st_bills ( idd int identity not null, billid varchar(20) not null, -- 单号 srv_type varchar(10) null, gender int null, -- 性别 custtype int null, -- 0-新客 1-老客 add by lyj primary key(idd) ) -- add by lyj 添加列印是否打印新老客数 create table #st_bills_old ( idd int identity not null, billid varchar(20) not null, ) -- add by lyj 添加列印是否打印新老客数 create table #st_bills_new ( idd int identity not null, billid varchar(20) not null, ) -- 指定的单据 -- 是否指定按单据为单位来统计 create table #st_bills_fix ( idd int identity not null, billid varchar(20) not null, -- 单号 primary key(idd) ) -- 不指定的单据,不指定的单据 + 指定的单据 = 所有的单据(这里有问题, 实际上还有产品外卖的单据) create table #st_bills_nofix ( idd int identity not null, billid varchar(20) not null, -- 单号 primary key(idd) ) -- 产品外卖的单据 create table #st_bills_good ( idd int identity not null, billid varchar(20) null, primary key(idd) ) insert #st_bills_good(billid) select distinct ggc01c from ggm03 b with (nolock), ggm01 a with (nolock) where a.gga00c = b.ggc00c and a.gga00c = @compid and a.gga80d >= @fromdate and a.gga80d <= @todate and (@fromstaff = '' or (b.ggc12c >= @fromstaff and b.ggc12c <= @tostaff) or (b.ggc15c >= @fromstaff and b.ggc15c <= @tostaff) or (b.ggc17c >= @fromstaff and b.ggc17c <= @tostaff)) -- 所有这段时间来得客户 insert #st_bills(billid, gender, custtype) select gga01c, isnull(gga31i, 0), isnull(gga13f, 0) from ggm01 with (nolock) where gga00c = @compid and gga80d >= @fromdate and gga80d <= @todate -- 指定的总单据 if @fromstaff = '' begin -- 所有员工的指定的单据 insert #st_bills_fix(billid) select distinct ggb01c from ggm02 b with (nolock), ggm01 a with (nolock) where a.gga00c = b.ggb00c and a.gga01c = b.ggb01c and a.gga00c = @compid and a.gga80d >= @fromdate and a.gga80d <= @todate and isnull(b.ggb15c, '') = '1' end else -- 确定员工范围的 begin insert #st_bills_fix(billid) select distinct ggb01c from ggm02 b with (nolock), ggm01 a with (nolock) where a.gga00c = b.ggb00c and a.gga01c = b.ggb01c and a.gga00c = @compid and a.gga80d >= @fromdate and a.gga80d <= @todate and ((isnull(b.ggb15c, '') = '1' and ggb12c >= @fromstaff and ggb12c <= @tostaff) or (isnull(b.ggb16c, '') = '1' and ggb13c >= @fromstaff and ggb13c <= @tostaff) or (isnull(b.ggb19c, '') = '1' and ggb14c >= @fromstaff and ggb14c <= @tostaff) or (isnull(b.ggb25c, '') = '1' and ggb26c >= @fromstaff and ggb26c <= @tostaff)) end -- 统计不指定的单据 if isnull(@fromstaff, '') <> '' begin -- 一个员工的不指定的单据 insert #st_bills_nofix(billid) select distinct ggb01c from ggm02 b with (nolock), ggm01 a with (nolock) where a.gga00c = b.ggb00c and a.gga01c = b.ggb01c and a.gga00c = @compid and a.gga80d >= @fromdate and a.gga80d <= @todate and ((isnull(b.ggb15c, '') = '2' and ggb12c >= @fromstaff and ggb12c <= @tostaff) or (isnull(b.ggb16c, '') = '2' and ggb13c >= @fromstaff and ggb13c <= @tostaff) or (isnull(b.ggb19c, '') = '2' and ggb14c >= @fromstaff and ggb14c <= @tostaff) or (isnull(b.ggb25c, '') = '2' and ggb26c >= @fromstaff and ggb26c <= @tostaff)) end -- 新客数 --add by lyj for 新老客数和金额 if @fromstaff = '' begin -- 所有员工的 新客数 insert #st_bills_new(billid) select distinct ggb01c from ggm02 b with (nolock), #st_bills a where b.ggb00c = @compid and a.billid = b.ggb01c and custtype = 0 end else begin insert #st_bills_new(billid) select distinct ggb01c from ggm02 b with (nolock), #st_bills a where b.ggb00c = @compid and a.billid = b.ggb01c and ((ggb12c >= @fromstaff and ggb12c <= @tostaff) or (ggb13c >= @fromstaff and ggb13c <= @tostaff) or (ggb14c >= @fromstaff and ggb14c <= @tostaff) or (ggb26c >= @fromstaff and ggb26c <= @tostaff)) and custtype = 0 end -- 老客数 --add by lyj for 新老客数和金额 if @fromstaff = '' begin -- 所有员工的老客数 insert #st_bills_old(billid) select distinct ggb01c from ggm02 b with (nolock), #st_bills a where b.ggb00c = @compid and a.billid = b.ggb01c and custtype = 1 end else begin insert #st_bills_old(billid) select distinct ggb01c from ggm02 b with (nolock), #st_bills a where b.ggb00c = @compid and a.billid = b.ggb01c and ((ggb12c >= @fromstaff and ggb12c <= @tostaff) or (ggb13c >= @fromstaff and ggb13c <= @tostaff) or (ggb14c >= @fromstaff and ggb14c <= @tostaff) or (ggb26c >= @fromstaff and ggb26c <= @tostaff)) and custtype = 1 end if @fromstaff = '' begin -- 不区分员工 update a set a.srv_type = '1' from #st_bills a, #st_bills_fix b where a.billid = b.billid update a set a.srv_type = '2' from #st_bills a where isnull(srv_type, '') = '' -- 轮班总的单据 insert #st_bills_nofix(billid) select billid from #st_bills a where isnull(srv_type, '') = '2' end else begin delete a from #st_bills_nofix a, #st_bills_fix b where a.billid = b.billid update a set a.srv_type = '1' from #st_bills a, #st_bills_fix b where a.billid = b.billid update a set a.srv_type = '2' from #st_bills a, #st_bills_nofix b where a.billid = b.billid end -- add by leiji on 2013年7月23日(如果不增加这一步, -- 当@fromstaff <> ''时, 会删除#st_bills中购买只产品的单据号, 可能会导致卡付总和计算不正确) update a set a.srv_type = '3' -- 那些只购买产品的单据(不分指定轮班) from #st_bills a, #st_bills_good b where a.billid = b.billid and isnull(srv_type, '') = '' drop table #st_bills_good -- 到此为止,已经在#st_bills中设置了是否指定的标志 delete #st_bills where isnull(srv_type, '') = '' -- 指定总客数 if(@fromstaff = '' or @tostaff= '') begin select @t_f_cnt = count(distinct a.gga01c) from ggm01 a with (nolock),ggm02 b with (nolock) where a.gga00c = b.ggb00c and a.gga01c = b.ggb01c and a.gga00c = @compid and a.gga80d >= @fromdate and a.gga80d <= @todate and ((isnull(b.ggb15c, '') = '1') or (isnull(b.ggb16c, '') = '1' ) or (isnull(b.ggb19c, '') = '1' ) or (isnull(b.ggb25c, '') = '1')) end else begin select @t_f_cnt = count(distinct a.gga01c) from ggm01 a with (nolock),ggm02 b with (nolock) where a.gga00c = b.ggb00c and a.gga01c = b.ggb01c and a.gga00c = @compid and a.gga80d >= @fromdate and a.gga80d <= @todate and ((isnull(b.ggb15c, '') = '1' and ggb12c >=@fromstaff and ggb12c <= @tostaff) or (isnull(b.ggb16c, '') = '1' and ggb13c >= @fromstaff and ggb13c <= @tostaff) or (isnull(b.ggb19c, '') = '1' and ggb14c >= @fromstaff and ggb14c <= @tostaff) or (isnull(b.ggb25c, '') = '1' and ggb26c >= @fromstaff and ggb26c <= @tostaff)) --modify by limin 2014-12-31 end -- 统计男女的数量 select @female_count = count(*) from #st_bills where gender = 0 and (srv_type = '1' or srv_type = '2') select @male_count = count(*) from #st_bills where gender = 1 and (srv_type = '1' or srv_type = '2') -- add by lyj 添加新老客数 select @old_count = count(*) from #st_bills_old select @new_count = count(*) from #st_bills_new if @fromstaff = '' begin if @amttype = 0 begin -- 指定客业绩 select @t_f_amt = sum(a.ggb11f) from ggm02 a with (nolock), #st_bills_fix b where a.ggb01c = b.billid and a.ggb00c = @compid -- 不定客业绩 select @t_n_amt = sum(a.ggb11f) from ggm02 a with (nolock), #st_bills_nofix b where a.ggb01c = b.billid and a.ggb00c = @compid -- 新客-add by lyj select @new_amt = sum(a.ggb11f) from ggm02 a with (nolock), #st_bills_new b where a.ggb01c = b.billid and a.ggb00c = @compid -- 老客 --add by lyj for 新老客数和金额 select @old_amt = sum(a.ggb11f) from ggm02 a with (nolock), #st_bills_old b where a.ggb01c = b.billid and a.ggb00c = @compid end else if @amttype = 1 begin -- 指定客业绩 select @t_f_amt = sum(isnull(a.ggb20f, 0) + isnull(a.ggb21f, 0) + isnull(a.ggb22f, 0) + isnull(a.ggb35f, 0) ) from ggm02 a with (nolock), #st_bills_fix b where a.ggb01c = b.billid and a.ggb00c = @compid -- 不定客业绩 select @t_n_amt = sum(isnull(a.ggb20f, 0) + isnull(a.ggb21f, 0) + isnull(a.ggb22f, 0) + isnull(a.ggb35f, 0) ) from ggm02 a with (nolock), #st_bills_nofix b where a.ggb01c = b.billid and a.ggb00c = @compid -- 新客 add by lyj select @new_amt = sum(isnull(a.ggb20f, 0) + isnull(a.ggb21f, 0) + isnull(a.ggb22f, 0) + isnull(a.ggb35f, 0) ) from ggm02 a with (nolock), #st_bills_new b where a.ggb01c = b.billid and a.ggb00c = @compid -- 老客 add by lyj select @old_amt = sum(isnull(a.ggb20f, 0) + isnull(a.ggb21f, 0) + isnull(a.ggb22f, 0) + isnull(a.ggb35f, 0) ) from ggm02 a with (nolock), #st_bills_old b where a.ggb01c = b.billid and a.ggb00c = @compid end else if @amttype = 3 begin --新客 --add by lyj for 新老客数和金额 select @new_amt = sum(isnull(a.ggb36f, 0) + isnull(a.ggb37f, 0) + isnull(a.ggb38f, 0) + isnull(a.ggb39f, 0) ) from ggm02 a with (nolock), #st_bills_new b where a.ggb01c = b.billid and a.ggb00c = @compid --老客 --add by lyj for 新老客数和金额 select @old_amt = sum(isnull(a.ggb36f, 0) + isnull(a.ggb37f, 0) + isnull(a.ggb38f, 0) + isnull(a.ggb39f, 0) ) from ggm02 a with (nolock), #st_bills_old b where a.ggb01c = b.billid and a.ggb00c = @compid --指定客业绩 select @t_f_amt = sum(isnull(a.ggb36f, 0) + isnull(a.ggb37f, 0) + isnull(a.ggb38f, 0) + isnull(a.ggb39f, 0) ) from ggm02 a with (nolock), #st_bills_fix b where a.ggb01c = b.billid and a.ggb00c = @compid --不定客业绩 select @t_n_amt = sum(isnull(a.ggb36f, 0) + isnull(a.ggb37f, 0) + isnull(a.ggb38f, 0) + isnull(a.ggb39f, 0) ) from ggm02 a with (nolock), #st_bills_nofix b where a.ggb01c = b.billid and a.ggb00c = @compid end end else begin --指定客业绩 if @amttype = 0 begin --新客 add by lyj select @new_amt = sum(a.ggb11f) from ggm02 a with (nolock), #st_bills_new b where a.ggb01c = b.billid and a.ggb00c = @compid and ((ggb12c >= @fromstaff and ggb12c <= @tostaff) or (ggb13c >= @fromstaff and ggb13c <= @tostaff) or (ggb14c >= @fromstaff and ggb14c <= @tostaff) or (ggb26c >= @fromstaff and ggb26c <= @tostaff)) --老客 add by lyj select @old_amt = sum(a.ggb11f) from ggm02 a with (nolock), #st_bills_old b where a.ggb01c = b.billid and a.ggb00c = @compid and ((ggb12c >= @fromstaff and ggb12c <= @tostaff) or (ggb13c >= @fromstaff and ggb13c <= @tostaff) or (ggb14c >= @fromstaff and ggb14c <= @tostaff) or (ggb26c >= @fromstaff and ggb26c <= @tostaff)) --指定业绩 select @t_f_amt = sum(b.ggb11f) from ggm01 a with (nolock),ggm02 b with (nolock) where a.gga00c = b.ggb00c and a.gga01c = b.ggb01c and a.gga00c = @compid and a.gga80d >= @fromdate and a.gga80d <= @todate and ((isnull(b.ggb15c, '') = '1' and ggb12c >=@fromstaff and ggb12c <= @tostaff) or (isnull(b.ggb16c, '') = '1' and ggb13c >= @fromstaff and ggb13c <= @tostaff) or (isnull(b.ggb19c, '') = '1' and ggb14c >= @fromstaff and ggb14c <= @tostaff) or (isnull(b.ggb25c, '') = '1' and ggb26c >= @fromstaff and ggb26c <= @tostaff)) --modify by limin 2014-12-31 group by b.ggb00c --不定客业绩 select @t_n_amt = sum(b.ggb11f) from ggm01 a with (nolock) ,ggm02 b with (nolock) where a.gga00c = b.ggb00c and a.gga01c = b.ggb01c and a.gga00c = @compid and a.gga80d >= @fromdate and a.gga80d <= @todate and ((isnull(b.ggb15c, '') = '2' and ggb12c >=@fromstaff and ggb12c <= @tostaff) or (isnull(b.ggb16c, '') = '2' and ggb13c >= @fromstaff and ggb13c <= @tostaff) or (isnull(b.ggb19c, '') = '2' and ggb14c >= @fromstaff and ggb14c <= @tostaff) or (isnull(b.ggb25c, '') = '2' and ggb26c >= @fromstaff and ggb26c <= @tostaff)) --modify by limin 2014-12-31 group by b.ggb00c end else if @amttype = 1 begin --指定客业绩 --大工 select @t_f_amt_tmp = 0 select @t_f_amt_tmp = sum(isnull(a.ggb20f, 0)) from ggm02 a with (nolock), #st_bills_fix b where a.ggb01c = b.billid and a.ggb00c = @compid and ((ggb12c >= @fromstaff and ggb12c <= @tostaff)) select @t_f_amt = isnull(@t_f_amt, 0) + isnull(@t_f_amt_tmp, 0) --中工业绩 select @t_f_amt_tmp = 0 select @t_f_amt_tmp = sum(isnull(a.ggb21f, 0)) from ggm02 a with (nolock), #st_bills_fix b where a.ggb01c = b.billid and a.ggb00c = @compid and ((ggb13c >= @fromstaff and ggb13c <= @tostaff)) select @t_f_amt = isnull(@t_f_amt, 0) + isnull(@t_f_amt_tmp, 0) --小工业绩 select @t_f_amt_tmp = 0 select @t_f_amt_tmp = sum(isnull(a.ggb22f, 0)) from ggm02 a with (nolock), #st_bills_fix b where a.ggb01c = b.billid and a.ggb00c = @compid and ((ggb14c >= @fromstaff and ggb14c <= @tostaff)) select @t_f_amt = isnull(@t_f_amt, 0) + isnull(@t_f_amt_tmp, 0) --四工业绩 select @t_f_amt_tmp = 0 select @t_f_amt_tmp = sum(isnull(a.ggb35f, 0)) from ggm02 a with (nolock), #st_bills_fix b where a.ggb01c = b.billid and a.ggb00c = @compid and ((ggb26c >= @fromstaff and ggb26c <= @tostaff)) select @t_f_amt = isnull(@t_f_amt, 0) + isnull(@t_f_amt_tmp, 0) --不指定客业绩 --大工业绩 select @t_n_amt_tmp = 0 select @t_n_amt_tmp = sum(isnull(a.ggb20f, 0)) from ggm02 a with (nolock), #st_bills_nofix b where a.ggb01c = b.billid and a.ggb00c = @compid and ((ggb12c >= @fromstaff and ggb12c <= @tostaff)) select @t_n_amt = isnull(@t_n_amt, 0) + isnull(@t_n_amt_tmp, 0) --中工业绩 select @t_n_amt_tmp = 0 select @t_n_amt_tmp = sum(isnull(a.ggb21f, 0)) from ggm02 a with (nolock), #st_bills_nofix b where a.ggb01c = b.billid and a.ggb00c = @compid and ((ggb13c >= @fromstaff and ggb13c <= @tostaff)) select @t_n_amt = isnull(@t_n_amt, 0) + isnull(@t_n_amt_tmp, 0) --小工业绩 select @t_n_amt_tmp = 0 select @t_n_amt_tmp = sum(isnull(a.ggb22f, 0)) from ggm02 a with (nolock), #st_bills_nofix b where a.ggb01c = b.billid and a.ggb00c = @compid and ((ggb14c >= @fromstaff and ggb14c <= @tostaff)) select @t_n_amt = isnull(@t_n_amt, 0) + isnull(@t_n_amt_tmp, 0) --四工业绩 select @t_n_amt_tmp = 0 select @t_n_amt_tmp = sum(isnull(a.ggb35f, 0)) from ggm02 a with (nolock), #st_bills_nofix b where a.ggb01c = b.billid and a.ggb00c = @compid and ((ggb26c >= @fromstaff and ggb26c <= @tostaff)) select @t_n_amt = isnull(@t_n_amt, 0) + isnull(@t_n_amt_tmp, 0) --add by lyj --新客 --大工业绩 select @new_amt_tmp = 0 select @new_amt_tmp = sum(isnull(a.ggb20f, 0)) from ggm02 a with (nolock), #st_bills_new b where a.ggb01c = b.billid and a.ggb00c = @compid and ((ggb12c >= @fromstaff and ggb12c <= @tostaff)) select @new_amt = isnull(@new_amt, 0) + isnull(@new_amt_tmp, 0) --中工业绩 select @new_amt_tmp = 0 select @new_amt_tmp = sum(isnull(a.ggb21f, 0)) from ggm02 a with (nolock), #st_bills_new b where a.ggb01c = b.billid and a.ggb00c = @compid and ((ggb13c >= @fromstaff and ggb13c <= @tostaff)) select @new_amt = isnull(@new_amt, 0) + isnull(@new_amt_tmp, 0) --小工业绩 select @new_amt_tmp = 0 select @new_amt_tmp = sum(isnull(a.ggb22f, 0)) from ggm02 a with (nolock), #st_bills_new b where a.ggb01c = b.billid and a.ggb00c = @compid and ((ggb14c >= @fromstaff and ggb14c <= @tostaff)) select @new_amt = isnull(@new_amt, 0) + isnull(@new_amt_tmp, 0) --四工业绩 select @new_amt_tmp = 0 select @new_amt_tmp = sum(isnull(a.ggb35f, 0)) from ggm02 a with (nolock), #st_bills_new b where a.ggb01c = b.billid and a.ggb00c = @compid and ((ggb26c >= @fromstaff and ggb26c <= @tostaff)) select @new_amt = isnull(@new_amt, 0) + isnull(@new_amt_tmp, 0) --老客数 --大工业绩 select @old_amt_tmp = 0 select @old_amt_tmp = sum(isnull(a.ggb20f, 0)) from ggm02 a with (nolock), #st_bills_old b where a.ggb01c = b.billid and a.ggb00c = @compid and ((ggb12c >= @fromstaff and ggb12c <= @tostaff)) select @old_amt = isnull(@old_amt, 0) + isnull(@old_amt_tmp, 0) --中工业绩 select @old_amt_tmp = 0 select @old_amt_tmp = sum(isnull(a.ggb21f, 0)) from ggm02 a with (nolock), #st_bills_old b where a.ggb01c = b.billid and a.ggb00c = @compid and ((ggb13c >= @fromstaff and ggb13c <= @tostaff)) select @old_amt = isnull(@old_amt, 0) + isnull(@old_amt_tmp, 0) --小工业绩 select @old_amt_tmp = 0 select @old_amt_tmp = sum(isnull(a.ggb22f, 0)) from ggm02 a with (nolock), #st_bills_old b where a.ggb01c = b.billid and a.ggb00c = @compid and ((ggb14c >= @fromstaff and ggb14c <= @tostaff)) select @old_amt = isnull(@old_amt, 0) + isnull(@old_amt_tmp, 0) --四工业绩 select @old_amt_tmp = 0 select @old_amt_tmp = sum(isnull(a.ggb35f, 0)) from ggm02 a with (nolock), #st_bills_old b where a.ggb01c = b.billid and a.ggb00c = @compid and ((ggb26c >= @fromstaff and ggb26c <= @tostaff)) select @old_amt = isnull(@old_amt, 0) + isnull(@old_amt_tmp, 0) --end end else if @amttype = 3 begin --计算实业绩 --指定客业绩 --大工 select @t_f_amt_tmp = 0 select @t_f_amt_tmp = sum(isnull(a.ggb36f, 0)) from ggm02 a with (nolock), #st_bills_fix b where a.ggb01c = b.billid and a.ggb00c = @compid and ((ggb12c >= @fromstaff and ggb12c <= @tostaff)) select @t_f_amt = isnull(@t_f_amt, 0) + isnull(@t_f_amt_tmp, 0) --中工业绩 select @t_f_amt_tmp = 0 select @t_f_amt_tmp = sum(isnull(a.ggb37f, 0)) from ggm02 a with (nolock), #st_bills_fix b where a.ggb01c = b.billid and a.ggb00c = @compid and ((ggb13c >= @fromstaff and ggb13c <= @tostaff)) select @t_f_amt = isnull(@t_f_amt, 0) + isnull(@t_f_amt_tmp, 0) --小工业绩 select @t_f_amt_tmp = 0 select @t_f_amt_tmp = sum(isnull(a.ggb38f, 0)) from ggm02 a with (nolock), #st_bills_fix b where a.ggb01c = b.billid and a.ggb00c = @compid and ((ggb14c >= @fromstaff and ggb14c <= @tostaff)) select @t_f_amt = isnull(@t_f_amt, 0) + isnull(@t_f_amt_tmp, 0) --四工业绩 select @t_f_amt_tmp = 0 select @t_f_amt_tmp = sum(isnull(a.ggb39f, 0)) from ggm02 a with (nolock), #st_bills_fix b where a.ggb01c = b.billid and a.ggb00c = @compid and ((ggb26c >= @fromstaff and ggb26c <= @tostaff)) select @t_f_amt = isnull(@t_f_amt, 0) + isnull(@t_f_amt_tmp, 0) --不指定客业绩 --大工业绩 select @t_n_amt_tmp = 0 select @t_n_amt_tmp = sum(isnull(a.ggb36f, 0)) from ggm02 a with (nolock), #st_bills_nofix b where a.ggb01c = b.billid and a.ggb00c = @compid and ((ggb12c >= @fromstaff and ggb12c <= @tostaff)) select @t_n_amt = isnull(@t_n_amt, 0) + isnull(@t_n_amt_tmp, 0) --中工业绩 select @t_n_amt_tmp = 0 select @t_n_amt_tmp = sum(isnull(a.ggb37f, 0)) from ggm02 a with (nolock), #st_bills_nofix b where a.ggb01c = b.billid and a.ggb00c = @compid and ((ggb13c >= @fromstaff and ggb13c <= @tostaff)) select @t_n_amt = isnull(@t_n_amt, 0) + isnull(@t_n_amt_tmp, 0) --小工业绩 select @t_n_amt_tmp = 0 select @t_n_amt_tmp = sum(isnull(a.ggb38f, 0)) from ggm02 a with (nolock), #st_bills_nofix b where a.ggb01c = b.billid and a.ggb00c = @compid and ((ggb14c >= @fromstaff and ggb14c <= @tostaff)) select @t_n_amt = isnull(@t_n_amt, 0) + isnull(@t_n_amt_tmp, 0) --四工业绩 select @t_n_amt_tmp = 0 select @t_n_amt_tmp = sum(isnull(a.ggb39f, 0)) from ggm02 a with (nolock), #st_bills_nofix b where a.ggb01c = b.billid and a.ggb00c = @compid and ((ggb26c >= @fromstaff and ggb26c <= @tostaff)) select @t_n_amt = isnull(@t_n_amt, 0) + isnull(@t_n_amt_tmp, 0) --add by lyj --新客 --大工业绩 select @new_amt_tmp = 0 select @new_amt_tmp = sum(isnull(a.ggb36f, 0)) from ggm02 a with (nolock), #st_bills_new b where a.ggb01c = b.billid and a.ggb00c = @compid and ((ggb12c >= @fromstaff and ggb12c <= @tostaff)) select @new_amt = isnull(@new_amt, 0) + isnull(@new_amt_tmp, 0) --中工业绩 select @new_amt_tmp = 0 select @new_amt_tmp = sum(isnull(a.ggb37f, 0)) from ggm02 a with (nolock), #st_bills_new b where a.ggb01c = b.billid and a.ggb00c = @compid and ((ggb13c >= @fromstaff and ggb13c <= @tostaff)) select @new_amt = isnull(@new_amt, 0) + isnull(@new_amt_tmp, 0) --小工业绩 select @new_amt_tmp = 0 select @new_amt_tmp = sum(isnull(a.ggb38f, 0)) from ggm02 a with (nolock), #st_bills_new b where a.ggb01c = b.billid and a.ggb00c = @compid and ((ggb14c >= @fromstaff and ggb14c <= @tostaff)) select @new_amt = isnull(@t_n_amt, 0) + isnull(@new_amt_tmp, 0) --四工业绩 select @new_amt_tmp = 0 select @new_amt_tmp = sum(isnull(a.ggb39f, 0)) from ggm02 a with (nolock), #st_bills_new b where a.ggb01c = b.billid and a.ggb00c = @compid and ((ggb26c >= @fromstaff and ggb26c <= @tostaff)) select @new_amt = isnull(@t_n_amt, 0) + isnull(@new_amt_tmp, 0) --add by lyj 老客数 --大工业绩 select @old_amt_tmp = 0 select @old_amt_tmp = sum(isnull(a.ggb36f, 0)) from ggm02 a with (nolock), #st_bills_old b where a.ggb01c = b.billid and a.ggb00c = @compid and ((ggb12c >= @fromstaff and ggb12c <= @tostaff)) select @old_amt = isnull(@old_amt, 0) + isnull(@old_amt_tmp, 0) --中工业绩 select @old_amt_tmp = 0 select @old_amt_tmp = sum(isnull(a.ggb37f, 0)) from ggm02 a with (nolock), #st_bills_old b where a.ggb01c = b.billid and a.ggb00c = @compid and ((ggb13c >= @fromstaff and ggb13c <= @tostaff)) select @old_amt = isnull(@t_n_amt, 0) + isnull(@old_amt_tmp, 0) --小工业绩 select @old_amt_tmp = 0 select @old_amt_tmp = sum(isnull(a.ggb38f, 0)) from ggm02 a with (nolock), #st_bills_old b where a.ggb01c = b.billid and a.ggb00c = @compid and ((ggb14c >= @fromstaff and ggb14c <= @tostaff)) select @old_amt = isnull(@t_n_amt, 0) + isnull(@old_amt_tmp, 0) --四工业绩 select @old_amt_tmp = 0 select @old_amt_tmp = sum(isnull(a.ggb39f, 0)) from ggm02 a with (nolock), #st_bills_old b where a.ggb01c = b.billid and a.ggb00c = @compid and ((ggb26c >= @fromstaff and ggb26c <= @tostaff)) select @old_amt = isnull(@t_n_amt, 0) + isnull(@old_amt_tmp, 0) end end -- 不定客数 select @t_n_cnt = count(distinct a.gga01c) from ggm01 a with (nolock),ggm02 b with (nolock) where a.gga00c = b.ggb00c and a.gga01c = b.ggb01c and a.gga00c = @compid and a.gga80d >= @fromdate and a.gga80d <= @todate and ((isnull(b.ggb15c, '') = '2' and ggb12c >=@fromstaff and ggb12c <= @tostaff) or (isnull(b.ggb16c, '') = '2' and ggb13c >= @fromstaff and ggb13c <= @tostaff) or (isnull(b.ggb19c, '') = '2' and ggb14c >= @fromstaff and ggb14c <= @tostaff) or (isnull(b.ggb25c, '') = '2' and ggb26c >= @fromstaff and ggb26c <= @tostaff)) and a.gga01c not in( select gga01c from ggm01 a with (nolock),ggm02 b with (nolock) where a.gga00c = b.ggb00c and a.gga01c = b.ggb01c and a.gga00c = @compid and a.gga80d >= @fromdate and a.gga80d <= @todate and ((isnull(b.ggb15c, '') = '1' and ggb12c >=@fromstaff and ggb12c <= @tostaff) or (isnull(b.ggb16c, '') = '1' and ggb13c >= @fromstaff and ggb13c <= @tostaff) or (isnull(b.ggb19c, '') = '1' and ggb14c >= @fromstaff and ggb14c <= @tostaff) or (isnull(b.ggb25c, '') = '1' and ggb26c >= @fromstaff and ggb26c <= @tostaff)) ) -- 合计总的客数与总的业绩 select @t_cnt = isnull(@t_f_cnt, 0) + isnull(@t_n_cnt, 0), @t_amt = isnull(@t_f_amt, 0) + isnull(@t_n_amt, 0) -- 插入到返回结果的表中 --总的客数,总的金额 insert #st_results(type, cnt, amt) values('0-0', @t_cnt, @t_amt) -- 指定的客数,总的金额 insert #st_results(type, cnt, amt) values('0-1', @t_f_cnt, @t_f_amt) -- 不指定的客数,总的金额 insert #st_results(type, cnt, amt) values('0-2', @t_n_cnt, @t_n_amt) -- 男女客数量 insert #st_results(type, cnt, amt) values('A-1', @female_count, @female_count) insert #st_results(type, cnt, amt) values('A-2', @male_count, @male_count) -- add by lyj for 添加列印新老客数 declare @price float if (isnull(@new_count, 0) = 0) set @price = 0 else set @price = @new_amt / @new_count insert #st_results(type, cnt, amt, price) values('N-1', @new_count, @new_amt, @price) insert #st_results(type, cnt, amt, price) values('N-2', @old_count, @old_amt, @price) -- 不同统计类的 -- 所有员工的 if @fromstaff = '' begin if (@amttype = 0 and @printtype = 0)--modify by lyj/2007-08-15 begin insert #st_results(type, cnt, amt, tamt) select s_type = gda13c, cnt = sum(ggb05f), amt = sum(isnull(a.ggb11f, 0)), tamt = sum(isnull(a.ggb11f, 0) * isnull(a.ggb34f, 1)) from ggm02 a with (nolock), gdm01 b with (nolock), ggm01 c with (nolock) where a.ggb00c = b.gda00c and a.ggb03c = b.gda01c and a.ggb00c = @compid and a.ggb00c = c.gga00c and a.ggb01c = c.gga01c and c.gga80d >= @fromdate and c.gga80d <= @todate and isnull(gda13c, '') <> '' group by gda13c -- 指定部分 insert #st_results_fixorno(type, cnt, amt) select s_type = gda13c, cnt = sum(ggb05f), amt = sum(isnull(a.ggb11f, 0)) from ggm02 a with (nolock), gdm01 b with (nolock), ggm01 c with (nolock), #st_bills_fix d where a.ggb00c = b.gda00c and a.ggb03c = b.gda01c and a.ggb00c = @compid and a.ggb00c = c.gga00c and a.ggb01c = c.gga01c and c.gga80d >= @fromdate and c.gga80d <= @todate and isnull(gda13c, '') <> '' and c.gga01c = d.billid group by gda13c update a set a.cnt_f = b.cnt, a.amt_f = b.amt from #st_results a, #st_results_fixorno b where a.type = b.type delete #st_results_fixorno insert #st_results_fixorno(type, cnt, amt) select s_type = gda13c, cnt = sum(ggb05f), amt = sum(isnull(a.ggb11f, 0)) from ggm02 a with (nolock), gdm01 b with (nolock), ggm01 c with (nolock), #st_bills_nofix d where a.ggb00c = b.gda00c and a.ggb03c = b.gda01c and a.ggb00c = @compid and a.ggb00c = c.gga00c and a.ggb01c = c.gga01c and c.gga80d >= @fromdate and c.gga80d <= @todate and isnull(gda13c, '')<>'' and c.gga01c = d.billid group by gda13c --轮班部分 update a set a.cnt_t = b.cnt, a.amt_t = b.amt from #st_results a, #st_results_fixorno b where a.type = b.type delete #st_results_fixorno end else if (@printtype = 2)--modify by lyj/2007-08-15 begin --所有 insert #st_results(type, cnt, amt, tamt) select s_type = gda13c, cnt = sum(ggb05f), amt = sum(isnull(a.ggb11f, 0)), tamt = sum(isnull(a.ggb11f, 0)*isnull(a.ggb34f, 1)) from ggm02 a with (nolock), gdm01 b with (nolock), ggm01 c with (nolock) where a.ggb00c = b.gda00c and a.ggb03c = b.gda01c and a.ggb00c = @compid and a.ggb00c = c.gga00c and a.ggb01c = c.gga01c and c.gga80d >= @fromdate and c.gga80d <= @todate and isnull(gda13c, '')<>'' group by gda13c --指定 insert #st_results_fixorno(type, cnt, amt) select s_type = gda13c, cnt = sum(ggb05f), amt = sum(isnull(a.ggb11f, 0)) from ggm02 a with (nolock), gdm01 b with (nolock), ggm01 c with (nolock), #st_bills_fix d where a.ggb00c = b.gda00c and a.ggb03c = b.gda01c and a.ggb00c = @compid and a.ggb00c = c.gga00c and a.ggb01c = c.gga01c and c.gga80d >= @fromdate and c.gga80d <= @todate and isnull(gda13c, '')<>'' and c.gga01c = d.billid group by gda13c update a set a.cnt_f = b.cnt, a.amt_f = b.amt from #st_results a, #st_results_fixorno b where a.type = b.type delete #st_results_fixorno --轮班 insert #st_results_fixorno(type, cnt, amt) select s_type = gda13c, cnt = sum(ggb05f), amt = sum(isnull(a.ggb11f, 0)) from ggm02 a with (nolock), gdm01 b with (nolock), ggm01 c with (nolock), #st_bills_nofix d where a.ggb00c = b.gda00c and a.ggb03c = b.gda01c and a.ggb00c = @compid and a.ggb00c = c.gga00c and a.ggb01c = c.gga01c and c.gga80d >= @fromdate and c.gga80d <= @todate and isnull(gda13c, '')<>'' and c.gga01c = d.billid group by gda13c update a set a.cnt_t = b.cnt, a.amt_t = b.amt from #st_results a, #st_results_fixorno b where a.type = b.type delete #st_results_fixorno end else if @amttype = 1 begin insert #st_results(type, cnt, amt) select s_type = gda13c, cnt = sum(ggb05f), amt = sum(isnull(a.ggb20f, 0) + isnull(a.ggb21f, 0) + isnull(a.ggb22f, 0) + isnull(a.ggb35f, 0) ) from ggm02 a with (nolock), gdm01 b with (nolock), ggm01 c with (nolock) where a.ggb00c = b.gda00c and a.ggb03c = b.gda01c and a.ggb00c = @compid and a.ggb00c = c.gga00c and a.ggb01c = c.gga01c and c.gga80d >= @fromdate and c.gga80d <= @todate and isnull(gda13c, '')<>'' group by gda13c --指定 insert #st_results_fixorno(type, cnt, amt) select s_type = gda13c, cnt = sum(ggb05f), amt = sum(isnull(a.ggb20f, 0) + isnull(a.ggb21f, 0) + isnull(a.ggb22f, 0) + isnull(a.ggb35f, 0) ) from ggm02 a with (nolock), gdm01 b with (nolock), ggm01 c with (nolock), #st_bills_fix d where a.ggb00c = b.gda00c and a.ggb03c = b.gda01c and a.ggb00c = @compid and a.ggb00c = c.gga00c and a.ggb01c = c.gga01c and c.gga80d >= @fromdate and c.gga80d <= @todate and isnull(gda13c, '')<>'' and c.gga01c = d.billid group by gda13c update a set a.cnt_f = b.cnt, a.amt_f = b.amt from #st_results a, #st_results_fixorno b where a.type = b.type delete #st_results_fixorno --轮班 insert #st_results_fixorno(type, cnt, amt) select s_type = gda13c, cnt = sum(ggb05f), amt = sum(isnull(a.ggb20f, 0) + isnull(a.ggb21f, 0) + isnull(a.ggb22f, 0) + isnull(a.ggb35f, 0) ) from ggm02 a with (nolock), gdm01 b with (nolock), ggm01 c with (nolock), #st_bills_nofix d where a.ggb00c = b.gda00c and a.ggb03c = b.gda01c and a.ggb00c = @compid and a.ggb00c = c.gga00c and a.ggb01c = c.gga01c and c.gga80d >= @fromdate and c.gga80d <= @todate and isnull(gda13c, '')<>'' and c.gga01c = d.billid group by gda13c update a set a.cnt_t = b.cnt, a.amt_t = b.amt from #st_results a, #st_results_fixorno b where a.type = b.type delete #st_results_fixorno end else if @amttype = 3 begin -- 实业绩 insert #st_results(type, cnt, amt) select s_type = gda13c, cnt = sum(ggb05f), amt = sum(isnull(a.ggb36f, 0) + isnull(a.ggb37f, 0) + isnull(a.ggb38f, 0) + isnull(a.ggb39f, 0) ) from ggm02 a with (nolock), gdm01 b with (nolock), ggm01 c with (nolock) where a.ggb00c = b.gda00c and a.ggb03c = b.gda01c and a.ggb00c = @compid and a.ggb00c = c.gga00c and a.ggb01c = c.gga01c and c.gga80d >= @fromdate and c.gga80d <= @todate and isnull(gda13c, '')<>'' group by gda13c --指定部分 insert #st_results_fixorno(type, cnt, amt) select s_type = gda13c, cnt = sum(ggb05f), amt = sum(isnull(a.ggb36f, 0) + isnull(a.ggb37f, 0) + isnull(a.ggb38f, 0) + isnull(a.ggb39f, 0) ) from ggm02 a with (nolock), gdm01 b with (nolock), ggm01 c with (nolock), #st_bills_fix d where a.ggb00c = b.gda00c and a.ggb03c = b.gda01c and a.ggb00c = @compid and a.ggb00c = c.gga00c and a.ggb01c = c.gga01c and c.gga80d >= @fromdate and c.gga80d <= @todate and isnull(gda13c, '')<>'' and c.gga01c = d.billid group by gda13c update a set a.cnt_f = b.cnt, a.amt_f = b.amt from #st_results a, #st_results_fixorno b where a.type = b.type delete #st_results_fixorno --轮班部分 insert #st_results_fixorno(type, cnt, amt) select s_type = gda13c, cnt = sum(ggb05f), amt = sum(isnull(a.ggb36f, 0) + isnull(a.ggb37f, 0) + isnull(a.ggb38f, 0) + isnull(a.ggb39f, 0) ) from ggm02 a with (nolock), gdm01 b with (nolock), ggm01 c with (nolock), #st_bills_nofix d where a.ggb00c = b.gda00c and a.ggb03c = b.gda01c and a.ggb00c = @compid and a.ggb00c = c.gga00c and a.ggb01c = c.gga01c and c.gga80d >= @fromdate and c.gga80d <= @todate and isnull(gda13c, '')<>'' and c.gga01c = d.billid group by gda13c update a set a.cnt_t = b.cnt, a.amt_t = b.amt from #st_results a, #st_results_fixorno b where a.type = b.type delete #st_results_fixorno end end else begin --一个员工范围的 if (@amttype = 0 and @printtype = 0)--modify by lyj/2007-08-15 begin insert #st_results(type, cnt, amt, tamt) select s_type = gda13c, cnt = sum(ggb05f), amt = sum(ggb11f), tamt = sum(ggb11f*isnull(ggb34f, 1)) from ggm02 a with (nolock), gdm01 b with (nolock), ggm01 c with (nolock) where a.ggb00c = b.gda00c and a.ggb03c = b.gda01c and a.ggb00c = @compid and a.ggb00c = c.gga00c and a.ggb01c = c.gga01c and c.gga80d >= @fromdate and c.gga80d <= @todate and isnull(gda13c, '')<>'' and ((ggb12c >= @fromstaff and ggb12c <= @tostaff) or (ggb13c >= @fromstaff and ggb13c <= @tostaff) or (ggb14c >= @fromstaff and ggb14c <= @tostaff) or (ggb26c >= @fromstaff and ggb26c <= @tostaff)) group by gda13c --指定部分 insert #st_results_fixorno(type, cnt, amt) select s_type = gda13c, cnt = sum(ggb05f), amt = sum(ggb11f) from ggm02 a with (nolock), gdm01 b with (nolock), ggm01 c with (nolock), #st_bills_fix d where a.ggb00c = b.gda00c and a.ggb03c = b.gda01c and a.ggb00c = @compid and a.ggb00c = c.gga00c and a.ggb01c = c.gga01c and c.gga80d >= @fromdate and c.gga80d <= @todate and isnull(gda13c, '')<>'' and ((ggb12c >= @fromstaff and ggb12c <= @tostaff) or (ggb13c >= @fromstaff and ggb13c <= @tostaff) or (ggb14c >= @fromstaff and ggb14c <= @tostaff) or (ggb26c >= @fromstaff and ggb26c <= @tostaff)) and c.gga01c = d.billid group by gda13c update a set a.cnt_f = b.cnt, a.amt_f = b.amt from #st_results a, #st_results_fixorno b where a.type = b.type delete #st_results_fixorno --轮班部分 insert #st_results_fixorno(type, cnt, amt) select s_type = gda13c, cnt = sum(ggb05f), amt = sum(ggb11f) from ggm02 a with (nolock), gdm01 b with (nolock), ggm01 c with (nolock), #st_bills_nofix d where a.ggb00c = b.gda00c and a.ggb03c = b.gda01c and a.ggb00c = @compid and a.ggb00c = c.gga00c and a.ggb01c = c.gga01c and c.gga80d >= @fromdate and c.gga80d <= @todate and isnull(gda13c, '')<>'' and ((ggb12c >= @fromstaff and ggb12c <= @tostaff) or (ggb13c >= @fromstaff and ggb13c <= @tostaff) or (ggb14c >= @fromstaff and ggb14c <= @tostaff) or (ggb26c >= @fromstaff and ggb26c <= @tostaff)) and c.gga01c = d.billid group by gda13c update a set a.cnt_t = b.cnt, a.amt_t = b.amt from #st_results a, #st_results_fixorno b where a.type = b.type delete #st_results_fixorno end else if (@printtype = 2)--modify by lyj/2007-08-15 begin insert #st_results(type, cnt, amt, tamt) select s_type = gda13c, cnt = sum(ggb05f), amt = sum(ggb11f), tamt = sum(ggb11f*isnull(ggb34f, 1)) from ggm02 a with (nolock), gdm01 b with (nolock), ggm01 c with (nolock) where a.ggb00c = b.gda00c and a.ggb03c = b.gda01c and a.ggb00c = @compid and a.ggb00c = c.gga00c and a.ggb01c = c.gga01c and c.gga80d >= @fromdate and c.gga80d <= @todate and isnull(gda13c, '')<>'' and ((ggb12c >= @fromstaff and ggb12c <= @tostaff) or (ggb13c >= @fromstaff and ggb13c <= @tostaff) or (ggb14c >= @fromstaff and ggb14c <= @tostaff) or (ggb26c >= @fromstaff and ggb26c <= @tostaff)) group by gda13c --指定部分 insert #st_results_fixorno(type, cnt, amt) select s_type = gda13c, cnt = sum(ggb05f), amt = sum(ggb11f) from ggm02 a with (nolock), gdm01 b with (nolock), ggm01 c with (nolock), #st_bills_fix d where a.ggb00c = b.gda00c and a.ggb03c = b.gda01c and a.ggb00c = @compid and a.ggb00c = c.gga00c and a.ggb01c = c.gga01c and c.gga80d >= @fromdate and c.gga80d <= @todate and isnull(gda13c, '')<>'' and ((ggb12c >= @fromstaff and ggb12c <= @tostaff) or (ggb13c >= @fromstaff and ggb13c <= @tostaff) or (ggb14c >= @fromstaff and ggb14c <= @tostaff) or (ggb26c >= @fromstaff and ggb26c <= @tostaff)) and c.gga01c = d.billid group by gda13c update a set a.cnt_f = b.cnt, a.amt_f = b.amt from #st_results a, #st_results_fixorno b where a.type = b.type delete #st_results_fixorno --轮班部分 insert #st_results_fixorno(type, cnt, amt) select s_type = gda13c, cnt = sum(ggb05f), amt = sum(ggb11f) from ggm02 a with (nolock), gdm01 b with (nolock), ggm01 c with (nolock), #st_bills_nofix d where a.ggb00c = b.gda00c and a.ggb03c = b.gda01c and a.ggb00c = @compid and a.ggb00c = c.gga00c and a.ggb01c = c.gga01c and c.gga80d >= @fromdate and c.gga80d <= @todate and isnull(gda13c, '')<>'' and ((ggb12c >= @fromstaff and ggb12c <= @tostaff) or (ggb13c >= @fromstaff and ggb13c <= @tostaff) or (ggb14c >= @fromstaff and ggb14c <= @tostaff) or (ggb26c >= @fromstaff and ggb26c <= @tostaff)) and c.gga01c = d.billid group by gda13c update a set a.cnt_t = b.cnt, a.amt_t = b.amt from #st_results a, #st_results_fixorno b where a.type = b.type delete #st_results_fixorno end else if @amttype = 1 begin --大工虚业绩 insert #st_results_2(type, cnt, amt) select s_type = gda13c, cnt = sum(ggb05f), amt = sum(isnull(ggb20f, 0)) from ggm02 a with (nolock), gdm01 b with (nolock), ggm01 c with (nolock) where a.ggb00c = b.gda00c and a.ggb03c = b.gda01c and a.ggb00c = @compid and a.ggb00c = c.gga00c and a.ggb01c = c.gga01c and c.gga80d >= @fromdate and c.gga80d <= @todate and isnull(gda13c, '')<>'' and ((ggb12c >= @fromstaff and ggb12c <= @tostaff)) group by gda13c --大工指定 insert #st_results_fixorno_buf(type, cnt_f, amt_f) select s_type = gda13c, cnt = sum(ggb05f), amt = sum(isnull(ggb20f, 0)) from ggm02 a with (nolock), gdm01 b with (nolock), ggm01 c with (nolock), #st_bills_fix d where a.ggb00c = b.gda00c and a.ggb03c = b.gda01c and a.ggb00c = @compid and a.ggb00c = c.gga00c and a.ggb01c = c.gga01c and c.gga80d >= @fromdate and c.gga80d <= @todate and isnull(gda13c, '')<>'' and ((ggb12c >= @fromstaff and ggb12c <= @tostaff)) and c.gga01c = d.billid group by gda13c --大工轮班 insert #st_results_fixorno_buf(type, cnt_t, amt_t) select s_type = gda13c, cnt = sum(ggb05f), amt = sum(isnull(ggb20f, 0)) from ggm02 a with (nolock), gdm01 b with (nolock), ggm01 c with (nolock), #st_bills_nofix d where a.ggb00c = b.gda00c and a.ggb03c = b.gda01c and a.ggb00c = @compid and a.ggb00c = c.gga00c and a.ggb01c = c.gga01c and c.gga80d >= @fromdate and c.gga80d <= @todate and isnull(gda13c, '')<>'' and ((ggb12c >= @fromstaff and ggb12c <= @tostaff)) and c.gga01c = d.billid group by gda13c --中工虚业绩 insert #st_results_2(type, cnt, amt) select s_type = gda13c, cnt = sum(ggb05f), amt = sum(isnull(ggb21f, 0)) from ggm02 a with (nolock), gdm01 b with (nolock), ggm01 c with (nolock) where a.ggb00c = b.gda00c and a.ggb03c = b.gda01c and a.ggb00c = @compid and a.ggb00c = c.gga00c and a.ggb01c = c.gga01c and c.gga80d >= @fromdate and c.gga80d <= @todate and isnull(gda13c, '')<>'' and ((ggb13c >= @fromstaff and ggb13c <= @tostaff)) group by gda13c --中工指定 insert #st_results_fixorno_buf(type, cnt_f, amt_f) select s_type = gda13c, cnt = sum(ggb05f), amt = sum(isnull(ggb21f, 0)) from ggm02 a with (nolock), gdm01 b with (nolock), ggm01 c with (nolock), #st_bills_fix d where a.ggb00c = b.gda00c and a.ggb03c = b.gda01c and a.ggb00c = @compid and a.ggb00c = c.gga00c and a.ggb01c = c.gga01c and c.gga80d >= @fromdate and c.gga80d <= @todate and isnull(gda13c, '')<>'' and ((ggb13c >= @fromstaff and ggb13c <= @tostaff)) and c.gga01c = d.billid group by gda13c --中工轮班 insert #st_results_fixorno_buf(type, cnt_t, amt_t) select s_type = gda13c, cnt = sum(ggb05f), amt = sum(isnull(ggb21f, 0)) from ggm02 a with (nolock), gdm01 b with (nolock), ggm01 c with (nolock), #st_bills_nofix d where a.ggb00c = b.gda00c and a.ggb03c = b.gda01c and a.ggb00c = @compid and a.ggb00c = c.gga00c and a.ggb01c = c.gga01c and c.gga80d >= @fromdate and c.gga80d <= @todate and isnull(gda13c, '')<>'' and ((ggb13c >= @fromstaff and ggb13c <= @tostaff)) and c.gga01c = d.billid group by gda13c --小工虚业绩 insert #st_results_2(type, cnt, amt) select s_type = gda13c, cnt = sum(ggb05f), amt = sum(isnull(ggb22f, 0)) from ggm02 a with (nolock), gdm01 b with (nolock), ggm01 c with (nolock) where a.ggb00c = b.gda00c and a.ggb03c = b.gda01c and a.ggb00c = @compid and a.ggb00c = c.gga00c and a.ggb01c = c.gga01c and c.gga80d >= @fromdate and c.gga80d <= @todate and isnull(gda13c, '')<>'' and ((ggb14c >= @fromstaff and ggb14c <= @tostaff)) group by gda13c --小工指定 insert #st_results_fixorno_buf(type, cnt_f, amt_f) select s_type = gda13c, cnt = sum(ggb05f), amt = sum(isnull(ggb22f, 0)) from ggm02 a with (nolock), gdm01 b with (nolock), ggm01 c with (nolock), #st_bills_fix d where a.ggb00c = b.gda00c and a.ggb03c = b.gda01c and a.ggb00c = @compid and a.ggb00c = c.gga00c and a.ggb01c = c.gga01c and c.gga80d >= @fromdate and c.gga80d <= @todate and isnull(gda13c, '')<>'' and ((ggb14c >= @fromstaff and ggb14c <= @tostaff)) and c.gga01c = d.billid group by gda13c --小工轮班 insert #st_results_fixorno_buf(type, cnt_t, amt_t) select s_type = gda13c, cnt = sum(ggb05f), amt = sum(isnull(ggb22f, 0)) from ggm02 a with (nolock), gdm01 b with (nolock), ggm01 c with (nolock), #st_bills_nofix d where a.ggb00c = b.gda00c and a.ggb03c = b.gda01c and a.ggb00c = @compid and a.ggb00c = c.gga00c and a.ggb01c = c.gga01c and c.gga80d >= @fromdate and c.gga80d <= @todate and isnull(gda13c, '')<>'' and ((ggb14c >= @fromstaff and ggb14c <= @tostaff)) and c.gga01c = d.billid group by gda13c --四工虚业绩 insert #st_results_2(type, cnt, amt) select s_type = gda13c, cnt = sum(ggb05f), amt = sum(isnull(ggb35f, 0)) from ggm02 a with (nolock), gdm01 b with (nolock), ggm01 c with (nolock) where a.ggb00c = b.gda00c and a.ggb03c = b.gda01c and a.ggb00c = @compid and a.ggb00c = c.gga00c and a.ggb01c = c.gga01c and c.gga80d >= @fromdate and c.gga80d <= @todate and isnull(gda13c, '')<>'' and ((ggb26c >= @fromstaff and ggb26c <= @tostaff)) group by gda13c insert #st_results_fixorno_buf(type, cnt_f, amt_f) select s_type = gda13c, cnt = sum(ggb05f), amt = sum(isnull(ggb35f, 0)) from ggm02 a with (nolock), gdm01 b with (nolock), ggm01 c with (nolock), #st_bills_fix d where a.ggb00c = b.gda00c and a.ggb03c = b.gda01c and a.ggb00c = @compid and a.ggb00c = c.gga00c and a.ggb01c = c.gga01c and c.gga80d >= @fromdate and c.gga80d <= @todate and isnull(gda13c, '')<>'' and ((ggb26c >= @fromstaff and ggb26c <= @tostaff)) and c.gga01c = d.billid group by gda13c insert #st_results_fixorno_buf(type, cnt_t, amt_t) select s_type = gda13c, cnt = sum(ggb05f), amt = sum(isnull(ggb35f, 0)) from ggm02 a with (nolock), gdm01 b with (nolock), ggm01 c with (nolock), #st_bills_nofix d where a.ggb00c = b.gda00c and a.ggb03c = b.gda01c and a.ggb00c = @compid and a.ggb00c = c.gga00c and a.ggb01c = c.gga01c and c.gga80d >= @fromdate and c.gga80d <= @todate and isnull(gda13c, '')<>'' and ((ggb26c >= @fromstaff and ggb26c <= @tostaff)) and c.gga01c = d.billid group by gda13c insert #st_results_fixorno_grp(type, cnt_f, amt_f, cnt_t, amt_t) select type, sum(cnt_f), sum(amt_f), sum(cnt_t), sum(amt_t) from #st_results_fixorno_buf group by type insert #st_results(type, cnt, amt) select type, sum(isnull(cnt, 0)), sum(isnull(amt, 0)) from #st_results_2 group by type update a set a.cnt_f = b.cnt_f, a.amt_f = b.amt_f, a.cnt_t = b.cnt_t, a.amt_t = b.amt_t from #st_results a, #st_results_fixorno_grp b where a.type = b.type end else if @amttype = 3 begin --大工实业绩 insert #st_results_2(type, cnt, amt) select s_type = gda13c, cnt = sum(ggb05f), amt = sum(isnull(ggb36f, 0)) from ggm02 a with (nolock), gdm01 b with (nolock), ggm01 c with (nolock) where a.ggb00c = b.gda00c and a.ggb03c = b.gda01c and a.ggb00c = @compid and a.ggb00c = c.gga00c and a.ggb01c = c.gga01c and c.gga80d >= @fromdate and c.gga80d <= @todate and isnull(gda13c, '')<>'' and ((ggb12c >= @fromstaff and ggb12c <= @tostaff)) group by gda13c insert #st_results_fixorno_buf(type, cnt_f, amt_f) select s_type = gda13c, cnt = sum(ggb05f), amt = sum(isnull(ggb36f, 0)) from ggm02 a with (nolock), gdm01 b with (nolock), ggm01 c with (nolock), #st_bills_fix d where a.ggb00c = b.gda00c and a.ggb03c = b.gda01c and a.ggb00c = @compid and a.ggb00c = c.gga00c and a.ggb01c = c.gga01c and c.gga80d >= @fromdate and c.gga80d <= @todate and isnull(gda13c, '')<>'' and ((ggb12c >= @fromstaff and ggb12c <= @tostaff)) and c.gga01c = d.billid group by gda13c insert #st_results_fixorno_buf(type, cnt_t, amt_t) select s_type = gda13c, cnt = sum(ggb05f), amt = sum(isnull(ggb36f, 0)) from ggm02 a with (nolock), gdm01 b with (nolock), ggm01 c with (nolock), #st_bills_nofix d where a.ggb00c = b.gda00c and a.ggb03c = b.gda01c and a.ggb00c = @compid and a.ggb00c = c.gga00c and a.ggb01c = c.gga01c and c.gga80d >= @fromdate and c.gga80d <= @todate and isnull(gda13c, '')<>'' and ((ggb12c >= @fromstaff and ggb12c <= @tostaff)) and c.gga01c = d.billid group by gda13c --中工实业绩 insert #st_results_2(type, cnt, amt) select s_type = gda13c, cnt = sum(ggb05f), amt = sum(isnull(ggb37f, 0)) from ggm02 a with (nolock), gdm01 b with (nolock), ggm01 c with (nolock) where a.ggb00c = b.gda00c and a.ggb03c = b.gda01c and a.ggb00c = @compid and a.ggb00c = c.gga00c and a.ggb01c = c.gga01c and c.gga80d >= @fromdate and c.gga80d <= @todate and isnull(gda13c, '')<>'' and ((ggb13c >= @fromstaff and ggb13c <= @tostaff)) group by gda13c insert #st_results_fixorno_buf(type, cnt_f, amt_f) select s_type = gda13c, cnt = sum(ggb05f), amt = sum(isnull(ggb37f, 0)) from ggm02 a with (nolock), gdm01 b with (nolock), ggm01 c with (nolock), #st_bills_fix d where a.ggb00c = b.gda00c and a.ggb03c = b.gda01c and a.ggb00c = @compid and a.ggb00c = c.gga00c and a.ggb01c = c.gga01c and c.gga80d >= @fromdate and c.gga80d <= @todate and isnull(gda13c, '')<>'' and ((ggb13c >= @fromstaff and ggb13c <= @tostaff)) and c.gga01c = d.billid group by gda13c insert #st_results_fixorno_buf(type, cnt_t, amt_t) select s_type = gda13c, cnt = sum(ggb05f), amt = sum(isnull(ggb37f, 0)) from ggm02 a with (nolock), gdm01 b with (nolock), ggm01 c with (nolock), #st_bills_nofix d where a.ggb00c = b.gda00c and a.ggb03c = b.gda01c and a.ggb00c = @compid and a.ggb00c = c.gga00c and a.ggb01c = c.gga01c and c.gga80d >= @fromdate and c.gga80d <= @todate and isnull(gda13c, '')<>'' and ((ggb13c >= @fromstaff and ggb13c <= @tostaff)) and c.gga01c = d.billid group by gda13c --小工实业绩 insert #st_results_2(type, cnt, amt) select s_type = gda13c, cnt = sum(ggb05f), amt = sum(isnull(ggb38f, 0)) from ggm02 a with (nolock), gdm01 b with (nolock), ggm01 c with (nolock) where a.ggb00c = b.gda00c and a.ggb03c = b.gda01c and a.ggb00c = @compid and a.ggb00c = c.gga00c and a.ggb01c = c.gga01c and c.gga80d >= @fromdate and c.gga80d <= @todate and isnull(gda13c, '')<>'' and ((ggb14c >= @fromstaff and ggb14c <= @tostaff)) group by gda13c insert #st_results_fixorno_buf(type, cnt_f, amt_f) select s_type = gda13c, cnt = sum(ggb05f), amt = sum(isnull(ggb38f, 0)) from ggm02 a with (nolock), gdm01 b with (nolock), ggm01 c with (nolock), #st_bills_fix d where a.ggb00c = b.gda00c and a.ggb03c = b.gda01c and a.ggb00c = @compid and a.ggb00c = c.gga00c and a.ggb01c = c.gga01c and c.gga80d >= @fromdate and c.gga80d <= @todate and isnull(gda13c, '')<>'' and ((ggb14c >= @fromstaff and ggb14c <= @tostaff)) and c.gga01c = d.billid group by gda13c insert #st_results_fixorno_buf(type, cnt_t, amt_t) select s_type = gda13c, cnt = sum(ggb05f), amt = sum(isnull(ggb38f, 0)) from ggm02 a with (nolock), gdm01 b with (nolock), ggm01 c with (nolock), #st_bills_nofix d where a.ggb00c = b.gda00c and a.ggb03c = b.gda01c and a.ggb00c = @compid and a.ggb00c = c.gga00c and a.ggb01c = c.gga01c and c.gga80d >= @fromdate and c.gga80d <= @todate and isnull(gda13c, '')<>'' and ((ggb14c >= @fromstaff and ggb14c <= @tostaff)) and c.gga01c = d.billid group by gda13c --四工实业绩 insert #st_results_2(type, cnt, amt) select s_type = gda13c, cnt = sum(ggb05f), amt = sum(isnull(ggb39f, 0)) from ggm02 a with (nolock), gdm01 b with (nolock), ggm01 c with (nolock) where a.ggb00c = b.gda00c and a.ggb03c = b.gda01c and a.ggb00c = @compid and a.ggb00c = c.gga00c and a.ggb01c = c.gga01c and c.gga80d >= @fromdate and c.gga80d <= @todate and isnull(gda13c, '')<>'' and ((ggb26c >= @fromstaff and ggb26c <= @tostaff)) group by gda13c insert #st_results_fixorno_buf(type, cnt_f, amt_f) select s_type = gda13c, cnt = sum(ggb05f), amt = sum(isnull(ggb39f, 0)) from ggm02 a with (nolock), gdm01 b with (nolock), ggm01 c with (nolock), #st_bills_fix d where a.ggb00c = b.gda00c and a.ggb03c = b.gda01c and a.ggb00c = @compid and a.ggb00c = c.gga00c and a.ggb01c = c.gga01c and c.gga80d >= @fromdate and c.gga80d <= @todate and isnull(gda13c, '')<>'' and ((ggb26c >= @fromstaff and ggb26c <= @tostaff)) and c.gga01c = d.billid group by gda13c insert #st_results_fixorno_buf(type, cnt_t, amt_t) select s_type = gda13c, cnt = sum(ggb05f), amt = sum(isnull(ggb39f, 0)) from ggm02 a with (nolock), gdm01 b with (nolock), ggm01 c with (nolock), #st_bills_nofix d where a.ggb00c = b.gda00c and a.ggb03c = b.gda01c and a.ggb00c = @compid and a.ggb00c = c.gga00c and a.ggb01c = c.gga01c and c.gga80d >= @fromdate and c.gga80d <= @todate and isnull(gda13c, '')<>'' and ((ggb26c >= @fromstaff and ggb26c <= @tostaff)) and c.gga01c = d.billid group by gda13c insert #st_results_fixorno_grp(type, cnt_f, amt_f, cnt_t, amt_t) select type, sum(cnt_f), sum(amt_f), sum(cnt_t), sum(amt_t) from #st_results_fixorno_buf group by type insert #st_results(type, cnt, amt) select type, sum(isnull(cnt, 0)), sum(isnull(amt, 0)) from #st_results_2 group by type update a set a.cnt_f = b.cnt_f, a.amt_f = b.amt_f, a.cnt_t = b.cnt_t, a.amt_t = b.amt_t from #st_results a, #st_results_fixorno_grp b where a.type = b.type end end drop table #st_results_fixorno drop table #st_results_fixorno_buf drop table #st_results_fixorno_grp update #st_results set price = isnull(amt, 0)/isnull(cnt, 1) where cnt <> 0.0 update #st_results set price = 0 where cnt = 0.0 select @card_total_amt = 0 --卡销售 if isnull(@fromstaff, '') = '' or isnull(@fromstaff, '') = '*' begin if (@amttype<>1 and @amttype<>3) begin select @card_total_amt = sum(gnb08f) from gnm02 b with (nolock), gnm01 a with (nolock) where b.gnb00c = a.gna00c and b.gnb01c = a.gna01c and a.gna00c = @compid and a.gna80d >= @fromdate and a.gna80d <= @todate insert #st_results(type, amt) values('卡销售', @card_total_amt) --加上充值金额 select @tmp_amt = sum(gcl07f) from gcm10 b with (nolock) where gcl00c = @compid and gcl80d >= @fromdate and gcl80d <= @todate and (gcl06i = 0 or gcl06i = 6) select @card_total_amt = isnull(@card_total_amt, 0) + isnull(@tmp_amt, 0) select @tmp_amt = sum(gcl07f) from gcm10 with (nolock) where gcl00c = @compid and gcl80d >= @fromdate and gcl80d <= @todate and gcl06i = 5 select @card_total_amt = isnull(@card_total_amt, 0) - isnull(@tmp_amt, 0) insert #st_results(type, amt) values('99-0', @card_total_amt) end else begin select @card_total_amt = sum(isnull(gna67f, 0) + isnull(gna69f, 0) + isnull(gna71f, 0) + isnull(gna86f, 0) + isnull(gna87f, 0) + isnull(gna88f, 0) + isnull(gna89f, 0) + isnull(gna90f, 0) + isnull(gna91f, 0) + isnull(gna92f, 0)) from gnm01 a with (nolock) where a.gna00c = @compid and a.gna80d >= @fromdate and a.gna80d <= @todate insert #st_results(type, amt) values('卡销售', @card_total_amt) --加上充值金额 select @tmp_amt = sum(isnull(gcl58f, 0) + isnull(gcl60f, 0) + isnull(gcl62f, 0) + isnull(gcl83f, 0) + isnull(gcl84f, 0) + isnull(gcl85f, 0) + isnull(gcl86f, 0) + isnull(gcl87f, 0) + isnull(gcl88f, 0) + isnull(gcl89f, 0)) from gcm10 b with (nolock) where gcl00c = @compid and gcl80d >= @fromdate and gcl80d <= @todate and (gcl06i = 0 or gcl06i = 6) select @card_total_amt = isnull(@card_total_amt, 0) + isnull(@tmp_amt, 0) select @tmp_amt = sum(isnull(gcl58f, 0) + isnull(gcl60f, 0) + isnull(gcl62f, 0) + isnull(gcl83f, 0) + isnull(gcl84f, 0) + isnull(gcl85f, 0) + isnull(gcl86f, 0) + isnull(gcl87f, 0) + isnull(gcl88f, 0) + isnull(gcl89f, 0)) from gcm10 with (nolock) where gcl00c = @compid and gcl80d >= @fromdate and gcl80d <= @todate and gcl06i = 5 select @card_total_amt = isnull(@card_total_amt, 0) - isnull(@tmp_amt, 0) insert #st_results(type, amt) values('99-0', @card_total_amt) end -- 用卡 insert #st_results(type, amt) select '99-1', sum(gsc05f) from gsm03 a with (nolock), #st_bills b, pay_classify g with (nolock) where a.gsc01c = b.billid and a.gsc02c = 'gx' and a.gsc00c = @compid and g.comp = @compid and g.pay = a.gsc04c and g.category = 2 --外卖 if @amttype = 0 or @amttype = 2 begin insert #st_results(type, amt) select '99-2', sum(ggc11f) from ggm03 b with (nolock), ggm01 a with (nolock) where a.gga00c = b.ggc00c and a.gga01c = b.ggc01c and a.gga80d >= @fromdate and a.gga80d <= @todate and a.gga00c = @compid end else if @amttype = 1 begin insert #st_results(type, amt) select '99-2', sum(isnull(ggc21f, 0) + isnull(ggc23f, 0) + isnull(ggc25f, 0)) from ggm03 b with (nolock), ggm01 a with (nolock) where a.gga00c = b.ggc00c and a.gga01c = b.ggc01c and a.gga80d >= @fromdate and a.gga80d <= @todate and a.gga00c = @compid end else if @amttype = 3 begin insert #st_results(type, amt) select '99-2', sum(isnull(ggc21f, 0) + isnull(ggc23f, 0) + isnull(ggc25f, 0) - isnull(ggc13f, 0)) -- 成本到底应该扣几个成本额呢 from ggm03 b with (nolock), ggm01 a with (nolock) where a.gga00c = b.ggc00c and a.gga01c = b.ggc01c and a.gga80d >= @fromdate and a.gga80d <= @todate and a.gga00c = @compid end end else begin -- 员工 -- 0 营业额 1 虚业绩 2-数量 3-实业绩 --卖卡 --modified by lyj for 2008-01-09 虚业绩现在通过单据取得 --员工统计gnm01 和gcm10的时候只统计了3个人员现在统计了10个人员 if (@amttype<>1 and @amttype<>3) begin select @card_total_amt = sum(gnb08f) from gnm02 b with (nolock), gnm01 a with (nolock) where b.gnb00c = a.gna00c and b.gnb01c = a.gna01c and a.gna00c = @compid and a.gna80d >= @fromdate and a.gna80d <= @todate and ((a.gna20c >= @fromstaff and a.gna20c <= @tostaff) or (gna28c >= @fromstaff and gna28c <= @tostaff) or (gna30c >= @fromstaff and gna30c <= @tostaff) or (gna32c >= @fromstaff and gna32c <= @tostaff) or (gna34c >= @fromstaff and gna34c <= @tostaff) or (gna36c >= @fromstaff and gna36c <= @tostaff) or (gna38c >= @fromstaff and gna38c <= @tostaff) or (gna40c >= @fromstaff and gna40c <= @tostaff) or (gna42c >= @fromstaff and gna42c <= @tostaff) or (gna44c >= @fromstaff and gna44c <= @tostaff)) --加上充值金额 select @tmp_amt = sum(gcl07f) from gcm10 b with (nolock) where gcl00c = @compid and gcl80d >= @fromdate and gcl80d <= @todate and (gcl06i = 0 or gcl06i = 6) and ((gcl09c >= @fromstaff and gcl09c <= @tostaff) or (gcl17c >= @fromstaff and gcl17c <= @tostaff) or (gcl19c >= @fromstaff and gcl19c <= @tostaff) or (gcl22c >= @fromstaff and gcl22c <= @tostaff) or (gcl24c >= @fromstaff and gcl24c <= @tostaff) or (gcl26c >= @fromstaff and gcl26c <= @tostaff) or (gcl28c >= @fromstaff and gcl28c <= @tostaff) or (gcl30c >= @fromstaff and gcl30c <= @tostaff) or (gcl32c >= @fromstaff and gcl32c <= @tostaff) or (gcl34c >= @fromstaff and gcl34c <= @tostaff) ) select @card_total_amt = isnull(@card_total_amt, 0) + isnull(@tmp_amt, 0) select @tmp_amt = sum(gcl07f) from gcm10 with (nolock) where gcl00c = @compid and gcl80d >= @fromdate and gcl80d <= @todate and gcl06i = 5 and ((gcl09c >= @fromstaff and gcl09c <= @tostaff) or (gcl17c >= @fromstaff and gcl17c <= @tostaff) or (gcl19c >= @fromstaff and gcl19c <= @tostaff) or (gcl22c >= @fromstaff and gcl22c <= @tostaff) or (gcl24c >= @fromstaff and gcl24c <= @tostaff) or (gcl26c >= @fromstaff and gcl26c <= @tostaff) or (gcl28c >= @fromstaff and gcl28c <= @tostaff) or (gcl30c >= @fromstaff and gcl30c <= @tostaff) or (gcl32c >= @fromstaff and gcl32c <= @tostaff) or (gcl34c >= @fromstaff and gcl34c <= @tostaff) ) select @card_total_amt = isnull(@card_total_amt, 0) - isnull(@tmp_amt, 0) insert #st_results(type, amt) values('99-0', @card_total_amt) end else begin create table #gnm01 ( gna20c varchar(20) null, gna28c varchar(20) null, gna30c varchar(20) null, gna32c varchar(20) null, gna34c varchar(20) null, gna36c varchar(20) null, gna38c varchar(20) null, gna40c varchar(20) null, gna42c varchar(20) null, gna44c varchar(20) null, gna67f float null, gna69f float null, gna71f float null, gna86f float null, gna87f float null, gna88f float null, gna89f float null, gna90f float null, gna91f float null, gna92f float null, ) insert #gnm01(gna20c, gna28c, gna30c, gna32c, gna34c, gna36c, gna38c, gna40c, gna42c, gna44c, gna67f, gna69f, gna71f, gna86f, gna87f, gna88f, gna89f, gna90f, gna91f, gna92f) select gna20c, gna28c, gna30c, gna32c, gna34c, gna36c, gna38c, gna40c, gna42c, gna44c, gna67f, gna69f, gna71f, gna86f, gna87f, gna88f, gna89f, gna90f, gna91f, gna92f from gnm01 a with (nolock) where a.gna00c = @compid and a.gna80d >= @fromdate and a.gna80d <= @todate and ((a.gna20c >= @fromstaff and a.gna20c <= @tostaff) or (gna28c >= @fromstaff and gna28c <= @tostaff) or (gna30c >= @fromstaff and gna30c <= @tostaff) or (gna32c >= @fromstaff and gna32c <= @tostaff) or (gna34c >= @fromstaff and gna34c <= @tostaff) or (gna36c >= @fromstaff and gna36c <= @tostaff) or (gna38c >= @fromstaff and gna38c <= @tostaff) or (gna40c >= @fromstaff and gna40c <= @tostaff) or (gna42c >= @fromstaff and gna42c <= @tostaff) or (gna44c >= @fromstaff and gna44c <= @tostaff)) declare @verp01 float declare @verp02 float declare @verp03 float declare @verp04 float declare @verp05 float declare @verp06 float declare @verp07 float declare @verp08 float declare @verp09 float declare @verp10 float select @verp01 = sum(isnull(gna67f, 0)) from #gnm01 where gna20c >= @fromstaff and gna20c <= @tostaff group by gna20c select @verp02 = sum(isnull(gna69f, 0)) from #gnm01 where gna28c >= @fromstaff and gna28c <= @tostaff group by gna28c select @verp03 = sum(isnull(gna71f, 0)) from #gnm01 where gna30c >= @fromstaff and gna30c <= @tostaff group by gna30c select @verp04 = sum(isnull(gna86f, 0)) from #gnm01 where gna32c >= @fromstaff and gna32c <= @tostaff group by gna32c select @verp05 = sum(isnull(gna87f, 0)) from #gnm01 where gna34c >= @fromstaff and gna34c <= @tostaff group by gna34c select @verp06 = sum(isnull(gna88f, 0)) from #gnm01 where gna36c >= @fromstaff and gna36c <= @tostaff group by gna36c select @verp07 = sum(isnull(gna89f, 0)) from #gnm01 where gna38c >= @fromstaff and gna38c <= @tostaff group by gna38c select @verp08 = sum(isnull(gna90f, 0)) from #gnm01 where gna40c >= @fromstaff and gna40c <= @tostaff group by gna40c select @verp09 = sum(isnull(gna91f, 0)) from #gnm01 where gna42c >= @fromstaff and gna42c <= @tostaff group by gna42c select @verp10 = sum(isnull(gna92f, 0)) from #gnm01 where gna44c >= @fromstaff and gna44c <= @tostaff group by gna44c set @card_total_amt = isnull(@verp01, 0) + isnull(@verp02, 0) + isnull(@verp03, 0) + isnull(@verp04, 0) + isnull(@verp05, 0) + isnull(@verp06, 0) + isnull(@verp07, 0) + isnull(@verp08, 0) + isnull(@verp09, 0) + isnull(@verp10, 0) set @verp01 = 0 set @verp02 = 0 set @verp03 = 0 set @verp04 = 0 set @verp05 = 0 set @verp06 = 0 set @verp07 = 0 set @verp08 = 0 set @verp09 = 0 set @verp10 = 0 --加上充值金额 create table #gcm10 ( gcl09c varchar(20) null, gcl17c varchar(20) null, gcl19c varchar(20) null, gcl22c varchar(20) null, gcl24c varchar(20) null, gcl26c varchar(20) null, gcl28c varchar(20) null, gcl30c varchar(20) null, gcl32c varchar(20) null, gcl34c varchar(20) null, gcl58f float null, gcl60f float null, gcl62f float null, gcl83f float null, gcl84f float null, gcl85f float null, gcl86f float null, gcl87f float null, gcl88f float null, gcl89f float null, ) insert #gcm10(gcl09c, gcl17c, gcl19c, gcl22c, gcl24c, gcl26c, gcl28c, gcl30c, gcl32c, gcl34c, gcl58f, gcl60f, gcl62f, gcl83f, gcl84f, gcl85f, gcl86f, gcl87f, gcl88f, gcl89f) select gcl09c, gcl17c, gcl19c, gcl22c, gcl24c, gcl26c, gcl28c, gcl30c, gcl32c, gcl34c, gcl58f, gcl60f, gcl62f, gcl83f, gcl84f, gcl85f, gcl86f, gcl87f, gcl88f, gcl89f from gcm10 with (nolock) where gcl00c = @compid and gcl80d >= @fromdate and gcl80d <= @todate and (gcl06i = 0 or gcl06i = 5 or gcl06i = 6) and ((gcl09c >= @fromstaff and gcl09c <= @tostaff) or (gcl17c >= @fromstaff and gcl17c <= @tostaff) or (gcl19c >= @fromstaff and gcl19c <= @tostaff) or (gcl22c >= @fromstaff and gcl22c <= @tostaff) or (gcl24c >= @fromstaff and gcl24c <= @tostaff) or (gcl26c >= @fromstaff and gcl26c <= @tostaff) or (gcl28c >= @fromstaff and gcl28c <= @tostaff) or (gcl30c >= @fromstaff and gcl30c <= @tostaff) or (gcl32c >= @fromstaff and gcl32c <= @tostaff) or (gcl34c >= @fromstaff and gcl34c <= @tostaff) ) select @verp01 = sum(isnull(gcl58f, 0)) from #gcm10 where gcl09c >= @fromstaff and gcl09c <= @tostaff group by gcl09c select @verp02 = sum(isnull(gcl60f, 0)) from #gcm10 where gcl17c >= @fromstaff and gcl17c <= @tostaff group by gcl17c select @verp03 = sum(isnull(gcl62f, 0)) from #gcm10 where gcl19c >= @fromstaff and gcl19c <= @tostaff group by gcl19c select @verp04 = sum(isnull(gcl83f, 0)) from #gcm10 where gcl22c >= @fromstaff and gcl22c <= @tostaff group by gcl22c select @verp05 = sum(isnull(gcl84f, 0)) from #gcm10 where gcl24c >= @fromstaff and gcl24c <= @tostaff group by gcl24c select @verp06 = sum(isnull(gcl85f, 0)) from #gcm10 where gcl26c >= @fromstaff and gcl26c <= @tostaff group by gcl26c select @verp07 = sum(isnull(gcl86f, 0)) from #gcm10 where gcl28c >= @fromstaff and gcl28c <= @tostaff group by gcl28c select @verp08 = sum(isnull(gcl87f, 0)) from #gcm10 where gcl30c >= @fromstaff and gcl30c <= @tostaff group by gcl30c select @verp09 = sum(isnull(gcl88f, 0)) from #gcm10 where gcl32c >= @fromstaff and gcl32c <= @tostaff group by gcl32c select @verp10 = sum(isnull(gcl88f, 0)) from #gcm10 where gcl34c >= @fromstaff and gcl34c <= @tostaff group by gcl34c set @tmp_amt = @verp01 + @verp02 + @verp03 + @verp04 + @verp05 + @verp06 + @verp07 + @verp08 + @verp09 + @verp10 select @card_total_amt = isnull(@card_total_amt, 0) + isnull(@tmp_amt, 0) insert #st_results(type, amt) values('99-0', @card_total_amt) end --用卡 --也需要计算到员工,但按单据支付是没有办法统计的, --故按单据支付保持原来的情况 declare @sp055f float select @sp055f = gsa03f from gsm01 with (nolock) where gsa00c = @compid and gsa01c = 'SP055' if @sp055f = 1 -- 单据支付 begin insert #st_results(type, amt) select '99-1', sum(gsc05f) from gsm03 a with (nolock), #st_bills b, pay_classify g with (nolock) where a.gsc01c = b.billid and a.gsc02c = 'gx' and a.gsc00c = @compid and g.comp = @compid and g.pay = a.gsc04c and g.category = 2 end else if @sp055f = 0 -- 项目支付 begin declare @srv_use_card float declare @good_use_card float select @srv_use_card = sum(ggb11f) from ggm02 a with (nolock), #st_bills b, pay_classify g with (nolock) where ggb00c = @compid and g.comp = @compid and g.pay = ggb27c and g.category = 2 and a.ggb01c = b.billid and ((ggb12c >= @fromstaff and ggb12c <= @tostaff) or (ggb13c >= @fromstaff and ggb13c <= @tostaff) or (ggb14c >= @fromstaff and ggb14c <= @tostaff) or (ggb26c >= @fromstaff and ggb26c <= @tostaff)) select @good_use_card = sum(ggc11f) from ggm03 a with (nolock), #st_bills b, pay_classify g with (nolock) where ggc00c = @compid and g.comp = @compid and g.pay = ggc27c and g.category = 2 and a.ggc01c = b.billid and ((ggc12c >= @fromstaff and ggc12c <= @tostaff) or (ggc15c >= @fromstaff and ggc15c <= @tostaff) or (ggc17c >= @fromstaff and ggc17c <= @tostaff)) insert #st_results(type, amt) values( '99-1', isnull(@srv_use_card, 0) + isnull(@good_use_card, 0)) end --外卖 -- 0 营业额 1 虚业绩 2-数量 3-实业绩 if @amttype = 0 or @amttype = 2 begin insert #st_results(type, amt) select '99-2', sum(ggc11f) from ggm03 b with (nolock), ggm01 a with (nolock) where a.gga00c = b.ggc00c and a.gga01c = b.ggc01c and a.gga80d >= @fromdate and a.gga80d <= @todate and a.gga00c = @compid and ((ggc12c >= @fromstaff and ggc12c <= @tostaff) or (ggc15c >= @fromstaff and ggc15c <= @tostaff) or (ggc17c >= @fromstaff and ggc17c <= @tostaff)) end else if @amttype = 1 begin declare @vperf_total_1 float declare @vperf_total_2 float declare @vperf_total_3 float select @vperf_total_1 = sum(isnull(ggc21f, 0)) from ggm03 b with (nolock), ggm01 a with (nolock) where a.gga00c = b.ggc00c and a.gga01c = b.ggc01c and a.gga80d >= @fromdate and a.gga80d <= @todate and a.gga00c = @compid and ((ggc12c >= @fromstaff and ggc12c <= @tostaff)) select @vperf_total_2 = sum(isnull(ggc23f, 0)) from ggm03 b with (nolock), ggm01 a with (nolock) where a.gga00c = b.ggc00c and a.gga01c = b.ggc01c and a.gga80d >= @fromdate and a.gga80d <= @todate and a.gga00c = @compid and ((ggc15c >= @fromstaff and ggc15c <= @tostaff)) select @vperf_total_3 = sum(isnull(ggc25f, 0)) from ggm03 b with (nolock), ggm01 a with (nolock) where a.gga00c = b.ggc00c and a.gga01c = b.ggc01c and a.gga80d >= @fromdate and a.gga80d <= @todate and a.gga00c = @compid and ((ggc17c >= @fromstaff and ggc17c <= @tostaff)) insert #st_results(type, amt) values( '99-2', isnull(@vperf_total_1, 0) + isnull(@vperf_total_2, 0) + isnull(@vperf_total_3, 0) ) end else if @amttype = 3 begin declare @tperf_total_1 float declare @tperf_total_2 float declare @tperf_total_3 float select @tperf_total_1 = sum(isnull(ggc21f, 0)-isnull(ggc13f, 0)) from ggm03 b with (nolock), ggm01 a with (nolock) where a.gga00c = b.ggc00c and a.gga01c = b.ggc01c and a.gga80d >= @fromdate and a.gga80d <= @todate and a.gga00c = @compid and ((ggc12c >= @fromstaff and ggc12c <= @tostaff)) select @tperf_total_2 = sum(isnull(ggc23f, 0)-isnull(ggc13f, 0)) from ggm03 b with (nolock), ggm01 a with (nolock) where a.gga00c = b.ggc00c and a.gga01c = b.ggc01c and a.gga80d >= @fromdate and a.gga80d <= @todate and a.gga00c = @compid and ((ggc15c >= @fromstaff and ggc15c <= @tostaff)) select @tperf_total_3 = sum(isnull(ggc25f, 0)-isnull(ggc13f, 0)) from ggm03 b with (nolock), ggm01 a with (nolock) where a.gga00c = b.ggc00c and a.gga01c = b.ggc01c and a.gga80d >= @fromdate and a.gga80d <= @todate and a.gga00c = @compid and ((ggc17c >= @fromstaff and ggc17c <= @tostaff)) insert #st_results(type, amt) values( '99-2', isnull(@tperf_total_1, 0) + isnull(@tperf_total_2, 0) + isnull(@tperf_total_3, 0)) end end declare @amt_99_0 float select @amt_99_0 = amt from #st_results where type = '99-0' update a set a.amt = isnull(a.amt,0) + isnull(@amt_99_0,0) from #st_results a where type = '0-0' select week = case (datepart(dw, cast(@fromdate as datetime))) when 1 then '星期日' when 2 then '星期一' when 3 then '星期二' when 4 then '星期三' when 5 then '星期四' when 6 then '星期五' when 7 then '星期六' end, isnull(type,'') type, isnull(cnt,0) cnt, isnull(amt,0) amt, isnull(price,0) price , isnull(cnt_f,0) cnt_f, isnull(amt_f,0) amt_f, isnull(cnt_t,0)cnt_t , isnull(amt_t,0) amt_t, isnull(tamt,0) tamt from #st_results order by type drop table #st_bills drop table #st_bills_fix drop table #st_bills_nofix drop table #st_results drop table #st_results_2 end GO /****** Object: StoredProcedure [dbo].[upg_updatedepart_all] Script Date: 06/12/2017 06:20:51 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_updatedepart_all] as begin -- update gnm01 因为大工是必须输入的,所以只要更新大工就可以了 -- 第1销售的部门 update a set a.gna81c = b.haa06c from gnm01 a, ham01 b where a.gna00c = b.haa00c and b.haa01c = a.gna20c and isnull(gna81c, '') = '' and isnull(gna20c, '') <> '' -- 第2销售的部门 update a set a.gna82c = b.haa06c from gnm01 a, ham01 b where a.gna00c = b.haa00c and b.haa01c = a.gna28c and isnull(gna82c, '') = '' and isnull(gna28c, '') <> '' -- 第3销售的部门 update a set a.gna83c = b.haa06c from gnm01 a, ham01 b where a.gna00c = b.haa00c and b.haa01c = a.gna30c and isnull(gna83c, '') = '' and isnull(gna30c, '') <> '' -- update gcm10 因为大工是必须输入的,所以只要更新大工就可以了 -- 第1销售的部门 update a set a.gcl81c = b.haa06c from gcm10 a, ham01 b where a.gcl00c = b.haa00c and a.gcl09c = b.haa01c and isnull(gcl81c, '') = '' and isnull(gcl09c, '') <> '' -- 第2销售的部门 update a set a.gcl82c = b.haa06c from gcm10 a, ham01 b where a.gcl00c = b.haa00c and a.gcl17c = b.haa01c and isnull(gcl82c, '') = '' and isnull(gcl17c, '') <> '' -- 第3销售的部门 update a set a.gcl83c = b.haa06c from gcm10 a, ham01 b where a.gcl00c = b.haa00c and a.gcl19c = b.haa01c and isnull(gcl83c, '') = '' and isnull(gcl19c, '') <> '' -- update ggm02, 用第一员工来判断 update a set a.ggb81c = b.haa06c from ggm02 a, ham01 b where a.ggb00c = b.haa00c and a.ggb12c = b.haa01c and isnull(ggb81c, '') = '' and isnull(ggb12c, '') <> '' -- 可能没有第1员工, 则用第2员工来判断 update a set a.ggb81c = b.haa06c from ggm02 a, ham01 b where a.ggb00c = b.haa00c and a.ggb13c = b.haa01c and isnull(ggb81c, '') = '' and isnull(ggb13c, '') <> '' update a set a.ggb81c = b.haa06c from ggm02 a, ham01 b where a.ggb00c = b.haa00c and a.ggb14c = b.haa01c and isnull(ggb81c, '') = '' and isnull(ggb14c, '') <> '' update a set a.ggb81c = b.haa06c from ggm02 a, ham01 b where a.ggb00c = b.haa00c and a.ggb26c = b.haa01c and isnull(ggb81c, '') = '' and isnull(ggb26c, '') <> '' update a set a.ggc81c = b.haa06c from ggm03 a, ham01 b where a.ggc00c = b.haa00c and a.ggc12c = b.haa01c and isnull(ggc81c, '') = '' and isnull(ggc12c, '') <> '' update a set a.ggc81c = b.haa06c from ggm03 a, ham01 b where a.ggc00c = b.haa00c and a.ggc15c = b.haa01c and isnull(ggc81c, '') = '' and isnull(ggc15c, '') <> '' update a set a.ggc81c = b.haa06c from ggm03 a, ham01 b where a.ggc00c = b.haa00c and a.ggc17c = b.haa01c and isnull(ggc81c, '') = '' and isnull(ggc17c, '') <> '' update a set a.gge81c = b.haa06c from ggm05 a, ham01 b where a.gge00c = b.haa00c and a.gge12c = b.haa01c and isnull(gge81c, '') = '' and isnull(gge12c, '') <> '' update a set a.gqa81c = b.haa06c from gqm01 a, ham01 b where a.gqa00c = b.haa00c and a.gqa07c = b.haa01c and isnull(gqa81c, '') = '' and isnull(gqa07c, '') <> '' update a set a.gea81c = b.haa06c from gem01 a, ham01 b where a.gea00c = b.haa00c and a.gea21c = b.haa01c and isnull(gea81c, '') = '' and isnull(gea21c, '') <> '' end GO /****** Object: StoredProcedure [dbo].[upg_initialize_gsm23] Script Date: 06/12/2017 06:20:14 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_initialize_gsm23] as begin -- 清除旧数据 delete from gsm23 insert into gsm23(q_index, q_start, q_end, q_rate, q_deduction) values(1, 0, 1500, 0.03, 0) insert into gsm23(q_index, q_start, q_end, q_rate, q_deduction) values(2, 1500, 4500, 0.1, 105) insert into gsm23(q_index, q_start, q_end, q_rate, q_deduction) values(3, 4500, 9000, 0.2, 555) insert into gsm23(q_index, q_start, q_end, q_rate, q_deduction) values(4, 9000, 35000, 0.25, 1005) insert into gsm23(q_index, q_start, q_end, q_rate, q_deduction) values(5, 35000, 55000, 0.3, 2755) insert into gsm23(q_index, q_start, q_end, q_rate, q_deduction) values(6, 55000, 80000, 0.35, 5505) insert into gsm23(q_index, q_start, q_end, q_rate, q_deduction) values(7, 80000, 99999999, 0.45, 13505) end GO /****** Object: StoredProcedure [dbo].[upg_cal_tax] Script Date: 06/12/2017 06:19:34 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_cal_tax] ( @income float, -- 应纳税所得额 @foreign int, -- 0-国内 1-国外 @tax float output -- 税 ) as begin declare @base float set @base = 0 if @foreign = 0 begin select @base = isnull(gsa03f, 0) from gsm01 where gsa00c = '001' and gsa01c = 'SP019' end else if @foreign = 1 begin select @base = isnull(gsa03f, 0) from gsm01 where gsa00c = '001' and gsa01c = 'SP202' end -- 实际工资 减去 个税起征点 得到应纳税所得额 set @income = @income - @base if @income <= 0 begin set @tax = 0 return end declare @rate float declare @deduction float select @rate = q_rate, @deduction = q_deduction from gsm23 where q_start < @income and q_end >= @income set @rate = isnull(@rate, 0) set @deduction = isnull(@deduction, 0) set @tax = @rate * @income + @deduction end GO /****** Object: StoredProcedure [dbo].[upg_check_feeling_tiems] Script Date: 06/12/2017 06:19:40 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_check_feeling_tiems] ( @memid varchar(20), @prjid varchar(20), @done int output, @total_cnt int output ) as begin --是否有购买过 if exists(select 1 from gcm20 with (nolock) where gct07c = @prjid and gct03c = @memid) begin select @done = 1 end else begin select @done = 0 end end GO /****** Object: StoredProcedure [dbo].[upg_modify_good_std_unit] Script Date: 06/12/2017 06:20:24 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_modify_good_std_unit] ( @compid varchar(10), -- 公司别 @goodid varchar(20), -- 产品编号 @oldunit varchar(20), -- 老单位, 支 @newunit varchar(20), -- 新单位,包 @rate float -- 比率 新单位 = rate * 老单位,1包 = 10支 ) as begin -- 库存资料 update a set a.gfd05f = a.gfd05f / @rate, a.gfd06f = a.gfd06f * @rate from gfm04 a, gam26 z where a.gfd00c = z.gaz02c and z.gaz01c = @compid and a.gfd04c = @goodid -- 消耗数量 update a set a.gfe03f = a.gfe03f / @rate from gfm05 a, gam26 z where a.gfe00c = z.gaz02c and z.gaz01c = @compid and a.gfe02c = @goodid -- 入库 update a set a.ghb04c = @newunit, ghb13f = ghb13f / @rate, ghb14f = ghb14f * @rate from ghm02 a, gam26 z where a.ghb00c = z.gaz02c and z.gaz01c = @compid and a.ghb03c = @goodid --出库 update a set a.gib04c = @newunit, gib08f = gib08f/@rate, gib11f = gib11f*@rate from gim02 a, gam26 z where a.gib00c = z.gaz02c and z.gaz01c = @compid and a.gib03c = @goodid --盘点 update a set a.gkb04c = @newunit, gkb05f = gkb05f/@rate, gkb06f = gkb06f/@rate, gkb07f = gkb07f/@rate from gkm02 a, gam26 z where a.gkb00c = z.gaz02c and z.gaz01c = @compid and a.gkb03c = @goodid --产品资料 update a set a.gfa07c = @newunit, gfa09f = gfa09f*@rate, gfa11f = gfa11f*@rate, gfa18f = gfa18f*@rate, gfa19f = gfa19f*@rate, gfa21f = gfa21f/@rate, gfa22f = gfa22f*@rate from gfm01 a, gam26 z where a.gfa00c = z.gaz02c and z.gaz01c = @compid and a.gfa01c = @goodid --收银 update a set a.ggc05f = a.ggc05f/@rate, ggc08f = ggc08f*@rate, ggc10f = ggc10f*@rate, ggc04c = @newunit from ggm03 a, gam26 z where a.ggc00c = z.gaz02c and z.gaz01c = @compid and a.ggc03c = @goodid --交易历史 update a set a.gct11f = a.gct11f/@rate, gct09f = gct09f*@rate, gct10f = gct10f*@rate from gcm20 a, gam26 z where a.gct00c = z.gaz02c and z.gaz01c = @compid and a.gct07c = @goodid and a.gct06c = '4' end GO /****** Object: StoredProcedure [dbo].[upg_rename_billid] Script Date: 06/12/2017 06:20:35 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_rename_billid] ( @compid varchar(10), @oldbillid varchar(20), @newbillid varchar(20) ) as begin update ggm01 set gga01c = @newbillid where gga00c = @compid and gga01c = @oldbillid update ggm02 set ggb01c = @newbillid where ggb00c = @compid and ggb01c = @oldbillid update ggm03 set ggc01c = @newbillid where ggc00c = @compid and ggc01c = @oldbillid update gsm03 set gsc01c = @newbillid where gsc00c = @compid and gsc01c = @oldbillid and gsc02c = 'gx' update gcm20 set gct14c = @newbillid where gct00c = @compid and gct14c = @oldbillid and gct13c = 'gx' update gcm04 set gcd08c = @newbillid where gcd00c = @compid and gcd08c = @oldbillid and gcd07c = 'gx' end GO /****** Object: StoredProcedure [dbo].[upg_update_ser_to_gcm20] Script Date: 06/12/2017 06:20:50 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_update_ser_to_gcm20]( @compid varchar(10), -- 公司别 @billid varchar(20), -- 消费单号 @mberno varchar(20)) --会员卡号 as begin -- create table #tbl_emp -- ( -- compid varchar(10) not null, --公司编号 -- billid varchar(20) not null, --消费单号 -- itemid varchar(20) null, --项目/产品代码 -- member1 varchar(20) null, --员工1 -- member2 varchar(20) null, --员工2 -- member3 varchar(20) null, --员工3 -- member4 varchar(20) null, --员工4 -- payway varchar(5) null --支付方式 -- ) -- insert into #tbl_emp(compid, billid, itemid, member1, member2, member3, member4, payway) -- select ggb00c, ggb01c, ggb03c, ggb12c, ggb13c, ggb14c, ggb26c, ggb27c -- from ggm02 where ggb00c = @compid and ggb01c = @billid -- -- -- insert into #tbl_emp(compid, billid, itemid, member1, member2, member3, member4, payway) -- select ggc00c, ggc01c, ggc03c, ggc12c, ggc15c, ggc17c, '', ggc27c -- from ggm03 where ggc00c = @compid and ggc01c = @billid -- update a -- set a.gct15c = b.member1, -- a.gct16c = b.member2, -- a.gct17c = b.member3, -- a.gct18c = b.member4 -- from gcm20 a, #tbl_emp b -- where a.gct00c = b.compid -- and a.gct02c = @mberno -- and a.gct13c = 'gx' -- and a.gct14c = b.billid -- and a.gct07c = b.itemid -- and a.gct20c = b.payway -- drop table #tbl_emp delete from gcm20 where gct00c = @compid and gct14c = @billid and gct13c = 'gx' insert gcm20(gct00c, gct02c, gct03c, gct04d, gct05c, gct06c, gct07c, gct08c, gct09f, gct10f, gct11f, gct12f, gct13c, gct14c, gct15c, gct16c, gct17c, gct18c, gct19t, gct20c, gct21f) select gga00c, gga09c, gga05c, gga02d, '', '3', ggb03c, gda03c, ggb08f, ggb10f, ggb05f, ggb11f, 'gx', gga01c, ggb12c, ggb13c, ggb14c, ggb26c, gga04t, ggb27c, ggb02f from ggm01 a with (nolock), ggm02 b with (nolock), gdm01 c where a.gga00c = b.ggb00c and a.gga01c = b.ggb01c and b.ggb00c = c.gda00c and b.ggb03c = c.gda01c and b.ggb00c = @compid and b.ggb01c = @billid -- 产品 insert gcm20(gct00c, gct02c, gct03c, gct04d, gct05c, gct06c, gct07c, gct08c, gct09f, gct10f, gct11f, gct12f, gct13c, gct14c, gct15c, gct16c, gct17c, gct18c, gct19t, gct20c) select gga00c, gga09c, gga05c, gga02d, '', '4', ggc03c, gfa03c, ggc08f, ggc10f, ggc05f, ggc11f, 'gx', gga01c, ggc12c, ggc15c, ggc17c, '', gga04t, ggc27c from ggm01 a with (nolock), ggm03 b with (nolock), gfm01 c where a.gga00c = b.ggc00c and a.gga01c = b.ggc01c and b.ggc00c = c.gfa00c and b.ggc03c = c.gfa01c and b.ggc00c = @compid and b.ggc01c = @billid end GO /****** Object: StoredProcedure [dbo].[upg_m_dispatch_hbm02] Script Date: 06/12/2017 06:20:21 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_m_dispatch_hbm02] ( @compid varchar(10), --公司 @staff varchar(20) --员工 ) as begin -- 创建临时表 create table #ham12_hbm02_history ( compId varchar(10), staffId varchar(20) ) declare @CurrentCompId varchar(10) set @CurrentCompId = @compid declare @CurrentStaffId varchar(20) set @CurrentStaffId = @staff declare @PairCompId varchar(10) declare @PairStaffId varchar(20) -- 创建循环变量 declare @HasRecord int set @HasRecord = 1 declare @Index int set @Index = -1 declare @BorderIndex int set @BorderIndex = 2147483647 while(@HasRecord = 1) begin if exists ( select 1 from ham12 where hal03c = @CurrentCompId and hal04c = @CurrentStaffId and hal02i >= @Index) begin set @HasRecord = 1 select top 1 @PairCompId = [hal06c], @PairStaffId = [hal07c] from ham12 where hal03c = @CurrentCompId and hal04c = @CurrentStaffId and hal02i >= @Index order by hal02i if not exists (select 1 from #ham12_hbm02_history where compId = @PairCompId and staffId = @PairStaffId) begin insert into #ham12_hbm02_history ( compId, staffId) select @PairCompId, @PairStaffId delete from hbm02 where hbb00c = @PairCompId and hbb01c = @PairStaffId insert into hbm02 (hbb00c, hbb01c, hbb02c, hbb03c, hbb04c, hbb05f, hbb06c) select @PairCompId, @PairStaffId, hbb02c, hbb03c, hbb04c, hbb05f, hbb06c from hbm02 where hbb00c = @compid and hbb01c = @staff end select top 1 @CurrentCompId = [hal06c], @CurrentStaffId = [hal07c], @Index = hal02i from ham12 where hal03c = @CurrentCompId and hal04c = @CurrentStaffId and hal02i >= @Index order by hal02i if (@BorderIndex = 2147483647) begin set @BorderIndex = @Index end end else begin set @HasRecord = 0 end end set @HasRecord = 1 set @CurrentCompId = @compid set @CurrentStaffId = @staff set @Index = @BorderIndex while(@HasRecord = 1) begin if exists ( select 1 from ham12 where hal06c = @CurrentCompId and hal07c = @CurrentStaffId and hal02i <= @Index) begin set @HasRecord = 1 select top 1 @PairCompId = [hal03c], @PairStaffId = [hal04c] from ham12 where hal06c = @CurrentCompId and hal07c = @CurrentStaffId and hal02i <= @Index order by hal02i desc if not exists (select 1 from #ham12_hbm02_history where compId = @PairCompId and staffId = @PairStaffId) begin insert into #ham12_hbm02_history ( compId, staffId) select @PairCompId, @PairStaffId delete from hbm02 where hbb00c = @PairCompId and hbb01c = @PairStaffId insert into hbm02 (hbb00c, hbb01c, hbb02c, hbb03c, hbb04c, hbb05f, hbb06c) select @PairCompId, @PairStaffId, hbb02c, hbb03c, hbb04c, hbb05f, hbb06c from hbm02 where hbb00c = @compid and hbb01c = @staff end select top 1 @CurrentCompId = [hal03c], @CurrentStaffId = [hal04c], @Index = hal02i from ham12 where hal06c = @CurrentCompId and hal07c = @CurrentStaffId and hal02i <= @Index order by hal02i desc end else begin set @HasRecord = 0 end end -- 删除临时表 drop table #ham12_hbm02_history end GO /****** Object: StoredProcedure [dbo].[upg_get_staff_dispatch_history] Script Date: 06/12/2017 06:20:09 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_get_staff_dispatch_history] ( @compid varchar(10), --公司 @staff varchar(20) --员工 ) as begin -- 创建临时表 create table #ham12_history( hal01c varchar(20) COLLATE Chinese_PRC_CI_as not null, hal02i int not null, hal03c varchar(10) COLLATE Chinese_PRC_CI_as null, hal04c varchar(20) COLLATE Chinese_PRC_CI_as null, hal05c varchar(5) COLLATE Chinese_PRC_CI_as null, hal06c varchar(10) COLLATE Chinese_PRC_CI_as null, hal07c varchar(20) COLLATE Chinese_PRC_CI_as null, hal08c varchar(5) COLLATE Chinese_PRC_CI_as null, hal09c varchar(80) COLLATE Chinese_PRC_CI_as null ) declare @CurrentCompId varchar(10) set @CurrentCompId = @compid declare @CurrentStaffId varchar(20) set @CurrentStaffId = @staff -- 创建循环变量 declare @HasRecord int set @HasRecord = 1 declare @Index int set @Index = -1 declare @BorderIndex int set @BorderIndex = 2147483647 while(@HasRecord = 1) begin if exists ( select 1 from ham12 where hal03c = @CurrentCompId and hal04c = @CurrentStaffId and hal02i >= @Index) begin set @HasRecord = 1 insert into #ham12_history ( hal01c, hal02i, hal03c, hal04c, hal05c, hal06c, hal07c, hal08c, hal09c) select top 1 hal01c, hal02i, hal03c, hal04c, hal05c, hal06c, hal07c, hal08c, hal09c from ham12 where hal03c = @CurrentCompId and hal04c = @CurrentStaffId and hal02i >= @Index order by hal02i select top 1 @CurrentCompId = hal06c, @CurrentStaffId = hal07c, @Index = hal02i from ham12 where hal03c = @CurrentCompId and hal04c = @CurrentStaffId and hal02i >= @Index order by hal02i if (@BorderIndex = 2147483647) begin set @BorderIndex = @Index end end else begin set @HasRecord = 0 end end set @HasRecord = 1 set @CurrentCompId = @compid set @CurrentStaffId = @staff set @Index = @BorderIndex while(@HasRecord = 1) begin if exists ( select 1 from ham12 where hal06c = @CurrentCompId and hal07c = @CurrentStaffId and hal02i <= @Index) begin set @HasRecord = 1 insert into #ham12_history ( hal01c, hal02i, hal03c, hal04c, hal05c, hal06c, hal07c, hal08c, hal09c) select top 1 hal01c, hal02i, hal03c, hal04c, hal05c, hal06c, hal07c, hal08c, hal09c from ham12 where hal06c = @CurrentCompId and hal07c = @CurrentStaffId and hal02i <= @Index order by hal02i desc select top 1 @CurrentCompId = hal03c, @CurrentStaffId = hal04c, @Index = hal02i from ham12 where hal06c = @CurrentCompId and hal07c = @CurrentStaffId and hal02i <= @Index order by hal02i desc end else begin set @HasRecord = 0 end end -- 返回结果 select * from #ham12_history order by hal02i -- 删除临时表 drop table #ham12_history end GO /****** Object: StoredProcedure [dbo].[upg_m_dispatch_kam01] Script Date: 06/12/2017 06:20:21 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_m_dispatch_kam01] ( @fromcompid varchar(10), -- 从哪一个公司发布 @tocompid varchar(10), -- 目的公司 @mandatory varchar(2) -- 是否强制统一 ) as begin if @fromcompid = @tocompid return if @mandatory <> '1' and @mandatory <> '0' return if @mandatory = '1' begin delete kam01 where kaa00c = @tocompid insert kam01(kaa00c, kaa01c, kaa02c, kaa03f, kaa04f, kaa05f, kaa06c) select @tocompid, kaa01c, kaa02c, kaa03f, kaa04f, kaa05f, kaa06c from kam01 where kaa00c = @fromcompid return end create table #kam01_key -- 疗程资料关键字 ( kaa01c varchar(5) not null -- 类别代码 ) insert into #kam01_key(kaa01c) select kaa01c from kam01 where kaa00c = @fromcompid declare @current_kaa01c varchar(5) -- 类别代码 -- 创建循环变量 declare @HasRecord int set @HasRecord = 1 declare @Index int set @Index = -1 while(@HasRecord = 1) begin if exists ( select 1 from #kam01_key) begin set @HasRecord = 1 select top 1 @current_kaa01c = kaa01c from #kam01_key if not exists (select 1 from kam01 where kaa00c = @tocompid and kaa01c = @current_kaa01c) begin insert kam01(kaa00c, kaa01c, kaa02c, kaa03f, kaa04f, kaa05f, kaa06c) select @tocompid, kaa01c, kaa02c, kaa03f, kaa04f, kaa05f, kaa06c from kam01 where kaa00c = @fromcompid and kaa01c = @current_kaa01c end delete from #kam01_key where kaa01c = @current_kaa01c end else begin set @HasRecord = 0 end end drop table #kam01_key end GO /****** Object: StoredProcedure [dbo].[upg_m_dispatch_kam01_single] Script Date: 06/12/2017 06:20:21 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_m_dispatch_kam01_single] ( @fromcompid varchar(10), -- 从哪一个公司发布 @tocompid varchar(10), -- 目的公司 @mandatory varchar(2) -- 是否强制统一 ) as begin if @fromcompid = @tocompid return if @mandatory <> '1' and @mandatory <> '0' return if @mandatory = '1' begin delete kam01 where kaa00c = @tocompid end create table #kam01_key -- 疗程资料关键字 ( kaa01c varchar(5) not null -- 类别代码 ) insert into #kam01_key(kaa01c) select kaa01c from kam01 where kaa00c = @fromcompid declare @current_kaa01c varchar(5) -- 创建循环变量 declare @HasRecord int set @HasRecord = 1 declare @Index int set @Index = -1 while(@HasRecord = 1) begin if exists ( select 1 from #kam01_key) begin set @HasRecord = 1 select top 1 @current_kaa01c = kaa01c from #kam01_key if not exists (select 1 from kam01 where kaa00c = @tocompid and kaa01c = @current_kaa01c) begin insert kam01(kaa00c, kaa01c, kaa02c, kaa03f, kaa04f, kaa05f, kaa06c) select @tocompid, kaa01c, kaa02c, kaa03f, kaa04f, kaa05f, kaa06c from kam01 where kaa00c = @fromcompid and kaa01c = @current_kaa01c end delete from #kam01_key where kaa01c = @current_kaa01c end else begin set @HasRecord = 0 end end drop table #kam01_key end GO /****** Object: StoredProcedure [dbo].[upg_data_recover_kam01] Script Date: 06/12/2017 06:19:55 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_data_recover_kam01] as begin -- 判断是否已做数据恢复 if exists (select 1 from data_recover where rdd00c = '001' and rdd01c = 'kam01' and rdd02c = 'init') begin return end -- 创建临时表 create table #data_recover_comp ( compId varchar(10) not null ) insert into #data_recover_comp(compId) select gae01c from gam05 with (nolock) -- 创建循环变量 declare @HasRecord int set @HasRecord = 1 declare @currentCompId varchar(10) while(@HasRecord = 1) begin if exists (select 1 from #data_recover_comp) begin set @HasRecord = 1 select top 1 @currentCompId = compId from #data_recover_comp delete from kam01 where kaa00c = @currentCompId insert into kam01(kaa00c, kaa01c, kaa02c) select gsb00c, gsb02c, gsb03c from gsm02 where gsb00c = @currentCompId and gsb01c = 'B9' -- 清除已处理的门店 delete #data_recover_comp where compId = @currentCompId end else begin set @HasRecord = 0 end end drop table #data_recover_comp insert into data_recover(rdd00c, rdd01c, rdd02c) values ('001', 'kam01', 'init') end GO /****** Object: StoredProcedure [dbo].[upg_cal_profit] Script Date: 06/12/2017 06:19:34 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_cal_profit] ( @compid varchar(10), -- 支持门店或分公司 @fromdate varchar(10), @todate varchar(10), @calmode int ) as begin declare @cur_date datetime set @cur_date = convert(datetime, @fromdate) declare @strtmp varchar(30) declare @str_date varchar(20) declare @amt float declare @tmpamt float create table #tbl_profit_day_2 ( idd int not null, code varchar(20) not null, itemname varchar(60) null, is_add int null, amt float null, is_leaf int null, payment varchar(10) null, -- 支付方式 kind int null, -- 0-充值 1-卡销售 9-产品销售 2-服务 -1-取款 8-保证金收入 6-还款 primary key(idd) ) while (@cur_date <= convert(datetime, @todate)) begin select @strtmp = convert(varchar(10), cast(@cur_date as datetime), 120); select @str_date = substring(@strtmp, 1, 4) + substring(@strtmp, 6, 2) + substring(@strtmp, 9, 2) delete #tbl_profit_day_2 insert #tbl_profit_day_2(idd, code, itemname, is_add, amt, is_leaf, payment, kind) exec upg_analyse_profit_inter @compid, @str_date, @str_date, 1, '', @calmode delete gcm21 where gcu00c = @compid and gcu01c = @str_date insert gcm21(gcu00c, gcu01c, gcu02i, gcu03c, gcu04c, gcu05i, gcu06f, gcu07i, gcu08c, gcu09i) select @compid, @str_date, idd, code, itemname, is_add, isnull(amt, 0), is_leaf, payment, kind from #tbl_profit_day_2 select @cur_date = dateadd(day, 1, @cur_date) end end GO /****** Object: StoredProcedure [dbo].[upg_gtc04_verify] Script Date: 06/12/2017 06:20:12 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_gtc04_verify] ( @confirm int, --1审核,2 取消审核 @gte00c varchar(10), --门店编号 @gte01c varchar(20), --卡号 @gte02c varchar(20), --套餐编号 @gth01c varchar(50), --当前单号 @gte11c varchar(50) --变更单号 ) as begin if( @confirm = 1) begin delete from gtc04 where gte00c = @gte00c and gte01c = @gte01c and gte02c = @gte02c and gte11c = @gth01c --审核则进行退货,状态改为-1 update gtc04 set gte12i = -1 where gte00c = @gte00c and gte01c = @gte01c and gte02c = @gte02c and gte11c = @gte11c delete from gtc08 where gtj00c = @gte00c and gtj01c = @gte01c and gtj02c = @gte02c and gtj11c = @gth01c update gtc08 set gtj12i = -1 where gtj00c = @gte00c and gtj01c = @gte01c and gtj02c = @gte02c and gtj11c = @gte11c update gtc06 set gth19c = @gte11c where gth00c = @gte00c and gth01c = @gth01c and gth02c = @gte02c --gcm03 会员卡账户 --增加余额 insert into gtc07(gti00c,gti02d,gti03c,gti04c,gti05c,gti06f,gti07f,gti08c,gti09c,gti10f,gti11f, gti12i,gti13c,gti14i,gti17c,gti18f,gti19c,gti20f,gti21c,gti22f,gti23c,gti24f) select gti00c,convert(varchar(8),getdate(),112),gti03c,gti04c,gti05c,-gti06f,-gti07f,gti08c,gti09c,gti10f,-gti11f, gti12i,gti13c,gti14i,gti17c,gti18f,gti19c,gti20f,gti21c,gti22f,gti23c,gti24f from gtc07 where gti00c = @gte00c and gti03c = @gte01c and gti04c = @gte02c and gti09c = @gte11c and gti12i = 1 end else begin delete from gtc04 where gte00c = @gte00c and gte01c = @gte01c and gte02c = @gte02c and gte11c = @gth01c update gtc04 set gte12i = 0 from gtc04 where gte00c = @gte00c and gte01c = @gte01c and gte02c = @gte02c and gte11c = @gte11c --变更单号 delete from gtc08 where gtj00c = @gte00c and gtj01c = @gte01c and gtj02c = @gte02c and gtj11c = @gth01c update gtc08 set gtj12i = 0 where gtj00c = @gte00c and gtj01c = @gte01c and gtj02c = @gte02c and gtj11c = @gte11c delete from gtc07 where gti00c = @gte00c and gti03c = @gte01c and gti04c = @gte02c and gti09c = @gte11c and gti12i = 1 end end GO /****** Object: StoredProcedure [dbo].[upg_check_combo_times] Script Date: 06/12/2017 06:19:40 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_check_combo_times] ( @compid varchar(10), @cardid varchar(30), @prjid varchar(30), -- 项目代码或产品代码 @times float, -- 项目次数或产品数量 @type int, -- 类别,1 项目 2 物品 @maincomboseq int -- 套餐主序号 ) as begin -- 根据卡号获取归属公司: declare @ownercompid nvarchar(10) --卡的归属公司 select @ownercompid = min(gca13d) from gcm01 where gca01c = @cardid and gca00c = gca13d declare @detPrj varchar(30) declare @remain_times float declare @ret int declare @price float -- 核算价格 declare @comboid varchar(20) -- 套餐编号 declare @mainproc varchar(20) -- 主项目 declare @maingood varchar(20) -- 主产品 set @mainproc = '' set @detPrj = isnull(@prjid, '') set @remain_times = 0 set @ret = 1 create table #gtc04_gte08c ( gte00c varchar(10) null, -- 公司编号 gte01c varchar(20) null, -- 卡号 gte02c varchar(20) null, -- 套餐编号 gte03c varchar(20) null, -- 项目编号 gte04f float null, -- 项目总次数 gte05f float null, -- 已经使用次数 gte06f float null, -- 剩余次数 gte08c varchar(20) null, -- 主项目 gte09i int null, -- 序号 gte10c varchar(20) null, -- 单据类型 gte11c varchar(50) null, -- 单号 ) create table #gtc08_gtj07c ( gtj00c varchar(10) null, -- 公司编号 gtj01c varchar(20) null, -- 卡号 gtj02c varchar(20) null, -- 套餐编号 gtj03c varchar(20) null, -- 产品编号 gtj04f float null, -- 项目总次数 gtj05f float null, -- 已经使用次数 gtj06f float null, -- 剩余次数 gtj07c varchar(20) null, -- 主产品 gtj09i int null, -- 序号 gtj10c varchar(20) null, -- 单据类型 gtj11c varchar(50) null, -- 单号 ) insert into #gtc08_gtj07c(gtj00c, gtj01c, gtj02c, gtj03c, gtj04f, gtj05f, gtj06f, gtj07c, gtj09i, gtj10c, gtj11c) select gtj00c, gtj01c, gtj02c, gtj03c, gtj04f, gtj05f, gtj06f, gtj07c, gtj09i, gtj10c, gtj11c from gtc08 where gtj00c = @ownercompid and gtj01c = @cardid and isnull(gtj12i,0) <> -1 --增加状态判断(0为正常,-1为退货) and gtj03c = gtj07c insert into #gtc04_gte08c(gte00c, gte01c, gte02c, gte03c, gte04f, gte05f, gte06f, gte08c, gte09i, gte10c, gte11c ) select gte00c, gte01c, gte02c, gte03c, gte04f, gte05f, gte06f, gte08c, gte09i, gte10c, gte11c from gtc04 a where a.gte00c = @ownercompid and a.gte01c = @cardid and isnull(gte12i,0) <> -1 --增加状态判断(0为正常,-1为退货) and gte06f > 0 and gte03c = gte08c -- 如果gtc04中不存在该项目,则查找相应的套餐中是否存在此项目,如果存在,则对其替换项目做处理 if @type = 1 begin if isnull(@maincomboseq, -1) <> -1 begin select @mainproc = gte08c, @remain_times = gte06f, @maincomboseq = gte09i, @comboid = gte02c from gtc04 where gte00c = @ownercompid and gte01c = @cardid and gte09i = @maincomboseq end else begin declare @SP217_2 varchar(2) --套餐是否严格控制有效时长 declare @SP217_3 varchar(2) --套餐是否严格控制有效时间范围 select @SP217_2 = gsa02c, @SP217_3 = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP217' if (@SP217_2 = '0' and @SP217_3 = '0') begin select @mainproc = a.gte08c, @remain_times =b.gte06f, @maincomboseq = b.gte09i, @comboid = b.gte02c from gtc04 a, #gtc04_gte08c b where a.gte00c = b.gte00c and a.gte01c = b.gte01c and a.gte08c = b.gte08c and (a.gte03c = @prjid or a.gte03c in (select d.gte03c from gtc04 d where d.gte00c = a.gte00c and d.gte01c = a.gte01c and d.gte03c = a.gte08c and gte03c = @prjid) ) order by a.gte06f end else begin --套餐有效期限判断 --获取当前日期 declare @curdate varchar(20), @curtime varchar(20) exec upg_getcurrdate @curdate output, @curtime output --2个权限都有,则取第1个 if((@SP217_2 = '1' and @SP217_3 = '0') or (@SP217_2 = '1' and @SP217_3 = '1')) begin select @mainproc = a.gte08c, @remain_times =b.gte06f, @maincomboseq = b.gte09i, @comboid = b.gte02c from (select gte00c, gte01c,gte03c,gte08c,gte06f from gtc04 where gte00c = @ownercompid and gte01c = @cardid and isnull(gte15d,'') = '' union all select gte00c, gte01c,gte03c,gte08c,gte06f from (select gte00c, gte01c,gte03c,gte08c,gte06f, convert(varchar(10),(case gte14c when '1' then dateadd(year,gte13i,gte15d) --年 when '2' then dateadd(month,gte13i,gte15d) --月 when '3' then dateadd(day,gte13i,gte15d) --日 when '4' then dateadd(month,gte13i * 3 ,gte15d) --季度 else dateadd(year,10,@curdate) --如果都为空,默认日期加10年 end),112) as gte15d from gtc04 where gte00c = @ownercompid and gte01c = @cardid --and gte06f > 0 and gte03c = @prjid and isnull(gte15d,'') <> '' ) as get04_gte15d where gte15d >= @curdate )a, #gtc04_gte08c b where a.gte00c = b.gte00c and a.gte01c = b.gte01c and a.gte08c = b.gte08c and (a.gte03c = @prjid or a.gte03c in (select d.gte03c from gtc04 d where d.gte00c = a.gte00c and d.gte01c = a.gte01c and d.gte03c = a.gte08c and gte03c = @prjid) ) order by a.gte06f end if(@SP217_2 = '0' and @SP217_3 = '1' ) begin select @mainproc = a.gte08c, @remain_times =b.gte06f, @maincomboseq = b.gte09i, @comboid = b.gte02c from (select gte00c, gte01c,gte03c,gte08c,gte06f from gtc04 where gte00c = @ownercompid and gte01c = @cardid and (isnull(gte16c,'') = '' or isnull(gte17c,'') = '') union select gte00c, gte01c,gte03c,gte08c,gte06f from gtc04 where gte00c = @ownercompid and gte01c = @cardid and isnull(gte16c,'') != '' and isnull(gte17c,'') != '' and replace(gte16c,'/','') <= @curdate and replace(gte17c,'/','') >= @curdate )a, #gtc04_gte08c b where a.gte00c = b.gte00c and a.gte01c = b.gte01c and a.gte08c = b.gte08c and (a.gte03c = @prjid or a.gte03c in (select d.gte03c from gtc04 d where d.gte00c = a.gte00c and d.gte01c = a.gte01c and d.gte03c = a.gte08c and gte03c = @prjid) ) order by a.gte06f end end end if @mainproc = '' --@@rowcount = 0 begin set @ret = 2 --套餐中不存在项目 end else begin -- 存在套餐中 if @mainproc <> @prjid begin -- 如果不是主项目,则直接取的剩余次数 select @remain_times = gte06f, @maincomboseq = gte09i, @comboid = gte02c from gtc04 a where a.gte00c = @ownercompid and a.gte01c = @cardid and a.gte03c = @mainproc and exists(select 1 from gtc04 b where a.gte00c = b.gte00c and a.gte01c = b.gte01c and a.gte02c = b.gte02c and b.gte03c = @prjid) and gte02c = @comboid end if @remain_times >= @times begin set @ret = 1 -- 有可以扣 end else begin set @ret = 0 -- 不够本次扣的 end select @price = dbo.fun_divide_default(isnull((case gte10c when 'gn' then (select top 1 isnull(gtf05f, 1) from gtc05 where gtf00c = d.gti00c and gtf01c = gte11c and gtf02c = gte02c) when 'gz' then (select top 1 isnull(gth05f, 1) from gtc06 where gth00c = d.gti00c and gth01c = gte11c and gth02c = gte02c) end), 0) * d.gti09f, a.gte04f, 0) from gtc04 a, gtc09 d where d.gti10i = a.gte09i and a.gte09i = @maincomboseq end end else if (@type = 2) begin if isnull(@maincomboseq, -1) <> -1 begin -- 检查产品是否足够 select @maingood = gtj07c, @remain_times = gtj06f, @maincomboseq = gtj09i, @comboid = gtj02c from gtc08 where gtj00c = @ownercompid and gtj01c = @cardid and gtj09i = @maincomboseq end else begin -- 检查产品是否足够 select @maingood = a.gtj07c, @remain_times = b.gtj06f, @maincomboseq = b.gtj09i, @comboid = b.gtj02c from gtc08 a, #gtc08_gtj07c b where a.gtj00c = b.gtj00c and a.gtj01c = b.gtj01c and a.gtj07c = b.gtj07c and (a.gtj03c = @prjid or a.gtj03c in (select d.gtj03c from gtc08 d where d.gtj00c = a.gtj00c and d.gtj01c = a.gtj01c and d.gtj03c = a.gtj07c and gtj03c = @prjid) ) order by a.gtj06f end if @@rowcount = 0 begin set @ret = 2 --套餐中不存在项目 end else begin -- 存在套餐中 if @maingood <> @prjid begin -- 如果不是主项目,则直接取的剩余次数 select @remain_times = gtj06f, @maincomboseq = gtj09i, @comboid = gtj02c from gtc08 a where a.gtj00c = @ownercompid and a.gtj01c = @cardid and a.gtj03c = @maingood and exists(select 1 from gtc08 b where a.gtj00c = b.gtj00c and a.gtj01c = b.gtj01c and a.gtj02c = b.gtj02c and b.gtj03c = @prjid) end if @remain_times >= @times begin set @ret = 1 -- 有可以扣 end else begin set @ret = 0 -- 不够本次扣的 end select @price = dbo.fun_divide_default(isnull((case gtj10c when 'gn' then (select top 1 isnull(gtf05f, 1) from gtc05 where gtf00c = d.gtj00c and gtf01c = a.gtj11c and gtf02c = a.gtj02c) when 'gz' then (select top 1 isnull(gth05f, 1) from gtc06 where gth00c = d.gtj00c and gth01c = a.gtj11c and gth02c = a.gtj02c) end), 0), a.gtj04f, 0) from gtc08 a, gtc10 d where d.gtj10i = a.gtj09i and a.gtj09i = @maincomboseq end end select @ret as ret, @remain_times as remain, @price as price, @maincomboseq as maincomboseq drop table #gtc04_gte08c drop table #gtc08_gtj07c end GO /****** Object: StoredProcedure [dbo].[upg_used_combo] Script Date: 06/12/2017 06:20:51 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_used_combo] ( @compid varchar(10), @cardid varchar(30), @prjid varchar(30), --实际消耗的项目 @times float, @confirm int, -- 1 存复核,0存取消复核 3 取-取消复核 2取复核 @billtype varchar(20), @billid varchar(20), @curdate varchar(8), @type int, -- 1项目 2-产品 @maincomboseq int -- 主套餐序号 ) as begin declare @ownercompid nvarchar(10) --卡的归属公司 select @ownercompid = min(gca13d) from gcm01 where gca01c = @cardid and gca00c = gca13d declare @comboid varchar(20) --套餐编号 declare @mainproc varchar(20) -- 主项目 declare @beforetimes float --消费前次数 declare @maingood varchar(20) -- 主产品 declare @local_comboid varchar(20) declare @local_times float -- 处理项目的情况 if @type = 1 begin -- 项目的扣 -- 这个卡的这个项目可能对应的套餐编号,注意这个地方没有时间的因素,可能会出现套餐重复的概念 -- 但只能选择一个套餐作为本次扣的套餐项目 select @comboid = gte02c, @mainproc = gte08c, @beforetimes = isnull(gte06f, 0) from gtc04 where gte00c = @ownercompid and gte01c = @cardid and gte09i = @maincomboseq --如果是没有找到这个项目作为套餐,直接返回 if isnull(@mainproc, '') = '' return --如果gtc04中不存在该项目,则查找相应的套餐中是否存在此项目,如果存在,则对其替换项目做处理 --对于两种套餐,都含同一个项目 那就随机选择一个项进行扣除 --复核则是用掉了套餐 if (@confirm = 1 or @confirm = 3) begin if @confirm = 1 begin insert gtc07(gti00c, gti02d, gti03c, gti04c, gti05c, gti06f, gti07f, gti08c, gti09c, gti10f, gti11f, gti12i, gti13c, gti14i) values(@compid, @curdate, @cardid, @comboid, @mainproc, 0, @times, @billtype, @billid, @beforetimes, @beforetimes-@times, 1, @prjid, @maincomboseq) end else begin delete gtc07 where gti00c = @compid and gti08c = @billtype and gti09c = @billid and gti14i = @maincomboseq and gti04c = @comboid and gti03c = @cardid and gti12i = 1 and gti02d = @curdate end update gtc04 set gte05f = isnull(gte05f, 0) + @times, gte06f = isnull(gte06f, 0) - @times where gte00c = @ownercompid and gte01c = @cardid and gte02c = @comboid and gte03c = @mainproc and gte09i = @maincomboseq end else if (@confirm = 0 or @confirm = 2) begin --必须更新到主项目中 --这个卡的这个项目可能对应的套餐编号,注意这个地方没有时间的因素,可能会出现套餐重复的概念 select @mainproc = gte08c from gtc04 where gte00c = @ownercompid and gte01c = @cardid and gte09i = @maincomboseq and gte02c = @comboid --如果是没有找到这个项目作为套餐,直接返回 if isnull(@mainproc, '') = '' return if @confirm = 0 begin delete gtc07 where gti00c = @compid and gti08c = @billtype and gti09c = @billid and gti14i = @maincomboseq and gti04c = @comboid and gti03c = @cardid and gti12i = 1 and gti02d = @curdate end else begin insert gtc07(gti00c, gti02d, gti03c, gti04c, gti05c, gti06f, gti07f, gti08c, gti09c, gti10f, gti11f, gti12i, gti13c, gti14i) values(@compid, @curdate, @cardid, @comboid, @mainproc, 0, 0-@times, @billtype, @billid, @beforetimes, @beforetimes + @times, 1, @prjid, @maincomboseq) end update gtc04 set gte05f = isnull(gte05f, 0) - @times, gte06f = isnull(gte06f, 0) + @times where gte00c = @ownercompid and gte01c = @cardid and gte03c = @mainproc and gte02c = @comboid and gte09i = @maincomboseq end end else if @type = 2 begin --产品 select @comboid = gtj02c, @maingood = gtj07c from gtc08 where gtj00c = @ownercompid and gtj01c = @cardid and gtj09i = @maincomboseq --如果是没有找到这个产品作为套餐,直接返回 if isnull(@maingood, '') = '' return --剩余次数的取得 select @beforetimes = isnull(gtj06f, 0) from gtc08 where gtj00c = @ownercompid and gtj01c = @cardid and gtj02c = @comboid and gtj03c = @maingood and gtj09i = @maincomboseq --对于两种套餐,都含同一个产品 那就随机选择一个项进行扣除 if (@confirm = 1 or @confirm = 3) begin if @confirm = 1 begin insert into gtc07(gti00c, gti02d, gti03c, gti04c, gti05c, gti06f, gti07f, gti08c, gti09c, gti10f, gti11f, gti12i, gti13c, gti14i) values(@compid, @curdate, @cardid, @comboid, @maingood, 0, @times, @billtype, @billid, @beforetimes, @beforetimes-@times, 2, @prjid, @maincomboseq) end else begin delete gtc07 where gti00c = @compid and gti08c = @billtype and gti09c = @billid and gti13c = @prjid and gti14i = @maincomboseq and gti03c = @cardid and gti12i = 2 and gti02d = @curdate end update gtc08 set gtj05f = isnull(gtj05f, 0) + @times, gtj06f = isnull(gtj06f, 0) - @times where gtj00c = @ownercompid and gtj01c = @cardid and gtj02c = @comboid and gtj03c = @maingood and gtj09i = @maincomboseq end else if (@confirm = 0 or @confirm = 2) begin select @maingood = gtj07c from gtc08 where gtj00c = @ownercompid and gtj01c = @cardid and gtj09i = @maincomboseq and gtj02c = @comboid --如果是没有找到这个产品作为套餐,直接返回 if isnull(@maingood, '') = '' return if @confirm = 0 begin delete gtc07 where gti00c = @compid and gti08c = @billtype and gti09c = @billid and gti04c = @comboid and gti03c = @cardid and gti14i = @maincomboseq and gti12i = 2 and gti02d = @curdate end else begin insert into gtc07(gti00c, gti02d, gti03c, gti04c, gti05c, gti06f, gti07f, gti08c, gti09c, gti10f, gti11f, gti12i, gti13c, gti14i) values(@compid, @curdate, @cardid, @comboid, @maingood, 0, 0-@times, @billtype, @billid, @beforetimes, @beforetimes + @times, 2, @prjid, @maincomboseq) end --必须更新到主产品上 update gtc08 set gtj05f = isnull(gtj05f, 0) - @times, gtj06f = isnull(gtj06f, 0) + @times where gtj00c = @ownercompid and gtj01c = @cardid and gtj03c = @maingood and gtj02c = @comboid and gtj09i = @maincomboseq end end end GO /****** Object: StoredProcedure [dbo].[upg_unconfirm_replace_combo] Script Date: 06/12/2017 06:20:48 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_unconfirm_replace_combo] ( @compid varchar(10), -- 公司别 @billid varchar(20) ) as begin -- 套餐项目 update a set a.gte05f = a.gte05f - b.hej08f, a.gte06f = a.gte06f + b.hej08f from gtc04 a, hem09 b, gcm01 c where a.gte00c = c.gca00c -- 公司 and c.gca00c = c.gca13d and c.gca01c = a.gte01c and a.gte01c = b.hej02c -- 卡号 and b.hej01c = @billid -- 单号 and b.hej00c = @compid and b.hej05i = 1 and a.gte03c = b.hej06c --项目 and a.gte02c = b.hej04c --套餐 and a.gte09i = b.hej11i -- 套餐产品 update a set a.gtj05f = a.gtj05f - b.hej08f, a.gtj06f = a.gtj06f + b.hej08f from gtc08 a, hem09 b, gcm01 c where a.gtj00c = c.gca00c -- 公司 and a.gtj01c = c.gca01c and c.gca13d = c.gca00c and a.gtj01c = b.hej02c -- 卡号 and b.hej01c = @billid -- 单号 and b.hej00c = @compid and b.hej05i = 2 and a.gtj03c = b.hej06c --项目 and a.gtj02c = b.hej04c --套餐 and a.gtj09i = b.hej11i -- 删除hem09的套餐更换历史 delete hem09 where hej01c = @billid -- 单号 and hej00c = @compid -- 删除套餐的变换历史 delete gtc07 where gti00c = @compid and gti09c = @billid and gti08c = 'pj' end GO /****** Object: StoredProcedure [dbo].[upg_reinit_card] Script Date: 06/12/2017 06:20:34 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_reinit_card] ( @compid varchar(10), -- 公司别 @from varchar(20), -- 开始卡号 @to varchar(20) -- 截止卡号 ) as begin if @compid = '*' or @compid = '' begin select @compid = '%' end delete gbm01 where gba00c like @compid and ((gba23c >= @from and gba23c <= @to) or (gba01c >= @from and gba01c <= @to and isnull(gba23c, '') = '')) delete gnm02 where gnb00c like @compid and gnb03c >= @from and gnb03c <= @to delete gcm10 where gcl00c like @compid and gcl03c >= @from and gcl03c <= @to delete gem01 where gea00c like @compid and gea04c >= @from and gea04c <= @to delete gcm05 where gce00c like @compid and gce01c >= @from and gce01c <= @to update gcm01 set gca08i = 1, gca04c = '', gca05d = '', gca06d = '', gca07d = '', gca11c = '', gca19f = 0, gca20c = '', gca21d = '', gca22d = '' where gca00c like @compid and gca01c >= @from and gca01c <= @to update gcm03 set gcc06f = 0 where gcc00c like @compid and gcc01c >= @from and gcc01c <= @to delete gcm02 where gcb00c like @compid and gcb01c >= @from and gcb01c <= @to delete gcm04 where gcd00c like @compid and gcd01c >= @from and gcd01c <= @to delete gcm05 where gce00c like @compid and gce01c >= @from and gce01c <= @to end GO /****** Object: StoredProcedure [dbo].[upg_def_card_must_remain] Script Date: 06/12/2017 06:19:57 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_def_card_must_remain]( @compid varchar(10), --消费公司 @homecompid varchar(10), --归属公司 @cardid varchar(20)) as begin --是否有设置欠款必须留一部分 declare @can_consume_rate float select @can_consume_rate = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP140' if @can_consume_rate = 1 begin select 0 return end --判断这个卡是否有欠款, 只要处理储值账户 declare @has_def int -- 是否有欠款 select @has_def = 0 select @has_def = count(*) from gcm03 with (nolock) where gcc00c = @homecompid and gcc01c = @cardid and isnull(gcc10f, 0)>0 if isnull(@has_def, 0)<1 begin select 0 return end --有欠款,也需要控制 --找到哪个有欠款的单据 declare @bill_type varchar(20) declare @billid varchar(20) declare @bill_compid varchar(10) declare @saveamt float declare @def_amt float -- 欠款金额 select @bill_compid = gsc00c, @bill_type = gsc02c, @billid = gsc01c, @def_amt = gsc05f from gsm03 with (nolock) where gsc04c = '5' and gsc08c = @cardid order by gsc10d if @bill_type = 'gn' begin select @saveamt = gnb11f from gnm02 with (nolock) where gnb00c = @bill_compid and gnb01c = @billid and gnb03c = @cardid end else if @bill_type = 'gz' begin select @saveamt = gcl64f from gcm10 with (nolock) where gcl00c = @bill_compid and gcl01c = @billid and gcl03c = @cardid end select (@saveamt-isnull(@def_amt, 0))*(1-@can_consume_rate) end GO /****** Object: StoredProcedure [dbo].[upg_conv_procamt_card_bill] Script Date: 06/12/2017 06:19:46 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_conv_procamt_card_bill] ( @compid varchar(10), @billid varchar(20) ) as begin if @billid = '*' select @billid = '%' create table #sale_bill_one_card ( billid varchar(20) not null, cardid varchar(20) not null, sumamt float null, primary key(billid) ) create table #sale_bill_proc_amt ( billid varchar(20) not null, sumamt float null, primary key(billid) ) insert #sale_bill_one_card(billid, cardid) select gnb01c, min(gnb03c) from gnm01 a with (nolock), gnm02 b with (nolock) where a.gna00c = b.gnb00c and a.gna01c = b.gnb01c and a.gna01c like @billid and a.gna00c = @compid group by gnb01c insert #sale_bill_proc_amt(billid, sumamt) select gnc01c, sum(gnc05f) from gnm01 a with (nolock), gnm03 b with (nolock) where a.gna00c = b.gnc00c and a.gna01c = b.gnc01c and a.gna01c like @billid and a.gna00c = @compid group by gnc01c update a set a.sumamt = isnull(b.sumamt, 0) from #sale_bill_one_card a, #sale_bill_proc_amt b where a.billid = b.billid -- 处理套餐,与疗程相同的处理方式 delete #sale_bill_proc_amt insert #sale_bill_proc_amt(billid, sumamt) select gtf01c, sum(gtf05f) from gnm01 a with (nolock), gtc05 b with (nolock) where a.gna00c = b.gtf00c and a.gna01c = b.gtf01c and a.gna01c like @billid and a.gna00c = @compid group by gtf01c update a set a.sumamt = isnull(a.sumamt, 0) + isnull(b.sumamt, 0) from #sale_bill_one_card a, #sale_bill_proc_amt b where a.billid = b.billid -- 把疗程,套餐的销售金额全部加过来 update a set a.gnb08f = isnull(a.gnb07f, 0) + isnull(b.sumamt, 0) from gnm02 a, #sale_bill_one_card b where a.gnb00c = @compid and a.gnb01c = b.billid and a.gnb03c = b.cardid update a set a.gnb08f = isnull(gnb07f, 0) from gnm02 a where isnull(gnb08f, 0) = 0 and gnb00c = @compid and gnb01c like @billid drop table #sale_bill_one_card drop table #sale_bill_proc_amt end GO /****** Object: StoredProcedure [dbo].[upg_create_buyproc_payment] Script Date: 06/12/2017 06:19:50 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_create_buyproc_payment] ( @compid varchar(10), @billtype varchar(10), -- 单据类别 @billid varchar(20) ) as begin declare @pay_1 varchar(5), @pay_2 varchar(5), @pay_3 varchar(5), @pay_4 varchar(5), @pay_5 varchar(5), @pay_6 varchar(5), @pay_7 varchar(5), @pay_8 varchar(5) declare @amt_1 float, @amt_2 float, @amt_3 float, @amt_4 float, @amt_5 float, @amt_6 float, @amt_7 float, @amt_8 float declare @paymode varchar(5) declare @payamt float declare @i int declare @remain_pay varchar(5) declare @remain_pay_amt float declare @payment_send varchar(20) -- 赠送支付方式 declare @payment_sent_amt float -- 赠送的金额 declare @fix_sent_amt float -- 已经在购买明细中指定的赠送部分 declare @notfix_sent_amt float -- 除了指定的留下来的赠送部分,需要被随机分配的部分 declare @notfix_guarantee_amt float -- 没有被取用完的那部分取用保证金支付 declare @payment_guarantee_amt float -- 支付明细中的取用保证金的金额 select @payment_send = gsa02c from gsm01 where gsa01c = 'SP058' and gsa00c = @compid select @i = 0 -- 赠送的金额先从gsm05中取出来 select @payment_sent_amt = 0 select @payment_sent_amt = gsc05f from gsm03 with (nolock) where gsc00c = @compid and gsc02c = @billtype and gsc01c = @billid and gsc04c = @payment_send -- 明细中已经声明的赠送金额也统计出来 select @fix_sent_amt = 0 if @billtype = 'gn' and isnull(@payment_sent_amt, 0) > 0 begin select @fix_sent_amt = sum(gnc05f) from gnm03 with (nolock) where gnc00c = @compid and gnc01c = @billid and isnull(gnc22i, 0) = 1 select @fix_sent_amt = isnull(@fix_sent_amt, 0) + sum(gtf05f) from gtc05 where gtf00c = @compid and gtf01c = @billid and isnull(gtf08i, 0) = 1 end else if @billtype = 'gz' and isnull(@payment_sent_amt, 0) > 0 begin select @fix_sent_amt = sum(gcm05f) from gcm11 with (nolock) where gcm00c = @compid and gcm01c = @billid and isnull(gcm22i, 0) = 1 select @fix_sent_amt = isnull(@fix_sent_amt, 0) + sum(gth05f) from gtc06 where gth00c = @compid and gth01c = @billid and isnull(gth08i, 0) = 1 end -- 计算出除了指定的留下来的赠送部分,需要被随机分配的部分 select @notfix_sent_amt = isnull(@payment_sent_amt, 0) - isnull(@fix_sent_amt, 0) -- 处理取用保证金的部分 declare @available_guarantee_amt float -- 保证金的金额, 是可用的部分,变化的 select @available_guarantee_amt = 0 select @payment_guarantee_amt = 0 -- 取支付明细中的取用保证金部分 select @payment_guarantee_amt = gsc05f from gsm03 with (nolock) where gsc00c = @compid and gsc02c = @billtype and gsc01c = @billid and gsc04c = '&' select @available_guarantee_amt = isnull(@payment_guarantee_amt, 0) -------------先将指定要用保证金支付的疗程和套餐统计出来, 并在下面的分摊过程中除去已经分摊了的保证金------------------- declare @paydepositamt float -- 套餐或疗程的金额 declare @factpaydepositamt float --指定使用保证金支付的单个套餐或疗程实际支付额度,可能是部分支付 declare @sumfactpaydepositamt float --所有指定保证金支付的套餐或疗程实际实际支付的总额度 set @sumfactpaydepositamt = 0 if @billtype = 'gn' and isnull(@payment_guarantee_amt, 0) > 0 begin ------------预设疗程指定使用的保证金---------------------------- declare cur_each_buyproc_by_deposit cursor for select isnull(gnc05f, 0) from gnm03 where gnc00c = @compid and gnc01c = @billid and isnull(gnc02c, '') <> '' and isnull(gnc22i, 0)<>1 and isnull(gnc26i, 0) = 1 -- 选择保证金 for update of gnc27f open cur_each_buyproc_by_deposit fetch cur_each_buyproc_by_deposit into @paydepositamt while @@fetch_status = 0 begin if isnull(@available_guarantee_amt, 0)>0 begin if isnull(@available_guarantee_amt, 0) >= isnull(@paydepositamt, 0) begin set @available_guarantee_amt = @available_guarantee_amt-isnull(@paydepositamt, 0) set @factpaydepositamt = isnull(@paydepositamt, 0) end else begin set @factpaydepositamt = isnull(@available_guarantee_amt, 0) set @available_guarantee_amt = 0 end set @sumfactpaydepositamt = @sumfactpaydepositamt + @factpaydepositamt update gnm03 set gnc27f = isnull(@factpaydepositamt, 0) where current of cur_each_buyproc_by_deposit end fetch cur_each_buyproc_by_deposit into @paydepositamt end close cur_each_buyproc_by_deposit deallocate cur_each_buyproc_by_deposit ------------预设套餐指定使用的保证金---------------------------- declare cur_each_buycombo_by_deposit cursor for select gtf05f from gtc05 where gtf00c = @compid and gtf01c = @billid and isnull(gtf08i, 0)<>1 and isnull(gtf17i, 0) = 1 for update of gtf18f open cur_each_buycombo_by_deposit fetch cur_each_buycombo_by_deposit into @paydepositamt while @@fetch_status = 0 begin if isnull(@available_guarantee_amt, 0)>0 begin if isnull(@available_guarantee_amt, 0)>isnull(@paydepositamt, 0) begin set @available_guarantee_amt = @available_guarantee_amt-isnull(@paydepositamt, 0) set @factpaydepositamt = isnull(@paydepositamt, 0) end else begin set @factpaydepositamt = isnull(@available_guarantee_amt, 0) set @available_guarantee_amt = 0 end set @sumfactpaydepositamt = @sumfactpaydepositamt + @factpaydepositamt update gtc05 set gtf18f = isnull(@factpaydepositamt, 0) where current of cur_each_buycombo_by_deposit end fetch cur_each_buycombo_by_deposit into @paydepositamt end close cur_each_buycombo_by_deposit deallocate cur_each_buycombo_by_deposit end else if @billtype = 'gz' and isnull(@payment_guarantee_amt, 0)>0 begin ------------预设疗程指定使用的保证金---------------------------- declare cur_each_buyproc_by_deposit cursor for select isnull(gcm05f, 0) from gcm11 where gcm00c = @compid and gcm01c = @billid and isnull(gcm22i, 0)<>1 and isnull(gcm26i, 0) = 1 for update of gcm27f open cur_each_buyproc_by_deposit fetch cur_each_buyproc_by_deposit into @paydepositamt while @@fetch_status = 0 begin if isnull(@available_guarantee_amt, 0)>0 begin if isnull(@available_guarantee_amt, 0) >= isnull(@paydepositamt, 0) begin set @available_guarantee_amt = @available_guarantee_amt-isnull(@paydepositamt, 0) set @factpaydepositamt = isnull(@paydepositamt, 0) end else begin set @factpaydepositamt = isnull(@available_guarantee_amt, 0) set @available_guarantee_amt = 0 end set @sumfactpaydepositamt = @sumfactpaydepositamt + @factpaydepositamt update gcm11 set gcm27f = isnull(@factpaydepositamt, 0) where current of cur_each_buyproc_by_deposit end fetch cur_each_buyproc_by_deposit into @paydepositamt end close cur_each_buyproc_by_deposit deallocate cur_each_buyproc_by_deposit ------------预设套餐指定使用的保证金---------------------------- declare cur_each_buycombo_by_deposit cursor for select gth05f from gtc06 where gth00c = @compid and gth01c = @billid and isnull(gth08i, 0)<>1 and isnull(gth17i, 0) = 1 for update of gth18f open cur_each_buycombo_by_deposit fetch cur_each_buycombo_by_deposit into @paydepositamt while @@fetch_status = 0 begin if isnull(@available_guarantee_amt, 0)>0 begin if isnull(@available_guarantee_amt, 0)>isnull(@paydepositamt, 0) begin set @available_guarantee_amt = @available_guarantee_amt-isnull(@paydepositamt, 0) set @factpaydepositamt = isnull(@paydepositamt, 0) end else begin set @factpaydepositamt = isnull(@available_guarantee_amt, 0) set @available_guarantee_amt = 0 end set @sumfactpaydepositamt = @sumfactpaydepositamt + @factpaydepositamt update gtc06 set gth18f = isnull(@factpaydepositamt, 0) where current of cur_each_buycombo_by_deposit end fetch cur_each_buycombo_by_deposit into @paydepositamt end close cur_each_buycombo_by_deposit deallocate cur_each_buycombo_by_deposit end --开始准备支付方式的数组 if(exists(select 1 from gsm03 with (nolock) where gsc00c = @compid and gsc02c = @billtype and gsc01c = @billid and gsc04c = '5' and isnull(gsc05f,0)<> 0 )) begin select @pay_1 = gsc04c, @amt_1= gsc05f from gsm03 with (nolock) where gsc00c = @compid and gsc02c = @billtype and gsc01c = @billid and gsc04c = '5' set @i = 1 end declare cur_each_pay cursor for select gsc04c, gsc05f from gsm03 with (nolock) where gsc00c = @compid and gsc02c = @billtype and gsc01c = @billid and gsc04c not in (@payment_send,'5') -- and gsc04c <>@payment_send -- and gsc04c <>'&' order by gsc05f desc open cur_each_pay fetch cur_each_pay into @paymode, @payamt while @@fetch_status = 0 begin if (@i = 0) select @pay_1 = @paymode, @amt_1 = @payamt else if (@i = 1) select @pay_2 = @paymode, @amt_2 = @payamt else if (@i = 2) select @pay_3 = @paymode, @amt_3 = @payamt else if (@i = 3) select @pay_4 = @paymode, @amt_4 = @payamt else if (@i = 4) select @pay_5 = @paymode, @amt_5 = @payamt else if (@i = 5) select @pay_6 = @paymode, @amt_6 = @payamt else if (@i = 6) select @pay_7 = @paymode, @amt_7 = @payamt else if (@i = 7) select @pay_8 = @paymode, @amt_8 = @payamt select @i = @i + 1 fetch cur_each_pay into @paymode, @payamt end close cur_each_pay deallocate cur_each_pay --把赠送的支付方式放到最后 --参与随机分配的只是没有被指定的部分 if @i = 0 select @pay_1 = @payment_send, @amt_1 = @notfix_sent_amt, @i = @i + 1 else if @i = 1 select @pay_2 = @payment_send, @amt_2 = @notfix_sent_amt, @i = @i + 1 else if @i = 2 select @pay_3 = @payment_send, @amt_3 = @notfix_sent_amt, @i = @i + 1 else if @i = 2 select @pay_4 = @payment_send, @amt_4 = @notfix_sent_amt, @i = @i + 1 else if @i = 2 select @pay_5 = @payment_send, @amt_5 = @notfix_sent_amt, @i = @i + 1 else if @i = 2 select @pay_6 = @payment_send, @amt_6 = @notfix_sent_amt, @i = @i + 1 else if @i = 2 select @pay_7 = @payment_send, @amt_7 = @notfix_sent_amt, @i = @i + 1 else if @i = 2 select @pay_8 = @payment_send, @amt_8 = @notfix_sent_amt, @i = @i + 1 --如果有取用保证金的支付(没有被取用完的那部分), 也需要放到最后 -- 也就是需要随机支付的取用保证金 select @notfix_guarantee_amt = isnull(@payment_guarantee_amt, 0) - isnull(@sumfactpaydepositamt, 0) if @i = 0 select @pay_1 = '&', @amt_1 = @notfix_guarantee_amt else if @i = 1 select @pay_2 = '&', @amt_2 = @notfix_guarantee_amt else if @i = 2 select @pay_3 = '&', @amt_3 = @notfix_guarantee_amt else if @i = 2 select @pay_4 = '&', @amt_4 = @notfix_guarantee_amt else if @i = 2 select @pay_5 = '&', @amt_5 = @notfix_guarantee_amt else if @i = 2 select @pay_6 = '&', @amt_6 = @notfix_guarantee_amt else if @i = 2 select @pay_7 = '&', @amt_7 = @notfix_guarantee_amt else if @i = 2 select @pay_8 = '&', @amt_8 = @notfix_guarantee_amt --支付方式的数组已经准备好 --默认开始的值 select @remain_pay = @pay_1, @remain_pay_amt = @amt_1 declare @cur_proc_amt float -- 当前的疗程金额 declare @write_pos int -- 当前更新的疗程的支付情况 declare @need_next_pay int -- 是否需要下一个支付方式来帮忙,一旦取之后,则设置不用需要,用完则设置 declare @next_pay_record_no int --下一个应该被取得支付方式 declare @writed int declare @isneg int declare @need_to_next_record int select @need_to_next_record = 0 if @billtype = 'gn' begin -- 如果一个疗程是赠送的,则直接默认为赠送的支付方式以及金额, -- 因为支付明细中必然有对应的赠送的金额,所以直接更新到第一个位置。 -- 我们假设gsm03中的赠送支付的金额必然大于购买明细中指定的赠送 update gnm03 set gnc15c = @payment_send, gnc16f = gnc05f, gnc17c = '', gnc18f = 0, gnc19c = '', gnc20f = 0, gnc23c = '', gnc24f = 0 where gnc00c = @compid and gnc01c = @billid and isnull(gnc22i, 0) = 1 update gnm03 set gnc15c = '', gnc16f = 0, gnc17c = '', gnc18f = 0, gnc19c = '', gnc20f = 0, gnc23c = '', gnc24f = 0 where gnc00c = @compid and gnc01c = @billid and isnull(gnc22i, 0)<>1 declare cur_each_buyproc cursor for select case when isnull(gnc26i, 0) = 1 then isnull(gnc05f, 0)-isnull(gnc27f, 0) else gnc05f end from gnm03 where gnc00c = @compid and gnc01c = @billid and isnull(gnc02c, '')<>'' and isnull(gnc22i, 0)<>1 for update of gnc15c, gnc16f, gnc17c, gnc18f, gnc19c, gnc20f, gnc23c, gnc24f end else if @billtype = 'gz' -- 取款的时候需要特殊处理 begin update gcm11 set gcm15c = @payment_send, gcm16f = gcm05f, gcm17c = '', gcm18f = 0, gcm19c = '', gcm20f = 0, gcm23c = '', gcm24f = 0 where gcm00c = @compid and gcm01c = @billid and isnull(gcm22i, 0) = 1 update gcm11 set gcm15c = '', gcm16f = 0, gcm17c = '', gcm18f = 0, gcm19c = '', gcm20f = 0, gcm23c = '', gcm24f = 0 where gcm00c = @compid and gcm01c = @billid and isnull(gcm22i, 0)<>1 declare cur_each_buyproc cursor for select case when isnull(gcm26i, 0) = 1 then isnull(gcm05f, 0)-isnull(gcm27f, 0) else gcm05f end from gcm11 where gcm00c = @compid and gcm01c = @billid and isnull(gcm02c, '')<>'' and isnull(gcm22i, 0)<>1 for update of gcm15c, gcm16f, gcm17c, gcm18f, gcm19c, gcm20f, gcm23c, gcm24f end open cur_each_buyproc --支付方式从第一个开始 select @next_pay_record_no = 0 -- 可以用的当前支付方式位置 select @need_next_pay = 1 -- 需要取支付数据 select @write_pos = 0 fetch cur_each_buyproc into @cur_proc_amt while @@fetch_status = 0 begin -- 一开始进入疗程循环,默认不需要下一笔记录来处理 select @need_to_next_record = 0 if @need_next_pay = 1 begin -- 只有在前面一个用完之后才能取下一个支付方式 if @next_pay_record_no = 0 begin select @remain_pay = @pay_1, @remain_pay_amt = @amt_1 end else if @next_pay_record_no = 1 begin select @remain_pay = @pay_2, @remain_pay_amt = @amt_2 end else if @next_pay_record_no = 2 begin select @remain_pay = @pay_3, @remain_pay_amt = @amt_3 end else if @next_pay_record_no = 3 begin select @remain_pay = @pay_4, @remain_pay_amt = @amt_4 end else if @next_pay_record_no = 4 begin select @remain_pay = @pay_5, @remain_pay_amt = @amt_5 end else if @next_pay_record_no = 5 begin select @remain_pay = @pay_6, @remain_pay_amt = @amt_6 end else if @next_pay_record_no = 6 begin select @remain_pay = @pay_7, @remain_pay_amt = @amt_7 end else if @next_pay_record_no = 7 begin select @remain_pay = @pay_8, @remain_pay_amt = @amt_8 end select @need_next_pay = 0 --如果用到的支付方式为负数,则需要调整为正,但需要记住 if isnull(@remain_pay_amt, 0)<0 begin select @remain_pay_amt = 0 - @remain_pay_amt select @isneg = -1 end else if isnull(@remain_pay_amt, 0) > 0 begin select @isneg = 1 end else begin -- 没有可用的支付的钱了,可能存在0的疗程,则不需要处理了 break end end --疗程的金额小于支付方式的金额,本疗程可以直接处理结束 if @cur_proc_amt <= @remain_pay_amt begin --当前支付方式足够满足现在疗程的剩余金额了,当前的疗程就可以处理结束了,不需要取下一个支付方式了 select @writed = 0 if @billtype = 'gn' begin if @write_pos = 0 begin update gnm03 set gnc15c = @remain_pay, gnc16f = @cur_proc_amt where current of cur_each_buyproc select @writed = 1 end else if @write_pos = 1 begin update gnm03 set gnc17c = @remain_pay, gnc18f = @cur_proc_amt where current of cur_each_buyproc select @writed = 1 end else if @write_pos = 2 begin update gnm03 set gnc19c = @remain_pay, gnc20f = @cur_proc_amt where current of cur_each_buyproc select @writed = 1 end else if @write_pos = 3 begin update gnm03 set gnc23c = @remain_pay, gnc24f = @cur_proc_amt where current of cur_each_buyproc select @writed = 1 end end else if @billtype = 'gz' begin if @write_pos = 0 begin update gcm11 set gcm15c = @remain_pay, gcm16f = @isneg*@cur_proc_amt where current of cur_each_buyproc select @writed = 1 end else if @write_pos = 1 begin update gcm11 set gcm17c = @remain_pay, gcm18f = @isneg*@cur_proc_amt where current of cur_each_buyproc select @writed = 1 end else if @write_pos = 2 begin update gcm11 set gcm19c = @remain_pay, gcm20f = @isneg*@cur_proc_amt where current of cur_each_buyproc select @writed = 1 end else if @write_pos = 3 begin update gcm11 set gcm23c = @remain_pay, gcm24f = @isneg*@cur_proc_amt where current of cur_each_buyproc select @writed = 1 end end if @writed = 1 begin select @write_pos = @write_pos + 1 end --当前的疗程已经处理结束了 select @need_to_next_record = 1 --判断当前的支付方式是否已经用完了 if @remain_pay_amt = @cur_proc_amt begin --正好用完, 需要取下一个支付方式 select @need_next_pay = 1 select @next_pay_record_no = @next_pay_record_no + 1 select @remain_pay_amt = 0 end else begin --没有用完,把可以用的支付金额减少 select @remain_pay_amt = @remain_pay_amt - @cur_proc_amt end end else -- 疗程金额大于当前的支付的金额 begin --需要用下一个支付方式 select @need_next_pay = 1 select @next_pay_record_no = @next_pay_record_no + 1 --用第一个支付方式不够,后面的支付方式跟着上, 把第一个支付方式的所有的钱用掉 select @writed = 0 if @billtype = 'gn' begin if @write_pos = 0 begin update gnm03 set gnc15c = @remain_pay, gnc16f = @remain_pay_amt where current of cur_each_buyproc select @writed = 1 end else if @write_pos = 1 begin update gnm03 set gnc17c = @remain_pay, gnc18f = @remain_pay_amt where current of cur_each_buyproc select @writed = 1 end else if @write_pos = 2 begin update gnm03 set gnc19c = @remain_pay, gnc20f = @remain_pay_amt where current of cur_each_buyproc select @writed = 1 end else if @write_pos = 3 begin update gnm03 set gnc23c = @remain_pay, gnc24f = @remain_pay_amt where current of cur_each_buyproc select @writed = 1 end end else if @billtype = 'gz' begin if @write_pos = 0 begin update gcm11 set gcm15c = @remain_pay, gcm16f = @isneg*@remain_pay_amt where current of cur_each_buyproc select @writed = 1 end else if @write_pos = 1 begin update gcm11 set gcm17c = @remain_pay, gcm18f = @isneg*@remain_pay_amt where current of cur_each_buyproc select @writed = 1 end else if @write_pos = 2 begin update gcm11 set gcm19c = @remain_pay, gcm20f = @isneg*@remain_pay_amt where current of cur_each_buyproc select @writed = 1 end else if @write_pos = 3 begin update gcm11 set gcm23c = @remain_pay, gcm24f = @isneg*@remain_pay_amt where current of cur_each_buyproc select @writed = 1 end end --需要下一个支付方式,所以当前疗程没有处理完,但把疗程的金额可以减少了 --不需要去更新支付方式与支付的金额,马上就被重新取了 select @cur_proc_amt = @cur_proc_amt - @remain_pay_amt if @writed = 1 begin select @write_pos = @write_pos + 1 select @remain_pay_amt = 0 end end --循环可能是下一个支付方式而不是处理下一个疗程 if @need_next_pay = 0 or @need_to_next_record = 1 begin --处理下一个疗程 fetch cur_each_buyproc into @cur_proc_amt select @write_pos = 0 end end close cur_each_buyproc deallocate cur_each_buyproc select @remain_pay_amt = @remain_pay_amt * @isneg --下面处理套餐的支付 declare @has_combo int if @billtype = 'gn' begin if exists(select 1 from gtc05 with (nolock) where gtf00c = @compid and gtf01c = @billid) select @has_combo = 1 else select @has_combo = 0 end else if @billtype = 'gz' begin if exists(select 1 from gtc06 with (nolock) where gth00c = @compid and gth01c = @billid) select @has_combo = 1 else select @has_combo = 0 end if @has_combo = 1 begin select @need_to_next_record = 0 if @billtype = 'gn' begin update gtc05 set gtf09c = @payment_send, gtf10f = gtf05f, gtf11c = '', gtf12f = 0, gtf13c = '', gtf14f = 0, gtf15c = '', gtf16f = 0 where gtf00c = @compid and gtf01c = @billid and isnull(gtf08i, 0) = 1 update gtc05 set gtf09c = '', gtf10f = 0, gtf11c = '', gtf12f = 0, gtf13c = '', gtf14f = 0, gtf15c = '', gtf16f = 0 where gtf00c = @compid and gtf01c = @billid and isnull(gtf08i, 0)<>1 declare cur_each_buycombo cursor for select case when isnull(gtf17i, 0) = 1 then isnull(gtf05f, 0)-isnull(gtf18f, 0) else gtf05f end from gtc05 where gtf00c = @compid and gtf01c = @billid and isnull(gtf08i, 0)<>1 for update of gtf09c, gtf10f, gtf11c, gtf12f, gtf13c, gtf14f, gtf15c, gtf16f end else if @billtype = 'gz' -- 取款的时候需要特殊处理 begin update gtc06 set gth09c = @payment_send, gth10f = gth05f, gth11c = '', gth12f = 0, gth13c = '', gth14f = 0, gth15c = '', gth16f = 0 where gth00c = @compid and gth01c = @billid and isnull(gth08i, 0) = 1 update gtc06 set gth09c = '', gth10f = 0, gth11c = '', gth12f = 0, gth13c = '', gth14f = 0, gth15c = '', gth16f = 0 where gth00c = @compid and gth01c = @billid and isnull(gth08i, 0)<>1 declare cur_each_buycombo cursor for select case when isnull(gth17i, 0) = 1 then isnull(gth05f, 0)-isnull(gth18f, 0) else gth05f end from gtc06 where gth00c = @compid and gth01c = @billid and isnull(gth08i, 0)<>1 for update of gth09c, gth10f, gth11c, gth12f, gth13c, gth14f, gth15c, gth16f end open cur_each_buycombo select @write_pos = 0 declare @cur_combo_amt float fetch cur_each_buycombo into @cur_combo_amt while @@fetch_status = 0 begin --一旦进入套餐的循环,先设置不需要到下一笔记录 select @need_to_next_record = 0 if @need_next_pay = 1 begin --只有在前面一个用完之后才能取下一个支付方式 if @next_pay_record_no = 0 begin select @remain_pay = @pay_1, @remain_pay_amt = @amt_1 end else if @next_pay_record_no = 1 begin select @remain_pay = @pay_2, @remain_pay_amt = @amt_2 end else if @next_pay_record_no = 2 begin select @remain_pay = @pay_3, @remain_pay_amt = @amt_3 end else if @next_pay_record_no = 3 begin select @remain_pay = @pay_4, @remain_pay_amt = @amt_4 end else if @next_pay_record_no = 4 begin select @remain_pay = @pay_5, @remain_pay_amt = @amt_5 end else if @next_pay_record_no = 5 begin select @remain_pay = @pay_6, @remain_pay_amt = @amt_6 end else if @next_pay_record_no = 6 begin select @remain_pay = @pay_7, @remain_pay_amt = @amt_7 end else if @next_pay_record_no = 7 begin select @remain_pay = @pay_8, @remain_pay_amt = @amt_8 end select @need_next_pay = 0 -- 刚取所以假设前面的支付方式没有被用掉 --如果用到的支付方式为负数,则需要调整为正,但需要记住 if isnull(@remain_pay_amt, 0)<0 begin select @remain_pay_amt = 0-@remain_pay_amt select @isneg = -1 end else if isnull(@remain_pay_amt, 0)>0 begin select @isneg = 1 end else begin -- 没有可用的支付的钱了,可能存在0的疗程,则不需要处理了 break end end --疗程的金额小于支付方式的金额,本疗程可以直接处理结束 if convert(numeric(20, 2), @cur_combo_amt) <= convert(numeric(20, 2), @remain_pay_amt) begin --当前支付方式足够满足现在疗程的剩余金额了,当前的疗程就可以处理结束了,不需要取下一个支付方式了 select @writed = 0 if @billtype = 'gn' begin if @write_pos = 0 begin update gtc05 set gtf09c = @remain_pay, gtf10f = @cur_combo_amt where current of cur_each_buycombo select @writed = 1 end else if @write_pos = 1 begin update gtc05 set gtf11c = @remain_pay, gtf12f = @cur_combo_amt where current of cur_each_buycombo select @writed = 1 end else if @write_pos = 2 begin update gtc05 set gtf13c = @remain_pay, gtf14f = @cur_combo_amt where current of cur_each_buycombo select @writed = 1 end else if @write_pos = 3 begin update gtc05 set gtf15c = @remain_pay, gtf16f = @cur_combo_amt where current of cur_each_buycombo select @writed = 1 end end else if @billtype = 'gz' begin if @write_pos = 0 begin update gtc06 set gth09c = @remain_pay, gth10f = @isneg*@cur_combo_amt where current of cur_each_buycombo select @writed = 1 end else if @write_pos = 1 begin update gtc06 set gth11c = @remain_pay, gth12f = @isneg*@cur_combo_amt where current of cur_each_buycombo select @writed = 1 end else if @write_pos = 2 begin update gtc06 set gth13c = @remain_pay, gth14f = @isneg*@cur_combo_amt where current of cur_each_buycombo select @writed = 1 end else if @write_pos = 3 begin update gtc06 set gth15c = @remain_pay, gth16f = @isneg*@cur_combo_amt where current of cur_each_buycombo select @writed = 1 end end if @writed = 1 begin select @write_pos = @write_pos + 1 end --当前套餐已经支付结束,需要到下一笔套餐 select @need_to_next_record = 1 --判断当前的支付方式是否已经用完了 if @remain_pay_amt = @cur_combo_amt begin --正好用完, 需要取下一个支付方式 select @need_next_pay = 1 select @next_pay_record_no = @next_pay_record_no + 1 select @remain_pay_amt = 0 end else begin --没有用完,可以到下一笔套餐记录去了,把可以用的支付金额减少 select @remain_pay_amt = @remain_pay_amt - @cur_combo_amt end end else -- 套餐金额大于当前的支付的金额,把当前支付填进去,并需要下一个支付方式继续来支付 begin --需要用下一个支付方式 select @need_next_pay = 1 select @next_pay_record_no = @next_pay_record_no + 1 --用第一个支付方式不够,后面的支付方式跟着上, 把第一个支付方式的所有的钱用掉 select @writed = 0 if @billtype = 'gn' begin if @write_pos = 0 begin update gtc05 set gtf09c = @remain_pay, gtf10f = @remain_pay_amt where current of cur_each_buycombo select @writed = 1 end else if @write_pos = 1 begin update gtc05 set gtf11c = @remain_pay, gtf12f = @remain_pay_amt where current of cur_each_buycombo select @writed = 1 end else if @write_pos = 2 begin update gtc05 set gtf13c = @remain_pay, gtf14f = @remain_pay_amt where current of cur_each_buycombo select @writed = 1 end else if @write_pos = 3 begin update gtc05 set gtf15c = @remain_pay, gtf16f = @remain_pay_amt where current of cur_each_buycombo select @writed = 1 end end else if @billtype = 'gz' begin if @write_pos = 0 begin update gtc06 set gth09c = @remain_pay, gth10f = @isneg*@remain_pay_amt where current of cur_each_buycombo select @writed = 1 end else if @write_pos = 1 begin update gtc06 set gth11c = @remain_pay, gth12f = @isneg*@remain_pay_amt where current of cur_each_buycombo select @writed = 1 end else if @write_pos = 2 begin update gtc06 set gth13c = @remain_pay, gth14f = @isneg*@remain_pay_amt where current of cur_each_buycombo select @writed = 1 end else if @write_pos = 3 begin update gtc06 set gth15c = @remain_pay, gth16f = @isneg*@remain_pay_amt where current of cur_each_buycombo select @writed = 1 end end --需要下一个支付方式,所以当前疗程没有处理完,但把疗程的金额可以减少了 --不需要去更新支付方式与支付的金额,马上就被重新取了 select @cur_combo_amt = @cur_combo_amt - @remain_pay_amt if @writed = 1 begin select @write_pos = @write_pos + 1 select @remain_pay_amt = 0 end end --循环可能是下一个支付方式而不是处理下一个疗程 if @need_next_pay = 0 or @need_to_next_record = 1 begin --处理下一个疗程 fetch cur_each_buycombo into @cur_combo_amt select @write_pos = 0 end end close cur_each_buycombo deallocate cur_each_buycombo end --处理了疗程的支付明细的情况后,把留下的支付写到购买卡的明细中 --先把前面的支付情况清空 --注意: 这个地方我们假设的只购买一个会员卡,否则系统的数据就出现不对了 declare @max_card_payamt float -- 买卡本身得最大金额,如果留下的支付金额都超过这个数字,则必然存在购买套餐的情况 declare @set_amt float -- 更新到gnm02, gcm10中的金额 if @billtype = 'gn' begin select @max_card_payamt = gnb07f from gnm02 with (nolock) where gnb00c = @compid and gnb01c = @billid update gnm02 set gnb15c = '', gnb16f = 0, gnb17c = '', gnb18f = 0, gnb19c = '', gnb20f = 0, gnb21c = '', gnb22f = 0 where gnb00c = @compid and gnb01c = @billid end else if @billtype = 'gz' begin select @max_card_payamt = gcl53f from gcm10 with (nolock) where gcl00c = @compid and gcl01c = @billid update gcm10 set gcl67c = '', gcl68f = 0, gcl69c = '', gcl70f = 0, gcl71c = '', gcl72f = 0, gcl73c = '', gcl74f = 0 where gcl00c = @compid and gcl01c = @billid end select @write_pos = 0 while @next_pay_record_no<8 and isnull(@max_card_payamt, 0)>0 begin --先判断在疗程中是否用完了当前的支付方式,如果用完了,则取下一个支付方式 if isnull(@remain_pay_amt, 0)>0 begin --还有,当前的支付方式不需要动 print '继续用' + @remain_pay + '还有' + convert(varchar(30), @remain_pay_amt) select @need_next_pay = 0 end else begin if @need_next_pay = 1 begin --并需要取这个支付方式 if @next_pay_record_no = 0 begin select @remain_pay = @pay_1, @remain_pay_amt = @amt_1 end else if @next_pay_record_no = 1 begin select @remain_pay = @pay_2, @remain_pay_amt = @amt_2 end else if @next_pay_record_no = 2 begin select @remain_pay = @pay_3, @remain_pay_amt = @amt_3 end else if @next_pay_record_no = 3 begin select @remain_pay = @pay_4, @remain_pay_amt = @amt_4 end else if @next_pay_record_no = 4 begin select @remain_pay = @pay_5, @remain_pay_amt = @amt_5 end else if @next_pay_record_no = 5 begin select @remain_pay = @pay_6, @remain_pay_amt = @amt_6 end else if @next_pay_record_no = 6 begin select @remain_pay = @pay_7, @remain_pay_amt = @amt_7 end else if @next_pay_record_no = 7 begin select @remain_pay = @pay_8, @remain_pay_amt = @amt_8 end end select @need_next_pay = 0 print '开始用' + @remain_pay --下一个支付方式 end -- 可以用的支付的钱如果都没有了, 则不需要再更新了 if isnull(@remain_pay_amt, 0) = 0 break if @remain_pay_amt > @max_card_payamt begin select @set_amt = @max_card_payamt end else begin select @set_amt = @remain_pay_amt end if @billtype = 'gn' begin if @write_pos = 0 update gnm02 set gnb15c = @remain_pay, gnb16f = convert(decimal(30, 4), @set_amt) where gnb00c = @compid and gnb01c = @billid else if @write_pos = 1 update gnm02 set gnb17c = @remain_pay, gnb18f = convert(decimal(30, 4), @set_amt) where gnb00c = @compid and gnb01c = @billid else if @write_pos = 2 update gnm02 set gnb19c = @remain_pay, gnb20f = convert(decimal(30, 4), @set_amt) where gnb00c = @compid and gnb01c = @billid else if @write_pos = 3 update gnm02 set gnb21c = @remain_pay, gnb22f = convert(decimal(30, 4), @set_amt) where gnb00c = @compid and gnb01c = @billid end else if @billtype = 'gz' begin if @write_pos = 0 update gcm10 set gcl67c = @remain_pay, gcl68f = convert(decimal(30, 4), @set_amt) where gcl00c = @compid and gcl01c = @billid else if @write_pos = 1 update gcm10 set gcl69c = @remain_pay, gcl70f = convert(decimal(30, 4), @set_amt) where gcl00c = @compid and gcl01c = @billid else if @write_pos = 2 update gcm10 set gcl71c = @remain_pay, gcl72f = convert(decimal(30, 4), @set_amt) where gcl00c = @compid and gcl01c = @billid else if @write_pos = 3 update gcm10 set gcl73c = @remain_pay, gcl74f = convert(decimal(30, 4), @set_amt) where gcl00c = @compid and gcl01c = @billid end select @write_pos = @write_pos + 1 select @remain_pay_amt = @remain_pay_amt - isnull(@set_amt, 0) -- 可能还有套餐的支付 select @max_card_payamt = @max_card_payamt - isnull(@set_amt, 0) if @remain_pay_amt <= 0 begin select @need_next_pay = 1 -- 当前支付方式的金额用完了 select @next_pay_record_no = @next_pay_record_no + 1 end end -- 将开始预先设置的保证金分摊到当前的疗程和项目中去 if @billtype = 'gn' and isnull(@payment_guarantee_amt, 0)>0 begin --保证金的部分,直接放到最后的位置,也可能在第4的位置上已经有剩余的保证金分配了 --所以需要累加上去 update gnm03 set gnc23c = '&', gnc24f = isnull(gnc24f, 0) + isnull(gnc27f, 0) where gnc00c = @compid and gnc01c = @billid and isnull(gnc22i, 0)<>1 and isnull(gnc26i, 0) = 1 update gtc05 set gtf15c = '&', gtf16f = isnull(gtf16f, 0) + isnull(gtf18f, 0) where gtf00c = @compid and gtf01c = @billid and isnull(gtf08i, 0)<>1 and isnull(gtf17i, 0) = 1 end else if @billtype = 'gz' and isnull(@payment_guarantee_amt, 0)>0 begin --保证金的部分,直接放到最后的位置,也可能在第4的位置上已经有剩余的保证金分配了 --所以需要累加上去 update gcm11 set gcm23c = '&', gcm24f = isnull(gcm24f, 0) + isnull(gcm27f, 0) where gcm00c = @compid and gcm01c = @billid and isnull(gcm22i, 0)<>1 and isnull(gcm26i, 0) = 1 update gtc06 set gth15c = '&', gth16f = isnull(gth16f, 0) + isnull(gth18f, 0) where gth00c = @compid and gth01c = @billid and isnull(gth08i, 0)<>1 and isnull(gth17i, 0) = 1 end end GO /****** Object: StoredProcedure [dbo].[upg_check_notify_everyday_ex] Script Date: 06/12/2017 06:19:41 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_check_notify_everyday_ex]( @compid varchar(20), @ddate varchar(20), --阳历 @ydate varchar(20), --阴历 @ttime varchar(20) ) as begin --积分提醒 declare @maxamt float declare @limitdays float --卡到期提醒 declare @emplyeeleavedate float --离职开始提醒 declare @safemoney float --安全余额 declare @remaintimes float --疗程剩余次数 declare @booktime varchar(6) --预约提醒 declare @overdue float --疗程过期提醒 declare @resultdate varchar(8) declare @resulttime varchar(6) select @safemoney = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP028' select @limitdays = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP022' select @maxamt = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP061' select @emplyeeleavedate = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP068' select @booktime = gsa02c from gsm01 where gsa00c = @compid and gsa01c = 'SP068' select @remaintimes = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP069' --select @overdue = 0-gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'RP002' select @overdue = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'RP002' delete gam19 where (gaq00c = @compid or @compid = '001')and gaq06c<>'1' and gaq06c<>'10' and gaq06c<>'12' delete gam23 where (gau00c = @compid or @compid = '001')and gau01c<>'1' and gau01c<>'10'and gau01c<>'12' --检查卡疗程是否过期 execute upg_date_plus @ddate, @overdue, @resultdate output insert gam23(gau00c, gau01c, gau03c, gau04c, gau11c, gau05d, gau06f, gau07c, gau08c, gau09c, gau16d, gau17i) select distinct gcf00c, '21', gcf01c, cast(gba03c as varchar(20)), gcf02c, gcf12d, gcf10f, gba07c, gba08c, gba05c, @ddate, 0 from gcm06 a with (nolock), gbm01 b with (nolock) where a.gcf00c = b.gba00c and a.gcf01c = b.gba23c and gcf19i<>2 and gcf27i = 0 and (a.gcf00c = @compid or @compid = '001') --and a.gcf12d >= @resultdate and a.gcf12d<@ddate and a.gcf12d = @resultdate and a.gcf28i = 0 if exists(select 1 from gam23 where gau00c = @compid and gau01c = '21') begin select * from gam23 insert gam19(gaq00c, gaq02c, gaq03c, gaq04d, gaq05t, gaq06c, gaq08i, gaq09i) values(@compid, @compid, '疗程过期提醒', @ddate, @ttime, '21', 0, 0) end --检查会员生日 insert gam23(gau00c, gau01c, gau03c, gau04c, gau05d, gau07c, gau08c, gau09c, gau16d, gau17i) select distinct gba00c, '2', gba01c, cast(gba03c as varchar(20)), gba17d = case when isnull(gba21c, '')<>''then cast(gba21c as varchar(8)) else isnull(gba17d, '') end, gba07c, gba08c, gba05c, @ddate, 0 from gbm01 with (nolock) where gba00c = @compid and (substring(gba17d, 5, 4) = substring(@ddate, 5, 4)or substring(gba21c, 5, 4) = substring(@ydate, 5, 4)) if exists(select 1 from gam23 where gau00c = @compid and gau01c = '2') begin insert gam19(gaq00c, gaq02c, gaq03c, gaq04d, gaq05t, gaq06c, gaq08i, gaq09i) values(@compid, @compid, '会员生日提醒', @ddate, @ttime, '2', 0, 0) end --检查员工生日 insert gam23(gau00c, gau01c, gau03c, gau04c, gau05d, gau07c, gau08c, gau09c, gau16d, gau17i) select distinct haa00c, '3', haa01c, haa02c, haa14d, haa20c, haa21c, haa18c, @ddate, 0 from ham01 where (haa00c = @compid or @compid = '001')and haa27c<>'3' and substring(haa14d, 5, 4) = substring(@ddate, 5, 4) if exists (select 1 from gam23 where gau00c = @compid and gau01c = '3') begin insert gam19(gaq00c, gaq02c, gaq03c, gaq04d, gaq05t, gaq06c, gaq08i, gaq09i) values(@compid, @compid, '员工生日提醒', @ddate, @ttime, '3', 0, 0) end insert gam23(gau00c, gau01c, gau03c, gau04c, gau05d, gau07c, gau08c, gau09c, gau16d, gau17i) select distinct haa00c, '4', haa01c, haa02c, haa11d, haa20c, haa21c, haa18c, @ddate, 0 from ham01 where (haa00c = @compid or @compid = '001')and haa27c<>'3' and @emplyeeleavedate >= DATEDIFF(day, haa11d, @ddate) if exists (select 1 from gam23 where gau00c = @compid and gau01c = '4') begin insert gam19(gaq00c, gaq02c, gaq03c, gaq04d, gaq05t, gaq06c, gaq08i, gaq09i) values(@compid, @compid, '员工离职提醒', @ddate, @ttime, '4', 0, 0) end insert gam23(gau00c, gau01c, gau03c, gau04c, gau06f, gau07c, gau08c, gau09c, gau16d, gau17i) select distinct cast(gca13d as varchar(10)), '5', gcc01c, cast(gba03c as varchar(20)), gcc06f, gba07c, gba08c, gba05c, @ddate, 0 from gcm03 a with (nolock), gcm01 b with (nolock), gbm01 c with (nolock) where a.gcc00c = b.gca00c and a.gcc01c = b.gca01c and b.gca00c = c.gba00c and b.gca01c = c.gba23c and a.gcc03i = 2 and (gcc06f <= @safemoney and abs(@safemoney)>0.005) and (b.gca13d = @compid or @compid = '001') if exists(select 1 from gam23 where gau01c = '5' and gau00c = @compid) begin insert gam19(gaq00c, gaq02c, gaq03c, gaq04d, gaq05t, gaq06c, gaq08i, gaq09i) values(@compid, @compid, '安全余额提醒', @ddate, @ttime, '5', 0, 0) end insert gam23(gau00c, gau01c, gau03c, gau04c, gau06f, gau07c, gau08c, gau09c, gau16d, gau17i,gau11c) select distinct cast(gca13d as varchar(10)), '6', gcf01c, cast(gba03c as varchar(20)), gcf07f, gba07c, gba08c, gba05c, @ddate, 0,gcf02c from gcm06 a with (nolock), gbm01 b with (nolock), gcm01 c with (nolock) where a.gcf00c = b.gba00c and a.gcf01c = b.gba23c and a.gcf01c = c.gca01c and a.gcf00c = c.gca00c and (c.gca13d = @compid or @compid = '001') and (gcf07f <= @remaintimes and abs(@remaintimes)>0.005) if exists(select 1 from gam23 where gau00c = @compid and gau01c = '6') begin insert gam19(gaq00c, gaq02c, gaq03c, gaq04d, gaq05t, gaq06c, gaq08i, gaq09i) values(@compid, @compid, '疗程剩余次数提醒', @ddate, @ttime, '6', 0, 0) end execute upg_date_plus @ddate, @limitdays, @resultdate output insert gam23(gau00c, gau01c, gau03c, gau04c, gau05d, gau07c, gau08c, gau09c, gau16d, gau17i) select distinct cast(gca13d as varchar(10)), '7', gca01c, cast(gba03c as varchar(20)), gca07d, gba07c, gba08c, gba05c, @ddate, 0 from gcm01 a, gbm01 b where a.gca00c = b.gba00c and a.gca01c = b.gba23c and (a.gca13d = @compid or @compid = '001') and a.gca07d <= @resultdate and gca07d>@ddate if exists(select 1 from gam23 where gau00c = @compid and gau01c = '7') begin insert gam19(gaq00c, gaq02c, gaq03c, gaq04d, gaq05t, gaq06c, gaq08i, gaq09i) values(@compid, @compid, '会员卡到期提醒', @ddate, @ttime, '7', 0, 0) end set @ttime = left(@ttime, 6) execute time_add_plus @ttime, @booktime, @resulttime output insert gam23(gau00c, gau01c, gau03c, gau04c, gau05d, gau07c, gau08c, gau09c, gau10t, gau11c, gau12c, gau13c, gau16d, gau17i) select distinct gwa00c, '8', gwa04c, gwa05c, gwa01d, gau08c = case when substring(gwa04c, 1, 2) = '散客' then gwa07c else (select top 1 gba08c from gbm01 where gba01c = a.gwa04c) end, gau07c = case when substring(gwa04c, 1, 2) = '散客'then gwa07c else (select top 1 gba07c from gbm01 where gba01c = a.gwa04c )end, gau09c = case when substring(gwa04c, 1, 2) = '散客'then '散客' else (select gba05c from gbm01 where gba00c = a.gwa00c and gba23c = a.gwa04c) end, gwa02t, gwa13c = (select gda03c from gdm01 where gda00c = a.gwa00c and gda01c = a.gwa13c), gwa14c = (select gda03c from gdm01 where gda00c = a.gwa00c and gda01c = a.gwa14c), gwa15c = (select gda03c from gdm01 where gda00c = a.gwa00c and gda01c = a.gwa15c), @ddate, 0 from gwm01 a where a.gwa00c = @compid and a.gwa01d = @ddate and a.gwa02t <= substring(@resulttime, 1, 4) if exists (select 1 from gam23 where gau00c = @compid and gau01c = '8') begin insert gam19(gaq00c, gaq02c, gaq03c, gaq04d, gaq05t, gaq06c, gaq08i, gaq09i) values(@compid, @compid, '会员预约提醒', @ddate, @ttime, '08', 0, 0) end insert gam23(gau00c, gau01c, gau03c, gau04c, gau06f, gau07c, gau08c, gau09c, gau16d, gau17i) select distinct cast(gca13d as varchar(10)), '9', gcc01c, cast(gba03c as varchar(20)), gcc06f, gba07c, gba08c, gba05c, @ddate, 0 from gcm03 a with (nolock), gbm01 c with (nolock), gcm01 b with (nolock) where a.gcc00c = c.gba00c and a.gcc00c = b.gca00c and a.gcc01c = b.gca01c and a.gcc01c = c.gba23c and a.gcc03i = 3 and (a.gcc06f >= @maxamt and abs(gcc06f)>0.005) and (b.gca13d = @compid or @compid = '001') if exists(select 1 from gam23 where gau00c = @compid and gau01c = '9') begin insert gam19(gaq00c, gaq02c, gaq03c, gaq04d, gaq05t, gaq06c, gaq08i, gaq09i) values(@compid, @compid, '卡积分提醒', @ddate, @ttime, '9', 0, 0) end create table #result ( goodsno varchar(20) null, --产品编号 goodsname varchar(80) null, --产品名称 garageno varchar(20) null, -- 仓库编号 garagename varchar(80) null, --仓库名称 goodstype varchar(80) null, --产品类型 price float null, --单价 unit varchar(10) null, --单位 quantity float null, --数量 amt float null, stattype varchar(20) null -- 统计分 ) insert #result(goodsno, goodsname, garageno, garagename, goodstype, price, unit, quantity, amt, stattype) exec upg_stock_statistic @compid, @ddate, '*', '', '*', '', 1, 1.0, '*', '*', '', 0 if exists(select 1 from #result) begin if not exists(select 1 from gam19 where gaq00c = @compid and gaq06c = '11') begin insert gam19(gaq00c, gaq02c, gaq03c, gaq04d, gaq05t, gaq06c, gaq08i, gaq09i) values(@compid, @compid, '库存提醒', @ddate, @ttime, '11', 0, 0) end insert gam23(gau00c, gau01c, gau03c, gau17c, gau06f, gau18c, gau16d, gau17i) select distinct @compid, '11', goodsno, goodsname, quantity, unit, @ddate, 0 from #result where goodsno not in (select gau03c from gam23 where gau00c = @compid and gau01c = '11') end --疏远提醒 Create table #tblgcm20 ( gtc00c varchar(10) not null, gtc02c varchar(60) not null, --卡号 gba01c varchar(60) null, --会员编号 gct04d varchar(10) null, --消费日期 diff int, --多久没来 gba03c varchar(60) null, --会员姓名 gba07c varchar(60) null, --联系电话 gba08c varchar(60) null, --手机号码 gba14i varchar(5) null, --性别 gba10c varchar(60) null, --负责业务 ) --会员卡多久没来通知的天数(-1不检查) --为空则默认为30天 declare @sp220 float select @sp220 = isnull(gsa03f,30) from gsm01 where gsa00c = @compid and gsa01c = 'SP220' insert into #tblgcm20 exec upg_customer_etg @compid,@sp220,0 insert into gam23(gau00c,gau01c,gau03c,gau04c,gau05d,gau06f,gau07c,gau08c) select gtc00c,'13',gtc02c,gba03c,gct04d,diff,gba07c,gba08c from #tblgcm20 drop table #result drop table #tblgcm20 end GO /****** Object: StoredProcedure [dbo].[upg_update_hdm01] Script Date: 06/12/2017 06:20:50 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_update_hdm01] @compid varchar(10), @date varchar(8) as begin declare @userid varchar(20) set @userid = (select top 1 hda91c from hdm01 where hda00c = @compid and hda02d = @date) declare @curruser varchar(20) declare @currtype varchar(20)--班次 declare @starttime varchar(6) declare @endtime varchar(6) declare @result varchar(6) declare @result2 varchar(6) declare @dutystarttime varchar(6) --上班时间 declare @dutyendtime varchar(6) --下班时间 select @dutystarttime = gsa02c from gsm01 where gsa01c = 'SP050' and gsa00c = @compid select @dutyendtime = gsa02c from gsm01 where gsa01c = 'SP050' and gsa00c = @compid --这个时间内的所有班次 declare inserthdm01 cursor for select gicj01v, gicj02v, gicj03t, gicj04t from gicm10 where gicj00c = @compid and gicj05d <= @date and gicj06d >= @date open inserthdm01 fetch next from inserthdm01 into @curruser, @currtype, @starttime, @endtime while @@fetch_status = 0 begin if not exists(select 1 from hdm01 where hda00c = @compid and hda02d = @date and hda01c = @curruser and hda18c = @currtype) begin if (@starttime<'240000'and @endtime<@dutyendtime and @starttime<>'000000') begin execute time_sub_plus '240000', @starttime, @result2 output execute time_add_plus @result2, @endtime, @result output end else begin execute time_sub_plus @endtime, @starttime, @result output end insert hdm01(hda00c, hda01c, hda02d, hda03t, hda04t, hda91c, hda92d, hda93c, hda18c, hda07c, hda08i) values(@compid, @curruser, @date, @starttime, @starttime, @userid, @date, @userid, @currtype, '3', @result) end fetch next from inserthdm01 into @curruser, @currtype, @starttime, @endtime end close inserthdm01 deallocate inserthdm01 end GO /****** Object: StoredProcedure [dbo].[upg_stat_coupon_all] Script Date: 06/12/2017 06:20:43 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_stat_coupon_all] ( @incompid varchar(10), @fromdate varchar(20), @todate varchar(20), @datascope int , -- 0-所有 1-未消费 2-已经消费的 @billfrom varchar(20), @billto varchar(20), @cardfrom varchar(20), @cardto varchar(20) ) as begin if @datascope = 0 begin select isnull(gah00c, '') gah00c, isnull(gah01c,'') gah01c,isnull(gah02c,'') gah02c,isnull(gah03d,'') gah03d,isnull(gah04d,'') gah04d, isnull(gah05c,'') gah05c,isnull(gah06i,0) gah06i,isnull(gah07c,'') gah07c,isnull(gah08c,'') gah08c,isnull(gah09d,'') gah09d, isnull(gah10c,'') gah10c,isnull(gah11c,'') gah11c,isnull(gah12f,'') gah12f,isnull(gah13c,'') gah13c,isnull(gah14c,'') gah14c, isnull(gah15d,'') gah15d,isnull(gah16c,'') gah16c,isnull(gah17c,'') gah17c,isnull(gah18f,0) gah18f,isnull(gah19i,0) gah19i, isnull(gah20c,'') gah20c,isnull(gah21c,'') gah21c,isnull(gah22f,0) gah22f,isnull(gah23c,'') gah23c,isnull(gah24f,0) gah24f, isnull(gah25c,'') gah25c,isnull(gah26f,0) gah26f,isnull(gah27c,'') gah27c,isnull(gah28f,0) gah28f,isnull(gah29c,'') gah29c from gam08 a with(nolock), gam26 z with(nolock) where a.gah09d >= @fromdate and a.gah09d <= @todate and a.gah00c = z.gaz02c and z.gaz01c = @incompid and ((a.gah02c >= @billfrom and a.gah02c <= @billto) or @billfrom = '*') and ((a.gah10c >= @cardfrom and a.gah10c <= @cardto) or @cardfrom = '*') end else if @datascope = 1 begin select isnull(gah00c, '') gah00c, isnull(gah01c,'') gah01c,isnull(gah02c,'') gah02c,isnull(gah03d,'') gah03d,isnull(gah04d,'') gah04d, isnull(gah05c,'') gah05c,isnull(gah06i,0) gah06i,isnull(gah07c,'') gah07c,isnull(gah08c,'') gah08c,isnull(gah09d,'') gah09d, isnull(gah10c,'') gah10c,isnull(gah11c,'') gah11c,isnull(gah12f,'') gah12f,isnull(gah13c,'') gah13c,isnull(gah14c,'') gah14c, isnull(gah15d,'') gah15d,isnull(gah16c,'') gah16c,isnull(gah17c,'') gah17c,isnull(gah18f,0) gah18f,isnull(gah19i,0) gah19i, isnull(gah20c,'') gah20c,isnull(gah21c,'') gah21c,isnull(gah22f,0) gah22f,isnull(gah23c,'') gah23c,isnull(gah24f,0) gah24f, isnull(gah25c,'') gah25c,isnull(gah26f,0) gah26f,isnull(gah27c,'') gah27c,isnull(gah28f,0) gah28f,isnull(gah29c,'') gah29c from gam08 a with(nolock), gam26 z with(nolock) where a.gah09d >= @fromdate and a.gah09d <= @todate and a.gah00c = z.gaz02c and z.gaz01c = @incompid and isnull(a.gah06i, 0) = 0 and ((a.gah02c >= @billfrom and a.gah02c <= @billto) or @billfrom = '*') and ((a.gah10c >= @cardfrom and a.gah10c <= @cardto) or @cardfrom = '*') end else if @datascope = 2 begin select isnull(gah00c, '') gah00c, isnull(gah01c,'') gah01c,isnull(gah02c,'') gah02c,isnull(gah03d,'') gah03d,isnull(gah04d,'') gah04d, isnull(gah05c,'') gah05c,isnull(gah06i,0) gah06i,isnull(gah07c,'') gah07c,isnull(gah08c,'') gah08c,isnull(gah09d,'') gah09d, isnull(gah10c,'') gah10c,isnull(gah11c,'') gah11c,isnull(gah12f,'') gah12f,isnull(gah13c,'') gah13c,isnull(gah14c,'') gah14c, isnull(gah15d,'') gah15d,isnull(gah16c,'') gah16c,isnull(gah17c,'') gah17c,isnull(gah18f,0) gah18f,isnull(gah19i,0) gah19i, isnull(gah20c,'') gah20c,isnull(gah21c,'') gah21c,isnull(gah22f,0) gah22f,isnull(gah23c,'') gah23c,isnull(gah24f,0) gah24f, isnull(gah25c,'') gah25c,isnull(gah26f,0) gah26f,isnull(gah27c,'') gah27c,isnull(gah28f,0) gah28f,isnull(gah29c,'') gah29c from gam08 a with(nolock), gam26 z with(nolock) where a.gah09d >= @fromdate and a.gah09d <= @todate and a.gah00c = z.gaz02c and z.gaz01c = @incompid and isnull(a.gah06i, 0) = 1 and ((a.gah02c >= @billfrom and a.gah02c <= @billto) or @billfrom = '*') and ((a.gah10c >= @cardfrom and a.gah10c <= @cardto) or @cardfrom = '*') end end GO /****** Object: StoredProcedure [dbo].[upg_searchinfo_orderby] Script Date: 06/12/2017 06:20:39 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_searchinfo_orderby] ( @compid varchar(10), @date varchar(8), @time varchar(6), @type varchar(2), @condition varchar(20) -- 没有什么用 ) as begin create table #glm07 ( glg00c varchar(10) not null, -- 公司别 glg01c varchar(20) not null, -- 入院批号 glg02i int identity not null, -- 流水号 glg03c varchar(20) null, -- 钥匙牌号 glg04v varchar(20) null, -- 房间编号 glg05v varchar(20) null, -- 床位编号 glg06c varchar(20) null, -- 项目编号 glg08t varchar(6) null, -- 项目开始时间 glg09t varchar(6) null, -- 项目结束时间 glg10d varchar(8) null, -- 日期 glg11c varchar(20) null, -- 员工编号 glg12i int null, -- 是否已经结帐 0, 1是否已经结算 glg13i int null, -- 用于传递参数标记,选取状态 glg15c varchar(10) null, -- 员工类别, 指定或轮班 ) create table #result ( [key] varchar(20) null, room varchar(20) null, bed varchar(20) null, emplyee varchar(20) null, project varchar(20) null, remaintime varchar(6) null, starttime varchar(6) null, endtime varchar(6) null, ) if (@type = '2') -- 房间 begin insert #glm07(glg00c, glg01c, glg03c, glg04v, glg05v, glg06c, glg08t, glg09t, glg10d, glg11c, glg12i, glg13i, glg15c) select glg00c, glg01c, glg03c, glg04v, glg05v, substring(gda03c, 1, 20), glg08t, glg09t, glg10d, glg11c, glg12i, glg13i, glg15c from glm07, gdm01 where glg00c = gda00c and glg06c = gda01c and glg00c = @compid and glg10d = @date and (glg04v = @condition or @condition = '*') and ((glg08t <= @time and glg09t >= @time or glg08t >= @time) or isnull(glg13i, 0) = 2) and isnull(glg12i, 0) = 0 and isnull(glg13i, 0) != 1 order by glg09t asc end if (@type = '3') -- 床位 begin insert #glm07(glg00c, glg01c, glg03c, glg04v, glg05v, glg06c, glg08t, glg09t, glg10d, glg11c, glg12i, glg13i, glg15c) select glg00c, glg01c, glg03c, glg04v, glg05v, substring(gda03c, 1, 20), glg08t, glg09t, glg10d, glg11c, glg12i, glg13i, glg15c from glm07, gdm01 where glg00c = gda00c and glg06c = gda01c and glg00c = @compid and glg10d = @date and (glg05v = @condition or @condition = '*') and ((glg08t <= @time and glg09t >= @time or glg08t >= @time) or isnull(glg13i, 0) = 2) and isnull(glg12i, 0) = 0 and isnull(glg13i, 0) != 1 order by glg09t asc end if (@type = '1') -- 钥匙 begin insert #glm07(glg00c, glg01c, glg03c, glg04v, glg05v, glg06c, glg08t, glg09t, glg10d, glg11c, glg12i, glg13i, glg15c) select glg00c, glg01c, glg03c, glg04v, glg05v, substring(gda03c, 1, 20), glg08t, glg09t, glg10d, glg11c, glg12i, glg13i, glg15c from glm07, gdm01 where glg00c = gda00c and glg06c = gda01c and glg00c = @compid and glg10d = @date and (glg03c = @condition or @condition = '*') and (glg08t <= @time and glg09t >= @time or glg08t >= @time) and isnull(glg12i, 0) = 0 and isnull(glg13i, 0) = 0 order by glg09t asc end if (@type = '4') -- 员工 begin insert #glm07(glg00c, glg01c, glg03c, glg04v, glg05v, glg06c, glg08t, glg09t, glg10d, glg11c, glg12i, glg13i, glg15c) select glg00c, glg01c, glg03c, glg04v, glg05v, substring(gda03c, 1, 20), glg08t, glg09t, glg10d, glg11c, glg12i, glg13i, glg15c from glm07, gdm01 where glg00c = gda00c and glg06c = gda01c and glg00c = @compid and glg10d = @date and (glg11c = @condition or @condition = '*') and (glg08t <= @time and glg09t >= @time or glg08t >= @time) and isnull(glg12i, 0) = 0 and isnull(glg13i, 0) = 0 order by glg09t asc end declare @flag int declare @endtime varchar(6) declare @resulttime varchar(6) declare @key varchar(20) declare @room varchar(20) declare @bed varchar(20) declare @emplyee varchar(20) declare @project varchar(20) declare @starttime varchar(6) declare cursor_01 cursor for select glg02i, glg09t, glg03c, glg04v, glg05v, glg06c, glg11c, glg08t from #glm07 open cursor_01 fetch cursor_01 into @flag, @endtime, @key, @room, @bed, @project, @emplyee, @starttime while @@fetch_status = 0 begin execute time_sub_plus @endtime, @time, @resulttime output insert #result([key], room, bed, emplyee, project, remaintime, starttime, endtime) values(@key, @room, @bed, @emplyee, @project, @resulttime, @starttime, @endtime) fetch cursor_01 into @flag, @endtime, @key, @room, @bed, @project, @emplyee, @starttime end close cursor_01 deallocate cursor_01 delete #glm07 if (@type = '2') begin select max([key]) as [key], room, max(bed) as bed, max(emplyee)as emplyee, max(project)as project, max(remaintime) as remaintime, min(starttime) as starttime, max(endtime) as endtime from #result group by room end else if (@type = '4') -- 员工 begin select max([key]) as [key], room, max(bed) as bed, emplyee, max(project)as project, max(remaintime) as remaintime, min(starttime) as starttime, max(endtime) as endtime from #result group by room, emplyee order by remaintime, emplyee end else if (@type = '1') -- 钥匙 begin select * from #result order by [key], remaintime end else select * from #result end GO /****** Object: StoredProcedure [dbo].[upg_createout_goods] Script Date: 06/12/2017 06:19:54 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_createout_goods] @compid varchar(10), -- 公司单号 @billid varchar(20), -- 收银单号 @cardid varchar(20), --会员卡号 @memname varchar(20), --会员名称 @flag int, --复合标志 @savebill varchar(20), @userid varchar(20), @date varchar(8), @time varchar(6) as set nocount on set rowcount 0 set transaction isolation level read uncommitted begin begin try if (substring(@cardid, 1, 4) = '散客') return --新增事务 2016/06/07 begin transaction if (@flag = 1) begin create table #ggm03_buf -- 项目消费--产品明细 ( ggc00c varchar(10) not null, --公司编号 ggc01c varchar(20) not null, --消费单号 ggc03c varchar(20) null, --产品代码 ggc02f float not null, --序号 ggc05f float null, --数量 ggc37f float null, --标准单位数量 ggc12c varchar(20) null, --产品销售人员 ggc15c varchar(20) null, --第二人 ggc17c varchar(20) null, --第三人 ggc36i int null --方式 0带走 1寄存 2欠货 ) create nonclustered index idx_ggm03_buf on #ggm03_buf(ggc00c,ggc01c,ggc03c,ggc02f,ggc36i) insert #ggm03_buf(ggc00c, ggc01c, ggc03c, ggc02f, ggc05f, ggc37f) select ggc00c, ggc01c, ggc03c, ggc02f, sum(ggc05f), sum(ggc37f) from ggm03 with (nolock) where ggc00c = @compid and ggc01c = @billid and ggc36i = 2 group by ggc00c, ggc01c, ggc03c, ggc02f update a set a.ggc12c = isnull(b.ggc12c, ''), a.ggc15c = isnull(b.ggc15c, ''), a.ggc17c = isnull(b.ggc17c, '') from #ggm03_buf a, ggm03 b with (nolock) where a.ggc00c = b.ggc00c and a.ggc01c = b.ggc01c and a.ggc03c = b.ggc03c and a.ggc02f = b.ggc02f and b.ggc36i = 2 --欠货 delete gcm22 where gcp00c = @compid and gcp01c = @billid --gcm22缺货纪录 insert gcm22(gcp00c, gcp01c, gcp02f, gcp03c, gcp04c, gcp05c, gcp06f, gcp07f) select ggc00c, ggc01c, ggc02f, ggc03c, @cardid, ggc04c, ggc05f, ggc11f from ggm03 with(nolock) where ggc00c = @compid and ggc01c = @billid and ggc36i = 2 drop table #ggm03_buf end else if (@flag = 0) begin delete gcm22 where gcp00c = @compid and gcp01c = @billid end commit transaction end try begin catch if(@@trancount > 0) begin rollback transaction end end catch end GO /****** Object: StoredProcedure [dbo].[upg_copy_staffinfo] Script Date: 06/12/2017 06:19:48 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_copy_staffinfo] ( @compid varchar(10), @fromstaff varchar(10), @tostaff varchar(10), @type int -- 1-的时候是拷贝人头数 2-的时候是拷贝提成 3-总业绩分段提成 ) as begin if (@type = 1) begin delete ham14 where han00c = @compid and han01c = @tostaff insert into ham14(han00c, han01c, han02f, han03f, han04f, han05i, han06f, han07c) select han00c, @tostaff, han02f, han03f, han04f, han05i, han06f, han07c from ham14 where han00c = @compid and han01c = @fromstaff end else if (@type = 2) begin delete ham15 where hap00c = @compid and hap01c = @tostaff insert into ham15(hap00c, hap01c, hap02c, hap03i, hap04f, hap05i, hap06f, hap07i, hap08f, hap09i, hap10f, hap11i, hap12f, hap13i, hap14f, hap15i, hap16f, hap17i, hap18f, hap19i, hap20f, hap21i, hap22f, hap23i, hap24f, hap25i, hap26f) select hap00c, @tostaff, hap02c, hap03i, hap04f, hap05i, hap06f, hap07i, hap08f, hap09i, hap10f, hap11i, hap12f, hap13i, hap14f, hap15i, hap16f, hap17i, hap18f, hap19i, hap20f, hap21i, hap22f, hap23i, hap24f, hap25i, hap26f from ham15 where hap00c = @compid and hap01c = @fromstaff end else if (@type = 3) begin delete ham16 where haq00c = @compid and haq01c = @tostaff insert into ham16(haq00c, haq01c, haq02f, haq03f, haq04f, haq05i, haq06f, haq08c) select haq00c, haq01c, haq02f, haq03f, haq04f, haq05i, haq06f, haq08c from ham16 where haq00c = @compid and haq01c = @fromstaff end end GO /****** Object: StoredProcedure [dbo].[upg_cal_sum_comm] Script Date: 06/12/2017 06:19:34 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_cal_sum_comm] ( @compid varchar(10), @amt float, -- 营业收入 @vperf float, -- 虚业绩金额 @tperf float, -- 实业绩 @quan float, -- 数量 @f_amt float, -- 指定营业收入 @f_vperf float, -- 指定虚业绩金额 @f_tperf float, -- 指定实业绩 @f_quan float, -- 指定数量 @t_amt float, -- 轮班营业收入 @t_vperf float, -- 轮班虚业绩金额 @t_tperf float, -- 轮班实业绩 @t_quan float, -- 轮班数量 @staff varchar(20), -- 员工编号 @cls_flag int, -- 类别, 1:项目;2:产品:3:卡 4:员工总合计 5店营业总合计 @cls_type varchar(20), -- 项目类别 @total_f_quan float, -- 总的这个员工的指定客数,因为分段标准可能是总的指定客数 @total_cust_num float, -- 总的客户数量 @comm float output, -- 佣金 @fa_type varchar(20), --提成方案编号 @f_cash_vperf float, --指定现金业绩 @t_cash_vperf float --轮班现金业绩 ) as--wuxm, 2004/12/28 begin declare @remain float -- 用来计算业绩是否达到标准的指标 declare @pf_remain float -- 指定业绩的剩余值 declare @pt_remain float -- 轮班业绩的剩余值 declare @i int declare @compare_kind int -- 分段标准,可能是金额(1),指定客数(2),回头率(3) ,总指定客数(4) 总人数(5) 指定业绩(6) 营业额(7) declare @fromamt float declare @toamt float declare @f_mode int -- 指定的提成方式 declare @t_mode int -- 轮班的提成方式 declare @f_setvalue float declare @t_setvalue float declare @Sum_toamt float declare @cash_vperf_total float set @cash_vperf_total = isnull(@f_cash_vperf,0) + isnull(@t_cash_vperf,0) declare @sp0352 varchar(2) select @sp0352 = isnull(pre04i, 1) from precept where pre00c = @compid and pre01c = @fa_type and isnull(pre22i, 0) = 0 if @cls_flag = 2 or @cls_flag = 3 begin -- 产品,与卡 没有所谓的指定与轮班,就默认为轮班的 select @t_amt = @amt, @t_vperf = @vperf, @t_tperf = @tperf, @t_quan = @quan end -- 回头率 指定数量/总数量 declare @fix_rate float -- 被除数不可以为0 if (@quan = 0) set @fix_rate = 0 else select @fix_rate = @f_quan / @quan -- 计算回头率 select @i = 0 select @comm = 0 if (@cls_flag = 1) -- 项目 begin declare cur_level_comm cursor for select hec03f, hec04f, hec05i, hec06f, hec08i, hec09f, hec10c from hem03 where hec00c = @compid and hec01c = @fa_type and isnull(hec07c, '') = @cls_type order by hec03f desc -- 获取最大累计截至业绩 select @Sum_toamt = max(hec04f) from hem03 where hec00c = @compid and hec01c = @fa_type and isnull(hec07c, '') = @cls_type end else if (@cls_flag = 2) -- 产品 begin declare cur_level_comm cursor for select hed03f, hed04f, hed05i, hed06f, hed05i, hed06f, hed08c from hem04 where hed00c = @compid and hed01c = @fa_type and isnull(hed07c, '') = @cls_type order by hed03f desc -- 获取最大累计截至业绩 select @Sum_toamt = max(hed04f) from hem04 where hed00c = @compid and hed01c = @fa_type and isnull(hed07c, '') = @cls_type end else if (@cls_flag = 3) -- 会员卡 begin -- 如果是会员卡,则没有传送指定业绩以及轮班业绩进来,都是0,所以必须设置为全部为指定业绩就是营业额 select @f_amt = isnull(@amt, 0) declare cur_level_comm cursor for select hee03f, hee04f, hee05i, hee06f, hee05i, hee06f, hee08c from hem05 where hee00c = @compid and hee01c = @fa_type and isnull(hee07c, '') = @cls_type order by hee03f desc -- 获取最大累计截至业绩 select @Sum_toamt = max(hee04f) from hem05 where hee00c = @compid and hee01c = @fa_type and isnull(hee07c, '') = @cls_type end else if (@cls_flag = 4) -- 总累计 begin declare cur_level_comm cursor for select hef03f, hef04f, hef05i, hef06f, hef05i, hef06f, hef08c from hem06 where hef00c = @compid and hef01c = @fa_type order by hef03f desc -- 获取最大累计截至业绩 select @Sum_toamt = max(hef04f) from hem06 where hef00c = @compid and hef01c = @fa_type end else if (@cls_flag = 5) -- 店总业绩 begin declare cur_level_comm cursor for select haq03f, haq04f, haq05i, haq06f, haq05i, haq06f, haq08c from ham16 where haq00c = @compid and haq01c = @fa_type order by haq03f desc -- 获取最大累计截至业绩 select @Sum_toamt = max(haq04f) from ham16 where haq00c = @compid and haq01c = @fa_type end else if (@cls_flag = 6) -- 按人头数 begin declare cur_level_comm cursor for select han03f, han04f, han05i, han06f, han05i, han06f, '1' from ham14 where han00c = @compid and han01c = @fa_type order by han03f desc -- 获取最大累计截至业绩 select @Sum_toamt = max(han04f) from ham14 where han00c = @compid and han01c = @fa_type end -- 注意是倒序的排列 -- @f_mode, @f_setvalue, @t_mode, @t_setvalue 先指定,后轮班 open cur_level_comm fetch cur_level_comm into @fromamt, @toamt, @f_mode, @f_setvalue, @t_mode, @t_setvalue, @compare_kind while @@fetch_status = 0 begin -- 如果轮班没有设置,则默认为指定的设置 select @t_mode = isnull(@t_mode, @f_mode), @t_setvalue = isnull(@t_setvalue, @f_setvalue) if @cls_flag = 5 -- 店总业绩不区分指定和轮班 begin select @remain = isnull(@amt, 0) end else begin if @i = 0 begin -- 指定的方式 if @f_mode = 1 begin select @remain = isnull(@quan, 0), @pf_remain = isnull(@f_quan, 0), @pt_remain = isnull(@t_quan, 0) end else if @f_mode = 16 begin select @remain = isnull(@cash_vperf_total, 0), @pf_remain = isnull(@f_cash_vperf, 0), @pt_remain = isnull(@t_cash_vperf, 0) end else if @f_mode = 2 or @f_mode = 7 or @f_mode = 8 or @f_mode = 9 begin select @remain = isnull(@amt, 0), @pf_remain = isnull(@f_amt, 0), @pt_remain = isnull(@t_amt, 0) end else if @f_mode = 3 or @f_mode = 93 or @f_mode = 94 or @f_mode = 95 begin select @remain = isnull(@tperf, 0), @pf_remain = isnull(@f_tperf, 0), @pt_remain = isnull(@t_tperf, 0) end else if @f_mode = 4 or @f_mode = 90 or @f_mode = 91 or @f_mode = 92 begin select @remain = isnull(@vperf, 0), @pf_remain = isnull(@f_vperf, 0), @pt_remain = isnull(@t_vperf, 0) end -- 轮班的方式,因为有可能轮班与指定的提成基数不同 if @t_mode = 1 begin select @pt_remain = isnull(@t_quan, 0) end else if @f_mode = 16 begin select @pt_remain = isnull(@t_cash_vperf, 0) end else if @t_mode = 2 or @t_mode = 7 or @t_mode = 8 or @t_mode = 9 begin select @pt_remain = isnull(@t_amt, 0) end else if @t_mode = 3 or @t_mode = 93 or @t_mode = 94 or @t_mode = 95 begin select @pt_remain = isnull(@t_tperf, 0) end else if @t_mode = 4 or @t_mode = 90 or @t_mode = 91 or @t_mode = 92 begin select @pt_remain = isnull(@t_vperf, 0) end end else begin -- 如果是按最高标准,是可以重新取设置的 if @sp0352 = '1' -- 按最高标准来计算提成 begin -- 指定的方式 if @f_mode = 1 begin select @remain = isnull(@quan, 0), @pf_remain = isnull(@f_quan, 0), @pt_remain = isnull(@t_quan, 0) end else if @f_mode = 16 begin select @remain = isnull(@cash_vperf_total, 0), @pf_remain = isnull(@f_cash_vperf, 0), @pt_remain = isnull(@t_cash_vperf, 0) end else if @f_mode = 2 or @f_mode = 7 or @f_mode = 8 or @f_mode = 9 begin select @remain = isnull(@amt, 0), @pf_remain = isnull(@f_amt, 0), @pt_remain = isnull(@t_amt, 0) end else if @f_mode = 3 or @f_mode = 93 or @f_mode = 94 or @f_mode = 95 begin select @remain = isnull(@tperf, 0), @pf_remain = isnull(@f_tperf, 0), @pt_remain = isnull(@t_tperf, 0) end else if @f_mode = 4 or @f_mode = 90 or @f_mode = 91 or @f_mode = 92 begin select @remain = isnull(@vperf, 0), @pf_remain = isnull(@f_vperf, 0), @pt_remain = isnull(@t_vperf, 0) end -- 轮班的方式 if @t_mode = 1 begin select @pt_remain = isnull(@t_quan, 0) end else if @f_mode = 16 begin select @pt_remain = isnull(@t_cash_vperf, 0) end else if @t_mode = 2 or @t_mode = 7 or @t_mode = 8 or @t_mode = 9 begin select @pt_remain = isnull(@t_amt, 0) end else if @t_mode = 3 or @t_mode = 93 or @t_mode = 94 or @t_mode = 95 begin select @pt_remain = isnull(@t_tperf, 0) end else if @t_mode = 4 or @t_mode = 90 or @t_mode = 91 or @t_mode = 92 begin select @pt_remain = isnull(@t_vperf, 0) end end end end select @i = @i + 1 if @compare_kind = 2 -- 指定客户数分段标准 begin if (isnull(@f_quan, 0) >= @fromamt or (isnull(@f_quan, 0) = 0 and @fromamt = 0)) and isnull(@f_quan, 0) <= @toamt begin select @comm = isnull(@comm, 0) + isnull(@pf_remain, 0) * @f_setvalue select @comm = isnull(@comm, 0) + isnull(@pt_remain, 0) * @t_setvalue select @remain = 0, @pf_remain = 0, @pt_remain = 0 end end else if @compare_kind = 4 -- 按总指定客户数分段标准 begin if (isnull(@total_f_quan, 0) >= @fromamt or (isnull(@total_f_quan, 0) = 0 and @fromamt = 0)) and isnull(@total_f_quan, 0) <= @toamt begin select @comm = isnull(@comm, 0) + isnull(@pf_remain, 0) * @f_setvalue select @comm = isnull(@comm, 0) + isnull(@pt_remain, 0) * @t_setvalue select @remain = 0, @pf_remain = 0, @pt_remain = 0 end end else if @compare_kind = 5 -- 按总人数分段标准 begin if @sp0352 = '1' -- 按最高标准来计算提成 begin if (@quan > @Sum_toamt) begin select @comm = isnull(@comm, 0) + isnull(@pf_remain, 0) * @f_setvalue select @comm = isnull(@comm, 0) + isnull(@pt_remain, 0) * @t_setvalue break end if (isnull(@quan, 0) >= @fromamt or (isnull(@quan, 0) = 0 and @fromamt = 0)) and isnull(@quan, 0) <= @toamt begin select @comm = isnull(@comm, 0) + isnull(@pf_remain, 0) * @f_setvalue select @comm = isnull(@comm, 0) + isnull(@pt_remain, 0) * @t_setvalue select @remain = 0, @pf_remain = 0, @pt_remain = 0 end end -- 按分段标准该如何计算, 无法计算 end else if @compare_kind = 8 -- 按总客数分段标准 begin if @sp0352 = '1' -- 按最高标准来计算提成 begin if (@total_cust_num >= @Sum_toamt) begin select @comm = isnull(@comm, 0) + isnull(@pf_remain, 0) * @f_setvalue select @comm = isnull(@comm, 0) + isnull(@pt_remain, 0) * @t_setvalue break end if (isnull(@f_quan, 0) >= @fromamt or (isnull(@f_quan, 0) = 0 and @fromamt = 0)) and isnull(@f_quan, 0) <= @toamt begin select @comm = isnull(@comm, 0) + isnull(@pf_remain, 0) * @f_setvalue select @comm = isnull(@comm, 0) + isnull(@pt_remain, 0) * @t_setvalue select @remain = 0, @pf_remain = 0, @pt_remain = 0 end end -- 按分段标准该如何计算, 无法计算 end else if @compare_kind = 6 -- 按指定业绩分段标准 begin if @sp0352 = '1' -- 按最高标准来计算提成 begin if (@pt_remain > @Sum_toamt) begin select @comm = isnull(@comm, 0) + isnull(@pf_remain, 0) * @f_setvalue select @comm = isnull(@comm, 0) + isnull(@pt_remain, 0) * @t_setvalue break end if (isnull(@pf_remain, 0) >= @fromamt or (isnull(@pf_remain, 0) = 0 and @fromamt = 0)) and isnull(@pf_remain, 0) <= @toamt begin select @comm = isnull(@comm, 0) + isnull(@pf_remain, 0) * @f_setvalue select @comm = isnull(@comm, 0) + isnull(@pt_remain, 0) * @t_setvalue select @remain = 0, @pf_remain = 0, @pt_remain = 0 end end -- 按分段标准该如何计算, 无法计算 end else if @compare_kind = 7 -- 按营业额分段标准 begin if @sp0352 = '1' -- 只能按最高标准来计算提成 begin if (@amt > @Sum_toamt) begin select @comm = isnull(@comm, 0) + isnull(@pf_remain, 0) * @f_setvalue select @comm = isnull(@comm, 0) + isnull(@pt_remain, 0) * @t_setvalue break end if (isnull(@amt, 0) >= @fromamt or (isnull(@amt, 0) = 0 and @fromamt = 0)) and isnull(@amt, 0) <= @toamt begin if @cls_flag = 1 begin select @comm = isnull(@comm, 0) + isnull(@pf_remain, 0) * @f_setvalue select @comm = isnull(@comm, 0) + isnull(@pt_remain, 0) * @t_setvalue select @remain = 0, @pf_remain = 0, @pt_remain = 0 end else begin select @comm = isnull(@comm, 0) + isnull(@remain, 0) * @f_setvalue select @remain = 0, @pf_remain = 0, @pt_remain = 0 end end end -- 按分段标准该如何计算, 无法计算 end else if @compare_kind = 9 -- 虚业绩分段标准 begin if @sp0352 = '1' -- 只能按最高标准来计算提成 begin if (@vperf > @Sum_toamt) begin select @comm = isnull(@comm, 0) + isnull(@pf_remain, 0) * @f_setvalue select @comm = isnull(@comm, 0) + isnull(@pt_remain, 0) * @t_setvalue break end if (isnull(@vperf, 0) >= @fromamt or (isnull(@vperf, 0) = 0 and @fromamt = 0)) and isnull(@vperf, 0) <= @toamt begin if @cls_flag = 1 begin select @comm = isnull(@comm, 0) + isnull(@pf_remain, 0) * @f_setvalue select @comm = isnull(@comm, 0) + isnull(@pt_remain, 0) * @t_setvalue select @remain = 0, @pf_remain = 0, @pt_remain = 0 end else begin select @comm = isnull(@comm, 0) + isnull(@remain, 0) * @f_setvalue select @remain = 0, @pf_remain = 0, @pt_remain = 0 end end end -- 按分段标准该如何计算, 无法计算 end else if @compare_kind = 10 -- 总现金分段标准 begin if @sp0352 = '1' -- 只能按最高标准来计算提成 begin if (@cash_vperf_total > @Sum_toamt) begin select @comm = isnull(@comm, 0) + isnull(@pf_remain, 0) * @f_setvalue select @comm = isnull(@comm, 0) + isnull(@pt_remain, 0) * @t_setvalue break end if (@cash_vperf_total >= @fromamt or @cash_vperf_total = 0 and @fromamt = 0) and @cash_vperf_total <= @toamt begin if @cls_flag = 1 begin select @comm = isnull(@comm, 0) + isnull(@pf_remain, 0) * @f_setvalue select @comm = isnull(@comm, 0) + isnull(@pt_remain, 0) * @t_setvalue select @remain = 0, @pf_remain = 0, @pt_remain = 0 end else begin select @comm = isnull(@comm, 0) + isnull(@remain, 0) * @f_setvalue select @remain = 0, @pf_remain = 0, @pt_remain = 0 end end end -- 按分段标准该如何计算, 无法计算 end else if @compare_kind = 3 -- 回头率标准 begin if @fix_rate > @fromamt and @fix_rate <= @toamt begin select @comm = isnull(@comm, 0) + isnull(@pf_remain, 0) * @f_setvalue select @comm = isnull(@comm, 0) + isnull(@pt_remain, 0) * @t_setvalue select @remain = 0, @pf_remain = 0, @pt_remain = 0 end end else if @compare_kind = 1 -- 指定金额类型 begin -- 分段来计算提成,也就是按高标准来计算提成 -- 也只种方式能支持指定总业绩与轮班总业绩分开来计算 -- 指标是总的指标, 只是在计算提成的时候比率不同 if @sp0352 = '1' begin if (@remain > @Sum_toamt) begin select @comm = isnull(@comm, 0) + isnull(@pf_remain, 0) * @f_setvalue select @comm = isnull(@comm, 0) + isnull(@pt_remain, 0) * @t_setvalue break end if @remain >= @fromamt and @remain <= @toamt begin if @cls_flag = 1 begin select @comm = isnull(@comm, 0) + @pf_remain * @f_setvalue select @comm = isnull(@comm, 0) + @pt_remain * @t_setvalue select @remain = 0, @pf_remain = 0, @pt_remain = 0 end else -- 非项目只能有一个设置值 begin select @comm = isnull(@comm, 0) + @remain * @f_setvalue select @remain = 0 end end -- 按最高标准计算提成, 则不在范围内则不考虑 -- else if @remain > @toamt --begin -- select @comm = @comm + @toamt * @f_setvalue -- select @remain = @remain - @toamt --end end else -- 分段来计算提成,而且每段按自己的标准来计算,只有超过部分按高标准来计算提成 begin if @remain >= @fromamt and @remain <= @toamt begin select @comm = isnull(@comm, 0) + (@remain - @fromamt) * @f_setvalue select @remain = @fromamt end else if @remain >= @fromamt and isnull(@toamt, -1) = -1 begin select @comm = isnull(@comm, 0) + (@remain - @fromamt) * @f_setvalue select @remain = @fromamt end else if @remain >= @toamt begin select @comm = isnull(@comm, 0) + (@remain - @fromamt) * @f_setvalue select @remain = @fromamt end end end else if isnull(@compare_kind, 0) = 0 -- 指定类型为空 begin if @remain >= @fromamt and @remain <= @toamt begin if @f_mode = 1 -- 固定金额 begin select @comm = isnull(@comm, 0) + @f_setvalue end else begin select @comm = isnull(@comm, 0) + @remain * @f_setvalue end select @remain = 0 end end fetch cur_level_comm into @fromamt, @toamt, @f_mode, @f_setvalue, @t_mode, @t_setvalue, @compare_kind end close cur_level_comm deallocate cur_level_comm end GO /****** Object: StoredProcedure [dbo].[upg_m_dispatch_hem01_single_mandatory] Script Date: 06/12/2017 06:20:21 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_m_dispatch_hem01_single_mandatory] ( @fromcompid varchar(10), -- 从哪一个公司发布 @tocompid varchar(10), -- 目的公司 @pre01c varchar(20) -- ) as begin if @fromcompid = @tocompid return if exists (select 1 from precept where pre00c = @tocompid and pre01c = @pre01c) return insert precept(pre00c, pre01c, pre02c, pre03i, pre04i, pre05i, pre06i, pre07f, pre08f, pre09i, pre10f, pre11f, pre12i, pre13i, pre14i, pre15d, pre16c, pre17c, pre18c, pre19c, pre20c, pre21c, pre22i ) select @tocompid, pre01c, pre02c, pre03i, pre04i, pre05i, pre06i, pre07f, pre08f, pre09i, pre10f, pre11f, pre12i, pre13i, pre14i, pre15d, pre16c, pre17c, pre18c, pre19c, pre20c, pre21c, pre22i from precept where pre00c = @fromcompid and pre01c = @pre01c -- hem01 delete hem01 where hea00c = @tocompid and hea33c = @pre01c insert hem01(hea00c, hea01c, hea02i, hea03f, hea04i, hea05f, hea06i, hea07f, hea08i, hea09f, hea10i, hea11f, hea12f, hea13f, hea14i, hea15f, hea16i, hea17f, hea18i, hea19i, hea20i, hea21f, hea22i, hea23f, hea24f, hea25i, hea26f, hea27i, hea28f, hea29i, hea30i, hea31i, hea32i, hea33c, hea34c, hea35i,hea36f,hea37i,hea38f,hea39i,hea40f,hea41i,hea42f,hea43i,hea44f, hea45i,hea46f,hea47i,hea48f,hea49i ) select @tocompid, hea01c, hea02i, hea03f, hea04i, hea05f, hea06i, hea07f, hea08i, hea09f, hea10i, hea11f, hea12f, hea13f, hea14i, hea15f, hea16i, hea17f, hea18i, hea19i, hea20i, hea21f, hea22i, hea23f, hea24f, hea25i, hea26f, hea27i, hea28f, hea29i, hea30i, hea31i, hea32i, hea33c, isnull(hea34c, '*'),hea35i,hea36f,hea37i,hea38f,hea39i,hea40f,hea41i,hea42f,hea43i,hea44f, hea45i,hea46f,hea47i,hea48f,hea49i from hem01 where hea00c = @fromcompid and hea33c = @pre01c -- productprecept delete productprecept where pro00c = @tocompid and pro02c = @pre01c insert productprecept(pro00c, pro01c, pro02c, pro03i, pro04f, pro05i, pro06f, pro07f, pro08c, pro09c, pro10c, pro11c, pro12c ) select @tocompid, pro01c, pro02c, pro03i, pro04f, pro05i, pro06f, pro07f, pro08c, pro09c, isnull(pro10c, '*'), pro11c, pro12c from productprecept where pro00c = @fromcompid and pro02c = @pre01c -- cardprecept delete cardprecept where car00c = @tocompid and car02c = @pre01c insert cardprecept(car00c, car01c, car02c, car03i, car04f, car05i, car06f, car07i, car08f, car09i, car10f, car11f, car12f, car13f, car14f, car15f, car16c, car17c, car18c, car19c, car20c ) select @tocompid, car01c, car02c, car03i, car04f, car05i, car06f, car07i, car08f, car09i, car10f, car11f, car12f, car13f, car14f, car15f, car16c, car17c, car18c, car19c, car20c from cardprecept where car00c = @fromcompid and car02c = @pre01c -- aloneproductprecept delete aloneproductprecept where gap00c = @tocompid and gap02c = @pre01c insert aloneproductprecept(gap00c, gap01c, gap02c, gap03i, gap04f, gap05c, gap06c, gap07c, gap08c, gap09c) select @tocompid, gap01c, gap02c, gap03i, gap04f, gap05c, gap06c, gap07c, gap08c, gap09c from aloneproductprecept where gap00c = @fromcompid and gap02c = @pre01c -- alonecardprecept delete alonecardprecept where gac00c = @tocompid and gac02c = @pre01c insert alonecardprecept(gac00c, gac01c, gac02c, gac03i, gac04f, gac05i, gac06f, gac07i, gac08f, gac09i, gac10f, gac11f, gac12f, gac13f, gac14f, gac15f, gac16c, gac17c, gac18c, gac19c, gac20c ) select @tocompid, gac01c, gac02c, gac03i, gac04f, gac05i, gac06f, gac07i, gac08f, gac09i, gac10f, gac11f, gac12f, gac13f, gac14f, gac15f, gac16c, gac17c, gac18c, gac19c, gac20c from alonecardprecept where gac00c = @fromcompid and gac02c = @pre01c -- hem03 delete hem03 where hec00c = @tocompid and hec01c = @pre01c insert hem03(hec00c, hec01c, hec02f, hec03f, hec04f, hec05i, hec06f, hec07c, hec08i, hec09f, hec10c ) select @tocompid, hec01c, hec02f, hec03f, hec04f, hec05i, hec06f, hec07c, hec08i, hec09f, hec10c from hem03 where hec00c = @fromcompid and hec01c = @pre01c -- hem04 delete hem04 where hed00c = @tocompid and hed01c = @pre01c insert hem04(hed00c, hed01c, hed02f, hed03f, hed04f, hed05i, hed06f, hed07c, hed08c ) select @tocompid, hed01c, hed02f, hed03f, hed04f, hed05i, hed06f, hed07c, hed08c from hem04 where hed00c = @fromcompid and hed01c = @pre01c -- hem05 delete hem05 where hee00c = @tocompid and hee01c = @pre01c insert hem05(hee00c, hee01c, hee02f, hee03f, hee04f, hee05i, hee06f, hee07c, hee08c ) select @tocompid, hee01c, hee02f, hee03f, hee04f, hee05i, hee06f, hee07c, hee08c from hem05 where hee00c = @fromcompid and hee01c = @pre01c -- hem06 delete hem06 where hef00c = @tocompid and hef01c = @pre01c insert hem06(hef00c, hef01c, hef02f, hef03f, hef04f, hef05i, hef06f, hef08c ) select @tocompid, hef01c, hef02f, hef03f, hef04f, hef05i, hef06f, hef08c from hem06 where hef00c = @fromcompid and hef01c = @pre01c -- ham14 delete ham14 where han00c = @tocompid and han01c = @pre01c insert ham14(han00c, han01c, han02f, han03f, han04f, han05i, han06f, han07c ) select @tocompid, han01c, han02f, han03f, han04f, han05i, han06f, han07c from ham14 where han00c = @fromcompid and han01c = @pre01c -- ham16 delete ham16 where haq00c = @tocompid and haq01c = @pre01c insert ham16(haq00c, haq01c, haq02f, haq03f, haq04f, haq05i, haq06f, haq08c ) select @tocompid, haq01c, haq02f, haq03f, haq04f, haq05i, haq06f, haq08c from ham16 where haq00c = @fromcompid and haq01c = @pre01c -- ham15 delete ham15 where hap00c = @tocompid and hap01c = @pre01c insert ham15(hap00c, hap01c, hap02c, hap03i, hap04f, hap05i, hap06f, hap07i, hap08f, hap09i, hap10f, hap11i, hap12f, hap13i, hap14f, hap15i, hap16f, hap17i, hap18f, hap19i, hap20f, hap21i, hap22f, hap23i, hap24f, hap25i, hap26f, hap37i, hap38f, hap39i, hap40f, hap41i, hap42f, hap43i, hap44f, hap45i, hap46f, hap47i, hap48f, hap49i, hap50f, hap51i, hap52f ) select @tocompid, hap01c, hap02c, hap03i, hap04f, hap05i, hap06f, hap07i, hap08f, hap09i, hap10f, hap11i, hap12f, hap13i, hap14f, hap15i, hap16f, hap17i, hap18f, hap19i, hap20f, hap21i, hap22f, hap23i, hap24f, hap25i, hap26f, hap37i, hap38f, hap39i, hap40f, hap41i, hap42f, hap43i, hap44f, hap45i, hap46f, hap47i, hap48f, hap49i, hap50f, hap51i, hap52f from ham15 where hap00c = @fromcompid and hap01c = @pre01c end GO /****** Object: StoredProcedure [dbo].[upg_set_flag_treatment_combo] Script Date: 06/12/2017 06:20:41 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_set_flag_treatment_combo] ( @compid varchar(10), @billno varchar(20), @carid varchar(20), @date varchar(10), -- 异动日期 @confirm int, @accout int ) as begin if(@accout <> 4 and @accout <> 889) return declare @amt_4 float declare @remainamt_4 float declare @billamt_4 float declare @amt_889 float declare @remainamt_889 float declare @billamt_889 float declare @sate int --是否还款到疗程或套餐 set @sate = 0 if(@confirm = 0) begin update gcm11 set gcm30c = '', gcm31d = '', gcm32f = gcm33f, gcm29c = 1, gcm33f = 0 where gcm00c = @compid and gcm30c = @billno update gtc06 set gth21c = '', gth22d = '', gth23f = gth25f, gth20c = 1, gth25f = 0 where gth00c = @compid and gth21c = @billno update gnm03 set gnc31c = '', gnc32d = '', gnc30c = 1, gnc33f = gnc35f, gnc35f = 0 where gnc00c = @compid and gnc31c = @billno update gtc05 set gtf20c = '', gtf21d = '' , gtf22f = gtf24f, gtf19c = 1, gtf24f = 0 where gtf00c = @compid and gtf20c = @billno update a set a.gcl112i = 0 from gcm10 a where gcl00c = @compid and gcl01c = @billno and gcl04c = @accout and gcl06i = 6 and gcl03c = @carid end else begin if( @accout = 4) --疗程账户异动金额 select @amt_4 = isnull(gcl07f,0) from gcm10 with (nolock) where gcl00c = @compid and gcl01c = @billno and gcl04c = @accout and gcl06i = 6 and gcl03c = @carid else --套餐账户异动金额 select @amt_889 = isnull(gcl07f,0) from gcm10 with (nolock) where gcl00c = @compid and gcl01c = @billno and gcl04c = @accout and gcl06i = 6 and gcl03c = @carid if(@amt_4 > 0) begin select @remainamt_4 = @amt_4 declare @gcm00c varchar(20) declare @gcm14i int declare fcur_get_each_def_flag_gcm11 cursor for select gcm32f,gcm00c,gcm14i from gcm11 b where b.gcm29c = 1 and b.gcm33c = @carid and isnull(b.gcm32f, 0)> 0 for update of gcm30c,gcm31d,gcm32f,gcm33f,gcm29c open fcur_get_each_def_flag_gcm11 fetch fcur_get_each_def_flag_gcm11 into @billamt_4,@gcm00c,@gcm14i while @@fetch_status = 0 and @remainamt_4 > 0 begin if @billamt_4 <= @remainamt_4 begin update gcm11 set gcm30c = @billno,gcm31d = @date, gcm32f = 0, gcm33f = @billamt_4,gcm29c = 0 where gcm00c= @gcm00c and gcm14i = @gcm14i select @remainamt_4 = @remainamt_4 - @billamt_4 end else --if @remainamt_4 > @billamt_4/2 begin update gcm11 set gcm30c = @billno,gcm31d = @date,gcm32f = gcm32f - @remainamt_4, gcm33f = @remainamt_4,gcm29c = 0 where gcm00c= @gcm00c and gcm14i = @gcm14i select @remainamt_4 = 0 end set @sate = 1 -- else -- begin -- update gcm11 set gcm32f = gcm32f - @remainamt_4, gcm33f = @remainamt_4 -- where gcm00c= @gcm00c and gcm14i = @gcm14i -- select @remainamt_4 = 0 -- -- end fetch fcur_get_each_def_flag_gcm11 into @billamt_4,@gcm00c,@gcm14i end close fcur_get_each_def_flag_gcm11 deallocate fcur_get_each_def_flag_gcm11 select @remainamt_4 = @amt_4 ----------------- declare @gnc00c varchar(10) declare @gnc29i int declare fcur_get_each_def_flag_3 cursor for select gnc33f,gnc00c,gnc29i from gnm03 b where b.gnc30c = 1 and b.gnc34c = @carid and isnull(b.gnc33f, 0)> 0 for update of gnc31c,gnc32d,gnc33f,gnc35f,gnc30c open fcur_get_each_def_flag_3 fetch fcur_get_each_def_flag_3 into @billamt_4,@gnc00c,@gnc29i while @@fetch_status = 0 and @remainamt_4 > 0 begin if @billamt_4 <= @remainamt_4 begin update gnm03 set gnc31c = @billno, gnc32d = @date, gnc33f = 0, gnc35f = @billamt_4,gnc30c = 0 where gnc00c = @gnc00c and gnc29i = @gnc29i select @remainamt_4 = @remainamt_4 - @billamt_4 end else --if @remainamt_4 > @billamt_4/2 begin update gnm03 set gnc31c = @billno, gnc32d = @date, gnc33f = gnc33f - @remainamt_4, gnc35f = @remainamt_4,gnc30c = 0 where gnc00c = @gnc00c and gnc29i = @gnc29i select @remainamt_4 = 0 end set @sate = 1 -- else -- begin -- update gnm03 set gnc33f = gnc33f - @remainamt_4, gnc35f = @remainamt_4 -- where gnc00c = @gnc00c and gnc29i = @gnc29i -- select @remainamt_4 = 0 -- end fetch fcur_get_each_def_flag_3 into @billamt_4,@gnc00c,@gnc29i end close fcur_get_each_def_flag_3 deallocate fcur_get_each_def_flag_3 end if (@amt_889>0) begin select @remainamt_889 = @amt_889 declare @gth00c varchar(10) declare @gth01c varchar(20) declare @gth02c varchar(20) declare fcur_get_each_def_flag_2 cursor for select gth23f,gth00c,gth01c,gth02c from gtc06 b where b.gth20c = 1 and b.gth24c = @carid and isnull(b.gth23f, 0)> 0 for update of gth21c,gth22d,gth23f,gth25f,gth20c open fcur_get_each_def_flag_2 fetch fcur_get_each_def_flag_2 into @billamt_889,@gth00c,@gth01c,@gth02c while @@fetch_status = 0 and @remainamt_889>0 begin if @billamt_889 <= @remainamt_889 begin update gtc06 set gth21c =@billno, gth22d = @date, gth23f = 0, gth25f = @billamt_889,gth20c = 0 where gth00c = @gth00c and gth01c = @gth01c and gth02c = @gth02c select @remainamt_889 = @remainamt_889 - @billamt_889 end else --if @remainamt_889 >@billamt_889/2 begin update gtc06 set gth21c =@billno, gth22d = @date, gth23f = gth23f - @remainamt_889, gth25f = @remainamt_889,gth20c = 0 where gth00c = @gth00c and gth01c = @gth01c and gth02c = @gth02c select @remainamt_889 = 0 end set @sate = 1 -- else -- begin -- update gtc06 set gth23f = gth23f - @remainamt_889, gth25f = @remainamt_889 -- where gth00c = @gth00c and gth01c = @gth01c and gth02c = @gth02c -- select @remainamt_889 = 0 -- end fetch fcur_get_each_def_flag_2 into @billamt_889,@gth00c,@gth01c,@gth02c end close fcur_get_each_def_flag_2 deallocate fcur_get_each_def_flag_2 select @remainamt_889 = @amt_889 -------------- declare @gtf00c varchar(10) declare @gtf01c varchar(20) declare @gtf02c varchar(20) declare fcur_get_each_def_flag_4 cursor for select gtf22f,gtf00c,gtf01c,gtf02c from gtc05 b where b.gtf19c = 1 and b.gtf23c = @carid and isnull(b.gtf22f, 0)> 0 for update of gtf20c,gtf21d,gtf22f,gtf24f,gtf19c open fcur_get_each_def_flag_4 fetch fcur_get_each_def_flag_4 into @billamt_889,@gtf00c,@gtf01c,@gtf02c while @@fetch_status = 0 and @remainamt_889>0 begin if @billamt_889 <= @remainamt_889 begin update gtc05 set gtf20c = @billno, gtf21d= @date, gtf22f = 0, gtf24f = @billamt_889,gtf19c = 0 where gtf00c = @gtf00c and gtf01c = @gtf01c and gtf02c = @gtf02c select @remainamt_889 = @remainamt_889 - @billamt_889 end else --if @remainamt_889>@billamt_889/2 begin update gtc05 set gtf20c = @billno, gtf21d= @date, gtf22f = gtf22f - @remainamt_889, gtf24f = @remainamt_889,gtf19c = 0 where gtf00c = @gtf00c and gtf01c = @gtf01c and gtf02c = @gtf02c select @remainamt_889 = 0 end set @sate = 1 -- else -- begin -- update gtc05 set gtf22f = gtf22f - @remainamt_889, gtf24f = @remainamt_889 -- where gtf00c = @gtf00c and gtf01c = @gtf01c and gtf02c = @gtf02c -- select @remainamt_889 = 0 -- end fetch fcur_get_each_def_flag_4 into @billamt_889,@gtf00c,@gtf01c,@gtf02c end close fcur_get_each_def_flag_4 deallocate fcur_get_each_def_flag_4 end end if(@sate = 1) begin --设置标志 update a set a.gcl112i = 1 from gcm10 a where gcl00c = @compid and gcl01c = @billno and gcl04c = @accout and gcl06i = 6 and gcl03c = @carid end end GO /****** Object: StoredProcedure [dbo].[upg_update_procid_rawbill] Script Date: 06/12/2017 06:20:50 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_update_procid_rawbill] ( @compid varchar(10), @billtype varchar(20), @billseq int, -- 售卡/充值-疗程序号(gnm03.gnc29i/gcm11.gcm14i) @procseq int -- 疗程序号 ) as begin if @billtype = 'gn' begin update gnm03 set gnc25i = @procseq where gnc00c = @compid and gnc29i = @billseq end else if @billtype = 'gz' begin update gcm11 set gcm25i = @procseq where gcm00c = @compid and gcm14i = @billseq end end GO /****** Object: StoredProcedure [dbo].[upg_get_procid_rawbill] Script Date: 06/12/2017 06:20:08 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_get_procid_rawbill] ( @compid varchar(10), @billtype varchar(20), @billseq int, -- 售卡/充值-疗程序号(gnm03.gnc29i/gcm11.gcm14i) @procseq int output -- 疗程序号 ) as begin if @billtype = 'gn' begin select @procseq = gnc25i from gnm03 with (nolock) where gnc00c = @compid and gnc29i = @billseq end else if @billtype = 'gz' begin select @procseq = gcm25i from gcm11 with (nolock) where gcm00c = @compid and gcm14i = @billseq end end GO /****** Object: StoredProcedure [dbo].[upg_set_deficit_flag_treatment_combo] Script Date: 06/12/2017 06:20:41 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_set_deficit_flag_treatment_combo] ( @compid varchar(10), @billno varchar(20), @carid varchar(20), @billtype varchar(5), -- 单据类别 'gn' = 会员卡销售 'gz' = 会员帐户管理 @confirm int ) as begin if(@billtype = 'gz') begin declare @pay_5 float declare @amt_4 float declare @amt_2 float declare @remainamt_4 float declare @billamt_4 float declare @amt_889 float declare @remainamt_889 float declare @billamt_889 float if(@confirm = 0) begin update gcm11 set gcm29c = 0, gcm32f = 0, gcm33c = '' where gcm00c = @compid and gcm01c = @billno update gtc06 set gth20c = 0, gth23f = 0, gth24c = '' where gth00c = @compid and gth01c = @billno -- return end else begin --签单挂账金额 select @pay_5 = gsc05f from gsm03 with(nolock) where gsc00c = @compid and gsc01c = @billno and gsc02c = 'gz' and gsc04c = '5' --疗程账户异动金额 --select @amt_4 = gcd06f from gcm04 where gcd00c = @compid and gcd08c = @billno and gcd07c = 'gz' and gcd05i = 0 and gcd02i = 4 and gcd01c = @carid select @amt_4 = isnull(gcl66f,0) from gcm10 where gcl00c = @compid and gcl01c = @billno and gcl06i = 0 and gcl03c = @carid --套餐异动金额 --select @amt_2 = gcd06f from gcm04 where gcd00c = @compid and gcd08c = @billno and gcd07c = 'gz' and gcd05i = 0 and gcd02i = 2 and gcd01c = @carid select @amt_2 = isnull(gcl51f,0) from gcm10 where gcl00c = @compid and gcl01c = @billno and gcl06i = 0 and gcl03c = @carid --select @pay_5,@amt_4,@amt_2 --账户欠款:有疗程账户先欠疗程账户,然后套餐账户,最后储值账户。 -- --select @pay_5, @amt_4, @amt_2 if isnull(@pay_5,0) <= isnull(@amt_4,0) begin --疗程欠款 set @amt_4 = isnull(@pay_5,0) end else if(isnull(@amt_2,0) > 0) begin --套餐欠款 set @amt_889 = isnull(@pay_5,0) - isnull(@amt_4,0) --- isnull(@amt_2,0) end if(isnull(@amt_4,0) > 0) begin select @remainamt_4 = @amt_4 declare @gcm00c varchar(20) declare @gcm14i int declare cur_get_each_def_treatment cursor for select gcm05f,gcm00c,gcm14i from gcm11 b where b.gcm00c = @compid and b.gcm01c = @billno for update of gcm29c,gcm32f,gcm33c open cur_get_each_def_treatment fetch cur_get_each_def_treatment into @billamt_4,@gcm00c,@gcm14i while @@fetch_status = 0 and @remainamt_4 > 0 begin if @billamt_4 <= @remainamt_4 begin update gcm11 set gcm29c = 1,gcm32f = @billamt_4, gcm33c = @carid where gcm00c= @gcm00c and gcm14i = @gcm14i select @remainamt_4 = @remainamt_4 - @billamt_4 end else --if @remainamt_4 > @remainamt_4/2 begin update gcm11 set gcm29c = 1 ,gcm32f = @remainamt_4, gcm33c = @carid where gcm00c= @gcm00c and gcm14i = @gcm14i select @remainamt_4 = 0 end fetch cur_get_each_def_treatment into @billamt_4,@gcm00c,@gcm14i end close cur_get_each_def_treatment deallocate cur_get_each_def_treatment end if (isnull(@amt_889,0)>0) begin select @remainamt_889 = @amt_889 declare @gth00c varchar(10) declare @gth01c varchar(20) declare @gth02c varchar(20) declare cur_get_each_def_combo cursor for select gth05f,gth00c,gth01c,gth02c from gtc06 b where b.gth00c = @compid and b.gth01c = @billno for update of gth20c,gth23f,gth24c open cur_get_each_def_combo fetch cur_get_each_def_combo into @billamt_889,@gth00c,@gth01c,@gth02c while @@fetch_status = 0 and @remainamt_889>0 begin if @billamt_889 <= @remainamt_889 begin update gtc06 set gth20c = 1, gth23f = @billamt_889, gth24c = @carid where gth00c = @gth00c and gth01c = @gth01c and gth02c = @gth02c select @remainamt_889 = @remainamt_889 - @billamt_889 end else --if @remainamt>@billamt/2 begin update gtc06 set gth20c = 1, gth23f = @remainamt_889, gth24c = @carid where gth00c = @gth00c and gth01c = @gth01c and gth02c = @gth02c select @remainamt_889 = 0 end fetch cur_get_each_def_combo into @billamt_889,@gth00c,@gth01c,@gth02c end close cur_get_each_def_combo deallocate cur_get_each_def_combo end end end else begin set @amt_4 = 0 set @remainamt_4 = 0 set @billamt_4 = 0 set @amt_889 = 0 set @remainamt_889 = 0 set @billamt_889 = 0 if(@confirm = 0) begin update gnm03 set gnc30c = 0, gnc33f = 0, gnc34c = '' where gnc00c = @compid and gnc01c = @billno update gtc05 set gtf19c = 0, gtf22f = 0, gtf23c = '' where gtf00c = @compid and gtf01c = @billno -- return end else begin select @amt_4 = isnull(gcc10f,0) from gcm03 with(nolock) where gcc00c = @compid and gcc03i = 4 and gcc01c = @carid select @amt_889 = isnull(gcc10f,0) from gcm03 with(nolock) where gcc00c = @compid and gcc03i = 889 and gcc01c = @carid if(@amt_4 > 0) begin declare @gnc00c varchar(10) declare @gnc29i int select @remainamt_4 = @amt_4 declare cur_get_each_def_treatment cursor for select gnc05f,gnc00c,gnc29i from gnm03 b where b.gnc00c = @compid and b.gnc01c = @billno for update of gnc30c,gnc33f,gnc34c open cur_get_each_def_treatment fetch cur_get_each_def_treatment into @billamt_4,@gnc00c,@gnc29i while @@fetch_status = 0 and @remainamt_4 > 0 begin if @billamt_4 <= @remainamt_4 begin update gnm03 set gnc30c = 1,gnc33f = @billamt_4, gnc34c = @carid where gnc00c = @gnc00c and gnc29i = @gnc29i select @remainamt_4 = @remainamt_4 - @billamt_4 end else --if @remainamt_4 > @remainamt_4/2 begin update gnm03 set gnc30c = 1 ,gnc33f = @remainamt_4, gnc34c = @carid where gnc00c = @gnc00c and gnc29i = @gnc29i select @remainamt_4 = 0 end fetch cur_get_each_def_treatment into @billamt_4,@gnc00c,@gnc29i end close cur_get_each_def_treatment deallocate cur_get_each_def_treatment end if (@amt_889>0) begin select @remainamt_889 = @amt_889 declare @gtf00c varchar(10) declare @gtf01c varchar(20) declare @gtf02c varchar(20) declare cur_get_each_def_combo cursor for select gtf05f,gtf00c,gtf01c,gtf02c from gtc05 b where b.gtf00c = @compid and b.gtf01c = @billno for update of gtf19c,gtf22f,gtf23c open cur_get_each_def_combo fetch cur_get_each_def_combo into @billamt_889,@gtf00c,@gtf01c,@gtf02c while @@fetch_status = 0 and @remainamt_889>0 begin if @billamt_889 <= @remainamt_889 begin update gtc05 set gtf19c = 1, gtf22f = @billamt_889, gtf23c = @carid where gtf00c = @gtf00c and gtf01c = @gtf01c and gtf02c = @gtf02c select @remainamt_889 = @remainamt_889 - @billamt_889 end else --if @remainamt>@billamt/2 begin update gtc05 set gtf19c = 1, gtf22f = @remainamt_889, gtf23c = @carid where gtf00c = @gtf00c and gtf01c = @gtf01c and gtf02c = @gtf02c select @remainamt_889 = 0 end fetch cur_get_each_def_combo into @billamt_889,@gtf00c,@gtf01c,@gtf02c end close cur_get_each_def_combo deallocate cur_get_each_def_combo end end end end GO /****** Object: StoredProcedure [dbo].[copy_gicm10_with_datetime] Script Date: 06/12/2017 06:19:19 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[copy_gicm10_with_datetime]( @gicj00c varchar(20), @gicj05d varchar(8), @strBans varchar(200), @begtime varchar(8), @endtime varchar(8)) as begin declare @nowdate varchar(8) set @nowdate = @begtime while @nowdate >= @begtime and @nowdate <= @endtime begin if (@nowdate<>@gicj05d) begin delete from gicm10 where gicj00c = @gicj00c and gicj05d = @nowdate and gicj02v = @strBans insert gicm10(gicj00c, gicj01v, gicj02v, gicj03t, gicj04t, gicj05d, gicj06d, gicj07v, gicj08t, gicj09d, gicj12t, gicj13c) select gicj00c, gicj01v, gicj02v, gicj03t, gicj04t, @nowdate, @nowdate, gicj07v, gicj08t, gicj09d, gicj12t, gicj13c from gicm10 where gicj00c = @gicj00c and gicj05d = @gicj05d and gicj02v = @strBans end execute upg_date_plus @nowdate, 1, @nowdate output end end GO /****** Object: StoredProcedure [dbo].[upg_del_recover_ggm01] Script Date: 06/12/2017 06:19:57 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_del_recover_ggm01] ( @compid varchar(10), @id int ) as begin delete from recover_ggm01_master where comp = @compid and id = @id delete from recover_ggm01 where gga00c = @compid and id = @id delete from recover_ggm02 where ggb00c = @compid and id = @id delete from recover_ggm03 where ggc00c = @compid and id = @id delete from recover_ggm05 where gge00c = @compid and id = @id end GO /****** Object: StoredProcedure [dbo].[upg_cal_thismonth_usedtimes] Script Date: 06/12/2017 06:19:34 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_cal_thismonth_usedtimes] ( @compid varchar(10), -- 公司别 @cardid varchar(20), -- 会员卡号 @cardtype varchar(20), -- 会员卡类别 @procid varchar(20), -- 疗程编码 @procidd int, -- 疗程序号 @mmonth varchar(10), -- 月份 @isgift int, -- 是否为永久免费 @nums float output ) as begin declare @mainproc varchar(20) select @nums = 0 create table #tbl_exchange_proc ( procid varchar(20) not null ) insert #tbl_exchange_proc(procid) values(@procid) if (@isgift = 1) begin -- 永久免费卡 select @mainproc = gaj04c from gam09 where gaj00c = @compid and gaj01c = @cardtype and gaj02c = @procid --表面项目 insert #tbl_exchange_proc(procid) select gaj02c from gam09 where gaj00c = @compid and gaj01c = @cardtype and gaj04c = @mainproc --表面项目 end else begin insert #tbl_exchange_proc(procid) values(@procid) end -- 不需要区分公司 select @nums = sum(isnull(gch08f, 0)) from gcm08 a with (nolock), (select distinct procid from #tbl_exchange_proc) b where a.gch10c = 'gx' and a.gch03c = @cardid and substring(a.gch02d, 1, 6) = @mmonth and a.gch04c = b.procid and gch16i = @procidd drop table #tbl_exchange_proc end GO /****** Object: StoredProcedure [dbo].[upg_send_new_gift] Script Date: 06/12/2017 06:20:40 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_send_new_gift] ( @compid varchar(10), -- 公司别 @cardid varchar(20), -- 会员卡号 @cardtype varchar(20) -- 会员卡类别 ) as begin --根据卡号获取归属公司: declare @ownercompid nvarchar(10) --卡的归属公司 select @ownercompid = min(gca13d) from gcm01 where gca01c = @cardid and gca00c = gca13d delete a from gcm06 a, gam09 b where a.gcf00c = b.gaj00c and a.gcf01c = @cardid and a.gcf00c = @ownercompid and a.gcf02c = b.gaj02c and b.gaj01c = @cardtype and a.gcf22i = 1 insert gcm06(gcf00c, gcf01c, gcf02c, gcf07f, gcf10f, gcf14f, gcf17i, gcf18f, gcf19i, gcf20i, gcf22i) select @ownercompid, @cardid, gaj02c, 0, 0, gaj03i, 0, 1, 1, 0, 1 from gam09 where gaj00c = @compid and gaj01c = @cardtype end GO /****** Object: StoredProcedure [dbo].[upg_delete_old_gift] Script Date: 06/12/2017 06:19:59 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_delete_old_gift]( @compid varchar(10), --公司别 @cardid varchar(20), -- 会员卡号 @cardtype varchar(20)) --会员卡类别 as begin --根据卡号获取归属公司: declare @ownercompid nvarchar(10) --卡的归属公司 select @ownercompid = min(gca13d) from gcm01 where gca01c = @cardid and gca00c = gca13d delete a from gcm06 a, gam09 b where a.gcf00c = b.gaj00c and a.gcf01c = @cardid and a.gcf00c = @ownercompid and a.gcf02c = b.gaj02c and b.gaj01c = @cardtype and a.gcf22i = 1 end GO /****** Object: StoredProcedure [dbo].[upg_statistic_proj_add_comm] Script Date: 06/12/2017 06:20:44 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_statistic_proj_add_comm] ( @compid varchar(10), -- 公司别 @proj varchar(20), -- 卡代码,产品代码,项目代码 @class varchar(20), -- 卡类别 @standardprice float, -- 标准单价 @total float, -- 总金额, 实际金额 @vperf float, -- 虚业绩,应该已经计算好 @cardrate float, -- 转卡比率 @cost float, -- 成本 @times float, -- 服务次数,产品数, 卡数量 @staff varchar(200), -- 人员 @flag int, -- 1-卡销售, 2-指定,3-轮班, 4-补工, 56-中工, 78-小工, 10, 11-四工 9-产品销售 0-卡充值 @comm float output, -- 佣金 @trueperf float output, -- 真实业绩 @ex_rate float, -- 实际营业额折算比率 @last_m_comm float, -- 中工的提成 @last_l_comm float, -- 小工的提成 @share_rate float, -- 分享比率 @orderdate varchar(8), -- 账单日期 @paycode varchar(20) -- ) as--wuxm, 2004/06/12 begin --注意: 卡的成本总是为0 --服务项目, 产品的成本已经计算好 declare @type int declare @setvalue float declare @fa_type varchar(20) --提成方案编号 select top 1 @fa_type = ppt02c from personprecept where ppt00c = @compid and ppt03c = @staff and ppt04i = 1 and ppt02c in (select pre01c from precept where pre00c = @compid and isnull(pre22i, 0) = 0) order by ppt05d desc --获取在账单有效日期内最新一条员工提成方案 if @cardrate > 1 select @cardrate = 1 select @comm = 0 --单独项目 if ((@flag >= 2 and @flag <= 8) or (@flag >= 10 and @flag <= 11) or (@flag >= 15 and @flag <=16) or (@flag >= 18 and @flag <=21) or (@flag >= 23 and @flag <= 24)) begin if (@flag = 2) -- 大工指定 begin select @type = hap03i, @setvalue = hap04f from ham15 where hap00c = @compid and hap02c = @proj and hap01c = @fa_type and hap53c = @paycode if(isnull(@type,'') = '') select @type = hap03i, @setvalue = hap04f from ham15 where hap00c = @compid and hap02c = @proj and hap01c = @fa_type and hap53c = '*' end else if (@flag = 3) -- 轮班大工 begin select @type = hap11i, @setvalue = hap12f from ham15 where hap00c = @compid and hap02c = @proj and hap01c = @fa_type and hap53c = @paycode if(isnull(@type,'') = '') select @type = hap11i, @setvalue = hap12f from ham15 where hap00c = @compid and hap02c = @proj and hap01c = @fa_type and hap53c = '*' end else if (@flag = 4) -- 补工大工 begin select @type = hap19i, @setvalue = hap20f from ham15 where hap00c = @compid and hap02c = @proj and hap01c = @fa_type and hap53c = @paycode if(isnull(@type,'') = '') select @type = hap19i, @setvalue = hap20f from ham15 where hap00c = @compid and hap02c = @proj and hap01c = @fa_type and hap53c = '*' end else if (@flag = 5) -- 指定中工 begin select @type = hap05i, @setvalue = hap06f from ham15 where hap00c = @compid and hap02c = @proj and hap01c = @fa_type and hap53c = @paycode if(isnull(@type,'') = '') select @type = hap05i, @setvalue = hap06f from ham15 where hap00c = @compid and hap02c = @proj and hap01c = @fa_type and hap53c = '*' end else if (@flag = 6) -- 轮班中工 begin select @type = hap13i, @setvalue = hap14f from ham15 where hap00c = @compid and hap02c = @proj and hap01c = @fa_type and hap53c = @paycode if(isnull(@type,'') = '') select @type = hap13i, @setvalue = hap14f from ham15 where hap00c = @compid and hap02c = @proj and hap01c = @fa_type and hap53c = '*' end else if (@flag = 7) -- 指定小工 begin select @type = hap07i, @setvalue = hap08f from ham15 where hap00c = @compid and hap02c = @proj and hap01c = @fa_type and hap53c = @paycode if(isnull(@type,'') = '') select @type = hap07i, @setvalue = hap08f from ham15 where hap00c = @compid and hap02c = @proj and hap01c = @fa_type and hap53c = '*' end else if (@flag = 8) -- 轮班小工 begin select @type = hap15i, @setvalue = hap16f from ham15 where hap00c = @compid and hap02c = @proj and hap01c = @fa_type and hap53c = @paycode if(isnull(@type,'') = '') select @type = hap15i, @setvalue = hap16f from ham15 where hap00c = @compid and hap02c = @proj and hap01c = @fa_type and hap53c = '*' end else if (@flag = 10) -- 指定四工 begin select @type = hap09i, @setvalue = hap10f from ham15 where hap00c = @compid and hap02c = @proj and hap01c = @fa_type and hap53c = @paycode if(isnull(@type,'') = '') select @type = hap09i, @setvalue = hap10f from ham15 where hap00c = @compid and hap02c = @proj and hap01c = @fa_type and hap53c = '*' end else if (@flag = 11) -- 轮班四工 begin select @type = hap17i, @setvalue = hap18f from ham15 where hap00c = @compid and hap02c = @proj and hap01c = @fa_type and hap53c = @paycode if(isnull(@type,'') = '') select @type = hap17i, @setvalue = hap18f from ham15 where hap00c = @compid and hap02c = @proj and hap01c = @fa_type and hap53c = '*' end else if (@flag = 15) -- 大工内创 begin select @type = hap37i, @setvalue = hap38f from ham15 where hap00c = @compid and hap02c = @proj and hap01c = @fa_type and hap53c = @paycode if(isnull(@type,'') = '') select @type = hap37i, @setvalue = hap38f from ham15 where hap00c = @compid and hap02c = @proj and hap01c = @fa_type and hap53c = '*' end else if (@flag = 16) -- 大工外创 begin select @type = hap45i, @setvalue = hap46f from ham15 where hap00c = @compid and hap02c = @proj and hap01c = @fa_type and hap53c = @paycode if(isnull(@type,'') = '') select @type = hap45i, @setvalue = hap46f from ham15 where hap00c = @compid and hap02c = @proj and hap01c = @fa_type and hap53c = '*' end else if (@flag = 18) --中工内创 begin select @type = hap39i, @setvalue = hap40f from ham15 where hap00c = @compid and hap02c = @proj and hap01c = @fa_type and hap53c = @paycode if(isnull(@type,'') = '') select @type = hap39i, @setvalue = hap40f from ham15 where hap00c = @compid and hap02c = @proj and hap01c = @fa_type and hap53c = '*' end else if (@flag = 19) --中工外创 begin select @type = hap47i, @setvalue = hap48f from ham15 where hap00c = @compid and hap02c = @proj and hap01c = @fa_type and hap53c = @paycode if(isnull(@type,'') = '') select @type = hap47i, @setvalue = hap48f from ham15 where hap00c = @compid and hap02c = @proj and hap01c = @fa_type and hap53c = '*' end else if (@flag = 20) --小工内创 begin select @type = hap43i, @setvalue = hap44f from ham15 where hap00c = @compid and hap02c = @proj and hap01c = @fa_type and hap53c = @paycode if(isnull(@type,'') = '') select @type = hap43i, @setvalue = hap44f from ham15 where hap00c = @compid and hap02c = @proj and hap01c = @fa_type and hap53c = '*' end else if (@flag = 21) --小工外创 begin select @type = hap49i, @setvalue = hap50f from ham15 where hap00c = @compid and hap02c = @proj and hap01c = @fa_type and hap53c = @paycode if(isnull(@type,'') = '') select @type = hap49i, @setvalue = hap50f from ham15 where hap00c = @compid and hap02c = @proj and hap01c = @fa_type and hap53c = '*' end else if (@flag = 23) --四工内创 begin select @type = hap41i, @setvalue = hap42f from ham15 where hap00c = @compid and hap02c = @proj and hap01c = @fa_type and hap53c = @paycode if(isnull(@type,'') = '') select @type = hap41i, @setvalue = hap42f from ham15 where hap00c = @compid and hap02c = @proj and hap01c = @fa_type and hap53c = '*' end else if (@flag = 24) --四工外创 begin select @type = hap51i, @setvalue = hap52f from ham15 where hap00c = @compid and hap02c = @proj and hap01c = @fa_type and hap53c = @paycode if(isnull(@type,'') = '') select @type = hap51i, @setvalue = hap52f from ham15 where hap00c = @compid and hap02c = @proj and hap01c = @fa_type and hap53c = '*' end end else if (@flag = 1 or @flag = 0)--单独卡提成 begin if @flag = 1--卡销售 begin select @type = gac05i, @setvalue = gac06f from alonecardprecept where gac00c = @compid and gac01c = @class and gac02c = @fa_type end if @flag = 0--卡提成 begin select @type = gac09i, @setvalue = gac10f from alonecardprecept where gac00c = @compid and gac01c = @class and gac02c = @fa_type end end else if (@flag = 9)--单独产品提成 begin select @type = gap03i, @setvalue = gap04f from aloneproductprecept where gap00c = @compid and gap01c = @proj and gap02c = @fa_type end select @setvalue = isnull(@setvalue, 0) --真实业绩 = (虚业绩-成本) * 转卡率 if @flag = 9 or @flag = 1 or @flag = 0 select @trueperf = (@vperf - isnull(@cost, 0)*isnull(@share_rate, 1))* @cardrate else select @trueperf = @trueperf * @cardrate if isnull(@type, 0) = 0 return if isnull(@ex_rate, -1) = -1 begin select @ex_rate = 1 end -- 计算提成 if (@type = 1) -- 固定金额 select @comm = @setvalue * @times * @share_rate else if (@type = 2) -- 营业额比率 select @comm = @total * @setvalue * @cardrate * @share_rate else if (@type = 3) -- 实业绩比率 select @comm = @trueperf * @setvalue else if (@type = 4) -- 虚业绩比率 select @comm = @vperf * @setvalue else if (@type = 5) -- 标准价比率 select @comm = @standardprice * @times * @setvalue * @cardrate * @share_rate else if (@type = 6) -- 折算虚业绩比率, 考虑实际营业额的比率 select @comm = @vperf * @setvalue * @ex_rate else if (@type = 61) -- 实际营业额, 考虑实际营业额的比率 select @comm = @total * @setvalue * @ex_rate else if (@type = 7) -- 营业额比率(扣小工提成) begin if @setvalue>2 select @comm = @setvalue * @times * @share_rate - @last_l_comm else select @comm = @total * @setvalue - @last_l_comm end else if (@type = 8) -- 营业额比率(扣中工小工提成) begin if @setvalue>2 select @comm = @setvalue * @times * @share_rate - (@last_l_comm + @last_m_comm) else select @comm = @total * @setvalue - (@last_l_comm + @last_m_comm) end else if (@type = 9) -- 营业额比率(扣中工提成) begin if @setvalue>2 select @comm = @setvalue * @times * @share_rate - @last_m_comm else select @comm = @total * @setvalue - @last_m_comm end else if (@type = 90) -- 虚业绩比率(扣小工提成) select @comm = @vperf * @setvalue - @last_l_comm else if (@type = 91) -- 虚业绩比率(扣中工小工提成) select @comm = @vperf * @setvalue - (@last_l_comm + @last_m_comm) else if (@type = 92) -- 虚业绩比率(扣中工提成) select @comm = @vperf * @setvalue - @last_m_comm else if (@type = 93) -- 实业绩比率(扣小工提成) select @comm = @trueperf * @setvalue - @last_l_comm else if (@type = 94) -- 实业绩比率(扣中工小工提成) select @comm = @trueperf * @setvalue - (@last_l_comm + @last_m_comm) else if (@type = 95) -- 实业绩比率(扣中工提成) select @comm = @trueperf * @setvalue - @last_m_comm else if (@type = 10) -- 营业额再折扣 select @comm = @total * @setvalue * @cardrate * @share_rate * (@total/(@standardprice * @times )) else if (@type = 14) -- 固定金额口中工成本 select @comm = @setvalue * @times * @share_rate - @last_m_comm end GO /****** Object: StoredProcedure [dbo].[upg_m_dispatch_hem01_single_optional] Script Date: 06/12/2017 06:20:21 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_m_dispatch_hem01_single_optional] ( @fromcompid varchar(10), -- 从哪一个公司发布 @tocompid varchar(10), -- 目的公司 @pre01c varchar(20) -- 是否强制统一 ) as begin if @fromcompid = @tocompid return if not exists (select 1 from precept where pre00c = @tocompid and pre01c = @pre01c) return -- hem01 insert hem01(hea00c, hea01c, hea02i, hea03f, hea04i, hea05f, hea06i, hea07f, hea08i, hea09f, hea10i, hea11f, hea12f, hea13f, hea14i, hea15f, hea16i, hea17f, hea18i, hea19i, hea20i, hea21f, hea22i, hea23f, hea24f, hea25i, hea26f, hea27i, hea28f, hea29i, hea30i, hea31i, hea32i, hea33c, hea34c, hea35i,hea36f,hea37i,hea38f,hea39i,hea40f,hea41i,hea42f,hea43i,hea44f, hea45i,hea46f,hea47i,hea48f,hea49i ) select @tocompid, hea01c, hea02i, hea03f, hea04i, hea05f, hea06i, hea07f, hea08i, hea09f, hea10i, hea11f, hea12f, hea13f, hea14i, hea15f, hea16i, hea17f, hea18i, hea19i, hea20i, hea21f, hea22i, hea23f, hea24f, hea25i, hea26f, hea27i, hea28f, hea29i, hea30i, hea31i, hea32i, hea33c, isnull(hea34c, '*'),hea35i,hea36f,hea37i,hea38f,hea39i,hea40f,hea41i,hea42f,hea43i,hea44f, hea45i,hea46f,hea47i,hea48f,hea49i from hem01 where hea00c = @fromcompid and hea33c = @pre01c and (CONVERT(varchar(20), hea18i) + '$' + hea33c + '$' + isnull(hea34c, '*')) not in (select CONVERT(varchar(20), hea18i) + '$' + hea33c + '$' + isnull(hea34c, '*') from hem01 where hea00c = @tocompid and hea33c = @pre01c) -- productprecept insert productprecept(pro00c, pro01c, pro02c, pro03i, pro04f, pro05i, pro06f, pro07f, pro08c, pro09c, pro10c, pro11c, pro12c ) select @tocompid, pro01c, pro02c, pro03i, pro04f, pro05i, pro06f, pro07f, pro08c, pro09c, isnull(pro10c, '*'), pro11c, pro12c from productprecept where pro00c = @fromcompid and pro02c = @pre01c and pro01c not in(select pro01c from productprecept where pro00c = @tocompid and pro02c = @pre01c) -- cardprecept insert cardprecept(car00c, car01c, car02c, car03i, car04f, car05i, car06f, car07i, car08f, car09i, car10f, car11f, car12f, car13f, car14f, car15f, car16c, car17c, car18c, car19c, car20c ) select @tocompid, car01c, car02c, car03i, car04f, car05i, car06f, car07i, car08f, car09i, car10f, car11f, car12f, car13f, car14f, car15f, car16c, car17c, car18c, car19c, car20c from cardprecept where car00c = @fromcompid and car02c = @pre01c and car01c not in(select car01c from cardprecept where car00c = @tocompid and car02c = @pre01c) -- aloneproductprecept insert aloneproductprecept(gap00c, gap01c, gap02c, gap03i, gap04f, gap05c, gap06c, gap07c, gap08c, gap09c) select @tocompid, gap01c, gap02c, gap03i, gap04f, gap05c, gap06c, gap07c, gap08c, gap09c from aloneproductprecept where gap00c = @fromcompid and gap02c = @pre01c and gap01c not in(select gap01c from aloneproductprecept where gap00c = @tocompid and gap02c = @pre01c) -- alonecardprecept insert alonecardprecept(gac00c, gac01c, gac02c, gac03i, gac04f, gac05i, gac06f, gac07i, gac08f, gac09i, gac10f, gac11f, gac12f, gac13f, gac14f, gac15f, gac16c, gac17c, gac18c, gac19c, gac20c ) select @tocompid, gac01c, gac02c, gac03i, gac04f, gac05i, gac06f, gac07i, gac08f, gac09i, gac10f, gac11f, gac12f, gac13f, gac14f, gac15f, gac16c, gac17c, gac18c, gac19c, gac20c from alonecardprecept where gac00c = @fromcompid and gac02c = @pre01c and gac01c not in(select gac01c from alonecardprecept where gac00c = @tocompid and gac02c = @pre01c) -- ham15 insert ham15(hap00c, hap01c, hap02c, hap03i, hap04f, hap05i, hap06f, hap07i, hap08f, hap09i, hap10f, hap11i, hap12f, hap13i, hap14f, hap15i, hap16f, hap17i, hap18f, hap19i, hap20f, hap21i, hap22f, hap23i, hap24f, hap25i, hap26f, hap37i, hap38f, hap39i, hap40f, hap41i, hap42f, hap43i, hap44f, hap45i, hap46f, hap47i, hap48f, hap49i, hap50f, hap51i, hap52f ) select @tocompid, hap01c, hap02c, hap03i, hap04f, hap05i, hap06f, hap07i, hap08f, hap09i, hap10f, hap11i, hap12f, hap13i, hap14f, hap15i, hap16f, hap17i, hap18f, hap19i, hap20f, hap21i, hap22f, hap23i, hap24f, hap25i, hap26f, hap37i, hap38f, hap39i, hap40f, hap41i, hap42f, hap43i, hap44f, hap45i, hap46f, hap47i, hap48f, hap49i, hap50f, hap51i, hap52f from ham15 where hap00c = @fromcompid and hap01c = @pre01c and hap02c not in(select hap02c from ham15 where hap00c = @tocompid and hap01c = @pre01c) end GO /****** Object: StoredProcedure [dbo].[upg_compute_gcm03_gcc09d] Script Date: 06/12/2017 06:19:44 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_compute_gcm03_gcc09d] ( @compid varchar(10), --门店编号 @cardno varchar(20), --卡号 @category int, --账户类别 @output varchar(10) output --返回日期 ) as begin --从新计算 账户有效日期 --声明变量 declare @fromDate varchar(10),@gak81f float,@gak82c varchar(5),@todate varchar(10),@gsa02c varchar(160),@sp223 varchar(10) select @sp223 = isnull(gsa03f,0) from gsm01 a with(nolock) where a.gsa00c = @compid and a.gsa01c = 'SP223' if(@sp223 = 0) begin -- 如果存在此账户 返回当前账户的余额 if(exists(select 1 from gcm03 a where a.gcc01c = @cardno and a.gcc03i = @category )) begin select top 1 @output = isnull(gcc09d,'') from gcm03 where gcc01c = @cardno and gcc03i = @category if(ltrim(rtrim(@output)) = '') begin select @output = gca07d from gcm01 where gca01c = @cardno end return; end --查询账户期限 和单位 如果为空 就还使用卡期限和 单位 select @fromDate = convert(varchar(8),getdate(),112), @gak81f = (case when isnull(a.gak81f ,0) = 0 then isnull(a.gak07f,0) else isnull(a.gak81f,0) end), @gak82c = (case when isnull(a.gak81f ,0) = 0 then isnull(a.gak08c,1) else isnull(a.gak82c,1) end) from gam10 a with(nolock),gcm01 b with(nolock) where a.gak00c = b.gca00c and a.gak01c = b.gca02c and b.gca01c = @cardno --若账户期限为空,那么账户有效期限就使用卡期限来计算 if(@gak81f <> 0) begin -- 计算日期 exec upg_cal_next_day @fromDate,@gak82c,@gak81f,@todate output select @output = @todate end end else begin --查询账户期限 和单位 如果为空 就还使用卡期限和 单位 select @fromDate = convert(varchar(8),getdate(),112), @gak81f = (case when isnull(a.gak81f ,0) = 0 then isnull(a.gak07f,0) else isnull(a.gak81f,0) end), @gak82c = (case when isnull(a.gak81f ,0) = 0 then isnull(a.gak08c,1) else isnull(a.gak82c,1) end) from gam10 a with(nolock),gcm01 b with(nolock) where a.gak00c = b.gca00c and a.gak01c = b.gca02c and b.gca01c = @cardno --若账户期限为空,那么账户有效期限就使用卡期限来计算 if(@gak81f <> 0) begin -- 计算日期 exec upg_cal_next_day @fromDate,@gak82c,@gak81f,@todate output select @output = @todate end end end GO /****** Object: StoredProcedure [dbo].[upg_check_can_consume] Script Date: 06/12/2017 06:19:39 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_check_can_consume] ( @compid varchar(10), -- 卡是哪个公司的 @cardtype varchar(10), -- 卡的类别 @cur_compid varchar(10), -- 当前消费的公司 @ret int output -- 是否可以消费 ) as begin declare @consumeflag int select @consumeflag = isnull(gak44i, 1) from gam10 with (nolock) where gak00c = @compid and gak01c = @cardtype if @consumeflag = 1 select @ret = 1 else if @consumeflag = 2 begin if exists(select 1 from gam27 where compid = @compid and cardtype = @cardtype and descompid = @cur_compid) begin select @ret = 1 end else begin select @ret = 0 end end else if @consumeflag = 3 begin if @compid = @cur_compid begin select @ret = 1 end else begin select @ret = 0 end end end GO /****** Object: StoredProcedure [dbo].[upg_init_gam10] Script Date: 06/12/2017 06:20:13 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_init_gam10] ( @compid varchar(10) ) as begin if not exists(select 1 from gam10 where gak00c = @compid and gak01c = 'ZZ') begin insert gam10(gak00c, gak01c, gak02c, gak03i, gak07f, gak08c, gak41i, gak42i, gak29i, gak06i, gak44i) values(@compid, 'ZZ', '电子会员卡', 1, 1, '1', 0, 1, 0, 0, 1) end end GO /****** Object: StoredProcedure [dbo].[upg_gcm03_gcc09d] Script Date: 06/12/2017 06:20:03 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_gcm03_gcc09d] ( @compid varchar(10), --门店编号 @cardno varchar(20) --卡号 ) as begin --声明变量 declare @fromDate varchar(10),@gak81f float,@gak82c varchar(5),@todate varchar(10),@gsa02c varchar(160) --查询账户期限 和单位 如果为空 就还使用卡期限和 单位 select @fromDate = convert(varchar(8),getdate(),112), @gak81f = (case when isnull(a.gak81f ,0) = 0 then isnull(a.gak07f,0) else isnull(a.gak81f,0) end), @gak82c = (case when isnull(a.gak81f,0) = 0 then isnull(a.gak08c,1) else isnull(a.gak82c,1) end) from gam10 a with(nolock),gcm01 b with(nolock) where a.gak00c = b.gca00c and a.gak01c = b.gca02c and b.gca01c = @cardno and b.gca00c = @compid --若账户期限为空,那么账户有效期限就使用卡期限来计算 if(@gak81f <> 0) begin -- 计算日期 exec upg_cal_next_day @fromDate,@gak82c,@gak81f,@todate output --查询当前系统设定的赠送账户- select @gsa02c = gsa02c from gsm01 where gsa01c = 'sp147' and gsa00c = @compid --更新会员卡账户 排除特定账户外 更改账户有效期限 update a set gcc08c = @fromDate, gcc09d = @todate from gcm03 a,gsm01 b where a.gcc00c = b.gsa00c and a.gcc01c = @cardno and a.gcc03i <> @gsa02c and a.gcc03i not in ('889','888','999') end end GO /****** Object: StoredProcedure [dbo].[upg_update_from_gam10] Script Date: 06/12/2017 06:20:49 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_update_from_gam10]( @compid varchar(10), @personid varchar(20)) as begin delete hem07 where heg00c = @compid and heg01c = @personid insert hem07(heg00c, heg01c, heg02c, heg03i, heg04f, heg05i, heg06f, heg07i, heg08f, heg09i, heg10f) select @compid, @personid, gak01c, gak20i, gak21f, gak17i, gak18f, gak25i, gak26f, gak27i, gak28f from gam10 where gak00c = @compid end GO /****** Object: StoredProcedure [dbo].[upg_get_card_upgrade_expired] Script Date: 06/12/2017 06:20:06 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_get_card_upgrade_expired] ( @compid varchar(10), -- 门店编号 @cardid varchar(20), -- 会员卡号 @newcardtype varchar(20) -- 新会员卡类别 ) as begin declare @gak07f float declare @gak08c varchar(5) select @gak07f = (case when isnull(a.gak81f ,0) = 0 then isnull(a.gak07f,0) else isnull(a.gak81f,0) end), @gak08c = (case when isnull(a.gak81f ,0) = 0 then isnull(a.gak08c,1) else isnull(a.gak82c,1) end) from gam10 a with(nolock) where gak00c = @compid and gak01c = @newcardtype declare @oldcardtype varchar(20) -- 旧会员卡类别 declare @gca07d varchar(8) -- 有效日期 select @oldcardtype = gca02c, @gca07d = gca07d from gcm01 where gca01c = @cardid and gca00c = gca13d declare @sp207_2 int select @sp207_2 = gza07i from gzm01 where gza00c = @compid and gza01c = @oldcardtype and gza02c = @newcardtype if @sp207_2 is null select @sp207_2 = convert(int, gsa02c) from gsm01 where gsa00c = @compid and gsa01c = 'SP207' select @gca07d as gca07d, @gak07f as gak07f, @gak08c as gak08c, isnull(@sp207_2, 1) as sp207_2 end GO /****** Object: StoredProcedure [dbo].[upg_init_demodata] Script Date: 06/12/2017 06:20:13 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_init_demodata](@compid varchar(10)) as begin insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, '000', '开单', '6', '2', '次', 0, 1, '010000') insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, '601', '美白保湿', '6', '2', '次', 50, 1, '010000') insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, '602', '双眼皮', '6', '2', '次', 400, 1, '010000') insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, '603', '皮肤护理', '6', '2', '次', 300, 1, '010000') insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, '604', '电睫毛', '6', '2', '次', 50, 1, '010000') insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, '605', '激光除皱', '6', '2', '次', 100, 1, '010000') insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, '606', '脱皮', '6', '2', '次', 200, 1, '010000') insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, '607', '化学去皮', '6', '2', '次', 100, 1, '010000') insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, '608', '拉皮', '6', '2', '次', 100, 1, '010000') insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, '609', '注射除皱', '6', '2', '次', 100, 1, '010000') insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, '610', '负压吸脂', '6', '2', '次', 100, 1, '010000') insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, '611', '超声吸脂', '6', '2', '次', 100, 1, '010000') insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, '612', '电子吸脂', '6', '2', '次', 100, 1, '010000') insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, '613', '绣眉', '6', '2', '次', 100, 1, '010000') insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, '614', '水晶凛唇', '6', '2', '次', 100, 1, '010000') insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, '615', '美目袪斑', '6', '2', '次', 100, 1, '010000') insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, '616', '玫瑰油压', '6', '2', '次', 100, 1, '010000') insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, '617', '抗敏护理', '6', '2', '次', 100, 1, '010000') insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, '618', '悬灸', '6', '2', '次', 100, 1, '010000') insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, '619', '腊疗', '6', '2', '次', 100, 1, '010000') insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, '620', '刮痧', '6', '2', '次', 100, 1, '010000') insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, '701', '手部按摩', '7', '2', '次', 50, 1, '010000') insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, '801', '足部按摩', '8', '2', '次', 88, 1, '010000') insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, '802', '采耳', '8', '2', '次', 50, 1, '010000') insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, '803', '洗眼', '8', '2', '次', 50, 1, '010000') insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, '804', '敲背', '8', '2', '次', 10, 1, '010000') insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, '805', '拔罐', '8', '2', '次', 30, 1, '010000') insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, '806', '刮痧', '8', '2', '次', 30, 1, '010000') insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, '807', '扦脚', '8', '2', '次', 30, 1, '010000') insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, '808', '刮脚', '8', '2', '次', 30, 1, '010000') insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, '809', '捏脚', '8', '2', '次', 30, 1, '010000') insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, '810', '泰式按摩', '8', '2', '次', 148, 1, '010000') insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, '811', '全身推拿45分钟', '8', '2', '次', 98, 1, '010000') insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, '812', '全身推拿60分钟', '8', '2', '次', 128, 1, '010000') insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, '813', '推油60分钟', '8', '2', '次', 180, 1, '010000') insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, '814', '推油90分钟', '8', '2', '次', 228, 1, '010000') insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, '901', '单剪', '9', '2', '次', 10, 1, '010000') insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, '902', '单洗', '9', '2', '次', 10, 1, '010000') insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, '903', '单吹', '9', '2', '次', 10, 1, '010000') insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, '904', '洗剪吹', '9', '2', '次', 30, 1, '010000') insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, '905', '烫发', '9', '2', '次', 80, 1, '010000') insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, '906', '电发', '9', '2', '次', 70, 1, '010000') insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, '907', '平头', '9', '2', '次', 30, 1, '010000') insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, '908', '碎发', '9', '2', '次', 20, 1, '010000') insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, '909', '接发', '9', '2', '次', 100, 1, '010000') insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, '910', '大波浪', '9', '2', '次', 200, 1, '010000') insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, '911', '护理', '9', '2', '次', 10, 1, '010000') insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, '912', '盘发', '9', '2', '次', 20, 1, '010000') insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, '913', '首席洗吹', '9', '2', '次', 80, 1, '010000') insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, '914', '首席洗剪吹', '9', '2', '次', 100, 1, '010000') insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, '915', '总监洗吹', '9', '2', '次', 90, 1, '010000') insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, '916', '总监洗剪吹', '9', '2', '次', 120, 1, '010000') update gdm01 set gda13c = gda04c where isnull(gda13c, '') = '' delete gfm01 where gfa00c = @compid insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, 'OD1901', '00001', '卓约时尚染膏', '2', 'BOT', 200, 90, '0') insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, 'OD1902', '00002', '卓约炫红系列染膏', '2', 'BOT', 200, 90, '0') insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, 'OD1903', '00003', '卓约无尘漂粉', '2', 'BOT', 200, 90, '0') insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, 'OD1904', '00004', '卓约SV10双养奶', '2', 'BOT', 200, 90, '0') insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, 'OD1905', '00005', '卓约SV20双养奶', '2', 'BOT', 200, 90, '0') insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, 'OD1906', '00006', '卓约SV30双养奶', '2', 'BOT', 200, 90, '0') insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, 'OD1907', '00007', '卓约SV40双养奶', '2', 'BOT', 200, 90, '0') insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, 'OD1908', '00008', '卓约炫红色板', '2', 'BOT', 200, 90, '0') insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, 'OD1909', '00009', '卓约染烫洗发水', '2', 'BOT', 200, 90, '0') insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, 'OD1910', '00010', '卓约深层清洁洗发水', '2', 'BOT', 200, 90, '0') insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, 'OD1911', '00011', '卓约锁色润发素', '2', 'BOT', 200, 90, '0') insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, 'OD1912', '00012', '卓约锁色洗发水', '2', 'BOT', 200, 90, '0') insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, 'OD1913', '00013', '卓约全能润发素', '2', 'BOT', 200, 90, '0') insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, 'OD1914', '00014', '卓约去屑洗发水250ml', '2', 'BOT', 200, 90, '0') insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, 'OD1915', '00015', '卓约去屑洗发水1200ml', '2', 'BOT', 200, 90, '0') insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, 'OD1916', '00016', '卓约滋润洗发水', '2', 'BOT', 200, 90, '0') insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, 'OD1917', '00017', '卓约滋养洗发水', '2', 'BOT', 200, 90, '0') insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, 'OD1918', '00018', '卓约清洁洗发水', '2', 'BOT', 200, 90, '0') insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, 'OD1919', '00019', '卓约草本精华护法素', '2', 'BOT', 200, 90, '0') insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, 'OD1920', '00020', '卓约浪漫曲发套装', '2', 'T', 200, 90, '0') insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, 'OD1921', '00021', '卓约草本曲发套装', '2', 'T', 200, 90, '0') insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, 'OD1922', '00022', '卓约草本直发组合', '2', 'T', 200, 90, '0') insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, 'OD1923', '00023', '卓约草本造型精英蜡', '2', 'T', 200, 90, '0') insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, 'OD1924', '00024', '卓约质感发胶', '2', 'BOT', 200, 90, '0') insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, 'OD1925', '00025', '卓约主体层次发蜡', '2', 'T', 200, 90, '0') insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, 'OD1926', '00026', '卓约动感造型发泥', '2', 'BOT', 200, 90, '0') insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, 'OD1927', '00027', '卓约时尚造型者喱', '2', 'BOT', 200, 90, '0') insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, 'OD1928', '00028', '卓约亮丽喷雾者喱', '2', 'BOT', 200, 90, '0') insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, 'OD1929', '00029', '卓约毛发贴眼顺滑素', '2', 'BOT', 200, 90, '0') insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, 'OD1930', '00030', '卓约全能毛发护理液', '2', 'BOT', 200, 90, '0') insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, 'OD1931', '00031', '卓约毛端修护液', '2', 'BOT', 200, 90, '0') insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, 'OD1932', '00032', '卓约纤弱发质护理液', '2', 'BOT', 200, 90, '0') insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, 'OD1933', '00033', '卓约染烫发质护理液', '2', 'BOT', 200, 90, '0') insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, 'OD1934', '00034', '卓约高效去屑精华素', '2', 'BOT', 200, 90, '0') insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, 'OD1935', '00035', '卓约草本养发组合', '2', 'BOT', 200, 90, '0') insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, 'OD1936', '00036', '卓约SPA护法膜', '2', 'T', 200, 90, '0') insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa12c, gfa28i, gfa10c) values(@compid, 'HYBH001', '00037', '花样风采净白洁肤液', '1', 'BOT', 100, '30g', 90, '0') insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa12c, gfa28i, gfa10c) values(@compid, 'HYBH002', '00038', '花样风采净白柔肤液', '1', 'BOT', 100, '30g', 90, '0') insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa12c, gfa28i, gfa10c) values(@compid, 'HYBH003', '00039', '花样风采净白精华液', '1', 'BOT', 100, '30g', 90, '0') insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa12c, gfa28i, gfa10c) values(@compid, 'HYBH004', '00040', '花样风采净白柔乳', '1', 'BOT', 100, '30g', 90, '0') insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa12c, gfa28i, gfa10c) values(@compid, 'HYFR001', '00041', '花样凝脂水嫩颈霜', '1', 'BOT', 100, '30g', 90, '0') insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa12c, gfa28i, gfa10c) values(@compid, 'HYFR002', '00042', '花样凝脂水嫩晚霜', '1', 'BOT', 100, '30g', 90, '0') insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa12c, gfa28i, gfa10c) values(@compid, 'HYFR003', '00043', '花样凝脂水嫩日霜', '1', 'BOT', 100, '30g', 90, '0') insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa12c, gfa28i, gfa10c) values(@compid, 'HYFR004', '00044', '花样凝脂水嫩精华液', '1', 'BOT', 100, '30g', 90, '0') insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa12c, gfa28i, gfa10c) values(@compid, 'HYFR005', '00045', '花样凝脂水嫩醒肤水', '1', 'BOT', 100, '30g', 90, '0') insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa12c, gfa28i, gfa10c) values(@compid, 'HYFR006', '00046', '花样凝脂水嫩洁肤液', '1', 'BOT', 100, '30g', 90, '0') insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa12c, gfa28i, gfa10c) values(@compid, 'HYMG001', '00047', '花样情怀PMS保湿因子原液', '1', 'BOT', 100, '30g', 90, '0') insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa12c, gfa28i, gfa10c) values(@compid, 'HYMG002', '00048', '花样情怀PMS舒缓柔敏原液', '1', 'BOT', 100, '30g', 90, '0') insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa12c, gfa28i, gfa10c) values(@compid, 'HYMG003', '00049', '花样情怀PMS平衡净化原液', '1', 'BOT', 100, '30g', 90, '0') insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa12c, gfa28i, gfa10c) values(@compid, 'HYMG004', '00050', '花样情怀PMS舒缓柔肤水', '1', 'BOT', 100, '30g', 90, '0') insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa12c, gfa28i, gfa10c) values(@compid, 'HYMG005', '00051', '花样情怀PMS黄铜抗皱原液', '1', 'BOT', 100, '30g', 90, '0') insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa12c, gfa28i, gfa10c) values(@compid, 'HYMG006', '00052', '花样情怀PMS植物活化再生冻净粉', '1', 'BOT', 100, '30g', 90, '0') insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa12c, gfa28i, gfa10c) values(@compid, 'HYMG007', '00053', '花样情怀PMS舒缓素', '1', 'BOT', 100, '30g', 90, '0') insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa12c, gfa28i, gfa10c) values(@compid, 'HYYL001', '00054', '花样窈窕循坏代谢沐浴精油', '1', 'BOT', 100, '30g', 90, '0') insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa12c, gfa28i, gfa10c) values(@compid, 'HYYL002', '00055', '花样窈窕循坏代谢按摩精油', '1', 'BOT', 100, '30g', 90, '0') insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa12c, gfa28i, gfa10c) values(@compid, 'HYYL003', '00056', '花样窈窕舒缓镇静沐浴精油', '1', 'BOT', 100, '30g', 90, '0') truncate table gsm11 insert gsm11(gsk02c, gsk03c) values('06', '自然美') insert gsm11(gsk02c, gsk03c) values('06', '安利') insert gsm11(gsk02c, gsk03c) values('06', '花样年华') insert gsm11(gsk02c, gsk03c) values('06', '蕾施梦') insert gsm11(gsk02c, gsk03c) values('06', '羽西') insert gsm11(gsk02c, gsk03c) values('06', '欧莱雅') insert gsm11(gsk02c, gsk03c) values('06', '玉兰油') insert gsm11(gsk02c, gsk03c) values('06', '雅芳') insert gsm11(gsk02c, gsk03c) values('06', '倩碧') insert gsm11(gsk02c, gsk03c) values('06', '资生堂') insert gsm11(gsk02c, gsk03c) values('02', '室内空调小间') insert gsm11(gsk02c, gsk03c) values('02', '室内空调大间') insert gsm11(gsk02c, gsk03c) values('02', '室内通风通气') insert gsm11(gsk02c, gsk03c) values('02', '室内通风不畅') insert gsm11(gsk02c, gsk03c) values('02', '室外露天') insert gsm11(gsk02c, gsk03c) values('02', '室外遮荫') insert gsm11(gsk02c, gsk03c) values('05', '饮食偏辣') insert gsm11(gsk02c, gsk03c) values('05', '饮食偏甜') insert gsm11(gsk02c, gsk03c) values('05', '饮食偏淡') insert gsm11(gsk02c, gsk03c) values('05', '饮食偏咸') insert gsm11(gsk02c, gsk03c) values('05', '饮食偏油腻') insert gsm11(gsk02c, gsk03c) values('03', '外来快餐') insert gsm11(gsk02c, gsk03c) values('03', '简单饭食') insert gsm11(gsk02c, gsk03c) values('03', '简单面食') insert gsm11(gsk02c, gsk03c) values('03', '家庭餐') insert gsm11(gsk02c, gsk03c) values('03', '宴会餐') insert gsm11(gsk02c, gsk03c) values('07', '美丽田园') insert gsm11(gsk02c, gsk03c) values('07', '文峰美容美发') insert gsm11(gsk02c, gsk03c) values('07', 'NB自然美') insert gsm11(gsk02c, gsk03c) values('07', '华安美容') insert gsm11(gsk02c, gsk03c) values('07', '邱竞美容') insert gsm11(gsk02c, gsk03c) values('07', '王磊形象') insert gsm11(gsk02c, gsk03c) values('07', '孙平形象') insert gsm11(gsk02c, gsk03c) values('07', '永琪美容美发') insert gsm11(gsk02c, gsk03c) values('08', '长时间效果不明显') insert gsm11(gsk02c, gsk03c) values('08', '护肤后持续时间很短,感觉干燥不舒服') insert gsm11(gsk02c, gsk03c) values('08', '价格比较贵,但产品没有特殊之处') insert gsm11(gsk02c, gsk03c) values('08', '环境感觉不是很舒服') insert gsm11(gsk02c, gsk03c) values('08', '感觉没有达到最佳效果') insert gsm11(gsk02c, gsk03c) values('09', '希望皮肤看上去湿润光滑') insert gsm11(gsk02c, gsk03c) values('09', '希望脸部斑点能够浅一些') insert gsm11(gsk02c, gsk03c) values('09', '希望消除黑眼圈') insert gsm11(gsk02c, gsk03c) values('09', '希望能够有较好腰身') insert gsm11(gsk02c, gsk03c) values('09', '希望发型充满活力') insert gsm11(gsk02c, gsk03c) values('09', '希望发型让人看上去成熟稳重') insert gsm11(gsk02c, gsk03c) values('10', '力度重一些') insert gsm11(gsk02c, gsk03c) values('10', '力度柔和一些') insert gsm11(gsk02c, gsk03c) values('11', '比较严重,有很长时间历史') insert gsm11(gsk02c, gsk03c) values('11', '开始症状,不太明显') insert gsm11(gsk02c, gsk03c) values('11', '中度,正常护理能够消除') insert gsm11(gsk02c, gsk03c) values('12', '清洁皮肤') insert gsm11(gsk02c, gsk03c) values('12', '蒸汽护理') insert gsm11(gsk02c, gsk03c) values('12', '脱 屑') insert gsm11(gsk02c, gsk03c) values('12', '涂去斑霜') insert gsm11(gsk02c, gsk03c) values('12', '面部按摩') insert gsm11(gsk02c, gsk03c) values('12', '敷中药膜') insert gsm11(gsk02c, gsk03c) values('12', '电子导入') insert gsm11(gsk02c, gsk03c) values('12', '上收缩膜') insert gsm11(gsk02c, gsk03c) values('12', '涂去斑霜') insert gsm11(gsk02c, gsk03c) values('12', '去斑按摩') insert gsm11(gsk02c, gsk03c) values('13', '斑已经淡化') insert gsm11(gsk02c, gsk03c) values('13', '眼袋开始不明显') insert gsm11(gsk02c, gsk03c) values('13', '体重下降') delete ham01 where haa00c = @compid insert ham01(haa00c, haa01c, haa02c, haa03c, haa05i, haa12c, haa13c, haa14d, haa15f, haa16f, haa17c, haa18c, haa19c, haa20c, haa21c, haa24c, haa25c, haa91c, haa92d, haa27c, haa06c, haa39i, ownhaa00c, ownhaa01c) values(@compid, '001', '吴新明', 'Wu Xinming', 1, '340504195403138001', '3', '19740101', 178, 75, '201100', '上海宝成路158弄38号101', '021-54157753', '13565522685', '021-54159432', 'shboka@shboka.com', '001', 'ADMIN', '20040604', '2', '003', 1, @compid, '001') insert ham01(haa00c, haa01c, haa02c, haa03c, haa05i, haa12c, haa13c, haa14d, haa15f, haa16f, haa17c, haa18c, haa19c, haa20c, haa21c, haa24c, haa25c, haa91c, haa92d, haa27c, haa06c, haa39i, ownhaa00c, ownhaa01c) values(@compid, '002', '温观望', 'Mr. Wen', 1, '340504195403138002', '3', '19740101', 178, 75, '201100', '上海宝成路158弄38号102', '021-54157753', '13565522685', '021-54159432', 'shboka@shboka.com', '001', 'ADMIN', '20040604', '2', '003', 1, @compid, '002') insert ham01(haa00c, haa01c, haa02c, haa03c, haa05i, haa12c, haa13c, haa14d, haa15f, haa16f, haa17c, haa18c, haa19c, haa20c, haa21c, haa24c, haa25c, haa91c, haa92d, haa27c, haa06c, haa39i, ownhaa00c, ownhaa01c) values(@compid, '003', '宗绪杰', 'Mr. Zong', 1, '340504195403138003', '3', '19740101', 178, 75, '201100', '上海宝成路158弄38号103', '021-54157753', '13565522685', '021-54159432', 'shboka@shboka.com', '001', 'ADMIN', '20040604', '2', '003', 1, @compid, '003') insert ham01(haa00c, haa01c, haa02c, haa03c, haa05i, haa12c, haa13c, haa14d, haa15f, haa16f, haa17c, haa18c, haa19c, haa20c, haa21c, haa24c, haa25c, haa91c, haa92d, haa27c, haa06c, haa39i, ownhaa00c, ownhaa01c) values(@compid, '004', '毕鹏先', 'Li Ying', 1, '340504195403138004', '3', '19740101', 178, 75, '201100', '上海宝成路158弄38号104', '021-54157753', '13565522685', '021-54159432', 'shboka@shboka.com', '001', 'ADMIN', '20040604', '2', '003', 1, @compid, '004') insert ham01(haa00c, haa01c, haa02c, haa03c, haa05i, haa12c, haa13c, haa14d, haa15f, haa16f, haa17c, haa18c, haa19c, haa20c, haa21c, haa24c, haa25c, haa91c, haa92d, haa27c, haa06c, haa39i, ownhaa00c, ownhaa01c) values(@compid, '005', '王奇', 'Simon', 1, '340504195403138005', '3', '19740101', 178, 75, '201100', '上海宝成路158弄38号105', '021-54157753', '13565522685', '021-54159432', 'shboka@shboka.com', '001', 'ADMIN', '20040604', '2', '004', 1, @compid, '005') insert ham01(haa00c, haa01c, haa02c, haa03c, haa05i, haa12c, haa13c, haa14d, haa15f, haa16f, haa17c, haa18c, haa19c, haa20c, haa21c, haa24c, haa25c, haa91c, haa92d, haa27c, haa06c, haa39i, ownhaa00c, ownhaa01c) values(@compid, '006', '陈继光', 'Mr, Chen', 1, '340504195403138006', '3', '19740101', 178, 75, '201100', '上海宝成路158弄38号105', '021-54157753', '13565522685', '021-54159432', 'shboka@shboka.com', '001', 'ADMIN', '20040604', '2', '004', 1, @compid, '006') insert ham01(haa00c, haa01c, haa02c, haa03c, haa05i, haa12c, haa13c, haa14d, haa15f, haa16f, haa17c, haa18c, haa19c, haa20c, haa21c, haa24c, haa25c, haa91c, haa92d, haa27c, haa06c, haa39i, ownhaa00c, ownhaa01c) values(@compid, '007', '吴文晋', 'Mr, Wu', 1, '340504195403138007', '3', '19740101', 178, 75, '201100', '上海宝成路158弄38号105', '021-54157753', '13565522685', '021-54159432', 'shboka@shboka.com', '001', 'ADMIN', '20040604', '2', '004', 1, @compid, '007') insert ham01(haa00c, haa01c, haa02c, haa03c, haa05i, haa12c, haa13c, haa14d, haa15f, haa16f, haa17c, haa18c, haa19c, haa20c, haa21c, haa24c, haa25c, haa91c, haa92d, haa27c, haa06c, haa39i, ownhaa00c, ownhaa01c) values(@compid, '008', '洪强', 'Mr, Hong', 1, '340504195403138008', '3', '19740101', 178, 75, '201100', '上海宝成路158弄38号105', '021-54157753', '13565522685', '021-54159432', 'shboka@shboka.com', '001', 'ADMIN', '20040604', '2', '004', 1, @compid, '008') delete gam10 where gak00c = @compid insert gam10(gak00c, gak01c, gak02c, gak03i, gak04i, gak05i, gak06i, gak07f, gak08c, gak09f, gak16f, gak14i, gak29i) values(@compid, 'A', '500储值卡', 1, 0, 0, 0, 10, '1', 500, 500, 2, 0) insert gam10(gak00c, gak01c, gak02c, gak03i, gak04i, gak05i, gak06i, gak07f, gak08c, gak09f, gak16f, gak14i, gak29i) values(@compid, 'B', '1000储值卡', 1, 0, 0, 0, 10, '1', 1000, 1000, 2, 0) insert gam10(gak00c, gak01c, gak02c, gak03i, gak04i, gak05i, gak06i, gak07f, gak08c, gak09f, gak16f, gak14i, gak29i) values(@compid, 'C', '5000储值卡', 1, 0, 0, 0, 10, '1', 5000, 5000, 2, 0) delete gcm01 where gca00c = @compid insert gcm01(gca00c, gca01c, gca02c, gca03i, gca08i, gca09c, gca10f, gca14i ) values(@compid, 'A001', 'A', 1, 1, '1', 10, 0) insert gcm01(gca00c, gca01c, gca02c, gca03i, gca08i, gca09c, gca10f, gca14i ) values(@compid, 'A002', 'A', 1, 1, '1', 10, 0) insert gcm01(gca00c, gca01c, gca02c, gca03i, gca08i, gca09c, gca10f, gca14i ) values(@compid, 'A003', 'A', 1, 1, '1', 10, 0) insert gcm01(gca00c, gca01c, gca02c, gca03i, gca08i, gca09c, gca10f, gca14i ) values(@compid, 'A004', 'A', 1, 1, '1', 10, 0) insert gcm01(gca00c, gca01c, gca02c, gca03i, gca08i, gca09c, gca10f, gca14i ) values(@compid, 'A005', 'A', 1, 1, '1', 10, 0) insert gcm01(gca00c, gca01c, gca02c, gca03i, gca08i, gca09c, gca10f, gca14i ) values(@compid, 'A006', 'A', 1, 1, '1', 10, 0) insert gcm01(gca00c, gca01c, gca02c, gca03i, gca08i, gca09c, gca10f, gca14i ) values(@compid, 'A007', 'A', 1, 1, '1', 10, 0) insert gcm01(gca00c, gca01c, gca02c, gca03i, gca08i, gca09c, gca10f, gca14i ) values(@compid, 'A008', 'A', 1, 1, '1', 10, 0) insert gcm01(gca00c, gca01c, gca02c, gca03i, gca08i, gca09c, gca10f, gca14i ) values(@compid, 'A009', 'A', 1, 1, '1', 10, 0) insert gcm01(gca00c, gca01c, gca02c, gca03i, gca08i, gca09c, gca10f, gca14i ) values(@compid, 'A010', 'A', 1, 1, '1', 10, 0) insert gcm01(gca00c, gca01c, gca02c, gca03i, gca08i, gca09c, gca10f, gca14i ) values(@compid, 'B001', 'B', 1, 1, '1', 10, 0) insert gcm01(gca00c, gca01c, gca02c, gca03i, gca08i, gca09c, gca10f, gca14i ) values(@compid, 'B002', 'B', 1, 1, '1', 10, 0) insert gcm01(gca00c, gca01c, gca02c, gca03i, gca08i, gca09c, gca10f, gca14i ) values(@compid, 'B003', 'B', 1, 1, '1', 10, 0) insert gcm01(gca00c, gca01c, gca02c, gca03i, gca08i, gca09c, gca10f, gca14i ) values(@compid, 'B004', 'B', 1, 1, '1', 10, 0) insert gcm01(gca00c, gca01c, gca02c, gca03i, gca08i, gca09c, gca10f, gca14i ) values(@compid, 'B005', 'B', 1, 1, '1', 10, 0) insert gcm01(gca00c, gca01c, gca02c, gca03i, gca08i, gca09c, gca10f, gca14i ) values(@compid, 'B006', 'B', 1, 1, '1', 10, 0) insert gcm01(gca00c, gca01c, gca02c, gca03i, gca08i, gca09c, gca10f, gca14i ) values(@compid, 'B007', 'B', 1, 1, '1', 10, 0) insert gcm01(gca00c, gca01c, gca02c, gca03i, gca08i, gca09c, gca10f, gca14i ) values(@compid, 'B008', 'B', 1, 1, '1', 10, 0) insert gcm01(gca00c, gca01c, gca02c, gca03i, gca08i, gca09c, gca10f, gca14i ) values(@compid, 'B009', 'B', 1, 1, '1', 10, 0) insert gcm01(gca00c, gca01c, gca02c, gca03i, gca08i, gca09c, gca10f, gca14i ) values(@compid, 'B010', 'B', 1, 1, '1', 10, 0) insert gcm01(gca00c, gca01c, gca02c, gca03i, gca08i, gca09c, gca10f, gca14i ) values(@compid, 'C001', 'C', 1, 1, '1', 10, 0) insert gcm01(gca00c, gca01c, gca02c, gca03i, gca08i, gca09c, gca10f, gca14i ) values(@compid, 'C002', 'C', 1, 1, '1', 10, 0) insert gcm01(gca00c, gca01c, gca02c, gca03i, gca08i, gca09c, gca10f, gca14i ) values(@compid, 'C003', 'C', 1, 1, '1', 10, 0) insert gcm01(gca00c, gca01c, gca02c, gca03i, gca08i, gca09c, gca10f, gca14i ) values(@compid, 'C004', 'C', 1, 1, '1', 10, 0) insert gcm01(gca00c, gca01c, gca02c, gca03i, gca08i, gca09c, gca10f, gca14i ) values(@compid, 'C005', 'C', 1, 1, '1', 10, 0) insert gcm01(gca00c, gca01c, gca02c, gca03i, gca08i, gca09c, gca10f, gca14i ) values(@compid, 'C006', 'C', 1, 1, '1', 10, 0) insert gcm01(gca00c, gca01c, gca02c, gca03i, gca08i, gca09c, gca10f, gca14i ) values(@compid, 'C007', 'C', 1, 1, '1', 10, 0) insert gcm01(gca00c, gca01c, gca02c, gca03i, gca08i, gca09c, gca10f, gca14i ) values(@compid, 'C008', 'C', 1, 1, '1', 10, 0) insert gcm01(gca00c, gca01c, gca02c, gca03i, gca08i, gca09c, gca10f, gca14i ) values(@compid, 'C009', 'C', 1, 1, '1', 10, 0) insert gcm01(gca00c, gca01c, gca02c, gca03i, gca08i, gca09c, gca10f, gca14i ) values(@compid, 'C010', 'C', 1, 1, '1', 10, 0) update gcm01 set gca13d = gca00c where gca00c = @compid delete gcm03 where gcc00c = @compid insert gcm03(gcc00c, gcc01c, gcc02f, gcc03i, gcc05c, gcc06f) values(@compid, 'A001', 1, 2, 'RMB', 500) insert gcm03(gcc00c, gcc01c, gcc02f, gcc03i, gcc05c, gcc06f) values(@compid, 'A002', 1, 2, 'RMB', 500) insert gcm03(gcc00c, gcc01c, gcc02f, gcc03i, gcc05c, gcc06f) values(@compid, 'A003', 1, 2, 'RMB', 500) insert gcm03(gcc00c, gcc01c, gcc02f, gcc03i, gcc05c, gcc06f) values(@compid, 'A004', 1, 2, 'RMB', 500) insert gcm03(gcc00c, gcc01c, gcc02f, gcc03i, gcc05c, gcc06f) values(@compid, 'A005', 1, 2, 'RMB', 500) insert gcm03(gcc00c, gcc01c, gcc02f, gcc03i, gcc05c, gcc06f) values(@compid, 'A006', 1, 2, 'RMB', 500) insert gcm03(gcc00c, gcc01c, gcc02f, gcc03i, gcc05c, gcc06f) values(@compid, 'A007', 1, 2, 'RMB', 500) insert gcm03(gcc00c, gcc01c, gcc02f, gcc03i, gcc05c, gcc06f) values(@compid, 'A008', 1, 2, 'RMB', 500) insert gcm03(gcc00c, gcc01c, gcc02f, gcc03i, gcc05c, gcc06f) values(@compid, 'A009', 1, 2, 'RMB', 500) insert gcm03(gcc00c, gcc01c, gcc02f, gcc03i, gcc05c, gcc06f) values(@compid, 'A010', 1, 2, 'RMB', 500) insert gcm03(gcc00c, gcc01c, gcc02f, gcc03i, gcc05c, gcc06f) values(@compid, 'B001', 1, 2, 'RMB', 1000) insert gcm03(gcc00c, gcc01c, gcc02f, gcc03i, gcc05c, gcc06f) values(@compid, 'B002', 1, 2, 'RMB', 1000) insert gcm03(gcc00c, gcc01c, gcc02f, gcc03i, gcc05c, gcc06f) values(@compid, 'B003', 1, 2, 'RMB', 1000) insert gcm03(gcc00c, gcc01c, gcc02f, gcc03i, gcc05c, gcc06f) values(@compid, 'B004', 1, 2, 'RMB', 1000) insert gcm03(gcc00c, gcc01c, gcc02f, gcc03i, gcc05c, gcc06f) values(@compid, 'B005', 1, 2, 'RMB', 1000) insert gcm03(gcc00c, gcc01c, gcc02f, gcc03i, gcc05c, gcc06f) values(@compid, 'B006', 1, 2, 'RMB', 1000) insert gcm03(gcc00c, gcc01c, gcc02f, gcc03i, gcc05c, gcc06f) values(@compid, 'B007', 1, 2, 'RMB', 1000) insert gcm03(gcc00c, gcc01c, gcc02f, gcc03i, gcc05c, gcc06f) values(@compid, 'B008', 1, 2, 'RMB', 1000) insert gcm03(gcc00c, gcc01c, gcc02f, gcc03i, gcc05c, gcc06f) values(@compid, 'B009', 1, 2, 'RMB', 1000) insert gcm03(gcc00c, gcc01c, gcc02f, gcc03i, gcc05c, gcc06f) values(@compid, 'B010', 1, 2, 'RMB', 1000) insert gcm03(gcc00c, gcc01c, gcc02f, gcc03i, gcc05c, gcc06f) values(@compid, 'C001', 1, 2, 'RMB', 5000) insert gcm03(gcc00c, gcc01c, gcc02f, gcc03i, gcc05c, gcc06f) values(@compid, 'C002', 1, 2, 'RMB', 5000) insert gcm03(gcc00c, gcc01c, gcc02f, gcc03i, gcc05c, gcc06f) values(@compid, 'C003', 1, 2, 'RMB', 5000) insert gcm03(gcc00c, gcc01c, gcc02f, gcc03i, gcc05c, gcc06f) values(@compid, 'C004', 1, 2, 'RMB', 5000) insert gcm03(gcc00c, gcc01c, gcc02f, gcc03i, gcc05c, gcc06f) values(@compid, 'C005', 1, 2, 'RMB', 5000) insert gcm03(gcc00c, gcc01c, gcc02f, gcc03i, gcc05c, gcc06f) values(@compid, 'C006', 1, 2, 'RMB', 5000) insert gcm03(gcc00c, gcc01c, gcc02f, gcc03i, gcc05c, gcc06f) values(@compid, 'C007', 1, 2, 'RMB', 5000) insert gcm03(gcc00c, gcc01c, gcc02f, gcc03i, gcc05c, gcc06f) values(@compid, 'C008', 1, 2, 'RMB', 5000) insert gcm03(gcc00c, gcc01c, gcc02f, gcc03i, gcc05c, gcc06f) values(@compid, 'C009', 1, 2, 'RMB', 5000) insert gcm03(gcc00c, gcc01c, gcc02f, gcc03i, gcc05c, gcc06f) values(@compid, 'C010', 1, 2, 'RMB', 5000) --房间资料 delete gicm02 where gicb00c = @compid insert gicm02(gicb00c, gicb01v, gicb02v, gicb13c) values(@compid, '101', '茶花厅', '002') insert gicm02(gicb00c, gicb01v, gicb02v, gicb13c) values(@compid, '102', '菊花厅', '002') insert gicm02(gicb00c, gicb01v, gicb02v, gicb13c) values(@compid, '103', '玫瑰厅', '002') insert gicm02(gicb00c, gicb01v, gicb02v, gicb13c) values(@compid, '104', '牡丹厅', '002') insert gicm02(gicb00c, gicb01v, gicb02v, gicb13c) values(@compid, '105', '桂花厅', '002') insert gicm02(gicb00c, gicb01v, gicb02v, gicb13c) values(@compid, '201', '黄海厅', @compid) insert gicm02(gicb00c, gicb01v, gicb02v, gicb13c) values(@compid, '202', '东海厅', @compid) insert gicm02(gicb00c, gicb01v, gicb02v, gicb13c) values(@compid, '203', '南海厅', @compid) insert gicm02(gicb00c, gicb01v, gicb02v, gicb13c) values(@compid, '204', '红海厅', @compid) insert gicm02(gicb00c, gicb01v, gicb02v, gicb13c) values(@compid, '205', '蓝海厅', @compid) delete gicm03 where gicc00c = @compid insert gicm03(gicc00c, gicc01v, gicc02v, gicc03v) values(@compid, '101', '101-1', '101-1') insert gicm03(gicc00c, gicc01v, gicc02v, gicc03v) values(@compid, '101', '101-2', '101-2') insert gicm03(gicc00c, gicc01v, gicc02v, gicc03v) values(@compid, '102', '102-1', '102-1') insert gicm03(gicc00c, gicc01v, gicc02v, gicc03v) values(@compid, '102', '102-2', '102-2') insert gicm03(gicc00c, gicc01v, gicc02v, gicc03v) values(@compid, '103', '103-1', '103-1') insert gicm03(gicc00c, gicc01v, gicc02v, gicc03v) values(@compid, '103', '103-2', '103-2') insert gicm03(gicc00c, gicc01v, gicc02v, gicc03v) values(@compid, '104', '104-1', '104-1') insert gicm03(gicc00c, gicc01v, gicc02v, gicc03v) values(@compid, '104', '104-2', '104-2') insert gicm03(gicc00c, gicc01v, gicc02v, gicc03v) values(@compid, '105', '105-1', '105-1') insert gicm03(gicc00c, gicc01v, gicc02v, gicc03v) values(@compid, '105', '105-2', '105-2') insert gicm03(gicc00c, gicc01v, gicc02v, gicc03v) values(@compid, '201', '201-1', '201-1') insert gicm03(gicc00c, gicc01v, gicc02v, gicc03v) values(@compid, '202', '202-1', '202-1') insert gicm03(gicc00c, gicc01v, gicc02v, gicc03v) values(@compid, '203', '203-1', '203-1') insert gicm03(gicc00c, gicc01v, gicc02v, gicc03v) values(@compid, '204', '204-1', '204-1') insert gicm03(gicc00c, gicc01v, gicc02v, gicc03v) values(@compid, '205', '205-1', '205-1') end GO /****** Object: StoredProcedure [dbo].[upg_ymnstaff_verf] Script Date: 06/12/2017 06:20:53 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_ymnstaff_verf] ( @compid varchar(10), @fromdate varchar(8), @todate varchar(8), @fromstaff varchar(20), @tostaff varchar(20) ) as begin create table #result ( staffno varchar(20) null, --员工编号 prnum float null, --人数 prdlnum float null, --成交数 prdlrate float null, --成交率 prgdnum float null, --当次办理产品人数 prprjnum float null, --当次办理项目人数 prspecnum float null, --单次特护人数 prbackcardnum float null, --二次回卡人数 prebeaprjnum float null, --美容项目人数 prgdverp float null, --产品业绩 prbeautverp float null, --美容业绩 prspecverp float null, --特护业绩-单次 prprjverp float null, --特护业绩-项目 prsubverp float null, --特护业绩小计 prtotalverp float null, --业绩合计 praverage float null, --平均值 ltgoldcardnum float null, --售后金卡及以上 ltplatcardnum float null, --售后白金及以上 ltsubnum float null, --售后小计 dlgoldcardnum float null, --成交金卡 dlplatcardnum float null, --成交白金卡 dlsubnum float null, --成交小计 gdgoldcardnum float null, --产品销售金卡 gdplatcardnum float null, --产品业绩白金卡 gdsubnum float null, --产品业绩小计 gdaverage float null, --产品业绩平均值 prjsnum float null, --项目服务人次 prjpnum float null, --项目实际人数 prjdlnum float null, --项目成交人数 prjdjrate float null, --项目成交率 prjnewcardnum float null, --项目新增办卡人数 prjbeautverp float null, --项目美容业绩 prjspeciverp float null, --项目特护业绩 prjaverage float null, --项目平均值 ltbackcardverp float null, --退卡业绩 lttotalverp float null, --售后业绩合计 gstnum float null, --嘉宾人数 gstdlnum float null, --嘉宾成交人数 gstdlrate float null, --嘉宾成交率 gstgdverp float null, --嘉宾产品业绩 gstbeaverp float null, --嘉宾美容业绩 gstspecverp float null, --嘉宾特护业绩 gstsubverp float null, --嘉宾业绩小计 gstaverage float null, --嘉宾平均值 todaygdverp float null, --当日产品业绩 todaybeaverp float null, --当日美容业绩 todayspecverp float null, --当日特护业绩 todaytotal float null, --当日总业绩合计 otheramt float null, --其他收入 deposit float null, --保证金收入 backdeposit float null, --退保证金 depositsale float null, --定金转销售 remain float null, --余额 ) declare @platlevel int --白金卡的等级 declare @goldlevel int --金卡的等级 select @platlevel = gak15i from gam10 where gak02c like '%白金卡%' and gak00c = @compid select @goldlevel = gak15i from gam10 where gak02c like '%金卡%' and gak00c = @compid create table #prjinfo ( compid varchar(10) null, billno varchar(20) null, billtype varchar(5) null, staffno varchar(20) null, prjno varchar(20) null, prnum float null, prdlnum float null, prspecnum float null, prspecverp float null, ltgoldcardnum float null, ltplatcardnum float null, prjsnum float null, prjpnum float null, gstnum float null, gstdlnum float null, otheramt float null, depositsale float null, prjbeautverp float null, ) create table #goodinfo ( compid varchar(10) null, billno varchar(20) null, billtype varchar(5) null, staffno varchar(20) null, goodno varchar(20) null, prnum float null, --人数 prgdnum float null, --当次办理产品人数 ltgoldcardnum float null, --售后金卡及以上 ltplatcardnum float null, --售后白金及以上 prjpnum float null, --项目实际人数 prgdverp float null, --产品业绩 gdgoldcardnum float null, --产品业绩金卡 gdplatcardnum float null, --产品业绩白金卡 ltbackcardverp float null, --退卡业绩 gstnum float null, --嘉宾人数 gstdlnum float null, --嘉宾成交人数 gstgdverp float null, --嘉宾产品业绩 depositsale float null, ) --先处理各类项目和产品的 --收银消费明细 --因为伊美娜只有一个大工所以只对大工进行处理 insert into #prjinfo(compid, billno, billtype, prjno, staffno, prnum, prdlnum, prspecnum, prspecverp, ltgoldcardnum, ltplatcardnum, prjsnum, prjpnum, gstnum, gstdlnum, otheramt, depositsale, prjbeautverp) select gga00c, gga01c, 'gx', ggb03c, ggb12c, a = case when gga99i = 1 then gga14f else 0 end, b = case when ggb27c in(select gsn01c from gsm14 where gsn02c = '999' or isnull(gsn02c, '') = '') and ggb45i = 0 and gga99i = 1 then 1 else 0 end, c = case when gga99i = 1 and gda04c = '7' and ggb45i = 0 then 1 else 0 end, d = case when gga99i = 1 and gda04c = '7' and ggb45i = 0 then ggb20f else 0 end, e = case when gak15i <= @goldlevel and gga99i = 2 then 1 else 0 end, f = case when gak15i >@platlevel and gga99i = 2 then 1 else 0 end, g = case when gda04c<>'Z' and gga99i = 2 then 1 else 0 end, h = case when gga99i = 2 then gga14f else 0 end, i = case when gga99i = 3 then gga14f else 0 end, j = case when ggb27c = '1' and ggb45i = 0 and gga99i = 3 then 1 else 0 end, k = case when gda04c = 'Z' then ggb11f else 0 end, l = case when ggb27c = '&'then ggb11f else 0 end, m = case when gga99i = 2 then ggb20f else 0 end from ggm02, ggm01, gdm01, gcm01, gam10 where gda00c = @compid and gda01c = ggb03c and gga08c = gca01c and gca00c = @compid and gak00c = gca00c and gak01c = gca02c and (ggb12c between @fromstaff and @tostaff or @fromstaff = '*') and gga80d between @fromdate and @todate and gga00c = ggb00c and gga01c = ggb01c and gga00c = @compid and isnull(ggb12c, '')<>'' and isnull(gga99i, 0)<>0 --产品明细,--伊美娜没有对产品的销售人员进行处理,所以还是三个 --处理第一销售 insert into #goodinfo(compid, billno, billtype, goodno, staffno, prnum, prgdnum, prgdverp, gdgoldcardnum, gdplatcardnum, ltbackcardverp, gstnum, gstdlnum, gstgdverp, depositsale, ltgoldcardnum, ltplatcardnum, prjpnum) select gga00c, gga01c, 'gx', ggc03c, ggc12c, b = case when gga99i = 1 then gga14f else 0 end, b = case when gga99i = 1 then 1 else 0 end, a = case when gga99i = 1 then ggc21f else 0 end, c = case when gak15i <= @goldlevel and gga99i = 2 then ggc21f else 0 end, e = case when gak15i >@platlevel and gga99i = 2 then ggc21f else 0 end, f = case when gga99i = 2 and isnull(ggc11f, 0)<0 then ggc21f else 0 end, i = case when gga99i = 3 then 1 else 0 end, j = case when gga99i = 3 then 1 else 0 end, k = case when gga99i = 3 then ggc21f else 0 end, l = case when ggc27c = '&'then ggc11f else 0 end, e = case when gak15i <= @goldlevel and gga99i = 2 then 1 else 0 end, f = case when gak15i >@platlevel and gga99i = 2 then 1 else 0 end, i = case when gga99i = 2 then 1 else 0 end from ggm03, ggm01, gcm01, gam10 where gak00c = gca00c and gak01c = gca02c and gga00c = ggc00c and gga01c = ggc01c and gga08c = gca01c and gca00c = gga00c and (ggc12c between @fromstaff and @tostaff or @fromstaff = '*') and gga00c = @compid and isnull(ggc12c, '') <>'' and gga80d between @fromdate and @todate and gga00c = ggc00c and gga01c = ggc01c and isnull(gga99i, 0)<>0 --处理第二销售 insert into #goodinfo(compid, billno, billtype, goodno, staffno, prnum, prgdnum, prgdverp, gdgoldcardnum, gdplatcardnum, ltbackcardverp, gstnum, gstdlnum, gstgdverp, depositsale, ltgoldcardnum, ltplatcardnum, prjpnum) select gga00c, gga01c, 'gx', ggc03c, ggc15c, b = case when gga99i = 1 then gga14f else 0 end, b = case when gga99i = 1 then 1 else 0 end, a = case when gga99i = 1 then ggc23f else 0 end, c = case when gak15i <= @goldlevel and gga99i = 2 then ggc23f else 0 end, e = case when gak15i >@platlevel and gga99i = 2 then ggc23f else 0 end, f = case when gga99i = 2 and isnull(ggc11f, 0)<0 then ggc23f else 0 end, i = case when gga99i = 3 then 1 else 0 end, j = case when gga99i = 3 then 1 else 0 end, h = case when gga99i = 3 then ggc23f else 0 end, l = case when ggc27c = '&'then ggc11f else 0 end, e = case when gak15i <= @goldlevel and gga99i = 2 then 1 else 0 end, f = case when gak15i >@platlevel and gga99i = 2 then 1 else 0 end, i = case when gga99i = 2 then 1 else 0 end from ggm03, ggm01, gcm01, gam10 where gak00c = gca00c and gak01c = gca02c and gga08c = gca01c and gca00c = @compid and ( ggc15c between @fromstaff and @tostaff or @fromstaff = '*') and gga00c = @compid and isnull(ggc15c, '')<>'' and gga80d between @fromdate and @todate and gga00c = ggc00c and gga01c = ggc01c and isnull(gga99i, 0)<>0 --处理第三销售 insert into #goodinfo(compid, billno, billtype, goodno, staffno, prnum, prgdnum, prgdverp, gdgoldcardnum, gdplatcardnum, ltbackcardverp, gstnum, gstdlnum, gstgdverp, depositsale, ltgoldcardnum, ltplatcardnum, prjpnum) select gga00c, gga01c, 'gx', ggc03c, ggc17c, b = case when gga99i = 1 then gga14f else 0 end, b = case when gga99i = 1 then 1 else 0 end, a = case when gga99i = 1 then ggc25f else 0 end, c = case when gak15i <= @goldlevel and gga99i = 2 then ggc25f else 0 end, e = case when gak15i >@platlevel and gga99i = 2 then ggc25f else 0 end, f = case when gga99i = 2 and isnull(ggc11f, 0)<0 then ggc25f else 0 end, i = case when gga99i = 3 then 1 else 0 end, j = case when gga99i = 3 then 1 else 0 end, h = case when gga99i = 3 then ggc25f else 0 end, l = case when ggc27c = '&'then ggc11f else 0 end, e = case when gak15i <= @goldlevel and gga99i = 2 then 1 else 0 end, f = case when gak15i >@platlevel and gga99i = 2 then 1 else 0 end, i = case when gga99i = 2 then 1 else 0 end from ggm03, ggm01, gcm01, gam10 where gak00c = gca00c and gak01c = gca02c and gga08c = gca01c and gca00c = @compid and (ggc17c between @fromstaff and @tostaff or @fromstaff = '*') and gga00c = @compid and isnull(ggc17c, '') <>'' and gga80d between @fromdate and @todate and gga00c = ggc00c and gga01c = ggc01c and isnull(gga99i, 0)<>0 ---因为存在同一个收银单及有项目也有产品,那么人数应该是只统计一次 update a set a.prnum = 0, a.gstnum = 0, a.prgdnum = 0, a.gstdlnum = 0, a.ltgoldcardnum = 0, a.ltplatcardnum = 0, a.prjpnum = 0 from #goodinfo a, #prjinfo b where a.compid = b.compid and a.billno = b.billno and a.staffno = b.staffno and a.staffno = b.staffno insert into #result(staffno, prnum, prdlnum, prspecnum, prspecverp, ltgoldcardnum, ltplatcardnum, prjsnum, prjpnum, gstnum, gstdlnum, otheramt, depositsale, prjbeautverp) select staffno, prnum, prdlnum, prspecnum, prspecverp, ltgoldcardnum, ltplatcardnum, prjsnum, prjpnum, gstnum, gstdlnum, otheramt, depositsale, prjbeautverp from #prjinfo insert into #result(staffno, prnum, prdlnum, prgdnum, prgdverp, gdgoldcardnum, gdplatcardnum, ltbackcardverp, gstnum, gstdlnum, gstgdverp, depositsale, ltgoldcardnum, ltplatcardnum, prjpnum) select staffno, prnum, prgdnum, prgdnum, prgdverp, gdgoldcardnum, gdplatcardnum, ltbackcardverp, gstnum, gstdlnum, gstgdverp, depositsale, ltgoldcardnum, ltplatcardnum, prjpnum from #goodinfo --处理买卡的 create table #salecard ( seq int identity not null, compid varchar(10) null, --公司编号 billno varchar(20) null, --单号 billtype varchar(5) null, --单据类型 staffno varchar(20) null, --员工编号 prjno varchar(20) null, --项目编号 prdlnum float null, --成交人数 prprjnum float null, --当次办理项目人数 prbackcardnum float null, --二次回卡人数 prebeaprjnum float null, --美容项目人数 prbeautverp float null, --美容业绩 prprjverp float null, --特护业绩-项目 dlgoldcardnum float null, --成交金卡 dlplatcardnum float null, --成交白金卡 gstdlnum float null, --嘉宾成交人数 depositsale float null, --定金转销售 ) --伊美娜的版本最多有3个销售人员 --第一销售 注意这里有可能存在重复的值,所以最后的时候要把单号相同的给删除了 --同时办理项目人数只算在第一销售的头上 insert into #salecard(compid, billno, billtype, staffno, prjno, prdlnum, prprjnum, prbackcardnum, prebeaprjnum, prbeautverp, prprjverp, dlgoldcardnum, dlplatcardnum, gstdlnum, depositsale) select distinct gna00c, gna01c, 'gn', gna20c, gnc02c, a = case when gna99i = 1 then 1 else 0 end, b = case when gna99i = 1 then 1 else 0 end, c = case when gsc04c = '&' and gna99i = 1 then 1 else 0 end, d = case when gda04c = '6' and gna99i = 1 then 1 else 0 end, e = case when gda04c = '6' and gna99i = 1 then gna67f else 0 end, f = case when gda04c = '7' and gna99i = 1 then gna67f else 0 end, g = case when gak15i <= @goldlevel and gna99i = 2 then 1 else 0 end, h = case when gak15i >@platlevel and gna99i = 2 then 1 else 0 end, j = case when gna99i = 3 then 1 else 0 end, l = case when gsc04c = '&' then gsc05f else 0 end from gnm01 left outer join gnm03 on gna00c = gnc00c and gna01c = gnc01c left outer join gsm03 on gna00c = gsc00c and gna01c = gsc01c left outer join gcm01 on gna00c = gca00c and gna13c = gca01c left outer join gam10 on gca00c = gak00c and gca02c = gak01c left outer join gdm01 on gnc00c = gda00c and gnc02c = gda01c where gsc02c = 'gn' and gna00c = @compid and gna80d between @fromdate and @todate and (gna20c between @fromstaff and @tostaff or @fromstaff = '*') and isnull(gna93c, '')<>'' and isnull(gna20c, '')<>'' and isnull(gna99i, 0)<>0 --第二销售 insert into #salecard(compid, billno, billtype, staffno, prjno, prdlnum, prprjnum, prbackcardnum, prebeaprjnum, prbeautverp, prprjverp, dlgoldcardnum, dlplatcardnum, gstdlnum, depositsale) select distinct gna00c, gna01c, 'gn', gna28c, gnc02c, a = case when gna99i = 1 then 1 else 0 end, 0, c = case when gsc04c = '&' and gna99i = 1 then 1 else 0 end, d = case when gda04c = '6' and gna99i = 1 then 1 else 0 end, e = case when gda04c = '6' and gna99i = 1 then gna67f else 0 end, f = case when gda04c = '7' and gna99i = 1 then gna67f else 0 end, g = case when gak15i <= @goldlevel and gna99i = 2 then 1 else 0 end, h = case when gak15i > @platlevel and gna99i = 2 then 1 else 0 end, j = case when gna99i = 3 then 1 else 0 end, l = case when gsc04c = '&' then gsc05f else 0 end from gnm01 left outer join gnm03 on gna00c = gnc00c and gna01c = gnc01c left outer join gsm03 on gna00c = gsc00c and gna01c = gsc01c left outer join gcm01 on gna00c = gca00c and gna13c = gca01c left outer join gam10 on gca00c = gak00c and gca02c = gak01c left outer join gdm01 on gnc00c = gda00c and gnc02c = gda01c where gsc02c = 'gn' and gna00c = @compid and gna80d between @fromdate and @todate and (gna28c between @fromstaff and @tostaff or @fromstaff = '*') and isnull(gna93c, '')<>'' and isnull(gna28c, '')<>'' and isnull(gna99i, 0)<>0 --第三销售 insert into #salecard(compid, billno, billtype, staffno, prjno, prdlnum, prprjnum, prbackcardnum, prebeaprjnum, prbeautverp, prprjverp, dlgoldcardnum, dlplatcardnum, gstdlnum, depositsale) select distinct gna00c, gna01c, 'gn', gna30c, gnc02c, a = case when gna99i = 1 then 1 else 0 end, 0, c = case when gsc04c = '&' and gna99i = 1 then 1 else 0 end, d = case when gda04c = '6' and gna99i = 1 then 1 else 0 end, e = case when gda04c = '6' and gna99i = 1 then gna67f else 0 end, f = case when gda04c = '7' and gna99i = 1 then gna67f else 0 end, g = case when gak15i <= @goldlevel and gna99i = 2 then 1 else 0 end, h = case when gak15i > @platlevel and gna99i = 2 then 1 else 0 end, j = case when gna99i = 3 then 1 else 0 end, l = case when gsc04c = '&' then gsc05f else 0 end from gnm01 left outer join gnm03 on gna00c = gnc00c and gna01c = gnc01c left outer join gsm03 on gna00c = gsc00c and gna01c = gsc01c left outer join gcm01 on gna00c = gca00c and gna13c = gca01c left outer join gam10 on gca00c = gak00c and gca02c = gak01c left outer join gdm01 on gnc00c = gda00c and gnc02c = gda01c where gsc02c = 'gn' and gna00c = @compid and gna80d between @fromdate and @todate and (gna30c between @fromstaff and @tostaff or @fromstaff = '*') and isnull(gna93c, '')<>'' and isnull(gna30c, '')<>'' and isnull(gna99i, 0)<>0 --对于一个单子有多个项目应该把其中的嘉宾成交人数和人数设置成0 只保留一个 update a set a.prdlnum = 0, a.prprjnum = 0 from #salecard a, #salecard b where a.compid = b.compid and a.billno = b.billno and a.seq>b.seq and a.staffno = b.staffno --debug insert into #result(staffno, prdlnum, prprjnum, prbackcardnum, prebeaprjnum, prbeautverp, prprjverp, dlgoldcardnum, dlplatcardnum, gstdlnum, depositsale) select staffno, prdlnum, prprjnum, prbackcardnum, prebeaprjnum, prbeautverp, prprjverp, dlgoldcardnum, dlplatcardnum, gstdlnum, depositsale from #salecard create table #buyprjinfo ( seq int identity not null, compid varchar(10) null, --公司编号 billno varchar(20) null, --单号 billtype varchar(5) null, --单据类型 staffno varchar(20) null, --员工编号 prjno varchar(20) null, --项目编号 prbeautverp float null, --美容业绩 dlgoldcardnum float null, --成交金卡 dlplatcardnum float null, --成交白金卡 prjnewcardnum float null, --项目新增办卡人数 prjbeautverp float null, --项目美容业绩 prjspeciverp float null, --项目特护业绩 ltbackcardverp float null, --退卡业绩 gstbeaverp float null, --嘉宾美容业绩 gstspecverp float null, --嘉宾特护业绩 depositsale float null, --定金转销售 gstnum float null, --嘉宾人数 gstdlnum float null, --嘉宾成交人数 ) --第一个销售 insert into #buyprjinfo (compid, billno, billtype, staffno, prjno, prbeautverp, dlgoldcardnum, dlplatcardnum, prjnewcardnum, prjbeautverp, prjspeciverp, ltbackcardverp, gstbeaverp, gstspecverp, depositsale, gstnum, gstdlnum) select distinct gcl00c, gcl01c, 'gz', gcl09c, gcm02c, a = case when gcl99i = 1 and gda04c = '6' then gcl58f else 0 end, b = case when gak15i <= @goldlevel and gcl99i = 2 then 1 else 0 end, c = case when gak15i >@platlevel and gcl99i = 2 then 1 else 0 end, d = case when gcl99i = 2 then 1 else 0 end, e = case when gcl99i = 2 and gda04c = '6' then gcl58f else 0 end, f = case when gcl99i = 2 and gda04c = '7' then gcl58f else 0 end, g = case when gcl99i = 2 and gcl06i = 1 then gcl58f else 0 end, h = case when gcl99i = 3 and gda04c = '6' then gcl58f else 0 end, i = case when gcl99i = 3 and gda04c = '7' then gcl58f else 0 end, j = case when gsc04c = '&' then gsc05f else 0 end, i = case when gcl99i = 3 then 1 else 0 end, i = case when gcl99i = 3 then 1 else 0 end from gcm10 left outer join gcm11 on gcl00c = gcm00c and gcl01c = gcm01c left outer join gsm03 on gcl00c = gsc00c and gcl01c = gsc01c left outer join gcm01 on gcl00c = gca00c and gcl03c = gca01c left outer join gam10 on gca02c = gak01c and gak00c = gca00c left outer join gdm01 on gcm00c = gda00c and gcm02c = gda01c where gcl00c = @compid and gsc02c = 'gz' and gcl80d between @fromdate and @todate and (gcl09c between @fromstaff and @tostaff or @fromstaff = '*') and isnull(gcl93c, '')<>'' and isnull(gcl09c, '')<>'' and isnull(gcl99i, '')<>0 --第二个销售 insert into #buyprjinfo (compid, billno, billtype, staffno, prjno, prbeautverp, dlgoldcardnum, dlplatcardnum, prjnewcardnum, prjbeautverp, prjspeciverp, ltbackcardverp, gstbeaverp, gstspecverp, depositsale, gstnum, gstdlnum) select distinct gcl00c, gcl01c, 'gz', gcl17c, gcm02c, a = case when gcl99i = 1 and gda04c = '6' then gcl60f else 0 end, b = case when gak15i <= @goldlevel and gcl99i = 2 then 1 else 0 end, c = case when gak15i >@platlevel and gcl99i = 2 then 1 else 0 end, d = case when gcl99i = 2 then 1 else 0 end, e = case when gcl99i = 2 and gda04c = '6' then gcl60f else 0 end, f = case when gcl99i = 2 and gda04c = '7' then gcl60f else 0 end, g = case when gcl99i = 2 and gcl06i = 1 then gcl60f else 0 end, h = case when gcl99i = 3 and gda04c = '6' then gcl60f else 0 end, i = case when gcl99i = 3 and gda04c = '7' then gcl60f else 0 end, j = case when gsc04c = '&' then gsc05f else 0 end, i = case when gcl99i = 3 then 1 else 0 end, i = case when gcl99i = 3 then 1 else 0 end from gcm10 left outer join gcm11 on gcl00c = gcm00c and gcl01c = gcm01c left outer join gsm03 on gcl00c = gsc00c and gcl01c = gsc01c left outer join gcm01 on gcl00c = gca00c and gcl03c = gca01c left outer join gam10 on gca02c = gak01c and gak00c = gca00c left outer join gdm01 on gcm00c = gda00c and gcm02c = gda01c where gcl00c = @compid and gsc02c = 'gz' and gcl80d between @fromdate and @todate and (gcl17c between @fromstaff and @tostaff or @fromstaff = '*') and isnull(gcl93c, '')<>'' and isnull(gcl17c, '')<>'' and isnull(gcl99i, '')<>0 --第三个销售 insert into #buyprjinfo (compid, billno, billtype, staffno, prjno, prbeautverp, dlgoldcardnum, dlplatcardnum, prjnewcardnum, prjbeautverp, prjspeciverp, ltbackcardverp, gstbeaverp, gstspecverp, depositsale, gstnum, gstdlnum) select distinct gcl00c, gcl01c, 'gz', gcl19c, gcm02c, a = case when gcl99i = 1 and gda04c = '6' then gcl62f else 0 end, b = case when gak15i <= @goldlevel and gcl99i = 2 then 1 else 0 end, c = case when gak15i >@platlevel and gcl99i = 2 then 1 else 0 end, d = case when gcl99i = 2 then 1 else 0 end, e = case when gcl99i = 2 and gda04c = '6' then gcl62f else 0 end, f = case when gcl99i = 2 and gda04c = '7' then gcl62f else 0 end, g = case when gcl99i = 2 and gcl06i = 1 then gcl62f else 0 end, h = case when gcl99i = 3 and gda04c = '6' then gcl62f else 0 end, i = case when gcl99i = 3 and gda04c = '7' then gcl62f else 0 end, j = case when gsc04c = '&' then gsc05f else 0 end, i = case when gcl99i = 3 then 1 else 0 end, i = case when gcl99i = 3 then 1 else 0 end from gcm10 left outer join gcm11 on gcl00c = gcm00c and gcl01c = gcm01c left outer join gsm03 on gcl00c = gsc00c and gcl01c = gsc01c left outer join gcm01 on gcl00c = gca00c and gcl03c = gca01c left outer join gam10 on gca02c = gak01c and gak00c = gca00c left outer join gdm01 on gcm00c = gda00c and gcm02c = gda01c where gcl00c = @compid and gsc02c = 'gz' and gcl80d between @fromdate and @todate and (gcl19c between @fromstaff and @tostaff or @fromstaff = '*') and isnull(gcl93c, '')<>'' and isnull(gcl19c, '')<>'' and isnull(gcl99i, '')<>0 --对于一个单子有多个项目应该把其中的嘉宾成交人数和人数设置成0 只保留一个 update a set a.gstnum = 0, a.gstdlnum = 0, a.dlgoldcardnum = 0, a.dlplatcardnum = 0, a.prjnewcardnum = 0, a.depositsale = 0 from #buyprjinfo a, #buyprjinfo b where a.compid = b.compid and a.billno = b.billno and a.seq>b.seq and a.staffno = b.staffno --debug ---insert 到最后的结果表中 insert into #result(staffno, prbeautverp, dlgoldcardnum, dlplatcardnum, prjnewcardnum, prjbeautverp, prjspeciverp, ltbackcardverp, gstbeaverp, gstspecverp, depositsale, gstnum, gstdlnum) select staffno, prbeautverp, dlgoldcardnum, dlplatcardnum, prjnewcardnum, prjbeautverp, prjspeciverp, ltbackcardverp, gstbeaverp, gstspecverp, depositsale, gstnum, gstdlnum from #buyprjinfo --一下是计算员工保证金的 create table #deposit -- 会员基本资料--客户保证金 ( compid varchar(10) null, --公司编号 staffno varchar(20) null, --员工编号 savemoney float null, --当时存入金额 savedate varchar(8) null, --当时存入日期 getmoney float null, -- 取用金额 getddate varchar(8) null, --取用日期 flag int null, --标志没有任何意思 0 --1 ) --先出入存入的保证金 insert into #deposit(compid, staffno, savemoney, savedate, getmoney, getddate, flag) select gbl00c, gbl06c, gbl04f, gbl05d, 0, '', 0 from gbm15 where gbl00c = @compid and (gbl06c between @fromstaff and @tostaff or @fromstaff = '*') and (gbl05d between @fromdate and @todate) --在插入直接取款的 --先出入存入的保证金 insert into #deposit(compid, staffno, savemoney, savedate, getmoney, getddate, flag) select gbl00c, a = case when isnull(gbl10c, '') = '' then gbl06c else gbl10c end, 0, '', gbl07f, gbl08d, 0 from gbm15 where gbl00c = @compid and (gbl10c between @fromstaff and @tostaff or @fromstaff = '*') and (gbl08d between @fromdate and @todate) and isnull(gbl09c, '') = '' --统计每员工的存入和去除 insert into #deposit(compid, staffno, savemoney, savedate, getmoney, getddate, flag) select compid, staffno, sum(isnull(savemoney, 0)), '', sum(isnull(getmoney, 0)), '', 1 from #deposit group by compid, staffno delete #deposit where flag = 0 create table #finallyresult ( staffno varchar(20) null, --员工编号 prnum float null, --人数 prdlnum float null, --成交数 prdlrate float null, --成交率 prgdnum float null, --当次办理产品人数 prprjnum float null, --当次办理项目人数 prspecnum float null, --单次特护人数 prbackcardnum float null, --二次回卡人数 prebeaprjnum float null, --美容项目人数 prgdverp float null, --产品业绩 prbeautverp float null, --美容业绩 prspecverp float null, --特护业绩-单次 prprjverp float null, --特护业绩-项目 prsubverp float null, --特护业绩小计 prtotalverp float null, --业绩合计 praverage float null, --平均值 ltgoldcardnum float null, --售后金卡及以上 ltplatcardnum float null, --售后白金及以上 ltsubnum float null, --售后小计 dlgoldcardnum float null, --成交金卡 dlplatcardnum float null, --成交白金卡 dlsubnum float null, --成交小计 gdgoldcardnum float null, --产品销售金卡 gdplatcardnum float null, --产品业绩白金卡 gdsubnum float null, --产品业绩小计 gdaverage float null, --产品业绩平均值 prjsnum float null, --项目服务人次 prjpnum float null, --项目实际人数 prjdlnum float null, --项目成交人数 prjdjrate float null, --项目成交率 prjnewcardnum float null, --项目新增办卡人数 prjbeautverp float null, --项目美容业绩 prjspeciverp float null, --项目特护业绩 prjaverage float null, --项目平均值 ltbackcardverp float null, --退卡业绩 lttotalverp float null, --售后业绩合计 gstnum float null, --嘉宾人数 gstdlnum float null, --嘉宾成交人数 gstdlrate float null, --嘉宾成交率 gstgdverp float null, --嘉宾产品业绩 gstbeaverp float null, --嘉宾美容业绩 gstspecverp float null, --嘉宾特护业绩 gstsubverp float null, --嘉宾业绩小计 gstaverage float null, --嘉宾平均值 todaygdverp float null, --当日产品业绩 todaybeaverp float null, --当日美容业绩 todayspecverp float null, --当日特护业绩 todaytotal float null, --当日总业绩合计 otheramt float null, --其他收入 deposit float null, --保证金收入 backdeposit float null, --退保证金 depositsale float null, --定金转销售 remain float null, --余额 flag int null, --没有任何意义 只是判断这个条记录是合计还是详细 ) insert into #finallyresult(staffno, prnum, prdlnum, prgdnum, prprjnum, prspecnum, prbackcardnum, prebeaprjnum, prgdverp, prbeautverp, prspecverp, prprjverp, ltgoldcardnum, ltplatcardnum, dlgoldcardnum, dlplatcardnum, gdgoldcardnum, gdplatcardnum, prjsnum, prjpnum, prjdlnum, prjnewcardnum, prjbeautverp, prjspeciverp, ltbackcardverp, gstnum, gstdlnum, gstgdverp, gstbeaverp, gstspecverp, otheramt, deposit, backdeposit, depositsale, flag) select staffno, sum(isnull(prnum, 0)), sum(isnull(prdlnum, 0)), sum(isnull(prgdnum, 0)), sum(isnull(prprjnum, 0)), sum(isnull(prspecnum, 0)), sum(isnull(prbackcardnum, 0)), sum(isnull(prebeaprjnum, 0)), sum(isnull(prgdverp, 0)), sum(isnull(prbeautverp, 0)), sum(isnull(prspecverp, 0)), sum(isnull(prprjverp, 0)), sum(isnull(ltgoldcardnum, 0)), sum(isnull(ltplatcardnum, 0)), sum(isnull(dlgoldcardnum, 0)), sum(isnull(dlplatcardnum, 0)), sum(isnull(gdgoldcardnum, 0)), sum(isnull(gdplatcardnum, 0)), sum(isnull(prjsnum, 0)), sum(isnull(prjpnum, 0)), sum(isnull(prjdlnum, 0)), sum(isnull(prjnewcardnum, 0)), sum(isnull(prjbeautverp, 0)), sum(isnull(prjspeciverp, 0)), sum(isnull(ltbackcardverp, 0)), sum(isnull(gstnum, 0)), sum(isnull(gstdlnum, 0)), sum(isnull(gstgdverp, 0)), sum(isnull(gstbeaverp, 0)), sum(isnull(gstspecverp, 0)), sum(isnull(otheramt, 0)), sum(isnull(deposit, 0)), sum(isnull(backdeposit, 0)), sum(isnull(depositsale, 0)), 0 from #result group by staffno --如果这个保证金的员工存在那么就更新否则就insert update a set a.deposit = b.savemoney, a.backdeposit = b.getmoney from #finallyresult a, #deposit b where a.staffno = b.staffno delete b from #deposit b, #finallyresult a where a.staffno = b.staffno insert into #finallyresult(staffno, deposit, backdeposit, flag) select staffno, savemoney, getmoney, 0 from #deposit --更新各种比率或者平均值 update a set a.prdlrate = case when a.prnum = 0 then 0 else (a.prdlnum/a.prnum)*100 end, a.prsubverp = a.prspecverp + a.prprjverp, a.prtotalverp = a.prgdverp + a.prbeautverp + a.prspecverp, a.praverage = case when a.prdlnum = 0 then 0 else (a.prtotalverp/a.prdlnum) end, a.ltsubnum = a.ltgoldcardnum + a.ltplatcardnum, a.dlsubnum = a.dlgoldcardnum + a.dlplatcardnum, a.gdsubnum = a.gdgoldcardnum + a.gdplatcardnum, a.gdaverage = case when (a.gdgoldcardnum + a.gdplatcardnum) = 0 then 0 else (a.dlsubnum/(a.gdgoldcardnum + a.gdplatcardnum)) end, a.prjdjrate = case when a.prjpnum = 0 then 0 else (a.prjdlnum/a.prjpnum)*100 end, a.prjaverage = case when a.prjdlnum = 0 then 0 else ((a.gdgoldcardnum + a.gdplatcardnum + a.prjbeautverp + a.prjspeciverp)/a.prjdlnum) end, a.lttotalverp = a.gdgoldcardnum + a.gdplatcardnum + a.prjbeautverp + a.prjspeciverp - a.ltbackcardverp, a.gstdlrate = case when a.gstnum = 0 then 0 else (a.gstdlnum/a.gstnum)*100 end, a.gstsubverp = a.gstgdverp + a.gstbeaverp + a.gstspecverp, a.gstaverage = case when a.gstdlnum = 0 then 0 else ((a.gstgdverp + a.gstbeaverp + a.gstspecverp)/a.gstdlnum) end, a.todaygdverp = a.prgdverp + a.gdplatcardnum + a.gdgoldcardnum + a.gstgdverp, a.todaybeaverp = a.prbeautverp + a.prjbeautverp + a.gstbeaverp, a.todayspecverp = a.prspecverp + a.prprjverp + a.prjspeciverp + gstspecverp, a.todaytotal = (a.prgdverp + a.gdplatcardnum + a.gdgoldcardnum + a.gstgdverp) + (a.prbeautverp + a.prjbeautverp + a.gstbeaverp) + (a.prspecverp + a.prprjverp + a.prjspeciverp + gstspecverp), a.remain = a.deposit - a.backdeposit - a.depositsale from #finallyresult a --insert 合计 insert into #finallyresult(staffno, prnum, prdlnum, prgdnum, prprjnum, prspecnum, prbackcardnum, prebeaprjnum, prgdverp, prbeautverp, prspecverp, prprjverp, ltgoldcardnum, ltplatcardnum, dlgoldcardnum, dlplatcardnum, gdgoldcardnum, gdplatcardnum, prjsnum, prjpnum, prjdlnum, prjnewcardnum, prjbeautverp, prjspeciverp, ltbackcardverp, gstnum, gstdlnum, gstgdverp, gstbeaverp, gstspecverp, otheramt, deposit, backdeposit, depositsale, flag) select '合计', sum(isnull(prnum, 0)), sum(isnull(prdlnum, 0)), sum(isnull(prgdnum, 0)), sum(isnull(prprjnum, 0)), sum(isnull(prspecnum, 0)), sum(isnull(prbackcardnum, 0)), sum(isnull(prebeaprjnum, 0)), sum(isnull(prgdverp, 0)), sum(isnull(prbeautverp, 0)), sum(isnull(prspecverp, 0)), sum(isnull(prprjverp, 0)), sum(isnull(ltgoldcardnum, 0)), sum(isnull(ltplatcardnum, 0)), sum(isnull(dlgoldcardnum, 0)), sum(isnull(dlplatcardnum, 0)), sum(isnull(gdgoldcardnum, 0)), sum(isnull(gdplatcardnum, 0)), sum(isnull(prjsnum, 0)), sum(isnull(prjpnum, 0)), sum(isnull(prjdlnum, 0)), sum(isnull(prjnewcardnum, 0)), sum(isnull(prjbeautverp, 0)), sum(isnull(prjspeciverp, 0)), sum(isnull(ltbackcardverp, 0)), sum(isnull(gstnum, 0)), sum(isnull(gstdlnum, 0)), sum(isnull(gstgdverp, 0)), sum(isnull(gstbeaverp, 0)), sum(isnull(gstspecverp, 0)), sum(isnull(otheramt, 0)), sum(isnull(deposit, 0)), sum(isnull(backdeposit, 0)), sum(isnull(depositsale, 0)), 1 from #finallyresult where flag = 0 --更新合计的各种比率或者平均值 update a set a.prdlrate = case when a.prnum = 0 then 0 else (a.prdlnum/a.prnum)*100 end, a.prsubverp = a.prspecverp + a.prprjverp, a.prtotalverp = a.prgdverp + a.prbeautverp + a.prspecverp, a.praverage = case when a.prdlnum = 0 then 0 else (a.prtotalverp/a.prdlnum) end, a.ltsubnum = a.ltgoldcardnum + a.ltplatcardnum, a.dlsubnum = a.dlgoldcardnum + a.dlplatcardnum, a.gdsubnum = a.gdgoldcardnum + a.gdplatcardnum, a.gdaverage = case when (a.gdgoldcardnum + a.gdplatcardnum) = 0 then 0 else (a.dlsubnum/(a.gdgoldcardnum + a.gdplatcardnum)) end, a.prjdjrate = case when a.prjpnum = 0 then 0 else (a.prjdlnum/a.prjpnum)*100 end, a.prjaverage = case when a.prjdlnum = 0 then 0 else ((a.gdgoldcardnum + a.gdplatcardnum + a.prjbeautverp + a.prjspeciverp)/a.prjdlnum) end, a.lttotalverp = a.gdgoldcardnum + a.gdplatcardnum + a.prjbeautverp + a.prjspeciverp - a.ltbackcardverp, a.gstdlrate = case when a.gstnum = 0 then 0 else (a.gstdlnum/a.gstnum)*100 end, a.gstsubverp = a.gstgdverp + a.gstbeaverp + a.gstspecverp, a.gstaverage = case when a.gstdlnum = 0 then 0 else ((a.gstgdverp + a.gstbeaverp + a.gstspecverp)/a.gstdlnum) end, a.todaygdverp = a.prgdverp + a.gdplatcardnum + a.gdgoldcardnum + a.gstgdverp, a.todaybeaverp = a.prbeautverp + a.prjbeautverp + a.gstbeaverp, a.todayspecverp = a.prspecverp + a.prprjverp + a.prjspeciverp + gstspecverp, a.todaytotal = (a.prgdverp + a.gdplatcardnum + a.gdgoldcardnum + a.gstgdverp) + (a.prbeautverp + a.prjbeautverp + a.gstbeaverp) + (a.prspecverp + a.prprjverp + a.prjspeciverp + gstspecverp), a.remain = a.deposit - a.backdeposit - a.depositsale from #finallyresult a where flag = 1 --更新员工名称 update a set a.staffno = haa02c from #finallyresult a, ham01 where a.staffno = haa01c and haa00c = @compid --- delete #finallyresult where isnull(prnum, 0) = 0 and isnull(prdlnum, 0) = 0 and isnull(prgdnum, 0) = 0 and isnull(prprjnum, 0) = 0 and isnull(prspecnum, 0) = 0 and isnull(prbackcardnum, 0) = 0 and isnull(prebeaprjnum, 0) = 0 and isnull(prgdverp, 0) = 0 and isnull(prbeautverp, 0) = 0 and isnull(prspecverp, 0) = 0 and isnull(prprjverp, 0) = 0 and isnull(ltgoldcardnum, 0) = 0 and isnull(ltplatcardnum, 0) = 0 and isnull(dlgoldcardnum, 0) = 0 and isnull(dlplatcardnum, 0) = 0 and isnull(gdgoldcardnum, 0) = 0 and isnull(gdplatcardnum, 0) = 0 and isnull(prjsnum, 0) = 0 and isnull(prjpnum, 0) = 0 and isnull(prjdlnum, 0) = 0 and isnull(prjnewcardnum, 0) = 0 and isnull(prjbeautverp, 0) = 0 and isnull(prjspeciverp, 0) = 0 and isnull(ltbackcardverp, 0) = 0 and isnull(gstnum, 0) = 0 and isnull(gstdlnum, 0) = 0 and isnull(gstgdverp, 0) = 0 and isnull(gstbeaverp, 0) = 0 and isnull(gstspecverp, 0) = 0 and isnull(otheramt, 0) = 0 and isnull(deposit, 0) = 0 and isnull(backdeposit, 0) = 0 and isnull(depositsale, 0) = 0 select * from #finallyresult order by flag, staffno drop table #result drop table #prjinfo drop table #goodinfo drop table #salecard drop table #buyprjinfo drop table #finallyresult end GO /****** Object: StoredProcedure [dbo].[upg_change_cardkind] Script Date: 06/12/2017 06:19:39 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_change_cardkind] ( @compid varchar(10), --公司编号 @kindno varchar(20), --类别编号 @flag int --大类值(1.储值卡 2.资格卡 3.计次卡 4.单次卡) ) as--WUXM, 2004/12/26 begin update gam10 set gak03i = @flag where gak00c = @compid and gak01c = @kindno end GO /****** Object: StoredProcedure [dbo].[upg_gam29_date_overdue] Script Date: 06/12/2017 06:20:03 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_gam29_date_overdue] ( @gax00c varchar(10), --门店 @gax01c varchar(20), --会员卡类别 @chkstr varchar(300), --选择中的账号类别(以,分割) @copy varchar(10) --要拷贝到的门店 ) as begin declare @gca01c varchar(20) declare @gca07d varchar(10) --截止日期 if(@copy = '') begin --先清空对应类别的所有数据 delete from gam29 where gax00c = @gax00c and gax01c = @gax01c if(@chkstr <> '') --如果为空,则仅清空,无需再插入数据 begin insert into gam29(gax00c,gax01c,gax02i,gax03i) select @gax00c,@gax01c,f1,1 from f_splitstr(@chkstr,',') where isnull(F1,'') != '' --清空对应的日期 update gcm03 set gcc09d = '' where gcc00c = @gax00c and gcc01c in (select gca01c from gcm01 where gca00c = @gax00c and gca02c = @gax01c) and gcc03i in (select gax02i from gam29 where gax00c = @gax00c) --还原 declare cursor_gcm01 cursor for select gca01c,gca07d from gcm01 where gca00c = @gax00c and gca02c = @gax01c open cursor_gcm01 fetch next from cursor_gcm01 into @gca01c,@gca07d while(@@fetch_status = 0) begin --还原日期 update gcm03 set gcc09d = @gca07d where gcc00c = @gax00c and gcc01c = @gca01c and gcc03i not in ('4','9','888','889','999') and gcc03i not in (select gax02i from gam29 where gax00c = @gax00c and gax01c = @gax01c and gax03i = 1) fetch next from cursor_gcm01 into @gca01c,@gca07d end close cursor_gcm01 deallocate cursor_gcm01 end else begin --如果清空,则还原 declare cursor_gcm01 cursor for select gca01c,gca07d from gcm01 where gca00c = @gax00c and gca02c = @gax01c open cursor_gcm01 fetch next from cursor_gcm01 into @gca01c,@gca07d while(@@fetch_status = 0) begin --还原日期 update gcm03 set gcc09d = @gca07d where gcc00c = @gax00c and gcc01c = @gca01c and gcc03i not in ('4','9','888','889','999') and gcc03i not in (select gax02i from gam29 where gax00c = @gax00c and gax01c = @gax01c and gax03i = 1) fetch next from cursor_gcm01 into @gca01c,@gca07d end close cursor_gcm01 deallocate cursor_gcm01 end end else --@copy非空则表示拷贝数据 begin if(@copy != -1) begin delete from gam29 where gax00c = @gax00c and gax01c = @copy insert into gam29(gax00c,gax01c,gax02i,gax03i) select gax00c,@copy,gax02i,1 from gam29 where gax00c = @gax00c and gax01c = @gax01c end else begin delete from gam29 where gax00c = @gax00c and gax01c != @gax01c declare @gak01c varchar(10) --类别代码 declare cursor_gam10 cursor for select gak01c from gam10 where gak00c = @gax00c and gak01c != @gax01c open cursor_gam10 fetch next from cursor_gam10 into @gak01c while(@@fetch_status = 0) begin insert into gam29(gax00c,gax01c,gax02i,gax03i) select gax00c,@gak01c,gax02i,1 from gam29 where gax00c = @gax00c and gax01c = @gax01c --清空对应的日期 update gcm03 set gcc09d = '' where gcc00c = @gax00c and gcc01c in (select gca01c from gcm01 where gca00c = @gax00c and gca02c = @gak01c) and gcc03i not in ('4','9','888','889','999') and gcc03i in (select gax02i from gam29 where gax00c = @gax00c) fetch next from cursor_gam10 into @gak01c end close cursor_gam10 deallocate cursor_gam10 end end end GO /****** Object: StoredProcedure [dbo].[upg_change_one_card_type] Script Date: 06/12/2017 06:19:39 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_change_one_card_type] ( @compid varchar(10), @cardid varchar(20), @oldcardtype varchar(20), @newcardtype varchar(20), @userid varchar(20), @curdate varchar(10), @curtime varchar(10) ) as begin -- 根据卡号获取归属公司: declare @ownercompid nvarchar(10) --卡的归属公司 select @ownercompid = min(gca13d) from gcm01 where gca01c = @cardid and gca00c = gca13d declare @oldtype int select @oldtype = gak03i from gam10 where gak00c = @compid and gak01c = @oldcardtype declare @newtype int declare @totaltimes int declare @gak07f float -- 有效期限 declare @gak08c varchar(5) -- 期限单位 select @newtype = gak03i, @totaltimes = gak06i, @gak07f = gak07f, @gak08c = gak08c from gam10 where gak00c = @compid and gak01c = @newcardtype if @oldtype <> 3 and @newtype = 3 -- 老的是储值卡,新的是次卡 begin update gcm01 set gca02c = @newcardtype, gca03i = @newtype, gca14i = @totaltimes, gca15i = 0, gca09c = @gak08c, gca10f = @gak07f where gca00c = @ownercompid and gca01c = @cardid end else if @oldtype = 3 and @newtype <> 3 -- 老的是次卡,新的是储值卡 begin update gcm01 set gca02c = @newcardtype, gca03i = @newtype, gca14i = 0, gca09c = @gak08c, gca10f = @gak07f where gca00c = @ownercompid and gca01c = @cardid end else begin update gcm01 set gca02c = @newcardtype, gca03i = @newtype, gca09c = @gak08c, gca10f = @gak07f where gca00c = @ownercompid and gca01c = @cardid end end GO /****** Object: StoredProcedure [dbo].[upg_ymn_divide_bill_onesale_good] Script Date: 06/12/2017 06:20:52 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_ymn_divide_bill_onesale_good]( @compid varchar(10), -- 公司别 @fromdate varchar(10), @todate varchar(10)) as begin declare @billtype varchar(10) select @billtype = 'gx' delete a from gcm24 a, gam26 z where a.gcx00c = z.gaz02c and z.gaz01c = @compid and gcx03d >= @fromdate and gcx03d <= @todate --单次销售的项目 --美容 insert gcm24(gcx00c, gcx01c, gcx02c, gcx03d, gcx04c, gcx05c, gcx06c, gcx07c, gcx09c, gcx11c, gcx14f, gcx13f, gcx99i, gcx22i) select gga00c, 'gx', gga01c, gga80d, gga09c, gga05c, gga07c, ggb12c, ggb13c, ggb14c, ggb05f, ggb11f, gga99i, 1 from ggm02 b with (nolock), ggm01 a with (nolock), gam26 z, gdm01 c where a.gga00c = b.ggb00c and a.gga01c = b.ggb01c and a.gga00c = z.gaz02c and z.gaz01c = @compid and b.ggb00c = c.gda00c and b.ggb03c = c.gda01c and c.gda13c = '233' and b.ggb27c<>'9' and isnull(ggb11f, 0)>0 --仪器 insert gcm24(gcx00c, gcx01c, gcx02c, gcx03d, gcx04c, gcx05c, gcx06c, gcx07c, gcx09c, gcx11c, gcx18f, gcx17f, gcx99i, gcx22i) select gga00c, 'gx', gga01c, gga80d, gga09c, gga05c, gga07c, ggb12c, ggb13c, ggb14c, ggb05f, ggb11f, gga99i, 1 from ggm02 b with (nolock), ggm01 a with (nolock), gam26 z, gdm01 c where a.gga00c = b.ggb00c and a.gga01c = b.ggb01c and a.gga00c = z.gaz02c and z.gaz01c = @compid and b.ggb00c = c.gda00c and b.ggb03c = c.gda01c and (gda13c = '231' or gda13c = '232' or gda13c = '234') and b.ggb27c<>'9' and isnull(ggb11f, 0)>0 --美体 insert gcm24(gcx00c, gcx01c, gcx02c, gcx03d, gcx04c, gcx05c, gcx06c, gcx07c, gcx09c, gcx11c, gcx16f, gcx15f, gcx99i, gcx22i) select gga00c, 'gx', gga01c, gga80d, gga09c, gga05c, gga07c, ggb12c, ggb13c, ggb14c, ggb05f, ggb11f, gga99i, 1 from ggm02 b with (nolock), ggm01 a with (nolock), gam26 z, gdm01 c where a.gga00c = b.ggb00c and a.gga01c = b.ggb01c and a.gga00c = z.gaz02c and z.gaz01c = @compid and b.ggb00c = c.gda00c and b.ggb03c = c.gda01c and (gda13c = '228' or gda13c = '229' or gda13c = '230') and b.ggb27c<>'9' and isnull(ggb11f, 0)>0 --产品业绩 insert gcm24(gcx00c, gcx01c, gcx02c, gcx03d, gcx04c, gcx05c, gcx06c, gcx07c, gcx08f, gcx09c, gcx10f, gcx11c, gcx12f, gcx20f, gcx19f, gcx99i, gcx22i) select gga00c, 'gx', gga01c, gga80d, gga09c, gga05c, gga07c, ggc12c, ggc14f, ggc15c, ggc16f, ggc17c, ggc18f, ggc05f, ggc11f, gga99i, 2 from ggm03 b with (nolock), ggm01 a with (nolock), gam26 z where a.gga00c = b.ggc00c and a.gga01c = b.ggc01c and a.gga00c = z.gaz02c and z.gaz01c = @compid and (b.ggc27c = '1' or b.ggc27c = '6') and isnull(ggc11f, 0)>0 update a set a.gcx06c = b.gca02c from gcm24 a, gcm01 b, gam26 z where a.gcx00c = b.gca00c and a.gcx04c = b.gca01c and gcx03d >= @fromdate and gcx03d <= @todate and a.gcx00c = z.gaz02c and z.gaz01c = @compid and isnull(gcx06c, '') = '' end GO /****** Object: StoredProcedure [dbo].[upg_update_gom01] Script Date: 06/12/2017 06:20:49 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_update_gom01] ( @compid varchar(10), -- 公司编号 @BillNo varchar(20), -- 测试单号 @CardNo varchar(20) -- 会员卡号 ) as ---Write By WUXM begin update gom01 set goa11i = 0 where goa00c = @compid and goa01c <> @BillNo and goa03c = @CardNo end GO /****** Object: StoredProcedure [dbo].[upg_public_clean] Script Date: 06/12/2017 06:20:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_public_clean] ( @tablename varchar(20)) as begin if @tablename = 'gdm09' begin --删除项目/产品折扣设定(时间设定)的三级明细垃圾数据 delete from gdm09 where not exists ( select gdh00c from gdm08 where gdh00c = gdi00c and gdh01c = gdi01c and gdh02c = gdi02c and gdh03f = gdi03f union select gdg00c from gdm07 where gdg00c = gdi00c and gdg01c = gdi01c and gdg02c = gdi02c and gdg10f = gdi03f ) end if @tablename = 'gam15' begin --删除会员卡类别设定的(变更收费)三级明细的垃圾数据 delete from gam15 where not exists ( select gal02f from gam11 where gal00c = gao00c and gal01c = gao01c and gal02f = gao02f ) end end GO /****** Object: StoredProcedure [dbo].[upg_query_sum_lc] Script Date: 06/12/2017 06:20:30 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_query_sum_lc] ( @compid varchar(10), @fromdate varchar(10), @todate varchar(10), @xmdl varchar(10), --项目大类 @tjfl varchar(10) --统计分类 ) as begin select a.gga09c cardid, --会员卡号 c.gba03c [name], --姓名 case c.gba14i when 0 then '女' else '男' end sex, --性别 c.gba17d date, --生日 count(1) cs, --次数 sum(b.ggb05f) num, --疗程数量 sum(b.ggb11f) jine -- 金额 from ggm01 as a with(nolock) inner join ggm02 as b with(nolock) on a.gga00c = b.ggb00c and a.gga01c = b.ggb01c inner join gbm01 as c with(nolock) on c.gba23c = a.gga09c where a.gga00c in (select gaz02c from gam26 where gaz01c = '001' ) and isnull(a.gga93c,'') <> '' and a.gga94d between @fromdate and @todate and b.ggb47i <> -1 --疗程判断 and (@xmdl = '*' or ggb03c in (select gda01c from gdm01 where gda04c = @xmdl)) and (@tjfl = '*' or ggb03c in (select gda01c from gdm01 where gda13c = @tjfl)) group by a.gga09c,c.gba03c,c.gba14i,c.gba17d end GO /****** Object: StoredProcedure [dbo].[reminder_treatment_project] Script Date: 06/12/2017 06:19:20 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[reminder_treatment_project] @compid varchar(10), @remainder int, @cardtype varchar(5), @project varchar(20), @startdate varchar(8), @enddate varchar(8) as begin select distinct gcf00c, gba01c, gcf01c, cast(gba03c as varchar(20)) as gba03c, gcf23i, gcf02c, d.gda03c, gcf12d, gcf07f, gcf10f, gba07c, gba08c, gba05c from gcm06 a with (nolock), gbm01 b with (nolock), gcm01 c with (nolock), gdm01 d with (nolock) where a.gcf00c = b.gba00c and a.gcf01c = b.gba23c and a.gcf01c = c.gca01c and a.gcf00c = c.gca00c and c.gca13d = @compid and a.gcf00c = d.gda00c and a.gcf02c = d.gda01c and (isnull(@cardtype, '') = '' or c.gca02c = @cardtype) and (isnull(@project, '') = '' or a.gcf02c = @project) and (isnull(@remainder, -1) = -1 or gcf07f <= @remainder) and (isnull(@startdate, '') = '' or a.gcf12d >= @startdate) and (isnull(@enddate, '') = '' or a.gcf12d <= @enddate) end GO /****** Object: StoredProcedure [dbo].[upg_insert_notify_content] Script Date: 06/12/2017 06:20:16 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_insert_notify_content]( @compid varchar(10), -- 公司别 @billid varchar(20), -- 单号 @cardid varchar(20), -- 会员卡号 @principal varchar(20), -- 负责人 @objtype int, -- 类型 1 项目 2 产品 @objid varchar(20), -- 项目代码或产品代码 @ddate varchar(8)) -- 本次消费日期 as begin declare @tdate datetime declare @notifydate varchar(8) declare @content varchar(128) declare @interval int if substring(@cardid, 1, 2) = '散客' return if @objtype = 1 begin -- 项目 select @interval = gda20i from gdm01 where gda00c = @compid and gda01c = @objid end else if @objtype = 2 begin --产品 select @interval = gfa27i from gfm01 where gfa00c = @compid and gfa01c = @objid end if (isnull(@interval, 0) < 1) return select @tdate = convert(datetime, @ddate) select @tdate = dateadd(day, @interval, @tdate) select @notifydate = convert(varchar(8), @tdate, 112) --如果通知日期已经超过当前,则不要通知了 declare @curdate varchar(20), @time varchar(20) exec upg_getcurrdate @curdate output, @time output if @notifydate<@curdate return declare @nname varchar(40) declare @objname varchar(40) declare @mobile varchar(20) declare @gender int select @nname = gba03c, @mobile = gba08c, @gender = gba14i from gbm01 with (nolock) where gba00c = @compid and gba23c = @cardid if isnull(@mobile, '') = '' return if @gender = 0 select @content = @nname + '女士您好, ' else select @content = @nname + '先生您好, ' if @objtype = 1 begin select @objname = gda03c from gdm01 where gda00c = @compid and gda01c = @objid select @content = @content + '按疗程安排, 建议您今天来店做' + @objname end else begin select @objname = gfa03c from gfm01 where gfa00c = @compid and gfa01c = @objid select @content = @content + '本店新到产品' + @objname + ', 若有需要欢迎来店购买' end if not exists(select 1 from gam23 where gau00c = @compid and gau01c = '12' and gau03c = @cardid and gau11c = @billid and gau14c = @content) insert gam23(gau00c, gau01c, gau03c, gau05d, gau08c, gau11c, gau12c, gau14c, gau16d, gau17i) values (@compid, '12', @cardid, @notifydate, @mobile, @billid, @principal, @content, @ddate, 0) declare @max_idd float select @max_idd = max(gxc01f) + 1 from gxm03 with (nolock) where gxc00c = @compid select @max_idd = isnull(@max_idd, 1) --101代表是消费通知短信,如果此号码相同内容已经存在,则不要再通知了 if not exists(select 1 from gxm03 where gxc09c = @mobile and gxc02f = 101 and gxc05d = @notifydate and gxc10c = @content) begin insert gxm03(gxc00c, gxc01f, gxc02f, gxc03c, gxc04c, gxc05d, gxc09c, gxc10c) values(@compid, @max_idd, 101, '01', '02', @notifydate, @mobile, @content) end end GO /****** Object: StoredProcedure [dbo].[upg_refresh_hem01] Script Date: 06/12/2017 06:20:34 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_refresh_hem01](@compid varchar(10)) as begin delete a from hem01 a where a.hea00c = @compid and a.hea01c not in ( select b.gda01c from gdm01 b where a.hea00c = b.gda00c) delete a from hem02 a where a.heb00c = @compid and a.heb02c not in ( select b.gda01c from gdm01 b where a.heb00c = b.gda00c) create table #hem01_1 ( compid varchar(10) not null, itemid varchar(20) not null ) create table #hem02_1 ( compid varchar(10) not null, itemid varchar(20) not null ) insert #hem01_1 select gda00c, gda01c from gdm01 where gda00c = @compid and isnull(gda01c, '') <> '' insert #hem02_1 select gda00c, gda01c from gdm01 where gda00c = @compid and isnull(gda01c, '')<>'' delete a from #hem01_1 a, hem01 b where a.compid = b.hea00c and a.itemid = b.hea01c and a.compid = @compid delete a from #hem02_1 a, hem02 b where a.compid = b.heb00c and a.itemid = b.heb02c and a.compid = @compid insert hem01(hea00c, hea01c, hea12f, hea13f, hea18i, hea24f, hea02i, hea03f, hea04i, hea05f, hea06i, hea07f, hea08i, hea09f, hea10i, hea11f, hea14i, hea15f, hea16i, hea17f, hea20i, hea21f, hea22i, hea23f, hea25i, hea26f, hea27i, hea28f) select distinct compid, itemid, 0, 1, 1, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1 from #hem01_1 insert hem01(hea00c, hea01c, hea12f, hea13f, hea18i, hea19i, hea24f, hea02i, hea03f, hea04i, hea05f, hea06i, hea07f, hea08i, hea09f, hea10i, hea11f, hea14i, hea15f, hea16i, hea17f, hea20i, hea21f, hea22i, hea23f, hea25i, hea26f, hea27i, hea28f) select distinct compid, itemid, 0, 1, 2, 1, 1, 4, 0, 4, 0, 4, 0, 4, 0, 4, 0, 4, 0, 4, 0, 4, 0, 4, 0, 4, 0, 4, 0 from #hem01_1 insert hem02(heb00c, heb01c, heb02c) select distinct compid, haa01c, itemid from #hem02_1 a, ham01 b where a.compid = b.haa00c and isnull(haa01c, '')<>'' and isnull(itemid, '')<>'' drop table #hem02_1 drop table #hem01_1 end GO /****** Object: StoredProcedure [dbo].[upg_copy_hem01_payment] Script Date: 06/12/2017 06:19:47 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_copy_hem01_payment] ( @compid varchar(10), @fatype varchar(20), -- 从哪一个方案编号复制 @itemCode varchar(20), -- 从哪一个方案编号的项目编号 @pay varchar(10) ) as begin set nocount on -- 当前的事务数 declare @__transaction_count int set @__transaction_count = @@trancount -- try...catch处理 begin try -- a.开启事务,或者设置事务保存点 if @__transaction_count = 0 begin tran else save tran __tran_SavePoint declare @gda01c varchar(20) declare cur_gdm01 cursor for select gda01c from gdm01 with(nolock) where gda00c = @compid and gda01c <> @itemCode open cur_gdm01 fetch cur_gdm01 into @gda01c while @@fetch_status = 0 begin delete hem01 where hea00c = @compid and hea33c = @fatype and hea01c = @gda01c and hea34c = @pay insert hem01(hea00c, hea01c, hea02i, hea03f, hea04i, hea05f, hea06i, hea07f, hea08i, hea09f, hea10i, hea11f, hea12f, hea13f, hea14i, hea15f, hea16i, hea17f, hea18i, hea19i, hea20i, hea21f, hea22i, hea23f, hea24f, hea25i, hea26f, hea27i, hea28f, hea29i, hea30i, hea31i, hea32i, hea33c, hea34c ) select @compid, @gda01c, hea02i, hea03f, hea04i, hea05f, hea06i, hea07f, hea08i, hea09f, hea10i, hea11f, hea12f, hea13f, hea14i, hea15f, hea16i, hea17f, hea18i, hea19i, hea20i, hea21f, hea22i, hea23f, hea24f, hea25i, hea26f, hea27i, hea28f, hea29i, hea30i, hea31i, hea32i, @fatype, @pay from hem01 where hea00c = @compid and hea33c = @fatype and hea01c = @itemCode and hea34c = @pay fetch cur_gdm01 into @gda01c end close cur_gdm01 deallocate cur_gdm01 -- 提交事务 -- 有可提交的事务, 并且事务是在当前模块中开启的情况下, 才提交事务 if xact_state() = 1 and @__transaction_count = 0 commit -- 为了防止TRY 中有遗漏的事务处理, 可以在TRY 模拟的结束部分做最终的判断 if @__transaction_count = 0 begin if xact_state() = -1 rollback tran else begin while @@trancount > 0 commit tran end end end try begin catch -- 在CATCH 模块, 应该首先处理事务 if xact_state() <> 0 begin if @__transaction_count = 0 rollback tran -- xact_state 为-1 时, 不能回滚到事务保存点, 这种情况留给外层调用者做统一的事务回滚 -- 通过@@trancount > @__transaction_count 的判断, 即使在TRY 模块中没有设置事务保存点的情况下跳到此步骤, 也不会出错 else if xact_state() = 1 and @@trancount > @__transaction_count rollback tran __tran_SavePoint end declare @SP_Parameters varchar(max) set @SP_Parameters = '@compid = ' + isnull(@compid, '') + '@fatype = ' + isnull(@fatype, '') + '@itemCode = ' + isnull(@itemCode, '') + '@pay = ' + isnull(@pay, '') insert into SP_TranLog(SPName, SPTime, SPParameter) values('upg_copy_hem01_payment', getdate(), @SP_Parameters) return -1 end catch end GO /****** Object: StoredProcedure [dbo].[upg_re_updatedepart_all_must_by_item] Script Date: 06/12/2017 06:20:31 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_re_updatedepart_all_must_by_item]( @compid varchar(10), @fromdate varchar(10), @todate varchar(10)) as begin --update ggm02,用第一员工来判断 --先设置为空 update a set a.ggb81c='' from ggm02 a,ggm01 b where a.ggb00c = b.gga00c and a.ggb01c = b.gga01c and b.gga00c = @compid and b.gga02d>=@fromdate and b.gga02d<=@todate --设置项目的部门 update a set a.ggb81c=b.gda02c from ggm02 a,gdm01 b,ggm01 c where a.ggb00c = c.gga00c and a.ggb01c = c.gga01c and c.gga00c = @compid and c.gga02d>=@fromdate and c.gga02d<=@todate and a.ggb00c=b.gda00c and a.ggb03c=b.gda01c and isnull(b.gda02c,'')<>'' --update ggm05 --先设置为空 update a set a.gge81c='' from ggm05 a,ggm01 c where a.gge00c = c.gga00c and a.gge01c = c.gga01c and c.gga00c = @compid and c.gga02d>=@fromdate and c.gga02d<=@todate update a set a.gge81c=b.gda02c from ggm05 a,gdm01 b,ggm01 c where a.gge00c = c.gga00c and a.gge01c = c.gga01c and c.gga00c = @compid and c.gga02d>=@fromdate and c.gga02d<=@todate and a.gge00c =b.gda00c and a.gge14c = b.gda01c and isnull(b.gda02c,'')<>'' end GO /****** Object: StoredProcedure [dbo].[upg_insert_ggm06_liu] Script Date: 06/12/2017 06:20:15 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_insert_ggm06_liu] ( @compid varchar(10), @billid varchar(20), -- 收银单号 @bigname varchar(20), @bigdeg int, @midname varchar(20), @middeg int, @smaname varchar(20), @smadeg int, @smallarname varchar(20), @smallardeg int, @bigname2 varchar(20), @bigdeg2 int, @midname2 varchar(20), @middeg2 int, @smaname2 varchar(20), @smadeg2 int, @smallarname2 varchar(20), @smallardeg2 int) as begin declare @bigdesc varchar(120) declare @middesc varchar(120) declare @smadesc varchar(120) declare @smallardesc varchar(120) declare @bigdesc2 varchar(120) declare @middesc2 varchar(120) declare @smadesc2 varchar(120) declare @smallardesc2 varchar(120) delete ggm06 where ggf00c = @compid and ggf01c = @billid select @bigdesc = @bigdesc + gda03c + ',' from ggm02 a, gdm01 b where a.ggb00c = b.gda00c and a.ggb03c = b.gda01c and ggb00c = @compid and ggb01c = @billid and ggb12c = @bigname select @middesc = @middesc + gda03c + ',' from ggm02 a, gdm01 b where a.ggb00c = b.gda00c and a.ggb03c = b.gda01c and ggb00c = @compid and ggb01c = @billid and ggb13c = @midname select @smadesc = @smadesc + gda03c + ',' from ggm02 a, gdm01 b where a.ggb00c = b.gda00c and a.ggb03c = b.gda01c and ggb00c = @compid and ggb01c = @billid and ggb14c = @smaname select @smallardesc = @smallardesc + gda03c + ',' from ggm02 a, gdm01 b where a.ggb00c = b.gda00c and a.ggb03c = b.gda01c and ggb00c = @compid and ggb01c = @billid and ggb14c = @smallarname select @bigdesc2 = @bigdesc2 + gda03c + ',' from ggm02 a, gdm01 b where a.ggb00c = b.gda00c and a.ggb03c = b.gda01c and ggb00c = @compid and ggb01c = @billid and ggb12c = @bigname2 select @middesc2 = @middesc2 + gda03c + ',' from ggm02 a, gdm01 b where a.ggb00c = b.gda00c and a.ggb03c = b.gda01c and ggb00c = @compid and ggb01c = @billid and ggb13c = @midname2 select @smadesc2 = @smadesc2 + gda03c + ',' from ggm02 a, gdm01 b where a.ggb00c = b.gda00c and a.ggb03c = b.gda01c and ggb00c = @compid and ggb01c = @billid and ggb14c = @smaname2 select @smallardesc2 = @smallardesc2 + gda03c + ',' from ggm02 a, gdm01 b where a.ggb00c = b.gda00c and a.ggb03c = b.gda01c and ggb00c = @compid and ggb01c = @billid and ggb14c = @smallarname2 insert ggm06(ggf00c, ggf01c, ggf02c, ggf03c, ggf04i, ggf05c, ggf06c, ggf07i, ggf08c, ggf09c, ggf10i, ggf20c, ggf21c, ggf22i, ggf11c, ggf12c, ggf13i, ggf14c, ggf15c, ggf16i, ggf17c, ggf18c, ggf19i, ggf23c, ggf24c, ggf25i) values(@compid, @billid, @bigname, @bigdesc, @bigdeg, @midname, @middesc, @middeg, @smaname, @smadesc, @smadeg, @smallarname, @smallardesc, @smallardeg, @bigname2, @bigdesc2, @bigdeg2, @midname2, @middesc2, @middeg2, @smaname2, @smadesc2, @smadeg2, @smallarname2, @smallardesc2, @smallardeg2) end GO /****** Object: StoredProcedure [dbo].[upg_get_split_ggm02_payment] Script Date: 06/12/2017 06:20:09 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_get_split_ggm02_payment] ( @compid varchar(10), -- 公司别 @cardid varchar(20), -- 卡号 @user varchar(10), -- 使用者 @proj varchar(20), -- 项目编号 @pay varchar(5) -- 支付方式 ) as begin declare @ismember int set @ismember = 1 if charindex(N'散客', @cardid) > 0 set @ismember = 0 declare @homecomp varchar(10) if @ismember = 1 begin select @homecomp = gca13d from gcm01 where gca01c = @cardid and gca00c = gca13d if @homecomp is null set @ismember = 0 end create table #result ( pay varchar(10) ) insert into #result(pay) select gsa02c from gsm02_3 with(nolock) where gsa01c = @user and ((@ismember = 0 and gsa07i = 1) or (@ismember = 1 and gsa03i = 1)) delete from #result where pay = '9' or pay = '#' or pay = '4' or pay = '2' or pay = '7' or (pay >= 'A' and pay <= 'ZZZ' and pay <> 'WX' and pay <> 'ZFB') declare @gda19i int select @gda19i = gda19i from gdm01 where gda00c = @compid and gda01c = @proj if @gda19i = 1 begin delete from #result where pay not in (select pay from pay_classify where comp = @compid and category = 1) end else if @ismember = 0 begin delete from #result where pay not in('0', '1', '3', '6', '8', 'ZFB', 'WX') and left(pay, 1) <> '$' end else if @ismember = 1 begin declare @ur034 varchar(20) select @ur034 = gab04c from gam02 where gab01c = @user and gab02c = '4' and gab03c = 'UR034' if @ur034 = '0' delete from #result where pay = '5' if @compid <> @homecomp delete from #result where pay = '&' declare @today varchar(10) select @today = convert(varchar(10), getdate(), 112) insert into #result(pay) select gsn01c from gcm03 with (nolock), gsm14 with (nolock) where gcc00c = @homecomp and gcc01c = @cardid and (isnull(gcc09d, '') = '' or gcc09d >= @today) and gcc06f > 0 and gcc03i > 0 and isnull(gsn02c, '') <> '' and gcc03i = gsn02c end delete from #result where pay = @pay or pay = '9' select * from #result drop table #result end GO /****** Object: StoredProcedure [dbo].[upg_insert_ggm06] Script Date: 06/12/2017 06:20:15 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_insert_ggm06] ( @compid varchar(10), @billid varchar(20), -- 收银单号 @bigname varchar(20), @bigdeg int, @midname varchar(20), @middeg int, @smaname varchar(20), @smadeg int, @bigname2 varchar(20), @bigdeg2 int, @midname2 varchar(20), @middeg2 int, @smaname2 varchar(20), @smadeg2 int, @fourname varchar(20), --四工评价 ADD BY LEIJI on 2012年11月13日 @fourdeg int, @fourname2 varchar(20), @fourdeg2 int ) as begin declare @bigdesc varchar(120) declare @middesc varchar(120) declare @smadesc varchar(120) declare @bigdesc2 varchar(120) declare @middesc2 varchar(120) declare @smadesc2 varchar(120) declare @fourdesc varchar(120) declare @fourdesc2 varchar(120) delete ggm06 where ggf00c = @compid and ggf01c = @billid select @bigdesc = @bigdesc + gda03c + ', ' from ggm02 a, gdm01 b where a.ggb00c = b.gda00c and a.ggb03c = b.gda01c and ggb00c = @compid and ggb01c = @billid and ggb12c = @bigname select @middesc = @middesc + gda03c + ', ' from ggm02 a, gdm01 b where a.ggb00c = b.gda00c and a.ggb03c = b.gda01c and ggb00c = @compid and ggb01c = @billid and ggb13c = @midname select @smadesc = @smadesc + gda03c + ', ' from ggm02 a, gdm01 b where a.ggb00c = b.gda00c and a.ggb03c = b.gda01c and ggb00c = @compid and ggb01c = @billid and ggb14c = @smaname select @bigdesc2 = @bigdesc2 + gda03c + ', ' from ggm02 a, gdm01 b where a.ggb00c = b.gda00c and a.ggb03c = b.gda01c and ggb00c = @compid and ggb01c = @billid and ggb12c = @bigname2 select @middesc2 = @middesc2 + gda03c + ', ' from ggm02 a, gdm01 b where a.ggb00c = b.gda00c and a.ggb03c = b.gda01c and ggb00c = @compid and ggb01c = @billid and ggb13c = @midname2 select @smadesc2 = @smadesc2 + gda03c + ', ' from ggm02 a, gdm01 b where a.ggb00c = b.gda00c and a.ggb03c = b.gda01c and ggb00c = @compid and ggb01c = @billid and ggb14c = @smaname2 select @fourdesc = @fourdesc + gda03c + ', ' from ggm02 a, gdm01 b where a.ggb00c = b.gda00c and a.ggb03c = b.gda01c and ggb00c = @compid and ggb01c = @billid and ggb26c = @fourname select @fourdesc2 = @fourdesc2 + gda03c + ', ' from ggm02 a, gdm01 b where a.ggb00c = b.gda00c and a.ggb03c = b.gda01c and ggb00c = @compid and ggb01c = @billid and ggb26c = @fourname2 insert ggm06(ggf00c, ggf01c, ggf02c, ggf03c, ggf04i, ggf05c, ggf06c, ggf07i, ggf08c, ggf09c, ggf10i, ggf11c, ggf12c, ggf13i, ggf14c, ggf15c, ggf16i, ggf17c, ggf18c, ggf19i, ggf20c, ggf21c, ggf22i, ggf23c, ggf24c, ggf25i) values(@compid, @billid, @bigname, @bigdesc, @bigdeg, @midname, @middesc, @middeg, @smaname, @smadesc, @smadeg, @bigname2, @bigdesc2, @bigdeg2, @midname2, @middesc2, @middeg2, @smaname2, @smadesc2, @smadeg2, @fourname, @fourdesc, @fourdeg, @fourname2, @fourdesc2, @fourdeg2) end GO /****** Object: StoredProcedure [dbo].[upg_proc_sale_stat] Script Date: 06/12/2017 06:20:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_proc_sale_stat] ( @compid varchar(10), -- 公司别 @fromdate varchar(20), -- 开始日期 @todate varchar(20), -- 截止日期 @fromproc varchar(20), -- 疗程项目 @toproc varchar(20), -- 疗程项目 @fromcard varchar(20), -- 会员卡号 --modify by limin -2014-12-01 @tocard varchar(20) -- 会员卡号 ) as begin declare @incompid varchar(10) select @incompid = @compid declare @payment_zp varchar(10) --支票的支付方式 declare @is_revenue varchar(10) -- 判断是否为收入项目 select @is_revenue = gsa02c from gsm01 where gsa00c = @compid and gsa01c = 'SP063' if @is_revenue = '1' begin select @payment_zp = '$' end else begin select @payment_zp = 'ZZ' -- 无此支付方式 end create table #tbl_proc_sale ( idd int identity not null, compid varchar(10) not null, procid varchar(20) not null, cardid varchar(20) not null, ddate varchar(8) null, btimes float null, stimes float null, procamt float null, billtype varchar(20) null, billid varchar(20) null, srv1 varchar(20) null, srv2 varchar(20) null, srv3 varchar(20) null, mem_name varchar(40) null, -- 会员姓名 cardnum float null, -- 卡数量 payment_way1 varchar(5) null, -- 支付方式1 payment_name1 varchar(40) null, -- 支付名称1 payment_way2 varchar(5) null, -- 支付方式2 payment_name2 varchar(40) null, -- 支付名称2 payment_way3 varchar(5) null, -- 支付方式3 payment_name3 varchar(40) null, -- 支付名称3 payment_way4 varchar(5) null, -- 支付方式4 payment_name4 varchar(40) null, -- 支付名称4 gch12i int null, --价格序号 ) declare @PK_Sql nvarchar(max) set @PK_Sql = ' alter table #tbl_proc_sale add constraint PK_#tbl_proc_sale_' + convert(varchar(10), cast(floor(rand() * 1000000) as int)) + ' primary key nonclustered(idd)' exec sp_executesql @PK_Sql create clustered index idx_proc_sale_01 on #tbl_proc_sale(compid, billtype, billid) if @fromdate = '*' or @fromdate = '' begin select @fromdate = '19900101', @todate = '20901231' end if @fromproc = '*' or @fromproc = '' begin select @fromproc = min(gda01c), @toproc = max(gda01c) from gdm01 with(nolock) where gda00c = @compid end declare @onlycash float select @onlycash = gsa03f from gsm01 with(nolock) where gsa00c = @compid and gsa01c = 'SP139' ---------------begin -----------add by lm 2015/02/09 if @onlycash = 2 -- 所有支付方式 begin -- 支付方式1 insert #tbl_proc_sale(compid, procid, cardid, ddate, btimes, stimes, procamt, billtype, billid, cardnum, payment_way1,gch12i) select compid = gch00c, procid = gch04c, cardid = gch03c, ddate = gch02d, btimes = gch05f, stimes = gch06f, procamt = gch18f, billtype = gch10c, billid = gch11c, isnull(gch14f, 0) , payment_way1 = gch17c,gch12i from gcm08 a with (nolock), gam26 z with(nolock) where gch00c = z.gaz02c and z.gaz01c = @incompid and gch02d >= @fromdate and gch02d <= @todate and (gch10c = 'gn' or gch10c = 'gz') -- 需要考虑购买产品赠送的疗程 and gch04c >= @fromproc and gch04c <= @toproc and isnull(gch17c,'') <> '' and (isnull(gch18f,0) > 0 or isnull(gch18f,0) < 0) order by gch04c, gch02d --支付方式2 insert #tbl_proc_sale(compid, procid, cardid, ddate, btimes, stimes, procamt, billtype, billid, cardnum, payment_way1,gch12i) select compid = gch00c, procid = gch04c, cardid = gch03c, ddate = gch02d, btimes = gch05f, stimes = gch06f, procamt =gch20f, billtype = gch10c, billid = gch11c, isnull(gch14f, 0) ,payment_way1 = gch19c,gch12i from gcm08 a with (nolock), gam26 z with(nolock) where gch00c = z.gaz02c and z.gaz01c = @incompid and gch02d >= @fromdate and gch02d <= @todate and (gch10c = 'gn' or gch10c = 'gz') -- 需要考虑购买产品赠送的疗程 and gch04c >= @fromproc and gch04c <= @toproc and isnull(gch19c,'')<>'' and (isnull(gch20f,0) > 0 or isnull(gch20f,0) < 0) order by gch04c, gch02d --支付方式3 insert #tbl_proc_sale(compid, procid, cardid, ddate, btimes, stimes, procamt, billtype, billid, cardnum, payment_way1,gch12i) select compid = gch00c, procid = gch04c, cardid = gch03c, ddate = gch02d, btimes = gch05f, stimes = gch06f, procamt =gch22f , billtype = gch10c, billid = gch11c, isnull(gch14f, 0) , payment_way1 = gch21c ,gch12i from gcm08 a with (nolock), gam26 z with(nolock) where gch00c = z.gaz02c and z.gaz01c = @incompid and gch02d >= @fromdate and gch02d <= @todate and (gch10c = 'gn' or gch10c = 'gz') -- 需要考虑购买产品赠送的疗程 and gch04c >= @fromproc and gch04c <= @toproc and isnull(gch21c,'')<>'' and (isnull(gch22f,0) > 0 or isnull(gch22f,0) < 0) order by gch04c, gch02d --支付方式4 insert #tbl_proc_sale(compid, procid, cardid, ddate, btimes, stimes, procamt, billtype, billid, cardnum, payment_way1,gch12i) select compid = gch00c, procid = gch04c, cardid = gch03c, ddate = gch02d, btimes = gch05f, stimes = gch06f, procamt =gch28f, billtype = gch10c, billid = gch11c, isnull(gch14f, 0) ,payment_way1 = gch27c,gch12i from gcm08 a with (nolock), gam26 z with(nolock) where gch00c = z.gaz02c and z.gaz01c = @incompid and gch02d >= @fromdate and gch02d <= @todate and (gch10c = 'gn' or gch10c = 'gz') -- 需要考虑购买产品赠送的疗程 and gch04c >= @fromproc and gch04c <= @toproc and isnull(gch27c,'')<>'' and (isnull(gch28f,0) > 0 or isnull(gch28f,0) < 0) order by gch04c, gch02d --支付方式4 insert #tbl_proc_sale(compid, procid, cardid, ddate, btimes, stimes, procamt, billtype, billid, cardnum, payment_way1,gch12i) select compid = gch00c, procid = gch04c, cardid = gch03c, ddate = gch02d, btimes = gch05f, stimes = gch06f, procamt =gch28f, billtype = gch10c, billid = gch11c, isnull(gch14f, 0) ,payment_way1 = gch27c,gch12i from gcm08 a with (nolock), gam26 z with(nolock) where gch00c = z.gaz02c and z.gaz01c = @incompid and gch02d >= @fromdate and gch02d <= @todate and (gch10c = 'gn' or gch10c = 'gz') -- 需要考虑购买产品赠送的疗程 and gch04c >= @fromproc and gch04c <= @toproc and isnull(gch17c,'')='' and isnull(gch18f,0) = 0 and isnull(gch19c,'')='' and isnull(gch20f,0) = 0 and isnull(gch21c,'')='' and isnull(gch22f,0) = 0 and isnull(gch27c,'')='' and isnull(gch28f,0) = 0 and isnull(gch06f,0) = 0 order by gch04c, gch02d --赠送的项目统计 -- 支付方式1 insert #tbl_proc_sale(compid, procid, cardid, ddate, btimes, stimes, procamt, billtype, billid, cardnum, payment_way1,gch12i) select compid = gch00c, procid = gch04c, cardid = gch03c, ddate = gch02d, btimes = gch05f, stimes = gch06f, procamt = gch18f, billtype = gch10c, billid = gch11c, isnull(gch14f, 0) , payment_way1 = gch17c,gch12i from gcm08 a with (nolock), gam26 z with(nolock) where gch00c = z.gaz02c and z.gaz01c = @incompid and gch02d >= @fromdate and gch02d <= @todate and (gch10c = 'gn' or gch10c = 'gz') -- 需要考虑购买产品赠送的疗程 and gch04c >= @fromproc and gch04c <= @toproc and gch06f > 0 and gch05f = 0 and isnull(gch18f,0) = 0 and isnull(gch20f,0) = 0 and isnull(gch22f,0) = 0 and isnull(gch28f,0) = 0 order by gch04c, gch02d end else --只包括现金类 begin --支付方式1 insert #tbl_proc_sale(compid, procid, cardid, ddate, btimes, stimes, procamt, billtype, billid, cardnum, payment_way1,gch12i) select compid = gch00c, procid = gch04c, cardid = gch03c, ddate = gch02d, btimes = gch05f, stimes = gch06f, procamt = gch18f, billtype = gch10c, billid = gch11c, isnull(gch14f, 0) , payment_way1 = gch17c,gch12i from gcm08 a with (nolock), gam26 z with(nolock) where gch00c = z.gaz02c and z.gaz01c = @incompid and gch02d >= @fromdate and gch02d <= @todate and (gch10c = 'gn' or gch10c = 'gz') -- 需要考虑购买产品赠送的疗程 and gch04c >= @fromproc and gch04c <= @toproc and gch17c in (select pay from pay_classify with(nolock) where comp = @incompid and category = '1') or gch17c = @payment_zp --modify by lm 2015-05-06 修改支付方式判断 and isnull(gch17c,'')<>'' and (isnull(gch18f,0) > 0 or isnull(gch18f,0) < 0) order by gch04c, gch02d --支付方式2 insert #tbl_proc_sale(compid, procid, cardid, ddate, btimes, stimes, procamt, billtype, billid, cardnum, payment_way1,gch12i) select compid = gch00c, procid = gch04c, cardid = gch03c, ddate = gch02d, btimes = gch05f, stimes = gch06f, procamt =gch20f, billtype = gch10c, billid = gch11c, isnull(gch14f, 0) , payment_way1 = gch19c ,gch12i from gcm08 a with (nolock), gam26 z with(nolock) where gch00c = z.gaz02c and z.gaz01c = @incompid and gch02d >= @fromdate and gch02d <= @todate and (gch10c = 'gn' or gch10c = 'gz') -- 需要考虑购买产品赠送的疗程 and gch04c >= @fromproc and gch04c <= @toproc and gch19c in (select pay from pay_classify with(nolock) where comp = @incompid and category = '1') or gch19c = @payment_zp --modify by lm 2015-05-06 修改支付方式判断 and isnull(gch19c,'')<>'' and (isnull(gch20f,0) > 0 or isnull(gch20f,0) < 0) order by gch04c, gch02d --支付方式3 insert #tbl_proc_sale(compid, procid, cardid, ddate, btimes, stimes, procamt, billtype, billid, cardnum, payment_way1,gch12i) select compid = gch00c, procid = gch04c, cardid = gch03c, ddate = gch02d, btimes = gch05f, stimes = gch06f, procamt =gch22f , billtype = gch10c, billid = gch11c, isnull(gch14f, 0) , payment_way1 = gch21c,gch12i from gcm08 a with (nolock), gam26 z with(nolock) where gch00c = z.gaz02c and z.gaz01c = @incompid and gch02d >= @fromdate and gch02d <= @todate and (gch10c = 'gn' or gch10c = 'gz') -- 需要考虑购买产品赠送的疗程 and gch04c >= @fromproc and gch04c <= @toproc and gch21c in (select pay from pay_classify with(nolock) where comp = @incompid and category = '1') or gch21c = @payment_zp --modify by lm 2015-05-06 修改支付方式判断 and isnull(gch21c,'')<>'' and (isnull(gch22f,0) > 0 or isnull(gch22f,0) < 0) order by gch04c, gch02d --支付方式4 insert #tbl_proc_sale(compid, procid, cardid, ddate, btimes, stimes, procamt, billtype, billid, cardnum, payment_way1,gch12i) select compid = gch00c, procid = gch04c, cardid = gch03c, ddate = gch02d, btimes = gch05f, stimes = gch06f, procamt =gch28f, billtype = gch10c, billid = gch11c, isnull(gch14f, 0) ,payment_way1 = gch27c ,gch12i from gcm08 a with (nolock), gam26 z with(nolock) where gch00c = z.gaz02c and z.gaz01c = @incompid and gch02d >= @fromdate and gch02d <= @todate and (gch10c = 'gn' or gch10c = 'gz') -- 需要考虑购买产品赠送的疗程 and gch04c >= @fromproc and gch04c <= @toproc and gch27c in (select pay from pay_classify with(nolock) where comp = @incompid and category = '1') or gch27c = @payment_zp --modify by lm 2015-05-06 修改支付方式判断 and isnull(gch27c,'')<>'' and (isnull(gch28f,0) > 0 or isnull(gch28f,0) < 0) order by gch04c, gch02d --支付方式4 insert #tbl_proc_sale(compid, procid, cardid, ddate, btimes, stimes, procamt, billtype, billid, cardnum, payment_way1,gch12i) select compid = gch00c, procid = gch04c, cardid = gch03c, ddate = gch02d, btimes = gch05f, stimes = gch06f, procamt =gch28f, billtype = gch10c, billid = gch11c, isnull(gch14f, 0) ,payment_way1 = gch27c,gch12i from gcm08 a with (nolock), gam26 z with(nolock) where gch00c = z.gaz02c and z.gaz01c = @incompid and gch02d >= @fromdate and gch02d <= @todate and (gch10c = 'gn' or gch10c = 'gz') -- 需要考虑购买产品赠送的疗程 and gch04c >= @fromproc and gch04c <= @toproc and isnull(gch17c,'')='' and isnull(gch18f,0) = 0 and isnull(gch19c,'')='' and isnull(gch20f,0) = 0 and isnull(gch21c,'')='' and isnull(gch22f,0) = 0 and isnull(gch27c,'')='' and isnull(gch28f,0) = 0 and isnull(gch06f,0) = 0 order by gch04c, gch02d end --支付方式1 insert #tbl_proc_sale(compid, procid, cardid, ddate, btimes, stimes, procamt, billtype, billid, cardnum, payment_way1,gch12i) select compid = gch00c, procid = gch04c, cardid = gch03c, ddate = gch02d, btimes = 0-gch08f, stimes = 0, procamt = 0-gch09f, billtype = gch10c, billid = gch11c, isnull(gch14f, 0), payment_way1 = gch17c ,gch12i from gcm08 a with (nolock), gam26 z with(nolock) where gch00c = z.gaz02c and z.gaz01c = @incompid and gch02d >= @fromdate and gch02d <= @todate and (gch10c = 'pj') and isnull(gch17c,'')<>'' and gch04c >= @fromproc and gch04c <= @toproc and isnull(gch18f,0) > 0 order by gch04c, gch02d --支付方式2 insert #tbl_proc_sale(compid, procid, cardid, ddate, btimes, stimes, procamt, billtype, billid, cardnum, payment_way1,gch12i) select compid = gch00c, procid = gch04c, cardid = gch03c, ddate = gch02d, btimes = 0-gch08f, stimes = 0, procamt = 0-gch09f, billtype = gch10c, billid = gch11c, isnull(gch14f, 0), payment_way1 = gch19c ,gch12i from gcm08 a with (nolock), gam26 z with(nolock) where gch00c = z.gaz02c and z.gaz01c = @incompid and gch02d >= @fromdate and gch02d <= @todate and (gch10c = 'pj') and isnull(gch19c,'')<>'' and gch04c >= @fromproc and gch04c <= @toproc and isnull(gch20f,0) > 0 order by gch04c, gch02d --支付方式3 insert #tbl_proc_sale(compid, procid, cardid, ddate, btimes, stimes, procamt, billtype, billid, cardnum, payment_way1,gch12i) select compid = gch00c, procid = gch04c, cardid = gch03c, ddate = gch02d, btimes = 0-gch08f, stimes = 0, procamt = 0-gch09f, billtype = gch10c, billid = gch11c, isnull(gch14f, 0), payment_way1 = gch21c,gch12i from gcm08 a with (nolock), gam26 z with(nolock) where gch00c = z.gaz02c and z.gaz01c = @incompid and gch02d >= @fromdate and gch02d <= @todate and (gch10c = 'pj') and isnull(gch21c,'')<>'' and isnull(gch22f,0) > 0 and gch04c >= @fromproc and gch04c <= @toproc order by gch04c, gch02d --支付方式4 insert #tbl_proc_sale(compid, procid, cardid, ddate, btimes, stimes, procamt, billtype, billid, cardnum, payment_way1,gch12i) select compid = gch00c, procid = gch04c, cardid = gch03c, ddate = gch02d, btimes = 0-gch08f, stimes = 0, procamt = 0-gch09f, billtype = gch10c, billid = gch11c, isnull(gch14f, 0), payment_way1 = gch27c ,gch12i from gcm08 a with (nolock), gam26 z with(nolock) where gch00c = z.gaz02c and z.gaz01c = @incompid and gch02d >= @fromdate and gch02d <= @todate and (gch10c = 'pj') and isnull(gch27c,'')<>'' and isnull(gch28f,0) > 0 and gch04c >= @fromproc and gch04c <= @toproc order by gch04c, gch02d --------------end ----------- update a set a.srv1 = b.gna20c, a.srv2 = b.gna28c, a.srv3 = b.gna30c from #tbl_proc_sale a, gnm01 b with (nolock) where a.compid = b.gna00c and a.billid = b.gna01c and a.billtype = 'gn' update a set a.srv1 = b.gcl09c, a.srv2 = b.gcl17c, a.srv3 = b.gcl19c from #tbl_proc_sale a, gcm10 b with (nolock) where a.compid = b.gcl00c and a.billid = b.gcl01c and (a.billtype = 'gz' or a.billtype = 'pj') --购买产品赠送服务的 -- update a -- set a.srv1 = b.ggc12c, a.srv2 = b.ggc15c, a.srv3 = b.ggc17c -- from #tbl_proc_sale a, ggm03 b with (nolock) -- where a.compid = b.ggc00c -- and a.billid = b.ggc01c -- and (a.billtype = 'gx') update #tbl_proc_sale set billtype = '更换疗程' where billtype = 'pj' ----更改每个单号的疗程次数------ modify by lm 2015/04/22 create table #tbl_proc_camt ( compid varchar(10) not null, procid varchar(20) not null, --疗程ID procamt float null, --套餐金额 billtype varchar(20) null, billid varchar(20) null, payway varchar(5) null, gch16i int null --疗程中的序号 ) create table #tbl_proc_camt_2 ( compid varchar(10) not null, procid varchar(20) not null, --疗程ID procamt float null, --疗程金额 billtype varchar(20) null, billid varchar(20) null, payway varchar(5) null, gch16i int null --疗程中的序号 ) ----begin数据新增----- --支付方式1 insert #tbl_proc_camt(compid, procid, procamt, billtype,billid,payway,gch16i) select compid = gch00c, procid = gch04c,procamt = gch18f, billtype = gch10c, billid = gch11c,gch17c,gch16i from gcm08 a with (nolock), gam26 z with(nolock) where gch00c = z.gaz02c and z.gaz01c = @incompid and gch02d >= @fromdate and gch02d <= @todate and (gch10c = 'gn' or gch10c = 'gz') -- 需要考虑购买产品赠送的疗程 and gch04c >= @fromproc and gch04c <= @toproc and isnull(gch05f, 0) + isnull(gch06f, 0)>0 and isnull(gch17c,'')<>'' and isnull(gch18f,0) > 0 order by gch04c, gch02d --支付方式2 insert #tbl_proc_camt(compid, procid, procamt, billtype,billid,payway,gch16i) select compid = gch00c, procid = gch04c, procamt = gch20f, billtype = gch10c, billid = gch11c,gch19c,gch16i from gcm08 a with (nolock), gam26 z with(nolock) where gch00c = z.gaz02c and z.gaz01c = @incompid and gch02d >= @fromdate and gch02d <= @todate and (gch10c = 'gn' or gch10c = 'gz') -- 需要考虑购买产品赠送的疗程 and gch04c >= @fromproc and gch04c <= @toproc and isnull(gch05f, 0) + isnull(gch06f, 0)>0 and isnull(gch19c,'')<>'' and isnull(gch20f,0) > 0 order by gch04c, gch02d --支付方式3 insert #tbl_proc_camt(compid, procid, procamt, billtype,billid,payway,gch16i) select compid = gch00c, procid = gch04c, procamt = gch22f, billtype = gch10c, billid = gch11c,gch21c,gch16i from gcm08 a with (nolock), gam26 z with(nolock) where gch00c = z.gaz02c and z.gaz01c = @incompid and gch02d >= @fromdate and gch02d <= @todate and (gch10c = 'gn' or gch10c = 'gz') -- 需要考虑购买产品赠送的疗程 and gch04c >= @fromproc and gch04c <= @toproc and isnull(gch05f, 0) + isnull(gch06f, 0)>0 and isnull(gch21c,'')<>'' and isnull(gch22f,0) > 0 order by gch04c, gch02d --支付方式4 insert #tbl_proc_camt(compid, procid, procamt, billtype,billid,payway,gch16i) select compid = gch00c, procid = gch04c, procamt = gch28f, billtype = gch10c, billid = gch11c,gch27c,gch16i from gcm08 a with (nolock), gam26 z with(nolock) where gch00c = z.gaz02c and z.gaz01c = @incompid and gch02d >= @fromdate and gch02d <= @todate and (gch10c = 'gn' or gch10c = 'gz') -- 需要考虑购买产品赠送的疗程 and gch04c >= @fromproc and gch04c <= @toproc and isnull(gch05f, 0) + isnull(gch06f, 0)>0 and isnull(gch27c,'')<>'' and isnull(gch28f,0) > 0 order by gch04c, gch02d ---------插入总金额---- insert #tbl_proc_camt_2 (compid,procid,procamt,billtype,billid,gch16i) select compid,procid,sum(procamt),billtype,billid,gch16i from #tbl_proc_camt group by compid,procid,billtype,billid,gch16i -------总金额赋值------- update a set a.procamt=b.procamt from #tbl_proc_camt a,#tbl_proc_camt_2 b where a.compid=b.compid and a.procid=b.procid and a.billtype=b.billtype and a.billid=b.billid ---end---------------- ----begin疗程次数赋值---- update b set b.btimes=dbo.fun_divide_default(b.procamt*b.btimes,a.procamt,0) from #tbl_proc_camt a ,#tbl_proc_sale b where a.compid=b.compid and a.procid=b.procid and a.billtype=b.billtype and a.billid=b.billid and a.payway=b.payment_way1 ---end------------ update a set a.billtype = b.gsf03c from #tbl_proc_sale a, gsm06 b with(nolock) where a.billtype = b.gsf02c and b.gsf01c = '1' update a set a.mem_name = b.gba03c from #tbl_proc_sale a left join gbm01 b with(nolock) --modify by lm 20150629 on a.cardid = b.gba23c and b.gba00c = @compid ------------------------------- 下面是增加支付方式------------------------- update a set a.payment_name1 = b.gsb03c from #tbl_proc_sale a, gsm02 b with(nolock) where a.compid = b.gsb00c and a.payment_way1 = b.gsb02c and b.gsb01c = '3' update a set a.payment_name2 = b.gsb03c from #tbl_proc_sale a, gsm02 b with(nolock) where a.compid = b.gsb00c and a.payment_way2 = b.gsb02c and b.gsb01c = '3' update a set a.payment_name3 = b.gsb03c from #tbl_proc_sale a, gsm02 b with(nolock) where a.compid = b.gsb00c and a.payment_way3 = b.gsb02c and b.gsb01c = '3' update a set a.payment_name4 = b.gsb03c from #tbl_proc_sale a, gsm02 b with(nolock) where a.compid = b.gsb00c and a.payment_way4 = b.gsb02c and b.gsb01c = '3' update a set a.ddate = b.gcl02d from #tbl_proc_sale a, gcm10 b with(nolock) where a.compid = b.gcl00c and a.billid = b.gcl01c and a.billtype = 'gz' select isnull(idd,'') idd,isnull(compid,'') compid,isnull(procid,'') procid,isnull(cardid,'') cardid,isnull(ddate,'') ddate, isnull(btimes,0) btimes,isnull(stimes,0) stimes,isnull(procamt,0) procamt,isnull(billtype,'') billtype,isnull(billid,'') billid, isnull(srv1,'') srv1,isnull(srv2,'') srv2,isnull(srv3,'') srv3,isnull(mem_name,'') mem_name,isnull(cardnum,0) cardnum, isnull(payment_way1,'') payment_way1,isnull(payment_name1,'') payment_name1,isnull(payment_way2,'') payment_way2, isnull(payment_name2,'') payment_name2,isnull(payment_way3,'') payment_way3,isnull(payment_name3,'') payment_name3, isnull(payment_way4,'') payment_way4,isnull(payment_name4,'') payment_name4,gch12i from #tbl_proc_sale where cardid between @fromcard and @tocard or @fromcard='*' --modify by limin 2014/12/11 --order by procid, ddate union select '' idd,'' compid,'' procid,'' cardid,'' ddate, 0 btimes,0 stimes,sum(procamt) procamt,'' billtype,'paysum' billid, '' srv1,'' srv2,'' srv3,'' mem_name,0 cardnum, '' payment_way1, isnull(payment_name1,''),'', '' payment_name2,'' payment_way3,'' payment_name3, '' payment_way4,'' payment_name4,'' gch12i from #tbl_proc_sale where cardid between @fromcard and @tocard or @fromcard='*' --modify by limin 2014/12/11 group by payment_way1,payment_name1 order by procid desc, ddate drop table #tbl_proc_camt drop table #tbl_proc_camt_2 drop table #tbl_proc_sale end GO /****** Object: StoredProcedure [dbo].[upg_clean_prj_consume] Script Date: 06/12/2017 06:19:41 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_clean_prj_consume] ( @compid varchar(10) ) as begin delete gfm05 where gfe00c = @compid and gfe01c not in (select gda01c from gdm01 where gda00c = @compid) delete gfm05 where gfe00c = @compid and gfe02c not in (select gfa01c from gfm01 where gfa00c = @compid) end GO /****** Object: StoredProcedure [dbo].[upg_consume_info] Script Date: 06/12/2017 06:19:45 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_consume_info] ( @compid varchar(10), -- 公司别 @billtype varchar(20), -- 单据别 'gx'收银单 'gl'出院单 @billno varchar(20), -- 消费单号 @money float, -- 欠款金额 @account varchar(5) -- 欠款账户 ) as --WUXM 2005/07/20 begin create table #consume ( idd int identity not null, billno varchar(20) null, -- 消费单号 cardno varchar(20) null, -- 会员卡号 client varchar(40) null, -- 会员名称 date varchar(8) null, -- 消费日期 time varchar(6) null, -- 时间 project varchar(1024) null, -- 项目 congoods varchar(1024) null, -- 消耗产品 buygoods varchar(1024) null, -- 购买产品 money float null, -- 欠款额 account varchar(5) null, -- 欠款账户 primary key(idd) ) -- 如果是出院欠款,则需要把出院单号转为收银单号 if @billtype = 'gl' begin select @billno = gga01c from ggm01 with (nolock) where gga00c = @compid and gga30c = @billno if isnull(@billno, '') = '' return end declare @cardno varchar(20) declare @client varchar(40) declare @date varchar(8) declare @time varchar(6) declare @project varchar(30) declare @name varchar(1024) -- 用于存储项目名称,消耗产品,以及购买产品的信息 declare @saler varchar(20) -- 插入消费单号 insert #consume(billno, money, account) values (@billno, @money, @account) -- 插入该消费单号的会员卡号和会员名称 select @cardno = gca01c, @client = gba03c, @date = gga80d, @time = gga04t from gbm01 with (nolock), gcm01 with (nolock), ggm01 with (nolock) where gba00c = gca00c and gca00c = gca13d and gca01c = gga09c and gca01c = gba23c and gga01c = @billno and gga00c = @compid update a set a.cardno = @cardno, a.client = @client, a.date = @date, a.time = @time from #consume a where a.billno = @billno -- 插入项目名称, if (@account = '888' or @account = '889') begin declare cur_project_name cursor for select gda03c, ggb12c from ggm02 a with (nolock), gdm01 b where a.ggb00c = b.gda00c and a.ggb03c = b.gda01c and a.ggb01c = @billno and ggb00c = @compid and ggb27c = '5' end else begin declare cur_project_name cursor for select gda03c, ggb12c from ggm02 a with (nolock), gdm01 b where a.ggb00c = b.gda00c and a.ggb03c = b.gda01c and a.ggb01c = @billno and ggb00c = @compid end open cur_project_name fetch next from cur_project_name into @project, @saler while @@fetch_status = 0 begin if (isnull(@saler, '') = '') select @name = isnull(@name, '') + @project + ', ' else select @name = isnull(@name, '') + @project + '(' + @saler + ')' + ', ' fetch next from cur_project_name into @project, @saler end close cur_project_name deallocate cur_project_name update a set a.project = @name from #consume a where a.billno = @billno -- 插入消耗产品名称 select @project = '' select @name = '' if (@account = '888' or @account = '889') begin declare cur_consume_goods cursor for select gfa03c, ggc12c from ggm03 a with(nolock), gfm01 b with(nolock) where a.ggc00c = b.gfa00c and a.ggc03c = b.gfa01c and a.ggc01c = @billno and ggc00c = @compid and ggc27c = '5' end else begin declare cur_consume_goods cursor for select gfa03c, ggc12c from ggm03 a, gfm01 b where a.ggc00c = b.gfa00c and a.ggc03c = b.gfa01c and a.ggc01c = @billno and ggc00c = @compid end open cur_consume_goods fetch next from cur_consume_goods into @project, @saler while @@fetch_status = 0 begin if (isnull(@saler, '') = '') select @name = @name + substring(@project, 1, 4) + ', ' else select @name = @name + substring(@project, 1, 4) + '(' + @saler + ')' + ', ' fetch next from cur_consume_goods into @project, @saler end close cur_consume_goods deallocate cur_consume_goods update a set a.congoods = @name from #consume a where a.billno = @billno -- 插入购买产品名称 select @project = '' select @name = '' declare cur_buy_goods cursor for select gfa03c from ggm05 a, gfm01 b where gge00c = @compid and a.gge01c = @billno and a.gge00c = b.gfa00c and a.gge03c = b.gfa01c open cur_buy_goods fetch next from cur_buy_goods into @project while @@fetch_status = 0 begin select @name = @name + substring(@project, 1, 4) + ', ' fetch next from cur_buy_goods into @project end close cur_buy_goods deallocate cur_buy_goods update a set a.buygoods = @name from #consume a where a.billno = @billno select @compid, @billno, cardno, client, date, '', case when substring(isnull(project, '') + isnull(congoods, '') + isnull(buygoods, ''), len(isnull(project, '') + isnull(congoods, '') + isnull(buygoods, '')), 1) = ',' then substring(isnull(project, '') + isnull(congoods, '') + isnull(buygoods, ''), 1, len(isnull(project, '') + isnull(congoods, '') + isnull(buygoods, '')) - 1) else isnull(project, '') + isnull(congoods, '') + isnull(buygoods, '') end, money, account, time from #consume drop table #consume end GO /****** Object: StoredProcedure [dbo].[upg_report_gwn04r_lm2] Script Date: 06/12/2017 06:20:37 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO Create proc [dbo].[upg_report_gwn04r_lm2] ( @fromdate varchar(10), --起始日期 @todate varchar(10), --结束日期 @companyid varchar(10), --门店 @gda02c varchar(10) --项目大类 ) as begin if( @companyid = '' and @gda02c = '' ) begin --男,女 select case gender when 0 then '女' when 1 then '男' end [name], isnull(sum(amt3),'') amt3 from work_detail with(nolock) where cls_flag = 1 and srvdate between @fromdate and @todate and amt3 > 0 group by gender end else if( @companyid != '' and @gda02c = '' ) begin --男,女 select case gender when 0 then '女' when 1 then '男' end [name], isnull(sum(amt3),'') amt3 from work_detail with(nolock) where cls_flag = 1 and compid = @companyid and srvdate between @fromdate and @todate and amt3 > 0 group by gender end else if( @companyid != '' and @gda02c != '' ) begin --男,女 select case gender when 0 then '女' when 1 then '男' end [name], isnull(sum(amt3),'') amt3 from work_detail with(nolock) where cls_flag = 1 and compid = @companyid and code in ( select gda01c from gdm01 with(nolock) where gda00c = compid and gda02c = @gda02c ) and srvdate between @fromdate and @todate and amt3 > 0 group by gender end end GO /****** Object: StoredProcedure [dbo].[upg_report_gwn04r_lm3] Script Date: 06/12/2017 06:20:37 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO Create proc [dbo].[upg_report_gwn04r_lm3] ( @fromdate varchar(10), --起始日期 @todate varchar(10), --结束日期 @companyid varchar(10), --门店 @gda02c varchar(10) --项目大类 ) as begin if( @companyid = '' and @gda02c = '' ) begin --会员,非会员 select '非会员' [name], isnull(sum(amt3),'') amt3 from work_detail with(nolock) where cls_flag = 1 and srvdate between @fromdate and @todate and cardid like '散客%' and amt3 > 0 union all select '会员' [name], isnull(sum(amt3),'') amt3 from work_detail with(nolock) where cls_flag = 1 and srvdate between @fromdate and @todate and cardid not like '散客%' and amt3 > 0 end else if( @companyid != '' and @gda02c = '' ) begin --会员,非会员 select '非会员' [name], isnull(sum(amt3),'') amt3 from work_detail with(nolock) where cls_flag = 1 and compid = @companyid and srvdate between @fromdate and @todate and cardid like '散客%' and amt3 > 0 union all select '会员' [name], isnull(sum(amt3),'') amt3 from work_detail with(nolock) where cls_flag = 1 and compid = @companyid and srvdate between @fromdate and @todate and cardid not like '散客%' and amt3 > 0 end else if( @companyid != '' and @gda02c != '' ) begin --会员,非会员 select '非会员' [name], isnull(sum(amt3),'') amt3 from work_detail with(nolock) where cls_flag = 1 and compid = @companyid and code in ( select gda01c from gdm01 with(nolock) where gda00c = compid and gda02c = @gda02c ) and srvdate between @fromdate and @todate and cardid like '散客%' and amt3 > 0 union all select '会员' [name], isnull(sum(amt3),'') amt3 from work_detail with(nolock) where cls_flag = 1 and compid = @companyid and code in ( select gda01c from gdm01 with(nolock) where gda00c = compid and gda02c = @gda02c ) and srvdate between @fromdate and @todate and cardid not like '散客%' and memid != '' and amt3 > 0 end end GO /****** Object: StoredProcedure [dbo].[upg_copy_hem01] Script Date: 06/12/2017 06:19:47 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_copy_hem01] ( @compid varchar(10), @fromfatype varchar(20), -- 从哪一个方案编号复制 @tofatype varchar(20), -- 目的方案编号 @itemCode varchar(20), -- 从哪一个方案编号的项目编号 @itemTypefrom varchar(10), @itemTypeTo varchar(10), @itemCodefrom varchar(20), @itemCodeTo varchar(20) ) as begin set nocount on -- 当前的事务数 declare @__transaction_count int set @__transaction_count = @@trancount -- try...catch处理 begin try -- a.开启事务,或者设置事务保存点 if @__transaction_count = 0 begin tran else save tran __tran_SavePoint if @itemCode = '' set @itemCode = '*' if @itemCode = '*' begin if (@fromfatype <> @tofatype) begin delete hem01 where hea00c = @compid and hea33c = @tofatype insert hem01(hea00c, hea01c, hea02i, hea03f, hea04i, hea05f, hea06i, hea07f, hea08i, hea09f, hea10i, hea11f, hea12f, hea13f, hea14i, hea15f, hea16i, hea17f, hea18i, hea19i, hea20i, hea21f, hea22i, hea23f, hea24f, hea25i, hea26f, hea27i, hea28f, hea29i, hea30i, hea31i, hea32i, hea33c, hea34c,hea35i,hea36f,hea37i,hea38f,hea39i,hea40f,hea41i,hea42f ) select @compid, hea01c, hea02i, hea03f, hea04i, hea05f, hea06i, hea07f, hea08i, hea09f, hea10i, hea11f, hea12f, hea13f, hea14i, hea15f, hea16i, hea17f, hea18i, hea19i, hea20i, hea21f, hea22i, hea23f, hea24f, hea25i, hea26f, hea27i, hea28f, hea29i, hea30i, hea31i, hea32i, @tofatype, isnull(hea34c, '*'),hea35i,hea36f,hea37i,hea38f,hea39i,hea40f,hea41i,hea42f from hem01 where hea00c = @compid and hea33c = @fromfatype end end else begin declare @gda01c varchar(20) if @fromfatype <> @tofatype begin declare cur_gdm01 cursor for select gda01c from gdm01 with(nolock) where gda00c = @compid and ((gda01c >= @itemCodefrom and gda01c <= @itemCodeTo) or @itemCodefrom = '*') and ((gda13c >= @itemTypefrom and gda13c <= @itemTypeTo) or @itemTypefrom = '*') end else begin declare cur_gdm01 cursor for select gda01c from gdm01 with(nolock) where gda00c = @compid and ((gda01c >= @itemCodefrom and gda01c <= @itemCodeTo) or @itemCodefrom = '*') and ((gda13c >= @itemTypefrom and gda13c <= @itemTypeTo) or @itemTypefrom = '*') and gda01c <> @itemCode end open cur_gdm01 fetch cur_gdm01 into @gda01c while @@fetch_status = 0 begin if (@fromfatype <> @tofatype or @gda01c <> @itemCode) begin delete hem01 where hea00c = @compid and hea33c = @tofatype and hea01c = @gda01c insert hem01(hea00c, hea01c, hea02i, hea03f, hea04i, hea05f, hea06i, hea07f, hea08i, hea09f, hea10i, hea11f, hea12f, hea13f, hea14i, hea15f, hea16i, hea17f, hea18i, hea19i, hea20i, hea21f, hea22i, hea23f, hea24f, hea25i, hea26f, hea27i, hea28f, hea29i, hea30i, hea31i, hea32i, hea33c, hea34c,hea35i,hea36f,hea37i,hea38f,hea39i,hea40f,hea41i,hea42f ) select @compid, @gda01c, hea02i, hea03f, hea04i, hea05f, hea06i, hea07f, hea08i, hea09f, hea10i, hea11f, hea12f, hea13f, hea14i, hea15f, hea16i, hea17f, hea18i, hea19i, hea20i, hea21f, hea22i, hea23f, hea24f, hea25i, hea26f, hea27i, hea28f, hea29i, hea30i, hea31i, hea32i, @tofatype, isnull(hea34c, '*'),hea35i,hea36f,hea37i,hea38f,hea39i,hea40f,hea41i,hea42f from hem01 where hea00c = @compid and hea33c = @fromfatype and hea01c = @itemCode end fetch cur_gdm01 into @gda01c end close cur_gdm01 deallocate cur_gdm01 end -- 提交事务 -- 有可提交的事务, 并且事务是在当前模块中开启的情况下, 才提交事务 if xact_state() = 1 and @__transaction_count = 0 commit -- 为了防止TRY 中有遗漏的事务处理, 可以在TRY 模拟的结束部分做最终的判断 if @__transaction_count = 0 begin if xact_state() = -1 rollback tran else begin while @@trancount > 0 commit tran end end end try begin catch -- 在CATCH 模块, 应该首先处理事务 if xact_state() <> 0 begin if @__transaction_count = 0 rollback tran -- xact_state 为-1 时, 不能回滚到事务保存点, 这种情况留给外层调用者做统一的事务回滚 -- 通过@@trancount > @__transaction_count 的判断, 即使在TRY 模块中没有设置事务保存点的情况下跳到此步骤, 也不会出错 else if xact_state() = 1 and @@trancount > @__transaction_count rollback tran __tran_SavePoint end declare @SP_Parameters varchar(max) set @SP_Parameters = '@compid = ' + isnull(@compid, '') + '@fromfatype = ' + isnull(@fromfatype, '') + '@tofatype = ' + isnull(@tofatype, '') + '@itemCode = ' + isnull(@itemCode, '') + '@itemTypefrom = ' + isnull(@itemTypefrom, '') + '@itemTypeTo = ' + isnull(@itemTypeTo, '') + '@itemCodefrom = ' + isnull(@itemCodefrom, '') + '@itemCodeTo = ' + isnull(@itemCodeTo, '') insert into SP_TranLog(SPName, SPTime, SPParameter) values('upg_copy_hem01', getdate(), @SP_Parameters) return -1 end catch end GO /****** Object: StoredProcedure [dbo].[upg_off_statistic] Script Date: 06/12/2017 06:20:25 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_off_statistic] ( @compid varchar(10), -- 公司别 @staffid varchar(10), -- 员工卡号 @fromdate varchar(8), -- 开始时间 @todate varchar(8) -- 结束时间 ) as --WUXM 2005/09/02 begin if (isnull(@staffid, '') = '' or isnull(@staffid, '') = '*') begin select isnull(gga80d,'') gga80d, isnull(gga01c,'') gga01c, isnull(gga09c,'') gga09c, isnull(gga91c,'') gga91c, isnull(gda03c,'') gda03c, isnull(ggb24f*ggb05f,'') ggb24f05f, isnull(ggb11f,'') ggb11f, isnull(ggb23c,'') ggb23c from ggm01 with(nolock), ggm02 with(nolock), gdm01 with(nolock) where gga00c = ggb00c and gga00c = gda00c and gga01c = ggb01c and ggb03c = gda01c and gga00c = @compid and ggb24f * ggb05f > ggb11f and gga80d >= @fromdate and gga80d <= @todate union select isnull(gga80d,'') gga80d, isnull(gga01c,'') gga01c, isnull(gga09c,'') gga09c, isnull(gga91c,'') gga91c, isnull(gfa03c,'') gda03c, isnull(ggc20f * ggc05f,'') ggb24f05f, isnull(ggc11f,'') ggb11f, isnull(ggc19c,'') ggb23c from ggm01 with(nolock), ggm03 with(nolock), gfm01 with(nolock) where gga00c = ggc00c and gga00c = gfa00c and gga01c = ggc01c and ggc03c = gfa01c and gga00c = @compid and ggc20f * ggc05f > ggc11f and gga80d >= @fromdate and gga80d <= @todate order by gga80d, gga01c end else begin select isnull(gga80d,'') gga80d, isnull(gga01c,'') gga01c, isnull(gga09c,'') gga09c, isnull(gga91c,'') gga91c, isnull(gda03c,'') gda03c, isnull(ggb24f*ggb05f,'') ggb24f05f, isnull(ggb11f,'') ggb11f, isnull(ggb23c,'') ggb23c from ggm01 with(nolock), ggm02 with(nolock), gdm01 with(nolock) where gga00c = ggb00c and gga00c = gda00c and gga01c = ggb01c and ggb03c = gda01c and gga00c = @compid and ggb24f * ggb05f > ggb11f and gga80d >= @fromdate and gga80d <= @todate and gga91c = @staffid union select isnull(gga80d,'') gga80d, isnull(gga01c,'') gga01c, isnull(gga09c,'') gga09c, isnull(gga91c,'') gga91c, isnull(gfa03c,'') gda03c, isnull(ggc20f * ggc05f,'') ggb24f05f, isnull(ggc11f,'') ggb11f, isnull(ggc19c,'') ggb23c from ggm01 with(nolock), ggm03 with(nolock), gfm01 with(nolock) where gga00c = ggc00c and gga00c = gfa00c and gga01c = ggc01c and ggc03c = gfa01c and gga00c = @compid and ggc20f * ggc05f > ggc11f and gga91c = @staffid and gga80d >= @fromdate and gga80d <= @todate order by gga80d, gga01c end end GO /****** Object: StoredProcedure [dbo].[upg_m_dispatch_gdm01_single] Script Date: 06/12/2017 06:20:19 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_m_dispatch_gdm01_single] ( @fromcompid varchar(10), -- 从哪一个公司发布 @tocompid varchar(10), -- 目的公司 @mandatory varchar(2), -- 是否强制统一 @gda01c varchar(20) -- 项目编号 ) as begin if @fromcompid = @tocompid return if @mandatory <> '1' and @mandatory <> '0' return if @mandatory = '0' begin if exists (select 1 from gdm01 where gda00c = @tocompid and gda01c = @gda01c) return end delete gdm01 where gda00c = @tocompid and gda01c = @gda01c insert gdm01(gda00c, gda01c, gda02c, gda03c, gda04c, gda05i, gda06c, gda07c, gda08c, gda09c, gda10f, gda11f, gda12f, gda13c, gda14i, gda15c, gda16i, gda17i, gda18f, gda19i, gda20i, gda21t, gda22f, gda23f, gda24f, gda25f, gda26f, gda27f, gda28f, gda29f, gda30f, gda31f, gda32f, gda33f, gda34i, gda35i, gda36i, gda37i, gda38c, gda38i, gda39d, gda11i, gda98c) select @tocompid, gda01c, gda02c, gda03c, gda04c, gda05i, gda06c, gda07c, gda08c, gda09c, gda10f, gda11f, gda12f, gda13c, gda14i, gda15c, gda16i, gda17i, gda18f, gda19i, gda20i, gda21t, gda22f, gda23f, gda24f, gda25f, gda26f, gda27f, gda28f, gda29f, gda30f, gda31f, gda32f, gda33f, gda34i, gda35i, gda36i, gda37i, gda38c, gda38i, gda39d, gda11i, gda98c from gdm01 where gda00c = @fromcompid and gda01c = @gda01c --Add by maql 20100506 declare @curdate varchar(20) declare @curtime varchar(20) exec upg_getcurrdate @curdate output, @curtime output -- 写入sysoperationlog Add by maql 20100506 insert sysoperationlog(userid, program, operation, operationdate, operationtime, origatedate, compid, keyvalue1, keyvalue2, keyvalue3) values('system', 'GSB02I', 'M', @curdate, @curtime, @curdate, @fromcompid, @tocompid, @mandatory, 'upg_m_dispatch_gdm01_single') end GO /****** Object: StoredProcedure [dbo].[upg_insert_project] Script Date: 06/12/2017 06:20:16 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_insert_project]( @compid varchar(10), @prefix varchar(20), --编号前缀 @suffix varchar(20), --编号后缀 @flow int, --开始编号 @nlen int) as --WUXM 2006/02/13 begin declare @i int declare @projectno varchar(20) declare @strpre varchar(20) --项目编号长度 select @i = 0 select @strpre = '0' while(@i < @nlen) begin select @strpre = @strpre + '0' select @i = @i + 1 end select @projectno = @prefix + right(@strpre + convert(varchar(20), @flow), @nlen) + @suffix insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, @projectno, '单剪', '9', '2', '次', 10, 1, '010000') select @projectno = @prefix + right(@strpre + convert(varchar(20), @flow + 1), @nlen) + @suffix insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, @projectno, '单洗', '9', '2', '次', 10, 1, '010000') select @projectno = @prefix + right(@strpre + convert(varchar(20), @flow + 2), @nlen) + @suffix insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, @projectno, '单吹', '9', '2', '次', 10, 1, '010000') select @projectno = @prefix + right(@strpre + convert(varchar(20), @flow + 3), @nlen) + @suffix insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, @projectno, '洗剪吹', '9', '2', '次', 10, 1, '010000') select @projectno = @prefix + right(@strpre + convert(varchar(20), @flow + 4), @nlen) + @suffix insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, @projectno, '平头', '9', '2', '次', 10, 1, '010000') select @projectno = @prefix + right(@strpre + convert(varchar(20), @flow + 5), @nlen) + @suffix insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, @projectno, '首席洗剪吹', '9', '2', '次', 10, 1, '010000') select @projectno = @prefix + right(@strpre + convert(varchar(20), @flow + 6), @nlen) + @suffix insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, @projectno, '总监洗剪吹', '9', '2', '次', 10, 1, '010000') select @projectno = @prefix + right(@strpre + convert(varchar(20), @flow + 7), @nlen) + @suffix insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, @projectno, '干洗', '9', '2', '次', 10, 1, '010000') select @projectno = @prefix + right(@strpre + convert(varchar(20), @flow + 8), @nlen) + @suffix insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, @projectno, '洗吹首次体验', '9', '2', '次', 10, 1, '010000') select @projectno = @prefix + right(@strpre + convert(varchar(20), @flow + 9), @nlen) + @suffix insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, @projectno, '润丝特效修护发膜短发', '9', '2', '次', 10, 1, '010000') select @projectno = @prefix + right(@strpre + convert(varchar(20), @flow + 10), @nlen) + @suffix insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, @projectno, '润丝特效修护发膜中发', '9', '2', '次', 10, 1, '010000') select @projectno = @prefix + right(@strpre + convert(varchar(20), @flow + 11), @nlen) + @suffix insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, @projectno, '润丝特效修护发膜长发', '9', '2', '次', 10, 1, '010000') select @projectno = @prefix + right(@strpre + convert(varchar(20), @flow + 12), @nlen) + @suffix insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, @projectno, '倍欧加倍营养发膜短发', '9', '2', '次', 10, 1, '010000') select @projectno = @prefix + right(@strpre + convert(varchar(20), @flow + 13), @nlen) + @suffix insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, @projectno, '倍欧加倍营养发膜中发', '9', '2', '次', 10, 1, '010000') select @projectno = @prefix + right(@strpre + convert(varchar(20), @flow + 14), @nlen) + @suffix insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, @projectno, '倍欧加倍营养发膜长发', '9', '2', '次', 10, 1, '010000') select @projectno = @prefix + right(@strpre + convert(varchar(20), @flow + 15), @nlen) + @suffix insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, @projectno, '烫后营养发膜短发', '9', '2', '次', 10, 1, '010000') select @projectno = @prefix + right(@strpre + convert(varchar(20), @flow + 16), @nlen) + @suffix insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, @projectno, '烫后营养发膜中发', '9', '2', '次', 10, 1, '010000') select @projectno = @prefix + right(@strpre + convert(varchar(20), @flow + 17), @nlen) + @suffix insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, @projectno, '烫后营养发膜长发', '9', '2', '次', 10, 1, '010000') select @projectno = @prefix + right(@strpre + convert(varchar(20), @flow + 18), @nlen) + @suffix insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, @projectno, '染后营养发膜短发', '9', '2', '次', 10, 1, '010000') select @projectno = @prefix + right(@strpre + convert(varchar(20), @flow + 19), @nlen) + @suffix insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, @projectno, '染后营养发膜中发', '9', '2', '次', 10, 1, '010000') select @projectno = @prefix + right(@strpre + convert(varchar(20), @flow + 20), @nlen) + @suffix insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, @projectno, '染后营养发膜长发', '9', '2', '次', 10, 1, '010000') select @projectno = @prefix + right(@strpre + convert(varchar(20), @flow + 21), @nlen) + @suffix insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, @projectno, '威娜精华护理液(一支)', '9', '2', '次', 10, 1, '010000') select @projectno = @prefix + right(@strpre + convert(varchar(20), @flow + 22), @nlen) + @suffix insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, @projectno, '接发(全头)', '9', '2', '次', 10, 1, '010000') select @projectno = @prefix + right(@strpre + convert(varchar(20), @flow + 23), @nlen) + @suffix insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, @projectno, '离子烫短发', '9', '2', '次', 10, 1, '010000') select @projectno = @prefix + right(@strpre + convert(varchar(20), @flow + 24), @nlen) + @suffix insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, @projectno, '离子烫中发', '9', '2', '次', 10, 1, '010000') select @projectno = @prefix + right(@strpre + convert(varchar(20), @flow + 25), @nlen) + @suffix insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, @projectno, '离子烫长发', '9', '2', '次', 10, 1, '010000') select @projectno = @prefix + right(@strpre + convert(varchar(20), @flow + 26), @nlen) + @suffix insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, @projectno, '直发膏短发', '9', '2', '次', 10, 1, '010000') select @projectno = @prefix + right(@strpre + convert(varchar(20), @flow + 27), @nlen) + @suffix insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, @projectno, '直发膏中发', '9', '2', '次', 10, 1, '010000') select @projectno = @prefix + right(@strpre + convert(varchar(20), @flow + 28), @nlen) + @suffix insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, @projectno, '直发膏长发', '9', '2', '次', 10, 1, '010000') select @projectno = @prefix + right(@strpre + convert(varchar(20), @flow + 29), @nlen) + @suffix insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, @projectno, '可丽丝倍佳染发短发', '9', '2', '次', 10, 1, '010000') select @projectno = @prefix + right(@strpre + convert(varchar(20), @flow + 30), @nlen) + @suffix insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, @projectno, '可丽丝倍佳染发中发', '9', '2', '次', 10, 1, '010000') select @projectno = @prefix + right(@strpre + convert(varchar(20), @flow + 31), @nlen) + @suffix insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, @projectno, '可丽丝倍佳染发长发', '9', '2', '次', 10, 1, '010000') select @projectno = @prefix + right(@strpre + convert(varchar(20), @flow + 32), @nlen) + @suffix insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, @projectno, '挑染短发(全头)', '9', '2', '次', 10, 1, '010000') select @projectno = @prefix + right(@strpre + convert(varchar(20), @flow + 33), @nlen) + @suffix insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, @projectno, '挑染中发(全头)', '9', '2', '次', 10, 1, '010000') select @projectno = @prefix + right(@strpre + convert(varchar(20), @flow + 34), @nlen) + @suffix insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, @projectno, '挑染长发(全头)', '9', '2', '次', 10, 1, '010000') select @projectno = @prefix + right(@strpre + convert(varchar(20), @flow + 35), @nlen) + @suffix insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, @projectno, '挑染(组)', '9', '2', '次', 10, 1, '010000') select @projectno = @prefix + right(@strpre + convert(varchar(20), @flow + 36), @nlen) + @suffix insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, @projectno, '洗色短发', '9', '2', '次', 10, 1, '010000') select @projectno = @prefix + right(@strpre + convert(varchar(20), @flow + 37), @nlen) + @suffix insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, @projectno, '洗色中发', '9', '2', '次', 10, 1, '010000') select @projectno = @prefix + right(@strpre + convert(varchar(20), @flow + 38), @nlen) + @suffix insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, @projectno, '洗色长发', '9', '2', '次', 10, 1, '010000') select @projectno = @prefix + right(@strpre + convert(varchar(20), @flow + 39), @nlen) + @suffix insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, @projectno, '陶瓷烫', '9', '2', '次', 10, 1, '010000') select @projectno = @prefix + right(@strpre + convert(varchar(20), @flow + 40), @nlen) + @suffix insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, @projectno, '热能烫', '9', '2', '次', 10, 1, '010000') select @projectno = @prefix + right(@strpre + convert(varchar(20), @flow + 41), @nlen) + @suffix insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, @projectno, '美白保湿', '6', '2', '次', 50, 1, '010000') select @projectno = @prefix + right(@strpre + convert(varchar(20), @flow + 42), @nlen) + @suffix insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, @projectno, '中干保湿(油性肌肤)', '6', '2', '次', 50, 1, '010000') select @projectno = @prefix + right(@strpre + convert(varchar(20), @flow + 43), @nlen) + @suffix insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, @projectno, '基础护理', '6', '2', '次', 50, 1, '010000') select @projectno = @prefix + right(@strpre + convert(varchar(20), @flow + 44), @nlen) + @suffix insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, @projectno, '美白更新', '6', '2', '次', 50, 1, '010000') select @projectno = @prefix + right(@strpre + convert(varchar(20), @flow + 45), @nlen) + @suffix insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, @projectno, '敏感肌肤修护', '6', '2', '次', 50, 1, '010000') select @projectno = @prefix + right(@strpre + convert(varchar(20), @flow + 46), @nlen) + @suffix insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, @projectno, '毛囊炎肌肤修护', '6', '2', '次', 50, 1, '010000') select @projectno = @prefix + right(@strpre + convert(varchar(20), @flow + 47), @nlen) + @suffix insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, @projectno, '活化细胞精华', '6', '2', '次', 50, 1, '010000') select @projectno = @prefix + right(@strpre + convert(varchar(20), @flow + 48), @nlen) + @suffix insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, @projectno, '凹洞疤痕修护', '6', '2', '次', 50, 1, '010000') select @projectno = @prefix + right(@strpre + convert(varchar(20), @flow + 49), @nlen) + @suffix insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, @projectno, '紧肤除皱修护', '6', '2', '次', 50, 1, '010000') select @projectno = @prefix + right(@strpre + convert(varchar(20), @flow + 50), @nlen) + @suffix insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, @projectno, '光彩柔肤精华', '6', '2', '次', 50, 1, '010000') select @projectno = @prefix + right(@strpre + convert(varchar(20), @flow + 51), @nlen) + @suffix insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, @projectno, '生化更新', '6', '2', '次', 50, 1, '010000') select @projectno = @prefix + right(@strpre + convert(varchar(20), @flow + 52), @nlen) + @suffix insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, @projectno, '玫瑰焕彩', '6', '2', '次', 50, 1, '010000') select @projectno = @prefix + right(@strpre + convert(varchar(20), @flow + 53), @nlen) + @suffix insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, @projectno, '眼部护理', '6', '2', '次', 50, 1, '010000') select @projectno = @prefix + right(@strpre + convert(varchar(20), @flow + 54), @nlen) + @suffix insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, @projectno, '修眉', '6', '2', '次', 50, 1, '010000') select @projectno = @prefix + right(@strpre + convert(varchar(20), @flow + 55), @nlen) + @suffix insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, @projectno, '美容首次体验', '6', '2', '次', 50, 1, '010000') select @projectno = @prefix + right(@strpre + convert(varchar(20), @flow + 56), @nlen) + @suffix insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, @projectno, '眼部体验价', '6', '2', '次', 50, 1, '010000') select @projectno = @prefix + right(@strpre + convert(varchar(20), @flow + 57), @nlen) + @suffix insert gdm01(gda00c, gda01c, gda03c, gda04c, gda07c, gda09c, gda10f, gda14i, gda21t) values(@compid, @projectno, '美白保湿', '6', '2', '次', 50, 1, '010000') update gdm01 set gda13c = gda04c where isnull(gda13c, '') = '' end GO /****** Object: StoredProcedure [dbo].[upg_m_dispatch_gdm01] Script Date: 06/12/2017 06:20:19 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_m_dispatch_gdm01] ( @fromcompid varchar(10), -- 从哪一个公司发布 @tocompid varchar(10), -- 目的公司 @mandatory varchar(2) -- 是否强制统一 ) as begin if @fromcompid = @tocompid return --Add by maql 20100506 declare @curdate varchar(20) declare @curtime varchar(20) exec upg_getcurrdate @curdate output, @curtime output -- 需要记录日志 create table #gdm01_01 -- 项目资料主档 ( gda00c varchar(10) not null, --公司编号 gda01c varchar(20) not null, --项目编号 gda02c varchar(30) null, --(预留) gda03c varchar(100) null, --项目简称 gda04c varchar(5) null, --项目类别 gda05i int null, --计费单位(1-按次,2-按小时) gda06c varchar(10) null, --负责人 gda07c char(1) null, --收费性质(1-免费,2收费) gda08c varchar(40) null, --安全次数(购买疗程超过则不让继续购买) gda09c varchar(5) null, --计价单位 gda10f float null, --标准价格 gda11f float null, --统一会员价 gda12f float null, --体验价格 gda13c varchar(10) null, --当前负责教练 gda14i int null, --是否允许预约(1- 允许 0 -不允许) gda15c varchar(128)null, --项目简介资料(文件path) gda16i int null, --回访间隔天数 gda17i tinyint null, -- 积分方式 gda18f float null, -- 积分或比率 gda19i int null, --项目是否必须现金支付,1为必须现金支付,0为未必 gda20i int null, -- 消费间隔 gda21t varchar(20) null, -- 消费持续时间,add by sdw 2006/11/9,足疗版本 gda22f float null, -- 价格1次数, 月 gda23f float null, -- 价格1金额, 月 gda24f float null, -- 价格2次数, 季度 gda25f float null, -- 价格2金额, 季度 gda26f float null, -- 价格3次数, 半年 gda27f float null, -- 价格3金额, 半年 gda28f float null, -- 价格4次数, 年 gda29f float null, -- 价格4金额, 年 gda30f float null, -- 赠送,月 gda31f float null, -- 赠送,季 gda32f float null, -- 赠送,半年 gda33f float null, -- 赠送,年 gda34i float null, -- 控制销售,月 0 不可 1 售前可 2售后可 3嘉宾可 4售前后可 5都可 gda35i float null, -- 控制销售,季 gda36i float null, -- 控制销售,半年 gda37i float null, -- 控制销售,年 gda38c varchar(200) null, -- 拼音检索 gda38i int default(1) null, -- 是否在终端上显示出来 0 不显示 1(null默认为1)显示 gda39d datetime default getdate(), -- 创建时间 gda11i int default(0) null, -- 启用会员价 1为启用 0不启用 默认不启用 gda98c varchar(100) null, --项目名称助记码 ) declare @PK_Sql nvarchar(max) set @PK_Sql = ' alter table #gdm01_01 add constraint PK_#gdm01_01_' + convert(varchar(10), cast(floor(rand() * 1000000) as int)) + ' primary key clustered(gda00c, gda01c)' exec sp_executesql @PK_Sql insert #gdm01_01(gda00c, gda01c, gda02c, gda03c, gda04c, gda05i, gda06c, gda07c, gda08c, gda09c, gda10f, gda11f, gda12f, gda13c, gda14i, gda15c, gda16i, gda17i, gda18f, gda19i, gda20i, gda21t, gda22f, gda23f, gda24f, gda25f, gda26f, gda27f, gda28f, gda29f, gda30f, gda31f, gda32f, gda33f, gda34i, gda35i, gda36i, gda37i, gda38c, gda38i, gda39d, gda11i, gda98c) select gda00c, gda01c, gda02c, gda03c, gda04c, gda05i, gda06c, gda07c, gda08c, gda09c, gda10f, gda11f, gda12f, gda13c, gda14i, gda15c, gda16i, gda17i, gda18f, gda19i, gda20i, gda21t, gda22f, gda23f, gda24f, gda25f, gda26f, gda27f, gda28f, gda29f, gda30f, gda31f, gda32f, gda33f, gda34i, gda35i, gda36i, gda37i, gda38c, gda38i, gda39d, gda11i, gda98c from gdm01 where gda00c = @fromcompid if @mandatory = '1' begin delete gdm01 where gda00c = @tocompid end else begin delete a from #gdm01_01 a, gdm01 b where a.gda00c = @fromcompid and a.gda01c = b.gda01c and b.gda00c = @tocompid end insert gdm01(gda00c, gda01c, gda02c, gda03c, gda04c, gda05i, gda06c, gda07c, gda08c, gda09c, gda10f, gda11f, gda12f, gda13c, gda14i, gda15c, gda16i, gda17i, gda18f, gda19i, gda20i, gda21t, gda22f, gda23f, gda24f, gda25f, gda26f, gda27f, gda28f, gda29f, gda30f, gda31f, gda32f, gda33f, gda34i, gda35i, gda36i, gda37i, gda38c, gda38i, gda39d, gda11i, gda98c) select @tocompid, gda01c, gda02c, gda03c, gda04c, gda05i, gda06c, gda07c, gda08c, gda09c, gda10f, gda11f, gda12f, gda13c, gda14i, gda15c, gda16i, gda17i, gda18f, gda19i, gda20i, gda21t, gda22f, gda23f, gda24f, gda25f, gda26f, gda27f, gda28f, gda29f, gda30f, gda31f, gda32f, gda33f, gda34i, gda35i, gda36i, gda37i, gda38c, gda38i, gda39d, gda11i, gda98c from #gdm01_01 where gda00c = @fromcompid -- 写入sysoperationlog Add by maql 20100506 insert sysoperationlog(userid, program, operation, operationdate, operationtime, origatedate, compid, keyvalue1, keyvalue2, keyvalue3) values('system', 'GSB02I', 'M', @curdate, @curtime, @curdate, @fromcompid, @tocompid, @mandatory, 'upg_m_dispatch_gdm01') drop table #gdm01_01 end GO /****** Object: StoredProcedure [dbo].[upg_report_gmn03r] Script Date: 06/12/2017 06:20:36 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create proc [dbo].[upg_report_gmn03r] ( @companyId varchar(30), --公司编号 @startCard varchar(30), --开卡日期 起始 @endCard varchar(30), --开卡日期 截止 @type int, --会员分类 @startCardNo varchar(30), --卡号 起始 @endCardNO varchar(30), --卡号 截止 @sex int, --性别:-1 全部, 0 女,1 男 @hun int, --婚否: -1 全部,0未婚,1已婚 @juli int, --多久没来(死亡,休眠客户) @fromcs int, --来店次数 @tocs int, --来店次数 @jine float, --平均消费金额 @xmdl varchar(30), --项目大类 @xmjc varchar(30), --项目简称 @radxm int, --项目分类 -1 全部 0 体验项目 1 叠加项目 @startyue float, --卡余额 起始 @endyue float, --卡余额 截止 @startfwcode varchar(30), --服务员工编号 起始 @endfwcode varchar(30), --服务员工编号 截止 @startyewu varchar(30), --负责业务 起始 @endyewu varchar(30), --负责业务 截止 @qudao int -- 进店渠道 ) as begin select isnull(gba00c,'') gba00c, -- 公司编号 isnull(gba01c,'') gba01c, -- 会员编号 isnull(juli,'') juli, --多久没来 cast(round(isnull(pjxh,0),2) as numeric(20,2)) pjxh, --平均消费金额 gba24cName = isnull((select gsb03c from gsm02 with(nolock) where gsb01c ='AB' and gsb00c = gba00c and gsb02c = gba24c ),'') ,--会员分类 isnull(gba03c,'') gba03c, -- 会员姓名 case gba14i when 0 then '女' when 1 then '男' else '未知' end gba14iName, -- 性别 isnull(gba17d,'') gba17d, -- 出生日期 case gba39i when 0 then '未婚' when 1 then '已婚' else '不详' end gba39iName, -- 婚 isnull(cishu,0) cishu, --消费总次数 cast(round(isnull(jine,0),2) as numeric(20,2)) jine, --消费总金额 isnull(startime,'') startime, --第一次消费时间 isnull(endtime,'') endtime, --最近消费时间 isnull(gba22c,'') gba22c, -- 目前状况 isnull(gba36c,'') gba36c, -- 围度 isnull(gba37c,'') gba37c, -- 血压 isnull(gba38c,'') gba38c, -- 身高/体重/BMI isnull(gba40c,'') gba40c, -- 脉搏 isnull(gba31c,'') gba31c, -- 口味 isnull(gba29c,'') gba29c, -- 饮食习惯 isnull(gba30c,'') gba30c, -- 喜好食物 gba18cName = isnull((select gsb03c from gsm02 with(nolock) where gsb01c ='H' and gsb00c = gba00c and gsb02c = gba18c ),'') , -- 职业 isnull(gba28i,0) gba28i, -- 工作年限 isnull(gba26c,'') gba26c, -- 工作单位 isnull(gba27c,'') gba27c, -- 工作场所 isnull(gba19c,'') gba19c, -- 历史 isnull(gba07c,'') gba07c, -- 家庭Tele No. isnull(gba08c,'') gba08c, -- Mobile No. isnull(gba09c,'') gba09c, -- Fax No.-1 isnull(gba13c,'') gba13c, -- 公司电话 isnull(gba06c,'') gba06c, -- 微信号. isnull(gba11c,'') gba11c, -- E-Mail地址 isnull(gba32c,'') gba32c, -- QQ号码 isnull(gba33c,'') gba33c, -- MSN号码 isnull(gba25c,'') gba25c, -- 居住区域 isnull(gba05c,'') gba05c, -- 联络地址 isnull(gba12c,'') gba12c, -- 邮编 gba42cName = isnull((select gsb03c from gsm02 with(nolock) where gsb01c ='NOS0' and gsb00c = gba00c and gsb02c = gba42c ),'') , --会员进店渠道 isnull((case gba20c when 1 then '售前:'+gba10c when 2 then '售后:'+gba10c when 3 then '嘉宾:'+gba10c end),'') gba20c, --售前 1 售后 2 嘉宾 3 isnull(gba23c,'') gba23c, -- 会员卡号 isnull(gca06d,'') gca06d, --开卡日期 isnull(gca07d,'') gca07d, --有效日期 --gca08i, --状态(未销售, 销售未开卡, 正常使用中, 挂失转卡,越期可续卡 , 越期作废卡) cast(round(isnull(gca24f,0),2) as numeric(20,2)) gca24f, --单次入馆金额 cast(round(isnull(gca29f,0),2) as numeric(20,2)) gca29f, --可用余额 case gca26f when 0 then '正常' when 1 then '未用' when 2 then '关闭' else '不详' end gca26f, isnull(gba90c,'') gba90c, --介绍人卡号 isnull(gba41d,'') gba41d, -- 会员创建时间 case gba04c when 1 then '可短信通知' when 2 then '不可短信通知' else '未标明' end gba04c, -- 是否发送短信通知(1是2否) case gba43c when 1 then '接受群发' when 0 then '不接受群发' else '未标明' end gba43c -- 是否接收群发短信(1是0否) from gbm01 with(nolock) --会员基本资料主档 left join ( select isnull(gga00c,'') compyId, isnull(gga05c,'') membid, -- 会员代码, isnull(count(gga01c),0) cishu, --消费总次数 isnull(sum(gga16f),0) jine, --消费总金额 isnull(sum(gga16f)/count(gga01c),0) pjxh, --平均消费 isnull(min(gga94d),'') startime, --开始消费日期 isnull(max(gga94d),'') endtime, --最后消费日期 isnull(-datediff(dd,getdate(),convert(datetime,max(gga94d))),'') juli --多久没来店 from ggm01 with(nolock) group by gga00c, gga05c ) aTable on gba00c = compyId and gba01c = membid left join gcm01 with(nolock) --会员卡 on gba00c = gca00c and gba23c = gca01c where gba00c = @companyId and gca06d between @startCard and @endCard --开卡日期 and ( @type = -1 or gba24c = @type ) --会员分类 and ( @startCardNo = '' or gba23c between @startCardNo and @endCardNo ) --卡号 and ( @sex = -1 or gba14i = @sex ) --性别 and ( @hun = -1 or gba39i = @hun ) --婚否 and ( @juli = -1 or juli >= @juli ) --距离多久没来 and ( @fromcs = -1 or isnull(cishu,0) between @fromcs and @tocs ) --来店次数 and ( @jine = -1 or jine >= @jine ) --平均消费金额 and ( @xmdl = '' or gba01c in( select gga05c from ggm01 with(nolock) left join ggm02 with(nolock) on ggm01.gga00c = ggm02.ggb00c And ggm01.gga01c = ggm02.ggb01c inner join gdm01 with(nolock) on gdm01.gda00c = ggm02.ggb00c And gdm01.gda01c = ggm02.ggb03c where gga00c = @companyId and gda02c = @xmdl --项目大类 and ( @xmjc = '' or gda01c = @xmjc ) --项目简称 group by gga05c ) ) and ( @radxm = -1 or gba01c in( select gga05c from ggm01 with(nolock) left join ggm02 with(nolock) on ggm01.gga00c = ggm02.ggb00c And ggm01.gga01c = ggm02.ggb01c inner join gdm01 with(nolock) on gdm01.gda00c = ggm02.ggb00c And gdm01.gda01c = ggm02.ggb03c where gga00c = @companyId and ( ( @radxm = 0 and ggb45i = 1) or --体验项目 ( @radxm = 1 and ggb47i != -1) --叠加项目 ) group by gga05c ) ) and ( @startyue = -1 or gca29f between @startyue and @endyue) --卡余额 and ( @startfwcode = '' or gba01c in( --服务员工 select gga05c from ggm01 with(nolock) left join ggm02 with(nolock) on ggm01.gga00c = ggm02.ggb00c and ggm01.gga01c = ggm02.ggb01c where isnull(ggb12c,'') <> '' and ggb12c between @startfwcode and @endfwcode ) --大工 ) and ( @startfwcode = '' or gba01c in( --服务员工 select gga05c from ggm01 with(nolock) left join ggm02 with(nolock) on ggm01.gga00c = ggm02.ggb00c and ggm01.gga01c = ggm02.ggb01c where isnull(ggb13c,'') <> '' and ggb13c between @startfwcode and @endfwcode ) --中工 ) and ( @startfwcode = '' or gba01c in( --服务员工 select gga05c from ggm01 with(nolock) left join ggm02 with(nolock) on ggm01.gga00c = ggm02.ggb00c and ggm01.gga01c = ggm02.ggb01c where isnull(ggb14c,'') <> '' and ggb14c between @startfwcode and @endfwcode ) --小工 ) and ( @startfwcode = '' or gba01c in( --服务员工 select gga05c from ggm01 with(nolock) left join ggm02 with(nolock) on ggm01.gga00c = ggm02.ggb00c and ggm01.gga01c = ggm02.ggb01c where isnull(ggb26c,'') <> '' and ggb26c between @startfwcode and @endfwcode ) --四工 ) and ( @startyewu = '' or gba10c between @startyewu and @endyewu ) --负责业务 and ( @qudao = -1 or gba42c = @qudao ) end GO /****** Object: StoredProcedure [dbo].[upg_Check_Gcm10_Can_UnConfirm] Script Date: 06/12/2017 06:19:40 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_Check_Gcm10_Can_UnConfirm] ( @comp varchar(10), -- 门店 @billid varchar(20) -- 单据号 ) as begin declare @gcl06i int declare @card varchar(20) select @gcl06i = gcl06i, @card = gcl03c from gcm10 where gcl00c = @comp and gcl01c = @billid if @gcl06i = 5 return -1 if @gcl06i = 2 or @gcl06i = 6 or @gcl06i is null return 0 declare @gcd09d varchar(8) declare @gcd13t varchar(6) select @gcd09d = gcd09d, @gcd13t = gcd13t from gcm04 with (nolock) where gcd00c = @comp and gcd01c = @card and gcd07c = 'gz' and gcd08c = @billid if exists( select 1 from gcm04 with (nolock) where gcd01c = @card and gcd07c = 'gx' and (gcd09d + gcd13t) > (@gcd09d + @gcd13t) and gcd02i in (select gcd02i from gcm04 with (nolock) where gcd00c = @comp and gcd01c = @card and gcd07c = 'gz' and gcd08c = @billid and gcd02i = 4)) return -1 if exists(select 1 from gtc07 a, gtc09 b where b.gti00c = @comp and b.gti01c = 'gz' and b.gti02c = @billid and a.gti03c = @card and a.gti04c = b.gti03c and a.gti05c = b.gti04c and a.gti08c = 'gx' and a.gti14i = b.gti10i and a.gti12i = 1 and a.gti07f > 0) return -1 if exists(select 1 from gtc07 a, gtc10 b where b.gtj00c = @comp and b.gtj01c = 'gz' and b.gtj02c = @billid and a.gti03c = @card and a.gti04c = b.gtj03c and a.gti05c = b.gtj04c and a.gti08c = 'gx' and a.gti14i = b.gtj10i and a.gti12i = 2 and a.gti07f > 0) return -1 return 0 end GO /****** Object: StoredProcedure [dbo].[upg_recreate_gtc07_scope] Script Date: 06/12/2017 06:20:34 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_recreate_gtc07_scope]( @compid varchar(20), @fromdate varchar(20), @todate varchar(20)) as begin create table #gtc07_buf ( gti00c varchar(10) not null, --公司编号 gti02d varchar(8) not null, --日期 gti03c varchar(20) null, --卡号 gti04c varchar(20) null, --套餐编号 gti05c varchar(20) null, --主项目编号/产品编号 gti06f float null, --购买次数 gti07f float null, --使用次数 gti08c varchar(20) null, --单据类别 gti09c varchar(20) null, --单号 gti10f float null, --上次剩余次数 gti11f float null, --当前剩余次数 gti12i int null, --类别 1 项目 2物品 gti13c varchar(20) null, --实际消耗的项目或产品 gti14i int null, --套餐序号 gti17c varchar(5) null, --支付方式1 gti18f float null, --支付方式1金额 gti19c varchar(5) null, --支付方式2 gti20f float null, --支付方式2金额 gti21c varchar(5) null, --支付方式3 gti22f float null, --支付方式3金额 gti23c varchar(5) null, --支付方式4 gti24f float null, --支付方式4金额 ttime varchar(20) null, --时间 ) create clustered index idx_gtc07_01 on #gtc07_buf(gti00c, gti03c, gti14i) --套餐购买历史, 项目 insert #gtc07_buf(gti00c, gti02d, gti03c, gti04c, gti05c, gti06f, gti07f, gti08c, gti09c, gti12i, gti13c, gti14i, gti17c, gti18f, gti19c, gti20f, gti21c, gti22f, gti23c, gti24f, ttime) select gti00c, gna02d, gna13c, gti03c, gti08c, gti05f, 0, 'gn', gna01c, 1, gti04c, 0, gtf09c, gtf10f*isnull(gti09f, 0), gtf11c, gtf12f*isnull(gti09f, 0), gtf13c, gtf14f*isnull(gti09f, 0), gtf15c, gtf16f*isnull(gti09f, 0), gna12t from gtc05 a with (nolock), gtc09 b with (nolock), gnm01 c with (nolock), gam26 z where a.gtf00c = b.gti00c and a.gtf01c = b.gti02c and a.gtf02c = b.gti03c and b.gti01c = 'gn' and a.gtf00c = c.gna00c and a.gtf01c = c.gna01c and c.gna00c = z.gaz02c and z.gaz01c = @compid and c.gna02d >= @fromdate and c.gna02d <= @todate --套餐购买历史, 产品 insert #gtc07_buf(gti00c, gti02d, gti03c, gti04c, gti05c, gti06f, gti07f, gti08c, gti09c, gti12i, gti13c, gti14i, gti17c, gti18f, gti19c, gti20f, gti21c, gti22f, gti23c, gti24f, ttime) select gtj00c, gna02d, gna13c, gtj03c, gtj08c, gtj05f, 0, 'gn', gna01c, 2, gtj04c, 0, gtf09c, gtf10f*isnull(gtj09f, 0), gtf11c, gtf12f*isnull(gtj09f, 0), gtf13c, gtf14f*isnull(gtj09f, 0), gtf15c, gtf16f*isnull(gtj09f, 0), gna12t from gtc05 a with (nolock), gtc10 b with (nolock), gnm01 c with (nolock), gam26 z where a.gtf00c = b.gtj00c and a.gtf01c = b.gtj02c and a.gtf02c = b.gtj03c and b.gtj01c = 'gn' and a.gtf00c = c.gna00c and a.gtf01c = c.gna01c and c.gna00c = z.gaz02c and z.gaz01c = @compid and c.gna02d >= @fromdate and c.gna02d <= @todate --会员账户管理套餐购买历史--项目 insert #gtc07_buf(gti00c, gti02d, gti03c, gti04c, gti05c, gti06f, gti07f, gti08c, gti09c, gti12i, gti13c, gti14i, gti17c, gti18f, gti19c, gti20f, gti21c, gti22f, gti23c, gti24f, ttime) select gti00c, gcl02d, gcl03c, gti03c, gti08c, gti05f, 0, 'gz', gcl01c, 1, gti04c, 0, gth09c, gth10f*isnull(gti09f, 0), gth11c, gth12f*isnull(gti09f, 0), gth13c, gth14f*isnull(gti09f, 0), gth15c, gth16f*isnull(gti09f, 0), gcl56t from gtc06 a with (nolock), gtc09 b with (nolock), gcm10 c with (nolock), gam26 z where a.gth00c = b.gti00c and a.gth01c = b.gti02c and a.gth02c = b.gti03c and b.gti01c = 'gz' and a.gth00c = c.gcl00c and a.gth01c = c.gcl01c and c.gcl00c = z.gaz02c and z.gaz01c = @compid and c.gcl02d >= @fromdate and c.gcl02d <= @todate and c.gcl06i in (0, 6) --会员账户管理套餐购买历史--产品 insert #gtc07_buf(gti00c, gti02d, gti03c, gti04c, gti05c, gti06f, gti07f, gti08c, gti09c, gti12i, gti13c, gti14i, gti17c, gti18f, gti19c, gti20f, gti21c, gti22f, gti23c, gti24f, ttime) select gtj00c, gcl02d, gcl03c, gtj03c, gtj08c, gtj05f, 0, 'gz', gcl01c, 2, gtj04c, 0, gth09c, gth10f*isnull(gtj09f, 0), gth11c, gth12f*isnull(gtj09f, 0), gth13c, gth14f*isnull(gtj09f, 0), gth15c, gth16f*isnull(gtj09f, 0), gcl56t from gtc06 a with (nolock), gtc10 b with (nolock), gcm10 c with (nolock), gam26 z where a.gth00c = b.gtj00c and a.gth01c = b.gtj02c and a.gth02c = b.gtj03c and b.gtj01c = 'gz' and a.gth00c = c.gcl00c and a.gth01c = c.gcl01c and c.gcl00c = z.gaz02c and z.gaz01c = @compid and c.gcl02d >= @fromdate and c.gcl02d <= @todate and c.gcl06i in (0, 6) --会员账户管理套餐购买历史--项目, 退 insert #gtc07_buf(gti00c, gti02d, gti03c, gti04c, gti05c, gti06f, gti07f, gti08c, gti09c, gti12i, gti13c, gti14i, gti17c, gti18f, gti19c, gti20f, gti21c, gti22f, gti23c, gti24f, ttime) select gti00c, gcl02d, gcl03c, gti03c, gti08c, 0-gti05f, 0, 'gz', gcl01c, 1, gti04c, 0, gth09c, gth10f*isnull(gti09f, 0), gth11c, gth12f*isnull(gti09f, 0), gth13c, gth14f*isnull(gti09f, 0), gth15c, gth16f*isnull(gti09f, 0), gcl56t from gtc06 a with (nolock), gtc09 b with (nolock), gcm10 c with (nolock), gam26 z where a.gth00c = b.gti00c and a.gth01c = b.gti02c and a.gth02c = b.gti03c and b.gti01c = 'gz' and a.gth00c = c.gcl00c and a.gth01c = c.gcl01c and c.gcl00c = z.gaz02c and z.gaz01c = @compid and c.gcl02d >= @fromdate and c.gcl02d <= @todate and c.gcl06i = 1 --会员账户管理套餐购买历史--产品, 退 insert #gtc07_buf(gti00c, gti02d, gti03c, gti04c, gti05c, gti06f, gti07f, gti08c, gti09c, gti12i, gti13c, gti14i, gti17c, gti18f, gti19c, gti20f, gti21c, gti22f, gti23c, gti24f, ttime) select gtj00c, gcl02d, gcl03c, gtj03c, gtj08c, 0 - gtj05f, 0, 'gz', gcl01c, 2, gtj04c, 0, gth09c, gth10f*isnull(gtj09f, 0), gth11c, gth12f*isnull(gtj09f, 0), gth13c, gth14f*isnull(gtj09f, 0), gth15c, gth16f*isnull(gtj09f, 0), gcl56t from gtc06 a with (nolock), gtc10 b with (nolock), gcm10 c with (nolock), gam26 z where a.gth00c = b.gtj00c and a.gth01c = b.gtj02c and a.gth02c = b.gtj03c and b.gtj01c = 'gz' and a.gth00c = c.gcl00c and a.gth01c = c.gcl01c and c.gcl00c = z.gaz02c and z.gaz01c = @compid and c.gcl02d >= @fromdate and c.gcl02d <= @todate and c.gcl06i = 1 --需要考虑换套餐的情况 insert #gtc07_buf(gti00c, gti02d, gti03c, gti04c, gti05c, gti06f, gti07f, gti08c, gti09c, gti12i, gti13c, gti14i, gti17c, gti18f, gti19c, gti20f, gti21c, gti22f, gti23c, gti24f, ttime) select hej00c, hej03d, hej02c, hej04c, hej06c, 0, hej08f, 'pj', hej01c, hej05i, hej06c, hej11i, '#', hej07f, '', 0, '', 0, '', 0, '' from hem09 a with (nolock), gam26 z where a.hej00c = z.gaz02c and z.gaz01c = @compid and a.hej03d >= @fromdate and a.hej03d <= @todate --消费的套餐, 项目 insert #gtc07_buf(gti00c, gti02d, gti03c, gti04c, gti05c, gti06f, gti07f, gti08c, gti09c, gti12i, gti13c, gti14i, gti17c, gti18f, gti19c, gti20f, gti21c, gti22f, gti23c, gti24f, ttime) select ggb00c, gga02d, gga09c, '', '', 0, ggb05f, 'gx', gga01c, 1, ggb03c, ggb48i, ggb27c, ggb11f, '', 0, '', 0, '', 0, gga04t from ggm02 a with (nolock), ggm01 b with (nolock), gam26 z where a.ggb00c = b.gga00c and a.ggb01c = b.gga01c and a.ggb00c = z.gaz02c and z.gaz01c = @compid and b.gga02d >= @fromdate and b.gga02d <= @todate and a.ggb27c = '#' --消费的套餐, 产品 insert #gtc07_buf(gti00c, gti02d, gti03c, gti04c, gti05c, gti06f, gti07f, gti08c, gti09c, gti12i, gti13c, gti14i, gti17c, gti18f, gti19c, gti20f, gti21c, gti22f, gti23c, gti24f, ttime) select ggc00c, gga02d, gga09c, '', '', 0, ggc05f, 'gx', gga01c, 2, ggc03c, ggc48i, ggc27c, ggc11f, '', 0, '', 0, '', 0, gga04t from ggm03 a with (nolock), ggm01 b with (nolock), gam26 z where a.ggc00c = b.gga00c and a.ggc01c = b.gga01c and a.ggc00c = z.gaz02c and z.gaz01c = @compid and b.gga02d >= @fromdate and b.gga02d <= @todate and a.ggc27c = '#' --把gx的套餐编号以及主项目号更新过来 update a set a.gti04c = b.gte02c, gti05c = b.gte08c from #gtc07_buf a, gtc04 b where a.gti00c = b.gte00c and a.gti03c = b.gte01c and a.gti14i = b.gte09i and a.gti08c = 'gx' and a.gti12i = 1 update a set a.gti04c = b.gtj02c, gti05c = b.gtj07c from #gtc07_buf a, gtc08 b where a.gti00c = b.gtj00c and a.gti03c = b.gtj01c and a.gti14i = b.gtj09i and a.gti08c = 'gx' and a.gti12i = 2 --换套餐的情况 delete a from gtc07 a, gam26 z where a.gti02d >= @fromdate and a.gti02d <= @todate and a.gti00c = z.gaz02c and z.gaz01c = @compid insert gtc07(gti00c, gti02d, gti03c, gti04c, gti05c, gti06f, gti07f, gti08c, gti09c, gti12i, gti13c, gti14i, gti17c, gti18f, gti19c, gti20f, gti21c, gti22f, gti23c, gti24f) select gti00c, gti02d, gti03c, gti04c, gti05c, gti06f, gti07f, gti08c, gti09c, gti12i, gti13c, gti14i, gti17c, gti18f, gti19c, gti20f, gti21c, gti22f, gti23c, gti24f from #gtc07_buf order by gti02d, ttime drop table #gtc07_buf end GO /****** Object: StoredProcedure [dbo].[upg_m_dispatch_hbm01] Script Date: 06/12/2017 06:20:21 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_m_dispatch_hbm01]( @fromcompid varchar(10), -- 从哪一个公司发布 @tocompid varchar(10), -- 目的公司 @mandatory varchar(2)) -- 是否强制统一 as begin create table #hbm01 --[岗位--薪资项目设定] ( hba00c varchar(10) not null, --公司别 hba01c varchar(5) not null, --工作岗位 hba02c varchar(5) not null, --类别(1- 发放项目, 2-扣款项目) hba03c varchar(10) not null, --基本项目代码 hba04c varchar(10) null, --项目类别(1- 固定项目 2-计算项目) hba05f float null, --金额/计算系数 hba06c varchar(5) null, --计算来源(1-出勤天数, 2-销售卡数量) ) declare @PK_Sql nvarchar(max) set @PK_Sql = ' alter table #hbm01 add constraint PK_#hbm01_' + convert(varchar(10), cast(floor(rand() * 1000000) as int)) + ' primary key clustered(hba00c, hba01c, hba02c, hba03c)' exec sp_executesql @PK_Sql insert #hbm01(hba00c, hba01c, hba02c, hba03c, hba04c, hba05f, hba06c) select hba00c, hba01c, hba02c, hba03c, hba04c, hba05f, hba06c from hbm01 where hba00c = @fromcompid if @mandatory = '1' begin delete hbm01 where hba00c = @tocompid end else begin delete a from #hbm01 a, hbm01 b where a.hba00c = @fromcompid and a.hba01c = b.hba01c and a.hba02c = b.hba02c and a.hba03c = b.hba03c and b.hba00c = @tocompid end insert hbm01(hba00c, hba01c, hba02c, hba03c, hba04c, hba05f, hba06c) select @tocompid, hba01c, hba02c, hba03c, hba04c, hba05f, hba06c from #hbm01 where hba00c = @fromcompid drop table #hbm01 end GO /****** Object: StoredProcedure [dbo].[upg_update_gam02] Script Date: 06/12/2017 06:20:49 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_update_gam02] ( @useid varchar(10) -- 使用者编号 ) as--WUXM, 2004/12/31 begin delete a from gam02 a where gab01c = @useid and gab02c = '1' and gab03c not in (select gam04c from gam12, gam02 b where gam01c = b.gab03c and gam03c = '1' and b.gab01c = a.gab01c and b.gab02c = '3') end GO /****** Object: StoredProcedure [dbo].[upg_SetRight] Script Date: 06/12/2017 06:20:41 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_SetRight] ( @Actor varchar(5) ) as-- wuxinming, 2002/02/22 begin delete from gam13 where gan01c = @Actor if (@Actor = 1) insert into gam13 select @Actor, program, 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', system, 'Y', 'Y', 'Y', 'Y', 'Y', 'Y' from modulestruct where version > '' and isnull(program, '') <> '' and system in (select gam04c from gam12 where gam01c = @Actor) else insert into gam13 select @Actor, program, 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', system, 'N', 'N', 'N', 'N', 'N', 'N' from modulestruct where version > '' and isnull(program, '') <> '' and system in (select gam04c from gam12 where gam01c = @Actor) end GO /****** Object: StoredProcedure [dbo].[upg_pay_together_add] Script Date: 06/12/2017 06:20:25 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_pay_together_add] ( @compid varchar(10), -- 公司别 @keystr varchar(1000), -- 所有一起支付的手牌 @rootcard varchar(20), -- 根手牌 @date varchar(8), -- 日期 @time varchar(8) -- 时间 ) as -- 插入绑定的手牌 begin declare @card varchar(20) if(@rootcard='') return declare cur_get_each_card cursor for select line from dbo.fun_split(@keystr,',') open cur_get_each_card fetch cur_get_each_card into @card while @@fetch_status=0 begin print 'each card ='+@card if not exists (select 1 from openpolicy_pay_together a where isnull(billflag,0) in(0,3) and handcard=@card and a.compid=@compid) begin print 'insert card = ' + @card insert openpolicy_pay_together(compid,operationdate,operationtime,rootcard,handcard) values(@compid,@date,@time,@rootcard,@card) end fetch cur_get_each_card into @card end close cur_get_each_card deallocate cur_get_each_card end GO /****** Object: StoredProcedure [dbo].[upg_pay_together] Script Date: 06/12/2017 06:20:25 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_pay_together] ( @compid varchar(10), -- 公司别 @keystr varchar(1000), -- 所有一起支付的手牌 @date varchar(8), -- 日期 @time varchar(8) -- 时间 ) as -- 插入绑定的手牌 begin declare @card varchar(20) declare @firstcard varchar(20) declare @cnt int select @cnt = 0 declare cur_get_each_card cursor for select line From dbo.fun_split(@keystr,',') open cur_get_each_card fetch cur_get_each_card into @card while @@fetch_status=0 begin if(@cnt=0) select @firstcard=@card insert openpolicy_pay_together(compid,operationdate,operationtime,rootcard,handcard) values(@compid,@date,@time,@firstcard,@card) set @cnt=@cnt+1 fetch cur_get_each_card into @card end close cur_get_each_card deallocate cur_get_each_card end GO /****** Object: StoredProcedure [dbo].[upg_delete_pay_together] Script Date: 06/12/2017 06:19:59 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_delete_pay_together] ( @compid varchar(10), @confirmer varchar(20), -- 删除确认人 @type int, -- 1-删除整个团队 2-一个客户手牌 @rootcard varchar(20), -- 根手牌 @operationdate varchar(10), -- 绑定日期 @operationtime varchar(10), -- 绑定时间 @id int -- id ) as begin declare @curdate varchar(20),@time varchar(20) exec upg_getcurrdate @curdate output,@time output if( @type = 1) begin insert openpolicy_pay_together_del(id,compid,operationdate,operationtime,rootcard,handcard,userid,ddate,ttime) select id,compid,operationdate,operationtime,rootcard,handcard,@confirmer,@curdate,@time from openpolicy_pay_together where compid=@compid and rootcard = @rootcard and operationdate=@operationdate and operationtime=@operationtime delete openpolicy_pay_together where compid=@compid and rootcard = @rootcard and operationdate=@operationdate and operationtime=@operationtime end if( @type=2) begin insert openpolicy_pay_together_del(id,compid,operationdate,operationtime,rootcard,handcard,userid,ddate,ttime) select id,compid,operationdate,operationtime,rootcard,handcard,@confirmer,@curdate,@time from openpolicy_pay_together where compid=@compid and id = @id delete openpolicy_pay_together where compid=@compid and id = @id end end GO /****** Object: StoredProcedure [dbo].[upg_recover_GIA04I_sp2] Script Date: 06/12/2017 06:20:32 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_recover_GIA04I_sp2] as begin -- 判断是否已做数据恢复 if exists (select 1 from data_recover where rdd00c = '001' and rdd01c = 'GIA04I' and rdd02c = 'GIA04I_sp2') begin return end update gam03 set gac13c = 'Y' where gac02c = 'GIA04I' update gzm02 set gzb13c = 'Y' where gzb02c = 'GIA04I' update gam13 set gan13c = 'Y' where gan02c = 'GIA04I' insert into data_recover(rdd00c, rdd01c, rdd02c) values ('001', 'GIA04I', 'GIA04I_sp2') end GO /****** Object: StoredProcedure [dbo].[upg_recover_GMA02R_sp123] Script Date: 06/12/2017 06:20:32 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_recover_GMA02R_sp123] as begin -- 判断是否已做数据恢复 if exists (select 1 from data_recover where rdd00c = '001' and rdd01c = 'GMA02R' and rdd02c = 'GMA02R_sp123') begin return end update gam03 set gac12c = 'Y', gac13c = 'Y', gac14c = 'Y' where gac02c = 'GMA02R' update gzm02 set gzb12c = 'Y', gzb13c = 'Y', gzb14c = 'Y' where gzb02c = 'GMA02R' update gam13 set gan12c = 'Y', gan13c = 'Y', gan14c = 'Y' where gan02c = 'GMA02R' insert into data_recover(rdd00c, rdd01c, rdd02c) values ('001', 'GMA02R', 'GMA02R_sp123') end GO /****** Object: StoredProcedure [dbo].[upg_check_hcm01] Script Date: 06/12/2017 06:19:40 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_check_hcm01] ( @hca00c varchar(10), -- 公司别 @hca01c varchar(20), -- 发薪单号 @hca91c varchar(10), -- 登录人 @confirm int, -- 0-审批通过 1-审批不通过 @reason varchar(1000) -- 描述 ) as begin begin transaction begin try if not exists(select 1 from hcm01 where hca00c = @hca00c and hca01c = @hca01c) begin return end if not exists(select 1 from hcm02 where hcb00c = @hca00c and hcb01c = @hca01c) begin return end declare @id int declare @hca93c varchar(10) -- 复核人 declare @hca94d varchar(8) -- 复核日期 select @id = id, @hca93c = hca93c, @hca94d = hca94d from hcm01 where hca00c = @hca00c and hca01c = @hca01c declare @state int select top 1 @state = state from hcm01_operation where hcId = @id order by id desc if @confirm = 0 begin if isnull(@state, 0) = 1 and (isnull(@hca93c, '') = '' or isnull(@hca94d, '') = '') begin update hcm01 set hca93c = @hca91c, hca94d = replace( convert(varchar(20), getdate(), 23), '-', '') where hca00c = @hca00c and hca01c = @hca01c insert into hcm01_operation(hcId, operation, operator, state, description) values(@id, 2, @hca91c, 2, @reason) end end else begin if isnull(@state, 0) = 1 and (isnull(@hca93c, '') = '' or isnull(@hca94d, '') = '') begin insert into hcm01_operation(hcId, operation, operator, state, description) values(@id, 3, @hca91c, 0, @reason) end end end try begin catch if @@TRANCOUNT > 0 begin rollback transaction declare @SP_Parameters varchar(max) set @SP_Parameters = '@hca00c = ' + isnull(@hca00c, '') + '@hca01c = ' + isnull(@hca01c, '') + '@hca91c = ' + isnull(@hca91c, '') + '@confirm = ' + convert(varchar(100), isnull(@confirm, -1)) + '@reason = ' + isnull(@reason, '') insert into SP_TranLog (SPName ,SPTime ,SPParameter) values ( 'upg_check_hcm01' ,getdate() ,@SP_Parameters) raiserror ('transaction is failed.', 16, 1) WITH NOWAIT end end catch if @@TRANCOUNT > 0 begin commit transaction end end GO /****** Object: StoredProcedure [dbo].[upg_insert_hcm01] Script Date: 06/12/2017 06:20:16 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_insert_hcm01] ( @hca00c varchar(10), -- 公司别 @hca01c varchar(20), -- 发薪单号 @hca02d varchar(8), -- 发薪日期 @hca03c varchar(6), -- 薪资月份 @hca05d varchar(8), -- 计薪起始日期 @hca06d varchar(8), -- 计薪终止日期 @hca09i int, -- 税率计算(1-计算, 2-不计算) @hca11f float, -- 本月工作天数 @hca91c varchar(10), -- 登录人 @hca92d varchar(8) -- 登录日期 ) as begin begin transaction begin try -- 存在同门店 相同发薪单号 if exists(select 1 from hcm01 where hca00c = @hca00c and hca01c = @hca01c) begin select -1 return end -- 存在同门店, 同一发薪月份 if exists(select 1 from hcm01 where hca00c = @hca00c and hca03c = @hca03c) begin select -2 return end insert into hcm01(hca00c, hca01c, hca02d, hca03c, hca05d, hca06d, hca09i, hca11f, hca91c, hca92d) values(@hca00c, @hca01c, @hca02d, @hca03c, @hca05d, @hca06d, @hca09i, @hca11f, @hca91c, @hca92d) declare @id int select @id = id from hcm01 where hca00c = @hca00c and hca01c = @hca01c insert into hcm01_operation(hcId, operation, operator, state) values(@id, 0, @hca91c, 0) select @id end try begin catch if @@TRANCOUNT > 0 begin rollback transaction declare @SP_Parameters varchar(max) set @SP_Parameters = '@hca00c = ' + isnull(@hca00c, '') + '@hca01c = ' + isnull(@hca01c, '') + '@hca02d = ' + isnull(@hca02d, '') + '@hca03c = ' + isnull(@hca03c, '') + '@hca05d = ' + isnull(@hca05d, '') + '@hca06d = ' + isnull(@hca06d, '') + '@hca09i = ' + convert(varchar(100), isnull(@hca09i, -1)) + '@hca11f = ' + convert(varchar(100), isnull(@hca11f, -1)) + '@hca91c = ' + isnull(@hca91c, '') + '@hca92d = ' + isnull(@hca92d, '') insert into SP_TranLog (SPName ,SPTime ,SPParameter) values ( 'upg_insert_hcm01' ,getdate() ,@SP_Parameters) raiserror ('transaction is failed.', 16, 1) WITH NOWAIT end end catch if @@TRANCOUNT > 0 begin commit transaction end end GO /****** Object: StoredProcedure [dbo].[upg_commit_hcm01_for_check] Script Date: 06/12/2017 06:19:43 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_commit_hcm01_for_check] ( @hca00c varchar(10), -- 公司别 @hca01c varchar(20), -- 发薪单号 @hca91c varchar(10) -- 登录人 ) as begin begin transaction begin try if not exists(select 1 from hcm01 where hca00c = @hca00c and hca01c = @hca01c) begin return end if not exists(select 1 from hcm02 where hcb00c = @hca00c and hcb01c = @hca01c) begin return end declare @id int declare @hca93c varchar(10) -- 复核人 declare @hca94d varchar(8) -- 复核日期 select @id = id, @hca93c = hca93c, @hca94d = hca94d from hcm01 where hca00c = @hca00c and hca01c = @hca01c declare @state int select top 1 @state = state from hcm01_operation where hcId = @id order by id desc if isnull(@state, 0) <> 0 or (isnull(@hca93c, '') <> '' and isnull(@hca94d, '') <> '') begin return end insert into hcm01_operation(hcId, operation, operator, state) values(@id, 1, @hca91c, 1) end try begin catch if @@TRANCOUNT > 0 begin rollback transaction declare @SP_Parameters varchar(max) set @SP_Parameters = '@hca00c = ' + isnull(@hca00c, '') + '@hca01c = ' + isnull(@hca01c, '') + '@hca91c = ' + isnull(@hca91c, '') insert into SP_TranLog (SPName ,SPTime ,SPParameter) values ( 'upg_commit_hcm01_for_check' ,getdate() ,@SP_Parameters) raiserror ('transaction is failed.', 16, 1) WITH NOWAIT end end catch if @@TRANCOUNT > 0 begin commit transaction end end GO /****** Object: StoredProcedure [dbo].[upg_getdata_for_prepare_day_account] Script Date: 06/12/2017 06:20:10 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_getdata_for_prepare_day_account] ( @compid varchar(10), @ddate varchar(10), @lastremain float output, @today_save float output, @lastday_save float output, @total_in_cash float output, @total_in_credit float output, @total_out float output, @total_check float output -- 支票,目前默认为$ ) as begin if exists(select 1 from gqm03 where gqc00c = @compid and gqc01d<@ddate) begin select top 1 @lastremain = gqc13f from gqm03 where gqc00c = @compid and gqc01d<@ddate order by gqc01d desc end else begin select @lastremain = gqc02f from gqm03 where gqc00c = @compid and gqc01d = @ddate end --当日存款 select @today_save = sum(gqb05f) from gqm02 b, gqm01 a where b.gqb00c = a.gqa00c and b.gqb01c = a.gqa01c and b.gqb03c = 'A' and a.gqa80d = @ddate and a.gqa00c = @compid --昨日余额存款 select @lastday_save = sum(gqb05f) from gqm02 b, gqm01 a where b.gqb00c = a.gqa00c and b.gqb01c = a.gqa01c and b.gqb03c = 'B' and a.gqa80d = @ddate and a.gqa00c = @compid --计算当日收入,现金,银行卡(POS) select @total_in_cash = sum(gsc05f) from gsm03 where gsc00c = @compid and gsc04c = '1' and gsc10d = @ddate select @total_check = sum(gsc05f) from gsm03 where gsc00c = @compid and gsc04c = '$' and gsc10d = @ddate select @total_in_credit = sum(gsc05f) from gsm03 where gsc00c = @compid and gsc04c = '6' and gsc10d = @ddate --其他费用支出 select @total_out = sum(gqb05f) from gqm02 b, gqm01 a where b.gqb00c = a.gqa00c and b.gqb01c = a.gqa01c and b.gqb03c<>'B' and b.gqb03c<>'A' and a.gqa80d = @ddate and a.gqa00c = @compid --and isnull(b.gqb19i, 0) = 0 select @lastremain = isnull(@lastremain, 0), @today_save = isnull(@today_save, 0), @lastday_save = isnull(@lastday_save, 0), @total_in_cash = isnull(@total_in_cash, 0), @total_in_credit = isnull(@total_in_credit, 0), @total_out = isnull(@total_out, 0) end GO /****** Object: StoredProcedure [dbo].[upg_updategqm01_accountdate] Script Date: 06/12/2017 06:20:51 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_updategqm01_accountdate] ( @compid varchar(10), --公司编号 @consumeid varchar(20) --消费单号 ) as --WUXM 2005/08/16 begin declare @begtime varchar(20) --工作日开始时间 declare @endtime varchar(20) --工作日结束时间,表示凌晨时间,如果为000000,则表示帐务日期和实际日期一致 declare @time varchar(6) --实际结帐时间 declare @date varchar(8) --实际日期 select @begtime = gsa02c from gsm01 where gsa00c = @compid and gsa01c = 'SP050' select @endtime = gsa02c from gsm01 where gsa00c = @compid and gsa01c = 'SP051' declare @curtime varchar(8) declare @curdate varchar(10) select @curtime = gqa13t, @curdate = gqa02d from gqm01 where gqa00c = @compid and gqa01c = @consumeid select @time = @curtime select @date = @curdate declare @newdate varchar(8) declare @strdate varchar(10) --帐务日期比正常日期短 if (@endtime > @begtime and @time > @endtime) begin select @strdate = convert(varchar(10), dateadd(day, 1, cast(@date as datetime)), 120) select @newdate = substring(@strdate, 1, 4) + substring(@strdate, 6, 2) + substring(@strdate, 9, 2) update a set a.gqa80d = @newdate from gqm01 a where gqa00c = @compid and gqa01c = @consumeid end --帐务日期比正常日期长 else if (@endtime <= @begtime and @time <= @endtime) begin select @strdate = convert(varchar(10), dateadd(day, -1, cast(@date as datetime)), 120) select @newdate = substring(@strdate, 1, 4) + substring(@strdate, 6, 2) + substring(@strdate, 9, 2) update a set a.gqa80d = @newdate from gqm01 a where gqa00c = @compid and gqa01c = @consumeid end --帐务日期和正常日期一致 else begin update a set a.gqa80d = @date from gqm01 a where gqa00c = @compid and gqa01c = @consumeid end end GO /****** Object: StoredProcedure [dbo].[upg_copy_gam14] Script Date: 06/12/2017 06:19:46 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_copy_gam14] ( @compid varchar(10), @from varchar(20), @to varchar(20) ) as begin delete gam14 where gal00c = @compid and gal01c = @to insert gam14(gal00c, gal01c, gal02i, gal03f) select gal00c, @to, gal02i, gal03f from gam14 where gal00c = @compid and gal01c = @from end GO /****** Object: StoredProcedure [dbo].[upg_gbm19_Add] Script Date: 06/12/2017 06:20:03 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_gbm19_Add] ( @company varchar(10) --门店编号 ) as begin declare @nowdate varchar(10) --当天时间 Select @nowdate = CONVERT(varchar(10), GETDATE(), 112) --判断当天门店是否有数据 if(exists(select 1 from gbm19 where gbs00c = @company and gbs01d = @nowdate)) begin return 0 end create table #gcm06_date ( gcf00c varchar(10) not null, --门店编号 gcf01c varchar(20) not null, --卡号 gcf02c varchar(20) not null, --项目编号 gcf05f float null, --总次数 gcf06f float null, --已经使用次数 gcf07f float null, --剩余次数 gcf13i int null, --疗程间隔 gcf19i int null, --是否停用 2停用 1正常 3买产品赠送项目 gcf22i int null, --永久免费 startdate varchar(10) null, -- 开始疗程日期 laidian varchar(10) null, --最近来店 tel varchar(30) null, --手机号 ) create nonclustered index gcm06_date_index_01 ON #gcm06_date(gcf00c,gcf01c) --会员卡--疗程 insert into #gcm06_date(gcf00c,gcf01c,gcf02c,gcf05f,gcf06f,gcf07f,gcf13i,gcf19i,gcf22i,startdate,laidian) select gcf00c,gcf01c,gcf02c,sum(isnull(gcf05f,0)),sum(isnull(gcf06f,0)),sum(isnull(gcf07f,0)),min(isnull(gcf13i,7)),min(isnull(gcf19i,0)),min(isnull(gcf22i,0)),'','' from gcm06 where gcf00c = @company and gcf19i = 1 ----是否停用 2停用 1正常 3买产品赠送项目 and gcf07f > 0 group by gcf00c,gcf01c,gcf02c Create table #tb_gbm01 ( gba00c varchar(10), --门店 gba08c varchar(30), --手机号 gba23c varchar(60), --卡号 ) create nonclustered index tb_gbm01_index_01 ON #tb_gbm01(gba00c,gba23c) Create table #tb_ggm01 ( gga00c varchar(10), gga09c varchar(60), gga94d varchar(10), ggb47i int ) create nonclustered index tb_ggm01_index_01 ON #tb_ggm01(gga00c,gga09c) insert into #tb_gbm01(gba00c,gba08c,gba23c) select gba00c, --门店 gba08c, --手机号 gba23c --卡号 from gbm01 with(nolock) where gba00c = @company and isnull(gba08c,'') != '' insert into #tb_ggm01(gga00c,gga09c,gga94d,ggb47i) select gga00c, gga09c, --会员卡号 gga94d, isnull(ggb47i,-1) ggb47i from ggm01 with(nolock) inner join ggm02 with(nolock) on gga00c = ggb00c and gga01c = ggb01c where gga00c = @company --更新手机号 update a set tel = b.gba08c from #gcm06_date a,( select gba08c, --手机号 gba23c, --卡号 gba00c --门店 from #tb_gbm01 where gba23c in (select gcf01c from #gcm06_date) ) as b where a.gcf00c = b.gba00c and a.gcf01c = b.gba23c --更新疗程日期 --取该项目最后一次疗程日期 update a set a.startdate = b.gga94d from #gcm06_date a,( select gga00c, gga09c, --会员卡号 max(gga94d) gga94d from #tb_ggm01 where ggb47i <> -1 group by gga00c,gga09c ) as b where gcf00c = gga00c and gcf01c = gga09c and isnull(tel,'') = '' update a set a.startdate = b.gga94d from #gcm06_date a,( select gga00c, gba08c, --手机号 max(gga94d) gga94d from #tb_ggm01 inner join #tb_gbm01 on gba00c = gga00c and gga09c = gba23c where gga00c = @company and ggb47i <> -1 group by gga00c,gba08c ) as b where gcf00c = gga00c and isnull(tel,'') != '' and tel = gba08c --最近来店:客户最近来店,不管是不是疗程 update a set a.laidian = b.gga94d from #gcm06_date a,( select gga00c, gga09c, --会员卡号 max(gga94d) gga94d from #tb_ggm01 group by gga00c,gga09c ) as b where gcf00c = gga00c and gcf01c = gga09c and isnull(tel,'') = '' update a set a.laidian = b.gga94d from #gcm06_date a,( select gga00c, gba08c, --手机号 max(gga94d) gga94d from #tb_ggm01 inner join #tb_gbm01 on gba00c = gga00c and gga09c = gba23c where gga00c = @company group by gga00c,gba08c ) as b where gcf00c = gga00c and isnull(tel,'') != '' and tel = gba08c --清掉没有日期的数据 delete from #gcm06_date where isnull(startdate,'') = '' or -datediff(dd,@nowdate,convert(datetime,startdate)) = 0 -- 更新今日疗程提醒表 insert into gbm19(gbs00c,gbs01d,gbs02c,gbs03c,gbs04i,gbs05f,gbs06f,gbs07f,gbs08f,gbs09c,gbs10i,gbs11i) select gcf00c, @nowdate, gcf01c, gcf02c, 0, gcf05f, gcf06f, gcf07f, 0, '', -datediff(dd,@nowdate,convert(datetime,startdate)), -datediff(dd,@nowdate,convert(datetime,laidian)) from #gcm06_date where -datediff(dd,@nowdate,convert(datetime,startdate)) % 7 = 0 drop table #gcm06_date drop table #tb_ggm01 drop table #tb_gbm01 end GO /****** Object: StoredProcedure [dbo].[upg_gbm19_Verify] Script Date: 06/12/2017 06:20:03 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_gbm19_Verify] ( @company varchar(10), -- 门店编号 @billno varchar(30), -- 消费单号 @verfiy int -- 1审核,0反审 ) as begin create table #ggm01 ( gga00c varchar(10) not null, --门店编号 gga01c varchar(20) not null, --消费单号 gga09c varchar(20) null, --会员卡号 ggb03c varchar(20) null, --项目代码 ggb05f float null, --数量 gga94d varchar(10) null --日期 ) insert into #ggm01 select gga00c, gga01c, gga09c, --会员卡号 ggb03c, --项目编号 ggb05f, --数量 gga94d from ggm02 with(nolock) inner join ggm01 with(nolock) on ggb00c = gga00c and ggb01c = gga01c where gga00c = @company and gga01c = @billno and ggb47i <> -1 declare @gga00c varchar(10) --门店编号 declare @gga01c varchar(20) --消费单号 declare @gga09c varchar(20) --会员卡号 declare @ggb03c varchar(20) --项目代码 declare @ggb05f float --数量 declare @gga94d varchar(10) --当天时间 --Select @nowdate = CONVERT(varchar(10), GETDATE(), 112) declare ggm01_cursor cursor for select gga00c, gga01c, gga09c, --会员卡号 ggb03c, --项目编号 ggb05f, --数量 gga94d from #ggm01 open ggm01_cursor fetch next from ggm01_cursor into @gga00c,@gga01c,@gga09c,@ggb03c,@ggb05f,@gga94d while(@@fetch_status = 0) begin if(exists(select 1 from gbm19 where gbs00c = @gga00c and gbs01d = @gga94d and gbs02c = @gga09c and gbs03c = @ggb03c )) begin if(@verfiy = 1) --审核 update gbm19 set gbs04i = 1 , gbs08f = isnull(gbs08f,0) + @ggb05f, gbs09c = @gga01c where gbs00c = @gga00c and gbs01d = @gga94d and gbs02c = @gga09c and gbs03c = @ggb03c else update gbm19 set gbs04i = 0 , gbs08f = isnull(gbs08f,0) - @ggb05f, gbs09c = '' where gbs00c = @gga00c and gbs01d = @gga94d and gbs02c = @gga09c and gbs03c = @ggb03c end else --gbm19中不存在, begin if(@verfiy = 1) --审核 begin declare @juli int declare @nowdate varchar(10) --当天时间 Select @nowdate = CONVERT(varchar(10), GETDATE(), 112) select @juli = -datediff(dd,@nowdate,convert(datetime, max(gga94d))) from ggm02 with(nolock) inner join ggm01 with(nolock) on ggb00c = gga00c and ggb01c = gga01c where gga00c = @company and gga01c != @billno and gga09c in (select gga09c from #ggm01) and ggb47i <> -1 insert into gbm19(gbs00c,gbs01d,gbs02c,gbs03c,gbs04i,gbs05f,gbs06f,gbs07f,gbs08f,gbs09c,gbs10i) select @gga00c, @gga94d, gcf01c, gcf02c, 2, --未预约完成 sum(gcf05f), sum(gcf06f), sum(gcf07f), @ggb05f, @gga01c, isnull(@juli,0) from gcm06 with(nolock) where gcf01c = @gga09c and gcf02c = @ggb03c group by gcf01c,gcf02c end else --反审,则删除 delete from gbm19 where gbs00c = @gga00c and gbs01d = @gga94d and gbs02c = @gga09c and gbs03c = @ggb03c and gbs04i = 2 end fetch next from ggm01_cursor into @gga00c,@gga01c,@gga09c,@ggb03c,@ggb05f,@gga94d end close ggm01_cursor deallocate ggm01_cursor end GO /****** Object: StoredProcedure [dbo].[upg_grade_gmb18i] Script Date: 06/12/2017 06:20:12 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create proc [dbo].[upg_grade_gmb18i] ( @companyId varchar(30), --公司编号 @xmdl varchar(30), --项目大类 @fromjuli int, --多久没来 @tojuli int, --多久没来 @fromcs int, --来店次数 @tocs int, --来店次数 @fromjine float, --平均消费金额 @tojine float, --平均消费金额 @fromdate varchar(10), --消费时间 @todate varchar(10), --消费时间 @fromlcsale float, --购买能力(疗程销售) @tolcsale float, --购买能力(疗程销售) @frommaxjine float, --单笔消费 @tomaxjine float, --单笔消费 @frompinlu float, --来店频率(消费频率) @topinlu float, --来店频率(消费频率) @gbr04i int --会员等级 ) as begin create table #grade_ls ( gba00c varchar(10), -- 公司编号 gba01c varchar(30), -- 会员编号 gba03c varchar(60), -- 会员姓名 gna26f float, -- 办卡(卡销售) gcl07f float, -- 卡充值金额 lcxfnl float, -- 疗程消费金额(消费能力) lcsale float, -- 购买疗程(办卡) lccs float, -- 疗程总次数 lczs float, -- 疗程赠送次数 lcxhcs float, -- 疗程消耗次数 lcxhjine float, -- 疗程消耗金额 lcfromtime varchar(10), -- 疗程第一次消费日期 lctotime varchar(10), -- 疗程最后一次消费日期 jine float, -- 消费总金额 maxjine float, -- 最大单笔消费金额 juli float, -- 多久没来 cishu float, -- 消费总次数 pinlu float, -- 消费频率 pjxh float, -- 平均消费金额 ggc11f float, --外卖品 startime varchar(30), --第一次消费时间 endtime varchar(30), --最近消费时间 gba08c varchar(30), -- Mobile No. gca01c varchar(30) --会员卡号 ) insert into #grade_ls exec upg_grade_gmb18i_query @companyId,@xmdl,@fromjuli,@tojuli, @fromcs,@tocs,@fromjine,@tojine, @fromdate,@todate,-1,-1,@fromlcsale,@tolcsale,@frommaxjine,@tomaxjine, @frompinlu,@topinlu ----更新已经存在的会员 update gbm18 Set gbr04i = @gbr04i where gbr00c = @companyId and gbr02c = @xmdl and gbr01c in (select gba01c from #grade_ls) --新增不存在的 insert into gbm18(gbr00c,gbr01c,gbr02c,gbr03c,gbr04i,gbr05i,gbr06c,gbr90c,gbr91d) select @companyId,gba01c,@xmdl,'',@gbr04i,-1,'自动更新','',@todate from #grade_ls where gba01c not in (select gbr01c from gbm18 where gbr00c = @companyId and gbr02c = @xmdl) --更改自动更新设置表日期 update grade_setting set rsb07d = @todate where rsb00c = @companyId and rsb01c = @xmdl --部门 and rsb02i = @gbr04i --等级 drop table #grade_ls end GO /****** Object: StoredProcedure [dbo].[upg_report_gmn05r] Script Date: 06/12/2017 06:20:36 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create proc [dbo].[upg_report_gmn05r] ( @year int, @startmonth int, @endmonth int, @companyid varchar(10), -- 门店 @yuangong varchar(30), -- 员工 @rad int -- 0-劳动,1-现金 ) as begin if (@rad = 0) --劳动业绩 begin select Convert(varchar(10), month(convert(datetime,srvdate))) + '月' srvmonth, Convert(decimal(19,2),sum(amt2)) amt2, --虚业绩 Convert(decimal(19,2),sum(amt3)) amt3 --真实业绩 from work_detail with(nolock) where cls_flag = 1 and ( @companyid = '' or compid = @companyid ) and ( @yuangong = '' or person_id = @yuangong ) and year(convert(datetime,srvdate)) = @year and month(convert(datetime,srvdate)) >= @startmonth and month(convert(datetime,srvdate)) <= @endmonth group by month(convert(datetime,srvdate)) end else --现金业绩 begin select Convert(varchar(10), month(convert(datetime,srvdate))) + '月' srvmonth, Convert(decimal(19,2),sum(amt2)) amt2, --虚业绩 Convert(decimal(19,2),sum(amt3)) amt3 --真实业绩 from work_detail with(nolock) where cls_flag != 1 and ( @companyid = '' or compid = @companyid ) and ( @yuangong = '' or person_id = @yuangong ) and year(convert(datetime,srvdate)) = @year and month(convert(datetime,srvdate)) >= @startmonth and month(convert(datetime,srvdate)) <= @endmonth group by month(convert(datetime,srvdate)) end end GO /****** Object: StoredProcedure [dbo].[upg_report_gwn06r] Script Date: 06/12/2017 06:20:37 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO Create proc [dbo].[upg_report_gwn06r] ( @fromdate varchar(10), --起始日期 @todate varchar(10), --结束日期 @type int, --0 劳动,1 现金 @rad int -- 0 男女,1 会员,非会员, 2卡销售,3 卡充值, 4 外卖品 ) as begin if( @type = 0) --劳动 begin if( @rad = 0) --性别 begin select compid, case gender when 0 then '女' when 1 then '男' else '未知' end [name], sum(amt2) amt2, sum(amt3) amt3 from work_detail with(nolock) where cls_flag = 1 and ( amt2 > 0 or amt3 > 0 ) and srvdate between @fromdate and @todate group by compid,gender order by compid end else if( @rad = 1) --会员,非会员 begin select compid, '非会员' [name], sum(amt2) amt2, sum(amt3) amt3 from work_detail with(nolock) where cls_flag = 1 and srvdate between @fromdate and @todate and (memid is null or memid = '') and amt3 > 0 group by compid union all select compid, '会员' [name], sum(amt2) amt2, sum(amt3) amt3 from work_detail with(nolock) where cls_flag = 1 and srvdate between @fromdate and @todate and memid is not null and memid != '' and amt3 > 0 group by compid end end else --现金 begin if( @rad = 0)--性别 begin select compid, case gender when 0 then '女' when 1 then '男' else '未知' end [name], sum(amt2) amt2, sum(amt3) amt3 from work_detail with(nolock) where cls_flag != 1 and ( amt2 > 0 or amt3 > 0 ) and srvdate between @fromdate and @todate group by compid,gender order by compid end else if( @rad = 1) --会员,非会员 begin select compid, '非会员' [name], sum(amt2) amt2, sum(amt3) amt3 from work_detail with(nolock) where cls_flag != 1 and srvdate between @fromdate and @todate and (memid is null or memid = '') and amt3 > 0 group by compid union all select compid, '会员' [name], sum(amt2) amt2, sum(amt3) amt3 from work_detail with(nolock) where cls_flag != 1 and srvdate between @fromdate and @todate and memid is not null and memid != '' and amt3 > 0 group by compid end else if( @rad = 2 ) --卡销售 begin select compid, case gender when 0 then '女' when 1 then '男' else '未知' end [name], sum(amt2) amt2, sum(amt3) amt3 from work_detail with(nolock) where action_id = 1 and srvdate between @fromdate and @todate group by compid,gender end else if( @rad = 3) --卡充值 begin select compid, case gender when 0 then '女' when 1 then '男' else '未知' end [name], sum(amt2) amt2, sum(amt3) amt3 from work_detail with(nolock) where action_id = 0 and srvdate between @fromdate and @todate group by compid,gender end else if( @rad = 4) --外卖品 begin select compid, case gender when 0 then '女' when 1 then '男' else '未知' end [name], sum(amt2) amt2, sum(amt3) amt3 from work_detail with(nolock) where action_id = 9 and srvdate between @fromdate and @todate group by compid,gender end end end GO /****** Object: StoredProcedure [dbo].[upg_cal_work_detail_by_item] Script Date: 06/12/2017 06:19:35 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_cal_work_detail_by_item] ( @spid int, @compid varchar(10), -- 公司别 @fromdate varchar(10), -- 开始日期 @todate varchar(10), -- 截至日期 @fromempl varchar(20), -- 查询开始人员 @toempl varchar(20) -- 查询截至人员 ) as begin if @fromempl = @toempl begin -- 只能一个员工统计分析 -- 充值的金额,视为卡的一次动作 update work_detail set quan = 1 where isnull(quan, 0) = 0 and person_id = @fromempl and spid = @spid create table #group_data_2 ( idd int identity not null, person_id varchar(20) not null, -- 员工代码 cls_flag int null, -- 1:项目 2:产品 3:卡 code varchar(20) not null, -- 项目代码, 或是卡号, 产品码 amt float null, -- 营业金额 quan float null, -- 数量 amt2 float null, -- 虚业绩 cost float null, -- 成本 amt3 float null, -- 真实业绩 comm float null, -- 提成 primary key(idd) ) create table #group_data_3 ( idd int identity not null, person_id varchar(20) not null, -- 员工代码 cls_flag int null, -- 1:项目 2:产品 3:卡 action_id tinyint not null, -- 类别, 1-卡销售 2-指定大工, 3-轮班大工, 4-补工大工, -- 5-指定中工, 6-轮班中工 -- 7-指定小工, 8-轮班小工, 9-产品销售 code varchar(20) not null, --项目代码, 或是卡号, 产品码 name varchar(40) null, --名称 gender int null, --性别 amt float null, --营业金额 quan float null, --数量 amt2 float null, --虚业绩 cost float null, --成本 amt3 float null, --真实业绩 comm float null, --提成 primary key(idd) ) delete workdetail_by_item where spid = @spid -- 按项目,服务类型统计总金额 insert workdetail_by_item(spid, compid, person_id, cls_flag, action_id, code, name, amt, quan, amt2, cost, amt3, comm) select @spid, @compid, person_id, cls_flag, action_id, code, name, amt = sum(amt), quan = sum(quan), amt2 = sum(amt2), cost = sum(cost), amt3 = sum(amt3), comm = sum(comm) from #work_detail_ceshi where person_id = @fromempl and spid = @spid group by person_id, cls_flag, action_id, code, name -- 按性别,服务类型不同 insert #group_data_3(person_id, cls_flag, action_id, code, name, gender, amt, quan, amt2, cost, amt3, comm) select person_id, cls_flag, action_id, code, name, isnull(gender, 0), amt = sum(amt), quan = sum(quan), amt2 = sum(amt2), cost = sum(cost), amt3 = sum(amt3), comm = sum(comm) from #work_detail_ceshi where person_id = @fromempl and spid = @spid group by person_id, cls_flag, action_id, code, name, isnull(gender, 0) update a set a.quan_f = b.quan, a.amt_f = b.amt from workdetail_by_item a, #group_data_3 b where spid = @spid and a.person_id = b.person_id and a.cls_flag = b.cls_flag and a.action_id = b.action_id and a.code = b.code and isnull(b.gender, 0) = 0 update a set a.quan_m = b.quan, a.amt_m = b.amt from workdetail_by_item a, #group_data_3 b where spid = @spid and a.person_id = b.person_id and a.cls_flag = b.cls_flag and a.action_id = b.action_id and a.code = b.code and isnull(b.gender, 0) = 1 drop table #group_data_3 --按项目统计总项目金额 insert #group_data_2(person_id, cls_flag, code, amt, quan, amt2, cost, amt3, comm) select person_id, cls_flag, code, amt = sum(amt), quan = sum(quan), amt2 = sum(amt2), cost = sum(cost), amt3 = sum(amt3), comm = sum(comm) from #work_detail_ceshi where spid = @spid and person_id = @fromempl group by person_id, cls_flag, code update a set a.pt_amt = b.amt, a.pt_quan = b.quan, a.pt_amt2 = b.amt2, a.pt_amt3 = b.amt3, a.pt_cost = b.cost, a.pt_comm = b.comm from workdetail_by_item a, #group_data_2 b where spid = @spid and a.person_id = b.person_id and a.cls_flag = b.cls_flag and a.code = b.code update workdetail_by_item set rt_quan = quan/pt_quan where isnull(pt_quan, 0)<>0 and spid = @spid update workdetail_by_item set rt_amt = amt/pt_amt where isnull(pt_amt, 0)<>0 and spid = @spid update workdetail_by_item set rt_amt2 = amt2/pt_amt2 where isnull(pt_amt2, 0)<>0 and spid = @spid update workdetail_by_item set rt_cost = cost/pt_cost where isnull(pt_cost, 0)<>0 and spid = @spid update workdetail_by_item set rt_amt3 = amt3/pt_amt3 where isnull(pt_amt3, 0)<>0 and spid = @spid update workdetail_by_item set rt_comm = comm/pt_comm where isnull(pt_comm, 0)<>0 and spid = @spid --统计每一组的金额占总的这个人的所有项目金额的比率 declare @person_total float select @person_total = sum(amt) from workdetail_by_item with (nolock) where spid = @spid if isnull(@person_total, 0)>0 begin update workdetail_by_item set prj_amt_rt = amt/@person_total where spid = @spid end drop table #group_data_2 end end GO /****** Object: StoredProcedure [dbo].[upg_personal_statistics_clear] Script Date: 06/12/2017 06:20:26 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_personal_statistics_clear] ( @spid int ) as begin delete work_detail where spid = @spid delete workdetail_by_kind where spid = @spid delete workdetail_by_empl where spid = @spid end GO /****** Object: StoredProcedure [dbo].[upg_recover_PNIC_Account] Script Date: 06/12/2017 06:20:33 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_recover_PNIC_Account] as begin -- 判断是否已做数据恢复 if exists (select 1 from data_recover where rdd00c = '001' and rdd01c = 'PNIC_Account' and rdd02c = 'PNIC_Account') begin return end insert into PNIC_Account(pna00c, pna01c, pna02i) select '001', gsb02c, 0 from gsm02 where gsb00c = '001' and gsb01c = 'o' and gsb02c not in('888', '999', '889') update PNIC_Account set pna02i = 1 where pna00c = '001' and pna01c in ('2', '3', '4', '5', '6', '7') insert into data_recover(rdd00c, rdd01c, rdd02c) values ('001', 'PNIC_Account', 'PNIC_Account') end GO /****** Object: StoredProcedure [dbo].[upg_create_dailydata] Script Date: 06/12/2017 06:19:51 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_create_dailydata] ( @compid varchar(10), @fromdate varchar(10), @todate varchar(10) ) as begin -- 删除这个日期范围内的日结数据,需要重新产生 delete a from gmm01 a, gam26 z where a.gma01d >= @fromdate and a.gma01d <= @todate and a.gma00c = z.gaz02c and z.gaz01c = @compid -- 项目资料的日结 insert gmm01(gma00c, gma01d, gma02i, gma03c, gma04c, gma05f, gma06f, gma07f) select ggb00c, gga80d, 1, ggb03c, ggb27c, sum(ggb05f), sum(ggb11f), sum(ggb05f) from ggm01 a with (nolock), ggm02 b with (nolock), gam26 z where a.gga00c = b.ggb00c and a.gga01c = b.ggb01c and a.gga80d >= @fromdate and a.gga80d <= @todate and a.gga00c = z.gaz02c and z.gaz01c = @compid group by ggb00c, gga80d, ggb03c, ggb27c --产品资料的日结 insert gmm01(gma00c, gma01d, gma02i, gma03c, gma04c, gma05f, gma06f, gma07f) select ggc00c, gga80d, 2, ggc03c, ggc27c, sum(ggc05f), sum(ggc11f), sum(ggc05f) from ggm01 a with (nolock), ggm03 b with (nolock), gam26 z where a.gga00c = b.ggc00c and a.gga01c = b.ggc01c and a.gga80d >= @fromdate and a.gga80d <= @todate and a.gga00c = z.gaz02c and z.gaz01c = @compid group by ggc00c, gga80d, ggc03c, ggc27c --疗程的销售 --疗程的销售分布在gcm11, 以及gnm03中,所以不能直接插入,先不考虑价格序号的差别 create table #gmm01_buf -- 日结表 -- 项目/产品 ( gma00c varchar(10) not null, --公司别 gma01d varchar(10) not null, --日期 seq int identity not null, --序号 gma03c varchar(20) not null, --项目代码/产品代码 gma04c varchar(5) not null, --支付方式 gma05f float null, --数量 gma06f float null, --金额 gma07f float null, --数量(疗程的时候是疗程次数, 产品数量, 充值卡数量) ) declare @PK_Sql nvarchar(max) set @PK_Sql = ' alter table #gmm01_buf add constraint PK_#gmm01_buf_' + convert(varchar(10), cast(floor(rand() * 1000000) as int)) + ' primary key clustered(seq)' exec sp_executesql @PK_Sql --会员卡销售疗程-支付1 insert #gmm01_buf(gma00c, gma01d, gma03c, gma04c, gma05f, gma06f, gma07f) select gna00c, gna80d, gnc02c, isnull(gnc15c, ''), sum(case isnull(gnc05f, 0) when 0 then gnc11f else gnc11f*gnc16f/gnc05f end), sum(gnc16f), sum(case isnull(gnc05f, 0) when 0 then (isnull(gnc03f, 0) + isnull(gnc04f, 0)) else (isnull(gnc03f, 0) + isnull(gnc04f, 0))*gnc16f/gnc05f end) from gnm01 a with (nolock), gnm03 b with (nolock), gam26 z where a.gna00c = b.gnc00c and a.gna01c = b.gnc01c and a.gna00c = z.gaz02c and z.gaz01c = @compid and a.gna80d >= @fromdate and a.gna80d <= @todate and isnull(gnc15c, '')<>'' group by gna00c, gna80d, gnc02c, isnull(gnc15c, '') --会员卡销售疗程-支付2 insert #gmm01_buf(gma00c, gma01d, gma03c, gma04c, gma05f, gma06f, gma07f) select gna00c, gna80d, gnc02c, isnull(gnc17c, ''), sum(case isnull(gnc05f, 0) when 0 then gnc11f else gnc11f*gnc18f/gnc05f end), sum(gnc18f), sum(case isnull(gnc05f, 0) when 0 then (isnull(gnc03f, 0) + isnull(gnc04f, 0)) else (isnull(gnc03f, 0) + isnull(gnc04f, 0))*gnc18f/gnc05f end) from gnm01 a with (nolock), gnm03 b with (nolock), gam26 z where a.gna00c = b.gnc00c and a.gna01c = b.gnc01c and a.gna00c = z.gaz02c and z.gaz01c = @compid and a.gna80d >= @fromdate and a.gna80d <= @todate and isnull(gnc17c, '')<>'' group by gna00c, gna80d, gnc02c, isnull(gnc17c, '') --会员卡销售疗程-支付3 insert #gmm01_buf(gma00c, gma01d, gma03c, gma04c, gma05f, gma06f, gma07f) select gna00c, gna80d, gnc02c, isnull(gnc19c, ''), sum(case isnull(gnc05f, 0) when 0 then gnc11f else gnc11f*gnc20f/gnc05f end), sum(gnc20f), sum(case isnull(gnc05f, 0) when 0 then (isnull(gnc03f, 0) + isnull(gnc04f, 0)) else (isnull(gnc03f, 0) + isnull(gnc04f, 0))*gnc20f/gnc05f end) from gnm01 a with (nolock), gnm03 b with (nolock), gam26 z where a.gna00c = b.gnc00c and a.gna01c = b.gnc01c and a.gna00c = z.gaz02c and z.gaz01c = @compid and a.gna80d >= @fromdate and a.gna80d <= @todate and isnull(gnc19c, '')<>'' group by gna00c, gna80d, gnc02c, isnull(gnc19c, '') --会员卡销售疗程-支付4 insert #gmm01_buf(gma00c, gma01d, gma03c, gma04c, gma05f, gma06f, gma07f) select gna00c, gna80d, gnc02c, isnull(gnc23c, ''), sum(case isnull(gnc05f, 0) when 0 then gnc11f else gnc11f*gnc24f/gnc05f end), sum(gnc24f), sum(case isnull(gnc05f, 0) when 0 then (isnull(gnc03f, 0) + isnull(gnc04f, 0)) else (isnull(gnc03f, 0) + isnull(gnc04f, 0))*gnc24f/gnc05f end) from gnm01 a with (nolock), gnm03 b with (nolock), gam26 z where a.gna00c = b.gnc00c and a.gna01c = b.gnc01c and a.gna00c = z.gaz02c and z.gaz01c = @compid and a.gna80d >= @fromdate and a.gna80d <= @todate and isnull(gnc23c, '')<>'' group by gna00c, gna80d, gnc02c, isnull(gnc23c, '') --会员账户管理购买疗程-支付1 insert #gmm01_buf(gma00c, gma01d, gma03c, gma04c, gma05f, gma06f, gma07f) select gcl00c, gcl80d, gcm02c, isnull(gcm15c, ''), sum(case isnull(gcm05f, 0) when 0 then gcm11f else gcm11f*gcm16f/gcm05f end), sum(gcm16f), sum(case isnull(gcm05f, 0) when 0 then (isnull(gcm03f, 0) + isnull(gcm04f, 0)) else (isnull(gcm03f, 0) + isnull(gcm04f, 0))*gcm16f/gcm05f end) from gcm10 a with (nolock), gcm11 b with (nolock), gam26 z where a.gcl00c = b.gcm00c and a.gcl01c = b.gcm01c and a.gcl00c = z.gaz02c and z.gaz01c = @compid and a.gcl80d >= @fromdate and a.gcl80d <= @todate and isnull(gcm15c, '')<>'' group by gcl00c, gcl80d, gcm02c, isnull(gcm15c, '') --会员账户管理购买疗程-支付2 insert #gmm01_buf(gma00c, gma01d, gma03c, gma04c, gma05f, gma06f, gma07f) select gcl00c, gcl80d, gcm02c, isnull(gcm17c, ''), sum(case isnull(gcm05f, 0) when 0 then gcm11f else gcm11f*gcm18f/gcm05f end), sum(gcm18f), sum(case isnull(gcm05f, 0) when 0 then (isnull(gcm03f, 0) + isnull(gcm04f, 0)) else (isnull(gcm03f, 0) + isnull(gcm04f, 0))*gcm18f/gcm05f end) from gcm10 a with (nolock), gcm11 b with (nolock), gam26 z where a.gcl00c = b.gcm00c and a.gcl01c = b.gcm01c and a.gcl00c = z.gaz02c and z.gaz01c = @compid and a.gcl80d >= @fromdate and a.gcl80d <= @todate and isnull(gcm17c, '')<>'' group by gcl00c, gcl80d, gcm02c, isnull(gcm17c, '') --会员账户管理购买疗程-支付3 insert #gmm01_buf(gma00c, gma01d, gma03c, gma04c, gma05f, gma06f, gma07f) select gcl00c, gcl80d, gcm02c, isnull(gcm19c, ''), sum(case isnull(gcm05f, 0) when 0 then gcm11f else gcm11f*gcm20f/gcm05f end), sum(gcm20f), sum(case isnull(gcm05f, 0) when 0 then (isnull(gcm03f, 0) + isnull(gcm04f, 0)) else (isnull(gcm03f, 0) + isnull(gcm04f, 0))*gcm20f/gcm05f end) from gcm10 a with (nolock), gcm11 b with (nolock), gam26 z where a.gcl00c = b.gcm00c and a.gcl01c = b.gcm01c and a.gcl00c = z.gaz02c and z.gaz01c = @compid and a.gcl80d >= @fromdate and a.gcl80d <= @todate and isnull(gcm19c, '')<>'' group by gcl00c, gcl80d, gcm02c, isnull(gcm19c, '') --会员账户管理购买疗程-支付4 insert #gmm01_buf(gma00c, gma01d, gma03c, gma04c, gma05f, gma06f, gma07f) select gcl00c, gcl80d, gcm02c, isnull(gcm23c, ''), sum(case isnull(gcm05f, 0) when 0 then gcm11f else gcm11f*gcm24f/gcm05f end), sum(gcm24f), sum(case isnull(gcm05f, 0) when 0 then (isnull(gcm03f, 0) + isnull(gcm04f, 0)) else (isnull(gcm03f, 0) + isnull(gcm04f, 0))*gcm24f/gcm05f end) from gcm10 a with (nolock), gcm11 b with (nolock), gam26 z where a.gcl00c = b.gcm00c and a.gcl01c = b.gcm01c and a.gcl00c = z.gaz02c and z.gaz01c = @compid and a.gcl80d >= @fromdate and a.gcl80d <= @todate and isnull(gcm23c, '')<>'' group by gcl00c, gcl80d, gcm02c, isnull(gcm23c, '') --需要 处理疗程换疗程的部分,卡数不调整,但金额需要减少 insert #gmm01_buf(gma00c, gma01d, gma03c, gma04c, gma05f, gma06f, gma07f) select hei00c, hei03d, hei04c, '9', 0, sum(0-hei05f), sum(isnull(hei06f, 0)) from hem08 a with (nolock), gam26 z where a.hei00c = z.gaz02c and z.gaz01c = @compid and a.hei03d >= @fromdate and a.hei03d <= @todate group by hei00c, hei03d, hei04c insert gmm01(gma00c, gma01d, gma02i, gma03c, gma04c, gma05f, gma06f, gma07f) select gma00c, gma01d, 3, gma03c, gma04c, sum(gma05f), sum(gma06f), sum(gma07f) from #gmm01_buf group by gma00c, gma01d, gma03c, gma04c --处理卡销售套餐部分 delete #gmm01_buf --先把结果放到临时的表#gmm01_buf中 --会员卡销售-套餐-支付方式1 insert #gmm01_buf(gma00c, gma01d, gma03c, gma04c, gma05f, gma06f, gma07f) select gti00c, gna80d, gti04c, gtf09c, sum(gtf04f*gtf10f/gtf05f), sum(gtf10f*isnull(gti09f, 0)), sum(gtf04f*gti05f*gtf10f/gtf05f) from gtc09 a with (nolock), gtc05 b with (nolock), gnm01 c with (nolock), gam26 z where a.gti00c = b.gtf00c and a.gti01c = 'gn' and a.gti02c = b.gtf01c and a.gti03c = b.gtf02c and a.gti00c = c.gna00c and a.gti02c = c.gna01c and c.gna00c = z.gaz02c and z.gaz01c = @compid and c.gna80d >= @fromdate and c.gna80d <= @todate and a.gti04c = a.gti08c and isnull(gtf09c, '')<>'' group by gti00c, gna80d, gti04c, gtf09c --会员卡销售-套餐-支付方式2 insert #gmm01_buf(gma00c, gma01d, gma03c, gma04c, gma05f, gma06f, gma07f) select gti00c, gna80d, gti04c, gtf11c, sum(gtf04f*gtf12f/gtf05f), sum(gtf12f*isnull(gti09f, 0)), sum(gtf04f*gti05f*gtf12f/gtf05f) from gtc09 a with (nolock), gtc05 b with (nolock), gnm01 c with (nolock), gam26 z where a.gti00c = b.gtf00c and a.gti01c = 'gn' and a.gti02c = b.gtf01c and a.gti03c = b.gtf02c and a.gti00c = c.gna00c and a.gti02c = c.gna01c and c.gna00c = z.gaz02c and z.gaz01c = @compid and c.gna80d >= @fromdate and c.gna80d <= @todate and a.gti04c = a.gti08c and isnull(gtf11c, '')<>'' group by gti00c, gna80d, gti04c, gtf11c --会员卡销售-套餐-支付方式3 insert #gmm01_buf(gma00c, gma01d, gma03c, gma04c, gma05f, gma06f, gma07f) select gti00c, gna80d, gti04c, gtf13c, sum(gtf04f*gtf14f/gtf05f), sum(gtf14f*isnull(gti09f, 0)), sum(gtf04f*gti05f*gtf14f/gtf05f) from gtc09 a with (nolock), gtc05 b with (nolock), gnm01 c with (nolock), gam26 z where a.gti00c = b.gtf00c and a.gti01c = 'gn' and a.gti02c = b.gtf01c and a.gti03c = b.gtf02c and a.gti00c = c.gna00c and a.gti02c = c.gna01c and c.gna00c = z.gaz02c and z.gaz01c = @compid and c.gna80d >= @fromdate and c.gna80d <= @todate and a.gti04c = a.gti08c and isnull(gtf13c, '')<>'' group by gti00c, gna80d, gti04c, gtf13c --会员卡销售-套餐-支付方式4 insert #gmm01_buf(gma00c, gma01d, gma03c, gma04c, gma05f, gma06f, gma07f) select gti00c, gna80d, gti04c, gtf15c, sum(gtf04f*gtf16f/gtf05f), sum(gtf16f*isnull(gti09f, 0)), sum(gtf04f*gti05f*gtf16f/gtf05f) from gtc09 a with (nolock), gtc05 b with (nolock), gnm01 c with (nolock), gam26 z where a.gti00c = b.gtf00c and a.gti01c = 'gn' and a.gti02c = b.gtf01c and a.gti03c = b.gtf02c and a.gti00c = c.gna00c and a.gti02c = c.gna01c and c.gna00c = z.gaz02c and z.gaz01c = @compid and c.gna80d >= @fromdate and c.gna80d <= @todate and a.gti04c = a.gti08c and isnull(gtf15c, '')<>'' group by gti00c, gna80d, gti04c, gtf15c --会员帐户管理-套餐-支付方式1 insert #gmm01_buf(gma00c, gma01d, gma03c, gma04c, gma05f, gma06f, gma07f) select gti00c, gcl80d, gti04c, gth09c, sum(gth04f*gth10f/gth05f), sum(gth10f*isnull(gti09f, 0)), sum(gth04f*gti05f*gth10f/gth05f) from gtc09 a with (nolock), gtc06 b with (nolock), gcm10 c with (nolock), gam26 z where a.gti00c = b.gth00c and a.gti01c = 'gz' and a.gti02c = b.gth01c and a.gti03c = b.gth02c and a.gti00c = c.gcl00c and a.gti02c = c.gcl01c and c.gcl00c = z.gaz02c and z.gaz01c = @compid and c.gcl80d >= @fromdate and c.gcl80d <= @todate and a.gti04c = a.gti08c and isnull(gth09c, '')<>'' group by gti00c, gcl80d, gti04c, gth09c --会员帐户管理-套餐-支付方式2 insert #gmm01_buf(gma00c, gma01d, gma03c, gma04c, gma05f, gma06f, gma07f) select gti00c, gcl80d, gti04c, gth11c, sum(gth04f*gth12f/gth05f), sum(gth12f*isnull(gti09f, 0)), sum(gth04f*gti05f*gth12f/gth05f) from gtc09 a with (nolock), gtc06 b with (nolock), gcm10 c with (nolock), gam26 z where a.gti00c = b.gth00c and a.gti01c = 'gz' and a.gti02c = b.gth01c and a.gti03c = b.gth02c and a.gti00c = c.gcl00c and a.gti02c = c.gcl01c and c.gcl00c = z.gaz02c and z.gaz01c = @compid and c.gcl80d >= @fromdate and c.gcl80d <= @todate and a.gti04c = a.gti08c and isnull(gth11c, '')<>'' group by gti00c, gcl80d, gti04c, gth11c --会员帐户管理-套餐-支付方式3 insert #gmm01_buf(gma00c, gma01d, gma03c, gma04c, gma05f, gma06f, gma07f) select gti00c, gcl80d, gti04c, gth13c, sum(gth04f*gth14f/gth05f), sum(gth14f*isnull(gti09f, 0)), sum(gth04f*gti05f*gth14f/gth05f) from gtc09 a with (nolock), gtc06 b with (nolock), gcm10 c with (nolock), gam26 z where a.gti00c = b.gth00c and a.gti01c = 'gz' and a.gti02c = b.gth01c and a.gti03c = b.gth02c and a.gti00c = c.gcl00c and a.gti02c = c.gcl01c and c.gcl00c = z.gaz02c and z.gaz01c = @compid and c.gcl80d >= @fromdate and c.gcl80d <= @todate and a.gti04c = a.gti08c and isnull(gth13c, '')<>'' group by gti00c, gcl80d, gti04c, gth13c --会员帐户管理-套餐-支付方式4 insert #gmm01_buf(gma00c, gma01d, gma03c, gma04c, gma05f, gma06f, gma07f) select gti00c, gcl80d, gti04c, gth15c, sum(gth04f*gth16f/gth05f), sum(gth16f*isnull(gti09f, 0)), sum(gth04f*gti05f*gth16f/gth05f) from gtc09 a with (nolock), gtc06 b with (nolock), gcm10 c with (nolock), gam26 z where a.gti00c = b.gth00c and a.gti01c = 'gz' and a.gti02c = b.gth01c and a.gti03c = b.gth02c and a.gti00c = c.gcl00c and a.gti02c = c.gcl01c and c.gcl00c = z.gaz02c and z.gaz01c = @compid and c.gcl80d >= @fromdate and c.gcl80d <= @todate and a.gti04c = a.gti08c and isnull(gth15c, '')<>'' group by gti00c, gcl80d, gti04c, gth15c --转移到正式的表中 insert gmm01(gma00c, gma01d, gma02i, gma03c, gma04c, gma05f, gma06f, gma07f) select gma00c, gma01d, 6, gma03c, gma04c, sum(gma05f), sum(gma06f), sum(gma07f) from #gmm01_buf group by gma00c, gma01d, gma03c, gma04c --处理会员卡销售部分 delete #gmm01_buf insert #gmm01_buf(gma00c, gma01d, gma03c, gma04c, gma05f, gma06f, gma07f) select gna00c, gna80d, gnb02c, isnull(gnb15c, ''), sum(gnb16f/isnull(gnb07f, 1)), sum(gnb16f), sum(gnb16f/isnull(gnb07f, 1)) from gnm01 a with (nolock), gnm02 b with (nolock), gam26 z where a.gna00c = b.gnb00c and a.gna01c = b.gnb01c and a.gna00c = z.gaz02c and z.gaz01c = @compid and a.gna80d >= @fromdate and a.gna80d <= @todate and isnull(gnb15c, '')<>'' and isnull(gnb07f, 0)>0 group by gna00c, gna80d, gnb02c, isnull(gnb15c, '') insert #gmm01_buf(gma00c, gma01d, gma03c, gma04c, gma05f, gma06f, gma07f) select gna00c, gna80d, gnb02c, isnull(gnb17c, ''), sum(gnb18f/isnull(gnb07f, 1)), sum(gnb18f), sum(gnb18f/isnull(gnb07f, 1)) from gnm01 a with (nolock), gnm02 b with (nolock), gam26 z where a.gna00c = b.gnb00c and a.gna01c = b.gnb01c and a.gna00c = z.gaz02c and z.gaz01c = @compid and a.gna80d >= @fromdate and a.gna80d <= @todate and isnull(gnb17c, '')<>'' and isnull(gnb07f, 0)>0 group by gna00c, gna80d, gnb02c, isnull(gnb17c, '') --第三支付方式 insert #gmm01_buf(gma00c, gma01d, gma03c, gma04c, gma05f, gma06f, gma07f) select gna00c, gna80d, gnb02c, isnull(gnb19c, ''), sum(gnb20f/isnull(gnb07f, 1)), sum(gnb20f), sum(gnb20f/isnull(gnb07f, 1)) from gnm01 a with (nolock), gnm02 b with (nolock), gam26 z where a.gna00c = b.gnb00c and a.gna01c = b.gnb01c and a.gna00c = z.gaz02c and z.gaz01c = @compid and a.gna80d >= @fromdate and a.gna80d <= @todate and isnull(gnb19c, '')<>'' and isnull(gnb07f, 0)>0 group by gna00c, gna80d, gnb02c, isnull(gnb19c, '') --第四支付方式 insert #gmm01_buf(gma00c, gma01d, gma03c, gma04c, gma05f, gma06f, gma07f) select gna00c, gna80d, gnb02c, isnull(gnb21c, ''), sum(gnb22f/isnull(gnb07f, 1)), sum(gnb22f), sum(gnb22f/isnull(gnb07f, 1)) from gnm01 a with (nolock), gnm02 b with (nolock), gam26 z where a.gna00c = b.gnb00c and a.gna01c = b.gnb01c and a.gna00c = z.gaz02c and z.gaz01c = @compid and a.gna80d >= @fromdate and a.gna80d <= @todate and isnull(gnb21c, '')<>'' and isnull(gnb07f, 0)>0 group by gna00c, gna80d, gnb02c, isnull(gnb21c, '') insert gmm01(gma00c, gma01d, gma02i, gma03c, gma04c, gma05f, gma06f, gma07f) select gma00c, gma01d, 4, gma03c, gma04c, sum(gma05f), sum(gma06f), sum(gma07f) from #gmm01_buf group by gma00c, gma01d, gma03c, gma04c --充值 delete #gmm01_buf declare @custverflag varchar(20) select @custverflag = gsa02c from gsm01 where gsa00c = @compid and gsa01c = 'SP109' if @custverflag = 'FANGZI' begin insert #gmm01_buf(gma00c, gma01d, gma03c, gma04c, gma05f, gma06f, gma07f) select gcl00c, gcl80d, gcl15c, isnull(gcl67c, ''), sum(gcl68f/gcl65f), sum(gcl68f), sum(gcl68f/gcl65f) from gcm10 a with (nolock), gam26 z where a.gcl00c = z.gaz02c and z.gaz01c = @compid and a.gcl80d >= @fromdate and a.gcl80d <= @todate and isnull(gcl67c, '')<>'' and isnull(gcl14i, 1) = 1 and isnull(gcl65f, 0)>0 and isnull(gcl68f, 0)>0 group by gcl00c, gcl80d, gcl15c, isnull(gcl67c, '') insert #gmm01_buf(gma00c, gma01d, gma03c, gma04c, gma05f, gma06f, gma07f) select gcl00c, gcl80d, gcl15c, isnull(gcl69c, ''), sum(gcl70f/gcl65f), sum(gcl70f), sum(gcl70f/gcl65f) from gcm10 a with (nolock), gam26 z where a.gcl00c = z.gaz02c and z.gaz01c = @compid and a.gcl80d >= @fromdate and a.gcl80d <= @todate and isnull(gcl69c, '')<>'' and isnull(gcl14i, 1) = 1 and isnull(gcl65f, 0)>0 and isnull(gcl70f, 0)>0 group by gcl00c, gcl80d, gcl15c, isnull(gcl69c, '') --第三支付方式 insert #gmm01_buf(gma00c, gma01d, gma03c, gma04c, gma05f, gma06f, gma07f) select gcl00c, gcl80d, gcl15c, isnull(gcl71c, ''), sum(gcl72f/gcl65f), sum(gcl72f), sum(gcl72f/gcl65f) from gcm10 a with (nolock), gam26 z where a.gcl00c = z.gaz02c and z.gaz01c = @compid and a.gcl80d >= @fromdate and a.gcl80d <= @todate and isnull(gcl71c, '')<>'' and isnull(gcl14i, 1) = 1 and isnull(gcl65f, 0)>0 and isnull(gcl72f, 0)>0 group by gcl00c, gcl80d, gcl15c, isnull(gcl71c, '') --第四支付方式 insert #gmm01_buf(gma00c, gma01d, gma03c, gma04c, gma05f, gma06f, gma07f) select gcl00c, gcl80d, gcl15c, isnull(gcl73c, ''), sum(gcl74f/gcl65f), sum(gcl74f), sum(gcl74f/gcl65f) from gcm10 a with (nolock), gam26 z where a.gcl00c = z.gaz02c and z.gaz01c = @compid and a.gcl80d >= @fromdate and a.gcl80d <= @todate and isnull(gcl73c, '')<>'' and isnull(gcl14i, 1) = 1 and isnull(gcl65f, 0)>0 and isnull(gcl74f, 0)>0 group by gcl00c, gcl80d, gcl15c, isnull(gcl73c, '') end else begin insert #gmm01_buf(gma00c, gma01d, gma03c, gma04c, gma05f, gma06f, gma07f) select gcl00c, gcl80d, gcl15c, isnull(gcl67c, ''), sum(gcl68f/gcl65f), sum(gcl68f), sum(gcl68f/gcl65f) from gcm10 a with (nolock), gam26 z where a.gcl00c = z.gaz02c and z.gaz01c = @compid and a.gcl80d >= @fromdate and a.gcl80d <= @todate and isnull(gcl67c, '')<>'' and isnull(gcl65f, 0)>0 and isnull(gcl68f, 0)>0 group by gcl00c, gcl80d, gcl15c, isnull(gcl67c, '') insert #gmm01_buf(gma00c, gma01d, gma03c, gma04c, gma05f, gma06f, gma07f) select gcl00c, gcl80d, gcl15c, isnull(gcl69c, ''), sum(gcl70f/gcl65f), sum(gcl70f), sum(gcl70f/gcl65f) from gcm10 a with (nolock), gam26 z where a.gcl00c = z.gaz02c and z.gaz01c = @compid and a.gcl80d >= @fromdate and a.gcl80d <= @todate and isnull(gcl69c, '')<>'' and isnull(gcl65f, 0)>0 and isnull(gcl70f, 0)>0 group by gcl00c, gcl80d, gcl15c, isnull(gcl69c, '') --第三支付方式 insert #gmm01_buf(gma00c, gma01d, gma03c, gma04c, gma05f, gma06f, gma07f) select gcl00c, gcl80d, gcl15c, isnull(gcl71c, ''), sum(gcl72f/gcl65f), sum(gcl72f), sum(gcl72f/gcl65f) from gcm10 a with (nolock), gam26 z where a.gcl00c = z.gaz02c and z.gaz01c = @compid and a.gcl80d >= @fromdate and a.gcl80d <= @todate and isnull(gcl71c, '')<>'' and isnull(gcl65f, 0)>0 and isnull(gcl72f, 0)>0 group by gcl00c, gcl80d, gcl15c, isnull(gcl71c, '') --第四支付方式 insert #gmm01_buf(gma00c, gma01d, gma03c, gma04c, gma05f, gma06f, gma07f) select gcl00c, gcl80d, gcl15c, isnull(gcl73c, ''), sum(gcl74f/gcl65f), sum(gcl74f), sum(gcl74f/gcl65f) from gcm10 a with (nolock), gam26 z where a.gcl00c = z.gaz02c and z.gaz01c = @compid and a.gcl80d >= @fromdate and a.gcl80d <= @todate and isnull(gcl73c, '')<>'' and isnull(gcl65f, 0)>0 and isnull(gcl74f, 0)>0 group by gcl00c, gcl80d, gcl15c, isnull(gcl73c, '') end insert gmm01(gma00c, gma01d, gma02i, gma03c, gma04c, gma05f, gma06f, gma07f) select gma00c, gma01d, 5, gma03c, gma04c, sum(gma05f), sum(gma06f), sum(gma07f) from #gmm01_buf group by gma00c, gma01d, gma03c, gma04c delete gmm01 where isnull(gma04c, '') = '' or (isnull(gma05f, 0) = 0 and isnull(gma06f, 0) = 0) drop table #gmm01_buf end GO /****** Object: StoredProcedure [dbo].[upg_inoutstock_statistic] Script Date: 06/12/2017 06:20:14 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_inoutstock_statistic] ( @compid varchar(10), -- 公司编号 @datefrom varchar(10), -- 日期from @dateto varchar(10), -- 日期to @goodsfrom varchar(20), -- 产品编号from @goodsto varchar(20), -- 产品编号to @garagefrom varchar(20), -- 仓库编号from @garageto varchar(20), -- 仓库编号to @kindfrom varchar(20), -- 开始产品类别 @kindto varchar(20), -- 截至产品类别 @amtType int , -- 打印金额类型 @supplierfrom varchar(20) -- 供应商 ) as--mzh, 2005/07/09 begin if (@kindto = '*' and @kindfrom <> '*') set @kindto = @kindfrom if (@goodsto = '*' and @goodsfrom <> '*') set @goodsto = @goodsfrom if (@garageto = '*' and @garagefrom <> '*') set @garageto = @garagefrom declare @includesons float select @includesons = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP103' create table #ghm01_ghm02_buf -- 产品入库单 ( gha00c varchar(10) not null, -- 公司编号 gha01c varchar(100) not null, -- 入库单号 gha03d varchar(8) null, -- 入库日期 gha04c varchar(4) null, -- 仓库代码 gha07i int null, -- 入库性质(1- 正常入库 2- 盘点冲抵, 3-退货) gha10i int null, -- 出库转入库 0 销售转入库 1 ghb02f float not null, -- 序号 ghb03c varchar(20) null, -- 产品代码 ghb04c varchar(5) null, -- 标准单位 ghb06c varchar(5) null, -- 进货单位 ghb07f float null, -- (进货单位)数量 ghb10f float null, -- 单价 ghb11f float null, -- 金额 ghb13f float null, -- 标准单位数量 ghb14f float null -- 标准单位价格 ) create clustered index idx_ghm01_02_01 on #ghm01_ghm02_buf(gha04c, ghb03c) create table #gim01_gim02_buf -- 产品出库单 ( gia00c varchar(10) not null, -- 公司编号 gia01c varchar(100) not null, -- 出库单号 gia03d varchar(8) null, -- 出库日期 gia04c varchar(4) null, -- 仓库代码 gia07i int null, -- 入库性质(1- 正常出库 2- 盘点冲抵 3-供应商退货) gib02f float not null, -- 序号 gib03c varchar(20) null, -- 产品代码 gib05f float null, -- 现库存数量(出库单位) gib06c varchar(5) null, -- 出库单位 gib07f float null, -- (出库单位)数量 gib08f float null, -- (标准单位)数量 gib09f float null, -- 单价(出库单位) gib10f float null, -- 金额 gib11f float null -- 单价(标准单位) ) create clustered index idx_gim01_02_01 on #gim01_gim02_buf(gia04c, gib03c) insert #ghm01_ghm02_buf(gha00c, gha01c, gha03d, gha04c, gha07i, gha10i, ghb02f, ghb03c, ghb04c, ghb06c, ghb07f, ghb10f, ghb11f, ghb13f, ghb14f) select gha00c, gha01c, gha03d, gha04c, gha07i, gha10i, ghb02f, ghb03c, ghb04c, ghb06c, ghb07f, ghb10f, ghb11f, ghb13f, ghb14f from ghm01 a with (nolock), ghm02 b with (nolock), gam26 z where a.gha00c = b.ghb00c and a.gha01c = b.ghb01c and a.gha00c = z.gaz02c and z.gaz01c = @compid and a.gha03d >= @datefrom and a.gha03d <= @dateto and isnull(gha94d, '')<>'' -- 需要复核的 insert #gim01_gim02_buf(gia00c, gia01c, gia03d, gia04c, gia07i, gib02f, gib03c, gib05f, gib06c, gib07f, gib08f, gib09f, gib10f, gib11f) select gia00c, gia01c, gia03d, gia04c, gia07i, gib02f, gib03c, gib05f, gib06c, gib07f, gib08f, gib09f, gib10f, gib11f from gim01 a with (nolock), gim02 b with (nolock), gam26 z where a.gia00c = b.gib00c and a.gia01c = b.gib01c and a.gia00c = z.gaz02c and z.gaz01c = @compid and a.gia03d >= @datefrom and a.gia03d <= @dateto and isnull(gia94d, '')<>'' -- 需要复核的 create table #begstockquan --存放期初量 ( id int identity not null, goodsno varchar(20) null, --产品编号 goodsname varchar(80) null, --产品名称 garageno varchar(20) null, --仓库编号 garagename varchar(20) null, --仓库名称 unit varchar(10) null, --单位 quantity float null, --数量 amt float null, --金额 amt_cost float null, --成本单价金额 ) declare @PK_Sql nvarchar(max) set @PK_Sql = ' alter table #begstockquan add constraint PK_#begstockquan_' + convert(varchar(10), cast(floor(rand() * 1000000) as int)) + ' primary key nonclustered(id)' exec sp_executesql @PK_Sql create clustered INDEX IDX_begstockquan_01 on #begstockquan(goodsno, garageno) create table #endstockquan -- 存放期末量 ( id int identity not null, goodsno varchar(20) null, --产品编号 goodsname varchar(80) null, --产品名称 garageno varchar(20) null, --仓库编号 garagename varchar(20) null, --仓库名称 unit varchar(10) null, --单位 quantity float null, --数量 amt float null, --金额 amt_cost float null, --成本金额 ) set @PK_Sql = ' alter table #endstockquan add constraint PK_#endstockquan_' + convert(varchar(10), cast(floor(rand() * 1000000) as int)) + ' primary key nonclustered(id)' exec sp_executesql @PK_Sql create clustered INDEX IDX_endstockquan_01 on #endstockquan(goodsno, garageno) create table #instock --存放入库量 ( id int identity not null, goodsno varchar(20) null, -- 产品编号 garageno varchar(20) null, -- 仓库编号 quantity float null, -- 数量 amt float null, -- 入库金额 add by lyj for mos-31 2007/4/29 stdamt float null, -- 标准金额 mon_inamt float null --月加平均价 ) set @PK_Sql = ' alter table #instock add constraint PK_#instock_' + convert(varchar(10), cast(floor(rand() * 1000000) as int)) + ' primary key nonclustered(id) ' exec sp_executesql @PK_Sql create clustered INDEX IDX_instock_01 on #instock(goodsno, garageno) declare @sql nvarchar(max) declare @InCount int select @InCount = count(1) from gsm02 where gsb00c = @compid and gsb01c = 'C1' create table #instock_ways ( id int identity, in_way int ) insert into #instock_ways(in_way) select gsb02c from gsm02 where gsb00c = @compid and gsb01c = 'C1' set @InCount = isnull(@InCount, 0) declare @OutCount int select @OutCount = count(1) from gsm02 where gsb00c = @compid and gsb01c = 'C2' create table #outstock_ways ( id int identity, out_way int ) insert into #outstock_ways(out_way) select gsb02c from gsm02 where gsb00c = @compid and gsb01c = 'C2' set @OutCount = isnull(@OutCount, 0) declare @inout_way varchar(max) create table #instock_detail --Add by maql 2010/03/29 入库明细 ( id int identity not null, goodsno varchar(20) null, --产品编号 garageno varchar(20) null, --仓库编号 ) create clustered INDEX IDX_instockdetail_01 on #instock_detail(goodsno, garageno) create table #gim01_gim02_out --出库明细 --add by lm 20150611 ( id int identity not null, gia00c varchar(10) not null, -- 公司编号 gib03c varchar(20) not null, -- 产品代码 gia07i int null, -- 入库性质(1- 正常出库 2- 盘点冲抵 3-供应商退货) gib08f float null, -- 现库存数量(出库单位) gib10f float null, -- 金额 ) declare @index int set @index = 1 set @sql = '' while(@index <= @InCount + @InCount) begin set @sql = @sql + 'alter table #instock_detail add insum' + convert(varchar(100), @index) + ' float null ' + 'alter table #instock_detail add inamt' + convert(varchar(100), @index + 1) + ' float null ' set @index = @index + 2 end exec sp_executesql @sql create table #instock_detail_group --Add by maql 2010/03/29 入库明细 ( goodsno varchar(20) null, --产品编号 garageno varchar(20) null, --仓库编号 ) create clustered INDEX IDX_instockdetail_group_01 on #instock_detail_group(goodsno, garageno) set @index = 1 set @sql = '' while(@index <= @InCount + @InCount) begin set @sql = @sql + 'alter table #instock_detail_group add insum' + convert(varchar(100), @index) + ' float null ' + 'alter table #instock_detail_group add inamt' + convert(varchar(100), @index + 1) + ' float null ' set @index = @index + 2 end exec sp_executesql @sql create table #outstock --存放出库量 ( id int identity not null, goodsno varchar(20) null, --产品编号 garageno varchar(20) null, --仓库编号 quantity float null, --数量 amt float null, --入库金额 add by lyj for mos-31 2007/4/29 stdamt float null, --标准金额 mon_outamt float null --月加平均价 ) set @PK_Sql = ' alter table #outstock add constraint PK_#outstock_' + convert(varchar(10), cast(floor(rand() * 1000000) as int)) + ' primary key nonclustered(id)' exec sp_executesql @PK_Sql create clustered INDEX IDX_outstock_01 on #outstock(goodsno, garageno) create table #outstock_detail --出库量明細 Add by maql 2010/03/29 ( id int identity not null, goodsno varchar(20) null, --产品编号 garageno varchar(20) null, --仓库编号 outtype varchar(10) null, -- 出库类型 normalquan float null, -- 正常出库 --modify by lm 2015-06-25 normalamt float null, -- 出库金额 --modify by lm 2015-06-25 primary key (id) ) create nonclustered INDEX IDX_outstock_detail_01 on #outstock_detail(goodsno, garageno) set @index = 1 set @sql = '' while(@index <= @OutCount + @OutCount) begin set @sql = @sql + 'alter table #outstock_detail add outsum' + convert(varchar(100), @index) + ' float null ' + 'alter table #outstock_detail add outamt' + convert(varchar(100), @index + 1) + ' float null ' set @index = @index + 2 end exec sp_executesql @sql create table #outstock_detail_2 --出库量明細 --add by lm 2015-06-25 ( id int identity not null, goodsno varchar(20) null, --产品编号 garageno varchar(20) null, --仓库编号 outtype varchar(10) null, -- 出库类型 normalquan float null, -- 正常出库 normalamt float null -- 出库金额 primary key (id) ) create nonclustered INDEX IDX_outstock_detail_02 on #outstock_detail_2(goodsno, garageno) set @index = 1 set @sql = '' while(@index <= @OutCount + @OutCount) begin set @sql = @sql + 'alter table #outstock_detail_2 add outsum' + convert(varchar(100), @index) + ' float null ' + 'alter table #outstock_detail_2 add outamt' + convert(varchar(100), @index + 1) + ' float null ' set @index = @index + 2 end exec sp_executesql @sql create table #proconsume -- add by lm 2016.06.24 ( id int identity not null, goodsno varchar(20) null, --产品编号 garageno varchar(20) null, --仓库编号 quan float null, --出库数量(标准单位) amt float null, quan_out float null,--出库数量 primary key(id) ) create nonclustered index index_proconsume on #proconsume(goodsno, garageno) -------add by lm 20150625 ------- create table #outstock_detail_group --出库量明細 Add by maql 2010/03/29 ( goodsno varchar(20) null, --产品编号 garageno varchar(20) null, --仓库编号 ) create table #outstock_detail_group_out --出库量明細 Add by maql 2010/03/29 ( goodsno varchar(20) null, --产品编号 garageno varchar(20) null, --仓库编号 normalquan float null, -- 正常出库 --modify by lm 2015/06/11 normalamt float null, -- 出库金额 --modify by lm 2015/06/11 proconsumequan float null, -- 产品消耗数量 proconsumeamt float null, -- 产品消耗金额 quan_out float null, --出库数量 ) create clustered INDEX IDX_outstock_detail_group_01 on #outstock_detail_group(goodsno, garageno) set @index = 1 set @sql = '' while(@index <= @OutCount + @OutCount) begin set @sql = @sql + 'alter table #outstock_detail_group add outsum' + convert(varchar(100), @index) + ' float null ' + 'alter table #outstock_detail_group add outamt' + convert(varchar(100), @index + 1) + ' float null ' set @index = @index + 2 end exec sp_executesql @sql -- --start增加成本单价,金额 add by lm 2016.06.23 -- -- declare @SP058f float -- 成本计算方式 -- select @SP058f = gsa03f -- from gsm01 with(nolock) -- where gsa00c = @compid and gsa01c = 'sp058' -- -- --需要修改成本 -- if @SP058f = 5 -- begin -- update a -- set a.amt = isnull(a.quan, 0) * isnull(b.gfh03f, 0) -- from #proconsume a, gfm08 b -- where a.goodsno = b.gfh01c -- and b.gfh00c = @compid -- and b.gfh02c = substring(@dateto, 1, 6) -- end -- else if @SP058f = 2 -- begin -- update a -- set a.amt = isnull(a.quan, 0) * isnull(b.gfa19f, 0) -- from #proconsume a, gfm01 b -- where a.goodsno = b.gfa01c -- and b.gfa00c = @compid -- end -- else if @SP058f = 3 -- begin -- update a -- set a.amt = isnull(a.quan, 0) * isnull(b.gfa11f, 0) -- from #proconsume a, gfm01 b -- where a.goodsno = b.gfa01c -- and b.gfa00c = @compid -- end -- else -- begin -- update a -- set a.amt = isnull(a.quan, 0) * isnull(b.gfa22f, 0) -- from #proconsume a, gfm01 b -- where a.goodsno = b.gfa01c -- and b.gfa00c = @compid -- end --end --end -- 对日期处理, 对1天的数据进行查询 declare @start_date varchar(8) declare @strtmp varchar(10) set @strtmp = convert(varchar(10), dateadd(day, -1, @datefrom), 120) set @start_date = substring(@strtmp, 1, 4) + substring(@strtmp, 6, 2) + substring(@strtmp, 9, 2) if @includesons = 1 begin insert #instock(goodsno, garageno, quantity, amt, stdamt) select gfd04c, gfd08c, sum(gfd05f), sum(gfd13f), sum(isnull(gfd12f, 0)*gfa11f) from gfm04 a with (nolock), gam26 z, gfm01 b with (nolock) where gfd01c = '1' -- 入库 and gfd00c = z.gaz02c and z.gaz01c = @compid and a.gfd04c = b.gfa01c and a.gfd00c = b.gfa00c and gfd07c between @datefrom and @dateto and ((@garagefrom = '*') or (gfd08c between @garagefrom and @garageto)) and ((@supplierfrom = '*') or (gfa23c = @supplierfrom)) and ((@goodsfrom = '*') or (gfd04c between @goodsfrom and @goodsto)) and ((@kindfrom = '*') or (gfa24c between @kindfrom and @kindto)) group by gfd04c, gfd08c set @sql = 'insert #instock_detail(goodsno, garageno ' set @index = 1 while(@index <= @InCount + @InCount) begin set @sql = @sql + ' , insum' + convert(varchar(100), @index) + ' , inamt' + convert(varchar(100), @index + 1) set @index = @index + 2 end set @sql = @sql + ' ) select ghb03c, t.garageno' set @index = 1 while(@index <= @InCount + @InCount) begin set @inout_way = '' if @index = 1 begin set @inout_way = ' not in(' select @inout_way = @inout_way + convert(varchar(100), in_way) from #instock_ways where id = 2 select @inout_way = @inout_way + ',' + convert(varchar(100), in_way) from #instock_ways where id = 3 select @inout_way = @inout_way + ',' + convert(varchar(100), in_way) from #instock_ways where id = 4 set @inout_way = @inout_way + ') ' end else begin select @inout_way = ' = ' + convert(varchar(100), in_way) from #instock_ways where id = (@index + 1) / 2 end set @sql = @sql + ' , case when gha07i ' + @inout_way + ' then sum(ghb13f) else 0 end as insum' + convert(varchar(100), @index) + ' , case when gha07i ' + @inout_way + ' then sum(isnull(ghb11f, 0)) else 0 end as inamt' + convert(varchar(100), @index + 1) set @index = @index + 2 end set @sql = @sql + ' from #ghm01_ghm02_buf b, #instock t where b.gha04c = t.garageno and b.ghb03c = t.goodsno group by ghb03c, gha07i, t.garageno' exec sp_executesql @sql insert #outstock(goodsno, garageno, quantity, amt, stdamt) select gfd04c, gfd08c, sum(gfd12f), sum(isnull(gfd12f, 0)*isnull(gfa22f, 0)), sum(isnull(gfd12f, 0)*gfa11f) from gfm04 a with (nolock), gam26 z, gfm01 b with (nolock) where gfd01c in ('2', '3', '4','5','6','7', '8') and gfd00c = z.gaz02c and z.gaz01c = @compid and a.gfd04c = b.gfa01c and a.gfd00c = b.gfa00c and gfd07c between @datefrom and @dateto and ((@garagefrom = '*') or (gfd08c between @garagefrom and @garageto)) and ((@supplierfrom = '*') or (gfa23c = @supplierfrom)) and ((@goodsfrom = '*') or (gfd04c between @goodsfrom and @goodsto)) and ((@kindfrom = '*') or (gfa24c between @kindfrom and @kindto)) group by gfd04c, gfd08c set @sql = 'insert into #outstock_detail(goodsno, garageno, outtype ' set @index = 1 while(@index <= @OutCount + @OutCount) begin set @sql = @sql + ' , outsum' + convert(varchar(100), @index) + ' , outamt' + convert(varchar(100), @index + 1) set @index = @index + 2 end set @sql = @sql + ' ) select gib03c, t.garageno, gia07i ' set @index = 1 while(@index <= @OutCount + @OutCount) begin set @inout_way = '' select @inout_way = convert(varchar(100), out_way) from #outstock_ways where id = (@index + 1) / 2 set @sql = @sql + ' , case when gia07i = ' + @inout_way + ' then sum(gib08f) else 0 end' + ' , 0 ' set @index = @index + 2 end set @sql = @sql + ' from #gim01_gim02_buf b with (nolock), #outstock t where b.gia04c = t.garageno and b.gib03c = t.goodsno group by gib03c, gia07i, t.garageno ' exec sp_executesql @sql end else begin insert #instock(goodsno, garageno, quantity, amt, stdamt) select gfd04c, gfd08c, sum(gfd05f), sum(gfd13f), sum(isnull(gfd12f, 0)*gfa11f) from gfm04 a with (nolock), gfm01 b with (nolock) where gfd01c = '1' and gfd00c = @compid and a.gfd04c = b.gfa01c and a.gfd00c = b.gfa00c and gfd07c between @datefrom and @dateto and ((@garagefrom = '*') or (gfd08c between @garagefrom and @garageto)) and ((@supplierfrom = '*') or (gfa23c = @supplierfrom)) and ((@goodsfrom = '*') or (gfd04c between @goodsfrom and @goodsto)) and ((@kindfrom = '*') or (gfa24c between @kindfrom and @kindto)) group by gfd04c, gfd08c set @sql = 'insert #instock_detail(goodsno, garageno ' set @index = 1 while(@index <= @InCount + @InCount) begin set @sql = @sql + ' , insum' + convert(varchar(100), @index) + ' , inamt' + convert(varchar(100), @index + 1) set @index = @index + 2 end set @sql = @sql + ' ) select ghb03c, t.garageno' set @index = 1 while(@index <= @InCount + @InCount) begin set @inout_way = '' if @index = 1 begin set @inout_way = ' not in(' select @inout_way = @inout_way + convert(varchar(100), in_way) from #instock_ways where id = 2 select @inout_way = @inout_way + ',' + convert(varchar(100), in_way) from #instock_ways where id = 3 select @inout_way = @inout_way + ',' + convert(varchar(100), in_way) from #instock_ways where id = 4 set @inout_way = @inout_way + ') ' end else begin select @inout_way = ' = ' + convert(varchar(100), in_way) from #instock_ways where id = (@index + 1) / 2 end set @sql = @sql + ' , case when gha07i ' + @inout_way + ' then sum(ghb13f) else 0 end as insum' + convert(varchar(100), @index) + ' , case when gha07i ' + @inout_way + ' then sum(isnull(ghb11f, 0)) else 0 end as inamt' + convert(varchar(100), @index + 1) set @index = @index + 2 end set @sql = @sql + ' from #ghm01_ghm02_buf b with (nolock), #instock t where b.gha04c = t.garageno and b.ghb03c = t.goodsno and b.gha00c = ''' + @compid + ''' group by ghb03c, gha07i, t.garageno' exec sp_executesql @sql insert #outstock(goodsno, garageno, quantity, amt, stdamt) select gfd04c, gfd08c, sum(gfd12f), sum(isnull(gfd12f, 0) * isnull(gfa22f, 0)), sum(isnull(gfd12f , 0) * gfa11f) from gfm04 a with (nolock), gfm01 b with (nolock) where gfd01c in ('2', '3', '4','5','6','7', '8') and a.gfd00c = @compid and a.gfd04c = b.gfa01c and a.gfd00c = b.gfa00c and gfd07c between @datefrom and @dateto and ((@garagefrom = '*') or (gfd08c between @garagefrom and @garageto)) and ((@supplierfrom = '*') or (gfa23c = @supplierfrom)) and ((@goodsfrom = '*') or (gfd04c between @goodsfrom and @goodsto)) and ((@kindfrom = '*') or (gfa24c between @kindfrom and @kindto)) group by gfd04c, gfd08c set @sql = 'insert into #outstock_detail(goodsno, garageno, outtype ' set @index = 1 while(@index <= @OutCount + @OutCount) begin set @sql = @sql + ' , outsum' + convert(varchar(100), @index) + ' , outamt' + convert(varchar(100), @index + 1) set @index = @index + 2 end set @sql = @sql + ' ) select gib03c, t.garageno, gia07i ' set @index = 1 while(@index <= @OutCount + @OutCount) begin set @inout_way = '' select @inout_way = convert(varchar(100), out_way) from #outstock_ways where id = (@index + 1) / 2 set @sql = @sql + ' , case when gia07i = ' + @inout_way + ' then sum(gib08f) else 0 end' + ' , 0 ' set @index = @index + 2 end set @sql = @sql + ' from #gim01_gim02_buf b with (nolock), #outstock t where b.gia04c = t.garageno and b.gib03c = t.goodsno and b.gia00c = ''' + @compid + ''' group by gib03c, gia07i, t.garageno ' exec sp_executesql @sql end ---begin 插入出库数据------ add by lm 20150611 insert #gim01_gim02_out(gia00c,gib03c,gia07i,gib08f,gib10f) select gia00c,gib03c,gia07i,sum(gib08f),sum(gib10f) from #gim01_gim02_buf a group by a.gia00c,a.gib03c,a.gia07i ----end----------------------- -- 产品消耗,产品销售,产品配料等 if @includesons = 1 begin set @sql = 'insert into #outstock_detail(goodsno, garageno, outtype ' set @index = 1 while(@index <= @OutCount + @OutCount) begin set @sql = @sql + ' , outsum' + convert(varchar(100), @index) + ' , outamt' + convert(varchar(100), @index + 1) set @index = @index + 2 end set @sql = @sql + ') select goodsno, garageno, outtype, sum(outsum1), sum(outamt2)' set @index = 3 while(@index <= @OutCount + @OutCount) begin set @sql = @sql + ' , 0 as outsum' + convert(varchar(100), @index) + ' , 0 as outamt' + convert(varchar(100), @index + 1) set @index = @index + 2 end set @sql = @sql + ' from (select gfd04c as goodsno, t.garageno as garageno,gfc06i as outtype, case when gfc01c = ''4'' then 0 else gfd12f end as outsum1, 0 as outamt2 from gfm03 a with (nolock), gfm04 b with (nolock), gam26 z, #outstock t where a.gfc00c = b.gfd00c and a.gfc01c = b.gfd01c and a.gfc02c = b.gfd02c and a.gfc01c in (''3'', ''4'',''5'',''6'',''7'', ''8'') and ((a.gfc03c >= ''' + @datefrom + ''' and a.gfc03c <= + ''' + @dateto + ''') or (''' + @datefrom + ''' = ''*'')) and b.gfd08c = t.garageno and b.gfd04c = t.goodsno and a.gfc00c = z.gaz02c and z.gaz01c = ''' + @compid + ''') P group by P.goodsno, P.garageno, P.outtype' exec sp_executesql @sql end else begin set @sql = 'insert into #outstock_detail(goodsno, garageno, outtype ' set @index = 1 while(@index <= @OutCount + @OutCount) begin set @sql = @sql + ' , outsum' + convert(varchar(100), @index) + ' , outamt' + convert(varchar(100), @index + 1) set @index = @index + 2 end set @sql = @sql + ') select goodsno, garageno, outtype, sum(outsum1), sum(outamt2)' set @index = 3 while(@index <= @OutCount + @OutCount) begin set @sql = @sql + ' , 0 as outsum' + convert(varchar(100), @index) + ' , 0 as outamt' + convert(varchar(100), @index + 1) set @index = @index + 2 end set @sql = @sql + ' from (select gfd04c as goodsno, t.garageno as garageno, gfc06i as outtype, case when gfc01c = ''4'' then 0 else gfd12f end as outsum1, 0 as outamt2 from gfm03 a with (nolock), gfm04 b with (nolock), #outstock t where a.gfc00c = b.gfd00c and a.gfc01c = b.gfd01c and a.gfc02c = b.gfd02c and a.gfc01c in (''3'', ''4'',''5'',''6'',''7'', ''8'') and ((a.gfc03c >= ''' + @datefrom + '''and a.gfc03c <= ''' + @dateto + ''') or (''' + @datefrom + ''' = ''*'')) and b.gfd08c = t.garageno and b.gfd04c = t.goodsno and a.gfc00c = ''' + @compid + ''' ) P group by P.goodsno, P.garageno, P.outtype' exec sp_executesql @sql end drop table #ghm01_ghm02_buf drop table #gim01_gim02_buf --期初库存量 insert #begstockquan(goodsno, goodsname, garageno, garagename, unit, quantity, amt, amt_cost) exec upg_stock_statistic @compid, @start_date, @goodsfrom, @goodsto, @garagefrom, @garageto, 4, '', @supplierfrom, @kindfrom, @kindto, 0 --期末库存量 insert #endstockquan(goodsno, goodsname, garageno, garagename, unit, quantity, amt, amt_cost) exec upg_stock_statistic @compid, @dateto, @goodsfrom, @goodsto, @garagefrom, @garageto, 4, '', @supplierfrom, @kindfrom, @kindto, 0 declare @include_invalid_good varchar(20) declare @SP054f float -- 成本计算方式 select @SP054f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP054' --begin 去掉重复的正常出库方式(收银卖产品也是正常出库) set @sql = 'insert #outstock_detail_2(goodsno, garageno, outtype ' set @index = 1 while(@index <= @OutCount + @OutCount) begin set @sql = @sql + ' , outsum' + convert(varchar(100), @index) + ' , outamt' + convert(varchar(100), @index + 1) set @index = @index + 2 end set @sql = @sql + ' ) select goodsno, garageno, outtype ' set @index = 1 while(@index <= @OutCount + @OutCount) begin set @sql = @sql + ' , sum(outsum' + convert(varchar(100), @index) + ')' + ' , sum(outamt' + convert(varchar(100), @index + 1) + ')' set @index = @index + 2 end set @sql = @sql + ' from #outstock_detail group by goodsno, garageno, outtype ' exec sp_executesql @sql --end -- 需要修改成本 if @SP054f = 2 begin declare @curmonth varchar(20) select @curmonth = substring(@dateto, 1, 6) -- 计算上月的月加权 -- 可能本月没有计算月加权 -- exec upg_create_good_month_cost @compid, @curmonth update a set a.amt_cost = b.gfh03f * a.quantity from #begstockquan a, gfm08 b where a.goodsno = b.gfh01c and b.gfh02c = substring(@start_date, 1, 6) and b.gfh00c = @compid -- 期末金额 = 月末数量 * 本月的加权金额,也应该登陆计算出来的 -- 期末金额 = 期初金额 + 入库总金额 - 出库总金额 update a set a.amt_cost = b.gfh03f*a.quantity from #endstockquan a, gfm08 b where a.goodsno = b.gfh01c and b.gfh02c = substring(@dateto, 1, 6) and b.gfh00c = @compid -- 入库的金额应该是正式的价格*数量 -- 不应该加权金额 update a set a.amt = b.gfh03f * a.quantity, a.mon_outamt = b.gfh03f from #outstock a, gfm08 b where a.goodsno = b.gfh01c and b.gfh02c = substring(@dateto, 1, 6) and b.gfh00c = @compid update a set a.mon_inamt = b.gfh03f from #instock a, gfm08 b where a.goodsno = b.gfh01c and b.gfh02c = substring(@dateto, 1, 6) and b.gfh00c = @compid end -- Add by maql 2010/03/29 --begin 出入库金额,按库存异动明细单据金额来算--add by lm 20150626 set @sql = 'update a set ' set @index = 1 while(@index <= @OutCount + @OutCount) begin if @index <> 1 set @sql = @sql + ' , ' set @inout_way = '' select @inout_way = convert(varchar(100), out_way) from #outstock_ways where id = (@index + 1) / 2 set @sql = @sql + ' a.outamt' + convert(varchar(100), @index+1 ) + ' = case when a.outtype = ' + @inout_way + ' then e.totamt else 0 end ,' + ' a.outsum' + convert(varchar(100), @index) + ' = case when a.outtype = ' + @inout_way + ' then e.tocount else 0 end ' set @index = @index + 2 end set @sql = @sql + ' from #outstock_detail_2 a, gfm01 b, (select c.gfd00c,f.gfc06i,c.gfd04c, c.gfd08c, sum(gfd13f) totamt,sum(gfd12f) as tocount from gfm04 c, gfm03 f,gam26 d where c.gfd00c = d.gaz02c and f.gfc00c = c.gfd00c and f.gfc01c = c.gfd01c and f.gfc02c = c.gfd02c and f.gfc01c in (''2'',''3'',''5'',''6'',''7'', ''8'') and d.gaz01c = ''' + @compid + ''' and c.gfd07c between ''' + @datefrom + ''' and ''' + @dateto + ''' and c.gfd01c <> ''1'' group by c.gfd00c,f.gfc06i, c.gfd04c, c.gfd08c) e where a.goodsno = b.gfa01c and b.gfa00c = ''' + @compid + ''' and e.gfd04c = b.gfa01c and e.gfd00c = b.gfa00c and e.gfc06i = a.outtype ' exec sp_executesql @sql -----begin--正常出库,出库金额--- add by lm 20150610 update a set a.normalquan=b.gib08f, a.normalamt=isnull(b.gib10f,0) from #outstock_detail_2 a ,#gim01_gim02_out b where a.goodsno=b.gib03c and b.gia07i=a.outtype and b.gia07i='1' -----end--------- -----begin--销售量,销售额--- add by lm 20150715 (减掉产品出库中出库性质为正常出库的产品数量和金额) set @sql = 'update a set ' set @index = 1 set @sql = @sql + ' a.outamt' + convert(varchar(100), @index + 1) + ' = a.outamt' + convert(varchar(100), @index + 1) + '- b.gib10f ,' + ' a.outsum' + convert(varchar(100), @index) + ' = a.outsum' + convert(varchar(100), @index) + ' - b.gib08f ' set @sql = @sql + ' from #outstock_detail_2 a, #gim01_gim02_out b where a.goodsno=b.gib03c and b.gia07i=a.outtype and b.gia07i=''1''' exec sp_executesql @sql -----end--------- --begin 卖产品出库金额 按成本价来算 只考虑产品销售,消耗。 if @amtType != 1 begin set @sql = 'update a set ' set @sql = @sql + ' a.outamt' + convert(varchar(100), @index + 1) + ' = ' + 'e.tocount * b.gfa22f ,' + ' a.outsum' + convert(varchar(100), @index) + ' = ' + ' e.tocount ' set @sql = @sql + ' from #outstock_detail_2 a, gfm01 b, (select c.gfd00c,f.gfc06i,c.gfd04c, c.gfd08c, sum(gfd12f) totamt,sum(gfd12f) as tocount from gfm04 c, gfm03 f,gam26 d where c.gfd00c = d.gaz02c and f.gfc00c = c.gfd00c and f.gfc01c = c.gfd01c and f.gfc02c = c.gfd02c and c.gfd01c <> ''1'' and f.gfc01c in (''3'',''5'',''6'',''7'',''8'') and d.gaz01c = ''' + @compid + ''' and c.gfd07c between ''' + @datefrom + ''' and ''' + @dateto + ''' group by c.gfd00c,f.gfc06i, c.gfd04c, c.gfd08c) e where a.goodsno = b.gfa01c and b.gfa00c = ''' + @compid + ''' and e.gfd04c = b.gfa01c and e.gfd00c = b.gfa00c and e.gfc06i = a.outtype ' exec sp_executesql @sql -- --begin 卖产品出库金额,按消耗成本来算----------------------add by lm 20150626 --暂时注释 modify by lm 2016.06.24 -- set @sql = 'update a -- set ' -- -- set @index = 1 -- -- set @sql = @sql + ' a.outamt' + convert(varchar(100), @index + 1) -- + ' = ' -- + 'e.tocount * b.gfa22f,' -- + ' a.outsum' + convert(varchar(100), @index ) -- + ' = ' -- + ' e.tocount ' -- set @sql = @sql + ' from #outstock_detail_2 a, gfm01 b, -- (select c.gfd00c,f.gfc06i,c.gfd04c, c.gfd08c, sum(gfd12f) totamt,sum(gfd12f) as tocount from gfm04 c, gfm03 f,gam26 d -- where c.gfd00c = d.gaz02c -- and f.gfc00c = c.gfd00c -- and f.gfc01c = c.gfd01c -- and f.gfc02c = c.gfd02c -- and c.gfd01c <> ''1'' -- and f.gfc01c in (''4'',''8'') -- and d.gaz01c = ''' + @compid + ''' -- and c.gfd07c between ''' + @datefrom + ''' and ''' + @dateto + ''' -- group by c.gfd00c,f.gfc06i, c.gfd04c, c.gfd08c) e -- where a.goodsno = b.gfa01c -- and b.gfa00c = ''' + @compid + ''' -- and e.gfd04c = b.gfa01c -- and e.gfd00c = b.gfa00c -- and e.gfc06i = a.outtype ' -- exec sp_executesql @sql --begin 正常出库量 按消耗陈本来算 -add by lm 2015-07-15 update a set a.normalamt=a.normalquan * b.gfa22f from #outstock_detail_2 a,gfm01 b where a.goodsno = b.gfa01c and b.gfa00c = @compid --end --begin 除(正常入库)出入库金额,按进货陈本来算--add by lm 20150722 set @sql = 'update a set ' set @index = 1 while(@index <= @OutCount + @OutCount) begin set @sql = @sql + ' a.outamt'+ convert(varchar(100), @index+1) + ' = ' + ' a.outsum'+ convert(varchar(100), @index) +'* b.gfa22f ' set @index = @index + 2 if @index <@OutCount + @OutCount set @sql = @sql +',' end set @sql = @sql + ' from #outstock_detail_2 a, gfm01 b, (select c.gfd00c,f.gfc06i,c.gfd04c, c.gfd08c, sum(gfd13f) totamt,sum(gfd12f) as tocount from gfm04 c, gfm03 f,gam26 d where c.gfd00c = d.gaz02c and f.gfc00c = c.gfd00c and f.gfc01c = c.gfd01c and f.gfc02c = c.gfd02c and f.gfc01c in (''2'',''3'',''5'',''6'',''7'', ''8'') and d.gaz01c = ''' + @compid + ''' and c.gfd07c between ''' + @datefrom + ''' and ''' + @dateto + ''' and c.gfd01c <> ''1'' group by c.gfd00c,f.gfc06i, c.gfd04c, c.gfd08c) e where a.goodsno = b.gfa01c and b.gfa00c = ''' + @compid + ''' and e.gfd04c = b.gfa01c and e.gfd00c = b.gfa00c and e.gfc06i = a.outtype ' exec sp_executesql @sql --begin 产品入库金额,按消耗成本来算 --------------------add by lm 2015-07-14 set @sql = 'update a set ' set @index = 1 while(@index <= @InCount + @InCount) begin set @sql = @sql + ' a.inamt'+ convert(varchar(100), @index+1) + ' = ' + ' a.insum'+ convert(varchar(100), @index) +'* b.gfa22f ' set @index = @index + 2 if @index <@InCount + @InCount set @sql = @sql +',' end set @sql = @sql + ' from #instock_detail a, gfm01 b, (select c.gfd00c,f.gfc06i,c.gfd04c, c.gfd08c, sum(gfd12f) totamt,sum(gfd12f) as tocount from gfm04 c, gfm03 f,gam26 d where c.gfd00c = d.gaz02c and f.gfc00c = c.gfd00c and f.gfc01c = c.gfd01c and f.gfc02c = c.gfd02c and c.gfd01c = ''1'' and f.gfc01c =''1'' and d.gaz01c = ''' + @compid + ''' and c.gfd07c between ''' + @datefrom + ''' and ''' + @dateto + ''' group by c.gfd00c,f.gfc06i, c.gfd04c, c.gfd08c) e where a.goodsno = b.gfa01c and b.gfa00c = ''' + @compid + ''' and e.gfd04c = b.gfa01c and e.gfd00c = b.gfa00c ' exec sp_executesql @sql --end --end --start 计算产品消耗 add by lm 2016.06.23 insert #proconsume(goodsno, garageno, quan, amt,quan_out) select gfd04c, gfd08c, sum(gfd05f), sum(gfd05f * gfa22f) ,sum(gfd12f) from gfm04 a with (nolock), gam26 z, gfm01 b with (nolock) where gfd01c = '4' -- 入库 and gfd00c = z.gaz02c and z.gaz01c = @compid and a.gfd04c = b.gfa01c and a.gfd00c = b.gfa00c and gfd07c between @datefrom and @dateto and ((@garagefrom = '*') or (gfd08c between @garagefrom and @garageto)) and ((@supplierfrom = '*') or (gfa23c = @supplierfrom)) and ((@goodsfrom = '*') or (gfd04c between @goodsfrom and @goodsto)) and ((@kindfrom = '*') or (gfa24c between @kindfrom and @kindto)) group by gfd04c, gfd08c --end end else begin --begin 修改销售数量只计算 收银产品消耗,销售---------------------- add by lm 20150626 set @sql = 'update a set ' set @index = 1 set @sql = @sql + ' a.outamt' + convert(varchar(100), @index + 1) + ' = ' + ' e.totamt,' + ' a.outsum' + convert(varchar(100), @index ) + ' = ' + ' e.tocount ' set @sql = @sql + ' from #outstock_detail_2 a, gfm01 b, (select c.gfd00c,f.gfc06i,c.gfd04c, c.gfd08c, sum(gfd13f) totamt,sum(gfd12f) as tocount from gfm04 c, gfm03 f,gam26 d where c.gfd00c = d.gaz02c and f.gfc00c = c.gfd00c and f.gfc01c = c.gfd01c and f.gfc02c = c.gfd02c and c.gfd01c <> ''1'' and f.gfc01c in (''3'',''5'',''6'',''7'', ''8'') and d.gaz01c = ''' + @compid + ''' and c.gfd07c between ''' + @datefrom + ''' and ''' + @dateto + ''' group by c.gfd00c,f.gfc06i, c.gfd04c, c.gfd08c) e where a.goodsno = b.gfa01c and b.gfa00c = ''' + @compid + ''' and e.gfd04c = b.gfa01c and e.gfd00c = b.gfa00c and e.gfc06i = a.outtype ' exec sp_executesql @sql --end --start 计算产品消耗 add by lm 2016.06.23 insert #proconsume(goodsno, garageno, quan, amt,quan_out) select gfd04c, gfd08c, sum(gfd05f), sum(gfd05f * gfa15f) ,sum(gfd12f) from gfm04 a with (nolock), gam26 z, gfm01 b with (nolock) where gfd01c = '4' -- 入库 and gfd00c = z.gaz02c and z.gaz01c = @compid and a.gfd04c = b.gfa01c and a.gfd00c = b.gfa00c and gfd07c between @datefrom and @dateto and ((@garagefrom = '*') or (gfd08c between @garagefrom and @garageto)) and ((@supplierfrom = '*') or (gfa23c = @supplierfrom)) and ((@goodsfrom = '*') or (gfd04c between @goodsfrom and @goodsto)) and ((@kindfrom = '*') or (gfa24c between @kindfrom and @kindto)) group by gfd04c, gfd08c --end end select @include_invalid_good = gsa02c from gsm01 where gsa01c = 'SP113' and gsa00c = @compid if @include_invalid_good = '0' begin -- 删除已经停止之产品 delete a from #endstockquan a, gfm01 b where a.goodsno = b.gfa01c and b.gfa00c = @compid and isnull(gfa10c, '0') = '1' end set @sql = 'insert #instock_detail_group(goodsno, garageno ' set @index = 1 while(@index <= @InCount + @InCount) begin set @sql = @sql + ' , insum' + convert(varchar(100), @index) + ' , inamt' + convert(varchar(100), @index + 1) set @index = @index + 2 end set @sql = @sql + ' ) select goodsno, garageno ' set @index = 1 while(@index <= @InCount + @InCount) begin set @sql = @sql + ' , sum(insum' + convert(varchar(100), @index) + ') insum' + convert(varchar(100), @index) + + ' , sum(inamt' + convert(varchar(100), @index + 1) + ') inamt' + convert(varchar(100), @index + 1) set @index = @index + 2 end set @sql = @sql + ' from #instock_detail group by goodsno, garageno' exec sp_executesql @sql set @sql = 'insert #outstock_detail_group(goodsno, garageno ' set @index = 1 while(@index <= @OutCount + @OutCount) begin set @sql = @sql + ' , outsum' + convert(varchar(100), @index) + ' , outamt' + convert(varchar(100), @index + 1) set @index = @index + 2 end set @sql = @sql + ' ) select goodsno, garageno ' set @index = 1 while(@index <= @OutCount + @OutCount) begin set @sql = @sql + ' , sum(outsum' + convert(varchar(100), @index) + ') outsum' + convert(varchar(100), @index) + + ' , sum(outamt' + convert(varchar(100), @index + 1) + ') outamt' + convert(varchar(100), @index + 1) set @index = @index + 2 end set @sql = @sql + ' from #outstock_detail_2 group by goodsno, garageno ' exec sp_executesql @sql set @sql = 'insert #outstock_detail_group_out(goodsno, garageno,normalquan,normalamt ' set @sql = @sql + ' ) select goodsno, garageno,sum(isnull(normalquan,0)),sum(isnull(normalamt,0))' set @sql = @sql + ' from #outstock_detail_2 group by goodsno, garageno ' exec sp_executesql @sql --start 产品消耗数量,金额赋值 add by lm 2016.06.23 update a set a.proconsumequan = b.quan, a.quan_out = b.quan_out, a.proconsumeamt = b.amt from #outstock_detail_group_out a,#proconsume b where a.goodsno = b.goodsno and a.garageno = b.garageno --end set @sql = 'select idd = isnull(a.goodsno,''''), nname = isnull(a.goodsname,''''), garageid = isnull(a.garageno,''''), garagename = isnull(a.garagename,''''), pquan = (isnull(b.quantity, 0)), unit = isnull(a.unit,''''), pamt = (isnull(b.amt, 0)), pcamt = (isnull(b.amt_cost, 0)), inquan = (isnull(c.quantity, 0)), inamt = (isnull(c.amt, 0)), instdamt = (isnull(c.stdamt, 0)), mon_inamt = (isnull(c.mon_inamt, 0)), mon_outamt = (isnull(d.mon_outamt, 0)) ' set @index = 1 while(@index <= @InCount + @InCount) begin set @sql = @sql + ' , insum' + convert(varchar(100), @index) + ' = (isnull(t1.insum' + convert(varchar(100), @index) + ', 0))' + + ' , inamt' + convert(varchar(100), @index + 1) + ' = (isnull(t1.inamt' + convert(varchar(100), @index + 1) + ', 0))' set @index = @index + 2 end set @sql = @sql + ', outquan = (isnull(d.quantity, 0)), outamt = (isnull(d.amt, 0)), outstdamt = (isnull(d.stdamt, 0)), normalquan =(isnull(e.normalquan,0)), normalamt =(isnull(e.normalamt,0)), proconsumequan = (isnull(e.proconsumequan, 0)), proconsumeamt = (isnull(e.proconsumeamt, 0)), quan_out = (isnull(e.quan_out,0)), proconsumeamt =(isnull(e.proconsumeamt,0)) ' set @index = 1 while(@index <= @OutCount + @OutCount) begin set @sql = @sql + ' , outsum' + convert(varchar(100), @index) + ' = (isnull(t2.outsum' + convert(varchar(100), @index) + ', 0))' + + ' , outamt' + convert(varchar(100), @index + 1) + ' = (isnull(t2.outamt' + convert(varchar(100), @index + 1) + ', 0))' set @index = @index + 2 end set @sql = @sql + ' ,equan = (isnull(a.quantity, 0)), eamt = (isnull(a.amt, 0)), ecamt = (isnull(a.amt_cost, 0)) from #endstockquan a left outer join #begstockquan b on a.goodsno = b.goodsno and a.garageno = b.garageno left outer join #instock c on a.goodsno = c.goodsno and a.garageno = c.garageno left outer join #instock_detail_group t1 on a.goodsno = t1.goodsno and a.garageno = t1.garageno left outer join #outstock d on a.goodsno = d.goodsno and a.garageno = d.garageno left outer join #outstock_detail_group t2 on a.goodsno = t2.goodsno and a.garageno = t2.garageno left outer join #outstock_detail_group_out e on e.goodsno= a.goodsno and a.garageno = e.garageno' exec sp_executesql @sql drop table #instock_ways drop table #outstock_ways drop table #begstockquan drop table #endstockquan drop table #instock drop table #outstock drop table #instock_detail drop table #outstock_detail drop table #outstock_detail_2 drop table #instock_detail_group drop table #outstock_detail_group drop table #gim01_gim02_out drop table #outstock_detail_group_out drop table #proconsume end GO /****** Object: StoredProcedure [dbo].[upg_inform_glm01_when_fill] Script Date: 06/12/2017 06:20:12 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_inform_glm01_when_fill] ( @compid varchar(10), @billid varchar(20) ) as begin declare @accttype varchar(10) declare @acctamt float declare @procamt float declare @cardid varchar(20) declare @curdate varchar(10) declare @curtime varchar(10) select @accttype = gcl04c, @acctamt = gcl65f, @procamt = gcl66f, @cardid = gcl03c, @curdate = gcl02d, @curtime = gcl56t from gcm10 where gcl00c = @compid and gcl01c = @billid and (gcl06i = 0 or gcl06i = 6) -- 判断是否存在这样的glm01 if not exists(select 1 from glm01 with (nolock) where gla00c = @compid and gla06c = @cardid and gla23i = 0 and gla02d = @curdate and gla03t <= @curtime) return if isnull(@acctamt, 0) > 0 begin if @accttype = '1' begin update glm01 set gla29f = isnull(gla29f, 0) + @acctamt where gla00c = @compid and gla06c = @cardid and gla23i = 0 and gla02d = @curdate and gla03t <= @curtime end else if @accttype = '2' begin update glm01 set gla30f = isnull(gla30f, 0) + @acctamt where gla00c = @compid and gla06c = @cardid and gla23i = 0 and gla02d = @curdate and gla03t <= @curtime end else if @accttype = '3' begin update glm01 set gla31f = isnull(gla31f, 0) + @acctamt where gla00c = @compid and gla06c = @cardid and gla23i = 0 and gla02d = @curdate and gla03t <= @curtime end else if @accttype = '4' begin update glm01 set gla32f = isnull(gla32f, 0) + @acctamt where gla00c = @compid and gla06c = @cardid and gla23i = 0 and gla02d = @curdate and gla03t <= @curtime end else if @accttype = '5' begin update glm01 set gla33f = isnull(gla33f, 0) + @acctamt where gla00c = @compid and gla06c = @cardid and gla23i = 0 and gla02d = @curdate and gla03t <= @curtime end update glm01 set gla35f = isnull(gla35f, 0) + @acctamt where gla00c = @compid and gla06c = @cardid and gla23i = 0 and gla34i = convert(int, @accttype) and gla02d = @curdate and gla03t <= @curtime end if isnull(@procamt, 0)>0 begin declare @procacct varchar(10) select @procacct = gsa02c from gsm01 where gsa00c = @compid and gsa01c = 'SP057' if @procacct = '1' begin update glm01 set gla29f = isnull(gla29f, 0) + @procamt where gla00c = @compid and gla06c = @cardid and gla23i = 0 and gla02d = @curdate and gla03t <= @curtime end else if @procacct = '2' begin update glm01 set gla30f = isnull(gla30f, 0) + @procamt where gla00c = @compid and gla06c = @cardid and gla23i = 0 and gla02d = @curdate and gla03t <= @curtime end else if @procacct = '3' begin update glm01 set gla31f = isnull(gla31f, 0) + @procamt where gla00c = @compid and gla06c = @cardid and gla23i = 0 and gla02d = @curdate and gla03t <= @curtime end else if @procacct = '4' begin update glm01 set gla32f = isnull(gla32f, 0) + @procamt where gla00c = @compid and gla06c = @cardid and gla23i = 0 and gla02d = @curdate and gla03t <= @curtime end else if @procacct = '5' begin update glm01 set gla33f = isnull(gla33f, 0) + @procamt where gla00c = @compid and gla06c = @cardid and gla23i = 0 and gla02d = @curdate and gla03t <= @curtime end update glm01 set gla35f = isnull(gla35f, 0) + @procamt where gla00c = @compid and gla06c = @cardid and gla23i = 0 and gla34i = convert(int, @procacct) and gla02d = @curdate and gla03t <= @curtime end end GO /****** Object: StoredProcedure [dbo].[upg_stock_statistic_for_single] Script Date: 06/12/2017 06:20:45 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_stock_statistic_for_single] ( @compid varchar(10), -- 公司编号 @date varchar(10), -- 日期 @goodsfrom varchar(20), -- 产品编号from @goodsto varchar(20), -- 产品编号to @garagefrom varchar(20), -- 仓库编号from @garageto varchar(20) -- 仓库编号to ) as--WUXM 2006/02/19 begin create table #instock_buf -- 存放入库的数量(进货单位向标准单位转换) ( id int identity not null, compid varchar(10) null, -- 公司别 goodsno varchar(20) null, -- 产品编号 goodsname varchar(80) null, -- 产品名称 garageno varchar(20) null, -- 仓库编号 garagename varchar(20) null, -- 仓库名称 goodstype varchar(40) null, -- 产品类别 goodstype_key varchar(20) null, -- 产品类型 unit varchar(10) null, -- 单位 quantity float null, -- 数量 safetyquan float null, -- 安全存量 amt float null, -- 金额 provider varchar(20) null, -- 供应商 price float null, -- 标准单价 inprice float null, -- 销售价格 cost float null, -- 成本单价 amt_cost float null, -- 成本 ) declare @PK_Sql nvarchar(max) set @PK_Sql = ' alter table #instock_buf add constraint PK_#instock_buf_' + convert(varchar(10), cast(floor(rand() * 1000000) as int)) + ' primary key nonclustered(id)' exec sp_executesql @PK_Sql create clustered index idx_instock_001 on #instock_buf(goodsno) create table #outstock_buf -- 存放出库、销货、耗用的数(消耗单位向标准单位转换) ( id int identity not null, goodsno varchar(20) null, -- 产品编号 goodsname varchar(80) null, -- 产品名称 garageno varchar(20) null, -- 仓库编号 garagename varchar(20) null, -- 仓库名称 unit varchar(10) null, -- 单位 quantity float null, -- 数量 primary key nonclustered(id), ) create clustered index idx_out_stock_01 on #outstock_buf(goodsno) if isnull(@goodsfrom, '') = '' or isnull(@goodsfrom, '') = '*' begin -- 统计入库的数量放入#instock_buf (进货单位和标准单位) if isnull(@garagefrom, '') = '' or isnull(@garagefrom, '') = '*' begin insert #instock_buf(compid, goodsno, garageno, quantity) select @compid, gfd04c, gfd08c, isnull(sum(gfd05f), 0) from gfm03 with (nolock) join gfm04 with (nolock) on gfc00c = gfd00c and gfc01c = gfd01c and gfc02c = gfd02c where gfc00c = @compid and gfc01c = '1' and gfc03c <= @date group by gfd04c, gfd08c insert #outstock_buf(goodsno, goodsname, garageno, garagename, unit, quantity) select gfd04c, '', gfd08c, '', '', isnull(sum(gfd05f), 0) from gfm04 with (nolock) left outer join gfm03 with (nolock) on gfc00c = gfd00c and gfc01c = gfd01c and gfc02c = gfd02c where gfd00c = @compid and ( gfd01c = '2' or gfd01c = '3' or gfd01c = '4' or gfd01c = '5' or gfd01c = '6' or gfd01c = '7' or gfd01c = '8') and gfd07c <= @date group by gfd04c, gfd08c end else begin insert #instock_buf(compid, goodsno, garageno, quantity) select @compid, gfd04c, gfd08c, isnull(sum(gfd05f), 0) from gfm03 with (nolock) join gfm04 with (nolock) on gfc00c = gfd00c and gfc01c = gfd01c and gfc02c = gfd02c where gfc00c = @compid and gfc01c = '1' and gfc03c <= @date and gfd08c >= @garagefrom and gfd08c <= @garageto group by gfd04c, gfd08c insert #outstock_buf(goodsno, goodsname, garageno, garagename, unit, quantity) select gfd04c, '', gfd08c, '', '', isnull(sum(gfd05f), 0) from gfm04 with (nolock) left outer join gfm03 with (nolock) on gfc00c = gfd00c and gfc01c = gfd01c and gfc02c = gfd02c where gfd00c = @compid and ( gfd01c = '2' or gfd01c = '3' or gfd01c = '4' or gfd01c = '5' or gfd01c = '6' or gfd01c = '7' or gfd01c = '8') and gfd08c >= @garagefrom and gfd08c <= @garageto and gfd07c <= @date group by gfd04c, gfd08c end end else begin if isnull(@garagefrom, '') = '' or isnull(@garagefrom, '') = '*' begin insert #instock_buf(compid, goodsno, garageno, quantity) select @compid, gfd04c, gfd08c, isnull(sum(gfd05f), 0) from gfm03 with (nolock) join gfm04 with (nolock) on gfc00c = gfd00c and gfc01c = gfd01c and gfc02c = gfd02c where gfc00c = @compid and gfc01c = '1' and gfc03c <= @date and gfd04c >= @goodsfrom and gfd04c <= @goodsto group by gfd04c, gfd08c insert #outstock_buf(goodsno, goodsname, garageno, garagename, unit, quantity) select gfd04c, '', gfd08c, '', '', isnull(sum(gfd05f), 0) from gfm04 with (nolock) left outer join gfm03 with (nolock) on gfc00c = gfd00c and gfc01c = gfd01c and gfc02c = gfd02c where gfd00c = @compid and ( gfd01c = '2' or gfd01c = '3' or gfd01c = '4' or gfd01c = '5' or gfd01c = '6' or gfd01c = '7' or gfd01c = '8') and gfd04c >= @goodsfrom and gfd04c <= @goodsto and gfd07c <= @date group by gfd04c, gfd08c end else begin insert #instock_buf(compid, goodsno, garageno, quantity) select @compid, gfd04c, gfd08c, isnull(sum(gfd05f), 0) from gfm03 with (nolock) join gfm04 with (nolock) on gfc00c = gfd00c and gfc01c = gfd01c and gfc02c = gfd02c where gfc00c = @compid and gfc01c = '1' and gfc03c <= @date and gfd08c >= @garagefrom and gfd08c <= @garageto and gfd04c >= @goodsfrom and gfd04c <= @goodsto group by gfd04c, gfd08c insert #outstock_buf(goodsno, goodsname, garageno, garagename, unit, quantity) select gfd04c, '', gfd08c, '', '', isnull(sum(gfd05f), 0) from gfm04 with (nolock) left outer join gfm03 with (nolock) on gfc00c = gfd00c and gfc01c = gfd01c and gfc02c = gfd02c where gfd00c = @compid and ( gfd01c = '2' or gfd01c = '3' or gfd01c = '4' or gfd01c = '5' or gfd01c = '6' or gfd01c = '7' or gfd01c = '8') and gfd08c >= @garagefrom and gfd08c <= @garageto and gfd04c >= @goodsfrom and gfd04c <= @goodsto and gfd07c <= @date group by gfd04c, gfd08c end end -- 统计出库(包括销货)的数量更新库存量 update t set t.quantity = isnull(t.quantity, 0) - isnull(a.quantity, 0) from #outstock_buf a, #instock_buf t where a.goodsno = t.goodsno and a.garageno = t.garageno update a set a.amt = isnull(a.quantity, 0) * isnull(b.gfa11f, 0), a.price = isnull(b.gfa11f, 0), a.inprice = isnull(b.gfa22f, 0), a.goodsname = b.gfa03c, a.goodstype = b.gfa05c, a.goodstype_key = b.gfa24c, a.unit = b.gfa07c, a.provider = b.gfa23c from #instock_buf a, gfm01 b where a.goodsno = b.gfa01c and b.gfa00c = @compid declare @SP054f float -- 成本计算方式 select @SP054f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP054' -- 需要修改成本 if @SP054f = 2 -- 月加权 begin update a set a.cost = b.gfh03f, a.amt_cost = isnull(b.gfh03f, 0) * a.quantity from #instock_buf a, gfm08 b with(nolock) where a.goodsno = b.gfh01c and b.gfh00c = @compid and b.gfh02c = substring(@date, 1, 6) end else begin update a set a.cost = b.gfa22f, a.amt_cost = isnull(b.gfa22f, 0) * a.quantity from #instock_buf a, gfm01 b with(nolock) where a.goodsno = b.gfa01c and b.gfa00c = @compid end update a set a.unit = b.gsb03c from #instock_buf a, gsm02 b where b.gsb00c = @compid and b.gsb01c = 'K' and a.unit = b.gsb02c update a set a.garagename = b.gad02c from #instock_buf a, gam04 b where b.gad00c = @compid and b.gad01c = a.garageno update a set a.goodstype = b.gsb03c from #instock_buf a, gsm02 b where b.gsb00c = @compid and b.gsb01c = 'F' and a.goodstype = b.gsb02c select compid, goodsno, goodsname, garageno, garagename, goodstype, goodstype_key, price, inprice, unit, quantity = (cast(quantity as numeric(38, 5))), amt = (cast(amt as numeric(38, 5))), provider, cost, amt_cost = (cast(amt_cost as numeric(38, 5))) from #instock_buf order by goodstype, goodsno drop table #instock_buf drop table #outstock_buf end GO /****** Object: StoredProcedure [dbo].[upg_update_glm01account] Script Date: 06/12/2017 06:20:49 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_update_glm01account] ( @compid varchar(10), -- 公司编号 @enterno varchar(20), -- 入院单号 @billno varchar(20) -- 收银单号 ) as -- WUXM 2005/09/09 modified by WUXM 2005/09/14 modified by WUXM 2005/09/15 set nocount on set rowcount 0 set transaction isolation level read uncommitted begin declare @consume_amt float --结帐金额 declare @payment varchar(5) --支付方式 declare @checkout int --结账方式 declare @acctid int -- 账户代码 --获得该客户的支付方式 select @payment = gsn01c, @acctid = gga27i from gsm14, ggm01 where gga00c = @compid and gga01c = @billno and gsn02c = cast(gga27i as varchar(5)) --获得系统参数中设定的结帐方式 select @checkout = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP031' if (@checkout = 1) --收银时结帐 begin select @consume_amt = isnull(sum(gsc05f), 0) from gsm03 c where c.gsc00c = @compid and c.gsc01c = @billno and c.gsc02c = 'gx' and c.gsc04c = @payment end else --统一结帐 begin select @consume_amt = isnull(sum(gsc05f), 0) from gsm03 b where b.gsc00c = @compid and b.gsc01c = @enterno and b.gsc02c = 'gl' and b.gsc04c = @payment end --在会员入院资料中更新该会员这次消费的消费方式和消费金额 update a set a.gla34i = @acctid, a.gla36f = @consume_amt from glm01 a where a.gla00c = @compid and a.gla01c = @enterno end GO /****** Object: StoredProcedure [dbo].[upg_consume_statistic_all_ex] Script Date: 06/12/2017 06:19:46 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_consume_statistic_all_ex] @compid varchar(10), @from varchar(8), @to varchar(8), @type int, --1-本公司的卡在别的店消费,0-别的公司的卡在本公司消费 @fromcard varchar(20), @tocard varchar(20), @goods int, --1列印,0不列印 @project int, --1列印,0不列印 @cardclass varchar(10), --会员卡类别 @fill int, --1列印,0不列印 --add by lyj 打印充值 @payway varchar(10) as begin create table #result ( idd int identity not null, comp varchar(5) null, --公司别 cardno varchar(20) null, --会员卡号 billno varchar(20) null, --消费单号 billdate varchar(8) null, --消费日期 payway varchar(20) null, --支付方式 price float null, --单价 amt float null, --金额 type varchar(10) null, --类型,1-本公司的卡在别的店消费,0-别的公司的卡在本公司消费 consume varchar(10)null, --消费类型 1项目,2物品, 3充值 class varchar(10) null, --会员卡类别 prj_goods varchar(20) null, samt float null, num float null default 0, znum float null default 0, primary key(idd) ) create table #cardnoinfo ( gca13d varchar(5) not null, --卡的归属公司 gca01c varchar(20) not null, --卡号 gca02c varchar(5) null, --卡类型 ) create table #projectcash --收银项目 ( gga00c varchar(5) not null, --公司别 gga01c varchar(20) not null, --单据号 gga80d varchar(8) null, --日期 gga30c varchar(20) null, gga09c varchar(20) null, ) create table #goodscash --收银物品 ( gga00c varchar(5) not null, --公司别 gga01c varchar(20) not null, --单据号 gga80d varchar(8) null, --日期 gga30c varchar(20) null, gga09c varchar(20) null, ) create table #gsm03 ( gsc00c char(10) not null, --会馆编号 gsc01c varchar(20) not null, --单据编号 gsc02c varchar(5) not null, --单据类别 gsc03f float not null, --序号 gsc04c varchar(5) null, --支付方式 gsc05f float null, --支付金额 ) declare @sp031f float --1:服务时收银 2:统一出院收银' select @sp031f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP031' if (@type = 1) ---本公司的卡在别的店消费 begin insert into #gsm03(gsc00c, gsc01c, gsc02c, gsc03f, gsc04c, gsc05f) select gsc00c, gsc01c, gsc02c, gsc03f, gsc04c, gsc05f from gsm03 with (nolock) where gsc00c<>@compid and gsc10d between @from and @to and (gsc04c = @payway or @payway = '*') insert into #cardnoinfo(gca13d, gca01c, gca02c) select distinct gca13d, gca01c, gca02c from gcm01 with (nolock) where (gca01c between @fromcard and @tocard or @fromcard = '*') and (gca02c = @cardclass or @cardclass = '*') and gca13d = @compid and substring(gca01c, 1, 2)<>'散客' --这么做的原因是因为有可能一个收银单有项目也有物品 ,这种情况钱就算在项目上 insert into #projectcash(gga00c, gga01c, gga80d, gga30c, gga09c) select gga00c, gga01c, gga80d, gga30c, gga09c from ggm01 a with (nolock), #cardnoinfo b where a.gga00c<>@compid and gga80d between @from and @to and a.gga09c = b.gca01c --在别的公司的消费纪录 if (@sp031f = 1)--收银的时候统一结帐 begin if (@goods = 1) begin insert into #result(cardno, billno, billdate, payway, amt, type, consume, class, comp) select gga09c, gga01c, gga80d, gsc04c, gsc05f, 1, 2, gca02c, gga00c from #goodscash , #gsm03, #cardnoinfo where gga09c = gca01c and gga00c = gsc00c and gga01c = gsc01c and gsc02c = 'gx' and gga00c <> @compid end if (@project = 1) begin insert into #result(cardno, billno, billdate, payway, amt, type, consume, class, comp) select gga09c, gga01c, gga80d, gsc04c, gsc05f, 1, 1, gca02c, gga00c from #projectcash, #gsm03, #cardnoinfo where gga09c = gca01c and gga00c = gsc00c and gga01c = gsc01c and gsc02c = 'gx' and gga00c <> @compid end end else --多账单统一出院 begin if (@goods = 1) begin insert into #result(cardno, billno, billdate, payway, amt, type, consume, class, comp) select gga09c, gga01c, gga80d, gsc04c, gsc05f, 1, 2, gca02c, gga00c from #goodscash, glm01 with (nolock), #gsm03, #cardnoinfo where gga09c = gca01c and gga00c = gsc00c and gla01c = gsc01c and gga00c = gla00c and gga30c = gla01c and gsc02c = 'gl' and gga00c <> @compid end if (@project = 1) begin insert into #result(cardno, billno, billdate, payway, amt, type, consume, class, comp) select gga09c, gga01c, gga80d, gsc04c, gsc05f, 1, 1, gca02c, gga00c from #projectcash, glm01 with (nolock), #gsm03, #cardnoinfo where gga09c = gca01c and gga00c = gsc00c and gla01c = gsc01c and gga30c = gla01c and gga00c = gla00c and gla00c = gsc01c and gsc02c = 'gl' and gga00c <> @compid end end if (@fill = 1) begin insert into #result(cardno, billno, billdate, payway, amt, type, consume, class, comp) select gcl03c, gcl01c, gcl80d, gsc04c, gsc05f, 1, 3, gcl15c, gcl00c from gcm10 a with (nolock), #gsm03 b, #cardnoinfo c where a.gcl00c = b.gsc00c and a.gcl01c = b.gsc01c and b.gsc02c = 'gz' and (gcl06i = 0 or gcl06i = 6) and a.gcl00c<>@compid and a.gcl80d between @from and @to and a.gcl03c = c.gca01c insert into #result(cardno, billno, billdate, payway, amt, type, consume, class, comp) select gcl03c, gcl01c, gcl80d, gsc04c, (-1)*gsc05f, 1, 3, gcl15c, gcl00c from gcm10 a with (nolock), #gsm03 b, #cardnoinfo c where gcl00c = gsc00c and gcl01c = gsc01c and gsc02c = 'gz' and gcl06i = 1 and gcl00c<>@compid and gcl80d between @from and @to and a.gcl03c = c.gca01c end end else begin --别的卡在我公司的消费 --在本公司的支付明细 insert into #gsm03(gsc00c, gsc01c, gsc02c, gsc03f, gsc04c, gsc05f) select gsc00c, gsc01c, gsc02c, gsc03f, gsc04c, gsc05f from gsm03 with (nolock) where gsc00c = @compid and gsc10d between @from and @to and (gsc04c = @payway or @payway = '*') --符合条件的别公司的卡 insert into #cardnoinfo(gca13d, gca01c, gca02c) select distinct gca13d, gca01c, gca02c from gcm01 with (nolock) where (gca01c between @fromcard and @tocard or @fromcard = '*') and (gca02c = @cardclass or @cardclass = '*') and gca13d <> @compid and substring(gca01c, 1, 2)<>'散客' --这么做的原因是因为有可能一个收银单有项目也有物品 ,这种情况钱就算在项目上 insert into #projectcash(gga00c, gga01c, gga80d, gga30c, gga09c) select gga00c, gga01c, gga80d, gga30c, gga09c from ggm01 a with (nolock), #cardnoinfo b where a.gga00c = @compid -- 我公司的消费 and (gga80d between @from and @to) and a.gga09c = b.gca01c if (@sp031f = 1)--收银的时候统一结帐 begin if (@goods = 1) begin insert into #result(cardno, billno, billdate, payway, amt, type, consume, class, comp) select gga09c, gga01c, gga80d, gsc04c, gsc05f, 1, 2, gca02c, gca13d from #goodscash a, #gsm03 b, #cardnoinfo c where a.gga09c = c.gca01c and a.gga00c = @compid and a.gga00c = b.gsc00c and a.gga01c = b.gsc01c and b.gsc02c = 'gx' end if (@project = 1) begin insert into #result(cardno, billno, billdate, payway, amt, type, consume, class, comp) select gga09c, gga01c, gga80d, gsc04c, gsc05f, 1, 1, gca02c, gca13d from #projectcash a, #gsm03 b, #cardnoinfo c where a.gga09c = c.gca01c and a.gga00c = @compid and a.gga00c = b.gsc00c and a.gga01c = b.gsc01c and b.gsc02c = 'gx' end end else --多账单统一出院 begin if (@goods = 1) begin insert into #result(cardno, billno, billdate, payway, amt, type, consume, class, comp) select gga09c, gga01c, gga80d, gsc04c, gsc05f, 1, 2, gca02c, gca13d from #goodscash a, glm01 b with (nolock), #gsm03 c, #cardnoinfo d where a.gga09c = d.gca01c and a.gga00c = @compid and a.gga00c = c.gsc00c and b.gla01c = c.gsc01c and a.gga00c = b.gla00c and a.gga30c = b.gla01c and b.gla00c = c.gsc00c and c.gsc02c = 'gl' end if (@project = 1) begin insert into #result(cardno, billno, billdate, payway, amt, type, consume, class, comp) select gga09c, gga01c, gga80d, gsc04c, gsc05f, 1, 1, gca02c, gca13d from #projectcash , glm01 with (nolock), #gsm03, #cardnoinfo where gga09c = gca01c and gga00c = @compid and gga00c = gsc00c and gla01c = gsc01c and gga30c = gla01c and gga00c = gla00c and gla01c = gsc01c and gsc02c = 'gl' end end if (@fill = 1) begin insert into #result(cardno, billno, billdate, payway, amt, type, consume, class, comp) select gcl03c, gcl01c, gcl80d, gsc04c, gsc05f, 1, 3, gcl15c, gca13d from gcm10 a with (nolock), #gsm03 b, #cardnoinfo c where a.gcl00c = b.gsc00c and a.gcl01c = b.gsc01c and b.gsc02c = 'gz' and (a.gcl06i = 0 or a.gcl06i = 6) and a.gcl00c = @compid and (a.gcl80d between @from and @to) and a.gcl03c = c.gca01c insert into #result(cardno, billno, billdate, payway, amt, type, consume, class, comp) select gcl03c, gcl01c, gcl80d, gsc04c, (-1)*gsc05f, 1, 3, gcl15c, gca13d from gcm10 a with (nolock), #gsm03 b, #cardnoinfo c where a.gcl00c = b.gsc00c and a.gcl01c = b.gsc01c and b.gsc02c = 'gz' and a.gcl06i = 1 and a.gcl00c = @compid and gcl80d between @from and @to and a.gcl03c = c.gca01c end end update a set a.samt = isnull(a.amt*isnull(b.gso03i, 1), 0) from #result a, gsm15 b where a.comp = b.gso00c and a.payway = b.gso02c and a.consume = '1' and a.payway<>'4' update a set a.samt = isnull(a.amt*isnull(b.gso06f, 1), 0) from #result a, gsm15 b where a.comp = b.gso00c and a.payway = b.gso02c and a.consume = '2' and a.payway<>'4' update a set a.samt = isnull(a.amt*isnull(c.gak24f, 0), 0) from #result a, gam10 c where a.comp = c.gak00c and a.class = c.gak01c and consume = '2' and a.payway = '4' update a set a.samt = isnull(a.amt*isnull(b.gso05f, 1), 0) from #result a, gsm15 b where a.comp = b.gso00c and a.payway = b.gso02c and a.consume = '3' delete #gsm03 delete #cardnoinfo delete #goodscash delete #projectcash drop table #gsm03 drop table #cardnoinfo drop table #goodscash drop table #projectcash select * from #result order by comp, class, cardno end GO /****** Object: StoredProcedure [dbo].[upg_stock_statistic] Script Date: 06/12/2017 06:20:45 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_stock_statistic] ( @compid varchar(10), -- 公司编号 @date varchar(10), -- 日期 @goodsfrom varchar(20), -- 产品编号from @goodsto varchar(20), -- 产品编号to @garagefrom varchar(20), -- 仓库编号from @garageto varchar(20), -- 仓库编号to @returntype int, -- 1-返回所有满足条件的产品,2-返回库存量少的产品 3-返回现库存量,安全存量,低于安全存量, 5-用于库存量统计报表 @quan float, -- 安全库存量 @provider varchar(20), -- 供应商 @kindfrom varchar(20), -- 开始产品类别 @kindto varchar(20), -- 截至产品类别 @sorttype int -- 排序类别 ) as--wuxm, 2004/07/10 --WUXM modified 2005/08/10 begin declare @incompid varchar(10) select @incompid = @compid declare @includesons float select @includesons = gsa03f from gsm01 where gsa00c = @incompid and gsa01c = 'SP103' create table #in_remain_stock --存放入库的数量(进货单位向标准单位转换) ( id int identity not null, goodsno varchar(20) null, --产品编号 goodsname varchar(80) null, --产品名称 garageno varchar(20) null, --仓库编号 garagename varchar(20) null, --仓库名称 goodstype varchar(40) null, --产品类别 unit varchar(10) null, --单位 quantity float null, --数量 safetyquan float null, --安全存量 maxsafequan float null, --最高安全库存 --add by liyh amt float null, --金额 amt_cost float null, --成本价核算成本 price float null, --标准单价 cost float null, --成本单价 provider varchar(20) null, --供应商 stattype varchar(20) null, --统计类别 goodspec varchar(40) null, --产品规格 primary key nonclustered(id) ) create clustered INDEX IDX_in_remain_stock_01 on #in_remain_stock(goodsno, garageno) create table #out_stock -- 存放出库、销货、耗用的数(消耗单位向标准单位转换) ( id int identity not null, goodsno varchar(20) null, --产品编号 goodsname varchar(80) null, --产品名称 garageno varchar(20) null, --仓库编号 garagename varchar(20) null, --仓库名称 unit varchar(10) null, --单位 quantity float null, --数量 primary key nonclustered(id) ) create clustered INDEX IDX_out_stock_01 on #out_stock(goodsno, garageno) if @includesons = 1 begin insert #in_remain_stock(goodsno, goodsname, garageno, garagename, goodstype, unit, quantity, safetyquan, amt) select gfd04c, '', gfd08c, '', '', '', sum(isnull(gfd05f, 0)), --modify by lm 20150627(gfd05f标准单位数量改为入库单位或出库单位数量) 0, sum(isnull(gfd13f, 0)) from gfm04 b with (nolock), gam26 z, gfm01 c with (nolock) where gfd00c = z.gaz02c and z.gaz01c = @incompid and gfd01c = '1' and b.gfd04c = c.gfa01c and c.gfa00c = @incompid and b.gfd07c <= @date and ((@garagefrom = '*') or (gfd08c between @garagefrom and @garageto)) and ((@goodsfrom = '*') or (gfd04c between @goodsfrom and @goodsto)) and ((@provider = '*') or (gfa23c = @provider)) and ((@kindfrom = '*') or (gfa24c between @kindfrom and @kindto)) group by gfd04c, gfd08c insert #out_stock(goodsno, goodsname, garageno, garagename, unit, quantity) select gfd04c, '', gfd08c, '', '', sum(isnull(gfd05f, 0)) --modify by lm 20150627(gfd05f标准单位数量改为入库单位或出库单位数量) from gfm04 b with (nolock), gam26 z, gfm01 c with (nolock) where gfd00c = z.gaz02c and z.gaz01c = @incompid and gfd01c in ('2', '3', '4', '5', '6', '7', '8') and b.gfd04c = c.gfa01c and c.gfa00c = @incompid and gfd07c <= @date and ((@garagefrom = '*') or (gfd08c between @garagefrom and @garageto)) and ((@goodsfrom = '*') or (gfd04c between @goodsfrom and @goodsto)) and ((@provider = '*') or (gfa23c = @provider)) and ((@kindfrom = '*') or (gfa24c between @kindfrom and @kindto)) group by gfd04c, gfd08c end else begin insert #in_remain_stock(goodsno, goodsname, garageno, garagename, goodstype, unit, quantity, safetyquan, amt) select gfd04c, '', gfd08c, '', '', '', sum(isnull(gfd05f, 0)), --modify by lm 20150627(gfd05f标准单位数量改为入库单位或出库单位数量) 0, sum(isnull(gfd13f, 0)) from gfm04 b with (nolock), gfm01 c with (nolock) where gfd00c = @incompid and gfd01c = '1' and b.gfd04c = c.gfa01c and c.gfa00c = @incompid and b.gfd07c <= @date and ((@garagefrom = '*') or (gfd08c between @garagefrom and @garageto)) and ((@goodsfrom = '*') or (gfd04c between @goodsfrom and @goodsto)) and ((@provider = '*') or (gfa23c = @provider)) and ((@kindfrom = '*') or (gfa24c between @kindfrom and @kindto)) group by gfd04c, gfd08c --select * from #in_remain_stock insert #out_stock(goodsno, goodsname, garageno, garagename, unit, quantity) select gfd04c, '', gfd08c, '', '', sum(isnull(gfd05f, 0)) --modify by lm 20150627(gfd05f标准单位数量改为入库单位或出库单位数量) from gfm04 b with (nolock), gfm01 c with (nolock) where gfd00c = @incompid and gfd01c in ('2', '3', '4', '5', '6', '7', '8') and b.gfd04c = c.gfa01c and c.gfa00c = @incompid and gfd07c <= @date and ((@garagefrom = '*') or (gfd08c between @garagefrom and @garageto)) and ((@goodsfrom = '*') or (gfd04c between @goodsfrom and @goodsto)) and ((@provider = '*') or (gfa23c = @provider)) and ((@kindfrom = '*') or (gfa24c between @kindfrom and @kindto)) group by gfd04c, gfd08c end --统计出库(包括销货)的数量更新库存量 update t set t.quantity = t.quantity - a.quantity from #out_stock a, #in_remain_stock t where a.goodsno = t.goodsno and a.garageno = t.garageno --注意可能没有入库的而只有出库的产品 delete a from #out_stock a, #in_remain_stock t where a.goodsno = t.goodsno and a.garageno = t.garageno insert #in_remain_stock(goodsno, goodsname, garageno, garagename, unit, quantity) select goodsno, goodsname, garageno, garagename, unit, 0 - quantity from #out_stock --更新仓库名称 update a set a.garagename = b.gad02c from #in_remain_stock a, gam04 b where a.garageno = b.gad01c and b.gad00c = @compid --更新安全存量,更新产品类别, update a set a.amt = isnull(a.quantity, 0) * isnull(b.gfa11f, 0), a.price = isnull(b.gfa11f, 0), a.goodsname = b.gfa03c, a.goodspec = b.gfa12c, a.safetyquan = isnull(b.gfa18f, 0), a.unit = b.gfa07c, a.goodstype = b.gfa05c, a.provider = b.gfa23c, a.stattype = b.gfa24c, a.maxsafequan = isnull(b.gfa29f, 0) from #in_remain_stock a, gfm01 b where a.goodsno = b.gfa01c and b.gfa00c = @compid declare @SP054f float -- 成本计算方式 select @SP054f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP054' --需要修改成本 if @SP054f = 2 begin update a set a.amt_cost = isnull(a.quantity, 0) * isnull(b.gfh03f, 0), a.cost = isnull(b.gfh03f, 0) from #in_remain_stock a, gfm08 b where a.goodsno = b.gfh01c and b.gfh00c = @compid and b.gfh02c = substring(@date, 1, 6) end else begin update a set a.amt_cost = isnull(a.quantity, 0) * isnull(b.gfa22f, 0), a.cost = isnull(b.gfa22f, 0) from #in_remain_stock a, gfm01 b where a.goodsno = b.gfa01c and b.gfa00c = @compid end update a set a.unit = b.gsb03c from #in_remain_stock a, gsm02 b where a.unit = b.gsb02c and b.gsb00c = @compid and b.gsb01c = 'K' --判断是否统计已经停止之产品 declare @include_invalid_good varchar(20) select @include_invalid_good = gsa02c from gsm01 where gsa01c = 'SP113' and gsa00c = @compid if @include_invalid_good = '0' begin --删除已经停止之产品 delete a from #in_remain_stock a, gfm01 b where a.goodsno = b.gfa01c and b.gfa00c = @compid and isnull(gfa10c, '0') = '1' end if @returntype = 1 begin select isnull(goodsno,'') goodsno, isnull(goodsname,'') goodsname, isnull(garageno,'') garageno, isnull(garagename,'') garagename, isnull(goodstype,'') goodstype, isnull(price,0) price, isnull(unit,'') unit, isnull(quantity,0) quantity, isnull(amt,0) amt, isnull(stattype,'') stattype from #in_remain_stock order by goodstype, goodsno end else if @returntype = 2 begin select isnull(goodsno,'') goodsno, isnull(goodsname,'') goodsname, isnull(unit,'') unit, isnull(quantity,0) quantity, isnull(provider,'') provider from #in_remain_stock where quantity < isnull(safetyquan, 0) order by goodsno end else if @returntype = 3 begin select isnull(goodsno,'') goodsno, isnull(goodsname,'') goodsname, isnull(garageno,'') garageno, isnull(garagename,'') garagename, isnull(quantity,0) quantity, isnull(unit,'') unit, isnull(safetyquan, 0) safetyquan, val = (isnull(safetyquan, 0) - isnull(quantity, 0)) from #in_remain_stock where isnull(safetyquan, 0) - isnull(quantity, 0) > 0 order by val desc end else if @returntype = 4 begin select isnull(goodsno,'') goodsno, isnull(goodsname,'') goodsname, isnull(garageno,'') garageno, isnull(garagename,'') garagename, isnull(unit,'') unit, isnull(quantity,0) quantity, isnull(amt,0) amt, isnull(amt_cost,0) amt_cost from #in_remain_stock order by goodstype, goodsno end else if @returntype = 100503 begin select isnull(goodsno,'') goodsno, isnull(goodsname,'') goodsname, isnull(garageno,'') garageno, isnull(garagename,'') garagename, isnull(quantity,0) quantity, isnull(unit,'') unit, isnull(maxsafequan, 0) safetyquan, val = (isnull(quantity, 0) - isnull(maxsafequan, 0)) from #in_remain_stock where (isnull(quantity, 0) - isnull(maxsafequan, 0)) > 0 order by val desc end else if @returntype = 5 or @returntype = 6 begin --Added by WUXM, 2005/11/02 --用于库存统计报表,原用的是@returntype = 1, 先改为返回数量为一个字串 --因别的函数或SP中用了@returntype = 1的,所以只能新增加类型为5 if @returntype = 5 begin delete #in_remain_stock where isnull(quantity, 0) = 0 end if (isnull(@provider, '') = '' or isnull(@provider, '') = '*') --所有供应商的产品 begin if (@sorttype = 0) begin select d.* from ( select isnull(goodsno,'') goodsno, isnull(goodsname,'') goodsname, isnull(garageno,'') garageno, isnull(garagename,'') garagename, isnull(goodstype,'') goodstype, isnull(price,0) price, isnull(cost,0) cost, isnull(unit,'') unit, quantity = '', isnull(amt_cost,0) amt_cost, isnull(amt,0) amt, quan = isnull(quantity,0), isnull(stattype,'') stattype, isnull(goodspec,'') goodspec from #in_remain_stock a where a.provider = @provider union select isnull(gfa01c,'') as goodsno, isnull(gfa03c,'') as goodsname, '' as garageno, '' as garagename, '' as goodstype, 0 as price, isnull(gfa22f,0) as cost, isnull(b.gsb03c,'') as unit, 0 as quantity, 0 as amt_cost, 0 as amt, 0 as quan, --'' as stattype, isnull(gfa24c,'') as stattype, isnull(gfa12c,'') as goodspec from gfm01 c, gsm02 b where c.gfa20c = b.gsb02c and b.gsb00c = @compid and c.gfa00c = @compid and b.gsb01c = 'K' and isnull(c.gfa10c, '1') <> '1' and ((@goodsfrom = '*') or (gfa01c between @goodsfrom and @goodsto)) and ((gfa23c= @provider) or @provider='*') and ((@kindfrom = '*') or (gfa24c between @kindfrom and @kindto)) and gfa01c not in(select goodsno from #in_remain_stock)) d --modify by limin -2014-12-11 where ((@garagefrom = '*') or (d.garageno between @garagefrom and @garageto)) order by d.goodsno end else if (@sorttype = 1) begin select d.* from ( select isnull(goodsno,'') goodsno, isnull(goodsname,'') goodsname, isnull(garageno,'') garageno, isnull(garagename,'') garagename, isnull(goodstype,'') goodstype, isnull(price,0) price, isnull(cost,0) cost, isnull(unit,'') unit, quantity = '', isnull(amt_cost,0) amt_cost, isnull(amt,0) amt, quan = isnull(quantity,0), isnull(stattype,'') stattype, isnull(goodspec,'') goodspec from #in_remain_stock a where a.provider = @provider union select isnull(gfa01c,'') as goodsno, isnull(gfa03c,'') as goodsname, '' as garageno, '' as garagename, '' as goodstype, 0 as price, isnull(gfa22f,0) as cost, isnull(b.gsb03c,'') as unit, 0 as quantity, 0 as amt_cost, 0 as amt, 0 as quan, --'' as stattype, isnull(gfa24c,'') as stattype, isnull(gfa12c,'') as goodspec from gfm01 c, gsm02 b where c.gfa20c = b.gsb02c and b.gsb00c = @compid and c.gfa00c = @compid and b.gsb01c = 'K' and isnull(c.gfa10c, '1') <> '1' and ((@goodsfrom = '*') or (gfa01c between @goodsfrom and @goodsto)) and ((gfa23c= @provider) or @provider='*') and ((@kindfrom = '*') or (gfa24c between @kindfrom and @kindto)) and gfa01c not in(select goodsno from #in_remain_stock)) d --modify by limin -2014-12-11 where ((@garagefrom = '*') or (d.garageno between @garagefrom and @garageto)) order by d.goodstype end else if (@sorttype = 2) begin select d.* from ( select isnull(goodsno,'') goodsno, isnull(goodsname,'') goodsname, isnull(garageno,'') garageno, isnull(garagename,'') garagename, isnull(goodstype,'') goodstype, isnull(price,0) price, isnull(cost,0) cost, isnull(unit,'') unit, quantity = '', isnull(amt_cost,0) amt_cost, isnull(amt,0) amt, quan = isnull(quantity,0), isnull(stattype,'') stattype, isnull(goodspec,'') goodspec from #in_remain_stock a where a.provider = @provider union select isnull(gfa01c,'') as goodsno, isnull(gfa03c,'') as goodsname, '' as garageno, '' as garagename, '' as goodstype, 0 as price, isnull(gfa22f,0) as cost, isnull(b.gsb03c,'') as unit, 0 as quantity, 0 as amt_cost, 0 as amt, 0 as quan, --'' as stattype, isnull(gfa24c,'') as stattype, isnull(gfa12c,'') as goodspec from gfm01 c, gsm02 b where c.gfa20c = b.gsb02c and b.gsb00c = @compid and c.gfa00c = @compid and b.gsb01c = 'K' and isnull(c.gfa10c, '1') <> '1' and ((@goodsfrom = '*') or (gfa01c between @goodsfrom and @goodsto)) and ((gfa23c= @provider) or @provider='*') and ((@kindfrom = '*') or (gfa24c between @kindfrom and @kindto)) and gfa01c not in(select goodsno from #in_remain_stock)) d --modify by limin -2014-12-11 where ((@garagefrom = '*') or (d.garageno between @garagefrom and @garageto)) order by d.provider end end else --指定供应商的产品 begin if (@sorttype = 0) begin select d.* from ( select isnull(goodsno,'') goodsno, isnull(goodsname,'') goodsname, isnull(garageno,'') garageno, isnull(garagename,'') garagename, isnull(goodstype,'') goodstype, isnull(price,0) price, isnull(cost,0) cost, isnull(unit,'') unit, quantity = '', isnull(amt_cost,0) amt_cost, isnull(amt,0) amt, quan = isnull(quantity,0), isnull(stattype,'') stattype, isnull(goodspec,'') goodspec from #in_remain_stock a where a.provider = @provider union select isnull(gfa01c,'') as goodsno, isnull(gfa03c,'') as goodsname, '' as garageno, '' as garagename, '' as goodstype, 0 as price, isnull(gfa22f,0) as cost, isnull(b.gsb03c,'') as unit, 0 as quantity, 0 as amt_cost, 0 as amt, 0 as quan, --'' as stattype, isnull(gfa24c,'') as stattype, isnull(gfa12c,'') as goodspec from gfm01 c, gsm02 b where c.gfa20c = b.gsb02c and b.gsb00c = @compid and c.gfa00c = @compid and b.gsb01c = 'K' and isnull(c.gfa10c, '1') <> '1' and ((@goodsfrom = '*') or (gfa01c between @goodsfrom and @goodsto)) and ((gfa23c= @provider) or @provider='*') and ((@kindfrom = '*') or (gfa24c between @kindfrom and @kindto)) and gfa01c not in(select goodsno from #in_remain_stock )) d --modify by limin -2014-12-11 where ((@garagefrom = '*') or (d.garageno between @garagefrom and @garageto)) order by d.goodsno end else if (@sorttype = 1) begin select d.* from ( select isnull(goodsno,'') goodsno, isnull(goodsname,'') goodsname, isnull(garageno,'') garageno, isnull(garagename,'') garagename, isnull(goodstype,'') goodstype, isnull(price,0) price, isnull(cost,0) cost, isnull(unit,'') unit, quantity = '', isnull(amt_cost,0) amt_cost, isnull(amt,0) amt, quan = isnull(quantity,0), isnull(stattype,'') stattype, isnull(goodspec,'') goodspec from #in_remain_stock a where a.provider = @provider union select isnull(gfa01c,'') as goodsno, isnull(gfa03c,'') as goodsname, '' as garageno, '' as garagename, '' as goodstype, 0 as price, isnull(gfa22f,0) as cost, isnull(b.gsb03c,'') as unit, 0 as quantity, 0 as amt_cost, 0 as amt, 0 as quan, --'' as stattype, isnull(gfa24c,'') as stattype, isnull(gfa12c,'') as goodspec from gfm01 c, gsm02 b where c.gfa20c = b.gsb02c and b.gsb00c = @compid and c.gfa00c = @compid and b.gsb01c = 'K' and isnull(c.gfa10c, '1') <> '1' and ((@goodsfrom = '*') or (gfa01c between @goodsfrom and @goodsto)) and ((gfa23c= @provider) or @provider='*') and ((@kindfrom = '*') or (gfa24c between @kindfrom and @kindto)) and gfa01c not in(select goodsno from #in_remain_stock )) d --modify by limin -2014-12-11 where ((@garagefrom = '*') or (d.garageno between @garagefrom and @garageto)) order by d.goodstype end else if (@sorttype = 2) begin select d.* from ( select isnull(goodsno,'') goodsno, isnull(goodsname,'') goodsname, isnull(garageno,'') garageno, isnull(garagename,'') garagename, isnull(goodstype,'') goodstype, isnull(price,0) price, isnull(cost,0) cost, isnull(unit,'') unit, quantity = '', isnull(amt_cost,0) amt_cost, isnull(amt,0) amt, quan = isnull(quantity,0), isnull(stattype,'') stattype, isnull(goodspec,'') goodspec from #in_remain_stock a where a.provider = @provider union select isnull(gfa01c,'') as goodsno, isnull(gfa03c,'') as goodsname, '' as garageno, '' as garagename, '' as goodstype, 0 as price, isnull(gfa22f,0) as cost, isnull(b.gsb03c,'') as unit, 0 as quantity, 0 as amt_cost, 0 as amt, 0 as quan, --'' as stattype, isnull(gfa24c,'') as stattype, isnull(gfa12c,'') as goodspec from gfm01 c, gsm02 b where c.gfa20c = b.gsb02c and b.gsb00c = @compid and c.gfa00c = @compid and b.gsb01c = 'K' and isnull(c.gfa10c, '1') <> '1' and ((@goodsfrom = '*') or (gfa01c between @goodsfrom and @goodsto)) and ((gfa23c= @provider) or @provider='*') and ((@kindfrom = '*') or (gfa24c between @kindfrom and @kindto)) and gfa01c not in(select goodsno from #in_remain_stock)) d --modify by limin -2014-12-11 where ((@garagefrom = '*') or (d.garageno between @garagefrom and @garageto)) order by d.provider end end end drop table #in_remain_stock drop table #out_stock end GO /****** Object: StoredProcedure [dbo].[upg_m_dispatch_gfm01] Script Date: 06/12/2017 06:20:19 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_m_dispatch_gfm01] ( @fromcompid varchar(10), -- 从哪一个公司发布 @tocompid varchar(10), -- 目的公司 @mandatory varchar(2) -- 是否强制统一 ) as begin -- Add by maql 20100506 declare @curdate varchar(20), @curtime varchar(20) exec upg_getcurrdate @curdate output, @curtime output if @fromcompid = @tocompid return create table #gfm01_01 -- 产品资料档 ( gfa00c varchar(10) not null, -- 公司编号 gfa01c varchar(20) not null, -- 产品编号 gfa02c varchar(20) null, -- 条码编号 gfa03c varchar(100) null, -- 产品名称 gfa04d varchar(8) null, -- 建档日期 gfa05c varchar(5) null, -- 产品大类 gfa06c varchar(10) null, -- 负责采购 gfa07c varchar(5) null, -- 销售单位 gfa08c varchar(5) null, -- 消耗单位 gfa09f float null, -- 数量/销售单位 gfa10c varchar(5) null, -- 是否停用 gfa11f float null, -- 标准价格/销售单位 gfa12c varchar(40) null, -- 产品规格 gfa13i tinyint null, -- 提成方式 gfa14f float null, -- 金额或比率 gfa15f float null, -- 成本金额/消耗单位 gfa16i tinyint null, -- 业绩方式 gfa17f float null, -- 金额或比率 gfa18f float null, -- 安全存量 gfa19f float null, -- 成本金额/销售单位 gfa20c varchar(5) null, -- 产品进货单位 gfa21f float null, -- 产品销售单位/进货单位 gfa22f float null, -- 产品进价 gfa23c varchar(20) null, -- 产品供应商 gfa24c varchar(10) null, -- 统计分类 gfa25i tinyint null, -- 积分方式 gfa26f float null, -- 积分或比率 gfa27i int null, -- 可用时间 gfa28i int null, -- 保质期 gfa29f float null, -- 最高存量 gfa30f float null, -- 总部销售门店价格 gfa31f float null, -- 第2零售价格 gfa32f float null, -- 第2零售消耗数量 gfa33c varchar(5) null, -- 第2零售单位 gfa34f float null, -- 第3零售价格 gfa35f float null, -- 第3零售消耗数量 gfa36c varchar(5) null, -- 第3零售单位 gfa37i int null, -- 是否允许赠送 gfa38i int null, -- 回访间隔 gfa39f float null, -- 第4零售价格 gfa40f float null, -- 第4零售消耗数量 gfa41c varchar(5) null, -- 第4零售单位 gfa42f float null, -- 第5零售价格 gfa43f float null, -- 第5零售消耗数量 gfa44c varchar(5) null, -- 第5零售单位 gfa45i int null, -- 免费服务次数 gfa46c varchar(20) null, -- 免费服务项目代码 gfa47c varchar(5) null, -- 是否停止销售 Added by maql 20100603 gfa97c varchar(20) null, -- 存放仓库 gfa98c varchar(200) null, -- 拼音检索 gfa99d datetime null, --创建时间 gfa39i int null, -- 启用第4零售价格 1为启用 0不启用 默认不启用 gfa48f float null, -- 转卡率 gfa49f float null, -- 转现金率 gfa50i int null, -- 免费服务有效期 gfa51c varchar(20) null, -- 免费服务有效期单位 gfa52c varchar(500) null, -- 备注 gfa70i int null, -- 是否允许外购 gfa71f float null -- 外购价格, 图片在产品图片中 ) declare @PK_Sql nvarchar(max) set @PK_Sql = ' alter table #gfm01_01 add constraint PK_#gfm01_01_' + convert(varchar(10), cast(floor(rand() * 1000000) as int)) + ' primary key clustered(gfa00c, gfa01c)' exec sp_executesql @PK_Sql insert #gfm01_01(gfa00c, gfa01c, gfa02c, gfa03c, gfa04d, gfa05c, gfa06c, gfa07c, gfa08c, gfa09f, gfa10c, gfa11f, gfa12c, gfa13i, gfa14f, gfa15f, gfa16i, gfa17f, gfa18f, gfa19f, gfa20c, gfa21f, gfa22f, gfa23c, gfa24c, gfa25i, gfa26f, gfa27i, gfa28i, gfa29f, gfa30f, gfa31f, gfa32f, gfa33c, gfa34f, gfa35f, gfa36c, gfa37i, gfa38i, gfa39f, gfa40f, gfa41c, gfa42f, gfa43f, gfa44c, gfa45i, gfa46c, gfa47c, gfa97c, gfa98c, gfa99d, gfa39i, gfa50i, gfa51c, gfa52c, gfa70i, gfa71f) select gfa00c, gfa01c, gfa02c, gfa03c, gfa04d, gfa05c, gfa06c, gfa07c, gfa08c, gfa09f, gfa10c, gfa11f, gfa12c, gfa13i, gfa14f, gfa15f, gfa16i, gfa17f, gfa18f, gfa19f, gfa20c, gfa21f, gfa22f, gfa23c, gfa24c, gfa25i, gfa26f, gfa27i, gfa28i, gfa29f, gfa30f, gfa31f, gfa32f, gfa33c, gfa34f, gfa35f, gfa36c, gfa37i, gfa38i, gfa39f, gfa40f, gfa41c, gfa42f, gfa43f, gfa44c, gfa45i, gfa46c, gfa47c, gfa97c, gfa98c, gfa99d, gfa39i, gfa50i, gfa51c, gfa52c, gfa70i, gfa71f from gfm01 where gfa00c = @fromcompid create table #gfm01_gfa22f -- 产品成本价格 gfa29f gfa18f gfa15f gfa19f ( gfa00c varchar(10) not null, -- 公司编号 gfa01c varchar(20) not null, -- 产品编号 gfa22f float null, -- 产品进价 gfa15f float null, -- 成本金额(消耗单位) gfa18f float null, -- 安全存量(标准单位) gfa19f float null, -- 销售成本(标准单位) gfa29f float null, -- 最高存量(标准单位) gfa72i int null ) --select * from gsm01 where gsa00c = '001' and gsa01c = 'SP207' declare @sp207_3 float select @sp207_3 = gsa03f from gsm01 where gsa01c = 'SP207' and gsa00c = @tocompid set @sp207_3 = isnull(@sp207_3, 1) if @sp207_3 <> 1 begin insert #gfm01_gfa22f(gfa00c, gfa01c, gfa22f,gfa15f,gfa18f,gfa19f,gfa29f) select gfa00c, gfa01c, gfa22f,gfa15f,gfa18f,gfa19f,gfa29f from gfm01 where gfa00c = @tocompid end else begin --判断是否勾选 gfa72i insert #gfm01_gfa22f(gfa00c, gfa01c, gfa22f,gfa15f,gfa18f,gfa19f,gfa29f,gfa72i) select gfa00c, gfa01c, gfa22f,gfa15f,gfa18f,gfa19f,gfa29f,gfa72i from gfm01 where gfa00c = @tocompid and gfa72i = 0 end if @mandatory = '1' begin delete gfm01 where gfa00c = @tocompid -- delete gfm05 -- where gfe00c = @tocompid end else begin delete a from #gfm01_01 a, gfm01 b where a.gfa00c = @fromcompid and a.gfa01c = b.gfa01c and b.gfa00c = @tocompid end declare @sp148 float -----Add by maql 20100723 select @sp148 = gsa03f from gsm01 where gsa00c = @fromcompid and gsa01c = 'SP148' if @sp148 = 1 begin insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa04d, gfa05c, gfa06c, gfa07c, gfa08c, gfa09f, gfa10c, gfa11f, gfa12c, gfa13i, gfa14f, gfa15f, gfa16i, gfa17f, gfa18f, gfa19f, gfa20c, gfa21f, gfa22f, gfa23c, gfa24c, gfa25i, gfa26f, gfa27i, gfa28i, gfa29f, gfa30f, gfa31f, gfa32f, gfa33c, gfa34f, gfa35f, gfa36c, gfa37i, gfa38i, gfa39f, gfa40f, gfa41c, gfa42f, gfa43f, gfa44c, gfa45i, gfa46c, gfa47c, gfa97c, gfa98c, gfa99d, gfa39i, gfa50i, gfa51c, gfa52c, gfa70i, gfa71f) select @tocompid, gfa01c, gfa02c, gfa03c, gfa04d, gfa05c, gfa06c, gfa07c, gfa08c, gfa09f, gfa10c, gfa11f, gfa12c, gfa13i, gfa14f, gfa15f, gfa16i, gfa17f, gfa18f, gfa19f, gfa20c, gfa21f, gfa22f, gfa23c, gfa24c, gfa25i, gfa26f, gfa27i, gfa28i, gfa29f, gfa30f, gfa31f, gfa32f, gfa33c, gfa34f, gfa35f, gfa36c, gfa37i, gfa38i, gfa39f, gfa40f, gfa41c, gfa42f, gfa43f, gfa44c, gfa45i, gfa46c, gfa47c, gfa97c, gfa98c, gfa99d, gfa39i, gfa50i, gfa51c, gfa52c, gfa70i, gfa71f from #gfm01_01 where gfa00c = @fromcompid end else ----------不拷贝最高存量和安全存量 begin insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa04d, gfa05c, gfa06c, gfa07c, gfa08c, gfa09f, gfa10c, gfa11f, gfa12c, gfa13i, gfa14f, gfa15f, gfa16i, gfa17f, gfa19f, gfa20c, gfa21f, gfa22f, gfa23c, gfa24c, gfa25i, gfa26f, gfa27i, gfa28i, gfa30f, gfa31f, gfa32f, gfa33c, gfa34f, gfa35f, gfa36c, gfa37i, gfa38i, gfa39f, gfa40f, gfa41c, gfa42f, gfa43f, gfa44c, gfa45i, gfa46c, gfa47c, gfa97c, gfa98c, gfa99d, gfa39i, gfa50i, gfa51c, gfa52c, gfa70i, gfa71f) select @tocompid, gfa01c, gfa02c, gfa03c, gfa04d, gfa05c, gfa06c, gfa07c, gfa08c, gfa09f, gfa10c, gfa11f, gfa12c, gfa13i, gfa14f, gfa15f, gfa16i, gfa17f, gfa19f, gfa20c, gfa21f, gfa22f, gfa23c, gfa24c, gfa25i, gfa26f, gfa27i, gfa28i, gfa30f, gfa31f, gfa32f, gfa33c, gfa34f, gfa35f, gfa36c, gfa37i, gfa38i, gfa39f, gfa40f, gfa41c, gfa42f, gfa43f, gfa44c, gfa45i, gfa46c, gfa47c, gfa97c, gfa98c, gfa99d, gfa39i, gfa50i, gfa51c, gfa52c, gfa70i, gfa71f from #gfm01_01 where gfa00c = @fromcompid end -- if @sp207_3 <> 1 -- begin --gfa29f gfa18f gfa15f gfa19f add lucz 2016.03.30 update a set a.gfa22f = b.gfa22f, a.gfa15f = b.gfa15f, a.gfa18f = b.gfa18f, a.gfa19f = b.gfa19f, a.gfa29f = b.gfa29f, a.gfa72i = b.gfa72i from gfm01 a, #gfm01_gfa22f b where a.gfa00c = b.gfa00c and a.gfa01c = b.gfa01c -- end -- insert gfm05(gfe00c, gfe01c, gfe02c, gfe03f, gfe04i) -- select @tocompid, gfe01c, gfe02c, gfe03f, gfe04i -- from gfm05 a, #gfm01_01 b -- where a.gfe00c = b.gfa00c -- and a.gfe02c = b.gfa01c -- and b.gfa00c = @fromcompid --写入sysoperationlog Add by maql 20100506 insert sysoperationlog(userid, program, operation, operationdate, operationtime, origatedate, compid, keyvalue1, keyvalue2, keyvalue3) values('system', 'GSB02I', 'M', @curdate, @curtime, @curdate, @fromcompid, @tocompid, @mandatory, 'upg_m_dispatch_gfm01') drop table #gfm01_01 drop table #gfm01_gfa22f end GO /****** Object: StoredProcedure [dbo].[upg_m_dispatch_gsm01] Script Date: 06/12/2017 06:20:19 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_m_dispatch_gsm01] ( @fromcompid varchar(10), -- 从哪一个公司发布 @tocompid varchar(10), -- 目的公司 @mandatory varchar(2) -- 是否强制统一 ) as begin if @fromcompid = @tocompid return if @mandatory = '1' begin delete gsm01 where gsa00c = @tocompid and gsa01c not in ('SP064', 'SP065') end else begin delete b from gsm01 a, gsm01 b where a.gsa00c = @fromcompid and a.gsa01c = b.gsa01c and b.gsa00c = @tocompid and b.gsa01c not in ('SP064', 'SP065') end insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) select @tocompid, gsa01c, gsa02c, gsa03f, gsa04c from gsm01 where gsa00c = @fromcompid and gsa01c not in ('SP064', 'SP065') if not exists(select 1 from gsm01 where gsa00c = @tocompid and gsa01c = 'SP064') begin insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) select @tocompid, gsa01c, gsa02c, gsa03f, gsa04c from gsm01 where gsa00c = @fromcompid and gsa01c = 'SP064' end else begin update b set b.gsa03f = a.gsa03f from gsm01 a, gsm01 b where a.gsa00c = @fromcompid and a.gsa01c = b.gsa01c and b.gsa00c = @tocompid and b.gsa01c = 'SP064' end if not exists(select 1 from gsm01 where gsa00c = @tocompid and gsa01c = 'SP065') begin insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) select @tocompid, gsa01c, gsa02c, gsa03f, gsa04c from gsm01 where gsa00c = @fromcompid and gsa01c = 'SP065' end else begin update b set b.gsa03f = a.gsa03f from gsm01 a, gsm01 b where a.gsa00c = @fromcompid and a.gsa01c = b.gsa01c and b.gsa00c = @tocompid and b.gsa01c = 'SP065' end delete gsm15 where gso00c = @tocompid insert gsm15(gso00c, gso01c, gso02c, gso03i, gso04f, gso05f, gso06f, gso07f, gso08f, gso09f, gso10f) select @tocompid, gso01c, gso02c, gso03i, gso04f, gso05f, gso06f, gso07f, gso08f, gso09f, gso10f from gsm15 where gso00c = @fromcompid end GO /****** Object: StoredProcedure [dbo].[upg_member_operation_details] Script Date: 06/12/2017 06:20:23 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_member_operation_details] ( @compid varchar(10), @cardno varchar(20), @fromdate varchar(8), @todate varchar(8), @orderby int -- 0 asc 1 desc ) as begin declare @incompid varchar(10) select @incompid = @compid -- 是否允许门店查看所有分店的消费历史 declare @sp023 varchar(10) select @sp023 = gsa02c from gsm01 with(nolock) where gsa00c = @compid and gsa01c = 'SP023' if @compid = '001' or @sp023 = '1' begin select @compid = '%', @incompid = '001' end if @fromdate = '' or @fromdate = '*' begin select @fromdate = '19900101' select @todate = '20901231' end create table #details ( seq int identity not null, date varchar(8) not null, action varchar(10) not null, item varchar(20) null, name varchar(100) null, memo varchar(1000) null, -- 备注 price float null, -- 标准价格 amt float null, person1 varchar(20) null, person2 varchar(20) null, person3 varchar(20) null, person4 varchar(20) null, compid varchar(40) null, billid varchar(20) null, type varchar(10) null, fillmode varchar(10) null, quan float null, ttime varchar(20) null, paycode varchar(5) null, gct21f float null, compname varchar(40) null, primary key(seq) ) insert #details(date, action, item, name, price, amt, person1, person2, person3, person4, compid, billid, type, fillmode, quan, ttime, paycode, gct21f) select gct04d, gct06c, gct07c, gct08c, gct09f, gct12f, gct15c, gct16c, gct17c, gct18c, gct00c, gct14c, gct13c, gct05c, gct11f, gct19t, gct20c, gct21f from gcm20 with (nolock), gam26 z with(nolock) where gct02c = @cardno and gct00c = z.gaz02c and z.gaz01c = @incompid and gct04d >= @fromdate and gct04d <= @todate order by gct01i update a set a.memo = d.memo from #details a, ggm02 d with(nolock) where a.action = 3 and a.type = 'gx' and a.compid = d.ggb00c and a.billid = d.ggb01c and a.item = d.ggb03c and (isnull(a.gct21f, -1) = -1 or a.gct21f = d.ggb02f) update a set a.memo = b.gna07c from #details a,gnm01 b with(nolock) where a.action = 1 and a.type = 'gn' and a.compid = b.gna00c and a.billid = b.gna01c update a set a.memo = b.gcl57c from #details a,gcm10 b with(nolock) where a.action = 5 and a.type = 'gz' and a.compid = b.gcl00c and a.billid = b.gcl01c update #details set action = '买卡' where action = '1' update #details set action = '疗程' where action = '2' update #details set action = '产品' where action = '4' update #details set action = '消费' where action = '3' update #details set action = '充卡' where action = '5' and fillmode = '0' update #details set action = '疗程' where action = '6' and fillmode = '0' update #details set action = '还款' where (action = '5' or action = '6') and fillmode = '6' update #details set action = '取款' where (action = '5' or action = '6') and (fillmode = '1' or fillmode = '5') update #details set action = '套餐' where action = '7' or action = '8' update a set a.person1 = d.haa02c from #details a, ham01 d with(nolock) where a.person1 = d.haa01c and a.compid = d.haa00c and isnull(a.person1, '') <> '' update a set a.person2 = d.haa02c from #details a, ham01 d with(nolock) where a.person2 = d.haa01c and a.compid = d.haa00c and isnull(a.person2, '') <> '' update a set a.person3 = d.haa02c from #details a, ham01 d with(nolock) where a.person3 = d.haa01c and a.compid = d.haa00c and isnull(a.person3, '') <> '' update a set a.person4 = d.haa02c from #details a, ham01 d with(nolock) where a.person4 = d.haa01c and a.compid = d.haa00c and isnull(a.person4, '') <> '' update a set a.compname = d.gae03c from #details a, gam05 d with(nolock) where compid = d.gae01c if @orderby = 0 begin select * from #details order by date asc, ttime asc, seq asc end else begin select * from #details order by date desc, ttime desc, seq desc end drop table #details end GO /****** Object: StoredProcedure [dbo].[upg_billid_search] Script Date: 06/12/2017 06:19:30 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_billid_search] ( @compid varchar(10), -- 公司别 @frombillid varchar(20), -- 单号起始 @tobillid varchar(20), @key bigint, -- 钥匙号 @remark varchar(1000) -- 备注 ) as begin declare @use_recebill float -- 是否启用了接单 select @use_recebill = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SPT04' declare @billflag int if @use_recebill = 1 begin select @billflag = 2 end else begin select @billflag = 0 end create table #tbl_master ( compid varchar(20) not null, billid varchar(20) not null, person varchar(20) null, ddate varchar(16) null, ttime varchar(12) null, memtype int null, -- 开单类型 0-会员;1-散客 memcard varchar(20) null, -- 开单类型为会员,则填会员卡号 billflag int null, -- 标志列 0-开单 1-已经结账(不可修改了) remark varchar(1000) null, -- 开单备注信息, 方便快速查询(以前用的) keyid bigint null, -- 开单钥匙牌号 gender int null, -- 性别 0:女 1:男 ggm_billid varchar(20) null, -- 收银单号 selected int null, idd int identity not null, primary key(idd) ) insert #tbl_master(compid, billid, person, ddate, ttime, memtype, memcard, billflag, keyid, selected, remark) select compid, billid, person, ddate = substring(datetime, 1, 8), ttime = substring(datetime, 9, 6), memtype, memcard, billflag, keyid, selected = 0, remark from openpolicy_master where billflag = @billflag and compid = @compid and (billid >= @frombillid and billid <= @tobillid or @frombillid = '*') and (keyid = @key or @key = 0) create table #tbl_bill_srv ( compid varchar(10) not null, billid varchar(20) not null, srvcode varchar(20) null, srvname varchar(30) null, primary key(compid, billid) ) insert #tbl_bill_srv(compid, billid, srvcode) select a.compid, a.billid, min(b.empa) from #tbl_master a, openpolicy_detail b where a.compid = b.compid and a.billid = b.billid group by a.compid, a.billid update a set a.srvname = b.haa02c from #tbl_bill_srv a, ham01 b where a.compid = b.haa00c and a.srvcode = b.haa01c -- 先显示发型师名字 update a set a.remark = b.srvname + '(' + b.srvcode + '):' + isnull(gba03c, '') + ':' + isnull(a.remark, '') from #tbl_master a, #tbl_bill_srv b, gbm01 c where a.compid = b.compid and a.billid = b.billid and a.compid = c.gba00c and c.gba23c = a.memcard and a.memtype = 0 -- 散客 update a set a.remark = b.srvname + '(' + b.srvcode + '):' + isnull(a.remark, '') from #tbl_master a, #tbl_bill_srv b where a.compid = b.compid and a.billid = b.billid and a.memtype = 1 -- 显示客户的名字 drop table #tbl_bill_srv if @remark = '' or @remark = '*' begin select compid, billid, person, ddate, ttime, memtype, memcard, billflag, keyid, selected, remark from #tbl_master end else begin select @remark = '%' + @remark + '%' select compid, billid, person, ddate, ttime, memtype, memcard, billflag, keyid, selected, remark from #tbl_master where remark like @remark end drop table #tbl_master end GO /****** Object: StoredProcedure [dbo].[upg_struct_analyse] Script Date: 06/12/2017 06:20:46 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_struct_analyse] ( @compid varchar(10), -- 会馆编号 @condition varchar(1024), -- condition @fromdate varchar(10), @todate varchar(10) ) as--WUXM, 2004/12/25 begin declare @incompid varchar(10) select @incompid = @compid create table #result_struct ( id int identity not null, class varchar(5) null, name varchar(40) null, qty float null, per float null, carduse float null, -- 项目消费销卡 act_use float null, -- 卡消费实际营业额 primary key (id), ) create nonclustered index idx_result_struct_class on #result_struct(class) declare @sql nvarchar(1024) set @sql = ' insert #result_struct(class, qty) select gca02c, count(1) from gcm01 a with (nolock), gam26 z with (nolock) where gca00c = gca13d and gca00c = z.gaz02c and z.gaz01c = ''' + @incompid + ''' and ' + @condition + 'group by gca02c ' exec sp_executesql @sql --- update the card class name update t set name = gak02c from gam10 a with (nolock), #result_struct t where gak00c = @compid and gak01c = t.class declare @total float select @total = sum(qty) from #result_struct if isnull(@total, 0) > 0 begin update #result_struct set per = (qty / @total) end declare @sp055f float select @sp055f = gsa03f from gsm01 with (nolock) where gsa00c = @compid and gsa01c = 'SP055' create table #tbl_cardtype_consume ( cardtype varchar(10) not null, carduse float null, act_use float null ) if @sp055f = 1 begin -- 按单据支付 insert #tbl_cardtype_consume(cardtype, carduse, act_use) select gca02c, sum(gsc05f), sum(gsc05f) from gsm03 a with (nolock), ggm01 b with (nolock), gcm01 c with (nolock), #result_struct d, gam26 z with (nolock), gsm14 g with (nolock) where a.gsc00c = b.gga00c and a.gsc01c = b.gga01c and a.gsc02c = 'gx' and c.gca13d = c.gca00c and b.gga09c = c.gca01c and b.gga00c = z.gaz02c and z.gaz01c = @incompid and c.gca02c = d.class and a.gsc10d >= @fromdate and a.gsc10d <= @todate and a.gsc04c = g.gsn01c and isnull(g.gsn02c, '') <> '' and isnull(g.gsn02c, '') <> '999' group by gca02c update a set a.carduse = b.carduse, a.act_use = b.act_use from #result_struct a, #tbl_cardtype_consume b where a.class = b.cardtype end else begin -- 按项目支付 -- 项目部分 insert #tbl_cardtype_consume(cardtype, carduse, act_use) select gca02c, sum(ggb11f), sum(ggb11f * isnull(ggb34f, 1)) from ggm02 a with (nolock), ggm01 b with (nolock), gcm01 c, #result_struct d, gam26 z with (nolock), gsm14 g with (nolock) where a.ggb00c = b.gga00c and a.ggb01c = b.gga01c and c.gca13d = c.gca00c and b.gga09c = c.gca01c and b.gga00c = z.gaz02c and z.gaz01c = @incompid and c.gca02c = d.class and b.gga80d >= @fromdate and b.gga80d <= @todate and a.ggb27c = g.gsn01c and isnull(g.gsn02c, '') <> '' and isnull(g.gsn02c, '') <> '999' group by gca02c update a set a.carduse = b.carduse, a.act_use = b.act_use from #result_struct a, #tbl_cardtype_consume b where a.class = b.cardtype -- 物品部分 delete #tbl_cardtype_consume insert #tbl_cardtype_consume(cardtype, carduse, act_use) select gca02c, sum(ggc11f), sum(ggc11f * isnull(ggc30f, 1)) from ggm03 a with (nolock), ggm01 b with (nolock), gcm01 c with (nolock), #result_struct d, gam26 z with (nolock), gsm14 g with (nolock) where a.ggc00c = b.gga00c and a.ggc01c = b.gga01c and c.gca13d = c.gca00c and b.gga09c = c.gca01c and b.gga00c = z.gaz02c and z.gaz01c = @incompid and c.gca02c = d.class and b.gga80d >= @fromdate and b.gga80d <= @todate and a.ggc27c = g.gsn01c and isnull(g.gsn02c, '') <> '' and isnull(g.gsn02c, '') <> '999' group by gca02c update a set a.carduse = isnull(a.carduse, 0) + isnull(b.carduse, 0), a.act_use = isnull(a.act_use, 0) + isnull(b.act_use, 0) from #result_struct a, #tbl_cardtype_consume b where a.class = b.cardtype end drop table #tbl_cardtype_consume select isnull(class,'') class, isnull(name,'') name, isnull(qty,0) qty, isnull(per,0) per, isnull(carduse,0) carduse, isnull(act_use,0) act_use from #result_struct drop table #result_struct end GO /****** Object: StoredProcedure [dbo].[upg_trans_proc_amt] Script Date: 06/12/2017 06:20:47 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_trans_proc_amt] ( @compid varchar(10), @oldacct int, -- 老疗程所在的账户,一般在储值账户中 @newacct int -- 新疗程所在的账户 ) as begin if isnull(@oldacct, -1) = -1 or isnull(@newacct, -1) = -1 or @oldacct = @newacct begin return end declare @sp057f float select @sp057f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP057' if @sp057f = 2 begin return end -- 每个卡的疗程总金额 create table #tbl_proc_amt ( cardno varchar(20) not null, seq float null, amt float null, primary key(cardno) ) -- 可用的序号 create table #tbl_gcm03_seq ( cardno varchar(20) not null, seq float null, primary key(cardno) ) -- 统计每个卡的所有疗程金额之和 insert #tbl_proc_amt(cardno, amt) select gcf01c, sum(gcf10f) from gcm06 with (nolock) where gcf00c = @compid and isnull(gcf01c, '')<>'' group by gcf01c --先把新的疗程账户中的钱转移到老账户中 update a set a.gcc06f = a.gcc06f + b.gcc06f from gcm03 a, gcm03 b where a.gcc00c = b.gcc00c and a.gcc01c = b.gcc01c and a.gcc00c = @compid and a.gcc03i = @oldacct and b.gcc03i = @newacct --先扣原来的疗程账户中的钱 update a set a.gcc06f = isnull(a.gcc06f, 0) - isnull(amt, 0) from gcm03 a, #tbl_proc_amt b where a.gcc00c = @compid and a.gcc01c = cardno and a.gcc03i = @oldacct --可能不存在这个账户 --先更新存在的账户,然后删除temp table update a set a.gcc06f = isnull(amt, 0) from gcm03 a, #tbl_proc_amt b where a.gcc00c = @compid and a.gcc01c = b.cardno and a.gcc03i = @newacct --已经存在的先删除 delete b from #tbl_proc_amt b, gcm03 a where a.gcc00c = @compid and a.gcc01c = b.cardno and a.gcc03i = @newacct --先得到每个会员卡可用的序号 insert #tbl_gcm03_seq(cardno, seq) select gcc01c, max(gcc02f) + 1 from gcm03 a with (nolock), #tbl_proc_amt b where a.gcc00c = @compid and a.gcc01c = b.cardno group by gcc00c, gcc01c update a set a.seq = b.seq from #tbl_proc_amt a, #tbl_gcm03_seq b where a.cardno = b.cardno insert gcm03(gcc00c, gcc01c, gcc02f, gcc03i, gcc04c, gcc05c, gcc06f) select @compid, cardno, seq, @newacct, '', 'RMB', amt from #tbl_proc_amt drop table #tbl_gcm03_seq drop table #tbl_proc_amt update gsm01 set gsa03f = 2 where gsa00c = @compid and gsa01c = 'SP057' update gsm02 set gsb03c = '疗程账户' where gsb00c = @compid and gsb01c = 'O' and gsb02c = convert(varchar(10), @newacct) update a set gsb03c = '疗程账户' from gsm02 a, gsm14 b where a.gsb00c = @compid and a.gsb01c = '3' and a.gsb02c = b.gsn01c and b.gsn02c = convert(varchar(10), @newacct) end GO /****** Object: StoredProcedure [dbo].[upg_out_stock_stat] Script Date: 06/12/2017 06:20:25 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_out_stock_stat] ( @compid varchar(10), -- 公司 @fromdate varchar(10), -- 日期 @todate varchar(10), @fromtime varchar(10), -- 时间 @totime varchar(10), @goodtype varchar(20), -- 产品类别 @stockid varchar(20), -- 仓库 @fromgoodid varchar(30), -- 产品代码 @togoodid varchar(30), @outkind int, -- 领取类型 @outfromcode varchar(20), -- 领取人 @outtocode varchar(20), @outtype int, -- 出库方式 @supplier varchar(20), -- 供应商那个 @includeflag int -- 0 出库 1出库 + 销售 2出库 + 销售 + 套餐 ) as begin declare @includesons float select @includesons = gsa03f from gsm01 with (nolock) where gsa00c = @compid and gsa01c = 'SP103' create table #gfm04_buf -- 库存日异动统计明细档(所有异动单据明细 update) ( gfd00c varchar(10) not null, -- 公司别 gfd01c varchar(2) not null, -- 异动别 (1-入库, 2-出库, 3-销货, 4-耗用 5-卡销售套餐 6-充值套餐) gfd02c varchar(100) not null, -- 异动单号 -- Modify 2013-08-06 gfm04表中是varchar(100) gfd04c varchar(30) not null, -- 产品代号 gfd05f float null, -- 标准单位数量 gfd06f float null, -- 标准单位单价 gfd07c varchar(8) null, -- 异动日期 gfd08c varchar(4) null, -- 仓库代号 gfd11c varchar(8) null, -- 入库或出库单位 gfd12f float null, -- 入库单位或出库单位数量 gfd13f float null, -- 金额 gfd14i int null, -- 类型 1 员工 2 门店 gfd15c varchar(20) null, -- 代码(员工代码或门店代码) gfc06i int null, --入库或出库方式 gfc08i int null, --明细类别(默认为1, 员工领取) gfc09t varchar(10) null, --时间 gfa05c varchar(5) null, -- 产品大类 gfa03c varchar(100) null, -- 产品名称 supplier varchar(20) null) --供应商 create clustered index idx_gfm04_buf_01 on #gfm04_buf(gfd07c, gfd04c) --大致的过滤 if @includesons = 1 begin insert #gfm04_buf(gfd00c, gfd01c, gfd02c, gfd04c, gfd05f, gfd06f, gfd07c, gfd08c, gfd11c, gfd12f, gfd13f, gfd14i, gfd15c, gfc06i, gfc08i, gfc09t) select gfd00c, gfd01c, gfd02c, gfd04c, gfd05f, gfd06f, gfd07c, gfd08c, gfd11c, gfd12f, gfd13f, gfd14i, gfd15c, gfc06i, gfc08i, gfc09t from gfm03 a with (nolock), gfm04 b with (nolock), gam26 z where a.gfc00c = b.gfd00c and a.gfc01c = b.gfd01c and a.gfc02c = b.gfd02c and a.gfc00c = z.gaz02c and z.gaz01c = @compid and gfc03c >= @fromdate and gfc03c <= @todate end else begin insert #gfm04_buf(gfd00c, gfd01c, gfd02c, gfd04c, gfd05f, gfd06f, gfd07c, gfd08c, gfd11c, gfd12f, gfd13f, gfd14i, gfd15c, gfc06i, gfc08i, gfc09t) select gfd00c, gfd01c, gfd02c, gfd04c, gfd05f, gfd06f, gfd07c, gfd08c, gfd11c, gfd12f, gfd13f, gfd14i, gfd15c, gfc06i, gfc08i, gfc09t from gfm03 a with (nolock), gfm04 b with (nolock) where a.gfc00c = b.gfd00c and a.gfc01c = b.gfd01c and a.gfc02c = b.gfd02c and a.gfc00c = @compid and gfc03c >= @fromdate and gfc03c <= @todate end --更新那些需要作为条件的字段 update a set a.gfa05c = b.gfa05c, a.gfa03c = b.gfa03c, a.supplier = isnull(gfa23c, '') from #gfm04_buf a, gfm01 b where a.gfd00c = b.gfa00c and a.gfd04c = b.gfa01c --更新供应商字段, 如果在出库的时候输入了,则以此为准 update a set a.supplier = b.gib12c from #gfm04_buf a, gim02 b where a.gfd00c = b.gib00c and a.gfd01c = '2' and a.gfd02c = b.gib01c and isnull(b.gib12c, '')<>'' and a.gfd04c = b.gib03c --add by hzy 2011-06-15 create table #month_cost_buf ( compid varchar(10) null, orderid varchar(20) null, -- Modify 2013-08-06 gfm04表中是varchar(100) ddate varchar(8) null, itemid varchar(30) null, -- Modify 2013-08-06 长度应和#gfm04_buf保持一致 itemname varchar(100) null, -- Modify 2013-08-06 长度应和#gfm04_buf保持一致 outtype varchar(40) null, outclass varchar(2) null, --异动别 (1-入库, 2-出库, 3-销货, 4-耗用 5-卡销售套餐 6-充值套餐) -- Modify 2013-08-06 长度应和#gfm04_buf保持一致 unit varchar(8) null, mode int null, person varchar(20) null, totalqty float null, totalamt float null, seq int, provider varchar(30) null ) create table #month_cost_result ( ddate varchar(8) null, itemid varchar(20) null, -- Modify 2013-08-06 长度应和#gfm04_buf保持一致 itemname varchar(80) null, -- Modify 2013-08-06 长度应和#gfm04_buf保持一致 outtype varchar(40) null, -- 出库方式 出库方式 + 销售 + 消耗 + 套餐 provider varchar(30) null, providername varchar(120) null, unit varchar(8) null, mode int null, person varchar(20) null, totalqty float null, totalamt float null, amt_cost float null, --成本金额 cost float null, --成本单价 seq int ) insert #month_cost_buf(compid, orderid, ddate, itemid, itemname, outtype, outclass, unit, mode, person, totalqty, totalamt, seq, provider) select compid = gfd00c, orderid = gfd02c, ddate = gfd07c, itemid = gfd04c, itemname = a.gfa03c, outtype = convert(varchar(40), gfc06i), outclass = gfd01c, -- 2013-08-06 类型不一致不可以直接赋值 unit = b.gfa07c, mode = gfc08i, person = gfd15c, sum(isnull(gfd05f, 0)) as totalqty, sum(gfd13f) as totalamt, 1, supplier from #gfm04_buf a,gfm01 b with(nolock) where a.gfd00c = b.gfa00c and a.gfd04c = b.gfa01c and (gfd01c = '2' or (gfd01c = '3' and @includeflag >= 1) or (gfd01c = '4' and @includeflag >= 1) or (gfd01c = '5' and @includeflag = 2) or (gfd01c = '6' and @includeflag = 2) or (gfd01c = '7' and @includeflag >= 1) or (gfd01c = '8' and @includeflag >= 1)) and (gfd07c + gfc09t >= @fromdate + @fromtime and gfd07c + gfc09t <= @todate + @totime) and ((@goodtype = '*') or (a.gfa05c = @goodtype)) and ((@stockid = '*') or (gfd08c = @stockid)) and ((@fromgoodid = '*') or (gfd04c >= @fromgoodid and gfd04c <= @togoodid)) and ((@outkind = 0) or (@outkind = gfd14i)) and ((@outfromcode = '*') or (gfd15c >= @outfromcode and gfd15c <= @outtocode)) and ((@outtype = 0) or (@outtype = gfc06i)) and ((@supplier = '*') or (@supplier = supplier)) group by gfd07c, gfd04c, a.gfa03c, gfa07c, gfc08i, gfd15c, gfc06i, gfd01c, gfd02c, gfd00c, supplier order by gfd07c, gfd04c --更新outtype为文字 update a set a.outtype = case when outclass = '3' then '销货' when outclass = '5' then '套餐' when outclass = '6' then '套餐' when outclass = '4' then '耗用' when outclass = '7' then '欠货领取' when outclass = '8' then '配料' end from #month_cost_buf a where a.outclass <> '2' update a set a.outtype = b.gsb03c from #month_cost_buf a, gsm02 b where a.outtype = b.gsb02c and b.gsb00c = @compid and b.gsb01c = 'C2' and a.outclass = '2' insert #month_cost_result(ddate, itemid, itemname, outtype, provider, unit, mode, person, totalqty, totalamt, seq) select ddate, itemid, itemname, outtype, provider, unit, mode, person, totalqty, totalamt, seq from #month_cost_buf order by ddate, seq update a set a.providername = gbt03c from #month_cost_result a, gbm20 b where b.gbt01c = a.provider and b.gbt00c = @compid --start增加成本单价,金额 declare @SP058f float -- 成本计算方式 select @SP058f = gsa03f from gsm01 with(nolock) where gsa00c = @compid and gsa01c = 'sp058' --需要修改成本 if @SP058f = 5 begin update a set a.amt_cost = isnull(a.totalqty, 0) * isnull(b.gfh03f, 0), a.cost = isnull(b.gfh03f, 0) from #month_cost_result a, gfm08 b where a.itemid = b.gfh01c and b.gfh00c = @compid and b.gfh02c = substring(@todate, 1, 6) end else if @SP058f = 2 begin update a set a.amt_cost = isnull(a.totalqty, 0) * isnull(b.gfa19f, 0), a.cost = isnull(b.gfa19f, 0) from #month_cost_result a, gfm01 b where a.itemid = b.gfa01c and b.gfa00c = @compid end else if @SP058f = 3 begin update a set a.amt_cost = isnull(a.totalqty, 0) * isnull(b.gfa11f, 0), a.cost = isnull(b.gfa11f, 0) from #month_cost_result a, gfm01 b where a.itemid = b.gfa01c and b.gfa00c = @compid end else begin update a set a.amt_cost = isnull(a.totalqty, 0) * isnull(b.gfa22f, 0), a.cost = isnull(b.gfa22f, 0) from #month_cost_result a, gfm01 b where a.itemid = b.gfa01c and b.gfa00c = @compid end --end select isnull(ddate,'') ddate, isnull(itemid,'') itemid, isnull(itemname,'') itemname, isnull(outtype,'') outtype, isnull(provider,'') provider, isnull(providername,'') providername, isnull(unit,'') unit, isnull(mode,'') mode, isnull(person,'') person, isnull(totalqty,'') totalqty, isnull(totalamt,'') totalamt, isnull(seq,'') seq,isnull(amt_cost ,0) amt_cost, isnull(cost, 0) cost from #month_cost_result order by itemid, seq drop table #gfm04_buf drop table #month_cost_buf drop table #month_cost_result end GO /****** Object: StoredProcedure [dbo].[upg_inoutstock_statistic_gfm08] Script Date: 06/12/2017 06:20:15 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_inoutstock_statistic_gfm08] ( @compid varchar(10), -- 公司编号 @month varchar(10) -- 结束日期 ) as begin --删除 delete gfm08 where gfh00c = @compid and gfh02c <= @month or isnull(gfh02c,'') ='' create table #gfm01 ( id int identity not null, gfa00c varchar(10) not null, -- 门店编号 gfa01c varchar(20) not null, -- 产品编号 gfa03c varchar(100) null, -- 产品名称 ) --插入该门店所有未停用的产品资料 insert #gfm01(gfa00c,gfa01c,gfa03c) select gfa00c,gfa01c,gfa03c from gfm01 where gfa10c = '0' and gfa00c = @compid --and gfa01c = 'AV107' --声明变量 declare @curdate varchar(20), @time varchar(20) declare @starmonth varchar(8)--最初入库或出库日期 declare @domonth varchar(8) declare @initial_amount float --期初量 declare @initial_amt float --期初金额 declare @next_num float --下一月入库数量 declare @next_amt float --下一月入库金额 declare @next_out_num float --下一月出库数量 declare @next_out_amt float --下一月出库金额 declare @startnum float --最初入库数量 declare @startamt float --最初入库金额 declare @outnum float --最初入库数量 declare @outamt float --最初入库金额 declare @initial_volume float --期初量 declare @gfh03f float --月加权价 declare @dogfh03f float --月加权价 declare @gfa00c varchar(10) --门店编号 declare @gfa01c varchar(20) --产品编号 declare @gfa03c varchar(100) --产品名称 declare @id int --循环变量id declare @SP103 int declare @state int --循环条件 set @state = 1 select @SP103 = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP103' exec upg_getcurrdate @curdate output, @time output --set @compid = '002' while(@state=1) begin if exists(select 1 from #gfm01) begin select top 1 @id = id,@gfa00c = gfa00c,@gfa01c = gfa01c ,@gfa03c = gfa03c from #gfm01 set @state = 1 if(@SP103 = 1) begin --起初入库或出库时间 select @starmonth = min(gfd07c) from gfm04 with (nolock), gam26 with (nolock) where gaz01c = @gfa00c and gfd00c = gaz02c and gfd04c = @gfa01c --and gfd13f > 0 and gfd05f > 0 --and gfd01c = '1' select @starmonth = substring(@starmonth,0,7) --print @starmonth --起初入库数量,金额 select @startnum = sum(gfd05f),@startamt = sum(gfd13f) from gfm04 with (nolock), gam26 with (nolock) where gaz01c = @gfa00c and gfd00c = gaz02c and gfd01c = '1' --and gfd13f > 0 and gfd05f > 0 and gfd04c = @gfa01c and substring(gfd07c,0,7) = @starmonth --起初出库数量和金额 select @outnum = sum(gfd05f),@outamt = sum(gfd13f) from gfm04 with (nolock), gam26 with (nolock) where gaz01c = @gfa00c and gfd00c = gaz02c and gfd01c <> '1' and gfd04c = @gfa01c and substring(gfd07c,0,7) = @starmonth end else begin --起初入库或出库时间 select @starmonth = min(gfd07c) from gfm04 with (nolock) where gfd00c = @gfa00c and gfd04c = @gfa01c --and gfd13f > 0 and gfd05f > 0 --and gfd01c = '1' select @starmonth = substring(@starmonth,0,7) --print @starmonth --起初入库数量,金额 select @startnum = sum(gfd05f),@startamt = sum(gfd13f) from gfm04 with (nolock) where gfd00c = @gfa00c and gfd01c = '1' --and gfd13f > 0 and gfd05f > 0 and gfd04c = @gfa01c and substring(gfd07c,0,7) = @starmonth --起初出库数量和金额 select @outnum = sum(gfd05f),@outamt = sum(gfd13f) from gfm04 with (nolock) where gfd00c = @gfa00c and gfd01c <> '1' and gfd04c = @gfa01c and substring(gfd07c,0,7) = @starmonth end -- 初月月加权平均价 set @gfh03f = dbo.fun_divide_default(@startamt,@startnum,0) if(@starmonth <= @month) begin print @starmonth insert gfm08(gfh00c,gfh01c,gfh02c,gfh03f,gfh04d,gfh05t,gfh06f,gfh07f,gfh08i) values(@gfa00c, @gfa01c, isnull(@starmonth,''), @gfh03f, @curdate, @time, isnull(@startnum,0),0,0) end --初始化变量 declare @start2 int set @start2 = 1 set @starmonth = @starmonth+'01' set @initial_volume = 0 set @initial_amount = 0 set @next_out_num = 0 set @initial_amt = 0 set @next_out_amt = 0 --期末金额 set @initial_amt = (isnull(@startnum,0) - isnull(@outnum,0))*isnull(@gfh03f,0) --期末量 set @initial_amount = isnull(@startnum,0) - isnull(@outnum,0) while(@start2 = 1) begin select @domonth = substring(convert(char(8),dateadd(month,1,@starmonth),112),0,7) if(@domonth <= @month) begin if(@SP103 = 1) begin --本月的入库数量,金额 select @next_num = sum(gfd05f),@next_amt = sum(gfd13f) from gfm04 with (nolock), gam26 with (nolock) where gaz01c = @gfa00c and gfd00c = gaz02c and gfd01c = '1' --and gfd13f > 0 and gfd05f > 0 and gfd04c = @gfa01c and substring(gfd07c,0,7) = @domonth --本月的出库数量,金额 select @next_out_num = sum(gfd05f),@next_out_amt = sum(gfd13f) from gfm04 with (nolock), gam26 with (nolock) where gaz01c = @gfa00c and gfd00c = gaz02c and gfd01c <> '1' and gfd04c = @gfa01c and substring(gfd07c,0,7) = @domonth end else begin --本月的入库数量,金额 select @next_num = sum(gfd05f),@next_amt = sum(gfd13f) from gfm04 with (nolock) where gfd00c = @gfa00c and gfd01c = '1' --and gfd13f > 0 and gfd05f > 0 and gfd04c = @gfa01c and substring(gfd07c,0,7) = @domonth --本月的出库数量,金额 select @next_out_num = sum(gfd05f),@next_out_amt = sum(gfd13f) from gfm04 with (nolock) where gfd00c = @gfa00c and gfd01c <> '1' and gfd04c = @gfa01c and substring(gfd07c,0,7) = @domonth end --本月和上一个月的入库金额,数量 set @initial_amt = isnull(@initial_amt,0)+isnull(@next_amt,0) set @initial_amount = isnull(@initial_amount,0)+isnull(@next_num,0) --本月的月加权平均价 set @dogfh03f = dbo.fun_divide_default(@initial_amt,@initial_amount,0) if(isnull(@dogfh03f,0) = 0) begin insert gfm08(gfh00c,gfh01c,gfh02c,gfh03f,gfh04d,gfh05t,gfh06f,gfh07f,gfh08i) values(@gfa00c, @gfa01c, isnull(@domonth,''), @gfh03f, @curdate, @time, isnull(@next_num,0),isnull(@initial_volume,0),0) --期初量计算 set @initial_volume = isnull(@initial_amount,0) - isnull(@next_out_num,0) set @initial_amount = @initial_volume end else begin insert gfm08(gfh00c,gfh01c,gfh02c,gfh03f,gfh04d,gfh05t,gfh06f,gfh07f,gfh08i) values(@gfa00c, @gfa01c, isnull(@domonth,''), @dogfh03f, @curdate, @time, isnull(@next_num,0),isnull(@initial_volume,0),0) -- 这个月的期末量,金额,最为下一个月的期初量,金额 set @initial_volume = isnull(@initial_amount,0) - isnull(@next_out_num,0) set @initial_amt = isnull(@initial_volume,0)*@dogfh03f set @initial_amount = @initial_volume set @gfh03f = @dogfh03f set @next_out_num = 0 end set @starmonth = @domonth+'01' --初始化变量 set @next_amt = 0 set @next_num = 0 set @outnum = 0 set @next_amt = 0 set @outamt = 0 end else begin set @start2 = 0 end end set @gfh03f = 0 delete #gfm01 where id = @id end else begin set @state = 0 end end drop table #gfm01 end GO /****** Object: StoredProcedure [dbo].[upg_updateglm01_accountdate] Script Date: 06/12/2017 06:20:51 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_updateglm01_accountdate] ( @compid varchar(10), -- 公司编号 @consumeid varchar(20) -- 消费单号 ) as --WUXM 2005/08/16 begin declare @begtime varchar(20) --工作日开始时间 declare @endtime varchar(20) --工作日结束时间,表示凌晨时间,如果为000000,则表示帐务日期和实际日期一致 declare @time varchar(6) --实际结帐时间 declare @date varchar(8) --实际日期 declare @staff varchar(20) --登陆人 declare @acctdate varchar(20) -- 账户日期 select @begtime = gsa02c from gsm01 where gsa00c = @compid and gsa01c = 'SP050' select @endtime = gsa02c from gsm01 where gsa00c = @compid and gsa01c = 'SP051' declare @curtime varchar(8) declare @curdate varchar(10) select @curtime = gla04t, @curdate = gla96d, @staff = gla95c from glm01 with (nolock) where gla00c = @compid and gla01c = @consumeid select @time = @curtime select @date = @curdate declare @newdate varchar(8) declare @strdate varchar(10) --帐务日期比正常日期短 if (@endtime > @begtime and @time > @endtime) begin select @strdate = convert(varchar(10), dateadd(day, 1, cast(@date as datetime)), 120) select @newdate = substring(@strdate, 1, 4) + substring(@strdate, 6, 2) + substring(@strdate, 9, 2) update a set a.gla80d = @newdate from glm01 a where gla00c = @compid and gla01c = @consumeid select @acctdate = @newdate end --帐务日期比正常日期长 else if (@endtime <= @begtime and @time <= @endtime) begin select @strdate = convert(varchar(10), dateadd(day, -1, cast(@date as datetime)), 120) select @newdate = substring(@strdate, 1, 4) + substring(@strdate, 6, 2) + substring(@strdate, 9, 2) update a set a.gla80d = @newdate from glm01 a where gla00c = @compid and gla01c = @consumeid select @acctdate = @newdate end --帐务日期和正常日期一致 else begin update a set a.gla80d = @date from glm01 a where gla00c = @compid and gla01c = @consumeid select @acctdate = @date end --更新gsm03新增加的字段 update a set a.gsc10d = @acctdate, a.gsc11c = @staff from gsm03 a where gsc00c = @compid and gsc01c = @consumeid and gsc02c = 'gl' end GO /****** Object: StoredProcedure [dbo].[upg_personal_statistics_ex] Script Date: 06/12/2017 06:20:26 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_personal_statistics_ex] ( @compid varchar(10), -- 公司别 @fromdate varchar(10), -- 开始日期 @todate varchar(10), -- 截至日期 @fromempl varchar(20), -- 查询开始人员 @toempl varchar(20), -- 查询截至人员 @inc_card int, -- 卡销售 @inc_service int, -- 服务 @inc_goods int, -- 是否包括产品销售 @return_type int, @nsortby int ) as begin declare @spid int select @spid = @@spid create table #empl_list_2 ( id int not null, -- 流水好 person_id varchar(20) not null, -- 员工代码 name varchar(40) null, -- 名称 gsb03c varchar(40) null, -- 员工岗位 amt_c float null, -- 卡销售 amt_1 float null, -- 指定大工 amt_12 float null, -- 轮班大工 amt_13 float null, -- 补工大工 amt_2 float null, -- 指定中工 amt_21 float null, -- 轮班中工 amt_3 float null, -- 指定小工 amt_31 float null, -- 轮班小工 amt_4 float null, -- 指定四工 amt_41 float null, -- 轮班四工 amt_1_14 float null, -- 内创大工 amt_1_15 float null, -- 外创大工 amt_2_14 float null, -- 内创中工 amt_2_15 float null, -- 外创中工 amt_3_14 float null, -- 内创小工 amt_3_15 float null, -- 外创小工 amt_4_14 float null, -- 内创四工 amt_4_15 float null, -- 外创四工 amt_g float null, -- 产品销售金额 amtrate_1 float null, -- 一个员工指定服务的金额占总的指定金额的比率 comm float null, -- 提成 total_srv float null, -- 服务项目总业绩 total_perf float null, -- 员工总业绩(服务项目+产品销售) total_cash float null, -- 员工总现金(现金产品+现金服务), 包括银行卡 total_cash_c float null, -- 员工总现金(现金产品+现金服务), 包括银行卡, 包括卡 bonus float null, -- 奖罚 total_srv_cash float null, -- 项目现金实做 total_srv_card float null, -- 项目卡实做 num_fix float null, -- 指定项目数量 num_nofix float null, -- 轮班项目数量 num_in float null, -- 内创项目数量 num_outer float null, -- 外创项目数量 consume_cost float null, -- 员工领用的消耗产品的成本 compid varchar(10) null, primary key(id) ) insert #empl_list_2(id, person_id, name, amt_c, amt_1, amt_12, amt_13, amt_2, amt_21, amt_3, amt_31, amt_4, amt_41,amt_1_14,amt_1_15,amt_2_14,amt_2_15,amt_3_14,amt_3_15,amt_4_14,amt_4_15, amt_g, amtrate_1, comm, total_srv, total_perf, total_cash, total_cash_c, bonus, total_srv_cash, total_srv_card, num_fix, num_nofix,num_in,num_outer, consume_cost, compid) exec upg_personal_statistics @compid, @fromdate, @todate, @fromempl, @toempl, @inc_card, @inc_service, @inc_goods, @return_type, @spid declare @sp143_3 float select @sp143_3 = gsa03f from gsm01 with(nolock) where gsa01c = 'SP143' and gsa00c = @compid update a set a.name = b.haa02c, a.gsb03c = b.haa25c from #empl_list_2 a, ham01 b with(nolock) where haa01c = person_id and ( @sp143_3 = 0 or haa00c = compid ) update a set a.gsb03c = b.gsb03c from #empl_list_2 a, gsm02 b with(nolock) where b.gsb01c = 'R' and a.gsb03c = b.gsb02c and gsb00c = @compid declare @pay varchar(8) select @pay = isnull(gsa02c,'') from gsm01 with(nolock) where gsa01c = 'SP058' and gsa00c = @compid set @pay = isnull(@pay, '') create table #worl_emp_money ( person_id varchar(20) not null, paycode varchar(20) not null, amt float null, amt2 float null, amt3 float null, primary key (person_id,paycode) ) -- 增加抵用卷,赠送,内调支付方式金额 modify by limin 2014-11-25 create table #worl_emp_money_pay ( person_id varchar(20) not null, pay_0 float null, pay_3 float null, pay_$ float null, pay_gave float null, amt_sell float null, -- add by lm 2015.10.29 amt_cons float null -- primary key(person_id) ) insert #worl_emp_money_pay(person_id) select person_id from #empl_list_2 -- 添加员工 insert into #worl_emp_money(person_id,paycode,amt,amt2,amt3) select person_id,isnull(paycode,''),sum(amt),sum(amt2),sum(amt3) from work_detail with (nolock) where (paycode = '0' or paycode = '$' or paycode = '3' or (paycode = @pay and @pay <> '')) and spid = @spid and action_id <> 0 and action_id <> 1 group by person_id,paycode --Start create table #bill ( compid varchar(20) not null, billtype varchar(20) not null, billid varchar(20) not null-- 单号 ) --start日期筛选单号 insert #bill(compid, billtype, billid) select distinct gcl00c, gsc02c, gcl01c from gcm10 a with (nolock), gam26 z with (nolock), gsm03 b with (nolock) where a.gcl00c = z.gaz02c and a.gcl00c = b.gsc00c and a.gcl01c = b.gsc01c and b.gsc02c = 'gz' and z.gaz01c = @compid and ((a.gcl02d >= @fromdate and a.gcl02d <= @todate) or @fromdate = '*') insert #bill(compid, billtype, billid) select distinct gna00c, gsc02c, gna01c from gnm01 a with (nolock), gam26 z with (nolock), gsm03 b with (nolock) where a.gna00c = z.gaz02c and a.gna00c = b.gsc00c and a.gna01c = b.gsc01c and b.gsc02c = 'gn' and z.gaz01c = @compid and ((a.gna02d >= @fromdate and a.gna02d <= @todate) or @fromdate = '*') --end --疗程销售业绩 create table #card_fill_detail_by_payment ( compid varchar(10) not null, -- 公司别 billtype varchar(10) not null, -- 单别 billid varchar(20) not null, -- 单号 personid varchar(20) null, -- 员工号 share_rate float null, -- 分享比率 paycode varchar(20) null, -- 支付代码 payamt float null, -- 支付金额 filltype int null, -- 1 充值 2 疗程 3 套餐 cardid varchar(20) null, -- 会员卡号 cardtype varchar(20) null, -- 会员卡类别 code varchar(20) null, -- 代码 会员卡代码 ,疗程代码,套餐代码 times float null, -- 疗程次数或套餐的次数,卡销售=1 procid float null, -- 疗程的价格序号 perf float null, -- 业绩 person_pos int null, -- 第几销售 1,2,3,4 comm float null, -- 提成 pctimes float null, -- 疗程或套餐的实际次数(支付方式拆分) ) create clustered index idx_card_fill_detail_by_payment on #card_fill_detail_by_payment(compid,billtype,billid) create table #treatment_sell ( person_id varchar(20) null, -- 员工号 amt_sell float null, -- 金额 ) create clustered index idx_treatment_sell on #treatment_sell(person_id) create table #treatment_consumption ( person_id varchar(20) null, -- 员工号 amt_cons float null, -- 金额 ) create clustered index idx_treatment_consumption on #treatment_consumption(person_id) --新增数据 insert #card_fill_detail_by_payment (compid, billtype, billid, personid, share_rate, paycode, payamt, filltype, cardid, cardtype, code, times, procid, perf, person_pos, comm, pctimes ) select a.compid, a.billtype, a.billid, personid, share_rate, paycode, payamt, filltype, cardid, cardtype, code, times, procid, perf, person_pos, comm, pctimes from #bill a, card_fill_detail_by_payment b with(nolock) where a.billid = b.billid and a.compid = b.compid and a.billtype = b.billtype and b.filltype = 2 --金额 if(@return_type = 4) begin --疗程销售合计 insert #treatment_sell (person_id, amt_sell) select personid, isnull(sum(payamt),0) from #card_fill_detail_by_payment group by compid, personid --疗程消耗合计 insert #treatment_consumption (person_id, amt_cons) select person_id, isnull(sum(amt),0) from work_detail with (nolock) where compid = @compid and spid = @spid and paycode = '9' group by compid, person_id --内调金额赋值 update a set a.pay_0 = b.amt from #worl_emp_money_pay a , #worl_emp_money b where a.person_id = b.person_id and b.paycode = '0' -- 抵用卷A金额赋值 update a set a.pay_3 = b.amt from #worl_emp_money_pay a, #worl_emp_money b where a.person_id = b.person_id and b.paycode = '3' -- 抵用卷B金额赋值 update a set a.pay_$ = b.amt from #worl_emp_money_pay a, #worl_emp_money b where a.person_id = b.person_id and b.paycode = '$' -- 赠送金额赋值 update a set a.pay_gave = b.amt from #worl_emp_money_pay a, #worl_emp_money b where a.person_id = b.person_id and b.paycode = @pay end else if (@return_type = 3) --虚业绩 begin --疗程销售合计 insert #treatment_sell (person_id, amt_sell) select personid, isnull(sum(perf),0) from #card_fill_detail_by_payment group by compid, personid --疗程消耗合计 insert #treatment_consumption (person_id, amt_cons) select person_id, isnull(sum(amt2),0) from work_detail with (nolock) where compid = @compid and spid = @spid and paycode = '9' group by compid, person_id --内调金额赋值 update a set a.pay_0 = b.amt2 from #worl_emp_money_pay a , #worl_emp_money b where a.person_id = b.person_id and b.paycode = '0' -- 抵用卷A金额赋值 update a set a.pay_3 = b.amt2 from #worl_emp_money_pay a, #worl_emp_money b where a.person_id = b.person_id and b.paycode = '3' -- 抵用卷B金额赋值 update a set a.pay_$ = b.amt2 from #worl_emp_money_pay a, #worl_emp_money b where a.person_id = b.person_id and b.paycode = '$' -- 赠送金额赋值 update a set a.pay_gave = b.amt2 from #worl_emp_money_pay a, #worl_emp_money b where a.person_id = b.person_id and b.paycode = @pay end else if (@return_type = 5)--真实业绩 begin --疗程销售合计 insert #treatment_sell (person_id, amt_sell) select personid, isnull(sum(perf),0) from #card_fill_detail_by_payment group by compid, personid --疗程消耗合计 insert #treatment_consumption (person_id, amt_cons) select person_id, isnull(sum(amt3),0) from work_detail with (nolock) where compid = @compid and spid = @spid and paycode = '9' group by compid, person_id --内调金额赋值 update a set a.pay_0 = b.amt3 from #worl_emp_money_pay a , #worl_emp_money b where a.person_id = b.person_id and b.paycode = '0' -- 抵用卷A金额赋值 update a set a.pay_3 = b.amt3 from #worl_emp_money_pay a, #worl_emp_money b where a.person_id = b.person_id and b.paycode = '3' -- 抵用卷B金额赋值 update a set a.pay_$ = b.amt3 from #worl_emp_money_pay a, #worl_emp_money b where a.person_id = b.person_id and b.paycode = '$' -- 赠送金额赋值 update a set a.pay_gave = b.amt3 from #worl_emp_money_pay a, #worl_emp_money b where a.person_id = b.person_id and b.paycode = @pay end --End update a set a.amt_sell = b.amt_sell from #worl_emp_money_pay a, #treatment_sell b where a.person_id = b.person_id update a set a.amt_cons = b.amt_cons from #worl_emp_money_pay a, #treatment_consumption b where a.person_id = b.person_id declare @custverflag varchar(20) if (@nsortby = 0) select isnull(id,'') id, isnull(#empl_list_2.person_id,'') person_id, isnull(name,'') name, isnull(gsb03c, 0) gsb03c,isnull(amt_c, 0) amt_c, isnull(amt_1, 0) amt_1, isnull(amt_12, 0) amt_12, isnull(amt_13, 0) amt_13, isnull(amt_2, 0) amt_2, isnull(amt_21, 0) amt_21, isnull(amt_3, 0) amt_3, isnull(amt_31, 0) amt_31, isnull(amt_4, 0) amt_4, isnull(amt_41, 0) amt_41, isnull(amt_1_14,0) amt_1_14, isnull(amt_1_15,0) amt_1_15, isnull(amt_2_14,0) amt_2_14, isnull(amt_2_15,0) amt_2_15, isnull(amt_3_14,0) amt_3_14, isnull(amt_3_15,0) amt_3_15, isnull(amt_4_14,0) amt_4_14, isnull(amt_4_15,0) amt_4_15, isnull(amt_g, 0) amt_g, isnull(amtrate_1, 0) amtrate_1, isnull(comm, 0) comm, isnull(total_srv, 0) total_srv, isnull(total_perf, 0) total_perf, isnull(total_cash, 0) total_cash, isnull(total_cash_c, 0) total_cash_c, isnull(bonus, 0) bonus, isnull(total_srv_cash, 0) total_srv_cash, isnull(total_srv_card, 0) total_srv_card, isnull(num_fix, 0) num_fix, isnull(num_nofix, 0) num_nofix, isnull(num_in, 0) num_in, isnull(num_outer, 0) num_outer, isnull(consume_cost, 0) consume_cost, @compid compid,isnull(pay_0, 0) pay_0,isnull(pay_3, 0) pay_3,isnull(pay_$, 0) pay_$, isnull(pay_gave, 0) pay_gave,isnull(amt_sell, 0) amt_sell,isnull(amt_cons, 0) amt_cons from #empl_list_2, #worl_emp_money_pay where #empl_list_2.person_id = #worl_emp_money_pay.person_id order by isnull(amt_c, 0) desc else if (@nsortby = 1) begin select isnull(id,'') id, isnull(#empl_list_2.person_id,'') person_id, isnull(name,'') name, isnull(gsb03c, 0) gsb03c,isnull(amt_c, 0) amt_c, isnull(amt_1, 0) amt_1, isnull(amt_12, 0) amt_12, isnull(amt_13, 0) amt_13, isnull(amt_2, 0) amt_2, isnull(amt_21, 0) amt_21, isnull(amt_3, 0) amt_3, isnull(amt_31, 0) amt_31, isnull(amt_4, 0) amt_4, isnull(amt_41, 0) amt_41, isnull(amt_1_14,0) amt_1_14, isnull(amt_1_15,0) amt_1_15, isnull(amt_2_14,0) amt_2_14, isnull(amt_2_15,0) amt_2_15, isnull(amt_3_14,0) amt_3_14, isnull(amt_3_15,0) amt_3_15, isnull(amt_4_14,0) amt_4_14, isnull(amt_4_15,0) amt_4_15, isnull(amt_g, 0) amt_g, isnull(amtrate_1, 0) amtrate_1, isnull(comm, 0) comm, isnull(total_srv, 0) total_srv, isnull(total_perf, 0) total_perf, isnull(total_cash, 0) total_cash, isnull(total_cash_c, 0) total_cash_c, isnull(bonus, 0) bonus, isnull(total_srv_cash, 0) total_srv_cash, isnull(total_srv_card, 0) total_srv_card, isnull(num_fix, 0) num_fix, isnull(num_nofix, 0) num_nofix, isnull(num_in, 0) num_in, isnull(num_outer, 0) num_outer, isnull(consume_cost, 0) consume_cost, @compid compid,isnull(pay_0, 0) pay_0,isnull(pay_3, 0) pay_3,isnull(pay_$, 0) pay_$, isnull(pay_gave, 0) pay_gave,isnull(amt_sell, 0) amt_sell,isnull(amt_cons, 0) amt_cons from #empl_list_2, #worl_emp_money_pay where #empl_list_2.person_id = #worl_emp_money_pay.person_id order by isnull(amt_1, 0) + isnull(amt_12, 0) + isnull(amt_13, 0) + isnull(amt_2, 0) + isnull(amt_21, 0) + isnull(amt_3, 0) + isnull(amt_31, 0) + isnull(amt_4, 0) + isnull(amt_41, 0) desc end else if (@nsortby = 2) select isnull(id,'') id, isnull(#empl_list_2.person_id,'') person_id, isnull(name,'') name, isnull(gsb03c, 0) gsb03c,isnull(amt_c, 0) amt_c, isnull(amt_1, 0) amt_1, isnull(amt_12, 0) amt_12, isnull(amt_13, 0) amt_13, isnull(amt_2, 0) amt_2, isnull(amt_21, 0) amt_21, isnull(amt_3, 0) amt_3, isnull(amt_31, 0) amt_31, isnull(amt_4, 0) amt_4, isnull(amt_41, 0) amt_41, isnull(amt_1_14,0) amt_1_14, isnull(amt_1_15,0) amt_1_15, isnull(amt_2_14,0) amt_2_14, isnull(amt_2_15,0) amt_2_15, isnull(amt_3_14,0) amt_3_14, isnull(amt_3_15,0) amt_3_15, isnull(amt_4_14,0) amt_4_14, isnull(amt_4_15,0) amt_4_15, isnull(amt_g, 0) amt_g, isnull(amtrate_1, 0) amtrate_1, isnull(comm, 0) comm, isnull(total_srv, 0) total_srv, isnull(total_perf, 0) total_perf, isnull(total_cash, 0) total_cash, isnull(total_cash_c, 0) total_cash_c, isnull(bonus, 0) bonus, isnull(total_srv_cash, 0) total_srv_cash, isnull(total_srv_card, 0) total_srv_card, isnull(num_fix, 0) num_fix, isnull(num_nofix, 0) num_nofix, isnull(num_in, 0) num_in, isnull(num_outer, 0) num_outer, isnull(consume_cost, 0) consume_cost, @compid compid,isnull(pay_0, 0) pay_0,isnull(pay_3, 0) pay_3,isnull(pay_$, 0) pay_$, isnull(pay_gave, 0) pay_gave,isnull(amt_sell, 0) amt_sell,isnull(amt_cons, 0) amt_cons from #empl_list_2, #worl_emp_money_pay where #empl_list_2.person_id = #worl_emp_money_pay.person_id order by isnull(amt_g, 0) desc else if (@nsortby = 3) begin select isnull(id,'') id, isnull(#empl_list_2.person_id,'') person_id, isnull(name,'') name, isnull(gsb03c, 0) gsb03c,isnull(amt_c, 0) amt_c, isnull(amt_1, 0) amt_1, isnull(amt_12, 0) amt_12, isnull(amt_13, 0) amt_13, isnull(amt_2, 0) amt_2, isnull(amt_21, 0) amt_21, isnull(amt_3, 0) amt_3, isnull(amt_31, 0) amt_31, isnull(amt_4, 0) amt_4, isnull(amt_41, 0) amt_41, isnull(amt_1_14,0) amt_1_14, isnull(amt_1_15,0) amt_1_15, isnull(amt_2_14,0) amt_2_14, isnull(amt_2_15,0) amt_2_15, isnull(amt_3_14,0) amt_3_14, isnull(amt_3_15,0) amt_3_15, isnull(amt_4_14,0) amt_4_14, isnull(amt_4_15,0) amt_4_15, isnull(amt_g, 0) amt_g, isnull(amtrate_1, 0) amtrate_1, isnull(comm, 0) comm, isnull(total_srv, 0) total_srv, isnull(total_perf, 0) total_perf, isnull(total_cash, 0) total_cash, isnull(total_cash_c, 0) total_cash_c, isnull(bonus, 0) bonus, isnull(total_srv_cash, 0) total_srv_cash, isnull(total_srv_card, 0) total_srv_card, isnull(num_fix, 0) num_fix, isnull(num_nofix, 0) num_nofix, isnull(num_in, 0) num_in, isnull(num_outer, 0) num_outer, isnull(consume_cost, 0) consume_cost, @compid compid,isnull(pay_0, 0) pay_0,isnull(pay_3, 0) pay_3,isnull(pay_$, 0) pay_$, isnull(pay_gave, 0) pay_gave,isnull(amt_sell, 0) amt_sell,isnull(amt_cons, 0) amt_cons from #empl_list_2, #worl_emp_money_pay where #empl_list_2.person_id = #worl_emp_money_pay.person_id order by isnull(amt_c, 0) + isnull(amt_1, 0) + isnull(amt_12, 0) + isnull(amt_13, 0) + isnull(amt_2, 0) + isnull(amt_21, 0) + isnull(amt_3, 0) + isnull(amt_31, 0) + isnull(amt_4, 0) + isnull(amt_41, 0) + isnull(amt_g, 0) desc end else if (@nsortby = 4) begin select isnull(id,'') id, isnull(#empl_list_2.person_id,'') person_id, isnull(name,'') name, isnull(gsb03c, 0) gsb03c,isnull(amt_c, 0) amt_c, isnull(amt_1, 0) amt_1, isnull(amt_12, 0) amt_12, isnull(amt_13, 0) amt_13, isnull(amt_2, 0) amt_2, isnull(amt_21, 0) amt_21, isnull(amt_3, 0) amt_3, isnull(amt_31, 0) amt_31, isnull(amt_4, 0) amt_4, isnull(amt_41, 0) amt_41, isnull(amt_1_14,0) amt_1_14, isnull(amt_1_15,0) amt_1_15, isnull(amt_2_14,0) amt_2_14, isnull(amt_2_15,0) amt_2_15, isnull(amt_3_14,0) amt_3_14, isnull(amt_3_15,0) amt_3_15, isnull(amt_4_14,0) amt_4_14, isnull(amt_4_15,0) amt_4_15, isnull(amt_g, 0) amt_g, isnull(amtrate_1, 0) amtrate_1, isnull(comm, 0) comm, isnull(total_srv, 0) total_srv, isnull(total_perf, 0) total_perf, isnull(total_cash, 0) total_cash, isnull(total_cash_c, 0) total_cash_c, isnull(bonus, 0) bonus, isnull(total_srv_cash, 0) total_srv_cash, isnull(total_srv_card, 0) total_srv_card, isnull(num_fix, 0) num_fix, isnull(num_nofix, 0) num_nofix, isnull(num_in, 0) num_in, isnull(num_outer, 0) num_outer, isnull(consume_cost, 0) consume_cost, @compid compid,isnull(pay_0, 0) pay_0,isnull(pay_3, 0) pay_3,isnull(pay_$, 0) pay_$, isnull(pay_gave, 0) pay_gave,isnull(amt_sell, 0) amt_sell,isnull(amt_cons, 0) amt_cons from #empl_list_2, #worl_emp_money_pay where #empl_list_2.person_id = #worl_emp_money_pay.person_id order by total_perf desc end else if (@nsortby = 5) begin select isnull(id,'') id, isnull(#empl_list_2.person_id,'') person_id, isnull(name,'') name, isnull(gsb03c, 0) gsb03c,isnull(amt_c, 0) amt_c, isnull(amt_1, 0) amt_1, isnull(amt_12, 0) amt_12, isnull(amt_13, 0) amt_13, isnull(amt_2, 0) amt_2, isnull(amt_21, 0) amt_21, isnull(amt_3, 0) amt_3, isnull(amt_31, 0) amt_31, isnull(amt_4, 0) amt_4, isnull(amt_41, 0) amt_41, isnull(amt_1_14,0) amt_1_14, isnull(amt_1_15,0) amt_1_15, isnull(amt_2_14,0) amt_2_14, isnull(amt_2_15,0) amt_2_15, isnull(amt_3_14,0) amt_3_14, isnull(amt_3_15,0) amt_3_15, isnull(amt_4_14,0) amt_4_14, isnull(amt_4_15,0) amt_4_15, isnull(amt_g, 0) amt_g, isnull(amtrate_1, 0) amtrate_1, isnull(comm, 0) comm, isnull(total_srv, 0) total_srv, isnull(total_perf, 0) total_perf, isnull(total_cash, 0) total_cash, isnull(total_cash_c, 0) total_cash_c, isnull(bonus, 0) bonus, isnull(total_srv_cash, 0) total_srv_cash, isnull(total_srv_card, 0) total_srv_card, isnull(num_fix, 0) num_fix, isnull(num_nofix, 0) num_nofix, isnull(num_in, 0) num_in, isnull(num_outer, 0) num_outer, isnull(consume_cost, 0) consume_cost, @compid compid,isnull(pay_0, 0) pay_0,isnull(pay_3, 0) pay_3,isnull(pay_$, 0) pay_$, isnull(pay_gave, 0) pay_gave,isnull(amt_sell, 0) amt_sell,isnull(amt_cons, 0) amt_cons from #empl_list_2, #worl_emp_money_pay where #empl_list_2.person_id = #worl_emp_money_pay.person_id order by total_cash_c desc end else if (@nsortby = 8) begin select isnull(id,'') id, isnull(#empl_list_2.person_id,'') person_id, isnull(name,'') name, isnull(gsb03c, 0) gsb03c,isnull(amt_c, 0) amt_c, isnull(amt_1, 0) amt_1, isnull(amt_12, 0) amt_12, isnull(amt_13, 0) amt_13, isnull(amt_2, 0) amt_2, isnull(amt_21, 0) amt_21, isnull(amt_3, 0) amt_3, isnull(amt_31, 0) amt_31, isnull(amt_4, 0) amt_4, isnull(amt_41, 0) amt_41, isnull(amt_1_14,0) amt_1_14, isnull(amt_1_15,0) amt_1_15, isnull(amt_2_14,0) amt_2_14, isnull(amt_2_15,0) amt_2_15, isnull(amt_3_14,0) amt_3_14, isnull(amt_3_15,0) amt_3_15, isnull(amt_4_14,0) amt_4_14, isnull(amt_4_15,0) amt_4_15, isnull(amt_g, 0) amt_g, isnull(amtrate_1, 0) amtrate_1, isnull(comm, 0) comm, isnull(total_srv, 0) total_srv, isnull(total_perf, 0) total_perf, isnull(total_cash, 0) total_cash, isnull(total_cash_c, 0) total_cash_c, isnull(bonus, 0) bonus, isnull(total_srv_cash, 0) total_srv_cash, isnull(total_srv_card, 0) total_srv_card, isnull(num_fix, 0) num_fix, isnull(num_nofix, 0) num_nofix, isnull(num_in, 0) num_in, isnull(num_outer, 0) num_outer, isnull(consume_cost, 0) consume_cost, @compid compid,isnull(pay_0, 0) pay_0,isnull(pay_3, 0) pay_3,isnull(pay_$, 0) pay_$, isnull(pay_gave, 0) pay_gave,isnull(amt_sell, 0) amt_sell,isnull(amt_cons, 0) amt_cons from #empl_list_2, #worl_emp_money_pay where #empl_list_2.person_id = #worl_emp_money_pay.person_id order by #empl_list_2.person_id asc end else if (@nsortby = 6) begin --芳子版本需要修正 --如果选择 主人员服务项目业绩,那么报表中的 项目业绩;也应该不包括这个主人员,作为 中工,小工,四工的服务项目业绩 select @custverflag = gsa02c from gsm01 with(nolock) where gsa00c = @compid and gsa01c = 'SP109' if @custverflag = 'FANGZI' begin update a set a.total_srv = isnull(amt_1, 0) + isnull(amt_12, 0) + isnull(amt_13, 0) from #empl_list_2 a end select isnull(id,'') id, isnull(#empl_list_2.person_id,'') person_id, isnull(name,'') name, isnull(gsb03c, 0) gsb03c,isnull(amt_c, 0) amt_c, isnull(amt_1, 0) amt_1, isnull(amt_12, 0) amt_12, isnull(amt_13, 0) amt_13, isnull(amt_2, 0) amt_2, isnull(amt_21, 0) amt_21, isnull(amt_3, 0) amt_3, isnull(amt_31, 0) amt_31, isnull(amt_4, 0) amt_4, isnull(amt_41, 0) amt_41, isnull(amt_1_14,0) amt_1_14, isnull(amt_1_15,0) amt_1_15, isnull(amt_2_14,0) amt_2_14, isnull(amt_2_15,0) amt_2_15, isnull(amt_3_14,0) amt_3_14, isnull(amt_3_15,0) amt_3_15, isnull(amt_4_14,0) amt_4_14, isnull(amt_4_15,0) amt_4_15, isnull(amt_g, 0) amt_g, isnull(amtrate_1, 0) amtrate_1, isnull(comm, 0) comm, isnull(total_srv, 0) total_srv, isnull(total_perf, 0) total_perf, isnull(total_cash, 0) total_cash, isnull(total_cash_c, 0) total_cash_c, isnull(bonus, 0) bonus, isnull(total_srv_cash, 0) total_srv_cash, isnull(total_srv_card, 0) total_srv_card, isnull(num_fix, 0) num_fix, isnull(num_nofix, 0) num_nofix, isnull(num_in, 0) num_in, isnull(num_outer, 0) num_outer, isnull(consume_cost, 0) consume_cost, @compid compid,isnull(pay_0, 0) pay_0,isnull(pay_3, 0) pay_3,isnull(pay_$, 0) pay_$, isnull(pay_gave, 0) pay_gave,isnull(amt_sell, 0) amt_sell,isnull(amt_cons, 0) amt_cons from #empl_list_2, #worl_emp_money_pay where #empl_list_2.person_id = #worl_emp_money_pay.person_id order by total_srv desc end else if (@nsortby = 7) begin --芳子版本需要修正 --排序标准是其他服务人员总业绩,即 中工,小工,四工服务业绩 + 产品 + 卡销售,那么,总业绩(卡)中,就不要包括大工的 项目服务的业绩了 select @custverflag = gsa02c from gsm01 with(nolock) where gsa00c = @compid and gsa01c = 'SP109' if @custverflag = 'FANGZI' begin update a set a.total_srv = isnull(amt_2, 0) + isnull(amt_21, 0) + isnull(amt_3, 0) + isnull(amt_31, 0) + isnull(amt_4, 0) + isnull(amt_41, 0) from #empl_list_2 a end select isnull(id,'') id, isnull(#empl_list_2.person_id,'') person_id, isnull(name,'') name, isnull(gsb03c, 0) gsb03c,isnull(amt_c, 0) amt_c, isnull(amt_1, 0) amt_1, isnull(amt_12, 0) amt_12, isnull(amt_13, 0) amt_13, isnull(amt_2, 0) amt_2, isnull(amt_21, 0) amt_21, isnull(amt_3, 0) amt_3, isnull(amt_31, 0) amt_31, isnull(amt_4, 0) amt_4, isnull(amt_41, 0) amt_41, isnull(amt_1_14,0) amt_1_14, isnull(amt_1_15,0) amt_1_15, isnull(amt_2_14,0) amt_2_14, isnull(amt_2_15,0) amt_2_15, isnull(amt_3_14,0) amt_3_14, isnull(amt_3_15,0) amt_3_15, isnull(amt_4_14,0) amt_4_14, isnull(amt_4_15,0) amt_4_15, isnull(amt_g, 0) amt_g, isnull(amtrate_1, 0) amtrate_1, isnull(comm, 0) comm, isnull(total_srv, 0) total_srv, isnull(total_perf, 0) total_perf, isnull(total_cash, 0) total_cash, isnull(total_cash_c, 0) total_cash_c, isnull(bonus, 0) bonus, isnull(total_srv_cash, 0) total_srv_cash, isnull(total_srv_card, 0) total_srv_card, isnull(num_fix, 0) num_fix, isnull(num_nofix, 0) num_nofix, isnull(num_in, 0) num_in, isnull(num_outer, 0) num_outer, isnull(consume_cost, 0) consume_cost, @compid compid,isnull(pay_0, 0) pay_0,isnull(pay_3, 0) pay_3,isnull(pay_$, 0) pay_$, isnull(pay_gave, 0) pay_gave,isnull(amt_sell, 0) amt_sell,isnull(amt_cons, 0) amt_cons from #empl_list_2, #worl_emp_money_pay where #empl_list_2.person_id = #worl_emp_money_pay.person_id order by isnull(total_srv, 0) + isnull(amt_c, 0) + isnull(amt_g, 0) desc end drop table #empl_list_2 drop table #worl_emp_money drop table #worl_emp_money_pay drop table #treatment_consumption drop table #treatment_sell drop table #card_fill_detail_by_payment drop table #bill end GO /****** Object: StoredProcedure [dbo].[upg_billpay_2_itempay] Script Date: 06/12/2017 06:19:30 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_billpay_2_itempay]( @compid varchar(10), @billid varchar(20)) as begin --需要把套餐支付以及疗程支付排除在外 declare @pay_proc varchar(20) select @pay_proc = '9' --建立gsm03的缓冲表 create table #gsm03_buf -- 单据--支付明细 ( gsc04c varchar(5) not null, --支付方式 gsc05f float null, --支付金额 PRIMARY KEY NonCLUSTERED(gsc04c) ) insert #gsm03_buf(gsc04c, gsc05f) select gsc04c, gsc05f from gsm03 with (nolock) where gsc00c = @compid and gsc02c = 'gx' and gsc01c = @billid and gsc04c not in (@pay_proc, '#') --建立ggm02的bug create table #ggm02_buf -- 项目消费(收银)--消费明细 ( idd int identity not null, -- 序号 ggb00c varchar(10) not null, --公司编号 ggb01c varchar(20) not null, --消费单号 ggb02f float not null, --序号 ggb03c varchar(20) null, --项目代码 ggb04c varchar(5) null, --单位 ggb05f float null, --数量 ggb06c varchar(5) null, --价格序号(可能为体验价格) ggb07f float null, --汇率 ggb08f float null, --标准单价 ggb09f float null, --折让 ggb10f float null, --折让单价 ggb11f float null, --金额 ggb12c varchar(20) null, --大工代码 ggb13c varchar(20) null, --中工代码 ggb14c varchar(20) null, --小工代码 ggb15c varchar(20) null, --大工类型 ggb16c varchar(20) null, --中工类型 ggb17f float null, --成本金额 ggb18f float null, --转卡比率 ggb19c varchar(20) null, --小工类型 ggb20f float null, --大工虚业绩 ggb21f float null, --中工虚业绩 ggb22f float null, --小工虚业绩 ggb23c varchar(128) null, --项目打折原因, added by MZH, 2005/09/01 ggb24f float null, --项目应收金额, added by MZH, 2005/09/08 ggb25c varchar(20) null, --四工类型 ggb26c varchar(20) null, --四工代码 ggb27c varchar(5) null, --项目支付方式 Added by WUXM 2005/11/09 ggb28f float null, --大工提成 Added by WUXM 2005/11/10 ggb29f float null, --中工提成 Added by WUXM 2005/11/10 ggb30f float null, --小工提成 Added by WUXM 2005/11/10 ggb31c varchar(20) null, --房间号(足浴) ggb32c varchar(20) null, --床位号(足浴) ggb33f float null, --项目积分 ggb34f float null, --实际营业额折算率 ggb35f float null, --四工虚业绩 ggb81c varchar(10) null, --部门编号 ggb36f float null, --大工实业绩 ggb37f float null, --中工实业绩 ggb38f float null, --小工实业绩 ggb39f float null, --四工实业绩 ggb40i int null, --欠款标志 ggb41d varchar(20) null, --还款日期 ggb42c varchar(20) null, --还款单号 ggb43c varchar(6) null, --开单时间 ggb44c varchar(6) null, --下钟时间 ggb45i int null, --价格序号,当用疗程的时候需要知道用了哪个疗程 ggb46f float null, --折扣 ggb47i int null, --疗程支付的时候的疗程ID ggb48i int null, --套餐序号 amt float null, --未满足金额 pay1 varchar(10) null, --第一支付方式 pay1amt float null, --第一支付方式金额 pay1num float null, --第一支付方式对应数量 pay2 varchar(10) null, pay2amt float null, pay2num float null, pay3 varchar(10) null, pay3amt float null, pay3num float null, primary key clustered(ggb00c, ggb01c, ggb02f) ) insert #ggm02_buf(ggb00c, ggb01c, ggb02f, ggb03c, ggb04c, ggb05f, ggb06c, ggb07f, ggb08f, ggb09f, ggb10f, ggb11f, ggb12c, ggb13c, ggb14c, ggb15c, ggb16c, ggb17f, ggb18f, ggb19c, ggb20f, ggb21f, ggb22f, ggb23c, ggb24f, ggb25c, ggb26c, ggb27c, ggb28f, ggb29f, ggb30f, ggb31c, ggb32c, ggb33f, ggb34f, ggb35f, ggb81c, ggb36f, ggb37f, ggb38f, ggb39f, ggb40i, ggb41d, ggb42c, ggb43c, ggb44c, ggb45i, ggb46f, ggb47i, ggb48i) select ggb00c, ggb01c, ggb02f, ggb03c, ggb04c, ggb05f, ggb06c, ggb07f, ggb08f, ggb09f, ggb10f, ggb11f, ggb12c, ggb13c, ggb14c, ggb15c, ggb16c, ggb17f, ggb18f, ggb19c, ggb20f, ggb21f, ggb22f, ggb23c, ggb24f, ggb25c, ggb26c, ggb27c, ggb28f, ggb29f, ggb30f, ggb31c, ggb32c, ggb33f, ggb34f, ggb35f, ggb81c, ggb36f, ggb37f, ggb38f, ggb39f, ggb40i, ggb41d, ggb42c, ggb43c, ggb44c, ggb45i, ggb46f, ggb47i, ggb48i from ggm02 with (nolock) where ggb00c = @compid and ggb01c = @billid and ggb27c not in (@pay_proc, '#') declare @cnt_gsm03 int declare @cnt_ggm02 int select @cnt_gsm03 = count(*) from #gsm03_buf select @cnt_ggm02 = count(*) from #ggm02_buf declare cur_each_gsm03 cursor for select gsc04c, gsc05f from #gsm03_buf --先初始化为项目的金额 update a set a.amt = ggb11f, pay1 = '', pay1amt = 0, pay1num = 0, pay2 = '', pay2amt = 0, pay2num = 0, pay3 = '', pay3amt = 0, pay3num = 0 from #ggm02_buf a declare cur_each_ggm02 cursor for select ggb05f, ggb11f, amt from #ggm02_buf where ggb27c not in (@pay_proc, '#') for update of amt, pay1, pay1amt, pay1num, pay2, pay2amt, pay2num, pay3, pay3amt, pay3num declare @gsm03_i int declare @ggm02_i int select @gsm03_i = 0, @ggm02_i = 0 --打开cussor declare @cur_pay varchar(10) declare @cur_payamt float open cur_each_gsm03 fetch cur_each_gsm03 into @cur_pay, @cur_payamt declare @ggm02_num float declare @ggm02_amt float declare @ggm02_remain float --未满足的金额 --ggm02中更新的位置 declare @update_pos int select @update_pos = 0 open cur_each_ggm02 fetch cur_each_ggm02 into @ggm02_num, @ggm02_amt, @ggm02_remain while @gsm03_i<@cnt_gsm03 and @ggm02_i<@cnt_ggm02 begin --如果第一个人需要的量不超过第一种水果,第一个人立刻满足 --150 100 if @ggm02_remain <= @cur_payamt begin if @update_pos = 0 begin update #ggm02_buf set pay1 = @cur_pay, pay1amt = @ggm02_remain, amt = 0 where current of cur_each_ggm02 select @update_pos = 1 end else if @update_pos = 1 begin update #ggm02_buf set pay2 = @cur_pay, pay2amt = @ggm02_remain, amt = 0 where current of cur_each_ggm02 select @update_pos = 2 end else if @update_pos = 2 begin update #ggm02_buf set pay3 = @cur_pay, pay3amt = @ggm02_remain, amt = 0 where current of cur_each_ggm02 select @update_pos = 0 end --支付方式没有用完,当前可用的支付金额还有 select @cur_payamt = @cur_payamt - @ggm02_remain --本人已经分配满足,去下一个人继续处理 select @ggm02_i = @ggm02_i + 1 fetch cur_each_ggm02 into @ggm02_num, @ggm02_amt, @ggm02_remain select @update_pos = 0 end else -- 苹果不够 begin if @update_pos = 0 begin update #ggm02_buf set pay1 = @cur_pay, pay1amt = @cur_payamt, amt = amt- @cur_payamt where current of cur_each_ggm02 select @update_pos = @update_pos + 1 end else if @update_pos = 1 begin update #ggm02_buf set pay2 = @cur_pay, pay2amt = @cur_payamt, amt = amt- @cur_payamt where current of cur_each_ggm02 select @update_pos = @update_pos + 1 end else if @update_pos = 2 begin update #ggm02_buf set pay3 = @cur_pay, pay3amt = @cur_payamt, amt = amt- @cur_payamt where current of cur_each_ggm02 select @update_pos = 0 end select @ggm02_remain = @ggm02_remain-@cur_payamt --取下一个水果 select @gsm03_i = @gsm03_i + 1 fetch cur_each_gsm03 into @cur_pay, @cur_payamt end end close cur_each_ggm02 deallocate cur_each_ggm02 --可能ggm02没有处理完,需要继续处理 --if @ggm02_i<@cnt_ggm02 begin update a set a.pay1 = @cur_pay, a.pay1amt = amt from #ggm02_buf a where isnull(pay1, '') = '' end --处理数量 declare @dig_num int declare @dig_prod_num int select @dig_num = gsa03f from gsm01 with (nolock) where gsa00c = @compid and gsa01c = 'SP005' select @dig_prod_num = gsa03f from gsm01 with (nolock) where gsa00c = @compid and gsa01c = 'SP027' update a set a.pay1num = convert(decimal(20, 2), a.ggb05f * (pay1amt/ggb11f)), a.pay2num = convert(decimal(20, 2), a.ggb05f * (pay2amt/ggb11f)), a.pay3num = a.ggb05f * (pay3amt/ggb11f) from #ggm02_buf a update a set a.pay3num = a.ggb05f - isnull(a.pay1num, 0) - isnull(a.pay2num, 0) from #ggm02_buf a --更新到原始的ggm02 update a set a.ggb27c = b.pay1, a.ggb05f = b.pay1num, a.ggb11f = b.pay1amt from ggm02 a, #ggm02_buf b where a.ggb00c = b.ggb00c and a.ggb01c = b.ggb01c and a.ggb02f = b.ggb02f and isnull(b.pay1, '')<>'' --新扩展的记录需要插入到ggm02 declare @max_seq float select @max_seq = max(ggb02f) from ggm02 with (nolock) where ggb00c = @compid and ggb01c = @billid insert ggm02(ggb00c, ggb01c, ggb02f, ggb03c, ggb04c, ggb05f, ggb06c, ggb07f, ggb08f, ggb09f, ggb10f, ggb11f, ggb12c, ggb13c, ggb14c, ggb15c, ggb16c, ggb17f, ggb18f, ggb19c, ggb20f, ggb21f, ggb22f, ggb23c, ggb24f, ggb25c, ggb26c, ggb27c, ggb28f, ggb29f, ggb30f, ggb31c, ggb32c, ggb33f, ggb34f, ggb35f, ggb81c, ggb36f, ggb37f, ggb38f, ggb39f, ggb40i, ggb41d, ggb42c, ggb43c, ggb44c, ggb45i, ggb46f, ggb47i, ggb48i) select ggb00c, ggb01c, @max_seq + idd, ggb03c, ggb04c, pay2num, ggb06c, ggb07f, ggb08f, ggb09f, ggb10f, pay2amt, ggb12c, ggb13c, ggb14c, ggb15c, ggb16c, ggb17f, ggb18f, ggb19c, ggb20f, ggb21f, ggb22f, ggb23c, ggb24f, ggb25c, ggb26c, pay2, ggb28f, ggb29f, ggb30f, ggb31c, ggb32c, ggb33f, ggb34f, ggb35f, ggb81c, ggb36f, ggb37f, ggb38f, ggb39f, ggb40i, ggb41d, ggb42c, ggb43c, ggb44c, ggb45i, ggb46f, ggb47i, ggb48i from #ggm02_buf where isnull(pay2, '')<>'' select @max_seq = max(ggb02f) from ggm02 with (nolock) where ggb00c = @compid and ggb01c = @billid insert ggm02(ggb00c, ggb01c, ggb02f, ggb03c, ggb04c, ggb05f, ggb06c, ggb07f, ggb08f, ggb09f, ggb10f, ggb11f, ggb12c, ggb13c, ggb14c, ggb15c, ggb16c, ggb17f, ggb18f, ggb19c, ggb20f, ggb21f, ggb22f, ggb23c, ggb24f, ggb25c, ggb26c, ggb27c, ggb28f, ggb29f, ggb30f, ggb31c, ggb32c, ggb33f, ggb34f, ggb35f, ggb81c, ggb36f, ggb37f, ggb38f, ggb39f, ggb40i, ggb41d, ggb42c, ggb43c, ggb44c, ggb45i, ggb46f, ggb47i, ggb48i) select ggb00c, ggb01c, @max_seq + idd, ggb03c, ggb04c, pay3num, ggb06c, ggb07f, ggb08f, ggb09f, ggb10f, pay3amt, ggb12c, ggb13c, ggb14c, ggb15c, ggb16c, ggb17f, ggb18f, ggb19c, ggb20f, ggb21f, ggb22f, ggb23c, ggb24f, ggb25c, ggb26c, pay3, ggb28f, ggb29f, ggb30f, ggb31c, ggb32c, ggb33f, ggb34f, ggb35f, ggb81c, ggb36f, ggb37f, ggb38f, ggb39f, ggb40i, ggb41d, ggb42c, ggb43c, ggb44c, ggb45i, ggb46f, ggb47i, ggb48i from #ggm02_buf where isnull(pay3, '')<>'' --处理产品 create table #ggm03_buf -- 项目消费--产品明细 ( idd int identity not null, ggc00c varchar(10) not null, --公司编号 ggc01c varchar(20) not null, --消费单号 ggc02f float not null, --序号 ggc03c varchar(20) null, --产品代码 ggc04c varchar(5) null, --单位 ggc05f float null, --数量 ggc06c varchar(5) null, --币别 ggc07f float null, --当时的进货成本, 也可能是月加权成本 ggc08f float null, --标准单价 ggc09f float null, --折让 ggc10f float null, --折让单价 ggc11f float null, --金额 ggc12c varchar(20) null, --产品销售人员 ggc13f float null, --总销售成本金额 ggc14f float null, --第一分享比率 ggc15c varchar(20) null, --第二人 ggc16f float null, --第二分享比率 ggc17c varchar(20) null, --第三人 ggc18f float null, --第三分享比率 ggc19c varchar(128) null, --产品打折原因, added by MZH, 2005/09/01 ggc20f float null, --产品应收单价, added by MZH, 2005/09/08 ggc21f float null, --第一销售虚业绩 Added by WUXM 2005/11/10 ggc22f float null, --第一销售提成 ggc23f float null, --第二销售虚业绩 Added by WUXM 2005/11/10 ggc24f float null, --第二销售提成 ggc25f float null, --第三销售虚业绩 Added by WUXM 2005/11/10 ggc26f float null, --第三销售提成 ggc27c varchar(5) null, --产品支付方式 ggc28f float null, --产品积分 ggc29f float null, --产品折扣折算比率(不是实际营业额折算比率) ggc30f float null, --实际营业额折算比率 ggc31c varchar(20) null, --仓库编号 ggc32i int null, --欠款标志 ggc33d varchar(20) null, --还款日期, 这个时候才计算提成 ggc34c varchar(20) null, --还款单号 ggc35i int null, --价格序号 ggc36i int null, --方式 0带走 1寄存 2欠货 ggc37f float null, --标准单位数量 ggc38f float null, --标准单位价格 ggc81c varchar(10) null, --部门编号 销售人员1 ggc82c varchar(10) null, --部门编号 销售人员2 ggc83c varchar(10) null, --部门编号 销售人员3 ggc48i int null, --套餐序号 ggc49f float null, --产品转卡率 amt float null, --未满足金额 pay1 varchar(10) null, --第一支付方式 pay1amt float null, --第一支付方式金额 pay1num float null, --第一支付方式对应数量 pay2 varchar(10) null, pay2amt float null, pay2num float null, pay3 varchar(10) null, pay3amt float null, pay3num float null, primary key clustered(ggc00c, ggc01c, ggc02f) ) insert #ggm03_buf(ggc00c, ggc01c, ggc02f, ggc03c, ggc04c, ggc05f, ggc06c, ggc07f, ggc08f, ggc09f, ggc10f, ggc11f, ggc12c, ggc13f, ggc14f, ggc15c, ggc16f, ggc17c, ggc18f, ggc19c, ggc20f, ggc21f, ggc22f, ggc23f, ggc24f, ggc25f, ggc26f, ggc27c, ggc28f, ggc29f, ggc30f, ggc31c, ggc32i, ggc33d, ggc34c, ggc35i, ggc36i, ggc37f, ggc38f, ggc81c, ggc82c, ggc83c, ggc48i, ggc49f) select ggc00c, ggc01c, ggc02f, ggc03c, ggc04c, ggc05f, ggc06c, ggc07f, ggc08f, ggc09f, ggc10f, ggc11f, ggc12c, ggc13f, ggc14f, ggc15c, ggc16f, ggc17c, ggc18f, ggc19c, ggc20f, ggc21f, ggc22f, ggc23f, ggc24f, ggc25f, ggc26f, ggc27c, ggc28f, ggc29f, ggc30f, ggc31c, ggc32i, ggc33d, ggc34c, ggc35i, ggc36i, ggc37f, ggc38f, ggc81c, ggc82c, ggc83c, ggc48i, ggc49f from ggm03 with (nolock) where ggc00c = @compid and ggc01c = @billid and ggc27c not in (@pay_proc, '#') declare @cnt_ggm03 int select @cnt_ggm03 = count(*) from #ggm03_buf --先初始化为项目的金额 update a set a.amt = ggc11f, pay1 = '', pay1amt = 0, pay1num = 0, pay2 = '', pay2amt = 0, pay2num = 0, pay3 = '', pay3amt = 0, pay3num = 0 from #ggm03_buf a declare cur_each_ggm03 cursor for select ggc05f, ggc11f, amt from #ggm03_buf where ggc27c not in (@pay_proc, '#') for update of amt, pay1, pay1amt, pay1num, pay2, pay2amt, pay2num, pay3, pay3amt, pay3num declare @ggm03_i int select @ggm03_i = 0 declare @ggm03_num float declare @ggm03_amt float declare @ggm03_remain float --未满足的金额 --ggm03中更新的位置 select @update_pos = 0 open cur_each_ggm03 fetch cur_each_ggm03 into @ggm03_num, @ggm03_amt, @ggm03_remain while @gsm03_i<@cnt_gsm03 and @ggm03_i<@cnt_ggm03 begin --如果第一个人需要的量不超过第一种水果,第一个人立刻满足 --150 100 if @ggm03_remain <= @cur_payamt begin if @update_pos = 0 begin update #ggm03_buf set pay1 = @cur_pay, pay1amt = @ggm03_remain, amt = 0 where current of cur_each_ggm03 select @update_pos = 1 end else if @update_pos = 1 begin update #ggm03_buf set pay2 = @cur_pay, pay2amt = @ggm03_remain, amt = 0 where current of cur_each_ggm03 select @update_pos = 2 end else if @update_pos = 2 begin update #ggm03_buf set pay3 = @cur_pay, pay3amt = @ggm03_remain, amt = 0 where current of cur_each_ggm03 select @update_pos = 0 end --支付方式没有用完,当前可用的支付金额还有 select @cur_payamt = @cur_payamt - @ggm03_remain --本人已经分配满足,去下一个人继续处理 select @ggm03_i = @ggm03_i + 1 fetch cur_each_ggm03 into @ggm03_num, @ggm03_amt, @ggm03_remain select @update_pos = 0 end else -- 苹果不够 begin if @update_pos = 0 begin update #ggm03_buf set pay1 = @cur_pay, pay1amt = @cur_payamt, amt = amt- @cur_payamt where current of cur_each_ggm03 select @update_pos = @update_pos + 1 end else if @update_pos = 1 begin update #ggm03_buf set pay2 = @cur_pay, pay2amt = @cur_payamt, amt = amt- @cur_payamt where current of cur_each_ggm03 select @update_pos = @update_pos + 1 end else if @update_pos = 2 begin update #ggm03_buf set pay3 = @cur_pay, pay3amt = @cur_payamt, amt = amt- @cur_payamt where current of cur_each_ggm03 select @update_pos = 0 end select @ggm03_remain = @ggm03_remain-@cur_payamt --取下一个水果 select @gsm03_i = @gsm03_i + 1 fetch cur_each_gsm03 into @cur_pay, @cur_payamt end end close cur_each_ggm03 deallocate cur_each_ggm03 --可能ggm03没有处理完,需要继续处理 --if @ggm03_i<@cnt_ggm03 begin update a set a.pay1 = @cur_pay, a.pay1amt = amt from #ggm03_buf a where isnull(pay1, '') = '' end --处理数量 update a set a.pay1num = convert(decimal(20, 2), a.ggc05f * (pay1amt/ggc11f)), a.pay2num = convert(decimal(20, 2), a.ggc05f * (pay2amt/ggc11f)), a.pay3num = a.ggc05f * (pay3amt/ggc11f) from #ggm03_buf a update a set a.pay3num = a.ggc05f - isnull(a.pay1num, 0) - isnull(a.pay2num, 0) from #ggm03_buf a --更新到原始的ggm03 update a set a.ggc27c = b.pay1, a.ggc05f = b.pay1num, a.ggc11f = b.pay1amt from ggm03 a, #ggm03_buf b where a.ggc00c = b.ggc00c and a.ggc01c = b.ggc01c and a.ggc02f = b.ggc02f and isnull(b.pay1, '')<>'' --新扩展的记录需要插入到ggm03 select @max_seq = max(ggc02f) from ggm03 with (nolock) where ggc00c = @compid and ggc01c = @billid insert ggm03(ggc00c, ggc01c, ggc02f, ggc03c, ggc04c, ggc05f, ggc06c, ggc07f, ggc08f, ggc09f, ggc10f, ggc11f, ggc12c, ggc13f, ggc14f, ggc15c, ggc16f, ggc17c, ggc18f, ggc19c, ggc20f, ggc21f, ggc22f, ggc23f, ggc24f, ggc25f, ggc26f, ggc27c, ggc28f, ggc29f, ggc30f, ggc31c, ggc32i, ggc33d, ggc34c, ggc35i, ggc36i, ggc37f, ggc38f, ggc81c, ggc82c, ggc83c, ggc48i, ggc49f) select ggc00c, ggc01c, @max_seq + idd, ggc03c, ggc04c, pay2num, ggc06c, ggc07f, ggc08f, ggc09f, ggc10f, pay2amt, ggc12c, ggc13f, ggc14f, ggc15c, ggc16f, ggc17c, ggc18f, ggc19c, ggc20f, ggc21f, ggc22f, ggc23f, ggc24f, ggc25f, ggc26f, pay2, ggc28f, ggc29f, ggc30f, ggc31c, ggc32i, ggc33d, ggc34c, ggc35i, ggc36i, ggc37f, ggc38f, ggc81c, ggc82c, ggc83c, ggc48i, ggc49f from #ggm03_buf where isnull(pay2, '')<>'' select @max_seq = max(ggc02f) from ggm03 with (nolock) where ggc00c = @compid and ggc01c = @billid insert ggm03(ggc00c, ggc01c, ggc02f, ggc03c, ggc04c, ggc05f, ggc06c, ggc07f, ggc08f, ggc09f, ggc10f, ggc11f, ggc12c, ggc13f, ggc14f, ggc15c, ggc16f, ggc17c, ggc18f, ggc19c, ggc20f, ggc21f, ggc22f, ggc23f, ggc24f, ggc25f, ggc26f, ggc27c, ggc28f, ggc29f, ggc30f, ggc31c, ggc32i, ggc33d, ggc34c, ggc35i, ggc36i, ggc37f, ggc38f, ggc81c, ggc82c, ggc83c, ggc48i, ggc49f) select ggc00c, ggc01c, @max_seq + idd, ggc03c, ggc04c, pay3num, ggc06c, ggc07f, ggc08f, ggc09f, ggc10f, pay3amt, ggc12c, ggc13f, ggc14f, ggc15c, ggc16f, ggc17c, ggc18f, ggc19c, ggc20f, ggc21f, ggc22f, ggc23f, ggc24f, ggc25f, ggc26f, pay3, ggc28f, ggc29f, ggc30f, ggc31c, ggc32i, ggc33d, ggc34c, ggc35i, ggc36i, ggc37f, ggc38f, ggc81c, ggc82c, ggc83c, ggc48i, ggc49f from #ggm03_buf where isnull(pay3, '')<>'' close cur_each_gsm03 deallocate cur_each_gsm03 drop table #gsm03_buf drop table #ggm02_buf drop table #ggm03_buf end GO /****** Object: StoredProcedure [dbo].[upg_getstdsum] Script Date: 06/12/2017 06:20:10 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_getstdsum]( @compid varchar(10), @goods varchar(20), @unit varchar(20), @sum float, --差异数量 @date varchar(8), @currsum float output, @outstdunit varchar(20)output, @incost float output) -- 进货产品 as begin declare @stdunit varchar(20) declare @smallunit varchar(20) declare @bigunit varchar(20) declare @bigstdrate float declare @stdsmallrate float declare @sp058 int select @sp058 = gsa03f from gsm01 where gsa01c = 'SP058' and gsa00c = @compid select @stdunit = gfa07c, -- 销售单位(标准单位) @smallunit = isnull(gfa08c, gfa07c), -- 消耗单位 @bigunit = isnull(gfa20c, gfa07c), -- 产品进货单位 @bigstdrate = isnull(gfa21f, 1), -- 进货单位对应销售单位数量 @stdsmallrate = isnull(gfa09f, 1), -- 销售单位对应消耗单位数量 @incost = (case @sp058 when 1 then gfa22f when 3 then gfa11f when 2 then gfa19f else -1 end) from gfm01 where gfa00c = @compid and gfa01c = @goods if(@sp058 = 5) --月加权成本 begin select @incost = gfh03f from gfm08 where gfh00c = @compid and gfh01c = @goods and gfh02c = substring(@date,0,7) end if (@unit = @stdunit) set @currsum = @sum else if (@unit = @smallunit) begin if (@stdsmallrate = 0) set @currsum = 0 else set @currsum = @sum/@stdsmallrate end else if (@unit = @bigunit) begin set @currsum = @sum*@bigstdrate end set @outstdunit = @stdunit end GO /****** Object: StoredProcedure [dbo].[upg_recalc_account] Script Date: 06/12/2017 06:20:32 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_recalc_account] ( @compid varchar(10), -- 公司编号, @noteId varchar(20) -- 单据编号, ) as--WUXM, 2004/04/05 set nocount on set rowcount 0 set transaction isolation level read uncommitted set transaction isolation level read uncommitted begin begin try begin transaction declare @time varchar(6) declare @hour varchar(2) declare @minute varchar(2) declare @second varchar(2) select @hour = convert(varchar(2), DATEPART(hh, GETDATE())) if len(@hour) = 1 select @hour = '0' + @hour select @minute = convert(varchar(2), DATEPART(mi, GETDATE())) if len(@minute) = 1 select @minute = '0' + @minute select @second = convert(varchar(2), DATEPART(ss, GETDATE())) if len(@second) = 1 select @second = '0' + @second select @time = @hour + @minute + @second -- re calculate 各钥匙牌的消费金额 -- 项目消费金额 update glm03 set glc03f = (select sum(gga16f) from ggm01 with (nolock), glm02 with (nolock) where gga00c = glb00c and gga01c = glb03c and gga29c = glb02c and glb00c = glc00c and glb01c = glc01c and glc02c = gga08c), glc04f = (select sum(gga17f) from ggm01 with (nolock), glm02 with (nolock) where gga00c = glb00c and gga01c = glb03c and gga29c = glb02c and glb00c = glc00c and glb01c = glc01c and glc02c = gga08c), glc06f = 0 where glc00c = @compid and glc01c = @noteId -- 更新该钥匙牌的总台支付 declare @sp031f float select @sp031f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP031' if isnull(@sp031f, 0) = 2 begin update c set glc06f = (select isnull(sum(gsc05f), 0) from gsm03 with (nolock) where gsc00c = glc00c and gsc01c = glc01c and gsc02c = 'gl' and gsc08c = glc02c ) from glm03 c where glc00c = @compid and glc01c = @noteId end -- update 主档 update glm01 --gla04t = @time, set gla11f = (select sum(gga16f) from ggm01 with (nolock) where gga00c = gla00c and gga30c = gla01c), gla12f = (select sum(gga17f) from ggm01 with (nolock) where gga00c = gla00c and gga30c = gla01c), gla20f = (select sum(gsc05f) from gsm03 with (nolock) where gsc00c = gla00c and gsc01c = gla01c and gsc02c = 'gl') where gla00c = @compid and gla01c = @noteId commit transaction end try begin catch if(@@trancount > 0) begin rollback transaction end end catch end GO /****** Object: StoredProcedure [dbo].[upg_personal_statistics_paymode_ex] Script Date: 06/12/2017 06:20:26 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_personal_statistics_paymode_ex] ( @compid varchar(10), -- 公司别 @fromdate varchar(10), -- 开始日期 @todate varchar(10), -- 截至日期 @fromempl varchar(20), -- 查询开始人员 @toempl varchar(20), -- 查询截至人员 @inc_card int, -- 卡销售 @inc_service int, -- 服务 @inc_goods int, -- 是否包括产品销售 @return_type int, @paymode varchar(20), @cardtype varchar(5), @spid int ) -- return 数据的类型, 1-明细数据员工业绩统计报表 -- 2-每人一个提成数据, 用于计算员工薪资 -- 3-业绩排行-虚业绩, 4-业绩排行-营业金额 -- 5-业绩排行-实业绩 -- 7-每人按项目分组 6-总提成 as begin exec upg_paymode_to_java @paymode,@paymode output create table #empl_stat_outer ( id int identity, -- 流水号 person_id varchar(20) not null, -- 员工代码 comp_ascription varchar(10) null, -- 公司别(员工) action_id tinyint not null, -- 类别, 1-卡销售 2-指定大工, 3-轮班大工, 4-补工大工, -- 5-指定中工, 6-轮班中工 -- 7-指定小工, 8-轮班小工, 9-产品销售 10,11-四工 -- 0 卡充值 srvdate varchar(10) not null, -- 日期 code varchar(20) not null, -- 项目代码, 或是卡号, 产品码 name varchar(200) null, -- 名称 payway varchar(40) null, -- 支付方式 amt float null, -- 营业金额 quan float null, -- 数量 cost float null, -- 成本 comm float null, -- 提成 amt2 float null, -- 虚业绩 amt3 float null, -- 真实业绩 cardrate float null, -- 转卡比率 stdprice float null, -- 标准单价 prj_type varchar(20) null, -- 项目类别 share_rate float default 1 null, --分享比率 cls_flag int null, -- 1:项目 2:产品 3:卡 gender int null, -- 性别 0:女 1:男 billid varchar(30) null, -- 单号 paycode varchar(30) null, -- 支付代码 ex_rate float null, -- 实际营业额折算比率 ggm02_seq float null, -- ggm02中的序列号, 与单号一起来标志一个项目 room_id varchar(30) null, -- 房间号,只在spa或足浴版本需要 cardid varchar(60) null, -- 会员卡号 memid varchar(60) null, -- 会员编号 sumMoney float null, -- 会员卡销售总金额 compid varchar(20) null, is_cashpay int null, ) create clustered index idx_empl_stat_outer_01 on #empl_stat_outer(person_id, prj_type) create table #cash_pay --现金类支付方式 ( paycode varchar(20) not null, -- 支付代码 primary key (paycode) ) -- 子门店业绩数据循环放到upg_personal_statistics中了 insert #empl_stat_outer(person_id, comp_ascription, action_id, srvdate, code, name, payway, amt, quan, cost, comm, amt2, amt3, cardrate, stdprice, prj_type, share_rate, cls_flag, gender, billid, paycode, ex_rate, ggm02_seq, room_id, cardid, memid) exec upg_personal_statistics @compid, @fromdate, @todate, @fromempl, @toempl, @inc_card, @inc_service, @inc_goods, @return_type, @spid declare @sp048 varchar(10) select @sp048 = gsa02c from gsm01 with (nolock) where gsa00c = @compid and gsa01c = 'SP048' if @sp048 <> '1' begin create table #tab_sum ( empid varchar(20), code varchar(20), action_id tinyint, billid varchar(20) null, sumMoney float ) insert into #tab_sum(empid, code, action_id, sumMoney, billid) select person_id, code, action_id, sum(isnull(amt, 0)), billid from #empl_stat_outer where action_id in(1, 0) group by code, person_id, action_id, billid update a set a.sumMoney = b.sumMoney from #empl_stat_outer a, #tab_sum b where a.action_id in(1, 0) and a.code = b.code and a.action_id = b.action_id and empid = person_id and a.billid = b.billid update #empl_stat_outer set quan = (case when sumMoney = 0 then quan else amt / sumMoney end) where action_id in(1, 0) drop table #tab_sum end insert #cash_pay(paycode) select pay from pay_classify with(nolock) where comp = @compid and category = '1' update a set a.is_cashpay = 1 from #empl_stat_outer a,#cash_pay b where a.paycode = b.paycode and a.action_id not in (0,1,9) declare @sql nvarchar(2000) set @sql = ' select isnull(id,'''') id, isnull(person_id,'''') person_id, isnull(comp_ascription,'''') comp_ascription, isnull(action_id,'''') action_id, isnull(srvdate,'''') srvdate, isnull(code,'''') code, isnull(name,'''') name, isnull(payway,'''') payway, isnull(amt,'''') amt, isnull(quan,'''') quan, isnull(cost,'''') cost, isnull(comm,'''') comm, isnull(amt2,'''') amt2, isnull(amt3,'''') amt3, isnull(cardrate,'''') cardrate, isnull(stdprice,'''') stdprice, isnull(prj_type,'''') prj_type, isnull(share_rate,'''') share_rate, isnull(cls_flag,'''') cls_flag, isnull(gender,'''') gender, isnull(billid,'''') billid, isnull(paycode,'''') paycode, isnull(ex_rate,'''') ex_rate, isnull(ggm02_seq,'''') ggm02_seq, isnull(room_id,'''') room_id, isnull(cardid,'''') cardid, isnull(memid,'''') memid, isnull(sumMoney,'''') sumMoney, isnull(compid,'''') compid, isnull(is_cashpay,0) is_cashpay from #empl_stat_outer ' if isnull(@cardtype, '') <> '' and @cardtype <> '*' begin set @sql = @sql + ', gcm01 with (nolock) where isnull(cardid, '''') <> '''' and substring(cardid, 1, 2) <> ''散客'' and gca01c = cardid and gca00c = gca13d and gca02c = ''' + @cardtype + '''' end if isnull(@paymode, '') <> '' and @paymode <> '*' begin if isnull(@cardtype, '') = '' or @cardtype = '*' begin set @sql = @sql + ' where' end else begin set @sql = @sql + ' and' end set @sql = @sql + ' (paycode = ''' + @paymode + ''' or isnull(paycode, '''') = '''')' end set @sql = @sql + ' order by person_id, action_id, srvdate, id ' exec sp_executesql @sql drop table #empl_stat_outer drop table #cash_pay end GO /****** Object: StoredProcedure [dbo].[upg_member_nocard_operation_details_ceshi] Script Date: 06/12/2017 06:20:23 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_member_nocard_operation_details_ceshi] ( @compid varchar(10), @fromdate varchar(20), @todate varchar(20), @spid int, @state int ) as begin declare @incompid varchar(10) select @incompid = @compid declare @sp023 varchar(10) select @sp023 = gsa02c from gsm01 where gsa00c = @compid and gsa01c = 'SP023' if @fromdate = '' or @fromdate = '*' begin select @fromdate = '19900101' select @todate = '20901231' end create table #details ( memid varchar(20) not null, seq int identity not null, date varchar(8) not null, action varchar(4) null, action_name varchar(20) null, item varchar(20) not null, name varchar(100) null, price float null, --标准价格 amt float null, person1 varchar(20) null, person2 varchar(20) null, person3 varchar(20) null, compid varchar(10) null, billid varchar(20) null, type varchar(10) null, fillmode varchar(10) null, quan float null, ttime varchar(20) null, paycode varchar(5) null, person varchar(40) null, primary key(seq) ) create table #details_sum ( memid varchar(20) not null, quan_sum float null, amt_sum float null, primary key(memid) ) insert #details(date, action, item, name, price, amt, person1, person2, person3, compid, billid, type, fillmode, quan, ttime, paycode,memid) select gct04d, gct06c, gct07c, gct08c, gct09f, gct12f, gct15c, gct16c, gct17c, gct00c, gct14c, gct13c, gct05c, gct11f, gct19t, gct20c,memid from gcm20 with (nolock),gma06r_cursor b with(nolock), gam26 z where gct03c = b.memid and gct00c = z.gaz02c and z.gaz01c = @incompid and gct04d >= @fromdate and gct04d <= @todate and b.spid = @spid order by gct01i insert #details_sum(amt_sum,quan_sum,memid) select sum(gct12f),sum(gct11f),memid from gcm20 with (nolock),gma06r_cursor b with(nolock), gam26 z where gct03c = b.memid and gct00c = z.gaz02c and z.gaz01c = @incompid and gct04d >= @fromdate and gct04d <= @todate and b.spid = @spid group by memid update #details set action_name = '产品' where action = '4' update #details set action_name = '消费' where action = '3' update a set a.person1 = d.haa02c from #details a, ham01 d where a.person1 = d.haa01c and a.compid = d.haa00c and isnull(a.person1, '') <> '' update a set a.person2 = d.haa02c from #details a, ham01 d where a.person2 = d.haa01c and a.compid = d.haa00c and isnull(a.person2, '') <> '' update a set a.person3 = d.haa02c from #details a, ham01 d where a.person3 = d.haa01c and a.compid = d.haa00c and isnull(a.person3, '') <> '' update a set a.person = a.person1 + a.person2 + a.person3 from #details a select * from #details a, #details_sum b where (action ='4' or action ='3') and a.memid = b.memid order by date, ttime, seq if(@state = 0) begin delete from gma06r_cursor where spid = @spid end drop table #details drop table #details_sum end GO /****** Object: StoredProcedure [dbo].[upg_inoutstock_statistic_gfm08_fh] Script Date: 06/12/2017 06:20:15 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_inoutstock_statistic_gfm08_fh] ( @compid varchar(10), -- 公司编号 @month varchar(10), -- 结束日期 @gfd02c varchar(100) -- 异动单号 ) as begin create table #gfm01 ( id int identity not null, gfa00c varchar(10) not null, -- 门店编号 gfa01c varchar(20) not null, -- 产品编号 gfa03c varchar(100) null, -- 产品名称 ) create clustered index idx_gfm01 on #gfm01(gfa00c,gfa01c) --插入该门店所有未停用的产品资料 insert #gfm01(gfa00c,gfa01c,gfa03c) select distinct gfa00c,gfa01c,gfa03c from gfm01 with(nolock),gfm04 b with (nolock) where gfa10c = '0' and gfa00c = @compid and b.gfd00c = @compid and b.gfd02c = @gfd02c --异动单号 --and gfa01c = 'AV107' --删除 delete a from gfm08 a,#gfm01 b where a.gfh00c = @compid and (a.gfh02c <= @month or isnull(a.gfh02c,'') = '') and a.gfh00c = b.gfa00c and a.gfh01c = b.gfa01c --声明变量 declare @curdate varchar(20), @time varchar(20) declare @starmonth varchar(8)--最初入库或出库日期 declare @domonth varchar(8) declare @initial_amount float --期初量 declare @initial_amt float --期初金额 declare @next_num float --下一月入库数量 declare @next_amt float --下一月入库金额 declare @next_out_num float --下一月出库数量 declare @next_out_amt float --下一月出库金额 declare @startnum float --最初入库数量 declare @startamt float --最初入库金额 declare @outnum float --最初入库数量 declare @outamt float --最初入库金额 declare @initial_volume float --期初量 declare @gfh03f float --月加权价 declare @dogfh03f float --月加权价 declare @gfa00c varchar(10) --门店编号 declare @gfa01c varchar(20) --产品编号 declare @gfa03c varchar(100) --产品名称 declare @id int --循环变量id declare @SP103 int declare @state int --循环条件 set @state = 1 select @SP103 = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP103' exec upg_getcurrdate @curdate output, @time output --set @compid = '002' while(@state=1) begin if exists(select 1 from #gfm01) begin select top 1 @id = id,@gfa00c = gfa00c,@gfa01c = gfa01c ,@gfa03c = gfa03c from #gfm01 set @state = 1 if(@SP103 = 1) begin --起初入库或出库时间 select @starmonth = min(gfd07c) from gfm04 with (nolock), gam26 with (nolock) where gaz01c = @gfa00c and gfd00c = gaz02c and gfd04c = @gfa01c --and gfd13f > 0 and gfd05f > 0 --and gfd01c = '1' select @starmonth = substring(@starmonth,0,7) --print @starmonth --起初入库数量,金额 select @startnum = sum(gfd05f),@startamt = sum(gfd13f) from gfm04 with (nolock), gam26 with (nolock) where gaz01c = @gfa00c and gfd00c = gaz02c and gfd01c = '1' --and gfd13f > 0 and gfd05f > 0 and gfd04c = @gfa01c and substring(gfd07c,0,7) = @starmonth --起初出库数量和金额 select @outnum = sum(gfd05f),@outamt = sum(gfd13f) from gfm04 with (nolock), gam26 with (nolock) where gaz01c = @gfa00c and gfd00c = gaz02c and gfd01c <> '1' and gfd04c = @gfa01c and substring(gfd07c,0,7) = @starmonth end else begin --起初入库或出库时间 select @starmonth = min(gfd07c) from gfm04 with (nolock) where gfd00c = @gfa00c and gfd04c = @gfa01c --and gfd13f > 0 and gfd05f > 0 --and gfd01c = '1' select @starmonth = substring(@starmonth,0,7) --print @starmonth --起初入库数量,金额 select @startnum = sum(gfd05f),@startamt = sum(gfd13f) from gfm04 with (nolock) where gfd00c = @gfa00c and gfd01c = '1' --and gfd13f > 0 and gfd05f > 0 and gfd04c = @gfa01c and substring(gfd07c,0,7) = @starmonth --起初出库数量和金额 select @outnum = sum(gfd05f),@outamt = sum(gfd13f) from gfm04 with (nolock) where gfd00c = @gfa00c and gfd01c <> '1' and gfd04c = @gfa01c and substring(gfd07c,0,7) = @starmonth end -- 初月月加权平均价 set @gfh03f = dbo.fun_divide_default(@startamt,@startnum,0) if(@starmonth <= @month) begin print @starmonth insert gfm08(gfh00c,gfh01c,gfh02c,gfh03f,gfh04d,gfh05t,gfh06f,gfh07f,gfh08i) values(@gfa00c, @gfa01c, isnull(@starmonth,''), @gfh03f, @curdate, @time, isnull(@startnum,0),0,0) end --初始化变量 declare @start2 int set @start2 = 1 set @starmonth = @starmonth+'01' set @initial_volume = 0 set @initial_amount = 0 set @next_out_num = 0 set @initial_amt = 0 set @next_out_amt = 0 --期末金额 set @initial_amt = (isnull(@startnum,0) - isnull(@outnum,0))*isnull(@gfh03f,0) --期末量 set @initial_amount = isnull(@startnum,0) - isnull(@outnum,0) while(@start2 = 1) begin select @domonth = substring(convert(char(8),dateadd(month,1,@starmonth),112),0,7) if(@domonth <= @month) begin if(@SP103 = 1) begin --本月的入库数量,金额 select @next_num = sum(gfd05f),@next_amt = sum(gfd13f) from gfm04 with (nolock), gam26 with (nolock) where gaz01c = @gfa00c and gfd00c = gaz02c and gfd01c = '1' --and gfd13f > 0 and gfd05f > 0 and gfd04c = @gfa01c and substring(gfd07c,0,7) = @domonth --本月的出库数量,金额 select @next_out_num = sum(gfd05f),@next_out_amt = sum(gfd13f) from gfm04 with (nolock), gam26 with (nolock) where gaz01c = @gfa00c and gfd00c = gaz02c and gfd01c <> '1' and gfd04c = @gfa01c and substring(gfd07c,0,7) = @domonth end else begin --本月的入库数量,金额 select @next_num = sum(gfd05f),@next_amt = sum(gfd13f) from gfm04 with (nolock) where gfd00c = @gfa00c and gfd01c = '1' --and gfd13f > 0 and gfd05f > 0 and gfd04c = @gfa01c and substring(gfd07c,0,7) = @domonth --本月的出库数量,金额 select @next_out_num = sum(gfd05f),@next_out_amt = sum(gfd13f) from gfm04 with (nolock) where gfd00c = @gfa00c and gfd01c <> '1' and gfd04c = @gfa01c and substring(gfd07c,0,7) = @domonth end --本月和上一个月的入库金额,数量 set @initial_amt = isnull(@initial_amt,0)+isnull(@next_amt,0) set @initial_amount = isnull(@initial_amount,0)+isnull(@next_num,0) --本月的月加权平均价 set @dogfh03f = dbo.fun_divide_default(@initial_amt,@initial_amount,0) if(isnull(@dogfh03f,0) = 0) begin insert gfm08(gfh00c,gfh01c,gfh02c,gfh03f,gfh04d,gfh05t,gfh06f,gfh07f,gfh08i) values(@gfa00c, @gfa01c, isnull(@domonth,''), @gfh03f, @curdate, @time, isnull(@next_num,0),isnull(@initial_volume,0),0) --期初量计算 set @initial_volume = isnull(@initial_amount,0) - isnull(@next_out_num,0) set @initial_amount = @initial_volume end else begin insert gfm08(gfh00c,gfh01c,gfh02c,gfh03f,gfh04d,gfh05t,gfh06f,gfh07f,gfh08i) values(@gfa00c, @gfa01c, isnull(@domonth,''), @dogfh03f, @curdate, @time, isnull(@next_num,0),isnull(@initial_volume,0),0) -- 这个月的期末量,金额,最为下一个月的期初量,金额 set @initial_volume = isnull(@initial_amount,0) - isnull(@next_out_num,0) set @initial_amt = isnull(@initial_volume,0)*@dogfh03f set @initial_amount = @initial_volume set @gfh03f = @dogfh03f set @next_out_num = 0 end set @starmonth = @domonth+'01' --初始化变量 set @next_amt = 0 set @next_num = 0 set @outnum = 0 set @next_amt = 0 set @outamt = 0 end else begin set @start2 = 0 end end set @gfh03f = 0 delete #gfm01 where id = @id end else begin set @state = 0 end end drop table #gfm01 end GO /****** Object: StoredProcedure [dbo].[upg_updategcm10_accountdate] Script Date: 06/12/2017 06:20:51 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_updategcm10_accountdate] ( @compid varchar(10), --公司编号 @consumeid varchar(20) --消费单号 ) as --WUXM 2005/08/16 begin declare @begtime varchar(20) -- 工作日开始时间 declare @endtime varchar(20) -- 工作日结束时间,表示凌晨时间,如果为000000,则表示帐务日期和实际日期一致 declare @time varchar(6) -- 实际结帐时间 declare @date varchar(8) -- 实际日期 declare @staff varchar(20) -- 登陆人 declare @acctdate varchar(20) -- 账户日期 declare @acttmode int declare @cardid varchar(20) select @begtime = gsa02c from gsm01 where gsa00c = @compid and gsa01c = 'SP050' select @endtime = gsa02c from gsm01 where gsa00c = @compid and gsa01c = 'SP051' declare @curtime varchar(8) declare @curdate varchar(10) select @curtime = gcl56t, @curdate = gcl02d, @staff = gcl93c, @acttmode = gcl06i, @cardid = gcl03c from gcm10 where gcl00c = @compid and gcl01c = @consumeid select @time = @curtime select @date = @curdate declare @newdate varchar(8) declare @strdate varchar(10) --帐务日期比正常日期短 if (@endtime > @begtime and @time > @endtime) begin select @strdate = convert(varchar(10), dateadd(day, 1, cast(@date as datetime)), 120) select @newdate = substring(@strdate, 1, 4) + substring(@strdate, 6, 2) + substring(@strdate, 9, 2) update a set a.gcl80d = @newdate from gcm10 a where gcl00c = @compid and gcl01c = @consumeid select @acctdate = @newdate end --帐务日期比正常日期长 else if (@endtime <= @begtime and @time <= @endtime) begin select @strdate = convert(varchar(10), dateadd(day, -1, cast(@date as datetime)), 120) select @newdate = substring(@strdate, 1, 4) + substring(@strdate, 6, 2) + substring(@strdate, 9, 2) update a set a.gcl80d = @newdate from gcm10 a where gcl00c = @compid and gcl01c = @consumeid select @acctdate = @newdate end --帐务日期和正常日期一致 else begin update a set a.gcl80d = @date from gcm10 a where a.gcl00c = @compid and a.gcl01c = @consumeid select @acctdate = @date end --更新gsm03新增加的字段 update a set a.gsc10d = @acctdate, a.gsc11c = @staff, a.gsc12i = @acttmode, gsc08c = @cardid from gsm03 a where gsc00c = @compid and gsc01c = @consumeid and gsc02c = 'gz' end GO /****** Object: StoredProcedure [dbo].[upg_member_operation_details_ceshi] Script Date: 06/12/2017 06:20:23 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_member_operation_details_ceshi] ( @compid varchar(10), @fromdate varchar(8), @todate varchar(8), @orderby int, -- 0 asc 1 desc @spid int, @state int ) as begin declare @incompid varchar(10) select @incompid = @compid -- 是否允许门店查看所有分店的消费历史 declare @sp023 varchar(10) select @sp023 = gsa02c from gsm01 with(nolock) where gsa00c = @compid and gsa01c = 'SP023' if @fromdate = '' or @fromdate = '*' begin select @fromdate = '19900101' select @todate = '20901231' end create table #details ( cardid varchar(20) not null, seq int identity not null, date varchar(8) not null, action varchar(20) null, action_name varchar(20) null, item varchar(20) null, name varchar(100) null, memo varchar(50) null, -- 备注 price float null, -- 标准价格 amt float null, person varchar(40) null, person1 varchar(20) null, person2 varchar(20) null, person3 varchar(20) null, person4 varchar(20) null, compid varchar(40) null, billid varchar(20) null, type varchar(10) null, fillmode varchar(10) null, quan float null, ttime varchar(20) null, paycode varchar(5) null, gct21f float null, compname varchar(40) null, primary key(seq) ) create table #details_sum ( cardid varchar(20) not null, amt_sum float null, quan_sum float null, primary key(cardid) ) insert #details(date, action, item, name, price, amt, person1, person2, person3, person4, compid, billid, type, fillmode, quan, ttime, paycode, gct21f,cardid) select gct04d, gct06c, gct07c, gct08c, gct09f, gct12f, gct15c, gct16c, gct17c, gct18c, gct00c, gct14c, gct13c, gct05c, gct11f, gct19t, gct20c, gct21f,cardid from gcm20 with (nolock), gma06r_cursor b with(nolock), gam26 z with(nolock) where gct02c = b.cardid and gct00c = z.gaz02c and z.gaz01c = @incompid and gct04d >= @fromdate and gct04d <= @todate and b.spid = @spid order by gct01i insert #details_sum(amt_sum,quan_sum,cardid) select sum(gct12f), sum(gct11f),cardid from gcm20 with (nolock), gma06r_cursor b with(nolock), gam26 z with(nolock) where gct02c = b.cardid and gct00c = z.gaz02c and z.gaz01c = @incompid and gct04d >= @fromdate and gct04d <= @todate and b.spid = @spid group by cardid update a set a.memo = d.memo from #details a, ggm02 d with(nolock) where a.action = 3 and a.type = 'gx' and a.compid = d.ggb00c and a.billid = d.ggb01c and a.item = d.ggb03c and (isnull(a.gct21f, -1) = -1 or a.gct21f = d.ggb02f) update #details set action_name = '买卡' where action = '1' update #details set action_name = '疗程' where action = '2' update #details set action_name = '产品' where action = '4' update #details set action_name = '消费' where action = '3' update #details set action_name = '充卡' where action = '5' and fillmode = '0' update #details set action_name = '疗程' where action = '6' and fillmode = '0' update #details set action_name = '还款' where (action = '5' or action = '6') and fillmode = '6' update #details set action_name = '取款' where (action = '5' or action = '6') and (fillmode = '1' or fillmode = '5') update #details set action_name = '套餐' where action = '7' or action = '8' update a set a.person1 = d.haa02c from #details a, ham01 d with(nolock) where a.person1 = d.haa01c and a.compid = d.haa00c and isnull(a.person1, '') <> '' update a set a.person2 = d.haa02c from #details a, ham01 d with(nolock) where a.person2 = d.haa01c and a.compid = d.haa00c and isnull(a.person2, '') <> '' update a set a.person3 = d.haa02c from #details a, ham01 d with(nolock) where a.person3 = d.haa01c and a.compid = d.haa00c and isnull(a.person3, '') <> '' update a set a.person4 = d.haa02c from #details a, ham01 d with(nolock) where a.person4 = d.haa01c and a.compid = d.haa00c and isnull(a.person4, '') <> '' update a set a.person = a.person1 + a.person2 + a.person3+ a.person4 from #details a update a set a.compname = d.gae03c from #details a, gam05 d with(nolock) where compid = d.gae01c if @orderby = 0 begin select * from #details a, #details_sum b where a.cardid = b.cardid and (action ='4' or action ='3') order by date asc, ttime asc, seq asc end else begin select * from #details a, #details_sum b where a.cardid = b.cardid and (action ='4' or action ='3') order by date desc, ttime desc, seq desc end -- if(@state = 0) -- begin -- delete from gma06r_cursor where spid = @spid -- end drop table #details drop table #details_sum end GO /****** Object: StoredProcedure [dbo].[upg_get_card_combo] Script Date: 06/12/2017 06:20:05 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_get_card_combo] ( @gte00c varchar(10), -- 公司编号 @gte01c varchar(20), -- 卡号 @onlymain int -- ) as begin create table #result ( groupc varchar(200) null, gte00c varchar(10) null, -- 公司编号 gte01c varchar(20) null, -- 卡号 gte02c varchar(20) null, -- 套餐编号 gte03c varchar(20) null, -- 项目编号 gte04f float null, -- 项目总次数 gte05f float null, -- 已经使用次数 gte06f float null, -- 剩余次数 gte08c varchar(20) null, -- 主项目 gte09i int null, -- 序号 gte10c varchar(20) null, -- 单据类型 gte11c varchar(50) null, -- 单号 gda03c varchar(100) null, -- 项目简称 amt float null, -- 核算金额 remainamt float null, -- 核算金额 type varchar(20) null, -- 类别 valid varchar(20) null, -- 是否有效 sdate varchar(10) null, -- 购买日期 edate varchar(10) null, -- 套餐截止日期 ) declare @SP217_2 varchar(2) --套餐是否严格控制有效时长 declare @SP217_3 varchar(2) --套餐是否严格控制有效时间范围 select @SP217_2 = gsa02c, @SP217_3 = gsa03f from gsm01 where gsa00c = @gte00c and gsa01c = 'SP217' if (@SP217_2 = '0' and @SP217_3 = '0') begin insert into #result(gte00c, gte01c, gte02c, gte03c, gte04f, gte05f, gte06f, gte08c, gte09i, gte10c, gte11c, type, valid,sdate,edate) select gte00c, gte01c, gte02c, gte03c, gte04f, gte05f, gte06f, gte08c, gte09i, gte10c, gte11c, '项目', (case when (gte10c is null or gte11c is null) then 1 else 0 end),'','' from gtc04 a where a.gte00c = @gte00c and a.gte01c = @gte01c and isnull(gte12i,0) <> -1 --增加状态判断(0为正常,-1为退货) and ((a.gte06f > 0 and a.gte03c = a.gte08c) or (0 = @onlymain and a.gte03c <> a.gte08c)) end else begin --2个权限都有,则取第1个 if((@SP217_2 = '1' and @SP217_3 = '0') or (@SP217_2 = '1' and @SP217_3 = '1')) begin insert into #result(gte00c, gte01c, gte02c, gte03c, gte04f, gte05f, gte06f, gte08c, gte09i, gte10c, gte11c, type, valid,sdate,edate) select gte00c, gte01c, gte02c, gte03c, gte04f, gte05f, gte06f, gte08c, gte09i, gte10c, gte11c, '项目', (case when (gte10c is null or gte11c is null) then 1 else 0 end), isnull(gte15d,'') as sdate, convert(varchar(10),(case gte14c when '1' then dateadd(year,gte13i,gte15d) --年 when '2' then dateadd(month,gte13i,gte15d) --月 when '3' then dateadd(day,gte13i,gte15d) --日 when '4' then dateadd(month,gte13i * 3 ,gte15d) --季度 end),112) as edate from gtc04 a where a.gte00c = @gte00c and a.gte01c = @gte01c and isnull(gte12i,0) <> -1 --增加状态判断(0为正常,-1为退货) and ((a.gte06f > 0 and a.gte03c = a.gte08c) or (0 = @onlymain and a.gte03c <> a.gte08c)) end if(@SP217_2 = '0' and @SP217_3 = '1' ) begin insert into #result(gte00c, gte01c, gte02c, gte03c, gte04f, gte05f, gte06f, gte08c, gte09i, gte10c, gte11c, type, valid,sdate,edate) select gte00c, gte01c, gte02c, gte03c, gte04f, gte05f, gte06f, gte08c, gte09i, gte10c, gte11c, '项目', (case when (gte10c is null or gte11c is null) then 1 else 0 end),--gte16c,gte17c isnull(gte15d,'') as sdate, convert(varchar(10),(case gte14c when '1' then dateadd(year,gte13i,gte15d) --年 when '2' then dateadd(month,gte13i,gte15d) --月 when '3' then dateadd(day,gte13i,gte15d) --日 when '4' then dateadd(month,gte13i * 3 ,gte15d) --季度 end),112) as edate from gtc04 a where a.gte00c = @gte00c and a.gte01c = @gte01c and isnull(gte12i,0) <> -1 --增加状态判断(0为正常,-1为退货) and ((a.gte06f > 0 and a.gte03c = a.gte08c) or (0 = @onlymain and a.gte03c <> a.gte08c)) end end update a set a.amt = c.gtb04f, a.remainamt = a.gte06f * c.gtb04f from #result a, gtc02 c where c.gtb00c = @gte00c and c.gtb01c = a.gte02c and c.gtb02c = a.gte03c and a.type = N'项目' update a set a.gda03c = b.gda03c from #result a, gdm01 b where b.gda00c = @gte00c and a.gte03c = b.gda01c and a.type = N'项目' update a set a.groupc = (a.gte02c + '-' + case a.gte10c when 'gn' then '会员卡销售' when 'gz' then '账户异动单' end) + '-门店:' + isnull(d.gti00c, '') + '-单号:' + isnull(a.gte11c, ''), a.gda03c = b.gda03c, a.amt = c.gtb04f, a.valid = 1, a.remainamt = dbo.fun_divide_default(a.gte06f, a.gte04f, 0) * isnull((case gte10c when 'gn' then (select top 1 isnull(gtf05f,1) from gtc05 where gtf00c = d.gti00c and gtf01c = gte11c and gtf02c = gte02c) when 'gz' then (select top 1 isnull(gth05f,1) from gtc06 where gth00c = d.gti00c and gth01c = gte11c and gth02c = gte02c) end), 0) * d.gti09f from #result a, gdm01 b, gtc02 c, gtc09 d where b.gda00c = d.gti00c and a.gte03c = b.gda01c and c.gtb00c = d.gti00c and c.gtb01c = a.gte02c and c.gtb02c = a.gte03c and d.gti10i = a.gte09i and a.type = N'项目' insert into #result(gte00c, gte01c, gte02c, gte03c, gte04f, gte05f, gte06f, gte08c, gte09i, gte10c, gte11c, type, valid) select gtj00c, gtj01c, gtj02c, gtj03c, gtj04f, gtj05f, gtj06f, gtj07c, gtj09i, gtj10c, gtj11c, '产品', (case when (gtj10c is null or gtj11c is null) then 1 else 0 end) from gtc08 a where a.gtj00c = @gte00c and a.gtj01c = @gte01c and isnull(a.gtj12i,0) <> -1 --增加状态判断(0为正常,-1为退货) and ((a.gtj06f > 0 and a.gtj03c = a.gtj07c) or (0 = @onlymain and a.gtj03c <> a.gtj07c)) update a set a.amt = c.gtd04f, a.remainamt = a.gte06f * c.gtd04f from #result a, gtc03 c where c.gtd00c = @gte00c and c.gtd01c = a.gte02c and c.gtd02c = a.gte03c and a.type = N'产品' update a set a.gda03c = b.gfa03c from #result a, gfm01 b where b.gfa00c = @gte00c and a.gte03c = b.gfa01c and a.type = N'产品' update a set a.groupc = (a.gte02c + '-' + case a.gte10c when 'gn' then '会员卡销售' when 'gz' then '账户异动单' end) + '-门店:' + isnull(d.gtj00c, '') + '-单号:' + isnull(a.gte11c, ''), a.gda03c = b.gfa03c, a.amt = c.gtd04f, a.valid = 1, a.remainamt = dbo.fun_divide_default(a.gte06f, a.gte04f, 0) * isnull((case gte10c when 'gn' then (select top 1 isnull(gtf05f,1) from gtc05 where gtf00c = d.gtj00c and gtf01c = a.gte11c and gtf02c = a.gte02c) when 'gz' then (select top 1 isnull(gth05f,1) from gtc06 where gth00c = d.gtj00c and gth01c = a.gte11c and gth02c = a.gte02c) end), 0) * d.gtj09f from #result a, gfm01 b, gtc03 c, gtc10 d where b.gfa00c = d.gtj00c and a.gte03c = b.gfa01c and c.gtd00c = d.gtj00c and c.gtd01c = a.gte02c and c.gtd02c = a.gte03c and d.gtj10i = a.gte09i and a.type = N'产品' select groupc, gte00c, gte01c, gte02c, gte03c, gte04f, gte05f, gte06f, gte08c, gte09i, gte10c, gte11c, gda03c, isnull(amt,0) amt, isnull(remainamt,0) remainamt, [type],sdate,edate from #result where valid = 1 drop table #result end GO /****** Object: StoredProcedure [dbo].[upg_set_version] Script Date: 06/12/2017 06:20:41 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_set_version] ( @compid varchar(10), -- 公司别 @ver int -- 版本; 1-美容版本; 2-美发版本; 3-888版本 ) as begin update modulestruct set name = '会员入店' where program = 'GMA02I' if @ver = 1 -- 美容院顾问版本 begin update modulestruct set version = 'Ver3000' where program = 'GMA02I' or program = 'GSD01I' or program = 'GMA03I' or program = 'GMB01I' or program = 'GMA04I' or program = 'GMA04R' or program = 'GSB12I' or program = 'GIC02I' or program = 'GIC01I' or program = 'GIC04I' or program = 'GMB13R' update gsm01 set gsa02c = '447' where gsa00c = @compid and gsa01c = 'SP029' update sysstruct set version = 'Ver3000' where system = 'g9' update sysstruct set version = 'Ver3000' where system = 'g5' update sysstruct set version = 'Ver3000' where system = 'g7' update gsm01 set gsa02c = '1' where gsa00c = @compid and gsa01c = 'SP056' end else if @ver = 4 -- 美容院顾问版本 + 供求管理 begin update modulestruct set version = 'Ver3000' where program = 'GMA02I' or program = 'GSD01I' or program = 'GMA03I' or program = 'GMB01I' or program = 'GMA04I' or program = 'GMA04R' or program = 'GSB12I' or program = 'GIC02I' or program = 'GIC01I' or program = 'GIC04I' or program = 'GMB13R' update gsm01 set gsa02c = '511' where gsa00c = @compid and gsa01c = 'SP029' update sysstruct set version = 'Ver3000' where system = 'g9' update sysstruct set version = 'Ver3000' where system = 'g5' update sysstruct set version = 'Ver3000' where system = 'g7' update gsm01 set gsa02c = '1' where gsa00c = @compid and gsa01c = 'SP056' end else if @ver = 2 -- 美发豪华版本 begin update modulestruct set version = '' where program = 'GMA02I' or program = 'GSD01I' or program = 'GMA03I' or program = 'GMB01I' or program = 'GMA04I' or program = 'GMA04R' or program = 'GSB12I' or program = 'GIC02I' or program = 'GIC01I' or program = 'GIC04I' update gsm01 set gsa02c = '444' where gsa00c = @compid and gsa01c = 'SP029' update sysstruct set version = '' where system = 'g9' update sysstruct set version = 'Ver3000' where system = 'g5' update sysstruct set version = 'Ver3000' where system = 'g7' --豪华版本,肯定没有出院的,而且也没有统一买单,钥匙牌结账等 update gsm01 set gsa03f = 1 where gsa01c = 'SP031' and gsa00c = @compid update gsm01 set gsa02c = '1' where gsa01c = 'SP033' and gsa00c = @compid update gsm01 set gsa03f = 0 where gsa01c = 'SP034' and gsa00c = @compid update gsm01 set gsa03f = 0 where gsa01c = 'SP039' and gsa00c = @compid update gsm01 set gsa02c = '2' where gsa00c = @compid and gsa01c = 'SP056' end else if @ver = 5 -- 美发豪华版本 + 供求管理 begin update modulestruct set version = '' where program = 'GMA02I' or program = 'GSD01I' or program = 'GMA03I' or program = 'GMB01I' or program = 'GMA04I' or program = 'GMA04R' or program = 'GSB12I' or program = 'GIC02I' or program = 'GIC01I' or program = 'GIC04I' update gsm01 set gsa02c = '508' where gsa00c = @compid and gsa01c = 'SP029' update sysstruct set version = '' where system = 'g9' update sysstruct set version = 'Ver3000' where system = 'g5' update sysstruct set version = 'Ver3000' where system = 'g7' -- 豪华版本,肯定没有出院的,而且也没有统一买单,钥匙牌结账等 update gsm01 set gsa03f = 1 where gsa01c = 'SP031' and gsa00c = @compid update gsm01 set gsa02c = '1' where gsa01c = 'SP033' and gsa00c = @compid update gsm01 set gsa03f = 0 where gsa01c = 'SP034' and gsa00c = @compid update gsm01 set gsa03f = 0 where gsa01c = 'SP039' and gsa00c = @compid update gsm01 set gsa02c = '2' where gsa00c = @compid and gsa01c = 'SP056' end else if @ver = 6 -- 供求管理系统 begin update modulestruct set version = '' where program = 'GMA02I' or program = 'GSD01I' or program = 'GMA03I' or program = 'GMB01I' or program = 'GMA04I' or program = 'GMA04R' or program = 'GSB12I' or program = 'GIC02I' or program = 'GIC01I' or program = 'GIC04I' update gsm01 set gsa02c = '490' where gsa00c = @compid and gsa01c = 'SP029' update sysstruct set version = '' where system = 'g5' update sysstruct set version = '' where system = 'g7' update sysstruct set version = '' where system = 'g9' end else if @ver = 7 -- 足疗版本 begin update modulestruct set version = 'Ver3000' where program = 'GMA02I' or program = 'GIC02I' or program = 'GSD01I' update modulestruct set version = '' where program = 'GMA04I' or program = 'GMA04R' or program = 'GMB02R' or program = 'GSB12I' or program = 'GIC04I' or program = 'GMB13R' or program = 'GIC01I' or program = 'GIC04I' or program = 'GSB12I' or program = 'GMA04R' or program = 'GMB01I' or program = 'GMA03I' or program = 'GMB03R' update gsm01 set gsa02c = '447' where gsa00c = @compid and gsa01c = 'SP029' update gsm01 set gsa02c = '4' where gsa00c = @compid and gsa01c = 'SP056' update sysstruct set version = '' where system = 'g9' update sysstruct set version = 'Ver3000' where system = 'g5' update sysstruct set version = 'Ver3000' where system = 'g7' update modulestruct set name = '开单' where program = 'GMA02I' end else if @ver = 8 -- SPA版本 begin update modulestruct set version = 'Ver3000' where program = 'GMA02I' or program = 'GIC02I' or program = 'GSD01I' update modulestruct set version = '' where program = 'GMA04I' or program = 'GMA04R' or program = 'GMB02R' or program = 'GSB12I' or program = 'GIC04I' or program = 'GMB13R' or program = 'GIC01I' or program = 'GIC04I' or program = 'GSB12I' or program = 'GMA04R' or program = 'GMB01I' or program = 'GMA03I' or program = 'GMB03R' update gsm01 set gsa02c = '447' where gsa00c = @compid and gsa01c = 'SP029' update gsm01 set gsa02c = '3' where gsa00c = @compid and gsa01c = 'SP056' update sysstruct set version = '' where system = 'g9' update sysstruct set version = 'Ver3000' where system = 'g5' update sysstruct set version = 'Ver3000' where system = 'g7' update modulestruct set name = '开单' where program = 'GMA02I' end end GO /****** Object: StoredProcedure [dbo].[upg_get_card_combo_for_consuming] Script Date: 06/12/2017 06:20:05 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_get_card_combo_for_consuming] ( @comp varchar(10), -- 公司编号 @home varchar(10), -- 卡归属公司编号 @card varchar(20) -- 卡号 ) as begin create table #gtc04_gte08c ( gte00c varchar(10) null, -- 公司编号 gte01c varchar(20) null, -- 卡号 gte02c varchar(20) null, -- 套餐编号 gte03c varchar(20) null, -- 项目编号 gte04f float null, -- 项目总次数 gte05f float null, -- 已经使用次数 gte06f float null, -- 剩余次数 gte08c varchar(20) null, -- 主项目 gte09i int null, -- 序号 gte10c varchar(20) null, -- 单据类型 gte11c varchar(50) null, -- 单号 ) insert into #gtc04_gte08c(gte00c, gte01c, gte02c, gte03c, gte04f, gte05f, gte06f, gte08c, gte09i, gte10c, gte11c ) select gte00c, gte01c, gte02c, gte03c, gte04f, gte05f, gte06f, gte08c, gte09i, gte10c, gte11c from gtc04 a where a.gte00c = @home and a.gte01c = @card and isnull(gte12i,0) <> -1 --增加状态判断(0为正常,-1为退货) and gte06f > 0 and gte03c = gte08c create table #gtc04_result ( gte00c varchar(10) null, -- 公司编号 gte01c varchar(20) null, -- 卡号 gte02c varchar(20) null, -- 套餐编号 gte03c varchar(20) null, -- 项目编号 gte04f float null, -- 项目总次数 gte05f float null, -- 已经使用次数 gte06f float null, -- 剩余次数 gte08c varchar(20) null, -- 主项目 gte09i int null, -- 序号 gte10c varchar(20) null, -- 单据类型 gte11c varchar(50) null, -- 单号 gda03c varchar(100) null, -- 项目简称 gtb04f float null, -- 核算金额 bcheck float null -- 核算金额 ) declare @SP217_2 varchar(2) --套餐是否严格控制有效时长 declare @SP217_3 varchar(2) --套餐是否严格控制有效时间范围 select @SP217_2 = gsa02c, @SP217_3 = gsa03f from gsm01 where gsa00c = @comp and gsa01c = 'SP217' if (@SP217_2 = '0' and @SP217_3 = '0') begin insert into #gtc04_result(gte00c, gte01c, gte02c, gte03c, gte04f, gte05f, gte06f, gte08c, gte09i, gte10c, gte11c, bcheck) select gte00c, gte01c, gte02c, gte03c, gte04f, gte05f, gte06f, gte08c, gte09i, gte10c, gte11c,0 from gtc04 a where a.gte00c = @home and a.gte01c = @card and isnull(gte12i,0) <> -1 --增加状态判断(0为正常,-1为退货) and gte06f > 0 and gte03c = gte08c -- select a.gte00c, a.gte01c, a.gte02c, a.gte03c, a.gte04f, a.gte05f, a.gte06f, a.gte08c, a.gte09i, a.gte10c, a.gte11c, 0 -- from gtc04 a inner join #gtc04_gte08c b -- on a.gte00c = b.gte00c -- and a.gte01c = b.gte01c -- and a.gte02c = b.gte02c -- and a.gte08c = b.gte08c -- and isnull(a.gte10c,'') = isnull(b.gte10c,'') -- and isnull(a.gte11c,'') = isnull(b.gte11c,'') -- where a.gte00c = @home -- and a.gte01c = @card -- and isnull(a.gte12i,0) <> -1 --增加状态判断(0为正常,-1为退货) end else begin -- add lucz 判断套餐有效期限 --获取当前日期 declare @curdate varchar(20), @curtime varchar(20) exec upg_getcurrdate @curdate output, @curtime output --2个权限都有,则取第1个 if((@SP217_2 = '1' and @SP217_3 = '0') or (@SP217_2 = '1' and @SP217_3 = '1')) begin insert into #gtc04_result(gte00c, gte01c, gte02c, gte03c, gte04f, gte05f, gte06f, gte08c, gte09i, gte10c, gte11c, bcheck) select a.gte00c, a.gte01c, a.gte02c, a.gte03c, a.gte04f, a.gte05f, a.gte06f, a.gte08c, a.gte09i, a.gte10c, a.gte11c, 0 from (select gte00c,gte01c,gte02c,gte03c,gte04f,gte05f, gte06f, gte08c, gte09i, gte10c, gte11c,gte12i from gtc04 where gte00c = @home and gte01c = @card and isnull(gte15d,'') = '' --日期为空则不作判断(以前数据) union all select gte00c,gte01c,gte02c,gte03c,gte04f,gte05f, gte06f, gte08c, gte09i, gte10c, gte11c,gte12i from (select gte00c,gte01c,gte02c,gte03c,gte04f,gte05f, gte06f, gte08c, gte09i, gte10c, gte11c,gte12i, convert(varchar(10),(case gte14c when '1' then dateadd(year,gte13i,gte15d) --年 when '2' then dateadd(month,gte13i,gte15d) --月 when '3' then dateadd(day,gte13i,gte15d) --日 when '4' then dateadd(month,gte13i * 3 ,gte15d) --季度 end),112) as gte15d from gtc04 where gte00c = @home and gte01c = @card and isnull(gte15d,'') <> '' ) as get04_gte15d where gte15d >= @curdate ) as a inner join #gtc04_gte08c b on a.gte00c = b.gte00c and a.gte01c = b.gte01c and a.gte02c = b.gte02c and a.gte08c = b.gte08c and isnull(a.gte10c,'') = isnull(b.gte10c,'') and isnull(a.gte11c,'') = isnull(b.gte11c,'') where a.gte00c = @home and a.gte01c = @card and isnull(a.gte12i,0) <> -1 --增加状态判断(0为正常,-1为退货) end if(@SP217_2 = '0' and @SP217_3 = '1' ) begin insert into #gtc04_result(gte00c, gte01c, gte02c, gte03c, gte04f, gte05f, gte06f, gte08c, gte09i, gte10c, gte11c, bcheck) select a.gte00c, a.gte01c, a.gte02c, a.gte03c, a.gte04f, a.gte05f, a.gte06f, a.gte08c, a.gte09i, a.gte10c, a.gte11c, 0 from (select gte00c,gte01c,gte02c,gte03c,gte04f,gte05f, gte06f, gte08c, gte09i, gte10c, gte11c,gte12i from gtc04 where gte00c = @home and gte01c = @card and (isnull(gte16c,'') = '' or isnull(gte17c,'') = '') union all select gte00c,gte01c,gte02c,gte03c,gte04f,gte05f, gte06f, gte08c, gte09i, gte10c, gte11c,gte12i from gtc04 where gte00c = @home and gte01c = @card and isnull(gte16c,'') != '' and isnull(gte17c,'') != '' and replace(gte16c,'/','') <= @curdate and replace(gte17c,'/','') >= @curdate ) as a inner join #gtc04_gte08c b on a.gte00c = b.gte00c and a.gte01c = b.gte01c and a.gte02c = b.gte02c and a.gte08c = b.gte08c and isnull(a.gte10c,'') = isnull(b.gte10c,'') and isnull(a.gte11c,'') = isnull(b.gte11c,'') where a.gte00c = @home and a.gte01c = @card and isnull(a.gte12i,0) <> -1 --增加状态判断(0为正常,-1为退货) end end update a set a.gtb04f = c.gtb04f from #gtc04_result a, gtc02 c where c.gtb00c = @comp and c.gtb01c = a.gte02c and c.gtb02c = a.gte03c update a set a.gda03c = b.gda03c from #gtc04_result a, gdm01 b where b.gda00c = @comp and a.gte03c = b.gda01c update a set a.gda03c = b.gda03c, a.gtb04f = dbo.fun_divide_default(isnull((case gte10c when 'gn' then (select top 1 isnull(gtf05f,1) from gtc05 where gtf00c = d.gti00c and gtf01c = gte11c and gtf02c = gte02c) when 'gz' then (select top 1 isnull(gth05f,1) from gtc06 where gth00c = d.gti00c and gth01c = gte11c and gth02c = gte02c) end), 0) * d.gti09f, a.gte04f, 0) from #gtc04_result a, gdm01 b, gtc09 d where b.gda00c = d.gti00c and a.gte03c = b.gda01c and d.gti10i = a.gte09i create table #gtc08_gtj07c ( gtj00c varchar(10) null, -- 公司编号 gtj01c varchar(20) null, -- 卡号 gtj02c varchar(20) null, -- 套餐编号 gtj03c varchar(20) null, -- 产品编号 gtj04f float null, -- 项目总次数 gtj05f float null, -- 已经使用次数 gtj06f float null, -- 剩余次数 gtj07c varchar(20) null, -- 主产品 gtj09i int null, -- 序号 gtj10c varchar(20) null, -- 单据类型 gtj11c varchar(50) null, -- 单号 ) insert into #gtc08_gtj07c(gtj00c, gtj01c, gtj02c, gtj03c, gtj04f, gtj05f, gtj06f, gtj07c, gtj09i, gtj10c, gtj11c) select gtj00c, gtj01c, gtj02c, gtj03c, gtj04f, gtj05f, gtj06f, gtj07c, gtj09i, gtj10c, gtj11c from gtc08 where gtj00c = @home and gtj01c = @card and isnull(gtj12i,0) <> -1 --增加状态判断(0为正常,-1为退货) and gtj03c = gtj07c and gtj06f > 0 create table #gtc08_result ( gtj00c varchar(10) null, -- 公司编号 gtj01c varchar(20) null, -- 卡号 gtj02c varchar(20) null, -- 套餐编号 gtj03c varchar(20) null, -- 产品编号 gtj04f float null, -- 项目总次数 gtj05f float null, -- 已经使用次数 gtj06f float null, -- 剩余次数 gtj07c varchar(20) null, -- 主产品 gtj09i int null, -- 序号 gtj10c varchar(20) null, -- 单据类型 gtj11c varchar(50) null, -- 单号 gfa03c varchar(100) null, -- 产品简称 gtd04f float null, -- 核算金额 bcheck float null -- 核算金额 ) insert into #gtc08_result(gtj00c, gtj01c, gtj02c, gtj03c, gtj04f, gtj05f, gtj06f, gtj07c, gtj09i, gtj10c, gtj11c, bcheck) select a.gtj00c, a.gtj01c, a.gtj02c, a.gtj03c, a.gtj04f, a.gtj05f, a.gtj06f, a.gtj07c, a.gtj09i, a.gtj10c, a.gtj11c, 0 from gtc08 a inner join #gtc08_gtj07c b on a.gtj00c = b.gtj00c and a.gtj01c = b.gtj01c and a.gtj02c = b.gtj02c and a.gtj10c = b.gtj10c and a.gtj11c = b.gtj11c where a.gtj00c = @home and a.gtj01c = @card and a.gtj07c = b.gtj07c and isnull(a.gtj12i,0) <> -1 --增加状态判断(0为正常,-1为退货) -- and (a.gtj06f > 0 or a.gtj03c <> a.gtj07c) update a set a.gtd04f = c.gtd04f from #gtc08_result a, gtc03 c where c.gtd00c = @comp and c.gtd01c = a.gtj02c and c.gtd02c = a.gtj03c update a set a.gfa03c = b.gfa03c from #gtc08_result a, gfm01 b where b.gfa00c = @comp and a.gtj03c = b.gfa01c update a set a.gfa03c = b.gfa03c, a.gtd04f = dbo.fun_divide_default(isnull((case gtj10c when 'gn' then (select top 1 isnull(gtf05f,1) from gtc05 where gtf00c = d.gtj00c and gtf01c = a.gtj11c and gtf02c = a.gtj02c) when 'gz' then (select top 1 isnull(gth05f,1) from gtc06 where gth00c = d.gtj00c and gth01c = a.gtj11c and gth02c = a.gtj02c) end), 0) * d.gtj09f, a.gtj04f, 0) from #gtc08_result a, gfm01 b, gtc10 d where b.gfa00c = d.gtj00c and a.gtj03c = b.gfa01c and d.gtj10i = a.gtj09i select gte00c, gte01c, gte02c, gte03c, gte04f, gte05f, gte06f, gte08c, gte09i, gda03c, gtb04f, bcheck from #gtc04_result drop table #gtc04_result select gtj00c, gtj01c, gtj02c, gtj03c, gtj04f, gtj05f, gtj06f, gtj07c, gtj09i, gfa03c, gtd04f, bcheck from #gtc08_result drop table #gtc08_result drop table #gtc04_gte08c end GO /****** Object: StoredProcedure [dbo].[upg_stock_statistic_costprice] Script Date: 06/12/2017 06:20:45 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_stock_statistic_costprice] ( @compid varchar(10), -- 公司编号 @date varchar(10), -- 日期 @goodsfrom varchar(20), -- 产品编号from @goodsto varchar(20), -- 产品编号to @garagefrom varchar(20), -- 仓库编号from @garageto varchar(20), -- 仓库编号to @returntype int, -- 1-返回所有满足条件的产品,2-返回库存量少的产品 3-返回现库存量,安全存量,低于安全存量, 5-用于库存量统计报表 @quan float, -- 安全库存量 @provider varchar(20), -- 供应商 @kindfrom varchar(20), -- 开始产品类别 @kindto varchar(20), -- 截至产品类别 @sorttype int , -- 排序类别 @display varchar(10) -- 隐藏停用的产品 ) as--wuxm, 2004/07/10 --WUXM modified 2005/08/10 begin declare @incompid varchar(10) select @incompid = @compid declare @includesons float select @includesons = gsa03f from gsm01 with (nolock) where gsa00c = @incompid and gsa01c = 'SP103' create table #in_remain_stock --存放入库的数量(进货单位向标准单位转换) ( id int identity not null, goodsno varchar(20) null, --产品编号 goodsname varchar(80) null, --产品名称 garageno varchar(20) null, --仓库编号 garagename varchar(20) null, --仓库名称 goodstype varchar(40) null, --产品类别 unit varchar(10) null, --单位 quantity float null, --数量 safetyquan float null, --安全存量 maxsafequan float null, --最高安全库存 --add by liyh amt float null, --金额 amt_cost float null, --成本价核算成本 price float null, --标准单价 cost float null, --成本单价 provider varchar(20) null, --供应商 stattype varchar(20) null, --统计类别 goodspec varchar(40) null, --产品规格 cost_price float null, --成本单价 cost_price_amt float null, --成本金额 primary key nonclustered(id) ) create clustered INDEX IDX_in_remain_stock_01 on #in_remain_stock(goodsno, garageno) create table #out_stock -- 存放出库、销货、耗用的数(消耗单位向标准单位转换) ( id int identity not null, goodsno varchar(20) null, --产品编号 goodsname varchar(80) null, --产品名称 garageno varchar(20) null, --仓库编号 garagename varchar(20) null, --仓库名称 unit varchar(10) null, --单位 quantity float null, --数量 primary key nonclustered(id) ) create clustered INDEX IDX_out_stock_01 on #out_stock(goodsno, garageno) if @includesons = 1 begin insert #in_remain_stock(goodsno, goodsname, garageno, garagename, goodstype, unit, quantity, safetyquan, amt) select gfd04c, '', gfd08c, '', '', '', sum(isnull(gfd05f, 0)), --modify by lm 20150627(gfd05f标准单位数量改为入库单位或出库单位数量) 0, sum(isnull(gfd13f, 0)) from gfm04 b with (nolock), gam26 z, gfm01 c with (nolock) where gfd00c = z.gaz02c and z.gaz01c = @incompid and gfd01c = '1' and b.gfd04c = c.gfa01c and c.gfa00c = @incompid and (@display = '*' or c.gfa10c = @display) and b.gfd07c <= @date and ((@garagefrom = '*') or (gfd08c between @garagefrom and @garageto)) and ((@goodsfrom = '*') or (gfd04c between @goodsfrom and @goodsto)) and ((@kindfrom = '*') or (gfa24c between @kindfrom and @kindto)) group by gfd04c, gfd08c insert #out_stock(goodsno, goodsname, garageno, garagename, unit, quantity) select gfd04c, '', gfd08c, '', '', sum(isnull(gfd05f, 0)) --modify by lm 20150627(gfd05f标准单位数量改为入库单位或出库单位数量) from gfm04 b with (nolock), gam26 z, gfm01 c with (nolock) where gfd00c = z.gaz02c and z.gaz01c = @incompid and gfd01c in ('2', '3', '4', '5', '6', '7', '8') and b.gfd04c = c.gfa01c and c.gfa00c = @incompid and (@display = '*' or c.gfa10c = @display) and gfd07c <= @date and ((@garagefrom = '*') or (gfd08c between @garagefrom and @garageto)) and ((@goodsfrom = '*') or (gfd04c between @goodsfrom and @goodsto)) and ((@kindfrom = '*') or (gfa24c between @kindfrom and @kindto)) group by gfd04c, gfd08c end else begin insert #in_remain_stock(goodsno, goodsname, garageno, garagename, goodstype, unit, quantity, safetyquan, amt) select gfd04c, '', gfd08c, '', '', '', sum(isnull(gfd05f, 0)), --modify by lm 20150627(gfd05f标准单位数量改为入库单位或出库单位数量) 0, sum(isnull(gfd13f, 0)) from gfm04 b with (nolock), gfm01 c with (nolock) where gfd00c = @incompid and gfd01c = '1' and b.gfd04c = c.gfa01c and c.gfa00c = @incompid and (@display = '*' or c.gfa10c = @display) and b.gfd07c <= @date and ((@garagefrom = '*') or (gfd08c between @garagefrom and @garageto)) and ((@goodsfrom = '*') or (gfd04c between @goodsfrom and @goodsto)) and ((@kindfrom = '*') or (gfa24c between @kindfrom and @kindto)) group by gfd04c, gfd08c --select * from #in_remain_stock insert #out_stock(goodsno, goodsname, garageno, garagename, unit, quantity) select gfd04c, '', gfd08c, '', '', sum(isnull(gfd05f, 0)) --modify by lm 20150627(gfd05f标准单位数量改为入库单位或出库单位数量) from gfm04 b with (nolock), gfm01 c with (nolock) where gfd00c = @incompid and gfd01c in ('2', '3', '4', '5', '6', '7', '8') and b.gfd04c = c.gfa01c and c.gfa00c = @incompid and (@display = '*' or c.gfa10c = @display) and gfd07c <= @date and ((@garagefrom = '*') or (gfd08c between @garagefrom and @garageto)) and ((@goodsfrom = '*') or (gfd04c between @goodsfrom and @goodsto)) and ((@kindfrom = '*') or (gfa24c between @kindfrom and @kindto)) group by gfd04c, gfd08c end --统计出库(包括销货)的数量更新库存量 update t set t.quantity = t.quantity - a.quantity from #out_stock a, #in_remain_stock t where a.goodsno = t.goodsno and a.garageno = t.garageno --注意可能没有入库的而只有出库的产品 delete a from #out_stock a, #in_remain_stock t where a.goodsno = t.goodsno and a.garageno = t.garageno insert #in_remain_stock(goodsno, goodsname, garageno, garagename, unit, quantity) select goodsno, goodsname, garageno, garagename, unit, 0 - quantity from #out_stock --更新仓库名称 update a set a.garagename = b.gad02c from #in_remain_stock a, gam04 b where a.garageno = b.gad01c and b.gad00c = @compid --更新安全存量,更新产品类别, update a set a.amt = isnull(a.quantity, 0) * isnull(b.gfa11f, 0), a.price = isnull(b.gfa11f, 0), a.goodsname = b.gfa03c, a.goodspec = b.gfa12c, a.safetyquan = isnull(b.gfa18f, 0), a.unit = b.gfa07c, a.goodstype = b.gfa05c, a.provider = b.gfa23c, a.stattype = b.gfa24c, a.maxsafequan = isnull(b.gfa29f, 0) from #in_remain_stock a, gfm01 b where a.goodsno = b.gfa01c and b.gfa00c = @compid and (@display = '*' or b.gfa10c = @display) declare @SP054f float -- 成本计算方式 select @SP054f = gsa03f from gsm01 with(nolock) where gsa00c = @compid and gsa01c = 'SP054' --需要修改成本 if @SP054f = 2 begin update a set a.amt_cost = isnull(a.quantity, 0) * isnull(b.gfh03f, 0), a.cost = isnull(b.gfh03f, 0) from #in_remain_stock a, gfm08 b where a.goodsno = b.gfh01c and b.gfh00c = @compid and b.gfh02c = substring(@date, 1, 6) end else begin update a set a.amt_cost = isnull(a.quantity, 0) * isnull(b.gfa22f, 0), a.cost = isnull(b.gfa22f, 0) from #in_remain_stock a, gfm01 b where a.goodsno = b.gfa01c and b.gfa00c = @compid and (@display = '*' or b.gfa10c = @display) end update a set a.cost_price_amt = isnull(a.quantity, 0) * isnull(b.gfa19f, 0), a.cost_price = isnull(b.gfa19f, 0) from #in_remain_stock a, gfm01 b where a.goodsno = b.gfa01c and b.gfa00c = @compid and (@display = '*' or b.gfa10c = @display) update a set a.unit = b.gsb03c from #in_remain_stock a, gsm02 b where a.unit = b.gsb02c and b.gsb00c = @compid and b.gsb01c = 'K' --判断是否统计已经停止之产品 declare @include_invalid_good varchar(20) select @include_invalid_good = gsa02c from gsm01 where gsa01c = 'SP113' and gsa00c = @compid if @include_invalid_good = '0' begin --删除已经停止之产品 delete a from #in_remain_stock a, gfm01 b where a.goodsno = b.gfa01c and b.gfa00c = @compid and isnull(gfa10c, '0') = '1' and (@display = '*' or b.gfa10c = @display) end --Added by WUXM, 2005/11/02 --用于库存统计报表,原用的是@returntype = 1, 先改为返回数量为一个字串 --因别的函数或SP中用了@returntype = 1的,所以只能新增加类型为5 if @returntype = 5 begin delete #in_remain_stock where isnull(quantity, 0) = 0 end if (isnull(@provider, '') = '' or isnull(@provider, '') = '*') --所有供应商的产品 begin if (@sorttype = 0) begin select d.* from ( select isnull(goodsno,'') goodsno, isnull(goodsname,'') goodsname, isnull(garageno,'') garageno, isnull(garagename,'') garagename, isnull(goodstype,'') goodstype, isnull(price,0) price, isnull(cost,0) cost, isnull(unit,0) unit, quantity = '', isnull(amt_cost,0) amt_cost, isnull(amt,0) amt, isnull(quantity,0) quan, isnull(stattype,'') stattype, isnull(goodspec,'') goodspec, isnull(cost_price_amt,0) cost_price_amt, isnull(cost_price,0) cost_price from #in_remain_stock union select isnull(gfa01c,'') goodsno, isnull(gfa03c,'') goodsname, '' as garageno, '' as garagename, isnull(gfa05c,'') goodstype, 0 as price, isnull(gfa22f,'') cost, isnull(b.gsb03c ,'') unit, 0 as quantity, 0 as amt_cost, 0 as amt, 0 as quan, -- '' as stattype, isnull(gfa24c,'') stattype, isnull(gfa12c,'') goodspec, 0 as cost_price_amt, 0 as cost_price from gfm01 c, gsm02 b where c.gfa20c = b.gsb02c and b.gsb00c = @compid and c.gfa00c = @compid and b.gsb01c = 'K' and isnull(c.gfa10c, '1') <> '1' and ((@goodsfrom = '*') or (gfa01c between @goodsfrom and @goodsto)) and ((gfa23c= @provider) or @provider='*') and ((@kindfrom = '*') or (gfa24c between @kindfrom and @kindto)) and gfa01c not in(select goodsno from #in_remain_stock)) d --modify by limin -2014-12-11 where ((@garagefrom = '*') or (d.garageno between @garagefrom and @garageto)) order by d.goodsno end else if (@sorttype = 1) begin select d.* from ( select isnull(goodsno,'') goodsno, isnull(goodsname,'') goodsname, isnull(garageno,'') garageno, isnull(garagename,'') garagename, isnull(goodstype,'') goodstype, isnull(price,0) price, isnull(cost,0) cost, isnull(unit,0) unit, quantity = '', isnull(amt_cost,0) amt_cost, isnull(amt,0) amt, isnull(quantity,0) quan, isnull(stattype,'') stattype, isnull(goodspec,'') goodspec, isnull(cost_price_amt,0) cost_price_amt, isnull(cost_price,0) cost_price from #in_remain_stock union select isnull(gfa01c,'') goodsno, isnull(gfa03c,'') goodsname, '' as garageno, '' as garagename, isnull(gfa05c,'') goodstype, 0 as price, isnull(gfa22f,'') cost, isnull(b.gsb03c ,'') unit, 0 as quantity, 0 as amt_cost, 0 as amt, 0 as quan, -- '' as stattype, isnull(gfa24c,'') stattype, isnull(gfa12c,'') goodspec, 0 as cost_price_amt, 0 as cost_price from gfm01 c, gsm02 b where c.gfa20c = b.gsb02c and b.gsb00c = @compid and c.gfa00c = @compid and b.gsb01c = 'K' and isnull(c.gfa10c, '1') <> '1' and ((@goodsfrom = '*') or (gfa01c between @goodsfrom and @goodsto)) and ((gfa23c= @provider) or @provider='*') and ((@kindfrom = '*') or (gfa24c between @kindfrom and @kindto)) and gfa01c not in(select goodsno from #in_remain_stock)) d --modify by limin -2014-12-11 where ((@garagefrom = '*') or (d.garageno between @garagefrom and @garageto)) order by d.goodstype end else if (@sorttype = 2) begin select d.* from ( select isnull(goodsno,'') goodsno, isnull(goodsname,'') goodsname, isnull(garageno,'') garageno, isnull(garagename,'') garagename, isnull(goodstype,'') goodstype, isnull(price,0) price, isnull(cost,0) cost, isnull(unit,0) unit, quantity = '', isnull(amt_cost,0) amt_cost, isnull(amt,0) amt, isnull(quantity,0) quan, isnull(stattype,'') stattype, isnull(goodspec,'') goodspec, isnull(cost_price_amt,0) cost_price_amt, isnull(cost_price,0) cost_price from #in_remain_stock union select isnull(gfa01c,'') goodsno, isnull(gfa03c,'') goodsname, '' as garageno, '' as garagename, isnull(gfa05c,'') goodstype, 0 as price, isnull(gfa22f,'') cost, isnull(b.gsb03c ,'') unit, 0 as quantity, 0 as amt_cost, 0 as amt, 0 as quan, -- '' as stattype, isnull(gfa24c,'') stattype, isnull(gfa12c,'') goodspec, 0 as cost_price_amt, 0 as cost_price from gfm01 c, gsm02 b where c.gfa20c = b.gsb02c and b.gsb00c = @compid and c.gfa00c = @compid and b.gsb01c = 'K' and isnull(c.gfa10c, '1') <> '1' and ((@goodsfrom = '*') or (gfa01c between @goodsfrom and @goodsto)) and ((gfa23c= @provider) or @provider='*') and ((@kindfrom = '*') or (gfa24c between @kindfrom and @kindto)) and gfa01c not in(select goodsno from #in_remain_stock)) d --modify by limin -2014-12-11 where ((@garagefrom = '*') or (d.garageno between @garagefrom and @garageto)) order by d.provider end end else --指定供应商的产品 begin if (@sorttype = 0) begin select d.* from ( select isnull(goodsno,'') goodsno, isnull(goodsname,'') goodsname, isnull(garageno,'') garageno, isnull(garagename,'') garagename, isnull(goodstype,'') goodstype, isnull(price,0) price, isnull(cost,0) cost, isnull(unit,0) unit, quantity = '', isnull(amt_cost,0) amt_cost, isnull(amt,0) amt, isnull(quantity,0) quan, isnull(stattype,'') stattype, isnull(goodspec,'') goodspec, isnull(cost_price_amt,0) cost_price_amt, isnull(cost_price,0) cost_price from #in_remain_stock union select isnull(gfa01c,'') goodsno, isnull(gfa03c,'') goodsname, '' as garageno, '' as garagename, isnull(gfa05c,'') goodstype, 0 as price, isnull(gfa22f,'') cost, isnull(b.gsb03c ,'') unit, 0 as quantity, 0 as amt_cost, 0 as amt, 0 as quan, -- '' as stattype, isnull(gfa24c,'') stattype, isnull(gfa12c,'') goodspec, 0 as cost_price_amt, 0 as cost_price from gfm01 c, gsm02 b where c.gfa20c = b.gsb02c and b.gsb00c = @compid and c.gfa00c = @compid and b.gsb01c = 'K' and isnull(c.gfa10c, '1') <> '1' and ((@goodsfrom = '*') or (gfa01c between @goodsfrom and @goodsto)) and ((gfa23c= @provider) or @provider='*') and ((@kindfrom = '*') or (gfa24c between @kindfrom and @kindto)) and gfa01c not in(select goodsno from #in_remain_stock )) d --modify by limin -2014-12-11 where ((@garagefrom = '*') or (d.garageno between @garagefrom and @garageto)) order by d.goodsno end else if (@sorttype = 1) begin select d.* from ( select isnull(goodsno,'') goodsno, isnull(goodsname,'') goodsname, isnull(garageno,'') garageno, isnull(garagename,'') garagename, isnull(goodstype,'') goodstype, isnull(price,0) price, isnull(cost,0) cost, isnull(unit,0) unit, quantity = '', isnull(amt_cost,0) amt_cost, isnull(amt,0) amt, isnull(quantity,0) quan, isnull(stattype,'') stattype, isnull(goodspec,'') goodspec, isnull(cost_price_amt,0) cost_price_amt, isnull(cost_price,0) cost_price from #in_remain_stock union select isnull(gfa01c,'') goodsno, isnull(gfa03c,'') goodsname, '' as garageno, '' as garagename, isnull(gfa05c,'') goodstype, 0 as price, isnull(gfa22f,'') cost, isnull(b.gsb03c ,'') unit, 0 as quantity, 0 as amt_cost, 0 as amt, 0 as quan, -- '' as stattype, isnull(gfa24c,'') stattype, isnull(gfa12c,'') goodspec, 0 as cost_price_amt, 0 as cost_price from gfm01 c, gsm02 b where c.gfa20c = b.gsb02c and b.gsb00c = @compid and c.gfa00c = @compid and b.gsb01c = 'K' and isnull(c.gfa10c, '1') <> '1' and ((@goodsfrom = '*') or (gfa01c between @goodsfrom and @goodsto)) and ((gfa23c= @provider) or @provider='*') and ((@kindfrom = '*') or (gfa24c between @kindfrom and @kindto)) and gfa01c not in(select goodsno from #in_remain_stock )) d --modify by limin -2014-12-11 where ((@garagefrom = '*') or (d.garageno between @garagefrom and @garageto)) order by d.goodstype end else if (@sorttype = 2) begin select d.* from ( select isnull(goodsno,'') goodsno, isnull(goodsname,'') goodsname, isnull(garageno,'') garageno, isnull(garagename,'') garagename, isnull(goodstype,'') goodstype, isnull(price,0) price, isnull(cost,0) cost, isnull(unit,0) unit, quantity = '', isnull(amt_cost,0) amt_cost, isnull(amt,0) amt, isnull(quantity,0) quan, isnull(stattype,'') stattype, isnull(goodspec,'') goodspec, isnull(cost_price_amt,0) cost_price_amt, isnull(cost_price,0) cost_price from #in_remain_stock union select isnull(gfa01c,'') goodsno, isnull(gfa03c,'') goodsname, '' as garageno, '' as garagename, isnull(gfa05c,'') goodstype, 0 as price, isnull(gfa22f,'') cost, isnull(b.gsb03c ,'') unit, 0 as quantity, 0 as amt_cost, 0 as amt, 0 as quan, -- '' as stattype, isnull(gfa24c,'') stattype, isnull(gfa12c,'') goodspec, 0 as cost_price_amt, 0 as cost_price from gfm01 c, gsm02 b where c.gfa20c = b.gsb02c and b.gsb00c = @compid and c.gfa00c = @compid and b.gsb01c = 'K' and isnull(c.gfa10c, '1') <> '1' and ((@goodsfrom = '*') or (gfa01c between @goodsfrom and @goodsto)) and ((gfa23c= @provider) or @provider='*') and ((@kindfrom = '*') or (gfa24c between @kindfrom and @kindto)) and gfa01c not in(select goodsno from #in_remain_stock)) d --modify by limin -2014-12-11 where ((@garagefrom = '*') or (d.garageno between @garagefrom and @garageto)) order by d.provider end end drop table #in_remain_stock drop table #out_stock end GO /****** Object: StoredProcedure [dbo].[upg_personal_statistics_paymode_calc_ex] Script Date: 06/12/2017 06:20:26 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_personal_statistics_paymode_calc_ex] ( @compid varchar(10), -- 公司别 @fromdate varchar(10), -- 开始日期 @todate varchar(10), -- 截至日期 @fromempl varchar(20), -- 查询开始人员 @toempl varchar(20), -- 查询截至人员 @inc_card int, -- 卡销售 @inc_service int, -- 服务 @inc_goods int, -- 是否包括产品销售 @return_type int, @paymode varchar(20), @cardtype varchar(5), @spid int ) -- return 数据的类型, 1-明细数据员工业绩统计报表 -- 2-每人一个提成数据, 用于计算员工薪资 -- 3-业绩排行-虚业绩, 4-业绩排行-营业金额 -- 5-业绩排行-实业绩 -- 7-每人按项目分组 6-总提成 as begin -- 因java请求无法识别 WWQ = #,WWR = $, WWE = & exec upg_paymode_to_java @paymode,@paymode output create table #empl_stat_outer ( id int identity, -- 流水号 person_id varchar(20) not null, -- 员工代码 comp_ascription varchar(10) null, -- 公司别(员工) action_id tinyint not null, -- 类别, 1-卡销售 2-指定大工, 3-轮班大工, 4-补工大工, -- 5-指定中工, 6-轮班中工 -- 7-指定小工, 8-轮班小工, 9-产品销售 10,11-四工 -- 0 卡充值 srvdate varchar(10) not null, -- 日期 code varchar(20) not null, -- 项目代码, 或是卡号, 产品码 name varchar(200) null, -- 名称 payway varchar(20) null, -- 支付方式 amt float null, -- 营业金额 quan float null, -- 数量 cost float null, -- 成本 comm float null, -- 提成 amt2 float null, -- 虚业绩 amt3 float null, -- 真实业绩 cardrate float null, -- 转卡比率 stdprice float null, -- 标准单价 prj_type varchar(20) null, -- 项目类别 share_rate float default 1 null, --分享比率 cls_flag int null, -- 1:项目 2:产品 3:卡 gender int null, -- 性别 0:女 1:男 billid varchar(20) null, -- 单号 paycode varchar(20) null, -- 支付代码 ex_rate float null, -- 实际营业额折算比率 ggm02_seq float null, -- ggm02中的序列号, 与单号一起来标志一个项目 room_id varchar(20) null, -- 房间号,只在spa或足浴版本需要 cardid varchar(20) null, -- 会员卡号 memid varchar(40) null, -- 会员编号 sumMoney float null, -- 会员卡销售总金额 compid varchar(20) null ) create clustered index idx_empl_stat_outer_01 on #empl_stat_outer(person_id, prj_type) -- 子门店业绩数据循环放到upg_personal_statistics中了 insert #empl_stat_outer(person_id, comp_ascription, action_id, srvdate, code, name, payway, amt, quan, cost, comm, amt2, amt3, cardrate, stdprice, prj_type, share_rate, cls_flag, gender, billid, paycode, ex_rate, ggm02_seq, room_id, cardid, memid) exec upg_personal_statistics_calc @compid, @fromdate, @todate, @fromempl, @toempl, @inc_card, @inc_service, @inc_goods, @return_type, @spid declare @sp048 varchar(10) select @sp048 = gsa02c from gsm01 with (nolock) where gsa00c = @compid and gsa01c = 'SP048' if @sp048 <> '1' begin create table #tab_sum ( empid varchar(20), code varchar(20), action_id tinyint, billid varchar(20) null, sumMoney float ) insert into #tab_sum(empid, code, action_id, sumMoney, billid) select person_id, code, action_id, sum(isnull(amt, 0)), billid from #empl_stat_outer where action_id in(1, 0) group by code, person_id, action_id, billid update a set a.sumMoney = b.sumMoney from #empl_stat_outer a, #tab_sum b where a.action_id in(1, 0) and a.code = b.code and a.action_id = b.action_id and empid = person_id and a.billid = b.billid update #empl_stat_outer set quan = (case when sumMoney = 0 then quan else amt / sumMoney end) where action_id in(1, 0) drop table #tab_sum end declare @sql nvarchar(max) set @sql = 'select isnull(id,'''') id, isnull(person_id,'''') person_id, isnull(comp_ascription,'''') comp_ascription, isnull(action_id,'''') action_id, isnull(srvdate,'''') srvdate, isnull(code,'''') code, isnull(name,'''') name, isnull(payway,'''') payway, isnull(amt,'''') amt, isnull(quan,'''') quan, isnull(cost,'''') cost, isnull(comm,'''') comm, isnull(amt2,'''') amt2, isnull(amt3,'''') amt3, isnull(cardrate,'''') cardrate, isnull(stdprice,'''') stdprice, isnull(prj_type,'''') prj_type, isnull(share_rate,'''') share_rate, isnull(cls_flag,'''') cls_flag, isnull(gender,'''') gender, isnull(billid,'''') billid, isnull(paycode,'''') paycode, isnull(ex_rate,'''') ex_rate, isnull(ggm02_seq,'''') ggm02_seq, isnull(room_id,'''') room_id, isnull(cardid,'''') cardid, isnull(memid,'''') memid, isnull(sumMoney,'''') sumMoney, isnull(compid,'''') compid from #empl_stat_outer ' if isnull(@cardtype, '') <> '' and @cardtype <> '*' begin set @sql = @sql + ', gcm01 with (nolock) where isnull(cardid, '''') <> '''' and substring(cardid, 1, 2) <> ''散客'' and gca01c = cardid and gca00c = gca13d and gca02c = ''' + @cardtype + '''' end if isnull(@paymode, '') <> '' and @paymode <> '*' begin if isnull(@cardtype, '') = '' or @cardtype = '*' begin set @sql = @sql + ' where' end else begin set @sql = @sql + ' and' end set @sql = @sql + ' (paycode = ''' + @paymode + ''' or isnull(paycode, '''') = '''')' end set @sql = @sql + ' order by person_id, action_id, srvdate, id ' exec sp_executesql @sql drop table #empl_stat_outer end GO /****** Object: StoredProcedure [dbo].[upg_m_dispatch_gfm01_single] Script Date: 06/12/2017 06:20:19 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_m_dispatch_gfm01_single] ( @fromcompid varchar(10), -- 从哪一个公司发布 @tocompid varchar(10), -- 目的公司 @mandatory varchar(2), -- 是否强制统一 @gfa01c varchar(20) -- 产品编号 ) as begin if @fromcompid = @tocompid return if @mandatory <> '1' and @mandatory <> '0' return if @mandatory = '0' begin if exists (select 1 from gfm01 where gfa00c = @tocompid and gfa01c = @gfa01c) return end declare @gfa22f float select @gfa22f = gfa22f from gfm01 where gfa00c = @tocompid and gfa01c = @gfa01c delete gfm01 where gfa00c = @tocompid and gfa01c = @gfa01c delete gfm05 where gfe00c = @tocompid and gfe02c = @gfa01c declare @sp148 float -----Add by maql 20100723 select @sp148 = gsa03f from gsm01 where gsa00c = @fromcompid and gsa01c = 'SP148' if @sp148 = 1 begin insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa04d, gfa05c, gfa06c, gfa07c, gfa08c, gfa09f, gfa10c, gfa11f, gfa12c, gfa13i, gfa14f, gfa15f, gfa16i, gfa17f, gfa18f, gfa19f, gfa20c, gfa21f, gfa22f, gfa23c, gfa24c, gfa25i, gfa26f, gfa27i, gfa28i, gfa29f, gfa30f, gfa31f, gfa32f, gfa33c, gfa34f, gfa35f, gfa36c, gfa37i, gfa38i, gfa39f, gfa40f, gfa41c, gfa42f, gfa43f, gfa44c, gfa45i, gfa46c, gfa47c, gfa97c, gfa98c, gfa99d, gfa39i, gfa50i, gfa51c, gfa52c, gfa70i, gfa71f) select @tocompid, gfa01c, gfa02c, gfa03c, gfa04d, gfa05c, gfa06c, gfa07c, gfa08c, gfa09f, gfa10c, gfa11f, gfa12c, gfa13i, gfa14f, gfa15f, gfa16i, gfa17f, gfa18f, gfa19f, gfa20c, gfa21f, gfa22f, gfa23c, gfa24c, gfa25i, gfa26f, gfa27i, gfa28i, gfa29f, gfa30f, gfa31f, gfa32f, gfa33c, gfa34f, gfa35f, gfa36c, gfa37i, gfa38i, gfa39f, gfa40f, gfa41c, gfa42f, gfa43f, gfa44c, gfa45i, gfa46c, gfa47c, gfa97c, gfa98c, gfa99d, gfa39i, gfa50i, gfa51c, gfa52c, gfa70i, gfa71f from gfm01 where gfa00c = @fromcompid and gfa01c = @gfa01c end else -- 不拷贝最高存量和安全存量 begin insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa04d, gfa05c, gfa06c, gfa07c, gfa08c, gfa09f, gfa10c, gfa11f, gfa12c, gfa13i, gfa14f, gfa15f, gfa16i, gfa17f, gfa19f, gfa20c, gfa21f, gfa22f, gfa23c, gfa24c, gfa25i, gfa26f, gfa27i, gfa28i, gfa30f, gfa31f, gfa32f, gfa33c, gfa34f, gfa35f, gfa36c, gfa37i, gfa38i, gfa39f, gfa40f, gfa41c, gfa42f, gfa43f, gfa44c, gfa45i, gfa46c, gfa47c, gfa97c, gfa98c, gfa99d, gfa39i, gfa50i, gfa51c, gfa52c, gfa70i, gfa71f) select @tocompid, gfa01c, gfa02c, gfa03c, gfa04d, gfa05c, gfa06c, gfa07c, gfa08c, gfa09f, gfa10c, gfa11f, gfa12c, gfa13i, gfa14f, gfa15f, gfa16i, gfa17f, gfa19f, gfa20c, gfa21f, gfa22f, gfa23c, gfa24c, gfa25i, gfa26f, gfa27i, gfa28i, gfa30f, gfa31f, gfa32f, gfa33c, gfa34f, gfa35f, gfa36c, gfa37i, gfa38i, gfa39f, gfa40f, gfa41c, gfa42f, gfa43f, gfa44c, gfa45i, gfa46c, gfa47c, gfa97c, gfa98c, gfa99d, gfa39i, gfa50i, gfa51c, gfa52c, gfa70i, gfa71f from gfm01 where gfa00c = @fromcompid and gfa01c = @gfa01c end declare @sp207_3 float select @sp207_3 = gsa03f from gsm01 where gsa01c = 'SP207' and gsa00c = @tocompid set @sp207_3 = isnull(@sp207_3, 1) if @sp207_3 <> 1 and @gfa22f is not null begin update gfm01 set gfa22f = @gfa22f where gfa00c = @tocompid and gfa01c = @gfa01c end insert gfm05(gfe00c, gfe01c, gfe02c, gfe03f, gfe04i) select @tocompid, gfe01c, gfe02c, gfe03f, gfe04i from gfm05 a where a.gfe00c = @fromcompid and a.gfe02c = @gfa01c --Add by maql 20100506 declare @curdate varchar(20) declare @curtime varchar(20) exec upg_getcurrdate @curdate output, @curtime output -- 写入sysoperationlog Add by maql 20100506 insert sysoperationlog(userid, program, operation, operationdate, operationtime, origatedate, compid, keyvalue1, keyvalue2, keyvalue3) values('system', 'GSB02I', 'M', @curdate, @curtime, @curdate, @fromcompid, @tocompid, @mandatory, 'upg_m_dispatch_gfm01_single') end GO /****** Object: StoredProcedure [dbo].[upg_stock_detail] Script Date: 06/12/2017 06:20:44 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_stock_detail] ( @compid varchar(10), -- 公司编号 @datefrom varchar(8), -- date from @dateto varchar(8), -- date to @itemno varchar(20), -- 产品编号 @stock varchar(10) -- 仓库 ) as--Jacky, 2004/10/07 begin declare @includesons float select @includesons = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP103' if @datefrom = '' set @datefrom = '00000000' if @dateto = '' set @dateto = '99999999' set @stock = ltrim(rtrim(@stock)) create table #detail ( id int identity not null, itemno varchar(30) not null, -- item no storage varchar(20) not null, -- 仓库 date varchar(8) null, -- date enter float null, -- 入库数量 out float null, -- 出库数量 type varchar(20) null, -- 异动类别 billno varchar(100) null, -- 异动单号 stock float null, -- 库存数量 primary key (id), ) if @includesons = 1 begin insert #detail(itemno, storage, date, enter, out, type, billno ) select gfd04c, gfd08c, gfc03c, enter = case gfc01c when 1 then gfd05f else 0 end, out = case gfc01c when 1 then 0 else gfd05f end, case gfc01c when '1' then '入库' when '2' then '出库' when '3' then '销货' when '5' then '套餐' when '6' then '套餐' when '7' then '欠货领取' when '8' then '配料' else '消耗' end, gfc02c from gfm04 with (nolock), gfm03 with (nolock), gam26 z where gfc00c = gfd00c and gfc01c = gfd01c and gfc02c = gfd02c and gfc00c = z.gaz02c and z.gaz01c = @compid and gfd04c = @itemno and (@stock = '' or @stock = '*' or gfd08c = @stock) order by gfd00c, gfd08c, gfd04c, gfd07c end else begin insert #detail(itemno, storage, date, enter, out, type, billno) select gfd04c, gfd08c, gfc03c, enter = case gfc01c when 1 then gfd05f else 0 end, out = case gfc01c when 1 then 0 else gfd05f end, case gfc01c when '1' then '入库' when '2' then '出库' when '3' then '销货' when '5' then '套餐' when '6' then '套餐' when '7' then '欠货领取' when '8' then '配料' else '消耗' end, gfc02c from gfm04 with (nolock), gfm03 with (nolock) where gfc00c = gfd00c and gfc01c = gfd01c and gfc02c = gfd02c and gfc00c = @compid and gfd04c = @itemno and (@stock = '' or @stock = '*' or gfd08c = @stock) order by gfd00c, gfd08c, gfd04c, gfd07c end update #detail set stock = isnull((select sum(enter) from #detail A where A.id <= #detail.id and A.itemno = #detail.itemno and A.storage = #detail.storage ), 0) update #detail set stock = stock - isnull((select sum(out) from #detail A where A.id <= #detail.id and A.itemno = #detail.itemno and A.storage = #detail.storage ), 0) delete #detail where date < @datefrom or date > @dateto select * from #detail order by storage, date asc drop table #detail end GO /****** Object: StoredProcedure [dbo].[upg_monthduty_report] Script Date: 06/12/2017 06:20:24 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_monthduty_report] ( @compid varchar(10), @fromstaff varchar(20), @tostaff varchar(20), @month varchar(8) ) as begin create table #result ( compid varchar(10) not null, staff varchar(20)not null, no1 varchar(5) null, no2 varchar(5) null, no3 varchar(5) null, no4 varchar(5) null, no5 varchar(5) null, no6 varchar(5) null, no7 varchar(5) null, no8 varchar(5) null, no9 varchar(5) null, no10 varchar(5) null, no11 varchar(5) null, no12 varchar(5) null, no13 varchar(5) null, no14 varchar(5) null, no15 varchar(5) null, no16 varchar(5) null, no17 varchar(5) null, no18 varchar(5) null, no19 varchar(5) null, no20 varchar(5) null, no21 varchar(5) null, no22 varchar(5) null, no23 varchar(5) null, no24 varchar(5) null, no25 varchar(5) null, no26 varchar(5) null, no27 varchar(5) null, no28 varchar(5) null, no29 varchar(5) null, no30 varchar(5) null, no31 varchar(5) null, ) create table #hdm01 ( hda00c varchar(10) null, hda01c varchar(20) null, hda02d varchar(8) null, hda07c varchar(20) null, ) insert into #hdm01(hda00c, hda01c, hda02d, hda07c) select hda00c, hda01c, hda02d, hda07c from hdm01 with(nolock) where hda00c = @compid and (hda01c between @fromstaff and @tostaff or @fromstaff = '*') and @month between substring(hda02d, 1, 6) and substring(hda02d, 1, 6) declare @SP102 float --考勤是否必须排班 0为必须排班 select @SP102 = gsa03f from gsm01 with(nolock) where gsa00c = @compid and gsa01c = 'SP102' if (@SP102 <> 0) begin insert into #result(compid, staff) select distinct haa00c, haa01c from ham01 with(nolock) where haa00c = @compid and isnull(haa27c, '') <> '3' and ( haa01c between @fromstaff and @tostaff or @fromstaff = '*') end else if (@SP102 = 0) begin insert into #result(compid, staff) select distinct gicj00c, gicj01v from gicm10 with(nolock) where gicj00c = @compid and @month between substring(gicj05d, 1, 6) and substring(gicj06d, 1, 6) and ( gicj01v between @fromstaff and @tostaff or @fromstaff = '*') end declare @date varchar(8) set @date = @month + '01' update a set a.no1 = case when isnull(b.hda07c, '') = '' then '5' else b.hda07c end from #result a, #hdm01 b where a.compid = b.hda00c and a.staff = b.hda01c and b.hda02d = @date set @date = @month + '02' update a set a.no2 = case when isnull(b.hda07c, '') = '' then '5' else b.hda07c end from #result a, #hdm01 b where a.compid = b.hda00c and a.staff = b.hda01c and b.hda02d = @date set @date = @month + '03' update a set a.no3 = case when isnull(b.hda07c, '') = '' then '5' else b.hda07c end from #result a, #hdm01 b where a.compid = b.hda00c and a.staff = b.hda01c and b.hda02d = @date set @date = @month + '04' update a set a.no4 = case when isnull(b.hda07c, '') = '' then '5' else b.hda07c end from #result a, #hdm01 b where a.compid = b.hda00c and a.staff = b.hda01c and b.hda02d = @date set @date = @month + '05' update a set a.no5 = case when isnull(b.hda07c, '') = '' then '5' else b.hda07c end from #result a, #hdm01 b where a.compid = b.hda00c and a.staff = b.hda01c and b.hda02d = @date set @date = @month + '06' update a set a.no6 = case when isnull(b.hda07c, '') = '' then '5' else b.hda07c end from #result a, #hdm01 b where a.compid = b.hda00c and a.staff = b.hda01c and b.hda02d = @date set @date = @month + '07' update a set a.no7 = case when isnull(b.hda07c, '') = '' then '5' else b.hda07c end from #result a, #hdm01 b where a.compid = b.hda00c and a.staff = b.hda01c and b.hda02d = @date set @date = @month + '08' update a set a.no8 = case when isnull(b.hda07c, '') = '' then '5' else b.hda07c end from #result a, #hdm01 b where a.compid = b.hda00c and a.staff = b.hda01c and b.hda02d = @date set @date = @month + '09' update a set a.no9 = case when isnull(b.hda07c, '') = '' then '5' else b.hda07c end from #result a, #hdm01 b where a.compid = b.hda00c and a.staff = b.hda01c and b.hda02d = @date set @date = @month + '10' update a set a.no10 = case when isnull(b.hda07c, '') = '' then '5' else b.hda07c end from #result a, #hdm01 b where a.compid = b.hda00c and a.staff = b.hda01c and b.hda02d = @date set @date = @month + '11' update a set a.no11 = case when isnull(b.hda07c, '') = '' then '5' else b.hda07c end from #result a, #hdm01 b where a.compid = b.hda00c and a.staff = b.hda01c and b.hda02d = @date set @date = @month + '12' update a set a.no12 = case when isnull(b.hda07c, '') = '' then '5' else b.hda07c end from #result a, #hdm01 b where a.compid = b.hda00c and a.staff = b.hda01c and b.hda02d = @date set @date = @month + '13' update a set a.no13 = case when isnull(b.hda07c, '') = '' then '5' else b.hda07c end from #result a, #hdm01 b where a.compid = b.hda00c and a.staff = b.hda01c and b.hda02d = @date set @date = @month + '14' update a set a.no14 = case when isnull(b.hda07c, '') = '' then '5' else b.hda07c end from #result a, #hdm01 b where a.compid = b.hda00c and a.staff = b.hda01c and b.hda02d = @date set @date = @month + '15' update a set a.no15 = case when isnull(b.hda07c, '') = '' then '5' else b.hda07c end from #result a, #hdm01 b where a.compid = b.hda00c and a.staff = b.hda01c and b.hda02d = @date set @date = @month + '16' update a set a.no16 = case when isnull(b.hda07c, '') = '' then '5' else b.hda07c end from #result a, #hdm01 b where a.compid = b.hda00c and a.staff = b.hda01c and b.hda02d = @date set @date = @month + '17' update a set a.no17 = case when isnull(b.hda07c, '') = '' then '5' else b.hda07c end from #result a, #hdm01 b where a.compid = b.hda00c and a.staff = b.hda01c and b.hda02d = @date set @date = @month + '18' update a set a.no18 = case when isnull(b.hda07c, '') = '' then '5' else b.hda07c end from #result a, #hdm01 b where a.compid = b.hda00c and a.staff = b.hda01c and b.hda02d = @date set @date = @month + '19' update a set a.no19 = case when isnull(b.hda07c, '') = '' then '5' else b.hda07c end from #result a, #hdm01 b where a.compid = b.hda00c and a.staff = b.hda01c and b.hda02d = @date set @date = @month + '20' update a set a.no20 = case when isnull(b.hda07c, '') = '' then '5' else b.hda07c end from #result a, #hdm01 b where a.compid = b.hda00c and a.staff = b.hda01c and b.hda02d = @date set @date = @month + '21' update a set a.no21 = case when isnull(b.hda07c, '') = '' then '5' else b.hda07c end from #result a, #hdm01 b where a.compid = b.hda00c and a.staff = b.hda01c and b.hda02d = @date set @date = @month + '22' update a set a.no22 = case when isnull(b.hda07c, '') = '' then '5' else b.hda07c end from #result a, #hdm01 b where a.compid = b.hda00c and a.staff = b.hda01c and b.hda02d = @date set @date = @month + '23' update a set a.no23 = case when isnull(b.hda07c, '') = '' then '5' else b.hda07c end from #result a, #hdm01 b where a.compid = b.hda00c and a.staff = b.hda01c and b.hda02d = @date set @date = @month + '24' update a set a.no24 = case when isnull(b.hda07c, '') = '' then '5' else b.hda07c end from #result a, #hdm01 b where a.compid = b.hda00c and a.staff = b.hda01c and b.hda02d = @date set @date = @month + '25' update a set a.no25 = case when isnull(b.hda07c, '') = '' then '5' else b.hda07c end from #result a, #hdm01 b where a.compid = b.hda00c and a.staff = b.hda01c and b.hda02d = @date set @date = @month + '26' update a set a.no26 = case when isnull(b.hda07c, '') = '' then '5' else b.hda07c end from #result a, #hdm01 b where a.compid = b.hda00c and a.staff = b.hda01c and b.hda02d = @date set @date = @month + '27' update a set a.no27 = case when isnull(b.hda07c, '') = '' then '5' else b.hda07c end from #result a, #hdm01 b where a.compid = b.hda00c and a.staff = b.hda01c and b.hda02d = @date set @date = @month + '28' update a set a.no28 = case when isnull(b.hda07c, '') = '' then '5' else b.hda07c end from #result a, #hdm01 b where a.compid = b.hda00c and a.staff = b.hda01c and b.hda02d = @date set @date = @month + '29' update a set a.no29 = case when isnull(b.hda07c, '') = '' then '5' else b.hda07c end from #result a, #hdm01 b where a.compid = b.hda00c and a.staff = b.hda01c and b.hda02d = @date set @date = @month + '30' update a set a.no30 = case when isnull(b.hda07c, '') = '' then '5' else b.hda07c end from #result a, #hdm01 b where a.compid = b.hda00c and a.staff = b.hda01c and b.hda02d = @date set @date = @month + '31' update a set a.no31 = case when isnull(b.hda07c, '') = '' then '5' else b.hda07c end from #result a, #hdm01 b where a.compid = b.hda00c and a.staff = b.hda01c and b.hda02d = @date update #result set staff = haa02c from ham01 with(nolock) where staff = haa01c and haa00c = compid select isnull(compid,'') compid, isnull(staff,'') staff, isnull(no1,'') no1, isnull(no2,'') no2, isnull(no3,'') no3, isnull(no4,'') no4, isnull(no5,'') no5, isnull(no6,'') no6, isnull(no7,'') no7, isnull(no8,'') no8, isnull(no9,'') no9, isnull(no10,'') no10, isnull(no11,'') no11, isnull(no12,'') no12, isnull(no13,'') no13, isnull(no14,'') no14, isnull(no15,'') no15, isnull(no16,'') no16, isnull(no17,'') no17, isnull(no18,'') no18, isnull(no19,'') no19, isnull(no20,'') no20, isnull(no21,'') no21, isnull(no22,'') no22, isnull(no23,'') no23, isnull(no24,'') no24, isnull(no25,'') no25, isnull(no26,'') no26, isnull(no27,'') no27, isnull(no28,'') no28, isnull(no29,'') no29, isnull(no30,'') no30, isnull(no31,'') no31 from #result end GO /****** Object: StoredProcedure [dbo].[upg_day_payment_group_depart] Script Date: 06/12/2017 06:19:56 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_day_payment_group_depart] ( @compid varchar(30), @fromdate varchar(10), -- 开始日期 @todate varchar(10), -- 截至日期 @fromdepart varchar(20), -- 部门 @todepart varchar(20) -- 部门 ) as begin --疗程支付方式 Create table #gcm08_pay ( sn int, paycode varchar(10), payment varchar(60), amt float ) --疗程支付方式分组 insert into #gcm08_pay(sn,paycode,amt) select row_number() over (order by paycode) as sn,paycode,sum(amt) as amt from( select gch17c as paycode, gch18f as amt from gcm08 left join gnm01 on gna00c = gch00c and gna01c = gch11c where gch00c in (select gaz02c from gam26 where gaz01c = @compid) and gch10c = 'gn' and gch02d between @fromdate and @todate and (@fromdepart = '*' or gna81c between @fromdepart and @todepart or gna82c between @fromdepart and @todepart or gna83c between @fromdepart and @todepart or gna100c between @fromdepart and @todepart or gna101c between @fromdepart and @todepart or gna102c between @fromdepart and @todepart or gna103c between @fromdepart and @todepart or gna104c between @fromdepart and @todepart or gna105c between @fromdepart and @todepart or gna106c between @fromdepart and @todepart ) and isnull(gch17c,'') != '' --支付方式 and isnull(gch18f,0) > 0 --支付方式金额 union all select gch19c as paycode, gch20f as amt from gcm08 left join gnm01 on gna00c = gch00c and gna01c = gch11c where gch00c in (select gaz02c from gam26 where gaz01c = @compid) and gch10c = 'gn' and gch02d between @fromdate and @todate and (@fromdepart = '*' or gna81c between @fromdepart and @todepart or gna82c between @fromdepart and @todepart or gna83c between @fromdepart and @todepart or gna100c between @fromdepart and @todepart or gna101c between @fromdepart and @todepart or gna102c between @fromdepart and @todepart or gna103c between @fromdepart and @todepart or gna104c between @fromdepart and @todepart or gna105c between @fromdepart and @todepart or gna106c between @fromdepart and @todepart ) and isnull(gch19c,'') != '' --支付方式 and isnull(gch20f,0) > 0 --支付方式金额 union all select gch21c as paycode, gch22f as amt from gcm08 left join gnm01 on gna00c = gch00c and gna01c = gch11c where gch00c in (select gaz02c from gam26 where gaz01c = @compid) and gch10c = 'gn' and gch02d between @fromdate and @todate and (@fromdepart = '*' or gna81c between @fromdepart and @todepart or gna82c between @fromdepart and @todepart or gna83c between @fromdepart and @todepart or gna100c between @fromdepart and @todepart or gna101c between @fromdepart and @todepart or gna102c between @fromdepart and @todepart or gna103c between @fromdepart and @todepart or gna104c between @fromdepart and @todepart or gna105c between @fromdepart and @todepart or gna106c between @fromdepart and @todepart ) and isnull(gch21c,'') != '' --支付方式 and isnull(gch22f,0) > 0 --支付方式金额 union all select gch17c as paycode, gch18f as amt from gcm08 left join gcm10 on gcl00c = gch00c and gcl01c = gch11c where gch00c in (select gaz02c from gam26 where gaz01c = @compid) and gch10c = 'gz' --充值 and gch02d between @fromdate and @todate and (@fromdepart = '*' or gcl81c between @fromdepart and @todepart or gcl82c between @fromdepart and @todepart or gcl83c between @fromdepart and @todepart or gcl100c between @fromdepart and @todepart or gcl101c between @fromdepart and @todepart or gcl102c between @fromdepart and @todepart or gcl103c between @fromdepart and @todepart or gcl104c between @fromdepart and @todepart or gcl105c between @fromdepart and @todepart or gcl106c between @fromdepart and @todepart ) and isnull(gch17c,'') != '' --支付方式 and isnull(gch18f,0) > 0 --支付方式金额 union all select gch19c as paycode, gch20f as amt from gcm08 left join gcm10 on gcl00c = gch00c and gcl01c = gch11c where gch00c in (select gaz02c from gam26 where gaz01c = @compid) and gch10c = 'gz' --充值 and gch02d between @fromdate and @todate and (@fromdepart = '*' or gcl81c between @fromdepart and @todepart or gcl82c between @fromdepart and @todepart or gcl83c between @fromdepart and @todepart or gcl100c between @fromdepart and @todepart or gcl101c between @fromdepart and @todepart or gcl102c between @fromdepart and @todepart or gcl103c between @fromdepart and @todepart or gcl104c between @fromdepart and @todepart or gcl105c between @fromdepart and @todepart or gcl106c between @fromdepart and @todepart ) and isnull(gch19c,'') != '' --支付方式 and isnull(gch20f,0) > 0 --支付方式金额 union all select gch21c as paycode, gch22f as amt from gcm08 left join gcm10 on gcl00c = gch00c and gcl01c = gch11c where gch00c in (select gaz02c from gam26 where gaz01c = @compid) and gch10c = 'gz' --充值 and gch02d between @fromdate and @todate and (@fromdepart = '*' or gcl81c between @fromdepart and @todepart or gcl82c between @fromdepart and @todepart or gcl83c between @fromdepart and @todepart or gcl100c between @fromdepart and @todepart or gcl101c between @fromdepart and @todepart or gcl102c between @fromdepart and @todepart or gcl103c between @fromdepart and @todepart or gcl104c between @fromdepart and @todepart or gcl105c between @fromdepart and @todepart or gcl106c between @fromdepart and @todepart ) and isnull(gch21c,'') != '' --支付方式 and isnull(gch22f,0) > 0 --支付方式金额 ) as Atable group by paycode --更新支付名称 update a set a.payment = '新增疗程 - '+ b.gsb03c from #gcm08_pay a,gsm02 b where b.gsb00c = @compid and b.gsb01c = '3' and a.paycode = b.gsb02c --储值账户支付方式分组 Create table #gnm02_pay ( sn int, paycode varchar(10), payment varchar(60), amt float ) insert into #gnm02_pay(sn,paycode,amt) select row_number() over (order by paycode) as sn,paycode,sum(amt) as amt from( select gnb15c as paycode,gnb16f as amt from gnm02 b with (nolock), gnm01 a with (nolock) where a.gna00c = b.gnb00c and a.gna01c = b.gnb01c and a.gna80d between @fromdate and @todate and gnb13i = 2 -- 储值账户 and gna00c in (select gaz02c from gam26 where gaz01c = @compid) and isnull(gnb15c,'') != '' and isnull(gnb16f,0) > 0 and (@fromdepart = '*' or gna81c between @fromdepart and @todepart or gna82c between @fromdepart and @todepart or gna83c between @fromdepart and @todepart or gna100c between @fromdepart and @todepart or gna101c between @fromdepart and @todepart or gna102c between @fromdepart and @todepart or gna103c between @fromdepart and @todepart or gna104c between @fromdepart and @todepart or gna105c between @fromdepart and @todepart or gna106c between @fromdepart and @todepart ) union all select gnb17c as paycode,gnb18f as amt from gnm02 b with (nolock), gnm01 a with (nolock) where a.gna00c = b.gnb00c and a.gna01c = b.gnb01c and a.gna80d between @fromdate and @todate and gnb13i = 2 -- 储值账户 and gna00c in (select gaz02c from gam26 where gaz01c = @compid) and isnull(gnb17c,'') != '' and isnull(gnb18f,0) > 0 and (@fromdepart = '*' or gna81c between @fromdepart and @todepart or gna82c between @fromdepart and @todepart or gna83c between @fromdepart and @todepart or gna100c between @fromdepart and @todepart or gna101c between @fromdepart and @todepart or gna102c between @fromdepart and @todepart or gna103c between @fromdepart and @todepart or gna104c between @fromdepart and @todepart or gna105c between @fromdepart and @todepart or gna106c between @fromdepart and @todepart ) union all select gnb19c as paycode,gnb20f as amt from gnm02 b with (nolock), gnm01 a with (nolock) where a.gna00c = b.gnb00c and a.gna01c = b.gnb01c and a.gna80d between @fromdate and @todate and gnb13i = 2 -- 储值账户 and gna00c in (select gaz02c from gam26 where gaz01c = @compid) and isnull(gnb19c,'') != '' and isnull(gnb20f,0) > 0 and (@fromdepart = '*' or gna81c between @fromdepart and @todepart or gna82c between @fromdepart and @todepart or gna83c between @fromdepart and @todepart or gna100c between @fromdepart and @todepart or gna101c between @fromdepart and @todepart or gna102c between @fromdepart and @todepart or gna103c between @fromdepart and @todepart or gna104c between @fromdepart and @todepart or gna105c between @fromdepart and @todepart or gna106c between @fromdepart and @todepart ) union all select gnb21c as paycode,gnb22f as amt from gnm02 b with (nolock), gnm01 a with (nolock) where a.gna00c = b.gnb00c and a.gna01c = b.gnb01c and a.gna80d between @fromdate and @todate and gnb13i = 2 -- 储值账户 and gna00c in (select gaz02c from gam26 where gaz01c = @compid) and isnull(gnb21c,'') != '' and isnull(gnb22f,0) > 0 and (@fromdepart = '*' or gna81c between @fromdepart and @todepart or gna82c between @fromdepart and @todepart or gna83c between @fromdepart and @todepart or gna100c between @fromdepart and @todepart or gna101c between @fromdepart and @todepart or gna102c between @fromdepart and @todepart or gna103c between @fromdepart and @todepart or gna104c between @fromdepart and @todepart or gna105c between @fromdepart and @todepart or gna106c between @fromdepart and @todepart ) union all --充值 select gcl67c as paycode,gcl68f as amt from gcm10 with (nolock) where gcl80d between @fromdate and @todate and gcl04c = '2' -- 储值账户 and gcl00c in (select gaz02c from gam26 where gaz01c = @compid) and gcl06i in (0,6) --0充值 and isnull(gcl67c,'') != '' and isnull(gcl68f,0) > 0 and (@fromdepart = '*' or gcl81c between @fromdepart and @todepart or gcl82c between @fromdepart and @todepart or gcl83c between @fromdepart and @todepart or gcl100c between @fromdepart and @todepart or gcl101c between @fromdepart and @todepart or gcl102c between @fromdepart and @todepart or gcl103c between @fromdepart and @todepart or gcl104c between @fromdepart and @todepart or gcl105c between @fromdepart and @todepart or gcl106c between @fromdepart and @todepart ) union all select gcl69c as paycode,gcl70f as amt from gcm10 with (nolock) where gcl80d between @fromdate and @todate and gcl04c = '2' -- 储值账户 and gcl00c in (select gaz02c from gam26 where gaz01c = @compid) and gcl06i in (0,6) --0充值 and isnull(gcl69c,'') != '' and isnull(gcl70f,0) > 0 and (@fromdepart = '*' or gcl81c between @fromdepart and @todepart or gcl82c between @fromdepart and @todepart or gcl83c between @fromdepart and @todepart or gcl100c between @fromdepart and @todepart or gcl101c between @fromdepart and @todepart or gcl102c between @fromdepart and @todepart or gcl103c between @fromdepart and @todepart or gcl104c between @fromdepart and @todepart or gcl105c between @fromdepart and @todepart or gcl106c between @fromdepart and @todepart ) union all select gcl71c as paycode,gcl72f as amt from gcm10 with (nolock) where gcl80d between @fromdate and @todate and gcl04c = '2' -- 储值账户 and gcl00c in (select gaz02c from gam26 where gaz01c = @compid) and gcl06i in (0,6) --0充值 and isnull(gcl71c,'') != '' and isnull(gcl72f,0) > 0 and (@fromdepart = '*' or gcl81c between @fromdepart and @todepart or gcl82c between @fromdepart and @todepart or gcl83c between @fromdepart and @todepart or gcl100c between @fromdepart and @todepart or gcl101c between @fromdepart and @todepart or gcl102c between @fromdepart and @todepart or gcl103c between @fromdepart and @todepart or gcl104c between @fromdepart and @todepart or gcl105c between @fromdepart and @todepart or gcl106c between @fromdepart and @todepart ) union all select gcl73c as paycode,gcl74f as amt from gcm10 with (nolock) where gcl80d between @fromdate and @todate and gcl04c = '2' -- 储值账户 and gcl00c in (select gaz02c from gam26 where gaz01c = @compid) and gcl06i in (0,6) --0充值 and isnull(gcl73c,'') != '' and isnull(gcl74f,0) > 0 and (@fromdepart = '*' or gcl81c between @fromdepart and @todepart or gcl82c between @fromdepart and @todepart or gcl83c between @fromdepart and @todepart or gcl100c between @fromdepart and @todepart or gcl101c between @fromdepart and @todepart or gcl102c between @fromdepart and @todepart or gcl103c between @fromdepart and @todepart or gcl104c between @fromdepart and @todepart or gcl105c between @fromdepart and @todepart or gcl106c between @fromdepart and @todepart ) union all --竞争转卡 select gsc04c as paycode, gsc05f as amt from gsm03 left join gem01 on gea00c = gsc00c and gea01c = gsc01c and gsc02c = 'ge' where gsc00c in (select gaz02c from gam26 where gaz01c = @compid) and gea02i = 4 and gea03d between @fromdate and @todate and (@fromdepart = '*' or gea81c between @fromdepart and @todepart or gea82c between @fromdepart and @todepart or gea83c between @fromdepart and @todepart or gea84c between @fromdepart and @todepart or gea85c between @fromdepart and @todepart or gea86c between @fromdepart and @todepart or gea87c between @fromdepart and @todepart or gea88c between @fromdepart and @todepart or gea89c between @fromdepart and @todepart or gea90c between @fromdepart and @todepart ) ) as Btable group by paycode --更新支付名称 update a set a.payment = '新增储值 - ' + b.gsb03c from #gnm02_pay a,gsm02 b where b.gsb00c = @compid and b.gsb01c = '3' and a.paycode = b.gsb02c --最后返回的表 Create table #tb_return ( sn int, paycode1 varchar(10), payment1 varchar(60), amt1 varchar(60), paycode2 varchar(10), payment2 varchar(60), amt2 varchar(60) ) declare @count_gcm08 int declare @count_gnm02 int select @count_gcm08= max(sn) from #gcm08_pay select @count_gnm02= max(sn) from #gnm02_pay if(@count_gcm08 >= @count_gnm02) begin insert into #tb_return(sn,paycode1,payment1,amt1) select sn,paycode,payment,amt from #gcm08_pay update a set a.paycode2 = b.paycode, a.payment2 = b.payment, a.amt2 = b.amt from #tb_return a,#gnm02_pay b where a.sn = b.sn end else begin insert into #tb_return(sn,paycode2,payment2,amt2) select sn,paycode,payment,amt from #gnm02_pay update a set a.paycode1 = b.paycode, a.payment1 = b.payment, a.amt1 = b.amt from #tb_return a,#gcm08_pay b where a.sn = b.sn end --0.整数;1.一位;2.二位;3.叁位;4.四位 declare @03f_cnt int select @03f_cnt = isnull(gsa03f,0) from gsm01 where gsa00c = @compid and gsa01c = 'SP005' select sn, isnull(paycode1,'') as paycode1, isnull(payment1,'') as payment1, Round(isnull(amt1,0),@03f_cnt) as amt1, isnull(paycode2,'') as paycode2, isnull(payment2,'') as payment2, Round(isnull(amt2,0),@03f_cnt) as amt2 from #tb_return drop table #gcm08_pay drop table #gnm02_pay drop table #tb_return end GO /****** Object: StoredProcedure [dbo].[upg_refresh_stock_for_ggm05] Script Date: 06/12/2017 06:20:34 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_refresh_stock_for_ggm05] ( @compid varchar(10), -- 公司编号 @date varchar(10), -- 日期 @billno varchar(20), -- 收银单号 @flag int -- 是否复核(1-复核,2-取消复核) ) as--WUXM, 2004/07/10 begin -- 消耗是否直接扣库存 declare @reducedirectly_u float select @reducedirectly_u = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP016' declare @use_stockid varchar(10) select @use_stockid = gsa02c from gsm01 where gsa00c = @compid and gsa01c = 'SP045' select @use_stockid = isnull(@use_stockid, '01') declare @has_goods int if @reducedirectly_u = 1 begin -- 扣除消耗 if @flag = 1 -- 复核 begin if exists(select 1 from ggm05 with (nolock) where gge00c = @compid and gge01c = @billno and isnull(gge18i, 0) <> 1) select @has_goods = 1 else select @has_goods = 0 -- 新增库存日异动的表头档 -- modify by lyj 对于表头档的仓库为系统参数设置的仓库 if (@has_goods = 1) begin delete from gfm03 where gfc00c = @compid and gfc01c = '4' and gfc02c = @billno delete from gfm04 where gfd00c = @compid and gfd01c = '4' and gfd02c = @billno insert gfm03(gfc00c, gfc01c, gfc02c, gfc03c, gfc040c, gfc041c, gfc05c, gfc06i, gfc08i, gfc07c, gfc09t) select gga00c, '4', gga01c, gga02d, '3', @use_stockid, @use_stockid, 1, 1, gga93c, gga04t from ggm01 with (nolock) where gga00c = @compid and gga01c = @billno -- 新增库存日异动的明细档 -- modify by lyj 对于明细档的要具体的仓库了 insert gfm04(gfd00c, gfd01c, gfd02c, gfd03f, gfd04c, gfd05f, gfd06f, gfd07c, gfd08c, gfd11c, gfd12f, gfd13f) select gge00c, '4', gge01c, gge02f, gge03c, isnull(gge05f / isnull(case when isnull(gfa09f, 0) = 0 then 1 else gfa09f end, 1), 0), isnull(gge10f, 0), @date, gge15c = case when isnull(gge15c, '') <> '' then gge15c else @use_stockid end, gge04c, isnull(gge05f, 0), 0 from ggm05 with (nolock), gfm01 where gge00c = @compid and gge01c = @billno and gge00c = gfa00c and gge03c = gfa01c and isnull(gge18i, 0) <> 1 end end else begin -- 取消复核 delete from gfm03 where gfc00c = @compid and gfc01c = '4' and gfc02c = @billno delete from gfm04 where gfd00c = @compid and gfd01c = '4' and gfd02c = @billno end end end GO /****** Object: StoredProcedure [dbo].[upg_get_param] Script Date: 06/12/2017 06:20:08 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_get_param] ( @compid varchar(10) ) as-- zy, 2004/12/09 begin declare @SP001 varchar(20), @SP001f float, @SP002 varchar(20), @SP002f float, @SP003 varchar(20), @SP003f float, @SP004 varchar(20), @SP004f float, @SP005 varchar(20), @SP005f float, @SP006 varchar(20), @SP006f float, @SP007 varchar(20), @SP007f float, @SP008 varchar(20), @SP008f float, @SP009 varchar(20), @SP009f float, @SP010 varchar(20), @SP010f float, @SP011 varchar(20), @SP011f float, @SP012 varchar(120), @SP012f float, @SP013 varchar(20), @SP013f float, @SP014 varchar(20), @SP014f float, @SP015 varchar(20), @SP015f float, @SP016 varchar(20), @SP016f float, @SP017 varchar(20), @SP017f float, @SP018 varchar(20), @SP018f float, @SP019 varchar(20), @SP019f float, @SP020 varchar(20), @SP020f float, @SP021 varchar(20), @SP021f float, @SP022 varchar(20), @SP022f float, @SP023 varchar(20), @SP023f float, @SP024 varchar(20), @SP024f float, @SP025 varchar(20), @SP025f float, @SP026 varchar(20), @SP026f float, @SP027 varchar(20), @SP027f float, @SP028 varchar(20), @SP028f float, @SP029 varchar(20), @SP029f float, @SP030 varchar(20), @SP030f float, @SP031 varchar(20), @SP031f float, @SP032 varchar(20), @SP032f float, @SP033 varchar(20), @SP033f float, @SP034 varchar(20), @SP034f float, @SP035 varchar(20), @SP035f float, @SP036 varchar(20), @SP036f float, @SP037 varchar(20), @SP037f float, @SP038 varchar(20), @SP038f float, @SP039 varchar(20), @SP039f float, @SP040 varchar(20), @SP040f float, @SP041 varchar(80), @SP041f float, @SP042 varchar(20), @SP042f float, @SP043 varchar(20), @SP043f float, @SP044 varchar(20), @SP044f float, @SP045 varchar(20), @SP045f float, @SP046 varchar(20), @SP046f float, @SP047 varchar(20), @SP047f float, @SP048 varchar(20), @SP048f float, @SP049 varchar(20), @SP049f float, @SP050 varchar(20), @SP050f float, @SP051 varchar(20), @SP051f float, @SP052 varchar(20), @SP052f float, @SP053 varchar(20), @SP053f float, @SP054 varchar(20), @SP054f float, @SP055 varchar(20), @SP055f float, @SP056 varchar(20), @SP056f float, @SP057 varchar(20), @SP057f float, @SP058 varchar(20), @SP058f float, @SP059 varchar(20), @SP059f float, @SP060 varchar(20), @SP060f float, @SP061 varchar(20), @SP061f float, @SP062 varchar(20), @SP062f float, @SP063 varchar(20), @SP063f float, @SP064 varchar(20), @SP064f float, @SP065 varchar(20), @SP065f float, @SP066 varchar(20), @SP066f float, @SP067 varchar(20), @SP067f float, @SP068 varchar(20), @SP068f float, @SP069 varchar(20), @SP069f float, @SP070 varchar(20), @SP070f float, @SP071 varchar(20), @SP071f float, @SP072 varchar(20), @SP072f float, @SP073 varchar(20), @SP073f float, @SP074 varchar(20), @SP074f float, @SP075 varchar(20), @SP075f float, @SP076 varchar(20), @SP076f float, @SP077 varchar(20), @SP077f float, @SP078 varchar(20), @SP078f float, @SP079 varchar(20), @SP079f float, @SP101 varchar(20), @SP101f float, @SP102 varchar(20), @SP102f float, @SP103 varchar(20), @SP103f float, @SP104 varchar(20), @SP104f float, @SP105 varchar(20), @SP105f float, @SP106 varchar(20), @SP106f float, @SP107 varchar(20), @SP107f float, @SP108 varchar(20), @SP108f float, @SP109 varchar(20), @SP109f float, @SP110 varchar(20), @SP110f float, @SP111 varchar(20), @SP111f float, @SP112 varchar(20), @SP112f float, @SP113 varchar(20), @SP113f float, @SP114 varchar(20), @SP114f float, @SP115 varchar(20), @SP115f float, @SP116 varchar(20), @SP116f float, @SP130 varchar(160), @SP130f float, @SP131 varchar(160), @SP131f float, @SP132 varchar(160), @SP132f float, @SP133 varchar(160), @SP133f float, @SP134 varchar(160), @SP134f float, @SP135 varchar(160), @SP135f float, @SP136 varchar(160), @SP136f float, @SP137 varchar(160), @SP137f float, @SP138 varchar(160), @SP138f float, @SP139 varchar(160), @SP139f float, @SP140 varchar(160), @SP140f float, --Add by maql 20100412 @SP141 varchar(160), @SP141f float, --Add by maql 20100427 @SP142 varchar(160), @SP142f float, --Add by maql 20100512 @SP143 varchar(160), @SP143f float, --Add by liyh 20100527 @SP144 varchar(160), @SP144f float, --Add by maql 20100602 @SP145 varchar(160), @SP145f float, --Add by maql 20100621 @SP146 varchar(160), @SP146f float, --Add by maql 20100624 @SP147 varchar(160), @SP147f float, --Add by maql 20100722 @SP148 varchar(160), @SP148f float, --Add by maql 20100723 @SP149 varchar(160), @SP149f float, --add by liyh 20101116 @SP200 varchar(160), @SP200f float --add by liyh 20101116 declare @SPD01 varchar(20), @SPD01f float, @SPD02 varchar(20), @SPD02f float, @SPD03 varchar(20), @SPD03f float, @SPD04 varchar(20), @SPD04f float, @SPD05 varchar(20), @SPD05f float, @SPD06 varchar(20), @SPD06f float, @SPD07 varchar(20), @SPD07f float, @SPD08 varchar(20), @SPD08f float, @SPD09 varchar(20), @SPD09f float, @SPD10 varchar(20), @SPD10f float, @SPD11 varchar(20), @SPD11f float, @SPD12 varchar(20), @SPD12f float, @SPD13 varchar(20), @SPD13f float, @SPD14 varchar(20), @SPD14f float, @SPD15 varchar(20), @SPD15f float, @SPD16 varchar(20), @SPD16f float, @SPD17 varchar(20), @SPD17f float, @SPD18 varchar(20), @SPD18f float, @SPD19 varchar(20), @SPD19f float, @SPD20 varchar(20), @SPD20f float, @SPD21 varchar(20), @SPD21f float, @SPD22 varchar(20), @SPD22f float, @SPD23 varchar(20), @SPD23f float, @SPD24 varchar(20), @SPD24f float, @SPD25 varchar(20), @SPD25f float, @SPD26 varchar(20), @SPD26f float, @SPD27 varchar(20), @SPD27f float, @SPD28 varchar(20), @SPD28f float, @SPD29 varchar(20), @SPD29f float, @SPD30 varchar(20), @SPD30f float, @SPD31 varchar(20), @SPD31f float select @SP001 = gsa02c, @SP001f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP001' select @SP002 = gsa02c, @SP002f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP002' select @SP003 = gsa02c, @SP003f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP003' select @SP004 = gsa02c, @SP004f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP004' select @SP005 = gsa02c , @SP005f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP005' select @SP006 = gsa02c , @SP006f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP006' select @SP007 = gsa02c , @SP007f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP007' select @SP008 = gsa02c , @SP008f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP008' select @SP009 = gsa02c , @SP009f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP009' select @SP010 = gsa02c , @SP010f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP010' select @SP011 = gsa02c , @SP011f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP011' select @SP012 = gsa02c , @SP012f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP012' select @SP013 = gsa02c , @SP013f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP013' select @SP014 = gsa02c , @SP014f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP014' select @SP015 = gsa02c , @SP015f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP015' select @SP016 = gsa02c , @SP016f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP016' select @SP017 = gsa02c , @SP017f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP017' select @SP018 = gsa02c , @SP018f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP018' select @SP019 = gsa02c , @SP019f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP019' select @SP020 = gsa02c , @SP020f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP020' select @SP021 = gsa02c , @SP021f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP021' select @SP022 = gsa02c , @SP022f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP022' select @SP023 = gsa02c , @SP023f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP023' select @SP024 = gsa02c , @SP024f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP024' select @SP025 = gsa02c , @SP025f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP025' select @SP026 = gsa02c , @SP026f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP026' select @SP027 = gsa02c , @SP027f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP027' select @SP028 = gsa02c , @SP028f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP028' select @SP029 = gsa02c , @SP029f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP029' select @SP030 = gsa02c , @SP030f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP030' select @SP031 = gsa02c , @SP031f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP031' select @SP032 = gsa02c , @SP032f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP032' select @SP033 = gsa02c , @SP033f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP033' select @SP034 = gsa02c , @SP034f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP034' select @SP035 = gsa02c , @SP035f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP035' select @SP036 = gsa02c , @SP036f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP036' select @SP037 = gsa02c , @SP037f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP037' select @SP038 = gsa02c , @SP038f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP038' select @SP039 = gsa02c , @SP039f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP039' select @SP040 = gsa02c , @SP040f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP040' select @SP041 = gsa02c , @SP041f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP041' select @SP042 = gsa02c , @SP042f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP042' select @SP043 = gsa02c , @SP043f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP043' select @SP044 = gsa02c , @SP044f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP044' select @SP045 = gsa02c , @SP045f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP045' select @SP046 = gsa02c , @SP046f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP046' select @SP047 = gsa02c , @SP047f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP047' select @SP048 = gsa02c , @SP048f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP048' select @SP049 = gsa02c , @SP049f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP049' select @SP050 = gsa02c , @SP050f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP050' select @SP051 = gsa02c , @SP051f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP051' select @SP052 = gsa02c , @SP052f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP052' select @SP053 = gsa02c , @SP053f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP053' select @SP054 = gsa02c , @SP054f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP054' select @SP055 = gsa02c , @SP055f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP055' select @SP056 = gsa02c , @SP056f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP056' select @SP057 = gsa02c , @SP057f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP057' select @SP058 = gsa02c , @SP058f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP058' select @SP059 = gsa02c , @SP059f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP059' select @SP060 = gsa02c , @SP060f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP060' select @SP061 = gsa02c , @SP061f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP061' select @SP062 = gsa02c , @SP062f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP062' select @SP063 = gsa02c , @SP063f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP063' select @SP064 = gsa02c , @SP064f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP064' select @SP065 = gsa02c , @SP065f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP065' select @SP066 = gsa02c , @SP066f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP066' select @SP067 = gsa02c , @SP067f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP067' select @SP068 = gsa02c , @SP068f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP068' select @SP069 = gsa02c , @SP069f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP069' select @SP070 = gsa02c , @SP070f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP070' select @SP071 = gsa02c , @SP071f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP071' select @SP072 = gsa02c , @SP072f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP072' select @SP073 = gsa02c , @SP073f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP073' select @SP074 = gsa02c , @SP074f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP074' select @SP075 = gsa02c , @SP075f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP075' select @SP076 = gsa02c , @SP076f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP076' select @SP077 = gsa02c , @SP077f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP077' select @SP078 = gsa02c , @SP078f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP078' select @SP079 = gsa02c , @SP079f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP079' select @SP101 = gsa02c , @SP101f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP101' select @SP102 = gsa02c , @SP102f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP102' select @SP103 = gsa02c , @SP103f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP103' select @SP104 = gsa02c , @SP104f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP104' select @SP105 = gsa02c , @SP105f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP105' select @SP106 = gsa02c , @SP106f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP106' select @SP107 = gsa02c , @SP107f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP107' select @SP108 = gsa02c , @SP108f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP108' select @SP109 = gsa02c , @SP109f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP109' select @SP110 = gsa02c , @SP110f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP110' select @SP111 = gsa02c , @SP111f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP111' select @SP112 = gsa02c , @SP112f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP112' select @SP113 = gsa02c , @SP113f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP113' select @SP114 = gsa02c , @SP114f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP114' select @SP115 = gsa02c , @SP115f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP115' select @SP116 = gsa02c , @SP116f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP116' select @SP130 = gsa02c , @SP130f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP130' select @SP131 = gsa02c , @SP131f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP131' select @SP132 = gsa02c , @SP132f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP132' select @SP133 = gsa02c , @SP133f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP133' select @SP134 = gsa02c , @SP134f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP134' select @SP135 = gsa02c , @SP135f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP135' select @SP136 = gsa02c , @SP136f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP136' select @SP137 = gsa02c , @SP137f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP137' select @SP138 = gsa02c , @SP138f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP138' select @SP139 = gsa02c , @SP139f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP139' select @SP140 = gsa02c , @SP140f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP140' --Add by maql 20100412 select @SP141 = gsa02c , @SP141f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP141' --Add by maql 20100427 select @SP142 = gsa02c , @SP142f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP142' --Add by maql 20100512 select @SP143 = gsa02c , @SP143f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP143' --Add by liyh 20100527 select @SP144 = gsa02c , @SP144f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP144' --Add by maql 20100602 select @SP145 = gsa02c , @SP145f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP145' --Add by maql 20100621 select @SP146 = gsa02c , @SP146f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP146' --Add by maql 20100624 select @SP147 = gsa02c , @SP147f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP147' --Add by maql 20100722 select @SP148 = gsa02c , @SP148f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP148' --Add by maql 20100723 select @SP149 = gsa02c , @SP149f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP149' --Add by lyh 20111116 select @SP200 = gsa02c , @SP200f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP200' --Add by lyh 20111116 select @SPD01 = gsa02c, @SPD01f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SPD01' select @SPD02 = gsa02c, @SPD02f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SPD02' select @SPD03 = gsa02c, @SPD03f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SPD03' select @SPD04 = gsa02c, @SPD04f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SPD04' select @SPD05 = gsa02c , @SPD05f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SPD05' select @SPD06 = gsa02c , @SPD06f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SPD06' select @SPD07 = gsa02c , @SPD07f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SPD07' select @SPD08 = gsa02c , @SPD08f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SPD08' select @SPD09 = gsa02c , @SPD09f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SPD09' select @SPD10 = gsa02c , @SPD10f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SPD10' select @SPD11 = gsa02c , @SPD11f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SPD11' select @SPD12 = gsa02c , @SPD12f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SPD12' select @SPD13 = gsa02c , @SPD13f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SPD13' select @SPD14 = gsa02c , @SPD14f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SPD14' select @SPD15 = gsa02c , @SPD15f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SPD15' select @SPD16 = gsa02c , @SPD16f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SPD16' select @SPD17 = gsa02c , @SPD17f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SPD17' select @SPD18 = gsa02c , @SPD18f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SPD18' select @SPD19 = gsa02c , @SPD19f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SPD19' select @SPD20 = gsa02c , @SPD20f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SPD20' select @SPD21 = gsa02c , @SPD21f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SPD21' select @SPD22 = gsa02c , @SPD22f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SPD22' select @SPD23 = gsa02c , @SPD23f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SPD23' select @SPD24 = gsa02c , @SPD24f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SPD24' select @SPD25 = gsa02c , @SPD25f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SPD25' select @SPD26 = gsa02c , @SPD26f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SPD26' select @SPD27 = gsa02c , @SPD27f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SPD27' select @SPD28 = gsa02c , @SPD28f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SPD28' select @SPD29 = gsa02c , @SPD29f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SPD29' select @SPD30 = gsa02c , @SPD30f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SPD30' select @SPD31 = gsa02c , @SPD31f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SPD31' select COMP = @compid, SP001 = @SP001, SP001f = @SP001f, SP002 = @SP002, SP002f = @SP002f, SP003 = @SP003, SP003f = @SP003f, SP004 = @SP004, SP004f = @SP004f, SP005 = @SP005, SP005f = @SP005f, SP006 = @SP006, SP006f = @SP006f, SP007 = @SP007, SP007f = @SP007f, SP008 = @SP008, SP008f = @SP008f, SP009 = @SP009, SP009f = @SP009f, SP010 = @SP010, SP010f = @SP010f, SP011 = @SP011, SP011f = @SP011f, SP012 = @SP012, SP012f = @SP012f, SP013 = @SP013, SP013f = @SP013f, SP014 = @SP014, SP014f = @SP014f, SP015 = @SP015, SP015f = @SP015f, SP016 = @SP016, SP016f = @SP016f, SP017 = @SP017, SP017f = @SP017f, SP018 = @SP018, SP018f = @SP018f, SP019 = @SP019, SP019f = @SP019f, SP020 = @SP020, SP020f = @SP020f, SP021 = @SP021, SP021f = @SP021f, SP022 = @SP022, SP022f = @SP022f, SP023 = @SP023, SP023f = @SP023f, SP024 = @SP024, SP024f = @SP024f, SP025 = @SP025, SP025f = @SP025f, SP026 = @SP026, SP026f = @SP026f, SP027 = @SP027, SP027f = @SP027f, SP028 = @SP028, SP028f = @SP028f, SP029 = @SP029, SP029f = @SP029f, SP030 = @SP030, SP030f = @SP030f, SP031 = @SP031, SP031f = @SP031f, SP032 = @SP032, SP032f = @SP032f, SP033 = @SP033, SP033f = @SP033f, SP034 = @SP034, SP034f = @SP034f, SP035 = @SP035, SP035f = @SP035f, SP036 = @SP036, SP036f = @SP036f, SP037 = @SP037, SP037f = @SP037f, SP038 = @SP038, SP038f = @SP038f, SP039 = @SP039, SP039f = @SP039f, SP040 = @SP040, SP040f = @SP040f, SP041 = @SP041, SP041f = @SP041f, SP042 = @SP042, SP042f = @SP042f, SP043 = @SP043, SP043f = @SP043f, SP044 = @SP044, SP044f = @SP044f, SP045 = @SP045, SP045f = @SP045f, SP046 = @SP046, SP046f = @SP046f, SP047 = @SP047, SP047f = @SP047f, SP048 = @SP048, SP048f = @SP048f, SP049 = @SP049, SP049f = @SP049f, SP050 = @SP050, SP050f = @SP050f, SP051 = @SP051, SP051f = @SP051f, SP052 = @SP052, SP052f = @SP052f, SP053 = @SP053, SP053f = @SP053f, SP054 = @SP054, SP054f = @SP054f, SP055 = @SP055, SP055f = @SP055f, SP056 = @SP056, SP056f = @SP056f, SP057 = @SP057, SP057f = @SP057f, SP058 = @SP058, SP058f = @SP058f, SP059 = @SP059, SP059f = @SP059f, SP060 = @SP060, SP060f = @SP060f, SP061 = @SP061, SP061f = @SP061f, SP062 = @SP062, SP062f = @SP062f, SP063 = @SP063, SP063f = @SP063f, SP064 = @SP064, SP064f = @SP064f, SP065 = @SP065, SP065f = @SP065f, SP066 = @SP066, SP066f = @SP066f, SP067 = @SP067, SP067f = @SP067f, SP068 = @SP068, SP068f = @SP068f, SP069 = @SP069, SP069f = @SP069f, SP070 = @SP070, SP070f = @SP070f, SP071 = @SP071, SP071f = @SP071f, SP072 = @SP072, SP072f = @SP072f, SP073 = @SP073, SP073f = @SP073f, SP074 = @SP074, SP074f = @SP074f, SP075 = @SP075, SP075f = @SP075f, SP076 = @SP076, SP076f = @SP076f, SP077 = @SP077, SP077f = @SP077f, SP078 = @SP078, SP078f = @SP078f, SP079 = @SP079, SP079f = @SP079f, SP101 = @SP101, SP101f = @SP101f, SP102 = @SP102, SP102f = @SP102f, SP103 = @SP103, SP103f = @SP103f, SP104 = @SP104, SP104f = @SP104f, SP105 = @SP105, SP105f = @SP105f, SP106 = @SP106, SP106f = @SP106f, SP107 = @SP107, SP107f = @SP107f, SP108 = @SP108, SP108f = @SP108f, SP109 = @SP109, SP109f = @SP109f, SP110 = @SP110, SP110f = @SP110f, SP111 = @SP111, SP111f = @SP111f, SP112 = @SP112, SP112f = @SP112f, SP113 = @SP113, SP113f = @SP113f, SP114 = @SP114, SP114f = @SP114f, SP115 = @SP115, SP115f = @SP115f, SP116 = @SP116, SP116f = @SP116f, SP130 = @SP130, SP130f = @SP130f, SP131 = @SP131, SP131f = @SP131f, SP132 = @SP132, SP132f = @SP132f, SP133 = @SP133, SP133f = @SP133f, SP134 = @SP134, SP134f = @SP134f, SP135 = @SP135, SP135f = @SP135f, SP136 = @SP136, SP136f = @SP136f, SP137 = @SP137, SP137f = @SP137f, SP138 = @SP138, SP138f = @SP138f, SP139 = @SP139, SP139f = @SP139f, SP140 = @SP140, SP140f = @SP140f, SP141 = @SP141, SP141f = @SP141f, SP142 = @SP142, SP142f = @SP142f, SP143 = @SP143, SP143f = @SP143f, SP144 = @SP144, SP144f = @SP144f, SP145 = @SP145, SP145f = @SP145f, SP146 = @SP146, SP146f = @SP146f, SP147 = @SP147, SP147f = @SP147f, SP148 = @SP148, SP148f = @SP148f, SP149 = @SP149, SP149f = @SP149f, SP200 = @SP200, SP200f = @SP200f, SPD01 = @SPD01, SPD01f = @SPD01f, SPD02 = @SPD02, SPD02f = @SPD02f, SPD03 = @SPD03, SPD03f = @SPD03f, SPD04 = @SPD04, SPD04f = @SPD04f, SPD05 = @SPD05, SPD05f = @SPD05f, SPD06 = @SPD06, SPD06f = @SPD06f, SPD07 = @SPD07, SPD07f = @SPD07f, SPD08 = @SPD08, SPD08f = @SPD08f, SPD09 = @SPD09, SPD09f = @SPD09f, SPD10 = @SPD10, SPD10f = @SPD10f, SPD11 = @SPD11, SPD11f = @SPD11f, SPD12 = @SPD12, SPD12f = @SPD12f, SPD13 = @SPD13, SPD13f = @SPD13f, SPD14 = @SPD14, SPD14f = @SPD14f, SPD15 = @SPD15, SPD15f = @SPD15f, SPD16 = @SPD16, SPD16f = @SPD16f, SPD17 = @SPD17, SPD17f = @SPD17f, SPD18 = @SPD18, SPD18f = @SPD18f, SPD19 = @SPD19, SPD19f = @SPD19f, SPD20 = @SPD20, SPD20f = @SPD20f, SPD21 = @SPD21, SPD21f = @SPD21f, SPD22 = @SPD22, SPD22f = @SPD22f, SPD23 = @SPD23, SPD23f = @SPD23f, SPD24 = @SPD24, SPD24f = @SPD24f, SPD25 = @SPD25, SPD25f = @SPD25f, SPD26 = @SPD26, SPD26f = @SPD26f, SPD27 = @SPD27, SPD27f = @SPD27f, SPD28 = @SPD28, SPD28f = @SPD28f, SPD29 = @SPD29, SPD29f = @SPD29f, SPD30 = @SPD30, SPD30f = @SPD30f, SPD31 = @SPD31, SPD31f = @SPD31f return 1 end GO /****** Object: StoredProcedure [dbo].[upg_update_ggm01_date] Script Date: 06/12/2017 06:20:49 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_update_ggm01_date] ( @compid varchar(10), --公司编号 @consumeid varchar(20) --消费单号 ) as --WUXM 2005/08/15 begin declare @begtime varchar(20) -- 工作日开始时间 declare @endtime varchar(20) -- 工作日结束时间,表示凌晨时间,如果为000000,则表示帐务日期和实际日期一致 select @begtime = gsa02c from gsm01 where gsa00c = @compid and gsa01c = 'SP050' select @endtime = gsa02c from gsm01 where gsa00c = @compid and gsa01c = 'SP051' declare @staff varchar(20) --登陆人 declare @curtime varchar(8) declare @stcurtime varchar(8) --消费开始时间 declare @curdate varchar(10) declare @cardid varchar(20) -- 会员卡号 select @stcurtime = gga03t, --消费开始时间 @curtime = gga04t, --消费结束时间 @curdate = gga02d, --消费日期 @staff = gga93c, @cardid = gga09c from ggm01 with(nolock) where gga00c = @compid and gga01c = @consumeid declare @time varchar(6) -- 实际结帐时间 declare @date varchar(8) -- 实际日期 select @time = @curtime select @date = @curdate declare @acctdate varchar(20) -- 账户日期 declare @newdate varchar(8) declare @strdate varchar(10) -- 帐务日期比正常日期短 if (@endtime > @begtime and @time > @endtime) begin select @strdate = convert(varchar(10), dateadd(day, 1, cast(@date as datetime)), 120) select @newdate = substring(@strdate, 1, 4) + substring(@strdate, 6, 2) + substring(@strdate, 9, 2) update a set a.gga80d = @newdate from ggm01 a where a.gga00c = @compid and a.gga01c = @consumeid select @acctdate = @newdate end -- 帐务日期比正常日期长 else if (@endtime <= @begtime and @time <= @endtime) begin select @strdate = convert(varchar(10), dateadd(day, -1, cast(@date as datetime)), 120) -- -- --判断消费结束时间是否跨天,则消费日期为次日--modify lm 20150930 -- if(@time < @stcurtime) -- begin -- select @strdate = convert(varchar(10), dateadd(day, 1, cast(@strdate as datetime)), 120) -- end select @newdate = substring(@strdate, 1, 4) + substring(@strdate, 6, 2) + substring(@strdate, 9, 2) update a set a.gga80d = @newdate from ggm01 a where a.gga00c = @compid and a.gga01c = @consumeid select @acctdate = @newdate end -- 帐务日期和正常日期一致 else begin update a set a.gga80d = @date from ggm01 a where a.gga00c = @compid and a.gga01c = @consumeid select @acctdate = @date end -- 更新gsm03新增加的字段 update a set a.gsc10d = @acctdate, a.gsc11c = @staff, a.gsc08c = @cardid from gsm03 a where gsc00c = @compid and gsc01c = @consumeid and gsc02c = 'gx' end GO /****** Object: StoredProcedure [dbo].[upg_update_goods_deposit] Script Date: 06/12/2017 06:20:49 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_update_goods_deposit] ( @compid varchar(20), @type int, -- 1-寄存 2-领取 3-欠货 @billno varchar(20) -- 寄存单号 ) as begin declare @memberid nvarchar(20) declare @goodsid nvarchar(50) declare @num float declare @unit nvarchar(10) declare @gin01c nvarchar(20) --循环中的寄存单号 declare @flag float declare @index float declare @id int declare @gim11c nvarchar(1) declare @gim12c nvarchar(20) declare @price float declare @gin03f float if @type = 1 or @type = 3 --1-寄存 --3-欠货 begin --循环gim21 declare cur_each_gim21_item cursor for select gim02c, gin02c, gin03f, gin07f, gin08c, gin01c, gim11c, gim12c, id from gim20 inner join gim21 on gim00c = gin00c and gim01c = gin01c where gim00c = @compid and gim01c = @billno for update of gin03f open cur_each_gim21_item fetch cur_each_gim21_item into @memberid, @goodsid, @gin03f, @num, @unit, @gin01c, @gim11c, @gim12c, @id while @@fetch_status = 0 begin if @gim11c = '0' begin select @price = (case when isnull(ggc05f, 0) = 0 then 0 else ggc11f / ggc05f end) from ggm03 where ggc00c = @compid and ggc01c = @gim12c and ggc03c = @goodsid and ggc02f = @gin03f end else begin select @price = gfa11f from gfm01 with(nolock) where gfa00c = @compid and gfa01c = @goodsid end if @type = 1 --1-寄存 begin insert gim22(gio00c, gio01c, gio02c, gio05c, gio06f, gio07f, gio08c, gio09f, gio10f, gio11c, gio12c, gio13c, gio14c, gio15c, gio16c, did) values(@compid, @memberid, @billno, @goodsid, @gin03f, @num, @unit, 0, @num, '1', '', convert(nvarchar(18), @price), '', '', '', @id) end else if @type = 3 --3-欠货 begin insert gim22(gio00c, gio01c, gio02c, gio05c, gio06f, gio07f, gio08c, gio09f, gio10f, gio11c, gio12c, gio13c, gio14c, gio15c, gio16c, did) values(@compid, @memberid, @billno, @goodsid, @gin03f, @num, @unit, 0, @num, '2', '', convert(nvarchar(18), @price), '', '', '', @id) end fetch cur_each_gim21_item into @memberid, @goodsid, @gin03f, @num, @unit, @gin01c, @gim11c, @gim12c, @id end close cur_each_gim21_item deallocate cur_each_gim21_item end else if @type = 2 --2-领取 begin --循环gim21 declare @gim09d nvarchar(8) -- 日期 declare @gim10t nvarchar(8) -- 时间 declare @gim07c nvarchar(20) -- 操作人员 declare @gin06c nvarchar(20) -- 负责人员 declare @take_from_id int declare cur_each_gim21_item cursor for select gim02c, gin02c, gin07f, gin08c, gin03f, gim09d, gim10t, gim07c, gin06c, take_from_id from gim20 inner join gim21 on gim00c = gin00c and gim01c = gin01c left join gfm01 on gfa00c = gin00c and gfa01c = gin02c where gim00c = @compid and gim01c = @billno for update of gin09c, gin10c open cur_each_gim21_item fetch cur_each_gim21_item into @memberid, @goodsid, @num, @unit, @flag, @gim09d, @gim10t, @gim07c, @gin06c, @take_from_id while @@fetch_status = 0 begin declare @gio11c nvarchar(1) -- 1-寄存 2-欠货 declare @RP012 float -- 产品领取是否扣库存 select @RP012 = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'RP012' declare @SP044 nvarchar(2) -- 所扣仓库 select @SP044 = isnull(gsa02c, '01') from gsm01 where gsa00c = @compid and gsa01c = 'SP044'--默认总仓库 -- @price 此价格为收银时的金额/数量 或产品标准价 select @gio11c = gio11c, @price = convert(decimal(18, 2), gio13c) from gim22 where gio00c = @compid and gio01c = @memberid and gio05c = @goodsid and gio06f = @flag update gim22 set gio09f = isnull(gio09f, 0) + @num, gio10f = isnull(gio10f, 0) - @num where gio00c = @compid and gio01c = @memberid and gio05c = @goodsid and gio06f = @flag and did = @take_from_id --如果是欠货领取产品,判断是否需要扣库存 update gim21 set gin09c = convert(nvarchar(1), @RP012), gin10c = @SP044 where current of cur_each_gim21_item if @gio11c = 2 and @RP012 = 1 begin if not exists(select 1 from gfm03 where gfc00c = @compid and gfc01c = '7' and gfc02c = @billno) begin insert gfm03(gfc00c, gfc01c, gfc02c, gfc03c, gfc040c, gfc041c, gfc05c, gfc06i, gfc07c, gfc08i, gfc09t) values(@compid, '7', @billno, @gim09d, '3', @SP044, @SP044, 1, @gim07c, 1, @gim10t) end insert gfm04(gfd00c, gfd01c, gfd02c, gfd03f, gfd04c, gfd05f, gfd06f, gfd07c, gfd08c, gfd11c, gfd12f, gfd13f, gfd14i, gfd15c) values(@compid, '7', @billno, @take_from_id, @goodsid, @num, @price, @gim09d, @SP044, @unit, @num, @num*@price, 1, @gin06c) end --领取需要增加一条异动历史 add lucz 2015.11.12 -- insert gfm04(gfd00c, gfd01c, gfd02c, gfd03f, gfd04c, gfd05f, gfd06f, gfd07c, gfd08c, gfd11c, gfd12f, gfd13f, gfd14i, gfd15c) -- values(@compid, '3', @billno, @take_from_id, @goodsid, @num, @price, @gim09d, @SP044, @unit, @num, @num*@price, 1, @gin06c) fetch cur_each_gim21_item into @memberid, @goodsid, @num, @unit, @flag, @gim09d, @gim10t, @gim07c, @gin06c, @take_from_id end close cur_each_gim21_item deallocate cur_each_gim21_item end end GO /****** Object: StoredProcedure [dbo].[upg_repair_gym_param] Script Date: 06/12/2017 06:20:36 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_repair_gym_param](@compid varchar(10)) as--WUXM, 2004/11/14 begin --delete gsm01 if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP001') begin insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values(@compid, 'SP001', '', 2, '2开始批量删除 3:2.YY/M/D 3.Y/M/D 4.M/D/YY 5.M/D/Y6.D/M/YY 7.D/M/Y') end if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP002') begin insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values(@compid, 'SP002', 'SHBOKA', 7, 'IC店标志码;开卡有效期 天') end if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP003') begin insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values(@compid, 'SP003', 'RMB', 0, '结算币别3是否用条码1') end if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP004') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP004', '1', 0, '2会员卡变更是否累加13金额小数位 0.整数;1.一位;2.二位;3.叁位;4.四位 ') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP005') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP005', '0', 0, '2-消费流程中是否需要入院过程 3-数量小数位 0.整数;1.一位;2.二位;3.叁位;4.四位 ') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP006') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP006', '', 2, '开单默认男女, 百分率小数位 0.整数;1.一位;2.二位;3.叁位;4.四位 ') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP007') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP007', '1', 1, '2产品销售负责业务1, 2项目大工3金额 1.后合计2-后四舍五入') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP008') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP008', '', 0, '2默认充值员工号3保存收银单直接打印吗') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP009') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP009', '0', 1, '2是否启用员工与项目关联3是否记忆卡密码') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP010') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP010', '0', 30, '2是否用正点会员入院;可以取消入院的最小时间(分钟)') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP011') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP011', '0', 2, '2服务时间顺延多少单价小数位 0.整数;1.一位;2.二位;3.叁位;4.四位 ') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP012') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP012', '0', 0, '是否启用扣除客户短信通知费用;严格控制有效日期') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP013') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP013', '', 1, '充值默认支付方式;是否自动读卡考勤') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP014') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP014', '', 0, '2是否允许负库存3产品销售是否直接更新库存 1-更新, 0 - 不更新') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP015') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP015', '180000', 0, '预约结束时间;是否IC卡记客户密码') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP016') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP016', '0', 1, '2启用项目照片3产品消耗是否直接扣库存') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP017') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP017', '0', 1, '2启用产品照片3退卡时, 充值余额是否退还 0 -不退还 1- 退还') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP018') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP018', '0', 1, '2启用员工照片3疗程金额是否可见') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP019') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP019', '20', 3500, '产品名称长度; 个税起征点(国内)') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP020') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP020', '20', 1, '项目名称长度;升级卡是否需要更改卡号') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP021') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP021', '', 0, '会员卡确省(查询,消费)密码;前台可见资料的天数') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP022') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP022', '0', 5, '短信月费用; 会员卡到期提前通知的天数(-1不检查)') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP023') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP023', '1', 0, '允许门店查看所有消费;是否允许各消费点支付 0- 不允许 1- 允许') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP024') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP024', '0', 1, '屏幕保护时间, 是否启用疗程功能') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP025') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP025', ', $M + !', 10, '指纹对比参数') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP026') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP026', '0', 5, '卡销售是否自动寻找对应会员;计次卡剩余()次提示客户') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP027') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP027', '2', 0, '产品单价小数:产品数量小数') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP028') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP028', '0', 50, '产品金额小数位, 储值卡剩余()金额提示客户') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP029') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP029', '511', 17.040317, '版本标志') --gsa02c:1-美发版本 9-美容高级版本 gsa03f:服务器端版本号 if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP030') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP030', '0', 5, '2是否平分分享比率1是3会员卡序号长度') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP031') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP031', '', 1, '服务器注册;收银方式 1:服务时收银 2:统一出院收银') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP032') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP032', '1', 200, '1.启动来电会员2显示3最大钥匙牌数') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP033') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP033', '1', 0, '21-结账用卡,2-结账用钥匙;30-分享比率比率必须为1') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP034') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP034', '0', 0, '2客显设备;是否需要会员出院,0-不需要,1-需要') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP035') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP035', '1', 0, '1: 一段; 2:分段提成 方式 0-分别提成,1-累计提成, -2每个员工, 3-分项目类别') --0:按项目单独算提成 --1:按卡/项目/产品累计业绩算提成 --2:按员工项目设定算提成 --3:按项目类别累计业绩算提成 --4:按员工累计总业绩算提成 --5:提成是输入的, 应付专案的,界面不可见 if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP036') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP036', '1', 1, '2业务互相不可见, 是否自动建立客户及开卡3') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP037') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP037', '4', 0, '2收银支持的员工数3积分小数位数') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP038') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP038', '30', 1, '预约间隔;低于安全余额是否允许结账') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP039') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP039', '1', 0, '会员卡号强制与会员编号一致;散客是否需要会员出院,0-不需要,1-需要') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP040') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP040', '1', 0, 'IC卡 0:不用IC卡, 1:同步操作IC卡, 2:分开操作IC卡') -- SP040-3 : 0非IC卡 1IC卡同步 2IC卡异步 3非IC卡异步 -- SP040-2 : 2顶航简单IC卡设备 3恒宇IC卡 4感应卡设备 5新500感应卡设备 8银行POS机 if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP041') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP041', '博卡技术支持021-64608413', 0, '2收银单口号30-可以不输入大工, 1-必须输入大工') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP042') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP042', '2', 0.05, '介绍客户入哪个账户, 赠送比率') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP043') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP043', '', 0, '没有销卡历史收银;0-各公司会员独立,1-统一会员 2-与总部同步') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP044') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP044', '01', 0, '产品销售所扣仓库;账户单直接打印') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP045') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP045', '01', 0, '产品消耗所扣仓库;销卡直接打印') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP046') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP046', '1', 5, '迟到或早退每1分钟扣3') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP047') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP047', '0', 50, '收银单号是否同入院单号强制一致;事假每天扣3') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP048') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP048', '0', 50, '2自己输入卡业绩;病假每天扣3') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP049') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP049', '', 30, '迟到或早退3分钟算作旷工') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP050') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP050', '050000', 0, '2工作日开始时间3ic卡为主') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP051') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP051', '000000', 0, '2工作日结束时间3产品销售人员必须输入') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP052') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP052', '0', 0, '2是否自动新增, 3打折输入原因 0-不需要, 1-可以输入, 2-必须输入') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP053') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP053', '', 0, '2指定或轮班3支付方式为经理签单,是否需要经理确认, 0-不需要, 1-需要') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP054') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP054', '0', 0, '2是否启用找零功能3产品进价计算方式 0-系统计算1-手工输入 2-月加权') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP055') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP055', '', 0, '2是否default员工3收银模式 0-按项目支付1-按单据支付') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP056') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP056', '', 0, '店类型 1美容2美发3SPA4足浴6专柜;收银产品支付方式 0-现金支付1-卡支付') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP057') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP057', '4', 0, '2疗程金额存放的账户3银行卡支付的折算率') --产品出库价默认本来是2, 现在调整为3, 2留给赠送对应的支付方式 if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP058') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP058', '$', 0, '赠送对应的支付方式;3产品出库价默认1进货价2销售成本3标准价4标准价(员)销售成本(门)') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP059') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP059', '', 0, '2短信所扣账户;3是否启用指纹设备') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP060') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP060', '1', 0, '2产品入库1进货成本2标准价3销售成本3自动复合总部出库对应入库1不自动2') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP061') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP061', '0', 1000, '2直接用指纹当会员卡3积分超过多少提醒') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP062') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP062', '0', 5, '支付方式3记入收入;控制多少个店才隔天产生帐') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP063') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP063', '0', 0, '支付方式$记入收入;提成是否扣领用消耗品成本') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP064') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP064', '0', 5, '短信注册号;预约提前通知分钟') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP065') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP065', '0', 0, '短信密码;启用批次管理') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP066') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP066', '1', 0, '卡销售是否必须输入密码;入院只看到未结帐') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP067') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP067', '0', 0, '2购买产品分享比率是否必须为13是否自动打印寄存单') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP068') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP068', '0', 0, '离职提前提醒时间, 预约提前提醒时间') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP069') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP069', '0', 0, '启用入院直接开单;疗程剩余次数提醒') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP070') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP070', '112233', 0, '会员通用密码, 未购买卡客户多少天后自动删除') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP071') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP071', '10', 0, '介绍人佣金给的方式, 是否支出需要审核') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP072') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP072', '10', 10, '项目代码长度, 会员编号长度') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP073') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP073', '10', 10, '物品代码长度, 会员卡号长度') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP074') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP074', '10', 0, '超过多少不让再次销售, 前期卡抵卡金额不能高于现有卡金额的比率') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP075') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP075', '1', 15, '收银是否显示疗程, 报表金额宽度') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP076') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP076', '', 0, '2手工输入收银项目业绩, 3备用') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP077') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP077', '0', 0, '2启用旧支付方式,是否启用自即时通讯,3YMN前期卡关闭标志, 是否启用自即时通讯') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP078') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP078', '', 0, '当天限制次数, 超过时的措施') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP079') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP079', '3', 0, '抵用券支付对应的支付方式, 启用抵用券号码管理') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP101') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP101', '0', 0, '是否放大客户次数;产品排序0类别 + 编号1:编号') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP102') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP102', '01', 0, '2默认的班次号;考勤是否必须(0必须)排班') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP103') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP103', '5', 0, '提前时间通知;统计所属的所有公司库存') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP104') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP104', '000', 0, '开单默认项目;开单是否启用钥匙牌') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP105') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP105', '090000', 0, '2预约开始时间3开单是否先确定服务人员') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP106') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP106', '', 3, '公司别前缀;公司别数字部分长度') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP107') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP107', '25', 1, 'YMN有效客户年龄开始;是否按房间来管理') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP108') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP108', '55', 0, 'YMN有效客户年龄截止;消费是否发送短信通知客户') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP109') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP109', '', 0, '客户标志, 会员卡开账是否需要审核') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP110') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP110', '1', 0, '启用积分, 会员卡销售是否默认现金') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP111') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP111', '30', 1, '标准吃饭时间, 超过1分钟扣的金额') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP112') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP112', '0', 0, '是否启用购买产品赠送服务功能, 新增会员是否默认发送消费通知短信') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP113') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP113', '', 1, '库存报表是否统计停用产品, 强行追加设置店标志密码') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP114') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP114', '', 0, '默认给积分, 是否发送营业汇报通知') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP115') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP115', '', 0, '商户代码, 计算利润的模式(0卡销售, 1劳动业绩)') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP116') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP116', 'COM1', 0, '与通联POS机连接COM口, 是否启用指纹支付') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP130') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP130', '0', 0, '是否启用电话小秘书, 是否连接银行POS') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP131') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP131', '1', 0, '是否接入电话线, 保留') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP132') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP132', '', 0, '2.本地区号, 3.是否连接Ipad:0-NOS为准,1-IPAD为准,2-IPAD * 折扣') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP133') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP133', '', 0, '分机出局号码, 是否允许跨店补卡') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP134') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP134', '', 0, '内线抢接号码, 超过当天销卡比率不算卡销售业绩') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP135') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP135', '', 0, '2 IP帐号密码, 3是否手工输入产品销售业绩') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP136') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP136', '0', 2, '代拨前是否先拨分机出局号码, 3服务类型(指定,轮班,补工,内创,外创)支持数') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP137') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP137', '0', 0, '2.1-自动开始录音/0-手动录音, 3.卡销售提醒短信') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP138') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP138', '', 0, '录音文件保存路径, 保留') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP139') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP139', '', 1, '保留, 疗程套餐支付方式(1现金类2所有)') --Add by maql 2010/04/09 if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP140') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP140', '0', 1, '启用门店传输, 控制欠款消费60%') --Add by maql 2010/04/27 if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP141') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP141', '', 1, '保留, 购买疗程时是否允许赠送次数大于购买次数') --Add by maql 2010/05/12 if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP142') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP142', '01', 1, '产品赠送所扣仓库, 产品赠送是否直接扣库存1是0否') --Add by maql 20100528 if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP143') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP143', '', 1, '保留, 控制员工编号 0系统唯一 1门店唯一') --Add by maql 20100602 if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP144') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP144', '', 1, '保留, 产品销售是否显示停止销售的产品 1是0否') --Add by maql 20100621 if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP145') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP145', '', 0, '2, 2:强制评价, 1:可选评价, 0: 不启用; 3, 是否对小数进行零截取控制 1是0否') --Add by maql 20100624 if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP146') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP146', '1', 0, '会员享受优惠价 1会员折扣价 2固定会员价, 是否隐藏会员卡号 1是0否') --Add by maql 20100722 if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP147') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP147', '', 1, '赠送账户, 是否需要自动备份日志 1是0否') --Add by maql 20100723 if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP148') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP148', '', 1, '保留, 分发拷贝产品资料时, 安全库存量、最高库存量是否一块拷贝 1是0否') --Add by liyh if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP149') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP149', '', 1, '保留, 预约是否发送短息 1是0否') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP180') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP180', '', 0, '2保留, 3是否允许跨店退卡') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP200') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SP200', '0', '0', '2, 是否允许父门店修改会员资料, 1: 允许, 0: 不允许; 3, 收银项目明细是否备注 0-不备注 1-备注') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP201') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values(@compid, 'SP201', '0', '0', '2-卡销售或充值分享比率之和最大值 3-购买产品分享比率之和最大值') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP202') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values(@compid, 'SP202', '0', '4800', '2-会员卡解挂是否可用通用密码 3-个税起征点(国际,包括港澳台)') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP203') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values(@compid, 'SP203', '0', '0', '2-门店库存统计报表是否可查看所有门店 3-会员卡的可查门店范围:0-可查所有门店 1-可查本门店及子门店 2-只能查本店') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP204') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values(@compid, 'SP204', '0', '0', '2-售卡是否必须输入手机号码(0-不需要, 1-需要) 3-产品入库是否需要审核 0-不需要 1-需要') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP205') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values(@compid, 'SP205', '1', '0', '2-客户端删除开单是否需要验证(0-不需要, 1-需要) 3-产品出库是否需要审核 0-不需要 1-需要') else begin update gsm01 set gsa04c = '2-客户端删除开单是否需要验证(0-不需要, 1-需要) 3-产品出库是否需要审核 0-不需要 1-需要' where gsa01c = 'SP205' and gsa00c = @compid end if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP206') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values(@compid, 'SP206', '', '0', '2-无 3-是否可以查看已停用提成方案') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP207') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values(@compid, 'SP207', '1', '1', '2-默认卡升级有效期计算规则(1-新卡有效期 2-旧卡有效期延期 3-取新旧卡有效期大值)3-分发拷贝时是否拷贝成本价格 1-拷贝 2-不拷贝 默认为1-拷贝') else begin update gsm01 set gsa04c = '2-默认卡升级有效期计算规则(1-新卡有效期 2-旧卡有效期延期 3-取新旧卡有效期大值)3-分发拷贝时是否拷贝成本价格 1-拷贝 2-不拷贝 默认为1-拷贝', gsa03f = 1 where gsa01c = 'SP207' and gsa03f = 0 and gsa00c = @compid end if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP208') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values(@compid, 'SP208', '01', 1, '2-配料仓库, 3-配料是否直接扣库存 1-是 0-否') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP209') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values(@compid, 'SP209', '0', 0, '2-拆分项目消费明细单子单折扣计算规则 1-与主单一致 0-各支付方式单独计算, 3-是否启用配料 1-是 0-否') else begin update gsm01 set gsa04c = '2-拆分项目消费明细单折扣计算规则 1-与主单一致 0-各支付方式单独计算, 3-是否启用配料 1-是 0-否' where gsa00c = @compid and gsa01c = 'SP209' end if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP210') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values(@compid, 'SP210', '', 3, '3-下次疗程时间提醒') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP211') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values(@compid, 'SP211', '0', 0, '2-会员卡挂失是否可用通用密码 3-修改收银单据是否重新发送消费短信(0-不发送 1-发送 默认0-不发送)') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP212') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values(@compid,'SP212','',1,'卡付实际营业额:计算方式(0充值储值支付疗程,1消费疗程支付)') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP213') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values(@compid,'SP213','',1,'是否必须等于购买金额(1:是,0:否)') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP214') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values(@compid,'SP214','1',0,'2产品销售是否允许欠货(1:是,0:否)') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP215') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values(@compid,'SP215','0',0,'2收银充值界面是否禁止输入卡号(1:是,0:否)') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP216') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values(@compid, 'SP216', '1',0, '2-产品采购是否需要审核 0-不需要 1-需要') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP217') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values(@compid,'SP217','0',0,'2套餐是否严格控制有效时长(0否,1是);3套餐是否严格控制有效时间范围(0否,1是)') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP218') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values(@compid,'SP218','0',0,'2-售卡是否必须输入会员生日(0-不需要, 1-需要)') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP219') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values(@compid, 'SP219', '0', 0, '2-派遣员工资料是否和归属公司保持一致 0-不需要 1-需要') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP220') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values(@compid, 'SP220', '1', '30', '短信月费用; 会员卡多久没来通知的天数(-1不检查)') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP221') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values(@compid, 'SP221', '', 0, '3收银消费单号是否自动增加(0否-则弹框提示单号重复,1是-则自动跳单号)') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP222') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values(@compid, 'SP222', '', '0', '2-收银排序 0-按单据时间 1-按单号 ;3-收银时是否需要输入验证码 0-不需要 1-需要') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP223') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values(@compid, 'SP223', '0', '0', '2-收银排序 0-按单据时间 1-按单号 ;3-充值时是否需要重新计算账户截止日期 0-不需要 1-需要') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP244') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values(@compid, 'SP244', '0', '0', '2:经理签单弹框提示 0通过员工授权, 1通过权限设定UR059') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP225') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values(@compid, 'SP225', '0', 0, '紧急状态开关 0-关 1-开') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP226') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values(@compid, 'SP226', '0', 0, '2:收银部门所属控制 0员工,1项目') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP228') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values(@compid, 'SP228', '1', 0, '2:银行卡支付方式是否需要弹框 0否,1是') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP229') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values(@compid, 'SP229', '1', 0, '2:会员卡跨店升级是否默认变更归属门店 0否 1是') if not exists(select 1 from gsm01 where gsa00c = '001' and gsa01c = 'SP997') insert into gsm01 values (@compid, 'SP997', '2000', '2', '2.竞争转卡最低充值额度;3.竞争转卡充值额度相对原卡倍数') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SPD01') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SPD01', 'YYMMDD9999', 0, '登记收费单号default原则 0. 不可修改 1. 可修改, 字符设定值为空:自行输入') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SPD02') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SPD02', 'YYMMDD9999', 0, '开卡单号default原则 0. 不可修改 1. 可修改, 字符设定值为空:自行输入') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SPD03') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SPD03', 'YYMMDD9999', 0, '美容咨询单号单号default原则 0. 不可修改 1. 可修改, 字符设定值为空:自行输入') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SPD04') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SPD04', 'YYMMDD9999', 0, '会员卡异动单号default原则0.不可修改1.可修改, 字符设定值为空:自行输入') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SPD05') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SPD05', '0999', 0, '会员编号default原则0.不可修改1.可修改, 字符设定值为空:自行输入') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SPD06') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SPD06', 'YYMMDD9999', 0, '寄存单号') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SPD07') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SPD07', 'CYYMMDD9999', 0, '会员编号默认规则') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SPD08') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SPD08', 'YYMMDD9999', 0, '') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SPD09') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SPD09', 'YYMMDD9999', 0, '') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SPD10') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SPD10', 'YYMMDD9999', 0, '充值单号default原则 0. 不可修改 1. 可修改, 字符设定值为空:自行输入') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SPD11') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SPD11', 'YYMMDD9999', 0, '客户调查单号default原则 0. 不可修改 1. 可修改, 字符设定值为空:自行输入') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SPD12') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SPD12', 'YYMMDD9999', 0, '入院登记单号default原则 0. 不可修改 1. 可修改, 字符设定值为空:自行输入') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SPD13') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SPD13', 'YYMMDD9999', 0, '壁球入院单号default原则 0. 不可修改 1. 可修改, 字符设定值为空:自行输入') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SPD14') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SPD14', 'YYMMDD9999', 0, '体育用品收银default原则 0. 不可修改 1. 可修改, 字符设定值为空:自行输入') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SPD15') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SPD15', 'YYMMDD9999', 0, '按摩收银单号default原则 0. 不可修改 1. 可修改, 字符设定值为空:自行输入') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SPD16') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SPD16', 'YYMMDD9999', 0, '美容收银单号default原则 0. 不可修改 1. 可修改, 字符设定值为空:自行输入') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SPD17') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SPD17', 'YYMMDD9999', 0, '项目预约单号default原则 0. 不可修改 1. 可修改, 字符设定值为空:自行输入') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SPD18') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SPD18', 'YYMMDD9999', 0, '课程预约单号default原则 0. 不可修改 1. 可修改, 字符设定值为空:自行输入') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SPD19') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SPD19', 'YYMMDD9999', 0, '教练预约单号default原则 0. 不可修改 1. 可修改, 字符设定值为空:自行输入') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SPD20') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SPD20', 'YYMMDD9999', 0, '入库单号default原则 0. 不可修改 1. 可修改, 字符设定值为空:自行输入') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SPD21') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SPD21', 'YYMMDD9999', 0, '出库单号default原则 0. 不可修改 1. 可修改, 字符设定值为空:自行输入') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SPD22') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SPD22', 'YYMMDD9999', 0, '盘点单号default原则 0. 不可修改 1. 可修改, 字符设定值为空:自行输入') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SPD23') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SPD23', 'YYMMDD9999', 0, '会员卡注册单号default原则 0. 不可修改 1. 可修改, 字符设定值为空:自行输入') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SPD24') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SPD24', 'YYMMDD9999', 0, '简餐单号default原则 0. 不可修改 1. 可修改, 字符设定值为空:自行输入') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SPD25') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SPD25', 'YYMMDD9999', 0, '其它收银单号default原则 0. 不可修改 1. 可修改, 字符设定值为空:自行输入') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SPD26') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SPD26', 'YYMMDD9999', 0, '费用登记单号default原则 0. 不可修改 1. 可修改, 字符设定值为空:自行输入') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SPD27') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SPD27', '1', 0, '2-售卡应收金额与储值金额是否必须相等, 3-满意度只评价一次') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SPD28') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SPD28', 'CYYMMDD9999', 0, '订单单号default原则 0. 不可修改 1. 可修改, 字符设定值为空:自行输入') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SPD29') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SPD29', 'YYMMDD9999', 0, '发货申请单号default原则 0. 不可修改 1. 可修改, 字符设定值为空:自行输入') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SPD30') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SPD30', 'YYMMDD9999', 0, '收货单号default原则 0. 不可修改 1. 可修改, 字符设定值为空:自行输入') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SPD31') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SPD31', 'YYMMDD9999', 0, '账户异动单号default原则 0. 不可修改 1. 可修改, 字符设定值为空:自行输入') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SPD32') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SPD32', 'YYMMDD9999', 0, '2-采购申请单号default原则 3-0.不可修改 1.可修改, 字符设定值为空:自行输入') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SPD33') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SPD33', 'YYMMDD9999', 0, '2-采购发货单号default原则 3-0.不可修改 1.可修改, 字符设定值为空:自行输入') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SPD34') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SPD34', 'YYMMDD9999', 0, '2-采购收货单号default原则 3-0.不可修改 1.可修改, 字符设定值为空:自行输入') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SPD35') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SPD35', 'YYMMDD9999', 0, '2-配发产品单号default原则 3-0.不可修改 1.可修改, 字符设定值为空:自行输入') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SPD36') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SPD36', 'YYMMDD9999', 0, '2-采购退货单号default原则 3-0.不可修改 1.可修改, 字符设定值为空:自行输入') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SPD37') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SPD37', 'YYMMDD9999', 0, '2-产品配料单号default原则 3-0.不可修改 1.可修改, 字符设定值为空:自行输入') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SPD38') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SPD38', 'YYMMDD9999', 0, '2-供应商采购单号default原则 3-0.不可修改 1.可修改, 字符设定值为空:自行输入') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SPD39') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SPD39', 'YYMMDD9999', 0, '2-竞争转卡单号default原则 3-0.不可修改 1.可修改, 字符设定值为空:自行输入') --added by cyr 20121016 if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'RP001') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'RP001', '18', 15, '2:系统列表分页设置每页默认显示的条数 3:报表列表分页设置每页默认显示的条数') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'RP002') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'RP002', '', 0, '2:报表站点 3:备用字段') if not exists(select 1 from gsm01 where gsa00c = '001' and gsa01c = 'RP004') insert into gsm01 values (@compid, 'RP004', '2', '0', '2:评价器端口号; 3: 评价依据 0按服务人员评价 1按项目评价 2按单据评价') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'RP005') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'RP005', '1', 0, '2:员工pad查询密码是否允许设置为空 3:卡销售或充值分享金额之和是否必须小于等于营业额') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'RP006') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'RP006', '0', 0, '2:员工业绩小单是否可打印提成 3:备用') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'RP007') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'RP007', '1', 1, '2:可视卡X坐标起始位置 3:可视卡Y坐标起始位置') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'RP008') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'RP008', '1', 1, '2:会员卡销售价是否可以低于标准价 3:备用') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'RP009') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'RP009', '荣大', 1, '2:可视卡机器型号 3:备用') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'RP010') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'RP010', '0', 1, '2:项目消耗是否只能按指定消耗产品类别加载 3:收银取单时的结账单号 1默认以系统自动生成 2默认以挂单时录入的单号') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'RP011') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'RP011', '', 1, '2:套餐替换套餐差额存放账户 3:备用') if not exists(select 1 from gsm01 where gsa00c = @compid and gsa01c = 'RP012') insert gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'RP012', '0', 0, '2:拷贝提成方案时是否同时拷贝员工提成方案设置 3:产品欠货领取是否扣库存') --移动终端参数 begin 20130411 if not exists (select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SPT01') begin insert into gsm01(gsa00c,gsa01c,gsa02c,gsa03f,gsa04c) values(@compid,'SPT01', '3', 0, '2-PC端软件类型,1:POS, 2:GOS, 3:NOS, 4:永琪GOS; 3-客户终端分享员工照片送积分') end if not exists (select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SPT02') insert into gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SPT02', '0', 0, '2-是否显示实业绩, 1:显示, 0:不显示 3-客户终端申请电子会员卡送多少积分') if not exists (select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SPT03') begin insert into gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values(@compid, 'SPT03', '0', 1, '2-是否允许修改价格,1:允许, 0:不允许; 3-客户端签到送多少积分') end if not exists (select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SPT04') insert into gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SPT04', '1', 0, '2-是否允许修改服务类型(大中小工) 1:允许 0:不允许; 3-是否启用接单 1:启用 0:未启用') if not exists (select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SPT05') insert into gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values( @compid, 'SPT05', '1', 0, '2-是否允许修改数量 1:允许, 0:不允许 3-添加项目是否自动载入员工 1:是 0:否') if not exists (select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SPT06') begin insert into gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values(@compid, 'SPT06', '0', 0, '2-是否显示公司名称 1:显示, 0:不显示 3-客户终端是否只显示积分余额 1:是 0:否') end else begin update gsm01 set gsa04c = '2-是否显示公司名称 1:显示, 0:不显示 3-客户终端是否只显示积分余额 1:是 0:否' where gsa01c = 'SPT06' end if not exists (select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SPT07') begin insert into gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values(@compid, 'SPT07', '0', 0, '2-是否显示登录者名字(员工、客户) 1:显示, 0:不显示 3-客户终端不显示疗程 1-是 0-否') end else begin update gsm01 set gsa04c = '2-是否显示登录者名字(员工、客户) 1:显示, 0:不显示 3-客户终端不显示疗程 1-是 0-否' where gsa01c = 'SPT07' end if not exists (select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SPT08') begin insert into gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values(@compid, 'SPT08', '60', 0, '2-客户终端预约时间间隔,以30分钟的倍数设置;3-申请电子卡填写介绍人送积分') end else begin update gsm01 set gsa04c = '2-客户终端预约时间间隔,以30分钟的倍数设置;3-申请电子卡填写介绍人送积分' where gsa01c = 'SPT08' end if not exists (select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SPT09') begin insert into gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values(@compid, 'SPT09', '123456', 0, '2-门店密码; 3-介绍客户申请电子卡送积分') end else begin update gsm01 set gsa04c = '2-门店密码; 3-介绍客户申请电子卡送积分' where gsa01c = 'SPT09' end if not exists (select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SPT10') begin insert into gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values(@compid, 'SPT10', '1', 0, '2-客户终端是否显示账户历史和交易历史 1:显示, 0:不显示;3-客户预约送积分数') end else begin update gsm01 set gsa04c = '2-客户终端是否显示账户历史和交易历史 1:显示, 0:不显示; 3-客户预约送积分数' where gsa01c = 'SPT10' end if not exists (select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SPT11') begin insert into gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values(@compid, 'SPT11', '1', 0, '2-员工终端上传作品是否自动审核 1:是, 0:否;3-结束服务是否继续开单 1:是, 0:否') end else begin update gsm01 set gsa04c = '2-员工终端上传作品是否自动审核 1:是, 0:否;3-结束服务是否继续开单 1:是, 0:否' where gsa01c = 'SPT11' end if not exists (select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SPT12') begin insert into gsm01(gsa00c, gsa01c, gsa02c, gsa03f, gsa04c) values(@compid, 'SPT12', '1', 0, '2-前台终端删除开单是否需要验证 1:是, 0:否 3-结束服务不需要验证 1:是,0:否') end if not exists (select 1 from gsm01 where gsa00c=@compid and gsa01c='SPT13') begin insert into gsm01(gsa00c,gsa01c,gsa02c,gsa03f,gsa04c) values(@compid,'SPT13','020310073920001',0,'2-iPOS开发者号,3-停用iPOS刷卡功能 1:停用 0:不停用') end if not exists (select 1 from gsm01 where gsa00c=@compid and gsa01c='SPT14') begin insert into gsm01(gsa00c,gsa01c,gsa02c,gsa03f,gsa04c) values(@compid,'SPT14','00000016',0,'2-iPOSapp编号,3-开单是否可查询所有时间 1:是 0:否') end if not exists (select 1 from gsm01 where gsa00c=@compid and gsa01c='SPT15') begin insert into gsm01(gsa00c,gsa01c,gsa02c,gsa03f,gsa04c) values(@compid,'SPT15','64608413',0,'2-iPOSappKey,3-客户预约员工需要排班 1:是 0:否') end if not exists (select 1 from gsm01 where gsa00c=@compid and gsa01c='SPT16') begin insert into gsm01(gsa00c,gsa01c,gsa02c,gsa03f,gsa04c) values(@compid,'SPT16','0216460841303',0,'2-iPOS开发者支付流水号,3-前台终端单号是否可修改') end if not exists (select 1 from gsm01 where gsa00c=@compid and gsa01c='SPT17') begin insert into gsm01(gsa00c,gsa01c,gsa02c,gsa03f,gsa04c) values(@compid,'SPT17','南融iPos前台',0,'2-iPOS应用名称') end --移动终端参数 end end GO /****** Object: StoredProcedure [dbo].[upg_cal_jiangfa_amt] Script Date: 06/12/2017 06:19:32 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_cal_jiangfa_amt]( @compid varchar(20), @fromdate varchar(20), @todate varchar(20), @flag int ) -- @flag = 1 需要计算吃饭时间的奖罚, 否则不要 as begin declare @num_unit int declare @amt_unit int select @num_unit = convert(int, gsa02c), @amt_unit = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP046' create table #tbl_staff_kq( empid varchar(20) not null, -- 员工代码 ddate varchar(20) not null, -- 日期 during1 int null, -- 迟到时间 during2 int null, -- 早退时间 amt float null, -- 扣款金额 lunch_start varchar(20) null, -- 中饭开始时间 lunch_end varchar(20) null, -- 中饭截至时间 dinner_start varchar(20) null, -- 晚饭开始时间 dinner_end varchar(20) null, -- 晚饭截至时间 lunch_during int null, -- 中饭时间 dinner_during int null, -- 晚饭时间 rice_amt float null, -- 吃饭奖罚金额 autoidd int identity not null, idd float null) -- 序号 create table #tbl_cur_maxid( compid varchar(10) not null, empid varchar(20) not null, maxid float null) create table #tbl_related_empl( empid varchar(20) not null, -- 员工编号 startid float null) -- 开始的编号 insert #tbl_staff_kq(empid, ddate, during1, during2, lunch_start, lunch_end, dinner_start, dinner_end) select hda01c, hda02d, hda08i, hda09i, hda04t, hda12t, hda13t, hda14t -- modify by lm 2015.12.04 from hdm01 where hda00c = @compid and hda02d >= @fromdate and hda02d <= @todate and hda07c<>'2' update a set a.amt = (during1 + during2)/@num_unit * @amt_unit from #tbl_staff_kq a --处理可能吃饭登记不完整,我们就默认不扣钱 if @flag = 1 begin update #tbl_staff_kq set lunch_start = '000000' where isnull(lunch_start, '') = '' update #tbl_staff_kq set dinner_start = '000000' where isnull(dinner_start, '') = '' update #tbl_staff_kq set lunch_end = lunch_start where isnull(lunch_end, '') = '' or lunch_end0 end drop table #tbl_staff_kq drop table #tbl_cur_maxid drop table #tbl_related_empl end GO /****** Object: StoredProcedure [dbo].[upg_staff_total_turnover] Script Date: 06/12/2017 06:20:42 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_staff_total_turnover] ( @compid varchar(10), @fromdate varchar(10), -- 开始日期 @todate varchar(10), -- 截至日期 @fromempl varchar(20), -- 查询开始人员 @toempl varchar(20) -- 查询截至人员 ) as --WUXM 2006/01/20 begin create table #empl_stat( id int identity , -- 流水好 person_id varchar(20) not null, -- 员工代码 action_id tinyint not null, -- 类别, 1-卡销售 2-指定大工, 3-轮班大工, 4-补工大工, -- 5-指定中工, 6-轮班中工 -- 7-指定小工, 8-轮班小工, 9-产品销售 srvdate varchar(10) not null, --日期 code varchar(20) not null, --项目代码, 或是卡号, 产品码 name varchar(100) null, --名称 项目简称(gda03c) / 产品名称(gfa03c) 长度为100 payway varchar(20) null, --支付方式 amt float null, --营业金额 amt_vperf float null, --虚业绩 srvtime varchar(20) null, --时间 custid varchar(20) null, --客户编号 custname varchar(40) null, --客户名字 num float null, --数量 cost float null default 0, --成本 Add by maql 20100702 primary key(id)) --add by lyj begin 2008-01-16 declare @SP04802 varchar(5) select @SP04802 = gsa02c from gsm01 where gsa00c = @compid and gsa01c = 'SP048' set @SP04802 = isnull(@SP04802, 0) --end if ((@fromempl = '' and @toempl = '')) or @fromempl = '*' begin select @fromempl = min(haa01c) from ham01 where haa00c = @compid select @toempl = max(haa01c) from ham01 where haa00c = @compid end create table #gnm01_gnm02 -- 会员卡销售单 ( gna00c varchar(10) not null, --公司编号 gna01c varchar(20) not null, --销售单号 gna02d varchar(8) null, --销售日期 gna03c varchar(5) null, --证件类别 gna04c varchar(20) null, --证件编号 gna05c varchar(5) null, --客户性质 gna06c varchar(40) null, --客户名称 gna07c varchar(160)null, --客户地址 gna08c varchar(6) null, --邮政编码 gna09c varchar(20) null, --介绍会员 gna10d varchar(8) null, --出生日期 gna11c varchar(5) null, --职业 gna12t varchar(6) null, --销售时间 gna13c varchar(20) null, --取gnm02中第一张卡 gna14f float null, --(预留) gna15c varchar(5) null, --币别 gna16f float null, --汇率 gna17f float null, --(预留) gna18f float null, --(预留) gna19f float null, --(预留) gna20c varchar(10) null, --负责业务 gna21c varchar(30) null, --发票号码 gna22i int null, --发票是否已列印(1-已列印, 0 -未列印) gna23d varchar(8) null, --开卡起始日 gna24d varchar(8) null, --开卡截止日 gna25c varchar(20) null, --原会员卡号 gna26f float null, --实收总额 gna91c varchar(10) null, --登陆人员代码 gna92d varchar(8) null, --登陆日期 gna93c varchar(10) null, --覆核人员代码 gna94d varchar(8) null, --覆核日期 gna27f float null, --第一分享比率 gna28c varchar(10) null, --第二卖卡人 gna29f float null, --分享比率 gna30c varchar(10) null, --第三卖卡人 gna31f float null, --分享比率 gna32c varchar(10) null, --第4卖卡人 gna33f float null, --分享比率 gna34c varchar(10) null, --第5卖卡人 gna35f float null, --分享比率 gna36c varchar(10) null, --第6卖卡人 gna37f float null, --分享比率 gna38c varchar(10) null, --第7卖卡人 gna39f float null, --分享比率 gna40c varchar(10) null, --第8卖卡人 gna41f float null, --分享比率 gna42c varchar(10) null, --第9卖卡人 gna43f float null, --分享比率 gna44c varchar(10) null, --第10卖卡人 gna45f float null, --分享比率 gna46c varchar(10) null, --第11卖卡人 gna47f float null, --分享比率 gna48c varchar(10) null, --第12卖卡人 gna49f float null, --分享比率 gna50c varchar(10) null, --第13卖卡人 gna51f float null, --分享比率 gna52c varchar(10) null, --第14卖卡人 gna53f float null, --分享比率 gna54c varchar(10) null, --第15卖卡人 gna55f float null, --分享比率 gna56c varchar(10) null, --第16卖卡人 gna57f float null, --分享比率 gna58c varchar(10) null, --第17卖卡人 gna59f float null, --分享比率 gna60c varchar(10) null, --第18卖卡人 gna61f float null, --分享比率 gna62c varchar(10) null, --第19卖卡人 gna63f float null, --分享比率 gna64c varchar(10) null, --第20卖卡人 gna65f float null, --分享比率 gna66c varchar(20) null, --会员手机号, added by WUXM, 2005/09/26 gna80d varchar(8) null, --帐务日期, added by WUXM, 2005/08/16 gna67f float null, --第一销售虚业绩 Added by WUXM 2005/11/04 gna68f float null, --第一销售提成 Added by WUXM 2005/11/04 gna69f float null, --第二销售虚业绩 Added by WUXM 2005/11/04 gna70f float null, --第二销售提成 Added by WUXM 2005/11/04 gna71f float null, --第三销售虚业绩 Added by WUXM 2005/11/04 gna72f float null, --第三销售提成 Added by WUXM 2005/11/04 gna73i int null, --会员卡备注中的信息在收银里是否显示过的标志,1为显示过,0为没有显示 gna81c varchar(10) null, --部门编号 gna82c varchar(10) null, --部门编号 gna83c varchar(10) null, --部门编号 gna85i int null, --1--男 0--女 gna86f float null, --第四人员业绩 gna87f float null, --第五人员业绩 gna88f float null, --第六人员业绩 gna89f float null, --第七人员业绩 gna90f float null, --第八人员业绩 gna91f float null, --第九人员业绩 gna92f float null, --第十人员业绩 gnb00c varchar(10) not null, --公司编号 gnb01c varchar(20) not null, --销售单号 gnb02c varchar(5) null, --卡种 gnb03c varchar(20) not null, --会员卡号 gnb04i int null, --折让类别(分为 1- 一般折让, 2-续卡优惠价, 3-内部优惠价) gnb05f float null, --标准金额 gnb06f float null, --折让 gnb07f float null, --折让金额 gnb08f float null, --卡销售金额(包括疗程的销售金额) gnb09i int null, --是否已经客户提佣 gnb10i int null, --是否已经员工提佣 gnb11f float null, --储值金额 , added by WUXM, 2005/10/28 gnb12f float null, --默认储值金额 gnb13i int null --账户 ) insert #gnm01_gnm02(gna00c, gna01c, gna02d, gna03c, gna04c, gna05c, gna06c, gna07c, gna08c, gna09c, gna10d, gna11c, gna12t, gna13c, gna14f, gna15c, gna16f, gna17f, gna18f, gna19f, gna20c, gna21c, gna22i, gna23d, gna24d, gna25c, gna26f, gna91c, gna92d, gna93c, gna94d, gna27f, gna28c, gna29f, gna30c, gna31f, gna32c, gna33f, gna34c, gna35f, gna36c, gna37f, gna38c, gna39f, gna40c, gna41f, gna42c, gna43f, gna44c, gna45f, gna46c, gna47f, gna48c, gna49f, gna50c, gna51f, gna52c, gna53f, gna54c, gna55f, gna56c, gna57f, gna58c, gna59f, gna60c, gna61f, gna62c, gna63f, gna64c, gna65f, gna66c, gna80d, gna67f, gna68f, gna69f, gna70f, gna71f, gna72f, gna73i, gna81c, gna82c, gna83c, gna85i, gna86f, gna87f, gna88f, gna89f, gna90f, gna91f, gna92f, gnb00c, gnb01c, gnb02c, gnb03c, gnb04i, gnb05f, gnb06f, gnb07f, gnb08f, gnb09i, gnb10i, gnb11f, gnb12f, gnb13i) select gna00c, gna01c, gna02d, gna03c, gna04c, gna05c, gna06c, gna07c, gna08c, gna09c, gna10d, gna11c, gna12t, gna13c, gna14f, gna15c, gna16f, gna17f, gna18f, gna19f, gna20c, gna21c, gna22i, gna23d, gna24d, gna25c, gna26f, gna91c, gna92d, gna93c, gna94d, gna27f, gna28c, gna29f, gna30c, gna31f, gna32c, gna33f, gna34c, gna35f, gna36c, gna37f, gna38c, gna39f, gna40c, gna41f, gna42c, gna43f, gna44c, gna45f, gna46c, gna47f, gna48c, gna49f, gna50c, gna51f, gna52c, gna53f, gna54c, gna55f, gna56c, gna57f, gna58c, gna59f, gna60c, gna61f, gna62c, gna63f, gna64c, gna65f, gna66c, gna80d, gna67f, gna68f, gna69f, gna70f, gna71f, gna72f, gna73i, gna81c, gna82c, gna83c, gna85i, gna86f, gna87f, gna88f, gna89f, gna90f, gna91f, gna92f, gnb00c, gnb01c, gnb02c, gnb03c, gnb04i, gnb05f, gnb06f, gnb07f, gnb08f, gnb09i, gnb10i, gnb11f, gnb12f, gnb13i from gnm01 a with (nolock), gnm02 b with (nolock) where a.gna00c = b.gnb00c and a.gna01c = b.gnb01c and a.gna00c = @compid and a.gna80d >= @fromdate and a.gna80d <= @todate --卡的与业绩相关的纪录 --卖卡第一销售人员 insert #empl_stat(person_id, action_id, srvdate, code, name, amt, amt_vperf, srvtime, custid, custname, num) select distinct gna20c, 1, gna80d, gnb03c, '', gnb08f, gna67f, gna12t, gna13c, gna06c, 1 from #gnm01_gnm02 where gna20c >= @fromempl and gna20c <= @toempl order by gna20c, gna80d --卖卡第二销售人员 insert #empl_stat(person_id, action_id, srvdate, code, name, amt, amt_vperf, srvtime, custid, custname, num) select distinct gna28c, 1, gna80d, gnb03c, '', gnb08f, gna69f, gna12t, gna13c, gna06c, 1 from #gnm01_gnm02 where gna28c >= @fromempl and gna28c <= @toempl order by gna28c, gna80d --卖卡第三销售人员 insert #empl_stat(person_id, action_id, srvdate, code, name, amt, amt_vperf, srvtime, custid, custname, num) select gna30c, 1, gna80d, gnb03c, '', gnb08f, gna71f, gna12t, gna13c, gna06c, 1 from #gnm01_gnm02 where gna30c >= @fromempl and gna30c <= @toempl order by gna30c, gna80d --第4销售 insert #empl_stat(person_id, action_id, srvdate, code, name, amt, amt_vperf, srvtime, custid, custname, num) select distinct gna32c, 1, gna80d, gnb03c, '', gnb08f, gna86f, gna12t, gna13c, gna06c, 1 from #gnm01_gnm02 where gna32c >= @fromempl and gna32c <= @toempl order by gna32c, gna80d --第5销售 insert #empl_stat(person_id, action_id, srvdate, code, name, amt, amt_vperf, srvtime, custid, custname, num) select distinct gna34c, 1, gna80d, gnb03c, '', gnb08f, gna87f, gna12t, gna13c, gna06c, 1 from #gnm01_gnm02 where gna34c >= @fromempl and gna34c <= @toempl order by gna34c, gna80d --第6销售 insert #empl_stat(person_id, action_id, srvdate, code, name, amt, amt_vperf, srvtime, custid, custname, num) select distinct gna36c, 1, gna80d, gnb03c, '', gnb08f, gna88f, gna12t, gna13c, gna06c, 1 from #gnm01_gnm02 where gna36c >= @fromempl and gna36c <= @toempl order by gna36c, gna80d --第7销售 insert #empl_stat(person_id, action_id, srvdate, code, name, amt, amt_vperf, srvtime, custid, custname, num) select distinct gna38c, 1, gna80d, gnb03c, '', gnb08f, gna89f, gna12t, gna13c, gna06c, 1 from #gnm01_gnm02 where gna38c >= @fromempl and gna38c <= @toempl order by gna38c, gna80d --第8销售 insert #empl_stat(person_id, action_id, srvdate, code, name, amt, amt_vperf, srvtime, custid, custname, num) select distinct gna40c, 1, gna80d, gnb03c, '', gnb08f, gna90f, gna12t, gna13c, gna06c, 1 from #gnm01_gnm02 where gna40c >= @fromempl and gna40c <= @toempl order by gna40c, gna80d --第9销售 insert #empl_stat(person_id, action_id, srvdate, code, name, amt, amt_vperf, srvtime, custid, custname, num) select distinct gna42c, 1, gna80d, gnb03c, '', gnb08f, gna91f, gna12t, gna13c, gna06c, 1 from #gnm01_gnm02 where gna42c >= @fromempl and gna42c <= @toempl order by gna42c, gna80d --第10销售 insert #empl_stat(person_id, action_id, srvdate, code, name, amt, amt_vperf, srvtime, custid, custname, num) select distinct gna44c, 1, gna80d, gnb03c, '', gnb08f, gna92f, gna12t, gna13c, gna06c, 1 from #gnm01_gnm02 where gna44c >= @fromempl and gna44c <= @toempl order by gna44c, gna80d drop table #gnm01_gnm02 --卡充值 create table #gcm10 -- 账户异动单 ( gcl00c varchar(10) not null, --公司编号 gcl01c varchar(20) not null, --异动单号 gcl02d varchar(8) not null, --异动日期 gcl03c varchar(20) null, --会员卡号 gcl04c varchar(5) null, --帐号类别(1.电子钱包 2.储值账户) gcl05c varchar(20) null, --帐号(暂不用) gcl06i int null, --异动类别( 0 - 充值, 1 - 取款) gcl07f float null, --异动金额(总收款金额), 含套餐,含疗程 gcl08f float null, --异动前余额 gcl09c varchar(20) null, --销售人员 gcl10c varchar(10) null, --支付方式 gcl11c varchar(2) null, --单据类别, 此账户异动单来自哪里 gcl12c varchar(20) null, --单号, Added by WUXM, 2004/04/10 gcl13f float null, --欠款金额 gcl91c varchar(10) null, --登陆人员代码 gcl92d varchar(8) null, --登陆日期 gcl93c varchar(10) null, --覆核人员代码 gcl94d varchar(8) null, --覆核日期 gcl14i int null, --是否升级会员卡类别 gcl15c varchar(5) null, --新的会员卡类别 gcl16f float null, --第一销售分享比率 gcl17c varchar(20) null, --第二销售 gcl18f float null, --分享比率 gcl19c varchar(20) null, --第三负责人 gcl20f float null, --分享比率 gcl21d varchar(8) null, --新截至日期 gcl22c varchar(20) null, --第4负责人 gcl23f float null, --分享比率 gcl24c varchar(20) null, --第5负责人 gcl25f float null, --分享比率 gcl26c varchar(20) null, --第6负责人 gcl27f float null, --分享比率 gcl28c varchar(20) null, --第7负责人 gcl29f float null, --分享比率 gcl30c varchar(20) null, --第8负责人 gcl31f float null, --分享比率 gcl32c varchar(20) null, --第9负责人 gcl33f float null, --分享比率 gcl34c varchar(20) null, --第10负责人 gcl35f float null, --分享比率 gcl36c varchar(20) null, --第11负责人 gcl37f float null, --分享比率 gcl38c varchar(20) null, --第12负责人 gcl39f float null, --分享比率 gcl40c varchar(20) null, --第13负责人 gcl41f float null, --分享比率 gcl42c varchar(20) null, --第14负责人 gcl43f float null, --分享比率 gcl44c varchar(20) null, --第15负责人 gcl45f float null, --分享比率 gcl46c varchar(20) null, --第16负责人 gcl47f float null, --分享比率 gcl48c varchar(20) null, --第17负责人 gcl49f float null, --分享比率 gcl50c varchar(20) null, --第18负责人 gcl51f float null, --套餐金额 gcl52c varchar(20) null, --备用 gcl53f float null, --储值应收金额 gcl54c varchar(20) null, --是否给积分 gcl55f float null, --储值卡的次数 gcl56t varchar(6) null, --消费时间 added by WUXM, 2005/08/16 gcl80d varchar(8) null, --帐务日期 added by WUXM, 2005/08/16 gcl57c varchar(120) null, --备注说明, added by WUXM, 2005/09/05 gcl58f float null, --第一销售虚业绩 Added by WUXM 2005/11/08 gcl59f float null, --第一销售提成 Added by WUXM 2005/11/08 gcl60f float null, --第二销售虚业绩 Added by WUXM 2005/11/08 gcl61f float null, --第二销售提成 Added by WUXM 2005/11/08 gcl62f float null, --第三销售虚业绩 Added by WUXM 2005/11/08 gcl63f float null, --第三销售提成 Added by WUXM 2005/11/08 gcl64f float null, --总储值金额(包括疗程) gcl65f float null, --总储值金额(除疗程) gcl66f float null, --总疗程金额 gcl81c varchar(10) null, --部门编号 gcl82c varchar(10) null, --部门编号 gcl83c varchar(10) null, --部门编号 gcl83f float null, --第四人员业绩 gcl84f float null, --第五人员业绩 gcl85f float null, --第六人员业绩 gcl86f float null, --第七人员业绩 gcl87f float null, --第八人员业绩 gcl88f float null, --第九人员业绩 gcl89f float null --第十人员业绩 ) insert #gcm10(gcl00c, gcl01c, gcl02d, gcl03c, gcl04c, gcl05c, gcl06i, gcl07f, gcl08f, gcl09c, gcl10c, gcl11c, gcl12c, gcl13f, gcl91c, gcl92d, gcl93c, gcl94d, gcl14i, gcl15c, gcl16f, gcl17c, gcl18f, gcl19c, gcl20f, gcl21d, gcl22c, gcl23f, gcl24c, gcl25f, gcl26c, gcl27f, gcl28c, gcl29f, gcl30c, gcl31f, gcl32c, gcl33f, gcl34c, gcl35f, gcl36c, gcl37f, gcl38c, gcl39f, gcl40c, gcl41f, gcl42c, gcl43f, gcl44c, gcl45f, gcl46c, gcl47f, gcl48c, gcl49f, gcl50c, gcl51f, gcl52c, gcl53f, gcl54c, gcl55f, gcl56t, gcl80d, gcl57c, gcl58f, gcl59f, gcl60f, gcl61f, gcl62f, gcl63f, gcl64f, gcl65f, gcl66f, gcl81c, gcl82c, gcl83c, gcl83f, gcl84f, gcl85f, gcl86f, gcl87f, gcl88f, gcl89f) select gcl00c, gcl01c, gcl02d, gcl03c, gcl04c, gcl05c, gcl06i, gcl07f, gcl08f, gcl09c, gcl10c, gcl11c, gcl12c, gcl13f, gcl91c, gcl92d, gcl93c, gcl94d, gcl14i, gcl15c, gcl16f, gcl17c, gcl18f, gcl19c, gcl20f, gcl21d, gcl22c, gcl23f, gcl24c, gcl25f, gcl26c, gcl27f, gcl28c, gcl29f, gcl30c, gcl31f, gcl32c, gcl33f, gcl34c, gcl35f, gcl36c, gcl37f, gcl38c, gcl39f, gcl40c, gcl41f, gcl42c, gcl43f, gcl44c, gcl45f, gcl46c, gcl47f, gcl48c, gcl49f, gcl50c, gcl51f, gcl52c, gcl53f, gcl54c, gcl55f, gcl56t, gcl80d, gcl57c, gcl58f, gcl59f, gcl60f, gcl61f, gcl62f, gcl63f, gcl64f, gcl65f, gcl66f, gcl81c, gcl82c, gcl83c, gcl83f, gcl84f, gcl85f, gcl86f, gcl87f, gcl88f, gcl89f from gcm10 where gcl00c = @compid and gcl80d >= @fromdate and gcl80d <= @todate and isnull(gcl94d, '')<>'' --第一销售 insert #empl_stat(person_id, action_id, srvdate, code, name, amt, amt_vperf, srvtime, custid, custname, num) select distinct gcl09c, 1, gcl80d, gcl03c, '', gcl07f, gcl58f, gcl56t, gcl03c, '', 1 from #gcm10 a where a.gcl09c >= @fromempl and a.gcl09c <= @toempl and (a.gcl06i = 0 or a.gcl06i = 6) order by gcl09c, gcl80d --第二销售 insert #empl_stat(person_id, action_id, srvdate, code, name, amt, amt_vperf, srvtime, custid, custname, num) select distinct gcl17c, 1, gcl80d, gcl03c, '', gcl07f, gcl60f, gcl56t, gcl03c, '', 1 from #gcm10 a where a.gcl17c >= @fromempl and a.gcl17c <= @toempl and (a.gcl06i = 0 or a.gcl06i = 6) order by gcl17c, gcl80d --第三销售 insert #empl_stat(person_id, action_id, srvdate, code, name, amt, amt_vperf, srvtime, custid, custname, num) select distinct gcl19c, 1, gcl80d, gcl03c, '', gcl07f, gcl62f, gcl56t, gcl03c, '', 1 from #gcm10 a where a.gcl19c >= @fromempl and a.gcl19c <= @toempl and (a.gcl06i = 0 or a.gcl06i = 6) order by gcl19c, gcl80d --第4销售 insert #empl_stat(person_id, action_id, srvdate, code, name, amt, amt_vperf, srvtime, custid, custname, num) select distinct gcl22c, 1, gcl80d, gcl03c, '', gcl07f, gcl83f, gcl56t, gcl03c, '', 1 from #gcm10 a where a.gcl22c >= @fromempl and a.gcl22c <= @toempl and (a.gcl06i = 0 or a.gcl06i = 6) order by gcl22c, gcl80d --第5销售 insert #empl_stat(person_id, action_id, srvdate, code, name, amt, amt_vperf, srvtime, custid, custname, num) select distinct gcl24c, 1, gcl80d, gcl03c, '', gcl07f, gcl84f, gcl56t, gcl03c, '', 1 from #gcm10 a where a.gcl24c >= @fromempl and a.gcl24c <= @toempl and (a.gcl06i = 0 or a.gcl06i = 6) order by gcl24c, gcl80d --第6销售 insert #empl_stat(person_id, action_id, srvdate, code, name, amt, amt_vperf, srvtime, custid, custname, num) select distinct gcl26c, 1, gcl80d, gcl03c, '', gcl07f, gcl85f, gcl56t, gcl03c, '', 1 from #gcm10 a where a.gcl26c >= @fromempl and a.gcl26c <= @toempl and (a.gcl06i = 0 or a.gcl06i = 6) order by gcl26c, gcl80d --第7销售 insert #empl_stat(person_id, action_id, srvdate, code, name, amt, amt_vperf, srvtime, custid, custname, num) select distinct gcl28c, 1, gcl80d, gcl03c, '', gcl07f, gcl86f, gcl56t, gcl03c, '', 1 from #gcm10 a where a.gcl28c >= @fromempl and a.gcl28c <= @toempl and (a.gcl06i = 0 or a.gcl06i = 6) order by gcl28c, gcl80d --第8销售 insert #empl_stat(person_id, action_id, srvdate, code, name, amt, amt_vperf, srvtime, custid, custname, num) select distinct gcl30c, 1, gcl80d, gcl03c, '', gcl07f, gcl87f, gcl56t, gcl03c, '', 1 from #gcm10 a where a.gcl30c >= @fromempl and a.gcl30c <= @toempl and (a.gcl06i = 0 or a.gcl06i = 6) order by gcl30c, gcl80d --第9销售 insert #empl_stat(person_id, action_id, srvdate, code, name, amt, amt_vperf, srvtime, custid, custname, num) select distinct gcl32c, 1, gcl80d, gcl03c, '', gcl07f, gcl88f, gcl56t, gcl03c, '', 1 from #gcm10 a where a.gcl32c >= @fromempl and a.gcl32c <= @toempl and (a.gcl06i = 0 or a.gcl06i = 6) order by gcl32c, gcl80d --第10销售 insert #empl_stat(person_id, action_id, srvdate, code, name, amt, amt_vperf, srvtime, custid, custname, num) select distinct gcl34c, 1, gcl80d, gcl03c, '', gcl07f, gcl89f, gcl56t, gcl03c, '', 1 from #gcm10 a where a.gcl34c >= @fromempl and a.gcl34c <= @toempl and (a.gcl06i = 0 or a.gcl06i = 6) order by gcl34c, gcl80d --第一销售销售取款,欠款 insert #empl_stat(person_id, action_id, srvdate, code, name, amt, amt_vperf, srvtime, custid, custname, num) select distinct gcl09c, 1, gcl80d, gcl03c, '', 0 - gcl07f, isnull(gcl16f, 1)*(0 - gcl07f), gcl56t, gcl03c, '', 0 from #gcm10 a where a.gcl09c >= @fromempl and a.gcl09c <= @toempl and (a.gcl06i = 1 or a.gcl06i = 5) order by gcl09c, gcl80d --第二销售销售取款,欠款 insert #empl_stat(person_id, action_id, srvdate, code, name, amt, amt_vperf, srvtime, custid, custname, num) select distinct gcl17c, 1, gcl80d, gcl03c, '', 0 - gcl07f, isnull(gcl18f, 0)*(0 - gcl07f), gcl56t, gcl03c, '', 0 from #gcm10 a where a.gcl17c >= @fromempl and a.gcl17c <= @toempl and (a.gcl06i = 1 or a.gcl06i = 5) order by gcl17c, gcl80d --第三销售销售取款,欠款 insert #empl_stat(person_id, action_id, srvdate, code, name, amt, amt_vperf, srvtime, custid, custname, num) select distinct gcl19c, 1, gcl80d, gcl03c, '', 0 - gcl07f, isnull(gcl20f, 0)*(0 - gcl07f), gcl56t, gcl03c, '', 0 from #gcm10 a where a.gcl19c >= @fromempl and a.gcl19c <= @toempl and (a.gcl06i = 1 or a.gcl06i = 5) order by gcl19c, gcl80d --第4销售销售取款,欠款 insert #empl_stat(person_id, action_id, srvdate, code, name, amt, amt_vperf, srvtime, custid, custname, num) select distinct gcl22c, 1, gcl80d, gcl03c, '', 0 - gcl07f, isnull(gcl23f, 0)*(0 - gcl07f), gcl56t, gcl03c, '', 0 from #gcm10 a where a.gcl22c >= @fromempl and a.gcl22c <= @toempl and (a.gcl06i = 1 or a.gcl06i = 5) order by gcl22c, gcl80d --第5销售销售取款,欠款 insert #empl_stat(person_id, action_id, srvdate, code, name, amt, amt_vperf, srvtime, custid, custname, num) select distinct gcl24c, 1, gcl80d, gcl03c, '', 0 - gcl07f, isnull(gcl25f, 0)*(0 - gcl07f), gcl56t, gcl03c, '', 0 from #gcm10 a where a.gcl24c >= @fromempl and a.gcl24c <= @toempl and (a.gcl06i = 1 or a.gcl06i = 5) order by gcl24c, gcl80d --第6销售销售取款,欠款 insert #empl_stat(person_id, action_id, srvdate, code, name, amt, amt_vperf, srvtime, custid, custname, num) select distinct gcl26c, 1, gcl80d, gcl03c, '', 0 - gcl07f, isnull(gcl27f, 0)*(0 - gcl07f), gcl56t, gcl03c, '', 0 from #gcm10 a where a.gcl26c >= @fromempl and a.gcl26c <= @toempl and (a.gcl06i = 1 or a.gcl06i = 5) order by gcl26c, gcl80d --第7销售销售取款,欠款 insert #empl_stat(person_id, action_id, srvdate, code, name, amt, amt_vperf, srvtime, custid, custname, num) select distinct gcl28c, 1, gcl80d, gcl03c, '', 0 - gcl07f, isnull(gcl29f, 0)*(0 - gcl07f), gcl56t, gcl03c, '', 0 from #gcm10 a where a.gcl28c >= @fromempl and a.gcl28c <= @toempl and (a.gcl06i = 1 or a.gcl06i = 5) order by gcl28c, gcl80d --第8销售销售取款,欠款 insert #empl_stat(person_id, action_id, srvdate, code, name, amt, amt_vperf, srvtime, custid, custname, num) select distinct gcl30c, 1, gcl80d, gcl03c, '', 0 - gcl07f, isnull(gcl31f, 0)*(0 - gcl07f), gcl56t, gcl03c, '', 0 from #gcm10 a where a.gcl30c >= @fromempl and a.gcl30c <= @toempl and (a.gcl06i = 1 or a.gcl06i = 5) order by gcl30c, gcl80d --第9销售销售取款,欠款 insert #empl_stat(person_id, action_id, srvdate, code, name, amt, amt_vperf, srvtime, custid, custname, num) select distinct gcl32c, 1, gcl80d, gcl03c, '', 0 - gcl07f, isnull(gcl33f, 0)*(0 - gcl07f), gcl56t, gcl03c, '', 0 from #gcm10 a where a.gcl32c >= @fromempl and a.gcl32c <= @toempl and (a.gcl06i = 1 or a.gcl06i = 5) order by gcl32c, gcl80d --第10销售销售取款,欠款 insert #empl_stat(person_id, action_id, srvdate, code, name, amt, amt_vperf, srvtime, custid, custname, num) select distinct gcl34c, 1, gcl80d, gcl03c, '', 0 - gcl07f, isnull(gcl35f, 0)*(0 - gcl07f), gcl56t, gcl03c, '', 0 from #gcm10 a where a.gcl34c >= @fromempl and a.gcl34c <= @toempl and (a.gcl06i = 1 or a.gcl06i = 5) order by gcl34c, gcl80d update a set a.name = c.gak02c from #empl_stat a, gcm01 b, gam10 c where a.code = b.gca01c and b.gca00c = b.gca13d--@compid and b.gca00c = c.gak00c and b.gca02c = c.gak01c and (a.action_id = 1 or a.action_id = 0) drop table #gcm10 create table #ggm01_ggm02 -- 项目消费单 ( gga00c varchar(10) not null, --公司编号 gga01c varchar(20) not null, --消费单号 gga02d varchar(8) null, --消费日期 gga03t varchar(6) null, --消费开始时间 gga04t varchar(6) null, --消费结束时间 gga05c varchar(20) null, --会员代码, 927版本 gga06c varchar(40) null, --客户姓名,如果是会员则是会员姓名 gga07c varchar(10) null, --项目负责人员(暂不用) gga08c varchar(20) null, --匙牌号 gga09c varchar(20) null, --会员卡号 gga10c varchar(5) null, --币别(不用) gga11f float null, --汇率(不用) gga12c varchar(5) null, --计价单位(不用) gga13f float null, --是否为老客 gga14f float null, --客人数量 gga15f float null, --项目基本金额(不用) gga16f float null, --总金额 gga17f float null, --(销售点)已支付金额 gga18f float null, --折扣 gga19f float null, --折扣单价 gga20c varchar(5) null, --预约单别 gga21c varchar(20) null, --预约单号 --壁球入馆消费专用 begin gga22c varchar(5) null, --客户类别(已不用) gga23c varchar(5) null, --证件类别(已不用) gga24c varchar(20) null, --证件编号(已不用) gga25c varchar(20) null, --赠券编号(已不用) gga26f float null, --预付金额(已不用) gga27i int null, --账户类别, gga28c varchar(10) null, --接待人员(已不用) --壁球入馆消费专用 end gga29c varchar(5) null, --单据类别 gga30c varchar(20) null, --入馆单号 gga31i int null, --顾客性别 gga91c varchar(10) null, --登陆人员代码 gga92d varchar(8) null, --登陆日期 gga93c varchar(10) null, --覆核人员代码 gga94d varchar(8) null, --覆核日期 gga95c varchar(10) null, --结账人员(壁球)(不用) gga96d varchar(8) null, --结账日期(壁球)(不用) gga80d varchar(8) null, --帐务日期, added by WUXM, 2005/08/15 gga81i int null, --是否寄存 ggb00c varchar(10) not null, --公司编号 ggb01c varchar(20) not null, --消费单号 ggb02f float not null, --序号 ggb03c varchar(20) null, --子项目代码 ggb04c varchar(5) null, --单位 ggb05f float null, --数量 ggb06c varchar(5) null, --币别 ggb07f float null, --汇率 ggb08f float null, --标准单价 ggb09f float null, --折让 ggb10f float null, --折让单价 ggb11f float null, --金额 ggb12c varchar(20) null, --大工代码 ggb13c varchar(20) null, --中工代码 ggb14c varchar(20) null, --小工代码 ggb15c varchar(20) null, --大工类型 ggb16c varchar(20) null, --中工类型 ggb17f float null, --成本金额 ggb18f float null, --转卡比率 ggb19c varchar(20) null, --小工类型 ggb20f float null, --大工虚业绩 ggb21f float null, --中工虚业绩 ggb22f float null, --小工虚业绩 ggb23c varchar(128) null, --项目打折原因, added by WUXM, 2005/09/01 ggb24f float null, --项目应收金额, added by WUXM, 2005/09/08 ggb25c varchar(20) null, --四工类型 ggb26c varchar(20) null, --四工代码 ggb27c varchar(5) null, --项目支付方式 Added by WUXM 2005/11/09 ggb28f float null, --大工提成 Added by WUXM 2005/11/10 ggb29f float null, --中工提成 Added by WUXM 2005/11/10 ggb30f float null, --小工提成 Added by WUXM 2005/11/10 ggb31c varchar(20) null, --五工类别 ggb32c varchar(20) null, --五工代码 ggb33f float null, --项目积分 ggb34f float null, --项目的折算率 ggb35f float null --四工虚业绩 ) insert #ggm01_ggm02(gga00c, gga01c, gga02d, gga03t, gga04t, gga05c, gga06c, gga07c, gga08c, gga09c, gga10c, gga11f, gga12c, gga13f, gga14f, gga15f, gga16f, gga17f, gga18f, gga19f, gga20c, gga21c, gga22c, gga23c, gga24c, gga25c, gga26f, gga27i, gga28c, gga29c, gga30c, gga31i, gga91c, gga92d, gga93c, gga94d, gga95c, gga96d, gga80d, gga81i, ggb00c, ggb01c, ggb02f, ggb03c, ggb04c, ggb05f, ggb06c, ggb07f, ggb08f, ggb09f, ggb10f, ggb11f, ggb12c, ggb13c, ggb14c, ggb15c, ggb16c, ggb17f, ggb18f, ggb19c, ggb20f, ggb21f, ggb22f, ggb23c, ggb24f, ggb25c, ggb26c, ggb27c, ggb28f , ggb29f, ggb30f, ggb31c, ggb32c, ggb33f, ggb34f, ggb35f) select gga00c, gga01c, gga02d, gga03t, gga04t, gga05c, gga06c, gga07c, gga08c, gga09c, gga10c, gga11f, gga12c, gga13f, gga14f, gga15f, gga16f, gga17f, gga18f, gga19f, gga20c, gga21c, gga22c, gga23c, gga24c, gga25c, gga26f, gga27i, gga28c, gga29c, gga30c, gga31i, gga91c, gga92d, gga93c, gga94d, gga95c, gga96d, gga80d, gga81i, ggb00c, ggb01c, ggb02f, ggb03c, ggb04c, ggb05f, ggb06c, ggb07f, ggb08f, ggb09f, ggb10f, ggb11f, ggb12c, ggb13c, ggb14c, ggb15c, ggb16c, ggb17f, ggb18f, ggb19c, ggb20f, ggb21f, ggb22f, ggb23c, ggb24f, ggb25c, ggb26c, ggb27c, ggb28f, ggb29f, ggb30f, ggb31c, ggb32c, ggb33f, ggb34f, ggb35f from ggm01 a with (nolock), ggm02 b with (nolock) where a.gga00c = b.ggb00c and a.gga01c = b.ggb01c and a.gga00c = @compid and a.gga80d >= @fromdate and a.gga80d <= @todate --项目服务 --大工数据,已经计算虚业绩与成本 insert #empl_stat(person_id, action_id, srvdate, code, name, payway, amt, amt_vperf, srvtime, custid, custname, num, cost) select distinct ggb12c, isnull(convert(int, isnull(ggb15c, '2')), 1) + 1, gga80d, ggb03c, '', ggb27c, ggb11f, ggb20f, gga04t, gga09c, gga06c, ggb05f, isnull(ggb17f, 0)*isnull(c.hea29i, 1) from #ggm01_ggm02 b, hem01 c where b.ggb12c >= @fromempl and b.ggb12c <= @toempl and isnull(b.ggb12c, '')<>'' and b.ggb15c <> '3' and b.ggb00c = c.hea00c and b.ggb03c = c.hea01c and c.hea18i = 2 and (c.hea34c = b.ggb27c or isnull(c.hea34c, '*') = '*') --added by cyr 20130221 order by ggb12c, gga80d --大工返工 --成本为0 insert #empl_stat(person_id, action_id, srvdate, code, name, payway, amt, amt_vperf, srvtime, custid, custname, num) select distinct ggb12c, isnull(convert(int, isnull(ggb15c, '2')), 1) + 1, gga80d, ggb03c, '', ggb27c, 0-ggb11f, 0-ggb20f, gga04t, gga09c, gga06c, ggb05f from #ggm01_ggm02 b where b.ggb12c >= @fromempl and b.ggb12c <= @toempl and isnull(b.ggb12c, '')<>'' and b.ggb15c = '3' order by ggb12c, gga80d --中工数据,已经计算虚业绩与成本 insert #empl_stat(person_id, action_id, srvdate, code, name, payway, amt, amt_vperf, srvtime, custid, custname, num, cost) select distinct ggb13c, isnull(convert(int, isnull(ggb16c, '2')), 1) + 4, gga80d, ggb03c, '', ggb27c, ggb11f, ggb21f, gga04t, gga09c, gga06c, ggb05f, isnull(ggb17f, 0)*isnull(c.hea30i, 1) from #ggm01_ggm02 b, hem01 c where b.ggb13c >= @fromempl and b.ggb13c <= @toempl and isnull(b.ggb13c, '')<>'' and b.ggb16c <> '3' and b.ggb00c = c.hea00c and b.ggb03c = c.hea01c and c.hea18i = 2 and (c.hea34c = b.ggb27c or isnull(c.hea34c, '*') = '*') --added by cyr 20130221 order by ggb13c, gga80d insert #empl_stat(person_id, action_id, srvdate, code, name, payway, amt, amt_vperf, srvtime, custid, custname, num) select distinct ggb13c, 6, gga80d, ggb03c, '', ggb27c, 0-ggb11f, 0-ggb21f, gga04t, gga09c, gga06c, ggb05f from #ggm01_ggm02 b where b.ggb13c >= @fromempl and b.ggb13c <= @toempl and isnull(b.ggb13c, '')<>'' and b.ggb16c = '3' order by ggb13c, gga80d --小工数据,已经计算虚业绩与成本 insert #empl_stat(person_id, action_id, srvdate, code, name, payway, amt, amt_vperf, srvtime, custid, custname, num, cost) select distinct ggb14c, isnull(convert(int, isnull(ggb19c, '2')), 1) + 6, gga80d, ggb03c, '', ggb27c, ggb11f, ggb22f, gga04t, gga09c, gga06c, ggb05f, isnull(ggb17f, 0)*isnull(c.hea31i, 1) from #ggm01_ggm02 b, hem01 c where b.ggb14c >= @fromempl and b.ggb14c <= @toempl and isnull(b.ggb14c, '')<>'' and b.ggb19c <> '3' and b.ggb00c = c.hea00c and b.ggb03c = c.hea01c and c.hea18i = 2 and (c.hea34c = b.ggb27c or isnull(c.hea34c, '*') = '*') --added by cyr 20130221 order by ggb14c, gga80d insert #empl_stat(person_id, action_id, srvdate, code, name, payway, amt, amt_vperf, srvtime, custid, custname, num) select distinct ggb14c, 8, gga80d, ggb03c, '', ggb27c, 0-ggb11f, 0-ggb22f, gga04t, gga09c, gga06c, ggb05f from #ggm01_ggm02 b where b.ggb14c >= @fromempl and b.ggb14c <= @toempl and isnull(b.ggb14c, '')<>'' and b.ggb19c = '3' order by ggb14c, gga80d --四工 insert #empl_stat(person_id, action_id, srvdate, code, name, payway, amt, amt_vperf, srvtime, custid, custname, num, cost) select distinct ggb26c, isnull(convert(int, isnull(ggb25c, '2')), 1) + 9, gga80d, ggb03c, '', ggb27c, ggb11f, ggb35f, gga04t, gga09c, gga06c, ggb05f, isnull(ggb17f, 0)*isnull(c.hea32i, 1) from #ggm01_ggm02 b, hem01 c where b.ggb26c >= @fromempl and b.ggb26c <= @toempl and isnull(b.ggb26c, '')<>'' and b.ggb25c <> '3' and b.ggb00c = c.hea00c and b.ggb03c = c.hea01c and c.hea18i = 2 and (c.hea34c = b.ggb27c or isnull(c.hea34c, '*') = '*') --added by cyr 20130221 order by ggb26c, gga80d update a set a.name = b.gda03c from #empl_stat a, gdm01 b where a.code = b.gda01c and b.gda00c = @compid and ((action_id >= 2 and action_id <= 8) or action_id = 10 or action_id = 11) drop table #ggm01_ggm02 create table #ggm01_ggm03 -- 项目消费单 ( gga00c varchar(10) not null, --公司编号 gga01c varchar(20) not null, --消费单号 gga02d varchar(8) null, --消费日期 gga03t varchar(6) null, --消费开始时间 gga04t varchar(6) null, --消费结束时间 gga05c varchar(20) null, --会员代码, 927版本 gga06c varchar(40) null, --客户姓名,如果是会员则是会员姓名 gga07c varchar(10) null, --项目负责人员(暂不用) gga08c varchar(20) null, --匙牌号 gga09c varchar(20) null, --会员卡号 gga10c varchar(5) null, --币别(不用) gga11f float null, --汇率(不用) gga12c varchar(5) null, --计价单位(不用) gga13f float null, --是否为老客 gga14f float null, --客人数量 gga15f float null, --项目基本金额(不用) gga16f float null, --总金额 gga17f float null, --(销售点)已支付金额 gga18f float null, --折扣 gga19f float null, --折扣单价 gga20c varchar(5) null, --预约单别 gga21c varchar(20) null, --预约单号 gga22c varchar(5) null, --客户类别(已不用) gga23c varchar(5) null, --证件类别(已不用) gga24c varchar(20) null, --证件编号(已不用) gga25c varchar(20) null, --赠券编号(已不用) gga26f float null, --预付金额(已不用) gga27i int null, --账户类别, gga28c varchar(10) null, --接待人员(已不用) gga29c varchar(5) null, --单据类别 gga30c varchar(20) null, --入馆单号 gga31i int null, --顾客性别 gga91c varchar(10) null, --登陆人员代码 gga92d varchar(8) null, --登陆日期 gga93c varchar(10) null, --覆核人员代码 gga94d varchar(8) null, --覆核日期 gga95c varchar(10) null, --结账人员(壁球)(不用) gga96d varchar(8) null, --结账日期(壁球)(不用) gga80d varchar(8) null, --帐务日期, added by WUXM, 2005/08/15 gga81i int null, --是否寄存 ggc00c varchar(10) not null, --公司编号 ggc01c varchar(20) not null, --消费单号 ggc02f float not null, --序号 ggc03c varchar(20) null, --产品代码 ggc04c varchar(5) null, --单位 ggc05f float null, --数量 ggc06c varchar(5) null, --币别 ggc07f float null, --汇率 ggc08f float null, --标准单价 ggc09f float null, --折让 ggc10f float null, --折让单价 ggc11f float null, --金额 ggc12c varchar(20) null, --产品销售人员 ggc13f float null, --成本金额 ggc14f float null, --第一分享比率 ggc15c varchar(20) null, --第二人 ggc16f float null, --第二分享比率 ggc17c varchar(20) null, --第三人 ggc18f float null, --第三分享比率 ggc19c varchar(128) null, --产品打折原因, added by WUXM, 2005/09/01 ggc20f float null, --产品应收金额, added by WUXM, 2005/09/08 ggc21f float null, --第一销售虚业绩 Added by WUXM 2005/11/10 ggc22f float null, --第一销售提成 ggc23f float null, --第二销售虚业绩 Added by WUXM 2005/11/10 ggc24f float null, --第二销售提成 ggc25f float null, --第三销售虚业绩 Added by WUXM 2005/11/10 ggc26f float null, --第三销售提成 ggc27c varchar(5) null, --产品支付方式 ggc28f float null, --产品积分 ggc29f float null, --产品折算比率 ggc30f float null, --实际营业额折算比率 ggc81c varchar(10) null --部门编号 ) insert #ggm01_ggm03(gga00c, gga01c, gga02d, gga03t, gga04t, gga05c, gga06c, gga07c, gga08c, gga09c, gga10c, gga11f, gga12c, gga13f, gga14f, gga15f, gga16f, gga17f, gga18f, gga19f, gga20c, gga21c, gga22c, gga23c, gga24c, gga25c, gga26f, gga27i, gga28c, gga29c, gga30c, gga31i, gga91c, gga92d, gga93c, gga94d, gga95c, gga96d, gga80d, gga81i, ggc00c, ggc01c, ggc02f, ggc03c, ggc04c, ggc05f, ggc06c, ggc07f, ggc08f, ggc09f, ggc10f, ggc11f, ggc12c, ggc13f, ggc14f, ggc15c, ggc16f, ggc17c, ggc18f, ggc19c, ggc20f, ggc21f, ggc22f, ggc23f, ggc24f, ggc25f, ggc26f, ggc27c, ggc28f, ggc29f, ggc30f) select gga00c, gga01c, gga02d, gga03t, gga04t, gga05c, gga06c, gga07c, gga08c, gga09c, gga10c, gga11f, gga12c, gga13f, gga14f, gga15f, gga16f, gga17f, gga18f, gga19f, gga20c, gga21c, gga22c, gga23c, gga24c, gga25c, gga26f, gga27i, gga28c, gga29c, gga30c, gga31i, gga91c, gga92d, gga93c, gga94d, gga95c, gga96d, gga80d, gga81i, ggc00c, ggc01c, ggc02f, ggc03c, ggc04c, ggc05f, ggc06c, ggc07f, ggc08f, ggc09f, ggc10f, ggc11f, ggc12c, ggc13f, ggc14f, ggc15c, ggc16f, ggc17c, ggc18f, ggc19c, ggc20f, ggc21f, ggc22f, ggc23f, ggc24f, ggc25f, ggc26f, ggc27c, ggc28f, ggc29f, ggc30f from ggm01 a with (nolock), ggm03 b with (nolock) where a.gga00c = b.ggc00c and a.gga01c = b.ggc01c and a.gga00c = @compid and a.gga80d >= @fromdate and a.gga80d <= @todate --产品 --卖产品第一销售 insert #empl_stat(person_id, action_id, srvdate, code, name, payway, amt, amt_vperf, srvtime, custid, custname, num, cost) select distinct ggc12c, 9, gga80d, ggc03c, '', ggc27c, ggc11f, ggc21f, gga04t, gga09c, gga06c, ggc05f, isnull(ggc07f, 0) from #ggm01_ggm03 b where b.ggc12c >= @fromempl and b.ggc12c <= @toempl order by ggc12c, gga80d --卖产品第二销售 insert #empl_stat(person_id, action_id, srvdate, code, name, payway, amt, amt_vperf, srvtime, custid, custname, num, cost) select distinct ggc15c, 9, gga80d, ggc03c, '', ggc27c, ggc11f, ggc23f, gga04t, gga09c, gga06c, ggc05f, isnull(ggc07f, 0) from #ggm01_ggm03 b where b.ggc15c >= @fromempl and b.ggc15c <= @toempl order by ggc15c, gga80d --卖产品第三销售 insert #empl_stat(person_id, action_id, srvdate, code, name, payway, amt, amt_vperf, srvtime, custid, custname, num, cost) select distinct ggc17c, 9, gga80d, ggc03c, '', ggc27c, ggc11f, ggc25f, gga04t, gga09c, gga06c, ggc05f, isnull(ggc07f, 0) from #ggm01_ggm03 b where b.ggc17c >= @fromempl and b.ggc17c <= @toempl order by ggc17c, gga80d drop table #ggm01_ggm03 update a set a.name = b.gfa03c from #empl_stat a, gfm01 b where a.code = b.gfa01c and b.gfa00c = @compid and action_id = 9 update a set a.payway = b.gsb03c from #empl_stat a, gsm02 b where a.payway = b.gsb02c and b.gsb00c = @compid and b.gsb01c = '3' and ((action_id >= 2 and action_id <= 9) or action_id = 10 or action_id = 11) update a set a.custname = b.gba03c from #empl_stat a, gbm01 b where a.custid = b.gba01c and b.gba00c = @compid and isnull(custname, '') = '' select * from #empl_stat order by person_id, action_id drop table #empl_stat end GO /****** Object: StoredProcedure [dbo].[upg_salary_report] Script Date: 06/12/2017 06:20:38 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_salary_report] ( @compid varchar(10), @billid varchar(20), @status varchar(5) ) as--wuxm, 2004/02/27 begin declare @itemid varchar(10) declare @sql1 nvarchar(max) declare @sql2 nvarchar(max) declare @gsa03f int set @sql1 = 'select isnull(haa01c,'''') personid, isnull(haa02c,'''') person, ' select @gsa03f = isnull(gsa03f,0) from gsm01 where gsa00c = @compid and gsa01c = 'SP006' set @sql2 = '' declare cur_each_item cursor for select gsb02c from gsm02 with(nolock) where gsb00c = @compid and gsb01c = 'A9' open cur_each_item fetch cur_each_item into @itemid while @@fetch_status = 0 begin set @sql2 = @sql2 + ' item' + @itemid + ' = cast(isnull((select top 1 (case when hcc03c = ''2'' then -1 else 1 end) * isnull(hcc09f, 0) from hcm03 with(nolock) where hcc02c = haa01c and hcc00c = ''' + @compid + ''' and hcc01c = ''' + @billid + ''' and hcc04c = ''' + @itemid + ''' ),'''') as decimal(18,'+convert(varchar(10),@gsa03f)+')), ' fetch cur_each_item into @itemid end close cur_each_item deallocate cur_each_item set @sql1 = @sql1 + ' ' + @sql2 + ' isnull(haa27c,'''') status from ham01 with(nolock) where haa00c = ''' + @compid + '''' set @sql1 = @sql1 + ' and haa27c <> ''' + @status + '''' exec sp_executesql @sql1 end GO /****** Object: StoredProcedure [dbo].[upg_stst_proc_prod_sale_ex] Script Date: 06/12/2017 06:20:46 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_stst_proc_prod_sale_ex] ( @compid varchar(10), -- 一次显示一个公司,包括下面的数据的合计 @fromdate varchar(20), @todate varchar(20), @outtype int, -- 输出类型 0-销售 1-销退 2-合并 @includecombo int -- 是否包括套餐 ) as begin declare @payment_zp varchar(10) -- 支票的支付方式 -- 判断是否为收入项目 declare @is_revenue varchar(10) select @is_revenue = gsa02c from gsm01 with(nolock) where gsa00c = @compid and gsa01c = 'SP063' if @is_revenue = '1' begin select @payment_zp = '$' end else begin select @payment_zp = 'ZZ' -- 无此支付方式 end create table #result_detail --疗程以及产品的销售临时表 ( iid int identity not null, compid varchar(10) null, --公司编号 cardid varchar(20) null, --会员卡号 memname varchar(20) null, --会员名称 billid varchar(20) null, --单据编号 billdate varchar(8) null, --单据日期 res05c varchar(100) null, --产品名称 res06f float null, --产品数量 res07f float null, --产品金额 res08c varchar(50) null, --美容名称 res09f float null, --美容数量 res10f float null, --美容次数 res11f float null, --美容金额 res12c varchar(50) null, --美体名称 res13f float null, --美体数量 res14f float null, --美体次数 res15f float null, --美体金额 res16c varchar(50) null, --仪器名称 res17f float null, --仪器数量 res18f float null, --仪器次数 res19f float null, --仪器金额 saveamt float null, --划减金额 res21i int null default 0, --是否为合计 1、是,0否 billtype varchar(5) null, --单据类别 cash float null, --现金 pos float null, --银行卡 transamt float null, --转销售 checkamt float null, --支票的金额 ) declare @PK_Sql nvarchar(max) set @PK_Sql = ' alter table #result_detail add constraint PK_#result_detail_' + convert(varchar(10), cast(floor(rand() * 1000000) as int)) + ' PRIMARY KEY NonCLUSTERED(iid)' exec sp_executesql @PK_Sql create clustered index idx_result_detail on #result_detail(compid, cardid) -- 最后分组后的结果 create table #result_final --疗程以及产品的销售临时表 ( iid int identity not null, compid varchar(10) null, --公司编号 cardid varchar(20) null, --会员卡号 memname varchar(40) null, --会员名称 billid varchar(20) null, --单据编号 billdate varchar(8) null, --单据日期 res05c varchar(100) null, --产品名称 res06f float null, --产品数量 res07f float null, --产品金额 res08c varchar(50) null, --美容名称 res09f float null, --美容数量 res10f float null, --美容次数 res11f float null, --美容金额 res12c varchar(50) null, --美体名称 res13f float null, --美体数量 res14f float null, --美体次数 res15f float null, --美体金额 res16c varchar(50) null, --仪器名称 res17f float null, --仪器数量 res18f float null, --仪器次数 res19f float null, --仪器金额 saveamt float null, --划减金额 res21i int null default 0, --是否为合计 1、是,0否 billtype varchar(5) null, --单据类别 cash float null, --现金 pos float null, --银行卡 transamt float null, --转销售 checkamt float null, --支票的金额 ) set @PK_Sql = ' alter table #result_final add constraint PK_#result_final_' + convert(varchar(10), cast(floor(rand() * 1000000) as int)) + ' PRIMARY KEY NonCLUSTERED(iid)' exec sp_executesql @PK_Sql create clustered index idx_result_final on #result_final(compid, cardid) create table #gcm08_buffer -- 会员卡--疗程变化历史 ( gch00c varchar(10) not null, --公司编号 gch01i int null, -- 序号 gch02d varchar(8) null, -- 日期 gch03c varchar(20) not null, --卡号 gch04c varchar(20) not null, --疗程项目编号 gch05f float null, --购买次数 gch06f float null, --赠送次数 gch07f float null, --购买金额 gch08f float null, --使用次数 gch09f float null, --已经使用金额 gch10c varchar(20) null, --单据类别 gch11c varchar(20) null, --单号 gch12i int null, --价格序号 gch13f float null, --折扣 gch14f float null, --购买数量 gch15f float null, --单价 gch16i int null, --疗程中的序号 cash float null, --现金 pos float null, --银行卡 transamt float null, --转销售 checkamt float null, --支票的金额 ) create clustered index idx_gcm08_buffer on #gcm08_buffer(gch00c, gch04c, gch10c) -- gcm08 添加满足条件的数据到#gcm08_buffer if @outtype = 2 begin insert #gcm08_buffer(gch00c, gch01i, gch02d, gch03c, gch04c, gch05f, gch06f, gch07f, gch08f, gch09f, gch10c, gch11c, gch12i, gch13f, gch14f, gch15f, gch16i, cash, pos, transamt, checkamt) select gch00c, gch01i, gch02d, gch03c, gch04c, gch05f, gch06f, (case when gch17c in ('1', '6', 'ZFB', '&', 'WX', @payment_zp) then gch18f else 0 end + case when gch19c in ('1', '6', 'ZFB', '&', 'WX', @payment_zp) then gch20f else 0 end + case when gch21c in ('1', '6', 'ZFB', '&', 'WX', @payment_zp) then gch22f else 0 end + case when gch27c in ('1', '6', 'ZFB', '&', 'WX', @payment_zp) then gch28f else 0 end), gch08f, gch09f, gch10c, gch11c, gch12i, gch13f, gch14f, gch15f, gch16i, (case gch17c when '1' then gch18f else 0 end + case gch19c when '1' then gch20f else 0 end + case gch21c when '1' then gch22f else 0 end + case gch27c when '1' then gch28f else 0 end ), (case gch17c when '6' then gch18f else 0 end + case gch19c when '6' then gch20f else 0 end + case gch21c when '6' then gch22f else 0 end + case gch27c when '6' then gch28f else 0 end ), (case gch17c when '&' then gch18f else 0 end + case gch19c when '&' then gch20f else 0 end + case gch21c when '&' then gch22f else 0 end + case gch27c when '&' then gch28f else 0 end ), (case gch17c when @payment_zp then gch18f else 0 end + case gch19c when @payment_zp then gch20f else 0 end + case gch21c when @payment_zp then gch22f else 0 end + case gch27c when @payment_zp then gch28f else 0 end ) from gcm08 a with (nolock), gam26 z with(nolock) where gch00c = z.gaz02c and z.gaz01c = @compid and gch02d >= @fromdate and gch02d <= @todate and (gch10c = 'gn' or gch10c = 'gz' or gch10c = 'gx') and ((isnull(gch05f, 0) + isnull(gch06f, 0) <> 0) or isnull(gch07f, 0) > 0) --考虑单次的项目 insert #gcm08_buffer(gch00c, gch02d, gch03c, gch04c, gch05f, gch07f, gch10c, gch11c, gch12i, gch13f, gch14f, gch15f, gch16i, cash, pos, transamt, checkamt) select ggb00c, gga02d, gga09c, ggb03c, ggb05f, ggb11f, 'gx', gga01c, 0, ggb09f, ggb05f, ggb10f, 0, (case ggb27c when '1' then ggb11f else 0 end), (case ggb27c when '6' then ggb11f else 0 end), (case ggb27c when '&' then ggb11f else 0 end), (case ggb27c when @payment_zp then ggb11f else 0 end) from ggm01 a with (nolock), ggm02 b with (nolock), gam26 z with(nolock) where a.gga00c = b.ggb00c and a.gga01c = b.ggb01c and a.gga02d >= @fromdate and a.gga02d <= @todate and a.gga00c = z.gaz02c and z.gaz01c = @compid and b.ggb27c in ('1', '6', 'ZFB', '&', 'WX', @payment_zp) and isnull(ggb11f, 0)>0 if @includecombo = 1 begin insert #gcm08_buffer(gch00c, gch01i, gch02d, gch03c, gch04c, gch05f, gch07f, gch10c, gch11c, gch12i, gch13f, gch14f, gch15f, gch16i, cash, pos, transamt, checkamt) select gti00c, gti01i, gti02d, gti03c, gti05c, gti06f, (case when gti17c in ('1', '6', 'ZFB', '&', @payment_zp) then gti18f else 0 end + case when gti19c in ('1', '6', 'ZFB', '&', 'WX', @payment_zp) then gti20f else 0 end + case when gti21c in ('1', '6', 'ZFB', '&', 'WX', @payment_zp) then gti22f else 0 end + case when gti23c in ('1', '6', 'ZFB', '&', 'WX', @payment_zp) then gti24f else 0 end), gti08c, gti09c, 0, 1, 1, 0, 0, (case gti17c when '1' then gti18f else 0 end + case gti19c when '1' then gti20f else 0 end + case gti21c when '1' then gti22f else 0 end + case gti23c when '1' then gti24f else 0 end ), (case gti17c when '6' then gti18f else 0 end + case gti19c when '6' then gti20f else 0 end + case gti21c when '6' then gti22f else 0 end + case gti23c when '6' then gti24f else 0 end ), (case gti17c when '&' then gti18f else 0 end + case gti19c when '&' then gti20f else 0 end + case gti21c when '&' then gti22f else 0 end + case gti23c when '&' then gti24f else 0 end ), (case gti17c when @payment_zp then gti18f else 0 end + case gti19c when @payment_zp then gti20f else 0 end + case gti21c when @payment_zp then gti22f else 0 end + case gti23c when @payment_zp then gti24f else 0 end ) from gtc07 a with (nolock), gam26 z with(nolock) where gti00c = z.gaz02c and z.gaz01c = @compid and gti02d >= @fromdate and gti02d <= @todate and (gti08c = 'gn' or gti08c = 'gz') and gti05c = gti13c end end else if @outtype = 0 begin insert #gcm08_buffer(gch00c, gch01i, gch02d, gch03c, gch04c, gch05f, gch06f, gch07f, gch08f, gch09f, gch10c, gch11c, gch12i, gch13f, gch14f, gch15f, gch16i, cash, pos, transamt, checkamt) select gch00c, gch01i, gch02d, gch03c, gch04c, gch05f, gch06f, (case when gch17c in ('1', '6', 'ZFB', '&', 'WX', @payment_zp) then gch18f else 0 end + case when gch19c in ('1', '6', 'ZFB', '&', 'WX', @payment_zp) then gch20f else 0 end + case when gch21c in ('1', '6', 'ZFB', '&', 'WX', @payment_zp) then gch22f else 0 end + case when gch27c in ('1', '6', 'ZFB', '&', 'WX', @payment_zp) then gch28f else 0 end), gch08f, gch09f, gch10c, gch11c, gch12i, gch13f, gch14f, gch15f, gch16i, (case gch17c when '1' then gch18f else 0 end + case gch19c when '1' then gch20f else 0 end + case gch21c when '1' then gch22f else 0 end + case gch27c when '1' then gch28f else 0 end ), (case gch17c when '6' then gch18f else 0 end + case gch19c when '6' then gch20f else 0 end + case gch21c when '6' then gch22f else 0 end + case gch27c when '6' then gch28f else 0 end ), (case gch17c when '&' then gch18f else 0 end + case gch19c when '&' then gch20f else 0 end + case gch21c when '&' then gch22f else 0 end + case gch27c when '&' then gch28f else 0 end), (case gch17c when @payment_zp then gch18f else 0 end + case gch19c when @payment_zp then gch20f else 0 end + case gch21c when @payment_zp then gch22f else 0 end + case gch27c when @payment_zp then gch28f else 0 end ) from gcm08 a with (nolock), gam26 z with(nolock) where gch00c = z.gaz02c and z.gaz01c = @compid and gch02d >= @fromdate and gch02d <= @todate and (gch10c = 'gn' or gch10c = 'gz' or gch10c = 'gx') and ((isnull(gch05f, 0) + isnull(gch06f, 0)>0) or isnull(gch07f, 0)>0) --考虑单次的项目 insert #gcm08_buffer(gch00c, gch02d, gch03c, gch04c, gch05f, gch07f, gch10c, gch11c, gch12i, gch13f, gch14f, gch15f, gch16i, cash, pos, transamt, checkamt) select ggb00c, gga02d, gga09c, ggb03c, ggb05f, ggb11f, 'gx', gga01c, 0, ggb09f, ggb05f, ggb10f, 0, (case ggb27c when '1' then ggb11f else 0 end), (case ggb27c when '6' then ggb11f else 0 end), (case ggb27c when '&' then ggb11f else 0 end), (case ggb27c when @payment_zp then ggb11f else 0 end) from ggm01 a with (nolock), ggm02 b with (nolock), gam26 z with(nolock) where a.gga00c = b.ggb00c and a.gga01c = b.ggb01c and a.gga02d >= @fromdate and a.gga02d <= @todate and a.gga00c = z.gaz02c and z.gaz01c = @compid and b.ggb27c in ('1', '6', 'ZFB', '&', 'WX', @payment_zp) and isnull(ggb05f, 0)>0 and isnull(ggb11f, 0)>0 if @includecombo = 1 begin insert #gcm08_buffer(gch00c, gch01i, gch02d, gch03c, gch04c, gch05f, gch07f, gch10c, gch11c, gch12i, gch13f, gch14f, gch15f, gch16i, cash, pos, transamt, checkamt) select gti00c, gti01i, gti02d, gti03c, gti05c, gti06f, (case when gti17c in ('1', '6', 'ZFB', '&', @payment_zp) then gti18f else 0 end + case when gti19c in ('1', '6', 'ZFB', '&', 'WX', @payment_zp) then gti20f else 0 end + case when gti21c in ('1', '6', 'ZFB', '&', 'WX', @payment_zp) then gti22f else 0 end + case when gti23c in ('1', '6', 'ZFB', '&', 'WX', @payment_zp) then gti24f else 0 end), gti08c, gti09c, 0, 1, 1, 0, 0, (case gti17c when '1' then gti18f else 0 end + case gti19c when '1' then gti20f else 0 end + case gti21c when '1' then gti22f else 0 end + case gti23c when '1' then gti24f else 0 end ), (case gti17c when '6' then gti18f else 0 end + case gti19c when '6' then gti20f else 0 end + case gti21c when '6' then gti22f else 0 end + case gti23c when '6' then gti24f else 0 end), (case gti17c when '&' then gti18f else 0 end + case gti19c when '&' then gti20f else 0 end + case gti21c when '&' then gti22f else 0 end + case gti23c when '&' then gti24f else 0 end), (case gti17c when @payment_zp then gti18f else 0 end + case gti19c when @payment_zp then gti20f else 0 end + case gti21c when @payment_zp then gti22f else 0 end + case gti23c when @payment_zp then gti24f else 0 end) from gtc07 a with (nolock), gam26 z with(nolock) where gti00c = z.gaz02c and z.gaz01c = @compid and gti02d >= @fromdate and gti02d <= @todate and (gti08c = 'gn' or gti08c = 'gz') and gti05c = gti13c and isnull(gti06f, 0)>0 end end else if @outtype = 1 begin insert #gcm08_buffer(gch00c, gch01i, gch02d, gch03c, gch04c, gch05f, gch06f, gch07f, gch08f, gch09f, gch10c, gch11c, gch12i, gch13f, gch14f, gch15f, gch16i, cash, pos, transamt, checkamt) select gch00c, gch01i, gch02d, gch03c, gch04c, gch05f, gch06f, (case when gch17c in('1', '6', 'ZFB', '&', 'WX', @payment_zp) then gch18f else 0 end + case when gch19c in('1', '6', 'ZFB', '&', 'WX', @payment_zp) then gch20f else 0 end + case when gch21c in ('1', '6', 'ZFB', '&', 'WX', @payment_zp) then gch22f else 0 end + case when gch27c in ('1', '6', 'ZFB', '&', 'WX', @payment_zp) then gch28f else 0 end), gch08f, gch09f, gch10c, gch11c, gch12i, gch13f, gch14f, gch15f, gch16i, (case gch17c when '1' then gch18f else 0 end + case gch19c when '1' then gch20f else 0 end + case gch21c when '1' then gch22f else 0 end + case gch27c when '1' then gch28f else 0 end ), (case gch17c when '6' then gch18f else 0 end + case gch19c when '6' then gch20f else 0 end + case gch21c when '6' then gch22f else 0 end + case gch27c when '6' then gch28f else 0 end), (case gch17c when '&' then gch18f else 0 end + case gch19c when '&' then gch20f else 0 end + case gch21c when '&' then gch22f else 0 end + case gch27c when '&' then gch28f else 0 end ), (case gch17c when @payment_zp then gch18f else 0 end + case gch19c when @payment_zp then gch20f else 0 end + case gch21c when @payment_zp then gch22f else 0 end + case gch27c when @payment_zp then gch28f else 0 end ) from gcm08 a with (nolock), gam26 z with(nolock) where gch00c = z.gaz02c and z.gaz01c = @compid and gch02d >= @fromdate and gch02d <= @todate and (gch10c = 'gn' or gch10c = 'gz' or gch10c = 'gx') and isnull(gch05f, 0) + isnull(gch06f, 0)<0 --考虑单次的项目 insert #gcm08_buffer(gch00c, gch02d, gch03c, gch04c, gch05f, gch07f, gch10c, gch11c, gch12i, gch13f, gch14f, gch15f, gch16i, cash, pos, transamt, checkamt) select ggb00c, gga02d, gga09c, ggb03c, ggb05f, ggb11f, 'gx', gga01c, 0, ggb09f, ggb05f, ggb10f, 0, (case ggb27c when '1' then ggb11f else 0 end), (case ggb27c when '6' then ggb11f else 0 end), (case ggb27c when '&' then ggb11f else 0 end), (case ggb27c when @payment_zp then ggb11f else 0 end) from ggm01 a with (nolock), ggm02 b with (nolock), gam26 z with(nolock) where a.gga00c = b.ggb00c and a.gga01c = b.ggb01c and a.gga02d >= @fromdate and a.gga02d <= @todate and a.gga00c = z.gaz02c and z.gaz01c = @compid and b.ggb27c in ('1', '6', 'ZFB', '&', 'WX', @payment_zp) and isnull(ggb05f, 0)<0 and isnull(ggb11f, 0)<0 if @includecombo = 1 begin insert #gcm08_buffer(gch00c, gch01i, gch02d, gch03c, gch04c, gch05f, gch07f, gch10c, gch11c, gch12i, gch13f, gch14f, gch15f, gch16i, cash, pos, transamt, checkamt) select gti00c, gti01i, gti02d, gti03c, gti05c, gti06f, (case when gti17c in ('1', '6', 'ZFB', '&', @payment_zp) then gti18f else 0 end + case when gti19c in ('1', '6', 'ZFB', '&', 'WX', @payment_zp) then gti20f else 0 end + case when gti21c in ('1', '6', 'ZFB', '&', 'WX', @payment_zp) then gti22f else 0 end + case when gti23c in ('1', '6', 'ZFB', '&', 'WX', @payment_zp) then gti24f else 0 end), gti08c, gti09c, 0, 1, 1, 0, 0, (case gti17c when '1' then gti18f else 0 end + case gti19c when '1' then gti20f else 0 end + case gti21c when '1' then gti22f else 0 end + case gti23c when '1' then gti24f else 0 end), (case gti17c when '6' then gti18f else 0 end + case gti19c when '6' then gti20f else 0 end + case gti21c when '6' then gti22f else 0 end + case gti23c when '6' then gti24f else 0 end), (case gti17c when '&' then gti18f else 0 end + case gti19c when '&' then gti20f else 0 end + case gti21c when '&' then gti22f else 0 end + case gti23c when '&' then gti24f else 0 end), (case gti17c when @payment_zp then gti18f else 0 end + case gti19c when @payment_zp then gti20f else 0 end + case gti21c when @payment_zp then gti22f else 0 end + case gti23c when @payment_zp then gti24f else 0 end) from gtc07 a with (nolock), gam26 z with(nolock) where gti00c = z.gaz02c and z.gaz01c = @compid and gti02d >= @fromdate and gti02d <= @todate and (gti08c = 'gn' or gti08c = 'gz') and gti05c = gti13c and isnull(gti06f, 0)<0 end end --由于项目差价的购买次数都是错的,必须纠正为1 update a set a.gch05f = 1 from #gcm08_buffer a, gdm01 b with(nolock) where a.gch00c = b.gda00c and a.gch04c = b.gda01c and b.gda03c = '项目差价' --添加产品销售到#result_detail中 if @outtype = 2 begin insert #result_detail(compid, cardid, memname, billid, billdate, res05c, res06f, res07f, billtype, cash, pos, transamt, checkamt) select gct00c, gct02c, '', gct14c, gct04d, gct08c, gct11f, gct12f, gct13c, (case gct20c when '1' then gct12f else 0 end), (case gct20c when '6' then gct12f else 0 end), (case gct20c when '&' then gct12f else 0 end), (case gct20c when @payment_zp then gct12f else 0 end) from gcm20 a with (nolock), gam26 z with(nolock) where gct00c = z.gaz02c and z.gaz01c = @compid and gct06c = '4' and gct04d >= @fromdate and gct04d <= @todate and gct20c in ('1', '6', 'ZFB', '&', 'WX', @payment_zp) --需要考虑没有在gcm20中的散客的购买 insert #result_detail(compid, cardid, memname, billid, billdate, res05c, res06f, res07f, billtype, cash, pos, transamt, checkamt) select ggc00c, gga09c, '散客', ggc01c, gga02d, gfa03c, ggc05f, ggc11f, 'gx', (case ggc27c when '1' then ggc11f else 0 end), (case ggc27c when '6' then ggc11f else 0 end), (case ggc27c when '&' then ggc11f else 0 end), (case ggc27c when @payment_zp then ggc11f else 0 end) from ggm01 a with (nolock), ggm03 b with (nolock), gfm01 c with (nolock), gam26 z with(nolock) where a.gga00c = b.ggc00c and a.gga01c = b.ggc01c and b.ggc00c = c.gfa00c and b.ggc03c = c.gfa01c and a.gga02d >= @fromdate and a.gga02d <= @todate and a.gga00c = z.gaz02c and z.gaz01c = @compid and ggc27c in ('1', '6', 'ZFB', '&', 'WX', @payment_zp) and (gga05c like '散客%' and gga09c like '散客%') end else if @outtype = 0 begin insert #result_detail(compid, cardid, memname, billid, billdate, res05c, res06f, res07f, billtype, cash, pos, transamt, checkamt) select gct00c, gct02c, '', gct14c, gct04d, gct08c, gct11f, gct12f, gct13c, (case gct20c when '1' then gct12f else 0 end), (case gct20c when '6' then gct12f else 0 end), (case gct20c when '&' then gct12f else 0 end), (case gct20c when @payment_zp then gct12f else 0 end) from gcm20 a with (nolock), gam26 z with(nolock) where gct00c = z.gaz02c and z.gaz01c = @compid and gct06c = '4' and gct04d >= @fromdate and gct04d <= @todate and gct20c in ('1', '6', 'ZFB', '&', 'WX', @payment_zp) and isnull(gct11f, 0) >= 0 insert #result_detail(compid, cardid, memname, billid, billdate, res05c, res06f, res07f, billtype, cash, pos, transamt, checkamt) select ggc00c, gga09c, '散客', ggc01c, gga02d, gfa03c, ggc05f, ggc11f, 'gx', (case ggc27c when '1' then ggc11f else 0 end), (case ggc27c when '6' then ggc11f else 0 end), (case ggc27c when '&' then ggc11f else 0 end), (case ggc27c when @payment_zp then ggc11f else 0 end) from ggm01 a with (nolock), ggm03 b with (nolock), gfm01 c with (nolock), gam26 z with(nolock) where a.gga00c = b.ggc00c and a.gga01c = b.ggc01c and b.ggc00c = c.gfa00c and b.ggc03c = c.gfa01c and a.gga02d >= @fromdate and a.gga02d <= @todate and a.gga00c = z.gaz02c and z.gaz01c = @compid and ggc27c in ('1', '6', 'ZFB', '&', 'WX', @payment_zp) and (gga05c like '散客%' and gga09c like '散客%') and isnull(ggc05f, 0) >= 0 end else if @outtype = 1 begin insert #result_detail(compid, cardid, memname, billid, billdate, res05c, res06f, res07f, billtype, cash, pos, transamt, checkamt) select gct00c, gct02c, '', gct14c, gct04d, gct08c, gct11f, gct12f, gct13c, (case gct20c when '1' then gct12f else 0 end), (case gct20c when '6' then gct12f else 0 end), (case gct20c when '&' then gct12f else 0 end), (case gct20c when @payment_zp then gct12f else 0 end) from gcm20 a with (nolock), gam26 z with(nolock) where gct00c = z.gaz02c and z.gaz01c = @compid and gct06c = '4' and gct04d >= @fromdate and gct04d <= @todate and gct20c in ('1', '6', 'ZFB', '&', 'WX', @payment_zp) and isnull(gct11f, 0)<0 insert #result_detail(compid, cardid, memname, billid, billdate, res05c, res06f, res07f, billtype, cash, pos, transamt, checkamt) select ggc00c, gga09c, '散客', ggc01c, gga02d, gfa03c, ggc05f, ggc11f, 'gx', (case ggc27c when '1' then ggc11f else 0 end), (case ggc27c when '6' then ggc11f else 0 end), (case ggc27c when '&' then ggc11f else 0 end), (case ggc27c when @payment_zp then ggc11f else 0 end) from ggm01 a with (nolock), ggm03 b with (nolock), gfm01 c with (nolock), gam26 z with(nolock) where a.gga00c = b.ggc00c and a.gga01c = b.ggc01c and b.ggc00c = c.gfa00c and b.ggc03c = c.gfa01c and a.gga02d >= @fromdate and a.gga02d <= @todate and a.gga00c = z.gaz02c and z.gaz01c = @compid and ggc27c in ('1', '6', 'ZFB', '&', 'WX', @payment_zp) and (gga05c like '散客%' and gga09c like '散客%') and isnull(ggc05f, 0)<0 end --添加疗程美容到#result_detail中 insert #result_detail(compid, cardid, memname, billid, billdate, res08c, res09f, res10f, res11f, billtype, cash, pos, transamt, checkamt) select gch00c, gch03c, '', gch11c, gch02d, gda03c, gch14f, isnull(gch05f, 0) + isnull(gch06f, 0), gch07f, gch10c, cash, pos, transamt, checkamt from #gcm08_buffer a, gdm01 with(nolock) where (gch10c = 'gn' or gch10c = 'gz' or gch10c = 'gx') and gch00c = gda00c and gch04c = gda01c and gda13c = '233' --添加疗程美体到#result_detail中 insert #result_detail(compid, cardid, memname, billid, billdate, res12c, res13f, res14f, res15f, billtype, cash, pos, transamt, checkamt) select gch00c, gch03c, '', gch11c, gch02d, gda03c, gch14f, isnull(gch05f, 0) + isnull(gch06f, 0), gch07f, gch10c, cash, pos, transamt, checkamt from #gcm08_buffer a, gdm01 with(nolock) where (gch10c = 'gn' or gch10c = 'gz' or gch10c = 'gx') and gch00c = gda00c and gch04c = gda01c and (gda13c<>'233' and gda13c<>'231' and gda13c<>'232' and gda13c<>'234') --添加疗程仪器到#result_detail中 insert #result_detail(compid, cardid, memname, billid, billdate, res16c, res17f, res18f, res19f, billtype, cash, pos, transamt, checkamt) select gch00c, gch03c, '', gch11c, gch02d, gda03c, gch14f, isnull(gch05f, 0) + isnull(gch06f, 0), gch07f, gch10c, cash, pos, transamt, checkamt from #gcm08_buffer, gdm01 with(nolock) where (gch10c = 'gn' or gch10c = 'gz' or gch10c = 'gx') and gch00c = gda00c and gch04c = gda01c and (gda13c = '231' or gda13c = '232' or gda13c = '234') --把划减金额的明细写入 if @outtype = 0 or @outtype = 2 begin insert #result_detail(compid, cardid, billid, billtype, billdate, saveamt) select gcd00c, gcd01c, gcd08c, gcd07c, gcd09d, gcd06f from gcm04 a with (nolock), gam26 z with(nolock) where a.gcd00c = z.gaz02c and z.gaz01c = @compid and gcd09d >= @fromdate and gcd09d <= @todate and (gcd02i<>4 and gcd02i<>3) and gcd05i = 0 -- 只要充的 end --分组合计 insert #result_final(compid, cardid, memname, billid, billdate, res05c, res06f, res07f, res08c, res09f, res10f, res11f, res12c, res13f, res14f, res15f, res16c, res17f, res18f, res19f, saveamt, res21i, billtype, cash, pos, transamt, checkamt) select compid, cardid, memname, billid, billdate, res05c, sum(res06f), sum(res07f), res08c, sum(res09f), sum(res10f), sum(res11f), res12c, sum(res13f), sum(res14f), sum(res15f), res16c, sum(res17f), sum(res18f), sum(res19f), sum(saveamt), res21i, billtype, sum(cash), sum(pos), sum(transamt), sum(checkamt) from #result_detail group by compid, cardid, memname, billid, billdate, res05c, res08c, res12c, res16c, res21i, billtype order by compid, billdate, billtype, billid --更新会员名称 update a set a.memname = gba03c from #result_final a, gbm01 b with(nolock) where a.compid = b.gba00c and a.cardid = b.gba23c insert #result_final(compid, cardid, res06f, res07f, res09f, res10f, res11f, res13f, res14f, res15f, res17f, res18f, res19f, saveamt, res21i, cash, pos, transamt, checkamt) select @compid, '合计', isnull(sum(res06f), 0), isnull(sum(res07f), 0), isnull(sum(res09f), 0), isnull(sum(res10f), 0), isnull(sum(res11f), 0), isnull(sum(res13f), 0), isnull(sum(res14f), 0), isnull(sum(res15f), 0), isnull(sum(res17f), 0), isnull(sum(res18f), 0), isnull(sum(res19f), 0), sum(saveamt), 1, sum(cash), sum(pos), sum(transamt), sum(checkamt) from #result_final select iid, isnull(compid,'') compid,isnull(cardid,'') cardid,isnull(memname,'') memname,isnull(billid,'') billid,isnull(billdate,'') billdate, isnull(res05c,'') res05c,isnull(res06f,0) res06f,isnull(res07f,0) res07f,isnull(res08c,'') res08c,isnull(res09f,0) res09f, isnull(res10f,0) res10f,isnull(res11f,0) res11f,isnull(res12c,'') res12c,isnull(res13f,0) res13f,isnull(res14f,0) res14f, isnull(res15f,0) res15f,isnull(res16c,'') res16c,isnull(res17f,0) res17f,isnull(res18f,0) res18f,isnull(res19f,0) res19f, isnull(saveamt,0) saveamt,isnull(res21i,0) res21i,isnull(billtype,'') billtype,isnull(cash,0) cash,isnull(pos,0) pos, isnull(transamt,0) transamt,isnull(checkamt,0) checkamt from #result_final where isnull(compid, '') <> '' order by iid drop table #result_detail drop table #result_final end GO /****** Object: StoredProcedure [dbo].[upg_recover_remind_setting] Script Date: 06/12/2017 06:20:33 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_recover_remind_setting] ( @currentCompId varchar(10) --公司 ) as begin -- 员工的归属门店 if not exists (select 1 from data_recover where rdd00c = @currentCompId and rdd01c = 'remind_setting' and rdd02c = 'customer_birthday') begin if not exists (select 1 from remind_setting where rsa00c = @currentCompId and rsa01c = '30') begin declare @rsa02i int select @rsa02i = convert(int, gsa02c) from gsm01 with(nolock) where gsa00c = @currentCompId and gsa01c = 'SP049' declare @rsa04f float select @rsa04f = convert(float, gsa02c) from gsm01 with(nolock) where gsa00c = @currentCompId and gsa01c = 'SP144' insert into remind_setting(rsa00c, rsa01c, rsa02i, rsa03f, rsa04f) values (@currentCompId, '30', @rsa02i, 1, @rsa04f) end insert into data_recover(rdd00c, rdd01c, rdd02c) values (@currentCompId, 'remind_setting', 'customer_birthday') end end GO /****** Object: StoredProcedure [dbo].[upg_updategem01_accountdate] Script Date: 06/12/2017 06:20:51 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_updategem01_accountdate] ( @compid varchar(10), --公司编号 @consumeid varchar(20) --消费单号 ) as --WUXM 2005/08/16 begin declare @begtime varchar(20) --工作日开始时间 declare @endtime varchar(20) --工作日结束时间,表示凌晨时间,如果为000000,则表示帐务日期和实际日期一致 declare @time varchar(6) --实际结帐时间 declare @date varchar(8) --实际日期 declare @staff varchar(20) --登陆人 declare @acctdate varchar(20) -- 账户日期 select @begtime = gsa02c from gsm01 where gsa00c = @compid and gsa01c = 'SP050' select @endtime = gsa02c from gsm01 where gsa00c = @compid and gsa01c = 'SP051' declare @curtime varchar(8) declare @curdate varchar(10) select @curtime = gea22t, @curdate = gea03d, @staff = gea95c from gem01 where gea00c = @compid and gea01c = @consumeid select @time = @curtime select @date = @curdate declare @newdate varchar(8) declare @strdate varchar(10) --帐务日期比正常日期短 if (@endtime > @begtime and @time > @endtime) begin select @strdate = convert(varchar(10), dateadd(day, 1, cast(@date as datetime)), 120) select @newdate = substring(@strdate, 1, 4) + substring(@strdate, 6, 2) + substring(@strdate, 9, 2) update a set a.gea80d = @newdate from gem01 a where gea00c = @compid and gea01c = @consumeid select @acctdate = @newdate end --帐务日期比正常日期长 else if (@endtime <= @begtime and @time <= @endtime) begin select @strdate = convert(varchar(10), dateadd(day, -1, cast(@date as datetime)), 120) select @newdate = substring(@strdate, 1, 4) + substring(@strdate, 6, 2) + substring(@strdate, 9, 2) update a set a.gea80d = @newdate from gem01 a where gea00c = @compid and gea01c = @consumeid select @acctdate = @newdate end --帐务日期和正常日期一致 else begin update a set a.gea80d = @date from gem01 a where gea00c = @compid and gea01c = @consumeid select @acctdate = @date end --更新gsm03新增加的字段 update a set a.gsc10d = @acctdate, a.gsc11c = @staff from gsm03 a where gsc00c = @compid and gsc01c = @consumeid and gsc02c = 'ge' end GO /****** Object: StoredProcedure [dbo].[upg_cal_operation_result_new_lm1] Script Date: 06/12/2017 06:19:33 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_cal_operation_result_new_lm1] ( @compid varchar(10), -- 公司别 @fromdate varchar(10), -- 开始日期 @todate varchar(10), -- 截至日期 @fromstaff varchar(20), -- 员工代码 @tostaff varchar(20), -- 员工代码 @amttype int, -- 0-营业额 1-虚业绩 2-数量 3-实业绩 @printcom varchar(4) -- 打印提成 substring(, 1, 1) --总提成, substring(, 2, 1) - 项目, substring(, 3, 1) -产品 substring(, 4, 1) -卡 ) as begin -- 如果为数量, 也按营业额来生成报表 if @amttype = 2 begin set @amttype = 0 end if isnull(@fromstaff, '') = '' begin set @fromstaff = '*' end declare @sp055f float select @sp055f = gsa03f from gsm01 with(nolock) where gsa00c = @compid and gsa01c = 'SP055' declare @SP048 float select @SP048 = gsa02c from gsm01 with(nolock) where gsa00c = @compid and gsa01c = 'SP048' create table #results ( idd int identity not null, staffno varchar(20) not null, type varchar(10) not null, itemtype varchar(5) null, -- 项目类别 cnt float null, -- 数量 amt float null, -- 业绩 price float null, -- 客单价 cnt_f float null, -- 指定的数量 amt_f float null, -- 指定的金额 cnt_t float null, -- 轮班的数量 amt_t float null, -- 轮班的金额 tamt float null, -- 真实营业额 cash_amt float null, -- 现金总业绩 card_amt float null, -- 划卡总业绩 else_amt float null, -- 其他支付总业绩 cash_cnt float null, -- 现金总数量 card_cnt float null, -- 划卡总数量 else_cnt float null, -- 其他支付总数量 liaoc_cs float null, --疗程购买次数 lucz 2015.10.21 liaoc_cnt float null, --疗程总数量 lucz 2015.10.21 liaoc_t float null, --疗程金额 lucz 2015.10.21 liaoc_sum float null, --疗程销售 总计 liaoc_jine float null, --疗程销售 现金 liaoc_chuz float null, --疗程销售 储值 liaoc_hank float null, --疗程销售 银行卡 liaoc_qita float null, --疗程销售 其他 job_amt float null --劳动业绩 primary key(idd) ) create table #ggm01_ggm02 -- 项目消费单 ( gga00c varchar(10) not null, --公司编号 gga01c varchar(20) not null, --消费单号 gga02d varchar(8) null, --消费日期 gga03t varchar(6) null, --消费开始时间 gga04t varchar(6) null, --消费结束时间 gga05c varchar(20) null, --会员代码, 927版本 gga06c varchar(40) null, --客户姓名,如果是会员则是会员姓名 gga07c varchar(10) null, --项目负责人员(暂不用) gga08c varchar(20) null, --匙牌号 gga09c varchar(20) null, --会员卡号 gga10c varchar(5) null, --币别(不用) gga11f float null, --汇率(不用) gga12c varchar(5) null, --计价单位(不用) gga13f float null, --是否为老客 gga14f float null, --客人数量 gga15f float null, --项目基本金额(不用) gga16f float null, --总金额 gga17f float null, --(销售点)已支付金额 gga18f float null, --折扣 gga19f float null, --折扣单价 gga20c varchar(5) null, --预约单别 gga21c varchar(20) null, --预约单号 gga22c varchar(5) null, --客户类别(已不用) gga23c varchar(5) null, --证件类别(已不用) gga24c varchar(20) null, --证件编号(已不用) gga25c varchar(20) null, --赠券编号(已不用) gga26f float null, --预付金额(已不用) gga27i int null, --账户类别, gga28c varchar(10) null, --接待人员(已不用) gga29c varchar(5) null, --单据类别 gga30c varchar(20) null, --入馆单号 gga31i int null, --顾客性别 gga91c varchar(10) null, --登陆人员代码 gga92d varchar(8) null, --登陆日期 gga93c varchar(10) null, --覆核人员代码 gga94d varchar(8) null, --覆核日期 gga95c varchar(10) null, --结账人员(壁球)(不用) gga96d varchar(8) null, --结账日期(壁球)(不用) gga80d varchar(8) null, --帐务日期, added by WUXM, 2005/08/15 gga81i int null, --是否寄存 ggb00c varchar(10) not null, --公司编号 ggb01c varchar(20) not null, --消费单号 ggb02f float not null, --序号 ggb03c varchar(20) null, --子项目代码 ggb04c varchar(5) null, --单位 ggb05f float null, --数量 ggb06c varchar(5) null, --币别 ggb07f float null, --汇率 ggb08f float null, --标准单价 ggb09f float null, --折让 ggb10f float null, --折让单价 ggb11f float null, --金额 ggb12c varchar(20) null, --大工代码 ggb13c varchar(20) null, --中工代码 ggb14c varchar(20) null, --小工代码 ggb15c varchar(20) null, --大工类型 ggb16c varchar(20) null, --中工类型 ggb17f float null, --成本金额 ggb18f float null, --转卡比率 ggb19c varchar(20) null, --小工类型 ggb20f float null, --大工虚业绩 ggb21f float null, --中工虚业绩 ggb22f float null, --小工虚业绩 ggb23c varchar(128) null, --项目打折原因, added by WUXM, 2005/09/01 ggb24f float null, --项目应收金额, added by WUXM, 2005/09/08 ggb25c varchar(20) null, --四工类型 ggb26c varchar(20) null, --四工代码 ggb27c varchar(5) null, --项目支付方式 Added by WUXM 2005/11/09 ggb28f float null, --大工提成 Added by WUXM 2005/11/10 ggb29f float null, --中工提成 Added by WUXM 2005/11/10 ggb30f float null, --小工提成 Added by WUXM 2005/11/10 ggb31c varchar(20) null, --五工类别 ggb32c varchar(20) null, --五工代码 ggb33f float null, --项目积分 ggb34f float null, --项目的折算率 ggb35f float null, --四工虚业绩 ggb36f float null, --大工实业绩 ggb37f float null, --中工实业绩 ggb38f float null, --小工实业绩 ggb39f float null --四工实业绩 ) create clustered index idx_ggm01_ggm02 on #ggm01_ggm02(ggb01c, ggb02f) --把单据明细列转行 create table #bills ( idd int identity not null, billid varchar(20) not null, --单号 billtype int not null, --1项目, 2产品 gender int default 0 not null, --性别 isold int default 0 not null, --老客 itemtype varchar(5) default '' not null, --项目分类 detail_id float not null, --ggm02 id, ggm03 id staffno varchar(20) not null, --员工编号 stafftype varchar(5) default '' not null, --方式 paytype varchar(5) default '' not null, --支付方式 num float default 0 not null, --数量 amt float default 0 not null, --营业额 real_amt float default 0 not null, --实际营业额 real_ratio float default 0 not null, --实际营业比率 v_amt float default 0 not null, --虚业绩 r_amt float default 0 not null, --实业绩 ret_amt float default 0 not null, --报表需要返回的业绩 PRIMARY KEY NonCLUSTERED(idd) ) create clustered index idx_ggm01_ggm02 on #bills(billid, detail_id) --员工所有的单据 create table #allbill ( staffno varchar(20) not null, -- 员工 billid varchar(20) not null, -- 单号 gender int not null, -- 性别 isold float not null, -- 老客 type varchar(5) not null, -- 点/轮 iscard int not null, -- 1 卡, 2 现金, 0其他 num float default 0 not null, --数量 --add by lm 2015-07-16 -- PRIMARY KEY NonCLUSTERED(staffno, billid) ) create table #bills_2 ( billid varchar(20) not null, -- 单号 staffno varchar(20) not null, -- 员工 billtype int not null --1项目, 2产品 ) create table #bills_3 ( billid varchar(20) not null, -- 单号 billtype int not null, --1项目, 2产品 ret_amt float null ) create table #work_detail ( id int identity , -- 流水好 person_id varchar(20) not null, -- 员工代码 action_id tinyint not null, -- 类别,1-卡销售 2-指定大工,3-轮班大工,4-补工大工, -- 5-指定中工,6-轮班中工 -- 7-指定小工, 8-轮班小工,9-产品销售 10,11-四工 -- 0 卡充值 srvdate varchar(10) not null, --日期 code varchar(20) not null, --项目代码,或是卡号,产品码 name varchar(200) null, --名称 payway varchar(20) null, --支付方式 amt float null, --营业金额 quan float null, --数量 cost float null, --成本 comm float null, --提成 amt2 float null, --虚业绩 amt3 float null, --真实业绩 cardrate float null, --转卡比率 --当买卡或充值的时候,这个地方当作疗程账户金额用 stdprice float null, --标准单价 prj_type varchar(20) null, --项目类别 share_rate float null default 1, --分享比率 cls_flag int null, -- 1:项目 2:产品 3:卡 gender int null, --性别 0:女 1:男 billid varchar(20) null, --单号 paycode varchar(20) null, --支付代码 ex_rate float null, --实际营业额折算比率 ggm02_seq float null, --ggm02中的序列号,与单号一起来标志一个项目 room_id varchar(20) null, --房间号,只在spa或足浴版本需要 compid varchar(10) null, --公司别 spid int null, --连接号 cardid varchar(20) null, --会员卡号 memid varchar(40) null, --会员编号 comp_ascription varchar(10) null,-- 作业公司 ) insert #ggm01_ggm02(gga00c, gga01c, gga02d, gga03t, gga04t, gga05c, gga06c, gga07c, gga08c, gga09c, gga10c, gga11f, gga12c, gga13f, gga14f, gga15f, gga16f, gga17f, gga18f, gga19f, gga20c, gga21c, gga22c, gga23c, gga24c, gga25c, gga26f, gga27i, gga28c, gga29c, gga30c, gga31i, gga91c, gga92d, gga93c, gga94d, gga95c, gga96d, gga80d, gga81i, ggb00c, ggb01c, ggb02f, ggb03c, ggb04c, ggb05f, ggb06c, ggb07f, ggb08f, ggb09f, ggb10f, ggb11f, ggb12c, ggb13c, ggb14c, ggb15c, ggb16c, ggb17f, ggb18f, ggb19c, ggb20f, ggb21f, ggb22f, ggb23c, ggb24f, ggb25c, ggb26c, ggb27c, ggb28f, ggb29f, ggb30f, ggb31c, ggb32c, ggb33f, ggb34f, ggb35f, ggb36f, ggb37f, ggb38f, ggb39f) select gga00c, gga01c, gga02d, gga03t, gga04t, gga05c, gga06c, gga07c, gga08c, gga09c, gga10c, gga11f, gga12c, gga13f, gga14f, gga15f, gga16f, gga17f, gga18f, gga19f, gga20c, gga21c, gga22c, gga23c, gga24c, gga25c, gga26f, gga27i, gga28c, gga29c, gga30c, gga31i, gga91c, gga92d, gga93c, gga94d, gga95c, gga96d, gga80d, gga81i, ggb00c, ggb01c, ggb02f, ggb03c, ggb04c, ggb05f, ggb06c, ggb07f, ggb08f, ggb09f, ggb10f, ggb11f, ggb12c, ggb13c, ggb14c, ggb15c, ggb16c, ggb17f, ggb18f, ggb19c, ggb20f, ggb21f, ggb22f, ggb23c, ggb24f, ggb25c, ggb26c, ggb27c, ggb28f, ggb29f, ggb30f, ggb31c, ggb32c, ggb33f, ggb34f, ggb35f, ggb36f, ggb37f, ggb38f, ggb39f from ggm01 a with (nolock), ggm02 b with (nolock) where a.gga00c = b.ggb00c and a.gga01c = b.ggb01c and a.gga00c = @compid and a.gga80d >= @fromdate and a.gga80d <= @todate and (@fromstaff = '*' or b.ggb12c between @fromstaff and @tostaff or b.ggb13c between @fromstaff and @tostaff or b.ggb14c between @fromstaff and @tostaff or b.ggb26c between @fromstaff and @tostaff) insert #bills(billid, detail_id, staffno, stafftype, v_amt, r_amt, itemtype, billtype) --大工 select isnull(gga01c, ''), isnull(ggb02f, 1), isnull(ggb12c, ''), isnull(ggb15c, '2'), isnull(ggb20f, 0), isnull(ggb36f, 0), '', 1 from #ggm01_ggm02 where isnull(ggb12c, '') != '' union all --中工 select isnull(gga01c, ''), isnull(ggb02f, 1), isnull(ggb13c, ''), isnull(ggb16c, '2'), isnull(ggb21f, 0), isnull(ggb37f, 0), '', 1 from #ggm01_ggm02 where isnull(ggb13c, '') != '' union all --小工 select isnull(gga01c, ''), isnull(ggb02f, 1), isnull(ggb14c, ''), isnull(ggb19c, '2'), isnull(ggb22f, 0), isnull(ggb38f, 0), '', 1 from #ggm01_ggm02 where isnull(ggb14c, '') != '' union all --四工 select isnull(gga01c, ''), isnull(ggb02f, 1), isnull(ggb26c, ''), isnull(ggb25c, '2'), isnull(ggb35f, 0), isnull(ggb39f, 0), '', 1 from #ggm01_ggm02 where isnull(ggb26c, '') != '' update #bills set gender = isnull(gga31i, 0), isold = isnull(gga13f, 0), itemtype = isnull(gda13c, ''), paytype = isnull(ggb27c, ''), num = isnull(ggb05f, 0), amt = isnull(ggb11f, 0), real_ratio = isnull(ggb34f, 0), real_amt = isnull(ggb11f, 0) * isnull(ggb34f, 0), ret_amt = case when @amttype = 1 then isnull(v_amt, 0) --虚业绩 when @amttype = 3 then isnull(r_amt, 0) --实业绩 else isnull(ggb11f, 0) end from #ggm01_ggm02 left join gdm01 with(nolock) on gda00c = gga00c and ggb03c = gda01c where billid = gga01c and detail_id = ggb02f --and billtype = 1 insert #bills(billid, detail_id, staffno, amt, billtype, ret_amt, paytype) --产品第一销售 select billid = isnull(gga01c, ''), detail_id = isnull(ggc02f, 0), staffno = isnull(ggc12c, ''), amt = isnull(ggc11f, 0), billtype = 2, ret_amt = case when @amttype = 1 then isnull(ggc21f, 0) when @amttype = 3 then isnull(ggc21f, 0)-isnull(ggc13f, 0) else isnull(ggc11f, 0) end, paytype = ggc27c from ggm03 b with (nolock), ggm01 a with (nolock) where a.gga00c = b.ggc00c and a.gga01c = b.ggc01c and a.gga80d >= @fromdate and a.gga80d <= @todate and a.gga00c = @compid and isnull(ggc12c, '') != '' and (@fromstaff = '*' or ggc12c between @fromstaff and @tostaff) union all --产品第二销售 select billid = isnull(gga01c, 0), detail_id = isnull(ggc02f, 0), staffno = isnull(ggc15c, ''), amt = isnull(ggc11f, ''), billtype = 2, ret_amt = case when @amttype = 1 then isnull(ggc23f, 0) when @amttype = 3 then isnull(ggc23f, 0)-isnull(ggc13f, 0) else isnull(ggc11f, 0) end, paytype = ggc27c from ggm03 b with (nolock), ggm01 a with (nolock) where a.gga00c = b.ggc00c and a.gga01c = b.ggc01c and a.gga80d >= @fromdate and a.gga80d <= @todate and a.gga00c = @compid and isnull(ggc15c, '') != '' and (@fromstaff = '*' or ggc15c between @fromstaff and @tostaff) union all --产品第三销售 select billid = isnull(gga01c, ''), detail_id = isnull(ggc02f, ''), staffno = isnull(ggc17c, ''), amt = isnull(ggc11f, 0), billtype = 2, ret_amt = case when @amttype = 1 then isnull(ggc25f, 0) when @amttype = 3 then isnull(ggc25f, 0)-isnull(ggc13f, 0) else isnull(ggc11f, 0) end, paytype = ggc27c from ggm03 b with (nolock), ggm01 a with (nolock) where a.gga00c = b.ggc00c and a.gga01c = b.ggc01c and a.gga80d >= @fromdate and a.gga80d <= @todate and a.gga00c = @compid and isnull(ggc17c, '') != '' and (@fromstaff = '*' or ggc17c between @fromstaff and @tostaff) -- 每个员工, 每条单据一条记录 insert #allbill(staffno, billid, gender, isold, type, iscard, num) --modify lm 2015-08-21 select distinct staffno, billid, gender, isold, stafftype, 0 ,sum(num) from #bills where billtype = 1 group by staffno, billid, gender, isold, stafftype order by staffno, billid --统计卡付单据 if @sp055f = 1--单据支付 begin update a set iscard = 1 from gsm03 b with(nolock), #allbill a where a.billid = b.gsc01c and (b.gsc04c in('#', '4', '2', '9') or b.gsc04c between 'A' and 'ZZ') and b.gsc00c = @compid update a set iscard = 2 from gsm03 b with(nolock), #allbill a where a.billid = b.gsc01c and b.gsc04c in(select pay from pay_classify where category = '1' and comp = @compid) and a.iscard != 1 and b.gsc00c = @compid end else if @sp055f = 0--项目支付 begin update a set iscard = 1 from #bills b, #allbill a where a.staffno = b.staffno and a.billid = b.billid and (b.paytype in('#', '4', '2', '9') or b.paytype between 'A' and 'ZZ') and b.billtype = 1 update a set iscard = 2 from #bills b, #allbill a where a.staffno = b.staffno and a.billid = b.billid and b.paytype in(select pay from pay_classify where category = '1' and comp = @compid) and b.billtype = 1 and a.iscard != 1 end --总计 insert #results(staffno, type, cnt, amt) select staffno, type, sum(cnt), sum(amt) from ( --总项目数 select staffno, '0-0' as type, sum(num) as cnt, 0 as amt from #allbill group by staffno --modify lm 2015-07-16 union all --总业绩 select staffno, '0-0' as type, 0 as cnt, sum(ret_amt) as amt from #bills where billtype = 1 group by staffno union all --指定客数 select staffno, '0-1' as type, sum(num) as cnt, 0 as amt from #allbill where type = '1' group by staffno --modify by lm 2015-07-16 union all --指定客业绩 select staffno, '0-1' as type, 0 as cnt, sum(ret_amt) as amt from #bills where stafftype = '1' and billtype = 1 group by staffno union all --指定客现金业绩 select staffno, '0-1-1' as type, 0 as cnt, sum(ret_amt) as amt from #bills where stafftype = '1' and billtype = 1 and paytype in(select pay from pay_classify where category ='1' and comp=@compid) group by staffno union all --指定客卡付业绩 select staffno, '0-1-2' as type, 0 as cnt, sum(ret_amt) as amt from #bills where stafftype = '1' and billtype = 1 and paytype in(select pay from pay_classify where category ='2' and comp=@compid) group by staffno union all --不指定客数 select staffno, '0-2' as type, sum(num) as cnt, 0 as amt from #allbill where type = '2' group by staffno --modify by lm 2015-07-16 union all --不指定客业绩 select staffno, '0-2' as type, 0 as cnt, sum(ret_amt) as amt from #bills where stafftype = '2' and billtype = 1 group by staffno union all --指定客现金业绩 select staffno, '0-2-1' as type, 0 as cnt, sum(ret_amt) as amt from #bills where stafftype = '2' and billtype = 1 and paytype in(select pay from pay_classify where category ='1' and comp=@compid) group by staffno union all --指定卡付业绩 select staffno, '0-2-2' as type, 0 as cnt, sum(ret_amt) as amt from #bills where stafftype = '2' and billtype = 1 and paytype in(select pay from pay_classify where category ='2' and comp=@compid) group by staffno union all --男客数 select staffno, 'A-1', sum(num) as cnt, 0 as amt from #allbill where gender = 1 group by staffno --modify by lm 2015-07-16 union all --男客业绩 select staffno, 'A-1', 0 as cnt, sum(ret_amt) as amt from #bills where gender = 1 and billtype = 1 group by staffno union all --女客数 select staffno, 'A-2', sum(num) as cnt, 0 as amt from #allbill where gender = 0 group by staffno --modify by lm 2015-07-16 union all --女客业绩 select staffno, 'A-2', 0 as cnt, sum(ret_amt) as amt from #bills where gender = 0 and billtype = 1 group by staffno union all --新客数 select staffno, 'N-1', sum(num) as cnt, 0 as amt from #allbill where isold = 0 group by staffno --modify by lm 2015-07-16 union all --新客业绩 select staffno, 'N-1', 0 as cnt, sum(ret_amt) as amt from #bills where isold = 0 and billtype = 1 group by staffno union all --老客数 select staffno, 'N-2', sum(num) as cnt, 0 as amt from #allbill where isold = 1 group by staffno --modify by lm 2015-07-16 union all --老客业绩 select staffno, 'N-2', 0 as cnt, sum(ret_amt) as amt from #bills where isold = 1 and billtype = 1 group by staffno union all --总指定客数 select distinct staffno, '0-0-1', count(billid) as cnt, 0 as amt from #allbill where type = '1' group by staffno --add by lm 2015-07-27 union all --总非指定客数 select distinct staffno, '0-0-2', count(billid) as cnt, 0 as amt from #allbill where type = '2' group by staffno --add by lm 2015-07-27 union all --总客数 select a.staffno, a.type, count(a.staffno) as cnt, a.amt from (select staffno, '0-0-0' as type ,count(staffno) as cnt, 0 as amt from #allbill group by staffno,billid) a --add by lm 2015-08-26 group by a.staffno, a.type ,a.amt ) as tmp group by staffno, type order by staffno, type if @sp055f = 1 -- 单据支付 begin insert #results(staffno, type, cnt, amt) select staffno, 'C-1', sum(cnt), sum(amt) from ( select staffno, sum(num) as cnt, 0 as amt from #allbill where iscard = 1 group by staffno --modify by lm 2015-08-21 union all select staffno, 0 as cnt, sum(gsc05f) as amt from gsm03 with(nolock), #allbill where gsc01c = billid and gsc02c = 'gx' and (gsc04c in ('#', '2', '4') or gsc04c >= '9') and gsc00c = @compid group by staffno ) as tmp group by staffno union all select staffno, 'C-0', sum(cnt), sum(amt) from ( select staffno, sum(num) as cnt, 0 as amt from #allbill where iscard = 2 group by staffno --modify by lm 2015-07-16 union all select staffno, 0 as cnt, sum(gsc05f) as amt from gsm03 with(nolock), #allbill where gsc01c = billid and gsc02c = 'gx' and gsc04c in (select pay from pay_classify where category = '1' and comp = @compid) and gsc00c = @compid group by staffno ) as tmp group by staffno union all select staffno, 'C-2', sum(cnt), sum(amt) from ( select staffno, sum(num) as cnt, 0 as amt from #allbill where iscard = 0 group by staffno --modify by lm 2015-07-16 union all select staffno, 0 as cnt, sum(gsc05f) as amt from gsm03 with(nolock), #allbill where gsc01c = billid and gsc02c = 'gx' and (gsc04c in ('&', '3', '7', '0', '8')or gsc04c like '$%') and gsc00c = @compid group by staffno ) as tmp group by staffno end else if @sp055f = 0 -- 项目支付 begin insert #results(staffno, type, cnt, amt) select staffno, 'C-1', sum(cnt), sum(amt) from ( select staffno, sum(num) as cnt, 0 as amt from #allbill where iscard = 1 group by staffno --modify by lm 2015-07-16 union all select staffno, 0 as cnt, sum(ret_amt) as amt from #bills where (paytype in ('#', '2', '4') or paytype >= '9') and billtype = 1 group by staffno ) as tmp group by staffno union all select staffno, 'C-0', sum(cnt), sum(amt) from ( select staffno, sum(num) as cnt, 0 as amt from #allbill where iscard = 2 group by staffno --modify by lm 2015-07-16 union all select staffno, 0 as cnt, sum(ret_amt) as amt from #bills where paytype in (select pay from pay_classify where category = '1' and comp = @compid) and billtype = 1 group by staffno ) as tmp group by staffno union all select staffno, 'C-2', sum(cnt), sum(amt) from ( select staffno, sum(num) as cnt, 0 as amt from #allbill where iscard = 0 group by staffno --modify by lm 2015-07-16 union all select staffno, 0 as cnt, sum(ret_amt) as amt from #bills where (paytype in ('&', '3', '7', '0', '8')or paytype like '$%') and billtype = 1 group by staffno ) as tmp group by staffno end --项目分类总计 insert #results(staffno, type, itemtype, cnt, amt, tamt, cnt_f, amt_f, cnt_t, amt_t, cash_amt, card_amt, else_amt, cash_cnt, card_cnt, else_cnt) select staffno, 'ITEMCLasS', itemtype, sum(cnt), sum(amt), sum(tamt), sum(cnt_f), sum(amt_f), sum(cnt_t), sum(amt_t), sum(cash_amt), sum(card_amt), sum(else_amt), sum(cash_cnt), sum(card_cnt), sum(else_cnt) from ( select staffno, itemtype, sum(num) as cnt, sum(ret_amt) as amt, sum(real_amt) as tamt, --modify by lm 2015-07-16 0 as cnt_f, 0 as amt_f, 0 as cnt_t, 0 as amt_t, 0 as cash_amt, 0 as card_amt, 0 as else_amt, 0 as cash_cnt, 0 as card_cnt, 0 as else_cnt from #bills group by staffno, itemtype union all select staffno, itemtype, 0 as cnt, 0 as amt, 0 as tamt, sum(num) as cnt_f, sum(ret_amt) as amt_f, 0 as cnt_t, 0 as amt_t, --modify by lm 2015-07-16 0 as cash_amt, 0 as card_amt, 0 as else_amt, 0 as cash_cnt, 0 as card_cnt, 0 as else_cnt from #bills where stafftype = '1' group by staffno, itemtype union all select staffno, itemtype, 0 as cnt, 0 as amt, 0 as tamt, 0 as cnt_f, 0 as amt_f, sum(num) as cnt_t, sum(ret_amt) as amt_t, --modify by lm 2015-07-16 0 as cash_amt, 0 as card_amt, 0 as else_amt, 0 as cash_cnt, 0 as card_cnt, 0 as else_cnt from #bills where stafftype = '2' group by staffno, itemtype union all select staffno, itemtype, 0 as cnt, 0 as amt, 0 as tamt, 0 as cnt_f, 0 as amt_f, 0 as cnt_t, 0 as amt_t, sum(ret_amt) as cash_amt, 0 as card_amt, 0 as else_amt, sum(num) as cash_cnt, 0 as card_cnt, 0 as else_cnt --modify by lm 2015-07-16 from #bills where paytype in (select pay from pay_classify where category = '1' and comp = @compid) group by staffno, itemtype union all select staffno, itemtype, 0 as cnt, 0 as amt, 0 as tamt, 0 as cnt_f, 0 as amt_f, 0 as cnt_t, 0 as amt_t, 0 as cash_amt, sum(ret_amt) as card_amt, 0 as else_amt, 0 as cash_cnt, sum(num) as card_cnt, 0 as else_cnt --modify by lm 2015-07-16 from #bills where( paytype in('#', '4', '2', '9') or paytype between 'A' and 'ZZ' ) group by staffno, itemtype union all select staffno, itemtype, 0 as cnt, 0 as amt, 0 as tamt, 0 as cnt_f, 0 as amt_f, 0 as cnt_t, 0 as amt_t, 0 as cash_amt, 0 as card_amt, sum(ret_amt) as else_amt, 0 as cash_cnt, 0 as card_cnt, sum(num) as else_cnt --modify by lm 2015-07-16 from #bills where (paytype in ('&', '3', '7', '0', '8')or paytype like '$%') group by staffno, itemtype ) as tmp group by staffno, itemtype order by staffno, itemtype --start 更新购买疗程 lucz 2015.10.21 declare @staffno varchar(20) declare @gda13c varchar(60) declare @num float declare @gcm05f float Create table #tb_liaocheng ( staffno varchar(30) not null, gda13c varchar(30) null, --统计分类(统计) num float null, --次数 jine float null --疗程金额 ) insert into #tb_liaocheng(staffno,gda13c,num,jine) select staffno, gda13c, --统计分类(统计) sum(num) num, --次数 sum(jine) jine --疗程金额 from ( select ggb00c, ggb01c, ggb12c as staffno, --大工 gda13c = (select gda13c from gdm01 where gda00c = ggb00c and gda01c = ggb03c ), --统计分类(统计) ggb05f as num, --数量 case when @amttype = 1 then ggb20f when @amttype = 3 then ggb36f else ggb11f end as jine, --虚业绩 gga94d = (select top 1 gga94d from ggm01 where gga00c = ggb00c and gga01c = ggb01c) from ggm02 where ggb00c = @compid and isnull(ggb47i,-1) <> -1 and isnull(ggb12c,'')<> '' union all select ggb00c, ggb01c, ggb13c as staffno, --中工 gda13c = (select gda13c from gdm01 where gda00c = ggb00c and gda01c = ggb03c ), --统计分类(统计) ggb05f as num, --数量 case when @amttype = 1 then ggb21f when @amttype = 3 then ggb37f else ggb11f end as jine, --虚业绩 gga94d = (select top 1 gga94d from ggm01 where gga00c = ggb00c and gga01c = ggb01c) from ggm02 where ggb00c = @compid and isnull(ggb47i,-1) <> -1 and isnull(ggb13c,'')<> '' union all select ggb00c, ggb01c, ggb14c as staffno, --小工 gda13c = (select gda13c from gdm01 where gda00c = ggb00c and gda01c = ggb03c ), --统计分类(统计) ggb05f as num, --数量 case when @amttype = 1 then ggb22f when @amttype = 3 then ggb38f else ggb11f end as jine, --虚业绩 gga94d = (select top 1 gga94d from ggm01 where gga00c = ggb00c and gga01c = ggb01c) from ggm02 where ggb00c = @compid and isnull(ggb47i,-1) <> -1 and isnull(ggb14c,'')<> '' union all select ggb00c, ggb01c, ggb26c as staffno, --四工 gda13c = (select gda13c from gdm01 where gda00c = ggb00c and gda01c = ggb03c ), --统计分类(统计) ggb05f as num, --数量 case when @amttype = 1 then ggb35f when @amttype = 3 then ggb39f else ggb11f end as jine, --虚业绩 gga94d = (select top 1 gga94d from ggm01 where gga00c = ggb00c and gga01c = ggb01c) from ggm02 where ggb00c = @compid and isnull(ggb47i,-1) <> -1 and isnull(ggb26c,'')<> '' ) aTable where gga94d between @fromdate and @todate and (@fromstaff = '*' or staffno between @fromstaff and @tostaff) group by staffno,gda13c --更新 Update a set a.liaoc_cs = b.num, a.liaoc_cnt = b.num, a.liaoc_t = b.jine from #results a, #tb_liaocheng b where a.staffno = b.staffno and a.type = 'ITEMCLasS' and a. itemtype = b.gda13c --end lucz 2015.10.21 Create table #liaoc_payment ( personid varchar(60), -- 员工代码 [type] varchar(30), -- 统计分类 paycode varchar(20), -- 支付代码 amt float -- 金额或业绩 ) insert into #liaoc_payment select personid, [type], case paycode when '1' then '现金' when '6' then '银行卡' when '4' then '储值' when 'ZFB' then '银行卡' when 'WX' then '银行卡' else '其他' end paycode, case @amttype when 0 then sum(payamt) when 2 then sum(payamt) when 3 then sum(perf) when 4 then sum(perf) else sum(perf) end amt from ( select personid, code, [type] = (select gda13c from gdm01 where gda00c = compid and gda01c = code ), --统计分类(统计) paycode, --支付代码 sum(perf) perf, --业绩 sum(payamt) payamt--金额 from card_fill_detail_by_payment where compid = @compid and billid in ( select gnc01c from gnm03 a with (nolock), gsm03 b with (nolock) where a.gnc00c = b.gsc00c and a.gnc01c = b.gsc01c and b.gsc02c = 'gn' and a.gnc00c = @compid and (b.gsc10d between @fromdate and @todate) ) and filltype = 2 and billtype = 'gn' and (@fromstaff = '*' or personid between @fromstaff and @tostaff) group by compid,personid,code,paycode union all select personid, code, [type] = (select gda13c from gdm01 where gda00c = compid and gda01c = code ), --统计分类(统计) paycode, --支付代码 sum(perf) perf, --业绩 sum(payamt) payamt--金额 from card_fill_detail_by_payment where compid = @compid and billid in ( select gcl01c from gcm10 a with (nolock), gsm03 b with (nolock) where a.gcl00c = b.gsc00c and a.gcl01c = b.gsc01c and b.gsc02c = 'gz' and a.gcl00c = @compid and (a.gcl02d between @fromdate and @todate) ) and filltype = 2 and billtype = 'gz' and (@fromstaff = '*' or personid between @fromstaff and @tostaff) group by compid,personid,code,paycode ) as table_payment group by personid,paycode,[type] --更新#results insert into #results(staffno,[type],itemtype) select personid,'ITEMLCS',[type] from #liaoc_payment group by personid,type Update a set a.liaoc_jine = b.amt from #results a, (select personid,[type],sum(amt) amt from #liaoc_payment where paycode = '现金' group by personid,[type]) b where a.staffno = b.personid and a.type = 'ITEMLCS' and a. itemtype = b.[type] Update a set a.liaoc_chuz = b.amt from #results a, (select personid,[type],sum(amt) amt from #liaoc_payment where paycode = '储值' group by personid,[type]) b where a.staffno = b.personid and a.type = 'ITEMLCS' and a. itemtype = b.[type] Update a set a.liaoc_hank = b.amt from #results a, (select personid,[type],sum(amt) amt from #liaoc_payment where paycode = '银行卡' group by personid,[type]) b where a.staffno = b.personid and a.type = 'ITEMLCS' and a. itemtype = b.[type] Update a set a.liaoc_qita = b.amt from #results a, (select personid,[type],sum(amt) amt from #liaoc_payment where paycode = '其他' group by personid,[type]) b where a.staffno = b.personid and a.type = 'ITEMLCS' and a. itemtype = b.[type] -- 疗程销售 总计 Update a set a.liaoc_sum = b.amt from #results a, (select personid,[type],sum(amt) amt from #liaoc_payment group by personid,[type]) b where a.staffno = b.personid and a.type = 'ITEMLCS' and a. itemtype = b.[type] ------------------------- insert #results(staffno, type, itemtype, cnt, amt, price, cnt_f, amt_f, cnt_t, amt_t, cash_amt, cash_cnt, card_amt, card_cnt, else_amt, else_cnt, tamt, liaoc_cs,liaoc_cnt,liaoc_t,liaoc_sum,liaoc_jine,liaoc_chuz,liaoc_hank,liaoc_qita) select staffno, 'ITEMGROUP', gsr02c as itemgroup, sum(cnt), sum(amt), sum(price), sum(cnt_f), sum(amt_f), sum(cnt_t), sum(amt_t), sum(cash_amt), sum(cash_cnt), sum(card_amt), sum(card_cnt), sum(else_amt), sum(else_cnt), sum(tamt), sum(liaoc_cs),sum(liaoc_cnt),sum(liaoc_t),sum(liaoc_sum),sum(liaoc_jine),sum(liaoc_chuz),sum(liaoc_hank),sum(liaoc_qita) from #results, gsm29 with(nolock) where gsr00c = @compid and gsr01c = itemtype and isnull(gsr02c, '') != '' and gsr03i = 1 group by staffno, gsr02c --产品销售 insert #results(staffno, type, cnt, amt) select staffno, '99-2', sum(1), sum(ret_amt) from #bills where billtype = 2 group by staffno --产品销售现金 insert #results(staffno, type, cnt, amt) select staffno, '99-2-1', sum(1), sum(ret_amt) from #bills where billtype = 2 and paytype in(select pay from pay_classify where category = '1' and comp = @compid) group by staffno --产品销售卡付 insert #results(staffno, type, cnt, amt) select staffno, '99-2-2', sum(1), sum(ret_amt) from #bills where billtype = 2 and (paytype in('#', '4', '2', '9') or paytype between 'A' and 'ZZ') group by staffno --产品销售其他支付 insert #results(staffno, type, cnt, amt) select staffno, '99-2-3', sum(1), sum(ret_amt) from #bills where billtype = 2 and (paytype in ('&', '3', '7', '0', '8')or paytype like '$%') group by staffno create table #cardbill2 ( idd int identity not null, billid varchar(20) not null, -- 单号 billtype int not null, -- 1-买卡, 2-充值 staffno varchar(20) not null, -- 员工编号 cardno varchar(20) null, -- 卡号 ret_amt float default 0 null, -- 报表需要返回的业绩 rate float default 0 null, -- 员工分享比率 card_amt float default 0 null, cash_amt float default 0 null, pay_amt float default 0 null, -- 支付金额 pay_way varchar(5) default '' null, -- 支付方式 add by limin 2014/07/21 增加 支付方式 amt_sum float default 0 null -- 金额 PRIMARY KEY NonCLUSTERED(idd) ) create table #cardbill ( idd int identity not null, billid varchar(20) not null, -- 单号 billtype int not null, -- 1-买卡, 2-充值 staffno varchar(20) not null, -- 员工编号 cardno varchar(20) not null, -- 卡号 ret_amt float default 0 not null, -- 报表需要返回的业绩 rate float default 0 null, -- 员工分享比率 card_amt float default 0 null, cash_amt float default 0 null PRIMARY KEY NonCLUSTERED(idd) ) create table #cardbill_amt_sum --add by lm 2015.10.29 ( idd int identity not null, billid varchar(20) not null, -- 单号 billtype int not null, --1项目, 2产品 amt float default 0 null, -- 金额 ret_amt float default 0 null -- 业绩 PRIMARY KEY NonCLUSTERED(idd) ) create table #cardbill_num ( staffno varchar(20) not null, -- 员工编号 num float default 0 null ) create table #gnm01_gnm02 ( gna01c varchar(20) not null, gnb03c varchar(20) not null, gnb08f float null, gna20c varchar(20) null, gna28c varchar(20) null, gna30c varchar(20) null, gna32c varchar(20) null, gna34c varchar(20) null, gna36c varchar(20) null, gna38c varchar(20) null, gna40c varchar(20) null, gna42c varchar(20) null, gna44c varchar(20) null, gna67f float null, gna69f float null, gna71f float null, gna86f float null, gna87f float null, gna88f float null, gna89f float null, gna90f float null, gna91f float null, gna92f float null, gna27f float null,--第一分享比率 gna29f float null,--第二分享比率 gna31f float null,--第三分享比率 gna33f float null,--第四分享比率 gna35f float null,--第五分享比率 gna37f float null,--第六分享比率 gna39f float null,--第七分享比率 gna41f float null,--第八分享比率 gna43f float null,--第九分享比率 gna45f float null,--第十分享比率 pay_amt varchar(20) null, pay_way varchar(20) null ) create table #gcm10 ( gcl01c varchar(20) not null, gcl03c varchar(20) not null, gcl07f float null, gcl09c varchar(20) null, gcl17c varchar(20) null, gcl19c varchar(20) null, gcl22c varchar(20) null, gcl24c varchar(20) null, gcl26c varchar(20) null, gcl28c varchar(20) null, gcl30c varchar(20) null, gcl32c varchar(20) null, gcl34c varchar(20) null, gcl58f float null, gcl60f float null, gcl62f float null, gcl83f float null, gcl84f float null, gcl85f float null, gcl86f float null, gcl87f float null, gcl88f float null, gcl89f float null, gcl16f float null, --第一分享比率 gcl18f float null, --第二分享比率 gcl20f float null, --第三分享比率 gcl23f float null, --第四分享比率 gcl25f float null, --第五分享比率 gcl27f float null, --第六分享比率 gcl29f float null, --第七分享比率 gcl31f float null, --第八分享比率 gcl33f float null, --第九分享比率 gcl35f float null, --第十分享比率 pay_amt varchar(20) null, pay_way varchar(20) null, gcl06i int null ) create table #gcm11 --add by lm 2015.12.03 ( gcm00c varchar(10) not null, --门店编号 gcm01c varchar(20) not null, --异动单号 gcm03f float null, --次数 gcm04f float null, --赠送次数 gcm05f float null --疗程金额 ) insert #gnm01_gnm02(gna01c, gnb03c, gnb08f, gna20c, gna28c, gna30c, gna32c, gna34c, gna36c, gna38c, gna40c, gna42c, gna44c, gna67f, gna69f, gna71f, gna86f, gna87f, gna88f, gna89f, gna90f, gna91f, gna92f,pay_way, pay_amt, gna27f, gna29f, gna31f, gna33f, gna35f, gna37f, gna39f, gna41f, gna43f, gna45f) select gna01c, gnb03c, gnb08f, gna20c, gna28c, gna30c, gna32c, gna34c, gna36c, gna38c, gna40c, gna42c, gna44c, gna67f, gna69f, gna71f, gna86f, gna87f, gna88f, gna89f, gna90f, gna91f, gna92f, gsc04c, gsc05f, gna27f, gna29f ,gna31f, gna33f, gna35f, gna37f, gna39f, gna41f, gna43f, gna45f from gnm01 with (nolock), gnm02 with (nolock), gsm03 with (nolock) where gnb00c = gna00c and gnb01c = gna01c and gsc00c = gna00c and gsc01c = gna01c and gsc02c = 'gn' and gna00c = @compid and gna80d >= @fromdate and gna80d <= @todate and gsc04c <> '5' and (@fromstaff = '*' or isnull(gna20c, '') between @fromstaff and @tostaff or isnull(gna28c, '') between @fromstaff and @tostaff or isnull(gna30c, '') between @fromstaff and @tostaff or isnull(gna32c, '') between @fromstaff and @tostaff or isnull(gna34c, '') between @fromstaff and @tostaff or isnull(gna36c, '') between @fromstaff and @tostaff or isnull(gna40c, '') between @fromstaff and @tostaff or isnull(gna42c, '') between @fromstaff and @tostaff or isnull(gna44c, '') between @fromstaff and @tostaff) insert #gcm10(gcl01c, gcl03c, gcl07f, gcl09c, gcl17c, gcl19c, gcl22c, gcl24c, gcl26c, gcl28c, gcl30c, gcl32c, gcl34c, gcl58f, gcl60f, gcl62f, gcl83f, gcl84f, gcl85f, gcl86f, gcl87f, gcl88f, gcl89f, pay_way, pay_amt, gcl06i, gcl16f, gcl18f, gcl20f, gcl23f, gcl25f, gcl27f, gcl29f, gcl31f, gcl33f, gcl35f) select gcl01c, gcl03c, gcl07f, gcl09c, gcl17c, gcl19c, gcl22c, gcl24c, gcl26c, gcl28c, gcl30c, gcl32c, gcl34c, gcl58f, gcl60f, gcl62f, gcl83f, gcl84f, gcl85f, gcl86f, gcl87f, gcl88f, gcl89f, gsc04c, gsc05f, gcl06i, gcl16f, gcl18f, gcl20f, gcl23f, gcl25f, gcl27f, gcl29f, gcl31f, gcl33f, gcl35f from gcm10 with (nolock), gsm03 with (nolock) where gcl00c = @compid and gsc00c = gcl00c and gsc01c = gcl01c and gsc02c = 'gz' and gcl80d >= @fromdate and gcl80d <= @todate and gcl06i in (0, 6) and gcl04c not in('888', '889') and gsc04c <> '5' and (@fromstaff = '*' or isnull(gcl09c, '') between @fromstaff and @tostaff or isnull(gcl17c, '') between @fromstaff and @tostaff or isnull(gcl19c, '') between @fromstaff and @tostaff or isnull(gcl22c, '') between @fromstaff and @tostaff or isnull(gcl24c, '') between @fromstaff and @tostaff or isnull(gcl26c, '') between @fromstaff and @tostaff or isnull(gcl28c, '') between @fromstaff and @tostaff or isnull(gcl30c, '') between @fromstaff and @tostaff or isnull(gcl34c, '') between @fromstaff and @tostaff) --获取疗程次数和赠送次数 add by lm 2015.12.03 insert #gcm11(gcm00c, gcm01c, gcm03f, gcm04f, gcm05f) select gcm00c, gcm01c, sum(gcm03f), sum(gcm04f), sum(gcm05f) from gcm11 with(nolock) inner join #gcm10 with(nolock) on gcl01c = gcm01c and gcm00c = @compid group by gcm00c, gcm01c --declare @amttype int --set @amttype = 1 insert #cardbill2(billid, billtype, cardno, staffno, ret_amt, pay_way, pay_amt, rate) --卖卡第一人 select billid = gna01c, billtype = 1, cardno = gnb03c, staffno = gna20c, ret_amt = isnull(gna67f, 0), pay_way, pay_amt, gna27f from #gnm01_gnm02 where isnull(gna20c, '') != '' union all --卖卡第二人 select billid = gna01c, billtype = 1, cardno = gnb03c, staffno = gna28c, ret_amt = isnull(gna69f, 0), pay_way, pay_amt, gna29f from #gnm01_gnm02 where isnull(gna28c, '') != '' union all --卖卡第三人 select billid = gna01c, billtype = 1, cardno = gnb03c, staffno = gna30c, ret_amt = isnull(gna71f, 0), pay_way, pay_amt, gna31f from #gnm01_gnm02 where isnull(gna30c, '') != '' union all --卖卡第四人 select billid = gna01c, billtype = 1, cardno = gnb03c, staffno = gna32c, ret_amt = isnull(gna86f, 0), pay_way, pay_amt, gna33f from #gnm01_gnm02 where isnull(gna32c, '') != '' union all --卖卡第五人 select billid = gna01c, billtype = 1, cardno = gnb03c, staffno = gna34c, ret_amt = isnull(gna87f, 0), pay_way, pay_amt, gna35f from #gnm01_gnm02 where isnull(gna34c, '') != '' union all --卖卡第六人 select billid = gna01c, billtype = 1, cardno = gnb03c, staffno = gna36c, ret_amt = isnull(gna88f, 0), pay_way, pay_amt, gna37f from #gnm01_gnm02 where isnull(gna36c, '') != '' union all --卖卡第七人 select billid = gna01c, billtype = 1, cardno = gnb03c, staffno = gna38c, ret_amt = isnull(gna89f, 0), pay_way, pay_amt, gna39f from #gnm01_gnm02 where isnull(gna38c, '') != '' union all --卖卡第八人 select billid = gna01c, billtype = 1, cardno = gnb03c, staffno = gna40c, ret_amt = isnull(gna90f, 0), pay_way, pay_amt, gna41f from #gnm01_gnm02 where isnull(gna40c, '') != '' union all --卖卡第九人 select billid = gna01c, billtype = 1, cardno = gnb03c, staffno = gna42c, ret_amt = isnull(gna91f, 0), pay_way, pay_amt, gna43f from #gnm01_gnm02 where isnull(gna42c, '') != '' union all --卖卡第十人 select billid = gna01c, billtype = 1, cardno = gnb03c, staffno = gna44c, ret_amt = isnull(gna92f, 0), pay_way, pay_amt, gna45f from #gnm01_gnm02 where isnull(gna44c, '') != '' --统计充值,还款 modify by lm 2015.10.19 union all --充值第一人 select billid = gcl01c, billtype = 2, cardno = gcl03c, staffno = gcl09c, ret_amt = isnull(gcl58f, 0), pay_way, pay_amt, gcl16f from #gcm10 where isnull(gcl09c, '') != '' and (gcl06i = 0 or gcl06i = 6) union all --充值第二人 select billid = gcl01c, billtype = 2, cardno = gcl03c, staffno = gcl17c, ret_amt = isnull(gcl60f, 0), pay_way, pay_amt, gcl18f from #gcm10 with (nolock) where isnull(gcl17c, '') != '' and (gcl06i = 0 or gcl06i = 6) union all --充值第三人 select billid = gcl01c, billtype = 2, cardno = gcl03c, staffno = gcl19c, ret_amt = isnull(gcl62f, 0), pay_way, pay_amt, gcl20f from #gcm10 with (nolock) where isnull(gcl19c, '') != '' and (gcl06i = 0 or gcl06i = 6) union all --充值第四人 select billid = gcl01c, billtype = 2, cardno = gcl03c, staffno = gcl22c, ret_amt = isnull(gcl83f, 0), pay_way, pay_amt, gcl23f from #gcm10 with (nolock) where isnull(gcl22c, '') != '' and (gcl06i = 0 or gcl06i = 6) union all --充值第五人 select billid = gcl01c, billtype = 2, cardno = gcl03c, staffno = gcl24c, ret_amt = isnull(gcl84f, 0), pay_way, pay_amt, gcl25f from #gcm10 with (nolock) where isnull(gcl24c, '') != '' and (gcl06i = 0 or gcl06i = 6) union all --充值第六人 select billid = gcl01c, billtype = 2, cardno = gcl03c, staffno = gcl26c, ret_amt = isnull(gcl85f, 0), pay_way, pay_amt, gcl27f from #gcm10 with (nolock) where isnull(gcl26c, '') != '' and (gcl06i = 0 or gcl06i = 6) union all --充值第七人 select billid = gcl01c, billtype = 2, cardno = gcl03c, staffno = gcl28c, ret_amt = isnull(gcl86f, 0), pay_way, pay_amt, gcl29f from #gcm10 with (nolock) where isnull(gcl28c, '') != '' and (gcl06i = 0 or gcl06i = 6) union all --充值第八人 select billid = gcl01c, billtype = 2, cardno = gcl03c, staffno = gcl30c, ret_amt = isnull(gcl87f, 0), pay_way, pay_amt, gcl31f from #gcm10 with (nolock) where isnull(gcl30c, '') != '' and (gcl06i = 0 or gcl06i = 6) union all --充值第九人 select billid = gcl01c, billtype = 2, cardno = gcl03c, staffno = gcl32c, ret_amt = isnull(gcl88f, 0), pay_way, pay_amt, gcl33f from #gcm10 with (nolock) where isnull(gcl32c, '') != '' and (gcl06i = 0 or gcl06i = 6) union all --充值第十人 select billid = gcl01c, billtype = 2, cardno = gcl03c, staffno = gcl34c, ret_amt = isnull(gcl89f, 0), pay_way, pay_amt, gcl35f from #gcm10 with (nolock) where isnull(gcl34c, '') != '' and (gcl06i = 0 or gcl06i = 6) --统计取款 --modify lm 2015.10.19 union all --充值第一人 select billid = gcl01c, billtype = 2, cardno = gcl03c, staffno = gcl09c, ret_amt = isnull( @SP048 * gcl58f, 0), pay_way, pay_amt, gcl16f from #gcm10 where isnull(gcl09c, '') != '' and gcl06i = 1 union all --充值第二人 select billid = gcl01c, billtype = 2, cardno = gcl03c, staffno = gcl17c, rret_amt = isnull( @SP048 * gcl60f, 0), pay_way, pay_amt, gcl18f from #gcm10 with (nolock) where isnull(gcl17c, '') != '' and gcl06i = 1 union all --充值第三人 select billid = gcl01c, billtype = 2, cardno = gcl03c, staffno = gcl19c, ret_amt = isnull( @SP048 * gcl62f, 0), pay_way, pay_amt, gcl20f from #gcm10 with (nolock) where isnull(gcl19c, '') != '' and gcl06i = 1 union all --充值第四人 select billid = gcl01c, billtype = 2, cardno = gcl03c, staffno = gcl22c, ret_amt = isnull( @SP048 * gcl83f, 0), pay_way, pay_amt, gcl23f from #gcm10 with (nolock) where isnull(gcl22c, '') != '' and gcl06i = 1 union all --充值第五人 select billid = gcl01c, billtype = 2, cardno = gcl03c, staffno = gcl24c, ret_amt = isnull( @SP048 * gcl84f, 0), pay_way, pay_amt, gcl25f from #gcm10 with (nolock) where isnull(gcl24c, '') != '' and gcl06i = 1 union all --充值第六人 select billid = gcl01c, billtype = 2, cardno = gcl03c, staffno = gcl26c, ret_amt = isnull( @SP048 * gcl85f, 0), pay_way, pay_amt, gcl27f from #gcm10 with (nolock) where isnull(gcl26c, '') != '' and gcl06i = 1 union all --充值第七人 select billid = gcl01c, billtype = 2, cardno = gcl03c, staffno = gcl28c, ret_amt = isnull( @SP048 * gcl86f, 0), pay_way, pay_amt, gcl29f from #gcm10 with (nolock) where isnull(gcl28c, '') != '' and gcl06i = 1 union all --充值第八人 select billid = gcl01c, billtype = 2, cardno = gcl03c, staffno = gcl30c, ret_amt = isnull( @SP048 * gcl87f, 0), pay_way, pay_amt, gcl31f from #gcm10 with (nolock) where isnull(gcl30c, '') != '' and gcl06i = 1 union all --充值第九人 select billid = gcl01c, billtype = 2, cardno = gcl03c, staffno = gcl32c, ret_amt = isnull( @SP048 * gcl88f, 0), pay_way, pay_amt, gcl33f from #gcm10 with (nolock) where isnull(gcl32c, '') != '' and gcl06i = 1 union all --充值第十人 select billid = gcl01c, billtype = 2, cardno = gcl03c, staffno = gcl34c, ret_amt = isnull( @SP048 * gcl89f, 0), pay_way, pay_amt, gcl35f from #gcm10 with (nolock) where isnull(gcl34c, '') != '' and gcl06i = 1 --Start 计算现金,卡付金额和业绩 add by lm 2015.12.04 insert #cardbill_amt_sum (billid, billtype) select distinct billid, billtype from #cardbill2 insert #bills_2(billid, billtype, staffno) select distinct billid, billtype, staffno from #cardbill2 insert #bills_3(billid, billtype, ret_amt) select distinct billid, billtype, isnull(ret_amt,0) from #cardbill2 declare @id int set @id =0 declare @billid varchar(20) declare @staffno2 varchar(20) declare @cardno varchar(20) declare @billtype int declare @amt float declare @ret_amt float while(@id = 0) begin if exists(select 1 from #bills_2) begin select top 1 @billid = billid, @billtype = billtype, @staffno2 = staffno from #bills_2 select @amt = sum(pay_amt),@ret_amt = sum(ret_amt) from #cardbill2 where billid = @billid and staffno = @staffno2 and billtype = @billtype select @ret_amt = sum(ret_amt) from #bills_3 where billid = @billid and billtype = @billtype update a set a.ret_amt = @ret_amt from #cardbill_amt_sum a where billid = @billid and billtype = @billtype update b set b.amt_sum = @amt from #cardbill2 b where billid = @billid and billtype = @billtype delete from #bills_2 where billid = @billid and billtype = @billtype delete from #bills_3 where billid = @billid and billtype = @billtype set @id = 0 end else begin set @id = 1 end end -- modify by lm 2015.10.23 if @amttype in (0, 2) begin --卡付金额赋值 update a set a.card_amt = a.amt_sum *dbo.fun_divide_default(a.pay_amt, amt_sum,1) from #cardbill2 a where (a.pay_way in('#', '4', '2', '9') or pay_way between 'A' and 'ZZ') --现金赋值 update a set a.cash_amt = a.amt_sum *dbo.fun_divide_default(a.pay_amt, amt_sum,1) from #cardbill2 a where pay_way in(select pay from pay_classify where category = '1' and comp = @compid) update a set a.ret_amt = a.amt_sum *dbo.fun_divide_default(a.pay_amt, amt_sum,1) from #cardbill2 a end else begin --卡付业绩赋值 update a set a.card_amt = a.ret_amt * dbo.fun_divide_default(pay_amt,amt_sum,1) from #cardbill2 a where (a.pay_way in('#', '4', '2', '9') or pay_way between 'A' and 'ZZ') --现金业绩赋值 update a set a.cash_amt = a.ret_amt * dbo.fun_divide_default(pay_amt,amt_sum,1) from #cardbill2 a where pay_way in(select pay from pay_classify where category = '1' and comp = @compid) end if(@amttype = 1 or @amttype = 3) -- 0-营业额 1-虚业绩 2-数量 3-实业绩 begin insert #cardbill(billid, billtype, staffno, cardno, ret_amt, cash_amt, card_amt,rate) select billid, billtype, staffno, cardno, ret_amt, sum(cash_amt), sum(card_amt),rate from #cardbill2 group by billid, billtype, staffno, cardno, ret_amt,rate end else begin insert #cardbill(billid, billtype, staffno, cardno, ret_amt, cash_amt, card_amt,rate) select billid, billtype, staffno, cardno, sum(ret_amt), sum(cash_amt), sum(card_amt),rate from #cardbill2 group by billid, billtype, staffno, cardno, ret_amt,rate end --End -- insert #cardbill_amt_sum (billid, amt,ret_amt) -- select billid, sum(pay_amt),sum(ret_amt) -- from #cardbill2 -- group by billid -- -- -- modify by lm 2015.10.23 -- --卡付业绩赋值 -- update a -- set a.card_amt = a.ret_amt -- from #cardbill2 a -- where (a.pay_way in('#', '4', '2', '9') or pay_way between 'A' and 'ZZ') -- -- --现金业绩赋值 -- update a -- set a.cash_amt = a.ret_amt -- from #cardbill2 a -- where pay_way in(select pay from pay_classify where category = '1' and comp = @compid) -- -- insert #cardbill(billid, billtype, staffno, cardno, ret_amt, cash_amt, card_amt) -- select billid, billtype, staffno, cardno, ret_amt, sum(cash_amt), sum(card_amt) -- from #cardbill2 -- group by billid, billtype, staffno, cardno, ret_amt if @amttype in (0, 2) begin -- insert #results(staffno, type, cnt, amt, cash_amt, card_amt) select distinct staffno, '99-0-1' as type, sum(1) as cnt, sum(ret_amt) as amt, sum(cash_amt) as cash_amt, sum(card_amt) as card_amt from #cardbill where billtype = 1 group by staffno union all select distinct staffno, '99-0-2' as type, sum(1) as cnt, sum(ret_amt) as amt, sum(cash_amt) as cash_amt, sum(card_amt) as card_amt from #cardbill where billtype = 2 group by staffno insert #results(staffno, type, cnt, amt, cash_amt, card_amt) select distinct staffno, '99-0', sum(1), sum(amt), sum(cash_amt) as cash_amt, sum(card_amt) as card_amt from #results where type in ('99-0-1', '99-0-2') group by staffno --start 疗程销售 lucz 2015.10.21 insert #results(staffno, type, liaoc_cs, liaoc_cnt,liaoc_t) select staffno, '99-0-3' as type,sum(num),sum(num),sum(gnc05f) from ( select gna20c as staffno, gnc03f + gnc04f as num, -- 次数 gnc05f --金额 from gnm03 inner join gnm01 on gna00c = gnc00c and gna01c = gnc01c where gnc00c = @compid and isnull(gna20c,'') <> '' and gna02d between @fromdate and @todate and (@fromstaff = '*' or gna20c between @fromstaff and @tostaff) union all select gna28c as staffno, --第二卖卡人 gnc03f + gnc04f as num, -- 次数 gnc05f --金额 from gnm03 inner join gnm01 on gna00c = gnc00c and gna01c = gnc01c where gnc00c = @compid and isnull(gna28c,'') <> '' and gna02d between @fromdate and @todate and (@fromstaff = '*' or gna28c between @fromstaff and @tostaff) union all select gna30c as staffno, --第三卖卡人 gnc03f + gnc04f as num, -- 次数 gnc05f --金额 from gnm03 inner join gnm01 on gna00c = gnc00c and gna01c = gnc01c where gnc00c = @compid and isnull(gna30c,'') <> '' and gna02d between @fromdate and @todate and (@fromstaff = '*' or gna30c between @fromstaff and @tostaff) union all select gna32c as staffno, --第四卖卡人 gnc03f + gnc04f as num, -- 次数 gnc05f --金额 from gnm03 inner join gnm01 on gna00c = gnc00c and gna01c = gnc01c where gnc00c = @compid and isnull(gna32c,'') <> '' and gna02d between @fromdate and @todate and (@fromstaff = '*' or gna32c between @fromstaff and @tostaff) union all select gna34c as staffno, --第5卖卡人 gnc03f + gnc04f as num, -- 次数 gnc05f --金额 from gnm03 inner join gnm01 on gna00c = gnc00c and gna01c = gnc01c where gnc00c = @compid and isnull(gna34c,'') <> '' and gna02d between @fromdate and @todate and (@fromstaff = '*' or gna34c between @fromstaff and @tostaff) union all select gna36c as staffno, --第6卖卡人 gnc03f + gnc04f as num, -- 次数 gnc05f --金额 from gnm03 inner join gnm01 on gna00c = gnc00c and gna01c = gnc01c where gnc00c = @compid and isnull(gna36c,'') <> '' and gna02d between @fromdate and @todate and (@fromstaff = '*' or gna36c between @fromstaff and @tostaff) union all select gna38c as staffno, --第7卖卡人 gnc03f + gnc04f as num, -- 次数 gnc05f --金额 from gnm03 inner join gnm01 on gna00c = gnc00c and gna01c = gnc01c where gnc00c = @compid and isnull(gna38c,'') <> '' and gna02d between @fromdate and @todate and (@fromstaff = '*' or gna38c between @fromstaff and @tostaff) union all select gna40c as staffno, --第8卖卡人 gnc03f + gnc04f as num, -- 次数 gnc05f --金额 from gnm03 inner join gnm01 on gna00c = gnc00c and gna01c = gnc01c where gnc00c = @compid and isnull(gna40c,'') <> '' and gna02d between @fromdate and @todate and (@fromstaff = '*' or gna40c between @fromstaff and @tostaff) union all select gna42c as staffno, --第9卖卡人 gnc03f + gnc04f as num, -- 次数 gnc05f --金额 from gnm03 inner join gnm01 on gna00c = gnc00c and gna01c = gnc01c where gnc00c = @compid and isnull(gna42c,'') <> '' and gna02d between @fromdate and @todate and (@fromstaff = '*' or gna42c between @fromstaff and @tostaff) union all select gna44c as staffno, --第10卖卡人 gnc03f + gnc04f as num, -- 次数 gnc05f --金额 from gnm03 inner join gnm01 on gna00c = gnc00c and gna01c = gnc01c where gnc00c = @compid and isnull(gna44c,'') <> '' and gna02d between @fromdate and @todate and (@fromstaff = '*' or gna44c between @fromstaff and @tostaff) ) as xs_table group by staffno --疗程充值 insert #results(staffno, type, liaoc_cs, liaoc_cnt,liaoc_t) select staffno, '99-0-4' as type,sum(num),sum(num),sum(gcm05f) from ( select gcl09c as staffno, gcm03f + gcm04f as num, gcm05f --金额 from gcm11 inner join gcm10 with(nolock) on gcl01c = gcm01c and gcl00c = gcm00c where gcm00c = @compid and isnull(gcl09c,'') <> '' and gcl06i = 0 and gcl04c not in('888', '889') and gcl80d between @fromdate and @todate and (@fromstaff = '*' or gcl09c between @fromstaff and @tostaff) union all select gcl17c as staffno, --第2销售 gcm03f + gcm04f as num, gcm05f --金额 from gcm11 inner join gcm10 on gcl01c = gcm01c and gcl00c = gcm00c where gcm00c = @compid and isnull(gcl17c,'') <> '' and gcl06i = 0 and gcl04c not in('888', '889') and gcl80d between @fromdate and @todate and (@fromstaff = '*' or gcl17c between @fromstaff and @tostaff) union all select gcl19c as staffno, --第3销售 gcm03f + gcm04f as num, gcm05f --金额 from gcm11 inner join gcm10 on gcl01c = gcm01c and gcl00c = gcm00c where gcm00c = @compid and isnull(gcl19c,'') <> '' and gcl06i = 0 and gcl04c not in('888', '889') and gcl80d between @fromdate and @todate and (@fromstaff = '*' or gcl19c between @fromstaff and @tostaff) union all select gcl22c as staffno, --第4销售 gcm03f + gcm04f as num, gcm05f --金额 from gcm11 inner join gcm10 on gcl01c = gcm01c and gcl00c = gcm00c where gcm00c = @compid and isnull(gcl22c,'') <> '' and gcl06i = 0 and gcl04c not in('888', '889') and gcl80d between @fromdate and @todate and (@fromstaff = '*' or gcl22c between @fromstaff and @tostaff) union all select gcl24c as staffno, --第5销售 gcm03f + gcm04f as num, gcm05f --金额 from gcm11 inner join gcm10 on gcl01c = gcm01c and gcl00c = gcm00c where gcm00c = @compid and isnull(gcl24c,'') <> '' and gcl06i = 0 and gcl04c not in('888', '889') and gcl80d between @fromdate and @todate and (@fromstaff = '*' or gcl24c between @fromstaff and @tostaff) union all select gcl26c as staffno, --第6销售 gcm03f + gcm04f as num, gcm05f --金额 from gcm11 inner join gcm10 on gcl01c = gcm01c and gcl00c = gcm00c where gcm00c = @compid and isnull(gcl26c,'') <> '' and gcl06i = 0 and gcl04c not in('888', '889') and gcl80d between @fromdate and @todate and (@fromstaff = '*' or gcl26c between @fromstaff and @tostaff) union all select gcl28c as staffno, --第7销售 gcm03f + gcm04f as num, gcm05f --金额 from gcm11 inner join gcm10 on gcl01c = gcm01c and gcl00c = gcm00c where gcm00c = @compid and isnull(gcl28c,'') <> '' and gcl06i = 0 and gcl04c not in('888', '889') and gcl80d between @fromdate and @todate and (@fromstaff = '*' or gcl28c between @fromstaff and @tostaff) union all select gcl30c as staffno, --第8销售 gcm03f + gcm04f as num, gcm05f --金额 from gcm11 inner join gcm10 on gcl01c = gcm01c and gcl00c = gcm00c where gcm00c = @compid and isnull(gcl30c,'') <> '' and gcl06i = 0 and gcl04c not in('888', '889') and gcl80d between @fromdate and @todate and (@fromstaff = '*' or gcl30c between @fromstaff and @tostaff) union all select gcl32c as staffno, --第9销售 gcm03f + gcm04f as num, gcm05f --金额 from gcm11 inner join gcm10 on gcl01c = gcm01c and gcl00c = gcm00c where gcm00c = @compid and isnull(gcl32c,'') <> '' and gcl06i = 0 and gcl04c not in('888', '889') and gcl80d between @fromdate and @todate and (@fromstaff = '*' or gcl32c between @fromstaff and @tostaff) union all select gcl34c as staffno, --第10销售 gcm03f + gcm04f as num, gcm05f --金额 from gcm11 inner join gcm10 on gcl01c = gcm01c and gcl00c = gcm00c where gcm00c = @compid and isnull(gcl34c,'') <> '' and gcl06i = 0 and gcl04c not in('888', '889') and gcl80d between @fromdate and @todate and (@fromstaff = '*' or gcl34c between @fromstaff and @tostaff) ) as cz_table group by staffno --疗程卡销售和卡充值汇总 insert #results(staffno, type, cnt, amt) select staffno, '99-0-5', sum(liaoc_cs), sum(liaoc_t) from #results where type in ('99-0-3', '99-0-4') group by staffno --end lucz 2015.10.21 end else if @amttype in (1, 3) --实业绩,虚业绩 begin insert #results(staffno, type, cnt, amt, cash_amt, card_amt) select staffno, type as type, sum(cnt) as cnt, sum(amt) as amt,sum(cash_amt) as cash_amt, sum(card_amt) as card_amt from ( --充值数量, 业绩 select distinct staffno, '99-0-2' as type, sum(1) as cnt, sum(ret_amt) as amt, sum(cash_amt) as cash_amt, sum(card_amt) as card_amt from #cardbill where billtype = 2 group by staffno union all --买卡数量 select distinct staffno, '99-0-1' as type, sum(1) as cnt, 0 as amt, 0 as cash_amt, 0 as card_amt from #cardbill where billtype = 1 group by staffno union all --买卡业绩 select distinct staffno, '99-0-1' as type, 0 as cnt, sum(ret_amt) as amt, sum(cash_amt) as cash_amt, sum(card_amt) as card_amt from #cardbill, ( select billid as billid_tmp, min(idd) as idd_tmp from #cardbill where billtype = 1 group by billid, staffno )as tmp where billid = billid_tmp and idd = idd_tmp and billtype = 1 group by staffno ) as tmp group by staffno, type insert #results(staffno, type, cnt, amt, cash_amt, card_amt) select staffno, '99-0', sum(cnt), sum(amt), sum(cash_amt) as cash_amt, sum(card_amt) as card_amt from #results where type in ('99-0-1', '99-0-2') group by staffno --start 疗程销售 lucz 2015.10.21 insert #results(staffno, type, liaoc_cs, liaoc_cnt,liaoc_t) select staffno, '99-0-3' as type,sum(num),sum(num),sum(jine) from ( select gna20c as staffno, gnc03f + gnc04f as num, -- 次数 gnc05f*gna27f as jine --疗程金额 * 分享比率 from gnm03 inner join gnm01 on gna00c = gnc00c and gna01c = gnc01c where gnc00c = @compid and isnull(gna20c,'') <> '' and gna02d between @fromdate and @todate and (@fromstaff = '*' or gna20c between @fromstaff and @tostaff) union all select gna28c as staffno, --第二卖卡人 gnc03f + gnc04f as num, -- 次数 gnc05f*gna29f as jine --疗程金额 * 分享比率 from gnm03 inner join gnm01 on gna00c = gnc00c and gna01c = gnc01c where gnc00c = @compid and isnull(gna28c,'') <> '' and gna02d between @fromdate and @todate and (@fromstaff = '*' or gna28c between @fromstaff and @tostaff) union all select gna30c as staffno, --第三卖卡人 gnc03f + gnc04f as num, -- 次数 gnc05f*gna31f as jine --疗程金额 * 分享比率 from gnm03 inner join gnm01 on gna00c = gnc00c and gna01c = gnc01c where gnc00c = @compid and isnull(gna30c,'') <> '' and gna02d between @fromdate and @todate and (@fromstaff = '*' or gna30c between @fromstaff and @tostaff) union all select gna32c as staffno, --第四卖卡人 gnc03f + gnc04f as num, -- 次数 gnc05f*gna33f as jine --疗程金额 * 分享比率 from gnm03 inner join gnm01 on gna00c = gnc00c and gna01c = gnc01c where gnc00c = @compid and isnull(gna32c,'') <> '' and gna02d between @fromdate and @todate and (@fromstaff = '*' or gna32c between @fromstaff and @tostaff) union all select gna34c as staffno, --第5卖卡人 gnc03f + gnc04f as num, -- 次数 gnc05f*gna35f as jine --疗程金额 * 分享比率 from gnm03 inner join gnm01 on gna00c = gnc00c and gna01c = gnc01c where gnc00c = @compid and isnull(gna34c,'') <> '' and gna02d between @fromdate and @todate and (@fromstaff = '*' or gna34c between @fromstaff and @tostaff) union all select gna36c as staffno, --第6卖卡人 gnc03f + gnc04f as num, -- 次数 gnc05f*gna37f as jine --疗程金额 * 分享比率 from gnm03 inner join gnm01 on gna00c = gnc00c and gna01c = gnc01c where gnc00c = @compid and isnull(gna36c,'') <> '' and gna02d between @fromdate and @todate and (@fromstaff = '*' or gna36c between @fromstaff and @tostaff) union all select gna38c as staffno, --第7卖卡人 gnc03f + gnc04f as num, -- 次数 gnc05f*gna39f as jine --疗程金额 * 分享比率 from gnm03 inner join gnm01 on gna00c = gnc00c and gna01c = gnc01c where gnc00c = @compid and isnull(gna38c,'') <> '' and gna02d between @fromdate and @todate and (@fromstaff = '*' or gna38c between @fromstaff and @tostaff) union all select gna40c as staffno, --第8卖卡人 gnc03f + gnc04f as num, -- 次数 gnc05f*gna41f as jine --疗程金额 * 分享比率 from gnm03 inner join gnm01 on gna00c = gnc00c and gna01c = gnc01c where gnc00c = @compid and isnull(gna40c,'') <> '' and gna02d between @fromdate and @todate and (@fromstaff = '*' or gna40c between @fromstaff and @tostaff) union all select gna42c as staffno, --第9卖卡人 gnc03f + gnc04f as num, -- 次数 gnc05f*gna43f as jine --疗程金额 * 分享比率 from gnm03 inner join gnm01 on gna00c = gnc00c and gna01c = gnc01c where gnc00c = @compid and isnull(gna42c,'') <> '' and gna02d between @fromdate and @todate and (@fromstaff = '*' or gna42c between @fromstaff and @tostaff) union all select gna44c as staffno, --第10卖卡人 gnc03f + gnc04f as num, -- 次数 gnc05f*gna45f as jine --疗程金额 * 分享比率 from gnm03 inner join gnm01 on gna00c = gnc00c and gna01c = gnc01c where gnc00c = @compid and isnull(gna44c,'') <> '' and gna02d between @fromdate and @todate and (@fromstaff = '*' or gna44c between @fromstaff and @tostaff) ) as xs_table group by staffno --疗程充值 insert #results(staffno, type, liaoc_cs, liaoc_cnt,liaoc_t) select staffno, '99-0-4' as type,sum(num),sum(num),sum(jine) from ( select gcl09c as staffno, num = (select top 1 gcm03f + gcm04f from gcm11 where gcm00c=gcl00c and gcm01c = gcl01c ), gcl66f * gcl16f as jine --总疗程金额 * 分享比率 from gcm10 where gcl00c = @compid and gcl06i = 0 and isnull(gcl09c,'') <> '' and gcl02d between @fromdate and @todate and (@fromstaff = '*' or gcl09c between @fromstaff and @tostaff) union all select gcl17c as staffno, --第2销售 num = (select top 1 gcm03f + gcm04f from gcm11 where gcm00c=gcl00c and gcm01c = gcl01c ), gcl66f * gcl18f as jine --总疗程金额 * 分享比率 from gcm10 where gcl00c = @compid and gcl06i = 0 and isnull(gcl17c,'') <> '' and gcl02d between @fromdate and @todate and (@fromstaff = '*' or gcl17c between @fromstaff and @tostaff) union all select gcl19c as staffno, --第3销售 num = (select top 1 gcm03f + gcm04f from gcm11 where gcm00c=gcl00c and gcm01c = gcl01c ), gcl66f * gcl20f as jine --总疗程金额 * 分享比率 from gcm10 where gcl00c = @compid and gcl06i = 0 and isnull(gcl19c,'') <> '' and gcl02d between @fromdate and @todate and (@fromstaff = '*' or gcl19c between @fromstaff and @tostaff) union all select gcl22c as staffno, --第4销售 num = (select top 1 gcm03f + gcm04f from gcm11 where gcm00c=gcl00c and gcm01c = gcl01c ), gcl66f * gcl23f as jine --总疗程金额 * 分享比率 from gcm10 where gcl00c = @compid and gcl06i = 0 and isnull(gcl22c,'') <> '' and gcl02d between @fromdate and @todate and (@fromstaff = '*' or gcl22c between @fromstaff and @tostaff) union all select gcl24c as staffno, --第5销售 num = (select top 1 gcm03f + gcm04f from gcm11 where gcm00c=gcl00c and gcm01c = gcl01c ), gcl66f * gcl25f as jine --总疗程金额 * 分享比率 from gcm10 where gcl00c = @compid and gcl06i = 0 and isnull(gcl24c,'') <> '' and gcl02d between @fromdate and @todate and (@fromstaff = '*' or gcl24c between @fromstaff and @tostaff) union all select gcl26c as staffno, --第6销售 num = (select top 1 gcm03f + gcm04f from gcm11 where gcm00c=gcl00c and gcm01c = gcl01c ), gcl66f * gcl27f as jine --总疗程金额 * 分享比率 from gcm10 where gcl00c = @compid and gcl06i = 0 and isnull(gcl26c,'') <> '' and gcl02d between @fromdate and @todate and (@fromstaff = '*' or gcl26c between @fromstaff and @tostaff) union all select gcl28c as staffno, --第7销售 num = (select top 1 gcm03f + gcm04f from gcm11 where gcm00c=gcl00c and gcm01c = gcl01c ), gcl66f * gcl29f as jine --总疗程金额 * 分享比率 from gcm10 where gcl00c = @compid and gcl06i = 0 and isnull(gcl28c,'') <> '' and gcl02d between @fromdate and @todate and (@fromstaff = '*' or gcl28c between @fromstaff and @tostaff) union all select gcl30c as staffno, --第8销售 num = (select top 1 gcm03f + gcm04f from gcm11 where gcm00c=gcl00c and gcm01c = gcl01c ), gcl66f * gcl31f as jine --总疗程金额 * 分享比率 from gcm10 where gcl00c = @compid and gcl06i = 0 and isnull(gcl30c,'') <> '' and gcl02d between @fromdate and @todate and (@fromstaff = '*' or gcl30c between @fromstaff and @tostaff) union all select gcl32c as staffno, --第9销售 num = (select top 1 gcm03f + gcm04f from gcm11 where gcm00c=gcl00c and gcm01c = gcl01c ), gcl66f * gcl33f as jine --总疗程金额 * 分享比率 from gcm10 where gcl00c = @compid and gcl06i = 0 and isnull(gcl32c,'') <> '' and gcl02d between @fromdate and @todate and (@fromstaff = '*' or gcl32c between @fromstaff and @tostaff) union all select gcl34c as staffno, --第10销售 num = (select top 1 gcm03f + gcm04f from gcm11 where gcm00c=gcl00c and gcm01c = gcl01c ), gcl66f * gcl35f as jine --总疗程金额 * 分享比率 from gcm10 where gcl00c = @compid and gcl06i = 0 and isnull(gcl34c,'') <> '' and gcl02d between @fromdate and @todate and (@fromstaff = '*' or gcl34c between @fromstaff and @tostaff) -- 充值 还款 union all select gcl09c as staffno, 0 as num, gcl66f * gcl16f as jine --总疗程金额 * 分享比率 from gcm10 where gcl00c = @compid and gcl06i = 6 and gcl04c = '4' and isnull(gcl09c,'') <> '' and gcl02d between @fromdate and @todate and (@fromstaff = '*' or gcl09c between @fromstaff and @tostaff) union all select gcl17c as staffno, --第2销售 0 as num, gcl66f * gcl18f as jine --总疗程金额 * 分享比率 from gcm10 where gcl00c = @compid and gcl06i = 6 and gcl04c = '4' and isnull(gcl17c,'') <> '' and gcl02d between @fromdate and @todate and (@fromstaff = '*' or gcl17c between @fromstaff and @tostaff) union all select gcl19c as staffno, --第3销售 0 as num, gcl66f * gcl20f as jine --总疗程金额 * 分享比率 from gcm10 where gcl00c = @compid and gcl06i = 6 and gcl04c = '4' and isnull(gcl19c,'') <> '' and gcl02d between @fromdate and @todate and (@fromstaff = '*' or gcl19c between @fromstaff and @tostaff) union all select gcl22c as staffno, --第4销售 0 as num, gcl66f * gcl23f as jine --总疗程金额 * 分享比率 from gcm10 where gcl00c = @compid and gcl06i = 6 and gcl04c = '4' and isnull(gcl22c,'') <> '' and gcl02d between @fromdate and @todate and (@fromstaff = '*' or gcl22c between @fromstaff and @tostaff) union all select gcl24c as staffno, --第5销售 0 as num, gcl66f * gcl25f as jine --总疗程金额 * 分享比率 from gcm10 where gcl00c = @compid and gcl06i = 6 and gcl04c = '4' and isnull(gcl24c,'') <> '' and gcl02d between @fromdate and @todate and (@fromstaff = '*' or gcl24c between @fromstaff and @tostaff) union all select gcl26c as staffno, --第6销售 0 as num, gcl66f * gcl27f as jine --总疗程金额 * 分享比率 from gcm10 where gcl00c = @compid and gcl06i = 6 and gcl04c = '4' and isnull(gcl26c,'') <> '' and gcl02d between @fromdate and @todate and (@fromstaff = '*' or gcl26c between @fromstaff and @tostaff) union all select gcl28c as staffno, --第7销售 0 as num, gcl66f * gcl29f as jine --总疗程金额 * 分享比率 from gcm10 where gcl00c = @compid and gcl06i = 6 and gcl04c = '4' and isnull(gcl28c,'') <> '' and gcl02d between @fromdate and @todate and (@fromstaff = '*' or gcl28c between @fromstaff and @tostaff) union all select gcl30c as staffno, --第8销售 0 as num, gcl66f * gcl31f as jine --总疗程金额 * 分享比率 from gcm10 where gcl00c = @compid and gcl06i = 6 and gcl04c = '4' and isnull(gcl30c,'') <> '' and gcl02d between @fromdate and @todate and (@fromstaff = '*' or gcl30c between @fromstaff and @tostaff) union all select gcl32c as staffno, --第9销售 0 as num, gcl66f * gcl33f as jine --总疗程金额 * 分享比率 from gcm10 where gcl00c = @compid and gcl06i = 6 and gcl04c = '4' and isnull(gcl32c,'') <> '' and gcl02d between @fromdate and @todate and (@fromstaff = '*' or gcl32c between @fromstaff and @tostaff) union all select gcl34c as staffno, --第10销售 0 as num, gcl66f * gcl35f as jine --总疗程金额 * 分享比率 from gcm10 where gcl00c = @compid and gcl06i = 6 and gcl04c = '4' and isnull(gcl34c,'') <> '' and gcl02d between @fromdate and @todate and (@fromstaff = '*' or gcl34c between @fromstaff and @tostaff) ) as cz_table group by staffno --疗程卡销售和卡充值汇总 insert #results(staffno, type, cnt, amt) select staffno, '99-0-5', sum(liaoc_cs), sum(liaoc_t) from #results where type in ('99-0-3', '99-0-4') group by staffno --end lucz 2015.10.21 end --计算数量 delete from #cardbill2 insert #cardbill2 (billid, billtype, staffno) select distinct billid, billtype, staffno from #cardbill insert #cardbill_num (staffno, num) select staffno,sum(1) from #cardbill2 where billtype = 1 group by staffno update a set a.cnt = b.num from #results a, #cardbill_num b where a.staffno = b.staffno and type = '99-0-1' delete from #cardbill_num insert #cardbill_num (staffno, num) select staffno,sum(1) from #cardbill2 where billtype = 2 group by staffno update a set a.cnt = b.num from #results a, #cardbill_num b where a.staffno = b.staffno and type = '99-0-2' delete from #cardbill_num insert #cardbill_num (staffno, num) select staffno,sum(1) from #cardbill2 group by staffno update a set a.cnt = b.num from #results a, #cardbill_num b where a.staffno = b.staffno and type = '99-0' update #results set price = case when isnull(cnt, 0) = 0 then 0 else amt/cnt end declare @printtotalcom varchar(1) --打印总提成 declare @printitemcom varchar(1) --打印项目提成 declare @printgoodscom varchar(1) --打印产品提成 declare @printcardcom varchar(1) --打印卡提成 set @printtotalcom = substring(@printcom, 1, 1) set @printitemcom = substring(@printcom, 2, 1) set @printgoodscom = substring(@printcom, 3, 1) set @printcardcom = substring(@printcom, 4, 1) --提成 if @printtotalcom = '1' or @printitemcom = '1' or @printgoodscom = '1' or @printcardcom = '1' begin create table #com ( staffno varchar(20) not null, com float null ) declare @spid int select @spid = @@spid + 1000 insert #com(staffno, com) exec upg_personal_statistics @compid, @fromdate, @todate, @fromstaff, @tostaff, 1, 1, 1, 2, @spid insert #work_detail(person_id,action_id,srvdate,code ,name,payway, amt,quan,cost,comm,amt2,amt3,cardrate,stdprice,prj_type,share_rate, cls_flag,gender,billid,paycode,ex_rate,ggm02_seq,room_id,compid, spid,cardid,memid ,comp_ascription ) select person_id,action_id,srvdate,code ,name,payway, amt,quan,cost,comm,amt2,amt3,cardrate,stdprice,prj_type,share_rate, cls_flag,gender,billid,paycode,ex_rate,ggm02_seq,room_id,compid, spid,cardid,memid ,comp_ascription from work_detail with (nolock) where spid = @spid delete #com if (@printtotalcom = '1') begin insert #com(staffno, com) select person_id, sum(comm) from #work_detail group by person_id insert #results(staffno, type, cnt, amt) select staffno, 'T-1' as type, 0 as cnt, com as amt from #com end if (@printitemcom = '1') --项目提成 begin delete #com insert #com(staffno, com) select person_id, sum(comm) from #work_detail where (action_id >= 2 and action_id <= 8) or (action_id >= 10 and action_id <= 11) group by person_id insert #results(staffno, type, cnt, amt) select staffno, 'T-2' as type, 0 as cnt, com as amt from #com end if (@printgoodscom = '1') --产品提成 begin delete #com insert #com(staffno, com) select person_id, sum(comm) from #work_detail where action_id = 9 group by person_id insert #results(staffno, type, cnt, amt) select staffno, 'T-3' as type, 0 as cnt, com as amt from #com end if (@printcardcom = '1') --卡提成 begin delete #com insert #com(staffno, com) select person_id, sum(comm) from #work_detail where (action_id = 0 or action_id = 1) group by person_id insert #results(staffno, type, cnt, amt) select staffno, 'T-4' as type, 0 as cnt, com as amt from #com end drop table #com end --增加劳动业绩 add by lm 2016.01.04 create table #jobamt ( personid varchar(20) not null, job_amt_x float null, job_amt_c float null ) insert #jobamt (personid) select distinct staffno from #results a with(nolock) --项目 0-营业额 1-虚业绩 2-数量 3-实业绩 update a set a.job_amt_x = newTable.amt from #jobamt a , ( select person_id, case @amttype when 1 then sum(isnull(b.amt2,0)) when 3 then sum(isnull(b.amt3,0)) else sum(isnull(b.amt,0) * isnull(c.gso08f,0)) end amt from #work_detail b , gsm15 c with(nolock) where b.compid = c.gso00c and spid = @spid and b.paycode = c.gso02c and b.compid = @compid and ((action_id >= 2 and action_id <= 8) or (action_id >= 10 and action_id <= 11)) and isnull(gso09f,0) = 1 group by person_id ) as newTable where a.personid = newTable.person_id --产品 update a set a.job_amt_x = newTable.amt from #jobamt a , ( select person_id,case @amttype when 1 then sum(isnull(b.amt2,0)) when 3 then sum(isnull(b.amt3,0)) else sum(isnull(b.amt,0) * isnull(c.gso08f,0)) end amt from #work_detail b , gsm15 c with(nolock) where b.compid = c.gso00c and spid = @spid and b.paycode = c.gso02c and b.compid = @compid and action_id = 9 and isnull(gso10f,0) = 1 group by person_id ) as newTable where a.personid = newTable.person_id insert #results(staffno, type, cnt, amt) select personid, 'J-1' as type, 0 as cnt, (isnull(job_amt_x,0) + isnull(job_amt_c,0)) as amt from #jobamt delete #results where isnull(itemtype, '') = '' and type in ('itemclass', 'itemgroup') select isnull(staffno,'') staffno, isnull(type,'') type, isnull(itemtype,'') itemtype, isnull(cnt,'') cnt, isnull(amt,'') amt, isnull(price,'') price, isnull(cnt_f,'') cnt_f, isnull(amt_f,'') amt_f, isnull(cnt_t,'') cnt_t, isnull(amt_t,'') amt_t, isnull(cash_amt,'') cash_amt, isnull(cash_cnt,'') cash_cnt, isnull(card_amt,'') card_amt, isnull(card_cnt,'') card_cnt, isnull(else_amt,'') else_amt, isnull(else_cnt,'') else_cnt, isnull(tamt,'') tamt, isnull(liaoc_cs,'') liaoc_cs, isnull(liaoc_cnt,'') liaoc_cnt, isnull(liaoc_t,'') liaoc_t, isnull(liaoc_sum,'') liaoc_sum, isnull(liaoc_jine,'') liaoc_jine, isnull(liaoc_chuz,'') liaoc_chuz, isnull(liaoc_hank,'') liaoc_hank, isnull(liaoc_qita,'') liaoc_qita, isnull(job_amt,'') job_amt from #results order by staffno, type, itemtype --end drop table #cardbill_num drop table #jobamt drop table #work_detail drop table #ggm01_ggm02 drop table #bills drop table #allbill drop table #results drop table #gnm01_gnm02 drop table #gcm10 drop table #gcm11 drop table #cardbill drop table #bills_2 drop table #bills_3 drop table #tb_liaocheng drop table #liaoc_payment end GO /****** Object: StoredProcedure [dbo].[upg_goods_sale_report] Script Date: 06/12/2017 06:20:11 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_goods_sale_report] ( @compid varchar(10), -- 公司别 @flag int, -- 1:销售 2: 消耗, 3 全部, 4赠送, 5退货, 6纯销售 Add by maql 20100505 @condition1 varchar(1024), @condition2 varchar(1024), @catelogy varchar(10), -- 统计产品类别 @owe int, -- 是否包含欠货 @kind int, -- 1: 不同价格的产品单独显示, 2: 同个产品分组合计 @fromgoodstype varchar(10), -- 产品统计分类 @tomgoodstype varchar(10) -- 产品统计分类 ) as --modified by mzh 2005/08/04 begin declare @sql nvarchar(1024) declare @includesons float --销售统计报表,无论如何都要统计到下面的门店 select @includesons = 1 create table #good_result ( idd int identity not null, catelogy varchar(10) null, --产品分类 goodstype varchar(10) null, --产品统计分类 item varchar(20) not null, itemname varchar(100) null, costprice float null, --进价 unitprice float null, --标准价格 saleprice float null, --销售价格 salecostprice float null, --销售成本单价(产品资料中的销售成本) quan float null, --数量 saler varchar(10) null, --销售人员 saledate varchar(8) null, --销售日期 saletime varchar(6) null, --销售时间 到分钟 fact_quan float null, --经过单位转换的实际数量 unit varchar(10) null, costamt float null, --成本总金额 amt float null, --金额 salecostamt float null, --销售成本金额 type int null, -- 1: 销售 2:消耗 paymode varchar(5) null, -- 支付方式 &代表消耗 stock varchar(20) null, --add by lyj 仓库 sendnum float null, --赠送的数量 sendamt float null, --赠送的金额 profit_1 float null, -- 毛利润是 零售价总额-销售成本总价 profit_2 float null, -- 净利润是 销售成本总额-进货成本价总额 ) declare @PK_Sql nvarchar(max) set @PK_Sql = ' alter table #good_result add constraint PK_#good_result_' + convert(varchar(10), cast(floor(rand() * 1000000) as int)) + ' primary key(idd)' exec sp_executesql @PK_Sql --add by lyj declare @SP044 varchar(20) declare @SP045 varchar(20) declare @payment_send varchar(20) -- 赠送的对应支付方式 select @SP044 = gsa02c from gsm01 with(nolock) where gsa00c = @compid and gsa01c = 'SP044' select @SP045 = gsa02c from gsm01 with(nolock) where gsa00c = @compid and gsa01c = 'SP045' select @payment_send = gsa02c from gsm01 with(nolock) where gsa00c = @compid and gsa01c = 'SP058' --产品销售以及全部 -- 1:销售 2: 消耗, 3 全部, 4赠送, 5退货, 6纯销售 if @flag = 1 or @flag = 3 or @flag = 4 or @flag = 5 or @flag = 6 --modified by maql 20100505 begin if @owe = 1 begin if @includesons = 1 begin select @sql = 'insert #good_result(item, costprice, saleprice, quan, saler, saledate, saletime, costamt, amt, type, paymode, stock, unitprice, salecostprice) select ggc03c, isnull(ggc07f, 0), ggc10f, ggc05f, ggc12c, gga02d, gga03t, isnull(ggc07f, 0)*isnull(ggc05f, 0), ggc11f, 1, ggc27c, a = case when isnull(ggc31c, '''') = '''' then ''' + @SP044 + ''' else ggc31c end, isnull(b.ggc08f, 0), isnull(d.gfa19f, 0) from ggm03 b with (nolock), ggm01 a with (nolock), gam26 z with(nolock), gfm01 d with(nolock) where a.gga00c = b.ggc00c and a.gga01c = b.ggc01c and isnull(ggc03c, '''')<>'''' and a.gga00c = z.gaz02c and b.ggc00c = d.gfa00c and b.ggc03c = d.gfa01c and z.gaz01c = ''' + @compid + '''' end else begin select @sql = 'insert #good_result(item, costprice, saleprice, quan, saler, saledate, saletime, costamt, amt, type, paymode, stock, unitprice, salecostprice) select ggc03c, isnull(ggc07f, 0), ggc10f, ggc05f, ggc12c, gga02d, gga03t, isnull(ggc07f, 0)*isnull(ggc05f, 0), ggc11f, 1, ggc27c, a = case when isnull(ggc31c, '''') = '''' then ''' + @SP044 + ''' else ggc31c end, isnull(b.ggc08f, 0), isnull(d.gfa19f, 0) from ggm03 b with (nolock), ggm01 a with (nolock), gfm01 d with(nolock) where a.gga00c = b.ggc00c and a.gga01c = b.ggc01c and b.ggc00c = d.gfa00c and b.ggc03c = d.gfa01c and isnull(ggc03c, '''')<>'''' and a.gga00c = ''' + @compid + '''' end end else begin if @includesons = 1 begin select @sql = 'insert #good_result(item, costprice, saleprice, quan, saler, saledate, saletime, costamt, amt, type, paymode, stock, unitprice, salecostprice) select ggc03c, isnull(ggc07f, 0), ggc10f, ggc05f, ggc12c, gga02d, gga03t, isnull(ggc07f, 0)*isnull(ggc05f, 0), ggc11f, 1, ggc27c, a = case when isnull(ggc31c, '''') = '''' then ''' + @SP044 + ''' else ggc31c end, isnull(b.ggc08f, 0), isnull(d.gfa19f, 0) from ggm03 b with (nolock), ggm01 a with (nolock), gam26 z with(nolock), gfm01 d with(nolock) where a.gga00c = b.ggc00c and a.gga01c = b.ggc01c and isnull(ggc03c, '''')<>'''' and a.gga00c = z.gaz02c and b.ggc00c = d.gfa00c and b.ggc03c = d.gfa01c and b.ggc36i<>2 --不包括欠款 and z.gaz01c = ''' + @compid + '''' end else begin select @sql = 'insert #good_result(item, costprice, saleprice, quan, saler, saledate, saletime, costamt, amt, type, paymode, stock, unitprice, salecostprice) select ggc03c, isnull(ggc07f, 0), ggc10f, ggc05f, ggc12c, gga02d, gga03t, isnull(ggc07f, 0)*isnull(ggc05f, 0), ggc11f, 1, ggc27c, a = case when isnull(ggc31c, '''') = '''' then ''' + @SP044 + ''' else ggc31c end, isnull(b.ggc08f, 0), isnull(d.gfa19f, 0) from ggm03 b with (nolock), ggm01 a with (nolock), gfm01 d with(nolock) where a.gga00c = b.ggc00c and a.gga01c = b.ggc01c and isnull(ggc03c, '''')<>'''' and b.ggc36i<>2 and b.ggc00c = d.gfa00c and b.ggc03c = d.gfa01c and a.gga00c = ''' + @compid + '''' end end --赠送 Add by maql 20100505 if @flag = 4 select @sql = @sql + ' and b.ggc27c = ''' + @payment_send + '''' --退货 Add by maql 20100505 if @flag = 5 select @sql = @sql + ' and b.ggc05f < 0 ' --纯销售 Add by maql 20100505 if @flag = 6 select @sql = @sql + ' and b.ggc27c <> ''' + @payment_send + ''' and b.ggc05f > 0 ' if @condition1<>'' select @sql = @sql + ' and ' + @condition1; exec sp_executesql @sql --把其中赠送的产品数量与金额单独列出来 update a set a.sendnum = quan, a.sendamt = amt from #good_result a where a.paymode = @payment_send update a set a.quan = isnull(a.quan, 0)-isnull(a.sendnum, 0), a.amt = isnull(amt, 0) - isnull(a.sendamt, 0) from #good_result a where a.paymode = @payment_send end if @flag = 2 or @flag = 3 begin if @includesons = 1 begin select @sql = 'insert #good_result(item, costprice, saleprice, quan, saler, saledate, saletime, costamt, amt, type, paymode, stock, unitprice, salecostprice) select gge03c, isnull(gge07f, 0), gge10f, gge05f, gge12c, gga02d, gga03t, isnull(gge07f, 0)*( case when isnull(gfa09f, 0) = 0 then 0 else (gge05f/isnull(gfa09f, 0)) end ), gge11f, 2, ''' + ''' , a = case when isnull(gge15c, '''') = '''' then ''' + @SP044 + ''' else gge15c end, isnull(gfa11f, 0), isnull(gfa19f, 0) from ggm05 b with (nolock), ggm01 a with (nolock), gam26 z with(nolock), gfm01 d with(nolock) where a.gga00c = b.gge00c and a.gga01c = b.gge01c and a.gga00c = d.gfa00c and b.gge03c = d.gfa01c and isnull(gge03c, '''')<>'''' and a.gga00c = z.gaz02c and z.gaz01c = ''' + @compid + '''' end else begin select @sql = 'insert #good_result(item, costprice, saleprice, quan, saler, saledate, saletime, costamt, amt, type, paymode, stock, unitprice, salecostprice) select gge03c, isnull(gge07f, 0), gge10f, gge05f, gge12c, gga02d, gga03t, isnull(gge07f, 0)*( case when isnull(gfa09f, 0) = 0 then 0 else (gge05f/isnull(gfa09f, 0)) end ), gge11f, 2, ''' + ''' , a = case when isnull(gge15c, '''') = '''' then ''' + @SP044 + ''' else gge15c end, isnull(gfa11f, 0), isnull(gfa19f, 0) from ggm05 b with (nolock), ggm01 a with (nolock), gfm01 d with(nolock) where a.gga00c = b.gge00c and a.gga01c = b.gge01c and a.gga00c = d.gfa00c and b.gge03c = d.gfa01c and isnull(gge03c, '''')<>'''' and a.gga00c = ''' + @compid + '''' end if @condition2<>'' select @sql = @sql + ' and ' + @condition2; exec sp_executesql @sql end update a set a.fact_quan = a.quan from #good_result a where a.type = 1 --modify by lyj 被除数不可以为0 update a set a.fact_quan = case when isnull(b.gfa09f, 1) = 0 then 0 else a.quan / isnull(b.gfa09f, 1) end, a.amt = 0 from #good_result a, gfm01 b with(nolock) where a.type = 2 and b.gfa00c = @compid and b.gfa01c = a.item update a set a.itemname = b.gfa03c, a.unit = b.gfa07c, a.catelogy = b.gfa05c, --产品分类 a.goodstype = b.gfa24c --产品统计分类 from #good_result a, gfm01 b with(nolock) where a.item = b.gfa01c and b.gfa00c = @compid update a set a.unit = b.gsb03c from #good_result a, gsm02 b with(nolock) where a.unit = b.gsb02c and b.gsb00c = @compid and b.gsb01c = 'K' --计算销售成本的金额 update a set a.salecostamt = a.fact_quan * a.salecostprice from #good_result a --明细资料 if @kind = 1 begin if @catelogy = '' or @catelogy = '*' begin select isnull(item,'') item, isnull(itemname,'') itemname, isnull(costprice, 0) costprice, isnull(saleprice, 0) saleprice, isnull(fact_quan,'') fact_quan, isnull(saler,'') saler, isnull(unitprice*fact_quan, 0) bzprice, saledate = isnull(convert(varchar(10), cast(saledate as datetime), 120), ''), saletime = substring(saletime, 1, 2) + ':' + substring(saletime, 3, 2), isnull(unit,'') unit, isnull(costamt,'') costamt, isnull(amt,'') amt, isnull(paymode,'') paymode, isnull(stock,'') stock, isnull(sendnum,'') sendnum, isnull(sendamt,'') sendamt, isnull(salecostamt,'') salecostamt, isnull(salecostprice,'') salecostprice, isnull(profit_1,'') profit_1, isnull(profit_2,'') profit_2 from #good_result where ((goodstype >= @fromgoodstype and goodstype <= @tomgoodstype) or @fromgoodstype = '*') end else begin select isnull(item,'') item, isnull(itemname,'') itemname, isnull(costprice, 0) costprice, isnull(saleprice, 0) saleprice, isnull(fact_quan,'') fact_quan, isnull(saler,'') saler, isnull(unitprice*fact_quan, 0) bzprice, saledate = isnull(convert(varchar(10), cast(saledate as datetime), 120), ''), saletime = substring(saletime, 1, 2) + ':' + substring(saletime, 3, 2), isnull(unit,'') unit, isnull(costamt,'') costamt, isnull(amt,'') amt, isnull(paymode,'') paymode, isnull(stock,'') stock, isnull(sendnum,'') sendnum, isnull(sendamt,'') sendamt, isnull(salecostamt,'') salecostamt, isnull(salecostprice,'') salecostprice, isnull(profit_1,'') profit_1, isnull(profit_2,'') profit_2 from #good_result where catelogy = @catelogy and ((goodstype >= @fromgoodstype and goodstype <= @tomgoodstype) or @fromgoodstype = '*') end end else if @kind = 2 -- 按产品分组合计 begin --modify by lyj 被除数不可以为0 if @catelogy = '' or @catelogy = '*' begin select isnull(item,'') item, isnull(itemname,'') itemname, costprice = (case when isnull(sum(fact_quan), 0) = 0 then 0 else isnull(sum(costamt)/sum(fact_quan),0) end), saleprice = (case when isnull(sum(fact_quan), 0) = 0 then 0 else isnull(sum(amt)/sum(fact_quan),0) end), fact_quan = isnull(sum(fact_quan), 0), isnull(unit,'') unit, isnull(sum(unitprice*fact_quan), 0) bzprice, costamt = isnull(sum(costamt), 0), amt = isnull(sum(amt), 0), isnull(stock,'') stock, sendnum = isnull(sum(sendnum), 0), sendamt = isnull(sum(sendamt), 0), salecostamt = isnull(sum(salecostamt), 0), salecostprice = (case when isnull(sum(fact_quan), 0) = 0 then 0 else isnull(sum(salecostamt)/sum(fact_quan), 0) end), profit_1 = isnull((sum(amt)-sum(salecostamt)), 0), profit_2 = isnull((sum(salecostamt)-sum(costamt)), 0) from #good_result where ((goodstype >= @fromgoodstype and goodstype <= @tomgoodstype) or @fromgoodstype = '*') group by item, itemname, unit, stock end else begin select isnull(item,'') item, isnull(itemname,'') itemname, costprice = (case when isnull(sum(fact_quan), 0) = 0 then 0 else isnull(sum(costamt)/sum(fact_quan),0) end), saleprice = (case when isnull(sum(fact_quan), 0) = 0 then 0 else isnull(sum(amt)/sum(fact_quan),0) end), isnull(sum(unitprice*fact_quan), 0) bzprice, fact_quan = isnull(sum(fact_quan), 0), isnull(unit,'') unit, costamt = isnull(sum(costamt), 0), amt = isnull(sum(amt), 0), isnull(stock,'') stock, sendnum = isnull(sum(sendnum), 0), sendamt = isnull(sum(sendamt), 0), salecostamt = isnull(sum(salecostamt), 0), salecostprice = (case when isnull(sum(fact_quan), 0) = 0 then 0 else isnull(sum(salecostamt)/sum(fact_quan), 0) end), profit_1 = isnull((sum(amt)-sum(salecostamt)), 0), profit_2 = isnull((sum(salecostamt)-sum(costamt)), 0) from #good_result where catelogy = @catelogy and ((goodstype >= @fromgoodstype and goodstype <= @tomgoodstype) or @fromgoodstype = '*') group by item, itemname, unit , stock end end drop table #good_result end GO /****** Object: StoredProcedure [dbo].[upg_stores_treatmentUseRecord] Script Date: 06/12/2017 06:20:46 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_stores_treatmentUseRecord] ( @compid varchar(10), -- 已经能支持各级别的公司 @fromdate varchar(10), -- 开始日期 @todate varchar(10) -- 结束日期 ) as begin declare @onlycash float select @onlycash = gsa03f from gsm01 with(nolock) where gsa00c = @compid and gsa01c = 'SP139' create table #tbl_result ( compid varchar(20) not null, compname varchar(40) null, projId varchar(20) not null, projname varchar(60) null, sumamt float null, sumrecord float null ) -- 购买 create table #tbl_result_buy ( compid varchar(20) not null, compname varchar(40) null, projId varchar(20) not null, projname varchar(60) null, sumamt float null, sumrecord float null ) --消耗 create table #tbl_result_consume ( compid varchar(20) not null, compname varchar(40) null, projId varchar(20) not null, projname varchar(60) null, sumamt float null, sumrecord float null ) insert #tbl_result(compid, compname, projId, projname) select gch00c as compId, '', gch04c as projId, isnull((select top 1 gda03c from gdm01 with(nolock) where gda00c = gch00c and gda01c = gch04c), '') as projname from gcm08 a with (nolock), gam26 z with(nolock) where a.gch00c = z.gaz02c and z.gaz01c = @compid and a.gch02d between @fromdate and @todate group by gch00c, gch04c order by gch00c, gch04c if @onlycash = 2 -- 所有支付方式 begin insert #tbl_result_buy(compid, compname, projId, projname, sumamt, sumrecord) select gch00c as compId, '', gch04c as projId, isnull((select top 1 gda03c from gdm01 with(nolock) where gda00c = gch00c and gda01c = gch04c), '') as projname, sumamt = sum(gch07f), sumrecord = 0 from gcm08 a with (nolock), gam26 z with(nolock) where a.gch00c = z.gaz02c and z.gaz01c = @compid and a.gch02d between @fromdate and @todate and (gch10c = 'gn' or gch10c = 'gz' or gch10c = 'gx') and isnull(gch05f, 0) + isnull(gch06f, 0) > 0 group by gch00c, gch04c order by gch00c, gch04c end else -- 只有现金类支付方式 begin insert #tbl_result_buy(compid, compname, projId, projname, sumamt, sumrecord) select gch00c as compId, '', gch04c as projId, isnull((select top 1 gda03c from gdm01 with(nolock) where gda00c = gch00c and gda01c = gch04c), '') as projname, sumamt = sum(case gch17c when '1' then gch18f when '6' then gch18f else 0 end + case gch19c when '1' then gch20f when '6' then gch20f else 0 end + case gch21c when '1' then gch22f when '6' then gch22f else 0 end + case gch27c when '1' then gch28f when '6' then gch28f else 0 end), sumrecord = 0 from gcm08 a with (nolock), gam26 z with(nolock) where a.gch00c = z.gaz02c and z.gaz01c = @compid and a.gch02d between @fromdate and @todate and (gch10c = 'gn' or gch10c = 'gz' or gch10c = 'gx') and isnull(gch05f, 0) + isnull(gch06f, 0) > 0 group by gch00c, gch04c order by gch00c, gch04c end insert #tbl_result_consume(compid, compname, projId, projname, sumamt, sumrecord) select gch00c as compId, '', gch04c as projId, isnull((select top 1 gda03c from gdm01 with(nolock) where gda00c = gch00c and gda01c = gch04c), '') as projname, sumamt = 0, sum(gch08f) as sumrecord from gcm08 a with (nolock), gam26 z with(nolock) where a.gch00c = z.gaz02c and z.gaz01c = @compid and a.gch02d between @fromdate and @todate and (gch10c = 'gx') group by gch00c, gch04c order by gch00c, gch04c update a set a.sumamt = b.sumamt from #tbl_result a, #tbl_result_buy b where a.compid = b.compid and a.projId = b.projId update a set a.sumrecord = c.sumrecord from #tbl_result a, #tbl_result_consume c where a.compid = c.compid and a.projId = c.projId drop table #tbl_result_buy drop table #tbl_result_consume declare @maxAQ int select @maxAQ = max(gsb02c) from gsm02 with(nolock) where gsb00c = @compid and gsb01c = 'AQ' set @maxAQ = isnull(@maxAQ, 4) -- 处理非叶子节点的数据, START declare @each_parent varchar(20) begin create table #used_compid ( compid varchar(20) not null, isleaf int null ) insert #used_compid(compid, isleaf) select distinct gaz02c, 0 from gam26 with(nolock) where gaz01c = @compid update a set a.isleaf = 1 from #used_compid a, b_perf b with(nolock) where a.compid = b.bran_id and b.lvl = @maxAQ create table #notleaf_sons ( parantid varchar(20) not null, sonid varchar(20) not null ) insert #notleaf_sons(parantid, sonid) select gaz01c, gaz02c from gam26 z with(nolock), #used_compid b, #used_compid c where z.gaz01c = b.compid and b.isleaf = 0 and gaz01c <> gaz02c and z.gaz02c = c.compid and c.isleaf = 1 declare cur_each_parent_compid cursor for select compid from #used_compid where isleaf = 0 open cur_each_parent_compid fetch cur_each_parent_compid into @each_parent while @@fetch_status = 0 begin delete #tbl_result where compid = @each_parent insert #tbl_result(compid, compname, projId, projname, sumamt, sumrecord) select @each_parent, '', projId, projname, sum(sumamt), sum(sumrecord) from #tbl_result a, #notleaf_sons b where a.compid = b.sonid and b.parantid = @each_parent group by compname, projId, projname fetch cur_each_parent_compid into @each_parent end close cur_each_parent_compid deallocate cur_each_parent_compid end --处理非叶子节点的数据, end update a set a.compname = b.gae03c from #tbl_result a, gam05 b with(nolock) where a.compid = b.gae01c select compId = isnull(compid,''), isnull(compname,'') compname, isnull(projId,'') projId, isnull(projname,'') projname, isnull(sumamt,0) sumamt, isnull(sumrecord,0) sumrecord from #tbl_result order by compid drop table #tbl_result end GO /****** Object: StoredProcedure [dbo].[upg_day_account_depart] Script Date: 06/12/2017 06:19:56 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_day_account_depart] ( @incompid varchar(10), -- 公司编号 @type varchar(10), -- 类别 @fromdate varchar(8), -- date from @todate varchar(8), -- date to @inc_expense int, -- 是否包括费用 @staff varchar(20), -- 收银人员 @fromdept varchar(10), -- add by lyj 2007-09-06 @todept varchar(10), @billtype varchar(2) -- add by tanxulin 2010/02/24 ,查询条件 - 单据类别 ) as--WUXM, 2005/07/30 begin -- 因java请求无法识别WWQ = #,WWR = $, WWE = & exec upg_paymode_to_java @type,@type output declare @gsn02c varchar(20) select @gsn02c = gsn02c from gsm14 where gsn01c = @type declare @accttype int -- 支付方式对应的账户 if (isnull(@gsn02c, '') = '' or @gsn02c = '999') select @accttype = -1 else select @accttype = convert(int, @gsn02c) declare @compid varchar(10) select @compid = @incompid if @compid = '001' select @compid = '%' declare @procacct int -- 疗程账户 if @staff = '*' or @staff = '' begin select @staff = '%' end create table #tbl_gnm01_gnm02 ( gna00c varchar(10) not null, --公司编号 gna01c varchar(20) not null, --销售单号 gna02d varchar(8) null, --销售日期 gna12t varchar(6) null, --销售时间 gna20c varchar(10) null, --负责业务 gna27f float null, --第一分享比率 gna28c varchar(10) null, --第二卖卡人 gna29f float null, --分享比率2 gna30c varchar(10) null, --第三卖卡人 gna31f float null, --分享比率3 gna33f float null, --分享比率4 gna35f float null, --分享比率5 gna37f float null, --分享比率6 gna39f float null, --分享比率7 gna41f float null, --分享比率8 gna43f float null, --分享比率9 gna45f float null, --分享比率10 gna80d varchar(8) null, --帐务日期, added by MZH, 2005/08/16 gna81c varchar(10) null, --部门编号 gna82c varchar(10) null, --部门编号2 gna83c varchar(10) null, --部门编号3 gnb01c varchar(20) not null, --销售单号 gnb03c varchar(20) not null, --会员卡号 gnb08f float null, --卡销售金额(包括疗程套餐的销售金额) gnb11f float null, --储值金额 , added by MZH, 2005/10/28 gnb13i int null, --账户 gna100c varchar(10) null, --部门编号4 Add by maql 20100705 gna101c varchar(10) null, --部门编号5 Add by maql 20100705 gna102c varchar(10) null, --部门编号6 Add by maql 20100705 gna103c varchar(10) null, --部门编号7 Add by maql 20100705 gna104c varchar(10) null, --部门编号8 Add by maql 20100705 gna105c varchar(10) null, --部门编号9 Add by maql 20100705 gna106c varchar(10) null --部门编号10 Add by maql 20100705 ) create clustered index idx_gnm01_gnm02_01 on #tbl_gnm01_gnm02(gna00c, gna01c) create nonclustered index idx_gnm01_gnm02_02 on #tbl_gnm01_gnm02(gna81c) create nonclustered index idx_gnm01_gnm02_03 on #tbl_gnm01_gnm02(gna82c) create nonclustered index idx_gnm01_gnm02_04 on #tbl_gnm01_gnm02(gna83c) create nonclustered index idx_gnm01_gnm02_05 on #tbl_gnm01_gnm02(gna100c) create nonclustered index idx_gnm01_gnm02_06 on #tbl_gnm01_gnm02(gna101c) create nonclustered index idx_gnm01_gnm02_07 on #tbl_gnm01_gnm02(gna102c) create nonclustered index idx_gnm01_gnm02_08 on #tbl_gnm01_gnm02(gna103c) create nonclustered index idx_gnm01_gnm02_09 on #tbl_gnm01_gnm02(gna104c) create nonclustered index idx_gnm01_gnm02_10 on #tbl_gnm01_gnm02(gna105c) create nonclustered index idx_gnm01_gnm02_11 on #tbl_gnm01_gnm02(gna106c) insert #tbl_gnm01_gnm02(gna00c, gna01c, gna02d, gna12t, gna20c, gna27f, gna28c, gna29f, gna30c, gna31f, gna33f, gna35f, gna37f, gna39f, gna41f, gna43f, gna45f, gna80d, gna81c, gna82c, gna83c, gnb01c, gnb03c, gnb08f, gnb11f, gnb13i, gna100c, gna101c, gna102c, gna103c, gna104c, gna105c, gna106c) select gna00c, gna01c, gna02d, gna12t, gna20c, gna27f, gna28c, gna29f, gna30c, gna31f, gna33f, gna35f, gna37f, gna39f, gna41f, gna43f, gna45f, gna80d, gna81c, gna82c, gna83c, gnb01c, gnb03c, gnb08f, gnb11f, gnb13i, gna100c, gna101c, gna102c, gna103c, gna104c, gna105c, gna106c from gnm02 b with (nolock), gnm01 a with (nolock), gam26 z where a.gna00c = b.gnb00c and a.gna01c = b.gnb01c and a.gna80d >= @fromdate and a.gna80d <= @todate and a.gna00c = z.gaz02c and z.gaz01c = @incompid and a.gna93c like @staff create table #tbl_gnm01_gnm03( gna00c varchar(10) not null, --公司编号 gna01c varchar(20) not null, --销售单号 gna02d varchar(8) null, --销售日期 gna06c varchar(30) null, --客户名称 gna12t varchar(6) null, --销售时间 gna20c varchar(10) null, --负责业务 gna27f float null, --第一分享比率 gna28c varchar(10) null, --第二卖卡人 gna29f float null, --分享比率2 gna30c varchar(10) null, --第三卖卡人 gna31f float null, --分享比率3 gna33f float null, --分享比率4 gna35f float null, --分享比率5 gna37f float null, --分享比率6 gna39f float null, --分享比率7 gna41f float null, --分享比率8 gna43f float null, --分享比率9 gna45f float null, --分享比率10 gna80d varchar(8) null, --帐务日期, added by MZH, 2005/08/16 gna81c varchar(10) null, --部门编号 gna82c varchar(10) null, --部门编号2 gna83c varchar(10) null, --部门编号3 gnc05f float null, --疗程金额 gnc02c varchar(20) not null, --项目编号 gna100c varchar(10) null, --部门编号4 Add by maql 20100705 gna101c varchar(10) null, --部门编号5 Add by maql 20100705 gna102c varchar(10) null, --部门编号6 Add by maql 20100705 gna103c varchar(10) null, --部门编号7 Add by maql 20100705 gna104c varchar(10) null, --部门编号8 Add by maql 20100705 gna105c varchar(10) null, --部门编号9 Add by maql 20100705 gna106c varchar(10) null --部门编号10 Add by maql 20100705 ) create clustered index idx_gnm01_gnm03_01 on #tbl_gnm01_gnm03(gna00c, gna01c) create nonclustered index idx_gnm01_gnm03_02 on #tbl_gnm01_gnm03(gna81c) create nonclustered index idx_gnm01_gnm03_03 on #tbl_gnm01_gnm03(gna82c) create nonclustered index idx_gnm01_gnm03_04 on #tbl_gnm01_gnm03(gna83c) create nonclustered index idx_gnm01_gnm03_05 on #tbl_gnm01_gnm03(gna100c) create nonclustered index idx_gnm01_gnm03_06 on #tbl_gnm01_gnm03(gna101c) create nonclustered index idx_gnm01_gnm03_07 on #tbl_gnm01_gnm03(gna102c) create nonclustered index idx_gnm01_gnm03_08 on #tbl_gnm01_gnm03(gna103c) create nonclustered index idx_gnm01_gnm03_09 on #tbl_gnm01_gnm03(gna104c) create nonclustered index idx_gnm01_gnm03_10 on #tbl_gnm01_gnm03(gna105c) create nonclustered index idx_gnm01_gnm03_11 on #tbl_gnm01_gnm03(gna106c) insert #tbl_gnm01_gnm03(gna00c, gna01c, gna02d, gna06c, gna12t, gna20c, gna27f, gna28c, gna29f, gna30c, gna31f, gna33f, gna35f, gna37f, gna39f, gna41f, gna43f, gna45f, gna80d, gna81c, gna82c, gna83c, gnc05f, gnc02c, gna100c, gna101c, gna102c, gna103c, gna104c, gna105c, gna106c) select gna00c, gna01c, gna02d, gna06c, gna12t, gna20c, gna27f, gna28c, gna29f, gna30c, gna31f, gna33f, gna35f, gna37f, gna39f, gna41f, gna43f, gna45f, gna80d, gna81c, gna82c, gna83c, gnc05f, gnc02c, gna100c, gna101c, gna102c, gna103c, gna104c, gna105c, gna106c from gnm01 a with (nolock), gnm03 c with (nolock), gam26 z where a.gna00c = c.gnc00c and a.gna01c = c.gnc01c and a.gna80d >= @fromdate and a.gna80d <= @todate and a.gna00c = z.gaz02c and z.gaz01c = @incompid and a.gna93c like @staff create table #tbl_gcm10_buf( gcl00c varchar(10) not null, --公司编号 gcl01c varchar(20) not null, --异动单号 gcl02d varchar(8) not null, --异动日期 gcl03c varchar(20) null, --会员卡号 gcl04c varchar(5) null, --帐号类别(1.电子钱包 2.储值帐户) gcl06i int null, --异动类别( 0充值, 1取款 5欠款 6还款) gcl09c varchar(20) null, --销售人员 gcl16f float null, --第一销售分享比率 gcl17c varchar(20) null, --第二销售 gcl18f float null, --分享比率2 gcl19c varchar(20) null, --第三负责人 gcl20f float null, --分享比率3 gcl23f float null, --分享比率4 gcl25f float null, --分享比率5 gcl27f float null, --分享比率6 gcl29f float null, --分享比率7 gcl31f float null, --分享比率8 gcl33f float null, --分享比率9 gcl35f float null, --分享比率10 gcl56t varchar(6) null, --消费时间 added by MZH, 2005/08/16 gcl64f float null, --总储值金额(包括疗程) gcl65f float null, --总储值金额(除疗程) gcl66f float null, --总疗程金额 gcl80d varchar(8) null, --帐务日期 added by MZH, 2005/08/16 gcl81c varchar(10) null, --部门编号 gcl82c varchar(10) null, --部门编号2 gcl83c varchar(10) null, --部门编号3 gcl100c varchar(10) null, --部门编号4 gcl101c varchar(10) null, --部门编号5 gcl102c varchar(10) null, --部门编号6 gcl103c varchar(10) null, --部门编号7 gcl104c varchar(10) null, --部门编号8 gcl105c varchar(10) null, --部门编号9 gcl106c varchar(10) null, --部门编号10 ) create clustered index idx_tbl_gcm10_buf_01 on #tbl_gcm10_buf(gcl00c, gcl01c) create nonclustered index idx_tbl_gcm10_buf_02 on #tbl_gcm10_buf(gcl81c) create nonclustered index idx_tbl_gcm10_buf_03 on #tbl_gcm10_buf(gcl82c) create nonclustered index idx_tbl_gcm10_buf_04 on #tbl_gcm10_buf(gcl83c) create nonclustered index idx_tbl_gcm10_buf_05 on #tbl_gcm10_buf(gcl100c) create nonclustered index idx_tbl_gcm10_buf_06 on #tbl_gcm10_buf(gcl101c) create nonclustered index idx_tbl_gcm10_buf_07 on #tbl_gcm10_buf(gcl102c) create nonclustered index idx_tbl_gcm10_buf_08 on #tbl_gcm10_buf(gcl103c) create nonclustered index idx_tbl_gcm10_buf_09 on #tbl_gcm10_buf(gcl104c) create nonclustered index idx_tbl_gcm10_buf_10 on #tbl_gcm10_buf(gcl105c) create nonclustered index idx_tbl_gcm10_buf_11 on #tbl_gcm10_buf(gcl106c) insert #tbl_gcm10_buf(gcl00c, gcl01c, gcl02d, gcl03c, gcl04c, gcl06i, gcl09c, gcl16f, gcl17c, gcl18f, gcl19c, gcl20f, gcl23f, gcl25f, gcl27f, gcl29f, gcl31f, gcl33f, gcl35f, gcl56t, gcl64f, gcl65f, gcl66f, gcl80d, gcl81c, gcl82c, gcl83c, gcl100c, gcl101c, gcl102c, gcl103c, gcl104c, gcl105c, gcl106c) select gcl00c, gcl01c, gcl02d, gcl03c, gcl04c, gcl06i, gcl09c, gcl16f, gcl17c, gcl18f, gcl19c, gcl20f, gcl23f, gcl25f, gcl27f, gcl29f, gcl31f, gcl33f, gcl35f, gcl56t, gcl64f, gcl65f, gcl66f, gcl80d, gcl81c, gcl82c, gcl83c, gcl100c, gcl101c, gcl102c, gcl103c, gcl104c, gcl105c, gcl106c from gcm10 a with (nolock), gam26 z where gcl00c = z.gaz02c and z.gaz01c = @incompid and gcl93c like @staff and gcl80d >= @fromdate and gcl80d <= @todate create table #account ( id int identity not null, note varchar(20) null, --note id class varchar(5) null, --note class cname varchar(40) null, --class name date varchar(8) null, --单据 date time varchar(6) null, --时间 brief varchar(600) null, --摘要 get float null default 0, --借方金额 out float null default 0, --贷方金额 balance float null default 0, --余额 staff varchar(20) null, -- 收银人员 billdate varchar(8) null, -- 单据日期 primary key (id) ) --因为需要统计到部门,所以只能一个个销售人员来统计,但这样可能三个员工同一部门的情况 --会出现奇怪的分割情况,所以需要缓冲表来按部门分组 create table #account_dept ( id int identity not null, note varchar(20) null, --note id class varchar(5) null, --note class cname varchar(40) null, --class name date varchar(8) null, --单据 date time varchar(6) null, --时间 brief varchar(600) null, --摘要 get float null default 0, --借方金额 out float null default 0, --贷方金额 balance float null default 0, --余额 staff varchar(20) null, -- 收银人员 billdate varchar(8) null, -- 单据日期 ) declare @PK_Sql nvarchar(max) set @PK_Sql = ' alter table #account_dept add constraint PK_#account_dept_' + convert(varchar(10), cast(floor(rand() * 1000000) as int)) + ' primary key nonclustered(id)' exec sp_executesql @PK_Sql create clustered index idx_acct_dept_01 on #account_dept(note, class, brief, date, time, billdate) --处理一般的支付方式,如现金,银行卡,内调,抵用券等 --这些日记账可以容易的从gsm03取得 --无对应账户的支付 --modify by lyj 对于按部门统计下面的从gsm03 select已经没有任何意思了,所以去掉, --收益 begin --有对应账户的支付 -- 会员卡销售的金额都作为充值金额 -- 卡本来的储值金额 create table #gsm03_temp ( gsc00c varchar(10) null, --公司 gsc01c varchar(20) null, --单据编号 gsc02c varchar(5) null, --单据类别 gsc04c varchar(5) null, --支付方式 gsc05f float null, --支付金额 gsc10d varchar(8) null, --单据帐务日期 gsc11c varchar(20) null, --收银人 ) create nonclustered index #gsm03_temp_01 on #gsm03_temp(gsc02c) create nonclustered index #gsm03_temp_02 on #gsm03_temp(gsc01c) create nonclustered index #gsm03_temp_03 on #gsm03_temp(gsc04c) -------把gsm03里面符合条件的先insert到临时表中 insert #gsm03_temp(gsc00c, gsc01c, gsc02c, gsc04c, gsc05f, gsc10d, gsc11c) select gsc00c, gsc01c, gsc02c, gsc04c, gsc05f, gsc10d, gsc11c from gsm03 a, gam26 z where gsc00c = z.gaz02c and z.gaz01c = @incompid and gsc10d >= @fromdate and gsc10d <= @todate and gsc11c like @staff select @procacct = '4' if (isnull(@accttype, -1) = -1) begin --第一销售的部门 insert #account_dept(note, class, get, brief, date, time, billdate) select gnb01c, 'gn', gnb08f*isnull(gna27f, 0), gnb03c + '卡销售', gna80d, gna12t, gna02d from #tbl_gnm01_gnm02 a, #gsm03_temp c where a.gna00c = c.gsc00c and a.gna01c = c.gsc01c and c.gsc02c = 'gn' and c.gsc04c = @type and (a.gna81c between @fromdept and @todept or @fromdept = '*') --add by lyj 2007-09-06 --第二销售的部门 insert #account_dept(note, class, get, brief, date, time, billdate) select gnb01c, 'gn', gnb08f*isnull(gna29f, 0), gnb03c + '卡销售', gna80d, gna12t, gna02d from #tbl_gnm01_gnm02 a, #gsm03_temp c where a.gna00c = c.gsc00c and a.gna01c = c.gsc01c and c.gsc02c = 'gn' and c.gsc04c = @type and (a.gna82c between @fromdept and @todept or @fromdept = '*') --add by lyj 2007-09-06 --第三销售的部门 insert #account_dept(note, class, get, brief, date, time, billdate) select gnb01c, 'gn', gnb08f*isnull(gna31f, 0), gnb03c + '卡销售', gna80d, gna12t, gna02d from #tbl_gnm01_gnm02 a, #gsm03_temp c where a.gna00c = c.gsc00c and a.gna01c = c.gsc01c and c.gsc02c = 'gn' and c.gsc04c = @type and (a.gna83c between @fromdept and @todept or @fromdept = '*') --add by lyj 2007-09-06 --第4销售的部门 insert #account_dept(note, class, get, brief, date, time, billdate) select gnb01c, 'gn', gnb08f*isnull(gna33f, 0), gnb03c + '卡销售', gna80d, gna12t, gna02d from #tbl_gnm01_gnm02 a, #gsm03_temp c where a.gna00c = c.gsc00c and a.gna01c = c.gsc01c and c.gsc02c = 'gn' and c.gsc04c = @type and (a.gna100c between @fromdept and @todept or @fromdept = '*') --Add by maql 20100707 --第5销售的部门 insert #account_dept(note, class, get, brief, date, time, billdate) select gnb01c, 'gn', gnb08f*isnull(gna35f, 0), gnb03c + '卡销售', gna80d, gna12t, gna02d from #tbl_gnm01_gnm02 a, #gsm03_temp c where a.gna00c = c.gsc00c and a.gna01c = c.gsc01c and c.gsc02c = 'gn' and c.gsc04c = @type and (a.gna101c between @fromdept and @todept or @fromdept = '*') --Add by maql 20100707 --第6销售的部门 insert #account_dept(note, class, get, brief, date, time, billdate) select gnb01c, 'gn', gnb08f*isnull(gna37f, 0), gnb03c + '卡销售', gna80d, gna12t, gna02d from #tbl_gnm01_gnm02 a, #gsm03_temp c where a.gna00c = c.gsc00c and a.gna01c = c.gsc01c and c.gsc02c = 'gn' and c.gsc04c = @type and (a.gna102c between @fromdept and @todept or @fromdept = '*') --Add by maql 20100707 --第7销售的部门 insert #account_dept(note, class, get, brief, date, time, billdate) select gnb01c, 'gn', gnb08f*isnull(gna39f, 0), gnb03c + '卡销售', gna80d, gna12t, gna02d from #tbl_gnm01_gnm02 a, #gsm03_temp c where a.gna00c = c.gsc00c and a.gna01c = c.gsc01c and c.gsc02c = 'gn' and c.gsc04c = @type and (a.gna103c between @fromdept and @todept or @fromdept = '*') --Add by maql 20100707 --第8销售的部门 insert #account_dept(note, class, get, brief, date, time, billdate) select gnb01c, 'gn', gnb08f*isnull(gna41f, 0), gnb03c + '卡销售', gna80d, gna12t, gna02d from #tbl_gnm01_gnm02 a, #gsm03_temp c where a.gna00c = c.gsc00c and a.gna01c = c.gsc01c and c.gsc02c = 'gn' and c.gsc04c = @type and (a.gna104c between @fromdept and @todept or @fromdept = '*') --Add by maql 20100707 --第9销售的部门 insert #account_dept(note, class, get, brief, date, time, billdate) select gnb01c, 'gn', gnb08f*isnull(gna43f, 0), gnb03c + '卡销售', gna80d, gna12t, gna02d from #tbl_gnm01_gnm02 a, #gsm03_temp c where a.gna00c = c.gsc00c and a.gna01c = c.gsc01c and c.gsc02c = 'gn' and c.gsc04c = @type and (a.gna105c between @fromdept and @todept or @fromdept = '*') --Add by maql 20100707 --第10销售的部门 insert #account_dept(note, class, get, brief, date, time, billdate) select gnb01c, 'gn', gnb08f*isnull(gna45f, 0), gnb03c + '卡销售', gna80d, gna12t, gna02d from #tbl_gnm01_gnm02 a, #gsm03_temp c where a.gna00c = c.gsc00c and a.gna01c = c.gsc01c and c.gsc02c = 'gn' and c.gsc04c = @type and (a.gna106c between @fromdept and @todept or @fromdept = '*') --Add by maql 20100707 --合并起来 insert #account(note, class, get, brief, date, time, billdate) select note, class, sum(get), brief, date, time, billdate from #account_dept group by note, class, brief, date, time, billdate end else begin insert #account_dept(note, class, out, brief, date, time, billdate) select gnb01c, 'gn', gnb11f*isnull(gna27f, 0), gnb03c + '卡销售', gna80d, gna12t, gna02d from #tbl_gnm01_gnm02 a where a.gnb13i = @accttype and (a.gna81c between @fromdept and @todept or @fromdept = '*') --add by lyj 2007-09-06 insert #account_dept(note, class, out, brief, date, time, billdate) select gnb01c, 'gn', gnb11f*isnull(gna29f, 0), gnb03c + '卡销售', gna80d, gna12t, gna02d from #tbl_gnm01_gnm02 a where a.gnb13i = @accttype and (a.gna82c between @fromdept and @todept or @fromdept = '*') --add by lyj 2007-09-06 insert #account_dept(note, class, out, brief, date, time, billdate) select gnb01c, 'gn', gnb11f*isnull(gna31f, 0), gnb03c + '卡销售', gna80d, gna12t, gna02d from #tbl_gnm01_gnm02 a where a.gnb13i = @accttype and (a.gna83c between @fromdept and @todept or @fromdept = '*') --add by lyj 2007-09-06 insert #account_dept(note, class, out, brief, date, time, billdate) select gnb01c, 'gn', gnb11f*isnull(gna33f, 0), gnb03c + '卡销售', gna80d, gna12t, gna02d from #tbl_gnm01_gnm02 a where a.gnb13i = @accttype and (a.gna100c between @fromdept and @todept or @fromdept = '*') --Add by maql 20100707 insert #account_dept(note, class, out, brief, date, time, billdate) select gnb01c, 'gn', gnb11f*isnull(gna35f, 0), gnb03c + '卡销售', gna80d, gna12t, gna02d from #tbl_gnm01_gnm02 a where a.gnb13i = @accttype and (a.gna101c between @fromdept and @todept or @fromdept = '*') --Add by maql 20100707 insert #account_dept(note, class, out, brief, date, time, billdate) select gnb01c, 'gn', gnb11f*isnull(gna37f, 0), gnb03c + '卡销售', gna80d, gna12t, gna02d from #tbl_gnm01_gnm02 a where a.gnb13i = @accttype and (a.gna102c between @fromdept and @todept or @fromdept = '*') --Add by maql 20100707 insert #account_dept(note, class, out, brief, date, time, billdate) select gnb01c, 'gn', gnb11f*isnull(gna39f, 0), gnb03c + '卡销售', gna80d, gna12t, gna02d from #tbl_gnm01_gnm02 a where a.gnb13i = @accttype and (a.gna103c between @fromdept and @todept or @fromdept = '*') --Add by maql 20100707 insert #account_dept(note, class, out, brief, date, time, billdate) select gnb01c, 'gn', gnb11f*isnull(gna41f, 0), gnb03c + '卡销售', gna80d, gna12t, gna02d from #tbl_gnm01_gnm02 a where a.gnb13i = @accttype and (a.gna104c between @fromdept and @todept or @fromdept = '*') --Add by maql 20100707 insert #account_dept(note, class, out, brief, date, time, billdate) select gnb01c, 'gn', gnb11f*isnull(gna43f, 0), gnb03c + '卡销售', gna80d, gna12t, gna02d from #tbl_gnm01_gnm02 a where a.gnb13i = @accttype and (a.gna105c between @fromdept and @todept or @fromdept = '*') --Add by maql 20100707 insert #account_dept(note, class, out, brief, date, time, billdate) select gnb01c, 'gn', gnb11f*isnull(gna45f, 0), gnb03c + '卡销售', gna80d, gna12t, gna02d from #tbl_gnm01_gnm02 a where a.gnb13i = @accttype and (a.gna106c between @fromdept and @todept or @fromdept = '*') --Add by maql 20100707 insert #account(note, class, out, brief, date, time, billdate) select note, class, sum(out), brief, date, time, billdate from #account_dept group by note, class, brief, date, time, billdate end --卡销售的时候可能有疗程的金额 if (@procacct = @accttype) begin delete #account_dept insert #account_dept(note, class, out, brief, date, time, billdate) select gna01c, 'gn', gnc05f*isnull(gna27f, 0), gna06c + '购买疗程' + gnc02c, gna80d, gna12t, gna02d from #tbl_gnm01_gnm03 a where (a.gna81c between @fromdept and @todept or @fromdept = '*') --add by lyj 2007-09-06 insert #account_dept(note, class, out, brief, date, time, billdate) select gna01c, 'gn', gnc05f*isnull(gna29f, 0), gna06c + '购买疗程' + gnc02c, gna80d, gna12t, gna02d from #tbl_gnm01_gnm03 a where (a.gna82c between @fromdept and @todept or @fromdept = '*') --add by lyj 2007-09-06 insert #account_dept(note, class, out, brief, date, time, billdate) select gna01c, 'gn', gnc05f*isnull(gna31f, 0), gna06c + '购买疗程' + gnc02c, gna80d, gna12t, gna02d from #tbl_gnm01_gnm03 a where (a.gna83c between @fromdept and @todept or @fromdept = '*') --add by lyj 2007-09-06 insert #account_dept(note, class, out, brief, date, time, billdate) select gna01c, 'gn', gnc05f*isnull(gna33f, 0), gna06c + '购买疗程' + gnc02c, gna80d, gna12t, gna02d from #tbl_gnm01_gnm03 a where (a.gna100c between @fromdept and @todept or @fromdept = '*') --add by maql 20100707 insert #account_dept(note, class, out, brief, date, time, billdate) select gna01c, 'gn', gnc05f*isnull(gna35f, 0), gna06c + '购买疗程' + gnc02c, gna80d, gna12t, gna02d from #tbl_gnm01_gnm03 a where (a.gna101c between @fromdept and @todept or @fromdept = '*') --add by maql 20100707 insert #account_dept(note, class, out, brief, date, time, billdate) select gna01c, 'gn', gnc05f*isnull(gna37f, 0), gna06c + '购买疗程' + gnc02c, gna80d, gna12t, gna02d from #tbl_gnm01_gnm03 a where (a.gna102c between @fromdept and @todept or @fromdept = '*') --add by maql 20100707 insert #account_dept(note, class, out, brief, date, time, billdate) select gna01c, 'gn', gnc05f*isnull(gna39f, 0), gna06c + '购买疗程' + gnc02c, gna80d, gna12t, gna02d from #tbl_gnm01_gnm03 a where (a.gna103c between @fromdept and @todept or @fromdept = '*') --add by maql 20100707 insert #account_dept(note, class, out, brief, date, time, billdate) select gna01c, 'gn', gnc05f*isnull(gna41f, 0), gna06c + '购买疗程' + gnc02c, gna80d, gna12t, gna02d from #tbl_gnm01_gnm03 a where (a.gna104c between @fromdept and @todept or @fromdept = '*') --add by maql 20100707 insert #account_dept(note, class, out, brief, date, time, billdate) select gna01c, 'gn', gnc05f*isnull(gna43f, 0), gna06c + '购买疗程' + gnc02c, gna80d, gna12t, gna02d from #tbl_gnm01_gnm03 a where (a.gna105c between @fromdept and @todept or @fromdept = '*') --add by maql 20100707 insert #account_dept(note, class, out, brief, date, time, billdate) select gna01c, 'gn', gnc05f*isnull(gna45f, 0), gna06c + '购买疗程' + gnc02c, gna80d, gna12t, gna02d from #tbl_gnm01_gnm03 a where (a.gna106c between @fromdept and @todept or @fromdept = '*') --add by maql 20100707 insert #account(note, class, out, brief, date, time, billdate) select note, class, sum(out), brief, date, time, billdate from #account_dept group by note, class, brief, date, time, billdate end --收银的时候从账户中用掉的 declare @SP031 float --是否为多账单统一结帐 select @SP031 = cast(gsa03f as varchar(1)) from gsm01 where gsa00c = @incompid and gsa01c = 'SP031' --ggm02 --modify by lyj 因为有可能是一个单子即买项目又买产品所以data不可以是 gsc05f 而是ggb11f --而且要多挂上ggb27c = gsc04c 以部门统计必须是项目支付 if (@SP031 = '1') begin insert #account(note, class, get, brief, date, time, billdate) select ggb01c, 'gx', sum(isnull(ggb11f, 0)), '项目支付明细' + gga09c, gga80d, gga04t, gga02d from ggm01 a with (nolock), ggm02 b with (nolock), gam26 z where a.gga00c = b.ggb00c and a.gga01c = b.ggb01c and a.gga00c = z.gaz02c and z.gaz01c = @incompid and isnull(ggb27c, '') = @type and gga80d >= @fromdate and gga80d <= @todate and (ggb81c between @fromdept and @todept or @fromdept = '*') --add by lyj 2007-09-06 group by ggb01c, ggb81c, gga09c, gga80d, gga04t, gga02d insert #account(note, class, get, brief, date, time, billdate) select ggc01c, 'gx', sum(isnull(ggc11f, 0)), '产品支付明细' + gga09c, gga80d, gga04t, gga02d from ggm01 a with (nolock), ggm03 b with (nolock), gam26 z where a.gga00c = b.ggc00c and a.gga01c = b.ggc01c and a.gga00c = z.gaz02c and z.gaz01c = @incompid and isnull(ggc27c, '') = @type and gga80d >= @fromdate and gga80d <= @todate and (ggc81c between @fromdept and @todept or @fromdept = '*') --add by lyj 2007-09-06 group by ggc01c, ggc81c, gga09c, gga80d, gga04t, gga02d end ----------end------------------------------------------------------------ --会员账户管理中的账户的增加 --只是支付方式的时候 if (isnull(@accttype, -1) = -1) begin delete #account_dept --第一销售部门的 insert #account_dept(note, class, get, brief, date, time, billdate) select gcl01c, 'gz', (isnull(gsc05f, 0)*isnull(gcl16f, 0)), gcl03c + '-' + gcl04c + '充值', gcl80d, gcl56t, gcl02d from #tbl_gcm10_buf a, #gsm03_temp b where a.gcl00c = gsc00c and a.gcl01c = gsc01c and b.gsc02c = 'gz' and b.gsc04c = @type and (a.gcl06i = 0 or a.gcl06i = 6) and (gcl81c between @fromdept and @todept or @fromdept = '*') --add by lyj 2007-09-06 --第二销售部门的 insert #account_dept(note, class, get, brief, date, time, billdate) select gcl01c, 'gz', (isnull(gsc05f, 0)*isnull(gcl18f, 0)), gcl03c + '-' + gcl04c + '充值', gcl80d, gcl56t, gcl02d from #tbl_gcm10_buf a, #gsm03_temp b where a.gcl00c = gsc00c and a.gcl01c = gsc01c and b.gsc02c = 'gz' and b.gsc04c = @type and (a.gcl06i = 0 or a.gcl06i = 6) and (gcl82c between @fromdept and @todept or @fromdept = '*') --add by lyj 2007-09-06 --第三销售部门的 insert #account_dept(note, class, get, brief, date, time, billdate) select gcl01c, 'gz', (isnull(gsc05f, 0)*isnull(gcl20f, 0)), gcl03c + '-' + gcl04c + '充值', gcl80d, gcl56t, gcl02d from #tbl_gcm10_buf a, #gsm03_temp b where a.gcl00c = gsc00c and a.gcl01c = gsc01c and b.gsc02c = 'gz' and b.gsc04c = @type and (a.gcl06i = 0 or a.gcl06i = 6) and (gcl83c between @fromdept and @todept or @fromdept = '*') --add by lyj 2007-09-06 --第4销售部门的 insert #account_dept(note, class, get, brief, date, time, billdate) select gcl01c, 'gz', (isnull(gsc05f, 0)*isnull(gcl23f, 0)), gcl03c + '-' + gcl04c + '充值', gcl80d, gcl56t, gcl02d from #tbl_gcm10_buf a, #gsm03_temp b where a.gcl00c = gsc00c and a.gcl01c = gsc01c and b.gsc02c = 'gz' and b.gsc04c = @type and (a.gcl06i = 0 or a.gcl06i = 6) and (gcl100c between @fromdept and @todept or @fromdept = '*') --add by maql 20100707 --第5销售部门的 insert #account_dept(note, class, get, brief, date, time, billdate) select gcl01c, 'gz', (isnull(gsc05f, 0)*isnull(gcl25f, 0)), gcl03c + '-' + gcl04c + '充值', gcl80d, gcl56t, gcl02d from #tbl_gcm10_buf a, #gsm03_temp b where a.gcl00c = gsc00c and a.gcl01c = gsc01c and b.gsc02c = 'gz' and b.gsc04c = @type and (a.gcl06i = 0 or a.gcl06i = 6) and (gcl101c between @fromdept and @todept or @fromdept = '*') --add by maql 20100707 --第6销售部门的 insert #account_dept(note, class, get, brief, date, time, billdate) select gcl01c, 'gz', (isnull(gsc05f, 0)*isnull(gcl27f, 0)), gcl03c + '-' + gcl04c + '充值', gcl80d, gcl56t, gcl02d from #tbl_gcm10_buf a, #gsm03_temp b where a.gcl00c = gsc00c and a.gcl01c = gsc01c and b.gsc02c = 'gz' and b.gsc04c = @type and (a.gcl06i = 0 or a.gcl06i = 6) and (gcl102c between @fromdept and @todept or @fromdept = '*') --add by maql 20100707 --第7销售部门的 insert #account_dept(note, class, get, brief, date, time, billdate) select gcl01c, 'gz', (isnull(gsc05f, 0)*isnull(gcl29f, 0)), gcl03c + '-' + gcl04c + '充值', gcl80d, gcl56t, gcl02d from #tbl_gcm10_buf a, #gsm03_temp b where a.gcl00c = gsc00c and a.gcl01c = gsc01c and b.gsc02c = 'gz' and b.gsc04c = @type and (a.gcl06i = 0 or a.gcl06i = 6) and (gcl103c between @fromdept and @todept or @fromdept = '*') --add by maql 20100707 --第8销售部门的 insert #account_dept(note, class, get, brief, date, time, billdate) select gcl01c, 'gz', (isnull(gsc05f, 0)*isnull(gcl31f, 0)), gcl03c + '-' + gcl04c + '充值', gcl80d, gcl56t, gcl02d from #tbl_gcm10_buf a, #gsm03_temp b where a.gcl00c = gsc00c and a.gcl01c = gsc01c and b.gsc02c = 'gz' and b.gsc04c = @type and (a.gcl06i = 0 or a.gcl06i = 6) and (gcl104c between @fromdept and @todept or @fromdept = '*') --add by maql 20100707 --第9销售部门的 insert #account_dept(note, class, get, brief, date, time, billdate) select gcl01c, 'gz', (isnull(gsc05f, 0)*isnull(gcl33f, 0)), gcl03c + '-' + gcl04c + '充值', gcl80d, gcl56t, gcl02d from #tbl_gcm10_buf a, #gsm03_temp b where a.gcl00c = gsc00c and a.gcl01c = gsc01c and b.gsc02c = 'gz' and b.gsc04c = @type and (a.gcl06i = 0 or a.gcl06i = 6) and (gcl105c between @fromdept and @todept or @fromdept = '*') --add by maql 20100707 --第10销售部门的 insert #account_dept(note, class, get, brief, date, time, billdate) select gcl01c, 'gz', (isnull(gsc05f, 0)*isnull(gcl35f, 0)), gcl03c + '-' + gcl04c + '充值', gcl80d, gcl56t, gcl02d from #tbl_gcm10_buf a, #gsm03_temp b where a.gcl00c = gsc00c and a.gcl01c = gsc01c and b.gsc02c = 'gz' and b.gsc04c = @type and (a.gcl06i = 0 or a.gcl06i = 6) and (gcl106c between @fromdept and @todept or @fromdept = '*') --add by maql 20100707 insert #account(note, class, get, brief, date, time, billdate) select note, class, sum(get), brief, date, time, billdate from #account_dept group by note, class, brief, date, time, billdate delete #account_dept insert #account_dept(note, class, out, brief, date, time, billdate) select gcl01c, 'gz', (isnull(gsc05f, 0)*isnull(gcl16f, 0)), gcl03c + '-' + gcl04c + '取款或欠款', gcl80d, gcl56t, gcl02d from #tbl_gcm10_buf a, #gsm03_temp b where a.gcl00c = gsc00c and a.gcl01c = gsc01c and b.gsc02c = 'gz' and b.gsc04c = @type and (gcl06i = 1 or gcl06i = 5) and (gcl81c between @fromdept and @todept or @fromdept = '*') --add by lyj 2007-09-06 insert #account_dept(note, class, out, brief, date, time, billdate) select gcl01c, 'gz', (isnull(gsc05f, 0)*isnull(gcl18f, 0)), gcl03c + '-' + gcl04c + '取款或欠款', gcl80d, gcl56t, gcl02d from #tbl_gcm10_buf a, #gsm03_temp b where a.gcl00c = gsc00c and a.gcl01c = gsc01c and b.gsc02c = 'gz' and b.gsc04c = @type and (gcl06i = 1 or gcl06i = 5) and (gcl82c between @fromdept and @todept or @fromdept = '*') --add by lyj 2007-09-06 insert #account_dept(note, class, out, brief, date, time, billdate) select gcl01c, 'gz', (isnull(gsc05f, 0)*isnull(gcl20f, 0)), gcl03c + '-' + gcl04c + '取款或欠款', gcl80d, gcl56t, gcl02d from #tbl_gcm10_buf a, #gsm03_temp b where a.gcl00c = gsc00c and a.gcl01c = gsc01c and b.gsc02c = 'gz' and b.gsc04c = @type and (gcl06i = 1 or gcl06i = 5) and (gcl83c between @fromdept and @todept or @fromdept = '*') --add by lyj 2007-09-06 insert #account_dept(note, class, out, brief, date, time, billdate) select gcl01c, 'gz', (isnull(gsc05f, 0)*isnull(gcl23f, 0)), gcl03c + '-' + gcl04c + '取款或欠款', gcl80d, gcl56t, gcl02d from #tbl_gcm10_buf a, #gsm03_temp b where a.gcl00c = gsc00c and a.gcl01c = gsc01c and b.gsc02c = 'gz' and b.gsc04c = @type and (gcl06i = 1 or gcl06i = 5) and (gcl100c between @fromdept and @todept or @fromdept = '*') --add by maql 20100707 insert #account_dept(note, class, out, brief, date, time, billdate) select gcl01c, 'gz', (isnull(gsc05f, 0)*isnull(gcl25f, 0)), gcl03c + '-' + gcl04c + '取款或欠款', gcl80d, gcl56t, gcl02d from #tbl_gcm10_buf a, #gsm03_temp b where a.gcl00c = gsc00c and a.gcl01c = gsc01c and b.gsc02c = 'gz' and b.gsc04c = @type and (gcl06i = 1 or gcl06i = 5) and (gcl101c between @fromdept and @todept or @fromdept = '*') --add by maql 20100707 insert #account_dept(note, class, out, brief, date, time, billdate) select gcl01c, 'gz', (isnull(gsc05f, 0)*isnull(gcl27f, 0)), gcl03c + '-' + gcl04c + '取款或欠款', gcl80d, gcl56t, gcl02d from #tbl_gcm10_buf a, #gsm03_temp b where a.gcl00c = gsc00c and a.gcl01c = gsc01c and b.gsc02c = 'gz' and b.gsc04c = @type and (gcl06i = 1 or gcl06i = 5) and (gcl102c between @fromdept and @todept or @fromdept = '*') --add by maql 20100707 insert #account_dept(note, class, out, brief, date, time, billdate) select gcl01c, 'gz', (isnull(gsc05f, 0)*isnull(gcl29f, 0)), gcl03c + '-' + gcl04c + '取款或欠款', gcl80d, gcl56t, gcl02d from #tbl_gcm10_buf a, #gsm03_temp b where a.gcl00c = gsc00c and a.gcl01c = gsc01c and b.gsc02c = 'gz' and b.gsc04c = @type and (gcl06i = 1 or gcl06i = 5) and (gcl103c between @fromdept and @todept or @fromdept = '*') --add by maql 20100707 insert #account_dept(note, class, out, brief, date, time, billdate) select gcl01c, 'gz', (isnull(gsc05f, 0)*isnull(gcl31f, 0)), gcl03c + '-' + gcl04c + '取款或欠款', gcl80d, gcl56t, gcl02d from #tbl_gcm10_buf a, #gsm03_temp b where a.gcl00c = gsc00c and a.gcl01c = gsc01c and b.gsc02c = 'gz' and b.gsc04c = @type and (gcl06i = 1 or gcl06i = 5) and (gcl104c between @fromdept and @todept or @fromdept = '*') --add by maql 20100707 insert #account_dept(note, class, out, brief, date, time, billdate) select gcl01c, 'gz', (isnull(gsc05f, 0)*isnull(gcl33f, 0)), gcl03c + '-' + gcl04c + '取款或欠款', gcl80d, gcl56t, gcl02d from #tbl_gcm10_buf a, #gsm03_temp b where a.gcl00c = gsc00c and a.gcl01c = gsc01c and b.gsc02c = 'gz' and b.gsc04c = @type and (gcl06i = 1 or gcl06i = 5) and (gcl105c between @fromdept and @todept or @fromdept = '*') --add by maql 20100707 insert #account_dept(note, class, out, brief, date, time, billdate) select gcl01c, 'gz', (isnull(gsc05f, 0)*isnull(gcl35f, 0)), gcl03c + '-' + gcl04c + '取款或欠款', gcl80d, gcl56t, gcl02d from #tbl_gcm10_buf a, #gsm03_temp b where a.gcl00c = gsc00c and a.gcl01c = gsc01c and b.gsc02c = 'gz' and b.gsc04c = @type and (gcl06i = 1 or gcl06i = 5) and (gcl106c between @fromdept and @todept or @fromdept = '*') --add by maql 20100707 insert #account(note, class, out, brief, date, time, billdate) select note, class, sum(out), brief, date, time, billdate from #account_dept group by note, class, brief, date, time, billdate end else begin --即是支付方式也是账户类型的时候先insert账户的值,然后在insert支付方式的值,最后减 delete #account_dept --疗程账户, gcl66f已经限制为疗程的金额,所以没有必要看gcl04c是否为疗程账户了 --第一销售 insert #account_dept(note, class, out, brief, date, time, billdate) select gcl01c, 'gz', gcl66f*isnull(gcl16f, 0), gcl03c + '-' + gcl04c + '充值', gcl80d, gcl56t, gcl02d from #tbl_gcm10_buf a where @accttype = @procacct and (gcl06i = 0 or gcl06i = 6) and (gcl81c between @fromdept and @todept or @fromdept = '*') --add by lyj 2007-09-06 --第二销售 insert #account_dept(note, class, out, brief, date, time, billdate) select gcl01c, 'gz', gcl66f*isnull(gcl18f, 0), gcl03c + '-' + gcl04c + '充值', gcl80d, gcl56t, gcl02d from #tbl_gcm10_buf a where @accttype = @procacct and (gcl06i = 0 or gcl06i = 6) and (gcl82c between @fromdept and @todept or @fromdept = '*') --add by lyj 2007-09-06 --第三销售 insert #account_dept(note, class, out, brief, date, time, billdate) select gcl01c, 'gz', gcl66f*isnull(gcl20f, 0), gcl03c + '-' + gcl04c + '充值', gcl80d, gcl56t, gcl02d from #tbl_gcm10_buf a where @accttype = @procacct and (gcl06i = 0 or gcl06i = 6) and (gcl83c between @fromdept and @todept or @fromdept = '*') --add by lyj 2007-09-06 insert #account_dept(note, class, out, brief, date, time, billdate) select gcl01c, 'gz', gcl66f*isnull(gcl23f, 0), gcl03c + '-' + gcl04c + '充值', gcl80d, gcl56t, gcl02d from #tbl_gcm10_buf a where @accttype = @procacct and (gcl06i = 0 or gcl06i = 6) and (gcl100c between @fromdept and @todept or @fromdept = '*') --add by maql 20100707 insert #account_dept(note, class, out, brief, date, time, billdate) select gcl01c, 'gz', gcl66f*isnull(gcl25f, 0), gcl03c + '-' + gcl04c + '充值', gcl80d, gcl56t, gcl02d from #tbl_gcm10_buf a where @accttype = @procacct and (gcl06i = 0 or gcl06i = 6) and (gcl101c between @fromdept and @todept or @fromdept = '*') --add by maql 20100707 insert #account_dept(note, class, out, brief, date, time, billdate) select gcl01c, 'gz', gcl66f*isnull(gcl27f, 0), gcl03c + '-' + gcl04c + '充值', gcl80d, gcl56t, gcl02d from #tbl_gcm10_buf a where @accttype = @procacct and (gcl06i = 0 or gcl06i = 6) and (gcl102c between @fromdept and @todept or @fromdept = '*') --add by maql 20100707 insert #account_dept(note, class, out, brief, date, time, billdate) select gcl01c, 'gz', gcl66f*isnull(gcl29f, 0), gcl03c + '-' + gcl04c + '充值', gcl80d, gcl56t, gcl02d from #tbl_gcm10_buf a where @accttype = @procacct and (gcl06i = 0 or gcl06i = 6) and (gcl103c between @fromdept and @todept or @fromdept = '*') --add by maql 20100707 insert #account_dept(note, class, out, brief, date, time, billdate) select gcl01c, 'gz', gcl66f*isnull(gcl31f, 0), gcl03c + '-' + gcl04c + '充值', gcl80d, gcl56t, gcl02d from #tbl_gcm10_buf a where @accttype = @procacct and (gcl06i = 0 or gcl06i = 6) and (gcl104c between @fromdept and @todept or @fromdept = '*') --add by maql 20100707 insert #account_dept(note, class, out, brief, date, time, billdate) select gcl01c, 'gz', gcl66f*isnull(gcl33f, 0), gcl03c + '-' + gcl04c + '充值', gcl80d, gcl56t, gcl02d from #tbl_gcm10_buf a where @accttype = @procacct and (gcl06i = 0 or gcl06i = 6) and (gcl105c between @fromdept and @todept or @fromdept = '*') --add by maql 20100707 insert #account_dept(note, class, out, brief, date, time, billdate) select gcl01c, 'gz', gcl66f*isnull(gcl35f, 0), gcl03c + '-' + gcl04c + '充值', gcl80d, gcl56t, gcl02d from #tbl_gcm10_buf a where @accttype = @procacct and (gcl06i = 0 or gcl06i = 6) and (gcl106c between @fromdept and @todept or @fromdept = '*') --add by maql 20100707 --特定的账户 insert #account_dept(note, class, out, brief, date, time, billdate) select gcl01c, 'gz', gcl65f*isnull(gcl16f, 0), gcl03c + '-' + gcl04c + '充值', gcl80d, gcl56t, gcl02d from #tbl_gcm10_buf a where convert(int, gcl04c) = @accttype and (gcl06i = 0 or gcl06i = 6) and (gcl81c between @fromdept and @todept or @fromdept = '*') --add by lyj 2007-09-06 insert #account_dept(note, class, out, brief, date, time, billdate) select gcl01c, 'gz', gcl65f*isnull(gcl18f, 0), gcl03c + '-' + gcl04c + '充值', gcl80d, gcl56t, gcl02d from #tbl_gcm10_buf a where convert(int, gcl04c) = @accttype and (gcl06i = 0 or gcl06i = 6) and (gcl82c between @fromdept and @todept or @fromdept = '*') --add by lyj 2007-09-06 insert #account_dept(note, class, out, brief, date, time, billdate) select gcl01c, 'gz', gcl65f*isnull(gcl20f, 0), gcl03c + '-' + gcl04c + '充值', gcl80d, gcl56t, gcl02d from #tbl_gcm10_buf a where convert(int, gcl04c) = @accttype and (gcl06i = 0 or gcl06i = 6) and (gcl83c between @fromdept and @todept or @fromdept = '*') --add by lyj 2007-09-06 insert #account_dept(note, class, out, brief, date, time, billdate) select gcl01c, 'gz', gcl65f*isnull(gcl23f, 0), gcl03c + '-' + gcl04c + '充值', gcl80d, gcl56t, gcl02d from #tbl_gcm10_buf a where convert(int, gcl04c) = @accttype and (gcl06i = 0 or gcl06i = 6) and (gcl100c between @fromdept and @todept or @fromdept = '*') --add by maql 20100707 insert #account_dept(note, class, out, brief, date, time, billdate) select gcl01c, 'gz', gcl65f*isnull(gcl25f, 0), gcl03c + '-' + gcl04c + '充值', gcl80d, gcl56t, gcl02d from #tbl_gcm10_buf a where convert(int, gcl04c) = @accttype and (gcl06i = 0 or gcl06i = 6) and (gcl101c between @fromdept and @todept or @fromdept = '*') --add by maql 20100707 insert #account_dept(note, class, out, brief, date, time, billdate) select gcl01c, 'gz', gcl65f*isnull(gcl27f, 0), gcl03c + '-' + gcl04c + '充值', gcl80d, gcl56t, gcl02d from #tbl_gcm10_buf a where convert(int, gcl04c) = @accttype and (gcl06i = 0 or gcl06i = 6) and (gcl102c between @fromdept and @todept or @fromdept = '*') --add by maql 20100707 insert #account_dept(note, class, out, brief, date, time, billdate) select gcl01c, 'gz', gcl65f*isnull(gcl29f, 0), gcl03c + '-' + gcl04c + '充值', gcl80d, gcl56t, gcl02d from #tbl_gcm10_buf a where convert(int, gcl04c) = @accttype and (gcl06i = 0 or gcl06i = 6) and (gcl103c between @fromdept and @todept or @fromdept = '*') --add by maql 20100707 insert #account_dept(note, class, out, brief, date, time, billdate) select gcl01c, 'gz', gcl65f*isnull(gcl31f, 0), gcl03c + '-' + gcl04c + '充值', gcl80d, gcl56t, gcl02d from #tbl_gcm10_buf a where convert(int, gcl04c) = @accttype and (gcl06i = 0 or gcl06i = 6) and (gcl104c between @fromdept and @todept or @fromdept = '*') --add by maql 20100707 insert #account_dept(note, class, out, brief, date, time, billdate) select gcl01c, 'gz', gcl65f*isnull(gcl33f, 0), gcl03c + '-' + gcl04c + '充值', gcl80d, gcl56t, gcl02d from #tbl_gcm10_buf a where convert(int, gcl04c) = @accttype and (gcl06i = 0 or gcl06i = 6) and (gcl105c between @fromdept and @todept or @fromdept = '*') --add by maql 20100707 insert #account_dept(note, class, out, brief, date, time, billdate) select gcl01c, 'gz', gcl65f*isnull(gcl35f, 0), gcl03c + '-' + gcl04c + '充值', gcl80d, gcl56t, gcl02d from #tbl_gcm10_buf a where convert(int, gcl04c) = @accttype and (gcl06i = 0 or gcl06i = 6) and (gcl106c between @fromdept and @todept or @fromdept = '*') --add by maql 20100707 --特定的支付方式 insert #account_dept(note, class, out, brief, date, time, billdate) select gcl01c, 'gz', gsc05f*isnull(gcl16f, 0), gcl03c + '-' + gcl04c + '充值', gcl80d, gcl56t, gcl02d from #tbl_gcm10_buf a, #gsm03_temp b where a.gcl00c = gsc00c and a.gcl01c = gsc01c and b.gsc02c = 'gz' and b.gsc04c = @type and (gcl06i = 0 or gcl06i = 6) and (gcl81c between @fromdept and @todept or @fromdept = '*') --add by lyj 2007-09-06 insert #account_dept(note, class, out, brief, date, time, billdate) select gcl01c, 'gz', gsc05f*isnull(gcl18f, 0), gcl03c + '-' + gcl04c + '充值', gcl80d, gcl56t, gcl02d from #tbl_gcm10_buf a, #gsm03_temp b where a.gcl00c = gsc00c and a.gcl01c = gsc01c and b.gsc02c = 'gz' and b.gsc04c = @type and (gcl06i = 0 or gcl06i = 6) and (gcl82c between @fromdept and @todept or @fromdept = '*') --add by lyj 2007-09-06 insert #account_dept(note, class, out, brief, date, time, billdate) select gcl01c, 'gz', gsc05f*isnull(gcl20f, 0), gcl03c + '-' + gcl04c + '充值', gcl80d, gcl56t, gcl02d from #tbl_gcm10_buf a, #gsm03_temp b where a.gcl00c = gsc00c and a.gcl01c = gsc01c and b.gsc02c = 'gz' and b.gsc04c = @type and (gcl06i = 0 or gcl06i = 6) and (gcl83c between @fromdept and @todept or @fromdept = '*') --add by lyj 2007-09-06 insert #account_dept(note, class, out, brief, date, time, billdate) select gcl01c, 'gz', gsc05f*isnull(gcl23f, 0), gcl03c + '-' + gcl04c + '充值', gcl80d, gcl56t, gcl02d from #tbl_gcm10_buf a, #gsm03_temp b where a.gcl00c = gsc00c and a.gcl01c = gsc01c and b.gsc02c = 'gz' and b.gsc04c = @type and (gcl06i = 0 or gcl06i = 6) and (gcl100c between @fromdept and @todept or @fromdept = '*') --add by maql 20100707 insert #account_dept(note, class, out, brief, date, time, billdate) select gcl01c, 'gz', gsc05f*isnull(gcl25f, 0), gcl03c + '-' + gcl04c + '充值', gcl80d, gcl56t, gcl02d from #tbl_gcm10_buf a, #gsm03_temp b where a.gcl00c = gsc00c and a.gcl01c = gsc01c and b.gsc02c = 'gz' and b.gsc04c = @type and (gcl06i = 0 or gcl06i = 6) and (gcl101c between @fromdept and @todept or @fromdept = '*') --add by maql 20100707 insert #account_dept(note, class, out, brief, date, time, billdate) select gcl01c, 'gz', gsc05f*isnull(gcl27f, 0), gcl03c + '-' + gcl04c + '充值', gcl80d, gcl56t, gcl02d from #tbl_gcm10_buf a, #gsm03_temp b where a.gcl00c = gsc00c and a.gcl01c = gsc01c and b.gsc02c = 'gz' and b.gsc04c = @type and (gcl06i = 0 or gcl06i = 6) and (gcl102c between @fromdept and @todept or @fromdept = '*') --add by maql 20100707 insert #account_dept(note, class, out, brief, date, time, billdate) select gcl01c, 'gz', gsc05f*isnull(gcl29f, 0), gcl03c + '-' + gcl04c + '充值', gcl80d, gcl56t, gcl02d from #tbl_gcm10_buf a, #gsm03_temp b where a.gcl00c = gsc00c and a.gcl01c = gsc01c and b.gsc02c = 'gz' and b.gsc04c = @type and (gcl06i = 0 or gcl06i = 6) and (gcl103c between @fromdept and @todept or @fromdept = '*') --add by maql 20100707 insert #account_dept(note, class, out, brief, date, time, billdate) select gcl01c, 'gz', gsc05f*isnull(gcl31f, 0), gcl03c + '-' + gcl04c + '充值', gcl80d, gcl56t, gcl02d from #tbl_gcm10_buf a, #gsm03_temp b where a.gcl00c = gsc00c and a.gcl01c = gsc01c and b.gsc02c = 'gz' and b.gsc04c = @type and (gcl06i = 0 or gcl06i = 6) and (gcl104c between @fromdept and @todept or @fromdept = '*') --add by maql 20100707 insert #account_dept(note, class, out, brief, date, time, billdate) select gcl01c, 'gz', gsc05f*isnull(gcl33f, 0), gcl03c + '-' + gcl04c + '充值', gcl80d, gcl56t, gcl02d from #tbl_gcm10_buf a, #gsm03_temp b where a.gcl00c = gsc00c and a.gcl01c = gsc01c and b.gsc02c = 'gz' and b.gsc04c = @type and (gcl06i = 0 or gcl06i = 6) and (gcl105c between @fromdept and @todept or @fromdept = '*') --add by maql 20100707 insert #account_dept(note, class, out, brief, date, time, billdate) select gcl01c, 'gz', gsc05f*isnull(gcl35f, 0), gcl03c + '-' + gcl04c + '充值', gcl80d, gcl56t, gcl02d from #tbl_gcm10_buf a, #gsm03_temp b where a.gcl00c = gsc00c and a.gcl01c = gsc01c and b.gsc02c = 'gz' and b.gsc04c = @type and (gcl06i = 0 or gcl06i = 6) and (gcl106c between @fromdept and @todept or @fromdept = '*') --add by maql 20100707 insert #account_dept(note, class, get, brief, date, time, billdate) select gcl01c, 'gz', gcl66f*isnull(gcl16f, 0), gcl03c + '-' + gcl04c + '取款或欠款', gcl80d, gcl56t, gcl02d from #tbl_gcm10_buf a where convert(int, gcl04c) = @accttype and @accttype = @procacct and (gcl06i = 1 or gcl06i = 5) and (gcl81c between @fromdept and @todept or @fromdept = '*') --add by lyj 2007-09-06 insert #account_dept(note, class, get, brief, date, time, billdate) select gcl01c, 'gz', gcl66f*isnull(gcl18f, 0), gcl03c + '-' + gcl04c + '取款或欠款', gcl80d, gcl56t, gcl02d from #tbl_gcm10_buf a where convert(int, gcl04c) = @accttype and @accttype = @procacct and (gcl06i = 1 or gcl06i = 5) and (gcl82c between @fromdept and @todept or @fromdept = '*') --add by lyj 2007-09-06 insert #account_dept(note, class, get, brief, date, time, billdate) select gcl01c, 'gz', gcl66f*isnull(gcl20f, 0), gcl03c + '-' + gcl04c + '取款或欠款', gcl80d, gcl56t, gcl02d from #tbl_gcm10_buf a where convert(int, gcl04c) = @accttype and @accttype = @procacct and (gcl06i = 1 or gcl06i = 5) and (gcl83c between @fromdept and @todept or @fromdept = '*') --add by lyj 2007-09-06 insert #account_dept(note, class, get, brief, date, time, billdate) select gcl01c, 'gz', gcl66f*isnull(gcl23f, 0), gcl03c + '-' + gcl04c + '取款或欠款', gcl80d, gcl56t, gcl02d from #tbl_gcm10_buf a where convert(int, gcl04c) = @accttype and @accttype = @procacct and (gcl06i = 1 or gcl06i = 5) and (gcl100c between @fromdept and @todept or @fromdept = '*') --add by maql 20100707 insert #account_dept(note, class, get, brief, date, time, billdate) select gcl01c, 'gz', gcl66f*isnull(gcl25f, 0), gcl03c + '-' + gcl04c + '取款或欠款', gcl80d, gcl56t, gcl02d from #tbl_gcm10_buf a where convert(int, gcl04c) = @accttype and @accttype = @procacct and (gcl06i = 1 or gcl06i = 5) and (gcl101c between @fromdept and @todept or @fromdept = '*') --add by maql 20100707 insert #account_dept(note, class, get, brief, date, time, billdate) select gcl01c, 'gz', gcl66f*isnull(gcl27f, 0), gcl03c + '-' + gcl04c + '取款或欠款', gcl80d, gcl56t, gcl02d from #tbl_gcm10_buf a where convert(int, gcl04c) = @accttype and @accttype = @procacct and (gcl06i = 1 or gcl06i = 5) and (gcl102c between @fromdept and @todept or @fromdept = '*') --add by maql 20100707 insert #account_dept(note, class, get, brief, date, time, billdate) select gcl01c, 'gz', gcl66f*isnull(gcl29f, 0), gcl03c + '-' + gcl04c + '取款或欠款', gcl80d, gcl56t, gcl02d from #tbl_gcm10_buf a where convert(int, gcl04c) = @accttype and @accttype = @procacct and (gcl06i = 1 or gcl06i = 5) and (gcl103c between @fromdept and @todept or @fromdept = '*') --add by maql 20100707 insert #account_dept(note, class, get, brief, date, time, billdate) select gcl01c, 'gz', gcl66f*isnull(gcl31f, 0), gcl03c + '-' + gcl04c + '取款或欠款', gcl80d, gcl56t, gcl02d from #tbl_gcm10_buf a where convert(int, gcl04c) = @accttype and @accttype = @procacct and (gcl06i = 1 or gcl06i = 5) and (gcl104c between @fromdept and @todept or @fromdept = '*') --add by maql 20100707 insert #account_dept(note, class, get, brief, date, time, billdate) select gcl01c, 'gz', gcl66f*isnull(gcl33f, 0), gcl03c + '-' + gcl04c + '取款或欠款', gcl80d, gcl56t, gcl02d from #tbl_gcm10_buf a where convert(int, gcl04c) = @accttype and @accttype = @procacct and (gcl06i = 1 or gcl06i = 5) and (gcl105c between @fromdept and @todept or @fromdept = '*') --add by maql 20100707 insert #account_dept(note, class, get, brief, date, time, billdate) select gcl01c, 'gz', gcl66f*isnull(gcl35f, 0), gcl03c + '-' + gcl04c + '取款或欠款', gcl80d, gcl56t, gcl02d from #tbl_gcm10_buf a where convert(int, gcl04c) = @accttype and @accttype = @procacct and (gcl06i = 1 or gcl06i = 5) and (gcl106c between @fromdept and @todept or @fromdept = '*') --add by maql 20100707 insert #account_dept(note, class, get, brief, date, time, billdate) select gcl01c, 'gz', gcl65f*isnull(gcl16f, 0), gcl03c + '-' + gcl04c + '取款或欠款', gcl80d, gcl56t, gcl02d from #tbl_gcm10_buf a where convert(int, gcl04c) = @accttype and (gcl06i = 1 or gcl06i = 5) and (gcl81c between @fromdept and @todept or @fromdept = '*') --add by lyj 2007-09-06 insert #account_dept(note, class, get, brief, date, time, billdate) select gcl01c, 'gz', gcl65f*isnull(gcl18f, 0), gcl03c + '-' + gcl04c + '取款或欠款', gcl80d, gcl56t, gcl02d from #tbl_gcm10_buf a where convert(int, gcl04c) = @accttype and (gcl06i = 1 or gcl06i = 5) and (gcl82c between @fromdept and @todept or @fromdept = '*') --add by lyj 2007-09-06 insert #account_dept(note, class, get, brief, date, time, billdate) select gcl01c, 'gz', gcl65f*isnull(gcl20f, 0), gcl03c + '-' + gcl04c + '取款或欠款', gcl80d, gcl56t, gcl02d from #tbl_gcm10_buf a where convert(int, gcl04c) = @accttype and (gcl06i = 1 or gcl06i = 5) and (gcl83c between @fromdept and @todept or @fromdept = '*') --add by lyj 2007-09-06 insert #account_dept(note, class, get, brief, date, time, billdate) select gcl01c, 'gz', gcl65f*isnull(gcl23f, 0), gcl03c + '-' + gcl04c + '取款或欠款', gcl80d, gcl56t, gcl02d from #tbl_gcm10_buf a where convert(int, gcl04c) = @accttype and (gcl06i = 1 or gcl06i = 5) and (gcl100c between @fromdept and @todept or @fromdept = '*') --add by maql 20100707 insert #account_dept(note, class, get, brief, date, time, billdate) select gcl01c, 'gz', gcl65f*isnull(gcl25f, 0), gcl03c + '-' + gcl04c + '取款或欠款', gcl80d, gcl56t, gcl02d from #tbl_gcm10_buf a where convert(int, gcl04c) = @accttype and (gcl06i = 1 or gcl06i = 5) and (gcl101c between @fromdept and @todept or @fromdept = '*') --add by maql 20100707 insert #account_dept(note, class, get, brief, date, time, billdate) select gcl01c, 'gz', gcl65f*isnull(gcl27f, 0), gcl03c + '-' + gcl04c + '取款或欠款', gcl80d, gcl56t, gcl02d from #tbl_gcm10_buf a where convert(int, gcl04c) = @accttype and (gcl06i = 1 or gcl06i = 5) and (gcl102c between @fromdept and @todept or @fromdept = '*') --add by maql 20100707 insert #account_dept(note, class, get, brief, date, time, billdate) select gcl01c, 'gz', gcl65f*isnull(gcl29f, 0), gcl03c + '-' + gcl04c + '取款或欠款', gcl80d, gcl56t, gcl02d from #tbl_gcm10_buf a where convert(int, gcl04c) = @accttype and (gcl06i = 1 or gcl06i = 5) and (gcl103c between @fromdept and @todept or @fromdept = '*') --add by maql 20100707 insert #account_dept(note, class, get, brief, date, time, billdate) select gcl01c, 'gz', gcl65f*isnull(gcl31f, 0), gcl03c + '-' + gcl04c + '取款或欠款', gcl80d, gcl56t, gcl02d from #tbl_gcm10_buf a where convert(int, gcl04c) = @accttype and (gcl06i = 1 or gcl06i = 5) and (gcl104c between @fromdept and @todept or @fromdept = '*') --add by maql 20100707 insert #account_dept(note, class, get, brief, date, time, billdate) select gcl01c, 'gz', gcl65f*isnull(gcl33f, 0), gcl03c + '-' + gcl04c + '取款或欠款', gcl80d, gcl56t, gcl02d from #tbl_gcm10_buf a where convert(int, gcl04c) = @accttype and (gcl06i = 1 or gcl06i = 5) and (gcl105c between @fromdept and @todept or @fromdept = '*') --add by maql 20100707 insert #account_dept(note, class, get, brief, date, time, billdate) select gcl01c, 'gz', gcl65f*isnull(gcl35f, 0), gcl03c + '-' + gcl04c + '取款或欠款', gcl80d, gcl56t, gcl02d from #tbl_gcm10_buf a where convert(int, gcl04c) = @accttype and (gcl06i = 1 or gcl06i = 5) and (gcl106c between @fromdept and @todept or @fromdept = '*') --add by maql 20100707 insert #account_dept(note, class, get, brief, date, time, billdate) select gcl01c, 'gz', gsc05f*isnull(gcl16f, 0), gcl03c + '-' + gcl04c + '取款或欠款', gcl80d, gcl56t, gcl02d from #tbl_gcm10_buf a, #gsm03_temp b where a.gcl00c = gsc00c and a.gcl01c = gsc01c and gsc02c = 'gz' and gsc04c = @type and (gcl06i = 1 or gcl06i = 5) and (gcl81c between @fromdept and @todept or @fromdept = '*') --add by lyj 2007-09-06 insert #account_dept(note, class, get, brief, date, time, billdate) select gcl01c, 'gz', gsc05f*isnull(gcl18f, 0), gcl03c + '-' + gcl04c + '取款或欠款', gcl80d, gcl56t, gcl02d from #tbl_gcm10_buf a, #gsm03_temp b where a.gcl00c = gsc00c and a.gcl01c = gsc01c and gsc02c = 'gz' and gsc04c = @type and (gcl06i = 1 or gcl06i = 5) and (gcl82c between @fromdept and @todept or @fromdept = '*') --add by lyj 2007-09-06 insert #account_dept(note, class, get, brief, date, time, billdate) select gcl01c, 'gz', gsc05f*isnull(gcl20f, 0), gcl03c + '-' + gcl04c + '取款或欠款', gcl80d, gcl56t, gcl02d from #tbl_gcm10_buf a, #gsm03_temp b where a.gcl00c = gsc00c and a.gcl01c = gsc01c and gsc02c = 'gz' and gsc04c = @type and (gcl06i = 1 or gcl06i = 5) and (gcl83c between @fromdept and @todept or @fromdept = '*') --add by lyj 2007-09-06 insert #account_dept(note, class, get, brief, date, time, billdate) select gcl01c, 'gz', gsc05f*isnull(gcl23f, 0), gcl03c + '-' + gcl04c + '取款或欠款', gcl80d, gcl56t, gcl02d from #tbl_gcm10_buf a, #gsm03_temp b where a.gcl00c = gsc00c and a.gcl01c = gsc01c and gsc02c = 'gz' and gsc04c = @type and (gcl06i = 1 or gcl06i = 5) and (gcl100c between @fromdept and @todept or @fromdept = '*') --add by lyj 20100707 insert #account_dept(note, class, get, brief, date, time, billdate) select gcl01c, 'gz', gsc05f*isnull(gcl25f, 0), gcl03c + '-' + gcl04c + '取款或欠款', gcl80d, gcl56t, gcl02d from #tbl_gcm10_buf a, #gsm03_temp b where a.gcl00c = gsc00c and a.gcl01c = gsc01c and gsc02c = 'gz' and gsc04c = @type and (gcl06i = 1 or gcl06i = 5) and (gcl101c between @fromdept and @todept or @fromdept = '*') --add by lyj 20100707 insert #account_dept(note, class, get, brief, date, time, billdate) select gcl01c, 'gz', gsc05f*isnull(gcl27f, 0), gcl03c + '-' + gcl04c + '取款或欠款', gcl80d, gcl56t, gcl02d from #tbl_gcm10_buf a, #gsm03_temp b where a.gcl00c = gsc00c and a.gcl01c = gsc01c and gsc02c = 'gz' and gsc04c = @type and (gcl06i = 1 or gcl06i = 5) and (gcl102c between @fromdept and @todept or @fromdept = '*') --add by lyj 20100707 insert #account_dept(note, class, get, brief, date, time, billdate) select gcl01c, 'gz', gsc05f*isnull(gcl29f, 0), gcl03c + '-' + gcl04c + '取款或欠款', gcl80d, gcl56t, gcl02d from #tbl_gcm10_buf a, #gsm03_temp b where a.gcl00c = gsc00c and a.gcl01c = gsc01c and gsc02c = 'gz' and gsc04c = @type and (gcl06i = 1 or gcl06i = 5) and (gcl103c between @fromdept and @todept or @fromdept = '*') --add by lyj 20100707 insert #account_dept(note, class, get, brief, date, time, billdate) select gcl01c, 'gz', gsc05f*isnull(gcl31f, 0), gcl03c + '-' + gcl04c + '取款或欠款', gcl80d, gcl56t, gcl02d from #tbl_gcm10_buf a, #gsm03_temp b where a.gcl00c = gsc00c and a.gcl01c = gsc01c and gsc02c = 'gz' and gsc04c = @type and (gcl06i = 1 or gcl06i = 5) and (gcl104c between @fromdept and @todept or @fromdept = '*') --add by lyj 20100707 insert #account_dept(note, class, get, brief, date, time, billdate) select gcl01c, 'gz', gsc05f*isnull(gcl33f, 0), gcl03c + '-' + gcl04c + '取款或欠款', gcl80d, gcl56t, gcl02d from #tbl_gcm10_buf a, #gsm03_temp b where a.gcl00c = gsc00c and a.gcl01c = gsc01c and gsc02c = 'gz' and gsc04c = @type and (gcl06i = 1 or gcl06i = 5) and (gcl105c between @fromdept and @todept or @fromdept = '*') --add by lyj 20100707 insert #account_dept(note, class, get, brief, date, time, billdate) select gcl01c, 'gz', gsc05f*isnull(gcl35f, 0), gcl03c + '-' + gcl04c + '取款或欠款', gcl80d, gcl56t, gcl02d from #tbl_gcm10_buf a, #gsm03_temp b where a.gcl00c = gsc00c and a.gcl01c = gsc01c and gsc02c = 'gz' and gsc04c = @type and (gcl06i = 1 or gcl06i = 5) and (gcl106c between @fromdept and @todept or @fromdept = '*') --add by lyj 20100707 insert #account(note, class, get, out, brief, date, time, billdate) select note, class, sum(get), sum(out), brief, date, time, billdate from #account_dept group by note, class, brief, date, time, billdate end --不用管是否购买疗程 end -- 从费用登记明细中 得到金额 if @type = '1' and @inc_expense = 1 begin insert #account(note, class, out, brief, date, time, billdate) select gqb01c, 'gv', sum(gqb05f), gqb04c, gqa80d, gqa13t, gqa02d from gqm02 with (nolock), gqm01 with (nolock), gam26 z where gqa00c = gqb00c and gqa01c = gqb01c and gqa00c = z.gaz02c and z.gaz01c = @incompid and gqa02d >= @fromdate and gqa02d <= @todate and (gqa81c between @fromdept and @todept and @fromdept = '*') --add by lyj 2007-09-06 --and isnull(gqb19i, 0) = 0--是否虚拟款 group by gqb00c, gqb01c, gqb04c, gqa81c, gqa02d, gqa80d, gqa13t end --add by lyj 要把会籍变更涉及到的金额加进去 insert #account(note, class, get, brief, date, time, billdate) select gea01c, 'ge', sum(gsc05f), gea04c, gea80d, gea22t, gea03d from gem01, gsm03, gam26 z where gea00c = z.gaz02c and z.gaz01c = @incompid and gsc00c = gea00c and gea01c = gsc01c and gsc02c = 'ge' and gsc04c = @type and gea80d >= @fromdate and gea80d <= @todate and gea21c like @staff and (gea81c between @fromdept and @todept or @fromdept = '*') --add by lyj 2007-09-06 group by gea01c, gea04c, gea80d, gea03d, gea22t --end --- update the class name update a set cname = gsf03c from gsm06 s, #account a where gsf01c = '1' and gsf02c = class -- update 贷方金额, 余额 update #account set out = -get where get < 0 update #account set get = 0 where out > 0 update #account set balance = isnull(get, 0)-isnull(out, 0) -- update date -- 会员卡销售单 update a set date = gna80d, brief = brief, time = gna12t, staff = gna93c, billdate = gna02d from gnm01 g, #account a, gam26 z where gna00c = z.gaz02c and z.gaz01c = @incompid and gna01c = note and class = 'gn' --项目消费(收银)单 update a set date = gga80d, brief = gga09c + '收银', time = gga04t, staff = gga93c, billdate = gga02d from ggm01 g, #account a, gam26 z where gga00c = z.gaz02c and z.gaz01c = @incompid and gga01c = note and class = 'gx' update a set date = gla80d, brief = gla06c + '统一收银', time = gla04t, staff = gla95c, billdate = gla02d from glm01 g, #account a, gam26 z where gla00c = z.gaz02c and z.gaz01c = @incompid and gla01c = note and class = 'gl' update a set date = gcl80d, brief = gcl03c + '-' + gcl04c + '账户异动', time = gcl56t, staff = gcl93c, billdate = gcl02d from gcm10 g, #account a, gam26 z where gcl00c = z.gaz02c and z.gaz01c = @incompid and gcl01c = note and class = 'gz' --会员卡异动单 update a set date = gea80d, brief = gea04c, time = gea22t, staff = gea95c, billdate = gea03d from gem01 g, #account a, gam26 z where gea00c = z.gaz02c and z.gaz01c = @incompid and gea01c = note and class = 'ge' and (g.gea81c between @fromdept and @todept or @fromdept = '*')--add by lyj @fromdept --费用登记 update a set date = gqa80d, time = gqa13t, staff = gqa95c, billdate = gqa02d from gqm01 j, #account a, gam26 z where gqa00c = z.gaz02c and z.gaz01c = @incompid and gqa01c = note and class = 'gv' and (j.gqa81c between @fromdept and @todept or @fromdept = '*')--add by lyj @fromdept -- delete the outdated record declare @sql nvarchar(1024) declare @where nvarchar(1024) if @fromdate <> '*' or @todate <> '*' begin set @sql = ' delete #account where ' if @fromdate <> '*' set @where = ' date < ' + quotename(@fromdate, '''') if @todate <> '*' begin if @fromdate <>'*' set @where = @where + ' or date > ' + quotename(@todate, '''') else set @where = ' date > ' + quotename(@todate, '''') end set @sql = @sql + @where exec sp_executesql @sql end if @staff<>'%' begin delete #account where staff<>@staff end else begin delete #account where isnull(staff, '') = '' end select isnull(date,'') date, isnull(time,'') time, isnull(cname,'') cname, isnull(note,'') note, isnull(brief,'') brief, isnull(get,'') get, isnull(out,'') out, isnull(balance,'') balance, isnull(billdate,'') billdate from #account where isnull(balance, 0) <>0 and (class = @billtype or isnull(@billtype, '*') = '*') --add by tanxulin 2010/02/24,增加查询条件单据类别,如'gz', 'gx', ... order by date, cname, note drop table #account drop table #account_dept drop table #tbl_gnm01_gnm02 drop table #tbl_gnm01_gnm03 drop table #tbl_gcm10_buf end GO /****** Object: StoredProcedure [dbo].[upg_stst_instock_detail] Script Date: 06/12/2017 06:20:46 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_stst_instock_detail] ( @compid varchar(10), -- 公司别 @fromdate varchar(10), -- 开始日期 @todate varchar(10), -- 截至日期 @fromgood varchar(20), -- 开始产品编号 @togood varchar(20), -- 截至产品编号 @fromstockid varchar(10), -- 仓库编号 @tostockid varchar(10) -- 仓库编号 ) as begin declare @includesons float select @includesons = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP103' create table #tbl_in_stock( goodid varchar(20) not null, -- 产品代码 intype varchar(10) not null, -- 出库类型 quan float null, -- 数量 amt float null, unit varchar(10) null) if @includesons = 1 begin insert #tbl_in_stock(goodid, intype, quan, amt) select ghb03c, gha07i, sum(ghb13f), sum(isnull(ghb11f, 0)) from ghm01 a with (nolock), ghm02 b with (nolock), gam26 z where a.gha00c = b.ghb00c and a.gha01c = b.ghb01c and ((a.gha03d >= @fromdate and a.gha03d <= @todate) or (@fromdate = '*')) and ((a.gha04c >= @fromstockid and a.gha04c <= @tostockid) or (@fromstockid = '*')) and ((b.ghb03c >= @fromgood and b.ghb03c <= @togood) or (@fromgood = '*')) and a.gha00c = z.gaz02c and z.gaz01c = @compid group by ghb03c, gha07i end else begin insert #tbl_in_stock(goodid, intype, quan, amt) select ghb03c, gha07i, sum(ghb13f), sum(isnull(ghb11f, 0)) from ghm01 a with (nolock), ghm02 b with (nolock) where a.gha00c = b.ghb00c and a.gha01c = b.ghb01c and ((a.gha03d >= @fromdate and a.gha03d <= @todate) or (@fromdate = '*')) and ((a.gha04c >= @fromstockid and a.gha04c <= @tostockid) or (@fromstockid = '*')) and ((b.ghb03c >= @fromgood and b.ghb03c <= @togood) or (@fromgood = '*')) and a.gha00c = @compid group by ghb03c, gha07i end select * from #tbl_in_stock order by intype drop table #tbl_in_stock end GO /****** Object: StoredProcedure [dbo].[upg_stock_statistic_ex] Script Date: 06/12/2017 06:20:45 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_stock_statistic_ex] ( @compid varchar(10), -- 公司编号 @date varchar(10), -- 日期 @goodsfrom varchar(20), -- 产品编号from @goodsto varchar(20), -- 产品编号to @garagefrom varchar(20), -- 仓库编号from @garageto varchar(20), -- 仓库编号to @returntype int, -- 1-返回所有满足条件的产品,2-返回库存量少的产品 3-返回现库存量,安全存量,低于安全存量, 5-用于库存量统计报表 @quan float, -- 安全库存量 @provider varchar(20), -- 供应商 @kindfrom varchar(20), -- 开始产品类别 @kindto varchar(20), -- 截至产品类别 @sorttype int , -- 排序类别 @grb01c varchar(20), -- 单号 @grb00c varchar(20) -- 申请门店 ) as--wuxm, 2004/07/10 --WUXM modified 2005/08/10 begin declare @incompid varchar(10) select @incompid = @compid declare @includesons float select @includesons = gsa03f from gsm01 where gsa00c = @incompid and gsa01c = 'SP103' create table #in_remain_stock --存放入库的数量(进货单位向标准单位转换) ( id int identity not null, goodsno varchar(20) null, --产品编号 goodsname varchar(80) null, --产品名称 garageno varchar(20) null, --仓库编号 garagename varchar(20) null, --仓库名称 goodstype varchar(40) null, --产品类别 unit varchar(10) null, --单位 quantity float null, --数量 safetyquan float null, --安全存量 maxsafequan float null, --最高安全库存 --add by liyh amt float null, --金额 amt_cost float null, --成本价核算成本 price float null, --标准单价 cost float null, --成本单价 provider varchar(20) null, --供应商 stattype varchar(20) null, --统计类别 goodspec varchar(40) null, --产品规格 primary key nonclustered(id) ) create table #lrm02 ( grb03c varchar(20) null -- 产品编号 ) --查询当前单号所有 insert into #lrm02 select grb03c from lrm02 a with(nolock) where a.grb00c = @grb00c and a.grb01c = @grb01c create clustered INDEX IDX_in_remain_stock_01 on #in_remain_stock(goodsno, garageno) create table #out_stock -- 存放出库、销货、耗用的数(消耗单位向标准单位转换) ( id int identity not null, goodsno varchar(20) null, --产品编号 goodsname varchar(80) null, --产品名称 garageno varchar(20) null, --仓库编号 garagename varchar(20) null, --仓库名称 unit varchar(10) null, --单位 quantity float null, --数量 primary key nonclustered(id) ) create clustered INDEX IDX_out_stock_01 on #out_stock(goodsno, garageno) if @includesons = 1 begin insert #in_remain_stock(goodsno, goodsname, garageno, garagename, goodstype, unit, quantity, safetyquan, amt) select gfd04c, '', gfd08c, '', '', '', sum(isnull(gfd05f, 0)), --modify by lm 20150627(gfd05f标准单位数量改为入库单位或出库单位数量) 0, sum(isnull(gfd13f, 0)) from gfm04 b with (nolock), gam26 z, gfm01 c with (nolock) where gfd00c = z.gaz02c and z.gaz01c = @incompid and gfd01c = '1' and b.gfd04c = c.gfa01c and c.gfa00c = @incompid and b.gfd07c <= @date and ((@garagefrom = '*') or (gfd08c between @garagefrom and @garageto)) and ((@goodsfrom = '*') or (gfd04c in(select grb03c from #lrm02))) and ((@kindfrom = '*') or (gfa24c between @kindfrom and @kindto)) group by gfd04c, gfd08c insert #out_stock(goodsno, goodsname, garageno, garagename, unit, quantity) select gfd04c, '', gfd08c, '', '', sum(isnull(gfd05f, 0)) --modify by lm 20150627(gfd05f标准单位数量改为入库单位或出库单位数量) from gfm04 b with (nolock), gam26 z, gfm01 c with (nolock) where gfd00c = z.gaz02c and z.gaz01c = @incompid and gfd01c in ('2', '3', '4', '5', '6', '7', '8') and b.gfd04c = c.gfa01c and c.gfa00c = @incompid and gfd07c <= @date and ((@garagefrom = '*') or (gfd08c between @garagefrom and @garageto)) and ((@goodsfrom = '*') or (gfd04c in(select grb03c from #lrm02))) and ((@kindfrom = '*') or (gfa24c between @kindfrom and @kindto)) group by gfd04c, gfd08c end else begin insert #in_remain_stock(goodsno, goodsname, garageno, garagename, goodstype, unit, quantity, safetyquan, amt) select gfd04c, '', gfd08c, '', '', '', sum(isnull(gfd05f, 0)), --modify by lm 20150627(gfd05f标准单位数量改为入库单位或出库单位数量) 0, sum(isnull(gfd13f, 0)) from gfm04 b with (nolock), gfm01 c with (nolock) where gfd00c = @incompid and gfd01c = '1' and b.gfd04c = c.gfa01c and c.gfa00c = @incompid and b.gfd07c <= @date and ((@garagefrom = '*') or (gfd08c between @garagefrom and @garageto)) and ((@goodsfrom = '*') or (gfd04c in(select grb03c from #lrm02))) and ((@kindfrom = '*') or (gfa24c between @kindfrom and @kindto)) group by gfd04c, gfd08c --select * from #in_remain_stock insert #out_stock(goodsno, goodsname, garageno, garagename, unit, quantity) select gfd04c, '', gfd08c, '', '', sum(isnull(gfd05f, 0)) --modify by lm 20150627(gfd05f标准单位数量改为入库单位或出库单位数量) from gfm04 b with (nolock), gfm01 c with (nolock) where gfd00c = @incompid and gfd01c in ('2', '3', '4', '5', '6', '7', '8') and b.gfd04c = c.gfa01c and c.gfa00c = @incompid and gfd07c <= @date and ((@garagefrom = '*') or (gfd08c between @garagefrom and @garageto)) and ((@goodsfrom = '*') or (gfd04c in(select grb03c from #lrm02))) and ((@kindfrom = '*') or (gfa24c between @kindfrom and @kindto)) group by gfd04c, gfd08c end --统计出库(包括销货)的数量更新库存量 update t set t.quantity = t.quantity - a.quantity from #out_stock a, #in_remain_stock t where a.goodsno = t.goodsno and a.garageno = t.garageno --注意可能没有入库的而只有出库的产品 delete a from #out_stock a, #in_remain_stock t where a.goodsno = t.goodsno and a.garageno = t.garageno insert #in_remain_stock(goodsno, goodsname, garageno, garagename, unit, quantity) select goodsno, goodsname, garageno, garagename, unit, 0 - quantity from #out_stock --更新仓库名称 update a set a.garagename = b.gad02c from #in_remain_stock a, gam04 b where a.garageno = b.gad01c and b.gad00c = @compid --更新安全存量,更新产品类别, update a set a.amt = isnull(a.quantity, 0) * isnull(b.gfa11f, 0), a.price = isnull(b.gfa11f, 0), a.goodsname = b.gfa03c, a.goodspec = b.gfa12c, a.safetyquan = isnull(b.gfa18f, 0), a.unit = b.gfa07c, a.goodstype = b.gfa05c, a.provider = b.gfa23c, a.stattype = b.gfa24c, a.maxsafequan = isnull(b.gfa29f, 0) from #in_remain_stock a, gfm01 b where a.goodsno = b.gfa01c and b.gfa00c = @compid declare @SP054f float -- 成本计算方式 select @SP054f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP054' --需要修改成本 if @SP054f = 2 begin update a set a.amt_cost = isnull(a.quantity, 0) * isnull(b.gfh03f, 0), a.cost = isnull(b.gfh03f, 0) from #in_remain_stock a, gfm08 b where a.goodsno = b.gfh01c and b.gfh00c = @compid and b.gfh02c = substring(@date, 1, 6) end else begin update a set a.amt_cost = isnull(a.quantity, 0) * isnull(b.gfa22f, 0), a.cost = isnull(b.gfa22f, 0) from #in_remain_stock a, gfm01 b where a.goodsno = b.gfa01c and b.gfa00c = @compid end update a set a.unit = b.gsb03c from #in_remain_stock a, gsm02 b where a.unit = b.gsb02c and b.gsb00c = @compid and b.gsb01c = 'K' --判断是否统计已经停止之产品 declare @include_invalid_good varchar(20) select @include_invalid_good = gsa02c from gsm01 where gsa01c = 'SP113' and gsa00c = @compid if @include_invalid_good = '0' begin --删除已经停止之产品 delete a from #in_remain_stock a, gfm01 b where a.goodsno = b.gfa01c and b.gfa00c = @compid and isnull(gfa10c, '0') = '1' end if @returntype = 1 begin select isnull(goodsno,'') goodsno, isnull(goodsname,'') goodsname, isnull(garageno,'') garageno, isnull(garagename,'') garagename, isnull(goodstype,'') goodstype, isnull(price,0) price, isnull(unit,'') unit, isnull(quantity,0) quantity, isnull(amt,0) amt, isnull(stattype,'') stattype from #in_remain_stock order by goodstype, goodsno end else if @returntype = 2 begin select isnull(goodsno,'') goodsno, isnull(goodsname,'') goodsname, isnull(unit,'') unit, isnull(quantity,0) quantity, isnull(provider,'') provider from #in_remain_stock where quantity < isnull(safetyquan, 0) order by goodsno end else if @returntype = 3 begin select isnull(goodsno,'') goodsno, isnull(goodsname,'') goodsname, isnull(garageno,'') garageno, isnull(garagename,'') garagename, isnull(quantity,0) quantity, isnull(unit,'') unit, isnull(safetyquan, 0) safetyquan, val = (isnull(safetyquan, 0) - isnull(quantity, 0)) from #in_remain_stock where isnull(safetyquan, 0) - isnull(quantity, 0) > 0 order by val desc end else if @returntype = 4 begin select isnull(goodsno,'') goodsno, isnull(goodsname,'') goodsname, isnull(garageno,'') garageno, isnull(garagename,'') garagename, isnull(unit,'') unit, isnull(quantity,0) quantity, isnull(amt,0) amt, isnull(amt_cost,0) amt_cost from #in_remain_stock order by goodstype, goodsno end else if @returntype = 100503 begin select isnull(goodsno,'') goodsno, isnull(goodsname,'') goodsname, isnull(garageno,'') garageno, isnull(garagename,'') garagename, isnull(quantity,0) quantity, isnull(unit,'') unit, isnull(maxsafequan, 0) safetyquan, val = (isnull(quantity, 0) - isnull(maxsafequan, 0)) from #in_remain_stock where (isnull(quantity, 0) - isnull(maxsafequan, 0)) > 0 order by val desc end else if @returntype = 5 or @returntype = 6 begin --Added by WUXM, 2005/11/02 --用于库存统计报表,原用的是@returntype = 1, 先改为返回数量为一个字串 --因别的函数或SP中用了@returntype = 1的,所以只能新增加类型为5 if @returntype = 5 begin delete #in_remain_stock where isnull(quantity, 0) = 0 end if (isnull(@provider, '') = '' or isnull(@provider, '') = '*') --所有供应商的产品 begin if (@sorttype = 0) begin select d.* from ( select isnull(goodsno,'') goodsno, isnull(goodsname,'') goodsname, isnull(garageno,'') garageno, isnull(garagename,'') garagename, isnull(goodstype,'') goodstype, isnull(price,0) price, isnull(cost,0) cost, isnull(unit,'') unit, quantity = '', isnull(amt_cost,0) amt_cost, isnull(amt,0) amt, quan = isnull(quantity,0), isnull(stattype,'') stattype, isnull(goodspec,'') goodspec from #in_remain_stock a where a.provider = @provider union select isnull(gfa01c,'') as goodsno, isnull(gfa03c,'') as goodsname, '' as garageno, '' as garagename, '' as goodstype, 0 as price, isnull(gfa22f,0) as cost, isnull(b.gsb03c,'') as unit, 0 as quantity, 0 as amt_cost, 0 as amt, 0 as quan, --'' as stattype, isnull(gfa24c,'') as stattype, isnull(gfa12c,'') as goodspec from gfm01 c, gsm02 b where c.gfa20c = b.gsb02c and b.gsb00c = @compid and c.gfa00c = @compid and b.gsb01c = 'K' and isnull(c.gfa10c, '1') <> '1' and ((@goodsfrom = '*') or (gfa01c in(select grb03c from #lrm02))) and ((gfa23c= @provider) or @provider='*') and ((@kindfrom = '*') or (gfa24c between @kindfrom and @kindto)) and gfa01c not in(select goodsno from #in_remain_stock)) d --modify by limin -2014-12-11 where ((@garagefrom = '*') or (d.garageno between @garagefrom and @garageto)) order by d.goodsno end else if (@sorttype = 1) begin select d.* from ( select isnull(goodsno,'') goodsno, isnull(goodsname,'') goodsname, isnull(garageno,'') garageno, isnull(garagename,'') garagename, isnull(goodstype,'') goodstype, isnull(price,0) price, isnull(cost,0) cost, isnull(unit,'') unit, quantity = '', isnull(amt_cost,0) amt_cost, isnull(amt,0) amt, quan = isnull(quantity,0), isnull(stattype,'') stattype, isnull(goodspec,'') goodspec from #in_remain_stock a where a.provider = @provider union select isnull(gfa01c,'') as goodsno, isnull(gfa03c,'') as goodsname, '' as garageno, '' as garagename, '' as goodstype, 0 as price, isnull(gfa22f,0) as cost, isnull(b.gsb03c,'') as unit, 0 as quantity, 0 as amt_cost, 0 as amt, 0 as quan, --'' as stattype, isnull(gfa24c,'') as stattype, isnull(gfa12c,'') as goodspec from gfm01 c, gsm02 b where c.gfa20c = b.gsb02c and b.gsb00c = @compid and c.gfa00c = @compid and b.gsb01c = 'K' and isnull(c.gfa10c, '1') <> '1' and ((@goodsfrom = '*') or (gfa01c in(select grb03c from #lrm02))) and ((gfa23c= @provider) or @provider='*') and ((@kindfrom = '*') or (gfa24c between @kindfrom and @kindto)) and gfa01c not in(select goodsno from #in_remain_stock)) d --modify by limin -2014-12-11 where ((@garagefrom = '*') or (d.garageno between @garagefrom and @garageto)) order by d.goodstype end else if (@sorttype = 2) begin select d.* from ( select isnull(goodsno,'') goodsno, isnull(goodsname,'') goodsname, isnull(garageno,'') garageno, isnull(garagename,'') garagename, isnull(goodstype,'') goodstype, isnull(price,0) price, isnull(cost,0) cost, isnull(unit,'') unit, quantity = '', isnull(amt_cost,0) amt_cost, isnull(amt,0) amt, quan = isnull(quantity,0), isnull(stattype,'') stattype, isnull(goodspec,'') goodspec from #in_remain_stock a where a.provider = @provider union select isnull(gfa01c,'') as goodsno, isnull(gfa03c,'') as goodsname, '' as garageno, '' as garagename, '' as goodstype, 0 as price, isnull(gfa22f,0) as cost, isnull(b.gsb03c,'') as unit, 0 as quantity, 0 as amt_cost, 0 as amt, 0 as quan, --'' as stattype, isnull(gfa24c,'') as stattype, isnull(gfa12c,'') as goodspec from gfm01 c, gsm02 b where c.gfa20c = b.gsb02c and b.gsb00c = @compid and c.gfa00c = @compid and b.gsb01c = 'K' and isnull(c.gfa10c, '1') <> '1' and ((@goodsfrom = '*') or (gfa01c in(select grb03c from #lrm02))) and ((gfa23c= @provider) or @provider='*') and ((@kindfrom = '*') or (gfa24c between @kindfrom and @kindto)) and gfa01c not in(select goodsno from #in_remain_stock)) d --modify by limin -2014-12-11 where ((@garagefrom = '*') or (d.garageno between @garagefrom and @garageto)) order by d.provider end end else --指定供应商的产品 begin if (@sorttype = 0) begin select d.* from ( select isnull(goodsno,'') goodsno, isnull(goodsname,'') goodsname, isnull(garageno,'') garageno, isnull(garagename,'') garagename, isnull(goodstype,'') goodstype, isnull(price,0) price, isnull(cost,0) cost, isnull(unit,'') unit, quantity = '', isnull(amt_cost,0) amt_cost, isnull(amt,0) amt, quan = isnull(quantity,0), isnull(stattype,'') stattype, isnull(goodspec,'') goodspec from #in_remain_stock a where a.provider = @provider union select isnull(gfa01c,'') as goodsno, isnull(gfa03c,'') as goodsname, '' as garageno, '' as garagename, '' as goodstype, 0 as price, isnull(gfa22f,0) as cost, isnull(b.gsb03c,'') as unit, 0 as quantity, 0 as amt_cost, 0 as amt, 0 as quan, --'' as stattype, isnull(gfa24c,'') as stattype, isnull(gfa12c,'') as goodspec from gfm01 c, gsm02 b where c.gfa20c = b.gsb02c and b.gsb00c = @compid and c.gfa00c = @compid and b.gsb01c = 'K' and isnull(c.gfa10c, '1') <> '1' and ((@goodsfrom = '*') or (gfa01c in(select grb03c from #lrm02))) and ((gfa23c= @provider) or @provider='*') and ((@kindfrom = '*') or (gfa24c between @kindfrom and @kindto)) and gfa01c not in(select goodsno from #in_remain_stock )) d --modify by limin -2014-12-11 where ((@garagefrom = '*') or (d.garageno between @garagefrom and @garageto)) order by d.goodsno end else if (@sorttype = 1) begin select d.* from ( select isnull(goodsno,'') goodsno, isnull(goodsname,'') goodsname, isnull(garageno,'') garageno, isnull(garagename,'') garagename, isnull(goodstype,'') goodstype, isnull(price,0) price, isnull(cost,0) cost, isnull(unit,'') unit, quantity = '', isnull(amt_cost,0) amt_cost, isnull(amt,0) amt, quan = isnull(quantity,0), isnull(stattype,'') stattype, isnull(goodspec,'') goodspec from #in_remain_stock a where a.provider = @provider union select isnull(gfa01c,'') as goodsno, isnull(gfa03c,'') as goodsname, '' as garageno, '' as garagename, '' as goodstype, 0 as price, isnull(gfa22f,0) as cost, isnull(b.gsb03c,'') as unit, 0 as quantity, 0 as amt_cost, 0 as amt, 0 as quan, --'' as stattype, isnull(gfa24c,'') as stattype, isnull(gfa12c,'') as goodspec from gfm01 c, gsm02 b where c.gfa20c = b.gsb02c and b.gsb00c = @compid and c.gfa00c = @compid and b.gsb01c = 'K' and isnull(c.gfa10c, '1') <> '1' and ((@goodsfrom = '*') or (gfa01c in(select grb03c from #lrm02))) and ((gfa23c= @provider) or @provider='*') and ((@kindfrom = '*') or (gfa24c between @kindfrom and @kindto)) and gfa01c not in(select goodsno from #in_remain_stock )) d --modify by limin -2014-12-11 where ((@garagefrom = '*') or (d.garageno between @garagefrom and @garageto)) order by d.goodstype end else if (@sorttype = 2) begin select d.* from ( select isnull(goodsno,'') goodsno, isnull(goodsname,'') goodsname, isnull(garageno,'') garageno, isnull(garagename,'') garagename, isnull(goodstype,'') goodstype, isnull(price,0) price, isnull(cost,0) cost, isnull(unit,'') unit, quantity = '', isnull(amt_cost,0) amt_cost, isnull(amt,0) amt, quan = isnull(quantity,0), isnull(stattype,'') stattype, isnull(goodspec,'') goodspec from #in_remain_stock a where a.provider = @provider union select isnull(gfa01c,'') as goodsno, isnull(gfa03c,'') as goodsname, '' as garageno, '' as garagename, '' as goodstype, 0 as price, isnull(gfa22f,0) as cost, isnull(b.gsb03c,'') as unit, 0 as quantity, 0 as amt_cost, 0 as amt, 0 as quan, --'' as stattype, isnull(gfa24c,'') as stattype, isnull(gfa12c,'') as goodspec from gfm01 c, gsm02 b where c.gfa20c = b.gsb02c and b.gsb00c = @compid and c.gfa00c = @compid and b.gsb01c = 'K' and isnull(c.gfa10c, '1') <> '1' and ((@goodsfrom = '*') or (gfa01c in(select grb03c from #lrm02))) and ((gfa23c= @provider) or @provider='*') and ((@kindfrom = '*') or (gfa24c between @kindfrom and @kindto)) and gfa01c not in(select goodsno from #in_remain_stock)) d --modify by limin -2014-12-11 where ((@garagefrom = '*') or (d.garageno between @garagefrom and @garageto)) order by d.provider end end end drop table #in_remain_stock drop table #out_stock drop table #lrm02 end GO /****** Object: StoredProcedure [dbo].[upg_order_sales_allcomp_upgrade] Script Date: 06/12/2017 06:20:25 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_order_sales_allcomp_upgrade] ( @compid varchar(10), -- 公司代码 @fromdate varchar(10), -- 开始日期 @todate varchar(10), -- 截至日期 @orderkind int, -- 1 项目 2物品 3疗程 4卡销售 5 会员卡升级 6销售 + 充值 7所有 @orderby int, -- 0 按金额排序 1 按数量排序 @amttype int -- 0 所有 1现金 2银行卡 3只包括银行卡 + 现金 ) as begin create table #tbl_order ( seq int not null identity, kind int default 5 null, oldcode varchar(20) not null, -- 代码 newcode varchar(20) not null, -- 新的卡类别代码 code varchar(60) null, -- 描述 nname varchar(30) null, -- 名称 num float null, -- 数量 amt float null, -- 金额 ) declare @PK_Sql nvarchar(max) set @PK_Sql = ' alter table #tbl_order add constraint PK_#tbl_order_' + convert(varchar(10), cast(floor(rand() * 1000000) as int)) + ' PRIMARY KEY(seq)' exec sp_executesql @PK_Sql create table #gmm01_buf -- 日结表 -- 项目/产品 ( gma00c varchar(10) not null, --公司编号 gma01d varchar(8) not null, --日期 gma02c varchar(20) not null, --旧的卡类别 gma03c varchar(20) not null, --新的卡类别代码 gma04c varchar(5) not null, --支付方式 gma05f float null, --数量 gma06f float null, --金额 ) set @PK_Sql = ' alter table #gmm01_buf add constraint PK_#gmm01_buf_' + convert(varchar(10), cast(floor(rand() * 1000000) as int)) + ' primary key clustered(gma00c, gma01d, gma02c, gma03c, gma04c)' exec sp_executesql @PK_Sql create table #gmm01_buf_inter -- 日结表 -- 项目/产品 ( gma00c varchar(10) not null, --公司别 gma01d varchar(10) not null, --日期 seq int identity not null, --序号 gma02c varchar(20) not null, --旧的卡类别 gma03c varchar(20) not null, --新的卡类别代码 gma04c varchar(5) not null, --支付方式 gma05f float null, --数量 gma06f float null, --金额 ) set @PK_Sql = ' alter table #gmm01_buf_inter add constraint PK_#gmm01_buf_inter_' + convert(varchar(10), cast(floor(rand() * 1000000) as int)) + ' primary key clustered(seq)' exec sp_executesql @PK_Sql declare @custverflag varchar(20) select @custverflag = gsa02c from gsm01 where gsa00c = @compid and gsa01c = 'SP109' if @custverflag = 'FANGZI' begin insert #gmm01_buf_inter(gma00c, gma01d, gma02c, gma03c, gma04c, gma05f, gma06f) select gcl00c, gcl80d, isnull(gcl52c, ''), isnull(gcl15c, ''), isnull(gcl67c, ''), sum(gcl68f/gcl65f), sum(gcl68f) from gcm10 a with (nolock), gam26 z where a.gcl00c = z.gaz02c and z.gaz01c = @compid and a.gcl80d >= @fromdate and a.gcl80d <= @todate and isnull(gcl67c, '')<>'' and isnull(gcl14i, 1) = 1 and isnull(gcl65f, 0)>0 and isnull(gcl68f, 0)>0 group by gcl00c, gcl80d, isnull(gcl52c, ''), isnull(gcl15c, ''), isnull(gcl67c, '') insert #gmm01_buf_inter(gma00c, gma01d, gma02c, gma03c, gma04c, gma05f, gma06f) select gcl00c, gcl80d, isnull(gcl52c, ''), isnull(gcl15c, ''), isnull(gcl69c, ''), sum(gcl70f/gcl65f), sum(gcl70f) from gcm10 a with (nolock), gam26 z where a.gcl00c = z.gaz02c and z.gaz01c = @compid and a.gcl80d >= @fromdate and a.gcl80d <= @todate and isnull(gcl69c, '')<>'' and isnull(gcl14i, 1) = 1 and isnull(gcl65f, 0)>0 and isnull(gcl70f, 0)>0 group by gcl00c, gcl80d, isnull(gcl52c, ''), isnull(gcl15c, ''), isnull(gcl69c, '') --第三支付方式 insert #gmm01_buf_inter(gma00c, gma01d, gma02c, gma03c, gma04c, gma05f, gma06f) select gcl00c, gcl80d, isnull(gcl52c, ''), isnull(gcl15c, ''), isnull(gcl71c, ''), sum(gcl72f/gcl65f), sum(gcl72f) from gcm10 a with (nolock), gam26 z where a.gcl00c = z.gaz02c and z.gaz01c = @compid and a.gcl80d >= @fromdate and a.gcl80d <= @todate and isnull(gcl71c, '')<>'' and isnull(gcl14i, 1) = 1 and isnull(gcl65f, 0)>0 and isnull(gcl72f, 0)>0 group by gcl00c, gcl80d, isnull(gcl52c, ''), isnull(gcl15c, ''), isnull(gcl71c, '') --第四支付方式 insert #gmm01_buf_inter(gma00c, gma01d, gma02c, gma03c, gma04c, gma05f, gma06f) select gcl00c, gcl80d, isnull(gcl52c, ''), isnull(gcl15c, ''), isnull(gcl73c, ''), sum(gcl74f/gcl65f), sum(gcl74f) from gcm10 a with (nolock), gam26 z where a.gcl00c = z.gaz02c and z.gaz01c = @compid and a.gcl80d >= @fromdate and a.gcl80d <= @todate and isnull(gcl73c, '')<>'' and isnull(gcl14i, 1) = 1 and isnull(gcl65f, 0)>0 and isnull(gcl74f, 0)>0 group by gcl00c, gcl80d, isnull(gcl52c, ''), isnull(gcl15c, ''), isnull(gcl73c, '') end else begin insert #gmm01_buf_inter(gma00c, gma01d, gma02c, gma03c, gma04c, gma05f, gma06f) select gcl00c, gcl80d, isnull(gcl52c, ''), isnull(gcl15c, ''), isnull(gcl67c, ''), sum(gcl68f/gcl65f), sum(gcl68f) from gcm10 a with (nolock), gam26 z where a.gcl00c = z.gaz02c and z.gaz01c = @compid and a.gcl80d >= @fromdate and a.gcl80d <= @todate and isnull(gcl67c, '')<>'' and isnull(gcl65f, 0)>0 and isnull(gcl68f, 0)>0 group by gcl00c, gcl80d, isnull(gcl52c, ''), isnull(gcl15c, ''), isnull(gcl67c, '') insert #gmm01_buf_inter(gma00c, gma01d, gma02c, gma03c, gma04c, gma05f, gma06f) select gcl00c, gcl80d, isnull(gcl52c, ''), isnull(gcl15c, ''), isnull(gcl69c, ''), sum(gcl70f/gcl65f), sum(gcl70f) from gcm10 a with (nolock), gam26 z where a.gcl00c = z.gaz02c and z.gaz01c = @compid and a.gcl80d >= @fromdate and a.gcl80d <= @todate and isnull(gcl69c, '')<>'' and isnull(gcl65f, 0)>0 and isnull(gcl70f, 0)>0 group by gcl00c, gcl80d, isnull(gcl52c, ''), isnull(gcl15c, ''), isnull(gcl69c, '') --第三支付方式 insert #gmm01_buf_inter(gma00c, gma01d, gma02c, gma03c, gma04c, gma05f, gma06f) select gcl00c, gcl80d, isnull(gcl52c, ''), isnull(gcl15c, ''), isnull(gcl71c, ''), sum(gcl72f/gcl65f), sum(gcl72f) from gcm10 a with (nolock), gam26 z where a.gcl00c = z.gaz02c and z.gaz01c = @compid and a.gcl80d >= @fromdate and a.gcl80d <= @todate and isnull(gcl71c, '')<>'' and isnull(gcl65f, 0)>0 and isnull(gcl72f, 0)>0 group by gcl00c, gcl80d, isnull(gcl52c, ''), isnull(gcl15c, ''), isnull(gcl71c, '') --第四支付方式 insert #gmm01_buf_inter(gma00c, gma01d, gma02c, gma03c, gma04c, gma05f, gma06f) select gcl00c, gcl80d, isnull(gcl52c, ''), isnull(gcl15c, ''), isnull(gcl73c, ''), sum(gcl74f/gcl65f), sum(gcl74f) from gcm10 a with (nolock), gam26 z where a.gcl00c = z.gaz02c and z.gaz01c = @compid and a.gcl80d >= @fromdate and a.gcl80d <= @todate and isnull(gcl73c, '')<>'' and isnull(gcl65f, 0)>0 and isnull(gcl74f, 0)>0 group by gcl00c, gcl80d, isnull(gcl52c, ''), isnull(gcl15c, ''), isnull(gcl73c, '') end insert #gmm01_buf(gma00c, gma01d, gma02c, gma03c, gma04c, gma05f, gma06f) select gma00c, gma01d, gma02c, gma03c, gma04c, sum(gma05f), sum(gma06f) from #gmm01_buf_inter group by gma00c, gma01d, gma02c, gma03c, gma04c delete #gmm01_buf where isnull(gma04c, '') = '' or (isnull(gma05f, 0) = 0 and isnull(gma06f, 0) = 0) drop table #gmm01_buf_inter if (@orderkind = 5) begin if @amttype = 0 begin insert #tbl_order(oldcode, newcode, nname, num, amt) select gma02c, gma03c, '', sum(gma05f), sum(gma06f) from #gmm01_buf a, gam26 z where a.gma00c = z.gaz02c and z.gaz01c = @compid and a.gma01d >= @fromdate and a.gma01d <= @todate and isnull(a.gma04c, '')<>'' group by gma02c, gma03c end else if @amttype = 1 begin insert #tbl_order(oldcode, newcode, nname, num, amt) select gma02c, gma03c, nname = '', sum(gma05f), sum(gma06f) from #gmm01_buf a, gam26 z where a.gma00c = z.gaz02c and z.gaz01c = @compid and a.gma01d >= @fromdate and a.gma01d <= @todate and a.gma04c = '1' group by gma02c, gma03c end else if @amttype = 2 begin insert #tbl_order(oldcode, newcode, nname, num, amt) select gma02c, gma03c, nname = '', sum(gma05f), sum(gma06f) from #gmm01_buf a, gam26 z where a.gma00c = z.gaz02c and z.gaz01c = @compid and a.gma01d >= @fromdate and a.gma01d <= @todate and a.gma04c = '6' group by gma02c, gma03c end else if @amttype = 3 begin insert #tbl_order(oldcode, newcode, nname, num, amt) select gma02c, gma03c, nname = '', sum(gma05f), sum(gma06f) from #gmm01_buf a, gam26 z where a.gma00c = z.gaz02c and z.gaz01c = @compid and a.gma01d >= @fromdate and a.gma01d <= @todate and (a.gma04c = '1' or a.gma04c = '6') group by gma02c, gma03c end else if @amttype = 4 begin insert #tbl_order(oldcode, newcode, nname, num, amt) select gma02c, gma03c, nname = '', sum(gma05f), sum(gma06f) from #gmm01_buf a, gam26 z where a.gma00c = z.gaz02c and z.gaz01c = @compid and a.gma01d >= @fromdate and a.gma01d <= @todate and (a.gma04c = '1' or a.gma04c = '6' or a.gma04c = '4') group by gma02c, gma03c end end --更新tbl_order中的名称 update a set a.code = isnull(a.oldcode, '') + '->' + isnull(a.newcode, ''), a.nname = isnull(b.gak02c, '') + '->' + isnull(c.gak02c, '') from #tbl_order a, gam10 b, gam10 c where b.gak00c = @compid and a.oldcode *= b.gak01c and c.gak00c = @compid and a.newcode = c.gak01c and a.newcode <> '' drop table #gmm01_buf if @orderby = 0 select seq, isnull(kind,0) kind,isnull(oldcode,'') oldcode,isnull(newcode,'') newcode,isnull(code,'') code, isnull(nname,'') nname,isnull(num,0) num,isnull(amt,0) amt from #tbl_order order by amt desc, num desc else select seq, isnull(kind,0) kind,isnull(oldcode,'') oldcode,isnull(newcode,'') newcode,isnull(code,'') code, isnull(nname,'') nname,isnull(num,0) num,isnull(amt,0) amt from #tbl_order order by num desc, amt desc drop table #tbl_order end GO /****** Object: StoredProcedure [dbo].[upg_data_recover_sp064_sp065_2] Script Date: 06/12/2017 06:19:55 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_data_recover_sp064_sp065_2] as begin -- 员工的归属门店 if not exists (select 1 from data_recover where rdd00c = '001' and rdd01c = 'gsm01' and rdd02c = 'sp064_sp065_2') begin declare @sp064_2 varchar(160) select @sp064_2 = gsa02c from gsm01 where gsa00c = '001' and gsa01c = 'SP064' declare @sp065_2 varchar(160) select @sp065_2 = gsa02c from gsm01 where gsa00c = '001' and gsa01c = 'SP065' if @sp064_2 is not null or @sp065_2 is not null begin -- 创建临时表 create table #data_recover_comp ( compId varchar(10) not null ) insert into #data_recover_comp (compId) select gae01c from gam05 with (nolock) where gae01c <> '001' -- 创建循环变量 declare @HasRecord int set @HasRecord = 1 declare @currentCompId varchar(10) while(@HasRecord = 1) begin if exists ( select 1 from #data_recover_comp) begin set @HasRecord = 1 select top 1 @currentCompId = compId from #data_recover_comp update gsm01 set gsa02c = @sp064_2 where gsa00c = @currentCompId and gsa01c = 'SP064' and gsa02c is null update gsm01 set gsa02c = @sp065_2 where gsa00c = @currentCompId and gsa01c = 'SP065' and gsa02c is null -- 清除已处理的门店 delete #data_recover_comp where compId = @currentCompId end else begin set @HasRecord = 0 end end drop table #data_recover_comp end insert into data_recover(rdd00c, rdd01c, rdd02c) values ('001', 'gsm01', 'sp064_sp065_2') end end GO /****** Object: StoredProcedure [dbo].[upg_prepare_work_detail] Script Date: 06/12/2017 06:20:27 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_prepare_work_detail] ( @spid int, -- 连接号 @compid varchar(10), -- 公司别 @fromdate varchar(10), -- 开始日期 @todate varchar(10), -- 截至日期 @fromempl varchar(20), -- 查询开始人员 @toempl varchar(20), -- 查询截至人员 @inc_card int, -- 卡销售 @inc_service int, -- 服务 @inc_goods int, -- 是否包括产品销售 @return_type int ) -- return 数据的类型, 1-明细数据员工业绩统计报表 -- 2-每人一个提成数据, 用于计算员工薪资 -- 3-业绩排行-虚业绩, 4-业绩排行-营业金额 -- 5-业绩排行-实业绩 -- 7-每人按项目分组 6-总提成 as begin if ((@fromempl = '' and @toempl = '')) or @fromempl = '*' begin select @fromempl = min(haa01c) from ham01 with (nolock) where haa00c = @compid select @toempl = max(haa01c) from ham01 with (nolock) where haa00c = @compid end declare @sp048 varchar(10) select @sp048 = gsa02c from gsm01 with (nolock) where gsa00c = @compid and gsa01c = 'SP048' -- 卡的与业绩相关的纪录 if (@inc_card = 1) begin create table #gnm02_buf -- 会员卡销售--相关会员卡 ( gnb00c varchar(10) not null, --公司编号 gnb01c varchar(20) not null, --销售单号 gnb02c varchar(5) null, --卡种 gnb03c varchar(20) not null, --会员卡号 gnb04i int null, --折让类别(分为 1- 一般折让, 2-续卡优惠价, 3-内部优惠价) gnb05f float null, --标准金额 gnb06f float null, --折让 gnb07f float null, --折让金额 gnb08f float null, --卡销售金额(包括疗程套餐的销售金额) gnb09i int null, --是否已经客户提佣 gnb10i int null, --是否已经员工提佣 gnb11f float null, --储值金额 , added by WUXM, 2005/10/28 gnb12f float null, --默认储值金额 gnb13i int null, --账户 gna20c varchar(10) null, --负责业务 gna26f float null, --实收总额 gna27f float null, --第一分享比率 gna28c varchar(10) null, --第二卖卡人 gna29f float null, --分享比率 gna30c varchar(10) null, --第三卖卡人 gna31f float null, --分享比率 gna32c varchar(10) null, --第4卖卡人 gna33f float null, --分享比率 gna34c varchar(10) null, --第5卖卡人 gna35f float null, --分享比率 gna36c varchar(10) null, --第6卖卡人 gna37f float null, --分享比率 gna38c varchar(10) null, --第7卖卡人 gna39f float null, --分享比率 gna40c varchar(10) null, --第8卖卡人 gna41f float null, --分享比率 gna42c varchar(10) null, --第9卖卡人 gna43f float null, --分享比率 gna44c varchar(10) null, --第10卖卡人 gna45f float null, --分享比率 gna46c varchar(10) null, --第11卖卡人 gna47f float null, --分享比率 gna48c varchar(10) null, --第12卖卡人 gna49f float null, --分享比率 gna50c varchar(10) null, --第13卖卡人 gna51f float null, --分享比率 gna52c varchar(10) null, --第14卖卡人 gna53f float null, --分享比率 gna54c varchar(10) null, --第15卖卡人 gna55f float null, --分享比率 gna56c varchar(10) null, --第16卖卡人 gna57f float null, --分享比率 gna58c varchar(10) null, --第17卖卡人 gna59f float null, --分享比率 gna60c varchar(10) null, --第18卖卡人 gna61f float null, --分享比率 gna62c varchar(10) null, --第19卖卡人 gna63f float null, --分享比率 gna64c varchar(10) null, --第20卖卡人 gna65f float null, --分享比率 gna80d varchar(8) null, --帐务日期, added by WUXM, 2005/08/16 gna67f float null, --第一销售虚业绩 Added by WUXM 2005/11/04 gna68f float null, --第一销售提成 Added by WUXM 2005/11/04 gna69f float null, --第二销售虚业绩 Added by WUXM 2005/11/04 gna70f float null, --第二销售提成 Added by WUXM 2005/11/04 gna71f float null, --第三销售虚业绩 Added by WUXM 2005/11/04 gna72f float null, --第三销售提成 Added by WUXM 2005/11/04 gsc04c varchar(20) null, --支付方式 gsc05f float null, --支付金额 gba14i int null, --性别 gna86f float null, --第四销售虚业绩 Added by maql 20100426 gna87f float null, --第五人员业绩 Added by maql 20100426 gna88f float null, --第六人员业绩 Added by maql 20100426 gna89f float null, --第七人员业绩 Added by maql 20100426 gna90f float null, --第八人员业绩 Added by maql 20100426 gna91f float null, --第九人员业绩 Added by maql 20100426 gna92f float null, --第十人员业绩 Added by maql 20100426 flag int null, gsc00c varchar(10) null, --公司编号 gsc01c varchar(20) null, --单据编号 gsc02c varchar(5) null, --单据类别 gsc03f float null, --序号 bill_total_amt float null, ) create clustered index gnm02_buf_idx_01 on #gnm02_buf(gna20c) create nonclustered index gnm02_buf_idx_02 on #gnm02_buf(gna28c) create nonclustered index gnm02_buf_idx_03 on #gnm02_buf(gna30c) create nonclustered index gnm02_buf_idx_04 on #gnm02_buf(gnb00c, gnb02c) insert #gnm02_buf(gnb00c, gnb01c, gnb02c, gnb03c, gnb04i, gnb05f, gnb06f, gnb07f, gnb08f, gnb09i, gnb10i, gnb11f, gnb12f, gnb13i, gna20c, gna26f, gna27f, gna28c, gna29f, gna30c, gna31f, gna32c, gna33f, gna34c, gna35f, gna36c, gna37f, gna38c, gna39f, gna40c, gna41f, gna42c, gna43f, gna44c, gna45f, gna46c, gna47f, gna48c, gna49f, gna50c, gna51f, gna52c, gna53f, gna54c, gna55f, gna56c, gna57f, gna58c, gna59f, gna60c, gna61f, gna62c, gna63f, gna64c, gna65f, gna80d, gna67f, gna68f, gna69f, gna70f, gna71f, gna72f, gsc04c, gsc05f, gba14i, gna86f, gna87f, gna88f, gna89f, gna90f, gna91f, gna92f, gsc00c, gsc01c, gsc02c, gsc03f) select gnb00c, gnb01c, gnb02c, gnb03c, gnb04i, gnb05f, gnb06f, gnb07f, gnb08f, gnb09i, gnb10i, gnb11f, gnb12f, gnb13i, upper(gna20c), gna26f, gna27f, upper(gna28c), gna29f, upper(gna30c), gna31f, gna32c, gna33f, gna34c, gna35f, gna36c, gna37f, gna38c, gna39f, gna40c, gna41f, gna42c, gna43f, gna44c, gna45f, gna46c, gna47f, gna48c, gna49f, gna50c, gna51f, gna52c, gna53f, gna54c, gna55f, gna56c, gna57f, gna58c, gna59f, gna60c, gna61f, gna62c, gna63f, gna64c, gna65f, gna80d, gna67f, gna68f, gna69f, gna70f, gna71f, gna72f, gsc04c, gsc05f, 0, gna86f, gna87f, gna88f, gna89f, gna90f, gna91f, gna92f, gsc00c, gsc01c, gsc02c, gsc03f from gnm01 c with (nolock), gnm02 a with (nolock), gsm03 b with (nolock) where a.gnb00c = @compid and c.gna80d >= @fromdate and c.gna80d <= @todate and c.gna00c = a.gnb00c and c.gna01c = a.gnb01c and c.gna00c = b.gsc00c and c.gna01c = b.gsc01c and b.gsc02c = 'gn' create table #tbl_bill_total ( compid varchar(20) not null, billid varchar(20) not null, amt float null, primary key(compid, billid) ) insert #tbl_bill_total(compid, billid, amt) select gnb00c, gnb01c, sum(gnb08f) from gnm01 c with (nolock), gnm02 a with (nolock) where a.gnb00c = @compid and c.gna80d >= @fromdate and c.gna80d <= @todate and c.gna00c = a.gnb00c and c.gna01c = a.gnb01c group by gnb00c, gnb01c update a set a.bill_total_amt = b.amt from #gnm02_buf a, #tbl_bill_total b where a.gnb00c = b.compid and a.gnb01c = b.billid update a set a.bill_total_amt = 1 from #gnm02_buf a where isnull(a.bill_total_amt,0) = 0 drop table #tbl_bill_total update a set a.gba14i = b.gba14i from #gnm02_buf a, gbm01 b with (nolock) where a.gnb00c = b.gba00c and a.gnb03c = b.gba23c -- 卖卡第一销售人员 insert #work_detail_ceshi(spid, compid, person_id, action_id, srvdate, code, name, amt, quan, cost, stdprice, share_rate, prj_type, billid, gender, paycode, amt2) select @spid, @compid, gna20c, 1, gna80d, gnb03c, gak02c, case when bill_total_amt = 0 then 0 else (gnb08f / bill_total_amt) * gsc05f end, gsc05f / bill_total_amt, gak19f, gnb05f, isnull(gna27f, 1), gnb02c, gnb01c, gba14i, gsc04c, (case when bill_total_amt = 0 or gna26f = 0 then 0 else (gnb08f / bill_total_amt) * gna67f * gsc05f / gna26f end) from #gnm02_buf a, gam10 b with (nolock) where a.gnb00c = b.gak00c and a.gnb02c = b.gak01c and a.gna20c >= @fromempl and a.gna20c <= @toempl --and isnull(a.gna26f, 0) <> 0 --and isnull(a.bill_total_amt, 0) <> 0 order by gna20c, gna80d -- 卖卡第二销售人员 insert #work_detail_ceshi(spid, compid, person_id, action_id, srvdate, code, name, amt, quan, cost, stdprice, share_rate, prj_type, billid, gender, paycode, amt2) select @spid, @compid, gna28c, 1, gna80d, gnb03c, gak02c, case when bill_total_amt = 0 then 0 else (gnb08f / bill_total_amt) * gsc05f end, gsc05f / bill_total_amt, gak19f, gnb05f, gna29f, gnb02c, gnb01c, gba14i, gsc04c, (case when bill_total_amt = 0 or gna26f = 0 then 0 else (gnb08f / bill_total_amt) * gna69f * gsc05f / gna26f end) from #gnm02_buf a, gam10 b with (nolock) where a.gnb00c = b.gak00c and a.gnb02c = b.gak01c and a.gna28c >= @fromempl and a.gna28c <= @toempl --and isnull(a.gna26f, 0) <> 0 --and isnull(a.bill_total_amt, 0) <> 0 order by gna28c, gna80d -- 卖卡第三销售人员 insert #work_detail_ceshi(spid, compid, person_id, action_id, srvdate, code, name, amt, quan, cost, stdprice, share_rate, prj_type, billid, gender, paycode, amt2) select @spid, @compid, gna30c, 1, gna80d, gnb03c, gak02c, case when bill_total_amt = 0 then 0 else (gnb08f / bill_total_amt) * gsc05f end, gsc05f / bill_total_amt, gak19f, gnb05f, gna31f, gnb02c, gnb01c, gba14i, gsc04c, (case when bill_total_amt = 0 or gna26f = 0 then 0 else (gnb08f / bill_total_amt) * gna71f * gsc05f / gna26f end) from #gnm02_buf a, gam10 b with (nolock) where a.gnb00c = b.gak00c and a.gnb02c = b.gak01c and a.gna30c >= @fromempl and a.gna30c <= @toempl --and isnull(a.gna26f, 0) <> 0 --and isnull(a.bill_total_amt, 0) <> 0 order by gna30c, gna80d -- 卖卡第4销售人员 insert #work_detail_ceshi(spid, compid, person_id, action_id, srvdate, code, name, amt, quan, cost, stdprice, share_rate, prj_type, billid, gender, paycode, amt2) select @spid, @compid, gna32c, 1, gna80d, gnb03c, gak02c, case when bill_total_amt = 0 then 0 else (gnb08f / bill_total_amt) * gsc05f end, gsc05f / bill_total_amt, gak19f, gnb05f, gna33f, gnb02c, gnb01c, gba14i, gsc04c, (case when bill_total_amt = 0 or gna26f = 0 then 0 else (gnb08f / bill_total_amt) * gna86f * gsc05f / gna26f end) from #gnm02_buf a, gam10 b with (nolock) where a.gnb00c = b.gak00c and a.gnb02c = b.gak01c and a.gnb00c = @compid and a.gna32c >= @fromempl and a.gna32c <= @toempl --and isnull(a.gna26f, 0) <> 0 --and isnull(a.bill_total_amt, 0) <> 0 order by gna32c, gna80d -- 卖卡第5销售人员 insert #work_detail_ceshi(spid, compid, person_id, action_id, srvdate, code, name, amt, quan, cost, stdprice, share_rate, prj_type, billid, gender, paycode, amt2) select @spid, @compid, gna34c, 1, gna80d, gnb03c, gak02c, case when bill_total_amt = 0 then 0 else (gnb08f / bill_total_amt) * gsc05f end, gsc05f / bill_total_amt, gak19f, gnb05f, gna35f, gnb02c, gnb01c, gba14i, gsc04c, (case when bill_total_amt = 0 or gna26f = 0 then 0 else (gnb08f / bill_total_amt) * gna87f * gsc05f / gna26f end) from #gnm02_buf a, gam10 b with (nolock) where a.gnb00c = b.gak00c and a.gnb02c = b.gak01c and a.gna34c >= @fromempl and a.gna34c <= @toempl --and isnull(a.gna26f, 0) <> 0 --and isnull(a.bill_total_amt, 0) <> 0 order by gna34c, gna80d -- 卖卡第6销售人员 insert #work_detail_ceshi(spid, compid, person_id, action_id, srvdate, code, name, amt, quan, cost, stdprice, share_rate, prj_type, billid, gender, paycode, amt2) select @spid, @compid, gna36c, 1, gna80d, gnb03c, gak02c, case when bill_total_amt = 0 then 0 else (gnb08f / bill_total_amt) * gsc05f end, gsc05f / bill_total_amt, gak19f, gnb05f, gna37f, gnb02c, gnb01c, gba14i, gsc04c, (case when bill_total_amt = 0 or gna26f = 0 then 0 else (gnb08f / bill_total_amt) * gna88f * gsc05f / gna26f end) from #gnm02_buf a, gam10 b with (nolock) where a.gnb00c = b.gak00c and a.gnb02c = b.gak01c and a.gna36c >= @fromempl and a.gna36c <= @toempl --and isnull(a.gna26f, 0) <> 0 --and isnull(a.bill_total_amt, 0) <> 0 order by gna36c, gna80d -- 卖卡第7销售人员 insert #work_detail_ceshi(spid, compid, person_id, action_id, srvdate, code, name, amt, quan, cost, stdprice, share_rate, prj_type, billid, gender, paycode, amt2) select @spid, @compid, gna38c, 1, gna80d, gnb03c, gak02c, case when bill_total_amt = 0 then 0 else (gnb08f / bill_total_amt) * gsc05f end, gsc05f / bill_total_amt, gak19f, gnb05f, gna39f, gnb02c, gnb01c, gba14i, gsc04c, (case when bill_total_amt = 0 or gna26f = 0 then 0 else (gnb08f / bill_total_amt) * gna89f * gsc05f / gna26f end) from #gnm02_buf a, gam10 b with (nolock) where a.gnb00c = b.gak00c and a.gnb02c = b.gak01c and a.gna38c >= @fromempl and a.gna38c <= @toempl --and isnull(a.gna26f, 0) <> 0 --and isnull(a.bill_total_amt, 0) <> 0 order by gna38c, gna80d -- 卖卡第8销售人员 insert #work_detail_ceshi(spid, compid, person_id, action_id, srvdate, code, name, amt, quan, cost, stdprice, share_rate, prj_type, billid, gender, paycode, amt2) select @spid, @compid, gna40c, 1, gna80d, gnb03c, gak02c, case when bill_total_amt = 0 then 0 else (gnb08f / bill_total_amt) * gsc05f end, gsc05f / bill_total_amt, gak19f, gnb05f, gna41f, gnb02c, gnb01c, gba14i, gsc04c, (case when bill_total_amt = 0 or gna26f = 0 then 0 else (gnb08f / bill_total_amt) * gna90f * gsc05f / gna26f end) from #gnm02_buf a, gam10 b with (nolock) where a.gnb00c = b.gak00c and a.gnb02c = b.gak01c and a.gna40c >= @fromempl and a.gna40c <= @toempl --and isnull(a.gna26f, 0) <> 0 --and isnull(a.bill_total_amt, 0) <> 0 order by gna40c, gna80d -- 卖卡第9销售人员 insert #work_detail_ceshi(spid, compid, person_id, action_id, srvdate, code, name, amt, quan, cost, stdprice, share_rate, prj_type, billid, gender, paycode, amt2) select @spid, @compid, gna42c, 1, gna80d, gnb03c, gak02c, case when bill_total_amt = 0 then 0 else (gnb08f / bill_total_amt) * gsc05f end, gsc05f / bill_total_amt, gak19f, gnb05f, gna43f, gnb02c, gnb01c, gba14i, gsc04c, (case when bill_total_amt = 0 or gna26f = 0 then 0 else (gnb08f / bill_total_amt) * gna91f * gsc05f / gna26f end) from #gnm02_buf a, gam10 b with (nolock) where a.gnb00c = b.gak00c and a.gnb02c = b.gak01c and a.gna42c >= @fromempl and a.gna42c <= @toempl --and isnull(a.gna26f, 0) <> 0 --and isnull(a.bill_total_amt, 0) <> 0 order by gna42c, gna80d -- 卖卡第10销售人员 insert #work_detail_ceshi(spid, compid, person_id, action_id, srvdate, code, name, amt, quan, cost, stdprice, share_rate, prj_type, billid, gender, paycode, amt2) select @spid, @compid, gna44c, 1, gna80d, gnb03c, gak02c, case when bill_total_amt = 0 then 0 else (gnb08f / bill_total_amt) * gsc05f end, gsc05f / bill_total_amt, gak19f, gnb05f, gna45f, gnb02c, gnb01c, gba14i, gsc04c, (case when bill_total_amt = 0 or gna26f = 0 then 0 else (gnb08f / bill_total_amt) * gna92f * gsc05f / gna26f end) from #gnm02_buf a, gam10 b with (nolock) where a.gnb00c = b.gak00c and a.gnb02c = b.gak01c and a.gna44c >= @fromempl and a.gna44c <= @toempl --and isnull(a.gna26f, 0) <> 0 --and isnull(a.bill_total_amt, 0) <> 0 order by gna44c, gna80d drop table #gnm02_buf -- 卡充值 create table #gcm10_buf -- 帐户异动单 ( gcl00c varchar(10) not null, --公司编号 gcl01c varchar(20) not null, --异动单号 gcl02d varchar(8) not null, --异动日期 gcl03c varchar(20) null, --会员卡号 gcl04c varchar(5) null, --帐号类别(1.电子钱包 2.储值帐户) gcl05c varchar(20) null, --帐号(暂不用) gcl06i int null, --异动类别( 0 - 充值, 1 - 取款) gcl07f float null, --异动金额(总收款金额), 含套餐,含疗程 gcl08f float null, --异动前余额 gcl09c varchar(20) null, --销售人员 gcl10c varchar(10) null, --支付方式 gcl11c varchar(2) null, --单据类别, 此账户异动单来自哪里 gcl12c varchar(20) null, --单号, Added by WUXM, 2004/04/10 gcl13f float null, --欠款金额 gcl15c varchar(5) null, --新的会员卡类别 gcl16f float null, --第一销售分享比率 gcl17c varchar(20) null, --第二销售 gcl18f float null, --分享比率 gcl19c varchar(20) null, --第三负责人 gcl20f float null, --分享比率 gcl21d varchar(8) null, --新截至日期 gcl22c varchar(20) null, --第4负责人 gcl23f float null, --分享比率 gcl24c varchar(20) null, --第5负责人 gcl25f float null, --分享比率 gcl26c varchar(20) null, --第6负责人 gcl27f float null, --分享比率 gcl28c varchar(20) null, --第7负责人 gcl29f float null, --分享比率 gcl30c varchar(20) null, --第8负责人 gcl31f float null, --分享比率 gcl32c varchar(20) null, --第9负责人 gcl33f float null, --分享比率 gcl34c varchar(20) null, --第10负责人 gcl35f float null, --分享比率 gcl36c varchar(20) null, --第11负责人 gcl37f float null, --分享比率 gcl38c varchar(20) null, --第12负责人 gcl39f float null, --分享比率 gcl40c varchar(20) null, --第13负责人 gcl41f float null, --分享比率 gcl42c varchar(20) null, --第14负责人 gcl43f float null, --分享比率 gcl44c varchar(20) null, --第15负责人 gcl45f float null, --分享比率 gcl46c varchar(20) null, --第16负责人 gcl47f float null, --分享比率 gcl48c varchar(20) null, --会员编号 gcl49f float null, --分享比率 gcl50c varchar(20) null, --第18负责人 gcl51f float null, --套餐金额 gcl52c varchar(20) null, --备用 gcl53f float null, --储值应收金额 gcl54c varchar(20) null, --是否给积分 gcl55f float null, --储值卡的次数 gcl56t varchar(6) null, --消费时间 added by WUXM, 2005/08/16 gcl80d varchar(8) null, --帐务日期 added by WUXM, 2005/08/16 gcl57c varchar(800) null, --备注说明, added by WUXM, 2005/09/05 gcl58f float null, --第一销售虚业绩 Added by WUXM 2005/11/08 gcl59f float null, --第一销售提成 Added by WUXM 2005/11/08 gcl60f float null, --第二销售虚业绩 Added by WUXM 2005/11/08 gcl61f float null, --第二销售提成 Added by WUXM 2005/11/08 gcl62f float null, --第三销售虚业绩 Added by WUXM 2005/11/08 gcl63f float null, --第三销售提成 Added by WUXM 2005/11/08 gcl64f float null, --总储值金额(包括疗程) gcl65f float null, --总储值金额(除疗程) gcl66f float null, --总疗程金额 gcl81c varchar(10) null, --部门编号 gcl82c varchar(10) null, --部门编号 gcl83c varchar(10) null, --部门编号 gsc04c varchar(5) null, --支付方式 gsc05f float null, --支付金额 gba14i int null, --性别 gcl83f float null, --第四人员业绩 gcl84f float null, --第五人员业绩 gcl85f float null, --第六人员业绩 gcl86f float null, --第七人员业绩 gcl87f float null, --第八人员业绩 gcl88f float null, --第九人员业绩 gcl89f float null, --第十人员业绩 flag int null, gsc00c varchar(10) null, --公司编号 gsc01c varchar(20) null, --单据编号 gsc02c varchar(5) null, --单据类别 gsc03f float null --序号 ) create clustered index gcm10_buf_idx_01 on #gcm10_buf(gcl09c) create nonclustered index gcm10_buf_idx_02 on #gcm10_buf(gcl17c) create nonclustered index gcm10_buf_idx_03 on #gcm10_buf(gcl19c) create nonclustered index gcm10_buf_idx_04 on #gcm10_buf(gcl00c, gcl15c) insert #gcm10_buf(gcl00c, gcl01c, gcl02d, gcl03c, gcl04c, gcl05c, gcl06i, gcl07f, gcl08f, gcl09c, gcl10c, gcl11c, gcl12c, gcl13f, gcl15c, gcl16f, gcl17c, gcl18f, gcl19c, gcl20f, gcl21d, gcl22c, gcl23f, gcl24c, gcl25f, gcl26c, gcl27f, gcl28c, gcl29f, gcl30c, gcl31f, gcl32c, gcl33f, gcl34c, gcl35f, gcl36c, gcl37f, gcl38c, gcl39f, gcl40c, gcl41f, gcl42c, gcl43f, gcl44c, gcl45f, gcl46c, gcl47f, gcl48c, gcl49f, gcl50c, gcl51f, gcl52c, gcl53f, gcl54c, gcl55f, gcl56t, gcl80d, gcl57c, gcl58f, gcl59f, gcl60f, gcl61f, gcl62f, gcl63f, gcl64f, gcl65f, gcl66f, gcl81c, gcl82c, gcl83c, gsc04c, gsc05f, gba14i, gcl83f, gcl84f, gcl85f, gcl86f, gcl87f, gcl88f, gcl89f, gsc00c, gsc01c, gsc02c, gsc03f) select gcl00c, gcl01c, gcl02d, gcl03c, gcl04c, gcl05c, gcl06i, gcl07f, gcl08f, gcl09c, gcl10c, gcl11c, gcl12c, gcl13f, gcl15c, gcl16f, gcl17c, gcl18f, gcl19c, gcl20f, gcl21d, gcl22c, gcl23f, gcl24c, gcl25f, gcl26c, gcl27f, gcl28c, gcl29f, gcl30c, gcl31f, gcl32c, gcl33f, gcl34c, gcl35f, gcl36c, gcl37f, gcl38c, gcl39f, gcl40c, gcl41f, gcl42c, gcl43f, gcl44c, gcl45f, gcl46c, gcl47f, gcl48c, gcl49f, gcl50c, gcl51f, gcl52c, gcl53f, gcl54c, gcl55f, gcl56t, gcl80d, gcl57c, gcl58f, gcl59f, gcl60f, gcl61f, gcl62f, gcl63f, gcl64f, gcl65f, gcl66f, gcl81c, gcl82c, gcl83c, isnull(gsc04c, '1'), isnull(gsc05f, 0), 0, gcl83f, gcl84f, gcl85f, gcl86f, gcl87f, gcl88f, gcl89f, isnull(gsc00c, gcl00c), isnull(gsc01c, gcl01c), isnull(gsc02c, 'gz'), gsc03f from gcm10 a with (nolock), gsm03 d with (nolock) where a.gcl00c = @compid and a.gcl80d >= @fromdate and a.gcl80d <= @todate and isnull(a.gcl94d, '') <> '' and a.gcl00c *= d.gsc00c and a.gcl01c *= d.gsc01c and d.gsc02c = 'gz' update a set a.gba14i = isnull(b.gba14i, 0) from #gcm10_buf a, gbm01 b where a.gcl00c = b.gba00c and a.gcl03c = b.gba23c -- 如果是自行输入业绩,当时欠款的时候业绩应该为0,所以这个地方需要判断调整 -- 第一销售 insert #work_detail_ceshi(spid, compid, person_id, action_id, srvdate, code, name, payway, amt, quan, cost, stdprice, prj_type, billid, gender, share_rate, comm, paycode, amt2, repayment_sign) select @spid, @compid, gcl09c, 0, gcl80d, gcl03c, gak02c, '', gsc05f, case when gcl07f = 0 then gcl16f else gsc05f / gcl07f end, gak19f, gcl07f, gcl15c, gcl01c, gba14i, isnull(gcl16f, 1), gcl59f, gsc04c, (case when isnull(gcl07f, 0) = 0 then 0 else gcl58f * gsc05f / gcl07f end),(case when isnull(gcl06i, 0) = 6 then 1 else 0 end) from #gcm10_buf a, gam10 b with (nolock) where a.gcl00c = b.gak00c and a.gcl15c = b.gak01c and a.gcl09c >= @fromempl and a.gcl09c <= @toempl and (a.gcl06i = 0 or a.gcl06i = 6) and a.gcl04c <> '888' --and isnull(a.gcl07f, 0) <> 0 order by gcl09c, gcl80d -- 第二销售 insert #work_detail_ceshi(spid, compid, person_id, action_id, srvdate, code, name, payway, amt, quan, cost, stdprice, prj_type, billid, gender, share_rate, comm, paycode, amt2, repayment_sign) select @spid, @compid, gcl17c, 0, gcl80d, gcl03c, gak02c, '', gsc05f, case when gcl07f = 0 then gcl18f else gsc05f/gcl07f end, gak19f, gcl07f, gcl15c, gcl01c, gba14i, isnull(gcl18f, 0), gcl61f, gsc04c, (case when isnull(gcl07f, 0) = 0 then 0 else gcl60f*gsc05f/gcl07f end),(case when isnull(gcl06i, 0) = 6 then 1 else 0 end) from #gcm10_buf a, gam10 b with (nolock) where a.gcl00c = b.gak00c and a.gcl15c = b.gak01c and a.gcl17c >= @fromempl and a.gcl17c <= @toempl and (a.gcl06i = 0 or a.gcl06i = 6) and a.gcl04c<>'888' --and isnull(a.gcl07f, 0)<>0 order by gcl17c, gcl80d -- 第三销售 insert #work_detail_ceshi(spid, compid, person_id, action_id, srvdate, code, name, payway, amt, quan, cost, stdprice, prj_type, billid, gender, share_rate, comm, paycode, amt2, repayment_sign) select @spid, @compid, gcl19c, 0, gcl80d, gcl03c, gak02c, '', gsc05f, case when gcl07f = 0 then gcl20f else gsc05f/gcl07f end, gak19f, gcl07f, gcl15c, gcl01c, gba14i, isnull(gcl20f, 0), gcl63f, gsc04c, (case when isnull(gcl07f, 0) = 0 then 0 else gcl62f*gsc05f/gcl07f end),(case when isnull(gcl06i, 0) = 6 then 1 else 0 end) from #gcm10_buf a, gam10 b with (nolock) where a.gcl00c = b.gak00c and a.gcl15c = b.gak01c and a.gcl19c >= @fromempl and a.gcl19c <= @toempl and (a.gcl06i = 0 or a.gcl06i = 6) and a.gcl04c<>'888' --and isnull(a.gcl07f, 0)<>0 order by gcl19c, gcl80d --第4销售 insert #work_detail_ceshi(spid, compid, person_id, action_id, srvdate, code, name, amt, quan, cost, stdprice, prj_type, billid, gender, share_rate, comm, paycode, amt2, repayment_sign) select @spid, @compid, gcl22c, 0, gcl80d, gcl03c, gak02c, gsc05f, case when gcl07f = 0 then gcl23f else gsc05f/gcl07f end, gak19f, gcl07f, gcl15c, gcl01c, gba14i, isnull(gcl23f, 0), gcl63f, gsc04c, (case when isnull(gcl07f, 0) = 0 then 0 else gcl83f*gsc05f/gcl07f end),(case when isnull(gcl06i, 0) = 6 then 1 else 0 end) from #gcm10_buf a, gam10 b with (nolock) where a.gcl00c = b.gak00c and a.gcl15c = b.gak01c and a.gcl22c >= @fromempl and a.gcl22c <= @toempl and (a.gcl06i = 0 or a.gcl06i = 6) and a.gcl04c<>'888' --and isnull(a.gcl07f, 0)<>0 order by gcl22c, gcl80d --第5销售 insert #work_detail_ceshi(spid, compid, person_id, action_id, srvdate, code, name, amt, quan, cost, stdprice, prj_type, billid, gender, share_rate, comm, paycode, amt2, repayment_sign) select @spid, @compid, gcl24c, 0, gcl80d, gcl03c, gak02c, gsc05f, case when gcl07f = 0 then gcl25f else gsc05f/gcl07f end, gak19f, gcl07f, gcl15c, gcl01c, gba14i, isnull(gcl25f, 0), gcl63f, gsc04c, (case when isnull(gcl07f, 0) = 0 then 0 else gcl84f*gsc05f/gcl07f end),(case when isnull(gcl06i, 0) = 6 then 1 else 0 end) from #gcm10_buf a, gam10 b with (nolock) where a.gcl00c = b.gak00c and a.gcl15c = b.gak01c and a.gcl24c >= @fromempl and a.gcl24c <= @toempl and (a.gcl06i = 0 or a.gcl06i = 6) and a.gcl04c<>'888' --and isnull(a.gcl07f, 0)<>0 order by gcl24c, gcl80d --第6销售 insert #work_detail_ceshi(spid, compid, person_id, action_id, srvdate, code, name, amt, quan, cost, stdprice, prj_type, billid, gender, share_rate, comm, paycode, amt2, repayment_sign) select @spid, @compid, gcl26c, 0, gcl80d, gcl03c, gak02c, gsc05f, case when gcl07f = 0 then gcl27f else gsc05f/gcl07f end, gak19f, gcl07f, gcl15c, gcl01c, gba14i, isnull(gcl27f, 0), gcl63f, gsc04c, (case when isnull(gcl07f, 0) = 0 then 0 else gcl85f*gsc05f/gcl07f end),(case when isnull(gcl06i, 0) = 6 then 1 else 0 end) from #gcm10_buf a, gam10 b with (nolock) where a.gcl00c = b.gak00c and a.gcl15c = b.gak01c and a.gcl26c >= @fromempl and a.gcl26c <= @toempl and (a.gcl06i = 0 or a.gcl06i = 6) and a.gcl04c<>'888' --and isnull(a.gcl07f, 0)<>0 order by gcl26c, gcl80d --第7销售 insert #work_detail_ceshi(spid, compid, person_id, action_id, srvdate, code, name, amt, quan, cost, stdprice, prj_type, billid, gender, share_rate, comm, paycode, amt2, repayment_sign) select @spid, @compid, gcl28c, 0, gcl80d, gcl03c, gak02c, gsc05f, case when gcl07f = 0 then gcl29f else gsc05f/gcl07f end, gak19f, gcl07f, gcl15c, gcl01c, gba14i, isnull(gcl29f, 0), gcl63f, gsc04c, (case when isnull(gcl07f, 0) = 0 then 0 else gcl86f*gsc05f/gcl07f end),(case when isnull(gcl06i, 0) = 6 then 1 else 0 end) from #gcm10_buf a, gam10 b with (nolock) where a.gcl00c = b.gak00c and a.gcl15c = b.gak01c and a.gcl28c >= @fromempl and a.gcl28c <= @toempl and (a.gcl06i = 0 or a.gcl06i = 6) and a.gcl04c<>'888' -- and isnull(a.gcl07f, 0)<>0 order by gcl28c, gcl80d --第8销售 insert #work_detail_ceshi(spid, compid, person_id, action_id, srvdate, code, name, amt, quan, cost, stdprice, prj_type, billid, gender, share_rate, comm, paycode, amt2, repayment_sign) select @spid, @compid, gcl30c, 0, gcl80d, gcl03c, gak02c, gsc05f, case when gcl07f = 0 then gcl31f else gsc05f/gcl07f end, gak19f, gcl07f, gcl15c, gcl01c, gba14i, isnull(gcl31f, 0), gcl63f, gsc04c, (case when isnull(gcl07f, 0) = 0 then 0 else gcl87f*gsc05f/gcl07f end),(case when isnull(gcl06i, 0) = 6 then 1 else 0 end) from #gcm10_buf a, gam10 b with (nolock) where a.gcl00c = b.gak00c and a.gcl15c = b.gak01c and a.gcl30c >= @fromempl and a.gcl30c <= @toempl and (a.gcl06i = 0 or a.gcl06i = 6) and a.gcl04c<>'888' --and isnull(a.gcl07f, 0)<>0 order by gcl30c, gcl80d --第9销售 insert #work_detail_ceshi(spid, compid, person_id, action_id, srvdate, code, name, amt, quan, cost, stdprice, prj_type, billid, gender, share_rate, comm, paycode, amt2, repayment_sign) select @spid, @compid, gcl32c, 0, gcl80d, gcl03c, gak02c, gsc05f, case when gcl07f = 0 then gcl33f else gsc05f/gcl07f end, gak19f, gcl07f, gcl15c, gcl01c, gba14i, isnull(gcl33f, 0), gcl63f, gsc04c, (case when isnull(gcl07f, 0) = 0 then 0 else gcl88f*gsc05f/gcl07f end),(case when isnull(gcl06i, 0) = 6 then 1 else 0 end) from #gcm10_buf a, gam10 b with (nolock) where a.gcl00c = b.gak00c and a.gcl15c = b.gak01c and a.gcl32c >= @fromempl and a.gcl32c <= @toempl and (a.gcl06i = 0 or a.gcl06i = 6) and a.gcl04c<>'888' --and isnull(a.gcl07f, 0)<>0 order by gcl32c, gcl80d --第10销售 insert #work_detail_ceshi(spid, compid, person_id, action_id, srvdate, code, name, amt, quan, cost, stdprice, prj_type, billid, gender, share_rate, comm, paycode, amt2, repayment_sign) select @spid, @compid, gcl34c, 0, gcl80d, gcl03c, gak02c, gsc05f, case when gcl07f = 0 then gcl35f else gsc05f/gcl07f end, gak19f, gcl07f, gcl15c, gcl01c, gba14i, isnull(gcl35f, 0), gcl63f, gsc04c, (case when isnull(gcl07f, 0) = 0 then 0 else gcl89f*gsc05f/gcl07f end ),(case when isnull(gcl06i, 0) = 6 then 1 else 0 end) from #gcm10_buf a, gam10 b with (nolock) where a.gcl00c = b.gak00c and a.gcl15c = b.gak01c and a.gcl34c >= @fromempl and a.gcl34c <= @toempl and (a.gcl06i = 0 or a.gcl06i = 6) and a.gcl04c<>'888' --and isnull(a.gcl07f, 0)<>0 order by gcl34c, gcl80d --第一销售销售取款,欠款 insert #work_detail_ceshi(spid, compid, person_id, action_id, srvdate, code, name, payway, paycode, amt, amt2, amt3, quan, cost, stdprice, prj_type, billid, gender, share_rate, comm) select @spid, @compid, gcl09c, 0, gcl80d, gcl03c, gak02c, '欠/取款', gsc04c, gsc05f, (case when @sp048 = '1' then (case when isnull(gcl07f, 0) = 0 then 0 else (isnull(gcl58f, 0)*gsc05f/gcl07f) end) else isnull(gcl16f, 0)*gsc05f end), --取款时gsc05f 为负数 ((case when @sp048 = '1' then (case when isnull(gcl07f, 0) = 0 then 0 else isnull(gcl58f, 0)*gsc05f/gcl07f end) else isnull(gcl16f, 0)*isnull(gsc05f, 0) end)-isnull(gak19f, 0)), 0, 0 - gak19f, 0 - gcl07f, gcl15c, gcl01c, gba14i, isnull(gcl16f, 1), gcl59f from #gcm10_buf a, gam10 b with (nolock) where a.gcl00c = b.gak00c and a.gcl15c = b.gak01c and a.gcl09c >= @fromempl and a.gcl09c <= @toempl and (a.gcl06i = 1 ) and a.gcl04c <> '888' order by gcl09c, gcl80d --第二销售销售取款,欠款 insert #work_detail_ceshi(spid, compid, person_id, action_id, srvdate, code, name, payway, paycode, amt, amt2, amt3, quan, cost, stdprice, prj_type, billid, gender, share_rate, comm) select @spid, @compid, gcl17c, 0, gcl80d, gcl03c, gak02c, '欠/取款', gsc04c, gsc05f, (case when @sp048 = '1' then (case when isnull(gcl07f, 0) = 0 then 0 else (isnull(gcl60f, 0)*gsc05f/gcl07f) end) else isnull(gcl18f, 0)*(gsc05f) end), --取款时gsc05f 为负数 ((case when @sp048 = '1' then (case when isnull(gcl07f, 0) = 0 then 0 else isnull(gcl60f, 0)*gsc05f/gcl07f end) else isnull(gcl18f, 0)*isnull(gsc05f, 0) end)-isnull(gak19f, 0)), 0, 0 - gak19f, 0 - gcl07f, gcl15c, gcl01c, gba14i, isnull(gcl18f, 0), gcl61f from #gcm10_buf a, gam10 b with (nolock) where a.gcl00c = b.gak00c and a.gcl15c = b.gak01c and a.gcl17c >= @fromempl and a.gcl17c <= @toempl and (a.gcl06i = 1 ) and a.gcl04c<>'888' order by gcl17c, gcl80d --第三销售销售取款,欠款 insert #work_detail_ceshi(spid, compid, person_id, action_id, srvdate, code, name, payway, paycode, amt, amt2, amt3, quan, cost, stdprice, prj_type, billid, gender, share_rate, comm) select @spid, @compid, gcl19c, 0, gcl80d, gcl03c, gak02c, '欠/取款', gsc04c, gsc05f, (case when @sp048 = '1' then (case when isnull(gcl07f, 0) = 0 then 0 else (isnull(gcl62f, 0)*gsc05f/gcl07f) end) else isnull(gcl20f, 0)*(gsc05f) end), --取款时gsc05f 为负数 ((case when @sp048 = '1' then (case when isnull(gcl07f, 0) = 0 then 0 else isnull(gcl62f, 0)*gsc05f/gcl07f end) else isnull(gcl20f, 0)*isnull(gsc05f, 0) end)-isnull(gak19f, 0)), 0, 0 - gak19f, 0 - gcl07f, gcl15c, gcl01c, gba14i, isnull(gcl20f, 0), gcl63f from #gcm10_buf a, gam10 b with (nolock) where a.gcl00c = b.gak00c and a.gcl15c = b.gak01c and a.gcl19c >= @fromempl and a.gcl19c <= @toempl and (a.gcl06i = 1 ) and a.gcl04c<>'888' order by gcl19c, gcl80d --第4销售销售取款,欠款 insert #work_detail_ceshi(spid, compid, person_id, action_id, srvdate, code, name, payway, paycode, amt, amt2, amt3, quan, cost, stdprice, prj_type, billid, gender, share_rate, comm) select @spid, @compid, gcl22c, 0, gcl80d, gcl03c, gak02c, '欠/取款', gsc04c, gsc05f, (case when @sp048 = '1' then (case when isnull(gcl07f, 0) = 0 then 0 else (isnull(gcl83f, 0)*gsc05f/gcl07f) end) else isnull(gcl23f, 0)*(gsc05f) end), --取款时gsc05f 为负数 ((case when @sp048 = '1' then (case when isnull(gcl07f, 0) = 0 then 0 else isnull(gcl83f, 0)*gsc05f/gcl07f end) else isnull(gcl23f, 0)*isnull(gsc05f, 0) end)-isnull(gak19f, 0)), 0, 0 - gak19f, 0 - gcl07f, gcl15c, gcl01c, gba14i, isnull(gcl23f, 0), gcl63f from #gcm10_buf a, gam10 b with (nolock) where a.gcl00c = b.gak00c and a.gcl15c = b.gak01c and a.gcl22c >= @fromempl and a.gcl22c <= @toempl and (a.gcl06i = 1 ) and a.gcl04c<>'888' order by gcl22c, gcl80d --第5销售销售取款,欠款 insert #work_detail_ceshi(spid, compid, person_id, action_id, srvdate, code, name, payway, paycode, amt, amt2, amt3, quan, cost, stdprice, prj_type, billid, gender, share_rate, comm) select @spid, @compid, gcl24c, 0, gcl80d, gcl03c, gak02c, '欠/取款', gsc04c, gsc05f, (case when @sp048 = '1' then (case when isnull(gcl07f, 0) = 0 then 0 else (isnull(gcl84f, 0)*gsc05f/gcl07f) end) else isnull(gcl25f, 0)*(gsc05f) end), --取款时gsc05f 为负数 ((case when @sp048 = '1' then (case when isnull(gcl07f, 0) = 0 then 0 else isnull(gcl84f, 0)*gsc05f/gcl07f end) else isnull(gcl25f, 0)*isnull(gsc05f, 0) end)-isnull(gak19f, 0)), 0, 0 - gak19f, 0 - gcl07f, gcl15c, gcl01c, gba14i, isnull(gcl25f, 0), gcl63f from #gcm10_buf a, gam10 b with (nolock) where a.gcl00c = b.gak00c and a.gcl15c = b.gak01c and a.gcl24c >= @fromempl and a.gcl24c <= @toempl and (a.gcl06i = 1 ) and a.gcl04c<>'888' order by gcl24c, gcl80d --第6销售销售取款,欠款 insert #work_detail_ceshi(spid, compid, person_id, action_id, srvdate, code, name, payway, paycode, amt, amt2, amt3, quan, cost, stdprice, prj_type, billid, gender, share_rate, comm) select @spid, @compid, gcl26c, 0, gcl80d, gcl03c, gak02c, '欠/取款', gsc04c, gsc05f, (case when @sp048 = '1' then (case when isnull(gcl07f, 0) = 0 then 0 else (isnull(gcl85f, 0)*gsc05f/gcl07f) end) else isnull(gcl27f, 0)*(gsc05f) end), --取款时gsc05f 为负数 ((case when @sp048 = '1' then (case when isnull(gcl07f, 0) = 0 then 0 else isnull(gcl85f, 0)*gsc05f/gcl07f end) else isnull(gcl27f, 0)*isnull(gsc05f, 0) end)-isnull(gak19f, 0)), 0, 0 - gak19f, 0 - gcl07f, gcl15c, gcl01c, gba14i, isnull(gcl27f, 0), gcl63f from #gcm10_buf a, gam10 b with (nolock) where a.gcl00c = b.gak00c and a.gcl15c = b.gak01c and a.gcl26c >= @fromempl and a.gcl26c <= @toempl and (a.gcl06i = 1 ) and a.gcl04c<>'888' order by gcl26c, gcl80d --第7销售销售取款,欠款 insert #work_detail_ceshi(spid, compid, person_id, action_id, srvdate, code, name, payway, paycode, amt, amt2, amt3, quan, cost, stdprice, prj_type, billid, gender, share_rate, comm) select @spid, @compid, gcl28c, 0, gcl80d, gcl03c, gak02c, '欠/取款', gsc04c, gsc05f, (case when @sp048 = '1' then (case when isnull(gcl07f, 0) = 0 then 0 else (isnull(gcl86f, 0)*gsc05f/gcl07f) end) else isnull(gcl29f, 0)*(gsc05f) end), --取款时gsc05f 为负数 ((case when @sp048 = '1' then (case when isnull(gcl07f, 0) = 0 then 0 else isnull(gcl86f, 0)*gsc05f/gcl07f end) else isnull(gcl29f, 0)*isnull(gsc05f, 0) end)-isnull(gak19f, 0)), 0, 0 - gak19f, 0 - gcl07f, gcl15c, gcl01c, gba14i, isnull(gcl29f, 0), gcl63f from #gcm10_buf a, gam10 b with (nolock) where a.gcl00c = b.gak00c and a.gcl15c = b.gak01c and a.gcl28c >= @fromempl and a.gcl28c <= @toempl and (a.gcl06i = 1 ) and a.gcl04c<>'888' order by gcl28c, gcl80d --第8销售销售取款,欠款 insert #work_detail_ceshi(spid, compid, person_id, action_id, srvdate, code, name, payway, paycode, amt, amt2, amt3, quan, cost, stdprice, prj_type, billid, gender, share_rate, comm) select @spid, @compid, gcl30c, 0, gcl80d, gcl03c, gak02c, '欠/取款', gsc04c, gsc05f, (case when @sp048 = '1' then (case when isnull(gcl07f, 0) = 0 then 0 else (isnull(gcl87f, 0)*gsc05f/gcl07f) end) else isnull(gcl31f, 0)*(gsc05f) end), --取款时gsc05f 为负数 ((case when @sp048 = '1' then (case when isnull(gcl07f, 0) = 0 then 0 else isnull(gcl87f, 0)*gsc05f/gcl07f end) else isnull(gcl31f, 0)*isnull(gsc05f, 0) end)-isnull(gak19f, 0)), 0, 0 - gak19f, 0 - gcl07f, gcl15c, gcl01c, gba14i, isnull(gcl31f, 0), gcl63f from #gcm10_buf a, gam10 b with (nolock) where a.gcl00c = b.gak00c and a.gcl15c = b.gak01c and a.gcl30c >= @fromempl and a.gcl30c <= @toempl and (a.gcl06i = 1 ) and a.gcl04c<>'888' order by gcl30c, gcl80d --第9销售销售取款,欠款 insert #work_detail_ceshi(spid, compid, person_id, action_id, srvdate, code, name, payway, paycode, amt, amt2, amt3, quan, cost, stdprice, prj_type, billid, gender, share_rate, comm) select @spid, @compid, gcl32c, 0, gcl80d, gcl03c, gak02c, '欠/取款', gsc04c, gsc05f, (case when @sp048 = '1' then (case when isnull(gcl07f, 0) = 0 then 0 else (isnull(gcl88f, 0)*gsc05f/gcl07f) end) else isnull(gcl33f, 0)*(gsc05f) end), --取款时gsc05f 为负数 ((case when @sp048 = '1' then (case when isnull(gcl07f, 0) = 0 then 0 else isnull(gcl88f, 0)*gsc05f/gcl07f end) else isnull(gcl33f, 0)*isnull(gsc05f, 0) end)-isnull(gak19f, 0)), 0, 0 - gak19f, 0 - gcl07f, gcl15c, gcl01c, gba14i, isnull(gcl33f, 0), gcl63f from #gcm10_buf a, gam10 b with (nolock) where a.gcl00c = b.gak00c and a.gcl15c = b.gak01c and a.gcl32c >= @fromempl and a.gcl32c <= @toempl and (a.gcl06i = 1 ) and a.gcl04c<>'888' order by gcl32c, gcl80d -- 第10销售销售取款,欠款 insert #work_detail_ceshi(spid, compid, person_id, action_id, srvdate, code, name, payway, paycode, amt, amt2, amt3, quan, cost, stdprice, prj_type, billid, gender, share_rate, comm) select @spid, @compid, gcl34c, 0, gcl80d, gcl03c, gak02c, '欠/取款', gsc04c, gsc05f, (case when @sp048 = '1' then (case when isnull(gcl07f, 0) = 0 then 0 else (isnull(gcl89f, 0)*gsc05f/gcl07f) end) else isnull(gcl35f, 0)*(gsc05f) end), --取款时gsc05f 为负数 ((case when @sp048 = '1' then (case when isnull(gcl07f, 0) = 0 then 0 else isnull(gcl89f, 0)*gsc05f/gcl07f end) else isnull(gcl35f, 0)*isnull(gsc05f, 0) end)-isnull(gak19f, 0)), 0, 0 - gak19f, 0 - gcl07f, gcl15c, gcl01c, gba14i, isnull(gcl35f, 0), gcl63f from #gcm10_buf a, gam10 b with (nolock) where a.gcl00c = b.gak00c and a.gcl15c = b.gak01c and a.gcl34c >= @fromempl and a.gcl34c <= @toempl and (a.gcl06i = 1 ) and a.gcl04c<>'888' order by gcl34c, gcl80d update a set a.payway = b.gsb03c from #work_detail_ceshi a, gsm02 b where b.gsb00c = @compid and b.gsb01c = '3' and b.gsb02c = a.paycode and a.spid = @spid drop table #gcm10_buf end if (@inc_service = 1) begin create table #ggm02_buf -- 项目消费(收银) - 消费明细 ( ggb00c varchar(10) not null, --公司编号 ggb01c varchar(20) not null, --消费单号 ggb02f float not null, --序号 ggb03c varchar(20) null, --子项目代码 ggb04c varchar(5) null, --单位 ggb05f float null, --数量 ggb06c varchar(5) null, --币别 ggb07f float null, --汇率 ggb08f float null, --标准单价 ggb09f float null, --折让 ggb10f float null, --折让单价 ggb11f float null, --金额 ggb12c varchar(20) null, --大工代码 ggb13c varchar(20) null, --中工代码 ggb14c varchar(20) null, --小工代码 ggb15c varchar(20) null, --大工类型 ggb16c varchar(20) null, --中工类型 ggb17f float null, --成本金额 ggb18f float null, --转卡比率 ggb19c varchar(20) null, --小工类型 ggb20f float null, --大工虚业绩 ggb21f float null, --中工虚业绩 ggb22f float null, --小工虚业绩 ggb23c varchar(128) null, --项目打折原因, added by WUXM, 2005/09/01 ggb24f float null, --项目应收金额, added by WUXM, 2005/09/08 ggb25c varchar(20) null, --四工类型 ggb26c varchar(20) null, --四工代码 ggb27c varchar(5) null, --项目支付方式 Added by WUXM 2005/11/09 ggb28f float null, --大工提成 Added by WUXM 2005/11/10 ggb29f float null, --中工提成 Added by WUXM 2005/11/10 ggb30f float null, --小工提成 Added by WUXM 2005/11/10 ggb31c varchar(20) null, --五工类别 ggb32c varchar(20) null, --五工代码 ggb33f float null, --项目积分 ggb34f float null, --实际营业额折算率 ggb35f float null, --四工虚业绩 ggb81c varchar(10) null, --部门编号 ggb36f float null, --大工实业绩 ggb37f float null, --中工实业绩 ggb38f float null, --小工实业绩 ggb39f float null, --四工实业绩 ggb40i int null, --欠款标志 ggb41d varchar(20) null, --还款日期 ggb42c varchar(20) null, --还款单号 gga31i int null, gga80d varchar(10) null, --帐务日期 gga09c varchar(20) null, --会员卡号 ) create clustered index ggm02_buf_idx_01 on #ggm02_buf(ggb12c) create nonclustered index ggm02_buf_idx_02 on #ggm02_buf(ggb13c) create nonclustered index ggm02_buf_idx_03 on #ggm02_buf(ggb14c) -- 除了还款部分 insert #ggm02_buf(ggb00c, ggb01c, ggb02f, ggb03c, ggb04c, ggb05f, ggb06c, ggb07f, ggb08f, ggb09f, ggb10f, ggb11f, ggb12c, ggb13c, ggb14c, ggb15c, ggb16c, ggb17f, ggb18f, ggb19c, ggb20f, ggb21f, ggb22f, ggb23c, ggb24f, ggb25c, ggb26c, ggb27c, ggb28f, ggb29f, ggb30f, ggb31c, ggb32c, ggb33f, ggb34f, ggb35f, ggb81c, ggb36f, ggb37f, ggb38f, ggb39f, ggb40i, ggb41d, ggb42c, gga31i, gga80d, gga09c) select ggb00c, ggb01c, ggb02f, ggb03c, ggb04c, ggb05f, ggb06c, ggb07f, ggb08f, ggb09f, ggb10f, ggb11f, upper(ggb12c), upper(ggb13c), upper(ggb14c), ggb15c, ggb16c, ggb17f, ggb18f, ggb19c, ggb20f, ggb21f, ggb22f, ggb23c, ggb24f, ggb25c, upper(ggb26c), ggb27c, ggb28f, ggb29f, ggb30f, ggb31c, ggb32c, ggb33f, ggb34f, ggb35f, ggb81c, ggb36f, ggb37f, ggb38f, ggb39f, ggb40i, '', ggb42c, gga31i, gga80d, gga09c from ggm02 b with (nolock), ggm01 a with (nolock) where a.gga00c = b.ggb00c and a.gga01c = b.ggb01c and a.gga00c = @compid and a.gga80d >= @fromdate and a.gga80d <= @todate -- 还款部分 -- 以还款那天作为业绩统计 insert #ggm02_buf(ggb00c, ggb01c, ggb02f, ggb03c, ggb04c, ggb05f, ggb06c, ggb07f, ggb08f, ggb09f, ggb10f, ggb11f, ggb12c, ggb13c, ggb14c, ggb15c, ggb16c, ggb17f, ggb18f, ggb19c, ggb20f, ggb21f, ggb22f, ggb23c, ggb24f, ggb25c, ggb26c, ggb27c, ggb28f, ggb29f, ggb30f, ggb31c, ggb32c, ggb33f, ggb34f, ggb35f, ggb81c, ggb36f, ggb37f, ggb38f, ggb39f, ggb40i, ggb41d, ggb42c, gga31i, gga80d, gga09c) select ggb00c, ggb01c, ggb02f, ggb03c, ggb04c, ggb05f, ggb06c, ggb07f, ggb08f, ggb09f, ggb10f, ggb11f, upper(ggb12c), upper(ggb13c), upper(ggb14c), ggb15c, ggb16c, ggb17f, ggb18f, ggb19c, ggb20f, ggb21f, ggb22f, ggb23c, ggb24f, ggb25c, upper(ggb26c), '', ggb28f, ggb29f, ggb30f, ggb31c, ggb32c, ggb33f, ggb34f, ggb35f, ggb81c, ggb36f, ggb37f, ggb38f, ggb39f, 0, ggb41d, ggb42c, gga31i, ggb41d, gga09c from ggm02 b with (nolock), ggm01 a with (nolock) where a.gga00c = b.ggb00c and a.gga01c = b.ggb01c and a.gga00c = @compid and b.ggb41d >= @fromdate and b.ggb41d <= @todate and isnull(b.ggb41d, '') <> '' -- 四工数据 insert #work_detail_ceshi(spid, compid, person_id, action_id, srvdate, code, name, payway, amt, amt2, quan, cost, cardrate, stdprice, prj_type, billid, gender, comm, paycode, ex_rate, ggm02_seq, amt3, cardid) select @spid, @compid, ggb26c, isnull(convert(int, (case when isnull(ggb25c, '') = '' then '2' else ggb25c end)), 1) + 9, gga80d, ggb03c, gda03c, gsb03c, ggb11f, ggb35f, ggb05f, ggb17f, ggb18f, ggb08f, gda04c, ggb01c, gga31i, 0, ggb27c, ggb34f, ggb02f, ggb39f, gga09c from #ggm02_buf b, gdm01 c with (nolock), gsm02 e with (nolock) where b.ggb00c = c.gda00c and b.ggb03c = c.gda01c and b.ggb00c = e.gsb00c and e.gsb01c = '3' and e.gsb02c = b.ggb27c and b.ggb26c >= @fromempl and b.ggb26c <= @toempl and isnull(b.ggb26c, '') <> '' and b.ggb25c <> '3' order by ggb26c, gga80d -- 小工数据,已经计算虚业绩与成本 insert #work_detail_ceshi(spid, compid, person_id, action_id, srvdate, code, name, payway, amt, amt2, quan, cost, cardrate, stdprice, prj_type, billid, gender, comm, paycode, ex_rate, ggm02_seq, amt3, cardid) select @spid, @compid, ggb14c, isnull(convert(int, (case when isnull(ggb19c, '') = '' then '2' else ggb19c end)), 1) + 6, gga80d, ggb03c, gda03c, gsb03c, ggb11f, ggb22f, ggb05f, ggb17f, ggb18f, ggb08f, gda04c, ggb01c, gga31i, ggb30f, ggb27c, ggb34f, ggb02f, ggb38f, gga09c from #ggm02_buf b, gdm01 c with (nolock), gsm02 e with (nolock) where b.ggb00c = c.gda00c and b.ggb03c = c.gda01c and b.ggb00c = e.gsb00c and e.gsb01c = '3' and e.gsb02c = b.ggb27c and b.ggb14c >= @fromempl and b.ggb14c <= @toempl and isnull(b.ggb14c, '') <> '' and b.ggb19c <> '3' order by ggb14c, gga80d --update lucz insert #work_detail_ceshi(spid, compid, person_id, action_id, srvdate, code, name, payway, amt, amt2, quan, cost, cardrate, stdprice, prj_type, billid, gender, comm, paycode, ex_rate, ggm02_seq, amt3, cardid) select @spid, @compid, ggb14c, 8, gga80d, ggb03c, gda03c, gsb03c, 0-ggb11f, 0-ggb22f, ggb05f, ggb17f, ggb18f, ggb08f, gda04c, ggb01c, gga31i, ggb30f, ggb27c, ggb34f, ggb02f, 0 - ggb38f, gga09c from #ggm02_buf b, gdm01 c with (nolock), gsm02 e with (nolock) where b.ggb00c = c.gda00c and b.ggb03c = c.gda01c and b.ggb00c = e.gsb00c and e.gsb01c = '3' and e.gsb02c = b.ggb27c and b.ggb14c >= @fromempl and b.ggb14c <= @toempl and isnull(b.ggb14c, '') <> '' and b.ggb19c = '3' order by ggb14c, gga80d -- 中工数据,已经计算虚业绩与成本 insert #work_detail_ceshi(spid, compid, person_id, action_id, srvdate, code, name, payway, amt, amt2, quan, cost, cardrate, stdprice, prj_type, billid, gender, comm, paycode, ex_rate, ggm02_seq, amt3, cardid) select @spid, @compid, ggb13c, isnull(convert(int, (case when isnull(ggb16c, '') = '' then '2' else ggb16c end)), 1) + 4, gga80d, ggb03c, gda03c, gsb03c, ggb11f, ggb21f, ggb05f, ggb17f, ggb18f, ggb08f, gda04c, ggb01c, gga31i, ggb29f, ggb27c, ggb34f, ggb02f, ggb37f, gga09c from #ggm02_buf b, gdm01 c with (nolock), gsm02 e with (nolock) where b.ggb00c = c.gda00c and b.ggb03c = c.gda01c and b.ggb00c = e.gsb00c and e.gsb01c = '3' and e.gsb02c = b.ggb27c and b.ggb13c >= @fromempl and b.ggb13c <= @toempl and isnull(b.ggb13c, '') <> '' and b.ggb16c <> '3' order by ggb13c, gga80d --update lucz insert #work_detail_ceshi(spid, compid, person_id, action_id, srvdate, code, name, payway, amt, amt2, quan, cost, cardrate, stdprice, prj_type, billid, gender, comm, paycode, ex_rate, ggm02_seq, amt3, cardid) select @spid, @compid, ggb13c, 6, gga80d, ggb03c, gda03c, gsb03c, 0-ggb11f, 0-ggb21f, ggb05f, ggb17f, ggb18f, ggb08f, gda04c, ggb01c, gga31i, ggb29f, ggb27c, ggb34f, ggb02f, 0 - ggb37f, gga09c from #ggm02_buf b, gdm01 c with (nolock), gsm02 e with (nolock) where b.ggb00c = c.gda00c and b.ggb03c = c.gda01c and b.ggb00c = e.gsb00c and e.gsb01c = '3' and e.gsb02c = b.ggb27c and b.ggb13c >= @fromempl and b.ggb13c <= @toempl and isnull(b.ggb13c, '') <> '' and b.ggb16c = '3' order by ggb13c, gga80d -- 大工数据,已经计算虚业绩与成本 insert #work_detail_ceshi(spid, compid, person_id, action_id, srvdate, code, name, payway, amt, amt2, quan, cost, cardrate, stdprice, prj_type, billid, gender, comm, paycode, ex_rate, ggm02_seq, amt3, cardid) select @spid, @compid, ggb12c, isnull(convert(int, (case when isnull(ggb15c, '') = '' then '2' else ggb15c end)), 1) + 1, gga80d, ggb03c, gda03c, gsb03c, ggb11f, ggb20f, ggb05f, ggb17f, ggb18f, ggb08f, gda04c, ggb01c, gga31i, ggb28f, ggb27c, ggb34f, ggb02f, ggb36f, gga09c from #ggm02_buf b, gdm01 c with (nolock), gsm02 e with (nolock) where b.ggb00c = c.gda00c and b.ggb03c = c.gda01c and b.ggb00c = e.gsb00c and e.gsb01c = '3' and e.gsb02c = b.ggb27c and b.ggb12c >= @fromempl and b.ggb12c <= @toempl and isnull(b.ggb12c, '') <> '' and b.ggb15c <> '3' order by ggb12c, gga80d -- update lucz -- 大工返工 -- 成本为0 insert #work_detail_ceshi(spid, compid, person_id, action_id, srvdate, code, name, payway, amt, amt2, quan, cost, cardrate, stdprice, prj_type, billid, gender, comm, paycode, ex_rate, ggm02_seq, amt3, cardid) select @spid, @compid, ggb12c, isnull(convert(int, (case when isnull(ggb15c, '') = '' then '2' else ggb15c end)), 1) + 1, gga80d, ggb03c, gda03c, gsb03c, 0-ggb11f, 0-ggb20f, ggb05f, ggb17f, ggb18f, ggb08f, gda04c, ggb01c, gga31i, ggb28f, ggb27c, ggb34f, ggb02f, 0 - ggb36f, gga09c from #ggm02_buf b, gdm01 c with (nolock), gsm02 e with (nolock) where b.ggb00c = c.gda00c and b.ggb03c = c.gda01c and b.ggb00c = e.gsb00c and e.gsb01c = '3' and e.gsb02c = b.ggb27c and b.ggb12c >= @fromempl and b.ggb12c <= @toempl and isnull(b.ggb12c, '') <> '' and b.ggb15c = '3' order by ggb12c, gga80d -- 项目欠款部分 -- 四工数据 insert #work_detail_ceshi(spid, compid, person_id, action_id, srvdate, code, name, payway, amt, amt2, quan, cost, cardrate, stdprice, prj_type, billid, gender, comm, paycode, ex_rate, ggm02_seq, amt3, cardid) select @spid, @compid, ggb26c, isnull(convert(int, (case when isnull(ggb25c, '') = '' then '2' else ggb25c end)), 1) + 9, gga80d, ggb03c, gda03c, '欠款', 0-ggb11f, 0-ggb35f, 0-ggb05f, 0-ggb17f, ggb18f, ggb08f, gda04c, ggb01c, gga31i, 0, '$$', ggb34f, ggb02f, 0-ggb39f, gga09c from #ggm02_buf b, gdm01 c with (nolock) where b.ggb00c = c.gda00c and b.ggb03c = c.gda01c and b.ggb26c >= @fromempl and b.ggb26c <= @toempl and isnull(b.ggb26c, '') <> '' and b.ggb25c <> '3' and isnull(b.ggb40i, 0) = 1 -- 欠款标志 order by ggb26c, gga80d -- 小工数据,已经计算虚业绩与成本 insert #work_detail_ceshi(spid, compid, person_id, action_id, srvdate, code, name, payway, amt, amt2, quan, cost, cardrate, stdprice, prj_type, billid, gender, comm, paycode, ex_rate, ggm02_seq, amt3, cardid) select @spid, @compid, ggb14c, isnull(convert(int, (case when isnull(ggb19c, '') = '' then '2' else ggb19c end)), 1) + 6, gga80d, ggb03c, gda03c, '欠款', 0-ggb11f, 0-ggb22f, 0-ggb05f, 0-ggb17f, ggb18f, ggb08f, gda04c, ggb01c, gga31i, ggb30f, '$$', ggb34f, ggb02f, 0-ggb38f, gga09c from #ggm02_buf b, gdm01 c with (nolock) where b.ggb00c = c.gda00c and b.ggb03c = c.gda01c and b.ggb14c >= @fromempl and b.ggb14c <= @toempl and isnull(b.ggb14c, '') <> '' and b.ggb19c <> '3' and isnull(b.ggb40i, 0) = 1 order by ggb14c, gga80d -- 补工不欠款 -- 中工数据,已经计算虚业绩与成本 insert #work_detail_ceshi(spid, compid, person_id, action_id, srvdate, code, name, payway, amt, amt2, quan, cost, cardrate, stdprice, prj_type, billid, gender, comm, paycode, ex_rate, ggm02_seq, amt3, cardid) select @spid, @compid, ggb13c, isnull(convert(int, (case when isnull(ggb16c, '') = '' then '2' else ggb16c end)), 1) + 4, gga80d, ggb03c, gda03c, '欠款', 0-ggb11f, 0-ggb21f, 0-ggb05f, 0-ggb17f, ggb18f, ggb08f, gda04c, ggb01c, gga31i, ggb29f, '$$', ggb34f, ggb02f, 0-ggb37f, gga09c from #ggm02_buf b, gdm01 c with (nolock) where b.ggb00c = c.gda00c and b.ggb03c = c.gda01c and b.ggb13c >= @fromempl and b.ggb13c <= @toempl and isnull(b.ggb13c, '') <> '' and b.ggb16c <> '3' and isnull(b.ggb40i, 0) = 1 order by ggb13c, gga80d -- 大工数据,已经计算虚业绩与成本 insert #work_detail_ceshi(spid, compid, person_id, action_id, srvdate, code, name, payway, amt, amt2, quan, cost, cardrate, stdprice, prj_type, billid, gender, comm, paycode, ex_rate, ggm02_seq, amt3, cardid) select @spid, @compid, ggb12c, isnull(convert(int, (case when isnull(ggb15c, '') = '' then '2' else ggb15c end)), 1) + 1, gga80d, ggb03c, gda03c, '欠款', 0-ggb11f, 0-ggb20f, 0-ggb05f, 0-ggb17f, ggb18f, ggb08f, gda04c, ggb01c, gga31i, ggb28f, '$$', ggb34f, ggb02f, 0-ggb36f, gga09c from #ggm02_buf b, gdm01 c with (nolock) where b.ggb00c = c.gda00c and b.ggb03c = c.gda01c and b.ggb12c >= @fromempl and b.ggb12c <= @toempl and isnull(b.ggb12c, '') <> '' and b.ggb15c <> '3' and isnull(b.ggb40i, 0) = 1 order by ggb12c, gga80d -- 项目欠款对应的还款部分 -- 还款, 四工数据 insert #work_detail_ceshi(spid, compid, person_id, action_id, srvdate, code, name, payway, amt, amt2, quan, cost, cardrate, stdprice, prj_type, billid, gender, comm, paycode, ex_rate, ggm02_seq, amt3, cardid) select @spid, @compid, ggb26c, isnull(convert(int, (case when isnull(ggb25c, '') = '' then '2' else ggb25c end)), 1) + 9, ggb41d, ggb03c, gda03c, '还款', b.ggb11f, ggb35f, ggb05f, ggb17f, ggb18f, ggb08f, gda04c, ggb01c, gga31i, 0, d.gsc04c, ggb34f, ggb02f, ggb39f, gga09c from #ggm02_buf b, gdm01 c with (nolock), gsm03 d with(nolock) where b.ggb00c = c.gda00c and b.ggb03c = c.gda01c and b.ggb26c >= @fromempl and b.ggb26c <= @toempl and b.ggb00c = d.gsc00c and b.ggb42c = d.gsc01c and d.gsc02c = 'gz' and isnull(b.ggb26c, '') <> '' and b.ggb25c <> '3' and isnull(b.ggb41d, '') <> '' order by ggb26c, gga80d -- 还款, 小工数据,已经计算虚业绩与成本 insert #work_detail_ceshi(spid, compid, person_id, action_id, srvdate, code, name, payway, amt, amt2, quan, cost, cardrate, stdprice, prj_type, billid, gender, comm, paycode, ex_rate, ggm02_seq, amt3, cardid) select @spid, @compid, ggb14c, isnull(convert(int, (case when isnull(ggb19c, '') = '' then '2' else ggb19c end)), 1) + 6, ggb41d, ggb03c, gda03c, '还款', b.ggb11f, ggb22f, ggb05f, ggb17f, ggb18f, ggb08f, gda04c, ggb01c, gga31i, ggb30f, d.gsc04c, ggb34f, ggb02f, ggb38f, gga09c from #ggm02_buf b, gdm01 c with (nolock), gsm03 d with(nolock) where b.ggb00c = c.gda00c and b.ggb03c = c.gda01c and b.ggb14c >= @fromempl and b.ggb14c <= @toempl and b.ggb00c = d.gsc00c and b.ggb42c = d.gsc01c and d.gsc02c = 'gz' and isnull(b.ggb14c, '') <> '' and b.ggb19c <> '3' and isnull(b.ggb41d, '') <> '' order by ggb14c, gga80d -- 还款, 中工数据,已经计算虚业绩与成本 insert #work_detail_ceshi(spid, compid, person_id, action_id, srvdate, code, name, payway, amt, amt2, quan, cost, cardrate, stdprice, prj_type, billid, gender, comm, paycode, ex_rate, ggm02_seq, amt3, cardid) select @spid, @compid, ggb13c, isnull(convert(int, (case when isnull(ggb16c, '') = '' then '2' else ggb16c end)), 1) + 4, ggb41d, ggb03c, gda03c, '还款', b.ggb11f, ggb21f, ggb05f, ggb17f, ggb18f, ggb08f, gda04c, ggb01c, gga31i, ggb29f, d.gsc04c, ggb34f, ggb02f, ggb37f, gga09c from #ggm02_buf b, gdm01 c with (nolock), gsm03 d with(nolock) where b.ggb00c = c.gda00c and b.ggb03c = c.gda01c and b.ggb13c >= @fromempl and b.ggb13c <= @toempl and b.ggb00c = d.gsc00c and b.ggb42c = d.gsc01c and d.gsc02c = 'gz' and isnull(b.ggb13c, '') <> '' and b.ggb16c <> '3' and isnull(b.ggb41d, '') <> '' order by ggb13c, gga80d -- 大工数据,已经计算虚业绩与成本 insert #work_detail_ceshi(spid, compid, person_id, action_id, srvdate, code, name, payway, amt, amt2, quan, cost, cardrate, stdprice, prj_type, billid, gender, comm, paycode, ex_rate, ggm02_seq, amt3, cardid) select @spid, @compid, ggb12c, isnull(convert(int, (case when isnull(ggb15c, '') = '' then '2' else ggb15c end)), 1) + 1, ggb41d, ggb03c, gda03c, '还款', b.ggb11f, ggb20f, ggb05f, ggb17f, ggb18f, ggb08f, gda04c, ggb01c, gga31i, ggb28f, d.gsc04c, ggb34f, ggb02f, ggb36f, gga09c from #ggm02_buf b, gdm01 c with (nolock), gsm03 d with(nolock) where b.ggb00c = c.gda00c and b.ggb03c = c.gda01c and b.ggb12c >= @fromempl and b.ggb12c <= @toempl and b.ggb00c = d.gsc00c and b.ggb42c = d.gsc01c and d.gsc02c = 'gz' and isnull(b.ggb12c, '') <> '' and b.ggb15c <> '3' and isnull(b.ggb41d, '') <> '' order by ggb12c, gga80d drop table #ggm02_buf end -- 虚业绩与成本已经在收银单复合的时候计算好 -- 因为在计算虚业绩的已经考虑了分享比率, 所以这个地方的分享比率只能为1 if (@inc_goods = 1) begin -- 卖产品第一销售 insert #work_detail_ceshi(spid, compid, person_id, action_id, srvdate, code, name, payway, amt, amt2, amt3, quan, cost, stdprice, share_rate, prj_type, billid, gender, comm, ex_rate, paycode, cardid) select @spid, @compid, ggc12c, 9, gga80d, ggc03c, gfa03c, gsb03c, ggc11f, ggc21f, ggc21f - isnull(ggc13f, 0), ggc05f, ggc13f, ggc08f, isnull(ggc14f, 1), gfa05c, gga01c, gga31i, ggc22f, ggc30f, ggc27c, gga09c from ggm03 b with (nolock), ggm01 a with (nolock), gfm01 c with (nolock), gsm02 with (nolock) where a.gga00c = b.ggc00c and a.gga01c = b.ggc01c and b.ggc00c = c.gfa00c and b.ggc03c = c.gfa01c and gga00c = gsb00c and gsb01c = '3' and gsb02c = ggc27c and a.gga00c = @compid and a.gga80d >= @fromdate and a.gga80d <= @todate and b.ggc12c >= @fromempl and b.ggc12c <= @toempl order by ggc12c, gga80d -- 卖产品第二销售 insert #work_detail_ceshi(spid, compid, person_id, action_id, srvdate, code, name, payway, amt, amt2, amt3, quan, cost, stdprice, share_rate, prj_type, billid, gender, comm, ex_rate, paycode, cardid) select @spid, @compid, ggc15c, 9, gga80d, ggc03c, gfa03c, gsb03c, ggc11f, ggc23f, ggc23f - isnull(ggc13f, 0), ggc05f, ggc13f, ggc08f, ggc16f, gfa05c, gga01c, gga31i, ggc24f, ggc30f, ggc27c, gga09c from ggm03 b with (nolock), ggm01 a with (nolock), gfm01 c with (nolock), gsm02 with (nolock) where a.gga00c = b.ggc00c and a.gga01c = b.ggc01c and b.ggc00c = c.gfa00c and b.ggc03c = c.gfa01c and gga00c = gsb00c and gsb01c = '3' and gsb02c = ggc27c and a.gga00c = @compid and a.gga80d >= @fromdate and a.gga80d <= @todate and b.ggc15c >= @fromempl and b.ggc15c <= @toempl order by ggc15c, gga80d -- 卖产品第三销售 insert #work_detail_ceshi(spid, compid, person_id, action_id, srvdate, code, name, payway, amt, amt2, amt3, quan, cost, stdprice, share_rate, prj_type, billid, gender, comm, ex_rate, paycode, cardid) select @spid, @compid, ggc17c, 9, gga80d, ggc03c, gfa03c, gsb03c, ggc11f, ggc25f, ggc25f - isnull(ggc13f, 0), ggc05f, ggc13f, ggc08f, ggc18f, gfa05c, gga01c, gga31i, ggc26f, ggc30f, ggc27c, gga09c from ggm03 b with (nolock), ggm01 a with (nolock), gfm01 c with (nolock), gsm02 with (nolock) where a.gga00c = b.ggc00c and a.gga01c = b.ggc01c and b.ggc00c = c.gfa00c and b.ggc03c = c.gfa01c and gga00c = gsb00c and gsb01c = '3' and gsb02c = ggc27c and a.gga00c = @compid and a.gga80d >= @fromdate and a.gga80d <= @todate and b.ggc17c >= @fromempl and b.ggc17c <= @toempl order by ggc17c, gga80d -- 欠款的产品 insert #work_detail_ceshi(spid, compid, person_id, action_id, srvdate, code, name, payway, amt, amt2, amt3, quan, cost, stdprice, share_rate, prj_type, billid, gender, comm, ex_rate, paycode, cardid) select @spid, @compid, ggc12c, 9, gga80d, ggc03c, gfa03c, '欠款', 0 - ggc11f, 0 - ggc21f, 0 - (ggc21f - isnull(ggc13f, 0)), 0 - ggc05f, 0 - ggc13f, ggc08f, isnull(ggc14f, 1), gfa05c, gga01c, gga31i, ggc22f, ggc30f, '$$', gga09c from ggm03 b with (nolock), ggm01 a with (nolock), gfm01 c with (nolock) where a.gga00c = b.ggc00c and a.gga01c = b.ggc01c and b.ggc00c = c.gfa00c and b.ggc03c = c.gfa01c and a.gga00c = @compid and a.gga80d >= @fromdate and a.gga80d <= @todate and b.ggc12c >= @fromempl and b.ggc12c <= @toempl and b.ggc32i = 1 order by ggc12c, gga80d -- 卖产品第二销售 insert #work_detail_ceshi(spid, compid, person_id, action_id, srvdate, code, name, payway, amt, amt2, amt3, quan, cost, stdprice, share_rate, prj_type, billid, gender, comm, ex_rate, paycode, cardid) select @spid, @compid, ggc15c, 9, gga80d, ggc03c, gfa03c, '欠款', 0 - ggc11f, 0 - ggc23f, 0 - (ggc23f - isnull(ggc13f, 0)), 0 - ggc05f, 0 - ggc13f, ggc08f, ggc16f, gfa05c, gga01c, gga31i, ggc24f, ggc30f, '$$', gga09c from ggm03 b with (nolock), ggm01 a with (nolock), gfm01 c with (nolock) where a.gga00c = b.ggc00c and a.gga01c = b.ggc01c and b.ggc00c = c.gfa00c and b.ggc03c = c.gfa01c and a.gga00c = @compid and a.gga80d >= @fromdate and a.gga80d <= @todate and b.ggc15c >= @fromempl and b.ggc15c <= @toempl and b.ggc32i = 1 order by ggc15c, gga80d -- 卖产品第三销售 insert #work_detail_ceshi(spid, compid, person_id, action_id, srvdate, code, name, payway, amt, amt2, amt3, quan, cost, stdprice, share_rate, prj_type, billid, gender, comm, ex_rate, paycode, cardid) select @spid, @compid, ggc17c, 9, gga80d, ggc03c, gfa03c, '欠款', 0 - ggc11f, 0 - ggc25f, 0 - (ggc25f - isnull(ggc13f, 0)), 0 - ggc05f, 0 - ggc13f, ggc08f, ggc18f, gfa05c, gga01c, gga31i, ggc26f, ggc30f, '$$', gga09c from ggm03 b with (nolock), ggm01 a with (nolock), gfm01 c with (nolock) where a.gga00c = b.ggc00c and a.gga01c = b.ggc01c and b.ggc00c = c.gfa00c and b.ggc03c = c.gfa01c and a.gga00c = @compid and a.gga80d >= @fromdate and a.gga80d <= @todate and b.ggc17c >= @fromempl and b.ggc17c <= @toempl and b.ggc32i = 1 order by ggc17c, gga80d -- -- 还款的产品, 在此体现业绩, 所以日期是按还款日期来统计的 -- insert #work_detail_ceshi(spid, compid, person_id, action_id, srvdate, code, name, payway, amt, amt2, amt3, quan, cost, stdprice, share_rate, prj_type, billid, gender, comm, ex_rate, paycode, cardid) -- select @spid, @compid, ggc12c, 9, ggc33d, ggc03c, gfa03c, '还款', ggc11f, ggc21f, ggc21f - isnull(ggc13f, 0), ggc05f, ggc13f, ggc08f, isnull(ggc14f, 1), gfa05c, gga01c, gga31i, ggc22f, ggc30f, '1', gga09c -- from ggm03 b with (nolock), ggm01 a with (nolock), gfm01 c with (nolock) -- where a.gga00c = b.ggc00c -- and a.gga01c = b.ggc01c -- and b.ggc00c = c.gfa00c -- and b.ggc03c = c.gfa01c -- and a.gga00c = @compid -- and b.ggc33d >= @fromdate -- 还款日期 -- and b.ggc33d <= @todate -- 还款日期 -- and b.ggc12c >= @fromempl -- and b.ggc12c <= @toempl -- order by ggc12c, gga80d -- -- -- 还款,卖产品第二销售 -- insert #work_detail_ceshi(spid, compid, person_id, action_id, srvdate, code, name, payway, amt, amt2, amt3, quan, cost, stdprice, share_rate, prj_type, billid, gender, comm, ex_rate, paycode, cardid) -- select @spid, @compid, ggc15c, 9, ggc33d, ggc03c, gfa03c, '还款', ggc11f, ggc23f, ggc23f - isnull(ggc13f, 0), ggc05f, ggc13f, ggc08f, ggc16f, gfa05c, gga01c, gga31i, ggc24f, ggc30f, '1', gga09c -- from ggm03 b with (nolock), ggm01 a with (nolock), gfm01 c with (nolock) -- where a.gga00c = b.ggc00c -- and a.gga01c = b.ggc01c -- and b.ggc00c = c.gfa00c -- and b.ggc03c = c.gfa01c -- and a.gga00c = @compid -- and b.ggc33d >= @fromdate -- and b.ggc33d <= @todate -- and b.ggc15c >= @fromempl -- and b.ggc15c <= @toempl -- order by ggc15c, gga80d -- -- -- 还款,卖产品第三销售 -- insert #work_detail_ceshi(spid, compid, person_id, action_id, srvdate, code, name, payway, amt, amt2, amt3, quan, cost, stdprice, share_rate, prj_type, billid, gender, comm, ex_rate, paycode, cardid) -- select @spid, @compid, ggc17c, 9, ggc33d, ggc03c, gfa03c, '还款', ggc11f, ggc25f, ggc25f-isnull(ggc13f, 0), ggc05f, ggc13f, ggc08f, ggc18f, gfa05c, gga01c, gga31i, ggc26f, ggc30f, '1', gga09c -- from ggm03 b with (nolock), ggm01 a with (nolock), gfm01 c with (nolock) -- where a.gga00c = b.ggc00c -- and a.gga01c = b.ggc01c -- and b.ggc00c = c.gfa00c -- and b.ggc03c = c.gfa01c -- and a.gga00c = @compid -- and b.ggc33d >= @fromdate -- and b.ggc33d <= @todate -- and b.ggc17c >= @fromempl -- and b.ggc17c <= @toempl -- order by ggc17c, gga80d -- 还款的产品, 在此体现业绩, 所以日期是按还款日期来统计的 insert #work_detail_ceshi(spid, compid, person_id, action_id, srvdate, code, name, payway, amt, amt2, amt3, quan, cost, stdprice, share_rate, prj_type, billid, gender, comm, ex_rate, paycode, cardid) select @spid, @compid, ggc12c, 9, ggc33d, ggc03c, gfa03c, gsb03c+ '(还款)', ggc11f, ggc21f, ggc21f - isnull(ggc13f, 0), ggc05f, ggc13f, ggc08f, isnull(ggc14f, 1), gfa05c, gga01c, gga31i, ggc22f, ggc30f, gsc04c, gga09c from ggm03 b with (nolock), ggm01 a with (nolock), gfm01 c with (nolock),gsm03 d with(nolock),gsm02 e with(nolock) where a.gga00c = b.ggc00c and a.gga01c = b.ggc01c and b.ggc00c = c.gfa00c and b.ggc03c = c.gfa01c and a.gga00c = @compid and b.ggc33d >= @fromdate -- 还款日期 and b.ggc33d <= @todate -- 还款日期 and b.ggc12c >= @fromempl and b.ggc12c <= @toempl and b.ggc00c = d.gsc00c and b.ggc34c = d.gsc01c and d.gsc00c = e.gsb00c and d.gsc04c = e.gsb02c and e.gsb01c = '3' order by ggc12c, gga80d -- 还款,卖产品第二销售 insert #work_detail_ceshi(spid, compid, person_id, action_id, srvdate, code, name, payway, amt, amt2, amt3, quan, cost, stdprice, share_rate, prj_type, billid, gender, comm, ex_rate, paycode, cardid) select @spid, @compid, ggc15c, 9, ggc33d, ggc03c, gfa03c, gsb03c+ '(还款)', ggc11f, ggc23f, ggc23f - isnull(ggc13f, 0), ggc05f, ggc13f, ggc08f, ggc16f, gfa05c, gga01c, gga31i, ggc24f, ggc30f, gsc04c, gga09c from ggm03 b with (nolock), ggm01 a with (nolock), gfm01 c with (nolock) ,gsm03 d with(nolock),gsm02 e with(nolock) where a.gga00c = b.ggc00c and a.gga01c = b.ggc01c and b.ggc00c = c.gfa00c and b.ggc03c = c.gfa01c and a.gga00c = @compid and b.ggc33d >= @fromdate and b.ggc33d <= @todate and b.ggc15c >= @fromempl and b.ggc15c <= @toempl and b.ggc00c = d.gsc00c and b.ggc34c = d.gsc01c and d.gsc00c = e.gsb00c and d.gsc04c = e.gsb02c and e.gsb01c = '3' order by ggc15c, gga80d -- 还款,卖产品第三销售 insert #work_detail_ceshi(spid, compid, person_id, action_id, srvdate, code, name, payway, amt, amt2, amt3, quan, cost, stdprice, share_rate, prj_type, billid, gender, comm, ex_rate, paycode, cardid) select @spid, @compid, ggc17c, 9, ggc33d, ggc03c, gfa03c, gsb03c+ '(还款)', ggc11f, ggc25f, ggc25f-isnull(ggc13f, 0), ggc05f, ggc13f, ggc08f, ggc18f, gfa05c, gga01c, gga31i, ggc26f, ggc30f, gsc04c, gga09c from ggm03 b with (nolock), ggm01 a with (nolock), gfm01 c with (nolock) ,gsm03 d with(nolock),gsm02 e with(nolock) where a.gga00c = b.ggc00c and a.gga01c = b.ggc01c and b.ggc00c = c.gfa00c and b.ggc03c = c.gfa01c and a.gga00c = @compid and b.ggc33d >= @fromdate and b.ggc33d <= @todate and b.ggc17c >= @fromempl and b.ggc17c <= @toempl and b.ggc00c = d.gsc00c and b.ggc34c = d.gsc01c and d.gsc00c = e.gsb00c and d.gsc04c = e.gsb02c and e.gsb01c = '3' order by ggc17c, gga80d end -- Added by WUXMM 2004/11/29 -- 设置标志 -- update #work_detail_ceshi -- set cls_flag = 1 -- where spid = @spid and (action_id >= 2 and action_id <= 8) or (action_id >= 10 and action_id <= 11) update #work_detail_ceshi set cls_flag = 1 where spid = @spid and action_id = 2 update #work_detail_ceshi set cls_flag = 1 where spid = @spid and action_id = 3 update #work_detail_ceshi set cls_flag = 1 where spid = @spid and action_id = 4 update #work_detail_ceshi set cls_flag = 1 where spid = @spid and action_id = 5 update #work_detail_ceshi set cls_flag = 1 where spid = @spid and action_id = 6 update #work_detail_ceshi set cls_flag = 1 where spid = @spid and action_id = 7 update #work_detail_ceshi set cls_flag = 1 where spid = @spid and action_id = 8 update #work_detail_ceshi set cls_flag = 1 where spid = @spid and action_id = 10 update #work_detail_ceshi set cls_flag = 1 where spid = @spid and action_id = 11 update #work_detail_ceshi set cls_flag = 1 where spid = @spid and action_id = 15 update #work_detail_ceshi set cls_flag = 1 where spid = @spid and action_id = 16 update #work_detail_ceshi set cls_flag = 1 where spid = @spid and action_id = 18 update #work_detail_ceshi set cls_flag = 1 where spid = @spid and action_id = 19 update #work_detail_ceshi set cls_flag = 1 where spid = @spid and action_id = 20 update #work_detail_ceshi set cls_flag = 1 where spid = @spid and action_id = 21 update #work_detail_ceshi set cls_flag = 1 where spid = @spid and action_id = 23 update #work_detail_ceshi set cls_flag = 1 where spid = @spid and action_id = 24 update #work_detail_ceshi set cls_flag = 2 where spid = @spid and action_id = 9 update #work_detail_ceshi set cls_flag = 3, cardid = code where spid = @spid and (action_id = 1 or action_id = 0) --卡销售与充值 --Start add by lm 2015-08-05 create table #personprecept ( id int identity(1,1) not null, ppt03c varchar(20) not null ) insert #personprecept(ppt03c) select ppt03c from personprecept with(nolock) where ppt00c = @compid and ppt02c in (select pre01c from precept with (nolock) where pre03i = 1 and pre00c = ppt00c and isnull(pre22i, 0) = 0) and ((ppt03c >= @fromempl and ppt03c <= @toempl) or @fromempl = '*') -- 创建循环变量 declare @HasRecord int declare @idd int set @HasRecord = 1 declare @ppt03c nvarchar(50) -- 按店业绩分段计算按天来 while(@HasRecord = 1) begin if exists(select 1 from #personprecept) begin set @HasRecord = 1 select top 1 @ppt03c = ppt03c ,@idd = id from #personprecept insert #work_detail_ceshi(spid, person_id, action_id, code, compid, srvdate) values(@spid, @ppt03c, 101, '店总业绩', @compid, '--') insert workdetail_by_empl(spid, person_id, name, compid, fromdate, todate) values(@spid, @ppt03c, '店总业绩', @compid, @fromdate, @todate) -- 清除已处理的数据 delete #personprecept where id = @idd end else begin set @HasRecord = 0 end end drop table #personprecept end GO /****** Object: StoredProcedure [dbo].[upg_consume_statistic_ceshi] Script Date: 06/12/2017 06:19:46 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_consume_statistic_ceshi] ( @flag varchar(2), -- 是否列印产品资料(1-列印项目资料 2-列印产品资料 3-列印项目和产品资料) @conditionP nvarchar(1024), -- 项目资料条件执行语句(前台组织好的) @conditionG nvarchar(1024), -- 产品资料执行语句(前台组织好的) @ret_type int, -- 返回类型, 1-原来的完整的明细 2-按项目分组合计 3-按项目类别 @addtoSMS int, @compid varchar(10), @project int, @goods int, @paycode varchar(20), @fromtime varchar(6), @totime varchar(6), @fromdate varchar(8), @todate varchar(8), @type varchar(4), @frombill varchar(20), @tobill varchar(20), @fromcard varchar(20), @tocard varchar(20), @fromcardtype varchar(20), @tocardtype varchar(20), @sort int, @currentpageindex int, @pagesize int, @pageall int --1:查询所有,2:查询部分 ) as--WUXM, 2004/12/26 begin exec upg_paymode_to_java @paycode,@paycode output exec upg_paymode_to_java @conditionP,@conditionP output exec upg_paymode_to_java @conditionG,@conditionG output declare @sqlpage nvarchar(1024) declare @pagecount int create table #struct -- 产品消费单的临时表 ( id int identity not null, billid varchar(20) null, -- 单号 billtype varchar(5) null, -- 单据别 type_gz_gn varchar(5) null, -- 单据别(gz,gn) cardno varchar(20) null, -- 会员卡编号 gender int null, -- 性别 checkdate varchar(10) null, -- 结账日期 checktime varchar(8) null, -- 结账时间 kind varchar(200) null, -- 品种 price float null, -- 实收单价 quantity float null, -- 数量 summoney float null, -- 金额 itemtype int null, -- 类别, 1-项目,2-产品 srv_type varchar(20) null, -- 大工类型,指定或轮班 staff01 varchar(20) null, -- 第一销售 staff01_p float null, -- 第一销售分享比例----------ADD BY LEIJI 2012-7-23 staff01_kpi float null, -- 第一销售业绩--------------add by lm 2015.12.29 staff02 varchar(20) null, -- 第二销售 staff02_p float null, -- 第二销售分享比例----------ADD BY LEIJI 2012-7-23 staff02_kpi float null, -- 第二销售业绩--------------add by lm 2015.12.29 staff03 varchar(20) null, -- 第三销售 staff03_p float null, -- 第三销售分享比例----------ADD BY LEIJI 2012-7-23 staff03_kpi float null, -- 第二销售业绩--------------add by lm 2015.12.29 payway varchar(40) null, -- 项目支付方式 paycode varchar(40) null, -- 项目支付方式 代码 --------add by lm on 2015/03/17 memid varchar(20) null, -- 会员编号 confirmer varchar(20) null, -- 复核人 m_name varchar(100) null, -- 会员名称 cardtype varchar(100) null, -- 卡类别名称 --add by leiji on 2013年4月10日 cardtypecode varchar(100) null, -- 卡类别代码 --add by limin on 2015/01/31 isold int null, -- 是否老客 person_quan float null, -- 客户数量 srv_type02 varchar(20) null, -- 中工类型 --add by lyj srv_type03 varchar(20) null, -- 小工类型 srv_type04 varchar(20) null, -- 四工类型 staff04 varchar(20) null, -- 四工代码, staff04_kpi float null, -- 第四销售业绩--------------add by lm 2015.12.29 acctdate varchar(20) null, -- 帐务日期,gga80d compid varchar(10) null, -- 公司别--add by lyj compidname varchar(100) null, --公司名称 billtotalamt float default 0 null, -- 单据的总金额 --add by lyj stdprice float null, -- 标准单价 stdamt float null, -- 标准金额 card_act float null, -- 卡付实际营业额 memo varchar(1000) null, -- 备注 pagecount int null, dsum float null,--总金额 dsumquan float null,--总单价 dcustcount float null,--总人数 card_act_sum float null,--总卡付金额 sum_billtotalamt float null,--总单据金额 sum_summoney float null,--总项目金额 m_femalecount float null,--女客 m_femaleamt float null, m_malecount float null,--男客 m_maleamt float null, m_nmember float null,--会员 m_nmemberamt float null,--会员金额 m_ncustomer float null,--散客 m_ncustomeramt float null,--散客金额 m_noldcust float null,--老客 m_noldcustamt float null,--老客金额 m_nnewcust float null,--新客 m_nnewcustamt float null,--新客金额 primary key (id), ) create nonclustered index idx_struct on #struct(billid,billtype,cardno) create table #struct_2 -- 产品消费单的临时表 ( id int identity not null, billid varchar(20) null, -- 单号 billtype varchar(5) null, -- 单据别 type_gz_gn varchar(5) null, -- 单据别(gz,gn) cardno varchar(20) null, -- 会员卡编号 gender int null, -- 性别 checkdate varchar(10) null, -- 结账日期 checktime varchar(8) null, -- 结账时间 kind varchar(200) null, -- 品种 price float null, -- 实收单价 quantity float null, -- 数量 summoney float null, -- 金额 itemtype int null, -- 类别, 1-项目,2-产品 srv_type varchar(20) null, -- 大工类型,指定或轮班 staff01 varchar(20) null, -- 第一销售 staff01_p float null, -- 第一销售分享比例----------ADD BY LEIJI 2012-7-23 staff01_kpi float null, -- 第一销售业绩--------------add by lm 2015.12.29 staff02 varchar(20) null, -- 第二销售 staff02_p float null, -- 第二销售分享比例----------ADD BY LEIJI 2012-7-23 staff02_kpi float null, -- 第二销售业绩--------------add by lm 2015.12.29 staff03 varchar(20) null, -- 第三销售 staff03_p float null, -- 第三销售分享比例----------ADD BY LEIJI 2012-7-23 staff03_kpi float null, -- 第二销售业绩--------------add by lm 2015.12.29 payway varchar(40) null, -- 项目支付方式 paycode varchar(40) null, -- 项目支付方式 代码 --------add by lm on 2015/03/17 memid varchar(20) null, -- 会员编号 confirmer varchar(20) null, -- 复核人 m_name varchar(100) null, -- 会员名称 cardtype varchar(100) null, -- 卡类别名称 --add by leiji on 2013年4月10日 cardtypecode varchar(100) null, -- 卡类别代码 --add by limin on 2015/01/31 isold int null, -- 是否老客 person_quan float null, -- 客户数量 srv_type02 varchar(20) null, -- 中工类型 --add by lyj srv_type03 varchar(20) null, -- 小工类型 srv_type04 varchar(20) null, -- 四工类型 staff04 varchar(20) null, -- 四工代码, staff04_kpi float null, -- 第四销售业绩--------------add by lm 2015.12.29 acctdate varchar(20) null, -- 帐务日期,gga80d compid varchar(10) null, -- 公司别--add by lyj compidname varchar(100) null, --公司名称 billtotalamt float default 0 null, -- 单据的总金额 --add by lyj stdprice float null, -- 标准单价 stdamt float null, -- 标准金额 card_act float null, -- 卡付实际营业额 memo varchar(1000) null, -- 备注 isgoods int null, -- primary key (id), ) create nonclustered index idx_struct_2 on #struct_2(billid,billtype,cardno) create table #struct_page -- 产品消费单的临时表 ( id int identity not null, billid varchar(20) null, -- 单号 billtype varchar(5) null, -- 单据别 type_gz_gn varchar(5) null, -- 单据别(gz,gn) cardno varchar(20) null, -- 会员卡编号 gender int null, -- 性别 checkdate varchar(10) null, -- 结账日期 checktime varchar(8) null, -- 结账时间 kind varchar(200) null, -- 品种 price float null, -- 实收单价 quantity float null, -- 数量 summoney float null, -- 金额 itemtype int null, -- 类别, 1-项目,2-产品 srv_type varchar(20) null, -- 大工类型,指定或轮班 staff01 varchar(20) null, -- 第一销售 staff01_p float null, -- 第一销售分享比例----------ADD BY LEIJI 2012-7-23 staff01_kpi float null, -- 第一销售业绩--------------add by lm 2015.12.29 staff02 varchar(20) null, -- 第二销售 staff02_p float null, -- 第二销售分享比例----------ADD BY LEIJI 2012-7-23 staff02_kpi float null, -- 第二销售业绩--------------add by lm 2015.12.29 staff03 varchar(20) null, -- 第三销售 staff03_p float null, -- 第三销售分享比例----------ADD BY LEIJI 2012-7-23 staff03_kpi float null, -- 第二销售业绩--------------add by lm 2015.12.29 payway varchar(40) null, -- 项目支付方式 paycode varchar(40) null, -- 项目支付方式 代码 --------add by lm on 2015/03/17 memid varchar(20) null, -- 会员编号 confirmer varchar(20) null, -- 复核人 m_name varchar(100) null, -- 会员名称 cardtype varchar(100) null, -- 卡类别名称 --add by leiji on 2013年4月10日 cardtypecode varchar(100) null, -- 卡类别代码 --add by limin on 2015/01/31 isold int null, -- 是否老客 person_quan float null, -- 客户数量 srv_type02 varchar(20) null, -- 中工类型 --add by lyj srv_type03 varchar(20) null, -- 小工类型 srv_type04 varchar(20) null, -- 四工类型 staff04 varchar(20) null, -- 四工代码, staff04_kpi float null, -- 第四销售业绩--------------add by lm 2015.12.29 acctdate varchar(20) null, -- 帐务日期,gga80d compid varchar(10) null, -- 公司别--add by lyj compidname varchar(100) null, --公司名称 billtotalamt float default 0 null, -- 单据的总金额 --add by lyj stdprice float null, -- 标准单价 stdamt float null, -- 标准金额 card_act float null, -- 卡付实际营业额 memo varchar(1000) null, -- 备注 pagecount int null, primary key (id), ) create table #struct_customer ( compid varchar(10) not null, -- 公司别--add by lyj billid varchar(20) not null, -- 单号 billtotalamt float null, -- 金额 gender int null, -- 性别 isold int null, -- 是否老客 cardno varchar(20) null, -- 会员卡编号 person_quan float null, -- 客户数量 primary key(compid,billid) ) create table #struct_pay_sum ( id int identity not null, paycode varchar(40) null, -- 项目支付方式 代码 --------add by lm on 2015/03/17 payway varchar(40) null, -- 项目支付方式 pay_count int null, pay_amt float null, primary key (id) ) create nonclustered index index_struct_pay_sum on #struct_pay_sum(paycode) create table #struct_page_sum -- 产品消费单的临时表 ( id int identity not null, dsum float null,--总金额 dsumquan float null,--总单价 dcustcount float null,--总人数 card_act_sum float null,--总卡付金额 sum_billtotalamt float null,--总单据金额 sum_summoney float null,--总项目金额 m_femalecount float null,--女客 m_femaleamt float null, m_malecount float null,--男客 m_maleamt float null, m_nmember float null,--会员 m_ncustomer float null,--散客 m_noldcust float null,--老客 m_nnewcust float null,--新客 m_nmemberamt float null,--会员 m_ncustomeramt float null,--散客 m_noldcustamt float null,--老客 m_nnewcustamt float null,--新客 primary key (id), ) create table #gcm11 ( gcm00c varchar (20) not null, gcm01c varchar (20) not null, gcm02c varchar (20) null, gcm03f float null ) create nonclustered index idx_gcm11 on #gcm11(gcm00c,gcm01c,gcm02c) create table #gnm03 ( gnc00c varchar (20) not null, gnc01c varchar (20) not null, gnc02c varchar (20) null, gnc03f float null ) create nonclustered index idx_gnm03 on #gnm03(gnc00c,gnc01c,gnc02c) create table #gtc06 ( gth00c varchar (20) not null, gth01c varchar (20) not null, gth02c varchar (20) null, gth04f float null ) create nonclustered index idx_gtc06 on #gtc06(gth00c,gth01c,gth02c) create table #gtc05 ( gtf00c varchar (20) not null, gtf01c varchar (20) not null, gtf02c varchar (20) null, gtf04f float null ) create nonclustered index idx_gtc05 on #gtc05(gtf00c,gtf01c,gtf02c) create table #gsm03 ( gsc00c varchar(20) not null, gsc01c varchar(20) not null, gsc02c varchar(2) not null, gsc05f float null, ) create clustered index idx_gsm03 on #gsm03(gsc00c,gsc01c,gsc02c) insert #gsm03 (gsc00c,gsc01c,gsc02c,gsc05f) select gsc00c,gsc01c,gsc02c,gsc05f from gsm03 with(nolock) where gsc00c = @compid and (gsc10d >= @fromdate and gsc10d <= @todate) and (gsc01c = @type or @type = '*') declare @sql_p nvarchar(2048) declare @sql_g nvarchar(2048) declare @staff01 varchar(20) declare @staff02 varchar(20) declare @staff03 varchar(20) declare @staff04 varchar(20) declare @sp031f float declare @incompid varchar(10) select @incompid = @compid select @sp031f = gsa03f from gsm01 with (nolock) where gsa00c = @compid and gsa01c = 'SP031' select @staff01 = isnull(gsb03c, '') from gsm02 with (nolock) where gsb01c = 'AK' and gsb02c = '1' select @staff02 = isnull(gsb03c, '') from gsm02 with (nolock) where gsb01c = 'AK' and gsb02c = '2' select @staff03 = isnull(gsb03c, '') from gsm02 with (nolock) where gsb01c = 'AK' and gsb02c = '3' select @staff04 = isnull(gsb03c, '') from gsm02 with (nolock) where gsb01c = 'AK' and gsb02c = '4' -- modify by lyj 添加账务时间的过滤条件 2007-12-14 账务时间只精确到分钟 -- begin if isnull(@fromtime, '') = '' begin set @fromtime = '000000' set @totime = '240000' end else begin set @fromtime = substring(@fromtime, 1, 6) end if isnull(@totime, '') = '' set @totime = '240000' else set @totime = substring(@totime, 1, 6) -- end if @flag = '1' begin if @sp031f = 2 -- 出院结帐 begin select @sql_p = 'insert #struct_2(billid, billtype, cardno, checkdate, checktime, kind, price, quantity, summoney, itemtype, staff01, staff02, staff03, srv_type, gender, payway, paycode, memid, confirmer, isold, person_quan, srv_type02, srv_type03, srv_type04, staff04, acctdate, billtotalamt, compid, stdprice, m_name, memo ) select gga01c, isnull(gga29c, ''gx''), isnull(gga09c, ''''), gla96d, gla04t, ggb03c, ggb10f, ggb05f, ggb11f, 1, ggb12c, ggb13c, ggb14c, ggb15c, gla10i, gsb03c, gsb02c, gla27c, gga93c, gga13f, gga14f, ggb16c, ggb19c, ggb25c, ggb26c, gga80d, isnull(gga16f, 0), gga00c, ggb08f, gga06c, case when isnull(ggm02.memo, '''') = '''' then '''' else ''单据备注: '' + ggm02.memo end from ggm01 with (nolock), ggm02 with (nolock), glm01 with (nolock), gsm02 with (nolock) , gam26 z with (nolock) where gga00c = ggb00c and gga01c = ggb01c and gga00c = gla00c and gga30c = gla01c and ggb00c *= gsb00c and ggb27c *= gsb02c and gga00c = gaz02c and isnull(gla80d, '''') <> '''' and gsb01c = ''3'''+ @conditionP end else -- 收银结帐 begin select @sql_p = 'insert #struct_2(billid, billtype, cardno, checkdate, checktime, kind, price, quantity, summoney, itemtype, staff01, staff02, staff03, srv_type, gender, payway, paycode, memid, confirmer, isold, person_quan, srv_type02, srv_type03, srv_type04, staff04, acctdate, billtotalamt, compid, stdprice, m_name, memo) select gga01c, isnull(gga29c, ''gx''), isnull(gga09c, ''''), gga02d, gga04t, ggb03c, ggb10f, ggb05f, ggb11f, 1, ggb12c, ggb13c, ggb14c, ggb15c, gga31i, gsb03c, gsb02c, gga05c, gga93c, gga13f, gga14f, ggb16c, ggb19c, ggb25c, ggb26c, gga80d, isnull(gga16f, 0), gga00c, ggb08f, gga06c, case when isnull(ggm02.memo, '''') = '''' then '''' else ''单据备注: '' + ggm02.memo end from ggm01 with (nolock), ggm02 with (nolock), gsm02 with (nolock) , gam26 z with (nolock) where gga00c = ggb00c and gga01c = ggb01c and ggb00c *= gsb00c and ggb27c *= gsb02c and gga00c = gaz02c and gsb01c = ''3'''+ @conditionP end exec sp_executesql @sql_p end else begin if @flag = '2' -- 产品销售 begin select @sql_g = 'insert #struct_2(billid, billtype, cardno, checkdate, checktime, kind, price, quantity, summoney, itemtype, staff01, staff01_p, staff02, staff02_p, staff03, staff03_p, gender, memid, confirmer, isold, person_quan, acctdate, billtotalamt, compid, stdprice, m_name) select gga01c, isnull(gga29c, ''gx''), isnull(gga09c, ''''), gga02d, gga04t, ggc03c, ggc10f, ggc05f, ggc11f, 2, ggc12c, ggc14f, ggc15c, ggc16f, ggc17c, ggc18f, gga31i, gga05c, gga93c, gga13f, gga14f, gga80d, isnull(gga16f, 0), gga00c, ggc08f, gga06c from ggm01 with (nolock) , gam26 with (nolock) , ggm03 with (nolock) where gga00c = ggc00c and gga01c = ggc01c and gga00c = gaz02c ' + @conditionG exec sp_executesql @sql_g end else -- 项目 + 产品销售 begin if( @type = 'gx' or @type = '*') begin if @sp031f = 2 -- 出院结帐 begin if (@project = 1)-- 出院结帐 begin select @sql_p = 'insert #struct_2(billid, billtype,type_gz_gn, cardno, checkdate, checktime, kind, price, quantity, summoney, itemtype, staff01, staff02, staff03, srv_type, gender, payway, paycode, memid, confirmer, isold, person_quan, srv_type02, srv_type03, srv_type04, staff04, acctdate, billtotalamt, compid, stdprice, m_name, memo, staff01_kpi, staff02_kpi, staff03_kpi, staff04_kpi,card_act,isgoods) select gga01c, isnull(gga29c, ''gx''),isnull(gga29c, ''gx''), isnull(gga09c, ''''), gla96d, gla04t, ggb03c, ggb10f, ggb05f, ggb11f, 1, ggb12c, ggb13c, ggb14c, ggb15c, gla10i, gsb03c, gsb02c, gla27c, gga93c, gga13f, gga14f, ggb16c, ggb19c, ggb25c, ggb26c, gga80d, isnull(gga16f, 0), gga00c, ggb08f, gga06c, case when isnull(ggm02.memo, '''') = '''' then '''' else ''单据备注: '' + ggm02.memo end, ggb20f, ggb21f, ggb22f, ggb35f,isnull(isnull(ggb11f, 0), 0),0 from ggm01 with (nolock), ggm02 with (nolock), glm01 with (nolock), gsm02 with (nolock) , gam26 with (nolock) where gga00c = ggb00c and gga01c = ggb01c and gga00c = gla00c and gga30c = gla01c and ggb00c *= gsb00c and ggb27c *= gsb02c and gga00c = gaz02c and isnull(gla80d, '''') <> '''' and gsb01c = ''3''' + @conditionP exec sp_executesql @sql_p end if (@goods = 1)-- 出院结帐 begin select @sql_g = 'insert #struct_2(billid, billtype,type_gz_gn, cardno, checkdate, checktime, kind, price, quantity, summoney, itemtype, staff01, staff01_p, staff02, staff02_p, staff03, staff03_p, gender, payway, paycode, memid, confirmer, isold, person_quan, acctdate, billtotalamt, compid, stdprice, m_name, staff01_kpi, staff02_kpi, staff03_kpi,card_act,isgoods) select gga01c, isnull(gga29c, ''gx''),isnull(gga29c, ''gx''), isnull(gga09c, ''''), gla96d, gla04t, ggc03c, ggc10f, ggc05f, ggc11f, 2, ggc12c, ggc14f, ggc15c, ggc16f, ggc17c, ggc18f, gla10i, gsb03c, gsb02c, gla27c, gga93c, gga13f, gga14f, gga80d, isnull(gga16f, 0), gga00c, ggc08f, gga06c, ggc21f, ggc23f, ggc25f,isnull(isnull(ggc11f, 0), 0),1 from ggm01 with (nolock), ggm03 with (nolock), glm01 with (nolock), gsm02 with (nolock) , gam26 with (nolock) where gga00c = ggc00c and gga01c = ggc01c and gga00c = gla00c and gga30c = gla01c and ggc00c *= gsb00c and ggc27c *= gsb02c and gga00c = gaz02c and isnull(gla80d, '''') <> '''' and gsb01c = ''3''' + @conditionG exec sp_executesql @sql_g end end else -- 收银结帐 begin if (@project = 1)-- 收银结帐 begin select @sql_p = 'insert #struct_2(billid, billtype,type_gz_gn, cardno, checkdate, checktime, kind, price, quantity, summoney, itemtype, staff01, staff02, staff03, srv_type, gender, payway, paycode, memid, confirmer, isold, person_quan, srv_type02, srv_type03, srv_type04, staff04, acctdate, billtotalamt, compid, stdprice, m_name, memo, staff01_kpi, staff02_kpi, staff03_kpi, staff04_kpi,card_act,isgoods) select gga01c, isnull(gga29c, ''gx''),isnull(gga29c, ''gx''), isnull(gga09c, ''''), gga02d, gga04t, ggb03c, ggb10f, ggb05f, ggb11f, 1, ggb12c, ggb13c, ggb14c, ggb15c, gga31i, gsb03c, gsb02c, gga05c, gga93c, gga13f, gga14f, ggb16c, ggb19c, ggb25c, ggb26c, gga80d, isnull(gga16f, 0), gga00c, ggb08f, gga06c, case when isnull(ggm02.memo, '''') = '''' then '''' else ''单据备注: '' + ggm02.memo end, ggb20f, ggb21f, ggb22f, ggb35f,isnull(isnull(ggb11f, 0), 0),0 from ggm01 with (nolock), ggm02 with (nolock), gsm02 with (nolock) , gam26 with (nolock) where gga00c = ggb00c and gga01c = ggb01c and ggb00c *= gsb00c and ggb27c *= gsb02c and gga00c = gaz02c and gsb01c = ''3''' + @conditionP exec sp_executesql @sql_p end if (@goods = 1)-- 收银结帐 begin select @sql_g = 'insert #struct_2(billid, billtype,type_gz_gn, cardno, checkdate, checktime, kind, price, quantity, summoney, itemtype, staff01, staff01_p, staff02, staff02_p, staff03, staff03_p, gender, payway, paycode, memid, confirmer, isold, person_quan, acctdate, billtotalamt, compid, stdprice, m_name, staff01_kpi, staff02_kpi, staff03_kpi,card_act,isgoods) select gga01c, isnull(gga29c, ''gx''),isnull(gga29c, ''gx''), isnull(gga09c, ''''), gga02d, gga04t, ggc03c, ggc10f, ggc05f, ggc11f, 2, ggc12c, ggc14f, ggc15c, ggc16f, ggc17c, ggc18f, gga31i, gsb03c, gsb02c, gga05c, gga93c, gga13f, gga14f, gga80d, isnull(gga16f, 0), gga00c, ggc08f, gga06c, ggc21f, ggc23f, ggc25f,isnull(isnull(ggc11f, 0), 0),1 from ggm01 with (nolock), ggm03 with (nolock), gsm02 with (nolock) , gam26 with (nolock) where gga00c = ggc00c and gga01c = ggc01c and ggc00c *= gsb00c and ggc27c *= gsb02c and gga00c = gaz02c and gsb01c = ''3'''+ @conditionG exec sp_executesql @sql_g end end if( @type = 'gx' or @type = '*') begin declare @sp215f float --储值方式支付是否作为卡付实际营业额 select @sp215f = gsa03f from gsm01 with (nolock) where gsa00c = @compid and gsa01c = 'SP212' if(@sp215f = 1) begin update a set a.card_act = isnull(a.card_act,0) * isnull(gso03i, 1) from #struct_2 a with (nolock), gsm15 b where a.compid = b.gso00c and a.paycode = b.gso02c and b.gso01c = '3' and b.gso09f = 1 and (paycode = '9' or (paycode >= 'A' and paycode <= 'Z') or paycode = '#') and paycode not in ('WX','ZFB') and isgoods = 0 update a set a.card_act = isnull(a.card_act,0) * isnull(gso03i, 1) * isnull(gak22f, 1) from #struct_2 a with (nolock), gsm15 b, gam10 c where a.compid = b.gso00c and a.paycode = b.gso02c and a.compid = c.gak00c and a.cardno = c.gak01c and b.gso01c = '3' and b.gso09f = 1 and paycode = '4' and isgoods = 0 update a set a.card_act = isnull(a.card_act,0) * isnull(gso06f, 1) from #struct_2 a with (nolock), gsm15 b where a.compid = b.gso00c and a.paycode = b.gso02c and b.gso01c = '3' and b.gso10f = 1 and (paycode = '9' or (paycode >= 'A' and paycode <= 'Z') or paycode = '#') and paycode not in ('WX','ZFB') and isgoods = 1 update a set a.card_act = isnull(a.card_act,0) * isnull(gso06f, 1) * isnull(gak24f, 1) from #struct_2 a with (nolock), gsm15 b, gam10 c where a.compid = b.gso00c and a.paycode = b.gso02c and a.compid = c.gak00c and a.cardno = c.gak01c and b.gso01c = '3' and b.gso10f = 1 and paycode = '4' and isgoods = 1 update a set a.card_act = 0 from #struct_2 a where paycode not in ('4','9','#') and (paycode <'A' or paycode > 'Z') end else begin update a set a.card_act = isnull(a.card_act,0) * isnull(gso03i, 1) from #struct_2 a with (nolock), gsm15 b where a.compid = b.gso00c and a.paycode = b.gso02c and b.gso01c = '3' and b.gso09f = 1 and ((paycode >= 'A' and paycode <= 'Z') or paycode = '#') and paycode not in ('WX','ZFB') and isgoods = 0 update a set a.card_act = isnull(a.card_act,0) * isnull(gso03i, 1) * isnull(gak22f, 1) from #struct_2 a with (nolock), gsm15 b, gam10 c where a.compid = b.gso00c and a.paycode = b.gso02c and a.compid = c.gak00c and a.cardno = c.gak01c and b.gso01c = '3' and b.gso09f = 1 and paycode = '4' and isgoods = 0 update a set a.card_act = isnull(a.card_act,0) * isnull(gso06f, 1) from #struct_2 a with (nolock), gsm15 b where a.compid = b.gso00c and a.paycode = b.gso02c and b.gso01c = '3' and b.gso10f = 1 and ((paycode >= 'A' and paycode <= 'Z') or paycode = '#') and paycode not in ('WX','ZFB') and isgoods = 1 update a set a.card_act = isnull(a.card_act,0) * isnull(gso06f, 1) * isnull(gak24f, 1) from #struct_2 a with (nolock), gsm15 b, gam10 c where a.compid = b.gso00c and a.paycode = b.gso02c and a.compid = c.gak00c and a.cardno = c.gak01c and b.gso01c = '3' and b.gso10f = 1 and paycode = '4' and isgoods = 1 update a set a.card_act = 0 from #struct_2 a where paycode not in ('4','#') and (paycode <'A' or paycode > 'Z') end end end end --Start 增加卖卡和充值 add by lm 2015.12.29 if(@type = '*' or @type = 'gz') begin --卡充值 insert #struct_2(billid, billtype,type_gz_gn, cardno, checkdate, acctdate,checktime, memid, itemtype, staff01, staff02, staff03, billtotalamt, stdamt, compid, staff01_kpi, staff02_kpi, staff03_kpi, kind, payway, confirmer, quantity) select gcl01c, 'gz' ,'gz', gcl03c, gcl02d, gcl02d, gcl56t, gcl03c,3, gcl09c, gcl17c,gcl19c,--+':'+gcl68f+gcl69c+':'+gcl70f+gcl71c+':'+gcl72f+gcl73c+':'+gcl74f, gcl07f,gcl07f, gcl00c, gcl58f, gcl60f, gcl62f, gsb03c,[dbo].[payway_gsm03](gcl00c,gcl01c,'gz'),gcl93c, 1 from gcm10 a with(nolock), gcm01 b with(nolock), gsm02 c with (nolock),gam26 z with (nolock) where c.gsb00c = a.gcl00c and c.gsb02c = a.gcl04c and a.gcl00c = b.gca00c and a.gcl03c = b.gca01c and ((b.gca02c >= @fromcardtype and b.gca02c <= @tocardtype) or (@fromcardtype = '*') ) and b.gca00c = b.gca13d and c.gsb01c = 'O' and a.gcl00c = z.gaz02c and z.gaz01c = @compid and ( isnull(@paycode, '') = '' or a.gcl67c = @paycode or a.gcl69c = @paycode or a.gcl71c = @paycode or a.gcl73c = @paycode) and ((a.gcl03c >= @fromcard and a.gcl03c <= @tocard) or (@fromcard = '*')) and ((a.gcl01c >= @frombill and a.gcl01c <= @tobill) or (@frombill = '*')) and ((a.gcl80d >= @fromdate and a.gcl80d <= @todate) or (@fromdate = '*')) end if(@type = '*' or @type = 'gn') begin --卖卡 insert #struct_2(billid, billtype,type_gz_gn, cardno, checkdate, acctdate, checktime, memid, itemtype, staff01, staff02, staff03, billtotalamt, stdamt, compid, staff01_kpi, staff02_kpi, staff03_kpi, kind, payway, confirmer, quantity) select gna01c, 'gn','gn', gna13c, gna02d, gna02d, gna12t, gna13c,4, gna20c, gna28c,gna30c,--+':'+gnb16f+gnb17c+':'+gnb18f+gnb19c+':'+gnb20f+gnb21c+':'+gnb22f, gnb08f,0, gna00c,gna67f, gna69f, gna71f,gsb03c,[dbo].[payway_gsm03](gna00c,gna01c,'gn'),gna93c, 1 from gnm01 a with(nolock), gnm02 b with (nolock),gsm02 c,gam26 z with (nolock),gcm01 d with(nolock) where a.gna00c = b.gnb00c and a.gna01c = b.gnb01c and c.gsb00c = a.gna00c and c.gsb02c = b.gnb13i and a.gna00c = d.gca00c and a.gna13c = d.gca01c and ((d.gca02c >= @fromcardtype and d.gca02c <= @tocardtype) or (@fromcardtype = '*')) and d.gca00c = d.gca13d and c.gsb01c = 'O' and a.gna00c = z.gaz02c and z.gaz01c = @compid and ( isnull(@paycode, '') = '' or b.gnb15c = @paycode or b.gnb17c = @paycode or b.gnb19c = @paycode or b.gnb21c = @paycode) and ((a.gna13c >= @fromcard and a.gna13c <= @tocard) or (@fromcard = '*')) and ((a.gna01c >= @frombill and a.gna01c <= @tobill) or (@frombill = '*')) and ((a.gna80d >= @fromdate and a.gna80d <= @todate) or (@fromdate = '*')) end insert #struct_page (billid, billtype,type_gz_gn, cardno, checkdate, checktime, kind, price, quantity, summoney, itemtype, staff01, staff01_p, staff02, staff02_p, staff03, staff03_p, gender, payway, paycode, memid, confirmer, isold, person_quan, acctdate, billtotalamt, compid, stdprice, m_name, staff01_kpi, staff02_kpi, staff03_kpi,card_act) select billid, billtype,type_gz_gn, cardno, checkdate, checktime, kind, price, quantity, summoney, itemtype, staff01, staff01_p, staff02, staff02_p, staff03, staff03_p, gender, payway, paycode, memid, confirmer, isold, person_quan, acctdate, billtotalamt, compid, stdprice, m_name, staff01_kpi, staff02_kpi, staff03_kpi,card_act from #struct_2 end update #struct_page set gender = 0 where isnull(gender, -1) = -1 --start数据合计 if(@ret_type = 1) begin --start计算支付方式总合计 insert #struct_pay_sum (paycode,pay_count,pay_amt) select paycode,count(paycode),sum(summoney) from #struct_page where isnull(kind, '') <> '' group by paycode update a set a.payway = b.gsb03c from #struct_pay_sum a,gsm02 b with(nolock) where a.paycode = b.gsb02c and b.gsb01c = '3' and b.gsb00c = @compid --end --去掉男女客重复单据 insert #struct_customer(compid, billid, billtotalamt,gender,isold,cardno,person_quan) select distinct compid,billid,billtotalamt,gender,isold,cardno,person_quan from #struct_page where isnull(kind, '') <> '' and billtype = 'gx' declare @m_femalecount float declare @m_femaleamt float declare @m_malecount float declare @m_maleamt float declare @m_nmember float declare @m_nmemberamt float declare @m_ncustomer float declare @m_ncustomeramt float declare @m_noldcust float declare @m_noldcustamt float declare @m_nnewcust float declare @m_nnewcustamt float declare @dcustcount float declare @sum_billtotalamt float insert #struct_page_sum (dsum, dsumquan ,card_act_sum, sum_summoney) select sum(summoney),sum(quantity),sum(card_act),sum(summoney) from #struct_page where isnull(kind, '') <> '' select @m_femaleamt = sum(billtotalamt), @m_femalecount = sum(person_quan) from #struct_customer where gender = 0 select @sum_billtotalamt = sum(billtotalamt) from #struct_customer select @m_maleamt = sum(billtotalamt), @m_malecount = sum(person_quan) from #struct_customer where gender = 1 select @m_nnewcust = sum(person_quan),@m_nnewcustamt = sum(billtotalamt) from #struct_customer where isold = 0 select @m_noldcust = sum(person_quan),@m_noldcustamt = sum(billtotalamt) from #struct_customer where isold = 1 select @dcustcount = sum(person_quan) from #struct_customer select @m_ncustomer = sum(person_quan),@m_ncustomeramt = sum(billtotalamt) from #struct_customer where substring(cardno,1,2) = '散客' select @m_nmember = sum(person_quan),@m_nmemberamt = sum(billtotalamt) from #struct_customer where substring(cardno,1,2) <> '散客' select @pagecount = count(*) from #struct_page where isnull(kind, '') <> '' update a set a.m_femaleamt = @m_femaleamt, a.m_femalecount =@m_femalecount, a.m_maleamt = @m_maleamt, a.m_malecount = @m_malecount, a.m_noldcust = @m_noldcust, a.m_nnewcust = @m_nnewcust, a.m_ncustomer = @m_ncustomer, a.m_nmember = @m_nmember, a.dcustcount = @dcustcount, a.sum_billtotalamt = @sum_billtotalamt, a.m_noldcustamt = @m_noldcustamt, a.m_nnewcustamt = @m_nnewcustamt, a.m_ncustomeramt = @m_ncustomeramt, a.m_nmemberamt = @m_nmemberamt from #struct_page_sum a --end --start 数据提取 if(@pageall = 1) begin insert #struct (billid, billtype,type_gz_gn, cardno, checkdate, checktime, kind, price, quantity, summoney, itemtype, staff01, staff01_p, staff02, staff02_p, staff03, staff03_p, gender, payway, paycode, memid, confirmer, isold, person_quan, acctdate, billtotalamt, compid, stdprice, m_name, staff01_kpi, staff02_kpi, staff03_kpi,card_act) select billid, billtype,type_gz_gn, cardno, checkdate, checktime, kind, price, quantity, summoney, itemtype, staff01, staff01_p, staff02, staff02_p, staff03, staff03_p, gender, payway, paycode, memid, confirmer, isold, person_quan, acctdate, billtotalamt, compid, stdprice, m_name, staff01_kpi, staff02_kpi, staff03_kpi,card_act from #struct_page end else begin select @sqlpage = 'insert #struct (billid, billtype,type_gz_gn, cardno, checkdate, checktime, kind, price, quantity, summoney, itemtype, staff01, staff01_p, staff02, staff02_p, staff03, staff03_p, gender, payway, paycode, memid, confirmer, isold, person_quan, acctdate, billtotalamt, compid, stdprice, m_name, staff01_kpi, staff02_kpi, staff03_kpi,card_act) select top '+ cast(3 * @pagesize as nvarchar(20)) +'billid, billtype,type_gz_gn, cardno, checkdate, checktime, kind, price, quantity, summoney, itemtype, staff01, staff01_p, staff02, staff02_p, staff03, staff03_p, gender, payway, paycode, memid, confirmer, isold, person_quan, acctdate, billtotalamt, compid, stdprice, m_name, staff01_kpi, staff02_kpi, staff03_kpi,card_act from #struct_page where id > '+cast(@currentpageindex * @pagesize as nvarchar(20)) + 'order by id asc' exec sp_executesql @sqlpage end update a set a.pagecount = @pagecount from #struct a end else begin insert #struct (billid, billtype,type_gz_gn, cardno, checkdate, checktime, kind, price, quantity, summoney, itemtype, staff01, staff01_p, staff02, staff02_p, staff03, staff03_p, gender, payway, paycode, memid, confirmer, isold, person_quan, acctdate, billtotalamt, compid, stdprice, m_name, staff01_kpi, staff02_kpi, staff03_kpi,card_act) select billid, billtype,type_gz_gn, cardno, checkdate, checktime, kind, price, quantity, summoney, itemtype, staff01, staff01_p, staff02, staff02_p, staff03, staff03_p, gender, payway, paycode, memid, confirmer, isold, person_quan, acctdate, billtotalamt, compid, stdprice, m_name, staff01_kpi, staff02_kpi, staff03_kpi,card_act from #struct_page end --end update a set a.stdamt = a.stdprice * quantity from #struct a -- -- add by lyj for 北京 一个单子应该只有一个总计金额 -- update a -- set a.billtotalamt = 0 -- from #struct a, #struct b -- where a.compid = b.compid and a.billid = b.billid and a.id > b.id -- -- end -- 不在时间范围内的过滤掉 -- 可能没有查到时间 delete #struct where isnull(checktime, '') <> '' and (isnull(checktime, '') < @fromtime or isnull(checktime, '') > @totime) update a set a.m_name = b.gba03c from #struct a, gbm01 b with (nolock), gam26 z with (nolock) where a.memid = b.gba01c and b.gba00c = z.gaz02c and z.gaz01c = @incompid and a.memid not like '散客%' update a set a.cardtype = c.gak02c, a.cardtypecode = c.gak01c --add by llimin 2015/01/31 from #struct a, gcm01 b with (nolock), gam10 c with (nolock) where a.memid = b.gca01c and c.gak00c = b.gca00c and c.gak01c = b.gca02c and gca00c = gca13d --更改公司名称 update a set compidname = compid+'('+b.gae03c+')' from #struct a, gam05 b with(nolock) where a.compid = b.gae01c -- 更新备注 update #struct set memo = isnull(memo, '') + ' 支付备注: ' + gsc06c from #struct, gsm03 with (nolock) where gsc00c = compid and gsc01c = billid and gsc04c = payway and gsc02c = 'gx' and isnull(gsc06c, '') <> '' if @ret_type = 1 begin if(@type <> 'gx') begin declare @id int declare @state int set @state = 0 declare @cardno varchar(20) declare @billid varchar(20) declare @billtype varchar(2) declare @comp varchar(20) declare @kind varchar(40) declare @str varchar(200) declare @payway varchar(40) declare @payamt float while(@state = 0) begin if exists(select 1 from #struct_2 where (billtype = 'gz' or billtype = 'gn')) begin select top 1 @id = id, @cardno = cardno, @billid = billid, @billtype = billtype, @comp = compid, @payway = payway from #struct_2 where (billtype = 'gz' or billtype = 'gn') select @payamt = sum(gsc05f) from gsm03 with(nolock) where gsc00c = @comp and gsc01c = @billid and gsc02c = @billtype and (@paycode = '' or gsc04c = @paycode) if(@billtype = 'gz') begin insert #gcm11(gcm00c, gcm01c, gcm02c, gcm03f) select gcm00c, gcm01c, gcm02c, sum(gcm03f) from gcm11 a with (nolock),gam26 z with (nolock) where a.gcm00c = z.gaz02c and z.gaz01c = @compid and a.gcm01c = @billid group by gcm00c, gcm01c, gcm02c insert #gtc06(gth00c, gth01c, gth02c, gth04f) select gth00c, gth01c, gth02c, sum(gth04f) from gtc06 a with (nolock),gam26 z with (nolock) where a.gth00c = z.gaz02c and z.gaz01c = @compid and a.gth01c = @billid group by gth00c, gth01c, gth02c --疗程 select @str= isnull(@str+':','')+ gda03c + '('+ cast(gcm03f as varchar)+'次)' from #gcm11 a with (nolock), gdm01 b with(nolock) where a.gcm00c = b.gda00c and b.gda01c = a.gcm02c --套餐 select @str= isnull(@str+':','')+ gta02c + '('+ cast(gth04f as varchar)+'次)' from #gtc06 a with (nolock), gtc01 b with(nolock) where a.gth00c = b.gta00c and b.gta01c = a.gth02c end else begin insert #gnm03(gnc00c, gnc01c, gnc02c, gnc03f) select gnc00c, gnc01c, gnc02c, sum(gnc03f) from gnm03 a with (nolock),gam26 z with (nolock) where a.gnc00c = z.gaz02c and z.gaz01c = @compid and a.gnc01c = @billid group by gnc00c, gnc01c, gnc02c insert #gtc05(gtf00c, gtf01c, gtf02c, gtf04f) select gtf00c, gtf01c, gtf02c, sum(gtf04f) from gtc05 a with (nolock),gam26 z with (nolock) where a.gtf00c = z.gaz02c and z.gaz01c = @compid and a.gtf01c = @billid group by gtf00c, gtf01c, gtf02c --疗程 select @str= +isnull(@str+':','')+ gda03c+ '('+ cast(gnc03f as varchar)+'次)' from #gnm03 a with (nolock), gdm01 b with(nolock) where a.gnc00c = b.gda00c and b.gda01c = a.gnc02c --套餐 select @str = + isnull(@str+':','')+ gta02c + '('+ cast(gtf04f as varchar)+'次)' from #gtc05 a with (nolock), gtc01 b with(nolock) where a.gtf00c = b.gta00c and b.gta01c = a.gtf02c end update a set a.payway = @payway, a.kind = isnull(cardtype,'') +':' + kind +isnull(@str,''), a.summoney = @payamt, a.billtotalamt = @payamt, a.stdamt = @payamt from #struct a where a.cardno = @cardno and a.billid = @billid and a.billtype = @billtype and a.compid = @comp set @str = '' delete from #gcm11 delete from #gnm03 delete from #gtc06 delete from #gtc05 delete from #struct_2 where id = @id end else begin set @state = 1 end end end --End if(@type <> 'gx') begin declare @card_amt float select @card_amt = isnull(sum(billtotalamt),0) from #struct where (billtype = 'gz' or billtype = 'gn') update a set a.sum_billtotalamt = isnull(sum_billtotalamt,0) + isnull(@card_amt,0) from #struct_page_sum a --卖卡充值单据类别 归纳到收银单据类别 update a set a.billtype = 'gx' from #struct a where (a.billtype = 'gz' or a.billtype = 'gn') end if @addtoSMS = 1 begin create table #tbl_member ( idd int identity not null, cardno varchar(20) null, memid varchar(20) null, mobile varchar(30) null, primary key(idd) ) insert #tbl_member(memid) select distinct memid from #struct delete a from #tbl_member a, gxm01 b with (nolock) where a.memid = b.gxa02c and b.gxa00c = @compid update a set a.mobile = b.gba08c from #tbl_member a, gbm01 b with (nolock) where a.memid = b.gba01c and b.gba00c = @compid delete #tbl_member where isnull(mobile, '') = '' declare @cur_max float select @cur_max = max(gxa01f) from gxm01 with (nolock) where gxa00c = @compid select @cur_max = isnull(@cur_max, 0) insert gxm01(gxa00c, gxa01f, gxa02c, gxa03c) select @compid, idd = @cur_max + idd, memid, mobile from #tbl_member where isnull(memid, '') <> '' drop table #tbl_member end if(@sort = 0) begin select * from ( select isnull(billtype,'') billtype, isnull(cardno,'') cardno, isnull(checkdate,'') checkdate, isnull(checktime,'') checktime, isnull(kind,'') kind, isnull(price,'') price, isnull(quantity,'') quantity, isnull(summoney,'') summoney, isnull(itemtype,'') itemtype, isnull(billid,'') billid, isnull(staff01,'') staff01, isnull(staff01_p,'') staff01_p, isnull(staff02,'') staff02, isnull(staff02_p,'') staff02_p, isnull(staff03,'') staff03, isnull(staff03_p,'') staff03_p, isnull(srv_type,'') srv_type, isnull(gender,'') gender, isnull(payway,'') payway, isnull(confirmer,'') confirmer, isnull(m_name,'') m_name, isnull(cardtype,'') cardtype, isnull(cardtypecode,'') cardtypecode, isnull(isold,'') isold, isnull(person_quan,'') person_quan, isnull(staff04,'') staff04, isnull(srv_type02,'') srv_type02, isnull(srv_type03,'') srv_type03, isnull(srv_type04,'') srv_type04, isnull(acctdate,'') acctdate, isnull(billtotalamt,'') billtotalamt, isnull(compid,'') compid, isnull(stdprice,'') stdprice, isnull(stdamt,'') stdamt, isnull(memo,'') memo, isnull(staff01_kpi,'') staff01_kpi, isnull(staff02_kpi,'') staff02_kpi, isnull(staff03_kpi,'') staff03_kpi, isnull(staff04_kpi,'') staff04_kpi, isnull(compidname,'') compidname,isnull(type_gz_gn,'') type_gz_gn, isnull(card_act,'') card_act, pagecount = isnull(pagecount,0) , isnull(dsum,'') dsum,isnull(dsumquan,'') dsumquan,isnull(dcustcount,'') dcustcount,isnull(card_act_sum,'') card_act_sum, isnull(sum_billtotalamt,'') sum_billtotalamt,isnull(sum_summoney,'') sum_summoney,isnull(m_femalecount,'') m_femalecount, isnull(m_femaleamt,'') m_femaleamt,isnull(m_malecount,'') m_malecount,isnull(m_maleamt,'') m_maleamt, isnull(m_nmember,'') m_nmember,isnull(m_ncustomer,'') m_ncustomer,isnull(m_noldcust,'') m_noldcust,isnull(m_nnewcust,'') m_nnewcust, isnull(m_nmemberamt,'') m_nmemberamt,isnull(m_ncustomeramt,'') m_ncustomeramt,isnull(m_noldcustamt,'') m_noldcustamt,isnull(m_nnewcustamt,'') m_nnewcustamt from #struct where isnull(kind, '') <> '' union all select '' billtype, '' cardno, '' checkdate, '' checktime, '', '' price, '' quantity, '' summoney, '' itemtype, '' billid, '' staff01, '' staff01_p, '' staff02, '' staff02_p, '' staff03, '' staff03_p, '' srv_type, '' gender, '' payway, '' confirmer, '' m_name, '' cardtype, '' cardtypecode, '', '' person_quan, '' staff04, '' srv_type02, '' srv_type03, '' srv_type04, '' acctdate, '' billtotalamt, '' compid, '' stdprice, '' stdamt, '' memo, '' staff01_kpi, '' staff02_kpi, '' staff03_kpi, '' staff04_kpi, '' compidname,'' type_gz_gn, '' card_act, '' pagecount , isnull(dsum,'') dsum,isnull(dsumquan,'') dsumquan,isnull(dcustcount,'') dcustcount,isnull(card_act_sum,'') card_act_sum, isnull(sum_billtotalamt,'') sum_billtotalamt,isnull(sum_summoney,'') sum_summoney,isnull(m_femalecount,'') m_femalecount, isnull(m_femaleamt,'') m_femaleamt,isnull(m_malecount,'') m_malecount,isnull(m_maleamt,'') m_maleamt, isnull(m_nmember,'') m_nmember,isnull(m_ncustomer,'') m_ncustomer,isnull(m_noldcust,'') m_noldcust,isnull(m_nnewcust,'') m_nnewcust, isnull(m_nmemberamt,'') m_nmemberamt,isnull(m_ncustomeramt,'') m_ncustomeramt,isnull(m_noldcustamt,'') m_noldcustamt,isnull(m_nnewcustamt,'') m_nnewcustamt from #struct_page_sum union all select '' billtype, '' cardno, '' checkdate, '' checktime, '', '' price, pay_count quantity, pay_amt summoney, '' itemtype, '' billid, '' staff01, '' staff01_p, '' staff02, '' staff02_p, '' staff03, '' staff03_p, '' srv_type, '' gender, payway, '' confirmer, '' m_name, '' cardtype, '' cardtypecode, '', '' person_quan, '' staff04, '' srv_type02, '' srv_type03, '' srv_type04, '' acctdate, '' billtotalamt, 'paysum' compid, '' stdprice, '' stdamt, '' memo, '' staff01_kpi, '' staff02_kpi, '' staff03_kpi, '' staff04_kpi, '' compidname,'' type_gz_gn, '' card_act, '' pagecount , '' dsum,'' dsumquan,'' dcustcount,'' card_act_sum, '' sum_billtotalamt,'' sum_summoney,'' m_femalecount, '' m_femaleamt,'' m_malecount,'' m_maleamt, '' m_nmember,'' m_ncustomer,'' m_noldcust,'' m_nnewcust, '' m_nmemberamt,'' m_ncustomeramt,'' m_noldcustamt,'' m_nnewcustamt from #struct_pay_sum ) a order by type_gz_gn,billid, compid asc end else begin select * from ( select isnull(billtype,'') billtype, isnull(cardno,'') cardno, isnull(checkdate,'') checkdate, isnull(checktime,'') checktime, isnull(kind,'') kind, isnull(price,'') price, isnull(quantity,'') quantity, isnull(summoney,'') summoney, isnull(itemtype,'') itemtype, isnull(billid,'') billid, isnull(staff01,'') staff01, isnull(staff01_p,'') staff01_p, isnull(staff02,'') staff02, isnull(staff02_p,'') staff02_p, isnull(staff03,'') staff03, isnull(staff03_p,'') staff03_p, isnull(srv_type,'') srv_type, isnull(gender,'') gender, isnull(payway,'') payway, isnull(confirmer,'') confirmer, isnull(m_name,'') m_name, isnull(cardtype,'') cardtype, isnull(cardtypecode,'') cardtypecode, isnull(isold,'') isold, isnull(person_quan,'') person_quan, isnull(staff04,'') staff04, isnull(srv_type02,'') srv_type02, isnull(srv_type03,'') srv_type03, isnull(srv_type04,'') srv_type04, isnull(acctdate,'') acctdate, isnull(billtotalamt,'') billtotalamt, isnull(compid,'') compid, isnull(stdprice,'') stdprice, isnull(stdamt,'') stdamt, isnull(memo,'') memo, isnull(staff01_kpi,'') staff01_kpi, isnull(staff02_kpi,'') staff02_kpi, isnull(staff03_kpi,'') staff03_kpi, isnull(staff04_kpi,'') staff04_kpi, isnull(compidname,'') compidname,isnull(type_gz_gn,'') type_gz_gn, isnull(card_act,'') card_act, pagecount = isnull(pagecount,0) , isnull(dsum,'') dsum,isnull(dsumquan,'') dsumquan,isnull(dcustcount,'') dcustcount,isnull(card_act_sum,'') card_act_sum, isnull(sum_billtotalamt,'') sum_billtotalamt,isnull(sum_summoney,'') sum_summoney,isnull(m_femalecount,'') m_femalecount, isnull(m_femaleamt,'') m_femaleamt,isnull(m_malecount,'') m_malecount,isnull(m_maleamt,'') m_maleamt, isnull(m_nmember,'') m_nmember,isnull(m_ncustomer,'') m_ncustomer,isnull(m_noldcust,'') m_noldcust,isnull(m_nnewcust,'') m_nnewcust, isnull(m_nmemberamt,'') m_nmemberamt,isnull(m_ncustomeramt,'') m_ncustomeramt,isnull(m_noldcustamt,'') m_noldcustamt,isnull(m_nnewcustamt,'') m_nnewcustamt from #struct where isnull(kind, '') <> '' union all select '' billtype, '' cardno, '' checkdate, '' checktime, '', '' price, '' quantity, '' summoney, '' itemtype, '' billid, '' staff01, '' staff01_p, '' staff02, '' staff02_p, '' staff03, '' staff03_p, '' srv_type, '' gender, '' payway, '' confirmer, '' m_name, '' cardtype, '' cardtypecode, '', '' person_quan, '' staff04, '' srv_type02, '' srv_type03, '' srv_type04, '' acctdate, '' billtotalamt, '' compid, '' stdprice, '' stdamt, '' memo, '' staff01_kpi, '' staff02_kpi, '' staff03_kpi, '' staff04_kpi, '' compidname,'' type_gz_gn, '' card_act, '' pagecount , isnull(dsum,'') dsum,isnull(dsumquan,'') dsumquan,isnull(dcustcount,'') dcustcount,isnull(card_act_sum,'') card_act_sum, isnull(sum_billtotalamt,'') sum_billtotalamt,isnull(sum_summoney,'') sum_summoney,isnull(m_femalecount,'') m_femalecount, isnull(m_femaleamt,'') m_femaleamt,isnull(m_malecount,'') m_malecount,isnull(m_maleamt,'') m_maleamt, isnull(m_nmember,'') m_nmember,isnull(m_ncustomer,'') m_ncustomer,isnull(m_noldcust,'') m_noldcust,isnull(m_nnewcust,'') m_nnewcust, isnull(m_nmemberamt,'') m_nmemberamt,isnull(m_ncustomeramt,'') m_ncustomeramt,isnull(m_noldcustamt,'') m_noldcustamt,isnull(m_nnewcustamt,'') m_nnewcustamt from #struct_page_sum union all select '' billtype, '' cardno, '' checkdate, '' checktime, '', '' price, pay_count quantity, pay_amt summoney, '' itemtype, '' billid, '' staff01, '' staff01_p, '' staff02, '' staff02_p, '' staff03, '' staff03_p, '' srv_type, '' gender, payway, '' confirmer, '' m_name, '' cardtype, '' cardtypecode, '', '' person_quan, '' staff04, '' srv_type02, '' srv_type03, '' srv_type04, '' acctdate, '' billtotalamt, 'paysum' compid, '' stdprice, '' stdamt, '' memo, '' staff01_kpi, '' staff02_kpi, '' staff03_kpi, '' staff04_kpi, '' compidname,'' type_gz_gn, '' card_act, '' pagecount , '' dsum,'' dsumquan,'' dcustcount,'' card_act_sum, '' sum_billtotalamt,'' sum_summoney,'' m_femalecount, '' m_femaleamt,'' m_malecount,'' m_maleamt, '' m_nmember,'' m_ncustomer,'' m_noldcust,'' m_nnewcust, '' m_nmemberamt,'' m_ncustomeramt,'' m_noldcustamt,'' m_nnewcustamt from #struct_pay_sum ) a order by checkdate, checktime desc, type_gz_gn, compid end end else if @ret_type = 2 or @ret_type = 3 begin create table #group_struct ( idd int identity not null, itemtype varchar(20) null, kind varchar(20) null, gender int null, cnt float null, amt float null, stdamt float null, -- 标准金额 primary key(idd) ) create nonclustered index idx_group_struct on #group_struct(itemtype,kind) create table #result_struct ( idd int identity not null, itemtype varchar(20) null, kind varchar(20) null, cnt_f float null, amt_f float null, cnt_m float null, amt_m float null, cnt float null, amt float null, totalamt float null, stdamt float null, -- 标准金额 amt_cash float null, amt_card float null, primary key(idd) ) create nonclustered index idx_result_struct on #result_struct(itemtype,kind) -- 现金临时表 -- add by lm on 2015/03/17 create table #result_cash ( idd int identity not null, itemtype varchar(20) null, kind varchar(20) null, amt_cash float null, primary key(idd) ) create nonclustered index idx_result_cash on #result_cash(itemtype,kind) -- 卡付临时表 -- add by lm on 2015/03/17 create table #result_card ( idd int identity not null, itemtype varchar(20) null, kind varchar(20) null, amt_card float null, primary key(idd) ) create nonclustered index idx_result_card on #result_card(itemtype,kind) if (@ret_type = 2) begin insert #group_struct(itemtype, kind, gender, cnt, amt, stdamt) select itemtype, kind, gender, sum(person_quan), sum(summoney), sum(stdamt) from (select itemtype, kind, gender, compid, billid, max(person_quan) as person_quan, sum(summoney) as summoney, sum(stdamt) as stdamt from #struct where isnull(kind, '') <> '' and (paycode = @paycode or isnull(@paycode, '') = '') --modified by maql 20100514 增加支付方式查询条件 group by itemtype, kind, compid, billid, gender ) T --modified by maql 20100514 增加支付方式查询条件 group by itemtype, kind, gender end else if (@ret_type = 3) begin insert #group_struct(itemtype, kind, gender, cnt, amt, stdamt) select itemtype, gda13c, gender, sum(person_quan), sum(summoney), sum(stdamt) from (select itemtype, kind, gender, compid, billid, max(person_quan) as person_quan, sum(summoney) as summoney, sum(stdamt) as stdamt from #struct where isnull(kind, '') <> '' and itemtype = 1 and (paycode = @paycode or isnull(@paycode, '') = '') --modified by maql 20100514 增加支付方式查询条件 group by itemtype, kind, compid, billid, gender ) a, gdm01 b with (nolock) where b.gda00c = @compid and a.kind = b.gda01c and isnull(gda13c, '') <> '' group by itemtype, gda13c, gender insert #group_struct(itemtype, kind, gender, cnt, amt, stdamt) select itemtype, gfa05c, gender, sum(person_quan), sum(summoney), sum(stdamt) from (select itemtype, kind, gender, compid, billid, max(person_quan) as person_quan, sum(summoney) as summoney, sum(stdamt) as stdamt from #struct where isnull(kind, '') <> '' and itemtype = 2 and (paycode = @paycode or isnull(@paycode, '') = '') --modified by maql 20100514 增加支付方式查询条件 group by itemtype, kind, compid, billid, gender ) a, gfm01 b with (nolock) where b.gfa00c = @compid and a.kind = b.gfa01c group by itemtype, gfa05c, gender end if (@ret_type = 2) begin insert #result_struct(itemtype, kind, cnt, amt, stdamt) select itemtype, kind, sum(quantity), sum(summoney), sum(stdamt) from #struct where isnull(kind, '') <> '' and (paycode = @paycode or isnull(@paycode, '') = '') --modified by maql 20100514 增加支付方式查询条件 group by itemtype, kind -- #result_card插入卡付数据 -- modify by lm 2015/03/17 insert #result_card(itemtype, kind, amt_card) select itemtype, kind, sum(summoney) from #struct where isnull(kind, '') <> '' and paycode in (select pay from pay_classify where comp = @compid and category = '2') and (paycode = @paycode or isnull(@paycode, '') = '') -- modified by maql 20100514 增加支付方式查询条件 group by itemtype, kind -- #result_cash 插入现金数据 -- modify by lm 2015/03/17 insert #result_cash(itemtype, kind, amt_cash) select itemtype, kind, sum(summoney) from #struct where isnull(kind, '') <> '' and (paycode = @paycode or isnull(@paycode, '') = '') -- modified by maql 20100514 增加支付方式查询条件 and paycode in (select pay from pay_classify where comp = @compid and category = '1') group by itemtype, kind end else if (@ret_type = 3) begin insert #result_struct(itemtype, kind, cnt, amt, stdamt) select itemtype, gda13c, sum(quantity), sum(summoney), sum(stdamt) from #struct a, gdm01 b with (nolock) where isnull(kind, '') <> '' and itemtype = 1 and (a.paycode = @paycode or isnull(@paycode, '') = '') --modified by maql 20100514 增加支付方式查询条件 and b.gda00c = @compid and a.kind = b.gda01c and isnull(gda13c, '') <> '' group by itemtype, gda13c -- #result_card 插入卡付数据 -- modify by lm 2015/03/17 insert #result_card(itemtype, kind, amt_card) select itemtype, gda13c, sum(summoney) from #struct a, gdm01 b with (nolock) where isnull(kind, '') <> '' and itemtype = 1 and a.paycode in (select pay from pay_classify where comp = @compid and category = '2') and (a.paycode = @paycode or isnull(@paycode, '') = '') -- modified by maql 20100514 增加支付方式查询条件 and b.gda00c = @compid and a.kind = b.gda01c and isnull(gda13c, '') <> '' group by itemtype, gda13c -- result_cash 插入现金数据 -- modify by lm 2015/03/17 insert #result_cash(itemtype, kind, amt_cash) select itemtype, gda13c, sum(summoney) from #struct a, gdm01 b with (nolock) where isnull(kind, '') <> '' and itemtype = 1 and a.paycode in (select pay from pay_classify where comp = @compid and category = '1') and (a.paycode = @paycode or isnull(@paycode, '') = '') -- modified by maql 20100514 增加支付方式查询条件 and b.gda00c = @compid and a.kind = b.gda01c and isnull(gda13c, '') <> '' group by itemtype, gda13c insert #result_struct(itemtype, kind, cnt, amt, stdamt) select itemtype, gfa05c, sum(quantity), sum(summoney), sum(stdamt) from #struct a, gfm01 b with (nolock) where isnull(kind, '') <> '' and itemtype = 2 and (a.paycode = @paycode or isnull(@paycode, '') = '') --modified by maql 20100514 增加支付方式查询条件 and b.gfa00c = @compid and a.kind = b.gfa01c group by itemtype, gfa05c -- #result_card 插入卡付数据 -- modify by lm 2015/03/17 insert #result_card(itemtype, kind, amt_card) select itemtype, gfa05c, sum(summoney) from #struct a, gfm01 b with (nolock) where isnull(kind, '') <> '' and itemtype = 2 and a.paycode in (select pay from pay_classify where comp = @compid and category = '2') and (a.paycode = @paycode or isnull(@paycode, '') = '') --modified by maql 20100514 增加支付方式查询条件 and b.gfa00c = @compid and a.kind = b.gfa01c group by itemtype, gfa05c -- result_cash 插入现金数据 -- modify by lm 2015/03/17 insert #result_cash(itemtype, kind, amt_cash) select itemtype, gfa05c, sum(summoney) from #struct a, gfm01 b with (nolock) where isnull(kind, '') <> '' and itemtype = 2 and a.paycode in (select pay from pay_classify where comp = @compid and category = '1') and (a.paycode = @paycode or isnull(@paycode, '') = '') --modified by maql 20100514 增加支付方式查询条件 and b.gfa00c = @compid and a.kind = b.gfa01c group by itemtype, gfa05c end update a set a.cnt_f = b.cnt, a.amt_f = b.amt from #result_struct a, #group_struct b where a.itemtype = b.itemtype and a.kind = b.kind and b.gender = 0 update a set a.cnt_m = b.cnt, a.amt_m = b.amt from #result_struct a, #group_struct b where a.itemtype = b.itemtype and a.kind = b.kind and b.gender = 1 declare @totalamt float select @totalamt = sum(amt) from #result_struct update a set a.totalamt = @totalamt from #result_struct a -- 现金赋值 -- modify by lm 2015/03/17 update a set a.amt_cash = b.amt_cash from #result_struct a, #result_cash b where a.itemtype = b.itemtype and a.kind = b.kind -- 卡付赋值 -- modify by lm 2015/03/17 update a set a.amt_card = b.amt_card from #result_struct a, #result_card b where a.itemtype = b.itemtype and a.kind = b.kind select isnull(idd,'') idd, isnull(itemtype,'') itemtype, isnull(kind,'') kind, isnull(cnt_f,'') cnt_f, isnull(amt_f,'') amt_f, isnull(cnt_m,'') cnt_m, isnull(amt_m,'') amt_m, isnull(cnt,'') cnt, isnull(amt,'') amt, isnull(totalamt,'') totalamt, isnull(stdamt,'') stdamt, -- 标准金额 isnull(amt_cash,'') amt_cash, isnull(amt_card,'') amt_card from #result_struct order by itemtype, amt desc drop table #result_struct drop table #group_struct drop table #result_cash drop table #result_card end drop table #gsm03 drop table #struct drop table #gcm11 drop table #gnm03 drop table #gtc05 drop table #gtc06 drop table #struct_2 drop table #struct_page drop table #struct_page_sum drop table #struct_customer drop table #struct_pay_sum end GO /****** Object: StoredProcedure [dbo].[upg_work_statistic] Script Date: 06/12/2017 06:20:52 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_work_statistic] ( @compid varchar(10), --公司编号 @datefrom varchar(10), --开始日期 @dateto varchar(10) --结束日期 ) as --WUXM 2005/0712 modified by WUXM 2005/08/10 begin create table #arrange --排班表 ( idd int identity not null, compid varchar(10) null, --公司编号 personno varchar(10) null, --员工号 date varchar(8) null, --日期 onduty varchar(6) null, --应上班时间 offduty varchar(6) null, --应下班时间 primary key(idd) ) create table #work --员工出勤表 ( idd int identity not null, compid varchar(10) null, --公司编号 personno varchar(10) null, --员工号 date varchar(8) null, --日期 onduty varchar(6) null, --实际上班时间 offduty varchar(6) null, --实际下班时间 primary key(idd) ) create table #person --员工出勤信息,时间的存储方式不同 ( idd int identity not null, compid varchar(10) null, --公司编号 personno varchar(10) null, --员工号 date varchar(8) null, --日期 begintime int null, --应上班时间 endtime int null, --应下班时间 onwork int null, --上班时间 outwork int null, --下班时间 primary key(idd) ) create table #holiday --存储休假的临时表 ( personno varchar(10) null, workdate varchar(8) null ) create table #absent --未出勤员工信息 ( personno varchar(10) null, date varchar(8) null ) --将员工出勤数据插入到#work中 --可能是原始的出勤纪录,也有可能已经审核过的出勤纪录 insert #work(compid, personno, date, onduty, offduty) select hda00c, hda01c, hda02d, hda03t, hda10t from hdm01 where hda00c = @compid and hda02d between @datefrom and @dateto --建立游标, 插入满足条件的记录, 在对每条记录进行时间转换 --得到出勤审核时间范围内的员工排班记录 declare cur_insert_work cursor for select gicj00c, gicj01v, gicj03t, gicj12t, gicj05d, gicj06d from gicm10 where gicj00c = @compid and (@datefrom between gicj05d and gicj06d) or (@dateto between gicj05d and gicj06d) open cur_insert_work declare @gicj00 varchar(10) --存放公司编号 declare @gicj01 varchar(10) --存放员工编号 declare @gicj02 varchar(6) --存放上班时间 declare @gicj03 varchar(6) --存放下班时间 declare @gicj04 varchar(8) --排班开始日期 declare @gicj05 varchar(8) --排班结束日期 fetch next from cur_insert_work into @gicj00, @gicj01, @gicj02, @gicj03, @gicj04, @gicj05 while(@@fetch_status = 0) begin --根据每条记录的日期,将其转换成一个日期对应一条记录 insert #arrange(compid, personno, date, onduty, offduty) exec upg_arrange_transform @gicj00, @gicj01, @gicj02, @gicj03, @gicj04, @gicj05, '1' fetch next from cur_insert_work into @gicj00, @gicj01, @gicj02, @gicj03, @gicj04, @gicj05 end close cur_insert_work deallocate cur_insert_work --指定时间段内的员工排班表,根据输入日期获得该时间段内的员工排班信息 delete a from #arrange a where (a.date not between @datefrom and @dateto) --旷工信息 --先假设所有员工都旷工, 再删除实际已经出勤的员工 insert #absent(personno, date) select personno, date from #arrange delete a from #absent a, #work b where a.personno = b.personno and a.date = b.date --为了计算迟到或早退的时间, 把 varchar型转化为int insert #person(compid, personno, date, begintime, endtime, onwork, outwork) select a.compid, a.personno, a.date, begintime = case(len(isnull(a.onduty, 0))) when 6 then 60*cast(substring(a.onduty, 1, 2) as int) + cast(substring(a.onduty, 3, 2) as int) when 5 then 60*cast(substring(a.onduty, 1, 1) as int) + cast(substring(a.onduty, 2, 2) as int) when 4 then cast(substring(a.onduty, 1, 2) as int) when 3 then cast(substring(a.onduty, 1, 1) as int) else 0 end, endtime = case(len(isnull(a.offduty, 0))) when 6 then 60*cast(substring(a.offduty, 1, 2) as int) + cast(substring(a.offduty, 3, 2) as int) when 5 then 60*cast(substring(a.offduty, 1, 1) as int) + cast(substring(a.offduty, 2, 2) as int) when 4 then cast(substring(a.offduty, 1, 2) as int) when 3 then cast(substring(a.offduty, 1, 1) as int) else 0 end, onwork = case(len(isnull(b.onduty, 0))) when 6 then 60*cast(substring(b.onduty, 1, 2) as int) + cast(substring(b.onduty, 3, 2) as int) when 5 then 60*cast(substring(b.onduty, 1, 1) as int) + cast(substring(b.onduty, 2, 2) as int) when 4 then cast(substring(b.onduty, 1, 2) as int) when 3 then cast(substring(b.onduty, 1, 1) as int) else 0 end, outwork = case(len(isnull(b.offduty, 0))) when 6 then 60*cast(substring(b.offduty, 1, 2) as int) + cast(substring(b.offduty, 3, 2) as int) when 5 then 60*cast(substring(b.offduty, 1, 1) as int) + cast(substring(b.offduty, 2, 2) as int) when 4 then cast(substring(b.offduty, 1, 2) as int) when 3 then cast(substring(b.offduty, 1, 1) as int) else 0 end from #arrange a left outer join #work b on a.compid = b.compid and a.personno = b.personno and a.date = b.date --得到指定日期内所有员工的出勤日程(没有休息日) declare cur_person_date cursor for select haa01c from ham01 where haa00c = @compid open cur_person_date declare @person varchar(10) fetch next from cur_person_date into @person while @@fetch_status = 0 begin insert #holiday(personno, workdate) exec upg_arrange_transform @compid, @person, '', '', @datefrom, @dateto, '2' fetch next from cur_person_date into @person end close cur_person_date deallocate cur_person_date --统计没有排班的员工记录,即休假信息 delete a from #holiday a, #arrange b where a.personno = b.personno and a.workdate = b.date --将休假信息更新 update a set a.hda07c = '2' from hdm01 a, #holiday b where a.hda00c = @compid and a.hda01c = b.personno and a.hda02d = b.workdate --加班信息 update a set a.hda07c = '7' from hdm01 a, #holiday b where a.hda00c = @compid and a.hda01c = b.personno and a.hda02d = b.workdate and isnull(a.hda03t, 0) <> 0 and isnull(a.hda10t, 0)<>0 --将迟到早退的时间(以分钟为单位)存入hdm01 update a set hda08i = case when onwork-begintime <= 0 then 0 when onwork-begintime > 0 then onwork-begintime end, hda09i = case when endtime-outwork <= 0 then 0 when endtime-outwork > 0 then endtime-outwork end from #person b, hdm01 a where a.hda00c = b.compid and a.hda01c = b.personno and a.hda02d = b.date --删除休假信息中已审核过的休假信息,即在hdm01中存在的记录 delete a from #holiday a, hdm01 b where a.personno = b.hda01c and a.workdate = b.hda02d and b.hda00c = @compid and (b.hda02d between @datefrom and @dateto) --删除#absent表中已审核过的旷工信息 delete a from #absent a, hdm01 b where a.personno = b.hda01c and a.date = b.hda02d and b.hda00c = @compid and (b.hda02d between @datefrom and @dateto) --审核出勤情况 --1--正常 --2--休假 --3--事假 --4--病假 --5--旷工 --6--迟到 --7--加班 --8--不确定,上班时间或下班时间为空 --根据公司出勤制度判断员工假别,gsm01 declare @num float select @num = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP049' update a set a.hda07c = case when hda08i + hda09i > @num then '5' when hda08i + hda09i = 0 then '1' else '6' end from hdm01 a where a.hda00c = @compid and a.hda02d between @datefrom and @dateto and a.hda07c = '1' --旷工 update a set hda07c = '5' from hdm01 a where a.hda00c = @compid and isnull(hda03t, 0) = 0 and isnull(hda10t, 0) = 0 and hda07c = '6' --插入旷工信息 insert hdm01(hda00c, hda01c, hda02d, hda07c) select distinct @compid, personno, date, '5' from #absent a where a.date between @datefrom and @dateto --休假 insert hdm01(hda00c, hda01c, hda02d, hda07c) select distinct @compid, personno, workdate, '2' from #holiday a where a.workdate between @datefrom and @dateto --休假 update a set a.hda07c = '2' from hdm01 a, #holiday b where a.hda00c = @compid and a.hda01c = b.personno and a.hda02d = b.workdate --不确定 update a set a.hda07c = '8' from hdm01 a where a.hda00c = @compid and a.hda07c = '5' and (isnull(a.hda03t, '') <> '' and isnull(a.hda10t, '') = '') or (isnull(a.hda03t, '') = '' and isnull(a.hda10t, '') <> '') update a set a.hda08i = 0, a.hda09i = 0 from hdm01 a where a.hda00c = @compid and isnull(a.hda03t, '') = '' and isnull(a.hda10t, '') = '' update a set a.hda08i = 0 from hdm01 a where a.hda00c = @compid and isnull(a.hda03t, '') = '' update a set a.hda09i = 0 from hdm01 a where a.hda00c = @compid and isnull(a.hda10t, '') = '' drop table #arrange drop table #work drop table #person drop table #holiday drop table #absent end GO /****** Object: StoredProcedure [dbo].[upg_analyzer_member_2] Script Date: 06/12/2017 06:19:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_analyzer_member_2] ( @compid varchar(10), @frommemid varchar(20), @tomemid varchar(20), @fromdate varchar(20), @todate varchar(20), @acon_amt float, @at_times float ) as begin create table #mem_result ( idd int identity not null, memid varchar(20) not null, -- 会员编号 m_name varchar(40) null, -- 姓名 cardid varchar(20) null, -- 卡号 gender int null, -- 性别 0:女 mobile varchar(20) null, -- 手机 tel varchar(50) null, -- 电话 con_amt float null, -- 消费金额 t_times float null, -- 消费次数 cash float null, -- 现金 save_1 float null, -- 账户一 save_2 float null, -- 账户二 save_3 float null, -- 账户三 save_4 float null, -- 账户四 save_5 float null, -- 账户五 credit float null, -- 银行卡 gift float null, -- 经理签单 coupon float null, -- 抵用券 borrow float null, -- 欠款 primary key(idd) ) create table #sum_tmp ( vid varchar(20) not null, vsum float null, v_1 varchar(8) null, f_1 float null, v_2 varchar(8) null, times int null primary key(vid) ) declare @pay varchar(8) select @pay = gsa02c from gsm01 where gsa01c = 'SP058' and gsa00c = @compid set @pay = isnull(@pay, '') create table #worl_card_money ( paymoney float null, card_id varchar(20) not null, paymode varchar(20) ) -- 增加抵用卷,赠送,内调支付方式金额 modify by limin 2014-12-03 create table #worl_card_money_pay ( card_id varchar(20) not null, pay_0 float null, pay_$ float null, pay_gave float null ) -- 会员初始化 if @frommemid = '' or @frommemid = '*' begin insert #mem_result(memid, m_name, cardid, gender, mobile, tel) select distinct gba01c, gba03c, gba23c, gba14i,'' gba08c, gba07c from gcm01 inner join gbm01 on gca01c = gba23c where gca00c = gca13d and gca13d = @compid -- select distinct gba01c, gba03c, gba23c, gba14i, gba08c, gba07c -- from gbm01 with (nolock), gam26 z with (nolock) -- where gba00c = z.gaz02c -- and z.gaz01c = @compid -- and gba23c in (select gca01c from gcm01 where gca00c = gca13d and gca13d = @compid) --本店的会员卡 end else begin insert #mem_result(memid, m_name, cardid, gender, mobile, tel) select distinct gba01c, gba03c, gba23c, gba14i, '' gba08c, gba07c from gbm01 with (nolock), gam26 z with (nolock) where gba00c = z.gaz02c and z.gaz01c = @compid and gba23c in (select gca01c from gcm01 where gca00c = gca13d and gca13d = @compid) --本店的会员卡 and gba01c >= @frommemid and gba01c <= @tomemid end update a set a.mobile = b.gba08c from #mem_result a,gbm01 b where a.memid = b.gba01c and b.gba00c = @compid -- 添加会员卡 insert into #worl_card_money_pay(card_id) select distinct memid from #mem_result -- 消费总金额, 最后消费日期, 消费次数 exec upg_stat_card_consume @compid, @fromdate, @todate, @frommemid, @tomemid, 0 update a set a.con_amt = b.vsum, a.t_times = b.f_1 from #mem_result a, #sum_tmp b where a.cardid = b.vid -- 统计现金 delete #sum_tmp insert #sum_tmp(vid, vsum) select gga09c, sum(gsc05f) from gsm03 a with (nolock), ggm01 b with (nolock), gam26 z with (nolock) where a.gsc00c = b.gga00c and a.gsc01c = b.gga01c and a.gsc00c = z.gaz02c and a.gsc02c = 'gx' and a.gsc04c = '1' and z.gaz01c = @compid and b.gga02d >= @fromdate and b.gga02d <= @todate and isnull(gga09c,'')<>'' group by gga09c update a set a.cash = b.vsum from #mem_result a, #sum_tmp b where a.cardid = b.vid --统计账户一 delete #sum_tmp insert #sum_tmp(vid, vsum) select gga09c, sum(gsc05f) from gsm03 a with (nolock), ggm01 b with (nolock),gam26 z with (nolock) --增加子门店会员消费金额(gam26) modified by limin 2014/07/30 where a.gsc00c = b.gga00c and a.gsc01c = b.gga01c and a.gsc00c = z.gaz02c and a.gsc02c = 'gx' and a.gsc04c = '2' and z.gaz01c = @compid and b.gga02d >= @fromdate and b.gga02d <= @todate and isnull(gga09c,'')<>'' group by gga09c update a set a.save_1 = b.vsum from #mem_result a, #sum_tmp b where a.memid = b.vid --抵用A delete #sum_tmp insert #sum_tmp(vid, vsum) select gga09c, sum(gsc05f) from gsm03 a with (nolock), ggm01 b with (nolock),gam26 z with (nolock) --增加子门店会员消费金额(gam26) modified by limin 2014/07/30 where a.gsc00c = b.gga00c and a.gsc01c = b.gga01c and a.gsc00c = z.gaz02c and a.gsc02c = 'gx' and a.gsc04c = '3' and z.gaz01c = @compid and b.gga02d >= @fromdate and b.gga02d <= @todate and isnull(gga09c,'')<>'' group by gga09c update a set a.coupon = b.vsum from #mem_result a, #sum_tmp b where a.cardid = b.vid --项目收银 --modify by limin 2014/12/03 insert into #worl_card_money(card_id,paymoney, paymode) select gga09c, sum(ggb11f), ggb27c from ggm01 a with (nolock), ggm02 b with (nolock),gam26 z with (nolock) where a.gga00c = b.ggb00c and a.gga01c = b.ggb01c and a.gga00c = z.gaz02c and (ggb27c = '0' or ggb27c = '$' or (ggb27c=@pay and @pay <> ''))--项目支付方式 and gaz01c = @compid and gga02d between @fromdate and @todate and isnull(gga09c,'')<>'' group by gga09c,ggb27c --产品收银 insert into #worl_card_money(card_id,paymoney,paymode) select gga09c, sum(ggc11f),ggc27c from ggm01 a with (nolock), ggm03 b with (nolock), gam26 z with (nolock) where a.gga00c=b.ggc00c and a.gga01c=b.ggc01c and a.gga00c = z.gaz02c and (ggc27c='0' or ggc27c='$' or (ggc27c=@pay and @pay <> ''))--产品支付方式 and gaz01c=@compid and gga02d between @fromdate and @todate and isnull(gga09c,'')<>'' group by gga09c,ggc27c --内调金额赋值 update a set a.pay_0=b.paymoney from #worl_card_money_pay a ,(select card_id,sum(paymoney)as paymoney from #worl_card_money where paymode='0'group by card_id ) b where b.card_id=a.card_id --抵用卷B金额赋值 update a set a.pay_$=b.paymoney from #worl_card_money_pay a ,(select card_id,sum(paymoney) as paymoney from #worl_card_money where paymode='$' group by card_id ) b where b.card_id=a.card_id --赠送金额赋值 update a set a.pay_gave=b.paymoney from #worl_card_money_pay a ,(select card_id,sum(paymoney) as paymoney from #worl_card_money where paymode=@pay group by card_id ) b where b.card_id=a.card_id --账户二 delete #sum_tmp insert #sum_tmp(vid, vsum) select gga09c, sum(gsc05f) from gsm03 a with (nolock), ggm01 b with (nolock), gam26 z with (nolock) --增加子门店会员消费金额(gam26) modified by limin 2014/07/30 where a.gsc00c = b.gga00c and a.gsc01c = b.gga01c and a.gsc00c = z.gaz02c and a.gsc02c = 'gx' and a.gsc04c = '4' and z.gaz01c = @compid and b.gga02d >= @fromdate and b.gga02d <= @todate and isnull(gga09c,'')<>'' group by gga09c update a set a.save_2 = b.vsum from #mem_result a, #sum_tmp b where a.memid = b.vid --欠款 delete #sum_tmp insert #sum_tmp(vid, vsum) select gga09c, sum(gsc05f) from gsm03 a with (nolock), ggm01 b with (nolock),gam26 z with (nolock) --增加子门店会员消费金额(gam26) modified by limin 2014/07/30 where a.gsc00c = b.gga00c and a.gsc01c = b.gga01c and a.gsc00c = z.gaz02c and a.gsc02c = 'gx' and a.gsc04c = '5' and z.gaz01c = @compid and b.gga02d >= @fromdate and b.gga02d <= @todate and isnull(gga09c,'')<>'' group by gga09c update a set a.borrow = b.vsum from #mem_result a, #sum_tmp b where a.cardid = b.vid --银行卡 delete #sum_tmp insert #sum_tmp(vid, vsum) select gga09c, sum(gsc05f) from gsm03 a with (nolock), ggm01 b with (nolock), gam26 z with (nolock) --增加子门店会员消费金额(gam26) modified by limin 2014/07/30 where a.gsc00c = b.gga00c and a.gsc01c = b.gga01c and a.gsc00c = z.gaz02c and a.gsc02c = 'gx' and a.gsc04c = '6' and z.gaz01c = @compid and b.gga02d >= @fromdate and b.gga02d <= @todate and isnull(gga09c,'')<>'' group by gga09c update a set a.credit = b.vsum from #mem_result a, #sum_tmp b where a.cardid = b.vid --积分 delete #sum_tmp insert #sum_tmp(vid, vsum) select gga09c, sum(gsc05f) from gsm03 a with (nolock), ggm01 b with (nolock), gam26 z with (nolock) --增加子门店会员消费金额(gam26) modified by limin 2014/07/30 where a.gsc00c = b.gga00c and a.gsc01c = b.gga01c and a.gsc00c = z.gaz02c and a.gsc02c = 'gx' and a.gsc04c = '7' and z.gaz01c = @compid and b.gga02d >= @fromdate and b.gga02d <= @todate and isnull(gga09c,'')<>'' group by gga09c update a set a.save_3 = b.vsum from #mem_result a, #sum_tmp b where a.cardid = b.vid --经理免单 delete #sum_tmp insert #sum_tmp(vid, vsum) select gga09c, sum(gsc05f) from gsm03 a with (nolock), ggm01 b with (nolock), gam26 z with (nolock) --增加子门店会员消费金额(gam26) modified by limin 2014/07/30 where a.gsc00c = b.gga00c and a.gsc01c = b.gga01c and a.gsc00c = z.gaz02c and a.gsc02c = 'gx' and a.gsc04c = '8' and z.gaz01c = @compid and b.gga02d >= @fromdate and b.gga02d <= @todate and isnull(gga09c,'')<>'' group by gga09c update a set a.gift = b.vsum from #mem_result a, #sum_tmp b where a.cardid = b.vid --账户二 delete #sum_tmp insert #sum_tmp(vid, vsum) select gga09c, sum(gsc05f) from gsm03 a with (nolock), ggm01 b with (nolock), gam26 z with (nolock) --增加子门店会员消费金额(gam26) modified by limin 2014/07/30 where a.gsc00c = b.gga00c and a.gsc01c = b.gga01c and a.gsc00c = z.gaz02c and a.gsc02c = 'gx' and a.gsc04c = '9' and z.gaz01c = @compid and b.gga02d >= @fromdate and b.gga02d <= @todate and isnull(gga09c,'')<>'' group by gga09c update a set a.save_4 = b.vsum from #mem_result a, #sum_tmp b where a.cardid = b.vid --账户三 delete #sum_tmp insert #sum_tmp(vid, vsum) select gga09c, sum(gsc05f) from gsm03 a with (nolock), ggm01 b with (nolock),gam26 z with (nolock) --增加子门店会员消费金额(gam26) modified by limin 2014/07/30 where a.gsc00c = b.gga00c and a.gsc01c = b.gga01c and a.gsc00c = z.gaz02c and a.gsc02c = 'gx' and a.gsc04c = 'A' and z.gaz01c = @compid and b.gga02d >= @fromdate and b.gga02d <= @todate and isnull(gga09c,'')<>'' group by gga09c update a set a.save_5 = b.vsum from #mem_result a, #sum_tmp b where a.cardid = b.vid --select a.*,b.pay_0,pay_$,b.pay_gave from #mem_result a,#worl_card_money_pay b where a.cardid=b.card_id -- and isnull(con_amt, 0) >= @acon_amt -- and isnull(t_times, 0) >= @at_times --order by con_amt desc select distinct a.idd,isnull(a.memid,'') memid,isnull(a.m_name,'') m_name,isnull(a.cardid,'') cardid,isnull(a.gender,0) gender, isnull(a.mobile,'') mobile,isnull(a.tel,'') tel,isnull(a.con_amt,0) con_amt,isnull(a.t_times,0) t_times, isnull(a.cash,0) cash,isnull(a.save_1,0) save_1,isnull(a.save_2,0) save_2,isnull(a.save_3,0) save_3, isnull(a.save_4,0) save_4,isnull(a.save_5,0) save_5,isnull(a.credit,0) credit,isnull(a.gift,0) gift, isnull(a.coupon,0) coupon,isnull(a.borrow,0) borrow,isnull(b.pay_0,0) pay_0,isnull(b.pay_$,0) pay_$,isnull(b.pay_gave,0) pay_gave from #mem_result a,#worl_card_money_pay b where a.cardid=b.card_id and isnull(con_amt, 0) >= @acon_amt and isnull(t_times, 0) >= @at_times order by con_amt desc drop table #mem_result drop table #sum_tmp drop table #worl_card_money_pay drop table #worl_card_money end GO /****** Object: StoredProcedure [dbo].[upg_set_deficit_flag] Script Date: 06/12/2017 06:20:40 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_set_deficit_flag] ( @compid varchar(10), @billid varchar(20) ) as set nocount on set rowcount 0 set transaction isolation level read uncommitted begin --目前只考虑按项目支付的情况 declare @sp055f float select @sp055f = gsa03f from gsm01 with (nolock) where gsa00c = @compid and gsa01c = 'SP055' if @sp055f = 0 -- 按项目支付 begin update ggm03 set ggc32i = 1, ggc33d = '' where ggc00c = @compid and ggc01c = @billid and ggc27c = '5' update ggm02 set ggb40i = 1, ggb41d = '' where ggb00c = @compid and ggb01c = @billid and ggb27c = '5' end else begin --按单据支付 declare @amt float declare @remainamt float declare @billamt float select @amt = gsc05f from gsm03 where gsc00c = @compid and gsc01c = @billid and gsc02c = 'gx' and gsc04c = '5' if isnull(@amt, 0) = 0 return select @remainamt = @amt declare cur_get_each_def cursor for select ggc11f from ggm03 b where b.ggc00c = @compid and b.ggc01c = @billid for update of ggc32i open cur_get_each_def fetch cur_get_each_def into @billamt while @@fetch_status = 0 and @remainamt>0 begin if @billamt <= @remainamt begin update ggm03 set ggc32i = 1 where current of cur_get_each_def select @remainamt = @remainamt - @billamt end else if @remainamt > @billamt/2 begin update ggm03 set ggc32i = 1 where current of cur_get_each_def select @remainamt = @remainamt - @billamt end fetch cur_get_each_def into @billamt end close cur_get_each_def deallocate cur_get_each_def if @remainamt>0 begin declare cur_get_each_def_2 cursor for select ggb11f from ggm02 b where b.ggb00c = @compid and b.ggb01c = @billid for update of ggb40i open cur_get_each_def_2 fetch cur_get_each_def_2 into @billamt while @@fetch_status = 0 and @remainamt>0 begin if @billamt <= @remainamt begin update ggm02 set ggb40i = 1 where current of cur_get_each_def_2 select @remainamt = @remainamt - @billamt end else if @remainamt>@billamt/2 begin update ggm02 set ggb40i = 1 where current of cur_get_each_def_2 select @remainamt = @remainamt - @billamt end fetch cur_get_each_def_2 into @billamt end close cur_get_each_def_2 deallocate cur_get_each_def_2 end end end GO /****** Object: StoredProcedure [dbo].[upg_gbm24_finish_amt] Script Date: 06/12/2017 06:20:03 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_gbm24_finish_amt] ( @compid varchar(10), -- 门店编号 @startdate varchar(10), @enddate varchar(10) ) as begin begin try create table #empl_stat_outer ( id int identity, -- 流水号 person_id varchar(20) not null, -- 员工代码 comp_ascription varchar(10) null, -- 公司别(员工) action_id tinyint not null, -- 类别, 1-卡销售 2-指定大工, 3-轮班大工, 4-补工大工, -- 5-指定中工, 6-轮班中工 -- 7-指定小工, 8-轮班小工, 9-产品销售 10,11-四工 -- 0 卡充值 srvdate varchar(10) not null, -- 日期 code varchar(20) not null, -- 项目代码, 或是卡号, 产品码 name varchar(200) null, -- 名称 payway varchar(40) null, -- 支付方式 amt float null, -- 营业金额 quan float null, -- 数量 cost float null, -- 成本 comm float null, -- 提成 amt2 float null, -- 虚业绩 amt3 float null, -- 真实业绩 cardrate float null, -- 转卡比率 stdprice float null, -- 标准单价 prj_type varchar(20) null, -- 项目类别 share_rate float default 1 null, --分享比率 cls_flag int null, -- 1:项目 2:产品 3:卡 gender int null, -- 性别 0:女 1:男 billid varchar(20) null, -- 单号 paycode varchar(20) null, -- 支付代码 ex_rate float null, -- 实际营业额折算比率 ggm02_seq float null, -- ggm02中的序列号, 与单号一起来标志一个项目 room_id varchar(20) null, -- 房间号,只在spa或足浴版本需要 cardid varchar(20) null, -- 会员卡号 memid varchar(40) null, -- 会员编号 sumMoney float null, -- 会员卡销售总金额 compid varchar(20) null ) create clustered index idx_empl_stat_outer_01 on #empl_stat_outer(person_id, prj_type) declare @spid int select @spid = @@spid insert #empl_stat_outer(person_id, comp_ascription, action_id, srvdate, code, name, payway, amt, quan, cost, comm, amt2, amt3, cardrate, stdprice, prj_type, share_rate, cls_flag, gender, billid, paycode, ex_rate, ggm02_seq, room_id, cardid, memid) exec upg_personal_statistics @compid,@startdate,@enddate,'*','*',1,1,1,1,@spid declare @startYear varchar(10) declare @endYear varchar(10) set @startYear = substring(@startdate, 1, 6) set @endYear = substring(@enddate, 1, 6) Create table #gbm23 ( gbw00c varchar(10) not null, --门店编号 gbw01c varchar(30) not null, --员工编号 gbw02c varchar(60) null, --员工姓名 gbw03c varchar(10) null, --职位 gbw04f float null, --目标营业额 gbw05f float null, --完成营业额 gbw06c varchar(10) null, --完成营业率 gbw07f float null, --目标消耗额 gbw08f float null, --完成消耗 gbw09c varchar(10) null, --完成消耗率 ) insert into #gbm23 select gbw00c, gbw01c, gbw02c = (select haa02c from ham01 where haa00c = gbw00c and haa01c = gbw01c), gbw03c = (select gsb03c from gsm02 where gsb01c = 'R' and gsb00c = gbw00c and gsb02c = gbw03c ), sum(isnull(gbw04f,0)) as gbw04f, 0 as gbw05f, '' as gbw06c, sum(isnull(gbw05f,0)) as gbw07f, 0 as gbw08f, '' as gbw09c from gbm23 with(nolock) where gbw00c in (select gaz02c from gam26 where gaz01c = @compid) and gbw02d between @startYear and @endYear group by gbw00c,gbw01c,gbw03c --百分率小数位 0.整数;1.一位;2.二位;3.叁位;4.四位 declare @gsa03f int select @gsa03f = isnull(gsa03f,0) from gsm01 where gsa00c = @compid and gsa01c = 'SP006' --营业额 update a set a.gbw05f = b.amt, a.gbw06c = Convert(varchar(20),Round((case a.gbw04f when 0 then 0 else b.amt / a.gbw04f end) * 100,@gsa03f)) + '%' from #gbm23 a,( select comp_ascription,person_id,sum(amt2) amt from #empl_stat_outer with (nolock) where comp_ascription in (select gaz02c from gam26 where gaz01c = @compid) and srvdate between @startdate and @enddate and paycode in (select pay from pay_classify where category = '1' and comp = comp_ascription) group by comp_ascription,person_id ) as b where a.gbw00c = b.comp_ascription and a.gbw01c = b.person_id --消耗额(划卡) pay_category update a set a.gbw08f = b.amt, a.gbw09c = Convert(varchar(20),Round((case a.gbw07f when 0 then 0 else b.amt / a.gbw07f end) * 100,@gsa03f)) + '%' from #gbm23 a,( select comp_ascription,person_id,sum(amt2) amt from #empl_stat_outer with (nolock) where comp_ascription in (select gaz02c from gam26 where gaz01c = @compid) and srvdate between @startdate and @enddate and paycode in (select pay from pay_classify where category = '2' and comp = comp_ascription) group by comp_ascription,person_id ) as b where a.gbw00c = b.comp_ascription and a.gbw01c = b.person_id select gbw00c, gbw01c, gbw02c, gbw03c, gbw04f, gbw05f, gbw06c, gbw07f, gbw08f, gbw09c from #gbm23 end try begin catch drop table #empl_stat_outer drop table #gbm23 end catch drop table #empl_stat_outer drop table #gbm23 end GO /****** Object: StoredProcedure [dbo].[upg_member_nocard_operation_details] Script Date: 06/12/2017 06:20:23 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_member_nocard_operation_details] ( @compid varchar(10), @memid varchar(20), @fromdate varchar(20), @todate varchar(20) ) as begin declare @incompid varchar(10) select @incompid = @compid declare @sp023 varchar(10) select @sp023 = gsa02c from gsm01 where gsa00c = @compid and gsa01c = 'SP023' if @compid = '001' or @sp023 = '1' begin select @compid = '%' end if @fromdate = '' or @fromdate = '*' begin select @fromdate = '19900101' select @todate = '20901231' end create table #details ( seq int identity not null, date varchar(8) not null, action varchar(4) not null, item varchar(20) not null, name varchar(100) null, price float null, --标准价格 amt float null, person1 varchar(20) null, person2 varchar(20) null, person3 varchar(20) null, compid varchar(10) null, billid varchar(20) null, type varchar(10) null, fillmode varchar(10) null, quan float null, ttime varchar(20) null, paycode varchar(5) null, primary key(seq) ) if (@memid <> '') begin insert #details(date, action, item, name, price, amt, person1, person2, person3, compid, billid, type, fillmode, quan, ttime, paycode) select gct04d, gct06c, gct07c, gct08c, gct09f, gct12f, gct15c, gct16c, gct17c, gct00c, gct14c, gct13c, gct05c, gct11f, gct19t, gct20c from gcm20 with (nolock), gam26 z where gct03c = @memid and gct00c = z.gaz02c and z.gaz01c = @incompid and gct04d >= @fromdate and gct04d <= @todate order by gct01i update #details set action = '产品' where action = '4' update #details set action = '消费' where action = '3' update a set a.person1 = d.haa02c from #details a, ham01 d where a.person1 = d.haa01c and a.compid = d.haa00c and isnull(a.person1, '') <> '' update a set a.person2 = d.haa02c from #details a, ham01 d where a.person2 = d.haa01c and a.compid = d.haa00c and isnull(a.person2, '') <> '' update a set a.person3 = d.haa02c from #details a, ham01 d where a.person3 = d.haa01c and a.compid = d.haa00c and isnull(a.person3, '') <> '' end select * from #details order by date, ttime, seq drop table #details end GO /****** Object: StoredProcedure [dbo].[upg_consume_statistic] Script Date: 06/12/2017 06:19:45 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO Create procedure [dbo].[upg_consume_statistic] ( @flag varchar(2), -- 是否列印产品资料(1-列印项目资料 2-列印产品资料 3-列印项目和产品资料) @conditionP nvarchar(1024), -- 项目资料条件执行语句(前台组织好的) @conditionG nvarchar(1024), -- 产品资料执行语句(前台组织好的) @ret_type int, -- 返回类型, 1-原来的完整的明细 2-按项目分组合计 3-按项目类别 @addtoSMS int, @compid varchar(10), @project int, @goods int, @paycode varchar(20), @fromtime varchar(6), @totime varchar(6), @fromdate varchar(8), @todate varchar(8), @type varchar(4), @frombill varchar(20), @tobill varchar(20), @fromcard varchar(20), @tocard varchar(20), @fromcardtype varchar(20), @tocardtype varchar(20) ) as--WUXM, 2004/12/26 begin -- 因java请求无法识别WWQ = #,WWR = $, WWE = & exec upg_paymode_to_java @paycode,@paycode output exec upg_paymode_to_java @conditionP,@conditionP output exec upg_paymode_to_java @conditionG,@conditionG output create table #struct -- 产品消费单的临时表 ( id int identity not null, billid varchar(20) null, -- 单号 billtype varchar(5) null, -- 单据别 type_gz_gn varchar(5) null, -- 单据别(gz,gn) cardno varchar(20) null, -- 会员卡编号 gender int null, -- 性别 checkdate varchar(10) null, -- 结账日期 checktime varchar(8) null, -- 结账时间 kind varchar(2000) null, -- 品种 price float null, -- 实收单价 quantity float null, -- 数量 summoney float null, -- 金额 itemtype int null, -- 类别, 1-项目,2-产品 srv_type varchar(20) null, -- 大工类型,指定或轮班 staff01 varchar(20) null, -- 第一销售 staff01_p float null, -- 第一销售分享比例----------ADD BY LEIJI 2012-7-23 staff01_kpi float null, -- 第一销售业绩--------------add by lm 2015.12.29 staff02 varchar(20) null, -- 第二销售 staff02_p float null, -- 第二销售分享比例----------ADD BY LEIJI 2012-7-23 staff02_kpi float null, -- 第二销售业绩--------------add by lm 2015.12.29 staff03 varchar(20) null, -- 第三销售 staff03_p float null, -- 第三销售分享比例----------ADD BY LEIJI 2012-7-23 staff03_kpi float null, -- 第二销售业绩--------------add by lm 2015.12.29 payway varchar(40) null, -- 项目支付方式 paycode varchar(40) null, -- 项目支付方式 代码 --------add by lm on 2015/03/17 memid varchar(20) null, -- 会员编号 confirmer varchar(20) null, -- 复核人 m_name varchar(100) null, -- 会员名称 cardtype varchar(100) null, -- 卡类别名称 --add by leiji on 2013年4月10日 cardtypecode varchar(100) null, -- 卡类别代码 --add by limin on 2015/01/31 isold int null, -- 是否老客 person_quan float null, -- 客户数量 srv_type02 varchar(20) null, -- 中工类型 --add by lyj srv_type03 varchar(20) null, -- 小工类型 srv_type04 varchar(20) null, -- 四工类型 staff04 varchar(20) null, -- 四工代码, staff04_kpi float null, -- 第四销售业绩--------------add by lm 2015.12.29 staff05 varchar(20) null, -- 第五员工, staff05_kpi float null, -- 第五销售业绩--------------add by lm 2015.12.29 acctdate varchar(20) null, -- 帐务日期,gga80d compid varchar(10) null, -- 公司别--add by lyj compidname varchar(100) null, --公司名称 billtotalamt float default 0 null, -- 单据的总金额 --add by lyj stdprice float null, -- 标准单价 stdamt float null, -- 标准金额 card_act float null, -- 卡付实际营业额 memo varchar(1000) null, -- 备注 primary key (id), ) create table #struct_2 -- 产品消费单的临时表 ( id int identity not null, billid varchar(20) null, -- 单号 billtype varchar(5) null, -- 单据别 type_gz_gn varchar(5) null, -- 单据别(gz,gn) cardno varchar(20) null, -- 会员卡编号 gender int null, -- 性别 checkdate varchar(10) null, -- 结账日期 checktime varchar(8) null, -- 结账时间 kind varchar(200) null, -- 品种 quantity float null, -- 数量 summoney float null, -- 金额 itemtype int null, -- 类别, 1-项目,2-产品 payway varchar(40) null, -- 项目支付方式 paycode varchar(40) null, -- 项目支付方式 代码 --------add by lm on 2015/03/17 staff01 varchar(20) null, -- 第一销售 staff01_p float null, -- 第一销售分享比例----------ADD BY LEIJI 2012-7-23 staff01_kpi float null, -- 第一销售业绩--------------add by lm 2015.12.29 staff02 varchar(20) null, -- 第二销售 staff02_p float null, -- 第二销售分享比例----------ADD BY LEIJI 2012-7-23 staff02_kpi float null, -- 第二销售业绩--------------add by lm 2015.12.29 staff03 varchar(20) null, -- 第三销售 staff03_p float null, -- 第三销售分享比例----------ADD BY LEIJI 2012-7-23 staff03_kpi float null, -- 第二销售业绩--------------add by lm 2015.12.29 memid varchar(20) null, -- 会员编号 confirmer varchar(20) null, -- 复核人 m_name varchar(100) null, -- 会员名称 cardtype varchar(100) null, -- 卡类别名称 --add by leiji on 2013年4月10日 cardtypecode varchar(100) null, -- 卡类别代码 --add by limin on 2015/01/31 staff04 varchar(20) null, -- 四工代码, staff04_kpi float null, -- 第四销售业绩--------------add by lm 2015.12.29 staff05 varchar(20) null, -- 第五销售虚业绩, staff05_kpi float null, -- 第五销售业绩--------------add by lm 2015.12.29 acctdate varchar(20) null, -- 帐务日期,gga80d compid varchar(10) null, -- 公司别--add by lyj billtotalamt float default 0 null, -- 单据的总金额 --add by lyj stdamt float null, -- 标准金额 card_act float null, -- 卡付实际营业额 memo varchar(1000) null, -- 备注 -- pay_1 varchar(10) null, -- pay_2 varchar(10) null, -- pay_3 varchar(10) null, -- pay_4 varchar(10) null, -- pay_amt_1 float null, -- pay_amt_2 float null, -- pay_amt_3 float null, -- pay_amt_4 float null primary key (id), ) create table #gcm11 ( gcm00c varchar (20) not null, gcm01c varchar (20) not null, gcm02c varchar (20) null, gcm03f float null ) create table #gnm03 ( gnc00c varchar (20) not null, gnc01c varchar (20) not null, gnc02c varchar (20) null, gnc03f float null ) create table #gtc06 ( gth00c varchar (20) not null, gth01c varchar (20) not null, gth02c varchar (20) null, gth04f float null ) create table #gtc05 ( gtf00c varchar (20) not null, gtf01c varchar (20) not null, gtf02c varchar (20) null, gtf04f float null ) create table #gsm03 ( gsc00c varchar(20) not null, gsc01c varchar(20) not null, gsc02c varchar(2) not null, gsc05f float null, ) insert #gsm03 (gsc00c,gsc01c,gsc02c,gsc05f) select gsc00c,gsc01c,gsc02c,gsc05f from gsm03 with(nolock) where gsc00c = @compid and (gsc10d >= @fromdate and gsc10d <= @todate) and (gsc01c = @type or @type = '*') declare @sql_p nvarchar(4000) declare @sql_g nvarchar(4000) declare @staff01 varchar(20) declare @staff02 varchar(20) declare @staff03 varchar(20) declare @staff04 varchar(20) declare @sp031f float declare @incompid varchar(10) select @incompid = @compid select @sp031f = gsa03f from gsm01 with (nolock) where gsa00c = @compid and gsa01c = 'SP031' select @staff01 = isnull(gsb03c, '') from gsm02 with (nolock) where gsb01c = 'AK' and gsb02c = '1' select @staff02 = isnull(gsb03c, '') from gsm02 with (nolock) where gsb01c = 'AK' and gsb02c = '2' select @staff03 = isnull(gsb03c, '') from gsm02 with (nolock) where gsb01c = 'AK' and gsb02c = '3' select @staff04 = isnull(gsb03c, '') from gsm02 with (nolock) where gsb01c = 'AK' and gsb02c = '4' -- modify by lyj 添加账务时间的过滤条件 2007-12-14 账务时间只精确到分钟 -- begin if isnull(@fromtime, '') = '' begin set @fromtime = '000000' set @totime = '240000' end else begin set @fromtime = substring(@fromtime, 1, 6) end if isnull(@totime, '') = '' set @totime = '240000' else set @totime = substring(@totime, 1, 6) -- end if @flag = '1' begin if @sp031f = 2 -- 出院结帐 begin select @sql_p = 'insert #struct(billid, billtype, cardno, checkdate, checktime, kind, price, quantity, summoney, itemtype, staff01, staff02, staff03, srv_type, gender, payway, paycode, memid, confirmer, isold, person_quan, srv_type02, srv_type03, srv_type04, staff04, acctdate, billtotalamt, compid, stdprice, m_name, memo ) select gga01c, isnull(gga29c, ''gx''), isnull(gga09c, ''''), gla96d, gla04t, ggb03c, ggb10f, ggb05f, ggb11f, 1, ggb12c, ggb13c, ggb14c, ggb15c, gla10i, gsb03c, gsb02c, gla27c, gga93c, gga13f, gga14f, ggb16c, ggb19c, ggb25c, ggb26c, gga80d, isnull(gga16f, 0), gga00c, ggb08f, gga06c, case when isnull(ggm02.memo, '''') = '''' then '''' else ''单据备注: '' + ggm02.memo end from ggm01 with (nolock), ggm02 with (nolock), glm01 with (nolock), gsm02 with (nolock) , gam26 z with (nolock) where gga00c = ggb00c and gga01c = ggb01c and gga00c = gla00c and gga30c = gla01c and ggb00c *= gsb00c and ggb27c *= gsb02c and gga00c = gaz02c and isnull(gla80d, '''') <> '''' and gsb01c = ''3'' ' + @conditionP end else -- 收银结帐 begin select @sql_p = 'insert #struct(billid, billtype, cardno, checkdate, checktime, kind, price, quantity, summoney, itemtype, staff01, staff02, staff03, srv_type, gender, payway, paycode, memid, confirmer, isold, person_quan, srv_type02, srv_type03, srv_type04, staff04, acctdate, billtotalamt, compid, stdprice, m_name, memo) select gga01c, isnull(gga29c, ''gx''), isnull(gga09c, ''''), gga02d, gga04t, ggb03c, ggb10f, ggb05f, ggb11f, 1, ggb12c, ggb13c, ggb14c, ggb15c, gga31i, gsb03c, gsb02c, gga05c, gga93c, gga13f, gga14f, ggb16c, ggb19c, ggb25c, ggb26c, gga80d, isnull(gga16f, 0), gga00c, ggb08f, gga06c, case when isnull(ggm02.memo, '''') = '''' then '''' else ''单据备注: '' + ggm02.memo end from ggm01 with (nolock), ggm02 with (nolock), gsm02 with (nolock) , gam26 z with (nolock) where gga00c = ggb00c and gga01c = ggb01c and ggb00c *= gsb00c and ggb27c *= gsb02c and gga00c = gaz02c and gsb01c = ''3'' ' + @conditionP end exec sp_executesql end else begin if @flag = '2' -- 产品销售 begin select @sql_g = 'insert #struct(billid, billtype, cardno, checkdate, checktime, kind, price, quantity, summoney, itemtype, staff01, staff01_p, staff02, staff02_p, staff03, staff03_p, gender, memid, confirmer, isold, person_quan, acctdate, billtotalamt, compid, stdprice, m_name) select gga01c, isnull(gga29c, ''gx''), isnull(gga09c, ''''), gga02d, gga04t, ggc03c, ggc10f, ggc05f, ggc11f, 2, ggc12c, ggc14f, ggc15c, ggc16f, ggc17c, ggc18f, gga31i, gga05c, gga93c, gga13f, gga14f, gga80d, isnull(gga16f, 0), gga00c, ggc08f, gga06c from ggm01 with (nolock) , gam26 with (nolock) , ggm03 with (nolock) where gga00c = ggc00c and gga01c = ggc01c and gga00c = gaz02c ' + @conditionG exec sp_executesql @sql_g end else -- 项目 + 产品销售 begin if( @type = 'gx' or @type = '*') begin if @sp031f = 2 -- 出院结帐 begin if (@project = 1)-- 出院结帐 begin select @sql_p = 'insert #struct(billid, billtype,type_gz_gn, cardno, checkdate, checktime, kind, price, quantity, summoney, itemtype, staff01, staff02, staff03, srv_type, gender, payway, paycode, memid, confirmer, isold, person_quan, srv_type02, srv_type03, srv_type04, staff04, acctdate, billtotalamt, compid, stdprice, m_name, memo, staff01_kpi, staff02_kpi, staff03_kpi, staff04_kpi,card_act) select gga01c, isnull(gga29c, ''gx''),isnull(gga29c, ''gx''), isnull(gga09c, ''''), gla96d, gla04t, ggb03c, ggb10f, ggb05f, ggb11f, 1, ggb12c, ggb13c, ggb14c, ggb15c, gla10i, gsb03c, gsb02c, gla27c, gga93c, gga13f, gga14f, ggb16c, ggb19c, ggb25c, ggb26c, gga80d, isnull(gga16f, 0), gga00c, ggb08f, gga06c, case when isnull(ggm02.memo, '''') = '''' then '''' else ''单据备注: '' + ggm02.memo end, ggb20f, ggb21f, ggb22f, ggb35f,isnull(isnull(ggb11f, 0) * isnull(ggb34f, 1), 0) from ggm01 with (nolock), ggm02 with (nolock), glm01 with (nolock), gsm02 with (nolock) , gam26 with (nolock) where gga00c = ggb00c and gga01c = ggb01c and gga00c = gla00c and gga30c = gla01c and ggb00c *= gsb00c and ggb27c *= gsb02c and gga00c = gaz02c and isnull(gla80d, '''') <> '''' and gsb01c = ''3'' ' + @conditionP exec sp_executesql @sql_p end if (@goods = 1)-- 出院结帐 begin select @sql_g = 'insert #struct(billid, billtype,type_gz_gn, cardno, checkdate, checktime, kind, price, quantity, summoney, itemtype, staff01, staff01_p, staff02, staff02_p, staff03, staff03_p, gender, payway, paycode, memid, confirmer, isold, person_quan, acctdate, billtotalamt, compid, stdprice, m_name, staff01_kpi, staff02_kpi, staff03_kpi,card_act) select gga01c, isnull(gga29c, ''gx''),isnull(gga29c, ''gx''), isnull(gga09c, ''''), gla96d, gla04t, ggc03c, ggc10f, ggc05f, ggc11f, 2, ggc12c, ggc14f, ggc15c, ggc16f, ggc17c, ggc18f, gla10i, gsb03c, gsb02c, gla27c, gga93c, gga13f, gga14f, gga80d, isnull(gga16f, 0), gga00c, ggc08f, gga06c, ggc21f, ggc23f, ggc25f,isnull(isnull(ggc11f, 0) * isnull(ggc30f, 1), 0) from ggm01 with (nolock), ggm03 with (nolock), glm01 with (nolock), gsm02 with (nolock) , gam26 with (nolock) where gga00c = ggc00c and gga01c = ggc01c and gga00c = gla00c and gga30c = gla01c and ggc00c *= gsb00c and ggc27c *= gsb02c and gga00c = gaz02c and isnull(gla80d, '''') <> '''' and gsb01c = ''3'' ' + @conditionG exec sp_executesql @sql_g end end else -- 收银结帐 begin if (@project = 1)-- 收银结帐 begin select @sql_p = 'insert #struct(billid, billtype,type_gz_gn, cardno, checkdate, checktime, kind, price, quantity, summoney, itemtype, staff01, staff02, staff03, srv_type, gender, payway, paycode, memid, confirmer, isold, person_quan, srv_type02, srv_type03, srv_type04, staff04, acctdate, billtotalamt, compid, stdprice, m_name, memo, staff01_kpi, staff02_kpi, staff03_kpi, staff04_kpi,card_act) select gga01c, isnull(gga29c, ''gx''),isnull(gga29c, ''gx''), isnull(gga09c, ''''), gga02d, gga04t, ggb03c, ggb10f, ggb05f, ggb11f, 1, ggb12c, ggb13c, ggb14c, ggb15c, gga31i, gsb03c, gsb02c, gga05c, gga93c, gga13f, gga14f, ggb16c, ggb19c, ggb25c, ggb26c, gga80d, isnull(gga16f, 0), gga00c, ggb08f, gga06c, case when isnull(ggm02.memo, '''') = '''' then '''' else ''单据备注: '' + ggm02.memo end, ggb20f, ggb21f, ggb22f, ggb35f,isnull(isnull(ggb11f, 0) * isnull(ggb34f, 1), 0) from ggm01 with (nolock), ggm02 with (nolock), gsm02 with (nolock) , gam26 with (nolock) where gga00c = ggb00c and gga01c = ggb01c and ggb00c *= gsb00c and ggb27c *= gsb02c and gga00c = gaz02c and gsb01c = ''3'' ' + @conditionP exec sp_executesql @sql_p end if (@goods = 1)-- 收银结帐 begin select @sql_g = 'insert #struct(billid, billtype,type_gz_gn, cardno, checkdate, checktime, kind, price, quantity, summoney, itemtype, staff01, staff01_p, staff02, staff02_p, staff03, staff03_p, gender, payway, paycode, memid, confirmer, isold, person_quan, acctdate, billtotalamt, compid, stdprice, m_name, staff01_kpi, staff02_kpi, staff03_kpi,card_act) select gga01c, isnull(gga29c, ''gx''),isnull(gga29c, ''gx''), isnull(gga09c, ''''), gga02d, gga04t, ggc03c, ggc10f, ggc05f, ggc11f, 2, ggc12c, ggc14f, ggc15c, ggc16f, ggc17c, ggc18f, gga31i, gsb03c, gsb02c, gga05c, gga93c, gga13f, gga14f, gga80d, isnull(gga16f, 0), gga00c, ggc08f, gga06c, ggc21f, ggc23f, ggc25f,isnull(isnull(ggc11f, 0) * isnull(ggc30f, 1), 0) from ggm01 with (nolock), ggm03 with (nolock), gsm02 with (nolock) , gam26 with (nolock) where gga00c = ggc00c and gga01c = ggc01c and ggc00c *= gsb00c and ggc27c *= gsb02c and gga00c = gaz02c and gsb01c = ''3'' ' + @conditionG exec sp_executesql @sql_g end end declare @sp215f float --储值方式支付是否作为卡付实际营业额 select @sp215f = gsa03f from gsm01 with (nolock) where gsa00c = @compid and gsa01c = 'SP212' if(@sp215f = 1) begin update a set a.card_act = 0 from #struct a where paycode not in ('4','9','#') and ((paycode <'A' or paycode > 'Z') or (paycode in('WX','ZFB'))) end else begin update a set a.card_act = 0 from #struct a where paycode not in ('4','#') and ((paycode <'A' or paycode > 'Z') or (paycode in('WX','ZFB'))) end end end --Start 增加卖卡和充值 add by lm 2015.12.29 if(@type = '*' or @type = 'gz') begin --卡充值 insert #struct_2(billid, billtype,type_gz_gn, cardno, checkdate, acctdate,checktime, memid, itemtype, staff01, staff02, staff03,staff04, staff05, billtotalamt, stdamt, compid, staff01_kpi, staff02_kpi, staff03_kpi,staff04_kpi,staff05_kpi, kind, payway, confirmer, quantity) select gcl01c, 'gz' ,'gz', gcl03c, gcl02d, gcl02d, gcl56t, gcl03c,3, gcl09c, gcl17c,gcl19c,gcl22c,gcl24c,--+':'+gcl68f+gcl69c+':'+gcl70f+gcl71c+':'+gcl72f+gcl73c+':'+gcl74f, gcl07f,gcl07f, gcl00c, gcl58f, gcl60f, gcl62f,gcl83f,gcl84f, gsb03c,[dbo].[payway_gsm03](gcl00c,gcl01c,'gz'),gcl93c, 1 from gcm10 a with(nolock), gcm01 b with(nolock), gsm02 c with (nolock),gam26 z with (nolock) where c.gsb00c = a.gcl00c and c.gsb02c = a.gcl04c --and a.gcl00c = b.gca00c and a.gcl03c = b.gca01c and ((b.gca02c >= @fromcardtype and b.gca02c <= @tocardtype) or (@fromcardtype = '*') ) and b.gca00c = b.gca13d and c.gsb01c = 'O' and a.gcl00c = z.gaz02c and z.gaz01c = @compid and ((a.gcl03c >= @fromcard and a.gcl03c <= @tocard) or (@fromcard = '*')) and ((a.gcl01c >= @frombill and a.gcl01c <= @tobill) or (@frombill = '*')) and ((a.gcl02d >= @fromdate and a.gcl02d <= @todate) or (@fromdate = '*')) end if(@type = '*' or @type = 'gn') begin --卖卡 insert #struct_2(billid, billtype,type_gz_gn, cardno, checkdate, acctdate, checktime, memid, itemtype, staff01, staff02, staff03, staff04, staff05, billtotalamt, stdamt, compid, staff01_kpi, staff02_kpi, staff03_kpi,staff04_kpi,staff05_kpi, kind, payway, confirmer, quantity) select gna01c, 'gn','gn', gna13c, gna02d, gna02d, gna12t, gna13c,4, gna20c, gna28c,gna30c,gna32c,gna34c,--+':'+gnb16f+gnb17c+':'+gnb18f+gnb19c+':'+gnb20f+gnb21c+':'+gnb22f, gnb08f,gnb08f, gna00c,gna67f, gna69f, gna71f,gna86f,gna87f,gsb03c,[dbo].[payway_gsm03](gna00c,gna01c,'gn'),gna93c, 1 from gnm01 a with(nolock), gnm02 b with (nolock),gsm02 c,gam26 z with (nolock),gcm01 d with(nolock) where a.gna00c = b.gnb00c and a.gna01c = b.gnb01c and c.gsb00c = a.gna00c and c.gsb02c = b.gnb13i and a.gna00c = d.gca00c and a.gna13c = d.gca01c and ((d.gca02c >= @fromcardtype and d.gca02c <= @tocardtype) or (@fromcardtype = '*')) and d.gca00c = d.gca13d and c.gsb01c = 'O' and a.gna00c = z.gaz02c and z.gaz01c = @compid and ((a.gna13c >= @fromcard and a.gna13c <= @tocard) or (@fromcard = '*')) and ((a.gna01c >= @frombill and a.gna01c <= @tobill) or (@frombill = '*')) and ((a.gna02d >= @fromdate and a.gna02d <= @todate) or (@fromdate = '*')) end insert #struct (billid, billtype,type_gz_gn, cardno, checkdate, checktime,kind, summoney, itemtype, staff01,staff02, staff03, staff04, staff05, memid, acctdate, billtotalamt, compid, m_name, staff01_kpi, staff02_kpi, staff03_kpi,staff04_kpi,staff05_kpi, confirmer, quantity,card_act) select billid, billtype, type_gz_gn, cardno, checkdate, checktime,kind, summoney, itemtype, staff01,staff02, staff03,staff04, staff05, memid, acctdate, billtotalamt, compid, m_name, staff01_kpi, staff02_kpi, staff03_kpi,staff04_kpi,staff05_kpi, confirmer, quantity,card_act from #struct_2 end update a set a.stdamt = a.stdprice * quantity from #struct a -- add by lyj for 北京 一个单子应该只有一个总计金额 update a set a.billtotalamt = 0 from #struct a, #struct b where a.compid = b.compid and a.billid = b.billid and a.id > b.id -- end -- 不在时间范围内的过滤掉 -- 可能没有查到时间 delete #struct where isnull(checktime, '') <> '' and (isnull(checktime, '') < @fromtime or isnull(checktime, '') > @totime) update #struct set gender = 0 where isnull(gender, -1) = -1 update a set a.m_name = b.gba03c from #struct a, gbm01 b with (nolock), gam26 z with (nolock) where a.memid = b.gba01c and b.gba00c = z.gaz02c and z.gaz01c = @incompid and a.memid not like '散客%' update a set a.cardtype = c.gak02c, a.cardtypecode = c.gak01c --add by llimin 2015/01/31 from #struct a, gcm01 b with (nolock), gam10 c with (nolock) where a.memid = b.gca01c and c.gak00c = b.gca00c and c.gak01c = b.gca02c and gca00c = gca13d --更改公司名称 update a set compidname = compid+'('+b.gae03c+')' from #struct a, gam05 b with(nolock) where a.compid = b.gae01c -- 更新备注 update #struct set memo = isnull(memo, '') + ' 支付备注: ' + gsc06c from #struct, gsm03 with (nolock), gsm02 with (nolock) where gsc00c = compid and gsc01c = billid and gsc04c = gsb02c and gsb03c = payway and gsc02c = 'gx' and gsb01c = '3' and gsb00c = compid and isnull(gsc06c, '') <> '' and id in (select min (id) as iid from #struct, gsm03 with (nolock), gsm02 with (nolock) where gsc00c = compid and gsc01c = billid and gsc04c = gsb02c and gsc02c = 'gx' and gsb03c = payway and gsb01c = '3' and gsb00c = compid group by billid, compid, payway) if @ret_type = 1 begin if(@type <> 'gx') begin declare @id int declare @state int set @state = 0 declare @cardno varchar(20) declare @billid varchar(20) declare @billtype varchar(2) declare @comp varchar(20) declare @kind varchar(40) declare @str varchar(200) declare @payway varchar(40) declare @payamt float while(@state = 0) begin if exists(select 1 from #struct_2) begin select top 1 @id = id, @cardno = cardno, @billid = billid, @billtype = billtype, @comp = compid, @payway = payway from #struct_2 select @payamt = sum(gsc05f) from gsm03 with(nolock) where gsc00c = @comp and gsc01c = @billid and gsc02c = @billtype if(@billtype = 'gz') begin insert #gcm11(gcm00c, gcm01c, gcm02c, gcm03f) select gcm00c, gcm01c, gcm02c, sum(gcm03f) from gcm11 a with (nolock),gam26 z with (nolock) where a.gcm00c = z.gaz02c and z.gaz01c = @compid and a.gcm01c = @billid group by gcm00c, gcm01c, gcm02c insert #gtc06(gth00c, gth01c, gth02c, gth04f) select gth00c, gth01c, gth02c, sum(gth04f) from gtc06 a with (nolock),gam26 z with (nolock) where a.gth00c = z.gaz02c and z.gaz01c = @compid and a.gth01c = @billid group by gth00c, gth01c, gth02c --疗程 select @str= isnull(@str+':','')+ gda03c + '('+ cast(gcm03f as varchar)+'次)' from #gcm11 a with (nolock), gdm01 b with(nolock) where a.gcm00c = b.gda00c and b.gda01c = a.gcm02c --套餐 select @str= isnull(@str+':','')+ gta02c + '('+ cast(gth04f as varchar)+'次)' from #gtc06 a with (nolock), gtc01 b with(nolock) where a.gth00c = b.gta00c and b.gta01c = a.gth02c end else begin insert #gnm03(gnc00c, gnc01c, gnc02c, gnc03f) select gnc00c, gnc01c, gnc02c, sum(gnc03f) from gnm03 a with (nolock),gam26 z with (nolock) where a.gnc00c = z.gaz02c and z.gaz01c = @compid and a.gnc01c = @billid group by gnc00c, gnc01c, gnc02c insert #gtc05(gtf00c, gtf01c, gtf02c, gtf04f) select gtf00c, gtf01c, gtf02c, sum(gtf04f) from gtc05 a with (nolock),gam26 z with (nolock) where a.gtf00c = z.gaz02c and z.gaz01c = @compid and a.gtf01c = @billid group by gtf00c, gtf01c, gtf02c --疗程 select @str= +isnull(@str+':','')+ gda03c+ '('+ cast(gnc03f as varchar)+'次)' from #gnm03 a with (nolock), gdm01 b with(nolock) where a.gnc00c = b.gda00c and b.gda01c = a.gnc02c --套餐 select @str = + isnull(@str+':','')+ gta02c + '('+ cast(gtf04f as varchar)+'次)' from #gtc05 a with (nolock), gtc01 b with(nolock) where a.gtf00c = b.gta00c and b.gta01c = a.gtf02c end update a set a.payway = @payway, a.kind = isnull(cardtype,'') +':' + kind +isnull(@str,''), a.summoney = @payamt, a.billtotalamt = @payamt, a.stdamt = @payamt from #struct a where a.cardno = @cardno and a.billid = @billid and a.billtype = @billtype and a.compid = @comp set @str = '' delete from #gcm11 delete from #gnm03 delete from #gtc06 delete from #gtc05 delete from #struct_2 where id = @id end else begin set @state = 1 end end end --End --卖卡充值单据类别 归纳到收银单据类别 update a set a.billtype = 'gx' from #struct a where (a.billtype = 'gz' or a.billtype = 'gn') if @addtoSMS = 1 begin create table #tbl_member ( idd int identity not null, cardno varchar(20) null, memid varchar(20) null, mobile varchar(30) null, primary key(idd) ) insert #tbl_member(memid) select distinct memid from #struct delete a from #tbl_member a, gxm01 b with (nolock) where a.memid = b.gxa02c and b.gxa00c = @compid update a set a.mobile = b.gba08c from #tbl_member a, gbm01 b with (nolock) where a.memid = b.gba01c and b.gba00c = @compid delete #tbl_member where isnull(mobile, '') = '' declare @cur_max float select @cur_max = max(gxa01f) from gxm01 with (nolock) where gxa00c = @compid select @cur_max = isnull(@cur_max, 0) insert gxm01(gxa00c, gxa01f, gxa02c, gxa03c) select @compid, idd = @cur_max + idd, memid, mobile from #tbl_member where isnull(memid, '') <> '' drop table #tbl_member end if (isnull(@paycode, '') = '') begin select isnull(billtype,'') billtype, isnull(cardno,'') cardno, isnull(checkdate,'') checkdate, isnull(checktime,'') checktime, isnull(kind,'') kind, isnull(price,'') price, isnull(quantity,'') quantity, isnull(summoney,'') summoney, isnull(itemtype,'') itemtype, isnull(billid,'') billid, isnull(staff01,'') staff01, isnull(staff01_p,'') staff01_p, isnull(staff02,'') staff02, isnull(staff02_p,'') staff02_p, isnull(staff03,'') staff03, isnull(staff03_p,'') staff03_p, isnull(srv_type,'') srv_type, isnull(gender,'') gender, isnull(payway,'') payway, isnull(confirmer,'') confirmer, isnull(m_name,'') m_name, isnull(cardtype,'') cardtype, isnull(cardtypecode,'') cardtypecode, isnull(isold,'') isold, isnull(person_quan,'') person_quan, isnull(staff04,'') staff04,isnull(staff05,'') staff05, isnull(srv_type02,'') srv_type02, isnull(srv_type03,'') srv_type03, isnull(srv_type04,'') srv_type04, isnull(acctdate,'') acctdate, isnull(billtotalamt,'') billtotalamt, isnull(compid,'') compid, isnull(stdprice,'') stdprice, isnull(stdamt,'') stdamt, isnull(memo,'') memo, isnull(staff01_kpi,'') staff01_kpi, isnull(staff02_kpi,'') staff02_kpi, isnull(staff03_kpi,'') staff03_kpi, isnull(staff04_kpi,'') staff04_kpi,isnull(staff05_kpi,'') staff05_kpi, isnull(compidname,'') compidname,isnull(type_gz_gn,'') type_gz_gn, isnull(card_act,'') card_act from #struct where isnull(kind, '') <> '' order by id,type_gz_gn, billid,checkdate, checktime end else begin select isnull(billtype,'') billtype, isnull(cardno,'') cardno, isnull(checkdate,'') checkdate, isnull(checktime,'') checktime, isnull(kind,'') kind, isnull(price,'') price, isnull(quantity,'') quantity, isnull(summoney,'') summoney, isnull(itemtype,'') itemtype, isnull(billid,'') billid, isnull(staff01,'') staff01, isnull(staff01_p,'') staff01_p, isnull(staff02,'') staff02, isnull(staff02_p,'') staff02_p, isnull(staff03,'') staff03, isnull(staff03_p,'') staff03_p, isnull(srv_type,'') srv_type, isnull(gender,'') gender, isnull(payway,'') payway, isnull(confirmer,'') confirmer, isnull(m_name,'') m_name, isnull(cardtype,'') cardtype, isnull(cardtypecode,'') cardtypecode, isnull(isold,'') isold, isnull(person_quan,'') person_quan, isnull(staff04,'') staff04,isnull(staff05,'') staff05, isnull(srv_type02,'') srv_type02, isnull(srv_type03,'') srv_type03, isnull(srv_type04,'') srv_type04, isnull(acctdate,'') acctdate, isnull(billtotalamt,'') billtotalamt, isnull(compid,'') compid, isnull(stdprice,'') stdprice, isnull(stdamt,'') stdamt, isnull(memo,'') memo, isnull(staff01_kpi,'') staff01_kpi, isnull(staff02_kpi,'') staff02_kpi, isnull(staff03_kpi,'') staff03_kpi, isnull(staff04_kpi,'') staff04_kpi,isnull(staff05_kpi,'') staff05_kpi, isnull(compidname,'') compidname,isnull(type_gz_gn,'') type_gz_gn, isnull(card_act,'') card_act from #struct where isnull(kind, '') <> '' and paycode = @paycode order by id,type_gz_gn, billid, checkdate, checktime end end else if @ret_type = 2 or @ret_type = 3 begin create table #group_struct ( idd int identity not null, itemtype varchar(20) null, kind varchar(20) null, gender int null, cnt float null, amt float null, stdamt float null, -- 标准金额 primary key(idd) ) create table #result_struct ( idd int identity not null, itemtype varchar(20) null, kind varchar(20) null, cnt_f float null, amt_f float null, cnt_m float null, amt_m float null, cnt float null, amt float null, totalamt float null, stdamt float null, -- 标准金额 amt_cash float null, amt_card float null, primary key(idd) ) -- 现金临时表 -- add by lm on 2015/03/17 create table #result_cash ( idd int identity not null, itemtype varchar(20) null, kind varchar(20) null, amt_cash float null, primary key(idd) ) -- 卡付临时表 -- add by lm on 2015/03/17 create table #result_card ( idd int identity not null, itemtype varchar(20) null, kind varchar(20) null, amt_card float null, primary key(idd) ) if (@ret_type = 2) begin insert #group_struct(itemtype, kind, gender, cnt, amt, stdamt) select itemtype, kind, gender, sum(person_quan), sum(summoney), sum(stdamt) from (select itemtype, kind, gender, compid, billid, max(person_quan) as person_quan, sum(summoney) as summoney, sum(stdamt) as stdamt from #struct where isnull(kind, '') <> '' and (paycode = @paycode or isnull(@paycode, '') = '') --modified by maql 20100514 增加支付方式查询条件 group by itemtype, kind, compid, billid, gender ) T --modified by maql 20100514 增加支付方式查询条件 group by itemtype, kind, gender end else if (@ret_type = 3) begin insert #group_struct(itemtype, kind, gender, cnt, amt, stdamt) select itemtype, gda13c, gender, sum(person_quan), sum(summoney), sum(stdamt) from (select itemtype, kind, gender, compid, billid, max(person_quan) as person_quan, sum(summoney) as summoney, sum(stdamt) as stdamt from #struct where isnull(kind, '') <> '' and itemtype = 1 and (paycode = @paycode or isnull(@paycode, '') = '') --modified by maql 20100514 增加支付方式查询条件 group by itemtype, kind, compid, billid, gender ) a, gdm01 b with (nolock) where b.gda00c = @compid and a.kind = b.gda01c and isnull(gda13c, '') <> '' group by itemtype, gda13c, gender insert #group_struct(itemtype, kind, gender, cnt, amt, stdamt) select itemtype, gfa05c, gender, sum(person_quan), sum(summoney), sum(stdamt) from (select itemtype, kind, gender, compid, billid, max(person_quan) as person_quan, sum(summoney) as summoney, sum(stdamt) as stdamt from #struct where isnull(kind, '') <> '' and itemtype = 2 and (paycode = @paycode or isnull(@paycode, '') = '') --modified by maql 20100514 增加支付方式查询条件 group by itemtype, kind, compid, billid, gender ) a, gfm01 b with (nolock) where b.gfa00c = @compid and a.kind = b.gfa01c group by itemtype, gfa05c, gender end if (@ret_type = 2) begin insert #result_struct(itemtype, kind, cnt, amt, stdamt) select itemtype, kind, sum(quantity), sum(summoney), sum(stdamt) from #struct where isnull(kind, '') <> '' and (paycode = @paycode or isnull(@paycode, '') = '') --modified by maql 20100514 增加支付方式查询条件 group by itemtype, kind -- #result_card插入卡付数据 -- modify by lm 2015/03/17 insert #result_card(itemtype, kind, amt_card) select itemtype, kind, sum(summoney) from #struct where isnull(kind, '') <> '' and paycode in (select pay from pay_classify where comp = @compid and category = '2') and (paycode = @paycode or isnull(@paycode, '') = '') -- modified by maql 20100514 增加支付方式查询条件 group by itemtype, kind -- #result_cash 插入现金数据 -- modify by lm 2015/03/17 insert #result_cash(itemtype, kind, amt_cash) select itemtype, kind, sum(summoney) from #struct where isnull(kind, '') <> '' and (paycode = @paycode or isnull(@paycode, '') = '') -- modified by maql 20100514 增加支付方式查询条件 and paycode in (select pay from pay_classify where comp = @compid and category = '1') group by itemtype, kind end else if (@ret_type = 3) begin insert #result_struct(itemtype, kind, cnt, amt, stdamt) select itemtype, gda13c, sum(quantity), sum(summoney), sum(stdamt) from #struct a, gdm01 b with (nolock) where isnull(kind, '') <> '' and itemtype = 1 and (a.paycode = @paycode or isnull(@paycode, '') = '') --modified by maql 20100514 增加支付方式查询条件 and b.gda00c = @compid and a.kind = b.gda01c and isnull(gda13c, '') <> '' group by itemtype, gda13c -- #result_card 插入卡付数据 -- modify by lm 2015/03/17 insert #result_card(itemtype, kind, amt_card) select itemtype, gda13c, sum(summoney) from #struct a, gdm01 b with (nolock) where isnull(kind, '') <> '' and itemtype = 1 and a.paycode in (select pay from pay_classify where comp = @compid and category = '2') and (a.paycode = @paycode or isnull(@paycode, '') = '') -- modified by maql 20100514 增加支付方式查询条件 and b.gda00c = @compid and a.kind = b.gda01c and isnull(gda13c, '') <> '' group by itemtype, gda13c -- result_cash 插入现金数据 -- modify by lm 2015/03/17 insert #result_cash(itemtype, kind, amt_cash) select itemtype, gda13c, sum(summoney) from #struct a, gdm01 b with (nolock) where isnull(kind, '') <> '' and itemtype = 1 and a.paycode in (select pay from pay_classify where comp = @compid and category = '1') and (a.paycode = @paycode or isnull(@paycode, '') = '') -- modified by maql 20100514 增加支付方式查询条件 and b.gda00c = @compid and a.kind = b.gda01c and isnull(gda13c, '') <> '' group by itemtype, gda13c insert #result_struct(itemtype, kind, cnt, amt, stdamt) select itemtype, gfa05c, sum(quantity), sum(summoney), sum(stdamt) from #struct a, gfm01 b with (nolock) where isnull(kind, '') <> '' and itemtype = 2 and (a.paycode = @paycode or isnull(@paycode, '') = '') --modified by maql 20100514 增加支付方式查询条件 and b.gfa00c = @compid and a.kind = b.gfa01c group by itemtype, gfa05c -- #result_card 插入卡付数据 -- modify by lm 2015/03/17 insert #result_card(itemtype, kind, amt_card) select itemtype, gfa05c, sum(summoney) from #struct a, gfm01 b with (nolock) where isnull(kind, '') <> '' and itemtype = 2 and a.paycode in (select pay from pay_classify where comp = @compid and category = '2') and (a.paycode = @paycode or isnull(@paycode, '') = '') --modified by maql 20100514 增加支付方式查询条件 and b.gfa00c = @compid and a.kind = b.gfa01c group by itemtype, gfa05c -- result_cash 插入现金数据 -- modify by lm 2015/03/17 insert #result_cash(itemtype, kind, amt_cash) select itemtype, gfa05c, sum(summoney) from #struct a, gfm01 b with (nolock) where isnull(kind, '') <> '' and itemtype = 2 and a.paycode in (select pay from pay_classify where comp = @compid and category = '1') and (a.paycode = @paycode or isnull(@paycode, '') = '') --modified by maql 20100514 增加支付方式查询条件 and b.gfa00c = @compid and a.kind = b.gfa01c group by itemtype, gfa05c end update a set a.cnt_f = b.cnt, a.amt_f = b.amt from #result_struct a, #group_struct b where a.itemtype = b.itemtype and a.kind = b.kind and b.gender = 0 update a set a.cnt_m = b.cnt, a.amt_m = b.amt from #result_struct a, #group_struct b where a.itemtype = b.itemtype and a.kind = b.kind and b.gender = 1 declare @totalamt float select @totalamt = sum(amt) from #result_struct update a set a.totalamt = @totalamt from #result_struct a -- 现金赋值 -- modify by lm 2015/03/17 update a set a.amt_cash = b.amt_cash from #result_struct a, #result_cash b where a.itemtype = b.itemtype and a.kind = b.kind -- 卡付赋值 -- modify by lm 2015/03/17 update a set a.amt_card = b.amt_card from #result_struct a, #result_card b where a.itemtype = b.itemtype and a.kind = b.kind select isnull(idd,'') idd, isnull(itemtype,'') itemtype, isnull(kind,'') kind, isnull(cnt_f,'') cnt_f, isnull(amt_f,'') amt_f, isnull(cnt_m,'') cnt_m, isnull(amt_m,'') amt_m, isnull(cnt,'') cnt, isnull(amt,'') amt, isnull(totalamt,'') totalamt, isnull(stdamt,'') stdamt, -- 标准金额 isnull(amt_cash,'') amt_cash, isnull(amt_card,'') amt_card from #result_struct order by itemtype, amt desc drop table #result_struct drop table #group_struct drop table #result_cash drop table #result_card end drop table #gsm03 drop table #struct drop table #gcm11 drop table #gnm03 drop table #gtc05 drop table #gtc06 drop table #struct_2 end GO /****** Object: StoredProcedure [dbo].[upg_cal_operation_result_new] Script Date: 06/12/2017 06:19:33 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_cal_operation_result_new] ( @compid varchar(10), -- 公司别 @fromdate varchar(10), -- 开始日期 @todate varchar(10), -- 截至日期 @fromstaff varchar(20), -- 员工代码 @tostaff varchar(20), -- 员工代码 @amttype int, -- 0-营业额 1-虚业绩 2-数量 3-实业绩 @printcom varchar(4) -- 打印提成 substring(, 1, 1) --总提成, substring(, 2, 1) - 项目, substring(, 3, 1) -产品 substring(, 4, 1) -卡 ) as begin -- 如果为数量, 也按营业额来生成报表 if @amttype = 2 begin set @amttype = 0 end if isnull(@fromstaff, '') = '' begin set @fromstaff = '*' end declare @sp055f float select @sp055f = gsa03f from gsm01 with(nolock) where gsa00c = @compid and gsa01c = 'SP055' declare @SP048 float select @SP048 = gsa02c from gsm01 with(nolock) where gsa00c = @compid and gsa01c = 'SP048' create table #results ( idd int identity not null, staffno varchar(20) not null, type varchar(10) not null, itemtype varchar(10) null, -- 项目类别 cnt float null, -- 数量 amt float null, -- 业绩 price float null, -- 客单价 cnt_f float null, -- 指定的数量 amt_f float null, -- 指定的金额 cnt_t float null, -- 轮班的数量 amt_t float null, -- 轮班的金额 tamt float null, -- 真实营业额 cash_amt float null, -- 现金总业绩 card_amt float null, -- 划卡总业绩 else_amt float null, -- 其他支付总业绩 cash_cnt float null, -- 现金总数量 card_cnt float null, -- 划卡总数量 else_cnt float null, -- 其他支付总数量 liaoc_cs float null, --疗程购买次数 lucz 2015.10.21 liaoc_cnt float null, --疗程总数量 lucz 2015.10.21 liaoc_t float null, --疗程金额 lucz 2015.10.21 liaoc_sum float null, --疗程销售 总计 liaoc_jine float null, --疗程销售 现金 liaoc_chuz float null, --疗程销售 储值 liaoc_hank float null, --疗程销售 银行卡 liaoc_qita float null, --疗程销售 其他 job_amt float null --劳动业绩 primary key(idd) ) create table #ggm01_ggm02 -- 项目消费单 ( gga00c varchar(10) not null, --公司编号 gga01c varchar(20) not null, --消费单号 gga02d varchar(8) null, --消费日期 gga03t varchar(6) null, --消费开始时间 gga04t varchar(6) null, --消费结束时间 gga05c varchar(20) null, --会员代码, 927版本 gga06c varchar(40) null, --客户姓名,如果是会员则是会员姓名 gga07c varchar(10) null, --项目负责人员(暂不用) gga08c varchar(20) null, --匙牌号 gga09c varchar(20) null, --会员卡号 gga10c varchar(5) null, --币别(不用) gga11f float null, --汇率(不用) gga12c varchar(5) null, --计价单位(不用) gga13f float null, --是否为老客 gga14f float null, --客人数量 gga15f float null, --项目基本金额(不用) gga16f float null, --总金额 gga17f float null, --(销售点)已支付金额 gga18f float null, --折扣 gga19f float null, --折扣单价 gga20c varchar(5) null, --预约单别 gga21c varchar(20) null, --预约单号 gga22c varchar(5) null, --客户类别(已不用) gga23c varchar(5) null, --证件类别(已不用) gga24c varchar(20) null, --证件编号(已不用) gga25c varchar(20) null, --赠券编号(已不用) gga26f float null, --预付金额(已不用) gga27i int null, --账户类别, gga28c varchar(10) null, --接待人员(已不用) gga29c varchar(5) null, --单据类别 gga30c varchar(20) null, --入馆单号 gga31i int null, --顾客性别 gga91c varchar(10) null, --登陆人员代码 gga92d varchar(8) null, --登陆日期 gga93c varchar(10) null, --覆核人员代码 gga94d varchar(8) null, --覆核日期 gga95c varchar(10) null, --结账人员(壁球)(不用) gga96d varchar(8) null, --结账日期(壁球)(不用) gga80d varchar(8) null, --帐务日期, added by WUXM, 2005/08/15 gga81i int null, --是否寄存 ggb00c varchar(10) not null, --公司编号 ggb01c varchar(20) not null, --消费单号 ggb02f float not null, --序号 ggb03c varchar(20) null, --子项目代码 ggb04c varchar(5) null, --单位 ggb05f float null, --数量 ggb06c varchar(5) null, --币别 ggb07f float null, --汇率 ggb08f float null, --标准单价 ggb09f float null, --折让 ggb10f float null, --折让单价 ggb11f float null, --金额 ggb12c varchar(20) null, --大工代码 ggb13c varchar(20) null, --中工代码 ggb14c varchar(20) null, --小工代码 ggb15c varchar(20) null, --大工类型 ggb16c varchar(20) null, --中工类型 ggb17f float null, --成本金额 ggb18f float null, --转卡比率 ggb19c varchar(20) null, --小工类型 ggb20f float null, --大工虚业绩 ggb21f float null, --中工虚业绩 ggb22f float null, --小工虚业绩 ggb23c varchar(128) null, --项目打折原因, added by WUXM, 2005/09/01 ggb24f float null, --项目应收金额, added by WUXM, 2005/09/08 ggb25c varchar(20) null, --四工类型 ggb26c varchar(20) null, --四工代码 ggb27c varchar(5) null, --项目支付方式 Added by WUXM 2005/11/09 ggb28f float null, --大工提成 Added by WUXM 2005/11/10 ggb29f float null, --中工提成 Added by WUXM 2005/11/10 ggb30f float null, --小工提成 Added by WUXM 2005/11/10 ggb31c varchar(20) null, --五工类别 ggb32c varchar(20) null, --五工代码 ggb33f float null, --项目积分 ggb34f float null, --项目的折算率 ggb35f float null, --四工虚业绩 ggb36f float null, --大工实业绩 ggb37f float null, --中工实业绩 ggb38f float null, --小工实业绩 ggb39f float null --四工实业绩 ) create clustered index idx_ggm01_ggm02 on #ggm01_ggm02(ggb01c, ggb02f) --把单据明细列转行 create table #bills ( idd int identity not null, billid varchar(20) not null, --单号 billtype int not null, --1项目, 2产品 gender int default 0 not null, --性别 isold int default 0 not null, --老客 itemtype varchar(10) default '' not null, --项目分类 detail_id float not null, --ggm02 id, ggm03 id staffno varchar(20) not null, --员工编号 stafftype varchar(5) default '' not null, --方式 paytype varchar(5) default '' not null, --支付方式 num float default 0 not null, --数量 amt float default 0 not null, --营业额 real_amt float default 0 not null, --实际营业额 real_ratio float default 0 not null, --实际营业比率 v_amt float default 0 not null, --虚业绩 r_amt float default 0 not null, --实业绩 ret_amt float default 0 not null, --报表需要返回的业绩 PRIMARY KEY NonCLUSTERED(idd) ) create clustered index idx_ggm01_ggm02 on #bills(billid, detail_id) --员工所有的单据 create table #allbill ( staffno varchar(20) not null, -- 员工 billid varchar(20) not null, -- 单号 gender int not null, -- 性别 isold float not null, -- 老客 type varchar(5) not null, -- 点/轮 iscard int not null, -- 1 卡, 2 现金, 0其他 num float default 0 not null, --数量 --add by lm 2015-07-16 -- PRIMARY KEY NonCLUSTERED(staffno, billid) ) create clustered index idx_allbill on #allbill(billid, staffno) create table #bills_2 ( billid varchar(20) not null, -- 单号 staffno varchar(20) not null, -- 员工 billtype int not null --1项目, 2产品 ) create table #bills_3 ( billid varchar(20) not null, -- 单号 billtype int not null, --1项目, 2产品 ret_amt float null ) create table #work_detail_new ( id int identity, -- 流水好 person_id varchar(20) not null, -- 员工代码 action_id tinyint not null, -- 类别,1-卡销售 2-指定大工,3-轮班大工,4-补工大工, -- 5-指定中工,6-轮班中工 -- 7-指定小工, 8-轮班小工,9-产品销售 10,11-四工 -- 0 卡充值 srvdate varchar(10) not null, --日期 code varchar(20) not null, --项目代码,或是卡号,产品码 name varchar(200) null, --名称 payway varchar(20) null, --支付方式 amt float null, --营业金额 quan float null, --数量 cost float null, --成本 comm float null, --提成 amt2 float null, --虚业绩 amt3 float null, --真实业绩 cardrate float null, --转卡比率 --当买卡或充值的时候,这个地方当作疗程账户金额用 stdprice float null, --标准单价 prj_type varchar(20) null, --项目类别 share_rate float null default 1, --分享比率 cls_flag int null, -- 1:项目 2:产品 3:卡 gender int null, --性别 0:女 1:男 billid varchar(20) null, --单号 paycode varchar(20) null, --支付代码 ex_rate float null, --实际营业额折算比率 ggm02_seq float null, --ggm02中的序列号,与单号一起来标志一个项目 room_id varchar(20) null, --房间号,只在spa或足浴版本需要 compid varchar(10) null, --公司别 spid int null, --连接号 cardid varchar(20) null, --会员卡号 memid varchar(40) null, --会员编号 comp_ascription varchar(10) null,-- 作业公司 PRIMARY KEY NonCLUSTERED(id) ) create clustered index work_detail_new_idx_01 on #work_detail_new(spid,person_id) insert #ggm01_ggm02(gga00c, gga01c, gga02d, gga03t, gga04t, gga05c, gga06c, gga07c, gga08c, gga09c, gga10c, gga11f, gga12c, gga13f, gga14f, gga15f, gga16f, gga17f, gga18f, gga19f, gga20c, gga21c, gga22c, gga23c, gga24c, gga25c, gga26f, gga27i, gga28c, gga29c, gga30c, gga31i, gga91c, gga92d, gga93c, gga94d, gga95c, gga96d, gga80d, gga81i, ggb00c, ggb01c, ggb02f, ggb03c, ggb04c, ggb05f, ggb06c, ggb07f, ggb08f, ggb09f, ggb10f, ggb11f, ggb12c, ggb13c, ggb14c, ggb15c, ggb16c, ggb17f, ggb18f, ggb19c, ggb20f, ggb21f, ggb22f, ggb23c, ggb24f, ggb25c, ggb26c, ggb27c, ggb28f, ggb29f, ggb30f, ggb31c, ggb32c, ggb33f, ggb34f, ggb35f, ggb36f, ggb37f, ggb38f, ggb39f) select gga00c, gga01c, gga02d, gga03t, gga04t, gga05c, gga06c, gga07c, gga08c, gga09c, gga10c, gga11f, gga12c, gga13f, gga14f, gga15f, gga16f, gga17f, gga18f, gga19f, gga20c, gga21c, gga22c, gga23c, gga24c, gga25c, gga26f, gga27i, gga28c, gga29c, gga30c, gga31i, gga91c, gga92d, gga93c, gga94d, gga95c, gga96d, gga80d, gga81i, ggb00c, ggb01c, ggb02f, ggb03c, ggb04c, ggb05f, ggb06c, ggb07f, ggb08f, ggb09f, ggb10f, ggb11f, ggb12c, ggb13c, ggb14c, ggb15c, ggb16c, ggb17f, ggb18f, ggb19c, ggb20f, ggb21f, ggb22f, ggb23c, ggb24f, ggb25c, ggb26c, ggb27c, ggb28f, ggb29f, ggb30f, ggb31c, ggb32c, ggb33f, ggb34f, ggb35f, ggb36f, ggb37f, ggb38f, ggb39f from ggm01 a with (nolock), ggm02 b with (nolock), gam26 z with (nolock) where a.gga00c = b.ggb00c and a.gga01c = b.ggb01c and a.gga00c = z.gaz02c and z.gaz01c = @compid and a.gga80d >= @fromdate and a.gga80d <= @todate and (@fromstaff = '*' or b.ggb12c between @fromstaff and @tostaff or b.ggb13c between @fromstaff and @tostaff or b.ggb14c between @fromstaff and @tostaff or b.ggb26c between @fromstaff and @tostaff) insert #bills(billid, detail_id, staffno, stafftype, v_amt, r_amt, itemtype, billtype) --大工 select isnull(gga01c, ''), isnull(ggb02f, 1), isnull(ggb12c, ''), isnull(ggb15c, '2'), isnull(ggb20f, 0), isnull(ggb36f, 0), '', 1 from #ggm01_ggm02 where isnull(ggb12c, '') != '' union all --中工 select isnull(gga01c, ''), isnull(ggb02f, 1), isnull(ggb13c, ''), isnull(ggb16c, '2'), isnull(ggb21f, 0), isnull(ggb37f, 0), '', 1 from #ggm01_ggm02 where isnull(ggb13c, '') != '' union all --小工 select isnull(gga01c, ''), isnull(ggb02f, 1), isnull(ggb14c, ''), isnull(ggb19c, '2'), isnull(ggb22f, 0), isnull(ggb38f, 0), '', 1 from #ggm01_ggm02 where isnull(ggb14c, '') != '' union all --四工 select isnull(gga01c, ''), isnull(ggb02f, 1), isnull(ggb26c, ''), isnull(ggb25c, '2'), isnull(ggb35f, 0), isnull(ggb39f, 0), '', 1 from #ggm01_ggm02 where isnull(ggb26c, '') != '' update #bills set gender = isnull(gga31i, 0), isold = isnull(gga13f, 0), itemtype = isnull(gda13c, ''), paytype = isnull(ggb27c, ''), num = isnull(ggb05f, 0), amt = isnull(ggb11f, 0), real_ratio = isnull(ggb34f, 0), real_amt = isnull(ggb11f, 0) * isnull(ggb34f, 0), ret_amt = case when @amttype = 1 then isnull(v_amt, 0) --虚业绩 when @amttype = 3 then isnull(r_amt, 0) --实业绩 else isnull(ggb11f, 0) end from #ggm01_ggm02 left join gdm01 with(nolock) on gda00c = gga00c and ggb03c = gda01c where billid = gga01c and detail_id = ggb02f --and billtype = 1 insert #bills(billid, detail_id, staffno, amt, billtype, ret_amt, paytype) --产品第一销售 select billid = isnull(gga01c, ''), detail_id = isnull(ggc02f, 0), staffno = isnull(ggc12c, ''), amt = isnull(ggc11f, 0), billtype = 2, ret_amt = case when @amttype = 1 then isnull(ggc21f, 0) when @amttype = 3 then isnull(ggc21f, 0)-isnull(ggc13f, 0) else isnull(ggc11f, 0) end, paytype = ggc27c from ggm03 b with (nolock), ggm01 a with (nolock) where a.gga00c = b.ggc00c and a.gga01c = b.ggc01c and a.gga80d >= @fromdate and a.gga80d <= @todate and a.gga00c = @compid and isnull(ggc12c, '') != '' and (@fromstaff = '*' or ggc12c between @fromstaff and @tostaff) union all --产品第二销售 select billid = isnull(gga01c, 0), detail_id = isnull(ggc02f, 0), staffno = isnull(ggc15c, ''), amt = isnull(ggc11f, ''), billtype = 2, ret_amt = case when @amttype = 1 then isnull(ggc23f, 0) when @amttype = 3 then isnull(ggc23f, 0)-isnull(ggc13f, 0) else isnull(ggc11f, 0) end, paytype = ggc27c from ggm03 b with (nolock), ggm01 a with (nolock) where a.gga00c = b.ggc00c and a.gga01c = b.ggc01c and a.gga80d >= @fromdate and a.gga80d <= @todate and a.gga00c = @compid and isnull(ggc15c, '') != '' and (@fromstaff = '*' or ggc15c between @fromstaff and @tostaff) union all --产品第三销售 select billid = isnull(gga01c, ''), detail_id = isnull(ggc02f, ''), staffno = isnull(ggc17c, ''), amt = isnull(ggc11f, 0), billtype = 2, ret_amt = case when @amttype = 1 then isnull(ggc25f, 0) when @amttype = 3 then isnull(ggc25f, 0)-isnull(ggc13f, 0) else isnull(ggc11f, 0) end, paytype = ggc27c from ggm03 b with (nolock), ggm01 a with (nolock) where a.gga00c = b.ggc00c and a.gga01c = b.ggc01c and a.gga80d >= @fromdate and a.gga80d <= @todate and a.gga00c = @compid and isnull(ggc17c, '') != '' and (@fromstaff = '*' or ggc17c between @fromstaff and @tostaff) -- 每个员工, 每条单据一条记录 insert #allbill(staffno, billid, gender, isold, type, iscard, num) --modify lm 2015-08-21 select distinct staffno, billid, gender, isold, stafftype, 0 ,sum(num) from #bills where billtype = 1 group by staffno, billid, gender, isold, stafftype order by staffno, billid --统计卡付单据 if @sp055f = 1--单据支付 begin update a set iscard = 1 from gsm03 b with(nolock), #allbill a where a.billid = b.gsc01c -- and (b.gsc04c in('#', '4', '2', '9') or b.gsc04c between 'A' and 'ZZ') -- and b.gsc04c not in('WX','ZFB') and b.gsc04c in(select pay from pay_classify where category = '2' and comp = @compid) and b.gsc00c = @compid update a set iscard = 2 from gsm03 b with(nolock), #allbill a where a.billid = b.gsc01c and b.gsc04c in(select pay from pay_classify where category = '1' and comp = @compid) and a.iscard != 1 and b.gsc00c = @compid end else if @sp055f = 0--项目支付 begin update a set iscard = 1 from #bills b, #allbill a where a.staffno = b.staffno and a.billid = b.billid and b.paytype in(select pay from pay_classify where category = '2' and comp = @compid) -- and (b.paytype in('#', '4', '2', '9') or b.paytype between 'A' and 'ZZ') -- and b.paytype not in('WX','ZFB') and b.billtype = 1 update a set iscard = 2 from #bills b, #allbill a where a.staffno = b.staffno and a.billid = b.billid and b.paytype in(select pay from pay_classify where category = '1' and comp = @compid) and b.billtype = 1 and a.iscard != 1 end --总计 insert #results(staffno, type, cnt, amt) select staffno, type, sum(cnt), sum(amt) from ( --总项目数 select staffno, '0-0' as type, sum(num) as cnt, 0 as amt from #allbill group by staffno --modify lm 2015-07-16 union all --总业绩 select staffno, '0-0' as type, 0 as cnt, sum(ret_amt) as amt from #bills where billtype = 1 group by staffno union all --指定客数 select staffno, '0-1' as type, sum(num) as cnt, 0 as amt from #allbill where type = '1' group by staffno --modify by lm 2015-07-16 union all --指定客业绩 select staffno, '0-1' as type, 0 as cnt, sum(ret_amt) as amt from #bills where stafftype = '1' and billtype = 1 group by staffno union all --指定客现金业绩 select staffno, '0-1-1' as type, 0 as cnt, sum(ret_amt) as amt from #bills where stafftype = '1' and billtype = 1 and paytype in(select pay from pay_classify where category ='1' and comp=@compid) group by staffno union all --指定客卡付业绩 select staffno, '0-1-2' as type, 0 as cnt, sum(ret_amt) as amt from #bills where stafftype = '1' and billtype = 1 and paytype in(select pay from pay_classify where category ='2' and comp=@compid) group by staffno union all --不指定客数 select staffno, '0-2' as type, sum(num) as cnt, 0 as amt from #allbill where type = '2' group by staffno --modify by lm 2015-07-16 union all --不指定客业绩 select staffno, '0-2' as type, 0 as cnt, sum(ret_amt) as amt from #bills where stafftype = '2' and billtype = 1 group by staffno union all --指定客现金业绩 select staffno, '0-2-1' as type, 0 as cnt, sum(ret_amt) as amt from #bills where stafftype = '2' and billtype = 1 and paytype in(select pay from pay_classify where category ='1' and comp=@compid) group by staffno union all --指定卡付业绩 select staffno, '0-2-2' as type, 0 as cnt, sum(ret_amt) as amt from #bills where stafftype = '2' and billtype = 1 and paytype in(select pay from pay_classify where category ='2' and comp=@compid) group by staffno union all --男客数 select staffno, 'A-1', sum(num) as cnt, 0 as amt from #allbill where gender = 1 group by staffno --modify by lm 2015-07-16 union all --男客业绩 select staffno, 'A-1', 0 as cnt, sum(ret_amt) as amt from #bills where gender = 1 and billtype = 1 group by staffno union all --女客数 select staffno, 'A-2', sum(num) as cnt, 0 as amt from #allbill where gender = 0 group by staffno --modify by lm 2015-07-16 union all --女客业绩 select staffno, 'A-2', 0 as cnt, sum(ret_amt) as amt from #bills where gender = 0 and billtype = 1 group by staffno union all --新客数 select staffno, 'N-1', sum(num) as cnt, 0 as amt from #allbill where isold = 0 group by staffno --modify by lm 2015-07-16 union all --新客业绩 select staffno, 'N-1', 0 as cnt, sum(ret_amt) as amt from #bills where isold = 0 and billtype = 1 group by staffno union all --老客数 select staffno, 'N-2', sum(num) as cnt, 0 as amt from #allbill where isold = 1 group by staffno --modify by lm 2015-07-16 union all --老客业绩 select staffno, 'N-2', 0 as cnt, sum(ret_amt) as amt from #bills where isold = 1 and billtype = 1 group by staffno union all --总指定客数 select distinct staffno, '0-0-1', count(billid) as cnt, 0 as amt from #allbill where type = '1' group by staffno --add by lm 2015-07-27 union all --总非指定客数 select distinct staffno, '0-0-2', count(billid) as cnt, 0 as amt from #allbill where type = '2' group by staffno --add by lm 2015-07-27 union all --总客数 select a.staffno, a.type, count(a.staffno) as cnt, a.amt from (select staffno, '0-0-0' as type ,count(staffno) as cnt, 0 as amt from #allbill group by staffno,billid) a --add by lm 2015-08-26 group by a.staffno, a.type ,a.amt ) as tmp group by staffno, type order by staffno, type if @sp055f = 1 -- 单据支付 begin insert #results(staffno, type, cnt, amt) select staffno, 'C-1', sum(cnt), sum(amt) from ( select staffno, sum(num) as cnt, 0 as amt from #allbill where iscard = 1 group by staffno --modify by lm 2015-08-21 union all select staffno, 0 as cnt, sum(gsc05f) as amt from gsm03 with(nolock), #allbill where gsc01c = billid and gsc02c = 'gx' and gsc04c in(select pay from pay_classify where category = '2' and comp = @compid) and gsc00c = @compid group by staffno ) as tmp group by staffno union all select staffno, 'C-0', sum(cnt), sum(amt) from ( select staffno, sum(num) as cnt, 0 as amt from #allbill where iscard = 2 group by staffno --modify by lm 2015-07-16 union all select staffno, 0 as cnt, sum(gsc05f) as amt from gsm03 with(nolock), #allbill where gsc01c = billid and gsc02c = 'gx' and gsc04c in (select pay from pay_classify where category = '1' and comp = @compid) and gsc00c = @compid group by staffno ) as tmp group by staffno union all select staffno, 'C-2', sum(cnt), sum(amt) from ( select staffno, sum(num) as cnt, 0 as amt from #allbill where iscard = 0 group by staffno --modify by lm 2015-07-16 union all select staffno, 0 as cnt, sum(gsc05f) as amt from gsm03 with(nolock), #allbill where gsc01c = billid and gsc02c = 'gx' and (gsc04c in ('&', '3', '7', '0', '8')or gsc04c like '$%') and gsc00c = @compid group by staffno ) as tmp group by staffno end else if @sp055f = 0 -- 项目支付 begin insert #results(staffno, type, cnt, amt) select staffno, 'C-1', sum(cnt), sum(amt) from ( select staffno, sum(num) as cnt, 0 as amt from #allbill where iscard = 1 group by staffno --modify by lm 2015-07-16 union all select staffno, 0 as cnt, sum(ret_amt) as amt from #bills where billtype = 1 and paytype in(select pay from pay_classify where category = '2' and comp = @compid) group by staffno ) as tmp group by staffno union all select staffno, 'C-0', sum(cnt), sum(amt) from ( select staffno, sum(num) as cnt, 0 as amt from #allbill where iscard = 2 group by staffno --modify by lm 2015-07-16 union all select staffno, 0 as cnt, sum(ret_amt) as amt from #bills where paytype in (select pay from pay_classify where category = '1' and comp = @compid) and billtype = 1 group by staffno ) as tmp group by staffno union all select staffno, 'C-2', sum(cnt), sum(amt) from ( select staffno, sum(num) as cnt, 0 as amt from #allbill where iscard = 0 group by staffno --modify by lm 2015-07-16 union all select staffno, 0 as cnt, sum(ret_amt) as amt from #bills where (paytype in ('&', '3', '7', '0', '8')or paytype like '$%') and billtype = 1 group by staffno ) as tmp group by staffno end --项目分类总计 insert #results(staffno, type, itemtype, cnt, amt, tamt, cnt_f, amt_f, cnt_t, amt_t, cash_amt, card_amt, else_amt, cash_cnt, card_cnt, else_cnt) select staffno, 'ITEMCLasS', itemtype, sum(cnt), sum(amt), sum(tamt), sum(cnt_f), sum(amt_f), sum(cnt_t), sum(amt_t), sum(cash_amt), sum(card_amt), sum(else_amt), sum(cash_cnt), sum(card_cnt), sum(else_cnt) from ( select staffno, itemtype, sum(num) as cnt, sum(ret_amt) as amt, sum(real_amt) as tamt, --modify by lm 2015-07-16 0 as cnt_f, 0 as amt_f, 0 as cnt_t, 0 as amt_t, 0 as cash_amt, 0 as card_amt, 0 as else_amt, 0 as cash_cnt, 0 as card_cnt, 0 as else_cnt from #bills group by staffno, itemtype union all select staffno, itemtype, 0 as cnt, 0 as amt, 0 as tamt, sum(num) as cnt_f, sum(ret_amt) as amt_f, 0 as cnt_t, 0 as amt_t, --modify by lm 2015-07-16 0 as cash_amt, 0 as card_amt, 0 as else_amt, 0 as cash_cnt, 0 as card_cnt, 0 as else_cnt from #bills where stafftype = '1' group by staffno, itemtype union all select staffno, itemtype, 0 as cnt, 0 as amt, 0 as tamt, 0 as cnt_f, 0 as amt_f, sum(num) as cnt_t, sum(ret_amt) as amt_t, --modify by lm 2015-07-16 0 as cash_amt, 0 as card_amt, 0 as else_amt, 0 as cash_cnt, 0 as card_cnt, 0 as else_cnt from #bills where stafftype = '2' group by staffno, itemtype union all select staffno, itemtype, 0 as cnt, 0 as amt, 0 as tamt, 0 as cnt_f, 0 as amt_f, 0 as cnt_t, 0 as amt_t, sum(ret_amt) as cash_amt, 0 as card_amt, 0 as else_amt, sum(num) as cash_cnt, 0 as card_cnt, 0 as else_cnt --modify by lm 2015-07-16 from #bills where paytype in (select pay from pay_classify where category = '1' and comp = @compid) group by staffno, itemtype union all select staffno, itemtype, 0 as cnt, 0 as amt, 0 as tamt, 0 as cnt_f, 0 as amt_f, 0 as cnt_t, 0 as amt_t, 0 as cash_amt, sum(ret_amt) as card_amt, 0 as else_amt, 0 as cash_cnt, sum(num) as card_cnt, 0 as else_cnt --modify by lm 2015-07-16 from #bills where( paytype in('#', '4', '2', '9') or paytype between 'A' and 'ZZ' ) and paytype not in ('WX','ZFB') group by staffno, itemtype union all select staffno, itemtype, 0 as cnt, 0 as amt, 0 as tamt, 0 as cnt_f, 0 as amt_f, 0 as cnt_t, 0 as amt_t, 0 as cash_amt, 0 as card_amt, sum(ret_amt) as else_amt, 0 as cash_cnt, 0 as card_cnt, sum(num) as else_cnt --modify by lm 2015-07-16 from #bills where (paytype in ('&', '3', '7', '0', '8')or paytype like '$%') group by staffno, itemtype ) as tmp group by staffno, itemtype order by staffno, itemtype --start 更新购买疗程 lucz 2015.10.21 declare @staffno varchar(20) declare @gda13c varchar(60) declare @num float declare @gcm05f float Create table #tb_liaocheng ( staffno varchar(30) not null, gda13c varchar(30) null, --统计分类(统计) num float null, --次数 jine float null --疗程金额 ) create clustered index idx_tb_liaocheng on #tb_liaocheng(staffno, gda13c) insert into #tb_liaocheng(staffno,gda13c,num,jine) select staffno, gda13c, --统计分类(统计) sum(num) num, --次数 sum(jine) jine --疗程金额 from ( select ggb00c, ggb01c, ggb12c as staffno, --大工 gda13c = (select gda13c from gdm01 where gda00c = ggb00c and gda01c = ggb03c ), --统计分类(统计) ggb05f as num, --数量 case when @amttype = 1 then ggb20f when @amttype = 3 then ggb36f else ggb11f end as jine, --虚业绩 gga94d = (select top 1 gga94d from ggm01 where gga00c = ggb00c and gga01c = ggb01c) from ggm02 where ggb00c = @compid and isnull(ggb47i,-1) <> -1 and isnull(ggb12c,'')<> '' union all select ggb00c, ggb01c, ggb13c as staffno, --中工 gda13c = (select gda13c from gdm01 where gda00c = ggb00c and gda01c = ggb03c ), --统计分类(统计) ggb05f as num, --数量 case when @amttype = 1 then ggb21f when @amttype = 3 then ggb37f else ggb11f end as jine, --虚业绩 gga94d = (select top 1 gga94d from ggm01 where gga00c = ggb00c and gga01c = ggb01c) from ggm02 where ggb00c = @compid and isnull(ggb47i,-1) <> -1 and isnull(ggb13c,'')<> '' union all select ggb00c, ggb01c, ggb14c as staffno, --小工 gda13c = (select gda13c from gdm01 where gda00c = ggb00c and gda01c = ggb03c ), --统计分类(统计) ggb05f as num, --数量 case when @amttype = 1 then ggb22f when @amttype = 3 then ggb38f else ggb11f end as jine, --虚业绩 gga94d = (select top 1 gga94d from ggm01 where gga00c = ggb00c and gga01c = ggb01c) from ggm02 where ggb00c = @compid and isnull(ggb47i,-1) <> -1 and isnull(ggb14c,'')<> '' union all select ggb00c, ggb01c, ggb26c as staffno, --四工 gda13c = (select gda13c from gdm01 where gda00c = ggb00c and gda01c = ggb03c ), --统计分类(统计) ggb05f as num, --数量 case when @amttype = 1 then ggb35f when @amttype = 3 then ggb39f else ggb11f end as jine, --虚业绩 gga94d = (select top 1 gga94d from ggm01 where gga00c = ggb00c and gga01c = ggb01c) from ggm02 where ggb00c = @compid and isnull(ggb47i,-1) <> -1 and isnull(ggb26c,'')<> '' ) aTable where gga94d between @fromdate and @todate and (@fromstaff = '*' or staffno between @fromstaff and @tostaff) group by staffno,gda13c --更新 Update a set a.liaoc_cs = b.num, a.liaoc_cnt = b.num, a.liaoc_t = b.jine from #results a, #tb_liaocheng b where a.staffno = b.staffno and a.type = 'ITEMCLasS' and a. itemtype = b.gda13c --end lucz 2015.10.21 Create table #liaoc_payment ( personid varchar(60), -- 员工代码 [type] varchar(30), -- 统计分类 paycode varchar(20), -- 支付代码 amt float -- 金额或业绩 ) create clustered index idx_liaoc_payment on #liaoc_payment(personid, type) insert into #liaoc_payment select personid, [type], case paycode when '1' then '现金' when '6' then '银行卡' when '4' then '储值' when 'ZFB' then '银行卡' when 'WX' then '银行卡' else '其他' end paycode, case @amttype when 0 then sum(payamt) when 2 then sum(payamt) when 3 then sum(perf) when 4 then sum(perf) else sum(perf) end amt from ( select personid, code, [type] = (select gda13c from gdm01 where gda00c = compid and gda01c = code ), --统计分类(统计) paycode, --支付代码 sum(perf) perf, --业绩 sum(payamt) payamt--金额 from card_fill_detail_by_payment where compid = @compid and billid in ( select gnc01c from gnm03 a with (nolock), gsm03 b with (nolock) where a.gnc00c = b.gsc00c and a.gnc01c = b.gsc01c and b.gsc02c = 'gn' and a.gnc00c = @compid and (b.gsc10d between @fromdate and @todate) ) and filltype = 2 and billtype = 'gn' and (@fromstaff = '*' or personid between @fromstaff and @tostaff) group by compid,personid,code,paycode union all select personid, code, [type] = (select gda13c from gdm01 where gda00c = compid and gda01c = code ), --统计分类(统计) paycode, --支付代码 sum(perf) perf, --业绩 sum(payamt) payamt--金额 from card_fill_detail_by_payment where compid = @compid and billid in ( select gcl01c from gcm10 a with (nolock), gsm03 b with (nolock) where a.gcl00c = b.gsc00c and a.gcl01c = b.gsc01c and b.gsc02c = 'gz' and a.gcl00c = @compid and (a.gcl02d between @fromdate and @todate) ) and filltype = 2 and billtype = 'gz' and (@fromstaff = '*' or personid between @fromstaff and @tostaff) group by compid,personid,code,paycode ) as table_payment group by personid,paycode,[type] --更新#results insert into #results(staffno,[type],itemtype) select personid,'ITEMLCS',[type] from #liaoc_payment group by personid,type Update a set a.liaoc_jine = b.amt from #results a, (select personid,[type],sum(amt) amt from #liaoc_payment where paycode = '现金' group by personid,[type]) b where a.staffno = b.personid and a.type = 'ITEMLCS' and a. itemtype = b.[type] Update a set a.liaoc_chuz = b.amt from #results a, (select personid,[type],sum(amt) amt from #liaoc_payment where paycode = '储值' group by personid,[type]) b where a.staffno = b.personid and a.type = 'ITEMLCS' and a. itemtype = b.[type] Update a set a.liaoc_hank = b.amt from #results a, (select personid,[type],sum(amt) amt from #liaoc_payment where paycode = '银行卡' group by personid,[type]) b where a.staffno = b.personid and a.type = 'ITEMLCS' and a. itemtype = b.[type] Update a set a.liaoc_qita = b.amt from #results a, (select personid,[type],sum(amt) amt from #liaoc_payment where paycode = '其他' group by personid,[type]) b where a.staffno = b.personid and a.type = 'ITEMLCS' and a. itemtype = b.[type] -- 疗程销售 总计 Update a set a.liaoc_sum = b.amt from #results a, (select personid,[type],sum(amt) amt from #liaoc_payment group by personid,[type]) b where a.staffno = b.personid and a.type = 'ITEMLCS' and a. itemtype = b.[type] ------------------------- insert #results(staffno, type, itemtype, cnt, amt, price, cnt_f, amt_f, cnt_t, amt_t, cash_amt, cash_cnt, card_amt, card_cnt, else_amt, else_cnt, tamt, liaoc_cs,liaoc_cnt,liaoc_t,liaoc_sum,liaoc_jine,liaoc_chuz,liaoc_hank,liaoc_qita) select staffno, 'ITEMGROUP', gsr02c as itemgroup, sum(cnt), sum(amt), sum(price), sum(cnt_f), sum(amt_f), sum(cnt_t), sum(amt_t), sum(cash_amt), sum(cash_cnt), sum(card_amt), sum(card_cnt), sum(else_amt), sum(else_cnt), sum(tamt), sum(liaoc_cs),sum(liaoc_cnt),sum(liaoc_t),sum(liaoc_sum),sum(liaoc_jine),sum(liaoc_chuz),sum(liaoc_hank),sum(liaoc_qita) from #results, gsm29 with(nolock) where gsr00c = @compid and gsr01c = itemtype and isnull(gsr02c, '') != '' and gsr03i = 1 group by staffno, gsr02c --产品销售 insert #results(staffno, type, cnt, amt) select staffno, '99-2', sum(1), sum(ret_amt) from #bills where billtype = 2 group by staffno --产品销售现金 insert #results(staffno, type, cnt, amt) select staffno, '99-2-1', sum(1), sum(ret_amt) from #bills where billtype = 2 and paytype in(select pay from pay_classify where category = '1' and comp = @compid) group by staffno --产品销售卡付 insert #results(staffno, type, cnt, amt) select staffno, '99-2-2', sum(1), sum(ret_amt) from #bills where billtype = 2 and (paytype in('#', '4', '2', '9') or paytype between 'A' and 'ZZ') and paytype not in ('WX','ZFB') group by staffno --产品销售其他支付 insert #results(staffno, type, cnt, amt) select staffno, '99-2-3', sum(1), sum(ret_amt) from #bills where billtype = 2 and (paytype in ('&', '3', '7', '0', '8')or paytype like '$%') group by staffno declare @printtotalcom varchar(1) --打印总提成 declare @printitemcom varchar(1) --打印项目提成 declare @printgoodscom varchar(1) --打印产品提成 declare @printcardcom varchar(1) --打印卡提成 set @printtotalcom = substring(@printcom, 1, 1) set @printitemcom = substring(@printcom, 2, 1) set @printgoodscom = substring(@printcom, 3, 1) set @printcardcom = substring(@printcom, 4, 1) --提成 if @printtotalcom = '1' or @printitemcom = '1' or @printgoodscom = '1' or @printcardcom = '1' begin create table #com ( staffno varchar(20) not null, com float null ) declare @spid int select @spid = @@spid + 1000 insert #com(staffno, com) exec upg_personal_statistics @compid, @fromdate, @todate, @fromstaff, @tostaff, 1, 1, 1, 2, @spid insert #work_detail_new(person_id,action_id,srvdate,code ,name,payway, amt,quan,cost,comm,amt2,amt3,cardrate,stdprice,prj_type,share_rate, cls_flag,gender,billid,paycode,ex_rate,ggm02_seq,room_id,compid, spid,cardid,memid ,comp_ascription ) select person_id,action_id,srvdate,code ,name,payway, amt,quan,cost,comm,amt2,amt3,cardrate,stdprice,prj_type,share_rate, cls_flag,gender,billid,paycode,ex_rate,ggm02_seq,room_id,compid, spid,cardid,memid ,comp_ascription from work_detail with (nolock) where spid = @spid delete #com if (@printtotalcom = '1') begin insert #com(staffno, com) select person_id, sum(comm) from #work_detail_new group by person_id -- insert #results(staffno, type, cnt, amt) -- select staffno, 'T-1' as type, 0 as cnt, com as amt -- from #com insert #results(staffno, type, cnt, amt) select staffno,'T-1',0,sum(amt) from ( select staffno, isnull(com,0) as amt from #com union all select person_id as staffno,sum(isnull(comm_prj,0) + isnull(comm_good,0)+isnull(comm_card,0)) amt from workdetail_by_empl where spid = @spid group by person_id ) as a group by staffno end if (@printitemcom = '1') --项目提成 begin delete #com insert #com(staffno, com) select person_id, sum(comm) from #work_detail_new where (action_id >= 2 and action_id <= 8) or (action_id >= 10 and action_id <= 11) group by person_id -- insert #results(staffno, type, cnt, amt) -- select staffno, 'T-2' as type, 0 as cnt, com as amt -- from #com insert #results(staffno, type, cnt, amt) select staffno,'T-2',0,sum(amt) from ( select staffno,isnull(com,0) as amt from #com union all select person_id as staffno,isnull(comm_prj,0) as amt from workdetail_by_empl where spid = @spid ) as a group by staffno end if (@printgoodscom = '1') --产品提成 begin delete #com insert #com(staffno, com) select person_id, sum(comm) from #work_detail_new where action_id = 9 group by person_id -- insert #results(staffno, type, cnt, amt) -- select staffno, 'T-3' as type, 0 as cnt, com as amt -- from #com insert #results(staffno, type, cnt, amt) select staffno,'T-3',0,sum(amt) from ( select staffno,isnull(com,0) as amt from #com union all select person_id as staffno,isnull(comm_good,0) as amt from workdetail_by_empl where spid = @spid ) as a group by staffno end if (@printcardcom = '1') --卡提成 begin delete #com insert #com(staffno, com) select person_id, sum(comm) from #work_detail_new where (action_id = 0 or action_id = 1) group by person_id -- insert #results(staffno, type, cnt, amt) -- select staffno, 'T-4' as type, 0 as cnt, com as amt -- from #com insert #results(staffno, type, cnt, amt) select staffno,'T-4',0,sum(amt) from ( select staffno,isnull(com,0) as amt from #com union all select person_id as staffno,isnull(comm_card,0) from workdetail_by_empl where spid = @spid ) as a group by staffno end drop table #com end create table #cardbill2 ( idd int identity not null, billid varchar(20) not null, -- 单号 billtype int not null, -- 1-买卡, 2-充值 staffno varchar(20) not null, -- 员工编号 cardno varchar(20) null, -- 卡号 ret_amt float default 0 null, -- 报表需要返回的业绩 rate float default 0 null, -- 员工分享比率 card_amt float default 0 null, cash_amt float default 0 null, pay_amt float default 0 null, -- 支付金额 pay_way varchar(5) default '' null, -- 支付方式 add by limin 2014/07/21 增加 支付方式 amt_sum float default 0 null -- 金额 PRIMARY KEY NonCLUSTERED(idd) ) create clustered index idx_cardbill2 on #cardbill2(billid, billtype) create table #cardbill ( idd int identity not null, billid varchar(20) not null, -- 单号 billtype int not null, -- 1-买卡, 2-充值 staffno varchar(20) not null, -- 员工编号 cardno varchar(20) not null, -- 卡号 ret_amt float default 0 not null, -- 报表需要返回的业绩 rate float default 0 null, -- 员工分享比率 card_amt float default 0 null, cash_amt float default 0 null PRIMARY KEY NonCLUSTERED(idd) ) create clustered index idx_cardbill on #cardbill(billid, billtype) create table #cardbill_amt_sum --add by lm 2015.10.29 ( idd int identity not null, billid varchar(20) not null, -- 单号 billtype int not null, --1项目, 2产品 amt float default 0 null, -- 金额 ret_amt float default 0 null -- 业绩 PRIMARY KEY NonCLUSTERED(idd) ) create clustered index idx_cardbill_amt_sum on #cardbill_amt_sum(billid, billtype) create table #cardbill_num ( staffno varchar(20) not null, -- 员工编号 num float default 0 null ) create clustered index idx_cardbill_num on #cardbill_num(staffno) create table #gnm01_gnm02 ( gna01c varchar(20) not null, gnb03c varchar(20) not null, gnb08f float null, gna20c varchar(20) null, gna28c varchar(20) null, gna30c varchar(20) null, gna32c varchar(20) null, gna34c varchar(20) null, gna36c varchar(20) null, gna38c varchar(20) null, gna40c varchar(20) null, gna42c varchar(20) null, gna44c varchar(20) null, gna67f float null, gna69f float null, gna71f float null, gna86f float null, gna87f float null, gna88f float null, gna89f float null, gna90f float null, gna91f float null, gna92f float null, gna27f float null,--第一分享比率 gna29f float null,--第二分享比率 gna31f float null,--第三分享比率 gna33f float null,--第四分享比率 gna35f float null,--第五分享比率 gna37f float null,--第六分享比率 gna39f float null,--第七分享比率 gna41f float null,--第八分享比率 gna43f float null,--第九分享比率 gna45f float null,--第十分享比率 pay_amt varchar(20) null, pay_way varchar(20) null ) create clustered index idx_gnm01_gnm02 on #gnm01_gnm02(gna01c) create table #gcm10 ( gcl01c varchar(20) not null, gcl03c varchar(20) not null, gcl07f float null, gcl09c varchar(20) null, gcl17c varchar(20) null, gcl19c varchar(20) null, gcl22c varchar(20) null, gcl24c varchar(20) null, gcl26c varchar(20) null, gcl28c varchar(20) null, gcl30c varchar(20) null, gcl32c varchar(20) null, gcl34c varchar(20) null, gcl58f float null, gcl60f float null, gcl62f float null, gcl83f float null, gcl84f float null, gcl85f float null, gcl86f float null, gcl87f float null, gcl88f float null, gcl89f float null, gcl16f float null, --第一分享比率 gcl18f float null, --第二分享比率 gcl20f float null, --第三分享比率 gcl23f float null, --第四分享比率 gcl25f float null, --第五分享比率 gcl27f float null, --第六分享比率 gcl29f float null, --第七分享比率 gcl31f float null, --第八分享比率 gcl33f float null, --第九分享比率 gcl35f float null, --第十分享比率 pay_amt varchar(20) null, pay_way varchar(20) null, gcl06i int null ) create clustered index idx_gcm10 on #gcm10(gcl01c) create table #gcm11 --add by lm 2015.12.03 ( gcm00c varchar(10) not null, --门店编号 gcm01c varchar(20) not null, --异动单号 gcm03f float null, --次数 gcm04f float null, --赠送次数 gcm05f float null --疗程金额 ) create clustered index idx_gcm11 on #gcm11(gcm00c, gcm01c) insert #gnm01_gnm02(gna01c, gnb03c, gnb08f, gna20c, gna28c, gna30c, gna32c, gna34c, gna36c, gna38c, gna40c, gna42c, gna44c, gna67f, gna69f, gna71f, gna86f, gna87f, gna88f, gna89f, gna90f, gna91f, gna92f, gna27f, gna29f, gna31f, gna33f, gna35f, gna37f, gna39f, gna41f, gna43f, gna45f) select gna01c, gnb03c, gnb08f, gna20c, gna28c, gna30c, gna32c, gna34c, gna36c, gna38c, gna40c, gna42c, gna44c, gna67f, gna69f, gna71f, gna86f, gna87f, gna88f, gna89f, gna90f, gna91f, gna92f, gna27f, gna29f ,gna31f, gna33f, gna35f, gna37f, gna39f, gna41f, gna43f, gna45f from gnm01 with (nolock), gnm02 with (nolock), gsm03 with (nolock), gam26 z with(nolock) where gnb00c = gna00c and gnb01c = gna01c and gsc00c = gna00c and gsc01c = gna01c and gsc02c = 'gn' and gna00c = gaz02c and gaz01c = @compid and gna80d >= @fromdate and gna80d <= @todate and gsc04c <> '5' and (@fromstaff = '*' or isnull(gna20c, '') between @fromstaff and @tostaff or isnull(gna28c, '') between @fromstaff and @tostaff or isnull(gna30c, '') between @fromstaff and @tostaff or isnull(gna32c, '') between @fromstaff and @tostaff or isnull(gna34c, '') between @fromstaff and @tostaff or isnull(gna36c, '') between @fromstaff and @tostaff or isnull(gna40c, '') between @fromstaff and @tostaff or isnull(gna42c, '') between @fromstaff and @tostaff or isnull(gna44c, '') between @fromstaff and @tostaff) insert #gcm10(gcl01c, gcl03c, gcl07f, gcl09c, gcl17c, gcl19c, gcl22c, gcl24c, gcl26c, gcl28c, gcl30c, gcl32c, gcl34c, gcl58f, gcl60f, gcl62f, gcl83f, gcl84f, gcl85f, gcl86f, gcl87f, gcl88f, gcl89f, gcl06i, gcl16f, gcl18f, gcl20f, gcl23f, gcl25f, gcl27f, gcl29f, gcl31f, gcl33f, gcl35f) select gcl01c, gcl03c, gcl07f, gcl09c, gcl17c, gcl19c, gcl22c, gcl24c, gcl26c, gcl28c, gcl30c, gcl32c, gcl34c, gcl58f, gcl60f, gcl62f, gcl83f, gcl84f, gcl85f, gcl86f, gcl87f, gcl88f, gcl89f, gcl06i, gcl16f, gcl18f, gcl20f, gcl23f, gcl25f, gcl27f, gcl29f, gcl31f, gcl33f, gcl35f from gcm10 with (nolock), gam26 z with(nolock) where gcl00c = gaz02c and gaz01c = @compid and gcl80d >= @fromdate and gcl80d <= @todate and gcl06i in (0, 6) --and gcl04c not in('888', '889') and (@fromstaff = '*' or isnull(gcl09c, '') between @fromstaff and @tostaff or isnull(gcl17c, '') between @fromstaff and @tostaff or isnull(gcl19c, '') between @fromstaff and @tostaff or isnull(gcl22c, '') between @fromstaff and @tostaff or isnull(gcl24c, '') between @fromstaff and @tostaff or isnull(gcl26c, '') between @fromstaff and @tostaff or isnull(gcl28c, '') between @fromstaff and @tostaff or isnull(gcl30c, '') between @fromstaff and @tostaff or isnull(gcl34c, '') between @fromstaff and @tostaff) --获取疗程次数和赠送次数 add by lm 2015.12.03 insert #gcm11(gcm00c, gcm01c, gcm03f, gcm04f, gcm05f) select gcm00c, gcm01c, sum(gcm03f), sum(gcm04f), sum(gcm05f) from gcm11 with(nolock) inner join #gcm10 with(nolock) on gcl01c = gcm01c and gcm00c = @compid group by gcm00c, gcm01c --declare @amttype int --set @amttype = 1 insert #cardbill2(billid, billtype, cardno, staffno, ret_amt) --卖卡第一人 select billid = gna01c, billtype = 1, cardno = gnb03c, staffno = gna20c, ret_amt = isnull(gna67f, 0) from #gnm01_gnm02 where isnull(gna20c, '') != '' union all --卖卡第二人 select billid = gna01c, billtype = 1, cardno = gnb03c, staffno = gna28c, ret_amt = isnull(gna69f, 0) from #gnm01_gnm02 where isnull(gna28c, '') != '' union all --卖卡第三人 select billid = gna01c, billtype = 1, cardno = gnb03c, staffno = gna30c, ret_amt = isnull(gna71f, 0) from #gnm01_gnm02 where isnull(gna30c, '') != '' union all --卖卡第四人 select billid = gna01c, billtype = 1, cardno = gnb03c, staffno = gna32c, ret_amt = isnull(gna86f, 0) from #gnm01_gnm02 where isnull(gna32c, '') != '' union all --卖卡第五人 select billid = gna01c, billtype = 1, cardno = gnb03c, staffno = gna34c, ret_amt = isnull(gna87f, 0) from #gnm01_gnm02 where isnull(gna34c, '') != '' union all --卖卡第六人 select billid = gna01c, billtype = 1, cardno = gnb03c, staffno = gna36c, ret_amt = isnull(gna88f, 0) from #gnm01_gnm02 where isnull(gna36c, '') != '' union all --卖卡第七人 select billid = gna01c, billtype = 1, cardno = gnb03c, staffno = gna38c, ret_amt = isnull(gna89f, 0) from #gnm01_gnm02 where isnull(gna38c, '') != '' union all --卖卡第八人 select billid = gna01c, billtype = 1, cardno = gnb03c, staffno = gna40c, ret_amt = isnull(gna90f, 0) from #gnm01_gnm02 where isnull(gna40c, '') != '' union all --卖卡第九人 select billid = gna01c, billtype = 1, cardno = gnb03c, staffno = gna42c, ret_amt = isnull(gna91f, 0) from #gnm01_gnm02 where isnull(gna42c, '') != '' union all --卖卡第十人 select billid = gna01c, billtype = 1, cardno = gnb03c, staffno = gna44c, ret_amt = isnull(gna92f, 0) from #gnm01_gnm02 where isnull(gna44c, '') != '' --统计充值,还款 modify by lm 2015.10.19 union all --充值第一人 select billid = gcl01c, billtype = 2, cardno = gcl03c, staffno = gcl09c, ret_amt = isnull(gcl58f, 0) from #gcm10 where isnull(gcl09c, '') != '' and (gcl06i = 0 or gcl06i = 6) union all --充值第二人 select billid = gcl01c, billtype = 2, cardno = gcl03c, staffno = gcl17c, ret_amt = isnull(gcl60f, 0) from #gcm10 with (nolock) where isnull(gcl17c, '') != '' and (gcl06i = 0 or gcl06i = 6) union all --充值第三人 select billid = gcl01c, billtype = 2, cardno = gcl03c, staffno = gcl19c, ret_amt = isnull(gcl62f, 0) from #gcm10 with (nolock) where isnull(gcl19c, '') != '' and (gcl06i = 0 or gcl06i = 6) union all --充值第四人 select billid = gcl01c, billtype = 2, cardno = gcl03c, staffno = gcl22c, ret_amt = isnull(gcl83f, 0) from #gcm10 with (nolock) where isnull(gcl22c, '') != '' and (gcl06i = 0 or gcl06i = 6) union all --充值第五人 select billid = gcl01c, billtype = 2, cardno = gcl03c, staffno = gcl24c, ret_amt = isnull(gcl84f, 0) from #gcm10 with (nolock) where isnull(gcl24c, '') != '' and (gcl06i = 0 or gcl06i = 6) union all --充值第六人 select billid = gcl01c, billtype = 2, cardno = gcl03c, staffno = gcl26c, ret_amt = isnull(gcl85f, 0) from #gcm10 with (nolock) where isnull(gcl26c, '') != '' and (gcl06i = 0 or gcl06i = 6) union all --充值第七人 select billid = gcl01c, billtype = 2, cardno = gcl03c, staffno = gcl28c, ret_amt = isnull(gcl86f, 0) from #gcm10 with (nolock) where isnull(gcl28c, '') != '' and (gcl06i = 0 or gcl06i = 6) union all --充值第八人 select billid = gcl01c, billtype = 2, cardno = gcl03c, staffno = gcl30c, ret_amt = isnull(gcl87f, 0) from #gcm10 with (nolock) where isnull(gcl30c, '') != '' and (gcl06i = 0 or gcl06i = 6) union all --充值第九人 select billid = gcl01c, billtype = 2, cardno = gcl03c, staffno = gcl32c, ret_amt = isnull(gcl88f, 0) from #gcm10 with (nolock) where isnull(gcl32c, '') != '' and (gcl06i = 0 or gcl06i = 6) union all --充值第十人 select billid = gcl01c, billtype = 2, cardno = gcl03c, staffno = gcl34c, ret_amt = isnull(gcl89f, 0) from #gcm10 with (nolock) where isnull(gcl34c, '') != '' and (gcl06i = 0 or gcl06i = 6) --统计取款 --modify lm 2015.10.19 union all --充值第一人 select billid = gcl01c, billtype = 2, cardno = gcl03c, staffno = gcl09c, ret_amt = isnull( @SP048 * gcl58f, 0) from #gcm10 where isnull(gcl09c, '') != '' and gcl06i = 1 union all --充值第二人 select billid = gcl01c, billtype = 2, cardno = gcl03c, staffno = gcl17c, rret_amt = isnull( @SP048 * gcl60f, 0) from #gcm10 with (nolock) where isnull(gcl17c, '') != '' and gcl06i = 1 union all --充值第三人 select billid = gcl01c, billtype = 2, cardno = gcl03c, staffno = gcl19c, ret_amt = isnull( @SP048 * gcl62f, 0) from #gcm10 with (nolock) where isnull(gcl19c, '') != '' and gcl06i = 1 union all --充值第四人 select billid = gcl01c, billtype = 2, cardno = gcl03c, staffno = gcl22c, ret_amt = isnull( @SP048 * gcl83f, 0) from #gcm10 with (nolock) where isnull(gcl22c, '') != '' and gcl06i = 1 union all --充值第五人 select billid = gcl01c, billtype = 2, cardno = gcl03c, staffno = gcl24c, ret_amt = isnull( @SP048 * gcl84f, 0) from #gcm10 with (nolock) where isnull(gcl24c, '') != '' and gcl06i = 1 union all --充值第六人 select billid = gcl01c, billtype = 2, cardno = gcl03c, staffno = gcl26c, ret_amt = isnull( @SP048 * gcl85f, 0) from #gcm10 with (nolock) where isnull(gcl26c, '') != '' and gcl06i = 1 union all --充值第七人 select billid = gcl01c, billtype = 2, cardno = gcl03c, staffno = gcl28c, ret_amt = isnull( @SP048 * gcl86f, 0) from #gcm10 with (nolock) where isnull(gcl28c, '') != '' and gcl06i = 1 union all --充值第八人 select billid = gcl01c, billtype = 2, cardno = gcl03c, staffno = gcl30c, ret_amt = isnull( @SP048 * gcl87f, 0) from #gcm10 with (nolock) where isnull(gcl30c, '') != '' and gcl06i = 1 union all --充值第九人 select billid = gcl01c, billtype = 2, cardno = gcl03c, staffno = gcl32c, ret_amt = isnull( @SP048 * gcl88f, 0) from #gcm10 with (nolock) where isnull(gcl32c, '') != '' and gcl06i = 1 union all --充值第十人 select billid = gcl01c, billtype = 2, cardno = gcl03c, staffno = gcl34c, ret_amt = isnull( @SP048 * gcl89f, 0) from #gcm10 with (nolock) where isnull(gcl34c, '') != '' and gcl06i = 1 if(@amttype = 1 or @amttype = 3) -- 0-营业额 1-虚业绩 2-数量 3-实业绩 begin insert #cardbill(billid, billtype, staffno, cardno, ret_amt, cash_amt, card_amt,rate) select billid, billtype, staffno, cardno, ret_amt, sum(cash_amt), sum(card_amt),rate from #cardbill2 group by billid, billtype, staffno, cardno, ret_amt,rate end else begin insert #cardbill(billid, billtype, staffno, cardno, ret_amt, cash_amt, card_amt,rate) select billid, billtype, staffno, cardno, sum(ret_amt), sum(cash_amt), sum(card_amt),rate from #cardbill2 group by billid, billtype, staffno, cardno, ret_amt,rate end --End if @amttype in (0, 2) begin --充值数量 insert #results(staffno, type, cnt, amt, cash_amt, card_amt) select distinct staffno, '99-0-2' as type, sum(1) as cnt, sum(0) as amt, sum(0) as cash_amt, sum(0) as card_amt from #cardbill where billtype = 2 group by staffno --买卡数量 insert #results(staffno, type, cnt, amt, cash_amt, card_amt) select distinct staffno, '99-0-1' as type, sum(1) as cnt, 0 as amt, 0 as cash_amt, 0 as card_amt from #cardbill where billtype = 1 group by staffno insert #results(staffno, type, cnt, amt, cash_amt, card_amt) select person_id, '99-0-1' as type, sum(1) as cnt, sum(amt) as amt, sum(amt) as cash_amt, 0 as card_amt from #work_detail_new where action_id = 1 -- and paycode in(select pay from pay_classify where category = '1' and comp = @compid) group by person_id insert #results(staffno, type, cnt, amt, cash_amt, card_amt) select person_id, '99-0-1' as type, sum(1) as cnt, sum(amt) as amt, 0 as cash_amt,sum(amt) as card_amt from #work_detail_new where action_id = 1 and paycode in(select pay from pay_classify where category = '2' and comp = @compid) group by person_id insert #results(staffno, type, cnt, amt, cash_amt, card_amt) select person_id, '99-0-2' as type, sum(1) as cnt, sum(amt) as amt, sum(amt) as cash_amt, 0 as card_amt from #work_detail_new where action_id = 0 --and paycode in(select pay from pay_classify where category = '1' and comp = @compid) group by person_id insert #results(staffno, type, cnt, amt, cash_amt, card_amt) select person_id, '99-0-2' as type, sum(1) as cnt, sum(amt) as amt, 0 as cash_amt, sum(amt) as card_amt from #work_detail_new where action_id = 0 and paycode in(select pay from pay_classify where category = '2' and comp = @compid) group by person_id insert #results(staffno, type, cnt, amt, cash_amt, card_amt) select distinct staffno, '99-0', sum(1), sum(amt), sum(cash_amt) as cash_amt, sum(card_amt) as card_amt from #results where type in ('99-0-1', '99-0-2') group by staffno --start 疗程销售 lucz 2015.10.21 insert #results(staffno, type, liaoc_cs, liaoc_cnt,liaoc_t) select staffno, '99-0-3' as type,sum(num),sum(num),sum(gnc05f) from ( select gna20c as staffno, gnc03f + gnc04f as num, -- 次数 gnc05f --金额 from gnm03 inner join gnm01 on gna00c = gnc00c and gna01c = gnc01c where gnc00c = @compid and isnull(gna20c,'') <> '' and gna02d between @fromdate and @todate and (@fromstaff = '*' or gna20c between @fromstaff and @tostaff) union all select gna28c as staffno, --第二卖卡人 gnc03f + gnc04f as num, -- 次数 gnc05f --金额 from gnm03 inner join gnm01 on gna00c = gnc00c and gna01c = gnc01c where gnc00c = @compid and isnull(gna28c,'') <> '' and gna02d between @fromdate and @todate and (@fromstaff = '*' or gna28c between @fromstaff and @tostaff) union all select gna30c as staffno, --第三卖卡人 gnc03f + gnc04f as num, -- 次数 gnc05f --金额 from gnm03 inner join gnm01 on gna00c = gnc00c and gna01c = gnc01c where gnc00c = @compid and isnull(gna30c,'') <> '' and gna02d between @fromdate and @todate and (@fromstaff = '*' or gna30c between @fromstaff and @tostaff) union all select gna32c as staffno, --第四卖卡人 gnc03f + gnc04f as num, -- 次数 gnc05f --金额 from gnm03 inner join gnm01 on gna00c = gnc00c and gna01c = gnc01c where gnc00c = @compid and isnull(gna32c,'') <> '' and gna02d between @fromdate and @todate and (@fromstaff = '*' or gna32c between @fromstaff and @tostaff) union all select gna34c as staffno, --第5卖卡人 gnc03f + gnc04f as num, -- 次数 gnc05f --金额 from gnm03 inner join gnm01 on gna00c = gnc00c and gna01c = gnc01c where gnc00c = @compid and isnull(gna34c,'') <> '' and gna02d between @fromdate and @todate and (@fromstaff = '*' or gna34c between @fromstaff and @tostaff) union all select gna36c as staffno, --第6卖卡人 gnc03f + gnc04f as num, -- 次数 gnc05f --金额 from gnm03 inner join gnm01 on gna00c = gnc00c and gna01c = gnc01c where gnc00c = @compid and isnull(gna36c,'') <> '' and gna02d between @fromdate and @todate and (@fromstaff = '*' or gna36c between @fromstaff and @tostaff) union all select gna38c as staffno, --第7卖卡人 gnc03f + gnc04f as num, -- 次数 gnc05f --金额 from gnm03 inner join gnm01 on gna00c = gnc00c and gna01c = gnc01c where gnc00c = @compid and isnull(gna38c,'') <> '' and gna02d between @fromdate and @todate and (@fromstaff = '*' or gna38c between @fromstaff and @tostaff) union all select gna40c as staffno, --第8卖卡人 gnc03f + gnc04f as num, -- 次数 gnc05f --金额 from gnm03 inner join gnm01 on gna00c = gnc00c and gna01c = gnc01c where gnc00c = @compid and isnull(gna40c,'') <> '' and gna02d between @fromdate and @todate and (@fromstaff = '*' or gna40c between @fromstaff and @tostaff) union all select gna42c as staffno, --第9卖卡人 gnc03f + gnc04f as num, -- 次数 gnc05f --金额 from gnm03 inner join gnm01 on gna00c = gnc00c and gna01c = gnc01c where gnc00c = @compid and isnull(gna42c,'') <> '' and gna02d between @fromdate and @todate and (@fromstaff = '*' or gna42c between @fromstaff and @tostaff) union all select gna44c as staffno, --第10卖卡人 gnc03f + gnc04f as num, -- 次数 gnc05f --金额 from gnm03 inner join gnm01 on gna00c = gnc00c and gna01c = gnc01c where gnc00c = @compid and isnull(gna44c,'') <> '' and gna02d between @fromdate and @todate and (@fromstaff = '*' or gna44c between @fromstaff and @tostaff) ) as xs_table group by staffno --疗程充值 insert #results(staffno, type, liaoc_cs, liaoc_cnt,liaoc_t) select staffno, '99-0-4' as type,sum(num),sum(num),sum(gcm05f) from ( select gcl09c as staffno, gcm03f + gcm04f as num, gcm05f --金额 from gcm11 inner join gcm10 with(nolock) on gcl01c = gcm01c and gcl00c = gcm00c where gcm00c = @compid and isnull(gcl09c,'') <> '' and gcl06i = 0 and gcl04c not in('888', '889') and gcl80d between @fromdate and @todate and (@fromstaff = '*' or gcl09c between @fromstaff and @tostaff) union all select gcl17c as staffno, --第2销售 gcm03f + gcm04f as num, gcm05f --金额 from gcm11 inner join gcm10 on gcl01c = gcm01c and gcl00c = gcm00c where gcm00c = @compid and isnull(gcl17c,'') <> '' and gcl06i = 0 and gcl04c not in('888', '889') and gcl80d between @fromdate and @todate and (@fromstaff = '*' or gcl17c between @fromstaff and @tostaff) union all select gcl19c as staffno, --第3销售 gcm03f + gcm04f as num, gcm05f --金额 from gcm11 inner join gcm10 on gcl01c = gcm01c and gcl00c = gcm00c where gcm00c = @compid and isnull(gcl19c,'') <> '' and gcl06i = 0 and gcl04c not in('888', '889') and gcl80d between @fromdate and @todate and (@fromstaff = '*' or gcl19c between @fromstaff and @tostaff) union all select gcl22c as staffno, --第4销售 gcm03f + gcm04f as num, gcm05f --金额 from gcm11 inner join gcm10 on gcl01c = gcm01c and gcl00c = gcm00c where gcm00c = @compid and isnull(gcl22c,'') <> '' and gcl06i = 0 and gcl04c not in('888', '889') and gcl80d between @fromdate and @todate and (@fromstaff = '*' or gcl22c between @fromstaff and @tostaff) union all select gcl24c as staffno, --第5销售 gcm03f + gcm04f as num, gcm05f --金额 from gcm11 inner join gcm10 on gcl01c = gcm01c and gcl00c = gcm00c where gcm00c = @compid and isnull(gcl24c,'') <> '' and gcl06i = 0 and gcl04c not in('888', '889') and gcl80d between @fromdate and @todate and (@fromstaff = '*' or gcl24c between @fromstaff and @tostaff) union all select gcl26c as staffno, --第6销售 gcm03f + gcm04f as num, gcm05f --金额 from gcm11 inner join gcm10 on gcl01c = gcm01c and gcl00c = gcm00c where gcm00c = @compid and isnull(gcl26c,'') <> '' and gcl06i = 0 and gcl04c not in('888', '889') and gcl80d between @fromdate and @todate and (@fromstaff = '*' or gcl26c between @fromstaff and @tostaff) union all select gcl28c as staffno, --第7销售 gcm03f + gcm04f as num, gcm05f --金额 from gcm11 inner join gcm10 on gcl01c = gcm01c and gcl00c = gcm00c where gcm00c = @compid and isnull(gcl28c,'') <> '' and gcl06i = 0 and gcl04c not in('888', '889') and gcl80d between @fromdate and @todate and (@fromstaff = '*' or gcl28c between @fromstaff and @tostaff) union all select gcl30c as staffno, --第8销售 gcm03f + gcm04f as num, gcm05f --金额 from gcm11 inner join gcm10 on gcl01c = gcm01c and gcl00c = gcm00c where gcm00c = @compid and isnull(gcl30c,'') <> '' and gcl06i = 0 and gcl04c not in('888', '889') and gcl80d between @fromdate and @todate and (@fromstaff = '*' or gcl30c between @fromstaff and @tostaff) union all select gcl32c as staffno, --第9销售 gcm03f + gcm04f as num, gcm05f --金额 from gcm11 inner join gcm10 on gcl01c = gcm01c and gcl00c = gcm00c where gcm00c = @compid and isnull(gcl32c,'') <> '' and gcl06i = 0 and gcl04c not in('888', '889') and gcl80d between @fromdate and @todate and (@fromstaff = '*' or gcl32c between @fromstaff and @tostaff) union all select gcl34c as staffno, --第10销售 gcm03f + gcm04f as num, gcm05f --金额 from gcm11 inner join gcm10 on gcl01c = gcm01c and gcl00c = gcm00c where gcm00c = @compid and isnull(gcl34c,'') <> '' and gcl06i = 0 and gcl04c not in('888', '889') and gcl80d between @fromdate and @todate and (@fromstaff = '*' or gcl34c between @fromstaff and @tostaff) ) as cz_table group by staffno --疗程卡销售和卡充值汇总 insert #results(staffno, type, cnt, amt) select staffno, '99-0-5', sum(liaoc_cs), sum(liaoc_t) from #results where type in ('99-0-3', '99-0-4') group by staffno --end lucz 2015.10.21 end else if @amttype in (1, 3) --实业绩,虚业绩 begin insert #results(staffno, type, cnt, amt, cash_amt, card_amt) select staffno, type as type, sum(cnt) as cnt, sum(amt) as amt,sum(cash_amt) as cash_amt, sum(card_amt) as card_amt from ( --充值数量 select distinct staffno, '99-0-2' as type, sum(1) as cnt, sum(0) as amt, sum(0) as cash_amt, sum(0) as card_amt from #cardbill where billtype = 2 group by staffno union all --买卡数量 select distinct staffno, '99-0-1' as type, sum(1) as cnt, 0 as amt, 0 as cash_amt, 0 as card_amt from #cardbill where billtype = 1 group by staffno union all select person_id, '99-0-1' as type, 0 as cnt, sum(amt2) as amt, sum(amt2) as cash_amt, 0 as card_amt from #work_detail_new where action_id = 1 --and paycode in(select pay from pay_classify where category = '1' and comp = @compid) group by person_id union all select person_id, '99-0-1' as type, 0 as cnt, sum(amt2) as amt, 0 as cash_amt,sum(amt2) as card_amt from #work_detail_new where action_id = 1 and paycode in(select pay from pay_classify where category = '2' and comp = @compid) group by person_id union all select person_id, '99-0-2' as type, 0 as cnt, sum(amt2) as amt, sum(amt2) as cash_amt, 0 as card_amt from #work_detail_new where action_id = 0 --and paycode in(select pay from pay_classify where category = '1' and comp = @compid) group by person_id union all select person_id, '99-0-2' as type, 0 as cnt, sum(amt2) as amt, 0 as cash_amt, sum(amt2) as card_amt from #work_detail_new where action_id = 0 and paycode in(select pay from pay_classify where category = '2' and comp = @compid) group by person_id union all select person_id, '99-0-2' as type, 0 as cnt, sum(amt2) as amt, 0 as cash_amt, sum(amt2) as card_amt from #work_detail_new where action_id = 0 and paycode in(select pay from pay_classify where category = '4' and comp = @compid) group by person_id -- union all -- --买卡业绩 -- select distinct staffno, '99-0-1' as type, 0 as cnt, sum(ret_amt) as amt, sum(cash_amt) as cash_amt, sum(card_amt) as card_amt -- from #cardbill, -- ( -- select billid as billid_tmp, min(idd) as idd_tmp from #cardbill where billtype = 1 group by billid, staffno -- )as tmp -- where billid = billid_tmp -- and idd = idd_tmp -- and billtype = 1 -- group by staffno ) as tmp group by staffno, type insert #results(staffno, type, cnt, amt, cash_amt, card_amt) select staffno, '99-0', sum(cnt), sum(amt), sum(cash_amt) as cash_amt, sum(card_amt) as card_amt from #results where type in ('99-0-1', '99-0-2') group by staffno --start 疗程销售 lucz 2015.10.21 insert #results(staffno, type, liaoc_cs, liaoc_cnt,liaoc_t) select staffno, '99-0-3' as type,sum(num),sum(num),sum(jine) from ( select gna20c as staffno, gnc03f + gnc04f as num, -- 次数 gnc05f*gna27f as jine --疗程金额 * 分享比率 from gnm03 inner join gnm01 on gna00c = gnc00c and gna01c = gnc01c where gnc00c = @compid and isnull(gna20c,'') <> '' and gna02d between @fromdate and @todate and (@fromstaff = '*' or gna20c between @fromstaff and @tostaff) union all select gna28c as staffno, --第二卖卡人 gnc03f + gnc04f as num, -- 次数 gnc05f*gna29f as jine --疗程金额 * 分享比率 from gnm03 inner join gnm01 on gna00c = gnc00c and gna01c = gnc01c where gnc00c = @compid and isnull(gna28c,'') <> '' and gna02d between @fromdate and @todate and (@fromstaff = '*' or gna28c between @fromstaff and @tostaff) union all select gna30c as staffno, --第三卖卡人 gnc03f + gnc04f as num, -- 次数 gnc05f*gna31f as jine --疗程金额 * 分享比率 from gnm03 inner join gnm01 on gna00c = gnc00c and gna01c = gnc01c where gnc00c = @compid and isnull(gna30c,'') <> '' and gna02d between @fromdate and @todate and (@fromstaff = '*' or gna30c between @fromstaff and @tostaff) union all select gna32c as staffno, --第四卖卡人 gnc03f + gnc04f as num, -- 次数 gnc05f*gna33f as jine --疗程金额 * 分享比率 from gnm03 inner join gnm01 on gna00c = gnc00c and gna01c = gnc01c where gnc00c = @compid and isnull(gna32c,'') <> '' and gna02d between @fromdate and @todate and (@fromstaff = '*' or gna32c between @fromstaff and @tostaff) union all select gna34c as staffno, --第5卖卡人 gnc03f + gnc04f as num, -- 次数 gnc05f*gna35f as jine --疗程金额 * 分享比率 from gnm03 inner join gnm01 on gna00c = gnc00c and gna01c = gnc01c where gnc00c = @compid and isnull(gna34c,'') <> '' and gna02d between @fromdate and @todate and (@fromstaff = '*' or gna34c between @fromstaff and @tostaff) union all select gna36c as staffno, --第6卖卡人 gnc03f + gnc04f as num, -- 次数 gnc05f*gna37f as jine --疗程金额 * 分享比率 from gnm03 inner join gnm01 on gna00c = gnc00c and gna01c = gnc01c where gnc00c = @compid and isnull(gna36c,'') <> '' and gna02d between @fromdate and @todate and (@fromstaff = '*' or gna36c between @fromstaff and @tostaff) union all select gna38c as staffno, --第7卖卡人 gnc03f + gnc04f as num, -- 次数 gnc05f*gna39f as jine --疗程金额 * 分享比率 from gnm03 inner join gnm01 on gna00c = gnc00c and gna01c = gnc01c where gnc00c = @compid and isnull(gna38c,'') <> '' and gna02d between @fromdate and @todate and (@fromstaff = '*' or gna38c between @fromstaff and @tostaff) union all select gna40c as staffno, --第8卖卡人 gnc03f + gnc04f as num, -- 次数 gnc05f*gna41f as jine --疗程金额 * 分享比率 from gnm03 inner join gnm01 on gna00c = gnc00c and gna01c = gnc01c where gnc00c = @compid and isnull(gna40c,'') <> '' and gna02d between @fromdate and @todate and (@fromstaff = '*' or gna40c between @fromstaff and @tostaff) union all select gna42c as staffno, --第9卖卡人 gnc03f + gnc04f as num, -- 次数 gnc05f*gna43f as jine --疗程金额 * 分享比率 from gnm03 inner join gnm01 on gna00c = gnc00c and gna01c = gnc01c where gnc00c = @compid and isnull(gna42c,'') <> '' and gna02d between @fromdate and @todate and (@fromstaff = '*' or gna42c between @fromstaff and @tostaff) union all select gna44c as staffno, --第10卖卡人 gnc03f + gnc04f as num, -- 次数 gnc05f*gna45f as jine --疗程金额 * 分享比率 from gnm03 inner join gnm01 on gna00c = gnc00c and gna01c = gnc01c where gnc00c = @compid and isnull(gna44c,'') <> '' and gna02d between @fromdate and @todate and (@fromstaff = '*' or gna44c between @fromstaff and @tostaff) ) as xs_table group by staffno --疗程充值 insert #results(staffno, type, liaoc_cs, liaoc_cnt,liaoc_t) select staffno, '99-0-4' as type,sum(num),sum(num),sum(jine) from ( select gcl09c as staffno, num = (select top 1 gcm03f + gcm04f from gcm11 where gcm00c=gcl00c and gcm01c = gcl01c ), gcl66f * gcl16f as jine --总疗程金额 * 分享比率 from gcm10 where gcl00c = @compid and gcl06i = 0 and isnull(gcl09c,'') <> '' and gcl02d between @fromdate and @todate and (@fromstaff = '*' or gcl09c between @fromstaff and @tostaff) union all select gcl17c as staffno, --第2销售 num = (select top 1 gcm03f + gcm04f from gcm11 where gcm00c=gcl00c and gcm01c = gcl01c ), gcl66f * gcl18f as jine --总疗程金额 * 分享比率 from gcm10 where gcl00c = @compid and gcl06i = 0 and isnull(gcl17c,'') <> '' and gcl02d between @fromdate and @todate and (@fromstaff = '*' or gcl17c between @fromstaff and @tostaff) union all select gcl19c as staffno, --第3销售 num = (select top 1 gcm03f + gcm04f from gcm11 where gcm00c=gcl00c and gcm01c = gcl01c ), gcl66f * gcl20f as jine --总疗程金额 * 分享比率 from gcm10 where gcl00c = @compid and gcl06i = 0 and isnull(gcl19c,'') <> '' and gcl02d between @fromdate and @todate and (@fromstaff = '*' or gcl19c between @fromstaff and @tostaff) union all select gcl22c as staffno, --第4销售 num = (select top 1 gcm03f + gcm04f from gcm11 where gcm00c=gcl00c and gcm01c = gcl01c ), gcl66f * gcl23f as jine --总疗程金额 * 分享比率 from gcm10 where gcl00c = @compid and gcl06i = 0 and isnull(gcl22c,'') <> '' and gcl02d between @fromdate and @todate and (@fromstaff = '*' or gcl22c between @fromstaff and @tostaff) union all select gcl24c as staffno, --第5销售 num = (select top 1 gcm03f + gcm04f from gcm11 where gcm00c=gcl00c and gcm01c = gcl01c ), gcl66f * gcl25f as jine --总疗程金额 * 分享比率 from gcm10 where gcl00c = @compid and gcl06i = 0 and isnull(gcl24c,'') <> '' and gcl02d between @fromdate and @todate and (@fromstaff = '*' or gcl24c between @fromstaff and @tostaff) union all select gcl26c as staffno, --第6销售 num = (select top 1 gcm03f + gcm04f from gcm11 where gcm00c=gcl00c and gcm01c = gcl01c ), gcl66f * gcl27f as jine --总疗程金额 * 分享比率 from gcm10 where gcl00c = @compid and gcl06i = 0 and isnull(gcl26c,'') <> '' and gcl02d between @fromdate and @todate and (@fromstaff = '*' or gcl26c between @fromstaff and @tostaff) union all select gcl28c as staffno, --第7销售 num = (select top 1 gcm03f + gcm04f from gcm11 where gcm00c=gcl00c and gcm01c = gcl01c ), gcl66f * gcl29f as jine --总疗程金额 * 分享比率 from gcm10 where gcl00c = @compid and gcl06i = 0 and isnull(gcl28c,'') <> '' and gcl02d between @fromdate and @todate and (@fromstaff = '*' or gcl28c between @fromstaff and @tostaff) union all select gcl30c as staffno, --第8销售 num = (select top 1 gcm03f + gcm04f from gcm11 where gcm00c=gcl00c and gcm01c = gcl01c ), gcl66f * gcl31f as jine --总疗程金额 * 分享比率 from gcm10 where gcl00c = @compid and gcl06i = 0 and isnull(gcl30c,'') <> '' and gcl02d between @fromdate and @todate and (@fromstaff = '*' or gcl30c between @fromstaff and @tostaff) union all select gcl32c as staffno, --第9销售 num = (select top 1 gcm03f + gcm04f from gcm11 where gcm00c=gcl00c and gcm01c = gcl01c ), gcl66f * gcl33f as jine --总疗程金额 * 分享比率 from gcm10 where gcl00c = @compid and gcl06i = 0 and isnull(gcl32c,'') <> '' and gcl02d between @fromdate and @todate and (@fromstaff = '*' or gcl32c between @fromstaff and @tostaff) union all select gcl34c as staffno, --第10销售 num = (select top 1 gcm03f + gcm04f from gcm11 where gcm00c=gcl00c and gcm01c = gcl01c ), gcl66f * gcl35f as jine --总疗程金额 * 分享比率 from gcm10 where gcl00c = @compid and gcl06i = 0 and isnull(gcl34c,'') <> '' and gcl02d between @fromdate and @todate and (@fromstaff = '*' or gcl34c between @fromstaff and @tostaff) -- 充值 还款 union all select gcl09c as staffno, 0 as num, gcl66f * gcl16f as jine --总疗程金额 * 分享比率 from gcm10 where gcl00c = @compid and gcl06i = 6 and gcl04c = '4' and isnull(gcl09c,'') <> '' and gcl02d between @fromdate and @todate and (@fromstaff = '*' or gcl09c between @fromstaff and @tostaff) union all select gcl17c as staffno, --第2销售 0 as num, gcl66f * gcl18f as jine --总疗程金额 * 分享比率 from gcm10 where gcl00c = @compid and gcl06i = 6 and gcl04c = '4' and isnull(gcl17c,'') <> '' and gcl02d between @fromdate and @todate and (@fromstaff = '*' or gcl17c between @fromstaff and @tostaff) union all select gcl19c as staffno, --第3销售 0 as num, gcl66f * gcl20f as jine --总疗程金额 * 分享比率 from gcm10 where gcl00c = @compid and gcl06i = 6 and gcl04c = '4' and isnull(gcl19c,'') <> '' and gcl02d between @fromdate and @todate and (@fromstaff = '*' or gcl19c between @fromstaff and @tostaff) union all select gcl22c as staffno, --第4销售 0 as num, gcl66f * gcl23f as jine --总疗程金额 * 分享比率 from gcm10 where gcl00c = @compid and gcl06i = 6 and gcl04c = '4' and isnull(gcl22c,'') <> '' and gcl02d between @fromdate and @todate and (@fromstaff = '*' or gcl22c between @fromstaff and @tostaff) union all select gcl24c as staffno, --第5销售 0 as num, gcl66f * gcl25f as jine --总疗程金额 * 分享比率 from gcm10 where gcl00c = @compid and gcl06i = 6 and gcl04c = '4' and isnull(gcl24c,'') <> '' and gcl02d between @fromdate and @todate and (@fromstaff = '*' or gcl24c between @fromstaff and @tostaff) union all select gcl26c as staffno, --第6销售 0 as num, gcl66f * gcl27f as jine --总疗程金额 * 分享比率 from gcm10 where gcl00c = @compid and gcl06i = 6 and gcl04c = '4' and isnull(gcl26c,'') <> '' and gcl02d between @fromdate and @todate and (@fromstaff = '*' or gcl26c between @fromstaff and @tostaff) union all select gcl28c as staffno, --第7销售 0 as num, gcl66f * gcl29f as jine --总疗程金额 * 分享比率 from gcm10 where gcl00c = @compid and gcl06i = 6 and gcl04c = '4' and isnull(gcl28c,'') <> '' and gcl02d between @fromdate and @todate and (@fromstaff = '*' or gcl28c between @fromstaff and @tostaff) union all select gcl30c as staffno, --第8销售 0 as num, gcl66f * gcl31f as jine --总疗程金额 * 分享比率 from gcm10 where gcl00c = @compid and gcl06i = 6 and gcl04c = '4' and isnull(gcl30c,'') <> '' and gcl02d between @fromdate and @todate and (@fromstaff = '*' or gcl30c between @fromstaff and @tostaff) union all select gcl32c as staffno, --第9销售 0 as num, gcl66f * gcl33f as jine --总疗程金额 * 分享比率 from gcm10 where gcl00c = @compid and gcl06i = 6 and gcl04c = '4' and isnull(gcl32c,'') <> '' and gcl02d between @fromdate and @todate and (@fromstaff = '*' or gcl32c between @fromstaff and @tostaff) union all select gcl34c as staffno, --第10销售 0 as num, gcl66f * gcl35f as jine --总疗程金额 * 分享比率 from gcm10 where gcl00c = @compid and gcl06i = 6 and gcl04c = '4' and isnull(gcl34c,'') <> '' and gcl02d between @fromdate and @todate and (@fromstaff = '*' or gcl34c between @fromstaff and @tostaff) ) as cz_table group by staffno --疗程卡销售和卡充值汇总 insert #results(staffno, type, cnt, amt) select staffno, '99-0-5', sum(liaoc_cs), sum(liaoc_t) from #results where type in ('99-0-3', '99-0-4') group by staffno --end lucz 2015.10.21 end --计算数量 delete from #cardbill2 insert #cardbill2 (billid, billtype, staffno) select distinct billid, billtype, staffno from #cardbill insert #cardbill_num (staffno, num) select staffno,sum(1) from #cardbill2 where billtype = 1 group by staffno update a set a.cnt = b.num from #results a, #cardbill_num b where a.staffno = b.staffno and type = '99-0-1' delete from #cardbill_num insert #cardbill_num (staffno, num) select staffno,sum(1) from #cardbill2 where billtype = 2 group by staffno update a set a.cnt = b.num from #results a, #cardbill_num b where a.staffno = b.staffno and type = '99-0-2' delete from #cardbill_num insert #cardbill_num (staffno, num) select staffno,sum(1) from #cardbill2 group by staffno update a set a.cnt = b.num from #results a, #cardbill_num b where a.staffno = b.staffno and type = '99-0' update #results set price = case when isnull(cnt, 0) = 0 then 0 else amt/cnt end --增加劳动业绩 add by lm 2016.01.04 create table #jobamt ( personid varchar(20) not null, job_amt_x float null, job_amt_c float null ) insert #jobamt (personid) select distinct staffno from #results a with(nolock) --项目 0-营业额 1-虚业绩 2-数量 3-实业绩 update a set a.job_amt_x = newTable.amt from #jobamt a , ( select person_id, case @amttype when 1 then sum(isnull(b.amt2,0)) when 3 then sum(isnull(b.amt3,0)) else sum(isnull(b.amt,0) * isnull(c.gso08f,0)) end amt from #work_detail_new b , gsm15 c with(nolock) where b.compid = c.gso00c and spid = @spid and b.paycode = c.gso02c and b.compid = @compid and ((action_id >= 2 and action_id <= 8) or (action_id >= 10 and action_id <= 11)) and isnull(gso09f,0) = 1 group by person_id ) as newTable where a.personid = newTable.person_id --产品 update a set a.job_amt_c = newTable.amt from #jobamt a , ( select person_id,case @amttype when 1 then sum(isnull(b.amt2,0)) when 3 then sum(isnull(b.amt3,0)) else sum(isnull(b.amt,0) * isnull(c.gso08f,0)) end amt from #work_detail_new b , gsm15 c with(nolock) where b.compid = c.gso00c and spid = @spid and b.paycode = c.gso02c and b.compid = @compid and action_id = 9 and isnull(gso10f,0) = 1 group by person_id ) as newTable where a.personid = newTable.person_id insert #results(staffno, type, cnt, amt) select personid, 'J-1' as type, 0 as cnt, (isnull(job_amt_x,0) + isnull(job_amt_c,0)) as amt from #jobamt delete #results where isnull(itemtype, '') = '' and type in ('itemclass', 'itemgroup') --创建临时表,为了存储所有员工 + 门店总计 create table #results1 ( idd int identity not null, staffno varchar(20) null, type varchar(10) null, itemtype varchar(10) null, -- 项目类别 cnt float null, -- 数量 amt float null, -- 业绩 price float null, -- 客单价 cnt_f float null, -- 指定的数量 amt_f float null, -- 指定的金额 cnt_t float null, -- 轮班的数量 amt_t float null, -- 轮班的金额 tamt float null, -- 真实营业额 cash_amt float null, -- 现金总业绩 card_amt float null, -- 划卡总业绩 else_amt float null, -- 其他支付总业绩 cash_cnt float null, -- 现金总数量 card_cnt float null, -- 划卡总数量 else_cnt float null, -- 其他支付总数量 liaoc_cs float null, --疗程购买次数 lucz 2015.10.21 liaoc_cnt float null, --疗程总数量 lucz 2015.10.21 liaoc_t float null, --疗程金额 lucz 2015.10.21 liaoc_sum float null, --疗程销售 总计 liaoc_jine float null, --疗程销售 现金 liaoc_chuz float null, --疗程销售 储值 liaoc_hank float null, --疗程销售 银行卡 liaoc_qita float null, --疗程销售 其他 job_amt float null, --劳动业绩 compid varchar(20) null,--公司别 codetype varchar(20) null,--项目统计分类 cashamt float default 0 null,--营业额 cashamt2 float default 0 null,--虚业绩(营业额折算金额) cashnum float default 0 null,--数量 cardamt float default 0 null,--营业额 cardamt2 float default 0 null,--虚业绩(营业额折算金额) cardnum float default 0 null,--数量 otheramt float default 0 null,--营业额 otheramt2 float default 0 null,--虚业绩(营业额折算金额) othernum float default 0 null,--数量 cashsumamt float default 0 null,--营业额 cardsumamt float default 0 null --营业额 ) create clustered index idx_#results1_1 on #results1(staffno, type, itemtype) --此处compid的空与下面的区分,在报表里面方便分开 insert #results1(compid,staffno,type,itemtype,cnt,amt,price,cnt_f,amt_f,cnt_t,amt_t, tamt,cash_amt,card_amt,else_amt,cash_cnt,card_cnt,else_cnt,liaoc_cs,liaoc_cnt, liaoc_t,liaoc_sum,liaoc_jine,liaoc_chuz,liaoc_hank,liaoc_qita,job_amt) select '' compid,staffno,type,itemtype,cnt,amt,price,cnt_f,amt_f,cnt_t,amt_t, tamt,cash_amt,card_amt,else_amt,cash_cnt,card_cnt,else_cnt,liaoc_cs,liaoc_cnt, liaoc_t,liaoc_sum,liaoc_jine,liaoc_chuz,liaoc_hank,liaoc_qita,job_amt from #results order by staffno, type, itemtype --start计算 门店总业绩 add by lm 2016.01.06 create table #comp_amt ( compid varchar(20) not null,--公司别 billid varchar(20) not null,--单据编号 code varchar(20) null,--项目类别 codetype varchar(20) null,--项目统计分类 paycode varchar(20) null,--支付方式 amt float default 0 null,--营业额 amt2 float default 0 null,--虚业绩(营业额折算金额) num float default 0 null --数量 ) create clustered index idx_comp_amt on #comp_amt(compid, billid) create table #comp_cashamt ( compid varchar(20) not null,--公司别 code varchar(20) null,--项目类别 codetype varchar(20) null,--项目统计分类 paycode varchar(20) null,--支付方式 cashamt float default 0 null,--营业额 cashamt2 float default 0 null,--虚业绩(营业额折算金额) cashnum float default 0 null --数量 ) create clustered index idx_comp_cashamt on #comp_cashamt(compid, paycode, codetype) create table #comp_cardamt ( compid varchar(20) not null,--公司别 code varchar(20) null,--项目类别 codetype varchar(20) null,--项目统计分类 paycode varchar(20) null,--支付方式 cardamt float default 0 null,--营业额 cardamt2 float default 0 null,--虚业绩(营业额折算金额) cardnum float default 0 null --数量 ) create clustered index idx_comp_cardamt on #comp_cardamt(compid, paycode, codetype) create table #comp_otheramt ( compid varchar(20) not null,--公司别 code varchar(20) null,--项目类别 codetype varchar(20) null,--项目统计分类 paycode varchar(20) null,--支付方式 otheramt float default 0 null,--营业额 otheramt2 float default 0 null,--虚业绩(营业额折算金额) othernum float default 0 null,--数量 ) create clustered index idx_comp_otheramt on #comp_otheramt(compid, codetype) create table #comp_sum_amt ( compid varchar(20) not null,--公司别 codetype varchar(20) null,--项目统计分类 cashamt float default 0 null,--营业额 cashamt2 float default 0 null,--虚业绩(营业额折算金额) cashnum float default 0 null,--数量 cardamt float default 0 null,--营业额 cardamt2 float default 0 null,--虚业绩(营业额折算金额) cardnum float default 0 null,--数量 otheramt float default 0 null,--营业额 otheramt2 float default 0 null,--虚业绩(营业额折算金额) othernum float default 0 null,--数量 cashsumamt float default 0 null,--营业额 cardsumamt float default 0 null --营业额 ) create clustered index idx_comp_sum_amt on #comp_sum_amt(compid, codetype) insert #comp_amt(billid, compid, code, paycode, amt, amt2, num) select distinct billid, compid, code, paycode,isnull(a.amt,0), isnull(a.amt,0) * isnull(b.gso03i,0),quan from #work_detail_new a with(nolock), gsm15 b with(nolock) where spid = @spid and b.gso00c = @compid and a.paycode = b.gso02c and a.compid = @compid and a.comp_ascription = @compid and a.action_id not in (0,1,9) update a set a.codetype = b.gda13c from #comp_amt a, gdm01 b with(nolock), gsm02 c with(nolock) where a.compid = b.gda00c and a.code = b.gda01c and b.gda13c = c.gsb02c and b.gda00c = c.gsb00c and c.gsb01c = 'AJ' -- 项目 insert #comp_cashamt(compid, codetype, cashamt, cashamt2, cashnum) select compid, codetype, sum(amt), sum(amt2),sum(num) from #comp_amt where paycode in (select pay from pay_classify where category = '1' and comp = @compid) group by compid, codetype insert #comp_cardamt(compid, codetype, cardamt, cardamt2, cardnum) select compid, codetype, sum(amt), sum(amt2),sum(num) from #comp_amt where paycode in (select pay from pay_classify where category = '2' and comp = @compid) group by compid, codetype insert #comp_otheramt(compid, codetype, otheramt, otheramt2, othernum) select compid, codetype, sum(amt), sum(amt2),sum(num) from #comp_amt where paycode not in(select pay from pay_classify where category = '1' and comp = @compid) and paycode not in(select pay from pay_classify where category = '2' and comp = @compid) group by compid, codetype insert #comp_sum_amt (compid, codetype) select distinct compid, codetype from #comp_amt group by compid, codetype update a set a.cashamt = b.cashamt, a.cashamt2 = b.cashamt2, a.cashnum = b.cashnum from #comp_sum_amt a, #comp_cashamt b where a.codetype = b.codetype and a.compid = b.compid update a set a.cardamt = b.cardamt, a.cardamt2 = b.cardamt2, a.cardnum = b.cardnum from #comp_sum_amt a, #comp_cardamt b where a.codetype = b.codetype and a.compid = b.compid update a set a.otheramt = b.otheramt, a.otheramt2 = b.otheramt2, a.othernum = b.othernum from #comp_sum_amt a, #comp_otheramt b where a.codetype = b.codetype and a.compid = b.compid -- 产品 delete from #comp_amt delete from #comp_cardamt delete from #comp_cashamt delete from #comp_otheramt insert #comp_amt(billid, compid, code, paycode, amt, amt2, num) select distinct billid, compid, code, paycode,isnull(a.amt,0), isnull(a.amt,0) * isnull(b.gso03i,0),quan from #work_detail_new a , gsm15 b with(nolock) where spid = @spid and b.gso00c = @compid and a.paycode = b.gso02c and a.compid = @compid and a.comp_ascription = @compid and a.action_id = 9 insert #comp_sum_amt (compid, codetype, cashamt, cashamt2, cashnum ) select compid, 'goods', sum(amt), sum(amt2),sum(num) from #comp_amt group by compid insert #comp_cashamt(compid, codetype, cashamt, cashamt2, cashnum) select compid, 'goods', sum(amt), sum(amt2),sum(num) from #comp_amt where paycode in (select pay from pay_classify where category = '1' and comp = @compid) group by compid insert #comp_cardamt(compid, codetype, cardamt, cardamt2, cardnum) select compid, 'goods', sum(amt), sum(amt2),sum(num) from #comp_amt where paycode in (select pay from pay_classify where category = '2' and comp = @compid) group by compid insert #comp_otheramt(compid, codetype, otheramt, otheramt2, othernum) select compid, 'goods', sum(amt), sum(amt2),sum(num) from #comp_amt where paycode not in(select pay from pay_classify where category = '1' and comp = @compid) and paycode not in(select pay from pay_classify where category = '2' and comp = @compid) group by compid update a set a.cashamt = b.cashamt, a.cashamt2 = b.cashamt2, a.cashnum = b.cashnum from #comp_sum_amt a, #comp_cashamt b where a.codetype = b.codetype and a.compid = b.compid update a set a.cardamt = b.cardamt, a.cardamt2 = b.cardamt2, a.cardnum = b.cardnum from #comp_sum_amt a, #comp_cardamt b where a.codetype = b.codetype and a.compid = b.compid update a set a.otheramt = b.otheramt, a.otheramt2 = b.otheramt2, a.othernum = b.othernum from #comp_sum_amt a, #comp_otheramt b where a.codetype = b.codetype and a.compid = b.compid --充值 delete from #comp_amt insert #comp_amt(billid, compid, code, paycode, amt, amt2, num) select distinct billid, compid, code, paycode,isnull(a.amt,0), isnull(a.amt,0) * isnull(b.gso03i,0),quan from #work_detail_new a , gsm15 b with(nolock) where spid = @spid and b.gso00c = @compid and a.paycode = b.gso02c and a.compid = @compid and a.comp_ascription = @compid and a.action_id = 0 insert #comp_sum_amt (compid, codetype, cashamt, cashamt2, cashnum ) select compid, 'sellcard', sum(amt), sum(amt2),sum(num) from #comp_amt group by compid --卖卡 delete from #comp_amt insert #comp_amt(billid, compid, code, paycode, amt, amt2, num) select distinct billid, compid, code, paycode,isnull(a.amt,0), isnull(a.amt,0) * isnull(b.gso03i,0),quan from #work_detail_new a, gsm15 b with(nolock) where spid = @spid and b.gso00c = @compid and a.paycode = b.gso02c and a.compid = @compid and a.comp_ascription = @compid and a.action_id = 1 insert #comp_sum_amt (compid, codetype, cardamt, cardamt2, cardnum ) select compid, 'recharge', sum(amt), sum(amt2),sum(num) from #comp_amt group by compid --总现金,总卡付 delete from #comp_amt insert #comp_amt(billid, compid, code, paycode, amt, amt2, num) select distinct billid, compid, code, paycode,isnull(a.amt,0), isnull(a.amt,0) * isnull(b.gso03i,0),quan from #work_detail_new a , gsm15 b with(nolock) where spid = @spid and b.gso00c = @compid and a.paycode = b.gso02c and a.compid = @compid and a.comp_ascription = @compid insert #comp_sum_amt (compid, codetype, cardamt, cardamt2, cardnum ) select compid, 'sumcashamt', sum(amt), sum(amt2),sum(num) from #comp_amt where paycode in (select pay from pay_classify where category = '1' and comp = @compid) group by compid insert #comp_sum_amt (compid, codetype, cardamt, cardamt2, cardnum ) select compid, 'sumcardamt', sum(amt), sum(amt2),sum(num) from #comp_amt where paycode in (select pay from pay_classify where category = '2' and comp = @compid) group by compid if @amttype in (0, 2) begin insert #results1(compid,codetype,cashamt,cashnum,cardamt,cardnum,otheramt,othernum) select compid,isnull(codetype,'') codetype, isnull(cashamt,'') cashamt, isnull(cashnum,'') cashnum, isnull(cardamt,'') cardamt, isnull(cardnum,'') cardnum, isnull(otheramt,'') otheramt, isnull(othernum,'') othernum from #comp_sum_amt end else begin insert #results1(compid,codetype,cashamt,cashnum,cardamt,cardnum,otheramt,othernum) select compid,isnull(codetype,'') codetype, isnull(cashamt2,'') cashamt, isnull(cashnum,'') cashnum, isnull(cardamt2,'') cardamt, isnull(cardnum,'') cardnum, isnull(otheramt2,'') otheramt, isnull(othernum,'') othernum from #comp_sum_amt end --end --0.整数;1.一位;2.二位;3.叁位;4.四位 declare @03f_price int declare @03f_cnt int select @03f_cnt = isnull(gsa03f,0) from gsm01 where gsa00c = @compid and gsa01c = 'SP005' select @03f_price = isnull(gsa03f,0) from gsm01 where gsa00c = @compid and gsa01c = 'SP011' select isnull(staffno,'') staffno, isnull(type,'') type, isnull(itemtype,'') itemtype, Round(isnull(cnt,0),@03f_cnt) cnt, isnull(amt,0) amt, Round(isnull(price,0),@03f_price) price, isnull(cnt_f,'') cnt_f, isnull(amt_f,'') amt_f, isnull(cnt_t,'') cnt_t, isnull(amt_t,'') amt_t, isnull(cash_amt,'') cash_amt, isnull(cash_cnt,'') cash_cnt, isnull(card_amt,'') card_amt, isnull(card_cnt,'') card_cnt, isnull(else_amt,'') else_amt, isnull(else_cnt,'') else_cnt, isnull(tamt,'') tamt, isnull(liaoc_cs,'') liaoc_cs, isnull(liaoc_cnt,'') liaoc_cnt, isnull(liaoc_t,'') liaoc_t, isnull(liaoc_sum,'') liaoc_sum, isnull(liaoc_jine,'') liaoc_jine, isnull(liaoc_chuz,'') liaoc_chuz, isnull(liaoc_hank,'') liaoc_hank, isnull(liaoc_qita,'') liaoc_qita, isnull(job_amt,'') job_amt, isnull(compid,'') compid, isnull(codetype,'') codetype, isnull(cashamt,'') cashamt, isnull(cashnum,'') cashnum, isnull(cardamt,'') cardamt, isnull(cardnum,'') cardnum, isnull(otheramt,'') otheramt, isnull(othernum,'') othernum from #results1 order by idd drop table #cardbill_num drop table #comp_sum_amt drop table #comp_amt drop table #jobamt drop table #work_detail_new drop table #comp_cashamt drop table #comp_cardamt drop table #comp_otheramt drop table #ggm01_ggm02 drop table #bills drop table #allbill drop table #results drop table #results1 drop table #gnm01_gnm02 drop table #gcm10 drop table #gcm11 drop table #cardbill drop table #bills_2 drop table #bills_3 drop table #tb_liaocheng drop table #liaoc_payment end GO /****** Object: StoredProcedure [dbo].[upg_undo_send_gift_prjs] Script Date: 06/12/2017 06:20:48 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_undo_send_gift_prjs] ( @compid varchar(10), @billid varchar(20) ) as begin declare @send_srv_when_buy_goods varchar(20) select @send_srv_when_buy_goods = isnull(gsa02c, '') from gsm01 where gsa00c = @compid and gsa01c = 'SP112' if @send_srv_when_buy_goods <> '1' return -- 找出已经赠送的疗程 create table #tbl_sent_proc ( cardid varchar(20) not null, itemid varchar(20) not null, procseq int not null, senttimes float null, usedtimes float null ) create clustered index idx_sent_proc_01 on #tbl_sent_proc(cardid, itemid, procseq) -- 注意gcm08是否已经被删除了 insert #tbl_sent_proc(cardid, itemid, procseq, senttimes, usedtimes) select gch03c, gch04c, isnull(gch16i, 0), gch06f, 0 from gcm08 b with (nolock) where b.gch10c = 'gx' and b.gch11c = @billid and b.gch00c = @compid and b.gch05f = 0 -- 不是购买 and isnull(b.gch06f, 0) > 0 -- 有赠送 declare @cardid varchar(20) select @cardid = cardid from #tbl_sent_proc if substring(@cardid, 1, 2) = '散客' begin drop table #tbl_sent_proc return end -- 根据卡号获取归属公司: declare @ownercompid nvarchar(10) -- 卡的归属公司 select @ownercompid = min(gca13d) from gcm01 with (nolock) where gca01c = @cardid and gca00c = gca13d -- 统计赠送项目可能的消费次数 update a set a.usedtimes = b.gcf06f from #tbl_sent_proc a, gcm06 b where a.cardid = b.gcf01c and a.itemid = b.gcf02c and a.procseq = b.gcf23i and b.gcf00c = @ownercompid -- 消费历史是不可能删除的,赠送的历史如果没有消费,赠送历史需要删除,如果有消费,则赠送历史不能删除 delete a from gcm06 a, #tbl_sent_proc b where a.gcf00c = @ownercompid and a.gcf01c = b.cardid and a.gcf02c = b.itemid and a.gcf23i = b.procseq and isnull(a.gcf06f, 0) = 0 -- 没有使用过的疗程可以删除 and a.gcf19i = 3 and isnull(usedtimes, 0) = 0 -- 没有消费过 delete a from gcm08 a, #tbl_sent_proc b where a.gch00c = @compid and a.gch10c = 'gx' and a.gch11c = @billid and a.gch05f = 0 -- 没有购买 and isnull(a.gch06f, 0) > 0 -- 只有赠送 and a.gch03c = b.cardid and a.gch04c = b.itemid and a.gch16i = b.procseq and isnull(b.usedtimes, 0) = 0 -- 没有消费过 drop table #tbl_sent_proc end GO /****** Object: StoredProcedure [dbo].[upg_recover_PrintableAccount] Script Date: 06/12/2017 06:20:33 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_recover_PrintableAccount] as begin -- 判断是否已做数据恢复 if exists (select 1 from data_recover where rdd00c = '001' and rdd01c = 'PrintableAccount' and rdd02c = 'PrintableAccount') begin return end insert into PrintableAccount(paa00c, paa01c, paa02i) select '001', gsb02c, 1 from gsm02 where gsb00c = '001' and gsb01c = 'o' insert into data_recover(rdd00c, rdd01c, rdd02c) values ('001', 'PrintableAccount', 'PrintableAccount') end GO /****** Object: StoredProcedure [dbo].[upg_copy_gam16] Script Date: 06/12/2017 06:19:46 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_copy_gam16] ( @compid varchar(10), @from varchar(20), @to varchar(20)) as begin delete gam16 where gan00c = @compid and gan01c = @to insert gam16(gan00c, gan01c, gan02i, gan03f) select gan00c, @to, gan02i, gan03f from gam16 where gan00c = @compid and gan01c = @from end GO /****** Object: StoredProcedure [dbo].[upg_init_recover_gsm02_ba] Script Date: 06/12/2017 06:20:14 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_init_recover_gsm02_ba] as begin -- 创建临时表 create table #data_recover_comp ( compId varchar(10) not null ) insert into #data_recover_comp (compId) select gae01c from gam05 with (nolock) -- 创建循环变量 declare @HasRecord int set @HasRecord = 1 declare @currentCompId varchar(10) while (@HasRecord = 1) begin if exists (select 1 from #data_recover_comp) begin set @HasRecord = 1 select top 1 @currentCompId = compId from #data_recover_comp if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UR001') insert gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@currentCompId, 'BA', 'UR001', '查看日记账的天数', '使用者权限设置', 2) if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UR002') insert gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@currentCompId, 'BA', 'UR002', '查看消费明细表的天数', '使用者权限设置', 2) if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UR003') insert gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@currentCompId, 'BA', 'UR003', '查看日常单据的天数', '使用者权限设置', 2) if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UR004') insert gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@currentCompId, 'BA', 'UR004', '可修改销售卡业绩的天数', '使用者权限设置', 2) if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UR005') insert gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@currentCompId, 'BA', 'UR005', '是否可查看产品成本', '使用者权限设置', 2) if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UR006') insert gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@currentCompId, 'BA', 'UR006', '是否可修改销售疗程次数与金额', '使用者权限设置', 2) if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UR007') insert gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@currentCompId, 'BA', 'UR007', '是否可以修改套餐内的项目或产品数量', '使用者权限设置', 2) if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UR008') insert gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@currentCompId, 'BA', 'UR008', '修改单据不需要客户密码', '使用者权限设置', 2) if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UR009') insert gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@currentCompId, 'BA', 'UR009', '保证金几天内允许退款', '使用者权限设置', 2) if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UR010') insert gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@currentCompId, 'BA', 'UR010', '保证金几天内允许修改', '使用者权限设置', 2) if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UR011') insert gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@currentCompId, 'BA', 'UR011', '会员资料登记后几天内允许修改', '使用者权限设置', 2) if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UR012') insert gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@currentCompId, 'BA', 'UR012', '是否允许疗程账户透支消费', '使用者权限设置', 2) if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UR013') insert gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@currentCompId, 'BA', 'UR013', '是否可直接销售不存在的会员卡号', '使用者权限设置', 2) if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UR014') insert gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@currentCompId, 'BA', 'UR014', '销售时是否可修改会员卡资料', '使用者权限设置', 2) if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UR015') insert gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@currentCompId, 'BA', 'UR015', '是否可修改的单据类型(售前, 售后, 嘉宾)', '使用者权限设置', 2) if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UR016') insert gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@currentCompId, 'BA', 'UR016', '是否可用内调支付方式', '使用者权限设置', 2) if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UR017') insert gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@currentCompId, 'BA', 'UR017', '是否可以进行套餐明细抵扣', '使用者权限设置', 2) if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UR018') insert gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@currentCompId, 'BA', 'UR018', '是否可以强行删除卡', '使用者权限设置', 2) if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UR019') insert gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@currentCompId, 'BA', 'UR019', '是否可以查看需要隐藏的特殊客户', '使用者权限设置', 2) if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UR020') insert gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@currentCompId, 'BA', 'UR020', '是否可以直接输入套餐资料', '使用者权限设置', 2) if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UR021') insert gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@currentCompId, 'BA', 'UR021', '是否可以修改账户余额', '使用者权限设置', 2) if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UR022') insert gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@currentCompId, 'BA', 'UR022', '是否可以修改疗程次数', '使用者权限设置', 2) if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UR023') insert gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@currentCompId, 'BA', 'UR023', '奖罚控制(1-可见0-不可见2-只可见)', '使用者权限设置', 2) if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UR024') insert gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@currentCompId, 'BA', 'UR024', '是否可看店长看板', '使用者权限设置', 2) if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UR025') insert gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@currentCompId, 'BA', 'UR025', '是否可分发基础数据', '使用者权限设置', 2) if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UR026') insert gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@currentCompId, 'BA', 'UR026', '查看库存单据的天数', '使用者权限设置', 2) if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UR027') insert gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@currentCompId, 'BA', 'UR027', '是否可以修改会员卡备注', '使用者权限设置', 2) if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UR028') insert gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@currentCompId, 'BA', 'UR028', '是否允许导致负库存的操作', '使用者权限设置', 2) if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UR029') insert gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@currentCompId, 'BA', 'UR029', '可见考勤资料的天数', '使用者权限设置', 2) if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UR030') insert gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@currentCompId, 'BA', 'UR030', '是否可以修改收银项目标准价格', '使用者权限设置', 2) if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UR031') insert gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@currentCompId, 'BA', 'UR031', '是否可以修改收银项目折扣与金额', '使用者权限设置', 2) if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UR032') insert gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@currentCompId, 'BA', 'UR032', '是否可以修改收银产品标准价格', '使用者权限设置', 2) if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UR033') insert gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@currentCompId, 'BA', 'UR033', '是否可以修改收银产品折扣与金额', '使用者权限设置', 2) if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UR034') insert gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@currentCompId, 'BA', 'UR034', '是否允许消费项目欠款', '使用者权限设置', 2) if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UR035') insert gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@currentCompId, 'BA', 'UR035', '是否允许购买产品欠款', '使用者权限设置', 2) if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UR036') insert gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@currentCompId, 'BA', 'UR036', '是否允许会员卡负余额', '使用者权限设置', 2) if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UR037') insert gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@currentCompId, 'BA', 'UR037', '是否允许修改产品成本', '使用者权限设置', 2) if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UR038') insert gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@currentCompId, 'BA', 'UR038', '是否可以挂失解挂', '使用者权限设置', 2) if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UR039') insert gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@currentCompId, 'BA', 'UR039', '是否可以修改服务人员', '使用者权限设置', 2) if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UR040') insert gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@currentCompId, 'BA', 'UR040', '是否可以查看开单统计信息', '使用者权限设置', 2) if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UR041') insert gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@currentCompId, 'BA', 'UR041', '是否可以修改卡销售支付明细', '使用者权限设置', 2) if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UR042') insert gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@currentCompId, 'BA', 'UR042', '是否可以看到客户联系方式', '使用者权限设置', 2) if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UR043') insert gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@currentCompId, 'BA', 'UR043', '是否可以修改疗程单价', '使用者权限设置', 2) if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UR044') insert gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@currentCompId, 'BA', 'UR044', '员工业绩查看天数', '使用者权限设置', 2) if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UR045') insert gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@currentCompId, 'BA', 'UR045', '是否可登录经理终端', '使用者权限设置', 2) if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UR046') insert gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@currentCompId, 'BA', 'UR046', '是否可设置授权', '使用者权限设置', 2) if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UR047') insert gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@currentCompId, 'BA', 'UR047', '是否可以修改疗程截止日期', '使用者权限设置', 2) if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UR048') insert gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@currentCompId, 'BA', 'UR048', '是否有分发拷贝权限', '使用者权限设置', 2) if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UR049') insert gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@currentCompId, 'BA', 'UR049', '是否有前台终端-顾客开单权限', '使用者权限设置', 2) if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UR051') insert gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@currentCompId, 'BA', 'UR051', '是否有员工信息维护中心权限', '使用者权限设置', 2) if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UR052') insert gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@currentCompId, 'BA', 'UR052', '是否可以审核员工入职', '使用者权限设置', 2) if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UR053') insert gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@currentCompId, 'BA', 'UR053', '是否可以审核员工离职', '使用者权限设置', 2) if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UR054') insert gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@currentCompId, 'BA', 'UR054', '是否有察看离职员工权限', '使用者权限设置', 2) if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UR055') insert gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@currentCompId, 'BA', 'UR055', '是否关闭预约提醒', '使用者权限设置', 2) if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UR063') begin insert gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@currentCompId, 'BA', 'UR063', '充值后几天内允许修改', '使用者权限设置', 2) end if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UR064') begin insert gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@currentCompId, 'BA', 'UR064', '收银后几天内允许修改', '使用者权限设置', 2) end if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UR065') begin insert gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@currentCompId, 'BA', 'UR065', '取单单据项目的数量金额是否可修改', '使用者权限设置', 2) end if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UR066') begin insert gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@currentCompId, 'BA', 'UR066', '取单单据项目是否可删除', '使用者权限设置', 2) end if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UN001') insert into gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) --Added by cyr 20121107 values(@currentCompId, 'BA', 'UN001', '是否可以发送员工业绩汇总短信', '使用者权限设置', 2) if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UN002') insert into gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) --Added by cyr 20121116 values(@currentCompId, 'BA', 'UN002', '是否可以发送当日员工业绩短信', '使用者权限设置', 2) if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UN003') insert into gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) --Added by cyr 20121130 values(@currentCompId, 'BA', 'UN003', '是否可以操作收入支持登记中的虚拟款项', '使用者权限设置', 2) if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UN004') insert into gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) --Added by cyr 20121203 values(@currentCompId, 'BA', 'UN004', '是否可以修改奖罚日期', '使用者权限设置', 2) if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UN005') insert into gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) --Added by cyr 20121217 values(@currentCompId, 'BA', 'UN005', '是否可以修改收银产品消耗', '使用者权限设置', 2) if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UN006') insert into gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) --Added by cyr 20121217 values(@currentCompId, 'BA', 'UN006', '是否可以修改卡销售折扣', '使用者权限设置', 2) if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UN007') insert into gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) --Added by cyr 20121228 values(@currentCompId, 'BA', 'UN007', '是否可以查看产品销售成本', '使用者权限设置', 2) if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UN008') insert into gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) --Added by cyr 20121228 values(@currentCompId, 'BA', 'UN008', '是否可以查看产品消耗成本', '使用者权限设置', 2) if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UN009') insert into gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) --Added by cyr 20121228 values(@currentCompId, 'BA', 'UN009', '是否可以查看产品标准价', '使用者权限设置', 2) if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UN010') insert into gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) --Added by cyr 20121228 values(@currentCompId, 'BA', 'UN010', '是否可以编辑产品出库/入库明细金额', '使用者权限设置', 2) if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UN011') insert into gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@currentCompId, 'BA', 'UN011', '是否可以派遣员工', '使用者权限设置', 2) if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UN012') --Added by lm 20130128 insert into gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@currentCompId, 'BA', 'UN012', '是否可以修改支付方式', '使用者权限设置', 2) --Added by cyr 20130218 if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UN013') insert into gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@currentCompId, 'BA', 'UN013', '是否可以设定回访满意度对照图', '使用者权限设置', 2) --Added by cyr 201303005 if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UN014') insert into gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@currentCompId, 'BA', 'UN014', '是否可以查看服务人员满意度', '使用者权限设置', 2) --Added by cyr 20130305 if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UN015') insert into gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@currentCompId, 'BA', 'UN015', '是否可以回访服务人员满意度', '使用者权限设置', 2) --Added by cyr 20130305 if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UN016') insert into gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@currentCompId, 'BA', 'UN016', '是否可以查看收银员满意度', '使用者权限设置', 2) --Added by cyr 20130305 if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UN017') insert into gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@currentCompId, 'BA', 'UN017', '是否可以回访收银员满意度', '使用者权限设置', 2) --Added by cyr 20130305 if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UN018') insert into gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@currentCompId, 'BA', 'UN018', '是否可以查看门店满意度', '使用者权限设置', 2) --Added by cyr 20130305 if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UN019') insert into gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@currentCompId, 'BA', 'UN019', '是否可以回访门店满意度', '使用者权限设置', 2) --Added by cyr 20130305 if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UN020') insert into gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@currentCompId, 'BA', 'UN020', '是否可以查看实业绩', '使用者权限设置', 2) --Added by cyr 20130312 if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UN021') insert into gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@currentCompId, 'BA', 'UN021', '是否可以查看每日提醒', '使用者权限设置', 2) --Added by cyr 20130314 if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UN022') insert into gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@currentCompId, 'BA', 'UN022', '是否可以修改已回访的回访单', '使用者权限设置', 2) --Added by cyr 20130314 if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UN023') insert into gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@currentCompId, 'BA', 'UN023', '可以查看已回访的天数', '使用者权限设置', 2) --Added by cyr 20130314 if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UN024') insert into gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@currentCompId, 'BA', 'UN024', '是否可以批量开帐', '使用者权限设置', 2) --Added by cyr 20130403 if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UN025') insert into gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@currentCompId, 'BA', 'UN025', '可以查看员工奖罚的天数', '使用者权限设置', 2) --Added by cyr 20130407 if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UN026') insert into gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@currentCompId, 'BA', 'UN026', '是否可以修改卡销售时的卡标准价', '使用者权限设置', 2) --Added by cyr 20130407 if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UN027') insert into gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@currentCompId, 'BA', 'UN027', '是否可以导出会员卡资料', '使用者权限设置', 2) --Added by cyr 20130415 if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UN028') insert into gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@currentCompId, 'BA', 'UN028', '销售卡时是否可修改卡应收金额', '使用者权限设置', 2) --Added by cyr 20130510 if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UN029') insert into gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@currentCompId, 'BA', 'UN029', '销售卡时是否可修改卡储值金额', '使用者权限设置', 2) --Added by cyr 20130510 if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UN030') insert into gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@currentCompId, 'BA', 'UN030', '销售卡时卡标准价是否允许低于卡类别标准价', '使用者权限设置', 2) --Added by cyr 20130603 if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UN031') insert gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@currentCompId, 'BA', 'UN031', '是否允许修改个人账户折扣系数', '使用者权限设置', 2) --Added by cyr 20120407 if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UN032') insert gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@currentCompId, 'BA', 'UN032', '是否允许设置员工提成方案', '使用者权限设置', 2) --Added by cyr 20120416 if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UN033') insert gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@currentCompId, 'BA', 'UN033', '是否允许打印员工业绩小单', '使用者权限设置', 2) --Added by cyr 20120904 if not exists (select 1 from gsm02 where gsb00c = @currentCompId and gsb01c = 'BA' and gsb02c = 'UN034') insert gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@currentCompId, 'BA', 'UN034', '是否允许打印日记账小单', '使用者权限设置', 2) --Added by cyr 20120904 -- 清除已处理的门店 delete #data_recover_comp where compId = @currentCompId end else begin set @HasRecord = 0 end end drop table #data_recover_comp end GO /****** Object: StoredProcedure [dbo].[upg_recover_pay_classify_single] Script Date: 06/12/2017 06:20:33 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_recover_pay_classify_single] ( @currentCompId varchar(10) --公司 ) as begin -- 员工的归属门店 if not exists (select 1 from data_recover where rdd00c = @currentCompId and rdd01c = 'pay_classify' and rdd02c = 'pay_classify') begin insert into pay_classify(comp, pay) select @currentCompId, gsb02c from gsm02 where gsb00c = @currentCompId and gsb01c = '3' update pay_classify set category = 1 where comp = @currentCompId and pay in('1', '6', 'WX', 'ZFB', '&') update pay_classify set category = 2 where comp = @currentCompId and (pay in('4', '#', '9') or ((pay >= 'A' and pay <= 'Z') and ((len(pay) = 1) or ((len(pay) = 2) and (substring(pay, 1, 1) in ('A', 'B', 'C', 'D')))))) update pay_classify set category = 3 where comp = @currentCompId and pay in('$', '3') update pay_classify set category = 4 where comp = @currentCompId and pay in('5') update pay_classify set category = 5 where comp = @currentCompId and pay in('0', '8') insert into data_recover(rdd00c, rdd01c, rdd02c) values (@currentCompId, 'pay_classify', 'pay_classify') end end GO /****** Object: StoredProcedure [dbo].[upg_get_store_display] Script Date: 06/12/2017 06:20:09 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_get_store_display] ( @comp varchar(10) ) as begin declare @lvl int declare @stype int declare @lvlName varchar(100) declare @stypeName varchar(100) declare @maxAQ int select @lvl = lvl, @stype = stype from b_perf with(nolock) where bran_id = @comp select @lvlName = gsb03c from gsm02 with(nolock) where gsb00c = @comp and gsb01c = 'AQ' and gsb02c = @lvl select @stypeName = gsb03c from gsm02 with(nolock) where gsb00c = @comp and gsb01c = 'ST' and gsb02c = @stype select @maxAQ = max(gsb02c) from gsm02 with(nolock) where gsb00c = @comp and gsb01c = 'AQ' declare @name varchar(100) if @maxAQ = @lvl and isnull(@stype, -1) > 0 set @name = @stypeName else set @name = @lvlName select @name name end GO /****** Object: StoredProcedure [dbo].[upg_init_acct2pay] Script Date: 06/12/2017 06:20:12 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_init_acct2pay] as begin delete gsm14 insert into gsm14(gsn01c, gsn02c) select distinct gsb02c, '' from gsm02 where gsb01c = '3' update gsm14 set gsn02c = '999' where gsn01c = '1' update gsm14 set gsn02c = '1' where gsn01c = '2' update gsm14 set gsn02c = '2' where gsn01c = '4' update gsm14 set gsn02c = '3' where gsn01c = '7' update gsm14 set gsn02c = '4' where gsn01c = '9' update gsm14 set gsn02c = '5' where gsn01c = 'A' update gsm14 set gsn02c = convert(varchar(2), 5 + ascii(gsn01c) - ascii('A')) where gsn01c > 'A' and gsn01c <= 'Z' and len(gsn01c) = 1 update gsm14 set gsn02c = convert(varchar(2), 31 + ascii(substring(gsn01c, 2, 1)) - ascii('A')) where gsn01c > 'A' and gsn01c <= 'Z' and len(gsn01c) = 2 and substring(gsn01c, 1, 1) = 'A' update gsm14 set gsn02c = convert(varchar(2), 57 + ascii(substring(gsn01c, 2, 1)) - ascii('A')) where gsn01c > 'A' and gsn01c <= 'Z' and len(gsn01c) = 2 and substring(gsn01c, 1, 1) = 'B' update gsm14 set gsn02c = convert(varchar(2), 83 + ascii(substring(gsn01c, 2, 1)) - ascii('A')) where gsn01c > 'A' and gsn01c <= 'Z' and len(gsn01c) = 2 and substring(gsn01c, 1, 1) = 'C' update gsm14 set gsn02c = convert(varchar(2), 109 + ascii(substring(gsn01c, 2, 1)) - ascii('A')) where gsn01c > 'A' and gsn01c <= 'Z' and len(gsn01c) = 2 and substring(gsn01c, 1, 1) = 'D' end GO /****** Object: StoredProcedure [dbo].[upg_init_gsm02_3_role] Script Date: 06/12/2017 06:20:14 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_init_gsm02_3_role] as begin -- 判断是否已做数据恢复 if exists (select 1 from data_recover where rdd00c = '001' and rdd01c = 'gsm02_3_role' and rdd02c = 'init') begin return end insert into gsm02_3_role(gsa01c_role, gsa02c_role, gsa03i_role, gsa04i_role, gsa05i_role, gsa06i_role, gsa07i_role, gsa08i_role) select _role, _pay, 1, 1, --(case when _pay in ('2', '4', '9', 'A', '7', '#') then 0 else 1 end), 1, 1, 1, 1 from (select gsb02c as _role from gsm02 where gsb00c = '001' and gsb01c = 'J') P join (select gsb02c as _pay from gsm02 where gsb00c = '001' and gsb01c = '3') Q on 1 = 1 insert into data_recover(rdd00c, rdd01c, rdd02c) values ('001', 'gsm02_3_role', 'init') end GO /****** Object: StoredProcedure [dbo].[getinto_store_channel] Script Date: 06/12/2017 06:19:20 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create proc [dbo].[getinto_store_channel] ( @compid varchar(10), -- 公司别 @type varchar(20), -- 统计类别 0--按天统计 1--按月统计 2--按年统计 @fromdate varchar(10), -- 起始日期 @todate varchar(10) -- 截止日期 ) as begin if isnull(@todate, '*') = '*' set @todate = @fromdate create table #into_store -- 产品资料档 ( gis00c varchar(10) not null, -- 公司编号 gis01c varchar(10) null, -- 进店渠道类别 gis02c varchar(50) null, -- 进店渠道名称 gis03f float null, -- 统计数量 gis04f float null, -- 统计金额 gis05d varchar(8) null, -- 统计日期 gis06c varchar(10) null -- 订单类别 gx--收银 gn--卖卡 gz--充值 ) insert #into_store(gis00c, gis01c, gis03f, gis04f, gis05d, gis06c) select @compid, case when isnull(max(gga84c), '') = '' then '0' else max(gga84c) end item, max(isnull(gga14f, 0)), isnull((select sum(gsc05f) from gsm03 where gsc00c = gga00c and gsc02c = 'gx' and gsc01c = gga01c), 0) amt, max(gga02d) date, 'gx' from ggm01 where gga00c = @compid and gga02d >= @fromdate and gga02d <= @todate group by gga00c, gga01c insert #into_store(gis00c, gis01c, gis03f, gis04f, gis05d, gis06c) select @compid, case when isnull(max(gna109c), '') = '' then '0' else max(gna109c) end item, 1, isnull((select sum(gsc05f) from gsm03 where gsc00c = gna00c and gsc02c = 'gn' and gsc01c = gna01c), 0) amt, max(gna02d) date, 'gn' from gnm01 where gna00c = @compid and gna02d >= @fromdate and gna02d <= @todate group by gna00c, gna01c insert #into_store(gis00c, gis01c, gis03f, gis04f, gis05d, gis06c) select @compid, case when isnull(max(gcl109c), '') = '' then '0' else max(gcl109c) end item, 1, isnull((select sum(gsc05f) from gsm03 where gsc00c = gcl00c and gsc02c = 'gz' and gsc01c = gcl01c), 0) amt, max(gcl02d) date, 'gz' from gcm10 where gcl00c = @compid and gcl02d >= @fromdate and gcl02d <= @todate group by gcl00c, gcl01c update a set a.gis02c = b.gsb03c from #into_store a, gsm02 b where a.gis00c = b.gsb00c and a.gis01c = b.gsb02c and b.gsb01c = 'NOS0' declare @informationKey varchar(5) --资料代码 declare @informationValue varchar(40) --资料内容 declare @condtion varchar(max) --动态的条件语句 set @condtion = '' declare @sql varchar(max) declare cur_gsm02x cursor for select gsb02c, gsb03c from gsm02 where gsb00c = @compid and gsb01c = 'NOS0' open cur_gsm02x fetch cur_gsm02x into @informationKey, @informationValue while @@fetch_status = 0 begin set @condtion = @condtion + ' sum(case when gis01c = ' + convert(nvarchar(10), @informationKey) + ' and gis06c = ''gx'' then gis03f else 0 end) as gxnum' + convert(nvarchar(10), @informationKey) + ', ' set @condtion = @condtion + ' sum(case when gis01c = ' + convert(nvarchar(10), @informationKey) + ' and gis06c = ''gx'' then gis04f else 0 end) as gxamt' + convert(nvarchar(10), @informationKey) + ', ' set @condtion = @condtion + ' sum(case when gis01c = ' + convert(nvarchar(10), @informationKey) + ' and gis06c = ''gn'' then gis03f else 0 end) as gnnum' + convert(nvarchar(10), @informationKey) + ', ' set @condtion = @condtion + ' sum(case when gis01c = ' + convert(nvarchar(10), @informationKey) + ' and gis06c = ''gn'' then gis04f else 0 end) as gnamt' + convert(nvarchar(10), @informationKey) + ', ' set @condtion = @condtion + ' sum(case when gis01c = ' + convert(nvarchar(10), @informationKey) + ' and gis06c = ''gz'' then gis03f else 0 end) as gznum' + convert(nvarchar(10), @informationKey) + ', ' set @condtion = @condtion + ' sum(case when gis01c = ' + convert(nvarchar(10), @informationKey) + ' and gis06c = ''gz'' then gis04f else 0 end) as gzamt' + convert(nvarchar(10), @informationKey) + ', ' set @condtion = @condtion + ' sum(case when gis01c = ' + convert(nvarchar(10), @informationKey) + ' then gis03f else 0 end) as totalnum' + convert(nvarchar(10), @informationKey) + ', ' set @condtion = @condtion + ' sum(case when gis01c = ' + convert(nvarchar(10), @informationKey) + ' then gis04f else 0 end) as totalamt' + convert(nvarchar(10), @informationKey) + ', ' fetch cur_gsm02x into @informationKey, @informationValue end set @condtion = substring(@condtion, 1, len(@condtion)-1) if @type = 0 begin set @sql = '' set @sql = 'select convert(nvarchar(10), cast(gis05d as datetime), 120) gis05d, ' + @condtion + ' from #into_store group by gis05d ' exec (@sql) end else if @type = 1 begin set @sql = '' set @sql = 'select right(left(gis05d, 6), 2) gis05d, ' + @condtion + ' from #into_store group by left(gis05d, 6) ' exec (@sql) end else if @type = 2 begin set @sql = '' set @sql = 'select left(gis05d, 4) gis05d, ' + @condtion + ' from #into_store group by left(gis05d, 4) ' exec (@sql) end close cur_gsm02x deallocate cur_gsm02x drop table #into_store end GO /****** Object: StoredProcedure [dbo].[upg_lrm01_total] Script Date: 06/12/2017 06:20:18 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_lrm01_total] ( @companyId varchar(10), @status int, --状态(0 申请未发货,1 已发货 , 2 已收货) @gra23i int, -- 申请单位 0-门店申请,1-个人申请 , -1为全部 @fromgy varchar(60), --采购人 @togy varchar(60), --采购人 @fromdate varchar(10), --开始日期 @todate varchar(10) --截止日期 ) as begin if @status = 0 begin select isnull(grb00c,'') grb00c, -- 门店编号 isnull(grb01c,'') grb01c, -- 订单号 isnull(gra02d,'') gra02d, -- 订单日期 isnull(grb03c,'') grb03c, -- 产品编号 grb03cName = isnull((select gfa03c from gfm01 where gfa00c = grb00c and gfa01c = grb03c),''), grb05cName = isnull((select gsb03c from gsm02 where gsb01c = 'K' and gsb00c = grb00c and gsb02c = grb05c),''),--单位 isnull(grb06f,'') grb06f, -- 进货单价 isnull(grb04f,'') grb04f, -- 数量 isnull(grb07f,'') grb07f, -- 金额 gra23iName = isnull((case gra23i when 1 then '员工申请' else '门店申请' end),''), isnull(gra03c,'') gra03c, -- 采购人 gra03cName = isnull((select haa02c from ham01 where haa00c = grb00c and haa01c = gra03c),''), isnull(gra17c,'') gra17c -- 备注 from lrm02 inner join lrm01 on gra00c = grb00c and gra01c = grb01c where grb00c in (select gaz02c from gam26 where gaz01c = @companyId ) and gra04c < 4 --未发货 and ( @gra23i = -1 or gra23i = @gra23i) and ( @fromgy = '' or (gra03c between @fromgy and @togy)) and ( @fromdate = '' or (gra02d between @fromdate and @todate )) end else if @status = 1 begin select isnull(gta03c,'') gta03c, -- 门店编号 isnull(gtb01c,'') gtb01c, -- 申请单号 isnull(gta08d,'') gta08d, -- 发货日期 isnull(gtb03c,'') gtb03c, -- 产品编号 gtb03cName = isnull((select gfa03c from gfm01 where gfa00c = gtb00c and gfa01c = gtb03c),''), gtb05cName = isnull((select gsb03c from gsm02 where gsb01c = 'K' and gsb00c = gtb00c and gsb02c = gtb05c),''),-- 产品单位 isnull(gtb10f,'') gtb10f, -- 产品单价 isnull(gtb07f,'') gtb07f, -- 申请发货数量 isnull(gtb08f,'') gtb08f, -- 实际发货量 isnull(gtb06f,'') gtb06f, -- 产品金额 isnull((case gta20i when 1 then '供应商发货' else '总部发货' end),'') gta20iName, isnull(gta21c,'') gta21c, -- 快递名称 isnull(gta22c,'') gta22c, -- 快递单号 isnull((case gtb13i when 0 then '' else '赠品' end),'') gtb13iName, -- 是否为赠品(0默认非赠品,1赠品) isnull(gta19c,'') gta19c -- 备注 from ltm02 inner join ltm01 on gta00c = gtb00c and gta01c = gtb01c where gtb00c in (select gaz02c from gam26 where gaz01c = @companyId ) and gta06i = 1 and ( @fromdate = '' or (gta08d between @fromdate and @todate )) end else begin select isnull(gvb00c,'') gvb00c, isnull(gva01c,'') gva01c, -- 收货单号 isnull(gva02d,'') gva02d, -- 收货日期 isnull(gva06c,'') gva06c, -- 采购单号 isnull(gva05c,'') gva05c, -- 发货单号 isnull(gva04c,'') gva04c, -- 收货人 gva04cName = isnull((select haa02c from ham01 where haa00c = gvb00c and haa01c = gva04c),''), isnull(gvb03c,'') gvb03c, -- 产品编号 gvb09cName = isnull((select gsb03c from gsm02 where gsb01c = 'K' and gsb00c = gvb00c and gsb02c = gvb09c),''), -- 单位 isnull(gvb08f,'') gvb08f, -- 订单数量 isnull(gvb05f,'') gvb05f, -- 发货数量 isnull(gvb04f,'') gvb04f, -- 收货数量 isnull(gvb06f,'') gvb06f, -- 损坏数量 isnull(gvb07f,'') gvb07f, -- 欠货数量 isnull((case gvb13i when 0 then '' else '赠品' end),'') gvb13iName -- 收货来源 0-正常, 1-赠送 from lvm02 inner join lvm01 on gva00c = gvb00c and gva01c = gvb01c where gva00c in (select gaz02c from gam26 where gaz01c = @companyId ) and ( @fromdate = '' or (gva02d between @fromdate and @todate )) end end GO /****** Object: StoredProcedure [dbo].[upg_m_dispatch_gsm02] Script Date: 06/12/2017 06:20:20 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_m_dispatch_gsm02] ( @fromcompid varchar(10), -- 从哪一个公司发布 @tocompid varchar(10), -- 目的公司 @mandatory varchar(2) -- 是否强制统一 ) as begin if @fromcompid = @tocompid return if @mandatory = '1' begin delete gsm02 where gsb00c = @tocompid end else begin delete b from gsm02 a, gsm02 b where a.gsb00c = @fromcompid and a.gsb01c = b.gsb01c and a.gsb02c = b.gsb02c and b.gsb00c = @tocompid end insert gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i, gsb06i) select @tocompid, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i, gsb06i from gsm02 where gsb00c = @fromcompid -- 产品大类下面分小类 delete gsm26 where gsp00c = @tocompid insert gsm26(gsp00c, gsp02i, gsp03c, gsp04c, gsp05c, gsp06c) select @tocompid, gsp02i, gsp03c, gsp04c, gsp05c, gsp06c from gsm26 a where gsp00c = @fromcompid delete gsm27 where gsq00c = @tocompid insert gsm27(gsq00c, gsq02i, gsq03c, gsq04c, gsq05c) select @tocompid, gsq02i, gsq03c, gsq04c, gsq05c from gsm27 a where gsq00c = @fromcompid end GO /****** Object: StoredProcedure [dbo].[upg_staff_rewardOrPunishment] Script Date: 06/12/2017 06:20:42 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_staff_rewardOrPunishment] ( @compid varchar(10), -- 已经能支持各级别的公司 @fromdate varchar(10), -- 开始日期 @todate varchar(10), -- 结束日期 @fromstaff varchar(10), -- 员工开编号 @tostaff varchar(10), -- 员工结束编号 @type varchar(10) -- 奖罚类别 ) as begin create table #ham08x -- 员工基本资料--员工奖罚 ( hah00c varchar(10) not null, -- 公司别 hah01c varchar(10) not null, -- 员工代码 hah02f float not null, -- 序号 hah03d varchar(8) null, -- 日期 hah04c varchar(10) null, -- 罚款类别 hah05f float null, -- 罚款金额 hah06c varchar(200) null, -- 备注 ) insert into #ham08x(hah00c, hah01c, hah02f, hah03d, hah04c, hah05f, hah06c) select hah00c, hah01c, hah02f, hah03d, hah04c, hah05f, hah06c from ham08 with(nolock) where (hah03d between @fromdate and @todate) and (hah01c between @fromstaff and @tostaff or @fromstaff = '*') and hah00c = @compid and (hah04c = @type or @type = '*' ) declare @informationKey varchar(5) --资料代码 declare @condtion varchar(max) -- 动态的条件语句 set @condtion = '' declare cur_gsm02x cursor for select gsb02c from gsm02 with(nolock) where gsb00c = @compid and gsb01c = 'AI' open cur_gsm02x fetch cur_gsm02x into @informationKey while @@fetch_status = 0 begin set @condtion = @condtion + 'isnull(sum(case hah04c when ' + @informationKey + ' then hah05f else 0 end),0) as type' + @informationKey + ', ' fetch cur_gsm02x into @informationKey end set @condtion = 'select isnull(hah00c,'''') as compId, isnull(hah01c,'''') as empId, isnull((select top 1 haa02c from ham01 with(nolock) where haa00c = hah00c and haa01c = hah01c),'''') as empname, ' + @condtion + ' isnull(sum(hah05f),0) as sumamt from #ham08x group by hah00c, hah01c' exec (@condtion) close cur_gsm02x deallocate cur_gsm02x drop table #ham08x end GO /****** Object: StoredProcedure [dbo].[upg_gia04i_paymode_user] Script Date: 06/12/2017 06:20:11 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_gia04i_paymode_user] ( @compid varchar(10), @user varchar(20), --使用者 @customer varchar(60) --客户卡号 ) as begin if( @customer = '') begin select A.gsa02c, B.gsb03c, A.gsa03i, A.gsa04i, A.gsa05i, A.gsa06i, A.gsa07i, A.gsa08i from gsm02_3 A with(nolock) inner join gsm02 B with(nolock) on B.gsb00c = @compId and B.gsb01c = '3' and isnull(B.gsb06i, 1) <> 0 and A.gsa01c = @user and A.gsa02c = B.gsb02c and (A.gsa02c < 'A' or A.gsa02c > 'Z' or A.gsa02c = 'WX' or A.gsa02c = 'ZFB' ) and A.gsa02c not in ('7','4','9','#') order by B.gsb06i end else begin create table #gcm03_pay ( gsa02c varchar(30) ) --卡账户所对应的支付方式(不能关联部门,卡不一定在当前部门存在账户) insert into #gcm03_pay select gsn01c from gcm03 with(nolock) left join gsm14 with(nolock) on gsn02c = convert(varchar(10), gcc03i) where gcc01c = @customer select a.gsa02c,b.gsb03c,gsa03i,gsa04i,gsa05i,gsa06i,gsa07i,gsa08i from ( --使用者可见支付方式 select gsa02c,gsa03i,gsa04i,gsa05i,gsa06i,gsa07i,gsa08i from gsm02_3 with(nolock) where gsa01c = @user and (gsa02c < 'A' or gsa02c > 'Z' or gsa02c = 'WX') union --卡账户所对应的支付方式 select gsa02c,1 as gsa03i,1 as gsa04i,1 as gsa05i,1 as gsa06i,1 as gsa07i,1 as gsa08i from #gcm03_pay ) as a left join gsm02 b with(nolock) on b.gsb00c = @compid and b.gsb01c = '3' and b.gsb02c = a.gsa02c drop table #gcm03_pay end end GO /****** Object: StoredProcedure [dbo].[upg_customexpired] Script Date: 06/12/2017 06:19:55 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_customexpired] ( @compid varchar(20), --已经能支持各级别的公司 @yyear varchar(10), --年份查询条件 @balancefrom float, --余额开始范围 @balanceto float, --余额结束范围 @balancesplit float --余额间隔 ) as begin create table #gcm06 -- 会员卡--疗程临时表 ( gcf00c varchar(10) not null, --公司编号 gcf01c varchar(20) not null, --卡号 gcf02c varchar(20) not null, --项目编号 gcf03f float null, --次数 gcf04f float null, --赠送次数 gcf05f float null, --总次数 gcf06f float null, --已经使用次数 gcf07f float null, --剩余次数 gcf08f float null, --疗程金额 gcf09f float null, --已经使用金额 gcf10f float null, --剩余金额 gcf11d varchar(8) null, --账户开立日 gcf12d varchar(8) null, --账户截至日 gcf13i int null, --疗程间隔 gcf14f float null, --一个月可使用的次数 0--代表不控制 直到这个疗程用完其他float就是一个月可以使用的次数 gcf15c varchar(60) null, --输入的时候的备注 gcf16i int null, --是否已经审核 gcf19i int null, --是否停用 gcf20i int null, --标志 是否前期卡 ) create table #customexpired --客户到期人数统计 ( iid int identity not null, compid varchar(20), --公司别 quarter int null, --季度 cardType int null, --疗程卡类型(0当前,1前期) balancescope varchar(20) null, --余额范围 personcount int null, --到期人数 balance float null default 0, --到期结余金额 scopeorde int null, --余额范围序号 ) insert #gcm06 (gcf00c, gcf01c, gcf02c, gcf10f, gcf12d, gcf20i) select gcf00c, gcf01c, gcf02c, gcf10f, gcf12d, gcf20i from gcm06 a with (nolock), gam26 z with (nolock), gcm01 c with (nolock) where isnull(a.gcf12d, '') >= (@yyear + '0101') and isnull(a.gcf12d, '') <= (@yyear + '1231') and a.gcf00c = z.gaz02c and z.gaz01c = @compid and a.gcf00c = c.gca00c and a.gcf01c = c.gca01c and c.gca00c = c.gca13d --前期卡的设置剩余金额 update a set a.gcf10f = a.gcf07f * 28 from #gcm06 a, gdm01 b with (nolock) where isnull(gcf20i, 0) > 0 and a.gcf00c = b.gda00c and a.gcf02c = b.gda01c and isnull(b.gda13c, '') = '233' and isnull(gcf10f, 0) <= 0 update a set a.gcf10f = a.gcf07f * 38 from #gcm06 a, gdm01 b with (nolock) where isnull(gcf20i, 0)>0 and a.gcf00c = b.gda00c and a.gcf02c = b.gda01c and isnull(b.gda13c, '')<>'233' and isnull(gcf10f, 0) <= 0 declare @tempbalance float --结余金额 set @tempbalance = @balancefrom declare @tempscopeorde int --余额范围序号 set @tempscopeorde = 0 while(@tempbalance <@balanceto) begin if ((@tempbalance + @balancesplit) <= @balanceto) begin set @tempscopeorde = @tempscopeorde + 1 ---第一季度当期卡 insert #customexpired(compid, quarter, cardType, balancescope, personcount, balance, scopeorde) select gcf00c, 1, 0, cast(@tempbalance as varchar(10)) + '-' + cast((@tempbalance + @balancesplit) as varchar(10)), count(*), isnull(sum(isnull(gcf10f, 0)), 0), @tempscopeorde from #gcm06 a, gam26 z with (nolock) where isnull(a.gcf20i, 0) = 0 -- 当期 and isnull(a.gcf12d, '') >= (@yyear + '0101') and isnull(a.gcf12d, '') <= (@yyear + '0331') and a.gcf10f >= @tempbalance and a.gcf10f <= (@tempbalance + @balancesplit) and a.gcf00c = z.gaz02c and z.gaz01c = @compid group by gcf00c ---第一季度前期卡 insert #customexpired(compid, quarter, cardType, balancescope, personcount, balance, scopeorde) select gcf00c, 1, 1, cast(@tempbalance as varchar(10)) + '-' + cast((@tempbalance + @balancesplit) as varchar(10)), count(*), isnull(sum(isnull(gcf10f, 0)), 0), @tempscopeorde from #gcm06 a, gam26 z with (nolock) where isnull(a.gcf20i, 0) > 0 --前期 and isnull(a.gcf12d, '') >= (@yyear + '0101') and isnull(a.gcf12d, '') <= (@yyear + '0331') and a.gcf10f >= @tempbalance and a.gcf10f <= (@tempbalance + @balancesplit) and a.gcf00c = z.gaz02c and z.gaz01c = @compid group by gcf00c ---第二季度当期卡 insert #customexpired(compid, quarter, cardType, balancescope, personcount, balance, scopeorde) select gcf00c, 2, 0, cast(@tempbalance as varchar(10)) + '-' + cast((@tempbalance + @balancesplit) as varchar(10)), count(*), isnull(sum(isnull(gcf10f, 0)), 0), @tempscopeorde from #gcm06 a, gam26 z with (nolock) where isnull(a.gcf20i, 0) = 0 and isnull(a.gcf12d, '') >= (@yyear + '0401') and isnull(a.gcf12d, '') <= (@yyear + '0630') and a.gcf10f >= @tempbalance and a.gcf10f <= (@tempbalance + @balancesplit) and a.gcf00c = z.gaz02c and z.gaz01c = @compid group by gcf00c ---第二季度前期卡 insert #customexpired(compid, quarter, cardType, balancescope, personcount, balance, scopeorde) select gcf00c, 2, 1, cast(@tempbalance as varchar(10)) + '-' + cast((@tempbalance + @balancesplit) as varchar(10)), count(*), isnull(sum(isnull(gcf10f, 0)), 0), @tempscopeorde from #gcm06 a, gam26 z with (nolock) where isnull(a.gcf20i, 0) >0 and isnull(a.gcf12d, '') >= (@yyear + '0401') and isnull(a.gcf12d, '') <= (@yyear + '0630') and a.gcf10f >= @tempbalance and a.gcf10f <= (@tempbalance + @balancesplit) and a.gcf00c = z.gaz02c and z.gaz01c = @compid group by gcf00c ---第三季度当期卡 insert #customexpired(compid, quarter, cardType, balancescope, personcount, balance, scopeorde) select gcf00c, 3, 0, cast(@tempbalance as varchar(10)) + '-' + cast((@tempbalance + @balancesplit) as varchar(10)), count(*), isnull(sum(isnull(gcf10f, 0)), 0), @tempscopeorde from #gcm06 a, gam26 z with (nolock) where isnull(a.gcf20i, 0) = 0 and isnull(a.gcf12d, '') >= (@yyear + '0701') and isnull(a.gcf12d, '') <= (@yyear + '0930') and a.gcf10f >= @tempbalance and a.gcf10f <= (@tempbalance + @balancesplit) and a.gcf00c = z.gaz02c and z.gaz01c = @compid group by gcf00c ---第三季度前期卡 insert #customexpired(compid, quarter, cardType, balancescope, personcount, balance, scopeorde) select gcf00c, 3, 1, cast(@tempbalance as varchar(10)) + '-' + cast((@tempbalance + @balancesplit) as varchar(10)), count(*), isnull(sum(isnull(gcf10f, 0)), 0), @tempscopeorde from #gcm06 a, gam26 z with (nolock) where isnull(a.gcf20i, 0) >0 and isnull(a.gcf12d, '') >= (@yyear + '0701') and isnull(a.gcf12d, '') <= (@yyear + '0930') and a.gcf10f >= @tempbalance and a.gcf10f <= (@tempbalance + @balancesplit) and a.gcf00c = z.gaz02c and z.gaz01c = @compid group by gcf00c ---第四季度当期卡 insert #customexpired(compid, quarter, cardType, balancescope, personcount, balance, scopeorde) select gcf00c, 4, 0, cast(@tempbalance as varchar(10)) + '-' + cast((@tempbalance + @balancesplit) as varchar(10)), count(*), isnull(sum(isnull(gcf10f, 0)), 0), @tempscopeorde from #gcm06 a, gam26 z with (nolock) where isnull(a.gcf20i, 0) = 0 and isnull(a.gcf12d, '') >= (@yyear + '1001') and isnull(a.gcf12d, '') <= (@yyear + '1231') and a.gcf10f >= @tempbalance and a.gcf10f <= (@tempbalance + @balancesplit) and a.gcf00c = z.gaz02c and z.gaz01c = @compid group by gcf00c ---第四季度前期卡 insert #customexpired(compid, quarter, cardType, balancescope, personcount, balance, scopeorde) select gcf00c, 4, 1, cast(@tempbalance as varchar(10)) + '-' + cast((@tempbalance + @balancesplit) as varchar(10)), count(*), isnull(sum(isnull(gcf10f, 0)), 0), @tempscopeorde from #gcm06 a, gam26 z with (nolock) where isnull(a.gcf20i, 0)>0 and isnull(a.gcf12d, '') >= (@yyear + '1001') and isnull(a.gcf12d, '') <= (@yyear + '1231') and a.gcf10f >= @tempbalance and a.gcf10f <= (@tempbalance + @balancesplit) and a.gcf00c = z.gaz02c and z.gaz01c = @compid group by gcf00c ---年度合计当期卡 insert #customexpired(compid, quarter, cardType, balancescope, personcount, balance, scopeorde) select gcf00c, 5, 0, cast(@tempbalance as varchar(10)) + '-' + cast((@tempbalance + @balancesplit) as varchar(10)), count(*), isnull(sum(isnull(gcf10f, 0)), 0), @tempscopeorde from #gcm06 a, gam26 z with (nolock) where isnull(a.gcf20i, 0) = 0 and a.gcf10f >= @tempbalance and a.gcf10f <= (@tempbalance + @balancesplit) and a.gcf00c = z.gaz02c and z.gaz01c = @compid group by gcf00c ---年度合计前期卡 insert #customexpired(compid, quarter, cardType, balancescope, personcount, balance, scopeorde) select gcf00c, 5, 1, cast(@tempbalance as varchar(10)) + '-' + cast((@tempbalance + @balancesplit) as varchar(10)), count(*), isnull(sum(isnull(gcf10f, 0)), 0), @tempscopeorde from #gcm06 a, gam26 z with (nolock) where isnull(a.gcf20i, 0) > 0 and a.gcf10f >= @tempbalance and a.gcf10f <= (@tempbalance + @balancesplit) and a.gcf00c = z.gaz02c and z.gaz01c = @compid group by gcf00c set @tempbalance = @balancesplit + @tempbalance end else begin set @tempscopeorde = @tempscopeorde + 1 ---第一季度当期卡 insert #customexpired(compid, quarter, cardType, balancescope, personcount, balance, scopeorde) select gcf00c, 1, 0, cast(@tempbalance as varchar(10)) + '-' + cast(@balanceto as varchar(10)), count(*), isnull(sum(isnull(gcf10f, 0)), 0), @tempscopeorde from #gcm06 a, gam26 z with (nolock) where isnull(a.gcf20i, 0) = 0 and isnull(a.gcf12d, '') >= (@yyear + '0101') and isnull(a.gcf12d, '') <= (@yyear + '0331') and a.gcf10f >= @tempbalance and a.gcf10f <= @balanceto and a.gcf00c = z.gaz02c and z.gaz01c = @compid group by gcf00c ---第一季度前期卡 insert #customexpired(compid, quarter, cardType, balancescope, personcount, balance, scopeorde) select gcf00c, 1, 1, cast(@tempbalance as varchar(10)) + '-' + cast(@balanceto as varchar(10)), count(*), isnull(sum(isnull(gcf10f, 0)), 0), @tempscopeorde from #gcm06 a, gam26 z with (nolock) where isnull(a.gcf20i, 0) >0 and isnull(a.gcf12d, '') >= (@yyear + '0101') and isnull(a.gcf12d, '') <= (@yyear + '0331') and a.gcf10f >= @tempbalance and a.gcf10f <= @balanceto and a.gcf00c = z.gaz02c and z.gaz01c = @compid group by gcf00c ---第二季度当期卡 insert #customexpired(compid, quarter, cardType, balancescope, personcount, balance, scopeorde) select gcf00c, 2, 0, cast(@tempbalance as varchar(10)) + '-' + cast(@balanceto as varchar(10)), count(*), isnull(sum(isnull(gcf10f, 0)), 0), @tempscopeorde from #gcm06 a, gam26 z with (nolock) where isnull(a.gcf20i, 0) = 0 and isnull(a.gcf12d, '') >= (@yyear + '0401') and isnull(a.gcf12d, '') <= (@yyear + '0630') and a.gcf10f >= @tempbalance and a.gcf10f <= @balanceto and a.gcf00c = z.gaz02c and z.gaz01c = @compid group by gcf00c ---第二季度前期卡 insert #customexpired(compid, quarter, cardType, balancescope, personcount, balance, scopeorde) select gcf00c, 2, 1, cast(@tempbalance as varchar(10)) + '-' + cast(@balanceto as varchar(10)), count(*), isnull(sum(isnull(gcf10f, 0)), 0), @tempscopeorde from #gcm06 a, gam26 z with (nolock) where isnull(a.gcf20i, 0) >0 and isnull(a.gcf12d, '') >= (@yyear + '0401') and isnull(a.gcf12d, '') <= (@yyear + '0630') and a.gcf10f >= @tempbalance and a.gcf10f <= @balanceto and a.gcf00c = z.gaz02c and z.gaz01c = @compid group by gcf00c ---第三季度当期卡 insert #customexpired(compid, quarter, cardType, balancescope, personcount, balance, scopeorde) select gcf00c, 3, 0, cast(@tempbalance as varchar(10)) + '-' + cast(@balanceto as varchar(10)), count(*), isnull(sum(isnull(gcf10f, 0)), 0), @tempscopeorde from #gcm06 a, gam26 z with (nolock) where isnull(a.gcf20i, 0) = 0 and isnull(a.gcf12d, '') >= (@yyear + '0701') and isnull(a.gcf12d, '') <= (@yyear + '0930') and a.gcf10f >= @tempbalance and a.gcf10f <= @balanceto and a.gcf00c = z.gaz02c and z.gaz01c = @compid group by gcf00c ---第三季度前期卡 insert #customexpired(compid, quarter, cardType, balancescope, personcount, balance, scopeorde) select gcf00c, 3, 1, cast(@tempbalance as varchar(10)) + '-' + cast(@balanceto as varchar(10)), count(*), isnull(sum(isnull(gcf10f, 0)), 0), @tempscopeorde from #gcm06 a, gam26 z with (nolock) where isnull(a.gcf20i, 0) > 0 and isnull(a.gcf12d, '') >= (@yyear + '0701') and isnull(a.gcf12d, '') <= (@yyear + '0930') and a.gcf10f >= @tempbalance and a.gcf10f <= @balanceto and a.gcf00c = z.gaz02c and z.gaz01c = @compid group by gcf00c ---第四季度当期卡 insert #customexpired(compid, quarter, cardType, balancescope, personcount, balance, scopeorde) select gcf00c, 4, 0, cast(@tempbalance as varchar(10)) + '-' + cast(@balanceto as varchar(10)), count(*), isnull(sum(isnull(gcf10f, 0)), 0), @tempscopeorde from #gcm06 a, gam26 z with (nolock) where isnull(a.gcf20i, 0) = 0 and isnull(a.gcf12d, '') >= (@yyear + '1001') and isnull(a.gcf12d, '') <= (@yyear + '1231') and a.gcf10f >= @tempbalance and a.gcf10f <= @balanceto and a.gcf00c = z.gaz02c and z.gaz01c = @compid group by gcf00c ---第四季度前期卡 insert #customexpired(compid, quarter, cardType, balancescope, personcount, balance, scopeorde) select gcf00c, 4, 1, cast(@tempbalance as varchar(10)) + '-' + cast(@balanceto as varchar(10)), count(*), isnull(sum(isnull(gcf10f, 0)), 0), @tempscopeorde from #gcm06 a, gam26 z with (nolock) where isnull(a.gcf20i, 0) > 0 and isnull(a.gcf12d, '') >= (@yyear + '1001') and isnull(a.gcf12d, '') <= (@yyear + '1231') and a.gcf10f >= @tempbalance and a.gcf10f <= @balanceto and a.gcf00c = z.gaz02c and z.gaz01c = @compid group by gcf00c ---年度合计当期卡 insert #customexpired(compid, quarter, cardType, balancescope, personcount, balance, scopeorde) select gcf00c, 5, 0, cast(@tempbalance as varchar(10)) + '-' + cast(@balanceto as varchar(10)), count(*), isnull(sum(isnull(gcf10f, 0)), 0), @tempscopeorde from #gcm06 a, gam26 z with (nolock) where isnull(a.gcf20i, 0) = 0 and a.gcf10f >= @tempbalance and a.gcf10f <= @balanceto and a.gcf00c = z.gaz02c and z.gaz01c = @compid group by gcf00c ---年度合计前期卡 insert #customexpired(compid, quarter, cardType, balancescope, personcount, balance, scopeorde) select gcf00c, 5, 1, cast(@tempbalance as varchar(10)) + '-' + cast(@balanceto as varchar(10)), count(*), isnull(sum(isnull(gcf10f, 0)), 0), @tempscopeorde from #gcm06 a, gam26 z with (nolock) where isnull(a.gcf20i, 0) >0 and a.gcf10f >= @tempbalance and a.gcf10f <= @balanceto and a.gcf00c = z.gaz02c and z.gaz01c = @compid group by gcf00c set @tempbalance = @balancesplit + @tempbalance end end -------------大于结余范围------------------- ---第一季度当期卡 insert #customexpired(compid, quarter, cardType, balancescope, personcount, balance, scopeorde) select gcf00c, 1, 0, cast(@balanceto as varchar(10)) + '以上', count(*), isnull(sum(isnull(gcf10f, 0)), 0), @tempscopeorde from #gcm06 a, gam26 z with (nolock) where isnull(a.gcf20i, 0) = 0 and isnull(a.gcf12d, '') >= (@yyear + '0101') and isnull(a.gcf12d, '') <= (@yyear + '0331') and a.gcf10f >= @balanceto and a.gcf00c = z.gaz02c and z.gaz01c = @compid group by gcf00c ---第一季度前期卡 insert #customexpired(compid, quarter, cardType, balancescope, personcount, balance, scopeorde) select gcf00c, 1, 1, cast(@tempbalance as varchar(10)) + '-' + cast(@balanceto as varchar(10)), count(*), isnull(sum(isnull(gcf10f, 0)), 0), @tempscopeorde from #gcm06 a, gam26 z with (nolock) where isnull(a.gcf20i, 0) >0 and isnull(a.gcf12d, '') >= (@yyear + '0101') and isnull(a.gcf12d, '') <= (@yyear + '0331') and a.gcf10f >= @balanceto and a.gcf00c = z.gaz02c and z.gaz01c = @compid group by gcf00c ---第二季度当期卡 insert #customexpired(compid, quarter, cardType, balancescope, personcount, balance, scopeorde) select gcf00c, 2, 0, cast(@tempbalance as varchar(10)) + '-' + cast(@balanceto as varchar(10)), count(*), isnull(sum(isnull(gcf10f, 0)), 0), @tempscopeorde from #gcm06 a, gam26 z with (nolock) where isnull(a.gcf20i, 0) = 0 and isnull(a.gcf12d, '') >= (@yyear + '0401') and isnull(a.gcf12d, '') <= (@yyear + '0630') and a.gcf10f >= @balanceto and a.gcf00c = z.gaz02c and z.gaz01c = @compid group by gcf00c ---第二季度前期卡 insert #customexpired(compid, quarter, cardType, balancescope, personcount, balance, scopeorde) select gcf00c, 2, 1, cast(@tempbalance as varchar(10)) + '-' + cast(@balanceto as varchar(10)), count(*), isnull(sum(isnull(gcf10f, 0)), 0), @tempscopeorde + 1 from #gcm06 a, gam26 z with (nolock) where isnull(a.gcf20i, 0) >0 and isnull(a.gcf12d, '') >= (@yyear + '0401') and isnull(a.gcf12d, '') <= (@yyear + '0630') and a.gcf10f >= @balanceto and a.gcf00c = z.gaz02c and z.gaz01c = @compid group by gcf00c ---第三季度当期卡 insert #customexpired(compid, quarter, cardType, balancescope, personcount, balance, scopeorde) select gcf00c, 3, 0, cast(@tempbalance as varchar(10)) + '-' + cast(@balanceto as varchar(10)), count(*), isnull(sum(isnull(gcf10f, 0)), 0), @tempscopeorde + 1 from #gcm06 a, gam26 z with (nolock) where isnull(a.gcf20i, 0) = 0 and isnull(a.gcf12d, '') >= (@yyear + '0701') and isnull(a.gcf12d, '') <= (@yyear + '0930') and a.gcf10f >= @balanceto and a.gcf00c = z.gaz02c and z.gaz01c = @compid group by gcf00c ---第三季度前期卡 insert #customexpired(compid, quarter, cardType, balancescope, personcount, balance, scopeorde) select gcf00c, 3, 1, cast(@tempbalance as varchar(10)) + '-' + cast(@balanceto as varchar(10)), count(*), isnull(sum(isnull(gcf10f, 0)), 0), @tempscopeorde + 1 from #gcm06 a, gam26 z with (nolock) where isnull(a.gcf20i, 0) > 0 and isnull(a.gcf12d, '') >= (@yyear + '0701') and isnull(a.gcf12d, '') <= (@yyear + '0930') and a.gcf10f >= @balanceto and a.gcf00c = z.gaz02c and z.gaz01c = @compid group by gcf00c ---第四季度当期卡 insert #customexpired(compid, quarter, cardType, balancescope, personcount, balance, scopeorde) select gcf00c, 4, 0, cast(@tempbalance as varchar(10)) + '-' + cast(@balanceto as varchar(10)), count(*), isnull(sum(isnull(gcf10f, 0)), 0), @tempscopeorde + 1 from #gcm06 a, gam26 z with (nolock) where isnull(a.gcf20i, 0) = 0 and isnull(a.gcf12d, '') >= (@yyear + '1001') and isnull(a.gcf12d, '') <= (@yyear + '1231') and a.gcf10f >= @balanceto and a.gcf00c = z.gaz02c and z.gaz01c = @compid group by gcf00c ---第四季度前期卡 insert #customexpired(compid, quarter, cardType, balancescope, personcount, balance, scopeorde) select gcf00c, 4, 1, cast(@tempbalance as varchar(10)) + '-' + cast(@balanceto as varchar(10)), count(*), isnull(sum(isnull(gcf10f, 0)), 0), @tempscopeorde + 1 from #gcm06 a, gam26 z with (nolock) where isnull(a.gcf20i, 0) > 0 and isnull(a.gcf12d, '') >= (@yyear + '1001') and isnull(a.gcf12d, '') <= (@yyear + '1231') and a.gcf10f >= @balanceto and a.gcf00c = z.gaz02c and z.gaz01c = @compid group by gcf00c ---年度合计当期卡 insert #customexpired(compid, quarter, cardType, balancescope, personcount, balance, scopeorde) select gcf00c, 5, 0, cast(@tempbalance as varchar(10)) + '-' + cast(@balanceto as varchar(10)), count(*), isnull(sum(isnull(gcf10f, 0)), 0), @tempscopeorde + 1 from #gcm06 a, gam26 z with (nolock) where isnull(a.gcf20i, 0) = 0 and a.gcf10f >= @balanceto and a.gcf00c = z.gaz02c and z.gaz01c = @compid group by gcf00c ---年度合计前期卡 insert #customexpired(compid, quarter, cardType, balancescope, personcount, balance, scopeorde) select gcf00c, 5, 1, cast(@tempbalance as varchar(10)) + '-' + cast(@balanceto as varchar(10)), count(*), isnull(sum(isnull(gcf10f, 0)), 0), @tempscopeorde + 1 from #gcm06 a, gam26 z with (nolock) where isnull(a.gcf20i, 0) > 0 and a.gcf10f >= @balanceto and a.gcf00c = z.gaz02c and z.gaz01c = @compid group by gcf00c create table #result ( balancescope varchar(20) null, quarter int null, scopeindex int null ) declare @nClassCount int declare @tempNum int declare @count int declare @fromvalue float declare @tovalue float set @fromvalue = @balancefrom set @tovalue = @balanceto set @count = 0 set @nClassCount = (@balanceto-@balancefrom)/@balancesplit set @tempNum = (cast((@balanceto - @balancefrom) as int))% (cast(@balancesplit as int)) if (@tempNum!= 0) begin set @nClassCount = @nClassCount + 1; end while(@count< @nClassCount) begin if ((@fromvalue + @balancesplit) <= @balanceto) begin set @count = @count + 1 insert #result(balancescope, quarter, scopeindex) values(cast(@fromvalue as varchar(10)) + '-' + cast((@balancesplit + @fromvalue) as varchar(20)), 1, @count) insert #result(balancescope, quarter, scopeindex) values(cast(@fromvalue as varchar(10)) + '-' + cast((@balancesplit + @fromvalue) as varchar(20)), 2, @count) insert #result(balancescope, quarter, scopeindex) values(cast(@fromvalue as varchar(10)) + '-' + cast((@balancesplit + @fromvalue) as varchar(20)), 3, @count) insert #result(balancescope, quarter, scopeindex) values(cast(@fromvalue as varchar(10)) + '-' + cast((@balancesplit + @fromvalue) as varchar(20)), 4, @count) insert #result(balancescope, quarter, scopeindex) values(cast(@fromvalue as varchar(10)) + '-' + cast((@balancesplit + @fromvalue) as varchar(20)), 5, @count) set @fromvalue = @balancesplit + @fromvalue end else begin set @count = @count + 1 insert #result(balancescope, quarter, scopeindex) values(cast(@fromvalue as varchar(10)) + '-' + cast(@balanceto as varchar(20)), 1, @count) insert #result(balancescope, quarter, scopeindex) values(cast(@fromvalue as varchar(10)) + '-' + cast(@balanceto as varchar(20)), 2, @count) insert #result(balancescope, quarter, scopeindex) values(cast(@fromvalue as varchar(10)) + '-' + cast(@balanceto as varchar(20)), 3, @count) insert #result(balancescope, quarter, scopeindex) values(cast(@fromvalue as varchar(10)) + '-' + cast(@balanceto as varchar(20)), 4, @count) insert #result(balancescope, quarter, scopeindex) values(cast(@fromvalue as varchar(10)) + '-' + cast(@balanceto as varchar(20)), 5, @count) set @fromvalue = @balancesplit + @fromvalue end end -------------大于结余范围------------------- set @count = @count + 1 insert #result(balancescope, quarter, scopeindex) values(cast(@balanceto as varchar(10)) + '以上', 1, @count) insert #result(balancescope, quarter, scopeindex) values(cast(@balanceto as varchar(10)) + '以上', 2, @count) insert #result(balancescope, quarter, scopeindex) values(cast(@balanceto as varchar(10)) + '以上', 3, @count) insert #result(balancescope, quarter, scopeindex) values(cast(@balanceto as varchar(10)) + '以上', 4, @count) insert #result(balancescope, quarter, scopeindex) values(cast(@balanceto as varchar(10)) + '以上', 5, @count) delete a from #result a, #customexpired b where a.balancescope = b.balancescope and a.quarter = b.quarter insert #customexpired(compid, quarter, cardType, balancescope, personcount, balance, scopeorde) select @compid, quarter, 0, b.balancescope, 0, 0, b.scopeindex from #result b insert #customexpired(compid, quarter, cardType, balancescope, personcount, balance, scopeorde) select @compid, quarter, 1, b.balancescope, 0, 0, b.scopeindex from #result b create table #customexpiredrestult ( compid varchar(20) null, quarter varchar(20) null, col1 float null, col2 float null, col3 float null, col4 float null, col5 float null, col6 float null, col7 float null, col8 float null, col9 float null, col10 float null, col11 float null, col12 float null, col13 float null, col14 float null, col15 float null, col16 float null, col17 float null, col18 float null, col19 float null, col20 float null, col21 float null, col22 float null, col23 float null, col24 float null, col25 float null, col26 float null, col27 float null, col28 float null, col29 float null, col30 float null, col31 float null, col32 float null, col33 float null, col34 float null, col35 float null, col36 float null, col37 float null, col38 float null, col39 float null, col40 float null, col41 float null, col42 float null, col43 float null, col44 float null, ) --select * from #customexpired insert #customexpiredrestult(compid, quarter) select compid, quarter from #customexpired group by compid, quarter ----------------#customexpired更新到#customexpiredrestult update #customexpiredrestult set col1 = personcount from #customexpired a, #customexpiredrestult b where a.compid = b.compid and a.quarter = b.quarter and a.scopeorde = 1 and a.cardType = 0 update #customexpiredrestult set col2 = personcount from #customexpired a, #customexpiredrestult b where a.compid = b.compid and a.quarter = b.quarter and a.scopeorde = 1 and a.cardType = 1 update #customexpiredrestult set col3 = balance from #customexpired a, #customexpiredrestult b where a.compid = b.compid and a.quarter = b.quarter and a.scopeorde = 1 and a.cardType = 0 update #customexpiredrestult set col4 = balance from #customexpired a, #customexpiredrestult b where a.compid = b.compid and a.quarter = b.quarter and a.scopeorde = 1 and a.cardType = 1 update #customexpiredrestult set col5 = personcount from #customexpired a, #customexpiredrestult b where a.compid = b.compid and a.quarter = b.quarter and a.scopeorde = 2 and a.cardType = 0 update #customexpiredrestult set col6 = personcount from #customexpired a, #customexpiredrestult b where a.compid = b.compid and a.quarter = b.quarter and a.scopeorde = 2 and a.cardType = 1 update #customexpiredrestult set col7 = balance from #customexpired a, #customexpiredrestult b where a.compid = b.compid and a.quarter = b.quarter and a.scopeorde = 2 and a.cardType = 0 update #customexpiredrestult set col8 = balance from #customexpired a, #customexpiredrestult b where a.compid = b.compid and a.quarter = b.quarter and a.scopeorde = 2 and a.cardType = 1 update #customexpiredrestult set col9 = personcount from #customexpired a, #customexpiredrestult b where a.compid = b.compid and a.quarter = b.quarter and a.scopeorde = 3 and a.cardType = 0 update #customexpiredrestult set col10 = personcount from #customexpired a, #customexpiredrestult b where a.compid = b.compid and a.quarter = b.quarter and a.scopeorde = 3 and a.cardType = 1 update #customexpiredrestult set col11 = balance from #customexpired a, #customexpiredrestult b where a.compid = b.compid and a.quarter = b.quarter and a.scopeorde = 3 and a.cardType = 0 update #customexpiredrestult set col12 = balance from #customexpired a, #customexpiredrestult b where a.compid = b.compid and a.quarter = b.quarter and a.scopeorde = 3 and a.cardType = 1 update #customexpiredrestult set col13 = personcount from #customexpired a, #customexpiredrestult b where a.compid = b.compid and a.quarter = b.quarter and a.scopeorde = 4 and a.cardType = 0 update #customexpiredrestult set col14 = personcount from #customexpired a, #customexpiredrestult b where a.compid = b.compid and a.quarter = b.quarter and a.scopeorde = 4 and a.cardType = 1 update #customexpiredrestult set col15 = balance from #customexpired a, #customexpiredrestult b where a.compid = b.compid and a.quarter = b.quarter and a.scopeorde = 4 and a.cardType = 0 update #customexpiredrestult set col16 = balance from #customexpired a, #customexpiredrestult b where a.compid = b.compid and a.quarter = b.quarter and a.scopeorde = 4 and a.cardType = 1 update #customexpiredrestult set col17 = personcount from #customexpired a, #customexpiredrestult b where a.compid = b.compid and a.quarter = b.quarter and a.scopeorde = 5 and a.cardType = 0 update #customexpiredrestult set col18 = personcount from #customexpired a, #customexpiredrestult b where a.compid = b.compid and a.quarter = b.quarter and a.scopeorde = 5 and a.cardType = 1 update #customexpiredrestult set col19 = balance from #customexpired a, #customexpiredrestult b where a.compid = b.compid and a.quarter = b.quarter and a.scopeorde = 5 and a.cardType = 0 update #customexpiredrestult set col20 = balance from #customexpired a, #customexpiredrestult b where a.compid = b.compid and a.quarter = b.quarter and a.scopeorde = 5 and a.cardType = 1 update #customexpiredrestult set col21 = personcount from #customexpired a, #customexpiredrestult b where a.compid = b.compid and a.quarter = b.quarter and a.scopeorde = 6 and a.cardType = 0 update #customexpiredrestult set col22 = personcount from #customexpired a, #customexpiredrestult b where a.compid = b.compid and a.quarter = b.quarter and a.scopeorde = 6 and a.cardType = 1 update #customexpiredrestult set col23 = balance from #customexpired a, #customexpiredrestult b where a.compid = b.compid and a.quarter = b.quarter and a.scopeorde = 6 and a.cardType = 0 update #customexpiredrestult set col24 = balance from #customexpired a, #customexpiredrestult b where a.compid = b.compid and a.quarter = b.quarter and a.scopeorde = 6 and a.cardType = 0 update #customexpiredrestult set col25 = personcount from #customexpired a, #customexpiredrestult b where a.compid = b.compid and a.quarter = b.quarter and a.scopeorde = 7 and a.cardType = 0 update #customexpiredrestult set col26 = personcount from #customexpired a, #customexpiredrestult b where a.compid = b.compid and a.quarter = b.quarter and a.scopeorde = 7 and a.cardType = 1 update #customexpiredrestult set col27 = balance from #customexpired a, #customexpiredrestult b where a.compid = b.compid and a.quarter = b.quarter and a.scopeorde = 7 and a.cardType = 0 update #customexpiredrestult set col28 = balance from #customexpired a, #customexpiredrestult b where a.compid = b.compid and a.quarter = b.quarter and a.scopeorde = 7 and a.cardType = 1 update #customexpiredrestult set col29 = personcount from #customexpired a, #customexpiredrestult b where a.compid = b.compid and a.quarter = b.quarter and a.scopeorde = 8 and a.cardType = 0 update #customexpiredrestult set col30 = personcount from #customexpired a, #customexpiredrestult b where a.compid = b.compid and a.quarter = b.quarter and a.scopeorde = 8 and a.cardType = 1 update #customexpiredrestult set col31 = balance from #customexpired a, #customexpiredrestult b where a.compid = b.compid and a.quarter = b.quarter and a.scopeorde = 8 and a.cardType = 0 update #customexpiredrestult set col32 = balance from #customexpired a, #customexpiredrestult b where a.compid = b.compid and a.quarter = b.quarter and a.scopeorde = 8 and a.cardType = 1 update #customexpiredrestult set col33 = personcount from #customexpired a, #customexpiredrestult b where a.compid = b.compid and a.quarter = b.quarter and a.scopeorde = 9 and a.cardType = 0 update #customexpiredrestult set col34 = personcount from #customexpired a, #customexpiredrestult b where a.compid = b.compid and a.quarter = b.quarter and a.scopeorde = 9 and a.cardType = 1 update #customexpiredrestult set col35 = balance from #customexpired a, #customexpiredrestult b where a.compid = b.compid and a.quarter = b.quarter and a.scopeorde = 9 and a.cardType = 0 update #customexpiredrestult set col36 = balance from #customexpired a, #customexpiredrestult b where a.compid = b.compid and a.quarter = b.quarter and a.scopeorde = 9 and a.cardType = 1 update #customexpiredrestult set col37 = personcount from #customexpired a, #customexpiredrestult b where a.compid = b.compid and a.quarter = b.quarter and a.scopeorde = 10 and a.cardType = 0 update #customexpiredrestult set col38 = personcount from #customexpired a, #customexpiredrestult b where a.compid = b.compid and a.quarter = b.quarter and a.scopeorde = 10 and a.cardType = 1 update #customexpiredrestult set col39 = balance from #customexpired a, #customexpiredrestult b where a.compid = b.compid and a.quarter = b.quarter and a.scopeorde = 10 and a.cardType = 0 update #customexpiredrestult set col40 = balance from #customexpired a, #customexpiredrestult b where a.compid = b.compid and a.quarter = b.quarter and a.scopeorde = 10 and a.cardType = 1 update #customexpiredrestult set col41 = personcount from #customexpired a, #customexpiredrestult b where a.compid = b.compid and a.quarter = b.quarter and a.scopeorde = 11 and a.cardType = 0 update #customexpiredrestult set col42 = personcount from #customexpired a, #customexpiredrestult b where a.compid = b.compid and a.quarter = b.quarter and a.scopeorde = 11 and a.cardType = 1 update #customexpiredrestult set col43 = balance from #customexpired a, #customexpiredrestult b where a.compid = b.compid and a.quarter = b.quarter and a.scopeorde = 11 and a.cardType = 0 update #customexpiredrestult set col44 = balance from #customexpired a, #customexpiredrestult b where a.compid = b.compid and a.quarter = b.quarter and a.scopeorde = 11 and a.cardType = 1 --处理非叶子节点的数据, START declare @maxAQ int select @maxAQ = max(gsb02c) from gsm02 with (nolock) where gsb00c = @compid and gsb01c = 'AQ' set @maxAQ = isnull(@maxAQ, 4) declare @each_parent varchar(20) begin create table #used_compid( compid varchar(20) not null, isleaf int null) insert #used_compid(compid, isleaf) select distinct gaz02c, 0 from gam26 with (nolock) where gaz01c = @compid update a set a.isleaf = 1 from #used_compid a, b_perf b with (nolock) where a.compid = b.bran_id and b.lvl = @maxAQ create table #notleaf_sons( parantid varchar(20) not null, sonid varchar(20) not null) insert #notleaf_sons(parantid, sonid) select gaz01c, gaz02c from gam26 z with (nolock), #used_compid b, #used_compid c where z.gaz01c = b.compid and b.isleaf = 0 and gaz01c <> gaz02c and z.gaz02c = c.compid and c.isleaf = 1 declare cur_each_parent_compid cursor for select compid from #used_compid where isleaf = 0 open cur_each_parent_compid fetch cur_each_parent_compid into @each_parent while @@fetch_status = 0 begin delete #customexpiredrestult where compid = @each_parent insert #customexpiredrestult(compid, quarter, col1, col2, col3, col4, col5, col6, col7, col8, col9, col10, col11, col12, col13, col14, col15, col16, col17, col18, col19, col20, col21, col22, col23, col24, col25, col26, col27, col28, col29, col30, col31, col32, col33, col34, col35, col36, col37, col38, col39, col40, col41, col42, col43, col44) select @each_parent, quarter, sum(col1), sum(col2), sum(col3), sum(col4), sum(col5), sum(col6), sum(col7), sum(col8), sum(col9), sum(col10), sum(col11), sum(col12), sum(col13), sum(col14), sum(col15), sum(col16), sum(col17), sum(col18), sum(col19), sum(col20), sum(col21), sum(col22), sum(col23), sum(col24), sum(col25), sum(col26), sum(col27), sum(col28), sum(col29), sum(col30), sum(col31), sum(col32), sum(col33), sum(col34), sum(col35), sum(col36), sum(col37), sum(col38), sum(col39), sum(col40), sum(col41), sum(col42), sum(col43), sum(col44) from #customexpiredrestult a, #notleaf_sons b where a.compid = b.sonid and b.parantid = @each_parent group by quarter fetch cur_each_parent_compid into @each_parent end close cur_each_parent_compid deallocate cur_each_parent_compid end --处理非叶子节点的数据, end select isnull(compid,'') compid,isnull(quarter,'') quarter,isnull(col1,0) col1,isnull(col2,0) col2, isnull(col3,0) col3, isnull(col4,0) col4, isnull(col5,0) col5, isnull(col6,0) col6, isnull(col7,0) col7, isnull(col8,0) col8, isnull(col9,0) col9, isnull(col10,0) col10, isnull(col11,0) col11, isnull(col12,0) col12, isnull(col13,0) col13, isnull(col14,0) col14, isnull(col15,0) col15, isnull(col16,0) col16, isnull(col17,0) col17, isnull(col18,0) col18, isnull(col19,0) col19, isnull(col20,0) col20, isnull(col21,0) col21, isnull(col22,0) col22, isnull(col23,0) col23, isnull(col24,0) col24, isnull(col25,0) col25, isnull(col26,0) col26, isnull(col27,0) col27, isnull(col28,0) col28, isnull(col29,0) col29, isnull(col30,0) col30, isnull(col31,0) col31, isnull(col32,0) col32, isnull(col33,0) col33, isnull(col34,0) col34, isnull(col35,0) col35, isnull(col36,0) col36, isnull(col37,0) col37, isnull(col38,0) col38, isnull(col39,0) col39, isnull(col40,0) col40, isnull(col41,0) col41, isnull(col42,0) col42, isnull(col43,0) col43, isnull(col44,0) col44 from #customexpiredrestult order by compid, quarter drop table #customexpired drop table #gcm06 drop table #customexpiredrestult end GO /****** Object: StoredProcedure [dbo].[upg_stores_customerstat] Script Date: 06/12/2017 06:20:45 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_stores_customerstat] ( @compid varchar(10), -- 已经能支持各级别的公司 @fromdate varchar(10), -- 开始日期 @todate varchar(10) -- 结束日期 ) as begin create table #customerstat -- 各级别顾客统计表 ( compid varchar(10) not null, -- 公司别 compname varchar(40) null, -- 公司名称 cardtype varchar(20) null, -- 会员卡类别 cardname varchar(40) null, -- 会员卡类别名称 customercount float null, -- 顾客人数 times3ofcustomer float null, -- 来店3次的人数 customerpercent float null, -- 顾客所占百分比 ) create table #cardcustomers --卡消费历史表 ( compid varchar(10) not null, -- 公司别 cardid varchar(20) null, -- 会员卡编号 cardtype varchar(20) null, -- 消费卡号 date varchar(20) null , num int null, ) create table #tbl_card_num( compid varchar(20) not null, cardtype varchar(20) not null, num int null) insert #tbl_card_num(compid, cardtype, num) select gca00c, gca02c, count(*) from gcm01 a with (nolock), gam26 z with (nolock) where gca00c = gca13d and gca00c = z.gaz02c and z.gaz01c = @compid group by gca00c, gca02c ----------------添加本门店的卡在本门店的消费记录-------------------------- insert #cardcustomers(compid, cardid, cardtype, date) select a.gct00c, a.gct02c, b.gca02c, gct04d from gcm20 a with (nolock), gcm01 b with (nolock), gam26 z with (nolock) where a.gct00c = b.gca13d and a.gct02c = b.gca01c and a.gct00c = z.gaz02c and z.gaz01c = @compid and gct04d between @fromdate and @todate group by gct00c, gct02c, gct04d, gca02c order by gct00c, gca02c ------------------添加个级别卡所有有的卡类别--------------------------- insert #customerstat(compid, cardtype, cardname) select gak00c, gak01c, gak02c from gam10 a with (nolock), gam26 z with (nolock) where a.gak00c = z.gaz02c and z.gaz01c = @compid --更新每个类别的客户会员卡的数量 update a set a.customercount = b.num from #customerstat a, #tbl_card_num b where a.compid = b.compid and a.cardtype = b.cardtype drop table #tbl_card_num -----------------------更新某张卡在本店消费的次数--------------------------- update a set a.num = (select count(*) from #cardcustomers b where a.cardid = b.cardid and a.compid = b.compid ) from #cardcustomers a ----------------------更新所在店拥有的卡在本店消费的次数-------------------- update a set a.times3ofcustomer = (select count(distinct cardid) from #cardcustomers b where a.compid = b.compid and a.cardtype = b.cardtype and b.num>3) from #customerstat a --处理非叶子节点的数据 declare @maxAQ int select @maxAQ = max(gsb02c) from gsm02 with (nolock) where gsb00c = @compid and gsb01c = 'AQ' set @maxAQ = isnull(@maxAQ, 4) declare @each_parent varchar(20) begin create table #used_compid( compid varchar(20) not null, isleaf int null) insert #used_compid(compid, isleaf) select distinct gaz02c, 0 from gam26 with (nolock) where gaz01c = @compid update a set a.isleaf = 1 from #used_compid a, b_perf b with (nolock) where a.compid = b.bran_id and b.lvl = @maxAQ create table #notleaf_sons( parantid varchar(20) not null, sonid varchar(20) not null) insert #notleaf_sons(parantid, sonid) select gaz01c, gaz02c from gam26 z with (nolock), #used_compid b, #used_compid c where z.gaz01c = b.compid and b.isleaf = 0 and gaz01c <> gaz02c and z.gaz02c = c.compid and c.isleaf = 1 declare cur_each_parent_compid cursor for select compid from #used_compid where isleaf = 0 open cur_each_parent_compid fetch cur_each_parent_compid into @each_parent while @@fetch_status = 0 begin delete #customerstat where compid = @each_parent insert #customerstat(compid, compname, cardtype, cardname, customercount, times3ofcustomer) select @each_parent, '', cardtype, cardname, sum(customercount), sum(times3ofcustomer) from #customerstat a, #notleaf_sons b where a.compid = b.sonid and b.parantid = @each_parent group by cardtype, cardname fetch cur_each_parent_compid into @each_parent end close cur_each_parent_compid deallocate cur_each_parent_compid end ----------------------更新公司别所对应的公司名称------------------------------ update #customerstat set compname = (select top 1 gae03c from gam05 with (nolock) where gae01c = compid) -----------------------更新消费超过3次的卡在所有卡中的百分比----------------------------------- update a set a.customerpercent = case when isnull(customercount, 0) = 0 then 0 else isnull(times3ofcustomer, 0)/isnull(customercount, 0)*100 end from #customerstat a --select * from #customerstat order by compid, cardtype select isnull(compid,'') compid, isnull(compname,'') compname, isnull(cardtype,'') cardtype, isnull(cardname,'') cardname, isnull(customercount,0) customercount, isnull(times3ofcustomer,0) times3ofcustomer, isnull(customerpercent,0) customerpercent from #customerstat order by compid, cardtype delete #customerstat delete #cardcustomers drop table #cardcustomers drop table #customerstat end GO /****** Object: StoredProcedure [dbo].[upg_card_specialoperation] Script Date: 06/12/2017 06:19:37 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_card_specialoperation] ( @compid varchar(10), -- 公司别 @fromdate varchar(10), --开始日期 @todate varchar(10) --结束日期 ) as begin create table #gcm10_buf -- 帐户异动单 ( gcl00c varchar(10) not null, --公司编号 gcl01c varchar(20) not null, --异动单号 gcl02d varchar(8) not null, --异动日期 gcl03c varchar(20) null, --会员卡号 gcl04c varchar(5) null, --帐号类别(1.电子钱包 2.储值帐户) gcl05c varchar(20) null, --是否为重新购卡 1 gcl06i int null, --异动类别( 0充值, 1取款 5欠款 6还款) gcl07f float null, --异动金额(总收款金额), 含套餐,含疗程 gcl08f float null, --异动前余额 gcl14i int null, --是否升级会员卡类别 gcl15c varchar(5) null, --新的会员卡类别 gcl52c varchar(20) null, --升级前会员卡类别 gcl53f float null, --储值应收金额 gcl80d varchar(8) null, --帐务日期 added by MZH, 2005/08/16 gcl67c varchar(5) null, --支付方式1 gcl68f float null, --支付方式1金额 gcl69c varchar(5) null, --支付方式2 gcl70f float null, --支付方式2金额 gcl71c varchar(5) null, --支付方式3 gcl72f float null, --支付方式3金额 gcl73c varchar(5) null, --支付方式4 gcl74f float null, --支付方式4金额 gcl99i int null, --单据类型: 1售前,2售后,3嘉宾 ) declare @PK_Sql nvarchar(max) set @PK_Sql = ' alter table #gcm10_buf add constraint PK_#gcm10_buf_' + convert(varchar(10), cast(floor(rand() * 1000000) as int)) + ' PRIMARY KEY NonCLUSTERED(gcl00c, gcl01c)' exec sp_executesql @PK_Sql insert #gcm10_buf(gcl00c, gcl01c, gcl02d, gcl03c, gcl04c, gcl05c, gcl06i, gcl07f, gcl08f, gcl14i, gcl15c, gcl52c, gcl53f, gcl80d, gcl67c, gcl68f, gcl69c, gcl70f, gcl71c, gcl72f, gcl73c, gcl74f, gcl99i) select gcl00c, gcl01c, gcl02d, gcl03c, gcl04c, gcl05c, gcl06i, gcl07f, gcl08f, gcl14i, gcl15c, gcl52c, gcl53f, gcl80d, gcl67c, gcl68f, gcl69c, gcl70f, gcl71c, gcl72f, gcl73c, gcl74f, gcl99i from gcm10 a with (nolock), gam26 z where a.gcl00c = z.gaz02c and z.gaz01c = @compid and a.gcl80d >= @fromdate and a.gcl80d <= @todate create nonclustered index idx_gcm10_buf on #gcm10_buf(gcl00c, gcl01c, gcl04c) create table #hem08 --疗程更换的疗程记录 ( hei00c varchar(10) not null, --公司 hei01c varchar(20) null, --单号 hei02c varchar(20) null, --卡号 hei03d varchar(8) null, --日期 hei04c varchar(20) null, --疗程换疗程疗程的编号 hei05f float null, --疗程换疗程的实际更换的金额 hei06f float null, --疗程换疗程实际更换的次数 hei07f float null, --当时更换金额 hei08f float null, --当时更换单价 hei09f float null, --当时的剩余金额 hei10f float null, --当时的剩余次数 hei11i int null, --价格序号 hei12f float null, --折扣 hei13i int null --疗程序号 ) create clustered index idx_tmp_hem08_1 on #hem08(hei00c, hei02c, hei13i) create nonclustered index idx_tmp_hem08_2 on #hem08(hei00c, hei01c) create table #gcm08a -- 会员卡--疗程变化历史(用于续办卡) ( gch00c varchar(10) not null, --公司编号 gch01i int identity not null, -- 序号 gch02d varchar(8) null, -- 日期 gch03c varchar(20) null, --卡号 gch04c varchar(20) null, --疗程项目编号 gch05f float null, --购买次数 gch06f float null, --赠送次数 gch07f float null, --购买金额 gch08f float null, --使用次数 gch09f float null, --已经使用金额 gch10c varchar(20) null, --单据类别 gch11c varchar(20) null, --单号 gch12i int null, --价格序号 gch13f float null, --折扣 gch14f float null, --购买数量 gch15f float null, --单价 gch16i int null --疗程中的序号 ) create clustered index idx_tmp_gcm08a_1 on #gcm08a(gch00c, gch03c, gch04c) create table #datalist ( iid int identity not null, --序号 compid varchar(20) null, --公司 cardtype int null, --统计的卡类型(1前期卡抵产品, 2前期卡抵卡, 3当期卡抵产品, 4当期卡抵卡, 5升卡, 6续办卡, 7新办卡) numbertotal int null, --统计张数 moneytotal float null, --统计金额 ) create table #result ( iid int identity not null, --序号 res00c varchar(20) null, --公司 res01i float null, --前期卡抵产品(张数) res02f float null, --前期卡抵产品(金额) res03i float null, --前期卡抵卡(张数) res04f float null, --前期卡抵卡(金额) res05i float null, --当期卡抵产品(张数) res06f float null, --当期卡抵产品(金额) res07i float null, --当期卡抵卡(张数) res08f float null, --当期卡抵卡(金额) res09i float null, --升卡(张数) res10f float null, --升卡(金额) res11i float null, --续办卡(张数) res12f float null, --续办卡(金额) res13i float null, --新办卡(张数) res14f float null, --新办卡(金额) ) --所有的抵卡记录 insert #hem08(hei00c, hei01c, hei02c, hei03d, hei04c, hei05f, hei06f, hei07f, hei08f, hei09f, hei10f, hei11i, hei12f, hei13i) select hei00c, hei01c, hei02c, hei03d, hei04c, hei05f, hei06f, hei07f, hei08f, hei09f, hei10f, hei11i, hei12f, hei13i from hem08 a, gam26 z where a.hei03d >= @fromdate and a.hei03d <= @todate and a.hei00c = z.gaz02c and z.gaz01c = @compid --新办卡与续办卡的记录 insert #gcm08a(gch00c, gch02d, gch03c, gch04c, gch05f, gch06f, gch07f, gch08f, gch09f, gch10c, gch11c, gch12i, gch13f, gch14f, gch15f, gch16i) select gch00c, gch02d, gch03c, gch04c, gch05f, gch06f, gch07f, gch08f, gch09f, gch10c, gch11c, gch12i, gch13f, gch14f, gch15f, gch16i from gcm08 a with (nolock), gam26 z where a.gch02d >= @fromdate and a.gch02d <= @todate and (a.gch10c = 'gn' or a.gch10c = 'gz') and a.gch00c = z.gaz02c and z.gaz01c = @compid ---前期卡抵产品-------------- insert #datalist(compid, cardtype, numbertotal, moneytotal) select a.hei00c, 1, count(*), sum(isnull(hei07f, 0)) from #hem08 a, gcm06 b with (nolock), #gcm10_buf c where a.hei00c = b.gcf00c and a.hei02c = b.gcf01c and a.hei13i = b.gcf23i and isnull(b.gcf20i, 0)>0 and a.hei00c = c.gcl00c and a.hei01c = c.gcl01c and c.gcl04c = '9' group by a.hei00c ---前期卡抵卡-------------- insert #datalist(compid, cardtype, numbertotal, moneytotal) select a.hei00c, 2, count(*), sum(isnull(hei07f, 0)) from #hem08 a, gcm06 b with (nolock), #gcm10_buf c where a.hei00c = b.gcf00c and a.hei02c = b.gcf01c and a.hei13i = b.gcf23i and isnull(b.gcf20i, 0)>0 and a.hei00c = c.gcl00c and a.hei01c = c.gcl01c and c.gcl04c<>'9' group by a.hei00c ---当期卡抵产品-------------- insert #datalist(compid, cardtype, numbertotal, moneytotal) select a.hei00c, 3, count(*), sum(isnull(hei07f, 0)) from #hem08 a, gcm06 b with (nolock), #gcm10_buf c where a.hei00c = b.gcf00c and a.hei02c = b.gcf01c and a.hei13i = b.gcf23i and isnull(b.gcf20i, 0) = 0 and a.hei00c = c.gcl00c and a.hei01c = c.gcl01c and c.gcl04c = '9' group by a.hei00c ---当期卡抵卡-------------- insert #datalist(compid, cardtype, numbertotal, moneytotal) select a.hei00c, 4, count(*), sum(isnull(hei07f, 0)) from #hem08 a, gcm06 b with (nolock), #gcm10_buf c where a.hei00c = b.gcf00c and a.hei02c = b.gcf01c and a.hei13i = b.gcf23i and isnull(b.gcf20i, 0) = 0 and a.hei00c = c.gcl00c and a.hei01c = c.gcl01c and c.gcl04c<>'9' group by a.hei00c ---升卡-------------- create table #tbl_card_upgrade_bills( compid varchar(20) not null, billid varchar(20) not null, cardid varchar(20) not null, primary key(compid, billid, cardid)) insert #tbl_card_upgrade_bills(compid, billid, cardid) select distinct compid = gcb00c, billid = gcb04c, cardid = gcb01c from gcm02 a, gam26 z where a.gcb00c = z.gaz02c and z.gaz01c = @compid and a.gcb03i = 11 and a.gcb07d >= @fromdate and a.gcb07d <= @todate --只统计金额 insert #datalist(compid, cardtype, numbertotal, moneytotal) select gcl00c, 5, 0, moneytotal = sum(gsc05f) from #gcm10_buf a, #tbl_card_upgrade_bills b, gsm03 c where a.gcl00c = b.compid and a.gcl01c = b.billid and a.gcl03c = b.cardid and a.gcl00c = c.gsc00c and a.gcl01c = c.gsc01c and c.gsc02c = 'gz' and (c.gsc04c = '1' or c.gsc04c = '6') group by gcl00c --再统计升卡的张数 create table #tbl_upgrade_num( compid varchar(20) not null, totalnum float null) insert #tbl_upgrade_num(compid, totalnum) select gcl00c, count(*) from #gcm10_buf a, #tbl_card_upgrade_bills b where a.gcl00c = b.compid and a.gcl01c = b.billid and a.gcl03c = b.cardid group by gcl00c update a set a.numbertotal = b.totalnum from #datalist a, #tbl_upgrade_num b where a.compid = b.compid and a.cardtype = 5 drop table #tbl_upgrade_num drop table #tbl_card_upgrade_bills ---新办卡-------------- --统计出来所有只有一次的卡项目 create table #tbl_card_prj_num( cardid varchar(20) not null, prjid varchar(20) not null, num int null, min int null, primary key(cardid, prjid)) insert #tbl_card_prj_num(cardid, prjid, num, min) select gch03c, gch04c, count(*), min(gch01i) from #gcm08a group by gch03c, gch04c insert #datalist(compid, cardtype, numbertotal, moneytotal) select gch00c, 6, count(*), sum(gch07f) from #gcm08a a, #tbl_card_prj_num b where a.gch03c = b.cardid and a.gch04c = b.prjid and b.num = 1 group by gch00c --把所有只有一次的删除了, 因为已经处理过了 delete #tbl_card_prj_num where num = 1 --把最新的作为新卡 insert #datalist(compid, cardtype, numbertotal, moneytotal) select gch00c, 6, count(*), sum(gch07f) from #gcm08a a, #tbl_card_prj_num b where a.gch03c = b.cardid and a.gch04c = b.prjid and b.num > 1 and b.min = a.gch01i group by gch00c --把最新的删除 delete a from #tbl_card_prj_num a, #gcm08a b where a.cardid = b.gch03c and a.prjid = b.gch04c and a.min = b.gch01i drop table #tbl_card_prj_num --剩下的就是续卡的 insert #datalist(compid, cardtype, numbertotal, moneytotal) select gch00c, 7, count(*), sum(gch07f) from #gcm08a a group by gch00c ------------转换格式------------- insert #result(res00c) select compid from #datalist group by compid --前期卡抵产品 update #result set res01i = isnull(numbertotal, 0) from #datalist where compid = res00c and cardtype = 1 --前期卡抵卡 update #result set res02f = isnull(moneytotal, 0) from #datalist where compid = res00c and cardtype = 1 update #result set res03i = isnull(numbertotal, 0) from #datalist where compid = res00c and cardtype = 2 update #result set res04f = isnull(moneytotal, 0) from #datalist where compid = res00c and cardtype = 2 update #result set res05i = isnull(numbertotal, 0) from #datalist where compid = res00c and cardtype = 3 update #result set res06f = isnull(moneytotal, 0) from #datalist where compid = res00c and cardtype = 3 update #result set res07i = isnull(numbertotal, 0) from #datalist where compid = res00c and cardtype = 4 update #result set res08f = isnull(moneytotal, 0) from #datalist where compid = res00c and cardtype = 4 update #result set res09i = isnull(numbertotal, 0) from #datalist where compid = res00c and cardtype = 5 update #result set res10f = isnull(moneytotal, 0) from #datalist where compid = res00c and cardtype = 5 update #result set res11i = isnull(numbertotal, 0) from #datalist where compid = res00c and cardtype = 6 update #result set res12f = isnull(moneytotal, 0) from #datalist where compid = res00c and cardtype = 6 update #result set res13i = isnull(numbertotal, 0) from #datalist where compid = res00c and cardtype = 7 update #result set res14f = isnull(moneytotal, 0) from #datalist where compid = res00c and cardtype = 7 --处理非叶子节点的数据, START declare @maxAQ int select @maxAQ = max(gsb02c) from gsm02 where gsb00c = @compid and gsb01c = 'AQ' set @maxAQ = isnull(@maxAQ, 4) declare @each_parent varchar(20) begin create table #used_compid( compid varchar(20) not null, isleaf int null) insert #used_compid(compid, isleaf) select distinct gaz02c, 0 from gam26 where gaz01c = @compid update a set a.isleaf = 1 from #used_compid a, b_perf b where a.compid = b.bran_id and b.lvl = @maxAQ create table #notleaf_sons( parantid varchar(20) not null, sonid varchar(20) not null) insert #notleaf_sons(parantid, sonid) select gaz01c, gaz02c from gam26 z, #used_compid b, #used_compid c where z.gaz01c = b.compid and b.isleaf = 0 and gaz01c <> gaz02c and z.gaz02c = c.compid and c.isleaf = 1 declare cur_each_parent_compid cursor for select compid from #used_compid where isleaf = 0 open cur_each_parent_compid fetch cur_each_parent_compid into @each_parent while @@fetch_status = 0 begin delete #result where res00c = @each_parent insert #result(res00c, res01i, res02f, res03i, res04f, res05i, res06f, res07i, res08f, res09i, res10f, res11i, res12f, res13i, res14f) select @each_parent, sum(res01i), sum(res02f), sum(res03i), sum(res04f), sum(res05i), sum(res06f), sum(res07i), sum(res08f), sum(res09i), sum(res10f), sum(res11i), sum(res12f), sum(res13i), sum(res14f) from #result a, #notleaf_sons b where a.res00c = b.sonid and b.parantid = @each_parent fetch cur_each_parent_compid into @each_parent end close cur_each_parent_compid deallocate cur_each_parent_compid end --处理非叶子节点的数据, end select res00c, isnull(res01i, 0)as res01i, isnull(res02f, 0) as res02f, isnull(res03i, 0) as res03i , isnull(res04f, 0) as res04f, isnull(res05i, 0) as res05i, isnull(res06f, 0) as res06f, isnull(res07i, 0) as res07i , isnull(res08f, 0) as res08f, isnull(res09i, 0) as res09i, isnull(res10f, 0) as res10f, isnull(res11i, 0) as res11i , isnull(res12f, 0) as res12f, isnull(res13i, 0) as res13i, isnull(res14f, 0) as res14f from #result order by res00c drop table #gcm08a drop table #datalist drop table #result drop table #gcm10_buf end GO /****** Object: StoredProcedure [dbo].[upg_ymn_send_stat] Script Date: 06/12/2017 06:20:53 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_ymn_send_stat] ( @compid varchar(10), @fromdate varchar(20), @todate varchar(20) ) as begin create table #tbl_result ( compid varchar(10) not null, compname varchar(50) null, amt_good float null, -- 赠送的产品 amt_item float null, -- 赠送的项目 amt_save float null, -- 增送的储值 amt_coupon float null -- ) insert #tbl_result(compid) select gaz02c from gam26 z with(nolock) where gaz01c = @compid create table #tbl_result_buf ( compid varchar(10) not null, amt float null ) -- 赠送产品 create table #send_good ( compid varchar(10) not null, goodid varchar(20) not null, num float null, amt float null ) create table #gtc10_buf ( gtj00c varchar(10) not null, -- 公司编号 gtj01c varchar(20) not null, -- 类别gn, gz gtj02c varchar(20) not null, -- 单据编号 gtj03c varchar(20) not null, -- 套餐编号 gtj04c varchar(20) not null, -- 产品编号 gtj05f float null, -- 总次数 gtj06f float null, -- 已经使用次数 gtj07f float null, -- 剩余次数 gtj08c varchar(20) null, -- 替换产品编号 ddate varchar(10) null, sendflag int null ) create clustered index idx_gtc10_buf on #gtc10_buf(gtj00c, gtj03c, gtj04c) -- 套餐中赠送的产品 insert #gtc10_buf(gtj00c, gtj01c, gtj02c, gtj03c, gtj04c, gtj05f, gtj06f, gtj07f, gtj08c) select gtj00c, gtj01c, gtj02c, gtj03c, gtj04c, gtj05f, gtj06f, gtj07f, gtj08c from gtc10 a with (nolock), gnm01 b with (nolock), gam26 z with(nolock) where a.gtj01c = 'gn' and a.gtj00c = b.gna00c and a.gtj02c = b.gna01c and b.gna00c = z.gaz02c and z.gaz01c = @compid and b.gna02d >= @fromdate and b.gna02d <= @todate insert #gtc10_buf(gtj00c, gtj01c, gtj02c, gtj03c, gtj04c, gtj05f, gtj06f, gtj07f, gtj08c) select gtj00c, gtj01c, gtj02c, gtj03c, gtj04c, gtj05f, gtj06f, gtj07f, gtj08c from gtc10 a with (nolock), gcm10 b with (nolock), gam26 z with(nolock) where a.gtj01c = 'gz' and a.gtj00c = b.gcl00c and a.gtj02c = b.gcl01c and b.gcl00c = z.gaz02c and z.gaz01c = @compid and b.gcl02d >= @fromdate and b.gcl02d <= @todate update a set a.sendflag = 1 from #gtc10_buf a, gtc03 b with(nolock) where a.gtj00c = b.gtd00c and a.gtj03c = b.gtd01c and a.gtj04c = b.gtd02c and b.gtd04f = 0 and a.gtj04c = a.gtj08c delete #gtc10_buf where isnull(sendflag, 0) = 0 insert #send_good(compid, goodid, num) select gtj00c, gtj04c, sum(gtj05f) from #gtc10_buf group by gtj00c, gtj04c --销售的时候赠送的产品 insert #send_good(compid, goodid, num) select ggc00c, ggc03c, sum(ggc05f) from ggm01 a with (nolock), ggm03 b with (nolock), gam26 z with(nolock) where a.gga00c = b.ggc00c and a.gga01c = b.ggc01c and a.gga00c = z.gaz02c and z.gaz01c = @compid and a.gga02d >= @fromdate and a.gga02d <= @todate and b.ggc27c = '$1' group by ggc00c, ggc03c update a set a.amt = a.num * isnull(b.gfa11f, 0) from #send_good a, gfm01 b with(nolock) where a.compid = b.gfa00c and a.goodid = b.gfa01c insert #tbl_result_buf(compid, amt) select compid, sum(amt) from #send_good group by compid update a set a.amt_good = b.amt from #tbl_result a, #tbl_result_buf b where a.compid = b.compid drop table #gtc10_buf drop table #send_good delete #tbl_result_buf --赠送项目 create table #send_item( compid varchar(10) not null, itemid varchar(20) not null, num float null, amt float null, calflag int null) --0 没有计算 --疗程购买的赠送部分 create table #send_proc( compid varchar(10) not null, itemid varchar(20) not null, num float null, amt float null, calflag int null) create clustered index idx_send_proc on #send_proc(compid, itemid, num) insert #send_proc(compid, itemid, num) select gch00c, gch04c, gch06f from gcm08 a with (nolock), gam26 z with(nolock) where a.gch00c = z.gaz02c and z.gaz01c = @compid and a.gch02d >= @fromdate and a.gch02d <= @todate and isnull(gch06f, 0)>0 and isnull(gch06f, 0)<100 and (gch10c = 'gz' or gch10c = 'gn') update a set a.amt = (a.num/b.gda28f)*b.gda29f, calflag = 1 from #send_proc a, gdm01 b with(nolock) where a.compid = b.gda00c and a.itemid = b.gda01c and a.num >= isnull(b.gda28f, 0) and isnull(calflag, 0) = 0 and isnull(b.gda28f, 0)>0 update a set a.amt = (a.num/b.gda26f)*b.gda27f, calflag = 1 from #send_proc a, gdm01 b with(nolock) where a.compid = b.gda00c and a.itemid = b.gda01c and a.num >= isnull(b.gda26f, 0) and isnull(calflag, 0) = 0 and isnull(b.gda26f, 0)>0 update a set a.amt = (a.num/b.gda24f)*b.gda25f, calflag = 1 from #send_proc a, gdm01 b with(nolock) where a.compid = b.gda00c and a.itemid = b.gda01c and a.num >= isnull(b.gda24f, 0) and isnull(calflag, 0) = 0 and isnull(b.gda24f, 0)>0 update a set a.amt = (a.num/b.gda22f)*b.gda23f, calflag = 1 from #send_proc a, gdm01 b with(nolock) where a.compid = b.gda00c and a.itemid = b.gda01c and a.num >= isnull(b.gda22f, 0) and isnull(calflag, 0) = 0 and isnull(b.gda22f, 0)>0 update a set a.amt = a.num*b.gda10f, calflag = 1 from #send_proc a, gdm01 b with(nolock) where a.compid = b.gda00c and a.itemid = b.gda01c and isnull(calflag, 0) = 0 insert #send_item(compid, itemid, num, amt, calflag) select compid, itemid, sum(num), sum(amt), 1 from #send_proc group by compid, itemid drop table #send_proc --套餐中赠送项目 create table #gtc09_buf ( gti00c varchar(10) not null, --公司编号 gti01c varchar(20) not null, --类别gn, gz gti02c varchar(20) not null, --卡销售单号 gti03c varchar(20) not null, --套餐编号 gti04c varchar(20) not null, --项目编号 gti05f float null, --项目总次数 gti06f float null, --已经使用次数 gti07f float null, --剩余次数 gti08c varchar(20) null, --主项目 ddate varchar(10) null, sendflag int null ) create clustered index idx_gtc09_buf on #gtc09_buf(gti00c, gti03c, gti04c) insert #gtc09_buf(gti00c, gti01c, gti02c, gti03c, gti04c, gti05f, gti06f, gti07f, gti08c, ddate) select gti00c, gti01c, gti02c, gti03c, gti04c, gti05f, gti06f, gti07f, gti08c, gna02d from gtc09 a with (nolock), gnm01 b with (nolock), gam26 z with(nolock) where a.gti01c = 'gn' and a.gti02c = b.gna01c and a.gti00c = b.gna00c and b.gna02d >= @fromdate and b.gna02d <= @todate and b.gna00c = z.gaz02c and z.gaz01c = @compid insert #gtc09_buf(gti00c, gti01c, gti02c, gti03c, gti04c, gti05f, gti06f, gti07f, gti08c, ddate) select gti00c, gti01c, gti02c, gti03c, gti04c, gti05f, gti06f, gti07f, gti08c, gcl02d from gtc09 a with (nolock), gcm10 b with (nolock), gam26 z with(nolock) where a.gti01c = 'gz' and a.gti02c = b.gcl01c and a.gti00c = b.gcl00c and b.gcl02d >= @fromdate and b.gcl02d <= @todate and b.gcl00c = z.gaz02c and z.gaz01c = @compid --设置赠送项目的标志 update a set a.sendflag = 1 from #gtc09_buf a, gtc02 b with(nolock) where a.gti00c = b.gtb00c and a.gti03c = b.gtb01c and a.gti04c = b.gtb02c and b.gtb04f = 0 and a.gti04c = a.gti08c delete #gtc09_buf where isnull(sendflag, 0) = 0 insert #send_item(compid, itemid, num) select gti00c, gti04c, sum(gti05f) from #gtc09_buf a group by gti00c, gti04c --单独赠送的项目 insert #send_item(compid, itemid, num) select ggb00c, ggb03c, sum(ggb05f) from ggm01 a with (nolock), ggm02 b with (nolock), gam26 z with(nolock) where a.gga00c = b.ggb00c and a.gga01c = b.ggb01c and a.gga02d >= @fromdate and a.gga02d <= @todate and a.gga00c = z.gaz02c and z.gaz01c = @compid and b.ggb27c = '$1' group by ggb00c, ggb03c update a set a.amt = a.num * isnull(b.gda10f, 0) from #send_item a, gdm01 b with(nolock) where a.compid = b.gda00c and a.itemid = b.gda01c and isnull(calflag, 0) = 0 insert #tbl_result_buf(compid, amt) select compid, sum(amt) from #send_item group by compid update a set a.amt_item = b.amt from #tbl_result a, #tbl_result_buf b where a.compid = b.compid delete #tbl_result_buf drop table #send_item drop table #gtc09_buf --赠送划减金额 insert #tbl_result_buf(compid, amt) select gcl00c, sum(gcl68f) from gcm10 a with (nolock), gam26 z with(nolock) where a.gcl00c = z.gaz02c and z.gaz01c = @compid and a.gcl02d >= @fromdate and a.gcl02d <= @todate and (gcl04c <> '4' and gcl04c <> '9') and gcl06i = 0 and gcl67c = '$1' group by gcl00c update a set a.amt_save = b.amt from #tbl_result a, #tbl_result_buf b where a.compid = b.compid delete #tbl_result_buf --赠送地用券 insert #tbl_result_buf(compid, amt) select gah00c, SUM(gah12f) from gam08 a with (nolock), gam26 z with(nolock) where a.gah00c = z.gaz02c and z.gaz01c = @compid and a.gah09d >= @fromdate and a.gah09d <= @todate group by gah00c update a set a.amt_coupon = b.amt from #tbl_result a, #tbl_result_buf b where a.compid = b.compid drop table #tbl_result_buf --处理非叶子节点的数据, START declare @maxAQ int select @maxAQ = max(gsb02c) from gsm02 with(nolock) where gsb00c = @compid and gsb01c = 'AQ' set @maxAQ = isnull(@maxAQ, 4) declare @each_parent varchar(20) begin create table #used_compid( compid varchar(20) not null, isleaf int null) insert #used_compid(compid, isleaf) select distinct gaz02c, 0 from gam26 with(nolock) where gaz01c = @compid update a set a.isleaf = 1 from #used_compid a, b_perf b with(nolock) where a.compid = b.bran_id and b.lvl = @maxAQ create table #notleaf_sons( parantid varchar(20) not null, sonid varchar(20) not null) insert #notleaf_sons(parantid, sonid) select gaz01c, gaz02c from gam26 z with(nolock), #used_compid b, #used_compid c where z.gaz01c = b.compid and b.isleaf = 0 and gaz01c <> gaz02c and z.gaz02c = c.compid and c.isleaf = 1 declare cur_each_parent_compid cursor for select compid from #used_compid where isleaf = 0 open cur_each_parent_compid fetch cur_each_parent_compid into @each_parent while @@fetch_status = 0 begin delete #tbl_result where compid = @each_parent insert #tbl_result(compid, compname, amt_good, amt_item, amt_save, amt_coupon) select @each_parent, '', sum(amt_good), sum(amt_item), sum(amt_save), sum(amt_coupon) from #tbl_result a, #notleaf_sons b where a.compid = b.sonid and b.parantid = @each_parent fetch cur_each_parent_compid into @each_parent end close cur_each_parent_compid deallocate cur_each_parent_compid end --处理非叶子节点的数据, end update a set a.compname = b.gae03c from #tbl_result a, gam05 b with(nolock) where a.compid = b.gae01c select isnull(compid,'') compid, isnull(compname,'') compname, isnull(amt_good,0) amt_good, isnull(amt_item,0) amt_item, isnull(amt_save,0) amt_save, isnull(amt_coupon,0) amt_coupon from #tbl_result order by compid drop table #tbl_result end GO /****** Object: UserDefinedFunction [dbo].[payway_gsm03] Script Date: 06/12/2017 06:20:58 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO --创建一个函数 Create function [dbo].[payway_gsm03] ( @gsc00c varchar(40), @gsc01c varchar(20), @gsc02c varchar(20) ) returns varchar(40) as begin declare @str varchar(40) select @str=isnull(@str+':','')+cast(gsb03c as varchar)+ ';'+ cast(gsc05f as varchar) from gsm03 a with (nolock),gsm02 b with(nolock) where gsc00c = @gsc00c and gsc01c = @gsc01c and gsc02c = @gsc02c and gsb00c = gsc00c and gsb02c = gsc04c and gsb01c = '3' return @str end GO /****** Object: StoredProcedure [dbo].[upg_quick_recover_gsm02_ba] Script Date: 06/12/2017 06:20:30 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_quick_recover_gsm02_ba] as begin -- 创建临时表 create table #data_recover_comp ( compId varchar(10) not null ) insert into #data_recover_comp (compId) select gae01c from gam05 with (nolock) where gae01c <> '001' -- 创建循环变量 declare @HasRecord int set @HasRecord = 1 declare @currentCompId varchar(10) while(@HasRecord = 1) begin if exists ( select 1 from #data_recover_comp) begin set @HasRecord = 1 select top 1 @currentCompId = compId from #data_recover_comp delete from gsm02 where gsb01c = 'BA' and gsb00c = @currentCompId insert into gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i, gsb06i) select @currentCompId, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i, gsb06i from gsm02 where gsb01c = 'BA' and gsb00c = '001' -- 清除已处理的门店 delete #data_recover_comp where compId = @currentCompId end else begin set @HasRecord = 0 end end drop table #data_recover_comp end GO /****** Object: StoredProcedure [dbo].[upg_init_gsm02_3] Script Date: 06/12/2017 06:20:14 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_init_gsm02_3] as begin -- 初始化gsm02_3表 使用者可见支付方式设置 declare @gaa01c varchar(10) declare cur_get_each_gam01 cursor for select gaa01c from gam01 with(nolock) -- 使用者基本资料 open cur_get_each_gam01 fetch cur_get_each_gam01 into @gaa01c while @@fetch_status = 0 begin declare @gsb02c varchar(10) declare cur_get_each_gsm02 cursor for select gsb02c from gsm02 with(nolock) where gsb01c = '3' and gsb00c = '001' open cur_get_each_gsm02 fetch cur_get_each_gsm02 into @gsb02c while @@fetch_status = 0 begin if not exists (select 1 from gsm02_3 where gsa01c = @gaa01c and gsa02c = @gsb02c) begin insert into gsm02_3(gsa01c, gsa02c, gsa03i, gsa04i, gsa05i, gsa06i, gsa07i, gsa08i) values(@gaa01c, @gsb02c, 1, 1, 1, 1, 1, 1) -- values(@gaa01c, @gsb02c, 1, (case when @gsb02c in ('2', '4', '9', 'A', '7', '#') then 0 else 1 end), 1, 1, 1, 1) end fetch cur_get_each_gsm02 into @gsb02c end close cur_get_each_gsm02 deallocate cur_get_each_gsm02 fetch cur_get_each_gam01 into @gaa01c end close cur_get_each_gam01 deallocate cur_get_each_gam01 end GO /****** Object: StoredProcedure [dbo].[upg_get_discount] Script Date: 06/12/2017 06:20:07 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_get_discount] ( @compid varchar(10), -- 公司别 @kind varchar(5), -- 类别(1-项目, 2-产品, 3-会员卡) @class varchar(5), -- 类别代码(项目类别,产品类别,会员卡类别 @cardtype varchar(20), -- 会员卡的类别 @curdate varchar(10), -- 当前日期 @curtime varchar(10), -- 当前时间 @accttype int, -- 账户 @discount float output ) as begin declare @dis_mode int --会员折扣方式(1- 只享受会员折扣, 2- = 会员折扣 * 一般折扣) declare @time_mode int --一般折扣时间设定(0 - 不设定, 1- 设定) declare @date_mode int --一般折扣依据(1 - 依日期, 2- 依周次) declare @mem_discount float -- 会员折扣 declare @nor_discount float -- 一般折扣 select @dis_mode = gdf03i, @time_mode = gdf04i, @date_mode = gdf05i from gdm06 where gdf00c = @compid and gdf01c = @class and gdf02c = @kind if (@date_mode = 1) begin -- 日期 if (@dis_mode = 1 or @dis_mode = 2) begin select @mem_discount = gdg04f from gdm07 where gdg00c = @compid and gdg01c = @class and gdg02c = @kind and gdg03c = @cardtype and gdg06d <= @curdate and gdg07d >= @curdate and gdg11i = @accttype end if (@dis_mode = 3 or @dis_mode = 2) begin select @nor_discount = gdh08f from gdm08 where gdh00c = @compid and gdh01c = @class and gdh02c = @kind and gdh04d <= @curdate and gdh05d >= @curdate end if (@dis_mode = 2) select @discount = isnull(@mem_discount, 1) * isnull(@nor_discount, 1) else if (@dis_mode = 1) select @discount = isnull(@mem_discount, 1) else if (@dis_mode = 3) select @discount = isnull(@nor_discount, 1) end end GO /****** Object: StoredProcedure [dbo].[upg_m_dispatch_gdm06] Script Date: 06/12/2017 06:20:19 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_m_dispatch_gdm06] ( @fromcompid varchar(10), -- 从哪一个公司发布 @tocompid varchar(10), -- 目的公司 @mandatory varchar(2)) -- 是否强制统一 as begin if @fromcompid = @tocompid return create table #gdm06_1 -- 项目/产品/会员卡类别价格折扣 ( gdf00c varchar(10) not null, --公司编号 gdf01c varchar(5) not null, --类别编号 gdf02c varchar(5) not null, --类别(1- 项目, 2-产品, 3 -会员卡) gdf03i int null, --会员折扣方式(1- 只享受会员折扣, 2- = 会员折扣 * 一般折扣 3-一般折扣) gdf04i int null, --一般折扣时间设定(0 - 不设定, 1- 设定) gdf05i int null, --一般折扣依据(1 - 依日期, 2- 依周次) gdf06f float null, --买卡默认折扣 gdf07f float null, --充值默认折扣 gdf08f float null, --买卡最低折扣 gdf09f float null, --充值最低折扣 gdf10f float null, --储值账户购买疗程的折扣 gdf11f float null, --购买套餐默认折扣 gdf12f float null, --购买套餐最低折扣 ) declare @PK_Sql nvarchar(max) set @PK_Sql = ' alter table #gdm06_1 add constraint PK_#gdm06_1_' + convert(varchar(10), cast(floor(rand() * 1000000) as int)) + ' primary key clustered(gdf00c, gdf01c, gdf02c)' exec sp_executesql @PK_Sql insert #gdm06_1(gdf00c, gdf01c, gdf02c, gdf03i, gdf04i, gdf05i, gdf06f, gdf07f, gdf08f, gdf09f, gdf10f, gdf11f, gdf12f) select gdf00c, gdf01c, gdf02c, gdf03i, gdf04i, gdf05i, gdf06f, gdf07f, gdf08f, gdf09f, gdf10f, gdf11f, gdf12f from gdm06 where gdf00c = @fromcompid if @mandatory = '1' begin delete gdm06 where gdf00c = @tocompid end else begin delete a from #gdm06_1 a, gdm06 b where a.gdf00c = @fromcompid and a.gdf01c = b.gdf01c and b.gdf00c = @tocompid end insert gdm06(gdf00c, gdf01c, gdf02c, gdf03i, gdf04i, gdf05i, gdf06f, gdf07f, gdf08f, gdf09f, gdf10f, gdf11f, gdf12f) select @tocompid, gdf01c, gdf02c, gdf03i, gdf04i, gdf05i, gdf06f, gdf07f, gdf08f, gdf09f, gdf10f, gdf11f, gdf12f from #gdm06_1 where gdf00c = @fromcompid insert gdm07(gdg00c, gdg01c, gdg02c, gdg03c, gdg04f, gdg05f, gdg06d, gdg07d, gdg08i, gdg09i, gdg10f, gdg11i, gdg12f) select @tocompid, gdg01c, gdg02c, gdg03c, gdg04f, gdg05f, gdg06d, gdg07d, gdg08i, gdg09i, gdg10f, gdg11i, gdg12f from gdm07 where gdg00c = @fromcompid insert gdm08(gdh00c, gdh01c, gdh02c, gdh03f, gdh04d, gdh05d, gdh06i, gdh07i, gdh08f, gdh09i, gdh10f, gdh11c) select @tocompid, gdh01c, gdh02c, gdh03f, gdh04d, gdh05d, gdh06i, gdh07i, gdh08f, gdh09i, gdh10f, gdh11c from gdm08 where gdh00c = @fromcompid insert gdm09(gdi00c, gdi01c, gdi02c, gdi03f, gdi04t, gdi05t, gdi06f, gdi07i, gdi08i, gdi09f) select @tocompid, gdi01c, gdi02c, gdi03f, gdi04t, gdi05t, gdi06f, gdi07i, gdi08i, gdi09f from gdm09 where gdi00c = @fromcompid end GO /****** Object: StoredProcedure [dbo].[upg_copy_gdm06] Script Date: 06/12/2017 06:19:46 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_copy_gdm06] ( @compid varchar(10), -- 公司别 @kind varchar(5), -- 类别(1- 项目, 2-产品, 3 -会员卡) @fromtype varchar(20), -- 来源的类别 @totype varchar(20)) as begin delete gdm06 where gdf00c = @compid and gdf01c = @totype and gdf02c = @kind insert gdm06(gdf00c, gdf01c, gdf02c, gdf03i, gdf04i, gdf05i, gdf06f, gdf07f, gdf08f, gdf09f, gdf10f, gdf11f, gdf12f) select gdf00c, @totype, gdf02c, gdf03i, gdf04i, gdf05i, gdf06f, gdf07f, gdf08f, gdf09f, gdf10f, gdf11f, gdf12f from gdm06 where gdf00c = @compid and gdf01c = @fromtype and gdf02c = @kind delete gdm07 where gdg00c = @compid and gdg01c = @totype and gdg02c = @kind insert gdm07(gdg00c, gdg01c, gdg02c, gdg03c, gdg04f, gdg05f, gdg06d, gdg07d, gdg08i, gdg09i, gdg10f, gdg11i) select gdg00c, @totype, gdg02c, gdg03c, gdg04f, gdg05f, gdg06d, gdg07d, gdg08i, gdg09i, gdg10f, gdg11i from gdm07 where gdg00c = @compid and gdg01c = @fromtype and gdg02c = @kind delete gdm08 where gdh00c = @compid and gdh01c = @totype and gdh02c = @kind insert gdm08(gdh00c, gdh01c, gdh02c, gdh03f, gdh04d, gdh05d, gdh06i, gdh07i, gdh08f, gdh09i) select gdh00c, @totype, gdh02c, gdh03f, gdh04d, gdh05d, gdh06i, gdh07i, gdh08f, gdh09i from gdm08 where gdh00c = @compid and gdh01c = @fromtype and gdh02c = @kind delete gdm09 where gdi00c = @compid and gdi01c = @totype and gdi02c = @kind insert gdm09(gdi00c, gdi01c, gdi02c, gdi03f, gdi04t, gdi05t, gdi06f, gdi07i) select gdi00c, @totype, gdi02c, gdi03f, gdi04t, gdi05t, gdi06f, gdi07i from gdm09 where gdi00c = @compid and gdi01c = @fromtype and gdi02c = @kind end GO /****** Object: StoredProcedure [dbo].[upg_pickmessage] Script Date: 06/12/2017 06:20:27 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_pickmessage] ( @compid varchar(10), -- 哪个公司来取信 @ddate varchar(8) -- 取信的日期 ) as begin if @ddate = '' begin declare @curtime varchar(20) exec upg_getcurrdate @ddate output, @curtime output end -- 时间太长的垃圾信需要删除,我们假设1周之前都需要删除 delete gam19 where gaq00c = @compid and datediff(day, gcq10d, @ddate) > 7 -- 提醒日期在今天以及今天之前的都需要取过来 insert gam19(gaq00c, gaq02c, gaq03c, gaq04d, gaq05t, gaq06c, gaq07c, gaq08i, gaq09i, gcq10d, gcq11c, gcq12c) select @compid, gao00c, gao03c, gao04d, gao05t, gao06c, gao07c, 0, 0, gco08d, gco09c, gco10c from gam17 where gao02c = @compid and gco08d <= @ddate delete gam17 where gao02c = @compid and gco08d <= @ddate end GO /****** Object: StoredProcedure [dbo].[upg_recover_gsm02_3_role_custom] Script Date: 06/12/2017 06:20:32 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_recover_gsm02_3_role_custom] as begin -- 判断是否已做数据恢复 if exists (select 1 from data_recover where rdd00c = '001' and rdd01c = 'gsm02_3_role' and rdd02c = 'recover_custom') begin return end update gsm02_3_role set gsa06i_role = gsa03i_role, gsa07i_role = gsa03i_role, gsa08i_role = gsa03i_role insert into data_recover(rdd00c, rdd01c, rdd02c) values ('001', 'gsm02_3_role', 'recover_custom') end GO /****** Object: StoredProcedure [dbo].[upg_copy_user_module_right_paymenet] Script Date: 06/12/2017 06:19:49 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_copy_user_module_right_paymenet] ( @fromuser varchar(20), @touser varchar(20) ) as begin begin transaction begin try --模组权限拷贝 delete gam02 where gab01c = @touser and gab02c = '1' insert gam02(gab01c, gab02c, gab03c, gab04c, gab06c) select @touser, gab02c, gab03c, gab04c, gab06c from gam02 where gab01c = @fromuser and gab02c = '1' --使用者权限参数拷贝 delete gam02 where gab01c=@touser and gab02c='4' insert gam02(gab01c, gab02c, gab03c, gab04c) select @touser, gab02c, gab03c, gab04c from gam02 where gab01c = @fromuser and gab02c = '4' --可见支付方式拷贝 delete gsm02_3 where gsa01c=@touser insert gsm02_3(gsa01c, gsa02c, gsa03i, gsa04i, gsa05i, gsa06i, gsa07i, gsa08i) select @touser, gsa02c, gsa03i, gsa04i, gsa05i, gsa06i, gsa07i, gsa08i from gsm02_3 where gsa01c = @fromuser end try begin catch if @@TRANCOUNT > 0 begin rollback transaction declare @SP_Parameters varchar(max) set @SP_Parameters = '@fromuser = ' + isnull(@fromuser, '') + '@touser = ' + isnull(@touser, '') insert into SP_TranLog (SPName , SPTime , SPParameter) values ( 'upg_copy_userright' , getdate() , @SP_Parameters) raiserror ('transaction is failed.', 16, 1) with NOWAIT end end catch if @@TRANCOUNT > 0 begin commit transaction end end GO /****** Object: StoredProcedure [dbo].[upg_recover_gsm02_3_custom] Script Date: 06/12/2017 06:20:32 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_recover_gsm02_3_custom] as begin -- 判断是否已做数据恢复 if exists (select 1 from data_recover where rdd00c = '001' and rdd01c = 'gsm02_3' and rdd02c = 'recover_custom') begin return end update gsm02_3 set gsa06i = gsa03i, gsa07i = gsa03i, gsa08i = gsa03i insert into data_recover(rdd00c, rdd01c, rdd02c) values ('001', 'gsm02_3', 'recover_custom') end GO /****** Object: StoredProcedure [dbo].[upg_recover_gsm02_3] Script Date: 06/12/2017 06:20:32 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_recover_gsm02_3] as begin -- 判断是否已做数据恢复 if exists (select 1 from data_recover where rdd00c = '001' and rdd01c = 'gsm02_3' and rdd02c = 'recover') begin return end update gsm02_3 --set gsa04i = (case when gsa02c in ('2', '4', '9', 'A', '7', '#') then 0 else gsa03i end), set gsa04i = 1, gsa05i = gsa03i insert into data_recover(rdd00c, rdd01c, rdd02c) values ('001', 'gsm02_3', 'recover') end GO /****** Object: StoredProcedure [dbo].[upg_m_dispatch_gzm06] Script Date: 06/12/2017 06:20:20 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_m_dispatch_gzm06] ( @fromcompid varchar(10), -- 从哪一个公司发布 @tocompid varchar(10), -- 目的公司 @mandatory varchar(2) -- 是否强制统一 ) as begin if @fromcompid = @tocompid return if @mandatory <> '1' and @mandatory <> '0' return if @mandatory = '1' begin delete gzm06 where gzf00c = @tocompid insert gzm06(gzf00c, gzf01c, gzf02c, gzf03f, gzf04f, gzf05f, gzf06i, gzf07i) select @tocompid, gzf01c, gzf02c, gzf03f, gzf04f, gzf05f, gzf06i, gzf07i from gzm06 with (nolock) where gzf00c = @fromcompid return end create table #gzm06_key -- 赠送抵用券资料关键字 ( gzf01c varchar(5) not null, -- 卡类别, *代表不限制卡类别 gzf02c varchar(5) not null, -- 赠券类别 gzf03f float not null, -- 购买的金额 ) insert into #gzm06_key(gzf01c, gzf02c, gzf03f) select gzf01c, gzf02c, gzf03f from gzm06 with (nolock) where gzf00c = @fromcompid declare @current_gzf01c varchar(5) -- 卡类别, declare @current_gzf02c varchar(5) -- 赠券类别 declare @current_gzf03f float -- 购买的金额 -- 创建循环变量 declare @HasRecord int set @HasRecord = 1 declare @Index int set @Index = -1 while(@HasRecord = 1) begin if exists ( select 1 from #gzm06_key) begin set @HasRecord = 1 select top 1 @current_gzf01c = gzf01c, @current_gzf02c = gzf02c , @current_gzf03f = gzf03f from #gzm06_key if not exists (select 1 from gzm06 with (nolock) where gzf00c = @tocompid and gzf01c = @current_gzf01c and gzf02c = @current_gzf02c and gzf03f = @current_gzf03f) begin insert gzm06(gzf00c, gzf01c, gzf02c, gzf03f, gzf04f, gzf05f, gzf06i, gzf07i) select @tocompid, gzf01c, gzf02c, gzf03f, gzf04f, gzf05f, gzf06i, gzf07i from gzm06 with (nolock) where gzf00c = @fromcompid and gzf01c = @current_gzf01c and gzf02c = @current_gzf02c and gzf03f = @current_gzf03f end delete from #gzm06_key where gzf01c = @current_gzf01c and gzf02c = @current_gzf02c and gzf03f = @current_gzf03f end else begin set @HasRecord = 0 end end drop table #gzm06_key end GO /****** Object: StoredProcedure [dbo].[upg_sendmessage] Script Date: 06/12/2017 06:20:40 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_sendmessage] ( @compid varchar(10), -- 从哪个公司发出来的 @tocompid varchar(10), -- 发送到哪个公司 @content varchar(128), -- 内容 @ddate varchar(10), -- 日期 @ttime varchar(10), -- 时间 @billtype varchar(10), -- 单据类型 @billid varchar(20) -- 单据号 ) as begin declare @comp varchar(10) if @tocompid = '*' select @comp = '%' else select @comp = @tocompid --输入的对话内容,DEBUG if not exists(select 1 from gam19 where gaq06c = '1' and (gaq00c = @tocompid or @tocompid = '*')) begin insert gam19(gaq00c, gaq02c, gaq03c, gaq04d, gaq05t, gaq06c, gaq08i, gaq09i) select @tocompid, @compid, '公告发布', @ddate, @ttime, '1', 0, 0 end if @billtype = '1' -- 输入的通讯内容 begin insert gam23(gau00c, gau01c, gau03c, gau05d, gau10t, gau14c, gau15c, gau16d, gau17i) values (@tocompid, '1', null, @ddate, @ttime, @content, @compid, @ddate, 0) end else begin if not exists(select 1 from gam19 where gaq06c = '10' and gaq00c = @tocompid) begin insert gam19(gaq00c, gaq02c, gaq03c, gaq04d, gaq05t, gaq06c, gaq08i, gaq09i) values (@tocompid, @compid, '供求管理', @ddate, @ttime, '10', 0, 0) end insert gam23(gau00c, gau01c, gau03c, gau05d, gau10t, gau14c, gau15c, gau16d, gau17i) values (@tocompid, '10', @billid, @ddate, @ttime, @content, @billtype, @ddate, 0) end end GO /****** Object: StoredProcedure [dbo].[upg_delete_message] Script Date: 06/12/2017 06:19:59 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_delete_message] as --WUXM 2005/08/04 --modified WUXM 2005/08/12 begin declare @dateto varchar(10) declare @datefrom varchar(10) declare @datebegin varchar(8) declare @dateend varchar(8) select @dateto = convert(varchar(10), getdate(), 120) select @datefrom = convert(varchar(10), dateadd(day, -7, getdate()), 120) select @datebegin = substring(@datefrom, 1, 4) + substring(@datefrom, 6, 2) + substring(@datefrom, 9, 2) select @dateend = substring(@dateend, 1, 4) + substring(@dateend, 6, 2) + substring(@datefrom, 9, 2) delete from gam19 where gaq04d < @datebegin delete from gam23 where gau16d < @datebegin --控件缓存(显示隐藏) truncate table FormCustom end GO /****** Object: StoredProcedure [dbo].[upg_dispmessage_ex] Script Date: 06/12/2017 06:20:01 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_dispmessage_ex] ( @compid varchar(10), -- 哪个公司来取信 @dispflag int, -- 条件 @cardid varchar(20) ) as begin if @dispflag = 1 begin select * from gam19 where gaq00c = @compid and gcq11c = @cardid order by gaq01i desc end else if (@dispflag = 2) begin select * from gam19 where gaq00c = @compid and isnull(gaq09i, 0) = 0 and gcq11c = @cardid order by gaq01i desc end end GO /****** Object: StoredProcedure [dbo].[upg_dispmessage] Script Date: 06/12/2017 06:20:01 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_dispmessage] ( @compid varchar(10), -- 哪个公司来取信 @dispflag int -- 条件 ) as begin if @dispflag = 1 begin select * from gam19 where gaq00c = @compid end else if (@dispflag = 2) begin select * from gam19 where gaq00c = @compid and isnull(gaq09i, 0) = 0 end end GO /****** Object: StoredProcedure [dbo].[upg_initial_endtime] Script Date: 06/12/2017 06:20:14 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_initial_endtime] ( @compid varchar(10) ) as --Mzh 2005/12/21 begin update gicm11 set gick09t = gick04t where gick00c = @compid and isnull(gick09t, '') = '' and isnull(gick04t, '') <> '' update hdm01 set hda10t = hda04t where hda00c = @compid and isnull(hda10t, '') = '' and isnull(hda04t, '') <> '' update gicm10 set gicj12t = gicj04t where gicj00c = @compid and isnull(gicj12t, '') = '' and isnull(gicj04t, '') <> '' end GO /****** Object: StoredProcedure [dbo].[upg_checkonWorkAttendance] Script Date: 06/12/2017 06:19:41 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_checkonWorkAttendance] ( @stuffNo varchar(20), -- 员工编号 @compid varchar(10), -- 公司别 @strDateFo varchar(8), -- 起始日期 @strDateTo varchar(8), -- 截止日期 @UserNo varchar(10), -- 会员编号 @strDate varchar(8), -- 考勤日期 @checked int -- 复核否 ) as-- wuxinming, 2004/12/17 begin if (@checked = 0) begin update hdm01 set hda93c = @UserNo, hda94d = @strDate where hda00c = @compid and hda01c = @stuffNo and hda02d between @strDateFo and @strDateTo end if (@checked = 1) begin update hdm01 set hda93c = '', hda94d = '' where hda00c = @compid and hda01c = @stuffNo and hda02d between @strDateFo and @strDateTo end end GO /****** Object: StoredProcedure [dbo].[upg_duty_record] Script Date: 06/12/2017 06:20:01 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_duty_record] ( @compid varchar(10), --公司编号 @strdate varchar(8), --日期 @empid varchar(20), --员工编号 @strduty varchar(5), --班次 @iscls int, --是否排班 @relt int output --返回考勤时间的位置 ) as begin declare @strExisted int --是否存在 declare @strTime1 varchar(6) --考勤时间1 declare @strTime2 varchar(6) --考勤时间2 declare @strTime3 varchar(6) --考勤时间3 declare @strTime4 varchar(6) --考勤时间4 declare @strTime5 varchar(6) --考勤时间5 declare @strTime6 varchar(6) --考勤时间6 if (@iscls = 0) begin select @strExisted = 1, @strTime1 = hda03t, @strTime2 = hda04t, @strTime3 = hda12t, @strTime4 = hda13t, @strTime5 = hda14t, @strTime6 = hda15t from hdm01 where hda00c = @compid and hda02d = @strdate and hda18c = @strduty and hda01c = @empid end else begin select @strExisted = 1, @strTime1 = hda03t, @strTime2 = hda04t, @strTime3 = hda12t, @strTime4 = hda13t, @strTime5 = hda14t, @strTime6 = hda15t from hdm01 where hda00c = @compid and hda02d = @strdate and hda01c = @empid end if (isnull(@strExisted, 0) = 0) begin set @relt = -1 end else if (isnull(@strTime1, '') = '') begin set @relt = 0 end else if (isnull(@strTime2, '') = '') begin set @relt = 1 end else if (isnull(@strTime3, '') = '') begin set @relt = 2 end else if (isnull(@strTime4, '') = '') begin set @relt = 3 end else if (isnull(@strTime5, '') = '') begin set @relt = 4 end else if (isnull(@strTime6, '') = '') begin set @relt = 5 end end GO /****** Object: StoredProcedure [dbo].[upg_create_gsm03_from_one_gcm10] Script Date: 06/12/2017 06:19:53 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_create_gsm03_from_one_gcm10] ( @compid varchar(10), -- 公司别 @billid varchar(20), @confirm int ) as begin if (@confirm = 0) begin delete a from gsm03 a where a.gsc02c = 'gz' and a.gsc01c = @billid and a.gsc00c = @compid end else begin insert gsm03(gsc00c, gsc01c, gsc02c, gsc03f, gsc04c, gsc05f, gsc06c, gsc10d, gsc11c, gsc12i) select gcl00c, gcl01c, 'gz', 1, gcl10c, gcl07f-isnull(gcl13f, 0), gcl03c + '-' + gcl04c, gcl80d, gcl93c, gcl06i from gcm10 where gcl00c = @compid and (gcl06i = 0 or gcl06i = 6) -- 存款以及还款 and gcl01c = @billid and isnull(gcl10c, '') <> '' -- 会员账户管理中的欠款 insert gsm03(gsc00c, gsc01c, gsc02c, gsc03f, gsc04c, gsc05f, gsc06c, gsc10d, gsc11c, gsc12i) select gcl00c, gcl01c, 'gz', 2, '5', isnull(gcl13f, 0), gcl03c + '-' + gcl04c, gcl80d, gcl93c, gcl06i from gcm10 where gcl00c = @compid and (gcl06i = 0 or gcl06i = 6) -- 存款以及还款 and isnull(gcl13f, 0) > 0 and gcl01c = @billid and isnull(gcl10c, '') <> '' -- 单独的在会员账户管理新建的欠款 insert gsm03(gsc00c, gsc01c, gsc02c, gsc03f, gsc04c, gsc05f, gsc06c, gsc10d, gsc11c, gsc12i) select gcl00c, gcl01c, 'gz', 1, '5', 0-isnull(gcl07f, 0), gcl03c + '-' + gcl04c, gcl80d, gcl93c, gcl06i from gcm10 where gcl00c = @compid and (gcl06i = 5) -- 欠款 and isnull(gcl11c, '') = '' and gcl01c = @billid and isnull(gcl10c, '') <> '' -- 取款 insert gsm03(gsc00c, gsc01c, gsc02c, gsc03f, gsc04c, gsc05f, gsc06c, gsc10d, gsc11c, gsc12i) select gcl00c, gcl01c, 'gz', 1, gcl10c, 0-isnull(gcl07f, 0), gcl03c + '-' + gcl04c, gcl80d, gcl93c, gcl06i from gcm10 where gcl00c = @compid and (gcl06i = 1) -- 取款 and gcl01c = @billid and isnull(gcl10c, '') <> '' end end GO /****** Object: StoredProcedure [dbo].[delete_gcm10_gcm04] Script Date: 06/12/2017 06:19:19 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[delete_gcm10_gcm04]( @compid varchar(10), @billid varchar(20), @srcbillid varchar(20), @srctype varchar(10)) as begin delete gcm10 where gcl00c = @compid and gcl01c = @billid delete gcm04 where gcd00c = @compid and gcd07c = 'gz' and gcd08c = @billid update gsm03 set gsc09c = '' where gsc00c = @compid and gsc01c = @srcbillid and gsc02c = @srctype and gsc04c = '5' end GO /****** Object: StoredProcedure [dbo].[upg_create_ggm01_for_stat] Script Date: 06/12/2017 06:19:52 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_create_ggm01_for_stat] ( @billtype varchar(10), -- 单据类别 @compid varchar(50), @billid varchar(20) -- 传入的是收银单号还是入店单号是根据系统参数设置的而来的 ) as begin declare @pay_total_amt float -- 付款总额 -- declare @billid_ex varchar(20) --如果传入是收银单号并且支付方式是多账单统一出院的时候,此单号是对应的入店单号 --但是对于ggm02 和 ggm03 表 挂的条件还应该还是@billid 对于gsm03中挂的条件应该是@billid_ex --判断是否已经平衡了 create table #gsm03_buf -- 单据--支付明细 ( gsc04c varchar(5) null, --支付方式 gsc05f float null --支付金额 ) create table #gsm03_buf_2 -- 单据--支付明细 ( gsc04c varchar(5) null, --支付方式 gsc05f float null --支付金额 ) if (@billtype = 'gl') begin insert #gsm03_buf(gsc04c, gsc05f) select ggb27c, sum(ggb11f) from ggm02 with (nolock), ggm01 with (nolock) where gga00c = ggb00c and gga01c = ggb01c and ggb00c = @compid and gga30c = @billid group by ggb27c union select ggc27c, sum(ggc11f) from ggm03, ggm01 where gga00c = ggc00c and gga01c = ggc01c and ggc00c = @compid and gga30c = @billid group by ggc27c end else begin insert #gsm03_buf(gsc04c, gsc05f) select ggb27c, sum(ggb11f) from ggm02 with (nolock) where ggb00c = @compid and ggb01c = @billid group by ggb27c union select ggc27c, sum(ggc11f) from ggm03 with (nolock) where ggc00c = @compid and ggc01c = @billid group by ggc27c end insert #gsm03_buf_2(gsc04c, gsc05f) select gsc04c, sum(gsc05f) from #gsm03_buf group by gsc04c delete a from #gsm03_buf_2 a, gsm03 b where b.gsc00c = @compid and b.gsc02c = @billtype and b.gsc01c = @billid and a.gsc04c = b.gsc04c and convert(numeric(20, 2), a.gsc05f) = convert(numeric(20, 2), b.gsc05f) if not exists(select 1 from #gsm03_buf_2) return drop table #gsm03_buf drop table #gsm03_buf_2 select @pay_total_amt = sum(isnull(gsc05f, 0)) from gsm03 where gsc00c = @compid and gsc01c = @billid and gsc02c = @billtype create table #ggm02_buf -- 项目消费(收银)--消费明细 ( ggb00c varchar(10) not null, --公司编号 ggb01c varchar(20) not null, --消费单号 ggb02f int identity not null, --序号 ggb03c varchar(20) null, --子项目代码 ggb04c varchar(5) null, --单位 ggb05f float null, --数量 ggb06c varchar(5) null, --币别 ggb07f float null, --汇率 ggb08f float null, --标准单价 ggb09f float null, --折让 ggb10f float null, --折让单价 ggb11f float null, --金额 ggb12c varchar(20) null, --大工代码 ggb13c varchar(20) null, --中工代码 ggb14c varchar(20) null, --小工代码 ggb15c varchar(20) null, --大工类型 ggb16c varchar(20) null, --中工类型 ggb17f float null, --成本金额 ggb18f float null, --转卡比率 ggb19c varchar(20) null, --小工类型 ggb20f float null, --大工虚业绩 ggb21f float null, --中工虚业绩 ggb22f float null, --小工虚业绩 ggb23c varchar(128) null, --项目打折原因, added by WUXM, 2005/09/01 ggb24f float null, --项目应收金额, added by WUXM, 2005/09/08 ggb25c varchar(20) null, --四工类型 ggb26c varchar(20) null, --四工代码 ggb27c varchar(5) null, --项目支付方式 Added by WUXM 2005/11/09 ggb28f float null, --大工提成 Added by WUXM 2005/11/10 ggb29f float null, --中工提成 Added by WUXM 2005/11/10 ggb30f float null, --小工提成 Added by WUXM 2005/11/10 ggb31c varchar(20) null, --五工类别 ggb32c varchar(20) null, --五工代码 ggb33f float null, --项目积分 ggb34f float null, --项目的折算率 ggb35f float null, --四工虚业绩 ggb36f float null, --大工实业绩 ggb37f float null, --中工实业绩 ggb38f float null, --小工实业绩 ggb39f float null, --四工实业绩 ggb40i int null, --欠款标志 ggb41d varchar(20) null, --还款日期 ggb42c varchar(20) null, --还款单号 ggb43c varchar(6) null, --开单时间 ggb44c varchar(6) null, --下钟时间 ggb45i int null, --价格序号,当用疗程的时候需要知道用了哪个疗程 ggb46f float null, --折扣 ggb47i int null, --疗程支付的时候的疗程ID ggb48i int null, --套餐序号 ggb81c varchar(10) null, --部门编号 gsc00c varchar(10) not null, --公司编号 gsc01c varchar(20) not null, --单据编号 gsc02c varchar(5) not null, --单据类别 gsc03f float not null, --序号 gsc04c varchar(5) null, --支付方式 gsc05f float null, --支付金额 gsc06c varchar(400) null, --备注说明 gsc07f float null, --预留-标志单据是否已经付款了(1.付款 0.未付款) gsc08c varchar(20) null, --收银钥匙牌号, 卡销售会员卡号 gsc09c varchar(20) null, --账户异动单号, Added by WUXM, 2004/04/10 gsc10d varchar(8) null, --单据帐务日期 gsc11c varchar(20) null, --登陆人 gsc12i int null --方式 ) create table #ggm03_buf -- 项目消费--产品明细 ( ggc00c varchar(10) not null, --公司编号 ggc01c varchar(20) not null, --消费单号 ggc02f int identity not null, --序号 ggc03c varchar(20) null, --产品代码 ggc04c varchar(5) null, --单位 ggc05f float null, --数量 ggc06c varchar(5) null, --币别 ggc07f float null, --汇率 ggc08f float null, --标准单价 ggc09f float null, --折让 ggc10f float null, --折让单价 ggc11f float null, --金额 ggc12c varchar(20) null, --产品销售人员 ggc13f float null, --成本金额 ggc14f float null, --第一分享比率 ggc15c varchar(20) null, --第二人 ggc16f float null, --第二分享比率 ggc17c varchar(20) null, --第三人 ggc18f float null, --第三分享比率 ggc19c varchar(128) null, --产品打折原因, added by WUXM, 2005/09/01 ggc20f float null, --产品应收金额, added by WUXM, 2005/09/08 ggc21f float null, --第一销售虚业绩 Added by WUXM 2005/11/10 ggc22f float null, --第一销售提成 ggc23f float null, --第二销售虚业绩 Added by WUXM 2005/11/10 ggc24f float null, --第二销售提成 ggc25f float null, --第三销售虚业绩 Added by WUXM 2005/11/10 ggc26f float null, --第三销售提成 ggc27c varchar(5) null, --产品支付方式 ggc28f float null, --产品积分 ggc29f float null, --产品折算比率 ggc30f float null, --产品折算比率 ggc31c varchar(20) null, --仓库编号 ggc32i int null, --欠款标志 ggc33d varchar(20) null, --还款日期, 这个时候才计算提成 ggc34c varchar(20) null, --还款单号 ggc35i int null, --价格序号 ggc36i int null, --方式 0带走 1寄存 2欠货 ggc37f float null, --标准单位数量 ggc38f float null, --标准单位价格 ggc81c varchar(10) null, --部门编号 ggc48i int null, --套餐序号 gsc00c varchar(10) not null, --公司编号 gsc01c varchar(20) not null, --单据编号 gsc02c varchar(5) not null, --单据类别 gsc03f float not null, --序号 gsc04c varchar(5) null, --支付方式 gsc05f float null, --支付金额 gsc06c varchar(400) null, --备注说明 gsc07f float null, --预留-标志单据是否已经付款了(1.付款 0.未付款) gsc08c varchar(20) null, --收银钥匙牌号, 卡销售会员卡号 gsc09c varchar(20) null, --账户异动单号, Added by WUXM, 2004/04/10 gsc10d varchar(8) null, --单据帐务日期 gsc11c varchar(20) null, --登陆人 gsc12i int null --方式 ) if @billtype = 'gl' begin insert #ggm02_buf(ggb00c, ggb01c, ggb03c, ggb04c, ggb05f, ggb06c, ggb07f, ggb08f, ggb09f, ggb10f, ggb11f, ggb12c, ggb13c, ggb14c, ggb15c, ggb16c, ggb17f, ggb18f, ggb19c, ggb20f, ggb21f, ggb22f, ggb23c, ggb24f, ggb25c, ggb26c, ggb27c, ggb28f, ggb29f, ggb30f, ggb31c, ggb32c, ggb33f, ggb34f, ggb35f, ggb36f, ggb37f, ggb38f, ggb39f, ggb40i, ggb41d, ggb42c, ggb43c, ggb44c, ggb45i, ggb46f, ggb47i, ggb48i, ggb81c, gsc00c, gsc01c, gsc02c, gsc03f, gsc04c, gsc05f, gsc06c, gsc07f, gsc08c, gsc09c, gsc10d, gsc11c, gsc12i) select ggb00c, ggb01c, ggb03c, ggb04c, ggb05f, ggb06c, ggb07f, ggb08f, ggb09f, ggb10f, ggb11f, ggb12c, ggb13c, ggb14c, ggb15c, ggb16c, ggb17f, ggb18f, ggb19c, ggb20f, ggb21f, ggb22f, ggb23c, ggb24f, ggb25c, ggb26c, gsc04c, ggb28f, ggb29f, ggb30f, ggb31c, ggb32c, ggb33f, ggb34f, ggb35f, ggb36f, ggb37f, ggb38f, ggb39f, ggb40i, ggb41d, ggb42c, ggb43c, ggb44c, ggb45i, ggb46f, ggb47i, ggb48i, ggb81c, gsc00c, gsc01c, gsc02c, gsc03f, gsc04c, gsc05f, gsc06c, gsc07f, gsc08c, gsc09c, gsc10d, gsc11c, gsc12i from ggm02 b with (nolock), gsm03 c with (nolock), ggm01 a with (nolock) where a.gga00c = b.ggb00c and a.gga01c = b.ggb01c and a.gga00c = c.gsc00c and a.gga30c = c.gsc01c and c.gsc02c = @billtype and a.gga30c = @billid and a.gga00c = @compid order by ggb03c, ggb02f end else begin insert #ggm02_buf(ggb00c, ggb01c, ggb03c, ggb04c, ggb05f, ggb06c, ggb07f, ggb08f, ggb09f, ggb10f, ggb11f, ggb12c, ggb13c, ggb14c, ggb15c, ggb16c, ggb17f, ggb18f, ggb19c, ggb20f, ggb21f, ggb22f, ggb23c, ggb24f, ggb25c, ggb26c, ggb27c, ggb28f, ggb29f, ggb30f, ggb31c, ggb32c, ggb33f, ggb34f, ggb35f, ggb36f, ggb37f, ggb38f, ggb39f, ggb40i, ggb41d, ggb42c, ggb43c, ggb44c, ggb45i, ggb46f, ggb47i, ggb48i, ggb81c, gsc00c, gsc01c, gsc02c, gsc03f, gsc04c, gsc05f, gsc06c, gsc07f, gsc08c, gsc09c, gsc10d, gsc11c, gsc12i) select ggb00c, ggb01c, ggb03c, ggb04c, ggb05f, ggb06c, ggb07f, ggb08f, ggb09f, ggb10f, ggb11f, ggb12c, ggb13c, ggb14c, ggb15c, ggb16c, ggb17f, ggb18f, ggb19c, ggb20f, ggb21f, ggb22f, ggb23c, ggb24f, ggb25c, ggb26c, gsc04c, ggb28f, ggb29f, ggb30f, ggb31c, ggb32c, ggb33f, ggb34f, ggb35f, ggb36f, ggb37f, ggb38f, ggb39f, ggb40i, ggb41d, ggb42c, ggb43c, ggb44c, ggb45i, ggb46f, ggb47i, ggb48i, ggb81c, gsc00c, gsc01c, gsc02c, gsc03f, gsc04c, gsc05f, gsc06c, gsc07f, gsc08c, gsc09c, gsc10d, gsc11c, gsc12i from ggm02 with (nolock), gsm03 with (nolock) where ggb00c = @compid and ggb01c = @billid and ggb00c = gsc00c and ggb01c = gsc01c and gsc02c = @billtype order by ggb03c, ggb02f end update a set a.ggb05f = a.ggb05f * a.gsc05f/@pay_total_amt, a.ggb11f = a.ggb11f * (gsc05f/@pay_total_amt) from #ggm02_buf a if @billtype = 'gl' begin delete b from ggm02 b, ggm01 a where a.gga00c = b.ggb00c and a.gga01c = b.ggb01c and a.gga00c = @compid and a.gga30c = @billid end else begin delete ggm02 where ggb00c = @compid and ggb01c = @billid end insert ggm02(ggb00c, ggb01c, ggb02f, ggb03c, ggb04c, ggb05f, ggb06c, ggb07f, ggb08f, ggb09f, ggb10f, ggb11f, ggb12c, ggb13c, ggb14c, ggb15c, ggb16c, ggb17f, ggb18f, ggb19c, ggb20f, ggb21f, ggb22f, ggb23c, ggb24f, ggb25c, ggb26c, ggb27c, ggb28f, ggb29f, ggb30f, ggb31c, ggb32c, ggb33f, ggb34f, ggb35f, ggb36f, ggb37f, ggb38f, ggb39f, ggb40i, ggb41d, ggb42c, ggb81c, ggb43c, ggb44c, ggb45i, ggb46f, ggb47i, ggb48i) select ggb00c, ggb01c, ggb02f, ggb03c, ggb04c, ggb05f, ggb06c, ggb07f, ggb08f, ggb09f, ggb10f, ggb11f, ggb12c, ggb13c, ggb14c, ggb15c, ggb16c, ggb17f, ggb18f, ggb19c, ggb20f, ggb21f, ggb22f, ggb23c, ggb24f, ggb25c, ggb26c, ggb27c, ggb28f, ggb29f, ggb30f, ggb31c, ggb32c, ggb33f, ggb34f, ggb35f, ggb36f, ggb37f, ggb38f, ggb39f, ggb40i, ggb41d, ggb42c, ggb81c, ggb43c, ggb44c, ggb45i, ggb46f, ggb47i, ggb48i from #ggm02_buf order by ggb03c drop table #ggm02_buf if @billtype = 'gl' begin insert #ggm03_buf(ggc00c, ggc01c, ggc03c, ggc04c, ggc05f, ggc06c, ggc07f, ggc08f, ggc09f, ggc10f, ggc11f, ggc12c, ggc13f, ggc14f, ggc15c, ggc16f, ggc17c, ggc18f, ggc19c, ggc20f, ggc21f, ggc22f, ggc23f, ggc24f, ggc25f, ggc26f, ggc27c, ggc28f, ggc29f, ggc30f, ggc31c, ggc32i, ggc33d, ggc34c, ggc35i, ggc36i, ggc37f, ggc38f, ggc48i, ggc81c, gsc00c, gsc01c, gsc02c, gsc03f, gsc04c, gsc05f, gsc06c, gsc07f, gsc08c, gsc09c, gsc10d, gsc11c, gsc12i) select ggc00c, ggc01c, ggc03c, ggc04c, ggc05f, ggc06c, ggc07f, ggc08f, ggc09f, ggc10f, ggc11f, ggc12c, ggc13f, ggc14f, ggc15c, ggc16f, ggc17c, ggc18f, ggc19c, ggc20f, ggc21f, ggc22f, ggc23f, ggc24f, ggc25f, ggc26f, gsc04c, ggc28f, ggc29f, ggc30f, ggc31c, ggc32i, ggc33d, ggc34c, ggc35i, ggc36i, ggc37f, ggc38f, ggc48i, ggc81c, gsc00c, gsc01c, gsc02c, gsc03f, gsc04c, gsc05f, gsc06c, gsc07f, gsc08c, gsc09c, gsc10d, gsc11c, gsc12i from ggm03 b with (nolock), gsm03 c with (nolock), ggm01 a with (nolock) where a.gga00c = b.ggc00c and a.gga01c = b.ggc01c and a.gga00c = c.gsc00c and a.gga30c = c.gsc01c and c.gsc02c = @billtype and a.gga30c = @billid and a.gga00c = @compid order by ggc03c, ggc02f end else begin insert #ggm03_buf(ggc00c, ggc01c, ggc03c, ggc04c, ggc05f, ggc06c, ggc07f, ggc08f, ggc09f, ggc10f, ggc11f, ggc12c, ggc13f, ggc14f, ggc15c, ggc16f, ggc17c, ggc18f, ggc19c, ggc20f, ggc21f, ggc22f, ggc23f, ggc24f, ggc25f, ggc26f, ggc27c, ggc28f, ggc29f, ggc30f, ggc31c, ggc32i, ggc33d, ggc34c, ggc35i, ggc36i, ggc37f, ggc38f, ggc48i, ggc81c, gsc00c, gsc01c, gsc02c, gsc03f, gsc04c, gsc05f, gsc06c, gsc07f, gsc08c, gsc09c, gsc10d, gsc11c, gsc12i) select ggc00c, ggc01c, ggc03c, ggc04c, ggc05f, ggc06c, ggc07f, ggc08f, ggc09f, ggc10f, ggc11f, ggc12c, ggc13f, ggc14f, ggc15c, ggc16f, ggc17c, ggc18f, ggc19c, ggc20f, ggc21f, ggc22f, ggc23f, ggc24f, ggc25f, ggc26f, gsc04c, ggc28f, ggc29f, ggc30f, ggc31c, ggc32i, ggc33d, ggc34c, ggc35i, ggc36i, ggc37f, ggc38f, ggc48i, ggc81c, gsc00c, gsc01c, gsc02c, gsc03f, gsc04c, gsc05f, gsc06c, gsc07f, gsc08c, gsc09c, gsc10d, gsc11c, gsc12i from ggm03, gsm03 where ggc00c = @compid and ggc01c = @billid and ggc00c = gsc00c and ggc01c = gsc01c and gsc02c = @billtype order by ggc03c, ggc02f end update a set a.ggc05f = a.ggc05f * a.gsc05f/@pay_total_amt, a.ggc11f = a.ggc11f * (gsc05f/@pay_total_amt) from #ggm03_buf a if @billtype = 'gl' begin delete b from ggm03 b, ggm01 a where a.gga00c = b.ggc00c and a.gga01c = b.ggc01c and a.gga00c = @compid and a.gga30c = @billid end else begin delete ggm03 where ggc00c = @compid and ggc01c = @billid end insert ggm03(ggc00c, ggc01c, ggc02f, ggc03c, ggc04c, ggc05f, ggc06c, ggc07f, ggc08f, ggc09f, ggc10f, ggc11f, ggc12c, ggc13f, ggc14f, ggc15c, ggc16f, ggc17c, ggc18f, ggc19c, ggc20f, ggc21f, ggc22f, ggc23f, ggc24f, ggc25f, ggc26f, ggc27c, ggc28f, ggc29f, ggc30f, ggc31c, ggc32i, ggc33d, ggc34c, ggc35i, ggc36i, ggc37f, ggc38f, ggc48i, ggc81c) select ggc00c, ggc01c, ggc02f, ggc03c, ggc04c, ggc05f, ggc06c, ggc07f, ggc08f, ggc09f, ggc10f, ggc11f, ggc12c, ggc13f, ggc14f, ggc15c, ggc16f, ggc17c, ggc18f, ggc19c, ggc20f, ggc21f, ggc22f, ggc23f, ggc24f, ggc25f, ggc26f, ggc27c, ggc28f, ggc29f, ggc30f, ggc31c, ggc32i, ggc33d, ggc34c, ggc35i, ggc36i, ggc37f, ggc38f, ggc48i, ggc81c from #ggm03_buf drop table #ggm03_buf end GO /****** Object: StoredProcedure [dbo].[upg_updatecard_consumestat] Script Date: 06/12/2017 06:20:50 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_updatecard_consumestat] ( @compid varchar(10), @billid varchar(20), -- 收银单号 @cardno varchar(20), -- 卡号 @flag int -- 复核/取消复核 ) as begin declare @amt float declare @paymode varchar(5) declare @gcd02i int declare @gcd04f float declare cursor_01 cursor for select gsc04c, isnull(gsc05f, 0) from gsm03 where gsc00c = @compid and gsc01c = @billid and gsc02c = 'gx' open cursor_01 fetch cursor_01 into @paymode, @amt while @@fetch_status = 0 begin select @gcd02i = convert(int, gsn02c) from gsm14 where gsn01c = @paymode if (@flag = 1) begin if @amt > 0 begin select @gcd04f = gcd04f from gcm04 with (nolock) where gcd00c = @compid and gcd01c = @cardno and gcd02i = @gcd02i and isnull(gcd12i, 0) = 0 and gcd05i = 2 and gcd06f = @amt update gcm04 set gcd12i = 1, gcd18c = @billid where gcd00c = @compid and gcd01c = @cardno and gcd02i = @gcd02i and isnull(gcd12i, 0) = 0 and gcd05i = 2 and gcd06f = @amt and gcd04f = @gcd04f end end fetch cursor_01 into @paymode, @amt end close cursor_01 deallocate cursor_01 if (@flag = 0) begin update gcm04 set gcd12i = 0 where gcd00c = @compid and gcd01c = @cardno and isnull(gcd12i, 0) = 1 and gcd05i = 2 and gcd18c = @billid end end GO /****** Object: StoredProcedure [dbo].[upg_create_gsm03_from_gcm10] Script Date: 06/12/2017 06:19:53 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_create_gsm03_from_gcm10] ( @compid varchar(10), -- 公司别 @fromdate varchar(10), -- 开始日期 @todate varchar(10) ) as begin delete a from gsm03 a, gcm10 b where a.gsc00c = b.gcl00c and a.gsc01c = b.gcl01c and a.gsc02c = 'gz' and b.gcl02d >= @fromdate and b.gcl02d <= @todate and a.gsc00c = @compid and isnull(gcl10c, '') <> '' insert gsm03(gsc00c, gsc01c, gsc02c, gsc03f, gsc04c, gsc05f, gsc06c, gsc10d, gsc11c, gsc12i) select gcl00c, gcl01c, 'gz', 1, gcl10c, gcl07f-isnull(gcl13f, 0), gcl03c + '-' + gcl04c, gcl80d, gcl93c, gcl06i from gcm10 where gcl00c = @compid and (gcl06i = 0 or gcl06i = 6) -- 存款以及还款 and gcl02d >= @fromdate and gcl02d <= @todate and isnull(gcl10c, '')<>'' --会员账户管理中的欠款 insert gsm03(gsc00c, gsc01c, gsc02c, gsc03f, gsc04c, gsc05f, gsc06c, gsc10d, gsc11c, gsc12i) select gcl00c, gcl01c, 'gz', 2, '5', isnull(gcl13f, 0), gcl03c + '-' + gcl04c, gcl80d, gcl93c, gcl06i from gcm10 where gcl00c = @compid and (gcl06i = 0 or gcl06i = 6) -- 存款以及还款 and isnull(gcl13f, 0)>0 and gcl02d >= @fromdate and gcl02d <= @todate and isnull(gcl10c, '')<>'' --单独的在会员账户管理新建的欠款 insert gsm03(gsc00c, gsc01c, gsc02c, gsc03f, gsc04c, gsc05f, gsc06c, gsc10d, gsc11c, gsc12i) select gcl00c, gcl01c, 'gz', 1, '5', 0-isnull(gcl07f, 0), gcl03c + '-' + gcl04c, gcl80d, gcl93c, gcl06i from gcm10 where gcl00c = @compid and (gcl06i = 5) -- 欠款 and isnull(gcl11c, '') = '' and gcl02d >= @fromdate and gcl02d <= @todate and isnull(gcl10c, '')<>'' --取款 insert gsm03(gsc00c, gsc01c, gsc02c, gsc03f, gsc04c, gsc05f, gsc06c, gsc10d, gsc11c, gsc12i) select gcl00c, gcl01c, 'gz', 1, gcl10c, 0-isnull(gcl07f, 0), gcl03c + '-' + gcl04c, gcl80d, gcl93c, gcl06i from gcm10 where gcl00c = @compid and (gcl06i = 1) -- 取款 and gcl02d >= @fromdate and gcl02d <= @todate and isnull(gcl10c, '')<>'' end GO /****** Object: UserDefinedFunction [dbo].[memo_gsm03] Script Date: 06/12/2017 06:20:58 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO --创建一个函数 Create function [dbo].[memo_gsm03] ( @gsc00c varchar(20) , @gsc01c varchar(20), @gsc02c varchar(20) ) returns varchar(20) as begin declare @str varchar(20) select @str = isnull(@str+':','') + cast(gsc04c as varchar) + ';' + cast(gsc05f as varchar) from gsm03 with (nolock) where gsc00c = @gsc00c and gsc01c =@gsc01c and gsc02c = @gsc02c return @str end GO /****** Object: StoredProcedure [dbo].[upg_m_dispatch_gzm07] Script Date: 06/12/2017 06:20:20 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_m_dispatch_gzm07] ( @fromcompid varchar(10), -- 从哪一个公司发布 @tocompid varchar(10), -- 目的公司 @mandatory varchar(2) -- 是否强制统一 ) as begin if @fromcompid = @tocompid return if @mandatory <> '1' and @mandatory <> '0' return if @mandatory = '1' begin delete gzm07 where gzg00c = @tocompid insert gzm07(gzg00c, gzg01c, gzg02c, gzg03c, gzg04c, gzg05c) select @tocompid, gzg01c, gzg02c, gzg03c, gzg04c, gzg05c from gzm07 where gzg00c = @fromcompid return end create table #gzm07_key -- 赠送抵用券资料关键字 ( gzg01c varchar(5) not null -- 抵用券类别 ) insert into #gzm07_key(gzg01c ) select gzg01c from gzm07 with (nolock) where gzg00c = @fromcompid declare @current_gzg01c varchar(5) -- 赠券类别 -- 创建循环变量 declare @HasRecord int set @HasRecord = 1 declare @Index int set @Index = -1 while(@HasRecord = 1) begin if exists ( select 1 from #gzm07_key) begin set @HasRecord = 1 select top 1 @current_gzg01c = gzg01c from #gzm07_key if not exists (select 1 from gzm07 with (nolock) where gzg00c = @tocompid and gzg01c = @current_gzg01c) begin insert gzm07(gzg00c, gzg01c, gzg02c, gzg03c, gzg04c, gzg05c) select @tocompid, gzg01c, gzg02c, gzg03c, gzg04c, gzg05c from gzm07 with (nolock) where gzg00c = @fromcompid and gzg01c = @current_gzg01c end delete from #gzm07_key where gzg01c = @current_gzg01c end else begin set @HasRecord = 0 end end drop table #gzm07_key end GO /****** Object: StoredProcedure [dbo].[upg_getonsales_info] Script Date: 06/12/2017 06:20:10 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_getonsales_info] ( @compid varchar(10), -- 公司别 @billtype varchar(5), -- 单据类别 @billno varchar(20), -- 单据编号 @billdate varchar(10), -- 日期 @cardno varchar(20), -- 卡号 @flag int output -- 0-不需要促销 1-需要促销 只会在insert into gdm10成功后才会是1 ) as begin set @flag = 0 if (isnull(@billtype, '') = '' or isnull(@billno, '') = '') return create table #billproject ( projectno varchar(20), --疗程编号 projecttype varchar(5), --疗程类别 projectamt float, --疗程金额 projectflow int , --疗程序号 ) create table #gdm21 ( gdu00c varchar(10) not null, -- 公司编号 gdu01c varchar(20) not null, -- 促销编号 gdu02f float not null, -- 促销子编号 gdu03i int not null, -- 促销依据 1按项目 + 价格序号 2按类别合计金额 gdu04c varchar(20) not null, -- 项目编号 促销依据 1按项目 + 价格序号才用意义否则在后台用SHBOKA来填充 gdu05f float not null, -- 当促销依据是:1按项目 + 价格序号的时候 价格序号 9代表所有价格 是2按类别合计金额--分段序号 gdu06f float null, -- 达到金额 gdu07i int null, -- 本人赠送帐户标志 gdu08i int null, -- 本人赠送套餐标志 gdu10i int null, -- 本人帐户代码 gdu11i int null, -- 本人帐户赠送类别 1-:固定金额 2:比率 gdu12f float null, -- 本人帐户金额或比率 gdu13i int null, -- 介绍人赠送帐户标志 gdu14i int null, -- 介绍人赠送套餐标志 gdu16i int null, -- 介绍人帐户代码 gdu17i int null, -- 介绍人帐户赠送类别 1:固定金额 2:比率 gdu18f float null, -- 介绍人帐户金额 gdu19c varchar(60) null, -- 备注 gdu20c varchar(20) null, -- 项目类别 gdu21c varchar(20) null, -- 本人套餐编号 gdu22c varchar(20) null, -- 介绍人套餐编号 ) create table #gdm11 -- 单据实际促销-帐户 --建立这个表的原因 是因为同帐户的金额要合并 ( gdk00c varchar(10) null, -- 公司编号 gdk01c varchar(5) null, -- 单据类别 gdk02c varchar(20) null, -- 单号 gdk03i int null, -- 赠送原因 1-本人 2介绍人 gdk04c varchar(5) null, -- 帐户代码 gdk05f float null, -- 帐户金额 ) create table #gdm12 -- 单据实际套餐 建立这个表的原因 是因为同项目合并 ( gdl00c varchar(10) null, -- 公司编号 gdl01c varchar(5) null, -- 单据类别 gdl02c varchar(20) null, -- 单号 gdl03i int null, -- 赠送原因 1-本人 2介绍人 gdl04c varchar(20) null, -- 套餐编号 gdl05f float null, -- 套餐次数 ) -- 如果现在同时有几种促销方案那么选择top1 declare @saleno varchar(20) -- 促销方案编号 select top 1 @saleno = gdt01c from gdm20 where gdt00c = @compid and gdt03i = 1 and @billdate between gdt04d and gdt05d -- 如果这段时间没有促销方案就返回 if (isnull(@saleno, '') = '') return ---------把此促销方案的2级明细insert 到临时表中---------------- insert into #gdm21(gdu00c, gdu01c, gdu02f, gdu03i, gdu04c, gdu05f, gdu06f, gdu07i, gdu08i, gdu10i, gdu11i, gdu12f, gdu13i, gdu14i, gdu16i, gdu17i, gdu18f, gdu19c, gdu20c, gdu21c, gdu22c) select gdu00c, gdu01c, gdu02f, gdu03i, gdu04c, gdu05f, gdu06f, gdu07i, gdu08i, gdu10i, gdu11i, gdu12f, gdu13i, gdu14i, gdu16i, gdu17i, gdu18f, gdu19c, gdu20c, gdu21c, gdu22c from gdm21 where gdu00c = @compid and gdu01c = @saleno if @@rowcount = 0 return declare @gdu03i int -- 促销依据 0按项目 + 价格序号 1按类别合计金额 declare @gdu04c varchar(20) -- 项目编号 促销依据 0按项目 + 价格序号才用意义否则在后台用SHBOKA来填充 declare @gdu05f float -- 当促销依据是:0按项目 + 价格序号的时候 价格序号 9代表所有价格 是1按类别合计金额--分段序号 declare @gdu06f float -- 达到金额 declare @gdu07i int -- 本人赠送帐户标志 declare @gdu08i int -- 本人赠送套餐标志 declare @gdu10i int -- 本人帐户代码 declare @gdu11i int -- 本人帐户赠送类别 1-:固定金额 2:比率 declare @gdu12f float -- 本人帐户金额或比率 declare @gdu13i int -- 介绍人赠送帐户标志 declare @gdu14i int -- 介绍人赠送套餐标志 declare @gdu16i int -- 介绍人帐户代码 declare @gdu17i int -- 介绍人帐户赠送类别 1:固定金额 2:比率 declare @gdu18f float -- 介绍人帐户金额 declare @gdu20c varchar(20) -- 项目类别 declare @gdu19c varchar(60) -- 备注 declare @gdu21c varchar(20) -- 本人套餐编号 declare @gdu22c varchar(20) -- 介绍人套餐编号 -- 相同的促销方案只有同种促销策略 declare @saletype int select @saletype = isnull(gdu03i, 0) from #gdm21 if (@billtype = 'gn') --会员卡销售 begin insert into #billproject(projectno, projecttype, projectamt, projectflow) select gnc02c, gda13c, gnc05f, gnc09i from gnm03, gdm01 where gda00c = gnc00c and gda01c = gnc02c and gnc00c = @compid and gnc01c = @billno end else if (@billtype = 'gz') --处理帐户异动 begin insert into #billproject(projectno, projecttype, projectamt, projectflow) select gcm02c, gda13c, gcm05f, gcm09i from gcm11, gdm01 where gda00c = gcm00c and gda01c = gcm02c and gcm00c = @compid and gcm01c = @billno end declare @projectno varchar(20) --项目编号 declare @projecttype varchar(5) --项目类别 declare @typeamt float --当促销方案是同类项目合计的时候是同类项目总金额 否则是项目的金额 (只有在同类项目合计的时候才有意思) declare @sendamt float --应该总送的金额 declare @projectflow int --价格序号 --只有在按项目 + 价格序号的时候才有用的 if (@saletype = 1)--1按项目 + 价格序号 begin declare cursor_01 cursor for select projectno, projectflow, projectamt from #billproject open cursor_01 fetch cursor_01 into @projectno, @projectflow, @typeamt end else if (@saletype = 2) begin declare cursor_01 cursor for select sum(isnull(projectamt, 0)), projecttype from #billproject group by projecttype open cursor_01 fetch cursor_01 into @typeamt, @projecttype end while @@fetch_status = 0 begin if (@saletype = 1)--1按项目 + 价格序号 begin select @gdu06f = gdu06f, @gdu07i = gdu07i, @gdu08i = gdu08i, @gdu10i = gdu10i, @gdu11i = gdu11i, @gdu12f = gdu12f, @gdu13i = gdu13i, @gdu14i = gdu14i, @gdu16i = gdu16i, @gdu17i = gdu17i, @gdu18f = gdu18f, @gdu19c = gdu19c, @gdu21c = gdu21c, @gdu22c = gdu22c from #gdm21 where gdu03i = @saletype and gdu04c = @projectno and gdu05f = @projectflow end else if (@saletype = 2) begin select top 1 @gdu06f = gdu06f, @gdu07i = gdu07i, @gdu08i = gdu08i, @gdu10i = gdu10i, @gdu11i = gdu11i, @gdu12f = gdu12f, @gdu13i = gdu13i, @gdu14i = gdu14i, @gdu16i = gdu16i, @gdu17i = gdu17i, @gdu18f = gdu18f, @gdu19c = gdu19c, @gdu20c = gdu20c, @gdu21c = gdu21c, @gdu22c = gdu22c from #gdm21 where gdu03i = @saletype and gdu20c = @projecttype and (gdu06f < @typeamt or gdu06f > @typeamt) order by gdu06f desc end -------------------以下是处理本人的-明细----------------------------------- --处理本人帐户 if (isnull(@gdu07i, 0) = 1 and isnull(@gdu12f, 0)>0) begin if (isnull(@gdu11i, 0) = 1) begin set @sendamt = @gdu12f end else begin set @sendamt = @gdu12f*@typeamt end insert into #gdm11(gdk00c, gdk01c, gdk02c, gdk03i, gdk04c, gdk05f) values(@compid, @billtype, @billno, 1, @gdu10i, @sendamt) end ---处理本人套餐 if (isnull(@gdu08i, 0) = 1) begin insert into #gdm12 (gdl00c, gdl01c, gdl02c, gdl03i, gdl04c, gdl05f) values (@compid, @billtype, @billno, 1, @gdu21c, 1) end ---------------以下是处理介绍人的--明细--------------------------------- --处理本人帐户 if (isnull(@gdu13i, 0) = 1 and isnull(@gdu18f, 0)>0) begin if (isnull(@gdu17i, 0) = 1) begin set @sendamt = @gdu18f end else begin set @sendamt = @gdu18f*@typeamt end insert into #gdm11(gdk00c, gdk01c, gdk02c, gdk03i, gdk04c, gdk05f) values(@compid, @billtype, @billno, 2, @gdu16i, @sendamt) end ---处理介绍人套餐 if (isnull(@gdu14i, 0) = 1) begin insert into #gdm12 (gdl00c, gdl01c, gdl02c, gdl03i, gdl04c, gdl05f) values (@compid, @billtype, @billno, 2, @gdu22c, 1) end if (@saletype = 2) begin fetch cursor_01 into @typeamt, @projecttype end else if (@saletype = 1)--1按项目 + 价格序号 begin fetch cursor_01 into @projectno, @projectflow, @typeamt end end close cursor_01 deallocate cursor_01 --开始合并帐户相同的 delete gdm11 where gdk00c = @compid and gdk01c = @billtype and gdk02c = @billno insert into gdm11(gdk00c, gdk01c, gdk02c, gdk03i, gdk04c, gdk05f) select gdk00c, gdk01c, gdk02c, gdk03i, gdk04c, sum(gdk05f) from #gdm11 group by gdk00c, gdk00c, gdk01c, gdk02c, gdk03i, gdk04c --开始合并套餐相同的 delete gdm12 where gdl00c = @compid and gdl01c = @billtype and gdl02c = @billno insert into gdm12 (gdl00c, gdl01c, gdl02c, gdl03i, gdl04c, gdl05f) select gdl00c, gdl01c, gdl02c, gdl03i, gdl04c, sum(gdl05f) from #gdm12 group by gdl00c, gdl01c, gdl02c, gdl03i, gdl04c --------insert 单据实际促销--主档-------------------- --主档放在这个地方 是因为如果有促销设定,但是没有找到那么就返回 ---insert 本人的 delete gdm10 where gdj00c = @compid and gdj01c = @billtype and gdj02c = @billno if (exists(select 1 from #gdm11 where gdk03i = 1) or exists(select 1 from #gdm12 where gdl03i = 1) ) begin insert gdm10(gdj00c, gdj01c, gdj02c, gdj03c, gdj04i, gdj05i, gdj06c, gdj07c, gdj08i) values(@compid, @billtype, @billno, @cardno, 1, 0, @billdate, @saleno, @saletype) set @flag = 1 end ----insert 介绍人的 if (exists(select 1 from #gdm11 where gdk03i = 2) or exists(select 1 from #gdm12 where gdl03i = 2) ) begin insert into gdm10(gdj00c, gdj01c, gdj02c, gdj03c, gdj04i, gdj05i, gdj06c, gdj07c, gdj08i) values(@compid, @billtype, @billno, @cardno, 2, 0, @billdate, @saleno, @saletype) set @flag = 1 end delete #billproject delete #gdm11 delete #gdm12 delete #gdm21 end GO /****** Object: StoredProcedure [dbo].[upg_refresh_hem02] Script Date: 06/12/2017 06:20:34 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_refresh_hem02] ( @compid varchar(10) ) as begin select hea00c, haa01c, hea01c, hea02i, hea03f, hea04i, hea05f, hea06i, hea07f, hea08i, hea09f, hea14i, hea15f, hea16i, hea17f, hea10i, hea11f, hea20i, hea21f, hea22i, hea23f, hea25i, hea26f, hea27i, hea28f into #tmp_hem02 from hem01 a, ham01 b where a.hea00c = b.haa00c and a.hea18i = 2 and a.hea00c = @compid and (isnull(a.hea02i, 0) <> 0 or isnull(a.hea03f, 0) <> 0 or isnull(a.hea04i, 0) <> 0 or isnull(a.hea05f, 0) <> 0 or isnull(a.hea06i, 0) <> 0 or isnull(a.hea07f, 0) <> 0) delete a from #tmp_hem02 a, hem02 b where a.hea00c = b.heb00c and a.haa01c = b.heb01c and a.hea01c = b.heb02c and b.heb00c = @compid insert hem02(heb00c, heb01c, heb02c, heb03i, heb04f, heb05i, heb06f, heb07i, heb08f, heb09i, heb10f, heb11i, heb12f, heb13i, heb14f, heb15i, heb16f, heb17i, heb18f, heb19i, heb20f, heb21i, heb22f, heb23i, heb24f) select hea00c, haa01c, hea01c, hea02i, hea03f, hea04i, hea05f, hea06i, hea07f, hea08i, hea09f, hea14i, hea15f, hea16i, hea17f, hea10i, hea11f, hea20i, hea21f, hea22i, hea23f, hea25i, hea26f, hea27i, hea28f from #tmp_hem02 drop table #tmp_hem02 end GO /****** Object: StoredProcedure [dbo].[upg_update_one_hem02] Script Date: 06/12/2017 06:20:50 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_update_one_hem02] ( @compid varchar(10), @staff varchar(20) ) as begin delete hem02 where heb00c = @compid and heb01c = @staff insert hem02(heb00c, heb01c, heb02c, heb03i, heb04f, heb05i, heb06f, heb07i, heb08f, heb09i, heb10f, heb11i, heb12f, heb13i, heb14f, heb15i, heb16f, heb17i, heb18f, heb19i, heb20f, heb21i, heb22f, heb23i, heb24f) select hea00c, @staff, hea01c, hea02i, hea03f, hea04i, hea05f, hea06i, hea07f, hea08i, hea09f, hea14i, hea15f, hea16i, hea17f, hea10i, hea11f, hea20i, hea21f, hea22i, hea23f, hea25i, hea26f, hea27i, hea28f from hem01 a where a.hea18i = 2 and a.hea00c = @compid and (isnull(a.hea02i, 0)<>0 or isnull(a.hea03f, 0)<>0 or isnull(a.hea04i, 0)<>0 or isnull(a.hea05f, 0)<>0 or isnull(a.hea06i, 0)<>0 or isnull(a.hea07f, 0)<>0) end GO /****** Object: UserDefinedFunction [dbo].[fun_combine_gsm04_gsd05c] Script Date: 06/12/2017 06:20:57 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create function [dbo].[fun_combine_gsm04_gsd05c] ( @gsd00c varchar(10), --公司别 @gsd01c varchar(20), -- 单据/资料编号 @gsd02c varchar(5), -- 单据/资料类别 @gsd03c char(1)) -- 类别: 1.表头 2.表尾 3.参考 4, 评测结果 5, 运动处方 6-公司简介 7- 人简介/简历 returns varchar(1000) as -- gsm04.gsd05c 字符拼接函数,一列多行合并成一行 begin declare @str varchar(1000) set @str = '' select @str = @str + gsd05c from gsm04 where gsd00c = @gsd00c and gsd01c = @gsd01c and gsd02c = @gsd02c and gsd03c = @gsd03c return @str end GO /****** Object: StoredProcedure [dbo].[upg_m_dispatch_member_enter_parameter] Script Date: 06/12/2017 06:20:21 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_m_dispatch_member_enter_parameter] ( @fromcompid varchar(10), -- 从哪一个公司发布 @tocompid varchar(10), -- 目的公司 @mandatory varchar(2) -- 是否强制统一 ) as begin if @fromcompid = @tocompid return if @mandatory <> '1' and @mandatory <> '0' return if @mandatory = '1' begin if exists(select 1 from member_enter_parameter where mep00c = @tocompid) begin update a set a.mep01i = b.mep01i, a.mep02c = b.mep02c, a.mep03i = b.mep03i, a.mep04c = b.mep04c, a.mep05i = b.mep05i, a.mep06i = b.mep06i from member_enter_parameter a, member_enter_parameter b where a.mep00c = @tocompid and b.mep00c = @fromcompid return end end insert member_enter_parameter(mep00c, mep01i, mep02c, mep03i, mep04c, mep05i, mep06i) select @tocompid, mep01i, mep02c, mep03i, mep04c, mep05i, mep06i from member_enter_parameter where mep00c = @fromcompid end GO /****** Object: StoredProcedure [dbo].[upg_get_potential_member_enter_remind] Script Date: 06/12/2017 06:20:08 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_get_potential_member_enter_remind] ( @compid varchar(10) -- 公司 ) as begin -- 创建临时表 create table #result ( compid varchar(20) null, -- 门店编码 cardid varchar(50) null, -- 会员卡号 membername varchar(50) null, -- 会员名 owecompid varchar(20) null, -- 卡归属门店编码 mobile varchar(20) null, -- 手机号码 sendsms int null, -- 是否已发短信 ismember int null, -- 是否是会员 bememberdate datetime null -- 成为会员的日期 ) declare @mep07i int select @mep07i = mep07i from member_enter_parameter where mep00c = @compid set @mep07i = isnull(@mep07i, 180) declare @start datetime set @start = dateadd(ss, -@mep07i, getdate()) declare @date varchar(10) set @date = convert(varchar(10), getdate(), 112) insert into #result(compid, mobile) select distinct comp, mobile from potential_member_enter where comp = @compid and entertime >= @start and convert(varchar(10), entertime, 112) = @date update a set a.ismember = b.ismember, a.bememberdate = b.bememberdate from #result a, potential_member b with(nolock) where a.mobile = b.mobile update a set a.owecompid = b.gca13d, a.cardid = b.gca01c, a.membername = c.gba03c from #result a, gcm01 b with(nolock), gbm01 c with(nolock) where a.mobile = c.gba08c and b.gca00c = b.gca13d and b.gca00c = c.gba00c and b.gca01c = c.gba23c update a set a.sendsms = b.sendsms from #result a, potential_member_enter_sms b with(nolock) where a.compid = b.compid and a.mobile = b.mobile and b.enterdate = @date select compid, cardid, membername, owecompid, mobile, sendsms, ismember, bememberdate from #result drop table #result end GO /****** Object: StoredProcedure [dbo].[upg_get_member_enter_remind] Script Date: 06/12/2017 06:20:08 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_get_member_enter_remind] ( @compid varchar(10) -- 公司 ) as begin -- 创建临时表 create table #unread_shop_remind_idd ( id int not null ) declare @mep07i int select @mep07i = mep07i from member_enter_parameter where mep00c = @compid set @mep07i = isnull(@mep07i, 180) declare @start datetime set @start = dateadd(ss, -@mep07i, getdate()) declare @date varchar(10) set @date = convert(varchar(10), getdate(), 112) insert into #unread_shop_remind_idd(id) select id from shop_remind where compid = @compid and createdate >= @start and convert(varchar(10), createdate, 112) = @date and isnull(status, 0) = 0 update shop_remind set status = 1 where id in (select id from #unread_shop_remind_idd) create table #result ( id int not null, -- ID compid varchar(20) null, -- 门店编码 cardid varchar(50) null, -- 会员卡号 membername varchar(50) null, -- 会员名 owecompid varchar(20) null, -- 卡归属门店编码 mobile varchar(20) null, -- 门店编码 sendsms int null -- 是否已发短信 ) insert into #result(id, compid, cardid) select distinct id, compid, cardid from shop_remind where id in (select id from #unread_shop_remind_idd) update a set a.owecompid = b.gca13d, a.mobile = c.gba08c, a.membername = c.gba03c from #result a, gcm01 b with(nolock), gbm01 c with(nolock) where a.cardid = b.gca01c and b.gca00c = b.gca13d and b.gca00c = c.gba00c and b.gca04c = c.gba01c update a set a.sendsms = b.sendsms from #result a, member_enter_sms b with(nolock) where a.compid = b.compid and a.cardid = b.cardid and b.enterdate = @date select id, compid, cardid, membername, owecompid, mobile, sendsms from #result drop table #result drop table #unread_shop_remind_idd end GO /****** Object: StoredProcedure [dbo].[upg_init_gam21] Script Date: 06/12/2017 06:20:13 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_init_gam21] as begin if not exists(select 1 from gam21) begin insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GFB01I', '本模块用于管理店里中所有的支出情况,存档后此笔金额即已进入支出帐上。收费项目的类别可在常用资料, 中添加修改。') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GFB01R', '以日期范围来查看费用支出统计情况') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GFB02R', '以收费项目类别来查看费用支出统计情况') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GHA01I', '"用于员工的考勤,前面班次,排班均已设定,在此即可进行员工考勤, 员工上班的时候记录一下,下班的时候", 在记录一下来考勤') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GHA02I', '用于员工薪资的计算维护。通过员工工资设定的项目,这里系统会自动根据计薪时间计算员工的薪资。计算, 之后可以到薪资调整页面去调整薪资。') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GHA03I', '用于完成薪资发放的发放工作。通过薪资计算,再通过薪资调整确定无误后,就可以完成薪资发放。确认信, 息无误后,点击页面右下侧[批准]按钮,完成薪资发放。') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GHA04I', '用于对员工薪资计算的确认。如果发现有些员工有其它的项目需要补充或修改,这时可以在此进行修改更正, 。') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GHA06I', '本模块用于院长设置员工某个月的评分') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GHA04R', '"用于查看员工工资项目的具体情况。包括:基本工资,交通费补助,店长奖金,业绩提成,日常奖罚,所得 ", 税,实发工资等项目。') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GHB01I', '设定工作岗位的基本工资,补贴,奖罚等,其中计算方式还分固定和系统计算,为你提供便利的设置') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GHB01R', '用于员工一周所做业绩的统计查询。') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GHB02R', '用于员工一周所做业绩的统计查询(专业)。') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GHB02I', '用于工作岗位薪资的设定。这里可以设定店中每个岗位不同的薪资计算方式,可以为员工薪资设定提供参考, 和快速调用') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GHB03I', '员工修改密码及修改员工卡的密码') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GIA03R', '查看顾客一段时间范围内的详细消费历史。报表内容可根据需要进行选择,还可以将这些会员加入短信名单, 中。') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GIA04I', '本模块用于对顾客消费收银结帐进行维护管理作业。') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GIA04R', '查看收银员给顾客折扣的原因。') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GIA05I', '此模块用于扣除会员卡本次消费金额的情况进行维护操作。当[基本设定-启动参数设置]会员卡类型选择IC卡, 异步方可生效,反之,此模块无效') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GIA06I', '此模块用于扣除会员卡本次消费金额的情况进行维护操作。当[基本设定-启动参数设置]会员卡类型选择非IC卡异步方可生效,反之,此模块无效。') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GIB05I', '用于把一段时间范围内的所以帐务全部冻结,使其不能作新增、删除、修改等操作。') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GIB07R', '统计一段时间内,每一天的总收入和收入来源。') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GIB08R', '统计一段时间内各分店以支付方式和服务类别为基础记录详细的收入和支出。') --insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GIC01I', '"本模块用于对本院系统中项目预约的管理, 以便于管理人员及时了解系统中项目的预约情况。"') --insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GIC01R', '查看一段时间范围内客户、员工、房间等预约情况。') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GIC02I', '管理本店系统中房间资料的设定') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GIC03I', '用于工作岗位班次的设定,是员工排班的前提。这里可以按照本店中的实际班次,上下班时间依次设定。') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GIC04I', '这里可以查看房间、床位、员工的预约空闲情况。可以对非指定的员工进行排工安排。如果会员指定预约,, 则可以查看在预约时间段员工是否空闲,便于人工调工,安排会员的预约') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GIC05I', '用于员工排班的设定,设定好后,即可进行考勤操作。这里即是员工进行考勤的依据。可以通过调整日期,, 分不同时段进行设定。') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GWA11I', '设定员工薪资项目, 计算薪资') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GID01R', '查看支付方式是‘经理签单’时的原因') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GMA01I', '本模块用于卖卡和对会员卡销售时的相关信息进行操作维护。') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GMA01R', '给顾客打印的消费小单。') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GMA02I', '"入店是充分的了解客户, 包括上次来店时间、消费过的项目、购买过的产品、前次服务的过程与服务的效果等", ,此功能模块只有美容版本才有。') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GMA02R', '"查看一天的营业状况。如 现金、银行卡等的收入情况"') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GMA03I', '本模块主要用于对店里的顾客出院服务进行维护管理。可以在次记录对客人的操作过程和美容效果,在客人再次入院的时候可以可以查看到所做的记录。') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GMA04I', '本模块用于建立会员的美容档案,可以进行查询和修改。此窗口可以查询会员来店咨询资料,美容后的真实, 感受,以及美容师为会员建议的美容处方。') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GMA04R', '对已经产生的美容处方列印报表的功能。查询美容师为做过美容咨询的会员或散客开的处方单。为会员提供, 参考,备案。') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GMA06R', '查看会员的余额、消费历史等资料情况。') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GMA07R', '用于对员工业绩的查询,查看员工当前业绩、提成情况。包括卖卡,作项目和产品销售三部分。') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GMA08R', '对员工业绩进行排序,从多到少,可以清楚的看到员工对店里的贡献。包括卡销售,项目服务和产品销售。') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GMA09R', '根据需要,把会员姓名、地址和邮编打印到信封或者A4纸上。增加与会员的互动。') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GMA10R', '查看哪些会员好久没有来了, 并显示最后到店消费时间、最后回访时间。') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GMA11R', '对员工完成业绩指标的情况进行分析,可以分时段进行分析。') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GMA14R', '用于每天营业结束,员工查询一天工作的业绩,以单据的形式打印出来。') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GMB01I', '为会员卡开卡,将会员卡和会员的基本资料联系起来,起到一个桥梁的作用。会员只有进行开卡才能进行收, 银操作。(一般设为自动开卡,就不需要再到此模块开卡)') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GMB02R', '统计疗程项目的销售情况') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GMB03R', '统计一段时间内疗程项目类别的销售以及消耗情况') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GMB04R', '统计一段时间内疗程项目的消耗情况') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GMB09I', '本模块用于维护公司系统中会籍管理的相关作业。经由本模块可以进入会籍延期、会籍保留、会籍转让、卡, "种变更、 遗失补卡、会员退卡等相关作业。"') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GMB11I', '查询会员的回访结果,并对未回访的客户安排回访时间或执行回访。') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GMB12I', '当店中有新的服务项目推出时,或是店中做促销,或在客户生日时店中表示祝贺并针对此类会员有优惠政策, 需要通知会员时,可以通过短信形式及时传达给会员。') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GMB13R', '打印一个会员的详细医学诊断资料表格。') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GMC01I', '本模块用于会员卡充值还款等操作。同时可对会员卡储值账户、购买美容疗程作业进行维护管理。在此作业, 中可以对会员卡储值账户中的金额/美容疗程进行充值或提取。') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GMC01R', '某个日期范围内会员卡的各个账户的消费、存款、还款等的统计情况') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GMC03I', '用于纪录会员对本店的各个项目的满意程度。根据此信息,本院的管理人员可以及时对本店的项目管理和人, 事管理进行调整。这里可以加强员工服务') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GMC03R', '此模块用于查看会员欠款情况。') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GMC04R', '主要统计连锁分店间,跨店消费情况,可以统计本店的会员在其他分店的消费情况,也可以统计别的店的会, 员在本店的消费情况。') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GSA01I', '设定店的基本情况,其中本店名称(中),电话,网址,公司地址这四项会显示在打印的小票单据上。') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GSA02I', '启动参数是需要在开始使用本系统的时候设置,而且作为启动参数,关系到系统的数据的一致性') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GSA05I', '本系统绝大多数有下拉框选择的内容可以在此设置,支付方式和账户类别不可以随意修改,建议这两项不要, 去改动') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GSA08I', '用于设置店里的会员卡种类,以及该种类的会员卡的有效期限、标准售价、储值金额等') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GSA09I', '设置店内员工的基本信息,员工编号建立后不要随意修改,如果有员工离开,不要删除这个员工的资料,可, 以把他的‘’目前状况‘’修改成离职') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GSA11I', '用于管理顾客做项目时的折扣的设定,可以按照日期和周次来设定折扣;且折扣分为一般折扣和会员等级的, 折扣。') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GSA12I', '用于管理顾客购买产品时的折扣的设定,可以按照日期和周次来设定折扣;且折扣分为一般折扣和会员等级, 的折扣。') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GSA13I', '本模块主要用于设定本店正常营业时,实际业务变化所需要的参数。参数设置好之后,在以后的各个模块中, 的参数就直接读取设定的参数') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GSA15I', '针对会员卡销售时候的折扣,设置是依据会员卡类别来设置的') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GSA16I', '设定本软件使用者及不同使用者的不同权限,使用者可以按照店长,收银员,财务等角色不同划分,不同收, 银员对同一操作又不同权限,比如收银操作中是否有权利打折操作等') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GSB01I', '通过本系统出售或通过会员卡开帐产生的会员卡都会显示在此,此处包括会员卡种类,余额,会员卡交易历, 史,账户历史等信息') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GSB02I', '设定管理本店系统中服务项目及服务项目的价格') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GSB04I', '"设定管理本店系统中产品的价格和产品相关信息 "') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GSB06I', '本模块主要用于会员原始资料的建立及维护作业。包括会员基本信息,生活状况,曾经的历程,现在的感受, ,明天的期望,以及交易消费历史等模组') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GSB08I', '用于维护店内员工资料进行查询,以及在此作业中对个人资料进行修改的相关作业。') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GSB09I', '用于对本店所设的各种产品资料的基本情况标准价格进行查询的作业。') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GSB12I', '主要用于美容院会员美容咨询历史的查询。') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GSB13I', '用于更改会员卡的密码设定。新开卡的原始密码可在[基本设定]中[系统参数]里查到。') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GSB14I', '用于维护本店资料进行查询,以及在此作业中对本店的资料进行修改的相关作业。') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GSB15I', '用于维护店内会员对会员卡及消费资料进行查询,以及在此作业中对个人资料进行修改的相关作业。') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GSB17I', '用于对本店所设的各种服务项目的基本情况标准价格进行查询的作业。') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GSB20I', '设置产品的供应商信息') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GSD01I', '设置套餐的基本信息') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GSB21I', '设置店内员工做项目可以得到多少相应的提成') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GSB22I', '用户可以通过此操作,把没有使用本软件前就已经卖给客户的卡在此进行登记,这样既不会产生现金等帐目, 又使以前卖出去的卡可以在本系统中使用') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GSB23I', '店内在产品管理里面可以在此设置做什么项目消耗多少产品,以跟踪店内项目的产品消耗情况') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GSB24I', '此处可做操作:彻底删除会一张或多张会员卡;删除一段时间内的收银单据;修改会员卡的状态') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GSB25I', '是用于连锁店之间宽带直连模式,有什么通告、通知等可利用此模块传达。') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GSC01R', '以[统计分类]的类别分别统计一天中做该类别的人次和总金额。') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GSC02R', '以单个项目为基础,按[统计分别]统计。是月报表里的一天的详细的信息。') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GSC05R', '统计只做[IC卡销卡]的销卡,还未在[收银]里记录具体项目的历史记录。用于[IC卡(异步)]。') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GWA01I', '本模块用于管理系统中产品的入库。所有的产品在入库时都要在此作业出一张入库单,以便日后进行财务成, 本核算。') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GWA02I', '"本模块主要用于对仓库产品的数量进行盘点, 把盘点结果登记到此作业的盘点单中,然后对差异数量不为零的", 产品重新出库或入库,在出库或入库作业中选择方式为「盘点冲抵」') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GWA02R', '根据会员卡消售情况数据,系统自动产生当日或者一段日期内的会员卡销售报表!') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GWA03I', '本模块用于管理系统中产品的出库、供应商退货、调拨出库、损坏、赠送以及用来冲抵盘点单中差异数量为, 负的产品,仓库中所有的产品在出库时都要在此进行出库单的作业。') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GWA04R', '根据当日或一段日期内的会员卡的销售及变更情况产生的数据,系统自动产生相应报表!') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GWA06R', '根据现有的会员所拥有的会员卡,分析每个类别会员卡数量占总卡量的百分比。') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GWA07R', '根据现有的会员资料,分析会员年龄层次比例和男女比例。') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GWA15R', '在一段时间内,产品入库的总和的统计') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GWA16R', '在一段时间内,产品出库的总和的统计') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GWA17R', '统计一段时间内卖品的销售量的统计,这里的数据来自于是在[收银管理]中的[收银]里的[购买产品]里,卖出的, 产品') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GWA18R', '指到某一个时刻仓库里的产品的总的存量。') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GWA19R', '根据以前的会员消费数据,分析会员消费总金额,并以降序的方式排列会员消费金额。') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GWA20I', '是在三个时段里,卡销售、冲值、现金消费和卡消费的一个对比。可以以某一个员工的业绩来比较。') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GWA20R', '统计[IC卡销卡]中的的销卡历史记录。') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GWA21R', '显示一段时间内,所有出库的单据。和在入库的时候打印的单据一样。') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GWA22I', '统计在[分析期间]中,统计消费前10名的项目,以金额或数量排名。') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GWA22R', '根据产品入库、销售、消耗、出库等变更情况数据,系统自动产生相应报表,便于用户了解。') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GWA23I', '统计在[分析期间]中,统计消费前10名的产品,以金额或数量排名。') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GWA23R', '显示一段时间内,所有入库单据。和在入库的时候打印的单据一样。') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GWA24R', '分析一段时间内,项目的产品消耗数量。') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GWA25R', '统计一段时间以支付方式和服务类别为基础记录详细的收入和支出。') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GWA26R', '提供店里产品的最低存量的提示。先在[基本设定]的[产品资料]里,每一个产品都设一个安全存量。在那里设, 置好了以后就可以来这里查询了。只显示存量已低于该产品设') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GWA27R', '统计一段时期内,产品入库总量、出库总量和期末量。') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GWA28R', '查询一段时间内从入库到现在都没有销售、消耗、出库等变更的产品') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GWB01R', '以小时为单位统计各个时段来客数量。') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GWB02R', '根据客户的消费历史,分析统计店内客户消费流量高峰时段。') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GWC01R', '根据以前的会员消费数据,分析会员到店里消费的频率和平均消费的价格的高低。') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GWC02R', '"查看一段时间范围内会员卡做了什么变动。如 遗失补卡。"') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GWC03R', '统计员工领取产品的情况,领取主要是在产品出库中做的。') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GWA33I', '本模块用于产品的寄存和领取。') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GWA34R', '本模块用于打印寄存凭证') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GHA05R', '用于查看员工考勤具体情况,包括迟退时间,加班时间。') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GHC01R', '员工按小组竞赛, 分小组统计业绩') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GMA20R', '本模块用于对打印员工资料。') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GWA35R', '本模块用于打印寄存产品的统计') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GWA36R', '统计产品保质期到期的产品') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GWA38R', '选择门店查询所选择门店的库存') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GDA01I', '总经理或店长对整个店的经营情况进行详细的决策分析') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GHC03R', '用于比较不同门店不同月的经营状况分析') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GWC04R', '统计会员卡账户的变化情况') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GMA21R', '统计抵用券的发放或消费情况') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GMA22R', '美容院中统计每个美容院的总业绩以及人数指标') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GMB05R', '美容院统计当前购买疗程项目的数量以及金额') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GMA23R', '美容院中统计疗程的消耗情况YMN') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GWA10I', '包括会员卡类别结构分析, 会员结构分析, 消费时段分析') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GSB28I', '设置疗程或次数项目的促销方案') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GHA08R', '用于查看某段时间内员工的出勤信息') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GMA17R', '用于查看某段时间内员工售前,售后,嘉宾,保证金等业绩') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GHA09R', '员工的业绩奖罚统计报表') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'GMA16R', '用于查看某段时间内推广员工的业绩') insert gam21(gas01c, gas02c, gas03c) values( 'ADMIN', 'MER01I', '客户到店提醒') end end GO /****** Object: StoredProcedure [dbo].[upg_save_program_desc] Script Date: 06/12/2017 06:20:38 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_save_program_desc] ( @userid varchar(10), @program varchar(20), @desc varchar(160) ) as begin if (isnull(@desc, '') = '') return delete gam21 where gas01c = @userid and gas02c = @program insert gam21(gas01c, gas02c, gas03c) values(@userid, @program, @desc) end GO /****** Object: StoredProcedure [dbo].[upg_get_program_desc] Script Date: 06/12/2017 06:20:09 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_get_program_desc] ( @userid varchar(10), @program varchar(20), @desc varchar(160) output ) as begin select @desc = gas03c from gam21 where gas01c = @userid and gas02c = @program if (isnull(@desc, '') = '') begin select @desc = gas03c from gam21 where gas01c = 'ADMIN' and gas02c = @program end if (isnull(@desc, '') = '') begin select '可自行输入每个使用者对每个功能的个性化描述' end select @desc end GO /****** Object: StoredProcedure [dbo].[upg_copy_hem02_from] Script Date: 06/12/2017 06:19:47 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_copy_hem02_from] ( @compid varchar(10), @fromid varchar(20), @toid varchar(20) ) as begin delete hem02 where heb00c = @compid and heb01c = @toid insert hem02(heb00c, heb01c, heb02c, heb03i, heb04f, heb05i, heb06f, heb07i, heb08f, heb09i, heb10f, heb11i, heb12f, heb13i, heb14f, heb15i, heb16f, heb17i, heb18f, heb19i, heb20f, heb21i, heb22f, heb23i, heb24f) select heb00c, @toid, heb02c, heb03i, heb04f, heb05i, heb06f, heb07i, heb08f, heb09i, heb10f, heb11i, heb12f, heb13i, heb14f, heb15i, heb16f, heb17i, heb18f, heb19i, heb20f, heb21i, heb22f, heb23i, heb24f from hem02 where heb00c = @compid and heb01c = @fromid end GO /****** Object: StoredProcedure [dbo].[upg_add_fav_program] Script Date: 06/12/2017 06:19:23 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_add_fav_program] ( @userid varchar(10), @program varchar(20) ) as begin delete gam22 where gat01c = @userid and gat02c = @program insert gam22(gat01c, gat02c) values(@userid, @program) end GO /****** Object: StoredProcedure [dbo].[upg_del_fav_program] Script Date: 06/12/2017 06:19:57 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_del_fav_program] ( @userid varchar(10), @program varchar(20) ) as begin delete gam22 where gat01c = @userid and gat02c = @program end GO /****** Object: StoredProcedure [dbo].[upg_select_stafftask] Script Date: 06/12/2017 06:20:40 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_select_stafftask] ( @compid varchar(10),--公司编号 @fromdate varchar(8),--开始时间 @todate varchar(8),--结束时间 @type1 varchar(2),-- 该来做疗程的会员(0:不计算,1:计算) @type2 varchar(2),-- 本应做疗程,但没来的会员(0:不计算,1:计算) @type3 varchar(2),-- 该来做服务项目的会员(0:不计算,1:计算) @type4 varchar(2),-- 该回访的会员(0:不计算,1:计算) @type5 varchar(2),-- 连续一个月未来的会员 @type6 varchar(2),-- 连续三个月未来的会员 @type7 varchar(2),-- 储值账户低于安全余额的会员 @type8 varchar(2),-- 会员卡使用到期 @type9 varchar(2),-- 疗程次数低于安全次数 @type10 varchar(2),-- 今日生日的会员 @type11 varchar(2), -- 达到某消费额的会员 @amount float--消费金额 ) as begin declare @type_1 varchar(2),-- 该来做疗程的会员(0:不计算,1:计算) @type_2 varchar(2),-- 本应做疗程,但没来的会员(0:不计算,1:计算) @type_3 varchar(2),-- 该来做服务项目的会员(0:不计算,1:计算) @type_4 varchar(2),-- 该回访的会员(0:不计算,1:计算) @type_5 varchar(2),-- 连续一个月未来的会员 @type_6 varchar(2),-- 连续三个月未来的会员 @type_7 varchar(2),-- 储值账户低于安全余额的会员 @type_8 varchar(2),-- 会员卡使用到期 @type_9 varchar(2),-- 疗程次数低于安全次数 @type_10 varchar(2),-- 今日生日的会员 @type_11 varchar(2) -- 达到某消费额的会员 set @type_1 = @type1 set @type_2 = @type2 set @type_3 = @type3 set @type_4 = @type4 set @type_5 = @type5 set @type_6 = @type6 set @type_7 = @type7 set @type_8 = @type8 set @type_9 = @type9 set @type_10 = @type10 set @type_11 = @type11 create table #result ( compid varchar(10) not null, --门店编号 cardid varchar(30) null, --会员卡号 mobile varchar(20) null, --手机号码 memberid varchar(20) not null,--会员编号 memname varchar(50) null,--会员名称, birthday varchar(15) null,--生日 notcomedays float null,--多久没来的天数 itemcode varchar(30) null,--项目/产品编号 itemname varchar(50) null,--项目/产品名称 istreatment float null,--是否是疗程 date varchar(8) null,--应做日期 /会员回访上次所做日期 cardamount float null,--储值金额 rechargeamount float null,--累计充值金额 consumeamount float null,--累计消费金额 remaincount float null,--剩余次数 remaintreatamt float null,--剩余疗程金额 expiredate varchar(8) null,--到期时间 goodsid varchar(30) null,--产品编号 goodsname varchar(50) null,--产品名称 goodsqty float null,--产品数量 goodsunit varchar(30),--产品单位 personcode varchar(20),--负责人编号 personname varchar(30),--负责人名称 taskdate varchar(8),--分配任务日期 taskstatus int, --任务状态 0:未完成 1:已完成 itemtype int null--数据类型 ) create table #gbm19 ( gbs00c varchar(10) not null, -- 门店编号 gbs01d varchar(10) not null, -- 时间 gbs02c varchar(30) not null, -- 会员卡号 gbs03c varchar(30) not null, -- 项目编号 gbs04i int not null, -- 完成状态(0未完成,1已完成,2未预约完成) gbs05f float null, -- 疗程总次数 gbs06f float null, -- 已用次数 gbs07f float null, -- 剩余次数 gbs08f float null, -- 当前完成次数 gbs09c varchar(30) null, -- 消费单号 gbs10i int null, --多久没来(依疗程来计算) gbs11i int null, --最近来店 primary key(gbs00c,gbs01d,gbs02c,gbs03c) ) create table #gcm20 ( gct00c varchar(10) not null, --门店编号 gct01i int identity not null, --序号 gct02c varchar(20) not null, --卡号 gct03c varchar(20) not null, --会员编号 gct04d varchar(8 ) not null, --日期 gct05c varchar(5) null, --异动标志,账户的异动方式 gct06c varchar(10) null, --代码的类别--1-卡销售 2-疗程销售 3-项目 4-产品销售 - 5卡充值 - 6疗程充值 gct07c varchar(20) null, --代码 gct08c varchar(100) null, --名称 gct09f float null, --标准单价 gct10f float null, --折扣单价 gct11f float null, --数量 gct12f float null, --价格 gct13c varchar(10) null, --单据类别 gct14c varchar(20) null, --单据单号 gct15c varchar(20) null, --员工1 gct16c varchar(20) null, --员工2 gct17c varchar(20) null, --员工3 gct18c varchar(20) null, --员工4 gct19t varchar(10) null, --时间 gct20c varchar(5) null, --支付方式 gct21f float null, --项目消费序号 nextdate varchar(8) null, --下次消费时间 primary key (gct00c,gct01i) ) create table #gbm10 ( gbj00c varchar(10) not null, -- 门店编号 gbj01i int identity not null, -- 流水号 gbj02c varchar(20) null, -- 消费单号 gbj03f float null, -- 序号 gbj04c varchar(20) null, -- 会员编号 gbj05c varchar(20) null, -- 项目编号 gbj06c varchar(20) null, -- 联系方式固定电话 gbj07d varchar(8) null, -- 服务日期 gbj08d varchar(8) null, -- 应该回访日期 gbj09c varchar(10) null, -- 回访员工编号 gbj10i int null, -- 是否已经回访 gbj11c varchar(5) null, -- 访问结果 gbj12c varchar(500) null, -- 一访结果描述 gbj13i int null, -- 类别 1:会员 2:散客 gbj14c varchar(20) null, -- 姓名 gbj15c varchar(20) null, -- 手机 gbj16i int null, -- 类别 0项目 1产品 gbj17c varchar(500) null, -- 名称 gbj18c varchar(10) null, --大工 gbj19c varchar(5) null, --服务满意度 gbj20c varchar(5) null, --技术满意度 gbj21c varchar(10) null, --中工 gbj22c varchar(5) null, --服务满意度 gbj23c varchar(5) null, --技术满意度 gbj24c varchar(10) null, --小工 gbj25c varchar(5) null, --服务满意度 gbj26c varchar(5) null, --技术满意度 gbj27c varchar(10) null, --四工 gbj28c varchar(5) null, --服务满意度 gbj29c varchar(5) null, --技术满意度 gbj30c varchar(10) null, --前台 gbj31c varchar(5) null, --服务满意度 gbj32c varchar(10) null, --门店 gbj33c varchar(5) null, --服务满意度 gbj34c varchar(5) null, --技术满意度 gbj35c varchar(500) null, --二访结果描述 primary key(gbj00c,gbj01i) ) declare @begindate varchar(8), @enddate varchar(8) if(@type_1='1') --本周该来做疗程的会员 begin insert into #gbm19(gbs00c,gbs01d,gbs02c,gbs03c,gbs04i,gbs05f,gbs06f,gbs07f,gbs08f,gbs09c,gbs10i,gbs11i) select gbs00c,gbs01d,gbs02c,gbs03c,gbs04i,gbs05f,gbs06f,gbs07f,gbs08f,gbs09c,gbs10i,gbs11i from gbm19 where gbs00c = @compid and gbs01d >= @fromdate and gbs01d <= @todate insert into #result(compid,cardid,mobile,memberid,memname,birthday,notcomedays,itemcode,itemname,istreatment,date,personcode,taskdate,taskstatus,itemtype) select a.gbs00c,a.gbs02c,c.gba08c,c.gba01c,c.gba03c,c.gba21c,a.gbs10i,a.gbs03c,d.gda03c,0,a.gbs01d,c.gba10c,a.gbs01d,-1,1 from #gbm19 a,gcm01 b,gbm01 c,gdm01 d where b.gca00c = @compid and c.gba00c = @compid and d.gda00c = @compid and a.gbs02c = b.gca01c and b.gca00c = b.gca13d and b.gca04c = c.gba01c and a.gbs03c = d.gda01c update a set a.personname = b.haa02c from #result a,ham01 b where b.haa00c = @compid and a.personcode = b.haa01c end if(@type_2='1') --上周本应该来,但没来的会员 begin set @begindate = convert(varchar(8),dateadd(day,-7,@fromdate),112) --上周开始时间 set @enddate = convert(varchar(8),dateadd(day,-7,@todate),112) --上周结束时间 print @begindate print @enddate delete from #gbm19 insert into #gbm19(gbs00c,gbs01d,gbs02c,gbs03c,gbs04i,gbs05f,gbs06f,gbs07f,gbs08f,gbs09c,gbs10i,gbs11i) select gbs00c,gbs01d,gbs02c,gbs03c,gbs04i,gbs05f,gbs06f,gbs07f,gbs08f,gbs09c,gbs10i,gbs11i from gbm19 where gbs00c = @compid and gbs01d >= @begindate and gbs01d <= @enddate and isnull(gbs09c,'') = '' insert into #result(compid,cardid,mobile,memberid,memname,birthday,notcomedays,itemcode,itemname,istreatment,date,personcode,taskdate,taskstatus,itemtype) select a.gbs00c,a.gbs02c,c.gba08c,c.gba01c,c.gba03c,c.gba21c,a.gbs10i,a.gbs03c,d.gda03c,0,a.gbs01d,c.gba10c,convert(varchar(8),dateadd(day,7,a.gbs01d),112),-1,2 from #gbm19 a,gcm01 b,gbm01 c,gdm01 d where b.gca00c = @compid and c.gba00c = @compid and d.gda00c = @compid and a.gbs02c = b.gca01c and b.gca00c = b.gca13d and b.gca04c = c.gba01c and a.gbs03c = d.gda01c update a set a.personname = b.haa02c from #result a,ham01 b where b.haa00c = @compid and a.personcode = b.haa01c end if(@type_3='1') --该来做服务项目的会员 begin insert into #gcm20(gct00c,gct02c,gct03c,gct04d,gct05c,gct06c,gct07c,gct08c,gct09f,gct10f,gct11f,gct12f,gct13c,gct14c,gct15c,gct16c,gct17c,gct18c,gct19t,gct20c,gct21f) select gct00c,gct02c,gct03c,gct04d,gct05c,gct06c,gct07c,gct08c,gct09f,gct10f,gct11f,gct12f,gct13c,gct14c,gct15c,gct16c,gct17c,gct18c,gct19t,gct20c,gct21f from gcm20 where gct00c = @compid and gct06c = 3 and gct13c = 'gx' --消费项目 update a set a.nextdate = convert(varchar(8),dateadd(day,b.gda20i,a.gct04d),112) from #gcm20 a,gdm01 b where b.gda00c = @compid and a.gct07c = b.gda01c insert into #result(compid,cardid,mobile,memberid,memname,birthday,itemcode,itemname,istreatment,date,personcode,taskdate,taskstatus,itemtype) select a.gct00c,a.gct02c,c.gba08c,c.gba01c,c.gba03c,c.gba17d,a.gct07c,d.gda03c,0,a.nextdate,c.gba10c,a.nextdate,-1,3 from #gcm20 a,gcm01 b,gbm01 c,gdm01 d where a.nextdate >= @fromdate and a.nextdate <= @todate and b.gca00c = @compid and c.gba00c = @compid and d.gda00c = @compid and a.gct02c = b.gca01c and b.gca00c = b.gca13d and b.gca04c = c.gba01c and a.gct07c = d.gda01c update a set a.personname = b.haa02c from #result a,ham01 b where b.haa00c = @compid and a.personcode = b.haa01c end if(@type_4='1') --该回访的会员 begin insert into #gbm10(gbj00c,gbj02c,gbj03f,gbj04c,gbj05c,gbj06c,gbj07d,gbj08d,gbj09c,gbj10i,gbj11c,gbj12c,gbj13i,gbj14c,gbj15c,gbj16i,gbj17c,gbj18c,gbj19c,gbj20c,gbj21c,gbj22c,gbj23c,gbj24c,gbj25c,gbj26c,gbj27c,gbj28c,gbj29c,gbj30c,gbj31c,gbj32c,gbj33c,gbj34c,gbj35c) select gbj00c,gbj02c,gbj03f,gbj04c,gbj05c,gbj06c,gbj07d,gbj08d,gbj09c,gbj10i,gbj11c,gbj12c,gbj13i,gbj14c,gbj15c,gbj16i,gbj17c,gbj18c,gbj19c,gbj20c,gbj21c,gbj22c,gbj23c,gbj24c,gbj25c,gbj26c,gbj27c,gbj28c,gbj29c,gbj30c,gbj31c,gbj32c,gbj33c,gbj34c,gbj35c from gbm10 where gbj00c = @compid and gbj08d >= @fromdate and gbj08d <= @todate and gbj13i = 1 --会员 insert into #result(compid,cardid,mobile,memberid,memname,itemcode,itemname,istreatment,date,personcode,taskdate,taskstatus,itemtype) select a.gbj00c,b.gca01c,a.gbj15c,a.gbj04c,c.gba03c,a.gbj05c,d.gda03c,0,a.gbj07d,c.gba10c,a.gbj08d,-1,4 from #gbm10 a,gcm01 b,gbm01 c,gdm01 d where b.gca00c = @compid and c.gba00c = @compid and d.gda00c = @compid and b.gca04c = c.gba01c and b.gca00c = b.gca13d and a.gbj04c = c.gba01c and a.gbj05c = d.gda01c update a set a.personname = b.haa02c from #result a,ham01 b where b.haa00c = @compid and a.personcode = b.haa01c end create table #cardtable ( compid varchar(10) not null, cardid varchar(20) not null, memberid varchar(20) not null, memname varchar(50) null, mobile varchar(20) null, cardamount float null,--储值金额 safeamount float null,--安全余额 rechargeamount float null,--累计充值金额 consumeamount float null,--累计消费金额 remaincount float null,--剩余次数 lastconsumedate varchar(8) null, --最后消费时间, notcomedays float null,--多久没来的天数 personcode varchar(20) null,--员工编号 personname varchar(30) null,--员工名称 expiredate varchar(8) null,--会员卡有效时间 primary key(compid,cardid) ) if(@type_5='1' or @type_6='1' or @type_7='1' or @type_8='1' or @type_9='1' or @type_11='1') begin insert into #cardtable(compid,cardid,memberid,memname,mobile,personcode,expiredate) select a.gca00c,a.gca01c,b.gba01c,b.gba03c,b.gba08c,b.gba10c,a.gca07d from gcm01 a,gbm01 b where a.gca00c = @compid and b.gba00c = @compid and a.gca00c = a.gca13d and a.gca04c = b.gba01c update a set a.personname = b.haa02c from #cardtable a,ham01 b where b.haa00c = @compid and a.personcode = b.haa01c update a set a.cardamount = b.gcc06f, a.safeamount = b.gcc07f from #cardtable a,gcm03 b where b.gcc00c = @compid and a.cardid = b.gcc01c and b.gcc03i = 2 delete #gcm20 --清除历史数据 insert into #gcm20(gct00c,gct02c,gct03c,gct04d,gct05c,gct06c,gct07c,gct08c,gct09f,gct10f,gct11f,gct12f,gct13c,gct14c,gct15c,gct16c,gct17c,gct18c,gct19t,gct20c,gct21f) select gct00c,gct02c,gct03c,gct04d,gct05c,gct06c,gct07c,gct08c,gct09f,gct10f,gct11f,gct12f,gct13c,gct14c,gct15c,gct16c,gct17c,gct18c,gct19t,gct20c,gct21f from gcm20 a,#cardtable b where a.gct02c = b.cardid update a set a.rechargeamount = b.amount from #cardtable a,(select gct02c,sum(gct12f)amount from #gcm20 where gct13c = 'gz' group by gct02c) b where a.cardid = b.gct02c update a set a.consumeamount = b.sumamount, a.lastconsumedate = b.lastdate, a.notcomedays = datediff(day,b.lastdate,convert(varchar(8),getdate(),112)) from #cardtable a,(select gct02c,sum(gct12f)sumamount,max(gct04d)lastdate from #gcm20 group by gct02c )b where a.cardid = b.gct02c update a set a.remaincount = b.remaincount from #cardtable a,(select gcf00c,gcf01c,sum(gcf07f) remaincount from gcm06 where gcf00c = @compid group by gcf00c,gcf01c) b where a.cardid = b.gcf01c --连续一个月未来的会员 declare @last_onemonth varchar(8), @last_threemonth varchar(8) set @last_onemonth = convert(varchar(8),dateadd(month,-1,@fromdate),112) --一个月之前 set @last_threemonth = convert(varchar(8),dateadd(month,-3,@fromdate),112) --三个月之前 if(@type_5='1') --连续一个月未来的会员 begin insert into #result(compid,cardid,mobile,memberid,memname,notcomedays,expiredate,cardamount,rechargeamount,consumeamount,remaincount,personcode,personname,taskdate,taskstatus,itemtype) select compid,cardid,mobile,memberid,memname,notcomedays,expiredate,cardamount,rechargeamount,consumeamount,remaincount,personcode,personname,@fromdate,-1,5 from #cardtable where lastconsumedate >= @last_threemonth and lastconsumedate <= @last_onemonth end if(@type_6='1') --连续3个月未来的会员 begin insert into #result(compid,cardid,mobile,memberid,memname,notcomedays,expiredate,cardamount,rechargeamount,consumeamount,remaincount,personcode,personname,taskdate,taskstatus,itemtype) select compid,cardid,mobile,memberid,memname,notcomedays,expiredate,cardamount,rechargeamount,consumeamount,remaincount,personcode,personname,@fromdate,-1,6 from #cardtable where lastconsumedate < @last_threemonth end if(@type_7='1') --储值账户低于安全余额 begin insert into #result(compid,cardid,mobile,memberid,memname,notcomedays,expiredate,cardamount,rechargeamount,consumeamount,remaincount,personcode,personname,taskdate,taskstatus,itemtype) select compid,cardid,mobile,memberid,memname,notcomedays,expiredate,cardamount,rechargeamount,consumeamount,remaincount,personcode,personname,@fromdate,-1,7 from #cardtable where isnull(cardamount,0) < isnull(safeamount,0) end if(@type_8='1') --会员卡使用到期提醒 begin insert into #result(compid,cardid,mobile,memberid,memname,notcomedays,expiredate,cardamount,rechargeamount,consumeamount,remaincount,personcode,personname,taskdate,taskstatus,itemtype) select compid,cardid,mobile,memberid,memname,notcomedays,expiredate,cardamount,rechargeamount,consumeamount,remaincount,personcode,personname,@fromdate,-1,8 from #cardtable where expiredate < convert(varchar(8),getdate(),112) --会员卡到期时间小于当前时间 end if(@type_9='1') --会员卡疗程剩余次数提醒 begin insert into #result(compid,cardid,mobile,memberid,memname,itemcode,itemname,expiredate,remaincount,remaintreatamt,personcode,taskdate,taskstatus,itemtype) select a.gcf00c,a.gcf01c,d.gba08c,d.gba01c,d.gba03c,a.gcf02c,c.gda03c,a.gcf12d,a.gcf07f,a.gcf10f,d.gba10c,@fromdate,-1,9 from gcm06 a,gcm01 b,gdm01 c,gbm01 d where a.gcf00c = @compid and b.gca00c = @compid and c.gda00c = @compid and d.gba00c = @compid and a.gcf01c = b.gca01c and b.gca00c = b.gca13d and a.gcf02c = c.gda01c and b.gca04c = d.gba01c and a.gcf07f < c.gda08c update a set a.personname = b.haa02c from #result a,ham01 b where b.haa00c = @compid and a.personcode = b.haa01c end if(@type_11='1' and isnull(@amount,0)>0) --达到某消费额的客户 begin insert into #result(compid,cardid,mobile,memberid,memname,notcomedays,expiredate,cardamount,rechargeamount,consumeamount,remaincount,personcode,personname,taskdate,taskstatus,itemtype) select compid,cardid,mobile,memberid,memname,notcomedays,expiredate,cardamount,rechargeamount,consumeamount,remaincount,personcode,personname,@fromdate,-1,11 from #cardtable where isnull(consumeamount,0) > isnull(@amount,0) update a set a.personname = b.haa02c from #result a,ham01 b where b.haa00c = @compid and a.personcode = b.haa01c end end create table #gcm06 ( gcf00c varchar(10) not null, --门店编号 gcf01c varchar(20) not null, --卡号 gcf02c varchar(20) not null, --项目编号 gcf03f float null, --次数 gcf04f float null, --赠送次数 gcf05f float null, --总次数 gcf06f float null, --已经使用次数 gcf07f float null, --剩余次数 gcf08f float null, --疗程金额 gcf09f float null, --已经使用金额 gcf10f float null, --剩余金额 gcf11d varchar(8) null, --账户开立日 gcf12d varchar(8) null, --账户截至日 gcf13i int null, --疗程间隔 gcf14f float null, --一个月可使用的次数 0--代表不控制 直到这个疗程用完其他float就是一个月可以使用的次数 gcf15c varchar(60) null, --输入的时候的备注 gcf16i int null, --是否已经审核 gcf17i int not null, --价格序号 gcf18f float not null, --折扣 gcf19i int null, --是否停用 2停用 1正常 3买产品赠送项目 gcf20i int null, --标志 是否前期卡 gcf21i int null, --是否赠送 gcf22i int null, --永久免费 gcf23i int identity not null, --序号 gcf24i int null, --开帐中的序号(gcm16.gcr23i) gcf25f float null, --虚假次数(只有增加部分) gcf26c nvarchar(2000) null, --疗程备注 gcf27i int not null default 0 ,--过期疗程是否处理,0表示没处理 gcf28i int not null default 0, --是否已退 lastconsumedate varchar(8) null, --最后消费时间 nextdate varchar(8) null,--下次消费时间 primary key(gcf23i,gcf00c,gcf01c) ) if(@type_10='1') --本周过生日的会员 begin insert into #result(compid,cardid,mobile,memberid,memname,birthday,personcode,taskdate,taskstatus,itemtype) select a.gba00c,b.gca01c,a.gba08c,a.gba01c,a.gba03c,a.gba21c,a.gba10c,substring(@fromdate,1,4)+ substring(gba21c,5,4),-1,10 from gbm01 a,gcm01 b where a.gba00c = @compid and b.gca00c = @compid and convert(varchar(8),dateadd(yy,datediff(yy,a.gba21c,getdate()),a.gba21c),112) >= @fromdate and convert(varchar(8),dateadd(yy,datediff(yy,a.gba21c,getdate()),a.gba21c),112) <= @todate and a.gba01c = b.gca04c and b.gca00c = b.gca13d update a set a.personname = b.haa02c from #result a,ham01 b where b.haa00c = @compid and a.personcode = b.haa01c end --清空未指定负责人的员工 delete from #result where isnull(personcode,'') = '' --清空离职的员工记录 delete from #result where exists(select 1 from ham01 where haa00c = @compid and haa27c = '3' and haa01c = personcode) --清空历史记录 delete from staff_task where compid = @compid and taskdate >= @fromdate and taskdate <= @todate insert into staff_task(compid,cardid,mobile,memberid,memname,birthday,notcomedays,itemcode,itemname,istreatment,date,cardamount,rechargeamount,consumeamount, remaincount,remaintreatamt,expiredate,goodsid,goodsname,goodsqty,goodsunit,personcode,personname,taskdate,taskstatus,itemtype) select compid,cardid,mobile,memberid,memname,birthday,notcomedays,itemcode,itemname,istreatment,date,cardamount,rechargeamount,consumeamount, remaincount,remaintreatamt,expiredate,goodsid,goodsname,goodsqty,goodsunit,personcode,personname,taskdate,taskstatus,itemtype from #result select compid,cardid,mobile,memberid,memname,birthday,notcomedays,itemcode,itemname,istreatment,date,cardamount,rechargeamount,consumeamount, remaincount,remaintreatamt,expiredate,goodsid,goodsname,goodsqty,goodsunit,personcode,personname,taskdate,taskstatus,itemtype from #result order by personcode drop table #gcm06 drop table #cardtable drop table #gbm10 drop table #gcm20 drop table #gbm19 drop table #result end GO /****** Object: StoredProcedure [dbo].[upg_copy_hem03] Script Date: 06/12/2017 06:19:47 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_copy_hem03] ( @compid varchar(10), @fromid varchar(20), @toid varchar(20) ) as begin delete hem03 where hec00c = @compid and hec01c = @toid insert hem03(hec00c, hec01c, hec02f, hec03f, hec04f, hec05i, hec06f, hec07c, hec08i, hec09f, hec10c) select @compid, @toid, hec02f, hec03f, hec04f, hec05i, hec06f, hec07c, hec08i, hec09f, hec10c from hem03 where hec00c = @compid and hec01c = @fromid end GO /****** Object: UserDefinedFunction [dbo].[fun_getPY] Script Date: 06/12/2017 06:20:57 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create function [dbo].[fun_getPY](@word nvarchar(4000)) returns nvarchar(4000) as begin declare @result nvarchar(4000) set @result = '' declare @index int set @index = 1 while(@index <= len(@word)) begin set @result = @result + dbo.fun_getFirstPY(substring(@word, @index, 1)) set @index = @index + 1 end return @result end GO /****** Object: StoredProcedure [dbo].[upg_m_dispatch_gdm20] Script Date: 06/12/2017 06:20:19 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_m_dispatch_gdm20]( @fromcompid varchar(10), -- 从哪一个公司发布 @tocompid varchar(10), -- 目的公司 @mandatory varchar(2)) -- 是否强制统一 as begin if @fromcompid = @tocompid return create table #gdm20_1 -- 项目/产品/会员卡类别价格折扣 ( gdt00c varchar(10) not null, --公司编号 gdt01c varchar(20) not null, --促销编号 gdt02c varchar(40) null, --促销名称 gdt03i int null, --是否有效 gdt04d varchar(10) null, --开始日期 gdt05d varchar(10) null, --截止日期 gdt06i int null, --促销依据 1按项目 + 价格序号 2按类别合计金额 gdt91c varchar(10) null, --登录人 gdt92d varchar(10) null, --登录日期 gdt93c varchar(10) null, --复核人 gdt94d varchar(10) null, --复核日期 ) declare @PK_Sql nvarchar(max) set @PK_Sql = ' alter table #gdm20_1 add constraint PK_#gdm20_1_' + convert(varchar(10), cast(floor(rand() * 1000000) as int)) + ' PRIMARY KEY NonCLUSTERED(gdt00c, gdt01c)' exec sp_executesql @PK_Sql insert #gdm20_1(gdt00c, gdt01c, gdt02c, gdt03i, gdt04d, gdt05d, gdt06i, gdt91c, gdt92d, gdt93c, gdt94d) select gdt00c, gdt01c, gdt02c, gdt03i, gdt04d, gdt05d, gdt06i, gdt91c, gdt92d, gdt93c, gdt94d from gdm20 where gdt00c = @fromcompid if @mandatory = '1' begin delete gdm20 where gdt00c = @tocompid end else begin delete a from #gdm20_1 a, gdm20 b where a.gdt00c = @fromcompid and a.gdt01c = b.gdt01c and b.gdt00c = @tocompid end insert gdm20(gdt00c, gdt01c, gdt02c, gdt03i, gdt04d, gdt05d, gdt06i, gdt91c, gdt92d, gdt93c, gdt94d) select @tocompid, gdt01c, gdt02c, gdt03i, gdt04d, gdt05d, gdt06i, gdt91c, gdt92d, gdt93c, gdt94d from #gdm20_1 where gdt00c = @fromcompid insert gdm21(gdu00c, gdu01c, gdu02f, gdu03i, gdu04c, gdu05f, gdu06f, gdu07i, gdu08i, gdu09i, gdu10i, gdu11i, gdu12f, gdu13i, gdu14i, gdu15i, gdu16i, gdu17i, gdu18f, gdu19c, gdu20c, gdu21c, gdu22c) select @tocompid, gdu01c, gdu02f, gdu03i, gdu04c, gdu05f, gdu06f, gdu07i, gdu08i, gdu09i, gdu10i, gdu11i, gdu12f, gdu13i, gdu14i, gdu15i, gdu16i, gdu17i, gdu18f, gdu19c, gdu20c, gdu21c, gdu22c from gdm21 where gdu00c = @fromcompid end GO /****** Object: StoredProcedure [dbo].[upg_copy_hem04] Script Date: 06/12/2017 06:19:47 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_copy_hem04](@compid varchar(10), @fromid varchar(20), @toid varchar(20)) as begin delete hem04 where hed00c = @compid and hed01c = @toid insert hem04(hed00c, hed01c, hed02f, hed03f, hed04f, hed05i, hed06f, hed07c, hed08c) select @compid, @toid, hed02f, hed03f, hed04f, hed05i, hed06f, hed07c, hed08c from hem04 where hed00c = @compid and hed01c = @fromid end GO /****** Object: StoredProcedure [dbo].[upg_copy_hem05] Script Date: 06/12/2017 06:19:47 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_copy_hem05](@compid varchar(10), @fromid varchar(20), @toid varchar(20)) as begin delete hem05 where hee00c = @compid and hee01c = @toid insert hem05(hee00c, hee01c, hee02f, hee03f, hee04f, hee05i, hee06f, hee07c, hee08c) select @compid, @toid, hee02f, hee03f, hee04f, hee05i, hee06f, hee07c, hee08c from hem05 where hee00c = @compid and hee01c = @fromid end GO /****** Object: StoredProcedure [dbo].[upg_m_dispatch_appointment_perf_ratio_single] Script Date: 06/12/2017 06:20:18 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_m_dispatch_appointment_perf_ratio_single] ( @fromcompid varchar(10), -- 从哪一个公司发布 @tocompid varchar(10), -- 目的公司 @mandatory varchar(2) -- 是否强制统一 ) as begin if @fromcompid = @tocompid return if @mandatory <> '1' and @mandatory <> '0' return if @mandatory = '1' begin delete appointment_perf_ratio where compid = @tocompid end create table #appointment_perf_ratio_dispatch_single_key -- 疗程资料关键字 ( projid varchar(20) not null, -- 疗程代码 projtype varchar(10) not null -- 统计分类(统计), 对应于gdm01.gda13c ) declare @PK_Sql nvarchar(max) set @PK_Sql = ' alter table #appointment_perf_ratio_dispatch_single_key add constraint PK_#appointment_perf_ratio_dispatch_single_key_' + convert(varchar(10), cast(floor(rand() * 1000000) as int)) + ' primary key clustered(projid, projtype)' exec sp_executesql @PK_Sql insert into #appointment_perf_ratio_dispatch_single_key(projid, projtype) select projid, projtype from appointment_perf_ratio where compid = @fromcompid declare @current_projtype varchar(10) -- 统计分类(统计), 对应于gdm01.gda13c declare @current_projid varchar(20) -- 疗程代码 -- 创建循环变量 declare @HasRecord int set @HasRecord = 1 declare @Index int set @Index = -1 while(@HasRecord = 1) begin if exists ( select 1 from #appointment_perf_ratio_dispatch_single_key) begin set @HasRecord = 1 select top 1 @current_projtype = projtype, @current_projid = projid from #appointment_perf_ratio_dispatch_single_key if not exists (select 1 from appointment_perf_ratio where compid = @tocompid and projid = @current_projid and projtype = @current_projtype) begin insert appointment_perf_ratio(compid, projid, projtype, ratio) select @tocompid, projid, projtype, ratio from appointment_perf_ratio where compid = @fromcompid and projid = @current_projid and projtype = @current_projtype end delete from #appointment_perf_ratio_dispatch_single_key where projtype = @current_projtype and projid = @current_projid end else begin set @HasRecord = 0 end end drop table #appointment_perf_ratio_dispatch_single_key end GO /****** Object: StoredProcedure [dbo].[upg_get_appointment_perf_ratio] Script Date: 06/12/2017 06:20:04 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_get_appointment_perf_ratio] ( @compid varchar(20), -- 公司别 @projid varchar(20), -- 疗程代码 @projtype varchar(10), -- 统计分类(统计), 对应于gdm01.gda13c @ratio float out -- 比率 ) as-- WUXM 2004/12/6 PM 21:31 begin select @ratio = ratio from appointment_perf_ratio where compid = @compid set @ratio = isnull(@ratio, 1) if @ratio <= 0 set @ratio = 1 end GO /****** Object: StoredProcedure [dbo].[upg_m_dispatch_appointment_perf_ratio] Script Date: 06/12/2017 06:20:18 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_m_dispatch_appointment_perf_ratio] ( @fromcompid varchar(10), -- 从哪一个公司发布 @tocompid varchar(10), -- 目的公司 @mandatory varchar(2) -- 是否强制统一 ) as begin if @fromcompid = @tocompid return if @mandatory <> '1' and @mandatory <> '0' return if @mandatory = '1' begin delete appointment_perf_ratio where compid = @tocompid insert appointment_perf_ratio(compid, projid, projtype, ratio) select @tocompid, projid, projtype, ratio from appointment_perf_ratio where compid = @fromcompid return end create table #appointment_perf_ratio_dispatch_single_key -- 疗程资料关键字 ( projid varchar(20) not null, -- 疗程代码 projtype varchar(10) not null -- 统计分类(统计), 对应于gdm01.gda13c ) declare @PK_Sql nvarchar(max) set @PK_Sql = ' alter table #appointment_perf_ratio_dispatch_single_key add constraint PK_#appointment_perf_ratio_dispatch_single_key_' + convert(varchar(10), cast(floor(rand() * 1000000) as int)) + ' primary key clustered(projid, projtype)' exec sp_executesql @PK_Sql insert into #appointment_perf_ratio_dispatch_single_key(projid, projtype) select projid, projtype from appointment_perf_ratio where compid = @fromcompid declare @current_projtype varchar(10) -- 统计分类(统计), 对应于gdm01.gda13c declare @current_projid varchar(20) -- 疗程代码 -- 创建循环变量 declare @HasRecord int set @HasRecord = 1 declare @Index int set @Index = -1 while(@HasRecord = 1) begin if exists ( select 1 from #appointment_perf_ratio_dispatch_single_key) begin set @HasRecord = 1 select top 1 @current_projtype = projtype, @current_projid = projid from #appointment_perf_ratio_dispatch_single_key if not exists (select 1 from appointment_perf_ratio where compid = @tocompid and projid = @current_projid and projtype = @current_projtype) begin insert appointment_perf_ratio(compid, projid, projtype, ratio) select @tocompid, projid, projtype, ratio from appointment_perf_ratio where compid = @fromcompid and projid = @current_projid and projtype = @current_projtype end delete from #appointment_perf_ratio_dispatch_single_key where projtype = @current_projtype and projid = @current_projid end else begin set @HasRecord = 0 end end drop table #appointment_perf_ratio_dispatch_single_key end GO /****** Object: StoredProcedure [dbo].[upg_autokey] Script Date: 06/12/2017 06:19:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_autokey] ( @date varchar(8), @tablename varchar(100), @keyfieldname varchar(100), @format varchar(100), @comp varchar(10), @compfieldname varchar(100), @result varchar(100) output ) as begin if dbo.fun_AsCharCount(@format, 'c') > 0 and isnull(@comp, '') = '' return if isnull(@date, '') = '' set @date = convert(varchar(8), getdate(), 112) declare @section table ( code varchar(1), inde int null, lengt int null, valu varchar(20) null ) declare @c_index int set @c_index = charindex('c', @format) if @c_index > 0 insert into @section(code, inde, lengt, valu) values('c', @c_index, len(@comp), @comp) declare @i_index int set @i_index = charindex('i', @format) if @i_index > 0 insert into @section(code, inde, lengt, valu) values('i', @i_index, 1, 'I') declare @o_index int set @o_index = charindex('o', @format) if @o_index > 0 insert into @section(code, inde, lengt, valu) values('o', @o_index, 1, 'O') declare @y_len int set @y_len = dbo.fun_AsCharCount(@format, 'y') declare @m_len int set @m_len = dbo.fun_AsCharCount(@format, 'm') declare @d_len int set @d_len = dbo.fun_AsCharCount(@format, 'd') if @y_len > 4 and @m_len > 2 and @d_len > 2 return declare @y_index int set @y_index = charindex('y', @format) if @y_index > 0 insert into @section(code, inde, lengt, valu) values('y', @y_index, @y_len, substring(@date, 4 - @y_len + 1, @y_len)) --select '2', * from @section declare @m_index int set @m_index = charindex('m', @format) declare @m_value varchar(20) if @m_len = 2 set @m_value = substring(@date, 5, 2) else if @m_len = 1 set @m_value = dbo.fun_IntToHexString(convert(int, substring(@date, 5, 2))) if @m_index > 0 insert into @section(code, inde, lengt, valu) values('m', @m_index, @m_len, @m_value) declare @d_index int set @d_index = charindex('d', @format) if @d_index > 0 insert into @section(code, inde, lengt, valu) values('d', @d_index, @d_len, substring(@date, 7, 2)) declare @nine_len int set @nine_len = dbo.fun_AsCharCount(@format, '9') declare @nine_index int set @nine_index = charindex('9', @format) if @nine_index > 0 insert into @section(code, inde, lengt) values('9', @nine_index, @nine_len) declare @sorted_section table ( code varchar(1), inde int null, lengt int null, valu varchar(20) null ) declare @close int set @close = 1 declare @record_index int set @record_index = 1 declare @code varchar(1) declare @inde int declare @lengt int declare @valu varchar(20) while @close = 1 begin if exists (select 1 from @section) begin select @inde = min(inde) from @section select @code = code, @lengt = lengt, @valu = valu from @section where inde = @inde insert into @sorted_section(code, inde, lengt, valu) values(@code, @record_index, @lengt, @valu) set @record_index = @record_index + 1 delete from @section where inde = @inde end else begin set @close = 0 end end declare @likeNo varchar(200) set @likeNo = '' declare @maxCount int select @maxCount = count(0) from @sorted_section set @close = 1 while @close <= @maxCount begin select @likeNo = @likeNo + isnull(valu, '') from @sorted_section where inde = @close set @close = @close + 1 end declare @sql nvarchar(1024) declare @maxKey varchar(200) create table #max_key_tb ( maxKey varchar(200) ) set @sql = 'select max(' + @keyfieldname + ') from ' + @tablename + ' with(nolock) where ' + @keyfieldname + ' like ''' + @likeNo + '%''' if isnull(@compfieldname, '') <> '' and isnull(@comp, '') <> '' set @sql = @sql + ' and ' + @compfieldname + ' = ''' + @comp + '''' insert into #max_key_tb(maxKey) exec sp_executesql @sql select @maxKey = maxKey from #max_key_tb drop table #max_key_tb set @nine_index = 1 set @close = 1 while @close < @maxCount begin select @code = code from @sorted_section where inde = @close if @code = '9' begin set @close = @maxCount + 1 end else begin select @nine_index = @nine_index + lengt from @sorted_section where inde = @close set @close = @close + 1 end end declare @maxKey_nine varchar(20) if (isnull(@maxKey, '') <> '') and len(@maxKey) >= (@nine_index + @nine_len - 1) set @maxKey_nine = substring(@maxKey, @nine_index, @nine_len) declare @nine_value int set @nine_value = 0 if isnull(@maxKey_nine, '') <> '' begin set @nine_value = convert(int, @maxKey_nine) end set @nine_value = @nine_value + 1 set @maxKey_nine = dbo.fun_IntToFixLengthString(@nine_value, @nine_len) update @sorted_section set valu = @maxKey_nine where code = '9' set @close = 1 set @result = '' while @close <= @maxCount begin select @result = @result + isnull(valu, '') from @sorted_section where inde = @close set @close = @close + 1 end end GO /****** Object: StoredProcedure [dbo].[upg_cal_work_detail_gem01] Script Date: 06/12/2017 06:19:36 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_cal_work_detail_gem01] ( @spid int, @compid varchar(10), -- 公司别 @fromdate varchar(10), -- 开始日期 @todate varchar(10), -- 截至日期 @fromempl varchar(20), -- 查询开始人员 @toempl varchar(20), -- 查询截至人员 @inc_card int ) as begin --条件赋值 if(@fromdate = '') begin set @fromdate = '*' end if(@fromempl = '') begin set @fromempl = '*' end --Start --创建临时表 create table #gem01_amt ( compid varchar(20) null, billid varchar(20) null, bilamt float null ) create table #gem01 -- 会员卡--异动单 ( gea00c char(10) not null, --门店编号 gea01c varchar(20) not null, --异动单号 gea03d varchar(8) null, --异动日期 gea07c varchar(20) null, --异动後卡号(只对 -卡种变更 有效) gea21c varchar(20) null, --销售人员(对 -卡种变更 有效) gea27f float null, --第一销售业绩 gea28c varchar(20) null, --第二销售(支付方式4) gea29f float null, --第二销售业绩 gea30c varchar(20) null, --第3销售 gea31f float null, --第3销售业绩 gea32c varchar(20) null, --第4销售 gea33f float null, --第4销售业绩 gea34c varchar(20) null, --第5销售 gea35f float null, --第5销售业绩 gea36c varchar(20) null, --第6销售 gea37f float null, --第6销售业绩 gea38c varchar(20) null, --第7销售 gea39f float null, --第7销售业绩 gea40c varchar(20) null, --第8销售 gea41f float null, --第8销售业绩 gea42c varchar(20) null, --第9销售 gea43f float null, --第9销售业绩 gea44c varchar(20) null, --第10销售 gea45f float null, --第10销售业绩 gsc04c varchar(20) null, --支付方式代码 payway varchar(20) null, --支付方式名称 gsc05f float null, --支付金额 bilamt float null, --单据金额 gsc00c varchar(10) null, --公司编号 gsc01c varchar(20) null, --单据编号 gsc02c varchar(5) null, --单据类别 gsc03f float null, --序号 gea81c varchar(10) null, --部门编号 ) if(@inc_card = 1) begin --插入数据 insert #gem01 (gea00c, gea01c, gea03d, gea07c, gea21c, gea27f, gea28c, gea29f, gea30c, gea31f, gea32c, gea33f, gea34c, gea35f, gea36c, gea37f, gea38c, gea39f, gea40c, gea41f, gea42c, gea43f, gea44c, gea45f, gsc04c, gsc05f, gsc00c, gsc01c, gsc02c, gsc03f, gea81c, payway) select gea00c, gea01c, gea03d, gea07c, gea21c, gea27f, gea28c, gea29f, gea30c, gea31f, gea32c, gea33f, gea34c, gea35f, gea36c, gea37f, gea38c, gea39f, gea40c, gea41f, gea42c, gea43f, gea44c, gea45f, gsc04c, gsc05f, gsc00c, gsc01c, gsc02c, gsc03f, gea81c, gsb03c FROM gem01 a with (nolock), gsm03 b with(nolock),gsm02 c with (nolock) where gea00c = @compid and a.gea00c = b.gsc00c and a.gea01c = b.gsc01c and b.gsc00c = c.gsb00c and b.gsc04c = c.gsb02c and c.gsb01c = '3' and b.gsc02c = 'ge' and a.gea02i = 4 and ((a.gea03d >= @fromdate and a.gea03d <= @todate) or @fromdate = '*') insert #gem01_amt(compid,billid,bilamt) select gea00c,gea01c,sum(gsc05f) from #gem01 group by gea00c,gea01c update a set a.bilamt = b.bilamt from #gem01 a, #gem01_amt b where a.gea00c = b.compid and a.gea01c = b.billid end --创建临时表 create table #work_detail_gem01 ( id int identity , -- 流水好 person_id varchar(20) not null, -- 员工代码 action_id tinyint not null, srvdate varchar(10) not null, --日期 code varchar(20) not null, --项目代码,或是卡号,产品码 name varchar(200) null, --名称 payway varchar(20) null, --支付方式 amt float null, --营业金额 quan float null, --数量 cost float null, --成本 comm float null, --提成 amt2 float null, --虚业绩 amt3 float null, --真实业绩 cardrate float null, --转卡比率 --当买卡或充值的时候,这个地方当作疗程账户金额用 stdprice float null, --标准单价 prj_type varchar(20) null, --项目类别 share_rate float null default 1, --分享比率 cls_flag int null, -- 1:项目 2:产品 3:卡 gender int null, --性别 0:女 1:男 billid varchar(20) null, --单号 paycode varchar(20) null, --支付代码 ex_rate float null, --实际营业额折算比率 ggm02_seq float null, --ggm02中的序列号,与单号一起来标志一个项目 room_id varchar(20) null, --房间号,只在spa或足浴版本需要 compid varchar(10) null, --公司别 spid int null, --连接号 cardid varchar(20) null, --会员卡号 memid varchar(40) null, --会员编号 comp_ascription varchar(10) null -- 作业公司 ) --第一销售 insert #work_detail_gem01(person_id, action_id, srvdate, code, name, amt, quan, cost, stdprice, share_rate, prj_type, billid, gender, paycode, amt2, payway, cardid, memid) select gea21c, 1, gea03d, gea07c, gak02c, gsc05f, 1, 0, gsc05f, 1, gca02c, gea01c, b.gba14i, gsc04c, gea27f * dbo.fun_divide_default(gsc05f,bilamt,0), payway, gba01c, gba03c from #gem01 a, gbm01 b with(nolock), gcm01 c with(nolock),gam10 d with(nolock) where a.gea00c = b.gba00c and a.gea07c = b.gba23c and b.gba00c = c.gca00c and b.gba23c = c.gca01c and c.gca00c = c.gca13d and c.gca00c = d.gak00c and c.gca02c = d.gak01c and isnull(a.gea21c,'') <>'' and ((a.gea21c >= @fromempl and a.gea21c <= @toempl) or @fromempl = '*') --第二销售 insert #work_detail_gem01(person_id, action_id, srvdate, code, name, amt, quan, cost, stdprice, share_rate, prj_type, billid, gender, paycode, amt2, payway, cardid, memid) select gea28c, 1, gea03d, gea07c, gak02c, gsc05f, 1, 0, gsc05f, 1, gca02c, gea01c, b.gba14i, gsc04c, gea29f * dbo.fun_divide_default(gsc05f,bilamt,0), payway, gba01c, gba03c from #gem01 a, gbm01 b with(nolock), gcm01 c with(nolock),gam10 d with(nolock) where a.gea00c = b.gba00c and a.gea07c = b.gba23c and b.gba00c = c.gca00c and b.gba23c = c.gca01c and c.gca00c = c.gca13d and c.gca00c = d.gak00c and c.gca02c = d.gak01c and isnull(a.gea28c,'') <>'' and ((a.gea28c >= @fromempl and a.gea28c <= @toempl) or @fromempl = '*') --第三销售 insert #work_detail_gem01(person_id, action_id, srvdate, code, name, amt, quan, cost, stdprice, share_rate, prj_type, billid, gender, paycode, amt2, payway, cardid, memid) select gea30c, 1, gea03d, gea07c, gak02c, gsc05f, 1, 0, gsc05f, 1, gca02c, gea01c, b.gba14i, gsc04c, gea31f* dbo.fun_divide_default(gsc05f,bilamt,0), payway, gba01c, gba03c from #gem01 a, gbm01 b with(nolock), gcm01 c with(nolock),gam10 d with(nolock) where a.gea00c = b.gba00c and a.gea07c = b.gba23c and b.gba00c = c.gca00c and b.gba23c = c.gca01c and c.gca00c = c.gca13d and c.gca00c = d.gak00c and c.gca02c = d.gak01c and isnull(a.gea30c,'') <>'' and ((a.gea30c >= @fromempl and a.gea30c <= @toempl) or @fromempl = '*') --第四销售 insert #work_detail_gem01(person_id, action_id, srvdate, code, name, amt, quan, cost, stdprice, share_rate, prj_type, billid, gender, paycode, amt2, payway, cardid, memid) select gea32c, 1, gea03d, gea07c, gak02c, gsc05f, 1, 0, gsc05f, 1, gca02c, gea01c, b.gba14i, gsc04c, gea33f* dbo.fun_divide_default(gsc05f,bilamt,0), payway, gba01c, gba03c from #gem01 a, gbm01 b with(nolock), gcm01 c with(nolock),gam10 d with(nolock) where a.gea00c = b.gba00c and a.gea07c = b.gba23c and b.gba00c = c.gca00c and b.gba23c = c.gca01c and c.gca00c = c.gca13d and c.gca00c = d.gak00c and c.gca02c = d.gak01c and isnull(a.gea32c,'') <>'' and ((a.gea32c >= @fromempl and a.gea32c <= @toempl) or @fromempl = '*') --第五销售 insert #work_detail_gem01(person_id, action_id, srvdate, code, name, amt, quan, cost, stdprice, share_rate, prj_type, billid, gender, paycode, amt2, payway, cardid, memid) select gea34c, 1, gea03d, gea07c, gak02c, gsc05f, 1, 0, gsc05f, 1, gca02c, gea01c, b.gba14i, gsc04c, gea35f* dbo.fun_divide_default(gsc05f,bilamt,0), payway, gba01c, gba03c from #gem01 a, gbm01 b with(nolock), gcm01 c with(nolock),gam10 d with(nolock) where a.gea00c = b.gba00c and a.gea07c = b.gba23c and b.gba00c = c.gca00c and b.gba23c = c.gca01c and c.gca00c = c.gca13d and c.gca00c = d.gak00c and c.gca02c = d.gak01c and isnull(a.gea34c,'') <>'' and ((a.gea34c >= @fromempl and a.gea34c <= @toempl) or @fromempl = '*') --第六销售 insert #work_detail_gem01(person_id, action_id, srvdate, code, name, amt, quan, cost, stdprice, share_rate, prj_type, billid, gender, paycode, amt2, payway, cardid, memid) select gea36c, 1, gea03d, gea07c, gak02c, gsc05f, 1, 0, gsc05f, 1, gca02c, gea01c, b.gba14i, gsc04c, gea37f* dbo.fun_divide_default(gsc05f,bilamt,0), payway, gba01c, gba03c from #gem01 a, gbm01 b with(nolock), gcm01 c with(nolock),gam10 d with(nolock) where a.gea00c = b.gba00c and a.gea07c = b.gba23c and b.gba00c = c.gca00c and b.gba23c = c.gca01c and c.gca00c = c.gca13d and c.gca00c = d.gak00c and c.gca02c = d.gak01c and isnull(a.gea36c,'') <>'' and ((a.gea36c >= @fromempl and a.gea36c <= @toempl) or @fromempl = '*') --第七销售 insert #work_detail_gem01(person_id, action_id, srvdate, code, name, amt, quan, cost, stdprice, share_rate, prj_type, billid, gender, paycode, amt2, payway, cardid, memid) select gea38c, 1, gea03d, gea07c, gak02c, gsc05f, 1, 0, gsc05f, 1, gca02c, gea01c, b.gba14i, gsc04c, gea39f* dbo.fun_divide_default(gsc05f,bilamt,0), payway, gba01c, gba03c from #gem01 a, gbm01 b with(nolock), gcm01 c with(nolock),gam10 d with(nolock) where a.gea00c = b.gba00c and a.gea07c = b.gba23c and b.gba00c = c.gca00c and b.gba23c = c.gca01c and c.gca00c = c.gca13d and c.gca00c = d.gak00c and c.gca02c = d.gak01c and isnull(a.gea38c,'') <>'' and ((a.gea38c >= @fromempl and a.gea38c <= @toempl) or @fromempl = '*') --第八销售 insert #work_detail_gem01(person_id, action_id, srvdate, code, name, amt, quan, cost, stdprice, share_rate, prj_type, billid, gender, paycode, amt2, payway, cardid, memid) select gea40c, 1, gea03d, gea07c, gak02c, gsc05f, 1, 0, gsc05f, 1, gca02c, gea01c, b.gba14i, gsc04c, gea41f* dbo.fun_divide_default(gsc05f,bilamt,0), payway, gba01c, gba03c from #gem01 a, gbm01 b with(nolock), gcm01 c with(nolock),gam10 d with(nolock) where a.gea00c = b.gba00c and a.gea07c = b.gba23c and b.gba00c = c.gca00c and b.gba23c = c.gca01c and c.gca00c = c.gca13d and c.gca00c = d.gak00c and c.gca02c = d.gak01c and isnull(a.gea40c,'') <>'' and ((a.gea40c >= @fromempl and a.gea40c <= @toempl) or @fromempl = '*') --第九销售 insert #work_detail_gem01(person_id, action_id, srvdate, code, name, amt, quan, cost, stdprice, share_rate, prj_type, billid, gender, paycode, amt2, payway, cardid, memid) select gea42c, 1, gea03d, gea07c, gak02c, gsc05f, 1, 0, gsc05f, 1, gca02c, gea01c, b.gba14i, gsc04c, gea43f* dbo.fun_divide_default(gsc05f,bilamt,0), payway, gba01c, gba03c from #gem01 a, gbm01 b with(nolock), gcm01 c with(nolock),gam10 d with(nolock) where a.gea00c = b.gba00c and a.gea07c = b.gba23c and b.gba00c = c.gca00c and b.gba23c = c.gca01c and c.gca00c = c.gca13d and c.gca00c = d.gak00c and c.gca02c = d.gak01c and isnull(a.gea42c,'') <>'' and ((a.gea42c >= @fromempl and a.gea42c <= @toempl) or @fromempl = '*') --第十销售 insert #work_detail_gem01(person_id, action_id, srvdate, code, name, amt, quan, cost, stdprice, share_rate, prj_type, billid, gender, paycode, amt2, payway, cardid, memid) select gea44c, 1, gea03d, gea07c, gak02c, gsc05f, 1, 0, gsc05f, 1, gca02c, gea01c, b.gba14i, gsc04c, gea45f* dbo.fun_divide_default(gsc05f,bilamt,0), payway, gba01c, gba03c from #gem01 a, gbm01 b with(nolock), gcm01 c with(nolock),gam10 d with(nolock) where a.gea00c = b.gba00c and a.gea07c = b.gba23c and b.gba00c = c.gca00c and b.gba23c = c.gca01c and c.gca00c = c.gca13d and c.gca00c = d.gak00c and c.gca02c = d.gak01c and isnull(a.gea44c,'') <>'' and ((a.gea44c >= @fromempl and a.gea44c <= @toempl) or @fromempl = '*') --创建变量 declare @person_id varchar(20) -- 员工代码 declare @action_id tinyint declare @srvdate varchar(10) --日期 declare @code varchar(20) --项目代码,或是卡号,产品码 declare @name varchar(200) --名称 declare @payway varchar(20) --支付方式 declare @amt float --营业金额 declare @quan float --数量 declare @cost float --成本 declare @comm float --提成 declare @amt2 float --虚业绩 declare @amt3 float --真实业绩 declare @cardrate float --转卡比率 --当买卡或充值的时候,这个地方当作疗程账户金额用 declare @stdprice float --标准单价 declare @prj_type varchar(20) --项目类别 declare @share_rate float --分享比率 declare @cls_flag int -- 1:项目 2:产品 3:卡 declare @gender int --性别 0:女 1:男 declare @billid varchar(20) --单号 declare @paycode varchar(20) --支付代码 declare @ex_rate float --实际营业额折算比率 declare @ggm02_seq float --ggm02中的序列号,与单号一起来标志一个项目 declare @room_id varchar(20) --房间号,只在spa或足浴版本需要 declare @cardid varchar(20) --会员卡号 declare @memid varchar(40) --会员编号 declare @comp_ascription varchar(10) -- 作业公司 --循环变量 declare @idd int declare @state int set @state = 0 while(@state = 0) begin if(exists(select 1 from #work_detail_gem01)) begin set @state = 0 select top 1 @idd =id, @person_id = person_id, @action_id = action_id, @srvdate = srvdate, @code = code, @name = name, @payway = payway, @amt = amt, @quan = quan, @cost = cost, @comm = comm, @amt2 = amt2 * @quan , @amt3 = amt3 * @quan, @cardrate = cardrate, @stdprice = stdprice, @prj_type = prj_type, @share_rate = share_rate, @cls_flag = cls_flag, @gender = gender, @billid = billid, @paycode = paycode, @ex_rate = ex_rate, @ggm02_seq = ggm02_seq, @room_id = room_id, @cardid = cardid, @memid = memid, @comp_ascription = comp_ascription from #work_detail_gem01 --计算提成 declare @setvalue float declare @commway float -- 提成方式 declare @cardway float -- 卡提成方式 declare @fa_type varchar(20) -- 提成方案编号 -- 获取在账单有效日期内最新一条员工提成 方案 select top 1 @fa_type = ppt02c from personprecept where ppt00c = @compid and ppt03c = @person_id and ppt04i = 1 and ppt02c in (select pre01c from precept where pre00c = @compid and isnull(pre22i, 0) = 0) order by ppt05d desc --(1店总业绩 2个人总业绩 3项目/产品/卡分别算 ) select @commway = pre03i, @cardway = pre14i from precept where pre00c = @compid and pre01c = @fa_type and isnull(pre22i, 0) = 0 -- 提成设置,提成方式,提成金额或比率 select @setvalue = car06f from cardprecept with (nolock) where car00c = @compid and car01c = @prj_type and car02c = @fa_type if (@cardway = 2 or @cardway = 3) --卡提成按累计算 begin -- 计算累计提成 -- 每项计算提成设置为0 select @setvalue = 0 end select @comm = @amt2 * @setvalue INSERT INTO #work_detail_ceshi(person_id, action_id, srvdate, code, name, payway, amt, quan, cost, comm, amt2, amt3, cardrate, stdprice, prj_type, share_rate, cls_flag, gender, billid, paycode, ex_rate, ggm02_seq, room_id, compid, spid, cardid, memid, comp_ascription) VALUES (@person_id, @action_id, @srvdate, @code, @name, @payway, @amt, @quan, @cost, @comm, @amt2, @amt2, 1, @stdprice, @prj_type, 1, 3, @gender, @billid, @paycode, 1, @ggm02_seq, @room_id, @compid, @spid ,@cardid, @memid, @compid) --删除已经计算的数据 delete a from #work_detail_gem01 a where a.id = @idd end else begin select @state = 1 end end --删除临时表 drop table #gem01 drop table #gem01_amt drop table #work_detail_gem01 --End end GO /****** Object: StoredProcedure [dbo].[upg_copy_hem06] Script Date: 06/12/2017 06:19:48 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_copy_hem06](@compid varchar(10), @fromid varchar(20), @toid varchar(20)) as begin delete hem06 where hef00c = @compid and hef01c = @toid insert hem06(hef00c, hef01c, hef02f, hef03f, hef04f, hef05i, hef06f, hef08c) select @compid, @toid, hef02f, hef03f, hef04f, hef05i, hef06f, hef08c from hem06 where hef00c = @compid and hef01c = @fromid end GO /****** Object: StoredProcedure [dbo].[upg_create_good_month_cost] Script Date: 06/12/2017 06:19:53 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_create_good_month_cost] ( @compid varchar(10), @month varchar(10) ) as begin declare @curdate varchar(20), @time varchar(20) declare @priormonth varchar(20) declare @todate varchar(20) declare @date_todate datetime declare @cal_date varchar(20) select @cal_date = @month + '01' select @date_todate = dateadd(month, -1, @cal_date) select @todate = substring(convert(varchar(20), @date_todate, 102), 1, 4) + substring(convert(varchar(20), @date_todate, 102), 6, 2) + substring(convert(varchar(20), @date_todate, 102), 9, 2) select @priormonth = substring(@todate, 1, 6) --把上个月的成本数据先copy过来,因为可能本月没有入库,需要继续 create table #tbl_prior_cost ( gfh00c varchar(10) not null, -- 门店编号 gfh01c varchar(20) not null, -- 产品编号 gfh03f float null, -- 成本 gfh04d varchar(10) null, -- 计算日期 gfh05t varchar(10) null, -- 计算时间 gfh06f float null, -- 入数量 gfh07f float null, -- 出数量 gfh08i int null, -- 是否已经过期 primary key(gfh01c) ) --统计上月末(本月初的库存量 create table #begstockquan_2 --存放期初量 ( id int identity not null, goodsno varchar(20) null, --产品编号 goodsname varchar(80) null, --产品名称 garageno varchar(20) null, --仓库编号 garagename varchar(20) null, --仓库名称 unit varchar(10) null, --单位 quantity float null, --数量 amt float null, --金额 amt_cost float null, --成本单价金额 primary key nonclustered(id) ) create clustered INDEX IDX_begstockquan_03 on #begstockquan_2(goodsno, garageno) declare @prior_month_last_day varchar(20) select @prior_month_last_day = @priormonth + '31' --计算上月库存 insert #begstockquan_2(goodsno, goodsname, garageno, garagename, unit, quantity, amt, amt_cost) exec upg_stock_statistic @compid, @prior_month_last_day, '*', '', '*', '', 4, '', '*', '*', '', 0 --把上个月的数据复制过来 insert #tbl_prior_cost(gfh00c, gfh01c, gfh03f, gfh04d, gfh05t, gfh08i) select gfh00c, gfh01c, gfh03f, gfh04d, gfh05t, gfh08i from gfm08 with (nolock) where gfh00c = @compid and gfh02c = @priormonth --把期末的成本价更新过去 update a set a.amt_cost = b.gfh03f from #begstockquan_2 a, #tbl_prior_cost b where a.goodsno = b.gfh01c exec upg_getcurrdate @curdate output, @time output create table #tbl_cost_buf( goodid varchar(20) not null, num float null, amt float null, start_num float null, -- 期初量 in_num float null -- 入库量 ) create clustered index idx_tbl_cost_buf_01 on #tbl_cost_buf(goodid) --把上月末的库存也作为一次入库,只是单价应该是上月的加权成本 insert #tbl_cost_buf(goodid, num, amt, start_num) select goodsno, sum(quantity), sum(quantity*amt_cost), sum(quantity) from #begstockquan_2 a group by goodsno drop table #begstockquan_2 --正常的本月入库 insert #tbl_cost_buf(goodid, num, amt, in_num) select gfd04c, sum(gfd05f), sum(gfd05f*gfd06f), sum(gfd05f) from gfm04 with (nolock), gam26 with (nolock) where gaz01c = @compid and gfd00c = gaz02c and substring(gfd07c, 1, 6) = @month and gfd01c = '1' group by gfd04c --还需要考虑单价为0,数量为0,但金额有的 供应商返利 insert #tbl_cost_buf(goodid, num, amt) select gfd04c, 0, sum(gfd13f) from gfm04 with (nolock), gam26 with (nolock) where gaz01c = @compid and gfd00c = gaz02c and substring(gfd07c, 1, 6) = @month and gfd01c = '1' and isnull(gfd05f, 0) = 0 group by gfd04c --需要考虑供应商退货的情况,只有在gfm03才知道是这个类别 insert #tbl_cost_buf(goodid, num, amt) select gfd04c, 0- sum(gfd05f), 0 - sum(gfd05f*gfd06f) from gfm04 b with (nolock), gfm03 a with (nolock), gam26 with (nolock) where gaz01c = @compid and a.gfc00c = b.gfd00c and a.gfc01c = b.gfd01c and a.gfc02c = b.gfd02c and a.gfc06i = 3 -- 已经约定为供应商退货 and b.gfd00c = gaz02c and substring(b.gfd07c, 1, 6) = @month and gfd01c = '2' -- 出库 group by gfd04c --本月的数据 create table #tbl_cost( goodid varchar(20) not null, num float null, amt float null, start_num float null, -- 期初量 in_num float null, -- 入库量 primary key(goodid) ) insert #tbl_cost(goodid, num, amt, start_num, in_num) select goodid, sum(num), sum(amt), sum(isnull(start_num, 0)), sum(isnull(in_num, 0)) from #tbl_cost_buf group by goodid drop table #tbl_cost_buf --把本月没有的产品也要复制过来 delete #tbl_cost where isnull(num, 0) = 0 delete a from #tbl_prior_cost a, #tbl_cost b where a.gfh01c = b.goodid delete a from gfm08 a, gam26 z where gfh00c = z.gaz02c and z.gaz01c = @compid and a.gfh02c = @month --需要下发到每个子公司吗 insert gfm08(gfh00c, gfh01c, gfh02c, gfh03f, gfh04d, gfh05t, gfh06f, gfh07f, gfh08i) select gaz02c, gfh01c, @month, gfh03f, gfh04d, gfh05t, 0, 0, gfh08i from #tbl_prior_cost a, gam26 z where z.gaz01c = @compid drop table #tbl_prior_cost insert gfm08(gfh00c, gfh01c, gfh02c, gfh03f, gfh04d, gfh05t, gfh06f, gfh07f, gfh08i) select gaz02c, goodid, @month, dbo.fun_divide_default(amt, num, 0), @curdate, @time, isnull(in_num, 0), isnull(start_num, 0), 0 from #tbl_cost a, gam26 z where z.gaz01c = @compid drop table #tbl_cost end GO /****** Object: StoredProcedure [dbo].[upg_save_deleted_card] Script Date: 06/12/2017 06:20:38 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_save_deleted_card] ( @compid varchar(10), @from varchar(20), @to varchar(20), @userid varchar(20), @curdate varchar(10), @curtime varchar(10) ) as begin declare @incompid varchar(10) select @incompid = @compid declare @compcode varchar(10) declare @cardid varchar(20) declare @remain float declare @acct int declare cur_each_card cursor for select gcc00c, gcc01c, gcc06f, gcc03i from gcm03 with (nolock), gcm01 with (nolock), gam26 z where gcc00c = gca00c and gcc01c = gca01c and gca00c = gca13d and gca00c = z.gaz02c and z.gaz01c = @incompid and gca01c >= @from and gca01c <= @to and (gcc03i = 2 or gcc03i = 4) open cur_each_card fetch cur_each_card into @compcode, @cardid, @remain, @acct while @@fetch_status = 0 begin insert sysoperationlog(userid, program, operation, operationdate, operationtime, origatedate, compid, keyvalue1, keyvalue2, keyvalue3, keyvalue4) values(@userid, 'GSB01I', 'D', @curdate, @curtime, @curdate, @compcode, @cardid, convert(varchar(20), @remain), 'upg_delete_card', convert(varchar(10), @acct)) fetch cur_each_card into @compcode, @cardid, @remain, @acct end close cur_each_card deallocate cur_each_card end GO /****** Object: StoredProcedure [dbo].[upg_get_card_snop_local] Script Date: 06/12/2017 06:20:06 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_get_card_snop_local] ( @compid varchar(10), -- 门店编号 @input varchar(100) -- 输入 ) as set nocount on set rowcount 0 set transaction isolation level read uncommitted begin if isnull(@input, '') = '' return if isnull(@compid, '') = '' return declare @localcompid varchar(10) declare @menberid varchar(20) declare @cardid varchar(20) declare @gca08i int declare @gca07d varchar(8) if exists (select 1 from gcm01, gam26 where gca00c = gca13d and gca01c = @input and gaz01c = @compid and gca00c = gaz02c) begin select @localcompid = gca00c, @cardid = gca01c, @menberid = gca04c, @gca08i = gca08i, @gca07d = gca07d from gcm01 with (nolock) , gam26 with (nolock) where gca00c = gca13d and gca01c = @input and gaz01c = @compid and gca00c = gaz02c if exists(select 1 from gbm01 where gba00c = @localcompid and gba23c = @menberid) begin select gba00c, gba01c, gba03c, gba08c, gba17d, gba21c, (case when isnull(gba23c, '') = '' then @cardid else gba23c end) gba23c, gba98c, @gca08i as gca08i, @gca07d as gca07d from gbm01 with (nolock) where gba00c = @localcompid and gba23c = @menberid end else begin select @localcompid gba00c, @menberid gba01c, '' gba03c, '' gba08c, '' gba17d, '' gba21c, @input gba23c, '' gba98c, isnull(@gca08i, 0) gca08i, '' as gca07d end return end select gba00c, gba01c, gba03c, gba08c, gba17d, gba21c, (case when isnull(gba23c, '') = '' then gca01c else gba23c end) gba23c, gba98c, gca08i, gca07d from gbm01 a with (nolock) , gcm01 b with (nolock) , gam26 c with (nolock) where a.gba00c = b.gca00c and a.gba23c = b.gca01c and gca00c = gca13d and c.gaz01c = @compid and gca00c = c.gaz02c and a.gba01c not like '散客%' and gba98c like '%' + @input + '%' order by gba03c, gba08c, gba00c end GO /****** Object: StoredProcedure [dbo].[upg_client_return_info_by_person] Script Date: 06/12/2017 06:19:42 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_client_return_info_by_person] ( @compid varchar(10), @fromdate varchar(8), @todate varchar(8), @goodis varchar(5), @rettype int, @formperson varchar(20), @toperson varchar(20), @type varchar(10)-- 1-员工 2-收银员 3-门店 ) as begin create table #gbm10 ( gbj00c varchar(10) not null, -- 公司编号 gbj01i int identity not null, -- 流水号 gbj02c varchar(20) null, -- 消费单号 gbj03f float null, -- 序号 gbj04c varchar(20) null, -- 会员编号 gbj05c varchar(20) null, -- 项目编号 gbj06c varchar(20) null, -- 联系方式固定电话 gbj07d varchar(8) null, -- 服务日期 gbj08d varchar(8) null, -- 应该回访日期 gbj09c varchar(10) null, -- 回访员工编号 gbj10i int null, -- 是否已经回访 gbj11c varchar(5) null, -- 访问结果 gbj12c varchar(500) null, -- 访问结果描述 gbj13i int null, -- 类别 1:会员 2:散客 gbj14c varchar(20) null, -- 姓名 gbj15c varchar(20) null, -- 手机 gbj16i int null, -- 类别 0项目 1产品 gbj17c varchar(300) null, -- 名称 gbj18c1 varchar(20) null, --会员卡号 person varchar(20) null, --员工 、收银员 、门店 services varchar(50) null, --服务满意度 skill varchar(100) null, --技术满意度 gbj28f float null, --价格 servertype varchar(50) null, --服务类型(0-大工 1-中工 2-小工 3-四工 c-门店 s-收银员) ) declare @PK_Sql nvarchar(max) set @PK_Sql = ' alter table #gbm10 add constraint PK_#gbm10_' + convert(varchar(10), cast(floor(rand() * 1000000) as int)) + ' primary key clustered(gbj00c, gbj01i)' exec sp_executesql @PK_Sql --游标 declare @gbj01i int declare @gbj18c varchar(10) declare @gbj19c varchar(5) declare @gbj20c varchar(5) declare @gbj21c varchar(10) declare @gbj22c varchar(5) declare @gbj23c varchar(5) declare @gbj24c varchar(10) declare @gbj25c varchar(5) declare @gbj26c varchar(5) declare @gbj27c varchar(10) declare @gbj28c varchar(5) declare @gbj29c varchar(5) declare @gbj30c varchar(10) declare @gbj31c varchar(5) declare @gbj32c varchar(10) declare @gbj33c varchar(5) declare @gbj34c varchar(5) declare cur_client_gbm10 cursor for select gbj01i, gbj18c, gbj19c, gbj20c, gbj21c, gbj22c, gbj23c, gbj24c, gbj25c, gbj26c, gbj27c, gbj28c, gbj29c, gbj30c, gbj31c, gbj32c, gbj33c, gbj34c from gbm10 a, gam26 z where gbj00c = z.gaz02c and z.gaz01c = @compid and gbj08d between @fromdate and @todate and gbj10i = 1 open cur_client_gbm10 fetch next from cur_client_gbm10 into @gbj01i, @gbj18c, @gbj19c, @gbj20c, @gbj21c, @gbj22c, @gbj23c, @gbj24c, @gbj25c, @gbj26c, @gbj27c, @gbj28c, @gbj29c, @gbj30c, @gbj31c, @gbj32c, @gbj33c, @gbj34c while @@fetch_status = 0 begin if @type = '1' or @type = '*' begin if isnull(@gbj18c, '')<>''--大工 begin insert into #gbm10(gbj00c, gbj02c, gbj03f, gbj04c, gbj05c, gbj06c, gbj07d, gbj08d, gbj09c, gbj10i, gbj11c, gbj12c, gbj13i, gbj14c, gbj15c, gbj16i, gbj17c, person, services, skill, servertype) select gbj00c, gbj02c, gbj03f, gbj04c, gbj05c, gbj06c, gbj07d, gbj08d, gbj09c, gbj10i, gbj11c, gbj12c, gbj13i, gbj14c, gbj15c, gbj16i, gbj17c, (select top 1 haa01c + '-' + haa02c from ham01 where haa00c = gbj00c and haa01c = @gbj18c), (select top 1 gsb03c from gsm02 where gsb01c = 'A1' and gsb00c = gbj00c and gsb02c = @gbj19c and (gsb02c = @goodis or @goodis = '*')), (select top 1 gsb03c from gsm02 where gsb01c = 'A1' and gsb00c = gbj00c and gsb02c = @gbj20c and (gsb02c = @goodis or @goodis = '*')), '0' from gbm10 where gbj01i = @gbj01i and ((gbj18c >= @formperson and gbj18c <= @toperson)or @formperson = '*') and (gbj19c = @goodis or gbj20c = @goodis or @goodis = '*') end if isnull(@gbj21c, '')<>''--中工 begin insert into #gbm10(gbj00c, gbj02c, gbj03f, gbj04c, gbj05c, gbj06c, gbj07d, gbj08d, gbj09c, gbj10i, gbj11c, gbj12c, gbj13i, gbj14c, gbj15c, gbj16i, gbj17c, person, services, skill, servertype) select gbj00c, gbj02c, gbj03f, gbj04c, gbj05c, gbj06c, gbj07d, gbj08d, gbj09c, gbj10i, gbj11c, gbj12c, gbj13i, gbj14c, gbj15c, gbj16i, gbj17c, (select top 1 haa01c + '-' + haa02c from ham01 where haa00c = gbj00c and haa01c = @gbj21c), (select top 1 gsb03c from gsm02 where gsb01c = 'A1' and gsb00c = gbj00c and gsb02c = @gbj22c and (gsb02c = @goodis or @goodis = '*')), (select top 1 gsb03c from gsm02 where gsb01c = 'A1' and gsb00c = gbj00c and gsb02c = @gbj23c and (gsb02c = @goodis or @goodis = '*')), '1' from gbm10 where gbj01i = @gbj01i and ((gbj21c >= @formperson and gbj21c <= @toperson)or @formperson = '*' ) and (gbj22c = @goodis or gbj23c = @goodis or @goodis = '*') end if isnull(@gbj24c, '')<>''--小工 begin insert into #gbm10(gbj00c, gbj02c, gbj03f, gbj04c, gbj05c, gbj06c, gbj07d, gbj08d, gbj09c, gbj10i, gbj11c, gbj12c, gbj13i, gbj14c, gbj15c, gbj16i, gbj17c, person, services, skill, servertype) select gbj00c, gbj02c, gbj03f, gbj04c, gbj05c, gbj06c, gbj07d, gbj08d, gbj09c, gbj10i, gbj11c, gbj12c, gbj13i, gbj14c, gbj15c, gbj16i, gbj17c, (select top 1 haa01c + '-' + haa02c from ham01 where haa00c = gbj00c and haa01c = @gbj24c), (select top 1 gsb03c from gsm02 where gsb01c = 'A1' and gsb00c = gbj00c and gsb02c = @gbj25c and (gsb02c = @goodis or @goodis = '*')), (select top 1 gsb03c from gsm02 where gsb01c = 'A1' and gsb00c = gbj00c and gsb02c = @gbj26c and (gsb02c = @goodis or @goodis = '*')), '2' from gbm10 where gbj01i = @gbj01i and ((gbj24c >= @formperson and gbj24c <= @toperson)or @formperson = '*' ) and (gbj25c = @goodis or gbj26c = @goodis or @goodis = '*') end if isnull(@gbj27c, '')<>''--四工 begin insert into #gbm10(gbj00c, gbj02c, gbj03f, gbj04c, gbj05c, gbj06c, gbj07d, gbj08d, gbj09c, gbj10i, gbj11c, gbj12c, gbj13i, gbj14c, gbj15c, gbj16i, gbj17c, person, services, skill, servertype) select gbj00c, gbj02c, gbj03f, gbj04c, gbj05c, gbj06c, gbj07d, gbj08d, gbj09c, gbj10i, gbj11c, gbj12c, gbj13i, gbj14c, gbj15c, gbj16i, gbj17c, (select top 1 haa01c + '-' + haa02c from ham01 where haa00c = gbj00c and haa01c = @gbj27c), (select top 1 gsb03c from gsm02 where gsb01c = 'A1' and gsb00c = gbj00c and gsb02c = @gbj28c and (gsb02c = @goodis or @goodis = '*')), (select top 1 gsb03c from gsm02 where gsb01c = 'A1' and gsb00c = gbj00c and gsb02c = @gbj29c and (gsb02c = @goodis or @goodis = '*')), '3' from gbm10 where gbj01i = @gbj01i and ((gbj27c >= @formperson and gbj27c <= @toperson)or @formperson = '*' ) and (gbj28c = @goodis or gbj29c = @goodis or @goodis = '*') end end if @type = '2' or @type = '*' begin if isnull(@gbj30c, '')<>''--前台 begin insert into #gbm10(gbj00c, gbj02c, gbj03f, gbj04c, gbj05c, gbj06c, gbj07d, gbj08d, gbj09c, gbj10i, gbj11c, gbj12c, gbj13i, gbj14c, gbj15c, gbj16i, gbj17c, person, services, skill, servertype) select gbj00c, gbj02c, gbj03f, gbj04c, gbj05c, gbj06c, gbj07d, gbj08d, gbj09c, gbj10i, gbj11c, gbj12c, gbj13i, gbj14c, gbj15c, gbj16i, gbj17c, (select top 1 gaa01c + '-' + gaa06c from gam01 where gaa01c = @gbj30c), (select top 1 gsb03c from gsm02 where gsb01c = 'A1' and gsb00c = gbj00c and gsb02c = @gbj31c and (gsb02c = @goodis or @goodis = '*')), '', 's' from gbm10 where gbj01i = @gbj01i and ((gbj30c >= @formperson and gbj30c <= @toperson)or @formperson = '*' ) and (gbj31c = @goodis or @goodis = '*') end end if @type = '3' or @type = '*' begin if isnull(@gbj32c, '')<>''--门店 begin insert into #gbm10(gbj00c, gbj02c, gbj03f, gbj04c, gbj05c, gbj06c, gbj07d, gbj08d, gbj09c, gbj10i, gbj11c, gbj12c, gbj13i, gbj14c, gbj15c, gbj16i, gbj17c, person, services, skill, servertype) select gbj00c, gbj02c, gbj03f, gbj04c, gbj05c, gbj06c, gbj07d, gbj08d, gbj09c, gbj10i, gbj11c, gbj12c, gbj13i, gbj14c, gbj15c, gbj16i, gbj17c, (select top 1 gae01c + '-' + gae03c from gam05 where gae01c = @gbj32c), (select top 1 gsb03c from gsm02 where gsb01c = 'A1' and gsb00c = gbj00c and gsb02c = @gbj33c and (gsb02c = @goodis or @goodis = '*')), (select top 1 gsb03c from gsm02 where gsb01c = 'A1' and gsb00c = gbj00c and gsb02c = @gbj34c and (gsb02c = @goodis or @goodis = '*')), 'c' from gbm10 where gbj01i = @gbj01i and ((gbj32c >= @formperson and gbj32c <= @toperson)or @formperson = '*' ) and (gbj33c = @goodis or gbj34c = @goodis or @goodis = '*') end end fetch next from cur_client_gbm10 into @gbj01i, @gbj18c, @gbj19c, @gbj20c, @gbj21c, @gbj22c, @gbj23c, @gbj24c, @gbj25c, @gbj26c, @gbj27c, @gbj28c, @gbj29c, @gbj30c, @gbj31c, @gbj32c, @gbj33c, @gbj34c end close cur_client_gbm10 deallocate cur_client_gbm10 ---项目回访 update a set gbj18c1 = gca01c from #gbm10 a, gcm01 where gbj04c = gca04c and gca00c = gca13d and substring(gbj04c, 1, 2)<>'散客' if @rettype = 1 begin select distinct isnull(gbj00c,'') gbj00c, isnull(gbj02c,'') gbj02c, isnull(gbj04c,'') gbj04c, isnull(gbj14c,'') gbj14c, isnull(gbj15c,'') gbj15c, isnull(gbj17c,'') gbj17c, isnull(gbj12c,'') gbj12c, isnull((case when servertype = 's' then '收银员' when servertype = 'c' then '门店' else '员工' end),'') type, isnull(person,'') person, isnull(services,'') services, isnull(skill,'') skill, isnull(gbj28f,'') gbj28f, isnull( (case when servertype = 's' or servertype = 'c' then '-' when servertype = '0' then isnull((select gsb03c from gsm02 where gsb01c = 'AK' and gsb00c = gbj00c and gsb02c = '1'), '大工') when servertype = '1' then isnull((select gsb03c from gsm02 where gsb01c = 'AK' and gsb00c = gbj00c and gsb02c = '2'), '中工') when servertype = '2' then isnull((select gsb03c from gsm02 where gsb01c = 'AK' and gsb00c = gbj00c and gsb02c = '3'), '小工') when servertype = '3' then isnull((select gsb03c from gsm02 where gsb01c = 'AK' and gsb00c = gbj00c and gsb02c = '4'), '四工') end),'') servertype, substring(gbj07d, 1, 4) + '-' + substring(gbj07d, 5, 2) + '-' + substring(gbj07d, 7, 2) as gbj07d, substring(gbj08d, 1, 4) + '-' + substring(gbj08d, 5, 2) + '-' + substring(gbj08d, 7, 2) as gbj08d --增加了gbj07d和gbj08d,服务日期和回访日期 from #gbm10 order by gbj02c, person end else begin declare @stasum float declare @stasum0 float declare @stasum1 float declare @stasum2 float declare @stasum3 float declare @stasum4 float create table #gbm10_per ( compid varchar(10) not null, statype varchar(100) not null, staclass varchar(50) null, staname varchar(50) null, fcount float null, sumcount float null ) create table #gbm10_sum ( compid varchar(10) not null, statype varchar(100) null, --长度太短,内容过长,造成截断的问题,所以加长了长度到20 stacount float null, staclass varchar(50) null ) insert into #gbm10_per(compid, statype, staname, staclass) select gsb00c, gsb02c, gsb03c, '0' from gsm02 where gsb00c = @compid and gsb01c = 'A1' insert into #gbm10_per(compid, statype, staname, staclass) select gsb00c, gsb02c, gsb03c, '1' from gsm02 where gsb00c = @compid and gsb01c = 'A1' insert into #gbm10_per(compid, statype, staname, staclass) select gsb00c, gsb02c, gsb03c, '2' from gsm02 where gsb00c = @compid and gsb01c = 'A1' insert into #gbm10_per(compid, statype, staname, staclass) select gsb00c, gsb02c, gsb03c, '3' from gsm02 where gsb00c = @compid and gsb01c = 'A1' insert into #gbm10_per(compid, statype, staname, staclass) select gsb00c, gsb02c, gsb03c, '4' from gsm02 where gsb00c = @compid and gsb01c = 'A1' insert into #gbm10_sum(compid, statype, stacount, staclass) select gbj00c, services, count(services), '0' from #gbm10 where isnull(services, '')<>'' group by gbj00c, services insert into #gbm10_sum(compid, statype, stacount, staclass) select gbj00c, skill, count(skill), '1' from #gbm10 where isnull(skill, '')<>'' group by gbj00c, skill update a set a.fcount = isnull(b.stacount, 0) from #gbm10_per a, (select compid, statype, sum(stacount) stacount, staclass from #gbm10_sum group by compid, statype, staclass) b where a.staname = b.statype and a.staclass = b.staclass select @stasum = sum(isnull(fcount, 0)) from #gbm10_per select isnull(compid,'') compid, isnull(statype,'') statype, isnull(staname,'') staname, isnull(sum(convert(decimal(18, 2), isnull(fcount, 0))),0) fcount, (case when isnull(@stasum, 0) = 0 then 0 else convert(decimal(18, 2), sum(isnull(fcount, 0))/@stasum) end) sumcount from #gbm10_per group by compid, statype, staname order by statype asc end drop table #gbm10 end GO /****** Object: StoredProcedure [dbo].[upg_recover_pay_category] Script Date: 06/12/2017 06:20:33 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_recover_pay_category] as begin -- 判断是否已做数据恢复 if exists (select 1 from data_recover where rdd00c = '001' and rdd01c = 'pay_category' and rdd02c = 'pay_category') begin return end insert into pay_category(code, caption) values ('1', '现金类') insert into pay_category(code, caption) values ('2', '卡类') insert into pay_category(code, caption) values ('3', '抵用类') insert into pay_category(code, caption) values ('4', '欠款类') insert into pay_category(code, caption) values ('5', '免单类') insert into data_recover(rdd00c, rdd01c, rdd02c) values ('001', 'pay_category', 'pay_category') end GO /****** Object: StoredProcedure [dbo].[upg_copy_hem07_from] Script Date: 06/12/2017 06:19:48 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_copy_hem07_from] ( @compid varchar(10), @frompersonid varchar(20), @topersonid varchar(20) ) as begin delete hem07 where heg00c = @compid and heg01c = @topersonid insert hem07(heg00c, heg01c, heg02c, heg03i, heg04f, heg05i, heg06f, heg07i, heg08f, heg09i, heg10f) select heg00c, @topersonid, heg02c, heg03i, heg04f, heg05i, heg06f, heg07i, heg08f, heg09i, heg10f from hem07 where heg00c = @compid and heg01c = @frompersonid end GO /****** Object: StoredProcedure [dbo].[upg_m_dispatch_personprecept] Script Date: 06/12/2017 06:20:22 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_m_dispatch_personprecept] ( @fromcompid varchar(10), -- 从哪一个公司发布 @tocompid varchar(10), -- 目的公司 @mandatory varchar(2) -- 是否强制统一 ) as begin if @fromcompid = @tocompid return if @mandatory = '1' begin delete personprecept where ppt00c = @tocompid insert personprecept(ppt00c, ppt02c, ppt03c, ppt04i, ppt05d, ppt05c, ppt06c, ppt07c, ppt08c, ppt09c, ppt10c, ppt11i) select @tocompid, ppt02c, ppt03c, ppt04i, ppt05d, ppt05c, ppt06c, ppt07c, ppt08c, ppt09c, ppt10c, ppt11i from personprecept where ppt00c = @fromcompid and ppt03c in (select haa01c from ham01 where haa00c = @tocompid) end end GO /****** Object: StoredProcedure [dbo].[upg_emp_salary_set_liu] Script Date: 06/12/2017 06:20:02 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_emp_salary_set_liu] ( @oldcomid varchar(10), @oldempid varchar(20), @comid varchar(10), @empid varchar(20), @opeid varchar(20) ) as begin insert personprecept_log(ppl00c, ppl02c, ppl03c, ppl04d, ppl05d, ppl06c, ppl07d, ppl08c) select ppt00c as ppl00c, ppt02c as ppl02c, ppt03c as ppl03c, ' ', ' ', '删除' as ppl06c, getdate() as ppl07d, @opeid as ppl08c from personprecept with (nolock) where ppt00c = @comid and ppt03c = @empid delete from personprecept where ppt00c = @comid and ppt03c = @empid insert personprecept(ppt00c, ppt02c, ppt03c, ppt04i, ppt05d, ppt05c, ppt06c, ppt07c, ppt08c, ppt09c, ppt10c) select @comid, ppt02c, @empid, ppt04i, ppt05d, ppt05c, ppt06c, ppt07c, ppt08c, ppt09c, ppt10c from personprecept with (nolock) where ppt00c = @oldcomid and ppt03c = @oldempid insert personprecept_log(ppl00c, ppl02c, ppl03c, ppl04d, ppl05d, ppl06c, ppl07d, ppl08c) select ppt00c as ppl00c, ppt02c as ppl02c, ppt03c as ppl03c, ' ', ' ', '新增' as ppl06c, getdate() as ppl07d, @opeid as ppl08c from personprecept with (nolock) where ppt00c = @comid and ppt03c = @empid end GO /****** Object: StoredProcedure [dbo].[upg_m_dispatch_gdn01] Script Date: 06/12/2017 06:20:19 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_m_dispatch_gdn01]( @fromcompid varchar(10), -- 从哪一个公司发布 @tocompid varchar(10), -- 目的公司 @mandatory varchar(2)) -- 是否强制统一 as begin --Add by maql 20100506 declare @curdate varchar(20), @curtime varchar(20) exec upg_getcurrdate @curdate output, @curtime output if @fromcompid = @tocompid return --需要记录日志 create table #gdn01_01 -- 项目 产品消耗类别设定 ( gdn00c varchar(10) not null, -- 公司别 gdn01c varchar(20) not null, -- 项目编号 gdn02c varchar(20) not null, -- 产品类别--产品统计分类AL ) declare @PK_Sql nvarchar(max) set @PK_Sql = ' alter table #gdn01_01 add constraint PK_#gdn01_01_' + convert(varchar(10), cast(floor(rand() * 1000000) as int)) + ' primary key clustered(gdn00c, gdn01c, gdn02c)' exec sp_executesql @PK_Sql insert #gdn01_01(gdn00c, gdn01c, gdn02c) select gdn00c, gdn01c, gdn02c from gdn01 where gdn00c = @fromcompid if @mandatory = '1' begin delete gdn01 where gdn00c = @tocompid end else begin delete a from #gdn01_01 a, gdn01 b where a.gda00c = @fromcompid and a.gda01c = b.gda01c and b.gda00c = @tocompid end insert gdn01(gdn00c, gdn01c, gdn02c) select @tocompid, gdn01c, gdn02c from #gdn01_01 where gdn00c = @fromcompid --写入sysoperationlog Add by maql 20100506 insert sysoperationlog(userid, program, operation, operationdate, operationtime, origatedate, compid, keyvalue1, keyvalue2, keyvalue3) values('system', 'GSB02I', 'M', @curdate, @curtime, @curdate, @fromcompid, @tocompid, @mandatory, 'upg_m_dispatch_gdn01') drop table #gdn01_01 end GO /****** Object: StoredProcedure [dbo].[upg_m_dispatch_pay_classify_single] Script Date: 06/12/2017 06:20:22 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_m_dispatch_pay_classify_single] ( @fromcompid varchar(10), -- 从哪一个公司发布 @tocompid varchar(10), -- 目的公司 @mandatory varchar(2) -- 是否强制统一 ) as begin if @fromcompid = @tocompid return if @mandatory <> '1' and @mandatory <> '0' return if @mandatory = '1' begin delete pay_classify where comp = @tocompid end create table #pay_classify_dispatch_single_key -- 疗程资料关键字 ( pay varchar(5) not null, -- 支付方式 ) insert into #pay_classify_dispatch_single_key(pay) select pay from pay_classify where comp = @fromcompid declare @current_pay varchar(5) -- 支付方式 -- 创建循环变量 declare @HasRecord int set @HasRecord = 1 declare @Index int set @Index = -1 while(@HasRecord = 1) begin if exists ( select 1 from #pay_classify_dispatch_single_key) begin set @HasRecord = 1 select top 1 @current_pay = pay from #pay_classify_dispatch_single_key if not exists(select 1 from pay_classify where comp = @tocompid and pay = @current_pay) begin insert pay_classify(comp, pay, category) select @tocompid, pay, category from pay_classify where comp = @fromcompid and pay = @current_pay end delete from #pay_classify_dispatch_single_key where pay = @current_pay end else begin set @HasRecord = 0 end end drop table #pay_classify_dispatch_single_key end GO /****** Object: StoredProcedure [dbo].[upg_m_dispatch_pay_classify] Script Date: 06/12/2017 06:20:21 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_m_dispatch_pay_classify] ( @fromcompid varchar(10), -- 从哪一个公司发布 @tocompid varchar(10), -- 目的公司 @mandatory varchar(2) -- 是否强制统一 ) as begin if @fromcompid = @tocompid return if @mandatory <> '1' and @mandatory <> '0' return if @mandatory = '1' begin delete pay_classify where comp = @tocompid insert pay_classify(comp, pay, category) select @tocompid, pay, category from pay_classify where comp = @fromcompid return end create table #pay_classify_dispatch_single_key -- 疗程资料关键字 ( pay varchar(5) not null -- 支付方式 ) insert into #pay_classify_dispatch_single_key(pay) select pay from pay_classify where comp = @fromcompid declare @current_pay varchar(5) -- 支付方式 -- 创建循环变量 declare @HasRecord int set @HasRecord = 1 declare @Index int set @Index = -1 while(@HasRecord = 1) begin if exists (select 1 from #pay_classify_dispatch_single_key) begin set @HasRecord = 1 select top 1 @current_pay = pay from #pay_classify_dispatch_single_key if not exists (select 1 from pay_classify where comp = @tocompid and pay = @current_pay) begin insert pay_classify(comp, pay, category) select @tocompid, pay, category from pay_classify where comp = @fromcompid and pay = @current_pay end delete from #pay_classify_dispatch_single_key where pay = @current_pay end else begin set @HasRecord = 0 end end drop table #pay_classify_dispatch_single_key end GO /****** Object: StoredProcedure [dbo].[upg_changeproject_info] Script Date: 06/12/2017 06:19:39 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_changeproject_info] ( @compid varchar(10), @billid varchar(20), @cardno varchar(20), @date varchar(8) ) as begin create table #buffer_hem08 ( hei00c varchar(10) not null, --公司 hei01c varchar(20) not null, --单号 hei02c varchar(20) not null, --卡号 hei03d varchar(8) null, --日期 hei04c varchar(20) not null, --疗程换疗程疗程的编号 hei05f float null, --疗程换疗程的实际更换的金额 hei06f float null, --疗程换疗程实际更换的次数 hei07f float null, --当时更换金额 hei08f float null, --当时更换单价 hei09f float null, --当时的剩余金额 hei10f float null, --当时的剩余次数 hei11i int null, --价格序号 hei12f float null, --折扣 hei13i int null, --疗程序号 ) insert #buffer_hem08(hei00c, hei01c, hei02c, hei03d, hei04c, hei05f, hei06f, hei07f, hei08f, hei09f, hei10f, hei11i, hei12f, hei13i) select hei00c, hei01c, hei02c, hei03d, hei04c, hei05f, hei06f, hei07f, hei08f, hei09f, hei10f, hei11i, hei12f, hei13i from hem08 where hei00c = @compid and hei01c = @billid and hei02c = @cardno declare @cardcompid varchar(10) select @cardcompid = min(gca13d) from gcm01 where gca01c = @cardno and gca00c = gca13d update a set gcf06f = isnull(gcf06f, 0) - isnull(hei06f, 0) -- 已经使用次数 , gcf07f = isnull(gcf07f, 0) + isnull(hei06f, 0) -- 剩余次数 , gcf09f = isnull(gcf09f, 0) - isnull(hei05f, 0) , gcf10f = isnull(gcf10f, 0) + isnull(hei05f, 0) from gcm06 a, #buffer_hem08 b where gcf01c = @cardno and a.gcf00c = @cardcompid and a.gcf01c = b.hei02c and a.gcf02c = b.hei04c and gcf00c = @compid and hei01c = @billid and a.gcf17i = b.hei11i and a.gcf18f = b.hei12f and a.gcf23i = b.hei13i declare @projectno varchar(20) declare @projectamt float declare @projecttimes float declare @pricetype int declare @discount float declare @procid int delete hem08 where hei00c = @compid and hei01c = @billid and hei02c = @cardno and hei03d = @date delete gcm08 where gch00c = @compid and gch11c = @billid and gch03c = @cardno and gch02d = @date delete #buffer_hem08 declare cursor_01 cursor for select hei04c, hei05f, hei06f, hei11i, hei12f, hei13i from #buffer_hem08 open cursor_01 fetch cursor_01 into @projectno, @projectamt, @projecttimes, @pricetype, @discount, @procid while @@fetch_status = 0 begin --换疗程的时候,gcm08需要处理,debug insert gcm08(gch00c, gch02d, gch03c, gch04c, gch05f, gch06f, gch07f, gch08f, gch09f, gch10c, gch11c, gch12i, gch13f, gch16i) values(@compid, @date, @cardno, @projectno, 0, 0, 0, (-1)*@projecttimes, (-1)*@projectamt, 'pj', @billid, @pricetype, @discount, @procid) fetch cursor_01 into @projectno, @projectamt, @projecttimes, @pricetype, @discount, @procid end close cursor_01 deallocate cursor_01 end GO /****** Object: StoredProcedure [dbo].[upg_m_dispatch_gam28] Script Date: 06/12/2017 06:20:18 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_m_dispatch_gam28] ( @fromcompid varchar(10), -- 从哪一个公司发布 @tocompid varchar(10), -- 目的公司 @mandatory varchar(2) -- 是否强制统一 ) as begin if @fromcompid = @tocompid return if @mandatory = '1' begin delete gam28 where compid = @tocompid end -- 插入 insert into gam28 (compid, cardtype, prjtype, ratio) select @tocompid, cardtype, prjtype, ratio from gam28 where compid = @fromcompid and (cardtype + '@' + prjtype) not in (select cardtype + '@' + prjtype from gam28 where compid = @tocompid) end GO /****** Object: StoredProcedure [dbo].[upg_log_send_sms] Script Date: 06/12/2017 06:20:18 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_log_send_sms] ( @compid varchar(10), @userid varchar(20), @mobile varchar(200), @content varchar(200), @date varchar(8) ) as begin declare @curdate varchar(20) declare @time varchar(20) exec upg_getcurrdate @curdate output, @time output insert gxm04_log(gxd04c, gxd06c, compid, userid, ddate, ttime) values(@mobile, @content, @compid, @userid, @date, @time) end GO /****** Object: StoredProcedure [dbo].[upg_change_card_status] Script Date: 06/12/2017 06:19:38 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_change_card_status] ( @compid varchar(10), @from varchar(20), @to varchar(20), @state int, @ddate varchar(20), -- 日期 @userid varchar(20) ) as begin declare @curdate varchar(20) declare @curtime varchar(20) exec upg_getcurrdate @curdate output, @curtime output -- 写入sysoperationlog的日志 insert sysoperationlog(userid, program, operation, operationdate, operationtime, origatedate, compid, keyvalue1, keyvalue2, keyvalue3, keyvalue4) values(@userid, 'GMB09I', 'M', @curdate, @curtime, @curdate, @compid, @from, @to, 'change_card_status', @state) insert into gcm02(gcb00c, gcb01c, gcb02f, gcb03i, gcb04c, gcb05i, gcb06i, gcb07d) select gca00c, gca01c, (select isnull(max(gcb02f), 0) + 1 from gcm02 with (nolock) where gcb00c = gca00c and gcb01c = gca01c), -1, '', gca08i, @state, @ddate from gcm01 a with (nolock) where gca00c = gca13d and gca01c >= @from and gca01c <= @to update a set gca08i = @state from gcm01 a where a.gca00c = a.gca13d and gca01c >= @from and gca01c <= @to if @state = 4 or @state = 5 begin update a set gca16d = '', gca17d = '' from gcm01 a where a.gca00c = a.gca13d and gca01c >= @from and gca01c <= @to end end GO /****** Object: StoredProcedure [dbo].[upg_delete_log] Script Date: 06/12/2017 06:19:59 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_delete_log]( @compid varchar(10), @from varchar(8), @to varchar(8), @login varchar(10), @info varchar(20)) as begin declare @curdate varchar(20), @curtime varchar(20) exec upg_getcurrdate @curdate output, @curtime output insert sysoperationlog(userid, program, operation, operationdate, operationtime, origatedate, compid, keyvalue1, keyvalue2, keyvalue3, keyvalue4) values(@login, 'GSB24I', 'M', @curdate, @curtime, @curdate, @compid, @from, @to, @info, '数据处理中心') end GO /****** Object: StoredProcedure [dbo].[upg_app_confirm_grm01] Script Date: 06/12/2017 06:19:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_app_confirm_grm01] ( @compid varchar(10), @billid varchar(20), @userid varchar(20), -- 经理终端的登录者代码 @confirm int -- 1 审核 0 取消审核 ) as begin declare @curdate varchar(20) declare @time varchar(20) exec upg_getcurrdate @curdate output, @time output end GO /****** Object: StoredProcedure [dbo].[upg_delete_open_bill] Script Date: 06/12/2017 06:19:59 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_delete_open_bill] ( @compid varchar(10), @confirmer varchar(20), -- 删除确认人 @type int, -- 1-删除完整单据 2-删除项目 3-删除产品 @billid varchar(20), @flagid int ) as begin declare @curdate varchar(20), @time varchar(20) exec upg_getcurrdate @curdate output, @time output if (@type = 1 or @type = 2) begin insert openpolicy_detail_del(compid, billid, flagid, project, servicer, price, number, amount, empa, empb, empc, empaflag, empbflag, empcflag, empd, empdflag, userid, ddate, ttime) select compid, billid, flagid, project, servicer, price, number, amount, empa, empb, empc, empaflag, empbflag, empcflag, empd, empdflag, @confirmer, @curdate, @time from openpolicy_detail where compid = @compid and billid = @billid and ((@type = 1) or (@type = 2 and flagid = @flagid)) delete openpolicy_detail where compid = @compid and billid = @billid and ((@type = 1) or (@type = 2 and flagid = @flagid)) end if ( @type = 1 or @type = 3) begin insert openpolicy_prod_del(compid, billid, flagid, project, servicer, price, number, amount, empa, empb, empc, userid, ddate, ttime) select compid, billid, flagid, project, servicer, price, number, amount, empa, empb, empc, @confirmer, @curdate, @time from openpolicy_prod where compid = @compid and billid = @billid and ((@type = 1) or (@type = 3 and flagid = @flagid)) delete openpolicy_prod where compid = @compid and billid = @billid and ((@type = 1) or (@type = 3 and flagid = @flagid)) end if ( @type = 1) begin insert openpolicy_master_del(compid, billid, person, datetime, memtype, memcard, billflag, remark, keyid, gender, userid, ddate, ttime) select compid, billid, person, datetime, memtype, memcard, billflag, remark, keyid, gender, @confirmer, @curdate, @time from openpolicy_master where compid = @compid and billid = @billid delete a from openpolicy_master a where compid = @compid and billid = @billid end end GO /****** Object: StoredProcedure [dbo].[upg_batch_postpone] Script Date: 06/12/2017 06:19:30 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_batch_postpone] ( @compid varchar(10), @ChangeNo varchar(20), @CurrDate varchar(8), @UserId varchar(10), @iTerm int, @ExpiredDate varchar(8) ) as begin begin transaction begin try update gem01 set gea95c = @UserId, gea96d = @CurrDate where gea00c = @compid and gea01c = @ChangeNo and gea02i = 9 if @iTerm > 0 begin update gcm01 set gca07d = convert(varchar(8), dateadd(day, @iTerm, gca07d), 112) from gcm01, gem03 where gca00c = @compid and gca01c = gec02c and gec00c = @compid and gec01c = @ChangeNo end else if @ExpiredDate is not null begin update gcm01 set gca07d = @ExpiredDate from gcm01, gem03 where gca00c = @compid and gca01c = gec02c and gec00c = @compid and gec01c = @ChangeNo end insert into gcm02(gcb00c, gcb01c, gcb02f, gcb03i, gcb04c, gcb05i, gcb06i, gcb07d) select gec00c, gec02c, (select isnull(max(gcb02f), 0 ) + 1 from gcm02 with (nolock) where gcb00c = @compid and gcb01c = gec02c), 9, gec01c, gca08i, gca08i, gea03d from gem01, gem03, gcm01 where gea00c = @compid and gea01c = @ChangeNo and gec00c = gea00c and gec01c = gea01c and gca00c = gea00c and gca01c = gec02c end try begin catch if @@TRANCOUNT > 0 begin rollback transaction declare @SP_Parameters varchar(max) set @SP_Parameters = '@compid = ' + isnull(@compid, '') + '@ChangeNo = ' + isnull(@ChangeNo, '') + '@CurrDate = ' + isnull(@CurrDate, '') + '@UserId = ' + isnull(@UserId, '') + '@iTerm = ' + (case when @iTerm is null then '' else convert(varchar(100), @iTerm) end) insert into SP_TranLog (SPName , SPTime , SPParameter) values ( 'upg_batch_postpone' , getdate() , @SP_Parameters) raiserror ('transaction is failed.', 16, 1) with NOWAIT end end catch if @@TRANCOUNT > 0 begin commit transaction end end GO /****** Object: StoredProcedure [dbo].[upg_get_deficit_info] Script Date: 06/12/2017 06:20:06 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_get_deficit_info] ( @comp varchar(10), @card varchar(20), @account int ) as begin create table #result ( gcc03i int not null, gcc10f float null, -- 帐户欠款 gsb03c varchar(40) null, gcc00c varchar(10) null, -- 欠款发生门店 gcl09c varchar(20) null, -- 销售人员(编号) gcl09c_name varchar(20) null, -- 销售人员(姓名) ) create clustered index idx_result_001 on #result(gcc03i) if isnull(@account, 0) = 0 begin insert into #result(gcc03i, gcc10f, gsb03c) select p.gcc03i, p.gcc10f, q.gsb03c from (select gcc03i, sum(isnull(gcc10f, 0)) gcc10f from gcm03 with(nolock) where gcc00c = @comp and gcc01c = @card and (gcc03i <> 1 and gcc03i <> 3) and gcc10f > 0 group by gcc03i) p left outer join (select gsb02c, gsb03c from gsm02 with (nolock) where gsb00c = @comp and gsb01c = 'o') q on p.gcc03i = q.gsb02c end else begin insert into #result(gcc03i, gcc10f, gsb03c) select p.gcc03i, p.gcc10f, q.gsb03c from (select gcc03i, sum(isnull(gcc10f, 0)) gcc10f from gcm03 with(nolock) where gcc00c = @comp and gcc01c = @card and gcc03i = @account group by gcc03i) p left outer join (select gsb02c, gsb03c from gsm02 with (nolock) where gsb00c = @comp and gsb01c = 'o') q on p.gcc03i = q.gsb02c end declare @gcc03i int declare @gcl09c varchar(20) declare @gcl09c_name varchar(20) declare @current_comp varchar(10) declare cur_update_each_gcl09c cursor for select gcc03i from #result open cur_update_each_gcl09c fetch cur_update_each_gcl09c into @gcc03i while @@fetch_status = 0 begin set @gcl09c = '' set @current_comp = '' set @gcl09c_name = '' select @gcl09c = gcl09c, @current_comp = gcl00c from (select top 1 gcl09c, --销售人员 gcl00c --门店编号 from gcm10 with(nolock) where gcl03c = @card and gcl06i = 5 and gcl04c = @gcc03i and isnull(gcl09c, '') <> '' order by gcl02d desc, gcl01c desc) T --如果为空,则可能是竞争转卡过来的 if(isnull(@gcl09c,'') = '') begin select @gcl09c = gea21c, @current_comp = gea00c from gem01 with(nolock) where gea07c = @card end select @gcl09c_name = haa02c --员工姓名 from ham01 with (nolock) where haa00c = @current_comp and haa01c = @gcl09c update #result set gcl09c = @gcl09c, gcl09c_name = @gcl09c_name, gcc00c = @current_comp where gcc03i = @gcc03i fetch cur_update_each_gcl09c into @gcc03i end close cur_update_each_gcl09c deallocate cur_update_each_gcl09c declare @total float select @total = sum(gcc10f) from #result select top 1 @gcl09c = gcl09c from gcm10 with(nolock) where gcl03c = @card and gcl06i = 5 order by gcl02d desc, gcl01c desc select * from #result select @total as total, @gcl09c as gcl09c drop table #result end GO /****** Object: StoredProcedure [dbo].[upg_search_memid_from_card] Script Date: 06/12/2017 06:20:38 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_search_memid_from_card] ( @cardid varchar(20) ) as begin declare @oldcardid varchar(20) declare @memid varchar(20) --找到新卡对应的会员编号 select @memid = gca04c from gcm01 with (nolock) where gca01c = @cardid --如果这个会员编号不存在于gbm01中 if not exists(select 1 from gbm01 where gba01c = @memid) begin --寻原来的卡号 select @oldcardid = gea04c from gem01 where gea07c = @cardid and gea02i = 4 if isnull(@oldcardid, '')<>'' begin select @memid = gca04c from gcm01 with (nolock) where gca01c = @oldcardid if exists(select 1 from gbm01 where gba01c = @memid) begin update gbm01 set gba23c = @cardid where gba01c = @memid update gcm01 set gca04c = @memid where gca01c = @cardid end end end end GO /****** Object: StoredProcedure [dbo].[upg_copy_productprecept] Script Date: 06/12/2017 06:19:48 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create proc [dbo].[upg_copy_productprecept] ( @compid varchar(10), @fromfatype varchar(20), -- 从哪一个方案编号复制 @tofatype varchar(20), -- 目的方案编号 @itemCode varchar(20), -- 从哪一个方案编号的项目编号 @itemTypefrom varchar(10), @itemTypeTo varchar(10), @itemCodefrom varchar(20), @itemCodeTo varchar(20) ) as begin if @itemCode = '' set @itemCode = '*' if @itemCode = '*' begin if (@fromfatype <> @tofatype) begin delete productprecept where pro00c = @compid and pro02c = @tofatype insert productprecept(pro00c, pro01c, pro02c, pro03i, pro04f, pro05i, pro06f, pro07f, pro08c, pro09c, pro10c, pro11c, pro12c) select @compid, pro01c, @tofatype, pro03i, pro04f, pro05i, pro06f, pro07f, pro08c, pro09c, isnull(pro10c, '*'), pro11c, pro12c from productprecept where pro00c = @compid and pro02c = @fromfatype end end else begin declare @gfa01c varchar(20) if @fromfatype <> @tofatype begin declare cur_gfm01 cursor for select gfa01c from gfm01 with(nolock) where gfa00c = @compid and ((gfa01c >= @itemCodefrom and gfa01c <= @itemCodeTo) or @itemCodefrom = '*') and ((gfa24c >= @itemTypefrom and gfa24c <= @itemTypeTo) or @itemTypefrom = '*') end else begin declare cur_gfm01 cursor for select gfa01c from gfm01 with(nolock) where gfa00c = @compid and ((gfa01c >= @itemCodefrom and gfa01c <= @itemCodeTo) or @itemCodefrom = '*') and ((gfa24c >= @itemTypefrom and gfa24c <= @itemTypeTo) or @itemTypefrom = '*') and gfa01c <> @itemCode end open cur_gfm01 fetch cur_gfm01 into @gfa01c while @@fetch_status = 0 begin if (@fromfatype <> @tofatype or @gfa01c <> @itemCode) begin delete productprecept where pro00c = @compid and pro02c = @tofatype and pro01c = @gfa01c insert productprecept(pro00c, pro01c, pro02c, pro03i, pro04f, pro05i, pro06f, pro07f, pro08c, pro09c, pro10c, pro11c, pro12c) select @compid, @gfa01c, @tofatype, pro03i, pro04f, pro05i, pro06f, pro07f, pro08c, pro09c, isnull(pro10c, '*'), pro11c, pro12c from productprecept where pro00c = @compid and pro02c = @fromfatype and pro01c = @itemCode end fetch cur_gfm01 into @gfa01c end close cur_gfm01 deallocate cur_gfm01 end end GO /****** Object: StoredProcedure [dbo].[upg_insert_rece_message] Script Date: 06/12/2017 06:20:16 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_insert_rece_message] ( @compid varchar(10), @frommobile varchar(20), @content varchar(150), @ddate varchar(10), @ttime varchar(10) ) as begin declare @idd float declare @memid varchar(20) select @idd = max(gxe01f) from gxm05 where gxe00c = @compid select @idd = isnull(@idd, 0) + 1 select @memid = gba01c from gbm01 where gba08c = @frommobile insert gxm05(gxe00c, gxe01f, gxe02d, gxe03t, gxe04c, gxe05c, gxe06c, gxe07c, gxe08d, gxe09t) values(@compid, @idd, @ddate, @ttime, @frommobile, @memid, @content, 1, '', '') end GO /****** Object: StoredProcedure [dbo].[upg_synchronous_reservation_change] Script Date: 06/12/2017 06:20:47 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_synchronous_reservation_change] ( @compid varchar(10) --公司编号 ) as begin --根据会员电话得到会员卡号 update a set a.gwa04c = b.gba01c from gwm01 a, gbm01 b where b.gba00c = @compid and b.gba08c = a.gwa07c and isnull(a.gwa07c, '') <> '' and isnull(a.gwa04c, '') = '' end GO /****** Object: StoredProcedure [dbo].[upg_check_cashcustid] Script Date: 06/12/2017 06:19:40 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_check_cashcustid] ( @compid varchar(10), @cashcustid varchar(10) ) as begin if not exists(select 1 from gcm01 with (nolock) where gca00c = @compid and gca01c = @cashcustid ) begin insert gcm01(gca13d, gca00c, gca01c, gca02c, gca03i, gca04c, gca07d, gca08i, gca09c, gca10f, gca12c, gca18c) values(@compid, @compid, @cashcustid, 'Z', 9, '散客', '29990101', 4, '1', 20, '', '') end if not exists(select 1 from gbm01 with (nolock) where gba00c = @compid and gba01c = '散客') begin insert gbm01(gba00c, gba01c, gba03c) values(@compid, '散客', '散客') end end GO /****** Object: StoredProcedure [dbo].[upg_insert_gim23] Script Date: 06/12/2017 06:20:15 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_insert_gim23] ( @compid varchar(10), --公司别 @billid varchar(20), --消费单号 @savedbillid varchar(20), --寄存单号 @cardid varchar (20), --会员卡号 @userid varchar(20), --用户编号 @currdate varchar(8), --当前日期 @currtime varchar(6), --当前时间 @custname varchar(20), --名称 @personcard varchar(20), --身份证号码 @tel varchar(20) --电话 ) as begin declare @trncnt int select @trncnt = @@trancount if (@trncnt = 0) begin transaction ts declare @flag int declare @telno varchar(20) declare @idd varchar(40) --身份证号码 declare @memname varchar(40) -- 会员名称 --判断是否有寄存的产品 select @flag = count(*) from ggm03 with (nolock) where ggc01c = @billid and ggc00c = @compid and isnull(ggc36i, 9) = 1 select @telno = case when isnull(gba07c, '') = '' then isnull(gba08c, '') else gba07c end, @memname = gba03c, @idd = gba16c from gbm01 with (nolock) where gba00c = @compid and gba23c = @cardid if @tel<>'' set @telno = @tel if isnull(@personcard, '') = '' select @personcard = @idd if isnull(@custname, '') = '' select @custname = @memname if (@flag<>0) begin insert gim23(gim00c, gim010c, gim01c, gim02c, gim03c, gim04c, gim06c, gim05c, gim07c, gim07d, gim08t, gim09c, gim10i, gim11i, gim12c ) values(@compid, @savedbillid, @cardid, @custname, @personcard, @telno, @userid, @userid, @userid, @currdate, @currtime, @compid, 0, 0, @billid) if @@error <> 0 goto error_label create table #tbl_ggm03( ggc00c varchar(10) not null, --公司编号 ggc01c varchar(20) not null, --消费单号 ggc03c varchar(20) null, --产品代码 ggc05f float null, --数量 ggc12c varchar(20) null, --产品销售人员 ggc15c varchar(20) null, --第二人 ggc17c varchar(20) null, --第三人 ggc36i int null --方式 0带走 1寄存 2欠货 ) create clustered index idx_tbl_ggm03 on #tbl_ggm03(ggc00c, ggc01c) insert #tbl_ggm03(ggc00c, ggc01c, ggc03c, ggc05f, ggc12c, ggc15c, ggc17c, ggc36i) select ggc00c, ggc01c, ggc03c, sum(ggc05f), ggc12c, ggc15c, ggc17c, ggc36i from ggm03 a with (nolock) where ggc00c = @compid and ggc01c = @billid group by ggc00c, ggc01c, ggc03c, ggc12c, ggc15c, ggc17c, ggc36i insert gim24(gin00c, gin010c, gin01c, gin02c, gin03i, gin04i, gin05c, gin06c, gin07d, gin08t, gin09i, gin10c, gin11c, gin12c, gin13i, gin15c, gin16c, gin17c, gin18c ) select @compid, @savedbillid, ggc03c, gfa03c, isnull(ggc05f, 0)as count, 0, null, null, null, null, null, null, null, null, isnull(ggc05f, 0), isnull(ggc12c, ''), isnull(ggc15c, ''), isnull(ggc17c, ''), '' from ggm01 a with (nolock), #tbl_ggm03 b, gfm01 c where a.gga00c = b.ggc00c and a.gga01c = b.ggc01c and b.ggc00c = c.gfa00c and b.ggc03c = c.gfa01c and a.gga01c = @billid and gfa00c = @compid and isnull(ggc36i, 9) = 1 drop table #tbl_ggm03 --modify by lyj for 伊美娜添加了过滤条件ggc36i if @@error <> 0 goto error_label end if @trncnt = 0 commit transaction ts set nocount off return 1 error_label: begin rollback transaction ts set nocount off return 0 end end GO /****** Object: StoredProcedure [dbo].[upg_get_card_snop_single] Script Date: 06/12/2017 06:20:06 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_get_card_snop_single] ( @compid varchar(10), -- 门店编号 @input varchar(100) -- 输入 ) as set nocount on set rowcount 0 set transaction isolation level read uncommitted begin if isnull(@input, '') = '' return if isnull(@compid, '') = '' return --旧的 declare @localcompid varchar(10) declare @menberid varchar(20) declare @cardid varchar(20) declare @gca08i int declare @gca07d varchar(8) if exists (select 1 from gcm01 with (nolock) where gca00c = gca13d and gca01c = @input and gca00c = @compid) begin select @localcompid = gca00c, @cardid = gca01c, @menberid = gca04c, @gca08i = gca08i, @gca07d = gca07d from gcm01 with (nolock) where gca00c = gca13d and gca01c = @input and gca00c = @compid if exists(select 1 from gbm01 with (nolock) where gba00c = @localcompid and gba23c = @menberid) begin select gba00c, gba01c, gba03c, gba08c, gba17d, gba21c, (case when isnull(gba23c, '') = '' then @cardid else gba23c end) gba23c, gba98c, @gca08i as gca08i, @gca07d as gca07d from gbm01 with (nolock) where gba00c = @localcompid and gba23c = @menberid end else begin select @localcompid gba00c, @menberid gba01c, '' gba03c, '' gba08c, '' gba17d, '' gba21c, @input gba23c, '' gba98c, isnull (@gca08i, 0) gca08i, '' as gca07d end return end select gba00c, gba01c, gba03c, gba08c, gba17d, gba21c, (case when isnull(gba23c, '') = '' then gca01c else gba23c end) gba23c, gba98c, gca08i, gca07d from gbm01 a with (nolock) , gcm01 b with (nolock) where a.gba00c = b.gca00c and gca00c = gca13d and a.gba23c = b.gca01c and gca00c = @compid and a.gba01c not like '散客%' and gba98c like '%' + @input + '%' order by gba03c, gba08c, gba00c end GO /****** Object: StoredProcedure [dbo].[upg_query_add_lc] Script Date: 06/12/2017 06:20:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_query_add_lc] ( @compid varchar(10), @fromdate varchar(10), @todate varchar(10) ) as begin --没有疗程的 select gba00c, --门店 gba03c, --姓名 gba23c, --卡号 case gba14i when 0 then '女' else '男' end sex, gba17d, --生日 gba08c, --手机 gba18c, --职业 gba26c, --工作单位 gba05c --地址 from gbm01 with(nolock) where gba23c not in (select gcf01c from gcm06 where gcf11d < @fromdate) -- 之前不存在的 and gba23c in (select gcf01c from gcm06 where gcf11d between @fromdate and @todate) union all --本月买疗程但三个月没来的 select gba00c, --门店 gba03c, --姓名 gba23c, --卡号 case gba14i when 0 then '女' else '男' end sex, gba17d, --生日 gba08c, --手机 gba18c, --职业 gba26c, --工作单位 gba05c --地址 from gbm01 with(nolock) where gba23c not in ( select gga09c from ggm02 a with(nolock) inner join ggm01 b with(nolock) on b.gga00c = a.ggb00c and b.gga01c = a.ggb01c where ggb47i <> 1 and gga94d between dateadd(month,-3,@fromdate) and @fromdate --三个月 -- and gga09c in (select gcf01c from gcm06 where gcf11d between @fromdate and @todate) ) and gba23c in (select gcf01c from gcm06 where gcf11d between @fromdate and @todate) end GO /****** Object: StoredProcedure [dbo].[upg_change_belongcomp] Script Date: 06/12/2017 06:19:38 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_change_belongcomp] ( @fromcard varchar(20), @tocard varchar(20), @compid varchar(10) ) as begin -- 多余的公司的会员卡删除 delete gcm01 where gca00c <> gca13d and gca01c >= @fromcard and gca01c <= @tocard -- trigger也同时删除gcm03 -- 更改gcm03的卡公司 update gcm03 set gcc00c = @compid where gcc01c >= @fromcard and gcc01c <= @tocard and gcc00c <> @compid -- 更改gcm01 update gcm01 set gca00c = @compid, gca13d = @compid where gca01c >= @fromcard and gca01c <= @tocard and gca00c <> @compid -- 会员资料的处理 -- 可能导致重复 update gbm01 set gba00c = @compid where gba23c >= @fromcard and gba23c <= @tocard and gba01c not in (select gba01c where gba00c = @compid) end GO /****** Object: StoredProcedure [dbo].[upg_get_card_facedistinguish] Script Date: 06/12/2017 06:20:05 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_get_card_facedistinguish] ( @cardid varchar(30) --卡号 ) as begin if(@cardid = '') return declare @localcompid varchar(10) declare @menberid varchar(20) declare @gca08i int declare @gca07d varchar(8) if exists(select 1 from gcm01 a with(nolock) where gca01c = @cardid) begin select @localcompid = gca00c, @menberid = gca04c, @gca08i = gca08i, @gca07d = gca07d from gcm01 with (nolock) where gca01c = @cardid if exists(select 1 from gbm01 where gba00c = @localcompid and gba23c = @cardid) begin select gba00c, gba01c, gba03c, gba08c, gba17d, gba21c, (case when isnull(gba23c, '') = '' then @cardid else gba23c end) gba23c, gba98c, @gca08i as gca08i, @gca07d as gca07d from gbm01 with (nolock) where gba00c = @localcompid and gba23c = @cardid end else begin select @localcompid gba00c, @menberid gba01c, '' gba03c, '' gba08c, '' gba17d, '' gba21c, @cardid gba23c, '' gba98c, isnull(@gca08i, 0) gca08i, '' as gca07d end return end select gba00c, gba01c, gba03c, gba08c, gba17d, gba21c, (case when isnull(gba23c, '') = '' then gca01c else gba23c end) gba23c, gba98c, gca08i, gca07d from gbm01 a with (nolock) , gcm01 b with (nolock) where a.gba00c = b.gca00c and a.gba23c = b.gca01c and b.gca01c like '%'+ @cardid order by gba03c, gba08c, gba00c end GO /****** Object: StoredProcedure [dbo].[upg_copy_customer] Script Date: 06/12/2017 06:19:46 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_copy_customer]( @fromcompid varchar(10), --公司别 @memid varchar(20), @tocompid varchar(10)) as begin if not exists(select 1 from gbm01 with (nolock) where gba00c = @tocompid and gba01c = @memid) begin insert gbm01(gba00c, gba01c, gba02c, gba03c, gba04c, gba05c, gba06c, gba07c, gba08c, gba09c, gba10c, gba11c, gba12c, gba13c, gba14i, gba15c, gba16c, gba17d, gba18c, gba19c, gba20c, gba21c, gba22c, gba23c, gba24c, gba25c, gba26c, gba27c, gba28i, gba29c, gba30c, gba31c, gba32c, gba33c, gba34c, gba35c, gba36c, gba37c, gba38c, gba39i, gba40c) select @tocompid, gba01c, gba02c, gba03c, gba04c, gba05c, gba06c, gba07c, gba08c, gba09c, gba10c, gba11c, gba12c, gba13c, gba14i, gba15c, gba16c, gba17d, gba18c, gba19c, gba20c, gba21c, gba22c, gba23c, gba24c, gba25c, gba26c, gba27c, gba28i, gba29c, gba30c, gba31c, gba32c, gba33c, gba34c, gba35c, gba36c, gba37c, gba38c, gba39i, gba40c from gbm01 with (nolock) where gba00c = @fromcompid and gba01c = @memid delete gbm04 where gbd00c = @tocompid and gbd01c = @memid insert gbm04(gbd00c, gbd01c, gbd02p) select @tocompid, gbd01c, gbd02p from gbm04 with (nolock) where gbd00c = @fromcompid and gbd01c = @memid end end GO /****** Object: StoredProcedure [dbo].[upg_get_card_snop_global] Script Date: 06/12/2017 06:20:05 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_get_card_snop_global] ( --@compid varchar(30), @input varchar(100) -- ) as set nocount on set rowcount 0 set transaction isolation level read uncommitted begin if isnull(@input, '') = '' return declare @compid varchar(10) declare @menberid varchar(20) declare @cardid varchar(20) declare @gca08i int declare @gca07d varchar(8) if exists (select 1 from gcm01 with (nolock) where gca00c = gca13d and gca01c = @input) begin select @compid = gca00c, @cardid = gca01c, @menberid = gca04c, @gca08i = gca08i, @gca07d = gca07d from gcm01 with (nolock) where gca00c = gca13d and gca01c = @input if exists(select 1 from gbm01 with (nolock) where gba00c = @compid and gba23c = @menberid) begin select gba00c, gba01c, gba03c, gba08c, gba17d, gba21c, (case when isnull(gba23c, '') = '' then @cardid else gba23c end) gba23c, gba98c, @gca08i as gca08i, @gca07d as gca07d from gbm01 with (nolock) where gba00c = @compid and gba23c = @menberid end else begin select @compid gba00c, @menberid gba01c, '' gba03c, '' gba08c, '' gba17d, '' gba21c, @input gba23c, '' gba98c, isnull(@gca08i,0) gca08i, '' as gca07d end return end select gba00c, gba01c, gba03c, gba08c, gba17d, gba21c, (case when isnull(gba23c, '') = '' then gca01c else gba23c end) gba23c, gba98c, gca08i, gca07d from gbm01 a with (nolock) , gcm01 b with (nolock) where a.gba23c = b.gca01c and a.gba00c = b.gca00c and gca00c = gca13d and a.gba01c not like '散客%' --and substring(a.gba01c,1,2) <> '散客' and gba98c like '%' + @input + '%' order by gba03c, gba08c, gba00c end GO /****** Object: StoredProcedure [dbo].[reminder_card_deadline] Script Date: 06/12/2017 06:19:20 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[reminder_card_deadline] @compid varchar(10), @cardtype varchar(5), @startdate varchar(8), @enddate varchar(8) as begin select distinct gca13d, gba01c, gca01c, cast(gba03c as varchar(20)) as gba03c, gca07d, gba07c, gba08c, gba05c from gcm01 a with (nolock), gbm01 b with (nolock) where a.gca00c = b.gba00c and a.gca01c = b.gba23c and a.gca13d = @compid and (isnull(@cardtype, '') = '' or a.gca02c = @cardtype) and (isnull(@startdate, '') = '' or a.gca07d >= @startdate) and (isnull(@enddate, '') = '' or a.gca07d <= @enddate) end GO /****** Object: StoredProcedure [dbo].[upg_sms_evalute] Script Date: 06/12/2017 06:20:41 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_sms_evalute]( @mobile varchar(20), @curdate varchar(10), @curtime varchar(10), @lvl int, @desc varchar(80)) as begin declare @compid varchar(10) declare @billid varchar(20) declare @seq float declare @memid varchar(20), @memname varchar(40) declare @cardid varchar(20), @srvdate varchar(20) declare @lvl_dsec varchar(20) select @memid = gba01c, @memname = gba03c, @cardid = gba23c from gbm01 with (nolock) where gba08c = @mobile --寻找最后的单据 select @billid = gga01c, @compid = gga00c, @srvdate = gga02d from ggm01 with (nolock) where gga05c = @memid and gga02d + gga03t < @curdate + @curtime order by gga02d, gga03t select @compid = isnull(@compid, '001'), @billid = isnull(@billid, '') select @seq = max(gbj03f) from gbm10 with (nolock) where gbj00c = @compid select @seq = isnull(@seq, 0) + 1 insert gbm10(gbj00c, gbj02c, gbj03f, gbj04c, gbj07d, gbj08d, gbj10i, gbj11c, gbj12c, gbj14c, gbj15c) values(@compid, @billid, @seq, @memid, @srvdate, @curdate, 1, convert(varchar(10), @lvl), @desc, @memname, @mobile) end GO /****** Object: StoredProcedure [dbo].[upg_clear_redundant_member] Script Date: 06/12/2017 06:19:41 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_clear_redundant_member] ( @must_have_card int ) as begin if @must_have_card = 1 begin delete gbm01 where isnull(gba23c, '') = '' end create table #member_key ( gba01c varchar(20), gba23c varchar(20) ) create table #unmember_key ( gba01c varchar(20), gba23c varchar(20) ) insert into #member_key(gba01c, gba23c) select gba01c, gba23c from (select gba01c, gba23c, count(1) as CCount from gbm01 group by gba01c, gba23c) T where T.CCount > 1 declare @gba01c varchar(20) declare @gba23c varchar(20) declare @own varchar(20) declare member_clear cursor for select gba01c, gba23c from #member_key open member_clear fetch member_clear into @gba01c, @gba23c while @@fetch_status = 0 begin if not exists(select 1 from gcm01 where gca01c = @gba23c and gca04c = @gba01c and gca00c = gca13d) begin insert into #unmember_key(gba01c, gba23c) select @gba01c, @gba23c end else begin select @own = gca00c from gcm01 where gca01c = @gba23c and gca04c = @gba01c and gca00c = gca13d delete from gbm01 where gba01c = @gba01c and gba00c <> @own end fetch member_clear into @gba01c, @gba23c end close member_clear deallocate member_clear select * from #unmember_key drop table #member_key drop table #unmember_key end GO /****** Object: StoredProcedure [dbo].[upg_member_operation_details_group_ceshi] Script Date: 06/12/2017 06:20:24 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_member_operation_details_group_ceshi] ( @compid varchar(10), @fromdate varchar(20), @todate varchar(20), @spid int ) as begin declare @incompid varchar(10) select @incompid = @compid if @fromdate = '' or @fromdate = '*' begin select @fromdate = '19900101' select @todate = '20901231' end create table #details ( memid varchar(20) not null, seq int identity not null, action varchar(4) null, action_name varchar(20) null, item varchar(20) not null, name varchar(100) null, quan float null, amt float null primary key(seq) ) create table #details_sum ( memid varchar(20) not null, quan_sum float null, amt_sum float null, primary key(memid) ) insert #details(action, item, name, quan, amt,memid) select gct06c, gct07c, gct08c, sum(gct11f), sum(gct12f),memid from gcm20 with (nolock),gma06r_cursor b with(nolock), gam26 z where gct03c = b.memid and gct00c = z.gaz02c and z.gaz01c = @incompid and gct04d >= @fromdate and gct04d <= @todate and b.spid = @spid group by gct06c, gct07c, gct08c,memid insert #details_sum(quan_sum, amt_sum,memid) select sum(gct11f), sum(gct12f),memid from gcm20 with (nolock),gma06r_cursor b with(nolock), gam26 z where gct03c = b.memid and gct00c = z.gaz02c and z.gaz01c = @incompid and gct04d >= @fromdate and gct04d <= @todate and b.spid = @spid group by memid update #details set action_name = '产品' where action = '4' update #details set action_name = '消费' where action = '3' select a.*,b.quan_sum,b.amt_sum from #details a,#details_sum b where a.memid = b.memid order by seq delete from gma06r_cursor where spid = @spid drop table #details drop table #details_sum end GO /****** Object: StoredProcedure [dbo].[upg_analyzer_customer_order_regdate_lm2_ceshi] Script Date: 06/12/2017 06:19:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_analyzer_customer_order_regdate_lm2_ceshi] ( @compid varchar(10), @class varchar(10), @memfromdate varchar(10), @memtodate varchar(10), @frommemid varchar(20), @tomemid varchar(20), @fromdate varchar(20), @todate varchar(20), @state varchar(200), -- 会员卡状态 @acon_amt float, -- 大于消费金额 @alastdate varchar(8), -- 某个日期之后 @at_times float, -- 大于消费次数 @aav_amt float, -- 大于平均消费金额 @aless_remain float, -- 小于某个储值余额 @abig_remain float, -- 大于某个储值余额 @addtoSMS int, @ordertype int, @only_card_pay int, @iscursor int,--记录会员信息 @acctsummary int,--打印所有账户余额 @spid int, @currentpageindex int, @page int, @pageall int ) as begin create table #mem_result_2 ( id int identity not null, compid varchar(10), memid varchar(20) not null, -- Member编号 m_name varchar(40) null, -- 姓名 cardid varchar(20) null, -- 卡号 gender int null, -- 性别 0:女 mobile varchar(20) null, -- 手机 tel varchar(20) null, -- 电话 birthday varchar(8) null, -- 会员生日 state varchar(10) null, -- Member卡状态 con_amt float null, -- Consumption金额 lastdate varchar(8) null, -- 最后Consumption日期 lastcall varchar(8) null, -- 最后回访日期 t_times float null, -- 用于比较的Consumption次数 t_compare float null, -- 总共来的次数 av_ytimes float null, -- 每年来的次数 av_times float null, -- 每月来的次数 av_wtimes float null, -- 每周来的次数 months float null, -- 客人来了多少个月了,客龄 av_amt float null, -- 每次Consumption的平均金额 start_date varchar(20) null, -- 开始日期 first_date varchar(20) null, -- 第一次来店日期 comp_date varchar(20) null, -- 开始比较的日期 dur_years float null, -- 统计期间来了多少个年了 dur_months float null, -- 统计期间来了多少个月了 dur_weeks float null, -- 统计期间来了多少个周了 remain01 float null, -- 余额 remain02 float null, -- 余额 remain03 float null, -- 余额 remain04 float null, -- 余额 remain05 float null, -- 余额 total_fill_amt float null, -- 累计Charge金额 memaddr varchar(160) null, -- 会员地址 postcode varchar(20) null, -- 邮编 principle varchar(20) null, -- 负责人 total_salecard_amt float null, -- 累计卡销售金额 regdate varchar(20) null, -- 登记日期 pagecount int null primary key(id) ) insert #mem_result_2(compid, memid, m_name, cardid, gender, mobile, tel, birthday, state, con_amt, lastdate, lastcall, t_times, t_compare, av_ytimes, av_times, av_wtimes, months, av_amt, start_date, first_date, comp_date, dur_years, dur_months, dur_weeks, remain01, remain02, remain03, remain04, remain05, total_fill_amt, memaddr, postcode, principle, total_salecard_amt,pagecount) exec upg_analyzer_customer_ex_ceshi @compid, @class, @frommemid, @tomemid, @memfromdate, @memtodate, @fromdate, @todate, @state, @acon_amt, @alastdate, @at_times, 0, @aav_amt,@aless_remain, @abig_remain, @addtoSMS, 3, @only_card_pay,@acctsummary,@currentpageindex,@page,@pageall --会员资料统计表,需要 if(@iscursor = 1) begin delete from gma06r_cursor where spid = @spid insert gma06r_cursor (spid, memid, cardid) select @spid,memid, cardid from #mem_result_2 where isnull(compid,'')<>'' and isnull(compid,'')<>'lm' end select isnull(id,'') id, isnull(compid,'') compid, isnull(memid,'') memid, isnull(m_name,'') m_name, isnull(cardid,'') cardid, isnull(gender,0) gender, isnull(mobile,'') mobile, isnull(tel,'') tel, isnull(birthday,'') birthday, isnull(state,'') state, isnull(con_amt,0) con_amt, isnull(lastdate,'') lastdate, isnull(lastcall,'') lastcall, isnull(t_times,0) t_times, isnull(t_compare,0) t_compare, isnull(av_ytimes,0) av_ytimes, isnull(av_times,0) av_times, isnull(av_wtimes,0) av_wtimes, isnull(months,0) months, isnull(av_amt,0) av_amt, isnull(start_date,'') start_date, isnull(first_date,'') first_date, isnull(comp_date,'') comp_date, isnull(dur_years,0) dur_years, isnull(dur_months,0) dur_months, isnull(dur_weeks,0) dur_weeks, isnull(remain01,0) remain01, isnull(remain02,0) remain02, isnull(remain03,0) remain03, isnull(remain04,0) remain04, isnull(remain05,0) remain05, isnull(total_fill_amt,0) total_fill_amt, isnull(memaddr,'') memaddr, isnull(postcode,'') postcode, isnull(principle,'') principle, isnull(total_salecard_amt,0) total_salecard_amt, isnull(regdate,'') regdate, isnull(pagecount,0) pagecount from #mem_result_2 drop table #mem_result_2 end GO /****** Object: StoredProcedure [dbo].[upg_stat_acct_history_ceshi] Script Date: 06/12/2017 06:20:42 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_stat_acct_history_ceshi] ( @compid varchar(10), @fromdate varchar(20), @todate varchar(20), @flag int, -- 1-一个账户一个余额 2-详细的每条纪录 @spid int ) as begin declare @incompid varchar(10) select @incompid = @compid declare @compcode varchar(20) if @compid = '001' or @compid = '*' or @compid = '%' begin select @compcode = '%' select @incompid = '001' end else begin select @compcode = @compid end if @fromdate = '*' or @fromdate = '' begin select @fromdate = '19900101' select @todate = '20901231' end create table #tbl_history ( seq int identity not null, cardid varchar(20) not null, nname varchar(40) null, acctno varchar(20) null, acctno_name varchar(20) null, ddate varchar(10) null, mode int null, lastamt float null, amt float null, billtype varchar(20) null, billid varchar(20) null, remain float null, total_fill float null, total_consume float null, total_fillname float null, total_consumename float null, modename varchar(40) null, acctnoAmt float null, primary key(seq) ) create table #tbl_history_sum ( seq int identity not null, cardid varchar(20) not null, lastamt_sum float null, amt_sum float null, remain_sum float null, total_fill_sum float null, total_consume_sum float null, total_fillname_sum float null, total_consumename_sum float null, primary key(seq) ) create table #tbl_stat ( cardid varchar(20) not null, acctno varchar(20) not null, amt float null ) insert #tbl_history(cardid, nname, acctno, ddate, mode, lastamt, amt, billtype, billid) select gcd01c, '', gcd02i, gcd09d, gcd05i, gcd10f, gcd06f, gcd07c, gcd08c from gcm04 a with (nolock), gma06r_cursor b , gam26 z with(nolock) where a.gcd00c = z.gaz02c and z.gaz01c = @incompid -- and a.gcd00c = b.gca00c and a.gcd01c = b.cardid and a.gcd09d >= @fromdate and a.gcd09d <= @todate and b.spid = @spid order by gcd01c, gcd02i, gcd04f delete #tbl_history where isnull(amt, 0) = 0 insert #tbl_history(cardid, nname, acctno, ddate, mode, lastamt, remain, billtype, billid) select gcc01c, '', gcc03i, '', 0, 0, gcc06f, '', '' from gcm03 a with (nolock), gma06r_cursor b where gcc01c = cardid and gcc01c <> '' and b.spid = @spid -- 统计累计充值以及累计消费 insert #tbl_stat(cardid, acctno, amt) select cardid, acctno, sum(isnull(amt, 0)) from #tbl_history where (mode = 0 or mode = 6) and isnull(ddate, '') <> '' group by cardid, acctno update a set a.total_fill = isnull(b.amt, 0) from #tbl_history a, #tbl_stat b where a.cardid = b.cardid and a.acctno = b.acctno and isnull(ddate, '') = '' delete #tbl_stat insert #tbl_stat(cardid, acctno, amt) select cardid, acctno, sum(isnull(amt, 0)) from #tbl_history where (mode = 5 or mode = 1) and isnull(ddate, '') <> '' group by cardid, acctno update a set a.total_fill = isnull(a.total_fill, 0) - isnull(b.amt, 0) from #tbl_history a, #tbl_stat b where a.cardid = b.cardid and a.acctno = b.acctno and isnull(ddate, '') = '' delete #tbl_stat insert #tbl_stat(cardid, acctno, amt) select cardid, acctno, sum(isnull(amt, 0)) from #tbl_history where mode = 2 and isnull(ddate, '') <> '' group by cardid, acctno update a set a.total_consume = isnull(b.amt, 0) from #tbl_history a, #tbl_stat b where a.cardid = b.cardid and a.acctno = b.acctno and isnull(ddate, '') = '' drop table #tbl_stat update a set a.nname = b.gba03c from #tbl_history a, gbm01 b with(nolock) where a.cardid = b.gba23c and a.ddate = '' update #tbl_history set billtype = '收银单' where billtype = 'gx' update #tbl_history set billtype = '账户异动' where billtype = 'gz' update #tbl_history set billtype = '卡销售' where billtype = 'gn' update #tbl_history set billtype = '卡异动' where billtype = 'ge' update a set a.acctno_name = b.gsb03c from #tbl_history a, gsm02 b with(nolock) where a.acctno = b.gsb02c and b.gsb01c = 'O' and b.gsb00c = @incompid if @flag = 2 -- 每个账户的详细变化历史,也包括一条汇总的纪录 begin update a set a.total_consumename = a.amt from #tbl_history a where isnull(ddate, '') <> '' and a.mode = 2 update a set a.total_fillname = a.total_fill, a.total_consumename = a.total_consume from #tbl_history a where isnull(ddate, '') ='' update a set a.total_fillname = a.amt from #tbl_history a where isnull(ddate, '') <> '' and a.mode <> 2 update a set a.modename = b.gsb03c from #tbl_history a, gsm02 b with(nolock) where isnull(ddate, '') <> '' and a.mode = b.gsb02c and b.gsb01c = 'A8' and b.gsb00c = @incompid update a set a.modename ='赠送积分' from #tbl_history a where isnull(ddate, '') <> '' and mode = 0 and acctno = 3 insert #tbl_history_sum(cardid,lastamt_sum,amt_sum,remain_sum,total_fill_sum,total_consume_sum,total_fillname_sum,total_consumename_sum) select cardid,sum(lastamt),sum(amt),sum(remain),sum(total_fill),sum(total_consume),sum(total_fillname),sum(total_consumename) from #tbl_history group by cardid select a.seq,a.cardid, isnull(nname,'') nname, isnull(acctno,'') acctno, isnull(acctno_name,'') acctno_name, isnull(ddate,'') ddate, isnull(mode,'') mode, isnull(lastamt,0) lastamt, isnull(amt,0) amt, isnull(billtype,'') billtype, isnull(billid,'') billid, isnull(remain,0) remain, isnull(total_fill,0) total_fill, isnull(total_consume,0) total_consume, isnull(modename,'') modename , isnull(total_fillname,0) total_fillname , isnull(total_consumename,0) total_consumename, isnull (lastamt_sum,0) lastamt_sum, isnull(amt_sum,0) amt_sum, isnull(remain_sum,0) remain_sum, isnull(total_fill_sum,0) total_fill_sum, isnull(total_consume_sum,0) total_consume_sum, isnull(total_fillname_sum,0) total_fillname_sum, isnull(total_consumename_sum,0) total_consumename_sum, isnull(acctnoAmt,0) acctnoAmt from #tbl_history a, #tbl_history_sum b where a.cardid = b.cardid order by a.cardid, acctno, ddate end else -- 每个账户一个余额 begin insert #tbl_history_sum(cardid,remain_sum,total_fill_sum) select cardid,sum(remain),sum(total_fill) from #tbl_history where isnull(ddate, '') = '' group by cardid select a.seq,a.cardid, isnull(nname,'') nname, isnull(acctno,'') acctno, isnull(ddate,'') ddate, isnull(mode,'') mode, isnull(lastamt,0) lastamt, isnull(amt,0) amt, isnull(billtype,'') billtype, isnull(billid,'') billid, isnull(remain,0) remain, isnull(total_fill,0) total_fill, isnull(total_consume,0) total_consume, isnull(modename,'') modename , isnull(total_fillname,0) total_fillname , isnull(total_consumename,0) total_consumename, isnull(acctnoAmt,0) acctnoAmt, isnull(remain_sum,0) remain_sum, isnull(total_fill_sum,0) total_fill_sum, isnull(acctno_name,'') acctno_name from #tbl_history a, #tbl_history_sum b where a.cardid = b.cardid and isnull(ddate, '') = '' order by a.cardid, acctno, ddate end delete from gma06r_cursor where spid = @spid drop table #tbl_history end GO /****** Object: StoredProcedure [dbo].[upg_member_operation_actdetail] Script Date: 06/12/2017 06:20:23 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_member_operation_actdetail] ( @compid varchar(10), @fromdate varchar(20), @todate varchar(20), @spid int ) as begin declare @incompid varchar(10) select @incompid = @compid if @fromdate = '' or @fromdate = '*' begin select @fromdate = '19900101' select @todate = '20901231' end create table #details ( seq int identity not null, cardid varchar(20) not null, date varchar(8) null, --异动日期 billtype varchar(40) null, -- 资料Text描述 bill varchar(20) null, --异动单号 mode varchar(40) null, lastremain float null, --前次余额 amt float null, --异动金 acct int null, --帐号类别(1-电子钱包 , 2-储值帐号) acctname varchar(40) null, --帐号类别(1-电子钱包 , 2-储值帐号) primary key(seq) ) create table #details_sum ( cardid varchar(20) not null, lastremain_sum float null, amt_sum float null, primary key(cardid) ) insert #details(date, billtype, bill, mode, lastremain, amt, acct, cardid) SELECT isnull(gcd09d,''), isnull(gsf03c,''), isnull(gcd08c,''), isnull(gsb03c,''), isnull(gcd10f,''), isnull(gcd06f,''), isnull(gcd02i,''), cardid FROM gcm04 a with(nolock), gsm06 b with(nolock), gsm02 c with(nolock), gma06r_cursor d with(nolock), gam26 z with(nolock) where a.gcd07c *= b.gsf02c and b.gsf01c = '1' and a.gcd00c *= c.gsb00c and a.gcd05i *= c.gsb02c and a.gcd01c = d.cardid and c.gsb01c = 'A8' and a.gcd00c = z.gaz02c and z.gaz01c = @incompid and a.gcd09d >= @fromdate and a.gcd09d <= @todate and d.spid = @spid order by gcd09d,gcd04f insert #details_sum(lastremain_sum, amt_sum, cardid) SELECT sum(isnull(gcd10f,0)), sum(isnull(gcd06f,0)),cardid FROM gcm04 a with(nolock), gsm06 b with(nolock), gsm02 c with(nolock), gma06r_cursor d with(nolock), gam26 z with(nolock) where a.gcd07c *= b.gsf02c and b.gsf01c = '1' and a.gcd00c *= c.gsb00c and a.gcd05i *= c.gsb02c and a.gcd01c = d.cardid and c.gsb01c = 'A8' and a.gcd00c = z.gaz02c and z.gaz01c = @incompid and d.spid = @spid group by cardid update a set a.acctname = b.gsb03c from #details a, gsm02 b with(nolock) where a.acct = b.gsb02c and b.gsb01c = 'O' and b.gsb00c = @incompid delete from gma06r_cursor where spid = @spid select a.*,b.lastremain_sum,b.amt_sum from #details a, #details_sum b where a.cardid = b.cardid order by seq drop table #details_sum drop table #details end GO /****** Object: StoredProcedure [dbo].[upg_day_account] Script Date: 06/12/2017 06:19:56 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_day_account] ( @incompid varchar(10), -- 公司编号 @type varchar(10), -- 类别 @from varchar(8), -- date from @to varchar(8), -- date to @inc_expense int, -- 是否包括费用 @staff varchar(20), -- 收银人员 @billtype varchar(2) -- 单据类别 ,add by tanxulin 2010/02/24 ) as--WUXM, 2005/07/30 begin -- 因java请求无法识别WWQ = #,WWR = $, WWE = & exec upg_paymode_to_java @type,@type output declare @compid varchar(10) declare @accttype int -- 支付方式对应的账户 declare @procacct int -- 疗程账户 declare @gsn02c varchar(20) select @gsn02c = gsn02c from gsm14 where gsn01c = @type if (isnull(@gsn02c, '') = '' or @gsn02c = '999') select @accttype = -1 else select @accttype = convert(int, @gsn02c) select @compid = @incompid if @compid = '001' select @compid = '%' if @staff = '*' or @staff = '' begin select @staff = '%' end create table #account ( id int identity not null, note varchar(20) null, -- note id class varchar(5) null, -- note class cname varchar(40) null, -- class name date varchar(8) null, -- 帐务日期 time varchar(6) null, -- 时间 brief varchar(600) null, -- 摘要 get float default 0 null, -- 借方金额 out float default 0 null, -- 贷方金额 balance float default 0 null, -- 余额 amt float default 0 null, -- 金额 --add by lm 2015-09-28 gqa13i float null, -- 支出或收入标志 --add by lm 2015-09-28 staff varchar(20) null, -- 收银人员 billdate varchar(20) null, -- 单据日期 primary key (id), ) -- 处理一般的支付方式,如现金,银行卡,内调,抵用券等 -- 这些日记账可以容易的从gsm03取得 -- 无对应账户的支付 -- 收益 if (isnull(@accttype, -1) = -1) begin -- 无对应账户的支付 insert #account(note, class, get, brief, date) select gsc01c, gsc02c, gsc05f, '支付明细' + gsc06c, '' from gsm03 a, gam26 z where a.gsc00c = z.gaz02c and z.gaz01c = @incompid and isnull(gsc04c, '') = @type and gsc10d >= @from and gsc10d <= @to end else begin -- 有对应账户的支付 -- 会员卡销售的金额都作为充值金额 -- 卡本来的储值金额 select @procacct = '4' insert #account(note, class, out, brief, date, time, billdate) select gnb01c, 'gn', gnb11f, gnb03c + '卡销售', gna80d, gna12t, gna02d from gnm02 b with (nolock), gnm01 a with (nolock), gam26 c where a.gna00c = b.gnb00c and a.gna01c = b.gnb01c and a.gna80d >= @from and a.gna80d <= @to and a.gna00c = c.gaz02c and c.gaz01c = @incompid and a.gna93c like @staff and b.gnb13i = @accttype -- 卡销售的时候可能有疗程的金额 if (@procacct = @accttype) begin insert #account(note, class, out, brief, date, time, billdate) select gna01c, 'gn', gnc05f, gna06c + '购买疗程' + gnc02c, gna80d, gna12t, gna02d from gnm01 a with (nolock), gnm03 c with (nolock), gam26 d where a.gna00c = c.gnc00c and a.gna01c = c.gnc01c and a.gna80d >= @from and a.gna80d <= @to and a.gna00c = d.gaz02c and d.gaz01c = @incompid and a.gna93c like @staff end -- 收银的时候从账户中用掉的 insert #account(note, class, get, brief, date, time) select gsc01c, gsc02c, gsc05f, '支付明细' + gsc06c, '', '' from gsm03 a, gam26 z where a.gsc00c = z.gaz02c and z.gaz01c = @incompid and isnull(gsc04c, '') = @type and gsc10d >= @from and gsc10d <= @to -- 会员账户管理中的账户的增加 insert #account(note, class, out, brief, date, time, billdate) select gcl01c, 'gz', gcl65f, gcl03c + '-' + gcl04c + '充值', gcl80d, gcl56t, gcl02d from gcm10 a with (nolock), gam26 z where gcl00c = z.gaz02c and z.gaz01c = @incompid and gcl93c like @staff and gcl80d >= @from and gcl80d <= @to and convert(int, gcl04c) = @accttype and (gcl06i = 0 or gcl06i = 6) insert #account(note, class, get, brief, date, time, billdate) select gcl01c, 'gz', gcl65f, gcl03c + '-' + gcl04c + '取款或欠款', gcl80d, gcl56t, gcl02d from gcm10 a with (nolock), gam26 z where gcl00c = z.gaz02c and z.gaz01c = @incompid and gcl93c like @staff and gcl80d >= @from and gcl80d <= @to and convert(int, gcl04c) = @accttype and (gcl06i = 1 or gcl06i = 5) if (@procacct = @accttype) begin --疗程的购买 insert #account(note, class, out, brief, date, time, billdate) select gcl01c, 'gz', gcl66f, gcl03c + '-' + gcl04c + '充值', gcl80d, gcl56t, gcl02d from gcm10 a with (nolock), gam26 z where a.gcl00c = z.gaz02c and z.gaz01c = @incompid and gcl93c like @staff and gcl80d >= @from and gcl80d <= @to and isnull(gcl66f, 0) > 0 and (gcl06i = 0 or gcl06i = 6) insert #account(note, class, get, brief, date, time, billdate) select gcl01c, 'gz', gcl66f, gcl03c + '-' + gcl04c + '取款或欠款', gcl80d, gcl56t, gcl02d from gcm10 a with (nolock), gam26 z where gcl00c = z.gaz02c and z.gaz01c = @incompid and gcl93c like @staff and gcl80d >= @from and gcl80d <= @to and isnull(gcl66f, 0) > 0 and (gcl06i = 1 or gcl06i = 5) end end -- 从费用登记明细中 得到金额 if @type = '1' and @inc_expense = 1 begin insert #account(note, class, amt, brief, date, time, gqa13i) select gqb01c, 'gv', sum(gqb05f), gqb04c, '', '', gqa13i from gqm02, gqm01, gam26 c where gqa00c = gqb00c and gqa01c = gqb01c and gqa00c = c.gaz02c and c.gaz01c = @incompid and gqa02d >= @from and gqa02d <= @to group by gqb00c, gqb01c, gqb04c, gqa13i end --- update the class name update a set cname = gsf03c from gsm06 s, #account a where gsf01c = '1' and gsf02c = class --modify by lm 2015-09-28 区分支出和收入 update a set a.out = a.amt from #account a where a.gqa13i = 0 update a set a.get = a.amt from #account a where a.gqa13i = 1 -- -- update 贷方金额, 余额 -- update #account -- set out = -get -- where get < 0 -- update #account set get = 0 where out > 0 update #account set balance = isnull(get, 0)-isnull(out, 0) -- update date -- 会员卡销售单 update a set date = gna80d, brief = brief, time = gna12t, staff = gna93c, billdate = gna02d from gnm01 g, #account a, gam26 c where gna00c = c.gaz02c and c.gaz01c = @incompid and gna01c = note and class = 'gn' --项目消费(收银)单 update a set date = gga80d, brief = gga09c + '收银', time = gga04t, staff = gga93c, billdate = gga02d from ggm01 g, #account a, gam26 c where gga00c = c.gaz02c and c.gaz01c = @incompid and gga01c = note and class = 'gx' update a set date = gla80d, brief = gla06c + '统一收银', time = gla04t, staff = gla95c, billdate = gla96d from glm01 g, #account a, gam26 c where gla00c = c.gaz02c and c.gaz01c = @incompid and gla01c = note and class = 'gl' update a set date = gcl80d, brief = gcl03c + '-' + gcl04c + '账户异动', time = gcl56t, staff = gcl93c, billdate = gcl02d from gcm10 g, #account a, gam26 c where gcl00c = c.gaz02c and c.gaz01c = @incompid and gcl01c = note and class = 'gz' --会员卡异动单 update a set date = gea80d, brief = gea04c, time = gea22t, staff = gea95c, billdate = gea03d from gem01 g, #account a, gam26 c where gea00c = c.gaz02c and c.gaz01c = @incompid and gea01c = note and class = 'ge' --费用登记 update a set date = gqa80d, time = gqa13t, staff = gqa95c, billdate = gqa02d from gqm01 j, #account a, gam26 c where gqa00c = c.gaz02c and c.gaz01c = @incompid and gqa01c = note and class = 'gv' -- delete the outdated record declare @sql nvarchar(1024) declare @where nvarchar(1024) if @from <> '*' or @to <> '*' begin set @sql = ' delete #account where ' if @from <> '*' set @where = ' date < ' + quotename(@from, '''') if @to <> '*' begin if @from <>'*' set @where = @where + ' or date > ' + quotename(@to, '''') else set @where = ' date > ' + quotename(@to, '''') end set @sql = @sql + @where exec sp_executesql @sql end if @staff<>'%' begin delete #account where staff<>@staff end else begin delete #account where isnull(staff, '') = '' end select isnull(date,'') date, isnull(time,'') time, isnull(cname,'') cname, isnull(note,'') note, isnull(brief,'') brief, isnull(get,'') get, isnull(out,'') out, isnull(balance,'') balance, isnull(billdate,'') billdate from #account where isnull(balance, 0) <>0 and (class = @billtype or isnull(@billtype, '*') = '*') --add by tanxulin 2010/02/24,增加查询条件单据类别,如'gz', 'gx', ... order by date, cname, note drop table #account end GO /****** Object: StoredProcedure [dbo].[upg_stock_initialization] Script Date: 06/12/2017 06:20:45 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_stock_initialization] ( @compid varchar(10) ) as --Mzh 2005/12/05 begin declare @comp varchar(10) declare @billid varchar(20) --单号 declare @flowno float --流水号 declare @goodsno varchar(20) --产品代码 declare @stdunit varchar(5) --标准单位 declare @stdprice float --标准单价 declare @stdquantity float --标准单位数量 declare @unit varchar(5) --单位 declare @stockunit varchar(5) --进出库单位 declare @consumeunit varchar(5) --消耗单位 declare @quantity float --进货数量 declare @price float --进货价格 declare @percent float --消耗/标准单位比例 declare @percent2 float --销售/进货单位比例 declare cur_calculate_std_instock cursor for select ghb00c, ghb01c, ghb02f, ghb03c, ghb06c, ghb07f, ghb10f from ghm02 where ghb00c = @compid and isnull(ghb13f, 0) = 0 for update of ghb04c, ghb13f, ghb14f open cur_calculate_std_instock fetch cur_calculate_std_instock into @comp, @billid, @flowno, @goodsno, @stockunit, @quantity, @price while @@fetch_status = 0 begin --根据产品代码更新产品的标准单位 select @stdunit = gfa07c, @consumeunit = isnull(gfa08c, gfa07c), @unit = isnull(gfa20c, gfa07c), @percent = isnull(gfa09f, 1), @percent2 = isnull(gfa21f, 1) from gfm01 where gfa00c = @compid and gfa01c = @goodsno if @percent = 0 select @percent = 1 if @percent2 = 0 select @percent2 = 1 --转换产品的标准单位数量和标准单位价格 if (@stockunit = @stdunit) -- 入出库单位就是标准单位 begin select @stdprice = @price select @stdquantity = @quantity end else if (@stockunit = @unit) -- 就是进货单位,大单位 begin select @stdprice = @price/@percent2 select @stdquantity = @quantity * @percent2 end else if (@stockunit = @consumeunit) -- 消耗单位 begin select @stdprice = @price*@percent select @stdquantity = @quantity/@percent end else select @stdprice = 1, @stdquantity = 1 update ghm02 set ghb13f = @stdquantity, ghb14f = @stdprice, ghb04c = @stdunit where current of cur_calculate_std_instock fetch cur_calculate_std_instock into @comp, @billid, @flowno, @goodsno, @stockunit, @quantity, @price end close cur_calculate_std_instock deallocate cur_calculate_std_instock declare cur_calculate_std_outstock cursor for select gib00c, gib01c, gib02f, gib03c, gib06c, gib07f from gim02 where gib00c = @compid and isnull(gib08f, 0) = 0 for update of gib04c, gib08f open cur_calculate_std_outstock fetch cur_calculate_std_outstock into @comp, @billid, @flowno, @goodsno, @stockunit, @quantity while @@fetch_status = 0 begin --更新产品的标准单位 select @stdunit = gfa07c, @consumeunit = isnull(gfa08c, gfa07c), @unit = isnull(gfa20c, gfa07c), @percent = isnull(gfa09f, 1), @percent2 = isnull(gfa21f, 1) from gfm01 where gfa00c = @compid and gfa01c = @goodsno if @percent = 0 select @percent = 1 if @percent2 = 0 select @percent2 = 1 --计算出库的标准出库数量 if (@stockunit = @stdunit) select @stdquantity = @quantity else if (@stockunit = @unit) select @stdquantity = @quantity * @percent2 else if (@stockunit = @consumeunit) select @stdquantity = @quantity/@percent update gim02 set gib04c = @stdunit, gib08f = @stdquantity where current of cur_calculate_std_outstock fetch cur_calculate_std_outstock into @comp, @billid, @flowno, @goodsno, @stockunit, @quantity end close cur_calculate_std_outstock deallocate cur_calculate_std_outstock end GO /****** Object: StoredProcedure [dbo].[upg_sum_goods_cost] Script Date: 06/12/2017 06:20:46 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_sum_goods_cost] ( @compid varchar(10), -- 公司别 @billid varchar(20), -- 收银单号 @item varchar(20), -- 项目编号 @ggm02_seq float, -- 序号 @goods_cost float output -- 消耗产品成本,已经考虑了数量 ) as begin select @goods_cost = sum(a.gge05f * b.gfa15f) from ggm05 a with(nolock), gfm01 b with(nolock) where a.gge00c = b.gfa00c and a.gge03c = b.gfa01c and a.gge14c = @item and a.gge00c = @compid and a.gge01c = @billid and a.gge16f = @ggm02_seq select @goods_cost = isnull(@goods_cost, 0) end GO /****** Object: StoredProcedure [dbo].[upg_create_goods] Script Date: 06/12/2017 06:19:53 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_create_goods] ( @compid varchar(10), -- 公司别 @fromgood varchar(20), -- 从哪个产品拷贝 @togood1 varchar(20), -- 从哪个产品拷贝 @togood2 varchar(20) -- 从哪个产品拷贝 ) as begin if isnull(@compid, '') = '' return if @togood1 = @fromgood return delete gfm01 where gfa00c = @compid and gfa01c = @togood1 insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa04d, gfa05c, gfa06c, gfa07c, gfa08c, gfa09f, gfa10c, gfa11f, gfa12c, gfa13i, gfa14f, gfa15f, gfa16i, gfa17f, gfa18f, gfa19f, gfa20c, gfa21f, gfa22f, gfa23c, gfa24c, gfa25i, gfa26f, gfa27i, gfa28i, gfa29f, gfa30f, gfa31f, gfa32f, gfa33c, gfa34f, gfa35f, gfa36c, gfa37i, gfa38i, gfa39f, gfa40f, gfa41c, gfa42f, gfa43f, gfa44c, gfa45i, gfa46c, gfa47c, gfa97c, gfa39i, gfa48f, gfa49f, gfa50i, gfa51c, gfa52c, gfa70i, gfa71f) select gfa00c, @togood1, gfa02c, gfa03c, gfa04d, gfa05c, gfa06c, gfa07c, gfa08c, gfa09f, gfa10c, gfa11f, gfa12c, gfa13i, gfa14f, gfa15f, gfa16i, gfa17f, gfa18f, gfa19f, gfa20c, gfa21f, gfa22f, gfa23c, gfa24c, gfa25i, gfa26f, gfa27i, gfa28i, gfa29f, gfa30f, gfa31f, gfa32f, gfa33c, gfa34f, gfa35f, gfa36c, gfa37i, gfa38i, gfa39f, gfa40f, gfa41c, gfa42f, gfa43f, gfa44c, gfa45i, gfa46c, gfa47c, gfa97c, gfa39i, gfa48f, gfa49f, gfa50i, gfa51c, gfa52c, gfa70i, gfa71f from gfm01 where gfa00c = @compid and gfa01c = @fromgood end GO /****** Object: StoredProcedure [dbo].[upg_insert_brand] Script Date: 06/12/2017 06:20:15 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_insert_brand] ( @compid varchar(10), @prefix varchar(20), --编号前缀 @suffix varchar(20), --编号后缀 @flow int, --开始编号 @brand int, @nlen int ) as --Writed by WUXM 2006/02/10 begin declare @i int declare @goodsno varchar(20) declare @strpre varchar(20) --产品编号长度 select @i = 0 select @strpre = '0' while(@i < @nlen) begin select @strpre = @strpre + '0' select @i = @i + 1 end --欧莱雅 if (@brand = 0) begin select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '欧莱雅染后营养发膜200ml', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 1), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '欧莱雅染后营养发膜500ml ', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 2), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '欧莱雅干性修复发膜500ml ', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 3), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '欧莱雅干性修复发膜200ml', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 4), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '欧莱雅直亮精华液125ml ', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 5), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '欧莱雅盈波亮泽精华液125ml ', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 6), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '欧莱雅染后营养发膜200ml', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 7), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '欧莱雅烫后活力洗发水250ml ', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 8), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '欧莱雅烫后活力发膜200ml ', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 9), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '欧莱雅烫后活力发膜500ml', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 10), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '欧莱雅烫后活力护发素150ml ', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 11), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '欧莱雅烫后活力护发素750ml ', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 12), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '欧莱雅果酸烫发水', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 13), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '欧莱雅专业染膏 ', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 14), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '欧莱雅染后维他命洗发水1500ml ', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 15), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '欧莱雅染后维他命护发素750ml', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 16), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '欧莱雅染后维他命护发素150ml ', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 17), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '欧莱雅染后维他命洗发水250ml ', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 18), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '欧莱雅深度修复洗发水1500ml', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 19), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '欧莱雅酸性颜色护理 ', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 20), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '欧莱雅劲致塑发啫哩200ml ', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 21), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '欧莱雅欧莱雅前卫塑发泥50ml', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 22), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '欧莱雅顺柔润丝发膜200ml ', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 23), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '欧莱雅顺柔润丝发膜500ml ', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 24), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '欧莱雅可丽雅系列顺柔润发乳液', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 25), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '欧莱雅可丽雅倍顺润发凝露50ml ', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 26), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '欧莱雅可丽雅丰泽润发摩丝250ml ', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 27), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '欧莱雅特丽雅晶亮发膏50ml', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 28), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '欧莱雅丝泉滋养精华素 ', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 29), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '欧莱雅强化护理剂10ml ', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 30), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '欧莱雅顺柔润丝护发素专为直发150ml', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 31), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '欧莱雅顺柔润丝洗发水专为直发1500ml ', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 32), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '欧莱雅最新离子烫x-tenso ', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 33), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '欧莱雅DV2滋养烫发水', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 34), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '卡诗绚亮护色发膜500ML ', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 35), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '卡诗寇丽恒卷护发素200ml ', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 36), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '卡诗蛋白护发露500ml', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 37), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '卡诗水溶奥兰精华 ', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 38), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '卡诗活力胶结物精华12ml ', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 39), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '卡诗奥丽顺柔洗发水250ml', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 40), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '卡诗发质强化洗发水250ml ', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 41), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '卡诗韵系列秀发柔亮乳 ', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 42), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '卡诗深度护发发膜150ml', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 43), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '卡诗奥丽顺柔洗发水1000ml ', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 44), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '卡诗纤弱发质护发露 ', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 45), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '卡诗寇丽恒卷洗发水1000ml', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 46), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '卡诗深度护发发膜500ml ', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 47), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '卡诗奥丽顺柔发膜500ml ', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 48), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '卡诗发质强化护发素200ml', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 49), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '卡诗奥丽顺柔液 ', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 50), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '卡诗绚亮护色发乳125ml ', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 51), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '卡诗活力修复发膜500ml', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 52), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '卡诗滋养1/2/3号洗发水 ', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 53), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '卡诗防脱发纤弱发质洗发水250ml ', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 54), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '卡诗寇丽恒卷洗发水250ml', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 55), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '卡诗绚亮护色洗发水250ml ', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 56), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '卡诗寇丽恒卷摩丝150ml ', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 57), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '卡诗绚亮护色护发露125ml', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 58), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '卡诗活力修复发膜150ml ', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 59), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '卡诗发质强化修护凝霜 ', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 60), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '卡诗绚亮护色发膜200ml', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 61), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '卡诗去屑洗发水100ml ', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 62), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '卡诗防脱发纤弱发质洗发水1000ml ', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 63), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '卡诗绚亮护色发膜500ml', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 64), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '卡诗莹亮水 ', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 65), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '卡诗莹亮理丝乳125ml ', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 66), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '卡诗奥丽顺柔发膜150ml', '2', 'BOT', 200, 90, '0') end --威娜 else if (@brand = 1) begin select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '德国威娜蕴露滋润香波250ml ', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 1), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '德国威娜蕴露染后香波1000ml ', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 2), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '德国威娜蕴露焗油膏500g ', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 3), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '德国威娜蕴露烫后护理局油500g', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 4), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '德国威娜蕴露深层滋润护理焗油发膜500ml ', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 5), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '德国威娜蕴露滋润套装 ', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 6), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '德国威娜蕴露敏感头皮香波均衡健康250ml ', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 7), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '德国威娜新蕴露滋润护发素', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 8), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '德国威娜蕴露营养发根防脱套装 ', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 9), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '德国威娜蕴露滋润护理局油500g', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 10), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '德国威娜蕴露染后护理局油500g ', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 11), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '德国威娜蕴露滋润护发素1000ml ', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 12), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '德国威娜蕴露烫后香波250ml', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 13), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '德国威娜仙露滋润发膜500g ', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 14), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '德国威娜精华素珍露强化液 ', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 15), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '德国威娜烫前护理液18ml', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 16), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '德国威娜新宾芳卡妮婷三支装冷烫精 ', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 17), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '德国威娜珍露精华液 ', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 18), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '德国威娜创艺系列亚光发蜡', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 19), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '德国威娜新创艺系列泡沫发蜡 ', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 20), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '德国威娜创艺系列爽发啫喱125ml ', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 21), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '德国威娜创艺系列修护露', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 22), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '德国威娜创艺水晶造型液200ml ', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 23), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '德国威娜可丽丝倍佳染膏60ml ', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 24), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '德国威娜直发套装(烫前护理液, 威娜拉直膏, 绿茶亮滑魔力霜)', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 25), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '德国威娜润雅香波1000ml ', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 26), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '德国原产威娜创艺泡沫发蜡新版150ml', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 27), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '德国威娜染膏-可丽丝倍佳染膏60ml', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 28), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '德国威娜自然直媲美离子烫100ml ', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 29), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '德国威娜拉直膏100ml ', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 30), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '德国威娜倍欧均衡营养系列', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 31), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '德国威娜倍欧烫后营养发膜750ml ', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 32), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '德国威娜倍欧加倍营养发膜750ml', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 33), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '德国威娜倍欧保湿营养系列', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 34), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '德国威娜典雅强力饰发透明膏 ', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 35), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '德国威娜焗油护发膏220g ', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 36), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '德国威娜润丝特效修护焗油膏', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 37), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '德国威娜润丝修护发膜 ', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 38), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '德国威娜丝美冷烫精(针对染后发质) ', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 39), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '德国威娜自然直套装', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 40), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '德国威娜植物焗油膏 ', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 41), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '德国威娜浓缩焗油护发膜454g ', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 42), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '德国威娜深层修护焗油膏 ', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 43), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '德国威娜滋丝特效发膜 ', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 44), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '德国威娜金华素', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 45), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '德国威娜珍露精华素 ', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 46), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '德国威娜润姿润雅洗护1000ml ', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 47), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '德国威娜SP*活力洁发乳 ', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 48), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '德国威娜SP固色洁发乳250ml', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 49), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '德国威娜sp专业护理系统3.1保湿润发膜 ', '2', 'BOT', 200, 90, '0') select @goodsno = @prefix + right(@strpre + convert(varchar(20), @flow + 50), @nlen) + @suffix insert gfm01(gfa00c, gfa01c, gfa02c, gfa03c, gfa05c, gfa07c, gfa11f, gfa28i, gfa10c) values(@compid, @goodsno, @goodsno, '德国威娜sp专业护理系统发尾修护胶', '2', 'BOT', 200, 90, '0') end end GO /****** Object: StoredProcedure [dbo].[upg_change_card_threekind] Script Date: 06/12/2017 06:19:38 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_change_card_threekind] ( @compid varchar(10), @fromcard varchar(20), @tocard varchar(20), @cardkind int, @userid varchar(20), @curdate varchar(10), @curtime varchar(10) ) as begin if @compid = '*' or @compid = '' or @compid = '001' begin select @compid = '%' end update gcm01 set gca03i = @cardkind where gca00c like @compid and gca01c >= @fromcard and gca01c <= @tocard insert sysoperationlog(userid, program, operation, operationdate, operationtime, origatedate, compid, keyvalue1, keyvalue2, keyvalue3, keyvalue4) values(@userid, 'GSB24I', 'M', @curdate, @curtime, @curdate, @compid, @fromcard, @tocard, 'card_threekind', '强行更改卡的大类代码') end GO /****** Object: StoredProcedure [dbo].[upg_change_card_type] Script Date: 06/12/2017 06:19:39 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_change_card_type] ( @compid varchar(10), @fromcard varchar(20), @tocard varchar(20), @cardtype varchar(20), @userid varchar(20), @curdate varchar(10), @curtime varchar(10) ) as begin if @compid = '*' or @compid = '' or @compid = '001' begin select @compid = '%' end -- 因为是批量,所以只能考虑在一种大类的会员卡之间进行变换 update gcm01 set gca02c = @cardtype where gca00c like @compid and gca01c >= @fromcard and gca01c <= @tocard insert sysoperationlog(userid, program, operation, operationdate, operationtime, origatedate, compid, keyvalue1, keyvalue2, keyvalue3, keyvalue4) values(@userid, 'GSB24I', 'M', @curdate, @curtime, @curdate, @compid, @fromcard, @tocard, 'change_card_type', '强行更改卡的类别代码') end GO /****** Object: StoredProcedure [dbo].[up_sysoperationlog_update] Script Date: 06/12/2017 06:19:22 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO --WUXM 2004/6/29 create procedure [dbo].[up_sysoperationlog_update] @userid varchar(10), --User ID @program varchar(10), --程式代码 @operation varchar(1), --操作 @operationdate varchar(8), --异动日期 @operationtime varchar(8), --异动时间 @origatedate varchar(8), --单据作业时间. @compid varchar(10), --Company ID @keyvalue1 varchar(160), @keyvalue2 varchar(160), @keyvalue3 varchar(160), @keyvalue4 varchar(160), @Dlog_id int as--WUXM, 2004/6/29 --update: wuxinming, 2004/11/22 begin insert sysoperationlog(userid, program, operation, operationdate, operationtime, origatedate, compid, keyvalue1, keyvalue2, keyvalue3, keyvalue4) values(@userid, @program, @operation, @operationdate, @operationtime, @origatedate, @compid, @keyvalue1, @keyvalue2, @keyvalue3, @keyvalue4); end GO /****** Object: StoredProcedure [dbo].[upg_link_card] Script Date: 06/12/2017 06:20:17 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_link_card] ( @oldcardid varchar(20), @newcardid varchar(20), @userid varchar(20), @curdate varchar(10), @curtime varchar(10) ) as begin declare @new_state int declare @old_state int declare @have_old_card int select @have_old_card = 1, @old_state = gca08i from gcm01 with (nolock) where gca01c = @oldcardid and gca00c = gca13d select @have_old_card = isnull(@have_old_card, 0), @old_state = isnull(@old_state, 1) select @new_state = gca08i from gcm01 with (nolock) where gca00c = gca13d and gca01c = @newcardid if (@have_old_card = 0) return insert sysoperationlog(userid, program, operation, operationdate, operationtime, origatedate, compid, keyvalue1, keyvalue2, keyvalue3) values(@userid, 'GSB01I', 'M', @curdate, @curtime, @curdate, '001', @oldcardid, @newcardid, 'upg_link_card') insert gcm02(gcb00c, gcb01c, gcb02f, gcb03i, gcb04c, gcb05i, gcb06i, gcb07d, gcb08c) values('001', @oldcardid, 99, 4, '', 0, 6, @curdate, @newcardid) update gcm01 set gca08i = 6 where gca01c = @oldcardid update gcm03 set gcc06f = 0, gcc10f = 0 where gcc01c = @oldcardid insert gcm02(gcb00c, gcb01c, gcb02f, gcb03i, gcb04c, gcb05i, gcb06i, gcb07d, gcb08c) values('001', @newcardid, -1, 4, '', 0, 4, @curdate, @oldcardid) end GO /****** Object: StoredProcedure [dbo].[upg_get_workperf_rate] Script Date: 06/12/2017 06:20:09 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_get_workperf_rate] ( @compid varchar(10), @cash_rate float output, @cash_good_rate float output, @credit_rate float output, @credit_good_rate float output, @pay3_rate float output, @pay3_good_rate float output, @pay$_rate float output, @pay$_good_rate float output, @pay0_rate float output, @pay0_good_rate float output, @manager_rate float output, @manager_good_rate float output, @point_rate float output, @point_good_rate float output, @user01_rate float output, @user01_good_rate float output, @user02_rate float output, @user02_good_rate float output, @user03_rate float output, @user03_good_rate float output, @user04_rate float output, @user04_good_rate float output, @user05_rate float output, @user05_good_rate float output, @zfb_rate float output, @zfb_good_rate float output, @wx_rate float output, @wx_good_rate float output ) as begin select @cash_rate = isnull(gso09f, 0) * isnull(gso03i,1) from gsm15 with (nolock) where gso00c = @compid and gso02c = '1' select @cash_good_rate = isnull(gso10f, 0) * isnull(gso06f,1) from gsm15 with (nolock) where gso00c = @compid and gso02c = '1' select @credit_rate = isnull(gso09f, 0) * isnull(gso03i,1) from gsm15 with (nolock) where gso00c = @compid and gso02c = '6' select @credit_good_rate = isnull(gso10f, 0) * isnull(gso06f,1) from gsm15 with (nolock) where gso00c = @compid and gso02c = '6' select @pay3_rate = isnull(gso09f, 0) * isnull(gso03i,1) from gsm15 with (nolock) where gso00c = @compid and gso02c = '3' select @pay3_good_rate = isnull(gso10f, 0) * isnull(gso06f,1) from gsm15 with (nolock) where gso00c = @compid and gso02c = '3' select @pay$_rate = isnull(gso09f, 0) * isnull(gso03i,1) from gsm15 with (nolock) where gso00c = @compid and gso02c = '$' select @pay$_good_rate = isnull(gso10f, 0) * isnull(gso06f,1) from gsm15 with (nolock) where gso00c = @compid and gso02c = '$' select @pay0_rate = isnull(gso09f, 0) * isnull(gso03i,1) from gsm15 with (nolock) where gso00c = @compid and gso02c = '0' select @pay0_good_rate = isnull(gso10f, 0) * isnull(gso06f,1) from gsm15 with (nolock) where gso00c = @compid and gso02c = '0' select @manager_rate = isnull(gso09f, 0) * isnull(gso03i,1) from gsm15 with (nolock) where gso00c = @compid and gso02c = '8' select @manager_good_rate = isnull(gso10f, 0) * isnull(gso06f,1) from gsm15 with (nolock) where gso00c = @compid and gso02c = '8' select @point_rate = isnull(gso09f, 0) * isnull(gso03i,1) from gsm15 with (nolock) where gso00c = @compid and gso02c = '7' select @point_good_rate = isnull(gso10f, 0) * isnull(gso06f,1) from gsm15 with (nolock) where gso00c = @compid and gso02c = '7' select @user01_rate = isnull(gso09f, 0) * isnull(gso03i,1) from gsm15 with (nolock) where gso00c = @compid and gso02c = '$1' select @user01_good_rate = isnull(gso10f, 0) * isnull(gso06f,1) from gsm15 with (nolock) where gso00c = @compid and gso02c = '$1' select @user02_rate = isnull(gso09f, 0) * isnull(gso03i,1) from gsm15 with (nolock) where gso00c = @compid and gso02c = '$2' select @user02_good_rate = isnull(gso10f, 0) * isnull(gso06f,1) from gsm15 with (nolock) where gso00c = @compid and gso02c = '$2' select @user03_rate = isnull(gso09f, 0) * isnull(gso03i,1) from gsm15 with (nolock) where gso00c = @compid and gso02c = '$3' select @user03_good_rate = isnull(gso10f, 0) * isnull(gso06f,1) from gsm15 with (nolock) where gso00c = @compid and gso02c = '$3' select @user04_rate = isnull(gso09f, 0) * isnull(gso03i,1) from gsm15 with (nolock) where gso00c = @compid and gso02c = '$4' select @user04_good_rate = isnull(gso10f, 0) * isnull(gso06f,1) from gsm15 with (nolock) where gso00c = @compid and gso02c = '$4' select @user05_rate = isnull(gso09f, 0) * isnull(gso03i,1) from gsm15 with (nolock) where gso00c = @compid and gso02c = '$5' select @user05_good_rate = isnull(gso10f, 0) * isnull(gso06f,1) from gsm15 with (nolock) where gso00c = @compid and gso02c = '$5' select @zfb_rate = isnull(gso09f, 0) * isnull(gso03i,1) from gsm15 with (nolock) where gso00c = @compid and gso02c = 'ZFB' select @zfb_good_rate = isnull(gso10f, 0) * isnull(gso06f,1) from gsm15 with (nolock) where gso00c = @compid and gso02c = 'ZFB' select @wx_rate = isnull(gso09f, 0) * isnull(gso03i,1) from gsm15 with (nolock) where gso00c = @compid and gso02c = 'WX' select @wx_good_rate = isnull(gso10f, 0) * isnull(gso06f,1) from gsm15 with (nolock) where gso00c = @compid and gso02c = 'WX' set @cash_rate = isnull(@cash_rate, 1); set @cash_good_rate = isnull(@cash_good_rate, 1); set @credit_rate = isnull(@credit_rate, 1); set @credit_good_rate = isnull(@credit_good_rate, 1); set @pay3_rate = isnull(@pay3_rate, 1); set @pay3_good_rate = isnull(@pay3_good_rate, 1); set @pay$_rate = isnull(@pay$_rate, 1); set @pay$_good_rate = isnull(@pay$_good_rate, 1); set @pay0_rate = isnull(@pay0_rate, 1); set @pay0_good_rate = isnull(@pay0_good_rate, 1); set @manager_rate = isnull(@manager_rate, 1); set @manager_good_rate = isnull(@manager_good_rate, 1); set @point_rate = isnull(@point_rate, 1); set @point_good_rate = isnull(@point_good_rate, 1); set @user01_rate = isnull(@user01_rate, 1); set @user01_good_rate = isnull(@user01_good_rate, 1); set @user02_rate = isnull(@user02_rate, 1); set @user02_good_rate = isnull(@user02_good_rate, 1); set @user03_rate = isnull(@user03_rate, 1); set @user03_good_rate = isnull(@user03_good_rate, 1); set @user04_rate = isnull(@user04_rate, 1); set @user04_good_rate = isnull(@user04_good_rate, 1); set @user05_rate = isnull(@user05_rate, 1); set @user05_good_rate = isnull(@user05_good_rate, 1); set @zfb_rate = isnull(@zfb_rate, 1) set @zfb_good_rate = isnull(@zfb_good_rate, 1) set @wx_rate = isnull(@wx_rate, 1) set @wx_good_rate = isnull(@wx_good_rate, 1) end GO /****** Object: StoredProcedure [dbo].[upg_isrevenue_payment] Script Date: 06/12/2017 06:20:17 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_isrevenue_payment] ( @compid varchar(10), @payment_0 varchar(10) output, @payment_3 varchar(10) output, @payment_$ varchar(10) output, @payment_u1 varchar(10) output, @payment_u2 varchar(10) output, @payment_u3 varchar(10) output, @payment_u4 varchar(10) output, @payment_u5 varchar(10) output ) as begin declare @val float -- select @val = gso07f from gsm15 with (nolock) where gso00c = @compid and gso01c = '3' and gso02c = '0' select @val = isnull(@val, 0) if @val = 1 select @payment_0 = '0' else select @payment_0 = 'ZZZ' -- select @val = gso07f from gsm15 with (nolock) where gso00c = @compid and gso01c = '3' and gso02c = '3' select @val = isnull(@val, 0) if @val = 1 select @payment_3 = '3' else select @payment_3 = 'ZZZ' -- select @val = gso07f from gsm15 with (nolock) where gso00c = @compid and gso01c = '3' and gso02c = '$' select @val = isnull(@val, 0) if @val = 1 select @payment_$ = '$' else select @payment_$ = 'ZZZ' -- select @val = gso07f from gsm15 with (nolock) where gso00c = @compid and gso01c = '3' and gso02c = '$1' select @val = isnull(@val, 0) if @val = 1 select @payment_u1 = '$1' else select @payment_u1 = 'ZZZ' -- select @val = gso07f from gsm15 with (nolock) where gso00c = @compid and gso01c = '3' and gso02c = '$2' select @val = isnull(@val, 0) if @val = 1 select @payment_u2 = '$2' else select @payment_u2 = 'ZZZ' -- select @val = gso07f from gsm15 with (nolock) where gso00c = @compid and gso01c = '3' and gso02c = '$3' select @val = isnull(@val, 0) if @val = 1 select @payment_u3 = '$3' else select @payment_u3 = 'ZZZ' -- select @val = gso07f from gsm15 with (nolock) where gso00c = @compid and gso01c = '3' and gso02c = '$4' select @val = isnull(@val, 0) if @val = 1 select @payment_u4 = '$4' else select @payment_u4 = 'ZZZ' -- select @val = gso07f from gsm15 with (nolock) where gso00c = @compid and gso01c = '3' and gso02c = '$5' select @val = isnull(@val, 0) if @val = 1 select @payment_u5 = '$5' else select @payment_u5 = 'ZZZ' end GO /****** Object: StoredProcedure [dbo].[upg_isrevenue_payment_shop] Script Date: 06/12/2017 06:20:17 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_isrevenue_payment_shop] ( @compid varchar(10), @payment_0 varchar(10) output, @payment_3 varchar(10) output, @payment_$ varchar(10) output, @payment_u1 varchar(10) output, @payment_u2 varchar(10) output, @payment_u3 varchar(10) output, @payment_u4 varchar(10) output, @payment_u5 varchar(10) output, @payment_u6 varchar(10) output, @payment_u7 varchar(10) output, @payment_u8 varchar(10) output, @payment_u9 varchar(10) output, @payment_u10 varchar(10) output, @payment_u11 varchar(10) output, @payment_u12 varchar(10) output, @payment_u13 varchar(10) output, @payment_u14 varchar(10) output, @payment_u15 varchar(10) output ) as begin declare @val float -- select @val = gso07f from gsm15 with (nolock) where gso00c = @compid and gso01c = '3' and gso02c = '0' select @val = isnull(@val, 0) if @val = 1 select @payment_0 = '0' else select @payment_0 = 'ZZZ' -- select @val = gso07f from gsm15 with (nolock) where gso00c = @compid and gso01c = '3' and gso02c = '3' select @val = isnull(@val, 0) if @val = 1 select @payment_3 = '3' else select @payment_3 = 'ZZZ' -- select @val = gso07f from gsm15 with (nolock) where gso00c = @compid and gso01c = '3' and gso02c = '$' select @val = isnull(@val, 0) if @val = 1 select @payment_$ = '$' else select @payment_$ = 'ZZZ' -- select @val = gso07f from gsm15 with (nolock) where gso00c = @compid and gso01c = '3' and gso02c = '$1' select @val = isnull(@val, 0) if @val = 1 select @payment_u1 = '$1' else select @payment_u1 = 'ZZZ' -- select @val = gso07f from gsm15 with (nolock) where gso00c = @compid and gso01c = '3' and gso02c = '$2' select @val = isnull(@val, 0) if @val = 1 select @payment_u2 = '$2' else select @payment_u2 = 'ZZZ' -- select @val = gso07f from gsm15 with (nolock) where gso00c = @compid and gso01c = '3' and gso02c = '$3' select @val = isnull(@val, 0) if @val = 1 select @payment_u3 = '$3' else select @payment_u3 = 'ZZZ' -- select @val = gso07f from gsm15 with (nolock) where gso00c = @compid and gso01c = '3' and gso02c = '$4' select @val = isnull(@val, 0) if @val = 1 select @payment_u4 = '$4' else select @payment_u4 = 'ZZZ' -- select @val = gso07f from gsm15 with (nolock) where gso00c = @compid and gso01c = '3' and gso02c = '$5' select @val = isnull(@val, 0) if @val = 1 select @payment_u5 = '$5' else select @payment_u5 = 'ZZZ' select @val = gso07f from gsm15 with (nolock) where gso00c = @compid and gso01c = '3' and gso02c = '$6' select @val = isnull(@val, 0) if @val = 1 select @payment_u6 = '$6' else select @payment_u6 = 'ZZZ' select @val = gso07f from gsm15 with (nolock) where gso00c = @compid and gso01c = '3' and gso02c = '$7' select @val = isnull(@val, 0) if @val = 1 select @payment_u7 = '$7' else select @payment_u7 = 'ZZZ' select @val = gso07f from gsm15 with (nolock) where gso00c = @compid and gso01c = '3' and gso02c = '$8' select @val = isnull(@val, 0) if @val = 1 select @payment_u8 = '$8' else select @payment_u8 = 'ZZZ' select @val = gso07f from gsm15 with (nolock) where gso00c = @compid and gso01c = '3' and gso02c = '$9' select @val = isnull(@val, 0) if @val = 1 select @payment_u9 = '$9' else select @payment_u9 = 'ZZZ' select @val = gso07f from gsm15 with (nolock) where gso00c = @compid and gso01c = '3' and gso02c = '$5' select @val = isnull(@val, 0) if @val = 1 select @payment_u10 = '$10' else select @payment_u10 = 'ZZZ' select @val = gso07f from gsm15 with (nolock) where gso00c = @compid and gso01c = '3' and gso02c = '$11' select @val = isnull(@val, 0) if @val = 1 select @payment_u11 = '$11' else select @payment_u11 = 'ZZZ' select @val = gso07f from gsm15 with (nolock) where gso00c = @compid and gso01c = '3' and gso02c = '$12' select @val = isnull(@val, 0) if @val = 1 select @payment_u12 = '$12' else select @payment_u12 = 'ZZZ' select @val = gso07f from gsm15 with (nolock) where gso00c = @compid and gso01c = '3' and gso02c = '$13' select @val = isnull(@val, 0) if @val = 1 select @payment_u13 = '$13' else select @payment_u13 = 'ZZZ' select @val = gso07f from gsm15 with (nolock) where gso00c = @compid and gso01c = '3' and gso02c = '$14' select @val = isnull(@val, 0) if @val = 1 select @payment_u14 = '$14' else select @payment_u14 = 'ZZZ' select @val = gso07f from gsm15 with (nolock) where gso00c = @compid and gso01c = '3' and gso02c = '$15' select @val = isnull(@val, 0) if @val = 1 select @payment_u15 = '$15' else select @payment_u15 = 'ZZZ' end GO /****** Object: StoredProcedure [dbo].[upg_m_dispatch_gsm15] Script Date: 06/12/2017 06:20:20 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_m_dispatch_gsm15] ( @fromcompid varchar(10), -- 从哪一个公司发布 @tocompid varchar(10), -- 目的公司 @mandatory varchar(2) -- 是否强制统一 ) as begin create table #gsm15 ( gso00c varchar(10) not null, -- 公司别 gso01c varchar(10) not null, -- 分类码, 3-支付方式 gso02c varchar(5) not null, -- 支付方式类别代码 gso03i float null, -- 项目实际营业额折算比率 gso04f float null, -- 积分折算比率 gso05f float null, -- 卖卡折算比率 gso06f float null, -- 产品实际营业额比率 gso07f float null, -- 作为营业收入的比率 gso08f float null, -- 计算劳动业绩的比率 gso09f float null, -- 项目劳动业绩的比率 gso10f float null, -- 产品劳动业绩的比率 ) declare @PK_Sql nvarchar(max) set @PK_Sql = ' alter table #gsm15 add constraint PK_#gsm15_' + convert(varchar(10), cast(floor(rand() * 1000000) as int)) + ' primary key (gso00c, gso01c, gso02c) ' exec sp_executesql @PK_Sql insert #gsm15(gso00c, gso01c, gso02c, gso03i, gso04f, gso05f, gso06f, gso07f, gso08f, gso09f, gso10f) select gso00c, gso01c, gso02c, gso03i, gso04f, gso05f, gso06f, gso07f, gso08f, gso09f, gso10f from gsm15 where gso00c = @fromcompid if @mandatory = '1' begin delete gsm15 where gso00c = @tocompid end else begin delete a from #gsm15 a, gsm15 b where a.gso00c = @fromcompid and a.gso01c = b.gso01c and a.gso02c = b.gso02c and b.gso00c = @tocompid end insert gsm15(gso00c, gso01c, gso02c, gso03i, gso04f, gso05f, gso06f, gso07f, gso08f, gso09f, gso10f) select @tocompid, gso01c, gso02c, gso03i, gso04f, gso05f, gso06f, gso07f, gso08f, gso09f, gso10f from #gsm15 where gso00c = @fromcompid drop table #gsm15 end GO /****** Object: StoredProcedure [dbo].[upg_recover_gsm15_gso09f_gs010f] Script Date: 06/12/2017 06:20:32 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_recover_gsm15_gso09f_gs010f] as begin -- 判断是否已做数据恢复 if exists (select 1 from data_recover where rdd00c = '001' and rdd01c = 'gsm15' and rdd02c = 'recover_gso09_10') begin return end update gsm15 set gso09f = gso08f, gso10f = gso08f insert into data_recover(rdd00c, rdd01c, rdd02c) values ('001', 'gsm15', 'recover_gso09_10') end GO /****** Object: StoredProcedure [dbo].[upg_unsalable_goods_analysis] Script Date: 06/12/2017 06:20:48 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_unsalable_goods_analysis] ( @compid varchar(10), -- 公司别 @deadline varchar(8), -- 截止日期 @goodsfrom varchar(20), -- 产品编号 @goodsto varchar(20), -- 产品编号 @goodstypefrom varchar(5), -- 产品大类 @goodstypeto varchar(5) -- 产品大类 ) as -- WUXM 2005/10/27 begin create table #result ( idd int identity not null, goodsno varchar(20) null, goodsname varchar(80) null, saledate varchar(8) null, --最后消费日期 unsale float null, --停滞天数 quantity float null, --现库存量 primary key (idd) ) declare @goodsno varchar(20) --产品代码 insert into #result(goodsno, goodsname) select distinct ghb03c, gfa03c from ghm02 with (nolock), gfm01 where ghb00c = gfa00c and ghb03c = gfa01c and ghb00c = @compid and (gfa01c between @goodsfrom and @goodsto or @goodsfrom = '*') and (gfa05c between @goodstypefrom and @goodstypeto or @goodstypefrom = '*') and ghb03c not in (select gfd04c from gfm04 with (nolock) where gfd07c>@deadline) declare cur_goods_sale_date cursor for select goodsno from #result open cur_goods_sale_date fetch cur_goods_sale_date into @goodsno while @@fetch_status = 0 begin declare @saledate varchar(20) declare @instock float declare @outstock float select @instock = sum(isnull(gfd05f, 0)) from gfm04 with (nolock) where gfd00c = @compid and gfd04c = @goodsno and gfd01c = '1' select @outstock = sum(isnull(gfd05f, 0)) from gfm04 with (nolock) where gfd00c = @compid and gfd04c = @goodsno and gfd01c in ('2', '3', '4', '5', '6', '7', '8') select @saledate = gfd07c from gfm04 with (nolock) where gfd00c = @compid and gfd04c = @goodsno and gfd01c in ('2', '3', '4', '5', '6', '7', '8') order by gfd07c --有销售记录,从gfm04中获得上次消费时间 if (len(@saledate) > 0) begin update a set a.saledate = @saledate, a.unsale = datediff(day, @saledate, getdate()), a.quantity = isnull(@instock, 0)-isnull(@outstock, 0) from #result a where a.goodsno = @goodsno end --无销售记录,从ghm02中获得入库时间 else begin select @saledate = gha03d from ghm01 with (nolock), ghm02 with (nolock) where gha00c = ghb00c and gha01c = ghb01c and gha00c = @compid and ghb03c = @goodsno order by ghb03c update a set a.saledate = @saledate, a.unsale = datediff(day, @saledate, getdate()), a.quantity = isnull(@instock, 0)-isnull(@outstock, 0) from #result a where a.goodsno = @goodsno end fetch cur_goods_sale_date into @goodsno end close cur_goods_sale_date deallocate cur_goods_sale_date select goodsno, goodsname, saledate, daynum = cast(unsale as int), quantity, gsb03c from #result, gfm01, gsm02 where gfa00c = gsb00c and gfa01c = goodsno and gfa00c = @compid and gfa07c = gsb02c and gsb01c = 'K' and isnull(quantity, 0) >0 order by unsale desc drop table #result end GO /****** Object: StoredProcedure [dbo].[upg_conditionghb02r] Script Date: 06/12/2017 06:19:44 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_conditionghb02r] ( @compid varchar(10), -- 公司别 @pccode varchar(20), @showtype int, @ghb03c varchar(2000) ) as begin if not exists( select 1 from conditionghb02r where ghb00c=@compid and ghb01c=@pccode and ghb02i=@showtype) begin insert into conditionghb02r (ghb00c,ghb01c,ghb02i,ghb03c,ghb04c) values(@compid,@pccode,@showtype,@ghb03c,'1') end else begin update conditionghb02r set ghb03c=@ghb03c where ghb00c=@compid and ghb01c=@pccode and ghb02i=@showtype end end GO /****** Object: StoredProcedure [dbo].[upg_get_adv] Script Date: 06/12/2017 06:20:04 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_get_adv] as begin declare @code varchar(10) declare @content varchar(200) select @code = gsn01i, @content = gsn03c from gsm24 where gsn05i = 1 order by gsn02c update gsm24 set gsn05i = 0 where gsn02c = @code update gsm24 set gsn05i = 1 where gsn02c<>@code select @content end GO /****** Object: StoredProcedure [dbo].[upg_m_dispatch_gfm05] Script Date: 06/12/2017 06:20:19 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_m_dispatch_gfm05] ( @fromcompid varchar(10), -- 从哪一个公司发布 @tocompid varchar(10), -- 目的公司 @mandatory varchar(2) -- 是否强制统一 ) as begin if @fromcompid = @tocompid return create table #gfm05_01 -- 产品消耗设定 ( gfe00c varchar(10) not null, -- 公司编号 gfe01c varchar(20) not null, -- 项目编号 gfe02c varchar(30) not null, -- 产品编号 gfe03f float null, -- 标准消耗数量 gfe04i int null, -- 锁定标识 1-锁定 0-未锁定 默认为0-未锁定 ) declare @PK_Sql nvarchar(max) set @PK_Sql = ' alter table #gfm05_01 add constraint PK_#gfm05_01_' + convert(varchar(10), cast(floor(rand() * 1000000) as int)) + ' primary key clustered (gfe00c, gfe01c, gfe02c)' exec sp_executesql @PK_Sql insert #gfm05_01 select gfe00c, gfe01c, gfe02c, gfe03f, gfe04i from gfm05 where gfe00c = @fromcompid if @mandatory = '1' begin delete gfm05 where gfe00c = @tocompid end else begin delete a from #gfm05_01 a, gfm05 b where a.gfe00c = @fromcompid and a.gfe01c = b.gfe01c and b.gfe00c = @tocompid and a.gfe02c = b.gfe02c end insert gfm05(gfe00c, gfe01c, gfe02c, gfe03f, gfe04i) select @tocompid, gfe01c, gfe02c, gfe03f, gfe04i from #gfm05_01 where gfe00c = @fromcompid drop table #gfm05_01 end GO /****** Object: StoredProcedure [dbo].[alter_key] Script Date: 06/12/2017 06:19:19 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[alter_key] ( @compid varchar(10), -- 公司编号 @billNo varchar(20) -- 开单单号 ) as begin declare @flag bigint --钥匙牌号 select @flag = keyid from openpolicy_master where compid = @compid and billid = @billNo if isnull(@flag, '') <> '' begin update flagkey set iuse = 0 where compid = @compid and keyid = @flag end end GO /****** Object: StoredProcedure [dbo].[upg_insert_gsm26_gsm27] Script Date: 06/12/2017 06:20:15 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_insert_gsm26_gsm27] @compid varchar(10), @type varchar(5), --F L AL AJ @typeno varchar(5), --0-1-2-3 @fatherclassno varchar(5), --父亲编号 @classno varchar(5), --编号 @classname varchar(40) as begin create table #insert ( classno varchar(5) null ) declare @remark varchar(100) if (@type = 'F') set @remark = '产品大类' else if (@type = 'L') set @remark = '项目大类' else if (@type = 'AL') set @remark = '产品统计大类' else set @remark = '项目统计大类' insert into #insert(classno) select distinct gsq03c from gsm27 where gsq00c = @compid and gsq02i = @typeno and gsq04c = @fatherclassno insert into gsm26(gsp00c, gsp02i, gsp03c, gsp04c, gsp05c, gsp06c) values(@compid, @typeno, @classno, @type, @fatherclassno, '') insert gsm27(gsq00c, gsq02i, gsq03c, gsq04c, gsq05c) select @compid, @typeno, classno, @classno, @type from #insert insert gsm27(gsq00c, gsq02i, gsq03c, gsq04c, gsq05c) values( @compid, @typeno, @classno, @classno, @type) end GO /****** Object: StoredProcedure [dbo].[upg_insert_lookon_item] Script Date: 06/12/2017 06:20:16 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_insert_lookon_item] ( @compid varchar(10), -- 公司别 @billid varchar(20), -- 单据号 @flow float, -- 流水号 @item varchar(20), -- 项目代号 @type int, -- 1:会员 2:散客 @cardid varchar(20), -- 会员卡号 @name varchar(20), -- 散客/会员姓名 @tel varchar(20), -- 固定电话 @mobile varchar(20), -- 手机 @date varchar(10), -- 收银日期 @lookon_date varchar(10), -- 回访日期 @kind_type int, -- 0项目 1产品 @nname varchar(500) -- 产品或项目的名称 ) as begin declare @memid varchar(20) declare @userid varchar(20) delete gbm10 where gbj00c = @compid and gbj02c = @billid and gbj03f = @flow and gbj16i = @kind_type select @memid = gla27c, @userid = gga91c from glm01 a with (nolock), ggm01 b with (nolock) where a.gla00c = b.gga00c and a.gla01c = b.gga30c and a.gla00c = @compid and b.gga01c = @billid select @memid = isnull(@memid, @cardid) declare @gbj18c varchar(20) declare @gbj21c varchar(20) declare @gbj24c varchar(20) declare @gbj27c varchar(20) if @kind_type = 0 begin select @gbj18c = isnull(ggb12c, ''), @gbj21c = isnull(ggb13c, ''), @gbj24c = isnull(ggb14c, ''), @gbj27c = isnull(ggb26c, '') from ggm02 where ggb00c = @compid and ggb01c = @billid and ggb03c = @item end else if @kind_type = 1 begin select @gbj18c = isnull(ggc12c, ''), @gbj21c = isnull(ggc15c, ''), @gbj24c = isnull(ggc17c, ''), @gbj27c = '' from ggm03 where ggc00c = @compid and ggc01c = @billid and ggc03c = @item end insert gbm10(gbj00c, gbj02c, gbj13i, gbj03f, gbj04c, gbj05c, gbj06c, gbj07d, gbj08d, gbj10i, gbj14c, gbj15c, gbj16i, gbj17c, gbj18c, gbj21c, gbj24c, gbj27c, gbj30c, gbj32c) values(@compid, @billid, @type, @flow, @memid, @item, @tel, @date, @lookon_date, 0, @name, @mobile, @kind_type, @nname, @gbj18c, @gbj21c, @gbj24c, @gbj27c, @userid, @compid) end GO /****** Object: StoredProcedure [dbo].[upg_m_dispatch_gsm29_1] Script Date: 06/12/2017 06:20:20 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_m_dispatch_gsm29_1] ( @fromcompid varchar(10), -- 从哪一个公司发布 @tocompid varchar(10), -- 目的公司 @mandatory varchar(2) -- 是否强制统一 ) as begin if @fromcompid = @tocompid return if @mandatory = '1' begin delete gsm29 where gsr00c = @tocompid and gsr03i = 1 end -- 插入 insert into gsm29 (gsr00c, gsr01c, gsr02c, gsr03i) select @tocompid, gsr01c, gsr02c, gsr03i from gsm29 where gsr00c = @fromcompid and gsr01c not in (select gsr01c from gsm29 where gsr00c = @tocompid and gsr03i = 1) end GO /****** Object: StoredProcedure [dbo].[upg_get_gx_time_department] Script Date: 06/12/2017 06:20:07 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_get_gx_time_department] ( @compid varchar(10), @billid varchar(20) ) as begin declare @r_date varchar(20) declare @r_time varchar(20) declare @r_dept varchar(20) declare @r_staff varchar(20) select @r_date = gga02d, @r_time = gga03t from ggm01 where gga00c = @compid and gga01c = @billid select @r_dept = ggb81c, @r_staff = ggb12c from ggm02 where ggb40i = 1 and ggb00c = @compid and ggb01c = @billid order by ggb02f desc if (isnull(@r_dept, '') = '') begin select @r_dept = ggc81c, @r_staff = ggc12c from ggm03 where ggc32i = 1 and ggc00c = @compid and ggc01c = @billid order by ggc02f desc end select gla03t = @r_time, gga02d = @r_date, dept = @r_dept, staff = @r_staff end GO /****** Object: StoredProcedure [dbo].[upg_client_count] Script Date: 06/12/2017 06:19:42 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_client_count] ( @compid varchar(10), @fromdate varchar(8), @todate varchar(8) ) as begin declare @cust_girl int declare @cust_man int declare @cust_old int declare @cust_new int select @cust_girl = 0, @cust_man = 0, @cust_old = 0, @cust_new = 0 -- 创建临时表 create table #temp_comp ( compId varchar(10) not null ) insert into #temp_comp(compId) select gaz02c from gam26 with (nolock) where gaz01c = @compid -- 创建循环变量 declare @HasRecord int set @HasRecord = 1 declare @currentCompId varchar(10) while(@HasRecord = 1) begin if exists(select 1 from #temp_comp) begin set @HasRecord = 1 select top 1 @currentCompId = compId from #temp_comp select @cust_girl = @cust_girl + isnull(sum(case when gga31i = 0 then gga14f else 0 end), 0), @cust_man = @cust_man + isnull(sum(case when gga31i = 1 then gga14f else 0 end), 0), @cust_old = @cust_old + isnull(sum(case when gga13f = 1 then gga14f else 0 end), 0), @cust_new = @cust_new + isnull(sum(case when gga13f = 0 then gga14f else 0 end), 0) from ggm01 where gga00c = @currentCompId and gga80d between @fromdate and @todate -- 清除已处理的门店 delete #temp_comp where compId = @currentCompId end else begin set @HasRecord = 0 end end drop table #temp_comp select cust_girl = @cust_girl, cust_man = @cust_man, cust_old = @cust_old, cust_new = @cust_new end GO /****** Object: StoredProcedure [dbo].[upg_set_return_flag] Script Date: 06/12/2017 06:20:41 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_set_return_flag] ( @compid varchar(10), @billid varchar(20), -- 还款单号 @currdate varchar(10), -- 应该为账务日期才合理 @confirm int ) as begin if @confirm = 0 begin update ggm03 set ggc33d = '', ggc34c = '' where ggc00c = @compid and ggc34c = @billid update ggm02 set ggb41d = '', ggb42c = '' where ggb00c = @compid and ggb42c = @billid return end -- 下面处理的都是复核的情况下 declare @cardid varchar(20) declare @amt float declare @billamt float declare @remainamt float select @cardid = gcl03c, @amt = gcl07f, @currdate = gcl80d from gcm10 where gcl00c = @compid and gcl01c = @billid and gcl06i = 6 select @remainamt = @amt if isnull(@amt, 0) = 0 or isnull(@cardid, '') = '' return -- 应该可以支持跨店还款 declare cur_get_each_def cursor for select ggc11f from ggm03 b, ggm01 a where b.ggc00c = a.gga00c and b.ggc01c = a.gga01c and a.gga09c = @cardid and b.ggc32i = 1 and isnull(ggc33d, '') = '' order by gga02d for update of ggc33d, ggc34c open cur_get_each_def fetch cur_get_each_def into @billamt while @@fetch_status = 0 and @remainamt > 0 begin if @billamt <= @remainamt begin update ggm03 set ggc33d = @currdate, ggc34c = @billid where current of cur_get_each_def select @remainamt = @remainamt - @billamt end else if @remainamt > @billamt / 2 begin update ggm03 set ggc33d = @currdate, ggc34c = @billid where current of cur_get_each_def select @remainamt = @remainamt - @billamt end fetch cur_get_each_def into @billamt end close cur_get_each_def deallocate cur_get_each_def if @remainamt > 0 begin -- 支持跨店还款 declare cur_get_each_def_2 cursor for select ggb11f from ggm02 b, ggm01 a where b.ggb00c = a.gga00c and b.ggb01c = a.gga01c and a.gga09c = @cardid and b.ggb40i = 1 and isnull(ggb41d, '') = '' order by gga02d for update of ggb41d, ggb42c open cur_get_each_def_2 fetch cur_get_each_def_2 into @billamt while @@fetch_status = 0 and @remainamt > 0 begin if @billamt <= @remainamt begin update ggm02 set ggb41d = @currdate, ggb42c = @billid where current of cur_get_each_def_2 select @remainamt = @remainamt - @billamt end else if @remainamt > @billamt / 2 begin update ggm02 set ggb41d = @currdate, ggb42c = @billid where current of cur_get_each_def_2 select @remainamt = @remainamt - @billamt end fetch cur_get_each_def_2 into @billamt end close cur_get_each_def_2 deallocate cur_get_each_def_2 end end GO /****** Object: StoredProcedure [dbo].[upg_recal_bill_sum] Script Date: 06/12/2017 06:20:31 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_recal_bill_sum] ( @compid varchar(10), -- 公司别 @enterid varchar(20) -- 入院单 ) as begin declare @total_amt float, @ggm02_total float, @ggm03_total float select @ggm02_total = 0, @ggm03_total = 0 select @ggm02_total = sum(ggb11f) from ggm02 b with (nolock), ggm01 a with (nolock) where b.ggb00c = a.gga00c and b.ggb01c = a.gga01c and a.gga30c = @enterid and a.gga00c = @compid select @ggm03_total = sum(ggc11f) from ggm03 b with (nolock), ggm01 a with (nolock) where b.ggc00c = a.gga00c and b.ggc01c = a.gga01c and a.gga30c = @enterid and a.gga00c = @compid --更新ggm01中总的合计以及glm01中总的合计 select @total_amt = isnull(@ggm02_total, 0) + isnull(@ggm03_total, 0) --如果对应多个收银单的时候可能就更新的错误了 create table #sum_bill_amt_2( billid varchar(20) not null, amt float null) create table #sum_bill_amt_3( billid varchar(20) not null, amt float null) insert #sum_bill_amt_2(billid, amt) select ggb01c, sum(ggb11f) from ggm02 b with (nolock), ggm01 a with (nolock) where b.ggb00c = a.gga00c and b.ggb01c = a.gga01c and a.gga00c = @compid and a.gga30c = @enterid group by ggb01c insert #sum_bill_amt_3(billid, amt) select ggc01c, sum(ggc11f) from ggm03 b with (nolock), ggm01 a with (nolock) where b.ggc00c = a.gga00c and b.ggc01c = a.gga01c and a.gga00c = @compid and a.gga30c = @enterid group by ggc01c --可能没有项目, 这个时候, gga16f就没有被重新设定 update a set a.gga16f = 0 from ggm01 a where a.gga00c = @compid and a.gga30c = @enterid update a set a.gga16f = isnull(b.amt, 0) from ggm01 a, #sum_bill_amt_2 b where a.gga00c = @compid and a.gga01c = b.billid update a set a.gga16f = isnull(a.gga16f, 0) + isnull(b.amt, 0) from ggm01 a, #sum_bill_amt_3 b where a.gga00c = @compid and a.gga01c = b.billid drop table #sum_bill_amt_2 drop table #sum_bill_amt_3 --可能是一个入院单对应的几个收银单的钥匙牌号有几个 update glm03 set glc03f = (select sum(isnull(gga16f, 0)) from ggm01 where gga00c = glc00c and gga08c = glc02c and gga30c = glc01c), glc04f = 0, glc06f = 0 where glc00c = @compid and glc01c = @enterid update glm01 set gla11f = @total_amt, gla20f = 0 where gla00c = @compid and gla01c = @enterid end GO /****** Object: StoredProcedure [dbo].[upg_get_service_times] Script Date: 06/12/2017 06:20:09 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_get_service_times] ( @comp varchar(10), -- 门店 @start varchar(8), -- 起始日期 @end varchar(8) -- 终止日期 ) as begin select P.employeeid, count(0) as times from (select distinct billid, employeeid from (select ggb01c as billid, ggb12c as employeeid from ggm02 a with(nolock), ggm01 b with(nolock) where b.gga00c = @comp and b.gga02d between @start and @end and a.ggb00c = b.gga00c and a.ggb01c = b.gga01c and isnull(ggb12c, '') <> '' union select ggb01c as billid, ggb13c as employeeid from ggm02 a with(nolock), ggm01 b with(nolock) where b.gga00c = @comp and b.gga02d between @start and @end and a.ggb00c = b.gga00c and a.ggb01c = b.gga01c and isnull(ggb13c, '') <> '' union select ggb01c as billid, ggb14c as employeeid from ggm02 a with(nolock), ggm01 b with(nolock) where b.gga00c = @comp and b.gga02d between @start and @end and a.ggb00c = b.gga00c and a.ggb01c = b.gga01c and isnull(ggb14c, '') <> '' union select ggb01c as billid, ggb26c as employeeid from ggm02 a with(nolock), ggm01 b with(nolock) where b.gga00c = @comp and b.gga02d between @start and @end and a.ggb00c = b.gga00c and a.ggb01c = b.gga01c and isnull(ggb26c, '') <> '' union select ggb01c as billid, ggb32c as employeeid from ggm02 a with(nolock), ggm01 b with(nolock) where b.gga00c = @comp and b.gga02d between @start and @end and a.ggb00c = b.gga00c and a.ggb01c = b.gga01c and isnull(ggb32c, '') <> '' union select ggc01c as billid, ggc12c as employeeid from ggm03 a with(nolock), ggm01 b with(nolock) where b.gga00c = @comp and b.gga02d between @start and @end and a.ggc00c = b.gga00c and a.ggc01c = b.gga01c and isnull(ggc12c, '') <> '' union select ggc01c as billid, ggc15c as employeeid from ggm03 a with(nolock), ggm01 b with(nolock) where b.gga00c = @comp and b.gga02d between @start and @end and a.ggc00c = b.gga00c and a.ggc01c = b.gga01c and isnull(ggc15c, '') <> '' union select ggc01c as billid, ggc17c as employeeid from ggm03 a with(nolock), ggm01 b with(nolock) where b.gga00c = @comp and b.gga02d between @start and @end and a.ggc00c = b.gga00c and a.ggc01c = b.gga01c and isnull(ggc17c, '') <> '') T) P group by P.employeeid order by times desc end GO /****** Object: StoredProcedure [dbo].[upg_re_updatedepart_all_must_good] Script Date: 06/12/2017 06:20:31 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_re_updatedepart_all_must_good]( @compid varchar(10), @fromdate varchar(10), @todate varchar(10)) as begin --update ggm03 --先设置产品销售的部门为空 update a set a.ggc81c='',ggc82c='',ggc83c='' from ggm03 a,ggm01 b where a.ggc00c = b.gga00c and a.ggc01c = b.gga01c and b.gga00c = @compid and b.gga02d>=@fromdate and b.gga02d<=@todate update a set a.ggc81c=b.haa06c from ggm03 a,ham01 b,ggm01 c where a.ggc00c = c.gga00c and a.ggc01c = c.gga01c and c.gga00c = @compid and c.gga02d>=@fromdate and c.gga02d<=@todate and a.ggc00c=b.haa00c and a.ggc12c=b.haa01c and isnull(ggc12c,'')<>'' update a set a.ggc82c=b.haa06c from ggm03 a ,ham01 b,ggm01 c where a.ggc00c = c.gga00c and a.ggc01c = c.gga01c and c.gga00c = @compid and c.gga02d>=@fromdate and c.gga02d<=@todate and a.ggc00c=b.haa00c and a.ggc15c=b.haa01c and isnull(ggc82c,'')='' and isnull(ggc15c,'')<>'' update a set a.ggc83c=b.haa06c from ggm03 a ,ham01 b,ggm01 c where a.ggc00c = c.gga00c and a.ggc01c = c.gga01c and c.gga00c = @compid and c.gga02d>=@fromdate and c.gga02d<=@todate and a.ggc00c=b.haa00c and a.ggc17c=b.haa01c and isnull(ggc83c,'')='' and isnull(ggc17c,'')<>'' end GO /****** Object: StoredProcedure [dbo].[upg_check_logined] Script Date: 06/12/2017 06:19:41 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_check_logined] ( @compid varchar(10), -- @noteClass varchar(5), -- 单据类别(1-入院单 2-壁球入院) @noteId varchar(20), -- 入院单号 @No varchar(20) -- 赠券编号 ) as--WUXM 2004/12/30 begin if @noteClass = 1 begin select gga25c from ggm01 with (nolock) where gga00c = @compid and gga25c = @No union select gla21c from glm01 with (nolock) where gla00c = @compid and gla21c = @No and gla01c <> @noteId end else begin select gga25c from ggm01 with (nolock) where gga00c = @compid and gga25c = @No and gga01c <> @noteId union select gla21c from glm01 with (nolock) where gla00c = @compid and gla21c = @No end end GO /****** Object: StoredProcedure [dbo].[upg_check_consume_times] Script Date: 06/12/2017 06:19:40 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_check_consume_times] ( @cardid varchar(20), @ddate varchar(10), -- 日期 @times int output ) as begin -- 需要检查做所有公司中的消费 -- 考虑到性能,访问的表最好是不常用的表,而不是热表 select @times = count(*) from ggm01 where gga09c = @cardid and gga02d = @ddate end GO /****** Object: StoredProcedure [dbo].[upg_init_gbm11] Script Date: 06/12/2017 06:20:13 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_init_gbm11] as begin if exists(select 1 from gbm11) begin return end insert gbm11(gbk01c, gbk02c) values('01', '生活起居状况') insert gbm11(gbk01c, gbk02c) values('0101', '精神情绪') insert gbm11(gbk01c, gbk02c) values('010101', '精神情绪-压力') insert gbm11(gbk01c, gbk02c) values('010102', '精神情绪-挫折') insert gbm11(gbk01c, gbk02c) values('010103', '精神情绪-紧张') insert gbm11(gbk01c, gbk02c) values('010104', '精神情绪-焦虑') insert gbm11(gbk01c, gbk02c) values('010105', '精神情绪-敏感') insert gbm11(gbk01c, gbk02c) values('010106', '精神情绪-担忧') insert gbm11(gbk01c, gbk02c) values('010107', '精神情绪-兴奋') insert gbm11(gbk01c, gbk02c) values('0102', '饮食') insert gbm11(gbk01c, gbk02c) values('010201', '饮食-规律') insert gbm11(gbk01c, gbk02c) values('010202', '饮食-烟酒') insert gbm11(gbk01c, gbk02c) values('010203', '饮食-口味重') insert gbm11(gbk01c, gbk02c) values('010204', '饮食-口味清淡') insert gbm11(gbk01c, gbk02c) values('010205', '饮食-零食') insert gbm11(gbk01c, gbk02c) values('0103', '睡眠') insert gbm11(gbk01c, gbk02c) values('010301', '睡眠-失眠') insert gbm11(gbk01c, gbk02c) values('010302', '睡眠-多梦') insert gbm11(gbk01c, gbk02c) values('010303', '睡眠-易醒') insert gbm11(gbk01c, gbk02c) values('010304', '睡眠-不足') insert gbm11(gbk01c, gbk02c) values('010305', '睡眠-正常') insert gbm11(gbk01c, gbk02c) values('0104', '运动') insert gbm11(gbk01c, gbk02c) values('010401', '运动-有-坚持') insert gbm11(gbk01c, gbk02c) values('010402', '运动-有-间断') insert gbm11(gbk01c, gbk02c) values('010403', '运动-无') insert gbm11(gbk01c, gbk02c) values('010404', '运动-剧烈运动项目') insert gbm11(gbk01c, gbk02c) values('0105', '环境') insert gbm11(gbk01c, gbk02c) values('010501', '环境-干燥') insert gbm11(gbk01c, gbk02c) values('010502', '环境-电脑') insert gbm11(gbk01c, gbk02c) values('010503', '环境-空调') insert gbm11(gbk01c, gbk02c) values('010504', '环境-污染') insert gbm11(gbk01c, gbk02c) values('0106', '女性生理') insert gbm11(gbk01c, gbk02c) values('010601', '女性生理-痛经') insert gbm11(gbk01c, gbk02c) values('010602', '女性生理-不规律') insert gbm11(gbk01c, gbk02c) values('010603', '女性生理-量多') insert gbm11(gbk01c, gbk02c) values('010604', '女性生理-量少') insert gbm11(gbk01c, gbk02c) values('010605', '女性生理-血块') insert gbm11(gbk01c, gbk02c) values('010606', '女性生理-色暗') insert gbm11(gbk01c, gbk02c) values('010607', '女性生理-炎症') insert gbm11(gbk01c, gbk02c) values('010608', '女性生理-行经期') insert gbm11(gbk01c, gbk02c) values('010609', '女性生理-末次月经') insert gbm11(gbk01c, gbk02c) values('0107', '消化系统') insert gbm11(gbk01c, gbk02c) values('010701', '消化系统-便秘') insert gbm11(gbk01c, gbk02c) values('010702', '消化系统-爱胀气') insert gbm11(gbk01c, gbk02c) values('010703', '消化系统-胃寒') insert gbm11(gbk01c, gbk02c) values('010704', '消化系统-腹泻') insert gbm11(gbk01c, gbk02c) values('010705', '消化系统-闭气') insert gbm11(gbk01c, gbk02c) values('0108', '您想改善--皮肤') insert gbm11(gbk01c, gbk02c) values('010801', '您想改善皮肤>美白') insert gbm11(gbk01c, gbk02c) values('010802', '您想改善皮肤>补水') insert gbm11(gbk01c, gbk02c) values('010803', '您想改善皮肤>抗衰老') insert gbm11(gbk01c, gbk02c) values('010804', '您想改善皮肤>改善现有面部问题') insert gbm11(gbk01c, gbk02c) values('0109', '您想改善--身体') insert gbm11(gbk01c, gbk02c) values('010901', '您想改善身体>减肥') insert gbm11(gbk01c, gbk02c) values('010902', '您想改善身体>美胸') insert gbm11(gbk01c, gbk02c) values('010903', '您想改善身体>内分秘') insert gbm11(gbk01c, gbk02c) values('010904', '您想改善身体>塑形') insert gbm11(gbk01c, gbk02c) values('0110', '调理频率') insert gbm11(gbk01c, gbk02c) values('011001', '一周/次') insert gbm11(gbk01c, gbk02c) values('011002', '二周/次') insert gbm11(gbk01c, gbk02c) values('011003', '三周/次') insert gbm11(gbk01c, gbk02c) values('011004', '不固定') insert gbm11(gbk01c, gbk02c) values('0111', '现使用护肤品') insert gbm11(gbk01c, gbk02c) values('011101', '护肤品01') insert gbm11(gbk01c, gbk02c) values('011102', '护肤品02') insert gbm11(gbk01c, gbk02c) values('011103', '护肤品03') insert gbm11(gbk01c, gbk02c) values('011104', '护肤品04') insert gbm11(gbk01c, gbk02c) values('011105', '护肤品05') insert gbm11(gbk01c, gbk02c) values('0112', '美容院曾用护肤品') insert gbm11(gbk01c, gbk02c) values('011201', '护肤品01') insert gbm11(gbk01c, gbk02c) values('011202', '护肤品02') insert gbm11(gbk01c, gbk02c) values('011203', '护肤品03') insert gbm11(gbk01c, gbk02c) values('011204', '护肤品04') insert gbm11(gbk01c, gbk02c) values('011205', '护肤品05') insert gbm11(gbk01c, gbk02c) values('02', '健康状况') insert gbm11(gbk01c, gbk02c) values('020001', '脑血管病') insert gbm11(gbk01c, gbk02c) values('020002', '心血管病') insert gbm11(gbk01c, gbk02c) values('020003', '神经衰弱') insert gbm11(gbk01c, gbk02c) values('020004', '糖尿病') insert gbm11(gbk01c, gbk02c) values('020005', '高血压') insert gbm11(gbk01c, gbk02c) values('020006', '肺炎') insert gbm11(gbk01c, gbk02c) values('020007', '高脂血症') insert gbm11(gbk01c, gbk02c) values('020008', '气管炎') insert gbm11(gbk01c, gbk02c) values('020009', '上呼吸道炎症') insert gbm11(gbk01c, gbk02c) values('020010', '消化不良') insert gbm11(gbk01c, gbk02c) values('020011', '胃炎') insert gbm11(gbk01c, gbk02c) values('020012', '溃疡病') insert gbm11(gbk01c, gbk02c) values('020013', '胆结石、胆囊炎') insert gbm11(gbk01c, gbk02c) values('020014', '肠炎') insert gbm11(gbk01c, gbk02c) values('020015', '肝炎') insert gbm11(gbk01c, gbk02c) values('020016', '泌尿系统感染') insert gbm11(gbk01c, gbk02c) values('020017', '肾炎') insert gbm11(gbk01c, gbk02c) values('020018', '肾结石、囊肿') insert gbm11(gbk01c, gbk02c) values('020019', '性功能障碍') insert gbm11(gbk01c, gbk02c) values('020020', '月经不调') insert gbm11(gbk01c, gbk02c) values('020021', '子宫肌瘤') insert gbm11(gbk01c, gbk02c) values('020022', '卵巢疾病') insert gbm11(gbk01c, gbk02c) values('020023', '甲亢') insert gbm11(gbk01c, gbk02c) values('020024', '甲低') insert gbm11(gbk01c, gbk02c) values('020025', '手术史') insert gbm11(gbk01c, gbk02c) values('020026', '多囊卵巢综合疹') insert gbm11(gbk01c, gbk02c) values('020027', '前列腺增生、炎症') insert gbm11(gbk01c, gbk02c) values('020028', '结核') insert gbm11(gbk01c, gbk02c) values('020029', '皮肤病') insert gbm11(gbk01c, gbk02c) values('020030', '关节炎') insert gbm11(gbk01c, gbk02c) values('020031', '肿瘤') insert gbm11(gbk01c, gbk02c) values('020032', '情绪易怒') insert gbm11(gbk01c, gbk02c) values('020033', '精神压力大') insert gbm11(gbk01c, gbk02c) values('020034', '烦躁') insert gbm11(gbk01c, gbk02c) values('020035', '健忘') insert gbm11(gbk01c, gbk02c) values('020036', '心理障碍') insert gbm11(gbk01c, gbk02c) values('03', '用药史') insert gbm11(gbk01c, gbk02c) values('030001', '抗菌素') insert gbm11(gbk01c, gbk02c) values('030002', '心血管药') insert gbm11(gbk01c, gbk02c) values('030003', '降压药') insert gbm11(gbk01c, gbk02c) values('030004', '感冒药') insert gbm11(gbk01c, gbk02c) values('030005', '安眠药') insert gbm11(gbk01c, gbk02c) values('030006', '补品') insert gbm11(gbk01c, gbk02c) values('030007', '激素类') insert gbm11(gbk01c, gbk02c) values('030008', '抗过敏类') insert gbm11(gbk01c, gbk02c) values('030009', '抗肿瘤类') insert gbm11(gbk01c, gbk02c) values('030010', '减肥药') insert gbm11(gbk01c, gbk02c) values('030011', '避孕药') insert gbm11(gbk01c, gbk02c) values('030012', '安眠药') insert gbm11(gbk01c, gbk02c) values('030013', '皮肤用药') insert gbm11(gbk01c, gbk02c) values('04', '家族疾病史') insert gbm11(gbk01c, gbk02c) values('040001', '心脏病') insert gbm11(gbk01c, gbk02c) values('040002', '高血压') insert gbm11(gbk01c, gbk02c) values('040003', '脑血管病') insert gbm11(gbk01c, gbk02c) values('040004', '糖尿病') insert gbm11(gbk01c, gbk02c) values('040005', '高血脂') insert gbm11(gbk01c, gbk02c) values('040006', '良、恶性肿瘤') insert gbm11(gbk01c, gbk02c) values('040007', '肥胖病') insert gbm11(gbk01c, gbk02c) values('040008', '痤疮') insert gbm11(gbk01c, gbk02c) values('040009', '精神疾病') insert gbm11(gbk01c, gbk02c) values('040010', '结核病') insert gbm11(gbk01c, gbk02c) values('040011', '风湿病') insert gbm11(gbk01c, gbk02c) values('040012', '皮肤病') insert gbm11(gbk01c, gbk02c) values('05', '过敏史') insert gbm11(gbk01c, gbk02c) values('0501', '药物') insert gbm11(gbk01c, gbk02c) values('050101', '药物-青霉素类') insert gbm11(gbk01c, gbk02c) values('050102', '药物-磺胺类') insert gbm11(gbk01c, gbk02c) values('0502', '食物') insert gbm11(gbk01c, gbk02c) values('050203', '食物-海鲜类') insert gbm11(gbk01c, gbk02c) values('050204', '食物-肉类') insert gbm11(gbk01c, gbk02c) values('050205', '食物-蔬菜类') insert gbm11(gbk01c, gbk02c) values('0503', '环境') insert gbm11(gbk01c, gbk02c) values('050306', '环境-粉尘') insert gbm11(gbk01c, gbk02c) values('050307', '环境-毛屑') end GO /****** Object: StoredProcedure [dbo].[upg_confirm_gum02] Script Date: 06/12/2017 06:19:45 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_confirm_gum02] ( @compid varchar(10), @billid varchar(20), --异动单号 @money float, --异动金额 @last float, --上次余额 @new float, --账户最新余额 @branchid varchar(10), --分店号 @account varchar(20), --分店账户 @userid varchar(20), --复核人 @date varchar(8), --复核日期 @confirm int ) as begin if (@confirm = 1) begin -- 复核人, 复核时间 update gum02 set gub93c = @userid, gub94d = @date where gub00c = @compid and gub01c = @billid -- 更新账户的最新余额 update gum01 set gua03f = @new, gua04f = @last, gua05d = @date where gua00c = @compid and gua01c = @branchid and gua02c = @account end if (@confirm = 0) begin -- 复核人, 复核时间 update gum02 set gub93c = '', gub94d = '' where gub00c = @compid and gub01c = @billid -- 更新账户的余额 update gum01 set gua03f = @last, gua04f = @new, gua05d = @date where gua00c = @compid and gua01c = @branchid and gua02c = @account end end GO /****** Object: StoredProcedure [dbo].[upg_consume_statistic_all] Script Date: 06/12/2017 06:19:45 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_consume_statistic_all] ( @compid varchar(10), @fromdate varchar(8), -- 开始时间 @todate varchar(8), -- 截止时间 @type int, -- 1-本公司的卡在别的店消费,0-别的公司的卡在本公司消费 @fromcard varchar(20), @tocard varchar(20), @inc_goods int, -- 1列印,0不列印 @inc_project int, -- 1列印,0不列印 @cardtype varchar(10), -- 会员卡类别 @inc_fill int, -- 1列印,0不列印 --add by lyj 打印充值 @payway varchar(10), -- 添加过滤支付方式的条件 *--所有卡支付方式 ALL-所有支付方式 或指定一个支付方式 @prj_from varchar(20), -- 项目统计分类 @prj_to varchar(20) -- 项目统计分类 ) as begin -- 因java请求无法识别WWQ = #,WWR = $, WWE = & exec upg_paymode_to_java @payway,@payway output -- 先把相关的单据找出来 create table #tbl_over_bills ( idd int identity not null, con_compid varchar(10) not null, -- 消费的公司 billtype varchar(10) not null, -- 单据类别 billid varchar(20) not null, -- 单号 bel_compid varchar(10) null, -- 卡的归属公司 cardid varchar(20) null, -- 卡号 type int null, -- 0-别的公司的卡在本公司的消费 1-本公司的卡在别的公司的消费 cardtype varchar(20) null, -- 卡类别 billdate varchar(20) null, gcl06i int null, primary key(idd) ) create table #result ( idd int identity not null, comp varchar(10) null, --公司别, 当是别公司来本店消费时,为 卡的归属公司,若为去别的公司消费,则为 消费公司 cardno varchar(20) null, --会员卡号 billno varchar(20) null, --消费单号 billdate varchar(8) null, --消费日期 prj_goods varchar(20) null, --消费项目或套餐或产品 payway varchar(20) null, --支付方式 price float null, --单价 num float null, --数量 znum float null, --赠送数量 --add by lyj amt float null, --金额 type varchar(10) null, --类型,1-本公司的卡在别的店消费,0-别的公司的卡在本公司消费 consume varchar(10) null, --消费类型 1-项目 2-产品 3-充值 4-套餐 5-疗程 class varchar(10) null, --会员卡类别 samt float null, --实际营业额 primary key(idd) ) if @type = 0 begin -- 别的公司的卡在本公司的消费 insert #tbl_over_bills(con_compid, billtype, billid, bel_compid, cardid, type, cardtype, billdate) select gga00c, 'gx', gga01c, isnull(a.gga32c,b.gca13d), gga08c, @type, gca02c, gga80d from ggm01 a with (nolock), gcm01 b with (nolock) where a.gga80d >= @fromdate and a.gga80d <= @todate and a.gga08c = b.gca01c and b.gca00c = b.gca13d and a.gga00c = @compid --and b.gca13d <> @compid and isnull(a.gga32c,'') <> '' --lucz 2015.10.28 and a.gga32c <> @compid and substring(b.gca01c, 1, 2) <> '散客' union all select gga00c, 'gx', gga01c, isnull(a.gga32c,b.gca13d), gga08c, @type, gca02c, gga80d from ggm01 a with (nolock), gcm01 b with (nolock) where a.gga80d >= @fromdate and a.gga80d <= @todate and a.gga08c = b.gca01c and b.gca00c = b.gca13d and a.gga00c = @compid --and b.gca13d <> @compid and isnull(a.gga32c,'') = '' --lucz 2015.10.28 and b.gca13d <> @compid and substring(b.gca01c, 1, 2) <> '散客' insert #tbl_over_bills(con_compid, billtype, billid, bel_compid, cardid, type, cardtype, billdate, gcl06i) select gcl00c, 'gz', gcl01c, isnull(a.gcl110c,b.gca13d), gcl03c, @type, gca02c, gcl80d, gcl06i from gcm10 a with (nolock), gcm01 b with (nolock) where a.gcl80d >= @fromdate and a.gcl80d <= @todate and a.gcl03c = b.gca01c and b.gca00c = b.gca13d and a.gcl00c = @compid --and b.gca13d <> @compid and isnull(a.gcl110c,b.gca13d) <> @compid --lucz 2015.10.28 and substring(b.gca01c, 1, 2) <> '散客' end else begin -- 我的卡在别的公司的消费 insert #tbl_over_bills(con_compid, billtype, billid, bel_compid, cardid, type, cardtype, billdate) select gga00c, 'gx', gga01c, gca13d, gga08c, @type, gca02c, gga80d from ggm01 a with (nolock), gcm01 b with (nolock) where a.gga80d >= @fromdate and a.gga80d <= @todate and a.gga08c = b.gca01c and b.gca00c = b.gca13d and a.gga00c <> @compid --and b.gca13d = @compid and isnull(a.gga32c,'') <> '' --lucz 2015.10.28 and a.gga32c <> @compid and substring(b.gca01c, 1, 2) <> '散客' union all select gga00c, 'gx', gga01c, gca13d, gga08c, @type, gca02c, gga80d from ggm01 a with (nolock), gcm01 b with (nolock) where a.gga80d >= @fromdate and a.gga80d <= @todate and a.gga08c = b.gca01c and b.gca00c = b.gca13d and a.gga00c <> @compid --and b.gca13d = @compid and isnull(a.gga32c,'') = '' --lucz 2015.10.28 and b.gca13d <> @compid and substring(b.gca01c, 1, 2) <> '散客' insert #tbl_over_bills(con_compid, billtype, billid, bel_compid, cardid, type, cardtype, billdate, gcl06i) select gcl00c, 'gz', gcl01c, gca13d, gcl03c, @type, gca02c, gcl80d, gcl06i from gcm10 a with (nolock), gcm01 b with (nolock) where a.gcl80d >= @fromdate and a.gcl80d <= @todate and a.gcl03c = b.gca01c and b.gca00c = b.gca13d and a.gcl00c <> @compid --and b.gca13d = @compid and isnull(a.gcl110c,b.gca13d) = @compid --lucz 2015.10.28 and substring(b.gca01c, 1, 2) <> '散客' end --如果单据归属门店为空值则去卡资料的归属门店 --lm 2016.06.21 update a set a.bel_compid = b.gca13d from #tbl_over_bills a, gcm01 b where a.con_compid = b.gca00c and a.cardid = b.gca01c and b.gca00c = b.gca13d and a.bel_compid = '' -- 把条件中的卡类别以及卡过滤条件生效 if @cardtype <> '*' begin delete #tbl_over_bills where cardtype <> @cardtype end if @fromcard <> '*' begin delete #tbl_over_bills where cardid < @fromcard or cardid > @tocard end -- 到此留下了合适的单据集合 -- 项目消费 if (@inc_project = 1) begin insert #result(comp, cardno, billno, billdate, prj_goods, payway, price, num, amt, type, consume, class) select case when type = 0 then bel_compid else con_compid end, cardid, billid, billdate, ggb03c, ggb27c, ggb10f, ggb05f, ggb11f, type, '1', cardtype from #tbl_over_bills a, ggm02 b with (nolock), gdm01 c with(nolock) where a.billtype = 'gx' and a.billid = b.ggb01c and a.con_compid = b.ggb00c and b.ggb00c = c.gda00c and b.ggb03c = c.gda01c and ((c.gda13c >= @prj_from and c.gda13c <= @prj_to) or @prj_from = '*') end -- 产品消费 if (@inc_goods = 1) begin insert #result(comp, cardno, billno, billdate, prj_goods, payway, price, num, amt, type, consume, class) select case when type = 0 then bel_compid else con_compid end, cardid, billid, billdate, ggc03c, ggc27c, ggc10f, ggc05f, ggc11f, type, '2', cardtype from #tbl_over_bills a, ggm03 b with (nolock) where a.billtype = 'gx' and a.billid = b.ggc01c and a.con_compid = b.ggc00c end if (@inc_fill = 1) begin insert #result(comp, cardno, billno, billdate, prj_goods, payway, price, num, znum, amt, type, consume, class) select case when type = 0 then bel_compid else con_compid end, cardid, billid, billdate, gcm02c, gcm15c, gcm16f, isnull(gcm03f, 0), isnull(gcm04f, 0), gcm16f, type, '5', cardtype from #tbl_over_bills a, gcm11 b with (nolock) where a.con_compid = b.gcm00c and a.billid = b.gcm01c and a.billtype = 'gz' and a.gcl06i = 0 and isnull(gcm15c, '') <> '' insert #result(comp, cardno, billno, billdate, prj_goods, payway, price, num, znum, amt, type, consume, class) select case when type = 0 then bel_compid else con_compid end, cardid, billid, billdate, gcm02c, gcm17c, gcm18f, isnull(gcm03f, 0), isnull(gcm04f, 0), gcm18f, type, '5', cardtype from #tbl_over_bills a, gcm11 b with (nolock) where a.con_compid = b.gcm00c and a.billid = b.gcm01c and a.billtype = 'gz' and a.gcl06i = 0 and isnull(gcm17c, '') <> '' insert #result(comp, cardno, billno, billdate, prj_goods, payway, price, num, znum, amt, type, consume, class) select case when type = 0 then bel_compid else con_compid end, cardid, billid, billdate, gcm02c, gcm19c, gcm20f, isnull(gcm03f, 0), isnull(gcm04f, 0), gcm20f, type, '5', cardtype from #tbl_over_bills a, gcm11 b with (nolock) where a.con_compid = b.gcm00c and a.billid = b.gcm01c and a.billtype = 'gz' and a.gcl06i = 0 and isnull(gcm19c, '') <> '' insert #result(comp, cardno, billno, billdate, prj_goods, payway, price, num, znum, amt, type, consume, class) select case when type = 0 then bel_compid else con_compid end, cardid, billid, billdate, gcm02c, gcm23c, gcm24f, isnull(gcm03f, 0), isnull(gcm04f, 0), gcm24f, type, '5', cardtype from #tbl_over_bills a, gcm11 b with (nolock) where a.con_compid = b.gcm00c and a.billid = b.gcm01c and a.billtype = 'gz' and a.gcl06i = 0 and isnull(gcm23c, '') <> '' -- 套餐的部分 insert #result(comp, cardno, billno, billdate, prj_goods, payway, price, num, znum, amt, type, consume, class) select case when type = 0 then bel_compid else con_compid end, cardid, billid, billdate, gth02c, gth09c, gth10f, isnull(gth04f, 0), 0, gth10f, type, '4', cardtype from #tbl_over_bills a, gtc06 b with (nolock) where a.con_compid = b.gth00c and a.billid = b.gth01c and a.billtype = 'gz' and a.gcl06i = 0 and isnull(gth09c, '') <> '' insert #result(comp, cardno, billno, billdate, prj_goods, payway, price, num, znum, amt, type, consume, class) select case when type = 0 then bel_compid else con_compid end, cardid, billid, billdate, gth02c, gth11c, gth12f, isnull(gth04f, 0), 0, gth12f, type, '4', cardtype from #tbl_over_bills a, gtc06 b with (nolock) where a.con_compid = b.gth00c and a.billid = b.gth01c and a.billtype = 'gz' and a.gcl06i = 0 and isnull(gth11c, '') <> '' insert #result(comp, cardno, billno, billdate, prj_goods, payway, price, num, znum, amt, type, consume, class) select case when type = 0 then bel_compid else con_compid end, cardid, billid, billdate, gth02c, gth13c, gth14f, isnull(gth04f, 0), 0, gth14f, type, '4', cardtype from #tbl_over_bills a, gtc06 b with (nolock) where a.con_compid = b.gth00c and a.billid = b.gth01c and a.billtype = 'gz' and a.gcl06i = 0 and isnull(gth13c, '') <> '' insert #result(comp, cardno, billno, billdate, prj_goods, payway, price, num, znum, amt, type, consume, class) select case when type = 0 then bel_compid else con_compid end, cardid, billid, billdate, gth02c, gth15c, gth16f, isnull(gth04f, 0), 0, gth16f, type, '4', cardtype from #tbl_over_bills a, gtc06 b with (nolock) where a.con_compid = b.gth00c and a.billid = b.gth01c and a.billtype = 'gz' and a.gcl06i = 0 and isnull(gth15c, '') <> '' -- 更新套餐的名称 update a set a.prj_goods = b.gta02c from #result a, gtc01 b with (nolock) where a.comp = b.gta00c and a.prj_goods = b.gta01c and a.consume = '4' -- 只充钱 insert #result(comp, cardno, billno, billdate, prj_goods, payway, price, num, znum, amt, type, consume, class) select case when type = 0 then bel_compid else con_compid end, cardid, billid, billdate, '充值', gcl67c, gcl68f, 0, 0, gcl68f, type, '3', cardtype from #tbl_over_bills b, gcm10 a with (nolock) where b.con_compid = a.gcl00c and b.billid = a.gcl01c and b.billtype = 'gz' and a.gcl06i <> 5 and isnull(gcl67c, '') <> '' insert #result(comp, cardno, billno, billdate, prj_goods, payway, price, num, znum, amt, type, consume, class) select case when type = 0 then bel_compid else con_compid end, cardid, billid, billdate, '充值', gcl69c, gcl70f, 0, 0, gcl70f, type, '3', cardtype from #tbl_over_bills b, gcm10 a with (nolock) where b.con_compid = a.gcl00c and b.billid = a.gcl01c and b.billtype = 'gz' and a.gcl06i <> 5 and isnull(gcl69c, '') <> '' insert #result(comp, cardno, billno, billdate, prj_goods, payway, price, num, znum, amt, type, consume, class) select case when type = 0 then bel_compid else con_compid end, cardid, billid, billdate, '充值', gcl71c, gcl72f, 0, 0, gcl72f, type, '3', cardtype from #tbl_over_bills b, gcm10 a with (nolock) where b.con_compid = a.gcl00c and b.billid = a.gcl01c and b.billtype = 'gz' and a.gcl06i <> 5 and isnull(gcl71c, '') <> '' insert #result(comp, cardno, billno, billdate, prj_goods, payway, price, num, znum, amt, type, consume, class) select case when type = 0 then bel_compid else con_compid end, cardid, billid, billdate, '充值', gcl73c, gcl74f, 0, 0, gcl74f, type, '3', cardtype from #tbl_over_bills b, gcm10 a with (nolock) where b.con_compid = a.gcl00c and b.billid = a.gcl01c and b.billtype = 'gz' and a.gcl06i <> 5 and isnull(gcl73c, '') <> '' -- 欠款部分 insert #result(comp, cardno, billno, billdate, prj_goods, payway, price, num, znum, amt, type, consume, class) select case when type = 0 then bel_compid else con_compid end, cardid, billid, billdate, '充值', gcl67c, 0 - gcl68f, 0, 0, 0 - gcl68f, type, '3', cardtype from #tbl_over_bills b, gcm10 a with (nolock) where b.con_compid = a.gcl00c and b.billid = a.gcl01c and b.billtype = 'gz' and a.gcl06i = 5 and isnull(gcl67c, '') <> '' insert #result(comp, cardno, billno, billdate, prj_goods, payway, price, num, znum, amt, type, consume, class) select case when type = 0 then bel_compid else con_compid end, cardid, billid, billdate, '充值', gcl69c, 0 - gcl70f, 0, 0, 0 - gcl70f, type, '3', cardtype from #tbl_over_bills b, gcm10 a with (nolock) where b.con_compid = a.gcl00c and b.billid = a.gcl01c and b.billtype = 'gz' and a.gcl06i = 5 and isnull(gcl69c, '') <> '' insert #result(comp, cardno, billno, billdate, prj_goods, payway, price, num, znum, amt, type, consume, class) select case when type = 0 then bel_compid else con_compid end, cardid, billid, billdate, '充值', gcl71c, 0 - gcl72f, 0, 0, 0 - gcl72f, type, '3', cardtype from #tbl_over_bills b, gcm10 a with (nolock) where b.con_compid = a.gcl00c and b.billid = a.gcl01c and b.billtype = 'gz' and a.gcl06i = 5 and isnull(gcl71c, '') <> '' insert #result(comp, cardno, billno, billdate, prj_goods, payway, price, num, znum, amt, type, consume, class) select case when type = 0 then bel_compid else con_compid end, cardid, billid, billdate, '充值', gcl73c, 0 - gcl74f, 0, 0, 0 - gcl74f, type, '3', cardtype from #tbl_over_bills b, gcm10 a with (nolock) where b.con_compid = a.gcl00c and b.billid = a.gcl01c and b.billtype = 'gz' and a.gcl06i = 5 and isnull(gcl73c, '') <> '' end if isnull(@payway, '') <> 'ALL' -- 不是所有的支付方式 begin if isnull(@payway, '') = '*' begin -- 只保留与卡支付有关的 delete #result where (payway = '0' or payway = '1' or payway = '3' or payway = '6' or payway like '$%') and (consume = '1' or consume = '2') end else begin delete #result where payway <> @payway end end -- 折算实际营业额 update a set a.samt = isnull(a.amt * isnull(b.gso03i, 1), 0) from #result a, gsm15 b with (nolock) where a.comp = b.gso00c and a.payway = b.gso02c and a.consume = '1' and a.payway <> '4' update a set a.samt = isnull(a.amt * isnull(c.gak22f, 1), 0) from #result a, gam10 c with (nolock) where a.comp = c.gak00c and a.class = c.gak01c and consume = '1' and a.payway = '4' update a set a.samt = isnull(a.amt * isnull(b.gso06f, 1), 0) from #result a, gsm15 b with (nolock) where a.comp = b.gso00c and a.payway = b.gso02c and a.consume = '2' and a.payway <> '4' update a set a.samt = isnull(a.amt * isnull(c.gak24f, 0), 0) from #result a, gam10 c with (nolock) where a.comp = c.gak00c and a.class = c.gak01c and consume = '2' and a.payway = '4' update a set a.samt = isnull(a.amt * isnull(b.gso05f, 1), 0) from #result a, gsm15 b with (nolock) where a.comp = b.gso00c and a.payway = b.gso02c and a.consume = '3' update a set a.samt = isnull(a.amt * isnull(b.gso05f, 1), 0) from #result a, gsm15 b with (nolock) where a.comp = b.gso00c and a.payway = b.gso02c and a.consume = '4' update a set a.samt = isnull(a.amt * isnull(b.gso05f, 1), 0) from #result a, gsm15 b with (nolock) where a.comp = b.gso00c and a.payway = b.gso02c and a.consume = '5' if (@type = 1) select isnull(comp,'') comp, isnull(cardno,'') cardno, isnull(billno,'') billno, isnull(billdate,'') billdate, isnull(prj_goods,'') prj_goods, isnull(payway,'') payway, isnull(convert(varchar(10), price),'') as price, isnull(convert(varchar(10), num),'') as num, isnull(znum, 0) as znum, isnull(convert(varchar(10), amt),'') as amt , isnull(type,'') type, isnull(consume,'') consume, isnull(class,'') class, isnull(convert(varchar(10), samt),'') as samt from #result where type = '1' order by comp, class, cardno else if (@type = 0) select isnull(comp,'') comp, isnull(cardno,'') cardno, isnull(billno,'') billno, isnull(billdate,'') billdate, isnull(prj_goods,'') prj_goods, isnull(payway,'') payway, isnull(convert(varchar(10), price),'') as price, isnull(convert(varchar(10), num),'') as num, isnull(znum, 0) as znum, isnull(convert(varchar(10), amt),'') as amt , isnull(type,'') type, isnull(consume,'') consume, isnull(class,'') class, isnull(convert(varchar(10), samt),'') as samt from #result where type = '0' order by comp, class, cardno drop table #result drop table #tbl_over_bills end GO /****** Object: StoredProcedure [dbo].[upg_personal_statistics_paymode_ex_02] Script Date: 06/12/2017 06:20:27 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_personal_statistics_paymode_ex_02] ( @compid varchar(10), -- 公司别 @fromdate varchar(10), -- 开始日期 @todate varchar(10), -- 截至日期 @fromempl varchar(20), -- 查询开始人员 @toempl varchar(20), -- 查询截至人员 @inc_card int, -- 卡销售 @inc_service int, -- 服务 @inc_goods int, -- 是否包括产品销售 @return_type int, @paymode varchar(20), @cardtype varchar(5), @spid int ) -- return 数据的类型, 1-明细数据员工业绩统计报表 -- 2-每人一个提成数据, 用于计算员工薪资 -- 3-业绩排行-虚业绩, 4-业绩排行-营业金额 -- 5-业绩排行-实业绩 -- 7-每人按项目分组 6-总提成 as begin -- 因java请求无法识别WWQ = #,WWR = $, WWE = & exec upg_paymode_to_java @paymode,@paymode output create table #empl_stat_outer ( id int identity, -- 流水号 person_id varchar(30) not null, -- 员工代码 comp_ascription varchar(10) null, -- 公司别(员工) action_id tinyint not null, -- 类别, 1-卡销售 2-指定大工, 3-轮班大工, 4-补工大工, -- 5-指定中工, 6-轮班中工 -- 7-指定小工, 8-轮班小工, 9-产品销售 10,11-四工 -- 0 卡充值 srvdate varchar(10) not null, -- 日期 code varchar(60) not null, -- 项目代码, 或是卡号, 产品码 name varchar(200) null, -- 名称 payway varchar(20) null, -- 支付方式 amt float null, -- 营业金额 quan float null, -- 数量 cost float null, -- 成本 comm float null, -- 提成 amt2 float null, -- 虚业绩 amt3 float null, -- 真实业绩 cardrate float null, -- 转卡比率 stdprice float null, -- 标准单价 prj_type varchar(20) null, -- 项目类别 share_rate float default 1 null, --分享比率 cls_flag int null, -- 1:项目 2:产品 3:卡 gender int null, -- 性别 0:女 1:男 billid varchar(30) null, -- 单号 paycode varchar(20) null, -- 支付代码 ex_rate float null, -- 实际营业额折算比率 ggm02_seq float null, -- ggm02中的序列号, 与单号一起来标志一个项目 room_id varchar(60) null, -- 房间号,只在spa或足浴版本需要 cardid varchar(60) null, -- 会员卡号 memid varchar(60) null, -- 会员编号 sumMoney float null, -- 会员卡销售总金额 compid varchar(30) null ) create clustered index idx_empl_stat_outer_01 on #empl_stat_outer(person_id, prj_type) -- 子门店业绩数据循环放到upg_personal_statistics中了 insert #empl_stat_outer(person_id, comp_ascription, action_id, srvdate, code, name, payway, amt, quan, cost, comm, amt2, amt3, cardrate, stdprice, prj_type, share_rate, cls_flag, gender, billid, paycode, ex_rate, ggm02_seq, room_id, cardid, memid) exec upg_personal_statistics @compid, @fromdate, @todate, @fromempl, @toempl, @inc_card, @inc_service, @inc_goods, @return_type, @spid create table #items_comm ( id int identity(1,1) not null, person_id varchar(30) not null,---员工编号 comp_ascription varchar(10) null, -- 公司别(员工) code varchar(20) not null,---项目编号或产品编号,卡号 name varchar(60) null, ---名称 cls_flag float null,---类型:1.表示项目,2.表示产品,3.卡销售,4.卡充值 prj_type varchar(20) null, -- 项目类别 quan float null,--数量 amt float null,---金额 amt2 float null,---虚业绩 amt3 float null,---真实业绩 zItems float null,---指定数 zmoney float null,---指定金额 zamt2 float null,---指定虚业绩 zamt3 float null,---指定实业绩 add by leiji on 2012年9月27日 lItem float null,---轮牌数 lmoney float null,---轮牌金额 lamt2 float null,---轮牌虚业绩 lamt3 float null,---轮牌实业绩 comm float null,---提成 clientSum float null, ---总客数 clientz float null,---指定客数 clientl float null,---轮牌客数 ) ----用来计算客数 create table #client ( id int identity not null, clientbill varchar(20) not null, appoint float, ring float, CONSTRAINT Pk_client_id PRIMARY KEY (id,clientbill) ) create table #client1 ( id int identity not null, clientbill varchar(20) not null, appoint float, ring float, CONSTRAINT Pk_client1_id PRIMARY KEY (id,clientbill) ) create table #itemSum ( id int identity(1,1) not null, person_id varchar(20) not null, itemid varchar(60) not null, prj_type varchar(20) null, -- 项目类别 lsum float null, lmoney float null,---轮牌金额 lamt2 float null,---轮牌虚业绩 lamt3 float null,---轮牌实业绩 zsum float null, zmoney float null,---指定金额 zamt2 float null,---指定虚业绩 zamt3 float null,---指定实业绩 CONSTRAINT Pk_itemsSum_id PRIMARY KEY (id,person_id,itemid) ) create table #itemSum1 ( id int identity(1,1) not null, person_id varchar(30)not null, itemid varchar(60) not null, prj_type varchar(20) null, -- 项目类别 lsum float null, lmoney float null,---轮牌金额 lamt2 float null,---轮牌虚业绩 lamt3 float null,---轮牌实业绩 zsum float null, zmoney float null,---指定金额 zamt2 float null,---指定虚业绩 zamt3 float null,---指定实业绩 CONSTRAINT Pk_itemSum1_id PRIMARY KEY (id,person_id,itemid) ) declare @clientSum float ---总客数 declare @clientz float ---指定客数 declare @clientl float ---轮牌客数 insert into #client(clientbill,appoint,ring) select distinct billid,1,0 from #empl_stat_outer where (action_id = '10' or action_id = '7' or action_id = '5' or action_id = '2') --指定 group by billid insert into #client(clientbill,ring,appoint) select distinct billid,1,0 from #empl_stat_outer where (action_id = '11' or action_id = '8' or action_id = '6' or action_id = '3') -- 轮班 group by billid update a set a.ring = b.ring from #client a , #client b where a.clientbill = b.clientbill and a.id<>b.id and isnull(a.ring,0)=0 update a set a.appoint = b.appoint from #client a , #client b where a.clientbill = b.clientbill and a.id<>b.id and isnull(a.appoint,0)=0 delete a from #client a , #client b where a.clientbill = b.clientbill and a.id>b.id update #client set ring=0 where appoint<>0 select @clientSum=count(clientbill) from #client select @clientz=sum(appoint) from #client select @clientl=sum(ring) from #client insert into #items_comm(person_id,code,prj_type,comp_ascription,name,cls_flag,quan,amt,amt2,amt3,comm,zItems,lItem,clientSum,clientz,clientl) select person_id,code,prj_type,comp_ascription,max(name),1,isnull(sum(quan),0),isnull(sum(amt),0),isnull(sum(amt2),0),isnull(sum(amt3),0),isnull(sum(comm),0),0,0,0,0,0 from #empl_stat_outer where cls_flag = '1'-- 项目 group by code,prj_type,person_id,comp_ascription insert #itemSum(person_id,prj_type,itemid,lsum,zsum,lmoney,lamt2,lamt3,zmoney,zamt2,zamt3) --轮班 select person_id,prj_type,code,sum(quan),0,isnull(sum(amt),0),isnull(sum(amt2),0),isnull(sum(amt3),0),0,0,0 from #empl_stat_outer where (action_id = '11' or action_id = '8' or action_id = '6' or action_id = '3') -- 轮班 group by code,person_id,prj_type insert #itemSum(person_id,prj_type,itemid,zsum,lsum,lmoney,lamt2,lamt3,zmoney,zamt2,zamt3) --指定 select person_id,prj_type,code,sum(quan),0,0,0,0,isnull(sum(amt),0),isnull(sum(amt2),0),isnull(sum(amt3),0) from #empl_stat_outer where (action_id = '10' or action_id = '7' or action_id = '5' or action_id = '2') --指定 group by code,person_id,prj_type insert into #itemSum1(person_id,prj_type,itemid,zsum,lsum,lmoney,lamt2,lamt3,zmoney,zamt2,zamt3) select person_id,prj_type,itemid,isnull(sum(zsum),0),isnull(sum(lsum),0),isnull(sum(lmoney),0),isnull(sum(lamt2),0),isnull(sum(lamt3),0),isnull(sum(zmoney),0),isnull(sum(zamt2),0),isnull(sum(zamt3),0) from #itemSum group by person_id,itemid ,prj_type delete #itemSum insert into #itemSum(person_id,itemid,zsum,lsum,lmoney,lamt2,lamt3,zmoney,zamt2,zamt3) select person_id,itemid,zsum,lsum,lmoney,lamt2,lamt3,zmoney,zamt2,zamt3 from #itemSum1 update a set a.lItem=isnull(b.lsum,0), a.zItems=isnull(b.zsum,0), a.lmoney=isnull(b.lmoney,0), a.lamt3=isnull(b.lamt3,0), a.lamt2=isnull(b.lamt2,0), a.zmoney=isnull(b.zmoney,0), a.zamt2=isnull(b.zamt2,0), a.zamt3=isnull(b.zamt3,0) from #items_comm a,#itemSum b where a.code=b.itemid and a.person_id=b.person_id update #items_comm set clientSum = @clientSum, clientz = @clientz, clientl = @clientl --产品 insert into #items_comm(person_id, code,prj_type,comp_ascription,name, cls_flag, quan, amt,amt2,amt3,comm,zItems,lItem,clientSum,clientz,clientl) select person_id,code, prj_type ,comp_ascription,max(name),2, isnull(sum(quan),0),isnull(sum(amt),0),isnull(sum(amt2),0),isnull(sum(amt3),0),isnull(sum(comm),0),0,0,0,0,0 from #empl_stat_outer where cls_flag = '2' group by person_id,code, prj_type,comp_ascription --卡充值 insert into #items_comm(person_id,code,prj_type,comp_ascription,name,cls_flag,quan,amt,amt2,amt3,comm,zItems,lItem,clientSum,clientz,clientl) select person_id,'',prj_type,comp_ascription,max(name),3,isnull(sum(quan),0),isnull(sum(amt),0),isnull(sum(amt2),0),isnull(sum(amt3),0),isnull(sum(comm),0),0,0,0,0,0 from #empl_stat_outer where cls_flag = '3' and action_id = '0' group by person_id, prj_type,comp_ascription --卡销售 insert into #items_comm(person_id,code,prj_type,comp_ascription,name,cls_flag,quan,amt,amt2,amt3,comm,zItems,lItem,clientSum,clientz,clientl) select person_id,'',prj_type,comp_ascription,max(name),4,isnull(sum(quan),0),isnull(sum(amt),0),isnull(sum(amt2),0),isnull(sum(amt3),0),isnull(sum(comm),0),0,0,0,0,0 from #empl_stat_outer where cls_flag = '3' and action_id = '1' group by person_id, prj_type,comp_ascription select id, isnull(person_id,'') person_id,---员工编号 isnull(comp_ascription,'') comp_ascription, -- 公司别(员工) isnull(code,'') code,---项目编号或产品编号,卡号 isnull(name,'') name, ---名称 isnull(cls_flag,'') cls_flag,---类型:1.表示项目,2.表示产品,3.卡销售,4.卡充值 isnull(prj_type,'') prj_type, -- 项目类别 isnull(quan,0) quan,--数量 isnull(amt,0) amt,---金额 isnull(amt2,0) amt2,---虚业绩 isnull(amt3,0) amt3,---真实业绩 isnull(zItems,0) zItems,---指定数 isnull(zmoney,0) zmoney,---指定金额 isnull(zamt2,0) zamt2,---指定虚业绩 isnull(zamt3,0) zamt3,---指定实业绩 add by leiji on 2012年9月27日 isnull(lItem,0) lItem,---轮牌数 isnull(lmoney,0) lmoney,---轮牌金额 isnull(lamt2,0) lamt2,---轮牌虚业绩 isnull(lamt3,0) lamt3,---轮牌实业绩 isnull(comm,0) comm,---提成 isnull(clientSum,0) clientSum, ---总客数 isnull(clientz,0) clientz,---指定客数 isnull(clientl,0) clientl ---轮牌客数 from #items_comm order by person_id,cls_flag,prj_type drop table #items_comm drop table #client drop table #client1 drop table #itemSum drop table #itemSum1 drop table #empl_stat_outer end GO /****** Object: StoredProcedure [dbo].[upg_insert_gym_message] Script Date: 06/12/2017 06:20:16 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_insert_gym_message] as begin truncate table messages insert messages values('系统公用','1','您是否确定删除该笔资料') insert messages values('系统公用','2','资料已修改, 是否存档') insert messages values('系统公用','3','该笔资料未存档, 您是否确定退出') insert messages values('系统公用','4','正在新增或修改状态, 是否放弃存档') insert messages values('系统公用','5','复核') insert messages values('系统公用','6','取消复核') insert messages values('系统公用','7','您正在新增或修改资料, 无法更换公司') insert messages values('系统公用','8','联络电话') insert messages values('系统公用','9','您没有权限, 不能切换至该页面') insert messages values('系统公用','10','您正在新增或修改资料, 请先存档或取消后再进行此操作') insert messages values('系统公用','11','您的资料已复核或您没有修改权限, 无法进行此操作') insert messages values('系统公用','12','传真') insert messages values('系统公用','13','键值重复') insert messages values('系统公用','14','键值(@)重复') insert messages values('系统公用','15','不能执行报表处理器') insert messages values('系统公用','16','行动电话') insert messages values('系统公用','17','键值不能为空') insert messages values('系统公用','18','日期格式非法') insert messages values('系统公用','19','日期不能为空') insert messages values('系统公用','20','登录者无此权限') insert messages values('系统公用','21','输入统一编号不符合规则, 能确定吗?') insert messages values('系统公用','22','数据不合法') insert messages values('系统公用','23','没有安装报表工具,无法处理报表文件') insert messages values('系统公用','24','没有生成报表文件') insert messages values('系统公用','25','数量不可小于等于0') insert messages values('系统公用','26','系统支持的日期必须在1753/01/01 与9999/12/31 之间.') insert messages values('系统公用','27','资料非法或不完整') insert messages values('系统公用','28','资料不存在') insert messages values('系统公用','29','资料重复') insert messages values('系统公用','30','无法删除当前登录使用者的资料') insert messages values('系统公用','31','请先选择需要升级的会员卡类别(选择升级会员并选择类别)') insert messages values('系统公用','32','您必须关闭所有模组之后才能执行此功能') insert messages values('系统公用','33','您没有权限, 不能执行此功能') insert messages values('系统公用','34','非法之使用者或密码, 请再次输入') insert messages values('系统公用','35','键值不能修改') insert messages values('系统公用','36','执行档未找到') insert messages values('系统公用','37','已经完成所您要求执行的任务') insert messages values('系统公用','38','您没有选择任何需要执行的任务,请先选择!') insert messages values('系统公用','39','总部服务器报告错误,可能是网络问题,请稍后再试') insert messages values('系统公用','40','总部服务器没有回应,请检查总部服务器是否已经启动') insert messages values('系统公用','41','国内手机号码必须为11位') insert messages values('系统公用','42','会员卡类别(@)不存在,请到会员卡类别设定中检查') insert messages values('系统公用','43','请放上IC卡重试。是(已经放上IC卡) 否(放弃本次操作)') insert messages values('系统公用','44','本销售单的会员卡已经消费过,不允许删除,若要删除请先删除其收银单') insert messages values('系统公用','45','在出店中已经结账,若要修改或删除,必须先到出店作业中取消结案') insert messages values('系统公用','46','该会员卡已经在出店中等待结帐,暂不能用本卡来支付') insert messages values('系统公用','47','该卡所购买的套餐中,项目或产品(@)的剩余次数不够!') insert messages values('系统公用','48','该卡所购买的套餐中,没有项目或产品(@)') insert messages values('系统公用','49','表尾行数应该小于25') insert messages values('系统公用','50','表头行数应该小于25') insert messages values('系统公用','51','报表行数>表头行数+表尾行数+20') insert messages values('系统公用','52','999现金账户只作为设定现金折扣之用,不能在此选择此账户') insert messages values('系统公用','53','您已经在折扣设定中设定了本会员卡此账户是不允许消费此类项目/产品的') insert messages values('系统公用','54','(@)密码修改成功!') insert messages values('系统公用','55','帐户(@)欠款金额是(@),还款金额(@)超过了欠款额') insert messages values('系统公用','56','当前日期范围内有类型为(@)的单据(@)张未复核,请先检查并复核') insert messages values('系统公用','57','资料输入重复') insert messages values('系统公用','58','本次开单已经结帐了,不能再次结帐') insert messages values('系统公用','59','该帐款别不能新增') insert messages values('系统公用','60','单位必须是物品的三个单位(标准单位/消耗单位/进货单位)中的一个') insert messages values('系统公用','61','请先确定与此卡关联的会员,否则无法取用会员保证金') insert messages values('系统公用','62','请先存档再进行此操作') insert messages values('系统公用','63','不能用套餐来买卡或充值') insert messages values('系统公用','64','输入不可为0') insert messages values('系统公用','65','设定错误') insert messages values('系统公用','66','日期非法') insert messages values('系统公用','67','输入错误') insert messages values('系统公用','68','累计买卡金额达到升级标准故本卡已经升级到类别(@),本次升级的级数是(@)级') insert messages values('系统公用','69','输入不可为空') insert messages values('系统公用','70','不允许当前的折扣低于系统设置的最低折扣') insert messages values('系统公用','71','系统设置可能有误,因为设置的默认折扣为零') insert messages values('系统公用','72','当前日已经确认审核,不允许再调整') insert messages values('系统公用','73',' 抱歉该项目不参与体验项目') insert messages values('系统公用','74','本项目该客户已经做过体验') insert messages values('系统公用','75','该客户在今天之前已经做过(@)次体验项目了') insert messages values('系统公用','76','您确定要删除本公司所有未复核的单据吗') insert messages values('系统公用','77','本单据是公司(@)之单据,只允许在其归属公司复核或取消复核') insert messages values('系统公用','78','本单据是公司(@)之单据,只允许在其归属公司删除或修改') insert messages values('系统公用','79','根据公司政策,有促销赠送项目,是否需要赠送?') insert messages values('系统公用','80','会员(@)实际年龄(@)不在有效推广客户规则内') insert messages values('系统公用','81','取用保证金超过了未取用的保证金,请检查') insert messages values('系统公用','82','本员工(@)没有经理确认的权限') insert messages values('系统公用','83','本员工(@)没有经理确认的权限或确认密码错误') insert messages values('系统公用','84','选取起始范围') insert messages values('系统公用','85','模糊查询') insert messages values('系统公用','86','一致查询') insert messages values('系统公用','87','正在修改资料,无法进行此操作') insert messages values('系统公用','88','已经经过经理指纹验证,可以进行经理签单的支付方式') insert messages values('系统公用','89','没有取用保证金,不能用支付方式保证金支付') insert messages values('系统公用','90','需要升级到此类别,需要再购买金额为(@),本次升级的级数是(@)级,已经购买的金额为(@)') insert messages values('系统公用','91','本疗程目前状态为暂停') insert messages values('系统公用','92','您无权修改本单据的业绩分享比率,已经超过您所能修改的日期范围') insert messages values('系统公用','93','资料不存在') insert messages values('系统公用','94','目前系统要求更换疗程必须是同种项目类别同种价格序号') insert messages values('系统公用','95','套餐至少设置一个项目或一个产品') insert messages values('系统公用','96','疗程开单数量(@)不能大于卡的剩余疗程次数(@)') insert messages values('系统公用','97','产品(@)在产品资料中设置为不可赠送的') insert messages values('系统公用','98','前期卡抵卡金额不能高于现有价格(@)的(@)') insert messages values('系统公用','99','不能连接数据库服务器') insert messages values('系统公用','100','讯息') insert messages values('系统公用','101','非法使用者,请再次输入') insert messages values('系统公用','102','密码不正确,请再次输入') insert messages values('系统公用','103','非法使用者, 模组输入不正确, 请再次输入') insert messages values('系统公用','104','非法使用者,公司输入不正确, 请再次输入') insert messages values('系统公用','105','密码不可以为空,会员卡必须设置密码') insert messages values('系统公用','106','您确定要把会员卡(@)的会员编号也保持与卡号一致吗') insert messages values('系统公用','107','已经超过了系统设定的修改时间范围,禁止修改会员资料') insert messages values('系统公用','108','已经购买本项目(@)而且还大于等于(@)次不允许再次购买') insert messages values('系统公用','109','已经被复核不允许修改') insert messages values('系统公用','110','抱歉您只有一次开账的权限,若需要为客户开其他的卡请通知后台人员') insert messages values('系统公用','111','若要换卡,则必须本卡是全部用完,不能是只换一部分') insert messages values('系统公用','112','在开账资料中没有找到符合条件的会员卡') insert messages values('系统公用','113','您确定要删除当前指定的单据吗(只是简单删除单据,不调整相关的数据)') insert messages values('系统公用','114','项目基本资料中销售控制设置本价格序号为不允许销售') insert messages values('系统公用','115','您确定要删除本公司所有开账资料吗') insert messages values('系统公用','116','您确定要强行更改指定范围的会员卡的类别代码吗') insert messages values('系统公用','117','最多只能同时打开(@)个窗口!' ) insert messages values('系统公用','118','本会员卡号已经开账过,现在重新载入这个会员卡的所有资料但不会重新开帐') insert messages values('系统公用','119','您确定要修改指定单据的日期吗') insert messages values('系统公用','120','您无权限进行取款或单独做欠款的单据') insert messages values('系统公用','400','讯息') insert messages values('系统公用','401','错误') insert messages values('系统公用','402','警告') insert messages values('系统公用','500','111111\\r\\n2222220\n333') insert messages values('系统设定','1000','此处不可以为空!') insert messages values('系统设定','1001','输入范围必须在1~9中!') insert messages values('系统设定','1002','请不要跳格输入!') insert messages values('系统设定','1003','总和不可超过23!') insert messages values('系统设定','1004','传票编号总长不可超过12!') insert messages values('系统设定','1005','输入范围必须在1~5中!') insert messages values('系统设定','1006','初始化失败!') insert messages values('系统设定','1007','该使用者为超级用户不可删除') insert messages values('系统设定','1008','单价已经低于成本!') insert messages values('系统设定','1009','该客户编号不存在!') insert messages values('系统设定','1010','该厂商编号不存在!') insert messages values('系统设定','1011','该(潜在)客户已经存在,请输入尚未建档的客户编号!') insert messages values('系统设定','1012','该厂商已经存在,请输入尚未建档的厂商编号!') insert messages values('系统设定','1013','客户不可为空,请输入尚未建档的客户编号!') insert messages values('系统设定','1014','(@)拷贝成功!') insert messages values('系统设定','1015','未拷贝任何纪录!') insert messages values('系统设定','1016','厂商不可为空,请输入尚未建档的厂商编号!') insert messages values('系统设定','1017','母件不能作为自己的子件!') insert messages values('系统设定','1018','这是隶属于自己的子部门!') insert messages values('系统设定','1019','该产品编号不存在!') insert messages values('系统设定','1020','该产品已经存在,请输入尚未建档的产品编号!') insert messages values('系统设定','1021','产品不可为空,请输入尚未建档的产品编号!') insert messages values('系统设定','1022','该产品已经存在,请输入尚未建档的产品编号!') insert messages values('系统设定','1023','请输入(@)到(@)范围内的值!') insert messages values('系统设定','1024','公斤,磅,公克,公吨,材积,CBM,箱') insert messages values('系统设定','1025','请先存档或取消后再进行此操作!') insert messages values('系统设定','1026','不可以删除当前登录公司!') insert messages values('系统设定','1027','共(@)笔记录拷贝成功!') insert messages values('系统设定','1028','文件一定要在(@)目录下!') insert messages values('系统设定','1029','该文件尚未建立!') insert messages values('系统设定','1030','路径"(@)"不存在,请先建立之!') insert messages values('系统设定','1031','(@)资料输入重复!') insert messages values('系统设定','1032','统计期间请不要超过12个月!') insert messages values('系统设定','1033','存档失败!') insert messages values('系统设定','1034','资料同步失败!') insert messages values('系统设定','1035','此基本资料已有交易历史,不可删除!') insert messages values('系统设定','1036','合计值与12个月份目标值加总不等,要平均到个月份中吗?') insert messages values('系统设定','1037','最多只可以过帐12个月的资料!') insert messages values('系统设定','1038','本操作将从"客户+产品"中加总(@)公司(@)年的目标设定,原产品中的目标值将被覆盖.要继续吗?') insert messages values('系统设定','1039','本操作将从"厂商+产品"中加总(@)公司(@)年的目标设定,原产品中的目标值将被覆盖.要继续吗?') insert messages values('系统设定','1040','该文件不存在,要建立吗?') insert messages values('系统设定','1041','打印栏位不可为空!') insert messages values('系统设定','1042','统计期间请不要超过12个年度!') insert messages values('系统设定','1043','资料备份成功!') insert messages values('系统设定','1044','资料备份失败!') insert messages values('系统设定','1045','是否确定删除备份历史资料?') insert messages values('系统设定','1046','确定要退出S3管理系统?') insert messages values('系统设定','1047','日志文件空间使用已超过90%, 是否确定执行清理日志文件空间?') insert messages values('系统设定','1048','该文件不存在!') insert messages values('系统设定','1049','S/N产品数量只能为1!') insert messages values('系统设定','1050','厂商编号不可为空!') insert messages values('系统设定','1051','此币别为系统保留!') insert messages values('系统设定','1052','单号最长为12码!') insert messages values('系统设定','1053','确定要删除这些资料吗?删除后将无法恢复') insert messages values('系统设定','1054','该产品已开帐,不可删除!') insert messages values('系统设定','1055','您尚未输入条件或输入条件不完整, 请完整输入条件!') insert messages values('系统设定','1056','博卡软件') insert messages values('系统设定','3203','没有数据, 未产生报表!') insert messages values('系统设定','3106','You Have Not Set Page Size!!') insert messages values('系统设定','3107','interInitReport 函数参数不正确!') insert messages values('系统设定','3918','没有完成转档!') insert messages values('美容系统','2001','数据存档出现异常, 请与系统管理员联系!') insert messages values('美容系统','2002','该会员卡资料(@)不存在!') insert messages values('美容系统','2003','该会员卡已经在使用中!') insert messages values('美容系统','2004','该单据已经结案, 不能取消复核!') insert messages values('美容系统','2005','该客户已经产生应付费用,不能取消复核!') insert messages values('美容系统','2006','该客户入院时间已经超过(@)分钟,不能取消复核!') insert messages values('美容系统','2007','该资料用户不可以修改!') insert messages values('美容系统','2008','请先存档再操作!') insert messages values('美容系统','2009','该员工(@)不存在!') insert messages values('美容系统','2010',' 该会员类别不存在!') insert messages values('美容系统','2011','该折让选项不存在!') insert messages values('美容系统','2012','星期要在1和7之间!') insert messages values('美容系统','2013',' 截止周次必须大于等于开始周次!') insert messages values('美容系统','2014',' 截止日期必须大于等于开始日期!') insert messages values('美容系统','2015','数量不可小于零!') insert messages values('美容系统','2016',' 时间栏位输入不合法') insert messages values('美容系统','2017','该资料用户不可以修改!') insert messages values('美容系统','2018',' 截止时间必须大于等于开始时间!') insert messages values('美容系统','2019','该资料(@)在常用资料设定中不存在!') insert messages values('美容系统','2020',' 该模组资料不存在!') insert messages values('美容系统','2021',' 时间栏位不可为空!') insert messages values('美容系统','2022',' 时间栏位输入不合法!') insert messages values('美容系统','2023',' 该项目编号不存在!') insert messages values('美容系统','2024',' 该子项目编号不存在!') insert messages values('美容系统','2025',' 您在新增或修改状态,是否放弃存档!') insert messages values('美容系统','2026',' 该物品编号(@)不存在!') insert messages values('美容系统','2027',' 该使用者不存在!') insert messages values('美容系统','2028',' 该会员卡号不存在!') insert messages values('美容系统','2029',' 该会员编号不存在!') insert messages values('美容系统','2030',' 会员卡号不能与主会员卡重复!') insert messages values('美容系统','2031',' 该会员卡号已经在使用中,请检查输入是否正确?') insert messages values('美容系统','2032',' 会员卡号 (@) 已经在使用中,请检查输入是否正确') insert messages values('美容系统','2033',' 抱歉,不允许非会员入院消费!') insert messages values('美容系统','2034',' 抱歉,该卡状态是(@),不允许入院消费! ') insert messages values('美容系统','2035',' 抱歉,该卡已过期不允许入院消费! ') insert messages values('美容系统','2036',' 抱歉, 没有发现该会员卡(@)的未结帐资料! ') insert messages values('美容系统','2037',' 该单据已结帐,不允许修改!') insert messages values('美容系统','2038',' 该会员卡已结帐,不允许修改!') insert messages values('美容系统','2039',' 抱歉,不允许非会员预约!') insert messages values('美容系统','2040',' 抱歉,不允许赠券客户预约!') insert messages values('美容系统','2041',' 起始时间不得大于截止时间!') insert messages values('美容系统','2042',' 折扣范围不能小于零!') insert messages values('美容系统','2043',' 折扣范围不能大于 1 !') insert messages values('美容系统','2044',' 数量不能小于零 !') insert messages values('美容系统','2045',' 该会员卡+子标志号已经登记入院了,不能重复入院!') insert messages values('美容系统','2046',' 该预约单已经消费,不能取消复核!') insert messages values('美容系统','2047',' 该单据已经取消预约,不能取消复核!') insert messages values('美容系统','2048',' 税率值必须在0至1之间!') insert messages values('美容系统','2049',' 期限单位不存在') insert messages values('美容系统','2050',' 该预约已经生效,不能取消复核') insert messages values('美容系统','2051',' 入院人数必须大于零') insert messages values('美容系统','2052',' 该赠券已经使用, 不能再次使用') insert messages values('美容系统','2053',' 最高净额要大于等于最低净额') insert messages values('美容系统','2054',' 出院时间应该大于入院时间') insert messages values('美容系统','2055',' 该会员的账户余额已经低于安全余额,请充值') insert messages values('美容系统','2056',' 该项目不是壁球项目') insert messages values('美容系统','2057',' 该赠券已经过期, 不能使用') insert messages values('美容系统','2058',' 该会员卡,一次只允许(@) 人次入院') insert messages values('美容系统','2059',' 该会员卡的剩余次数不足,只剩下(@)次') insert messages values('美容系统','2060',' 该(@)资料不存在') insert messages values('美容系统','2061',' 该项目不允许预约') insert messages values('美容系统','2062',' 预约消费日期应 >= 预约日期') insert messages values('美容系统','2063',' 因为非会员不允许入院消费,所以无法设定非会员预约') insert messages values('美容系统','2064',' 该时间段内该场地繁忙,请另外选择时间') insert messages values('美容系统','2065',' 该客户已经使用(@)次赠券,不能入院消费') insert messages values('美容系统','2066',' 课程表:(@)中没有安排该项健身活动') insert messages values('美容系统','2067',' (@)输入不可为空') insert messages values('美容系统','2068',' 该预约单不属于该客户') insert messages values('美容系统','2069',' 客户支付的金额不平,不能结帐!应付(@)实付(@)') insert messages values('美容系统','2070',' 已付金额应该 <= 总金额!') insert messages values('美容系统','2071',' 请先输入会员卡号!') insert messages values('美容系统','2072',' 单一会员卡只能选择统一支付!') insert messages values('美容系统','2073',' 请先输入会员卡号!') insert messages values('美容系统','2074',' 该会员卡已经登记入院了,不能重复入院!') insert messages values('美容系统','2075',' 只能选择一种帐款类别') insert messages values('美容系统','2076',' 场地编号不存在或者该场地没有安排课程') insert messages values('美容系统','2077',' 已经输入了预约课程,如果修改场地编号,系统将删除课程内容,确定删除?') insert messages values('美容系统','2078',' 该项目预约人数已满(人数上限(@)人)') insert messages values('美容系统','2079',' 开始日期不能小于预约日期') insert messages values('美容系统','2080',' 开始时间不能小于预约时间') insert messages values('美容系统','2081',' (@)不能小于零') insert messages values('美容系统','2082',' 已经结帐,不允许取消复核!') insert messages values('美容系统','2083',' 超出账户余额(目前余额(@))') insert messages values('美容系统','2084',' 超出储值账户余额(目前余额(@))') insert messages values('美容系统','2085',' 此预约单资料不存在,或者该单据不是壁球预约单') insert messages values('美容系统','2086',' 异动类别资料不存在') insert messages values('美容系统','2087',' 账户类别资料不存在') insert messages values('美容系统','2088',' 该会员的储值账户余额(@)已经低于安全余额(@),请充值') insert messages values('美容系统','2089',' 该会员的储值账户余额不足,谢绝入院') insert messages values('美容系统','2101','会员卡号与入会单主档内卡号重复,请修改!') insert messages values('美容系统','2102','折让值必须在0至1之间!') insert messages values('美容系统','2103','(@)不得为负') insert messages values('美容系统','2104','会员卡业已开卡,不得取消复核!') insert messages values('美容系统','2105','资料已复核,不可修改') insert messages values('美容系统','2106','请将主档中的必输栏位填写完后再载入相关的收费明细!') insert messages values('美容系统','2107','会员卡业已使用,不得取消复核!') insert messages values('美容系统','2108','起始日期不得大于截止日期!') insert messages values('美容系统','2109','保留起始日期不得大于会员卡原截止日期!') insert messages values('美容系统','2110','续卡新截止日期不得小于会员卡原截止日期!') insert messages values('美容系统','2111','退卡日期不得大于会员卡截止日期!') insert messages values('美容系统','2112','会员卡截止日期不得小于开卡日期!') insert messages values('美容系统','2113','您已计算完薪资,不得再次计算!') insert messages values('美容系统','2114','日期不在设置月份内!') insert messages values('美容系统','2115','税率必须在0至1间!') insert messages values('美容系统','2116','起始日期不得小于登记日期!') insert messages values('美容系统','2117','转让日期不得大于截止日期!') insert messages values('美容系统','2118','该会员卡已经(@),不可使用') insert messages values('美容系统','2119','对不起,该单号已复核,不可再调整!') insert messages values('美容系统','2120','对不起,该人员非销售人员,不可使用!') insert messages values('美容系统','2121','对不起,该卡与原卡非同类型,不可使用!') insert messages values('美容系统','2122','对不起,请先填写原卡卡号!') insert messages values('美容系统','2123','对不起,会员卡必须由低等级类型向高等级类型转换!') insert messages values('美容系统','2124','对不起,新卡不得低于原卡价格!') insert messages values('美容系统','2125','会员卡号不得为空!') insert messages values('美容系统','2126','旧密码输入不得为空!') insert messages values('美容系统','2127','新密码输入不得为空!') insert messages values('美容系统','2128','确认密码输入不得为空!') insert messages values('美容系统','2129','旧密码不正确,请重新输入!') insert messages values('美容系统','2130','确认密码与新密码不一致,请重新输入!') insert messages values('美容系统','2131','密码修改成功!') insert messages values('美容系统','2132','对不起,该卡号不存在!') insert messages values('美容系统','2133','对不起,该类别会员卡不可(@)!') insert messages values('美容系统','2134','对不起,保留期限最大不可超出(@)天!') insert messages values('美容系统','2135','请注意,您的账户上还剩(@)元,请先提取账户上的余额,再行退卡!') insert messages values('美容系统','2136','对不起,用户已支付金额与系统应收金额不等,不可复核,请修改!') insert messages values('美容系统','2137','至少要在表格中填入一张会员卡,否则单据无效!') insert messages values('美容系统','2138','对不起,会员卡状态不对,不可使用!') insert messages values('美容系统','2139','对不起,您未填写原卡号,不可享受续卡优惠价!') insert messages values('美容系统','2140','对不起,附属会员编号与主会员编号重复,请修改!') insert messages values('美容系统','2141','对不起,新会员编号不得与原会员编号重复,请修改!') insert messages values('美容系统','2142','对不起,转让日期不得超过会员卡截止日期!') insert messages values('美容系统','2143','对不起,请填写0或者1!') insert messages values('美容系统','2144','对不起,本张最多只可绑定(@)个会员!') insert messages values('美容系统','2145','保留开始日不得小于异动日期!') insert messages values('美容系统','2146','对不起,该卡号已入院,目前不可做此操作') insert messages values('美容系统','2147','对不起,该赠券已经使用了,不可再修改其资料!') insert messages values('美容系统','2148','对不起,销售人员不得为空!') insert messages values('美容系统','2149','对不起,起始截止日期不得为空!') insert messages values('美容系统','2150','对不起,该卡已被卡种变更或遗失补卡的异动单使用,目前不可使用!') insert messages values('美容系统','2151','对不起,该卡已被会员卡销售单使用,目前不可更改其状态!') insert messages values('美容系统','2152','对不起,该卡已被卡种变更或遗失补卡的异动单使用,目前不可更改其资料!') insert messages values('美容系统','2153','对不起,单据资料有误,至少有一张卡被误删除,请检查!') insert messages values('美容系统','2154','对不起,保留日期区间与以前的保留日期区间有冲突,请检查!') insert messages values('美容系统','2155','对不起,请先输入会员卡号!') insert messages values('美容系统','2156','对不起,(@)库存量不足,不可进行此操作!如果您一定要这么做,请调整您的权限(UR028)') insert messages values('美容系统','2157','对不起,该文件不存在!') insert messages values('美容系统','2158','对不起,该文件类型不符合要求!') insert messages values('美容系统','2159','请务必输入具体的明细资料!') insert messages(type,id,content) values ('美容系统',2160,'该会员卡已过期,请确认是再使用!') insert messages(type,id,content)values ('美容系统',2161,'该会员卡还在保留期内,请确认需要入院!') insert messages values('美容系统','2201','仓库资料不存在!') insert messages values('美容系统','2202','供货商资料不存在!') insert messages values('美容系统','2203','请先输入项目编号!') insert messages values('美容系统','2204','该会员卡(@)在入店记录中没有找到!') insert messages values('美容系统','2205','请先删除消费项目再选择项目编号!') insert messages values('美容系统','2206','请先输入会员卡号以便算出折让后的价格!') insert messages values('美容系统','2207','请先输入收费项目以便算出金额!') insert messages values('美容系统','2208','金额不可以小于等于0!') insert messages values('美容系统','2209','相关入院单已经结帐出院 , 不允许复核/取消复核!') insert messages values('美容系统','2210','不存在此收支别!') insert messages values('美容系统','2211','未找到对应的客户会员资料!') insert messages values('美容系统','2212','该会员卡没有登记入院!') insert messages values('美容系统','2213','该会员卡已经登记入院了!') insert messages values('美容系统','2214','已经结帐,不允许再修改!') insert messages values('美容系统','2215','该会员曾经做院体质测试!') insert messages values('美容系统','2216','该卡已经挂失!') insert messages values('美容系统','2217','该卡已经到期,请续卡后再做体质测试!') insert messages values('美容系统','2218','该卡已经作废!') insert messages values('美容系统','2219','该会院没有此教练!') insert messages values('美容系统','2220','该会员卡还没有销售!') insert messages values('美容系统','2221','该会员卡还没有注册!') insert messages values('美容系统','2222','该测试项目不存在!') insert messages values('美容系统','2223','人数上限不允许小于等于0!') insert messages values('美容系统','2224','标准价格不允许小于等于0!') insert messages values('美容系统','2225','附件张数不允许小于0!') insert messages values('美容系统','2226','签核后不允许修改!') insert messages values('美容系统','2227','该测试单不存在!') insert messages values('美容系统','2228','系统固有资料不可以删除!') insert messages values('美容系统','2229','有效期限不允许小于零!') insert messages values('美容系统','2230','标准售价不允许小于零!') insert messages values('美容系统','2231','优惠售价不允许小于零!') insert messages values('美容系统','2232','(@)不允许小于零!') insert messages values('美容系统','2233','已经存在该类别会员卡,不允许删除!') insert messages values('美容系统','2234','请先输入会员卡号,再输入密码!') insert messages values('美容系统','2235','请输入消费密码!') insert messages values('美容系统','2236','该会员卡已经过期!') insert messages values('美容系统','2237','该会员卡还有(@)天到期!') insert messages values('美容系统','2238','您的密码输错三次,请您与相关的管理处联系!') insert messages values('美容系统','2239','请您先输入变更代码再输入费用类别!') insert messages values('美容系统','2240','判断别必须是 <= 或 > !') insert messages values('美容系统','2241','请输入查询密码!') insert messages values('美容系统','2242','该单据类别不存在!') insert messages values('美容系统','2243','请先输入单据类别再选择单据编号!') insert messages values('美容系统','2244','该会员卡没有相关的账户!') insert messages values('美容系统','2245','该客户是赠券入院没有相关的账户!') insert messages values('美容系统','2246','该先删除费用资料再修改变更代码!') insert messages values('美容系统','2247','截止日期要大于等于当前日期!') insert messages values('美容系统','2248','开始日期要大于等于当前日期!') insert messages values('美容系统','2249','您已经填写了单次入院金额,最大使用次数不允许再填写!') insert messages values('美容系统','2250','您已经填写了最大使用次数,单次入院金额不允许再填写!') insert messages values('美容系统','2251','一旦修改了会员卡号,已有的明细资料将被删除!') insert messages values('美容系统','2252','一旦修改了变更代码,已有的费用明细资料将被删除!') insert messages values('美容系统','2253','该场地该时间段的课程表已经安排,请另外选择时间段!') insert messages values('美容系统','2254','该星期的该时间段的课程已经安排,请另外选择时间段!') insert messages values('美容系统','2255','已支付金额(@)不等于应付总金额(@)不可以存档!') insert messages values('美容系统','2256','用户已支付金额要和支付明细中金额总和相等!') insert messages values('美容系统','2257','您账户的余额不足,不允许取消复核!') insert messages values('美容系统','2258','请输入您的消费资料,否则单据无效!') insert messages values('美容系统','2259','您的会员卡(@)已经出院结帐(入院单号(@)),但系统将继续保存此收银单!') insert messages values('美容系统','2260','您的账户余额不足(余额只有(@),消费(@)),不允许结账!') insert messages values('美容系统','2261','您的账户余额不足(余额只有(@),消费(@)),不允许结账!') insert messages values('美容系统','2262','该角色的使用者资料已经存在,不允许删除!') insert messages values('美容系统','2263','请先填入仓库,否则无法找到物品!') insert messages values('美容系统','2264','(@)之前该仓库(@)内没有该物品(@)!') insert messages values('美容系统','2265','一旦修改了仓库,已有的明细资料将被删除!') insert messages values('美容系统','2266','(@)之前仓库(@)中已经没有该物品(@)!') insert messages values('美容系统','2267','(@)之前仓库(@)中该物品(@)数量不足!') insert messages values('美容系统','2268','该会员卡是赠券入院!') insert messages values('美容系统','2269','开卡截至日期要大于等于销售日期!') insert messages values('美容系统','2270','系统不允许使用该支付方式!') insert messages values('美容系统','2271','散客不允许这种付款方式!') insert messages values('美容系统','2300','数量不可小于等于零!') insert messages values('美容系统','2301','金额需大于零!') insert messages values('美容系统','2302','期限不可小于或等于零!') insert messages values('美容系统','2303','人数上限小于或等于零!') insert messages values('美容系统','2304','该类别不存在!') insert messages values('美容系统','2305','该赠券状态不存在!') insert messages values('美容系统','2306','已经复核,不可修改!') insert messages values('美容系统','2307','该会员卡已存在历史记录,不能删除!') insert messages values('美容系统','2308','金额或系数不可小于等于零!') insert messages values('美容系统','2309','请输入会员卡号!') insert messages values('美容系统','2310','该会员卡没有建立账户或未开卡,不能进行充值!') insert messages values('美容系统','2311','该会员卡账户已存在异动历史,不能删除!') insert messages values('美容系统','2312','该会员卡已销售,不能删除!') insert messages values('美容系统','2313','使用人次不可小于零!') insert messages values('美容系统','2314','您已选了固定项目,不能在此选择!') insert messages values('美容系统','2315','销售人员不能为空!') insert messages values('美容系统','2316','截止日期不能为空!') insert messages values('美容系统','2318','该会员卡状态不正确,不能建立账户!') insert messages values('美容系统','2319','该客户存在消费历史,不能删除!') insert messages values('美容系统','2320','该员工非教练人员!') insert messages values('美容系统','2321','会员卡号输入不能为空!') insert messages values('美容系统','2322','起始卡号不能小于截止卡号!') insert messages values('美容系统','2323','会员卡类别输入不能为空!') insert messages values('美容系统','2325','赠券编号输入不可为空!') insert messages values('美容系统','2326','起始编号不能小于截止编号!') insert messages values('美容系统','2327','赠券编号不能小于零!') insert messages values('美容系统','2328','赠券类别不能为空!') insert messages values('美容系统','2324','会员编号不可小于零!') insert messages values('美容系统','2329','您账户的余额太少,账户(@)只有(@),但您希望取出(@)!') insert messages values('美容系统','2330','请先选择一个会员卡号!') insert messages values('美容系统','2331','请先选择一个仓库!') insert messages values('美容系统','2332','该物品的库存量不足!') insert messages values('美容系统','2333','请先选择单据类别!') insert messages values('美容系统','2334','该单据类别不存在!') insert messages values('美容系统','2335','该单号不存在!') insert messages values('美容系统','2336','请输入使用者代号!') insert messages values('美容系统','2337','请输入单据类别!') insert messages values('美容系统','2338','请输入会员卡前缀!') insert messages values('美容系统','2339','会员卡状态不存在!') insert messages values('美容系统','2340','邮政编码只能输入数字!') insert messages values('美容系统','2341','该会员已经入院!是否需要取消上次入院,重新入院?') insert messages values('美容系统','2342','该会员卡还欠款:(@),欠款负责人:(@)') insert messages values('美容系统','2343','该项目在服务项目中不存在!请核对!') insert messages values('美容系统','2344','卡号中若有?或;号,请输入半角符号!') insert messages values('美容系统','2345','分享比率之和必须为1(100%)') insert messages values('美容系统','2346','从IC卡读取得金额(@)与系统中的金额(@)不相等,是否要继续?') insert messages values('美容系统','2347','IC卡读错误') insert messages values('美容系统','2348','IC卡写错误') insert messages values('美容系统','2349','IC卡初始化失败') insert messages values('美容系统','2350','IC卡密码错误,请在注册表中核对PWD1,PWD2,PWD3(密码三次错误本卡作废)') insert messages values('美容系统','2351','单次消费不能超过10000元') insert messages values('美容系统','2352','此IC卡未充值,请充值!') insert messages values('美容系统','2353','请不要轻易切换数据库,S3MEI为正式营业用数据库,\r\nS3MEIDEMO为学习用的数据库,您确定需要切换吗!') insert messages values('美容系统','2354','不正确的注册码格式,正确的注册码应该是XXXX-9XXX-9XXX-9XXX!') insert messages values('美容系统','2355','本IC卡有欠款,是否要继续?') insert messages values('美容系统','2356','请先输入销售人员!') insert messages values('美容系统','2357','会员卡当前状态不允许充值') insert messages values('美容系统','2358','请输入正确的金额') insert messages values('美容系统','2359','请先输入会员卡号再输入对应的金额') insert messages values('美容系统','2360','应收金额与付款总额不相等') insert messages values('美容系统','2361','打开数据线的通讯端口失败,请拨掉数据线重新插上!') insert messages values('美容系统','2362','本卡当天没有此金额的销卡记录,不允许记帐') insert messages values('美容系统','2363','本卡当天销卡金额与您输入的消费金额不相等,不允许记帐') insert messages values('美容系统','2364','消费金额不能超过IC卡的余额') insert messages values('美容系统','2365','只能输入存款或还款') insert messages values('美容系统','2366','卡的充值金额必然大于客户欠款金额!') insert messages values('美容系统','2367','已经等待太久但是您没有输入消费的金额!请重新销卡!') insert messages values('美容系统','2368','请输入数字!') insert messages values('美容系统','2369','还款不能大于欠款金额!') insert messages values('美容系统','2370','无法连接远端的服务器!') insert messages values('美容系统','2371','当前输入资料可能已经存在,请检查资料:(@)!') insert messages values('美容系统','2372','请先配置服务器IP地址,注册表boka\mrmf项目中ServerIP!') insert messages values('美容系统','2373','系统没有检测到可以发短信的端口!\r\n请确认数据线是否插好;\r\n请确认数据线的驱动盘是否正确安装') insert messages values('美容系统','2374','系统检测到可用于发短信的端口为:(@)!') insert messages values('美容系统','2375','下班时间必须大于上班时间') insert messages values('美容系统','2376','开始日期必须大于截止日期') insert messages values('美容系统','2377','班号不存在') insert messages values('美容系统','2378','该会员已经在相同时间预约了,请不要重复预约.') insert messages values('美容系统','2379','只有正常状态的预约单才可以修改') insert messages values('美容系统','2380','抱歉,该卡状态是(@),不允许消费!') insert messages values('美容系统','2381','是否需要更新会员的信息?') insert messages values('美容系统','2382','所要查询状态的时间必须是当前时间之后') insert messages values('美容系统','2383','请先输入时间范围') insert messages values('美容系统','2384','开始时间必须小于截止时间') insert messages values('美容系统','2385','客户介绍网络目前系统只支持10层!') insert messages values('美容系统','2386','提佣比率必须在0~1之间!') insert messages values('美容系统','2387','该时段内没有空闲员工!') insert messages values('美容系统','2388','服务时间超过预开单时间(@)分钟!') insert messages values('美容系统','2389','服务时间少于预开单时间(@)分钟!') insert messages values('美容系统','2390','此IC卡不是本公司会员卡!') insert messages values('美容系统','2391','此IC卡已经开卡并正在使用,不允许重新初始化!') insert messages values('美容系统','2392','此IC卡账户中有余额,不允许重新初始化!') insert messages values('美容系统','2393','消费金额不能超过会员卡储值账户中余额') insert messages values('美容系统','2400','还有员工当天没有打下班卡,不能自动检查考勤状况') insert messages values('美容系统','2401','异动金额至少应该大于或等于疗程金额!') insert messages values('美容系统','2406','查询时间不合法!') insert messages values('美容系统','2407','支付方式为经理签单必须要由经理确认!') insert messages values('美容系统','2408','需要传输的资料为:(@),是否确定要传输?') insert messages values('美容系统','2409','在本公司不存在此会员卡,是否需要新建此会员卡?') insert messages values('美容系统','2410','此会员卡号已经在别的公司存在,不允许在本公司新建重复的会员卡!') insert messages values('美容系统','2411','疗程金额只能存在储值账户中!') insert messages values('美容系统','2412','此会员尚欠款(@)金额,您确定客户继续充值而不是还款吗?') insert messages values('美容系统','2413','此日期的帐已经封结,不允许新增或删除或修改!') insert messages values('美容系统','2414','您确定要删除此日期范围内的所有账务吗!') insert messages values('美容系统','2415','此会员卡已经开帐,若要修改会员资料请直接到会员基本资料或会员账户管理中调整!') insert messages values('美容系统','2416','此物品没有此单位!') insert messages values('美容系统','2417','更新库存出错,请检查物品资料的设定!') insert messages values('美容系统','2418','确定要强行更改会员卡的状态吗?') insert messages values('美容系统','2419','该会员卡的剩余次数低于安全次数,只剩下(@)次') insert messages values('美容系统','2420','将把当前IC卡中金额清零!') insert messages values('美容系统','2421','取款疗程并不在已经购买的疗程清单中!') insert messages values('美容系统','2422','身份证号码只能是15位或18位,现在为(@)位,请检查!') insert messages values('美容系统','2423','请先选择产品单位!') insert messages values('美容系统','2424','疗程账户只能是账户2,4,5!') insert messages values('美容系统','2425','您选择的项目并没有购买疗程,请更换账户类别或选择别的项目!') insert messages values('美容系统','2426','您更改了日期,系统需要重新判断当时的库存是否够,所以系统已将数量全部改为0,请重新输入数量!') insert messages values('美容系统','2427','正在修改单据,请先保存!') insert messages values('美容系统','2428','该会员卡号正在入院消费中,请在其消费出院后再修改!') insert messages values('美容系统','2501','员工卡密码错误!') insert messages values('美容系统','2502','请输入打折原因!') -- Added by MZH 2005/09/01 insert messages values('美容系统','2503','请在备注输入说明信息!') -- Added by MZH 2005/09/02 insert messages values('美容系统','2504','账款类别请选择*!') -- Added by MZH 2005/09/27 insert messages values('每容美发','2505','计次卡的最大使用次数应该大于0!') --Added by Mzh 2005/12/01 insert messages values('美容美发','2506','盘点调帐自动产生入库单据(@),请自行到物品入库中复核!') --Added by Mzh 2005/12/07 insert messages values('美容美发','2507','盘点调帐自动产生出库单据(@),请自行到物品出库中复核!') --Added by Mzh 2005/12/07 insert messages values('美容美发','2508','此盘点单没有库存差异,不需要进行调帐') insert messages values('美容美发','2509','此盘点单产生的入库或出库单已经复核,不能再进行调帐') insert messages values('美容美发','2510','会员编号重复,请重新输入!') insert messages values('美容美发','2511','该公司别不存在,请重新输入!') insert messages values('美容美发','2512','(@)编号重复,请重新输入!') insert messages values('美容美发','2513','(@)长度大于系统定义长度,请重新输入!') insert messages values('美容美发','2514','(@)物品资料载入完成!') insert messages values('美容美发','2515','项目资料载入完成!') insert messages values('美容美发','2516','员工资料载入完成,员工编号从(@)到(@)') insert messages values('美容系统','2517','起始序号不能小于截止序号!') insert messages values('美容系统','2518','该会员欠款(@)元,欠款账户为(@),欠款负责人:(@)!') insert messages values('美容系统','2519','该会员用现金支付,请确认!') insert messages values('美容系统','2520','该会员卡不是本地卡,不能充值!') insert messages values('美容系统','2521','项目累计开始点数不可为空') insert messages values('美容系统','2522','项目累计开始点数不可小于0!') insert messages values('美容系统','2523','该会员不能消费该项目!') insert messages values('美容系统','2524','会员卡类别不可为空!') insert messages values('美容系统','2525','项目类别不可为空!') insert messages values('美容系统','2526','请选择不同的会员卡类型!') insert messages values('美容系统','2527','点数不能小于零!') insert messages values('美容系统','2528','该订单总部已下单,不能取消') insert messages values('美容系统','2529','该订单的发货申请单状态为(@),不能取消') insert messages values('美容系统','2530','该订单的一笔下单记录未处理,不能累计下单!') insert messages values('美容系统','2531','实际发货量应该等于申请发货量!') insert messages values('美容系统','2532','实收数量应该小于发货数量!') insert messages values('美容系统','2533','欠货数量应该小于发货数量!') insert messages values('美容系统','2534','损坏数量应该小于发货数量!') insert messages values('美容系统','2535','(@)下单量大于库存量,库存量为(@),下单量为(@),请确认是否下单!') insert messages values('美容系统','2536','取款金额大于账户余额,取款金额为(@),账户余额为(@)!') insert messages values('美容系统','2537','下单数量大于未下单数量!') insert messages values('美容系统','2538','(@)欠货单总部已确认,不可取消!') insert messages values('美容系统','2539','欠货产品必须一次全部下单!') insert messages values('美容系统','2540','欠货载入数量应小于欠货数量!') insert messages values('美容系统','2541','欠货载入的产品数量不可修改,该产品欠货数量为(@)!') insert messages values('美容系统','2542','欠货载入的产品单价不可修改!') insert messages values('美容系统','2543','欠货载入的产品单价,金额不可修改!') insert messages values('美容系统','2544','欠货载入的产品资料不可修改!') insert messages values('美容系统','2545','款项未申请到,不能发货!') insert messages values('美容系统','2546','账户余额不足,不允许款项申请!') insert messages values('美容系统','2547','账户取款异动,不能取消复核!') insert messages values('美容系统','2548','该员工这个时间段处于忙碌状态,请重新选择!') insert messages values('美容系统','2549','请选择预约时间为当前时间之后!') insert messages values('美容系统','2550','请选择预约结束时间大于开始时间!') insert messages values('美容系统','2551','请选择预约日期!') insert messages values('美容系统','2552','请输入会员卡号') insert messages values('美容系统','2553','散客请输入电话号码!') insert messages values('美容系统','2554','该会员已在相同时刻有预约!') insert messages values('美容系统','2555','当前版本不需要入院或设定为不需要入院,请直接到收银界面中操作!') insert messages values('美容系统','2556','当前版本需要入院过程,请先入院再来收银!') insert messages values('美容系统','2557','当前会员卡为正在使用状态,是否要变更到此卡?') insert messages values('美容系统','2558','因为设定为与总部必须保持统一,只能到总部001去修改') insert messages values('美容系统','2559','点此按钮将当前栏位设定复制到每个产品对应栏位,确定吗?') insert messages values('美容系统','2560',' 取款不允许该支付方式') insert messages values('美容系统','2561',' 在当前系统参数设定情况下卡销售时自动开卡的,不需要在此手工开卡') insert messages values('美容系统','2600','对不起,上次进货日期必须小于当前日期') insert messages values('美容系统','2601','本操作只能在总公司001中修改') insert messages values('美容系统','2602','输入的日期超过了在系统参数设定中设定的可见资料的范围!') insert messages values('美容系统','2603','此员工没有免单的权限!') insert messages values('美容系统','2604','等待太久,客户还没有输入密码!') insert messages values('美容系统','2605','您确定要删除本公司之外其他公司的所有账务以及基本资料吗!') insert messages values('美容系统','2606','您本次删除的资料有(@)笔,是否确认删除?') insert messages values('美容系统','2799','寄存物品不可以为空') insert messages values('美容系统','2809','本店没有此产品') insert messages values('美容系统','2810','该物品库存量不足') insert messages values('美容系统','2811','领取数量不可以大于剩余数量') insert messages values('美容系统','2812','新增状态不可以领取') insert messages values('美容系统','2813','已经存在的寄存不可以修改寄存数量') insert messages values('美容系统','2814','已经领取的物品不可以修改其状态') insert messages values('美容系统','2815','物品状态为寄存,请更新其状态') insert messages values('美容系统','2816','已经存在的寄存不可以删除寄存物品') insert messages values('美容系统','2817','每班次只能刷卡6次') insert messages values('美容系统','2818','现在是上班时间刷卡') insert messages values('美容系统','2819','现在是下班时间刷卡') insert messages values('美容系统','2820','下班没有刷卡不可以在其他班次刷卡') insert messages values('美容系统','2822','会员卡号与异动单号与账户类别不符合,请重新输入.') insert messages values('美容系统','2823','本次扣除金额不可大于单据原金额') insert messages values('美容系统','2824','已经入店而没有结帐的散客太多了,请到会员出店中把散客结账') insert messages values('美容系统','2825','客人(@)已经出店结账') insert messages values('美容系统','2826','被更改状态的卡的数量为(@),您确定要更改吗') insert messages values('美容系统','2827','请注意,当前员工(@)并不在职') insert messages values('美容系统','2828','请注意,已经存在新代码的员工') insert messages values('美容系统','2829','本项目代码已经在收银单据中被用到,不能删除') insert messages values('系统设定','2836','只能修改在本店开的的卡') insert messages values('系统设定','2833','物品(@)没有此单位') insert messages values('系统设定','2837','只能删除在本店开的的卡') insert messages values('系统设定','2838','单据时间小于当前时间不可以选取待结帐') insert messages values('系统设定','2839','已经结束的服务不可以取消') insert messages values('系统设定','2840','自己不可以委托给自己') insert messages values('系统设定','2841','选择客户不可以为空') insert messages values('系统设定','2842','已经结帐的不可以再次结帐') insert messages values('系统公用','2843','重新生成疗程历史只有在开卡公司生成') insert messages values('系统公用','2844','您已经成功调整销卡金额') insert messages values('系统设定','2845','充值额度不可以小于最低充值额度') insert messages values('系统设定','2846','此卡没有挂失不可以解挂') insert messages values('系统设定','2847','此卡状态不允许挂失') insert messages values('系统设定','2848','升级卡种请输入新卡种') insert messages values('系统设定','2849','自己不可以是自己的介绍人') insert messages values('系统设定','2850','你没有更改换任何疗程') insert messages values('系统设定','2851','更换金额(@)大于疗程剩余金额(@)') insert messages values('系统设定','2852','更换金额之和大于购买疗程金额') insert messages values('系统设定','2853','疗程支付金额和购买之后不相等') insert messages values('系统设定','2854','拷贝公司不可以为空') insert messages values('系统设定','2855','拷贝公司不可以为登录公司') insert messages values('美容系统','2856','你没有修改默认疗程的权限,系统会强制还原') insert messages values('美容系统','2857','此疗程本月可使用次数为(@)次但本月已经使用(@)次') insert messages values('美容系统','2860','已经考勤的班次不可以调整班次.') insert messages values('美容系统','2861','该员工已经有排班。') insert messages values('美容系统','2862','该员工原班次不存在。') insert messages values('美容系统','2863','原班次不可以为空') insert messages values('美容系统','2864','调整班次不可以为空') insert messages values('系统设定','2702','床位(@)使用时间上冲突,请先调整当前床位该时间后的项目') insert messages values('美容系统','2865','促销编号相同的促销,促销依据必须相同') insert messages values('美容系统','2866','帐户标志必须被选择') insert messages values('美容系统','2867','产品标志必须被选择') insert messages values('美容系统','2868','项目标志必须被选择') insert messages values('美容系统','2869','赠送类别类别为比率时,此值必须在0-1之间') insert messages values('系统公用','2870','提成设定必须是店总业绩比率') insert messages values('系统公用','2871','比率必须是在0-1之间') insert messages values('系统公用','2872','只有售前的单据才能将保证金转销售') insert messages values('系统公用','2873','本次积分换现金所影响到的会员卡数量为(@)') insert messages values('系统公用','2874','本次积分主动注入所影响到的会员卡数量为(@)') insert messages values('系统公用','2875','您还没有设置连锁结构,请别忘记设置') insert messages values('系统公用','2876','消费金额达到升级标准故本卡已经升级到类别(@),本次升级的级数是(@)级') insert messages values('系统公用','2877','您确定您已经恢复了隐藏的数据了吗?') insert messages values('系统公用','2878','开始恢复了被隐藏的数据') insert messages values('系统公用','2879','当前卡的级别比您选择的卡类别级别更高,无需升级') insert messages values('系统公用','2880','当天已经消费(@)次,将超过一天的会员卡消费次数限制(@)次,禁止消费!') insert messages values('系统公用','2881','当天已经消费(@)次,将超过一天的会员卡消费次数限制(@)次,需要经理验证才能消费!') insert messages values('系统公用','2882','当天已经消费(@)次,将超过一天的会员卡消费次数限制(@)次,请注意!') insert messages values('系统公用','2883','购买的疗程的消费单价将由当前的所购买的疗程自动计算,请直接选择疗程价就可以了!') insert messages values('系统公用','2884','对不起,没有您输入的抵用券(@)!') insert messages values('系统公用','2885','对不起,没有您输入的抵用券(@)已经被用过了!') insert messages values('系统公用','2886','对不起,没有您输入的抵用券(@)已经被送客户了!') insert messages values('系统公用','2887','保证金或抵用券支付金额超过了取用的金额!') insert messages values('系统公用','2888','没有取用抵用券支付!') insert messages values('系统公用','2889','您选择了疗程支付但没有与某个疗程对应(疗程序号为空)!') insert messages values('系统公用','2890','抱歉,并没有找到您需要寻找的资料(@),目前显示为最接近的资料') insert messages values('系统公用','2891','您输入的新卡号已经在系统中存在,请更换') insert messages values('系统公用','2892','当前登录者无权新增会员卡(会员卡资料管理的新增权限)') insert messages values('系统公用','2893','非主项目/产品数量只能为零') insert messages values('系统公用','2894','已经超过了保证金的最后取款日期(@)') insert messages values('系统公用','2895','已经超过了保证金的最后修改日期(@)') insert messages values('系统公用','2896','确定要根据产品的进价成本来重新计算所有产品的消耗成本吗') insert messages values('系统公用','2897','本盘点单已经进行库存调账不能再修改') insert messages values('系统公用','2898','当前用户没有权限启用内调的支付方式') insert messages values('系统公用','2899','替换的次数不能超过剩余的次数') insert messages values('系统公用','2900','您输入的替换金额与您选择替换的金额之和不相等,请重新选择') insert messages values('系统公用','2901','您输入的取款的疗程系统中并不存在,代码(@)价格序号(@)') insert messages values('系统公用','2902','支付的抵用券金额与您选择的抵用券金额之和不相等') insert messages values('系统公用','2903','本疗程已经过期,截至日期为(@)') insert messages values('系统公用','2904','您无权分发基础数据') insert messages values('系统公用','2905','赠送的疗程不允许用来抵卡') insert messages values('系统公用','2906','强烈建议设置IC卡的店标志密码保护您自己店的IC卡避免不必要的麻烦') insert messages values('系统公用','2907','本产品编号(@)已经停用') insert messages values('系统公用','2908','本项目编号(@)已经停用') insert messages values('系统公用','2909','本会员编号(@)在会员基本资料中已经存在') insert messages values('系统公用','2910','本赠券号(@)不在公司发行带密码的列表中') insert messages values('系统公用','2911','此卡对应的会员卡类别已经设置为不能继续购买或充值') insert messages values('系统公用','2912','当前使用者无权进行项目欠款消费') insert messages values('系统公用','2913','当前使用者无权进行产品欠款购买') insert messages values('系统公用','2914','产品(@)当前库存数量(@)已经超过本产品最高安全库存(@)') insert messages values('系统公用','2915','此会员卡对应的会员卡类别(@)已经设置为不允许继续充值了') insert messages values('系统公用','2916','此会员卡对应的会员卡类别(@)已经设置为不允许销售了') insert messages values('系统公用','2917','不能选择此账户类别(@)') insert messages values('系统公用','2918','请检查是否已经放上会员卡(卡号为(@))') insert messages values('系统公用','2919','当前IC卡的卡号是(@)但系统要求的IC卡的卡号是(@)') insert messages values('系统公用','2920','系统还没有传输好昨天之数据,故当天不能新增单据') insert messages values('系统公用','2921','核算金额之和(@)不等于套餐金额(@)') end GO /****** Object: StoredProcedure [dbo].[up_powerlogfile_update] Script Date: 06/12/2017 06:19:22 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO --WUXM 2004/6/29 create procedure [dbo].[up_powerlogfile_update] ( @userid varchar(10), -- User ID @program varchar(10), -- 程式代码 @operation varchar(1), -- 操作 @operationdate varchar(8), -- 异动日期 @operationtime varchar(8), -- 异动时间 @origatedate varchar(8), -- 单据作业时间. @compid varchar(10), -- Company ID @keyvalue1 varchar(160), @keyvalue2 varchar(160), @keyvalue3 varchar(160), @keyvalue4 varchar(160), @Dlogfile_id int ) as--WUXM, 2004/6/29 --update: wuxinming, 2004/11/22 declare @Doperationdate varchar(8), -- 异动日期 @Doperationtime varchar(8), -- 异动时间 @count int, @LasTCOUNT int select @LasTCOUNT = floatvalue from sysparams where id = 'LOGFILE_LasTCOUNT' if (ISnull(@LasTCOUNT, 0) > 0) begin select @Doperationdate = operrationdate, @Doperationtime = operrationtime from powerlogfile where logfile_id = @Dlogfile_id select @count = COUNT(1) from powerlogfile where program = @program and operrationdate + operrationtime >= @Doperationdate + @Doperationtime if (@count > @LasTCOUNT - 1) begin delete powerlogfile where logfile_id = @Dlogfile_id end end insert powerlogfile(userid, program, operration, operrationdate, operrationtime, origatedate, compid, keyvalue1, keyvalue2, keyvalue3, keyvalue4) values(@userid, @program, @operation, @operationdate, @operationtime, @origatedate, @compid, @keyvalue1, @keyvalue2, @keyvalue3, @keyvalue4); GO /****** Object: StoredProcedure [dbo].[upg_update_gbm15] Script Date: 06/12/2017 06:20:49 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_update_gbm15] ( @compid varchar(10), @billtype varchar(10), @billid varchar(20), @billdate varchar(8), @memid varchar(20), @amt float, @check int ) as begin if @check = 1 begin declare cur_each_gbm15 cursor for select isnull(gbl11f, 0), isnull(gbl07f, 0) from gbm15 where gbl00c = @compid and gbl01c = @memid and isnull(gbl12i, 0) = 0 order by gbl05d for update of gbl07f, gbl08d, gbl09c, gbl10c, gbl11f, gbl12i end else begin declare cur_each_gbm15 cursor for select isnull(gbl11f, 0), isnull(gbl07f, 0) from gbm15 where gbl00c = @compid and gbl01c = @memid and isnull(gbl12i, 0) = 1 order by gbl05d desc for update of gbl07f, gbl08d, gbl09c, gbl10c, gbl11f, gbl12i end open cur_each_gbm15 declare @unuseamt float declare @useamt float fetch cur_each_gbm15 into @unuseamt, @useamt while @@fetch_status = 0 and @amt>0 begin if @check = 1 begin --复核 if @amt >= @unuseamt begin update gbm15 set gbl07f = isnull(gbl07f, 0) + @unuseamt, gbl08d = @billdate, gbl09c = @billtype, gbl10c = @billid, gbl11f = 0, gbl12i = 1 where current of cur_each_gbm15 select @amt = @amt - @unuseamt end else begin update gbm15 set gbl07f = isnull(gbl07f, 0) + @amt, gbl08d = @billdate, gbl09c = @billtype, gbl10c = @billid, gbl11f = gbl11f-@amt, gbl12i = 0 where current of cur_each_gbm15 select @amt = 0 end end else begin --取消复核 if @amt <= @useamt begin update gbm15 set gbl07f = gbl07f-@amt, gbl08d = '', gbl09c = '', gbl10c = '', gbl11f = isnull(gbl11f, 0) + @amt, gbl12i = 0 where current of cur_each_gbm15 select @amt = 0 end else begin update gbm15 set gbl07f = gbl07f-@useamt, gbl08d = '', gbl09c = '', gbl10c = '', gbl11f = isnull(gbl11f, 0) + @useamt, gbl12i = 0 where current of cur_each_gbm15 select @amt = @amt-@useamt end end fetch cur_each_gbm15 into @unuseamt, @useamt end close cur_each_gbm15 deallocate cur_each_gbm15 end GO /****** Object: StoredProcedure [dbo].[upg_update_gbm15_ex] Script Date: 06/12/2017 06:20:49 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_update_gbm15_ex] ( @compid varchar(10), @billtype varchar(10), @billid varchar(20), @billdate varchar(8), @memid varchar(20), @amt float, @seq float, @check int ) as begin declare @inputamt float declare @unuseamt float declare @useamt float select @inputamt = @amt select @unuseamt = isnull(gbl11f, 0), -- 未取用金额 @useamt = isnull(gbl07f, 0) -- 取用金额 from gbm15 a with (nolock) where --a.gbl00c = @compid a.gbl01c = @memid and a.gbl02f = @seq if @check = 1 begin -- 复核 if @amt >= @unuseamt begin update a set gbl07f = isnull(gbl07f, 0) + @amt, -- 取用金额 gbl08d = @billdate, gbl09c = @billtype, gbl10c = @billid, gbl11f = 0, gbl12i = 1 from gbm15 a where a.gbl01c = @memid and a.gbl02f = @seq select @amt = @amt - @unuseamt end else begin update a set gbl07f = isnull(gbl07f, 0) + @amt, gbl08d = @billdate, gbl09c = @billtype, gbl10c = @billid, gbl11f = gbl11f - @amt, gbl12i = 0 from gbm15 a where a.gbl01c = @memid and a.gbl02f = @seq select @amt = 0 end -- 插入取用历史 insert gbm17(gbw00c, gbw01c, gbw02f, gbw03c, gbw04c, gbw05f, gbw06d) values(@compid, @memid, @seq, @billid, @billtype, @inputamt, @billdate) end else begin -- -- 取消复核 -- if @amt <= isnull(@useamt,0) -- begin -- update a -- set gbl07f = gbl07f - @amt, -- gbl08d = '', -- gbl09c = '', -- gbl10c = '', -- gbl11f = isnull(gbl11f, 0) + @amt, -- gbl12i = 0 -- from gbm15 a -- where a.gbl00c = @compid -- and a.gbl01c = @memid -- and a.gbl02f = @seq -- -- select @amt = 0 -- end -- else -- begin -- update a -- set gbl07f = gbl07f - @useamt, -- gbl08d = '', -- gbl09c = '', -- gbl10c = '', -- gbl11f = isnull(gbl11f, 0) + @useamt, -- gbl12i = 0 -- from gbm15 a -- where a.gbl00c = @compid -- and a.gbl01c = @memid -- and a.gbl02f = @seq -- -- select @amt = @amt - @useamt -- end --根据单号 门店 查询保证金历史表中的历史数据 更新到GBM15 保证金账户 select @amt = gbw05f,@seq = gbw02f from gbm17 where gbw00c = @compid and gbw01c = @memid and gbw03c = @billid and gbw06d = @billdate --根据保证金交易历史,取消复核时,还原保证金的金额 update a set gbl07f = gbl07f - gbw05f, gbl08d = '', gbl09c = '', gbl10c = '', gbl11f = isnull(gbl11f, 0) + gbw05f, gbl12i = 0 from gbm15 a ,gbm17 b where a.gbl01c = b.gbw01c and a.gbl02f = b.gbw02f and b.gbw00c = @compid and gbw01c = @memid and gbw03c = @billid and gbw06d = @billdate -- 删除取用的历史 delete gbm17 where gbw00c = @compid and gbw01c = @memid and gbw03c = @billid and gbw04c = @billtype and gbw06d = @billdate end end GO /****** Object: StoredProcedure [dbo].[upg_book_leave_glm01] Script Date: 06/12/2017 06:19:30 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_book_leave_glm01] ( @compid varchar(10), -- 公司别 @gla01c varchar(20) -- 入院单号 ) as--MaZhaohong 2006/06/15 begin --通过入院单号获取是否有预约单与它相关联 --获取关联预约单号 --置预约单状态为已结帐 declare @bookid int --预约单号键值 select @bookid = idd from gwm01 where gwa00c = @compid and gwa12c = @gla01c if (@bookid IS not null) update gwm01 set gwa11i = 4 where idd = @bookid end GO /****** Object: View [dbo].[gicm14] Script Date: 06/12/2017 06:16:30 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create VIEW [dbo].[gicm14](gicn00i,gicn01v,gicn02v,gicn03v,gicn04v,gicn05v,gicn06i,gicn07i,gicn08t,gicn09t,gicn10v,gicn12v,gicn13d) as select idd,gwa00c,gwa05c,gwa07c,gwa06c,gwa22c,gwa11i,gwa23i,gwa02t,gwa03t,gwa16c,gwa13c,gwa01d from gwm01 GO /****** Object: StoredProcedure [dbo].[upg_get_appointment_unhandled] Script Date: 06/12/2017 06:20:04 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_get_appointment_unhandled] ( @compid varchar(10) -- 公司 ) as begin -- 创建临时表 create table #unread_gwa01_idd ( idd int not null ) insert into #unread_gwa01_idd(idd) select idd from gwm01 where gwa00c = @compid and readflag = 0 update gwm01 set readflag = 1 where idd in (select idd from #unread_gwa01_idd) select idd from #unread_gwa01_idd select idd, gwa01d, gwa02t, gwa02i, gwa03t, gwa03i, gwa04c, gwa05c, gwa06c, gwa07c, gwa16c, status, source, llid from gwm01 where gwa00c = @compid and status = 0 drop table #unread_gwa01_idd end GO /****** Object: StoredProcedure [dbo].[upg_stat_member_dispear] Script Date: 06/12/2017 06:20:44 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_stat_member_dispear] ( @compid varchar(10), -- 公司别 @fromcard varchar(15), -- 开始会员卡号 @tocard varchar(15), -- 截止会员卡号 @type int, -- 分析类型 1:多久没有来 2: 分析某个时间段没有来 @fromweek int, -- 多久没来(周)开始 @toweek int, -- 多久没来(周)截止 @disfromdate varchar(15), -- 没有来得时间段开始 @distodate varchar(15), -- 没有来的时间的截至 @cardtype varchar(10), -- 会员卡的类型 @fromdate varchar(10), -- 会员卡的销售开始时间 @todate varchar(10), -- 会员卡的销售截至时间 @onlyone int, -- 是否只来过1次 1:只统计来过一次 @addtoSMS int, -- 是否加入短信发送名单中 @addBack int, -- 是否加入回访名单 @fromemployee varchar(20), -- 负责业务人员 -- add by l 2015.10.20 @toemployees varchar(20), -- @num int -- 次数 ) as begin create table #result_member_gone ( idd int identity not null, -- 序号 cardid varchar(20) null, -- 会员卡号 memid varchar(20) null, -- 会员编号 m_name varchar(40) null, -- 会员姓名 gender varchar(4) null, -- 性别 lastdate varchar(8) null, -- 最后消费日期 days int null, -- 多少天没有来了 weeks float null, -- 多少周没有来了 tel varchar(20) null, -- 电话 mobile varchar(20) null, -- 手机 address varchar(160) null, -- 地址 tel_date varchar(8) null, -- 最后电话日期 cardtype varchar(10) null, -- 会员卡类型 saledate varchar(10) null, -- 销售日期 principal varchar(20) null, -- 负责业务 remain float null, -- 余额 compid varchar(20) null, --公司别 owncard int null, --本公司会员卡标志 1 本公司 onlyfill int null, --最后消费的日期 是卡充值的日期,而不是消费的日期,因为没有消费 primary key(idd) ) create table #final_result_member_gone ( idd int identity not null, -- 序号 cardid varchar(20) null, -- 会员卡号 cardtype varchar(5) null, --会员卡类别 --会员卡类别 ADD BY LEIJI on 2012年10月30日 memid varchar(20) null, -- 会员编号 m_name varchar(40) null, -- 会员姓名 gender varchar(4) null, -- 性别 lastdate varchar(8) null, -- 最后消费日期 days int null, -- 多少天没有来了 weeks float null, -- 多少周没有来了 tel varchar(20) null, -- 电话 mobile varchar(20) null, -- 手机 address varchar(160) null, -- 地址 tel_date varchar(8) null, -- 最后电话日期 saledate varchar(10) null, -- 销售日期 principal varchar(20) null, -- 负责业务 remain float null, -- 余额 compid varchar(20) null, --公司别 onlyfill int null, --最后消费的日期 是卡充值的日期,而不是消费的日期,因为没有消费 primary key(idd) ) create table #last_lookon ( memid varchar(20) not null, -- 会员编号 lastdate varchar(8) null, -- 最后电话拜访日期 primary key(memid) ) --add by lm 2015.10.20 create table #cardnum ( memid varchar(20) not null, -- 会员编号 num int null -- 消费次数 ) --add by lm 2015.10.20 create table #cardnum_sum ( memid varchar(20) not null, -- 会员编号 num int null -- 消费次数 ) --Added by maql 20100601 --考虑会员消费不入院操作的可能 create table #tbl_card_come_consume ( memid varchar(20) not null, --会员编号 srvdate varchar(8) null, --消费日期 ) create clustered index tbl_tmp_idx on #tbl_card_come_consume(srvdate) create table #gcm01 ( gca00c varchar(10) not null, --门店编号 gca01c varchar(20) not null, --卡号 gca02c varchar(5) not null, --卡种(Z散客,ZZ门店,ZZZ员工) gca05d varchar(8) null, --销售日期 gca04c varchar(20) null, --会员编号 gca13d varchar(10) null, --发行公司 gca20c varchar(10) null, --销售人员 primary key (gca00c,gca01c) ) DECLARE @dt1 datetime, @dt2 datetime declare @Seconds float SELECT @dt1 = getdate() insert #gcm01 (gca00c, gca01c, gca02c, gca04c, gca05d, gca13d, gca20c) select gca00c, gca01c, gca02c, gca04c, gca05d, gca13d, gca20c from gcm01 with(nolock) where gca00c = gca13d and ((gca05d >= @fromdate and gca05d <= @todate) or (@fromdate = '*')) and substring(gca01c, 1, 2) <> '散客' --会员入院 --统计到所有入院地方的会员编号 insert #tbl_card_come_consume(memid, srvdate) select distinct gla27c, gla02d from glm01 with (nolock), #gcm01 b with (nolock), gam26 z with (nolock) where isnull(gla27c, '')<>'' and isnull(gla27c, '')<>'散客' and gla00c = z.gaz02c and gla06c = b.gca01c and b.gca00c = b.gca13d and (( b.gca20c >= @fromemployee and b.gca20c <= @toemployees ) or @fromemployee = '*') and z.gaz01c = @compid and ((gla02d >= @fromdate ) or (@fromdate = '*')) --直接消费 --统计的到收银中消费的所有会员 insert #tbl_card_come_consume(memid, srvdate) select distinct gga05c, gga02d from ggm01 with(nolock), #gcm01 b with (nolock), gam26 z with (nolock) where isnull(gga05c, '')<>'' and isnull(gga05c, '')<>'散客' --and isnull(gga30c, '') = '' and gga00c = z.gaz02c and gga09c = b.gca01c and b.gca00c = b.gca13d and (( b.gca20c >= @fromemployee and b.gca20c <= @toemployees ) or @fromemployee = '*') and z.gaz01c = @compid and ((gga02d >= @fromdate ) or (@fromdate = '*')) --本门店消费次统计 add by lm 2015.10.20 insert #cardnum(memid, num) select gga05c, count(gga01c) from ggm01 with(nolock), #gcm01 b with (nolock), gam26 z with (nolock) where isnull(gga05c, '')<>'' and isnull(gga05c, '')<>'散客' -- and isnull(gga30c, '') = '' and gga00c = z.gaz02c and gga09c = b.gca01c and b.gca00c = b.gca13d and (( b.gca20c >= @fromemployee and b.gca20c <= @toemployees ) or @fromemployee = '*') and z.gaz01c = @compid and ((gga02d >= @fromdate ) or (@fromdate = '*')) group by gga05c --只统计了曾经来消费过的客户(包括别的公司的会员卡),以及最后的消费日期 --以会员编号为关键值 insert #result_member_gone(memid, lastdate) select memid, max(srvdate) from #tbl_card_come_consume group by memid --还需要考虑从来没有来消费的客户,包括卡销售的或充值的 --对于开账进来的卡,只能认定为开卡日期为最后的消费日期了 create table #tbl_sales_cards ( memid varchar(20) not null, lastdate varchar(10) null, ) create clustered index idx_tbl_cards_001 on #tbl_sales_cards(memid) insert #tbl_sales_cards(memid, lastdate ) select isnull(gna04c, ''), gna02d from gnm01 a with (nolock), #gcm01 b with (nolock), gam26 z with (nolock) where a.gna00c = z.gaz02c and a.gna13c = b.gca01c and b.gca00c = b.gca13d and (( b.gca20c >= @fromemployee and b.gca20c <= @toemployees ) or @fromemployee = '*') and z.gaz01c = @compid and ((gna02d >= @fromdate ) or (@fromdate = '*')) insert #tbl_sales_cards(memid, lastdate) select isnull(b.gca04c, ''), max(gcl02d) from gcm10 a with (nolock), #gcm01 b with (nolock), gam26 z with (nolock) where a.gcl00c = z.gaz02c and z.gaz01c = @compid and a.gcl03c = b.gca01c and b.gca00c = b.gca13d and (( b.gca20c >= @fromemployee and b.gca20c <= @toemployees ) or @fromemployee = '*') and ((gcl02d >= @fromdate ) or (@fromdate = '*')) group by isnull(b.gca04c, '') insert #tbl_sales_cards(memid, lastdate) select isnull(gcn15c, ''), gcn12d from gcm12 a with (nolock), #gcm01 b with (nolock), gam26 z with (nolock) where a.gcn00c = z.gaz02c and a.gcn01c = b.gca01c and b.gca00c = b.gca13d and (( b.gca20c >= @fromemployee and b.gca20c <= @toemployees ) or @fromemployee = '*') and z.gaz01c = @compid and ((gcn12d >= @fromdate ) or (@fromdate = '*')) --到此把所有卡销售充值,开账的会员找出来了 delete a from #tbl_sales_cards a, #result_member_gone b where a.memid = b.memid --删除已经在消费中的会员,没有的合并进来 --需要做上标志,最后一次是充值 insert #result_member_gone(memid, lastdate, onlyfill) select memid, max(lastdate), 1 from #tbl_sales_cards group by memid drop table #tbl_sales_cards -- 这个时候 #result_member_gone 中包括了所有需要统计的会员,部分的卡不是本归属公司的卡 -- 需要过滤掉,另外我们的卡也有可能到别的门店去消费,则需要更新最后的日期 create table #tbl_our_cards( cardid varchar(20) not null, memid varchar(20) null, cardtype varchar(20) null, -- 卡类别 primary key(cardid)) --可能有卡在两个不同的归属有归属,这样这里就有错误了 insert #tbl_our_cards(cardid, memid, cardtype) select distinct gca01c, gca04c, gca02c from #gcm01 a with (nolock), gam26 z with (nolock) where a.gca00c = a.gca13d and a.gca00c = z.gaz02c and (( a.gca20c >= @fromemployee and a.gca20c <= @toemployees ) or @fromemployee = '*') and z.gaz01c = @compid and substring(gca01c, 1, 2)<>'散客' update a set a.owncard = 1, a.cardtype = b.cardtype, a.cardid = b.cardid from #result_member_gone a, #tbl_our_cards b where a.memid = b.memid --把不是本公司的会员记录删除 delete #result_member_gone where isnull(owncard, 0) = 0 -- 不是本公司的 --这个时候,cardid, memid 都是有值的 --在别的店消费的明细数据 create table #tbl_other_consume_detail( idd int identity not null, cardid varchar(20) not null, srvdate varchar(20) null, primary key(idd)) insert #tbl_other_consume_detail(cardid, srvdate) select gct02c, gct04d from gcm20 a with (nolock), #result_member_gone b where a.gct02c = b.cardid and a.gct00c not in (select gaz02c from gam26 where gaz01c = @compid) and isnull(gct02c, '')<>'' --统计会员跨店消费次数 insert #cardnum(memid, num) select gct02c, count(gct14c) from gcm20 a with (nolock), #result_member_gone b where a.gct02c = b.cardid and a.gct00c not in (select gaz02c from gam26 where gaz01c = @compid) and isnull(gct02c, '')<>'' group by gct02c create table #tbl_other_consume( cardid varchar(20) not null, lastdate varchar(20) null, come_own int null, -- 是否来过自己的门店消费过 primary key(cardid)) --在别的公司的消费的最后日期 insert #tbl_other_consume(cardid, lastdate) select cardid, max(srvdate) from #tbl_other_consume_detail group by cardid --在本公司是消费过来的 update a set a.come_own = 1 from #tbl_other_consume a, #result_member_gone b where a.cardid = b.cardid -- 更新到原来的表中,最后消费日期比原来的大的话 update a set a.lastdate = b.lastdate, onlyfill = 0 from #result_member_gone a, #tbl_other_consume b where a.cardid = b.cardid and b.lastdate > a.lastdate and isnull(b.come_own, 0) = 1 --把本公司没有来消费过的卡插进来 insert #result_member_gone(cardid, lastdate) select cardid, lastdate from #tbl_other_consume where isnull(come_own, 0) = 0 --只在别的公司消费的卡,因为新增加进来所以相关的字段都是空的,要更新下 update a set a.cardtype = b.cardtype, a.memid = b.memid from #result_member_gone a, #tbl_our_cards b where a.cardid = b.cardid and isnull(owncard, 0) = 0 -- 因为新增加进来,所以标志还为null drop table #tbl_our_cards if @type = 2 begin -- 找出这个时间段来的客户,包括在本店来的,以及在别的店来的会员 create table #tbl_comes( codeid varchar(20)) -- 会员编号或会员卡号 --删除本店在这段时间段内来消费过的(收银)的会员 insert #tbl_comes(codeid) select distinct memid from #tbl_card_come_consume where srvdate >= @disfromdate and srvdate <= @distodate delete a from #result_member_gone a, #tbl_comes b where a.memid = b.codeid --删除到别的店消费过的会员,必须现在在这个时间段内的 delete #tbl_comes insert #tbl_comes(codeid) select distinct cardid from #tbl_other_consume_detail where srvdate >= @disfromdate and srvdate <= @distodate delete a from #result_member_gone a, #tbl_comes b where a.memid = b.codeid drop table #tbl_comes --留下的就是这段时间没有来的客户 end drop table #tbl_card_come_consume drop table #tbl_other_consume drop table #tbl_other_consume_detail update a set days = datediff(dd, lastdate, getdate()) from #result_member_gone a update a set a.m_name = b.gba03c, a.tel = b.gba07c, a.mobile = b.gba08c, a.address = b.gba05c, a.weeks = days/7.0, a.gender = case when b.gba14i = 1 then '男' else '女' end, a.principal = b.gba10c from #result_member_gone a, gbm01 b with (nolock), gam26 z with (nolock) where a.memid = b.gba01c and b.gba00c = z.gaz02c and z.gaz01c = @compid --更新会员卡的类型与销售日期 update a set a.cardtype = b.gca02c, a.saledate = b.gca05d, a.remain = c.gcc06f from #result_member_gone a, #gcm01 b with (nolock), gcm03 c with (nolock) where a.cardid = b.gca01c and b.gca00c = b.gca13d and b.gca00c = c.gcc00c and b.gca01c = c.gcc01c and c.gcc03i = 2 --删除已经被删除会员卡的客户 delete #result_member_gone where isnull(cardtype, '') = '' insert #last_lookon(memid, lastdate) select gbf01c, max(gbf04c) from gbm06 with (nolock), gam26 z with (nolock) where gbf00c = z.gaz02c and z.gaz01c = @compid group by gbf01c update a set a.tel_date = b.lastdate from #result_member_gone a, #last_lookon b where a.memid = b.memid --会员卡类型 --处理查询条件 if @cardtype<>'*' and @cardtype<>'' begin delete #result_member_gone where cardtype<>@cardtype end if @fromdate<>'*' and @fromdate<>'' begin delete #result_member_gone where saledate<@fromdate or saledate>@todate or isnull(saledate, '') = '' end if @onlyone = 1 begin delete #result_member_gone where lastdate>saledate insert #cardnum(memid, num) select cardid, 0 from #result_member_gone end if @fromweek = -1 or @type = 2 begin if @fromcard = '*' begin insert #final_result_member_gone(cardid, memid, m_name, gender, lastdate, days, weeks, tel, mobile, address, tel_date, saledate, principal, remain, onlyfill ) select cardid, memid, m_name, gender, lastdate, days, weeks, tel, mobile, address, tel_date, saledate, principal, remain, onlyfill from #result_member_gone order by days desc end else begin insert #final_result_member_gone(cardid, memid, m_name, gender, lastdate, days, weeks, tel, mobile, address, tel_date, saledate, principal, remain, onlyfill ) select cardid, memid, m_name, gender, lastdate, days, weeks, tel, mobile, address, tel_date, saledate, principal, remain, onlyfill from #result_member_gone where cardid >= @fromcard and cardid <= @tocard order by days desc end end else begin if @fromcard = '*' begin insert #final_result_member_gone(cardid, memid, m_name, gender, lastdate, days, weeks, tel, mobile, address, tel_date, saledate, principal, remain, onlyfill ) select cardid, memid, m_name, gender, lastdate, days, weeks, tel, mobile, address, tel_date, saledate, principal, remain, onlyfill from #result_member_gone where weeks >= @fromweek and weeks <= @toweek order by days desc end else begin insert #final_result_member_gone(cardid, memid, m_name, gender, lastdate, days, weeks, tel, mobile, address, tel_date, saledate, principal, remain, onlyfill ) select cardid, memid, m_name, gender, lastdate, days, weeks, tel, mobile, address, tel_date, saledate, principal, remain, onlyfill from #result_member_gone where weeks >= @fromweek and weeks <= @toweek and cardid >= @fromcard and cardid <= @tocard order by days desc end end if @addtoSMS = 1 begin declare @cur_max float select @cur_max = max(gxa01f) from gxm01 with (nolock) where gxa00c = @compid select @cur_max = isnull(@cur_max, 0) insert gxm01(gxa00c, gxa01f, gxa02c, gxa03c ) select @compid, idd = @cur_max + idd, memid, mobile from #final_result_member_gone where memid not in (select gxa02c from gxm01 with (nolock) where gxa00c = @compid) and isnull(memid, '')<>'' end if @addBack = 1 begin declare @cur_date varchar(8) select @cur_date = datename(yy, getdate()) + datename(mm, getdate()) + datename(dd, getdate()) insert gbm10(gbj00c, gbj04c, gbj06c, gbj08d, gbj10i, gbj13i, gbj12c, gbj14c, gbj07d, gbj15c) select @compid, memid, tel, @cur_date, 0, 1, cast(days as varchar(10)) + '天没有来消费了', m_name, lastdate, mobile from #final_result_member_gone a where not exists(select 1 from gbm10 where gbj00c = @compid and gbj04c = a.memid and isnull(gbj06c, '') = isnull(a.tel, '') and gbj08d = @cur_date and gbj10i = 0) end update a set a.cardtype = b.gca02c, a.compid = gca13d from #final_result_member_gone a, gcm01 b with (nolock) where a.cardid = b.gca01c and b.gca00c = b.gca13d --统计会员卡消费总次数 insert #cardnum_sum(memid, num) select memid, sum(num) from #cardnum group by memid select idd = isnull(a.idd,''), cardid = isnull(a.cardid,''), cardtype = isnull(a.cardtype,''), memid = isnull(a.memid,''), m_name = isnull(a.m_name,''), gender = isnull(a.gender,''), lastdate = isnull(a.lastdate,''), days = isnull(a.days,''), weeks = isnull(a.weeks,''), tel = isnull(a.tel,''), mobile = isnull(a.mobile,''), address = isnull(a.address,''), tel_date = isnull(a.tel_date,''), saledate = isnull(a.saledate,''), principal = isnull(a.principal,''), remain = isnull(a.remain,''), compid = isnull(a.compid,''), onlyfill = isnull(a.onlyfill,''), num = isnull(b.num,'') from #final_result_member_gone a, #cardnum_sum b where a.memid = b.memid and b.num >= @num drop table #final_result_member_gone drop table #result_member_gone drop table #last_lookon drop table #gcm01 end GO /****** Object: StoredProcedure [dbo].[upg_get_mem_by_comid_type] Script Date: 06/12/2017 06:20:08 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_get_mem_by_comid_type] ( @opercompid varchar(10), -- 操作门店 @comid varchar(10), -- 会员所在门店 @ctype varchar(5) ) as begin create table #gxm01 -- 短信--选择的客户 ( gxa00c varchar(10) not null, -- 公司别 gxa01f float not null, -- 序号 gxa02c varchar(20) null, -- 会员编号 gxa03c varchar(20) null, -- 手机号码 name varchar(40) null, -- 会员姓名 addr varchar(160) null, -- 地址 area varchar(30) null, -- 居住区域 gxa04c varchar(10) null, -- 类型(0-会员 1-员工) ) declare @PK_Sql nvarchar(max) set @PK_Sql = ' alter table #gxm01 add constraint PK_#gxm01_' + convert(varchar(10), cast(floor(rand() * 1000000) as int)) + ' primary key(gxa00c, gxa01f) ' exec sp_executesql @PK_Sql declare @g01f float select @g01f = isnull(max(gxa01f), 0) from gxm01 where gxa00c = @comid declare @strSql Nvarchar(MAX) set @strSql = 'insert #gxm01(gxa00c, gxa01f, gxa02c, gxa03c, name, addr, area, gxa04c)' + ' select distinct ''' + @opercompid + ''', gxa01f = (Row_Number() over(order by gca01c desc) + ' + cast(@g01f as varchar(20)) + '), gca04c, gba08c, gba03c, gba05c, gba25c, 0' + ' from gcm01 left join gbm01 ' + ' on gba00c = gca00c and gba01c = gca04c ' + ' where gca00c = gca13d' + ' and gca08i In (2, 3, 4, 5) and isnull(gca04c, '''')<>'''' and isnull(gba43c, ''1'') = 1' + ' and gca04c<>''散客'' and isnull(gca04c, '''')<>'''' ' if (@comid <> '') begin set @strSql = @strSql + ' and gca00c = ''' + @comid + '''' end if (@ctype <> '') begin set @strSql = @strSql + ' and gca02c = ''' + @ctype + '''' end EXEC sp_executesql @strSql select * from #gxm01 drop table #gxm01 end GO /****** Object: StoredProcedure [dbo].[upg_load_save_gxm01] Script Date: 06/12/2017 06:20:17 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_load_save_gxm01] ( @compid varchar(10), -- 公司别 @c_name varchar(20), -- 名单 @isload int -- 是否为载入 ) as begin if @isload = 1 begin delete gxm01 where gxa00c = @compid insert gxm01(gxa00c, gxa01f, gxa02c, gxa03c) select gxf00c, gxf02f, gxf03c, gxf04c from gxm06 where gxf00c = @compid and gxf01c = @c_name and isnull(gxf04c, '') <> '' end else begin -- 保存 delete gxm06 where gxf00c = @compid and gxf01c = @c_name insert gxm06(gxf00c, gxf01c, gxf02f, gxf03c, gxf04c) select gxa00c, @c_name, gxa01f, gxa02c, gxa03c from gxm01 where gxa00c = @compid and isnull(gxa03c, '') <> '' end end GO /****** Object: StoredProcedure [dbo].[upg_get_mem_by_comid] Script Date: 06/12/2017 06:20:07 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_get_mem_by_comid] ( @opercompid varchar(10), -- 操作门店 @comid varchar(10) -- 会员所在门店 ) as begin create table #gxm01 -- 短信--选择的客户 ( gxa00c varchar(10) not null, -- 公司别 gxa01f float not null, -- 序号 gxa02c varchar(20) null, -- 会员编号 gxa03c varchar(20) null, -- 手机号码 name varchar(40) null, -- 会员姓名 addr varchar(160) null, -- 地址 area varchar(30) null, -- 居住区域 gxa04c varchar(10) null, -- 类型(0-会员 1-员工) ) declare @PK_Sql nvarchar(max) set @PK_Sql = ' alter table #gxm01 add constraint PK_#gxm01_' + convert(varchar(10), cast(floor(rand() * 1000000) as int)) + ' primary key(gxa00c, gxa01f) ' exec sp_executesql @PK_Sql declare @g01f float select @g01f = isnull(max(gxa01f), 0) from gxm01 where gxa00c = @comid or @comid = '*' if @comid = '' set @comid = '*' -- 有卡的会员 insert #gxm01(gxa00c, gxa01f, gxa02c, gxa03c, name, addr, area, gxa04c) select distinct @opercompid, gxa01f = (Row_Number() over(order by gca01c desc) + @g01f), b.gca04c, a.gba08c, a.gba03c, a.gba05c, a.gba25c, 0 from gbm01 a right join gcm01 b on a.gba00c = b.gca00c and a.gba01c = b.gca04c and b.gca00c = b.gca13d where isnull(a.gba43c, '1') = 1 and (gba00c = @comid or @comid = '*') and gca04c <> '散客' and isnull(gca04c, '') <> '' -- 无卡的会员 select @g01f = isnull(max(gxa01f), 0) + 1 from #gxm01 -- where gxa00c = @comid or @comid = '*' insert #gxm01(gxa00c, gxa01f, gxa02c, gxa03c, name, addr, area, gxa04c) select distinct @opercompid, gxa01f = (Row_Number() over(order by gba01c desc) + @g01f), a.gba01c, a.gba08c, a.gba03c, a.gba05c, a.gba25c, 0 from gbm01 a where isnull(gba23c, '') = '' and (gba00c = @comid or @comid = '*') and isnull(gba43c, '1') = 1 and gba01c <> '散客' and isnull(gba01c, '') <> '' select * from #gxm01 drop table #gxm01 end GO /****** Object: StoredProcedure [dbo].[upg_delete_gxm01] Script Date: 06/12/2017 06:19:59 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_delete_gxm01] ( @compid varchar(10) ) as --Mzh 2005/12/22 begin create table #message ( comp varchar(10) not null, -- 公司别 flowno float not null, -- 序号 customer varchar(20) null, -- 客户编号 phone varchar(20) null, -- 手机号码 primary key(comp, flowno) ) declare @phone varchar(20) declare cur_distinct_phone cursor for select distinct gxa03c from gxm01 where gxa00c = @compid open cur_distinct_phone fetch next from cur_distinct_phone into @phone while (@@fetch_status = 0) begin insert #message(comp, flowno, customer, phone) select top 1 * from gxm01 where gxa00c = @compid and gxa03c = @phone fetch next from cur_distinct_phone into @phone end close cur_distinct_phone deallocate cur_distinct_phone delete from gxm01 where gxa00c = @compid insert gxm01(gxa00c, gxa01f, gxa02c, gxa03c) select * from #message drop table #message end GO /****** Object: StoredProcedure [dbo].[upg_m_dispatch_gbm20] Script Date: 06/12/2017 06:20:18 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_m_dispatch_gbm20] ( @fromcompid varchar(10), -- 从哪一个公司发布 @tocompid varchar(10), -- 目的公司 @mandatory varchar(2) -- 是否强制统一 ) as begin create table #gbm20_01 --供应商基本资料 ( gbt00c varchar(10) not null, --公司别 gbt01c varchar(20) not null, --供应商ID gbt02c varchar(40) null, --供应商中文名称 gbt03c varchar(30) null, --供应商中文简称 gbt04c varchar(40) null, --供应商英文名称 gbt05c varchar(20) null, --供应商英文简称 gbt06i int null, --供应商类别, 详细见 gsm02 gbt07c varchar(20) null, --电话 gbt08c varchar(20) null, --传真 gbt09c varchar(20) null, --电子邮件 gbt10c varchar(20) null, --主页 gbt11c varchar(20) null, --qq号码 gbt12c varchar(20) null, --msn号码 gbt13c varchar(80) null, --供应商发票地址 gbt14c varchar(80) null, --供应商发票英文地址 gbt15c varchar(20) null, --邮政编码 gbt16c varchar(40) null, --供应商备注 gbt17c varchar(40) null, --主要联系人姓名 ) declare @PK_Sql nvarchar(max) set @PK_Sql = ' alter table #gbm20_01 add constraint PK_#gbm20_01_' + convert(varchar(10), cast(floor(rand() * 1000000) as int)) + ' primary key clustered(gbt00c, gbt01c)' exec sp_executesql @PK_Sql insert #gbm20_01(gbt00c, gbt01c, gbt02c, gbt03c, gbt04c, gbt05c, gbt06i, gbt07c, gbt08c, gbt09c, gbt10c, gbt11c, gbt12c, gbt13c, gbt14c, gbt15c, gbt16c, gbt17c) select gbt00c, gbt01c, gbt02c, gbt03c, gbt04c, gbt05c, gbt06i, gbt07c, gbt08c, gbt09c, gbt10c, gbt11c, gbt12c, gbt13c, gbt14c, gbt15c, gbt16c, gbt17c from gbm20 where gbt00c = @fromcompid if @mandatory = '1' begin delete gbm20 where gbt00c = @tocompid end else begin delete a from #gbm20_01 a, gbm20 b where a.gbt00c = @fromcompid and a.gbt01c = b.gbt01c and b.gbt00c = @tocompid end insert gbm20(gbt00c, gbt01c, gbt02c, gbt03c, gbt04c, gbt05c, gbt06i, gbt07c, gbt08c, gbt09c, gbt10c, gbt11c, gbt12c, gbt13c, gbt14c, gbt15c, gbt16c, gbt17c) select @tocompid, gbt01c, gbt02c, gbt03c, gbt04c, gbt05c, gbt06i, gbt07c, gbt08c, gbt09c, gbt10c, gbt11c, gbt12c, gbt13c, gbt14c, gbt15c, gbt16c, gbt17c from #gbm20_01 where gbt00c = @fromcompid drop table #gbm20_01 end GO /****** Object: StoredProcedure [dbo].[upg_m_dispatch_satisfaction] Script Date: 06/12/2017 06:20:22 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_m_dispatch_satisfaction] ( @fromcompid varchar(10), -- 从哪一个公司发布 @tocompid varchar(10), -- 目的公司 @mandatory varchar(2) -- 是否强制统一 ) as begin create table #satisfaction ( sf00c varchar(10) not null, -- 公司编号 sf01c varchar(10) not null, -- 图片文件名对应的索引 sf02c varchar(10) null, -- 满意度 ) declare @PK_Sql nvarchar(max) set @PK_Sql = ' alter table #satisfaction add constraint PK_#satisfaction_' + convert(varchar(10), cast(floor(rand() * 1000000) as int)) + ' primary key clustered(sf00c, sf01c)' exec sp_executesql @PK_Sql insert #satisfaction(sf00c, sf01c, sf02c) select sf00c, sf01c, sf02c from satisfaction where sf00c = @fromcompid if @mandatory = '1' begin delete satisfaction where sf00c = @tocompid end else begin delete a from #satisfaction a, satisfaction b where a.sf00c = @fromcompid and a.sf01c = b.sf01c and b.sf00c = @tocompid end insert satisfaction(sf00c, sf01c, sf02c) select @tocompid, sf01c, sf02c from #satisfaction where sf00c = @fromcompid drop table #satisfaction end GO /****** Object: StoredProcedure [dbo].[upg_m_dispatch_gxm02] Script Date: 06/12/2017 06:20:20 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_m_dispatch_gxm02] ( @fromcompid varchar(10), -- 从哪一个公司发布 @tocompid varchar(10), -- 目的公司 @mandatory varchar(2) -- 是否强制统一 ) as begin create table #gxm02 -- 短信--短信内容 ( gxb00c varchar(10) not null, -- 公司别 gxb01f float not null, -- 序号 gxb02c varchar(5) null, -- 短信类别 gxb03c varchar(1000) null, -- 短信内容([1]:会员) gxb04i int null, -- 本次选中(0;没有选择;1:要求发送) ) declare @PK_Sql nvarchar(max) set @PK_Sql = ' alter table #gxm02 add constraint PK_#gxm02_' + convert(varchar(10), cast(floor(rand() * 1000000) as int)) + ' primary key(gxb00c, gxb01f)' exec sp_executesql @PK_Sql insert #gxm02(gxb00c, gxb01f, gxb02c, gxb03c, gxb04i) select gxb00c, gxb01f, gxb02c, gxb03c, gxb04i from gxm02 where gxb00c = @fromcompid if @mandatory = '1' begin delete gxm02 where gxb00c = @tocompid end else begin delete a from #gxm02 a, gxm02 b where a.gxb00c = @fromcompid and a.gxb01f = b.gxb01f and b.gxb00c = @tocompid end insert gxm02(gxb00c, gxb01f, gxb02c, gxb03c, gxb04i) select @tocompid, gxb01f, gxb02c, gxb03c, gxb04i from #gxm02 where gxb00c = @fromcompid drop table #gxm02 end GO /****** Object: StoredProcedure [dbo].[upg_auto_po_from_center2] Script Date: 06/12/2017 06:19:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_auto_po_from_center2]( @compid varchar(10), -- 总公司别 @billid varchar(20), -- 门店入库单号 @centerid varchar(20)) -- 总部出库单号 as begin insert ghm01(gha00c, gha01c, gha03d, gha04c, gha05c, gha06c, gha07i, gha08c, gha91c, gha92d, gha93c, gha94d, gha09t, gha11c) select gia05c, @billid + '-' + gia05c, gia03d, gia04c, gia05c, '', 4, gia01c, gia91c, gia92d, '', '', gia08t, gia10c from gim01 a where gia09i = 2 and gia00c = @compid and gia01c = @centerid insert ghm02(ghb00c, ghb01c, ghb02f, ghb03c, ghb04c, ghb05f, ghb06c, ghb07f, ghb08c, ghb09f, ghb10f, ghb11f, ghb12c, ghb13f, ghb14f) select gia05c, @billid + '-' + gia05c, gib02f, gib03c, gib04c, isnull(gib08f, 0), gib06c, isnull(gib07f, 0), 'RMB', 4, isnull(gib09f, 0), isnull(gib10f, 0), '', isnull(gib08f, 0), isnull(gib11f, 0) from gim02 b with (nolock), gim01 a with (nolock) where b.gib00c = a.gia00c and b.gib01c = a.gia01c and a.gia09i = 2 and a.gia00c = @compid and a.gia01c = @centerid end GO /****** Object: StoredProcedure [dbo].[upg_auto_po_from_center] Script Date: 06/12/2017 06:19:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_auto_po_from_center] ( @compid varchar(10), -- 总公司别 @billid varchar(100), -- 门店入库单号 @centerid varchar(100), -- 总部出库单号 @Suppliers varchar(20), -- 供应商代码 @type int, -- 出库性质 add by leiji on 2012年10月15日 @incompid varchar(10) -- 入库到的公司编号 ) as begin set nocount on -- 当前的事务数 declare @__transaction_count int set @__transaction_count = @@trancount -- try...catch处理 begin try -- a.开启事务,或者设置事务保存点 if @__transaction_count = 0 begin tran else save tran __tran_SavePoint insert ghm01(gha00c, gha01c, gha03d, gha04c, gha05c, gha06c, gha07i, gha08c, gha91c, gha92d, gha93c, gha94d, gha09t, gha11c) select gia05c, @billid + '(' + @compid + '-' + @incompid + ')', gia03d, gia04c, gia05c, '', @type, gia01c, gia91c, gia92d, '', '', gia08t, gia10c from gim01 a where gia09i = 2 and gia00c = @compid and gia01c = @centerid insert ghm02(ghb00c, ghb01c, ghb02f, ghb03c, ghb04c, ghb05f, ghb06c, ghb07f, ghb08c, ghb09f, ghb10f, ghb11f, ghb12c, ghb13f, ghb14f) select gia05c, @billid + '(' + @compid + '-' + @incompid + ')', gib02f, gib03c, gib04c, gib08f, gib06c, gib07f, 'RMB', 1, gib09f, gib10f, @Suppliers, gib08f, gib11f from gim02 b with (nolock), gim01 a with (nolock) where b.gib00c = a.gia00c and b.gib01c = a.gia01c and a.gia09i = 2 and a.gia00c = @compid and a.gia01c = @centerid -- 提交事务 -- 有可提交的事务, 并且事务是在当前模块中开启的情况下, 才提交事务 if xact_state() = 1 and @__transaction_count = 0 commit -- 为了防止TRY 中有遗漏的事务处理, 可以在TRY 模拟的结束部分做最终的判断 if @__transaction_count = 0 begin if xact_state() = -1 rollback tran else begin while @@trancount > 0 commit tran end end end try begin catch -- 在CATCH 模块, 应该首先处理事务 if xact_state() <> 0 begin if @__transaction_count = 0 rollback tran -- xact_state 为-1 时, 不能回滚到事务保存点, 这种情况留给外层调用者做统一的事务回滚 -- 通过@@trancount > @__transaction_count 的判断, 即使在TRY 模块中没有设置事务保存点的情况下跳到此步骤, 也不会出错 else if xact_state() = 1 and @@trancount > @__transaction_count rollback tran __tran_SavePoint end declare @SP_Parameters varchar(max) set @SP_Parameters = '@compid = ' + isnull(@compid, '') + '@billid = ' + isnull(@billid, '') + '@centerid = ' + isnull(@centerid, '') + '@Suppliers = ' + isnull(@Suppliers, '') + '@type = ' + isnull(@type, '') + '@incompid = ' + isnull(@incompid, '') insert into SP_TranLog(SPName, SPTime, SPParameter) values('upg_auto_po_from_center', getdate(), @SP_Parameters) return -1 end catch end GO /****** Object: StoredProcedure [dbo].[upg_book_overtime] Script Date: 06/12/2017 06:19:30 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_book_overtime] @compid varchar(10), --公司别 @time varchar(6), --时间 @date varchar(8)--日期 as--MXQ 2005-1-15 begin update gicm01 set gica23i = '2' where gica00c = @compid and (gica19t <= @time and gica21d = @date or gica21d<@date) and gica23i = '0' end GO /****** Object: StoredProcedure [dbo].[upg_book_status] Script Date: 06/12/2017 06:19:30 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_book_status] @compid varchar(10), --公司别 @bookid varchar(20), --预约单号 @status int --状态 as--MXQ 2005-1-29 begin update gicm01 set gica23i = @status where gica00c = @compid and gica01v = @bookid end GO /****** Object: StoredProcedure [dbo].[upg_point_analysis] Script Date: 06/12/2017 06:20:27 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_point_analysis] ( @compid varchar(10), @depid varchar(10), @cardcls varchar(5), @cardfrom varchar(20), @cardto varchar(20), @prjfrom varchar(20), @prjto varchar(20), @datefrom varchar(8), @dateto varchar(8), @salesman varchar(20) ) as begin if isnull(@cardfrom, '') = '' set @cardfrom = '*' if isnull(@depid, '') = '' set @depid = '*' if isnull(@prjfrom, '') = '' set @prjfrom = '*' if isnull(@datefrom, '') = '' set @datefrom = '*' if isnull(@salesman, '') = '' set @salesman = '*' if isnull(@cardcls, '') = '' set @cardcls = '*' create table #temp_1 ( cardid varchar(20) null, --卡号 point_src int null, --积分来源:0-卡销售,1-卡充值,2-收银,3-介绍客户 point_type int null, --积分类型:0-积累,1-消费 point float null, --积分 point_date varchar(8) null, --日期 prjid varchar(20) null, --消费项目代码 prjname varchar(30) null --消费项目名称 ) --卡销售所产生积分 insert into #temp_1(cardid, point_src, point_type, point, point_date, prjid, prjname) select gnb03c, 0, 0, a = (case when gak30i = 1 then gak31i when gak30i = 2 then gnb08f*isnull(gak31i, 0) else gnb05f*isnull(gak31i, 0) end), gna80d, '', '' from gnm01, gnm02, gam10, gcm01 where gna00c = gnb00c and gna01c = gnb01c and (gna81c = @depid or @depid = '*') and gnb00c = gak00c and gnb02c = gak01c and gna00c = @compid and (gnb03c between @cardfrom and @cardto or @cardfrom = '*') and (gna80d between @datefrom and @dateto or @datefrom = '*') and gnb00c = gca00c and gnb03c = gca01c and gca13d = @compid--发行公司为本公司 and (gca20c = @salesman or @salesman = '*') and (gnb02c = @cardcls or @cardcls = '*') --卡充值所产生积分--仅考虑储值帐户充值产生积分 insert into #temp_1(cardid, point_src, point_type, point, point_date, prjid, prjname) select gcl03c, 1, 0, a = case when gak30i = 1 then gak31i when gak30i = 2 then gcl65f * isnull(gak31i, 0) else gcl65f * isnull(gak31i, 0) end, gcl80d, '', '' from gcm01 with (nolock), gcm10 with (nolock), gam10 where gcl00c = gca00c and gcl03c = gca01c and (gcl81c = @depid or @depid = '*') and gca00c = gak00c and gca02c = gak01c and gcl00c = @compid and gca13d = @compid--发行公司为本公司 and (gcl03c between @cardfrom and @cardto or @cardfrom = '*') and (gcl80d between @datefrom and @dateto or @datefrom = '*') and (gca20c = @salesman or @salesman = '*') and (gca02c = @cardcls or @cardcls = '*') --卡消费所产生积分 insert into #temp_1(cardid, point_src, point_type, point, point_date, prjid, prjname) select gga09c, 2, 0, ggb33f, gga80d, ggb03c, gda03c from ggm01, ggm02, gdm01, gcm01 where gga00c = ggb00c and gga01c = ggb01c and (gga09c between @cardfrom and @cardto or @cardfrom = '*') and (gga80d between @datefrom and @dateto or @datefrom = '*') and gga00c = @compid and (ggb81c = @depid or @depid = '*') and ggb00c = gda00c and ggb03c = gda01c and gga00c = gca00c and gga09c = gca01c and gca13d = @compid--发行公司为本公司 and (gca20c = @salesman or @salesman = '*') and (gca02c = @cardcls or @cardcls = '*') --介绍客户产生积分 create table #point_intro_net( idd int not null, card_type varchar(5) not null, --会员卡类别 lvl int not null, -- 层级 src_card varchar(20) null, -- 原始客户会员卡 intro_card varchar(20) null, -- 介绍来会员卡 card_amt float null, -- 介绍来卡的金额 ratio float null, -- 提佣比率 amt float null, -- 提佣金额 is_saved int null -- 是否已经给客户提佣 primary key(idd)) declare @card varchar(20) declare cur_gcm01 cursor for select gca01c from gcm01 with (nolock) where gca00c = @compid and (gca01c between @cardfrom and @cardto or @cardfrom = '*') open cur_gcm01 fetch next from cur_gcm01 into @card while @@fetch_status = 0 begin insert into #point_intro_net(idd, card_type, lvl, src_card, intro_card, card_amt, ratio, amt, is_saved) exec upg_expand_net @compid, @card, 1 fetch next from cur_gcm01 into @card end close cur_gcm01 deallocate cur_gcm01 create table #temp_2 ( cardid varchar(20) null, --卡号 point_src int null, --积分来源:0-卡销售,1-卡充值,2-收银,3-介绍客户, -1 - 消费 point_type int null, --积分类型:0-积累,1-消费 point float null, --积分 point_date varchar(8) null, --日期 introcard varchar(20) null --被介绍卡 ) insert into #temp_2(cardid, point_src, point_type, point, point_date, introcard) select src_card, 3, 0, amt, '', intro_card from #point_intro_net, gcm01 where gca00c = @compid and gca13d = @compid--发行公司为本公司 and gca01c = src_card and (gca02c = @cardcls or @cardcls = '*') update #temp_2 set point_date = gca05d from #temp_2, gcm01 where introcard = gca01c and gca00c = @compid insert into #temp_1(cardid, point_src, point_type, point, point_date, prjid, prjname) select #temp_2.cardid, #temp_2.point_src, #temp_2.point_type, #temp_2.point, #temp_2.point_date, '', '' from #temp_2 where (#temp_2.point_date between @datefrom and @dateto or @datefrom = '*') drop table #temp_2 drop table #point_intro_net --积分消费 insert into #temp_1(cardid, point_src, point_type, point, point_date, prjid, prjname) select gga09c, '-1', 1, ggb11f, gga80d, ggb03c, gda03c from ggm01, ggm02, gdm01, gcm01 where gga00c = ggb00c and gga01c = ggb01c and ggb27c = '7' and (gga09c between @cardfrom and @cardto or @cardfrom = '*') and (gga80d between @datefrom and @dateto or @datefrom = '*') and gga00c = @compid and (ggb81c = @depid or @depid = '*') and ggb00c = gda00c and ggb03c = gda01c and gga00c = gca00c and gga09c = gca01c and gca13d = @compid--发行公司为本公司 and (gca20c = @salesman or @salesman = '*') and (gca02c = @cardcls or @cardcls = '*') --跨店充值的情况 insert into #temp_1(cardid, point_src, point_type, point, point_date, prjid, prjname) select gcl03c, 1, 0, a = case when gak30i = 1 then gak31i when gak30i = 2 then gcl65f * isnull(gak31i, 0) else gcl65f * isnull(gak31i, 0) end, gcl80d, '', '' from gcm01 with (nolock), gcm10 with (nolock), gam10 where gca00c = @compid and gca13d = @compid--发行公司为本公司 and gcl03c = gca01c and (gcl81c = @depid or @depid = '*') and gca00c = gak00c and gca02c = gak01c and gcl00c <> @compid and (gcl03c between @cardfrom and @cardto or @cardfrom = '*') and (gcl80d between @datefrom and @dateto or @datefrom = '*') and (gca20c = @salesman or @salesman = '*') and (gca02c = @cardcls or @cardcls = '*') --跨店消费的情况 insert into #temp_1(cardid, point_src, point_type, point, point_date, prjid, prjname) select gga09c, 2, 0, ggb33f, gga80d, ggb03c, gda03c from ggm01, ggm02, gdm01, gcm01 where gga00c = ggb00c and gga01c = ggb01c and (gga09c between @cardfrom and @cardto or @cardfrom = '*') and (gga80d between @datefrom and @dateto or @datefrom = '*') and gga00c <> @compid and (ggb81c = @depid or @depid = '*') and ggb00c = gda00c and ggb03c = gda01c and gca00c = @compid and gca13d = @compid--发行公司为本公司 and gga09c = gca01c and (gca20c = @salesman or @salesman = '*') and (gca02c = @cardcls or @cardcls = '*') --跨店积分消费 insert into #temp_1(cardid, point_src, point_type, point, point_date, prjid, prjname) select gga09c, '-1', 1, ggb11f, gga80d, ggb03c, gda03c from ggm01, ggm02, gdm01, gcm01 where gga00c = ggb00c and gga01c = ggb01c and ggb27c = '7' and (gga09c between @cardfrom and @cardto or @cardfrom = '*') and (gga80d between @datefrom and @dateto or @datefrom = '*') and gga00c <> @compid and (ggb81c = @depid or @depid = '*') and ggb00c = gda00c and ggb03c = gda01c and gca00c = @compid and gca13d = @compid--发行公司为本公司 and gga09c = gca01c and (gca20c = @salesman or @salesman = '*') and (gca02c = @cardcls or @cardcls = '*') select * from #temp_1 where cast(isnull(point, 0) as int)<>0 order by cardid asc drop table #temp_1 end GO /****** Object: StoredProcedure [dbo].[upg_check_card_validate] Script Date: 06/12/2017 06:19:40 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_check_card_validate] ( @compid varchar(10), -- 公司别 @date varchar(8) -- 日期 ) as--WUXM 2004/12/05 begin declare @state int -- 状态 -- 将已开卡('4')或正在使用中('5') 中 且已经到达保留期的会员卡设定为 会籍保留( '10') select @state = 10 update gcm01 set gca08i = @state where gca00c = @compid and isnull(gca16d, '') <= @date and isnull(gca17d, '') >= @date and (gca08i = 5 or gca08i = 4) -- 将已经超过保留期的会员卡恢复为正常使用 select @state = 5 update gcm01 set gca08i = @state where gca00c = @compid and isnull(gca17d, '') < @date and gca08i = 10 -- 更新所有可能被状态修改错误的卡的状态修正过来 update gcm01 set gca08i = 4 where gca00c = @compid and (gca03i = 1 or gca03i = 2) and isnull(gca07d, '') <> '' and isnull(gca07d, '') >= @date and gca08i = 8 -- 将已开卡('4')或正在使用中('5') 且已经超过有效期的会员卡设定为 已过期( '8') select @state = 8 update gcm01 set gca08i = @state where gca00c = @compid and isnull(gca07d, '') <> '' and isnull(gca07d, '') < @date and (gca08i = 5 or gca08i = 4) -- 将已开卡('4')或正在使用中('5') 且使用次数超过次数限制 计次卡设定为 已过期( '8') -- select @state = 8 -- -- update gcm01 -- set gca08i = @state -- where gca00c = @compid -- and isnull(gca15i, 0) >= isnull(gca14i, '') -- and isnull(gca14i, '') > 0 -- and (gca08i = 5 or gca08i = 4) -- and gca03i = 3 -- 将状态为已过期( '8')但实际日期并没有过期, 剩余次数>0的几次卡 设定为 正在使用中('5') -- 目的: 处理单次卡 入院取消复核的情况 select @state = 5 update gcm01 set gca08i = @state where gca00c = @compid and isnull(gca15i, 0) < isnull(gca14i, '') and isnull(gca14i, '') > 0 and isnull(gca07d, '') >= @date and gca08i = 8 and gca03i = 3 end GO /****** Object: StoredProcedure [dbo].[upg_get_card_acct_remain] Script Date: 06/12/2017 06:20:05 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_get_card_acct_remain] ( @cardid varchar(20), @curacctid int, -- 当前帐户 @curamt float output, -- 当前帐户的余额 @amt_2 float output, -- 储值帐户 @amt_3 float output, -- 积分帐户 @amt_4 float output, -- 疗程帐户 @amt_5 float output, -- 帐户代码5, @amt_6 float output, -- 帐户代码6, @amt_7 float output -- 帐户代码7 ) as begin declare @acctid int declare @amt float select @curamt = 0, @amt_2 = 0, @amt_3 = 0, @amt_4 = 0, @amt_5 = 0, @amt_6 = 0, @amt_7 = 0 declare cur_each_acct cursor for select gcc03i, gcc06f from gcm03 c with(nolock), gcm01 a with(nolock) where a.gca00c = a.gca13d and a.gca01c = c.gcc01c and a.gca00c = c.gcc00c open cur_each_acct fetch cur_each_acct into @acctid, @amt while @@fetch_status = 0 begin if @acctid = 2 begin select @amt_2 = @amt end else if @acctid = 3 begin select @amt_3 = @amt end else if @acctid = 4 begin select @amt_4 = @amt end else if @acctid = 5 begin select @amt_5 = @amt end else if @acctid = 6 begin select @amt_6 = @amt end else if @acctid = 7 begin select @amt_7 = @amt end if @curacctid = @acctid begin select @curamt = @amt end fetch cur_each_acct into @acctid, @amt end close cur_each_acct deallocate cur_each_acct end GO /****** Object: StoredProcedure [dbo].[upg_used_cureproc_ex] Script Date: 06/12/2017 06:20:52 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_used_cureproc_ex] ( @compid varchar(10), @cardno varchar(30), @proj varchar(30), @times float, @amount float, @confirm int, @billtype varchar(20), @billid varchar(20), @curdate varchar(8), @procidd int --疗程的编号 ) as begin -- 根据卡号获取归属公司: declare @ownercompid nvarchar(10) --卡的归属公司 select @ownercompid = min(gca13d) from gcm01 where gca01c = @cardno and gca00c = gca13d -- modify by lyj 2008-04-09 伊美娜 declare @priceflow int -- 价格序号 declare @discount float -- 折扣 declare @isgift int declare @lasttime float -- 上次次数 -- 取的是最后一个,价格序号最小的,折扣数字最大的 if @procidd > 0 begin -- 精确定位 select @priceflow = isnull(gcf17i, 9), @discount = isnull(gcf18f, 9), @isgift = isnull(gcf22i, 0), @lasttime = gcf07f from gcm06 with (nolock) where gcf00c = @ownercompid and gcf23i = @procidd end else begin select @priceflow = isnull(gcf17i, 9), @discount = isnull(gcf18f, 9), @isgift = isnull(gcf22i, 0), @procidd = gcf23i, @lasttime = gcf07f from gcm06 with (nolock) where gcf00c = @ownercompid and gcf01c = @cardno and gcf02c = @proj and (gcf07f >= @times or (isnull(gcf22i, 0) = 1)) order by gcf23i desc end if (isnull(@priceflow, -1) = -1 and isnull(@discount, -1) = -1) return if (@confirm = 1) begin insert gcm08(gch00c, gch02d, gch03c, gch04c, gch05f, gch06f, gch07f, gch08f, gch09f, gch10c, gch11c, gch12i, gch13f, gch14f, gch15f, gch16i, gch17c, gch18f, gch23f) values(@compid, @curdate, @cardno, @proj, 0, 0, 0, @times, @amount, @billtype, @billid, @priceflow, @discount, 1, @amount/isnull(@times, 0), @procidd, '9', @amount, @lasttime) if (@isgift != 1) begin update gcm06 set gcf06f = isnull(gcf06f, 0) + @times, gcf07f = isnull(gcf07f, 0) - @times, gcf09f = isnull(gcf09f, 0) + @amount, gcf10f = isnull(gcf10f, 0) - @amount where gcf00c = @ownercompid and gcf23i = @procidd end end else begin delete gcm08 where gch00c = @compid and gch03c = @cardno and gch04c = @proj and gch10c = @billtype and gch11c = @billid and isnull(gch24c, '') = '' --isnull(gch24c, '') = '': 对应消费项目编号正好跟购买产品所赠送的项目编号一致的情况 if (@isgift != 1) begin update gcm06 set gcf06f = isnull(gcf06f, 0) - @times, gcf07f = isnull(gcf07f, 0) + @times, gcf09f = isnull(gcf09f, 0) - @amount, gcf10f = isnull(gcf10f, 0) + @amount, gcf28i = 0 where gcf00c = @ownercompid and gcf01c = @cardno and gcf23i = @procidd end end end GO /****** Object: StoredProcedure [dbo].[upg_get_card_compid] Script Date: 06/12/2017 06:20:05 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_get_card_compid] ( @cardid varchar(20), @cardcompid varchar(20) output ) as begin -- 根据卡号获取归属公司: select @cardcompid = min(gca13d) from gcm01 where gca01c = @cardid and gca00c = gca13d end GO /****** Object: StoredProcedure [dbo].[upg_m_dispatch_accountspendlimit] Script Date: 06/12/2017 06:20:18 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_m_dispatch_accountspendlimit] ( @fromcompid varchar(10), -- 从哪一个公司发布 @tocompid varchar(10), -- 目的公司 @mandatory varchar(2) -- 是否强制统一 ) as begin if @fromcompid = @tocompid return if @mandatory = '1' begin delete accountspendlimit where gal00c = @tocompid insert accountspendlimit(gal00c, gal01c, gal02f, gal03c, gal04c, gal05c, gal06c, gal07c) select @tocompid, gal01c, gal02f, gal03c, gal04c, gal05c, gal06c, gal07c from accountspendlimit where gal00c = @fromcompid end end GO /****** Object: StoredProcedure [dbo].[upg_change_card_comp_ex] Script Date: 06/12/2017 06:19:38 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_change_card_comp_ex] ( @cardid varchar(20), @tocardid varchar(20), @tocompid varchar(10), @cardtype varchar(5) --卡类别 ) as begin begin transaction begin try create table #gcm01 ( gca00c varchar(10) not null, -- 门店 gca01c varchar(20) not null -- 卡号 ) create clustered index idx_gcm01 on #gcm01(gca01c) -- 查询当前门店 的卡号 且是 未销售的 insert #gcm01(gca00c,gca01c) select gca00c,gca01c from gcm01 with(nolock) where gca01c between @cardid and @tocardid and (@cardtype = '*' or gca02c = @cardtype) and gca08i = '1' -- 更新会员卡归属门店 update a set a.gca00c = @tocompid,a.gca13d = @tocompid from gcm01 a,#gcm01 b where a.gca00c = b.gca00c and a.gca01c = b.gca01c -- 更新会员卡账户归属门店 update a set a.gcc00c = @tocompid from gcm03 a,#gcm01 b where a.gcc00c = b.gca00c and a.gcc01c = b.gca01c drop table #gcm01 end try begin catch drop table #gcm01 if @@TRANCOUNT > 0 begin declare @SP_Parameters varchar(max) set @SP_Parameters = '@cardid = '+ isnull(@cardid,'') + '@tocardid = '+ isnull(@tocardid,'') + '@tocompid = ' + isnull(@tocompid, '') insert into SP_TranLog (SPName,SPTime,SPParameter) values ('upg_change_card_comp_ex',getdate(),@SP_Parameters) raiserror ('transaction is failed.', 16, 1) WITH NOWAIT rollback transaction end end catch if @@TRANCOUNT > 0 begin commit transaction end end GO /****** Object: StoredProcedure [dbo].[upg_copy_userright] Script Date: 06/12/2017 06:19:49 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_copy_userright] ( @fromuser varchar(20), @touser varchar(20) ) as begin begin transaction begin try if not exists(select 1 from gam01 where gaa01c = @touser) begin insert gam01(gaa01c, gaa04c) select @touser, gaa04c from gam01 where gaa01c = @fromuser end delete gam02 where gab01c = @touser and gab02c <> '2' insert gam02(gab01c, gab02c, gab03c, gab04c, gab06c) select @touser, gab02c, gab03c, gab04c, gab06c from gam02 where gab01c = @fromuser and gab02c <> '2' delete gam03 where gac01c = @touser insert gam03(gac01c, gac02c, gac03c, gac04c, gac05c, gac06c, gac07c, gac08c, gac09c, gac10c, gac11c, gac12c, gac13c, gac14c, gac15c, gac16c, gac19c) select @touser, gac02c, gac03c, gac04c, gac05c, gac06c, gac07c, gac08c, gac09c, gac10c, gac11c, gac12c, gac13c, gac14c, gac15c, gac16c, gac19c from gam03 where gac01c = @fromuser end try begin catch if @@TRANCOUNT > 0 begin rollback transaction declare @SP_Parameters varchar(max) set @SP_Parameters = '@fromuser = ' + isnull(@fromuser, '') + '@touser = ' + isnull(@touser, '') insert into SP_TranLog (SPName , SPTime , SPParameter) values ( 'upg_copy_userright' , getdate() , @SP_Parameters) raiserror ('transaction is failed.', 16, 1) with NOWAIT end end catch if @@TRANCOUNT > 0 begin commit transaction end end GO /****** Object: StoredProcedure [dbo].[upg_synchronization] Script Date: 06/12/2017 06:20:46 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_synchronization] as--WUXM, 2005/05/30 begin --从床位表中拷贝新床位到gicm06表, 同步gicm06与gicm03表 --已经被删除的床位删除掉 delete a from gicm06 a where gicf00c + gicf01v + gicf02v not in (select gicc00c + gicc01v + gicc02v from gicm03) insert into gicm06 (gicf00c, gicf01v, gicf02v, gicf03i, gicf04t, gicf05t, gicf06d, gicf07v) select gicc00c, gicc01v, gicc02v, '0', null, null, null, null from gicm03 where gicc00c + gicc01v + gicc02v not in (select gicf00c + gicf01v + gicf02v from gicm06) --从员工表中拷贝新员工到gicm07表, 同步gicm07与ham01表 --已经被删除的员工需要删除掉 delete a from gicm07 a where gicg00c + gicg01v not in (select haa00c + haa01c from ham01) insert into gicm07 (gicg00c, gicg01v, gicg02i, gicg03t, gicg04t, gicg05d, gicg06v) select haa00c, haa01c, '0', null, null, null, null from ham01 where haa00c + haa01c not in (select gicg00c + gicg01v from gicm07) end GO /****** Object: StoredProcedure [dbo].[upg_analyze_usedgoods_byempl] Script Date: 06/12/2017 06:19:26 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_analyze_usedgoods_byempl] ( @compid varchar(10), -- 总公司 @fromdate varchar(10), -- 开始日期 @todate varchar(10), -- 截止日期 @staff varchar(20) -- 员工号 ) as begin if @staff = '*' select @staff = '%' create table #tbl_usedgoods_corr_prjs ( idd int identity not null, ddate varchar(20) null, ttime varchar(20) null, goodsno varchar(20) null, gnum float null, gunit varchar(20) null, prjno varchar(20) null, pnum float null, pamt float null, -- 项目金额 srv_1 varchar(20) null, -- 第一服务 srv_2 varchar(20) null, -- 第二服务 srv_3 varchar(20) null, -- 第三服务 billtype varchar(20) null, billid varchar(20) null, primary key(idd) ) declare @gia01c varchar(20) declare @gia03d varchar(10), @pre_gia03d varchar(10) declare @gia05c varchar(20) declare @gia08t varchar(10), @pre_gia08t varchar(10) declare @gib03c varchar(20) declare @gib06c varchar(20) declare @gib07f float declare @billtype varchar(20), @billid varchar(20) declare cur_each_good cursor for select gia01c, gia03d, gia05c, gia08t, gib03c, gib06c, gib07f, 'gi', gib01c from gim02 b, gim01 a where b.gib00c = a.gia00c and b.gib01c = a.gia01c and isnull(a.gia09i, 1) = 1 and a.gia07i = 1 and isnull(gia94d, '') <> '' and a.gia05c like @staff and a.gia00c = @compid and a.gia03d >= @fromdate and a.gia03d <= @todate open cur_each_good fetch cur_each_good into @gia01c, @gia03d, @gia05c, @gia08t, @gib03c, @gib06c, @gib07f, @billtype, @billid while @@fetch_status = 0 begin if (isnull(@pre_gia03d, '') <> '' and isnull(@pre_gia08t, '') <> '') begin --将前次领取时间到这次领取时间之间的项目都放进表中 insert #tbl_usedgoods_corr_prjs(ddate, ttime, billtype, billid, prjno, pnum, srv_1, srv_2, srv_3, pamt) select gga02d, gla04t, 'gx', gga01c, ggb03c, ggb05f, ggb12c, ggb13c, ggb14c, ggb11f from ggm02 b with (nolock), ggm01 a with (nolock), glm01 c with (nolock) where ggb00c = @compid and ((isnull(ggb12c, '') like @staff) or (isnull(ggb13c, '') like @staff) or (isnull(ggb14c, '') like @staff) ) and b.ggb00c = a.gga00c and b.ggb01c = a.gga01c and a.gga00c = c.gla00c and a.gga30c = c.gla01c and gga02d + gla04t >= @pre_gia03d + @pre_gia08t and gga02d + gla04t < @gia03d + @gia08t end --将本次产品领取放进表里 insert #tbl_usedgoods_corr_prjs(ddate, ttime, billtype, billid, goodsno, gnum, gunit, srv_1) values(@gia03d, @gia08t, @billtype, @billid, @gib03c, @gib07f, @gib06c, @gia05c) select @pre_gia03d = @gia03d, @pre_gia08t = @gia08t fetch cur_each_good into @gia01c, @gia03d, @gia05c, @gia08t, @gib03c, @gib06c, @gib07f, @billtype, @billid end close cur_each_good deallocate cur_each_good --将上次产品领取时间到截至日期的最晚235959的项目放进表里 insert #tbl_usedgoods_corr_prjs(ddate, ttime, billtype, billid, prjno, pnum, srv_1, srv_2, srv_3, pamt) select gga02d, gla04t, 'gx', gga01c, ggb03c, ggb05f, ggb12c, ggb13c, ggb14c, ggb11f from ggm02 b with (nolock), ggm01 a with (nolock), glm01 c with (nolock) where ggb00c = @compid and ((isnull(ggb12c, '') like @staff) or (isnull(ggb13c, '') like @staff) or (isnull(ggb14c, '') like @staff) ) and b.ggb00c = a.gga00c and b.ggb01c = a.gga01c and a.gga00c = c.gla00c and a.gga30c = c.gla01c and gga02d + gla04t >= @pre_gia03d + @pre_gia08t and gga02d + gla04t < @todate + '240000' select isnull(a.idd,'') idd, isnull(a.ddate,'') ddate, isnull(a.ttime,'') ttime, isnull(a.goodsno,'') goodsno, isnull(a.gnum,'') gnum, isnull(a.gunit,'') gunit, isnull(a.prjno,'') prjno, isnull(a.pnum,'') pnum, isnull(a.pamt,'') pamt, isnull(a.srv_1,'') srv_1, isnull(a.srv_2,'') srv_2, isnull(a.srv_3,'') srv_3, isnull(a.billtype,'') billtype, isnull(a.billid,'') billid, pname = isnull(b.gda03c,''), gname = isnull(c.gfa03c,'') from #tbl_usedgoods_corr_prjs a, gdm01 b, gfm01 c where a.prjno *= b.gda01c and b.gda00c = @compid and a.goodsno *= c.gfa01c and c.gfa00c = @compid and isnull(a.goodsno,'') <> '' drop table #tbl_usedgoods_corr_prjs end GO /****** Object: StoredProcedure [dbo].[upg_initial_defict] Script Date: 06/12/2017 06:20:14 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_initial_defict] as --mzh 2006/03/06 begin if exists(select 1 from gcm03 with (nolock) where isnull(gcc10f, 0)>0) return create table #defict ( idd int identity not null, cardno varchar(20) null, account int null, defict float null, primary key(idd) ) create table #return ( idd int identity not null, cardno varchar(20) null, account int null, rreturn float null, primary key(idd) ) create table #result ( idd int identity not null, cardno varchar(20) null, account int null, defict float null, primary key(idd) ) insert #defict(cardno, account, defict) select gcd01c, gcd02i, sum(gcd06f) from gcm04 with (nolock) where gcd05i = 5 group by gcd01c, gcd02i insert #return(cardno, account, rreturn) select gcd01c, gcd02i, sum(gcd06f) from gcm04 with (nolock) where gcd05i = 6 group by gcd01c, gcd02i insert #result(cardno, account, defict) select a.cardno, a.account, isnull(a.defict, 0)-isnull(b.rreturn, 0) from #defict a left outer join #return b on a.cardno = b.cardno and a.account = b.account update gcm03 set gcc10f = defict from gcm03, #result where gcc01c = cardno and gcc03i = account and isnull(gcc10f, 0) <> defict and isnull(gcc10f, 0) = 0 drop table #defict drop table #return end GO /****** Object: StoredProcedure [dbo].[upg_employee_nowstatus] Script Date: 06/12/2017 06:20:02 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_employee_nowstatus] ( @compid varchar(10), -- 公司别 @emplid varchar(20), -- 会员编号 @state int -- 状态 0-正常, 1-忙碌 ) as begin update gicm07 set gicg02i = @state where gicg00c = @compid and gicg01v = @emplid end GO /****** Object: StoredProcedure [dbo].[upg_query_seat_status] Script Date: 06/12/2017 06:20:30 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_query_seat_status] ( @compid varchar(10), -- 公司别 @qrydate varchar(8), -- 日期, 查询不能跨一天 @begintime varchar(6), -- 开始时间 @endtime varchar(6), -- 截止时间 @today varchar(20), -- 当天时间 @ret_type int -- 数据返回类型 ) as begin create table #seat_status( idd int not null identity, -- 流水号 roomid varchar(20) not null, -- 房间 seatid varchar(20) not null, -- 床位 fromtime varchar(20) null, -- 开始时间 totime varchar(20) null, -- 截止时间 st_type int null, -- 状态,[0:空闲,1:忙碌,2:预约, 3:放假休息中] person1 varchar(20) null, -- 服务员工 primary key(idd)) create table #related_bookbill( billid varchar(20) not null, -- 预约单号/入院单号 status int null, -- 状态,[0:空闲,1:忙碌,2:预约, 3:放假休息中] fromtime varchar(20) null, totime varchar(20) null, roomid varchar(20) not null, bedid varchar(20) not null, primary key(billid, roomid, bedid)) --这段时间内有效的预约单中的人 --可能查询的这个时间点可能是今天,需要考虑正在服务的员工 --已经开始在服务的员工默认的时间就是开单的数量的时间单位 if (@qrydate = @today) begin --查询今天所有没有结案的入院单,估计结束时间 delete #related_bookbill insert #related_bookbill(billid, status, fromtime, roomid, bedid) select gla01c, 9, gla03t, gll07v, gll08v from glm01 a with (nolock), glm12 b with (nolock) where gla00c = @compid and isnull(gla95c, '') = '' and a.gla00c = b.gll00c and a.gla01c = b.gll01v and isnull(gll07v, '')<>'' and isnull(gll08v, '')<>'' update a set totime = substring(convert(varchar(30), convert(datetime, substring(fromtime, 1, 2) + ':' + substring(fromtime, 3, 2) + ':00') + convert(datetime, substring(gda21t, 1, 2) + ':' + substring(gda21t, 3, 2) + ':00'), 8), 1, 2) + substring(convert(varchar(30), convert(datetime, substring(fromtime, 1, 2) + ':' + substring(fromtime, 3, 2) + ':00') + convert(datetime, substring(gda21t, 1, 2) + ':' + substring(gda21t, 3, 2) + ':00'), 8), 4, 2) + substring(convert(varchar(30), convert(datetime, substring(fromtime, 1, 2) + ':' + substring(fromtime, 3, 2) + ':00') + convert(datetime, substring(gda21t, 1, 2) + ':' + substring(gda21t, 3, 2) + ':00'), 8), 7, 2) from #related_bookbill a, glm12 b, gdm01 c where b.gll00c = @compid and b.gll01v = a.billid and b.gll00c = c.gda00c and b.gll03v = c.gda01c update a set totime = convert(varchar(2), convert(int, substring(totime, 1, 2)) + 24) + substring(totime, 3, 2) from #related_bookbill a where totime= @begintime and totime <= @endtime) or (fromtime<@endtime and totime >= @endtime) or (fromtime <= @begintime and totime>@begintime) delete #related_bookbill where status = 9 -- 临时用9状态 --把相关的人员的状态修改为忙碌 insert #seat_status(roomid, seatid, fromtime, totime, st_type, person1) select a.gll07v, a.gll08v, b.fromtime, b.totime, 1, gll04v from glm12 a, #related_bookbill b where a.gll00c = @compid and a.gll01v = b.billid and isnull(a.gll07v, '')<>'' and isnull(a.gll08v, '')<>'' and a.gll07v = b.roomid and a.gll08v = b.bedid end --add by lyj for 2007-12-19 把预约的给加入进去 --插入预约的房间 insert #seat_status(roomid, seatid, fromtime, totime, st_type, person1) select gwa17c, gwa18c, gwa02t, gwa03t, 2, '' from gwm01 where gwa00c = @compid and gwa01d = @qrydate and (substring(gwa02t, 1, 4) between @begintime and @endtime or substring(gwa03t, 1, 4) between @begintime and @endtime) and gwa11i = 0 --其他位置都认为有空 insert #seat_status(roomid, seatid, fromtime, totime, st_type, person1) select gicc01v, gicc02v, @begintime, @endtime, 0, '' from gicm03 a where a.gicc00c = @compid and gicc01v + '&' + gicc02v not in (select roomid + '&' + seatid from #seat_status) --先插入忙的位置 delete gicm08 where gich00c = @compid --忙的位置 insert gicm08(gich00c, gich01v, gich02v, gich03i, gich04t, gich05t, gich06d, gich07i, gich08c) select @compid, roomid, seatid, 1, min(fromtime), max(totime), @qrydate, 0, person1 from #seat_status a where st_type = 1 group by roomid, seatid, person1 --预约的位置 insert gicm08(gich00c, gich01v, gich02v, gich03i, gich04t, gich05t, gich06d, gich07i, gich08c) select @compid, roomid, seatid, 2, min(fromtime), max(totime), @qrydate, 0, person1 from #seat_status where st_type = 2 and roomid + '&' + seatid not in(select gich01v + '&' + gich02v from gicm08 where gich00c = @compid) group by roomid, seatid, person1 --其他认为是空闲的位置 insert gicm08(gich00c, gich01v, gich02v, gich03i, gich04t, gich05t, gich06d, gich07i, gich08c) select @compid, roomid, seatid, 0, min(fromtime), max(totime), @qrydate, 0, person1 from #seat_status where st_type = 0 and roomid + '&' + seatid not in(select gich02v + '&' + gich02v from gicm08 where gich00c = @compid) group by roomid, seatid, person1 drop table #related_bookbill drop table #seat_status end GO /****** Object: StoredProcedure [dbo].[upg_recover_own_ham01] Script Date: 06/12/2017 06:20:33 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_recover_own_ham01] ( @currentCompId varchar(10) --公司 ) as begin -- 员工的归属门店 if not exists (select 1 from data_recover where rdd00c = @currentCompId and rdd01c = 'ham01' and rdd02c = 'own') begin update ham01 set ownhaa00c = haa00c, ownhaa01c = haa01c where haa00c = @currentCompId and haa27c = 2 insert into data_recover(rdd00c, rdd01c, rdd02c) values (@currentCompId, 'ham01', 'own') end end GO /****** Object: StoredProcedure [dbo].[upg_recover_gim22_did] Script Date: 06/12/2017 06:20:32 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_recover_gim22_did] as begin declare @gio00c nvarchar(10) declare @gio01c nvarchar(20) declare @gio05c nvarchar(20) declare @gio06f float declare @id int -- 员工的归属门店 if not exists (select 1 from data_recover where rdd00c = '001' and rdd01c = 'gim22' and rdd02c = 'recover_gim22_did') begin declare cur_each_gim21_item cursor for select gin00c, gin01c, gin02c, gin03f, id from gim20 inner join gim21 on gim00c = gin00c and gim01c = gin01c where gim06c <> '2' open cur_each_gim21_item fetch cur_each_gim21_item into @gio00c, @gio01c, @gio05c, @gio06f, @id while @@fetch_status = 0 begin update gim22 set did = @id where gio00c = @gio00c and gio01c = @gio01c and gio05c = @gio05c and gio06f = @gio06f fetch cur_each_gim21_item into @gio00c, @gio01c, @gio05c, @gio06f, @id end close cur_each_gim21_item deallocate cur_each_gim21_item insert into data_recover(rdd00c, rdd01c, rdd02c) values ('001', 'gim22', 'recover_gim22_did') end end GO /****** Object: StoredProcedure [dbo].[upg_get_all_prod_price_mv] Script Date: 06/12/2017 06:20:04 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_get_all_prod_price_mv] ( @compid varchar(10), -- 公司编号 @currdate varchar(8) -- 复核日期 ) as-- WUXM 2004/12/6 PM 21:31 begin declare @price float declare @prod varchar(20) create table #prod_price ( comp varchar(10), prod varchar(20), price float, number int ) insert into #prod_price(number, comp, prod) select row_number() over(order by gfa01c) as number, gfa00c, gfa01c from gfm01 with(nolock) where gfa00c = @compid declare @i int set @i = 1 declare @max int select @max = max(number) from #prod_price set @max = 100 while @i <= @max begin set @price = 0 select @prod = prod from #prod_price where number = @i exec upg_calculate_prod_price_mv @compid, @prod, @currdate, @price output update #prod_price set price = @price where number = @i set @i = @i + 1 end select comp, prod, price from #prod_price drop table #prod_price end GO /****** Object: StoredProcedure [dbo].[upg_query_empl_status] Script Date: 06/12/2017 06:20:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_query_empl_status] ( @compid varchar(10), -- 公司别 @qrydate varchar(8), -- 日期, 查询不能跨一天 @begintime varchar(6), -- 开始时间 @endtime varchar(6), -- 截止时间 @today varchar(20), -- 当天时间 @ret_type int -- 数据返回类型 0: 所有员工的状态 1:返回空的员工 ) as begin create table #empl_status( idd int not null identity, -- 流水号 emplid varchar(20) not null, -- 员工号 fromtime varchar(20) null, -- 开始时间 totime varchar(20) null, -- 截止时间 st_type int null, -- 状态,[0:空闲,1:忙碌,2:预约, 3:放假休息中] room_id varchar(20) null, --房间 bed_id varchar(20) null, --位置 primary key(idd)) create table #related_bookbill( billid varchar(20) not null, -- 预约单号/入院单号 status int null, -- 状态,[0:空闲,1:忙碌,2:预约, 3:放假休息中] fromtime varchar(20) null, totime varchar(20) null, emplid varchar(20) not null, --员工编号 primary key(billid, emplid)) --这段时间内有效的预约单中的人 --这些预约单中的人都是被预约的员工 insert #empl_status(emplid, fromtime, totime, st_type, room_id, bed_id) select a.gwa06c, a.gwa02t, a.gwa03t, 2, '', '' from gwm01 a where isnull(a.gwa06c, '')<>'' and gwa00c = @compid and gwa11i = 0 and gwa01d = @qrydate and ((gwa02t >= @begintime and gwa03t <= @endtime) or (gwa02t<@endtime and gwa03t >= @endtime) or (gwa02t <= @begintime and gwa03t>@begintime)) --可能查询的这个时间点可能是今天,需要考虑正在服务的员工 --已经开始在服务的员工默认的时间就是开单的数量的时间单位 if (@qrydate = @today) begin --查询今天所有没有结案的入院单,估计结束时间 insert #related_bookbill(billid, status, fromtime, emplid) select gla01c, 9, gla03t, gll04v from glm01 a with (nolock), glm12 b with (nolock) where gla00c = @compid and isnull(gla95c, '') = '' and a.gla00c = b.gll00c and a.gla01c = b.gll01v and isnull(gll04v, '')<>'' insert #related_bookbill(billid, status, fromtime, emplid) select gla01c, 9, gla03t, gll05v from glm01 a with (nolock), glm12 b with (nolock) where gla00c = @compid and isnull(gla95c, '') = '' and a.gla00c = b.gll00c and a.gla01c = b.gll01v and isnull(gll05v, '')<>'' insert #related_bookbill(billid, status, fromtime, emplid) select gla01c, 9, gla03t, gll06v from glm01 a with (nolock), glm12 b with (nolock) where gla00c = @compid and isnull(gla95c, '') = '' and a.gla00c = b.gll00c and a.gla01c = b.gll01v and isnull(gll06v, '')<>'' update a set totime = substring(convert(varchar(30), convert(datetime, substring(fromtime, 1, 2) + ':' + substring(fromtime, 3, 2) + ':00') + convert(datetime, substring(gda21t, 1, 2) + ':' + substring(gda21t, 3, 2) + ':00'), 8), 1, 2) + substring(convert(varchar(30), convert(datetime, substring(fromtime, 1, 2) + ':' + substring(fromtime, 3, 2) + ':00') + convert(datetime, substring(gda21t, 1, 2) + ':' + substring(gda21t, 3, 2) + ':00'), 8), 4, 2) + substring(convert(varchar(30), convert(datetime, substring(fromtime, 1, 2) + ':' + substring(fromtime, 3, 2) + ':00') + convert(datetime, substring(gda21t, 1, 2) + ':' + substring(gda21t, 3, 2) + ':00'), 8), 7, 2) from #related_bookbill a, glm12 b, gdm01 c where b.gll00c = @compid and b.gll01v = a.billid and a.emplid = b.gll04v and b.gll00c = c.gda00c and b.gll03v = c.gda01c update a set totime = convert(varchar(2), convert(int, substring(totime, 1, 2)) + 24) + substring(totime, 3, 2) from #related_bookbill a where totime= @begintime and totime <= @endtime) or (fromtime<@endtime and totime >= @endtime) or (fromtime <= @begintime and totime>@begintime) delete #related_bookbill where status = 9 -- 临时用9状态 --把相关的人员的状态修改为忙碌 insert #empl_status(emplid, fromtime, totime, st_type, room_id, bed_id) select a.gll04v, b.fromtime, b.totime, 1, gll07v, gll08v from glm12 a, #related_bookbill b where a.gll00c = @compid and a.gll01v = b.billid and a.gll04v = b.emplid and isnull(a.gll04v, '')<>'' insert #empl_status(emplid, fromtime, totime, st_type, room_id, bed_id) select a.gll05v, b.fromtime, b.totime, 1, gll07v, gll08v from glm12 a, #related_bookbill b where a.gll00c = @compid and a.gll01v = b.billid and a.gll05v = b.emplid and isnull(a.gll05v, '')<>'' insert #empl_status(emplid, fromtime, totime, st_type, room_id, bed_id) select a.gll06v, b.fromtime, b.totime, 1, gll07v, gll08v from glm12 a, #related_bookbill b where a.gll00c = @compid and a.gll01v = b.billid and a.gll06v = b.emplid and isnull(a.gll06v, '')<>'' end --如果有排班设定,查询出哪些员工在放假中 --在太忙的时候可以请他们来帮忙 --如果没有排班,则认为别的员工都是空的 if exists(select 1 from gicm10 where gicj00c = @compid and gicj05d <= @qrydate and gicj06d >= @qrydate) begin --有排班 --这个时间范围内有排班的人, 这些人在上班但没有事情 insert #empl_status(emplid, fromtime, totime, st_type, room_id, bed_id) select gicj01v, @begintime, @endtime, 0, '', '' from gicm10 a where a.gicj00c = @compid and gicj05d <= @qrydate and gicj06d >= @qrydate and ((@begintime >= gicj03t and @endtime <= gicj04t) or (@begintime <= gicj03t and @endtime>gicj03t) or (@begintimegicj04t)) --其他人在放假休息中 insert #empl_status(emplid, fromtime, totime, st_type, room_id, bed_id) select haa01c, @begintime, @endtime, 3, '', '' from ham01 a where a.haa00c = @compid and a.haa01c not in (select emplid from #empl_status) end else begin --没有排班,其他人都认为有空 insert #empl_status(emplid, fromtime, totime, st_type, room_id, bed_id) select haa01c, @begintime, @endtime, 0, '', '' from ham01 a where a.haa00c = @compid and a.haa01c not in (select emplid from #empl_status) end --先插入忙的员工 delete gicm09 where gici00c = @compid --忙的人 insert into gicm09(gici00c, gici01v, gici02v, gici03i, gici04t, gici05t, gici06d, gici07i, gici08c, gici09c) select @compid, emplid, '', 1, min(fromtime), max(totime), @qrydate, 0, room_id, bed_id from #empl_status a where st_type = 1 group by emplid, room_id, bed_id --预约的人 insert into gicm09(gici00c, gici01v, gici02v, gici03i, gici04t, gici05t, gici06d, gici07i, gici08c, gici09c) select @compid, emplid, '', 2, min(fromtime), max(totime), @qrydate, count(*), room_id, bed_id from #empl_status where st_type = 2 and emplid not in(select gici01v from gicm09 where gici00c = @compid) group by emplid, room_id, bed_id --放假中人 insert into gicm09(gici00c, gici01v, gici02v, gici03i, gici04t, gici05t, gici06d, gici07i, gici08c, gici09c) select @compid, emplid, '', 3, min(fromtime), max(totime), @qrydate, 0, room_id, bed_id from #empl_status where st_type = 3 and emplid not in(select gici01v from gicm09 where gici00c = @compid) group by emplid, room_id, bed_id --其他认为是空闲的人 insert into gicm09(gici00c, gici01v, gici02v, gici03i, gici04t, gici05t, gici06d, gici07i, gici08c, gici09c) select @compid, emplid, '', 0, min(fromtime), max(totime), @qrydate, 0, room_id, bed_id from #empl_status where st_type = 0 and emplid not in(select gici01v from gicm09 where gici00c = @compid) group by emplid, room_id, bed_id update a set a.gici02v = b.haa02c from gicm09 a, ham01 b where a.gici00c = b.haa00c and a.gici01v = b.haa01c and a.gici00c = @compid drop table #related_bookbill drop table #empl_status end GO /****** Object: StoredProcedure [dbo].[upg_update_gam03_from_gzm02] Script Date: 06/12/2017 06:20:49 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_update_gam03_from_gzm02] ( @empid varchar(10) -- 角色编号 ) as begin update a set a.gac12c = b.gzb12c, a.gac13c = b.gzb13c, a.gac14c = b.gzb14c, a.gac15c = b.gzb15c, a.gac16c = b.gzb16c, a.gac19c = b.gzb19c from gam03 a, gzm02 b, gam02 c where a.gac01c = c.gab01c and a.gac02c = b.gzb02c and a.gac11c = b.gzb11c and c.gab03c = b.gzb01c and c.gab02c = '3' end GO /****** Object: StoredProcedure [dbo].[upg_quick_set_gam03_from_gzm02] Script Date: 06/12/2017 06:20:30 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_quick_set_gam03_from_gzm02] ( @user varchar(10), -- 使用者编号 @role varchar(10) -- 使用者角色 ) as begin if (isnull(@role, '') = '') begin select @role = gab03c from gam02 where gab01c = @user and gab02c = '3' end delete gam03 where gac01c = @user insert gam03(gac01c, gac02c, gac03c, gac04c, gac05c, gac06c, gac07c, gac08c, gac09c, gac10c, gac11c, gac12c, gac13c, gac14c, gac15c, gac16c, gac19c) select @user, gzb02c, gzb03c, gzb04c, gzb05c, gzb06c, gzb07c, gzb08c, gzb09c, gzb10c, gzb11c, gzb12c, gzb13c, gzb14c, gzb15c, gzb16c, gzb19c from gzm02 b, gam02 c where c.gab01c = @user and c.gab02c = '1' and b.gzb01c = @role and c.gab03c = b.gzb11c end GO /****** Object: StoredProcedure [dbo].[upg_delete_deficit_gcm10] Script Date: 06/12/2017 06:19:58 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_delete_deficit_gcm10] ( @compid varchar(10), @billid varchar(20) -- 帐户异动单号 ) as begin declare @cardid varchar(20) declare @acctid varchar(10) declare @amt float select @cardid = gcl03c, @acctid = gcl04c, @amt = gcl07f from gcm10 where gcl00c = @compid and gcl01c = @billid and gcl06i = 5 if @@rowcount = 1 begin -- 调整帐户历史 delete gcm04 where gcd00c = @compid and gcd01c = @cardid and gcd05i = 5 and gcd07c = 'gz' and gcd08c = @billid -- 删除帐户历史 delete gcm10 where gcl00c = @compid and gcl01c = @billid end end GO /****** Object: StoredProcedure [dbo].[upg_get_gcm04_flowno] Script Date: 06/12/2017 06:20:07 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_get_gcm04_flowno] ( @cardid varchar(20), @acctid int, @flowno int output ) as begin declare @flow int select @flow = 0 if @acctid = 0 begin select @flow = max(gcd04f) from gcm04 with (nolock) where gcd01c = @cardid end else begin select @flow = max(gcd04f) from gcm04 with (nolock) where gcd01c = @cardid and gcd02i = @acctid end select @flowno = isnull(@flow, 0) + 1 end GO /****** Object: StoredProcedure [dbo].[upg_recal_gcm04_lastremain] Script Date: 06/12/2017 06:20:31 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_recal_gcm04_lastremain] ( @cardid varchar(20), @acctid int ) as begin declare @gcd05i int declare @gcd06f float declare @remain float declare @billid varchar(20) declare @compid varchar(10) declare @min_gcm10_date varchar(10) declare @billdate varchar(10) declare @billtype varchar(10) select @min_gcm10_date = min(gcl02d) from gcm10 with (nolock) declare cur_sur_each_gcm04 cursor for select gcd05i, gcd06f, gcd08c, gcd00c, gcd09d, gcd07c from gcm04 where gcd01c = @cardid and gcd02i = @acctid and (gcd07c <> '' or gcd08c <> '') order by gcd09d, gcd13t, gcd04f for update of gcd10f open cur_sur_each_gcm04 fetch cur_sur_each_gcm04 into @gcd05i, @gcd06f, @billid, @compid, @billdate, @billtype select @remain = 0 while @@fetch_status = 0 begin if @billtype = 'ge' set @remain = 0 update gcm04 set gcd10f = @remain where current of cur_sur_each_gcm04 if @gcd05i = 0 or @gcd05i = 6 or @gcd05i = 7 begin select @remain = isnull(@remain, 0) + isnull(@gcd06f, 0) end else if @gcd05i = 2 or @gcd05i = 5 or @gcd05i = 1 begin -- 由于前面版本的BUG, 有欠款的存款单取消复核的时候欠款纪录没有删除,不应该考虑 if @gcd05i = 5 begin if exists(select 1 from gcm10 where gcl00c = @compid and gcl01c = @billid and gcl07f = @gcd06f) begin select @remain = isnull(@remain, 0) - isnull(@gcd06f, 0) end else begin -- 可能原始单据已经被删除 if @billdate >= @min_gcm10_date begin delete gcm04 where current of cur_sur_each_gcm04 end else begin select @remain = isnull(@remain, 0) - isnull(@gcd06f, 0) end end end else begin select @remain = isnull(@remain, 0) - isnull(@gcd06f, 0) end end fetch cur_sur_each_gcm04 into @gcd05i, @gcd06f, @billid, @compid, @billdate, @billtype end close cur_sur_each_gcm04 deallocate cur_sur_each_gcm04 end GO /****** Object: StoredProcedure [dbo].[update_OR_insert_gicm10_banci] Script Date: 06/12/2017 06:19:22 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[update_OR_insert_gicm10_banci]( @comid varchar(10), @oldbanci varchar(20), @newbanci varchar(20), @curdate varchar(8), @curtime varchar(6), @plyid varchar(20), @plyname varchar(20), @operid varchar(20) ) as begin declare @cot int select @cot = count(*) from gicm10 where gicj00c = @comid and gicj01v = @plyid and gicj05d = @curdate and gicj02v = @oldbanci if (@cot>0) begin declare @cot1 int select @cot1 = count(*) from gicm10 where gicj00c = @comid and gicj01v = @plyid and gicj05d = @curdate and gicj02v = @newbanci if (@cot1 <= 0) begin update a set a.gicj02v = b.gick01v, a.gicj03t = b.gick03t, a.gicj04t = b.gick04t, a.gicj12t = b.gick09t from gicm10 a, gicm11 b where a.gicj00c = b.gick00c and b.gick01v = @newbanci and a.gicj00c = @comid and a.gicj01v = @plyid and a.gicj05d = @curdate and a.gicj02v = @oldbanci end end else begin select @cot = count(*) from gicm10 where gicj00c = @comid and gicj01v = @plyid and gicj05d = @curdate and gicj02v = @newbanci if (@cot <= 0) begin insert into gicm10(gicj00c, gicj01v, gicj02v, gicj03t, gicj04t, gicj05d, gicj06d, gicj07v, gicj08t, gicj09d, gicj12t, gicj13c) select gick00c, @plyid, gick01v, gick03t, gick04t, @curdate, @curdate, @operid, @curtime, @curdate, gick09t, @plyname from gicm11 where gick00c = @comid and gick01v = @newbanci end end end GO /****** Object: StoredProcedure [dbo].[upg_modify_gicm11] Script Date: 06/12/2017 06:20:24 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_modify_gicm11] ( @compid varchar(10), -- 公司 @emplyeeno varchar(20), -- 员工编号 @emplyeename varchar(20), -- 员工名称 @modifydate varchar(8), -- 班次日期 @originalduty varchar(20), -- 原始班次 @nowduty varchar(20), -- 调整班次 @loginid varchar(20), -- 登录人ID @nowtime varchar(6) -- 当前时间 ) as begin declare @ondutytime varchar(6) -- 上班时间 declare @offdutytime varchar(6) -- 下班时间 select @ondutytime = gick03t, @offdutytime = gick04t from gicm11 where gick00c = @compid and gick01v = @nowduty if exists(select 1 from gicm10 where gicj00c = @compid and gicj01v = @emplyeeno and gicj02v = @nowduty and @modifydate between gicj05d and gicj06d ) begin update a set gicj02v = @nowduty, gicj03t = @ondutytime, gicj04t = @offdutytime from gicm10 a where a.gicj00c = @compid and a.gicj01v = @emplyeeno and a.gicj02v = @originalduty and @modifydate between a.gicj05d and a.gicj06d end else if not exists(select 1 from gicm10 where gicj00c = @compid and gicj01v = @emplyeeno and gicj02v = @nowduty and @modifydate between gicj05d and gicj06d ) begin delete gicm10 where gicj00c = @compid and gicj01v = @emplyeeno and gicj02v = @originalduty and @modifydate between gicj05d and gicj06d insert into gicm10(gicj00c, gicj01v, gicj02v, gicj03t, gicj04t, gicj05d, gicj06d, gicj07v, gicj08t, gicj09d, gicj10v, gicj12t, gicj13c) values(@compid, @emplyeeno, @nowduty, @ondutytime, @offdutytime, @modifydate, @modifydate, @loginid, @nowtime, @modifydate, null, @offdutytime, @emplyeename) end end GO /****** Object: StoredProcedure [dbo].[upg_m_dispatch_gicm11] Script Date: 06/12/2017 06:20:19 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_m_dispatch_gicm11] ( @fromcompid varchar(10), -- 从哪一个公司发布 @tocompid varchar(10), -- 目的公司 @mandatory varchar(2) -- 是否强制统一 ) as begin if @fromcompid = @tocompid return if @mandatory = '1' begin delete gicm11 where gick00c = @tocompid end -- 插入 insert into gicm11 (gick00c, gick01v, gick02v, gick03t, gick04t, gick05v, gick06t, gick07d, gick08v, gick09t) select @tocompid, gick01v, gick02v, gick03t, gick04t, gick05v, gick06t, gick07d, gick08v, gick09t from gicm11 where gick00c = @fromcompid and gick01v not in (select gick01v from gicm11 where gick00c = @tocompid) end GO /****** Object: StoredProcedure [dbo].[upg_data_recover_gnm03_gcm11_gcm06_gcf24i] Script Date: 06/12/2017 06:19:55 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_data_recover_gnm03_gcm11_gcm06_gcf24i] as begin -- 创建临时表 create table #data_recover_key ( comp varchar(10), gnc29i int, -- 售卡-疗程序号 gnc25i int, -- 会员卡-疗程序号 ) insert into #data_recover_key(comp, gnc29i, gnc25i) select gnc00c, gnc29i, gnc25i from gnm03 with(nolock) where gnc25i is not null insert into #data_recover_key(comp, gnc29i, gnc25i) select gcm00c, gcm14i, gcm25i from gcm11 a with(nolock) join gcm10 b with(nolock) on a.gcm00c = b.gcl00c and a.gcm01c = b.gcl01c and b.gcl06i = 0 and gcm25i is not null -- 创建循环变量 declare @HasRecord int set @HasRecord = 1 declare @currentCompId varchar(10) declare @currentGnc29i int declare @currentGnc25i int declare @gcf24i int while(@HasRecord = 1) begin if exists(select 1 from #data_recover_key) begin set @HasRecord = 1 select top 1 @currentCompId = comp, @currentGnc29i = gnc29i, @currentGnc25i = gnc25i from #data_recover_key if exists(select 1 from gcm06 where gcf00c = @currentCompId and gcf23i = @currentGnc25i) begin select @gcf24i = gcf24i from gcm06 where gcf00c = @currentCompId and gcf23i = @currentGnc25i -- if @gcf24i is null begin update gcm06 set gcf24i = @currentGnc29i where gcf00c = @currentCompId and gcf23i = @currentGnc25i end end -- 清除已处理的门店 delete #data_recover_key where comp = @currentCompId and gnc29i = @currentGnc29i and gnc25i = @currentGnc25i end else begin set @HasRecord = 0 end end drop table #data_recover_key end GO /****** Object: StoredProcedure [dbo].[upg_calc_share_bonus] Script Date: 06/12/2017 06:19:37 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_calc_share_bonus]( @frommonth varchar(20), -- 开始日期 @tomonth varchar(20), -- 截至日期 @limit_amt float, -- 限制金额 @kind int) -- 1季度 2年度 as begin declare @total_amt float declare @total_points float create table #tbl_group( branid varchar(20) not null, amt float null, points float null) --时间段的总业绩 select @total_amt = sum(isnull(perf_amt, 0)) from b_perf where mmonth >= @frommonth and mmonth <= @tomonth insert #tbl_group(branid, amt) select bran_id, sum(perf_amt) from b_perf where mmonth >= @frommonth and mmonth <= @tomonth group by bran_id delete #tbl_group where isnull(amt, 0)<@limit_amt select @total_points = sum(isnull(amt, 0)/10000) from #tbl_group if @kind = 1 begin update b_perf set quart_amt = 0 where mmonth = @tomonth update a set a.quart_amt = isnull(@total_amt, 0) * 0.01 * ((isnull(b.amt, 0)/10000) / isnull(@total_points, 1)) from b_perf a, #tbl_group b where a.mmonth = @tomonth and a.bran_id = b.branid end else if @kind = 2 begin update b_perf set year_amt = 0 where mmonth = @tomonth update a set a.year_amt = isnull(@total_amt, 0) * 0.02 * ((isnull(b.amt, 0)/10000) / isnull(@total_points, 1)) from b_perf a, #tbl_group b where a.mmonth = @tomonth and a.bran_id = b.branid end drop table #tbl_group end GO /****** Object: StoredProcedure [dbo].[upg_m_dispatch_gzm05] Script Date: 06/12/2017 06:20:20 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_m_dispatch_gzm05]( @fromcompid varchar(10), -- 从哪一个公司发布 @tocompid varchar(10), -- 目的公司 @mandatory varchar(2)) -- 是否强制统一 as begin create table #gzm05_01 --供应商基本资料 ( gze00c varchar(10) not null, -- 公司编号 gze01c varchar(20) not null, -- 卡类别, *代表不限制卡类别 gze02f float not null, -- 购买的金额 gze03f float null, -- 赠送的储值 gze04i int null, -- 是否为强制,不能修改的 ) declare @PK_Sql nvarchar(max) set @PK_Sql = ' alter table #gzm05_01 add constraint PK_#gzm05_01_' + convert(varchar(10), cast(floor(rand() * 1000000) as int)) + ' PRIMARY KEY(gze00c, gze01c, gze02f)' exec sp_executesql @PK_Sql insert #gzm05_01(gze00c, gze01c, gze02f, gze03f, gze04i) select gze00c, gze01c, gze02f, gze03f, gze04i from gzm05 where gze00c = @fromcompid if @mandatory = '1' begin delete gzm05 where gze00c = @tocompid end else begin delete a from #gzm05_01 a, gzm05 b where a.gze00c = @fromcompid and a.gze01c = b.gze01c and a.gze02f = b.gze02f and b.gze00c = @tocompid end insert gzm05(gze00c, gze01c, gze02f, gze03f, gze04i) select @tocompid, gze01c, gze02f, gze03f, gze04i from #gzm05_01 where gze00c = @fromcompid drop table #gzm05_01 end GO /****** Object: StoredProcedure [dbo].[upg_analyse_emplyee_ability] Script Date: 06/12/2017 06:19:25 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_analyse_emplyee_ability] ( @compid varchar(10), -- 公司别 @fromstaff varchar(20), -- 员工 @tostaff varchar(20), -- @fromdate varchar(10), -- 开始日期 @todate varchar(10), -- 截至日期 @ret_type int, -- 0-只返回类型 1-返回所有分析结果 @data_scope int -- 1-员工自己负责自己服务 2-自己负责,不管谁服务 3-不关心谁负责与服务 ) as begin declare @unlimited_staff int if (isnull(@fromstaff, '') = '*' or (isnull(@fromstaff, '') = '' and isnull(@tostaff, '') = '')) or @data_scope = 3 begin select @fromstaff = min(haa01c), @tostaff = max(haa01c) from ham01 where haa00c = @compid select @unlimited_staff = 1 end else begin select @unlimited_staff = 0 end -- 员工所卖卡 create table #collect_detail ( idd int identity not null, staff varchar(20) not null, memid varchar(20) null, cardid varchar(20) null, type int not null, -- 1-卡 2-服务 3-产品 prj_type varchar(10) null, -- 产品类别,卡类别, 服务项目类别 prj varchar(20) not null, qty float null, amt float null, primary key(idd) ) create table #group_data ( idd int identity not null, staff varchar(20) not null, memid varchar(20) null, cardid varchar(20) null, type int not null, -- 1-卡 2-服务 3-产品, 4-欠款, 5-还款 prj_type varchar(10) not null, -- 产品类别,卡类别, 服务项目类别 qty float null, amt float null, primary key(idd) ) create table #empl_result ( idd int identity not null, staff varchar(20) not null, cardid varchar(20) not null, primary key(idd) ) create table #idd_info ( idd int identity not null, type int not null, prj_type varchar(10) not null, typename varchar(40) null, primary key(idd) ) insert #empl_result(staff, cardid) select gba10c, gba23c from gbm01 where gba00c = @compid and gba10c >= @fromstaff and gba10c <= @tostaff and isnull(gba23c, '') <> '' and isnull(gba10c, '') <> '' if (@unlimited_staff = 1) begin -- 卖卡 insert #collect_detail(staff, memid, cardid, type, prj_type, prj, qty, amt) select isnull(gna20c, ''), '', gnb03c, 1, gnb02c, gnb03c, 1, gnb08f * isnull(gna27f, 1) from gnm01 a with (nolock), gnm02 b with (nolock) where a.gna00c = @compid and a.gna00c = b.gnb00c and a.gna01c = b.gnb01c and a.gna80d >= @fromdate and a.gna80d <= @todate and isnull(b.gnb02c, '') <> '' -- 充值的卡 insert #collect_detail(staff, memid, cardid, type, prj_type, prj, qty, amt) select isnull(gcl09c, ''), '', gcl03c, 1, gca02c, gcl03c, 1, gcl07f * isnull(gcl16f, 1) from gcm10 a with (nolock), gcm01 b with (nolock) where a.gcl00c = @compid and a.gcl00c = b.gca00c and a.gcl03c = b.gca01c and a.gcl06i = 0 and a.gcl04c = '2' and a.gcl80d >= @fromdate and a.gcl80d <= @todate and isnull(b.gca02c, '') <> '' -- 员工服务 insert #collect_detail(staff, memid, cardid, type, prj_type, prj, qty, amt) select isnull(ggb12c, ''), '', gga09c, 2, '', ggb03c, ggb05f, ggb11f from ggm01 a with (nolock), ggm02 b with (nolock) where a.gga00c = b.ggb00c and a.gga01c = b.ggb01c and a.gga00c = @compid and a.gga80d >= @fromdate and a.gga80d <= @todate -- 员工所卖产品 insert #collect_detail(staff, memid, cardid, type, prj_type, prj, qty, amt) select isnull(ggc12c, ''), '', gga09c, 3, '', ggc03c, ggc05f, ggc11f * isnull(ggc14f, 1) from ggm01 a with (nolock), ggm03 b with (nolock) where a.gga00c = b.ggc00c and a.gga01c = b.ggc01c and a.gga00c = @compid and a.gga80d >= @fromdate and a.gga80d <= @todate -- 统计欠款,还款 -- 所有欠款,还款由第一销售负责监督 insert #collect_detail(staff, memid, cardid, type, prj_type, prj, qty, amt) select isnull(gcl09c, ''), '', gcl03c, 4, '4', '4', 1, gcl07f from gcm10 a with (nolock) where a.gcl00c = @compid and a.gcl06i = 5 and a.gcl80d >= @fromdate and a.gcl80d <= @todate insert #collect_detail(staff, memid, cardid, type, prj_type, prj, qty, amt) select isnull(gcl09c, ''), '', gcl03c, 5, '5', '5', 1, gcl07f from gcm10 a with (nolock) where a.gcl00c = @compid and a.gcl06i = 6 and a.gcl80d >= @fromdate and a.gcl80d <= @todate end else begin -- 卖卡 insert #collect_detail(staff, memid, cardid, type, prj_type, prj, qty, amt) select gna20c, '', gnb03c, 1, gnb02c, gnb03c, 1, gnb08f * isnull(gna27f, 1) from gnm01 a with (nolock), gnm02 b with (nolock) where a.gna00c = @compid and a.gna00c = b.gnb00c and a.gna01c = b.gnb01c and a.gna80d >= @fromdate and a.gna80d <= @todate and isnull(a.gna20c, '') <> '' and isnull(b.gnb02c, '') <> '' insert #collect_detail(staff, memid, cardid, type, prj_type, prj, qty, amt) select gna28c, '', gnb03c, 1, gnb02c, gnb03c, 1, gnb08f * isnull(gna29f, 0) from gnm01 a with (nolock), gnm02 b with (nolock) where a.gna00c = @compid and a.gna00c = b.gnb00c and a.gna01c = b.gnb01c and a.gna80d >= @fromdate and a.gna80d <= @todate and isnull(a.gna28c, '') <> '' and isnull(b.gnb02c, '') <> '' insert #collect_detail(staff, memid, cardid, type, prj_type, prj, qty, amt) select gna30c, '', gnb03c, 1, gnb02c, gnb03c, 1, gnb08f * isnull(gna31f, 0) from gnm01 a with (nolock), gnm02 b with (nolock) where a.gna00c = @compid and a.gna00c = b.gnb00c and a.gna01c = b.gnb01c and a.gna80d >= @fromdate and a.gna80d <= @todate and isnull(a.gna30c, '') <> '' and isnull(b.gnb02c, '') <> '' -- 充值的卡 insert #collect_detail(staff, memid, cardid, type, prj_type, prj, qty, amt) select gcl09c, '', gcl03c, 1, gca02c, gcl03c, 1, gcl07f * isnull(gcl16f, 1) from gcm10 a with (nolock), gcm01 b with (nolock) where a.gcl00c = @compid and a.gcl00c = b.gca00c and a.gcl03c = b.gca01c and a.gcl06i = 0 and a.gcl04c = '2' and a.gcl80d >= @fromdate and a.gcl80d <= @todate and isnull(a.gcl09c, '') <> '' and isnull(b.gca02c, '') <> '' -- 充卡,第二销售 insert #collect_detail(staff, memid, cardid, type, prj_type, prj, qty, amt) select gcl17c, '', gcl03c, 1, gca02c, gcl03c, 1, gcl07f * isnull(gcl18f, 0) from gcm10 a with (nolock), gcm01 b with (nolock) where a.gcl00c = @compid and a.gcl00c = b.gca00c and a.gcl03c = b.gca01c and a.gcl06i = 0 and a.gcl04c = '2' and a.gcl80d >= @fromdate and a.gcl80d <= @todate and isnull(a.gcl17c, '') <> '' and isnull(b.gca02c, '') <> '' -- 充卡, 第三销售 insert #collect_detail(staff, memid, cardid, type, prj_type, prj, qty, amt) select gcl19c, '', gcl03c, 1, gca02c, gcl03c, 1, gcl07f * isnull(gcl20f, 0) from gcm10 a with (nolock), gcm01 b with (nolock) where a.gcl00c = @compid and a.gcl00c = b.gca00c and a.gcl03c = b.gca01c and a.gcl06i = 0 and a.gcl04c = '2' and a.gcl80d >= @fromdate and a.gcl80d <= @todate and isnull(a.gcl19c, '') <> '' and isnull(b.gca02c, '') <> '' -- 员工服务 insert #collect_detail(staff, memid, cardid, type, prj_type, prj, qty, amt) select ggb12c, '', gga09c, 2, '', ggb03c, ggb05f, ggb11f from ggm01 a with (nolock), ggm02 b with (nolock) where a.gga00c = b.ggb00c and a.gga01c = b.ggb01c and a.gga00c = @compid and a.gga80d >= @fromdate and a.gga80d <= @todate and isnull(b.ggb12c, '') <> '' insert #collect_detail(staff, memid, cardid, type, prj_type, prj, qty, amt) select ggb13c, '', gga09c, 2, '', ggb03c, ggb05f, ggb11f from ggm01 a with (nolock), ggm02 b with (nolock) where a.gga00c = b.ggb00c and a.gga01c = b.ggb01c and a.gga00c = @compid and a.gga80d >= @fromdate and a.gga80d <= @todate and isnull(b.ggb13c, '') <> '' insert #collect_detail(staff, memid, cardid, type, prj_type, prj, qty, amt) select ggb14c, '', gga09c, 2, '', ggb03c, ggb05f, ggb11f from ggm01 a with (nolock), ggm02 b with (nolock) where a.gga00c = b.ggb00c and a.gga01c = b.ggb01c and a.gga00c = @compid and a.gga80d >= @fromdate and a.gga80d <= @todate and isnull(b.ggb14c, '') <> '' -- 员工所卖产品 insert #collect_detail(staff, memid, cardid, type, prj_type, prj, qty, amt) select ggc12c, '', gga09c, 3, '', ggc03c, ggc05f, ggc11f * isnull(ggc14f, 1) from ggm01 a with (nolock), ggm03 b with (nolock) where a.gga00c = b.ggc00c and a.gga01c = b.ggc01c and a.gga00c = @compid and a.gga80d >= @fromdate and a.gga80d <= @todate and isnull(b.ggc12c, '') <> '' insert #collect_detail(staff, memid, cardid, type, prj_type, prj, qty, amt) select ggc15c, '', gga09c, 3, '', ggc03c, ggc05f, ggc11f * isnull(ggc16f, 1) from ggm01 a with (nolock), ggm03 b with (nolock) where a.gga00c = b.ggc00c and a.gga01c = b.ggc01c and a.gga00c = @compid and a.gga80d >= @fromdate and a.gga80d <= @todate and isnull(b.ggc15c, '') <> '' insert #collect_detail(staff, memid, cardid, type, prj_type, prj, qty, amt) select ggc17c, '', gga09c, 3, '', ggc03c, ggc05f, ggc11f * isnull(ggc18f, 1) from ggm01 a with (nolock), ggm03 b with (nolock) where a.gga00c = b.ggc00c and a.gga01c = b.ggc01c and a.gga00c = @compid and a.gga80d >= @fromdate and a.gga80d <= @todate and isnull(b.ggc17c, '') <> '' -- 统计欠款,还款 -- 所有欠款,还款由第一销售负责监督 insert #collect_detail(staff, memid, cardid, type, prj_type, prj, qty, amt) select gcl09c, '', gcl03c, 4, '4', '4', 1, gcl07f from gcm10 a with (nolock) where a.gcl00c = @compid and a.gcl06i = 5 and a.gcl80d >= @fromdate and a.gcl80d <= @todate and isnull(a.gcl09c, '') <> '' insert #collect_detail(staff, memid, cardid, type, prj_type, prj, qty, amt) select gcl09c, '', gcl03c, 5, '5', '5', 1, gcl07f from gcm10 a with (nolock) where a.gcl00c = @compid and a.gcl06i = 6 and a.gcl80d >= @fromdate and a.gcl80d <= @todate and isnull(a.gcl09c, '') <> '' end -- 将类别栏位设置 update a set prj_type = isnull(b.gda13c, '') -- gda13c 统计分类 from #collect_detail a, gdm01 b where a.type = 2 and b.gda00c = @compid and a.prj = b.gda01c update a set prj_type = isnull(b.gfa24c, '') -- gfa24c 统计分类 from #collect_detail a, gfm01 b where a.type = 3 and b.gfa00c = @compid and a.prj = b.gfa01c if @data_scope = 1 begin insert #group_data(staff, memid, cardid, type, prj_type, qty, amt) select staff, memid, cardid, type, prj_type, sum(qty), sum(amt) from #collect_detail group by staff, memid, cardid, type, prj_type end else if @data_scope = 2 or @data_scope = 3 begin insert #group_data(staff, memid, cardid, type, prj_type, qty, amt) select '', memid, cardid, type, prj_type, sum(qty), sum(amt) from #collect_detail group by memid, cardid, type, prj_type end insert #idd_info(type, prj_type, typename) select 2, gsb02c, gsb03c from gsm02 where gsb00c = @compid and gsb01c = 'AJ' insert #idd_info(type, prj_type, typename) select 1, gak01c, gak02c from gam10 where gak00c = @compid insert #idd_info(type, prj_type, typename) select 3, gsb02c, gsb03c from gsm02 where gsb00c = @compid and gsb01c = 'AL' insert #idd_info(type, prj_type, typename) values(4, '4', '欠款') insert #idd_info(type, prj_type, typename) values(5, '5', '还款') declare @tcol int-- 添加列名 select @tcol = count(*) from #idd_info declare @t int set @t = 1 declare @sql nvarchar(max) set @sql = '' while @t <= @tcol begin set @sql = @sql + 'alter table #empl_result add col' + convert(varchar(10), @t) + ' float null ' set @t = @t + 1 end exec sp_executesql @sql set @sql = '' set @t = 1 while @t < @tcol begin if @data_scope = 1 begin set @sql = 'update a set a.col' + convert(varchar(10), @t) + ' = b.amt from #empl_result a, #group_data b, #idd_info c where a.staff = b.staff and a.cardid = b.cardid and b.type = c.type and b.prj_type = c.prj_type and c.idd = ' + convert(varchar(10), @t) end else if @data_scope = 2 or @data_scope = 3 begin set @sql = 'update a set a.col' + convert(varchar(10), @t) + ' = b.amt from #empl_result a, #group_data b, #idd_info c where a.cardid = b.cardid and b.type = c.type and b.prj_type = c.prj_type and c.idd = ' + convert(varchar(10), @t) end exec sp_executesql @sql set @t = @t + 1 end if @data_scope = 3 begin update #empl_result set staff = '9999' end select * from #idd_info order by idd select * from #empl_result order by staff, idd drop table #collect_detail drop table #group_data drop table #empl_result drop table #idd_info end GO /****** Object: StoredProcedure [dbo].[upg_analyse_emplyee_ability_lm2] Script Date: 06/12/2017 06:19:25 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_analyse_emplyee_ability_lm2] ( @compid varchar(10), -- 公司别 @fromstaff varchar(20), -- 员工 @tostaff varchar(20), -- @fromdate varchar(10), -- 开始日期 @todate varchar(10), -- 截至日期 @ret_type int, -- 0-只返回类型 1-返回所有分析结果 @data_scope int -- 1-员工自己负责自己服务 2-自己负责,不管谁服务 3-不关心谁负责与服务 ) as begin declare @unlimited_staff int if (isnull(@fromstaff, '') = '*' or (isnull(@fromstaff, '') = '' and isnull(@tostaff, '') = '')) or @data_scope = 3 begin select @fromstaff = min(haa01c), @tostaff = max(haa01c) from ham01 where haa00c = @compid select @unlimited_staff = 1 end else begin select @unlimited_staff = 0 end -- 员工所卖卡 create table #collect_detail ( idd int identity not null, staff varchar(20) not null, memid varchar(20) null, cardid varchar(20) null, type int not null, -- 1-卡 2-服务 3-产品 prj_type varchar(10) null, -- 产品类别,卡类别, 服务项目类别 prj varchar(20) not null, qty float null, amt float null, primary key(idd) ) create table #group_data ( idd int identity not null, staff varchar(20) not null, memid varchar(20) null, cardid varchar(20) null, type int not null, -- 1-卡 2-服务 3-产品, 4-欠款, 5-还款 prj_type varchar(10) not null, -- 产品类别,卡类别, 服务项目类别 qty float null, amt float null, primary key(idd) ) create table #empl_result ( idd int identity not null, staff varchar(20) not null, cardid varchar(20) not null, primary key(idd) ) create table #idd_info ( idd int identity not null, type int not null, prj_type varchar(10) not null, typename varchar(40) null, primary key(idd) ) insert #empl_result(staff, cardid) select gba10c, gba23c from gbm01 where gba00c = @compid and gba10c >= @fromstaff and gba10c <= @tostaff and isnull(gba23c, '') <> '' and isnull(gba10c, '') <> '' if (@unlimited_staff = 1) begin -- 卖卡 insert #collect_detail(staff, memid, cardid, type, prj_type, prj, qty, amt) select isnull(gna20c, ''), '', gnb03c, 1, gnb02c, gnb03c, 1, gnb08f * isnull(gna27f, 1) from gnm01 a with (nolock), gnm02 b with (nolock) where a.gna00c = @compid and a.gna00c = b.gnb00c and a.gna01c = b.gnb01c and a.gna80d >= @fromdate and a.gna80d <= @todate and isnull(b.gnb02c, '') <> '' -- 充值的卡 insert #collect_detail(staff, memid, cardid, type, prj_type, prj, qty, amt) select isnull(gcl09c, ''), '', gcl03c, 1, gca02c, gcl03c, 1, gcl07f * isnull(gcl16f, 1) from gcm10 a with (nolock), gcm01 b with (nolock) where a.gcl00c = @compid and a.gcl00c = b.gca00c and a.gcl03c = b.gca01c and a.gcl06i = 0 and a.gcl04c = '2' and a.gcl80d >= @fromdate and a.gcl80d <= @todate and isnull(b.gca02c, '') <> '' -- 员工服务 insert #collect_detail(staff, memid, cardid, type, prj_type, prj, qty, amt) select isnull(ggb12c, ''), '', gga09c, 2, '', ggb03c, ggb05f, ggb11f from ggm01 a with (nolock), ggm02 b with (nolock) where a.gga00c = b.ggb00c and a.gga01c = b.ggb01c and a.gga00c = @compid and a.gga80d >= @fromdate and a.gga80d <= @todate -- 员工所卖产品 insert #collect_detail(staff, memid, cardid, type, prj_type, prj, qty, amt) select isnull(ggc12c, ''), '', gga09c, 3, '', ggc03c, ggc05f, ggc11f * isnull(ggc14f, 1) from ggm01 a with (nolock), ggm03 b with (nolock) where a.gga00c = b.ggc00c and a.gga01c = b.ggc01c and a.gga00c = @compid and a.gga80d >= @fromdate and a.gga80d <= @todate -- 统计欠款,还款 -- 所有欠款,还款由第一销售负责监督 insert #collect_detail(staff, memid, cardid, type, prj_type, prj, qty, amt) select isnull(gcl09c, ''), '', gcl03c, 4, '4', '4', 1, gcl07f from gcm10 a with (nolock) where a.gcl00c = @compid and a.gcl06i = 5 and a.gcl80d >= @fromdate and a.gcl80d <= @todate insert #collect_detail(staff, memid, cardid, type, prj_type, prj, qty, amt) select isnull(gcl09c, ''), '', gcl03c, 5, '5', '5', 1, gcl07f from gcm10 a with (nolock) where a.gcl00c = @compid and a.gcl06i = 6 and a.gcl80d >= @fromdate and a.gcl80d <= @todate end else begin -- 卖卡 insert #collect_detail(staff, memid, cardid, type, prj_type, prj, qty, amt) select gna20c, '', gnb03c, 1, gnb02c, gnb03c, 1, gnb08f * isnull(gna27f, 1) from gnm01 a with (nolock), gnm02 b with (nolock) where a.gna00c = @compid and a.gna00c = b.gnb00c and a.gna01c = b.gnb01c and a.gna80d >= @fromdate and a.gna80d <= @todate and isnull(a.gna20c, '') <> '' and isnull(b.gnb02c, '') <> '' insert #collect_detail(staff, memid, cardid, type, prj_type, prj, qty, amt) select gna28c, '', gnb03c, 1, gnb02c, gnb03c, 1, gnb08f * isnull(gna29f, 0) from gnm01 a with (nolock), gnm02 b with (nolock) where a.gna00c = @compid and a.gna00c = b.gnb00c and a.gna01c = b.gnb01c and a.gna80d >= @fromdate and a.gna80d <= @todate and isnull(a.gna28c, '') <> '' and isnull(b.gnb02c, '') <> '' insert #collect_detail(staff, memid, cardid, type, prj_type, prj, qty, amt) select gna30c, '', gnb03c, 1, gnb02c, gnb03c, 1, gnb08f * isnull(gna31f, 0) from gnm01 a with (nolock), gnm02 b with (nolock) where a.gna00c = @compid and a.gna00c = b.gnb00c and a.gna01c = b.gnb01c and a.gna80d >= @fromdate and a.gna80d <= @todate and isnull(a.gna30c, '') <> '' and isnull(b.gnb02c, '') <> '' -- 充值的卡 insert #collect_detail(staff, memid, cardid, type, prj_type, prj, qty, amt) select gcl09c, '', gcl03c, 1, gca02c, gcl03c, 1, gcl07f * isnull(gcl16f, 1) from gcm10 a with (nolock), gcm01 b with (nolock) where a.gcl00c = @compid and a.gcl00c = b.gca00c and a.gcl03c = b.gca01c and a.gcl06i = 0 and a.gcl04c = '2' and a.gcl80d >= @fromdate and a.gcl80d <= @todate and isnull(a.gcl09c, '') <> '' and isnull(b.gca02c, '') <> '' -- 充卡,第二销售 insert #collect_detail(staff, memid, cardid, type, prj_type, prj, qty, amt) select gcl17c, '', gcl03c, 1, gca02c, gcl03c, 1, gcl07f * isnull(gcl18f, 0) from gcm10 a with (nolock), gcm01 b with (nolock) where a.gcl00c = @compid and a.gcl00c = b.gca00c and a.gcl03c = b.gca01c and a.gcl06i = 0 and a.gcl04c = '2' and a.gcl80d >= @fromdate and a.gcl80d <= @todate and isnull(a.gcl17c, '') <> '' and isnull(b.gca02c, '') <> '' -- 充卡, 第三销售 insert #collect_detail(staff, memid, cardid, type, prj_type, prj, qty, amt) select gcl19c, '', gcl03c, 1, gca02c, gcl03c, 1, gcl07f * isnull(gcl20f, 0) from gcm10 a with (nolock), gcm01 b with (nolock) where a.gcl00c = @compid and a.gcl00c = b.gca00c and a.gcl03c = b.gca01c and a.gcl06i = 0 and a.gcl04c = '2' and a.gcl80d >= @fromdate and a.gcl80d <= @todate and isnull(a.gcl19c, '') <> '' and isnull(b.gca02c, '') <> '' -- 员工服务 insert #collect_detail(staff, memid, cardid, type, prj_type, prj, qty, amt) select ggb12c, '', gga09c, 2, '', ggb03c, ggb05f, ggb11f from ggm01 a with (nolock), ggm02 b with (nolock) where a.gga00c = b.ggb00c and a.gga01c = b.ggb01c and a.gga00c = @compid and a.gga80d >= @fromdate and a.gga80d <= @todate and isnull(b.ggb12c, '') <> '' insert #collect_detail(staff, memid, cardid, type, prj_type, prj, qty, amt) select ggb13c, '', gga09c, 2, '', ggb03c, ggb05f, ggb11f from ggm01 a with (nolock), ggm02 b with (nolock) where a.gga00c = b.ggb00c and a.gga01c = b.ggb01c and a.gga00c = @compid and a.gga80d >= @fromdate and a.gga80d <= @todate and isnull(b.ggb13c, '') <> '' insert #collect_detail(staff, memid, cardid, type, prj_type, prj, qty, amt) select ggb14c, '', gga09c, 2, '', ggb03c, ggb05f, ggb11f from ggm01 a with (nolock), ggm02 b with (nolock) where a.gga00c = b.ggb00c and a.gga01c = b.ggb01c and a.gga00c = @compid and a.gga80d >= @fromdate and a.gga80d <= @todate and isnull(b.ggb14c, '') <> '' -- 员工所卖产品 insert #collect_detail(staff, memid, cardid, type, prj_type, prj, qty, amt) select ggc12c, '', gga09c, 3, '', ggc03c, ggc05f, ggc11f * isnull(ggc14f, 1) from ggm01 a with (nolock), ggm03 b with (nolock) where a.gga00c = b.ggc00c and a.gga01c = b.ggc01c and a.gga00c = @compid and a.gga80d >= @fromdate and a.gga80d <= @todate and isnull(b.ggc12c, '') <> '' insert #collect_detail(staff, memid, cardid, type, prj_type, prj, qty, amt) select ggc15c, '', gga09c, 3, '', ggc03c, ggc05f, ggc11f * isnull(ggc16f, 1) from ggm01 a with (nolock), ggm03 b with (nolock) where a.gga00c = b.ggc00c and a.gga01c = b.ggc01c and a.gga00c = @compid and a.gga80d >= @fromdate and a.gga80d <= @todate and isnull(b.ggc15c, '') <> '' insert #collect_detail(staff, memid, cardid, type, prj_type, prj, qty, amt) select ggc17c, '', gga09c, 3, '', ggc03c, ggc05f, ggc11f * isnull(ggc18f, 1) from ggm01 a with (nolock), ggm03 b with (nolock) where a.gga00c = b.ggc00c and a.gga01c = b.ggc01c and a.gga00c = @compid and a.gga80d >= @fromdate and a.gga80d <= @todate and isnull(b.ggc17c, '') <> '' -- 统计欠款,还款 -- 所有欠款,还款由第一销售负责监督 insert #collect_detail(staff, memid, cardid, type, prj_type, prj, qty, amt) select gcl09c, '', gcl03c, 4, '4', '4', 1, gcl07f from gcm10 a with (nolock) where a.gcl00c = @compid and a.gcl06i = 5 and a.gcl80d >= @fromdate and a.gcl80d <= @todate and isnull(a.gcl09c, '') <> '' insert #collect_detail(staff, memid, cardid, type, prj_type, prj, qty, amt) select gcl09c, '', gcl03c, 5, '5', '5', 1, gcl07f from gcm10 a with (nolock) where a.gcl00c = @compid and a.gcl06i = 6 and a.gcl80d >= @fromdate and a.gcl80d <= @todate and isnull(a.gcl09c, '') <> '' end -- 将类别栏位设置 update a set prj_type = isnull(b.gda13c, '') -- gda13c 统计分类 from #collect_detail a, gdm01 b where a.type = 2 and b.gda00c = @compid and a.prj = b.gda01c update a set prj_type = isnull(b.gfa24c, '') -- gfa24c 统计分类 from #collect_detail a, gfm01 b where a.type = 3 and b.gfa00c = @compid and a.prj = b.gfa01c if @data_scope = 1 begin insert #group_data(staff, memid, cardid, type, prj_type, qty, amt) select staff, memid, cardid, type, prj_type, sum(qty), sum(amt) from #collect_detail group by staff, memid, cardid, type, prj_type end else if @data_scope = 2 or @data_scope = 3 begin insert #group_data(staff, memid, cardid, type, prj_type, qty, amt) select '', memid, cardid, type, prj_type, sum(qty), sum(amt) from #collect_detail group by memid, cardid, type, prj_type end insert #idd_info(type, prj_type, typename) select 2, gsb02c, gsb03c from gsm02 where gsb00c = @compid and gsb01c = 'AJ' insert #idd_info(type, prj_type, typename) select 1, gak01c, gak02c from gam10 where gak00c = @compid insert #idd_info(type, prj_type, typename) select 3, gsb02c, gsb03c from gsm02 where gsb00c = @compid and gsb01c = 'AL' insert #idd_info(type, prj_type, typename) values(4, '4', '欠款') insert #idd_info(type, prj_type, typename) values(5, '5', '还款') declare @tcol int-- 添加列名 select @tcol = count(*) from #idd_info declare @t int set @t = 1 declare @sql nvarchar(max) set @sql = '' while @t <= @tcol begin set @sql = @sql + 'alter table #empl_result add col' + convert(varchar(10), @t) + ' float null ' set @t = @t + 1 end exec sp_executesql @sql set @sql = '' set @t = 1 while @t < @tcol begin if @data_scope = 1 begin set @sql = 'update a set a.col' + convert(varchar(10), @t) + ' = b.amt from #empl_result a, #group_data b, #idd_info c where a.staff = b.staff and a.cardid = b.cardid and b.type = c.type and b.prj_type = c.prj_type and c.idd = ' + convert(varchar(10), @t) end else if @data_scope = 2 or @data_scope = 3 begin set @sql = 'update a set a.col' + convert(varchar(10), @t) + ' = b.amt from #empl_result a, #group_data b, #idd_info c where a.cardid = b.cardid and b.type = c.type and b.prj_type = c.prj_type and c.idd = ' + convert(varchar(10), @t) end exec sp_executesql @sql set @t = @t + 1 end if @data_scope = 3 begin update #empl_result set staff = '9999' end select isnull(idd,'') idd,isnull(staff,'') staff,isnull(cardid,'') cardid from #empl_result order by staff, idd drop table #collect_detail drop table #group_data drop table #empl_result drop table #idd_info end GO /****** Object: StoredProcedure [dbo].[upg_analyse_emplyee_ability_lm1] Script Date: 06/12/2017 06:19:25 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_analyse_emplyee_ability_lm1] ( @compid varchar(10), -- 公司别 @fromstaff varchar(20), -- 员工 @tostaff varchar(20), -- @fromdate varchar(10), -- 开始日期 @todate varchar(10), -- 截至日期 @ret_type int, -- 0-只返回类型 1-返回所有分析结果 @data_scope int -- 1-员工自己负责自己服务 2-自己负责,不管谁服务 3-不关心谁负责与服务 ) as begin declare @unlimited_staff int if (isnull(@fromstaff, '') = '*' or (isnull(@fromstaff, '') = '' and isnull(@tostaff, '') = '')) or @data_scope = 3 begin select @fromstaff = min(haa01c), @tostaff = max(haa01c) from ham01 where haa00c = @compid select @unlimited_staff = 1 end else begin select @unlimited_staff = 0 end -- 员工所卖卡 create table #collect_detail ( idd int identity not null, staff varchar(20) not null, memid varchar(20) null, cardid varchar(20) null, type int not null, -- 1-卡 2-服务 3-产品 prj_type varchar(10) null, -- 产品类别,卡类别, 服务项目类别 prj varchar(20) not null, qty float null, amt float null, primary key(idd) ) create table #group_data ( idd int identity not null, staff varchar(20) not null, memid varchar(20) null, cardid varchar(20) null, type int not null, -- 1-卡 2-服务 3-产品, 4-欠款, 5-还款 prj_type varchar(10) not null, -- 产品类别,卡类别, 服务项目类别 qty float null, amt float null, primary key(idd) ) create table #idd_info ( idd int identity not null, type int not null, prj_type varchar(10) not null, typename varchar(40) null, primary key(idd) ) if (@unlimited_staff = 1) begin -- 卖卡 insert #collect_detail(staff, memid, cardid, type, prj_type, prj, qty, amt) select isnull(gna20c, ''), '', gnb03c, 1, gnb02c, gnb03c, 1, gnb08f * isnull(gna27f, 1) from gnm01 a with (nolock), gnm02 b with (nolock) where a.gna00c = @compid and a.gna00c = b.gnb00c and a.gna01c = b.gnb01c and a.gna80d >= @fromdate and a.gna80d <= @todate and isnull(b.gnb02c, '') <> '' -- 充值的卡 insert #collect_detail(staff, memid, cardid, type, prj_type, prj, qty, amt) select isnull(gcl09c, ''), '', gcl03c, 1, gca02c, gcl03c, 1, gcl07f * isnull(gcl16f, 1) from gcm10 a with (nolock), gcm01 b with (nolock) where a.gcl00c = @compid and a.gcl00c = b.gca00c and a.gcl03c = b.gca01c and a.gcl06i = 0 and a.gcl04c = '2' and a.gcl80d >= @fromdate and a.gcl80d <= @todate and isnull(b.gca02c, '') <> '' -- 员工服务 insert #collect_detail(staff, memid, cardid, type, prj_type, prj, qty, amt) select isnull(ggb12c, ''), '', gga09c, 2, '', ggb03c, ggb05f, ggb11f from ggm01 a with (nolock), ggm02 b with (nolock) where a.gga00c = b.ggb00c and a.gga01c = b.ggb01c and a.gga00c = @compid and a.gga80d >= @fromdate and a.gga80d <= @todate -- 员工所卖产品 insert #collect_detail(staff, memid, cardid, type, prj_type, prj, qty, amt) select isnull(ggc12c, ''), '', gga09c, 3, '', ggc03c, ggc05f, ggc11f * isnull(ggc14f, 1) from ggm01 a with (nolock), ggm03 b with (nolock) where a.gga00c = b.ggc00c and a.gga01c = b.ggc01c and a.gga00c = @compid and a.gga80d >= @fromdate and a.gga80d <= @todate -- 统计欠款,还款 -- 所有欠款,还款由第一销售负责监督 insert #collect_detail(staff, memid, cardid, type, prj_type, prj, qty, amt) select isnull(gcl09c, ''), '', gcl03c, 4, '4', '4', 1, gcl07f from gcm10 a with (nolock) where a.gcl00c = @compid and a.gcl06i = 5 and a.gcl80d >= @fromdate and a.gcl80d <= @todate insert #collect_detail(staff, memid, cardid, type, prj_type, prj, qty, amt) select isnull(gcl09c, ''), '', gcl03c, 5, '5', '5', 1, gcl07f from gcm10 a with (nolock) where a.gcl00c = @compid and a.gcl06i = 6 and a.gcl80d >= @fromdate and a.gcl80d <= @todate end else begin -- 卖卡 insert #collect_detail(staff, memid, cardid, type, prj_type, prj, qty, amt) select gna20c, '', gnb03c, 1, gnb02c, gnb03c, 1, gnb08f * isnull(gna27f, 1) from gnm01 a with (nolock), gnm02 b with (nolock) where a.gna00c = @compid and a.gna00c = b.gnb00c and a.gna01c = b.gnb01c and a.gna80d >= @fromdate and a.gna80d <= @todate and isnull(a.gna20c, '') <> '' and isnull(b.gnb02c, '') <> '' insert #collect_detail(staff, memid, cardid, type, prj_type, prj, qty, amt) select gna28c, '', gnb03c, 1, gnb02c, gnb03c, 1, gnb08f * isnull(gna29f, 0) from gnm01 a with (nolock), gnm02 b with (nolock) where a.gna00c = @compid and a.gna00c = b.gnb00c and a.gna01c = b.gnb01c and a.gna80d >= @fromdate and a.gna80d <= @todate and isnull(a.gna28c, '') <> '' and isnull(b.gnb02c, '') <> '' insert #collect_detail(staff, memid, cardid, type, prj_type, prj, qty, amt) select gna30c, '', gnb03c, 1, gnb02c, gnb03c, 1, gnb08f * isnull(gna31f, 0) from gnm01 a with (nolock), gnm02 b with (nolock) where a.gna00c = @compid and a.gna00c = b.gnb00c and a.gna01c = b.gnb01c and a.gna80d >= @fromdate and a.gna80d <= @todate and isnull(a.gna30c, '') <> '' and isnull(b.gnb02c, '') <> '' -- 充值的卡 insert #collect_detail(staff, memid, cardid, type, prj_type, prj, qty, amt) select gcl09c, '', gcl03c, 1, gca02c, gcl03c, 1, gcl07f * isnull(gcl16f, 1) from gcm10 a with (nolock), gcm01 b with (nolock) where a.gcl00c = @compid and a.gcl00c = b.gca00c and a.gcl03c = b.gca01c and a.gcl06i = 0 and a.gcl04c = '2' and a.gcl80d >= @fromdate and a.gcl80d <= @todate and isnull(a.gcl09c, '') <> '' and isnull(b.gca02c, '') <> '' -- 充卡,第二销售 insert #collect_detail(staff, memid, cardid, type, prj_type, prj, qty, amt) select gcl17c, '', gcl03c, 1, gca02c, gcl03c, 1, gcl07f * isnull(gcl18f, 0) from gcm10 a with (nolock), gcm01 b with (nolock) where a.gcl00c = @compid and a.gcl00c = b.gca00c and a.gcl03c = b.gca01c and a.gcl06i = 0 and a.gcl04c = '2' and a.gcl80d >= @fromdate and a.gcl80d <= @todate and isnull(a.gcl17c, '') <> '' and isnull(b.gca02c, '') <> '' -- 充卡, 第三销售 insert #collect_detail(staff, memid, cardid, type, prj_type, prj, qty, amt) select gcl19c, '', gcl03c, 1, gca02c, gcl03c, 1, gcl07f * isnull(gcl20f, 0) from gcm10 a with (nolock), gcm01 b with (nolock) where a.gcl00c = @compid and a.gcl00c = b.gca00c and a.gcl03c = b.gca01c and a.gcl06i = 0 and a.gcl04c = '2' and a.gcl80d >= @fromdate and a.gcl80d <= @todate and isnull(a.gcl19c, '') <> '' and isnull(b.gca02c, '') <> '' -- 员工服务 insert #collect_detail(staff, memid, cardid, type, prj_type, prj, qty, amt) select ggb12c, '', gga09c, 2, '', ggb03c, ggb05f, ggb11f from ggm01 a with (nolock), ggm02 b with (nolock) where a.gga00c = b.ggb00c and a.gga01c = b.ggb01c and a.gga00c = @compid and a.gga80d >= @fromdate and a.gga80d <= @todate and isnull(b.ggb12c, '') <> '' insert #collect_detail(staff, memid, cardid, type, prj_type, prj, qty, amt) select ggb13c, '', gga09c, 2, '', ggb03c, ggb05f, ggb11f from ggm01 a with (nolock), ggm02 b with (nolock) where a.gga00c = b.ggb00c and a.gga01c = b.ggb01c and a.gga00c = @compid and a.gga80d >= @fromdate and a.gga80d <= @todate and isnull(b.ggb13c, '') <> '' insert #collect_detail(staff, memid, cardid, type, prj_type, prj, qty, amt) select ggb14c, '', gga09c, 2, '', ggb03c, ggb05f, ggb11f from ggm01 a with (nolock), ggm02 b with (nolock) where a.gga00c = b.ggb00c and a.gga01c = b.ggb01c and a.gga00c = @compid and a.gga80d >= @fromdate and a.gga80d <= @todate and isnull(b.ggb14c, '') <> '' -- 员工所卖产品 insert #collect_detail(staff, memid, cardid, type, prj_type, prj, qty, amt) select ggc12c, '', gga09c, 3, '', ggc03c, ggc05f, ggc11f * isnull(ggc14f, 1) from ggm01 a with (nolock), ggm03 b with (nolock) where a.gga00c = b.ggc00c and a.gga01c = b.ggc01c and a.gga00c = @compid and a.gga80d >= @fromdate and a.gga80d <= @todate and isnull(b.ggc12c, '') <> '' insert #collect_detail(staff, memid, cardid, type, prj_type, prj, qty, amt) select ggc15c, '', gga09c, 3, '', ggc03c, ggc05f, ggc11f * isnull(ggc16f, 1) from ggm01 a with (nolock), ggm03 b with (nolock) where a.gga00c = b.ggc00c and a.gga01c = b.ggc01c and a.gga00c = @compid and a.gga80d >= @fromdate and a.gga80d <= @todate and isnull(b.ggc15c, '') <> '' insert #collect_detail(staff, memid, cardid, type, prj_type, prj, qty, amt) select ggc17c, '', gga09c, 3, '', ggc03c, ggc05f, ggc11f * isnull(ggc18f, 1) from ggm01 a with (nolock), ggm03 b with (nolock) where a.gga00c = b.ggc00c and a.gga01c = b.ggc01c and a.gga00c = @compid and a.gga80d >= @fromdate and a.gga80d <= @todate and isnull(b.ggc17c, '') <> '' -- 统计欠款,还款 -- 所有欠款,还款由第一销售负责监督 insert #collect_detail(staff, memid, cardid, type, prj_type, prj, qty, amt) select gcl09c, '', gcl03c, 4, '4', '4', 1, gcl07f from gcm10 a with (nolock) where a.gcl00c = @compid and a.gcl06i = 5 and a.gcl80d >= @fromdate and a.gcl80d <= @todate and isnull(a.gcl09c, '') <> '' insert #collect_detail(staff, memid, cardid, type, prj_type, prj, qty, amt) select gcl09c, '', gcl03c, 5, '5', '5', 1, gcl07f from gcm10 a with (nolock) where a.gcl00c = @compid and a.gcl06i = 6 and a.gcl80d >= @fromdate and a.gcl80d <= @todate and isnull(a.gcl09c, '') <> '' end -- 将类别栏位设置 update a set prj_type = isnull(b.gda13c, '') -- gda13c 统计分类 from #collect_detail a, gdm01 b where a.type = 2 and b.gda00c = @compid and a.prj = b.gda01c update a set prj_type = isnull(b.gfa24c, '') -- gfa24c 统计分类 from #collect_detail a, gfm01 b where a.type = 3 and b.gfa00c = @compid and a.prj = b.gfa01c if @data_scope = 1 begin insert #group_data(staff, memid, cardid, type, prj_type, qty, amt) select staff, memid, cardid, type, prj_type, sum(qty), sum(amt) from #collect_detail group by staff, memid, cardid, type, prj_type end else if @data_scope = 2 or @data_scope = 3 begin insert #group_data(staff, memid, cardid, type, prj_type, qty, amt) select '', memid, cardid, type, prj_type, sum(qty), sum(amt) from #collect_detail group by memid, cardid, type, prj_type end insert #idd_info(type, prj_type, typename) select 2, gsb02c, gsb03c from gsm02 where gsb00c = @compid and gsb01c = 'AJ' insert #idd_info(type, prj_type, typename) select 1, gak01c, gak02c from gam10 where gak00c = @compid insert #idd_info(type, prj_type, typename) select 3, gsb02c, gsb03c from gsm02 where gsb00c = @compid and gsb01c = 'AL' insert #idd_info(type, prj_type, typename) values(4, '4', '欠款') insert #idd_info(type, prj_type, typename) values(5, '5', '还款') declare @tcol int-- 添加列名 select @tcol = count(*) from #idd_info select isnull(idd,'') idd,isnull(type,'') type,isnull(prj_type,'') prj_type,isnull(typename,'') typename from #idd_info order by idd drop table #collect_detail drop table #group_data drop table #idd_info end GO /****** Object: StoredProcedure [dbo].[upg_ham01_costamt] Script Date: 06/12/2017 06:20:12 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_ham01_costamt] ( @compid varchar(10), @amt float, --目标业绩 @amt2 float --目标消耗 ) as begin -- 001店长:计算公式=总业绩除以人数除以2 -- 002店助:计算公式=(总业绩-店长业绩)/(人数-1) -- 003员工:计算公式=(总业绩-店长业绩)/(人数-1) -- 005二期员工:计算公式={(总业绩-店长业绩)/(人数-1)}*0.3 -- 006三期员工:计算公式={(总业绩-店长业绩)/(人数-1)}*0.6 declare @Rowcount int select @Rowcount = count(1) from ham01 where haa00c = @compid and haa27c = 2 if(@Rowcount = 0) return; declare @amt_sup float --店长业绩 declare @amt_ass float --店助业绩 declare @amt2_sup float --店长消耗 declare @amt2_ass float --店助消耗 --店长业绩 set @amt_sup = @amt/@Rowcount/2 --店长消耗 set @amt2_sup = @amt2/@Rowcount/2 --人数-1 set @Rowcount = @Rowcount - 1 --店助业绩 set @amt_ass = (@amt - @amt_sup) / @Rowcount --店助消耗 set @amt2_ass = (@amt2 - @amt2_sup) / @Rowcount select haa00c as gbw00c, haa01c as gbw01c, haa02c, haa25c as gbw03c, case haa25c when '001' then @amt_sup when '002' then @amt_ass when '003' then @amt_ass when '004' then 0 when '005' then @amt_ass * 0.3 when '006' then @amt_ass * 0.6 end gbw04f, case haa25c when '001' then @amt2_sup when '002' then @amt2_ass when '003' then @amt2_ass when '004' then 0 when '005' then @amt2_ass * 0.3 when '006' then @amt2_ass * 0.6 end gbw05f from ham01 where haa00c = @compid and haa27c = 2 --目前状况(1-未到职 ,2-到职 ,3 -离职) and isnull(haa25c,'') <> '' end GO /****** Object: StoredProcedure [dbo].[upg_insert_staff] Script Date: 06/12/2017 06:20:16 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_insert_staff] ( @compid varchar(10), @prefix varchar(20), --编号前缀 @suffix varchar(20), --编号后缀 @begin int, --开始编号 @end int, --结束编号 @position varchar(20), @nlen int ) as --Writed by WUXM 2006/02/13 begin declare @staff varchar(20) declare @i int declare @strpre varchar(20) select @i = 0 select @strpre = '0' while(@i < @nlen) begin select @strpre = @strpre + '0' select @i = @i + 1 end while(@begin <= @end) begin select @staff = @prefix + right(@strpre + convert(varchar(20), @begin), @nlen) + @suffix insert ham01(haa00c, haa01c, haa02c, haa25c, haa27c, ownhaa00c, ownhaa01c) values (@compid, @staff, @staff, @position, '2', @compid, @staff) select @begin = @begin + 1 end end GO /****** Object: StoredProcedure [dbo].[sp_insertInfo] Script Date: 06/12/2017 06:19:21 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[sp_insertInfo] @title varchar(50), @content varchar(1000), @author varchar(20), @publishTime varchar(20), @importance int, @infoType int, @status int, @pubRange varchar(20), @createTime varchar(20), @authorCompID varchar(20) as begin insert into wip(wip01v, wip02v, wip03v, wip04v, wip05i, wip06i, wip07i, wip08v, wip09v, wip10v) values(@title, @content, @author, @publishTime, @importance, @infoType, @status, @pubRange, @createTime, @authorCompID) end GO /****** Object: StoredProcedure [dbo].[sp_deleteAllMsg] Script Date: 06/12/2017 06:19:21 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[sp_deleteAllMsg] as begin delete wmc end GO /****** Object: StoredProcedure [dbo].[upg_recal_proc_pretimes] Script Date: 06/12/2017 06:20:31 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_recal_proc_pretimes] ( @cardid varchar(20), @prjid varchar(20), @procid int ) as begin declare @buytimes float declare @sendtimes float declare @contimes float declare @billtype varchar(20) declare @openidd int declare @pretimes float select @pretimes = 0 declare cur_each_gcm08 cursor for select gch05f, gch06f, gch08f, gch10c from gcm08 where gch03c = @cardid and gch04c = @prjid and gch16i = @procid order by gch02d, gch01i for update of gch23f open cur_each_gcm08 fetch cur_each_gcm08 into @buytimes, @sendtimes, @contimes, @billtype while @@fetch_status = 0 begin select @pretimes = isnull(@pretimes, 0) + isnull(@buytimes, 0) + isnull(@sendtimes, 0) - isnull(@contimes, 0) update gcm08 set gch23f = @pretimes where current of cur_each_gcm08 fetch cur_each_gcm08 into @buytimes, @sendtimes, @contimes, @billtype end close cur_each_gcm08 deallocate cur_each_gcm08 end GO /****** Object: StoredProcedure [dbo].[sp_saveMail] Script Date: 06/12/2017 06:19:21 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[sp_saveMail] @subject varchar(50), @content varchar(5000), @receiver varchar(200), @sender varchar(20), @createTime varchar(20), @senderCompID varchar(20), @receiverCompID varchar(20) as begin declare @count int select @count = count(*) from wem insert into wem(wem00, wem01v, wem02v, wem03v, wem04v, wem06v, wem07i, wem10v, wem11v) values(@count, @subject, @content, @receiver, @sender, @createTime, 2, @receiverCompID, @senderCompID) --保存到写信人的草稿箱 insert into wmb(wmb01v, wmb02i, wmb03i, wmb05v) values(@sender, @count, 3, @senderCompID) end GO /****** Object: StoredProcedure [dbo].[sp_sendMail] Script Date: 06/12/2017 06:19:21 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[sp_sendMail] @subject varchar(50), @content varchar(5000), @receiver varchar(200), @sender varchar(20), @sendTime varchar(20), @createTime varchar(20), @fjFileName varchar(50), @receiverCompID varchar(20), @senderCompID varchar(20) as begin declare @count int select @count = count(*) from wem insert into wem(wem00, wem01v, wem02v, wem03v, wem04v, wem05v, wem06v, wem07i, wem08v, wem10v, wem11v) values(@count, @subject, @content, @receiver, @sender, @sendTime, @createTime, 1, @fjFileName, @receiverCompID, @senderCompID) --保存到发件人的发件箱 insert into wmb(wmb01v, wmb02i, wmb03i, wmb05v) values(@sender, @count, 2, @senderCompID) --保存到收件人的收件箱 insert into wmb(wmb01v, wmb02i, wmb03i, wmb04i, wmb05v) values(@receiver, @count, 1, 0, @receiverCompID) select @count as count end GO /****** Object: StoredProcedure [dbo].[sp_deleteMail] Script Date: 06/12/2017 06:19:21 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[sp_deleteMail] @mailType int, @mailNo int, @userEmpID varchar(20), @userCompID varchar(20) as begin if (@mailType != 4) update wmb set wmb03i = 4 where wmb03i = @mailType and wmb02i = @mailNo and wmb01v = @userEmpID and wmb05v = @userCompID else delete wmb where wmb03i = @mailType and wmb02i = @mailNo and wmb01v = @userEmpID and wmb05v = @userCompID end GO /****** Object: StoredProcedure [dbo].[upg_get_fillbill_comm] Script Date: 06/12/2017 06:20:07 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_get_fillbill_comm] ( @compid varchar(10), -- 公司编号 @billtype varchar(5), -- 单据类别 'gn' = 会员卡销售 'gz' = 会员帐户管理 @billid varchar(20), -- 单号 @personid varchar(20), -- 员工编号 @paycode varchar(20), -- 支付代码 @trueperf float output, -- 实业绩, 总是等于虚业绩 @comm float output -- 提成 ) as begin create table #card_fill_detail_by_payment ( compid varchar(10) not null, -- 公司别 billtype varchar(10) not null, -- 单别 billid varchar(20) not null, -- 单号 personid varchar(20) null, -- 员工号 paycode varchar(20) null, -- 支付代码 comm float null, -- 提成 perf float null, -- 业绩 code varchar(20) null -- 代码 会员卡代码 ,疗程代码,套餐代码 ) if( @billtype = 'gz' ) begin if(exists(select 1 from gcm11 a where a.gcm00c = @compid and a.gcm30c = @billid)) begin insert #card_fill_detail_by_payment(compid,billtype,billid,personid,paycode,comm,perf) select distinct compid,billtype,billid,personid,paycode,comm,perf from card_fill_detail_by_payment a with (nolock), gam26 z with (nolock) where a.compid = z.gaz02c and z.gaz01c = @compid and billtype = @billtype and billid = @billid and personid = @personid and paycode = @paycode end else begin insert #card_fill_detail_by_payment(compid,billtype,billid,personid,paycode,comm,perf,code) select distinct compid,billtype,billid,personid,paycode,comm,perf,code from card_fill_detail_by_payment a with (nolock), gam26 z with (nolock) where a.compid = z.gaz02c and z.gaz01c = @compid and billtype = @billtype and billid = @billid and personid = @personid and paycode = @paycode end end else begin insert #card_fill_detail_by_payment(compid,billtype,billid,personid,paycode,comm,perf,code) select distinct compid,billtype,billid,personid,paycode,comm,perf,code from card_fill_detail_by_payment a with (nolock), gam26 z with (nolock) where a.compid = z.gaz02c and z.gaz01c = @compid and billtype = @billtype and billid = @billid and personid = @personid and paycode = @paycode end --统计金额 select @comm = sum(comm), @trueperf = sum(perf) from #card_fill_detail_by_payment drop table #card_fill_detail_by_payment end GO /****** Object: StoredProcedure [dbo].[upg_stat_glm07] Script Date: 06/12/2017 06:20:44 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_stat_glm07]( @compid varchar(10), @curdate varchar(10), @close_num float output, --当天已经结帐人数 @close_amt float output, --当天已经结帐金额 @open_num float output, --当天未结帐人数 @open_amt float output --当天未结帐金额 ) as begin select @close_num = count(*), @close_amt = sum(glg07f) from glm07 where glg10d = @curdate and glg00c = @compid and isnull(glg12i, 0) = 1 select @open_num = count(*), @open_amt = sum(glg07f) from glm07 where glg10d = @curdate and glg00c = @compid and isnull(glg12i, 0) = 0 end GO /****** Object: StoredProcedure [dbo].[upg_random_duty] Script Date: 06/12/2017 06:20:31 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_random_duty] ( @compid varchar(10), @date varchar(8), @duty varchar(20), @type int ) as begin create table #gicm13 ( gick00c varchar(10) not null, -- 本院编号 gick03c varchar(20) null, -- 班次 gick04c varchar(20) null, -- 员工编号 gick05c varchar(20) null, -- 员工姓名 gick06i int null, -- 性别 0-女 1-男 gick07d varchar(8) null, -- 日期 gick08i int null, -- 轮排顺序 gick09f float null, -- 轮排次数 ) set @type = isnull(@type, '') insert #gicm13(gick00c, gick03c, gick04c, gick05c, gick06i, gick07d, gick08i, gick09f) select gick00c, gick03c, gick04c, gick05c, gick06i, gick07d, gick08i, 0 from gicm13 where gick00c = @compid and gick07d = @date and gick03c = @duty declare @randomcount float declare @emplyeeno varchar(20) declare cursor_01 cursor for select gick04c from #gicm13 open cursor_01 fetch cursor_01 into @emplyeeno while @@fetch_status = 0 begin select @randomcount = count(*) from glm07 where glg00c = @compid and glg10d = @date and isnull(glg12i, 0) = 1 and glg15c = 2 and glg11c = @emplyeeno update #gicm13 set gick09f = @randomcount where gick04c = @emplyeeno fetch cursor_01 into @emplyeeno end close cursor_01 deallocate cursor_01 if (@type <> 1) begin select emplyeeno = gick04c, emplyeename = gick05c, gender = gick06i, randomsort = gick08i, randomcount = gick09f from #gicm13 order by gick08i end else begin select emplyeeno = gick04c, emplyeename = gick05c, gender = gick06i, randomsort = gick08i, randomcount = gick09f from #gicm13 order by gick09f end end GO /****** Object: StoredProcedure [dbo].[upg_rename_card_CardChange] Script Date: 06/12/2017 06:20:35 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_rename_card_CardChange] ( @gecompid varchar(10), --补卡门店 @bill varchar(30), @oldcardid varchar(20), @newcardid varchar(20), @userid varchar(20) ) as begin set nocount on begin transaction begin try if(@gecompid = '' or @oldcardid = '' or @newcardid = '') return declare @compid varchar(20) declare @curdate varchar(20), @curtime varchar(20) exec upg_getcurrdate @curdate output, @curtime output -- 老卡 select @compid = gca00c from gcm01 with (nolock) where gca01c = @oldcardid and gca00c = gca13d --会员卡基本资料主档 update gcm01 set gca08i = 9 from gcm01 where gca01c = @oldcardid and gca00c = gca13d -- 更新新卡的会员信息 declare @gca05d varchar(10) declare @gca06d varchar(10) declare @gca07d varchar(10) declare @gca12c varchar(30) declare @gca18c varchar(30) declare @gca27c varchar(1000) select @gca05d = gca05d,@gca06d = gca06d,@gca07d=gca07d,@gca12c=gca12c,@gca18c=gca18c,@gca27c=gca27c from gcm01 with (nolock) where gca00c = gca13d and gca01c = @oldcardid update gcm01 set gca05d = @gca05d, gca06d = @gca06d, gca07d = @gca07d, gca12c = @gca12c, --消费密码 gca18c = @gca18c, --查询密码 gca04c = @newcardid, gca08i = 4, gca27c = @gca27c, gca19f = (select gea20f from gem01 where gea00c = @gecompid and gea01c = @bill ) from gcm01 where gca01c = @newcardid and gca00c = gca13d -- 会员基本资料主档 sp_help gbm01 delete gbm01 where (gba01c = @newcardid or gba23c = @newcardid) if not exists(select 1 from gbm01 where gba01c = @newcardid) begin update gbm01 set gba01c = @newcardid, gba23c = @newcardid where gba23c = @oldcardid -- 更新会员照片 delete gbm04 where gbd01c = @newcardid update gbm04 set gbd00c = @gecompid,gbd01c = @newcardid where gbd00c = @gecompid and gbd01c = @oldcardid end delete gcm02 where gcb01c = @newcardid update gcm02 set gcb01c = @newcardid where gcb01c = @oldcardid -- exec upg_log_copy_gcm0306 @compid, @oldcardid, @userid -- 会员卡账户 delete gcm03 where gcc01c = @newcardid update gcm03 set gcc01c = @newcardid,gcc00c = @gecompid where gcc01c = @oldcardid delete gcm06 where gcf01c = @newcardid update gcm06 set gcf01c = @newcardid,gcf00c = @gecompid where gcf01c = @oldcardid delete gtc04 where gte01c = @newcardid update gtc04 set gte01c = @newcardid where gte01c = @oldcardid delete gtc08 where gtj01c = @newcardid update gtc08 set gtj01c = @newcardid where gtj01c = @oldcardid delete gcm08 where gch03c = @newcardid update gcm08 set gch03c = @newcardid where gch03c = @oldcardid delete gtc07 where gti03c = @newcardid update gtc07 set gti03c = @newcardid where gti03c = @oldcardid -- 记录操作历史 insert sysoperationlog(userid, program, operation, operationdate, operationtime, origatedate, compid, keyvalue1, keyvalue2, keyvalue3) values(@userid, 'GMB07I', 'A', @curdate, @curtime, @curdate, @gecompid, @oldcardid, @newcardid, 'upg_rename_card_CardChange') -- exec upg_log_copy_gcm0306 @compid, @newcardid, @userid delete gbm15 where gbl01c = @newcardid update gbm15 set gbl00c = @gecompid,gbl01c = @newcardid where gbl01c = @oldcardid delete gbm17 where gbw01c = @newcardid update gbm17 set gbw01c = @newcardid where gbw01c = @oldcardid delete gcm12 where gcn01c = @newcardid update gcm12 set gcn00c = @gecompid,gcn01c = @newcardid where gcn01c = @oldcardid delete gcm16 where gcr01c = @newcardid update gcm16 set gcr00c = @gecompid,gcr01c = @newcardid where gcr01c = @oldcardid declare @gea08c varchar(20) declare @gea14i int select @gea08c = gea08c, @gea14i = gea14i from gem01 with (nolock) where gea00c = @gecompid and gea01c = @bill and gea02i = 7 -- 记录操作历史 insert sysoperationlog(userid, program, operation, operationdate, operationtime, origatedate, compid, keyvalue1, keyvalue2, keyvalue3) values(@userid, 'GMB07I', 'A', @curdate, @curtime, @curdate, @gecompid, @oldcardid, @newcardid, 'upg_rename_card_CardChange:1') --是否转移会员卡的历史 if(@gea14i = 1 ) exec upg_trans_history @oldcardid, @newcardid declare @flowno float select @flowno = isnull(max(gcb02f), 0) + 1 from gcm02 with (nolock) where gcb00c = @gecompid and gcb01c = @newcardid insert into gcm04(gcd00c,gcd01c,gcd02i,gcd04f,gcd05i,gcd06f,gcd07c,gcd08c,gcd09d,gcd10f,gcd11c,gcd12i,gcd13t) select @gecompid,gcc01c,2, (select isnull(max(gcd04f),0) + 1 from gcm04 with (nolock) where gcd00c = @gecompid and gcd01c = gcc01c and gcd02i = 2 ), 0,gcc06f,'ge',@bill,@curdate,0,@curdate,1,@curtime from gcm03 with (nolock) where gcc03i = 2 and gcc01c = @newcardid insert into gcm02(gcb00c, gcb01c, gcb02f, gcb03i, gcb04c, gcb05i, gcb06i, gcb07d) select gea00c, gea07c, @flowno, gea02i, gea01c, 1, 4, gea03d from gem01 with (nolock) where gea00c = @gecompid and gea01c = @bill --转移会员卡抵用券 delete gam08 where gah10c = @newcardid update gam08 set gah00c = @gecompid,gah10c = @newcardid where gah10c = @oldcardid and gah06i = 0 update gam08 set gah00c = @gecompid,gah10c = @newcardid,gah16c = @newcardid where gah10c = @oldcardid and gah06i = 1 end try begin catch if @@TRANCOUNT > 0 begin declare @SP_Parameters varchar(max) set @SP_Parameters = '@gecompid = '+ isnull(@gecompid,'') + '@bill = '+ isnull(@bill,'') + '@oldcardid = ' + isnull(@oldcardid, '') + '@newcardid = ' + isnull(@newcardid, '') + '@userid = ' + isnull(@userid, '') insert into SP_TranLog (SPName,SPTime,SPParameter) values ('upg_rename_card_CardChange',getdate(),@SP_Parameters) raiserror ('transaction is failed.', 16, 1) WITH NOWAIT rollback transaction end end catch if @@TRANCOUNT > 0 begin commit transaction end end GO /****** Object: StoredProcedure [dbo].[upg_rename_card] Script Date: 06/12/2017 06:20:35 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_rename_card] ( @oldcardid varchar(20), @newcardid varchar(20), @userid varchar(20), @curdate varchar(10), @curtime varchar(10) ) as begin begin transaction begin try declare @new_state int declare @old_state int declare @have_old_card int declare @have_new_card int declare @compid varchar(20) -- 老卡的判断,是否存在,以及状态 select @have_old_card = 1, @old_state = gca08i, @compid = gca00c from gcm01 with (nolock) where gca01c = @oldcardid and gca00c = gca13d select @have_old_card = isnull(@have_old_card, 0), @old_state = isnull(@old_state, 1) -- 判断新卡的是否存在,以及新卡的状态 select @have_new_card = 1, @new_state = gca08i from gcm01 with (nolock) where gca00c = gca13d and gca01c = @newcardid select @have_new_card = isnull(@have_new_card, 0), @new_state = isnull(@new_state, 1) --exec upg_log_copy_gcm0306 @compid, @oldcardid, @userid --exec upg_log_copy_gcm0306 @compid, @newcardid, @userid if (@have_old_card = 0) return if (@old_state = 9) return if (@new_state = 1 and @have_new_card = 1) begin delete gcm01 where gca08i = 1 and gca01c = @newcardid end if exists(select 1 from gcm01 with (nolock) where gca01c = @newcardid) begin return end --会员卡基本资料主档 update gcm01 set gca01c = @newcardid, gca04c = @newcardid where gca01c = @oldcardid --update gcm01 set gca01c = @newcardid where gca01c = @oldcardid delete gcm02 where gcb01c = @newcardid update gcm02 set gcb01c = @newcardid where gcb01c = @oldcardid delete gcm03 where gcc01c = @newcardid update gcm03 set gcc01c = @newcardid where gcc01c = @oldcardid -- 会员基本资料主档 if not exists(select 1 from gbm01 where gba01c = @newcardid) begin update gbm01 set gba01c = @newcardid, gba23c = @newcardid where gba23c = @oldcardid --gba01c = @oldcardid end --套餐 exec upg_trans_history @oldcardid, @newcardid delete gcm06 where gcf01c = @newcardid update gcm06 set gcf01c = @newcardid where gcf01c = @oldcardid delete gtc04 where gte01c = @newcardid update gtc04 set gte01c = @newcardid where gte01c = @oldcardid delete gtc08 where gtj01c = @newcardid update gtc08 set gtj01c = @newcardid where gtj01c = @oldcardid delete gcm08 where gch03c = @newcardid update gcm08 set gch03c = @newcardid where gch03c = @oldcardid delete gtc07 where gti03c = @newcardid update gtc07 set gti03c = @newcardid where gti03c = @oldcardid --update gbm01 set gba23c = @newcardid where gba23c = @oldcardid delete gbm15 where gbl01c = @newcardid update gbm15 set gbl01c = @newcardid where gbl01c = @oldcardid delete gbm17 where gbw01c = @newcardid update gbm17 set gbw01c = @newcardid where gbw01c = @oldcardid delete gcm12 where gcn01c = @newcardid update gcm12 set gcn01c = @newcardid where gcn01c = @oldcardid delete gcm16 where gcr01c = @newcardid update gcm16 set gcr01c = @newcardid where gcr01c = @oldcardid --抵用券 delete gam08 where gah10c = @newcardid update gam08 set gah10c = @newcardid where gah10c = @oldcardid declare @flowno float select @flowno = isnull(max(gcb02f), 0) + 1 from gcm02 with (nolock) where gcb01c = @newcardid insert gcm02(gcb00c, gcb01c, gcb02f, gcb03i, gcb07d, gcb08c) values('001', @newcardid, @flowno, 12, @curdate, @oldcardid) insert sysoperationlog(userid, program, operation, operationdate, operationtime, origatedate, compid, keyvalue1, keyvalue2, keyvalue3) values(@userid, 'GSB01I', 'M', @curdate, @curtime, @curdate, '001', @oldcardid, @newcardid, 'upg_rename_card') end try begin catch if @@TRANCOUNT > 0 begin declare @SP_Parameters varchar(max) set @SP_Parameters = '@oldcardid = ' + isnull(@oldcardid, '') + '@newcardid = ' + isnull(@newcardid, '') + '@userid = ' + isnull(@userid, '') + '@curdate = ' + isnull(@curdate, '') + '@curtime = ' + isnull(@curtime, '') insert into SP_TranLog (SPName,SPTime,SPParameter) values ('upg_rename_card',getdate(),@SP_Parameters) raiserror ('transaction is failed.', 16, 1) WITH NOWAIT rollback transaction end end catch if @@TRANCOUNT > 0 begin commit transaction end end GO /****** Object: StoredProcedure [dbo].[upg_rename_card_only] Script Date: 06/12/2017 06:20:35 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_rename_card_only] ( @oldcardid varchar(20), @newcardid varchar(20), @userid varchar(20), @curdate varchar(10), @curtime varchar(10) ) as begin declare @new_state int declare @old_state int declare @have_old_card int declare @have_new_card int declare @compid varchar(20) -- 老卡的判断,是否存在,以及状态 select @have_old_card = 1, @old_state = gca08i, @compid = gca00c from gcm01 with (nolock) where gca01c = @oldcardid and gca00c = gca13d select @have_old_card = isnull(@have_old_card, 0), @old_state = isnull(@old_state, 1) -- 判断新卡的是否存在,以及新卡的状态 select @have_new_card = 1, @new_state = gca08i from gcm01 with (nolock) where gca00c = gca13d and gca01c = @newcardid select @have_new_card = isnull(@have_new_card, 0), @new_state = isnull(@new_state, 1) --exec upg_log_copy_gcm0306 @compid, @oldcardid, @userid --exec upg_log_copy_gcm0306 @compid, @newcardid, @userid if (@have_old_card = 0) return if (@old_state = 9) return if ( @new_state = 1 and @have_new_card = 1) begin delete gcm01 where gca08i = 1 and gca01c = @newcardid end if exists(select 1 from gcm01 with (nolock) where gca01c = @newcardid) begin return end update gcm01 set gca01c = @newcardid where gca01c = @oldcardid update gcm02 set gcb01c = @newcardid where gcb01c = @oldcardid delete gcm03 where gcc01c = @newcardid update gcm03 set gcc01c = @newcardid where gcc01c = @oldcardid delete gcm06 where gcf01c = @newcardid update gcm06 set gcf01c = @newcardid where gcf01c = @oldcardid delete gtc04 where gte01c = @newcardid update gtc04 set gte01c = @newcardid where gte01c = @oldcardid delete gtc08 where gtj01c = @newcardid update gtc08 set gtj01c = @newcardid where gtj01c = @oldcardid update gcm08 set gch03c = @newcardid where gch03c = @oldcardid update gbm01 set gba23c = @newcardid where gba23c = @oldcardid update gcm12 set gcn01c = @newcardid where gcn01c = @oldcardid update gcm16 set gcr01c = @newcardid where gcr01c = @oldcardid -- 抵用券 update gam08 set gah10c = @newcardid where gah10c = @oldcardid --套餐 update gtc07 set gti03c = @newcardid where gti03c = @oldcardid exec upg_trans_history @oldcardid, @newcardid declare @flowno float select @flowno = isnull(max(gcb02f), 0) + 1 from gcm02 with (nolock) where gcb01c = @newcardid insert gcm02(gcb00c, gcb01c, gcb02f, gcb03i, gcb07d, gcb08c) values('001', @newcardid, @flowno, 12, @curdate, @oldcardid) insert sysoperationlog(userid, program, operation, operationdate, operationtime, origatedate, compid, keyvalue1, keyvalue2, keyvalue3) values(@userid, 'GSB01I', 'M', @curdate, @curtime, @curdate, '001', @oldcardid, @newcardid, 'upg_rename_card_only') end GO /****** Object: StoredProcedure [dbo].[upg_dispatch_data_proc_set_single] Script Date: 06/12/2017 06:20:01 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_dispatch_data_proc_set_single] ( @compid varchar(10), @projid varchar(20) -- 项目编号 ) as begin create table #tbl_control ( compid varchar(10) not null, -- 儿子公司的代码 lvl int null, primary key(compid) ) insert #tbl_control(compid, lvl) select bran_id, control_lvl from b_perf a, basic_data_control b where parent_id = @compid and a.bran_id = b.compid and b.datatype = 'proc_set' and b.control_lvl = 1 -- 创建循环变量 declare @HasRecord int set @HasRecord = 1 declare @currentCompId varchar(10) declare @lvl int while (@HasRecord = 1) begin if exists (select 1 from #tbl_control) begin set @HasRecord = 1 select top 1 @currentCompId = compid, @lvl = lvl from #tbl_control if @lvl = 1 -- 严格按总部的 begin -- 先删除自己的,把总部分发下来 -- 强制模式都是先删除原来的 exec upg_m_dispatch_proc_set_single @compid, @currentCompId, 1, @projid insert #tbl_control(compid, lvl) select bran_id, control_lvl from b_perf a, basic_data_control b where parent_id = @currentCompId and a.bran_id = b.compid and b.datatype = 'proc_set' and b.control_lvl = 1 end else if @lvl = 2 begin exec upg_m_dispatch_proc_set_single @compid, @currentCompId, 0, @projid insert #tbl_control(compid, lvl) select bran_id, case when (control_lvl <= 2) then 2 else control_lvl end from b_perf a, basic_data_control b where parent_id = @currentCompId and a.bran_id = b.compid and b.datatype = 'proc_set' and b.control_lvl = 1 end -- 清除已处理的门店 delete #tbl_control where compid = @currentCompId end else begin set @HasRecord = 0 end end drop table #tbl_control end GO /****** Object: StoredProcedure [dbo].[upg_insert_gym_commdata] Script Date: 06/12/2017 06:20:15 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_insert_gym_commdata] ( @compid varchar(10) --公司编号 ) as--WUXM, 2004/11/14 begin --将系统原始资料插入临时表中, --以系统已经有的资料为准 create table #gsm02 ( gsb00c varchar(10) not null, --公司编号 gsb01c varchar(10) not null, -- 资料类别分类码(1-, 2-消费方式, gsb02c varchar(5) not null, -- 类别字段代码值 gsb03c varchar(40) null, -- 类别字段显示Text gsb04c varchar(40) not null, -- 资料类别分类码描述 gsb05i int null, -- 维护标志(0- user 不可见, 1- user可见但不可维护, 2- user可维护) ) declare @PK_Sql nvarchar(max) set @PK_Sql = ' alter table #gsm02 add constraint PK_#gsm02_' + convert(varchar(10), cast(floor(rand() * 1000000) as int)) + ' primary key clustered(gsb00c, gsb01c, gsb02c)' exec sp_executesql @PK_Sql insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, '3', '1', '现金', '收费方式', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, '3', '2', '电子钱包', '收费方式', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, '3', '3', '抵用券', '收费方式', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, '3', '4', '储值账户', '收费方式', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, '3', '5', '签单挂帐', '收费方式', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, '3', '6', '银行卡支付', '收费方式', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, '3', '7', '积分支付', '收费方式', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, '3', '8', '经理签单', '收费方式', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, '3', '9', '疗程账户', '收费方式', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, '3', 'A', '储值账户三', '收费方式', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, '3', '0', '内调', '收费方式', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, '3', '$', '抵用券B', '收费方式', 2) ---added by cyr 20120919 begin insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, '3', '$1', '备用支付', '收费方式', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, '3', '$2', '备用支付', '收费方式', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, '3', '$3', '备用支付', '收费方式', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, '3', '$4', '备用支付', '收费方式', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, '3', '$5', '备用支付', '收费方式', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, '3', 'ZFB', '支付宝支付', '收费方式', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, '3', 'WX', '微信支付', '收费方式', 2) ---added by cyr 20120919 end insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, '3', '#', '套餐支付', '收费方式', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, '3', '&', '保证金转付', '收费方式', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, '4', '1', '所有时间', '折让选项', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, '4', '2', '节假日除外', '折让选项', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, '5', '1', '定额', '业绩指标', 1) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, '5', '2', '申报额', '业绩指标', 1) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, '5', '3', '业绩指标', '业绩指标', 1) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, '6', '1', '额头', '测试类别', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, '6', '2', '鼻子', '测试类别', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, '6', '3', '脸颊', '测试类别', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, '6', '4', '眼周', '测试类别', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, '6', '5', '腹部', '测试类别', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, '6', '6', '发质', '测试类别', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, '6', '7', '发型', '测试类别', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, '8', '1', '一般', '身体状况', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, '8', '2', '良好', '身体状况', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, '8', '3', '非常好', '身体状况', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'B', '1', '点工/A客', '服务类别', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'B', '2', '轮班/B客', '服务类别', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'B', '3', '补工/返工', '服务类别', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'B', '14', '内创', '服务类别', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'B', '15', '外创', '服务类别', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'D', '1', '促销赠券', '赠券类别', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'D', '2', '节日赠券', '赠券类别', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'E', '1', '非常喜欢', '项目兴趣程度', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'E', '2', '一般兴趣', '项目兴趣程度', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'E', '3', '无兴趣', '项目兴趣程度', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'E', '4', '非常厌烦', '项目兴趣程度', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'E', 'A', '其他', '项目兴趣程度', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'F', '1', '美容用品', '产品大类', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'F', '2', '美发用品', '产品大类', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'F', '3', '沐足用品', '产品大类', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'F', '4', '养生用品', '产品大类', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'G', '1', '针灸', '专长技能', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'G', '2', '按摩', '专长技能', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'G', '3', '推拿', '专长技能', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'H', '1', '工程师', '职业类别', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'H', '2', '律师', '职业类别', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'H', '3', '医生', '职业类别', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'H', '4', '教师', '职业类别', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'H', '5', '其他', '职业类别', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'H', '6', '企业经理', '职业类别', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'H', '7', '行政官员', '职业类别', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'H', '8', '私营企业主', '职业类别', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'I', '1', '身份证', '证件类别', 1) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'I', '2', '学生证', '证件类别', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'I', '3', '军官证', '证件类别', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'I', '4', '工作证', '证件类别', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'I', '5', '其他', '证件类别', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'I', '6', '护照', '证件类别', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'J', '1', '系统管理员', '使用者角色', 1) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'J', '10', '财务人员', '使用者角色', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'J', '2', '经营管理者', '使用者角色', 1) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'J', '8', '收银人员', '使用者角色', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'J', '9', '服务人员', '使用者角色', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'K', 'BOT', '瓶', '产品单位', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'K', 'BOX', '盒', '产品单位', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'K', 'F', '份', '产品单位', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'K', 'S', '双', '产品单位', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'K', 'g', '克', '产品单位', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'K', 'kg', '千克', '产品单位', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'K', 'ml', '毫升', '产品单位', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'K', 'l', '升', '产品单位', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'K', 'T', '套', '产品单位', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i)--add by lyj for mos-87 values(@compid, 'K2', '0', '寄存/欠货', '寄存', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i)--add by lyj for mos-87 values(@compid, 'K2', '1', '全部领取', '寄存', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i)--add by lyj for mos-87 values(@compid, 'K2', '2', '部分领取', '寄存', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i)--add by lyj for mos-87 values(@compid, 'K2', '3', '已无产品', '寄存', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'L', '6', '美容类', '项目类别', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'L', '7', '按摩类', '项目类别', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'L', '8', '足疗类', '项目类别', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'L', '9', '美发类', '项目类别', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'M', '1', '免费', '消费性质(对象)', 1) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'M', '2', '收费', '消费性质(对象)', 1) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'N', '0', '煤气费', '固定费用类别', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'N', '1', '手续费', '固定费用类别', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'N', '2', '水费', '固定费用类别', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'N', '3', '电费', '固定费用类别', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'N', '4', '房租', '固定费用类别', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'N', '5', '广告费', '固定费用类别', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'N', '6', '退款', '固定费用类别', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'N', '7', '错帐抵冲', '固定费用类别', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'N', '8', '餐费', '固定费用类别', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'N', '9', '交通费', '固定费用类别', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'N', 'A', '银行存款(当日)', '固定费用类别', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'N', 'B', '银行存款(前日余款)', '固定费用类别', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'N', 'C', '员工借支', '固定费用类别', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'O', '1', '电子钱包', '账户类别', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'O', '2', '储值账户', '账户类别', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'O', '3', '积分', '账户类别', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'O', '4', '疗程账户', '账户类别', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'O', '5', '储值账户三', '账户类别', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'O', '999', '现金', '账户类别', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'O', '888', '服务产品欠款', '账户类别', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'O', '889', '套餐欠款', '账户类别', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'P', '0', '开帐', '会员卡变动类别', 1) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'P', '2', '会员开卡', '会员卡变动类别', 1) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'P', '3', '会籍延期', '会员卡变动类别', 1) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'P', '4', '卡种变更', '会员卡变动类别', 1) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'P', '5', '会籍保留', '会员卡变动类别', 1) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'P', '6', '会籍转让', '会员卡变动类别', 1) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'P', '7', '遗失补卡', '会员卡变动类别', 1) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'P', '8', '会员退卡', '会员卡变动类别', 1) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'P', '9', '批量延期', '会员卡变动类别', 1) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'P', '99', '会员卡销售', '会员卡变动类别', 1) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'P', '10', '挂失解挂', '会员卡变动类别', 1) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'P', '11', '会员升级', '会员卡变动类别', 1) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'P', '12', '更改卡号', '会员卡变动类别', 1) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'Q', '1', '博士后', '学历', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'Q', '2', '博士', '学历', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'Q', '3', '硕士', '学历', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'Q', '4', '学士', '学历', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'Q', '5', '大专', '学历', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'Q', '6', '中专', '学历', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'Q', '7', '高中', '学历', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'Q', '8', '初中', '学历', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'Q', '9', '小学', '学历', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'R', '001', '美容师', '工作岗位', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'R', '002', '美发师', '工作岗位', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'R', '003', '收银人员', '工作岗位', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'R', '004', '按摩师', '工作岗位', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'R', '005', '足疗师', '工作岗位', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'R', '006', '美容经理', '工作岗位', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'R', '007', '美发经理', '工作岗位', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'R', '008', '店长', '工作岗位', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'R', '009', '管理人员', '工作岗位', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'S', '001', '学徒', '职称资料', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'S', '002', '初级美容师', '职称资料', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'S', '003', '助理', '职称资料', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'S', '004', '美容师', '职称资料', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'S', '005', '初级美发师', '职称资料', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'S', '006', '美发师', '职称资料', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'S', '007', '首席', '职称资料', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'S', '008', '总监', '职称资料', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'S', '009', '店长', '职称资料', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'S', '010', '经理', '职称资料', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'S', '011', '按摩师', '职称资料', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'S', '012', '美体师', '职称资料', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'S', '013', '顾问', '职称资料', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'T', '001', '合同聘用', '聘雇性质', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'T', '002', '正式职工', '聘雇性质', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'T', '003', '临时工', '聘雇性质', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'U', '001', '销售部', '部门资料', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'U', '002', '管理部', '部门资料', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'U', '003', '美容组', '部门资料', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'U', '004', '美发组', '部门资料', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'U', '005', '按摩组', '部门资料', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'U', '006', '足疗组', '部门资料', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'V', '1', '固定费用', '会员卡变动费用类别', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'V', '2', '以期限收费', '会员卡变动费用类别', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'V', '3', '参照会员卡单价', '会员卡变动费用类别', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'W', '1', '会员', '客户类别', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'W', '2', '准会员', '客户类别', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'W', '3', '非会员', '客户类别', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'X', '1', '年', '期限单位', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'X', '2', '月', '期限单位', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'X', '3', '日', '期限单位', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'X', '4', '季度', '期限单位', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'Y', '001', '欧莱雅', '供货商资料', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'Y', '002', '威娜', '供货商资料', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'Y', '003', '黑人头', '供货商资料', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'Y', '004', '乔微尔', '供货商资料', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'Y', '005', '肯达是', '供货商资料', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'Z', '1', '未销售', '会员卡状态', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'Z', '2', '已销售', '会员卡状态', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'Z', '3', '已注册', '会员卡状态', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'Z', '4', '已开卡', '会员卡状态', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'Z', '5', '正常使用中', '会员卡状态', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'Z', '6', '已转卡', '会员卡状态', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'Z', '7', '已退卡', '会员卡状态', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'Z', '8', '已过期', '会员卡状态', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'Z', '9', '已挂失', '会员卡状态', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'Z', '10', '会籍保留', '会员卡状态', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'A1', '1', '很满意', '客户满意度', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'A1', '2', '一般满意', '客户满意度', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'A1', '3', '不满意', '客户满意度', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'A1', '4', '非常不满意', '客户满意度', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'A3', '001', '前台人员态度', '调查项目', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'A3', '002', '美容师态度', '调查项目', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'A3', '003', '美容师技术', '调查项目', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'A3', '004', '美容效果', '调查项目', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'A6', '1', '国家级', '项目水平', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'A6', '2', '地区级', '项目水平', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'A6', '3', '市级', '项目水平', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'A7', '1', '促销折扣', '会员卡销售折扣', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'A7', '2', '内部折扣', '会员卡销售折扣', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'A7', '3', '特别优惠折扣', '会员卡销售折扣', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'A7', '4', '续卡折扣', '会员卡销售折扣', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'A7', '5', '促销折扣*续卡折扣', '会员卡销售折扣', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'A7', '6', '促销折扣*内部折扣', '会员卡销售折扣', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'A8', '0', '充值', '账户异动类别', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'A8', '1', '取款', '账户异动类别', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'A8', '2', '消费', '账户异动类别', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'A8', '5', '欠款', '账户异动类别', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'A8', '6', '欠款返还', '账户异动类别', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'A8', '7', '充值赠送', '账户异动类别', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'A9', '010', '所得税', '薪资项目', 1) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'A9', '006', '业绩提成', '薪资项目', 1) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'A9', '007', '日常奖罚', '薪资项目', 1) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'A9', '001', '基本工资', '薪资项目', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'A9', '002', '交通费补助', '薪资项目', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'A9', '003', '店长奖金', '薪资项目', 2) --项目的业绩方式, 业绩设定项目 insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AA', '1', '固定金额', '业绩分配', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AA', '2', '营业额比率', '业绩分配', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AA', '5', '标准价比率', '业绩分配', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AA', '6', '营业额扣除中工业绩', '业绩分配', 0) -- 营业额*比率 - 中工业绩 insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AA', '7', '营业额扣除小工业绩', '业绩分配', 0) -- 营业额*比率 - 小工业绩 insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AA', '8', '营业额扣除中小工业绩', '业绩分配', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AA', '9', '实际营业额比率', '业绩分配', 0) -- 营业额*实际营业额折算比率 insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AA', '61', '实际营业额扣中工', '业绩分配', 0) -- 营业额*实际营业额折算比率 - 中工 insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AA', '71', '实际营业额扣小工', '业绩分配', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AA', '81', '实际营业额扣中小工', '业绩分配', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AA', '10', '营业额再折扣', '业绩分配', 0) -- 营业额*价格折扣率 insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AA', '11', '实际营业额再折扣', '业绩分配', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AA', '62', '营业额先扣中工业绩', '业绩分配', 0) -- (营业额 - 中工业绩)*比率 insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AA', '72', '营业额先扣小工业绩', '业绩分配', 0) -- (营业额 - 小工业绩)*比率 insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AA', '82', '营业额先扣中小工业绩', '业绩分配', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AA', '99', '平分大工业绩', '业绩分配', 0) insert into #gsm02(gsb00c,gsb01c,gsb02c,gsb03c,gsb04c,gsb05i) values(@compid,'AA','14','固定提成扣中工','提成方式',0) insert into #gsm02(gsb00c,gsb01c,gsb02c,gsb03c,gsb04c,gsb05i) values(@compid,'AA','15','实际营业额比率扣四工','提成方式',0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AB', '0', '美发', '美容类别', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AB', '1', '去斑', '美容类别', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AB', '2', '去痘', '美容类别', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AB', '3', '减肥', '美容类别', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AB', '4', '身体', '美容类别', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AB', '5', '卵巢保养', '美容类别', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AB', '6', '手足护理', '美容类别', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AC', '1', '普通', '操作程序类别', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AC', '2', '强化', '操作程序类别', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AD', '1', '认可', '美容效果类别', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AD', '2', '疑惑', '美容效果类别', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AD', '3', '未达到', '美容效果类别', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AE', '1', '正常', '假别', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AE', '2', '休假', '假别', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AE', '3', '事假', '假别', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AE', '4', '病假', '假别', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AE', '5', '旷工', '假别', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AE', '6', '迟到', '假别', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AE', '7', '加班', '假别', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AE', '8', '未知', '假别', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AE', '9', '未知', '假别', 2) --项目,产品,会员卡销售的提成方式 insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AF', '1', '固定金额', '提成方式', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AF', '2', '营业额比率', '提成方式', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AF', '3', '实业绩比率', '提成方式', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AF', '4', '虚业绩比率', '提成方式', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AF', '41', '实际虚业绩比率', '提成方式', 0) -- 虚业绩*实际营业额折扣率 insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AF', '5', '标准价比率', '提成方式', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AF', '6', '折算虚业绩比率', '提成方式', 0) -- 虚业绩 * 产品的折扣率 insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AF', '61', '实际营业额比率', '提成方式', 0) -- 营业额* 实际营业额折算比率 insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AF', '7', '营业额比率(扣小工提成)', '提成方式', 0) -- 营业额*比率 - 小工提成 insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AF', '8', '营业额比率(扣中工小工提成)', '提成方式', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AF', '9', '营业额比率(扣中工提成)', '提成方式', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AF', '90', '虚业绩比率(扣小工提成)', '提成方式', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AF', '91', '虚业绩比率(扣中工小工提成)', '提成方式', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AF', '92', '虚业绩比率(扣中工提成)', '提成方式', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AF', '93', '实业绩比率(扣小工提成)', '提成方式', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AF', '94', '实业绩比率(扣中工小工提成)', '提成方式', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AF', '95', '实业绩比率(扣中工提成)', '提成方式', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AF', '10', '营业额再折扣', '提成方式', 0) -- 营业额*比率*折扣率 insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AF', '16', '总现金', '提成方式', 0) insert into #gsm02(gsb00c,gsb01c,gsb02c,gsb03c,gsb04c,gsb05i) values(@compid,'AF','14','固定提成扣中工','提成方式',0) insert into #gsm02(gsb00c,gsb01c,gsb02c,gsb03c,gsb04c,gsb05i) values(@compid,'AF','15','实际营业额比率扣四工','提成方式',0) --会员卡销售,产品销售的业绩设置方式 insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AH', '1', '固定金额', '产品销售业绩', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AH', '2', '营业额比率', '产品销售业绩', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AH', '5', '标准价比率', '产品销售业绩', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AH', '9', '实际营业额比率', '产品销售业绩', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AH', '10', '营业额再折扣', '产品销售业绩', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AH', '11', '实际营业额再折扣', '产品销售业绩', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AH', '20', '标准价-进货成本', '产品销售业绩', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AH', '21', '营业额-进货成本', '产品销售业绩', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AH', '22', '标准价-销售成本', '产品销售业绩', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AH', '23', '营业额-销售成本', '产品销售业绩', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AH', '25', '(营业额-当天消费)比率', '产品销售业绩', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AH', '26', '(营业额-当天实际营业额)比率', '产品销售业绩', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AH', '27', '营业额(当天销卡低于设置)比率', '产品销售业绩', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AH', '28', '(营业额-当天消费)(当天销卡低于设置)比率', '产品销售业绩', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AI', '01', '客户投诉', '奖罚', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AI', '02', '产品损耗', '奖罚', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AI', '03', '客户表扬', '奖罚', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AI', '04', '公司奖励', '奖罚', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AI', '05', '经理奖励', '奖罚', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AI', '06', '迟到早退', '奖罚', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AI', '07', '加班奖励', '奖罚', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AI', '08', '培训奖励', '奖罚', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AI', '09', '业绩调整', '奖罚', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AG', '1', '按实际价格的比率', '成本计算方式', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AG', '2', '按标准价格的比率', '成本计算方式', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AG', '3', '固定成本', '成本计算方式', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AG', '4', '按消耗产品的成本', '成本计算方式', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AG', '5', '(4)与(2)中高者', '成本计算方式', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AG', '6', '(4)与(3)中高者', '成本计算方式', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AG', '7', '按实际营业额比率', '成本计算方式', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) -- add lm values(@compid, 'AG', '8', '虚业绩比率', '成本计算方式', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AP', '1', '固定积分', '赠送积分', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AP', '2', '营业额比率', '赠送积分', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AP', '3', '标准额比率', '赠送积分', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AP', '4', '实际营业额比率', '赠送积分', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AQ', '1', '总部', '连锁级别名称', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AQ', '2', '事业部', '连锁级别名称', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AQ', '3', '区域', '连锁级别名称', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AQ', '4', '门店', '连锁级别名称', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, '66', '01', '青春痘', '咨询项目', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, '66', '02', '干燥', '咨询项目', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, '66', '03', '脱皮', '咨询项目', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, '66', '04', '缺水', '咨询项目', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, '66', '05', '油脂过多', '咨询项目', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, '66', '06', '黑斑', '咨询项目', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, '66', '07', '浅皱纹', '咨询项目', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, '66', '08', '深皱纹', '咨询项目', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, '66', '09', '粉刺', '咨询项目', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, '66', '10', '黑头', '咨询项目', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, '66', '11', '暗疮', '咨询项目', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, '66', '12', '深浅雀斑', '咨询项目', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, '66', '13', '暗疮印', '咨询项目', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, '66', '14', '暗疮疤洞', '咨询项目', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, '66', '15', '毛孔阻塞', '咨询项目', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, '66', '16', '毛孔粗大', '咨询项目', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, '66', '17', '黑头多', '咨询项目', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, '66', '18', '大黑头', '咨询项目', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, '66', '19', '小黑头', '咨询项目', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, '66', '20', '多油脂粉刺', '咨询项目', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, '66', '21', '多暗疮', '咨询项目', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, '66', '22', '深斑', '咨询项目', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, '66', '23', '多黑斑', '咨询项目', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, '66', '24', '有损伤', '咨询项目', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, '66', '25', '雀斑', '咨询项目', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, '66', '26', '皱纹', '咨询项目', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, '66', '27', '面疱', '咨询项目', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, '66', '28', '闭锁状粉刺', '咨询项目', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, '66', '29', '细纹/干纹', '咨询项目', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, '66', '30', '鱼尾纹', '咨询项目', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, '66', '31', '浮肿', '咨询项目', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, '66', '32', '眼袋', '咨询项目', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, '66', '33', '黑眼圈', '咨询项目', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, '66', '34', '美丽紧结', '咨询项目', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, '66', '35', '脂肪过多', '咨询项目', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, '66', '36', '下眼肚略黑', '咨询项目', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, '66', '37', '松弛', '咨询项目', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, '66', '38', '横幼皱纹', '咨询项目', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, '66', '39', '深鱼尾纹', '咨询项目', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'B1', '01', '节日问候', '短信类别', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'B1', '02', '活动通知', '短信类别', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'B1', '03', '生日问候', '短信类别', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'B1', '04', '促销通告', '短信类别', 2) --added by cyr 20121101 begin insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'B1', '05', '卖卡提醒', '短信类别', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'B1', '06', '充值提醒', '短信类别', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'B1', '07', '预约成功', '短信类别', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'B1', '08', '预约提醒', '短信类别', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'B1', '09', '消费短信', '短信类别', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'B1', '10', '月初生日问候', '短信类别', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'B1', '11', '员工业绩短信', '短信类别', 2) --added by cyr 20121101 end insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'B2', '01', '待发', '发送状态', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'B2', '02', '发送成功', '发送状态', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'B2', '03', '发送失败', '发送状态', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'B3', '01', '立即发送', '控制状态', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'B3', '02', '预定时间', '控制状态', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'B4', '001', '单人房', '房间分组', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'B4', '002', '两人房', '房间分组', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'B4', '003', '三人房', '房间分组', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'B4', '004', '四人房', '房间分组', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'B4', '005', '贵宾房', '房间分组', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'C1', '1', '正常入库', '入库方式', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'C1', '2', '盘盈入库', '入库方式', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'C1', '3', '客户退货', '入库方式', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'C1', '4', '调拨入库', '入库方式', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'C2', '1', '正常出库', '出库方式', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'C2', '2', '盘亏出库', '出库方式', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'C2', '3', '供应商退货', '出库方式', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'C2', '4', '调拨出库', '出库方式', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'C2', '5', '损坏', '出库方式', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'C2', '6', '赠送', '出库方式', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'C3', '1', '入库', '库存异动', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'C3', '2', '出库', '库存异动', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'C3', '3', '销货', '库存异动', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'C3', '4', '耗用', '库存异动', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'C3', '5', '套餐卖卡', '库存异动', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'C3', '6', '套餐充值', '库存异动', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AJ', '1', '洗头', '统计分类', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AJ', '2', '洗剪吹', '统计分类', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AJ', '3', '烫发', '统计分类', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AJ', '4', '染发', '统计分类', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AJ', '5', '护发', '统计分类', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AJ', '6', '美容', '统计分类', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AJ', '7', '按摩', '统计分类', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AJ', '8', '足疗', '统计分类', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AJ', '9', '美发', '统计分类', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AJ', '231', '仪器类', '统计分类', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AJ', '233', '美容类', '统计分类', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AJ', '235', '美体类', '统计分类', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AL', '1', '美发', '产品统计分类', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AL', '2', '护理', '产品统计分类', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AL', '3', '美容', '产品统计分类', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AL', '4', '精油', '产品统计分类', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AK', '1', '大工', '员工称谓', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AK', '2', '中工', '员工称谓', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AK', '3', '小工', '员工称谓', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AK', '4', '四工', '员工称谓', 2) --供求管理 --订单状态 insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'AM', '0', '未处理', '订单状态', 1) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'AM', '1', '已复核,未下单', '订单状态', 1) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'AM', '2', '部分下单', '订单状态', 1) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'AM', '3', '全部下单', '订单状态', 1) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'AM', '4', '部分款项申请到', '订单状态', 1) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'AM', '5', '款项申请到', '订单状态', 1) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'AM', '6', '部分发货', '订单状态', 1) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'AM', '7', '全部发货', '订单状态', 1) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'AM', '8', '部分收到', '订单状态', 1) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'AM', '9', '全部收到', '订单状态', 1) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'AM', '10', '有欠货', '订单状态', 1) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'AM', '11', '部分结案', '订单状态', 1) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'AM', '12', '全部结案', '订单状态', 1) --发货申请单状态 insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'AN', '0', '等待申请', '发货申请单状态', 1) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'AN', '1', '款项申请到', '发货申请单状态', 1) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'AN', '2', '货已发送', '发货申请单状态', 1) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'AN', '3', '货已收到', '发货申请单状态', 1) --发货申请单申请款项状态 insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'AO', '0', '等待申请', '发货申请单款项状态', 1) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'AO', '1', '款项申请到', '发货申请单款项状态', 1) --累计分段计算业绩标准 insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'B5', '1', '指定金额类型', '累计分段计算业绩标准', 1) -- 就是按后面设置的提成的基础数的类型,可能是虚业绩,可能是实业绩 insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'B5', '2', '指定项目数', '累计分段计算业绩标准', 1) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'B5', '3', '回头率', '累计分段计算业绩标准', 1) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'B5', '4', '总指定客数', '累计分段计算业绩标准', 1) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'B5', '5', '总项目数', '累计分段计算业绩标准', 1) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'B5', '6', '指定业绩', '累计分段计算业绩标准', 1) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'B5', '7', '营业额', '累计分段计算业绩标准', 1) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'B5', '8', '总客数', '累计分段计算业绩标准', 1) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'B5', '9', '虚业绩', '累计分段计算业绩标准', 1) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'B5', '10', '总现金', '累计分段计算业绩标准', 1) --卡累计分段计算业绩标准 insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'BD', '5', '卖卡数', '卡累计分段计算业绩标准', 1) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'BD', '6', '业绩', '卡累计分段计算业绩标准', 1) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'BD', '7', '营业额', '卡累计分段计算业绩标准', 1) --多次项目的价格称谓 insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'B6', '0', '标准价', '批次价格称谓', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'B6', '1', '月卡', '批次价格称谓', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'B6', '2', '季卡', '批次价格称谓', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'B6', '3', '半年卡', '批次价格称谓', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'B6', '4', '年卡', '批次价格称谓', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'B6', '5', '体验价', '批次价格称谓', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'B6', '6', '疗程价', '批次价格称谓', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'B6', '7', '套餐价', '批次价格称谓', 0) --产品的价格称谓 insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'B7', '0', '标准价', '产品价格称谓', 0) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'B7', '1', '身体价', '产品价格称谓', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'B7', '2', '面部价', '产品价格称谓', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'B7', '3', '门店价', '产品价格称谓', 1) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'B7', '4', '销售价格2', '产品价格称谓', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'B7', '5', '销售价格3', '产品价格称谓', 2) --保证金类型 insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'B8', '1', '产品保证金', '保证金类型', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'B8', '2', '买卡保证金', '保证金类型', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'B8', '3', '员工保证金', '保证金类型', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'B9', '1', '100面值', '抵用券类型', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'B9', '2', '200面值', '抵用券类型', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'B9', '3', '300面值', '抵用券类型', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'BA', 'UR001', '查看日记账的天数', '使用者权限设置', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'BA', 'UR002', '查看消费明细表的天数', '使用者权限设置', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'BA', 'UR003', '查看日常单据的天数', '使用者权限设置', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'BA', 'UR004', '可修改销售卡业绩的天数', '使用者权限设置', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'BA', 'UR005', '是否可查看产品成本', '使用者权限设置', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'BA', 'UR006', '是否可修改销售疗程次数与金额', '使用者权限设置', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'BA', 'UR007', '是否可以修改套餐内的项目或产品数量', '使用者权限设置', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'BA', 'UR008', '修改单据不需要客户密码', '使用者权限设置', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'BA', 'UR009', '保证金几天内允许退款', '使用者权限设置', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'BA', 'UR010', '保证金几天内允许修改', '使用者权限设置', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'BA', 'UR011', '会员资料登记后几天内允许修改', '使用者权限设置', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'BA', 'UR012', '是否允许疗程账户透支消费', '使用者权限设置', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'BA', 'UR013', '是否可直接销售不存在的会员卡号', '使用者权限设置', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'BA', 'UR014', '销售时是否可修改会员卡资料', '使用者权限设置', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'BA', 'UR015', '是否可修改的单据类型(售前, 售后, 嘉宾)', '使用者权限设置', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'BA', 'UR016', '是否可用内调支付方式', '使用者权限设置', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'BA', 'UR017', '是否可以进行套餐明细抵扣', '使用者权限设置', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'BA', 'UR018', '是否可以强行删除卡', '使用者权限设置', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'BA', 'UR019', '是否可以查看需要隐藏的特殊客户', '使用者权限设置', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'BA', 'UR020', '是否可以直接输入套餐资料', '使用者权限设置', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'BA', 'UR021', '是否可以修改账户余额', '使用者权限设置', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'BA', 'UR022', '是否可以修改疗程次数', '使用者权限设置', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'BA', 'UR023', '奖罚控制(1-可见0-不可见2-只可见)', '使用者权限设置', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'BA', 'UR024', '是否可看店长看板', '使用者权限设置', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'BA', 'UR025', '是否可分发基础数据', '使用者权限设置', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'BA', 'UR026', '查看库存单据的天数', '使用者权限设置', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'BA', 'UR027', '是否可以修改会员卡备注', '使用者权限设置', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'BA', 'UR028', '是否允许导致负库存的操作', '使用者权限设置', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'BA', 'UR029', '可见考勤资料的天数', '使用者权限设置', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'BA', 'UR030', '是否可以修改收银项目标准价格', '使用者权限设置', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'BA', 'UR031', '是否可以修改收银项目折扣与金额', '使用者权限设置', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'BA', 'UR032', '是否可以修改收银产品标准价格', '使用者权限设置', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'BA', 'UR033', '是否可以修改收银产品折扣与金额', '使用者权限设置', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'BA', 'UR034', '是否允许消费项目欠款', '使用者权限设置', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'BA', 'UR035', '是否允许购买产品欠款', '使用者权限设置', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'BA', 'UR036', '是否允许会员卡负余额', '使用者权限设置', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'BA', 'UR037', '是否允许修改产品成本', '使用者权限设置', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'BA', 'UR038', '是否可以挂失解挂', '使用者权限设置', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'BA', 'UR039', '是否可以修改服务人员', '使用者权限设置', 2) --Add by maql 20101013 insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'BA', 'UR040', '是否可以查看开单统计信息', '使用者权限设置', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'BA', 'UR041', '是否可以修改卡销售支付明细', '使用者权限设置', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'BA', 'UR042', '是否可以看到客户联系方式', '使用者权限设置', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'BA', 'UR043', '是否可以修改疗程单价', '使用者权限设置', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'BA', 'UR044', '员工业绩查看天数', '使用者权限设置', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'BA', 'UR045', '是否可登录经理终端', '使用者权限设置', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'BA', 'UR046', '是否可设置授权', '使用者权限设置', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'BA', 'UR047', '是否可以修改疗程截止日期', '使用者权限设置', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'BA', 'UR048', '是否有分发拷贝权限', '使用者权限设置', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'BA', 'UR049', '是否有前台终端-顾客开单权限', '使用者权限设置', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'BA', 'UR051', '是否有员工信息维护中心权限', '使用者权限设置', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'BA', 'UR052', '控制是否可以审核员工入职', '使用者权限设置', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'BA', 'UR053', '控制是否可以审核员工离职', '使用者权限设置', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'BA', 'UR054', '是否有察看离职员工权限', '使用者权限设置', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'BA', 'UR055', '是否关闭预约提醒', '使用者权限设置', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'BA', 'UR056', '控制跨店卡升级是否有变更卡归属门店的权限', '使用者权限设置', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) -- add 会员等级 2015.09.15 values(@compid, 'BA', 'UR057', '是否可设置会员等级', '使用者权限设置', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) -- add 赠送账户修改 2015.09.15 values(@compid, 'BA', 'UR058', '是否可修改赠送账户和赠送金额', '使用者权限设置', 2) insert gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'BA', 'UR059', '是否有可以使用经理签单', '使用者权限设置', 0) insert gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'BA', 'UR060', '是否允许修改疗程赠送次数', '使用者权限设置', 2) insert into #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) --Added by cyr 20121107 values(@compid, 'BA', 'UN001', '是否可以发送员工业绩汇总短信', '使用者权限设置', 2) insert into #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) --Added by cyr 20121116 values(@compid, 'BA', 'UN002', '是否可以发送当日员工业绩短信', '使用者权限设置', 2) insert into gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) --Added by cyr 20121130 values(@compid, 'BA', 'UN003', '是否可以操作收入支持登记中的虚拟款项', '使用者权限设置', 2) insert into gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) --Added by cyr 20121203 values(@compid, 'BA', 'UN004', '是否可以修改奖罚日期', '使用者权限设置', 2) insert into gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) --Added by cyr 20121217 values(@compid, 'BA', 'UN005', '是否可以修改收银产品消耗', '使用者权限设置', 2) insert into gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) --Added by cyr 20121217 values(@compid, 'BA', 'UN006', '是否可以修改卡销售折扣', '使用者权限设置', 2) insert into gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) --Added by cyr 20121228 values(@compid, 'BA', 'UN007', '是否可以查看产品销售成本', '使用者权限设置', 2) insert into gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) --Added by cyr 20121228 values(@compid, 'BA', 'UN008', '是否可以查看产品消耗成本', '使用者权限设置', 2) insert into gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) --Added by cyr 20121228 values(@compid, 'BA', 'UN009', '是否可以查看产品标准价', '使用者权限设置', 2) insert into gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) --Added by cyr 20121228 values(@compid, 'BA', 'UN010', '是否可以编辑产品出库/入库明细金额', '使用者权限设置', 2) insert into gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'BA', 'UN011', '是否可以派遣员工', '使用者权限设置', 2) --Added by lm 20130128 insert into gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'BA', 'UN012', '是否可以修改支付方式', '使用者权限设置', 2) --Added by cyr 20130218 insert into gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'BA', 'UN013', '是否可以设定回访满意度对照图', '使用者权限设置', 2) --Added by cyr 201303005 insert into gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'BA', 'UN014', '是否可以查看服务人员满意度', '使用者权限设置', 2) --Added by cyr 20130305 insert into gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'BA', 'UN015', '是否可以回访服务人员满意度', '使用者权限设置', 2) --Added by cyr 20130305 insert into gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'BA', 'UN016', '是否可以查看收银员满意度', '使用者权限设置', 2) --Added by cyr 20130305 insert into gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'BA', 'UN017', '是否可以回访收银员满意度', '使用者权限设置', 2) --Added by cyr 20130305 insert into gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'BA', 'UN018', '是否可以查看门店满意度', '使用者权限设置', 2) --Added by cyr 20130305 insert into gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'BA', 'UN019', '是否可以回访门店满意度', '使用者权限设置', 2) --Added by cyr 20130305 insert into gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'BA', 'UN020', '是否可以查看实业绩', '使用者权限设置', 2) --Added by cyr 20130312 insert into gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'BA', 'UN021', '是否可以查看每日提醒', '使用者权限设置', 2) --Added by cyr 20130314 insert into gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'BA', 'UN022', '是否可以修改已回访的回访单', '使用者权限设置', 2) --Added by cyr 20130314 insert into gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'BA', 'UN023', '可以查看已回访的天数', '使用者权限设置', 2) --Added by cyr 20130314 insert into gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'BA', 'UN024', '是否可以批量开帐', '使用者权限设置', 2) --Added by cyr 20130403 insert into gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'BA', 'UN025', '可以查看员工奖罚的天数', '使用者权限设置', 2) --Added by cyr 20130407 insert into gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'BA', 'UN026', '是否可以修改卡销售时的卡标准价', '使用者权限设置', 2) --Added by cyr 20130407 insert into gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'BA', 'UN027', '是否可以导出会员卡资料', '使用者权限设置', 2) --Added by cyr 20130415 insert into gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'BA', 'UN028', '销售卡时是否可修改卡应收金额', '使用者权限设置', 2) --Added by cyr 20130510 insert into gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'BA', 'UN029', '销售卡时是否可修改卡储值金额', '使用者权限设置', 2) --Added by cyr 20130510 insert into gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'BA', 'UN030', '销售卡时卡标准价是否允许低于卡类别标准价', '使用者权限设置', 2) --Added by cyr 20130603 insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'BA', 'UN031', '是否允许修改个人账户折扣系数', '使用者权限设置', 2) --Added by cyr 20120407 insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'BA', 'UN032', '是否允许设置员工提成方案', '使用者权限设置', 2) --Added by cyr 20120416 insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'BA', 'UN033', '是否允许打印员工业绩小单', '使用者权限设置', 2) --Added by cyr 20120904 insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'BA', 'UN034', '是否允许打印日记账小单', '使用者权限设置', 2) --Added by cyr 20120904 insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'BB', 'A4', 'A4(2100*2970)', '报表纸张类型', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'BB', 'A5', 'A5(1480*2100)', '报表纸张类型', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'BB', 'A3', 'A3(2970*4200)', '报表纸张类型', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'BB', 'Let', 'Letter(2159*2794)', '报表纸张类型', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'BB', 'B4', 'B4(2570*3640)', '报表纸张类型', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'BB', 'B5', 'B5(1820*2570)', '报表纸张类型', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'BB', '走半L', '纸张走半Letter(2159*1397)', '报表纸张类型', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'BB', '1/3L', '纸张走1/3Letter(2159*931)', '报表纸张类型', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'BB', 'L二分', 'Letter(2159*2794)等分', '报表纸张类型', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'BB', 'L三分', 'Letter(2159*2794)三等分', '报表纸张类型', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'BC', '0(单)', '1', '有效期限设置', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'BC', '1(月)', '3', '有效期限设置', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'BC', '2(季)', '6', '有效期限设置', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'BC', '3(半)', '12', '有效期限设置', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'BC', '41美', '24', '有效期限设置', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values (@compid, 'BC', '42特', '36', '有效期限设置', 2) --项目分组 cyr 2012/07/27 insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AR', '1', '洗剪吹', '统计分类', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AR', '2', '烫染护', '统计分类', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AR', '3', '美容', '统计分类', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'AR', '4', '足浴', '统计分类', 2) --进店渠道 cyr 2012/12/24 insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'NOS0', '0', '默认', '进店渠道', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'NOS0', '1', '朋友介绍', '进店渠道', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'NOS0', '2', '广告传单', '进店渠道', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'NOS0', '3', '电视广告', '进店渠道', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'APO', '1', '靓丽中国', '预约来源', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'APO', '2', '微信', '预约来源', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'APO', '3', 'NOS客户端', '预约来源', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'APO', '4', '前台终端', '预约来源', 2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid, 'APO', '5', '客户终端', '预约来源', 2) --会员等级 insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid,'DJ','0','黄金客户','客户等级',2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid,'DJ','1','白银客户','客户等级',2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid,'DJ','2','普通客户','客户等级',2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid,'DJ','3','休眠客户','客户等级',2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid,'DJ','4','死亡客户','客户等级',2) --会员发展潜质 insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid,'QZ','0','无','客户发展潜质',2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid,'QZ','1','重点发展','客户发展潜质',2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid,'QZ','2','可持续','客户发展潜质',2) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) values(@compid,'QZ','3','待发展','客户发展潜质',2) delete a from #gsm02 a, gsm02 b where a.gsb00c = b.gsb00c and a.gsb01c = b.gsb01c and a.gsb02c = b.gsb02c and b.gsb00c = @compid if exists(select 1 from gsm02 where gsb00c = @compid and gsb01c = 'AJ') begin delete #gsm02 where gsb00c = @compid and gsb01c = 'AJ' end if exists(select 1 from gsm02 where gsb00c = @compid and gsb01c = 'AL') begin delete #gsm02 where gsb00c = @compid and gsb01c = 'AL' end insert gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i) select gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i from #gsm02 update gsm02 set gsb06i = 1 where isnull(gsb06i,0) = 0 drop table #gsm02 exec upg_init_acct2pay if not exists(select 1 from gam04 where gad00c = @compid) begin insert gam04(gad00c, gad01c, gad02c, gad07c) values(@compid, '01', '总仓库', '总仓库') insert gam04(gad00c, gad01c, gad02c, gad07c) values(@compid, '02', '销售品仓库', '销售品仓库') insert gam04(gad00c, gad01c, gad02c, gad07c) values(@compid, '03', '消耗品仓库', '消耗品仓库') insert gam04(gad00c, gad01c, gad02c, gad07c) values(@compid, '04', '自用品仓库', '自用品仓库') end if not exists(select 1 from gsm26) begin insert into gsm26(gsp00c, gsp02i, gsp03c, gsp04c, gsp05c, gsp06c) select gsb00c, 0, gsb02c, gsb01c, '', ''from gsm02 where gsb01c = 'F' insert into gsm26(gsp00c, gsp02i, gsp03c, gsp04c, gsp05c, gsp06c) select gsb00c, 1, gsb02c, gsb01c, '', '' from gsm02 where gsb01c = 'L' insert into gsm26(gsp00c, gsp02i, gsp03c, gsp04c, gsp05c, gsp06c) select gsb00c, 2, gsb02c, gsb01c, '', '' from gsm02 where gsb01c = 'AL' insert into gsm26(gsp00c, gsp02i, gsp03c, gsp04c, gsp05c, gsp06c) select gsb00c, 3, gsb02c, gsb01c, '', '' from gsm02 where gsb01c = 'AJ' end if not exists(select 1 from gsm27) begin insert gsm27(gsq00c, gsq02i, gsq03c, gsq04c, gsq05c) select gsb00c, 0, gsb02c, gsb02c, gsb01c from gsm02 where gsb01c = 'F' insert gsm27(gsq00c, gsq02i, gsq03c, gsq04c, gsq05c) select gsb00c, 1, gsb02c, gsb02c, gsb01c from gsm02 where gsb01c = 'L' insert gsm27(gsq00c, gsq02i, gsq03c, gsq04c, gsq05c) select gsb00c, 2, gsb02c, gsb02c, gsb01c from gsm02 where gsb01c = 'AL' insert gsm27(gsq00c, gsq02i, gsq03c, gsq04c, gsq05c) select gsb00c, 3, gsb02c, gsb02c, gsb01c from gsm02 where gsb01c = 'AJ' end end GO /****** Object: StoredProcedure [dbo].[upg_undo_consume_in_iccard] Script Date: 06/12/2017 06:20:48 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_undo_consume_in_iccard]( @compid varchar(10), @cardid varchar(20), @seq int, @curdate varchar(10), @amt float, @billid varchar(20)) as begin delete gcm13 where gco00c = @compid and gco01c = @cardid and gco02i = @seq update gcm03 set gcc06f = gcc06f + @amt where gcc00c = @compid and gcc01c = @cardid and gcc03i = 2 delete gcm04 where gcd00c = @compid and gcd01c = @cardid and gcd02i = 2 and gcd06f = @amt and gcd07c = 'gx' and gcd09d = @curdate and gcd08c = @billid exec upg_recal_gcm04_lastremain @cardid, 2 end GO /****** Object: StoredProcedure [dbo].[upg_dispatch_data_proc_set_perf_comm_single] Script Date: 06/12/2017 06:20:01 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_dispatch_data_proc_set_perf_comm_single] ( @compid varchar(10) ) as begin create table #tbl_control ( compid varchar(10) not null, -- 儿子公司的代码 lvl int null, primary key(compid) ) insert #tbl_control(compid, lvl) select bran_id, control_lvl from b_perf a, basic_data_control b where parent_id = @compid and a.bran_id = b.compid and b.datatype = 'proc_set_perf_comm' and b.control_lvl = 1 -- 创建循环变量 declare @HasRecord int set @HasRecord = 1 declare @currentCompId varchar(10) declare @lvl int while (@HasRecord = 1) begin if exists (select 1 from #tbl_control) begin set @HasRecord = 1 select top 1 @currentCompId = compid, @lvl = lvl from #tbl_control if @lvl = 1 -- 严格按总部的 begin -- 先删除自己的,把总部分发下来 -- 强制模式都是先删除原来的 exec upg_m_dispatch_proc_set_perf_comm_single @compid, @currentCompId, 1 insert #tbl_control(compid, lvl) select bran_id, control_lvl from b_perf a, basic_data_control b where parent_id = @currentCompId and a.bran_id = b.compid and b.datatype = 'proc_set_perf_comm' and b.control_lvl = 1 end else if @lvl = 2 begin exec upg_m_dispatch_proc_set_perf_comm_single @compid, @currentCompId, 0 insert #tbl_control(compid, lvl) select bran_id, case when (control_lvl <= 2) then 2 else control_lvl end from b_perf a, basic_data_control b where parent_id = @currentCompId and a.bran_id = b.compid and b.datatype = 'proc_set_perf_comm' and b.control_lvl = 1 end -- 清除已处理的门店 delete #tbl_control where compid = @currentCompId end else begin set @HasRecord = 0 end end drop table #tbl_control end GO /****** Object: StoredProcedure [dbo].[upg_compute_cost_perf_project_ceshi] Script Date: 06/12/2017 06:19:44 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_compute_cost_perf_project_ceshi] ( @compid varchar(10), -- 公司别 @billid varchar(20), -- 收银单号 @cardtype varchar(20), -- 会员卡类别, 消费时候的类别 @cardno varchar(20), -- 会员卡号 @ddate varchar(8) , -- 账单日期 @cash_prj_rate_points float , @perf_rate_prj_gam10 float , -- 业绩折算比率,项目 @card_kind int , -- 主要为了识别是否为是否为资格卡 @pos_reduce_rate float , @input_project_perf varchar(10), @cash_prj_rate float, @item varchar(20), -- 项目编码 @standardprice float, -- 标准价 @price float, -- 实际价 @amt float, -- 项目总价 @quan float, -- 数量 @mode_1 varchar(20), @mode_2 varchar(20), @mode_3 varchar(20), @mode_4 varchar(20), @staff_1 varchar(20), @staff_2 varchar(20), @staff_3 varchar(20), @staff_4 varchar(20), @paymode varchar(20), @vperf_1 float, @vperf_2 float, @vperf_3 float, @vperf_4 float, @ggm02_seq float, @input_vperf1 float, @input_vperf2 float, @input_vperf3 float, @input_vperf4 float, @fa_type nvarchar(20), -- 方案类型 @fa_type2 nvarchar(20), @fa_type3 nvarchar(20), @fa_type4 nvarchar(20), @costtype1 int, @cost_setvalue1 float, @caltperf1_1 int , @caltperf1_2 int , @caltperf1_3 int, @caltperf1_4 int , @costtype2 int, @cost_setvalue2 float, @caltperf2_1 int , @caltperf2_2 int , @caltperf2_3 int, @caltperf2_4 int , @costtype3 int, @cost_setvalue3 float, @caltperf3_1 int , @caltperf3_2 int , @caltperf3_3 int, @caltperf3_4 int , @costtype4 int, @cost_setvalue4 float, @caltperf4_1 int , @caltperf4_2 int , @caltperf4_3 int, @caltperf4_4 int, @cardrate float, --转卡比率 @rmbrate float --转现比率 ) as begin -- 散客,只要在会员卡类别设定中有Z的类别,实际营业额的折算比率就按Z的会员卡类别设定中 -- 不管支付方式为多少 declare @is_cash int -- 标志是否设置了Z的类别 declare @saved_perf_rate_prj_gam10 float -- 业绩折算比率,项目 -- 散客 if substring(@cardno, 1, 2) = '散客' begin if (@cardtype = 'z') begin select @is_cash = 1 select @saved_perf_rate_prj_gam10 = @perf_rate_prj_gam10 end else begin select @is_cash = 0 end end declare @old_prj_ex_rate_points float -- 项目积分折算比率 select @saved_perf_rate_prj_gam10 = @perf_rate_prj_gam10 if isnull(@old_prj_ex_rate_points, -1) = -1 select @old_prj_ex_rate_points = 1 select @pos_reduce_rate = isnull(@pos_reduce_rate, 0) declare @old_prj_ex_rate float select @old_prj_ex_rate = @cash_prj_rate- @pos_reduce_rate -- 允许折算率为0存在 -- 虽然ggm01中有卡存在但不一定用卡来支付 -- 所以实际的折算率还应该考虑按支付方式的折扣率 if isnull(@old_prj_ex_rate, -1) = -1 select @old_prj_ex_rate = 1 declare @caltperf_1 int declare @caltperf_2 int declare @caltperf_3 int declare @caltperf_4 int declare @ex_prj_rate_points float -- 根据卡的类别取得积分的折算比率 declare @p_ex_rate float -- 根据支付方式的不同取得实际营业额的折算比率 declare @p_ex_rate_points float -- 根据支付方式的不同取得积分的折算比率 declare @ex_prj_rate float -- 根据卡的类别取得项目实际营业额的折算比率 declare @p_mode int declare @p_setvalue float declare @p_points float declare @cost float -- 最后计算出来的成本 declare @personidd nvarchar(10) declare @personidd2 nvarchar(10) declare @personidd3 nvarchar(10) declare @personidd4 nvarchar(10) declare @costtype int -- 计算成本类型 -- 1:按实际价格的比率,2-按标准价格的比率,3-固定成本 -- 4: 按消耗产品的成本,5: 按成本最高者(标准价格的比率与消耗成本比) declare @cost_setvalue float -- 设置成本的值 declare @goods_cost float -- 消耗产品的成本 select @p_ex_rate = 1, @p_ex_rate_points = 1, @vperf_1 = 0, @vperf_2 = 0, @vperf_3 = 0, @vperf_4 = 0 -- 还需要考虑支付方式的不同 select @p_ex_rate = gso03i, @p_ex_rate_points = gso04f from gsm15 where gso00c = @compid and gso02c = @paymode --允许折算率为0存在 if isnull(@p_ex_rate, -1) = -1 select @p_ex_rate = 1 if isnull(@p_ex_rate_points, -1) = -1 select @p_ex_rate_points = 1 if @card_kind = 2 -- 资格卡 begin select @ex_prj_rate_points = @old_prj_ex_rate_points * @p_ex_rate_points select @ex_prj_rate = @old_prj_ex_rate * @p_ex_rate end else begin if @paymode = '4' begin -- 按储值账户支付, 按卡类别的折算*支付方式的折算率 select @ex_prj_rate_points = @old_prj_ex_rate_points * @p_ex_rate_points select @ex_prj_rate = @old_prj_ex_rate * @p_ex_rate select @perf_rate_prj_gam10 = @saved_perf_rate_prj_gam10 end else begin -- 按其他账户支付或其他支付方式 select @ex_prj_rate_points = @p_ex_rate_points select @ex_prj_rate = @p_ex_rate select @perf_rate_prj_gam10 = 1 end -- 如果设定了Z(现金的实际营业额比率) if @is_cash = 1 begin -- Z的折扣率*支付方式设置的折扣率 select @ex_prj_rate_points = @cash_prj_rate_points * @p_ex_rate_points if isnull(@cash_prj_rate, -1) = -1 select @cash_prj_rate = 1 select @ex_prj_rate = @cash_prj_rate * @p_ex_rate end end -- 取得项目的积分设定 select @p_mode = gda17i, @p_setvalue = gda18f from gdm01 where gda00c = @compid and gda01c = @item -- 得到提成方案 -- 获取在账单有效日期内最新一条员工提成方案 set @personidd = isnull(@staff_1, '') set @personidd2 = isnull(@staff_2, '') set @personidd3 = isnull(@staff_3, '') set @personidd4 = isnull(@staff_4, '') -- 取得不同会员卡类别不同项目类别设置的实际营业额折扣率 declare @cardtype_itemtype_rate float exec upg_get_cardtype_itemtype_rate @compid, @cardtype, @item, @paymode, '1', @cardtype_itemtype_rate output select @ex_prj_rate = isnull(@ex_prj_rate, 1) * isnull(@cardtype_itemtype_rate, 1) update ggm02 set ggb18f = null where ggb00c = @compid and ggb01c = @billid and ggb03c = @item and ggb02f = @ggm02_seq if (@paymode = '#' or @paymode = '4' or @paymode = ''or @paymode = '9' or (@paymode >= 'A' and @paymode <= 'Z')) begin update ggm02 set ggb18f = @cardrate where ggb00c = @compid and ggb01c = @billid and ggb03c = @item and ggb02f = @ggm02_seq end else if (@paymode = '1' or @paymode = '6') begin update ggm02 set ggb18f = @rmbrate where ggb00c = @compid and ggb01c = @billid and ggb03c = @item and ggb02f = @ggm02_seq end --计算积分 exec upg_cal_all_employees_vperf @compid, @cardno, @item, @quan, @standardprice, @price, @amt, @ex_prj_rate, @ex_prj_rate_points, @perf_rate_prj_gam10, @mode_1, @mode_2, @mode_3, @mode_4, @personidd, '', '', '', @vperf_1 output, @vperf_2 output, @vperf_3 output, @vperf_4 output, @paymode, @p_mode, @p_setvalue, @p_points output, @fa_type, @fa_type2, @fa_type3, @fa_type4 -- ggb17f、ggb33f、ggb34f以第一销售为标准 --add by lm 2015.12.08 select @cardrate = hea13f, @rmbrate = hea24f from (select hea13f, hea24f from hem01 with (nolock) where hea00c = @compid and hea01c = @item and hea33c = @fa_type and hea18i = 2 and hea34c = @paymode union all select hea13f, hea24f from hem01 with (nolock) where hea00c = @compid and hea01c = @item and hea33c = @fa_type and hea18i = 2 and isnull(hea34c, '*') = '*' ) A update ggm02 set ggb17f = @cost, ggb33f = @p_points, ggb34f = @ex_prj_rate where ggb00c = @compid and ggb01c = @billid and ggb03c = @item and ggb02f = @ggm02_seq if (isnull(@staff_1, '') <> '') --add by lm 2015.12.08 begin select @costtype = isnull(@costtype1, 1) select @cost_setvalue = isnull(@cost_setvalue1, 0) select @caltperf_1 = @caltperf1_1 select @caltperf_2 = @caltperf1_2 select @caltperf_3 = @caltperf1_3 select @caltperf_4 = @caltperf1_4 -- 计算业绩 exec upg_cal_all_employees_vperf @compid, @cardno, @item, @quan, @standardprice, @price, @amt, @ex_prj_rate, @ex_prj_rate_points, @perf_rate_prj_gam10, @mode_1, @mode_2, @mode_3, @mode_4, @staff_1, @staff_2, @staff_3, @staff_4, @vperf_1 output, @vperf_2 output, @vperf_3 output, @vperf_4 output, @paymode, @p_mode, @p_setvalue, @p_points output, @fa_type, @fa_type2, @fa_type3, @fa_type4 if @input_project_perf = '1' begin select @vperf_1 = isnull(@input_vperf1, 0) end -- 计算成本 if @costtype = 1 select @cost = @amt * @cost_setvalue -- 金额已经数量 else if @costtype = 2 select @cost = @standardprice * @quan * @cost_setvalue else if @costtype = 3 select @cost = @cost_setvalue * @quan else if @costtype = 4 begin exec upg_sum_goods_cost @compid, @billid, @item, @ggm02_seq, @goods_cost output select @cost = @goods_cost end else if @costtype = 5 begin exec upg_sum_goods_cost @compid, @billid, @item, @ggm02_seq, @goods_cost output if @standardprice * @quan * @cost_setvalue > @goods_cost select @cost = @standardprice * @quan * @cost_setvalue else select @cost = @goods_cost end else if @costtype = 6 begin exec upg_sum_goods_cost @compid, @billid, @item, @ggm02_seq, @goods_cost output if @cost_setvalue * @quan > @goods_cost select @cost = @cost_setvalue * @quan else select @cost = @goods_cost end else if @costtype = 7 begin select @cost = @amt * @cost_setvalue * @ex_prj_rate end else if @costtype = 8 --虚业绩比率 add by lm 2015.11.05 begin select @cost = @vperf_1 * @cost_setvalue end else if @costtype = 9 -- add by lm 2016.04.22 begin exec upg_sum_goods_cost @compid, @billid, @item, @ggm02_seq, @goods_cost output if @vperf_1 * @cost_setvalue > @goods_cost select @cost = @vperf_1 * @cost_setvalue else select @cost = @goods_cost end update ggm02 set ggb20f = @vperf_1, ggb36f = @vperf_1 - isnull(@cost, 0) * isnull(@caltperf_1, 0) where ggb00c = @compid and ggb01c = @billid and ggb03c = @item and ggb02f = @ggm02_seq end if (isnull(@staff_2, '') <> '') --add by lm 2015.12.08 begin select @costtype = isnull(@costtype2, 1) select @cost_setvalue = isnull(@cost_setvalue2, 0) select @caltperf_1 = @caltperf2_1 select @caltperf_2 = @caltperf2_2 select @caltperf_3 = @caltperf2_3 select @caltperf_4 = @caltperf2_4 -- 计算业绩 exec upg_cal_all_employees_vperf @compid, @cardno, @item, @quan, @standardprice, @price, @amt, @ex_prj_rate, @ex_prj_rate_points, @perf_rate_prj_gam10, @mode_1, @mode_2, @mode_3, @mode_4, @staff_1, @staff_2, @staff_3, @staff_4, @vperf_1 output, @vperf_2 output, @vperf_3 output, @vperf_4 output, @paymode, @p_mode, @p_setvalue, @p_points output, @fa_type, @fa_type2, @fa_type3, @fa_type4 if @input_project_perf = '1' begin select @vperf_2 = isnull(@input_vperf2,0) end -- 计算成本 if @costtype = 1 select @cost = @amt * @cost_setvalue -- 金额已经数量 else if @costtype = 2 select @cost = @standardprice * @quan * @cost_setvalue else if @costtype = 3 select @cost = @cost_setvalue * @quan else if @costtype = 4 begin exec upg_sum_goods_cost @compid, @billid, @item, @ggm02_seq, @goods_cost output select @cost = @goods_cost end else if @costtype = 5 begin exec upg_sum_goods_cost @compid, @billid, @item, @ggm02_seq, @goods_cost output if @standardprice * @quan * @cost_setvalue > @goods_cost select @cost = @standardprice * @quan * @cost_setvalue else select @cost = @goods_cost end else if @costtype = 6 begin exec upg_sum_goods_cost @compid, @billid, @item, @ggm02_seq, @goods_cost output if @cost_setvalue * @quan > @goods_cost select @cost = @cost_setvalue * @quan else select @cost = @goods_cost end else if @costtype = 7 begin select @cost = @amt * @cost_setvalue * @ex_prj_rate end else if @costtype = 8 --虚业绩比率 add by lm 2015.11.05 begin select @cost = @vperf_2 * @cost_setvalue end else if @costtype = 9 -- add by lm 2016.04.22 begin exec upg_sum_goods_cost @compid, @billid, @item, @ggm02_seq, @goods_cost output if @vperf_2 * @cost_setvalue > @goods_cost select @cost = @vperf_2 * @cost_setvalue else select @cost = @goods_cost end update ggm02 set ggb21f = @vperf_2, ggb37f = @vperf_2 - isnull(@cost, 0) * isnull(@caltperf_2, 0) where ggb00c = @compid and ggb01c = @billid and ggb03c = @item and ggb02f = @ggm02_seq end if (isnull(@staff_3, '') <> '') --add by lm 2015.12.08 begin select @costtype = isnull(@costtype3, 3) select @cost_setvalue = isnull(@cost_setvalue3, 0) select @caltperf_1 = @caltperf3_1 select @caltperf_2 = @caltperf3_2 select @caltperf_3 = @caltperf3_3 select @caltperf_4 = @caltperf3_4 -- 计算业绩 exec upg_cal_all_employees_vperf @compid, @cardno, @item, @quan, @standardprice, @price, @amt, @ex_prj_rate, @ex_prj_rate_points, @perf_rate_prj_gam10, @mode_1, @mode_2, @mode_3, @mode_4, @staff_1, @staff_2, @staff_3, @staff_4, @vperf_1 output, @vperf_2 output, @vperf_3 output, @vperf_4 output, @paymode, @p_mode, @p_setvalue, @p_points output, @fa_type, @fa_type2, @fa_type3, @fa_type4 if @input_project_perf = '1' begin select @vperf_3 = isnull(@input_vperf3,0) end -- 计算成本 if @costtype = 1 select @cost = @amt * @cost_setvalue -- 金额已经数量 else if @costtype = 2 select @cost = @standardprice * @quan * @cost_setvalue else if @costtype = 3 select @cost = @cost_setvalue * @quan else if @costtype = 4 begin exec upg_sum_goods_cost @compid, @billid, @item, @ggm02_seq, @goods_cost output select @cost = @goods_cost end else if @costtype = 5 begin exec upg_sum_goods_cost @compid, @billid, @item, @ggm02_seq, @goods_cost output if @standardprice * @quan * @cost_setvalue > @goods_cost select @cost = @standardprice * @quan * @cost_setvalue else select @cost = @goods_cost end else if @costtype = 6 begin exec upg_sum_goods_cost @compid, @billid, @item, @ggm02_seq, @goods_cost output if @cost_setvalue * @quan > @goods_cost select @cost = @cost_setvalue * @quan else select @cost = @goods_cost end else if @costtype = 7 begin select @cost = @amt * @cost_setvalue * @ex_prj_rate end else if @costtype = 8 --虚业绩比率 add by lm 2015.11.05 begin select @cost = @vperf_3 * @cost_setvalue end else if @costtype = 9 -- add by lm 2016.04.22 begin exec upg_sum_goods_cost @compid, @billid, @item, @ggm02_seq, @goods_cost output if @vperf_3 * @cost_setvalue > @goods_cost select @cost = @vperf_3 * @cost_setvalue else select @cost = @goods_cost end update ggm02 set ggb22f = @vperf_3, ggb38f = @vperf_3 - isnull(@cost, 0) * isnull(@caltperf_3, 0) where ggb00c = @compid and ggb01c = @billid and ggb03c = @item and ggb02f = @ggm02_seq end if (isnull(@staff_4, '') <> '') --add by lm 2015.12.08 begin select @costtype = isnull(@costtype4, 1) select @cost_setvalue = isnull(@cost_setvalue4, 0) select @caltperf_1 = @caltperf4_1 select @caltperf_2 = @caltperf4_2 select @caltperf_3 = @caltperf4_3 select @caltperf_4 = @caltperf4_4 -- 计算业绩 exec upg_cal_all_employees_vperf @compid, @cardno, @item, @quan, @standardprice, @price, @amt, @ex_prj_rate, @ex_prj_rate_points, @perf_rate_prj_gam10, @mode_1, @mode_2, @mode_3, @mode_4, @staff_1, @staff_2, @staff_3, @staff_4, @vperf_1 output, @vperf_2 output, @vperf_3 output, @vperf_4 output, @paymode, @p_mode, @p_setvalue, @p_points output, @fa_type, @fa_type2, @fa_type3, @fa_type4 if @input_project_perf = '1' begin select @vperf_4 = isnull(@input_vperf4, 0) end -- 计算成本 if @costtype = 1 select @cost = @amt * @cost_setvalue -- 金额已经数量 else if @costtype = 2 select @cost = @standardprice * @quan * @cost_setvalue else if @costtype = 3 select @cost = @cost_setvalue * @quan else if @costtype = 4 begin exec upg_sum_goods_cost @compid, @billid, @item, @ggm02_seq, @goods_cost output select @cost = @goods_cost end else if @costtype = 5 begin exec upg_sum_goods_cost @compid, @billid, @item, @ggm02_seq, @goods_cost output if @standardprice * @quan * @cost_setvalue > @goods_cost select @cost = @standardprice * @quan * @cost_setvalue else select @cost = @goods_cost end else if @costtype = 6 begin exec upg_sum_goods_cost @compid, @billid, @item, @ggm02_seq, @goods_cost output if @cost_setvalue * @quan > @goods_cost select @cost = @cost_setvalue * @quan else select @cost = @goods_cost end else if @costtype = 7 begin select @cost = @amt * @cost_setvalue * @ex_prj_rate end else if @costtype = 8 --虚业绩比率 add by lm 2015.11.05 begin select @cost = @vperf_4 * @cost_setvalue end else if @costtype = 9 -- add by lm 2016.04.22 begin exec upg_sum_goods_cost @compid, @billid, @item, @ggm02_seq, @goods_cost output if @vperf_4 * @cost_setvalue > @goods_cost select @cost = @vperf_4 * @cost_setvalue else select @cost = @goods_cost end update ggm02 set ggb35f = @vperf_4, ggb39f = @vperf_4 - isnull(@cost, 0) * isnull(@caltperf_4, 0) where ggb00c = @compid and ggb01c = @billid and ggb03c = @item and ggb02f = @ggm02_seq end -- added by wuxm, 20060619, 增加项目消费提醒的功能 exec upg_insert_notify_content @compid, @billid, @cardno, @staff_1, 1, @item, @ddate end GO /****** Object: StoredProcedure [dbo].[upg_compute_cost_perf_project] Script Date: 06/12/2017 06:19:44 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_compute_cost_perf_project] ( @compid varchar(10), -- 公司别 @billid varchar(20), -- 收银单号 @cardtype varchar(20), -- 会员卡类别, 消费时候的类别 @cardno varchar(20), -- 会员卡号 @ddate varchar(8) -- 账单日期 ) as begin declare @input_project_perf varchar(10) select @input_project_perf = gsa02c from gsm01 with(nolock) where gsa00c = @compid and gsa01c = 'SP076' -- 散客,只要在会员卡类别设定中有Z的类别,实际营业额的折算比率就按Z的会员卡类别设定中 -- 不管支付方式为多少 declare @is_cash int -- 标志是否设置了Z的类别 declare @cash_prj_rate_points float declare @perf_rate_prj_gam10 float -- 业绩折算比率,项目 declare @saved_perf_rate_prj_gam10 float -- 业绩折算比率,项目 -- 散客 if substring(@cardno, 1, 2) = '散客' begin if exists(select 1 from gam10 where gak00c = @compid and gak01c = 'Z') begin select @is_cash = 1 select @cash_prj_rate_points = isnull(gak33f, 1), @perf_rate_prj_gam10 = isnull(gak45f, 1) from gam10 where gak00c = @compid and gak01c = 'Z' select @saved_perf_rate_prj_gam10 = @perf_rate_prj_gam10 end else begin select @is_cash = 0 end end declare @card_kind int -- 主要为了识别是否为是否为资格卡 declare @old_prj_ex_rate_points float -- 项目积分折算比率 select @card_kind = gak03i, @old_prj_ex_rate_points = isnull(gak33f, 1), @perf_rate_prj_gam10 = isnull(gak45f, 1) from gam10 a where a.gak00c = @compid and a.gak01c = @cardtype select @saved_perf_rate_prj_gam10 = @perf_rate_prj_gam10 if isnull(@old_prj_ex_rate_points, -1) = -1 select @old_prj_ex_rate_points = 1 declare @pos_reduce_rate float -- 银行卡支付的时候需要调整的折算比率 select @pos_reduce_rate = gca25f from gcm01 with (nolock) where gca00c = gca13d and gca01c = @cardno select @pos_reduce_rate = isnull(@pos_reduce_rate, 0) declare @old_prj_ex_rate float declare @cash_prj_rate float -- 折算后营业额(也称谓实际营业额), 实际营业额 = 营业额 * 折算率 select @old_prj_ex_rate = isnull(gak22f, 1) - @pos_reduce_rate, @cash_prj_rate = isnull(gak22f, 1) from gam10 where gak00c = @compid and gak01c = @cardtype -- 允许折算率为0存在 -- 虽然ggm01中有卡存在但不一定用卡来支付 -- 所以实际的折算率还应该考虑按支付方式的折扣率 if isnull(@old_prj_ex_rate, -1) = -1 select @old_prj_ex_rate = 1 declare @item varchar(20) -- 项目编码 declare @standardprice float -- 标准价 declare @price float -- 实际价 declare @amt float -- 项目总价 declare @quan float -- 数量 declare @mode_1 varchar(20) declare @mode_2 varchar(20) declare @mode_3 varchar(20) declare @mode_4 varchar(20) declare @staff_1 varchar(20) declare @staff_2 varchar(20) declare @staff_3 varchar(20) declare @staff_4 varchar(20) declare @vperf_1 float declare @vperf_2 float declare @vperf_3 float declare @vperf_4 float -- 大,中,小工的虚业绩 declare @input_vperf1 float declare @input_vperf2 float declare @input_vperf3 float declare @input_vperf4 float -- 输入的大,中,小工项目虚业绩 declare @caltperf_1 int declare @caltperf_2 int declare @caltperf_3 int declare @caltperf_4 int declare @paymode varchar(20) -- 项目的支付方式, 但按单据支付的时候, 没有此值 declare @ex_prj_rate_points float -- 根据卡的类别取得积分的折算比率 declare @p_ex_rate float -- 根据支付方式的不同取得实际营业额的折算比率 declare @p_ex_rate_points float -- 根据支付方式的不同取得积分的折算比率 declare @ex_prj_rate float -- 根据卡的类别取得项目实际营业额的折算比率 declare @p_mode int declare @p_setvalue float declare @p_points float declare @cost float -- 最后计算出来的成本 declare @personidd nvarchar(10) declare @personidd2 nvarchar(10) declare @personidd3 nvarchar(10) declare @personidd4 nvarchar(10) declare @fa_type nvarchar(20) -- 方案类型 declare @fa_type2 nvarchar(20) declare @fa_type3 nvarchar(20) declare @fa_type4 nvarchar(20) declare @cardrate float --转卡比率 declare @rmbrate float --转现比率 declare @costtype int -- 计算成本类型 -- 1:按实际价格的比率,2-按标准价格的比率,3-固定成本 -- 4: 按消耗产品的成本,5: 按成本最高者(标准价格的比率与消耗成本比) declare @cost_setvalue float -- 设置成本的值 declare @goods_cost float -- 消耗产品的成本 declare @ggm02_seq float declare cur_each_service_item cursor for select ggb03c, ggb08f, ggb10f, ggb11f, ggb05f, isnull(ggb15c, '2'), isnull(ggb16c, '2'), isnull(ggb19c, '2'), isnull(ggb25c, '2'), ggb12c, ggb13c, ggb14c, ggb26c, ggb27c, ggb02f, ggb20f, ggb21f, ggb22f, ggb35f from ggm02 where ggb00c = @compid and ggb01c = @billid for update of ggb17f, ggb20f, ggb21f, ggb22f, ggb33f, ggb34f, ggb35f, ggb36f, ggb37f, ggb38f, ggb39f, ggb18f open cur_each_service_item fetch next from cur_each_service_item into @item, @standardprice, @price, @amt, @quan, @mode_1, @mode_2, @mode_3, @mode_4, @staff_1, @staff_2, @staff_3, @staff_4, @paymode, @ggm02_seq, @input_vperf1, @input_vperf2, @input_vperf3, @input_vperf4 while @@fetch_status = 0 begin select @p_ex_rate = 1, @p_ex_rate_points = 1, @vperf_1 = 0, @vperf_2 = 0, @vperf_3 = 0, @vperf_4 = 0 -- 还需要考虑支付方式的不同 select @p_ex_rate = gso03i, @p_ex_rate_points = gso04f from gsm15 where gso00c = @compid and gso02c = @paymode --允许折算率为0存在 if isnull(@p_ex_rate, -1) = -1 select @p_ex_rate = 1 if isnull(@p_ex_rate_points, -1) = -1 select @p_ex_rate_points = 1 if @card_kind = 2 -- 资格卡 begin select @ex_prj_rate_points = @old_prj_ex_rate_points * @p_ex_rate_points select @ex_prj_rate = @old_prj_ex_rate * @p_ex_rate end else begin if @paymode = '4' begin -- 按储值账户支付, 按卡类别的折算*支付方式的折算率 select @ex_prj_rate_points = @old_prj_ex_rate_points * @p_ex_rate_points select @ex_prj_rate = @old_prj_ex_rate * @p_ex_rate select @perf_rate_prj_gam10 = @saved_perf_rate_prj_gam10 end else begin -- 按其他账户支付或其他支付方式 select @ex_prj_rate_points = @p_ex_rate_points select @ex_prj_rate = @p_ex_rate select @perf_rate_prj_gam10 = 1 end -- 如果设定了Z(现金的实际营业额比率) if @is_cash = 1 begin -- Z的折扣率*支付方式设置的折扣率 select @ex_prj_rate_points = @cash_prj_rate_points * @p_ex_rate_points if isnull(@cash_prj_rate, -1) = -1 select @cash_prj_rate = 1 select @ex_prj_rate = @cash_prj_rate * @p_ex_rate end end -- 取得项目的积分设定 select @p_mode = gda17i, @p_setvalue = gda18f from gdm01 where gda00c = @compid and gda01c = @item -- 得到提成方案 -- 获取在账单有效日期内最新一条员工提成方案 set @personidd = isnull(@staff_1, '') select top 1 @fa_type = ppt02c from personprecept where ppt00c = @compid and ppt03c = @personidd and ppt04i = 1 and ppt02c in (select pre01c from precept where pre00c = @compid and isnull(pre22i, 0) = 0) order by ppt05d desc set @personidd2 = isnull(@staff_2, '') select top 1 @fa_type2 = ppt02c from personprecept where ppt00c = @compid and ppt03c = @personidd2 and ppt04i = 1 and ppt02c in (select pre01c from precept where pre00c = @compid and isnull(pre22i, 0) = 0) order by ppt05d desc set @personidd3 = isnull(@staff_3, '') select top 1 @fa_type3 = ppt02c from personprecept where ppt00c = @compid and ppt03c = @personidd3 and ppt04i = 1 and ppt02c in (select pre01c from precept where pre00c = @compid and isnull(pre22i, 0) = 0) order by ppt05d desc set @personidd4 = isnull(@staff_4, '') select top 1 @fa_type4 = ppt02c from personprecept where ppt00c = @compid and ppt03c = @personidd4 and ppt04i = 1 and ppt02c in (select pre01c from precept where pre00c = @compid and isnull(pre22i, 0) = 0) order by ppt05d desc -- 取得不同会员卡类别不同项目类别设置的实际营业额折扣率 declare @cardtype_itemtype_rate float exec upg_get_cardtype_itemtype_rate @compid, @cardtype, @item, @paymode, '1', @cardtype_itemtype_rate output select @ex_prj_rate = isnull(@ex_prj_rate, 1) * isnull(@cardtype_itemtype_rate, 1) -- 转卡比率与转现金比率 if (@personidd <> '') --modify by lm 2015.12.08 begin select @cardrate = hea13f, @rmbrate = hea24f from hem01 with (nolock) where hea00c = @compid and hea01c = @item and hea33c = @fa_type and hea18i = 2 and hea34c = @paymode if(isnull(@cardrate,'') = '') begin select @cardrate = hea13f, @rmbrate = hea24f from hem01 with (nolock) where hea00c = @compid and hea01c = @item and hea33c = @fa_type and hea18i = 2 and isnull(hea34c, '*') = '*' end end else if (@personidd2 <> '') begin select @cardrate = hea13f, @rmbrate = hea24f from hem01 with (nolock) where hea00c = @compid and hea01c = @item and hea33c = @fa_type2 and hea18i = 2 and hea34c = @paymode if(isnull(@cardrate,'') = '') begin select @cardrate = hea13f, @rmbrate = hea24f from hem01 with (nolock) where hea00c = @compid and hea01c = @item and hea33c = @fa_type2 and hea18i = 2 and isnull(hea34c, '*') = '*' end end else if (@personidd3 <> '') begin select @cardrate = hea13f, @rmbrate = hea24f from hem01 with (nolock) where hea00c = @compid and hea01c = @item and hea33c = @fa_type3 and hea18i = 2 and hea34c = @paymode if(isnull(@cardrate,'') = '') begin select @cardrate = hea13f, @rmbrate = hea24f from hem01 with (nolock) where hea00c = @compid and hea01c = @item and hea33c = @fa_type3 and hea18i = 2 and isnull(hea34c, '*') = '*' end end else if (@personidd4 <> '') begin select @cardrate = hea13f, @rmbrate = hea24f from hem01 with (nolock) where hea00c = @compid and hea01c = @item and hea33c = @fa_type4 and hea18i = 2 and hea34c = @paymode if(isnull(@cardrate,'') = '') begin select @cardrate = hea13f, @rmbrate = hea24f from hem01 with (nolock) where hea00c = @compid and hea01c = @item and hea33c = @fa_type4 and hea18i = 2 and isnull(hea34c, '*') = '*' end end update ggm02 set ggb18f = null where current of cur_each_service_item if (@paymode = '#' or @paymode = '4' or @paymode = ''or @paymode = '9' or (@paymode >= 'A' and @paymode <= 'Z')) begin update ggm02 set ggb18f = @cardrate where current of cur_each_service_item end else if (@paymode = '1' or @paymode = '6') begin update ggm02 set ggb18f = @rmbrate where current of cur_each_service_item end --计算积分 exec upg_cal_all_employees_vperf @compid, @cardno, @item, @quan, @standardprice, @price, @amt, @ex_prj_rate, @ex_prj_rate_points, @perf_rate_prj_gam10, @mode_1, @mode_2, @mode_3, @mode_4, @personidd, '', '', '', @vperf_1 output, @vperf_2 output, @vperf_3 output, @vperf_4 output, @paymode, @p_mode, @p_setvalue, @p_points output, @fa_type, @fa_type2, @fa_type3, @fa_type4 -- ggb17f、ggb33f、ggb34f以第一销售为标准 --add by lm 2015.12.08 select @cardrate = hea13f, @rmbrate = hea24f from hem01 with (nolock) where hea00c = @compid and hea01c = @item and hea33c = @fa_type and hea18i = 2 and hea34c = @paymode if(isnull(@cardrate,'') = '') begin select @cardrate = hea13f, @rmbrate = hea24f from hem01 with (nolock) where hea00c = @compid and hea01c = @item and hea33c = @fa_type and hea18i = 2 and isnull(hea34c, '*') = '*' end update ggm02 set ggb17f = @cost, ggb33f = @p_points, ggb34f = @ex_prj_rate where current of cur_each_service_item -- 重置成本计算相关的 select @costtype = '',@cost_setvalue=0,@caltperf_1 = 0,@cost_setvalue =1, @caltperf_2 =0 ,@caltperf_3 = 0, @caltperf_4 = 0 ,@cost = 0 if (isnull(@staff_1, '') <> '') --add by lm 2015.12.08 begin select @costtype = hea19i, @cost_setvalue = hea12f, @caltperf_1 = isnull(hea29i, 1), @caltperf_2 = isnull(hea30i, 1), @caltperf_3 = isnull(hea31i, 1), @caltperf_4 = isnull(hea32i, 1) from hem01 with (nolock) where hea00c = @compid and hea01c = @item and hea18i = 2 and hea33c = @fa_type and hea34c = @paymode and isnull(hea29i,0) <> 0 if(isnull(@costtype,'') = '') begin select @costtype = hea19i, @cost_setvalue = hea12f, @caltperf_1 = isnull(hea29i, 1), @caltperf_2 = isnull(hea30i, 1), @caltperf_3 = isnull(hea31i, 1), @caltperf_4 = isnull(hea32i, 1) from hem01 with (nolock) where hea00c = @compid and hea01c = @item and hea18i = 2 and hea33c = @fa_type and isnull(hea34c, '*') = '*' and isnull(hea29i,0) <> 0 end select @costtype = isnull(@costtype, 1) select @cost_setvalue = isnull(@cost_setvalue, 0) -- 计算业绩 exec upg_cal_all_employees_vperf @compid, @cardno, @item, @quan, @standardprice, @price, @amt, @ex_prj_rate, @ex_prj_rate_points, @perf_rate_prj_gam10, @mode_1, @mode_2, @mode_3, @mode_4, @staff_1, @staff_2, @staff_3, @staff_4, @vperf_1 output, @vperf_2 output, @vperf_3 output, @vperf_4 output, @paymode, @p_mode, @p_setvalue, @p_points output, @fa_type, @fa_type2, @fa_type3, @fa_type4 if @input_project_perf = '1' begin select @vperf_1 = isnull(@input_vperf1, 0) end -- 计算成本 if @costtype = 1 select @cost = @amt * @cost_setvalue -- 金额已经数量 else if @costtype = 2 select @cost = @standardprice * @quan * @cost_setvalue else if @costtype = 3 select @cost = @cost_setvalue * @quan else if @costtype = 4 begin exec upg_sum_goods_cost @compid, @billid, @item, @ggm02_seq, @goods_cost output select @cost = @goods_cost end else if @costtype = 5 begin exec upg_sum_goods_cost @compid, @billid, @item, @ggm02_seq, @goods_cost output if @standardprice * @quan * @cost_setvalue > @goods_cost select @cost = @standardprice * @quan * @cost_setvalue else select @cost = @goods_cost end else if @costtype = 6 begin exec upg_sum_goods_cost @compid, @billid, @item, @ggm02_seq, @goods_cost output if @cost_setvalue * @quan > @goods_cost select @cost = @cost_setvalue * @quan else select @cost = @goods_cost end else if @costtype = 7 begin select @cost = @amt * @cost_setvalue * @ex_prj_rate end else if @costtype = 8 --虚业绩比率 add by lm 2015.11.05 begin select @cost = @vperf_1 * @cost_setvalue end else if @costtype = 9 -- add by lm 2016.04.22 begin exec upg_sum_goods_cost @compid, @billid, @item, @ggm02_seq, @goods_cost output if @vperf_1 * @cost_setvalue > @goods_cost select @cost = @vperf_1 * @cost_setvalue else select @cost = @goods_cost end update ggm02 set ggb20f = @vperf_1, ggb36f = @vperf_1 - isnull(@cost, 0) * isnull(@caltperf_1, 0) where current of cur_each_service_item end if (isnull(@staff_2, '') <> '') --add by lm 2015.12.08 begin select @costtype = hea19i, @cost_setvalue = hea12f, @caltperf_1 = isnull(hea29i, 1), @caltperf_2 = isnull(hea30i, 1), @caltperf_3 = isnull(hea31i, 1), @caltperf_4 = isnull(hea32i, 1) from hem01 with (nolock) where hea00c = @compid and hea01c = @item and hea18i = 2 and hea33c = @fa_type2 and hea34c = @paymode and isnull(hea30i,0) <> 0 if(isnull(@costtype,'') = '') begin select @costtype = hea19i, @cost_setvalue = hea12f, @caltperf_1 = isnull(hea29i, 1), @caltperf_2 = isnull(hea30i, 1), @caltperf_3 = isnull(hea31i, 1), @caltperf_4 = isnull(hea32i, 1) from hem01 with (nolock) where hea00c = @compid and hea01c = @item and hea18i = 2 and hea33c = @fa_type2 and isnull(hea34c, '*') = '*' and isnull(hea30i,0) <> 0 end select @costtype = isnull(@costtype, 1) select @cost_setvalue = isnull(@cost_setvalue, 0) -- 计算业绩 exec upg_cal_all_employees_vperf @compid, @cardno, @item, @quan, @standardprice, @price, @amt, @ex_prj_rate, @ex_prj_rate_points, @perf_rate_prj_gam10, @mode_1, @mode_2, @mode_3, @mode_4, @staff_1, @staff_2, @staff_3, @staff_4, @vperf_1 output, @vperf_2 output, @vperf_3 output, @vperf_4 output, @paymode, @p_mode, @p_setvalue, @p_points output, @fa_type, @fa_type2, @fa_type3, @fa_type4 if @input_project_perf = '1' begin select @vperf_2 = isnull(@input_vperf2,0) end -- 计算成本 if @costtype = 1 select @cost = @amt * @cost_setvalue -- 金额已经数量 else if @costtype = 2 select @cost = @standardprice * @quan * @cost_setvalue else if @costtype = 3 select @cost = @cost_setvalue * @quan else if @costtype = 4 begin exec upg_sum_goods_cost @compid, @billid, @item, @ggm02_seq, @goods_cost output select @cost = @goods_cost end else if @costtype = 5 begin exec upg_sum_goods_cost @compid, @billid, @item, @ggm02_seq, @goods_cost output if @standardprice * @quan * @cost_setvalue > @goods_cost select @cost = @standardprice * @quan * @cost_setvalue else select @cost = @goods_cost end else if @costtype = 6 begin exec upg_sum_goods_cost @compid, @billid, @item, @ggm02_seq, @goods_cost output if @cost_setvalue * @quan > @goods_cost select @cost = @cost_setvalue * @quan else select @cost = @goods_cost end else if @costtype = 7 begin select @cost = @amt * @cost_setvalue * @ex_prj_rate end else if @costtype = 8 --虚业绩比率 add by lm 2015.11.05 begin select @cost = @vperf_2 * @cost_setvalue end else if @costtype = 9 -- add by lm 2016.04.22 begin exec upg_sum_goods_cost @compid, @billid, @item, @ggm02_seq, @goods_cost output if @vperf_2 * @cost_setvalue > @goods_cost select @cost = @vperf_2 * @cost_setvalue else select @cost = @goods_cost end update ggm02 set ggb21f = @vperf_2, ggb37f = @vperf_2 - isnull(@cost, 0) * isnull(@caltperf_2, 0) where current of cur_each_service_item end if (isnull(@staff_3, '') <> '') --add by lm 2015.12.08 begin select @costtype = hea19i, @cost_setvalue = hea12f, @caltperf_1 = isnull(hea29i, 1), @caltperf_2 = isnull(hea30i, 1), @caltperf_3 = isnull(hea31i, 1), @caltperf_4 = isnull(hea32i, 1) from hem01 with (nolock) where hea00c = @compid and hea01c = @item and hea18i = 2 and hea33c = @fa_type3 and hea34c = @paymode and isnull(hea31i,0) <> 0 if(isnull(@costtype,'') = '') begin select @costtype = hea19i, @cost_setvalue = hea12f, @caltperf_1 = isnull(hea29i, 1), @caltperf_2 = isnull(hea30i, 1), @caltperf_3 = isnull(hea31i, 1), @caltperf_4 = isnull(hea32i, 1) from hem01 with (nolock) where hea00c = @compid and hea01c = @item and hea18i = 2 and hea33c = @fa_type3 and isnull(hea34c, '*') = '*' and isnull(hea31i,0) <> 0 end select @costtype = isnull(@costtype, 1) select @cost_setvalue = isnull(@cost_setvalue, 0) -- 计算业绩 exec upg_cal_all_employees_vperf @compid, @cardno, @item, @quan, @standardprice, @price, @amt, @ex_prj_rate, @ex_prj_rate_points, @perf_rate_prj_gam10, @mode_1, @mode_2, @mode_3, @mode_4, @staff_1, @staff_2, @staff_3, @staff_4, @vperf_1 output, @vperf_2 output, @vperf_3 output, @vperf_4 output, @paymode, @p_mode, @p_setvalue, @p_points output, @fa_type, @fa_type2, @fa_type3, @fa_type4 if @input_project_perf = '1' begin select @vperf_3 = isnull(@input_vperf3,0) end -- 计算成本 if @costtype = 1 select @cost = @amt * @cost_setvalue -- 金额已经数量 else if @costtype = 2 select @cost = @standardprice * @quan * @cost_setvalue else if @costtype = 3 select @cost = @cost_setvalue * @quan else if @costtype = 4 begin exec upg_sum_goods_cost @compid, @billid, @item, @ggm02_seq, @goods_cost output select @cost = @goods_cost end else if @costtype = 5 begin exec upg_sum_goods_cost @compid, @billid, @item, @ggm02_seq, @goods_cost output if @standardprice * @quan * @cost_setvalue > @goods_cost select @cost = @standardprice * @quan * @cost_setvalue else select @cost = @goods_cost end else if @costtype = 6 begin exec upg_sum_goods_cost @compid, @billid, @item, @ggm02_seq, @goods_cost output if @cost_setvalue * @quan > @goods_cost select @cost = @cost_setvalue * @quan else select @cost = @goods_cost end else if @costtype = 7 begin select @cost = @amt * @cost_setvalue * @ex_prj_rate end else if @costtype = 8 --虚业绩比率 add by lm 2015.11.05 begin select @cost = @vperf_3 * @cost_setvalue end else if @costtype = 9 -- add by lm 2016.04.22 begin exec upg_sum_goods_cost @compid, @billid, @item, @ggm02_seq, @goods_cost output if @vperf_3 * @cost_setvalue > @goods_cost select @cost = @vperf_3 * @cost_setvalue else select @cost = @goods_cost end update ggm02 set ggb22f = @vperf_3, ggb38f = @vperf_3 - isnull(@cost, 0) * isnull(@caltperf_3, 0) where current of cur_each_service_item end if (isnull(@staff_4, '') <> '') --add by lm 2015.12.08 begin select @costtype = hea19i, @cost_setvalue = hea12f, @caltperf_1 = isnull(hea29i, 1), @caltperf_2 = isnull(hea30i, 1), @caltperf_3 = isnull(hea31i, 1), @caltperf_4 = isnull(hea32i, 1) from hem01 with (nolock) where hea00c = @compid and hea01c = @item and hea18i = 2 and hea33c = @fa_type4 and hea34c = @paymode and isnull(hea32i,0) <> 0 if(isnull(@costtype,'') = '') begin select @costtype = hea19i, @cost_setvalue = hea12f, @caltperf_1 = isnull(hea29i, 1), @caltperf_2 = isnull(hea30i, 1), @caltperf_3 = isnull(hea31i, 1), @caltperf_4 = isnull(hea32i, 1) from hem01 with (nolock) where hea00c = @compid and hea01c = @item and hea18i = 2 and hea33c = @fa_type4 and isnull(hea34c, '*') = '*' and isnull(hea32i,0) <> 0 end select @costtype = isnull(@costtype, 1) select @cost_setvalue = isnull(@cost_setvalue, 0) -- 计算业绩 exec upg_cal_all_employees_vperf @compid, @cardno, @item, @quan, @standardprice, @price, @amt, @ex_prj_rate, @ex_prj_rate_points, @perf_rate_prj_gam10, @mode_1, @mode_2, @mode_3, @mode_4, @staff_1, @staff_2, @staff_3, @staff_4, @vperf_1 output, @vperf_2 output, @vperf_3 output, @vperf_4 output, @paymode, @p_mode, @p_setvalue, @p_points output, @fa_type, @fa_type2, @fa_type3, @fa_type4 if @input_project_perf = '1' begin select @vperf_4 = isnull(@input_vperf4, 0) end -- 计算成本 if @costtype = 1 select @cost = @amt * @cost_setvalue -- 金额已经数量 else if @costtype = 2 select @cost = @standardprice * @quan * @cost_setvalue else if @costtype = 3 select @cost = @cost_setvalue * @quan else if @costtype = 4 begin exec upg_sum_goods_cost @compid, @billid, @item, @ggm02_seq, @goods_cost output select @cost = @goods_cost end else if @costtype = 5 begin exec upg_sum_goods_cost @compid, @billid, @item, @ggm02_seq, @goods_cost output if @standardprice * @quan * @cost_setvalue > @goods_cost select @cost = @standardprice * @quan * @cost_setvalue else select @cost = @goods_cost end else if @costtype = 6 begin exec upg_sum_goods_cost @compid, @billid, @item, @ggm02_seq, @goods_cost output if @cost_setvalue * @quan > @goods_cost select @cost = @cost_setvalue * @quan else select @cost = @goods_cost end else if @costtype = 7 begin select @cost = @amt * @cost_setvalue * @ex_prj_rate end else if @costtype = 8 --虚业绩比率 add by lm 2015.11.05 begin select @cost = @vperf_4 * @cost_setvalue end else if @costtype = 9 -- add by lm 2016.04.22 begin exec upg_sum_goods_cost @compid, @billid, @item, @ggm02_seq, @goods_cost output if @vperf_4 * @cost_setvalue > @goods_cost select @cost = @vperf_4 * @cost_setvalue else select @cost = @goods_cost end update ggm02 set ggb35f = @vperf_4, ggb39f = @vperf_4 - isnull(@cost, 0) * isnull(@caltperf_4, 0) where current of cur_each_service_item end -- added by wuxm, 20060619, 增加项目消费提醒的功能 exec upg_insert_notify_content @compid, @billid, @cardno, @staff_1, 1, @item, @ddate fetch next from cur_each_service_item into @item, @standardprice, @price, @amt, @quan, @mode_1, @mode_2, @mode_3, @mode_4, @staff_1, @staff_2, @staff_3, @staff_4, @paymode, @ggm02_seq, @input_vperf1, @input_vperf2, @input_vperf3, @input_vperf4 end close cur_each_service_item deallocate cur_each_service_item end GO /****** Object: StoredProcedure [dbo].[upg_all_profit_analysis] Script Date: 06/12/2017 06:19:25 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_all_profit_analysis] ( @compOur varchar(10), --本店 @compfrom varchar(10), @compto varchar(10), @fromdate varchar(10), @todate varchar(10), @calmode int ) as --WUXM 2006/02/07 begin declare @amt float declare @tmpamt float -- 增加劳动业绩的计算 -- 店类型 1美容2美发3SPA4足浴6专柜 declare @sp056 varchar(20) -- 0-按项目支付1-按单据支付 declare @sp055f float declare @isadd_0 float declare @isadd_3 float declare @isadd_$ float -- 定义劳动业绩的比率 declare @cash_rate float -- 现金:1 declare @cash_good_rate float -- 现金:1 declare @credit_rate float -- 银行卡支付:6 declare @credit_good_rate float -- 银行卡支付:6 declare @pay3_rate float -- 支付方式:3 declare @pay3_good_rate float -- 支付方式:3 declare @pay$_rate float -- 支付方式:$ declare @pay$_good_rate float -- 支付方式:$ declare @pay0_rate float -- 内调:0 declare @pay0_good_rate float -- 内调:0 declare @manager_rate float -- 经理签单:8 declare @manager_good_rate float -- 经理签单:8 declare @point_rate float -- 积分支付:7 declare @point_good_rate float -- 积分支付:7 declare @user01_rate float -- 自定义支付:$1 declare @user01_good_rate float -- 自定义支付:$1 declare @user02_rate float -- 自定义支付:$2 declare @user02_good_rate float -- 自定义支付:$2 declare @user03_rate float -- 自定义支付:$3 declare @user03_good_rate float -- 自定义支付:$3 declare @user04_rate float -- 自定义支付:$4 declare @user04_good_rate float -- 自定义支付:$4 declare @user05_rate float -- 自定义支付:$5 declare @user05_good_rate float -- 自定义支付:$5 declare @user06_rate float -- 自定义支付:$6 declare @user06_good_rate float -- 自定义支付:$6 declare @user07_rate float -- 自定义支付:$7 declare @user07_good_rate float -- 自定义支付:$7 declare @user08_rate float -- 自定义支付:$8 declare @user08_good_rate float -- 自定义支付:$8 declare @user09_rate float -- 自定义支付:$9 declare @user09_good_rate float -- 自定义支付:$9 declare @user10_rate float -- 自定义支付:$10 declare @user10_good_rate float -- 自定义支付:$10 declare @user11_rate float -- 自定义支付:$11 declare @user11_good_rate float -- 自定义支付:$11 declare @user12_rate float -- 自定义支付:$12 declare @user12_good_rate float -- 自定义支付:$12 declare @user13_rate float -- 自定义支付:$13 declare @user13_good_rate float -- 自定义支付:$13 declare @user14_rate float -- 自定义支付:$14 declare @user14_good_rate float -- 自定义支付:$14 declare @user15_rate float -- 自定义支付:$15 declare @user15_good_rate float -- 自定义支付:$15 declare @zfb_rate float -- 支付宝支付 declare @zfb_good_rate float -- 支付宝支付 declare @wx_rate float -- 微信支付 declare @wx_good_rate float -- 微信支付 declare @guestflux float -- 客流数 declare @guestcount float -- 客数 declare @guestprice float -- 客单价 create table #tbl_profit_day ( idd int not null, code varchar(20) not null, itemname varchar(60) null, is_add int null, amt float null, is_leaf int null, payment varchar(10) null, -- 支付方式 kind int null -- 0 充值 1卡销售 9产品销售 2 服务 -1 取款 8 保证金收入 6 还款 ) create table #stat_byday ( comp varchar(80) not null, cash_card float null, cash_srv float null, cash_good float null, cash_total float null, credit_card float null, credit_srv float null, credit_good float null, credit_total float null, zfb_card float null, zfb_srv float null, zfb_good float null, zfb_total float null, wx_card float null, wx_srv float null, wx_good float null, wx_total float null, revene float null, outcome float null, profit float null, defict float null, returnamt float null, pay_card_srv float null, pay_card_good float null, pay_coupon float null, -- 券付 pay_coupon2 float null, -- 贵付 pay_coupon3 float null, -- 洗头 内调 pay_manager float null, -- 经理签单 sale_card_total float null, -- 总销售卡 pay_card_srv_act float null, -- 卡付实际营业额 = 在服务项目 + 产品中,那些卡付的金额转换为实际营业额 pay_card_srv_work float null, -- 卡付实际劳动业绩 = 在服务项目 + 产品中,那些卡付的金额转换为劳动业绩 work_perf float null, -- 劳动业绩 = 现金(服务) + 现金(物品) + 卡付(物品) + 卡付实营业额 + 项目或物品经理签单 + 内调 + 抵用券 + 银行卡(服务) + 银行卡(物品) pay_manager_prj float null, --只包括项目的经理签单 pay_coupon_srv float null, -- 券付 支付方式3, 只包括项目(含产品) pay_coupon2_srv float null, -- 贵付 支付方式$, 只包括项目(含产品) pay_coupon3_srv float null, -- 洗头 支付方式内调, 只包括项目(含产品) pay_manager_good float null, --只包括产品的经理签单 --modify by limin -2014-12-23 pay_coupon_good float null, -- 券付 支付方式3, 只包括产品 --modify by limin -2014-12-23 pay_coupon2_good float null, -- 贵付 支付方式$, 只包括产品 --modify by limin -2014-12-23 pay_coupon3_good float null, -- 洗头 支付方式内调, 只包括产品 --modify by limin -2014-12-23 pay_point float null, -- 积分支付,卡销售,项目,产品 pay_point_srv float null, -- 积分支付,只包括项目(含产品) pay_point_good float null, -- 积分支付,只有产品 --modify by limin -2014-12-23 pay_user01 float null, -- 自定义支付,卡销售,项目,产品 pay_user01_srv float null, -- 自定义支付,只包括项目(含产品) pay_user01_good float null, -- 自定义支付,只有产品 pay_user02 float null, -- 自定义支付,卡销售,项目,产品 pay_user02_srv float null, -- 自定义支付,只包括项目(含产品) pay_user02_good float null, -- 自定义支付,只有产品 pay_user03 float null, -- 自定义支付,卡销售,项目,产品 pay_user03_srv float null, -- 自定义支付,只包括项目(含产品) pay_user03_good float null, -- 自定义支付,只有产品 pay_user04 float null, -- 自定义支付,卡销售,项目,产品 pay_user04_srv float null, -- 自定义支付,只包括项目(含产品) pay_user04_good float null, -- 自定义支付,只有产品 pay_user05 float null, -- 自定义支付,卡销售,项目,产品 pay_user05_srv float null, -- 自定义支付,只包括项目(含产品) pay_user05_good float null, -- 自定义支付,只有产品 pay_user06 float null, -- 自定义支付,卡销售,项目,产品 pay_user06_srv float null, -- 自定义支付,只包括项目(含产品) pay_user06_good float null, -- 自定义支付,只有产品 pay_user07 float null, -- 自定义支付,卡销售,项目,产品 pay_user07_srv float null, -- 自定义支付,只包括项目(含产品) pay_user07_good float null, -- 自定义支付,只有产品 pay_user08 float null, -- 自定义支付,卡销售,项目,产品 pay_user08_srv float null, -- 自定义支付,只包括项目(含产品) pay_user08_good float null, -- 自定义支付,只有产品 pay_user09 float null, -- 自定义支付,卡销售,项目,产品 pay_user09_srv float null, -- 自定义支付,只包括项目(含产品) pay_user09_good float null, -- 自定义支付,只有产品 pay_user10 float null, -- 自定义支付,卡销售,项目,产品 pay_user10_srv float null, -- 自定义支付,只包括项目(含产品) pay_user10_good float null, -- 自定义支付,只有产品 pay_user11 float null, -- 自定义支付,卡销售,项目,产品 pay_user11_srv float null, -- 自定义支付,只包括项目(含产品) pay_user11_good float null, -- 自定义支付,只有产品 pay_user12 float null, -- 自定义支付,卡销售,项目,产品 pay_user12_srv float null, -- 自定义支付,只包括项目(含产品) pay_user12_good float null, -- 自定义支付,只有产品 pay_user13 float null, -- 自定义支付,卡销售,项目,产品 pay_user13_srv float null, -- 自定义支付,只包括项目(含产品) pay_user13_good float null, -- 自定义支付,只有产品 pay_user14 float null, -- 自定义支付,卡销售,项目,产品 pay_user14_srv float null, -- 自定义支付,只包括项目(含产品) pay_user14_good float null, -- 自定义支付,只有产品 pay_user15 float null, -- 自定义支付,卡销售,项目,产品 pay_user15_srv float null, -- 自定义支付,只包括项目(含产品) pay_user15_good float null, -- 自定义支付,只有产品 other_income float null, -- 其他收入 Huzy 2012/06/04 good_total float null, -- 产品总销售 guestflux float null, guestcount float null, guestprice float null, primary key(comp) ) declare @id int declare @comp varchar(10) -- 总店 if (isnull(@compfrom, '') = '*' or isnull(@compfrom, '') = '') begin declare cur_get_compid cursor for select gae01c from gam05 with(nolock) open cur_get_compid fetch cur_get_compid into @comp while @@fetch_status = 0 begin select @isadd_0 = gso07f from gsm15 with (nolock) where gso00c = @comp and gso02c = '0' select @isadd_0 = isnull(@isadd_0, 0) select @isadd_3 = gso07f from gsm15 with (nolock) where gso00c = @comp and gso02c = '3' select @isadd_3 = isnull(@isadd_3, 0) select @isadd_$ = gso07f from gsm15 with (nolock) where gso00c = @comp and gso02c = '$' select @isadd_$ = isnull(@isadd_$, 0) truncate table #tbl_profit_day insert #tbl_profit_day(idd, code, itemname, is_add, amt, is_leaf, payment, kind) exec upg_analyse_profit @comp, @fromdate, @todate, 1, '*', @calmode insert #stat_byday(comp) values(@comp) -- 转换为汇总表需要的一条记录 -- 现金(卖卡) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110101' or code = '110104' or code = '110107' or code = '110118' or code = '110140' -- 现金取款 select @tmpamt = 0 select @tmpamt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110119' update a set a.cash_card = isnull(@amt, 0) + isnull(@tmpamt, 0) from #stat_byday a where comp = @comp -- 现金(服务) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110201' or code = '110230' -- 项目或物品的现金还款也算 还款的不算在 select @tmpamt = 0 select @tmpamt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110301' or code = '110310' -- 现金的产品销售 以及 产品还款 update a set a.cash_srv = isnull(@amt, 0) - isnull(@tmpamt, 0) from #stat_byday a where comp = @comp -- 经理签单,只包括项目 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110203' update a set a.pay_manager_prj = isnull(@amt, 0) from #stat_byday a where comp = @comp -- 卡付实际营业额 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110206' update a set a.pay_card_srv_act = isnull(@amt, 0) from #stat_byday a where comp = @comp select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '11AA01' update a set a.pay_card_srv_work = isnull(@amt, 0) from #stat_byday a where comp = @comp -- 银行卡(卖卡) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110102' or code = '110105' or code = '110108' or code = '110141' update a set a.credit_card = isnull(@amt, 0) from #stat_byday a where comp = @comp -- 支付宝(卖卡) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '1101Z1' or code = '1101Z2' or code = '1101Z3' update a set a.zfb_card = isnull(@amt, 0) from #stat_byday a where comp = @comp -- 微信(卖卡) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '1101W1' or code = '1101W2' or code = '1101W3' update a set a.wx_card = isnull(@amt, 0) from #stat_byday a where comp = @comp -- 不管支付方式,实际上的卡销售额 select @amt = 0 select @amt = sum(isnull(amt, 0) * is_add) from #tbl_profit_day where code = '110101' or code = '110102' or code = '110104' or code = '110105' or code = '110107' or code = '110108' or code = '110115' or code = '110116' or code = '110117' or code = '110119' or code = '110120' or code = '110121' or code = '110122' or code = '110123' or code = '110125' or code = '110126' or code = '110127' or code = '110130' or code = '110131' or code = '110132' or code = '1101Z1' or code = '1101Z2' or code = '1101Z3' -- 最后2行是新增加的条件,20061130 update a set a.sale_card_total = isnull(@amt, 0) from #stat_byday a where comp = @comp -- 银行卡(服务) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110202' or code = '110231' --项目或物品的银行卡还款不算 select @tmpamt = 0 select @tmpamt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110306' or code = '110311' -- 产品部分的银行卡支付 以及 银行卡还款 update a set a.credit_srv = isnull(@amt, 0) - isnull(@tmpamt, 0) from #stat_byday a where comp = @comp -- 支付宝(服务) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '1102Z1' select @tmpamt = 0 select @tmpamt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '1103Z1' update a set a.zfb_srv = isnull(@amt, 0) - isnull(@tmpamt, 0) from #stat_byday a where comp = @comp -- 微信(服务) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '1102W1' select @tmpamt = 0 select @tmpamt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '1103W1' update a set a.wx_srv = isnull(@amt, 0) - isnull(@tmpamt, 0) from #stat_byday a where comp = @comp -- 其他收入 Huzy 2012/06/04 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '18' update a set a.other_income = isnull(@amt, 0) from #stat_byday a -- 支出 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '12' -- 房租不考虑在内 select @tmpamt = 0 select @tmpamt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '1299' update a set a.outcome = isnull(@amt, 0) - isnull(@tmpamt, 0) from #stat_byday a where comp = @comp -- 欠款 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110103' or code = '110106' or code = '110207' update a set a.defict = isnull(@amt, 0) from #stat_byday a where comp = @comp -- 还款 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110107' or code = '110108' update a set a.returnamt = isnull(@amt, 0) from #stat_byday a where comp = @comp -- 卡付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110205' select @tmpamt = 0 select @tmpamt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110302' update a set a.pay_card_srv = isnull(@amt, 0) - isnull(@tmpamt, 0) from #stat_byday a where comp = @comp -- 积分支付, 买项目或产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '11020507' -- 积分支付 update a set a.pay_point_srv = isnull(@amt, 0) from #stat_byday a where comp = @comp -- 积分支付, 卡充值,买项目或产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '11020507' or code = '110133' -- 积分支付 update a set a.pay_point = isnull(@amt, 0) from #stat_byday a where comp = @comp -- 抵用券付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110204' or code = '110125' or code = '110126' or code = '110127') update a set a.pay_coupon = isnull(@amt, 0) from #stat_byday a where comp = @comp -- 抵用券付, 只包括项目 + 产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110204' update a set a.pay_coupon_srv = isnull(@amt, 0) from #stat_byday a where comp = @comp -- 贵付(抵用券二) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110208' or code = '110130' or code = '110131' or code = '110132') update a set a.pay_coupon2 = isnull(@amt, 0) from #stat_byday a where comp = @comp -- 贵付(抵用券二), 只包括项目 + 产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110208' update a set a.pay_coupon2_srv = isnull(@amt, 0) from #stat_byday a where comp = @comp -- 内调(洗头) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110209' or code = '110120' or code = '110121' or code = '110122') update a set a.pay_coupon3 = isnull(@amt, 0) from #stat_byday a where comp = @comp -- 内调(洗头), 只包括项目 + 产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110209' update a set a.pay_coupon3_srv = isnull(@amt, 0) from #stat_byday a where comp = @comp -- 经理签单 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110110' or code = '110111' or code = '110203') update a set a.pay_manager = isnull(@amt, 0) from #stat_byday a where comp = @comp ----------------------------------------------$1 -> $8----------------------------------------------------------------------------- --自定义支付, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$1' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8) update a set a.pay_user01 = isnull(@amt, 0) from #stat_byday a where comp = @comp --自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$1' and (kind = 2) update a set a.pay_user01_srv = isnull(@amt, 0) from #stat_byday a where comp = @comp --自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$1' and (kind = 9) update a set a.pay_user01_good = isnull(@amt, 0) from #stat_byday a where comp = @comp --自定义支付, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$2' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8) update a set a.pay_user02 = isnull(@amt, 0) from #stat_byday a where comp = @comp --自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$2' and (kind = 2) update a set a.pay_user02_srv = isnull(@amt, 0) from #stat_byday a where comp = @comp --自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$2' and (kind = 9) update a set a.pay_user02_good = isnull(@amt, 0) from #stat_byday a where comp = @comp --自定义支付, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$3' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8 or kind = -1) update a set a.pay_user03 = isnull(@amt, 0) from #stat_byday a where comp = @comp --自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$3' and (kind = 2) update a set a.pay_user03_srv = isnull(@amt, 0) from #stat_byday a where comp = @comp --自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$3' and (kind = 9) update a set a.pay_user03_good = isnull(@amt, 0) from #stat_byday a where comp = @comp --自定义支付, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$4' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8) update a set a.pay_user04 = isnull(@amt, 0) from #stat_byday a where comp = @comp --自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$4' and (kind = 2) update a set a.pay_user04_srv = isnull(@amt, 0) from #stat_byday a where comp = @comp --自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$4' and (kind = 9) update a set a.pay_user04_good = isnull(@amt, 0) from #stat_byday a where comp = @comp --自定义支付, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$5' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8) update a set a.pay_user05 = isnull(@amt, 0) from #stat_byday a where comp = @comp --自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$5' and (kind = 2) update a set a.pay_user05_srv = isnull(@amt, 0) from #stat_byday a where comp = @comp --自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$5' and (kind = 9) update a set a.pay_user05_good = isnull(@amt, 0) from #stat_byday a where comp = @comp --自定义支付, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$6' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8) update a set a.pay_user06 = isnull(@amt, 0) from #stat_byday a where comp = @comp --自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$6' and (kind = 2) update a set a.pay_user06_srv = isnull(@amt, 0) from #stat_byday a where comp = @comp --自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$6' and (kind = 9) update a set a.pay_user06_good = isnull(@amt, 0) from #stat_byday a where comp = @comp --自定义支付, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$7' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8) update a set a.pay_user07 = isnull(@amt, 0) from #stat_byday a where comp = @comp --自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$7' and (kind = 2) update a set a.pay_user07_srv = isnull(@amt, 0) from #stat_byday a where comp = @comp --自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$7' and (kind = 9) update a set a.pay_user07_good = isnull(@amt, 0) from #stat_byday a where comp = @comp --自定义支付, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$8' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8) update a set a.pay_user08 = isnull(@amt, 0) from #stat_byday a where comp = @comp --自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$8' and (kind = 2) update a set a.pay_user08_srv = isnull(@amt, 0) from #stat_byday a where comp = @comp --自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$8' and (kind = 9) update a set a.pay_user08_good = isnull(@amt, 0) from #stat_byday a where comp = @comp ----------------------------------------------------------------------------------------------------------------------------------- ---------------------------------------------------------$9------------------------------------------------------------------------ --自定义支付, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$9' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8) update a set a.pay_user09 = isnull(@amt, 0) from #stat_byday a where comp = @comp --自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$9' and (kind = 2) update a set a.pay_user09_srv = isnull(@amt, 0) from #stat_byday a where comp = @comp --自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$9' and (kind = 9) update a set a.pay_user09_good = isnull(@amt, 0) from #stat_byday a where comp = @comp ----------------------------------------------------------------------------------------------------------------------------------- ---------------------------------------------------------$10------------------------------------------------------------------------ --自定义支付, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$10' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8) update a set a.pay_user10 = isnull(@amt, 0) from #stat_byday a where comp = @comp --自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$10' and (kind = 2) update a set a.pay_user10_srv = isnull(@amt, 0) from #stat_byday a where comp = @comp --自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$10' and (kind = 9) update a set a.pay_user10_good = isnull(@amt, 0) from #stat_byday a where comp = @comp ----------------------------------------------------------------------------------------------------------------------------------- ---------------------------------------------------------$11------------------------------------------------------------------------ --自定义支付, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$11' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8) update a set a.pay_user11 = isnull(@amt, 0) from #stat_byday a where comp = @comp --自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$11' and (kind = 2) update a set a.pay_user11_srv = isnull(@amt, 0) from #stat_byday a where comp = @comp --自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$11' and (kind = 9) update a set a.pay_user11_good = isnull(@amt, 0) from #stat_byday a where comp = @comp ----------------------------------------------------------------------------------------------------------------------------------- ---------------------------------------------------------$12------------------------------------------------------------------------ --自定义支付, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$12' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8) update a set a.pay_user12 = isnull(@amt, 0) from #stat_byday a where comp = @comp --自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$12' and (kind = 2) update a set a.pay_user12_srv = isnull(@amt, 0) from #stat_byday a where comp = @comp --自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$12' and (kind = 9) update a set a.pay_user12_good = isnull(@amt, 0) from #stat_byday a where comp = @comp ----------------------------------------------------------------------------------------------------------------------------------- ---------------------------------------------------------$13------------------------------------------------------------------------ --自定义支付, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$13' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8) update a set a.pay_user13 = isnull(@amt, 0) from #stat_byday a where comp = @comp --自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$13' and (kind = 2) update a set a.pay_user13_srv = isnull(@amt, 0) from #stat_byday a where comp = @comp --自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$13' and (kind = 9) update a set a.pay_user13_good = isnull(@amt, 0) from #stat_byday a where comp = @comp ----------------------------------------------------------------------------------------------------------------------------------- ---------------------------------------------------------$14------------------------------------------------------------------------ --自定义支付, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$14' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8) update a set a.pay_user14 = isnull(@amt, 0) from #stat_byday a where comp = @comp --自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$14' and (kind = 2) update a set a.pay_user14_srv = isnull(@amt, 0) from #stat_byday a where comp = @comp --自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$14' and (kind = 9) update a set a.pay_user14_good = isnull(@amt, 0) from #stat_byday a where comp = @comp ----------------------------------------------------------------------------------------------------------------------------------- ---------------------------------------------------------$15------------------------------------------------------------------------ --自定义支付, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$15' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8) update a set a.pay_user15 = isnull(@amt, 0) from #stat_byday a where comp = @comp --自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$15' and (kind = 2) update a set a.pay_user15_srv = isnull(@amt, 0) from #stat_byday a where comp = @comp --自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$15' and (kind = 9) update a set a.pay_user15_good = isnull(@amt, 0) from #stat_byday a where comp = @comp ----------------------------------------------------------------------------------------------------------------------------------- -- 产品购买的现金 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110301' or code = '110310' -- 包括产品现金的还款 update a set a.cash_good = isnull(@amt, 0) from #stat_byday a where comp = @comp -- 产品购买的银行卡 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110306' or code = '110311' -- 包括产品银行卡的还款 update a set a.credit_good = isnull(@amt, 0) from #stat_byday a where comp = @comp -- 产品购买的支付宝 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '1103Z1' or code = '1103Z2' -- 包括产品支付宝的还款 update a set a.zfb_good = isnull(@amt, 0) from #stat_byday a where comp = @comp -- 产品购买的微信 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '1103W1' or code = '1103W2' -- 包括产品微信的还款 update a set a.wx_good = isnull(@amt, 0) from #stat_byday a where comp = @comp -- 产品购买的卡付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110302' update a set a.pay_card_good = isnull(@amt, 0) from #stat_byday a where comp = @comp -- 产品总销售 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '1103' update a set a.good_total = isnull(@amt, 0) from #stat_byday a where comp = @comp update #stat_byday set cash_total = isnull(cash_card, 0) + isnull(cash_srv, 0) + isnull(cash_good, 0), credit_total = isnull(credit_card, 0) + isnull(credit_srv, 0) + isnull(credit_good, 0), zfb_total = isnull(zfb_card, 0) + isnull(zfb_srv, 0) + isnull(zfb_good, 0), wx_total = isnull(wx_card, 0) + isnull(wx_srv, 0) + isnull(wx_good, 0) where comp = @comp -- 收入都同利润分析表中的收入 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '11' -- 收入 update #stat_byday set revene = @amt where comp = @comp update #stat_byday set profit = isnull(revene, 0) + isnull(other_income, 0) - isnull(outcome, 0) where comp = @comp select @guestflux = 0, @guestcount = 0, @guestprice = 0 -- 客流数,客数,客单价 select @guestflux = count(gga01c), @guestcount =sum(gga14f), @guestprice = round(dbo.fun_divide_default(sum(gga16f), sum(gga14f), 0), 2) from ggm01 with(nolock) where gga00c = @comp and gga02d >= @fromdate and gga02d <= @todate update #stat_byday set guestflux = @guestflux, guestcount = @guestcount, guestprice = @guestprice where comp = @comp select @sp056 = gsa02c from gsm01 with(nolock) where gsa00c = @comp and gsa01c = 'SP056' select @sp055f = gsa03f from gsm01 with(nolock) where gsa00c = @comp and gsa01c = 'SP055' --查询支付方式$6设置的比率 如果不需要计算劳动业绩 返回0 select @user06_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso09f, 0) * isnull(gso03i,1) end, @user06_good_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso10f, 0) * isnull(gso06f,1) end from gsm15 with (nolock) where gso00c = @comp and gso02c = '$6' --查询支付方式$7设置的比率 如果不需要计算劳动业绩 返回0 select @user07_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso09f, 0) * isnull(gso03i,1) end, @user07_good_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso10f, 0) * isnull(gso06f,1) end from gsm15 with (nolock) where gso00c = @comp and gso02c = '$7' --查询支付方式$8设置的比率 如果不需要计算劳动业绩 返回0 select @user08_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso09f, 0) * isnull(gso03i,1) end, @user08_good_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso10f, 0) * isnull(gso06f,1) end from gsm15 with (nolock) where gso00c = @comp and gso02c = '$8' --查询支付方式$9设置的比率 如果不需要计算劳动业绩 返回0 select @user09_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso09f, 0) * isnull(gso03i,1) end, @user09_good_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso10f, 0) * isnull(gso06f,1) end from gsm15 with (nolock) where gso00c = @comp and gso02c = '$9' --查询支付方式$10设置的比率 如果不需要计算劳动业绩 返回0 select @user10_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso09f, 0) * isnull(gso03i,1) end, @user10_good_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso10f, 0) * isnull(gso06f,1) end from gsm15 with (nolock) where gso00c = @comp and gso02c = '$10' --查询支付方式$11设置的比率 如果不需要计算劳动业绩 返回0 select @user11_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso09f, 0) * isnull(gso03i,1) end, @user11_good_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso10f, 0) * isnull(gso06f,1) end from gsm15 with (nolock) where gso00c = @comp and gso02c = '$11' --查询支付方式$12设置的比率 如果不需要计算劳动业绩 返回0 select @user12_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso09f, 0) * isnull(gso03i,1) end, @user12_good_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso10f, 0) * isnull(gso06f,1) end from gsm15 with (nolock) where gso00c = @comp and gso02c = '$12' --查询支付方式$13设置的比率 如果不需要计算劳动业绩 返回0 select @user13_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso09f, 0) * isnull(gso03i,1) end, @user13_good_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso10f, 0) * isnull(gso06f,1) end from gsm15 with (nolock) where gso00c = @comp and gso02c = '$13' --查询支付方式$14设置的比率 如果不需要计算劳动业绩 返回0 select @user14_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso09f, 0) * isnull(gso03i,1) end, @user14_good_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso10f, 0) * isnull(gso06f,1) end from gsm15 with (nolock) where gso00c = @comp and gso02c = '$14' --查询支付方式$15设置的比率 如果不需要计算劳动业绩 返回0 select @user15_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso09f, 0) * isnull(gso03i,1) end, @user15_good_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso10f, 0) * isnull(gso06f,1) end from gsm15 with (nolock) where gso00c = @comp and gso02c = '$15' exec upg_get_workperf_rate @comp, @cash_rate output, @cash_good_rate output, @credit_rate output, @credit_good_rate output, @pay3_rate output, @pay3_good_rate output, @pay$_rate output, @pay$_good_rate output, @pay0_rate output, @pay0_good_rate output, @manager_rate output, @manager_good_rate output, @point_rate output, @point_good_rate output, @user01_rate output, @user01_good_rate output, @user02_rate output, @user02_good_rate output, @user03_rate output, @user03_good_rate output, @user04_rate output, @user04_good_rate output, @user05_rate output, @user05_good_rate output, @zfb_rate output, @zfb_good_rate output, @wx_rate output, @wx_good_rate output if @sp056 = '4' -- 足疗 begin -- 足疗的劳动业绩 -- 劳动业绩 = 现金(服务) + 现金(物品) + 卡付(物品) + 卡付 + 抵用券 + 银行卡(服务) + 银行卡(物品) + 积分支付(项目 + 产品)--modify by limin -2014-12-23 update #stat_byday set work_perf = isnull(cash_srv, 0) * @cash_rate + isnull(cash_good, 0) * @cash_good_rate + isnull(pay_card_srv, 0) + isnull(pay_card_good, 0) + isnull(credit_srv, 0) * @credit_rate + isnull(credit_good, 0) * @credit_good_rate + isnull(zfb_srv, 0) * @zfb_rate + isnull(zfb_good, 0) * @zfb_good_rate + isnull(wx_srv, 0) * @wx_rate + isnull(wx_good, 0) * @wx_good_rate + (isnull(pay_coupon_srv, 0) - isnull(pay_coupon_good, 0)) * @pay3_rate + isnull(pay_coupon_good, 0) * @pay3_good_rate + (isnull(pay_point_srv, 0) - isnull(pay_point_good, 0)) * @point_rate + isnull(pay_point_good, 0) * @point_good_rate + (isnull(pay_user01_srv, 0) - isnull(pay_user01_good, 0)) * @user01_rate + isnull(pay_user01_good, 0) * @user01_good_rate + (isnull(pay_user02_srv, 0) - isnull(pay_user02_good, 0)) * @user02_rate + isnull(pay_user02_good, 0) * @user02_good_rate + (isnull(pay_user03_srv, 0) - isnull(pay_user03_good, 0)) * @user03_rate + isnull(pay_user03_good, 0) * @user03_good_rate + (isnull(pay_user04_srv, 0) - isnull(pay_user04_good, 0)) * @user04_rate + isnull(pay_user04_good, 0) * @user04_good_rate + (isnull(pay_user05_srv, 0) - isnull(pay_user05_good, 0)) * @user05_rate + isnull(pay_user05_good, 0) * @user05_good_rate + (isnull(pay_user06_srv, 0) - isnull(pay_user06_good, 0)) * isnull(@user06_rate,1) + isnull(pay_user06_good, 0) * isnull(@user06_good_rate,1) + (isnull(pay_user07_srv, 0) - isnull(pay_user07_good, 0)) * isnull(@user07_rate,1) + isnull(pay_user07_good, 0) * isnull(@user07_good_rate,1) + (isnull(pay_user08_srv, 0) - isnull(pay_user08_good, 0)) * isnull(@user08_rate,1) + isnull(pay_user08_good, 0) * isnull(@user08_good_rate,1) + (isnull(pay_user09_srv, 0) - isnull(pay_user09_good, 0)) * isnull(@user09_rate,1) + isnull(pay_user09_good, 0) * isnull(@user09_good_rate,1) + (isnull(pay_user10_srv, 0) - isnull(pay_user10_good, 0)) * isnull(@user10_rate,1) + isnull(pay_user10_good, 0) * isnull(@user10_good_rate,1) + (isnull(pay_user11_srv, 0) - isnull(pay_user11_good, 0)) * isnull(@user11_rate,1) + isnull(pay_user11_good, 0) * isnull(@user11_good_rate,1) + (isnull(pay_user12_srv, 0) - isnull(pay_user12_good, 0)) * isnull(@user12_rate,1) + isnull(pay_user12_good, 0) * isnull(@user12_good_rate,1) + (isnull(pay_user13_srv, 0) - isnull(pay_user13_good, 0)) * isnull(@user13_rate,1) + isnull(pay_user13_good, 0) * isnull(@user13_good_rate,1) + (isnull(pay_user14_srv, 0) - isnull(pay_user14_good, 0)) * isnull(@user14_rate,1) + isnull(pay_user14_good, 0) * isnull(@user14_good_rate,1) + (isnull(pay_user15_srv, 0) - isnull(pay_user15_good, 0)) * isnull(@user15_rate,1) + isnull(pay_user15_good, 0) * isnull(@user15_good_rate,1) where comp = @comp end else begin if @sp055f = 1 begin -- 按单据支付 update #stat_byday set work_perf = isnull(cash_srv, 0) * @cash_rate + isnull(cash_good, 0) * @cash_good_rate + isnull(pay_card_srv, 0) + isnull(pay_card_good, 0) + isnull(credit_srv, 0) * @credit_rate + isnull(credit_good, 0) * @credit_good_rate + isnull(zfb_srv, 0) * @zfb_rate + isnull(zfb_good, 0) * @zfb_good_rate + isnull(wx_srv, 0) * @wx_rate + isnull(wx_good, 0) * @wx_good_rate + (isnull(pay_coupon_srv, 0) - isnull(pay_coupon_good, 0)) * @pay3_rate + isnull(pay_coupon_good, 0) * @pay3_good_rate + (isnull(pay_coupon2_srv, 0) - isnull(pay_coupon2_good, 0)) * @pay$_rate + isnull(pay_coupon2_good, 0) * @pay$_good_rate + (isnull(pay_coupon3_srv, 0) - isnull(pay_coupon3_good, 0)) * @pay0_rate + isnull(pay_coupon3_good, 0) * @pay0_good_rate + (isnull(pay_manager_prj, 0) - isnull(pay_manager_good, 0)) * @manager_rate + isnull(pay_manager_good, 0) * @manager_good_rate + (isnull(pay_point_srv, 0) - isnull(pay_point_good, 0)) * @point_rate + isnull(pay_point_good, 0) * @point_good_rate + (isnull(pay_user01_srv, 0) - isnull(pay_user01_good, 0)) * @user01_rate + isnull(pay_user01_good, 0) * @user01_good_rate + (isnull(pay_user02_srv, 0) - isnull(pay_user02_good, 0)) * @user02_rate + isnull(pay_user02_good, 0) * @user02_good_rate + (isnull(pay_user03_srv, 0) - isnull(pay_user03_good, 0)) * @user03_rate + isnull(pay_user03_good, 0) * @user03_good_rate + (isnull(pay_user04_srv, 0) - isnull(pay_user04_good, 0)) * @user04_rate + isnull(pay_user04_good, 0) * @user04_good_rate + (isnull(pay_user05_srv, 0) - isnull(pay_user05_good, 0)) * @user05_rate + isnull(pay_user05_good, 0) * @user05_good_rate + (isnull(pay_user06_srv, 0) - isnull(pay_user06_good, 0)) * isnull(@user06_rate,1) + isnull(pay_user06_good, 0) * isnull(@user06_good_rate,1) + (isnull(pay_user07_srv, 0) - isnull(pay_user07_good, 0)) * isnull(@user07_rate,1) + isnull(pay_user07_good, 0) * isnull(@user07_good_rate,1) + (isnull(pay_user08_srv, 0) - isnull(pay_user08_good, 0)) * isnull(@user08_rate,1) + isnull(pay_user08_good, 0) * isnull(@user08_good_rate,1) + (isnull(pay_user09_srv, 0) - isnull(pay_user09_good, 0)) * isnull(@user09_rate,1) + isnull(pay_user09_good, 0) * isnull(@user09_good_rate,1) + (isnull(pay_user10_srv, 0) - isnull(pay_user10_good, 0)) * isnull(@user10_rate,1) + isnull(pay_user10_good, 0) * isnull(@user10_good_rate,1) + (isnull(pay_user11_srv, 0) - isnull(pay_user11_good, 0)) * isnull(@user11_rate,1) + isnull(pay_user11_good, 0) * isnull(@user11_good_rate,1) + (isnull(pay_user11_srv, 0) - isnull(pay_user11_good, 0)) * isnull(@user11_rate,1) + isnull(pay_user11_good, 0) * isnull(@user11_good_rate,1) + (isnull(pay_user12_srv, 0) - isnull(pay_user12_good, 0)) * isnull(@user12_rate,1) + isnull(pay_user12_good, 0) * isnull(@user12_good_rate,1) + (isnull(pay_user13_srv, 0) - isnull(pay_user13_good, 0)) * isnull(@user13_rate,1) + isnull(pay_user13_good, 0) * isnull(@user13_good_rate,1) + (isnull(pay_user14_srv, 0) - isnull(pay_user14_good, 0)) * isnull(@user14_rate,1) + isnull(pay_user14_good, 0) * isnull(@user14_good_rate,1) + (isnull(pay_user15_srv, 0) - isnull(pay_user15_good, 0)) * isnull(@user15_rate,1) + isnull(pay_user15_good, 0) * isnull(@user15_good_rate,1) where comp = @comp end else begin -- 劳动业绩 = 现金(服务) + 现金(产品) + 卡付(产品) + 卡付实营业额 + 经理签单 + 内调 + 抵用券 + 银行卡(服务) + 银行卡(产品) update #stat_byday set work_perf = isnull(cash_srv, 0) * @cash_rate + isnull(cash_good, 0) * @cash_good_rate + isnull(pay_card_srv_work, 0) + isnull(credit_srv, 0) * @credit_rate + isnull(credit_good, 0) * @credit_good_rate + isnull(zfb_srv, 0) * @zfb_rate + isnull(zfb_good, 0) * @zfb_good_rate + isnull(wx_srv, 0) * @wx_rate + isnull(wx_good, 0) * @wx_good_rate + (isnull(pay_coupon_srv, 0) - isnull(pay_coupon_good, 0)) * @pay3_rate + isnull(pay_coupon_good, 0) * @pay3_good_rate + (isnull(pay_coupon2_srv, 0) - isnull(pay_coupon2_good, 0)) * @pay$_rate + isnull(pay_coupon2_good, 0) * @pay$_good_rate + (isnull(pay_coupon3_srv, 0) - isnull(pay_coupon3_good, 0)) * @pay0_rate + isnull(pay_coupon3_good, 0) * @pay0_good_rate + (isnull(pay_manager_prj, 0) - isnull(pay_manager_good, 0)) * @manager_rate + isnull(pay_manager_good, 0) * @manager_good_rate + (isnull(pay_point_srv, 0) - isnull(pay_point_good, 0)) * @point_rate + isnull(pay_point_good, 0) * @point_good_rate + (isnull(pay_user01_srv, 0) - isnull(pay_user01_good, 0)) * @user01_rate + isnull(pay_user01_good, 0) * @user01_good_rate + (isnull(pay_user02_srv, 0) - isnull(pay_user02_good, 0)) * @user02_rate + isnull(pay_user02_good, 0) * @user02_good_rate + (isnull(pay_user03_srv, 0) - isnull(pay_user03_good, 0)) * @user03_rate + isnull(pay_user03_good, 0) * @user03_good_rate + (isnull(pay_user04_srv, 0) - isnull(pay_user04_good, 0)) * @user04_rate + isnull(pay_user04_good, 0) * @user04_good_rate + (isnull(pay_user05_srv, 0) - isnull(pay_user05_good, 0)) * @user05_rate + isnull(pay_user05_good, 0) * @user05_good_rate + (isnull(pay_user06_srv, 0) - isnull(pay_user06_good, 0)) * isnull(@user06_rate,1) + isnull(pay_user06_good, 0) * isnull(@user06_good_rate,1) + (isnull(pay_user07_srv, 0) - isnull(pay_user07_good, 0)) * isnull(@user07_rate,1) + isnull(pay_user07_good, 0) * isnull(@user07_good_rate,1) + (isnull(pay_user08_srv, 0) - isnull(pay_user08_good, 0)) * isnull(@user08_rate,1) + isnull(pay_user08_good, 0) * isnull(@user08_good_rate,1) + (isnull(pay_user09_srv, 0) - isnull(pay_user09_good, 0)) * isnull(@user09_rate,1) + isnull(pay_user09_good, 0) * isnull(@user09_good_rate,1) + (isnull(pay_user10_srv, 0) - isnull(pay_user10_good, 0)) * isnull(@user10_rate,1) + isnull(pay_user10_good, 0) * isnull(@user10_good_rate,1) + (isnull(pay_user11_srv, 0) - isnull(pay_user11_good, 0)) * isnull(@user11_rate,1) + isnull(pay_user11_good, 0) * isnull(@user11_good_rate,1) + (isnull(pay_user12_srv, 0) - isnull(pay_user12_good, 0)) * isnull(@user12_rate,1) + isnull(pay_user12_good, 0) * isnull(@user12_good_rate,1) + (isnull(pay_user13_srv, 0) - isnull(pay_user13_good, 0)) * isnull(@user13_rate,1) + isnull(pay_user13_good, 0) * isnull(@user13_good_rate,1) + (isnull(pay_user14_srv, 0) - isnull(pay_user14_good, 0)) * isnull(@user14_rate,1) + isnull(pay_user14_good, 0) * isnull(@user14_good_rate,1) + (isnull(pay_user15_srv, 0) - isnull(pay_user15_good, 0)) * isnull(@user15_rate,1) + isnull(pay_user15_good, 0) * isnull(@user15_good_rate,1) where comp = @comp end end fetch cur_get_compid into @comp end close cur_get_compid deallocate cur_get_compid end -- 单店 else begin declare cur_get_compid_2 cursor for select gaz02c from gam26 where gaz01c = @compOur and gaz02c between @compfrom and @compto -- select gae01c from gam05 with(nolock) -- where gae01c >= @compfrom -- and gae01c <= @compto open cur_get_compid_2 fetch cur_get_compid_2 into @compfrom while @@fetch_status = 0 begin select @isadd_0 = gso07f from gsm15 with (nolock) where gso00c = @compfrom and gso02c = '0' select @isadd_0 = isnull(@isadd_0, 0) select @isadd_3 = gso07f from gsm15 with (nolock) where gso00c = @compfrom and gso02c = '3' select @isadd_3 = isnull(@isadd_3, 0) select @isadd_$ = gso07f from gsm15 with (nolock) where gso00c = @compfrom and gso02c = '$' select @isadd_$ = isnull(@isadd_$, 0) delete #tbl_profit_day insert #tbl_profit_day(idd, code, itemname, is_add, amt, is_leaf, payment, kind) exec upg_analyse_profit @compfrom, @fromdate, @todate, 1, '*', @calmode insert #stat_byday(comp) values(@compfrom) -- 转换为汇总表需要的一条记录 -- 现金(卖卡) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110101' or code = '110104' or code = '110107' or code = '110118' or code = '110140' -- 现金取款 select @tmpamt = 0 select @tmpamt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110119' update a set a.cash_card = isnull(@amt, 0) + isnull(@tmpamt, 0) from #stat_byday a where comp = @compfrom ----------------------------------------------$1 -> $8----------------------------------------------------------------------------- --自定义支付, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$1' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8) update a set a.pay_user01 = isnull(@amt, 0) from #stat_byday a where comp = @compfrom --自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$1' and (kind = 2) update a set a.pay_user01_srv = isnull(@amt, 0) from #stat_byday a where comp = @compfrom --自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$1' and (kind = 9) update a set a.pay_user01_good = isnull(@amt, 0) from #stat_byday a where comp = @compfrom --自定义支付, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$2' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8) update a set a.pay_user02 = isnull(@amt, 0) from #stat_byday a where comp = @compfrom --自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$2' and (kind = 2) update a set a.pay_user02_srv = isnull(@amt, 0) from #stat_byday a where comp = @compfrom --自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$2' and (kind = 9) update a set a.pay_user02_good = isnull(@amt, 0) from #stat_byday a where comp = @compfrom --自定义支付, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$3' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8 or kind = -1) update a set a.pay_user03 = isnull(@amt, 0) from #stat_byday a where comp = @compfrom --自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$3' and (kind = 2) update a set a.pay_user03_srv = isnull(@amt, 0) from #stat_byday a where comp = @compfrom --自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$3' and (kind = 9) update a set a.pay_user03_good = isnull(@amt, 0) from #stat_byday a where comp = @compfrom --自定义支付, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$4' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8) update a set a.pay_user04 = isnull(@amt, 0) from #stat_byday a where comp = @compfrom --自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$4' and (kind = 2) update a set a.pay_user04_srv = isnull(@amt, 0) from #stat_byday a where comp = @compfrom --自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$4' and (kind = 9) update a set a.pay_user04_good = isnull(@amt, 0) from #stat_byday a where comp = @compfrom --自定义支付, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$5' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8) update a set a.pay_user05 = isnull(@amt, 0) from #stat_byday a where comp = @compfrom --自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$5' and (kind = 2) update a set a.pay_user05_srv = isnull(@amt, 0) from #stat_byday a where comp = @compfrom --自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$5' and (kind = 9) update a set a.pay_user05_good = isnull(@amt, 0) from #stat_byday a where comp = @compfrom --自定义支付, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$6' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8) update a set a.pay_user06 = isnull(@amt, 0) from #stat_byday a where comp = @compfrom --自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$6' and (kind = 2) update a set a.pay_user06_srv = isnull(@amt, 0) from #stat_byday a where comp = @compfrom --自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$6' and (kind = 9) update a set a.pay_user06_good = isnull(@amt, 0) from #stat_byday a where comp = @compfrom --自定义支付, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$7' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8) update a set a.pay_user07 = isnull(@amt, 0) from #stat_byday a where comp = @compfrom --自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$7' and (kind = 2) update a set a.pay_user07_srv = isnull(@amt, 0) from #stat_byday a where comp = @compfrom --自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$7' and (kind = 9) update a set a.pay_user07_good = isnull(@amt, 0) from #stat_byday a where comp = @compfrom --自定义支付, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$8' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8) update a set a.pay_user08 = isnull(@amt, 0) from #stat_byday a where comp = @compfrom --自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$8' and (kind = 2) update a set a.pay_user08_srv = isnull(@amt, 0) from #stat_byday a where comp = @compfrom --自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$8' and (kind = 9) update a set a.pay_user08_good = isnull(@amt, 0) from #stat_byday a where comp = @compfrom ----------------------------------------------------------------------------------------------------------------------------------- ------------------------------------------------------------------$9支付方式------------------------------------------------------- --自定义支付, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$9' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8) update a set a.pay_user09 = isnull(@amt, 0) from #stat_byday a where comp = @compfrom --自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$9' and (kind = 2) update a set a.pay_user09_srv = isnull(@amt, 0) from #stat_byday a where comp = @compfrom --自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$9' and (kind = 9) update a set a.pay_user09_good = isnull(@amt, 0) from #stat_byday a where comp = @compfrom ----------------------------------------------------------------------------------------------------------------------------------- ------------------------------------------------------------------$10支付方式------------------------------------------------------- --自定义支付, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$10' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8) update a set a.pay_user10 = isnull(@amt, 0) from #stat_byday a where comp = @compfrom --自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$10' and (kind = 2) update a set a.pay_user10_srv = isnull(@amt, 0) from #stat_byday a where comp = @compfrom --自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$10' and (kind = 9) update a set a.pay_user10_good = isnull(@amt, 0) from #stat_byday a where comp = @compfrom ----------------------------------------------------------------------------------------------------------------------------------- ------------------------------------------------------------------$11支付方式------------------------------------------------------- --自定义支付, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$11' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8) update a set a.pay_user11 = isnull(@amt, 0) from #stat_byday a where comp = @compfrom --自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$11' and (kind = 2) update a set a.pay_user11_srv = isnull(@amt, 0) from #stat_byday a where comp = @compfrom --自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$11' and (kind = 9) update a set a.pay_user11_good = isnull(@amt, 0) from #stat_byday a where comp = @compfrom ----------------------------------------------------------------------------------------------------------------------------------- ------------------------------------------------------------------$12支付方式------------------------------------------------------- --自定义支付, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$12' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8) update a set a.pay_user12 = isnull(@amt, 0) from #stat_byday a where comp = @compfrom --自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$12' and (kind = 2) update a set a.pay_user12_srv = isnull(@amt, 0) from #stat_byday a where comp = @compfrom --自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$12' and (kind = 9) update a set a.pay_user12_good = isnull(@amt, 0) from #stat_byday a where comp = @compfrom ----------------------------------------------------------------------------------------------------------------------------------- ------------------------------------------------------------------$13支付方式------------------------------------------------------- --自定义支付, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$13' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8) update a set a.pay_user13 = isnull(@amt, 0) from #stat_byday a where comp = @compfrom --自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$13' and (kind = 2) update a set a.pay_user13_srv = isnull(@amt, 0) from #stat_byday a where comp = @compfrom --自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$13' and (kind = 9) update a set a.pay_user13_good = isnull(@amt, 0) from #stat_byday a where comp = @compfrom ----------------------------------------------------------------------------------------------------------------------------------- ------------------------------------------------------------------$14支付方式------------------------------------------------------- --自定义支付, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$14' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8) update a set a.pay_user14 = isnull(@amt, 0) from #stat_byday a where comp = @compfrom --自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$14' and (kind = 2) update a set a.pay_user14_srv = isnull(@amt, 0) from #stat_byday a where comp = @compfrom --自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$14' and (kind = 9) update a set a.pay_user14_good = isnull(@amt, 0) from #stat_byday a where comp = @compfrom ----------------------------------------------------------------------------------------------------------------------------------- ------------------------------------------------------------------$15支付方式------------------------------------------------------- --自定义支付, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$15' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8) update a set a.pay_user15 = isnull(@amt, 0) from #stat_byday a where comp = @compfrom --自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$15' and (kind = 2) update a set a.pay_user15_srv = isnull(@amt, 0) from #stat_byday a where comp = @compfrom --自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$15' and (kind = 9) update a set a.pay_user15_good = isnull(@amt, 0) from #stat_byday a where comp = @compfrom ----------------------------------------------------------------------------------------------------------------------------------- -- 现金(服务) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110201' or code = '110230' select @tmpamt = 0 select @tmpamt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110301' or code = '110310' -- 包括了产品的现金还款 update a set a.cash_srv = isnull(@amt, 0) - isnull(@tmpamt, 0) from #stat_byday a where comp = @compfrom -- 银行卡(卖卡) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110102' or code = '110105' or code = '110108' or code = '110141' update a set a.credit_card = isnull(@amt, 0) from #stat_byday a where comp = @compfrom -- 支付宝(卖卡) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '1101Z1' or code = '1101Z2' or code = '1101Z3' or code = '1101Z4' update a set a.zfb_card = isnull(@amt, 0) from #stat_byday a where comp = @compfrom -- 微信(卖卡) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '1101W1' or code = '1101W2' or code = '1101W3' update a set a.wx_card = isnull(@amt, 0) from #stat_byday a where comp = @compfrom -- 经理签单,只包括项目 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110203' update a set a.pay_manager_prj = isnull(@amt, 0) from #stat_byday a where comp = @compfrom -- 积分支付,只包括项目 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '11020507' update a set a.pay_point_srv = isnull(@amt, 0) from #stat_byday a where comp = @compfrom -- 积分支付,包括卡,项目 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '11020507' or code = '110133') update a set a.pay_point = isnull(@amt, 0) from #stat_byday a where comp = @compfrom -- 卡付实际营业额 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110206' update a set a.pay_card_srv_act = isnull(@amt, 0) from #stat_byday a where comp = @compfrom select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '11AA01' update a set a.pay_card_srv_work = isnull(@amt, 0) from #stat_byday a where comp = @compfrom -- 不管支付方式,实际上的卡销售额 select @amt = 0 select @amt = sum(isnull(amt, 0) * is_add) from #tbl_profit_day where code = '110101' or code = '110102' or code = '110104' or code = '110105' or code = '110107' or code = '110108' or code = '110115' or code = '110116' or code = '110117' or code = '110119' update a set a.sale_card_total = isnull(@amt, 0) from #stat_byday a where comp = @compfrom --银行卡(服务) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110202' or code = '110231' --项目或物品的银行卡还款不算 select @tmpamt = 0 select @tmpamt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110306' or code = '110311' -- 产品销售的银行卡支付 以及 还款 update a set a.credit_srv = isnull(@amt, 0) - isnull(@tmpamt, 0) from #stat_byday a where comp = @compfrom -- 支付宝(服务) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '1102Z1' select @tmpamt = 0 select @tmpamt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '1103Z1' or code = '1103Z2' -- 产品销售的支付宝支付 以及 还款 update a set a.zfb_srv = isnull(@amt, 0) - isnull(@tmpamt, 0) from #stat_byday a where comp = @compfrom -- 微信(服务) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '1102W1' select @tmpamt = 0 select @tmpamt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '1103W1' or code = '1103W2' -- 产品销售的微信支付 以及 还款 update a set a.wx_srv = isnull(@amt, 0) - isnull(@tmpamt, 0) from #stat_byday a where comp = @compfrom -- 其他收入 Huzy 2012/06/04 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '18' update a set a.other_income = isnull(@amt, 0) from #stat_byday a where comp = @compfrom -- 支出 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '12' -- 房租不在当天的支出考虑 select @tmpamt = 0 select @tmpamt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '1299' update a set a.outcome = isnull(@amt, 0) - isnull(@tmpamt, 0) from #stat_byday a where comp = @compfrom -- 欠款 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110103' or code = '110106' or code = '110207' update a set a.defict = isnull(@amt, 0) from #stat_byday a where comp = @compfrom -- 还款 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110107' or code = '110108' update a set a.returnamt = isnull(@amt, 0) from #stat_byday a where comp = @compfrom -- 卡付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110205' select @tmpamt = 0 select @tmpamt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110302' update a set a.pay_card_srv = isnull(@amt, 0) - isnull(@tmpamt, 0) from #stat_byday a where comp = @compfrom -- 抵用券付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110204' or code = '110125' or code = '110126' or code = '110127') update a set a.pay_coupon = isnull(@amt, 0) from #stat_byday a where comp = @compfrom -- 抵用券付, 只包括项目 + 产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110204') update a set a.pay_coupon_srv = isnull(@amt, 0) from #stat_byday a where comp = @compfrom -- 贵付(抵用券二) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110208' or code = '110130' or code = '110131' or code = '110132') update a set a.pay_coupon2 = isnull(@amt, 0) from #stat_byday a where comp = @compfrom -- 贵付(抵用券二), 只包括项目 + 产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110208') update a set a.pay_coupon2_srv = isnull(@amt, 0) from #stat_byday a where comp = @compfrom -- 内调(洗头) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110209' or code = '110120' or code = '110121' or code = '110122') update a set a.pay_coupon3 = isnull(@amt, 0) from #stat_byday a where comp = @compfrom -- 内调(洗头), 只包括项目 + 产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110209') update a set a.pay_coupon3_srv = isnull(@amt, 0) from #stat_byday a where comp = @compfrom -- 经理签单 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110110' or code = '110111' or code = '110203') update a set a.pay_manager = isnull(@amt, 0) from #stat_byday a where comp = @compfrom -- 产品购买的现金 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110301' or code = '110310' -- 包括产品现金的还款 update a set a.cash_good = isnull(@amt, 0) from #stat_byday a where comp = @compfrom -- 产品购买的银行卡 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110306' or code = '110311' -- 包括产品银行卡的还款 update a set a.credit_good = isnull(@amt, 0) from #stat_byday a where comp = @compfrom -- 产品购买的支付宝 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '1103Z1' or code = '1103Z2' -- 包括产品支付宝的还款 update a set a.zfb_good = isnull(@amt, 0) from #stat_byday a where comp = @compfrom -- 产品购买的微信 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '1103W1' or code = '1103W2' -- 包括产品微信的还款 update a set a.wx_good = isnull(@amt, 0) from #stat_byday a where comp = @compfrom -- 产品购买的卡付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110302' update a set a.pay_card_good = isnull(@amt, 0) from #stat_byday a where comp = @compfrom -- 产品总销售 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '1103' update a set a.good_total = isnull(@amt, 0) from #stat_byday a where comp = @compfrom update a set cash_total = isnull(cash_card, 0) + isnull(cash_srv, 0) + isnull(cash_good, 0), credit_total = isnull(credit_card, 0) + isnull(credit_srv, 0) + isnull(credit_good, 0), zfb_total = isnull(zfb_card, 0) + isnull(zfb_srv, 0) + isnull(zfb_good, 0), wx_total = isnull(wx_card, 0) + isnull(wx_srv, 0) + isnull(wx_good, 0) from #stat_byday a where comp = @compfrom -- 收入都同利润分析表中的收入 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '11' -- 收入 update #stat_byday set revene = @amt where comp = @compfrom update #stat_byday set profit = isnull(revene, 0) + isnull(other_income, 0) - isnull(outcome, 0) where comp = @compfrom select @guestflux = 0, @guestcount = 0, @guestprice = 0 -- 客流数,客数,客单价 select @guestflux = count(gga01c), @guestcount =sum(gga14f), @guestprice = round(dbo.fun_divide_default(sum(gga16f), sum(gga14f), 0), 2) from ggm01 with(nolock) where gga00c = @compfrom and gga02d >= @fromdate and gga02d <= @todate update #stat_byday set guestflux = @guestflux, guestcount = @guestcount, guestprice = @guestprice where comp = @compfrom select @sp056 = gsa02c from gsm01 with(nolock) where gsa00c = @compfrom and gsa01c = 'SP056' select @sp055f = gsa03f from gsm01 with(nolock) where gsa00c = @compfrom and gsa01c = 'SP055' --查询支付方式$6设置的比率 如果不需要计算劳动业绩 返回0 select @user06_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso09f, 0) * isnull(gso03i,1) end, @user06_good_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso10f, 0) * isnull(gso06f,1) end from gsm15 with (nolock) where gso00c = @comp and gso02c = '$6' --查询支付方式$7设置的比率 如果不需要计算劳动业绩 返回0 select @user07_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso09f, 0) * isnull(gso03i,1) end, @user07_good_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso10f, 0) * isnull(gso06f,1) end from gsm15 with (nolock) where gso00c = @comp and gso02c = '$7' --查询支付方式$8设置的比率 如果不需要计算劳动业绩 返回0 select @user08_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso09f, 0) * isnull(gso03i,1) end, @user08_good_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso10f, 0) * isnull(gso06f,1) end from gsm15 with (nolock) where gso00c = @comp and gso02c = '$8' --查询支付方式$9设置的比率 如果不需要计算劳动业绩 返回0 select @user09_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso09f, 0) * isnull(gso03i,1) end, @user09_good_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso10f, 0) * isnull(gso06f,1) end from gsm15 with (nolock) where gso00c = @comp and gso02c = '$9' --查询支付方式$10设置的比率 如果不需要计算劳动业绩 返回0 select @user10_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso09f, 0) * isnull(gso03i,1) end, @user10_good_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso10f, 0) * isnull(gso06f,1) end from gsm15 with (nolock) where gso00c = @comp and gso02c = '$10' --查询支付方式$11设置的比率 如果不需要计算劳动业绩 返回0 select @user11_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso09f, 0) * isnull(gso03i,1) end, @user11_good_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso10f, 0) * isnull(gso06f,1) end from gsm15 with (nolock) where gso00c = @comp and gso02c = '$11' --查询支付方式$12设置的比率 如果不需要计算劳动业绩 返回0 select @user12_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso09f, 0) * isnull(gso03i,1) end, @user12_good_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso10f, 0) * isnull(gso06f,1) end from gsm15 with (nolock) where gso00c = @comp and gso02c = '$12' --查询支付方式$13设置的比率 如果不需要计算劳动业绩 返回0 select @user13_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso09f, 0) * isnull(gso03i,1) end, @user13_good_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso10f, 0) * isnull(gso06f,1) end from gsm15 with (nolock) where gso00c = @comp and gso02c = '$13' --查询支付方式$14设置的比率 如果不需要计算劳动业绩 返回0 select @user14_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso09f, 0) * isnull(gso03i,1) end, @user14_good_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso10f, 0) * isnull(gso06f,1) end from gsm15 with (nolock) where gso00c = @comp and gso02c = '$14' --查询支付方式$15设置的比率 如果不需要计算劳动业绩 返回0 select @user15_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso09f, 0) * isnull(gso03i,1) end, @user15_good_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso10f, 0) * isnull(gso06f,1) end from gsm15 with (nolock) where gso00c = @comp and gso02c = '$15' exec upg_get_workperf_rate @compfrom, @cash_rate output, @cash_good_rate output, @credit_rate output, @credit_good_rate output, @pay3_rate output, @pay3_good_rate output, @pay$_rate output, @pay$_good_rate output, @pay0_rate output, @pay0_good_rate output, @manager_rate output, @manager_good_rate output, @point_rate output, @point_good_rate output, @user01_rate output, @user01_good_rate output, @user02_rate output, @user02_good_rate output, @user03_rate output, @user03_good_rate output, @user04_rate output, @user04_good_rate output, @user05_rate output, @user05_good_rate output, @zfb_rate output, @zfb_good_rate output, @wx_rate output, @wx_good_rate output if @sp056 = '4' -- 足疗 begin -- 足疗的劳动业绩 -- 劳动业绩 = 现金(服务) + 现金(物品) + 卡付(物品) + 卡付 + 抵用券 + 银行卡(服务) + 银行卡(物品) + 积分支付(项目 + 产品)--modify by limin -2014-12-23 update #stat_byday set work_perf = isnull(cash_srv, 0) * @cash_rate + isnull(cash_good, 0) * @cash_good_rate + isnull(pay_card_srv, 0) + isnull(pay_card_good, 0) + isnull(credit_srv, 0) * @credit_rate + isnull(credit_good, 0) * @credit_good_rate + isnull(zfb_srv, 0) * @zfb_rate + isnull(zfb_good, 0) * @zfb_good_rate + isnull(wx_srv, 0) * @wx_rate + isnull(wx_good, 0) * @wx_good_rate + (isnull(pay_coupon_srv, 0) - isnull(pay_coupon_good, 0)) * @pay3_rate + isnull(pay_coupon_good, 0) * @pay3_good_rate + (isnull(pay_point_srv, 0) - isnull(pay_point_good, 0)) * @point_rate + isnull(pay_point_good, 0) * @point_good_rate + (isnull(pay_user01_srv, 0) - isnull(pay_user01_good, 0)) * @user01_rate + isnull(pay_user01_good, 0) * @user01_good_rate + (isnull(pay_user02_srv, 0) - isnull(pay_user02_good, 0)) * @user02_rate + isnull(pay_user02_good, 0) * @user02_good_rate + (isnull(pay_user03_srv, 0) - isnull(pay_user03_good, 0)) * @user03_rate + isnull(pay_user03_good, 0) * @user03_good_rate + (isnull(pay_user04_srv, 0) - isnull(pay_user04_good, 0)) * @user04_rate + isnull(pay_user04_good, 0) * @user04_good_rate + (isnull(pay_user05_srv, 0) - isnull(pay_user05_good, 0)) * @user05_rate + isnull(pay_user05_good, 0) * @user05_good_rate + (isnull(pay_user06_srv, 0) - isnull(pay_user06_good, 0)) * isnull(@user06_rate,1) + isnull(pay_user06_good, 0) * isnull(@user06_good_rate,1) + (isnull(pay_user07_srv, 0) - isnull(pay_user07_good, 0)) * isnull(@user07_rate,1) + isnull(pay_user07_good, 0) * isnull(@user07_good_rate,1) + (isnull(pay_user08_srv, 0) - isnull(pay_user08_good, 0)) * isnull(@user08_rate,1) + isnull(pay_user08_good, 0) * isnull(@user08_good_rate,1) + (isnull(pay_user09_srv, 0) - isnull(pay_user09_good, 0)) * isnull(@user09_rate,1) + isnull(pay_user09_good, 0) * isnull(@user09_good_rate,1) + (isnull(pay_user10_srv, 0) - isnull(pay_user10_good, 0)) * isnull(@user10_rate,1) + isnull(pay_user10_good, 0) * isnull(@user10_good_rate,1) + (isnull(pay_user11_srv, 0) - isnull(pay_user11_good, 0)) * isnull(@user11_rate,1) + isnull(pay_user11_good, 0) * isnull(@user11_good_rate,1) + (isnull(pay_user12_srv, 0) - isnull(pay_user12_good, 0)) * isnull(@user12_rate,1) + isnull(pay_user12_good, 0) * isnull(@user12_good_rate,1) + (isnull(pay_user13_srv, 0) - isnull(pay_user13_good, 0)) * isnull(@user13_rate,1) + isnull(pay_user13_good, 0) * isnull(@user13_good_rate,1) + (isnull(pay_user14_srv, 0) - isnull(pay_user14_good, 0)) * isnull(@user14_rate,1) + isnull(pay_user14_good, 0) * isnull(@user14_good_rate,1) + (isnull(pay_user15_srv, 0) - isnull(pay_user15_good, 0)) * isnull(@user15_rate,1) + isnull(pay_user15_good, 0) * isnull(@user15_good_rate,1) where comp = @compfrom end else begin if @sp055f = 1 begin -- 按单据支付 update #stat_byday set work_perf = isnull(cash_srv, 0) * @cash_rate + isnull(cash_good, 0) * @cash_good_rate + isnull(pay_card_srv, 0) + isnull(pay_card_good, 0) + isnull(credit_srv, 0) * @credit_rate + isnull(credit_good, 0) * @credit_good_rate + isnull(zfb_srv, 0) * @zfb_rate + isnull(zfb_good, 0) * @zfb_good_rate + isnull(wx_srv, 0) * @wx_rate + isnull(wx_good, 0) * @wx_good_rate + (isnull(pay_coupon_srv, 0) - isnull(pay_coupon_good, 0)) * @pay3_rate + isnull(pay_coupon_good, 0) * @pay3_good_rate + (isnull(pay_coupon2_srv, 0) - isnull(pay_coupon2_good, 0)) * @pay$_rate + isnull(pay_coupon2_good, 0) * @pay$_good_rate + (isnull(pay_coupon3_srv, 0) - isnull(pay_coupon3_good, 0)) * @pay0_rate + isnull(pay_coupon3_good, 0) * @pay0_good_rate + (isnull(pay_manager_prj, 0) - isnull(pay_manager_good, 0)) * @manager_rate + isnull(pay_manager_good, 0) * @manager_good_rate + (isnull(pay_point_srv, 0) - isnull(pay_point_good, 0)) * @point_rate + isnull(pay_point_good, 0) * @point_good_rate + (isnull(pay_user01_srv, 0) - isnull(pay_user01_good, 0)) * @user01_rate + isnull(pay_user01_good, 0) * @user01_good_rate + (isnull(pay_user02_srv, 0) - isnull(pay_user02_good, 0)) * @user02_rate + isnull(pay_user02_good, 0) * @user02_good_rate + (isnull(pay_user03_srv, 0) - isnull(pay_user03_good, 0)) * @user03_rate + isnull(pay_user03_good, 0) * @user03_good_rate + (isnull(pay_user04_srv, 0) - isnull(pay_user04_good, 0)) * @user04_rate + isnull(pay_user04_good, 0) * @user04_good_rate + (isnull(pay_user05_srv, 0) - isnull(pay_user05_good, 0)) * @user05_rate + isnull(pay_user05_good, 0) * @user05_good_rate + (isnull(pay_user06_srv, 0) - isnull(pay_user06_good, 0)) * isnull(@user06_rate,1) + isnull(pay_user06_good, 0) * isnull(@user06_good_rate,1) + (isnull(pay_user07_srv, 0) - isnull(pay_user07_good, 0)) * isnull(@user07_rate,1) + isnull(pay_user07_good, 0) * isnull(@user07_good_rate,1) + (isnull(pay_user08_srv, 0) - isnull(pay_user08_good, 0)) * isnull(@user08_rate,1) + isnull(pay_user08_good, 0) * isnull(@user08_good_rate,1) + (isnull(pay_user09_srv, 0) - isnull(pay_user09_good, 0)) * isnull(@user09_rate,1) + isnull(pay_user09_good, 0) * isnull(@user09_good_rate,1) + (isnull(pay_user10_srv, 0) - isnull(pay_user10_good, 0)) * isnull(@user10_rate,1) + isnull(pay_user10_good, 0) * isnull(@user10_good_rate,1) + (isnull(pay_user11_srv, 0) - isnull(pay_user11_good, 0)) * isnull(@user11_rate,1) + isnull(pay_user11_good, 0) * isnull(@user11_good_rate,1) + (isnull(pay_user12_srv, 0) - isnull(pay_user12_good, 0)) * isnull(@user12_rate,1) + isnull(pay_user12_good, 0) * isnull(@user12_good_rate,1) + (isnull(pay_user13_srv, 0) - isnull(pay_user13_good, 0)) * isnull(@user13_rate,1) + isnull(pay_user13_good, 0) * isnull(@user13_good_rate,1) + (isnull(pay_user14_srv, 0) - isnull(pay_user14_good, 0)) * isnull(@user14_rate,1) + isnull(pay_user14_good, 0) * isnull(@user14_good_rate,1) + (isnull(pay_user15_srv, 0) - isnull(pay_user15_good, 0)) * isnull(@user15_rate,1) + isnull(pay_user15_good, 0) * isnull(@user15_good_rate,1) where comp = @compfrom end else begin -- 劳动业绩 = 现金(服务) + 现金(产品) + 卡付(产品) + 卡付实营业额 + 经理签单 + 内调 + 抵用券 + 银行卡(服务) + 银行卡(产品) update #stat_byday set work_perf = isnull(cash_srv, 0) * @cash_rate + isnull(cash_good, 0) * @cash_good_rate + isnull(pay_card_srv_work, 0) + isnull(credit_srv, 0) * @credit_rate + isnull(credit_good, 0) * @credit_good_rate + isnull(zfb_srv, 0) * @zfb_rate + isnull(zfb_good, 0) * @zfb_good_rate + isnull(wx_srv, 0) * @wx_rate + isnull(wx_good, 0) * @wx_good_rate + (isnull(pay_coupon_srv, 0) - isnull(pay_coupon_good, 0)) * @pay3_rate + isnull(pay_coupon_good, 0) * @pay3_good_rate + (isnull(pay_coupon2_srv, 0) - isnull(pay_coupon2_good, 0)) * @pay$_rate + isnull(pay_coupon2_good, 0) * @pay$_good_rate + (isnull(pay_coupon3_srv, 0) - isnull(pay_coupon3_good, 0)) * @pay0_rate + isnull(pay_coupon3_good, 0) * @pay0_good_rate + (isnull(pay_manager_prj, 0) - isnull(pay_manager_good, 0)) * @manager_rate + isnull(pay_manager_good, 0) * @manager_good_rate + (isnull(pay_point_srv, 0) - isnull(pay_point_good, 0)) * @point_rate + isnull(pay_point_good, 0) * @point_good_rate + (isnull(pay_user01_srv, 0) - isnull(pay_user01_good, 0)) * @user01_rate + isnull(pay_user01_good, 0) * @user01_good_rate + (isnull(pay_user02_srv, 0) - isnull(pay_user02_good, 0)) * @user02_rate + isnull(pay_user02_good, 0) * @user02_good_rate + (isnull(pay_user03_srv, 0) - isnull(pay_user03_good, 0)) * @user03_rate + isnull(pay_user03_good, 0) * @user03_good_rate + (isnull(pay_user04_srv, 0) - isnull(pay_user04_good, 0)) * @user04_rate + isnull(pay_user04_good, 0) * @user04_good_rate + (isnull(pay_user05_srv, 0) - isnull(pay_user05_good, 0)) * @user05_rate + isnull(pay_user05_good, 0) * @user05_good_rate + (isnull(pay_user06_srv, 0) - isnull(pay_user06_good, 0)) * isnull(@user06_rate,1) + isnull(pay_user06_good, 0) * isnull(@user06_good_rate,1) + (isnull(pay_user07_srv, 0) - isnull(pay_user07_good, 0)) * isnull(@user07_rate,1) + isnull(pay_user07_good, 0) * isnull(@user07_good_rate,1) + (isnull(pay_user08_srv, 0) - isnull(pay_user08_good, 0)) * isnull(@user08_rate,1) + isnull(pay_user08_good, 0) * isnull(@user08_good_rate,1) + (isnull(pay_user09_srv, 0) - isnull(pay_user09_good, 0)) * isnull(@user09_rate,1) + isnull(pay_user09_good, 0) * isnull(@user09_good_rate,1) + (isnull(pay_user10_srv, 0) - isnull(pay_user10_good, 0)) * isnull(@user10_rate,1) + isnull(pay_user10_good, 0) * isnull(@user10_good_rate,1) + (isnull(pay_user11_srv, 0) - isnull(pay_user11_good, 0)) * isnull(@user11_rate,1) + isnull(pay_user11_good, 0) * isnull(@user11_good_rate,1) + (isnull(pay_user12_srv, 0) - isnull(pay_user12_good, 0)) * isnull(@user12_rate,1) + isnull(pay_user12_good, 0) * isnull(@user12_good_rate,1) + (isnull(pay_user13_srv, 0) - isnull(pay_user13_good, 0)) * isnull(@user13_rate,1) + isnull(pay_user13_good, 0) * isnull(@user13_good_rate,1) + (isnull(pay_user14_srv, 0) - isnull(pay_user14_good, 0)) * isnull(@user14_rate,1) + isnull(pay_user14_good, 0) * isnull(@user14_good_rate,1) + (isnull(pay_user15_srv, 0) - isnull(pay_user15_good, 0)) * isnull(@user15_rate,1) + isnull(pay_user15_good, 0) * isnull(@user15_good_rate,1) where comp = @compfrom end end fetch cur_get_compid_2 into @compfrom end close cur_get_compid_2 deallocate cur_get_compid_2 end --门店简称 update a set a.comp = a.comp +'('+ isnull(b.gae03c,'') + ')' from #stat_byday a, gam05 b where a.comp = b.gae01c select isnull(comp,'') comp, isnull(cash_card,'') cash_card, isnull(cash_srv,'') cash_srv, isnull(cash_good,'') cash_good, isnull(cash_total,'') cash_total, isnull(credit_card,'') credit_card, isnull(credit_srv,'') credit_srv, isnull(credit_good,'') credit_good, isnull(credit_total,'') credit_total, isnull(zfb_card,'') zfb_card, isnull(zfb_srv,'') zfb_srv, isnull(zfb_good,'') zfb_good, isnull(zfb_total,'') zfb_total, isnull(wx_card,'') wx_card, isnull(wx_srv,'') wx_srv, isnull(wx_good,'') wx_good, isnull(wx_total,'') wx_total, isnull(revene,'') revene, isnull(outcome,'') outcome, isnull(profit,'') profit, isnull(defict,'') defict, isnull(returnamt,'') returnamt, isnull(pay_card_srv,'') pay_card_srv, isnull(pay_card_good,'') pay_card_good, isnull(pay_coupon,'') pay_coupon, isnull(pay_coupon2,'') pay_coupon2, isnull(pay_coupon3,'') pay_coupon3, isnull(pay_manager,'') pay_manager, isnull(sale_card_total,'') sale_card_total, isnull(pay_card_srv_act,'') pay_card_srv_act, isnull(pay_card_srv_work,'') pay_card_srv_work, isnull(work_perf,'') work_perf, isnull(pay_manager_prj,'') pay_manager_prj, isnull(pay_coupon_srv,'') pay_coupon_srv, isnull(pay_coupon2_srv,'') pay_coupon2_srv, isnull(pay_coupon3_srv,'') pay_coupon3_srv, isnull(pay_manager_good,'') pay_manager_good, isnull(pay_coupon_good,'') pay_coupon_good, isnull(pay_coupon2_good,'') pay_coupon2_good, isnull(pay_coupon3_good,'') pay_coupon3_good, isnull(pay_point,'') pay_point, isnull(pay_point_srv,'') pay_point_srv, isnull(pay_point_good,'') pay_point_good, isnull(pay_user01,'') pay_user01, isnull(pay_user01_srv,'') pay_user01_srv, isnull(pay_user01_good,'') pay_user01_good, isnull(pay_user02,'') pay_user02, isnull(pay_user02_srv,'') pay_user02_srv, isnull(pay_user02_good,'') pay_user02_good, isnull(pay_user03,'') pay_user03, isnull(pay_user03_srv,'') pay_user03_srv, isnull(pay_user03_good,'') pay_user03_good, isnull(pay_user04,'') pay_user04, isnull(pay_user04_srv,'') pay_user04_srv, isnull(pay_user04_good,'') pay_user04_good, isnull(pay_user05,'') pay_user05, isnull(pay_user05_srv,'') pay_user05_srv, isnull(pay_user05_good,'') pay_user05_good, isnull(pay_user06,'') pay_user06, isnull(pay_user06_srv,'') pay_user06_srv, isnull(pay_user06_good,'') pay_user06_good, isnull(pay_user07,'') pay_user07, isnull(pay_user07_srv,'') pay_user07_srv, isnull(pay_user07_good,'') pay_user07_good, isnull(pay_user08,'') pay_user08, isnull(pay_user08_srv,'') pay_user08_srv, isnull(pay_user08_good,'') pay_user08_good, isnull(pay_user09,'') pay_user09, isnull(pay_user09_srv,'') pay_user09_srv, isnull(pay_user09_good,'') pay_user09_good, isnull(pay_user10,'') pay_user10, isnull(pay_user10_srv,'') pay_user10_srv, isnull(pay_user10_good,'') pay_user10_good, isnull(pay_user11,'') pay_user11, isnull(pay_user11_srv,'') pay_user11_srv, isnull(pay_user11_good,'') pay_user11_good, isnull(pay_user12,'') pay_user12, isnull(pay_user12_srv,'') pay_user12_srv, isnull(pay_user12_good,'') pay_user12_good, isnull(pay_user13,'') pay_user13, isnull(pay_user13_srv,'') pay_user13_srv, isnull(pay_user13_good,'') pay_user13_good, isnull(pay_user14,'') pay_user14, isnull(pay_user14_srv,'') pay_user14_srv, isnull(pay_user14_good,'') pay_user14_good, isnull(pay_user15,'') pay_user15, isnull(pay_user15_srv,'') pay_user15_srv, isnull(pay_user15_good,'') pay_user15_good, isnull(other_income,'') other_income, isnull(guestflux,'') guestflux, isnull(guestcount,'') guestcount, isnull(guestprice,'') guestprice, isnull(good_total,0) good_total from #stat_byday order by comp drop table #stat_byday drop table #tbl_profit_day end GO /****** Object: StoredProcedure [dbo].[upg_create_gsm03_from_gcm10_all] Script Date: 06/12/2017 06:19:53 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_create_gsm03_from_gcm10_all] ( @fromdate varchar(10), @todate varchar(10) ) as begin declare @compid varchar(10) declare cur_each_comp cursor for select gae01c from gam05 open cur_each_comp fetch cur_each_comp into @compid while @@fetch_status = 0 begin if exists(select 1 from gcm01 with (nolock) where gca00c = @compid) begin exec upg_create_gsm03_from_gcm10 @compid, @fromdate, @todate end fetch cur_each_comp into @compid end close cur_each_comp deallocate cur_each_comp end GO /****** Object: StoredProcedure [dbo].[upg_all_profit_analysis_ex] Script Date: 06/12/2017 06:19:25 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_all_profit_analysis_ex] ( @compOur varchar(10), --本店 @compfrom varchar(10), @compto varchar(10), @fromdate varchar(10), @todate varchar(10), @calmode int ) as --WUXM 2006/02/07 begin declare @amt float declare @tmpamt float -- 增加劳动业绩的计算 -- 店类型 1美容2美发3SPA4足浴6专柜 declare @sp056 varchar(20) -- 0-按项目支付1-按单据支付 declare @sp055f float declare @isadd_0 float declare @isadd_3 float declare @isadd_$ float -- 定义劳动业绩的比率 declare @cash_rate float -- 现金:1 declare @cash_good_rate float -- 现金:1 declare @credit_rate float -- 银行卡支付:6 declare @credit_good_rate float -- 银行卡支付:6 declare @pay3_rate float -- 支付方式:3 declare @pay3_good_rate float -- 支付方式:3 declare @pay$_rate float -- 支付方式:$ declare @pay$_good_rate float -- 支付方式:$ declare @pay0_rate float -- 内调:0 declare @pay0_good_rate float -- 内调:0 declare @manager_rate float -- 经理签单:8 declare @manager_good_rate float -- 经理签单:8 declare @point_rate float -- 积分支付:7 declare @point_good_rate float -- 积分支付:7 declare @user01_rate float -- 自定义支付:$1 declare @user01_good_rate float -- 自定义支付:$1 declare @user02_rate float -- 自定义支付:$2 declare @user02_good_rate float -- 自定义支付:$2 declare @user03_rate float -- 自定义支付:$3 declare @user03_good_rate float -- 自定义支付:$3 declare @user04_rate float -- 自定义支付:$4 declare @user04_good_rate float -- 自定义支付:$4 declare @user05_rate float -- 自定义支付:$5 declare @user05_good_rate float -- 自定义支付:$5 declare @user06_rate float -- 自定义支付:$6 declare @user06_good_rate float -- 自定义支付:$6 declare @user07_rate float -- 自定义支付:$7 declare @user07_good_rate float -- 自定义支付:$7 declare @user08_rate float -- 自定义支付:$8 declare @user08_good_rate float -- 自定义支付:$8 declare @zfb_rate float -- 支付宝支付 declare @zfb_good_rate float -- 支付宝支付 declare @wx_rate float -- 微信支付 declare @wx_good_rate float -- 微信支付 declare @guestflux float -- 客流数 declare @guestcount float -- 客数 declare @guestprice float -- 客单价 create table #tbl_profit_day ( idd int not null, code varchar(20) not null, itemname varchar(60) null, is_add int null, amt float null, is_leaf int null, payment varchar(10) null, -- 支付方式 kind int null -- 0 充值 1卡销售 9产品销售 2 服务 -1 取款 8 保证金收入 6 还款 ) create table #stat_byday ( comp varchar(80) not null, cash_card float null, cash_srv float null, cash_good float null, cash_total float null, credit_card float null, credit_srv float null, credit_good float null, credit_total float null, zfb_card float null, zfb_srv float null, zfb_good float null, zfb_total float null, wx_card float null, wx_srv float null, wx_good float null, wx_total float null, revene float null, outcome float null, profit float null, defict float null, returnamt float null, pay_card_srv float null, pay_card_good float null, pay_coupon float null, -- 券付 pay_coupon2 float null, -- 贵付 pay_coupon3 float null, -- 洗头 内调 pay_manager float null, -- 经理签单 sale_card_total float null, -- 总销售卡 pay_card_srv_act float null, -- 卡付实际营业额 = 在服务项目 + 产品中,那些卡付的金额转换为实际营业额 pay_card_srv_work float null, -- 卡付实际劳动业绩 = 在服务项目 + 产品中,那些卡付的金额转换为劳动业绩 work_perf float null, -- 劳动业绩 = 现金(服务) + 现金(物品) + 卡付(物品) + 卡付实营业额 + 项目或物品经理签单 + 内调 + 抵用券 + 银行卡(服务) + 银行卡(物品) pay_manager_prj float null, --只包括项目的经理签单 pay_coupon_srv float null, -- 券付 支付方式3, 只包括项目(含产品) pay_coupon2_srv float null, -- 贵付 支付方式$, 只包括项目(含产品) pay_coupon3_srv float null, -- 洗头 支付方式内调, 只包括项目(含产品) pay_manager_good float null, --只包括产品的经理签单 --modify by limin -2014-12-23 pay_coupon_good float null, -- 券付 支付方式3, 只包括产品 --modify by limin -2014-12-23 pay_coupon2_good float null, -- 贵付 支付方式$, 只包括产品 --modify by limin -2014-12-23 pay_coupon3_good float null, -- 洗头 支付方式内调, 只包括产品 --modify by limin -2014-12-23 pay_point float null, -- 积分支付,卡销售,项目,产品 pay_point_srv float null, -- 积分支付,只包括项目(含产品) pay_point_good float null, -- 积分支付,只有产品 --modify by limin -2014-12-23 pay_user01 float null, -- 自定义支付,卡销售,项目,产品 pay_user01_srv float null, -- 自定义支付,只包括项目(含产品) pay_user01_good float null, -- 自定义支付,只有产品 pay_user02 float null, -- 自定义支付,卡销售,项目,产品 pay_user02_srv float null, -- 自定义支付,只包括项目(含产品) pay_user02_good float null, -- 自定义支付,只有产品 pay_user03 float null, -- 自定义支付,卡销售,项目,产品 pay_user03_srv float null, -- 自定义支付,只包括项目(含产品) pay_user03_good float null, -- 自定义支付,只有产品 pay_user04 float null, -- 自定义支付,卡销售,项目,产品 pay_user04_srv float null, -- 自定义支付,只包括项目(含产品) pay_user04_good float null, -- 自定义支付,只有产品 pay_user05 float null, -- 自定义支付,卡销售,项目,产品 pay_user05_srv float null, -- 自定义支付,只包括项目(含产品) pay_user05_good float null, -- 自定义支付,只有产品 pay_user06 float null, -- 自定义支付,卡销售,项目,产品 pay_user06_srv float null, -- 自定义支付,只包括项目(含产品) pay_user06_good float null, -- 自定义支付,只有产品 pay_user07 float null, -- 自定义支付,卡销售,项目,产品 pay_user07_srv float null, -- 自定义支付,只包括项目(含产品) pay_user07_good float null, -- 自定义支付,只有产品 pay_user08 float null, -- 自定义支付,卡销售,项目,产品 pay_user08_srv float null, -- 自定义支付,只包括项目(含产品) pay_user08_good float null, -- 自定义支付,只有产品 other_income float null, -- 其他收入 Huzy 2012/06/04 good_total float null, -- 产品总销售 guestflux float null, guestcount float null, guestprice float null, primary key(comp) ) declare @id int declare @comp varchar(10) -- 总店 if (isnull(@compfrom, '') = '*' or isnull(@compfrom, '') = '') begin declare cur_get_compid cursor for select gae01c from gam05 with(nolock) open cur_get_compid fetch cur_get_compid into @comp while @@fetch_status = 0 begin select @isadd_0 = gso07f from gsm15 with (nolock) where gso00c = @comp and gso02c = '0' select @isadd_0 = isnull(@isadd_0, 0) select @isadd_3 = gso07f from gsm15 with (nolock) where gso00c = @comp and gso02c = '3' select @isadd_3 = isnull(@isadd_3, 0) select @isadd_$ = gso07f from gsm15 with (nolock) where gso00c = @comp and gso02c = '$' select @isadd_$ = isnull(@isadd_$, 0) truncate table #tbl_profit_day insert #tbl_profit_day(idd, code, itemname, is_add, amt, is_leaf, payment, kind) exec upg_analyse_profit @comp, @fromdate, @todate, 1, '*', @calmode insert #stat_byday(comp) values(@comp) -- 转换为汇总表需要的一条记录 -- 现金(卖卡) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110101' or code = '110104' or code = '110107' or code = '110118' or code = '110140' -- 现金取款 select @tmpamt = 0 select @tmpamt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110119' update a set a.cash_card = isnull(@amt, 0) + isnull(@tmpamt, 0) from #stat_byday a where comp = @comp -- 现金(服务) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110201' or code = '110230' -- 项目或物品的现金还款也算 还款的不算在 select @tmpamt = 0 select @tmpamt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110301' or code = '110310' -- 现金的产品销售 以及 产品还款 update a set a.cash_srv = isnull(@amt, 0) - isnull(@tmpamt, 0) from #stat_byday a where comp = @comp -- 经理签单,只包括项目 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110203' update a set a.pay_manager_prj = isnull(@amt, 0) from #stat_byday a where comp = @comp -- 卡付实际营业额 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110206' update a set a.pay_card_srv_act = isnull(@amt, 0) from #stat_byday a where comp = @comp select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '11AA01' update a set a.pay_card_srv_work = isnull(@amt, 0) from #stat_byday a where comp = @comp -- 银行卡(卖卡) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110102' or code = '110105' or code = '110108' or code = '110141' update a set a.credit_card = isnull(@amt, 0) from #stat_byday a where comp = @comp -- 支付宝(卖卡) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '1101Z1' or code = '1101Z2' or code = '1101Z3' update a set a.zfb_card = isnull(@amt, 0) from #stat_byday a where comp = @comp -- 微信(卖卡) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '1101W1' or code = '1101W2' or code = '1101W3' update a set a.wx_card = isnull(@amt, 0) from #stat_byday a where comp = @comp -- 不管支付方式,实际上的卡销售额 select @amt = 0 select @amt = sum(isnull(amt, 0) * is_add) from #tbl_profit_day where code = '110101' or code = '110102' or code = '110104' or code = '110105' or code = '110107' or code = '110108' or code = '110115' or code = '110116' or code = '110117' or code = '110119' or code = '110120' or code = '110121' or code = '110122' or code = '110123' or code = '110125' or code = '110126' or code = '110127' or code = '110130' or code = '110131' or code = '110132' or code = '1101Z1' or code = '1101Z2' or code = '1101Z3' -- 最后2行是新增加的条件,20061130 update a set a.sale_card_total = isnull(@amt, 0) from #stat_byday a where comp = @comp -- 银行卡(服务) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110202' or code = '110231' --项目或物品的银行卡还款不算 select @tmpamt = 0 select @tmpamt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110306' or code = '110311' -- 产品部分的银行卡支付 以及 银行卡还款 update a set a.credit_srv = isnull(@amt, 0) - isnull(@tmpamt, 0) from #stat_byday a where comp = @comp -- 支付宝(服务) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '1102Z1' select @tmpamt = 0 select @tmpamt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '1103Z1' update a set a.zfb_srv = isnull(@amt, 0) - isnull(@tmpamt, 0) from #stat_byday a where comp = @comp -- 微信(服务) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '1102W1' select @tmpamt = 0 select @tmpamt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '1103W1' update a set a.wx_srv = isnull(@amt, 0) - isnull(@tmpamt, 0) from #stat_byday a where comp = @comp -- 其他收入 Huzy 2012/06/04 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '18' update a set a.other_income = isnull(@amt, 0) from #stat_byday a -- 支出 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '12' -- 房租不考虑在内 select @tmpamt = 0 select @tmpamt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '1299' update a set a.outcome = isnull(@amt, 0) - isnull(@tmpamt, 0) from #stat_byday a where comp = @comp -- 欠款 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110103' or code = '110106' or code = '110207' update a set a.defict = isnull(@amt, 0) from #stat_byday a where comp = @comp -- 还款 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110107' or code = '110108' update a set a.returnamt = isnull(@amt, 0) from #stat_byday a where comp = @comp -- 卡付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110205' select @tmpamt = 0 select @tmpamt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110302' update a set a.pay_card_srv = isnull(@amt, 0) - isnull(@tmpamt, 0) from #stat_byday a where comp = @comp -- 积分支付, 买项目或产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '11020507' -- 积分支付 update a set a.pay_point_srv = isnull(@amt, 0) from #stat_byday a where comp = @comp -- 积分支付, 卡充值,买项目或产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '11020507' or code = '110133' -- 积分支付 update a set a.pay_point = isnull(@amt, 0) from #stat_byday a where comp = @comp -- 抵用券付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110204' or code = '110125' or code = '110126' or code = '110127') update a set a.pay_coupon = isnull(@amt, 0) from #stat_byday a where comp = @comp -- 抵用券付, 只包括项目 + 产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110204' update a set a.pay_coupon_srv = isnull(@amt, 0) from #stat_byday a where comp = @comp -- 贵付(抵用券二) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110208' or code = '110130' or code = '110131' or code = '110132') update a set a.pay_coupon2 = isnull(@amt, 0) from #stat_byday a where comp = @comp -- 贵付(抵用券二), 只包括项目 + 产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110208' update a set a.pay_coupon2_srv = isnull(@amt, 0) from #stat_byday a where comp = @comp -- 内调(洗头) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110209' or code = '110120' or code = '110121' or code = '110122') update a set a.pay_coupon3 = isnull(@amt, 0) from #stat_byday a where comp = @comp -- 内调(洗头), 只包括项目 + 产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110209' update a set a.pay_coupon3_srv = isnull(@amt, 0) from #stat_byday a where comp = @comp -- 经理签单 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110110' or code = '110111' or code = '110203') update a set a.pay_manager = isnull(@amt, 0) from #stat_byday a where comp = @comp ----------------------------------------------$1 -> $8----------------------------------------------------------------------------- --自定义支付, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$1' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8) update a set a.pay_user01 = isnull(@amt, 0) from #stat_byday a where comp = @comp --自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$1' and (kind = 2) update a set a.pay_user01_srv = isnull(@amt, 0) from #stat_byday a where comp = @comp --自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$1' and (kind = 9) update a set a.pay_user01_good = isnull(@amt, 0) from #stat_byday a where comp = @comp --自定义支付, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$2' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8) update a set a.pay_user02 = isnull(@amt, 0) from #stat_byday a where comp = @comp --自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$2' and (kind = 2) update a set a.pay_user02_srv = isnull(@amt, 0) from #stat_byday a where comp = @comp --自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$2' and (kind = 9) update a set a.pay_user02_good = isnull(@amt, 0) from #stat_byday a where comp = @comp --自定义支付, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$3' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8 or kind = -1) update a set a.pay_user03 = isnull(@amt, 0) from #stat_byday a where comp = @comp --自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$3' and (kind = 2) update a set a.pay_user03_srv = isnull(@amt, 0) from #stat_byday a where comp = @comp --自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$3' and (kind = 9) update a set a.pay_user03_good = isnull(@amt, 0) from #stat_byday a where comp = @comp --自定义支付, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$4' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8) update a set a.pay_user04 = isnull(@amt, 0) from #stat_byday a where comp = @comp --自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$4' and (kind = 2) update a set a.pay_user04_srv = isnull(@amt, 0) from #stat_byday a where comp = @comp --自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$4' and (kind = 9) update a set a.pay_user04_good = isnull(@amt, 0) from #stat_byday a where comp = @comp --自定义支付, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$5' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8) update a set a.pay_user05 = isnull(@amt, 0) from #stat_byday a where comp = @comp --自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$5' and (kind = 2) update a set a.pay_user05_srv = isnull(@amt, 0) from #stat_byday a where comp = @comp --自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$5' and (kind = 9) update a set a.pay_user05_good = isnull(@amt, 0) from #stat_byday a where comp = @comp --自定义支付, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$6' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8) update a set a.pay_user06 = isnull(@amt, 0) from #stat_byday a where comp = @comp --自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$6' and (kind = 2) update a set a.pay_user06_srv = isnull(@amt, 0) from #stat_byday a where comp = @comp --自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$6' and (kind = 9) update a set a.pay_user06_good = isnull(@amt, 0) from #stat_byday a where comp = @comp --自定义支付, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$7' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8) update a set a.pay_user07 = isnull(@amt, 0) from #stat_byday a where comp = @comp --自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$7' and (kind = 2) update a set a.pay_user07_srv = isnull(@amt, 0) from #stat_byday a where comp = @comp --自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$7' and (kind = 9) update a set a.pay_user07_good = isnull(@amt, 0) from #stat_byday a where comp = @comp --自定义支付, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$8' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8) update a set a.pay_user08 = isnull(@amt, 0) from #stat_byday a where comp = @comp --自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$8' and (kind = 2) update a set a.pay_user08_srv = isnull(@amt, 0) from #stat_byday a where comp = @comp --自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$8' and (kind = 9) update a set a.pay_user08_good = isnull(@amt, 0) from #stat_byday a where comp = @comp ----------------------------------------------------------------------------------------------------------------------------------- -- 产品购买的现金 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110301' or code = '110310' -- 包括产品现金的还款 update a set a.cash_good = isnull(@amt, 0) from #stat_byday a where comp = @comp -- 产品购买的银行卡 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110306' or code = '110311' -- 包括产品银行卡的还款 update a set a.credit_good = isnull(@amt, 0) from #stat_byday a where comp = @comp -- 产品购买的支付宝 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '1103Z1' or code = '1103Z2' -- 包括产品支付宝的还款 update a set a.zfb_good = isnull(@amt, 0) from #stat_byday a where comp = @comp -- 产品购买的微信 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '1103W1' or code = '1103W2' -- 包括产品微信的还款 update a set a.wx_good = isnull(@amt, 0) from #stat_byday a where comp = @comp -- 产品购买的卡付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110302' update a set a.pay_card_good = isnull(@amt, 0) from #stat_byday a where comp = @comp -- 产品总销售 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '1103' update a set a.good_total = isnull(@amt, 0) from #stat_byday a where comp = @comp update #stat_byday set cash_total = isnull(cash_card, 0) + isnull(cash_srv, 0) + isnull(cash_good, 0), credit_total = isnull(credit_card, 0) + isnull(credit_srv, 0) + isnull(credit_good, 0), zfb_total = isnull(zfb_card, 0) + isnull(zfb_srv, 0) + isnull(zfb_good, 0), wx_total = isnull(wx_card, 0) + isnull(wx_srv, 0) + isnull(wx_good, 0) where comp = @comp -- 收入都同利润分析表中的收入 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '11' -- 收入 update #stat_byday set revene = @amt where comp = @comp update #stat_byday set profit = isnull(revene, 0) + isnull(other_income, 0) - isnull(outcome, 0) where comp = @comp select @guestflux = 0, @guestcount = 0, @guestprice = 0 -- 客流数,客数,客单价 select @guestflux = count(gga01c), @guestcount =sum(gga14f), @guestprice = round(dbo.fun_divide_default(sum(gga16f), sum(gga14f), 0), 2) from ggm01 with(nolock) where gga00c = @comp and gga02d >= @fromdate and gga02d <= @todate update #stat_byday set guestflux = @guestflux, guestcount = @guestcount, guestprice = @guestprice where comp = @comp select @sp056 = gsa02c from gsm01 with(nolock) where gsa00c = @comp and gsa01c = 'SP056' select @sp055f = gsa03f from gsm01 with(nolock) where gsa00c = @comp and gsa01c = 'SP055' --查询支付方式$6设置的比率 如果不需要计算劳动业绩 返回0 select @user06_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso09f, 0) * isnull(gso03i,1) end, @user06_good_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso10f, 0) * isnull(gso06f,1) end from gsm15 with (nolock) where gso00c = @comp and gso02c = '$6' --查询支付方式$7设置的比率 如果不需要计算劳动业绩 返回0 select @user07_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso09f, 0) * isnull(gso03i,1) end, @user07_good_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso10f, 0) * isnull(gso06f,1) end from gsm15 with (nolock) where gso00c = @comp and gso02c = '$7' --查询支付方式$8设置的比率 如果不需要计算劳动业绩 返回0 select @user08_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso09f, 0) * isnull(gso03i,1) end, @user08_good_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso10f, 0) * isnull(gso06f,1) end from gsm15 with (nolock) where gso00c = @comp and gso02c = '$8' exec upg_get_workperf_rate @comp, @cash_rate output, @cash_good_rate output, @credit_rate output, @credit_good_rate output, @pay3_rate output, @pay3_good_rate output, @pay$_rate output, @pay$_good_rate output, @pay0_rate output, @pay0_good_rate output, @manager_rate output, @manager_good_rate output, @point_rate output, @point_good_rate output, @user01_rate output, @user01_good_rate output, @user02_rate output, @user02_good_rate output, @user03_rate output, @user03_good_rate output, @user04_rate output, @user04_good_rate output, @user05_rate output, @user05_good_rate output, @zfb_rate output, @zfb_good_rate output, @wx_rate output, @wx_good_rate output if @sp056 = '4' -- 足疗 begin -- 足疗的劳动业绩 -- 劳动业绩 = 现金(服务) + 现金(物品) + 卡付(物品) + 卡付 + 抵用券 + 银行卡(服务) + 银行卡(物品) + 积分支付(项目 + 产品)--modify by limin -2014-12-23 update #stat_byday set work_perf = isnull(cash_srv, 0) * @cash_rate + isnull(cash_good, 0) * @cash_good_rate + isnull(pay_card_srv, 0) + isnull(pay_card_good, 0) + isnull(credit_srv, 0) * @credit_rate + isnull(credit_good, 0) * @credit_good_rate + isnull(zfb_srv, 0) * @zfb_rate + isnull(zfb_good, 0) * @zfb_good_rate + isnull(wx_srv, 0) * @wx_rate + isnull(wx_good, 0) * @wx_good_rate + (isnull(pay_coupon_srv, 0) - isnull(pay_coupon_good, 0)) * @pay3_rate + isnull(pay_coupon_good, 0) * @pay3_good_rate + (isnull(pay_point_srv, 0) - isnull(pay_point_good, 0)) * @point_rate + isnull(pay_point_good, 0) * @point_good_rate + (isnull(pay_user01_srv, 0) - isnull(pay_user01_good, 0)) * @user01_rate + isnull(pay_user01_good, 0) * @user01_good_rate + (isnull(pay_user02_srv, 0) - isnull(pay_user02_good, 0)) * @user02_rate + isnull(pay_user02_good, 0) * @user02_good_rate + (isnull(pay_user03_srv, 0) - isnull(pay_user03_good, 0)) * @user03_rate + isnull(pay_user03_good, 0) * @user03_good_rate + (isnull(pay_user04_srv, 0) - isnull(pay_user04_good, 0)) * @user04_rate + isnull(pay_user04_good, 0) * @user04_good_rate + (isnull(pay_user05_srv, 0) - isnull(pay_user05_good, 0)) * @user05_rate + isnull(pay_user05_good, 0) * @user05_good_rate + (isnull(pay_user06_srv, 0) - isnull(pay_user06_good, 0)) * isnull(@user06_rate,1) + isnull(pay_user06_good, 0) * isnull(@user06_good_rate,1) + (isnull(pay_user07_srv, 0) - isnull(pay_user07_good, 0)) * isnull(@user07_rate,1) + isnull(pay_user07_good, 0) * isnull(@user07_good_rate,1) + (isnull(pay_user08_srv, 0) - isnull(pay_user08_good, 0)) * isnull(@user08_rate,1) + isnull(pay_user08_good, 0) * isnull(@user08_good_rate,1) where comp = @comp end else begin if @sp055f = 1 begin -- 按单据支付 update #stat_byday set work_perf = isnull(cash_srv, 0) * @cash_rate + isnull(cash_good, 0) * @cash_good_rate + isnull(pay_card_srv, 0) + isnull(pay_card_good, 0) + isnull(credit_srv, 0) * @credit_rate + isnull(credit_good, 0) * @credit_good_rate + isnull(zfb_srv, 0) * @zfb_rate + isnull(zfb_good, 0) * @zfb_good_rate + isnull(wx_srv, 0) * @wx_rate + isnull(wx_good, 0) * @wx_good_rate + (isnull(pay_coupon_srv, 0) - isnull(pay_coupon_good, 0)) * @pay3_rate + isnull(pay_coupon_good, 0) * @pay3_good_rate + (isnull(pay_coupon2_srv, 0) - isnull(pay_coupon2_good, 0)) * @pay$_rate + isnull(pay_coupon2_good, 0) * @pay$_good_rate + (isnull(pay_coupon3_srv, 0) - isnull(pay_coupon3_good, 0)) * @pay0_rate + isnull(pay_coupon3_good, 0) * @pay0_good_rate + (isnull(pay_manager_prj, 0) - isnull(pay_manager_good, 0)) * @manager_rate + isnull(pay_manager_good, 0) * @manager_good_rate + (isnull(pay_point_srv, 0) - isnull(pay_point_good, 0)) * @point_rate + isnull(pay_point_good, 0) * @point_good_rate + (isnull(pay_user01_srv, 0) - isnull(pay_user01_good, 0)) * @user01_rate + isnull(pay_user01_good, 0) * @user01_good_rate + (isnull(pay_user02_srv, 0) - isnull(pay_user02_good, 0)) * @user02_rate + isnull(pay_user02_good, 0) * @user02_good_rate + (isnull(pay_user03_srv, 0) - isnull(pay_user03_good, 0)) * @user03_rate + isnull(pay_user03_good, 0) * @user03_good_rate + (isnull(pay_user04_srv, 0) - isnull(pay_user04_good, 0)) * @user04_rate + isnull(pay_user04_good, 0) * @user04_good_rate + (isnull(pay_user05_srv, 0) - isnull(pay_user05_good, 0)) * @user05_rate + isnull(pay_user05_good, 0) * @user05_good_rate + (isnull(pay_user06_srv, 0) - isnull(pay_user06_good, 0)) * isnull(@user06_rate,1) + isnull(pay_user06_good, 0) * isnull(@user06_good_rate,1) + (isnull(pay_user07_srv, 0) - isnull(pay_user07_good, 0)) * isnull(@user07_rate,1) + isnull(pay_user07_good, 0) * isnull(@user07_good_rate,1) + (isnull(pay_user08_srv, 0) - isnull(pay_user08_good, 0)) * isnull(@user08_rate,1) + isnull(pay_user08_good, 0) * isnull(@user08_good_rate,1) where comp = @comp end else begin -- 劳动业绩 = 现金(服务) + 现金(产品) + 卡付(产品) + 卡付实营业额 + 经理签单 + 内调 + 抵用券 + 银行卡(服务) + 银行卡(产品) update #stat_byday set work_perf = isnull(cash_srv, 0) * @cash_rate + isnull(cash_good, 0) * @cash_good_rate + isnull(pay_card_srv_work, 0) + isnull(credit_srv, 0) * @credit_rate + isnull(credit_good, 0) * @credit_good_rate + isnull(zfb_srv, 0) * @zfb_rate + isnull(zfb_good, 0) * @zfb_good_rate + isnull(wx_srv, 0) * @wx_rate + isnull(wx_good, 0) * @wx_good_rate + (isnull(pay_coupon_srv, 0) - isnull(pay_coupon_good, 0)) * @pay3_rate + isnull(pay_coupon_good, 0) * @pay3_good_rate + (isnull(pay_coupon2_srv, 0) - isnull(pay_coupon2_good, 0)) * @pay$_rate + isnull(pay_coupon2_good, 0) * @pay$_good_rate + (isnull(pay_coupon3_srv, 0) - isnull(pay_coupon3_good, 0)) * @pay0_rate + isnull(pay_coupon3_good, 0) * @pay0_good_rate + (isnull(pay_manager_prj, 0) - isnull(pay_manager_good, 0)) * @manager_rate + isnull(pay_manager_good, 0) * @manager_good_rate + (isnull(pay_point_srv, 0) - isnull(pay_point_good, 0)) * @point_rate + isnull(pay_point_good, 0) * @point_good_rate + (isnull(pay_user01_srv, 0) - isnull(pay_user01_good, 0)) * @user01_rate + isnull(pay_user01_good, 0) * @user01_good_rate + (isnull(pay_user02_srv, 0) - isnull(pay_user02_good, 0)) * @user02_rate + isnull(pay_user02_good, 0) * @user02_good_rate + (isnull(pay_user03_srv, 0) - isnull(pay_user03_good, 0)) * @user03_rate + isnull(pay_user03_good, 0) * @user03_good_rate + (isnull(pay_user04_srv, 0) - isnull(pay_user04_good, 0)) * @user04_rate + isnull(pay_user04_good, 0) * @user04_good_rate + (isnull(pay_user05_srv, 0) - isnull(pay_user05_good, 0)) * @user05_rate + isnull(pay_user05_good, 0) * @user05_good_rate + (isnull(pay_user06_srv, 0) - isnull(pay_user06_good, 0)) * isnull(@user06_rate,1) + isnull(pay_user06_good, 0) * isnull(@user06_good_rate,1) + (isnull(pay_user07_srv, 0) - isnull(pay_user07_good, 0)) * isnull(@user07_rate,1) + isnull(pay_user07_good, 0) * isnull(@user07_good_rate,1) + (isnull(pay_user08_srv, 0) - isnull(pay_user08_good, 0)) * isnull(@user08_rate,1) + isnull(pay_user08_good, 0) * isnull(@user08_good_rate,1) where comp = @comp end end fetch cur_get_compid into @comp end close cur_get_compid deallocate cur_get_compid end -- 单店 else begin declare cur_get_compid_2 cursor for select gaz02c from gam26 where gaz01c = @compfrom --and gaz02c between @compfrom and @compto -- select gae01c from gam05 with(nolock) -- where gae01c >= @compfrom -- and gae01c <= @compto open cur_get_compid_2 fetch cur_get_compid_2 into @compfrom while @@fetch_status = 0 begin select @isadd_0 = gso07f from gsm15 with (nolock) where gso00c = @compfrom and gso02c = '0' select @isadd_0 = isnull(@isadd_0, 0) select @isadd_3 = gso07f from gsm15 with (nolock) where gso00c = @compfrom and gso02c = '3' select @isadd_3 = isnull(@isadd_3, 0) select @isadd_$ = gso07f from gsm15 with (nolock) where gso00c = @compfrom and gso02c = '$' select @isadd_$ = isnull(@isadd_$, 0) delete #tbl_profit_day insert #tbl_profit_day(idd, code, itemname, is_add, amt, is_leaf, payment, kind) exec upg_analyse_profit @compfrom, @fromdate, @todate, 1, '*', @calmode insert #stat_byday(comp) values(@compfrom) -- 转换为汇总表需要的一条记录 -- 现金(卖卡) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110101' or code = '110104' or code = '110107' or code = '110118' or code = '110140' -- 现金取款 select @tmpamt = 0 select @tmpamt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110119' update a set a.cash_card = isnull(@amt, 0) + isnull(@tmpamt, 0) from #stat_byday a where comp = @compfrom ----------------------------------------------$1 -> $8----------------------------------------------------------------------------- --自定义支付, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$1' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8) update a set a.pay_user01 = isnull(@amt, 0) from #stat_byday a where comp = @compfrom --自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$1' and (kind = 2) update a set a.pay_user01_srv = isnull(@amt, 0) from #stat_byday a where comp = @compfrom --自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$1' and (kind = 9) update a set a.pay_user01_good = isnull(@amt, 0) from #stat_byday a where comp = @compfrom --自定义支付, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$2' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8) update a set a.pay_user02 = isnull(@amt, 0) from #stat_byday a where comp = @compfrom --自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$2' and (kind = 2) update a set a.pay_user02_srv = isnull(@amt, 0) from #stat_byday a where comp = @compfrom --自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$2' and (kind = 9) update a set a.pay_user02_good = isnull(@amt, 0) from #stat_byday a where comp = @compfrom --自定义支付, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$3' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8 or kind = -1) update a set a.pay_user03 = isnull(@amt, 0) from #stat_byday a where comp = @compfrom --自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$3' and (kind = 2) update a set a.pay_user03_srv = isnull(@amt, 0) from #stat_byday a where comp = @compfrom --自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$3' and (kind = 9) update a set a.pay_user03_good = isnull(@amt, 0) from #stat_byday a where comp = @compfrom --自定义支付, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$4' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8) update a set a.pay_user04 = isnull(@amt, 0) from #stat_byday a where comp = @compfrom --自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$4' and (kind = 2) update a set a.pay_user04_srv = isnull(@amt, 0) from #stat_byday a where comp = @compfrom --自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$4' and (kind = 9) update a set a.pay_user04_good = isnull(@amt, 0) from #stat_byday a where comp = @compfrom --自定义支付, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$5' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8) update a set a.pay_user05 = isnull(@amt, 0) from #stat_byday a where comp = @compfrom --自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$5' and (kind = 2) update a set a.pay_user05_srv = isnull(@amt, 0) from #stat_byday a where comp = @compfrom --自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$5' and (kind = 9) update a set a.pay_user05_good = isnull(@amt, 0) from #stat_byday a where comp = @compfrom --自定义支付, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$6' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8) update a set a.pay_user06 = isnull(@amt, 0) from #stat_byday a where comp = @compfrom --自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$6' and (kind = 2) update a set a.pay_user06_srv = isnull(@amt, 0) from #stat_byday a where comp = @compfrom --自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$6' and (kind = 9) update a set a.pay_user06_good = isnull(@amt, 0) from #stat_byday a where comp = @compfrom --自定义支付, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$7' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8) update a set a.pay_user07 = isnull(@amt, 0) from #stat_byday a where comp = @compfrom --自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$7' and (kind = 2) update a set a.pay_user07_srv = isnull(@amt, 0) from #stat_byday a where comp = @compfrom --自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$7' and (kind = 9) update a set a.pay_user07_good = isnull(@amt, 0) from #stat_byday a where comp = @compfrom --自定义支付, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$8' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8) update a set a.pay_user08 = isnull(@amt, 0) from #stat_byday a where comp = @compfrom --自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$8' and (kind = 2) update a set a.pay_user08_srv = isnull(@amt, 0) from #stat_byday a where comp = @compfrom --自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$8' and (kind = 9) update a set a.pay_user08_good = isnull(@amt, 0) from #stat_byday a where comp = @compfrom ----------------------------------------------------------------------------------------------------------------------------------- -- 现金(服务) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110201' or code = '110230' select @tmpamt = 0 select @tmpamt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110301' or code = '110310' -- 包括了产品的现金还款 update a set a.cash_srv = isnull(@amt, 0) - isnull(@tmpamt, 0) from #stat_byday a where comp = @compfrom -- 银行卡(卖卡) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110102' or code = '110105' or code = '110108' or code = '110141' update a set a.credit_card = isnull(@amt, 0) from #stat_byday a where comp = @compfrom -- 支付宝(卖卡) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '1101Z1' or code = '1101Z2' or code = '1101Z3' or code = '1101Z4' update a set a.zfb_card = isnull(@amt, 0) from #stat_byday a where comp = @compfrom -- 微信(卖卡) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '1101W1' or code = '1101W2' or code = '1101W3' update a set a.wx_card = isnull(@amt, 0) from #stat_byday a where comp = @compfrom -- 经理签单,只包括项目 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110203' update a set a.pay_manager_prj = isnull(@amt, 0) from #stat_byday a where comp = @compfrom -- 积分支付,只包括项目 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '11020507' update a set a.pay_point_srv = isnull(@amt, 0) from #stat_byday a where comp = @compfrom -- 积分支付,包括卡,项目 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '11020507' or code = '110133') update a set a.pay_point = isnull(@amt, 0) from #stat_byday a where comp = @compfrom -- 卡付实际营业额 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110206' update a set a.pay_card_srv_act = isnull(@amt, 0) from #stat_byday a where comp = @compfrom select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '11AA01' update a set a.pay_card_srv_work = isnull(@amt, 0) from #stat_byday a where comp = @compfrom -- 不管支付方式,实际上的卡销售额 select @amt = 0 select @amt = sum(isnull(amt, 0) * is_add) from #tbl_profit_day where code = '110101' or code = '110102' or code = '110104' or code = '110105' or code = '110107' or code = '110108' or code = '110115' or code = '110116' or code = '110117' or code = '110119' update a set a.sale_card_total = isnull(@amt, 0) from #stat_byday a where comp = @compfrom --银行卡(服务) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110202' or code = '110231' --项目或物品的银行卡还款不算 select @tmpamt = 0 select @tmpamt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110306' or code = '110311' -- 产品销售的银行卡支付 以及 还款 update a set a.credit_srv = isnull(@amt, 0) - isnull(@tmpamt, 0) from #stat_byday a where comp = @compfrom -- 支付宝(服务) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '1102Z1' select @tmpamt = 0 select @tmpamt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '1103Z1' or code = '1103Z2' -- 产品销售的支付宝支付 以及 还款 update a set a.zfb_srv = isnull(@amt, 0) - isnull(@tmpamt, 0) from #stat_byday a where comp = @compfrom -- 微信(服务) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '1102W1' select @tmpamt = 0 select @tmpamt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '1103W1' or code = '1103W2' -- 产品销售的微信支付 以及 还款 update a set a.wx_srv = isnull(@amt, 0) - isnull(@tmpamt, 0) from #stat_byday a where comp = @compfrom -- 其他收入 Huzy 2012/06/04 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '18' update a set a.other_income = isnull(@amt, 0) from #stat_byday a where comp = @compfrom -- 支出 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '12' -- 房租不在当天的支出考虑 select @tmpamt = 0 select @tmpamt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '1299' update a set a.outcome = isnull(@amt, 0) - isnull(@tmpamt, 0) from #stat_byday a where comp = @compfrom -- 欠款 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110103' or code = '110106' or code = '110207' update a set a.defict = isnull(@amt, 0) from #stat_byday a where comp = @compfrom -- 还款 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110107' or code = '110108' update a set a.returnamt = isnull(@amt, 0) from #stat_byday a where comp = @compfrom -- 卡付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110205' select @tmpamt = 0 select @tmpamt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110302' update a set a.pay_card_srv = isnull(@amt, 0) - isnull(@tmpamt, 0) from #stat_byday a where comp = @compfrom -- 抵用券付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110204' or code = '110125' or code = '110126' or code = '110127') update a set a.pay_coupon = isnull(@amt, 0) from #stat_byday a where comp = @compfrom -- 抵用券付, 只包括项目 + 产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110204') update a set a.pay_coupon_srv = isnull(@amt, 0) from #stat_byday a where comp = @compfrom -- 贵付(抵用券二) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110208' or code = '110130' or code = '110131' or code = '110132') update a set a.pay_coupon2 = isnull(@amt, 0) from #stat_byday a where comp = @compfrom -- 贵付(抵用券二), 只包括项目 + 产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110208') update a set a.pay_coupon2_srv = isnull(@amt, 0) from #stat_byday a where comp = @compfrom -- 内调(洗头) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110209' or code = '110120' or code = '110121' or code = '110122') update a set a.pay_coupon3 = isnull(@amt, 0) from #stat_byday a where comp = @compfrom -- 内调(洗头), 只包括项目 + 产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110209') update a set a.pay_coupon3_srv = isnull(@amt, 0) from #stat_byday a where comp = @compfrom -- 经理签单 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110110' or code = '110111' or code = '110203') update a set a.pay_manager = isnull(@amt, 0) from #stat_byday a where comp = @compfrom -- 产品购买的现金 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110301' or code = '110310' -- 包括产品现金的还款 update a set a.cash_good = isnull(@amt, 0) from #stat_byday a where comp = @compfrom -- 产品购买的银行卡 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110306' or code = '110311' -- 包括产品银行卡的还款 update a set a.credit_good = isnull(@amt, 0) from #stat_byday a where comp = @compfrom -- 产品购买的支付宝 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '1103Z1' or code = '1103Z2' -- 包括产品支付宝的还款 update a set a.zfb_good = isnull(@amt, 0) from #stat_byday a where comp = @compfrom -- 产品购买的微信 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '1103W1' or code = '1103W2' -- 包括产品微信的还款 update a set a.wx_good = isnull(@amt, 0) from #stat_byday a where comp = @compfrom -- 产品购买的卡付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110302' update a set a.pay_card_good = isnull(@amt, 0) from #stat_byday a where comp = @compfrom -- 产品总销售 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '1103' update a set a.good_total = isnull(@amt, 0) from #stat_byday a where comp = @compfrom update a set cash_total = isnull(cash_card, 0) + isnull(cash_srv, 0) + isnull(cash_good, 0), credit_total = isnull(credit_card, 0) + isnull(credit_srv, 0) + isnull(credit_good, 0), zfb_total = isnull(zfb_card, 0) + isnull(zfb_srv, 0) + isnull(zfb_good, 0), wx_total = isnull(wx_card, 0) + isnull(wx_srv, 0) + isnull(wx_good, 0) from #stat_byday a where comp = @compfrom -- 收入都同利润分析表中的收入 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '11' -- 收入 update #stat_byday set revene = @amt where comp = @compfrom update #stat_byday set profit = isnull(revene, 0) + isnull(other_income, 0) - isnull(outcome, 0) where comp = @compfrom select @guestflux = 0, @guestcount = 0, @guestprice = 0 -- 客流数,客数,客单价 select @guestflux = count(gga01c), @guestcount =sum(gga14f), @guestprice = round(dbo.fun_divide_default(sum(gga16f), sum(gga14f), 0), 2) from ggm01 with(nolock) where gga00c = @compfrom and gga02d >= @fromdate and gga02d <= @todate update #stat_byday set guestflux = @guestflux, guestcount = @guestcount, guestprice = @guestprice where comp = @compfrom select @sp056 = gsa02c from gsm01 with(nolock) where gsa00c = @compfrom and gsa01c = 'SP056' select @sp055f = gsa03f from gsm01 with(nolock) where gsa00c = @compfrom and gsa01c = 'SP055' --查询支付方式$6设置的比率 如果不需要计算劳动业绩 返回0 select @user06_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso09f, 0) * isnull(gso03i,1) end, @user06_good_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso10f, 0) * isnull(gso06f,1) end from gsm15 with (nolock) where gso00c = @comp and gso02c = '$6' --查询支付方式$7设置的比率 如果不需要计算劳动业绩 返回0 select @user07_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso09f, 0) * isnull(gso03i,1) end, @user07_good_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso10f, 0) * isnull(gso06f,1) end from gsm15 with (nolock) where gso00c = @comp and gso02c = '$7' --查询支付方式$8设置的比率 如果不需要计算劳动业绩 返回0 select @user08_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso09f, 0) * isnull(gso03i,1) end, @user08_good_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso10f, 0) * isnull(gso06f,1) end from gsm15 with (nolock) where gso00c = @comp and gso02c = '$8' exec upg_get_workperf_rate @compfrom, @cash_rate output, @cash_good_rate output, @credit_rate output, @credit_good_rate output, @pay3_rate output, @pay3_good_rate output, @pay$_rate output, @pay$_good_rate output, @pay0_rate output, @pay0_good_rate output, @manager_rate output, @manager_good_rate output, @point_rate output, @point_good_rate output, @user01_rate output, @user01_good_rate output, @user02_rate output, @user02_good_rate output, @user03_rate output, @user03_good_rate output, @user04_rate output, @user04_good_rate output, @user05_rate output, @user05_good_rate output, @zfb_rate output, @zfb_good_rate output, @wx_rate output, @wx_good_rate output if @sp056 = '4' -- 足疗 begin -- 足疗的劳动业绩 -- 劳动业绩 = 现金(服务) + 现金(物品) + 卡付(物品) + 卡付 + 抵用券 + 银行卡(服务) + 银行卡(物品) + 积分支付(项目 + 产品)--modify by limin -2014-12-23 update #stat_byday set work_perf = isnull(cash_srv, 0) * @cash_rate + isnull(cash_good, 0) * @cash_good_rate + isnull(pay_card_srv, 0) + isnull(pay_card_good, 0) + isnull(credit_srv, 0) * @credit_rate + isnull(credit_good, 0) * @credit_good_rate + isnull(zfb_srv, 0) * @zfb_rate + isnull(zfb_good, 0) * @zfb_good_rate + isnull(wx_srv, 0) * @wx_rate + isnull(wx_good, 0) * @wx_good_rate + (isnull(pay_coupon_srv, 0) - isnull(pay_coupon_good, 0)) * @pay3_rate + isnull(pay_coupon_good, 0) * @pay3_good_rate + (isnull(pay_point_srv, 0) - isnull(pay_point_good, 0)) * @point_rate + isnull(pay_point_good, 0) * @point_good_rate + (isnull(pay_user01_srv, 0) - isnull(pay_user01_good, 0)) * @user01_rate + isnull(pay_user01_good, 0) * @user01_good_rate + (isnull(pay_user02_srv, 0) - isnull(pay_user02_good, 0)) * @user02_rate + isnull(pay_user02_good, 0) * @user02_good_rate + (isnull(pay_user03_srv, 0) - isnull(pay_user03_good, 0)) * @user03_rate + isnull(pay_user03_good, 0) * @user03_good_rate + (isnull(pay_user04_srv, 0) - isnull(pay_user04_good, 0)) * @user04_rate + isnull(pay_user04_good, 0) * @user04_good_rate + (isnull(pay_user05_srv, 0) - isnull(pay_user05_good, 0)) * @user05_rate + isnull(pay_user05_good, 0) * @user05_good_rate + (isnull(pay_user06_srv, 0) - isnull(pay_user06_good, 0)) * isnull(@user06_rate,1) + isnull(pay_user06_good, 0) * isnull(@user06_good_rate,1) + (isnull(pay_user07_srv, 0) - isnull(pay_user07_good, 0)) * isnull(@user07_rate,1) + isnull(pay_user07_good, 0) * isnull(@user07_good_rate,1) + (isnull(pay_user08_srv, 0) - isnull(pay_user08_good, 0)) * isnull(@user08_rate,1) + isnull(pay_user08_good, 0) * isnull(@user08_good_rate,1) where comp = @compfrom end else begin if @sp055f = 1 begin -- 按单据支付 update #stat_byday set work_perf = isnull(cash_srv, 0) * @cash_rate + isnull(cash_good, 0) * @cash_good_rate + isnull(pay_card_srv, 0) + isnull(pay_card_good, 0) + isnull(credit_srv, 0) * @credit_rate + isnull(credit_good, 0) * @credit_good_rate + isnull(zfb_srv, 0) * @zfb_rate + isnull(zfb_good, 0) * @zfb_good_rate + isnull(wx_srv, 0) * @wx_rate + isnull(wx_good, 0) * @wx_good_rate + (isnull(pay_coupon_srv, 0) - isnull(pay_coupon_good, 0)) * @pay3_rate + isnull(pay_coupon_good, 0) * @pay3_good_rate + (isnull(pay_coupon2_srv, 0) - isnull(pay_coupon2_good, 0)) * @pay$_rate + isnull(pay_coupon2_good, 0) * @pay$_good_rate + (isnull(pay_coupon3_srv, 0) - isnull(pay_coupon3_good, 0)) * @pay0_rate + isnull(pay_coupon3_good, 0) * @pay0_good_rate + (isnull(pay_manager_prj, 0) - isnull(pay_manager_good, 0)) * @manager_rate + isnull(pay_manager_good, 0) * @manager_good_rate + (isnull(pay_point_srv, 0) - isnull(pay_point_good, 0)) * @point_rate + isnull(pay_point_good, 0) * @point_good_rate + (isnull(pay_user01_srv, 0) - isnull(pay_user01_good, 0)) * @user01_rate + isnull(pay_user01_good, 0) * @user01_good_rate + (isnull(pay_user02_srv, 0) - isnull(pay_user02_good, 0)) * @user02_rate + isnull(pay_user02_good, 0) * @user02_good_rate + (isnull(pay_user03_srv, 0) - isnull(pay_user03_good, 0)) * @user03_rate + isnull(pay_user03_good, 0) * @user03_good_rate + (isnull(pay_user04_srv, 0) - isnull(pay_user04_good, 0)) * @user04_rate + isnull(pay_user04_good, 0) * @user04_good_rate + (isnull(pay_user05_srv, 0) - isnull(pay_user05_good, 0)) * @user05_rate + isnull(pay_user05_good, 0) * @user05_good_rate + (isnull(pay_user06_srv, 0) - isnull(pay_user06_good, 0)) * isnull(@user06_rate,1) + isnull(pay_user06_good, 0) * isnull(@user06_good_rate,1) + (isnull(pay_user07_srv, 0) - isnull(pay_user07_good, 0)) * isnull(@user07_rate,1) + isnull(pay_user07_good, 0) * isnull(@user07_good_rate,1) + (isnull(pay_user08_srv, 0) - isnull(pay_user08_good, 0)) * isnull(@user08_rate,1) + isnull(pay_user08_good, 0) * isnull(@user08_good_rate,1) where comp = @compfrom end else begin -- 劳动业绩 = 现金(服务) + 现金(产品) + 卡付(产品) + 卡付实营业额 + 经理签单 + 内调 + 抵用券 + 银行卡(服务) + 银行卡(产品) update #stat_byday set work_perf = isnull(cash_srv, 0) * @cash_rate + isnull(cash_good, 0) * @cash_good_rate + isnull(pay_card_srv_work, 0) + isnull(credit_srv, 0) * @credit_rate + isnull(credit_good, 0) * @credit_good_rate + isnull(zfb_srv, 0) * @zfb_rate + isnull(zfb_good, 0) * @zfb_good_rate + isnull(wx_srv, 0) * @wx_rate + isnull(wx_good, 0) * @wx_good_rate + (isnull(pay_coupon_srv, 0) - isnull(pay_coupon_good, 0)) * @pay3_rate + isnull(pay_coupon_good, 0) * @pay3_good_rate + (isnull(pay_coupon2_srv, 0) - isnull(pay_coupon2_good, 0)) * @pay$_rate + isnull(pay_coupon2_good, 0) * @pay$_good_rate + (isnull(pay_coupon3_srv, 0) - isnull(pay_coupon3_good, 0)) * @pay0_rate + isnull(pay_coupon3_good, 0) * @pay0_good_rate + (isnull(pay_manager_prj, 0) - isnull(pay_manager_good, 0)) * @manager_rate + isnull(pay_manager_good, 0) * @manager_good_rate + (isnull(pay_point_srv, 0) - isnull(pay_point_good, 0)) * @point_rate + isnull(pay_point_good, 0) * @point_good_rate + (isnull(pay_user01_srv, 0) - isnull(pay_user01_good, 0)) * @user01_rate + isnull(pay_user01_good, 0) * @user01_good_rate + (isnull(pay_user02_srv, 0) - isnull(pay_user02_good, 0)) * @user02_rate + isnull(pay_user02_good, 0) * @user02_good_rate + (isnull(pay_user03_srv, 0) - isnull(pay_user03_good, 0)) * @user03_rate + isnull(pay_user03_good, 0) * @user03_good_rate + (isnull(pay_user04_srv, 0) - isnull(pay_user04_good, 0)) * @user04_rate + isnull(pay_user04_good, 0) * @user04_good_rate + (isnull(pay_user05_srv, 0) - isnull(pay_user05_good, 0)) * @user05_rate + isnull(pay_user05_good, 0) * @user05_good_rate + (isnull(pay_user06_srv, 0) - isnull(pay_user06_good, 0)) * isnull(@user06_rate,1) + isnull(pay_user06_good, 0) * isnull(@user06_good_rate,1) + (isnull(pay_user07_srv, 0) - isnull(pay_user07_good, 0)) * isnull(@user07_rate,1) + isnull(pay_user07_good, 0) * isnull(@user07_good_rate,1) + (isnull(pay_user08_srv, 0) - isnull(pay_user08_good, 0)) * isnull(@user08_rate,1) + isnull(pay_user08_good, 0) * isnull(@user08_good_rate,1) where comp = @compfrom end end fetch cur_get_compid_2 into @compfrom end close cur_get_compid_2 deallocate cur_get_compid_2 end --门店简称 update a set a.comp = a.comp +'('+ isnull(b.gae03c,'') + ')' from #stat_byday a, gam05 b where a.comp = b.gae01c select isnull(comp,'') comp, isnull(cash_card,'') cash_card, isnull(cash_srv,'') cash_srv, isnull(cash_good,'') cash_good, isnull(cash_total,'') cash_total, isnull(credit_card,'') credit_card, isnull(credit_srv,'') credit_srv, isnull(credit_good,'') credit_good, isnull(credit_total,'') credit_total, isnull(zfb_card,'') zfb_card, isnull(zfb_srv,'') zfb_srv, isnull(zfb_good,'') zfb_good, isnull(zfb_total,'') zfb_total, isnull(wx_card,'') wx_card, isnull(wx_srv,'') wx_srv, isnull(wx_good,'') wx_good, isnull(wx_total,'') wx_total, isnull(revene,'') revene, isnull(outcome,'') outcome, isnull(profit,'') profit, isnull(defict,'') defict, isnull(returnamt,'') returnamt, isnull(pay_card_srv,'') pay_card_srv, isnull(pay_card_good,'') pay_card_good, isnull(pay_coupon,'') pay_coupon, isnull(pay_coupon2,'') pay_coupon2, isnull(pay_coupon3,'') pay_coupon3, isnull(pay_manager,'') pay_manager, isnull(sale_card_total,'') sale_card_total, isnull(pay_card_srv_act,'') pay_card_srv_act, isnull(pay_card_srv_work,'') pay_card_srv_work, isnull(work_perf,'') work_perf, isnull(pay_manager_prj,'') pay_manager_prj, isnull(pay_coupon_srv,'') pay_coupon_srv, isnull(pay_coupon2_srv,'') pay_coupon2_srv, isnull(pay_coupon3_srv,'') pay_coupon3_srv, isnull(pay_manager_good,'') pay_manager_good, isnull(pay_coupon_good,'') pay_coupon_good, isnull(pay_coupon2_good,'') pay_coupon2_good, isnull(pay_coupon3_good,'') pay_coupon3_good, isnull(pay_point,'') pay_point, isnull(pay_point_srv,'') pay_point_srv, isnull(pay_point_good,'') pay_point_good, isnull(pay_user01,'') pay_user01, isnull(pay_user01_srv,'') pay_user01_srv, isnull(pay_user01_good,'') pay_user01_good, isnull(pay_user02,'') pay_user02, isnull(pay_user02_srv,'') pay_user02_srv, isnull(pay_user02_good,'') pay_user02_good, isnull(pay_user03,'') pay_user03, isnull(pay_user03_srv,'') pay_user03_srv, isnull(pay_user03_good,'') pay_user03_good, isnull(pay_user04,'') pay_user04, isnull(pay_user04_srv,'') pay_user04_srv, isnull(pay_user04_good,'') pay_user04_good, isnull(pay_user05,'') pay_user05, isnull(pay_user05_srv,'') pay_user05_srv, isnull(pay_user05_good,'') pay_user05_good, isnull(pay_user06,'') pay_user06, isnull(pay_user06_srv,'') pay_user06_srv, isnull(pay_user06_good,'') pay_user06_good, isnull(pay_user07,'') pay_user07, isnull(pay_user07_srv,'') pay_user07_srv, isnull(pay_user07_good,'') pay_user07_good, isnull(pay_user08,'') pay_user08, isnull(pay_user08_srv,'') pay_user08_srv, isnull(pay_user08_good,'') pay_user08_good, isnull(other_income,'') other_income, isnull(guestflux,'') guestflux, isnull(guestcount,'') guestcount, isnull(guestprice,'') guestprice, isnull(good_total,0) good_total from #stat_byday order by comp drop table #stat_byday drop table #tbl_profit_day end GO /****** Object: StoredProcedure [dbo].[upg_recreate_gfm08_scope] Script Date: 06/12/2017 06:20:33 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_recreate_gfm08_scope] ( @frommonth varchar(20), @tomonth varchar(20) ) as begin declare @start_date varchar(20) declare @start_month varchar(10) declare @end_month varchar(10) select @start_date = min(gfd07c) from gfm04 with (nolock) select @start_month = substring(@start_date, 1, 6) -- 开始统计的日期大于库存开始日期,就按现在要求的统计月份开始 if @frommonth > @start_month select @start_month = @frommonth -- 需要循环统计 from @start_month to @tomonth declare @each_compid varchar(10) declare @cur_month varchar(10) declare cur_each_comp cursor for select gae01c from gam05 open cur_each_comp fetch cur_each_comp into @each_compid while @@fetch_status = 0 begin select @cur_month = @start_month while @cur_month <= @tomonth begin exec upg_create_good_month_cost @each_compid, @cur_month -- 计算下个月 exec upg_cal_next_month @cur_month output end fetch cur_each_comp into @each_compid end close cur_each_comp deallocate cur_each_comp end GO /****** Object: StoredProcedure [dbo].[upg_correct_memberid_ex] Script Date: 06/12/2017 06:19:49 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_correct_memberid_ex] ( @fromCard varchar(20), -- 开始卡号 @toCard varchar(20) -- 截止卡号 ) as begin create table #cardid_temp ( idd int identity not null, cardid varchar(20) null -- 卡号 ) insert #cardid_temp(cardid) select gca01c from gcm01 where gca01c >= @fromCard and gca01c <= @toCard declare @cardid varchar(20) declare #cardid_cursor cursor for select cardid from #cardid_temp open #cardid_cursor fetch #cardid_cursor into @cardid while(@@fetch_status = 0) begin exec upg_correct_memberid @cardid fetch next from #cardid_cursor into @cardid end close #cardid_cursor deallocate #cardid_cursor drop table #cardid_temp end GO /****** Object: StoredProcedure [dbo].[upg_account_fix] Script Date: 06/12/2017 06:19:22 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_account_fix] ( @compid varchar(10), --公司别 @fixdate varchar(8), --封账日期 @userid varchar(20), --操作人 @curdate varchar(8) --操作日期 ) as begin create table #search_min_date ( idd int identity not null, compid varchar(10) not null, datein varchar(8) null, primary key(idd) ) declare @datetmp datetime declare @strtmp varchar(20) --单据的最小日期,以及最大日期 create table #tbl_bill_date ( compid varchar(10) not null, firstdate varchar(20) not null, enddate varchar(20) null, primary key(compid) ) --封帐的最小日期,以及最大日期 create table #tbl_gcm15_first ( compid varchar(10) not null, firstdate varchar(20) not null, enddate varchar(20) null, needadd int null, -- 是否需要增加封帐记录 primary key(compid) ) --buffer create table #tbl_buf_first ( compid varchar(10) not null, firstdate varchar(20) not null, enddate varchar(20) null, primary key(compid) ) --初始化封帐的日期 insert #tbl_gcm15_first(compid, firstdate, enddate) select gae01c, '20000101', '20000101' from gam05 a, gam26 z where a.gae01c = z.gaz02c and z.gaz01c = @compid --当前封帐的开始日期以及截止日期 insert #tbl_buf_first(compid, firstdate, enddate) select gcq00c, min(gcq01d), max(gcq01d) from gcm15 a, gam26 z where a.gcq00c = z.gaz02c and z.gaz01c = @compid group by gcq00c --调整当前已经封帐的情况 update a set a.firstdate = b.firstdate, a.enddate = b.enddate from #tbl_gcm15_first a, #tbl_buf_first b where a.compid = b.compid update #tbl_gcm15_first set needadd = 0 -- 默认不需要增加记录,就是要删除记录 --如果当前需要封帐的日期在后,则需要增加封帐记录 update a set needadd = 1 from #tbl_gcm15_first a where a.enddate < @fixdate --把这个日期之后的封帐记录全部删除 delete a from gcm15 a, #tbl_gcm15_first b where a.gcq00c = b.compid and b.needadd = 0 and a.gcq01d > @fixdate --收银, 会员卡销售, 会员帐务异动, 费用的最小日期 insert into #search_min_date(compid, datein) select gga00c, min(gga02d) from ggm01 a with (nolock), #tbl_gcm15_first b where gga00c = b.compid and b.firstdate = '20000101' group by gga00c insert into #search_min_date(compid, datein) select gna00c, min(gna02d) from gnm01 a with (nolock), #tbl_gcm15_first b where gna00c = b.compid and b.firstdate = '20000101' group by gna00c insert into #search_min_date(compid, datein) select gcl00c, min(gcl02d) from gcm10 a with (nolock), #tbl_gcm15_first b where gcl00c = b.compid and b.firstdate = '20000101' group by gcl00c --每个公司最早的单据日期 insert #tbl_bill_date(compid, firstdate) select compid, min(datein) from #search_min_date where isnull(datein, '') <> '' group by compid --如果没有封过帐,则需要把最新单据的前一天作为封帐的日期 update a set a.enddate = substring(convert(varchar(10), cast(dateadd(day, -1, cast(b.firstdate as datetime)) as datetime), 120), 1, 4) + substring(convert(varchar(10), cast(dateadd(day, -1, cast(b.firstdate as datetime)) as datetime), 120), 6, 2) + substring(convert(varchar(10), cast(dateadd(day, -1, cast(b.firstdate as datetime)) as datetime), 120), 9, 2) from #tbl_gcm15_first a, #tbl_bill_date b where a.compid = b.compid and a.firstdate = '20000101' --还需要处理没有封过帐,也没有单据的情况 update a set a.firstdate = @fixdate, a.enddate = @fixdate from #tbl_gcm15_first a where a.firstdate = '20000101' and a.enddate = '20000101' declare @insert_day varchar(10) declare @cur_compid varchar(10) declare @cur_date varchar(10) declare cur_each_gcm15 cursor for select compid, enddate from #tbl_gcm15_first where needadd = 1 -- 需要增加记录 open cur_each_gcm15 fetch cur_each_gcm15 into @cur_compid, @cur_date while @@fetch_status = 0 begin --已经封帐的下一天才是需要增加的开始 set @datetmp = dateadd(day, 1, cast(@cur_date as datetime)) set @strtmp = convert(varchar(10), cast(@datetmp as datetime), 120) set @insert_day = substring(@strtmp, 1, 4) + substring(@strtmp, 6, 2) + substring(@strtmp, 9, 2) while (select datediff(day, cast(@insert_day as datetime), cast(@fixdate as datetime))) >= 0 begin insert gcm15(gcq00c, gcq01d) values(@cur_compid, @insert_day) set @datetmp = dateadd(day, 1, cast(@insert_day as datetime)) set @strtmp = convert(varchar(10), cast(@datetmp as datetime), 120) set @insert_day = substring(@strtmp, 1, 4) + substring(@strtmp, 6, 2) + substring(@strtmp, 9, 2) end fetch cur_each_gcm15 into @cur_compid, @cur_date end close cur_each_gcm15 deallocate cur_each_gcm15 exec upg_account_fix_create_gcm15 @compid, @fixdate, @userid, @curdate --gcm14保存 delete a from gcm14 a, gam26 z where a.gcp00c = z.gaz02c and z.gaz01c = @compid insert gcm14(gcp00c, gcp01d, gcp02c, gcp03d) select gaz02c, @fixdate, @userid, @curdate from gam26 where gaz01c = @compid drop table #search_min_date drop table #tbl_bill_date drop table #tbl_gcm15_first drop table #tbl_buf_first end GO /****** Object: StoredProcedure [dbo].[upg_cal_cardfill_perf] Script Date: 06/12/2017 06:19:31 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_cal_cardfill_perf] ( @compid varchar(10), -- 公司别 @personid varchar(20), -- 员工代码 @code varchar(20), -- 会员卡代码, 疗程代码, 套餐代码 @cardtype varchar(20), -- 卡的类别 @standardprice float, -- 标准价 @total float, -- 营业额 @times float, -- 数量 当是卡销售的时候, 如果多个支付方式, 必须按支付金额分摊数量 @pctimes float, -- 疗程或套餐的实际次数 @procid float, -- 疗程的价格序号 @flag int, -- 1-卡销售 0-卡充值 2-疗程 3-套餐 @share_rate float, -- 分享比率 @paycode varchar(20), -- 支付方式 @billtype varchar(20), -- 单据类别 @billid varchar(20), -- 单据号 @vperf float output -- 虚业绩(已经考虑分享比率) ) as begin -- 业绩方式 declare @type int -- 业绩比率 declare @setvalue float -- 提成方案代码 declare @perf_code varchar(20) select @perf_code = ppt02c from personprecept where ppt00c = @compid and ppt03c = @personid and isnull(ppt04i, 0) = 1 and isnull(ppt11i, 0) = 0 if @flag = 1 -- 卡销售 begin select @type = car03i, @setvalue = car04f from cardprecept with (nolock) where car00c = @compid and car01c = @cardtype and car02c = @perf_code end else if @flag = 0 -- 卡充值 begin select @type = car07i, @setvalue = car08f from cardprecept with (nolock) where car00c = @compid and car01c = @cardtype and car02c = @perf_code end else if @flag = 2 --疗程的业绩设置 begin declare @free_mode int declare @gda13c varchar(10) if @times = 0 begin select @free_mode = free_perf_mode, @setvalue = free_perf_set from proc_set with (nolock) where compid = @compid and projid = @code and idd = @procid -- 获取疗程默认业绩/提成(按项目) if isnull(@free_mode, 0) = 0 begin select @free_mode = free_perf_mode, @setvalue = free_perf_set from proc_set_perf_comm with (nolock) where compid = @compid and projid = @code end -- 获取疗程默认业绩/提成(按项目统计类别) if isnull(@free_mode, 0) = 0 begin select @gda13c = gda13c from gdm01 where gda00c = @compid and gda01c = @code select @free_mode = free_perf_mode, @setvalue = free_perf_set from proc_set_perf_comm with (nolock) where compid = @compid and projid = '' and projtype = @gda13c end -- 获取疗程默认业绩/提成(默认) if isnull(@free_mode, 0) = 0 begin select @free_mode = free_perf_mode, @setvalue = free_perf_set from proc_set_perf_comm with (nolock) where compid = @compid and projid = '' and projtype = '' end end if isnull(@free_mode, 0) = 1 begin set @type = 100 end else begin declare @proc_times float select @type = perf_mode, @setvalue = perf_set, @proc_times = times from proc_set with (nolock) where compid = @compid and projid = @code and idd = @procid -- 固定金额比率 if @type = 1 begin if @proc_times > 0 set @setvalue = @setvalue / @proc_times end -- 获取疗程默认业绩/提成(按项目) if isnull(@type, '') = '' begin select @type = perf_mode, @setvalue = perf_set from proc_set_perf_comm with (nolock) where compid = @compid and projid = @code end -- 获取疗程默认业绩/提成(按项目统计类别) if isnull(@type, '') = '' begin select @gda13c = gda13c from gdm01 where gda00c = @compid and gda01c = @code select @type = perf_mode, @setvalue = perf_set from proc_set_perf_comm with (nolock) where compid = @compid and projid = '' and projtype = @gda13c end -- 获取疗程默认业绩/提成(默认) if isnull(@type, '') = '' begin select @type = perf_mode, @setvalue = perf_set from proc_set_perf_comm with (nolock) where compid = @compid and projid = '' and projtype = '' end end --如果没有设置疗程的业绩, 那么用卡充值的业绩比率 if isnull(@type, '') = '' begin select @type = car07i, @setvalue = car08f from cardprecept with (nolock) where car00c = @compid and car01c = @cardtype and car02c = @perf_code end -- 可能没有设置, 那么疗程的金额就是业绩 select @type = isnull(@type, 2), @setvalue = isnull(@setvalue, 0) end else if @flag = 3 -- 套餐业绩设置 begin select @type = gta07i, @setvalue = gta08f from gtc01 with (nolock) where gta00c = @compid and gta01c = @code -- 可能没有设置, 那么疗程的金额就是业绩 select @type = isnull(@type, 2), @setvalue = isnull(@setvalue, 0) end if(isnull(@type, 0) = 0) begin select @type = 2 end if(isnull(@setvalue, 0) = 0) begin select @setvalue = 1 end -- 如果是卡销售或卡充值,这根据支付方式赖调整业绩得计算,可能不算业绩 -- 卡充值, 疗程, 套餐都算卡销售的一部分, 都需要折算 -- 卡销售销售业绩折算比率 declare @pay_ratio float select @pay_ratio = gso05f from gsm15 where gso00c = @compid and gso02c = @paycode and gso01c = '3' select @pay_ratio = isnull(@pay_ratio, 1) select @setvalue = @setvalue * @pay_ratio --计算虚业绩 if @type = 1 -- 固定金额 select @vperf = @setvalue * @times * @share_rate else if @type = 100 -- 固定金额 select @vperf = @setvalue * @share_rate else if @type = 2 or @type = 9 -- 营业额比率, 卡销售没有实际营业额的概念 select @vperf = @total * @setvalue * @share_rate else if @type = 5 -- 标准价比率 select @vperf = @standardprice * @times * @setvalue * @share_rate else if @type = 25 or @type = 26 or @type = 27 or @type = 28 -- 营业额扣除当日消费, 26扣除当日实际营业额 27 低于小于等于70% begin declare @todayamt float declare @remain_sale_amt float -- 计算当天的消费额 exec upg_cal_consumeamt_afterfill @compid, @billtype, @billid, @type, @todayamt output -- 可能因为几个支付方式, 金额已经是部分了,所以当天的消费额也必须分摊 if @flag = 1 or @flag = 0 begin select @todayamt = @todayamt * @times end else if @flag = 2 or @flag = 3 begin select @todayamt = @todayamt * @pctimes end if @type = 27 or @type = 28 begin declare @limited_value float select @limited_value = gsa03f from gsm01 with (nolock) where gsa00c = @compid and gsa01c = 'SP134' if @limited_value = 0 -- 默认为0.7 select @limited_value = 0.7 if isnull(@todayamt, 0) <= @total * @limited_value begin select @remain_sale_amt = @total if @type = 28 begin select @remain_sale_amt = @total - isnull(@todayamt, 0) end end else -- 当天销卡太多了,不算卖卡业绩 begin select @remain_sale_amt = 0 end end else begin select @remain_sale_amt = @total- isnull(@todayamt, 0) if @remain_sale_amt < 0 select @remain_sale_amt = 0 end select @vperf = @remain_sale_amt * @setvalue * @share_rate end end GO /****** Object: StoredProcedure [dbo].[upg_cal_cardfill_perf_ceshi] Script Date: 06/12/2017 06:19:31 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_cal_cardfill_perf_ceshi] ( @compid varchar(10), -- 公司别 @personid varchar(20), -- 员工代码 @code varchar(20), -- 会员卡代码, 疗程代码, 套餐代码 @cardtype varchar(20), -- 卡的类别 @standardprice float, -- 标准价 @total float, -- 营业额 @times float, -- 数量 当是卡销售的时候, 如果多个支付方式, 必须按支付金额分摊数量 @pctimes float, -- 疗程或套餐的实际次数 @procid float, -- 疗程的价格序号 @flag int, -- 1-卡销售 0-卡充值 2-疗程 3-套餐 @share_rate float, -- 分享比率 @paycode varchar(20), -- 支付方式 @billtype varchar(20), -- 单据类别 @billid varchar(20), -- 单据号 @vperf float output, -- 虚业绩(已经考虑分享比率) @type int, -- 业绩方式 @setvalue float, -- 业绩比率 @perf_code varchar(20), -- 提成方案代码 @pay_ratio float -- 卡销售销售业绩折算比率 ) as begin -- -- 业绩方式 -- declare @type int -- -- -- 业绩比率 -- declare @setvalue float -- 提成方案代码 -- declare @perf_code varchar(20) -- select @perf_code = ppt02c -- from personprecept -- where ppt00c = @compid and ppt03c = @personid and isnull(ppt04i, 0) = 1 and isnull(ppt11i, 0) = 0 -- if @flag = 1 -- 卡销售 -- begin -- select @type = car03i, -- @setvalue = car04f -- from cardprecept with (nolock) -- where car00c = @compid and car01c = @cardtype and car02c = @perf_code -- end -- else if @flag = 0 -- 卡充值 -- begin -- select @type = car07i, -- @setvalue = car08f -- from cardprecept with (nolock) -- where car00c = @compid and car01c = @cardtype and car02c = @perf_code -- end -- else if @flag = 2 --疗程的业绩设置 begin declare @free_mode int declare @gda13c varchar(10) if @times = 0 begin select @free_mode = free_perf_mode, @setvalue = free_perf_set from proc_set with (nolock) where compid = @compid and projid = @code and idd = @procid -- 获取疗程默认业绩/提成(按项目) if isnull(@free_mode, 0) = 0 begin select @free_mode = free_perf_mode, @setvalue = free_perf_set from proc_set_perf_comm with (nolock) where compid = @compid and projid = @code end -- 获取疗程默认业绩/提成(按项目统计类别) if isnull(@free_mode, 0) = 0 begin select @gda13c = gda13c from gdm01 where gda00c = @compid and gda01c = @code select @free_mode = free_perf_mode, @setvalue = free_perf_set from proc_set_perf_comm with (nolock) where compid = @compid and projid = '' and projtype = @gda13c end -- 获取疗程默认业绩/提成(默认) if isnull(@free_mode, 0) = 0 begin select @free_mode = free_perf_mode, @setvalue = free_perf_set from proc_set_perf_comm with (nolock) where compid = @compid and projid = '' and projtype = '' end end if isnull(@free_mode, 0) = 1 begin set @type = 100 end else begin declare @proc_times float select @type = perf_mode, @setvalue = perf_set, @proc_times = times from proc_set with (nolock) where compid = @compid and projid = @code and idd = @procid -- 如果没有设置疗程的业绩, 那么用卡充值的业绩比率 if @type = 1 begin if @proc_times > 0 set @setvalue = @setvalue / @proc_times end -- 获取疗程默认业绩/提成(按项目) if isnull(@type, '') = '' begin select @type = perf_mode, @setvalue = perf_set from proc_set_perf_comm with (nolock) where compid = @compid and projid = @code end -- 获取疗程默认业绩/提成(按项目统计类别) if isnull(@type, '') = '' begin select @gda13c = gda13c from gdm01 where gda00c = @compid and gda01c = @code select @type = perf_mode, @setvalue = perf_set from proc_set_perf_comm with (nolock) where compid = @compid and projid = '' and projtype = @gda13c end -- 获取疗程默认业绩/提成(默认) if isnull(@type, '') = '' begin select @type = perf_mode, @setvalue = perf_set from proc_set_perf_comm with (nolock) where compid = @compid and projid = '' and projtype = '' end end -- 可能没有设置(默认)充值业绩 if isnull(@type, '') = '' begin select @type = car07i, @setvalue = car08f from cardprecept with (nolock) where car00c = @compid and car01c = @cardtype and car02c = @perf_code end -- 可能没有设置, 那么疗程的金额就是业绩 select @type = isnull(@type, 2), @setvalue = isnull(@setvalue, 0) end -- else if @flag = 3 -- 套餐业绩设置 -- begin ---- select @type = gta07i, ---- @setvalue = gta08f ---- from gtc01 with (nolock) ---- where gta00c = @compid and gta01c = @code -- -- -- 可能没有设置, 那么疗程的金额就是业绩 -- select @type = isnull(@type, 2), -- @setvalue = isnull(@setvalue, 0) -- end -- select @type = isnull(@type, 2) select @setvalue = isnull(@setvalue, 0) -- 如果是卡销售或卡充值,这根据支付方式赖调整业绩得计算,可能不算业绩 -- 卡充值, 疗程, 套餐都算卡销售的一部分, 都需要折算 -- 卡销售销售业绩折算比率 -- declare @pay_ratio float -- select @pay_ratio = gso05f -- from gsm15 -- where gso00c = @compid and gso02c = @paycode and gso01c = '3' select @pay_ratio = isnull(@pay_ratio, 1) select @setvalue = @setvalue * @pay_ratio --计算虚业绩 if @type = 1 -- 固定金额 select @vperf = @setvalue * @times * @share_rate else if @type = 100 -- 固定金额 begin if isnull(@free_mode, 0) = 1 --疗程 永久免费 lucz select @vperf = @setvalue * @share_rate * isnull(@pctimes,1) else select @vperf = @setvalue * @share_rate end else if @type = 2 or @type = 9 -- 营业额比率, 卡销售没有实际营业额的概念 select @vperf = @total * @setvalue * @share_rate else if @type = 5 -- 标准价比率 select @vperf = @standardprice * @times * @setvalue * @share_rate else if @type = 25 or @type = 26 or @type = 27 or @type = 28 -- 营业额扣除当日消费, 26扣除当日实际营业额 27 低于小于等于70% begin declare @todayamt float declare @remain_sale_amt float -- 计算当天的消费额 exec upg_cal_consumeamt_afterfill @compid, @billtype, @billid, @type, @todayamt output -- 可能因为几个支付方式, 金额已经是部分了,所以当天的消费额也必须分摊 if @flag = 1 or @flag = 0 begin select @todayamt = @todayamt * @times end else if @flag = 2 or @flag = 3 begin select @todayamt = @todayamt * @pctimes end if @type = 27 or @type = 28 begin declare @limited_value float select @limited_value = gsa03f from gsm01 with (nolock) where gsa00c = @compid and gsa01c = 'SP134' if @limited_value = 0 -- 默认为0.7 select @limited_value = 0.7 if isnull(@todayamt, 0) <= @total * @limited_value begin select @remain_sale_amt = @total if @type = 28 begin select @remain_sale_amt = @total - isnull(@todayamt, 0) end end else -- 当天销卡太多了,不算卖卡业绩 begin select @remain_sale_amt = 0 end end else begin select @remain_sale_amt = @total- isnull(@todayamt, 0) if @remain_sale_amt < 0 select @remain_sale_amt = 0 end select @vperf = @remain_sale_amt * @setvalue * @share_rate end end GO /****** Object: StoredProcedure [dbo].[upg_dispatch_data_kam01_single] Script Date: 06/12/2017 06:20:00 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_dispatch_data_kam01_single] ( @compid varchar(10) ) as begin create table #tbl_control ( compid varchar(10) not null, -- 儿子公司的代码 lvl int null, primary key(compid) ) insert #tbl_control(compid, lvl) select bran_id, control_lvl from b_perf a, basic_data_control b where parent_id = @compid and a.bran_id = b.compid and b.datatype = 'kam01' and b.control_lvl = 1 -- 创建循环变量 declare @HasRecord int set @HasRecord = 1 declare @currentCompId varchar(10) declare @lvl int while (@HasRecord = 1) begin if exists (select 1 from #tbl_control) begin set @HasRecord = 1 select top 1 @currentCompId = compid, @lvl = lvl from #tbl_control if @lvl = 1 -- 严格按总部的 begin -- 先删除自己的,把总部分发下来 -- 强制模式都是先删除原来的 exec upg_m_dispatch_kam01_single @compid, @currentCompId, 1 insert #tbl_control(compid, lvl) select bran_id, control_lvl from b_perf a, basic_data_control b where parent_id = @currentCompId and a.bran_id = b.compid and b.datatype = 'kam01' and b.control_lvl = 1 end else if @lvl = 2 begin exec upg_m_dispatch_kam01_single @compid, @currentCompId, 0 insert #tbl_control(compid, lvl) select bran_id, case when (control_lvl <= 2) then 2 else control_lvl end from b_perf a, basic_data_control b where parent_id = @currentCompId and a.bran_id = b.compid and b.datatype = 'kam01' and b.control_lvl = 1 end -- 清除已处理的门店 delete #tbl_control where compid = @currentCompId end else begin set @HasRecord = 0 end end drop table #tbl_control end GO /****** Object: StoredProcedure [dbo].[upg_ghm01_confirm2] Script Date: 06/12/2017 06:20:11 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_ghm01_confirm2] ( @compid varchar(10), --公司编号 @billno varchar(100), --入库(出库)单号 @billdate varchar(8), --单据日期 @storageno varchar(4), --仓库编号 @checkman varchar(10), --复核人 @currdate varchar(8), --复核日期 @flag int, --标志位 1 复核 0 取消复核 @operateid int --作业标记 1 入库 2 出库 ) as-- WUXM 2004/12/6 PM 21:31 begin declare @trncnt int select @trncnt = @@trancount if (@trncnt = 0) begin transaction ts else save transaction ts create table #stock ( idd int identity not null, goodsno varchar(20) null, --产品编号 price float null, --本次入库前的该产品进价 stock float null, --入库量 primary key(idd) ) create table #outstock( idd int identity not null, goods varchar(20) null, --产品编号 outstock float null, --出库量 primary key (idd) ) declare @caltype float select @caltype = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP054' if @operateid = 1 --入库作业 begin if @flag = 1 --复核 begin --入库前的库存量和产品进价 insert into #stock(goodsno, stock) select ghb03c, sum(isnull(gfd05f, 0)) from ghm02 with (nolock), gfm04 with (nolock) where ghb00c = gfd00c and ghb03c = gfd04c and ghb00c = @compid and ghb01c = @billno and gfd01c = '1' and isnull(gfd05f, 0)<>0 and isnull(ghb03c, '')<>'' group by ghb03c insert into #outstock(goods, outstock) select ghb03c, sum(isnull(gfd05f, 0)) from ghm02 with (nolock), gfm04 with (nolock) where ghb00c = gfd00c and ghb03c = gfd04c and ghb00c = @compid and ghb01c = @billno and gfd01c in ('2', '3', '4', '5', '6', '7', '8') and isnull(gfd05f, 0)<>0 and isnull(ghb03c, '')<>'' group by ghb03c update #stock set price = isnull(gfa22f, 0) from #stock, gfm01 where gfa00c = @compid and gfa01c = goodsno update #stock set stock = isnull(stock, 0)- isnull(outstock, 0) from #stock, #outstock where goodsno = goods insert gfm03(gfc00c, gfc01c, gfc02c, gfc03c, gfc040c, gfc041c, gfc05c, gfc06i, gfc08i, gfc07c, gfc09t) -- update 库存日异动统计表头档 select gha00c, '1', gha01c, gha03d, '3', gha04c, gha04c, gha07i, gha10i, gha05c, gha09t from ghm01 with (nolock) where gha00c = @compid and gha01c = @billno if @@error <> 0 goto error_label insert gfm04(gfd00c, gfd01c, gfd02c, gfd03f, gfd04c, gfd05f, gfd06f, gfd07c, gfd08c, gfd11c, gfd12f, gfd13f, gfd14i, gfd15c) --update 库存日异动统计明细档 select ghb00c, '1', ghb01c, ghb02f, ghb03c, ghb13f, ghb14f, @billdate, @storageno, ghb06c, isnull(ghb07f, 0), isnull(ghb11f, 0), 1, gha05c from ghm02 b with (nolock), ghm01 a with (nolock), gfm01 c where ghb00c = @compid and ghb01c = @billno and isnull(ghb13f, 0) <> 0 and ghb00c = gfa00c and ghb03c = gfa01c and b.ghb00c = a.gha00c and b.ghb01c = a.gha01c if @@error <> 0 goto error_label update gf set gf.gfb06f = isnull(gf.gfb06f, 0) + isnull(gh.ghb13f, 0), gf.gfb07f = isnull(gf.gfb07f, 0) + isnull(gh.ghb11f, 0) from gfm02 gf left outer join gfm01 on gfa00c = gfb00c and gfa01c = gfb01c left outer join ghm02 gh on ghb00c = gfa00c and ghb03c = gfa01c where gh.ghb00c = @compid and gh.ghb01c = @billno and ghb03c = gfb01c and gfb02c = substring(@billdate, 1, 6) and gfb00c = ghb00c and gfb04c = @storageno and gfb03c = '1' if @@error <> 0 goto error_label insert into gfm02 (gfb00c, gfb01c, gfb02c, gfb03c, gfb04c, gfb06f, gfb07f) select ghb00c, ghb03c, substring(@billdate, 1, 6), '1', @storageno, isnull(ghb13f, 0), isnull(ghb11f, 0) from ghm02 with (nolock) left outer join gfm01 on ghb00c = gfa00c and ghb03c = gfa01c where ghb00c = @compid and ghb01c = @billno and ghb03c not in (select gfb01c from gfm02 where gfb00c = ghb00c and gfb01c = ghb03c and gfb02c = substring(@billdate, 1, 6) and gfb03c = '1'and gfb04c = @storageno ) if @@error <> 0 goto error_label update ghm01 set gha93c = @checkman, gha94d = @currdate --update 产品入库单 where gha00c = @compid and gha01c = @billno if @@error <> 0 goto error_label --更新产品进价 added by mzh 2005/10/30 if (@caltype = 0) begin update gfm01 set gfa22f = (isnull(gfd05f, 0)*isnull(gfd06f, 0) + isnull(price, 0)*isnull(stock, 0))/(isnull(gfd05f, 0) + isnull(stock, 0)) from gfm01 left outer join #stock on gfa00c = @compid and gfa01c = goodsno left outer join gfm04 on gfa00c = gfd00c and gfa01c = gfd04c where gfa00c = @compid and gfd02c = @billno and gfd01c = '1' and isnull(gfd05f, 0) + isnull(stock, 0)>0 end if @@error <> 0 goto error_label end else if @flag = 0 --取消复核 begin insert into #stock(goodsno, stock) select ghb03c, sum(isnull(gfd05f, 0)) from ghm02 with (nolock), gfm04 with (nolock) where ghb00c = gfd00c and ghb03c = gfd04c and ghb00c = @compid and ghb01c = @billno and gfd01c = '1' group by ghb03c insert into #outstock(goods, outstock) select ghb03c, sum(isnull(gfd05f, 0)) from ghm02 with (nolock), gfm04 with (nolock) where ghb00c = gfd00c and ghb03c = gfd04c and ghb00c = @compid and ghb01c = @billno and gfd01c in ('2', '3', '4', '5', '6', '7', '8') group by ghb03c update #stock set price = isnull(gfa22f, 0) from #stock, gfm01 where gfa00c = @compid and gfa01c = goodsno update #stock set stock = stock-outstock from #stock, #outstock where goodsno = goods --更新产品进价 added by mzh 2005/10/30 if (@caltype = 0) begin update gfm01 set gfa22f = (isnull(price, 0)*isnull(stock, 0)-isnull(gfd05f, 0)*isnull(gfd06f, 0))/(isnull(stock, 0)-isnull(gfd05f, 0)) from gfm01 left outer join #stock on gfa00c = @compid and gfa01c = goodsno left outer join gfm04 on gfa00c = gfd00c and gfa01c = gfd04c where gfa00c = @compid and gfd02c = @billno and gfd01c = '1' and isnull(stock, 0)-isnull(gfd05f, 0) > 0 end if @@error <> 0 goto error_label delete from gfm03 where gfc00c = @compid and gfc01c = '1' and gfc02c = @billno -- update 库存日异动统计表头档 if @@error <> 0 goto error_label delete from gfm04 where gfd00c = @compid and gfd01c = '1' and gfd02c = @billno --update 库存日异动统计明细档 if @@error <> 0 goto error_label update gfm02 set gfb06f = isnull(gfb06f, 0) - isnull(gh.ghb13f, 0), gfb07f = isnull(gfb07f, 0) - isnull(gh.ghb11f, 0) from gfm02 gf left outer join gfm01 on gfa00c = gfb00c and gfa01c = gfb01c left outer join ghm02 gh on ghb00c = gfa00c and ghb03c = gfa01c where gh.ghb00c = @compid and gh.ghb01c = @billno and ghb03c = gfb01c and gfb02c = substring(@billdate, 1, 6) and gfb00c = ghb00c and gfb04c = @storageno and gfb03c = '1' if @@error <> 0 goto error_label update ghm01 set gha93c = '', gha94d = '' --update 产品入库单 where gha00c = @compid and gha01c = @billno if @@error <> 0 goto error_label end end else if @operateid = 2 --出库作业 begin if @flag = 1 --复核 begin insert gfm03(gfc00c, gfc01c, gfc02c, gfc03c, gfc040c, gfc041c, gfc05c, gfc06i, gfc08i, gfc07c, gfc09t) -- update 库存日异动统计表头档 select gia00c, '2', gia01c, gia03d, '3', gia04c, gia04c, gia07i, gia09i, gia05c, gia08t from gim01 with (nolock) where gia00c = @compid and gia01c = @billno if @@error <> 0 goto error_label insert gfm04(gfd00c, gfd01c, gfd02c, gfd03f, gfd04c, gfd05f, gfd06f, gfd07c, gfd08c, gfd11c, gfd12f, gfd13f, gfd14i, gfd15c) --update 库存日异动统计明细档 select gib00c, '2', gib01c, gib02f, gib03c, gib08f, gib11f, @billdate, @storageno, gib06c, isnull(gib07f, 0), isnull(gib10f, 0), gia09i, gia05c from gim02 b with (nolock), gim01 a with (nolock) where gib00c = @compid and gib01c = @billno and isnull(gib08f, 0)<>0 and b.gib00c = a.gia00c and b.gib01c = a.gia01c if @@error <> 0 goto error_label update gf set gf.gfb06f = isnull(gf.gfb06f, 0) + isnull(gi.gib08f, 0) from gfm02 gf, gim02 gi where gi.gib00c = @compid and gi.gib01c = @billno and gfb00c = gib00c and gib03c = gfb01c and gfb02c = substring(@billdate, 1, 6) and gfb04c = @storageno and gfb03c = '2' if @@error <> 0 goto error_label insert into gfm02 (gfb00c, gfb01c, gfb02c, gfb03c, gfb04c, gfb06f) select gib00c, gib03c, substring(@billdate, 1, 6), '2', @storageno, isnull(gib08f, 0) from gim02 with (nolock) where gib00c = @compid and gib01c = @billno and gib03c not in (select gfb01c from gfm02 where gfb00c = gib00c and gfb01c = gib03c and gfb02c = substring(@billdate, 1, 6) and gfb03c = '2'and gfb04c = @storageno ) if @@error <> 0 goto error_label update gim01 set gia93c = @checkman, gia94d = @currdate --update 产品出库单 where gia00c = @compid and gia01c = @billno if @@error <> 0 goto error_label --出库作业,如果是门店,则需要自动新增门店入库单 --Added by WUXM, 2006/04/23 declare @gia09i int declare @gia05c varchar(20) declare @new_billid varchar(30) declare @sp060f float select @gia09i = gia09i, @gia05c = gia05c from gim01 where gia00c = @compid and gia01c = @billno if @gia09i = 2 begin exec upg_auto_po_from_center2 @compid, @billno, @billno --根据系统设定来决定是否自动复合 select @sp060f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP060' if @sp060f<>2 begin select @new_billid = @billno + '-' + @gia05c exec upg_ghm01_confirm2 @gia05c, @new_billid, @billdate, @storageno, @checkman, @currdate, 1, 1 end end end else if @flag = 0 --取消复核 begin delete from gfm03 where gfc00c = @compid and gfc01c = '2' and gfc02c = @billno -- update 库存日异动统计表头档 if @@error <> 0 goto error_label delete from gfm04 where gfd00c = @compid and gfd01c = '2' and gfd02c = @billno --update 库存日异动统计明细档 if @@error <> 0 goto error_label update gfm02 set gfb06f = isnull(gfb06f, 0) - isnull(gi.gib08f, 0) from gfm02 gf, gim02 gi where gi.gib00c = @compid and gi.gib01c = @billno and gfb00c = gib00c and gib03c = gfb01c and gfb02c = substring(@billdate, 1, 6) and gfb04c = @storageno and gfb03c = '2' if @@error <> 0 goto error_label update gim01 set gia93c = '', gia94d = '' where gia00c = @compid and gia01c = @billno --update 产品出库单 if @@error <> 0 goto error_label end end if @trncnt = 0 commit transaction ts set nocount off return 1 error_label: begin rollback transaction ts set nocount off return 0 end end GO /****** Object: StoredProcedure [dbo].[upg_create_gcm20_one_company] Script Date: 06/12/2017 06:19:52 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_create_gcm20_one_company] ( @compid varchar(10) -- 门店编号 ) as begin -- 创建临时表 create table #data_recover_cardno ( cardno varchar(20) null ) insert into #data_recover_cardno(cardno) select gca04c from gcm01 where gca00c = @compid and gca00c = gca13d and isnull(gca04c, '') <> '' and substring(gca04c, 1, 2) <> '散客' -- 创建循环变量 declare @HasRecord int set @HasRecord = 1 declare @currentcardno varchar(20) while(@HasRecord = 1) begin if exists ( select 1 from #data_recover_cardno) begin set @HasRecord = 1 select top 1 @currentcardno = cardno from #data_recover_cardno exec upg_create_gcm20_one_card @currentcardno -- 清除已处理的门店 delete #data_recover_cardno where cardno = @currentcardno end else begin set @HasRecord = 0 end end drop table #data_recover_cardno end GO /****** Object: StoredProcedure [dbo].[upg_get_cashcustid] Script Date: 06/12/2017 06:20:06 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_get_cashcustid] ( @compid varchar(10) ) as begin declare @retcustid varchar(10) declare @custid varchar(20) declare @i int select @i = 1 while @i < 200 begin select @custid = '散客' + convert(varchar(10), @i) -- gam06 匙牌 -- 关联会员卡/凭据 if not exists(select 1 from gam06 with (nolock) where gaf00c = @compid and gaf03c = @custid) begin select @retcustid = @custid exec upg_check_cashcustid @compid, @custid select @retcustid return end select @i = @i + 1 end select '' end GO /****** Object: StoredProcedure [dbo].[upg_close_account] Script Date: 06/12/2017 06:19:42 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_close_account] ( @compid varchar(10), --公司编号, @noteId varchar(20), --单据编号, @KeyId varchar(20), --钥匙牌号, @UserId varchar(10), --User Id, @Date varchar(8), --date, @Mode int ) as--WUXM, 2005/02/15 begin declare @curdate varchar(20), @time varchar(20) exec upg_getcurrdate @curdate output, @time output -- 设定 钥匙付费标志 update glm03 set glc06f = isnull(glc03f, 0) - isnull(glc04f, 0), glc05i = 1 where glc00c = @compid and glc01c = @noteId -- update 已支付金额 update glm01 set gla20f = isnull(gla11f, 0)-isnull(gla12f, 0), gla23i = 1, gla95c = @UserId, gla96d = @Date, gla04t = @time where gla00c = @compid and gla01c = @noteId --Added by WUXM, 2005/10/16 exec upg_updateglm01_accountdate @compid, @noteId -- 取消钥匙牌关联 delete ga from gam06 ga, glm03 gl where gaf00c = glc00c and gaf01c = glc02c and glc00c = @compid and glc01c = @noteId and glc05i = 1 declare @gsa03f float select @gsa03f = isnull(gsa03f, 0) from gsm01 where gsa01c = 'SP031' and gsa00c = @compid if @gsa03f = 2 begin exec upg_create_gcm20_from_one_bill @compid, 'gl', @noteId, 1 end --Added by WUXM, 2005/02/15 --修改员工及位置的状态 exec upg_book_leave_glm01 @compid, @noteId --Added by WUXM, end end GO /****** Object: StoredProcedure [dbo].[upg_close_account_ggm01] Script Date: 06/12/2017 06:19:42 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_close_account_ggm01] ( @compid varchar(10), -- 公司编号, @enterid varchar(20), -- 单据编号, @keyid varchar(20), -- 钥匙牌号, @userid varchar(10), -- User Id, @billdate varchar(8), -- date, @gender int, -- 性别 @isoldcust int ) as--WUXM, 2005/02/15 begin declare @curdate varchar(20) declare @time varchar(20) exec upg_getcurrdate @curdate output, @time output update glm01 set gla10i = @gender, gla04t = @time, gla12f = gla11f, gla13d = @curdate, gla20f = 0, gla22i = 2, gla23i = 1, gla95c = @userid, gla96d = @billdate, gla09i = @isoldcust where gla00c = @compid and gla01c = @enterid -- 设定 钥匙付费标志 update glm03 set glc04f = glc03f, glc06f = 0, glc05i = 1 where glc00c = @compid and glc01c = @enterid -- Added by WUXM, 2005/10/16 exec upg_updateglm01_accountdate @compid, @enterid -- 取消钥匙牌关联 delete ga from gam06 ga, glm03 gl where gaf00c = glc00c and gaf01c = glc02c and glc00c = @compid and glc01c = @enterid and glc05i = 1 declare @gsa03f float select @gsa03f = isnull(gsa03f, 0) from gsm01 where gsa01c = 'SP031' and gsa00c = @compid if @gsa03f = 2 begin exec upg_create_gcm20_from_one_bill @compid, 'gl', @enterid, 1 end -- Added by WUXM, 2005/02/15 -- 修改员工及位置的状态 exec upg_book_leave_glm01 @compid, @enterid -- Added by WUXM, end end GO /****** Object: StoredProcedure [dbo].[upg_update_complete_hem02] Script Date: 06/12/2017 06:20:49 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_update_complete_hem02] ( @compid varchar(10) --公司编号 ) as --WUXM 2005/07/21 begin declare @staff varchar(20) -- 获得员工编号,在每个员工提成中插入项目提成设置 declare cur_get_staff cursor for select haa01c from ham01 where haa00c = @compid open cur_get_staff fetch next from cur_get_staff into @staff while @@fetch_status = 0 begin exec upg_update_one_hem02 @compid, @staff fetch next from cur_get_staff into @staff end close cur_get_staff deallocate cur_get_staff end GO /****** Object: StoredProcedure [dbo].[upg_dispatch_data_appointment_perf_ratio_single] Script Date: 06/12/2017 06:20:00 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_dispatch_data_appointment_perf_ratio_single] ( @compid varchar(10) ) as begin create table #tbl_control ( compid varchar(10) not null, -- 儿子公司的代码 lvl int null, primary key(compid) ) insert #tbl_control(compid, lvl) select bran_id, control_lvl from b_perf a, basic_data_control b where parent_id = @compid and a.bran_id = b.compid and b.datatype = 'appoint_perf_ratio' and b.control_lvl = 1 -- 创建循环变量 declare @HasRecord int set @HasRecord = 1 declare @currentCompId varchar(10) declare @lvl int while (@HasRecord = 1) begin if exists (select 1 from #tbl_control) begin set @HasRecord = 1 select top 1 @currentCompId = compid, @lvl = lvl from #tbl_control if @lvl = 1 -- 严格按总部的 begin -- 先删除自己的,把总部分发下来 -- 强制模式都是先删除原来的 exec upg_m_dispatch_appointment_perf_ratio_single @compid, @currentCompId, 1 insert #tbl_control(compid, lvl) select bran_id, control_lvl from b_perf a, basic_data_control b where parent_id = @currentCompId and a.bran_id = b.compid and b.datatype = 'appoint_perf_ratio' and b.control_lvl = 1 end else if @lvl = 2 begin exec upg_m_dispatch_appointment_perf_ratio_single @compid, @currentCompId, 0 insert #tbl_control(compid, lvl) select bran_id, case when (control_lvl <= 2) then 2 else control_lvl end from b_perf a, basic_data_control b where parent_id = @currentCompId and a.bran_id = b.compid and b.datatype = 'appoint_perf_ratio' and b.control_lvl = 1 end -- 清除已处理的门店 delete #tbl_control where compid = @currentCompId end else begin set @HasRecord = 0 end end drop table #tbl_control end GO /****** Object: StoredProcedure [dbo].[upg_total_perf] Script Date: 06/12/2017 06:20:47 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_total_perf] ( @compid varchar(10), -- 公司别 @staff varchar(20), -- 员工编号 @fromdate varchar(8), -- 开始日期 @todate varchar(8), -- 截至日期 @card_sale float output, -- 卡销售 @card_fill float output, -- 卡充值 @srv_cash float output, -- 现金服务 @srv_card float output -- 卡服务 ) as begin declare @incompid varchar(10) select @incompid = @compid declare @payment_0 varchar(10) declare @payment_3 varchar(10) declare @payment_$ varchar(10) declare @payment_u1 varchar(10), @payment_u2 varchar(10), @payment_u3 varchar(10), @payment_u4 varchar(10), @payment_u5 varchar(10) exec upg_isrevenue_payment @compid, @payment_0 output, @payment_3 output, @payment_$ output, @payment_u1 output, @payment_u2 output, @payment_u3 output, @payment_u4 output, @payment_u5 output if @compid = '001' select @compid = '%' select @card_sale = 0, @card_fill = 0, @srv_cash = 0, @srv_card = 0 if isnull(@staff, '') = '' begin -- 卡销售金额 select @card_sale = isnull(sum(gsc05f), 0) from gsm03 a with (nolock), gnm01 b with (nolock), gam26 z with (nolock) where a.gsc00c = b.gna00c and a.gsc01c = b.gna01c and a.gsc02c = 'gn' and a.gsc00c = z.gaz02c and z.gaz01c = @incompid and b.gna02d >= @fromdate and b.gna02d <= @todate and a.gsc04c in ('1', '6', 'ZFB', 'WX', @payment_0, @payment_3, @payment_$, @payment_u1, @payment_u2, @payment_u3, @payment_u4, @payment_u5) -- 卡充值 select @card_fill = isnull(sum(gsc05f), 0) from gsm03 a with (nolock), gcm10 b with (nolock), gam26 z with (nolock) where a.gsc00c = b.gcl00c and a.gsc01c = b.gcl01c and a.gsc02c = 'gz' and gcl00c = z.gaz02c and z.gaz01c = @incompid and gcl02d >= @fromdate and gcl02d <= @todate and isnull(gcl94d, '') <> '' and a.gsc04c in ('1', '6', 'ZFB', 'WX', @payment_0, @payment_3, @payment_$, @payment_u1, @payment_u2, @payment_u3, @payment_u4, @payment_u5) -- 服务项目 select @srv_cash = isnull(sum(gsc05f), 0) from gsm03 a with (nolock), ggm01 b with (nolock), gam26 z with (nolock) where a.gsc00c = b.gga00c and a.gsc01c = b.gga01c and a.gsc02c = 'gx' and a.gsc00c = z.gaz02c and z.gaz01c = @incompid and b.gga02d >= @fromdate and b.gga02d <= @todate and a.gsc04c in ('1', '6', 'ZFB', 'WX', @payment_0, @payment_3, @payment_$, @payment_u1, @payment_u2, @payment_u3, @payment_u4, @payment_u5) select @srv_card = isnull(sum(gsc05f), 0) from gsm03 a with (nolock), ggm01 b with (nolock), gsm14 c with (nolock), gam26 z with (nolock) where a.gsc00c = b.gga00c and a.gsc01c = b.gga01c and a.gsc02c = 'gx' and a.gsc00c = z.gaz02c and z.gaz01c = @incompid and b.gga02d >= @fromdate and b.gga02d <= @todate and a.gsc04c = c.gsn01c and isnull(c.gsn02c, '') <> '' and isnull(c.gsn02c, '') <> '999' select @srv_cash = @srv_cash + isnull(sum(gsc05f), 0) from gsm03 a with (nolock), glm01 b with (nolock), gam26 z with (nolock) where a.gsc00c = b.gla00c and a.gsc01c = b.gla01c and a.gsc02c = 'gl' and a.gsc00c = z.gaz02c and z.gaz01c = @incompid and b.gla02d >= @fromdate and b.gla02d <= @todate and a.gsc04c in ('1', '6', 'ZFB', 'WX', @payment_0, @payment_3, @payment_$, @payment_u1, @payment_u2, @payment_u3, @payment_u4, @payment_u5) select @srv_card = @srv_card + isnull(sum(gsc05f), 0) from gsm03 a with (nolock), glm01 b with (nolock), gsm14 c with (nolock), gam26 z with (nolock) where a.gsc00c = b.gla00c and a.gsc01c = b.gla01c and a.gsc02c = 'gl' and a.gsc00c = z.gaz02c and z.gaz01c = @incompid and b.gla02d >= @fromdate and b.gla02d <= @todate and a.gsc04c = c.gsn01c and isnull(c.gsn02c, '') <> '' and isnull(c.gsn02c, '') <> '999' end else begin -- 卡销售金额 select @card_sale = isnull(sum(gsc05f * isnull(gna27f, 0)), 0) from gsm03 a with (nolock), gnm01 b with (nolock), gam26 z with (nolock) where a.gsc00c = b.gna00c and a.gsc01c = b.gna01c and a.gsc02c = 'gn' and a.gsc00c = z.gaz02c and z.gaz01c = @incompid and b.gna02d >= @fromdate and b.gna02d <= @todate and b.gna20c = @staff and a.gsc04c in ('1', '6', 'ZFB', 'WX', @payment_0, @payment_3, @payment_$, @payment_u1, @payment_u2, @payment_u3, @payment_u4, @payment_u5) select @card_sale = isnull(@card_sale, 0) + isnull(sum(gsc05f * isnull(gna29f, 0)), 0) from gsm03 a with (nolock), gnm01 b with (nolock), gam26 z with (nolock) where a.gsc00c = b.gna00c and a.gsc01c = b.gna01c and a.gsc02c = 'gn' and a.gsc00c = z.gaz02c and z.gaz01c = @incompid and b.gna02d >= @fromdate and b.gna02d <= @todate and b.gna28c = @staff and a.gsc04c in ('1', '6', 'ZFB', 'WX', @payment_0, @payment_3, @payment_$, @payment_u1, @payment_u2, @payment_u3, @payment_u4, @payment_u5) select @card_sale = isnull(@card_sale, 0) + isnull(sum(gsc05f * isnull(gna31f, 0)), 0) from gsm03 a with (nolock), gnm01 b with (nolock), gam26 z with (nolock) where a.gsc00c = b.gna00c and a.gsc01c = b.gna01c and a.gsc02c = 'gn' and a.gsc00c = z.gaz02c and z.gaz01c = @incompid and b.gna02d >= @fromdate and b.gna02d <= @todate and b.gna30c = @staff and a.gsc04c in ('1', '6', 'ZFB', 'WX', @payment_0, @payment_3, @payment_$, @payment_u1, @payment_u2, @payment_u3, @payment_u4, @payment_u5) -- 卡充值 select @card_fill = isnull(sum(gsc05f * isnull(gcl16f, 0)), 0) from gsm03 a with (nolock), gcm10 b with (nolock), gam26 z with (nolock) where a.gsc00c = b.gcl00c and a.gsc01c = b.gcl01c and a.gsc02c = 'gz' and gcl00c = z.gaz02c and z.gaz01c = @incompid and gcl02d >= @fromdate and gcl02d <= @todate and isnull(gcl94d, '') <> '' and a.gsc04c in ('1', '6', 'ZFB', 'WX', @payment_0, @payment_3, @payment_$, @payment_u1, @payment_u2, @payment_u3, @payment_u4, @payment_u5) and gcl09c = @staff select @card_fill = isnull(@card_fill, 0) + isnull(sum(gsc05f * isnull(gcl18f, 0)), 0) from gsm03 a with (nolock), gcm10 b with (nolock), gam26 z with (nolock) where a.gsc00c = b.gcl00c and a.gsc01c = b.gcl01c and a.gsc02c = 'gz' and gcl00c = z.gaz02c and z.gaz01c = @incompid and gcl02d >= @fromdate and gcl02d <= @todate and isnull(gcl94d, '') <> '' and a.gsc04c in ('1', '6', 'ZFB', 'WX', @payment_0, @payment_3, @payment_$, @payment_u1, @payment_u2, @payment_u3, @payment_u4, @payment_u5) and gcl17c = @staff select @card_fill = isnull(@card_fill, 0) + isnull(sum(gsc05f * isnull(gcl20f, 0)), 0) from gsm03 a with (nolock), gcm10 b with (nolock), gam26 z with (nolock) where a.gsc00c = b.gcl00c and a.gsc01c = b.gcl01c and a.gsc02c = 'gz' and gcl00c = z.gaz02c and z.gaz01c = @incompid and gcl02d >= @fromdate and gcl02d <= @todate and isnull(gcl94d, '') <> '' and a.gsc04c in ('1', '6', 'ZFB', 'WX', @payment_0, @payment_3, @payment_$, @payment_u1, @payment_u2, @payment_u3, @payment_u4, @payment_u5) and gcl19c = @staff -- 服务项目 declare @sp055f float select @sp055f = gsa03f from gsm01 with (nolock) where gsa00c = @incompid and gsa01c = 'SP055' if @sp055f = 1 begin -- 按单据支付 select @srv_cash = isnull(sum(ggb20f), 0) from ggm01 a with (nolock), ggm02 b with (nolock), gam26 z with (nolock) where a.gga00c = b.ggb00c and a.gga01c = b.ggb01c and a.gga02d >= @fromdate and a.gga02d <= @todate and a.gga00c = z.gaz02c and z.gaz01c = @incompid and b.ggb12c = @staff select @srv_cash = isnull(@srv_cash, 0) + isnull(sum(ggb21f), 0) from ggm01 a with (nolock), ggm02 b with (nolock), gam26 z with (nolock) where a.gga00c = b.ggb00c and a.gga01c = b.ggb01c and a.gga02d >= @fromdate and a.gga02d <= @todate and a.gga00c = z.gaz02c and z.gaz01c = @incompid and b.ggb13c = @staff select @srv_cash = isnull(@srv_cash, 0) + isnull(sum(ggb22f), 0) from ggm01 a with (nolock), ggm02 b with (nolock), gam26 z with (nolock) where a.gga00c = b.ggb00c and a.gga01c = b.ggb01c and a.gga02d >= @fromdate and a.gga02d <= @todate and a.gga00c = z.gaz02c and z.gaz01c = @incompid and b.ggb14c = @staff end else begin -- 按项目支付 -- 现金支付 select @srv_cash = isnull(sum(ggb20f), 0) from ggm01 a with (nolock), ggm02 b with (nolock), gam26 z with (nolock) where a.gga00c = b.ggb00c and a.gga01c = b.ggb01c and a.gga02d >= @fromdate and a.gga02d <= @todate and a.gga00c = z.gaz02c and z.gaz01c = @incompid and b.ggb12c = @staff and b.ggb27c in ('1', '6', 'ZFB', 'WX', @payment_0, @payment_3, @payment_$, @payment_u1, @payment_u2, @payment_u3, @payment_u4, @payment_u5) select @srv_cash = isnull(@srv_cash, 0) + isnull(sum(isnull(ggb21f, 0)), 0) from ggm01 a with (nolock), ggm02 b with (nolock), gam26 z with (nolock) where a.gga00c = b.ggb00c and a.gga01c = b.ggb01c and a.gga02d >= @fromdate and a.gga02d <= @todate and a.gga00c = z.gaz02c and z.gaz01c = @incompid and b.ggb13c = @staff and b.ggb27c in ('1', '6', 'ZFB', 'WX', @payment_0, @payment_3, @payment_$, @payment_u1, @payment_u2, @payment_u3, @payment_u4, @payment_u5) select @srv_cash = isnull(@srv_cash, 0) + isnull(sum(isnull(ggb22f, 0)), 0) from ggm01 a with (nolock), ggm02 b with (nolock), gam26 z with (nolock) where a.gga00c = b.ggb00c and a.gga01c = b.ggb01c and a.gga02d >= @fromdate and a.gga02d <= @todate and a.gga00c = z.gaz02c and z.gaz01c = @incompid and b.ggb14c = @staff and b.ggb27c in ('1', '6', 'ZFB', 'WX', @payment_0, @payment_3, @payment_$, @payment_u1, @payment_u2, @payment_u3, @payment_u4, @payment_u5) -- 卡支付 select @srv_card = isnull(sum(isnull(ggb20f, 0)), 0) from ggm01 a with (nolock), ggm02 b with (nolock), gsm14 c with (nolock), gam26 z with (nolock) where a.gga00c = b.ggb00c and a.gga01c = b.ggb01c and a.gga02d >= @fromdate and a.gga02d <= @todate and a.gga00c = z.gaz02c and z.gaz01c = @incompid and b.ggb12c = @staff and b.ggb27c = c.gsn01c and isnull(c.gsn02c, '') <> '' and isnull(c.gsn02c, '') <> '999' select @srv_card = isnull(@srv_card, 0) + isnull(sum(isnull(ggb21f, 0)), 0) from ggm01 a with (nolock), ggm02 b with (nolock), gsm14 c with (nolock), gam26 z with (nolock) where a.gga00c = b.ggb00c and a.gga01c = b.ggb01c and a.gga02d >= @fromdate and a.gga02d <= @todate and a.gga00c = z.gaz02c and z.gaz01c = @incompid and b.ggb13c = @staff and b.ggb27c = c.gsn01c and isnull(c.gsn02c, '') <> '' and isnull(c.gsn02c, '') <> '999' select @srv_card = isnull(@srv_card, 0) + isnull(sum(isnull(ggb22f, 0)), 0) from ggm01 a with (nolock), ggm02 b with (nolock), gsm14 c with (nolock), gam26 z with (nolock) where a.gga00c = b.ggb00c and a.gga01c = b.ggb01c and a.gga02d >= @fromdate and a.gga02d <= @todate and a.gga00c = z.gaz02c and z.gaz01c = @incompid and b.ggb14c = @staff and b.ggb27c = c.gsn01c and isnull(c.gsn02c, '') <> '' and isnull(c.gsn02c, '') <> '999' end end end GO /****** Object: StoredProcedure [dbo].[upg_purse_deduct_extends] Script Date: 06/12/2017 06:20:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_purse_deduct_extends] ( @compid varchar(10), -- 公司编号 @cardno varchar(20), -- 会员卡号 @date varchar(10), -- 异动日期 @accttype int, -- 账户类型(1-电子钱包 2-储值账户) @consume float, -- 支付金额 @changway int, -- 异动方向(0-充值, 1-取款, 2-消费, 3-转入, 4-转出, 5-欠款 6-欠款返回 7-赠送) @billtype varchar(5), -- 单据类别 @billno varchar(20), -- 单据号 @confirmflag int, -- 是否复核的标记(1-复核 0-取消复核) @proc_seq int -- 疗程序号 ) as begin set nocount on declare @ownercompid nvarchar(10) -- 卡的归属公司 declare @gca02c nvarchar(5) -- 卡类别 select @ownercompid = gca00c, @gca02c = gca02c from gcm01 with (nolock) where gca01c = @cardno and gca00c = gca13d declare @gcm04_flowno float declare @lastremain float declare @newdate varchar(8) declare @billmemo varchar(160) -- 单据的备注 declare @oldcardtype varchar(20) declare @newcardtype varchar(20) declare @CanExecute int set @CanExecute = 1 if (isnull(@ownercompid, '') = '' ) set @ownercompid = @compid if @accttype = 3 and @billtype <> 'gx' begin declare @start_points varchar(20) select @start_points = gsa02c from gsm01 with (nolock) where gsa00c = @compid and gsa01c = 'SP110' if isnull(@start_points, '') <> '1' begin set @CanExecute = 0 end declare @gak76i int select @gak76i = gak76i from gam10 with (nolock) where gak00c = @ownercompid and gak01c = @gca02c if isnull(@gak76i, 0) = 1 set @CanExecute = 0 end select @accttype = isnull(@accttype, 2) if @accttype = 999 set @CanExecute = 0 -- 当前的事务数 declare @__transaction_count int set @__transaction_count = @@trancount -- try...catch处理 begin try -- a.开启事务,或者设置事务保存点 if @__transaction_count = 0 begin tran else save tran __tran_upg_purse_deduct declare @gcm03_exists int -- 加更新锁 select @gcm03_exists = 1 from gcm03 UPDLOCK where gcc00c = @ownercompid and gcc01c = @cardno and gcc03i = @accttype if @CanExecute = 1 begin -- 取得新的有效日期 if @changway = 0 and @billtype = 'gz' and @confirmflag = 1 begin select @newdate = gcl21d, @billmemo = gcl57c, @newcardtype = gcl15c, @oldcardtype = gcl52c from gcm10 with (nolock) where gcl00c = @compid and gcl01c = @billno if isnull(@billmemo, '') = '' begin if isnull(@oldcardtype, '') <> isnull(@newcardtype, '') select @billmemo = '升级' + isnull(@oldcardtype, '') + '->' + isnull(@newcardtype, '') end else begin if isnull(@oldcardtype, '') <> isnull(@newcardtype, '') select @billmemo = @billmemo + ' 升级' + isnull(@oldcardtype, '') + '->' + isnull(@newcardtype, '') end end if @changway = 0 and @billtype = 'gn' and @confirmflag = 1 begin select @billmemo = gna07c from gnm01 with (nolock) where gna00c = @compid and gna01c = @billno -- 如果是卡销售的时候,卡号决定卡的类别 select @newcardtype = gnb02c from gnm02 with (nolock) where gnb00c = @compid and gnb01c = @billno and gnb03c = @cardno end -- 取得下一个流水号 -- 如果在账户余额表中不存在,则增加此纪录 if isnull(@gcm03_exists, 0) <> 1 begin declare @flowno2 int declare @safeamt float select @flowno2 = isnull(max(gcc02f), 0) + 1 from gcm03 with (nolock) where gcc00c = @ownercompid and gcc01c = @cardno select @flowno2 = isnull(@flowno2, 1) if @accttype = 2 begin select @safeamt = isnull(b.gak43f, 0) from gcm01 a with (nolock), gam10 b with (nolock) where a.gca00c = b.gak00c and a.gca02c = b.gak01c and a.gca01c = @cardno and a.gca00c = @ownercompid end else begin select @safeamt = 0 end if @changway = 7 begin declare @sp076_3 int select @sp076_3 = convert(int, gsa03f) from gsm01 with (nolock) where gsa00c = @compid and gsa01c = 'SP076' set @sp076_3 = isnull(@sp076_3, 1) if @sp076_3 < 1 set @sp076_3 = 1 set @newdate = convert(varchar(10), dateadd(year, @sp076_3, getdate()), 112) end insert gcm03(gcc00c, gcc01c, gcc02f, gcc03i, gcc06f, gcc07f, gcc08c, gcc09d) values(@ownercompid, @cardno, @flowno2, @accttype, 0, @safeamt, @date, @newdate) end if @confirmflag = 1 --复核 begin -- 取款等 if @changway = 1 or @changway = 2 or @changway = 4 or @changway = 5 begin update gcm03 set gcc06f = isnull(gcc06f, 0) - @consume where gcc00c = @ownercompid and gcc01c = @cardno and gcc03i = @accttype -- 写入欠款金额 Added by mzh 2006/02/22 if (@changway = 5) begin update gcm03 set gcc10f = isnull(gcc10f, 0) + @consume where gcc00c = @ownercompid and gcc01c = @cardno and gcc03i = @accttype end select @lastremain = isnull(gcc06f, 0) + @consume from gcm03 with (nolock) where gcc00c = @ownercompid and gcc01c = @cardno and gcc03i = @accttype -- end else begin --start 帐户期限判断 lucz 2015.10.29 if (exists(select 1 from gam29 with (nolock) where gax00c = @ownercompid and gax01c = @gca02c and gax02i = @accttype and gax03i =1 )) begin set @newdate = '' end --end lucz 2015.10.29 if isnull(@newdate, '') = '' begin update gcm03 set gcc06f = isnull(gcc06f, 0) + @consume, -- 暂时不更新账户的开立日期 固这么写 gcc08c = gcc08c--@date where gcc00c = @ownercompid and gcc01c = @cardno and gcc03i = @accttype -- end else begin update gcm03 set gcc06f = isnull(gcc06f, 0) + @consume, -- 暂时不更新账户的开立日期 固这么写 gcc08c = gcc08c, --@date gcc09d = @newdate where gcc00c = @ownercompid and gcc01c = @cardno and gcc03i = @accttype -- end -- 还款,抵消欠款金额 added by mzh 2006/02/22 if (@changway = 6) begin update gcm03 set gcc10f = isnull(gcc10f, 0) - @consume where gcc00c = @ownercompid and gcc01c = @cardno and gcc03i = @accttype end select @lastremain = isnull(gcc06f, 0) - @consume from gcm03 with (nolock) where gcc00c = @ownercompid and gcc01c = @cardno and gcc03i = @accttype -- end -- 取得操作日期与时间 declare @curdate varchar(20), @curtime varchar(20) exec upg_getcurrdate @curdate output, @curtime output -- 似乎没有必要将为零的数据写到gcm04中 if @consume <> 0 begin declare @sp0403f float declare @isinputout_raw_bill int select @sp0403f = gsa03f from gsm01 with (nolock) where gsa00c = @compid and gsa01c = 'SP040' if @sp0403f = 2 or @sp0403f = 3 begin select @isinputout_raw_bill = 0 end else begin select @isinputout_raw_bill = 1 end exec upg_get_gcm04_flowno @cardno, @accttype, @gcm04_flowno output insert gcm04(gcd00c, gcd01c, gcd02i, gcd04f, gcd05i, gcd06f, gcd07c, gcd08c, gcd09d, gcd10f, gcd11c, gcd12i, gcd13t, gcd14c, gcd17i) values(@compid, @cardno, @accttype, @gcm04_flowno, @changway, @consume, @billtype, @billno, @date, @lastremain, @curdate, @isinputout_raw_bill, @curtime, @billmemo, @proc_seq) end end else -- 取消复核 begin if exists(select 1 from gcm04 with (nolock) where gcd00c = @compid and gcd01c = @cardno and gcd02i = @accttype and gcd05i = @changway and gcd07c = @billtype and gcd08c = @billno and (isnull(@proc_seq, -1) = -1 or isnull(gcd17i, -1) = -1 or gcd17i = @proc_seq)) begin if @changway = 1 or @changway = 2 or @changway = 4 or @changway = 5 begin update gcm03 set gcc06f = isnull(gcc06f, 0) + @consume where gcc00c = @ownercompid and gcc01c = @cardno and gcc03i = @accttype -- -- 欠款金额 added by mzh 2006/02/22 if (@changway = 5) begin update gcm03 set gcc10f = isnull(gcc10f, 0) - @consume where gcc00c = @ownercompid and gcc01c = @cardno and gcc03i = @accttype end end else begin update gcm03 set gcc06f = isnull(gcc06f, 0) - @consume where gcc00c = @ownercompid and gcc01c = @cardno and gcc03i = @accttype -- if (@changway = 6) begin update gcm03 set gcc10f = isnull(gcc10f, 0) + @consume where gcc00c = @ownercompid and gcc01c = @cardno and gcc03i = @accttype end end delete from gcm04 where gcd00c = @compid and gcd01c = @cardno and gcd02i = @accttype and gcd05i = @changway and gcd07c = @billtype and gcd08c = @billno and (isnull(@proc_seq, -1) = -1 or isnull(gcd17i, -1) = -1 or gcd17i = @proc_seq) end end end -- 提交事务 -- 有可提交的事务, 并且事务是在当前模块中开启的情况下, 才提交事务 if xact_state() = 1 and @__transaction_count = 0 commit -- 为了防止TRY 中有遗漏的事务处理, 可以在TRY 模拟的结束部分做最终的判断 if @__transaction_count = 0 begin if xact_state() = -1 rollback tran else begin while @@trancount > 0 commit tran end end end try begin catch -- 在CATCH 模块, 应该首先处理事务 if xact_state() <> 0 begin if @__transaction_count = 0 rollback tran -- xact_state 为-1 时, 不能回滚到事务保存点, 这种情况留给外层调用者做统一的事务回滚 -- 通过@@trancount > @__transaction_count 的判断, 即使在TRY 模块中没有设置事务保存点的情况下跳到此步骤, 也不会出错 else if xact_state() = 1 and @@trancount > @__transaction_count rollback tran __tran_upg_purse_deduct end return -1 end catch end GO /****** Object: StoredProcedure [dbo].[upg_create_buyproc_payment_datescope] Script Date: 06/12/2017 06:19:50 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_create_buyproc_payment_datescope] ( @compid varchar(10), @fromdate varchar(10), @todate varchar(10) ) as begin -- 为了性能,需要先判断是否有购买疗程的功能 declare @has_proc_fun int if exists(select 1 from gcm06 with (nolock)) begin select @has_proc_fun = 1 end else begin select @has_proc_fun = 0 end declare @billcompid varchar(10) declare @billid varchar(20) -- 先处理会员卡销售过程中的疗程购买 declare cur_each_gnm03 cursor for select distinct gna00c, gna01c from gnm01 a, gam26 z where a.gna00c = z.gaz02c and z.gaz01c = @compid and a.gna80d >= @fromdate and a.gna80d <= @todate open cur_each_gnm03 fetch cur_each_gnm03 into @billcompid, @billid while @@fetch_status = 0 begin exec upg_create_buyproc_payment @billcompid, 'gn', @billid fetch cur_each_gnm03 into @billcompid, @billid end close cur_each_gnm03 deallocate cur_each_gnm03 -- 处理会员账户管理购买的疗程 declare cur_each_gcm11 cursor for select distinct gcl00c, gcl01c from gcm10 a, gam26 z where a.gcl00c = z.gaz02c and z.gaz01c = @compid and a.gcl80d >= @fromdate and a.gcl80d <= @todate open cur_each_gcm11 fetch cur_each_gcm11 into @billcompid, @billid while @@fetch_status = 0 begin exec upg_create_buyproc_payment @billcompid, 'gz', @billid fetch cur_each_gcm11 into @billcompid, @billid end close cur_each_gcm11 deallocate cur_each_gcm11 end GO /****** Object: StoredProcedure [dbo].[upg_create_combo_paymat] Script Date: 06/12/2017 06:19:50 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_create_combo_paymat] ( @compid varchar(10), -- 公司别 @fromdate varchar(20), -- 开始日期 @todate varchar(20) -- 截止日期 ) as begin declare @each_compid varchar(20) declare @billid varchar(20) declare cur_each_bill cursor for select gna00c, gna01c from gnm01 a with (nolock), gam26 z where a.gna00c = z.gaz02c and z.gaz01c = @compid and a.gna02d >= @fromdate and a.gna02d <= @todate open cur_each_bill fetch cur_each_bill into @each_compid, @billid while @@fetch_status = 0 begin exec upg_share_combo_amtrate @each_compid, 'gn', @billid exec upg_create_buyproc_payment @each_compid, 'gn', @billid fetch cur_each_bill into @each_compid, @billid end close cur_each_bill deallocate cur_each_bill declare cur_each_bill_2 cursor for select gcl00c, gcl01c from gcm10 a with (nolock), gam26 z where a.gcl00c = z.gaz02c and z.gaz01c = @compid and a.gcl02d >= @fromdate and a.gcl02d <= @todate open cur_each_bill_2 fetch cur_each_bill_2 into @each_compid, @billid while @@fetch_status = 0 begin exec upg_share_combo_amtrate @each_compid, 'gz', @billid exec upg_create_buyproc_payment @each_compid, 'gz', @billid fetch cur_each_bill_2 into @each_compid, @billid end close cur_each_bill_2 deallocate cur_each_bill_2 end GO /****** Object: StoredProcedure [dbo].[upg_cal_compensation_single] Script Date: 06/12/2017 06:19:32 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_cal_compensation_single] ( @billid varchar(20), -- 发薪单号 @compid varchar(10), -- 公司别 @empid varchar(10) -- 员工编号 ) as-- WUXM, 2004/03/17 begin declare @fromdate varchar(8) --开始日期 declare @todate varchar(8) declare @tax float declare @taxType int declare @num float create table #tmp_comm ( stuff varchar(10) not null, comm float null ) select @fromdate = hca05d, @todate = hca06d, @taxType = hca09i, @tax = hca10f from hcm01 where hca00c = @compid and hca01c = @billid -- LOAD员工薪资项目设定至员工发薪金额明细中 delete hcm03 where hcc00c = @compid and hcc01c = @billid and hcc02c = @empid insert into hcm03(hcc00c, hcc01c, hcc02c, hcc03c, hcc04c, hcc05c, hcc06f, hcc07c, hcc09f) select hbb00c, @billid, hbb01c, hbb02c, hbb03c, hbb04c, hbb05f, hbb06c, hbb05f from hbm02 where hbb00c = @compid and hbb01c = @empid set @num = 0 select @num = isnull(count(*), 0) from hdm01 where hda00c = @compid and hda01c = @empid and hda02d between @fromdate and @todate and hda07c = '1' update hcm03 set hcc08f = @num from hcm03 where hcm03.hcc00c = @compid and hcm03.hcc02c = @empid and hcm03.hcc01c = @billid and hcm03.hcc05c = '2' and hcm03.hcc07c = '1' set @num = 0 select @num = isnull(count(gna25c), 0) + isnull(count(gnb03c), 0) from gnm01, gnm02 where gna00c = @compid and gna20c = @empid and gna02d between @fromdate and @todate and gna00c = gnb00c and gna01c = gnb01c update hcm03 set hcc08f = @num from hcm03 where hcm03.hcc00c = @compid and hcm03.hcc02c = @empid and hcm03.hcc01c = @billid and hcm03.hcc05c = '2' and hcm03.hcc07c = '2' -- 计算提成薪水部分 declare @spid int select @spid = @@spid insert #tmp_comm(stuff, comm) exec upg_personal_statistics @compid, @fromdate, @todate, @empid, @empid, 1, 1, 1, 2, @spid -- 2:每个员工计算一个提成数据 update a set a.hcc09f = isnull(b.comm, 0), a.hcc06f = isnull(b.comm, 0), a.hcc08f = 1 from hcm03 a, #tmp_comm b where a.hcc00c = @compid and a.hcc02c = b.stuff and a.hcc05c = '2' and a.hcc04c = '006' -- 提成薪资项目代码 and a.hcc01c = @billid delete #tmp_comm insert #tmp_comm(stuff, comm) select hah01c, sum(hah05f) from ham08 where hah00c = @compid and hah01c = @empid and hah03d >= @fromdate and hah03d <= @todate group by hah00c, hah01c update a set a.hcc09f = isnull(b.comm, 0), a.hcc06f = isnull(b.comm, 0), a.hcc08f = 1 from hcm03 a, #tmp_comm b where a.hcc00c = @compid and a.hcc02c = b.stuff and a.hcc05c = '2' and a.hcc04c = '007' -- 奖罚 and a.hcc01c = @billid drop table #tmp_comm update hcm03 set hcc06f = isnull(hcc06f, 0), hcc08f = isnull(hcc08f, 0), hcc09f = isnull(hcc09f, 0) where hcc00c = @compid and hcc01c = @billid and hcc02c = @empid update hcm03 set hcc09f = isnull(hcc08f, 0) * isnull(hcc06f, 0) where hcc00c = @compid and hcc01c = @billid and hcc02c = @empid and hcc05c = '2' -- LOAD员工资料至员工发薪总额表中 if not exists(select 1 from hcm02 where hcb00c = @compid and hcb01c = @billid and hcb02c = @empid) begin insert into hcm02(hcb02c, hcb00c, hcb01c) values(@empid, @compid, @billid) end -- 计算发放总额及扣款总额 update hcm02 set hcb03f = (select sum(isnull(hcc09f, 0)) from hcm03 where hcc00c = @compid and hcc01c = @billid and hcc03c = '1' and hcc02c = hcm02.hcb02c group by hcc02c), hcb04f = (select sum(isnull(hcc09f, 0)) from hcm03 where hcc00c = @compid and hcc01c = @billid and hcc03c = '2' and hcc02c = hcm02.hcb02c group by hcc02c) where hcb00c = @compid and hcb01c = @billid and hcb02c = @empid declare @stuffNo varchar(10) -- 员工编号 declare @InSum float -- 发放总额 declare @OutSum float -- 扣款总额 declare @taxSum float -- 税额 declare @tax2 float -- 税 declare @proSum float -- 累进金额 declare @income float -- 收入 select @stuffNo = hcb02c, @InSum = isnull(hcb03f, 0), @OutSum = isnull(hcb04f, 0) from hcm02 where hcb00c = @compid and hcb01c = @billid and hcb02c = @empid set @taxSum = 0 set @income = @InSum - @OutSum if (@taxType = 2) begin declare @SP019 float select @SP019 = isnull(gsa03f, 0) from gsm01 where gsa00c = '001' and gsa01c = 'SP019' set @taxSum = (@income - @SP019) * @tax end else begin exec upg_cal_tax @income, 0, @taxSum output end set @income = @income - @taxSum update hcm02 set hcb05f = isnull(@taxSum, 0), hcb06f = isnull(@income, 0) where hcb00c = @compid and hcb01c = @billid and hcb02c = @stuffNo update hcm02 set hcb03f = isnull(hcb03f, 0), hcb04f = isnull(hcb04f, 0), hcb05f = isnull(hcb05f, 0), hcb06f = isnull(hcb06f, 0) where hcb00c = @compid and hcb01c = @billid and hcb02c = @empid end GO /****** Object: StoredProcedure [dbo].[upg_purse_deduct_fic] Script Date: 06/12/2017 06:20:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_purse_deduct_fic] ( @compid varchar(10), -- 公司编号 @cardno varchar(20), -- 会员卡号 @date varchar(10), -- 异动日期 @accttype int, -- 账户类型(1.电子钱包 2.储值账户) @consume float, -- 支付金额 @changway int, -- 异动方向(0-充值, 1-取款, 2-消费, 3-转入,4-转出, 5-欠款 6-欠款返回) @billtype varchar(5), -- 单据类别 @billno varchar(20), -- 单据号 @confirmflag int, -- 是否复核的标记(1.复核 0.取消复核) @salse varchar(20) ) as--WUXM, 2004/12/26 begin -- 根据卡号获取归属公司: declare @ownercompid nvarchar(10) --卡的归属公司 declare @gca02c nvarchar(5) -- 卡类别 select @ownercompid = gca13d, @gca02c = gca02c from gcm01 where gca01c = @cardno and gca00c = gca13d declare @gcm04_flowno float declare @lastremain float declare @newdate varchar(8) declare @billmemo varchar(160) -- 单据的备注 if @accttype = 3 begin declare @start_points varchar(20) select @start_points = gsa02c from gsm01 where gsa00c = @compid and gsa01c = 'SP110' if isnull(@start_points, '')<>'1' begin return end declare @gak76i int select @gak76i = gak76i from gam10 where gak00c = @ownercompid and gak01c = @gca02c if isnull(@gak76i, 0) = 1 return end select @accttype = isnull(@accttype, 2) if @accttype = 999 return --取得新的有效日期 if @changway = 0 and @billtype = 'gz' and @confirmflag = 1 begin select @newdate = gcl21d, @billmemo = gcl57c from gcm10 with (nolock) where gcl00c = @compid and gcl01c = @billno end if @changway = 0 and @billtype = 'gn' and @confirmflag = 1 begin select @billmemo = gna07c from gnm01 with (nolock) where gna00c = @compid and gna01c = @billno end --取得下一个流水号 --如果在账户余额表中不存在,则增加此纪录 if not exists(select 1 from gcm03 with (nolock) where gcc00c = @ownercompid and gcc01c = @cardno and gcc03i = @accttype) begin declare @flowno2 int declare @safeamt float select @flowno2 = isnull(max(gcc02f), 0) + 1 from gcm03 with (nolock) where gcc00c = @ownercompid and gcc01c = @cardno select @flowno2 = isnull(@flowno2, 1) if @accttype = 2 begin select @safeamt = isnull(b.gak43f, 0) from gcm01 a, gam10 b where a.gca00c = b.gak00c and a.gca02c = b.gak01c and a.gca00c = @ownercompid and a.gca01c = @cardno end else begin select @safeamt = 0 end insert gcm03(gcc00c, gcc01c, gcc02f, gcc03i, gcc06f, gcc07f, gcc08c, gcc09d) values(@ownercompid, @cardno, @flowno2, @accttype, 0, @safeamt, @date, @newdate) end if @confirmflag = 1 --复核 begin begin transaction tr_003 --取款等 if @changway = 1 or @changway = 2 or @changway = 4 or @changway = 5 begin update gcm03 set gcc06f = isnull(gcc06f, 0)-@consume where gcc00c = @ownercompid and gcc01c = @cardno and gcc03i = @accttype --写入欠款金额 Added by mzh 2006/02/22 if (@changway = 5) begin update gcm03 set gcc10f = isnull(gcc10f, 0) + @consume where gcc00c = @ownercompid and gcc01c = @cardno and gcc03i = @accttype end select @lastremain = isnull(gcc06f, 0) + @consume from gcm03 with (nolock) where gcc00c = @ownercompid and gcc01c = @cardno and gcc03i = @accttype end else begin if isnull(@newdate, '') = '' begin update gcm03 set gcc06f = isnull(gcc06f, 0) + @consume, gcc08c = @date where gcc00c = @ownercompid and gcc01c = @cardno and gcc03i = @accttype end else begin update gcm03 set gcc06f = isnull(gcc06f, 0) + @consume, gcc08c = @date, gcc09d = @newdate where gcc00c = @ownercompid and gcc01c = @cardno and gcc03i = @accttype end --还款,抵消欠款金额 added by mzh 2006/02/22 if (@changway = 6) begin update gcm03 set gcc10f = isnull(gcc10f, 0) - @consume where gcc00c = @ownercompid and gcc01c = @cardno and gcc03i = @accttype end select @lastremain = isnull(gcc06f, 0)-@consume from gcm03 with (nolock) where gcc00c = @ownercompid and gcc01c = @cardno and gcc03i = @accttype end --取得操作日期与时间 declare @curdate varchar(20), @curtime varchar(20) exec upg_getcurrdate @curdate output, @curtime output --似乎没有必要将为零的数据写到gcm04中 if @consume <> 0 begin declare @sp0403f float declare @isinputout_raw_bill int select @sp0403f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP040' if @sp0403f = 2 or @sp0403f = 3 begin select @isinputout_raw_bill = 0 end else begin select @isinputout_raw_bill = 1 end exec upg_get_gcm04_flowno @cardno, @accttype, @gcm04_flowno output insert gcm04(gcd00c, gcd01c, gcd02i, gcd04f, gcd05i, gcd06f, gcd07c, gcd08c, gcd09d, gcd10f, gcd11c, gcd12i, gcd13t, gcd14c, gcd16c) values(@compid, @cardno, @accttype, @gcm04_flowno, @changway, @consume, @billtype, @billno, @date, @lastremain, @curdate, @isinputout_raw_bill, @curtime, @billmemo, @salse) end commit transaction tr_003 end else --取消复核 begin if @changway = 1 or @changway = 2 or @changway = 4 or @changway = 5 begin update gcm03 set gcc06f = isnull(gcc06f, 0) + @consume where gcc00c = @ownercompid and gcc01c = @cardno and gcc03i = @accttype --欠款金额 added by mzh 2006/02/22 if (@changway = 5) begin update gcm03 set gcc10f = isnull(gcc10f, 0) - @consume where gcc00c = @ownercompid and gcc01c = @cardno and gcc03i = @accttype end end else begin update gcm03 set gcc06f = isnull(gcc06f, 0)-@consume where gcc00c = @ownercompid and gcc01c = @cardno and gcc03i = @accttype if (@changway = 6) begin update gcm03 set gcc10f = isnull(gcc10f, 0) + @consume where gcc00c = @ownercompid and gcc01c = @cardno and gcc03i = @accttype end end delete from gcm04 where gcd00c = @compid and gcd01c = @cardno and gcd02i = @accttype and gcd05i = @changway and gcd07c = @billtype and gcd08c = @billno end end GO /****** Object: StoredProcedure [dbo].[upg_delete_gnm01] Script Date: 06/12/2017 06:19:58 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_delete_gnm01] ( @compid varchar(10), @billid varchar(20), @cardid varchar(30) ) as begin exec upg_create_gcm20_from_one_bill @compid, 'gn', @billid, 0 --如果有开卡纪录,需要删除 delete a from gem01 a, gnm02 b where a.gea00c = b.gnb00c and a.gea04c = b.gnb03c and a.gea02i = 2 and b.gnb00c = @compid and b.gnb01c = @billid --将卡的状态修改为未销售 --因为删除销售单的情况下很可能是卡错了, 所以需要重新建立这个卡, 在这个地方就先删除这个卡 delete a from gcm01 a, gnm02 b where a.gca00c = b.gnb00c and a.gca01c = b.gnb03c and b.gnb00c = @compid and b.gnb01c = @billid --卡销售错了,直接删除会员资料好象不好 --只删除与卡号相同的会员 --不相同的只要把对应的卡清空 delete a from gbm01 a, gnm02 b where a.gba00c = b.gnb00c and a.gba23c = b.gnb03c and b.gnb00c = @compid and b.gnb01c = @billid and a.gba01c = a.gba23c if @@rowcount = 0 begin update a set a.gba23c = '' from gbm01 a, gnm02 b where a.gba00c = b.gnb00c and a.gba23c = b.gnb03c and b.gnb00c = @compid and b.gnb01c = @billid and a.gba01c<> a.gba23c end --删除这个单据在会员卡帐户历史中的数据 create table #tbl_card_act_remain( cardid varchar(20) not null, acctid int not null, amt float null) insert #tbl_card_act_remain(cardid, acctid, amt) select gcd01c, gcd02i, sum(case when gcd05i = 0 then gcd06f else 0-gcd06f end) from gcm04 a with (nolock) where a.gcd00c = @compid and a.gcd07c = 'gn' and a.gcd08c = @billid and a.gcd01c = @cardid group by gcd01c, gcd02i update a set a.gcc06f = a.gcc06f - isnull(b.amt, 0) from gcm03 a, #tbl_card_act_remain b where a.gcc00c = @compid and a.gcc01c = b.cardid and a.gcc03i = b.acctid drop table #tbl_card_act_remain delete a from gcm04 a where a.gcd00c = @compid and a.gcd07c = 'gn' and a.gcd08c = @billid --删除可能的疗程历史gcm08 delete a from gcm08 a where a.gch00c = @compid and a.gch10c = 'gn' and a.gch11c = @billid --删除这个卡可能的套餐历史 delete a from gtc07 a where a.gti00c = @compid and a.gti08c = 'gn' and a.gti09c = @billid delete a from gcm05 a, gnm02 b where a.gce00c = b.gnb00c and a.gce01c = b.gnb03c and b.gnb00c = @compid and b.gnb01c = @billid delete a from gcm02 a, gnm02 b where a.gcb00c = b.gnb00c and a.gcb01c = b.gnb03c and b.gnb00c = @compid and b.gnb01c = @billid --删除可能存在的欠款异动单 delete a from gcm10 a, gnm02 b where a.gcl00c = b.gnb00c and a.gcl03c = b.gnb03c and b.gnb00c = @compid and b.gnb01c = @billid --删除会员卡基本资料中的疗程资料 delete a from gcm06 a, gnm02 b where a.gcf00c = b.gnb00c and a.gcf01c = b.gnb03c and b.gnb00c = @compid and b.gnb01c = @billid --可能存在的套餐的产品出库也应该删除 delete from gfm03 where gfc00c = @compid and gfc01c = '5' and gfc02c = @billid delete from gfm04 where gfd00c = @compid and gfd01c = '5' and gfd02c = @billid --删除这个单据对应的促销明细也应该删除 delete gdm10 where gdj00c = @compid and gdj01c = 'gn' and gdj02c = @billid --可能存在的保证金转付需要删除 declare @prepay_amt float -- 保证金转付的金额 select @prepay_amt = gsc05f from gsm03 with (nolock) where gsc00c = @compid and gsc01c = @billid and gsc02c = 'gn' and gsc04c = '&' if isnull(@prepay_amt, 0)>0 begin update a set gbl07f = isnull(gbl07f, 0) - @prepay_amt, gbl09c = '', gbl10c = '', gbl11f = isnull(gbl11f, 0) + @prepay_amt, gbl12i = 0, gbl18t = '' from gbm15 a where gbl09c = 'gn' and gbl10c = @billid and gbl00c = @compid end delete gnm01 where gna00c = @compid and gna01c = @billid end GO /****** Object: StoredProcedure [dbo].[upg_re_updatedepart_all_must] Script Date: 06/12/2017 06:20:31 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_re_updatedepart_all_must]( @compid varchar(10), @fromdate varchar(10), @todate varchar(10)) as begin --先清空 update a set a.gna81c = '',a.gna82c = '',a.gna83c = '' from gnm01 a where a.gna00c = @compid and a.gna02d >= @fromdate and a.gna02d <= @todate --update gnm01 因为大工是必须输入的,所以只要更新大工就可以了 update a set a.gna81c = b.haa06c from gnm01 a ,ham01 b where a.gna00c = @compid and a.gna02d >= @fromdate and a.gna02d <= @todate and a.gna00c=b.haa00c and b.haa01c=a.gna20c and isnull(gna20c,'')<>'' update a set a.gna82c = b.haa06c from gnm01 a ,ham01 b where a.gna00c=@compid and a.gna02d >= @fromdate and a.gna02d <= @todate and a.gna00c=b.haa00c and b.haa01c=a.gna28c and isnull(gna28c,'')<>'' update a set a.gna83c = b.haa06c from gnm01 a ,ham01 b where a.gna00c=@compid and a.gna02d >= @fromdate and a.gna02d <= @todate and a.gna00c=b.haa00c and b.haa01c=a.gna30c and isnull(gna30c,'')<>'' --update gcm10 因为大工是必须输入的,所以只要更新大工就可以了 update a set a.gcl81c = '',a.gcl82c = '',a.gcl83c = '' from gcm10 a where a.gcl00c=@compid and a.gcl02d >= @fromdate and a.gcl02d <= @todate update a set a.gcl81c = b.haa06c from gcm10 a, ham01 b where a.gcl00c=@compid and a.gcl02d >= @fromdate and a.gcl02d <= @todate and a.gcl00c=b.haa00c and a.gcl09c = b.haa01c and isnull(gcl09c,'')<>'' update a set a.gcl82c = b.haa06c from gcm10 a, ham01 b where a.gcl00c=@compid and a.gcl02d >= @fromdate and a.gcl02d <= @todate and a.gcl00c=b.haa00c and a.gcl17c = b.haa01c and isnull(gcl17c,'')<>'' update a set a.gcl83c = b.haa06c from gcm10 a, ham01 b where a.gcl00c=@compid and a.gcl02d >= @fromdate and a.gcl02d <= @todate and a.gcl00c=b.haa00c and a.gcl19c = b.haa01c and isnull(gcl19c,'')<>'' --update ggm02,用第一员工来判断 --先设置为空 update a set a.ggb81c='' from ggm02 a,ggm01 b where a.ggb00c = b.gga00c and a.ggb01c = b.gga01c and b.gga00c = @compid and b.gga02d>=@fromdate and b.gga02d<=@todate --设置项目的部门 update a set a.ggb81c=b.haa06c from ggm02 a,ham01 b,ggm01 c where a.ggb00c = c.gga00c and a.ggb01c = c.gga01c and c.gga00c = @compid and c.gga02d>=@fromdate and c.gga02d<=@todate and a.ggb00c=b.haa00c and a.ggb12c=b.haa01c and isnull(ggb12c,'')<>'' --可能没有第1员工,则用第2员工来判断 update a set a.ggb81c = b.haa06c from ggm02 a ,ham01 b,ggm01 c where a.ggb00c = c.gga00c and a.ggb01c = c.gga01c and c.gga00c = @compid and c.gga02d>=@fromdate and c.gga02d<=@todate and a.ggb00c=b.haa00c and a.ggb13c=b.haa01c and isnull(ggb81c,'')='' and isnull(ggb13c,'')<>'' update a set a.ggb81c = b.haa06c from ggm02 a ,ham01 b,ggm01 c where a.ggb00c = c.gga00c and a.ggb01c = c.gga01c and c.gga00c = @compid and c.gga02d>=@fromdate and c.gga02d<=@todate and a.ggb00c=b.haa00c and a.ggb14c=b.haa01c and isnull(ggb81c,'')='' and isnull(ggb14c,'')<>'' update a set a.ggb81c = b.haa06c from ggm02 a ,ham01 b,ggm01 c where a.ggb00c = c.gga00c and a.ggb01c = c.gga01c and c.gga00c = @compid and c.gga02d>=@fromdate and c.gga02d<=@todate and a.ggb00c=b.haa00c and a.ggb26c=b.haa01c and isnull(ggb81c,'')='' and isnull(ggb26c,'')<>'' --update ggm03 --先设置产品销售的部门为空 exec upg_re_updatedepart_all_must_good @compid,@fromdate,@todate --update ggm05 --先设置为空 update a set a.gge81c = '' from ggm05 a,ggm01 c where a.gge00c = c.gga00c and a.gge01c = c.gga01c and c.gga00c = @compid and c.gga02d>=@fromdate and c.gga02d<=@todate update a set a.gge81c=b.haa06c from ggm05 a,ham01 b,ggm01 c where a.gge00c = c.gga00c and a.gge01c = c.gga01c and c.gga00c = @compid and c.gga02d>=@fromdate and c.gga02d<=@todate and a.gge00c=b.haa00c and a.gge12c = b.haa01c and isnull(gge12c,'')<>'' --update gqm01 --先设置为空 update a set a.gqa81c='' from gqm01 a where a.gqa00c = @compid and a.gqa02d>=@fromdate and a.gqa02d<=@todate update a set a.gqa81c=b.haa06c from gqm01 a,ham01 b where a.gqa00c = @compid and a.gqa02d>=@fromdate and a.gqa02d<=@todate and a.gqa00c=b.haa00c and a.gqa07c=b.haa01c and isnull(gqa07c,'')<>'' --update gem01 update a set a.gea81c='' from gem01 a where a.gea00c=@compid and a.gea03d>=@fromdate and a.gea03d <=@todate update a set a.gea81c=b.haa06c from gem01 a,ham01 b where a.gea00c=@compid and a.gea03d>=@fromdate and a.gea03d <=@todate and a.gea00c=b.haa00c and a.gea21c=b.haa01c and isnull(gea21c,'')<>'' end GO /****** Object: StoredProcedure [dbo].[upg_delete_card] Script Date: 06/12/2017 06:19:58 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_delete_card] ( @compid varchar(10), @from varchar(20), @to varchar(20), @userid varchar(20), @curdate varchar(10), @curtime varchar(10) ) as begin declare @incompid varchar(10) declare @sp039 varchar(20) select @sp039 = gsa02c from gsm01 where gsa00c = @compid and gsa01c = 'SP039' insert sysoperationlog(userid, program, operation, operationdate, operationtime, origatedate, compid, keyvalue1, keyvalue2, keyvalue3) values(@userid, 'GSB01I', 'D', @curdate, @curtime, @curdate, @compid, @from, @to, 'upg_delete_card') select @incompid = @compid if @incompid = '*' select @incompid = '001' if @compid = '*' or @compid = '' begin select @compid = '%' end else if (@from = @to) begin if exists(select 1 from gcm01 with (nolock) where gca00c = gca13d and gca01c = @from and gca13d = @compid) select @compid = '%' end exec upg_save_deleted_card @compid, @from, @to, @userid, @curdate, @curtime delete a from gcm01 a where gca01c >= @from and gca01c <= @to delete a from gcm02 a where gcb01c >= @from and gcb01c <= @to delete a from gbm01 a where gba23c >= @from and gba23c <= @to --在强行删除卡的时候, 是否合适删除这个卡的所有原始单据 delete a from gnm02 a, gam26 z where gnb00c = z.gaz02c and z.gaz01c = @incompid and gnb03c >= @from and gnb03c <= @to delete a from gcm10 a, gam26 z where gcl00c = z.gaz02c and z.gaz01c = @incompid and gcl03c >= @from and gcl03c <= @to delete a from gem01 a, gam26 z where gea00c = z.gaz02c and z.gaz01c = @incompid and gea04c >= @from and gea04c <= @to delete a from gcm05 a, gam26 z where gce00c = z.gaz02c and z.gaz01c = @incompid and gce01c >= @from and gce01c <= @to delete a from gcm04 a, gam26 z where gcd00c = z.gaz02c and z.gaz01c = @incompid and gcd01c >= @from and gcd01c <= @to --删除卡的交易历史 delete a from gcm20 a, gam26 z where gct00c = z.gaz02c and z.gaz01c = @incompid and gct02c >= @from and gct02c <= @to delete a from gtc07 a, gam26 z where gti00c = z.gaz02c and z.gaz01c = @incompid and gti03c >= @from and gti03c <= @to end GO /****** Object: StoredProcedure [dbo].[upg_dispatch_data_pay_classify_single] Script Date: 06/12/2017 06:20:01 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_dispatch_data_pay_classify_single] ( @compid varchar(10) ) as begin create table #tbl_control ( compid varchar(10) not null, -- 儿子公司的代码 lvl int null, primary key(compid) ) insert #tbl_control(compid, lvl) select bran_id, control_lvl from b_perf a, basic_data_control b where parent_id = @compid and a.bran_id = b.compid and b.datatype = 'pay_classify' and b.control_lvl = 1 -- 创建循环变量 declare @HasRecord int set @HasRecord = 1 declare @currentCompId varchar(10) declare @lvl int while (@HasRecord = 1) begin if exists (select 1 from #tbl_control) begin set @HasRecord = 1 select top 1 @currentCompId = compid, @lvl = lvl from #tbl_control if @lvl = 1 -- 严格按总部的 begin -- 先删除自己的,把总部分发下来 -- 强制模式都是先删除原来的 exec upg_m_dispatch_pay_classify_single @compid, @currentCompId, 1 insert #tbl_control(compid, lvl) select bran_id, control_lvl from b_perf a, basic_data_control b where parent_id = @currentCompId and a.bran_id = b.compid and b.datatype = 'pay_classify' and b.control_lvl = 1 end else if @lvl = 2 begin exec upg_m_dispatch_pay_classify_single @compid, @currentCompId, 0 insert #tbl_control(compid, lvl) select bran_id, case when (control_lvl <= 2) then 2 else control_lvl end from b_perf a, basic_data_control b where parent_id = @currentCompId and a.bran_id = b.compid and b.datatype = 'pay_classify' and b.control_lvl = 1 end -- 清除已处理的门店 delete #tbl_control where compid = @currentCompId end else begin set @HasRecord = 0 end end drop table #tbl_control end GO /****** Object: StoredProcedure [dbo].[upg_buy_combo] Script Date: 06/12/2017 06:19:31 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_buy_combo] ( @compid varchar(10), @cardid varchar(20), @comboid varchar(20), @num float, -- 套餐数量 @confirm int, @billtype varchar(20), @billid varchar(20), @curdate varchar(8), @deStock int, -- 购买套餐的时候,产品是否出库:0,不出库;1,出库 @stockid varchar(20), -- 仓库代码 @outStockBill varchar(20), -- 出库单号 @operater varchar(20) -- 操作人 ) as begin -- 根据卡号获取归属公司: declare @ownercompid nvarchar(10) select @ownercompid = min(gca13d) from gcm01 where gca01c = @cardid and gca00c = gca13d declare @prjid varchar(20) declare @prjtimes float declare @gti14i int declare @pretimes float declare @amtrate float declare @pay_1 varchar(10), @pay_2 varchar(10), @pay_3 varchar(10), @pay_4 varchar(10) declare @amt_1 float, @amt_2 float, @amt_3 float, @amt_4 float -- 处理会员卡--套餐变化历史 if @billtype = 'gn' begin select @pay_1 = gtf09c, @amt_1 = isnull(gtf10f, 0), @pay_2 = gtf11c, @amt_2 = isnull(gtf12f, 0), @pay_3 = gtf13c, @amt_3 = isnull(gtf14f, 0), @pay_4 = gtf15c, @amt_4 = isnull(gtf16f, 0) from gtc05 with (nolock) where gtf00c = @compid and gtf01c = @billid and gtf02c = @comboid end else if @billtype = 'gz' begin select @pay_1 = gth09c, @amt_1 = isnull(gth10f, 0), @pay_2 = gth11c, @amt_2 = isnull(gth12f, 0), @pay_3 = gth13c, @amt_3 = isnull(gth14f, 0), @pay_4 = gth15c, @amt_4 = isnull(gth16f, 0) from gtc06 with (nolock) where gth00c = @compid and gth01c = @billid and gth02c = @comboid end -- 处理会员卡-套餐次数 if @confirm = 1 begin exec upg_give_card_combo @compid, @billtype, @billid, @comboid, @cardid, 1 end else begin exec upg_give_card_combo @compid, @billtype, @billid, @comboid, @cardid, 0 end if @confirm = 1 begin -- 先把金额的比率计算出来 exec upg_share_combo_amtrate @compid, @billtype, @billid -- 套餐的项目 部分 declare cur1 cursor for select gti04c, gti05f, isnull(gti09f, 0) from gtc09 with (nolock) where gti00c = @compid and gti01c = @billtype and gti02c = @billid and gti03c = @comboid and isnull(gti04c, '') = isnull(gti08c, '') open cur1 fetch next from cur1 into @prjid, @prjtimes, @amtrate while @@fetch_status = 0 begin select @gti14i = gte09i from gtc04 with (nolock) where gte00c = @ownercompid and gte01c = @cardid and gte02c = @comboid and gte03c = @prjid and gte10c = @billtype and gte11c = @billid insert gtc07(gti00c, gti02d, gti03c, gti04c, gti05c, gti06f, gti07f, gti08c, gti09c, gti10f, gti11f, gti12i, gti13c, gti17c, gti18f, gti19c, gti20f, gti21c, gti22f, gti23c, gti24f, gti14i) values(@compid, @curdate, @cardid, @comboid, @prjid, @prjtimes, 0, @billtype, @billid, 0, @prjtimes, 1, @prjid, @pay_1, @amt_1 * @amtrate, @pay_2, @amt_2 * @amtrate, @pay_3, @amt_3 * @amtrate, @pay_4, @amt_4 * @amtrate, @gti14i) fetch next from cur1 into @prjid, @prjtimes, @amtrate end close cur1 deallocate cur1 -- 套餐的产品部分 declare cur3 cursor for select gtj04c, gtj05f, isnull(gtj09f, 0) from gtc10 with (nolock) where gtj00c = @compid and gtj01c = @billtype and gtj02c = @billid and gtj03c = @comboid and isnull(gtj04c, '') = isnull(gtj08c, '') open cur3 fetch next from cur3 into @prjid, @prjtimes, @amtrate while @@fetch_status = 0 begin select @gti14i = gtj09i from gtc08 with (nolock) where gtj00c = @ownercompid and gtj01c = @cardid and gtj02c = @comboid and gtj03c = @prjid and gtj10c = @billtype and gtj11c = @billid insert into gtc07(gti00c, gti02d, gti03c, gti04c, gti05c, gti06f, gti07f, gti08c, gti09c, gti10f, gti11f, gti12i, gti13c, gti17c, gti18f, gti19c, gti20f, gti21c, gti22f, gti23c, gti24f, gti14i) values(@compid, @curdate, @cardid, @comboid, @prjid, @prjtimes, 0, @billtype, @billid, 0, @prjtimes, 2, @prjid, @pay_1, @amt_1 * @amtrate, @pay_2, @amt_2 * @amtrate, @pay_3, @amt_3 * @amtrate, @pay_4, @amt_4 * @amtrate, @gti14i) fetch next from cur3 into @prjid, @prjtimes, @amtrate end close cur3 deallocate cur3 end else -- 取消复核 begin -- 套餐的取消复核只要把gtc07中的交易历史记录直接删除就可以了 delete gtc07 where gti00c = @compid and gti08c = @billtype and gti09c = @billid end -- 处理套餐的产品的库存处理,本来放在单据的Confirm过程中实现,但因为还没有计算产品的比率 -- 所以转移到这里来处理库存 -- 20100826增加,WUXM declare @stock_mode int if @billtype = 'gn' select @stock_mode = 4 else if @billtype = 'gz' select @stock_mode = 5 end GO /****** Object: StoredProcedure [dbo].[upg_log_copy_gcm0306] Script Date: 06/12/2017 06:20:17 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_log_copy_gcm0306] ( @compid varchar(10), @cardid varchar(20), @userid varchar(20) ) as begin declare @curdate varchar(20), @time varchar(20) exec upg_getcurrdate @curdate output, @time output declare @cardcompid varchar(10) exec upg_get_card_compid @cardid, @cardcompid output if isnull(@cardcompid, '') != '' begin select @compid = @cardcompid end insert gcm03_log(gcc00c, gcc01c, gcc02f, gcc03i, gcc04c, gcc05c, gcc06f, gcc07f, gcc08c, gcc09d, gcc10f, gcc11c, compid, userid, ddate, ttime) select gcc00c, gcc01c, gcc02f, gcc03i, gcc04c, gcc05c, gcc06f, gcc07f, gcc08c, gcc09d, gcc10f, gcc11c, @compid, @userid, @curdate, @time from gcm03 with (nolock) where gcc00c = @compid and gcc01c = @cardid insert gcm06_log(gcf00c, gcf01c, gcf02c, gcf03f, gcf04f, gcf05f, gcf06f, gcf07f, gcf08f, gcf09f, gcf10f, gcf11d, gcf12d, gcf13i, gcf14f, gcf15c, gcf16i, gcf17i, gcf18f, gcf19i, gcf20i, gcf21i, gcf22i, gcf23i, gcf24i, gcf25f, compid, userid, ddate, ttime) select gcf00c, gcf01c, gcf02c, gcf03f, gcf04f, gcf05f, gcf06f, gcf07f, gcf08f, gcf09f, gcf10f, gcf11d, gcf12d, gcf13i, gcf14f, gcf15c, gcf16i, gcf17i, gcf18f, gcf19i, gcf20i, gcf21i, gcf22i, gcf23i, gcf24i, gcf25f, @compid, @userid, @curdate, @time from gcm06 with (nolock) where gcf00c = @compid and gcf01c = @cardid insert gtc04_log(gte00c, gte01c, gte02c, gte03c, gte04f, gte05f, gte06f, gte08c, gte09i, compid, userid, ddate, ttime) select gte00c, gte01c, gte02c, gte03c, gte04f, gte05f, gte06f, gte08c, gte09i, @compid, @userid, @curdate, @time from gtc04 with (nolock) where gte00c = @compid and gte01c = @cardid insert gtc08_log(gtj00c, gtj01c, gtj02c, gtj03c, gtj04f, gtj05f, gtj06f, gtj07c, gtj09i, compid, userid, ddate, ttime) select gtj00c, gtj01c, gtj02c, gtj03c, gtj04f, gtj05f, gtj06f, gtj07c, gtj09i, @compid, @userid, @curdate, @time from gtc08_log with (nolock) where gtj00c = @compid and gtj01c = @cardid end GO /****** Object: StoredProcedure [dbo].[upg_dispatch_data_member_enter_parameter_single] Script Date: 06/12/2017 06:20:01 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_dispatch_data_member_enter_parameter_single] ( @compid varchar(10) ) as begin create table #tbl_control ( compid varchar(10) not null, -- 儿子公司的代码 lvl int null, primary key(compid) ) insert #tbl_control(compid, lvl) select bran_id, control_lvl from b_perf a, basic_data_control b where parent_id = @compid and a.bran_id = b.compid and b.datatype = 'member_enter_parameter' and b.control_lvl = 1 -- 创建循环变量 declare @HasRecord int set @HasRecord = 1 declare @currentCompId varchar(10) declare @lvl int while (@HasRecord = 1) begin if exists (select 1 from #tbl_control) begin set @HasRecord = 1 select top 1 @currentCompId = compid, @lvl = lvl from #tbl_control if @lvl = 1 -- 严格按总部的 begin -- 先删除自己的,把总部分发下来 -- 强制模式都是先删除原来的 exec upg_m_dispatch_member_enter_parameter @compid, @currentCompId, 1 insert #tbl_control(compid, lvl) select bran_id, control_lvl from b_perf a, basic_data_control b where parent_id = @currentCompId and a.bran_id = b.compid and b.datatype = 'member_enter_parameter' and b.control_lvl = 1 end else if @lvl = 2 begin exec upg_m_dispatch_member_enter_parameter @compid, @currentCompId, 0 insert #tbl_control(compid, lvl) select bran_id, case when (control_lvl <= 2) then 2 else control_lvl end from b_perf a, basic_data_control b where parent_id = @currentCompId and a.bran_id = b.compid and b.datatype = 'member_enter_parameter' and b.control_lvl = 1 end -- 清除已处理的门店 delete #tbl_control where compid = @currentCompId end else begin set @HasRecord = 0 end end drop table #tbl_control end GO /****** Object: StoredProcedure [dbo].[upg_dispatch_data_member_enter_parameter_mandatory] Script Date: 06/12/2017 06:20:00 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_dispatch_data_member_enter_parameter_mandatory] ( @compid varchar(10) ) as begin create table #tbl_control ( compid varchar(10) not null, -- 儿子公司的代码 primary key(compid) ) -- 创建循环变量 declare @HasRecord int set @HasRecord = 1 declare @currentCompId varchar(10) while (@HasRecord = 1) begin if exists (select 1 from #tbl_control) begin set @HasRecord = 1 select top 1 @currentCompId = compid from #tbl_control exec upg_m_dispatch_member_enter_parameter @compid, @currentCompId, 1 -- 清除已处理的门店 delete #tbl_control where compid = @currentCompId end else begin set @HasRecord = 0 end end drop table #tbl_control end GO /****** Object: StoredProcedure [dbo].[upg_m_dispatch_hem01] Script Date: 06/12/2017 06:20:21 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_m_dispatch_hem01] ( @fromcompid varchar(10), -- 从哪一个公司发布 @tocompid varchar(10), -- 目的公司 @mandatory varchar(2) -- 是否强制统一 ) as begin if @fromcompid = @tocompid return if @mandatory = '1' begin delete precept where pre00c = @tocompid insert precept(pre00c, pre01c, pre02c, pre03i, pre04i, pre05i, pre06i, pre07f, pre08f, pre09i, pre10f, pre11f, pre12i, pre13i, pre14i, pre15d, pre16c, pre17c, pre18c, pre19c, pre20c, pre21c, pre22i ) select @tocompid, pre01c, pre02c, pre03i, pre04i, pre05i, pre06i, pre07f, pre08f, pre09i, pre10f, pre11f, pre12i, pre13i, pre14i, pre15d, pre16c, pre17c, pre18c, pre19c, pre20c, pre21c, pre22i from precept where pre00c = @fromcompid -- hem01 delete hem01 where hea00c = @tocompid insert hem01(hea00c, hea01c, hea02i, hea03f, hea04i, hea05f, hea06i, hea07f, hea08i, hea09f, hea10i, hea11f, hea12f, hea13f, hea14i, hea15f, hea16i, hea17f, hea18i, hea19i, hea20i, hea21f, hea22i, hea23f, hea24f, hea25i, hea26f, hea27i, hea28f, hea29i, hea30i, hea31i, hea32i, hea33c, hea34c, hea35i,hea36f,hea37i,hea38f,hea39i,hea40f,hea41i,hea42f,hea43i,hea44f, hea45i,hea46f,hea47i,hea48f,hea49i ) select @tocompid, hea01c, hea02i, hea03f, hea04i, hea05f, hea06i, hea07f, hea08i, hea09f, hea10i, hea11f, hea12f, hea13f, hea14i, hea15f, hea16i, hea17f, hea18i, hea19i, hea20i, hea21f, hea22i, hea23f, hea24f, hea25i, hea26f, hea27i, hea28f, hea29i, hea30i, hea31i, hea32i, hea33c, isnull(hea34c, '*'),hea35i,hea36f,hea37i,hea38f,hea39i,hea40f,hea41i,hea42f,hea43i,hea44f, hea45i,hea46f,hea47i,hea48f,hea49i from hem01 where hea00c = @fromcompid -- productprecept delete productprecept where pro00c = @tocompid insert productprecept(pro00c, pro01c, pro02c, pro03i, pro04f, pro05i, pro06f, pro07f, pro08c, pro09c, pro10c, pro11c, pro12c) select @tocompid, pro01c, pro02c, pro03i, pro04f, pro05i, pro06f, pro07f, pro08c, pro09c, isnull(pro10c, '*'), pro11c, pro12c from productprecept where pro00c = @fromcompid -- cardprecept delete cardprecept where car00c = @tocompid insert cardprecept(car00c, car01c, car02c, car03i, car04f, car05i, car06f, car07i, car08f, car09i, car10f, car11f, car12f, car13f, car14f, car15f, car16c, car17c, car18c, car19c, car20c ) select @tocompid, car01c, car02c, car03i, car04f, car05i, car06f, car07i, car08f, car09i, car10f, car11f, car12f, car13f, car14f, car15f, car16c, car17c, car18c, car19c, car20c from cardprecept where car00c = @fromcompid -- aloneproductprecept delete aloneproductprecept where gap00c = @tocompid insert aloneproductprecept(gap00c, gap01c, gap02c, gap03i, gap04f, gap05c, gap06c, gap07c, gap08c, gap09c) select @tocompid, gap01c, gap02c, gap03i, gap04f, gap05c, gap06c, gap07c, gap08c, gap09c from aloneproductprecept where gap00c = @fromcompid -- alonecardprecept delete alonecardprecept where gac00c = @tocompid insert alonecardprecept(gac00c, gac01c, gac02c, gac03i, gac04f, gac05i, gac06f, gac07i, gac08f, gac09i, gac10f, gac11f, gac12f, gac13f, gac14f, gac15f, gac16c, gac17c, gac18c, gac19c, gac20c ) select @tocompid, gac01c, gac02c, gac03i, gac04f, gac05i, gac06f, gac07i, gac08f, gac09i, gac10f, gac11f, gac12f, gac13f, gac14f, gac15f, gac16c, gac17c, gac18c, gac19c, gac20c from alonecardprecept where gac00c = @fromcompid -- hem03 delete hem03 where hec00c = @tocompid insert hem03(hec00c, hec01c, hec02f, hec03f, hec04f, hec05i, hec06f, hec07c, hec08i, hec09f, hec10c ) select @tocompid, hec01c, hec02f, hec03f, hec04f, hec05i, hec06f, hec07c, hec08i, hec09f, hec10c from hem03 where hec00c = @fromcompid -- hem04 delete hem04 where hed00c = @tocompid insert hem04(hed00c, hed01c, hed02f, hed03f, hed04f, hed05i, hed06f, hed07c, hed08c ) select @tocompid, hed01c, hed02f, hed03f, hed04f, hed05i, hed06f, hed07c, hed08c from hem04 where hed00c = @fromcompid -- hem05 delete hem05 where hee00c = @tocompid insert hem05(hee00c, hee01c, hee02f, hee03f, hee04f, hee05i, hee06f, hee07c, hee08c ) select @tocompid, hee01c, hee02f, hee03f, hee04f, hee05i, hee06f, hee07c, hee08c from hem05 where hee00c = @fromcompid -- hem06 delete hem06 where hef00c = @tocompid insert hem06(hef00c, hef01c, hef02f, hef03f, hef04f, hef05i, hef06f, hef08c ) select @tocompid, hef01c, hef02f, hef03f, hef04f, hef05i, hef06f, hef08c from hem06 where hef00c = @fromcompid -- ham14 delete ham14 where han00c = @tocompid insert ham14(han00c, han01c, han02f, han03f, han04f, han05i, han06f, han07c ) select @tocompid, han01c, han02f, han03f, han04f, han05i, han06f, han07c from ham14 where han00c = @fromcompid -- ham16 delete ham16 where haq00c = @tocompid insert ham16(haq00c, haq01c, haq02f, haq03f, haq04f, haq05i, haq06f, haq08c ) select @tocompid, haq01c, haq02f, haq03f, haq04f, haq05i, haq06f, haq08c from ham16 where haq00c = @fromcompid -- ham15 delete ham15 where hap00c = @tocompid insert ham15(hap00c, hap01c, hap02c, hap03i, hap04f, hap05i, hap06f, hap07i, hap08f, hap09i, hap10f, hap11i, hap12f, hap13i, hap14f, hap15i, hap16f, hap17i, hap18f, hap19i, hap20f, hap21i, hap22f, hap23i, hap24f, hap25i, hap26f, hap37i, hap38f, hap39i, hap40f, hap41i, hap42f, hap43i, hap44f, hap45i, hap46f, hap47i, hap48f, hap49i, hap50f, hap51i, hap52f,hap53c) select @tocompid, hap01c, hap02c, hap03i, hap04f, hap05i, hap06f, hap07i, hap08f, hap09i, hap10f, hap11i, hap12f, hap13i, hap14f, hap15i, hap16f, hap17i, hap18f, hap19i, hap20f, hap21i, hap22f, hap23i, hap24f, hap25i, hap26f, hap37i, hap38f, hap39i, hap40f, hap41i, hap42f, hap43i, hap44f, hap45i, hap46f, hap47i, hap48f, hap49i, hap50f, hap51i, hap52f,hap53c from ham15 where hap00c = @fromcompid return end create table #precept_key ( pre01c varchar(20) ) insert #precept_key(pre01c) select pre01c from precept where pre00c = @fromcompid -- 创建循环变量 declare @HasRecord int set @HasRecord = 1 declare @current_pre01c varchar(20) while (@HasRecord = 1) begin if exists (select 1 from #precept_key) begin set @HasRecord = 1 select top 1 @current_pre01c = pre01c from #precept_key if exists (select 1 from precept where pre00c = @tocompid and pre01c = @current_pre01c) begin exec upg_m_dispatch_hem01_single_optional @fromcompid, @tocompid, @current_pre01c end else begin exec upg_m_dispatch_hem01_single_mandatory @fromcompid, @tocompid, @current_pre01c end -- 清除已处理的门店 delete #precept_key where pre01c = @current_pre01c end else begin set @HasRecord = 0 end end drop table #precept_key end GO /****** Object: StoredProcedure [dbo].[upg_check_notify_everyday] Script Date: 06/12/2017 06:19:41 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_check_notify_everyday]( @compid varchar(20), @ddate varchar(20), --阳历 @ttime varchar(20)) as begin exec upg_check_notify_everyday_ex @compid, @ddate, '', @ttime end GO /****** Object: StoredProcedure [dbo].[upg_purse_deduct_ex] Script Date: 06/12/2017 06:20:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_purse_deduct_ex] ( @compid varchar(10), -- 公司编号 @cardno varchar(20), -- 会员卡号 @date varchar(10), -- 异动日期 @accttype int, -- 账户类型(1.电子钱包 2.储值账户) @consume float, -- 支付金额 @changway int, -- 异动方向(0- 充值, 1-取款, 2-消费, 3-转入,4-转出, 5-欠款 6-欠款返回) @billtype varchar(5), -- 单据类别 @billno varchar(20), -- 单据号 @billmemo varchar(160), -- 备注 @confirmflag int -- 是否复核的标记(1.复核 0.取消复核) ) as--WUXM, 2004/12/26 begin --根据卡号获取归属公司: declare @ownercompid nvarchar(10) --卡的归属公司 select @ownercompid = min(gca13d) from gcm01 where gca01c = @cardno and gca00c = gca13d declare @gcm04_flowno float declare @lastremain float declare @newdate varchar(8) if @accttype = 3 begin declare @start_points varchar(20) select @start_points = gsa02c from gsm01 where gsa00c = @compid and gsa01c = 'SP110' if isnull(@start_points, '')<>'1' begin return end end select @accttype = isnull(@accttype, 2) if @accttype = 999 return --取得新的有效日期 if @changway = 0 and @billtype = 'gz' and @confirmflag = 1 begin select @newdate = gcl21d from gcm10 with (nolock) where gcl00c = @compid and gcl01c = @billno end --取得下一个流水号 --如果在账户余额表中不存在,则增加此纪录 if not exists(select 1 from gcm03 with (nolock) where gcc00c = @ownercompid and gcc01c = @cardno and gcc03i = @accttype) begin declare @flowno2 int declare @safeamt float select @flowno2 = isnull(max(gcc02f), 0) + 1 from gcm03 with (nolock) where gcc00c = @ownercompid and gcc01c = @cardno select @flowno2 = isnull(@flowno2, 1) if @accttype = 2 begin select @safeamt = isnull(b.gak43f, 0) from gcm01 a, gam10 b where a.gca00c = b.gak00c and a.gca02c = b.gak01c and a.gca00c = @ownercompid and a.gca01c = @cardno end else begin select @safeamt = 0 end insert gcm03(gcc00c, gcc01c, gcc02f, gcc03i, gcc06f, gcc07f, gcc08c, gcc09d) values(@ownercompid, @cardno, @flowno2, @accttype, 0, @safeamt, @date, @newdate) end if @confirmflag = 1 --复核 begin begin transaction tr_003 -- 取款等 if @changway = 1 or @changway = 2 or @changway = 4 or @changway = 5 begin update gcm03 set gcc06f = isnull(gcc06f, 0) - @consume where gcc00c = @ownercompid and gcc01c = @cardno and gcc03i = @accttype --写入欠款金额 Added by mzh 2006/02/22 if (@changway = 5) begin update gcm03 set gcc10f = isnull(gcc10f, 0) + @consume where gcc00c = @ownercompid and gcc01c = @cardno and gcc03i = @accttype end select @lastremain = isnull(gcc06f, 0) + @consume from gcm03 with (nolock) where gcc00c = @ownercompid and gcc01c = @cardno and gcc03i = @accttype end else begin if isnull(@newdate, '') = '' begin update gcm03 set gcc06f = isnull(gcc06f, 0) + @consume, gcc08c = @date where gcc00c = @ownercompid and gcc01c = @cardno and gcc03i = @accttype end else begin update gcm03 set gcc06f = isnull(gcc06f, 0) + @consume, gcc08c = @date, gcc09d = @newdate where gcc00c = @ownercompid and gcc01c = @cardno and gcc03i = @accttype end --还款,抵消欠款金额 added by mzh 2006/02/22 if (@changway = 6) begin update gcm03 set gcc10f = isnull(gcc10f, 0) - @consume where gcc00c = @ownercompid and gcc01c = @cardno and gcc03i = @accttype end select @lastremain = isnull(gcc06f, 0) - @consume from gcm03 with (nolock) where gcc00c = @ownercompid and gcc01c = @cardno and gcc03i = @accttype end --取得操作日期与时间 declare @curdate varchar(20), @curtime varchar(20) exec upg_getcurrdate @curdate output, @curtime output --似乎没有必要将为零的数据写到gcm04中 if @consume <> 0 begin declare @sp0403f float declare @isinputout_raw_bill int select @sp0403f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP040' if @sp0403f = 2 or @sp0403f = 3 begin select @isinputout_raw_bill = 0 end else begin select @isinputout_raw_bill = 1 end exec upg_get_gcm04_flowno @cardno, @accttype, @gcm04_flowno output insert gcm04(gcd00c, gcd01c, gcd02i, gcd04f, gcd05i, gcd06f, gcd07c, gcd08c, gcd09d, gcd10f, gcd11c, gcd12i, gcd13t, gcd14c) values(@compid, @cardno, @accttype, @gcm04_flowno, @changway, @consume, @billtype, @billno, @date, @lastremain, @curdate, @isinputout_raw_bill, @curtime, @billmemo) end commit transaction tr_003 end else --取消复核 begin if @changway = 1 or @changway = 2 or @changway = 4 or @changway = 5 begin update gcm03 set gcc06f = isnull(gcc06f, 0) + @consume where gcc00c = @ownercompid and gcc01c = @cardno and gcc03i = @accttype --欠款金额 added by mzh 2006/02/22 if (@changway = 5) begin update gcm03 set gcc10f = isnull(gcc10f, 0) - @consume where gcc00c = @ownercompid and gcc01c = @cardno and gcc03i = @accttype end end else begin update gcm03 set gcc06f = isnull(gcc06f, 0)-@consume where gcc00c = @ownercompid and gcc01c = @cardno and gcc03i = @accttype if (@changway = 6) begin update gcm03 set gcc10f = isnull(gcc10f, 0) + @consume where gcc00c = @ownercompid and gcc01c = @cardno and gcc03i = @accttype end end delete from gcm04 where gcd00c = @compid and gcd01c = @cardno and gcd02i = @accttype and gcd05i = @changway and gcd07c = @billtype and gcd08c = @billno end end GO /****** Object: StoredProcedure [dbo].[upg_compute_cost_perf_produce_ceshi] Script Date: 06/12/2017 06:19:44 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_compute_cost_perf_produce_ceshi] ( @compid varchar(10), -- 公司别 @billid varchar(20), -- 收银单号 @cardtype varchar(20), -- 会员卡类别, 消费时候的类别 @cardno varchar(20), -- 会员卡号 @ddate varchar(8), -- 账单日期 @cash_good_rate float, @card_kind int, @old_good_ex_rate_points float, @perf_rate_good_gam10 float, @pos_reduce_rate float, @good varchar(20), -- 产品代码 @standardprice float, -- 标准价 @price float, -- 实际价 @amt float, -- 项目总价 @quan float, -- 数量 @ratio_1 float, -- 第一销售分享比率 @ratio_2 float, -- 第二销售分享比率 @ratio_3 float, -- 第三销售分享比率 @paymode varchar(20), -- 支付方式, 但按单据支付的时候, 没有此值 @staff_1 varchar(20), @staff_2 varchar(20), @staff_3 varchar(20), @fa_type1 nvarchar(20), @fa_type2 nvarchar(20), @fa_type3 nvarchar(20), @vmode1 int, @vsetvalue1 float, @vmode2 int, @vsetvalue2 float, @vmode3 int, @vsetvalue3 float, @cost float, -- 最后计算出来的成 @in_cost float, @in_cost_unit float, @p_mode int, @p_setvalue float, @input_good_perf float, @ggc02f float --产品序号 ) as begin -- declare @good varchar(20) -- 产品代码 -- declare @standardprice float -- 标准价 -- declare @price float -- 实际价 -- declare @amt float -- 项目总价 -- declare @quan float -- 数量 -- declare @ratio_1 float -- 第一销售分享比率 -- declare @ratio_2 float -- 第二销售分享比率 -- declare @ratio_3 float -- 第三销售分享比率 -- declare @paymode varchar(20) -- 支付方式, 但按单据支付的时候, 没有此值 -- declare @staff_1 varchar(20) -- 第一销售 -- declare @staff_2 varchar(20) -- 第二销售 -- declare @staff_3 varchar(20) -- 第三销售 --declare @cost float -- 最后计算出来的成本 -- declare @in_cost float -- declare @in_cost_unit float -- declare @p_mode int -- declare @p_setvalue float declare @p_points float -- 积分 -- declare @vmode int -- declare @vsetvalue float declare @ex_good_rate float -- 根据卡的类别取得产品实际营业额的折算比率 declare @ex_good_rate_pref float -- 根据卡的类别取得产品实际业绩的折算比率 declare @ex_good_rate_points float -- 根据卡的类别取得积分的折算比率 --declare @old_good_ex_rate_points float -- 物品积分折算比率 declare @p_ex_rate float -- 根据支付方式的不同取得实际营业额的折算比率 declare @p_ex_rate_points float -- 根据支付方式的不同取得积分的折算比率 declare @saved_perf_rate_good_gam10 float -- 业绩折算比率,产品 --declare @perf_rate_good_gam10 float -- 业绩折算比率,产品 -- declare @card_kind int -- 主要为了识别是否为是否为资格卡 -- select @card_kind = gak03i, -- @old_good_ex_rate_points = isnull(gak34f, 1) -- from gam10 a -- where a.gak00c = @compid and a.gak01c = @cardtype -- 散客,只要在会员卡类别设定中有Z的类别,实际营业额的折算比率就按Z的会员卡类别设定中 -- 不管支付方式为多少 declare @is_cash int -- 标志是否设置了Z的类别 declare @cash_good_rate_points float -- declare @input_good_perf float -- select @input_good_perf = gsa03f -- from gsm01 with(nolock) -- where gsa00c = @compid and gsa01c = 'SP135' -- 散客 if substring(@cardno, 1, 2) = '散客' begin -- 散客,只要在会员卡类别设定中有Z的类别,实际营业额的折算比率就按Z的会员卡类别设定中 -- 不管支付方式为多少 if exists(select 1 from gam10 where gak00c = @compid and gak01c = 'Z') begin select @is_cash = 1 -- select @cash_good_rate_points = isnull(gak34f, 1), -- @perf_rate_good_gam10 = isnull(gak46f, 1) -- from gam10 -- where gak00c = @compid and gak01c = 'Z' set @saved_perf_rate_good_gam10 = @perf_rate_good_gam10 end else begin select @is_cash = 0 end end -- declare @pos_reduce_rate float -- 银行卡支付的时候需要调整的折算比率 -- -- select @pos_reduce_rate = gca25f -- from gcm01 with (nolock) -- where gca00c = gca13d and gca01c = @cardno select @pos_reduce_rate = isnull(@pos_reduce_rate, 0) declare @old_good_ex_rate float -- declare @cash_good_rate float -- -- 折算后营业额(也称谓实际营业额), 实际营业额 = 营业额 *折算率 -- select @old_good_ex_rate = isnull(gak24f, 1) - @pos_reduce_rate, -- @cash_good_rate = isnull(gak24f, 1), -- @perf_rate_good_gam10 = isnull(gak46f, 1) -- from gam10 -- where gak00c = @compid and gak01c = @cardtype select @old_good_ex_rate = @cash_good_rate - @pos_reduce_rate set @saved_perf_rate_good_gam10 = @perf_rate_good_gam10 -- 允许折算率为0存在 -- 虽然ggm01中有卡存在但不一定用卡来支付 -- 所以实际的折算率还应该考虑按支付方式的折扣率 if isnull(@old_good_ex_rate, -1) = -1 select @old_good_ex_rate = 1 declare @vperf_1 float -- 第一销售的虚业绩 declare @vperf_2 float -- 第二销售的虚业绩 declare @vperf_3 float -- 第三销售的虚业绩 --declare @fa_type nvarchar(20) -- 提成方案类型 -- declare cur_each_goods_item cursor for -- select ggc03c, ggc08f, ggc10f, ggc11f, ggc05f, ggc14f, ggc16f, ggc18f, ggc27c, ggc12c, ggc15c, ggc17c -- from ggm03 -- where ggc00c = @compid and ggc01c = @billid -- for update of ggc13f, ggc21f, ggc23f, ggc25f, ggc28f, ggc29f, ggc30f, ggc07f -- -- open cur_each_goods_item -- fetch next -- from cur_each_goods_item into @good, @standardprice, @price, @amt, @quan, @ratio_1, @ratio_2, @ratio_3, @paymode, @staff_1, @staff_2, @staff_3 -- -- while @@fetch_status = 0 -- begin ------------计算积分折算比率(start)---------------- if isnull(@old_good_ex_rate_points, -1) = -1 select @old_good_ex_rate_points = 1 -- 还需要考虑支付方式的不同 select @p_ex_rate = gso06f, @p_ex_rate_points = gso04f from gsm15 where gso00c = @compid and gso02c = @paymode if @card_kind = 2 -- 资格卡 begin select @ex_good_rate_points = @old_good_ex_rate_points if isnull(@p_ex_rate_points, -1) = -1 select @p_ex_rate_points = 1 select @ex_good_rate_points = @ex_good_rate_points * @p_ex_rate_points select @ex_good_rate = @old_good_ex_rate select @ex_good_rate = @ex_good_rate * @p_ex_rate end else begin -- 允许折算率为0存在 if isnull(@p_ex_rate, -1) = -1 select @p_ex_rate = 1 if isnull(@p_ex_rate_points, -1) = -1 select @p_ex_rate_points = 1 if @paymode = '4' begin -- 按储值账户支付, 按卡类别的折算 * 支付方式的折算率 select @ex_good_rate_points = @old_good_ex_rate_points * @p_ex_rate_points -- 按储值账户支付, 按卡类别的折算 * 支付方式的折算率 select @ex_good_rate = @old_good_ex_rate * @p_ex_rate set @perf_rate_good_gam10 = @saved_perf_rate_good_gam10 end else begin -- 按其他账户支付或其他支付方式 select @ex_good_rate_points = @p_ex_rate_points --按其他账户支付或其他支付方式 select @ex_good_rate = @p_ex_rate set @perf_rate_good_gam10 = 1 end -- 如果设定了Z(现金的实际营业额比率) if @is_cash = 1 begin -- Z的折扣率 * 支付方式设置的折扣率 select @ex_good_rate_points = @cash_good_rate_points * @p_ex_rate_points -- Z的折扣率 * 支付方式设置的折扣率 if isnull(@cash_good_rate, -1) = -1 select @cash_good_rate = 1 select @ex_good_rate = @cash_good_rate * @p_ex_rate end end -- 计算成本 select @cost = gfa19f * @quan, @in_cost = gfa22f * @quan, @in_cost_unit = gfa22f, @p_mode = gfa25i, @p_setvalue = gfa26f from gfm01 where gfa00c = @compid and gfa01c = @good -- 取得不同会员卡类别不同项目类别设置的实际营业额折扣率 declare @cardtype_itemtype_rate float exec upg_get_cardtype_itemtype_rate @compid, @cardtype, @good, @paymode, '2', @cardtype_itemtype_rate output set @ex_good_rate = isnull(@ex_good_rate, 1) * isnull(@cardtype_itemtype_rate, 1) set @ex_good_rate_pref = @ex_good_rate * isnull(@perf_rate_good_gam10, 1) --计算积分 exec upg_cal_all_employees_produce_point @quan, @standardprice, @amt, @ex_good_rate_pref, @ex_good_rate_points, @p_mode, @p_setvalue, @p_points output if (@standardprice = 0) set @standardprice = 1 update ggm03 set ggc13f = @cost, ggc28f = @p_points, ggc29f = @price / @standardprice, ggc30f = @ex_good_rate, ggc07f = @in_cost_unit where ggc00c = @compid and ggc01c = @billid and ggc02f = @ggc02f if (@input_good_perf <> 1 and isnull(@staff_1, '') <> '') begin -- 得到提成方案 -- select top 1 @fa_type = ppt02c -- from personprecept -- where ppt00c = @compid and ppt03c = @staff_1 and ppt04i = 1 -- and ppt02c in (select pre01c from precept where pre00c = @compid and isnull(pre22i, 0) = 0) -- order by ppt05d desc -- 计算业绩 -- select @vmode = pro03i, -- @vsetvalue = pro04f -- from (select pro03i, pro04f -- from productprecept with (nolock) -- where pro00c = @compid and pro01c = @good and pro02c = @fa_type and isnull(pro10c, '*') = @paymode -- union all -- select pro03i, pro04f -- from productprecept with (nolock) -- where pro00c = @compid and pro01c = @good and pro02c = @fa_type and isnull(pro10c, '*') = '*' -- ) a exec upg_cal_all_employees_produce_vperf @quan, @standardprice, @in_cost, @cost, @price, @amt, @ex_good_rate_pref, @vmode1, @vsetvalue1, @ratio_1, @vperf_1 output update ggm03 set ggc21f = @vperf_1 where ggc00c = @compid and ggc01c = @billid and ggc02f = @ggc02f end if (@input_good_perf <> 1 and isnull(@staff_2, '') <> '') begin -- 得到提成方案 -- 获取在账单有效日期内最新一条员工提成方案 -- select top 1 @fa_type = ppt02c -- from personprecept -- where ppt00c = @compid and ppt03c = @staff_2 and ppt04i = 1 -- and ppt02c in (select pre01c from precept where pre00c = @compid and isnull(pre22i, 0) = 0) -- order by ppt05d desc -- -- -- 计算业绩 -- select @vmode = pro03i, -- @vsetvalue = pro04f -- from (select pro03i, pro04f -- from productprecept with (nolock) -- where pro00c = @compid and pro01c = @good and pro02c = @fa_type and isnull(pro10c, '*') = @paymode -- union all -- select pro03i, pro04f -- from productprecept with (nolock) -- where pro00c = @compid and pro01c = @good and pro02c = @fa_type and isnull(pro10c, '*') = '*' -- ) a -- exec upg_cal_all_employees_produce_vperf @quan, @standardprice, @in_cost, @cost, @price, @amt, @ex_good_rate_pref, @vmode2, @vsetvalue2, @ratio_2, @vperf_2 output update ggm03 set ggc23f = @vperf_2 where ggc00c = @compid and ggc01c = @billid and ggc02f = @ggc02f end if (@input_good_perf <> 1 and isnull(@staff_3, '') <> '') begin -- 得到提成方案 -- 获取在账单有效日期内最新一条员工提成方案 -- select top 1 @fa_type = ppt02c -- from personprecept -- where ppt00c = @compid and ppt03c = @staff_3 and ppt04i = 1 -- and ppt02c in (select pre01c from precept where pre00c = @compid and isnull(pre22i, 0) = 0) -- order by ppt05d desc -- -- -- 计算业绩 -- select @vmode = pro03i, -- @vsetvalue = pro04f -- from (select pro03i, pro04f -- from productprecept with (nolock) -- where pro00c = @compid and pro01c = @good and pro02c = @fa_type and isnull(pro10c, '*') = @paymode -- union all -- select pro03i, pro04f -- from productprecept with (nolock) -- where pro00c = @compid and pro01c = @good and pro02c = @fa_type and isnull(pro10c, '*') = '*' -- ) a exec upg_cal_all_employees_produce_vperf @quan, @standardprice, @in_cost, @cost, @price, @amt, @ex_good_rate_pref, @vmode3, @vsetvalue3, @ratio_3, @vperf_3 output update ggm03 set ggc25f = @vperf_3 where ggc00c = @compid and ggc01c = @billid and ggc02f = @ggc02f end -- added by wuxm, 20060619, 增加产品消费提醒的功能 exec upg_insert_notify_content @compid, @billid, @cardno, @staff_1, 2, @good, @ddate -- -- fetch next from cur_each_goods_item into @good, @standardprice, @price, @amt, @quan, @ratio_1, @ratio_2, @ratio_3, @paymode, @staff_1, @staff_2, @staff_3 -- end -- -- close cur_each_goods_item -- deallocate cur_each_goods_item -- -- 把产品的进货价格写到消耗的表中 update a set a.gge07f = b.gfa22f from ggm05 a, gfm01 b where a.gge00c = b.gfa00c and a.gge01c = @billid and a.gge00c = @compid and a.gge03c = b.gfa01c end GO /****** Object: StoredProcedure [dbo].[upg_compute_cost_perf_produce] Script Date: 06/12/2017 06:19:43 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_compute_cost_perf_produce] ( @compid varchar(10), -- 公司别 @billid varchar(20), -- 收银单号 @cardtype varchar(20), -- 会员卡类别, 消费时候的类别 @cardno varchar(20), -- 会员卡号 @ddate varchar(8) -- 账单日期 ) as begin declare @good varchar(20) -- 产品代码 declare @standardprice float -- 标准价 declare @price float -- 实际价 declare @amt float -- 项目总价 declare @quan float -- 数量 declare @ratio_1 float -- 第一销售分享比率 declare @ratio_2 float -- 第二销售分享比率 declare @ratio_3 float -- 第三销售分享比率 declare @paymode varchar(20) -- 支付方式, 但按单据支付的时候, 没有此值 declare @staff_1 varchar(20) -- 第一销售 declare @staff_2 varchar(20) -- 第二销售 declare @staff_3 varchar(20) -- 第三销售 declare @cost float -- 最后计算出来的成本 declare @in_cost float declare @in_cost_unit float declare @p_mode int declare @p_setvalue float declare @p_points float -- 积分 declare @vmode int declare @vsetvalue float declare @ex_good_rate float -- 根据卡的类别取得产品实际营业额的折算比率 declare @ex_good_rate_pref float -- 根据卡的类别取得产品实际业绩的折算比率 declare @ex_good_rate_points float -- 根据卡的类别取得积分的折算比率 declare @old_good_ex_rate_points float -- 物品积分折算比率 declare @p_ex_rate float -- 根据支付方式的不同取得实际营业额的折算比率 declare @p_ex_rate_points float -- 根据支付方式的不同取得积分的折算比率 declare @saved_perf_rate_good_gam10 float -- 业绩折算比率,产品 declare @perf_rate_good_gam10 float -- 业绩折算比率,产品 declare @card_kind int -- 主要为了识别是否为是否为资格卡 select @card_kind = gak03i, @old_good_ex_rate_points = isnull(gak34f, 1) from gam10 a where a.gak00c = @compid and a.gak01c = @cardtype -- 散客,只要在会员卡类别设定中有Z的类别,实际营业额的折算比率就按Z的会员卡类别设定中 -- 不管支付方式为多少 declare @is_cash int -- 标志是否设置了Z的类别 declare @cash_good_rate_points float declare @input_good_perf float select @input_good_perf = gsa03f from gsm01 with(nolock) where gsa00c = @compid and gsa01c = 'SP135' -- 散客 if substring(@cardno, 1, 2) = '散客' begin -- 散客,只要在会员卡类别设定中有Z的类别,实际营业额的折算比率就按Z的会员卡类别设定中 -- 不管支付方式为多少 if exists(select 1 from gam10 where gak00c = @compid and gak01c = 'Z') begin select @is_cash = 1 select @cash_good_rate_points = isnull(gak34f, 1), @perf_rate_good_gam10 = isnull(gak46f, 1) from gam10 where gak00c = @compid and gak01c = 'Z' set @saved_perf_rate_good_gam10 = @perf_rate_good_gam10 end else begin select @is_cash = 0 end end declare @pos_reduce_rate float -- 银行卡支付的时候需要调整的折算比率 select @pos_reduce_rate = gca25f from gcm01 with (nolock) where gca00c = gca13d and gca01c = @cardno select @pos_reduce_rate = isnull(@pos_reduce_rate, 0) declare @old_good_ex_rate float declare @cash_good_rate float -- 折算后营业额(也称谓实际营业额), 实际营业额 = 营业额 *折算率 select @old_good_ex_rate = isnull(gak24f, 1) - @pos_reduce_rate, @cash_good_rate = isnull(gak24f, 1), @perf_rate_good_gam10 = isnull(gak46f, 1) from gam10 where gak00c = @compid and gak01c = @cardtype set @saved_perf_rate_good_gam10 = @perf_rate_good_gam10 -- 允许折算率为0存在 -- 虽然ggm01中有卡存在但不一定用卡来支付 -- 所以实际的折算率还应该考虑按支付方式的折扣率 if isnull(@old_good_ex_rate, -1) = -1 select @old_good_ex_rate = 1 declare @vperf_1 float -- 第一销售的虚业绩 declare @vperf_2 float -- 第二销售的虚业绩 declare @vperf_3 float -- 第三销售的虚业绩 declare @fa_type nvarchar(20) -- 提成方案类型 declare cur_each_goods_item cursor for select ggc03c, ggc08f, ggc10f, ggc11f, ggc05f, ggc14f, ggc16f, ggc18f, ggc27c, ggc12c, ggc15c, ggc17c from ggm03 where ggc00c = @compid and ggc01c = @billid for update of ggc13f, ggc21f, ggc23f, ggc25f, ggc28f, ggc29f, ggc30f, ggc07f open cur_each_goods_item fetch next from cur_each_goods_item into @good, @standardprice, @price, @amt, @quan, @ratio_1, @ratio_2, @ratio_3, @paymode, @staff_1, @staff_2, @staff_3 while @@fetch_status = 0 begin ------------计算积分折算比率(start)---------------- if isnull(@old_good_ex_rate_points, -1) = -1 select @old_good_ex_rate_points = 1 -- 还需要考虑支付方式的不同 select @p_ex_rate = gso06f, @p_ex_rate_points = gso04f from gsm15 where gso00c = @compid and gso02c = @paymode if @card_kind = 2 -- 资格卡 begin select @ex_good_rate_points = @old_good_ex_rate_points if isnull(@p_ex_rate_points, -1) = -1 select @p_ex_rate_points = 1 select @ex_good_rate_points = @ex_good_rate_points * @p_ex_rate_points select @ex_good_rate = @old_good_ex_rate select @ex_good_rate = @ex_good_rate * @p_ex_rate end else begin -- 允许折算率为0存在 if isnull(@p_ex_rate, -1) = -1 select @p_ex_rate = 1 if isnull(@p_ex_rate_points, -1) = -1 select @p_ex_rate_points = 1 if @paymode = '4' begin -- 按储值账户支付, 按卡类别的折算 * 支付方式的折算率 select @ex_good_rate_points = @old_good_ex_rate_points * @p_ex_rate_points -- 按储值账户支付, 按卡类别的折算 * 支付方式的折算率 select @ex_good_rate = @old_good_ex_rate * @p_ex_rate set @perf_rate_good_gam10 = @saved_perf_rate_good_gam10 end else begin -- 按其他账户支付或其他支付方式 select @ex_good_rate_points = @p_ex_rate_points --按其他账户支付或其他支付方式 select @ex_good_rate = @p_ex_rate set @perf_rate_good_gam10 = 1 end -- 如果设定了Z(现金的实际营业额比率) if @is_cash = 1 begin -- Z的折扣率 * 支付方式设置的折扣率 select @ex_good_rate_points = @cash_good_rate_points * @p_ex_rate_points -- Z的折扣率 * 支付方式设置的折扣率 if isnull(@cash_good_rate, -1) = -1 select @cash_good_rate = 1 select @ex_good_rate = @cash_good_rate * @p_ex_rate end end -- 计算成本 select @cost = gfa19f * @quan, @in_cost = gfa22f * @quan, @in_cost_unit = gfa22f, @p_mode = gfa25i, @p_setvalue = gfa26f from gfm01 where gfa00c = @compid and gfa01c = @good -- 取得不同会员卡类别不同项目类别设置的实际营业额折扣率 declare @cardtype_itemtype_rate float exec upg_get_cardtype_itemtype_rate @compid, @cardtype, @good, @paymode, '2', @cardtype_itemtype_rate output set @ex_good_rate = isnull(@ex_good_rate, 1) * isnull(@cardtype_itemtype_rate, 1) set @ex_good_rate_pref = @ex_good_rate * isnull(@perf_rate_good_gam10, 1) --计算积分 exec upg_cal_all_employees_produce_point @quan, @standardprice, @amt, @ex_good_rate_pref, @ex_good_rate_points, @p_mode, @p_setvalue, @p_points output if (@standardprice = 0) set @standardprice = 1 update ggm03 set ggc13f = @cost, ggc28f = @p_points, ggc29f = @price / @standardprice, ggc30f = @ex_good_rate, ggc07f = @in_cost_unit where current of cur_each_goods_item if (@input_good_perf <> 1 and isnull(@staff_1, '') <> '') begin -- 得到提成方案 select top 1 @fa_type = ppt02c from personprecept where ppt00c = @compid and ppt03c = @staff_1 and ppt04i = 1 and ppt02c in (select pre01c from precept where pre00c = @compid and isnull(pre22i, 0) = 0) order by ppt05d desc -- 计算业绩 select @vmode = pro03i, @vsetvalue = pro04f from productprecept with (nolock) where pro00c = @compid and pro01c = @good and pro02c = @fa_type and isnull(pro10c, '*') = @paymode if(isnull(@vmode,'') = '') begin select @vmode = pro03i, @vsetvalue = pro04f from productprecept with (nolock) where pro00c = @compid and pro01c = @good and pro02c = @fa_type and isnull(pro10c, '*') = '*' end exec upg_cal_all_employees_produce_vperf @quan, @standardprice, @in_cost, @cost, @price, @amt, @ex_good_rate_pref, @vmode, @vsetvalue, @ratio_1, @vperf_1 output update ggm03 set ggc21f = @vperf_1 where current of cur_each_goods_item end if (@input_good_perf <> 1 and isnull(@staff_2, '') <> '') begin -- 得到提成方案 -- 获取在账单有效日期内最新一条员工提成方案 select top 1 @fa_type = ppt02c from personprecept where ppt00c = @compid and ppt03c = @staff_2 and ppt04i = 1 and ppt02c in (select pre01c from precept where pre00c = @compid and isnull(pre22i, 0) = 0) order by ppt05d desc -- 计算业绩 select @vmode = pro03i, @vsetvalue = pro04f from productprecept with (nolock) where pro00c = @compid and pro01c = @good and pro02c = @fa_type and isnull(pro10c, '*') = @paymode if(isnull(@vmode,'') = '') begin select @vmode = pro03i, @vsetvalue = pro04f from productprecept with (nolock) where pro00c = @compid and pro01c = @good and pro02c = @fa_type and isnull(pro10c, '*') = '*' end exec upg_cal_all_employees_produce_vperf @quan, @standardprice, @in_cost, @cost, @price, @amt, @ex_good_rate_pref, @vmode, @vsetvalue, @ratio_2, @vperf_2 output update ggm03 set ggc23f = @vperf_2 where current of cur_each_goods_item end if (@input_good_perf <> 1 and isnull(@staff_3, '') <> '') begin -- 得到提成方案 -- 获取在账单有效日期内最新一条员工提成方案 select top 1 @fa_type = ppt02c from personprecept where ppt00c = @compid and ppt03c = @staff_3 and ppt04i = 1 and ppt02c in (select pre01c from precept where pre00c = @compid and isnull(pre22i, 0) = 0) order by ppt05d desc -- 计算业绩 select @vmode = pro03i, @vsetvalue = pro04f from productprecept with (nolock) where pro00c = @compid and pro01c = @good and pro02c = @fa_type and isnull(pro10c, '*') = @paymode if(isnull(@vmode,'') = '') begin select @vmode = pro03i, @vsetvalue = pro04f from productprecept with (nolock) where pro00c = @compid and pro01c = @good and pro02c = @fa_type and isnull(pro10c, '*') = '*' end exec upg_cal_all_employees_produce_vperf @quan, @standardprice, @in_cost, @cost, @price, @amt, @ex_good_rate_pref, @vmode, @vsetvalue, @ratio_3, @vperf_3 output update ggm03 set ggc25f = @vperf_3 where current of cur_each_goods_item end -- added by wuxm, 20060619, 增加产品消费提醒的功能 exec upg_insert_notify_content @compid, @billid, @cardno, @staff_1, 2, @good, @ddate fetch next from cur_each_goods_item into @good, @standardprice, @price, @amt, @quan, @ratio_1, @ratio_2, @ratio_3, @paymode, @staff_1, @staff_2, @staff_3 end close cur_each_goods_item deallocate cur_each_goods_item -- 把产品的进货价格写到消耗的表中 update a set a.gge07f = b.gfa22f from ggm05 a, gfm01 b where a.gge00c = b.gfa00c and a.gge01c = @billid and a.gge00c = @compid and a.gge03c = b.gfa01c end GO /****** Object: StoredProcedure [dbo].[upg_check_upgrade_need] Script Date: 06/12/2017 06:19:41 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_check_upgrade_need]( @compid varchar(10), @cardid varchar(20), -- 会员卡 @curdate varchar(20), @oldcardtype varchar(10), @newcardtype varchar(10), @needamt float output, @lvl int output, -- 0 需要多少钱 @buyamt float output ) as begin declare @total_sale_card float -- 一个时间范围内总的卡销售额 declare @curlvl int declare @curperiod int declare @curcardamt float -- 需要卡销售的金额 declare @curcardtype varchar(10) declare @oldlvl int declare @discount float --老的会员卡的级别 select @oldlvl = gak15i from gam10 where gak00c = @compid and gak01c = @oldcardtype --得到新会员卡类别需要的条件 select @curlvl = gak15i, @curperiod = gak10f, @curcardamt = gak11f from gam10 where gak00c = @compid and gak01c = @newcardtype --计算跳的级别数 select @lvl = isnull(@curlvl, 0) - isnull(@oldlvl, 0) --统计这个时间范围内总的卡销售额 exec upg_cal_total_card_sale @compid, @cardid, @curperiod, @curdate, @total_sale_card output select @buyamt = @total_sale_card --两个相扣,就是需要加的钱 select @needamt = isnull(@curcardamt, 0) - isnull(@total_sale_card, 0) --根据级别来取得折扣 exec upg_get_upgrade_discount @compid, @lvl, @discount output select @needamt = @needamt * @discount return end GO /****** Object: StoredProcedure [dbo].[upg_check_can_consume_one] Script Date: 06/12/2017 06:19:40 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_check_can_consume_one] ( @compid varchar(10), -- 卡是哪个公司的 @cardno varchar(10), -- 卡的号码 @cur_compid varchar(10), -- 当前消费的公司 @ret int output -- 是否可以消费 ) as begin declare @cardtype varchar(5) select @cardtype = min(gca02c) from gcm01 where gca00c = gca13d and gca01c = @cardno exec upg_check_can_consume @compid, @cardtype, @cur_compid, @ret output if @ret = 1 and not exists (select 1 from gcm25 where gcy01c = @cardno and gcy02c = @cur_compid) begin insert gcm25(gcy01c, gcy02c) values(@cardno, @cur_compid) end end GO /****** Object: StoredProcedure [dbo].[upg_check_brother_card_start] Script Date: 06/12/2017 06:19:39 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_check_brother_card_start] ( @cur_compid varchar(10), -- 公司别, 目前所在公司 @cardid varchar(20), -- 卡号 @ret int output -- -2:卡资料不存在 -1:不支持跨店消费 1:支持跨店消费 0:散客 ) as begin if substring(@cardid, 1, 2) = '散客' or isnull(@cardid, '') = '' begin select @ret = 0 return end declare @src_compid varchar(10) declare @cardtype varchar(10) -- 卡的类别 declare @memid varchar(20) --会员编号 select @ret = 0; select @src_compid = min(gca13d), @cardtype = min(gca02c), @memid = min(gca04c) --会员编号 from gcm01 with (nolock) where gca01c = @cardid and gca00c = isnull(gca13d, '') if isnull(@src_compid, '') = '' begin select @ret = -2 return end else if @src_compid != @cur_compid begin declare @canconsume int exec upg_check_can_consume @src_compid, @cardtype, @cur_compid, @canconsume output if @canconsume != 1 begin select @ret = -1 return end end insert gcm25 (gcy01c, gcy02c) select @cardid, @cur_compid where not exists(select 1 from gcm25 where gcy01c = @cardid and gcy02c = @cur_compid) -- 来消费一次,就把会员基本资料给复制过来 exec upg_copy_customer @src_compid, @memid, @cur_compid select @ret = 1 end GO /****** Object: StoredProcedure [dbo].[upg_get_discount_detail] Script Date: 06/12/2017 06:20:07 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_get_discount_detail] ( @compid varchar(10), -- 公司别 @kind varchar(5), -- 类别(1-项目, 2-产品, 3-会员卡) @code varchar(5), -- 代码(项目类别,产品类别,会员卡类别 @cardid varchar(20), -- 会员卡号 @curdate varchar(10), -- 当前日期 @curtime varchar(10), -- 当前时间 @accttype int, -- 账户 @discount float output ) as begin declare @cardtype varchar(20) -- 会员卡的类别 declare @class varchar(5) -- 类别代码(项目类别,产品类别,会员卡类别 declare @card_kind int select @cardtype = gca02c, @card_kind = gca03i from gcm01 with (nolock) where gca00c = @compid and gca01c = @cardid if (@kind = 1) select @class = gda04c from gdm01 where gda00c = @compid and gda01c = @code else if (@kind = 2) select @class = gfa05c from gfm01 where gfa00c = @compid and gfa01c = @code else if (@kind = 3) select @class = @cardtype -- 散客的折扣 if (upper(@cardtype) = 'Z') begin select @accttype = '2' end -- 如果是资格卡, 则不管支付方式都是同样的折扣 if (@card_kind = 2) begin select @accttype = '2' end exec upg_get_discount @compid, @kind, @class, @cardtype, @curdate, @curtime, @accttype, @discount output end GO /****** Object: StoredProcedure [dbo].[upg_insert_gym_fixdata] Script Date: 06/12/2017 06:20:16 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_insert_gym_fixdata] as--WUXM, 2004/11/14 begin exec upg_init_gbm11 exec upg_init_gam21 if not exists(select 1 from gam12) begin --系统管理员 insert gam12(gam01c, gam02c, gam03c, gam04c, gam05c) values('1', '', '1', 'g1', '') insert gam12(gam01c, gam02c, gam03c, gam04c, gam05c) values('1', '', '1', 'g2', '') insert gam12(gam01c, gam02c, gam03c, gam04c, gam05c) values('1', '', '1', 'g3', '') insert gam12(gam01c, gam02c, gam03c, gam04c, gam05c) values('1', '', '1', 'g4', '') insert gam12(gam01c, gam02c, gam03c, gam04c, gam05c) values('1', '', '1', 'g5', '') insert gam12(gam01c, gam02c, gam03c, gam04c, gam05c) values('1', '', '1', 'g7', '') insert gam12(gam01c, gam02c, gam03c, gam04c, gam05c) values('1', '', '1', 'g8', '') insert gam12(gam01c, gam02c, gam03c, gam04c, gam05c) values('1', '', '1', 'g9', '') insert gam12(gam01c, gam02c, gam03c, gam04c, gam05c) values('1', '', '1', 'g10', '') --财务人员 insert gam12(gam01c, gam02c, gam03c, gam04c, gam05c) values('10', '', '1', 'g3', '') insert gam12(gam01c, gam02c, gam03c, gam04c, gam05c) values('10', '', '1', 'g4', '') insert gam12(gam01c, gam02c, gam03c, gam04c, gam05c) values('10', '', '1', 'g5', '') insert gam12(gam01c, gam02c, gam03c, gam04c, gam05c) values('10', '', '1', 'g8', '') --经营管理者 insert gam12(gam01c, gam02c, gam03c, gam04c, gam05c) values('2', '', '1', 'g1', '') insert gam12(gam01c, gam02c, gam03c, gam04c, gam05c) values('2', '', '1', 'g2', '') insert gam12(gam01c, gam02c, gam03c, gam04c, gam05c) values('2', '', '1', 'g3', '') insert gam12(gam01c, gam02c, gam03c, gam04c, gam05c) values('2', '', '1', 'g4', '') insert gam12(gam01c, gam02c, gam03c, gam04c, gam05c) values('2', '', '1', 'g5', '') insert gam12(gam01c, gam02c, gam03c, gam04c, gam05c) values('2', '', '1', 'g8', '') insert gam12(gam01c, gam02c, gam03c, gam04c, gam05c) values('2', '', '1', 'g9', '') insert gam12(gam01c, gam02c, gam03c, gam04c, gam05c) values('2', '', '1', 'g10', '') --收银人员 insert gam12(gam01c, gam02c, gam03c, gam04c, gam05c) values('8', '', '1', 'g1', '') insert gam12(gam01c, gam02c, gam03c, gam04c, gam05c) values('8', '', '1', 'g2', '') insert gam12(gam01c, gam02c, gam03c, gam04c, gam05c) values('8', '', '1', 'g3', '') insert gam12(gam01c, gam02c, gam03c, gam04c, gam05c) values('8', '', '1', 'g5', '') insert gam12(gam01c, gam02c, gam03c, gam04c, gam05c) values('8', '', '1', 'g7', '') insert gam12(gam01c, gam02c, gam03c, gam04c, gam05c) values('8', '', '1', 'g8', '') insert gam12(gam01c, gam02c, gam03c, gam04c, gam05c) values('8', '', '1', 'g9', '') insert gam12(gam01c, gam02c, gam03c, gam04c, gam05c) values('8', '', '1', 'g10', '') --服务人员 insert gam12(gam01c, gam02c, gam03c, gam04c, gam05c) values('9', '', '1', 'g3', '') insert gam12(gam01c, gam02c, gam03c, gam04c, gam05c) values('9', '', '1', 'g5', '') end truncate table gam13 insert gam13 values('1', 'GIA01I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g2', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GIA02I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g3', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GIA03I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g3', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GIA03R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g3', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GIA04I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g3', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GIA05I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g3', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GIA06I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g3', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GIB02I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g2', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') --insert gam13 values('1', 'GIC01I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g2', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GIC02I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g1', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GSB20I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g1', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GSD01I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g1', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GIC04I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g2', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'MER01I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g2', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GIB04I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g3', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GMA01I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g3', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GMA01R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g3', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GIA05R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g3', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GMA02I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g3', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GMA02R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g3', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GMA12R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g3', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GMA13R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g3', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GMA18R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g3', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GMA03I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g3', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GMA04I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g2', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GMA04R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g2', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GMA05R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g2', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GMA06R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g2', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GMA09R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g2', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GMA10R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g2', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GMB13R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g2', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') --insert gam13 values('1', 'GIC01R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g2', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GMA07R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g5', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GMA08R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g5', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GMA11R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g5', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GHB01R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g5', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GHB02R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g5', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GMA14R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g5', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GHA05R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g5', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GHA08R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g5', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GHC01R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g5', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GMA20R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g5', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GMB01I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g2', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GMB11I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g2', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GMB12I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g2', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GMB01R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g3', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GMB02I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g2', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GMB03I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g2', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GMB04I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g2', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GMB05I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g2', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GMB06I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g2', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GMB07I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g2', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GMB09I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g2', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GMB10I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g2', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GMC01I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g3', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GFB01I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g3', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GIB05I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g3', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GMC01R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g3', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GFB01R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g3', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GFB02R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g3', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GMC03R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g3', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GIA04R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g3', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GID01R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g3', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GMC04R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g3', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GMB02R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g4', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GWA12I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g4', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GMB03R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g4', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GMB04R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g4', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GIB06R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g3', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GMC02R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g2', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GMC03I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g7', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GSA01I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g1', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GSA02I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g1', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GSA03I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g1', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GSA05I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g1', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GSA07I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g1', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GSA08I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g1', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GSA09I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g1', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GHB01I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g5', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GHB02I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g5', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GIC03I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g5', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GIC05I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g5', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GWA11I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g5', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GHB03I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g1', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GHA01I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g5', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GHA02I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g5', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GHA04I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g5', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GHA06I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g5', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GHA03I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g5', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GMA16R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g5', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') --insert gam13 values('1', 'GMA17R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g5', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GHA09R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g5', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GHA10R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g5', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GHA04R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g5', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GSA10I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g1', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GSA11I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g1', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GSA12I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g1', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GSA13I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g1', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GSA14I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g1', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GSA15I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g1', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GSA16I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g1', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GSA17I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g1', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GSB01I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g1', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GSB22I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g1', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GSB02I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g1', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GSB04I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g1', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GSB23I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g1', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GSB24I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g1', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GSB06I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g2', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GSB07I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g4', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GSB08I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g7', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GSB09I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g7', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GSB10I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g7', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GSB11I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g7', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GSB12I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g7', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GSB13I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g1', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GSB14I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g7', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GSB15I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g7', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GSB16I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g7', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GSB17I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g7', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GSB18I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g7', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GSB21I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g1', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GWA01I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g8', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GWA02I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g8', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GWA02R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g4', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GWA03I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g8', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GWA03R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g4', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GWA04R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g4', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GWA05R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g4', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GWA06R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g4', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GWA07R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g4', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GWA08R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g4', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GWA09R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g4', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GWA10R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g4', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GWA15R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g8', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GWA16R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g8', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GWA17R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g8', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GWA18R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g8', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GWA21R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g8', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GWA22R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g8', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GWA23R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g8', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GWA26R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g8', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GWA27R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g8', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GWA28R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g8', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GWA29R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g8', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GWC03R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g8', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GWA30R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g8', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GWB01R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g4', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GWB02R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g4', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GMB06R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g4', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GWC01R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g4', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GWA19R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g4', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GWA24R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g4', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GWA25R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g4', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GSC01R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g4', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GSC02R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g4', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GIB07R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g4', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GSC05R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g4', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GIB08R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g4', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GWC04R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g4', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GMA21R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g4', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GMA22R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g4', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GMB05R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g4', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GMA23R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g4', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GMB07R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g4', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GWA10I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g4', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GSC03R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g4', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GSC04R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g4', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GWA20R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g4', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GWA20I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g4', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GWA22I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g4', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GWA23I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g4', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GWA09I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g4', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GSB25I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g10', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GSB28I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g1', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GWC02R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g2', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'goA01I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g9', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'goA02I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g9', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'goA03I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g9', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'goA04I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g9', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'goA05I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g9', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'goA06I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g9', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'goB01I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g9', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'goB02I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g9', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'goC01I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g9', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'goA01R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g9', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'goA04R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g9', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'goA05R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g9', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'goB01R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g9', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'goB02R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g9', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'goB03R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g9', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GWA33I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g3', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GWA34R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g8', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GWA35R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g8', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GWA36R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g8', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GWA38R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g8', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GWA41R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g8', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GWA42R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g8', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GBA01I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g10', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GDA01I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g10', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GSB32I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g10', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GHC03R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g10', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GSB28R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g2', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GSB29R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g2', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GMA19R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g2', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GMA27R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g2', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GMA28R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g2', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GMA29R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g4', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GSB30R', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g3', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') insert gam13 values('1', 'GBF02I', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'g3', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y') delete gam13 where gan02c not in(select program from modulestruct) update gam13 set gan02c = UPPER(gan02c) --Add by maql 20100713 初始化gzm02 declare @cur_role varchar(10) declare rolelist cursor for select distinct gsb02c from gsm02 where gsb01c = 'J' open rolelist fetch rolelist into @cur_role while @@fetch_status = 0 begin insert gzm02 select @cur_role, gan02c, gan03c, gan04c, gan05c, gan06c, gan07c, gan08c, gan09c, gan10c, gan11c, gan12c, gan13c, gan14c, gan15c, gan16c, gan19c from gam13 where not exists(select 1 from gzm02 where gzb01c = @cur_role and gzb02c = gan02c and gzb11c = gan11c) fetch rolelist into @cur_role end close rolelist deallocate rolelist -------set fix data----------------------------------------------- truncate table gsm06 insert gsm06(gsf01c, gsf02c, gsf03c, gsf04c) values('1', 'ga', '公司资料', '单据/数据类别') insert gsm06(gsf01c, gsf02c, gsf03c, gsf04c) values('1', 'gb', '会员资料', '单据/数据类别') insert gsm06(gsf01c, gsf02c, gsf03c, gsf04c) values('1', 'gc', '会员卡资料', '单据/数据类别') insert gsm06(gsf01c, gsf02c, gsf03c, gsf04c) values('1', 'gd', '项目资料', '单据/数据类别') insert gsm06(gsf01c, gsf02c, gsf03c, gsf04c) values('1', 'ge', '会员卡异动单', '单据/数据类别') insert gsm06(gsf01c, gsf02c, gsf03c, gsf04c) values('1', 'gf', '产品资料', '单据/数据类别') insert gsm06(gsf01c, gsf02c, gsf03c, gsf04c) values('1', 'gh', '产品入库单', '单据/数据类别') insert gsm06(gsf01c, gsf02c, gsf03c, gsf04c) values('1', 'gi', '产品出库单', '单据/数据类别') insert gsm06(gsf01c, gsf02c, gsf03c, gsf04c) values('1', 'gl', '出馆结账单', '单据/数据类别') insert gsm06(gsf01c, gsf02c, gsf03c, gsf04c) values('1', 'gn', '会员卡销售', '单据/数据类别') insert gsm06(gsf01c, gsf02c, gsf03c, gsf04c) values('1', 'go', '美容咨询单', '单据/数据类别') insert gsm06(gsf01c, gsf02c, gsf03c, gsf04c) values('1', 'gr', '批量延期单', '单据/数据类别') insert gsm06(gsf01c, gsf02c, gsf03c, gsf04c) values('1', 'gv', '费用登记单', '单据/数据类别') insert gsm06(gsf01c, gsf02c, gsf03c, gsf04c) values('1', 'gp', '其他收入单', '单据/数据类别') insert gsm06(gsf01c, gsf02c, gsf03c, gsf04c) values('1', 'gx', '收银单', '单据/数据类别') insert gsm06(gsf01c, gsf02c, gsf03c, gsf04c) values('1', 'gz', '账户异动单', '单据/数据类别') insert gsm06(gsf01c, gsf02c, gsf03c, gsf04c) values('1', 'ha', '员工资料', '单据/数据类别') insert gsm06(gsf01c, gsf02c, gsf03c, gsf04c) values('1', 'na', '赠劵资料', '单据/数据类别') insert gsm06(gsf01c, gsf02c, gsf03c, gsf04c, gsf05i, gsf06c) values('2', 'L', '项目折扣类别', '常用资料类别', 1, 'gp1') insert gsm06(gsf01c, gsf02c, gsf03c, gsf04c, gsf05i, gsf06c) values('2', 'AR', '项目统计分组', '常用资料类别', 2, 'gp1') insert gsm06(gsf01c, gsf02c, gsf03c, gsf04c, gsf05i, gsf06c) values('2', 'AJ', '项目统计分类', '常用资料类别', 3, 'gp1') insert gsm06(gsf01c, gsf02c, gsf03c, gsf04c, gsf05i, gsf06c) values('2', 'B6', '项目价格称谓', '常用资料类别', 4, 'gp1') insert gsm06(gsf01c, gsf02c, gsf03c, gsf04c, gsf05i, gsf06c) values('2', '66', '咨询项目', '常用资料类别', 5, 'gp1') insert gsm06(gsf01c, gsf02c, gsf03c, gsf04c, gsf05i, gsf06c) values('2', 'AB', '美容类别', '常用资料类别', 6, 'gp1') insert gsm06(gsf01c, gsf02c, gsf03c, gsf04c, gsf05i, gsf06c) values('2', 'A6', '项目水平', '常用资料类别', 7, 'gp1') insert gsm06(gsf01c, gsf02c, gsf03c, gsf04c, gsf05i, gsf06c) values('2', 'BC', '有效期限设置(月)', '常用资料类别', 8, 'gp1') insert gsm06(gsf01c, gsf02c, gsf03c, gsf04c, gsf05i, gsf06c) values('2', 'B', '服务类别', '常用资料类别', 9, 'gp1') insert gsm06(gsf01c, gsf02c, gsf03c, gsf04c, gsf05i, gsf06c) values('2', 'F', '产品折扣类别', '常用资料类别', 10, 'gp2') insert gsm06(gsf01c, gsf02c, gsf03c, gsf04c, gsf05i, gsf06c) values('2', 'AL', '产品统计分类', '常用资料类别', 11, 'gp2') insert gsm06(gsf01c, gsf02c, gsf03c, gsf04c, gsf05i, gsf06c) values('2', 'B7', '产品价格称谓', '常用资料类别', 12, 'gp2') insert gsm06(gsf01c, gsf02c, gsf03c, gsf04c, gsf05i, gsf06c) values('2', 'C1', '入库方式', '常用资料类别', 13, 'gp2') insert gsm06(gsf01c, gsf02c, gsf03c, gsf04c, gsf05i, gsf06c) values('2', 'C2', '出库方式', '常用资料类别', 14, 'gp2') insert gsm06(gsf01c, gsf02c, gsf03c, gsf04c, gsf05i, gsf06c) values('2', 'K', '产品单位', '常用资料类别', 15, 'gp2') insert gsm06(gsf01c, gsf02c, gsf03c, gsf04c, gsf05i, gsf06c) values('2', 'AK', '员工称谓', '常用资料类别', 16, 'gp3') insert gsm06(gsf01c, gsf02c, gsf03c, gsf04c, gsf05i, gsf06c) values('2', 'AI', '奖罚类别', '常用资料类别', 17, 'gp3') insert gsm06(gsf01c, gsf02c, gsf03c, gsf04c, gsf05i, gsf06c) values('2', 'J', '使用者角色', '常用资料类别', 18, 'gp3') insert gsm06(gsf01c, gsf02c, gsf03c, gsf04c, gsf05i, gsf06c) values('2', 'R', '职位资料', '常用资料类别', 19, 'gp3') insert gsm06(gsf01c, gsf02c, gsf03c, gsf04c, gsf05i, gsf06c) values('2', 'S', '职称资料', '常用资料类别', 20, 'gp3') insert gsm06(gsf01c, gsf02c, gsf03c, gsf04c, gsf05i, gsf06c) values('2', 'U', '部门资料', '常用资料类别', 21, 'gp3') insert gsm06(gsf01c, gsf02c, gsf03c, gsf04c, gsf05i, gsf06c) values('2', 'AE', '假别', '常用资料类别', 22, 'gp3') insert gsm06(gsf01c, gsf02c, gsf03c, gsf04c, gsf05i, gsf06c) values('2', '5', '目标名称', '常用资料类别', 23, 'gp3') insert gsm06(gsf01c, gsf02c, gsf03c, gsf04c, gsf05i, gsf06c) values('2', 'T', '聘雇性质', '常用资料类别', 24, 'gp3') insert gsm06(gsf01c, gsf02c, gsf03c, gsf04c, gsf05i, gsf06c) values('2', 'A9', '薪资项目', '常用资料类别', 25, 'gp3') insert gsm06(gsf01c, gsf02c, gsf03c, gsf04c, gsf05i, gsf06c) values('2', 'G', '专长技能', '常用资料类别', 26, 'gp3') insert gsm06(gsf01c, gsf02c, gsf03c, gsf04c, gsf05i, gsf06c) values('2', 'NOS0', '进店渠道', '常用资料类别', 27, 'gp4') insert gsm06(gsf01c, gsf02c, gsf03c, gsf04c, gsf05i, gsf06c) values('2', 'A1', '客户满意度', '常用资料类别', 28, 'gp4') insert gsm06(gsf01c, gsf02c, gsf03c, gsf04c, gsf05i, gsf06c) values('2', 'H', '职业类别', '常用资料类别', 29, 'gp4') insert gsm06(gsf01c, gsf02c, gsf03c, gsf04c, gsf05i, gsf06c) values('2', '6', '身体部位', '常用资料类别', 30, 'gp4') insert gsm06(gsf01c, gsf02c, gsf03c, gsf04c, gsf05i, gsf06c) values('2', 'A3', '调查项目', '常用资料类别', 31, 'gp4') insert gsm06(gsf01c, gsf02c, gsf03c, gsf04c, gsf05i, gsf06c) values('2', '3', '支付方式', '常用资料类别', 32, 'gp5') insert gsm06(gsf01c, gsf02c, gsf03c, gsf04c, gsf05i, gsf06c) values('2', 'A5', '其他收入', '常用资料类别', 33, 'gp5') insert gsm06(gsf01c, gsf02c, gsf03c, gsf04c, gsf05i, gsf06c) values('2', 'B8', '保证金类型', '常用资料类别', 34, 'gp5') insert gsm06(gsf01c, gsf02c, gsf03c, gsf04c, gsf05i, gsf06c) values('2', 'B9', '抵用券类型', '常用资料类别', 35, 'gp5') insert gsm06(gsf01c, gsf02c, gsf03c, gsf04c, gsf05i, gsf06c) values('2', 'AQ', '连锁级别名称', '常用资料类别', 36, 'gp5') insert gsm06(gsf01c, gsf02c, gsf03c, gsf04c, gsf05i, gsf06c) values('2', 'B4', '房间分组', '常用资料类别', 37, 'gp5') insert gsm06(gsf01c, gsf02c, gsf03c, gsf04c, gsf05i, gsf06c) values('2', 'O', '账户类别', '常用资料类别', 38, 'gp5') insert gsm06(gsf01c, gsf02c, gsf03c, gsf04c, gsf05i, gsf06c) values('2', 'N', '费用资料', '常用资料类别', 39, 'gp5') insert gsm06(gsf01c, gsf02c, gsf03c, gsf04c, gsf05i, gsf06c) values('2', 'W2', '竞品品牌', '常用资料类别', 40, 'gp5') insert gsm06(gsf01c, gsf02c, gsf03c, gsf04c, gsf05i, gsf06c) values('2', 'ST', '门店类别', '常用资料类别', 41, 'gp5') insert into gsm06(gsf01c,gsf02c,gsf03c,gsf04c,gsf05i,gsf06c) values('2','DJ','客户等级','常用资料类别',42,'gp5') insert into gsm06(gsf01c,gsf02c,gsf03c,gsf04c,gsf05i,gsf06c) values('2','QZ','客户发展潜质','常用资料类别',43,'gp5') -- 常用资料类别分组 insert gsm06(gsf01c, gsf02c, gsf03c, gsf04c) values('3', 'gp1', '项目', '常用资料类别分组') insert gsm06(gsf01c, gsf02c, gsf03c, gsf04c) values('3', 'gp2', '产品', '常用资料类别分组') insert gsm06(gsf01c, gsf02c, gsf03c, gsf04c) values('3', 'gp3', '员工', '常用资料类别分组') insert gsm06(gsf01c, gsf02c, gsf03c, gsf04c) values('3', 'gp4', '会员', '常用资料类别分组') insert gsm06(gsf01c, gsf02c, gsf03c, gsf04c) values('3', 'gp5', '其它', '常用资料类别分组') end GO /****** Object: StoredProcedure [dbo].[upg_cal_compensation] Script Date: 06/12/2017 06:19:32 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_cal_compensation] ( @billid varchar(20), -- 发薪单号 @compid varchar(10) -- 公司别 ) as-- WUXM, 2004/03/17 begin declare @fromdate varchar(8) --开始日期 declare @todate varchar(8) declare @tax float declare @taxType int create table #staff_list ( compid varchar(10) not null, stuff varchar(10) not null, num float null ) create table #tmp_comm ( stuff varchar(10) not null, comm float null ) select @fromdate = hca05d, @todate = hca06d, @taxType = hca09i, @tax = hca10f from hcm01 where hca00c = @compid and hca01c = @billid -- LOAD员工薪资项目设定至员工发薪金额明细中 delete hcm03 where hcc00c = @compid and hcc01c = @billid insert into hcm03(hcc00c, hcc01c, hcc02c, hcc03c, hcc04c, hcc05c, hcc06f, hcc07c, hcc09f) select hbb00c, @billid, hbb01c, hbb02c, hbb03c, hbb04c, hbb05f, hbb06c, hbb05f from hbm02 where hbb00c = @compid -- UPDATE来源数量栏位(出勤天数) truncate table #staff_list insert #staff_list(compid, stuff, num) select @compid, hda01c, isnull(count(*), 0) from hdm01 where hda00c = @compid and hda02d between @fromdate and @todate and hda07c = '1' group by hda01c update hcm03 set hcc08f = isnull(#staff_list.num, 0) from hcm03, #staff_list where hcm03.hcc00c = #staff_list.compid and hcm03.hcc02c = #staff_list.stuff and hcm03.hcc01c = @billid and hcm03.hcc05c = '2' and hcm03.hcc07c = '1' --- UPDATE来源数量栏位(销卡数量) truncate table #staff_list insert #staff_list(compid, stuff, num) select @compid, gna20c, isnull(count(gna25c), 0) + isnull(count(gnb03c), 0) from gnm01, gnm02 where gna00c = @compid and gna02d between @fromdate and @todate and gna00c = gnb00c and gna01c = gnb01c group by gna20c update hcm03 set hcc08f = isnull(#staff_list.num, 0) from hcm03, #staff_list where hcm03.hcc00c = #staff_list.compid and hcm03.hcc02c = #staff_list.stuff and hcm03.hcc01c = @billid and hcm03.hcc05c = '2' and hcm03.hcc07c = '2' -- 计算提成薪水部分 declare @spid int select @spid = @@spid insert #tmp_comm(stuff, comm) exec upg_personal_statistics @compid, @fromdate, @todate, '*', '', 1, 1, 1, 2, @spid -- 2:每个员工计算一个提成数据 update a set a.hcc09f = isnull(b.comm, 0), a.hcc06f = isnull(b.comm, 0), a.hcc08f = 1 from hcm03 a, #tmp_comm b where a.hcc00c = @compid and a.hcc02c = b.stuff and a.hcc05c = '2' and a.hcc04c = '006' -- 提成薪资项目代码 and a.hcc01c = @billid delete #tmp_comm insert #tmp_comm(stuff, comm) select hah01c, sum(hah05f) from ham08 where hah00c = @compid and hah03d >= @fromdate and hah03d <= @todate group by hah00c, hah01c update a set a.hcc09f = isnull(b.comm, 0), a.hcc06f = isnull(b.comm, 0), a.hcc08f = 1 from hcm03 a, #tmp_comm b where a.hcc00c = @compid and a.hcc02c = b.stuff and a.hcc05c = '2' and a.hcc04c = '007' -- 奖罚 and a.hcc01c = @billid drop table #tmp_comm update hcm03 set hcc06f = isnull(hcc06f, 0), hcc08f = isnull(hcc08f, 0), hcc09f = isnull(hcc09f, 0) where hcc00c = @compid and hcc01c = @billid update hcm03 set hcc09f = isnull(hcc08f, 0) * isnull(hcc06f, 0) where hcc00c = @compid and hcc01c = @billid and hcc05c = '2' -- LOAD员工资料至员工发薪总额表中 delete hcm02 where hcb00c = @compid and hcb01c = @billid insert into hcm02(hcb02c, hcb00c, hcb01c) select distinct hbb01c, @compid, @billid from hbm02 where hbb00c = @compid -- 计算发放总额及扣款总额 update hcm02 set hcb03f = (select sum(isnull(hcc09f, 0)) from hcm03 where hcc00c = @compid and hcc01c = @billid and hcc03c = '1' and hcc02c = hcm02.hcb02c group by hcc02c), hcb04f = (select sum(isnull(hcc09f, 0)) from hcm03 where hcc00c = @compid and hcc01c = @billid and hcc03c = '2' and hcc02c = hcm02.hcb02c group by hcc02c) where hcb00c = @compid and hcb01c = @billid declare temp_cursor cursor for select hcb02c, isnull(hcb03f, 0), isnull(hcb04f, 0) from hcm02 where hcb00c = @compid and hcb01c = @billid declare @stuffNo varchar(10) -- 员工编号 declare @InSum float -- 发放总额 declare @OutSum float -- 扣款总额 declare @taxSum float -- 税额 declare @tax2 float -- 税 declare @proSum float -- 累进金额 declare @income float -- 收入 declare @SP019 float select @SP019 = isnull(gsa03f, 0) from gsm01 where gsa00c = '001' and gsa01c = 'SP019' open temp_cursor fetch temp_cursor into @stuffNo, @InSum, @OutSum while @@FETCH_STATUS <> -1 begin set @taxSum = 0 set @income = @InSum - @OutSum if (@taxType = 2) begin set @taxSum = (@income - @SP019) * @tax end else begin exec upg_cal_tax @income, 0, @taxSum output end set @income = @income - @taxSum update hcm02 set hcb05f = isnull(@taxSum, 0), hcb06f = isnull(@income, 0) where hcb00c = @compid and hcb01c = @billid and hcb02c = @stuffNo fetch next from temp_cursor into @stuffNo, @InSum, @OutSum end close temp_cursor deallocate temp_cursor update hcm02 set hcb03f = isnull(hcb03f, 0), hcb04f = isnull(hcb04f, 0), hcb05f = isnull(hcb05f, 0), hcb06f = isnull(hcb06f, 0) where hcb00c = @compid and hcb01c = @billid end GO /****** Object: StoredProcedure [dbo].[upg_stst_proc_prod_sale] Script Date: 06/12/2017 06:20:46 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_stst_proc_prod_sale] ( @compid varchar(10), -- 一次显示一个公司,包括下面的数据的合计 @fromdate varchar(20), @todate varchar(20), @outtype int ) as begin exec upg_stst_proc_prod_sale_ex @compid, @fromdate, @todate, @outtype, 0 end GO /****** Object: StoredProcedure [dbo].[upg_update_gqm01] Script Date: 06/12/2017 06:20:50 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_update_gqm01] ( @compid varchar(10), -- 公司编号 @BillNo varchar(20), -- 收款/费用单号 @UserId varchar(10), -- 鉴核人 @Date varchar(20), -- 鉴核日期 @isconfirm int ) as ---Write By WUXM begin if @isconfirm = 1 begin update gqm01 set gqa95c = @UserId, gqa96d = @Date where gqa00c = @compid and gqa01c = @BillNo -- 设置帐务日期 WUXM 2005/08/26 exec upg_updategqm01_accountdate @compid, @BillNo end else begin update gqm01 set gqa95c = '', gqa96d = '' where gqa00c = @compid and gqa01c = @BillNo end end GO /****** Object: StoredProcedure [dbo].[upg_cal_work_detail_by_empl] Script Date: 06/12/2017 06:19:35 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_cal_work_detail_by_empl] ( @spid int, @compid varchar(10), -- 公司别 @fromdate varchar(10), -- 开始日期 @todate varchar(10), -- 截至日期 @fromempl varchar(20), -- 查询开始人员 @toempl varchar(20), -- 查询截至人员 @return_type int ) as begin declare @staff_id varchar(20) declare @oper_amt float declare @t_vperf float declare @t_tperf float declare @total_comm float declare @quan2 float -- 数量 declare @consume_cost float -- 消耗成本的变量 declare @cls_flag int declare @f_oper_amt float declare @f_t_vperf float declare @f_t_tperf float declare @f_quan2 float declare @t_oper_amt float declare @t_t_vperf float declare @t_t_tperf float declare @t_quan2 float declare @total_f_quan float declare @total_cust_num float declare @fa_type nvarchar(20) -- 方案编号 --begin add by lm 2015-07-21 create table #work_detail_cash ( id int identity , -- 流水好 person_id varchar(20) not null, -- 员工代码 amt2 float null, --虚业绩 amt3 float null, --真实业绩 billid varchar(20) null, --单号 paycode varchar(20) null, --支付代码 spid int null, --连接号 ) create table #work_detail_billid ( id int identity , -- 流水好 billid varchar(20) null, --单号 spid int null, --连接号 ) create table #work_detail_bill_account ( id int identity , -- 流水好 person_id varchar(20) not null, -- 员工代码 amt2 float null, --虚业绩 amt3 float null, --真实业绩 billid varchar(20) null, --单号 spid int null, ) create table #person_cash ( person_id varchar(20) not null, total_cash float null ) --end -- 是否需要扣除员工领用的消耗成本 declare @is_reduce_consume_cost float select @is_reduce_consume_cost = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP063' -- 从项目类别统计中分组到按员工 delete workdetail_by_empl where spid = @spid -- 按店业绩分段计算按天来 declare @ppt03c nvarchar(50) -- Start 创建临时表 add by lm 2015-08-05 create table #personprecept ( id int identity(1,1) not null, ppt03c varchar(20) not null ) insert #personprecept(ppt03c) select ppt03c from personprecept with (nolock) where ppt00c = @compid and ppt02c in (select pre01c from precept with (nolock) where pre03i = 1 and pre00c = ppt00c and isnull(pre22i, 0) = 0) and ((ppt03c >= @fromempl and ppt03c <= @toempl) or @fromempl = '*') -- 创建循环变量 declare @HasRecord int declare @idd int set @HasRecord = 1 while(@HasRecord = 1) begin if exists(select 1 from #personprecept) begin set @HasRecord = 1 select top 1 @idd = id , @ppt03c = ppt03c from #personprecept if not exists(select 1 from workdetail_by_empl with(nolock) where person_id = @ppt03c and compid = @compid and spid = @spid) insert workdetail_by_empl(spid, person_id, name, compid) values(@spid, @ppt03c, 'N/A', @compid) -- 清除已处理的门店 delete #personprecept where id = @idd end else begin set @HasRecord = 0 end end drop table #personprecept -- insert workdetail_by_empl(spid, compid, person_id, oper_amt, total_vperf, total_tperf, quan, comm, f_quan) select @spid, @compid, person_id, sum(oper_amt), sum(total_vperf), sum(total_tperf), sum(quan), sum(comm), sum(f_quan) from workdetail_by_kind with (nolock) where spid = @spid group by person_id -- Start 创建临时表 add by lm 2015-08-06 -- 依次计算每个员工的提成, 员工的卡,项目,物品合计在一起的业绩 create table #workdetail_by_empl ( id int not null, -- 流水好 person_id varchar(20) not null, -- 员工代码 oper_amt float null, -- 营业金额 total_vperf float null, -- 虚业绩 total_tperf float null, -- 实业绩 quan float null -- 数量 ) insert #workdetail_by_empl (id, person_id, oper_amt, total_vperf, total_tperf, quan ) select id, person_id, oper_amt, total_vperf, total_tperf, quan from workdetail_by_empl where spid = @spid -- 创建循环变量 set @HasRecord = 1 declare @currentCompId varchar(10) while(@HasRecord = 1) begin if exists(select 1 from #workdetail_by_empl) begin set @HasRecord = 1 select top 1 @idd = id, @staff_id = person_id, @oper_amt = oper_amt, @t_vperf = total_vperf, @t_tperf = total_tperf, @quan2 = quan from #workdetail_by_empl -- 判断是否这个员工是按员工合计来计算提成的 declare @commway float -- 提成方式 declare @projectway float -- 项目提成方式 declare @productway float -- 产品提成方式 declare @cardway float -- 卡提成方式 declare @pre06i float -- 店总业绩计算方式 1分段 2扣基数乘比率 declare @pre09i float -- 个人总业绩计算方式 1分段 2扣基数乘比率 declare @baseamt float -- 基数 declare @ratio float -- 比率 select @commway = pre03i, @projectway = pre12i, @productway = pre13i, @cardway = pre14i, @fa_type = pre01c, @pre06i = pre06i, @pre09i = pre09i, @baseamt = pre10f, @ratio = pre11f from precept with(nolock) where pre01c = (select top 1 ppt02c from personprecept where ppt03c = @staff_id and ppt00c = @compid and ppt04i = 1 order by ppt05d desc) and pre00c = @compid and isnull(pre22i, 0) = 0 select @total_comm = 0 if @commway = 2 --if (@commway = 1 and @pre06i = 1 ) or (@commway = 2 and @pre09i = 1)--按员工总业绩分段/店总业绩分段 begin if @pre09i = 1--分段计算 begin -- 按一个员工所有总的业绩计算提成,不支持指定客数分段的概念 exec upg_cal_sum_comm @compid, @oper_amt, @t_vperf, @t_tperf, @quan2, @oper_amt, @t_vperf, @t_tperf, @quan2, 0, 0, 0, 0, @staff_id, 4, '', 0, 0, @total_comm output, @fa_type,0,0 end else if @pre09i = 2--扣基数乘以比率 begin -- 店总的业绩扣一个基数计算提成 select @total_comm = (@t_vperf - @baseamt) * @ratio end update workdetail_by_empl set comm = isnull(comm, 0) + @total_comm where id = @idd end else if @commway = 3 and (@projectway = 2 or @productway = 2 or @cardway = 2) begin -- 只能到workdetail_by_kind中取的一个员工项目的总合计,物品的总合计,卡的总合计,然后三个分别计算 declare @three_sum_comm float -- 卡,物品,项目的三个类别的提成合计 declare @comm_prj float declare @comm_good float declare @comm_card float select @three_sum_comm = 0, @comm_prj = 0, @comm_good = 0, @comm_card = 0 --Start 创建临时表 add by lm 2015-08-06 create table #workdetail_by_kind ( id int identity(1,1) not null, cls_flag int not null, -- 1:项目,2:产品,3:卡 oper_amt float null, -- 营业金额 total_vperf float null, -- 虚业绩 total_tperf float null, -- 实业绩 quan float null, -- 数量 f_oper_amt float null, -- 营业金额(指定) f_total_vperf float null, -- 虚业绩(指定) f_total_tperf float null, -- 实业绩(指定) f_quan float null, -- 数量(指定) t_oper_amt float null, -- 营业金额(轮班) t_total_vperf float null, -- 虚业绩(轮班) t_total_tperf float null, -- 实业绩(轮班) t_quan float null, -- 数量(轮班) total_f_quan float null, --每个员工不分项目类别的总的指定客数 total_cust_num float null --总的服务客户数量 ) insert #workdetail_by_kind(cls_flag, oper_amt, total_vperf, total_tperf, quan, f_oper_amt, f_total_vperf, f_total_tperf, f_quan, t_oper_amt, t_total_vperf, t_total_tperf, t_quan, total_f_quan, total_cust_num ) select cls_flag, sum(oper_amt), sum(total_vperf), sum(total_tperf), sum(quan), sum(f_oper_amt), sum(f_total_vperf), sum(f_total_tperf), sum(f_quan), sum(t_oper_amt), sum(t_total_vperf), sum(t_total_tperf), sum(t_quan), sum(total_f_quan), sum(total_cust_num) from workdetail_by_kind with (nolock) where spid = @spid and person_id = @staff_id group by cls_flag declare @HasRecordInner int declare @iddInner int -- 循环变量 set @HasRecordInner = 1 while(@HasRecordInner = 1) begin if exists(select 1 from #workdetail_by_kind) begin set @HasRecordInner = 1 select top 1 @iddInner = id , @cls_flag = cls_flag, @oper_amt = oper_amt, @t_vperf = total_vperf, @t_tperf = total_tperf, @quan2 = quan, @f_oper_amt = f_oper_amt, @f_t_vperf = f_total_vperf, @f_t_tperf = f_total_tperf, @f_quan2 = f_quan, @t_oper_amt = t_oper_amt, @t_t_vperf = t_total_vperf, @t_t_tperf = t_total_tperf, @t_quan2 = t_quan, @total_f_quan = total_f_quan, @total_cust_num = total_cust_num from #workdetail_by_kind -- @cls_flag 1:项目 2:产品 3:卡 if (@commway = 3 and @cls_flag = 1 and @projectway = 2) or (@commway = 3 and @cls_flag = 2 and @productway = 2) or (@commway = 3 and @cls_flag = 3 and @cardway = 2) begin exec upg_cal_sum_comm @compid, @oper_amt, @t_vperf, @t_tperf, @quan2, @f_oper_amt, @f_t_vperf, @f_t_tperf, @f_quan2, @t_oper_amt, @t_t_vperf, @t_t_tperf, @t_quan2, @staff_id, @cls_flag, '', @total_f_quan, @total_cust_num, @total_comm output, @fa_type,0,0 if @cls_flag = 1 select @comm_prj = @total_comm else if @cls_flag = 2 select @comm_good = @total_comm else if @cls_flag = 3 select @comm_card = @total_comm select @three_sum_comm = @three_sum_comm + isnull(@total_comm, 0) end -- 清除已处理的数据 delete #workdetail_by_kind where id = @iddInner end else begin set @HasRecordInner = 0 end end drop table #workdetail_by_kind -- --End update workdetail_by_empl set comm = isnull(comm, 0) + @three_sum_comm, comm_prj = @comm_prj, comm_good = @comm_good, comm_card = @comm_card where id = @idd end select @consume_cost = 0 if @is_reduce_consume_cost = 1 begin -- 需要计算每个员工的消耗成本 exec upg_cal_staff_consume_cost @compid, @staff_id, @fromdate, @todate, @consume_cost output update workdetail_by_empl set consume_cost = @consume_cost where id = @idd end -- 清除已处理的门店 delete #workdetail_by_empl where id = @idd end else begin set @HasRecord = 0 end end drop table #workdetail_by_empl --End -- 更新员工姓名 update a set a.name = b.haa02c from workdetail_by_empl a, ham01 b where spid = @spid and b.haa00c = @compid and a.person_id = b.haa01c -- 再次更新员工姓名(总公司的下属公司的员工, 可能总公司没有资料) update a set a.name = b.haa02c from workdetail_by_empl a, ham01 b where spid = @spid and a.person_id = b.haa01c and isnull(a.name, '') = '' -- 统计每个员工的服务 + 产品的现金以及银行卡 declare @sp055f float select @sp055f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP055' if @sp055f = 0 -- 按项目支付 begin if (@return_type = 4) begin insert #person_cash(person_id, total_cash) select person_id, sum(isnull(amt, 0)) from work_detail with (nolock) where (paycode = '1' or paycode = '6' or paycode = 'ZFB' or paycode = 'WX') and (cls_flag = 1 or cls_flag = 2) and spid = @spid group by person_id end else if @return_type = 3 begin insert #person_cash(person_id, total_cash) select person_id, sum(isnull(amt2, 0)) from work_detail with (nolock) where spid = @spid and (paycode = '1' or paycode = '6' or paycode = 'ZFB' or paycode = 'WX') and (cls_flag = 1 or cls_flag = 2) group by person_id end else if @return_type = 5 begin insert #person_cash(person_id, total_cash) select person_id, sum(isnull(amt3, 0)) from work_detail with (nolock) where spid = @spid and (paycode = '1' or paycode = '6' or paycode = 'ZFB' or paycode = 'WX') and (cls_flag = 1 or cls_flag = 2) group by person_id end update a set a.total_cash = isnull( b.total_cash, 0) from workdetail_by_empl a, #person_cash b where a.person_id = b.person_id and spid = @spid -- Added by WUXM, 20070111 -- 统计项目的现金支付以及卡支付,所谓的实做业绩,服务实做,卡实做 delete #person_cash -- 先统计项目的现金实做,包括现金,银行卡 if @return_type = 4 begin insert #person_cash(person_id, total_cash) select person_id, sum(isnull(amt, 0)) from work_detail with (nolock) where spid = @spid and (paycode = '1' or paycode = '6' or paycode = 'ZFB' or paycode = 'WX') and (cls_flag = 1) group by person_id end else if @return_type = 3 begin insert #person_cash(person_id, total_cash) select person_id, sum(isnull(amt2, 0)) from work_detail with (nolock) where spid = @spid and (paycode = '1' or paycode = '6' or paycode = 'ZFB' or paycode = 'WX') and (cls_flag = 1) group by person_id end else if @return_type = 5 begin insert #person_cash(person_id, total_cash) select person_id, sum(isnull(amt3, 0)) from work_detail with (nolock) where spid = @spid and (paycode = '1' or paycode = '6'or paycode = 'ZFB' or paycode = 'WX') and (cls_flag = 1) group by person_id end update a set a.total_srv_cash = isnull( b.total_cash, 0) from workdetail_by_empl a, #person_cash b where a.person_id = b.person_id and spid = @spid --卖卡和冲值-现金单据包含签单挂帐,业绩计算add by lm 2015-06-03 --begin-- insert #work_detail_billid(billid, spid) --插入现金的单据 select distinct billid, spid from work_detail with(nolock) where spid = @spid and (paycode = '1' or paycode = '6' or paycode = 'ZFB' or paycode = 'WX') and (action_id = 1 or action_id = 0) insert #work_detail_cash(person_id, amt2, amt3, billid, paycode, spid)--插入数据 select person_id, a.amt2, a.amt3, a.billid, paycode, a.spid from #work_detail_billid b, work_detail a with(nolock) where a.spid = b.spid and a.billid = b.billid and a.spid = @spid and (a.action_id = 1 or a.action_id = 0) insert #work_detail_bill_account(person_id, amt2, amt3, billid, spid)--插入现金单据中支付方式为签单挂帐的 select person_id, amt2, amt3, billid, spid from #work_detail_cash where spid = @spid and paycode = '5' --end--------------- -- 统计项目的卡实做 delete #person_cash if @return_type = 4 begin insert #person_cash(person_id, total_cash) select person_id, sum(isnull(amt, 0)) from work_detail with(nolock) where spid = @spid and (paycode = '#' or paycode = '4' or paycode = '9' or (paycode >= 'A' and paycode <= 'Z')) and (cls_flag = 1) group by person_id end else if @return_type = 3 begin insert #person_cash(person_id, total_cash) select person_id, sum(isnull(amt2, 0)) from work_detail with(nolock) where spid = @spid and (paycode = '#' or paycode = '4' or paycode = '9' or (paycode >= 'A' and paycode <= 'Z')) and (cls_flag = 1) group by person_id end else if @return_type = 5 begin insert #person_cash(person_id, total_cash) select person_id, sum(isnull(amt3, 0)) from work_detail with(nolock) where spid = @spid and (paycode = '#' or paycode = '4' or paycode = '9' or (paycode >= 'A' and paycode <= 'Z')) and (cls_flag = 1) group by person_id end update a set a.total_srv_card = isnull( b.total_cash, 0) from workdetail_by_empl a, #person_cash b where a.person_id = b.person_id and spid = @spid end -- 统计每个员工的奖罚 create table #ham08_group ( person_id varchar(20) not null, amt float null, primary key(person_id) ) insert #ham08_group(person_id, amt) select hah01c, sum(isnull(hah05f, 0)) from ham08 with (nolock) where hah00c = @compid and hah03d >= @fromdate and hah03d <= @todate group by hah01c update a set a.bonus = b.amt from workdetail_by_empl a, #ham08_group b where a.person_id = b.person_id and spid = @spid drop table #ham08_group -- 统计每个员工的服务项目的数量,分别统计指定的数量及轮班的数量 add by lm (项目数改为单据数:用这个字段的过程有) update a set a.num_fix = (select count(distinct billid) from work_detail b with(nolock) where b.spid = @spid and a.person_id = b.person_id and (b.action_id = 2 or b.action_id = 5 or b.action_id = 7 or b.action_id = 10) group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.num_nofix = (select count(distinct billid) from work_detail b with(nolock) where b.spid = @spid and a.person_id = b.person_id and (b.action_id = 3 or b.action_id = 6 or b.action_id = 8 or b.action_id = 11) group by b.person_id) from workdetail_by_empl a where a.spid = @spid --内创数量 add by lm 2016.04.13 update a set a.num_in= (select count(distinct billid) from work_detail b where b.spid = @spid and a.person_id = b.person_id and (b.action_id = 15 or b.action_id = 18 or b.action_id = 20 or b.action_id = 23) group by b.person_id) from workdetail_by_empl a where a.spid = @spid --外创数量 add by lm 2016.04.13 update a set a.num_outer = (select count(distinct billid) from work_detail b where b.spid = @spid and a.person_id = b.person_id and (b.action_id = 16 or b.action_id = 19 or b.action_id = 21 or b.action_id = 24) group by b.person_id) from workdetail_by_empl a where a.spid = @spid if (@return_type = 4) --营业金额 begin update a set a.amt_c = ( select sum(b.amt) from work_detail b with(nolock) where b.spid = @spid and (b.action_id = 1 or b.action_id = 0) and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_c_cash = (select sum(b.amt) from work_detail b with(nolock) where b.spid = @spid and (b.action_id = 1 or b.action_id = 0) and a.person_id = b.person_id and (b.paycode = '1' or b.paycode = '6' or b.paycode = 'ZFB' or b.paycode = 'WX') group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_1 = (select sum(b.amt) from work_detail b with(nolock) where b.spid = @spid and action_id = 2 and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_12 = (select sum(b.amt) from work_detail b with(nolock) where b.spid = @spid and b.action_id = 3 and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_13 = (select sum(b.amt) from work_detail b with(nolock) where b.spid = @spid and b.action_id = 4 and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_2 = (select sum(b.amt) from work_detail b with(nolock) where b.spid = @spid and b.action_id = 5 and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_21 = (select sum(b.amt) from work_detail b with(nolock) where b.spid = @spid and b.action_id = 6 and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_3 = (select sum(b.amt) from work_detail b with(nolock) where b.spid = @spid and (b.action_id = 7) and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_31 = (select sum(b.amt) from work_detail b with(nolock) where b.spid = @spid and (b.action_id = 8) and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_4 = (select sum(b.amt) from work_detail b with(nolock) where b.spid = @spid and (b.action_id = 10) and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_41 = (select sum(b.amt) from work_detail b with(nolock) where b.spid = @spid and (b.action_id = 11) and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid --start增加内创,外创 营业额 add by lm 2016.04.12 update a set a.amt_1_14 = (select sum(b.amt) from work_detail b with(nolock) where b.spid = @spid and (b.action_id = 15) and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_1_15 = (select sum(b.amt) from work_detail b with(nolock) where b.spid = @spid and (b.action_id = 16) and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_2_14 = (select sum(b.amt) from work_detail b with(nolock) where b.spid = @spid and (b.action_id = 18) and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_2_15 = (select sum(b.amt) from work_detail b with(nolock) where b.spid = @spid and (b.action_id = 19) and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_3_14 = (select sum(b.amt) from work_detail b with(nolock) where b.spid = @spid and (b.action_id = 20) and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_3_15 = (select sum(b.amt) from work_detail b with(nolock) where b.spid = @spid and (b.action_id = 21) and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_4_14 = (select sum(b.amt) from work_detail b with(nolock) where b.spid = @spid and (b.action_id = 23) and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_4_15 = (select sum(b.amt) from work_detail b with(nolock) where b.spid = @spid and (b.action_id = 24) and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid --end update a set a.amt_g = (select sum(b.amt) from work_detail b with(nolock) where b.spid = @spid and b.action_id = 9 and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid end if (@return_type = 3) -- 虚业绩 begin update a set a.amt_c = (select sum(b.amt2) from work_detail b with(nolock) where b.spid = @spid and (b.action_id = 1 or b.action_id = 0) and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_c_cash = (select sum(b.amt2) from work_detail b with(nolock) where b.spid = @spid and (b.action_id = 1 or b.action_id = 0) and a.person_id = b.person_id and (b.paycode = '1' or b.paycode = '6' or b.paycode = 'ZFB' or b.paycode = 'WX') group by b.person_id) from workdetail_by_empl a where a.spid = @spid -- 把现金的签单挂帐业绩加上去 add by lm 2015-06-03 update a set a.amt_c_cash = a.amt_c_cash + isnull(b.amt2, 0) from workdetail_by_empl a, (select person_id, sum(amt2) as amt2 from #work_detail_bill_account with (nolock) where spid = @spid group by person_id)b where a.spid = @spid and a. person_id = b.person_id update a set a.amt_1 = (select sum(b.amt2) from work_detail b with(nolock) where b.spid = @spid and b.action_id = 2 and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_12 = (select sum(b.amt2) from work_detail b with(nolock) where b.spid = @spid and b.action_id = 3 and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_13 = (select sum(b.amt2) from work_detail b with(nolock) where b.spid = @spid and b.action_id = 4 and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_2 = (select sum(b.amt2) from work_detail b with(nolock) where b.spid = @spid and b.action_id = 5 and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_21 = (select sum(b.amt2) from work_detail b with(nolock) where b.spid = @spid and b.action_id = 6 and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_3 = (select sum(b.amt2) from work_detail b with(nolock) where b.spid = @spid and (b.action_id = 7) and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_31 = (select sum(b.amt2) from work_detail b with(nolock) where b.spid = @spid and (b.action_id = 8) and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_4 = (select sum(b.amt2) from work_detail b with(nolock) where b.spid = @spid and (b.action_id = 10) and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_41 = (select sum(b.amt2) from work_detail b with(nolock) where b.spid = @spid and (b.action_id = 11) and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid --start增加内创,外创 虚业绩 add by lm 2016.04.12 update a set a.amt_1_14 = (select sum(b.amt2) from work_detail b with(nolock) where b.spid = @spid and (b.action_id = 15) and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_1_15 = (select sum(b.amt2) from work_detail b with(nolock) where b.spid = @spid and (b.action_id = 16) and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_2_14 = (select sum(b.amt2) from work_detail b with(nolock) where b.spid = @spid and (b.action_id = 18) and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_2_15 = (select sum(b.amt2) from work_detail b with(nolock) where b.spid = @spid and (b.action_id = 19) and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_3_14 = (select sum(b.amt2) from work_detail b with(nolock) where b.spid = @spid and (b.action_id = 20) and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_3_15 = (select sum(b.amt2) from work_detail b with(nolock) where b.spid = @spid and (b.action_id = 21) and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_4_14 = (select sum(b.amt2) from work_detail b with(nolock) where b.spid = @spid and (b.action_id = 23) and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_4_15 = (select sum(b.amt2) from work_detail b with(nolock) where b.spid = @spid and (b.action_id = 24) and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid --end update a set a.amt_g = (select sum(b.amt2) from work_detail b with(nolock) where b.spid = @spid and b.action_id = 9 and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid end if (@return_type = 5) -- 真实业绩 begin update a set a.amt_c = (select sum(b.amt3) from work_detail b with(nolock) where b.spid = @spid and (b.action_id = 1 or b.action_id = 0) and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_c_cash = (select sum(b.amt3) from work_detail b with(nolock) where b.spid = @spid and (b.action_id = 1 or b.action_id = 0) and a.person_id = b.person_id and (b.paycode = '1' or b.paycode = '6' or b.paycode = 'ZFB' or b.paycode = 'WX') group by b.person_id) from workdetail_by_empl a where a.spid = @spid -- 把现金的签单挂帐业绩加上去 add by lm 2015-06-03 update a set a.amt_c_cash = a.amt_c_cash + isnull(b.amt3, 0) from workdetail_by_empl a, (select person_id, sum(amt3) as amt3 from #work_detail_bill_account with (nolock) where spid = @spid group by person_id)b where a.spid = @spid and a. person_id = b.person_id update a set a.amt_1 = (select sum(b.amt3) from work_detail b with(nolock) where b.spid = @spid and b.action_id = 2 and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_12 = (select sum(b.amt3) from work_detail b with(nolock) where b.spid = @spid and b.action_id = 3 and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_13 = (select sum(b.amt3) from work_detail b with(nolock) where b.spid = @spid and b.action_id = 4 and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_2 = (select sum(b.amt3) from work_detail b with(nolock) where b.spid = @spid and b.action_id = 5 and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_21 = (select sum(b.amt3) from work_detail b with(nolock) where b.spid = @spid and b.action_id = 6 and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_3 = (select sum(b.amt3) from work_detail b with(nolock) where b.spid = @spid and (b.action_id = 7) and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_31 = (select sum(b.amt3) from work_detail b with(nolock) where b.spid = @spid and (b.action_id = 8) and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_4 = (select sum(b.amt3) from work_detail b with(nolock) where b.spid = @spid and (b.action_id = 10) and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_41 = (select sum(b.amt3) from work_detail b with(nolock) where b.spid = @spid and (b.action_id = 11) and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid --start增加内创,外创实业绩 add by lm 2016.04.12 update a set a.amt_1_14 = (select sum(b.amt3) from work_detail b with(nolock) where b.spid = @spid and (b.action_id = 15) and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_1_15 = (select sum(b.amt3) from work_detail b with(nolock) where b.spid = @spid and (b.action_id = 16) and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_2_14 = (select sum(b.amt3) from work_detail b with(nolock) where b.spid = @spid and (b.action_id = 18) and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_2_15 = (select sum(b.amt3) from work_detail b with(nolock) where b.spid = @spid and (b.action_id = 19) and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_3_14 = (select sum(b.amt3) from work_detail b with(nolock) where b.spid = @spid and (b.action_id = 20) and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_3_15 = (select sum(b.amt3) from work_detail b with(nolock) where b.spid = @spid and (b.action_id = 21) and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_4_14 = (select sum(b.amt3) from work_detail b with(nolock) where b.spid = @spid and (b.action_id = 23) and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_4_15 = (select sum(b.amt3) from work_detail b with(nolock) where b.spid = @spid and (b.action_id = 24) and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid --end update a set a.amt_g = (select sum(b.amt3) from work_detail b with(nolock) where b.spid = @spid and b.action_id = 9 and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid end -- 计算一下那些计算型字段 update workdetail_by_empl set total_srv = isnull(amt_1, 0) + isnull(amt_12, 0) + isnull(amt_13, 0) + isnull(amt_2, 0) + isnull(amt_21, 0) + isnull(amt_3, 0) + isnull(amt_31, 0) + isnull(amt_4, 0) + isnull(amt_41, 0) + isnull(amt_1_14, 0) + isnull(amt_1_15, 0)+ isnull(amt_2_14, 0) + isnull(amt_2_15, 0)+ isnull(amt_3_14, 0) + isnull(amt_3_15, 0) + isnull(amt_4_14, 0) + isnull(amt_4_15, 0), total_perf = isnull(amt_1, 0) + isnull(amt_12, 0) + isnull(amt_13, 0) + isnull(amt_2, 0) + isnull(amt_21, 0) + isnull(amt_3, 0) + isnull(amt_31, 0) + isnull(amt_g, 0) + isnull(amt_4, 0) + isnull(amt_41, 0) + isnull(amt_1_14, 0) + isnull(amt_1_15, 0)+ isnull(amt_2_14, 0) + isnull(amt_2_15, 0)+ isnull(amt_3_14, 0) + isnull(amt_3_15, 0) + isnull(amt_4_14, 0) + isnull(amt_4_15, 0), total_cash_c = isnull(total_cash, 0) + isnull(amt_c_cash, 0) where spid = @spid declare @total_service float select @total_service = sum(amt_1) from workdetail_by_empl with (nolock) where spid = @spid if @total_service > 0 begin update a set a.amtrate_1 = isnull(amt_1, 0) / @total_service from workdetail_by_empl a where a.spid = @spid end drop table #person_cash drop table #work_detail_cash drop table #work_detail_billid drop table #work_detail_bill_account --add by lm 2015-07-21 end GO /****** Object: StoredProcedure [dbo].[upg_report_gwn04r] Script Date: 06/12/2017 06:20:37 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO Create proc [dbo].[upg_report_gwn04r] ( @fromdate varchar(10), --起始日期 @todate varchar(10), --结束日期 @companyid varchar(10), --门店 @gda02c varchar(10) --项目大类 ) as begin declare @spid int --Start 创建临时表 add by lm 2016-01-28 create table #gam05 ( gae01c varchar(20) not null ) create table #work_detail ( id int identity , -- 流水好 person_id varchar(20) not null, -- 员工代码 action_id tinyint not null, -- 类别,1-卡销售 2-指定大工,3-轮班大工,4-补工大工, -- 5-指定中工,6-轮班中工 -- 7-指定小工, 8-轮班小工,9-产品销售 10,11-四工 -- 0 卡充值 srvdate varchar(10) not null, --日期 code varchar(20) not null, --项目代码,或是卡号,产品码 name varchar(200) null, --名称 payway varchar(20) null, --支付方式 amt float null, --营业金额 quan float null, --数量 cost float null, --成本 comm float null, --提成 amt2 float null, --虚业绩 amt3 float null, --真实业绩 cardrate float null, --转卡比率 --当买卡或充值的时候,这个地方当作疗程账户金额用 stdprice float null, --标准单价 prj_type varchar(20) null, --项目类别 share_rate float null default 1, --分享比率 cls_flag int null, -- 1:项目 2:产品 3:卡 gender int null, --性别 0:女 1:男 billid varchar(20) null, --单号 paycode varchar(20) null, --支付代码 ex_rate float null, --实际营业额折算比率 ggm02_seq float null, --ggm02中的序列号,与单号一起来标志一个项目 room_id varchar(20) null, --房间号,只在spa或足浴版本需要 compid varchar(10) null, --公司别 spid int null, --连接号 cardid varchar(20) null, --会员卡号 memid varchar(40) null, --会员编号 comp_ascription varchar(10) null,-- 作业公司 ) insert #gam05(gae01c) select gae01c from gam05 with(nolock) -- 创建循环变量 declare @HasRecord int set @HasRecord = 1 declare @incompid varchar(10) -- 插入旗下所有子门店的营业数据 while(@HasRecord = 1) begin if exists(select 1 from #gam05) begin set @HasRecord = 1 select top 1 @incompid = gae01c from #gam05 select @spid = 3000 exec upg_prepare_work_detail @spid, @incompid, @fromdate, @todate, '*', '*', 1, 1, 1, 1 -- 保存原公司别 update work_detail set comp_ascription = compid where spid = @spid insert #work_detail(person_id,action_id,srvdate,code ,name,payway, amt,quan,cost,comm,amt2,amt3,cardrate,stdprice,prj_type,share_rate, cls_flag,gender,billid,paycode,ex_rate,ggm02_seq,room_id,compid, spid,cardid,memid ,comp_ascription ) select person_id,action_id,srvdate,code ,name,payway, amt,quan,cost,comm,amt2,amt3,cardrate,stdprice,prj_type,share_rate, cls_flag,gender,billid,paycode,ex_rate,ggm02_seq,room_id,compid, spid,cardid,memid ,comp_ascription from work_detail with (nolock) where spid = @spid and ((srvdate >= @fromdate and srvdate <= @todate) or @fromdate = '*' ) -- select * from #work_detail --where compid = '002' -- 清除已处理的数据 delete #gam05 where gae01c = @incompid end else begin set @HasRecord = 0 end end drop table #gam05 --End create table #comp_amt ( id int identity , -- 流水好 code varchar(20) not null, --项目代码,或是卡号,产品码 payway varchar(20) null, --支付方式 amt float null, --营业金额 comm float null, --提成 amt2 float null, --虚业绩 amt3 float null, --真实业绩 prj_type varchar(20) null, --项目类别 cls_flag int null, -- 1:项目 2:产品 3:卡 gender int null, --性别 0:女 1:男 billid varchar(20) null, --单号 paycode varchar(20) null, --支付代码 ggm02_seq float null, --ggm02中的序列号,与单号一起来标志一个项目 room_id varchar(20) null, --房间号,只在spa或足浴版本需要 compid varchar(10) null, --公司别 cardid varchar(20) null, --会员卡号 memid varchar(40) null, --会员编号 ) create nonclustered index idx_comp_amt_01 on #comp_amt(cls_flag) insert #comp_amt (code,payway,amt2,amt3,cls_flag,gender,billid,paycode, ggm02_seq,compid,cardid, memid ) select distinct code,payway,amt2,amt3,cls_flag,gender,billid,paycode, ggm02_seq,compid,cardid, memid from #work_detail with (nolock) if( @companyid = '' and @gda02c = '' ) begin --门店占比 select compid + '[' + (select gae02c from gam05 with(nolock) where gae01c = compid) + ']' [name], sum(amt3) amt3 from #comp_amt where cls_flag = 1 and srvdate between @fromdate and @todate and amt3 > 0 group by compid --男,女 select case gender when 0 then '女' when 1 then '男' end [name], sum(amt3) amt3 from #comp_amt where cls_flag = 1 and srvdate between @fromdate and @todate and amt3 > 0 group by gender --会员,非会员 select '非会员' [name], sum(amt3) amt3 from #comp_amt where cls_flag = 1 and srvdate between @fromdate and @todate and (memid is null or memid = '') and amt3 > 0 union all select '会员' [name], sum(amt3) amt3 from #comp_amt where cls_flag = 1 and srvdate between @fromdate and @todate and memid is not null and memid != '' and amt3 > 0 end else if( @companyid != '' and @gda02c = '' ) begin --项目(部门) select gda02c + '['+ (select top 1 gsb03c from gsm02 with(nolock) where gsb01c='U' and gsb00c = compid and gsb02c = gda02c) +']' [name], sum(amt3) amt3 from #comp_amt inner join gdm01 with(nolock) on gda00c = compid and code = gda01c where cls_flag = 1 and compid = @companyid and srvdate between @fromdate and @todate and amt3 > 0 group by gda02c, compid --男,女 select case gender when 0 then '女' when 1 then '男' end [name], sum(amt3) amt3 from #comp_amt where cls_flag = 1 and compid = @companyid and srvdate between @fromdate and @todate and amt3 > 0 group by gender --会员,非会员 select '非会员' [name], sum(amt3) amt3 from #comp_amt where cls_flag = 1 and compid = @companyid and srvdate between @fromdate and @todate and (memid is null or memid = '') and amt3 > 0 union all select '会员' [name], sum(amt3) amt3 from #comp_amt where cls_flag = 1 and compid = @companyid and srvdate between @fromdate and @todate and memid is not null and memid != '' and amt3 > 0 end else if( @companyid != '' and @gda02c != '' ) begin --小类 select code + '[' + name + ']' [name], sum(amt3) amt3 from #comp_amt where cls_flag = 1 and compid = @companyid and code in ( select gda01c from gdm01 with(nolock) where gda00c = compid and gda02c = @gda02c ) and srvdate between @fromdate and @todate and amt3 > 0 group by code,name --男,女 select case gender when 0 then '女' when 1 then '男' end [name], sum(amt3) amt3 from #comp_amt where cls_flag = 1 and compid = @companyid and code in ( select gda01c from gdm01 with(nolock) where gda00c = compid and gda02c = @gda02c ) and srvdate between @fromdate and @todate and amt3 > 0 group by gender --会员,非会员 select '非会员' [name], sum(amt3) amt3 from #comp_amt where cls_flag = 1 and compid = @companyid and code in ( select gda01c from gdm01 with(nolock) where gda00c = compid and gda02c = @gda02c ) and srvdate between @fromdate and @todate and (memid is null or memid = '') and amt3 > 0 union all select '会员' [name], sum(amt3) amt3 from #comp_amt where cls_flag = 1 and compid = @companyid and code in ( select gda01c from gdm01 with(nolock) where gda00c = compid and gda02c = @gda02c ) and srvdate between @fromdate and @todate and memid is not null and memid != '' and amt3 > 0 end drop table #work_detail drop table #comp_amt end GO /****** Object: StoredProcedure [dbo].[upg_prepare_day_account] Script Date: 06/12/2017 06:20:27 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_prepare_day_account] ( @compid varchar(10), @ddate varchar(10), @calflag int ) as begin declare @lastremain float declare @today_save float declare @lastday_save float declare @total_in_cash float declare @total_in_credit float declare @total_out float declare @total_check float -- 支票 if not exists(select 1 from gqm03 where gqc00c = @compid and gqc01d = @ddate) begin -- 没有当天的记录,需要插入一条 exec upg_getdata_for_prepare_day_account @compid, @ddate, @lastremain output, @today_save output, @lastday_save output, @total_in_cash output, @total_in_credit output, @total_out output, @total_check output insert gqm03(gqc00c, gqc01d, gqc02f, gqc03f, gqc05f, gqc06f, gqc10f, gqc17f, gqc08f) values(@compid, @ddate, @lastremain, @today_save, @lastday_save, @total_in_credit, @total_out, @total_in_cash, @total_check) end else begin --已经存在一条记录了 if exists(select 1 from gqm03 where gqc00c = @compid and gqc01d = @ddate and isnull(gqc15d, '')<>'') begin --已经审核,不允许重新计算 return end --则需要更新 if @calflag = 1 begin exec upg_getdata_for_prepare_day_account @compid, @ddate, @lastremain output, @today_save output, @lastday_save output, @total_in_cash output, @total_in_credit output, @total_out output, @total_check output update gqm03 set gqc02f = @lastremain, gqc03f = @today_save, gqc05f = @lastday_save, gqc06f = @total_in_credit, gqc10f = @total_out, gqc17f = @total_in_cash, gqc08f = @total_check where gqc00c = @compid and gqc01d = @ddate end end --重新计算总交款 update gqm03 set gqc12f = isnull(gqc03f, 0) + isnull(gqc05f, 0) + isnull(gqc06f, 0) + isnull(gqc08f, 0) where gqc00c = @compid and gqc01d = @ddate --重新计算剩余现金 update gqm03 set gqc13f = isnull(gqc02f, 0)-isnull(gqc03f, 0)-isnull(gqc05f, 0)-isnull(gqc10f, 0) + isnull(gqc17f, 0) where gqc00c = @compid and gqc01d = @ddate end GO /****** Object: StoredProcedure [dbo].[upg_recover_gsm23] Script Date: 06/12/2017 06:20:33 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_recover_gsm23] as begin -- 判断是否已做数据恢复 if exists (select 1 from data_recover where rdd00c = '001' and rdd01c = 'gsm23' and rdd02c = 'recover') begin return end exec upg_initialize_gsm23 insert into data_recover(rdd00c, rdd01c, rdd02c) values ('001', 'gsm23', 'recover') end GO /****** Object: StoredProcedure [dbo].[upg_create_ggm01_for_stat_scope] Script Date: 06/12/2017 06:19:53 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_create_ggm01_for_stat_scope] ( @compid varchar(10), -- 公司别 @fromdate varchar(20), -- 开始日期 @todate varchar(20) ) as begin declare @billid varchar(20) declare @sp031f float select @sp031f = gsa03f from gsm01 where gsa01c = 'SP031' and gsa00c = @compid if isnull(@sp031f, 0) = 2 begin -- 统一出院结账 declare cur_each_ggm01 cursor for select gla01c from glm01 where gla00c = @compid and gla80d >= @fromdate and gla80d <= @todate end else begin -- 收银时候结账 declare cur_each_ggm01 cursor for select gga01c from ggm01 where gga00c = @compid and gga80d >= @fromdate and gga80d <= @todate end open cur_each_ggm01 fetch cur_each_ggm01 into @billid while @@fetch_status = 0 begin if isnull(@sp031f, 0) = 2 begin exec upg_create_ggm01_for_stat 'gl', @compid, @billid end else begin exec upg_create_ggm01_for_stat 'gx', @compid, @billid end fetch cur_each_ggm01 into @billid end close cur_each_ggm01 deallocate cur_each_ggm01 end GO /****** Object: StoredProcedure [dbo].[upg_analyzer_customer_ex_GLZX] Script Date: 06/12/2017 06:19:27 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_analyzer_customer_ex_GLZX] ( @compid varchar(10), @class varchar(10), @frommemid varchar(20), -- 会员开始编号 @tomemid varchar(20), -- 会员截至编号 @memfromdate varchar(10), -- 资料输入日期 @memtodate varchar(10), -- 资料输入日期 @fromdate varchar(20), -- 消费开始日期 @todate varchar(20), -- 消费截至日期 @state varchar(200), -- 会员卡状态 @acon_amt float, -- 大于消费金额 @alastdate varchar(20), -- 某个日期之后 @at_times float, -- 大于消费次数 @at_history float, -- 会员历史大于年限 @aav_amt float, -- 大于平均消费金额 @aless_remain float, -- 小于某个储值余额 @abig_remain float, -- 大于某个储值余额 @addtoSMS int, @staitem int, -- 统计类型--2006/9/15 by sdw @only_card_pay int ) as begin declare @incompid varchar(10) select @incompid = @compid if(@alastdate <> '') set @alastdate = replace(@alastdate,'-','') declare @sp031f float select @sp031f = gsa03f from gsm01 with(nolock) where gsa00c = @compid and gsa01c = 'SP031' declare @payment_0 varchar(10) declare @payment_3 varchar(10) declare @payment_$ varchar(10) declare @payment_u1 varchar(10), @payment_u2 varchar(10), @payment_u3 varchar(10), @payment_u4 varchar(10), @payment_u5 varchar(10) exec upg_isrevenue_payment @compid, @payment_0 output, @payment_3 output, @payment_$ output, @payment_u1 output, @payment_u2 output, @payment_u3 output, @payment_u4 output, @payment_u5 output declare @compcode varchar(10) if @compid = '001' select @compcode = '%' else select @compcode = @compid if @fromdate<'19000101' select @fromdate = '19000101' if @todate>'20901231' select @todate = '20901231' -- 以卡的角度来统计会员 create table #card_member ( compid varchar(20) null, memid varchar(20) not null, m_name varchar(40) null, cardid varchar(20) not null, gender int null, mobile varchar(50) null, tel varchar(50) null, birthday varchar(8) null, start_date varchar(10) null, memaddr varchar(160) null, postcode varchar(20) null, principle varchar(20) null, cardclass varchar(20) null, -- primary key(cardid, memid) ) create table #mem_result( idd int identity not null, compid varchar(20) null, memid varchar(20) not null, -- Member编号 m_name varchar(40) null, -- 姓名 cardid varchar(20) null, -- 卡号 gender int null, -- 性别 0:女 mobile varchar(20) null, -- 手机 tel varchar(20) null, -- 电话 birthday varchar(8) null, state varchar(10) null, -- Member卡状态 con_amt float null, -- Consumption金额 lastdate varchar(8) null, -- 最后Consumption日期 lastcall varchar(8) null, -- 最后回访日期 t_times float null, -- 用于比较的Consumption次数 t_compare float null, -- 总共来的次数 av_ytimes float null, -- 每年来的次数 av_times float null, -- 每月来的次数 av_wtimes float null, -- 每周来的次数 months float null, -- 客人来了多少个月了,客龄 av_amt float null, -- 每次Consumption的平均金额 start_date varchar(20) null, -- 开始日期 first_date varchar(20) null, -- 第一次来店日期 comp_date varchar(20) null, --开始比较的日期 dur_years float null, -- 统计期间来了多少个年了 dur_months float null, -- 统计期间来了多少个月了 dur_weeks float null, -- 统计期间来了多少个周了 remain01 float null, -- 余额 remain02 float null, -- 余额 remain03 float null, -- 余额 remain04 float null, -- 余额 remain05 float null, -- 余额 total_fill_amt float null, -- 累计Charge金额 memaddr varchar(160) null, -- 会员地址 postcode varchar(20) null, -- 邮编 principle varchar(20) null, -- 负责人 total_salecard_amt float null, -- 累计卡销售金额 primary key(idd)) create table #sum_tmp( vid varchar(20) not null, vsum float null, v_1 varchar(20) null, f_1 float null, v_2 varchar(20) null, primary key(vid)) --会员初始化 insert #card_member(compid, memid, m_name, cardid, gender, mobile, tel, birthday, start_date, memaddr, postcode, principle, cardclass) select distinct isnull(gca13d, gca00c), isnull(gca04c, ''), gba03c, gca01c, gba14i, gba08c, gba07c, gba17d, isnull(gba02c, '20100101'), gba05c, gba12c, gba10c, gca02c from gcm01 a with (nolock), gam26 z with (nolock), gbm01 b with (nolock) where a.gca00c = a.gca13d and substring(a.gca01c, 1, 2)<>'散客' and a.gca00c = z.gaz02c and z.gaz01c = @incompid and b.gba00c = z.gaz02c and (b.gba02c between @memfromdate and @memtodate or @memfromdate = '*' or @memtodate = '*') and a.gca04c = b.gba01c update a set a.m_name = '未知' from #card_member a where isnull(m_name, '') = '' if (@class = '' or @class = '*') begin if @frommemid = '' or @frommemid = '*' begin insert #mem_result(compid, memid, m_name, cardid, gender, mobile, tel, birthday, start_date, memaddr, postcode, principle) select compid, memid, m_name, cardid, gender, mobile, tel, birthday, start_date, memaddr, postcode, principle from #card_member a end else begin insert #mem_result(compid, memid, m_name, cardid, gender, mobile, tel, birthday, start_date, memaddr, postcode, principle) select compid, memid, m_name, cardid, gender, mobile, tel, birthday, start_date, memaddr, postcode, principle from #card_member a where a.memid >= @frommemid and a.memid <= @tomemid end end else begin if @frommemid = '' or @frommemid = '*' begin insert #mem_result(compid, memid, m_name, cardid, gender, mobile, tel, birthday, start_date, memaddr, postcode, principle) select compid, memid, m_name, cardid, gender, mobile, tel, birthday, start_date, memaddr, postcode, principle from #card_member a where a.cardclass = @class end else begin insert #mem_result(compid, memid, m_name, cardid, gender, mobile, tel, birthday, start_date, memaddr, postcode, principle) select compid, memid, m_name, cardid, gender, mobile, tel, birthday, start_date, memaddr, postcode, principle from #card_member a where a.cardclass = @class and a.memid >= @frommemid and a.memid <= @tomemid end end drop table #card_member --去掉散客 delete #mem_result where substring(memid, 1, 2) = '散客' --删除没有卡号的会员资料 delete #mem_result where isnull(cardid, '') = '' --消费总金额, 最后消费日期, 消费次数 exec upg_stat_card_consume @incompid, @fromdate, @todate, @frommemid, @tomemid, @only_card_pay --统计每个会员消费的次数 update a set a.con_amt = b.vsum, a.lastdate = b.v_1, a.t_times = b.f_1, a.first_date = b.v_2 from #mem_result a, #sum_tmp b where a.memid = b.vid --最后回访日期 delete #sum_tmp --2006/9/15 by sdw, 新增,决定用来计算的开始时间,开始时间小于第一次消费时,则以第一次消费为准,否则,反之 update a set a.comp_date = a.first_date from #mem_result a where a.first_date>@fromdate if @frommemid = '' or @frommemid = '*' begin insert #sum_tmp(vid, v_1) select gbf01c, max(gbf04c) from gbm06 with(nolock), gam26 z with(nolock) where gbf00c = z.gaz02c and z.gaz01c = @incompid and isnull(gbf01c, '')<>'' group by gbf01c end else begin insert #sum_tmp(vid, v_1) select gbf01c, max(gbf04c) from gbm06 with(nolock), gam26 z with(nolock) where gbf00c = z.gaz02c and z.gaz01c = @incompid and gbf01c >= @frommemid and gbf01c <= @tomemid and isnull(gbf01c, '')<>'' group by gbf01c end update a set a.lastcall = b.v_1 from #mem_result a, #sum_tmp b where a.memid = b.vid --每次消费的平均金额 update a set a.av_amt = isnull(a.con_amt, 0)/isnull(a.t_times, 1) from #mem_result a where isnull(a.t_times, 0) <> 0 --更新余额 create table #gcm03_tmp( cardid varchar(20) not null, gcc03i int not null, --帐号类别(1-电子钱包, 2-储值帐号, 3-积分, 4-储值账户二, 5-储值账户二) gcc06f float null --馀额 ) create clustered index idx_gcm03_tmp on #gcm03_tmp(cardid, gcc03i) insert #gcm03_tmp(cardid, gcc03i, gcc06f) select gca01c, gcc03i, gcc06f from #mem_result a, gcm03 b with (nolock), gcm01 c with (nolock) where a.cardid = b.gcc01c and b.gcc00c = c.gca00c and b.gcc01c = c.gca01c and c.gca00c = c.gca13d update a set a.remain01 = b.gcc06f from #mem_result a, #gcm03_tmp b where a.cardid = b.cardid and b.gcc03i = 1 update a set a.remain02 = b.gcc06f from #mem_result a, #gcm03_tmp b where a.cardid = b.cardid and b.gcc03i = 2 update a set a.remain03 = b.gcc06f from #mem_result a, #gcm03_tmp b where a.cardid = b.cardid and b.gcc03i = 3 update a set a.remain04 = b.gcc06f from #mem_result a, #gcm03_tmp b where a.cardid = b.cardid and b.gcc03i = 4 update a set a.remain05 = b.gcc06f from #mem_result a, #gcm03_tmp b where a.cardid = b.cardid and b.gcc03i = 5 drop table #gcm03_tmp --客人来了多少个月了, 从来店那天开始 ----2006/9/15 by sdw, 新增,更新统计期间总共有多少个年/日 --处理一下可能的日期字段错误 update #mem_result set start_date = '20010101' where isnull(start_date, '') = '' update #mem_result set comp_date = '20010101' where isnull(comp_date, '') = '' update #mem_result set first_date = '20010101' where isnull(first_date, '') = '' update #mem_result set lastdate = '20010101' where isnull(lastdate, '') = '' --update a --set a.months = isnull(datediff(month, convert(datetime, substring(a.start_date, 1, 8)), getdate()), 0) + 1, -- a.dur_years = isnull(datediff(year, convert(datetime, substring(a.comp_date, 1, 8)), convert(datetime, @todate)), 0) + 1, -- a.dur_months = isnull(datediff(month, convert(datetime, substring(a.comp_date, 1, 8)), convert(datetime, @todate)), 0) + 1, -- a.dur_weeks = isnull(datediff(week, convert(datetime, substring(a.comp_date, 1, 8)), convert(datetime, @todate)), 0) + 1 -- from #mem_result a --平均每月多少次 ----2006/9/15 by sdw, 新增,更新统计期间的年/周平均次数 update a set a.av_ytimes = a.t_times/a.dur_years, a.av_times = a.t_times/a.dur_months, a.av_wtimes = a.t_times/a.dur_weeks from #mem_result a ----2006/9/15 by sdw, 新增,根据统计分类的不同,来决定参与比较的是年平均次数,还是月/日平均次数 if (@staitem = 2) begin update a set a.t_compare = a.av_ytimes from #mem_result a end else if (@staitem = 1) begin update a set a.t_compare = a.av_times from #mem_result a end else if (@staitem = 0) begin update a set a.t_compare = a.av_wtimes from #mem_result a end else if (@staitem = 3) begin update a set a.t_compare = a.t_times from #mem_result a end --统计会员的累计充值金额 --是从gcm04中统计的,如果gcm04中不准,则统计可能错误。与会员卡销售的统计的方面不同 --会员卡销售是从原始的gcm10中直接统计的 delete #sum_tmp declare @custverflag varchar(20) select @custverflag = gsa02c from gsm01 with (nolock) where gsa00c = @incompid and gsa01c = 'SP109' insert #sum_tmp(vid, vsum) select gcd01c, sum(gcd06f) from gcm04 a with (nolock), gam26 z with(nolock) -- #mem_result b add by lm 2015.11.17 where (gcd02i<>1 and gcd02i<>3) and (gcd05i = 0 or gcd05i = 6) and a.gcd00c = z.gaz02c and z.gaz01c = @incompid and a.gcd09d >= @fromdate and a.gcd09d <= @todate -- and a.gcd01c = b.cardid group by gcd01c update a set a.total_fill_amt = isnull(b.vsum, 0) from #mem_result a, #sum_tmp b where a.cardid = b.vid delete #sum_tmp insert #sum_tmp(vid, vsum) select gcd01c, sum(gcd06f) from gcm04 a with (nolock), gam26 z with(nolock) --, #mem_result b add by lm 2015.11.17 where (gcd02i<>1 and gcd02i<>3) and (gcd05i = 1 or gcd05i = 5) and a.gcd00c = z.gaz02c and z.gaz01c = @incompid -- and a.gcd01c = b.cardid and a.gcd09d >= @fromdate and a.gcd09d <= @todate and (a.gcd07c <> 'gx' and a.gcd07c <> 'gl') group by gcd01c update a set a.total_fill_amt = isnull(a.total_fill_amt, 0) - isnull(b.vsum, 0) from #mem_result a, #sum_tmp b where a.cardid = b.vid delete #sum_tmp -- 累计消费金额的计算完成 --计算累计的卡销售金额,因为卡销售金额可能不同于储值金额 --Added by WUXM, 20081005 --因为卡在统计累计充值的时候是考虑到所有账户的,所以统计销售的时候也要考虑疗程的销售 --从gsm03取比较合适 create table #tbl_gsm03( cardid varchar(20) not null, pay varchar(10) not null, ddate varchar(20) null, compid varchar(10) not null, amt float null) create clustered index idx_gsm03_01 on #tbl_gsm03(cardid) insert #tbl_gsm03(cardid, compid, pay, ddate, amt) select isnull(gna13c, ''), gsc00c, gsc04c, isnull(gsc10d, ''), gsc05f from gnm01 a with (nolock), gsm03 b with (nolock), gam26 z with(nolock) where a.gna00c = b.gsc00c and a.gna01c = b.gsc01c and b.gsc02c = 'gn' and a.gna00c = z.gaz02c and z.gaz01c = @compid and a.gna02d >= @fromdate and a.gna02d <= @todate insert #tbl_gsm03(cardid, compid, pay, ddate, amt) select isnull(gcl03c, ''), gsc00c, gsc04c, isnull(gsc10d, ''), gsc05f from gcm10 a with (nolock), gsm03 b with (nolock), gam26 z with(nolock) where a.gcl00c = b.gsc00c and a.gcl01c = b.gsc01c and b.gsc02c = 'gz' and a.gcl00c = z.gaz02c and z.gaz01c = @compid and a.gcl02d >= @fromdate and a.gcl02d <= @todate --start add by lm 2016.03.01 insert #tbl_gsm03(cardid, compid, pay, ddate, amt) select isnull(gea07c, ''), gsc00c, gsc04c, isnull(gsc10d, ''), gsc05f from gem01 a with (nolock), gsm03 b with (nolock), gam26 z with(nolock) where a.gea00c = b.gsc00c and a.gea01c = b.gsc01c and b.gsc02c = 'ge' and a.gea00c = z.gaz02c and z.gaz01c = @compid and a.gea03d >= @fromdate and a.gea03d <= @todate and isnull(b.gsc10d,'') <> '' -- add lucz and a.gea02i = 4 --end insert #sum_tmp(vid, vsum) select a.cardid, sum(amt) from #tbl_gsm03 a with (nolock), #mem_result b where a.cardid = b.cardid and a.pay in (select pay from pay_classify where comp = @compid and category = '1') -- modify 2015-08-07 by lm 支付方式判断改为支付方式大类判断。 group by a.cardid drop table #tbl_gsm03 update a set a.total_salecard_amt = isnull(b.vsum, 0) from #mem_result a, #sum_tmp b where a.cardid = b.vid --更新卡的状态 --Added by Mzh 2006/03/23 update a set a.state = cast(gca08i as varchar(10)) from #mem_result a, gcm01 b with(nolock) where gca04c = memid and gca00c = gca13d if @addtoSMS = 1 begin declare @cur_max float select @cur_max = max(gxa01f) from gxm01 with(nolock) where gxa00c = @compid select @cur_max = isnull(@cur_max, 0) insert gxm01(gxa00c, gxa01f, gxa02c, gxa03c) select @compid, idd = @cur_max + idd, memid, mobile from #mem_result where memid not in (select gxa02c from gxm01 where gxa00c = @compid) and isnull(memid, '')<>'' and isnull(con_amt, 0) >= @acon_amt and isnull(lastdate, '00000000') >= @alastdate and isnull(t_times, 0) >= @at_times and isnull(av_amt, 0) >= @aav_amt end if (@state = '' or @state = '*') begin declare @count int select @count = count(1) from #mem_result select idd, compid, memid, m_name, cardid, gender, mobile, tel, birthday, state, con_amt, lastdate, lastcall, t_times, t_compare, av_ytimes, av_times, av_wtimes, months, av_amt, start_date, first_date, comp_date, dur_years, dur_months, dur_weeks, remain01, remain02, remain03, remain04, remain05, total_fill_amt, memaddr, postcode, principle, total_salecard_amt from #mem_result where isnull(con_amt, 0) >= @acon_amt and isnull(lastdate, '19740313') >= @alastdate and isnull(t_compare, 0) >= @at_times --2006/9/15 by sdw, 修改,比较次数 and isnull(av_amt, 0) >= @aav_amt and isnull(con_amt, 0) >= @acon_amt and isnull(months, 0) >= isnull(@at_history, 0)*12 --2006/9/15 by sdw, 新增,会员历史条件 and isnull(remain02, 0) + isnull(remain04, 0) + isnull(remain05, 0) >= @abig_remain and isnull(remain02, 0) + isnull(remain04, 0) + isnull(remain05, 0) <= @aless_remain order by con_amt desc end else begin create table #tmp ( s varchar(10) ) declare @long_str_Tmp varchar(10), @short_str varchar(10), @split_str_length int, @split_str varchar(10), @Long_str varchar(200) set @split_str = ',' set @Long_str = @state SET @split_str_length = LEN(@split_str) IF CHARINDEX(@split_str, @Long_str) = 1 SET @long_str_Tmp = SUBSTRING(@Long_str, @split_str_length + 1, LEN(@Long_str)-@split_str_length) ELSE SET @long_str_Tmp = @Long_str IF CHARINDEX(REVERSE(@split_str), REVERSE(@long_str_Tmp))>1 SET @long_str_Tmp = @long_str_Tmp + @split_str ELSE SET @long_str_Tmp = @long_str_Tmp IF CHARINDEX(@split_str, @long_str_Tmp) = 0 insert into #tmp select @long_str_Tmp ELSE begin WHILE CHARINDEX(@split_str, @long_str_Tmp)>0 begin SET @short_str = SUBSTRING(@long_str_Tmp, 1, CHARINDEX(@split_str, @long_str_Tmp)-1) declare @long_str_Tmp_LEN INT, @split_str_Position_end int SET @long_str_Tmp_LEN = LEN(@long_str_Tmp) SET @split_str_Position_end = LEN(@short_str) + @split_str_length SET @long_str_Tmp = REVERSE(SUBSTRING(REVERSE(@long_str_Tmp), 1, @long_str_Tmp_LEN-@split_str_Position_end)) IF @short_str<>'' insert into #tmp select @short_str end end select idd, compid, memid, m_name, cardid, gender, mobile, tel, birthday, state, con_amt, lastdate, lastcall, t_times, t_compare, av_ytimes, av_times, av_wtimes, months, av_amt, start_date, first_date, comp_date, dur_years, dur_months, dur_weeks, remain01, remain02, remain03, remain04, remain05, total_fill_amt, memaddr, postcode, principle, total_salecard_amt from #mem_result where isnull(con_amt, 0) >= @acon_amt and isnull(lastdate, '19740313') >= @alastdate and isnull(t_compare, 0) >= @at_times --2006/9/15 by sdw, 修改,比较次数 and isnull(av_amt, 0) >= @aav_amt and isnull(con_amt, 0) >= @acon_amt and isnull(months, 0) >= isnull(@at_history, 0)*12 --2006/9/15 by sdw, 新增,会员历史条件 and isnull(remain02, 0) + isnull(remain04, 0) + isnull(remain05, 0) >= @abig_remain and isnull(remain02, 0) + isnull(remain04, 0) + isnull(remain05, 0) <= @aless_remain and state in (select s from #tmp) order by con_amt desc drop table #tmp end drop table #mem_result drop table #sum_tmp end GO /****** Object: StoredProcedure [dbo].[upg_purse_deduct_extends_sale] Script Date: 06/12/2017 06:20:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_purse_deduct_extends_sale] ( @compid varchar(10), -- 公司编号 @cardno varchar(20), -- 会员卡号 @date varchar(10), -- 异动日期 @accttype int, -- 账户类型(1-电子钱包 2-储值账户) @consume float, -- 支付金额 @changway int, -- 异动方向(0-充值, 1-取款, 2-消费, 3-转入, 4-转出, 5-欠款 6-欠款返回 7-赠送) @billtype varchar(5), -- 单据类别 @billno varchar(20), -- 单据号 @confirmflag int, -- 是否复核的标记(1-复核 0-取消复核) @jine float -- 套餐金额 ) as begin set nocount on declare @gcm04_flowno float declare @lastremain float declare @billmemo varchar(160) -- 单据的备注 declare @tcjine float --套餐欠款的金额 -- 当前的事务数 declare @__transaction_count int set @__transaction_count = @@trancount -- try...catch处理 begin try -- a.开启事务,或者设置事务保存点 if @__transaction_count = 0 begin tran else save tran __tran_upg_purse_deduct --支付金额大于套餐金额 if(@consume > @jine) select @tcjine = @jine -- 套餐金额 else select @tcjine = @consume -- 支付金额 if @confirmflag = 1 --复核 begin update gcm03 set gcc10f = isnull(gcc10f,0) - @consume, gcc06f = isnull(gcc06f,0) + @consume where gcc00c = @compid and gcc01c = @cardno and gcc03i = 2 if(exists(select 1 from gcm03 with (nolock) where gcc00c = @compid And gcc01c = @cardno and gcc03i = 889)) begin update gcm03 set gcc06f = gcc06f - @tcjine, gcc10f = gcc10f + @tcjine where gcc00c = @compid and gcc01c = @cardno and gcc03i = 889 end else begin declare @intRow int select @intRow = count(*) + 1 from gcm03 with (nolock) where gcc00c = @compid And gcc01c = @cardno insert into gcm03(gcc00c,gcc01c,gcc02f,gcc03i,gcc06f,gcc07f,gcc08c,gcc09d,gcc10f,gcc11c) values(@compid,@cardno,@intRow,889,-@tcjine,0,@date,'',@tcjine,'') end if(@consume > @jine) --支付金额大于套餐金额 begin if(exists(select 1 from gcm03 with (nolock) where gcc00c = @compid And gcc01c = @cardno and gcc03i = 2)) begin update gcm03 set gcc10f = isnull(gcc10f,0) +(@consume - @jine), gcc06f = isnull(gcc06f,0) -(@consume - @jine) where gcc00c = @compid and gcc01c = @cardno and gcc03i = 2 end else begin insert into gcm03(gcc00c,gcc01c,gcc02f,gcc03i,gcc06f,gcc07f,gcc08c,gcc09d,gcc10f,gcc11c) values(@compid,@cardno,@intRow,889,@consume - @jine,0,@date,'',@consume - @jine,'') end end --获取操作日期 declare @curdate varchar(20), @curtime varchar(20) exec upg_getcurrdate @curdate output, @curtime output exec upg_get_gcm04_flowno @cardno, 889, @gcm04_flowno output declare @sp0403f float declare @isinputout_raw_bill int select @sp0403f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP040' if @sp0403f = 2 or @sp0403f = 3 begin select @isinputout_raw_bill = 0 end else begin select @isinputout_raw_bill = 1 end insert gcm04(gcd00c, gcd01c, gcd02i, gcd04f, gcd05i, gcd06f, gcd07c, gcd08c, gcd09d, gcd10f, gcd11c, gcd12i, gcd13t, gcd14c) values(@compid, @cardno, 889, @gcm04_flowno, @changway, @tcjine, @billtype, @billno, @date, 0, @curdate, @isinputout_raw_bill, @curtime, @billmemo) update gcm04 set gcd06f = gcd06f - @tcjine where gcd02i = 2 and gcd00c = @compid and gcd01c = @cardno and gcd07c = 'gz' end else --取消复核 begin update gcm03 set gcc10f = isnull(gcc10f,0) + @consume, gcc06f = isnull(gcc06f,0) - @consume where gcc00c = @compid and gcc01c = @cardno and gcc03i = 2 update gcm03 set gcc06f = gcc06f + @tcjine, gcc10f = gcc10f - @tcjine where gcc00c = @compid and gcc01c = @cardno and gcc03i = 889 delete from gcm03 where gcc00c = @compid and gcc01c = @cardno and gcc03i = 889 and gcc06f = 0 and gcc10f = 0 if(@consume > @jine) --支付金额大于套餐金额 begin update gcm03 set gcc10f = gcc10f -(@consume - @jine), gcc06f = gcc06f +(@consume - @jine) where gcc00c = @compid and gcc01c = @cardno and gcc03i = 2 end if exists(select 1 from gcm04 with (nolock) where gcd00c = @compid and gcd01c = @cardno and gcd02i = @accttype and gcd05i = @changway and gcd07c = @billtype and gcd08c = @billno) begin delete from gcm04 where gcd00c = @compid and gcd01c = @cardno and gcd02i = @accttype and gcd05i = @changway and gcd07c = @billtype and gcd08c = @billno end end -- 提交事务 -- 有可提交的事务, 并且事务是在当前模块中开启的情况下, 才提交事务 if xact_state() = 1 and @__transaction_count = 0 commit -- 为了防止TRY 中有遗漏的事务处理, 可以在TRY 模拟的结束部分做最终的判断 if @__transaction_count = 0 begin if xact_state() = -1 rollback tran else begin while @@trancount > 0 commit tran end end end try begin catch -- 在CATCH 模块, 应该首先处理事务 if xact_state() <> 0 begin if @__transaction_count = 0 rollback tran -- xact_state 为-1 时, 不能回滚到事务保存点, 这种情况留给外层调用者做统一的事务回滚 -- 通过@@trancount > @__transaction_count 的判断, 即使在TRY 模块中没有设置事务保存点的情况下跳到此步骤, 也不会出错 else if xact_state() = 1 and @@trancount > @__transaction_count rollback tran __tran_upg_purse_deduct end return -1 end catch end GO /****** Object: StoredProcedure [dbo].[upg_day_total_detail] Script Date: 06/12/2017 06:19:56 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_day_total_detail] ( @compid varchar(10), @fromdate varchar(20), @todate varchar(20), @staff varchar(20) ) as begin declare @sp031f float select @sp031f = gsa03f from gsm01 with (nolock) where gsa00c = @compid and gsa01c = 'SP031' declare @payment_0 varchar(10) declare @payment_3 varchar(10) declare @payment_$ varchar(10) declare @payment_u1 varchar(10), @payment_u2 varchar(10), @payment_u3 varchar(10), @payment_u4 varchar(10), @payment_u5 varchar(10) exec upg_isrevenue_payment @compid, @payment_0 output, @payment_3 output, @payment_$ output, @payment_u1 output, @payment_u2 output, @payment_u3 output, @payment_u4 output, @payment_u5 output if @staff = '*' or @staff = '' begin select @staff = '%' end create table #tbl_item_group ( idd int identity not null, -- 序号 flag int not null, -- 标志 1-项目 2-会员卡 3-产品 prjtype varchar(20) null, -- 项目类别,产品类别 itemid varchar(20) null, -- 项目代码 产品代码 itemname varchar(60) null, -- 名称 quan float null, -- 数量 amt float null, -- 金额 primary key(idd) ) create table #tbl_fill_card ( cardtype varchar(20) null, --卡类别 cardid varchar(20) not null, -- 卡号 amt float null, quan float null, primary key(cardid) ) create table #tbl_fill_card_detail ( cardid varchar(20) not null, -- 卡号 amt float null, quan float null ) -- 每个项目的实际金额 if isnull(@sp031f, 0) = 2 begin -- 统一出院结账 insert #tbl_item_group(flag, itemid, quan, amt) select 1, ggb03c, sum(ggb05f), sum(ggb11f) from ggm02 b with (nolock), ggm01 a with (nolock), glm01 c with (nolock), gam26 z where b.ggb00c = a.gga00c and b.ggb01c = a.gga01c and a.gga00c = z.gaz02c and z.gaz01c = @compid and c.gla80d >= @fromdate and c.gla80d <= @todate and c.gla95c like @staff and a.gga00c = c.gla00c and a.gga30c = c.gla01c group by ggb03c -- 产品的统计 insert #tbl_item_group(flag, itemid, quan, amt) select 3, ggc03c, sum(ggc05f), sum(ggc11f) from ggm03 b with (nolock), ggm01 a with (nolock), glm01 c with (nolock), gam26 z where b.ggc00c = a.gga00c and b.ggc01c = a.gga01c and a.gga00c = z.gaz02c and z.gaz01c = @compid and c.gla80d >= @fromdate and c.gla80d <= @todate and c.gla95c like @staff and a.gga00c = c.gla00c and a.gga30c = c.gla01c group by ggc03c end else begin -- 收银结账 insert #tbl_item_group(flag, itemid, quan, amt) select 1, ggb03c, sum(ggb05f), sum(ggb11f) from ggm02 b with (nolock), ggm01 a with (nolock), gam26 z where b.ggb00c = a.gga00c and b.ggb01c = a.gga01c and a.gga00c = z.gaz02c and z.gaz01c = @compid and a.gga80d >= @fromdate and a.gga80d <= @todate and a.gga93c like @staff and isnull(gga80d, '') <> '' group by ggb03c -- 产品的统计 insert #tbl_item_group(flag, itemid, quan, amt) select 3, ggc03c, sum(ggc05f), sum(ggc11f) from ggm03 b with (nolock), ggm01 a with (nolock), gam26 z where b.ggc00c = a.gga00c and b.ggc01c = a.gga01c and a.gga00c = z.gaz02c and z.gaz01c = @compid and a.gga80d >= @fromdate and a.gga80d <= @todate and a.gga93c like @staff and isnull(gga80d, '') <> '' group by ggc03c end update a set a.prjtype = b.gda13c from #tbl_item_group a, gdm01 b where a.itemid = b.gda01c and a.flag = 1 and b.gda00c = @compid update a set a.prjtype = b.gfa24c from #tbl_item_group a, gfm01 b where a.itemid = b.gfa01c and a.flag = 3 and b.gfa00c = @compid -- 卡的统计, 一段时间内卡的销售, 包含所有的支付方式 insert #tbl_item_group(flag, itemid, quan, amt) select 2, gnb03c, 1, sum(gsc05f) from gnm02 b with (nolock), gnm01 a with (nolock), gam26 z,gsm03 c where a.gna00c = b.gnb00c and a.gna01c = b.gnb01c and a.gna00c = c.gsc00c and a.gna01c = c.gsc01c and c.gsc02c = 'gn' and a.gna80d >= @fromdate and a.gna80d <= @todate and a.gna00c = z.gaz02c and z.gaz01c = @compid and a.gna93c like @staff group by gnb03c -- 这段时间内的冲值 insert #tbl_fill_card_detail(cardid, amt, quan) select gcl03c, sum(gsc05f), count(gcl03c) from gcm10 a with (nolock), gam26 z ,gsm03 b where a.gcl00c = z.gaz02c and a.gcl00c = b.gsc00c and a.gcl01c = b.gsc01c and b.gsc02c = 'gz' and z.gaz01c = @compid and a.gcl80d >= @fromdate and a.gcl80d <= @todate and gcl93c like @staff and (gcl06i = 0 or gcl06i = 6) and isnull(gcl03c, '') <> '' group by gcl03c -- add by lyj 把会员卡变更的金额也应该给加进去 -- 因为明细分类帐里面有一个记录 insert #tbl_fill_card_detail(cardid, amt, quan) select gea07c, sum(gsc05f), 1 from gem01 a with (nolock), gsm03 b with(nolock), gam26 z where a.gea00c = z.gaz02c and z.gaz01c = @compid and gea80d between @fromdate and @todate and gsc00c = gea00c and gea01c = gsc01c and gsc02c = 'ge' --and gsc04c in ('1', '6', 'ZFB', 'WX', @payment_0, @payment_3, @payment_$, @payment_u1, @payment_u2, @payment_u3, @payment_u4, @payment_u5) and gea21c like @staff group by gea07c insert #tbl_fill_card(cardtype,cardid, amt, quan) select gca02c,cardid, sum(amt), sum(quan) from #tbl_fill_card_detail a,gcm01 b with(nolock) where a.cardid = b.gca01c group by gca02c,cardid drop table #tbl_fill_card_detail --更新卡类别 flag =2 代表的是卖卡的单子 update a set a.prjtype = b.gnb02c from #tbl_item_group a, gnm02 b where a.itemid = b.gnb03c and a.flag = 2 and b.gnb00c = @compid update a set a.amt = a.amt + isnull(b.amt, 0) from #tbl_item_group a, #tbl_fill_card b where a.itemid = b.cardid and a.prjtype = b.cardtype and a.flag = 2 -- 有些卡并没有卡销售,也应该加入 delete a from #tbl_fill_card a, #tbl_item_group b where a.cardid = b.itemid and a.cardtype = b.prjtype and b.flag = 2 insert #tbl_item_group(flag, itemid, quan, amt) select 5, cardid, quan, amt from #tbl_fill_card delete #tbl_fill_card -- insert #tbl_fill_card(cardid, amt, quan) -- select gcl03c, sum(gcl07f), 0 -- from gcm10 a with (nolock), gam26 z -- where gcl00c = z.gaz02c -- and z.gaz01c = @compid -- and a.gcl80d >= @fromdate -- and a.gcl80d <= @todate -- and gcl93c like @staff -- and (gcl06i = 1 or gcl06i = 5) -- and isnull(gcl03c, '') <> '' -- group by gcl03c -- -- update a -- set a.amt = a.amt - isnull(b.amt, 0) -- from #tbl_item_group a, #tbl_fill_card b -- where a.itemid = b.cardid -- and a.flag = 2 -- 有些卡并没有卡销售,也应该加入 delete a from #tbl_fill_card a, #tbl_item_group b where a.cardid = b.itemid and b.flag = 2 insert #tbl_item_group(flag, itemid, quan, amt) select 5, cardid, quan, 0-amt from #tbl_fill_card update a set a.prjtype = b.gca02c from #tbl_item_group a, gcm01 b where a.itemid = b.gca01c and b.gca00c = b.gca13d and a.flag = 5 update #tbl_item_group set flag = 2 where flag in (2,5) -- 前面是按会员卡来分组统计的 delete #tbl_fill_card insert #tbl_fill_card(cardid, quan, amt) select prjtype, count(prjtype), sum(amt) from #tbl_item_group where flag = 2 and isnull(prjtype, '') <> '' group by prjtype delete #tbl_item_group where flag = 2 -- 按卡的类别分分组统计 insert #tbl_item_group(flag, prjtype, itemid, quan, amt) select 2, 'card', cardid, quan, amt from #tbl_fill_card --新增疗程 lucz 2015.10.21 insert #tbl_item_group(flag, itemid, quan, amt) select 4,gch04c, sum(gch05f+gch06f) quan,sum(gch07f) amt from gcm08 where gch00c in (select gaz02c from gam26 where gaz01c = @compid) and gch10c in ('gn','gz') and gch02d between @fromdate and @todate group by gch04c --疗程消费(消耗) lucz 2015.10.21 insert #tbl_item_group(flag, itemid, quan, amt) select 5,gch04c, sum(gch08f) quan,sum(gch09f) amt from gcm08 where gch00c in (select gaz02c from gam26 where gaz01c = @compid) and gch10c = 'gx' and gch02d between @fromdate and @todate group by gch04c update a set a.itemname = b.gda03c from #tbl_item_group a, gdm01 b where a.itemid = b.gda01c and b.gda00c = @compid and a.flag in (1,4,5) --lucz 2015.10.22 update a set a.itemname = b.gfa03c from #tbl_item_group a, gfm01 b where a.itemid = b.gfa01c and b.gfa00c = @compid and a.flag = 3 update a set a.itemname = b.gak02c + '(含各种支付方式)' from #tbl_item_group a, gam10 b where a.itemid = b.gak01c and b.gak00c = @compid and a.flag = 2 select isnull(idd,'') idd, isnull(flag,'') flag, isnull(prjtype,'') prjtype, isnull(itemid,'') itemid, isnull(itemname,'') itemname, isnull(quan,'') quan, isnull(amt,'') amt from #tbl_item_group order by flag, prjtype, itemid drop table #tbl_item_group drop table #tbl_fill_card end GO /****** Object: StoredProcedure [dbo].[upg_cal_work_detail_by_empl_no_mobile] Script Date: 06/12/2017 06:19:35 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_cal_work_detail_by_empl_no_mobile] ( @spid int, @compid varchar(10), -- 公司别 @fromdate varchar(10), -- 开始日期 @todate varchar(10), -- 截至日期 @fromempl varchar(20), -- 查询开始人员 @toempl varchar(20), -- 查询截至人员 @return_type int ) as begin declare @staff_id varchar(20) declare @oper_amt float declare @t_vperf float declare @t_tperf float declare @total_comm float declare @quan2 float -- 数量 declare @consume_cost float -- 消耗成本的变量 declare @cls_flag int declare @f_oper_amt float declare @f_t_vperf float declare @f_t_tperf float declare @f_quan2 float declare @t_oper_amt float declare @t_t_vperf float declare @t_t_tperf float declare @t_quan2 float declare @total_f_quan float declare @total_cust_num float declare @fa_type nvarchar(20) -- 方案编号 --begin add by lm 2015-07-21 create table #work_detail_cash ( id int identity , -- 流水好 person_id varchar(20) not null, -- 员工代码 amt2 float null, --虚业绩 amt3 float null, --真实业绩 billid varchar(20) null, --单号 paycode varchar(20) null, --支付代码 spid int null, --连接号 ) create table #work_detail_billid ( id int identity , -- 流水好 billid varchar(20) null, --单号 spid int null, --连接号 ) create table #work_detail_bill_account ( id int identity , -- 流水好 person_id varchar(20) not null, -- 员工代码 amt2 float null, --虚业绩 amt3 float null, --真实业绩 billid varchar(20) null, --单号 spid int null, ) create table #person_cash ( person_id varchar(20) not null, total_cash float null ) --end -- 是否需要扣除员工领用的消耗成本 declare @is_reduce_consume_cost float select @is_reduce_consume_cost = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP063' -- 从项目类别统计中分组到按员工 delete workdetail_by_empl where spid = @spid -- 按店业绩分段计算按天来 declare @ppt03c nvarchar(50) -- Start 创建临时表 add by lm 2015-08-05 create table #personprecept ( id int identity(1,1) not null, ppt03c varchar(20) not null ) insert #personprecept(ppt03c) select ppt03c from personprecept with (nolock) where ppt00c = @compid and ppt02c in (select pre01c from precept with (nolock) where pre03i = 1 and pre00c = ppt00c and isnull(pre22i, 0) = 0) and ((ppt03c >= @fromempl and ppt03c <= @toempl) or @fromempl = '*') -- 创建循环变量 declare @HasRecord int declare @idd int set @HasRecord = 1 while(@HasRecord = 1) begin if exists(select 1 from #personprecept) begin set @HasRecord = 1 select top 1 @idd = id , @ppt03c = ppt03c from #personprecept if not exists(select 1 from workdetail_by_empl with(nolock) where person_id = @ppt03c and compid = @compid and spid = @spid) insert workdetail_by_empl(spid, person_id, name, compid) values(@spid, @ppt03c, 'N/A', @compid) -- 清除已处理的门店 delete #personprecept where id = @idd end else begin set @HasRecord = 0 end end drop table #personprecept --start add by lm 20160405 delete a from workdetail_by_empl a, workdetail_by_kind b where a.person_id = b.person_id and a.spid = b.spid and a.spid = @spid --end insert workdetail_by_empl(spid, compid, person_id, oper_amt, total_vperf, total_tperf, quan, comm, f_quan) select @spid, @compid, person_id, sum(oper_amt), sum(total_vperf), sum(total_tperf), sum(quan), sum(comm), sum(f_quan) from workdetail_by_kind with (nolock) where spid = @spid group by person_id -- Start 创建临时表 add by lm 2015-08-06 -- 依次计算每个员工的提成, 员工的卡,项目,物品合计在一起的业绩 create table #workdetail_by_empl ( id int not null, -- 流水好 person_id varchar(20) not null, -- 员工代码 oper_amt float null, -- 营业金额 total_vperf float null, -- 虚业绩 total_tperf float null, -- 实业绩 quan float null -- 数量 ) insert #workdetail_by_empl (id, person_id, oper_amt, total_vperf, total_tperf, quan ) select id, person_id, oper_amt, total_vperf, total_tperf, quan from workdetail_by_empl where spid = @spid -- 创建循环变量 set @HasRecord = 1 declare @currentCompId varchar(10) while(@HasRecord = 1) begin if exists(select 1 from #workdetail_by_empl) begin set @HasRecord = 1 select top 1 @idd = id, @staff_id = person_id, @oper_amt = oper_amt, @t_vperf = total_vperf, @t_tperf = total_tperf, @quan2 = quan from #workdetail_by_empl -- 判断是否这个员工是按员工合计来计算提成的 declare @commway float -- 提成方式 declare @projectway float -- 项目提成方式 declare @productway float -- 产品提成方式 declare @cardway float -- 卡提成方式 declare @pre06i float -- 店总业绩计算方式 1分段 2扣基数乘比率 declare @pre09i float -- 个人总业绩计算方式 1分段 2扣基数乘比率 declare @baseamt float -- 基数 declare @ratio float -- 比率 select @commway = pre03i, @projectway = pre12i, @productway = pre13i, @cardway = pre14i, @fa_type = pre01c, @pre06i = pre06i, @pre09i = pre09i, @baseamt = pre10f, @ratio = pre11f from precept with(nolock) where pre01c = (select top 1 ppt02c from personprecept where ppt03c = @staff_id and ppt00c = @compid and ppt04i = 1 order by ppt05d desc) and pre00c = @compid and isnull(pre22i, 0) = 0 select @total_comm = 0 -- 虚业绩现金求和 lucz 2016 declare @amt2_detail float select @amt2_detail = sum(isnull(amt2,0)) from #work_detail_ceshi where spid = @spid and compid = @compid and person_id = @staff_id and srvdate between @fromdate and @todate --and (paycode = '1' or paycode = '6' or paycode = 'ZFB' or paycode = 'WX') and paycode in (select pay from pay_classify where comp = @compid and category = '1') if @commway = 2 --if (@commway = 1 and @pre06i = 1 ) or (@commway = 2 and @pre09i = 1)--按员工总业绩分段/店总业绩分段 begin if @pre09i = 1--分段计算 begin -- 按一个员工所有总的业绩计算提成,不支持指定客数分段的概念 exec upg_cal_sum_comm @compid, @oper_amt, @t_vperf, @t_tperf, @quan2, @oper_amt, @t_vperf, @t_tperf, @quan2, 0, 0, 0, 0, @staff_id, 4, '', 0, 0, @total_comm output, @fa_type,@amt2_detail,0 end else if @pre09i = 2--扣基数乘以比率 begin -- 店总的业绩扣一个基数计算提成 select @total_comm = (@t_vperf - @baseamt) * @ratio end update workdetail_by_empl set comm = isnull(comm, 0) + @total_comm where id = @idd end else if @commway = 3 and (@projectway = 2 or @productway = 2 or @cardway = 2) begin -- 只能到workdetail_by_kind中取的一个员工项目的总合计,物品的总合计,卡的总合计,然后三个分别计算 declare @three_sum_comm float -- 卡,物品,项目的三个类别的提成合计 declare @comm_prj float declare @comm_good float declare @comm_card float select @three_sum_comm = 0, @comm_prj = 0, @comm_good = 0, @comm_card = 0 --Start 创建临时表 add by lm 2015-08-06 create table #workdetail_by_kind ( id int identity(1,1) not null, cls_flag int not null, -- 1:项目,2:产品,3:卡 oper_amt float null, -- 营业金额 total_vperf float null, -- 虚业绩 total_tperf float null, -- 实业绩 quan float null, -- 数量 f_oper_amt float null, -- 营业金额(指定) f_total_vperf float null, -- 虚业绩(指定) f_total_tperf float null, -- 实业绩(指定) f_quan float null, -- 数量(指定) t_oper_amt float null, -- 营业金额(轮班) t_total_vperf float null, -- 虚业绩(轮班) t_total_tperf float null, -- 实业绩(轮班) t_quan float null, -- 数量(轮班) total_f_quan float null, --每个员工不分项目类别的总的指定客数 total_cust_num float null --总的服务客户数量 ) insert #workdetail_by_kind(cls_flag, oper_amt, total_vperf, total_tperf, quan, f_oper_amt, f_total_vperf, f_total_tperf, f_quan, t_oper_amt, t_total_vperf, t_total_tperf, t_quan, total_f_quan, total_cust_num ) select cls_flag, sum(oper_amt), sum(total_vperf), sum(total_tperf), sum(quan), sum(f_oper_amt), sum(f_total_vperf), sum(f_total_tperf), sum(f_quan), sum(t_oper_amt), sum(t_total_vperf), sum(t_total_tperf), sum(t_quan), sum(total_f_quan), sum(total_cust_num) from workdetail_by_kind with (nolock) where spid = @spid and person_id = @staff_id group by cls_flag declare @HasRecordInner int declare @iddInner int -- 循环变量 set @HasRecordInner = 1 while(@HasRecordInner = 1) begin if exists(select 1 from #workdetail_by_kind) begin set @HasRecordInner = 1 select top 1 @iddInner = id , @cls_flag = cls_flag, @oper_amt = oper_amt, @t_vperf = total_vperf, @t_tperf = total_tperf, @quan2 = quan, @f_oper_amt = f_oper_amt, @f_t_vperf = f_total_vperf, @f_t_tperf = f_total_tperf, @f_quan2 = f_quan, @t_oper_amt = t_oper_amt, @t_t_vperf = t_total_vperf, @t_t_tperf = t_total_tperf, @t_quan2 = t_quan, @total_f_quan = total_f_quan, @total_cust_num = total_cust_num from #workdetail_by_kind -- @cls_flag 1:项目 2:产品 3:卡 -- if (@commway = 3 and @cls_flag = 1 and @projectway = 2) or (@commway = 3 and @cls_flag = 2 and @productway = 2) or (@commway = 3 and @cls_flag = 3 and @cardway = 2) begin select @total_f_quan = isnull(f_quan,0) from workdetail_by_empl with(nolock) where spid = @spid select @total_cust_num = count(1) from ggm07 b with(nolock), ggm01 a with(nolock) where a.gga00c = b.ggg00c and a.gga01c = b.ggg01c and a.gga02d >= @fromdate and a.gga02d <= @todate and a.gga00c = @compid and ggg02c = @staff_id group by ggg02c exec upg_cal_sum_comm @compid, @oper_amt, @t_vperf, @t_tperf, @quan2, @f_oper_amt, @f_t_vperf, @f_t_tperf, @f_quan2, @t_oper_amt, @t_t_vperf, @t_t_tperf, @t_quan2, @staff_id, @cls_flag, '', @total_f_quan, @total_cust_num, @total_comm output, @fa_type,@amt2_detail,0 if @cls_flag = 1 select @comm_prj = @total_comm else if @cls_flag = 2 select @comm_good = @total_comm else if @cls_flag = 3 select @comm_card = @total_comm select @three_sum_comm = @three_sum_comm + isnull(@total_comm, 0) end -- 清除已处理的数据 delete #workdetail_by_kind where id = @iddInner end else begin set @HasRecordInner = 0 end end drop table #workdetail_by_kind -- --End update workdetail_by_empl set comm = isnull(comm, 0) + @three_sum_comm, comm_prj = @comm_prj, comm_good = @comm_good, comm_card = @comm_card where id = @idd end select @consume_cost = 0 if @is_reduce_consume_cost = 1 begin -- 需要计算每个员工的消耗成本 exec upg_cal_staff_consume_cost @compid, @staff_id, @fromdate, @todate, @consume_cost output update workdetail_by_empl set consume_cost = @consume_cost where id = @idd end -- 清除已处理的门店 delete #workdetail_by_empl where id = @idd end else begin set @HasRecord = 0 end end drop table #workdetail_by_empl --End -- 更新员工姓名 update a set a.name = b.haa02c from workdetail_by_empl a, ham01 b where spid = @spid and b.haa00c = @compid and a.person_id = b.haa01c -- 再次更新员工姓名(总公司的下属公司的员工, 可能总公司没有资料) update a set a.name = b.haa02c from workdetail_by_empl a, ham01 b where spid = @spid and a.person_id = b.haa01c and isnull(a.name, '') = '' -- 统计每个员工的服务 + 产品的现金以及银行卡 declare @sp055f float select @sp055f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP055' if @sp055f = 0 -- 按项目支付 begin if (@return_type = 4) begin insert #person_cash(person_id, total_cash) select person_id, sum(isnull(amt, 0)) from #work_detail_ceshi with (nolock) where paycode in (select pay from pay_classify where comp = @compid and category = '1' ) and (cls_flag = 1 or cls_flag = 2) and spid = @spid group by person_id end else if @return_type = 3 begin insert #person_cash(person_id, total_cash) select person_id, sum(isnull(amt2, 0)) from #work_detail_ceshi with (nolock) where spid = @spid and paycode in (select pay from pay_classify where comp = @compid and category = '1' ) and (cls_flag = 1 or cls_flag = 2) group by person_id end else if @return_type = 5 begin insert #person_cash(person_id, total_cash) select person_id, sum(isnull(amt3, 0)) from #work_detail_ceshi with (nolock) where spid = @spid and paycode in (select pay from pay_classify where comp = @compid and category = '1' ) and (cls_flag = 1 or cls_flag = 2) group by person_id end update a set a.total_cash = isnull( b.total_cash, 0) from workdetail_by_empl a, #person_cash b where a.person_id = b.person_id and spid = @spid -- Added by WUXM, 20070111 -- 统计项目的现金支付以及卡支付,所谓的实做业绩,服务实做,卡实做 delete #person_cash -- 先统计项目的现金实做,包括现金,银行卡 if @return_type = 4 begin insert #person_cash(person_id, total_cash) select person_id, sum(isnull(amt, 0)) from #work_detail_ceshi with (nolock) where spid = @spid and paycode in (select pay from pay_classify where comp = @compid and category = '1' ) and (cls_flag = 1) group by person_id end else if @return_type = 3 begin insert #person_cash(person_id, total_cash) select person_id, sum(isnull(amt2, 0)) from #work_detail_ceshi with (nolock) where spid = @spid and paycode in (select pay from pay_classify where comp = @compid and category = '1' ) and (cls_flag = 1) group by person_id end else if @return_type = 5 begin insert #person_cash(person_id, total_cash) select person_id, sum(isnull(amt3, 0)) from #work_detail_ceshi with (nolock) where spid = @spid and paycode in (select pay from pay_classify where comp = @compid and category = '1' ) and (cls_flag = 1) group by person_id end update a set a.total_srv_cash = isnull( b.total_cash, 0) from workdetail_by_empl a, #person_cash b where a.person_id = b.person_id and spid = @spid --卖卡和冲值-现金单据包含签单挂帐,业绩计算add by lm 2015-06-03 --begin-- insert #work_detail_billid(billid, spid) --插入现金的单据 select distinct billid, spid from #work_detail_ceshi where spid = @spid and (paycode = '1' or paycode = '6' or paycode = 'ZFB' or paycode = 'WX') and (action_id = 1 or action_id = 0) insert #work_detail_cash(person_id, amt2, amt3, billid, paycode, spid)--插入数据 select person_id, a.amt2, a.amt3, a.billid, paycode, a.spid from #work_detail_billid b, #work_detail_ceshi a where a.spid = b.spid and a.billid = b.billid and a.spid = @spid and (a.action_id = 1 or a.action_id = 0) insert #work_detail_bill_account(person_id, amt2, amt3, billid, spid)--插入现金单据中支付方式为签单挂帐的 select person_id, amt2, amt3, billid, spid from #work_detail_cash where spid = @spid and paycode = '5' --end--------------- -- 统计项目的卡实做 delete #person_cash if @return_type = 4 begin insert #person_cash(person_id, total_cash) select person_id, sum(isnull(amt, 0)) from #work_detail_ceshi where spid = @spid and (paycode = '#' or paycode = '4' or paycode = '9' or (paycode >= 'A' and paycode <= 'Z')) and paycode not in ('WX','ZFB') and (cls_flag = 1) group by person_id end else if @return_type = 3 begin insert #person_cash(person_id, total_cash) select person_id, sum(isnull(amt2, 0)) from #work_detail_ceshi where spid = @spid and (paycode = '#' or paycode = '4' or paycode = '9' or (paycode >= 'A' and paycode <= 'Z')) and paycode not in ('WX','ZFB') and (cls_flag = 1) group by person_id end else if @return_type = 5 begin insert #person_cash(person_id, total_cash) select person_id, sum(isnull(amt3, 0)) from #work_detail_ceshi where spid = @spid and (paycode = '#' or paycode = '4' or paycode = '9' or (paycode >= 'A' and paycode <= 'Z')) and paycode not in ('WX','ZFB') and (cls_flag = 1) group by person_id end update a set a.total_srv_card = isnull( b.total_cash, 0) from workdetail_by_empl a, #person_cash b where a.person_id = b.person_id and spid = @spid end -- 统计每个员工的奖罚 create table #ham08_group ( person_id varchar(20) not null, amt float null, primary key(person_id) ) insert #ham08_group(person_id, amt) select hah01c, sum(isnull(hah05f, 0)) from ham08 with (nolock) where hah00c = @compid and hah03d >= @fromdate and hah03d <= @todate group by hah01c update a set a.bonus = b.amt from workdetail_by_empl a, #ham08_group b where a.person_id = b.person_id and spid = @spid drop table #ham08_group -- 统计每个员工的服务项目的数量,分别统计指定的数量及轮班的数量 add by lm (项目数改为单据数:用这个字段的过程有) update a set a.num_fix = (select count(distinct billid) from #work_detail_ceshi b where b.spid = @spid and a.person_id = b.person_id and (b.action_id = 2 or b.action_id = 5 or b.action_id = 7 or b.action_id = 10) group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.num_nofix = (select count(distinct billid) from #work_detail_ceshi b where b.spid = @spid and a.person_id = b.person_id and (b.action_id = 3 or b.action_id = 6 or b.action_id = 8 or b.action_id = 11) group by b.person_id) from workdetail_by_empl a where a.spid = @spid --内创数量 add by lm 2016.04.13 update a set a.num_in= (select count(distinct billid) from #work_detail_ceshi b where b.spid = @spid and a.person_id = b.person_id and (b.action_id = 15 or b.action_id = 18 or b.action_id = 20 or b.action_id = 23) group by b.person_id) from workdetail_by_empl a where a.spid = @spid --外创数量 add by lm 2016.04.13 update a set a.num_outer = (select count(distinct billid) from #work_detail_ceshi b where b.spid = @spid and a.person_id = b.person_id and (b.action_id = 16 or b.action_id = 19 or b.action_id = 21 or b.action_id = 24) group by b.person_id) from workdetail_by_empl a where a.spid = @spid if (@return_type = 4) --营业金额 begin update a set a.amt_c = ( select sum(b.amt) from #work_detail_ceshi b where b.spid = @spid and (b.action_id = 1 or b.action_id = 0) and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_c_cash = (select sum(b.amt) from #work_detail_ceshi b where b.spid = @spid and (b.action_id = 1 or b.action_id = 0) and a.person_id = b.person_id and paycode in (select pay from pay_classify where comp = @compid and category = '1' ) group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_1 = (select sum(b.amt) from #work_detail_ceshi b where b.spid = @spid and action_id = 2 and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_12 = (select sum(b.amt) from #work_detail_ceshi b where b.spid = @spid and b.action_id = 3 and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_13 = (select sum(b.amt) from #work_detail_ceshi b where b.spid = @spid and b.action_id = 4 and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_2 = (select sum(b.amt) from #work_detail_ceshi b where b.spid = @spid and b.action_id = 5 and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_21 = (select sum(b.amt) from #work_detail_ceshi b where b.spid = @spid and b.action_id = 6 and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_3 = (select sum(b.amt) from #work_detail_ceshi b where b.spid = @spid and (b.action_id = 7) and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_31 = (select sum(b.amt) from #work_detail_ceshi b where b.spid = @spid and (b.action_id = 8) and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_4 = (select sum(b.amt) from #work_detail_ceshi b where b.spid = @spid and (b.action_id = 10) and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_41 = (select sum(b.amt) from #work_detail_ceshi b where b.spid = @spid and (b.action_id = 11) and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid --start增加内创,外创 营业额 add by lm 2016.04.12 update a set a.amt_1_14 = (select sum(b.amt) from #work_detail_ceshi b where b.spid = @spid and (b.action_id = 15) and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_1_15 = (select sum(b.amt) from #work_detail_ceshi b where b.spid = @spid and (b.action_id = 16) and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_2_14 = (select sum(b.amt) from #work_detail_ceshi b where b.spid = @spid and (b.action_id = 18) and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_2_15 = (select sum(b.amt) from #work_detail_ceshi b where b.spid = @spid and (b.action_id = 19) and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_3_14 = (select sum(b.amt) from #work_detail_ceshi b where b.spid = @spid and (b.action_id = 20) and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_3_15 = (select sum(b.amt) from #work_detail_ceshi b where b.spid = @spid and (b.action_id = 21) and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_4_14 = (select sum(b.amt) from #work_detail_ceshi b where b.spid = @spid and (b.action_id = 23) and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_4_15 = (select sum(b.amt) from #work_detail_ceshi b where b.spid = @spid and (b.action_id = 24) and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid --end update a set a.amt_g = (select sum(b.amt) from #work_detail_ceshi b where b.spid = @spid and b.action_id = 9 and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid end if (@return_type = 3) -- 虚业绩 begin update a set a.amt_c = (select sum(b.amt2) from #work_detail_ceshi b where b.spid = @spid and (b.action_id = 1 or b.action_id = 0) and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_c_cash = (select sum(b.amt2) from #work_detail_ceshi b where b.spid = @spid and (b.action_id = 1 or b.action_id = 0) and a.person_id = b.person_id and paycode in (select pay from pay_classify where comp = @compid and category = '1' ) group by b.person_id) from workdetail_by_empl a where a.spid = @spid -- 把现金的签单挂帐业绩加上去 add by lm 2015-06-03 update a set a.amt_c_cash = a.amt_c_cash + isnull(b.amt2, 0) from workdetail_by_empl a, (select person_id, sum(amt2) as amt2 from #work_detail_bill_account with (nolock) where spid = @spid group by person_id)b where a.spid = @spid and a. person_id = b.person_id update a set a.amt_1 = (select sum(b.amt2) from #work_detail_ceshi b where b.spid = @spid and b.action_id = 2 and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_12 = (select sum(b.amt2) from #work_detail_ceshi b where b.spid = @spid and b.action_id = 3 and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_13 = (select sum(b.amt2) from #work_detail_ceshi b where b.spid = @spid and b.action_id = 4 and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_2 = (select sum(b.amt2) from #work_detail_ceshi b where b.spid = @spid and b.action_id = 5 and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_21 = (select sum(b.amt2) from #work_detail_ceshi b where b.spid = @spid and b.action_id = 6 and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_3 = (select sum(b.amt2) from #work_detail_ceshi b where b.spid = @spid and (b.action_id = 7) and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_31 = (select sum(b.amt2) from #work_detail_ceshi b where b.spid = @spid and (b.action_id = 8) and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_4 = (select sum(b.amt2) from #work_detail_ceshi b where b.spid = @spid and (b.action_id = 10) and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_41 = (select sum(b.amt2) from #work_detail_ceshi b where b.spid = @spid and (b.action_id = 11) and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid --start增加内创,外创 虚业绩 add by lm 2016.04.12 update a set a.amt_1_14 = (select sum(b.amt2) from #work_detail_ceshi b where b.spid = @spid and (b.action_id = 15) and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_1_15 = (select sum(b.amt2) from #work_detail_ceshi b where b.spid = @spid and (b.action_id = 16) and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_2_14 = (select sum(b.amt2) from #work_detail_ceshi b where b.spid = @spid and (b.action_id = 18) and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_2_15 = (select sum(b.amt2) from #work_detail_ceshi b where b.spid = @spid and (b.action_id = 19) and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_3_14 = (select sum(b.amt2) from #work_detail_ceshi b where b.spid = @spid and (b.action_id = 20) and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_3_15 = (select sum(b.amt2) from #work_detail_ceshi b where b.spid = @spid and (b.action_id = 21) and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_4_14 = (select sum(b.amt2) from #work_detail_ceshi b where b.spid = @spid and (b.action_id = 23) and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_4_15 = (select sum(b.amt2) from #work_detail_ceshi b where b.spid = @spid and (b.action_id = 24) and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid --end update a set a.amt_g = (select sum(b.amt2) from #work_detail_ceshi b where b.spid = @spid and b.action_id = 9 and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid end if (@return_type = 5) -- 真实业绩 begin update a set a.amt_c = (select sum(b.amt3) from #work_detail_ceshi b where b.spid = @spid and (b.action_id = 1 or b.action_id = 0) and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_c_cash = (select sum(b.amt3) from #work_detail_ceshi b where b.spid = @spid and (b.action_id = 1 or b.action_id = 0) and a.person_id = b.person_id and paycode in (select pay from pay_classify where comp = @compid and category = '1' ) group by b.person_id) from workdetail_by_empl a where a.spid = @spid -- 把现金的签单挂帐业绩加上去 add by lm 2015-06-03 update a set a.amt_c_cash = a.amt_c_cash + isnull(b.amt3, 0) from workdetail_by_empl a, (select person_id, sum(amt3) as amt3 from #work_detail_bill_account with (nolock) where spid = @spid group by person_id)b where a.spid = @spid and a. person_id = b.person_id update a set a.amt_1 = (select sum(b.amt3) from #work_detail_ceshi b where b.spid = @spid and b.action_id = 2 and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_12 = (select sum(b.amt3) from #work_detail_ceshi b where b.spid = @spid and b.action_id = 3 and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_13 = (select sum(b.amt3) from #work_detail_ceshi b where b.spid = @spid and b.action_id = 4 and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_2 = (select sum(b.amt3) from #work_detail_ceshi b where b.spid = @spid and b.action_id = 5 and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_21 = (select sum(b.amt3) from #work_detail_ceshi b where b.spid = @spid and b.action_id = 6 and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_3 = (select sum(b.amt3) from #work_detail_ceshi b where b.spid = @spid and (b.action_id = 7) and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_31 = (select sum(b.amt3) from #work_detail_ceshi b where b.spid = @spid and (b.action_id = 8) and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_4 = (select sum(b.amt3) from #work_detail_ceshi b where b.spid = @spid and (b.action_id = 10) and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_41 = (select sum(b.amt3) from #work_detail_ceshi b where b.spid = @spid and (b.action_id = 11) and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid --start增加内创,外创实业绩 add by lm 2016.04.12 update a set a.amt_1_14 = (select sum(b.amt3) from #work_detail_ceshi b where b.spid = @spid and (b.action_id = 15) and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_1_15 = (select sum(b.amt3) from #work_detail_ceshi b where b.spid = @spid and (b.action_id = 16) and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_2_14 = (select sum(b.amt3) from #work_detail_ceshi b where b.spid = @spid and (b.action_id = 18) and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_2_15 = (select sum(b.amt3) from #work_detail_ceshi b where b.spid = @spid and (b.action_id = 19) and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_3_14 = (select sum(b.amt3) from #work_detail_ceshi b where b.spid = @spid and (b.action_id = 20) and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_3_15 = (select sum(b.amt3) from #work_detail_ceshi b where b.spid = @spid and (b.action_id = 21) and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_4_14 = (select sum(b.amt3) from #work_detail_ceshi b where b.spid = @spid and (b.action_id = 23) and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid update a set a.amt_4_15 = (select sum(b.amt3) from #work_detail_ceshi b where b.spid = @spid and (b.action_id = 24) and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid --end update a set a.amt_g = (select sum(b.amt3) from #work_detail_ceshi b where b.spid = @spid and b.action_id = 9 and a.person_id = b.person_id group by b.person_id) from workdetail_by_empl a where a.spid = @spid end -- 计算一下那些计算型字段 update workdetail_by_empl set total_srv = isnull(amt_1, 0) + isnull(amt_12, 0) + isnull(amt_13, 0) + isnull(amt_2, 0) + isnull(amt_21, 0) + isnull(amt_3, 0) + isnull(amt_31, 0) + isnull(amt_4, 0) + isnull(amt_41, 0) + isnull(amt_1_14, 0) + isnull(amt_1_15, 0)+ isnull(amt_2_14, 0) + isnull(amt_2_15, 0)+ isnull(amt_3_14, 0) + isnull(amt_3_15, 0) + isnull(amt_4_14, 0) + isnull(amt_4_15, 0), total_perf = isnull(amt_1, 0) + isnull(amt_12, 0) + isnull(amt_13, 0) + isnull(amt_2, 0) + isnull(amt_21, 0) + isnull(amt_3, 0) + isnull(amt_31, 0) + isnull(amt_g, 0) + isnull(amt_4, 0) + isnull(amt_41, 0) + isnull(amt_1_14, 0) + isnull(amt_1_15, 0)+ isnull(amt_2_14, 0) + isnull(amt_2_15, 0)+ isnull(amt_3_14, 0) + isnull(amt_3_15, 0) + isnull(amt_4_14, 0) + isnull(amt_4_15, 0), total_cash_c = isnull(total_cash, 0) + isnull(amt_c_cash, 0) where spid = @spid declare @total_service float select @total_service = sum(amt_1) from workdetail_by_empl with (nolock) where spid = @spid if @total_service > 0 begin update a set a.amtrate_1 = isnull(amt_1, 0) / @total_service from workdetail_by_empl a where a.spid = @spid end drop table #person_cash drop table #work_detail_cash drop table #work_detail_billid drop table #work_detail_bill_account --add by lm 2015-07-21 end GO /****** Object: StoredProcedure [dbo].[upg_cal_work_detail_add_comm] Script Date: 06/12/2017 06:19:35 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_cal_work_detail_add_comm] ( @spid int, @compid varchar(10), -- 公司别 @fromdate varchar(10), -- 开始日期 @todate varchar(10), -- 截至日期 @fromempl varchar(20), -- 查询开始人员 @toempl varchar(20) -- 查询截至人员 ) as begin -- 计算实业绩,若可能就计算出提成 declare @total float declare @cardrate float declare @cost float declare @comm float declare @quan float declare @code varchar(20) declare @action_id int declare @flag int declare @trueperf float declare @vperf float declare @stdprice float declare @share_rate float declare @staff varchar(20) declare @ex_rate float declare @last_billid varchar(20) declare @last_seq float declare @last_m_comm float declare @last_l_comm float declare @ggm02_seq float declare @billid varchar(20) declare @prj_type varchar(20) -- 会员卡类别,产品类别,项目类别 declare @srvdate varchar(8) -- 账单日期 declare @paycode varchar(20) declare @sp048 varchar(20) select @sp048 = gsa02c from gsm01 where gsa00c = @compid and gsa01c = 'SP048' --Start 创建临时表 modify by lm 2015-08-05 create table #work_detail ( id int not null,-- 流水好 person_id varchar(20) not null, -- 员工代码 action_id tinyint not null, -- 类别,1-卡销售 2-指定大工,3-轮班大工,4-补工大工, -- 5-指定中工,6-轮班中工 -- 7-指定小工, 8-轮班小工,9-产品销售 10,11-四工 -- 0 卡充值 srvdate varchar(10) not null, --日期 code varchar(20) not null, --项目代码,或是卡号,产品码 paycode varchar(20) null, --支付代码 amt float null, --营业金额 quan float null, --数量 cost float null, --成本 amt2 float null, --虚业绩 amt3 float null, --真实业绩 cardrate float null, --转卡比率 --当买卡或充值的时候,这个地方当作疗程账户金额用 stdprice float null, --标准单价 prj_type varchar(20) null, --项目类别 share_rate float null default 1, billid varchar(20) null, --单号 ex_rate float null, --实际营业额折算比率 ggm02_seq float null, --ggm02中的序列号,与单号一起来标志一个项目 ) create nonclustered index idx_detail_comm_id_01 on #work_detail(id) insert #work_detail (id, person_id, action_id, code, paycode, amt, cost, quan, cardrate, amt2, amt3, stdprice, share_rate, ex_rate, ggm02_seq, billid, prj_type, srvdate) select id, person_id, action_id, code, paycode, amt, cost, quan, isnull(cardrate, 1), amt2, amt3, stdprice, share_rate, ex_rate, ggm02_seq, billid, prj_type, srvdate from #work_detail_ceshi a , (select hap00c, ppt03c, hap02c from personprecept with (nolock), ham15 with (nolock), precept with (nolock) where ppt02c = hap01c and ppt00c = hap00c and ppt02c = pre01c and ppt00c = pre00c and (isnull(pre17c, '0') = '1') and isnull(pre22i, 0) = 0 ) b where spid = @spid and a.person_id = b.ppt03c -- b.hap01c and b.hap00c = @compid and b.hap02c = a.code and cls_flag = 1 order by cls_flag, billid, ggm02_seq, action_id desc -- 创建循环变量 declare @HasRecord int declare @idd int set @HasRecord = 1 -- 按店业绩分段计算按天来 while(@HasRecord = 1) begin if exists(select 1 from #work_detail) begin set @HasRecord = 1 select top 1 @idd = id, @staff = person_id, @action_id = action_id, @code = code, @paycode = paycode, @total = amt, @cost = cost, @quan = quan, @cardrate = cardrate, @vperf = amt2, @trueperf = amt3, @stdprice = stdprice, @share_rate = share_rate, @ex_rate = ex_rate, @ggm02_seq = ggm02_seq, @billid = billid, @prj_type = prj_type, @srvdate = srvdate from #work_detail select @flag = @action_id if (@flag >= 2 and @flag <= 6) begin if (@billid <> @last_billid or @ggm02_seq <> @last_seq) begin select @last_m_comm = 0, @last_l_comm = 0 end else begin select @last_m_comm = isnull(@last_m_comm, 0), @last_l_comm = isnull(@last_l_comm, 0) end end else begin select @last_m_comm = 0, @last_l_comm = 0 end -- @ex_rate 项目的实际营业额折扣或产品的实际营业额折扣率 exec upg_statistic_proj_add_comm @compid, @code, @prj_type, @stdprice, @total, @vperf, @cardrate, @cost, @quan, @staff, @flag, @comm output, @trueperf output, @ex_rate, @last_m_comm, @last_l_comm, @share_rate, @srvdate, @paycode if (@flag = 7 or @flag = 8) -- 小工 begin select @last_l_comm = @comm, @last_billid = @billid, @last_seq = @ggm02_seq end else if (@flag = 5 or @flag = 6) begin select @last_m_comm = @comm, @last_billid = @billid, @last_seq = @ggm02_seq end else begin select @last_m_comm = 0, @last_l_comm = 0, @last_billid = '', @last_seq = -1 end update #work_detail_ceshi set comm = isnull(comm, 0) + @comm*@share_rate where id = @idd -- 清除已处理的门店 + delete #work_detail where id = @idd end else begin set @HasRecord = 0 end end insert #work_detail (id, person_id, action_id, code, amt, cost, quan, cardrate, amt2, amt3, stdprice, share_rate, ex_rate, ggm02_seq, billid, prj_type, srvdate) select id, person_id, action_id, code, amt, cost, quan, isnull(cardrate, 1), amt2, amt3, stdprice, share_rate, ex_rate, ggm02_seq, billid, prj_type, srvdate from #work_detail_ceshi a , (select gac00c, ppt03c, gac01c from personprecept with (nolock), alonecardprecept with (nolock), precept with (nolock) where ppt02c = gac02c and ppt00c = gac00c and ppt02c = pre01c and ppt00c = pre00c and isnull(pre19c, '0') = '1' and isnull(pre22i, 0) = 0 ) b where spid = @spid and a.person_id = b.ppt03c -- b.hap01c and b.gac00c = @compid and b.gac01c = a.prj_type and cls_flag = 3 and isnull(a.repayment_sign ,0) = 0 order by cls_flag, billid, ggm02_seq, action_id desc set @HasRecord = 1 -- 按店业绩分段计算按天来 while(@HasRecord = 1) begin if exists(select 1 from #work_detail) begin set @HasRecord = 1 select top 1 @idd = id, @staff = person_id, @action_id = action_id, @code = code, @total = amt, @cost = cost, @quan = quan, @cardrate = cardrate, @vperf = amt2, @trueperf = amt3, @stdprice = stdprice, @share_rate = share_rate, @ex_rate = ex_rate, @ggm02_seq = ggm02_seq, @billid = billid, @prj_type = prj_type, @srvdate = srvdate from #work_detail select @flag = @action_id -- @ex_rate 项目的实际营业额折扣或产品的实际营业额折扣率 exec upg_statistic_proj_add_comm @compid, @code, @prj_type, @stdprice, @total, @vperf, @cardrate, @cost, @quan, @staff, @flag, @comm output, @trueperf output, @ex_rate, 0, 0, @share_rate, @srvdate,'*' update #work_detail_ceshi set comm = isnull(comm, 0) + @comm where id = @idd -- 清除已处理的门店 delete #work_detail where id = @idd end else begin set @HasRecord = 0 end end insert #work_detail (id, person_id, action_id, code, amt, cost, quan, cardrate, amt2, amt3, stdprice, share_rate, ex_rate, ggm02_seq, billid, prj_type, srvdate) select id, person_id, action_id, code, amt, cost, quan, isnull(cardrate, 1), amt2, amt3, stdprice, share_rate, ex_rate, ggm02_seq, billid, prj_type, srvdate from #work_detail_ceshi a , (select gap00c, ppt03c, gap01c from personprecept with (nolock), aloneproductprecept with (nolock), precept with (nolock) where ppt02c = gap02c and ppt00c = gap00c and ppt02c = pre01c and ppt00c = pre00c and isnull(pre18c, '0') = '1' and isnull(pre22i, 0) = 0 ) b where spid = @spid and a.person_id = b.ppt03c -- b.hap01c and b.gap00c = @compid and b.gap01c = a.code and cls_flag = 2 order by cls_flag, billid, ggm02_seq, action_id desc -- 创建循环变量 set @HasRecord = 1 -- 按店业绩分段计算按天来 while(@HasRecord = 1) begin if exists(select 1 from #work_detail) begin set @HasRecord = 1 select top 1 @idd = id, @staff = person_id, @action_id = action_id, @code = code, @total = amt, @cost = cost, @quan = quan, @cardrate = cardrate, @vperf = amt2, @trueperf = amt3, @stdprice = stdprice, @share_rate = share_rate, @ex_rate = ex_rate, @ggm02_seq = ggm02_seq, @billid = billid, @prj_type = prj_type, @srvdate = srvdate from #work_detail select @flag = @action_id -- @ex_rate 项目的实际营业额折扣或产品的实际营业额折扣率 exec upg_statistic_proj_add_comm @compid, @code, @prj_type, @stdprice, @total, @vperf, @cardrate, @cost, @quan, @staff, @flag, @comm output, @trueperf output, @ex_rate, 0, 0, @share_rate, @srvdate,'*' update #work_detail_ceshi set comm = isnull(comm, 0) + @comm where id = @idd -- 清除已处理的门店 delete #work_detail where id = @idd end else begin set @HasRecord = 0 end end drop table #work_detail end GO /****** Object: StoredProcedure [dbo].[upg_statistic_card_goods] Script Date: 06/12/2017 06:20:44 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_statistic_card_goods] ( @compid varchar(10), -- 公司别 @personid varchar(20), -- 员工代码 @proj varchar(20), -- 产品代码或会员卡代码 @class varchar(20), -- 卡的类别 @standardprice float, -- 标准价 @total float, -- 营业额 @times float, -- 数量 @flag int, -- 1:卡销售 9:产品销售 0-卡充值 @share_rate float, -- 分享比率 @paycode varchar(20), -- 支付方式 @billtype varchar(20), @billid varchar(20), @vperf float output -- 虚业绩(已经考虑分享比率) ) as begin declare @type int declare @setvalue float declare @perf_type int declare @fa_type varchar(20) --提成方案编号 declare @orderdate varchar(8) --账单日期 added by cyr 2012-04-10 --只有在员工的提成按项目计算的时候,卡的业绩是根据会员卡类别中的设置来计算得 select top 1 @fa_type = ppt02c from personprecept where ppt00c = @compid and ppt03c = @personid and ppt04i = 1 and ppt02c in (select pre01c from precept where pre00c = @compid and isnull(pre22i, 0) = 0) order by ppt05d desc --获取在账单有效日期内最新一条员工提成方案 if (@flag = 1) -- 卡销售 begin if exists (select 1 from cardprecept where car01c = @class and car02c = @fa_type and car00c = @compid ) begin select @type = car03i, @setvalue = car04f from cardprecept where car01c = @class and car02c = @fa_type and car00c = @compid --得到卡销售业绩计算数据 end else begin select @type = gac03i, @setvalue = gac04f from alonecardprecept where gac01c = @class and gac02c = @fa_type and gac00c = @compid --得到卡销售业绩计算数据 end end else if (@flag = 0) -- 卡充值 begin if exists (select 1 from cardprecept where car01c = @class and car02c = @fa_type and car00c = @compid ) begin select @type = car07i, @setvalue = car08f from cardprecept where car01c = @class and car02c = @fa_type and car00c = @compid --得到卡充值业绩计算数据 end else begin select @type = gac07i, @setvalue = gac08f from alonecardprecept where gac01c = @class and gac02c = @fa_type and gac00c = @compid --得到卡销售业绩计算数据 end end else if (@flag = 9) begin select @type = pro03i, @setvalue = pro04f from productprecept where pro01c = @proj and pro02c = @fa_type and pro00c = @compid and (isnull(pro10c, '*') = '*' or isnull(pro10c, '*') = @paycode) end select @type = isnull(@type, 2) select @setvalue = isnull(@setvalue, 1) --如果是卡销售或卡充值,这根据支付方式赖调整业绩得计算,可能不算业绩 if @flag = 0 or @flag = 1 begin declare @pay_ratio float select @pay_ratio = gso05f from gsm15 where gso00c = @compid and gso02c = @paycode and gso01c = '3' select @pay_ratio = isnull(@pay_ratio, 1) select @setvalue = @setvalue * @pay_ratio end --计算虚业绩 if (@type = 1) -- 固定金额 select @vperf = @setvalue * @times * @share_rate else if (@type = 2) -- 营业额比率 select @vperf = @total * @setvalue * @share_rate else if (@type = 5) -- 标准价比率 select @vperf = @standardprice * @times * @setvalue * @share_rate else if (@type = 25 or @type = 26 or @type = 27 or @type = 28) -- 营业额扣除当日消费, 26扣除当日实际营业额 27 低于小于等于70% begin declare @todayamt float declare @remain_sale_amt float -- 计算当天的消费额 exec upg_cal_consumeamt_afterfill @compid, @billtype, @billid, @type, @todayamt output -- 可能因为几个支付方式, 金额已经是部分了,所以当天的消费额也必须分摊 select @todayamt = @todayamt * @times if @type = 27 or @type = 28 begin declare @limited_value float select @limited_value = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP134' if @limited_value = 0 -- 默认为0.7 select @limited_value = 0.7 declare @value float set @value = @total * @limited_value set @todayamt = Convert(decimal(18, 2), @todayamt) set @value = Convert(decimal(18, 2), @value) if (isnull(@todayamt, 0) <= isnull(@value, 0)) --modify by leiji on 2013年1月30日[多种支付方式买卡的情况框] begin select @remain_sale_amt = @total if @type = 28 begin select @remain_sale_amt = @total - isnull(@todayamt, 0) end end else -- 当天销卡太多了,不算卖卡业绩 begin select @remain_sale_amt = 0 end end else begin select @remain_sale_amt = @total - isnull(@todayamt, 0) if (@remain_sale_amt < 0) select @remain_sale_amt = 0 end select @vperf = (@remain_sale_amt) * @setvalue * @share_rate end end GO /****** Object: StoredProcedure [dbo].[upg_analyzer_customer_ex] Script Date: 06/12/2017 06:19:26 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_analyzer_customer_ex] ( @compid varchar(10), @class varchar(10), @frommemid varchar(20), -- 会员开始编号 @tomemid varchar(20), -- 会员截至编号 @memfromdate varchar(10), -- 资料输入日期 @memtodate varchar(10), -- 资料输入日期 @fromdate varchar(20), -- 消费开始日期 @todate varchar(20), -- 消费截至日期 @state varchar(200), -- 会员卡状态 @acon_amt float, -- 大于消费金额 @alastdate varchar(20), -- 某个日期之后 @at_times float, -- 大于消费次数 @at_history float, -- 会员历史大于年限 @aav_amt float, -- 大于平均消费金额 @aless_remain float, -- 小于某个储值余额 @abig_remain float, -- 大于某个储值余额 @addtoSMS int, @staitem int, -- 统计类型--2006/9/15 by sdw @only_card_pay int ) as begin declare @incompid varchar(10) select @incompid = @compid if(@alastdate <> '') set @alastdate = replace(@alastdate,'-','') declare @sp031f float select @sp031f = gsa03f from gsm01 with(nolock) where gsa00c = @compid and gsa01c = 'SP031' declare @payment_0 varchar(10) declare @payment_3 varchar(10) declare @payment_$ varchar(10) declare @payment_u1 varchar(10), @payment_u2 varchar(10), @payment_u3 varchar(10), @payment_u4 varchar(10), @payment_u5 varchar(10) exec upg_isrevenue_payment @compid, @payment_0 output, @payment_3 output, @payment_$ output, @payment_u1 output, @payment_u2 output, @payment_u3 output, @payment_u4 output, @payment_u5 output declare @compcode varchar(10) if @compid = '001' select @compcode = '%' else select @compcode = @compid if @fromdate<'19000101' select @fromdate = '19000101' if @todate>'20901231' select @todate = '20901231' -- 以卡的角度来统计会员 create table #card_member ( compid varchar(20) null, memid varchar(20) not null, m_name varchar(40) null, cardid varchar(20) not null, gender int null, mobile varchar(50) null, tel varchar(50) null, birthday varchar(8) null, start_date varchar(10) null, memaddr varchar(160) null, postcode varchar(20) null, principle varchar(20) null, cardclass varchar(20) null, -- primary key(cardid, memid) ) create table #mem_result( idd int identity not null, compid varchar(20) null, memid varchar(20) not null, -- Member编号 m_name varchar(40) null, -- 姓名 cardid varchar(20) null, -- 卡号 gender int null, -- 性别 0:女 mobile varchar(20) null, -- 手机 tel varchar(20) null, -- 电话 birthday varchar(8) null, state varchar(10) null, -- Member卡状态 con_amt float null, -- Consumption金额 consjamt float null, -- 实际消费金额 lastdate varchar(8) null, -- 最后Consumption日期 lastcall varchar(8) null, -- 最后回访日期 t_times float null, -- 用于比较的Consumption次数 t_compare float null, -- 总共来的次数 av_ytimes float null, -- 每年来的次数 av_times float null, -- 每月来的次数 av_wtimes float null, -- 每周来的次数 months float null, -- 客人来了多少个月了,客龄 av_amt float null, -- 每次Consumption的平均金额 start_date varchar(20) null, -- 开始日期 first_date varchar(20) null, -- 第一次来店日期 comp_date varchar(20) null, --开始比较的日期 dur_years float null, -- 统计期间来了多少个年了 dur_months float null, -- 统计期间来了多少个月了 dur_weeks float null, -- 统计期间来了多少个周了 remain01 float null, -- 余额 remain02 float null, -- 余额 remain03 float null, -- 余额 remain04 float null, -- 余额 remain05 float null, -- 余额 total_fill_amt float null, -- 累计Charge金额 memaddr varchar(160) null, -- 会员地址 postcode varchar(20) null, -- 邮编 principle varchar(20) null, -- 负责人 total_salecard_amt float null, -- 累计卡销售金额 primary key(idd)) create table #sum_tmp( vid varchar(20) not null, vsum float null, v_1 varchar(20) null, f_1 float null, v_2 varchar(20) null, primary key(vid)) --会员初始化 insert #card_member(compid, memid, m_name, cardid, gender, mobile, tel, birthday, start_date, memaddr, postcode, principle, cardclass) select distinct isnull(gca13d, gca00c), isnull(gca04c, ''), gba03c, gca01c, gba14i, gba08c, gba07c, gba17d, isnull(gba02c, '20100101'), gba05c, gba12c, gba10c, gca02c from gcm01 a with (nolock), gam26 z with (nolock), gbm01 b with (nolock) where a.gca00c = a.gca13d and substring(a.gca01c, 1, 2)<>'散客' and a.gca00c = z.gaz02c and z.gaz01c = @incompid and b.gba00c = z.gaz02c and (b.gba02c between @memfromdate and @memtodate or @memfromdate = '*' or @memtodate = '*') and a.gca04c = b.gba01c update a set a.m_name = '未知' from #card_member a where isnull(m_name, '') = '' if (@class = '' or @class = '*') begin if @frommemid = '' or @frommemid = '*' begin insert #mem_result(compid, memid, m_name, cardid, gender, mobile, tel, birthday, start_date, memaddr, postcode, principle) select compid, memid, m_name, cardid, gender, mobile, tel, birthday, start_date, memaddr, postcode, principle from #card_member a end else begin insert #mem_result(compid, memid, m_name, cardid, gender, mobile, tel, birthday, start_date, memaddr, postcode, principle) select compid, memid, m_name, cardid, gender, mobile, tel, birthday, start_date, memaddr, postcode, principle from #card_member a where a.memid >= @frommemid and a.memid <= @tomemid end end else begin if @frommemid = '' or @frommemid = '*' begin insert #mem_result(compid, memid, m_name, cardid, gender, mobile, tel, birthday, start_date, memaddr, postcode, principle) select compid, memid, m_name, cardid, gender, mobile, tel, birthday, start_date, memaddr, postcode, principle from #card_member a where a.cardclass = @class end else begin insert #mem_result(compid, memid, m_name, cardid, gender, mobile, tel, birthday, start_date, memaddr, postcode, principle) select compid, memid, m_name, cardid, gender, mobile, tel, birthday, start_date, memaddr, postcode, principle from #card_member a where a.cardclass = @class and a.memid >= @frommemid and a.memid <= @tomemid end end drop table #card_member --去掉散客 delete #mem_result where substring(memid, 1, 2) = '散客' --删除没有卡号的会员资料 delete #mem_result where isnull(cardid, '') = '' --消费总金额, 最后消费日期, 消费次数 exec upg_stat_card_consume @incompid, @fromdate, @todate, @frommemid, @tomemid, @only_card_pay --统计每个会员消费的次数 update a set a.con_amt = b.vsum, a.lastdate = b.v_1, a.t_times = b.f_1, a.first_date = b.v_2 from #mem_result a, #sum_tmp b where a.memid = b.vid create table #result ( compid varchar(20), --门店 uid varchar(20), --会员编号 amt float --现金类真是消费金额 ) create clustered index idx_result on #result(compid,uid) create table #gcm20 ( gct00c varchar(10) not null, --门店编号 gct02c varchar(20) not null, --卡号 gct03c varchar(20) not null, --会员编号 gct20c varchar(5) null, --支付方式 gct12f float null --消费金额 ) create clustered index idx_gcm20 on #gcm20(gct00c,gct03c) create table #pay_classify ( comp varchar(10) not null, -- 门店 pay varchar(5) not null -- 支付方式编码 ) create clustered index idx_pay_clssify on #pay_classify(comp,pay) insert #gcm20(gct00c,gct02c,gct03c,gct20c,gct12f) select gct00c,gct02c,gct02c,gsc04c,gsc05f from gcm20 a with(nolock),gsm03 b with(nolock) where a.gct00c = b.gsc00c and a.gct14c = b.gsc01c and a.gct13c = b.gsc02c and a.gct00c = @compid and gct04d between @memfromdate and @memtodate and (@frommemid = '*' or (a.gct03c >= @frommemid and a.gct03c <= @tomemid)) and gct13c in ('gn','gz') insert #gcm20(gct00c,gct02c,gct03c,gct20c,gct12f) select gct00c,gct02c,gct02c,gct20c,gct12f from gcm20 a with(nolock) where a.gct00c = @compid and gct04d between @memfromdate and @memtodate and (@frommemid = '*' or (a.gct03c >= @frommemid and a.gct03c <= @tomemid)) and gct13c in ('gx') insert #pay_classify(comp,pay) select comp,pay from pay_classify where comp = @compid and category = '1' insert #result(compid,uid,amt) select gct00c,gct03c,sum(gct12f)gct12f from #gcm20 a with(nolock),#pay_classify b with(nolock) where a.gct00c = b.comp and a.gct20c = b.pay group by a.gct00c,a.gct03c --更新实际消费金额 update a set a.consjamt = isnull(b.amt,0) from #mem_result a, #result b where a.memid = b.uid drop table #result drop table #gcm20 drop table #pay_classify --最后回访日期 delete #sum_tmp --2006/9/15 by sdw, 新增,决定用来计算的开始时间,开始时间小于第一次消费时,则以第一次消费为准,否则,反之 update a set a.comp_date = a.first_date from #mem_result a where a.first_date>@fromdate if @frommemid = '' or @frommemid = '*' begin insert #sum_tmp(vid, v_1) select gbf01c, max(gbf04c) from gbm06 with(nolock), gam26 z with(nolock) where gbf00c = z.gaz02c and z.gaz01c = @incompid and isnull(gbf01c, '')<>'' group by gbf01c end else begin insert #sum_tmp(vid, v_1) select gbf01c, max(gbf04c) from gbm06 with(nolock), gam26 z with(nolock) where gbf00c = z.gaz02c and z.gaz01c = @incompid and gbf01c >= @frommemid and gbf01c <= @tomemid and isnull(gbf01c, '')<>'' group by gbf01c end update a set a.lastcall = b.v_1 from #mem_result a, #sum_tmp b where a.memid = b.vid --每次消费的平均金额 update a set a.av_amt = isnull(a.con_amt, 0)/isnull(a.t_times, 1) from #mem_result a where isnull(a.t_times, 0) <> 0 --更新余额 create table #gcm03_tmp( cardid varchar(20) not null, gcc03i int not null, --帐号类别(1-电子钱包, 2-储值帐号, 3-积分, 4-储值账户二, 5-储值账户二) gcc06f float null --馀额 ) create clustered index idx_gcm03_tmp on #gcm03_tmp(cardid, gcc03i) insert #gcm03_tmp(cardid, gcc03i, gcc06f) select gca01c, gcc03i, gcc06f from #mem_result a, gcm03 b with (nolock), gcm01 c with (nolock) where a.cardid = b.gcc01c and b.gcc00c = c.gca00c and b.gcc01c = c.gca01c and c.gca00c = c.gca13d update a set a.remain01 = b.gcc06f from #mem_result a, #gcm03_tmp b where a.cardid = b.cardid and b.gcc03i = 1 update a set a.remain02 = b.gcc06f from #mem_result a, #gcm03_tmp b where a.cardid = b.cardid and b.gcc03i = 2 update a set a.remain03 = b.gcc06f from #mem_result a, #gcm03_tmp b where a.cardid = b.cardid and b.gcc03i = 3 update a set a.remain04 = b.gcc06f from #mem_result a, #gcm03_tmp b where a.cardid = b.cardid and b.gcc03i = 4 update a set a.remain05 = b.gcc06f from #mem_result a, #gcm03_tmp b where a.cardid = b.cardid and b.gcc03i = 5 drop table #gcm03_tmp --客人来了多少个月了, 从来店那天开始 ----2006/9/15 by sdw, 新增,更新统计期间总共有多少个年/日 --处理一下可能的日期字段错误 update #mem_result set start_date = '20010101' where isnull(start_date, '') = '' update #mem_result set comp_date = '20010101' where isnull(comp_date, '') = '' update #mem_result set first_date = '20010101' where isnull(first_date, '') = '' update #mem_result set lastdate = '20010101' where isnull(lastdate, '') = '' --update a --set a.months = isnull(datediff(month, convert(datetime, substring(a.start_date, 1, 8)), getdate()), 0) + 1, -- a.dur_years = isnull(datediff(year, convert(datetime, substring(a.comp_date, 1, 8)), convert(datetime, @todate)), 0) + 1, -- a.dur_months = isnull(datediff(month, convert(datetime, substring(a.comp_date, 1, 8)), convert(datetime, @todate)), 0) + 1, -- a.dur_weeks = isnull(datediff(week, convert(datetime, substring(a.comp_date, 1, 8)), convert(datetime, @todate)), 0) + 1 -- from #mem_result a --平均每月多少次 ----2006/9/15 by sdw, 新增,更新统计期间的年/周平均次数 update a set a.av_ytimes = a.t_times/a.dur_years, a.av_times = a.t_times/a.dur_months, a.av_wtimes = a.t_times/a.dur_weeks from #mem_result a ----2006/9/15 by sdw, 新增,根据统计分类的不同,来决定参与比较的是年平均次数,还是月/日平均次数 if (@staitem = 2) begin update a set a.t_compare = a.av_ytimes from #mem_result a end else if (@staitem = 1) begin update a set a.t_compare = a.av_times from #mem_result a end else if (@staitem = 0) begin update a set a.t_compare = a.av_wtimes from #mem_result a end else if (@staitem = 3) begin update a set a.t_compare = a.t_times from #mem_result a end --统计会员的累计充值金额 --是从gcm04中统计的,如果gcm04中不准,则统计可能错误。与会员卡销售的统计的方面不同 --会员卡销售是从原始的gcm10中直接统计的 delete #sum_tmp declare @custverflag varchar(20) select @custverflag = gsa02c from gsm01 with (nolock) where gsa00c = @incompid and gsa01c = 'SP109' insert #sum_tmp(vid, vsum) select gcd01c, sum(gcd06f) from gcm04 a with (nolock), gam26 z with(nolock) -- #mem_result b add by lm 2015.11.17 where (gcd02i<>1 and gcd02i<>3) and (gcd05i = 0 or gcd05i = 6) and a.gcd00c = z.gaz02c and z.gaz01c = @incompid and a.gcd09d >= @fromdate and a.gcd09d <= @todate -- and a.gcd01c = b.cardid group by gcd01c update a set a.total_fill_amt = isnull(b.vsum, 0) from #mem_result a, #sum_tmp b where a.cardid = b.vid delete #sum_tmp insert #sum_tmp(vid, vsum) select gcd01c, sum(gcd06f) from gcm04 a with (nolock), gam26 z with(nolock) --, #mem_result b add by lm 2015.11.17 where (gcd02i<>1 and gcd02i<>3) and (gcd05i = 1 or gcd05i = 5) and a.gcd00c = z.gaz02c and z.gaz01c = @incompid -- and a.gcd01c = b.cardid and a.gcd09d >= @fromdate and a.gcd09d <= @todate and (a.gcd07c <> 'gx' and a.gcd07c <> 'gl') group by gcd01c update a set a.total_fill_amt = isnull(a.total_fill_amt, 0) - isnull(b.vsum, 0) from #mem_result a, #sum_tmp b where a.cardid = b.vid delete #sum_tmp -- 累计消费金额的计算完成 --计算累计的卡销售金额,因为卡销售金额可能不同于储值金额 --Added by WUXM, 20081005 --因为卡在统计累计充值的时候是考虑到所有账户的,所以统计销售的时候也要考虑疗程的销售 --从gsm03取比较合适 create table #tbl_gsm03( cardid varchar(20) not null, pay varchar(10) not null, ddate varchar(20) null, compid varchar(10) not null, amt float null) create clustered index idx_gsm03_01 on #tbl_gsm03(cardid) insert #tbl_gsm03(cardid, compid, pay, ddate, amt) select isnull(gna13c, ''), gsc00c, gsc04c, isnull(gsc10d, ''), gsc05f from gnm01 a with (nolock), gsm03 b with (nolock), gam26 z with(nolock) where a.gna00c = b.gsc00c and a.gna01c = b.gsc01c and b.gsc02c = 'gn' and a.gna00c = z.gaz02c and z.gaz01c = @compid and a.gna02d >= @fromdate and a.gna02d <= @todate insert #tbl_gsm03(cardid, compid, pay, ddate, amt) select isnull(gcl03c, ''), gsc00c, gsc04c, isnull(gsc10d, ''), gsc05f from gcm10 a with (nolock), gsm03 b with (nolock), gam26 z with(nolock) where a.gcl00c = b.gsc00c and a.gcl01c = b.gsc01c and b.gsc02c = 'gz' and a.gcl00c = z.gaz02c and z.gaz01c = @compid and a.gcl02d >= @fromdate and a.gcl02d <= @todate --start add by lm 2016.03.01 insert #tbl_gsm03(cardid, compid, pay, ddate, amt) select isnull(gea07c, ''), gsc00c, gsc04c, isnull(gsc10d, ''), gsc05f from gem01 a with (nolock), gsm03 b with (nolock), gam26 z with(nolock) where a.gea00c = b.gsc00c and a.gea01c = b.gsc01c and b.gsc02c = 'ge' and a.gea00c = z.gaz02c and z.gaz01c = @compid and a.gea03d >= @fromdate and a.gea03d <= @todate and isnull(b.gsc10d,'') <> '' -- add lucz and a.gea02i = 4 --end insert #sum_tmp(vid, vsum) select a.cardid, sum(amt) from #tbl_gsm03 a with (nolock), #mem_result b where a.cardid = b.cardid and a.pay in (select pay from pay_classify where comp = @compid and category = '1') -- modify 2015-08-07 by lm 支付方式判断改为支付方式大类判断。 group by a.cardid drop table #tbl_gsm03 update a set a.total_salecard_amt = isnull(b.vsum, 0) from #mem_result a, #sum_tmp b where a.cardid = b.vid --更新卡的状态 --Added by Mzh 2006/03/23 update a set a.state = cast(gca08i as varchar(10)) from #mem_result a, gcm01 b with(nolock) where gca04c = memid and gca00c = gca13d if @addtoSMS = 1 begin declare @cur_max float select @cur_max = max(gxa01f) from gxm01 with(nolock) where gxa00c = @compid select @cur_max = isnull(@cur_max, 0) insert gxm01(gxa00c, gxa01f, gxa02c, gxa03c) select @compid, idd = @cur_max + idd, memid, mobile from #mem_result where memid not in (select gxa02c from gxm01 where gxa00c = @compid) and isnull(memid, '')<>'' and isnull(con_amt, 0) >= @acon_amt and isnull(lastdate, '00000000') >= @alastdate and isnull(t_times, 0) >= @at_times and isnull(av_amt, 0) >= @aav_amt end if (@state = '' or @state = '*') begin declare @count int select @count = count(1) from #mem_result select idd, isnull(compid,'')compid, isnull(memid,'')memid, isnull(m_name,'')m_name, isnull(cardid,'')cardid, isnull(gender,'')gender, isnull(mobile,'')mobile, isnull(tel,'')tel, isnull(birthday,'')birthday, isnull(state,'')state, isnull(con_amt,0)con_amt,isnull(consjamt,0)consjamt, isnull(lastdate,'')lastdate, isnull(lastcall,'')lastcall, isnull(t_times,'')t_times, isnull(t_compare,'')t_compare, isnull(av_ytimes,'')av_ytimes, isnull(av_times,'')av_times, isnull(av_wtimes,'')av_wtimes, isnull(months,'')months, isnull(av_amt,'')av_amt, isnull(start_date,'')start_date, isnull(first_date,'')first_date, isnull(comp_date,'')comp_date, isnull(dur_years,'')dur_years, isnull(dur_months,'')dur_months, isnull(dur_weeks,'')dur_weeks, isnull(remain01,'')remain01, isnull(remain02,'')remain02, isnull(remain03,'')remain03, isnull(remain04,'')remain04, isnull(remain05,'')remain05, isnull(total_fill_amt,'')total_fill_amt, isnull(memaddr,'')memaddr, isnull(postcode,'')postcode, isnull(principle,'')principle, isnull(total_salecard_amt,'')total_salecard_amt from #mem_result where isnull(con_amt, 0) >= @acon_amt and isnull(lastdate, '19740313') >= @alastdate and isnull(t_compare, 0) >= @at_times --2006/9/15 by sdw, 修改,比较次数 and isnull(av_amt, 0) >= @aav_amt and isnull(con_amt, 0) >= @acon_amt and isnull(months, 0) >= isnull(@at_history, 0)*12 --2006/9/15 by sdw, 新增,会员历史条件 and isnull(remain02, 0) + isnull(remain04, 0) + isnull(remain05, 0) >= @abig_remain and isnull(remain02, 0) + isnull(remain04, 0) + isnull(remain05, 0) <= @aless_remain order by con_amt desc end else begin create table #tmp ( s varchar(10) ) declare @long_str_Tmp varchar(10), @short_str varchar(10), @split_str_length int, @split_str varchar(10), @Long_str varchar(200) set @split_str = ',' set @Long_str = @state SET @split_str_length = LEN(@split_str) IF CHARINDEX(@split_str, @Long_str) = 1 SET @long_str_Tmp = SUBSTRING(@Long_str, @split_str_length + 1, LEN(@Long_str)-@split_str_length) ELSE SET @long_str_Tmp = @Long_str IF CHARINDEX(REVERSE(@split_str), REVERSE(@long_str_Tmp))>1 SET @long_str_Tmp = @long_str_Tmp + @split_str ELSE SET @long_str_Tmp = @long_str_Tmp IF CHARINDEX(@split_str, @long_str_Tmp) = 0 insert into #tmp select @long_str_Tmp ELSE begin WHILE CHARINDEX(@split_str, @long_str_Tmp)>0 begin SET @short_str = SUBSTRING(@long_str_Tmp, 1, CHARINDEX(@split_str, @long_str_Tmp)-1) declare @long_str_Tmp_LEN INT, @split_str_Position_end int SET @long_str_Tmp_LEN = LEN(@long_str_Tmp) SET @split_str_Position_end = LEN(@short_str) + @split_str_length SET @long_str_Tmp = REVERSE(SUBSTRING(REVERSE(@long_str_Tmp), 1, @long_str_Tmp_LEN-@split_str_Position_end)) IF @short_str<>'' insert into #tmp select @short_str end end select idd, compid, isnull(memid,'')memid, isnull(m_name,'')m_name, isnull(cardid,'')cardid, isnull(gender,'')gender, isnull(mobile,'')mobile, isnull(tel,'')tel, isnull(birthday,'')birthday, isnull(state,'')state, isnull(con_amt,'')con_amt,isnull(consjamt,0)consjamt, isnull(lastdate,'')lastdate, isnull(lastcall,'')lastcall, isnull(t_times,'')t_times, isnull(t_compare,'')t_compare, av_ytimes, isnull(av_times,'')av_times, isnull(av_wtimes,'')av_wtimes, isnull(months,'')months, isnull(av_amt,'')av_amt, isnull(start_date,'')start_date, first_date, comp_date, isnull(dur_years,'')dur_years, isnull(dur_months,'')dur_months, isnull(dur_weeks,'')dur_weeks, isnull(remain01,'')remain01, isnull(remain02,'')remain02, isnull(remain03,'')remain03, isnull(remain04,''), isnull(remain05,'')remain05, isnull(total_fill_amt,'')total_fill_amt, isnull(memaddr,'')memaddr, isnull(postcode,'')postcode, isnull(principle,'')principle, isnull(total_salecard_amt,'')total_salecard_amt from #mem_result where isnull(con_amt, 0) >= @acon_amt and isnull(lastdate, '19740313') >= @alastdate and isnull(t_compare, 0) >= @at_times --2006/9/15 by sdw, 修改,比较次数 and isnull(av_amt, 0) >= @aav_amt and isnull(con_amt, 0) >= @acon_amt and isnull(months, 0) >= isnull(@at_history, 0)*12 --2006/9/15 by sdw, 新增,会员历史条件 and isnull(remain02, 0) + isnull(remain04, 0) + isnull(remain05, 0) >= @abig_remain and isnull(remain02, 0) + isnull(remain04, 0) + isnull(remain05, 0) <= @aless_remain and state in (select s from #tmp) order by con_amt desc drop table #tmp end drop table #mem_result drop table #sum_tmp end GO /****** Object: StoredProcedure [dbo].[upg_zd_kq_info] Script Date: 06/12/2017 06:20:53 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_zd_kq_info] ( @compid varchar(10), --公司编号 @strdate varchar(8), --日期 @empid varchar(20), --员工编号 @strTimes varchar(6), --考勤时间 @userid varchar(20) --操作人员 ) as begin declare @SP102 float --是否排班 declare @strduty varchar(5) declare @relt int declare @begintime varchar(20) --开始上班时间(包括日期) declare @endtime varchar(20) --下班时间(包括日期) declare @kqtime varchar(20) --考勤时间(包括日期) declare @strTime varchar(20) declare @timeout int --相差的时间 declare @intseq int --标识 select @SP102 = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP102' if @SP102 = 0 --这个是必须排班 begin ---获得当天的班次 select @strduty = gicj02v from gicm10 where gicj00c = @compid and gicj01v = @empid and gicj05d = @strdate --获取考勤时间的位置 exec upg_duty_record @compid, @strdate, @empid, @strduty, 0, @relt output --获取当天开始上班的时间 select @begintime = gicj05d + gicj03t from gicm10 where gicj00c = @compid and gicj01v = @empid and gicj02v = @strduty and gicj05d = @strdate --获取当天的下班时间 select @endtime = gicj05d + gicj04t from gicm10 where gicj00c = @compid and gicj01v = @empid and gicj02v = @strduty and gicj05d = @strdate --单次的考勤日期 + 时间 set @kqtime = @strdate + @strTimes --计算2个时间的差 exec upg_getTimeout @kqtime, @endtime, @strTime output, @intseq output if (@relt = -1) begin insert into hdm01(hda00c, hda01c, hda18c, hda07c, hda91c, hda92d, hda02d, hda03t, hda93c, hda94d) values(@compid, @empid, @strduty, 1, @userid, @strdate, @strdate, @strTimes, @userid, @strdate) --计算迟到时间 exec upg_getTimeout @begintime, @kqtime, @strTime output, @intseq output if (@intseq = 1) begin update hdm01 set hda08i = @strTime, hda17c = 3 where hda00c = @compid and hda01c = @empid and hda18c = @strduty and hda02d = @strdate update hdm01 set hda07c = 6 where hda00c = @compid and hda01c = @empid and hda18c = @strduty and hda02d = @strdate and hda07c = 1 end end else if (@relt = 0) begin update hdm01 set hda03t = @strTimes where hda00c = @compid and hda01c = @empid and hda18c = @strduty and hda02d = @strdate --计算迟到时间 exec upg_getTimeout @begintime, @kqtime, @strTime output, @intseq output if (@intseq = 1) begin update hdm01 set hda08i = @strTime, hda17c = 3 where hda00c = @compid and hda01c = @empid and hda18c = @strduty and hda02d = @strdate update hdm01 set hda07c = 6 where hda00c = @compid and hda01c = @empid and hda18c = @strduty and hda02d = @strdate and hda07c = 1 end end else if ( @relt = 1) begin update hdm01 set hda04t = @strTimes where hda00c = @compid and hda01c = @empid and hda18c = @strduty and hda02d = @strdate if (@intseq = 1) begin update hdm01 set hda09i = @strTime where hda00c = @compid and hda01c = @empid and hda18c = @strduty and hda02d = @strdate end else begin update hdm01 set hda16t = @strTime where hda00c = @compid and hda01c = @empid and hda18c = @strduty and hda02d = @strdate end end else if (@relt = 2) begin update hdm01 set hda12t = @strTimes, hda09i = '000000', hda16t = '000000' where hda00c = @compid and hda01c = @empid and hda18c = @strduty and hda02d = @strdate end else if (@relt = 3) begin update hdm01 set hda13t = @strTimes where hda00c = @compid and hda01c = @empid and hda18c = @strduty and hda02d = @strdate if (@intseq = 1) begin update hdm01 set hda09i = @strTime where hda00c = @compid and hda01c = @empid and hda18c = @strduty and hda02d = @strdate end else begin update hdm01 set hda16t = @strTime where hda00c = @compid and hda01c = @empid and hda18c = @strduty and hda02d = @strdate end end else if (@relt = 4) begin update hdm01 set hda14t = @strTimes, hda09i = '000000', hda16t = '000000' where hda00c = @compid and hda01c = @empid and hda18c = @strduty and hda02d = @strdate end else if (@relt = 5) begin update hdm01 set hda15t = @strTimes where hda00c = @compid and hda01c = @empid and hda18c = @strduty and hda02d = @strdate if (@intseq = 1) begin update hdm01 set hda09i = @strTime where hda00c = @compid and hda01c = @empid and hda18c = @strduty and hda02d = @strdate end else begin update hdm01 set hda16t = @strTime where hda00c = @compid and hda01c = @empid and hda18c = @strduty and hda02d = @strdate end end end else begin --这个是不排班的考勤 exec upg_duty_record @compid, @strdate, @empid, @strduty, 1, @relt output if (@relt = -1) begin insert into hdm01(hda00c, hda01c, hda07c, hda91c, hda92d, hda02d, hda03t, hda93c, hda94d) values(@compid, @empid, 1, @userid, @strdate, @strdate, @strTimes, @userid, @strdate) end else if (@relt = 0) begin update hdm01 set hda03t = @strTimes where hda00c = @compid and hda01c = @empid and hda02d = @strdate end else if (@relt = 1) begin update hdm01 set hda04t = @strTimes where hda00c = @compid and hda01c = @empid and hda02d = @strdate end else if (@relt = 2) begin update hdm01 set hda12t = @strTimes where hda00c = @compid and hda01c = @empid and hda02d = @strdate end else if (@relt = 3) begin update hdm01 set hda13t = @strTimes where hda00c = @compid and hda01c = @empid and hda02d = @strdate end else if (@relt = 4) begin update hdm01 set hda14t = @strTimes where hda00c = @compid and hda01c = @empid and hda02d = @strdate end else if (@relt = 5) begin update hdm01 set hda15t = @strTimes where hda00c = @compid and hda01c = @empid and hda02d = @strdate end end end GO /****** Object: StoredProcedure [dbo].[upg_stat_byday_depart_emp] Script Date: 06/12/2017 06:20:42 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_stat_byday_depart_emp] ( @compid varchar(10), @fromdate varchar(10), @todate varchar(10), @flag int, -- 0-每天一纪录 1-返回一条记录 @staff varchar(20), @fromdepart varchar(10), -- add by lyj 2007-09-06 @todepart varchar(10), @calmode int -- 0-营业款 1-营业收入 2-综合小单 ) as begin declare @cur_date datetime declare @strtmp varchar(30) declare @str_date varchar(20) declare @amt float declare @tmpamt float declare @cash_sum float declare @qry_compid varchar(20) declare @incompid varchar(10) select @incompid = @compid if @compid = '001' or @compid = '*' begin select @qry_compid = '%' end else begin select @qry_compid = @compid end set @cur_date = convert(datetime, @fromdate) create table #tbl_profit_day ( idd int not null, code varchar(20) not null, compid varchar(20) not null, empno varchar(20) not null, itemname varchar(60) null, is_add int null, amt float null, is_leaf int null, payment varchar(10) null, -- 支付方式 kind int null -- 0 充值 1卡销售 9产品销售 2 服务 -1 取款 8 保证金收入 6还款 ) create clustered index idx_tbl_profit_day on #tbl_profit_day(compid, empno,code) create table #emp_info --add by lm 2016.06.21 ( empno varchar(20) not null, empname varchar(20) null, compid varchar(20) not null primary key (compid, empno) ) insert #emp_info(compid, empno,empname) select gsb00c, gsb02c,gsb03c from gsm02 a with(nolock), gam26 z with(nolock) where a.gsb00c = z.gaz02c and a.gsb01c = 'U' and z.gaz01c = @compid and ((a.gsb02c between @fromdepart and @todepart )or @fromdepart = '*') create table #stat_byday ( compid varchar(20) not null, empno varchar(20) not null, cash_card float null, -- 卡销售-现金 cash_srv float null, cash_good float null, cash_total float null, credit_card float null, credit_srv float null, credit_good float null, credit_total float null, zfb_card float null, zfb_srv float null, zfb_good float null, zfb_total float null, wx_card float null, wx_srv float null, wx_good float null, wx_total float null, revene float null, outcome float null, profit float null, defict float null, returnamt float null, pay_card_srv float null, -- 卡付中,已经不包括积分支付了 pay_card_good float null, -- 卡付,产品,不包括积分支付 pay_coupon float null, -- 券付 支付方式3 pay_coupon2 float null, -- 贵付 支付方式$ pay_coupon3 float null, -- 洗头 支付方式内调 pay_manager float null, -- 经理签单 sale_card_total float null, -- 总销售卡 pay_card_srv_act float null, -- 卡付实际营业额 = 在服务项目中,那些卡付的金额转换为实际营业额 pay_card_srv_work float null, -- 卡付实际劳动业绩 = 在服务项目 + 产品中,那些卡付的金额转换为劳动业绩 pay_coupon_srv float null, -- 券付 支付方式3, 只包括项目 pay_coupon2_srv float null, -- 贵付 支付方式$, 只包括项目 pay_coupon3_srv float null, -- 洗头 支付方式内调, 只包括项目 draw_total float null, -- 取款总额(包含现金以及内调) work_perf float null, -- 劳动业绩 = 现金(服务) + 现金(产品) + 卡付(产品) + 卡付实营业额 + 经理签单 + 内调 + 抵用券 + 银行卡(服务) + 银行卡(产品) cust_quan float null, -- 客人数量 proj_quan float null, -- 项目数 pay_manager_prj float null, --只包括项目的经理签单 pay_manager_good float null, --只包括产品的经理签单 pay_coupon_good float null, -- 券付 支付方式3, 只包括产品 pay_coupon2_good float null, -- 贵付 支付方式$, 只包括产品 pay_coupon3_good float null, -- 洗头 支付方式内调, 只包括产品 pay_point float null, -- 积分支付,卡销售,项目,产品 pay_point_srv float null, -- 积分支付,只包括项目(含产品) pay_point_good float null, -- 积分支付,只有产品 pay_user01 float null, -- 自定义支付,卡销售,项目,产品 pay_user01_srv float null, -- 自定义支付,只包括项目(含产品) pay_user01_good float null, -- 自定义支付,只有产品 pay_user02 float null, -- 自定义支付,卡销售,项目,产品 pay_user02_srv float null, -- 自定义支付,只包括项目(含产品) pay_user02_good float null, -- 自定义支付,只有产品 pay_user03 float null, -- 自定义支付,卡销售,项目,产品 pay_user03_srv float null, -- 自定义支付,只包括项目(含产品) pay_user03_good float null, -- 自定义支付,只有产品 pay_user04 float null, -- 自定义支付,卡销售,项目,产品 pay_user04_srv float null, -- 自定义支付,只包括项目(含产品) pay_user04_good float null, -- 自定义支付,只有产品 pay_user05 float null, -- 自定义支付,卡销售,项目,产品 pay_user05_srv float null, -- 自定义支付,只包括项目(含产品) pay_user05_good float null, -- 自定义支付,只有产品 other_income float null, -- 其他收入 Huzy 2012/05/31 virtual_income float null, --虚拟收入 virtual_outcome float null, --虚拟支出 prj_quan_buy float default 0 null, -- 疗程销售次数 prj_amt_buy float default 0 null, -- 疗程销售金额 prj_quan_consume float default 0 null, -- 疗程消耗次数 prj_amt_consume float default 0 null, -- 疗程消耗金额 cash_sum float default 0 null, -- 现金类金额 empname varchar(40) primary key(compid, empno)) --create nonclustered index index_stat_byday on #stat_byday(compid, empno) insert #tbl_profit_day(compid, empno, idd, code, itemname, is_add, amt, is_leaf, payment, kind) exec upg_analyse_profit_inter_depart_emp @compid, @fromdate, @todate, 1, @staff, @fromdepart, @todepart, @calmode --add by lyj 2007-09-06 --统计客户数量 create table #cust_num_by_day( compid varchar(20) not null, -- 公司 cust_num float null, -- 数量 primary key(compid)) insert #cust_num_by_day(compid, cust_num) select gga00c, sum(isnull(gga14f, 1)) from ggm01 with(nolock) where gga00c like @qry_compid and gga80d >= @fromdate and gga80d <= @todate group by gga00c declare @state int set @state = 0 declare @comp varchar(20) declare @empno varchar(20) declare @empname varchar(40) if not exists(select 1 from #tbl_profit_day) set @state = 1 while (@state = 0) begin if exists(select 1 from #emp_info ) begin select top 1 @comp = compid, @empno = empno, @empname = empname from #emp_info --where compid = '001' insert #stat_byday(compid, empno,empname) select @comp,@empno,@empname update a set a.cust_quan = b.cust_num from #stat_byday a, #cust_num_by_day b where a.compid = b.compid --转换为汇总表需要的一条记录 set @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = 'HB' and compid = @comp and empno = @empno update a set a.proj_quan = isnull(@amt, 0) from #stat_byday a where a.compid = @comp and a.empno = @empno --现金(卖卡) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110101' or code = '110104' or code = '110107' or code = '110118' or code = '110140') and compid = @comp and empno = @empno --现金取款 select @tmpamt = 0 select @tmpamt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110119' and compid = @comp and empno = @empno update a set a.cash_card = isnull(@amt, 0) + isnull(@tmpamt, 0) from #stat_byday a where a.compid = @comp and a.empno = @empno --现金(服务) --在按项目支付的情况下,是正确的项目的现金支付 --在单据支付的情况下,实际上包括了产品销售的现金支付 select @amt = 0 select @tmpamt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110201' or code = '110230')-- 项目或物品的现金还款也算 还款的不算在 and compid = @comp and empno = @empno select @tmpamt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110301' or code = '110310') -- 在单据支付的情况,这个值为0, 包括了产品的现金还款 and compid = @comp and empno = @empno update a set a.cash_srv = isnull(@amt, 0) - isnull(@tmpamt, 0) from #stat_byday a where compid = @comp and empno = @empno --银行卡(卖卡) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110102' or code = '110105' or code = '110108' or code = '110141') and compid = @comp and empno = @empno update a set a.credit_card = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --支付宝(卖卡) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '1101Z1' or code = '1101Z2' or code = '1101Z3' or code = '1101Z4') and compid = @comp and empno = @empno update a set a.zfb_card = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --微信(卖卡) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '1101W1' or code = '1101W2' or code = '1101W3' or code = '1101W4') and compid = @comp and empno = @empno update a set a.wx_card = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --不管支付方式,实际上的卡销售额 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '1101' and compid = @comp and empno = @empno update a set a.sale_card_total = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --银行卡(服务) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110202' and compid = @comp and empno = @empno select @tmpamt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110306' or code = '110311') -- 产品销售的银行卡支付 以及 还款 and compid = @comp and empno = @empno update a set a.credit_srv = isnull(@amt, 0) - isnull(@tmpamt, 0) from #stat_byday a where compid = @comp and empno = @empno --支付宝(服务) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '1102Z1' and compid = @comp and empno = @empno select @tmpamt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '1103Z2' or code = '1103Z1') -- 产品销售的银行卡支付 以及 还款 and compid = @comp and empno = @empno update a set a.zfb_srv = isnull(@amt, 0) - isnull(@tmpamt, 0) from #stat_byday a where compid = @comp and empno = @empno --微信(服务) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '1102W1' and compid = @comp and empno = @empno select @tmpamt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '1103W2' or code = '1103W1') -- 产品销售的微信支付 以及 还款 and compid = @comp and empno = @empno update a set a.wx_srv = isnull(@amt, 0) - isnull(@tmpamt, 0) from #stat_byday a where compid = @comp and empno = @empno --支出 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '12' and compid = @comp and empno = @empno select @tmpamt = 0 select @tmpamt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '1299' and compid = @comp and empno = @empno update a set a.outcome = isnull(@amt, 0) - isnull(@tmpamt, 0) from #stat_byday a where compid = @comp and empno = @empno --欠款 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110103' or code = '110106' or code = '110207' or code = '110145') and compid = @comp and empno = @empno update a set a.defict = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --还款 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where kind = 6 and is_leaf = 1 and compid = @comp and empno = @empno update a set a.returnamt = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --卡付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110205' and compid = @comp and empno = @empno select @tmpamt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110302' and compid = @comp and empno = @empno update a set a.pay_card_srv = isnull(@amt, 0) - isnull(@tmpamt, 0) from #stat_byday a where compid = @comp and empno = @empno --卡付实际营业额 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110206' and compid = @comp and empno = @empno update a set a.pay_card_srv_act = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '11AA01' and compid = @comp and empno = @empno update a set a.pay_card_srv_work = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --抵用券付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110204' or code = '110125' or code = '110126' or code = '110127') and compid = @comp and empno = @empno update a set a.pay_coupon = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --抵用券付, 只包括项目 + 产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110204') and compid = @comp and empno = @empno update a set a.pay_coupon_srv = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --抵用券付, 只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110303') and compid = @comp and empno = @empno update a set a.pay_coupon_good = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --贵付(抵用券二) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110208' or code = '110130' or code = '110131' or code = '110132') and compid = @comp and empno = @empno update a set a.pay_coupon2 = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --贵付(抵用券二), 只包括项目 + 产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110208') and compid = @comp and empno = @empno update a set a.pay_coupon2_srv = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --贵付(抵用券二), 只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110304') and compid = @comp and empno = @empno update a set a.pay_coupon2_good = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --内调(洗头) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where ((code = '110209' or code = '110120' or code = '110121' or code = '110122' or code = '1101400') or (kind = 6 and payment = '0' )) and compid = @comp and empno = @empno update a set a.pay_coupon3 = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --内调(洗头), 只包括项目 + 产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110209') and compid = @comp and empno = @empno update a set a.pay_coupon3_srv = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --内调(洗头), 只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110308') and compid = @comp and empno = @empno update a set a.pay_coupon3_good = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --经理签单, 包括卡销售的经理签单 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110110' or code = '110111' or code = '110203') and compid = @comp and empno = @empno update a set a.pay_manager = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --经理签单,只包括项目 + 产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110203') and compid = @comp and empno = @empno update a set a.pay_manager_prj = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --经理签单,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110305') and compid = @comp and empno = @empno update a set a.pay_manager_good = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --自定义支付, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$1' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8) and compid = @comp and empno = @empno update a set a.pay_user01 = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$1' and (kind = 2) and compid = @comp and empno = @empno update a set a.pay_user01_srv = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$1' and (kind = 9) and compid = @comp and empno = @empno update a set a.pay_user01_good = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --自定义支付, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$2' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8) and compid = @comp and empno = @empno update a set a.pay_user02 = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$2' and (kind = 2) and compid = @comp and empno = @empno update a set a.pay_user02_srv = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$2' and (kind = 9) and compid = @comp and empno = @empno update a set a.pay_user02_good = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --自定义支付, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$3' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8) and compid = @comp and empno = @empno update a set a.pay_user03 = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$3' and (kind = 2) and compid = @comp and empno = @empno update a set a.pay_user03_srv = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$3' and (kind = 9) and compid = @comp and empno = @empno update a set a.pay_user03_good = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --自定义支付, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$4' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8) and compid = @comp and empno = @empno update a set a.pay_user04 = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$4' and (kind = 2) and compid = @comp and empno = @empno update a set a.pay_user04_srv = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$4' and (kind = 9) and compid = @comp and empno = @empno update a set a.pay_user04_good = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --自定义支付, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$5' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8) and compid = @comp and empno = @empno update a set a.pay_user05 = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$5' and (kind = 2) and compid = @comp and empno = @empno update a set a.pay_user05_srv = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$5' and (kind = 9) and compid = @comp and empno = @empno update a set a.pay_user05_good = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --其他收入 Huzy 2012/05/31 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '18') and compid = @comp and empno = @empno update a set a.other_income = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --虚拟收入virtual_income select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = 'n1') and compid = @comp and empno = @empno update a set a.virtual_income = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --虚拟支出virtual_outcome select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = 'n2') and compid = @comp and empno = @empno update a set a.virtual_outcome = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --积分支付, 包括卡销售的积分支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110133' or code = '11020507') and compid = @comp and empno = @empno update a set a.pay_point = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --积分支付,只包括项目 + 产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '11020507') and compid = @comp and empno = @empno update a set a.pay_point_srv = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --积分支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110309') and compid = @comp and empno = @empno update a set a.pay_point_good = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --产品购买的现金 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110301' or code = '110310') -- 包括产品现金的还款 and compid = @comp and empno = @empno update a set a.cash_good = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --产品购买的银行卡 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110306' or code = '110311') -- 包括产品银行卡的还款 and compid = @comp and empno = @empno update a set a.credit_good = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --产品购买的支付宝 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '1103Z1' or code = '1103Z2') -- 包括产品支付宝的还款 and compid = @comp and empno = @empno update a set a.zfb_good = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --产品购买的微信 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '1103W1' or code = '1103W2') -- 包括产品微信的还款 and compid = @comp and empno = @empno update a set a.wx_good = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --产品购买的卡付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110302' and compid = @comp and empno = @empno update a set a.pay_card_good = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --取款的总额 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110119' or code = '110123' or code = 'drawother') and compid = @comp and empno = @empno update a set a.draw_total = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --收入都同利润分析表中的收入 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '11' -- 收入 and compid = @comp and empno = @empno update #stat_byday set revene = @amt where compid = @comp and empno = @empno --总现金流 modify by lm 2015.12.09 set @cash_sum = 0 select @cash_sum = sum(amt) from #tbl_profit_day where is_leaf = 1 and payment in (select pay from pay_classify where category = '1' and comp = @comp ) and kind <> 9 and compid = @comp and empno = @empno update a set a.cash_sum = isnull(@cash_sum, 0) from #stat_byday a where compid = @comp and empno = @empno delete from #emp_info where compid = @comp and empno = @empno end else begin set @state = 1 end end -- while 循环结束 update #stat_byday set cash_total = isnull(cash_card, 0) + isnull(cash_srv, 0) + isnull(cash_good, 0), cash_sum = cash_sum, credit_total = isnull(credit_card, 0) + isnull(credit_srv, 0) + isnull(credit_good, 0), zfb_total = isnull(zfb_card, 0) + isnull(zfb_srv, 0) + isnull(zfb_good, 0), wx_total = isnull(wx_card, 0) + isnull(wx_srv, 0) + isnull(wx_good, 0) -- 定义劳动业绩的比率 declare @cash_rate float -- 现金:1 declare @cash_good_rate float -- 现金:1 declare @credit_rate float -- 银行卡支付:6 declare @credit_good_rate float -- 银行卡支付:6 declare @pay3_rate float -- 支付方式:3 declare @pay3_good_rate float -- 支付方式:3 declare @pay$_rate float -- 支付方式:$ declare @pay$_good_rate float -- 支付方式:$ declare @pay0_rate float -- 内调:0 declare @pay0_good_rate float -- 内调:0 declare @manager_rate float -- 经理签单:8 declare @manager_good_rate float -- 经理签单:8 declare @point_rate float -- 积分支付:7 declare @point_good_rate float -- 积分支付:7 declare @user01_rate float -- 自定义支付:$1 declare @user01_good_rate float -- 自定义支付:$1 declare @user02_rate float -- 自定义支付:$2 declare @user02_good_rate float -- 自定义支付:$2 declare @user03_rate float -- 自定义支付:$3 declare @user03_good_rate float -- 自定义支付:$3 declare @user04_rate float -- 自定义支付:$4 declare @user04_good_rate float -- 自定义支付:$4 declare @user05_rate float -- 自定义支付:$5 declare @user05_good_rate float -- 自定义支付:$5 declare @zfb_rate float -- 支付宝支付 declare @zfb_good_rate float -- 支付宝支付 declare @wx_rate float -- 微信支付 declare @wx_good_rate float -- 微信支付 exec upg_get_workperf_rate @compid, @cash_rate output, @cash_good_rate output, @credit_rate output, @credit_good_rate output, @pay3_rate output, @pay3_good_rate output, @pay$_rate output, @pay$_good_rate output, @pay0_rate output, @pay0_good_rate output, @manager_rate output, @manager_good_rate output, @point_rate output, @point_good_rate output, @user01_rate output, @user01_good_rate output, @user02_rate output, @user02_good_rate output, @user03_rate output, @user03_good_rate output, @user04_rate output, @user04_good_rate output, @user05_rate output, @user05_good_rate output, @zfb_rate output, @zfb_good_rate output, @wx_rate output, @wx_good_rate output -- 增加劳动业绩的计算 declare @sp056 varchar(20) select @sp056 = gsa02c from gsm01 with (nolock) where gsa00c = @compid and gsa01c = 'SP056' if @sp056 = '4' -- 足疗 begin -- 足疗的劳动业绩 -- 劳动业绩 = 现金(服务) + 现金(产品) + 卡付(产品) + 卡付 + 抵用券 + 银行卡(服务) + 银行卡(产品) + 积分支付 update #stat_byday set work_perf = isnull(cash_srv, 0) * @cash_rate + isnull(cash_good, 0) * @cash_good_rate + isnull(pay_card_srv, 0) + isnull(pay_card_good, 0) + isnull(credit_srv, 0) * @credit_rate + isnull(credit_good, 0) * @credit_good_rate + isnull(zfb_srv, 0) * @zfb_rate + isnull(zfb_good, 0) * @zfb_good_rate + isnull(wx_srv, 0) * @wx_rate + isnull(wx_good, 0) * @wx_good_rate + (isnull(pay_coupon_srv, 0) - isnull(pay_coupon_good, 0)) * @pay3_rate + isnull(pay_coupon_good, 0) * @pay3_good_rate + (isnull(pay_point_srv, 0) - isnull(pay_point_good, 0)) * @point_rate + isnull(pay_point_good, 0) * @point_good_rate + (isnull(pay_user01_srv, 0) - isnull(pay_user01_good, 0)) * @user01_rate + isnull(pay_user01_good, 0) * @user01_good_rate + (isnull(pay_user02_srv, 0) - isnull(pay_user02_good, 0)) * @user02_rate + isnull(pay_user02_good, 0) * @user02_good_rate + (isnull(pay_user03_srv, 0) - isnull(pay_user03_good, 0)) * @user03_rate + isnull(pay_user03_good, 0) * @user03_good_rate + (isnull(pay_user04_srv, 0) - isnull(pay_user04_good, 0)) * @user04_rate + isnull(pay_user04_good, 0) * @user04_good_rate + (isnull(pay_user05_srv, 0) - isnull(pay_user05_good, 0)) * @user05_rate + isnull(pay_user05_good, 0) * @user05_good_rate end else begin -- 如果是按项目支付的情况下,采用卡付实际营业额,如单据支付则直接用卡付金额 -- 美发劳动业绩 declare @sp055f float select @sp055f = gsa03f from gsm01 with (nolock) where gsa00c = @incompid and gsa01c = 'SP055' if @sp055f = 1 begin -- 按单据支付 update #stat_byday set work_perf = isnull(cash_srv, 0) * @cash_rate + isnull(cash_good, 0) * @cash_good_rate + isnull(pay_card_srv, 0) + isnull(pay_card_good, 0) + isnull(credit_srv, 0) * @credit_rate + isnull(credit_good, 0) * @credit_good_rate + isnull(zfb_srv, 0) * @zfb_rate + isnull(zfb_good, 0) * @zfb_good_rate + isnull(wx_srv, 0) * @wx_rate + isnull(wx_good, 0) * @wx_good_rate + (isnull(pay_coupon_srv, 0) - isnull(pay_coupon_good, 0)) * @pay3_rate + isnull(pay_coupon_good, 0) * @pay3_good_rate + (isnull(pay_coupon2_srv, 0) - isnull(pay_coupon2_good, 0)) * @pay$_rate + isnull(pay_coupon2_good, 0) * @pay$_good_rate + (isnull(pay_coupon3_srv, 0) - isnull(pay_coupon3_good, 0)) * @pay0_rate + isnull(pay_coupon3_good, 0) * @pay0_good_rate + (isnull(pay_manager_prj, 0) - isnull(pay_manager_good, 0)) * @manager_rate + isnull(pay_manager_good, 0) * @manager_good_rate + (isnull(pay_point_srv, 0) - isnull(pay_point_good, 0)) * @point_rate + isnull(pay_point_good, 0) * @point_good_rate + (isnull(pay_user01_srv, 0) - isnull(pay_user01_good, 0)) * @user01_rate + isnull(pay_user01_good, 0) * @user01_good_rate + (isnull(pay_user02_srv, 0) - isnull(pay_user02_good, 0)) * @user02_rate + isnull(pay_user02_good, 0) * @user02_good_rate + (isnull(pay_user03_srv, 0) - isnull(pay_user03_good, 0)) * @user03_rate + isnull(pay_user03_good, 0) * @user03_good_rate + (isnull(pay_user04_srv, 0) - isnull(pay_user04_good, 0)) * @user04_rate + isnull(pay_user04_good, 0) * @user04_good_rate + (isnull(pay_user05_srv, 0) - isnull(pay_user05_good, 0)) * @user05_rate + isnull(pay_user05_good, 0) * @user05_good_rate end else begin -- 劳动业绩 = 现金(服务) + 现金(产品) + 卡付(产品) + 卡付实营业额 + 经理签单 + 内调 + 抵用券 + 银行卡(服务) + 银行卡(产品) + 积分支付 update #stat_byday set work_perf = isnull(cash_srv, 0) * @cash_rate + isnull(cash_good, 0) * @cash_good_rate + isnull(pay_card_srv_work, 0) + isnull(credit_srv, 0) * @credit_rate + isnull(credit_good, 0) * @credit_good_rate + isnull(zfb_srv, 0) * @zfb_rate + isnull(zfb_good, 0) * @zfb_good_rate + isnull(wx_srv, 0) * @wx_rate + isnull(wx_good, 0) * @wx_good_rate + (isnull(pay_coupon_srv, 0) - isnull(pay_coupon_good, 0)) * @pay3_rate + isnull(pay_coupon_good, 0) * @pay3_good_rate + (isnull(pay_coupon2_srv, 0) - isnull(pay_coupon2_good, 0)) * @pay$_rate + isnull(pay_coupon2_good, 0) * @pay$_good_rate + (isnull(pay_coupon3_srv, 0) - isnull(pay_coupon3_good, 0)) * @pay0_rate + isnull(pay_coupon3_good, 0) * @pay0_good_rate + (isnull(pay_manager_prj, 0) - isnull(pay_manager_good, 0)) * @manager_rate + isnull(pay_manager_good, 0) * @manager_good_rate + (isnull(pay_point_srv, 0) - isnull(pay_point_good, 0)) * @point_rate + isnull(pay_point_good, 0) * @point_good_rate + (isnull(pay_user01_srv, 0) - isnull(pay_user01_good, 0)) * @user01_rate + isnull(pay_user01_good, 0) * @user01_good_rate + (isnull(pay_user02_srv, 0) - isnull(pay_user02_good, 0)) * @user02_rate + isnull(pay_user02_good, 0) * @user02_good_rate + (isnull(pay_user03_srv, 0) - isnull(pay_user03_good, 0)) * @user03_rate + isnull(pay_user03_good, 0) * @user03_good_rate + (isnull(pay_user04_srv, 0) - isnull(pay_user04_good, 0)) * @user04_rate + isnull(pay_user04_good, 0) * @user04_good_rate + (isnull(pay_user05_srv, 0) - isnull(pay_user05_good, 0)) * @user05_rate + isnull(pay_user05_good, 0) * @user05_good_rate end end update #stat_byday set profit = isnull(revene, 0) + isnull(other_income, 0) - isnull(outcome, 0) --Start增加疗程销售,消耗 add by lm 2015.10.21 create table #prj_stock_buf ( compid varchar(20) not null, empno varchar(20) not null, prjnum float null, -- 疗程的数量 prjamt float null, -- 疗程金额 primary key(compid, empno) ) create table #prj_stock_buf1 ( compid varchar(20) not null, empno varchar(20) not null, prjnum float null, -- 疗程的数量 prjamt float null, -- 疗程金额 ) create nonclustered index index_prj_stock_buf1 on #prj_stock_buf1 (compid,empno) create table #prj_consume ( compid varchar(20) not null, empno varchar(20) not null, prjnum float null, -- 疗程的数量 prjamt float null, -- 疗程金额 primary key(compid, empno) ) create table #prj_consume1 ( compid varchar(20) not null, empno varchar(20) not null, prjnum float null, -- 疗程的数量 prjamt float null, -- 疗程金额 ) create nonclustered index index_prj_consume1on on #prj_consume1 (compid,empno) create table #gcm10 ( compid varchar(20) not null, empno varchar(20) not null, billid varchar(20) not null,--充值单号 primary key(compid,empno,billid) ) create table #gnm01 ( compid varchar(20) not null, empno varchar(20) not null, billid varchar(20) not null,--充值单号 primary key(compid,empno,billid) ) create table #ggm01 ( compid varchar(20) not null, empno varchar(20) not null, billid varchar(20) not null,--充值单号 primary key(compid,empno,billid) ) insert #gcm10(compid, billid,empno) select distinct gcl00c, gcl01c,gcl81c from gcm10 a with (nolock), gam26 z with (nolock) where a.gcl00c = z.gaz02c and z.gaz01c = @compid and ((a.gcl02d >= @fromdate and a.gcl02d <= @todate) or @fromdate = '*') and a.gcl93c like @staff and (gcl81c >= @fromdepart and gcl81c <= @todepart) union all select distinct gcl00c, gcl01c,gcl82c from gcm10 a with (nolock), gam26 z with (nolock) where a.gcl00c = z.gaz02c and z.gaz01c = @compid and ((a.gcl02d >= @fromdate and a.gcl02d <= @todate) or @fromdate = '*') and a.gcl93c like @staff and (gcl82c >= @fromdepart and gcl82c <= @todepart) union all select distinct gcl00c, gcl01c,gcl83c from gcm10 a with (nolock), gam26 z with (nolock) where a.gcl00c = z.gaz02c and z.gaz01c = @compid and ((a.gcl02d >= @fromdate and a.gcl02d <= @todate) or @fromdate = '*') and a.gcl93c like @staff and (gcl83c >= @fromdepart and gcl83c <= @todepart) union all select distinct gcl00c, gcl01c,gcl100c from gcm10 a with (nolock), gam26 z with (nolock) where a.gcl00c = z.gaz02c and z.gaz01c = @compid and ((a.gcl02d >= @fromdate and a.gcl02d <= @todate) or @fromdate = '*') and a.gcl93c like @staff and (gcl100c >= @fromdepart and gcl100c <= @todepart) union all select distinct gcl00c, gcl01c,gcl101c from gcm10 a with (nolock), gam26 z with (nolock) where a.gcl00c = z.gaz02c and z.gaz01c = @compid and ((a.gcl02d >= @fromdate and a.gcl02d <= @todate) or @fromdate = '*') and a.gcl93c like @staff and (gcl101c >= @fromdepart and gcl101c <= @todepart) union all select distinct gcl00c, gcl01c,gcl102c from gcm10 a with (nolock), gam26 z with (nolock) where a.gcl00c = z.gaz02c and z.gaz01c = @compid and ((a.gcl02d >= @fromdate and a.gcl02d <= @todate) or @fromdate = '*') and a.gcl93c like @staff and (gcl102c >= @fromdepart and gcl102c <= @todepart) union all select distinct gcl00c, gcl01c,gcl103c from gcm10 a with (nolock), gam26 z with (nolock) where a.gcl00c = z.gaz02c and z.gaz01c = @compid and ((a.gcl02d >= @fromdate and a.gcl02d <= @todate) or @fromdate = '*') and a.gcl93c like @staff and (gcl103c >= @fromdepart and gcl103c <= @todepart) union all select distinct gcl00c, gcl01c,gcl104c from gcm10 a with (nolock), gam26 z with (nolock) where a.gcl00c = z.gaz02c and z.gaz01c = @compid and ((a.gcl02d >= @fromdate and a.gcl02d <= @todate) or @fromdate = '*') and a.gcl93c like @staff and (gcl104c >= @fromdepart and gcl104c <= @todepart) union all select distinct gcl00c, gcl01c,gcl105c from gcm10 a with (nolock), gam26 z with (nolock) where a.gcl00c = z.gaz02c and z.gaz01c = @compid and ((a.gcl02d >= @fromdate and a.gcl02d <= @todate) or @fromdate = '*') and a.gcl93c like @staff and (gcl105c >= @fromdepart and gcl105c <= @todepart) union all select distinct gcl00c, gcl01c,gcl106c from gcm10 a with (nolock), gam26 z with (nolock) where a.gcl00c = z.gaz02c and z.gaz01c = @compid and ((a.gcl02d >= @fromdate and a.gcl02d <= @todate) or @fromdate = '*') and a.gcl93c like @staff and (gcl106c >= @fromdepart and gcl106c <= @todepart) insert #gnm01(compid, billid, empno) select distinct gna00c, gna01c, gna81c from gnm01 a with (nolock), gam26 z with (nolock) where a.gna00c = z.gaz02c and z.gaz01c = @compid and ((a.gna02d >= @fromdate and a.gna02d <= @todate) or @fromdate = '*') and a.gna93c like @staff and (gna81c >= @fromdepart and gna81c <= @todepart) union all select distinct gna00c, gna01c, gna82c from gnm01 a with (nolock), gam26 z with (nolock) where a.gna00c = z.gaz02c and z.gaz01c = @compid and ((a.gna02d >= @fromdate and a.gna02d <= @todate) or @fromdate = '*') and a.gna93c like @staff and (gna82c >= @fromdepart and gna82c <= @todepart) union all select distinct gna00c, gna01c, gna83c from gnm01 a with (nolock), gam26 z with (nolock) where a.gna00c = z.gaz02c and z.gaz01c = @compid and ((a.gna02d >= @fromdate and a.gna02d <= @todate) or @fromdate = '*') and a.gna93c like @staff and (gna83c >= @fromdepart and gna83c <= @todepart) union all select distinct gna00c, gna01c, gna100c from gnm01 a with (nolock), gam26 z with (nolock) where a.gna00c = z.gaz02c and z.gaz01c = @compid and ((a.gna02d >= @fromdate and a.gna02d <= @todate) or @fromdate = '*') and a.gna93c like @staff and (gna100c >= @fromdepart and gna100c <= @todepart) union all select distinct gna00c, gna01c, gna101c from gnm01 a with (nolock), gam26 z with (nolock) where a.gna00c = z.gaz02c and z.gaz01c = @compid and ((a.gna02d >= @fromdate and a.gna02d <= @todate) or @fromdate = '*') and a.gna93c like @staff and (gna101c >= @fromdepart and gna101c <= @todepart) union all select distinct gna00c, gna01c, gna102c from gnm01 a with (nolock), gam26 z with (nolock) where a.gna00c = z.gaz02c and z.gaz01c = @compid and ((a.gna02d >= @fromdate and a.gna02d <= @todate) or @fromdate = '*') and a.gna93c like @staff and (gna102c >= @fromdepart and gna102c <= @todepart) union all select distinct gna00c, gna01c, gna103c from gnm01 a with (nolock), gam26 z with (nolock) where a.gna00c = z.gaz02c and z.gaz01c = @compid and ((a.gna02d >= @fromdate and a.gna02d <= @todate) or @fromdate = '*') and a.gna93c like @staff and (gna103c >= @fromdepart and gna103c <= @todepart) union all select distinct gna00c, gna01c, gna104c from gnm01 a with (nolock), gam26 z with (nolock) where a.gna00c = z.gaz02c and z.gaz01c = @compid and ((a.gna02d >= @fromdate and a.gna02d <= @todate) or @fromdate = '*') and a.gna93c like @staff and (gna104c >= @fromdepart and gna104c <= @todepart) union all select distinct gna00c, gna01c, gna105c from gnm01 a with (nolock), gam26 z with (nolock) where a.gna00c = z.gaz02c and z.gaz01c = @compid and ((a.gna02d >= @fromdate and a.gna02d <= @todate) or @fromdate = '*') and a.gna93c like @staff and (gna105c >= @fromdepart and gna105c <= @todepart) union all select distinct gna00c, gna01c, gna106c from gnm01 a with (nolock), gam26 z with (nolock) where a.gna00c = z.gaz02c and z.gaz01c = @compid and ((a.gna02d >= @fromdate and a.gna02d <= @todate) or @fromdate = '*') and a.gna93c like @staff and (gna106c >= @fromdepart and gna106c <= @todepart) insert #ggm01(compid, billid, empno) select distinct gga00c, gga01c,ggb81c from ggm01 a with (nolock), ggm02 b, gam26 z with (nolock) where a.gga00c = b.ggb00c and a.gga01c = b.ggb01c and a.gga00c = z.gaz02c and z.gaz01c = @compid and a.gga93c like @staff and ((a.gga02d >= @fromdate and a.gga02d <= @todate) or @fromdate = '*') and (ggb81c >= @fromdepart and ggb81c <= @todepart) -- 这个时间点范围内销售的疗程 insert #prj_stock_buf1(compid, empno, prjnum, prjamt) select compid, empno, sum(gch05f + gch06f), sum(gch07f) from #gnm01 a, gcm08 b with (nolock) where a.compid = b.gch00c and a.billid = b.gch11c and b.gch10c = 'gn' and ((b.gch02d >= @fromdate and b.gch02d <= @todate) or @fromdate = '*') group by compid, empno order by compid, empno insert #prj_stock_buf1(compid, empno, prjnum, prjamt) select compid, empno, sum(gch05f + gch06f), sum(gch07f) from #gcm10 a, gcm08 b with (nolock) where a.compid = b.gch00c and a.billid = b.gch11c and b.gch10c = 'gz' and ((b.gch02d >= @fromdate and b.gch02d <= @todate) or @fromdate = '*') group by compid, empno order by compid, empno --合计 insert #prj_stock_buf (compid, empno, prjnum, prjamt) select compid, empno, sum(prjnum), sum(prjamt) from #prj_stock_buf1 group by compid, empno insert #prj_consume1 (compid, empno, prjnum, prjamt) select compid, empno, sum(gch08f), sum(gch09f) from #ggm01 a, gcm08 b with (nolock) where a.compid = b.gch00c and a.billid = b.gch11c and b.gch10c = 'gx' and ((b.gch02d >= @fromdate and b.gch02d <= @todate) or @fromdate = '*') group by compid, empno order by compid, empno --合计 insert #prj_consume (compid, empno, prjnum, prjamt) select compid, empno, sum(prjnum), sum(prjamt) from #prj_consume1 group by compid, empno --疗程销售,消耗金额,数量赋值 update a set a.prj_quan_buy = isnull(b.prjnum,0), a.prj_amt_buy = isnull(b.prjamt,0) from #stat_byday a, #prj_stock_buf b where a.compid = b.compid and b.empno = b.empno update a set a.prj_quan_consume = isnull(b.prjnum,0), a.prj_amt_consume = isnull(b.prjamt,0) from #stat_byday a, #prj_consume b where a.compid = b.compid and b.empno = b.empno --End select isnull(compid,'') compid, isnull(cash_card,0) cash_card, isnull(cash_srv,0) cash_srv, isnull(cash_good,0) cash_good, isnull(cash_total,0) cash_total, isnull(credit_card,0) credit_card, isnull(credit_srv,0) credit_srv, isnull(credit_good,0) credit_good, isnull(credit_total,0) credit_total, isnull(zfb_card,0) zfb_card, isnull(zfb_srv,0) zfb_srv, isnull(zfb_good,0) zfb_good, isnull(zfb_total,0) zfb_total, isnull(wx_card,0) wx_card, isnull(wx_srv,0) wx_srv, isnull(wx_good,0) wx_good, isnull(wx_total,0) wx_total, isnull(revene,0) revene, isnull(outcome,0) outcome, isnull(profit,0) profit, isnull(defict,0) defict, isnull(returnamt,0) returnamt, isnull(pay_card_srv,0) pay_card_srv, isnull(pay_card_good,0) pay_card_good, isnull(pay_coupon,0) pay_coupon, isnull(pay_coupon2,0) pay_coupon2, isnull(pay_coupon3,0) pay_coupon3, isnull(pay_manager,0) pay_manager, isnull(sale_card_total,0) sale_card_total, isnull(pay_card_srv_act,0) pay_card_srv_act, isnull(pay_card_srv_work,0) pay_card_srv_work, isnull(pay_coupon_srv,0) pay_coupon_srv, isnull(pay_coupon2_srv,0) pay_coupon2_srv, isnull(pay_coupon3_srv,0) pay_coupon3_srv, isnull(draw_total,0) draw_total, isnull(work_perf,0) work_perf, isnull(cust_quan,0) cust_quan, isnull(proj_quan,0) proj_quan, isnull(pay_manager_prj,0) pay_manager_prj, isnull(pay_manager_good,0) pay_manager_good, isnull(pay_coupon_good,0) pay_coupon_good, isnull(pay_coupon2_good,0) pay_coupon2_good, isnull(pay_coupon3_good,0) pay_coupon3_good, isnull(pay_point,0) pay_point, isnull(pay_point_srv,0) pay_point_srv, isnull(pay_point_good,0) pay_point_good, isnull(pay_user01,0) pay_user01, isnull(pay_user01_srv,0) pay_user01_srv, isnull(pay_user01_good,0) pay_user01_good, isnull(pay_user02,0) pay_user02, isnull(pay_user02_srv,0) pay_user02_srv, isnull(pay_user02_good,0) pay_user02_good, isnull(pay_user03,0) pay_user03, isnull(pay_user03_srv,0) pay_user03_srv, isnull(pay_user03_good,0) pay_user03_good, isnull(pay_user04,0) pay_user04, isnull(pay_user04_srv,0) pay_user04_srv, isnull(pay_user04_good,0) pay_user04_good, isnull(pay_user05,0) pay_user05, isnull(pay_user05_srv,0) pay_user05_srv, isnull(pay_user05_good,0) pay_user05_good, isnull(other_income,0) other_income, isnull(virtual_income,0) virtual_income, isnull(virtual_outcome,0) virtual_outcome, isnull(prj_quan_buy,0) prj_quan_buy, isnull(prj_amt_buy,0) prj_amt_buy, isnull(prj_quan_consume,0) prj_quan_consume, isnull(prj_amt_consume,0) prj_amt_consume, isnull(cash_sum,0) cash_sum, isnull(empname,'') empname, isnull(empno, '') empno from #stat_byday union all select isnull(compid,'') compid, isnull(sum(cash_card),0) cash_card, isnull(sum(cash_srv),0) cash_srv, isnull(sum(cash_good),0) cash_good, isnull(sum(cash_total),0) cash_total, isnull(sum(credit_card),0) credit_card, isnull(sum(credit_srv),0) credit_srv, isnull(sum(credit_good),0) credit_good, isnull(sum(credit_total),0) credit_total, isnull(sum(zfb_card),0) zfb_card, isnull(sum(zfb_srv),0) zfb_srv, isnull(sum(zfb_good),0) zfb_good, isnull(sum(zfb_total),0) zfb_total, isnull(sum(wx_card),0) wx_card, isnull(sum(wx_srv),0) wx_srv, isnull(sum(wx_good),0) wx_good, isnull(sum(wx_total),0) wx_total, isnull(sum(revene),0) revene, isnull(sum(outcome),0) outcome, isnull(sum(profit),0) profit, isnull(sum(defict),0) defict, isnull(sum(returnamt),0) returnamt, isnull(sum(pay_card_srv),0) pay_card_srv, isnull(sum(pay_card_good),0) pay_card_good, isnull(sum(pay_coupon),0) pay_coupon, isnull(sum(pay_coupon2),0) pay_coupon2, isnull(sum(pay_coupon3),0) pay_coupon3, isnull(sum(pay_manager),0) pay_manager, isnull(sum(sale_card_total),0) sale_card_total, isnull(sum(pay_card_srv_act),0) pay_card_srv_act, isnull(sum(pay_card_srv_work),0) pay_card_srv_work, isnull(sum(pay_coupon_srv),0) pay_coupon_srv, isnull(sum(pay_coupon2_srv),0) pay_coupon2_srv, isnull(sum(pay_coupon3_srv),0) pay_coupon3_srv, isnull(sum(draw_total),0) draw_total, isnull(sum(work_perf),0) work_perf, isnull(sum(cust_quan),0) cust_quan, isnull(sum(proj_quan),0) proj_quan, isnull(sum(pay_manager_prj),0) pay_manager_prj, isnull(sum(pay_manager_good),0) pay_manager_good, isnull(sum(pay_coupon_good),0) pay_coupon_good, isnull(sum(pay_coupon2_good),0) pay_coupon2_good, isnull(sum(pay_coupon3_good),0) pay_coupon3_good, isnull(sum(pay_point),0) pay_point, isnull(sum(pay_point_srv),0) pay_point_srv, isnull(sum(pay_point_good),0) pay_point_good, isnull(sum(pay_user01),0) pay_user01, isnull(sum(pay_user01_srv),0) pay_user01_srv, isnull(sum(pay_user01_good),0) pay_user01_good, isnull(sum(pay_user02),0) pay_user02, isnull(sum(pay_user02_srv),0) pay_user02_srv, isnull(sum(pay_user02_good),0) pay_user02_good, isnull(sum(pay_user03),0) pay_user03, isnull(sum(pay_user03_srv),0) pay_user03_srv, isnull(sum(pay_user03_good),0) pay_user03_good, isnull(sum(pay_user04),0) pay_user04, isnull(sum(pay_user04_srv),0) pay_user04_srv, isnull(sum(pay_user04_good),0) pay_user04_good, isnull(sum(pay_user05),0) pay_user05, isnull(sum(pay_user05_srv),0) pay_user05_srv, isnull(sum(pay_user05_good),0) pay_user05_good, isnull(sum(other_income),0) other_income, isnull(sum(virtual_income),0) virtual_income, isnull(sum(virtual_outcome),0) virtual_outcome, isnull(sum(prj_quan_buy),0) prj_quan_buy, isnull(sum(prj_amt_buy),0) prj_amt_buy, isnull(sum(prj_quan_consume),0) prj_quan_consume, isnull(sum(prj_amt_consume),0) prj_amt_consume, isnull(sum(cash_sum),0) cash_sum, '' empname, '' empno from #stat_byday group by compid drop table #stat_byday drop table #tbl_profit_day drop table #gcm10 drop table #gnm01 drop table #ggm01 drop table #prj_stock_buf drop table #prj_stock_buf1 drop table #prj_consume drop table #prj_consume1 drop table #cust_num_by_day drop table #emp_info end GO /****** Object: StoredProcedure [dbo].[upg_stat_byday_depart_emp_nochain] Script Date: 06/12/2017 06:20:42 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_stat_byday_depart_emp_nochain] ( @compid varchar(10), @fromdate varchar(10), @todate varchar(10), @flag int, -- 0-每天一纪录 1-返回一条记录 @staff varchar(20), @fromdepart varchar(10), -- add by lyj 2007-09-06 @todepart varchar(10), @calmode int -- 0-营业款 1-营业收入 2-综合小单 ) as begin declare @cur_date datetime declare @strtmp varchar(30) declare @str_date varchar(20) declare @amt float declare @tmpamt float declare @cash_sum float declare @qry_compid varchar(20) declare @incompid varchar(10) select @incompid = @compid select @qry_compid = @compid set @cur_date = convert(datetime, @fromdate) create table #tbl_profit_day ( idd int not null, code varchar(20) not null, compid varchar(20) not null, empno varchar(20) not null, itemname varchar(60) null, is_add int null, amt float null, is_leaf int null, payment varchar(10) null, -- 支付方式 kind int null -- 0 充值 1卡销售 9产品销售 2 服务 -1 取款 8 保证金收入 6还款 ) create clustered index idx_tbl_profit_day on #tbl_profit_day(compid, empno,code) create table #emp_info --add by lm 2016.06.21 ( empno varchar(20) not null, empname varchar(20) null, compid varchar(20) not null primary key (compid, empno) ) insert #emp_info(compid, empno,empname) select gsb00c, gsb02c,gsb03c from gsm02 a with(nolock) where a.gsb00c = @compid and a.gsb01c = 'U' and ((a.gsb02c between @fromdepart and @todepart )or @fromdepart = '*') create table #stat_byday ( compid varchar(20) not null, empno varchar(20) not null, cash_card float null, -- 卡销售-现金 cash_srv float null, cash_good float null, cash_total float null, credit_card float null, credit_srv float null, credit_good float null, credit_total float null, zfb_card float null, zfb_srv float null, zfb_good float null, zfb_total float null, wx_card float null, wx_srv float null, wx_good float null, wx_total float null, revene float null, outcome float null, profit float null, defict float null, returnamt float null, pay_card_srv float null, -- 卡付中,已经不包括积分支付了 pay_card_good float null, -- 卡付,产品,不包括积分支付 pay_coupon float null, -- 券付 支付方式3 pay_coupon2 float null, -- 贵付 支付方式$ pay_coupon3 float null, -- 洗头 支付方式内调 pay_manager float null, -- 经理签单 sale_card_total float null, -- 总销售卡 pay_card_srv_act float null, -- 卡付实际营业额 = 在服务项目中,那些卡付的金额转换为实际营业额 pay_card_srv_work float null, -- 卡付实际劳动业绩 = 在服务项目 + 产品中,那些卡付的金额转换为劳动业绩 pay_coupon_srv float null, -- 券付 支付方式3, 只包括项目 pay_coupon2_srv float null, -- 贵付 支付方式$, 只包括项目 pay_coupon3_srv float null, -- 洗头 支付方式内调, 只包括项目 draw_total float null, -- 取款总额(包含现金以及内调) work_perf float null, -- 劳动业绩 = 现金(服务) + 现金(产品) + 卡付(产品) + 卡付实营业额 + 经理签单 + 内调 + 抵用券 + 银行卡(服务) + 银行卡(产品) cust_quan float null, -- 客人数量 proj_quan float null, -- 项目数 pay_manager_prj float null, --只包括项目的经理签单 pay_manager_good float null, --只包括产品的经理签单 pay_coupon_good float null, -- 券付 支付方式3, 只包括产品 pay_coupon2_good float null, -- 贵付 支付方式$, 只包括产品 pay_coupon3_good float null, -- 洗头 支付方式内调, 只包括产品 pay_point float null, -- 积分支付,卡销售,项目,产品 pay_point_srv float null, -- 积分支付,只包括项目(含产品) pay_point_good float null, -- 积分支付,只有产品 pay_user01 float null, -- 自定义支付,卡销售,项目,产品 pay_user01_srv float null, -- 自定义支付,只包括项目(含产品) pay_user01_good float null, -- 自定义支付,只有产品 pay_user02 float null, -- 自定义支付,卡销售,项目,产品 pay_user02_srv float null, -- 自定义支付,只包括项目(含产品) pay_user02_good float null, -- 自定义支付,只有产品 pay_user03 float null, -- 自定义支付,卡销售,项目,产品 pay_user03_srv float null, -- 自定义支付,只包括项目(含产品) pay_user03_good float null, -- 自定义支付,只有产品 pay_user04 float null, -- 自定义支付,卡销售,项目,产品 pay_user04_srv float null, -- 自定义支付,只包括项目(含产品) pay_user04_good float null, -- 自定义支付,只有产品 pay_user05 float null, -- 自定义支付,卡销售,项目,产品 pay_user05_srv float null, -- 自定义支付,只包括项目(含产品) pay_user05_good float null, -- 自定义支付,只有产品 other_income float null, -- 其他收入 Huzy 2012/05/31 virtual_income float null, --虚拟收入 virtual_outcome float null, --虚拟支出 prj_quan_buy float default 0 null, -- 疗程销售次数 prj_amt_buy float default 0 null, -- 疗程销售金额 prj_quan_consume float default 0 null, -- 疗程消耗次数 prj_amt_consume float default 0 null, -- 疗程消耗金额 cash_sum float default 0 null, -- 现金类金额 empname varchar(40) primary key(compid, empno)) --create nonclustered index index_stat_byday on #stat_byday(compid, empno) insert #tbl_profit_day(compid, empno, idd, code, itemname, is_add, amt, is_leaf, payment, kind) exec upg_analyse_profit_inter_depart_emp_nochain @compid, @fromdate, @todate, 1, @staff, @fromdepart, @todepart, @calmode --add by lyj 2007-09-06 --统计客户数量 create table #cust_num_by_day( compid varchar(20) not null, -- 公司 cust_num float null, -- 数量 primary key(compid)) insert #cust_num_by_day(compid, cust_num) select gga00c, sum(isnull(gga14f, 1)) from ggm01 with(nolock) where gga00c like @qry_compid and gga80d >= @fromdate and gga80d <= @todate group by gga00c declare @state int set @state = 0 declare @comp varchar(20) declare @empno varchar(20) declare @empname varchar(40) if not exists(select 1 from #tbl_profit_day) set @state = 1 while (@state = 0) begin if exists(select 1 from #emp_info ) begin select top 1 @comp = compid, @empno = empno, @empname = empname from #emp_info --where compid = '001' insert #stat_byday(compid, empno,empname) select @comp,@empno,@empname update a set a.cust_quan = b.cust_num from #stat_byday a, #cust_num_by_day b where a.compid = b.compid --转换为汇总表需要的一条记录 set @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = 'HB' and compid = @comp and empno = @empno update a set a.proj_quan = isnull(@amt, 0) from #stat_byday a where a.compid = @comp and a.empno = @empno --现金(卖卡) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110101' or code = '110104' or code = '110107' or code = '110118' or code = '110140') and compid = @comp and empno = @empno --现金取款 select @tmpamt = 0 select @tmpamt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110119' and compid = @comp and empno = @empno update a set a.cash_card = isnull(@amt, 0) + isnull(@tmpamt, 0) from #stat_byday a where a.compid = @comp and a.empno = @empno --现金(服务) --在按项目支付的情况下,是正确的项目的现金支付 --在单据支付的情况下,实际上包括了产品销售的现金支付 select @amt = 0 select @tmpamt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110201' or code = '110230')-- 项目或物品的现金还款也算 还款的不算在 and compid = @comp and empno = @empno select @tmpamt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110301' or code = '110310') -- 在单据支付的情况,这个值为0, 包括了产品的现金还款 and compid = @comp and empno = @empno update a set a.cash_srv = isnull(@amt, 0) - isnull(@tmpamt, 0) from #stat_byday a where compid = @comp and empno = @empno --银行卡(卖卡) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110102' or code = '110105' or code = '110108' or code = '110141') and compid = @comp and empno = @empno update a set a.credit_card = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --支付宝(卖卡) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '1101Z1' or code = '1101Z2' or code = '1101Z3' or code = '1101Z4') and compid = @comp and empno = @empno update a set a.zfb_card = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --微信(卖卡) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '1101W1' or code = '1101W2' or code = '1101W3' or code = '1101W4') and compid = @comp and empno = @empno update a set a.wx_card = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --不管支付方式,实际上的卡销售额 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '1101' and compid = @comp and empno = @empno update a set a.sale_card_total = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --银行卡(服务) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110202' and compid = @comp and empno = @empno select @tmpamt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110306' or code = '110311') -- 产品销售的银行卡支付 以及 还款 and compid = @comp and empno = @empno update a set a.credit_srv = isnull(@amt, 0) - isnull(@tmpamt, 0) from #stat_byday a where compid = @comp and empno = @empno --支付宝(服务) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '1102Z1' and compid = @comp and empno = @empno select @tmpamt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '1103Z2' or code = '1103Z1') -- 产品销售的银行卡支付 以及 还款 and compid = @comp and empno = @empno update a set a.zfb_srv = isnull(@amt, 0) - isnull(@tmpamt, 0) from #stat_byday a where compid = @comp and empno = @empno --微信(服务) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '1102W1' and compid = @comp and empno = @empno select @tmpamt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '1103W2' or code = '1103W1') -- 产品销售的微信支付 以及 还款 and compid = @comp and empno = @empno update a set a.wx_srv = isnull(@amt, 0) - isnull(@tmpamt, 0) from #stat_byday a where compid = @comp and empno = @empno --支出 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '12' and compid = @comp and empno = @empno select @tmpamt = 0 select @tmpamt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '1299' and compid = @comp and empno = @empno update a set a.outcome = isnull(@amt, 0) - isnull(@tmpamt, 0) from #stat_byday a where compid = @comp and empno = @empno --欠款 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110103' or code = '110106' or code = '110207' or code = '110145') and compid = @comp and empno = @empno update a set a.defict = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --还款 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where kind = 6 and is_leaf = 1 and compid = @comp and empno = @empno update a set a.returnamt = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --卡付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110205' and compid = @comp and empno = @empno select @tmpamt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110302' and compid = @comp and empno = @empno update a set a.pay_card_srv = isnull(@amt, 0) - isnull(@tmpamt, 0) from #stat_byday a where compid = @comp and empno = @empno --卡付实际营业额 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110206' and compid = @comp and empno = @empno update a set a.pay_card_srv_act = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '11AA01' and compid = @comp and empno = @empno update a set a.pay_card_srv_work = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --抵用券付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110204' or code = '110125' or code = '110126' or code = '110127') and compid = @comp and empno = @empno update a set a.pay_coupon = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --抵用券付, 只包括项目 + 产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110204') and compid = @comp and empno = @empno update a set a.pay_coupon_srv = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --抵用券付, 只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110303') and compid = @comp and empno = @empno update a set a.pay_coupon_good = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --贵付(抵用券二) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110208' or code = '110130' or code = '110131' or code = '110132') and compid = @comp and empno = @empno update a set a.pay_coupon2 = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --贵付(抵用券二), 只包括项目 + 产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110208') and compid = @comp and empno = @empno update a set a.pay_coupon2_srv = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --贵付(抵用券二), 只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110304') and compid = @comp and empno = @empno update a set a.pay_coupon2_good = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --内调(洗头) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where ((code = '110209' or code = '110120' or code = '110121' or code = '110122' or code = '1101400') or (kind = 6 and payment = '0' )) and compid = @comp and empno = @empno update a set a.pay_coupon3 = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --内调(洗头), 只包括项目 + 产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110209') and compid = @comp and empno = @empno update a set a.pay_coupon3_srv = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --内调(洗头), 只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110308') and compid = @comp and empno = @empno update a set a.pay_coupon3_good = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --经理签单, 包括卡销售的经理签单 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110110' or code = '110111' or code = '110203') and compid = @comp and empno = @empno update a set a.pay_manager = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --经理签单,只包括项目 + 产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110203') and compid = @comp and empno = @empno update a set a.pay_manager_prj = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --经理签单,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110305') and compid = @comp and empno = @empno update a set a.pay_manager_good = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --自定义支付, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$1' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8) and compid = @comp and empno = @empno update a set a.pay_user01 = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$1' and (kind = 2) and compid = @comp and empno = @empno update a set a.pay_user01_srv = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$1' and (kind = 9) and compid = @comp and empno = @empno update a set a.pay_user01_good = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --自定义支付, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$2' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8) and compid = @comp and empno = @empno update a set a.pay_user02 = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$2' and (kind = 2) and compid = @comp and empno = @empno update a set a.pay_user02_srv = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$2' and (kind = 9) and compid = @comp and empno = @empno update a set a.pay_user02_good = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --自定义支付, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$3' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8) and compid = @comp and empno = @empno update a set a.pay_user03 = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$3' and (kind = 2) and compid = @comp and empno = @empno update a set a.pay_user03_srv = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$3' and (kind = 9) and compid = @comp and empno = @empno update a set a.pay_user03_good = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --自定义支付, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$4' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8) and compid = @comp and empno = @empno update a set a.pay_user04 = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$4' and (kind = 2) and compid = @comp and empno = @empno update a set a.pay_user04_srv = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$4' and (kind = 9) and compid = @comp and empno = @empno update a set a.pay_user04_good = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --自定义支付, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$5' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8) and compid = @comp and empno = @empno update a set a.pay_user05 = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$5' and (kind = 2) and compid = @comp and empno = @empno update a set a.pay_user05_srv = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$5' and (kind = 9) and compid = @comp and empno = @empno update a set a.pay_user05_good = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --其他收入 Huzy 2012/05/31 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '18') and compid = @comp and empno = @empno update a set a.other_income = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --虚拟收入virtual_income select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = 'n1') and compid = @comp and empno = @empno update a set a.virtual_income = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --虚拟支出virtual_outcome select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = 'n2') and compid = @comp and empno = @empno update a set a.virtual_outcome = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --积分支付, 包括卡销售的积分支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110133' or code = '11020507') and compid = @comp and empno = @empno update a set a.pay_point = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --积分支付,只包括项目 + 产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '11020507') and compid = @comp and empno = @empno update a set a.pay_point_srv = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --积分支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110309') and compid = @comp and empno = @empno update a set a.pay_point_good = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --产品购买的现金 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110301' or code = '110310') -- 包括产品现金的还款 and compid = @comp and empno = @empno update a set a.cash_good = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --产品购买的银行卡 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110306' or code = '110311') -- 包括产品银行卡的还款 and compid = @comp and empno = @empno update a set a.credit_good = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --产品购买的支付宝 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '1103Z1' or code = '1103Z2') -- 包括产品支付宝的还款 and compid = @comp and empno = @empno update a set a.zfb_good = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --产品购买的微信 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '1103W1' or code = '1103W2') -- 包括产品微信的还款 and compid = @comp and empno = @empno update a set a.wx_good = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --产品购买的卡付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110302' and compid = @comp and empno = @empno update a set a.pay_card_good = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --取款的总额 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110119' or code = '110123' or code = 'drawother') and compid = @comp and empno = @empno update a set a.draw_total = isnull(@amt, 0) from #stat_byday a where compid = @comp and empno = @empno --收入都同利润分析表中的收入 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '11' -- 收入 and compid = @comp and empno = @empno update #stat_byday set revene = @amt where compid = @comp and empno = @empno --总现金流 modify by lm 2015.12.09 set @cash_sum = 0 select @cash_sum = sum(amt) from #tbl_profit_day where is_leaf = 1 and payment in (select pay from pay_classify where category = '1' and comp = @comp ) and kind <> 9 and compid = @comp and empno = @empno update a set a.cash_sum = isnull(@cash_sum, 0) from #stat_byday a where compid = @comp and empno = @empno delete from #emp_info where compid = @comp and empno = @empno end else begin set @state = 1 end end -- while 循环结束 update #stat_byday set cash_total = isnull(cash_card, 0) + isnull(cash_srv, 0) + isnull(cash_good, 0), cash_sum = cash_sum, credit_total = isnull(credit_card, 0) + isnull(credit_srv, 0) + isnull(credit_good, 0), zfb_total = isnull(zfb_card, 0) + isnull(zfb_srv, 0) + isnull(zfb_good, 0), wx_total = isnull(wx_card, 0) + isnull(wx_srv, 0) + isnull(wx_good, 0) -- 定义劳动业绩的比率 declare @cash_rate float -- 现金:1 declare @cash_good_rate float -- 现金:1 declare @credit_rate float -- 银行卡支付:6 declare @credit_good_rate float -- 银行卡支付:6 declare @pay3_rate float -- 支付方式:3 declare @pay3_good_rate float -- 支付方式:3 declare @pay$_rate float -- 支付方式:$ declare @pay$_good_rate float -- 支付方式:$ declare @pay0_rate float -- 内调:0 declare @pay0_good_rate float -- 内调:0 declare @manager_rate float -- 经理签单:8 declare @manager_good_rate float -- 经理签单:8 declare @point_rate float -- 积分支付:7 declare @point_good_rate float -- 积分支付:7 declare @user01_rate float -- 自定义支付:$1 declare @user01_good_rate float -- 自定义支付:$1 declare @user02_rate float -- 自定义支付:$2 declare @user02_good_rate float -- 自定义支付:$2 declare @user03_rate float -- 自定义支付:$3 declare @user03_good_rate float -- 自定义支付:$3 declare @user04_rate float -- 自定义支付:$4 declare @user04_good_rate float -- 自定义支付:$4 declare @user05_rate float -- 自定义支付:$5 declare @user05_good_rate float -- 自定义支付:$5 declare @zfb_rate float -- 支付宝支付 declare @zfb_good_rate float -- 支付宝支付 declare @wx_rate float -- 微信支付 declare @wx_good_rate float -- 微信支付 exec upg_get_workperf_rate @compid, @cash_rate output, @cash_good_rate output, @credit_rate output, @credit_good_rate output, @pay3_rate output, @pay3_good_rate output, @pay$_rate output, @pay$_good_rate output, @pay0_rate output, @pay0_good_rate output, @manager_rate output, @manager_good_rate output, @point_rate output, @point_good_rate output, @user01_rate output, @user01_good_rate output, @user02_rate output, @user02_good_rate output, @user03_rate output, @user03_good_rate output, @user04_rate output, @user04_good_rate output, @user05_rate output, @user05_good_rate output, @zfb_rate output, @zfb_good_rate output, @wx_rate output, @wx_good_rate output -- 增加劳动业绩的计算 declare @sp056 varchar(20) select @sp056 = gsa02c from gsm01 with (nolock) where gsa00c = @compid and gsa01c = 'SP056' if @sp056 = '4' -- 足疗 begin -- 足疗的劳动业绩 -- 劳动业绩 = 现金(服务) + 现金(产品) + 卡付(产品) + 卡付 + 抵用券 + 银行卡(服务) + 银行卡(产品) + 积分支付 update #stat_byday set work_perf = isnull(cash_srv, 0) * @cash_rate + isnull(cash_good, 0) * @cash_good_rate + isnull(pay_card_srv, 0) + isnull(pay_card_good, 0) + isnull(credit_srv, 0) * @credit_rate + isnull(credit_good, 0) * @credit_good_rate + isnull(zfb_srv, 0) * @zfb_rate + isnull(zfb_good, 0) * @zfb_good_rate + isnull(wx_srv, 0) * @wx_rate + isnull(wx_good, 0) * @wx_good_rate + (isnull(pay_coupon_srv, 0) - isnull(pay_coupon_good, 0)) * @pay3_rate + isnull(pay_coupon_good, 0) * @pay3_good_rate + (isnull(pay_point_srv, 0) - isnull(pay_point_good, 0)) * @point_rate + isnull(pay_point_good, 0) * @point_good_rate + (isnull(pay_user01_srv, 0) - isnull(pay_user01_good, 0)) * @user01_rate + isnull(pay_user01_good, 0) * @user01_good_rate + (isnull(pay_user02_srv, 0) - isnull(pay_user02_good, 0)) * @user02_rate + isnull(pay_user02_good, 0) * @user02_good_rate + (isnull(pay_user03_srv, 0) - isnull(pay_user03_good, 0)) * @user03_rate + isnull(pay_user03_good, 0) * @user03_good_rate + (isnull(pay_user04_srv, 0) - isnull(pay_user04_good, 0)) * @user04_rate + isnull(pay_user04_good, 0) * @user04_good_rate + (isnull(pay_user05_srv, 0) - isnull(pay_user05_good, 0)) * @user05_rate + isnull(pay_user05_good, 0) * @user05_good_rate end else begin -- 如果是按项目支付的情况下,采用卡付实际营业额,如单据支付则直接用卡付金额 -- 美发劳动业绩 declare @sp055f float select @sp055f = gsa03f from gsm01 with (nolock) where gsa00c = @incompid and gsa01c = 'SP055' if @sp055f = 1 begin -- 按单据支付 update #stat_byday set work_perf = isnull(cash_srv, 0) * @cash_rate + isnull(cash_good, 0) * @cash_good_rate + isnull(pay_card_srv, 0) + isnull(pay_card_good, 0) + isnull(credit_srv, 0) * @credit_rate + isnull(credit_good, 0) * @credit_good_rate + isnull(zfb_srv, 0) * @zfb_rate + isnull(zfb_good, 0) * @zfb_good_rate + isnull(wx_srv, 0) * @wx_rate + isnull(wx_good, 0) * @wx_good_rate + (isnull(pay_coupon_srv, 0) - isnull(pay_coupon_good, 0)) * @pay3_rate + isnull(pay_coupon_good, 0) * @pay3_good_rate + (isnull(pay_coupon2_srv, 0) - isnull(pay_coupon2_good, 0)) * @pay$_rate + isnull(pay_coupon2_good, 0) * @pay$_good_rate + (isnull(pay_coupon3_srv, 0) - isnull(pay_coupon3_good, 0)) * @pay0_rate + isnull(pay_coupon3_good, 0) * @pay0_good_rate + (isnull(pay_manager_prj, 0) - isnull(pay_manager_good, 0)) * @manager_rate + isnull(pay_manager_good, 0) * @manager_good_rate + (isnull(pay_point_srv, 0) - isnull(pay_point_good, 0)) * @point_rate + isnull(pay_point_good, 0) * @point_good_rate + (isnull(pay_user01_srv, 0) - isnull(pay_user01_good, 0)) * @user01_rate + isnull(pay_user01_good, 0) * @user01_good_rate + (isnull(pay_user02_srv, 0) - isnull(pay_user02_good, 0)) * @user02_rate + isnull(pay_user02_good, 0) * @user02_good_rate + (isnull(pay_user03_srv, 0) - isnull(pay_user03_good, 0)) * @user03_rate + isnull(pay_user03_good, 0) * @user03_good_rate + (isnull(pay_user04_srv, 0) - isnull(pay_user04_good, 0)) * @user04_rate + isnull(pay_user04_good, 0) * @user04_good_rate + (isnull(pay_user05_srv, 0) - isnull(pay_user05_good, 0)) * @user05_rate + isnull(pay_user05_good, 0) * @user05_good_rate end else begin -- 劳动业绩 = 现金(服务) + 现金(产品) + 卡付(产品) + 卡付实营业额 + 经理签单 + 内调 + 抵用券 + 银行卡(服务) + 银行卡(产品) + 积分支付 update #stat_byday set work_perf = isnull(cash_srv, 0) * @cash_rate + isnull(cash_good, 0) * @cash_good_rate + isnull(pay_card_srv_work, 0) + isnull(credit_srv, 0) * @credit_rate + isnull(credit_good, 0) * @credit_good_rate + isnull(zfb_srv, 0) * @zfb_rate + isnull(zfb_good, 0) * @zfb_good_rate + isnull(wx_srv, 0) * @wx_rate + isnull(wx_good, 0) * @wx_good_rate + (isnull(pay_coupon_srv, 0) - isnull(pay_coupon_good, 0)) * @pay3_rate + isnull(pay_coupon_good, 0) * @pay3_good_rate + (isnull(pay_coupon2_srv, 0) - isnull(pay_coupon2_good, 0)) * @pay$_rate + isnull(pay_coupon2_good, 0) * @pay$_good_rate + (isnull(pay_coupon3_srv, 0) - isnull(pay_coupon3_good, 0)) * @pay0_rate + isnull(pay_coupon3_good, 0) * @pay0_good_rate + (isnull(pay_manager_prj, 0) - isnull(pay_manager_good, 0)) * @manager_rate + isnull(pay_manager_good, 0) * @manager_good_rate + (isnull(pay_point_srv, 0) - isnull(pay_point_good, 0)) * @point_rate + isnull(pay_point_good, 0) * @point_good_rate + (isnull(pay_user01_srv, 0) - isnull(pay_user01_good, 0)) * @user01_rate + isnull(pay_user01_good, 0) * @user01_good_rate + (isnull(pay_user02_srv, 0) - isnull(pay_user02_good, 0)) * @user02_rate + isnull(pay_user02_good, 0) * @user02_good_rate + (isnull(pay_user03_srv, 0) - isnull(pay_user03_good, 0)) * @user03_rate + isnull(pay_user03_good, 0) * @user03_good_rate + (isnull(pay_user04_srv, 0) - isnull(pay_user04_good, 0)) * @user04_rate + isnull(pay_user04_good, 0) * @user04_good_rate + (isnull(pay_user05_srv, 0) - isnull(pay_user05_good, 0)) * @user05_rate + isnull(pay_user05_good, 0) * @user05_good_rate end end update #stat_byday set profit = isnull(revene, 0) + isnull(other_income, 0) - isnull(outcome, 0) --Start增加疗程销售,消耗 add by lm 2015.10.21 create table #prj_stock_buf ( compid varchar(20) not null, empno varchar(20) not null, prjnum float null, -- 疗程的数量 prjamt float null, -- 疗程金额 primary key(compid, empno) ) create table #prj_stock_buf1 ( compid varchar(20) not null, empno varchar(20) not null, prjnum float null, -- 疗程的数量 prjamt float null, -- 疗程金额 ) create nonclustered index index_prj_stock_buf1 on #prj_stock_buf1 (compid,empno) create table #prj_consume ( compid varchar(20) not null, empno varchar(20) not null, prjnum float null, -- 疗程的数量 prjamt float null, -- 疗程金额 primary key(compid, empno) ) create table #prj_consume1 ( compid varchar(20) not null, empno varchar(20) not null, prjnum float null, -- 疗程的数量 prjamt float null, -- 疗程金额 ) create nonclustered index index_prj_consume1on on #prj_consume1 (compid,empno) create table #gcm10 ( compid varchar(20) not null, empno varchar(20) not null, billid varchar(20) not null,--充值单号 primary key(compid,empno,billid) ) create table #gnm01 ( compid varchar(20) not null, empno varchar(20) not null, billid varchar(20) not null,--充值单号 primary key(compid,empno,billid) ) create table #ggm01 ( compid varchar(20) not null, empno varchar(20) not null, billid varchar(20) not null,--充值单号 primary key(compid,empno,billid) ) insert #gcm10(compid, billid,empno) select distinct gcl00c, gcl01c,gcl81c from gcm10 a with (nolock) where a.gcl00c = @compid and ((a.gcl02d >= @fromdate and a.gcl02d <= @todate) or @fromdate = '*') and a.gcl93c like @staff and (gcl81c >= @fromdepart and gcl81c <= @todepart) union all select distinct gcl00c, gcl01c,gcl82c from gcm10 a with (nolock) where a.gcl00c = @compid and ((a.gcl02d >= @fromdate and a.gcl02d <= @todate) or @fromdate = '*') and a.gcl93c like @staff and (gcl82c >= @fromdepart and gcl82c <= @todepart) union all select distinct gcl00c, gcl01c,gcl83c from gcm10 a with (nolock) where a.gcl00c = @compid and ((a.gcl02d >= @fromdate and a.gcl02d <= @todate) or @fromdate = '*') and a.gcl93c like @staff and (gcl83c >= @fromdepart and gcl83c <= @todepart) union all select distinct gcl00c, gcl01c,gcl100c from gcm10 a with (nolock) where a.gcl00c = @compid and ((a.gcl02d >= @fromdate and a.gcl02d <= @todate) or @fromdate = '*') and a.gcl93c like @staff and (gcl100c >= @fromdepart and gcl100c <= @todepart) union all select distinct gcl00c, gcl01c,gcl101c from gcm10 a with (nolock) where a.gcl00c = @compid and ((a.gcl02d >= @fromdate and a.gcl02d <= @todate) or @fromdate = '*') and a.gcl93c like @staff and (gcl101c >= @fromdepart and gcl101c <= @todepart) union all select distinct gcl00c, gcl01c,gcl102c from gcm10 a with (nolock) where a.gcl00c = @compid and ((a.gcl02d >= @fromdate and a.gcl02d <= @todate) or @fromdate = '*') and a.gcl93c like @staff and (gcl102c >= @fromdepart and gcl102c <= @todepart) union all select distinct gcl00c, gcl01c,gcl103c from gcm10 a with (nolock) where a.gcl00c = @compid and ((a.gcl02d >= @fromdate and a.gcl02d <= @todate) or @fromdate = '*') and a.gcl93c like @staff and (gcl103c >= @fromdepart and gcl103c <= @todepart) union all select distinct gcl00c, gcl01c,gcl104c from gcm10 a with (nolock) where a.gcl00c = @compid and ((a.gcl02d >= @fromdate and a.gcl02d <= @todate) or @fromdate = '*') and a.gcl93c like @staff and (gcl104c >= @fromdepart and gcl104c <= @todepart) union all select distinct gcl00c, gcl01c,gcl105c from gcm10 a with (nolock) where a.gcl00c = @compid and ((a.gcl02d >= @fromdate and a.gcl02d <= @todate) or @fromdate = '*') and a.gcl93c like @staff and (gcl105c >= @fromdepart and gcl105c <= @todepart) union all select distinct gcl00c, gcl01c,gcl106c from gcm10 a with (nolock) where a.gcl00c = @compid and ((a.gcl02d >= @fromdate and a.gcl02d <= @todate) or @fromdate = '*') and a.gcl93c like @staff and (gcl106c >= @fromdepart and gcl106c <= @todepart) insert #gnm01(compid, billid, empno) select distinct gna00c, gna01c, gna81c from gnm01 a with (nolock) where a.gna00c = @compid and ((a.gna02d >= @fromdate and a.gna02d <= @todate) or @fromdate = '*') and a.gna93c like @staff and (gna81c >= @fromdepart and gna81c <= @todepart) union all select distinct gna00c, gna01c, gna82c from gnm01 a with (nolock) where a.gna00c = @compid and ((a.gna02d >= @fromdate and a.gna02d <= @todate) or @fromdate = '*') and a.gna93c like @staff and (gna82c >= @fromdepart and gna82c <= @todepart) union all select distinct gna00c, gna01c, gna83c from gnm01 a with (nolock) where a.gna00c = @compid and ((a.gna02d >= @fromdate and a.gna02d <= @todate) or @fromdate = '*') and a.gna93c like @staff and (gna83c >= @fromdepart and gna83c <= @todepart) union all select distinct gna00c, gna01c, gna100c from gnm01 a with (nolock) where a.gna00c = @compid and ((a.gna02d >= @fromdate and a.gna02d <= @todate) or @fromdate = '*') and a.gna93c like @staff and (gna100c >= @fromdepart and gna100c <= @todepart) union all select distinct gna00c, gna01c, gna101c from gnm01 a with (nolock) where a.gna00c = @compid and ((a.gna02d >= @fromdate and a.gna02d <= @todate) or @fromdate = '*') and a.gna93c like @staff and (gna101c >= @fromdepart and gna101c <= @todepart) union all select distinct gna00c, gna01c, gna102c from gnm01 a with (nolock) where a.gna00c = @compid and ((a.gna02d >= @fromdate and a.gna02d <= @todate) or @fromdate = '*') and a.gna93c like @staff and (gna102c >= @fromdepart and gna102c <= @todepart) union all select distinct gna00c, gna01c, gna103c from gnm01 a with (nolock) where a.gna00c = @compid and ((a.gna02d >= @fromdate and a.gna02d <= @todate) or @fromdate = '*') and a.gna93c like @staff and (gna103c >= @fromdepart and gna103c <= @todepart) union all select distinct gna00c, gna01c, gna104c from gnm01 a with (nolock) where a.gna00c = @compid and ((a.gna02d >= @fromdate and a.gna02d <= @todate) or @fromdate = '*') and a.gna93c like @staff and (gna104c >= @fromdepart and gna104c <= @todepart) union all select distinct gna00c, gna01c, gna105c from gnm01 a with (nolock) where a.gna00c = @compid and ((a.gna02d >= @fromdate and a.gna02d <= @todate) or @fromdate = '*') and a.gna93c like @staff and (gna105c >= @fromdepart and gna105c <= @todepart) union all select distinct gna00c, gna01c, gna106c from gnm01 a with (nolock) where a.gna00c = @compid and ((a.gna02d >= @fromdate and a.gna02d <= @todate) or @fromdate = '*') and a.gna93c like @staff and (gna106c >= @fromdepart and gna106c <= @todepart) insert #ggm01(compid, billid, empno) select distinct gga00c, gga01c,ggb81c from ggm01 a with (nolock), ggm02 b where a.gga00c = b.ggb00c and a.gga01c = b.ggb01c and a.gga00c = @compid and a.gga93c like @staff and ((a.gga02d >= @fromdate and a.gga02d <= @todate) or @fromdate = '*') and (ggb81c >= @fromdepart and ggb81c <= @todepart) -- 这个时间点范围内销售的疗程 insert #prj_stock_buf1(compid, empno, prjnum, prjamt) select compid, empno, sum(gch05f + gch06f), sum(gch07f) from #gnm01 a, gcm08 b with (nolock) where a.compid = b.gch00c and a.billid = b.gch11c and b.gch10c = 'gn' and ((b.gch02d >= @fromdate and b.gch02d <= @todate) or @fromdate = '*') group by compid, empno order by compid, empno insert #prj_stock_buf1(compid, empno, prjnum, prjamt) select compid, empno, sum(gch05f + gch06f), sum(gch07f) from #gcm10 a, gcm08 b with (nolock) where a.compid = b.gch00c and a.billid = b.gch11c and b.gch10c = 'gz' and ((b.gch02d >= @fromdate and b.gch02d <= @todate) or @fromdate = '*') group by compid, empno order by compid, empno --合计 insert #prj_stock_buf (compid, empno, prjnum, prjamt) select compid, empno, sum(prjnum), sum(prjamt) from #prj_stock_buf1 group by compid, empno insert #prj_consume1 (compid, empno, prjnum, prjamt) select compid, empno, sum(gch08f), sum(gch09f) from #ggm01 a, gcm08 b with (nolock) where a.compid = b.gch00c and a.billid = b.gch11c and b.gch10c = 'gx' and ((b.gch02d >= @fromdate and b.gch02d <= @todate) or @fromdate = '*') group by compid, empno order by compid, empno --合计 insert #prj_consume (compid, empno, prjnum, prjamt) select compid, empno, sum(prjnum), sum(prjamt) from #prj_consume1 group by compid, empno --疗程销售,消耗金额,数量赋值 update a set a.prj_quan_buy = isnull(b.prjnum,0), a.prj_amt_buy = isnull(b.prjamt,0) from #stat_byday a, #prj_stock_buf b where a.compid = b.compid and b.empno = b.empno update a set a.prj_quan_consume = isnull(b.prjnum,0), a.prj_amt_consume = isnull(b.prjamt,0) from #stat_byday a, #prj_consume b where a.compid = b.compid and b.empno = b.empno --End select isnull(compid,'') compid, isnull(cash_card,0) cash_card, isnull(cash_srv,0) cash_srv, isnull(cash_good,0) cash_good, isnull(cash_total,0) cash_total, isnull(credit_card,0) credit_card, isnull(credit_srv,0) credit_srv, isnull(credit_good,0) credit_good, isnull(credit_total,0) credit_total, isnull(zfb_card,0) zfb_card, isnull(zfb_srv,0) zfb_srv, isnull(zfb_good,0) zfb_good, isnull(zfb_total,0) zfb_total, isnull(wx_card,0) wx_card, isnull(wx_srv,0) wx_srv, isnull(wx_good,0) wx_good, isnull(wx_total,0) wx_total, isnull(revene,0) revene, isnull(outcome,0) outcome, isnull(profit,0) profit, isnull(defict,0) defict, isnull(returnamt,0) returnamt, isnull(pay_card_srv,0) pay_card_srv, isnull(pay_card_good,0) pay_card_good, isnull(pay_coupon,0) pay_coupon, isnull(pay_coupon2,0) pay_coupon2, isnull(pay_coupon3,0) pay_coupon3, isnull(pay_manager,0) pay_manager, isnull(sale_card_total,0) sale_card_total, isnull(pay_card_srv_act,0) pay_card_srv_act, isnull(pay_card_srv_work,0) pay_card_srv_work, isnull(pay_coupon_srv,0) pay_coupon_srv, isnull(pay_coupon2_srv,0) pay_coupon2_srv, isnull(pay_coupon3_srv,0) pay_coupon3_srv, isnull(draw_total,0) draw_total, isnull(work_perf,0) work_perf, isnull(cust_quan,0) cust_quan, isnull(proj_quan,0) proj_quan, isnull(pay_manager_prj,0) pay_manager_prj, isnull(pay_manager_good,0) pay_manager_good, isnull(pay_coupon_good,0) pay_coupon_good, isnull(pay_coupon2_good,0) pay_coupon2_good, isnull(pay_coupon3_good,0) pay_coupon3_good, isnull(pay_point,0) pay_point, isnull(pay_point_srv,0) pay_point_srv, isnull(pay_point_good,0) pay_point_good, isnull(pay_user01,0) pay_user01, isnull(pay_user01_srv,0) pay_user01_srv, isnull(pay_user01_good,0) pay_user01_good, isnull(pay_user02,0) pay_user02, isnull(pay_user02_srv,0) pay_user02_srv, isnull(pay_user02_good,0) pay_user02_good, isnull(pay_user03,0) pay_user03, isnull(pay_user03_srv,0) pay_user03_srv, isnull(pay_user03_good,0) pay_user03_good, isnull(pay_user04,0) pay_user04, isnull(pay_user04_srv,0) pay_user04_srv, isnull(pay_user04_good,0) pay_user04_good, isnull(pay_user05,0) pay_user05, isnull(pay_user05_srv,0) pay_user05_srv, isnull(pay_user05_good,0) pay_user05_good, isnull(other_income,0) other_income, isnull(virtual_income,0) virtual_income, isnull(virtual_outcome,0) virtual_outcome, isnull(prj_quan_buy,0) prj_quan_buy, isnull(prj_amt_buy,0) prj_amt_buy, isnull(prj_quan_consume,0) prj_quan_consume, isnull(prj_amt_consume,0) prj_amt_consume, isnull(cash_sum,0) cash_sum, isnull(empname,'') empname, isnull(empno, '') empno from #stat_byday union all select isnull(compid,'') compid, isnull(sum(cash_card),0) cash_card, isnull(sum(cash_srv),0) cash_srv, isnull(sum(cash_good),0) cash_good, isnull(sum(cash_total),0) cash_total, isnull(sum(credit_card),0) credit_card, isnull(sum(credit_srv),0) credit_srv, isnull(sum(credit_good),0) credit_good, isnull(sum(credit_total),0) credit_total, isnull(sum(zfb_card),0) zfb_card, isnull(sum(zfb_srv),0) zfb_srv, isnull(sum(zfb_good),0) zfb_good, isnull(sum(zfb_total),0) zfb_total, isnull(sum(wx_card),0) wx_card, isnull(sum(wx_srv),0) wx_srv, isnull(sum(wx_good),0) wx_good, isnull(sum(wx_total),0) wx_total, isnull(sum(revene),0) revene, isnull(sum(outcome),0) outcome, isnull(sum(profit),0) profit, isnull(sum(defict),0) defict, isnull(sum(returnamt),0) returnamt, isnull(sum(pay_card_srv),0) pay_card_srv, isnull(sum(pay_card_good),0) pay_card_good, isnull(sum(pay_coupon),0) pay_coupon, isnull(sum(pay_coupon2),0) pay_coupon2, isnull(sum(pay_coupon3),0) pay_coupon3, isnull(sum(pay_manager),0) pay_manager, isnull(sum(sale_card_total),0) sale_card_total, isnull(sum(pay_card_srv_act),0) pay_card_srv_act, isnull(sum(pay_card_srv_work),0) pay_card_srv_work, isnull(sum(pay_coupon_srv),0) pay_coupon_srv, isnull(sum(pay_coupon2_srv),0) pay_coupon2_srv, isnull(sum(pay_coupon3_srv),0) pay_coupon3_srv, isnull(sum(draw_total),0) draw_total, isnull(sum(work_perf),0) work_perf, isnull(sum(cust_quan),0) cust_quan, isnull(sum(proj_quan),0) proj_quan, isnull(sum(pay_manager_prj),0) pay_manager_prj, isnull(sum(pay_manager_good),0) pay_manager_good, isnull(sum(pay_coupon_good),0) pay_coupon_good, isnull(sum(pay_coupon2_good),0) pay_coupon2_good, isnull(sum(pay_coupon3_good),0) pay_coupon3_good, isnull(sum(pay_point),0) pay_point, isnull(sum(pay_point_srv),0) pay_point_srv, isnull(sum(pay_point_good),0) pay_point_good, isnull(sum(pay_user01),0) pay_user01, isnull(sum(pay_user01_srv),0) pay_user01_srv, isnull(sum(pay_user01_good),0) pay_user01_good, isnull(sum(pay_user02),0) pay_user02, isnull(sum(pay_user02_srv),0) pay_user02_srv, isnull(sum(pay_user02_good),0) pay_user02_good, isnull(sum(pay_user03),0) pay_user03, isnull(sum(pay_user03_srv),0) pay_user03_srv, isnull(sum(pay_user03_good),0) pay_user03_good, isnull(sum(pay_user04),0) pay_user04, isnull(sum(pay_user04_srv),0) pay_user04_srv, isnull(sum(pay_user04_good),0) pay_user04_good, isnull(sum(pay_user05),0) pay_user05, isnull(sum(pay_user05_srv),0) pay_user05_srv, isnull(sum(pay_user05_good),0) pay_user05_good, isnull(sum(other_income),0) other_income, isnull(sum(virtual_income),0) virtual_income, isnull(sum(virtual_outcome),0) virtual_outcome, isnull(sum(prj_quan_buy),0) prj_quan_buy, isnull(sum(prj_amt_buy),0) prj_amt_buy, isnull(sum(prj_quan_consume),0) prj_quan_consume, isnull(sum(prj_amt_consume),0) prj_amt_consume, isnull(sum(cash_sum),0) cash_sum, '' empname, '' empno from #stat_byday group by compid drop table #stat_byday drop table #tbl_profit_day drop table #gcm10 drop table #gnm01 drop table #ggm01 drop table #prj_stock_buf drop table #prj_stock_buf1 drop table #prj_consume drop table #prj_consume1 drop table #cust_num_by_day drop table #emp_info end GO /****** Object: StoredProcedure [dbo].[upg_stat_byday_operation_result_new] Script Date: 06/12/2017 06:20:43 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_stat_byday_operation_result_new] ( @compid varchar(10), -- 已经能支持各级别的公司 @fromdate varchar(10), @todate varchar(10), @flag int, -- 0-每天一纪录 1-返回一条记录 @staff varchar(20), @calmode int , -- 0-营业款 1-营业收入 2-综合小单 @work_perf float output ) as begin declare @incompid varchar(10) select @incompid = @compid declare @cur_date datetime declare @strtmp varchar(30) declare @str_date varchar(20) declare @amt float declare @tmpamt float declare @cash_sum float declare @qry_compid varchar(20) if @compid = '001' or @compid = '*' begin select @qry_compid = '%' end else begin select @qry_compid = @compid end set @cur_date = convert(datetime, @fromdate) create table #tbl_profit_day ( idd int not null, code varchar(20) not null, itemname varchar(60) null, is_add int null, amt float null, is_leaf int null, payment varchar(10) null, -- 支付方式 kind int null -- 0-充值 1-卡销售 9-产品销售 2-服务 1-取款 8-保证金收入 6-还款 ) create table #stat_byday ( day_date varchar(20) not null, cash_card float null, -- 卡销售-现金 cash_srv float null, -- 现金服务 cash_good float null, -- 现金卡销售 cash_total float null, -- 所有现金 credit_card float null, -- 卡销售 - 银行卡 credit_srv float null, -- 服务银行卡 credit_good float null, -- 银行卡买产品 credit_total float null, -- 总银行卡收入 zfb_card float null, -- 卡销售 - 支付宝 zfb_srv float null, -- 服务支付宝 zfb_good float null, -- 支付宝买产品 zfb_total float null, -- 总支付宝收入 wx_card float null, -- 卡销售 - 微信 wx_srv float null, -- 服务微信 wx_good float null, -- 微信买产品 wx_total float null, -- 总微信收入 pay_card_srv float null, -- 服务项目的卡付, 不按负债标准, 但不包括积分 pay_card_good float null, -- 产品的卡付 pay_coupon float null, -- 券付 支付方式3 pay_coupon2 float null, -- 贵付 支付方式$ pay_coupon3 float null, -- 洗头 支付方式内调 pay_manager float null, -- 经理签单, 包括了 卡销售,项目,产品 pay_card_srv_act float null, -- 卡付实际营业额 = 在服务项目 + 产品中,那些卡付的金额转换为实际营业额 pay_card_srv_work float null, -- 卡付实际劳动业绩 = 在服务项目 + 产品中,那些卡付的金额转换为劳动业绩 draw_total float null, -- 取款总额(包含现金以及内调) work_perf float null, -- 劳动业绩 = 现金(服务) + 现金(物品) + 卡付(物品) + 卡付实营业额 + 项目或物品经理签单 + 内调 + 抵用券 + 银行卡(服务) + 银行卡(物品) cust_quan float null, --客人数量 proj_quan float null, -- 总项目数 pay_manager_prj float null, --只包括项目的经理签单 pay_coupon_srv float null, -- 券付 支付方式3, 只包括项目(含产品) pay_coupon2_srv float null, -- 贵付 支付方式$, 只包括项目(含产品) pay_coupon3_srv float null, -- 洗头 支付方式内调, 只包括项目(含产品) pay_manager_good float null, --只包括产品的经理签单 pay_coupon_good float null, -- 券付 支付方式3, 只包括产品 pay_coupon2_good float null, -- 贵付 支付方式$, 只包括产品 pay_coupon3_good float null, -- 洗头 支付方式内调, 只包括产品 pay_point float null, -- 积分支付,卡销售,项目,产品 pay_point_srv float null, -- 积分支付,只包括项目(含产品) pay_point_good float null, -- 积分支付,只有产品 pay_user01 float null, -- 自定义支付,卡销售,项目,产品 pay_user01_srv float null, -- 自定义支付,只包括项目(含产品) pay_user01_good float null, -- 自定义支付,只有产品 pay_user02 float null, -- 自定义支付,卡销售,项目,产品 pay_user02_srv float null, -- 自定义支付,只包括项目(含产品) pay_user02_good float null, -- 自定义支付,只有产品 pay_user03 float null, -- 自定义支付,卡销售,项目,产品 pay_user03_srv float null, -- 自定义支付,只包括项目(含产品) pay_user03_good float null, -- 自定义支付,只有产品 pay_user04 float null, -- 自定义支付,卡销售,项目,产品 pay_user04_srv float null, -- 自定义支付,只包括项目(含产品) pay_user04_good float null, -- 自定义支付,只有产品 pay_user05 float null, -- 自定义支付,卡销售,项目,产品 pay_user05_srv float null, -- 自定义支付,只包括项目(含产品) pay_user05_good float null -- 自定义支付,只有产品 primary key(day_date) ) while (@cur_date <= convert(datetime, @todate)) begin select @strtmp = convert(varchar(10), cast(@cur_date as datetime), 120) select @str_date = substring(@strtmp, 1, 4) + substring(@strtmp, 6, 2) + substring(@strtmp, 9, 2) delete #tbl_profit_day if (@flag = 0) -- 一天一条记录 begin insert #tbl_profit_day(idd, code, itemname, is_add, amt, is_leaf, payment, kind) exec upg_analyse_profit @compid, @str_date, @str_date, 1, @staff, @calmode end else if (@flag = 1) begin insert #tbl_profit_day(idd, code, itemname, is_add, amt, is_leaf, payment, kind) exec upg_analyse_profit @compid, @fromdate, @todate, 1, @staff, @calmode end insert #stat_byday(day_date) values(@str_date) delete tbl_profit_day_buf where compid = @compid and fromdate = @fromdate and todate = @todate and flag = @flag and staff = @staff and fromdepart = '*' and todepart = '' and calmode = @calmode insert tbl_profit_day_buf(compid, fromdate, todate, flag, staff, fromdepart, todepart, calmode, code, itemname, is_add, amt, is_leaf, payment, kind) select @compid, @fromdate, @todate, @flag, @staff, '*', '', @calmode, code, itemname, is_add, amt, is_leaf, payment, kind from #tbl_profit_day -- 转换为汇总表需要的一条记录 -- 现金(卖卡) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110101' or code = '110104' or code = '110107' or code = '110118' or code = '110140' -- 现金取款 select @tmpamt = 0 select @tmpamt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110119' -- 现金(卖卡) + 现金取款 update a set a.cash_card = isnull(@amt, 0) + isnull(@tmpamt, 0) from #stat_byday a where day_date = @str_date -- 现金(服务) -- 在按项目支付的情况下,是正确的项目的现金支付 -- 在单据支付的情况下,实际上包括了物品销售的现金支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110201' or code = '110230' -- 项目或物品的现金还款也算 还款的不算在 select @tmpamt = 0 select @tmpamt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110301' or code = '110310' -- 在单据支付的情况,这个值为0 , 产品销售的现金以及 产品还款 update a set a.cash_srv = isnull(@amt, 0) - isnull(@tmpamt, 0) from #stat_byday a where day_date = @str_date -- 银行卡(卖卡) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110102' or code = '110105' or code = '110108' or code = '110141' update a set a.credit_card = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 支付宝(卖卡) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '1101Z1' or code = '1101Z2' or code = '1101Z3' or code = '1101Z4' update a set a.zfb_card = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 微信(卖卡) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '1101W1' or code = '1101W2' or code = '1101W3' or code = '1101W4' update a set a.wx_card = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 银行卡(服务) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110202' or code = '110231' --项目或物品的银行卡还款不算 select @tmpamt = 0 select @tmpamt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110306' or code = '110311' -- 产品部分的银行卡支付 以及 银行卡还款 -- 服务产品 - 产品 = 单纯服务项目的收入 update a set a.credit_srv = isnull(@amt, 0) - isnull(@tmpamt, 0) from #stat_byday a where day_date = @str_date -- 支付宝(服务) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '1102Z1' or code = '1102Z2' --项目或物品的支付宝还款不算 select @tmpamt = 0 select @tmpamt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '1103Z1' or code = '1103Z2' -- 产品部分的支付宝支付 以及 支付宝还款 -- 服务产品 - 产品 = 单纯服务项目的收入 update a set a.zfb_srv = isnull(@amt, 0) - isnull(@tmpamt, 0) from #stat_byday a where day_date = @str_date -- 微信(服务) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '1102W1' or code = '1102W2' --项目或物品的微信还款不算 select @tmpamt = 0 select @tmpamt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '1103W1' or code = '1103W2' -- 产品部分的微信支付 以及 微信还款 -- 服务产品 - 产品 = 单纯服务项目的收入 update a set a.wx_srv = isnull(@amt, 0) - isnull(@tmpamt, 0) from #stat_byday a where day_date = @str_date -- 卡付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110205' -- 这里面已经不包括了积分 11020507 select @tmpamt = 0 select @tmpamt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110302'--产品卡付部分,需要扣除,否则重复统计 update a set a.pay_card_srv = isnull(@amt, 0) - isnull(@tmpamt, 0) from #stat_byday a where day_date = @str_date -- 卡付实际营业额 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110206' update a set a.pay_card_srv_act = isnull(@amt, 0) from #stat_byday a where day_date = @str_date select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '11AA01' update a set a.pay_card_srv_work = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 抵用券付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110204' or code = '110125' or code = '110126' or code = '110127') update a set a.pay_coupon = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 抵用券付(只有项目, 包括产品部分) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110204') update a set a.pay_coupon_srv = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 抵用券付(只包括产品部分) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110303') update a set a.pay_coupon_good = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 抵用券二 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110208' or code = '110130' or code = '110131' or code = '110132') -- 因为服务中已经包括了产品的销售, 所以不能再包括110304(产品的$支付) update a set a.pay_coupon2 = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 抵用券二(只有项目) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110208') update a set a.pay_coupon2_srv = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 抵用券, 只有产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110304') update a set a.pay_coupon2_good = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 内调 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110209' or code = '110120' or code = '110121' or code = '110122') update a set a.pay_coupon3 = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 内调(只包括项目 + 产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110209') update a set a.pay_coupon3_srv = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 内调(只包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110308') update a set a.pay_coupon3_good = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 经理签单, 包括卡销售的经理签单 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110110' or code = '110111' or code = '110203') update a set a.pay_manager = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 经理签单,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110203') update a set a.pay_manager_prj = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 经理签单,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110305') update a set a.pay_manager_good = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 自定义支付, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$1' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8) update a set a.pay_user01 = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$1' and (kind = 2) update a set a.pay_user01_srv = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$1' and (kind = 9) update a set a.pay_user01_good = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 自定义支付, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$2' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8) update a set a.pay_user02 = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$2' and (kind = 2) update a set a.pay_user02_srv = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$2' and (kind = 9) update a set a.pay_user02_good = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 自定义支付, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$3' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8) update a set a.pay_user03 = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$3' and (kind = 2) update a set a.pay_user03_srv = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$3' and (kind = 9) update a set a.pay_user03_good = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 自定义支付, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$4' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8) update a set a.pay_user04 = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$4' and (kind = 2) update a set a.pay_user04_srv = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$4' and (kind = 9) update a set a.pay_user04_good = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 自定义支付, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$5' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8) update a set a.pay_user05 = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$5' and (kind = 2) update a set a.pay_user05_srv = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$5' and (kind = 9) update a set a.pay_user05_good = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 积分支付, 包括卡销售的积分支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110133' or code = '11020507') update a set a.pay_point = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 积分支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '11020507') update a set a.pay_point_srv = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 积分支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110309') update a set a.pay_point_good = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 物品购买的现金 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110301' or code = '110310' -- 包括产品现金的还款 update a set a.cash_good = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 物品购买的银行卡 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110306' or code = '110311' -- 包括产品银行卡的还款 update a set a.credit_good = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 物品购买的支付宝 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '1103Z1' or code = '1103Z2' -- 包括产品支付宝的还款 update a set a.zfb_good = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 物品购买的微信 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '1103W1' or code = '1103W2' -- 包括产品微信的还款 update a set a.wx_good = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 物品购买的卡付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110302' update a set a.pay_card_good = isnull(@amt, 0) from #stat_byday a where day_date = @str_date if (@flag = 0) select @cur_date = dateadd(day, 1, @cur_date) else if (@flag = 1) select @cur_date = dateadd(day, 1, convert(datetime, @todate)) end -- while 循环结束 -- 定义劳动业绩的比率 declare @cash_rate float -- 现金:1 declare @cash_good_rate float -- 现金:1 declare @credit_rate float -- 银行卡支付:6 declare @credit_good_rate float -- 银行卡支付:6 declare @pay3_rate float -- 支付方式:3 declare @pay3_good_rate float -- 支付方式:3 declare @pay$_rate float -- 支付方式:$ declare @pay$_good_rate float -- 支付方式:$ declare @pay0_rate float -- 内调:0 declare @pay0_good_rate float -- 内调:0 declare @manager_rate float -- 经理签单:8 declare @manager_good_rate float -- 经理签单:8 declare @point_rate float -- 积分支付:7 declare @point_good_rate float -- 积分支付:7 declare @user01_rate float -- 自定义支付:$1 declare @user01_good_rate float -- 自定义支付:$1 declare @user02_rate float -- 自定义支付:$2 declare @user02_good_rate float -- 自定义支付:$2 declare @user03_rate float -- 自定义支付:$3 declare @user03_good_rate float -- 自定义支付:$3 declare @user04_rate float -- 自定义支付:$4 declare @user04_good_rate float -- 自定义支付:$4 declare @user05_rate float -- 自定义支付:$5 declare @user05_good_rate float -- 自定义支付:$5 declare @zfb_rate float -- 支付宝支付 declare @zfb_good_rate float -- 支付宝支付 declare @wx_rate float -- 微信支付 declare @wx_good_rate float -- 微信支付 exec upg_get_workperf_rate @compid, @cash_rate output, @cash_good_rate output, @credit_rate output, @credit_good_rate output, @pay3_rate output, @pay3_good_rate output, @pay$_rate output, @pay$_good_rate output, @pay0_rate output, @pay0_good_rate output, @manager_rate output, @manager_good_rate output, @point_rate output, @point_good_rate output, @user01_rate output, @user01_good_rate output, @user02_rate output, @user02_good_rate output, @user03_rate output, @user03_good_rate output, @user04_rate output, @user04_good_rate output, @user05_rate output, @user05_good_rate output, @zfb_rate output, @zfb_good_rate output, @wx_rate output, @wx_good_rate output -- 增加劳动业绩的计算 declare @sp056 varchar(20) select @sp056 = gsa02c from gsm01 with (nolock) where gsa00c = @compid and gsa01c = 'SP056' if @sp056 = '4' -- 足疗 begin -- 足疗的劳动业绩 -- 劳动业绩 = 现金(服务) + 现金(物品) + 卡付(物品) + 卡付 + 抵用券 + 银行卡(服务) + 银行卡(物品) + 积分支付(项目 + 产品) update #stat_byday set work_perf = isnull(cash_srv, 0) * @cash_rate + isnull(cash_good, 0) * @cash_good_rate + isnull(pay_card_srv, 0) + isnull(pay_card_good, 0) + isnull(credit_srv, 0) * @credit_rate + isnull(credit_good, 0) * @credit_good_rate + isnull(zfb_srv, 0) * @zfb_rate + isnull(zfb_good, 0) * @zfb_good_rate + isnull(wx_srv, 0) * @wx_rate + isnull(wx_good, 0) * @wx_good_rate + (isnull(pay_coupon_srv, 0) - isnull(pay_coupon_good, 0)) * @pay3_rate + isnull(pay_coupon_good, 0) * @pay3_good_rate + (isnull(pay_point_srv, 0) - isnull(pay_point_good, 0)) * @point_rate + isnull(pay_point_good, 0) * @point_good_rate + (isnull(pay_user01_srv, 0) - isnull(pay_user01_good, 0)) * @user01_rate + isnull(pay_user01_good, 0) * @user01_good_rate + (isnull(pay_user02_srv, 0) - isnull(pay_user02_good, 0)) * @user02_rate + isnull(pay_user02_good, 0) * @user02_good_rate + (isnull(pay_user03_srv, 0) - isnull(pay_user03_good, 0)) * @user03_rate + isnull(pay_user03_good, 0) * @user03_good_rate + (isnull(pay_user04_srv, 0) - isnull(pay_user04_good, 0)) * @user04_rate + isnull(pay_user04_good, 0) * @user04_good_rate + (isnull(pay_user05_srv, 0) - isnull(pay_user05_good, 0)) * @user05_rate + isnull(pay_user05_good, 0) * @user05_good_rate end else begin -- 如果是按项目支付的情况下,采用卡付实际营业额,如单据支付则直接用卡付金额 -- 美发劳动业绩 declare @sp055f float select @sp055f = gsa03f from gsm01 with (nolock) where gsa00c = @incompid and gsa01c = 'SP055' if @sp055f = 1 begin -- 按单据支付 update #stat_byday set work_perf = isnull(cash_srv, 0) * @cash_rate + isnull(cash_good, 0) * @cash_good_rate + isnull(pay_card_srv, 0) + isnull(pay_card_good, 0) + isnull(credit_srv, 0) * @credit_rate + isnull(credit_good, 0) * @credit_good_rate + isnull(zfb_srv, 0) * @zfb_rate + isnull(zfb_good, 0) * @zfb_good_rate + isnull(wx_srv, 0) * @wx_rate + isnull(wx_good, 0) * @wx_good_rate + (isnull(pay_coupon_srv, 0) - isnull(pay_coupon_good, 0)) * @pay3_rate + isnull(pay_coupon_good, 0) * @pay3_good_rate + (isnull(pay_coupon2_srv, 0) - isnull(pay_coupon2_good, 0)) * @pay$_rate + isnull(pay_coupon2_good, 0) * @pay$_good_rate + (isnull(pay_coupon3_srv, 0) - isnull(pay_coupon3_good, 0)) * @pay0_rate + isnull(pay_coupon3_good, 0) * @pay0_good_rate + (isnull(pay_manager_prj, 0) - isnull(pay_manager_good, 0)) * @manager_rate + isnull(pay_manager_good, 0) * @manager_good_rate + (isnull(pay_point_srv, 0) - isnull(pay_point_good, 0)) * @point_rate + isnull(pay_point_good, 0) * @point_good_rate + (isnull(pay_user01_srv, 0) - isnull(pay_user01_good, 0)) * @user01_rate + isnull(pay_user01_good, 0) * @user01_good_rate + (isnull(pay_user02_srv, 0) - isnull(pay_user02_good, 0)) * @user02_rate + isnull(pay_user02_good, 0) * @user02_good_rate + (isnull(pay_user03_srv, 0) - isnull(pay_user03_good, 0)) * @user03_rate + isnull(pay_user03_good, 0) * @user03_good_rate + (isnull(pay_user04_srv, 0) - isnull(pay_user04_good, 0)) * @user04_rate + isnull(pay_user04_good, 0) * @user04_good_rate + (isnull(pay_user05_srv, 0) - isnull(pay_user05_good, 0)) * @user05_rate + isnull(pay_user05_good, 0) * @user05_good_rate end else begin -- 劳动业绩 = 现金(服务) + 现金(物品) + 卡付(物品) + 卡付实营业额 + 经理签单 + 内调 + 抵用券 + 银行卡(服务) + 银行卡(物品) + 积分支付(项目 + 产品) update #stat_byday set work_perf = isnull(cash_srv, 0) * @cash_rate + isnull(cash_good, 0) * @cash_good_rate + isnull(pay_card_srv_work, 0) + isnull(credit_srv, 0) * @credit_rate + isnull(credit_good, 0) * @credit_good_rate + isnull(zfb_srv, 0) * @zfb_rate + isnull(zfb_good, 0) * @zfb_good_rate + isnull(wx_srv, 0) * @wx_rate + isnull(wx_good, 0) * @wx_good_rate + (isnull(pay_coupon_srv, 0) - isnull(pay_coupon_good, 0)) * @pay3_rate + isnull(pay_coupon_good, 0) * @pay3_good_rate + (isnull(pay_coupon2_srv, 0) - isnull(pay_coupon2_good, 0)) * @pay$_rate + isnull(pay_coupon2_good, 0) * @pay$_good_rate + (isnull(pay_coupon3_srv, 0) - isnull(pay_coupon3_good, 0)) * @pay0_rate + isnull(pay_coupon3_good, 0) * @pay0_good_rate + (isnull(pay_manager_prj, 0) - isnull(pay_manager_good, 0)) * @manager_rate + isnull(pay_manager_good, 0) * @manager_good_rate + (isnull(pay_point_srv, 0) - isnull(pay_point_good, 0)) * @point_rate + isnull(pay_point_good, 0) * @point_good_rate + (isnull(pay_user01_srv, 0) - isnull(pay_user01_good, 0)) * @user01_rate + isnull(pay_user01_good, 0) * @user01_good_rate + (isnull(pay_user02_srv, 0) - isnull(pay_user02_good, 0)) * @user02_rate + isnull(pay_user02_good, 0) * @user02_good_rate + (isnull(pay_user03_srv, 0) - isnull(pay_user03_good, 0)) * @user03_rate + isnull(pay_user03_good, 0) * @user03_good_rate + (isnull(pay_user04_srv, 0) - isnull(pay_user04_good, 0)) * @user04_rate + isnull(pay_user04_good, 0) * @user04_good_rate + (isnull(pay_user05_srv, 0) - isnull(pay_user05_good, 0)) * @user05_rate + isnull(pay_user05_good, 0) * @user05_good_rate end end select @work_perf = work_perf from #stat_byday drop table #stat_byday drop table #tbl_profit_day end GO /****** Object: StoredProcedure [dbo].[upg_stat_byday_nochain] Script Date: 06/12/2017 06:20:43 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_stat_byday_nochain] ( @compid varchar(10), -- 已经能支持各级别的公司 @fromdate varchar(10), @todate varchar(10), @flag int, -- 0-每天一纪录 1-返回一条记录 @staff varchar(20), @calmode int -- 0-营业款 1-营业收入 2-综合小单 ) as begin declare @incompid varchar(10) select @incompid = @compid declare @cur_date datetime declare @strtmp varchar(30) declare @str_date varchar(20) declare @amt float declare @tmpamt float declare @cash_sum float declare @qry_compid varchar(20) set @cur_date = convert(datetime, @fromdate) create table #tbl_profit_day ( idd int not null, code varchar(20) not null, itemname varchar(60) null, is_add int null, amt float null, is_leaf int null, payment varchar(10) null, -- 支付方式 kind int null -- 0-充值 1-卡销售 9-产品销售 2-服务 1-取款 8-保证金收入 6-还款 ) create clustered index idx_tbl_profit_day on #tbl_profit_day(code) create table #stat_byday ( day_date varchar(20) not null, cash_card float null, -- 卡销售-现金 cash_srv float null, -- 现金服务 cash_good float null, -- 现金卡销售 cash_total float null, -- 所有现金 credit_card float null, -- 卡销售 - 银行卡 credit_srv float null, -- 服务银行卡 credit_good float null, -- 银行卡买产品 credit_total float null, -- 总银行卡收入 zfb_card float null, -- 卡销售 - 支付宝 zfb_srv float null, -- 服务支付宝 zfb_good float null, -- 支付宝买产品 zfb_total float null, -- 总支付宝收入 wx_card float null, -- 卡销售 - 微信 wx_srv float null, -- 服务微信 wx_good float null, -- 微信买产品 wx_total float null, -- 总微信收入 revene float null, -- 收入 outcome float null, profit float null, defict float null, returnamt float null, --还款,卡销售的还款,不包括项目产品的欠款 pay_card_srv float null, -- 服务项目的卡付, 不按负债标准, 但不包括积分 pay_card_good float null, -- 产品的卡付 pay_coupon float null, -- 券付 支付方式3 pay_coupon2 float null, -- 贵付 支付方式$ pay_coupon3 float null, -- 洗头 支付方式内调 pay_manager float null, -- 经理签单, 包括了 卡销售,项目,产品 sale_card_total float null, -- 总销售卡 pay_card_srv_act float null, -- 卡付实际营业额 = 在服务项目 + 产品中,那些卡付的金额转换为实际营业额 pay_card_srv_work float null, -- 卡付实际劳动业绩 = 在服务项目 + 产品中,那些卡付的金额转换为劳动业绩 draw_total float null, -- 取款总额(包含现金以及内调) work_perf float null, -- 劳动业绩 = 现金(服务) + 现金(物品) + 卡付(物品) + 卡付实营业额 + 项目或物品经理签单 + 内调 + 抵用券 + 银行卡(服务) + 银行卡(物品) cust_quan float null, --客人数量 proj_quan float null, -- 总项目数 pay_manager_prj float null, --只包括项目的经理签单 pay_coupon_srv float null, -- 券付 支付方式3, 只包括项目(含产品) pay_coupon2_srv float null, -- 贵付 支付方式$, 只包括项目(含产品) pay_coupon3_srv float null, -- 洗头 支付方式内调, 只包括项目(含产品) pay_manager_good float null, --只包括产品的经理签单 pay_coupon_good float null, -- 券付 支付方式3, 只包括产品 pay_coupon2_good float null, -- 贵付 支付方式$, 只包括产品 pay_coupon3_good float null, -- 洗头 支付方式内调, 只包括产品 pay_point float null, -- 积分支付,卡销售,项目,产品 pay_point_srv float null, -- 积分支付,只包括项目(含产品) pay_point_good float null, -- 积分支付,只有产品 pay_user01 float null, -- 自定义支付,卡销售,项目,产品 pay_user01_srv float null, -- 自定义支付,只包括项目(含产品) pay_user01_good float null, -- 自定义支付,只有产品 pay_user02 float null, -- 自定义支付,卡销售,项目,产品 pay_user02_srv float null, -- 自定义支付,只包括项目(含产品) pay_user02_good float null, -- 自定义支付,只有产品 pay_user03 float null, -- 自定义支付,卡销售,项目,产品 pay_user03_srv float null, -- 自定义支付,只包括项目(含产品) pay_user03_good float null, -- 自定义支付,只有产品 pay_user04 float null, -- 自定义支付,卡销售,项目,产品 pay_user04_srv float null, -- 自定义支付,只包括项目(含产品) pay_user04_good float null, -- 自定义支付,只有产品 pay_user05 float null, -- 自定义支付,卡销售,项目,产品 pay_user05_srv float null, -- 自定义支付,只包括项目(含产品) pay_user05_good float null, -- 自定义支付,只有产品 other_income float null, -- 其他收入, Huzy 2012/05/31 virtual_income float null, -- 虚拟收入 virtual_outcome float null, -- 虚拟支出 prj_quan_buy float default 0 null, -- 疗程销售次数 prj_amt_buy float default 0 null, -- 疗程销售金额 prj_quan_consume float default 0 null, -- 疗程消耗次数 prj_amt_consume float default 0 null, -- 疗程消耗金额 pay_user06 float null, -- 自定义支付,卡销售,项目,产品 pay_user06_srv float null, -- 自定义支付,只包括项目(含产品) pay_user06_good float null, -- 自定义支付,只有产品 pay_user07 float null, -- 自定义支付,卡销售,项目,产品 pay_user07_srv float null, -- 自定义支付,只包括项目(含产品) pay_user07_good float null, -- 自定义支付,只有产品 pay_user08 float null, -- 自定义支付,卡销售,项目,产品 pay_user08_srv float null, -- 自定义支付,只包括项目(含产品) pay_user08_good float null, -- 自定义支付,只有产品 cash_sum float default 0 null -- 现金类金额 primary key(day_date) ) while (@cur_date <= convert(datetime, @todate)) begin select @strtmp = convert(varchar(10), cast(@cur_date as datetime), 120) select @str_date = substring(@strtmp, 1, 4) + substring(@strtmp, 6, 2) + substring(@strtmp, 9, 2) delete #tbl_profit_day if (@flag = 0) -- 一天一条记录 begin insert #tbl_profit_day(idd, code, itemname, is_add, amt, is_leaf, payment, kind) exec upg_analyse_profit @compid, @str_date, @str_date, 1, @staff, @calmode end else if (@flag = 1) begin insert #tbl_profit_day(idd, code, itemname, is_add, amt, is_leaf, payment, kind) exec upg_analyse_profit @compid, @fromdate, @todate, 1, @staff, @calmode end insert #stat_byday(day_date) values(@str_date) delete tbl_profit_day_buf where compid = @compid and fromdate = @fromdate and todate = @todate and flag = @flag and staff = @staff and fromdepart = '*' and todepart = '' and calmode = @calmode insert tbl_profit_day_buf(compid, fromdate, todate, flag, staff, fromdepart, todepart, calmode, code, itemname, is_add, amt, is_leaf, payment, kind) select @compid, @fromdate, @todate, @flag, @staff, '*', '', @calmode, code, itemname, is_add, amt, is_leaf, payment, kind from #tbl_profit_day -- 统计客户数量 create table #cust_num_by_day ( ddate varchar(20) not null, -- 日期 cust_num float null, -- 数量 primary key(ddate) ) if (@flag = 0) -- 一天一条记录 begin insert #cust_num_by_day(ddate, cust_num) select gga80d, sum(isnull(gga14f, 1)) from ggm01 a with (nolock) where gga00c = @incompid and gga80d >= @fromdate and gga80d <= @todate group by gga80d end else if @flag = 1 -- 所有日期合并一条记录 begin insert #cust_num_by_day(ddate, cust_num) select @str_date, sum(isnull(gga14f, 1)) from ggm01 a with (nolock) where a.gga00c = @incompid and gga80d >= @fromdate and gga80d <= @todate end update a set a.cust_quan = b.cust_num from #stat_byday a, #cust_num_by_day b where a.day_date = b.ddate drop table #cust_num_by_day --转换为汇总表需要的一条记录 set @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = 'HB' update a set a.proj_quan = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 转换为汇总表需要的一条记录 -- 现金(卖卡) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110101' or code = '110104' or code = '110107' or code = '110118' or code = '110140' -- 现金取款 select @tmpamt = 0 select @tmpamt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110119' -- 现金(卖卡) + 现金取款 update a set a.cash_card = isnull(@amt, 0) + isnull(@tmpamt, 0) from #stat_byday a where day_date = @str_date -- 现金(服务) -- 在按项目支付的情况下,是正确的项目的现金支付 -- 在单据支付的情况下,实际上包括了物品销售的现金支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110201' or code = '110230' -- 项目或物品的现金还款也算 还款的不算在 select @tmpamt = 0 select @tmpamt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110301' or code = '110310' -- 在单据支付的情况,这个值为0 , 产品销售的现金以及 产品还款 update a set a.cash_srv = isnull(@amt, 0) - isnull(@tmpamt, 0) from #stat_byday a where day_date = @str_date -- 银行卡(卖卡) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110102' or code = '110105' or code = '110108' or code = '110141' update a set a.credit_card = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 支付宝(卖卡) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '1101Z1' or code = '1101Z2' or code = '1101Z3' or code = '1101Z4' update a set a.zfb_card = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 微信(卖卡) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '1101W1' or code = '1101W2' or code = '1101W3' or code = '1101W4' update a set a.wx_card = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 不管支付方式,实际上的卡销售额 -- 不区分支付方式所有的卡销售以及充值 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '1101' update a set a.sale_card_total = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 银行卡(服务) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110202' or code = '110231' or code = '1102Z2' or code = '1102W2' --项目或物品的银行卡还款不算 select @tmpamt = 0 select @tmpamt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110306' or code = '110311' or code = '1103Z2' or code = '1103W2' -- 产品部分的银行卡支付 以及 银行卡还款 -- 服务产品 - 产品 = 单纯服务项目的收入 update a set a.credit_srv = isnull(@amt, 0) - isnull(@tmpamt, 0) from #stat_byday a where day_date = @str_date -- 支付宝(服务) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '1102Z1' or code = '1102Z2' --项目或物品的支付宝还款不算 select @tmpamt = 0 select @tmpamt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '1103Z1' or code = '1103Z2' -- 产品部分的支付宝支付 以及 支付宝还款 -- 服务产品 - 产品 = 单纯服务项目的收入 update a set a.zfb_srv = isnull(@amt, 0) - isnull(@tmpamt, 0) from #stat_byday a where day_date = @str_date -- 微信(服务) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '1102W1' or code = '1102W2' --项目或物品的微信还款不算 select @tmpamt = 0 select @tmpamt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '1103W1' or code = '1103W2' -- 产品部分的微信支付 以及 微信还款 -- 服务产品 - 产品 = 单纯服务项目的收入 update a set a.wx_srv = isnull(@amt, 0) - isnull(@tmpamt, 0) from #stat_byday a where day_date = @str_date -- 支出 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '12' select @tmpamt = 0 select @tmpamt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '1299' update a set a.outcome = isnull(@amt, 0) - isnull(@tmpamt, 0) from #stat_byday a where day_date = @str_date -- 欠款 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110103' or code = '110106' or code = '110207' or code = '110145' update a set a.defict = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 还款 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (kind = 6 or (code='110230' or code='110231' or code = '1102Z2' or code = '1102W2')) and is_leaf = 1 --modify by lm 2015-1016 包括所有支付方式 update a set a.returnamt = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 卡付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110205' -- 这里面已经不包括了积分 11020507 select @tmpamt = 0 select @tmpamt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110302'--产品卡付部分,需要扣除,否则重复统计 update a set a.pay_card_srv = isnull(@amt, 0) - isnull(@tmpamt, 0) from #stat_byday a where day_date = @str_date -- 卡付实际营业额 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110206' update a set a.pay_card_srv_act = isnull(@amt, 0) from #stat_byday a where day_date = @str_date select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '11AA01' update a set a.pay_card_srv_work = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 抵用券付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110204' or code = '110125' or code = '110126' or code = '110127' or code = '110231$') update a set a.pay_coupon = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 抵用券付(只有项目, 包括产品部分) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110204') update a set a.pay_coupon_srv = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 抵用券付(只包括产品部分) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110303') update a set a.pay_coupon_good = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 抵用券二 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110208' or code = '110130' or code = '110131' or code = '110132' or code = '110231$') -- 因为服务中已经包括了产品的销售, 所以不能再包括110304(产品的$支付) update a set a.pay_coupon2 = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 抵用券二(只有项目) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110208') update a set a.pay_coupon2_srv = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 抵用券, 只有产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110304') update a set a.pay_coupon2_good = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 内调 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where ((code = '110209' or code = '110120' or code = '110121' or code = '110122' or code = '1101400') or (kind = 6 and payment = '0' )) update a set a.pay_coupon3 = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 内调(只包括项目 + 产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110209') update a set a.pay_coupon3_srv = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 内调(只包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110308') update a set a.pay_coupon3_good = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 经理签单, 包括卡销售的经理签单 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110110' or code = '110111' or code = '110203') update a set a.pay_manager = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 经理签单,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110203') update a set a.pay_manager_prj = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 经理签单,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110305') update a set a.pay_manager_good = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 自定义支付, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$1' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8) update a set a.pay_user01 = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$1' and (kind = 2) update a set a.pay_user01_srv = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$1' and (kind = 9) update a set a.pay_user01_good = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 自定义支付, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$2' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8) update a set a.pay_user02 = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$2' and (kind = 2) update a set a.pay_user02_srv = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$2' and (kind = 9) update a set a.pay_user02_good = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 自定义支付, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$3' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8) update a set a.pay_user03 = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$3' and (kind = 2) update a set a.pay_user03_srv = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$3' and (kind = 9) update a set a.pay_user03_good = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 自定义支付, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$4' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8) update a set a.pay_user04 = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$4' and (kind = 2) update a set a.pay_user04_srv = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$4' and (kind = 9) update a set a.pay_user04_good = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 自定义支付, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$5' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8) update a set a.pay_user05 = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$5' and (kind = 2) update a set a.pay_user05_srv = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$5' and (kind = 9) update a set a.pay_user05_good = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -------------新增默认支付$6----------------- --自定义支付$6, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment ='$6' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8) update a set a.pay_user06 = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$6' and (kind = 2) update a set a.pay_user06_srv = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$6' and (kind = 9) update a set a.pay_user06_good = isnull(@amt, 0) from #stat_byday a where day_date = @str_date ----------------------------------------------- -------------新增默认支付$7----------------- --自定义支付$7, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment ='$7' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8) update a set a.pay_user07 = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$7' and (kind = 2) update a set a.pay_user07_srv = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$7' and (kind = 9) update a set a.pay_user07_good = isnull(@amt, 0) from #stat_byday a where day_date = @str_date ----------------------------------------------- -------------新增默认支付$8----------------- --自定义支付$8, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment ='$8' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8) update a set a.pay_user08 = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$8' and (kind = 2) update a set a.pay_user08_srv = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$8' and (kind = 9) update a set a.pay_user08_good = isnull(@amt, 0) from #stat_byday a where day_date = @str_date ----------------------------------------------- -- 其他收入 Huzy 2012/05/31 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '18') update a set a.other_income = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 虚拟收入virtual_income select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = 'n1') update a set a.virtual_income = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 虚拟支出virtual_outcome select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = 'n2') update a set a.virtual_outcome = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 积分支付, 包括卡销售的积分支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110133' or code = '11020507') update a set a.pay_point = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 积分支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '11020507') update a set a.pay_point_srv = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 积分支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110309') update a set a.pay_point_good = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 物品购买的现金 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110301' or code = '110310' -- 包括产品现金的还款 update a set a.cash_good = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 物品购买的银行卡 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110306' or code = '110311' --or code = '1103Z2' or code = '1103W2' -- 包括产品银行卡的还款 update a set a.credit_good = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 物品购买的支付宝 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '1103Z1' or code = '1103Z2' -- 包括产品支付宝的还款 update a set a.zfb_good = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 物品购买的微信 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '1103W1' or code = '1103W2' -- 包括产品微信的还款 update a set a.wx_good = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 物品购买的卡付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110302' update a set a.pay_card_good = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 取款的总额 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110119' or code = '110123' or code = 'drawother' update a set a.draw_total = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 收入都同利润分析表中的收入 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '11' -- 收入 update #stat_byday set revene = @amt where day_date = @str_date --总现金流 modify by lm 2015.12.09 set @cash_sum = 0 select @cash_sum = sum(amt) from #tbl_profit_day where is_leaf = 1 and payment in (select pay from pay_classify where category = '1' and comp = @compid ) and kind <> 9 update a set a.cash_sum = isnull(@cash_sum, 0) from #stat_byday a where day_date = @str_date if (@flag = 0) select @cur_date = dateadd(day, 1, @cur_date) else if (@flag = 1) select @cur_date = dateadd(day, 1, convert(datetime, @todate)) end -- while 循环结束 update #stat_byday set cash_total = isnull(cash_card, 0) + isnull(cash_srv, 0) + isnull(cash_good, 0), cash_sum = cash_sum, credit_total = isnull(credit_card, 0) + isnull(credit_srv, 0) + isnull(credit_good, 0), zfb_total = isnull(zfb_card, 0) + isnull(zfb_srv, 0) + isnull(zfb_good, 0), wx_total = isnull(wx_card, 0) + isnull(wx_srv, 0) + isnull(wx_good, 0) -- 定义劳动业绩的比率 declare @cash_rate float -- 现金:1 declare @cash_good_rate float -- 现金:1 declare @credit_rate float -- 银行卡支付:6 declare @credit_good_rate float -- 银行卡支付:6 declare @pay3_rate float -- 支付方式:3 declare @pay3_good_rate float -- 支付方式:3 declare @pay$_rate float -- 支付方式:$ declare @pay$_good_rate float -- 支付方式:$ declare @pay0_rate float -- 内调:0 declare @pay0_good_rate float -- 内调:0 declare @manager_rate float -- 经理签单:8 declare @manager_good_rate float -- 经理签单:8 declare @point_rate float -- 积分支付:7 declare @point_good_rate float -- 积分支付:7 declare @user01_rate float -- 自定义支付:$1 declare @user01_good_rate float -- 自定义支付:$1 declare @user02_rate float -- 自定义支付:$2 declare @user02_good_rate float -- 自定义支付:$2 declare @user03_rate float -- 自定义支付:$3 declare @user03_good_rate float -- 自定义支付:$3 declare @user04_rate float -- 自定义支付:$4 declare @user04_good_rate float -- 自定义支付:$4 declare @user05_rate float -- 自定义支付:$5 declare @user05_good_rate float -- 自定义支付:$5 declare @zfb_rate float -- 支付宝支付 declare @zfb_good_rate float -- 支付宝支付 declare @wx_rate float -- 微信支付 declare @wx_good_rate float -- 微信支付 declare @user06_rate float declare @user06_good_rate float declare @user07_rate float declare @user07_good_rate float declare @user08_rate float declare @user08_good_rate float exec upg_get_workperf_rate @compid, @cash_rate output, @cash_good_rate output, @credit_rate output, @credit_good_rate output, @pay3_rate output, @pay3_good_rate output, @pay$_rate output, @pay$_good_rate output, @pay0_rate output, @pay0_good_rate output, @manager_rate output, @manager_good_rate output, @point_rate output, @point_good_rate output, @user01_rate output, @user01_good_rate output, @user02_rate output, @user02_good_rate output, @user03_rate output, @user03_good_rate output, @user04_rate output, @user04_good_rate output, @user05_rate output, @user05_good_rate output, @zfb_rate output, @zfb_good_rate output, @wx_rate output, @wx_good_rate output --查询支付方式$6设置的比率 如果不需要计算劳动业绩 返回0 select @user06_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso09f, 0) * isnull(gso03i,1) end, @user06_good_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso10f, 0) * isnull(gso06f,1) end from gsm15 with (nolock) where gso00c = @compid and gso02c = '$6' --查询支付方式$7设置的比率 如果不需要计算劳动业绩 返回0 select @user07_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso09f, 0) * isnull(gso03i,1) end, @user07_good_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso10f, 0) * isnull(gso06f,1) end from gsm15 with (nolock) where gso00c = @compid and gso02c = '$7' --查询支付方式$8设置的比率 如果不需要计算劳动业绩 返回0 select @user08_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso09f, 0) * isnull(gso03i,1) end, @user08_good_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso10f, 0) * isnull(gso06f,1) end from gsm15 with (nolock) where gso00c = @compid and gso02c = '$8' -- 增加劳动业绩的计算 declare @sp056 varchar(20) select @sp056 = gsa02c from gsm01 with (nolock) where gsa00c = @compid and gsa01c = 'SP056' if @sp056 = '4' -- 足疗 begin -- 足疗的劳动业绩 -- 劳动业绩 = 现金(服务) + 现金(物品) + 卡付(物品) + 卡付 + 抵用券 + 银行卡(服务) + 银行卡(物品) + 积分支付(项目 + 产品) update #stat_byday set work_perf = isnull(cash_srv, 0) * @cash_rate + isnull(cash_good, 0) * @cash_good_rate + isnull(pay_card_srv, 0) + isnull(pay_card_good, 0) + isnull(credit_srv, 0) * @credit_rate + isnull(credit_good, 0) * @credit_good_rate + isnull(zfb_srv, 0) * @zfb_rate + isnull(zfb_good, 0) * @zfb_good_rate + isnull(wx_srv, 0) * @wx_rate + isnull(wx_good, 0) * @wx_good_rate + (isnull(pay_coupon_srv, 0) - isnull(pay_coupon_good, 0)) * @pay3_rate + isnull(pay_coupon_good, 0) * @pay3_good_rate + (isnull(pay_point_srv, 0) - isnull(pay_point_good, 0)) * @point_rate + isnull(pay_point_good, 0) * @point_good_rate + (isnull(pay_user01_srv, 0) - isnull(pay_user01_good, 0)) * @user01_rate + isnull(pay_user01_good, 0) * @user01_good_rate + (isnull(pay_user02_srv, 0) - isnull(pay_user02_good, 0)) * @user02_rate + isnull(pay_user02_good, 0) * @user02_good_rate + (isnull(pay_user03_srv, 0) - isnull(pay_user03_good, 0)) * @user03_rate + isnull(pay_user03_good, 0) * @user03_good_rate + (isnull(pay_user04_srv, 0) - isnull(pay_user04_good, 0)) * @user04_rate + isnull(pay_user04_good, 0) * @user04_good_rate + (isnull(pay_user05_srv, 0) - isnull(pay_user05_good, 0)) * @user05_rate + isnull(pay_user05_good, 0) * @user05_good_rate + (isnull(pay_user06_srv, 0) - isnull(pay_user06_good, 0)) * isnull(@user06_rate,0) + isnull(pay_user06_good, 0) * isnull(@user06_good_rate,0) + (isnull(pay_user07_srv, 0) - isnull(pay_user07_good, 0)) * isnull(@user07_rate,0) + isnull(pay_user07_good, 0) * isnull(@user07_good_rate,0) + (isnull(pay_user08_srv, 0) - isnull(pay_user08_good, 0)) * isnull(@user08_rate,0) + isnull(pay_user08_good, 0) * isnull(@user08_good_rate,0) end else begin -- 如果是按项目支付的情况下,采用卡付实际营业额,如单据支付则直接用卡付金额 -- 美发劳动业绩 declare @sp055f float select @sp055f = gsa03f from gsm01 with (nolock) where gsa00c = @incompid and gsa01c = 'SP055' if @sp055f = 1 begin -- 按单据支付 update #stat_byday set work_perf = isnull(cash_srv, 0) * @cash_rate + isnull(cash_good, 0) * @cash_good_rate + isnull(pay_card_srv, 0) + isnull(pay_card_good, 0) + isnull(credit_srv, 0) * @credit_rate + isnull(credit_good, 0) * @credit_good_rate + isnull(zfb_srv, 0) * @zfb_rate + isnull(zfb_good, 0) * @zfb_good_rate + isnull(wx_srv, 0) * @wx_rate + isnull(wx_good, 0) * @wx_good_rate + (isnull(pay_coupon_srv, 0) - isnull(pay_coupon_good, 0)) * @pay3_rate + isnull(pay_coupon_good, 0) * @pay3_good_rate + (isnull(pay_coupon2_srv, 0) - isnull(pay_coupon2_good, 0)) * @pay$_rate + isnull(pay_coupon2_good, 0) * @pay$_good_rate + (isnull(pay_coupon3_srv, 0) - isnull(pay_coupon3_good, 0)) * @pay0_rate + isnull(pay_coupon3_good, 0) * @pay0_good_rate + (isnull(pay_manager_prj, 0) - isnull(pay_manager_good, 0)) * @manager_rate + isnull(pay_manager_good, 0) * @manager_good_rate + (isnull(pay_point_srv, 0) - isnull(pay_point_good, 0)) * @point_rate + isnull(pay_point_good, 0) * @point_good_rate + (isnull(pay_user01_srv, 0) - isnull(pay_user01_good, 0)) * @user01_rate + isnull(pay_user01_good, 0) * @user01_good_rate + (isnull(pay_user02_srv, 0) - isnull(pay_user02_good, 0)) * @user02_rate + isnull(pay_user02_good, 0) * @user02_good_rate + (isnull(pay_user03_srv, 0) - isnull(pay_user03_good, 0)) * @user03_rate + isnull(pay_user03_good, 0) * @user03_good_rate + (isnull(pay_user04_srv, 0) - isnull(pay_user04_good, 0)) * @user04_rate + isnull(pay_user04_good, 0) * @user04_good_rate + (isnull(pay_user05_srv, 0) - isnull(pay_user05_good, 0)) * @user05_rate + isnull(pay_user05_good, 0) * @user05_good_rate + (isnull(pay_user06_srv, 0) - isnull(pay_user06_good, 0)) * isnull(@user06_rate,0) + isnull(pay_user06_good, 0) * isnull(@user06_good_rate,0) + (isnull(pay_user07_srv, 0) - isnull(pay_user07_good, 0)) * isnull(@user07_rate,0) + isnull(pay_user07_good, 0) * isnull(@user07_good_rate ,0) + (isnull(pay_user08_srv, 0) - isnull(pay_user08_good, 0)) * isnull(@user08_rate,0) + isnull(pay_user08_good, 0) * isnull(@user08_good_rate ,0) end else begin -- 劳动业绩 = 现金(服务) + 现金(物品) + 卡付(物品) + 卡付实营业额 + 经理签单 + 内调 + 抵用券 + 银行卡(服务) + 银行卡(物品) + 积分支付(项目 + 产品) update #stat_byday set work_perf = isnull(cash_srv, 0) * @cash_rate + isnull(cash_good, 0) * @cash_good_rate + isnull(pay_card_srv_work, 0) + isnull(credit_srv, 0) * @credit_rate + isnull(credit_good, 0) * @credit_good_rate + isnull(zfb_srv, 0) * @zfb_rate + isnull(zfb_good, 0) * @zfb_good_rate + isnull(wx_srv, 0) * @wx_rate + isnull(wx_good, 0) * @wx_good_rate + (isnull(pay_coupon_srv, 0) - isnull(pay_coupon_good, 0)) * @pay3_rate + isnull(pay_coupon_good, 0) * @pay3_good_rate + (isnull(pay_coupon2_srv, 0) - isnull(pay_coupon2_good, 0)) * @pay$_rate + isnull(pay_coupon2_good, 0) * @pay$_good_rate + (isnull(pay_coupon3_srv, 0) - isnull(pay_coupon3_good, 0)) * @pay0_rate + isnull(pay_coupon3_good, 0) * @pay0_good_rate + (isnull(pay_manager_prj, 0) - isnull(pay_manager_good, 0)) * @manager_rate + isnull(pay_manager_good, 0) * @manager_good_rate + (isnull(pay_point_srv, 0) - isnull(pay_point_good, 0)) * @point_rate + isnull(pay_point_good, 0) * @point_good_rate + (isnull(pay_user01_srv, 0) - isnull(pay_user01_good, 0)) * @user01_rate + isnull(pay_user01_good, 0) * @user01_good_rate + (isnull(pay_user02_srv, 0) - isnull(pay_user02_good, 0)) * @user02_rate + isnull(pay_user02_good, 0) * @user02_good_rate + (isnull(pay_user03_srv, 0) - isnull(pay_user03_good, 0)) * @user03_rate + isnull(pay_user03_good, 0) * @user03_good_rate + (isnull(pay_user04_srv, 0) - isnull(pay_user04_good, 0)) * @user04_rate + isnull(pay_user04_good, 0) * @user04_good_rate + (isnull(pay_user05_srv, 0) - isnull(pay_user05_good, 0)) * @user05_rate + isnull(pay_user05_good, 0) * @user05_good_rate + (isnull(pay_user06_srv, 0) - isnull(pay_user06_good, 0)) * isnull(@user06_rate,0) + isnull(pay_user06_good, 0) * isnull(@user06_good_rate,0) + (isnull(pay_user07_srv, 0) - isnull(pay_user07_good, 0)) * isnull(@user07_rate,0) + isnull(pay_user07_good, 0) * isnull(@user07_good_rate,0) + (isnull(pay_user08_srv, 0) - isnull(pay_user08_good, 0)) * isnull(@user08_rate ,0) + isnull(pay_user08_good, 0) *isnull( @user08_good_rate,0) end end update #stat_byday set profit = isnull(revene, 0) + isnull(other_income, 0) - isnull(outcome, 0) --Start增加疗程销售,消耗 add by lm 2015.10.21 if @staff = '*' or @staff = '' begin select @staff = '%' end create table #prj_stock_buf ( gch02d varchar(8) null, -- 日期 prjnum float null, -- 疗程的数量 prjamt float null, -- 疗程金额 ) create table #prj_stock_buf1 ( gch02d varchar(8) null, -- 日期 prjnum float null, -- 疗程的数量 prjamt float null, -- 疗程金额 ) create table #prj_consume ( gch02d varchar(8) null, -- 日期 prjnum float null, -- 疗程的数量 prjamt float null, -- 疗程金额 ) create table #prj_consume1 ( gch02d varchar(8) null, -- 日期 prjnum float null, -- 疗程的数量 prjamt float null, -- 疗程金额 ) create table #gcm10 ( compid varchar(20) not null, billid varchar(20) not null,--充值单号 primary key(compid,billid) ) create table #gnm01 ( compid varchar(20) not null, billid varchar(20) not null,--充值单号 primary key(compid,billid) ) create table #ggm01 ( compid varchar(20) not null, billid varchar(20) not null,--充值单号 primary key(compid,billid) ) insert #gcm10(compid, billid) select distinct gcl00c, gcl01c from gcm10 a with (nolock) where a.gcl00c = @compid and (a.gcl80d >= @fromdate and a.gcl80d <= @todate) and a.gcl93c like @staff insert #gnm01(compid, billid) select distinct gna00c, gna01c from gnm01 a with (nolock) where a.gna00c = @compid and (a.gna80d >= @fromdate and a.gna80d <= @todate) and a.gna93c like @staff insert #ggm01(compid, billid) select distinct gga00c, gga01c from ggm01 a with (nolock), ggm02 b where a.gga00c = b.ggb00c and a.gga01c = b.ggb01c and a.gga00c = @compid and (a.gga80d >= @fromdate and a.gga80d <= @todate) and a.gga93c like @staff if(@flag = 0) --一天一条记录 begin -- 这个时间点范围内销售的疗程 insert #prj_stock_buf1(gch02d, prjnum, prjamt) select gch02d, sum(gch05f + gch06f), sum(gch07f) from #gnm01 a, gcm08 b with (nolock) where a.compid = b.gch00c and a.billid = b.gch11c and b.gch10c = 'gn' and ((b.gch02d >= @fromdate and b.gch02d <= @todate) or @fromdate = '*') group by gch02d order by gch02d insert #prj_stock_buf1(gch02d, prjnum, prjamt) select gch02d, sum(gch05f + gch06f), sum(gch07f) from #gcm10 a, gcm08 b with (nolock) where a.compid = b.gch00c and a.billid = b.gch11c and b.gch10c = 'gz' and ((b.gch02d >= @fromdate and b.gch02d <= @todate) or @fromdate = '*') group by gch02d order by gch02d --合计 insert #prj_stock_buf (gch02d, prjnum, prjamt) select gch02d, sum(prjnum), sum(prjamt) from #prj_stock_buf1 group by gch02d insert #prj_consume1 (gch02d, prjnum, prjamt) select gch02d, sum(gch08f), sum(gch09f) from #ggm01 a, gcm08 b with (nolock) where a.compid = b.gch00c and a.billid = b.gch11c and b.gch10c = 'gx' and ((b.gch02d >= @fromdate and b.gch02d <= @todate) or @fromdate = '*') group by gch02d order by gch02d --合计 insert #prj_consume (gch02d, prjnum, prjamt) select gch02d, sum(prjnum), sum(prjamt) from #prj_consume1 group by gch02d end else begin -- 这个时间点范围内销售的疗程 insert #prj_stock_buf1(gch02d, prjnum, prjamt) select @str_date, sum(gch05f + gch06f), sum(gch07f) from #gnm01 a, gcm08 b with (nolock) where a.compid = b.gch00c and a.billid = b.gch11c and b.gch10c = 'gn' and ((b.gch02d >= @fromdate and b.gch02d <= @todate) or @fromdate = '*') insert #prj_stock_buf1(gch02d, prjnum, prjamt) select @str_date, sum(gch05f + gch06f), sum(gch07f) from #gcm10 a, gcm08 b with (nolock) where a.compid = b.gch00c and a.billid = b.gch11c and b.gch10c = 'gz' and ((b.gch02d >= @fromdate and b.gch02d <= @todate) or @fromdate = '*') --合计 insert #prj_stock_buf (gch02d, prjnum, prjamt) select @str_date, sum(prjnum), sum(prjamt) from #prj_stock_buf1 -- 这个时间范围内消耗的疗程 insert #prj_consume1 (gch02d, prjnum, prjamt) select @str_date, sum(gch08f), sum(gch09f) from #gcm10 a, gcm08 b with (nolock) where a.compid = b.gch00c and a.billid = b.gch11c and b.gch10c = 'pj' and ((b.gch02d >= @fromdate and b.gch02d <= @todate) or @fromdate = '*') insert #prj_consume1 (gch02d, prjnum, prjamt) select @str_date, sum(gch08f), sum(gch09f) from #ggm01 a, gcm08 b with (nolock) where a.compid = b.gch00c and a.billid = b.gch11c and b.gch10c = 'gx' and ((b.gch02d >= @fromdate and b.gch02d <= @todate) or @fromdate = '*') --合计 insert #prj_consume (gch02d, prjnum, prjamt) select @str_date, sum(prjnum), sum(prjamt) from #prj_consume1 end --疗程销售,消耗金额,数量赋值 update a set a.prj_quan_buy = isnull(b.prjnum,0), a.prj_amt_buy = isnull(b.prjamt,0) from #stat_byday a, #prj_stock_buf b where a.day_date = b.gch02d update a set a.prj_quan_consume = isnull(b.prjnum,0), a.prj_amt_consume = isnull(b.prjamt,0) from #stat_byday a, #prj_consume b where a.day_date = b.gch02d --End select isnull(day_date,'') day_date, isnull(cash_card,0) cash_card, isnull(cash_srv,0) cash_srv, isnull(cash_good,0) cash_good, isnull(cash_total,0) cash_total, isnull(credit_card,0) credit_card, isnull(credit_srv,0) credit_srv, isnull(credit_good,0) credit_good, isnull(credit_total,0) credit_total, isnull(zfb_card,0) zfb_card, isnull(zfb_srv,0) zfb_srv, isnull(zfb_good,0) zfb_good, isnull(zfb_total,0) zfb_total, isnull(wx_card,0) wx_card, isnull(wx_srv,0) wx_srv, isnull(wx_good,0) wx_good, isnull(wx_total,0) wx_total, isnull(revene,0) revene, isnull(outcome,0) outcome, isnull(profit,0) profit, isnull(defict,0) defict, isnull(returnamt,0) returnamt, isnull(pay_card_srv,0) pay_card_srv, isnull(pay_card_good,0) pay_card_good, isnull(pay_coupon,0) pay_coupon, isnull(pay_coupon2,0) pay_coupon2, isnull(pay_coupon3,0) pay_coupon3, isnull(pay_manager,0) pay_manager, isnull(sale_card_total,0) sale_card_total, isnull(pay_card_srv_act,0) pay_card_srv_act, isnull(pay_card_srv_work,0) pay_card_srv_work, isnull(pay_coupon_srv,0) pay_coupon_srv, isnull(pay_coupon2_srv,0) pay_coupon2_srv, isnull(pay_coupon3_srv,0) pay_coupon3_srv, isnull(draw_total,0) draw_total, isnull(work_perf,0) work_perf, isnull(cust_quan,0) cust_quan, isnull(proj_quan,0) proj_quan, isnull(pay_manager_prj,0) pay_manager_prj, isnull(pay_manager_good,0) pay_manager_good, isnull(pay_coupon_good,0) pay_coupon_good, isnull(pay_coupon2_good,0) pay_coupon2_good, isnull(pay_coupon3_good,0) pay_coupon3_good, isnull(pay_point,0) pay_point, isnull(pay_point_srv,0) pay_point_srv, isnull(pay_point_good,0) pay_point_good, isnull(pay_user01,0) pay_user01, isnull(pay_user01_srv,0) pay_user01_srv, isnull(pay_user01_good,0) pay_user01_good, isnull(pay_user02,0) pay_user02, isnull(pay_user02_srv,0) pay_user02_srv, isnull(pay_user02_good,0) pay_user02_good, isnull(pay_user03,0) pay_user03, isnull(pay_user03_srv,0) pay_user03_srv, isnull(pay_user03_good,0) pay_user03_good, isnull(pay_user04,0) pay_user04, isnull(pay_user04_srv,0) pay_user04_srv, isnull(pay_user04_good,0) pay_user04_good, isnull(pay_user05,0) pay_user05, isnull(pay_user05_srv,0) pay_user05_srv, isnull(pay_user05_good,0) pay_user05_good, isnull(pay_user06,0) pay_user06, isnull(pay_user06_srv,0) pay_user06_srv, isnull(pay_user06_good,0) pay_user06_good, isnull(pay_user07,0) pay_user07, isnull(pay_user07_srv,0) pay_user07_srv, isnull(pay_user07_good,0) pay_user07_good, isnull(pay_user08,0) pay_user08, isnull(pay_user08_srv,0) pay_user08_srv, isnull(pay_user08_good,0) pay_user08_good, isnull(other_income,0) other_income, isnull(virtual_income,0) virtual_income, isnull(virtual_outcome,0) virtual_outcome, isnull(prj_quan_buy,0) prj_quan_buy, isnull(prj_amt_buy,0) prj_amt_buy, isnull(prj_quan_consume,0) prj_quan_consume, isnull(prj_amt_consume,0) prj_amt_consume, isnull(cash_sum,0) cash_sum from #stat_byday drop table #stat_byday drop table #tbl_profit_day drop table #gcm10 drop table #gnm01 drop table #ggm01 drop table #prj_stock_buf drop table #prj_stock_buf1 drop table #prj_consume drop table #prj_consume1 end GO /****** Object: StoredProcedure [dbo].[upgsh_create_gcm20_every_day] Script Date: 06/12/2017 06:20:53 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upgsh_create_gcm20_every_day] as begin declare @curdate varchar(20) declare @time varchar(20) exec upg_getcurrdate @curdate output, @time output declare @yesterday datetime select @yesterday = getdate() - 1 declare @calmode int select @calmode = gsa03f from gsm01 where gsa00c = '001' and gsa01c = 'SP115' declare @acctdate varchar(20) select @acctdate = substring(convert(varchar(20), @yesterday, 102), 1, 4) + substring(convert(varchar(20), @yesterday, 102), 6, 2) + substring(convert(varchar(20), @yesterday, 102), 9, 2) -- 自动生成卡交易历史 exec upg_create_gcm20_from_raw_bills @acctdate, @acctdate -- 自动进行账户数据统计 exec upg_cal_profit_all @acctdate, @acctdate, @calmode -- 自动生成日结数据 exec upg_create_dailydata '001', @acctdate, @acctdate -- 记录操作历史 insert sysoperationlog(userid, program, operation, operationdate, operationtime, origatedate, compid, keyvalue1, keyvalue2, keyvalue3) values('system', 'GSB24I', 'M', @curdate, @time, @acctdate, '001', @acctdate, @acctdate, 'upgsh_create_gcm20_every_day') end GO /****** Object: StoredProcedure [dbo].[upg_stat_byday_depart] Script Date: 06/12/2017 06:20:42 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_stat_byday_depart] ( @compid varchar(10), @fromdate varchar(10), @todate varchar(10), @flag int, -- 0-每天一纪录 1-返回一条记录 @staff varchar(20), @fromdepart varchar(10), -- add by lyj 2007-09-06 @todepart varchar(10), @calmode int -- 0-营业款 1-营业收入 2-综合小单 ) as begin declare @cur_date datetime declare @strtmp varchar(30) declare @str_date varchar(20) declare @amt float declare @tmpamt float declare @cash_sum float declare @qry_compid varchar(20) declare @incompid varchar(10) select @incompid = @compid if @compid = '001' or @compid = '*' begin select @qry_compid = '%' end else begin select @qry_compid = @compid end set @cur_date = convert(datetime, @fromdate) create table #tbl_profit_day ( idd int not null, code varchar(20) not null, itemname varchar(60) null, is_add int null, amt float null, is_leaf int null, payment varchar(10) null, -- 支付方式 kind int null -- 0 充值 1卡销售 9产品销售 2 服务 -1 取款 8 保证金收入 6还款 ) create clustered index idx_tbl_profit_day on #tbl_profit_day(code) create table #stat_byday ( day_date varchar(20) not null, cash_card float null, -- 卡销售-现金 cash_srv float null, cash_good float null, cash_total float null, credit_card float null, credit_srv float null, credit_good float null, credit_total float null, zfb_card float null, zfb_srv float null, zfb_good float null, zfb_total float null, wx_card float null, wx_srv float null, wx_good float null, wx_total float null, revene float null, outcome float null, profit float null, defict float null, returnamt float null, pay_card_srv float null, -- 卡付中,已经不包括积分支付了 pay_card_good float null, -- 卡付,产品,不包括积分支付 pay_coupon float null, -- 券付 支付方式3 pay_coupon2 float null, -- 贵付 支付方式$ pay_coupon3 float null, -- 洗头 支付方式内调 pay_manager float null, -- 经理签单 sale_card_total float null, -- 总销售卡 pay_card_srv_act float null, -- 卡付实际营业额 = 在服务项目中,那些卡付的金额转换为实际营业额 pay_card_srv_work float null, -- 卡付实际劳动业绩 = 在服务项目 + 产品中,那些卡付的金额转换为劳动业绩 pay_coupon_srv float null, -- 券付 支付方式3, 只包括项目 pay_coupon2_srv float null, -- 贵付 支付方式$, 只包括项目 pay_coupon3_srv float null, -- 洗头 支付方式内调, 只包括项目 draw_total float null, -- 取款总额(包含现金以及内调) work_perf float null, -- 劳动业绩 = 现金(服务) + 现金(产品) + 卡付(产品) + 卡付实营业额 + 经理签单 + 内调 + 抵用券 + 银行卡(服务) + 银行卡(产品) cust_quan float null, -- 客人数量 proj_quan float null, -- 项目数 pay_manager_prj float null, --只包括项目的经理签单 pay_manager_good float null, --只包括产品的经理签单 pay_coupon_good float null, -- 券付 支付方式3, 只包括产品 pay_coupon2_good float null, -- 贵付 支付方式$, 只包括产品 pay_coupon3_good float null, -- 洗头 支付方式内调, 只包括产品 pay_point float null, -- 积分支付,卡销售,项目,产品 pay_point_srv float null, -- 积分支付,只包括项目(含产品) pay_point_good float null, -- 积分支付,只有产品 pay_user01 float null, -- 自定义支付,卡销售,项目,产品 pay_user01_srv float null, -- 自定义支付,只包括项目(含产品) pay_user01_good float null, -- 自定义支付,只有产品 pay_user02 float null, -- 自定义支付,卡销售,项目,产品 pay_user02_srv float null, -- 自定义支付,只包括项目(含产品) pay_user02_good float null, -- 自定义支付,只有产品 pay_user03 float null, -- 自定义支付,卡销售,项目,产品 pay_user03_srv float null, -- 自定义支付,只包括项目(含产品) pay_user03_good float null, -- 自定义支付,只有产品 pay_user04 float null, -- 自定义支付,卡销售,项目,产品 pay_user04_srv float null, -- 自定义支付,只包括项目(含产品) pay_user04_good float null, -- 自定义支付,只有产品 pay_user05 float null, -- 自定义支付,卡销售,项目,产品 pay_user05_srv float null, -- 自定义支付,只包括项目(含产品) pay_user05_good float null, -- 自定义支付,只有产品 other_income float null, -- 其他收入 Huzy 2012/05/31 virtual_income float null, --虚拟收入 virtual_outcome float null, --虚拟支出 prj_quan_buy float default 0 null, -- 疗程销售次数 prj_amt_buy float default 0 null, -- 疗程销售金额 prj_quan_consume float default 0 null, -- 疗程消耗次数 prj_amt_consume float default 0 null, -- 疗程消耗金额 cash_sum float default 0 null, -- 现金类金额 prj_quan_lc float default 0 null, -- 疗程金额 实际劳动业绩需要加上疗程金额 pay_user06 float null, -- 自定义支付,卡销售,项目,产品 pay_user06_srv float null, -- 自定义支付,只包括项目(含产品) pay_user06_good float null, -- 自定义支付,只有产品 pay_user07 float null, -- 自定义支付,卡销售,项目,产品 pay_user07_srv float null, -- 自定义支付,只包括项目(含产品) pay_user07_good float null, -- 自定义支付,只有产品 pay_user08 float null, -- 自定义支付,卡销售,项目,产品 pay_user08_srv float null, -- 自定义支付,只包括项目(含产品) pay_user08_good float null, -- 自定义支付,只有产品 pay_user09 float null, -- 自定义支付,卡销售,项目,产品 pay_user09_srv float null, -- 自定义支付,只包括项目(含产品) pay_user09_good float null, -- 自定义支付,只有产品 pay_user10 float null, -- 自定义支付,卡销售,项目,产品 pay_user10_srv float null, -- 自定义支付,只包括项目(含产品) pay_user10_good float null, -- 自定义支付,只有产品 pay_user11 float null, -- 自定义支付,卡销售,项目,产品 pay_user11_srv float null, -- 自定义支付,只包括项目(含产品) pay_user11_good float null, -- 自定义支付,只有产品 pay_user12 float null, -- 自定义支付,卡销售,项目,产品 pay_user12_srv float null, -- 自定义支付,只包括项目(含产品) pay_user12_good float null, -- 自定义支付,只有产品 pay_user13 float null, -- 自定义支付,卡销售,项目,产品 pay_user13_srv float null, -- 自定义支付,只包括项目(含产品) pay_user13_good float null, -- 自定义支付,只有产品 pay_user14 float null, -- 自定义支付,卡销售,项目,产品 pay_user14_srv float null, -- 自定义支付,只包括项目(含产品) pay_user14_good float null, -- 自定义支付,只有产品 pay_user15 float null, -- 自定义支付,卡销售,项目,产品 pay_user15_srv float null, -- 自定义支付,只包括项目(含产品) pay_user15_good float null, -- 自定义支付,只有产品 primary key(day_date)) while (@cur_date <= convert(datetime, @todate)) begin select @strtmp = convert(varchar(10), cast(@cur_date as datetime), 120); select @str_date = substring(@strtmp, 1, 4) + substring(@strtmp, 6, 2) + substring(@strtmp, 9, 2) delete #tbl_profit_day if (@flag = 0) -- 一天一条记录 begin insert #tbl_profit_day(idd, code, itemname, is_add, amt, is_leaf, payment, kind) exec upg_analyse_profit_depart @compid, @str_date, @str_date, 1, @staff, @fromdepart, @todepart, @calmode --add by lyj 2007-09-06 end else if (@flag = 1) begin insert #tbl_profit_day(idd, code, itemname, is_add, amt, is_leaf, payment, kind) exec upg_analyse_profit_depart @compid, @fromdate, @todate, 1, @staff, @fromdepart, @todepart, @calmode --add by lyj 2007-09-06 end --把数据保存进去供别的用途 delete tbl_profit_day_buf where compid = @compid and fromdate = @fromdate and todate = @todate and flag = @flag and staff = @staff and fromdepart = @fromdepart and todepart = @todepart and calmode = @calmode insert tbl_profit_day_buf(compid, fromdate, todate, flag, staff, fromdepart, todepart, calmode, code, itemname, is_add, amt, is_leaf, payment, kind) select @compid, @fromdate, @todate, @flag, @staff, @fromdepart, @todepart, @calmode, code, itemname, is_add, amt, is_leaf, payment, kind from #tbl_profit_day insert #stat_byday(day_date) values(@str_date) --统计客户数量 create table #cust_num_by_day( ddate varchar(20) not null, -- 日期 cust_num float null, -- 数量 primary key(ddate)) if (@flag = 0) -- 一天一条记录 begin insert #cust_num_by_day(ddate, cust_num) select gga80d, sum(isnull(gga14f, 1)) from ggm01 with(nolock) where gga00c like @qry_compid and gga80d >= @fromdate and gga80d <= @todate group by gga80d end else if @flag = 1 -- 所有日期合并一条记录 begin insert #cust_num_by_day(ddate, cust_num) select @str_date, sum(isnull(gga14f, 1)) from ggm01 with(nolock) where gga00c like @qry_compid and gga80d >= @fromdate and gga80d <= @todate end update a set a.cust_quan = b.cust_num from #stat_byday a, #cust_num_by_day b where a.day_date = b.ddate drop table #cust_num_by_day --转换为汇总表需要的一条记录 set @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = 'HB' update a set a.proj_quan = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --现金(卖卡) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110101' or code = '110104' or code = '110107' or code = '110118' or code = '110140' --现金取款 select @tmpamt = 0 select @tmpamt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110119' update a set a.cash_card = isnull(@amt, 0) + isnull(@tmpamt, 0) from #stat_byday a where day_date = @str_date --现金(服务) --在按项目支付的情况下,是正确的项目的现金支付 --在单据支付的情况下,实际上包括了产品销售的现金支付 select @amt = 0 select @tmpamt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110201' or code = '110230' -- 项目或物品的现金还款也算 还款的不算在 select @tmpamt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110301' or code = '110310' -- 在单据支付的情况,这个值为0, 包括了产品的现金还款 update a set a.cash_srv = isnull(@amt, 0) - isnull(@tmpamt, 0) from #stat_byday a where day_date = @str_date --银行卡(卖卡) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110102' or code = '110105' or code = '110108' or code = '110141' update a set a.credit_card = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --支付宝(卖卡) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '1101Z1' or code = '1101Z2' or code = '1101Z3' or code = '1101Z4' update a set a.zfb_card = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --微信(卖卡) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '1101W1' or code = '1101W2' or code = '1101W3' or code = '1101W4' update a set a.wx_card = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --不管支付方式,实际上的卡销售额 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '1101' update a set a.sale_card_total = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --银行卡(服务) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110202' select @tmpamt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110306' or code = '110311' -- 产品销售的银行卡支付 以及 还款 update a set a.credit_srv = case when isnull(@amt, 0) <> 0 then isnull(@amt, 0) - isnull(@tmpamt, 0) else 0 end from #stat_byday a where day_date = @str_date --支付宝(服务) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '1102Z1' select @tmpamt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '1103Z1' -- or code = '1103Z2' 产品销售的银行卡支付 以及 还款 update a set a.zfb_srv = isnull(@amt, 0) - isnull(@tmpamt, 0) from #stat_byday a where day_date = @str_date --微信(服务) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '1102W1' select @tmpamt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '1103W1' -- or code = '1103W2' 产品销售的微信支付 以及 还款 update a set a.wx_srv = isnull(@amt, 0) - isnull(@tmpamt, 0) from #stat_byday a where day_date = @str_date --支出 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '12' select @tmpamt = 0 select @tmpamt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '1299' update a set a.outcome = isnull(@amt, 0) - isnull(@tmpamt, 0) from #stat_byday a where day_date = @str_date --欠款 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110103' or code = '110106' or code = '110207' or code = '110145' update a set a.defict = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --还款 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where kind = 6 and is_leaf = 1 update a set a.returnamt = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --卡付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110205' select @tmpamt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110302' update a set a.pay_card_srv = isnull(@amt, 0) - isnull(@tmpamt, 0) from #stat_byday a where day_date = @str_date --卡付实际营业额 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110206' update a set a.pay_card_srv_act = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --疗程金额 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '11020509' update a set a.prj_quan_lc = isnull(@amt, 0) from #stat_byday a where day_date = @str_date select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '11AA01' update a set a.pay_card_srv_work = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --抵用券付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110204' or code = '110125' or code = '110126' or code = '110127') update a set a.pay_coupon = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --抵用券付, 只包括项目 + 产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110204') update a set a.pay_coupon_srv = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --抵用券付, 只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110303') update a set a.pay_coupon_good = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --贵付(抵用券二) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110208' or code = '110130' or code = '110131' or code = '110132') update a set a.pay_coupon2 = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --贵付(抵用券二), 只包括项目 + 产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110208') update a set a.pay_coupon2_srv = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --贵付(抵用券二), 只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110304') update a set a.pay_coupon2_good = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --内调(洗头) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where ((code = '110209' or code = '110120' or code = '110121' or code = '110122' or code = '1101400') or (kind = 6 and payment = '0' )) update a set a.pay_coupon3 = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --内调(洗头), 只包括项目 + 产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110209') update a set a.pay_coupon3_srv = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --内调(洗头), 只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110308') update a set a.pay_coupon3_good = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --经理签单, 包括卡销售的经理签单 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110110' or code = '110111' or code = '110203') update a set a.pay_manager = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --经理签单,只包括项目 + 产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110203') update a set a.pay_manager_prj = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --经理签单,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110305') update a set a.pay_manager_good = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --自定义支付, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$1' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8) update a set a.pay_user01 = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$1' and (kind = 2) update a set a.pay_user01_srv = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$1' and (kind = 9) update a set a.pay_user01_good = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --自定义支付, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$2' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8) update a set a.pay_user02 = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$2' and (kind = 2) update a set a.pay_user02_srv = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$2' and (kind = 9) update a set a.pay_user02_good = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --自定义支付, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$3' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8) update a set a.pay_user03 = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$3' and (kind = 2) update a set a.pay_user03_srv = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$3' and (kind = 9) update a set a.pay_user03_good = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --自定义支付, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$4' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8) update a set a.pay_user04 = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$4' and (kind = 2) update a set a.pay_user04_srv = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$4' and (kind = 9) update a set a.pay_user04_good = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --自定义支付, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment ='$5' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8) update a set a.pay_user05 = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$5' and (kind = 2) update a set a.pay_user05_srv = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$5' and (kind = 9) update a set a.pay_user05_good = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -------------新增默认支付$6----------------- --自定义支付$6, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment ='$6' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8) update a set a.pay_user06 = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$6' and (kind = 2) update a set a.pay_user06_srv = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$6' and (kind = 9) update a set a.pay_user06_good = isnull(@amt, 0) from #stat_byday a where day_date = @str_date ----------------------------------------------- -------------新增默认支付$7----------------- --自定义支付$7, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment ='$7' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8) update a set a.pay_user07 = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$7' and (kind = 2) update a set a.pay_user07_srv = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$7' and (kind = 9) update a set a.pay_user07_good = isnull(@amt, 0) from #stat_byday a where day_date = @str_date ----------------------------------------------- -------------新增默认支付$8----------------- --自定义支付$8, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment ='$8' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8) update a set a.pay_user08 = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$8' and (kind = 2) update a set a.pay_user08_srv = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$8' and (kind = 9) update a set a.pay_user08_good = isnull(@amt, 0) from #stat_byday a where day_date = @str_date ----------------------------------------------- -------------新增默认支付$9----------------- --自定义支付$9, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment ='$9' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8) update a set a.pay_user09 = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$9' and (kind = 2) update a set a.pay_user09_srv = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$9' and (kind = 9) update a set a.pay_user09_good = isnull(@amt, 0) from #stat_byday a where day_date = @str_date ----------------------------------------------- -------------新增默认支付$10----------------- --自定义支付$10, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment ='$10' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8) update a set a.pay_user10 = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$10' and (kind = 2) update a set a.pay_user10_srv = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$10' and (kind = 9) update a set a.pay_user10_good = isnull(@amt, 0) from #stat_byday a where day_date = @str_date ----------------------------------------------- -------------新增默认支付$11----------------- --自定义支付$11, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment ='$11' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8) update a set a.pay_user11 = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$11' and (kind = 2) update a set a.pay_user11_srv = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$11' and (kind = 9) update a set a.pay_user11_good = isnull(@amt, 0) from #stat_byday a where day_date = @str_date ----------------------------------------------- -------------新增默认支付$12----------------- --自定义支付$12, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment ='$12' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8) update a set a.pay_user12 = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$12' and (kind = 2) update a set a.pay_user12_srv = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$12' and (kind = 9) update a set a.pay_user12_good = isnull(@amt, 0) from #stat_byday a where day_date = @str_date ----------------------------------------------- -------------新增默认支付$13----------------- --自定义支付$13, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment ='$13' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8) update a set a.pay_user13 = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$13' and (kind = 2) update a set a.pay_user13_srv = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$13' and (kind = 9) update a set a.pay_user13_good = isnull(@amt, 0) from #stat_byday a where day_date = @str_date ----------------------------------------------- -------------新增默认支付$14----------------- --自定义支付$14, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment ='$14' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8) update a set a.pay_user14 = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$14' and (kind = 2) update a set a.pay_user14_srv = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$14' and (kind = 9) update a set a.pay_user14_good = isnull(@amt, 0) from #stat_byday a where day_date = @str_date ----------------------------------------------- -------------新增默认支付$15----------------- --自定义支付$15, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment ='$15' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8) update a set a.pay_user15 = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$15' and (kind = 2) update a set a.pay_user15_srv = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$15' and (kind = 9) update a set a.pay_user15_good = isnull(@amt, 0) from #stat_byday a where day_date = @str_date ----------------------------------------------- --其他收入 Huzy 2012/05/31 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '18') update a set a.other_income = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --虚拟收入virtual_income select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = 'n1') update a set a.virtual_income = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --虚拟支出virtual_outcome select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = 'n2') update a set a.virtual_outcome = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --积分支付, 包括卡销售的积分支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110133' or code = '11020507') update a set a.pay_point = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --积分支付,只包括项目 + 产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '11020507') update a set a.pay_point_srv = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --积分支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110309') update a set a.pay_point_good = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --产品购买的现金 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110301' or code = '110310' -- 包括产品现金的还款 update a set a.cash_good = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --产品购买的银行卡 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110306' or code = '110311' -- 包括产品银行卡的还款 update a set a.credit_good = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --产品购买的支付宝 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '1103Z1' or code = '1103Z2' -- 包括产品支付宝的还款 update a set a.zfb_good = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --产品购买的微信 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '1103W1' or code = '1103W2' -- 包括产品微信的还款 update a set a.wx_good = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --产品购买的卡付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110302' update a set a.pay_card_good = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --取款的总额 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110119' or code = '110123' or code = 'drawother' update a set a.draw_total = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --收入都同利润分析表中的收入 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '11' -- 收入 update #stat_byday set revene = @amt where day_date = @str_date --总现金流 modify by lm 2015.12.09 set @cash_sum = 0 select @cash_sum = sum(amt) from #tbl_profit_day where is_leaf = 1 and payment in (select pay from pay_classify where category = '1' and comp = @compid ) and kind <> 9 update a set a.cash_sum = isnull(@cash_sum, 0) from #stat_byday a where day_date = @str_date if (@flag = 0) select @cur_date = dateadd(day, 1, @cur_date) else if (@flag = 1) select @cur_date = dateadd(day, 1, convert(datetime, @todate)) end -- while 循环结束 update #stat_byday set cash_total = isnull(cash_card, 0) + isnull(cash_srv, 0) + isnull(cash_good, 0), cash_sum = cash_sum, credit_total = isnull(credit_card, 0) + isnull(credit_srv, 0) + isnull(credit_good, 0), zfb_total = isnull(zfb_card, 0) + isnull(zfb_srv, 0) + isnull(zfb_good, 0), wx_total = isnull(wx_card, 0) + isnull(wx_srv, 0) + isnull(wx_good, 0) -- 定义劳动业绩的比率 declare @cash_rate float -- 现金:1 declare @cash_good_rate float -- 现金:1 declare @credit_rate float -- 银行卡支付:6 declare @credit_good_rate float -- 银行卡支付:6 declare @pay3_rate float -- 支付方式:3 declare @pay3_good_rate float -- 支付方式:3 declare @pay$_rate float -- 支付方式:$ declare @pay$_good_rate float -- 支付方式:$ declare @pay0_rate float -- 内调:0 declare @pay0_good_rate float -- 内调:0 declare @manager_rate float -- 经理签单:8 declare @manager_good_rate float -- 经理签单:8 declare @point_rate float -- 积分支付:7 declare @point_good_rate float -- 积分支付:7 declare @user01_rate float -- 自定义支付:$1 declare @user01_good_rate float -- 自定义支付:$1 declare @user02_rate float -- 自定义支付:$2 declare @user02_good_rate float -- 自定义支付:$2 declare @user03_rate float -- 自定义支付:$3 declare @user03_good_rate float -- 自定义支付:$3 declare @user04_rate float -- 自定义支付:$4 declare @user04_good_rate float -- 自定义支付:$4 declare @user05_rate float -- 自定义支付:$5 declare @user05_good_rate float -- 自定义支付:$5 declare @zfb_rate float -- 支付宝支付 declare @zfb_good_rate float -- 支付宝支付 declare @wx_rate float -- 微信支付 declare @wx_good_rate float -- 微信支付 declare @prj_quan_lc float --疗程 declare @user06_rate float declare @user06_good_rate float declare @user07_rate float declare @user07_good_rate float declare @user08_rate float declare @user08_good_rate float declare @user09_rate float declare @user09_good_rate float declare @user10_rate float declare @user10_good_rate float declare @user11_rate float declare @user11_good_rate float declare @user12_rate float declare @user12_good_rate float declare @user13_rate float declare @user13_good_rate float declare @user14_rate float declare @user14_good_rate float declare @user15_rate float declare @user15_good_rate float --查询疗程设置的比率 如果不需要计算劳动业绩 返回0 select @prj_quan_lc = case when isnull(gso08f,0) = 0 then 0 else isnull(gso09f, 0) * isnull(gso03i,1) end from gsm15 with (nolock) where gso00c = @compid and gso02c = '9' --查询支付方式$6设置的比率 如果不需要计算劳动业绩 返回0 select @user06_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso09f, 0) * isnull(gso03i,1) end, @user06_good_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso10f, 0) * isnull(gso06f,1) end from gsm15 with (nolock) where gso00c = @compid and gso02c = '$6' --查询支付方式$7设置的比率 如果不需要计算劳动业绩 返回0 select @user07_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso09f, 0) * isnull(gso03i,1) end, @user07_good_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso10f, 0) * isnull(gso06f,1) end from gsm15 with (nolock) where gso00c = @compid and gso02c = '$7' --查询支付方式$8设置的比率 如果不需要计算劳动业绩 返回0 select @user08_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso09f, 0) * isnull(gso03i,1) end, @user08_good_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso10f, 0) * isnull(gso06f,1) end from gsm15 with (nolock) where gso00c = @compid and gso02c = '$8' --查询支付方式$9设置的比率 如果不需要计算劳动业绩 返回0 select @user09_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso09f, 0) * isnull(gso03i,1) end, @user09_good_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso10f, 0) * isnull(gso06f,1) end from gsm15 with (nolock) where gso00c = @compid and gso02c = '$9' --查询支付方式$10设置的比率 如果不需要计算劳动业绩 返回0 select @user10_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso09f, 0) * isnull(gso03i,1) end, @user10_good_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso10f, 0) * isnull(gso06f,1) end from gsm15 with (nolock) where gso00c = @compid and gso02c = '$10' --查询支付方式$11设置的比率 如果不需要计算劳动业绩 返回0 select @user11_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso09f, 0) * isnull(gso03i,1) end, @user11_good_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso10f, 0) * isnull(gso06f,1) end from gsm15 with (nolock) where gso00c = @compid and gso02c = '$11' --查询支付方式$12设置的比率 如果不需要计算劳动业绩 返回0 select @user12_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso09f, 0) * isnull(gso03i,1) end, @user12_good_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso10f, 0) * isnull(gso06f,1) end from gsm15 with (nolock) where gso00c = @compid and gso02c = '$12' --查询支付方式$13设置的比率 如果不需要计算劳动业绩 返回0 select @user13_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso09f, 0) * isnull(gso03i,1) end, @user13_good_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso10f, 0) * isnull(gso06f,1) end from gsm15 with (nolock) where gso00c = @compid and gso02c = '$13' --查询支付方式$14设置的比率 如果不需要计算劳动业绩 返回0 select @user14_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso09f, 0) * isnull(gso03i,1) end, @user14_good_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso10f, 0) * isnull(gso06f,1) end from gsm15 with (nolock) where gso00c = @compid and gso02c = '$14' --查询支付方式$15设置的比率 如果不需要计算劳动业绩 返回0 select @user15_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso09f, 0) * isnull(gso03i,1) end, @user15_good_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso10f, 0) * isnull(gso06f,1) end from gsm15 with (nolock) where gso00c = @compid and gso02c = '$15' exec upg_get_workperf_rate @compid, @cash_rate output, @cash_good_rate output, @credit_rate output, @credit_good_rate output, @pay3_rate output, @pay3_good_rate output, @pay$_rate output, @pay$_good_rate output, @pay0_rate output, @pay0_good_rate output, @manager_rate output, @manager_good_rate output, @point_rate output, @point_good_rate output, @user01_rate output, @user01_good_rate output, @user02_rate output, @user02_good_rate output, @user03_rate output, @user03_good_rate output, @user04_rate output, @user04_good_rate output, @user05_rate output, @user05_good_rate output, @zfb_rate output, @zfb_good_rate output, @wx_rate output, @wx_good_rate output -- 增加劳动业绩的计算 declare @sp056 varchar(20) select @sp056 = gsa02c from gsm01 with (nolock) where gsa00c = @compid and gsa01c = 'SP056' if @sp056 = '4' -- 足疗 begin -- 足疗的劳动业绩 -- 劳动业绩 = 现金(服务) + 现金(产品) + 卡付(产品) + 卡付 + 抵用券 + 银行卡(服务) + 银行卡(产品) + 积分支付 update #stat_byday set work_perf = isnull(cash_srv, 0) * @cash_rate + isnull(cash_good, 0) * @cash_good_rate + isnull(pay_card_srv, 0) + isnull(pay_card_good, 0) + isnull(credit_srv, 0) * @credit_rate + isnull(credit_good, 0) * @credit_good_rate + isnull(zfb_srv, 0) * @zfb_rate + isnull(zfb_good, 0) * @zfb_good_rate + isnull(wx_srv, 0) * @wx_rate + isnull(wx_good, 0) * @wx_good_rate + isnull(prj_quan_lc,0) * @prj_quan_lc + (isnull(pay_coupon_srv, 0) - isnull(pay_coupon_good, 0)) * @pay3_rate + isnull(pay_coupon_good, 0) * @pay3_good_rate + (isnull(pay_point_srv, 0) - isnull(pay_point_good, 0)) * @point_rate + isnull(pay_point_good, 0) * @point_good_rate + (isnull(pay_user01_srv, 0) - isnull(pay_user01_good, 0)) * @user01_rate + isnull(pay_user01_good, 0) * @user01_good_rate + (isnull(pay_user02_srv, 0) - isnull(pay_user02_good, 0)) * @user02_rate + isnull(pay_user02_good, 0) * @user02_good_rate + (isnull(pay_user03_srv, 0) - isnull(pay_user03_good, 0)) * @user03_rate + isnull(pay_user03_good, 0) * @user03_good_rate + (isnull(pay_user04_srv, 0) - isnull(pay_user04_good, 0)) * @user04_rate + isnull(pay_user04_good, 0) * @user04_good_rate + (isnull(pay_user05_srv, 0) - isnull(pay_user05_good, 0)) * @user05_rate + isnull(pay_user05_good, 0) * @user05_good_rate + (isnull(pay_user06_srv, 0) - isnull(pay_user06_good, 0)) * isnull(@user06_rate,0) + isnull(pay_user06_good, 0) * isnull(@user06_good_rate,0) + (isnull(pay_user07_srv, 0) - isnull(pay_user07_good, 0)) * isnull(@user07_rate,0) + isnull(pay_user07_good, 0) * isnull(@user07_good_rate,0) + (isnull(pay_user08_srv, 0) - isnull(pay_user08_good, 0)) * isnull(@user08_rate,0) + isnull(pay_user08_good, 0) * isnull(@user08_good_rate,0) + (isnull(pay_user09_srv, 0) - isnull(pay_user09_good, 0)) * isnull(@user09_rate,0) + isnull(pay_user09_good, 0) * isnull(@user09_good_rate,0) + (isnull(pay_user10_srv, 0) - isnull(pay_user10_good, 0)) * isnull(@user10_rate,0) + isnull(pay_user10_good, 0) * isnull(@user10_good_rate,0) + (isnull(pay_user11_srv, 0) - isnull(pay_user11_good, 0)) * isnull(@user11_rate,0) + isnull(pay_user11_good, 0) * isnull(@user11_good_rate,0) + (isnull(pay_user12_srv, 0) - isnull(pay_user12_good, 0)) * isnull(@user12_rate,0) + isnull(pay_user12_good, 0) * isnull(@user12_good_rate,0) + (isnull(pay_user13_srv, 0) - isnull(pay_user13_good, 0)) * isnull(@user13_rate,0) + isnull(pay_user13_good, 0) * isnull(@user13_good_rate,0) + (isnull(pay_user14_srv, 0) - isnull(pay_user14_good, 0)) * isnull(@user14_rate,0) + isnull(pay_user14_good, 0) * isnull(@user14_good_rate,0) + (isnull(pay_user15_srv, 0) - isnull(pay_user15_good, 0)) * isnull(@user15_rate,0) + isnull(pay_user15_good, 0) * isnull(@user15_good_rate,0) end else begin -- 如果是按项目支付的情况下,采用卡付实际营业额,如单据支付则直接用卡付金额 -- 美发劳动业绩 declare @sp055f float select @sp055f = gsa03f from gsm01 with (nolock) where gsa00c = @incompid and gsa01c = 'SP055' if @sp055f = 1 begin -- 按单据支付 update #stat_byday set work_perf = isnull(cash_srv, 0) * @cash_rate + isnull(cash_good, 0) * @cash_good_rate + isnull(pay_card_srv, 0) + isnull(pay_card_good, 0) + isnull(credit_srv, 0) * @credit_rate + isnull(credit_good, 0) * @credit_good_rate + isnull(zfb_srv, 0) * @zfb_rate + isnull(zfb_good, 0) * @zfb_good_rate + isnull(wx_srv, 0) * @wx_rate + isnull(wx_good, 0) * @wx_good_rate + isnull(prj_quan_lc,0) * @prj_quan_lc + (isnull(pay_coupon_srv, 0) - isnull(pay_coupon_good, 0)) * @pay3_rate + isnull(pay_coupon_good, 0) * @pay3_good_rate + (isnull(pay_coupon2_srv, 0) - isnull(pay_coupon2_good, 0)) * @pay$_rate + isnull(pay_coupon2_good, 0) * @pay$_good_rate + (isnull(pay_coupon3_srv, 0) - isnull(pay_coupon3_good, 0)) * @pay0_rate + isnull(pay_coupon3_good, 0) * @pay0_good_rate + (isnull(pay_manager_prj, 0) - isnull(pay_manager_good, 0)) * @manager_rate + isnull(pay_manager_good, 0) * @manager_good_rate + (isnull(pay_point_srv, 0) - isnull(pay_point_good, 0)) * @point_rate + isnull(pay_point_good, 0) * @point_good_rate + (isnull(pay_user01_srv, 0) - isnull(pay_user01_good, 0)) * @user01_rate + isnull(pay_user01_good, 0) * @user01_good_rate + (isnull(pay_user02_srv, 0) - isnull(pay_user02_good, 0)) * @user02_rate + isnull(pay_user02_good, 0) * @user02_good_rate + (isnull(pay_user03_srv, 0) - isnull(pay_user03_good, 0)) * @user03_rate + isnull(pay_user03_good, 0) * @user03_good_rate + (isnull(pay_user04_srv, 0) - isnull(pay_user04_good, 0)) * @user04_rate + isnull(pay_user04_good, 0) * @user04_good_rate + (isnull(pay_user05_srv, 0) - isnull(pay_user05_good, 0)) * @user05_rate + isnull(pay_user05_good, 0) * @user05_good_rate + (isnull(pay_user06_srv, 0) - isnull(pay_user06_good, 0)) * isnull(@user06_rate,0) + isnull(pay_user06_good, 0) * isnull(@user06_good_rate,0) + (isnull(pay_user07_srv, 0) - isnull(pay_user07_good, 0)) * isnull(@user07_rate,0) + isnull(pay_user07_good, 0) * isnull(@user07_good_rate ,0) + (isnull(pay_user08_srv, 0) - isnull(pay_user08_good, 0)) * isnull(@user08_rate,0) + isnull(pay_user08_good, 0) * isnull(@user08_good_rate ,0) + (isnull(pay_user09_srv, 0) - isnull(pay_user09_good, 0)) * isnull(@user09_rate,0) + isnull(pay_user09_good, 0) * isnull(@user09_good_rate ,0) + (isnull(pay_user10_srv, 0) - isnull(pay_user10_good, 0)) * isnull(@user10_rate,0) + isnull(pay_user10_good, 0) * isnull(@user10_good_rate ,0) + (isnull(pay_user11_srv, 0) - isnull(pay_user11_good, 0)) * isnull(@user11_rate,0) + isnull(pay_user11_good, 0) * isnull(@user11_good_rate ,0) + (isnull(pay_user12_srv, 0) - isnull(pay_user12_good, 0)) * isnull(@user12_rate,0) + isnull(pay_user12_good, 0) * isnull(@user12_good_rate ,0) + (isnull(pay_user13_srv, 0) - isnull(pay_user13_good, 0)) * isnull(@user13_rate,0) + isnull(pay_user13_good, 0) * isnull(@user13_good_rate ,0) + (isnull(pay_user14_srv, 0) - isnull(pay_user14_good, 0)) * isnull(@user14_rate,0) + isnull(pay_user14_good, 0) * isnull(@user14_good_rate ,0) + (isnull(pay_user15_srv, 0) - isnull(pay_user15_good, 0)) * isnull(@user15_rate,0) + isnull(pay_user15_good, 0) * isnull(@user15_good_rate ,0) end else begin -- 劳动业绩 = 现金(服务) + 现金(产品) + 卡付(产品) + 卡付实营业额 + 经理签单 + 内调 + 抵用券 + 银行卡(服务) + 银行卡(产品) + 积分支付 update #stat_byday set work_perf = isnull(cash_srv, 0) * @cash_rate + isnull(cash_good, 0) * @cash_good_rate + isnull(pay_card_srv_work, 0) + isnull(pay_card_good,0) + isnull(credit_srv, 0) * @credit_rate + isnull(credit_good, 0) * @credit_good_rate + isnull(zfb_srv, 0) * @zfb_rate + isnull(zfb_good, 0) * @zfb_good_rate + isnull(wx_srv, 0) * @wx_rate + isnull(wx_good, 0) * @wx_good_rate + isnull(prj_quan_lc,0) * @prj_quan_lc + (isnull(pay_coupon_srv, 0) - isnull(pay_coupon_good, 0)) * @pay3_rate + isnull(pay_coupon_good, 0) * @pay3_good_rate + (isnull(pay_coupon2_srv, 0) - isnull(pay_coupon2_good, 0)) * @pay$_rate + isnull(pay_coupon2_good, 0) * @pay$_good_rate + (isnull(pay_coupon3_srv, 0) - isnull(pay_coupon3_good, 0)) * @pay0_rate + isnull(pay_coupon3_good, 0) * @pay0_good_rate + (isnull(pay_manager_prj, 0) - isnull(pay_manager_good, 0)) * @manager_rate + isnull(pay_manager_good, 0) * @manager_good_rate + (isnull(pay_point_srv, 0) - isnull(pay_point_good, 0)) * @point_rate + isnull(pay_point_good, 0) * @point_good_rate + (isnull(pay_user01_srv, 0) - isnull(pay_user01_good, 0)) * @user01_rate + isnull(pay_user01_good, 0) * @user01_good_rate + (isnull(pay_user02_srv, 0) - isnull(pay_user02_good, 0)) * @user02_rate + isnull(pay_user02_good, 0) * @user02_good_rate + (isnull(pay_user03_srv, 0) - isnull(pay_user03_good, 0)) * @user03_rate + isnull(pay_user03_good, 0) * @user03_good_rate + (isnull(pay_user04_srv, 0) - isnull(pay_user04_good, 0)) * @user04_rate + isnull(pay_user04_good, 0) * @user04_good_rate + (isnull(pay_user05_srv, 0) - isnull(pay_user05_good, 0)) * @user05_rate + isnull(pay_user05_good, 0) * @user05_good_rate + (isnull(pay_user06_srv, 0) - isnull(pay_user06_good, 0)) * isnull(@user06_rate,0) + isnull(pay_user06_good, 0) * isnull(@user06_good_rate,0) + (isnull(pay_user07_srv, 0) - isnull(pay_user07_good, 0)) * isnull(@user07_rate,0) + isnull(pay_user07_good, 0) * isnull(@user07_good_rate,0) + (isnull(pay_user08_srv, 0) - isnull(pay_user08_good, 0)) * isnull(@user08_rate ,0) + isnull(pay_user08_good, 0) *isnull( @user08_good_rate,0) + (isnull(pay_user09_srv, 0) - isnull(pay_user09_good, 0)) * isnull(@user09_rate ,0) + isnull(pay_user09_good, 0) *isnull( @user09_good_rate,0) + (isnull(pay_user10_srv, 0) - isnull(pay_user10_good, 0)) * isnull(@user10_rate ,0) + isnull(pay_user10_good, 0) *isnull( @user10_good_rate,0) + (isnull(pay_user11_srv, 0) - isnull(pay_user11_good, 0)) * isnull(@user11_rate ,0) + isnull(pay_user11_good, 0) *isnull( @user11_good_rate,0) + (isnull(pay_user12_srv, 0) - isnull(pay_user12_good, 0)) * isnull(@user12_rate ,0) + isnull(pay_user12_good, 0) *isnull( @user12_good_rate,0) + (isnull(pay_user13_srv, 0) - isnull(pay_user13_good, 0)) * isnull(@user13_rate ,0) + isnull(pay_user13_good, 0) *isnull( @user13_good_rate,0) + (isnull(pay_user14_srv, 0) - isnull(pay_user14_good, 0)) * isnull(@user14_rate ,0) + isnull(pay_user14_good, 0) *isnull( @user14_good_rate,0) + (isnull(pay_user15_srv, 0) - isnull(pay_user15_good, 0)) * isnull(@user15_rate ,0) + isnull(pay_user15_good, 0) *isnull( @user15_good_rate,0) end end --start 计算项目,产品中不需要计入劳动业绩的 lucz 2016.02.16 declare @del_sumamt_xm float --2服务 declare @del_sumamt_cp float --9产品销售 select @del_sumamt_xm = sum(amt) from #tbl_profit_day where kind = 2 and code not in ('110208','110130','110131', '110132') and payment in (select gso02c from gsm15 where gso00c = @compid and gso01c = '3' and gso09f = 0 ) and (payment = '#' or payment = '4' or (payment >= 'A' and payment <= 'Z')) and payment not in ('WX', 'ZFB') select @del_sumamt_cp =sum(amt) from #tbl_profit_day where kind = 9 and code ! = '110304' and payment in (select gso02c from gsm15 where gso00c = @compid and gso01c = '3' and gso10f = 0 ) and (payment = '#' or payment = '4' or (payment >= 'A' and payment <= 'Z')) and payment not in ('WX', 'ZFB') update #stat_byday set work_perf = case when isnull(work_perf,0) <> 0 then work_perf - (isnull(@del_sumamt_xm,0) + isnull(@del_sumamt_cp,0)) else (isnull(@del_sumamt_xm,0) + isnull(@del_sumamt_cp,0)) end -- end lucz 2016.02.16 update #stat_byday set profit = isnull(revene, 0) + isnull(other_income, 0) - isnull(outcome, 0) --Start增加疗程销售,消耗 add by lm 2015.10.21 create table #prj_stock_buf ( gch02d varchar(8) null, -- 日期 prjnum float null, -- 疗程的数量 prjamt float null, -- 疗程金额 ) create table #prj_stock_buf1 ( gch02d varchar(8) null, -- 日期 prjnum float null, -- 疗程的数量 prjamt float null, -- 疗程金额 ) create table #prj_consume ( gch02d varchar(8) null, -- 日期 prjnum float null, -- 疗程的数量 prjamt float null, -- 疗程金额 ) create table #prj_consume1 ( gch02d varchar(8) null, -- 日期 prjnum float null, -- 疗程的数量 prjamt float null, -- 疗程金额 ) create table #gcm10 ( compid varchar(20) not null, billid varchar(20) not null,--充值单号 primary key(compid,billid) ) create table #gnm01 ( compid varchar(20) not null, billid varchar(20) not null,--充值单号 primary key(compid,billid) ) create table #ggm01 ( compid varchar(20) not null, billid varchar(20) not null,--充值单号 primary key(compid,billid) ) insert #gcm10(compid, billid) select distinct gcl00c, gcl01c from gcm10 a with (nolock), gam26 z with (nolock) where a.gcl00c = z.gaz02c and z.gaz01c = @compid and ((a.gcl02d >= @fromdate and a.gcl02d <= @todate) or @fromdate = '*') and a.gcl93c like @staff and ((gcl81c >= @fromdepart and gcl81c <= @todepart) or (gcl82c >= @fromdepart and gcl82c <= @todepart) or (gcl83c >= @fromdepart and gcl83c <= @todepart) or (gcl100c >= @fromdepart and gcl100c <= @todepart) or (gcl101c >= @fromdepart and gcl101c <= @todepart) or (gcl102c >= @fromdepart and gcl102c <= @todepart) or (gcl103c >= @fromdepart and gcl103c <= @todepart) or (gcl104c >= @fromdepart and gcl104c <= @todepart) or (gcl105c >= @fromdepart and gcl105c <= @todepart) or (gcl106c >= @fromdepart and gcl106c <= @todepart) ) insert #gnm01(compid, billid) select distinct gna00c, gna01c from gnm01 a with (nolock), gam26 z with (nolock) where a.gna00c = z.gaz02c and z.gaz01c = @compid and ((a.gna02d >= @fromdate and a.gna02d <= @todate) or @fromdate = '*') and a.gna93c like @staff and ((gna81c >= @fromdepart and gna81c <= @todepart) or (gna82c >= @fromdepart and gna82c <= @todepart) or (gna83c >= @fromdepart and gna83c <= @todepart) or (gna100c >= @fromdepart and gna100c <= @todepart) or (gna101c >= @fromdepart and gna101c <= @todepart) or (gna102c >= @fromdepart and gna102c <= @todepart) or (gna103c >= @fromdepart and gna103c <= @todepart) or (gna104c >= @fromdepart and gna104c <= @todepart) or (gna105c >= @fromdepart and gna105c <= @todepart) or (gna106c >= @fromdepart and gna106c <= @todepart) ) insert #ggm01(compid, billid) select distinct gga00c, gga01c from ggm01 a with (nolock), ggm02 b, gam26 z with (nolock) where a.gga00c = b.ggb00c and a.gga01c = b.ggb01c and a.gga00c = z.gaz02c and z.gaz01c = @compid and a.gga93c like @staff and ((a.gga02d >= @fromdate and a.gga02d <= @todate) or @fromdate = '*') and (ggb81c >= @fromdepart and ggb81c <= @todepart) if(@flag = 0) --一天一条记录 begin -- 这个时间点范围内销售的疗程 insert #prj_stock_buf1(gch02d, prjnum, prjamt) select gch02d, sum(gch05f + gch06f), sum(gch07f) from #gnm01 a, gcm08 b with (nolock) where a.compid = b.gch00c and a.billid = b.gch11c and b.gch10c = 'gn' and ((b.gch02d >= @fromdate and b.gch02d <= @todate) or @fromdate = '*') group by gch02d order by gch02d insert #prj_stock_buf1(gch02d, prjnum, prjamt) select gch02d, sum(gch05f + gch06f), sum(gch07f) from #gcm10 a, gcm08 b with (nolock) where a.compid = b.gch00c and a.billid = b.gch11c and b.gch10c = 'gz' and ((b.gch02d >= @fromdate and b.gch02d <= @todate) or @fromdate = '*') group by gch02d order by gch02d --合计 insert #prj_stock_buf (gch02d, prjnum, prjamt) select gch02d, sum(prjnum), sum(prjamt) from #prj_stock_buf1 group by gch02d -- 这个时间范围内消耗的疗程 -- insert #prj_consume1 (gch02d, prjnum, prjamt) -- select gch02d, sum(gch08f), sum(gch09f) -- from #gcm10 a, gcm08 b with (nolock), gam26 z with (nolock) -- where a.compid = b.gch00c -- and a.billid = b.gch11c -- and a.compid = z.gaz02c -- and z.gaz01c = @compid -- and b.gch10c = 'pj' -- and ((b.gch02d >= @fromdate and b.gch02d <= @todate) or @fromdate = '*') -- group by gch02d -- order by gch02d insert #prj_consume1 (gch02d, prjnum, prjamt) select gch02d, sum(gch08f), sum(gch09f) from #ggm01 a, gcm08 b with (nolock) where a.compid = b.gch00c and a.billid = b.gch11c and b.gch10c = 'gx' and ((b.gch02d >= @fromdate and b.gch02d <= @todate) or @fromdate = '*') group by gch02d order by gch02d --合计 insert #prj_consume (gch02d, prjnum, prjamt) select gch02d, sum(prjnum), sum(prjamt) from #prj_consume1 group by gch02d end else begin -- 这个时间点范围内销售的疗程 insert #prj_stock_buf1(gch02d, prjnum, prjamt) select @str_date, sum(gch05f + gch06f), sum(gch07f) from #gnm01 a, gcm08 b with (nolock) where a.compid = b.gch00c and a.billid = b.gch11c and b.gch10c = 'gn' and ((b.gch02d >= @fromdate and b.gch02d <= @todate) or @fromdate = '*') insert #prj_stock_buf1(gch02d, prjnum, prjamt) select @str_date, sum(gch05f + gch06f), sum(gch07f) from #gcm10 a, gcm08 b with (nolock) where a.compid = b.gch00c and a.billid = b.gch11c and b.gch10c = 'gz' and ((b.gch02d >= @fromdate and b.gch02d <= @todate) or @fromdate = '*') --合计 insert #prj_stock_buf (gch02d, prjnum, prjamt) select @str_date, sum(prjnum), sum(prjamt) from #prj_stock_buf1 -- 这个时间范围内消耗的疗程 insert #prj_consume1 (gch02d, prjnum, prjamt) select @str_date, sum(gch08f), sum(gch09f) from #gcm10 a, gcm08 b with (nolock) where a.compid = b.gch00c and a.billid = b.gch11c and b.gch10c = 'pj' and ((b.gch02d >= @fromdate and b.gch02d <= @todate) or @fromdate = '*') insert #prj_consume1 (gch02d, prjnum, prjamt) select @str_date, sum(gch08f), sum(gch09f) from #ggm01 a, gcm08 b with (nolock) where a.compid = b.gch00c and a.billid = b.gch11c and b.gch10c = 'gx' and ((b.gch02d >= @fromdate and b.gch02d <= @todate) or @fromdate = '*') --合计 insert #prj_consume (gch02d, prjnum, prjamt) select @str_date, sum(prjnum), sum(prjamt) from #prj_consume1 end --疗程销售,消耗金额,数量赋值 update a set a.prj_quan_buy = isnull(b.prjnum,0), a.prj_amt_buy = isnull(b.prjamt,0) from #stat_byday a, #prj_stock_buf b where a.day_date = b.gch02d update a set a.prj_quan_consume = isnull(b.prjnum,0), a.prj_amt_consume = isnull(b.prjamt,0) from #stat_byday a, #prj_consume b where a.day_date = b.gch02d --End select isnull(day_date,'') day_date, isnull(cash_card,0) cash_card, isnull(cash_srv,0) cash_srv, isnull(cash_good,0) cash_good, isnull(cash_total,0) cash_total, isnull(credit_card,0) credit_card, isnull(credit_srv,0) credit_srv, isnull(credit_good,0) credit_good, isnull(credit_total,0) credit_total, isnull(zfb_card,0) zfb_card, isnull(zfb_srv,0) zfb_srv, isnull(zfb_good,0) zfb_good, isnull(zfb_total,0) zfb_total, isnull(wx_card,0) wx_card, isnull(wx_srv,0) wx_srv, isnull(wx_good,0) wx_good, isnull(wx_total,0) wx_total, isnull(revene,0) revene, isnull(outcome,0) outcome, isnull(profit,0) profit, isnull(defict,0) defict, isnull(returnamt,0) returnamt, isnull(pay_card_srv,0) pay_card_srv, isnull(pay_card_good,0) pay_card_good, isnull(pay_coupon,0) pay_coupon, isnull(pay_coupon2,0) pay_coupon2, isnull(pay_coupon3,0) pay_coupon3, isnull(pay_manager,0) pay_manager, isnull(sale_card_total,0) sale_card_total, isnull(pay_card_srv_act,0) pay_card_srv_act, isnull(pay_card_srv_work,0) pay_card_srv_work, isnull(pay_coupon_srv,0) pay_coupon_srv, isnull(pay_coupon2_srv,0) pay_coupon2_srv, isnull(pay_coupon3_srv,0) pay_coupon3_srv, isnull(draw_total,0) draw_total, isnull(work_perf,0) work_perf, isnull(cust_quan,0) cust_quan, isnull(proj_quan,0) proj_quan, isnull(pay_manager_prj,0) pay_manager_prj, isnull(pay_manager_good,0) pay_manager_good, isnull(pay_coupon_good,0) pay_coupon_good, isnull(pay_coupon2_good,0) pay_coupon2_good, isnull(pay_coupon3_good,0) pay_coupon3_good, isnull(pay_point,0) pay_point, isnull(pay_point_srv,0) pay_point_srv, isnull(pay_point_good,0) pay_point_good, isnull(pay_user01,0) pay_user01, isnull(pay_user01_srv,0) pay_user01_srv, isnull(pay_user01_good,0) pay_user01_good, isnull(pay_user02,0) pay_user02, isnull(pay_user02_srv,0) pay_user02_srv, isnull(pay_user02_good,0) pay_user02_good, isnull(pay_user03,0) pay_user03, isnull(pay_user03_srv,0) pay_user03_srv, isnull(pay_user03_good,0) pay_user03_good, isnull(pay_user04,0) pay_user04, isnull(pay_user04_srv,0) pay_user04_srv, isnull(pay_user04_good,0) pay_user04_good, isnull(pay_user05,0) pay_user05, isnull(pay_user05_srv,0) pay_user05_srv, isnull(pay_user05_good,0) pay_user05_good, isnull(pay_user06,0) pay_user06, isnull(pay_user06_srv,0) pay_user06_srv, isnull(pay_user06_good,0) pay_user06_good, isnull(pay_user07,0) pay_user07, isnull(pay_user07_srv,0) pay_user07_srv, isnull(pay_user07_good,0) pay_user07_good, isnull(pay_user08,0) pay_user08, isnull(pay_user08_srv,0) pay_user08_srv, isnull(pay_user08_good,0) pay_user08_good, isnull(pay_user09,0) pay_user09, isnull(pay_user09_srv,0) pay_user09_srv, isnull(pay_user09_good,0) pay_user09_good, isnull(pay_user10,0) pay_user10, isnull(pay_user10_srv,0) pay_user10_srv, isnull(pay_user10_good,0) pay_user10_good, isnull(pay_user11,0) pay_user11, isnull(pay_user11_srv,0) pay_user11_srv, isnull(pay_user11_good,0) pay_user11_good, isnull(pay_user12,0) pay_user12, isnull(pay_user12_srv,0) pay_user12_srv, isnull(pay_user12_good,0) pay_user12_good, isnull(pay_user13,0) pay_user13, isnull(pay_user13_srv,0) pay_user13_srv, isnull(pay_user13_good,0) pay_user13_good, isnull(pay_user14,0) pay_user14, isnull(pay_user14_srv,0) pay_user14_srv, isnull(pay_user14_good,0) pay_user14_good, isnull(pay_user15,0) pay_user15, isnull(pay_user15_srv,0) pay_user15_srv, isnull(pay_user15_good,0) pay_user15_good, isnull(other_income,0) other_income, isnull(virtual_income,0) virtual_income, isnull(virtual_outcome,0) virtual_outcome, isnull(prj_quan_buy,0) prj_quan_buy, isnull(prj_amt_buy,0) prj_amt_buy, isnull(prj_quan_consume,0) prj_quan_consume, isnull(prj_amt_consume,0) prj_amt_consume, isnull(cash_sum,0) cash_sum from #stat_byday drop table #stat_byday drop table #tbl_profit_day drop table #gcm10 drop table #gnm01 drop table #ggm01 drop table #prj_stock_buf drop table #prj_stock_buf1 drop table #prj_consume drop table #prj_consume1 end GO /****** Object: StoredProcedure [dbo].[upg_stat_byday] Script Date: 06/12/2017 06:20:42 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_stat_byday] ( @compid varchar(10), -- 已经能支持各级别的公司 @fromdate varchar(10), @todate varchar(10), @flag int, -- 0-每天一纪录 1-返回一条记录 @staff varchar(20), @calmode int -- 0-营业款 1-营业收入 2-综合小单 ) as begin declare @incompid varchar(10) select @incompid = @compid declare @cur_date datetime declare @strtmp varchar(30) declare @str_date varchar(20) declare @amt float declare @tmpamt float declare @cash_sum float declare @qry_compid varchar(20) if @compid = '001' or @compid = '*' begin select @qry_compid = '%' end else begin select @qry_compid = @compid end set @cur_date = convert(datetime, @fromdate) create table #tbl_profit_day ( idd int not null, code varchar(20) not null, itemname varchar(60) null, is_add int null, amt float null, is_leaf int null, payment varchar(10) null, -- 支付方式 kind int null -- 0-充值 1-卡销售 9-产品销售 2-服务 1-取款 8-保证金收入 6-还款 ) create clustered index idx_tbl_profit_day on #tbl_profit_day(code) create table #stat_byday ( day_date varchar(20) not null, cash_card float null, -- 卡销售-现金 cash_srv float null, -- 现金服务 cash_good float null, -- 现金卡销售 cash_total float null, -- 所有现金 credit_card float null, -- 卡销售 - 银行卡 credit_srv float null, -- 服务银行卡 credit_good float null, -- 银行卡买产品 credit_total float null, -- 总银行卡收入 zfb_card float null, -- 卡销售 - 支付宝 zfb_srv float null, -- 服务支付宝 zfb_good float null, -- 支付宝买产品 zfb_total float null, -- 总支付宝收入 wx_card float null, -- 卡销售 - 微信 wx_srv float null, -- 服务微信 wx_good float null, -- 微信买产品 wx_total float null, -- 总微信收入 revene float null, -- 收入 outcome float null, profit float null, defict float null, returnamt float null, --还款,卡销售的还款,不包括项目产品的欠款 pay_card_srv float null, -- 服务项目的卡付, 不按负债标准, 但不包括积分 pay_card_good float null, -- 产品的卡付 pay_coupon float null, -- 券付 支付方式3 pay_coupon2 float null, -- 贵付 支付方式$ pay_coupon3 float null, -- 洗头 支付方式内调 pay_manager float null, -- 经理签单, 包括了 卡销售,项目,产品 sale_card_total float null, -- 总销售卡 pay_card_srv_act float null, -- 卡付实际营业额 = 在服务项目 + 产品中,那些卡付的金额转换为实际营业额 pay_card_srv_work float null, -- 卡付实际劳动业绩 = 在服务项目 + 产品中,那些卡付的金额转换为劳动业绩 draw_total float null, -- 取款总额(包含现金以及内调) work_perf float null, -- 劳动业绩 = 现金(服务) + 现金(物品) + 卡付(物品) + 卡付实营业额 + 项目或物品经理签单 + 内调 + 抵用券 + 银行卡(服务) + 银行卡(物品) cust_quan float null, --客人数量 proj_quan float null, -- 总项目数 pay_manager_prj float null, --只包括项目的经理签单 pay_coupon_srv float null, -- 券付 支付方式3, 只包括项目(含产品) pay_coupon2_srv float null, -- 贵付 支付方式$, 只包括项目(含产品) pay_coupon3_srv float null, -- 洗头 支付方式内调, 只包括项目(含产品) pay_manager_good float null, --只包括产品的经理签单 pay_coupon_good float null, -- 券付 支付方式3, 只包括产品 pay_coupon2_good float null, -- 贵付 支付方式$, 只包括产品 pay_coupon3_good float null, -- 洗头 支付方式内调, 只包括产品 pay_point float null, -- 积分支付,卡销售,项目,产品 pay_point_srv float null, -- 积分支付,只包括项目(含产品) pay_point_good float null, -- 积分支付,只有产品 pay_user01 float null, -- 自定义支付,卡销售,项目,产品 pay_user01_srv float null, -- 自定义支付,只包括项目(含产品) pay_user01_good float null, -- 自定义支付,只有产品 pay_user02 float null, -- 自定义支付,卡销售,项目,产品 pay_user02_srv float null, -- 自定义支付,只包括项目(含产品) pay_user02_good float null, -- 自定义支付,只有产品 pay_user03 float null, -- 自定义支付,卡销售,项目,产品 pay_user03_srv float null, -- 自定义支付,只包括项目(含产品) pay_user03_good float null, -- 自定义支付,只有产品 pay_user04 float null, -- 自定义支付,卡销售,项目,产品 pay_user04_srv float null, -- 自定义支付,只包括项目(含产品) pay_user04_good float null, -- 自定义支付,只有产品 pay_user05 float null, -- 自定义支付,卡销售,项目,产品 pay_user05_srv float null, -- 自定义支付,只包括项目(含产品) pay_user05_good float null, -- 自定义支付,只有产品 other_income float null, -- 其他收入, Huzy 2012/05/31 virtual_income float null, -- 虚拟收入 virtual_outcome float null, -- 虚拟支出 prj_quan_buy float default 0 null, -- 疗程销售次数 prj_amt_buy float default 0 null, -- 疗程销售金额 prj_quan_consume float default 0 null, -- 疗程消耗次数 prj_amt_consume float default 0 null, -- 疗程消耗金额 pay_user06 float null, -- 自定义支付,卡销售,项目,产品 pay_user06_srv float null, -- 自定义支付,只包括项目(含产品) pay_user06_good float null, -- 自定义支付,只有产品 pay_user07 float null, -- 自定义支付,卡销售,项目,产品 pay_user07_srv float null, -- 自定义支付,只包括项目(含产品) pay_user07_good float null, -- 自定义支付,只有产品 pay_user08 float null, -- 自定义支付,卡销售,项目,产品 pay_user08_srv float null, -- 自定义支付,只包括项目(含产品) pay_user08_good float null, -- 自定义支付,只有产品 pay_user09 float null, -- 自定义支付,卡销售,项目,产品 pay_user09_srv float null, -- 自定义支付,只包括项目(含产品) pay_user09_good float null, -- 自定义支付,只有产品 pay_user10 float null, -- 自定义支付,卡销售,项目,产品 pay_user10_srv float null, -- 自定义支付,只包括项目(含产品) pay_user10_good float null, -- 自定义支付,只有产品 pay_user11 float null, -- 自定义支付,卡销售,项目,产品 pay_user11_srv float null, -- 自定义支付,只包括项目(含产品) pay_user11_good float null, -- 自定义支付,只有产品 pay_user12 float null, -- 自定义支付,卡销售,项目,产品 pay_user12_srv float null, -- 自定义支付,只包括项目(含产品) pay_user12_good float null, -- 自定义支付,只有产品 pay_user13 float null, -- 自定义支付,卡销售,项目,产品 pay_user13_srv float null, -- 自定义支付,只包括项目(含产品) pay_user13_good float null, -- 自定义支付,只有产品 pay_user14 float null, -- 自定义支付,卡销售,项目,产品 pay_user14_srv float null, -- 自定义支付,只包括项目(含产品) pay_user14_good float null, -- 自定义支付,只有产品 pay_user15 float null, -- 自定义支付,卡销售,项目,产品 pay_user15_srv float null, -- 自定义支付,只包括项目(含产品) pay_user15_good float null, -- 自定义支付,只有产品 cash_sum float default 0 null -- 现金类金额 primary key(day_date) ) while (@cur_date <= convert(datetime, @todate)) begin select @strtmp = convert(varchar(10), cast(@cur_date as datetime), 120) select @str_date = substring(@strtmp, 1, 4) + substring(@strtmp, 6, 2) + substring(@strtmp, 9, 2) delete #tbl_profit_day if (@flag = 0) -- 一天一条记录 begin insert #tbl_profit_day(idd, code, itemname, is_add, amt, is_leaf, payment, kind) exec upg_analyse_profit @compid, @str_date, @str_date, 1, @staff, @calmode end else if (@flag = 1) begin insert #tbl_profit_day(idd, code, itemname, is_add, amt, is_leaf, payment, kind) exec upg_analyse_profit @compid, @fromdate, @todate, 1, @staff, @calmode end insert #stat_byday(day_date) values(@str_date) delete tbl_profit_day_buf where compid = @compid and fromdate = @fromdate and todate = @todate and flag = @flag and staff = @staff and fromdepart = '*' and todepart = '' and calmode = @calmode insert tbl_profit_day_buf(compid, fromdate, todate, flag, staff, fromdepart, todepart, calmode, code, itemname, is_add, amt, is_leaf, payment, kind) select @compid, @fromdate, @todate, @flag, @staff, '*', '', @calmode, code, itemname, is_add, amt, is_leaf, payment, kind from #tbl_profit_day -- 统计客户数量 create table #cust_num_by_day ( ddate varchar(20) not null, -- 日期 cust_num float null, -- 数量 primary key(ddate) ) if (@flag = 0) -- 一天一条记录 begin insert #cust_num_by_day(ddate, cust_num) select gga80d, sum(isnull(gga14f, 1)) from ggm01 a with (nolock), gam26 z where gga00c = z.gaz02c and z.gaz01c = @incompid and gga80d >= @fromdate and gga80d <= @todate group by gga80d end else if @flag = 1 -- 所有日期合并一条记录 begin insert #cust_num_by_day(ddate, cust_num) select @str_date, sum(isnull(gga14f, 1)) from ggm01 a with (nolock), gam26 z where a.gga00c = z.gaz02c and z.gaz01c = @incompid and gga80d >= @fromdate and gga80d <= @todate end update a set a.cust_quan = b.cust_num from #stat_byday a, #cust_num_by_day b where a.day_date = b.ddate drop table #cust_num_by_day --转换为汇总表需要的一条记录 set @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = 'HB' update a set a.proj_quan = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 转换为汇总表需要的一条记录 -- 现金(卖卡) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110101' or code = '110104' or code = '110107' or code = '110118' or code = '110140' -- 现金取款 select @tmpamt = 0 select @tmpamt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110119' -- 现金(卖卡) + 现金取款 update a set a.cash_card = isnull(@amt, 0) + isnull(@tmpamt, 0) from #stat_byday a where day_date = @str_date -- 现金(服务) -- 在按项目支付的情况下,是正确的项目的现金支付 -- 在单据支付的情况下,实际上包括了物品销售的现金支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110201' or code = '110230' -- 项目或物品的现金还款也算 还款的不算在 select @tmpamt = 0 select @tmpamt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110301' or code = '110310' -- 在单据支付的情况,这个值为0 , 产品销售的现金以及 产品还款 update a set a.cash_srv = isnull(@amt, 0) - isnull(@tmpamt, 0) from #stat_byday a where day_date = @str_date -- 银行卡(卖卡) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110102' or code = '110105' or code = '110108' or code = '110141' or code = '110105' update a set a.credit_card = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 支付宝(卖卡) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '1101Z1' or code = '1101Z2' or code = '1101Z3' or code = '1101Z4' update a set a.zfb_card = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 微信(卖卡) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '1101W1' or code = '1101W2' or code = '1101W3' or code = '1101W4' update a set a.wx_card = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 不管支付方式,实际上的卡销售额 -- 不区分支付方式所有的卡销售以及充值 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '1101' update a set a.sale_card_total = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 银行卡(服务) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110202' or code = '110231' or code = '1102Z2' or code = '1102W2' --项目或物品的银行卡还款不算 select @tmpamt = 0 select @tmpamt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110306' or code = '110311' or code = '1103Z2' or code = '1103W2' -- 产品部分的银行卡支付 以及 银行卡还款 -- 服务产品 - 产品 = 单纯服务项目的收入 update a set a.credit_srv = isnull(@amt, 0) - isnull(@tmpamt, 0) from #stat_byday a where day_date = @str_date -- 支付宝(服务) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '1102Z1' or code = '1102Z2' --项目或物品的支付宝还款不算 select @tmpamt = 0 select @tmpamt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '1103Z1' or code = '1103Z2' -- 产品部分的支付宝支付 以及 支付宝还款 -- 服务产品 - 产品 = 单纯服务项目的收入 update a set a.zfb_srv = isnull(@amt, 0) - isnull(@tmpamt, 0) from #stat_byday a where day_date = @str_date -- 微信(服务) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '1102W1' or code = '1102W2' --项目或物品的微信还款不算 select @tmpamt = 0 select @tmpamt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '1103W1' or code = '1103W2' -- 产品部分的微信支付 以及 微信还款 -- 服务产品 - 产品 = 单纯服务项目的收入 update a set a.wx_srv = isnull(@amt, 0) - isnull(@tmpamt, 0) from #stat_byday a where day_date = @str_date -- 支出 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '12' select @tmpamt = 0 select @tmpamt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '1299' update a set a.outcome = isnull(@amt, 0) - isnull(@tmpamt, 0) from #stat_byday a where day_date = @str_date -- 欠款 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110103' or code = '110106' or code = '110207' or code = '110145' update a set a.defict = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 还款 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (kind = 6 or (code='110230' or code='110231' or code = '1102Z2' or code = '1102W2')) and is_leaf = 1 --modify by lm 2015-1016 包括所有支付方式 update a set a.returnamt = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 卡付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110205' -- 这里面已经不包括了积分 11020507 select @tmpamt = 0 select @tmpamt = sum(isnull(amt, 0)) from #tbl_profit_day where code like '%1103020%'--产品卡付部分,需要扣除,否则重复统计 update a set a.pay_card_srv = isnull(@amt, 0) - isnull(@tmpamt, 0) from #stat_byday a where day_date = @str_date -- 卡付实际营业额 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110206' update a set a.pay_card_srv_act = isnull(@amt, 0) from #stat_byday a where day_date = @str_date select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '11AA01' update a set a.pay_card_srv_work = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 抵用券付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110204' or code = '110125' or code = '110126' or code = '110127') and payment in('3') update a set a.pay_coupon = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 抵用券付(只有项目, 包括产品部分) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110204') and payment in('3') update a set a.pay_coupon_srv = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 抵用券付(只包括产品部分) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110303') and payment in('3') update a set a.pay_coupon_good = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 抵用券二 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110208' or code = '110130' or code = '110131' or code = '110132' or code = '110231$') and payment in ('$') -- 因为服务中已经包括了产品的销售, 所以不能再包括110304(产品的$支付) update a set a.pay_coupon2 = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 抵用券二(只有项目) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110208') and payment in ('$') update a set a.pay_coupon2_srv = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 抵用券, 只有产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110304') and payment in ('$') update a set a.pay_coupon2_good = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 内调 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where ((code = '110209' or code = '110120' or code = '110121' or code = '110122' or code = '1101400') or ((kind = 6 or kind = -1) and payment = '0' )) update a set a.pay_coupon3 = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 内调(只包括项目 + 产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110209') update a set a.pay_coupon3_srv = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 内调(只包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110308') update a set a.pay_coupon3_good = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 经理签单, 包括卡销售的经理签单 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110110' or code = '110111' or code = '110203') update a set a.pay_manager = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 经理签单,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110203') update a set a.pay_manager_prj = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 经理签单,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110305') update a set a.pay_manager_good = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 自定义支付, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$1' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8 or kind = -1) update a set a.pay_user01 = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$1' and (kind = 2) update a set a.pay_user01_srv = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$1' and (kind = 9) update a set a.pay_user01_good = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 自定义支付, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$2' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8 or kind = -1) update a set a.pay_user02 = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$2' and (kind = 2) update a set a.pay_user02_srv = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$2' and (kind = 9) update a set a.pay_user02_good = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 自定义支付, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$3' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8 or kind = -1) update a set a.pay_user03 = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$3' and (kind = 2) update a set a.pay_user03_srv = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$3' and (kind = 9) update a set a.pay_user03_good = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 自定义支付, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$4' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8 or kind = -1) update a set a.pay_user04 = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$4' and (kind = 2) update a set a.pay_user04_srv = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$4' and (kind = 9) update a set a.pay_user04_good = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 自定义支付, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$5' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8 or kind = -1) update a set a.pay_user05 = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$5' and (kind = 2) update a set a.pay_user05_srv = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$5' and (kind = 9) update a set a.pay_user05_good = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -------------新增默认支付$6----------------- --自定义支付$6, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment ='$6' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8 or kind = -1) update a set a.pay_user06 = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$6' and (kind = 2) update a set a.pay_user06_srv = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$6' and (kind = 9) update a set a.pay_user06_good = isnull(@amt, 0) from #stat_byday a where day_date = @str_date ----------------------------------------------- -------------新增默认支付$7----------------- --自定义支付$7, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment ='$7' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8 or kind = -1) update a set a.pay_user07 = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$7' and (kind = 2) update a set a.pay_user07_srv = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$7' and (kind = 9) update a set a.pay_user07_good = isnull(@amt, 0) from #stat_byday a where day_date = @str_date ----------------------------------------------- -------------新增默认支付$8----------------- --自定义支付$8, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment ='$8' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8 or kind = -1) update a set a.pay_user08 = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$8' and (kind = 2) update a set a.pay_user08_srv = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$8' and (kind = 9) update a set a.pay_user08_good = isnull(@amt, 0) from #stat_byday a where day_date = @str_date ----------------------------------------------- -------------新增默认支付$9----------------- --自定义支付$9, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment ='$9' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8 or kind = -1) update a set a.pay_user09 = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$9' and (kind = 2) update a set a.pay_user09_srv = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$9' and (kind = 9) update a set a.pay_user09_good = isnull(@amt, 0) from #stat_byday a where day_date = @str_date ----------------------------------------------- -------------新增默认支付$10----------------- --自定义支付$10, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment ='$10' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8 or kind = -1) update a set a.pay_user10 = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$10' and (kind = 2) update a set a.pay_user10_srv = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$10' and (kind = 9) update a set a.pay_user10_good = isnull(@amt, 0) from #stat_byday a where day_date = @str_date ----------------------------------------------- -------------新增默认支付$11----------------- --自定义支付$11, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment ='$11' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8 or kind = -1) update a set a.pay_user11 = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$11' and (kind = 2) update a set a.pay_user11_srv = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$11' and (kind = 9) update a set a.pay_user11_good = isnull(@amt, 0) from #stat_byday a where day_date = @str_date ----------------------------------------------- -------------新增默认支付$12----------------- --自定义支付$12, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment ='$12' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8 or kind = -1) update a set a.pay_user12 = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$12' and (kind = 2) update a set a.pay_user12_srv = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$12' and (kind = 9) update a set a.pay_user12_good = isnull(@amt, 0) from #stat_byday a where day_date = @str_date ----------------------------------------------- -------------新增默认支付$13----------------- --自定义支付$13, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment ='$13' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8 or kind = -1) update a set a.pay_user13 = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$13' and (kind = 2) update a set a.pay_user13_srv = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$13' and (kind = 9) update a set a.pay_user13_good = isnull(@amt, 0) from #stat_byday a where day_date = @str_date ----------------------------------------------- -------------新增默认支付$14----------------- --自定义支付$14, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment ='$14' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8 or kind = -1) update a set a.pay_user14 = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$14' and (kind = 2) update a set a.pay_user14_srv = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$14' and (kind = 9) update a set a.pay_user14_good = isnull(@amt, 0) from #stat_byday a where day_date = @str_date ----------------------------------------------- -------------新增默认支付$15----------------- --自定义支付$15, 包括卡销售的自定义支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment ='$15' and (kind = 0 or kind = 1 or kind = 2 or kind = 6 or kind = 8 or kind = -1) update a set a.pay_user15 = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --自定义支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$15' and (kind = 2) update a set a.pay_user15_srv = isnull(@amt, 0) from #stat_byday a where day_date = @str_date --自定义支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where is_leaf = 1 and payment = '$15' and (kind = 9) update a set a.pay_user15_good = isnull(@amt, 0) from #stat_byday a where day_date = @str_date ----------------------------------------------- -- 其他收入 Huzy 2012/05/31 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '18') update a set a.other_income = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 虚拟收入virtual_income select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = 'n1') update a set a.virtual_income = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 虚拟支出virtual_outcome select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = 'n2') update a set a.virtual_outcome = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 积分支付, 包括卡销售的积分支付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110133' or code = '11020507') update a set a.pay_point = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 积分支付,只包括项目(包括产品) select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '11020507') update a set a.pay_point_srv = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 积分支付,只包括产品 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where (code = '110309') update a set a.pay_point_good = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 物品购买的现金 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110301' or code = '110310' -- 包括产品现金的还款 update a set a.cash_good = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 物品购买的银行卡 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110306' or code = '110311' --or code = '1103Z2' or code = '1103W2' -- 包括产品银行卡的还款 update a set a.credit_good = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 物品购买的支付宝 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '1103Z1' or code = '1103Z2' -- 包括产品支付宝的还款 update a set a.zfb_good = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 物品购买的微信 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '1103W1' or code = '1103W2' -- 包括产品微信的还款 update a set a.wx_good = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 物品购买的卡付 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code like '%1103020%' update a set a.pay_card_good = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 取款的总额 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '110119' or code = '110123' or code = 'drawother' update a set a.draw_total = isnull(@amt, 0) from #stat_byday a where day_date = @str_date -- 收入都同利润分析表中的收入 select @amt = 0 select @amt = sum(isnull(amt, 0)) from #tbl_profit_day where code = '11' -- 收入 update #stat_byday set revene = @amt where day_date = @str_date --总现金流 modify by lm 2015.12.09 set @cash_sum = 0 select @cash_sum = sum(amt) from #tbl_profit_day where is_leaf = 1 and payment in (select pay from pay_classify where category = '1' and comp = @compid ) and kind <> 9 update a set a.cash_sum = isnull(@cash_sum, 0) from #stat_byday a where day_date = @str_date if (@flag = 0) select @cur_date = dateadd(day, 1, @cur_date) else if (@flag = 1) select @cur_date = dateadd(day, 1, convert(datetime, @todate)) end -- while 循环结束 update #stat_byday set cash_total = isnull(cash_card, 0) + isnull(cash_srv, 0) + isnull(cash_good, 0), cash_sum = cash_sum, credit_total = isnull(credit_card, 0) + isnull(credit_srv, 0) + isnull(credit_good, 0), zfb_total = isnull(zfb_card, 0) + isnull(zfb_srv, 0) + isnull(zfb_good, 0), wx_total = isnull(wx_card, 0) + isnull(wx_srv, 0) + isnull(wx_good, 0) -- 定义劳动业绩的比率 declare @cash_rate float -- 现金:1 declare @cash_good_rate float -- 现金:1 declare @credit_rate float -- 银行卡支付:6 declare @credit_good_rate float -- 银行卡支付:6 declare @pay3_rate float -- 支付方式:3 declare @pay3_good_rate float -- 支付方式:3 declare @pay$_rate float -- 支付方式:$ declare @pay$_good_rate float -- 支付方式:$ declare @pay0_rate float -- 内调:0 declare @pay0_good_rate float -- 内调:0 declare @manager_rate float -- 经理签单:8 declare @manager_good_rate float -- 经理签单:8 declare @point_rate float -- 积分支付:7 declare @point_good_rate float -- 积分支付:7 declare @user01_rate float -- 自定义支付:$1 declare @user01_good_rate float -- 自定义支付:$1 declare @user02_rate float -- 自定义支付:$2 declare @user02_good_rate float -- 自定义支付:$2 declare @user03_rate float -- 自定义支付:$3 declare @user03_good_rate float -- 自定义支付:$3 declare @user04_rate float -- 自定义支付:$4 declare @user04_good_rate float -- 自定义支付:$4 declare @user05_rate float -- 自定义支付:$5 declare @user05_good_rate float -- 自定义支付:$5 declare @zfb_rate float -- 支付宝支付 declare @zfb_good_rate float -- 支付宝支付 declare @wx_rate float -- 微信支付 declare @wx_good_rate float -- 微信支付 declare @user06_rate float declare @user06_good_rate float declare @user07_rate float declare @user07_good_rate float declare @user08_rate float declare @user08_good_rate float declare @user09_rate float declare @user09_good_rate float declare @user10_rate float declare @user10_good_rate float declare @user11_rate float declare @user11_good_rate float declare @user12_rate float declare @user12_good_rate float declare @user13_rate float declare @user13_good_rate float declare @user14_rate float declare @user14_good_rate float declare @user15_rate float declare @user15_good_rate float exec upg_get_workperf_rate @compid, @cash_rate output, @cash_good_rate output, @credit_rate output, @credit_good_rate output, @pay3_rate output, @pay3_good_rate output, @pay$_rate output, @pay$_good_rate output, @pay0_rate output, @pay0_good_rate output, @manager_rate output, @manager_good_rate output, @point_rate output, @point_good_rate output, @user01_rate output, @user01_good_rate output, @user02_rate output, @user02_good_rate output, @user03_rate output, @user03_good_rate output, @user04_rate output, @user04_good_rate output, @user05_rate output, @user05_good_rate output, @zfb_rate output, @zfb_good_rate output, @wx_rate output, @wx_good_rate output --查询支付方式$6设置的比率 如果不需要计算劳动业绩 返回0 select @user06_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso09f, 0) * isnull(gso03i,1) end, @user06_good_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso10f, 0) * isnull(gso06f,1) end from gsm15 with (nolock) where gso00c = @compid and gso02c = '$6' --查询支付方式$7设置的比率 如果不需要计算劳动业绩 返回0 select @user07_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso09f, 0) * isnull(gso03i,1) end, @user07_good_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso10f, 0) * isnull(gso06f,1) end from gsm15 with (nolock) where gso00c = @compid and gso02c = '$7' --查询支付方式$8设置的比率 如果不需要计算劳动业绩 返回0 select @user08_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso09f, 0) * isnull(gso03i,1) end, @user08_good_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso10f, 0) * isnull(gso06f,1) end from gsm15 with (nolock) where gso00c = @compid and gso02c = '$8' --查询支付方式$9设置的比率 如果不需要计算劳动业绩 返回0 select @user09_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso09f, 0) * isnull(gso03i,1) end, @user09_good_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso10f, 0) * isnull(gso06f,1) end from gsm15 with (nolock) where gso00c = @compid and gso02c = '$9' --查询支付方式$10设置的比率 如果不需要计算劳动业绩 返回0 select @user10_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso09f, 0) * isnull(gso03i,1) end, @user10_good_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso10f, 0) * isnull(gso06f,1) end from gsm15 with (nolock) where gso00c = @compid and gso02c = '$10' --查询支付方式$11设置的比率 如果不需要计算劳动业绩 返回0 select @user11_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso09f, 0) * isnull(gso03i,1) end, @user11_good_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso10f, 0) * isnull(gso06f,1) end from gsm15 with (nolock) where gso00c = @compid and gso02c = '$11' --查询支付方式$12设置的比率 如果不需要计算劳动业绩 返回0 select @user12_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso09f, 0) * isnull(gso03i,1) end, @user12_good_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso10f, 0) * isnull(gso06f,1) end from gsm15 with (nolock) where gso00c = @compid and gso02c = '$12' --查询支付方式$13设置的比率 如果不需要计算劳动业绩 返回0 select @user13_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso09f, 0) * isnull(gso03i,1) end, @user13_good_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso10f, 0) * isnull(gso06f,1) end from gsm15 with (nolock) where gso00c = @compid and gso02c = '$13' --查询支付方式$14设置的比率 如果不需要计算劳动业绩 返回0 select @user14_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso09f, 0) * isnull(gso03i,1) end, @user14_good_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso10f, 0) * isnull(gso06f,1) end from gsm15 with (nolock) where gso00c = @compid and gso02c = '$14' --查询支付方式$15设置的比率 如果不需要计算劳动业绩 返回0 select @user15_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso09f, 0) * isnull(gso03i,1) end, @user15_good_rate = case when isnull(gso08f,0) = 0 then 0 else isnull(gso10f, 0) * isnull(gso06f,1) end from gsm15 with (nolock) where gso00c = @compid and gso02c = '$15' -- 增加劳动业绩的计算 declare @sp056 varchar(20) select @sp056 = gsa02c from gsm01 with (nolock) where gsa00c = @compid and gsa01c = 'SP056' if @sp056 = '4' -- 足疗 begin -- 足疗的劳动业绩 -- 劳动业绩 = 现金(服务) + 现金(物品) + 卡付(物品) + 卡付 + 抵用券 + 银行卡(服务) + 银行卡(物品) + 积分支付(项目 + 产品) update #stat_byday set work_perf = isnull(cash_srv, 0) * @cash_rate + isnull(cash_good, 0) * @cash_good_rate + isnull(pay_card_srv, 0) + isnull(pay_card_good, 0) + isnull(credit_srv, 0) * @credit_rate + isnull(credit_good, 0) * @credit_good_rate + isnull(zfb_srv, 0) * @zfb_rate + isnull(zfb_good, 0) * @zfb_good_rate + isnull(wx_srv, 0) * @wx_rate + isnull(wx_good, 0) * @wx_good_rate + (isnull(pay_coupon_srv, 0) - isnull(pay_coupon_good, 0)) * @pay3_rate + isnull(pay_coupon_good, 0) * @pay3_good_rate + (isnull(pay_point_srv, 0) - isnull(pay_point_good, 0)) * @point_rate + isnull(pay_point_good, 0) * @point_good_rate + (isnull(pay_user01_srv, 0) - isnull(pay_user01_good, 0)) * @user01_rate + isnull(pay_user01_good, 0) * @user01_good_rate + (isnull(pay_user02_srv, 0) - isnull(pay_user02_good, 0)) * @user02_rate + isnull(pay_user02_good, 0) * @user02_good_rate + (isnull(pay_user03_srv, 0) - isnull(pay_user03_good, 0)) * @user03_rate + isnull(pay_user03_good, 0) * @user03_good_rate + (isnull(pay_user04_srv, 0) - isnull(pay_user04_good, 0)) * @user04_rate + isnull(pay_user04_good, 0) * @user04_good_rate + (isnull(pay_user05_srv, 0) - isnull(pay_user05_good, 0)) * @user05_rate + isnull(pay_user05_good, 0) * @user05_good_rate + (isnull(pay_user06_srv, 0) - isnull(pay_user06_good, 0)) * isnull(@user06_rate,0) + isnull(pay_user06_good, 0) * isnull(@user06_good_rate,0) + (isnull(pay_user07_srv, 0) - isnull(pay_user07_good, 0)) * isnull(@user07_rate,0) + isnull(pay_user07_good, 0) * isnull(@user07_good_rate,0) + (isnull(pay_user08_srv, 0) - isnull(pay_user08_good, 0)) * isnull(@user08_rate,0) + isnull(pay_user08_good, 0) * isnull(@user08_good_rate,0) + (isnull(pay_user09_srv, 0) - isnull(pay_user09_good, 0)) * isnull(@user09_rate,0) + isnull(pay_user09_good, 0) * isnull(@user09_good_rate,0) + (isnull(pay_user10_srv, 0) - isnull(pay_user10_good, 0)) * isnull(@user10_rate,0) + isnull(pay_user10_good, 0) * isnull(@user10_good_rate,0) + (isnull(pay_user11_srv, 0) - isnull(pay_user11_good, 0)) * isnull(@user11_rate,0) + isnull(pay_user11_good, 0) * isnull(@user11_good_rate,0) + (isnull(pay_user12_srv, 0) - isnull(pay_user12_good, 0)) * isnull(@user12_rate,0) + isnull(pay_user12_good, 0) * isnull(@user12_good_rate,0) + (isnull(pay_user13_srv, 0) - isnull(pay_user13_good, 0)) * isnull(@user13_rate,0) + isnull(pay_user13_good, 0) * isnull(@user13_good_rate,0) + (isnull(pay_user14_srv, 0) - isnull(pay_user14_good, 0)) * isnull(@user14_rate,0) + isnull(pay_user14_good, 0) * isnull(@user14_good_rate,0) + (isnull(pay_user15_srv, 0) - isnull(pay_user15_good, 0)) * isnull(@user15_rate,0) + isnull(pay_user15_good, 0) * isnull(@user15_good_rate,0) end else begin -- 如果是按项目支付的情况下,采用卡付实际营业额,如单据支付则直接用卡付金额 -- 美发劳动业绩 declare @sp055f float select @sp055f = gsa03f from gsm01 with (nolock) where gsa00c = @incompid and gsa01c = 'SP055' if @sp055f = 1 begin -- 按单据支付 update #stat_byday set work_perf = isnull(cash_srv, 0) * @cash_rate + isnull(cash_good, 0) * @cash_good_rate + isnull(pay_card_srv, 0) + isnull(pay_card_good, 0) + isnull(credit_srv, 0) * @credit_rate + isnull(credit_good, 0) * @credit_good_rate + isnull(zfb_srv, 0) * @zfb_rate + isnull(zfb_good, 0) * @zfb_good_rate + isnull(wx_srv, 0) * @wx_rate + isnull(wx_good, 0) * @wx_good_rate + (isnull(pay_coupon_srv, 0) - isnull(pay_coupon_good, 0)) * @pay3_rate + isnull(pay_coupon_good, 0) * @pay3_good_rate + (isnull(pay_coupon2_srv, 0) - isnull(pay_coupon2_good, 0)) * @pay$_rate + isnull(pay_coupon2_good, 0) * @pay$_good_rate + (isnull(pay_coupon3_srv, 0) - isnull(pay_coupon3_good, 0)) * @pay0_rate + isnull(pay_coupon3_good, 0) * @pay0_good_rate + (isnull(pay_manager_prj, 0) - isnull(pay_manager_good, 0)) * @manager_rate + isnull(pay_manager_good, 0) * @manager_good_rate + (isnull(pay_point_srv, 0) - isnull(pay_point_good, 0)) * @point_rate + isnull(pay_point_good, 0) * @point_good_rate + (isnull(pay_user01_srv, 0) - isnull(pay_user01_good, 0)) * @user01_rate + isnull(pay_user01_good, 0) * @user01_good_rate + (isnull(pay_user02_srv, 0) - isnull(pay_user02_good, 0)) * @user02_rate + isnull(pay_user02_good, 0) * @user02_good_rate + (isnull(pay_user03_srv, 0) - isnull(pay_user03_good, 0)) * @user03_rate + isnull(pay_user03_good, 0) * @user03_good_rate + (isnull(pay_user04_srv, 0) - isnull(pay_user04_good, 0)) * @user04_rate + isnull(pay_user04_good, 0) * @user04_good_rate + (isnull(pay_user05_srv, 0) - isnull(pay_user05_good, 0)) * @user05_rate + isnull(pay_user05_good, 0) * @user05_good_rate + (isnull(pay_user06_srv, 0) - isnull(pay_user06_good, 0)) * isnull(@user06_rate,0) + isnull(pay_user06_good, 0) * isnull(@user06_good_rate,0) + (isnull(pay_user07_srv, 0) - isnull(pay_user07_good, 0)) * isnull(@user07_rate,0) + isnull(pay_user07_good, 0) * isnull(@user07_good_rate ,0) + (isnull(pay_user08_srv, 0) - isnull(pay_user08_good, 0)) * isnull(@user08_rate,0) + isnull(pay_user08_good, 0) * isnull(@user08_good_rate ,0) + (isnull(pay_user09_srv, 0) - isnull(pay_user09_good, 0)) * isnull(@user09_rate,0) + isnull(pay_user09_good, 0) * isnull(@user09_good_rate ,0) + (isnull(pay_user10_srv, 0) - isnull(pay_user10_good, 0)) * isnull(@user10_rate,0) + isnull(pay_user10_good, 0) * isnull(@user10_good_rate ,0) + (isnull(pay_user11_srv, 0) - isnull(pay_user11_good, 0)) * isnull(@user11_rate,0) + isnull(pay_user11_good, 0) * isnull(@user11_good_rate ,0) + (isnull(pay_user12_srv, 0) - isnull(pay_user12_good, 0)) * isnull(@user12_rate,0) + isnull(pay_user12_good, 0) * isnull(@user12_good_rate ,0) + (isnull(pay_user13_srv, 0) - isnull(pay_user13_good, 0)) * isnull(@user13_rate,0) + isnull(pay_user13_good, 0) * isnull(@user13_good_rate ,0) + (isnull(pay_user14_srv, 0) - isnull(pay_user14_good, 0)) * isnull(@user14_rate,0) + isnull(pay_user14_good, 0) * isnull(@user14_good_rate ,0) + (isnull(pay_user15_srv, 0) - isnull(pay_user15_good, 0)) * isnull(@user15_rate,0) + isnull(pay_user15_good, 0) * isnull(@user15_good_rate ,0) end else begin -- 劳动业绩 = 现金(服务) + 现金(物品) + 卡付(物品) + 卡付实营业额 + 经理签单 + 内调 + 抵用券 + 银行卡(服务) + 银行卡(物品) + 积分支付(项目 + 产品) update #stat_byday set work_perf = isnull(cash_srv, 0) * @cash_rate + isnull(cash_good, 0) * @cash_good_rate + isnull(pay_card_srv_work, 0) + isnull(credit_srv, 0) * @credit_rate + isnull(credit_good, 0) * @credit_good_rate + isnull(zfb_srv, 0) * @zfb_rate + isnull(zfb_good, 0) * @zfb_good_rate + isnull(wx_srv, 0) * @wx_rate + isnull(wx_good, 0) * @wx_good_rate + (isnull(pay_coupon_srv, 0) - isnull(pay_coupon_good, 0)) * @pay3_rate + isnull(pay_coupon_good, 0) * @pay3_good_rate + (isnull(pay_coupon2_srv, 0) - isnull(pay_coupon2_good, 0)) * @pay$_rate + isnull(pay_coupon2_good, 0) * @pay$_good_rate + (isnull(pay_coupon3_srv, 0) - isnull(pay_coupon3_good, 0)) * @pay0_rate + isnull(pay_coupon3_good, 0) * @pay0_good_rate + (isnull(pay_manager_prj, 0) - isnull(pay_manager_good, 0)) * @manager_rate + isnull(pay_manager_good, 0) * @manager_good_rate + (isnull(pay_point_srv, 0) - isnull(pay_point_good, 0)) * @point_rate + isnull(pay_point_good, 0) * @point_good_rate + (isnull(pay_user01_srv, 0) - isnull(pay_user01_good, 0)) * @user01_rate + isnull(pay_user01_good, 0) * @user01_good_rate + (isnull(pay_user02_srv, 0) - isnull(pay_user02_good, 0)) * @user02_rate + isnull(pay_user02_good, 0) * @user02_good_rate + (isnull(pay_user03_srv, 0) - isnull(pay_user03_good, 0)) * @user03_rate + isnull(pay_user03_good, 0) * @user03_good_rate + (isnull(pay_user04_srv, 0) - isnull(pay_user04_good, 0)) * @user04_rate + isnull(pay_user04_good, 0) * @user04_good_rate + (isnull(pay_user05_srv, 0) - isnull(pay_user05_good, 0)) * @user05_rate + isnull(pay_user05_good, 0) * @user05_good_rate + (isnull(pay_user06_srv, 0) - isnull(pay_user06_good, 0)) * isnull(@user06_rate,0) + isnull(pay_user06_good, 0) * isnull(@user06_good_rate,0) + (isnull(pay_user07_srv, 0) - isnull(pay_user07_good, 0)) * isnull(@user07_rate,0) + isnull(pay_user07_good, 0) * isnull(@user07_good_rate,0) + (isnull(pay_user08_srv, 0) - isnull(pay_user08_good, 0)) * isnull(@user08_rate ,0) + isnull(pay_user08_good, 0) *isnull( @user08_good_rate,0) + (isnull(pay_user09_srv, 0) - isnull(pay_user09_good, 0)) * isnull(@user09_rate ,0) + isnull(pay_user09_good, 0) *isnull( @user09_good_rate,0) + (isnull(pay_user10_srv, 0) - isnull(pay_user10_good, 0)) * isnull(@user10_rate ,0) + isnull(pay_user10_good, 0) *isnull( @user10_good_rate,0) + (isnull(pay_user11_srv, 0) - isnull(pay_user11_good, 0)) * isnull(@user11_rate ,0) + isnull(pay_user11_good, 0) *isnull( @user11_good_rate,0) + (isnull(pay_user12_srv, 0) - isnull(pay_user12_good, 0)) * isnull(@user12_rate ,0) + isnull(pay_user12_good, 0) *isnull( @user12_good_rate,0) + (isnull(pay_user13_srv, 0) - isnull(pay_user13_good, 0)) * isnull(@user13_rate ,0) + isnull(pay_user13_good, 0) *isnull( @user13_good_rate,0) + (isnull(pay_user14_srv, 0) - isnull(pay_user14_good, 0)) * isnull(@user14_rate ,0) + isnull(pay_user14_good, 0) *isnull( @user14_good_rate,0) + (isnull(pay_user15_srv, 0) - isnull(pay_user15_good, 0)) * isnull(@user15_rate ,0) + isnull(pay_user15_good, 0) *isnull( @user15_good_rate,0) end end update #stat_byday set profit = isnull(revene, 0) + isnull(other_income, 0) - isnull(outcome, 0) --Start增加疗程销售,消耗 add by lm 2015.10.21 if @staff = '*' or @staff = '' begin select @staff = '%' end create table #prj_stock_buf ( gch02d varchar(8) null, -- 日期 prjnum float null, -- 疗程的数量 prjamt float null, -- 疗程金额 ) create table #prj_stock_buf1 ( gch02d varchar(8) null, -- 日期 prjnum float null, -- 疗程的数量 prjamt float null, -- 疗程金额 ) create table #prj_consume ( gch02d varchar(8) null, -- 日期 prjnum float null, -- 疗程的数量 prjamt float null, -- 疗程金额 ) create table #prj_consume1 ( gch02d varchar(8) null, -- 日期 prjnum float null, -- 疗程的数量 prjamt float null, -- 疗程金额 ) create table #gcm10 ( compid varchar(20) not null, billid varchar(20) not null,--充值单号 primary key(compid,billid) ) create table #gnm01 ( compid varchar(20) not null, billid varchar(20) not null,--充值单号 primary key(compid,billid) ) create table #ggm01 ( compid varchar(20) not null, billid varchar(20) not null,--充值单号 primary key(compid,billid) ) insert #gcm10(compid, billid) select distinct gcl00c, gcl01c from gcm10 a with (nolock), gam26 z with (nolock) where a.gcl00c = z.gaz02c and z.gaz01c = @compid and (a.gcl80d >= @fromdate and a.gcl80d <= @todate) and a.gcl93c like @staff insert #gnm01(compid, billid) select distinct gna00c, gna01c from gnm01 a with (nolock), gam26 z with (nolock) where a.gna00c = z.gaz02c and z.gaz01c = @compid and (a.gna80d >= @fromdate and a.gna80d <= @todate) and a.gna93c like @staff insert #ggm01(compid, billid) select distinct gga00c, gga01c from ggm01 a with (nolock), ggm02 b, gam26 z with (nolock) where a.gga00c = b.ggb00c and a.gga01c = b.ggb01c and a.gga00c = z.gaz02c and z.gaz01c = @compid and (a.gga80d >= @fromdate and a.gga80d <= @todate) and a.gga93c like @staff if(@flag = 0) --一天一条记录 begin -- 这个时间点范围内销售的疗程 insert #prj_stock_buf1(gch02d, prjnum, prjamt) select gch02d, sum(gch05f + gch06f), sum(gch07f) from #gnm01 a, gcm08 b with (nolock) where a.compid = b.gch00c and a.billid = b.gch11c and b.gch10c = 'gn' and ((b.gch02d >= @fromdate and b.gch02d <= @todate) or @fromdate = '*') group by gch02d order by gch02d insert #prj_stock_buf1(gch02d, prjnum, prjamt) select gch02d, sum(gch05f + gch06f), sum(gch07f) from #gcm10 a, gcm08 b with (nolock) where a.compid = b.gch00c and a.billid = b.gch11c and b.gch10c = 'gz' and ((b.gch02d >= @fromdate and b.gch02d <= @todate) or @fromdate = '*') group by gch02d order by gch02d --合计 insert #prj_stock_buf (gch02d, prjnum, prjamt) select gch02d, sum(prjnum), sum(prjamt) from #prj_stock_buf1 group by gch02d -- -- -- 这个时间范围内消耗的疗程 -- -- insert #prj_consume1 (gch02d, prjnum, prjamt) -- select gch02d, sum(gch08f), sum(gch09f) -- from #gcm10 a, gcm08 b with (nolock), gam26 z with (nolock) -- where a.compid = b.gch00c -- and a.billid = b.gch11c -- and a.compid = z.gaz02c -- and z.gaz01c = @compid -- and b.gch10c = 'pj' -- and ((b.gch02d >= @fromdate and b.gch02d <= @todate) or @fromdate = '*') -- group by gch02d -- order by gch02d insert #prj_consume1 (gch02d, prjnum, prjamt) select gch02d, sum(gch08f), sum(gch09f) from #ggm01 a, gcm08 b with (nolock) where a.compid = b.gch00c and a.billid = b.gch11c and b.gch10c = 'gx' and ((b.gch02d >= @fromdate and b.gch02d <= @todate) or @fromdate = '*') group by gch02d order by gch02d --合计 insert #prj_consume (gch02d, prjnum, prjamt) select gch02d, sum(prjnum), sum(prjamt) from #prj_consume1 group by gch02d end else begin -- 这个时间点范围内销售的疗程 insert #prj_stock_buf1(gch02d, prjnum, prjamt) select @str_date, sum(gch05f + gch06f), sum(gch07f) from #gnm01 a, gcm08 b with (nolock) where a.compid = b.gch00c and a.billid = b.gch11c and b.gch10c = 'gn' and ((b.gch02d >= @fromdate and b.gch02d <= @todate) or @fromdate = '*') insert #prj_stock_buf1(gch02d, prjnum, prjamt) select @str_date, sum(gch05f + gch06f), sum(gch07f) from #gcm10 a, gcm08 b with (nolock) where a.compid = b.gch00c and a.billid = b.gch11c and b.gch10c = 'gz' and ((b.gch02d >= @fromdate and b.gch02d <= @todate) or @fromdate = '*') --合计 insert #prj_stock_buf (gch02d, prjnum, prjamt) select @str_date, sum(prjnum), sum(prjamt) from #prj_stock_buf1 -- 这个时间范围内消耗的疗程 insert #prj_consume1 (gch02d, prjnum, prjamt) select @str_date, sum(gch08f), sum(gch09f) from #gcm10 a, gcm08 b with (nolock) where a.compid = b.gch00c and a.billid = b.gch11c and b.gch10c = 'pj' and ((b.gch02d >= @fromdate and b.gch02d <= @todate) or @fromdate = '*') insert #prj_consume1 (gch02d, prjnum, prjamt) select @str_date, sum(gch08f), sum(gch09f) from #ggm01 a, gcm08 b with (nolock) where a.compid = b.gch00c and a.billid = b.gch11c and b.gch10c = 'gx' and ((b.gch02d >= @fromdate and b.gch02d <= @todate) or @fromdate = '*') --合计 insert #prj_consume (gch02d, prjnum, prjamt) select @str_date, sum(prjnum), sum(prjamt) from #prj_consume1 end --疗程销售,消耗金额,数量赋值 update a set a.prj_quan_buy = isnull(b.prjnum,0), a.prj_amt_buy = isnull(b.prjamt,0) from #stat_byday a, #prj_stock_buf b where a.day_date = b.gch02d update a set a.prj_quan_consume = isnull(b.prjnum,0), a.prj_amt_consume = isnull(b.prjamt,0) from #stat_byday a, #prj_consume b where a.day_date = b.gch02d --End select isnull(day_date,'') day_date, isnull(cash_card,0) cash_card, isnull(cash_srv,0) cash_srv, isnull(cash_good,0) cash_good, isnull(cash_total,0) cash_total, isnull(credit_card,0) credit_card, isnull(credit_srv,0) credit_srv, isnull(credit_good,0) credit_good, isnull(credit_total,0) credit_total, isnull(zfb_card,0) zfb_card, isnull(zfb_srv,0) zfb_srv, isnull(zfb_good,0) zfb_good, isnull(zfb_total,0) zfb_total, isnull(wx_card,0) wx_card, isnull(wx_srv,0) wx_srv, isnull(wx_good,0) wx_good, isnull(wx_total,0) wx_total, isnull(revene,0) revene, isnull(outcome,0) outcome, isnull(profit,0) profit, isnull(defict,0) defict, isnull(returnamt,0) returnamt, isnull(pay_card_srv,0) pay_card_srv, isnull(pay_card_good,0) pay_card_good, isnull(pay_coupon,0) pay_coupon, isnull(pay_coupon2,0) pay_coupon2, isnull(pay_coupon3,0) pay_coupon3, isnull(pay_manager,0) pay_manager, isnull(sale_card_total,0) sale_card_total, isnull(pay_card_srv_act,0) pay_card_srv_act, isnull(pay_card_srv_work,0) pay_card_srv_work, isnull(pay_coupon_srv,0) pay_coupon_srv, isnull(pay_coupon2_srv,0) pay_coupon2_srv, isnull(pay_coupon3_srv,0) pay_coupon3_srv, isnull(draw_total,0) draw_total, isnull(work_perf,0) work_perf, isnull(cust_quan,0) cust_quan, isnull(proj_quan,0) proj_quan, isnull(pay_manager_prj,0) pay_manager_prj, isnull(pay_manager_good,0) pay_manager_good, isnull(pay_coupon_good,0) pay_coupon_good, isnull(pay_coupon2_good,0) pay_coupon2_good, isnull(pay_coupon3_good,0) pay_coupon3_good, isnull(pay_point,0) pay_point, isnull(pay_point_srv,0) pay_point_srv, isnull(pay_point_good,0) pay_point_good, isnull(pay_user01,0) pay_user01, isnull(pay_user01_srv,0) pay_user01_srv, isnull(pay_user01_good,0) pay_user01_good, isnull(pay_user02,0) pay_user02, isnull(pay_user02_srv,0) pay_user02_srv, isnull(pay_user02_good,0) pay_user02_good, isnull(pay_user03,0) pay_user03, isnull(pay_user03_srv,0) pay_user03_srv, isnull(pay_user03_good,0) pay_user03_good, isnull(pay_user04,0) pay_user04, isnull(pay_user04_srv,0) pay_user04_srv, isnull(pay_user04_good,0) pay_user04_good, isnull(pay_user05,0) pay_user05, isnull(pay_user05_srv,0) pay_user05_srv, isnull(pay_user05_good,0) pay_user05_good, isnull(pay_user06,0) pay_user06, isnull(pay_user06_srv,0) pay_user06_srv, isnull(pay_user06_good,0) pay_user06_good, isnull(pay_user07,0) pay_user07, isnull(pay_user07_srv,0) pay_user07_srv, isnull(pay_user07_good,0) pay_user07_good, isnull(pay_user08,0) pay_user08, isnull(pay_user08_srv,0) pay_user08_srv, isnull(pay_user08_good,0) pay_user08_good, isnull(pay_user09,0) pay_user09, isnull(pay_user09_srv,0) pay_user09_srv, isnull(pay_user09_good,0) pay_user09_good, isnull(pay_user10,0) pay_user10, isnull(pay_user10_srv,0) pay_user10_srv, isnull(pay_user10_good,0) pay_user10_good, isnull(pay_user11,0) pay_user11, isnull(pay_user11_srv,0) pay_user11_srv, isnull(pay_user11_good,0) pay_user11_good, isnull(pay_user12,0) pay_user12, isnull(pay_user12_srv,0) pay_user12_srv, isnull(pay_user12_good,0) pay_user12_good, isnull(pay_user13,0) pay_user13, isnull(pay_user13_srv,0) pay_user13_srv, isnull(pay_user13_good,0) pay_user13_good, isnull(pay_user14,0) pay_user14, isnull(pay_user14_srv,0) pay_user14_srv, isnull(pay_user14_good,0) pay_user14_good, isnull(pay_user15,0) pay_user15, isnull(pay_user15_srv,0) pay_user15_srv, isnull(pay_user15_good,0) pay_user15_good, isnull(other_income,0) other_income, isnull(virtual_income,0) virtual_income, isnull(virtual_outcome,0) virtual_outcome, isnull(prj_quan_buy,0) prj_quan_buy, isnull(prj_amt_buy,0) prj_amt_buy, isnull(prj_quan_consume,0) prj_quan_consume, isnull(prj_amt_consume,0) prj_amt_consume, isnull(cash_sum,0) cash_sum from #stat_byday drop table #stat_byday drop table #tbl_profit_day drop table #gcm10 drop table #gnm01 drop table #ggm01 drop table #prj_stock_buf drop table #prj_stock_buf1 drop table #prj_consume drop table #prj_consume1 end GO /****** Object: StoredProcedure [dbo].[upg_purse_deduct_extends_gz] Script Date: 06/12/2017 06:20:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_purse_deduct_extends_gz] ( @compid varchar(10), -- 公司编号 @cardno varchar(20), -- 会员卡号 @date varchar(10), -- 异动日期 @accttype int, -- 账户类型(1-电子钱包 2-储值账户) @consume float, -- 支付金额 @changway int, -- 异动方向(0-充值, 1-取款, 2-消费, 3-转入, 4-转出, 5-欠款 6-欠款返回 7-赠送) @billtype varchar(5), -- 单据类别 @billno varchar(20), -- 单据号 @confirmflag int, -- 是否复核的标记(1-复核 0-取消复核) @jine float -- 套餐金额 ) as begin set nocount on declare @gcm04_flowno float declare @lastremain float declare @billmemo varchar(160) -- 单据的备注 declare @tcjine float --套餐欠款的金额 -- 当前的事务数 declare @__transaction_count int set @__transaction_count = @@trancount -- try...catch处理 begin try -- a.开启事务,或者设置事务保存点 if @__transaction_count = 0 begin tran else save tran __tran_upg_purse_deduct --支付金额大于套餐金额 if(@consume > @jine) select @tcjine = @jine else select @tcjine = @consume if @confirmflag = 1 --复核 begin if(exists(select 1 from gcm03 with (nolock) where gcc00c = @compid And gcc01c = @cardno and gcc03i = 889)) begin update gcm03 set gcc06f = gcc06f - @tcjine, gcc10f = gcc10f + @tcjine where gcc00c = @compid and gcc01c = @cardno and gcc03i = 889 end else begin declare @intRow int select @intRow = count(*) + 1 from gcm03 with (nolock) where gcc00c = @compid And gcc01c = @cardno insert into gcm03(gcc00c,gcc01c,gcc02f,gcc03i,gcc06f,gcc07f,gcc08c,gcc09d,gcc10f,gcc11c) values(@compid,@cardno,@intRow,889,-@tcjine,0,'','',@tcjine,'') end -- if(@consume > @jine) --支付金额大于套餐金额 -- begin -- if(exists(select 1 from gcm03 where gcc00c = @compid And gcc01c = @cardno and gcc03i = 2)) -- begin -- update gcm03 set gcc10f = isnull(gcc10f,0) +(@consume - @jine), -- gcc06f = isnull(gcc06f,0) -(@consume - @jine) -- where gcc00c = @compid -- and gcc01c = @cardno -- and gcc03i = 2 -- end -- else -- begin -- insert into gcm03(gcc00c,gcc01c,gcc02f,gcc03i,gcc06f,gcc07f,gcc08c,gcc09d,gcc10f,gcc11c) -- values(@compid,@cardno,@intRow,889,@consume - @jine,0,'','',@consume - @jine,'') -- -- end -- end --获取操作日期 declare @curdate varchar(20), @curtime varchar(20) exec upg_getcurrdate @curdate output, @curtime output exec upg_get_gcm04_flowno @cardno, 889, @gcm04_flowno output declare @sp0403f float declare @isinputout_raw_bill int select @sp0403f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP040' if @sp0403f = 2 or @sp0403f = 3 begin select @isinputout_raw_bill = 0 end else begin select @isinputout_raw_bill = 1 end insert gcm04(gcd00c, gcd01c, gcd02i, gcd04f, gcd05i, gcd06f, gcd07c, gcd08c, gcd09d, gcd10f, gcd11c, gcd12i, gcd13t, gcd14c) values(@compid, @cardno, 889, @gcm04_flowno, @changway, @tcjine, @billtype, @billno, @date, 0, @curdate, @isinputout_raw_bill, @curtime, @billmemo) end else --取消复核 begin update gcm03 set gcc06f = gcc06f + @tcjine, gcc10f = gcc10f - @tcjine where gcc00c = @compid and gcc01c = @cardno and gcc03i = 889 delete from gcm03 where gcc00c = @compid and gcc01c = @cardno and gcc03i = 889 and gcc06f = 0 and gcc10f = 0 -- if(@consume > @jine) --支付金额大于套餐金额 -- begin -- update gcm03 set gcc10f = gcc10f -(@consume - @jine), -- gcc06f = gcc06f +(@consume - @jine) -- where gcc00c = @compid -- and gcc01c = @cardno -- and gcc03i = 2 -- end if exists(select 1 from gcm04 with (nolock) where gcd00c = @compid and gcd01c = @cardno and gcd02i = @accttype and gcd05i = @changway and gcd07c = @billtype and gcd08c = @billno) begin delete from gcm04 where gcd00c = @compid and gcd01c = @cardno and gcd02i = @accttype and gcd05i = @changway and gcd07c = @billtype and gcd08c = @billno end end -- 提交事务 -- 有可提交的事务, 并且事务是在当前模块中开启的情况下, 才提交事务 if xact_state() = 1 and @__transaction_count = 0 commit -- 为了防止TRY 中有遗漏的事务处理, 可以在TRY 模拟的结束部分做最终的判断 if @__transaction_count = 0 begin if xact_state() = -1 rollback tran else begin while @@trancount > 0 commit tran end end end try begin catch -- 在CATCH 模块, 应该首先处理事务 if xact_state() <> 0 begin if @__transaction_count = 0 rollback tran -- xact_state 为-1 时, 不能回滚到事务保存点, 这种情况留给外层调用者做统一的事务回滚 -- 通过@@trancount > @__transaction_count 的判断, 即使在TRY 模块中没有设置事务保存点的情况下跳到此步骤, 也不会出错 else if xact_state() = 1 and @@trancount > @__transaction_count rollback tran __tran_upg_purse_deduct end return -1 end catch end GO /****** Object: StoredProcedure [dbo].[upg_day_payment_detail] Script Date: 06/12/2017 06:19:56 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_day_payment_detail] ( @compid varchar(10), @fromdate varchar(20), @todate varchar(20), @staff varchar(20) ) as begin declare @sp031f float select @sp031f = gsa03f from gsm01 with (nolock) where gsa00c = @compid and gsa01c = 'SP031' declare @sp055f float select @sp031f = gsa03f from gsm01 with (nolock) where gsa00c = @compid and gsa01c = 'SP055' declare @payment_0 varchar(10) declare @payment_3 varchar(10) declare @payment_$ varchar(10) declare @payment_u1 varchar(10), @payment_u2 varchar(10), @payment_u3 varchar(10), @payment_u4 varchar(10), @payment_u5 varchar(10) exec upg_isrevenue_payment @compid, @payment_0 output, @payment_3 output, @payment_$ output, @payment_u1 output, @payment_u2 output, @payment_u3 output, @payment_u4 output, @payment_u5 output if @staff = '*' or @staff = '' begin select @staff = '%' end create table #tbl_item_group ( flag int not null, -- 标志 1-项目 2-会员卡 3-产品 payment varchar(5) null, -- 支付方式 quan float null, -- 数量 amt float null, -- 金额 ) if isnull(@sp055f, 0) = 0 begin insert #tbl_item_group(flag, payment, quan, amt) select 1, ggb27c, sum(ggb05f), sum(ggb11f) from ggm02 b with (nolock), ggm01 a with (nolock), gam26 z with (nolock) where b.ggb00c = a.gga00c and b.ggb01c = a.gga01c and a.gga00c = z.gaz02c and z.gaz01c = @compid and a.gga80d >= @fromdate and a.gga80d <= @todate and a.gga93c like @staff and isnull(gga80d, '') <> '' group by ggb27c -- 产品的统计 insert #tbl_item_group(flag, payment, quan, amt) select 3, ggc27c, sum(ggc05f), sum(ggc11f) from ggm03 b with (nolock), ggm01 a with (nolock), gam26 z with (nolock) where b.ggc00c = a.gga00c and b.ggc01c = a.gga01c and a.gga00c = z.gaz02c and z.gaz01c = @compid and a.gga80d >= @fromdate and a.gga80d <= @todate and a.gga93c like @staff and isnull(gga80d, '') <> '' group by ggc27c end else begin -- 每个项目的实际金额 if isnull(@sp031f, 0) = 2 begin -- 统一出院结账 insert #tbl_item_group(flag, payment, amt) select 4, gsc04c, gsc05f from gsm03 b with (nolock), glm01 a with (nolock), gam26 z with (nolock) where b.gsc00c = a.gla00c and b.gsc01c = a.gla01c and b.gsc02c = 'gl' and a.gla00c = z.gaz02c and z.gaz01c = @compid and a.gla80d >= @fromdate and a.gla80d <= @todate and a.gla95c like @staff end else begin -- 收银结账 insert #tbl_item_group(flag, payment, amt) select 4, gsc04c, gsc05f from gsm03 b with (nolock), ggm01 a with (nolock), gam26 z with (nolock) where b.gsc00c = a.gga00c and b.gsc01c = a.gga01c and b.gsc02c = 'gx' and a.gga00c = z.gaz02c and z.gaz01c = @compid and a.gga80d >= @fromdate and a.gga80d <= @todate and a.gga93c like @staff and isnull(gga80d, '') <> '' end end -- -- 卡的统计, 一段时间内卡的销售, 包含所有的支付方式 -- insert #tbl_item_group(flag, payment, quan, amt) -- select 2, b.gnb15c, dbo.fun_divide_default(b.gnb16f, isnull(b.gnb16f, 0) + isnull(b.gnb18f, 0) + isnull(b.gnb20f, 0) + isnull(b.gnb22f, 0), 0), b.gnb16f -- from gnm02 b with (nolock), gnm01 a with (nolock), gam26 z with (nolock) -- where a.gna00c = b.gnb00c -- and a.gna01c = b.gnb01c -- and a.gna80d >= @fromdate -- and a.gna80d <= @todate -- and a.gna00c = z.gaz02c -- and z.gaz01c = @compid -- and a.gna93c like @staff -- and b.gnb15c is not null -- and isnull(b.gnb16f, 0) > 0 -- -- insert #tbl_item_group(flag, payment, quan, amt) -- select 2, b.gnb17c, dbo.fun_divide_default(b.gnb18f, isnull(b.gnb16f, 0) + isnull(b.gnb18f, 0) + isnull(b.gnb20f, 0) + isnull(b.gnb22f, 0), 0), b.gnb18f -- from gnm02 b with (nolock), gnm01 a with (nolock), gam26 z with (nolock) -- where a.gna00c = b.gnb00c -- and a.gna01c = b.gnb01c -- and a.gna80d >= @fromdate -- and a.gna80d <= @todate -- and a.gna00c = z.gaz02c -- and z.gaz01c = @compid -- and a.gna93c like @staff -- and b.gnb17c is not null -- and isnull(b.gnb18f, 0) > 0 -- -- insert #tbl_item_group(flag, payment, quan, amt) -- select 2, b.gnb19c, dbo.fun_divide_default(b.gnb20f, isnull(b.gnb16f, 0) + isnull(b.gnb18f, 0) + isnull(b.gnb20f, 0) + isnull(b.gnb22f, 0), 0), b.gnb20f -- from gnm02 b with (nolock), gnm01 a with (nolock), gam26 z with (nolock) -- where a.gna00c = b.gnb00c -- and a.gna01c = b.gnb01c -- and a.gna80d >= @fromdate -- and a.gna80d <= @todate -- and a.gna00c = z.gaz02c -- and z.gaz01c = @compid -- and a.gna93c like @staff -- and b.gnb19c is not null -- and isnull(b.gnb20f, 0) > 0 -- -- insert #tbl_item_group(flag, payment, quan, amt) -- select 2, b.gnb21c, dbo.fun_divide_default(b.gnb22f, isnull(b.gnb16f, 0) + isnull(b.gnb18f, 0) + isnull(b.gnb20f, 0) + isnull(b.gnb22f, 0), 0), b.gnb22f -- from gnm02 b with (nolock), gnm01 a with (nolock), gam26 z with (nolock) -- where a.gna00c = b.gnb00c -- and a.gna01c = b.gnb01c -- and a.gna80d >= @fromdate -- and a.gna80d <= @todate -- and a.gna00c = z.gaz02c -- and z.gaz01c = @compid -- and a.gna93c like @staff -- and b.gnb21c is not null -- and isnull(b.gnb22f, 0) > 0 -- -- -- 这段时间内的冲值 -- insert #tbl_item_group(flag, payment, quan, amt) -- select 2, a.gcl67c, dbo.fun_divide_default(a.gcl68f, isnull(a.gcl68f, 0) + isnull(a.gcl70f, 0) + isnull(a.gcl72f, 0) + isnull(a.gcl74f, 0), 0), a.gcl68f -- from gcm10 a with (nolock), gam26 z with (nolock) -- where gcl00c = z.gaz02c -- and z.gaz01c = @compid -- and a.gcl80d >= @fromdate -- and a.gcl80d <= @todate -- and gcl93c like @staff -- and (gcl06i = 0 or gcl06i = 6) -- and isnull(gcl03c, '') <> '' -- and a.gcl67c is not null -- and isnull(a.gcl68f, 0) > 0 -- -- insert #tbl_item_group(flag, payment, quan, amt) -- select 2, a.gcl69c, dbo.fun_divide_default(a.gcl70f, isnull(a.gcl68f, 0) + isnull(a.gcl70f, 0) + isnull(a.gcl72f, 0) + isnull(a.gcl74f, 0), 0), a.gcl70f -- from gcm10 a with (nolock), gam26 z with (nolock) -- where gcl00c = z.gaz02c -- and z.gaz01c = @compid -- and a.gcl80d >= @fromdate -- and a.gcl80d <= @todate -- and gcl93c like @staff -- and (gcl06i = 0 or gcl06i = 6) -- and isnull(gcl03c, '') <> '' -- and a.gcl69c is not null -- and isnull(a.gcl70f, 0) > 0 -- -- insert #tbl_item_group(flag, payment, quan, amt) -- select 2, a.gcl71c, dbo.fun_divide_default(a.gcl72f, isnull(a.gcl68f, 0) + isnull(a.gcl70f, 0) + isnull(a.gcl72f, 0) + isnull(a.gcl74f, 0), 0), a.gcl72f -- from gcm10 a with (nolock), gam26 z with (nolock) -- where gcl00c = z.gaz02c -- and z.gaz01c = @compid -- and a.gcl80d >= @fromdate -- and a.gcl80d <= @todate -- and gcl93c like @staff -- and (gcl06i = 0 or gcl06i = 6) -- and isnull(gcl03c, '') <> '' -- and a.gcl71c is not null -- and isnull(a.gcl72f, 0) > 0 -- -- insert #tbl_item_group(flag, payment, quan, amt) -- select 2, a.gcl73c, dbo.fun_divide_default(a.gcl74f, isnull(a.gcl68f, 0) + isnull(a.gcl70f, 0) + isnull(a.gcl72f, 0) + isnull(a.gcl74f, 0), 0), a.gcl74f -- from gcm10 a with (nolock), gam26 z with (nolock) -- where gcl00c = z.gaz02c -- and z.gaz01c = @compid -- and a.gcl80d >= @fromdate -- and a.gcl80d <= @todate -- and gcl93c like @staff -- and (gcl06i = 0 or gcl06i = 6) -- and isnull(gcl03c, '') <> '' -- and a.gcl73c is not null -- and isnull(a.gcl74f, 0) > 0 -- -- insert #tbl_item_group(flag, payment, quan, amt) -- select 2, gsc04c, dbo.fun_divide_default(gsc05f,gea12f,0), sum(gsc05f) -- from gem01 with (nolock), gsm03 with (nolock), gam26 z with (nolock) -- where gea00c = z.gaz02c -- and z.gaz01c = @compid -- and gea80d between @fromdate and @todate -- and gsc00c = gea00c -- and gea01c = gsc01c -- and gsc02c = 'ge' -- and gsc04c in ('1', '6', 'ZFB', 'WX', @payment_0, @payment_3, @payment_$, @payment_u1, @payment_u2, @payment_u3, @payment_u4, @payment_u5) -- and gea21c like @staff -- group by gsc04c,gsc05f,gea12f -- -- insert #tbl_item_group(flag, payment, quan, amt) -- select 2, a.gcl67c, -dbo.fun_divide_default(a.gcl68f, isnull(a.gcl68f, 0) + isnull(a.gcl70f, 0) + isnull(a.gcl72f, 0) + isnull(a.gcl74f, 0), 0), -a.gcl68f -- from gcm10 a with (nolock), gam26 z with (nolock) -- where gcl00c = z.gaz02c -- and z.gaz01c = @compid -- and a.gcl80d >= @fromdate -- and a.gcl80d <= @todate -- and gcl93c like @staff -- and (gcl06i = 1 or gcl06i = 5) -- and isnull(gcl03c, '') <> '' -- and a.gcl67c is not null -- and isnull(a.gcl68f, 0) > 0 -- -- insert #tbl_item_group(flag, payment, quan, amt) -- select 2, a.gcl69c, -dbo.fun_divide_default(a.gcl70f, isnull(a.gcl68f, 0) + isnull(a.gcl70f, 0) + isnull(a.gcl72f, 0) + isnull(a.gcl74f, 0), 0), -a.gcl70f -- from gcm10 a with (nolock), gam26 z with (nolock) -- where gcl00c = z.gaz02c -- and z.gaz01c = @compid -- and a.gcl80d >= @fromdate -- and a.gcl80d <= @todate -- and gcl93c like @staff -- and (gcl06i = 1 or gcl06i = 5) -- and isnull(gcl03c, '') <> '' -- and a.gcl69c is not null -- and isnull(a.gcl70f, 0) > 0 -- -- insert #tbl_item_group(flag, payment, quan, amt) -- select 2, a.gcl71c, -dbo.fun_divide_default(a.gcl72f, isnull(a.gcl68f, 0) + isnull(a.gcl70f, 0) + isnull(a.gcl72f, 0) + isnull(a.gcl74f, 0), 0), -a.gcl72f -- from gcm10 a with (nolock), gam26 z with (nolock) -- where gcl00c = z.gaz02c -- and z.gaz01c = @compid -- and a.gcl80d >= @fromdate -- and a.gcl80d <= @todate -- and gcl93c like @staff -- and (gcl06i = 1 or gcl06i = 5) -- and isnull(gcl03c, '') <> '' -- and a.gcl71c is not null -- and isnull(a.gcl72f, 0) > 0 -- -- insert #tbl_item_group(flag, payment, quan, amt) -- select 2, a.gcl73c, -dbo.fun_divide_default(a.gcl74f, isnull(a.gcl68f, 0) + isnull(a.gcl70f, 0) + isnull(a.gcl72f, 0) + isnull(a.gcl74f, 0), 0), -a.gcl74f -- from gcm10 a with (nolock), gam26 z with (nolock) -- where gcl00c = z.gaz02c -- and z.gaz01c = @compid -- and a.gcl80d >= @fromdate -- and a.gcl80d <= @todate -- and gcl93c like @staff -- and (gcl06i = 1 or gcl06i = 5) -- and isnull(gcl03c, '') <> '' -- and a.gcl73c is not null -- and isnull(a.gcl74f, 0) > 0 insert #tbl_item_group(flag, payment, quan, amt) select 2,gsc04c,sum(1),sum(gsc05f) from gsm03 a with(nolock), gam26 b with(nolock) where a.gsc00c = b.gaz02c and b.gaz01c = @compid and a.gsc00c = @compid and a.gsc10d >= @fromdate and a.gsc10d <= @todate and a.gsc02c in ('gn','gz','ge') group by gsc02c,gsc04c create table #tbl_item_result ( flag int not null, -- 标志 1-项目 2-会员卡 3-产品 payment varchar(40) null, -- 支付方式 quan float null, -- 数量 amt float null, -- 金额 ) insert #tbl_item_result(flag, payment, quan, amt) select flag, payment, sum(quan), sum(amt) from #tbl_item_group group by flag, payment order by payment, flag update a set a.payment = b.gsb03c from #tbl_item_result a, gsm02 b where a.payment = b.gsb02c and b.gsb00c = @compid and b.gsb01c = '3' select isnull((case when flag = 1 then '项目' when flag = 2 then '卡' when flag = 3 then '产品' when flag = 4 then '项目/产品' end),'') item, isnull(payment,'') payment, isnull(quan,'') quan, isnull(amt,'') amt from #tbl_item_result drop table #tbl_item_group drop table #tbl_item_result end GO /****** Object: StoredProcedure [dbo].[upg_stst_outstock_detail] Script Date: 06/12/2017 06:20:46 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_stst_outstock_detail] ( @compid varchar(10), -- 公司别 @fromdate varchar(10), -- 开始日期 @todate varchar(10), -- 截至日期 @fromgood varchar(20), -- 开始产品编号 @togood varchar(20), -- 截至产品编号 @fromstockid varchar(10), -- 仓库编号 @tostockid varchar(10) -- 仓库编号 ) as begin declare @includesons float select @includesons = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP103' create table #tbl_out_stock( goodid varchar(20) not null, -- 产品代码 outtype varchar(10) not null, -- 出库类型 quan float null, -- 数量 amt float null, amt_cost float null, amt_std float null, unit varchar(10) null) if @includesons = 1 begin insert #tbl_out_stock(goodid, outtype, quan, amt) select gib03c, gia07i, sum(gib08f), sum(isnull(gib10f, 0)) from gim01 a with (nolock), gim02 b with (nolock), gam26 z where a.gia00c = b.gib00c and a.gia01c = b.gib01c and ((a.gia03d >= @fromdate and a.gia03d <= @todate) or (@fromdate = '*')) and ((a.gia04c >= @fromstockid and a.gia04c <= @tostockid) or (@fromstockid = '*')) and ((b.gib03c >= @fromgood and b.gib03c <= @togood) or (@fromgood = '*')) and a.gia00c = z.gaz02c and z.gaz01c = @compid group by gib03c, gia07i end else begin insert #tbl_out_stock(goodid, outtype, quan, amt) select gib03c, gia07i, sum(gib08f), sum(isnull(gib10f, 0)) from gim01 a with (nolock), gim02 b with (nolock) where a.gia00c = b.gib00c and a.gia01c = b.gib01c and ((a.gia03d >= @fromdate and a.gia03d <= @todate) or (@fromdate = '*')) and ((a.gia04c >= @fromstockid and a.gia04c <= @tostockid) or (@fromstockid = '*')) and ((b.gib03c >= @fromgood and b.gib03c <= @togood) or (@fromgood = '*')) and a.gia00c = @compid group by gib03c, gia07i end --如果销售是直接出仓库的,则需要加上 declare @sp014f float select @sp014f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP014' if @sp014f = 1 begin -- 设置0为产品销售的出库类别代码 if @includesons = 1 begin insert #tbl_out_stock(goodid, outtype, quan, amt) select gfd04c, 0, sum(gfd05f), sum(isnull(gfd13f, 0)) from gfm03 a with (nolock), gfm04 b with (nolock), gam26 z where a.gfc00c = b.gfd00c and a.gfc01c = b.gfd01c and a.gfc02c = b.gfd02c and a.gfc01c = '3' and ((a.gfc03c >= @fromdate and a.gfc03c <= @todate) or (@fromdate = '*')) and ((b.gfd08c >= @fromstockid and b.gfd08c <= @tostockid) or (@fromstockid = '*')) and ((b.gfd04c >= @fromgood and b.gfd04c <= @togood) or (@fromgood = '*')) and a.gfc00c = z.gaz02c and z.gaz01c = @compid group by gfd04c end else begin insert #tbl_out_stock(goodid, outtype, quan, amt) select gfd04c, 0, sum(gfd05f), sum(isnull(gfd13f, 0)) from gfm03 a with (nolock), gfm04 b with (nolock) where a.gfc00c = b.gfd00c and a.gfc01c = b.gfd01c and a.gfc02c = b.gfd02c and a.gfc01c = '3' and ((a.gfc03c >= @fromdate and a.gfc03c <= @todate) or (@fromdate = '*')) and ((b.gfd08c >= @fromstockid and b.gfd08c <= @tostockid) or (@fromstockid = '*')) and ((b.gfd04c >= @fromgood and b.gfd04c <= @togood) or (@fromgood = '*')) and a.gfc00c = @compid group by gfd04c end end declare @SP054f float -- 成本计算方式 select @SP054f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP054' --需要修改成本 if @SP054f = 2 begin declare @curmonth varchar(20) select @curmonth = substring(@todate, 1, 6) exec upg_create_good_month_cost @compid, @curmonth update a set a.amt_cost = b.gfh03f*a.quan from #tbl_out_stock a, gfm08 b where a.goodid = b.gfh01c and b.gfh02c = substring(@todate, 1, 6) and b.gfh00c = @compid end else begin update a set a.amt_cost = b.gfa22f*a.quan from #tbl_out_stock a, gfm01 b where a.goodid = b.gfa01c and b.gfa00c = @compid end --计算标准的金额 update a set a.amt_std = b.gfa11f*a.quan from #tbl_out_stock a, gfm01 b where a.goodid = b.gfa01c and b.gfa00c = @compid select * from #tbl_out_stock order by outtype drop table #tbl_out_stock end GO /****** Object: StoredProcedure [dbo].[upg_ghm01_confirm_in_check] Script Date: 06/12/2017 06:20:11 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_ghm01_confirm_in_check] ( @compid varchar(10), -- 公司编号 @billno varchar(100), -- 入库单号 @billdate varchar(8), -- 单据日期 @storageno varchar(4), -- 仓库编号 @checkman varchar(10), -- 复核人 @currdate varchar(8), -- 复核日期 @curtime varchar(20) -- 复核时间 ) as begin set nocount on -- 当前的事务数 declare @__transaction_count int set @__transaction_count = @@trancount -- try...catch处理 begin try -- a.开启事务,或者设置事务保存点 if @__transaction_count = 0 begin tran else save tran __tran_SavePoint -- update 库存日异动统计表头档 insert gfm03(gfc00c, gfc01c, gfc02c, gfc03c, gfc040c, gfc041c, gfc05c, gfc06i, gfc08i, gfc07c, gfc09t) select gha00c, '1', gha01c, gha03d, '3', gha04c, gha04c, gha07i, gha10i, gha05c, gha09t from ghm01 where gha00c = @compid and gha01c = @billno -- update 库存日异动统计明细档 --added gfd10d by WJG insert gfm04(gfd00c, gfd01c, gfd02c, gfd03f, gfd04c, gfd05f, gfd06f, gfd07c, gfd08c, gfd10d, gfd11c, gfd12f, gfd13f, gfd14i, gfd15c) select ghb00c, '1', ghb01c, ghb02f, ghb03c, ghb13f, ghb14f, @billdate, @storageno, ghb15d, ghb06c, isnull(ghb07f, 0), isnull(ghb11f, 0), 1, gha05c --added ghb15d by WJG from ghm02 b with (nolock), ghm01 a with (nolock), gfm01 c with (nolock) where ghb00c = @compid and ghb01c = @billno and ghb00c = gfa00c and ghb03c = gfa01c and a.gha00c = b.ghb00c and a.gha01c = b.ghb01c /*-----------Establish the patch information of commodities----------*/ /*-----------Added by WJG--------------------------------------------*/ -- begin insert into ghm03(ghc00c, ghc01c, ghc02c, ghc03d, ghc04d, ghc05f, ghc06c, ghc07d) select ghb00c, ghb03c, ghb01c, ghb15d, convert(varchar(8), dateadd(dd, isnull(gfa28i, 0), cast(ghb15d as datetime)), 112), ghb13f, gha04c, gha03d from ghm01 with (nolock), (select ghb00c, ghb03c, ghb01c, ghb15d, sum(isnull(ghb13f, 0)) ghb13f from ghm02 with (nolock) where ghb00c = @compid and ghb01c = @billno group by ghb00c, ghb03c, ghb01c, ghb15d) T, gfm01 where gha00c = ghb00c and ghb00c = gfa00c and ghb03c = gfa01c and gha01c = ghb01c -- end update gf set gf.gfb06f = isnull(gf.gfb06f, 0) + isnull(gh.ghb13f, 0), gf.gfb07f = isnull(gf.gfb07f, 0) + isnull(gh.ghb11f, 0) from gfm02 gf left outer join gfm01 on gfa00c = gfb00c and gfa01c = gfb01c left outer join (select ghb00c, ghb03c, ghb01c, sum(isnull(ghb11f, 0)) ghb11f, sum(isnull(ghb13f, 0)) ghb13f from ghm02 with (nolock) where ghb00c = @compid and ghb01c = @billno group by ghb00c, ghb03c, ghb01c, ghb15d) gh on ghb00c = gfa00c and ghb03c = gfa01c where ghb03c = gfb01c and gfb02c = substring(@billdate, 1, 6) and gfb00c = ghb00c and gfb04c = @storageno and gfb03c = '1' insert into gfm02 (gfb00c, gfb01c, gfb02c, gfb03c, gfb04c, gfb06f, gfb07f) select ghb00c, ghb03c, substring(@billdate, 1, 6), '1', @storageno, isnull(ghb13f, 0), isnull(ghb11f, 0) from (select ghb00c, ghb03c, ghb01c, sum(isnull(ghb11f, 0)) ghb11f, sum(isnull(ghb13f, 0)) ghb13f from ghm02 with (nolock) where ghb00c = @compid and ghb01c = @billno group by ghb00c, ghb03c, ghb01c, ghb15d) T left outer join gfm01 on ghb00c = gfa00c and ghb03c = gfa01c where ghb03c not in (select gfb01c from gfm02 where gfb00c = ghb00c and gfb01c = ghb03c and gfb02c = substring(@billdate, 1, 6) and gfb03c = '1' and gfb04c = @storageno ) -- update 产品入库单 update ghm01 set gha93c = @checkman, gha94d = @currdate where gha00c = @compid and gha01c = @billno -- 产品进价计算方式 declare @caltype float select @caltype = gsa03f from gsm01 with (nolock) where gsa00c = @compid and gsa01c = 'SP054' -- 更新产品进价 added by mzh 2005/10/30 if (@caltype = 0) begin -- 出库入库 create table #out_goods ( goods varchar(20) null, -- 产品编号 number int ) insert into #out_goods(number, goods) select row_number() over(order by P.ghb03c) as number, P.ghb03c from (select distinct ghb03c from ghm02 with (nolock) where ghb00c = @compid and ghb01c = @billno and isnull(ghb03c, '') <> '') P declare @price float declare @goods varchar(20) declare @date varchar(8) set @date = convert(varchar(10), getdate(), 112) declare @i int set @i = 1 declare @max int select @max = max(number) from #out_goods while @i <= @max begin set @price = 0 select @goods = goods from #out_goods where number = @i exec upg_calculate_prod_price_mv @compid, @goods, @date, @price output update gfm01 set gfa22f = @price from gfm01 where gfa00c = @compid and gfa01c = @goods set @i = @i + 1 end drop table #out_goods end else if @caltype = 2 begin exec upg_create_good_month_cost @compid, @date end --记录日志 insert sysoperationlog(userid, program, operation, operationdate, operationtime, origatedate, compid, keyvalue1, keyvalue2, keyvalue3) values(@checkman, 'GWA01I', 'M', @currdate, @curtime, @currdate, @compid, @billno, @storageno, '复核') -- 提交事务 -- 有可提交的事务, 并且事务是在当前模块中开启的情况下, 才提交事务 if xact_state() = 1 and @__transaction_count = 0 commit -- 为了防止TRY 中有遗漏的事务处理, 可以在TRY 模拟的结束部分做最终的判断 if @__transaction_count = 0 begin if xact_state() = -1 rollback tran else begin while @@trancount > 0 commit tran end end end try begin catch -- 在CATCH 模块, 应该首先处理事务 if xact_state() <> 0 begin if @__transaction_count = 0 rollback tran -- xact_state 为-1 时, 不能回滚到事务保存点, 这种情况留给外层调用者做统一的事务回滚 -- 通过@@trancount > @__transaction_count 的判断, 即使在TRY 模块中没有设置事务保存点的情况下跳到此步骤, 也不会出错 else if xact_state() = 1 and @@trancount > @__transaction_count rollback tran __tran_SavePoint end declare @SP_Parameters varchar(max) set @SP_Parameters = '@compid = ' + isnull(@compid, '') + '@billno = ' + isnull(@billno, '') + '@billdate = ' + isnull(@billdate, '') + '@storageno = ' + isnull(@storageno, '') + '@checkman = ' + isnull(@checkman, '') + '@currdate = ' + isnull(@currdate, '') + '@curtime = ' + isnull(@curtime, '') insert into SP_TranLog(SPName, SPTime, SPParameter) values('upg_ghm01_confirm_in_check', getdate(), @SP_Parameters) return -1 end catch end GO /****** Object: StoredProcedure [dbo].[upg_ghm01_confirm_in_uncheck] Script Date: 06/12/2017 06:20:11 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_ghm01_confirm_in_uncheck] ( @compid varchar(10), -- 公司编号 @billno varchar(100), -- 入库(出库)单号 @billdate varchar(8), -- 单据日期 @storageno varchar(4), -- 仓库编号 @checkman varchar(10), -- 复核人 @currdate varchar(8), -- 复核日期 @curtime varchar(20) -- 复核时间 ) as begin set nocount on -- 当前的事务数 declare @__transaction_count int set @__transaction_count = @@trancount -- try...catch处理 begin try -- a.开启事务,或者设置事务保存点 if @__transaction_count = 0 begin tran else save tran __tran_SavePoint -- update 库存日异动统计表头档 delete from gfm03 where gfc00c = @compid and gfc01c = '1' and gfc02c = @billno -- update 库存日异动统计明细档 delete from gfm04 where gfd00c = @compid and gfd01c = '1' and gfd02c = @billno /*-----------delete the patch information of commodities ----------*/ /*-----------Added by WJG--------------------------------------------*/ --begin delete ghm03 where ghc00c = @compid and ghc02c = @billno and ghc06c = @storageno --end update gfm02 set gfb06f = isnull(gfb06f, 0) - isnull(gh.ghb13f, 0), gfb07f = isnull(gfb07f, 0) - isnull(gh.ghb11f, 0) from gfm02 gf left outer join gfm01 on gfa00c = gfb00c and gfa01c = gfb01c left outer join (select ghb00c, ghb03c, ghb01c, sum(isnull(ghb11f, 0)) ghb11f, sum(isnull(ghb13f, 0)) ghb13f from ghm02 with (nolock) where ghb00c = @compid and ghb01c = @billno group by ghb00c, ghb03c, ghb01c, ghb15d) gh on ghb00c = gfa00c and ghb03c = gfa01c where ghb03c = gfb01c and gfb02c = substring(@billdate, 1, 6) and gfb00c = ghb00c and gfb04c = @storageno and gfb03c = '1' update ghm01 set gha93c = '', gha94d = '' --update 产品入库单 where gha00c = @compid and gha01c = @billno -- 产品进价计算方式 declare @caltype float select @caltype = gsa03f from gsm01 with (nolock) where gsa00c = @compid and gsa01c = 'SP054' -- 更新产品进价 added by mzh 2005/10/30 if (@caltype = 0) begin -- 出库入库 create table #out_goods ( goods varchar(20) null, -- 产品编号 number int ) insert into #out_goods(number, goods) select row_number() over(order by P.ghb03c) as number, P.ghb03c from (select distinct ghb03c from ghm02 with (nolock) where ghb00c = @compid and ghb01c = @billno and isnull(ghb03c, '') <> '') P declare @price float declare @goods varchar(20) declare @date varchar(8) set @date = convert(varchar(10), getdate(), 112) declare @i int set @i = 1 declare @max int select @max = max(number) from #out_goods while @i <= @max begin set @price = 0 select @goods = goods from #out_goods where number = @i exec upg_calculate_prod_price_mv @compid, @goods, @date, @price output update gfm01 set gfa22f = @price from gfm01 where gfa00c = @compid and gfa01c = @goods set @i = @i + 1 end drop table #out_goods end else if @caltype = 2 begin exec upg_create_good_month_cost @compid, @date end --记录日志 insert sysoperationlog(userid, program, operation, operationdate, operationtime, origatedate, compid, keyvalue1, keyvalue2, keyvalue3) values(@checkman, 'GWA01I', 'M', @currdate, @curtime, @currdate, @compid, @billno, @storageno, '取消复核') -- 提交事务 -- 有可提交的事务, 并且事务是在当前模块中开启的情况下, 才提交事务 if xact_state() = 1 and @__transaction_count = 0 commit -- 为了防止TRY 中有遗漏的事务处理, 可以在TRY 模拟的结束部分做最终的判断 if @__transaction_count = 0 begin if xact_state() = -1 rollback tran else begin while @@trancount > 0 commit tran end end end try begin catch -- 在CATCH 模块, 应该首先处理事务 if xact_state() <> 0 begin if @__transaction_count = 0 rollback tran -- xact_state 为-1 时, 不能回滚到事务保存点, 这种情况留给外层调用者做统一的事务回滚 -- 通过@@trancount > @__transaction_count 的判断, 即使在TRY 模块中没有设置事务保存点的情况下跳到此步骤, 也不会出错 else if xact_state() = 1 and @@trancount > @__transaction_count rollback tran __tran_SavePoint end declare @SP_Parameters varchar(max) set @SP_Parameters = '@compid = ' + isnull(@compid, '') + '@billno = ' + isnull(@billno, '') + '@billdate = ' + isnull(@billdate, '') + '@storageno = ' + isnull(@storageno, '') + '@checkman = ' + isnull(@checkman, '') + '@currdate = ' + isnull(@currdate, '') + '@curtime = ' + isnull(@curtime, '') insert into SP_TranLog(SPName, SPTime, SPParameter) values('upg_ghm01_confirm_in_uncheck', getdate(), @SP_Parameters) return -1 end catch end GO /****** Object: StoredProcedure [dbo].[upg_analyzer_customer_ex_ceshi] Script Date: 06/12/2017 06:19:27 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_analyzer_customer_ex_ceshi] ( @compid varchar(10), @class varchar(10), @frommemid varchar(20), -- 会员开始编号 @tomemid varchar(20), -- 会员截至编号 @memfromdate varchar(10), -- 资料输入日期 @memtodate varchar(10), -- 资料输入日期 @fromdate varchar(20), -- 消费开始日期 @todate varchar(20), -- 消费截至日期 @state varchar(200), -- 会员卡状态 @acon_amt float, -- 大于消费金额 @alastdate varchar(8), -- 某个日期之后 @at_times float, -- 大于消费次数 @at_history float, -- 会员历史大于年限 @aav_amt float, -- 大于平均消费金额 @aless_remain float, -- 小于某个储值余额 @abig_remain float, -- 大于某个储值余额 @addtoSMS int, @staitem int, -- 统计类型--2006/9/15 by sdw @only_card_pay int, @acctsummary int, @currentpageindex int, @pagesize int, @pageall int ) as begin declare @incompid varchar(10) select @incompid = @compid declare @sqlpage nvarchar(2314) declare @pagecount int declare @male int declare @female int declare @con_amt float declare @t_times float declare @remain01 float declare @remain02 float declare @remain03 float declare @remain04 float declare @remain05 float declare @total_fill_amt float declare @total_salecard_amt float declare @sp031f float select @sp031f = gsa03f from gsm01 with(nolock) where gsa00c = @compid and gsa01c = 'SP031' declare @payment_0 varchar(10) declare @payment_3 varchar(10) declare @payment_$ varchar(10) declare @payment_u1 varchar(10), @payment_u2 varchar(10), @payment_u3 varchar(10), @payment_u4 varchar(10), @payment_u5 varchar(10) exec upg_isrevenue_payment @compid, @payment_0 output, @payment_3 output, @payment_$ output, @payment_u1 output, @payment_u2 output, @payment_u3 output, @payment_u4 output, @payment_u5 output declare @compcode varchar(10) if @compid = '001' select @compcode = '%' else select @compcode = @compid if @fromdate<'19000101' select @fromdate = '19000101' if @todate>'20901231' select @todate = '20901231' -- 以卡的角度来统计会员 create table #card_member ( compid varchar(20) null, memid varchar(20) not null, m_name varchar(40) null, cardid varchar(20) not null, gender int null, mobile varchar(50) null, tel varchar(50) null, birthday varchar(8) null, start_date varchar(10) null, memaddr varchar(160) null, postcode varchar(20) null, principle varchar(20) null, cardclass varchar(20) null, pagecount int null primary key(cardid, memid) ) create table #mem_result( idd int identity not null, compid varchar(20) null, memid varchar(20) not null, -- Member编号 m_name varchar(40) null, -- 姓名 cardid varchar(20) null, -- 卡号 gender int null, -- 性别 0:女 mobile varchar(20) null, -- 手机 tel varchar(20) null, -- 电话 birthday varchar(8) null, state varchar(10) null, -- Member卡状态 con_amt float null, -- Consumption金额 lastdate varchar(8) null, -- 最后Consumption日期 lastcall varchar(8) null, -- 最后回访日期 t_times float null, -- 用于比较的Consumption次数 t_compare float null, -- 总共来的次数 av_ytimes float null, -- 每年来的次数 av_times float null, -- 每月来的次数 av_wtimes float null, -- 每周来的次数 months float null, -- 客人来了多少个月了,客龄 av_amt float null, -- 每次Consumption的平均金额 start_date varchar(20) null, -- 开始日期 first_date varchar(20) null, -- 第一次来店日期 comp_date varchar(20) null, --开始比较的日期 dur_years float null, -- 统计期间来了多少个年了 dur_months float null, -- 统计期间来了多少个月了 dur_weeks float null, -- 统计期间来了多少个周了 remain01 float null, -- 余额 remain02 float null, -- 余额 remain03 float null, -- 余额 remain04 float null, -- 余额 remain05 float null, -- 余额 total_fill_amt float null, -- 累计Charge金额 memaddr varchar(160) null, -- 会员地址 postcode varchar(20) null, -- 邮编 principle varchar(20) null, -- 负责人 total_salecard_amt float null, -- 累计卡销售金额 pagecount int null primary key(idd)) create table #mem_result_page_sum( idd int identity not null, con_amt float null, -- Consumption金额 t_times float null, -- 用于比较的Consumption次数 female float null, -- male float null, -- remain01 float null, -- 余额 remain02 float null, -- 余额 remain03 float null, -- 余额 remain04 float null, -- 余额 remain05 float null, -- 余额 total_fill_amt float null, -- 累计Charge金额 total_salecard_amt float null, -- 累计卡销售金额 pagecount int null primary key(idd)) create table #mem_result_page( idd int not null, compid varchar(20) null, memid varchar(20) not null, -- Member编号 m_name varchar(40) null, -- 姓名 cardid varchar(20) not null, -- 卡号 gender int null, -- 性别 0:女 mobile varchar(20) null, -- 手机 tel varchar(20) null, -- 电话 birthday varchar(8) null, state varchar(10) null, -- Member卡状态 con_amt float null, -- Consumption金额 lastdate varchar(8) null, -- 最后Consumption日期 lastcall varchar(8) null, -- 最后回访日期 t_times float null, -- 用于比较的Consumption次数 t_compare float null, -- 总共来的次数 av_ytimes float null, -- 每年来的次数 av_times float null, -- 每月来的次数 av_wtimes float null, -- 每周来的次数 months float null, -- 客人来了多少个月了,客龄 av_amt float null, -- 每次Consumption的平均金额 start_date varchar(20) null, -- 开始日期 first_date varchar(20) null, -- 第一次来店日期 comp_date varchar(20) null, --开始比较的日期 dur_years float null, -- 统计期间来了多少个年了 dur_months float null, -- 统计期间来了多少个月了 dur_weeks float null, -- 统计期间来了多少个周了 remain01 float null, -- 余额 remain02 float null, -- 余额 remain03 float null, -- 余额 remain04 float null, -- 余额 remain05 float null, -- 余额 total_fill_amt float null, -- 累计Charge金额 memaddr varchar(160) null, -- 会员地址 postcode varchar(20) null, -- 邮编 principle varchar(20) null, -- 负责人 total_salecard_amt float null, -- 累计卡销售金额 pagecount int null primary key(memid,cardid)) create table #sum_tmp( vid varchar(20) not null, vsum float null, v_1 varchar(20) null, f_1 float null, v_2 varchar(20) null, primary key(vid)) create table #gcm03 ( gcc00c varchar(10) not null, --门店编号 gcc01c varchar(20) not null, --卡号 gcc03i int not null, --账户类别 gcc03iname varchar(40) null, --账户名称 gcc06f float null --馀额 primary key (gcc00c,gcc01c,gcc03i) ) create nonclustered index idx_gcm03 on #gcm03(gcc00c,gcc01c,gcc03i) create table #sum_gcm03 ( gcc03i int not null, --账户类别 gcc03iname varchar(40) null, --账户名称 gcc06f float null, --馀额 pagecount float null, primary key (gcc03i) ) --会员初始化 insert #card_member(compid, memid, m_name, cardid, gender, mobile, tel, birthday, start_date, memaddr, postcode, principle, cardclass) select distinct isnull(gca13d, gca00c), isnull(gca01c, ''), gba03c, gca01c, gba14i, gba08c, gba07c, gba17d, isnull(gba02c, '20100101'), gba05c, gba12c, gba10c, gca02c from gcm01 a with (nolock), gam26 z with (nolock), gbm01 b with (nolock) where a.gca00c = b.gba00c and a.gca04c = b.gba01c and a.gca00c = z.gaz02c and z.gaz01c = @incompid and (a.gca05d between @memfromdate and @memtodate or @memfromdate = '*' or @memtodate = '*') and substring(a.gca01c, 1, 2)<>'散客' update a set a.m_name = '未知' from #card_member a where isnull(m_name, '') = '' if (@class = '' or @class = '*') begin if @frommemid = '' or @frommemid = '*' begin insert #mem_result(compid, memid, m_name, cardid, gender, mobile, tel, birthday, start_date, memaddr, postcode, principle,pagecount) select compid, memid, m_name, cardid, gender, mobile, tel, birthday, start_date, memaddr, postcode, principle,pagecount from #card_member a end else begin insert #mem_result(compid, memid, m_name, cardid, gender, mobile, tel, birthday, start_date, memaddr, postcode, principle,pagecount) select compid, memid, m_name, cardid, gender, mobile, tel, birthday, start_date, memaddr, postcode, principle,pagecount from #card_member a where a.memid >= @frommemid and a.memid <= @tomemid end end else begin if @frommemid = '' or @frommemid = '*' begin insert #mem_result(compid, memid, m_name, cardid, gender, mobile, tel, birthday, start_date, memaddr, postcode, principle,pagecount) select compid, memid, m_name, cardid, gender, mobile, tel, birthday, start_date, memaddr, postcode, principle,pagecount from #card_member a where a.cardclass = @class end else begin insert #mem_result(compid, memid, m_name, cardid, gender, mobile, tel, birthday, start_date, memaddr, postcode, principle,pagecount) select compid, memid, m_name, cardid, gender, mobile, tel, birthday, start_date, memaddr, postcode, principle,pagecount from #card_member a where a.cardclass = @class and a.memid >= @frommemid and a.memid <= @tomemid end end drop table #card_member --去掉散客 delete #mem_result where substring(memid, 1, 2) = '散客' --删除没有卡号的会员资料 delete #mem_result where isnull(cardid, '') = '' --消费总金额, 最后消费日期, 消费次数 exec upg_stat_card_consume @incompid, @fromdate, @todate, @frommemid, @tomemid, @only_card_pay --统计每个会员消费的次数 update a set a.con_amt = b.vsum, a.lastdate = b.v_1, a.t_times = b.f_1, a.first_date = b.v_2 from #mem_result a, #sum_tmp b where a.memid = b.vid --最后回访日期 delete #sum_tmp --2006/9/15 by sdw, 新增,决定用来计算的开始时间,开始时间小于第一次消费时,则以第一次消费为准,否则,反之 update a set a.comp_date = a.first_date from #mem_result a where a.first_date>@fromdate if @frommemid = '' or @frommemid = '*' begin insert #sum_tmp(vid, v_1) select gbf01c, max(gbf04c) from gbm06 with(nolock), gam26 z with(nolock) where gbf00c = z.gaz02c and z.gaz01c = @incompid and isnull(gbf01c, '')<>'' group by gbf01c end else begin insert #sum_tmp(vid, v_1) select gbf01c, max(gbf04c) from gbm06 with(nolock), gam26 z with(nolock) where gbf00c = z.gaz02c and z.gaz01c = @incompid and gbf01c >= @frommemid and gbf01c <= @tomemid and isnull(gbf01c, '')<>'' group by gbf01c end update a set a.lastcall = b.v_1 from #mem_result a, #sum_tmp b where a.memid = b.vid --每次消费的平均金额 update a set a.av_amt = isnull(a.con_amt, 0)/isnull(a.t_times, 1) from #mem_result a where isnull(a.t_times, 0) <> 0 --更新余额 create table #gcm03_tmp( cardid varchar(20) not null, gcc03i int not null, --帐号类别(1-电子钱包, 2-储值帐号, 3-积分, 4-储值账户二, 5-储值账户二) gcc06f float null --馀额 ) create clustered index idx_gcm03_tmp on #gcm03_tmp(cardid, gcc03i) insert #gcm03_tmp(cardid, gcc03i, gcc06f) select gca01c, gcc03i, gcc06f from #mem_result a, gcm03 b with (nolock), gcm01 c with (nolock) where a.cardid = b.gcc01c and b.gcc00c = c.gca00c and b.gcc01c = c.gca01c and c.gca00c = c.gca13d update a set a.remain01 = b.gcc06f from #mem_result a, #gcm03_tmp b where a.cardid = b.cardid and b.gcc03i = 1 update a set a.remain02 = b.gcc06f from #mem_result a, #gcm03_tmp b where a.cardid = b.cardid and b.gcc03i = 2 update a set a.remain03 = b.gcc06f from #mem_result a, #gcm03_tmp b where a.cardid = b.cardid and b.gcc03i = 3 update a set a.remain04 = b.gcc06f from #mem_result a, #gcm03_tmp b where a.cardid = b.cardid and b.gcc03i = 4 update a set a.remain05 = b.gcc06f from #mem_result a, #gcm03_tmp b where a.cardid = b.cardid and b.gcc03i = 5 drop table #gcm03_tmp --客人来了多少个月了, 从来店那天开始 ----2006/9/15 by sdw, 新增,更新统计期间总共有多少个年/日 --处理一下可能的日期字段错误 update #mem_result set start_date = '20010101' where isnull(start_date, '') = '' update #mem_result set comp_date = '20010101' where isnull(comp_date, '') = '' update #mem_result set first_date = '20010101' where isnull(first_date, '') = '' update #mem_result set lastdate = '20010101' where isnull(lastdate, '') = '' --update a --set a.months = isnull(datediff(month, convert(datetime, substring(a.start_date, 1, 8)), getdate()), 0) + 1, -- a.dur_years = isnull(datediff(year, convert(datetime, substring(a.comp_date, 1, 8)), convert(datetime, @todate)), 0) + 1, -- a.dur_months = isnull(datediff(month, convert(datetime, substring(a.comp_date, 1, 8)), convert(datetime, @todate)), 0) + 1, -- a.dur_weeks = isnull(datediff(week, convert(datetime, substring(a.comp_date, 1, 8)), convert(datetime, @todate)), 0) + 1 -- from #mem_result a --平均每月多少次 ----2006/9/15 by sdw, 新增,更新统计期间的年/周平均次数 update a set a.av_ytimes = a.t_times/a.dur_years, a.av_times = a.t_times/a.dur_months, a.av_wtimes = a.t_times/a.dur_weeks from #mem_result a ----2006/9/15 by sdw, 新增,根据统计分类的不同,来决定参与比较的是年平均次数,还是月/日平均次数 if (@staitem = 2) begin update a set a.t_compare = a.av_ytimes from #mem_result a end else if (@staitem = 1) begin update a set a.t_compare = a.av_times from #mem_result a end else if (@staitem = 0) begin update a set a.t_compare = a.av_wtimes from #mem_result a end else if (@staitem = 3) begin update a set a.t_compare = a.t_times from #mem_result a end --统计会员的累计充值金额 --是从gcm04中统计的,如果gcm04中不准,则统计可能错误。与会员卡销售的统计的方面不同 --会员卡销售是从原始的gcm10中直接统计的 delete #sum_tmp declare @custverflag varchar(20) select @custverflag = gsa02c from gsm01 with (nolock) where gsa00c = @incompid and gsa01c = 'SP109' insert #sum_tmp(vid, vsum) select gcd01c, sum(gcd06f) from gcm04 a with (nolock), gam26 z with(nolock) -- #mem_result b add by lm 2015.11.17 where (gcd02i<>1 and gcd02i<>3) and (gcd05i = 0 or gcd05i = 6) and a.gcd00c = z.gaz02c and z.gaz01c = @incompid and a.gcd09d >= @fromdate and a.gcd09d <= @todate -- and a.gcd01c = b.cardid group by gcd01c update a set a.total_fill_amt = isnull(b.vsum, 0) from #mem_result a, #sum_tmp b where a.cardid = b.vid delete #sum_tmp insert #sum_tmp(vid, vsum) select gcd01c, sum(gcd06f) from gcm04 a with (nolock), gam26 z with(nolock) --, #mem_result b add by lm 2015.11.17 where (gcd02i<>1 and gcd02i<>3) and (gcd05i = 1 or gcd05i = 5) and a.gcd00c = z.gaz02c and z.gaz01c = @incompid -- and a.gcd01c = b.cardid and a.gcd09d >= @fromdate and a.gcd09d <= @todate and (a.gcd07c <> 'gx' and a.gcd07c <> 'gl') group by gcd01c update a set a.total_fill_amt = isnull(a.total_fill_amt, 0) - isnull(b.vsum, 0) from #mem_result a, #sum_tmp b where a.cardid = b.vid delete #sum_tmp -- 累计消费金额的计算完成 --计算累计的卡销售金额,因为卡销售金额可能不同于储值金额 --Added by WUXM, 20081005 --因为卡在统计累计充值的时候是考虑到所有账户的,所以统计销售的时候也要考虑疗程的销售 --从gsm03取比较合适 create table #tbl_gsm03( cardid varchar(20) not null, pay varchar(10) not null, ddate varchar(20) null, compid varchar(10) not null, amt float null) create clustered index idx_gsm03_01 on #tbl_gsm03(cardid) insert #tbl_gsm03(cardid, compid, pay, ddate, amt) select isnull(gna13c, ''), gsc00c, gsc04c, isnull(gsc10d, ''), gsc05f from gnm01 a with (nolock), gsm03 b with (nolock), gam26 z with(nolock) where a.gna00c = b.gsc00c and a.gna01c = b.gsc01c and b.gsc02c = 'gn' and a.gna00c = z.gaz02c and z.gaz01c = @compid and a.gna02d >= @fromdate and a.gna02d <= @todate insert #tbl_gsm03(cardid, compid, pay, ddate, amt) select isnull(gcl03c, ''), gsc00c, gsc04c, isnull(gsc10d, ''), gsc05f from gcm10 a with (nolock), gsm03 b with (nolock), gam26 z with(nolock) where a.gcl00c = b.gsc00c and a.gcl01c = b.gsc01c and b.gsc02c = 'gz' and a.gcl00c = z.gaz02c and z.gaz01c = @compid and a.gcl02d >= @fromdate and a.gcl02d <= @todate --start add by lm 2016.03.01 insert #tbl_gsm03(cardid, compid, pay, ddate, amt) select isnull(gea07c, ''), gsc00c, gsc04c, isnull(gsc10d, ''), gsc05f from gem01 a with (nolock), gsm03 b with (nolock), gam26 z with(nolock) where a.gea00c = b.gsc00c and a.gea01c = b.gsc01c and b.gsc02c = 'ge' and a.gea00c = z.gaz02c and z.gaz01c = @compid and a.gea03d >= @fromdate and a.gea03d <= @todate and isnull(b.gsc10d,'') <> '' -- add lucz and a.gea02i = 4 --end insert #sum_tmp(vid, vsum) select a.cardid, sum(amt) from #tbl_gsm03 a with (nolock), #mem_result b where a.cardid = b.cardid and a.pay in (select pay from pay_classify where comp = @compid and category = '1') -- modify 2015-08-07 by lm 支付方式判断改为支付方式大类判断。 group by a.cardid drop table #tbl_gsm03 update a set a.total_salecard_amt = isnull(b.vsum, 0) from #mem_result a, #sum_tmp b where a.cardid = b.vid --更新卡的状态 --Added by Mzh 2006/03/23 update a set a.state = cast(gca08i as varchar(10)) from #mem_result a, gcm01 b where gca04c = memid and gca00c = gca13d if(isnull(@alastdate,'') = '' or isnull(@alastdate,'') = '*' ) set @alastdate = '00000000' if @addtoSMS = 1 begin declare @cur_max float select @cur_max = max(gxa01f) from gxm01 with(nolock) where gxa00c = @compid select @cur_max = isnull(@cur_max, 0) insert gxm01(gxa00c, gxa01f, gxa02c, gxa03c) select @compid, idd = @cur_max + idd, memid, mobile from #mem_result_page where memid not in (select gxa02c from gxm01 where gxa00c = @compid) and isnull(memid, '')<>'' and isnull(con_amt, 0) >= @acon_amt and isnull(lastdate, '00000000') >= @alastdate and isnull(t_times, 0) >= @at_times and isnull(av_amt, 0) >= @aav_amt end if (@state = '' or @state = '*') begin if(@pageall = 0) begin insert #mem_result_page(idd, compid, memid, m_name, cardid, gender, mobile, tel, birthday, state, con_amt, lastdate, lastcall, t_times, t_compare, av_ytimes, av_times, av_wtimes, months, av_amt, start_date, first_date, comp_date, dur_years, dur_months, dur_weeks, remain01, remain02, remain03, remain04, remain05, total_fill_amt, memaddr, postcode, principle, total_salecard_amt,pagecount) select idd, compid, memid, m_name, cardid, gender, mobile, tel, birthday, state, con_amt, lastdate, lastcall, t_times, t_compare, av_ytimes, av_times, av_wtimes, months, av_amt, start_date, first_date, comp_date, dur_years, dur_months, dur_weeks, remain01, remain02, remain03, remain04, remain05, total_fill_amt, memaddr, postcode, principle, total_salecard_amt,pagecount from #mem_result where isnull(con_amt, 0) >= @acon_amt and isnull(lastdate, '19740313') >= @alastdate and isnull(t_compare, 0) >= @at_times --2006/9/15 by sdw, 修改,比较次数 and isnull(av_amt, 0) >= @aav_amt and isnull(con_amt, 0) >= @acon_amt and isnull(months, 0) >= isnull(@at_history, 0)*12 --2006/9/15 by sdw, 新增,会员历史条件 and isnull(remain02, 0) + isnull(remain04, 0) + isnull(remain05, 0) >= @abig_remain and isnull(remain02, 0) + isnull(remain04, 0) + isnull(remain05, 0) <= @aless_remain select @pagecount = count(*) from #mem_result_page select @male = count(1) from #mem_result_page where gender = 1 select @female = count(1) from #mem_result_page where isnull(gender,0) = 0 select @con_amt = sum(con_amt), @t_times = sum(t_times), @remain01 = sum(remain01),@remain02 = sum(remain02), @remain03 = sum(remain03), @remain04 = sum(remain04), @remain05 = sum(remain05), @total_fill_amt = sum(total_fill_amt), @total_salecard_amt = sum(total_salecard_amt) from #mem_result_page update a set a.pagecount = @pagecount from #mem_result_page a insert #mem_result_page_sum(con_amt,t_times,male,female,remain01,remain02,remain03,remain04,remain05,total_fill_amt,total_salecard_amt,pagecount) select @con_amt,@t_times,@male,@female,@remain01,@remain02, @remain03,@remain04,@remain05,@total_fill_amt,@total_salecard_amt,@pagecount if(@acctsummary = 1) begin insert #gcm03(gcc00c,gcc01c, gcc03i,gcc06f) select gcc00c,gcc01c, gcc03i,gcc06f from gcm03 a with(nolock), #mem_result_page b, gam26 z with(nolock) where a.gcc00c = b.compid and a.gcc01c = b.cardid and a.gcc00c = z.gaz02c and z.gaz01c = @compid insert #sum_gcm03(gcc03i, gcc06f) select gcc03i, sum(gcc06f) from #gcm03 group by gcc03i update a set a.gcc03iname = b.gsb03c, a.pagecount = @pagecount from #sum_gcm03 a,gsm02 b with(nolock) where a.gcc03i = b.gsb02c and b.gsb01c = 'O' and b.gsb00c = @compid select @sqlpage = 'select * from (select top '+ cast(3 * @pagesize as nvarchar(20))+'compid, memid, m_name, cardid, gender, mobile, tel, birthday, state, con_amt, lastdate, lastcall, t_times, t_compare, av_ytimes, av_times, av_wtimes, months, av_amt, start_date, first_date, comp_date, dur_years, dur_months, dur_weeks, remain01, remain02, remain03, remain04, remain05, total_fill_amt, memaddr, postcode, principle, total_salecard_amt,pagecount from #mem_result_page where idd > '+cast(@currentpageindex * @pagesize as nvarchar(20))+ ' union all '+ ' select '''', '''', '''', '''', 0, '''', '''', '''', '''', con_amt, '''', '''', t_times, 0, 0, female, 0, male, 0, '''', '''', '''', 0, 0, 0, remain01, remain02, remain03, remain04, remain05, total_fill_amt, '''', '''', '''', total_salecard_amt,pagecount from #mem_result_page_sum '+ ' union all '+ ' select ''lm'', '''', isnull(gcc03iname,'''') m_name, '''', 0, '''', '''', '''', '''', isnull(gcc06f,0) con_amt, '''', '''', 0, 0, 0, 0, 0, 0, 0, '''', '''', '''', 0, 0, 0, 0, 0, 0, 0, 0, 0, '''', '''', '''', 0,pagecount from #sum_gcm03 ) a '+ ' order by con_amt desc' exec sp_executesql @sqlpage end else begin select @sqlpage = 'select * from (select top '+ cast(3 * @pagesize as nvarchar(20))+' compid, memid, m_name, cardid, gender, mobile, tel, birthday, state, con_amt, lastdate, lastcall, t_times, t_compare, av_ytimes, av_times, av_wtimes, months, av_amt, start_date, first_date, comp_date, dur_years, dur_months, dur_weeks, remain01, remain02, remain03, remain04, remain05, total_fill_amt, memaddr, postcode, principle, total_salecard_amt,pagecount from #mem_result_page where idd > '+cast(@currentpageindex * @pagesize as nvarchar(20))+ ' union all '+ ' select '''', '''', '''', '''', 0, '''', '''', '''', '''', con_amt, '''', '''', t_times, 0, 0, female, 0, male, 0, '''', '''', '''', 0, 0, 0, remain01, remain02, remain03, remain04, remain05, total_fill_amt, '''', '''', '''', total_salecard_amt,pagecount from #mem_result_page_sum) a '+ 'order by con_amt desc' exec sp_executesql @sqlpage end end else begin insert #mem_result_page(idd, compid, memid, m_name, cardid, gender, mobile, tel, birthday, state, con_amt, lastdate, lastcall, t_times, t_compare, av_ytimes, av_times, av_wtimes, months, av_amt, start_date, first_date, comp_date, dur_years, dur_months, dur_weeks, remain01, remain02, remain03, remain04, remain05, total_fill_amt, memaddr, postcode, principle, total_salecard_amt,pagecount) select idd, compid, memid, m_name, cardid, gender, mobile, tel, birthday, state, con_amt, lastdate, lastcall, t_times, t_compare, av_ytimes, av_times, av_wtimes, months, av_amt, start_date, first_date, comp_date, dur_years, dur_months, dur_weeks, remain01, remain02, remain03, remain04, remain05, total_fill_amt, memaddr, postcode, principle, total_salecard_amt,pagecount from #mem_result where isnull(con_amt, 0) >= @acon_amt and isnull(lastdate, '19740313') >= @alastdate and isnull(t_compare, 0) >= @at_times --2006/9/15 by sdw, 修改,比较次数 and isnull(av_amt, 0) >= @aav_amt and isnull(con_amt, 0) >= @acon_amt and isnull(months, 0) >= isnull(@at_history, 0)*12 --2006/9/15 by sdw, 新增,会员历史条件 and isnull(remain02, 0) + isnull(remain04, 0) + isnull(remain05, 0) >= @abig_remain and isnull(remain02, 0) + isnull(remain04, 0) + isnull(remain05, 0) <= @aless_remain order by idd select @pagecount = count(*) from #mem_result_page select @male = count(1) from #mem_result_page where gender = 1 select @female = count(1) from #mem_result_page where isnull(gender,0) = 0 select @con_amt = sum(con_amt), @t_times = sum(t_times), @remain01 = sum(remain01),@remain02 = sum(remain02), @remain03 = sum(remain03), @remain04 = sum(remain04), @remain05 = sum(remain05), @total_fill_amt = sum(total_fill_amt), @total_salecard_amt = sum(total_salecard_amt) from #mem_result_page update a set a.pagecount = @pagecount from #mem_result_page a insert #mem_result_page_sum(con_amt,t_times,male,female,remain01,remain02,remain03,remain04,remain05,total_fill_amt,total_salecard_amt,pagecount) select @con_amt,@t_times,@male,@female,@remain01,@remain02, @remain03,@remain04,@remain05,@total_fill_amt,@total_salecard_amt,@pagecount if(@acctsummary = 1) begin insert #gcm03(gcc00c,gcc01c, gcc03i,gcc06f) select gcc00c,gcc01c, gcc03i,gcc06f from gcm03 a with(nolock), #mem_result_page b, gam26 z with(nolock) where a.gcc00c = b.compid and a.gcc01c = b.cardid and a.gcc00c = z.gaz02c and z.gaz01c = @compid insert #sum_gcm03(gcc03i, gcc06f) select gcc03i, sum(gcc06f) from #gcm03 group by gcc03i update a set a.gcc03iname = b.gsb03c, a.pagecount = @pagecount from #sum_gcm03 a,gsm02 b with(nolock) where a.gcc03i = b.gsb02c and b.gsb01c = 'O' and b.gsb00c = @compid select @sqlpage = 'select * from (select compid, memid, m_name, cardid, gender, mobile, tel, birthday, state, con_amt, lastdate, lastcall, t_times, t_compare, av_ytimes, av_times, av_wtimes, months, av_amt, start_date, first_date, comp_date, dur_years, dur_months, dur_weeks, remain01, remain02, remain03, remain04, remain05, total_fill_amt, memaddr, postcode, principle, total_salecard_amt,pagecount from #mem_result_page '+ ' union all '+ ' select '''', '''', '''', '''', 0, '''', '''', '''', '''', con_amt, '''', '''', t_times, 0, 0, female, 0, male, 0, '''', '''', '''', 0, 0, 0, remain01, remain02, remain03, remain04, remain05, total_fill_amt, '''', '''', '''', total_salecard_amt,pagecount from #mem_result_page_sum '+ ' union all '+ ' select ''lm'', '''', isnull(gcc03iname,'''') m_name, '''', 0, '''', '''', '''', '''', isnull(gcc06f,0) con_amt, '''', '''', 0, 0, 0, 0, 0, 0, 0, '''', '''', '''', 0, 0, 0, 0, 0, 0, 0, 0, 0, '''', '''', '''', 0,pagecount from #sum_gcm03 ) a '+ ' order by con_amt desc' exec sp_executesql @sqlpage end else begin select @sqlpage = 'select * from (select compid, memid, m_name, cardid, gender, mobile, tel, birthday, state, con_amt, lastdate, lastcall, t_times, t_compare, av_ytimes, av_times, av_wtimes, months, av_amt, start_date, first_date, comp_date, dur_years, dur_months, dur_weeks, remain01, remain02, remain03, remain04, remain05, total_fill_amt, memaddr, postcode, principle, total_salecard_amt,pagecount from #mem_result_page '+ ' union all '+ ' select '''', '''', '''', '''', 0, '''', '''', '''', '''', con_amt, '''', '''', t_times, 0, 0, female, 0, male, 0, '''', '''', '''', 0, 0, 0, remain01, remain02, remain03, remain04, remain05, total_fill_amt, '''', '''', '''', total_salecard_amt,pagecount from #mem_result_page_sum) a '+ 'order by con_amt desc' exec sp_executesql @sqlpage end end end else begin create table #tmp ( s varchar(10) ) declare @long_str_Tmp varchar(10), @short_str varchar(10), @split_str_length int, @split_str varchar(10), @Long_str varchar(200) set @split_str = ',' set @Long_str = @state SET @split_str_length = LEN(@split_str) IF CHARINDEX(@split_str, @Long_str) = 1 SET @long_str_Tmp = SUBSTRING(@Long_str, @split_str_length + 1, LEN(@Long_str)-@split_str_length) ELSE SET @long_str_Tmp = @Long_str IF CHARINDEX(REVERSE(@split_str), REVERSE(@long_str_Tmp))>1 SET @long_str_Tmp = @long_str_Tmp + @split_str ELSE SET @long_str_Tmp = @long_str_Tmp IF CHARINDEX(@split_str, @long_str_Tmp) = 0 insert into #tmp select @long_str_Tmp ELSE begin WHILE CHARINDEX(@split_str, @long_str_Tmp)>0 begin SET @short_str = SUBSTRING(@long_str_Tmp, 1, CHARINDEX(@split_str, @long_str_Tmp)-1) declare @long_str_Tmp_LEN INT, @split_str_Position_end int SET @long_str_Tmp_LEN = LEN(@long_str_Tmp) SET @split_str_Position_end = LEN(@short_str) + @split_str_length SET @long_str_Tmp = REVERSE(SUBSTRING(REVERSE(@long_str_Tmp), 1, @long_str_Tmp_LEN-@split_str_Position_end)) IF @short_str<>'' insert into #tmp select @short_str end end if(@pageall = 0) begin insert #mem_result_page(idd, compid, memid, m_name, cardid, gender, mobile, tel, birthday, state, con_amt, lastdate, lastcall, t_times, t_compare, av_ytimes, av_times, av_wtimes, months, av_amt, start_date, first_date, comp_date, dur_years, dur_months, dur_weeks, remain01, remain02, remain03, remain04, remain05, total_fill_amt, memaddr, postcode, principle, total_salecard_amt,pagecount) select idd, compid, memid, m_name, cardid, gender, mobile, tel, birthday, state, con_amt, lastdate, lastcall, t_times, t_compare, av_ytimes, av_times, av_wtimes, months, av_amt, start_date, first_date, comp_date, dur_years, dur_months, dur_weeks, remain01, remain02, remain03, remain04, remain05, total_fill_amt, memaddr, postcode, principle, total_salecard_amt,pagecount from #mem_result where isnull(con_amt, 0) >= @acon_amt and isnull(lastdate, '19740313') >= @alastdate and isnull(t_compare, 0) >= @at_times --2006/9/15 by sdw, 修改,比较次数 and isnull(av_amt, 0) >= @aav_amt and isnull(con_amt, 0) >= @acon_amt and isnull(months, 0) >= isnull(@at_history, 0)*12 --2006/9/15 by sdw, 新增,会员历史条件 and isnull(remain02, 0) + isnull(remain04, 0) + isnull(remain05, 0) >= @abig_remain and isnull(remain02, 0) + isnull(remain04, 0) + isnull(remain05, 0) <= @aless_remain and state in (select s from #tmp) select @pagecount = count(*) from #mem_result_page select @male = count(1) from #mem_result_page where gender = 1 select @female = count(1) from #mem_result_page where isnull(gender,0) = 0 select @con_amt = sum(con_amt), @t_times = sum(t_times), @remain01 = sum(remain01),@remain02 = sum(remain02), @remain03 = sum(remain03), @remain04 = sum(remain04), @remain05 = sum(remain05), @total_fill_amt = sum(total_fill_amt), @total_salecard_amt = sum(total_salecard_amt) from #mem_result_page update a set a.pagecount = @pagecount from #mem_result_page a insert #mem_result_page_sum(con_amt,t_times,male,female,remain01,remain02,remain03,remain04,remain05,total_fill_amt,total_salecard_amt,pagecount) select @con_amt,@t_times,@male,@female,@remain01,@remain02, @remain03,@remain04,@remain05,@total_fill_amt,@total_salecard_amt,@pagecount if(@acctsummary = 1) begin insert #gcm03(gcc00c,gcc01c, gcc03i,gcc06f) select gcc00c,gcc01c, gcc03i,gcc06f from gcm03 a with(nolock), #mem_result_page b, gam26 z with(nolock) where a.gcc00c = b.compid and a.gcc01c = b.cardid and a.gcc00c = z.gaz02c and z.gaz01c = @compid insert #sum_gcm03(gcc03i, gcc06f) select gcc03i, sum(gcc06f) from #gcm03 group by gcc03i update a set a.gcc03iname = b.gsb03c, a.pagecount = @pagecount from #sum_gcm03 a,gsm02 b with(nolock) where a.gcc03i = b.gsb02c and b.gsb01c = 'O' and b.gsb00c = @compid select @sqlpage = 'select * from (select top '+ cast(3 * @pagesize as nvarchar(20))+'compid, memid, m_name, cardid, gender, mobile, tel, birthday, state, con_amt, lastdate, lastcall, t_times, t_compare, av_ytimes, av_times, av_wtimes, months, av_amt, start_date, first_date, comp_date, dur_years, dur_months, dur_weeks, remain01, remain02, remain03, remain04, remain05, total_fill_amt, memaddr, postcode, principle, total_salecard_amt,pagecount from #mem_result_page where idd > '+cast(@currentpageindex * @pagesize as nvarchar(20))+ ' union all '+ ' select '''', '''', '''', '''', 0, '''', '''', '''', '''', con_amt, '''', '''', t_times, 0, 0, female, 0, male, 0, '''', '''', '''', 0, 0, 0, remain01, remain02, remain03, remain04, remain05, total_fill_amt, '''', '''', '''', total_salecard_amt,pagecount from #mem_result_page_sum '+ ' union all '+ ' select ''lm'', '''', isnull(gcc03iname,'''') m_name, '''', 0, '''', '''', '''', '''', isnull(gcc06f,0) con_amt, '''', '''', 0, 0, 0, 0, 0, 0, 0, '''', '''', '''', 0, 0, 0, 0, 0, 0, 0, 0, 0, '''', '''', '''', 0,pagecount from #sum_gcm03 ) a '+ ' order by con_amt desc' exec sp_executesql @sqlpage end else begin select @sqlpage = 'select * from (select top '+ cast(3 * @pagesize as nvarchar(20))+' compid, memid, m_name, cardid, gender, mobile, tel, birthday, state, con_amt, lastdate, lastcall, t_times, t_compare, av_ytimes, av_times, av_wtimes, months, av_amt, start_date, first_date, comp_date, dur_years, dur_months, dur_weeks, remain01, remain02, remain03, remain04, remain05, total_fill_amt, memaddr, postcode, principle, total_salecard_amt,pagecount from #mem_result_page where idd > '+cast(@currentpageindex * @pagesize as nvarchar(20))+ ' union all '+ ' select '''', '''', '''', '''', 0, '''', '''', '''', '''', con_amt, '''', '''', t_times, 0, 0, female, 0, male, 0, '''', '''', '''', 0, 0, 0, remain01, remain02, remain03, remain04, remain05, total_fill_amt, '''', '''', '''', total_salecard_amt,pagecount from #mem_result_page_sum) a '+ 'order by con_amt desc' exec sp_executesql @sqlpage end end else begin insert #mem_result_page(compid, memid, m_name, cardid, gender, mobile, tel, birthday, state, con_amt, lastdate, lastcall, t_times, t_compare, av_ytimes, av_times, av_wtimes, months, av_amt, start_date, first_date, comp_date, dur_years, dur_months, dur_weeks, remain01, remain02, remain03, remain04, remain05, total_fill_amt, memaddr, postcode, principle, total_salecard_amt,pagecount) select compid, memid, m_name, cardid, gender, mobile, tel, birthday, state, con_amt, lastdate, lastcall, t_times, t_compare, av_ytimes, av_times, av_wtimes, months, av_amt, start_date, first_date, comp_date, dur_years, dur_months, dur_weeks, remain01, remain02, remain03, remain04, remain05, total_fill_amt, memaddr, postcode, principle, total_salecard_amt,pagecount from #mem_result where isnull(con_amt, 0) >= @acon_amt and isnull(lastdate, '19740313') >= @alastdate and isnull(t_compare, 0) >= @at_times --2006/9/15 by sdw, 修改,比较次数 and isnull(av_amt, 0) >= @aav_amt and isnull(con_amt, 0) >= @acon_amt and isnull(months, 0) >= isnull(@at_history, 0)*12 --2006/9/15 by sdw, 新增,会员历史条件 and isnull(remain02, 0) + isnull(remain04, 0) + isnull(remain05, 0) >= @abig_remain and isnull(remain02, 0) + isnull(remain04, 0) + isnull(remain05, 0) <= @aless_remain and state in (select s from #tmp) order by idd select @pagecount = count(*) from #mem_result_page select @male = count(1) from #mem_result_page where gender = 1 select @female = count(1) from #mem_result_page where isnull(gender,0) = 0 select @con_amt = sum(con_amt), @t_times = sum(t_times), @remain01 = sum(remain01),@remain02 = sum(remain02), @remain03 = sum(remain03), @remain04 = sum(remain04), @remain05 = sum(remain05), @total_fill_amt = sum(total_fill_amt), @total_salecard_amt = sum(total_salecard_amt) from #mem_result_page update a set a.pagecount = @pagecount from #mem_result_page a insert #mem_result_page_sum(con_amt,t_times,male,female,remain01,remain02,remain03,remain04,remain05,total_fill_amt,total_salecard_amt,pagecount) select @con_amt,@t_times,@male,@female,@remain01,@remain02, @remain03,@remain04,@remain05,@total_fill_amt,@total_salecard_amt,@pagecount if(@acctsummary = 1) begin insert #gcm03(gcc00c,gcc01c, gcc03i,gcc06f) select gcc00c,gcc01c, gcc03i,gcc06f from gcm03 a with(nolock), #mem_result_page b, gam26 z with(nolock) where a.gcc00c = b.compid and a.gcc01c = b.cardid and a.gcc00c = z.gaz02c and z.gaz01c = @compid insert #sum_gcm03(gcc03i, gcc06f) select gcc03i, sum(gcc06f) from #gcm03 group by gcc03i update a set a.gcc03iname = b.gsb03c, a.pagecount = @pagecount from #sum_gcm03 a,gsm02 b with(nolock) where a.gcc03i = b.gsb02c and b.gsb01c = 'O' and b.gsb00c = @compid select @sqlpage = 'select * from (select compid, memid, m_name, cardid, gender, mobile, tel, birthday, state, con_amt, lastdate, lastcall, t_times, t_compare, av_ytimes, av_times, av_wtimes, months, av_amt, start_date, first_date, comp_date, dur_years, dur_months, dur_weeks, remain01, remain02, remain03, remain04, remain05, total_fill_amt, memaddr, postcode, principle, total_salecard_amt,pagecount from #mem_result_page '+ ' union all '+ ' select '''', '''', '''', '''', 0, '''', '''', '''', '''', con_amt, '''', '''', t_times, 0, 0, female, 0, male, 0, '''', '''', '''', 0, 0, 0, remain01, remain02, remain03, remain04, remain05, total_fill_amt, '''', '''', '''', total_salecard_amt,pagecount from #mem_result_page_sum '+ ' union all '+ ' select ''lm'', '''', isnull(gcc03iname,'''') m_name, '''', 0, '''', '''', '''', '''', isnull(gcc06f,0) con_amt, '''', '''', 0, 0, 0, 0, 0, 0, 0, '''', '''', '''', 0, 0, 0, 0, 0, 0, 0, 0, 0, '''', '''', '''', 0,pagecount from #sum_gcm03 ) a '+ ' order by con_amt desc' exec sp_executesql @sqlpage end else begin select @sqlpage = 'select * from (select compid, memid, m_name, cardid, gender, mobile, tel, birthday, state, con_amt, lastdate, lastcall, t_times, t_compare, av_ytimes, av_times, av_wtimes, months, av_amt, start_date, first_date, comp_date, dur_years, dur_months, dur_weeks, remain01, remain02, remain03, remain04, remain05, total_fill_amt, memaddr, postcode, principle, total_salecard_amt,pagecount from #mem_result_page '+ ' union all '+ ' select '''', '''', '''', '''', 0, '''', '''', '''', '''', con_amt, '''', '''', t_times, 0, 0, female, 0, male, 0, '''', '''', '''', 0, 0, 0, remain01, remain02, remain03, remain04, remain05, total_fill_amt, '''', '''', '''', total_salecard_amt,pagecount from #mem_result_page_sum) a '+ 'order by con_amt desc' exec sp_executesql @sqlpage end end drop table #tmp end drop table #gcm03 drop table #sum_gcm03 drop table #mem_result_page_sum drop table #mem_result_page drop table #mem_result drop table #sum_tmp end GO /****** Object: StoredProcedure [dbo].[upg_day_summary] Script Date: 06/12/2017 06:19:56 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_day_summary] ( @compid varchar(10), -- 公司别 @fromday varchar(10), -- 开始日期 modify by limin 2014/12/01 @today varchar(10) -- 结束日期 ) as begin declare @type_01 varchar(10) declare @type_02 varchar(10) declare @type_03 varchar(10) declare @type_04 varchar(10) declare @type_05 varchar(10) declare @type_06 varchar(10) declare @type_07 varchar(10) declare @type_08 varchar(10) declare @type_09 varchar(10) declare @type_10 varchar(10) declare @type_11 varchar(10) declare @type_12 varchar(10) declare @type_13 varchar(10) declare @type_14 varchar(10) declare @type_15 varchar(10) declare @type_var varchar(10) declare @loop_i int declare @payment_0 varchar(10) declare @payment_3 varchar(10) declare @payment_$ varchar(10) declare @payment_u1 varchar(10), @payment_u2 varchar(10), @payment_u3 varchar(10), @payment_u4 varchar(10), @payment_u5 varchar(10) exec upg_isrevenue_payment @compid, @payment_0 output, @payment_3 output, @payment_$ output, @payment_u1 output, @payment_u2 output, @payment_u3 output, @payment_u4 output, @payment_u5 output declare cur_get_gsm02 cursor for select gsb02c from gsm02 with(nolock) where gsb01c = 'AJ' and gsb00c = @compid order by gsb02c select @loop_i = 1 open cur_get_gsm02 fetch cur_get_gsm02 into @type_var while @@fetch_status = 0 begin if @loop_i = 1 select @type_01 = @type_var else if @loop_i = 2 select @type_02 = @type_var else if @loop_i = 3 select @type_03 = @type_var else if @loop_i = 4 select @type_04 = @type_var else if @loop_i = 5 select @type_05 = @type_var else if @loop_i = 6 select @type_06 = @type_var else if @loop_i = 7 select @type_07 = @type_var else if @loop_i = 8 select @type_08 = @type_var else if @loop_i = 9 select @type_09 = @type_var else if @loop_i = 10 select @type_10 = @type_var else if @loop_i = 11 select @type_11 = @type_var else if @loop_i = 12 select @type_12 = @type_var else if @loop_i = 13 select @type_13 = @type_var else if @loop_i = 14 select @type_14 = @type_var else if @loop_i = 15 select @type_15 = @type_var select @loop_i = @loop_i + 1 fetch cur_get_gsm02 into @type_var end close cur_get_gsm02 deallocate cur_get_gsm02 -- 作为结果的临时表 create table #day_result ( idd int identity not null, -- 序号 billid varchar(20) not null, -- 单据编号 srv_type varchar(10) null, -- 新客或老客 mei_01 float null, -- 美发项目金额 mei_02 float null, -- 美发项目 mei_03 float null, -- 美发项目 mei_04 float null, -- 美发项目 mei_05 float null, -- 美发项目 mei_06 float null, -- 美发项目 mei_07 float null, -- 美发项目 mei_08 float null, -- 美发项目 mei_09 float null, -- 美发项目 mei_10 float null, -- 美发项目 mei_11 float null, -- 美发项目 mei_12 float null, -- 美发项目 mei_13 float null, -- 美发项目 mei_14 float null, -- 美发项目 mei_15 float null, -- 美发项目 amt_01 float null, -- 美发项目金额 amt_02 float null, -- 美发项目 amt_03 float null, -- 美发项目 amt_04 float null, -- 美发项目 amt_05 float null, -- 美发项目 amt_06 float null, -- 美发项目 amt_07 float null, -- 美发项目 amt_08 float null, -- 美发项目 amt_09 float null, -- 美发项目 amt_10 float null, -- 美发项目 amt_11 float null, -- 美发项目 amt_12 float null, -- 美发项目 amt_13 float null, -- 美发项目 amt_14 float null, -- 美发项目 amt_15 float null, -- 美发项目 cardno varchar(20) null, -- 卡号 card_rawamt float null, -- 卡销售金额 card_amt float null, -- 卡销售金额 otherraw float null, -- 其他金额 discount float null, -- 折扣金额 pay_discount float null, -- 支付折扣 total_rawamt float null, -- 折扣前的总金额 total_amt float null, -- 折扣后的总金额 total_tamt float null, -- 考虑支付折扣后的总金额 cash float null, -- 现金 credit float null, -- 银行卡 zfb float null, -- 支付宝 wx float null, -- 微信支付 use_card float null, -- 会员卡 otheramt float null, -- 实际其他金额 assistant varchar(128) null, -- 助手 staff varchar(128) null, -- 发型师 other_ass varchar(128) null, -- 其他人 mei_std_amt float null, -- 统计类金额 mei_amt float null, -- oldcount float null, -- 老客数量--add by lyj for 美宫足浴2008-03-12 取的是gga14f中的数量 newcount float null, -- 新客数量 primary key(idd) ) -- 指定的单据 create table #st_bills_fix ( idd int identity not null, billid varchar(20) not null, -- 单号 primary key(idd) ) -- 当天所有的纪录 insert #day_result(billid, cardno, srv_type, oldcount, newcount) select gga01c, gga09c, case gga13f when 1 then '1' else '2' end, case gga13f when 1 then gga14f else 0 end, case gga13f when 0 then gga14f else 0 end from ggm01 with (nolock) where gga00c = @compid and gga80d between @fromday and @today create table #tbl_bill ( idd int identity not null, billid varchar(20) not null, -- 单号 std_amt float null, -- 项目/产品标准金额 amt float null, -- 项目/产品实际金额 paymode varchar(10) null, -- 支付方式 staff varchar(128) null, -- 大工/中/小 billtype varchar(20) null, -- 单据类别 primary key(idd) ) create table #tbl_prjs ( idd int identity not null, billid varchar(20) not null, -- 单号 prj_type varchar(20) null, -- 项目统计类别 std_amt float null, -- 标准金额 amt float null, -- 实际金额 primary key(idd) ) -- 服务项目的金额 insert #tbl_prjs(billid, prj_type, std_amt, amt) select ggb01c, gda13c, sum(ggb08f * isnull(ggb05f, 1)), sum(ggb11f) from ggm02 a with (nolock), gdm01 b with(nolock), #day_result c where a.ggb00c = @compid --add by lm 20150721 and a.ggb01c = c.billid and a.ggb00c = b.gda00c and a.ggb03c = b.gda01c and isnull(gda13c, '') <> '' group by ggb01c, gda13c order by gda13c -- 把归类的项目金额放入结果的临时表中 update a set a.mei_01 = isnull(b.std_amt, 0), a.amt_01 = isnull(b.amt, 0) from #day_result a, #tbl_prjs b where a.billid = b.billid and b.prj_type = @type_01 update a set a.mei_02 = b.std_amt, a.amt_02 = isnull(b.amt, 0) from #day_result a, #tbl_prjs b where a.billid = b.billid and b.prj_type = @type_02 update a set a.mei_03 = b.std_amt, a.amt_03 = isnull(b.amt, 0) from #day_result a, #tbl_prjs b where a.billid = b.billid and b.prj_type = @type_03 update a set a.mei_04 = b.std_amt, a.amt_04 = isnull(b.amt, 0) from #day_result a, #tbl_prjs b where a.billid = b.billid and b.prj_type = @type_04 update a set a.mei_05 = b.std_amt, a.amt_05 = isnull(b.amt, 0) from #day_result a, #tbl_prjs b where a.billid = b.billid and b.prj_type = @type_05 update a set a.mei_06 = b.std_amt, a.amt_06 = isnull(b.amt, 0) from #day_result a, #tbl_prjs b where a.billid = b.billid and b.prj_type = @type_06 update a set a.mei_07 = b.std_amt, a.amt_07 = isnull(b.amt, 0) from #day_result a, #tbl_prjs b where a.billid = b.billid and b.prj_type = @type_07 update a set a.mei_08 = b.std_amt, a.amt_08 = isnull(b.amt, 0) from #day_result a, #tbl_prjs b where a.billid = b.billid and b.prj_type = @type_08 update a set a.mei_09 = b.std_amt, a.amt_09 = isnull(b.amt, 0) from #day_result a, #tbl_prjs b where a.billid = b.billid and b.prj_type = @type_09 update a set a.mei_10 = b.std_amt, a.amt_10 = isnull(b.amt, 0) from #day_result a, #tbl_prjs b where a.billid = b.billid and b.prj_type = @type_10 update a set a.mei_11 = b.std_amt, a.amt_11 = isnull(b.amt, 0) from #day_result a, #tbl_prjs b where a.billid = b.billid and b.prj_type = @type_11 update a set a.mei_12 = b.std_amt, a.amt_12 = isnull(b.amt, 0) from #day_result a, #tbl_prjs b where a.billid = b.billid and b.prj_type = @type_12 update a set a.mei_13 = b.std_amt, a.amt_13 = isnull(b.amt, 0) from #day_result a, #tbl_prjs b where a.billid = b.billid and b.prj_type = @type_13 update a set a.mei_14 = b.std_amt, a.amt_14 = isnull(b.amt, 0) from #day_result a, #tbl_prjs b where a.billid = b.billid and b.prj_type = @type_14 update a set a.mei_15 = b.std_amt, a.amt_15 = isnull(b.amt, 0) from #day_result a, #tbl_prjs b where a.billid = b.billid and b.prj_type = @type_15 delete #tbl_bill -- 所有归类的统计类别的金额 insert #tbl_bill(billid, std_amt, amt) select billid, sum(std_amt), sum(amt) from #tbl_prjs group by billid update a set a.mei_std_amt = b.std_amt, a.mei_amt = b.amt from #day_result a, #tbl_bill b where a.billid = b.billid -- 每一张单据的对应的卡的销售金额,不考虑支付的折扣 -- 也不考虑欠款的部分, 认为欠款只是支付方式的不同 -- 统计今天所有卡的销售 create table #tbl_cards ( idd int identity not null, cardno varchar(20) not null, rawamt float null, amt float null, primary key(idd) ) create table #tbl_fill_card ( idd int identity not null, cardno varchar(20) null, amt float null, rawamt float null, flag int null, -- 1: fill 0: primary key(idd) ) create table #tbl_fill_card_pay --add by lm 2015-05-30 ( idd int identity not null, compid varchar(20) not null, billid varchar(20) not null, cardno varchar(20) null, card_rawamt float null, total_tamt float null, cash float null, credit float null, zfb float null, wx float null, -- 微信支付 primary key(idd) ) create table #tbl_cards_pay --add by lm 2015-05-30 ( idd int identity not null, compid varchar(20) not null, billid varchar(20) not null, cardno varchar(20) not null, card_rawamt float null, total_tamt float null, cash float null, credit float null, zfb float null, wx float null, -- 微信支付 primary key(idd) ) -- 当天卡的销售 insert #tbl_cards(cardno, rawamt, amt) select gnb03c, sum(gsc05f), sum(gsc05f) from gnm02 b with (nolock), gnm01 a with (nolock), gsm03 c with (nolock) where b.gnb00c = a.gna00c and b.gnb01c = a.gna01c and a.gna00c = @compid --add by lm 20150721 and a.gna80d between @fromday and @today and a.gna00c = c.gsc00c and a.gna01c = c.gsc01c and c.gsc02c = 'gn' and c.gsc04c in ('1', '6', 'ZFB', 'WX', @payment_0, @payment_3, @payment_$, @payment_u1, @payment_u2, @payment_u3, @payment_u4, @payment_u5) group by gnb03c -- 充值 insert #tbl_fill_card(cardno, amt, flag) select gcl03c, sum(gsc05f), 1 from gcm10 a with (nolock), gsm03 b with (nolock) where gcl80d between @fromday and @today and a.gcl00c = b.gsc00c and a.gcl00c = @compid --add by lm 20150721 and a.gcl01c = b.gsc01c and b.gsc02c = 'gz' and b.gsc04c in ('1', '6', 'ZFB', 'WX', @payment_0, @payment_3, @payment_$, @payment_u1, @payment_u2, @payment_u3, @payment_u4, @payment_u5) group by gcl03c ----------充值和卖卡--- begin----------- add by lm 2015-05-30 --卖卡 insert #tbl_cards_pay(compid, billid, cardno, card_rawamt, total_tamt) select gna00c, gna01c, gnb03c, gnb07f, gnb07f from gnm02 b with (nolock), gnm01 a with (nolock) where b.gnb00c = a.gna00c and b.gnb01c = a.gna01c and a.gna00c = @compid --add by lm 20150721 and a.gna80d between @fromday and @today update a set a.cash = b.gsc05f from #tbl_cards_pay a, gsm03 b with(nolock) where b.gsc04c = '1' and a.compid = b.gsc00c and a.billid = b.gsc01c and b.gsc02c = 'gn' update a set a.credit =b.gsc05f from #tbl_cards_pay a, gsm03 b with(nolock) where b.gsc04c='6' and a.compid = b.gsc00c and a.billid = b.gsc01c and b.gsc02c = 'gn' update a set a.zfb = b.gsc05f from #tbl_cards_pay a, gsm03 b with(nolock) where b.gsc04c = 'ZFB' and a.compid = b.gsc00c and a.billid = b.gsc01c and b.gsc02c = 'gn' update a set a.wx = b.gsc05f from #tbl_cards_pay a, gsm03 b with(nolock) where b.gsc04c = 'WX' and a.compid = b.gsc00c and a.billid = b.gsc01c and b.gsc02c = 'gn' -- 充值 insert #tbl_fill_card_pay(compid, billid, cardno, card_rawamt, total_tamt) select gcl00c, gcl01c, gcl03c, gcl07f, gcl07f from gcm10 a with (nolock) where gcl80d between @fromday and @today and a.gcl00c = @compid --add by lm 20150721 update a set a.cash = b.gsc05f from #tbl_fill_card_pay a, gsm03 b with(nolock) where b.gsc04c = '1' and a.compid = b.gsc00c and a.billid = b.gsc01c and b.gsc02c = 'gz' update a set a.credit = b.gsc05f from #tbl_fill_card_pay a, gsm03 b with(nolock) where b.gsc04c = '6' and a.compid = b.gsc00c and a.billid = b.gsc01c and b.gsc02c = 'gz' update a set a.zfb = b.gsc05f from #tbl_fill_card_pay a, gsm03 b with(nolock) where b.gsc04c = 'ZFB' and a.compid = b.gsc00c and a.billid = b.gsc01c and b.gsc02c = 'gz' update a set a.wx = b.gsc05f from #tbl_fill_card_pay a, gsm03 b with(nolock) where b.gsc04c = 'WX' and a.compid = b.gsc00c and a.billid = b.gsc01c and b.gsc02c = 'gz' -------------------------end------------- -- 把充款加到卡的销售中 update a set a.rawamt = a.rawamt + b.amt, a.amt = a.amt + b.amt from #tbl_cards a, #tbl_fill_card b where a.cardno = b.cardno and b.flag = 1 -- 处理当天没有销售,但有充款的卡 delete b from #tbl_cards a, #tbl_fill_card b where a.cardno = b.cardno insert #tbl_cards(cardno, rawamt, amt) select cardno, amt, amt from #tbl_fill_card where flag = 1 insert #tbl_cards(cardno, rawamt, amt) select cardno, 0 - amt, 0 - amt from #tbl_fill_card where flag = 0 -- 临时借这个临时表用用 delete #tbl_fill_card insert #tbl_fill_card(cardno, amt, rawamt) select cardno, sum(amt), sum(rawamt) from #tbl_cards group by cardno drop table #tbl_cards -- 当天单据与卡号 create table #tbl_bill_card ( idd int identity not null, billid varchar(20) null, cardno varchar(20) null, amt float null, rawamt float null, primary key(idd) ) insert #tbl_bill_card(billid, cardno) select min(billid), cardno from #day_result group by cardno update a set a.amt = b.amt, a.rawamt = b.rawamt from #tbl_bill_card a, #tbl_fill_card b where a.cardno = b.cardno update a set a.card_amt = b.amt, a.card_rawamt = b.rawamt from #day_result a, #tbl_bill_card b where a.billid = b.billid -- 处理当天没有收银消费,但有卖卡 add by lm 2015-05-30 delete b from #day_result a, #tbl_cards_pay b where a.cardno = b.cardno -- 处理当天没有收银消费,但有充款的卡 add by lm 2015-05-30 delete b from #day_result a, #tbl_fill_card_pay b where a.cardno = b.cardno drop table #tbl_fill_card drop table #tbl_bill_card -- 计算卡销售金额,到此结束 -- 把服务与产品销售的总金额计算出来 delete #tbl_bill insert #tbl_bill(billid, std_amt, amt) select ggb01c, sum(ggb08f * isnull(ggb05f, 1)), sum(ggb11f) from ggm02 a with (nolock), #day_result c where a.ggb00c = @compid and a.ggb01c = c.billid group by ggb01c update a set a.total_rawamt = isnull(b.std_amt, 0), a.total_amt = isnull(b.amt, 0) from #day_result a, #tbl_bill b where a.billid = b.billid delete #tbl_bill -- 加入产品销售的金额 insert #tbl_bill(billid, std_amt, amt) select ggc01c, sum(ggc08f * isnull(ggc05f, 1)), sum(ggc11f) from ggm03 a with (nolock), #day_result c where a.ggc00c = @compid and a.ggc01c = c.billid group by ggc01c update a set a.total_rawamt = isnull(a.total_rawamt, 0) + isnull(b.std_amt, 0), a.total_amt = isnull(a.total_amt, 0) + isnull(b.amt, 0) from #day_result a, #tbl_bill b where a.billid = b.billid -- 加入卡的销售金额, 从而得到未折扣前的总金额,折扣前的总金额 update #day_result set total_rawamt = isnull(total_rawamt, 0) + isnull(card_rawamt, 0), total_amt = isnull(total_amt, 0) + isnull(card_amt, 0) -- 其他金额折扣前 = 非统计类 + 产品销售(不应该包括卡的金额) update #day_result set otherraw = isnull(total_rawamt, 0) - isnull(card_rawamt, 0) - isnull(mei_std_amt, 0) -- 折扣 = 项目折扣 + 支付折扣 -- 支付折扣中暂时不能考虑卡销售的折扣 create table #tbl_pay_discount ( idd int identity not null, billid varchar(20) null, amt float null, primary key(idd) ) insert #tbl_pay_discount(billid, amt) select gsc01c, sum(b.gsc05f) from #day_result a, gsm03 b with (nolock) where a.billid = b.gsc01c and b.gsc00c = @compid and b.gsc02c = 'gx' and (gsc04c = '3' or gsc04c = '7' or gsc04c = '8') group by gsc01c update a set a.pay_discount = b.amt from #day_result a, #tbl_pay_discount b where a.billid = b.billid drop table #tbl_pay_discount update #day_result set discount = 0 - (isnull(total_rawamt, 0) - isnull(total_amt, 0) + isnull(pay_discount, 0)) -- 实际的金额 update #day_result set total_tamt = isnull(total_amt, 0) - isnull(pay_discount, 0) -- 付款方式 delete #tbl_bill -- 这些单据在收银中的付款 insert #tbl_bill(billid, paymode, amt, billtype) select billid, gsc04c, sum(gsc05f), a.gsc02c from gsm03 a with (nolock), #day_result b where a.gsc01c = b.billid and a.gsc00c = @compid and a.gsc02c = 'gx' group by billid, gsc04c, gsc02c update a set a.cash = b.amt from #day_result a, #tbl_bill b where a.billid = b.billid and b.paymode = '1' update a set a.credit = b.amt from #day_result a, #tbl_bill b where a.billid = b.billid and b.paymode = '6' update a set a.zfb = b.amt from #day_result a, #tbl_bill b where a.billid = b.billid and b.paymode = 'ZFB' update a set a.wx = b.amt from #day_result a, #tbl_bill b where a.billid = b.billid and b.paymode = 'WX' update a set a.use_card = b.amt from #day_result a, #tbl_bill b, gsm14 c with(nolock) where a.billid = b.billid and b.paymode = c.gsn01c and isnull(c.gsn02c, '') <> '' and isnull(c.gsn02c, '') <> '999' -- 对应卡销售,卡充值的现金,银行卡 delete #tbl_bill -- 卡销售的支付 -- 注意billid作为cardno用 -- 这一天所有的卡销售单据统计出来 -- 如果一张单据上有多个卡,都算作第一张卡的卡销售金额 create table #tbl_card_sale ( billid varchar(20) not null, cardno varchar(20) not null, billtype varchar(10) null ) -- 收银单 create table #tbl_ggm01 ( billid varchar(20) not null, cardno varchar(20) not null ) -- 当天销售的卡 insert #tbl_card_sale(billid, cardno, billtype) select gnb01c, min(gnb03c), 'gn' from gnm02 b with (nolock), gnm01 a with (nolock) where b.gnb00c = a.gna00c and b.gnb01c = a.gna01c and a.gna00c = @compid and a.gna80d between @fromday and @today group by gnb01c -- 当天充值的卡 insert #tbl_card_sale(billid, cardno, billtype) select gcl01c, gcl03c, 'gz' from gcm10 a with (nolock) where a.gcl00c = @compid and a.gcl80d between @fromday and @today -- 当天一个卡的第一个收银单 insert #tbl_ggm01(billid, cardno) select min(billid), cardno from #day_result group by cardno insert #tbl_bill(billid, paymode, amt) select b.cardno, gsc04c, sum(gsc05f) from gsm03 a with (nolock), #day_result b, #tbl_card_sale c, #tbl_ggm01 d where a.gsc01c = c.billid and a.gsc00c = @compid and a.gsc02c = c.billtype and c.cardno = b.cardno and b.billid = d.billid group by b.cardno, gsc04c drop table #tbl_card_sale update a set a.cash = isnull(a.cash, 0) + isnull(b.amt, 0) from #day_result a, #tbl_bill b, #tbl_ggm01 c where a.cardno = b.billid and b.paymode = '1' and a.billid = c.billid and a.cardno = c.cardno update a set a.credit = isnull(a.credit, 0) + isnull(b.amt, 0) from #day_result a, #tbl_bill b, #tbl_ggm01 c where a.cardno = b.billid and b.paymode = '6' and a.billid = c.billid and a.cardno = c.cardno update a set a.zfb = isnull(a.zfb, 0) + isnull(b.amt, 0) from #day_result a, #tbl_bill b, #tbl_ggm01 c where a.cardno = b.billid and b.paymode = 'ZFB' and a.billid = c.billid and a.cardno = c.cardno update a set a.wx = isnull(a.wx, 0) + isnull(b.amt, 0) from #day_result a, #tbl_bill b, #tbl_ggm01 c where a.cardno = b.billid and b.paymode = 'WX' and a.billid = c.billid and a.cardno = c.cardno update a set a.use_card = isnull(a.use_card, 0) + isnull(b.amt, 0) from #day_result a, #tbl_bill b, #tbl_ggm01 c, gsm14 d with(nolock) where a.cardno = b.billid and b.paymode = d.gsn01c and isnull(d.gsn02c, '') <> '' and isnull(d.gsn02c, '') <> '999' and a.billid = c.billid and a.cardno = c.cardno drop table #tbl_ggm01 -- 充值的支付方式 delete #tbl_bill -- 其他付款方式 update #day_result set otheramt = total_tamt - (isnull(cash, 0) + isnull(credit, 0) + isnull(use_card, 0) + isnull(zfb, 0) + isnull(wx, 0)) -- 服务人员 declare @billid varchar(20) declare @billid_old varchar(20) declare @staff_group varchar(128) declare @staff varchar(128) select @billid = '', @billid_old = '', @staff = '', @staff_group = '' -- 大工 declare cur_each_bill_staff cursor for select distinct billid, ggb12c from #day_result a, ggm02 b with (nolock) where a.billid = b.ggb01c and b.ggb00c = @compid and isnull(ggb12c, '') <> '' order by billid, ggb12c open cur_each_bill_staff fetch cur_each_bill_staff into @billid, @staff while @@fetch_status = 0 begin if @billid = @billid_old begin -- 该单据还有服务人员 if isnull(@staff, '') <> '' begin select @staff_group = @staff_group + ',' + isnull(@staff, '') end end else begin -- 新的单据 if isnull(@staff_group, '') <> '' begin -- 更新到结果表中 update #day_result set staff = @staff_group where billid = @billid_old select @billid_old = @billid end select @staff_group = isnull(@staff, '') end select @billid_old = @billid fetch cur_each_bill_staff into @billid, @staff end close cur_each_bill_staff deallocate cur_each_bill_staff if isnull(@staff_group, '') <> '' begin -- 更新到结果表中 update #day_result set staff = @staff_group where billid = @billid end -- 中工 select @billid = '', @billid_old = '', @staff = '', @staff_group = '' declare cur_each_bill_staff_2 cursor for select distinct billid, ggb13c from #day_result a, ggm02 b with (nolock) where a.billid = b.ggb01c and isnull(ggb13c, '') <> '' and b.ggb00c = @compid open cur_each_bill_staff_2 fetch cur_each_bill_staff_2 into @billid, @staff while @@fetch_status = 0 begin if @billid = @billid_old begin -- 该单据还有服务人员 if isnull(@staff, '') <> '' begin select @staff_group = @staff_group + ',' + isnull(@staff, '') end end else begin -- 新的单据 if isnull(@staff_group, '') <> '' begin -- 更新到结果表中 update #day_result set assistant = @staff_group where billid = @billid_old select @billid_old = @billid end select @staff_group = isnull(@staff, '') end select @billid_old = @billid fetch cur_each_bill_staff_2 into @billid, @staff end close cur_each_bill_staff_2 deallocate cur_each_bill_staff_2 if isnull(@staff_group, '') <> '' begin -- 更新到结果表中 update #day_result set assistant = @staff_group where billid = @billid end -- 小工 select @billid = '', @billid_old = '', @staff = '', @staff_group = '' declare cur_each_bill_staff_3 cursor for select distinct billid, ggb14c from #day_result a, ggm02 b with (nolock) where a.billid = b.ggb01c and isnull(ggb14c, '') <> '' and b.ggb00c = @compid open cur_each_bill_staff_3 fetch cur_each_bill_staff_3 into @billid, @staff while @@fetch_status = 0 begin if @billid = @billid_old begin -- 该单据还有服务人员 if isnull(@staff, '') <> '' begin select @staff_group = @staff_group + ',' + isnull(@staff, '') end end else begin -- 新的单据 if isnull(@staff_group, '') <> '' begin -- 更新到结果表中 update #day_result set other_ass = @staff_group where billid = @billid_old select @billid_old = @billid end select @staff_group = isnull(@staff, '') end select @billid_old = @billid fetch cur_each_bill_staff_3 into @billid, @staff end close cur_each_bill_staff_3 deallocate cur_each_bill_staff_3 if isnull(@staff_group, '') <> '' begin -- 更新到结果表中 update #day_result set other_ass = @staff_group where billid = @billid end ----begin 插入到结果表中-------- --当天卖卡 insert #day_result(billid, srv_type, cardno, card_rawamt, total_tamt, cash, credit, zfb, wx) select billid, '1', cardno, card_rawamt, total_tamt, cash, credit, zfb, wx from #tbl_cards_pay --当天卡充值 insert #day_result(billid, srv_type, cardno, card_rawamt, total_tamt, cash, credit, zfb, wx) select billid, '1', cardno, card_rawamt, total_tamt, cash, credit, zfb, wx from #tbl_fill_card_pay drop table #tbl_cards_pay drop table #tbl_fill_card_pay ----end ------------------------ select isnull(idd,'') idd, isnull(srv_type,'') srv_type, isnull(cardno,'') cardno, isnull(mei_01,0) mei_01, isnull(mei_02,0) mei_02, isnull(mei_03,0) mei_03, isnull(mei_04,0) mei_04, isnull(mei_05,0) mei_05, isnull(mei_06,0) mei_06, isnull(mei_07,0) mei_07, isnull(mei_08,0) mei_08, isnull(mei_09,0) mei_09, isnull(mei_10,0) mei_10, isnull(mei_11,0) mei_11, isnull(mei_12,0) mei_12, isnull(mei_13,0) mei_13, isnull(mei_14,0) mei_14, isnull(mei_15,0) mei_15, isnull(amt_01,0) amt_01, isnull(amt_02,0) amt_02, isnull(amt_03,0) amt_03, isnull(amt_04,0) amt_04, isnull(amt_05,0) amt_05, isnull(amt_06,0) amt_06, isnull(amt_07,0) amt_07, isnull(amt_08,0) amt_08, isnull(amt_09,0) amt_09, isnull(amt_10,0) amt_10, isnull(amt_11,0) amt_11, isnull(amt_12,0) amt_12, isnull(amt_13,0) amt_13, isnull(amt_14,0) amt_14, isnull(amt_15,0) amt_15, cardamt = isnull(card_rawamt,0), isnull(card_amt,0) card_amt, isnull(otherraw,0) otherraw, isnull(discount,0) discount, isnull(pay_discount,0) pay_discount, isnull(total_rawamt,0) total_rawamt, isnull(total_amt,0) total_amt, amt = isnull(total_tamt,0), isnull(cash,0) cash, isnull(use_card,0) use_card, isnull(otheramt,0) otheramt, isnull(credit,'') credit, isnull(zfb,'') zfb, isnull(wx,'') wx, isnull(assistant,'') assistant, isnull(staff,'') staff, isnull(other_ass,'') other_ass, isnull(mei_std_amt, 0) mei_std_amt, isnull(mei_amt, 0) mei_amt, isnull(oldcount, 0) oldcount, isnull(newcount, 0) newcount from #day_result drop table #day_result drop table #tbl_bill end GO /****** Object: StoredProcedure [dbo].[upg_cal_work_detail_by_kind] Script Date: 06/12/2017 06:19:36 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_cal_work_detail_by_kind] ( @spid int, @compid varchar(10), -- 公司别 @fromdate varchar(10), -- 开始日期 @todate varchar(10), -- 截至日期 @fromempl varchar(20), -- 查询开始人员 @toempl varchar(20) -- 查询截至人员 ) as begin declare @staff_id varchar(20) declare @oper_amt float declare @t_vperf float declare @t_tperf float declare @total_comm float declare @prj_type varchar(20) declare @cls_flag int declare @quan2 float -- 数量 -- 指定的业绩 declare @f_oper_amt float, @f_t_vperf float, @f_t_tperf float, @f_quan2 float --轮班的业绩 declare @t_oper_amt float, @t_t_vperf float, @t_t_tperf float, @t_quan2 float declare @baseamt float declare @ratio float declare @total_f_quan float, @total_cust_num float-- 一个员工总的指定客数,注意不分项目类别 declare @fa_type nvarchar(20) --方案编号 delete workdetail_by_kind where spid = @spid -- 按卡/项目/产品累计算提成,按项目类别算提成 insert workdetail_by_kind(spid, compid, person_id, cls_flag, prj_type, oper_amt, total_vperf, total_tperf, quan, comm) select @spid, @compid, person_id, isnull(cls_flag, 1), isnull(prj_type, ''), sum(amt), sum(amt2), sum(amt3), sum(quan), sum(comm) --modify by lm 2015/02/11 isnull(cls_flag,'1') 取默认值1 from #work_detail_ceshi a with (nolock) where isnull(prj_type, '') <> '' and spid = @spid group by person_id, isnull(cls_flag, 1), isnull(prj_type, '') order by person_id, isnull(cls_flag, 1), isnull(prj_type, '') -- 分别统计指定业绩, 以及统计轮班业绩 create table #empl_stat_group_tmp ( person_id varchar(20) not null, -- 员工代码 cls_flag int not null, -- 1:项目, 2:产品, 3:卡 prj_type varchar(20) not null, -- 类别 oper_amt float null, -- 营业金额 total_vperf float null, -- 虚业绩 total_tperf float null, -- 实业绩 quan float null, -- 数量 primary key(person_id, cls_flag, prj_type) ) -- 统计指定的业绩 insert #empl_stat_group_tmp(person_id, cls_flag, prj_type, oper_amt, total_vperf, total_tperf, quan) select person_id, isnull(cls_flag, 1), isnull(prj_type, ''), sum(amt), sum(amt2), sum(amt3), sum(quan) -- modify by lm 2015/02/11 isnull(cls_flag,'1') 取默认值1 from #work_detail_ceshi where spid = @spid and (action_id = 2 or action_id = 5 or action_id = 7 or action_id = 10) group by person_id, isnull(cls_flag, 1), isnull(prj_type, '') update a set a.f_oper_amt = b.oper_amt, a.f_total_vperf = b.total_vperf, a.f_total_tperf = b.total_tperf, a.f_quan = b.quan from workdetail_by_kind a, #empl_stat_group_tmp b where spid = @spid and a.person_id = b.person_id and a.cls_flag = b.cls_flag and a.prj_type = b.prj_type -- 统计轮班的业绩 delete #empl_stat_group_tmp insert #empl_stat_group_tmp(person_id, cls_flag, prj_type, oper_amt, total_vperf, total_tperf, quan) select person_id, isnull(cls_flag, 1), isnull(prj_type, ''), sum(amt), sum(amt2), sum(amt3), sum(quan) --modify by lm 2015/02/11 isnull(cls_flag,'1') 取默认值1 from #work_detail_ceshi where spid = @spid and (action_id = 3 or action_id = 6 or action_id = 8 or action_id = 11 or action_id = 4) --modify by leiji on 2013年4月2日(把补工转到轮班当中, 否则在专业周记表中不会扣除补工业绩, 跟员工业绩统计表数据不一致) group by person_id, isnull(cls_flag, 1), isnull(prj_type, '') update a set a.t_oper_amt = b.oper_amt, a.t_total_vperf = b.total_vperf, a.t_total_tperf = b.total_tperf, a.t_quan = b.quan from workdetail_by_kind a, #empl_stat_group_tmp b where spid = @spid and a.person_id = b.person_id and a.cls_flag = b.cls_flag and a.prj_type = b.prj_type -- 计算每个员工不按项目类别分组的总的指定客数 -- 清空需要用到的缓冲 delete #empl_stat_group_tmp -- 分组统计 insert #empl_stat_group_tmp(person_id, cls_flag, prj_type, oper_amt, total_vperf, total_tperf, quan) select person_id, isnull(cls_flag, 1), '', sum(amt), sum(amt2), sum(amt3), sum(quan) --modify by lm 2015/02/11 isnull(cls_flag,'1') 取默认值1 from #work_detail_ceshi where spid = @spid and (action_id = 2 or action_id = 5 or action_id = 7 or action_id = 10) group by person_id, isnull(cls_flag, 1) update a set a.total_f_quan = b.quan from workdetail_by_kind a, #empl_stat_group_tmp b where spid = @spid and a.person_id = b.person_id and a.cls_flag = b.cls_flag drop table #empl_stat_group_tmp -- 把统计出来的每个员工总的指定客数更新到所需要更新的表中 -- 指定客户数量的统计,而不是项目数量的统计,用ggm07来统计 create table #tbl_personal_fixornot_num ( personid varchar(20) not null, num float null, primary key(personid) ) insert #tbl_personal_fixornot_num(personid, num) select ggg02c, count(*) from ggm07 b with (nolock), ggm01 a with (nolock) where a.gga00c = b.ggg00c and a.gga01c = b.ggg01c and a.gga02d >= @fromdate and a.gga02d <= @todate and a.gga00c = @compid and b.ggg03c = '1' group by ggg02c -- 把统计出来的每个员工总的指定客数更新到所需要更新的表中 update a set a.total_f_quan = b.num from workdetail_by_kind a, #tbl_personal_fixornot_num b where spid = @spid and a.person_id = b.personid delete #tbl_personal_fixornot_num -- 统计总的客数 insert #tbl_personal_fixornot_num(personid, num) select ggg02c, count(*) from ggm07 b with (nolock), ggm01 a with (nolock) where a.gga00c = b.ggg00c and a.gga01c = b.ggg01c and a.gga02d >= @fromdate and a.gga02d <= @todate and a.gga00c = @compid group by ggg02c update a set a.total_cust_num = b.num from workdetail_by_kind a, #tbl_personal_fixornot_num b where spid = @spid and a.person_id = b.personid drop table #tbl_personal_fixornot_num --Start创建临时表 add by lm 2015-08-07 create table #workdetail_by_kind ( id int identity(1,1) not null, person_id varchar(20) not null, -- 员工代码 cls_flag int not null, -- 1:项目,2:产品,3:卡 prj_type varchar(20) not null, -- 类别 oper_amt float null, -- 营业金额 total_vperf float null, -- 虚业绩 total_tperf float null, -- 实业绩 comm float null, -- 佣金 quan float null, -- 数量 f_oper_amt float null, -- 营业金额(指定) f_total_vperf float null, -- 虚业绩(指定) f_total_tperf float null, -- 实业绩(指定) f_comm float null, -- 佣金(指定) f_quan float null, -- 数量(指定) t_oper_amt float null, -- 营业金额(轮班) t_total_vperf float null, -- 虚业绩(轮班) t_total_tperf float null, -- 实业绩(轮班) t_comm float null, -- 佣金(轮班) t_quan float null, -- 数量(轮班) compid varchar(10) null, --公司别 fromdate varchar(10) null, --开始日期 todate varchar(10) null, --截止日期 total_f_quan float null, --每个员工不分项目类别的总的指定客数 total_cust_num float null --总的服务客户数量 ) insert #workdetail_by_kind(person_id, cls_flag, prj_type, oper_amt, total_vperf, total_tperf, quan, f_oper_amt, f_total_vperf, f_total_tperf, f_quan, t_oper_amt, t_total_vperf, t_total_tperf, t_quan, total_f_quan, total_cust_num) select person_id, cls_flag, prj_type, oper_amt, total_vperf, total_tperf, quan, f_oper_amt, f_total_vperf, f_total_tperf, f_quan, t_oper_amt, t_total_vperf, t_total_tperf, t_quan, total_f_quan, total_cust_num from workdetail_by_kind with(nolock) where spid = @spid -- 创建循环变量 declare @HasRecord int declare @idd int set @HasRecord = 1 declare @currentCompId varchar(10) while(@HasRecord = 1) begin if exists(select 1 from #workdetail_by_kind) begin set @HasRecord = 1 select top 1 @idd = id, @staff_id = person_id, @cls_flag = cls_flag, @prj_type = prj_type, @oper_amt = oper_amt, @t_vperf = total_vperf, @t_tperf = total_tperf, @quan2 = quan, @f_oper_amt = f_oper_amt, @f_t_vperf = f_total_vperf, @f_t_tperf = f_total_tperf, @f_quan2 = f_quan, @t_oper_amt = t_oper_amt, @t_t_vperf = t_total_vperf, @t_t_tperf = t_total_tperf, @t_quan2 = t_quan , @total_f_quan = total_f_quan, @total_cust_num = total_cust_num from #workdetail_by_kind with (nolock) declare @commway float -- 提成方式 declare @projectway float -- 项目提成方式 declare @productway float -- 产品提成方式 declare @cardway float -- 卡提成方式 select @commway = pre03i, @projectway = pre12i, @productway = pre13i, @cardway = pre14i, @fa_type = pre01c from precept with(nolock) where pre01c = (select top 1 ppt02c from personprecept with(nolock) where ppt03c = @staff_id and ppt00c = @compid and ppt04i = 1 order by ppt05d desc) and pre00c = @compid and isnull(pre22i, 0) = 0 if @commway = 3 and (@projectway = 3 or @productway = 3 or @cardway = 3) --项目、产品、卡分类别累计 begin -- 因为分段的标准不同。合计在一个员工一个数字与分别按类别计算是不同的,所以不能简单的把类别给去掉 exec upg_cal_sum_comm @compid,@oper_amt, @t_vperf, @t_tperf, @quan2, @f_oper_amt, @f_t_vperf, @f_t_tperf, @f_quan2, @t_oper_amt, @t_t_vperf, @t_t_tperf, @t_quan2, @staff_id, @cls_flag, @prj_type, @total_f_quan, @total_cust_num, @total_comm output, @fa_type,0,0 update workdetail_by_kind set comm = isnull(comm, 0) + @total_comm where person_id = @staff_id and cls_flag = @cls_flag and prj_type = @prj_type and spid = @spid end -- 清除已处理的门店 delete #workdetail_by_kind where id = @idd end else begin set @HasRecord = 0 end end drop table #workdetail_by_kind --End end GO /****** Object: StoredProcedure [dbo].[upg_cal_work_detail_by_empl_shop] Script Date: 06/12/2017 06:19:35 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_cal_work_detail_by_empl_shop] ( @spid int, @compid varchar(10), -- 公司别 @fromdate varchar(10), -- 开始日期 @todate varchar(10), -- 截至日期 @fromempl varchar(20), -- 查询开始人员 @toempl varchar(20), -- 查询截至人员 @amt float, -- 店总营业额 @vperf float, -- 店总虚业绩 @tperf float -- 店总真实业绩 ) as begin declare @staff_id varchar(20) declare @total_comm float declare @baseamt float -- 基数 declare @ratio float -- 比率 declare @fa_type nvarchar(20) -- 方案编号 create table #work_detail ( id int not null,-- 流水号 person_id varchar(20) not null -- 员工代码 ) insert #work_detail(id, person_id) select id, person_id from #work_detail_ceshi with (nolock) where spid = @spid and code = '店总业绩' -- 创建循环变量 declare @HasRecord int declare @idd int set @HasRecord = 1 while(@HasRecord = 1) begin if exists(select 1 from #work_detail) begin set @HasRecord = 1 select @idd = id, @staff_id = person_id from #work_detail -- 判断是否这个员工是按员工合计来计算提成的 declare @commway float -- 提成方式 declare @pre06i float -- 店总业绩计算方式 declare @pre07f float -- 扣除店总业绩的基数 declare @pre08f float -- 店总业绩比率 select @commway = pre03i, @pre06i = isnull(pre06i, 0), @fa_type = pre01c, @baseamt = isnull(pre07f, 0), @ratio = isnull(pre08f, 0) from precept with (nolock) where pre01c = (select top 1 ppt02c from personprecept with (nolock) where ppt03c = @staff_id and ppt00c = @compid and ppt04i = 1 order by ppt05d desc) and pre00c = @compid and isnull(pre22i, 0) = 0 if @commway = 1 and @pre06i = 1 begin -- 按店总业绩计算提成的人员,一般不会用到按指定客来计算提成,所以也把需要的参数设置为0 exec upg_cal_sum_comm @compid, @amt, @vperf, @tperf, 0, 0, 0, 0, 0, 0, 0, 0, 0, @staff_id, 5, '', 0, 0, @total_comm output, @fa_type,0,0 end else if @commway = 1 and @pre06i = 2 begin -- 店总的业绩扣一个基数计算提成 select @total_comm = (@vperf - @baseamt) * @ratio end if @commway = 1 begin update #work_detail_ceshi set comm = isnull(comm, 0) + @total_comm, code = '' where id = @idd update workdetail_by_empl set comm = isnull(comm, 0) + @total_comm, name = (select top 1 haa02c from ham01 with (nolock) where haa00c = @compid and haa01c = @staff_id) where person_id = @staff_id and compid = @compid and spid = @spid end delete #work_detail where id = @idd end else begin set @HasRecord = 0 end end drop table #work_detail end GO /****** Object: StoredProcedure [dbo].[upg_cal_work_detail_by_empl_num] Script Date: 06/12/2017 06:19:35 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_cal_work_detail_by_empl_num] ( @spid int, @compid varchar(10), -- 公司别 @fromdate varchar(10), -- 开始日期 @todate varchar(10), -- 截至日期 @fromempl varchar(20), -- 查询开始人员 @toempl varchar(20), -- 查询截至人员 @return_type int ) as begin declare @staff_id varchar(20) declare @cust_num float -- 数量 declare @haa40i int declare @total_comm float -- 从项目类别统计中分组到按员工 -- 先把每个员工的服务的客户数量统计出来 create table #person_cust_num ( person_id varchar(20) not null, cust_num float null, primary key(person_id) ) insert #person_cust_num(person_id, cust_num) select person_id, count(distinct billid) from work_detail with (nolock) where spid = @spid and cls_flag = 1 group by person_id update a set a.cust_num = b.cust_num from workdetail_by_empl a, #person_cust_num b where a.spid = @spid and a.person_id = b.person_id drop table #person_cust_num --Start创建临时表 add by lm 2015-05-06 create table #work_detail_by_empl_num ( id int not null, -- 流水号 person_id varchar(20) not null, -- 员工代码 cust_num float null -- 人头数 ) insert #work_detail_by_empl_num(id, person_id, cust_num) select id, person_id, cust_num from workdetail_by_empl with (nolock) where spid = @spid -- 创建循环变量 declare @HasRecord int set @HasRecord = 1 declare @idd int while (@HasRecord = 1) begin if exists(select 1 from #work_detail_by_empl_num) begin set @HasRecord = 1 select top 1 @idd = id, @staff_id = person_id, @cust_num = cust_num from #work_detail_by_empl_num declare @fa_type nvarchar(20) -- 提成方案 -- 判断是否这个员工是按员工合计来计算提成的 select @haa40i = isnull(pre05i, 0), @fa_type = pre01c from precept with (nolock) where pre01c = (select top 1 ppt02c from personprecept with (nolock) where ppt03c = @staff_id and ppt00c = @compid and ppt04i = 1 order by ppt05d desc) and pre00c = @compid and isnull(pre22i, 0) = 0 if @haa40i = 1 begin -- 因为没有指定客的概念,所以不用管多少指定客数量,参数都设置为0 exec upg_cal_sum_comm @compid, @cust_num, 0, 0, @cust_num, 0, 0, 0, 0, 0, 0, 0, 0, @staff_id, 6, '', 0, 0, @total_comm output, @fa_type,0,0 update workdetail_by_empl set comm = isnull(comm, 0) + @total_comm, cust_comm = @total_comm where id = @idd end delete #work_detail_by_empl_num where id = @idd end else begin set @HasRecord = 0 end end drop table #work_detail_by_empl_num end GO /****** Object: StoredProcedure [dbo].[upg_cal_work_detail_by_shop] Script Date: 06/12/2017 06:19:36 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_cal_work_detail_by_shop] ( @spid int, @compid varchar(10), -- 公司别 @fromdate varchar(10), -- 开始日期 @todate varchar(10), -- 截至日期 @fromempl varchar(20), -- 查询开始人员 @toempl varchar(20), -- 查询截至人员 @amt float output, -- 店总营业额 @vperf float output, -- 店总虚业绩 @tperf float output -- 店总真实业绩 ) as begin declare @payment_0 varchar(10) declare @payment_3 varchar(10) declare @payment_$ varchar(10) declare @payment_u1 varchar(10), @payment_u2 varchar(10), @payment_u3 varchar(10), @payment_u4 varchar(10), @payment_u5 varchar(10), @payment_u6 varchar(10), @payment_u7 varchar(10), @payment_u8 varchar(10), @payment_u9 varchar(10), @payment_u10 varchar(10), @payment_u11 varchar(10), @payment_u12 varchar(10), @payment_u13 varchar(10), @payment_u14 varchar(10), @payment_u15 varchar(10) exec upg_isrevenue_payment_shop @compid, @payment_0 output, @payment_3 output, @payment_$ output, @payment_u1 output, @payment_u2 output, @payment_u3 output, @payment_u4 output, @payment_u5 output, @payment_u6 output, @payment_u7 output, @payment_u8 output, @payment_u9 output, @payment_u10 output, @payment_u11 output, @payment_u12 output, @payment_u13 output, @payment_u14 output, @payment_u15 output select @amt = sum(gsc05f) from gsm03 with (nolock) where gsc10d >= @fromdate and gsc10d <= @todate and gsc00c = @compid and gsc04c in ('1', '6', 'ZFB', 'WX', @payment_0, @payment_3, @payment_$, @payment_u1, @payment_u2, @payment_u3, @payment_u4, @payment_u5, @payment_u6 , @payment_u7 , @payment_u8 , @payment_u9 , @payment_u10 , @payment_u11 , @payment_u12 , @payment_u13 , @payment_u14 , @payment_u15 ) select @vperf = @amt, @tperf = @amt end GO /****** Object: StoredProcedure [dbo].[upg_unclose_glm01] Script Date: 06/12/2017 06:20:47 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_unclose_glm01] ( @compid varchar(10), @billid varchar(20) ) as begin declare @cardid varchar(20) declare @keyid varchar(20) select @cardid = gla06c, @keyid = gla16c from glm01 with (nolock) where gla00c = @compid and gla01c = @billid exec upg_unclose_glm01_part @compid, @billid, @keyid -- 调整回卡的余额 update a set a.gcc06f = a.gcc06f + isnull(b.gsc05f, 0) from gcm03 a, gsm03 b, gsm14 c where a.gcc00c = b.gsc00c and a.gcc01c = @cardid and a.gcc03i = c.gsn02c and c.gsn01c = b.gsc04c and b.gsc00c = @compid and b.gsc01c = @billid and b.gsc02c = 'gl' delete a from gsm03 a, glm01 b where a.gsc00c = b.gla00c and a.gsc01c = b.gla01c and a.gsc02c = 'gl' and a.gsc01c = @billid and a.gsc00c = @compid delete a from gcm04 a where a.gcd00c = @compid and gcd01c = @cardid and gcd07c = 'gl' and gcd08c = @billid -- 可能这次支付是签单挂帐,则需要处理此部分 declare @gcm10_billid varchar(20) select @gcm10_billid = gcl01c from gcm10 where gcl11c = 'gl' and gcl12c = @billid and gcl06i = 5 if isnull(@gcm10_billid, '') <> '' begin exec upg_delete_deficit_gcm10 @compid, @gcm10_billid end end GO /****** Object: StoredProcedure [dbo].[upg_statistic_proj_comm] Script Date: 06/12/2017 06:20:44 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_statistic_proj_comm] ( @compid varchar(10), -- 公司别 @proj varchar(20), -- 卡代码,产品代码,项目代码 @class varchar(20), -- 卡类别 @standardprice float, -- 标准单价 @total float, -- 总金额, 实际金额 @vperf float, -- 虚业绩,应该已经计算好 @cardrate float, -- 转卡比率 @cost float, -- 成本, 是否扣成本是根据设置的 @times float, -- 服务次数,产品数, 卡数量 @staff varchar(200), -- 人员 @flag int, -- 1-卡销售, 2-指定,3-轮班, 4-补工, 56-中工, 78-小工, 10, 11-四工 9-产品销售 0-卡充值 @comm float output, -- 佣金 @trueperf float output, -- 真实业绩 @ex_rate float, -- 实际营业额折算比率 @last_m_comm float, -- 中工的提成 @last_l_comm float, -- 小工的提成 @share_rate float, -- 分享比率 @orderdate varchar(8), -- 账单日期 added by cyr 2012-04-10 @paycode varchar(10), -- 支付方式 added by cyr 2013-02-21 @billid varchar(20) ) as begin -- 注意: 卡的成本总是为0 -- 服务项目, 产品的成本已经计算好 declare @type int declare @setvalue float declare @commway float -- 提成方式 declare @projectway float -- 项目提成方式 declare @productway float -- 产品提成方式 declare @cardway float -- 卡提成方式 declare @fa_type varchar(20) -- 提成方案编号 -- 获取在账单有效日期内最新一条员工提成 方案 select top 1 @fa_type = ppt02c from personprecept where ppt00c = @compid and ppt03c = @staff and ppt04i = 1 and ppt02c in (select pre01c from precept where pre00c = @compid and isnull(pre22i, 0) = 0) order by ppt05d desc select @commway = pre03i, @projectway = pre12i, @productway = pre13i, @cardway = pre14i from precept where pre00c = @compid and pre01c = @fa_type and isnull(pre22i, 0) = 0 --(1店总业绩 2个人总业绩 3项目/产品/卡分别算 ) -- @commway为1和2时直接返回,不需要在这里计算 if @commway = 1 or @commway = 2 return if @cardrate > 1 select @cardrate = 1 select @comm = 0 -- 会员卡销售 if (@flag = 1 or @flag = 0) begin if @cardway = 2 or @cardway = 3 --卡提成按累计算 begin -- 计算累计提成 -- 每项计算提成设置为0 select @type = 1, @setvalue = 0 end else begin if (@flag = 1) begin if @cardway = 1 -- 卡销售-卡单独算 begin --提成设置,提成方式,提成金额或比率 select @type = car05i, @setvalue = car06f from cardprecept where car01c = @class and car02c = @fa_type and car00c = @compid end end else if (@flag = 0) begin --提成设置,提成方式,提成金额或比率 if @cardway = 1 --卡充值-卡单独算 begin select @type = car09i, @setvalue = car10f from cardprecept where car01c = @class and car02c = @fa_type and car00c = @compid end end end end else if ((@flag >= 2 and @flag <= 8) or (@flag >= 10 and @flag <= 11 ) or (@flag >= 15 and @flag <=16) or (@flag >= 18 and @flag <=21) or (@flag >= 23 and @flag <= 24)) -- 服务项目的提成设置 begin if @projectway = 1 -- 按项目设定来分别计算每个项目的提成 begin if (@flag = 2) if exists (select 1 from hem01 where hea33c = @fa_type and hea00c = @compid and hea01c = @proj and hea18i = 2 and hea34c = @paycode )-- modify by limin 2015/01/06 先取有支付方式,没有取默认值 select @type = hea02i, @setvalue = hea03f from hem01 where hea33c = @fa_type and hea00c = @compid and hea01c = @proj and hea18i = 2 and hea34c = @paycode else select @type = hea02i, @setvalue = hea03f from hem01 where hea33c = @fa_type and hea00c = @compid and hea01c = @proj and hea18i = 2 and isnull(hea34c, '*') = '*' else if (@flag = 3) if exists (select 1 from hem01 where hea33c = @fa_type and hea00c = @compid and hea01c = @proj and hea18i = 2 and hea34c = @paycode ) select @type = hea08i, @setvalue = hea09f from hem01 where hea33c = @fa_type and hea00c = @compid and hea01c = @proj and hea18i = 2 and hea34c = @paycode else select @type = hea08i, @setvalue = hea09f from hem01 where hea33c = @fa_type and hea00c = @compid and hea01c = @proj and hea18i = 2 and isnull(hea34c, '*') = '*' else if (@flag = 4) if exists (select 1 from hem01 where hea33c = @fa_type and hea00c = @compid and hea01c = @proj and hea18i = 2 and hea34c = @paycode ) select @type = hea10i, @setvalue = hea11f from hem01 where hea33c = @fa_type and hea00c = @compid and hea01c = @proj and hea18i = 2 and hea34c = @paycode else select @type = hea10i, @setvalue = hea11f from hem01 where hea33c = @fa_type and hea00c = @compid and hea01c = @proj and hea18i = 2 and isnull(hea34c, '*') = '*' else if (@flag = 5) if exists (select 1 from hem01 where hea33c = @fa_type and hea00c = @compid and hea01c = @proj and hea18i = 2 and hea34c = @paycode ) select @type = hea04i, @setvalue = hea05f from hem01 where hea33c = @fa_type and hea00c = @compid and hea01c = @proj and hea18i = 2 and hea34c = @paycode else select @type = hea04i, @setvalue = hea05f from hem01 where hea33c = @fa_type and hea00c = @compid and hea01c = @proj and hea18i = 2 and isnull(hea34c, '*') = '*' else if (@flag = 6) if exists (select 1 from hem01 where hea33c = @fa_type and hea00c = @compid and hea01c = @proj and hea18i = 2 and hea34c = @paycode ) select @type = hea14i, @setvalue = hea15f from hem01 where hea33c = @fa_type and hea00c = @compid and hea01c = @proj and hea18i = 2 and hea34c = @paycode else select @type = hea14i, @setvalue = hea15f from hem01 where hea33c = @fa_type and hea00c = @compid and hea01c = @proj and hea18i = 2 and isnull(hea34c, '*') = '*' else if (@flag = 7) if exists (select 1 from hem01 where hea33c = @fa_type and hea00c = @compid and hea01c = @proj and hea18i = 2 and hea34c = @paycode ) select @type = hea06i, @setvalue = hea07f from hem01 where hea33c = @fa_type and hea00c = @compid and hea01c = @proj and hea18i = 2 and hea34c = @paycode else select @type = hea06i, @setvalue = hea07f from hem01 where hea33c = @fa_type and hea00c = @compid and hea01c = @proj and hea18i = 2 and isnull(hea34c, '*') = '*' else if (@flag = 8) if exists (select 1 from hem01 where hea33c = @fa_type and hea00c = @compid and hea01c = @proj and hea18i = 2 and hea34c = @paycode ) select @type = hea16i, @setvalue = hea17f from hem01 where hea33c = @fa_type and hea00c = @compid and hea01c = @proj and hea18i = 2 and hea34c = @paycode else select @type = hea16i, @setvalue = hea17f from hem01 where hea33c = @fa_type and hea00c = @compid and hea01c = @proj and hea18i = 2 and isnull(hea34c, '*') = '*' else if (@flag = 10) if exists (select 1 from hem01 where hea33c = @fa_type and hea00c = @compid and hea01c = @proj and hea18i = 2 and hea34c = @paycode ) select @type = hea25i, @setvalue = hea26f from hem01 where hea33c = @fa_type and hea00c = @compid and hea01c = @proj and hea18i = 2 and hea34c = @paycode else select @type = hea25i, @setvalue = hea26f from hem01 where hea33c = @fa_type and hea00c = @compid and hea01c = @proj and hea18i = 2 and isnull(hea34c, '*') = '*' else if (@flag = 11) if exists (select 1 from hem01 where hea33c = @fa_type and hea00c = @compid and hea01c = @proj and hea18i = 2 and hea34c = @paycode ) select @type = hea27i, @setvalue = hea28f from hem01 where hea33c = @fa_type and hea00c = @compid and hea01c = @proj and hea18i = 2 and hea34c = @paycode else select @type = hea27i, @setvalue = hea28f from hem01 where hea33c = @fa_type and hea00c = @compid and hea01c = @proj and hea18i = 2 and isnull(hea34c, '*') = '*' else if (@flag = 15) --大工内 if exists (select 1 from hem01 where hea33c = @fa_type and hea00c = @compid and hea01c = @proj and hea18i = 2 and hea34c = @paycode ) select @type = hea35i, @setvalue = hea36f from hem01 where hea33c = @fa_type and hea00c = @compid and hea01c = @proj and hea18i = 2 and hea34c = @paycode else select @type = hea35i, @setvalue = hea36f from hem01 where hea33c = @fa_type and hea00c = @compid and hea01c = @proj and hea18i = 2 and isnull(hea34c, '*') = '*' else if (@flag = 16) --大工外 if exists (select 1 from hem01 where hea33c = @fa_type and hea00c = @compid and hea01c = @proj and hea18i = 2 and hea34c = @paycode ) select @type = hea43i, @setvalue = hea44f from hem01 where hea33c = @fa_type and hea00c = @compid and hea01c = @proj and hea18i = 2 and hea34c = @paycode else select @type = hea43i, @setvalue = hea44f from hem01 where hea33c = @fa_type and hea00c = @compid and hea01c = @proj and hea18i = 2 and isnull(hea34c, '*') = '*' else if (@flag = 18) --中工内 if exists (select 1 from hem01 where hea33c = @fa_type and hea00c = @compid and hea01c = @proj and hea18i = 2 and hea34c = @paycode ) select @type = hea37i, @setvalue = hea38f from hem01 where hea33c = @fa_type and hea00c = @compid and hea01c = @proj and hea18i = 2 and hea34c = @paycode else select @type = hea37i, @setvalue = hea38f from hem01 where hea33c = @fa_type and hea00c = @compid and hea01c = @proj and hea18i = 2 and isnull(hea34c, '*') = '*' else if (@flag = 19) --中工外 if exists (select 1 from hem01 where hea33c = @fa_type and hea00c = @compid and hea01c = @proj and hea18i = 2 and hea34c = @paycode ) select @type = hea45i, @setvalue = hea46f from hem01 where hea33c = @fa_type and hea00c = @compid and hea01c = @proj and hea18i = 2 and hea34c = @paycode else select @type = hea45i, @setvalue = hea46f from hem01 where hea33c = @fa_type and hea00c = @compid and hea01c = @proj and hea18i = 2 and isnull(hea34c, '*') = '*' else if (@flag = 20) --小工内 if exists (select 1 from hem01 where hea33c = @fa_type and hea00c = @compid and hea01c = @proj and hea18i = 2 and hea34c = @paycode ) select @type = hea39i, @setvalue = hea40f from hem01 where hea33c = @fa_type and hea00c = @compid and hea01c = @proj and hea18i = 2 and hea34c = @paycode else select @type = hea39i, @setvalue = hea40f from hem01 where hea33c = @fa_type and hea00c = @compid and hea01c = @proj and hea18i = 2 and isnull(hea34c, '*') = '*' else if (@flag = 21) --小工外 if exists (select 1 from hem01 where hea33c = @fa_type and hea00c = @compid and hea01c = @proj and hea18i = 2 and hea34c = @paycode ) select @type = hea47i, @setvalue = hea48f from hem01 where hea33c = @fa_type and hea00c = @compid and hea01c = @proj and hea18i = 2 and hea34c = @paycode else select @type = hea47i, @setvalue = hea48f from hem01 where hea33c = @fa_type and hea00c = @compid and hea01c = @proj and hea18i = 2 and isnull(hea34c, '*') = '*' else if (@flag = 23) --四工内 if exists (select 1 from hem01 where hea33c = @fa_type and hea00c = @compid and hea01c = @proj and hea18i = 2 and hea34c = @paycode ) select @type = hea41i, @setvalue = hea42f from hem01 where hea33c = @fa_type and hea00c = @compid and hea01c = @proj and hea18i = 2 and hea34c = @paycode else select @type = hea41i, @setvalue = hea42f from hem01 where hea33c = @fa_type and hea00c = @compid and hea01c = @proj and hea18i = 2 and isnull(hea34c, '*') = '*' else if (@flag = 24) --四工外 if exists (select 1 from hem01 where hea33c = @fa_type and hea00c = @compid and hea01c = @proj and hea18i = 2 and hea34c = @paycode ) select @type = hea49i, @setvalue = hea50f from hem01 where hea33c = @fa_type and hea00c = @compid and hea01c = @proj and hea18i = 2 and hea34c = @paycode else select @type = hea49i, @setvalue = hea50f from hem01 where hea33c = @fa_type and hea00c = @compid and hea01c = @proj and hea18i = 2 and isnull(hea34c, '*') = '*' end else if @projectway = 2 or @projectway = 3 begin --计算累计提成 --每项计算提成设置为0 select @type = 1, @setvalue = 0 end end else if (@flag = 9) begin if @productway = 2 or @productway = 3 begin --计算累计提成 --每项计算提成设置为0 select @type = 1, @setvalue = 0 end else begin select @type = pro05i, @setvalue = pro06f from productprecept where pro01c = @proj and pro02c = @fa_type and pro00c = @compid and isnull(pro10c, '*') = @paycode if(isnull(@type,'') = '') begin select @type = pro05i, @setvalue = pro06f from productprecept where pro01c = @proj and pro02c = @fa_type and pro00c = @compid and isnull(pro10c, '*') = '*' end end end select @setvalue = isnull(@setvalue, 0) --计算真实业绩 if @flag = 9 or @flag = 1 or @flag = 0 select @trueperf = isnull(@vperf, 0) if @flag = 9 or @flag = 1 or @flag = 0 select @cardrate = 1 --真实业绩 = (虚业绩-成本) * 转卡率 if @flag = 9 or @flag = 1 or @flag = 0 select @trueperf = (@vperf - isnull(@cost, 0)*isnull(@share_rate, 1))* @cardrate else select @trueperf = @trueperf * @cardrate if isnull(@type, 0) = 0 return if isnull(@ex_rate, -1) = -1 begin select @ex_rate = 1 end --计算提成 if (@type = 1) -- 固定金额 begin select @comm = @setvalue * @times * @share_rate end else if (@type = 2) -- 营业额比率 select @comm = @total * @setvalue * @share_rate --modify by leiji on 2013年1月21日 --select @comm = @total * @setvalue * @cardrate * @share_rate else if (@type = 3) -- 实业绩比率 select @comm = @trueperf * @setvalue else if (@type = 4) -- 虚业绩比率 select @comm = @vperf * @setvalue else if (@type = 41) -- 实际虚业绩比率 select @comm = @vperf * @setvalue * @ex_rate else if (@type = 5) -- 标准价比率 select @comm = @standardprice * @times * @setvalue * @cardrate * @share_rate else if (@type = 6) -- 折算虚业绩比率, 考虑实际营业额的比率 select @comm = @vperf * @setvalue * @ex_rate else if (@type = 61) -- 实际营业额, 考虑实际营业额的比率 select @comm = @total * @setvalue * @ex_rate else if (@type = 7) -- 营业额比率(扣小工提成) begin if @setvalue>2 select @comm = @setvalue * @times * @share_rate - @last_l_comm else select @comm = @total * @setvalue - @last_l_comm end else if (@type = 8) -- 营业额比率(扣中工小工提成) begin if @setvalue>2 select @comm = @setvalue * @times * @share_rate - (@last_l_comm + @last_m_comm) else select @comm = @total * @setvalue - (@last_l_comm + @last_m_comm) end else if (@type = 9) -- 营业额比率(扣中工提成) begin if @setvalue>2 select @comm = @setvalue * @times * @share_rate - @last_m_comm else select @comm = @total * @setvalue - @last_m_comm end else if (@type = 90) -- 虚业绩比率(扣小工提成) select @comm = @vperf * @setvalue - @last_l_comm else if (@type = 91) -- 虚业绩比率(扣中工小工提成) select @comm = @vperf * @setvalue - (@last_l_comm + @last_m_comm) else if (@type = 92) -- 虚业绩比率(扣中工提成) select @comm = @vperf * @setvalue - @last_m_comm else if (@type = 93) -- 实业绩比率(扣小工提成) select @comm = @trueperf * @setvalue - @last_l_comm else if (@type = 94) -- 实业绩比率(扣中工小工提成) select @comm = @trueperf * @setvalue - (@last_l_comm + @last_m_comm) else if (@type = 95) -- 实业绩比率(扣中工提成) select @comm = @trueperf * @setvalue - @last_m_comm else if (@type = 10) -- 营业额再折扣 begin declare @totall float select @totall = (@standardprice * @times ) if (isnull(@totall,0) = 0) select @comm = 0 else select @comm = @total * @setvalue * @cardrate * @share_rate * (@total/ @totall) end else if (@type = 14) -- 固定金额口中工成本 select @comm = @setvalue * @times * @share_rate - @last_m_comm end if @flag = 2 begin declare @op_billid varchar(20) select @op_billid = billid from openpolicy_master where compid = @compid and ggm_billid = @billid and billflag = 1 if (charindex('#map_', @op_billid) > 0) begin declare @ratio float exec upg_get_appointment_perf_ratio @compid, @proj, '', @ratio output set @comm = @comm * @ratio end end GO /****** Object: StoredProcedure [dbo].[upg_recover_pay_classify] Script Date: 06/12/2017 06:20:33 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_recover_pay_classify] as begin -- 创建临时表 create table #data_recover_comp ( compId varchar(10) not null ) insert into #data_recover_comp (compId) select gae01c from gam05 with (nolock) -- 创建循环变量 declare @HasRecord int set @HasRecord = 1 declare @currentCompId varchar(10) while(@HasRecord = 1) begin if exists(select 1 from #data_recover_comp) begin set @HasRecord = 1 select top 1 @currentCompId = compId from #data_recover_comp exec upg_recover_pay_classify_single @currentCompId -- 清除已处理的门店 delete #data_recover_comp where compId = @currentCompId end else begin set @HasRecord = 0 end end drop table #data_recover_comp end GO /****** Object: StoredProcedure [dbo].[upg_recover_gbm01_gba98c] Script Date: 06/12/2017 06:20:32 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_recover_gbm01_gba98c] as begin -- 员工的归属门店 if not exists (select 1 from data_recover where rdd00c = '001' and rdd01c = 'gbm01' and rdd02c = 'recover_gba98c') begin update gbm01 set gba98c = dbo.fun_getPY(isnull(gba03c, '')) + isnull(gba03c, '') + isnull(gba08c, '') + isnull(gba17d, '') + isnull(gba21c, '') insert into data_recover(rdd00c, rdd01c, rdd02c) values ('001', 'gbm01', 'recover_gba98c') end end GO /****** Object: StoredProcedure [dbo].[upg_billpay_2_itempay_all] Script Date: 06/12/2017 06:19:30 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_billpay_2_itempay_all] ( @compid varchar(10), @fromdate varchar(8), @todate varchar(8) ) as begin -- 复制到其下面的每个公司 declare @tocompid varchar(10) declare cur_each_comp cursor for select gaz02c from gam26 where gaz01c = @compid open cur_each_comp fetch cur_each_comp into @tocompid while @@fetch_status = 0 begin declare @gga01c nvarchar(20) declare cur_each_ggm01 cursor for select gga01c from ggm01 with(nolock) where gga00c = @tocompid and gga02d >= @fromdate and gga02d <= @todate open cur_each_ggm01 fetch cur_each_ggm01 into @gga01c while @@fetch_status = 0 begin exec upg_billpay_2_itempay @tocompid, @gga01c end fetch cur_each_ggm01 into @gga01c close cur_each_ggm01 deallocate cur_each_ggm01 end fetch cur_each_comp into @tocompid close cur_each_comp deallocate cur_each_comp end GO /****** Object: StoredProcedure [dbo].[upg_autocreate_stock_bills] Script Date: 06/12/2017 06:19:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_autocreate_stock_bills] ( @compid varchar(10), -- 公司别 @billid varchar(20), -- 盘点单号 @in_billid varchar(20) output, -- 需要产生的入库单号 @out_billid varchar(20) output, -- 需要产生的出库单号 @ret_st int output -- 返回的状态 0:不需要做 1:实际成功了 2:需要重新生成,但不允许 ) as--WUXM, 2006/02/10 begin declare @old_inbillid varchar(20) declare @old_outbillid varchar(20) declare @created_in int declare @created_out int declare @curdate varchar(20), @curtime varchar(20) exec upg_getcurrdate @curdate output, @curtime output select @created_in = 0, @created_out = 0 select @ret_st = 0 if not exists(select 1 from gkm02 where gkb00c = @compid and gkb01c = @billid and isnull(gkb06f, 0) <> 0) begin -- 没有找到需要调整库存的产品 return end select @old_inbillid = gka06c, @old_outbillid = gka07c from gkm01 where gka00c = @compid and gka01c = @billid --判断是否以前产生的入出库单已经复合了,如果已经复合就不能再生成了 if isnull(@old_inbillid, '')<>'' begin if exists(select 1 from ghm01 where gha00c = @compid and gha01c = @old_inbillid and isnull(gha94d, '')<>'') begin -- 已经复核 select @ret_st = 2 --return select @created_in = 1 end else begin delete ghm01 where gha00c = @compid and gha01c = @old_inbillid end end if isnull(@old_outbillid, '')<>'' begin if exists(select 1 from gim01 where gia00c = @compid and gia01c = @old_outbillid and isnull(gia94d, '')<>'') begin -- 已经复核 select @ret_st = 2 --return select @created_out = 1 end else begin delete gim01 where gia00c = @compid and gia01c = @old_outbillid end end declare @gkb02f float declare @gkb03c varchar(20) declare @gkb04c varchar(20) declare @gkb06f float declare @gkb07f float declare @stdsum float declare @currstdsum float declare @outstdunit varchar(20) declare @incost float -- 进货成本 -- 获取日期 declare @gka02d varchar(8) select @gka02d = gka02d from gkm01 where gka00c = @compid and gka01c = @billid --产生入库单 if exists(select 1 from gkm02 where gkb00c = @compid and gkb01c = @billid and isnull(gkb06f, 0)> 0 and @created_in = 0) begin insert ghm01(gha00c, gha01c, gha03d, gha04c, gha05c, gha07i, gha91c, gha92d, gha09t) select @compid, @in_billid, gka02d, gka03c, gka04c, 2, gka93c, gka94d, gka05t from gkm01 where gka00c = @compid and gka01c = @billid --add by lyj --盘点的时候可能不是标准单位盘点的 所以要把数量转换成标准单位的数量 declare cursor_01 cursor for select gkb02f, gkb03c, gkb04c, gkb06f from gkm02 where gkb00c = @compid and gkb01c = @billid and gkb06f>0 open cursor_01 fetch cursor_01 into @gkb02f, @gkb03c, @gkb04c, @gkb06f while @@fetch_status = 0 begin execute upg_getstdsum @compid, @gkb03c, @gkb04c, @gkb06f,@gka02d, @stdsum output, @outstdunit output, @incost output insert ghm02(ghb00c, ghb01c, ghb02f, ghb03c, ghb04c, ghb05f, ghb06c, ghb07f, ghb08c, ghb09f, ghb10f, ghb11f, ghb12c, ghb13f, ghb14f) values( @compid, @in_billid, @gkb02f, @gkb03c, @outstdunit, @stdsum, @outstdunit, @stdsum, 'RMB', 1, @incost, @incost*@stdsum, '', @stdsum, @incost) fetch cursor_01 into @gkb02f, @gkb03c, @gkb04c, @gkb06f end close cursor_01 deallocate cursor_01 select @ret_st = 1 --记录日志 insert sysoperationlog(userid, program, operation, operationdate, operationtime, origatedate, compid, keyvalue1, keyvalue2) values('system', 'GWA01I', 'A', @curdate, @curtime, @curdate, @compid, @in_billid, '盘点产生') end else begin select @in_billid = '' end --产生出库单 if exists(select 1 from gkm02 where gkb00c = @compid and gkb01c = @billid and isnull(gkb06f, 0)< 0 and @created_out = 0) begin insert gim01(gia00c, gia01c, gia03d, gia04c, gia05c, gia07i, gia91c, gia92d, gia08t, gia09i) select @compid, @out_billid, gka02d, gka03c, gka04c, 2, gka93c, gka94d, gka05t, 1 from gkm01 where gka00c = @compid and gka01c = @billid --add by lyj --盘点的时候可能不是标准单位盘点的 所以要把数量转换成标准单位的数量 declare cursor_02 cursor for select gkb02f, gkb03c, gkb04c, gkb06f, gkb07f from gkm02 where gkb00c = @compid and gkb01c = @billid and gkb06f<0 open cursor_02 fetch cursor_02 into @gkb02f, @gkb03c, @gkb04c, @gkb06f, @gkb07f while @@fetch_status = 0 begin execute upg_getstdsum @compid, @gkb03c, @gkb04c, @gkb06f,@gka02d, @stdsum output, @outstdunit output, @incost output execute upg_getstdsum @compid, @gkb03c, @gkb04c, @gkb07f,@gka02d, @currstdsum output, @outstdunit output, @incost output insert gim02(gib00c, gib01c, gib02f, gib03c, gib04c, gib05f, gib06c, gib07f, gib08f, gib09f, gib10f, gib11f) values(@compid, @out_billid, @gkb02f, @gkb03c, @outstdunit, @currstdsum, @outstdunit, @stdsum*(-1), @stdsum*(-1), @incost, @incost*@stdsum*(-1), @incost) fetch cursor_02 into @gkb02f, @gkb03c, @gkb04c, @gkb06f, @gkb07f end close cursor_02 deallocate cursor_02 select @ret_st = 1 --记录日志 insert sysoperationlog(userid, program, operation, operationdate, operationtime, origatedate, compid, keyvalue1, keyvalue2) values('system', 'GWA03I', 'A', @curdate, @curtime, @curdate, @compid, @out_billid, '盘点产生') end else begin select @out_billid = '' end if @ret_st = 1 begin update gkm01 set gka06c = @in_billid, gka07c = @out_billid where gka00c = @compid and gka01c = @billid end end GO /****** Object: StoredProcedure [dbo].[upg_insert_gim20] Script Date: 06/12/2017 06:20:15 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_insert_gim20] ( @compid varchar(10), -- 公司编号 @billNo varchar(20), -- 收银单号 @memberNo varchar(20), -- 会员编号 @savedbillid varchar(20), -- 寄存单号 @type varchar(1), -- 方式 0带走 1寄存 2欠货 @billDate varchar(8), -- 单据日期 @billtime varchar(8), -- 单据时间 @operationName varchar(20), -- 操作人 @operationdate varchar(8) -- 操作日期 ) as begin if not exists (select 1 from ggm03 where ggc00c = @compid and ggc01c = @billno and ggc36i = @type) return if @type = '1' --1寄存 begin insert gim20(gim00c, gim01c, gim02c, gim06c, gim07c, gim08d, gim09d, gim10t, gim11c, gim12c, gim15d) values(@compid, @savedbillid, @memberNo, '1', @operationName, @operationdate, @billDate, @billtime, '0', @billNo, getdate()) end else if @type = '2' --2欠货 begin insert gim20(gim00c, gim01c, gim02c, gim06c, gim07c, gim08d, gim09d, gim10t, gim11c, gim12c, gim15d) values(@compid, @savedbillid, @memberNo, '3', @operationName, @operationdate, @billDate, @billtime, '0', @billNo, getdate()) end declare @prodouctid nvarchar(20) -- 产品的标准单位 declare @unit nvarchar(20) --购买数量转成标准单位的数量 declare @num float --第一销售人员 declare @person nvarchar(20) declare @index int --ggm03产品序号 declare @ggc02f float --循环ggm03 declare cur_each_ggm03_item cursor for select ggc03c, gfa07c, ggc37f, ggc36i, ggc12c, ggc02f from ggm03 inner join gfm01 on gfa00c = ggc00c and gfa01c = ggc03c where ggc00c = @compid and ggc01c = @billno and ggc36i = @type open cur_each_ggm03_item fetch cur_each_ggm03_item into @prodouctid, @unit, @num, @type, @person, @ggc02f while @@fetch_status = 0 begin if @type = '1' --1寄存 begin insert gim21(gin00c, gin01c, gin02c, gin03f, gin04f, gin05c, gin06c, gin07f, gin08c) values(@compid, @savedbillid, @prodouctid, @ggc02f, @num, @unit, @person, @num, @unit) end else if @type = '2' --2欠货 begin insert gim21(gin00c, gin01c, gin02c, gin03f, gin04f, gin05c, gin06c, gin07f, gin08c) values(@compid, @savedbillid, @prodouctid, @ggc02f, @num, @unit, @person, @num, @unit) end fetch cur_each_ggm03_item into @prodouctid, @unit, @num, @type, @person, @ggc02f end close cur_each_ggm03_item deallocate cur_each_ggm03_item if @type = '1' --1寄存 begin exec upg_update_goods_deposit @compid, 1, @savedbillid end else if @type = '2' --2欠货 begin exec upg_update_goods_deposit @compid, 3, @savedbillid end update gim20 set gim13c = @operationName, gim14d = @operationdate where gim00c = @compid and gim01c = @savedbillid end GO /****** Object: StoredProcedure [dbo].[upg_get_cashcustid_app] Script Date: 06/12/2017 06:20:06 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_get_cashcustid_app] ( @compid varchar(10), @date varchar(20) ) as begin declare @retcustid varchar(10) declare @i int declare @custid varchar(20) select @i = 1 while @i < 200 begin select @custid = '散客' + convert(varchar(10), @i) if not exists(select 1 from gwm01 with(nolock) where gwa00c = @compid and gwa01d = @date and gwa04c = @custid) begin select @retcustid = @custid exec upg_check_cashcustid @compid, @custid select @retcustid return end select @i = @i + 1 end select '' end GO /****** Object: StoredProcedure [dbo].[upg_confirm_gim20] Script Date: 06/12/2017 06:19:45 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_confirm_gim20]( @compid varchar(20), @type int, --1-寄存 2-领取 3-欠货 @billno varchar(20), @confirm int, --是否复核 (1-复核 2-取消复核) @username varchar(20), --复核人 @date nvarchar(8) --复核时间 )--寄存单号 as begin declare @memberid nvarchar(10) declare @goodsid nvarchar(50) declare @num float declare @unit nvarchar(10) declare @flag float declare @take_from_id int if @confirm = 1--复核 begin exec upg_update_goods_deposit @compid, @type, @billno update gim20 set gim13c = @username, gim14d = @date where gim00c = @compid and gim01c = @billno end else--取消复核 begin if @type = 1 or @type = 3 --寄存或欠货 begin delete gim22 where gio00c = @compid and gio02c = @billno update gim20 set gim13c = '', gim14d = '' where gim00c = @compid and gim01c = @billno end else --领取 begin --循环 declare cur_each_gim21_item cursor for select gim02c, gin02c, gin07f, gin08c, gin03f, take_from_id from gim20 inner join gim21 on gim00c = gin00c and gim01c = gin01c where gim00c = @compid and gim01c = @billno open cur_each_gim21_item fetch cur_each_gim21_item into @memberid, @goodsid, @num, @unit, @flag, @take_from_id while @@fetch_status = 0 begin declare @gio11c nvarchar(1)--判断gim22是否为欠货领取 select @gio11c = gio11c from gim22 where gio00c = @compid and gio01c = @memberid and gio05c = @goodsid and gio06f = @flag and did = @take_from_id if @gio11c = '2' --欠货领取库存要返回 begin delete gfm03 where gfc00c = @compid and gfc01c = '7' and gfc02c = @billno delete gfm04 where gfd00c = @compid and gfd01c = '7' and gfd02c = @billno end update gim22 set gio09f = isnull(gio09f, 0) - @num, gio10f = isnull(gio10f, 0) + @num where gio00c = @compid and gio01c = @memberid and gio05c = @goodsid and gio06f = @flag and did = @take_from_id --删除库存历史 add lucz 2015.11.13 delete from gfm04 where gfd00c = @compid and gfd02c = @billno and gfd01c = '3' and gfd04c = @goodsid fetch cur_each_gim21_item into @memberid, @goodsid, @num, @unit, @flag, @take_from_id end close cur_each_gim21_item deallocate cur_each_gim21_item update gim20 set gim13c = '', gim14d = '' where gim00c = @compid and gim01c = @billno end end end GO /****** Object: StoredProcedure [dbo].[upg_day_total_detail_depart_ynkj] Script Date: 06/12/2017 06:19:57 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_day_total_detail_depart_ynkj]( @compid varchar(10), @fromdate varchar(20), @todate varchar(20), @staff varchar(20), --收银人员 @fromdepart varchar(20), --部门 @todepart varchar(20) --部门 ) as begin declare @payment_0 varchar(10) declare @payment_3 varchar(10) declare @payment_$ varchar(10) declare @payment_u1 varchar(10), @payment_u2 varchar(10), @payment_u3 varchar(10), @payment_u4 varchar(10), @payment_u5 varchar(10) exec upg_isrevenue_payment @compid, @payment_0 output, @payment_3 output, @payment_$ output, @payment_u1 output, @payment_u2 output, @payment_u3 output, @payment_u4 output, @payment_u5 output if @staff = '*' or @staff = '' begin select @staff = '%' end create table #tbl_item_group( idd int identity not null, -- 序号 flag int not null, -- 标志 1 项目 2会员卡 3产品,4新增疗程 prjtype varchar(20) null, -- 项目类别,产品类别 itemid varchar(20) null, -- 项目代码 产品代码 itemname varchar(60) null, -- 名称 quan float null, -- 数量 amt float null, -- 金额 zf varchar(5) null, --支付方式 lucz 2015.10.22 sort int null --序号 primary key(idd)) create table #tbl_item_group_dept( idd int identity not null, -- 序号 flag int not null, -- 标志 1 项目 2会员卡 3产品 prjtype varchar(20) null, -- 项目类别,产品类别 itemid varchar(20) null, -- 项目代码 产品代码 itemname varchar(60) null, -- 名称 quan float null, -- 数量 amt float null, -- 金额 zf varchar(5) null --支付方式 lucz 2015.10.22 ) declare @PK_Sql nvarchar(max) set @PK_Sql = ' alter table #tbl_item_group_dept add constraint PK_#tbl_item_group_dept_' + convert(varchar(10), cast(floor(rand() * 1000000) as int)) + ' primary key nonclustered(idd)' exec sp_executesql @PK_Sql create clustered index idx_item_group_dept_02 on #tbl_item_group_dept(flag, itemid) create table #tbl_fill_card( cardid varchar(20) not null, -- 卡号 amt float null, quan float null, zf varchar(5) null --支付方式 --primary key(cardid,zf) ) create table #tbl_fill_card_detail( cardid varchar(20) not null, -- 卡号 amt float null, quan float null, zf varchar(5) null --支付方式 ) create table #tbl_fill_card_dept( cardid varchar(20) not null, -- 卡号 amt float null, quan float null, zf varchar(5) null --支付方式 ) create clustered index idx_tbl_fill_card_dept_01 on #tbl_fill_card_dept(cardid) create table #tbl_fill_card_detail_dept( cardid varchar(20) not null, -- 卡号 amt float null, quan float null, zf varchar(5) null --支付方式 ) create clustered index idx_fill_card_detail_02 on #tbl_fill_card_detail_dept(cardid) --每个项目的实际金额 insert #tbl_item_group(flag, itemid, quan, amt,zf) select 1, ggb03c, sum(ggb05f), sum(ggb11f),ggb27c from ggm02 b with (nolock), ggm01 a with (nolock), gam26 z where b.ggb00c = a.gga00c and b.ggb01c = a.gga01c and a.gga00c = z.gaz02c and z.gaz01c = @compid and a.gga80d >= @fromdate and a.gga80d <= @todate and a.gga93c like @staff and (b.ggb81c between @fromdepart and @todepart or @fromdepart = '*') group by ggb03c,ggb27c --ham01 员工基本资料主档 haa06c varchar(10) null,--部门编号 --新增疗程 lucz 2015.10.21 insert #tbl_item_group(flag, itemid, quan, amt,zf) select 4,gch04c,sum(quan),sum(amt),gch17c from ( select gch04c, gch05f+gch06f quan,gch07f amt,gch17c from gcm08 left join gnm01 on gna00c = gch00c and gna01c = gch11c where gch00c = @compid and gch10c = 'gn' and gch02d between @fromdate and @todate and (@fromdepart = '*' or gna81c between @fromdepart and @todepart or gna82c between @fromdepart and @todepart or gna83c between @fromdepart and @todepart or gna100c between @fromdepart and @todepart or gna101c between @fromdepart and @todepart or gna102c between @fromdepart and @todepart or gna103c between @fromdepart and @todepart or gna104c between @fromdepart and @todepart or gna105c between @fromdepart and @todepart or gna106c between @fromdepart and @todepart ) union all select gch04c, gch05f+gch06f quan,gch07f amt,gch17c from gcm08 left join gcm10 on gcl00c = gch00c and gcl01c = gch11c where gch00c = @compid and gch10c = 'gz' and gch02d between @fromdate and @todate and (@fromdepart = '*' or gcl81c between @fromdepart and @todepart or gcl82c between @fromdepart and @todepart or gcl83c between @fromdepart and @todepart or gcl100c between @fromdepart and @todepart or gcl101c between @fromdepart and @todepart or gcl102c between @fromdepart and @todepart or gcl103c between @fromdepart and @todepart or gcl104c between @fromdepart and @todepart or gcl105c between @fromdepart and @todepart or gcl106c between @fromdepart and @todepart ) ) as Atable group by gch04c,gch17c --疗程消费(消耗) insert #tbl_item_group(flag, itemid, quan, amt,zf) select 5,ggb03c,sum(ggb05f),sum(ggb11f),ggb27c from ggm02 inner join ggm01 on gga00c = ggb00c and gga01c = ggb01c where ggb00c = @compid and isnull(ggb47i,-1) <> -1 and gga94d between @fromdate and @todate and (@fromdepart = '*' or ggb81c between @fromdepart and @todepart) group by ggb03c,ggb27c --产品的统计 create table #tbl_item_group_detail( idd int identity not null, -- 序号 flag int not null, -- 标志 1 项目 2会员卡 3产品 prjtype varchar(20) null, -- 项目类别,产品类别 itemid varchar(20) null, -- 项目代码 产品代码 itemname varchar(60) null, -- 名称 quan float null, -- 数量 amt float null, -- 金额 zf varchar(5) null --支付方式 primary key(idd)) --第一销售的部门 insert #tbl_item_group_detail(flag, itemid, quan, amt,zf) select 3, ggc03c, sum(ggc05f*isnull(ggc14f, 0)), sum(ggc11f*isnull(ggc14f, 0)),ggc27c from ggm03 b with (nolock), ggm01 a with (nolock), gam26 z where b.ggc00c = a.gga00c and b.ggc01c = a.gga01c and a.gga00c = z.gaz02c and z.gaz01c = @compid and a.gga80d >= @fromdate and a.gga80d <= @todate and a.gga93c like @staff and (b.ggc81c between @fromdepart and @todepart or (@fromdepart = '*')) group by ggc03c, ggc27c --第二销售的部门 insert #tbl_item_group_detail(flag, itemid, quan, amt,zf) select 3, ggc03c, sum(ggc05f*isnull(ggc16f, 0)), sum(ggc11f*isnull(ggc16f, 0)),ggc27c from ggm03 b with (nolock), ggm01 a with (nolock), gam26 z where b.ggc00c = a.gga00c and b.ggc01c = a.gga01c and a.gga00c = z.gaz02c and z.gaz01c = @compid and a.gga80d >= @fromdate and a.gga80d <= @todate and a.gga93c like @staff and (b.ggc82c between @fromdepart and @todepart or (@fromdepart = '*')) group by ggc03c,ggc27c --第三销售的部门 insert #tbl_item_group_detail(flag, itemid, quan, amt,zf) select 3, ggc03c, sum(ggc05f*isnull(ggc18f, 0)), sum(ggc11f*isnull(ggc18f, 0)),ggc27c from ggm03 b with (nolock), ggm01 a with (nolock), gam26 z where b.ggc00c = a.gga00c and b.ggc01c = a.gga01c and a.gga00c = z.gaz02c and z.gaz01c = @compid and a.gga80d >= @fromdate and a.gga80d <= @todate and a.gga93c like @staff and (b.ggc83c between @fromdepart and @todepart or (@fromdepart = '*')) group by ggc03c,ggc27c insert #tbl_item_group(flag, itemid, quan, amt,zf) select flag, itemid, sum(quan), sum(amt),zf from #tbl_item_group_detail group by flag, itemid,zf drop table #tbl_item_group_detail update a set a.prjtype = b.gda13c from #tbl_item_group a, gdm01 b where a.itemid = b.gda01c and a.flag in (1,4) and b.gda00c = @compid update a set a.prjtype = b.gfa24c from #tbl_item_group a, gfm01 b where a.itemid = b.gfa01c and a.flag = 3 and b.gfa00c = @compid --卡的统计 --一段时间内卡的销售 create table #tbl_gnm01_gnm02( gna00c varchar(10) not null, --公司编号 gna01c varchar(20) not null, --销售单号 gna27f float null, --第一分享比率 gna29f float null, --分享比率2 gna31f float null, --分享比率3 gna33f float null, --分享比率4 gna35f float null, --分享比率5 gna37f float null, --分享比率6 gna39f float null, --分享比率7 gna41f float null, --分享比率8 gna43f float null, --分享比率9 gna45f float null, --分享比率10 gna81c varchar(10) null, --部门编号 gna82c varchar(10) null, --部门编号2 gna83c varchar(10) null, --部门编号3 gnb03c varchar(20) not null, --会员卡号 zf varchar(5) null, gnb08f float null, --卡销售金额(包括疗程套餐的销售金额) num float null, gna100c varchar(10) null, --部门编号4 Add by maql 20100705 gna101c varchar(10) null, --部门编号5 Add by maql 20100705 gna102c varchar(10) null, --部门编号6 Add by maql 20100705 gna103c varchar(10) null, --部门编号7 Add by maql 20100705 gna104c varchar(10) null, --部门编号8 Add by maql 20100705 gna105c varchar(10) null, --部门编号9 Add by maql 20100705 gna106c varchar(10) null --部门编号10 Add by maql 20100705 ) create clustered index idx_gnm01_gnm02_02 on #tbl_gnm01_gnm02(gna00c, gna01c) insert #tbl_gnm01_gnm02(gna00c, gna01c, gna27f, gna29f, gna31f, gna33f, gna35f, gna37f, gna39f, gna41f, gna43f, gna45f, gna81c, gna82c, gna83c, gnb03c,zf, gnb08f, num, gna100c, gna101c, gna102c, gna103c, gna104c, gna105c, gna106c) select gna00c, gna01c, gna27f, gna29f, gna31f, gna33f, gna35f, gna37f, gna39f, gna41f, gna43f, gna45f, gna81c, gna82c, gna83c, gnb03c,gnb15c, gnb08f, 1, gna100c, gna101c, gna102c, gna103c, gna104c, gna105c, gna106c from gnm02 b with (nolock), gnm01 a with (nolock), gam26 z where a.gna00c = b.gnb00c and a.gna01c = b.gnb01c and a.gna80d >= @fromdate and a.gna80d <= @todate and a.gna00c = z.gaz02c and z.gaz01c = @compid and a.gna93c like @staff --第1销售的部门 delete #tbl_item_group_dept insert #tbl_item_group_dept(flag, itemid, quan, amt,zf) select 2, gnb03c, sum(num*isnull(gna27f, 0)), sum(gnb08f*isnull(gna27f, 0)),zf from #tbl_gnm01_gnm02 a where (a.gna81c between @fromdepart and @todepart or @fromdepart = '*') group by gnb03c, gna81c,zf insert #tbl_item_group_dept(flag, itemid, quan, amt,zf) select 2, gnb03c, sum(num*isnull(gna29f, 0)), sum(gnb08f*isnull(gna29f, 0)),zf from #tbl_gnm01_gnm02 a where (a.gna82c between @fromdepart and @todepart or @fromdepart = '*') group by gnb03c, gna82c,zf insert #tbl_item_group_dept(flag, itemid, quan, amt,zf) select 2, gnb03c, sum(num*isnull(gna31f, 0)), sum(gnb08f*isnull(gna31f, 0)),zf from #tbl_gnm01_gnm02 a where (a.gna83c between @fromdepart and @todepart or @fromdepart = '*') group by gnb03c, gna83c,zf insert #tbl_item_group_dept(flag, itemid, quan, amt,zf) select 2, gnb03c, sum(num*isnull(gna33f, 0)), sum(gnb08f*isnull(gna33f, 0)),zf from #tbl_gnm01_gnm02 a where (a.gna100c between @fromdepart and @todepart or @fromdepart = '*') group by gnb03c, gna100c,zf insert #tbl_item_group_dept(flag, itemid, quan, amt,zf) select 2, gnb03c, sum(num*isnull(gna35f, 0)), sum(gnb08f*isnull(gna35f, 0)),zf from #tbl_gnm01_gnm02 a where (a.gna101c between @fromdepart and @todepart or @fromdepart = '*') group by gnb03c, gna101c,zf insert #tbl_item_group_dept(flag, itemid, quan, amt,zf) select 2, gnb03c, sum(num*isnull(gna37f, 0)), sum(gnb08f*isnull(gna37f, 0)),zf from #tbl_gnm01_gnm02 a where (a.gna102c between @fromdepart and @todepart or @fromdepart = '*') group by gnb03c, gna102c,zf insert #tbl_item_group_dept(flag, itemid, quan, amt,zf) select 2, gnb03c, sum(num*isnull(gna39f, 0)), sum(gnb08f*isnull(gna39f, 0)),zf from #tbl_gnm01_gnm02 a where (a.gna103c between @fromdepart and @todepart or @fromdepart = '*') group by gnb03c, gna103c,zf insert #tbl_item_group_dept(flag, itemid, quan, amt,zf) select 2, gnb03c, sum(num*isnull(gna41f, 0)), sum(gnb08f*isnull(gna41f, 0)),zf from #tbl_gnm01_gnm02 a where (a.gna104c between @fromdepart and @todepart or @fromdepart = '*') group by gnb03c, gna104c,zf insert #tbl_item_group_dept(flag, itemid, quan, amt,zf) select 2, gnb03c, sum(num*isnull(gna43f, 0)), sum(gnb08f*isnull(gna43f, 0)),zf from #tbl_gnm01_gnm02 a where (a.gna105c between @fromdepart and @todepart or @fromdepart = '*') group by gnb03c, gna105c,zf insert #tbl_item_group_dept(flag, itemid, quan, amt,zf) select 2, gnb03c, sum(num*isnull(gna45f, 0)), sum(gnb08f*isnull(gna45f, 0)),zf from #tbl_gnm01_gnm02 a where (a.gna106c between @fromdepart and @todepart or @fromdepart = '*') --add by maql 20100707 group by gnb03c, gna106c,zf insert #tbl_item_group(flag, itemid, quan, amt,zf) select flag, itemid, sum(quan), sum(amt),zf from #tbl_item_group_dept group by flag, itemid,zf drop table #tbl_gnm01_gnm02 drop table #tbl_item_group_dept --这段时间内的冲值 create table #tbl_gcm10_buf( gcl03c varchar(20) null, --会员卡号 gcl07f float null, --异动金额(总收款金额), 含套餐,含疗程 zf varchar(5) null, --支付方式 gcl06i int null, --异动类别( 0充值, 1取款 5欠款 6还款) gcl81c varchar(10) null, --部门编号 gcl82c varchar(10) null, --部门编号2 gcl83c varchar(10) null, --部门编号3 gcl16f float null, --第一销售分享比率1 gcl18f float null, --分享比率2 gcl20f float null, --分享比率3 gcl23f float null, --分享比率4 gcl25f float null, --分享比率5 gcl27f float null, --分享比率6 gcl29f float null, --分享比率7 gcl31f float null, --分享比率8 gcl33f float null, --分享比率9 gcl35f float null, --分享比率10 num float null, gcl100c varchar(10) null, --部门编号4 gcl101c varchar(10) null, --部门编号5 gcl102c varchar(10) null, --部门编号6 gcl103c varchar(10) null, --部门编号7 gcl104c varchar(10) null, --部门编号8 gcl105c varchar(10) null, --部门编号9 gcl106c varchar(10) null --部门编号10 ) create clustered index idx_gcm10_buf on #tbl_gcm10_buf(gcl03c) insert #tbl_gcm10_buf(gcl03c, gcl07f, zf, gcl06i, gcl81c, gcl82c, gcl83c, gcl16f, gcl18f, gcl20f, gcl23f, gcl25f, gcl27f, gcl29f, gcl31f, gcl33f, gcl35f, num, gcl100c, gcl101c, gcl102c, gcl103c, gcl104c, gcl105c, gcl106c) select gcl03c, gcl07f,gcl67c, gcl06i, gcl81c, gcl82c, gcl83c, gcl16f, gcl18f, gcl20f, gcl23f, gcl25f, gcl27f, gcl29f, gcl31f, gcl33f, gcl35f, 1, gcl100c, gcl101c, gcl102c, gcl103c, gcl104c, gcl105c, gcl106c from gcm10 a with (nolock), gam26 z where gcl00c = z.gaz02c and z.gaz01c = @compid and a.gcl80d >= @fromdate and a.gcl80d <= @todate and gcl93c like @staff and isnull(gcl03c, '')<>'' --第1销售的部门的 insert #tbl_fill_card_detail_dept(cardid, amt, quan,zf) select gcl03c, sum(gcl07f*isnull(gcl16f, 0)), sum(num*isnull(gcl16f, 0)),zf from #tbl_gcm10_buf a where (gcl06i = 0 or gcl06i = 6) and (a.gcl81c between @fromdepart and @todepart or @fromdepart = '*') group by gcl03c, gcl81c,zf --第2销售的部门的 insert #tbl_fill_card_detail_dept(cardid, amt, quan,zf) select gcl03c, sum(gcl07f*isnull(gcl18f, 0)), sum(num*isnull(gcl18f, 0)),zf from #tbl_gcm10_buf a where (gcl06i = 0 or gcl06i = 6) and (a.gcl82c between @fromdepart and @todepart or @fromdepart = '*') group by gcl03c, gcl82c,zf --第3销售的部门的 insert #tbl_fill_card_detail_dept(cardid, amt, quan,zf) select gcl03c, sum(gcl07f*isnull(gcl20f, 0)), sum(num*isnull(gcl20f, 0)),zf from #tbl_gcm10_buf a where (gcl06i = 0 or gcl06i = 6) and (a.gcl83c between @fromdepart and @todepart or @fromdepart = '*') group by gcl03c, gcl83c,zf --第4销售的部门的 insert #tbl_fill_card_detail_dept(cardid, amt, quan,zf) select gcl03c, sum(gcl07f*isnull(gcl23f, 0)), sum(num*isnull(gcl23f, 0)),zf from #tbl_gcm10_buf a where (gcl06i = 0 or gcl06i = 6) and (a.gcl100c between @fromdepart and @todepart or @fromdepart = '*') group by gcl03c, gcl100c,zf --第5销售的部门的 insert #tbl_fill_card_detail_dept(cardid, amt, quan,zf) select gcl03c, sum(gcl07f*isnull(gcl25f, 0)), sum(num*isnull(gcl25f, 0)),zf from #tbl_gcm10_buf a where (gcl06i = 0 or gcl06i = 6) and (a.gcl101c between @fromdepart and @todepart or @fromdepart = '*') group by gcl03c, gcl101c,zf --第6销售的部门的 insert #tbl_fill_card_detail_dept(cardid, amt, quan,zf) select gcl03c, sum(gcl07f*isnull(gcl27f, 0)), sum(num*isnull(gcl27f, 0)),zf from #tbl_gcm10_buf a where (gcl06i = 0 or gcl06i = 6) and (a.gcl102c between @fromdepart and @todepart or @fromdepart = '*') group by gcl03c, gcl102c,zf --第7销售的部门的 insert #tbl_fill_card_detail_dept(cardid, amt, quan,zf) select gcl03c, sum(gcl07f*isnull(gcl29f, 0)), sum(num*isnull(gcl29f, 0)),zf from #tbl_gcm10_buf a where (gcl06i = 0 or gcl06i = 6) and (a.gcl103c between @fromdepart and @todepart or @fromdepart = '*') group by gcl03c, gcl103c,zf --第8销售的部门的 insert #tbl_fill_card_detail_dept(cardid, amt, quan,zf) select gcl03c, sum(gcl07f*isnull(gcl31f, 0)), sum(num*isnull(gcl31f, 0)),zf from #tbl_gcm10_buf a where (gcl06i = 0 or gcl06i = 6) and (a.gcl104c between @fromdepart and @todepart or @fromdepart = '*') group by gcl03c, gcl104c,zf --第9销售的部门的 insert #tbl_fill_card_detail_dept(cardid, amt, quan,zf) select gcl03c, sum(gcl07f*isnull(gcl33f, 0)), sum(num*isnull(gcl33f, 0)),zf from #tbl_gcm10_buf a where (gcl06i = 0 or gcl06i = 6) and (a.gcl105c between @fromdepart and @todepart or @fromdepart = '*') group by gcl03c, gcl105c,zf --第10销售的部门的 insert #tbl_fill_card_detail_dept(cardid, amt, quan,zf) select gcl03c, sum(gcl07f*isnull(gcl35f, 0)), sum(num*isnull(gcl35f, 0)),zf from #tbl_gcm10_buf a where (gcl06i = 0 or gcl06i = 6) and (a.gcl106c between @fromdepart and @todepart or @fromdepart = '*') group by gcl03c, gcl106c,zf insert #tbl_fill_card_detail(cardid, amt, quan,zf) select cardid, sum(amt), sum(quan),zf from #tbl_fill_card_detail_dept a group by cardid,zf drop table #tbl_fill_card_detail_dept --add by lyj 把会员卡变更的金额也应该给加进去 --因为明细分类帐里面有一个记录 --insert #tbl_fill_card_detail(cardid, amt, quan, zf) -- select gea07c, sum(gsc05f), 1, gea22c -- from gem01 with (nolock), -- gsm03 with (nolock), -- gam26 z -- where gea00c = z.gaz02c -- and z.gaz01c = @compid -- and gsc00c = gea00c -- and gea01c = gsc01c -- and gsc02c = 'ge' -- and gsc04c in ('1', '6', 'ZFB', 'WX', @payment_0, @payment_3, @payment_$, @payment_u1, @payment_u2, @payment_u3, @payment_u4, @payment_u5) -- and gea80d between @fromdate and @todate -- and gea21c like @staff -- and (gea81c between @fromdepart and @todepart or @fromdepart = '*') --add by lyj 2007-09-06 -- group by gea07c,gea22c --Start竞争转卡业绩 add by lm 2015-10-13 --第一销售 insert #tbl_fill_card_detail(cardid, quan, amt,zf) select gea07c, 1 , isnull(gea27f, 0), gea22c from gem01 a with (nolock), gsm03 b with(nolock), ham01 c with(nolock), gam26 z with (nolock) where a.gea00c = c.haa00c and a.gea21c = c.haa01c --员工编号 and a.gea00c = b.gsc00c and a.gea01c = b.gsc01c and a.gea00c = z.gaz02c and z.gaz01c = @compid and gsc10d >= @fromdate and gsc10d <= @todate and gsc11c like @staff and b.gsc02c = 'ge' and (isnull(c.haa06c, '') between @fromdepart and @todepart or @fromdepart = '*') --add by lm 2015-10-13 and isnull(gea21c,'') <> '' --第二销售 insert #tbl_fill_card_detail(cardid, quan, amt,zf) select gea07c, 1 , isnull(gea29f, 0),gea22c from gem01 a with (nolock), gsm03 b with(nolock), ham01 c with(nolock), gam26 z with (nolock) where a.gea00c = c.haa00c and a.gea28c = c.haa01c --员工编号 and a.gea00c = b.gsc00c and a.gea01c = b.gsc01c and a.gea00c = z.gaz02c and z.gaz01c = @compid and gsc10d >= @fromdate and gsc10d <= @todate and gsc11c like @staff and b.gsc02c = 'ge' and (isnull(c.haa06c, '') between @fromdepart and @todepart or @fromdepart = '*') --add by lm 2015-10-13 and isnull(gea28c,'') <> '' --第三销售 insert #tbl_fill_card_detail(cardid, quan, amt,zf) select gea07c, 1 , isnull(gea31f, 0),gea22c from gem01 a with (nolock), gsm03 b with(nolock), ham01 c with(nolock), gam26 z with (nolock) where a.gea00c = c.haa00c and a.gea30c = c.haa01c --员工编号 and a.gea00c = b.gsc00c and a.gea01c = b.gsc01c and a.gea00c = z.gaz02c and z.gaz01c = @compid and gsc10d >= @fromdate and gsc10d <= @todate and gsc11c like @staff and b.gsc02c = 'ge' and (isnull(c.haa06c, '') between @fromdepart and @todepart or @fromdepart = '*') --add by lm 2015-10-13 and isnull(gea30c,'') <> '' --第四销售 insert #tbl_fill_card_detail(cardid, quan, amt,zf) select gea07c, 1 ,isnull(gea33f, 0),gea22c from gem01 a with (nolock), gsm03 b with(nolock), ham01 c with(nolock), gam26 z with (nolock) where a.gea00c = c.haa00c and a.gea32c = c.haa01c --员工编号 and a.gea00c = b.gsc00c and a.gea01c = b.gsc01c and a.gea00c = z.gaz02c and z.gaz01c = @compid and gsc10d >= @fromdate and gsc10d <= @todate and gsc11c like @staff and b.gsc02c = 'ge' and (isnull(c.haa06c, '') between @fromdepart and @todepart or @fromdepart = '*') --add by lm 2015-10-13 and isnull(gea32c,'') <> '' --第五销售 insert #tbl_fill_card_detail(cardid, quan, amt,zf) select gea07c, 1 ,isnull(gea35f, 0),gea22c from gem01 a with (nolock), gsm03 b with(nolock), ham01 c with(nolock), gam26 z with (nolock) where a.gea00c = c.haa00c and a.gea34c = c.haa01c --员工编号 and a.gea00c = b.gsc00c and a.gea01c = b.gsc01c and a.gea00c = z.gaz02c and z.gaz01c = @compid and gsc10d >= @fromdate and gsc10d <= @todate and gsc11c like @staff and b.gsc02c = 'ge' and (isnull(c.haa06c, '') between @fromdepart and @todepart or @fromdepart = '*') --add by lm 2015-10-13 and isnull(gea34c,'') <> '' --第六销售 insert #tbl_fill_card_detail(cardid, quan, amt,zf) select gea07c, 1 ,isnull(gea37f, 0),gea22c from gem01 a with (nolock), gsm03 b with(nolock), ham01 c with(nolock), gam26 z with (nolock) where a.gea00c = c.haa00c and a.gea36c = c.haa01c --员工编号 and a.gea00c = b.gsc00c and a.gea01c = b.gsc01c and a.gea00c = z.gaz02c and z.gaz01c = @compid and gsc10d >= @fromdate and gsc10d <= @todate and gsc11c like @staff and b.gsc02c = 'ge' and (isnull(c.haa06c, '') between @fromdepart and @todepart or @fromdepart = '*') --add by lm 2015-10-13 and isnull(gea36c,'') <> '' --第七销售 insert #tbl_fill_card_detail(cardid, quan, amt,zf) select gea07c, 1 ,isnull(gea39f, 0),gea22c from gem01 a with (nolock), gsm03 b with(nolock), ham01 c with(nolock), gam26 z with (nolock) where a.gea00c = c.haa00c and a.gea38c = c.haa01c --员工编号 and a.gea00c = b.gsc00c and a.gea01c = b.gsc01c and a.gea00c = z.gaz02c and z.gaz01c = @compid and gsc10d >= @fromdate and gsc10d <= @todate and gsc11c like @staff and b.gsc02c = 'ge' and (isnull(c.haa06c, '') between @fromdepart and @todepart or @fromdepart = '*') --add by lm 2015-10-13 and isnull(gea38c,'') <> '' --第八销售 insert #tbl_fill_card_detail(cardid, quan, amt,zf) select gea07c, 1 ,isnull(gea41f, 0), gea22c from gem01 a with (nolock), gsm03 b with(nolock), ham01 c with(nolock), gam26 z with (nolock) where a.gea00c = c.haa00c and a.gea40c = c.haa01c --员工编号 and a.gea00c = b.gsc00c and a.gea01c = b.gsc01c and a.gea00c = z.gaz02c and z.gaz01c = @compid and gsc10d >= @fromdate and gsc10d <= @todate and gsc11c like @staff and b.gsc02c = 'ge' and (isnull(c.haa06c, '') between @fromdepart and @todepart or @fromdepart = '*') --add by lm 2015-10-13 and isnull(gea40c,'') <> '' --第九销售 insert #tbl_fill_card_detail(cardid, quan, amt,zf) select gea07c, 1 ,isnull(gea43f, 0), gea22c from gem01 a with (nolock), gsm03 b with(nolock), ham01 c with(nolock), gam26 z with (nolock) where a.gea00c = c.haa00c and a.gea42c = c.haa01c --员工编号 and a.gea00c = b.gsc00c and a.gea01c = b.gsc01c and a.gea00c = z.gaz02c and z.gaz01c = @compid and gsc10d >= @fromdate and gsc10d <= @todate and gsc11c like @staff and b.gsc02c = 'ge' and (isnull(c.haa06c, '') between @fromdepart and @todepart or @fromdepart = '*') --add by lm 2015-10-13 and isnull(gea42c,'') <> '' --第十销售 insert #tbl_fill_card_detail(cardid, quan, amt,zf) select gea07c, 1 ,isnull(gea45f, 0), gea22c from gem01 a with (nolock), gsm03 b with(nolock), ham01 c with(nolock), gam26 z with (nolock) where a.gea00c = c.haa00c and a.gea44c = c.haa01c --员工编号 and a.gea00c = b.gsc00c and a.gea01c = b.gsc01c and a.gea00c = z.gaz02c and z.gaz01c = @compid and gsc10d >= @fromdate and gsc10d <= @todate and gsc11c like @staff and b.gsc02c = 'ge' and (isnull(c.haa06c, '') between @fromdepart and @todepart or @fromdepart = '*') --add by lm 2015-10-13 and isnull(gea44c,'') <> '' --End insert #tbl_fill_card(cardid, amt, quan,zf) select cardid, sum(amt), sum(quan),zf from #tbl_fill_card_detail group by cardid,zf drop table #tbl_fill_card_detail update a set a.amt = a.amt + isnull(b.amt, 0) from #tbl_item_group a, #tbl_fill_card b where a.itemid = b.cardid and a.flag = 2 --有些卡并没有卡销售,也应该加入 delete a from #tbl_fill_card a, #tbl_item_group b where a.cardid = b.itemid and b.flag = 2 insert #tbl_item_group(flag, itemid, quan, amt,zf) select 2, cardid, quan, amt,zf from #tbl_fill_card delete #tbl_fill_card delete #tbl_fill_card_dept --欠款部分,需要扣掉 insert #tbl_fill_card_dept(cardid, amt, quan,zf) select gcl03c, sum(gcl07f*isnull(gcl16f, 0)), 0,zf from #tbl_gcm10_buf a where (gcl06i = 1 or gcl06i = 5) and (a.gcl81c between @fromdepart and @todepart or @fromdepart = '*') --add by lyj 2007-09-06 group by gcl03c, gcl81c,zf insert #tbl_fill_card_dept(cardid, amt, quan,zf) select gcl03c, sum(gcl07f*isnull(gcl18f, 0)), 0,zf from #tbl_gcm10_buf a where (gcl06i = 1 or gcl06i = 5) and (a.gcl82c between @fromdepart and @todepart or @fromdepart = '*') --add by lyj 2007-09-06 group by gcl03c, gcl82c,zf insert #tbl_fill_card_dept(cardid, amt, quan,zf) select gcl03c, sum(gcl07f*isnull(gcl20f, 0)), 0,zf from #tbl_gcm10_buf a where (gcl06i = 1 or gcl06i = 5) and (a.gcl83c between @fromdepart and @todepart or @fromdepart = '*') --add by lyj 2007-09-06 group by gcl03c, gcl83c,zf insert #tbl_fill_card(cardid, amt, quan,zf) select cardid, sum(amt), sum(quan),zf from #tbl_fill_card_dept group by cardid,zf drop table #tbl_fill_card_dept drop table #tbl_gcm10_buf update a set a.amt = a.amt - isnull(b.amt, 0) from #tbl_item_group a, #tbl_fill_card b where a.itemid = b.cardid and a.flag = 2 --有些卡并没有卡销售,也应该加入 delete a from #tbl_fill_card a, #tbl_item_group b where a.cardid = b.itemid and b.flag = 2 insert #tbl_item_group(flag, itemid, quan, amt,zf) select 2, cardid, quan, 0-amt,zf from #tbl_fill_card update a set a.prjtype = b.gca02c from #tbl_item_group a, gcm01 b where a.itemid = b.gca01c and b.gca00c = @compid and a.flag = 2 --前面是按会员卡来分组统计的 delete #tbl_fill_card insert #tbl_fill_card(cardid, quan, amt,zf) select prjtype, count(prjtype), sum(amt),zf from #tbl_item_group where flag = 2 and isnull(prjtype, '')<>'' group by prjtype,zf delete #tbl_item_group where flag = 2 --按卡的类别分分组统计 insert #tbl_item_group(flag, prjtype, itemid, quan, amt,zf) select 2, 'card', cardid, quan, amt,zf from #tbl_fill_card update a set a.itemname = b.gda03c from #tbl_item_group a, gdm01 b where a.itemid = b.gda01c and b.gda00c = @compid and a.flag in (1,4,5) --lucz 2015.10.22 update a set a.itemname = b.gfa03c from #tbl_item_group a, gfm01 b where a.itemid = b.gfa01c and b.gfa00c = @compid and a.flag = 3 update a set a.itemname = b.gak02c from #tbl_item_group a, gam10 b where a.itemid = b.gak01c and b.gak00c = @compid and a.flag = 2 --start更新序号 add by lm 2016.01.19 update a set a.sort = b.gsb06i from #tbl_item_group a, gsm02 b where a.prjtype = b.gsb02c and a.flag = 1 and b.gsb00c = @compid and b.gsb01c = 'AJ' update a set a.sort = b.gsb06i from #tbl_item_group a, gsm02 b where a.prjtype = b.gsb02c and a.flag = 3 and b.gsb00c = @compid and b.gsb01c = 'AL' --end select idd, flag, isnull(prjtype,'') prjtype, isnull(itemid,'') itemid, isnull(itemname,'') itemname, isnull(quan,0) quan, isnull(amt,0) amt, isnull(zf,'') zf, zfName = isnull((select gsb03c from gsm02 where gsb00c = @compid and gsb01c = '3' and gsb02c = zf),'') from #tbl_item_group order by flag, sort, itemid drop table #tbl_item_group drop table #tbl_fill_card end GO /****** Object: StoredProcedure [dbo].[upg_stat_card_sale] Script Date: 06/12/2017 06:20:43 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_stat_card_sale] ( @incompid varchar(10), -- 公司别 @fromdate varchar(10), -- 开始日期 @todate varchar(10), -- 截至日期 @fromcardtype varchar(10), -- 开始卡类别 @tocardtype varchar(10), -- 截止卡类别 @fromacctype varchar(10), -- 开始账户类别 Add by Huzy 2011/03/22 @toacctype varchar(10), -- 截止账户类别 @fromcardid varchar(20), -- 开始卡号 @tocardid varchar(20), -- 截至卡号 @orderby int, -- 0按卡类别 + 日期 1日期 2卡号 @include_proc int, -- 是否包括疗程部分 @saletype varchar(20) -- 业务类型 add by liyh '3' - 取款 ) as begin declare @payment_0 varchar(10) declare @payment_3 varchar(10) declare @payment_$ varchar(10) declare @payment_u1 varchar(10) declare @payment_u2 varchar(10) declare @payment_u3 varchar(10) declare @payment_u4 varchar(10) declare @payment_u5 varchar(10) exec upg_isrevenue_payment @incompid, @payment_0 output, @payment_3 output, @payment_$ output, @payment_u1 output, @payment_u2 output, @payment_u3 output, @payment_u4 output, @payment_u5 output if @fromdate = '*' or @fromdate = '' begin select @fromdate = '19000101', @todate = '20901231' end if @fromacctype = '*' set @fromacctype = '' if @toacctype = '*' set @toacctype = '' if (isnull(@fromacctype, '') = '') if (isnull(@toacctype, '') <> '') set @fromacctype = @toacctype else set @fromacctype = '' else if (isnull(@toacctype, '') = '') set @toacctype = @fromacctype create table #tbl_card_sale_detail ( compid varchar(20) not null, -- 公司别 ddate varchar(10) not null, -- 日期 ttime varchar(10) not null, -- 时间 billid varchar(20) not null, -- 单号 billtype varchar(20) not null, -- 单别 mode int null, -- 方式 0 会员卡销售 1 充 2 还款 3 取款 4欠款 5-变更 saleamt float null, -- 单据总金额 accamt float null, -- 账户应收金额 充进账户的实收金额 Add by Huzy 2011/03/22 savedamt float null, -- 储值金额 procamt float null, -- 疗程金额 Add by Huzy 2011/03/22 cardid varchar(20) null, cardtype varchar(10) null, acctype varchar(40) null, -- 账户类别 Add by Huzy 2011/03/22 acctypecode varchar(20) null, memname varchar(40) null, memphone varchar(20) null, sales varchar(40) null, salesid1 varchar(100) null, salesid1_p float null, -- 第一分享比率 ADD BY LEIJI on 2012-7-26 salesid2 varchar(100) null, salesid2_p float null, -- 第二分享比率 salesid3 varchar(100) null, salesid3_p float null, -- 第三分享比率 memo varchar(60) null, upgradeflag int null, oldcardtype varchar(20) null, sendacct varchar(200) null, -- 赠送账户 sendamt varchar(200) null -- 赠送金额 ) create table #gsm03 --add by lm 2015.12.03 ( compid varchar(20) not null, billid varchar(20) not null, -- 单号 billtype varchar(20) not null, -- 单别 memo varchar(60) null, ) create table #gcm11 ( gcm00c varchar(10) not null, --门店编号 gcm01c varchar(20) not null, --异动单号 gcm05f float null --疗程金额 ) create table #gnm03 -- 会员卡销售--疗程 ( gnc00c varchar(10) not null, -- 门店编号 gnc01c varchar(20) not null, -- 销售单号 gnc05f float null, -- 疗程金额 ) if @include_proc = 1 begin create table #tbl_card_sale_proc_detail ( compid varchar(20) not null, -- 公司别 billid varchar(20) not null, -- 单号 billtype varchar(20) not null, -- 单别 mode int null, -- 方式 0 会员卡销售 1 充 2 还款 3 取款 4欠款 5-变更 saleamt float null, cardid varchar(20) null, cardtype varchar(10) null ) end create table #gnm01_SendAcct -- 会员卡销售赠送记录 ( gns00c varchar(10) not null, -- 门店编号 gns01c varchar(20) not null, -- 单据编号 gns02c varchar(20) not null, -- 单据类型 gns03c varchar(200) not null, -- 赠送账户 gns04f varchar(200) null, -- 赠送金额 ) insert #gnm01_SendAcct(gns00c, gns01c, gns02c, gns03c, gns04f) select distinct gns00c, gns01c, gns02c, stuff((select ';' + rtrim((select top 1 gsb03c from gsm02 with(nolock) where gsb00c = gns00c and gsb01c = 'O' and gsb02c = gns03c)) from gnm01_SendAcct where k.gns00c = gns00c and k.gns01c = gns01c and k.gns02c = gns02c for xml path('')), 1, 1, '') gns03c, stuff((select ';' + rtrim(gns04f) from gnm01_SendAcct with(nolock) where k.gns00c = gns00c and k.gns01c = gns01c and k.gns02c = gns02c for xml path('')), 1, 1, '') gns04f from gnm01_SendAcct k with(nolock) where k.gns00c = @incompid and k.gns02c = 'gn' and gns01c in (select gna01c from gnm01 with(nolock) where gna00c = @incompid and gna80d >= @fromdate and gna80d <= @todate) -- 卡销售 账户类别取自 gnb13i insert #tbl_card_sale_detail(compid, ddate, ttime, billid, billtype, mode, saleamt, cardid, cardtype, acctype, acctypecode, salesid1, salesid1_p, salesid2, salesid2_p, salesid3, salesid3_p, memo, savedamt, sendacct, sendamt) select gna00c, gna80d, gna12t, gna01c, 'gn', 0, amt = isnull(gnb16f,0) + isnull(gnb18f,0)+ isnull(gnb20f,0)+ isnull(gnb22f,0) , gnb03c, gnb02c, gsb03c, gnb13i, gna20c, gna27f, gna28c, gna29f, gna30c, gna31f, memo = case isnull(gnb15c, '') when '' then '' else gnb15c + ':' + convert(varchar(20), gnb16f) + ';' end + case isnull(gnb17c, '') when '' then '' else gnb17c + ':' + convert(varchar(20), gnb18f) + ';' end + case isnull(gnb19c, '') when '' then '' else gnb19c + ':' + convert(varchar(20), gnb20f) + ';' end + case isnull(gnb21c, '') when '' then '' else gnb21c + ':' + convert(varchar(20), gnb22f) end, gnb11f - (case when gnb15c = '5' then gnb16f else 0 end + case when gnb17c = '5' then gnb18f else 0 end + case when gnb19c = '5' then gnb20f else 0 end + case when gnb21c = '5' then gnb22f else 0 end), (select top 1 gsb03c from gsm02 with(nolock) where gsb00c = @incompid and gsb01c = 'O' and gsb02c = gnb23c), gnb24f from gnm02 b with (nolock), gnm01 a with (nolock), gam26 z with(nolock), gsm02 s with(nolock) where a.gna00c = b.gnb00c and a.gna01c = b.gnb01c and a.gna80d >= @fromdate and a.gna80d <= @todate and ((b.gnb03c >= @fromcardid and b.gnb03c <= @tocardid) or (@fromcardid = '*')) and ((b.gnb02c >= @fromcardtype and b.gnb02c <= @tocardtype) or (@fromcardtype = '*')) and a.gna00c = z.gaz02c and z.gaz01c = @incompid and s.gsb00c = a.gna00c and s.gsb02c = b.gnb13i and s.gsb01c = 'O' -- update a -- set a.sendacct = c.gns03c, -- a.sendamt = c.gns04f -- from #tbl_card_sale_detail a, #gnm01_SendAcct c -- where c.gns00c = a.compid and c.gns01c = a.billid and c.gns02c = 'gn' if @include_proc = 1 begin insert #tbl_card_sale_proc_detail(compid, billid, billtype, mode, saleamt) select gna00c, gna01c, 'gn', 0, amt = sum(isnull(gnc16f,0)+ isnull(gnc18f,0)+ isnull(gnc20f,0)+ isnull(gnc24f,0) ) from gnm03 b with (nolock), gnm01 a with (nolock), gam26 z with(nolock) where a.gna00c = b.gnc00c and a.gna01c = b.gnc01c and a.gna80d >= @fromdate and a.gna80d <= @todate and ((a.gna13c >= @fromcardid and a.gna13c <= @tocardid) or (@fromcardid = '*')) and a.gna00c = z.gaz02c and z.gaz01c = @incompid group by gna00c, gna01c end -- 卡充值 账户取自 gcl04c insert #tbl_card_sale_detail(compid, ddate, ttime, billid, billtype, mode, saleamt, cardid, cardtype, acctype, acctypecode, salesid1, salesid1_p, salesid2, salesid2_p, salesid3, salesid3_p, upgradeflag, memo, savedamt, sendacct, sendamt) select gcl00c, gcl80d, gcl56t, gcl01c, 'gz', 1, amt = isnull(gcl68f,0)+ isnull(gcl70f,0)+ isnull(gcl72f,0)+ isnull(gcl74f,0), gcl03c, gcl15c, gsb03c, gcl04c, gcl09c, gcl16f, gcl17c, gcl18f, gcl19c, gcl20f, gcl14i, memo = case isnull(gcl67c, '') when '' then '' else gcl67c + ':' + convert(varchar(20), gcl68f) + ';' end + case isnull(gcl69c, '') when '' then '' else gcl69c + ':' + convert(varchar(20), gcl70f) + ';' end + case isnull(gcl71c, '') when '' then '' else gcl71c + ':' + convert(varchar(20), gcl72f) + ';' end + case isnull(gcl73c, '') when '' then '' else gcl73c + ':' + convert(varchar(20), gcl74f) end, gcl65f - (case when gcl67c = '5' then gcl68f else 0 end + case when gcl69c = '5' then gcl70f else 0 end + case when gcl71c = '5' then gcl72f else 0 end + case when gcl73c = '5' then gcl74f else 0 end), (select top 1 gsb03c from gsm02 with(nolock) where gsb00c = @incompid and gsb01c = 'O' and gsb02c = gcl107c) gcl107c, isnull(gcl108f, 0) from gcm10 a with (nolock), gam26 z with(nolock), gsm02 s with(nolock) where a.gcl80d >= @fromdate and a.gcl80d <= @todate and ((a.gcl03c >= @fromcardid and a.gcl03c <= @tocardid) or (@fromcardid = '*')) and ((a.gcl15c >= @fromcardtype and a.gcl15c <= @tocardtype) or (@fromcardtype = '*')) and a.gcl00c = z.gaz02c and z.gaz01c = @incompid and a.gcl06i = 0 and s.gsb00c = a.gcl00c and s.gsb01c = 'O' and s.gsb02c = a.gcl04c if @include_proc = 1 begin insert #tbl_card_sale_proc_detail(compid, billid, billtype, mode, saleamt) select gcl00c, gcl01c, 'gz', 1, sum(isnull(gcm16f,0)+ isnull(gcm18f,0)+ isnull(gcm20f,0)+ isnull(gcm24f,0) ) from gcm10 a with (nolock), gcm11 b with (nolock), gam26 z with(nolock) where a.gcl80d >= @fromdate and a.gcl80d <= @todate and a.gcl00c = b.gcm00c and a.gcl01c = b.gcm01c and ((a.gcl03c >= @fromcardid and a.gcl03c <= @tocardid) or (@fromcardid = '*')) and ((a.gcl15c >= @fromcardtype and a.gcl15c <= @tocardtype) or (@fromcardtype = '*')) and a.gcl00c = z.gaz02c and z.gaz01c = @incompid and a.gcl06i = 0 group by gcl00c, gcl01c end insert #tbl_card_sale_detail(compid, ddate, ttime, billid, billtype, mode, saleamt, cardid, cardtype, acctype, acctypecode, salesid1, salesid1_p, salesid2, salesid2_p, salesid3, salesid3_p, upgradeflag, memo, savedamt, sendacct, sendamt) select gcl00c, gcl80d, gcl56t, gcl01c, 'gz', 2, amt = isnull(gcl68f,0)+ isnull(gcl70f,0)+ isnull(gcl72f,0)+ isnull(gcl74f,0) , gcl03c, gcl15c, gsb03c, gcl04c, gcl09c, gcl16f, gcl17c, gcl18f, gcl19c, gcl20f, gcl14i, memo = case isnull(gcl67c, '') when '' then '' else gcl67c + ':' + convert(varchar(20), gcl68f) + ';' end + case isnull(gcl69c, '') when '' then '' else gcl69c + ':' + convert(varchar(20), gcl70f) + ';' end + case isnull(gcl71c, '') when '' then '' else gcl71c + ':' + convert(varchar(20), gcl72f) + ';' end + case isnull(gcl73c, '') when '' then '' else gcl73c + ':' + convert(varchar(20), gcl74f) end, gcl65f - (case when gcl67c = '5' then gcl68f else 0 end + case when gcl69c = '5' then gcl70f else 0 end + case when gcl71c = '5' then gcl72f else 0 end + case when gcl73c = '5' then gcl74f else 0 end), (select top 1 gsb03c from gsm02 with(nolock) where gsb00c = @incompid and gsb01c = 'O' and gsb02c = gcl107c) gcl107c, isnull(gcl108f, 0) from gcm10 a with (nolock), gam26 z with(nolock), gsm02 s with(nolock) where a.gcl80d >= @fromdate and a.gcl80d <= @todate and ((a.gcl03c >= @fromcardid and a.gcl03c <= @tocardid) or (@fromcardid = '*')) and ((a.gcl15c >= @fromcardtype and a.gcl15c <= @tocardtype) or (@fromcardtype = '*')) and a.gcl00c = z.gaz02c and z.gaz01c = @incompid and a.gcl06i = 6 and s.gsb00c = a.gcl00c and s.gsb01c = 'O' and s.gsb02c = a.gcl04c if @include_proc = 1 begin insert #tbl_card_sale_proc_detail(compid, billid, billtype, mode, saleamt) select gcl00c, gcl01c, 'gz', 2, sum( isnull(gcm16f,0) + isnull(gcm18f,0) + isnull(gcm20f,0) + isnull(gcm24f,0) ) from gcm10 a with (nolock), gcm11 b with (nolock), gam26 z with(nolock) where a.gcl80d >= @fromdate and a.gcl80d <= @todate and a.gcl00c = b.gcm00c and a.gcl01c = b.gcm01c and ((a.gcl03c >= @fromcardid and a.gcl03c <= @tocardid) or (@fromcardid = '*')) and ((a.gcl15c >= @fromcardtype and a.gcl15c <= @tocardtype) or (@fromcardtype = '*')) and a.gcl00c = z.gaz02c and z.gaz01c = @incompid and a.gcl06i = 6 group by gcl00c, gcl01c end -- 取款部分 insert #tbl_card_sale_detail(compid, ddate, ttime, billid, billtype, mode, saleamt, cardid, cardtype, acctype, acctypecode, salesid1, salesid1_p, salesid2, salesid2_p, salesid3, salesid3_p, upgradeflag, memo, savedamt, sendacct, sendamt) select gcl00c, gcl80d, gcl56t, gcl01c, 'gz', 3, amt = isnull(gcl68f,0) + isnull(gcl70f,0) + isnull(gcl72f,0) + isnull(gcl74f,0) , gcl03c, gcl15c, gsb03c, gcl04c, gcl09c, gcl16f, gcl17c, gcl18f, gcl19c, gcl20f, gcl14i, memo = case isnull(gcl67c, '') when '' then '' else gcl67c + ':' + convert(varchar(20), gcl68f) + ';' end + case isnull(gcl69c, '') when '' then '' else gcl69c + ':' + convert(varchar(20), gcl70f) + ';' end + case isnull(gcl71c, '') when '' then '' else gcl71c + ':' + convert(varchar(20), gcl72f) + ';' end + case isnull(gcl73c, '') when '' then '' else gcl73c + ':' + convert(varchar(20), gcl74f) end, 0 - (gcl65f - (case when gcl67c = '5' then gcl68f else 0 end + case when gcl69c = '5' then gcl70f else 0 end + case when gcl71c = '5' then gcl72f else 0 end + case when gcl73c = '5' then gcl74f else 0 end)), (select top 1 gsb03c from gsm02 with(nolock) where gsb00c = @incompid and gsb01c = 'O' and gsb02c = gcl107c) gcl107c, isnull(gcl108f, 0) from gcm10 a with (nolock), gam26 z with(nolock), gsm02 s with(nolock) where a.gcl80d >= @fromdate and a.gcl80d <= @todate and ((a.gcl03c >= @fromcardid and a.gcl03c <= @tocardid) or (@fromcardid = '*')) and a.gcl00c = z.gaz02c and z.gaz01c = @incompid and a.gcl06i = 1 and s.gsb00c = a.gcl00c and s.gsb01c = 'O' and s.gsb02c = a.gcl04c if @include_proc = 1 begin insert #tbl_card_sale_proc_detail(compid, billid, billtype, mode, saleamt) select gcl00c, gcl01c, 'gz', 3, sum(isnull(gcm16f,0) + isnull(gcm18f,0)+ isnull(gcm20f,0) + isnull(gcm24f,0) ) from gcm10 a with (nolock), gcm11 b with (nolock), gam26 z with(nolock) where a.gcl80d >= @fromdate and a.gcl80d <= @todate and a.gcl00c = b.gcm00c and a.gcl01c = b.gcm01c and ((a.gcl03c >= @fromcardid and a.gcl03c <= @tocardid) or (@fromcardid = '*')) and ((a.gcl15c >= @fromcardtype and a.gcl15c <= @tocardtype) or (@fromcardtype = '*')) and a.gcl00c = z.gaz02c and z.gaz01c = @incompid and a.gcl06i = 1 group by gcl00c, gcl01c end -- 把买卡 和充值的 销售金额更新 到 账户销售金额列 accamt By Huzy 2011/03/22 update #tbl_card_sale_detail set accamt = saleamt -- 把卖卡时,只卖了套餐的金额给追加到销售总额上 update a set saleamt = isnull(saleamt,0) + isnull(gtf05f,0) + isnull(gtf12f,0) + isnull(gtf14f,0) + isnull(gtf16f,0) from #tbl_card_sale_detail a ,gtc05 b with(nolock) where a.compid = b.gtf00c and a.billid = gtf01c -- 会员卡变更的费用 变更没有账户 insert #tbl_card_sale_detail(compid, ddate, ttime, billid, billtype, mode, saleamt,accamt,savedamt, cardid, cardtype, salesid1, salesid2, salesid3, memo,acctype) select gea00c, gea80d, gea22t, gea01c, 'ge', 5, saleamt = isnull(gea25f,0) , accamt = isnull(gea25f,0) , savedamt = isnull(gea25f,0) , gea07c, gca02c, gea21c, '', '', memo = dbo.memo_gsm03(a.gea00c ,a.gea01c, 'ge'), '储值账户' from gem01 a with(nolock), gcm01 b with(nolock), gam26 z with(nolock) where a.gea00c = b.gca00c and a.gea07c = b.gca01c and a.gea80d >= @fromdate and a.gea80d <= @todate and ((a.gea07c >= @fromcardid and a.gea07c <= @tocardid) or (@fromcardid = '*')) and ((b.gca02c >= @fromcardtype and b.gca02c <= @tocardtype) or (@fromcardtype = '*')) and a.gea00c = z.gaz02c and z.gaz01c = @incompid and a.gea02i = 4 -- 把疗程部分更新过去, -- 销售总额 = 前面没有疗程的"销售总额"加上"疗程金额", 同时把疗程金额更新到 新增的列 procamt 中 By Huzy 2011/03/22 if @include_proc = 1 begin update a set a.saleamt = isnull(a.saleamt, 0) + isnull(b.saleamt, 0), a.procamt = isnull(b.saleamt, 0) from #tbl_card_sale_detail a, #tbl_card_sale_proc_detail b where a.compid = b.compid and a.billid = b.billid and a.billtype = b.billtype and a.mode = b.mode drop table #tbl_card_sale_proc_detail end create table #temp_member ( cardid varchar(20), gba03c varchar(40), gba08c varchar(20) ) insert into #temp_member(cardid, gba03c, gba08c) select a.cardid, c.gba03c, c.gba08c from (select distinct cardid from #tbl_card_sale_detail) a, gcm01 b with(nolock), gbm01 c with(nolock) where a.cardid = b.gca01c and b.gca00c = b.gca13d and c.gba01c = b.gca04c -- 更新会员卡的名称 update p set p.memname = q.gba03c, p.memphone = q.gba08c from #tbl_card_sale_detail p join #temp_member q on p.cardid = q.cardid drop table #temp_member --修改充值,卖卡备注 add by lm 2015.12.02 insert #gsm03 (compid , billid, billtype, memo ) select gsc00c ,gsc01c, billtype, dbo.memo_gsm03(gsc00c ,gsc01c, gsc02c) from gsm03 a with (nolock), #tbl_card_sale_detail b where a.gsc00c = b.compid and a.gsc01c = b.billid and a.gsc02c = b.billtype and (a.gsc02c = 'gz' or a.gsc02c = 'gn') update a set a.memo = b.memo from #tbl_card_sale_detail a, #gsm03 b where a.compid = b.compid and a.billid = b.billid and a.billtype = b.billtype update a set a.sales = b.haa02c, a.salesid1 = b.haa02c from #tbl_card_sale_detail a, ham01 b with(nolock) where a.compid = b.haa00c and a.salesid1 = b.haa01c update a set a.sales = isnull(a.sales, '') + ';' + b.haa02c, a.salesid2 = b.haa02c from #tbl_card_sale_detail a, ham01 b with(nolock) where a.compid = b.haa00c and a.salesid2 = b.haa01c update a set a.sales = isnull(a.sales, '') + ';' + b.haa02c, a.salesid3 = b.haa02c from #tbl_card_sale_detail a, ham01 b with(nolock) where a.compid = b.haa00c and a.salesid3 = b.haa01c -- 卡升级的情况吧老卡找过来 update a set a.oldcardtype = b.gcl52c from #tbl_card_sale_detail a, gcm10 b with(nolock) where a.compid = b.gcl00c and a.billid = b.gcl01c and a.billtype = 'gz' and a.mode = 1 and isnull(a.upgradeflag, 0) = 1 --if(( '4' >= @fromacctype and '4' <= @toacctype) or @fromacctype = '') if(@fromacctype = '4' and @toacctype = '4') begin --Start 增加疗程账户 add by lm 2015.12.03 --不是疗程账户 疗程金额清零 insert #gcm11 (gcm00c, gcm01c, gcm05f) select gcm00c, gcm01c, sum(gcm05f) from gcm11 a with(nolock) , #tbl_card_sale_detail b where a.gcm00c = b.compid and a.gcm01c = b.billid and b.billtype = 'gz' and b.acctypecode <> '4' group by gcm00c, gcm01c insert #gnm03 (gnc00c, gnc01c, gnc05f) select gnc00c, gnc01c, sum(gnc05f) from gnm03 a with(nolock) , #tbl_card_sale_detail b where a.gnc00c = b.compid and a.gnc01c = b.billid and b.billtype= 'gn' and b.acctypecode <> '4' group by gnc00c, gnc01c insert #tbl_card_sale_detail (compid , ddate ,ttime ,billid ,billtype, mode,saleamt,accamt,savedamt,procamt,cardid ,cardtype,acctype, memname ,memphone ,sales , salesid1,salesid1_p ,salesid2 ,salesid2_p ,salesid3 ,salesid3_p,memo ,upgradeflag , oldcardtype, sendacct, sendamt, acctypecode) select compid , ddate ,ttime ,billid ,billtype, mode,gcm05f, 0, 0,gcm05f,cardid ,cardtype,'疗程账户', memname ,memphone ,sales , salesid1,salesid1_p ,salesid2 ,salesid2_p ,salesid3 ,salesid3_p,memo ,upgradeflag , oldcardtype, sendacct, sendamt, '4' from #gcm11 a with(nolock) , #tbl_card_sale_detail b where a.gcm00c = b.compid and a.gcm01c = b.billid and b.billtype = 'gz' and b.acctypecode <> '4' insert #tbl_card_sale_detail (compid , ddate ,ttime ,billid ,billtype, mode,saleamt,accamt,savedamt,procamt,cardid ,cardtype,acctype, memname ,memphone ,sales , salesid1,salesid1_p ,salesid2 ,salesid2_p ,salesid3 ,salesid3_p,memo ,upgradeflag , oldcardtype, sendacct, sendamt, acctypecode) select compid , ddate ,ttime ,billid ,billtype, mode,gnc05f, 0, 0,gnc05f,cardid ,cardtype,'疗程账户', memname ,memphone ,sales , salesid1,salesid1_p ,salesid2 ,salesid2_p ,salesid3 ,salesid3_p,memo ,upgradeflag , oldcardtype, sendacct, sendamt, '4' from #gnm03 a with(nolock) , #tbl_card_sale_detail b where a.gnc00c = b.compid and a.gnc01c = b.billid and b.billtype= 'gn' and b.acctypecode <> '4' update a set a.procamt = 0 from #tbl_card_sale_detail a where (a.billtype = 'gz' or a.billtype = 'gn') and a.acctypecode <> '4' update a set a.procamt = 0, a.saleamt = a.accamt + a.procamt from #tbl_card_sale_detail a where (a.billtype = 'gz' or a.billtype = 'gn') and a.acctypecode <> '4' delete from #tbl_card_sale_detail where acctypecode <> '4' end if(@fromacctype = '2' and @toacctype = '2') --储值账户不显示购买的疗程 begin update a set a.procamt = 0 from #tbl_card_sale_detail a where (a.billtype = 'gz' or a.billtype = 'gn') and a.acctypecode = '2' update a set a.procamt = 0, a.saleamt = a.accamt + a.procamt from #tbl_card_sale_detail a where (a.billtype = 'gz' or a.billtype = 'gn') and a.acctypecode = '2' delete from #tbl_card_sale_detail where acctypecode <> '4' and isnull(accamt,0) = 0 and isnull(savedamt,0) = 0 end --End if @orderby = 0 begin select compid , ddate ,ttime ,billid ,billtype, mode,saleamt,accamt,savedamt,procamt,cardid ,cardtype,acctype, memname ,memphone ,sales , salesid1,salesid1_p ,salesid2 ,salesid2_p ,salesid3 ,salesid3_p,memo ,upgradeflag , oldcardtype, sendacct, sendamt from #tbl_card_sale_detail where (@fromacctype = '' or (acctypecode >= @fromacctype and acctypecode <= @toacctype)) and charindex('*', @saletype) > 0 or (charindex('2', @saletype) > 0 and (billtype = 'gz' and mode = 1 and isnull(upgradeflag, 0) = 1)) or (charindex('3', @saletype) > 0 and (billtype = 'gz' and mode = 3)) or (charindex('4', @saletype) > 0 and (billtype = 'gz' and mode = 2)) or (charindex('1', @saletype) > 0 and (mode = 1 and isnull(upgradeflag, 0) != 1)) or (charindex('0', @saletype) > 0 and (mode = 0 and isnull(upgradeflag, 0) != 1)) order by cardtype asc, ddate asc, ttime asc, sales desc end else if @orderby = 1 begin select compid , ddate ,ttime ,billid ,billtype, mode,saleamt,accamt,savedamt,procamt,cardid ,cardtype,acctype, memname ,memphone ,sales , salesid1,salesid1_p ,salesid2 ,salesid2_p ,salesid3 ,salesid3_p,memo ,upgradeflag , oldcardtype, sendacct, sendamt from #tbl_card_sale_detail where (@fromacctype = '' or (acctypecode >= @fromacctype and acctypecode <= @toacctype)) and charindex('*', @saletype) > 0 or (charindex('2', @saletype) > 0 and (billtype = 'gz' and mode = 1 and isnull(upgradeflag, 0) = 1)) or (charindex('3', @saletype) > 0 and (billtype = 'gz' and mode = 3)) or (charindex('4', @saletype) > 0 and (billtype = 'gz' and mode = 2)) or (charindex('1', @saletype) > 0 and (mode = 1 and isnull(upgradeflag, 0) != 1)) or (charindex('0', @saletype) > 0 and (mode = 0 and isnull(upgradeflag, 0) != 1)) and (@fromacctype = '' or (acctypecode >= @fromacctype and acctypecode <= @toacctype)) order by ddate asc, ttime asc, sales desc end else if @orderby = 2 begin select compid , ddate ,ttime ,billid ,billtype, mode,saleamt,accamt,savedamt,procamt,cardid ,cardtype,acctype, memname ,memphone ,sales , salesid1,salesid1_p ,salesid2 ,salesid2_p ,salesid3 ,salesid3_p,memo ,upgradeflag , oldcardtype, sendacct, sendamt from #tbl_card_sale_detail where (@fromacctype = '' or (acctypecode >= @fromacctype and acctypecode <= @toacctype)) and charindex('*', @saletype) > 0 or (charindex('2', @saletype) > 0 and (billtype = 'gz' and mode = 1 and isnull(upgradeflag, 0) = 1)) or (charindex('3', @saletype) > 0 and (billtype = 'gz' and mode = 3)) or (charindex('3', @saletype) > 0 and (billtype = 'gz' and mode = 2)) or (charindex('4', @saletype) > 0 and (billtype = 'gz' and mode = 2)) or (charindex('1', @saletype) > 0 and (mode = 1 and isnull(upgradeflag, 0) != 1)) or (charindex('0', @saletype) > 0 and (mode = 0 and isnull(upgradeflag, 0) != 1)) and (@fromacctype = '' or (acctypecode >= @fromacctype and acctypecode <= @toacctype)) order by cardid asc, sales desc end else if @orderby = 3 begin select compid , ddate ,ttime ,billid ,billtype, mode,saleamt,accamt,savedamt,procamt,cardid ,cardtype,acctype, memname ,memphone ,sales , salesid1,salesid1_p ,salesid2 ,salesid2_p ,salesid3 ,salesid3_p,memo ,upgradeflag , oldcardtype, sendacct, sendamt from #tbl_card_sale_detail where (@fromacctype = '' or (acctypecode >= @fromacctype and acctypecode <= @toacctype)) and charindex('*', @saletype) > 0 or (charindex('2', @saletype) > 0 and (billtype = 'gz' and mode = 1 and isnull(upgradeflag, 0) = 1)) or (charindex('3', @saletype) > 0 and (billtype = 'gz' and mode = 3)) or (charindex('4', @saletype) > 0 and (billtype = 'gz' and mode = 2)) or (charindex('1', @saletype) > 0 and (mode = 1 and isnull(upgradeflag, 0) != 1)) or (charindex('0', @saletype) > 0 and (mode = 0 and isnull(upgradeflag, 0) != 1)) and (@fromacctype = '' or (acctypecode >= @fromacctype and acctypecode <= @toacctype)) order by cardtype asc end else if @orderby = 4 begin select compid , ddate ,ttime ,billid ,billtype, mode,saleamt,accamt,savedamt,procamt,cardid ,cardtype,acctype, memname ,memphone ,sales , salesid1,salesid1_p ,salesid2 ,salesid2_p ,salesid3 ,salesid3_p,memo ,upgradeflag , oldcardtype, sendacct, sendamt from #tbl_card_sale_detail where (@fromacctype = '' or (acctypecode >= @fromacctype and acctypecode <= @toacctype)) and charindex('*', @saletype) > 0 or (charindex('2', @saletype) > 0 and (billtype = 'gz' and mode = 1 and isnull(upgradeflag, 0) = 1)) or (charindex('3', @saletype) > 0 and (billtype = 'gz' and mode = 3)) or (charindex('4', @saletype) > 0 and (billtype = 'gz' and mode = 2)) or (charindex('1', @saletype) > 0 and (mode = 1 and isnull(upgradeflag, 0) != 1)) or (charindex('0', @saletype) > 0 and (mode = 0 and isnull(upgradeflag, 0) != 1)) and (@fromacctype = '' or (acctypecode >= @fromacctype and acctypecode <= @toacctype)) order by saleamt asc end else begin select compid , ddate ,ttime ,billid ,billtype, mode,saleamt,accamt,savedamt,procamt,cardid ,cardtype,acctype, memname ,memphone ,sales , salesid1,salesid1_p ,salesid2 ,salesid2_p ,salesid3 ,salesid3_p,memo ,upgradeflag , oldcardtype, sendacct, sendamt from #tbl_card_sale_detail where (@fromacctype = '' or (acctypecode >= @fromacctype and acctypecode <= @toacctype)) and charindex('*', @saletype) > 0 or (charindex('2', @saletype) > 0 and (billtype = 'gz' and mode = 1 and isnull(upgradeflag, 0) = 1)) or (charindex('3', @saletype) > 0 and (billtype = 'gz' and mode = 3)) or (charindex('4', @saletype) > 0 and (billtype = 'gz' and mode = 2)) or (charindex('1', @saletype) > 0 and (mode = 1 and isnull(upgradeflag, 0) != 1)) or (charindex('0', @saletype) > 0 and (mode = 0 and isnull(upgradeflag, 0) != 1)) and (@fromacctype = '' or (acctypecode >= @fromacctype and acctypecode <= @toacctype)) order by ddate asc, ttime asc, sales desc end drop table #tbl_card_sale_detail drop table #gnm01_SendAcct drop table #gsm03 drop table #gcm11 drop table #gnm03 end GO /****** Object: StoredProcedure [dbo].[upg_day_payment_detail_depart] Script Date: 06/12/2017 06:19:56 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_day_payment_detail_depart] ( @compid varchar(10), @fromdate varchar(20), @todate varchar(20), @staff varchar(20), @fromdepart varchar(20), --add by lyj 2007-09-06 @todepart varchar(20) ) as begin declare @payment_0 varchar(10) declare @payment_3 varchar(10) declare @payment_$ varchar(10) declare @payment_u1 varchar(10), @payment_u2 varchar(10), @payment_u3 varchar(10), @payment_u4 varchar(10), @payment_u5 varchar(10) exec upg_isrevenue_payment @compid, @payment_0 output, @payment_3 output, @payment_$ output, @payment_u1 output, @payment_u2 output, @payment_u3 output, @payment_u4 output, @payment_u5 output if @staff = '*' or @staff = '' begin select @staff = '%' end create table #tbl_item_group ( flag int not null, -- 标志 1-项目 2-会员卡 3-产品 payment varchar(5) null, -- 支付方式 quan float null, -- 数量 amt float null, -- 金额 ) create table #tbl_item_group_dept ( idd int identity not null, -- 序号 flag int not null, -- 标志 1 项目 2会员卡 3产品 prjtype varchar(20) null, -- 项目类别,产品类别 itemid varchar(20) null, -- 项目代码 产品代码 itemname varchar(60) null, -- 名称 quan float null, -- 数量 amt float null, -- 金额 ) -- 每个项目的实际金额 insert #tbl_item_group(flag, payment, quan, amt) select 1, ggb27c, sum(ggb05f), sum(ggb11f) from ggm02 b with (nolock), ggm01 a with (nolock), gam26 z with (nolock) where b.ggb00c = a.gga00c and b.ggb01c = a.gga01c and a.gga00c = z.gaz02c and z.gaz01c = @compid and a.gga80d >= @fromdate and a.gga80d <= @todate and a.gga93c like @staff and (b.ggb81c between @fromdepart and @todepart or @fromdepart = '*') --add by lyj 2007-09-06 group by ggb27c, ggb81c -- 第一销售的部门 insert #tbl_item_group(flag, payment, quan, amt) select 3, ggc27c, sum(ggc05f * isnull(ggc14f, 0)), sum(ggc11f * isnull(ggc14f, 0)) from ggm03 b with (nolock), ggm01 a with (nolock), gam26 z with (nolock) where b.ggc00c = a.gga00c and b.ggc01c = a.gga01c and a.gga00c = z.gaz02c and z.gaz01c = @compid and a.gga80d >= @fromdate and a.gga80d <= @todate and a.gga93c like @staff and (b.ggc81c between @fromdepart and @todepart or (@fromdepart = '*')) --add by lyj 2007-09-06 group by ggc27c, ggc81c -- 第二销售的部门 insert #tbl_item_group(flag, payment, quan, amt) select 3, ggc27c, sum(ggc05f * isnull(ggc16f, 0)), sum(ggc11f * isnull(ggc16f, 0)) from ggm03 b with (nolock), ggm01 a with (nolock), gam26 z with (nolock), ham01 d with(nolock) where b.ggc00c = a.gga00c and b.ggc01c = a.gga01c and a.gga00c = z.gaz02c and z.gaz01c = @compid and a.gga80d >= @fromdate and a.gga80d <= @todate and a.gga93c like @staff and a.gga00c = d.haa00c and b.ggc15c = d.haa01c and (d.haa06c between @fromdepart and @todepart or (@fromdepart = '*')) --add by lyj 2007-09-06 group by ggc27c, d.haa06c -- 第三销售的部门 insert #tbl_item_group(flag, payment, quan, amt) select 3, ggc27c, sum(ggc05f * isnull(ggc18f, 0)), sum(ggc11f * isnull(ggc18f, 0)) from ggm03 b with (nolock), ggm01 a with (nolock), gam26 z with (nolock), ham01 d with(nolock) where b.ggc00c = a.gga00c and b.ggc01c = a.gga01c and a.gga00c = z.gaz02c and z.gaz01c = @compid and a.gga80d >= @fromdate and a.gga80d <= @todate and a.gga93c like @staff and a.gga00c = d.haa00c and b.ggc17c = d.haa01c and (d.haa06c between @fromdepart and @todepart or (@fromdepart = '*')) --add by lyj 2007-09-06 group by ggc27c, d.haa06c -- 卡的统计 -- 一段时间内卡的销售 create table #tbl_gnm01_gnm02 ( gna00c varchar(10) not null, --公司编号 gna01c varchar(20) not null, --销售单号 gna27f float null, -- 第一分享比率 gna29f float null, -- 分享比率2 gna31f float null, -- 分享比率3 gna33f float null, -- 分享比率4 gna35f float null, -- 分享比率5 gna37f float null, -- 分享比率6 gna39f float null, -- 分享比率7 gna41f float null, -- 分享比率8 gna43f float null, -- 分享比率9 gna45f float null, -- 分享比率10 gna81c varchar(10) null, -- 部门编号 gna82c varchar(10) null, -- 部门编号2 gna83c varchar(10) null, -- 部门编号3 gna100c varchar(10) null, -- 部门编号4 Add by maql 20100705 gna101c varchar(10) null, -- 部门编号5 Add by maql 20100705 gna102c varchar(10) null, -- 部门编号6 Add by maql 20100705 gna103c varchar(10) null, -- 部门编号7 Add by maql 20100705 gna104c varchar(10) null, -- 部门编号8 Add by maql 20100705 gna105c varchar(10) null, -- 部门编号9 Add by maql 20100705 gna106c varchar(10) null, -- 部门编号10 Add by maql 20100705 gnb15c varchar(5) null, -- 支付方式1 gnb16f float null, -- 支付方式1金额 gnb17c varchar(5) null, -- 支付方式2 gnb18f float null, -- 支付方式2金额 gnb19c varchar(5) null, -- 支付方式3 gnb20f float null, -- 支付方式3金额 gnb21c varchar(5) null, -- 支付方式4 gnb22f float null, -- 支付方式4金额 gna67f float null, --第一销售虚业绩 Added by WUXM 2005/11/04 gna69f float null, --第二销售虚业绩 Added by WUXM 2005/11/04 gna71f float null, --第三销售虚业绩 Added by WUXM 2005/11/04 gna86f float null, --第四人员业绩 gna87f float null, --第五人员业绩 gna88f float null, --第六人员业绩 gna89f float null, --第七人员业绩 gna90f float null, --第八人员业绩 gna91f float null, --第九人员业绩 gna92f float null, --第十人员业绩 gna20c varchar(10) null, --负责业务 第一卖卡人 gna28c varchar(10) null, --第二卖卡人 gna30c varchar(10) null, --第三卖卡人 gna32c varchar(10) null, --第4卖卡人 gna34c varchar(10) null, --第5卖卡人 gna36c varchar(10) null, --第6卖卡人 gna38c varchar(10) null, --第7卖卡人 gna40c varchar(10) null, --第8卖卡人 gna42c varchar(10) null, --第9卖卡人 gna44c varchar(10) null, --第10卖卡人 bill_total_amt float null --业绩合计 ) insert #tbl_gnm01_gnm02(gna00c,gna01c,gna27f, gna29f, gna31f, gna33f, gna35f, gna37f, gna39f, gna41f, gna43f, gna45f, gna81c, gna82c, gna83c, gna101c, gna102c, gna103c, gna104c, gna105c, gna106c, gnb15c, gnb16f, gnb17c, gnb18f, gnb19c, gnb20f, gnb21c, gnb22f, gna67f,gna69f,gna71f,gna86f,gna87f,gna88f,gna89f,gna90f,gna91f,gna92f, gna20c,gna28c,gna30c,gna32c,gna34c,gna36c,gna38c,gna40c,gna42c,gna44c, bill_total_amt ) select gna00c,gna01c,gna27f, gna29f, gna31f, gna33f, gna35f, gna37f, gna39f, gna41f, gna43f, gna45f, gna81c, gna82c, gna83c, gna101c, gna102c, gna103c, gna104c, gna105c, gna106c, gnb15c, gnb16f, gnb17c, gnb18f, gnb19c, gnb20f, gnb21c, gnb22f, gna67f,gna69f,gna71f,gna86f,gna87f,gna88f,gna89f,gna90f,gna91f,gna92f,gna20c,gna28c,gna30c,gna32c,gna34c,gna36c,gna38c,gna40c,gna42c,gna44c, isnull(gna67f,0)+isnull(gna69f,0)+isnull(gna71f,0)+isnull(gna86f,0)+isnull(gna87f,0)+isnull(gna88f,0)+isnull(gna89f,0)+isnull(gna90f,0)+isnull(gna91f,0)+isnull(gna92f,0) from gnm02 b with (nolock), gnm01 a with (nolock), gam26 z with (nolock) where a.gna00c = b.gnb00c and a.gna01c = b.gnb01c and a.gna80d >= @fromdate and a.gna80d <= @todate and a.gna00c = z.gaz02c and z.gaz01c = @compid and a.gna93c like @staff -- 1 insert #tbl_item_group(flag, payment, quan, amt) select 2, gsc04c, 1, case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gna67f,0) * gsc05f / bill_total_amt end from #tbl_gnm01_gnm02 b,gsm03 a where b.gna00c = a.gsc00c and b.gna01c = a.gsc01c and a.gsc02c = 'gn' and (b.gna81c between @fromdepart and @todepart or @fromdepart = '*') and isnull(gna20c,'') <> '' insert #tbl_item_group(flag, payment, quan, amt) select 2, gsc04c, 1,case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gna69f,0) * gsc05f / bill_total_amt end from #tbl_gnm01_gnm02 b,gsm03 a where b.gna00c = a.gsc00c and b.gna01c = a.gsc01c and a.gsc02c = 'gn' and (b.gna81c between @fromdepart and @todepart or @fromdepart = '*') and isnull(gna28c,'') <> '' insert #tbl_item_group(flag, payment, quan, amt) select 2, gsc04c, 1,case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gna71f,0) * gsc05f / bill_total_amt end from #tbl_gnm01_gnm02 b,gsm03 a where b.gna00c = a.gsc00c and b.gna01c = a.gsc01c and a.gsc02c = 'gn' and (b.gna81c between @fromdepart and @todepart or @fromdepart = '*') and isnull(gna30c,'') <> '' insert #tbl_item_group(flag, payment, quan, amt) select 2,gsc04c, 1,case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gna86f,0) * gsc05f / bill_total_amt end from #tbl_gnm01_gnm02 b,gsm03 a where b.gna00c = a.gsc00c and b.gna01c = a.gsc01c and a.gsc02c = 'gn' and (b.gna81c between @fromdepart and @todepart or @fromdepart = '*') and isnull(gna32c,'') <> '' -- 2 insert #tbl_item_group(flag, payment, quan, amt) select 2, gsc04c, 1,case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gna87f,0) * gsc05f / bill_total_amt end from #tbl_gnm01_gnm02 b,gsm03 a where b.gna00c = a.gsc00c and b.gna01c = a.gsc01c and a.gsc02c = 'gn' and (b.gna82c between @fromdepart and @todepart or @fromdepart = '*') and isnull(gna34c,'') <> '' insert #tbl_item_group(flag, payment, quan, amt) select 2, gsc04c, 1,case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gna88f,0) * gsc05f / bill_total_amt end from #tbl_gnm01_gnm02 b,gsm03 a where b.gna00c = a.gsc00c and b.gna01c = a.gsc01c and a.gsc02c = 'gn' and (b.gna82c between @fromdepart and @todepart or @fromdepart = '*') and isnull(gna36c,'') <> '' insert #tbl_item_group(flag, payment, quan, amt) select 2, gsc04c,1,case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gna89f,0) * gsc05f / bill_total_amt end from #tbl_gnm01_gnm02 b,gsm03 a where b.gna00c = a.gsc00c and b.gna01c = a.gsc01c and a.gsc02c = 'gn' and (b.gna82c between @fromdepart and @todepart or @fromdepart = '*') and isnull(gna38c,'') <> '' insert #tbl_item_group(flag, payment, quan, amt) select 2, gsc04c,1,case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gna90f,0) * gsc05f / bill_total_amt end from #tbl_gnm01_gnm02 b,gsm03 a where b.gna00c = a.gsc00c and b.gna01c = a.gsc01c and a.gsc02c = 'gn' and (b.gna82c between @fromdepart and @todepart or @fromdepart = '*') and isnull(gna40c,'') <> '' -- 3 insert #tbl_item_group(flag, payment, quan, amt) select 2, gsc04c, 1,case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gna91f,0) * gsc05f / bill_total_amt end from #tbl_gnm01_gnm02 b,gsm03 a where b.gna00c = a.gsc00c and b.gna01c = a.gsc01c and a.gsc02c = 'gn' and (b.gna83c between @fromdepart and @todepart or @fromdepart = '*') and isnull(gna42c,'') <> '' insert #tbl_item_group(flag, payment, quan, amt) select 2, gsc04c, 1,case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gna92f,0) * gsc05f / bill_total_amt end from #tbl_gnm01_gnm02 b,gsm03 a where b.gna00c = a.gsc00c and b.gna01c = a.gsc01c and a.gsc02c = 'gn' and (b.gna83c between @fromdepart and @todepart or @fromdepart = '*') and isnull(gna44c,'') <> '' -- insert #tbl_item_group(flag, payment, quan, amt) -- select 2, b.gnb19c, isnull(gna31f, 0) * dbo.fun_divide_default(b.gnb20f, isnull(b.gnb16f, 0) + isnull(b.gnb18f, 0) + isnull(b.gnb20f, 0) + isnull(b.gnb22f, 0), 0), isnull(gna31f, 0) * b.gnb20f -- from #tbl_gnm01_gnm02 b -- where (b.gna83c between @fromdepart and @todepart or @fromdepart = '*') -- and b.gnb19c is not null -- and isnull(b.gnb20f, 0) > 0 -- -- insert #tbl_item_group(flag, payment, quan, amt) -- select 2, b.gnb21c, isnull(gna31f, 0) * dbo.fun_divide_default(b.gnb22f, isnull(b.gnb16f, 0) + isnull(b.gnb18f, 0) + isnull(b.gnb20f, 0) + isnull(b.gnb22f, 0), 0), isnull(gna31f, 0) * b.gnb22f -- from #tbl_gnm01_gnm02 b -- where (b.gna83c between @fromdepart and @todepart or @fromdepart = '*') -- and b.gnb21c is not null -- and isnull(b.gnb22f, 0) > 0 -- -- -- 4 -- insert #tbl_item_group(flag, payment, quan, amt) -- select 2, b.gnb15c, isnull(gna33f, 0) * dbo.fun_divide_default(b.gnb16f, isnull(b.gnb16f, 0) + isnull(b.gnb18f, 0) + isnull(b.gnb20f, 0) + isnull(b.gnb22f, 0), 0), isnull(gna33f, 0) * b.gnb16f -- from #tbl_gnm01_gnm02 b -- where (b.gna100c between @fromdepart and @todepart or @fromdepart = '*') -- and b.gnb15c is not null -- and isnull(b.gnb16f, 0) > 0 -- -- insert #tbl_item_group(flag, payment, quan, amt) -- select 2, b.gnb17c, isnull(gna33f, 0) * dbo.fun_divide_default(b.gnb18f, isnull(b.gnb16f, 0) + isnull(b.gnb18f, 0) + isnull(b.gnb20f, 0) + isnull(b.gnb22f, 0), 0), isnull(gna33f, 0) * b.gnb18f -- from #tbl_gnm01_gnm02 b -- where (b.gna100c between @fromdepart and @todepart or @fromdepart = '*') -- and b.gnb17c is not null -- and isnull(b.gnb18f, 0) > 0 -- -- insert #tbl_item_group(flag, payment, quan, amt) -- select 2, b.gnb19c, isnull(gna33f, 0) * dbo.fun_divide_default(b.gnb20f, isnull(b.gnb16f, 0) + isnull(b.gnb18f, 0) + isnull(b.gnb20f, 0) + isnull(b.gnb22f, 0), 0), isnull(gna33f, 0) * b.gnb20f -- from #tbl_gnm01_gnm02 b -- where (b.gna100c between @fromdepart and @todepart or @fromdepart = '*') -- and b.gnb19c is not null -- and isnull(b.gnb20f, 0) > 0 -- -- insert #tbl_item_group(flag, payment, quan, amt) -- select 2, b.gnb21c, isnull(gna33f, 0) * dbo.fun_divide_default(b.gnb22f, isnull(b.gnb16f, 0) + isnull(b.gnb18f, 0) + isnull(b.gnb20f, 0) + isnull(b.gnb22f, 0), 0), isnull(gna33f, 0) * b.gnb22f -- from #tbl_gnm01_gnm02 b -- where (b.gna100c between @fromdepart and @todepart or @fromdepart = '*') -- and b.gnb21c is not null -- and isnull(b.gnb22f, 0) > 0 -- -- -- 5 -- insert #tbl_item_group(flag, payment, quan, amt) -- select 2, b.gnb15c, isnull(gna35f, 0) * dbo.fun_divide_default(b.gnb16f, isnull(b.gnb16f, 0) + isnull(b.gnb18f, 0) + isnull(b.gnb20f, 0) + isnull(b.gnb22f, 0), 0), isnull(gna35f, 0) * b.gnb16f -- from #tbl_gnm01_gnm02 b -- where (b.gna101c between @fromdepart and @todepart or @fromdepart = '*') -- and b.gnb15c is not null -- and isnull(b.gnb16f, 0) > 0 -- -- insert #tbl_item_group(flag, payment, quan, amt) -- select 2, b.gnb17c, isnull(gna35f, 0) * dbo.fun_divide_default(b.gnb18f, isnull(b.gnb16f, 0) + isnull(b.gnb18f, 0) + isnull(b.gnb20f, 0) + isnull(b.gnb22f, 0), 0), isnull(gna35f, 0) * b.gnb18f -- from #tbl_gnm01_gnm02 b -- where (b.gna101c between @fromdepart and @todepart or @fromdepart = '*') -- and b.gnb17c is not null -- and isnull(b.gnb18f, 0) > 0 -- -- insert #tbl_item_group(flag, payment, quan, amt) -- select 2, b.gnb19c, isnull(gna35f, 0) * dbo.fun_divide_default(b.gnb20f, isnull(b.gnb16f, 0) + isnull(b.gnb18f, 0) + isnull(b.gnb20f, 0) + isnull(b.gnb22f, 0), 0), isnull(gna35f, 0) * b.gnb20f -- from #tbl_gnm01_gnm02 b -- where (b.gna101c between @fromdepart and @todepart or @fromdepart = '*') -- and b.gnb19c is not null -- and isnull(b.gnb20f, 0) > 0 -- -- insert #tbl_item_group(flag, payment, quan, amt) -- select 2, b.gnb21c, isnull(gna35f, 0) * dbo.fun_divide_default(b.gnb22f, isnull(b.gnb16f, 0) + isnull(b.gnb18f, 0) + isnull(b.gnb20f, 0) + isnull(b.gnb22f, 0), 0), isnull(gna35f, 0) * b.gnb22f -- from #tbl_gnm01_gnm02 b -- where (b.gna101c between @fromdepart and @todepart or @fromdepart = '*') -- and b.gnb21c is not null -- and isnull(b.gnb22f, 0) > 0 -- -- -- 6 -- insert #tbl_item_group(flag, payment, quan, amt) -- select 2, b.gnb15c, isnull(gna37f, 0) * dbo.fun_divide_default(b.gnb16f, isnull(b.gnb16f, 0) + isnull(b.gnb18f, 0) + isnull(b.gnb20f, 0) + isnull(b.gnb22f, 0), 0), isnull(gna37f, 0) * b.gnb16f -- from #tbl_gnm01_gnm02 b -- where (b.gna102c between @fromdepart and @todepart or @fromdepart = '*') -- and b.gnb15c is not null -- and isnull(b.gnb16f, 0) > 0 -- -- insert #tbl_item_group(flag, payment, quan, amt) -- select 2, b.gnb17c, isnull(gna37f, 0) * dbo.fun_divide_default(b.gnb18f, isnull(b.gnb16f, 0) + isnull(b.gnb18f, 0) + isnull(b.gnb20f, 0) + isnull(b.gnb22f, 0), 0), isnull(gna37f, 0) * b.gnb18f -- from #tbl_gnm01_gnm02 b -- where (b.gna102c between @fromdepart and @todepart or @fromdepart = '*') -- and b.gnb17c is not null -- and isnull(b.gnb18f, 0) > 0 -- -- insert #tbl_item_group(flag, payment, quan, amt) -- select 2, b.gnb19c, isnull(gna37f, 0) * dbo.fun_divide_default(b.gnb20f, isnull(b.gnb16f, 0) + isnull(b.gnb18f, 0) + isnull(b.gnb20f, 0) + isnull(b.gnb22f, 0), 0), isnull(gna37f, 0) * b.gnb20f -- from #tbl_gnm01_gnm02 b -- where (b.gna102c between @fromdepart and @todepart or @fromdepart = '*') -- and b.gnb19c is not null -- and isnull(b.gnb20f, 0) > 0 -- -- insert #tbl_item_group(flag, payment, quan, amt) -- select 2, b.gnb21c, isnull(gna37f, 0) * dbo.fun_divide_default(b.gnb22f, isnull(b.gnb16f, 0) + isnull(b.gnb18f, 0) + isnull(b.gnb20f, 0) + isnull(b.gnb22f, 0), 0), isnull(gna37f, 0) * b.gnb22f -- from #tbl_gnm01_gnm02 b -- where (b.gna102c between @fromdepart and @todepart or @fromdepart = '*') -- and b.gnb21c is not null -- and isnull(b.gnb22f, 0) > 0 -- -- -- 7 -- insert #tbl_item_group(flag, payment, quan, amt) -- select 2, b.gnb15c, isnull(gna39f, 0) * dbo.fun_divide_default(b.gnb16f, isnull(b.gnb16f, 0) + isnull(b.gnb18f, 0) + isnull(b.gnb20f, 0) + isnull(b.gnb22f, 0), 0), isnull(gna39f, 0) * b.gnb16f -- from #tbl_gnm01_gnm02 b -- where (b.gna103c between @fromdepart and @todepart or @fromdepart = '*') -- and b.gnb15c is not null -- and isnull(b.gnb16f, 0) > 0 -- -- insert #tbl_item_group(flag, payment, quan, amt) -- select 2, b.gnb17c, isnull(gna39f, 0) * dbo.fun_divide_default(b.gnb18f, isnull(b.gnb16f, 0) + isnull(b.gnb18f, 0) + isnull(b.gnb20f, 0) + isnull(b.gnb22f, 0), 0), isnull(gna39f, 0) * b.gnb18f -- from #tbl_gnm01_gnm02 b -- where (b.gna103c between @fromdepart and @todepart or @fromdepart = '*') -- and b.gnb17c is not null -- and isnull(b.gnb18f, 0) > 0 -- -- insert #tbl_item_group(flag, payment, quan, amt) -- select 2, b.gnb19c, isnull(gna39f, 0) * dbo.fun_divide_default(b.gnb20f, isnull(b.gnb16f, 0) + isnull(b.gnb18f, 0) + isnull(b.gnb20f, 0) + isnull(b.gnb22f, 0), 0), isnull(gna39f, 0) * b.gnb20f -- from #tbl_gnm01_gnm02 b -- where (b.gna103c between @fromdepart and @todepart or @fromdepart = '*') -- and b.gnb19c is not null -- and isnull(b.gnb20f, 0) > 0 -- -- insert #tbl_item_group(flag, payment, quan, amt) -- select 2, b.gnb21c, isnull(gna39f, 0) * dbo.fun_divide_default(b.gnb22f, isnull(b.gnb16f, 0) + isnull(b.gnb18f, 0) + isnull(b.gnb20f, 0) + isnull(b.gnb22f, 0), 0), isnull(gna39f, 0) * b.gnb22f -- from #tbl_gnm01_gnm02 b -- where (b.gna103c between @fromdepart and @todepart or @fromdepart = '*') -- and b.gnb21c is not null -- and isnull(b.gnb22f, 0) > 0 -- -- -- 8 -- insert #tbl_item_group(flag, payment, quan, amt) -- select 2, b.gnb15c, isnull(gna41f, 0) * dbo.fun_divide_default(b.gnb16f, isnull(b.gnb16f, 0) + isnull(b.gnb18f, 0) + isnull(b.gnb20f, 0) + isnull(b.gnb22f, 0), 0), isnull(gna41f, 0) * b.gnb16f -- from #tbl_gnm01_gnm02 b -- where (b.gna104c between @fromdepart and @todepart or @fromdepart = '*') -- and b.gnb15c is not null -- and isnull(b.gnb16f, 0) > 0 -- -- insert #tbl_item_group(flag, payment, quan, amt) -- select 2, b.gnb17c, isnull(gna41f, 0) * dbo.fun_divide_default(b.gnb18f, isnull(b.gnb16f, 0) + isnull(b.gnb18f, 0) + isnull(b.gnb20f, 0) + isnull(b.gnb22f, 0), 0), isnull(gna41f, 0) * b.gnb18f -- from #tbl_gnm01_gnm02 b -- where (b.gna104c between @fromdepart and @todepart or @fromdepart = '*') -- and b.gnb17c is not null -- and isnull(b.gnb18f, 0) > 0 -- -- insert #tbl_item_group(flag, payment, quan, amt) -- select 2, b.gnb19c, isnull(gna41f, 0) * dbo.fun_divide_default(b.gnb20f, isnull(b.gnb16f, 0) + isnull(b.gnb18f, 0) + isnull(b.gnb20f, 0) + isnull(b.gnb22f, 0), 0), isnull(gna41f, 0) * b.gnb20f -- from #tbl_gnm01_gnm02 b -- where (b.gna104c between @fromdepart and @todepart or @fromdepart = '*') -- and b.gnb19c is not null -- and isnull(b.gnb20f, 0) > 0 -- -- insert #tbl_item_group(flag, payment, quan, amt) -- select 2, b.gnb21c, isnull(gna41f, 0) * dbo.fun_divide_default(b.gnb22f, isnull(b.gnb16f, 0) + isnull(b.gnb18f, 0) + isnull(b.gnb20f, 0) + isnull(b.gnb22f, 0), 0), isnull(gna41f, 0) * b.gnb22f -- from #tbl_gnm01_gnm02 b -- where (b.gna104c between @fromdepart and @todepart or @fromdepart = '*') -- and b.gnb21c is not null -- and isnull(b.gnb22f, 0) > 0 -- -- -- 9 -- insert #tbl_item_group(flag, payment, quan, amt) -- select 2, b.gnb15c, isnull(gna43f, 0) * dbo.fun_divide_default(b.gnb16f, isnull(b.gnb16f, 0) + isnull(b.gnb18f, 0) + isnull(b.gnb20f, 0) + isnull(b.gnb22f, 0), 0), isnull(gna43f, 0) * b.gnb16f -- from #tbl_gnm01_gnm02 b -- where (b.gna105c between @fromdepart and @todepart or @fromdepart = '*') -- and b.gnb15c is not null -- and isnull(b.gnb16f, 0) > 0 -- -- insert #tbl_item_group(flag, payment, quan, amt) -- select 2, b.gnb17c, isnull(gna43f, 0) * dbo.fun_divide_default(b.gnb18f, isnull(b.gnb16f, 0) + isnull(b.gnb18f, 0) + isnull(b.gnb20f, 0) + isnull(b.gnb22f, 0), 0), isnull(gna43f, 0) * b.gnb18f -- from #tbl_gnm01_gnm02 b -- where (b.gna105c between @fromdepart and @todepart or @fromdepart = '*') -- and b.gnb17c is not null -- and isnull(b.gnb18f, 0) > 0 -- -- insert #tbl_item_group(flag, payment, quan, amt) -- select 2, b.gnb19c, isnull(gna43f, 0) * dbo.fun_divide_default(b.gnb20f, isnull(b.gnb16f, 0) + isnull(b.gnb18f, 0) + isnull(b.gnb20f, 0) + isnull(b.gnb22f, 0), 0), isnull(gna43f, 0) * b.gnb20f -- from #tbl_gnm01_gnm02 b -- where (b.gna105c between @fromdepart and @todepart or @fromdepart = '*') -- and b.gnb19c is not null -- and isnull(b.gnb20f, 0) > 0 -- -- insert #tbl_item_group(flag, payment, quan, amt) -- select 2, b.gnb21c, isnull(gna43f, 0) * dbo.fun_divide_default(b.gnb22f, isnull(b.gnb16f, 0) + isnull(b.gnb18f, 0) + isnull(b.gnb20f, 0) + isnull(b.gnb22f, 0), 0), isnull(gna43f, 0) * b.gnb22f -- from #tbl_gnm01_gnm02 b -- where (b.gna105c between @fromdepart and @todepart or @fromdepart = '*') -- and b.gnb21c is not null -- and isnull(b.gnb22f, 0) > 0 -- -- -- 10 -- insert #tbl_item_group(flag, payment, quan, amt) -- select 2, b.gnb15c, isnull(gna45f, 0) * dbo.fun_divide_default(b.gnb16f, isnull(b.gnb16f, 0) + isnull(b.gnb18f, 0) + isnull(b.gnb20f, 0) + isnull(b.gnb22f, 0), 0), isnull(gna45f, 0) * b.gnb16f -- from #tbl_gnm01_gnm02 b -- where (b.gna106c between @fromdepart and @todepart or @fromdepart = '*') -- and b.gnb15c is not null -- and isnull(b.gnb16f, 0) > 0 -- -- insert #tbl_item_group(flag, payment, quan, amt) -- select 2, b.gnb17c, isnull(gna45f, 0) * dbo.fun_divide_default(b.gnb18f, isnull(b.gnb16f, 0) + isnull(b.gnb18f, 0) + isnull(b.gnb20f, 0) + isnull(b.gnb22f, 0), 0), isnull(gna45f, 0) * b.gnb18f -- from #tbl_gnm01_gnm02 b -- where (b.gna106c between @fromdepart and @todepart or @fromdepart = '*') -- and b.gnb17c is not null -- and isnull(b.gnb18f, 0) > 0 -- -- insert #tbl_item_group(flag, payment, quan, amt) -- select 2, b.gnb19c, isnull(gna45f, 0) * dbo.fun_divide_default(b.gnb20f, isnull(b.gnb16f, 0) + isnull(b.gnb18f, 0) + isnull(b.gnb20f, 0) + isnull(b.gnb22f, 0), 0), isnull(gna45f, 0) * b.gnb20f -- from #tbl_gnm01_gnm02 b -- where (b.gna106c between @fromdepart and @todepart or @fromdepart = '*') -- and b.gnb19c is not null -- and isnull(b.gnb20f, 0) > 0 -- -- insert #tbl_item_group(flag, payment, quan, amt) -- select 2, b.gnb21c, isnull(gna45f, 0) * dbo.fun_divide_default(b.gnb22f, isnull(b.gnb16f, 0) + isnull(b.gnb18f, 0) + isnull(b.gnb20f, 0) + isnull(b.gnb22f, 0), 0), isnull(gna45f, 0) * b.gnb22f -- from #tbl_gnm01_gnm02 b -- where (b.gna106c between @fromdepart and @todepart or @fromdepart = '*') -- and b.gnb21c is not null -- and isnull(b.gnb22f, 0) > 0 drop table #tbl_gnm01_gnm02 -- 这段时间内的冲值 create table #tbl_gcm10_buf ( gcl00c varchar(10) not null, --门店编号 gcl01c varchar(20) not null, --异动单号 gcl06i int null, -- 异动类别( 0充值, 1取款 5欠款 6还款) gcl16f float null, -- 第一销售分享比率1 gcl18f float null, -- 分享比率2 gcl20f float null, -- 分享比率3 gcl23f float null, -- 分享比率4 gcl25f float null, -- 分享比率5 gcl27f float null, -- 分享比率6 gcl29f float null, -- 分享比率7 gcl31f float null, -- 分享比率8 gcl33f float null, -- 分享比率9 gcl35f float null, -- 分享比率10 gcl81c varchar(10) null, -- 部门编号 gcl82c varchar(10) null, -- 部门编号2 gcl83c varchar(10) null, -- 部门编号3 gcl100c varchar(10) null, -- 部门编号4 gcl101c varchar(10) null, -- 部门编号5 gcl102c varchar(10) null, -- 部门编号6 gcl103c varchar(10) null, -- 部门编号7 gcl104c varchar(10) null, -- 部门编号8 gcl105c varchar(10) null, -- 部门编号9 gcl106c varchar(10) null, -- 部门编号10 gcl67c varchar(5) null, -- 支付方式1 gcl68f float null, -- 支付方式1金额 gcl69c varchar(5) null, -- 支付方式2 gcl70f float null, -- 支付方式2金额 gcl71c varchar(5) null, -- 支付方式3 gcl72f float null, -- 支付方式3金额 gcl73c varchar(5) null, -- 支付方式4 gcl74f float null, -- 支付方式4金额 gcl58f float null, gcl60f float null, gcl62f float null, gcl83f float null, gcl84f float null, gcl85f float null, gcl86f float null, gcl87f float null, gcl88f float null, gcl89f float null, gcl07f float null, bill_total_amt float null, gcl09c varchar(20) null, --销售人员 gcl17c varchar(20) null, --第二销售 gcl19c varchar(20) null, --第三负责人 gcl22c varchar(20) null, --第4负责人 gcl24c varchar(20) null, --第5负责人 gcl26c varchar(20) null, --第6负责人 gcl28c varchar(20) null, --第7负责人 gcl30c varchar(20) null, --第8负责人 gcl32c varchar(20) null, --第9负责人 gcl34c varchar(20) null --第10负责人 ) insert #tbl_gcm10_buf(gcl00c,gcl01c,gcl06i, gcl16f, gcl18f, gcl20f, gcl23f, gcl25f, gcl27f, gcl29f, gcl31f, gcl33f, gcl35f, gcl81c, gcl82c, gcl83c, gcl100c, gcl101c, gcl102c, gcl103c, gcl104c, gcl105c, gcl106c, gcl67c, gcl68f, gcl69c, gcl70f, gcl71c, gcl72f, gcl73c, gcl74f, gcl58f, gcl60f, gcl62f, gcl83f, gcl84f, gcl85f, gcl86f, gcl87f, gcl88f, gcl89f, gcl07f, bill_total_amt, gcl09c,gcl17c,gcl19c,gcl22c,gcl24c,gcl26c,gcl28c,gcl30c,gcl32c,gcl34c ) select gcl00c,gcl01c,gcl06i, gcl16f, gcl18f, gcl20f, gcl23f, gcl25f, gcl27f, gcl29f, gcl31f, gcl33f, gcl35f, gcl81c, gcl82c, gcl83c, gcl100c, gcl101c, gcl102c, gcl103c, gcl104c, gcl105c, gcl106c, gcl67c, gcl68f, gcl69c, gcl70f, gcl71c, gcl72f, gcl73c, gcl74f, gcl58f, gcl60f, gcl62f, gcl83f, gcl84f, gcl85f, gcl86f, gcl87f, gcl88f, gcl89f, gcl07f, isnull(gcl58f, 0) + isnull(gcl60f, 0) + isnull(gcl62f, 0) + isnull(gcl83f, 0) + isnull(gcl84f, 0) + isnull(gcl85f, 0) + isnull(gcl86f, 0) + isnull(gcl87f, 0) + isnull(gcl88f, 0) + isnull(gcl89f, 0), gcl09c,gcl17c,gcl19c,gcl22c,gcl24c,gcl26c,gcl28c,gcl30c,gcl32c,gcl34c from gcm10 a with (nolock), gam26 z with (nolock) where gcl00c = z.gaz02c and z.gaz01c = @compid and a.gcl80d >= @fromdate and a.gcl80d <= @todate and gcl93c like @staff and isnull(gcl03c, '') <> '' -- 1 insert #tbl_item_group(flag, payment, quan, amt) select 2, gsc04c,1, case when isnull(bill_total_amt,0) =0 then 0 else isnull(gcl58f,0) * gsc05f / bill_total_amt end from #tbl_gcm10_buf a,gsm03 b where a.gcl00c = b.gsc00c and a.gcl01c = b.gsc01c and gsc02c= 'gz' and (gcl06i = 0 or gcl06i = 6) and (a.gcl81c between @fromdepart and @todepart or @fromdepart = '*') and a.gcl09c <> '' insert #tbl_item_group(flag, payment, quan, amt) select 2, gsc04c, 1,case when isnull(bill_total_amt,0) =0 then 0 else isnull(gcl60f,0) * gsc05f / bill_total_amt end from #tbl_gcm10_buf a,gsm03 b where a.gcl00c = b.gsc00c and a.gcl01c = b.gsc01c and gsc02c= 'gz' and (gcl06i = 0 or gcl06i = 6) and (a.gcl81c between @fromdepart and @todepart or @fromdepart = '*') and a.gcl17c <> '' insert #tbl_item_group(flag, payment, quan, amt) select 2, gsc04c, 1,case when isnull(bill_total_amt,0) =0 then 0 else isnull(gcl62f,0) * gsc05f / bill_total_amt end from #tbl_gcm10_buf a,gsm03 b where a.gcl00c = b.gsc00c and a.gcl01c = b.gsc01c and gsc02c= 'gz' and (gcl06i = 0 or gcl06i = 6) and (a.gcl81c between @fromdepart and @todepart or @fromdepart = '*') and a.gcl19c <> '' insert #tbl_item_group(flag, payment, quan, amt) select 2, gsc04c,1,case when isnull(bill_total_amt,0) =0 then 0 else isnull(gcl83f,0) * gsc05f / bill_total_amt end from #tbl_gcm10_buf a,gsm03 b where a.gcl00c = b.gsc00c and a.gcl01c = b.gsc01c and gsc02c= 'gz' and (gcl06i = 0 or gcl06i = 6) and (a.gcl81c between @fromdepart and @todepart or @fromdepart = '*') and a.gcl22c <> '' -- 2 insert #tbl_item_group(flag, payment, quan, amt) select 2, gsc04c, 1,case when isnull(bill_total_amt,0) =0 then 0 else isnull(gcl84f,0) * gsc05f / bill_total_amt end from #tbl_gcm10_buf a,gsm03 b where a.gcl00c = b.gsc00c and a.gcl01c = b.gsc01c and gsc02c= 'gz' and (gcl06i = 0 or gcl06i = 6) and (a.gcl82c between @fromdepart and @todepart or @fromdepart = '*') and a.gcl24c <>'' insert #tbl_item_group(flag, payment, quan, amt) select 2, gsc04c, 1,case when isnull(bill_total_amt,0) =0 then 0 else isnull(gcl85f,0) * gsc05f / bill_total_amt end from #tbl_gcm10_buf a,gsm03 b where a.gcl00c = b.gsc00c and a.gcl01c = b.gsc01c and gsc02c= 'gz' and (gcl06i = 0 or gcl06i = 6) and (a.gcl82c between @fromdepart and @todepart or @fromdepart = '*') and a.gcl26c <>'' insert #tbl_item_group(flag, payment, quan, amt) select 2, gsc04c, 1,case when isnull(bill_total_amt,0) =0 then 0 else isnull(gcl86f,0) * gsc05f / bill_total_amt end from #tbl_gcm10_buf a,gsm03 b where a.gcl00c = b.gsc00c and a.gcl01c = b.gsc01c and gsc02c= 'gz' and (gcl06i = 0 or gcl06i = 6) and (a.gcl82c between @fromdepart and @todepart or @fromdepart = '*') and a.gcl28c <>'' insert #tbl_item_group(flag, payment, quan, amt) select 2, gsc04c,1, case when isnull(bill_total_amt,0) =0 then 0 else isnull(gcl87f,0) * gsc05f / bill_total_amt end from #tbl_gcm10_buf a,gsm03 b where a.gcl00c = b.gsc00c and a.gcl01c = b.gsc01c and gsc02c= 'gz' and (gcl06i = 0 or gcl06i = 6) and (a.gcl82c between @fromdepart and @todepart or @fromdepart = '*') and a.gcl30c <>'' -- 3 insert #tbl_item_group(flag, payment, quan, amt) select 2, gsc04c,1,case when isnull(bill_total_amt,0) =0 then 0 else isnull(gcl88f,0) * gsc05f / bill_total_amt end from #tbl_gcm10_buf a,gsm03 b where a.gcl00c = b.gsc00c and a.gcl01c = b.gsc01c and gsc02c= 'gz' and (gcl06i = 0 or gcl06i = 6) and (a.gcl83c between @fromdepart and @todepart or @fromdepart = '*') and a.gcl32c <>'' insert #tbl_item_group(flag, payment, quan, amt) select 2, a.gcl69c,1,case when isnull(bill_total_amt,0) =0 then 0 else isnull(gcl89f,0) * gsc05f / bill_total_amt end from #tbl_gcm10_buf a,gsm03 b where a.gcl00c = b.gsc00c and a.gcl01c = b.gsc01c and gsc02c= 'gz' and (gcl06i = 0 or gcl06i = 6) and (a.gcl83c between @fromdepart and @todepart or @fromdepart = '*') and a.gcl34c <>'' -- insert #tbl_item_group(flag, payment, quan, amt) -- select 2, a.gcl71c, isnull(gcl20f, 0) * dbo.fun_divide_default(a.gcl72f, isnull(a.gcl68f, 0) + isnull(a.gcl70f, 0) + isnull(a.gcl72f, 0) + isnull(a.gcl74f, 0), 0), isnull(gcl20f, 0) * a.gcl72f -- from #tbl_gcm10_buf a -- where (gcl06i = 0 or gcl06i = 6) -- and (a.gcl83c between @fromdepart and @todepart or @fromdepart = '*') -- and a.gcl71c is not null -- and isnull(a.gcl72f, 0) > 0 -- -- insert #tbl_item_group(flag, payment, quan, amt) -- select 2, a.gcl73c, isnull(gcl20f, 0) * dbo.fun_divide_default(a.gcl74f, isnull(a.gcl68f, 0) + isnull(a.gcl70f, 0) + isnull(a.gcl72f, 0) + isnull(a.gcl74f, 0), 0), isnull(gcl20f, 0) * a.gcl74f -- from #tbl_gcm10_buf a -- where (gcl06i = 0 or gcl06i = 6) -- and (a.gcl83c between @fromdepart and @todepart or @fromdepart = '*') -- and a.gcl73c is not null -- and isnull(a.gcl74f, 0) > 0 -- -- -- 4 -- insert #tbl_item_group(flag, payment, quan, amt) -- select 2, a.gcl67c, isnull(gcl23f, 0) * dbo.fun_divide_default(a.gcl68f, isnull(a.gcl68f, 0) + isnull(a.gcl70f, 0) + isnull(a.gcl72f, 0) + isnull(a.gcl74f, 0), 0), isnull(gcl23f, 0) * a.gcl68f -- from #tbl_gcm10_buf a -- where (gcl06i = 0 or gcl06i = 6) -- and (a.gcl100c between @fromdepart and @todepart or @fromdepart = '*') -- and a.gcl67c is not null -- and isnull(a.gcl68f, 0) > 0 -- -- insert #tbl_item_group(flag, payment, quan, amt) -- select 2, a.gcl69c, isnull(gcl23f, 0) * dbo.fun_divide_default(a.gcl70f, isnull(a.gcl68f, 0) + isnull(a.gcl70f, 0) + isnull(a.gcl72f, 0) + isnull(a.gcl74f, 0), 0), isnull(gcl23f, 0) * a.gcl70f -- from #tbl_gcm10_buf a -- where (gcl06i = 0 or gcl06i = 6) -- and (a.gcl100c between @fromdepart and @todepart or @fromdepart = '*') -- and a.gcl69c is not null -- and isnull(a.gcl70f, 0) > 0 -- -- insert #tbl_item_group(flag, payment, quan, amt) -- select 2, a.gcl71c, isnull(gcl23f, 0) * dbo.fun_divide_default(a.gcl72f, isnull(a.gcl68f, 0) + isnull(a.gcl70f, 0) + isnull(a.gcl72f, 0) + isnull(a.gcl74f, 0), 0), isnull(gcl23f, 0) * a.gcl72f -- from #tbl_gcm10_buf a -- where (gcl06i = 0 or gcl06i = 6) -- and (a.gcl100c between @fromdepart and @todepart or @fromdepart = '*') -- and a.gcl71c is not null -- and isnull(a.gcl72f, 0) > 0 -- -- insert #tbl_item_group(flag, payment, quan, amt) -- select 2, a.gcl73c, isnull(gcl23f, 0) * dbo.fun_divide_default(a.gcl74f, isnull(a.gcl68f, 0) + isnull(a.gcl70f, 0) + isnull(a.gcl72f, 0) + isnull(a.gcl74f, 0), 0), isnull(gcl23f, 0) * a.gcl74f -- from #tbl_gcm10_buf a -- where (gcl06i = 0 or gcl06i = 6) -- and (a.gcl100c between @fromdepart and @todepart or @fromdepart = '*') -- and a.gcl73c is not null -- and isnull(a.gcl74f, 0) > 0 -- -- -- 5 -- insert #tbl_item_group(flag, payment, quan, amt) -- select 2, a.gcl67c, isnull(gcl25f, 0) * dbo.fun_divide_default(a.gcl68f, isnull(a.gcl68f, 0) + isnull(a.gcl70f, 0) + isnull(a.gcl72f, 0) + isnull(a.gcl74f, 0), 0), isnull(gcl25f, 0) * a.gcl68f -- from #tbl_gcm10_buf a -- where (gcl06i = 0 or gcl06i = 6) -- and (a.gcl101c between @fromdepart and @todepart or @fromdepart = '*') -- and a.gcl67c is not null -- and isnull(a.gcl68f, 0) > 0 -- -- insert #tbl_item_group(flag, payment, quan, amt) -- select 2, a.gcl69c, isnull(gcl25f, 0) * dbo.fun_divide_default(a.gcl70f, isnull(a.gcl68f, 0) + isnull(a.gcl70f, 0) + isnull(a.gcl72f, 0) + isnull(a.gcl74f, 0), 0), isnull(gcl25f, 0) * a.gcl70f -- from #tbl_gcm10_buf a -- where (gcl06i = 0 or gcl06i = 6) -- and (a.gcl101c between @fromdepart and @todepart or @fromdepart = '*') -- and a.gcl69c is not null -- and isnull(a.gcl70f, 0) > 0 -- -- insert #tbl_item_group(flag, payment, quan, amt) -- select 2, a.gcl71c, isnull(gcl25f, 0) * dbo.fun_divide_default(a.gcl72f, isnull(a.gcl68f, 0) + isnull(a.gcl70f, 0) + isnull(a.gcl72f, 0) + isnull(a.gcl74f, 0), 0), isnull(gcl25f, 0) * a.gcl72f -- from #tbl_gcm10_buf a -- where (gcl06i = 0 or gcl06i = 6) -- and (a.gcl101c between @fromdepart and @todepart or @fromdepart = '*') -- and a.gcl71c is not null -- and isnull(a.gcl72f, 0) > 0 -- -- insert #tbl_item_group(flag, payment, quan, amt) -- select 2, a.gcl73c, isnull(gcl25f, 0) * dbo.fun_divide_default(a.gcl74f, isnull(a.gcl68f, 0) + isnull(a.gcl70f, 0) + isnull(a.gcl72f, 0) + isnull(a.gcl74f, 0), 0), isnull(gcl25f, 0) * a.gcl74f -- from #tbl_gcm10_buf a -- where (gcl06i = 0 or gcl06i = 6) -- and (a.gcl101c between @fromdepart and @todepart or @fromdepart = '*') -- and a.gcl73c is not null -- and isnull(a.gcl74f, 0) > 0 -- -- -- 6 -- insert #tbl_item_group(flag, payment, quan, amt) -- select 2, a.gcl67c, isnull(gcl27f, 0) * dbo.fun_divide_default(a.gcl68f, isnull(a.gcl68f, 0) + isnull(a.gcl70f, 0) + isnull(a.gcl72f, 0) + isnull(a.gcl74f, 0), 0), isnull(gcl27f, 0) * a.gcl68f -- from #tbl_gcm10_buf a -- where (gcl06i = 0 or gcl06i = 6) -- and (a.gcl102c between @fromdepart and @todepart or @fromdepart = '*') -- and a.gcl67c is not null -- and isnull(a.gcl68f, 0) > 0 -- -- insert #tbl_item_group(flag, payment, quan, amt) -- select 2, a.gcl69c, isnull(gcl27f, 0) * dbo.fun_divide_default(a.gcl70f, isnull(a.gcl68f, 0) + isnull(a.gcl70f, 0) + isnull(a.gcl72f, 0) + isnull(a.gcl74f, 0), 0), isnull(gcl27f, 0) * a.gcl70f -- from #tbl_gcm10_buf a -- where (gcl06i = 0 or gcl06i = 6) -- and (a.gcl102c between @fromdepart and @todepart or @fromdepart = '*') -- and a.gcl69c is not null -- and isnull(a.gcl70f, 0) > 0 -- -- insert #tbl_item_group(flag, payment, quan, amt) -- select 2, a.gcl71c, isnull(gcl27f, 0) * dbo.fun_divide_default(a.gcl72f, isnull(a.gcl68f, 0) + isnull(a.gcl70f, 0) + isnull(a.gcl72f, 0) + isnull(a.gcl74f, 0), 0), isnull(gcl27f, 0) * a.gcl72f -- from #tbl_gcm10_buf a -- where (gcl06i = 0 or gcl06i = 6) -- and (a.gcl102c between @fromdepart and @todepart or @fromdepart = '*') -- and a.gcl71c is not null -- and isnull(a.gcl72f, 0) > 0 -- -- insert #tbl_item_group(flag, payment, quan, amt) -- select 2, a.gcl73c, isnull(gcl27f, 0) * dbo.fun_divide_default(a.gcl74f, isnull(a.gcl68f, 0) + isnull(a.gcl70f, 0) + isnull(a.gcl72f, 0) + isnull(a.gcl74f, 0), 0), isnull(gcl27f, 0) * a.gcl74f -- from #tbl_gcm10_buf a -- where (gcl06i = 0 or gcl06i = 6) -- and (a.gcl102c between @fromdepart and @todepart or @fromdepart = '*') -- and a.gcl73c is not null -- and isnull(a.gcl74f, 0) > 0 -- -- -- 7 -- insert #tbl_item_group(flag, payment, quan, amt) -- select 2, a.gcl67c, isnull(gcl29f, 0) * dbo.fun_divide_default(a.gcl68f, isnull(a.gcl68f, 0) + isnull(a.gcl70f, 0) + isnull(a.gcl72f, 0) + isnull(a.gcl74f, 0), 0), isnull(gcl29f, 0) * a.gcl68f -- from #tbl_gcm10_buf a -- where (gcl06i = 0 or gcl06i = 6) -- and (a.gcl103c between @fromdepart and @todepart or @fromdepart = '*') -- and a.gcl67c is not null -- and isnull(a.gcl68f, 0) > 0 -- -- insert #tbl_item_group(flag, payment, quan, amt) -- select 2, a.gcl69c, isnull(gcl29f, 0) * dbo.fun_divide_default(a.gcl70f, isnull(a.gcl68f, 0) + isnull(a.gcl70f, 0) + isnull(a.gcl72f, 0) + isnull(a.gcl74f, 0), 0), isnull(gcl29f, 0) * a.gcl70f -- from #tbl_gcm10_buf a -- where (gcl06i = 0 or gcl06i = 6) -- and (a.gcl103c between @fromdepart and @todepart or @fromdepart = '*') -- and a.gcl69c is not null -- and isnull(a.gcl70f, 0) > 0 -- -- insert #tbl_item_group(flag, payment, quan, amt) -- select 2, a.gcl71c, isnull(gcl29f, 0) * dbo.fun_divide_default(a.gcl72f, isnull(a.gcl68f, 0) + isnull(a.gcl70f, 0) + isnull(a.gcl72f, 0) + isnull(a.gcl74f, 0), 0), isnull(gcl29f, 0) * a.gcl72f -- from #tbl_gcm10_buf a -- where (gcl06i = 0 or gcl06i = 6) -- and (a.gcl103c between @fromdepart and @todepart or @fromdepart = '*') -- and a.gcl71c is not null -- and isnull(a.gcl72f, 0) > 0 -- -- insert #tbl_item_group(flag, payment, quan, amt) -- select 2, a.gcl73c, isnull(gcl29f, 0) * dbo.fun_divide_default(a.gcl74f, isnull(a.gcl68f, 0) + isnull(a.gcl70f, 0) + isnull(a.gcl72f, 0) + isnull(a.gcl74f, 0), 0), isnull(gcl29f, 0) * a.gcl74f -- from #tbl_gcm10_buf a -- where (gcl06i = 0 or gcl06i = 6) -- and (a.gcl103c between @fromdepart and @todepart or @fromdepart = '*') -- and a.gcl73c is not null -- and isnull(a.gcl74f, 0) > 0 -- -- -- 8 -- insert #tbl_item_group(flag, payment, quan, amt) -- select 2, a.gcl67c, isnull(gcl31f, 0) * dbo.fun_divide_default(a.gcl68f, isnull(a.gcl68f, 0) + isnull(a.gcl70f, 0) + isnull(a.gcl72f, 0) + isnull(a.gcl74f, 0), 0), isnull(gcl31f, 0) * a.gcl68f -- from #tbl_gcm10_buf a -- where (gcl06i = 0 or gcl06i = 6) -- and (a.gcl104c between @fromdepart and @todepart or @fromdepart = '*') -- and a.gcl67c is not null -- and isnull(a.gcl68f, 0) > 0 -- -- insert #tbl_item_group(flag, payment, quan, amt) -- select 2, a.gcl69c, isnull(gcl31f, 0) * dbo.fun_divide_default(a.gcl70f, isnull(a.gcl68f, 0) + isnull(a.gcl70f, 0) + isnull(a.gcl72f, 0) + isnull(a.gcl74f, 0), 0), isnull(gcl31f, 0) * a.gcl70f -- from #tbl_gcm10_buf a -- where (gcl06i = 0 or gcl06i = 6) -- and (a.gcl104c between @fromdepart and @todepart or @fromdepart = '*') -- and a.gcl69c is not null -- and isnull(a.gcl70f, 0) > 0 -- -- insert #tbl_item_group(flag, payment, quan, amt) -- select 2, a.gcl71c, isnull(gcl31f, 0) * dbo.fun_divide_default(a.gcl72f, isnull(a.gcl68f, 0) + isnull(a.gcl70f, 0) + isnull(a.gcl72f, 0) + isnull(a.gcl74f, 0), 0), isnull(gcl31f, 0) * a.gcl72f -- from #tbl_gcm10_buf a -- where (gcl06i = 0 or gcl06i = 6) -- and (a.gcl104c between @fromdepart and @todepart or @fromdepart = '*') -- and a.gcl71c is not null -- and isnull(a.gcl72f, 0) > 0 -- -- insert #tbl_item_group(flag, payment, quan, amt) -- select 2, a.gcl73c, isnull(gcl31f, 0) * dbo.fun_divide_default(a.gcl74f, isnull(a.gcl68f, 0) + isnull(a.gcl70f, 0) + isnull(a.gcl72f, 0) + isnull(a.gcl74f, 0), 0), isnull(gcl31f, 0) * a.gcl74f -- from #tbl_gcm10_buf a -- where (gcl06i = 0 or gcl06i = 6) -- and (a.gcl104c between @fromdepart and @todepart or @fromdepart = '*') -- and a.gcl73c is not null -- and isnull(a.gcl74f, 0) > 0 -- -- -- 9 -- insert #tbl_item_group(flag, payment, quan, amt) -- select 2, a.gcl67c, isnull(gcl33f, 0) * dbo.fun_divide_default(a.gcl68f, isnull(a.gcl68f, 0) + isnull(a.gcl70f, 0) + isnull(a.gcl72f, 0) + isnull(a.gcl74f, 0), 0), isnull(gcl33f, 0) * a.gcl68f -- from #tbl_gcm10_buf a -- where (gcl06i = 0 or gcl06i = 6) -- and (a.gcl105c between @fromdepart and @todepart or @fromdepart = '*') -- and a.gcl67c is not null -- and isnull(a.gcl68f, 0) > 0 -- -- insert #tbl_item_group(flag, payment, quan, amt) -- select 2, a.gcl69c, isnull(gcl33f, 0) * dbo.fun_divide_default(a.gcl70f, isnull(a.gcl68f, 0) + isnull(a.gcl70f, 0) + isnull(a.gcl72f, 0) + isnull(a.gcl74f, 0), 0), isnull(gcl33f, 0) * a.gcl70f -- from #tbl_gcm10_buf a -- where (gcl06i = 0 or gcl06i = 6) -- and (a.gcl105c between @fromdepart and @todepart or @fromdepart = '*') -- and a.gcl69c is not null -- and isnull(a.gcl70f, 0) > 0 -- -- insert #tbl_item_group(flag, payment, quan, amt) -- select 2, a.gcl71c, isnull(gcl33f, 0) * dbo.fun_divide_default(a.gcl72f, isnull(a.gcl68f, 0) + isnull(a.gcl70f, 0) + isnull(a.gcl72f, 0) + isnull(a.gcl74f, 0), 0), isnull(gcl33f, 0) * a.gcl72f -- from #tbl_gcm10_buf a -- where (gcl06i = 0 or gcl06i = 6) -- and (a.gcl105c between @fromdepart and @todepart or @fromdepart = '*') -- and a.gcl71c is not null -- and isnull(a.gcl72f, 0) > 0 -- -- insert #tbl_item_group(flag, payment, quan, amt) -- select 2, a.gcl73c, isnull(gcl33f, 0) * dbo.fun_divide_default(a.gcl74f, isnull(a.gcl68f, 0) + isnull(a.gcl70f, 0) + isnull(a.gcl72f, 0) + isnull(a.gcl74f, 0), 0), isnull(gcl33f, 0) * a.gcl74f -- from #tbl_gcm10_buf a -- where (gcl06i = 0 or gcl06i = 6) -- and (a.gcl105c between @fromdepart and @todepart or @fromdepart = '*') -- and a.gcl73c is not null -- and isnull(a.gcl74f, 0) > 0 -- -- -- 10 -- insert #tbl_item_group(flag, payment, quan, amt) -- select 2, a.gcl67c, isnull(gcl35f, 0) * dbo.fun_divide_default(a.gcl68f, isnull(a.gcl68f, 0) + isnull(a.gcl70f, 0) + isnull(a.gcl72f, 0) + isnull(a.gcl74f, 0), 0), isnull(gcl35f, 0) * a.gcl68f -- from #tbl_gcm10_buf a -- where (gcl06i = 0 or gcl06i = 6) -- and (a.gcl106c between @fromdepart and @todepart or @fromdepart = '*') -- and a.gcl67c is not null -- and isnull(a.gcl68f, 0) > 0 -- -- insert #tbl_item_group(flag, payment, quan, amt) -- select 2, a.gcl69c, isnull(gcl35f, 0) * dbo.fun_divide_default(a.gcl70f, isnull(a.gcl68f, 0) + isnull(a.gcl70f, 0) + isnull(a.gcl72f, 0) + isnull(a.gcl74f, 0), 0), isnull(gcl35f, 0) * a.gcl70f -- from #tbl_gcm10_buf a -- where (gcl06i = 0 or gcl06i = 6) -- and (a.gcl106c between @fromdepart and @todepart or @fromdepart = '*') -- and a.gcl69c is not null -- and isnull(a.gcl70f, 0) > 0 -- -- insert #tbl_item_group(flag, payment, quan, amt) -- select 2, a.gcl71c, isnull(gcl35f, 0) * dbo.fun_divide_default(a.gcl72f, isnull(a.gcl68f, 0) + isnull(a.gcl70f, 0) + isnull(a.gcl72f, 0) + isnull(a.gcl74f, 0), 0), isnull(gcl35f, 0) * a.gcl72f -- from #tbl_gcm10_buf a -- where (gcl06i = 0 or gcl06i = 6) -- and (a.gcl106c between @fromdepart and @todepart or @fromdepart = '*') -- and a.gcl71c is not null -- and isnull(a.gcl72f, 0) > 0 -- -- insert #tbl_item_group(flag, payment, quan, amt) -- select 2, a.gcl73c, isnull(gcl35f, 0) * dbo.fun_divide_default(a.gcl74f, isnull(a.gcl68f, 0) + isnull(a.gcl70f, 0) + isnull(a.gcl72f, 0) + isnull(a.gcl74f, 0), 0), isnull(gcl35f, 0) * a.gcl74f -- from #tbl_gcm10_buf a -- where (gcl06i = 0 or gcl06i = 6) -- and (a.gcl106c between @fromdepart and @todepart or @fromdepart = '*') -- and a.gcl73c is not null -- and isnull(a.gcl74f, 0) > 0 -- add by lyj 把会员卡变更的金额也应该给加进去 -- 因为明细分类帐里面有一个记录 -- insert #tbl_item_group(flag, payment, quan, amt) -- select 2, gsc04c , 1, gsc05f -- from gem01 with (nolock), gsm03 with (nolock), gam26 z with (nolock) -- where gea00c = z.gaz02c -- and z.gaz01c = @compid -- and gsc00c = gea00c -- and gea01c = gsc01c -- and gsc02c = 'ge' -- and gsc04c in ('1', '6', 'ZFB', 'WX', @payment_0, @payment_3, @payment_$, @payment_u1, @payment_u2, @payment_u3, @payment_u4, @payment_u5) -- and gea80d between @fromdate and @todate -- and gea21c like @staff -- and (gea81c between @fromdepart and @todepart or @fromdepart = '*') --add by lyj 2007-09-06 --Start竞争转卡业绩 add by lm 2015-10-13 --第一销售 insert #tbl_item_group(flag, payment, quan, amt) select 2, gsc04c, 1, isnull(gea27f, 0)*gsc05f/(isnull(gea27f,0)+isnull(gea29f,0)+isnull(gea31f,0)+isnull(gea33f,0)+isnull(gea35f,0)+isnull(gea37f,0)+isnull(gea39f,0)+isnull(gea41f,0)+isnull(gea43f,0)+isnull(gea45f,0)) from gem01 a with (nolock), gsm03 b with(nolock), gam26 z with (nolock) where a.gea00c = b.gsc00c and a.gea01c = b.gsc01c and a.gea00c = z.gaz02c and z.gaz01c = @compid and gsc10d >= @fromdate and gsc10d <= @todate and gsc11c like @staff and b.gsc02c = 'ge' and (isnull(a.gea81c, '') between @fromdepart and @todepart or @fromdepart = '*') --add by lm 2015-10-13 and isnull(gea21c,'') <> '' --第二销售 insert #tbl_item_group(flag, payment, quan, amt) select 2, gsc04c, 1, isnull(gea29f, 0)*gsc05f/(isnull(gea27f,0)+isnull(gea29f,0)+isnull(gea31f,0)+isnull(gea33f,0)+isnull(gea35f,0)+isnull(gea37f,0)+isnull(gea39f,0)+isnull(gea41f,0)+isnull(gea43f,0)+isnull(gea45f,0)) from gem01 a with (nolock), gsm03 b with(nolock),gam26 z with (nolock) where a.gea00c = b.gsc00c and a.gea01c = b.gsc01c and a.gea00c = z.gaz02c and z.gaz01c = @compid and gsc10d >= @fromdate and gsc10d <= @todate and gsc11c like @staff and b.gsc02c = 'ge' and (isnull(a.gea82c, '') between @fromdepart and @todepart or @fromdepart = '*') --add by lm 2015-10-13 and isnull(gea28c,'') <> '' --第三销售 insert #tbl_item_group(flag, payment, quan, amt) select 2, gsc04c,1, isnull(gea31f, 0)*gsc05f/(isnull(gea27f,0)+isnull(gea29f,0)+isnull(gea31f,0)+isnull(gea33f,0)+isnull(gea35f,0)+isnull(gea37f,0)+isnull(gea39f,0)+isnull(gea41f,0)+isnull(gea43f,0)+isnull(gea45f,0)) from gem01 a with (nolock), gsm03 b with(nolock), gam26 z with (nolock) where a.gea00c = b.gsc00c and a.gea01c = b.gsc01c and a.gea00c = z.gaz02c and z.gaz01c = @compid and gsc10d >= @fromdate and gsc10d <= @todate and gsc11c like @staff and b.gsc02c = 'ge' and (isnull(a.gea83c, '') between @fromdepart and @todepart or @fromdepart = '*') --add by lm 2015-10-13 and isnull(gea30c,'') <> '' --第四销售 insert #tbl_item_group(flag, payment, quan, amt) select 2, gsc04c, 1, isnull(gea33f, 0)*gsc05f/(isnull(gea27f,0)+isnull(gea29f,0)+isnull(gea31f,0)+isnull(gea33f,0)+isnull(gea35f,0)+isnull(gea37f,0)+isnull(gea39f,0)+isnull(gea41f,0)+isnull(gea43f,0)+isnull(gea45f,0)) from gem01 a with (nolock), gsm03 b with(nolock), gam26 z with (nolock) where a.gea00c = b.gsc00c and a.gea01c = b.gsc01c and a.gea00c = z.gaz02c and z.gaz01c = @compid and gsc10d >= @fromdate and gsc10d <= @todate and gsc11c like @staff and b.gsc02c = 'ge' and (isnull(a.gea84c, '') between @fromdepart and @todepart or @fromdepart = '*') --add by lm 2015-10-13 and isnull(gea32c,'') <> '' --第五销售 insert #tbl_item_group(flag, payment, quan, amt) select 2, gsc04c, 1, isnull(gea35f, 0)*gsc05f/(isnull(gea27f,0)+isnull(gea29f,0)+isnull(gea31f,0)+isnull(gea33f,0)+isnull(gea35f,0)+isnull(gea37f,0)+isnull(gea39f,0)+isnull(gea41f,0)+isnull(gea43f,0)+isnull(gea45f,0)) from gem01 a with (nolock), gsm03 b with(nolock), gam26 z with (nolock) where a.gea00c = b.gsc00c and a.gea01c = b.gsc01c and a.gea00c = z.gaz02c and z.gaz01c = @compid and gsc10d >= @fromdate and gsc10d <= @todate and gsc11c like @staff and b.gsc02c = 'ge' and (isnull(a.gea85c, '') between @fromdepart and @todepart or @fromdepart = '*') --add by lm 2015-10-13 and isnull(gea34c,'') <> '' --第六销售 insert #tbl_item_group(flag, payment, quan, amt) select 2, gsc04c,1, isnull(gea37f, 0)*gsc05f/(isnull(gea27f,0)+isnull(gea29f,0)+isnull(gea31f,0)+isnull(gea33f,0)+isnull(gea35f,0)+isnull(gea37f,0)+isnull(gea39f,0)+isnull(gea41f,0)+isnull(gea43f,0)+isnull(gea45f,0)) from gem01 a with (nolock), gsm03 b with(nolock), gam26 z with (nolock) where a.gea00c = b.gsc00c and a.gea01c = b.gsc01c and a.gea00c = z.gaz02c and z.gaz01c = @compid and gsc10d >= @fromdate and gsc10d <= @todate and gsc11c like @staff and b.gsc02c = 'ge' and (isnull(a.gea86c, '') between @fromdepart and @todepart or @fromdepart = '*') --add by lm 2015-10-13 and isnull(gea36c,'') <> '' --第七销售 insert #tbl_item_group(flag, payment, quan, amt) select 2, gsc04c, 1, isnull(gea39f, 0)*gsc05f/(isnull(gea27f,0)+isnull(gea29f,0)+isnull(gea31f,0)+isnull(gea33f,0)+isnull(gea35f,0)+isnull(gea37f,0)+isnull(gea39f,0)+isnull(gea41f,0)+isnull(gea43f,0)+isnull(gea45f,0)) from gem01 a with (nolock), gsm03 b with(nolock), gam26 z with (nolock) where a.gea00c = b.gsc00c and a.gea01c = b.gsc01c and a.gea00c = z.gaz02c and z.gaz01c = @compid and gsc10d >= @fromdate and gsc10d <= @todate and gsc11c like @staff and b.gsc02c = 'ge' and (isnull(a.gea87c, '') between @fromdepart and @todepart or @fromdepart = '*') --add by lm 2015-10-13 and isnull(gea38c,'') <> '' --第八销售 insert #tbl_item_group(flag, payment, quan, amt) select 2, gsc04c, 1, isnull(gea41f, 0)*gsc05f/(isnull(gea27f,0)+isnull(gea29f,0)+isnull(gea31f,0)+isnull(gea33f,0)+isnull(gea35f,0)+isnull(gea37f,0)+isnull(gea39f,0)+isnull(gea41f,0)+isnull(gea43f,0)+isnull(gea45f,0)) from gem01 a with (nolock), gsm03 b with(nolock), gam26 z with (nolock) where a.gea00c = b.gsc00c and a.gea01c = b.gsc01c and a.gea00c = z.gaz02c and z.gaz01c = @compid and gsc10d >= @fromdate and gsc10d <= @todate and gsc11c like @staff and b.gsc02c = 'ge' and (isnull(a.gea88c, '') between @fromdepart and @todepart or @fromdepart = '*') --add by lm 2015-10-13 and isnull(gea40c,'') <> '' --第九销售 insert #tbl_item_group(flag, payment, quan, amt) select 2, gsc04c, 1, isnull(gea43f, 0)*gsc05f/(isnull(gea27f,0)+isnull(gea29f,0)+isnull(gea31f,0)+isnull(gea33f,0)+isnull(gea35f,0)+isnull(gea37f,0)+isnull(gea39f,0)+isnull(gea41f,0)+isnull(gea43f,0)+isnull(gea45f,0)) from gem01 a with (nolock), gsm03 b with(nolock), gam26 z with (nolock) where a.gea00c = b.gsc00c and a.gea01c = b.gsc01c and a.gea00c = z.gaz02c and z.gaz01c = @compid and gsc10d >= @fromdate and gsc10d <= @todate and gsc11c like @staff and b.gsc02c = 'ge' and (isnull(a.gea89c, '') between @fromdepart and @todepart or @fromdepart = '*') --add by lm 2015-10-13 and isnull(gea42c,'') <> '' --第十销售 insert #tbl_item_group(flag, payment, quan, amt) select 2, gsc04c, 1, isnull(gea45f, 0)*gsc05f/(isnull(gea27f,0)+isnull(gea29f,0)+isnull(gea31f,0)+isnull(gea33f,0)+isnull(gea35f,0)+isnull(gea37f,0)+isnull(gea39f,0)+isnull(gea41f,0)+isnull(gea43f,0)+isnull(gea45f,0)) from gem01 a with (nolock), gsm03 b with(nolock), gam26 z with (nolock) where a.gea00c = b.gsc00c and a.gea01c = b.gsc01c and a.gea00c = z.gaz02c and z.gaz01c = @compid and gsc10d >= @fromdate and gsc10d <= @todate and gsc11c like @staff and b.gsc02c = 'ge' and (isnull(a.gea90c, '') between @fromdepart and @todepart or @fromdepart = '*') --add by lm 2015-10-13 and isnull(gea44c,'') <> '' --End -- 1 insert #tbl_item_group(flag, payment, quan, amt) select 2, a.gcl67c, -isnull(gcl16f, 0) * dbo.fun_divide_default(a.gcl68f, isnull(a.gcl68f, 0) + isnull(a.gcl70f, 0) + isnull(a.gcl72f, 0) + isnull(a.gcl74f, 0), 0), -isnull(gcl16f, 0) * a.gcl68f from #tbl_gcm10_buf a where gcl06i = 1 and (a.gcl81c between @fromdepart and @todepart or @fromdepart = '*') and a.gcl67c is not null and isnull(a.gcl68f, 0) > 0 insert #tbl_item_group(flag, payment, quan, amt) select 2, a.gcl69c, -isnull(gcl16f, 0) * dbo.fun_divide_default(a.gcl70f, isnull(a.gcl68f, 0) + isnull(a.gcl70f, 0) + isnull(a.gcl72f, 0) + isnull(a.gcl74f, 0), 0), -isnull(gcl16f, 0) * a.gcl70f from #tbl_gcm10_buf a where gcl06i = 1 and (a.gcl81c between @fromdepart and @todepart or @fromdepart = '*') and a.gcl69c is not null and isnull(a.gcl70f, 0) > 0 insert #tbl_item_group(flag, payment, quan, amt) select 2, a.gcl71c, -isnull(gcl16f, 0) * dbo.fun_divide_default(a.gcl72f, isnull(a.gcl68f, 0) + isnull(a.gcl70f, 0) + isnull(a.gcl72f, 0) + isnull(a.gcl74f, 0), 0), -isnull(gcl16f, 0) * a.gcl72f from #tbl_gcm10_buf a where gcl06i = 1 and (a.gcl81c between @fromdepart and @todepart or @fromdepart = '*') and a.gcl71c is not null and isnull(a.gcl72f, 0) > 0 insert #tbl_item_group(flag, payment, quan, amt) select 2, a.gcl73c, -isnull(gcl16f, 0) * dbo.fun_divide_default(a.gcl74f, isnull(a.gcl68f, 0) + isnull(a.gcl70f, 0) + isnull(a.gcl72f, 0) + isnull(a.gcl74f, 0), 0), -isnull(gcl16f, 0) * a.gcl74f from #tbl_gcm10_buf a where gcl06i = 1 and (a.gcl81c between @fromdepart and @todepart or @fromdepart = '*') and a.gcl73c is not null and isnull(a.gcl74f, 0) > 0 -- 2 insert #tbl_item_group(flag, payment, quan, amt) select 2, a.gcl67c, -isnull(gcl18f, 0) * dbo.fun_divide_default(a.gcl68f, isnull(a.gcl68f, 0) + isnull(a.gcl70f, 0) + isnull(a.gcl72f, 0) + isnull(a.gcl74f, 0), 0), -isnull(gcl18f, 0) * a.gcl68f from #tbl_gcm10_buf a where gcl06i = 1 and (a.gcl82c between @fromdepart and @todepart or @fromdepart = '*') and a.gcl67c is not null and isnull(a.gcl68f, 0) > 0 insert #tbl_item_group(flag, payment, quan, amt) select 2, a.gcl69c, -isnull(gcl18f, 0) * dbo.fun_divide_default(a.gcl70f, isnull(a.gcl68f, 0) + isnull(a.gcl70f, 0) + isnull(a.gcl72f, 0) + isnull(a.gcl74f, 0), 0), -isnull(gcl18f, 0) * a.gcl70f from #tbl_gcm10_buf a where gcl06i = 1 and (a.gcl82c between @fromdepart and @todepart or @fromdepart = '*') and a.gcl69c is not null and isnull(a.gcl70f, 0) > 0 insert #tbl_item_group(flag, payment, quan, amt) select 2, a.gcl71c, -isnull(gcl18f, 0) * dbo.fun_divide_default(a.gcl72f, isnull(a.gcl68f, 0) + isnull(a.gcl70f, 0) + isnull(a.gcl72f, 0) + isnull(a.gcl74f, 0), 0), -isnull(gcl18f, 0) * a.gcl72f from #tbl_gcm10_buf a where gcl06i = 1 and (a.gcl82c between @fromdepart and @todepart or @fromdepart = '*') and a.gcl71c is not null and isnull(a.gcl72f, 0) > 0 insert #tbl_item_group(flag, payment, quan, amt) select 2, a.gcl73c, -isnull(gcl18f, 0) * dbo.fun_divide_default(a.gcl74f, isnull(a.gcl68f, 0) + isnull(a.gcl70f, 0) + isnull(a.gcl72f, 0) + isnull(a.gcl74f, 0), 0), -isnull(gcl18f, 0) * a.gcl74f from #tbl_gcm10_buf a where gcl06i = 1 and (a.gcl82c between @fromdepart and @todepart or @fromdepart = '*') and a.gcl73c is not null and isnull(a.gcl74f, 0) > 0 -- 3 insert #tbl_item_group(flag, payment, quan, amt) select 2, a.gcl67c, -isnull(gcl20f, 0) * dbo.fun_divide_default(a.gcl68f, isnull(a.gcl68f, 0) + isnull(a.gcl70f, 0) + isnull(a.gcl72f, 0) + isnull(a.gcl74f, 0), 0), -isnull(gcl20f, 0) * a.gcl68f from #tbl_gcm10_buf a where gcl06i = 1 and (a.gcl83c between @fromdepart and @todepart or @fromdepart = '*') and a.gcl67c is not null and isnull(a.gcl68f, 0) > 0 insert #tbl_item_group(flag, payment, quan, amt) select 2, a.gcl69c, -isnull(gcl20f, 0) * dbo.fun_divide_default(a.gcl70f, isnull(a.gcl68f, 0) + isnull(a.gcl70f, 0) + isnull(a.gcl72f, 0) + isnull(a.gcl74f, 0), 0), -isnull(gcl20f, 0) * a.gcl70f from #tbl_gcm10_buf a where gcl06i = 1 and (a.gcl83c between @fromdepart and @todepart or @fromdepart = '*') and a.gcl69c is not null and isnull(a.gcl70f, 0) > 0 insert #tbl_item_group(flag, payment, quan, amt) select 2, a.gcl71c, -isnull(gcl20f, 0) * dbo.fun_divide_default(a.gcl72f, isnull(a.gcl68f, 0) + isnull(a.gcl70f, 0) + isnull(a.gcl72f, 0) + isnull(a.gcl74f, 0), 0), -isnull(gcl20f, 0) * a.gcl72f from #tbl_gcm10_buf a where gcl06i = 1 and (a.gcl83c between @fromdepart and @todepart or @fromdepart = '*') and a.gcl71c is not null and isnull(a.gcl72f, 0) > 0 insert #tbl_item_group(flag, payment, quan, amt) select 2, a.gcl73c, -isnull(gcl20f, 0) * dbo.fun_divide_default(a.gcl74f, isnull(a.gcl68f, 0) + isnull(a.gcl70f, 0) + isnull(a.gcl72f, 0) + isnull(a.gcl74f, 0), 0), -isnull(gcl20f, 0) * a.gcl74f from #tbl_gcm10_buf a where gcl06i = 1 and (a.gcl83c between @fromdepart and @todepart or @fromdepart = '*') and a.gcl73c is not null and isnull(a.gcl74f, 0) > 0 -- 4 insert #tbl_item_group(flag, payment, quan, amt) select 2, a.gcl67c, -isnull(gcl23f, 0) * dbo.fun_divide_default(a.gcl68f, isnull(a.gcl68f, 0) + isnull(a.gcl70f, 0) + isnull(a.gcl72f, 0) + isnull(a.gcl74f, 0), 0), -isnull(gcl23f, 0) * a.gcl68f from #tbl_gcm10_buf a where gcl06i = 1 and (a.gcl100c between @fromdepart and @todepart or @fromdepart = '*') and a.gcl67c is not null and isnull(a.gcl68f, 0) > 0 insert #tbl_item_group(flag, payment, quan, amt) select 2, a.gcl69c, -isnull(gcl23f, 0) * dbo.fun_divide_default(a.gcl70f, isnull(a.gcl68f, 0) + isnull(a.gcl70f, 0) + isnull(a.gcl72f, 0) + isnull(a.gcl74f, 0), 0), -isnull(gcl23f, 0) * a.gcl70f from #tbl_gcm10_buf a where gcl06i = 1 and (a.gcl100c between @fromdepart and @todepart or @fromdepart = '*') and a.gcl69c is not null and isnull(a.gcl70f, 0) > 0 insert #tbl_item_group(flag, payment, quan, amt) select 2, a.gcl71c, -isnull(gcl23f, 0) * dbo.fun_divide_default(a.gcl72f, isnull(a.gcl68f, 0) + isnull(a.gcl70f, 0) + isnull(a.gcl72f, 0) + isnull(a.gcl74f, 0), 0), -isnull(gcl23f, 0) * a.gcl72f from #tbl_gcm10_buf a where gcl06i = 1 and (a.gcl100c between @fromdepart and @todepart or @fromdepart = '*') and a.gcl71c is not null and isnull(a.gcl72f, 0) > 0 insert #tbl_item_group(flag, payment, quan, amt) select 2, a.gcl73c, -isnull(gcl23f, 0) * dbo.fun_divide_default(a.gcl74f, isnull(a.gcl68f, 0) + isnull(a.gcl70f, 0) + isnull(a.gcl72f, 0) + isnull(a.gcl74f, 0), 0), -isnull(gcl23f, 0) * a.gcl74f from #tbl_gcm10_buf a where gcl06i = 1 and (a.gcl100c between @fromdepart and @todepart or @fromdepart = '*') and a.gcl73c is not null and isnull(a.gcl74f, 0) > 0 -- 5 insert #tbl_item_group(flag, payment, quan, amt) select 2, a.gcl67c, -isnull(gcl25f, 0) * dbo.fun_divide_default(a.gcl68f, isnull(a.gcl68f, 0) + isnull(a.gcl70f, 0) + isnull(a.gcl72f, 0) + isnull(a.gcl74f, 0), 0), -isnull(gcl25f, 0) * a.gcl68f from #tbl_gcm10_buf a where gcl06i = 1 and (a.gcl101c between @fromdepart and @todepart or @fromdepart = '*') and a.gcl67c is not null and isnull(a.gcl68f, 0) > 0 insert #tbl_item_group(flag, payment, quan, amt) select 2, a.gcl69c, -isnull(gcl25f, 0) * dbo.fun_divide_default(a.gcl70f, isnull(a.gcl68f, 0) + isnull(a.gcl70f, 0) + isnull(a.gcl72f, 0) + isnull(a.gcl74f, 0), 0), -isnull(gcl25f, 0) * a.gcl70f from #tbl_gcm10_buf a where gcl06i = 1 and (a.gcl101c between @fromdepart and @todepart or @fromdepart = '*') and a.gcl69c is not null and isnull(a.gcl70f, 0) > 0 insert #tbl_item_group(flag, payment, quan, amt) select 2, a.gcl71c, -isnull(gcl25f, 0) * dbo.fun_divide_default(a.gcl72f, isnull(a.gcl68f, 0) + isnull(a.gcl70f, 0) + isnull(a.gcl72f, 0) + isnull(a.gcl74f, 0), 0), -isnull(gcl25f, 0) * a.gcl72f from #tbl_gcm10_buf a where gcl06i = 1 and (a.gcl101c between @fromdepart and @todepart or @fromdepart = '*') and a.gcl71c is not null and isnull(a.gcl72f, 0) > 0 insert #tbl_item_group(flag, payment, quan, amt) select 2, a.gcl73c, -isnull(gcl25f, 0) * dbo.fun_divide_default(a.gcl74f, isnull(a.gcl68f, 0) + isnull(a.gcl70f, 0) + isnull(a.gcl72f, 0) + isnull(a.gcl74f, 0), 0), -isnull(gcl25f, 0) * a.gcl74f from #tbl_gcm10_buf a where gcl06i = 1 and (a.gcl101c between @fromdepart and @todepart or @fromdepart = '*') and a.gcl73c is not null and isnull(a.gcl74f, 0) > 0 -- 6 insert #tbl_item_group(flag, payment, quan, amt) select 2, a.gcl67c, -isnull(gcl27f, 0) * dbo.fun_divide_default(a.gcl68f, isnull(a.gcl68f, 0) + isnull(a.gcl70f, 0) + isnull(a.gcl72f, 0) + isnull(a.gcl74f, 0), 0), -isnull(gcl27f, 0) * a.gcl68f from #tbl_gcm10_buf a where gcl06i = 1 and (a.gcl102c between @fromdepart and @todepart or @fromdepart = '*') and a.gcl67c is not null and isnull(a.gcl68f, 0) > 0 insert #tbl_item_group(flag, payment, quan, amt) select 2, a.gcl69c, -isnull(gcl27f, 0) * dbo.fun_divide_default(a.gcl70f, isnull(a.gcl68f, 0) + isnull(a.gcl70f, 0) + isnull(a.gcl72f, 0) + isnull(a.gcl74f, 0), 0), -isnull(gcl27f, 0) * a.gcl70f from #tbl_gcm10_buf a where gcl06i = 1 and (a.gcl102c between @fromdepart and @todepart or @fromdepart = '*') and a.gcl69c is not null and isnull(a.gcl70f, 0) > 0 insert #tbl_item_group(flag, payment, quan, amt) select 2, a.gcl71c, -isnull(gcl27f, 0) * dbo.fun_divide_default(a.gcl72f, isnull(a.gcl68f, 0) + isnull(a.gcl70f, 0) + isnull(a.gcl72f, 0) + isnull(a.gcl74f, 0), 0), -isnull(gcl27f, 0) * a.gcl72f from #tbl_gcm10_buf a where gcl06i = 1 and (a.gcl102c between @fromdepart and @todepart or @fromdepart = '*') and a.gcl71c is not null and isnull(a.gcl72f, 0) > 0 insert #tbl_item_group(flag, payment, quan, amt) select 2, a.gcl73c, -isnull(gcl27f, 0) * dbo.fun_divide_default(a.gcl74f, isnull(a.gcl68f, 0) + isnull(a.gcl70f, 0) + isnull(a.gcl72f, 0) + isnull(a.gcl74f, 0), 0), -isnull(gcl27f, 0) * a.gcl74f from #tbl_gcm10_buf a where gcl06i = 1 and (a.gcl102c between @fromdepart and @todepart or @fromdepart = '*') and a.gcl73c is not null and isnull(a.gcl74f, 0) > 0 -- 7 insert #tbl_item_group(flag, payment, quan, amt) select 2, a.gcl67c, -isnull(gcl29f, 0) * dbo.fun_divide_default(a.gcl68f, isnull(a.gcl68f, 0) + isnull(a.gcl70f, 0) + isnull(a.gcl72f, 0) + isnull(a.gcl74f, 0), 0), -isnull(gcl29f, 0) * a.gcl68f from #tbl_gcm10_buf a where gcl06i = 1 and (a.gcl103c between @fromdepart and @todepart or @fromdepart = '*') and a.gcl67c is not null and isnull(a.gcl68f, 0) > 0 insert #tbl_item_group(flag, payment, quan, amt) select 2, a.gcl69c, -isnull(gcl29f, 0) * dbo.fun_divide_default(a.gcl70f, isnull(a.gcl68f, 0) + isnull(a.gcl70f, 0) + isnull(a.gcl72f, 0) + isnull(a.gcl74f, 0), 0), -isnull(gcl29f, 0) * a.gcl70f from #tbl_gcm10_buf a where gcl06i = 1 and (a.gcl103c between @fromdepart and @todepart or @fromdepart = '*') and a.gcl69c is not null and isnull(a.gcl70f, 0) > 0 insert #tbl_item_group(flag, payment, quan, amt) select 2, a.gcl71c, -isnull(gcl29f, 0) * dbo.fun_divide_default(a.gcl72f, isnull(a.gcl68f, 0) + isnull(a.gcl70f, 0) + isnull(a.gcl72f, 0) + isnull(a.gcl74f, 0), 0), -isnull(gcl29f, 0) * a.gcl72f from #tbl_gcm10_buf a where gcl06i = 1 and (a.gcl103c between @fromdepart and @todepart or @fromdepart = '*') and a.gcl71c is not null and isnull(a.gcl72f, 0) > 0 insert #tbl_item_group(flag, payment, quan, amt) select 2, a.gcl73c, -isnull(gcl29f, 0) * dbo.fun_divide_default(a.gcl74f, isnull(a.gcl68f, 0) + isnull(a.gcl70f, 0) + isnull(a.gcl72f, 0) + isnull(a.gcl74f, 0), 0), -isnull(gcl29f, 0) * a.gcl74f from #tbl_gcm10_buf a where gcl06i = 1 and (a.gcl103c between @fromdepart and @todepart or @fromdepart = '*') and a.gcl73c is not null and isnull(a.gcl74f, 0) > 0 -- 8 insert #tbl_item_group(flag, payment, quan, amt) select 2, a.gcl67c, -isnull(gcl31f, 0) * dbo.fun_divide_default(a.gcl68f, isnull(a.gcl68f, 0) + isnull(a.gcl70f, 0) + isnull(a.gcl72f, 0) + isnull(a.gcl74f, 0), 0), -isnull(gcl31f, 0) * a.gcl68f from #tbl_gcm10_buf a where gcl06i = 1 and (a.gcl104c between @fromdepart and @todepart or @fromdepart = '*') and a.gcl67c is not null and isnull(a.gcl68f, 0) > 0 insert #tbl_item_group(flag, payment, quan, amt) select 2, a.gcl69c, -isnull(gcl31f, 0) * dbo.fun_divide_default(a.gcl70f, isnull(a.gcl68f, 0) + isnull(a.gcl70f, 0) + isnull(a.gcl72f, 0) + isnull(a.gcl74f, 0), 0), -isnull(gcl31f, 0) * a.gcl70f from #tbl_gcm10_buf a where gcl06i = 1 and (a.gcl104c between @fromdepart and @todepart or @fromdepart = '*') and a.gcl69c is not null and isnull(a.gcl70f, 0) > 0 insert #tbl_item_group(flag, payment, quan, amt) select 2, a.gcl71c, -isnull(gcl31f, 0) * dbo.fun_divide_default(a.gcl72f, isnull(a.gcl68f, 0) + isnull(a.gcl70f, 0) + isnull(a.gcl72f, 0) + isnull(a.gcl74f, 0), 0), -isnull(gcl31f, 0) * a.gcl72f from #tbl_gcm10_buf a where gcl06i = 1 and (a.gcl104c between @fromdepart and @todepart or @fromdepart = '*') and a.gcl71c is not null and isnull(a.gcl72f, 0) > 0 insert #tbl_item_group(flag, payment, quan, amt) select 2, a.gcl73c, -isnull(gcl31f, 0) * dbo.fun_divide_default(a.gcl74f, isnull(a.gcl68f, 0) + isnull(a.gcl70f, 0) + isnull(a.gcl72f, 0) + isnull(a.gcl74f, 0), 0), -isnull(gcl31f, 0) * a.gcl74f from #tbl_gcm10_buf a where gcl06i = 1 and (a.gcl104c between @fromdepart and @todepart or @fromdepart = '*') and a.gcl73c is not null and isnull(a.gcl74f, 0) > 0 -- 9 insert #tbl_item_group(flag, payment, quan, amt) select 2, a.gcl67c, -isnull(gcl33f, 0) * dbo.fun_divide_default(a.gcl68f, isnull(a.gcl68f, 0) + isnull(a.gcl70f, 0) + isnull(a.gcl72f, 0) + isnull(a.gcl74f, 0), 0), -isnull(gcl33f, 0) * a.gcl68f from #tbl_gcm10_buf a where gcl06i = 1 and (a.gcl105c between @fromdepart and @todepart or @fromdepart = '*') and a.gcl67c is not null and isnull(a.gcl68f, 0) > 0 insert #tbl_item_group(flag, payment, quan, amt) select 2, a.gcl69c, -isnull(gcl33f, 0) * dbo.fun_divide_default(a.gcl70f, isnull(a.gcl68f, 0) + isnull(a.gcl70f, 0) + isnull(a.gcl72f, 0) + isnull(a.gcl74f, 0), 0), -isnull(gcl33f, 0) * a.gcl70f from #tbl_gcm10_buf a where gcl06i = 1 and (a.gcl105c between @fromdepart and @todepart or @fromdepart = '*') and a.gcl69c is not null and isnull(a.gcl70f, 0) > 0 insert #tbl_item_group(flag, payment, quan, amt) select 2, a.gcl71c, -isnull(gcl33f, 0) * dbo.fun_divide_default(a.gcl72f, isnull(a.gcl68f, 0) + isnull(a.gcl70f, 0) + isnull(a.gcl72f, 0) + isnull(a.gcl74f, 0), 0), -isnull(gcl33f, 0) * a.gcl72f from #tbl_gcm10_buf a where gcl06i = 1 and (a.gcl105c between @fromdepart and @todepart or @fromdepart = '*') and a.gcl71c is not null and isnull(a.gcl72f, 0) > 0 insert #tbl_item_group(flag, payment, quan, amt) select 2, a.gcl73c, -isnull(gcl33f, 0) * dbo.fun_divide_default(a.gcl74f, isnull(a.gcl68f, 0) + isnull(a.gcl70f, 0) + isnull(a.gcl72f, 0) + isnull(a.gcl74f, 0), 0), -isnull(gcl33f, 0) * a.gcl74f from #tbl_gcm10_buf a where gcl06i = 1 and (a.gcl105c between @fromdepart and @todepart or @fromdepart = '*') and a.gcl73c is not null and isnull(a.gcl74f, 0) > 0 -- 10 insert #tbl_item_group(flag, payment, quan, amt) select 2, a.gcl67c, -isnull(gcl35f, 0) * dbo.fun_divide_default(a.gcl68f, isnull(a.gcl68f, 0) + isnull(a.gcl70f, 0) + isnull(a.gcl72f, 0) + isnull(a.gcl74f, 0), 0), -isnull(gcl35f, 0) * a.gcl68f from #tbl_gcm10_buf a where gcl06i = 1 and (a.gcl106c between @fromdepart and @todepart or @fromdepart = '*') and a.gcl67c is not null and isnull(a.gcl68f, 0) > 0 insert #tbl_item_group(flag, payment, quan, amt) select 2, a.gcl69c, -isnull(gcl35f, 0) * dbo.fun_divide_default(a.gcl70f, isnull(a.gcl68f, 0) + isnull(a.gcl70f, 0) + isnull(a.gcl72f, 0) + isnull(a.gcl74f, 0), 0), -isnull(gcl35f, 0) * a.gcl70f from #tbl_gcm10_buf a where gcl06i = 1 and (a.gcl106c between @fromdepart and @todepart or @fromdepart = '*') and a.gcl69c is not null and isnull(a.gcl70f, 0) > 0 insert #tbl_item_group(flag, payment, quan, amt) select 2, a.gcl71c, -isnull(gcl35f, 0) * dbo.fun_divide_default(a.gcl72f, isnull(a.gcl68f, 0) + isnull(a.gcl70f, 0) + isnull(a.gcl72f, 0) + isnull(a.gcl74f, 0), 0), -isnull(gcl35f, 0) * a.gcl72f from #tbl_gcm10_buf a where gcl06i = 1 and (a.gcl106c between @fromdepart and @todepart or @fromdepart = '*') and a.gcl71c is not null and isnull(a.gcl72f, 0) > 0 insert #tbl_item_group(flag, payment, quan, amt) select 2, a.gcl73c, -isnull(gcl35f, 0) * dbo.fun_divide_default(a.gcl74f, isnull(a.gcl68f, 0) + isnull(a.gcl70f, 0) + isnull(a.gcl72f, 0) + isnull(a.gcl74f, 0), 0), -isnull(gcl35f, 0) * a.gcl74f from #tbl_gcm10_buf a where gcl06i = 1 and (a.gcl106c between @fromdepart and @todepart or @fromdepart = '*') and a.gcl73c is not null and isnull(a.gcl74f, 0) > 0 drop table #tbl_gcm10_buf create table #tbl_item_result ( flag int not null, -- 标志 1-项目 2-会员卡 3-产品 payment varchar(40) null, -- 支付方式 quan float null, -- 数量 amt float null, -- 金额 ) insert #tbl_item_result(flag, payment, quan, amt) select flag, payment, sum(quan), sum(amt) from #tbl_item_group group by flag, payment order by payment, flag update a set a.payment = b.gsb03c from #tbl_item_result a, gsm02 b where a.payment = b.gsb02c and b.gsb00c = @compid and b.gsb01c = '3' select isnull((case when flag = 1 then '项目' when flag = 2 then '卡' when flag = 3 then '产品' when flag = 4 then '项目/产品' end),'') item, isnull(payment,'') payment, isnull(quan,'') quan, isnull(amt,'') amt from #tbl_item_result drop table #tbl_item_group drop table #tbl_item_result drop table #tbl_item_group_dept end GO /****** Object: StoredProcedure [dbo].[upg_stat_card_sale_summary_ex] Script Date: 06/12/2017 06:20:43 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_stat_card_sale_summary_ex] ( @incompid varchar(10), -- 公司别 @fromdate varchar(10), -- 开始日期 @todate varchar(10), -- 截至日期 @fromcardtype varchar(10), -- 卡类别 @tocardtype varchar(10), -- 卡类别 @fromacctype varchar(10), -- 账户类别 Add by Huzy 2011/03/22 @toacctype varchar(10), -- 账户类别 Add by Huzy 2011/03/22 @fromcardid varchar(20), -- 开始卡号 @tocardid varchar(20), -- 截至卡号 @include_proc int, -- 是否包括疗程部分 @saletype varchar(2) -- 业务类型 add by liyh '3' - 取款 ) as begin declare @payment_0 varchar(10) declare @payment_3 varchar(10) declare @payment_$ varchar(10) declare @payment_u1 varchar(10) declare @payment_u2 varchar(10) declare @payment_u3 varchar(10) declare @payment_u4 varchar(10) declare @payment_u5 varchar(10) exec upg_isrevenue_payment @incompid, @payment_0 output, @payment_3 output, @payment_$ output, @payment_u1 output, @payment_u2 output, @payment_u3 output, @payment_u4 output, @payment_u5 output if @fromdate = '*' or @fromdate = '' begin select @fromdate = '00010101', @todate = '99991231' end if @fromacctype = '*' set @fromacctype = '' if @toacctype = '*' set @toacctype = '' if (isnull(@fromacctype, '') = '') if (isnull(@toacctype, '') <> '') set @fromacctype = @toacctype else set @fromacctype = '' else if (isnull(@toacctype, '') = '') set @toacctype = @fromacctype create table #tbl_card_sale_detail ( billtype varchar(20) not null, -- 单别 mode int null, -- 方式 0 会员卡销售 1 充 2 还款 3 取款 4欠款 5-变更 PayMoney float null, -- 支付金额 PayDesc varchar(5) null, -- 支付方式 acctype varchar(40) null, -- 账户类别 Add by Huzy 2011/03/22 upgradeflag int null ) --卡销售 账户类别取自 gnb13i insert #tbl_card_sale_detail(billtype, mode, PayDesc, PayMoney, acctype) select p.billtype, p.mode, p.PayDesc, sum(p.PayMoney), p.acctype from ((select 'gn' billtype, 0 mode, gnb15c PayDesc, isnull(gnb16f, 0) PayMoney, gsb03c acctype from gnm02 b with (nolock), gnm01 a with (nolock), gam26 z with (nolock), gsm02 s with (nolock) where a.gna00c = b.gnb00c and a.gna01c = b.gnb01c and a.gna80d >= @fromdate and a.gna80d <= @todate and ((@fromcardid = '*') or (b.gnb03c >= @fromcardid and b.gnb03c <= @tocardid)) and ((@fromcardtype = '*') or (b.gnb02c >= @fromcardtype and b.gnb02c <= @tocardtype)) and a.gna00c = z.gaz02c and z.gaz01c = @incompid and (@fromacctype = '' or (b.gnb13i >= @fromacctype and b.gnb13i <= @toacctype )) and s.gsb00c = a.gna00c and s.gsb02c = b.gnb13i and s.gsb01c = 'O') union all (select 'gn' billtype, 0 mode, gnb17c PayDesc, isnull(gnb18f, 0) PayMoney, gsb03c acctype from gnm02 b with (nolock), gnm01 a with (nolock), gam26 z with (nolock), gsm02 s with (nolock) where a.gna00c = b.gnb00c and a.gna01c = b.gnb01c and a.gna80d >= @fromdate and a.gna80d <= @todate and ((@fromcardid = '*') or (b.gnb03c >= @fromcardid and b.gnb03c <= @tocardid)) and ((@fromcardtype = '*') or (b.gnb02c >= @fromcardtype and b.gnb02c <= @tocardtype)) and a.gna00c = z.gaz02c and z.gaz01c = @incompid and (@fromacctype = '' or (b.gnb13i >= @fromacctype and b.gnb13i <= @toacctype )) and s.gsb00c = a.gna00c and s.gsb02c = b.gnb13i and s.gsb01c = 'O') union all (select 'gn' billtype, 0 mode, gnb19c PayDesc, isnull(gnb20f, 0) PayMoney, gsb03c acctype from gnm02 b with (nolock), gnm01 a with (nolock), gam26 z with (nolock), gsm02 s with (nolock) where a.gna00c = b.gnb00c and a.gna01c = b.gnb01c and a.gna80d >= @fromdate and a.gna80d <= @todate and ((@fromcardid = '*') or (b.gnb03c >= @fromcardid and b.gnb03c <= @tocardid)) and ((@fromcardtype = '*') or (b.gnb02c >= @fromcardtype and b.gnb02c <= @tocardtype)) and a.gna00c = z.gaz02c and z.gaz01c = @incompid and (@fromacctype = '' or (b.gnb13i >= @fromacctype and b.gnb13i <= @toacctype )) and s.gsb00c = a.gna00c and s.gsb02c = b.gnb13i and s.gsb01c = 'O') union all (select 'gn' billtype, 0 mode, gnb21c PayDesc, isnull(gnb22f, 0) PayMoney, gsb03c acctype from gnm02 b with (nolock), gnm01 a with (nolock), gam26 z with (nolock), gsm02 s with (nolock) where a.gna00c = b.gnb00c and a.gna01c = b.gnb01c and a.gna80d >= @fromdate and a.gna80d <= @todate and ((@fromcardid = '*') or (b.gnb03c >= @fromcardid and b.gnb03c <= @tocardid)) and ((@fromcardtype = '*') or (b.gnb02c >= @fromcardtype and b.gnb02c <= @tocardtype)) and a.gna00c = z.gaz02c and z.gaz01c = @incompid and (@fromacctype = '' or (b.gnb13i >= @fromacctype and b.gnb13i <= @toacctype )) and s.gsb00c = a.gna00c and s.gsb02c = b.gnb13i and s.gsb01c = 'O') union all (select 'gn' billtype, 0 mode, gtf09c PayDesc, isnull(gtf10f, 0) PayMoney, gsb03c acctype from gnm02 b with (nolock), gnm01 a with (nolock), gam26 z with (nolock), gsm02 s with (nolock),gtc05 c where a.gna00c = b.gnb00c and a.gna01c = b.gnb01c and b.gnb00c = c.gtf00c and b.gnb01c = c.gtf01c and isnull(c.gtf09c,'') <> '' and a.gna80d >= @fromdate and a.gna80d <= @todate and ((@fromcardid = '*') or (b.gnb03c >= @fromcardid and b.gnb03c <= @tocardid)) and ((@fromcardtype = '*') or (b.gnb02c >= @fromcardtype and b.gnb02c <= @tocardtype)) and a.gna00c = z.gaz02c and z.gaz01c = @incompid and (@fromacctype = '' or (b.gnb13i >= @fromacctype and b.gnb13i <= @toacctype )) and s.gsb00c = a.gna00c and s.gsb02c = b.gnb13i and s.gsb01c = 'O') union all (select 'gn' billtype, 0 mode, gtf11c PayDesc, isnull(gtf12f, 0) PayMoney, gsb03c acctype from gnm02 b with (nolock), gnm01 a with (nolock), gam26 z with (nolock), gsm02 s with (nolock),gtc05 c where a.gna00c = b.gnb00c and a.gna01c = b.gnb01c and b.gnb00c = c.gtf00c and b.gnb01c = c.gtf01c and isnull(c.gtf11c,'') <> '' and a.gna80d >= @fromdate and a.gna80d <= @todate and ((@fromcardid = '*') or (b.gnb03c >= @fromcardid and b.gnb03c <= @tocardid)) and ((@fromcardtype = '*') or (b.gnb02c >= @fromcardtype and b.gnb02c <= @tocardtype)) and a.gna00c = z.gaz02c and z.gaz01c = @incompid and (@fromacctype = '' or (b.gnb13i >= @fromacctype and b.gnb13i <= @toacctype )) and s.gsb00c = a.gna00c and s.gsb02c = b.gnb13i and s.gsb01c = 'O') union all (select 'gn' billtype, 0 mode, gtf13c PayDesc, isnull(gtf14f, 0) PayMoney, gsb03c acctype from gnm02 b with (nolock), gnm01 a with (nolock), gam26 z with (nolock), gsm02 s with (nolock),gtc05 c where a.gna00c = b.gnb00c and a.gna01c = b.gnb01c and b.gnb00c = c.gtf00c and b.gnb01c = c.gtf01c and isnull(c.gtf13c,'') <> '' and a.gna80d >= @fromdate and a.gna80d <= @todate and ((@fromcardid = '*') or (b.gnb03c >= @fromcardid and b.gnb03c <= @tocardid)) and ((@fromcardtype = '*') or (b.gnb02c >= @fromcardtype and b.gnb02c <= @tocardtype)) and a.gna00c = z.gaz02c and z.gaz01c = @incompid and (@fromacctype = '' or (b.gnb13i >= @fromacctype and b.gnb13i <= @toacctype )) and s.gsb00c = a.gna00c and s.gsb02c = b.gnb13i and s.gsb01c = 'O') union all (select 'gn' billtype, 0 mode, gtf15c PayDesc, isnull(gtf16f, 0) PayMoney, gsb03c acctype from gnm02 b with (nolock), gnm01 a with (nolock), gam26 z with (nolock), gsm02 s with (nolock),gtc05 c where a.gna00c = b.gnb00c and a.gna01c = b.gnb01c and b.gnb00c = c.gtf00c and b.gnb01c = c.gtf01c and isnull(c.gtf15c,'') <> '' and a.gna80d >= @fromdate and a.gna80d <= @todate and ((@fromcardid = '*') or (b.gnb03c >= @fromcardid and b.gnb03c <= @tocardid)) and ((@fromcardtype = '*') or (b.gnb02c >= @fromcardtype and b.gnb02c <= @tocardtype)) and a.gna00c = z.gaz02c and z.gaz01c = @incompid and (@fromacctype = '' or (b.gnb13i >= @fromacctype and b.gnb13i <= @toacctype )) and s.gsb00c = a.gna00c and s.gsb02c = b.gnb13i and s.gsb01c = 'O') ) p group by p.billtype, p.mode, p.PayDesc, p.acctype if @include_proc = 1 begin insert #tbl_card_sale_detail(billtype, mode, PayDesc, PayMoney) select p.billtype, p.mode, p.PayDesc, sum(p.PayMoney) from ((select 'gn' billtype, 0 mode, gnc15c PayDesc, isnull(gnc16f, 0) PayMoney from gnm03 b with (nolock), gnm01 a with (nolock),gnm02 d with (nolock), gam26 z with (nolock) where a.gna00c = b.gnc00c and a.gna01c = b.gnc01c and a.gna00c = d.gnb00c and a.gna01c = d.gnb01c and a.gna80d >= @fromdate and a.gna80d <= @todate and ((@fromcardid = '*') or (d.gnb03c >= @fromcardid and d.gnb03c <= @tocardid)) and ((@fromcardtype = '*') or (d.gnb02c >= @fromcardtype and d.gnb02c <= @tocardtype)) and (@fromacctype = '' or (d.gnb13i >= @fromacctype and d.gnb13i <= @toacctype )) and a.gna00c = z.gaz02c and z.gaz01c = @incompid) union all (select 'gn' billtype, 0 mode, gnc17c PayDesc, isnull(gnc18f, 0) PayMoney from gnm03 b with (nolock), gnm01 a with (nolock),gnm02 d with (nolock), gam26 z with (nolock) where a.gna00c = b.gnc00c and a.gna01c = b.gnc01c and a.gna00c = d.gnb00c and a.gna01c = d.gnb01c and a.gna80d >= @fromdate and a.gna80d <= @todate and ((@fromcardid = '*') or (d.gnb03c >= @fromcardid and d.gnb03c <= @tocardid)) and ((@fromcardtype = '*') or (d.gnb02c >= @fromcardtype and d.gnb02c <= @tocardtype)) and (@fromacctype = '' or (d.gnb13i >= @fromacctype and d.gnb13i <= @toacctype )) and a.gna00c = z.gaz02c and z.gaz01c = @incompid) union all (select 'gn' billtype, 0 mode, gnc19c PayDesc, isnull(gnc20f, 0) PayMoney from gnm03 b with (nolock), gnm01 a with (nolock), gnm02 d with (nolock),gam26 z with (nolock) where a.gna00c = b.gnc00c and a.gna01c = b.gnc01c and a.gna00c = d.gnb00c and a.gna01c = d.gnb01c and a.gna80d >= @fromdate and a.gna80d <= @todate and ((@fromcardid = '*') or (d.gnb03c >= @fromcardid and d.gnb03c <= @tocardid)) and ((@fromcardtype = '*') or (d.gnb02c >= @fromcardtype and d.gnb02c <= @tocardtype)) and (@fromacctype = '' or (d.gnb13i >= @fromacctype and d.gnb13i <= @toacctype )) and a.gna00c = z.gaz02c and z.gaz01c = @incompid) union all (select 'gn' billtype, 0 mode, gnc23c PayDesc, isnull(gnc24f, 0) PayMoney from gnm03 b with (nolock), gnm01 a with (nolock),gnm02 d with (nolock), gam26 z with (nolock) where a.gna00c = b.gnc00c and a.gna01c = b.gnc01c and a.gna80d >= @fromdate and a.gna80d <= @todate and a.gna00c = d.gnb00c and a.gna01c = d.gnb01c and ((@fromcardid = '*') or (d.gnb03c >= @fromcardid and d.gnb03c <= @tocardid)) and ((@fromcardtype = '*') or (d.gnb02c >= @fromcardtype and d.gnb02c <= @tocardtype)) and (@fromacctype = '' or (d.gnb13i >= @fromacctype and d.gnb13i <= @toacctype )) and a.gna00c = z.gaz02c and z.gaz01c = @incompid)) p group by p.billtype, p.mode, p.PayDesc end --卡充值 账户取自 gcl04c insert #tbl_card_sale_detail(billtype, mode, PayDesc, PayMoney, acctype, upgradeflag) select p.billtype, p.mode, p.PayDesc, sum(p.PayMoney), p.acctype, p.upgradeflag from ((select 'gz' billtype, 1 mode, gcl67c PayDesc, isnull(gcl68f, 0) PayMoney, gsb03c acctype, gcl14i upgradeflag from gcm10 a with (nolock), gam26 z with (nolock), gsm02 s with (nolock) where a.gcl80d >= @fromdate and a.gcl80d <= @todate and ((@fromcardid = '*') or (a.gcl03c >= @fromcardid and a.gcl03c <= @tocardid)) and ((@fromcardtype = '*') or (a.gcl15c >= @fromcardtype and a.gcl15c <= @tocardtype)) and a.gcl00c = z.gaz02c and z.gaz01c = @incompid and a.gcl06i = 0 and (@fromacctype = '' or (a.gcl04c >= @fromacctype and a.gcl04c <= @toacctype )) and s.gsb00c = a.gcl00c and s.gsb01c = 'O' and s.gsb02c = a.gcl04c) union all (select 'gz' billtype, 1 mode, gcl69c PayDesc, isnull(gcl70f, 0) PayMoney, gsb03c acctype, gcl14i upgradeflag from gcm10 a with (nolock), gam26 z with (nolock), gsm02 s with (nolock) where a.gcl80d >= @fromdate and a.gcl80d <= @todate and ((@fromcardid = '*') or (a.gcl03c >= @fromcardid and a.gcl03c <= @tocardid)) and ((@fromcardtype = '*') or (a.gcl15c >= @fromcardtype and a.gcl15c <= @tocardtype)) and a.gcl00c = z.gaz02c and z.gaz01c = @incompid and a.gcl06i = 0 and (@fromacctype = '' or (a.gcl04c >= @fromacctype and a.gcl04c <= @toacctype)) and s.gsb00c = a.gcl00c and s.gsb01c = 'O' and s.gsb02c = a.gcl04c) union all (select 'gz' billtype, 1 mode, gcl71c PayDesc, isnull(gcl72f, 0) PayMoney, gsb03c acctype, gcl14i upgradeflag from gcm10 a with (nolock), gam26 z with (nolock), gsm02 s with (nolock) where a.gcl80d >= @fromdate and a.gcl80d <= @todate and ((@fromcardid = '*') or (a.gcl03c >= @fromcardid and a.gcl03c <= @tocardid)) and ((@fromcardtype = '*') or (a.gcl15c >= @fromcardtype and a.gcl15c <= @tocardtype)) and a.gcl00c = z.gaz02c and z.gaz01c = @incompid and a.gcl06i = 0 and (@fromacctype = '' or (a.gcl04c >= @fromacctype and a.gcl04c <= @toacctype )) and s.gsb00c = a.gcl00c and s.gsb01c = 'O' and s.gsb02c = a.gcl04c) union all (select 'gz' billtype, 1 mode, gcl73c PayDesc, isnull(gcl74f, 0) PayMoney, gsb03c acctype, gcl14i upgradeflag from gcm10 a with (nolock), gam26 z with (nolock), gsm02 s with (nolock) where a.gcl80d >= @fromdate and a.gcl80d <= @todate and ((@fromcardid = '*') or (a.gcl03c >= @fromcardid and a.gcl03c <= @tocardid)) and ((@fromcardtype = '*') or (a.gcl15c >= @fromcardtype and a.gcl15c <= @tocardtype)) and a.gcl00c = z.gaz02c and z.gaz01c = @incompid and a.gcl06i = 0 and (@fromacctype = '' or (a.gcl04c >= @fromacctype and a.gcl04c <= @toacctype )) and s.gsb00c = a.gcl00c and s.gsb01c = 'O' and s.gsb02c = a.gcl04c)) p group by p.billtype, p.mode, p.PayDesc, p.acctype, p.upgradeflag if @include_proc = 1 begin insert #tbl_card_sale_detail(billtype, mode, PayDesc, PayMoney) select p.billtype, p.mode, p.PayDesc, sum(p.PayMoney) from ((select 'gz' billtype, 1 mode, gcm15c PayDesc, isnull(gcm16f, 0) PayMoney from gcm10 a with (nolock), gcm11 b with (nolock), gam26 z with (nolock) where a.gcl80d >= @fromdate and a.gcl80d <= @todate and a.gcl00c = b.gcm00c and a.gcl01c = b.gcm01c and ((@fromcardid = '*') or (a.gcl03c >= @fromcardid and a.gcl03c <= @tocardid)) and ((@fromcardtype = '*') or (a.gcl15c >= @fromcardtype and a.gcl15c <= @tocardtype)) and a.gcl00c = z.gaz02c and z.gaz01c = @incompid and a.gcl06i = 0) union all (select 'gz' billtype, 1 mode, gcm17c PayDesc, isnull(gcm18f, 0) PayMoney from gcm10 a with (nolock), gcm11 b with (nolock), gam26 z with (nolock) where a.gcl80d >= @fromdate and a.gcl80d <= @todate and a.gcl00c = b.gcm00c and a.gcl01c = b.gcm01c and ((@fromcardid = '*') or (a.gcl03c >= @fromcardid and a.gcl03c <= @tocardid)) and ((@fromcardtype = '*') or (a.gcl15c >= @fromcardtype and a.gcl15c <= @tocardtype)) and a.gcl00c = z.gaz02c and z.gaz01c = @incompid and a.gcl06i = 0) union all (select 'gz' billtype, 1 mode, gcm19c PayDesc, isnull(gcm20f, 0) PayMoney from gcm10 a with (nolock), gcm11 b with (nolock), gam26 z with (nolock) where a.gcl80d >= @fromdate and a.gcl80d <= @todate and a.gcl00c = b.gcm00c and a.gcl01c = b.gcm01c and ((@fromcardid = '*') or (a.gcl03c >= @fromcardid and a.gcl03c <= @tocardid)) and ((@fromcardtype = '*') or (a.gcl15c >= @fromcardtype and a.gcl15c <= @tocardtype)) and a.gcl00c = z.gaz02c and z.gaz01c = @incompid and a.gcl06i = 0) union all (select 'gz' billtype, 1 mode, gcm23c PayDesc, isnull(gcm24f, 0) PayMoney from gcm10 a with (nolock), gcm11 b with (nolock), gam26 z with (nolock) where a.gcl80d >= @fromdate and a.gcl80d <= @todate and a.gcl00c = b.gcm00c and a.gcl01c = b.gcm01c and ((@fromcardid = '*') or (a.gcl03c >= @fromcardid and a.gcl03c <= @tocardid)) and ((@fromcardtype = '*') or (a.gcl15c >= @fromcardtype and a.gcl15c <= @tocardtype)) and a.gcl00c = z.gaz02c and z.gaz01c = @incompid and a.gcl06i = 0)) p group by p.billtype, p.mode, p.PayDesc end insert #tbl_card_sale_detail(billtype, mode, PayDesc, PayMoney, acctype, upgradeflag) select p.billtype, p.mode, p.PayDesc, sum(p.PayMoney), p.acctype, p.upgradeflag from ((select 'gz' billtype, 2 mode, gcl67c PayDesc, isnull(gcl68f, 0) PayMoney, gsb03c acctype, gcl14i upgradeflag from gcm10 a with (nolock), gam26 z with (nolock), gsm02 s with (nolock) where a.gcl80d >= @fromdate and a.gcl80d <= @todate and ((@fromcardid = '*') or (a.gcl03c >= @fromcardid and a.gcl03c <= @tocardid)) and ((@fromcardtype = '*') or (a.gcl15c >= @fromcardtype and a.gcl15c <= @tocardtype)) and a.gcl00c = z.gaz02c and z.gaz01c = @incompid and a.gcl06i = 6 and (@fromacctype = '' or (a.gcl04c >= @fromacctype and a.gcl04c <= @toacctype )) and s.gsb00c = a.gcl00c and s.gsb01c = 'O' and s.gsb02c = a.gcl04c) union all (select 'gz' billtype, 2 mode, gcl69c PayDesc, isnull(gcl70f, 0) PayMoney, gsb03c acctype, gcl14i upgradeflag from gcm10 a with (nolock), gam26 z with (nolock), gsm02 s with (nolock) where a.gcl80d >= @fromdate and a.gcl80d <= @todate and ((@fromcardid = '*') or (a.gcl03c >= @fromcardid and a.gcl03c <= @tocardid)) and ((@fromcardtype = '*') or (a.gcl15c >= @fromcardtype and a.gcl15c <= @tocardtype)) and a.gcl00c = z.gaz02c and z.gaz01c = @incompid and a.gcl06i = 6 and (@fromacctype = '' or (a.gcl04c >= @fromacctype and a.gcl04c <= @toacctype )) and s.gsb00c = a.gcl00c and s.gsb01c = 'O' and s.gsb02c = a.gcl04c) union all (select 'gz' billtype, 2 mode, gcl71c PayDesc, isnull(gcl72f, 0) PayMoney, gsb03c acctype, gcl14i upgradeflag from gcm10 a with (nolock), gam26 z with (nolock), gsm02 s with (nolock) where a.gcl80d >= @fromdate and a.gcl80d <= @todate and ((@fromcardid = '*') or (a.gcl03c >= @fromcardid and a.gcl03c <= @tocardid)) and ((@fromcardtype = '*') or (a.gcl15c >= @fromcardtype and a.gcl15c <= @tocardtype)) and a.gcl00c = z.gaz02c and z.gaz01c = @incompid and a.gcl06i = 6 and (@fromacctype = '' or (a.gcl04c >= @fromacctype and a.gcl04c <= @toacctype )) and s.gsb00c = a.gcl00c and s.gsb01c = 'O' and s.gsb02c = a.gcl04c) union all (select 'gz' billtype, 2 mode, gcl73c PayDesc, isnull(gcl74f, 0) PayMoney, gsb03c acctype, gcl14i upgradeflag from gcm10 a with (nolock), gam26 z with (nolock), gsm02 s with (nolock) where a.gcl80d >= @fromdate and a.gcl80d <= @todate and ((@fromcardid = '*') or (a.gcl03c >= @fromcardid and a.gcl03c <= @tocardid)) and ((@fromcardtype = '*') or (a.gcl15c >= @fromcardtype and a.gcl15c <= @tocardtype)) and a.gcl00c = z.gaz02c and z.gaz01c = @incompid and a.gcl06i = 6 and (@fromacctype = '' or (a.gcl04c >= @fromacctype and a.gcl04c <= @toacctype )) and s.gsb00c = a.gcl00c and s.gsb01c = 'O' and s.gsb02c = a.gcl04c)) p group by p.billtype, p.mode, p.PayDesc, p.acctype, p.upgradeflag if @include_proc = 1 begin insert #tbl_card_sale_detail(billtype, mode, PayDesc, PayMoney) select p.billtype, p.mode, p.PayDesc, sum(p.PayMoney) from ((select 'gz' billtype, 2 mode, gcm15c PayDesc, isnull(gcm16f, 0) PayMoney from gcm10 a with (nolock), gcm11 b with (nolock), gam26 z with (nolock) where a.gcl80d >= @fromdate and a.gcl80d <= @todate and a.gcl00c = b.gcm00c and a.gcl01c = b.gcm01c and ((@fromcardid = '*') or (a.gcl03c >= @fromcardid and a.gcl03c <= @tocardid)) and ((@fromcardtype = '*') or (a.gcl15c >= @fromcardtype and a.gcl15c <= @tocardtype)) and a.gcl00c = z.gaz02c and z.gaz01c = @incompid and a.gcl06i = 6) union all (select 'gz' billtype, 2 mode, gcm17c PayDesc, isnull(gcm18f, 0) PayMoney from gcm10 a with (nolock), gcm11 b with (nolock), gam26 z with (nolock) where a.gcl80d >= @fromdate and a.gcl80d <= @todate and a.gcl00c = b.gcm00c and a.gcl01c = b.gcm01c and ((@fromcardid = '*') or (a.gcl03c >= @fromcardid and a.gcl03c <= @tocardid)) and ((@fromcardtype = '*') or (a.gcl15c >= @fromcardtype and a.gcl15c <= @tocardtype)) and a.gcl00c = z.gaz02c and z.gaz01c = @incompid and a.gcl06i = 6) union all (select 'gz' billtype, 2 mode, gcm19c PayDesc, isnull(gcm20f, 0) PayMoney from gcm10 a with (nolock), gcm11 b with (nolock), gam26 z with (nolock) where a.gcl80d >= @fromdate and a.gcl80d <= @todate and a.gcl00c = b.gcm00c and a.gcl01c = b.gcm01c and ((@fromcardid = '*') or (a.gcl03c >= @fromcardid and a.gcl03c <= @tocardid)) and ((@fromcardtype = '*') or (a.gcl15c >= @fromcardtype and a.gcl15c <= @tocardtype)) and a.gcl00c = z.gaz02c and z.gaz01c = @incompid and a.gcl06i = 6) union all (select 'gz' billtype, 2 mode, gcm23c PayDesc, isnull(gcm24f, 0) PayMoney from gcm10 a with (nolock), gcm11 b with (nolock), gam26 z with (nolock) where a.gcl80d >= @fromdate and a.gcl80d <= @todate and a.gcl00c = b.gcm00c and a.gcl01c = b.gcm01c and ((@fromcardid = '*') or (a.gcl03c >= @fromcardid and a.gcl03c <= @tocardid)) and ((@fromcardtype = '*') or (a.gcl15c >= @fromcardtype and a.gcl15c <= @tocardtype)) and a.gcl00c = z.gaz02c and z.gaz01c = @incompid and a.gcl06i = 6 )) p group by p.billtype, p.mode, p.PayDesc end --取款部分 insert #tbl_card_sale_detail(billtype, mode, PayDesc, PayMoney, acctype, upgradeflag) select p.billtype, p.mode, p.PayDesc, sum(p.PayMoney), p.acctype, p.upgradeflag from ((select 'gz' billtype, 3 mode, gcl67c PayDesc, isnull(gcl68f, 0) PayMoney, gsb03c acctype, gcl14i upgradeflag from gcm10 a with (nolock), gam26 z with (nolock), gsm02 s with (nolock) where a.gcl80d >= @fromdate and a.gcl80d <= @todate and ((@fromcardid = '*') or (a.gcl03c >= @fromcardid and a.gcl03c <= @tocardid)) and ((@fromcardtype = '*') or (a.gcl15c >= @fromcardtype and a.gcl15c <= @tocardtype)) and a.gcl00c = z.gaz02c and z.gaz01c = @incompid and a.gcl06i = 1 and (@fromacctype = '' or (a.gcl04c >= @fromacctype and a.gcl04c <= @toacctype )) and s.gsb00c = a.gcl00c and s.gsb01c = 'O' and s.gsb02c = a.gcl04c) union all (select 'gz' billtype, 3 mode, gcl69c PayDesc, isnull(gcl70f, 0) PayMoney, gsb03c acctype, gcl14i upgradeflag from gcm10 a with (nolock), gam26 z with (nolock), gsm02 s with (nolock) where a.gcl80d >= @fromdate and a.gcl80d <= @todate and ((@fromcardid = '*') or (a.gcl03c >= @fromcardid and a.gcl03c <= @tocardid)) and ((@fromcardtype = '*') or (a.gcl15c >= @fromcardtype and a.gcl15c <= @tocardtype)) and a.gcl00c = z.gaz02c and z.gaz01c = @incompid and a.gcl06i = 1 and (@fromacctype = '' or (a.gcl04c >= @fromacctype and a.gcl04c <= @toacctype )) and s.gsb00c = a.gcl00c and s.gsb01c = 'O' and s.gsb02c = a.gcl04c) union all (select 'gz' billtype, 3 mode, gcl71c PayDesc, isnull(gcl72f, 0) PayMoney, gsb03c acctype, gcl14i upgradeflag from gcm10 a with (nolock), gam26 z with (nolock), gsm02 s with (nolock) where a.gcl80d >= @fromdate and a.gcl80d <= @todate and ((@fromcardid = '*') or (a.gcl03c >= @fromcardid and a.gcl03c <= @tocardid)) and ((@fromcardtype = '*') or (a.gcl15c >= @fromcardtype and a.gcl15c <= @tocardtype)) and a.gcl00c = z.gaz02c and z.gaz01c = @incompid and a.gcl06i = 1 and (@fromacctype = '' or (a.gcl04c >= @fromacctype and a.gcl04c <= @toacctype )) and s.gsb00c = a.gcl00c and s.gsb01c = 'O' and s.gsb02c = a.gcl04c) union all (select 'gz' billtype, 3 mode, gcl73c PayDesc, isnull(gcl74f, 0) PayMoney, gsb03c acctype, gcl14i upgradeflag from gcm10 a with (nolock), gam26 z with (nolock), gsm02 s with (nolock) where a.gcl80d >= @fromdate and a.gcl80d <= @todate and ((@fromcardid = '*') or (a.gcl03c >= @fromcardid and a.gcl03c <= @tocardid)) and ((@fromcardtype = '*') or (a.gcl15c >= @fromcardtype and a.gcl15c <= @tocardtype)) and a.gcl00c = z.gaz02c and z.gaz01c = @incompid and a.gcl06i = 1 and (@fromacctype = '' or (a.gcl04c >= @fromacctype and a.gcl04c <= @toacctype )) and s.gsb00c = a.gcl00c and s.gsb01c = 'O' and s.gsb02c = a.gcl04c)) p group by p.billtype, p.mode, p.PayDesc, p.acctype, p.upgradeflag if @include_proc = 1 begin insert #tbl_card_sale_detail(billtype, mode, PayDesc, PayMoney) select p.billtype, p.mode, p.PayDesc, sum(p.PayMoney) from ((select 'gz' billtype, 3 mode, gcm15c PayDesc, isnull(gcm16f, 0) PayMoney from gcm10 a with (nolock), gcm11 b with (nolock), gam26 z with (nolock) where a.gcl80d >= @fromdate and a.gcl80d <= @todate and a.gcl00c = b.gcm00c and a.gcl01c = b.gcm01c and ((@fromcardid = '*') or (a.gcl03c >= @fromcardid and a.gcl03c <= @tocardid)) and ((@fromcardtype = '*') or (a.gcl15c >= @fromcardtype and a.gcl15c <= @tocardtype)) and a.gcl00c = z.gaz02c and z.gaz01c = @incompid and a.gcl06i = 1) union all (select 'gz' billtype, 3 mode, gcm17c PayDesc, isnull(gcm18f, 0) PayMoney from gcm10 a with (nolock), gcm11 b with (nolock), gam26 z with (nolock) where a.gcl80d >= @fromdate and a.gcl80d <= @todate and a.gcl00c = b.gcm00c and a.gcl01c = b.gcm01c and ((@fromcardid = '*') or (a.gcl03c >= @fromcardid and a.gcl03c <= @tocardid)) and ((@fromcardtype = '*') or (a.gcl15c >= @fromcardtype and a.gcl15c <= @tocardtype)) and a.gcl00c = z.gaz02c and z.gaz01c = @incompid and a.gcl06i = 1) union all (select 'gz' billtype, 3 mode, gcm19c PayDesc, isnull(gcm20f, 0) PayMoney from gcm10 a with (nolock), gcm11 b with (nolock), gam26 z with (nolock) where a.gcl80d >= @fromdate and a.gcl80d <= @todate and a.gcl00c = b.gcm00c and a.gcl01c = b.gcm01c and ((@fromcardid = '*') or (a.gcl03c >= @fromcardid and a.gcl03c <= @tocardid)) and ((@fromcardtype = '*') or (a.gcl15c >= @fromcardtype and a.gcl15c <= @tocardtype)) and a.gcl00c = z.gaz02c and z.gaz01c = @incompid and a.gcl06i = 1) union all (select 'gz' billtype, 3 mode, gcm23c PayDesc, isnull(gcm24f, 0) PayMoney from gcm10 a with (nolock), gcm11 b with (nolock), gam26 z with (nolock) where a.gcl80d >= @fromdate and a.gcl80d <= @todate and a.gcl00c = b.gcm00c and a.gcl01c = b.gcm01c and ((@fromcardid = '*') or (a.gcl03c >= @fromcardid and a.gcl03c <= @tocardid)) and ((@fromcardtype = '*') or (a.gcl15c >= @fromcardtype and a.gcl15c <= @tocardtype)) and a.gcl00c = z.gaz02c and z.gaz01c = @incompid and a.gcl06i = 1)) p group by p.billtype, p.mode, p.PayDesc end -- 会员卡变更的费用 变更没有账户 insert #tbl_card_sale_detail(billtype, mode, PayDesc, PayMoney) select p.billtype, p.mode, p.PayDesc, sum(p.PayMoney) from ((select 'ge' billtype, 5 mode, gea22c PayDesc, isnull(gea23f, 0) PayMoney from gem01 a with (nolock), gcm01 b with (nolock), gam26 z with (nolock) where a.gea00c = b.gca00c and a.gea07c = b.gca01c and a.gea80d >= @fromdate and a.gea80d <= @todate and ((@fromcardid = '*') or (a.gea07c >= @fromcardid and a.gea07c <= @tocardid)) and ((@fromcardtype = '*') or (b.gca02c >= @fromcardtype and b.gca02c <= @tocardtype)) and a.gea00c = z.gaz02c and z.gaz01c = @incompid and a.gea02i = 4) union all (select 'ge' billtype, 5 mode, gea24c PayDesc, isnull(gea25f, 0) PayMoney from gem01 a with (nolock), gcm01 b with (nolock), gam26 z with (nolock) where a.gea00c = b.gca00c and a.gea07c = b.gca01c and a.gea80d >= @fromdate and a.gea80d <= @todate and ((@fromcardid = '*') or (a.gea07c >= @fromcardid and a.gea07c <= @tocardid)) and ((@fromcardtype = '*') or (b.gca02c >= @fromcardtype and b.gca02c <= @tocardtype)) and a.gea00c = z.gaz02c and z.gaz01c = @incompid and a.gea02i = 4) union all (select 'ge' billtype, 5 mode, gea26c PayDesc, isnull(gea27f, 0) PayMoney from gem01 a with (nolock), gcm01 b with (nolock), gam26 z with (nolock) where a.gea00c = b.gca00c and a.gea07c = b.gca01c and a.gea80d >= @fromdate and a.gea80d <= @todate and ((@fromcardid = '*') or (a.gea07c >= @fromcardid and a.gea07c <= @tocardid)) and ((@fromcardtype = '*') or (b.gca02c >= @fromcardtype and b.gca02c <= @tocardtype)) and a.gea00c = z.gaz02c and z.gaz01c = @incompid and a.gea02i = 4) --modify by lm 2015-09-22 (支付方式4取值第二销售人员) -- union all -- -- (select 'ge' billtype, -- 5 mode, -- gea28c PayDesc, -- isnull(gea29f, 0) PayMoney -- from gem01 a with (nolock), gcm01 b with (nolock), gam26 z with (nolock) -- where a.gea00c = b.gca00c -- and a.gea07c = b.gca01c -- and a.gea80d >= @fromdate -- and a.gea80d <= @todate -- and ((@fromcardid = '*') or (a.gea07c >= @fromcardid and a.gea07c <= @tocardid)) -- and ((@fromcardtype = '*') or (b.gca02c >= @fromcardtype and b.gca02c <= @tocardtype)) -- and a.gea00c = z.gaz02c -- and z.gaz01c = @incompid -- and a.gea02i = 4) ) p group by p.billtype, p.mode, p.PayDesc -- 代表查询所有的会员卡销售 select gsb03c PayName, PayDesc PayCode, Sum(PayMoney) PayMoney from #tbl_card_sale_detail, gsm02 with (nolock) where gsb00c = @incompid and gsb01c = '3' and PayDesc = gsb02c and (charindex('*', @saletype) > 0 or (charindex('2', @saletype) > 0 and (billtype = 'gz' and mode = 1 and isnull(upgradeflag, 0) = 1)) or (charindex('3', @saletype) > 0 and (billtype = 'gz' and mode = 3)) or (charindex('1', @saletype) > 0 and (mode = 1 and isnull(upgradeflag, 0) != 1)) or (charindex('0', @saletype) > 0 and (mode = 0 and isnull(upgradeflag, 0) != 1))) group by PayDesc, gsb03c drop table #tbl_card_sale_detail end GO /****** Object: StoredProcedure [dbo].[upg_stat_card_sale_ceshi] Script Date: 06/12/2017 06:20:43 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_stat_card_sale_ceshi] ( @incompid varchar(10), -- 公司别 @fromdate varchar(10), -- 开始日期 @todate varchar(10), -- 截至日期 @fromcardtype varchar(10), -- 开始卡类别 @tocardtype varchar(10), -- 截止卡类别 @fromacctype varchar(10), -- 开始账户类别 Add by Huzy 2011/03/22 @toacctype varchar(10), -- 截止账户类别 @fromcardid varchar(20), -- 开始卡号 @tocardid varchar(20), -- 截至卡号 @orderby int, -- 0按卡类别 + 日期 1日期 2卡号 @include_proc int, -- 是否包括疗程部分 @saletype varchar(20), -- 业务类型 add by liyh '3' - 取款 @currentpageindex int, @pagesize int, @pageall int ) as begin declare @sqlpage nvarchar(2614) declare @pagecount int declare @payment_0 varchar(10) declare @payment_3 varchar(10) declare @payment_$ varchar(10) declare @payment_u1 varchar(10) declare @payment_u2 varchar(10) declare @payment_u3 varchar(10) declare @payment_u4 varchar(10) declare @payment_u5 varchar(10) exec upg_isrevenue_payment @incompid, @payment_0 output, @payment_3 output, @payment_$ output, @payment_u1 output, @payment_u2 output, @payment_u3 output, @payment_u4 output, @payment_u5 output if @fromdate = '*' or @fromdate = '' begin select @fromdate = '19000101', @todate = '20901231' end if @fromacctype = '*' set @fromacctype = '' if @toacctype = '*' set @toacctype = '' if (isnull(@fromacctype, '') = '') if (isnull(@toacctype, '') <> '') set @fromacctype = @toacctype else set @fromacctype = '' else if (isnull(@toacctype, '') = '') set @toacctype = @fromacctype create table #tbl_card_sale_detail ( compid varchar(20) not null, -- 公司别 ddate varchar(10) not null, -- 日期 ttime varchar(10) not null, -- 时间 billid varchar(20) not null, -- 单号 billtype varchar(20) not null, -- 单别 mode int null, -- 方式 0 会员卡销售 1 充 2 还款 3 取款 4欠款 5-变更 saleamt float null, -- 单据总金额 accamt float null, -- 账户应收金额 充进账户的实收金额 Add by Huzy 2011/03/22 savedamt float null, -- 储值金额 procamt float null, -- 疗程金额 Add by Huzy 2011/03/22 cardid varchar(20) null, cardtype varchar(10) null, acctype varchar(40) null, -- 账户类别 Add by Huzy 2011/03/22 acctypecode varchar(20) null, memname varchar(40) null, memphone varchar(20) null, sales varchar(40) null, salesid1 varchar(100) null, salesid1_p float null, -- 第一分享比率 ADD BY LEIJI on 2012-7-26 salesid2 varchar(100) null, salesid2_p float null, -- 第二分享比率 salesid3 varchar(100) null, salesid3_p float null, -- 第三分享比率 memo varchar(60) null, upgradeflag int null, oldcardtype varchar(20) null, sendacct varchar(200) null, -- 赠送账户 sendamt float null, -- 赠送金额 pagecount float null ) create nonclustered index idx_tbl_card_sale_detail on #tbl_card_sale_detail(compid,billid,billtype) create table #tbl_card_sale_detail_page ( idd int identity not null, compid varchar(20) not null, -- 公司别 ddate varchar(10) not null, -- 日期 ttime varchar(10) not null, -- 时间 billid varchar(20) not null, -- 单号 billtype varchar(20) not null, -- 单别 mode int null, -- 方式 0 会员卡销售 1 充 2 还款 3 取款 4欠款 5-变更 saleamt float null, -- 单据总金额 accamt float null, -- 账户应收金额 充进账户的实收金额 Add by Huzy 2011/03/22 savedamt float null, -- 储值金额 procamt float null, -- 疗程金额 Add by Huzy 2011/03/22 cardid varchar(20) null, cardtype varchar(10) null, acctype varchar(40) null, -- 账户类别 Add by Huzy 2011/03/22 acctypecode varchar(20) null, memname varchar(40) null, memphone varchar(20) null, sales varchar(40) null, salesid1 varchar(100) null, salesid1_p float null, -- 第一分享比率 ADD BY LEIJI on 2012-7-26 salesid2 varchar(100) null, salesid2_p float null, -- 第二分享比率 salesid3 varchar(100) null, salesid3_p float null, -- 第三分享比率 memo varchar(60) null, upgradeflag int null, oldcardtype varchar(20) null, sendacct varchar(200) null, -- 赠送账户 sendamt float null, -- 赠送金额 pagecount float null primary key (idd) ) create table #tbl_card_sale_detail_page_sum ( id int identity not null, saleamt float null,--销售总额 accamt float null, -- 账户应收金额 sendamt float null, -- 赠送金额 procamt float null, -- 疗程金额 Add by Huzy 2011/03/2 savedamt float null, -- 储值金额 salecount float null, -- 销售数量 pagecount float null primary key (id) ) create table #tbl_card_sale_detail_page_cardtype_sum ( id int identity not null, cardtype varchar(40) null, saleamt float null,--销售总额 pagecount float null primary key (id) ) create table #gsm03 --add by lm 2015.12.03 ( compid varchar(20) not null, billid varchar(20) not null, -- 单号 billtype varchar(20) not null, -- 单别 memo varchar(60) null, ) create nonclustered index idx_gsm03 on #gsm03(compid,billid,billtype) create table #gcm11 ( gcm00c varchar(10) not null, --门店编号 gcm01c varchar(20) not null, --异动单号 gcm05f float null --疗程金额 ) create nonclustered index idx_gcm11 on #gcm11(gcm00c,gcm01c) create table #gnm03 -- 会员卡销售--疗程 ( gnc00c varchar(10) not null, -- 门店编号 gnc01c varchar(20) not null, -- 销售单号 gnc05f float null, -- 疗程金额 ) create nonclustered index idx_gnm03 on #gnm03(gnc00c,gnc01c) if @include_proc = 1 begin create table #tbl_card_sale_proc_detail ( compid varchar(20) not null, -- 公司别 billid varchar(20) not null, -- 单号 billtype varchar(20) not null, -- 单别 mode int null, -- 方式 0 会员卡销售 1 充 2 还款 3 取款 4欠款 5-变更 saleamt float null, cardid varchar(20) null, cardtype varchar(10) null ) create nonclustered index idx_tbl_card_sale_proc_detail on #tbl_card_sale_proc_detail(compid,billid,billtype) end create table #gnm01_SendAcct -- 会员卡销售赠送记录 ( gns00c varchar(10) not null, -- 门店编号 gns01c varchar(20) not null, -- 单据编号 gns02c varchar(20) not null, -- 单据类型 gns03c varchar(200) not null, -- 赠送账户 gns04f varchar(200) null, -- 赠送金额 ) create nonclustered index idx_gnm01_SendAcct on #gnm01_SendAcct(gns00c,gns01c,gns02c) insert #gnm01_SendAcct(gns00c, gns01c, gns02c, gns03c, gns04f) select distinct gns00c, gns01c, gns02c, stuff((select ';' + rtrim((select top 1 gsb03c from gsm02 with(nolock) where gsb00c = gns00c and gsb01c = 'O' and gsb02c = gns03c)) from gnm01_SendAcct where k.gns00c = gns00c and k.gns01c = gns01c and k.gns02c = gns02c for xml path('')), 1, 1, '') gns03c, stuff((select ';' + rtrim(gns04f) from gnm01_SendAcct with(nolock) where k.gns00c = gns00c and k.gns01c = gns01c and k.gns02c = gns02c for xml path('')), 1, 1, '') gns04f from gnm01_SendAcct k with(nolock) where k.gns00c = @incompid and k.gns02c = 'gn' and gns01c in (select gna01c from gnm01 with(nolock) where gna00c = @incompid and gna80d >= @fromdate and gna80d <= @todate) -- 卡销售 账户类别取自 gnb13i insert #tbl_card_sale_detail(compid, ddate, ttime, billid, billtype, mode, saleamt, cardid, cardtype, acctype, acctypecode, salesid1, salesid1_p, salesid2, salesid2_p, salesid3, salesid3_p, memo, savedamt, sendacct, sendamt) select gna00c, gna80d, gna12t, gna01c, 'gn', 0, amt = isnull(gnb16f,0) + isnull(gnb18f,0)+ isnull(gnb20f,0)+ isnull(gnb22f,0) , gnb03c, gnb02c, gsb03c, gnb13i, gna20c, gna27f, gna28c, gna29f, gna30c, gna31f, memo = case isnull(gnb15c, '') when '' then '' else gnb15c + ':' + convert(varchar(20), gnb16f) + ';' end + case isnull(gnb17c, '') when '' then '' else gnb17c + ':' + convert(varchar(20), gnb18f) + ';' end + case isnull(gnb19c, '') when '' then '' else gnb19c + ':' + convert(varchar(20), gnb20f) + ';' end + case isnull(gnb21c, '') when '' then '' else gnb21c + ':' + convert(varchar(20), gnb22f) end, gnb11f - (case when gnb15c = '5' then gnb16f else 0 end + case when gnb17c = '5' then gnb18f else 0 end + case when gnb19c = '5' then gnb20f else 0 end + case when gnb21c = '5' then gnb22f else 0 end), (select top 1 gsb03c from gsm02 with(nolock) where gsb00c = @incompid and gsb01c = 'O' and gsb02c = gnb23c), gnb24f from gnm02 b with (nolock), gnm01 a with (nolock), gam26 z with(nolock), gsm02 s with(nolock) where a.gna00c = b.gnb00c and a.gna01c = b.gnb01c and a.gna80d >= @fromdate and a.gna80d <= @todate and ((b.gnb03c >= @fromcardid and b.gnb03c <= @tocardid) or (@fromcardid = '*')) and ((b.gnb02c >= @fromcardtype and b.gnb02c <= @tocardtype) or (@fromcardtype = '*')) and a.gna00c = z.gaz02c and z.gaz01c = @incompid and s.gsb00c = a.gna00c and s.gsb02c = b.gnb13i and s.gsb01c = 'O' -- update a -- set a.sendacct = c.gns03c, -- a.sendamt = c.gns04f -- from #tbl_card_sale_detail a, #gnm01_SendAcct c -- where c.gns00c = a.compid and c.gns01c = a.billid and c.gns02c = 'gn' if @include_proc = 1 begin insert #tbl_card_sale_proc_detail(compid, billid, billtype, mode, saleamt) select gna00c, gna01c, 'gn', 0, amt = sum(isnull(gnc16f,0)+ isnull(gnc18f,0)+ isnull(gnc20f,0)+ isnull(gnc24f,0) ) from gnm03 b with (nolock), gnm01 a with (nolock), gam26 z with(nolock) where a.gna00c = b.gnc00c and a.gna01c = b.gnc01c and a.gna80d >= @fromdate and a.gna80d <= @todate and ((a.gna13c >= @fromcardid and a.gna13c <= @tocardid) or (@fromcardid = '*')) and a.gna00c = z.gaz02c and z.gaz01c = @incompid group by gna00c, gna01c end -- 卡充值 账户取自 gcl04c insert #tbl_card_sale_detail(compid, ddate, ttime, billid, billtype, mode, saleamt, cardid, cardtype, acctype, acctypecode, salesid1, salesid1_p, salesid2, salesid2_p, salesid3, salesid3_p, upgradeflag, memo, savedamt, sendacct, sendamt) select gcl00c, gcl80d, gcl56t, gcl01c, 'gz', 1, amt = isnull(gcl68f,0)+ isnull(gcl70f,0)+ isnull(gcl72f,0)+ isnull(gcl74f,0), gcl03c, gcl15c, gsb03c, gcl04c, gcl09c, gcl16f, gcl17c, gcl18f, gcl19c, gcl20f, gcl14i, memo = case isnull(gcl67c, '') when '' then '' else gcl67c + ':' + convert(varchar(20), gcl68f) + ';' end + case isnull(gcl69c, '') when '' then '' else gcl69c + ':' + convert(varchar(20), gcl70f) + ';' end + case isnull(gcl71c, '') when '' then '' else gcl71c + ':' + convert(varchar(20), gcl72f) + ';' end + case isnull(gcl73c, '') when '' then '' else gcl73c + ':' + convert(varchar(20), gcl74f) end, gcl65f - (case when gcl67c = '5' then gcl68f else 0 end + case when gcl69c = '5' then gcl70f else 0 end + case when gcl71c = '5' then gcl72f else 0 end + case when gcl73c = '5' then gcl74f else 0 end), (select top 1 gsb03c from gsm02 with(nolock) where gsb00c = @incompid and gsb01c = 'O' and gsb02c = gcl107c) gcl107c, isnull(gcl108f, 0) from gcm10 a with (nolock), gam26 z with(nolock), gsm02 s with(nolock) where a.gcl80d >= @fromdate and a.gcl80d <= @todate and ((a.gcl03c >= @fromcardid and a.gcl03c <= @tocardid) or (@fromcardid = '*')) and ((a.gcl15c >= @fromcardtype and a.gcl15c <= @tocardtype) or (@fromcardtype = '*')) and a.gcl00c = z.gaz02c and z.gaz01c = @incompid and a.gcl06i = 0 and s.gsb00c = a.gcl00c and s.gsb01c = 'O' and s.gsb02c = a.gcl04c if @include_proc = 1 begin insert #tbl_card_sale_proc_detail(compid, billid, billtype, mode, saleamt) select gcl00c, gcl01c, 'gz', 1, sum(isnull(gcm16f,0)+ isnull(gcm18f,0)+ isnull(gcm20f,0)+ isnull(gcm24f,0) ) from gcm10 a with (nolock), gcm11 b with (nolock), gam26 z with(nolock) where a.gcl80d >= @fromdate and a.gcl80d <= @todate and a.gcl00c = b.gcm00c and a.gcl01c = b.gcm01c and ((a.gcl03c >= @fromcardid and a.gcl03c <= @tocardid) or (@fromcardid = '*')) and ((a.gcl15c >= @fromcardtype and a.gcl15c <= @tocardtype) or (@fromcardtype = '*')) and a.gcl00c = z.gaz02c and z.gaz01c = @incompid and a.gcl06i = 0 group by gcl00c, gcl01c end insert #tbl_card_sale_detail(compid, ddate, ttime, billid, billtype, mode, saleamt, cardid, cardtype, acctype, acctypecode, salesid1, salesid1_p, salesid2, salesid2_p, salesid3, salesid3_p, upgradeflag, memo, savedamt, sendacct, sendamt) select gcl00c, gcl80d, gcl56t, gcl01c, 'gz', 2, amt = isnull(gcl68f,0)+ isnull(gcl70f,0)+ isnull(gcl72f,0)+ isnull(gcl74f,0) , gcl03c, gcl15c, gsb03c, gcl04c, gcl09c, gcl16f, gcl17c, gcl18f, gcl19c, gcl20f, gcl14i, memo = case isnull(gcl67c, '') when '' then '' else gcl67c + ':' + convert(varchar(20), gcl68f) + ';' end + case isnull(gcl69c, '') when '' then '' else gcl69c + ':' + convert(varchar(20), gcl70f) + ';' end + case isnull(gcl71c, '') when '' then '' else gcl71c + ':' + convert(varchar(20), gcl72f) + ';' end + case isnull(gcl73c, '') when '' then '' else gcl73c + ':' + convert(varchar(20), gcl74f) end, gcl65f - (case when gcl67c = '5' then gcl68f else 0 end + case when gcl69c = '5' then gcl70f else 0 end + case when gcl71c = '5' then gcl72f else 0 end + case when gcl73c = '5' then gcl74f else 0 end), (select top 1 gsb03c from gsm02 with(nolock) where gsb00c = @incompid and gsb01c = 'O' and gsb02c = gcl107c) gcl107c, isnull(gcl108f, 0) from gcm10 a with (nolock), gam26 z with(nolock), gsm02 s with(nolock) where a.gcl80d >= @fromdate and a.gcl80d <= @todate and ((a.gcl03c >= @fromcardid and a.gcl03c <= @tocardid) or (@fromcardid = '*')) and ((a.gcl15c >= @fromcardtype and a.gcl15c <= @tocardtype) or (@fromcardtype = '*')) and a.gcl00c = z.gaz02c and z.gaz01c = @incompid and a.gcl06i = 6 and s.gsb00c = a.gcl00c and s.gsb01c = 'O' and s.gsb02c = a.gcl04c if @include_proc = 1 begin insert #tbl_card_sale_proc_detail(compid, billid, billtype, mode, saleamt) select gcl00c, gcl01c, 'gz', 2, sum( isnull(gcm16f,0) + isnull(gcm18f,0) + isnull(gcm20f,0) + isnull(gcm24f,0) ) from gcm10 a with (nolock), gcm11 b with (nolock), gam26 z with(nolock) where a.gcl80d >= @fromdate and a.gcl80d <= @todate and a.gcl00c = b.gcm00c and a.gcl01c = b.gcm01c and ((a.gcl03c >= @fromcardid and a.gcl03c <= @tocardid) or (@fromcardid = '*')) and ((a.gcl15c >= @fromcardtype and a.gcl15c <= @tocardtype) or (@fromcardtype = '*')) and a.gcl00c = z.gaz02c and z.gaz01c = @incompid and a.gcl06i = 6 group by gcl00c, gcl01c end -- 取款部分 insert #tbl_card_sale_detail(compid, ddate, ttime, billid, billtype, mode, saleamt, cardid, cardtype, acctype, acctypecode, salesid1, salesid1_p, salesid2, salesid2_p, salesid3, salesid3_p, upgradeflag, memo, savedamt, sendacct, sendamt) select gcl00c, gcl80d, gcl56t, gcl01c, 'gz', 3, amt = isnull(gcl68f,0) + isnull(gcl70f,0) + isnull(gcl72f,0) + isnull(gcl74f,0) , gcl03c, gcl15c, gsb03c, gcl04c, gcl09c, gcl16f, gcl17c, gcl18f, gcl19c, gcl20f, gcl14i, memo = case isnull(gcl67c, '') when '' then '' else gcl67c + ':' + convert(varchar(20), gcl68f) + ';' end + case isnull(gcl69c, '') when '' then '' else gcl69c + ':' + convert(varchar(20), gcl70f) + ';' end + case isnull(gcl71c, '') when '' then '' else gcl71c + ':' + convert(varchar(20), gcl72f) + ';' end + case isnull(gcl73c, '') when '' then '' else gcl73c + ':' + convert(varchar(20), gcl74f) end, 0 - (gcl65f - (case when gcl67c = '5' then gcl68f else 0 end + case when gcl69c = '5' then gcl70f else 0 end + case when gcl71c = '5' then gcl72f else 0 end + case when gcl73c = '5' then gcl74f else 0 end)), (select top 1 gsb03c from gsm02 with(nolock) where gsb00c = @incompid and gsb01c = 'O' and gsb02c = gcl107c) gcl107c, isnull(gcl108f, 0) from gcm10 a with (nolock), gam26 z with(nolock), gsm02 s with(nolock) where a.gcl80d >= @fromdate and a.gcl80d <= @todate and ((a.gcl03c >= @fromcardid and a.gcl03c <= @tocardid) or (@fromcardid = '*')) and a.gcl00c = z.gaz02c and z.gaz01c = @incompid and a.gcl06i = 1 and s.gsb00c = a.gcl00c and s.gsb01c = 'O' and s.gsb02c = a.gcl04c if @include_proc = 1 begin insert #tbl_card_sale_proc_detail(compid, billid, billtype, mode, saleamt) select gcl00c, gcl01c, 'gz', 3, sum(isnull(gcm16f,0) + isnull(gcm18f,0)+ isnull(gcm20f,0) + isnull(gcm24f,0) ) from gcm10 a with (nolock), gcm11 b with (nolock), gam26 z with(nolock) where a.gcl80d >= @fromdate and a.gcl80d <= @todate and a.gcl00c = b.gcm00c and a.gcl01c = b.gcm01c and ((a.gcl03c >= @fromcardid and a.gcl03c <= @tocardid) or (@fromcardid = '*')) and ((a.gcl15c >= @fromcardtype and a.gcl15c <= @tocardtype) or (@fromcardtype = '*')) and a.gcl00c = z.gaz02c and z.gaz01c = @incompid and a.gcl06i = 1 group by gcl00c, gcl01c end -- 把买卡 和充值的 销售金额更新 到 账户销售金额列 accamt By Huzy 2011/03/22 update #tbl_card_sale_detail set accamt = saleamt -- 会员卡变更的费用 变更没有账户 insert #tbl_card_sale_detail(compid, ddate, ttime, billid, billtype, mode, saleamt, cardid, cardtype, salesid1, salesid2, salesid3, memo) select gea00c, gea80d, gea22t, gea01c, 'ge', 5, amt = isnull(gea23f,0)+ isnull(gea25f,0)+ isnull(gea27f,0)+ isnull(gea29f,0) , gea07c, gca02c, gea21c, '', '', memo = case isnull(gea22c, '') when '' then '' else gea22c + ':' + convert(varchar(20), gea23f) + ';' end + case isnull(gea24c, '') when '' then '' else gea24c + ':' + convert(varchar(20), gea25f) + ';' end + case isnull(gea26c, '') when '' then '' else gea26c + ':' + convert(varchar(20), gea27f) + ';' end from gem01 a with(nolock), gcm01 b with(nolock), gam26 z with(nolock) where a.gea00c = b.gca00c and a.gea07c = b.gca01c and a.gea80d >= @fromdate and a.gea80d <= @todate and ((a.gea07c >= @fromcardid and a.gea07c <= @tocardid) or (@fromcardid = '*')) and ((b.gca02c >= @fromcardtype and b.gca02c <= @tocardtype) or (@fromcardtype = '*')) and a.gea00c = z.gaz02c and z.gaz01c = @incompid and a.gea02i = 4 -- 把疗程部分更新过去, -- 销售总额 = 前面没有疗程的"销售总额"加上"疗程金额", 同时把疗程金额更新到 新增的列 procamt 中 By Huzy 2011/03/22 if @include_proc = 1 begin update a set a.saleamt = isnull(a.saleamt, 0) + isnull(b.saleamt, 0), a.procamt = isnull(b.saleamt, 0) from #tbl_card_sale_detail a, #tbl_card_sale_proc_detail b where a.compid = b.compid and a.billid = b.billid and a.billtype = b.billtype and a.mode = b.mode drop table #tbl_card_sale_proc_detail end create table #temp_member ( cardid varchar(20), gba03c varchar(40), gba08c varchar(20) ) insert into #temp_member(cardid, gba03c, gba08c) select a.cardid, c.gba03c, c.gba08c from (select distinct cardid from #tbl_card_sale_detail) a, gcm01 b with(nolock), gbm01 c with(nolock) where a.cardid = b.gca01c and b.gca00c = b.gca13d and c.gba01c = b.gca04c -- 更新会员卡的名称 update p set p.memname = q.gba03c, p.memphone = q.gba08c from #tbl_card_sale_detail p join #temp_member q on p.cardid = q.cardid drop table #temp_member --修改充值,卖卡备注 add by lm 2015.12.02 insert #gsm03 (compid , billid, billtype, memo ) select gsc00c ,gsc01c, billtype, dbo.memo_gsm03(gsc00c ,gsc01c, gsc02c) from gsm03 a with (nolock), #tbl_card_sale_detail b where a.gsc00c = b.compid and a.gsc01c = b.billid and a.gsc02c = b.billtype and (a.gsc02c = 'gz' or a.gsc02c = 'gn') update a set a.memo = b.memo from #tbl_card_sale_detail a, #gsm03 b where a.compid = b.compid and a.billid = b.billid and a.billtype = b.billtype update a set a.sales = b.haa02c, a.salesid1 = b.haa02c from #tbl_card_sale_detail a, ham01 b with(nolock) where a.compid = b.haa00c and a.salesid1 = b.haa01c update a set a.sales = isnull(a.sales, '') + ';' + b.haa02c, a.salesid2 = b.haa02c from #tbl_card_sale_detail a, ham01 b with(nolock) where a.compid = b.haa00c and a.salesid2 = b.haa01c update a set a.sales = isnull(a.sales, '') + ';' + b.haa02c, a.salesid3 = b.haa02c from #tbl_card_sale_detail a, ham01 b with(nolock) where a.compid = b.haa00c and a.salesid3 = b.haa01c -- 卡升级的情况吧老卡找过来 update a set a.oldcardtype = b.gcl52c from #tbl_card_sale_detail a, gcm10 b with(nolock) where a.compid = b.gcl00c and a.billid = b.gcl01c and a.billtype = 'gz' and a.mode = 1 and isnull(a.upgradeflag, 0) = 1 --if(( '4' >= @fromacctype and '4' <= @toacctype) or @fromacctype = '') if(@fromacctype = '4' and @toacctype = '4') begin --Start 增加疗程账户 add by lm 2015.12.03 --不是疗程账户 疗程金额清零 insert #gcm11 (gcm00c, gcm01c, gcm05f) select gcm00c, gcm01c, sum(gcm05f) from gcm11 a with(nolock) , #tbl_card_sale_detail b where a.gcm00c = b.compid and a.gcm01c = b.billid and b.billtype = 'gz' and b.acctypecode <> '4' group by gcm00c, gcm01c insert #gnm03 (gnc00c, gnc01c, gnc05f) select gnc00c, gnc01c, sum(gnc05f) from gnm03 a with(nolock) , #tbl_card_sale_detail b where a.gnc00c = b.compid and a.gnc01c = b.billid and b.billtype= 'gn' and b.acctypecode <> '4' group by gnc00c, gnc01c insert #tbl_card_sale_detail (compid , ddate ,ttime ,billid ,billtype, mode,saleamt,accamt,savedamt,procamt,cardid ,cardtype,acctype, memname ,memphone ,sales , salesid1,salesid1_p ,salesid2 ,salesid2_p ,salesid3 ,salesid3_p,memo ,upgradeflag , oldcardtype, sendacct, sendamt, acctypecode) select compid , ddate ,ttime ,billid ,billtype, mode,gcm05f, 0, 0,gcm05f,cardid ,cardtype,'疗程账户', memname ,memphone ,sales , salesid1,salesid1_p ,salesid2 ,salesid2_p ,salesid3 ,salesid3_p,memo ,upgradeflag , oldcardtype, sendacct, sendamt, '4' from #gcm11 a with(nolock) , #tbl_card_sale_detail b where a.gcm00c = b.compid and a.gcm01c = b.billid and b.billtype = 'gz' and b.acctypecode <> '4' insert #tbl_card_sale_detail (compid , ddate ,ttime ,billid ,billtype, mode,saleamt,accamt,savedamt,procamt,cardid ,cardtype,acctype, memname ,memphone ,sales , salesid1,salesid1_p ,salesid2 ,salesid2_p ,salesid3 ,salesid3_p,memo ,upgradeflag , oldcardtype, sendacct, sendamt, acctypecode) select compid , ddate ,ttime ,billid ,billtype, mode,gnc05f, 0, 0,gnc05f,cardid ,cardtype,'疗程账户', memname ,memphone ,sales , salesid1,salesid1_p ,salesid2 ,salesid2_p ,salesid3 ,salesid3_p,memo ,upgradeflag , oldcardtype, sendacct, sendamt, '4' from #gnm03 a with(nolock) , #tbl_card_sale_detail b where a.gnc00c = b.compid and a.gnc01c = b.billid and b.billtype= 'gn' and b.acctypecode <> '4' update a set a.procamt = 0 from #tbl_card_sale_detail a where (a.billtype = 'gz' or a.billtype = 'gn') and a.acctypecode <> '4' update a set a.procamt = 0, a.saleamt = a.accamt + a.procamt from #tbl_card_sale_detail a where (a.billtype = 'gz' or a.billtype = 'gn') and a.acctypecode <> '4' delete from #tbl_card_sale_detail where acctypecode <> '4' end if(@fromacctype = '2' and @toacctype = '2') --储值账户不显示购买的疗程 begin update a set a.procamt = 0 from #tbl_card_sale_detail a where (a.billtype = 'gz' or a.billtype = 'gn') and a.acctypecode = '2' update a set a.procamt = 0, a.saleamt = a.accamt + a.procamt from #tbl_card_sale_detail a where (a.billtype = 'gz' or a.billtype = 'gn') and a.acctypecode = '2' delete from #tbl_card_sale_detail where acctypecode <> '4' and isnull(accamt,0) = 0 and isnull(savedamt,0) = 0 end --End declare @saleamt float declare @saleamt3 float declare @accamt float declare @sendamt float declare @procamt float declare @savedamt float declare @savedamt3 float declare @salecount float if @orderby = 0 begin if(@pageall = 0) begin insert #tbl_card_sale_detail_page( compid , ddate ,ttime ,billid ,billtype, mode,saleamt,accamt,savedamt,procamt,cardid ,cardtype,acctype, memname ,memphone ,sales , salesid1,salesid1_p ,salesid2 ,salesid2_p ,salesid3 ,salesid3_p,memo ,upgradeflag , oldcardtype, sendacct, sendamt ) select compid , ddate ,ttime ,billid ,billtype, mode,saleamt,accamt,savedamt,procamt,cardid ,cardtype,acctype, memname ,memphone ,sales , salesid1,salesid1_p ,salesid2 ,salesid2_p ,salesid3 ,salesid3_p,memo ,upgradeflag , oldcardtype, sendacct, sendamt from #tbl_card_sale_detail where (@fromacctype = '' or (acctypecode >= @fromacctype and acctypecode <= @toacctype)) and charindex('*', @saletype) > 0 or (charindex('2', @saletype) > 0 and (billtype = 'gz' and mode = 1 and isnull(upgradeflag, 0) = 1)) or (charindex('3', @saletype) > 0 and (billtype = 'gz' and mode = 3)) or (charindex('4', @saletype) > 0 and (billtype = 'gz' and mode = 2)) or (charindex('1', @saletype) > 0 and (mode = 1 and isnull(upgradeflag, 0) != 1)) or (charindex('0', @saletype) > 0 and (mode = 0 and isnull(upgradeflag, 0) != 1)) order by cardtype asc, ddate asc, ttime asc, sales desc select @saleamt= sum(saleamt) from #tbl_card_sale_detail_page where mode in(0,1,2,3,5) select @accamt= sum(accamt) from #tbl_card_sale_detail_page where mode in(0,1,2,3,5) select @sendamt= sum(sendamt) from #tbl_card_sale_detail_page select @procamt= sum(procamt) from #tbl_card_sale_detail_page where mode in(0,1,2,3,5) select @savedamt= sum(savedamt) from #tbl_card_sale_detail_page where mode in(0,1,2,3,5) select @salecount =count(billid) from #tbl_card_sale_detail_page where mode in(0,1,5) select @pagecount = count(billid) from #tbl_card_sale_detail_page insert #tbl_card_sale_detail_page_sum(saleamt,accamt,sendamt,procamt,savedamt,salecount) select @saleamt ,@accamt,@sendamt,@procamt,@savedamt,@salecount insert #tbl_card_sale_detail_page_cardtype_sum (saleamt,pagecount,cardtype) select sum(saleamt),@pagecount,cardtype from #tbl_card_sale_detail_page group by cardtype update a set a.cardtype = b.gak02c from #tbl_card_sale_detail_page_cardtype_sum a, gam10 b with(nolock) where b.gak01c = a.cardtype and b.gak00c = @incompid update a set a.pagecount = @pagecount from #tbl_card_sale_detail_page a update a set a.pagecount = @pagecount from #tbl_card_sale_detail_page_sum a select @sqlpage = ' select top '+ cast(3 * @pagesize as nvarchar(20)) +' isnull(compid,'''') compid , isnull(ddate,'''') ddate ,isnull(ttime,'''') ttime ,isnull(billid,'''') billid ,isnull(billtype,'''') billtype , isnull(mode,0) mode ,isnull(saleamt,0) saleamt ,isnull(accamt,0) accamt ,isnull(savedamt,0) savedamt ,isnull(procamt,0) procamt ,isnull(cardid,'''') cardid ,isnull(cardtype,'''') cardtype ,isnull(acctype,'''') acctype, isnull(memname,'''') memname ,isnull(memphone,'''') memphone ,isnull(sales,'''') sales ,isnull(salesid1,'''') salesid1 ,isnull(salesid1_p,'''') salesid1_p ,isnull(salesid2,'''') salesid2, isnull(salesid2_p,'''') salesid2_p ,isnull(salesid3,'''') salesid3 ,isnull(salesid3_p,'''') salesid3_p,isnull(memo,'''') memo,isnull(upgradeflag,'''') upgradeflag , isnull(oldcardtype,'''') oldcardtype, isnull(sendacct,0) sendacct, isnull(sendamt,0) sendamt, isnull(pagecount,0) pagecount from #tbl_card_sale_detail_page where idd > '+cast(@currentpageindex * @pagesize as nvarchar(20))+ ' union all '+ ' select '''' compid , '''' ddate ,'''' ttime ,'''' billid ,'''' billtype, isnull(salecount,0) mode,isnull(saleamt,0) saleamt,isnull(accamt,0) accamt,isnull(savedamt,0) savedamt, isnull(procamt,0) procamt, '''' cardid , '''' cardtype, '''' acctype, '''' memname ,'''' memphone ,'''' sales , '''' salesid1, '''' salesid1_p ,'''' salesid2 ,'''' salesid2_p ,'''' salesid3 ,'''' salesid3_p,'''' memo ,'''' upgradeflag , '''' oldcardtype, '''' sendacct, isnull(sendamt,0) sendamt, isnull(pagecount,0) pagecount from #tbl_card_sale_detail_page_sum ' + ' union all '+ ' select ''cardtype'' compid , '''' ddate ,'''' ttime ,'''' billid ,'''' billtype, 0 mode,isnull(saleamt,0) saleamt,0 accamt,0 savedamt, 0 procamt, '''' cardid , isnull(cardtype,'''') cardtype, '''' acctype, '''' memname ,'''' memphone ,'''' sales , '''' salesid1, '''' salesid1_p ,'''' salesid2 ,'''' salesid2_p ,'''' salesid3 ,'''' salesid3_p,'''' memo ,'''' upgradeflag , '''' oldcardtype, '''' sendacct, 0 sendamt, isnull(pagecount,0) pagecount from #tbl_card_sale_detail_page_cardtype_sum ' print @sqlpage exec sp_executesql @sqlpage end else begin insert #tbl_card_sale_detail_page( compid , ddate ,ttime ,billid ,billtype, mode,saleamt,accamt,savedamt,procamt,cardid ,cardtype,acctype, memname ,memphone ,sales , salesid1,salesid1_p ,salesid2 ,salesid2_p ,salesid3 ,salesid3_p,memo ,upgradeflag , oldcardtype, sendacct, sendamt ) select compid , ddate ,ttime ,billid ,billtype, mode,saleamt,accamt,savedamt,procamt,cardid ,cardtype,acctype, memname ,memphone ,sales , salesid1,salesid1_p ,salesid2 ,salesid2_p ,salesid3 ,salesid3_p,memo ,upgradeflag , oldcardtype, sendacct, sendamt from #tbl_card_sale_detail where (@fromacctype = '' or (acctypecode >= @fromacctype and acctypecode <= @toacctype)) and charindex('*', @saletype) > 0 or (charindex('2', @saletype) > 0 and (billtype = 'gz' and mode = 1 and isnull(upgradeflag, 0) = 1)) or (charindex('3', @saletype) > 0 and (billtype = 'gz' and mode = 3)) or (charindex('4', @saletype) > 0 and (billtype = 'gz' and mode = 2)) or (charindex('1', @saletype) > 0 and (mode = 1 and isnull(upgradeflag, 0) != 1)) or (charindex('0', @saletype) > 0 and (mode = 0 and isnull(upgradeflag, 0) != 1)) order by cardtype asc, ddate asc, ttime asc, sales desc -- declare @saleamt float -- declare @saleamt3 float -- declare @accamt float -- declare @sendamt float -- declare @procamt float -- declare @savedamt float -- declare @savedamt3 float -- declare @salecount float select @saleamt= sum(saleamt) from #tbl_card_sale_detail_page where mode in(0,1,2,3,5) select @accamt= sum(accamt) from #tbl_card_sale_detail_page where mode in(0,1,2,3,5) select @sendamt= sum(sendamt) from #tbl_card_sale_detail_page select @procamt= sum(procamt) from #tbl_card_sale_detail_page where mode in(0,1,2,3,5) select @savedamt= sum(savedamt) from #tbl_card_sale_detail_page where mode in(0,1,2,3,5) select @salecount =count(billid) from #tbl_card_sale_detail_page where mode in(0,1,5) select @pagecount = count(billid) from #tbl_card_sale_detail_page insert #tbl_card_sale_detail_page_sum(saleamt,accamt,sendamt,procamt,savedamt,salecount) select @saleamt ,@accamt,@sendamt,@procamt,@savedamt,@salecount insert #tbl_card_sale_detail_page_cardtype_sum (saleamt,pagecount,cardtype) select sum(saleamt),@pagecount,cardtype from #tbl_card_sale_detail_page group by cardtype update a set a.cardtype = b.gak02c from #tbl_card_sale_detail_page_cardtype_sum a, gam10 b with(nolock) where b.gak01c = a.cardtype and b.gak00c = @incompid update a set a.pagecount = @pagecount from #tbl_card_sale_detail_page a update a set a.pagecount = @pagecount from #tbl_card_sale_detail_page_sum a select @sqlpage = ' select isnull(compid,'''') compid , isnull(ddate,'''') ddate ,isnull(ttime,'''') ttime ,isnull(billid,'''') billid ,isnull(billtype,'''') billtype , isnull(mode,0) mode ,isnull(saleamt,0) saleamt ,isnull(accamt,0) accamt ,isnull(savedamt,0) savedamt ,isnull(procamt,0) procamt ,isnull(cardid,'''') cardid ,isnull(cardtype,'''') cardtype ,isnull(acctype,'''') acctype, isnull(memname,'''') memname ,isnull(memphone,'''') memphone ,isnull(sales,'''') sales ,isnull(salesid1,'''') salesid1 ,isnull(salesid1_p,'''') salesid1_p ,isnull(salesid2,'''') salesid2, isnull(salesid2_p,'''') salesid2_p ,isnull(salesid3,'''') salesid3 ,isnull(salesid3_p,'''') salesid3_p,isnull(memo,'''') memo,isnull(upgradeflag,'''') upgradeflag , isnull(oldcardtype,'''') oldcardtype, isnull(sendacct,0) sendacct, isnull(sendamt,0) sendamt, isnull(pagecount,0) pagecount from #tbl_card_sale_detail_page '+ ' union all '+ ' select '''' compid , '''' ddate ,'''' ttime ,'''' billid ,'''' billtype, isnull(salecount,0) mode,isnull(saleamt,0) saleamt,isnull(accamt,0) accamt,isnull(savedamt,0) savedamt, isnull(procamt,0) procamt, '''' cardid , '''' cardtype, '''' acctype, '''' memname ,'''' memphone ,'''' sales , '''' salesid1, '''' salesid1_p ,'''' salesid2 ,'''' salesid2_p ,'''' salesid3 ,'''' salesid3_p,'''' memo ,'''' upgradeflag , '''' oldcardtype, '''' sendacct, isnull(sendamt,0) sendamt, isnull(pagecount,0) pagecount from #tbl_card_sale_detail_page_sum ' + ' union all '+ ' select ''cardtype'' compid , '''' ddate ,'''' ttime ,'''' billid ,'''' billtype, 0 mode,isnull(saleamt,0) saleamt,0 accamt,0 savedamt, 0 procamt, '''' cardid , isnull(cardtype,'''') cardtype, '''' acctype, '''' memname ,'''' memphone ,'''' sales , '''' salesid1, '''' salesid1_p ,'''' salesid2 ,'''' salesid2_p ,'''' salesid3 ,'''' salesid3_p,'''' memo ,'''' upgradeflag , '''' oldcardtype, '''' sendacct, 0 sendamt, isnull(pagecount,0) pagecount from #tbl_card_sale_detail_page_cardtype_sum ' exec sp_executesql @sqlpage end end else if @orderby = 1 begin if(@pageall = 0) begin insert #tbl_card_sale_detail_page( compid , ddate ,ttime ,billid ,billtype, mode,saleamt,accamt,savedamt,procamt,cardid ,cardtype,acctype, memname ,memphone ,sales , salesid1,salesid1_p ,salesid2 ,salesid2_p ,salesid3 ,salesid3_p,memo ,upgradeflag , oldcardtype, sendacct, sendamt ) select compid , ddate ,ttime ,billid ,billtype, mode,saleamt,accamt,savedamt,procamt,cardid ,cardtype,acctype, memname ,memphone ,sales , salesid1,salesid1_p ,salesid2 ,salesid2_p ,salesid3 ,salesid3_p,memo ,upgradeflag , oldcardtype, sendacct, sendamt from #tbl_card_sale_detail where (@fromacctype = '' or (acctypecode >= @fromacctype and acctypecode <= @toacctype)) and charindex('*', @saletype) > 0 or (charindex('2', @saletype) > 0 and (billtype = 'gz' and mode = 1 and isnull(upgradeflag, 0) = 1)) or (charindex('3', @saletype) > 0 and (billtype = 'gz' and mode = 3)) or (charindex('4', @saletype) > 0 and (billtype = 'gz' and mode = 2)) or (charindex('1', @saletype) > 0 and (mode = 1 and isnull(upgradeflag, 0) != 1)) or (charindex('0', @saletype) > 0 and (mode = 0 and isnull(upgradeflag, 0) != 1)) order by ddate asc, ttime asc, sales desc select @saleamt= sum(saleamt) from #tbl_card_sale_detail_page where mode in(0,1,2,3,5) select @accamt= sum(accamt) from #tbl_card_sale_detail_page where mode in(0,1,2,3,5) select @sendamt= sum(sendamt) from #tbl_card_sale_detail_page select @procamt= sum(procamt) from #tbl_card_sale_detail_page where mode in(0,1,2,3,5) select @savedamt= sum(savedamt) from #tbl_card_sale_detail_page where mode in(0,1,2,3,5) select @salecount =count(billid) from #tbl_card_sale_detail_page where mode in(0,1,5) select @pagecount = count(billid) from #tbl_card_sale_detail_page insert #tbl_card_sale_detail_page_sum(saleamt,accamt,sendamt,procamt,savedamt,salecount) select @saleamt ,@accamt,@sendamt,@procamt,@savedamt,@salecount insert #tbl_card_sale_detail_page_cardtype_sum (saleamt,pagecount,cardtype) select sum(saleamt),@pagecount,cardtype from #tbl_card_sale_detail_page group by cardtype update a set a.cardtype = b.gak02c from #tbl_card_sale_detail_page_cardtype_sum a, gam10 b with(nolock) where b.gak01c = a.cardtype and b.gak00c = @incompid update a set a.pagecount = @pagecount from #tbl_card_sale_detail_page a update a set a.pagecount = @pagecount from #tbl_card_sale_detail_page_sum a select @sqlpage = ' select top '+ cast(3 * @pagesize as nvarchar(20)) +' isnull(compid,'''') compid , isnull(ddate,'''') ddate ,isnull(ttime,'''') ttime ,isnull(billid,'''') billid ,isnull(billtype,'''') billtype , isnull(mode,0) mode ,isnull(saleamt,0) saleamt ,isnull(accamt,0) accamt ,isnull(savedamt,0) savedamt ,isnull(procamt,0) procamt ,isnull(cardid,'''') cardid ,isnull(cardtype,'''') cardtype ,isnull(acctype,'''') acctype, isnull(memname,'''') memname ,isnull(memphone,'''') memphone ,isnull(sales,'''') sales ,isnull(salesid1,'''') salesid1 ,isnull(salesid1_p,'''') salesid1_p ,isnull(salesid2,'''') salesid2, isnull(salesid2_p,'''') salesid2_p ,isnull(salesid3,'''') salesid3 ,isnull(salesid3_p,'''') salesid3_p,isnull(memo,'''') memo,isnull(upgradeflag,'''') upgradeflag , isnull(oldcardtype,'''') oldcardtype, isnull(sendacct,0) sendacct, isnull(sendamt,0) sendamt, isnull(pagecount,0) pagecount from #tbl_card_sale_detail_page where idd > '+cast(@currentpageindex * @pagesize as nvarchar(20))+ ' union all '+ ' select '''' compid , '''' ddate ,'''' ttime ,'''' billid ,'''' billtype, isnull(salecount,0) mode,isnull(saleamt,0) saleamt,isnull(accamt,0) accamt,isnull(savedamt,0) savedamt, isnull(procamt,0) procamt, '''' cardid , '''' cardtype, '''' acctype, '''' memname ,'''' memphone ,'''' sales , '''' salesid1, '''' salesid1_p ,'''' salesid2 ,'''' salesid2_p ,'''' salesid3 ,'''' salesid3_p,'''' memo ,'''' upgradeflag , '''' oldcardtype, '''' sendacct, isnull(sendamt,0) sendamt, isnull(pagecount,0) pagecount from #tbl_card_sale_detail_page_sum ' + ' union all '+ ' select ''cardtype'' compid , '''' ddate ,'''' ttime ,'''' billid ,'''' billtype, 0 mode,isnull(saleamt,0) saleamt,0 accamt,0 savedamt, 0 procamt, '''' cardid , isnull(cardtype,'''') cardtype, '''' acctype, '''' memname ,'''' memphone ,'''' sales , '''' salesid1, '''' salesid1_p ,'''' salesid2 ,'''' salesid2_p ,'''' salesid3 ,'''' salesid3_p,'''' memo ,'''' upgradeflag , '''' oldcardtype, '''' sendacct, 0 sendamt, isnull(pagecount,0) pagecount from #tbl_card_sale_detail_page_cardtype_sum ' print @sqlpage exec sp_executesql @sqlpage end else begin insert #tbl_card_sale_detail_page( compid , ddate ,ttime ,billid ,billtype, mode,saleamt,accamt,savedamt,procamt,cardid ,cardtype,acctype, memname ,memphone ,sales , salesid1,salesid1_p ,salesid2 ,salesid2_p ,salesid3 ,salesid3_p,memo ,upgradeflag , oldcardtype, sendacct, sendamt ) select compid , ddate ,ttime ,billid ,billtype, mode,saleamt,accamt,savedamt,procamt,cardid ,cardtype,acctype, memname ,memphone ,sales , salesid1,salesid1_p ,salesid2 ,salesid2_p ,salesid3 ,salesid3_p,memo ,upgradeflag , oldcardtype, sendacct, sendamt from #tbl_card_sale_detail where (@fromacctype = '' or (acctypecode >= @fromacctype and acctypecode <= @toacctype)) and charindex('*', @saletype) > 0 or (charindex('2', @saletype) > 0 and (billtype = 'gz' and mode = 1 and isnull(upgradeflag, 0) = 1)) or (charindex('3', @saletype) > 0 and (billtype = 'gz' and mode = 3)) or (charindex('4', @saletype) > 0 and (billtype = 'gz' and mode = 2)) or (charindex('1', @saletype) > 0 and (mode = 1 and isnull(upgradeflag, 0) != 1)) or (charindex('0', @saletype) > 0 and (mode = 0 and isnull(upgradeflag, 0) != 1)) order by ddate asc, ttime asc, sales desc -- declare @saleamt float -- declare @saleamt3 float -- declare @accamt float -- declare @sendamt float -- declare @procamt float -- declare @savedamt float -- declare @savedamt3 float -- declare @salecount float select @saleamt= sum(saleamt) from #tbl_card_sale_detail_page where mode in(0,1,2,3,5) select @accamt= sum(accamt) from #tbl_card_sale_detail_page where mode in(0,1,2,3,5) select @sendamt= sum(sendamt) from #tbl_card_sale_detail_page select @procamt= sum(procamt) from #tbl_card_sale_detail_page where mode in(0,1,2,3,5) select @savedamt= sum(savedamt) from #tbl_card_sale_detail_page where mode in(0,1,2,3,5) select @salecount =count(billid) from #tbl_card_sale_detail_page where mode in(0,1,5) select @pagecount = count(billid) from #tbl_card_sale_detail_page insert #tbl_card_sale_detail_page_sum(saleamt,accamt,sendamt,procamt,savedamt,salecount) select @saleamt ,@accamt,@sendamt,@procamt,@savedamt,@salecount insert #tbl_card_sale_detail_page_cardtype_sum (saleamt,pagecount,cardtype) select sum(saleamt),@pagecount,cardtype from #tbl_card_sale_detail_page group by cardtype update a set a.cardtype = b.gak02c from #tbl_card_sale_detail_page_cardtype_sum a, gam10 b with(nolock) where b.gak01c = a.cardtype and b.gak00c = @incompid update a set a.pagecount = @pagecount from #tbl_card_sale_detail_page a update a set a.pagecount = @pagecount from #tbl_card_sale_detail_page_sum a select @sqlpage = ' select isnull(compid,'''') compid , isnull(ddate,'''') ddate ,isnull(ttime,'''') ttime ,isnull(billid,'''') billid ,isnull(billtype,'''') billtype , isnull(mode,0) mode ,isnull(saleamt,0) saleamt ,isnull(accamt,0) accamt ,isnull(savedamt,0) savedamt ,isnull(procamt,0) procamt ,isnull(cardid,'''') cardid ,isnull(cardtype,'''') cardtype ,isnull(acctype,'''') acctype, isnull(memname,'''') memname ,isnull(memphone,'''') memphone ,isnull(sales,'''') sales ,isnull(salesid1,'''') salesid1 ,isnull(salesid1_p,'''') salesid1_p ,isnull(salesid2,'''') salesid2, isnull(salesid2_p,'''') salesid2_p ,isnull(salesid3,'''') salesid3 ,isnull(salesid3_p,'''') salesid3_p,isnull(memo,'''') memo,isnull(upgradeflag,'''') upgradeflag , isnull(oldcardtype,'''') oldcardtype, isnull(sendacct,0) sendacct, isnull(sendamt,0) sendamt, isnull(pagecount,0) pagecount from #tbl_card_sale_detail_page '+ ' union all '+ ' select '''' compid , '''' ddate ,'''' ttime ,'''' billid ,'''' billtype, isnull(salecount,0) mode,isnull(saleamt,0) saleamt,isnull(accamt,0) accamt,isnull(savedamt,0) savedamt, isnull(procamt,0) procamt, '''' cardid , '''' cardtype, '''' acctype, '''' memname ,'''' memphone ,'''' sales , '''' salesid1, '''' salesid1_p ,'''' salesid2 ,'''' salesid2_p ,'''' salesid3 ,'''' salesid3_p,'''' memo ,'''' upgradeflag , '''' oldcardtype, '''' sendacct, isnull(sendamt,0) sendamt, isnull(pagecount,0) pagecount from #tbl_card_sale_detail_page_sum ' + ' union all '+ ' select ''cardtype'' compid , '''' ddate ,'''' ttime ,'''' billid ,'''' billtype, 0 mode,isnull(saleamt,0) saleamt,0 accamt,0 savedamt, 0 procamt, '''' cardid , isnull(cardtype,'''') cardtype, '''' acctype, '''' memname ,'''' memphone ,'''' sales , '''' salesid1, '''' salesid1_p ,'''' salesid2 ,'''' salesid2_p ,'''' salesid3 ,'''' salesid3_p,'''' memo ,'''' upgradeflag , '''' oldcardtype, '''' sendacct, 0 sendamt, isnull(pagecount,0) pagecount from #tbl_card_sale_detail_page_cardtype_sum ' exec sp_executesql @sqlpage end end else if @orderby = 2 begin if(@pageall = 0) begin insert #tbl_card_sale_detail_page( compid , ddate ,ttime ,billid ,billtype, mode,saleamt,accamt,savedamt,procamt,cardid ,cardtype,acctype, memname ,memphone ,sales , salesid1,salesid1_p ,salesid2 ,salesid2_p ,salesid3 ,salesid3_p,memo ,upgradeflag , oldcardtype, sendacct, sendamt ) select compid , ddate ,ttime ,billid ,billtype, mode,saleamt,accamt,savedamt,procamt,cardid ,cardtype,acctype, memname ,memphone ,sales , salesid1,salesid1_p ,salesid2 ,salesid2_p ,salesid3 ,salesid3_p,memo ,upgradeflag , oldcardtype, sendacct, sendamt from #tbl_card_sale_detail where (@fromacctype = '' or (acctypecode >= @fromacctype and acctypecode <= @toacctype)) and charindex('*', @saletype) > 0 or (charindex('2', @saletype) > 0 and (billtype = 'gz' and mode = 1 and isnull(upgradeflag, 0) = 1)) or (charindex('3', @saletype) > 0 and (billtype = 'gz' and mode = 3)) or (charindex('3', @saletype) > 0 and (billtype = 'gz' and mode = 2)) or (charindex('4', @saletype) > 0 and (billtype = 'gz' and mode = 2)) or (charindex('1', @saletype) > 0 and (mode = 1 and isnull(upgradeflag, 0) != 1)) or (charindex('0', @saletype) > 0 and (mode = 0 and isnull(upgradeflag, 0) != 1)) order by cardid asc, sales desc select @saleamt= sum(saleamt) from #tbl_card_sale_detail_page where mode in(0,1,2,3,5) select @accamt= sum(accamt) from #tbl_card_sale_detail_page where mode in(0,1,2,3,5) select @sendamt= sum(sendamt) from #tbl_card_sale_detail_page select @procamt= sum(procamt) from #tbl_card_sale_detail_page where mode in(0,1,2,3,5) select @savedamt= sum(savedamt) from #tbl_card_sale_detail_page where mode in(0,1,2,3,5) select @salecount =count(billid) from #tbl_card_sale_detail_page where mode in(0,1,5) select @pagecount = count(billid) from #tbl_card_sale_detail_page insert #tbl_card_sale_detail_page_sum(saleamt,accamt,sendamt,procamt,savedamt,salecount) select @saleamt ,@accamt,@sendamt,@procamt,@savedamt,@salecount insert #tbl_card_sale_detail_page_cardtype_sum (saleamt,pagecount,cardtype) select sum(saleamt),@pagecount,cardtype from #tbl_card_sale_detail_page group by cardtype update a set a.cardtype = b.gak02c from #tbl_card_sale_detail_page_cardtype_sum a, gam10 b with(nolock) where b.gak01c = a.cardtype and b.gak00c = @incompid update a set a.pagecount = @pagecount from #tbl_card_sale_detail_page a update a set a.pagecount = @pagecount from #tbl_card_sale_detail_page_sum a select @sqlpage = ' select top '+ cast(3 * @pagesize as nvarchar(20)) +' isnull(compid,'''') compid , isnull(ddate,'''') ddate ,isnull(ttime,'''') ttime ,isnull(billid,'''') billid ,isnull(billtype,'''') billtype , isnull(mode,0) mode ,isnull(saleamt,0) saleamt ,isnull(accamt,0) accamt ,isnull(savedamt,0) savedamt ,isnull(procamt,0) procamt ,isnull(cardid,'''') cardid ,isnull(cardtype,'''') cardtype ,isnull(acctype,'''') acctype, isnull(memname,'''') memname ,isnull(memphone,'''') memphone ,isnull(sales,'''') sales ,isnull(salesid1,'''') salesid1 ,isnull(salesid1_p,'''') salesid1_p ,isnull(salesid2,'''') salesid2, isnull(salesid2_p,'''') salesid2_p ,isnull(salesid3,'''') salesid3 ,isnull(salesid3_p,'''') salesid3_p,isnull(memo,'''') memo,isnull(upgradeflag,'''') upgradeflag , isnull(oldcardtype,'''') oldcardtype, isnull(sendacct,0) sendacct, isnull(sendamt,0) sendamt, isnull(pagecount,0) pagecount from #tbl_card_sale_detail_page where idd > '+cast(@currentpageindex * @pagesize as nvarchar(20))+ ' union all '+ ' select '''' compid , '''' ddate ,'''' ttime ,'''' billid ,'''' billtype, isnull(salecount,0) mode,isnull(saleamt,0) saleamt,isnull(accamt,0) accamt,isnull(savedamt,0) savedamt, isnull(procamt,0) procamt, '''' cardid , '''' cardtype, '''' acctype, '''' memname ,'''' memphone ,'''' sales , '''' salesid1, '''' salesid1_p ,'''' salesid2 ,'''' salesid2_p ,'''' salesid3 ,'''' salesid3_p,'''' memo ,'''' upgradeflag , '''' oldcardtype, '''' sendacct, isnull(sendamt,0) sendamt, isnull(pagecount,0) pagecount from #tbl_card_sale_detail_page_sum ' + ' union all '+ ' select ''cardtype'' compid , '''' ddate ,'''' ttime ,'''' billid ,'''' billtype, 0 mode,isnull(saleamt,0) saleamt,0 accamt,0 savedamt, 0 procamt, '''' cardid , isnull(cardtype,'''') cardtype, '''' acctype, '''' memname ,'''' memphone ,'''' sales , '''' salesid1, '''' salesid1_p ,'''' salesid2 ,'''' salesid2_p ,'''' salesid3 ,'''' salesid3_p,'''' memo ,'''' upgradeflag , '''' oldcardtype, '''' sendacct, 0 sendamt, isnull(pagecount,0) pagecount from #tbl_card_sale_detail_page_cardtype_sum ' exec sp_executesql @sqlpage end else begin insert #tbl_card_sale_detail_page( compid , ddate ,ttime ,billid ,billtype, mode,saleamt,accamt,savedamt,procamt,cardid ,cardtype,acctype, memname ,memphone ,sales , salesid1,salesid1_p ,salesid2 ,salesid2_p ,salesid3 ,salesid3_p,memo ,upgradeflag , oldcardtype, sendacct, sendamt ) select compid , ddate ,ttime ,billid ,billtype, mode,saleamt,accamt,savedamt,procamt,cardid ,cardtype,acctype, memname ,memphone ,sales , salesid1,salesid1_p ,salesid2 ,salesid2_p ,salesid3 ,salesid3_p,memo ,upgradeflag , oldcardtype, sendacct, sendamt from #tbl_card_sale_detail where (@fromacctype = '' or (acctypecode >= @fromacctype and acctypecode <= @toacctype)) and charindex('*', @saletype) > 0 or (charindex('2', @saletype) > 0 and (billtype = 'gz' and mode = 1 and isnull(upgradeflag, 0) = 1)) or (charindex('3', @saletype) > 0 and (billtype = 'gz' and mode = 3)) or (charindex('3', @saletype) > 0 and (billtype = 'gz' and mode = 2)) or (charindex('4', @saletype) > 0 and (billtype = 'gz' and mode = 2)) or (charindex('1', @saletype) > 0 and (mode = 1 and isnull(upgradeflag, 0) != 1)) or (charindex('0', @saletype) > 0 and (mode = 0 and isnull(upgradeflag, 0) != 1)) order by cardid asc, sales desc select @saleamt= sum(saleamt) from #tbl_card_sale_detail_page where mode in(0,1,2,3,5) select @accamt= sum(accamt) from #tbl_card_sale_detail_page where mode in(0,1,2,3,5) select @sendamt= sum(sendamt) from #tbl_card_sale_detail_page select @procamt= sum(procamt) from #tbl_card_sale_detail_page where mode in(0,1,2,3,5) select @savedamt= sum(savedamt) from #tbl_card_sale_detail_page where mode in(0,1,2,3,5) select @salecount =count(billid) from #tbl_card_sale_detail_page where mode in(0,1,5) select @pagecount = count(billid) from #tbl_card_sale_detail_page insert #tbl_card_sale_detail_page_sum(saleamt,accamt,sendamt,procamt,savedamt,salecount) select @saleamt ,@accamt,@sendamt,@procamt,@savedamt,@salecount insert #tbl_card_sale_detail_page_cardtype_sum (saleamt,pagecount,cardtype) select sum(saleamt),@pagecount,cardtype from #tbl_card_sale_detail_page group by cardtype update a set a.cardtype = b.gak02c from #tbl_card_sale_detail_page_cardtype_sum a, gam10 b with(nolock) where b.gak01c = a.cardtype and b.gak00c = @incompid update a set a.pagecount = @pagecount from #tbl_card_sale_detail_page a update a set a.pagecount = @pagecount from #tbl_card_sale_detail_page_sum a select @sqlpage = ' select isnull(compid,'''') compid , isnull(ddate,'''') ddate ,isnull(ttime,'''') ttime ,isnull(billid,'''') billid ,isnull(billtype,'''') billtype , isnull(mode,0) mode ,isnull(saleamt,0) saleamt ,isnull(accamt,0) accamt ,isnull(savedamt,0) savedamt ,isnull(procamt,0) procamt ,isnull(cardid,'''') cardid ,isnull(cardtype,'''') cardtype ,isnull(acctype,'''') acctype, isnull(memname,'''') memname ,isnull(memphone,'''') memphone ,isnull(sales,'''') sales ,isnull(salesid1,'''') salesid1 ,isnull(salesid1_p,'''') salesid1_p ,isnull(salesid2,'''') salesid2, isnull(salesid2_p,'''') salesid2_p ,isnull(salesid3,'''') salesid3 ,isnull(salesid3_p,'''') salesid3_p,isnull(memo,'''') memo,isnull(upgradeflag,'''') upgradeflag , isnull(oldcardtype,'''') oldcardtype, isnull(sendacct,0) sendacct, isnull(sendamt,0) sendamt, isnull(pagecount,0) pagecount from #tbl_card_sale_detail_page '+ ' union all '+ ' select '''' compid , '''' ddate ,'''' ttime ,'''' billid ,'''' billtype, isnull(salecount,0) mode,isnull(saleamt,0) saleamt,isnull(accamt,0) accamt,isnull(savedamt,0) savedamt, isnull(procamt,0) procamt, '''' cardid , '''' cardtype, '''' acctype, '''' memname ,'''' memphone ,'''' sales , '''' salesid1, '''' salesid1_p ,'''' salesid2 ,'''' salesid2_p ,'''' salesid3 ,'''' salesid3_p,'''' memo ,'''' upgradeflag , '''' oldcardtype, '''' sendacct, isnull(sendamt,0) sendamt, isnull(pagecount,0) pagecount from #tbl_card_sale_detail_page_sum ' + ' union all '+ ' select ''cardtype'' compid , '''' ddate ,'''' ttime ,'''' billid ,'''' billtype, 0 mode,isnull(saleamt,0) saleamt,0 accamt,0 savedamt, 0 procamt, '''' cardid , isnull(cardtype,'''') cardtype, '''' acctype, '''' memname ,'''' memphone ,'''' sales , '''' salesid1, '''' salesid1_p ,'''' salesid2 ,'''' salesid2_p ,'''' salesid3 ,'''' salesid3_p,'''' memo ,'''' upgradeflag , '''' oldcardtype, '''' sendacct, 0 sendamt, isnull(pagecount,0) pagecount from #tbl_card_sale_detail_page_cardtype_sum ' exec sp_executesql @sqlpage end end else if @orderby = 3 begin if(@pageall = 0) begin insert #tbl_card_sale_detail_page( compid , ddate ,ttime ,billid ,billtype, mode,saleamt,accamt,savedamt,procamt,cardid ,cardtype,acctype, memname ,memphone ,sales , salesid1,salesid1_p ,salesid2 ,salesid2_p ,salesid3 ,salesid3_p,memo ,upgradeflag , oldcardtype, sendacct, sendamt ) select compid , ddate ,ttime ,billid ,billtype, mode,saleamt,accamt,savedamt,procamt,cardid ,cardtype,acctype, memname ,memphone ,sales , salesid1,salesid1_p ,salesid2 ,salesid2_p ,salesid3 ,salesid3_p,memo ,upgradeflag , oldcardtype, sendacct, sendamt from #tbl_card_sale_detail where (@fromacctype = '' or (acctypecode >= @fromacctype and acctypecode <= @toacctype)) and charindex('*', @saletype) > 0 or (charindex('2', @saletype) > 0 and (billtype = 'gz' and mode = 1 and isnull(upgradeflag, 0) = 1)) or (charindex('3', @saletype) > 0 and (billtype = 'gz' and mode = 3)) or (charindex('4', @saletype) > 0 and (billtype = 'gz' and mode = 2)) or (charindex('1', @saletype) > 0 and (mode = 1 and isnull(upgradeflag, 0) != 1)) or (charindex('0', @saletype) > 0 and (mode = 0 and isnull(upgradeflag, 0) != 1)) order by cardtype asc select @saleamt= sum(saleamt) from #tbl_card_sale_detail_page where mode in(0,1,2,3,5) select @accamt= sum(accamt) from #tbl_card_sale_detail_page where mode in(0,1,2,3,5) select @sendamt= sum(sendamt) from #tbl_card_sale_detail_page select @procamt= sum(procamt) from #tbl_card_sale_detail_page where mode in(0,1,2,3,5) select @savedamt= sum(savedamt) from #tbl_card_sale_detail_page where mode in(0,1,2,3,5) select @salecount =count(billid) from #tbl_card_sale_detail_page where mode in(0,1,5) select @pagecount = count(billid) from #tbl_card_sale_detail_page insert #tbl_card_sale_detail_page_sum(saleamt,accamt,sendamt,procamt,savedamt,salecount) select @saleamt ,@accamt,@sendamt,@procamt,@savedamt,@salecount insert #tbl_card_sale_detail_page_cardtype_sum (saleamt,pagecount,cardtype) select sum(saleamt),@pagecount,cardtype from #tbl_card_sale_detail_page group by cardtype update a set a.cardtype = b.gak02c from #tbl_card_sale_detail_page_cardtype_sum a, gam10 b with(nolock) where b.gak01c = a.cardtype and b.gak00c = @incompid update a set a.pagecount = @pagecount from #tbl_card_sale_detail_page a update a set a.pagecount = @pagecount from #tbl_card_sale_detail_page_sum a select @sqlpage = ' select top '+ cast(3 * @pagesize as nvarchar(20)) +' isnull(compid,'''') compid , isnull(ddate,'''') ddate ,isnull(ttime,'''') ttime ,isnull(billid,'''') billid ,isnull(billtype,'''') billtype , isnull(mode,0) mode ,isnull(saleamt,0) saleamt ,isnull(accamt,0) accamt ,isnull(savedamt,0) savedamt ,isnull(procamt,0) procamt ,isnull(cardid,'''') cardid ,isnull(cardtype,'''') cardtype ,isnull(acctype,'''') acctype, isnull(memname,'''') memname ,isnull(memphone,'''') memphone ,isnull(sales,'''') sales ,isnull(salesid1,'''') salesid1 ,isnull(salesid1_p,'''') salesid1_p ,isnull(salesid2,'''') salesid2, isnull(salesid2_p,'''') salesid2_p ,isnull(salesid3,'''') salesid3 ,isnull(salesid3_p,'''') salesid3_p,isnull(memo,'''') memo,isnull(upgradeflag,'''') upgradeflag , isnull(oldcardtype,'''') oldcardtype, isnull(sendacct,0) sendacct, isnull(sendamt,0) sendamt, isnull(pagecount,0) pagecount from #tbl_card_sale_detail_page where idd > '+cast(@currentpageindex * @pagesize as nvarchar(20))+ ' union all '+ ' select '''' compid , '''' ddate ,'''' ttime ,'''' billid ,'''' billtype, isnull(salecount,0) mode,isnull(saleamt,0) saleamt,isnull(accamt,0) accamt,isnull(savedamt,0) savedamt, isnull(procamt,0) procamt, '''' cardid , '''' cardtype, '''' acctype, '''' memname ,'''' memphone ,'''' sales , '''' salesid1, '''' salesid1_p ,'''' salesid2 ,'''' salesid2_p ,'''' salesid3 ,'''' salesid3_p,'''' memo ,'''' upgradeflag , '''' oldcardtype, '''' sendacct, isnull(sendamt,0) sendamt, isnull(pagecount,0) pagecount from #tbl_card_sale_detail_page_sum ' + ' union all '+ ' select ''cardtype'' compid , '''' ddate ,'''' ttime ,'''' billid ,'''' billtype, 0 mode,isnull(saleamt,0) saleamt,0 accamt,0 savedamt, 0 procamt, '''' cardid , isnull(cardtype,'''') cardtype, '''' acctype, '''' memname ,'''' memphone ,'''' sales , '''' salesid1, '''' salesid1_p ,'''' salesid2 ,'''' salesid2_p ,'''' salesid3 ,'''' salesid3_p,'''' memo ,'''' upgradeflag , '''' oldcardtype, '''' sendacct, 0 sendamt, isnull(pagecount,0) pagecount from #tbl_card_sale_detail_page_cardtype_sum ' print @sqlpage exec sp_executesql @sqlpage end else begin insert #tbl_card_sale_detail_page( compid , ddate ,ttime ,billid ,billtype, mode,saleamt,accamt,savedamt,procamt,cardid ,cardtype,acctype, memname ,memphone ,sales , salesid1,salesid1_p ,salesid2 ,salesid2_p ,salesid3 ,salesid3_p,memo ,upgradeflag , oldcardtype, sendacct, sendamt ) select compid , ddate ,ttime ,billid ,billtype, mode,saleamt,accamt,savedamt,procamt,cardid ,cardtype,acctype, memname ,memphone ,sales , salesid1,salesid1_p ,salesid2 ,salesid2_p ,salesid3 ,salesid3_p,memo ,upgradeflag , oldcardtype, sendacct, sendamt from #tbl_card_sale_detail where (@fromacctype = '' or (acctypecode >= @fromacctype and acctypecode <= @toacctype)) and charindex('*', @saletype) > 0 or (charindex('2', @saletype) > 0 and (billtype = 'gz' and mode = 1 and isnull(upgradeflag, 0) = 1)) or (charindex('3', @saletype) > 0 and (billtype = 'gz' and mode = 3)) or (charindex('4', @saletype) > 0 and (billtype = 'gz' and mode = 2)) or (charindex('1', @saletype) > 0 and (mode = 1 and isnull(upgradeflag, 0) != 1)) or (charindex('0', @saletype) > 0 and (mode = 0 and isnull(upgradeflag, 0) != 1)) order by cardtype asc select @saleamt= sum(saleamt) from #tbl_card_sale_detail_page where mode in(0,1,2,3,5) select @accamt= sum(accamt) from #tbl_card_sale_detail_page where mode in(0,1,2,3,5) select @sendamt= sum(sendamt) from #tbl_card_sale_detail_page select @procamt= sum(procamt) from #tbl_card_sale_detail_page where mode in(0,1,2,3,5) select @savedamt= sum(savedamt) from #tbl_card_sale_detail_page where mode in(0,1,2,3,5) select @salecount =count(billid) from #tbl_card_sale_detail_page where mode in(0,1,5) select @pagecount = count(billid) from #tbl_card_sale_detail_page insert #tbl_card_sale_detail_page_sum(saleamt,accamt,sendamt,procamt,savedamt,salecount) select @saleamt ,@accamt,@sendamt,@procamt,@savedamt,@salecount insert #tbl_card_sale_detail_page_cardtype_sum (saleamt,pagecount,cardtype) select sum(saleamt),@pagecount,cardtype from #tbl_card_sale_detail_page group by cardtype update a set a.cardtype = b.gak02c from #tbl_card_sale_detail_page_cardtype_sum a, gam10 b with(nolock) where b.gak01c = a.cardtype and b.gak00c = @incompid update a set a.pagecount = @pagecount from #tbl_card_sale_detail_page a update a set a.pagecount = @pagecount from #tbl_card_sale_detail_page_sum a select @sqlpage = ' select isnull(compid,'''') compid , isnull(ddate,'''') ddate ,isnull(ttime,'''') ttime ,isnull(billid,'''') billid ,isnull(billtype,'''') billtype , isnull(mode,0) mode ,isnull(saleamt,0) saleamt ,isnull(accamt,0) accamt ,isnull(savedamt,0) savedamt ,isnull(procamt,0) procamt ,isnull(cardid,'''') cardid ,isnull(cardtype,'''') cardtype ,isnull(acctype,'''') acctype, isnull(memname,'''') memname ,isnull(memphone,'''') memphone ,isnull(sales,'''') sales ,isnull(salesid1,'''') salesid1 ,isnull(salesid1_p,'''') salesid1_p ,isnull(salesid2,'''') salesid2, isnull(salesid2_p,'''') salesid2_p ,isnull(salesid3,'''') salesid3 ,isnull(salesid3_p,'''') salesid3_p,isnull(memo,'''') memo,isnull(upgradeflag,'''') upgradeflag , isnull(oldcardtype,'''') oldcardtype, isnull(sendacct,0) sendacct, isnull(sendamt,0) sendamt, isnull(pagecount,0) pagecount from #tbl_card_sale_detail_page '+ ' union all '+ ' select '''' compid , '''' ddate ,'''' ttime ,'''' billid ,'''' billtype, isnull(salecount,0) mode,isnull(saleamt,0) saleamt,isnull(accamt,0) accamt,isnull(savedamt,0) savedamt, isnull(procamt,0) procamt, '''' cardid , '''' cardtype, '''' acctype, '''' memname ,'''' memphone ,'''' sales , '''' salesid1, '''' salesid1_p ,'''' salesid2 ,'''' salesid2_p ,'''' salesid3 ,'''' salesid3_p,'''' memo ,'''' upgradeflag , '''' oldcardtype, '''' sendacct, isnull(sendamt,0) sendamt, isnull(pagecount,0) pagecount from #tbl_card_sale_detail_page_sum ' + ' union all '+ ' select ''cardtype'' compid , '''' ddate ,'''' ttime ,'''' billid ,'''' billtype, 0 mode,isnull(saleamt,0) saleamt,0 accamt,0 savedamt, 0 procamt, '''' cardid , isnull(cardtype,'''') cardtype, '''' acctype, '''' memname ,'''' memphone ,'''' sales , '''' salesid1, '''' salesid1_p ,'''' salesid2 ,'''' salesid2_p ,'''' salesid3 ,'''' salesid3_p,'''' memo ,'''' upgradeflag , '''' oldcardtype, '''' sendacct, 0 sendamt, isnull(pagecount,0) pagecount from #tbl_card_sale_detail_page_cardtype_sum ' exec sp_executesql @sqlpage end end else if @orderby = 4 begin if(@pageall = 0) begin insert #tbl_card_sale_detail_page( compid , ddate ,ttime ,billid ,billtype, mode,saleamt,accamt,savedamt,procamt,cardid ,cardtype,acctype, memname ,memphone ,sales , salesid1,salesid1_p ,salesid2 ,salesid2_p ,salesid3 ,salesid3_p,memo ,upgradeflag , oldcardtype, sendacct, sendamt ) select compid , ddate ,ttime ,billid ,billtype, mode,saleamt,accamt,savedamt,procamt,cardid ,cardtype,acctype, memname ,memphone ,sales , salesid1,salesid1_p ,salesid2 ,salesid2_p ,salesid3 ,salesid3_p,memo ,upgradeflag , oldcardtype, sendacct, sendamt from #tbl_card_sale_detail where (@fromacctype = '' or (acctypecode >= @fromacctype and acctypecode <= @toacctype)) and charindex('*', @saletype) > 0 or (charindex('2', @saletype) > 0 and (billtype = 'gz' and mode = 1 and isnull(upgradeflag, 0) = 1)) or (charindex('3', @saletype) > 0 and (billtype = 'gz' and mode = 3)) or (charindex('4', @saletype) > 0 and (billtype = 'gz' and mode = 2)) or (charindex('1', @saletype) > 0 and (mode = 1 and isnull(upgradeflag, 0) != 1)) or (charindex('0', @saletype) > 0 and (mode = 0 and isnull(upgradeflag, 0) != 1)) order by saleamt asc select @saleamt= sum(saleamt) from #tbl_card_sale_detail_page where mode in(0,1,2,3,5) select @accamt= sum(accamt) from #tbl_card_sale_detail_page where mode in(0,1,2,3,5) select @sendamt= sum(sendamt) from #tbl_card_sale_detail_page select @procamt= sum(procamt) from #tbl_card_sale_detail_page where mode in(0,1,2,3,5) select @savedamt= sum(savedamt) from #tbl_card_sale_detail_page where mode in(0,1,2,3,5) select @salecount =count(billid) from #tbl_card_sale_detail_page where mode in(0,1,5) select @pagecount = count(billid) from #tbl_card_sale_detail_page insert #tbl_card_sale_detail_page_sum(saleamt,accamt,sendamt,procamt,savedamt,salecount) select @saleamt ,@accamt,@sendamt,@procamt,@savedamt,@salecount insert #tbl_card_sale_detail_page_cardtype_sum (saleamt,pagecount,cardtype) select sum(saleamt),@pagecount,cardtype from #tbl_card_sale_detail_page group by cardtype update a set a.cardtype = b.gak02c from #tbl_card_sale_detail_page_cardtype_sum a, gam10 b with(nolock) where b.gak01c = a.cardtype and b.gak00c = @incompid update a set a.pagecount = @pagecount from #tbl_card_sale_detail_page a update a set a.pagecount = @pagecount from #tbl_card_sale_detail_page_sum a select @sqlpage = ' select top '+ cast(3 * @pagesize as nvarchar(20)) +' isnull(compid,'''') compid , isnull(ddate,'''') ddate ,isnull(ttime,'''') ttime ,isnull(billid,'''') billid ,isnull(billtype,'''') billtype , isnull(mode,0) mode ,isnull(saleamt,0) saleamt ,isnull(accamt,0) accamt ,isnull(savedamt,0) savedamt ,isnull(procamt,0) procamt ,isnull(cardid,'''') cardid ,isnull(cardtype,'''') cardtype ,isnull(acctype,'''') acctype, isnull(memname,'''') memname ,isnull(memphone,'''') memphone ,isnull(sales,'''') sales ,isnull(salesid1,'''') salesid1 ,isnull(salesid1_p,'''') salesid1_p ,isnull(salesid2,'''') salesid2, isnull(salesid2_p,'''') salesid2_p ,isnull(salesid3,'''') salesid3 ,isnull(salesid3_p,'''') salesid3_p,isnull(memo,'''') memo,isnull(upgradeflag,'''') upgradeflag , isnull(oldcardtype,'''') oldcardtype, isnull(sendacct,0) sendacct, isnull(sendamt,0) sendamt, isnull(pagecount,0) pagecount from #tbl_card_sale_detail_page where idd > '+cast(@currentpageindex * @pagesize as nvarchar(20))+ ' union all '+ ' select '''' compid , '''' ddate ,'''' ttime ,'''' billid ,'''' billtype, isnull(salecount,0) mode,isnull(saleamt,0) saleamt,isnull(accamt,0) accamt,isnull(savedamt,0) savedamt, isnull(procamt,0) procamt, '''' cardid , '''' cardtype, '''' acctype, '''' memname ,'''' memphone ,'''' sales , '''' salesid1, '''' salesid1_p ,'''' salesid2 ,'''' salesid2_p ,'''' salesid3 ,'''' salesid3_p,'''' memo ,'''' upgradeflag , '''' oldcardtype, '''' sendacct, isnull(sendamt,0) sendamt, isnull(pagecount,0) pagecount from #tbl_card_sale_detail_page_sum ' + ' union all '+ ' select ''cardtype'' compid , '''' ddate ,'''' ttime ,'''' billid ,'''' billtype, 0 mode,isnull(saleamt,0) saleamt,0 accamt,0 savedamt, 0 procamt, '''' cardid , isnull(cardtype,'''') cardtype, '''' acctype, '''' memname ,'''' memphone ,'''' sales , '''' salesid1, '''' salesid1_p ,'''' salesid2 ,'''' salesid2_p ,'''' salesid3 ,'''' salesid3_p,'''' memo ,'''' upgradeflag , '''' oldcardtype, '''' sendacct, 0 sendamt, isnull(pagecount,0) pagecount from #tbl_card_sale_detail_page_cardtype_sum ' exec sp_executesql @sqlpage end else begin insert #tbl_card_sale_detail_page( compid , ddate ,ttime ,billid ,billtype, mode,saleamt,accamt,savedamt,procamt,cardid ,cardtype,acctype, memname ,memphone ,sales , salesid1,salesid1_p ,salesid2 ,salesid2_p ,salesid3 ,salesid3_p,memo ,upgradeflag , oldcardtype, sendacct, sendamt ) select compid , ddate ,ttime ,billid ,billtype, mode,saleamt,accamt,savedamt,procamt,cardid ,cardtype,acctype, memname ,memphone ,sales , salesid1,salesid1_p ,salesid2 ,salesid2_p ,salesid3 ,salesid3_p,memo ,upgradeflag , oldcardtype, sendacct, sendamt from #tbl_card_sale_detail where (@fromacctype = '' or (acctypecode >= @fromacctype and acctypecode <= @toacctype)) and charindex('*', @saletype) > 0 or (charindex('2', @saletype) > 0 and (billtype = 'gz' and mode = 1 and isnull(upgradeflag, 0) = 1)) or (charindex('3', @saletype) > 0 and (billtype = 'gz' and mode = 3)) or (charindex('4', @saletype) > 0 and (billtype = 'gz' and mode = 2)) or (charindex('1', @saletype) > 0 and (mode = 1 and isnull(upgradeflag, 0) != 1)) or (charindex('0', @saletype) > 0 and (mode = 0 and isnull(upgradeflag, 0) != 1)) order by saleamt asc select @saleamt= sum(saleamt) from #tbl_card_sale_detail_page where mode in(0,1,2,3,5) select @accamt= sum(accamt) from #tbl_card_sale_detail_page where mode in(0,1,2,3,5) select @sendamt= sum(sendamt) from #tbl_card_sale_detail_page select @procamt= sum(procamt) from #tbl_card_sale_detail_page where mode in(0,1,2,3,5) select @savedamt= sum(savedamt) from #tbl_card_sale_detail_page where mode in(0,1,2,3,5) select @salecount =count(billid) from #tbl_card_sale_detail_page where mode in(0,1,5) select @pagecount = count(billid) from #tbl_card_sale_detail_page insert #tbl_card_sale_detail_page_sum(saleamt,accamt,sendamt,procamt,savedamt,salecount) select @saleamt ,@accamt,@sendamt,@procamt,@savedamt,@salecount insert #tbl_card_sale_detail_page_cardtype_sum (saleamt,pagecount,cardtype) select sum(saleamt),@pagecount,cardtype from #tbl_card_sale_detail_page group by cardtype update a set a.cardtype = b.gak02c from #tbl_card_sale_detail_page_cardtype_sum a, gam10 b with(nolock) where b.gak01c = a.cardtype and b.gak00c = @incompid update a set a.pagecount = @pagecount from #tbl_card_sale_detail_page a update a set a.pagecount = @pagecount from #tbl_card_sale_detail_page_sum a select @sqlpage = ' select isnull(compid,'''') compid , isnull(ddate,'''') ddate ,isnull(ttime,'''') ttime ,isnull(billid,'''') billid ,isnull(billtype,'''') billtype , isnull(mode,0) mode ,isnull(saleamt,0) saleamt ,isnull(accamt,0) accamt ,isnull(savedamt,0) savedamt ,isnull(procamt,0) procamt ,isnull(cardid,'''') cardid ,isnull(cardtype,'''') cardtype ,isnull(acctype,'''') acctype, isnull(memname,'''') memname ,isnull(memphone,'''') memphone ,isnull(sales,'''') sales ,isnull(salesid1,'''') salesid1 ,isnull(salesid1_p,'''') salesid1_p ,isnull(salesid2,'''') salesid2, isnull(salesid2_p,'''') salesid2_p ,isnull(salesid3,'''') salesid3 ,isnull(salesid3_p,'''') salesid3_p,isnull(memo,'''') memo,isnull(upgradeflag,'''') upgradeflag , isnull(oldcardtype,'''') oldcardtype, isnull(sendacct,0) sendacct, isnull(sendamt,0) sendamt, isnull(pagecount,0) pagecount from #tbl_card_sale_detail_page '+ ' union all '+ ' select '''' compid , '''' ddate ,'''' ttime ,'''' billid ,'''' billtype, isnull(salecount,0) mode,isnull(saleamt,0) saleamt,isnull(accamt,0) accamt,isnull(savedamt,0) savedamt, isnull(procamt,0) procamt, '''' cardid , '''' cardtype, '''' acctype, '''' memname ,'''' memphone ,'''' sales , '''' salesid1, '''' salesid1_p ,'''' salesid2 ,'''' salesid2_p ,'''' salesid3 ,'''' salesid3_p,'''' memo ,'''' upgradeflag , '''' oldcardtype, '''' sendacct, isnull(sendamt,0) sendamt, isnull(pagecount,0) pagecount from #tbl_card_sale_detail_page_sum ' + ' union all '+ ' select ''cardtype'' compid , '''' ddate ,'''' ttime ,'''' billid ,'''' billtype, 0 mode,isnull(saleamt,0) saleamt,0 accamt,0 savedamt, 0 procamt, '''' cardid , isnull(cardtype,'''') cardtype, '''' acctype, '''' memname ,'''' memphone ,'''' sales , '''' salesid1, '''' salesid1_p ,'''' salesid2 ,'''' salesid2_p ,'''' salesid3 ,'''' salesid3_p,'''' memo ,'''' upgradeflag , '''' oldcardtype, '''' sendacct, 0 sendamt, isnull(pagecount,0) pagecount from #tbl_card_sale_detail_page_cardtype_sum ' exec sp_executesql @sqlpage end end else begin if(@pageall = 0) begin insert #tbl_card_sale_detail_page( compid , ddate ,ttime ,billid ,billtype, mode,saleamt,accamt,savedamt,procamt,cardid ,cardtype,acctype, memname ,memphone ,sales , salesid1,salesid1_p ,salesid2 ,salesid2_p ,salesid3 ,salesid3_p,memo ,upgradeflag , oldcardtype, sendacct, sendamt ) select compid , ddate ,ttime ,billid ,billtype, mode,saleamt,accamt,savedamt,procamt,cardid ,cardtype,acctype, memname ,memphone ,sales , salesid1,salesid1_p ,salesid2 ,salesid2_p ,salesid3 ,salesid3_p,memo ,upgradeflag , oldcardtype, sendacct, sendamt from #tbl_card_sale_detail where (@fromacctype = '' or (acctypecode >= @fromacctype and acctypecode <= @toacctype)) and charindex('*', @saletype) > 0 or (charindex('2', @saletype) > 0 and (billtype = 'gz' and mode = 1 and isnull(upgradeflag, 0) = 1)) or (charindex('3', @saletype) > 0 and (billtype = 'gz' and mode = 3)) or (charindex('4', @saletype) > 0 and (billtype = 'gz' and mode = 2)) or (charindex('1', @saletype) > 0 and (mode = 1 and isnull(upgradeflag, 0) != 1)) or (charindex('0', @saletype) > 0 and (mode = 0 and isnull(upgradeflag, 0) != 1)) order by ddate asc, ttime asc, sales desc select @saleamt= sum(saleamt) from #tbl_card_sale_detail_page where mode in(0,1,2,3,5) select @accamt= sum(accamt) from #tbl_card_sale_detail_page where mode in(0,1,2,3,5) select @sendamt= sum(sendamt) from #tbl_card_sale_detail_page select @procamt= sum(procamt) from #tbl_card_sale_detail_page where mode in(0,1,2,3,5) select @savedamt= sum(savedamt) from #tbl_card_sale_detail_page where mode in(0,1,2,3,5) select @salecount =count(billid) from #tbl_card_sale_detail_page where mode in(0,1,5) select @pagecount = count(billid) from #tbl_card_sale_detail_page insert #tbl_card_sale_detail_page_sum(saleamt,accamt,sendamt,procamt,savedamt,salecount) select @saleamt ,@accamt,@sendamt,@procamt,@savedamt,@salecount insert #tbl_card_sale_detail_page_cardtype_sum (saleamt,pagecount,cardtype) select sum(saleamt),@pagecount,cardtype from #tbl_card_sale_detail_page group by cardtype update a set a.cardtype = b.gak02c from #tbl_card_sale_detail_page_cardtype_sum a, gam10 b with(nolock) where b.gak01c = a.cardtype and b.gak00c = @incompid update a set a.pagecount = @pagecount from #tbl_card_sale_detail_page a update a set a.pagecount = @pagecount from #tbl_card_sale_detail_page_sum a select @sqlpage = ' select top '+ cast(3 * @pagesize as nvarchar(20)) +' isnull(compid,'''') compid , isnull(ddate,'''') ddate ,isnull(ttime,'''') ttime ,isnull(billid,'''') billid ,isnull(billtype,'''') billtype , isnull(mode,0) mode ,isnull(saleamt,0) saleamt ,isnull(accamt,0) accamt ,isnull(savedamt,0) savedamt ,isnull(procamt,0) procamt ,isnull(cardid,'''') cardid ,isnull(cardtype,'''') cardtype ,isnull(acctype,'''') acctype, isnull(memname,'''') memname ,isnull(memphone,'''') memphone ,isnull(sales,'''') sales ,isnull(salesid1,'''') salesid1 ,isnull(salesid1_p,'''') salesid1_p ,isnull(salesid2,'''') salesid2, isnull(salesid2_p,'''') salesid2_p ,isnull(salesid3,'''') salesid3 ,isnull(salesid3_p,'''') salesid3_p,isnull(memo,'''') memo,isnull(upgradeflag,'''') upgradeflag , isnull(oldcardtype,'''') oldcardtype, isnull(sendacct,0) sendacct, isnull(sendamt,0) sendamt, isnull(pagecount,0) pagecount from #tbl_card_sale_detail_page where idd > '+cast(@currentpageindex * @pagesize as nvarchar(20))+ ' union all '+ ' select '''' compid , '''' ddate ,'''' ttime ,'''' billid ,'''' billtype, isnull(salecount,0) mode,isnull(saleamt,0) saleamt,isnull(accamt,0) accamt,isnull(savedamt,0) savedamt, isnull(procamt,0) procamt, '''' cardid , '''' cardtype, '''' acctype, '''' memname ,'''' memphone ,'''' sales , '''' salesid1, '''' salesid1_p ,'''' salesid2 ,'''' salesid2_p ,'''' salesid3 ,'''' salesid3_p,'''' memo ,'''' upgradeflag , '''' oldcardtype, '''' sendacct, isnull(sendamt,0) sendamt, isnull(pagecount,0) pagecount from #tbl_card_sale_detail_page_sum ' + ' union all '+ ' select ''cardtype'' compid , '''' ddate ,'''' ttime ,'''' billid ,'''' billtype, 0 mode,isnull(saleamt,0) saleamt,0 accamt,0 savedamt, 0 procamt, '''' cardid , isnull(cardtype,'''') cardtype, '''' acctype, '''' memname ,'''' memphone ,'''' sales , '''' salesid1, '''' salesid1_p ,'''' salesid2 ,'''' salesid2_p ,'''' salesid3 ,'''' salesid3_p,'''' memo ,'''' upgradeflag , '''' oldcardtype, '''' sendacct, 0 sendamt, isnull(pagecount,0) pagecount from #tbl_card_sale_detail_page_cardtype_sum ' exec sp_executesql @sqlpage end else begin insert #tbl_card_sale_detail_page( compid , ddate ,ttime ,billid ,billtype, mode,saleamt,accamt,savedamt,procamt,cardid ,cardtype,acctype, memname ,memphone ,sales , salesid1,salesid1_p ,salesid2 ,salesid2_p ,salesid3 ,salesid3_p,memo ,upgradeflag , oldcardtype, sendacct, sendamt ) select compid , ddate ,ttime ,billid ,billtype, mode,saleamt,accamt,savedamt,procamt,cardid ,cardtype,acctype, memname ,memphone ,sales , salesid1,salesid1_p ,salesid2 ,salesid2_p ,salesid3 ,salesid3_p,memo ,upgradeflag , oldcardtype, sendacct, sendamt from #tbl_card_sale_detail where (@fromacctype = '' or (acctypecode >= @fromacctype and acctypecode <= @toacctype)) and charindex('*', @saletype) > 0 or (charindex('2', @saletype) > 0 and (billtype = 'gz' and mode = 1 and isnull(upgradeflag, 0) = 1)) or (charindex('3', @saletype) > 0 and (billtype = 'gz' and mode = 3)) or (charindex('4', @saletype) > 0 and (billtype = 'gz' and mode = 2)) or (charindex('1', @saletype) > 0 and (mode = 1 and isnull(upgradeflag, 0) != 1)) or (charindex('0', @saletype) > 0 and (mode = 0 and isnull(upgradeflag, 0) != 1)) order by ddate asc, ttime asc, sales desc select @saleamt= sum(saleamt) from #tbl_card_sale_detail_page where mode in(0,1,2,3,5) select @accamt= sum(accamt) from #tbl_card_sale_detail_page where mode in(0,1,2,3,5) select @sendamt= sum(sendamt) from #tbl_card_sale_detail_page select @procamt= sum(procamt) from #tbl_card_sale_detail_page where mode in(0,1,2,3,5) select @savedamt= sum(savedamt) from #tbl_card_sale_detail_page where mode in(0,1,2,3,5) select @salecount =count(billid) from #tbl_card_sale_detail_page where mode in(0,1,5) select @pagecount = count(billid) from #tbl_card_sale_detail_page insert #tbl_card_sale_detail_page_sum(saleamt,accamt,sendamt,procamt,savedamt,salecount) select @saleamt ,@accamt,@sendamt,@procamt,@savedamt,@salecount insert #tbl_card_sale_detail_page_cardtype_sum (saleamt,pagecount,cardtype) select sum(saleamt),@pagecount,cardtype from #tbl_card_sale_detail_page group by cardtype update a set a.cardtype = b.gak02c from #tbl_card_sale_detail_page_cardtype_sum a, gam10 b with(nolock) where b.gak01c = a.cardtype and b.gak00c = @incompid update a set a.pagecount = @pagecount from #tbl_card_sale_detail_page a update a set a.pagecount = @pagecount from #tbl_card_sale_detail_page_sum a select @sqlpage = ' select isnull(compid,'''') compid , isnull(ddate,'''') ddate ,isnull(ttime,'''') ttime ,isnull(billid,'''') billid ,isnull(billtype,'''') billtype , isnull(mode,0) mode ,isnull(saleamt,0) saleamt ,isnull(accamt,0) accamt ,isnull(savedamt,0) savedamt ,isnull(procamt,0) procamt ,isnull(cardid,'''') cardid ,isnull(cardtype,'''') cardtype ,isnull(acctype,'''') acctype, isnull(memname,'''') memname ,isnull(memphone,'''') memphone ,isnull(sales,'''') sales ,isnull(salesid1,'''') salesid1 ,isnull(salesid1_p,'''') salesid1_p ,isnull(salesid2,'''') salesid2, isnull(salesid2_p,'''') salesid2_p ,isnull(salesid3,'''') salesid3 ,isnull(salesid3_p,'''') salesid3_p,isnull(memo,'''') memo,isnull(upgradeflag,'''') upgradeflag , isnull(oldcardtype,'''') oldcardtype, isnull(sendacct,0) sendacct, isnull(sendamt,0) sendamt, isnull(pagecount,0) pagecount from #tbl_card_sale_detail_page '+ ' union all '+ ' select '''' compid , '''' ddate ,'''' ttime ,'''' billid ,'''' billtype, isnull(salecount,0) mode,isnull(saleamt,0) saleamt,isnull(accamt,0) accamt,isnull(savedamt,0) savedamt, isnull(procamt,0) procamt, '''' cardid , '''' cardtype, '''' acctype, '''' memname ,'''' memphone ,'''' sales , '''' salesid1, '''' salesid1_p ,'''' salesid2 ,'''' salesid2_p ,'''' salesid3 ,'''' salesid3_p,'''' memo ,'''' upgradeflag , '''' oldcardtype, '''' sendacct, isnull(sendamt,0) sendamt, isnull(pagecount,0) pagecount from #tbl_card_sale_detail_page_sum ' + ' union all '+ ' select ''cardtype'' compid , '''' ddate ,'''' ttime ,'''' billid ,'''' billtype, 0 mode,isnull(saleamt,0) saleamt,0 accamt,0 savedamt, 0 procamt, '''' cardid , isnull(cardtype,'''') cardtype, '''' acctype, '''' memname ,'''' memphone ,'''' sales , '''' salesid1, '''' salesid1_p ,'''' salesid2 ,'''' salesid2_p ,'''' salesid3 ,'''' salesid3_p,'''' memo ,'''' upgradeflag , '''' oldcardtype, '''' sendacct, 0 sendamt, isnull(pagecount,0) pagecount from #tbl_card_sale_detail_page_cardtype_sum ' exec sp_executesql @sqlpage end end drop table #tbl_card_sale_detail drop table #gnm01_SendAcct drop table #gsm03 drop table #gcm11 drop table #gnm03 drop table #tbl_card_sale_detail_page drop table #tbl_card_sale_detail_page_sum drop table #tbl_card_sale_detail_page_cardtype_sum end GO /****** Object: StoredProcedure [dbo].[upg_expand_net_sub] Script Date: 06/12/2017 06:20:02 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_expand_net_sub] ( @compid varchar(10), -- 公司别 @main_card varchar(20), @lvl int ) as begin if isnull(@main_card, '') = '' return declare @payment_0 varchar(10) declare @payment_3 varchar(10) declare @payment_$ varchar(10) declare @payment_u1 varchar(10) declare @payment_u2 varchar(10) declare @payment_u3 varchar(10) declare @payment_u4 varchar(10) declare @payment_u5 varchar(10) exec upg_isrevenue_payment @compid, @payment_0 output, @payment_3 output, @payment_$ output, @payment_u1 output, @payment_u2 output, @payment_u3 output, @payment_u4 output, @payment_u5 output create table #tbl_related_bills ( compid varchar(20) not null, billid varchar(20) not null, cashamt float null, -- 真实的现金收入 cardid varchar(20) null, -- 所购买的会员卡 cardtype varchar(10) null, -- 介绍会员卡的类别 iscomm int null -- 是否已经提佣 ) create table #tbl_part_amt ( compid varchar(20) not null, billid varchar(20) not null, cashamt float null -- 真实的现金收入 ) declare @cardtype varchar(20) create table #unhandle_card ( card varchar(20) ) create table #unhandle_card_tmp ( card varchar(20) ) insert into #unhandle_card(card) values(@main_card) declare @current_card varchar(20) -- 创建循环变量 declare @HasRecord int set @HasRecord = 1 -- 创建循环变量 declare @HasSubRecord int set @HasSubRecord = 1 while(@HasRecord = 1) begin set @HasSubRecord = 1 while(@HasSubRecord = 1) begin if exists(select 1 from #unhandle_card) begin set @HasSubRecord = 1 select top 1 @current_card = card from #unhandle_card select @cardtype = gca02c from gcm01 with (nolock) where gca00c = gca13d and gca01c = @current_card delete #tbl_related_bills insert #tbl_related_bills(compid, billid, cardid, iscomm, cardtype) select gna00c, gna01c, gna13c, gnb09i, @cardtype from gnm01 a with (nolock), gnm02 b with (nolock) where a.gna00c = b.gnb00c and a.gna01c = b.gnb01c and a.gna13c = b.gnb03c and a.gna09c = @current_card delete #tbl_part_amt insert #tbl_part_amt(compid, billid, cashamt) select gnb00c, gnb01c, sum(case when gnb15c in ('1', '6', 'ZFB', 'WX', @payment_0, @payment_3, @payment_$, @payment_u1, @payment_u2, @payment_u3, @payment_u4, @payment_u5) then gnb16f else 0 end + case when gnb17c in ('1', '6', 'ZFB', 'WX', @payment_0, @payment_3, @payment_$, @payment_u1, @payment_u2, @payment_u3, @payment_u4, @payment_u5) then gnb18f else 0 end + case when gnb19c in ('1', '6', 'ZFB', 'WX', @payment_0, @payment_3, @payment_$, @payment_u1, @payment_u2, @payment_u3, @payment_u4, @payment_u5) then gnb20f else 0 end + case when gnb21c in ('1', '6', 'ZFB', 'WX', @payment_0, @payment_3, @payment_$, @payment_u1, @payment_u2, @payment_u3, @payment_u4, @payment_u5) then gnb22f else 0 end) from gnm02 a with (nolock), #tbl_related_bills b where a.gnb00c = b.compid and a.gnb01c = b.billid group by gnb00c, gnb01c update a set a.cashamt = isnull(b.cashamt, 0) from #tbl_related_bills a, #tbl_part_amt b where a.compid = b.compid and a.billid = b.billid -- 疗程的现金收入 delete #tbl_part_amt insert #tbl_part_amt(compid, billid, cashamt) select gnc00c, gnc01c, sum(case when gnc15c in ('1', '6', 'ZFB', 'WX', @payment_0, @payment_3, @payment_$, @payment_u1, @payment_u2, @payment_u3, @payment_u4, @payment_u5) then gnc16f else 0 end + case when gnc17c in ('1', '6', 'ZFB', 'WX', @payment_0, @payment_3, @payment_$, @payment_u1, @payment_u2, @payment_u3, @payment_u4, @payment_u5) then gnc18f else 0 end + case when gnc19c in ('1', '6', 'ZFB', 'WX', @payment_0, @payment_3, @payment_$, @payment_u1, @payment_u2, @payment_u3, @payment_u4, @payment_u5) then gnc20f else 0 end + case when gnc23c in ('1', '6', 'ZFB', 'WX', @payment_0, @payment_3, @payment_$, @payment_u1, @payment_u2, @payment_u3, @payment_u4, @payment_u5) then gnc24f else 0 end) from gnm03 a with (nolock), #tbl_related_bills b where a.gnc00c = b.compid and a.gnc01c = b.billid group by gnc00c, gnc01c update a set a.cashamt = isnull(a.cashamt, 0) + isnull(b.cashamt, 0) from #tbl_related_bills a, #tbl_part_amt b where a.compid = b.compid and a.billid = b.billid -- 套餐的现金收入 delete #tbl_part_amt insert #tbl_part_amt(compid, billid, cashamt) select gtf00c, gtf01c, sum(case when gtf09c in ('1', '6', 'ZFB', 'WX', @payment_0, @payment_3, @payment_$, @payment_u1, @payment_u2, @payment_u3, @payment_u4, @payment_u5) then gtf10f else 0 end + case when gtf11c in ('1', '6', 'ZFB', 'WX', @payment_0, @payment_3, @payment_$, @payment_u1, @payment_u2, @payment_u3, @payment_u4, @payment_u5) then gtf12f else 0 end + case when gtf13c in ('1', '6', 'ZFB', 'WX', @payment_0, @payment_3, @payment_$, @payment_u1, @payment_u2, @payment_u3, @payment_u4, @payment_u5) then gtf14f else 0 end + case when gtf15c in ('1', '6', 'ZFB', 'WX', @payment_0, @payment_3, @payment_$, @payment_u1, @payment_u2, @payment_u3, @payment_u4, @payment_u5) then gtf16f else 0 end) from gtc05 a, #tbl_related_bills b where a.gtf00c = b.compid and a.gtf01c = b.billid group by gtf00c, gtf01c update a set a.cashamt = isnull(a.cashamt, 0) + isnull(b.cashamt, 0) from #tbl_related_bills a, #tbl_part_amt b where a.compid = b.compid and a.billid = b.billid -- 将这个卡放入临时表中 insert #intro_net(lvl, src_card, intro_card, card_amt, card_type, is_saved, billid) select @lvl, @current_card, cardid, cashamt, cardtype, isnull(iscomm, 0), billid from #tbl_related_bills insert into #unhandle_card_tmp(card) select cardid from #tbl_related_bills -- 清除已处理的门店 delete #unhandle_card where card = @current_card end else begin set @HasSubRecord = 0 end end select @lvl = @lvl + 1 insert into #unhandle_card(card) select card from #unhandle_card_tmp delete from #unhandle_card_tmp if exists(select 1 from #unhandle_card) set if exists(select 1 from #unhandle_card) set @HasRecord = 1 else set @HasRecord = 0 end drop table #tbl_part_amt drop table #tbl_related_bills end GO /****** Object: StoredProcedure [dbo].[upg_expand_net_gcm10] Script Date: 06/12/2017 06:20:02 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_expand_net_gcm10] ( @compid varchar(10), -- 公司别 @main_card varchar(20), @lvl int, @billNo varchar(30) --单据号 ) as begin if isnull(@main_card, '') = '' return declare @payment_0 varchar(10) declare @payment_3 varchar(10) declare @payment_$ varchar(10) declare @payment_u1 varchar(10) declare @payment_u2 varchar(10) declare @payment_u3 varchar(10) declare @payment_u4 varchar(10) declare @payment_u5 varchar(10) exec upg_isrevenue_payment @compid, @payment_0 output, @payment_3 output, @payment_$ output, @payment_u1 output, @payment_u2 output, @payment_u3 output, @payment_u4 output, @payment_u5 output create table #tbl_related_bills --会员卡销售 ( compid varchar(20) not null, billid varchar(20) not null, cashamt float null, -- 真实的现金收入 cardid varchar(20) null, -- 所购买的会员卡 cardtype varchar(10) null, -- 介绍会员卡的类别 iscomm int null -- 是否已经提佣 ) create table #tbl_part_amt ( compid varchar(20) not null, billid varchar(20) not null, cashamt float null -- 真实的现金收入 ) declare @cardtype varchar(20) create table #unhandle_card ( card varchar(20) ) create table #unhandle_card_tmp ( card varchar(20) ) insert into #unhandle_card(card) values(@main_card) declare @current_card varchar(20) -- 创建循环变量 declare @HasRecord int set @HasRecord = 1 -- 创建循环变量 declare @HasSubRecord int set @HasSubRecord = 1 while(@HasRecord = 1) begin set @HasSubRecord = 1 while(@HasSubRecord = 1) begin if exists(select 1 from #unhandle_card) begin set @HasSubRecord = 1 select top 1 @current_card = card from #unhandle_card select @cardtype = gca02c from gcm01 with (nolock) where gca00c = gca13d and gca01c = @current_card delete #tbl_related_bills insert #tbl_related_bills(compid, billid, cardid, iscomm, cardtype) select gcl00c,gcl01c,gcl03c,isnull(gcl111i,0),@cardtype from gcm10 inner join gcm01 on gcl03c = gca04c where gcl00c = @compid and gca31c = @current_card --介绍人 and gcl01c = @billNo and gcl06i = 0 delete #tbl_part_amt insert #tbl_part_amt(compid, billid, cashamt) select gcl00c, gcl01c, sum(case when gcl67c in ('1', '6', 'ZFB', 'WX', @payment_0, @payment_3, @payment_$, @payment_u1, @payment_u2, @payment_u3, @payment_u4, @payment_u5) then gcl68f else 0 end + case when gcl69c in ('1', '6', 'ZFB', 'WX', @payment_0, @payment_3, @payment_$, @payment_u1, @payment_u2, @payment_u3, @payment_u4, @payment_u5) then gcl70f else 0 end + case when gcl71c in ('1', '6', 'ZFB', 'WX', @payment_0, @payment_3, @payment_$, @payment_u1, @payment_u2, @payment_u3, @payment_u4, @payment_u5) then gcl72f else 0 end + case when gcl73c in ('1', '6', 'ZFB', 'WX', @payment_0, @payment_3, @payment_$, @payment_u1, @payment_u2, @payment_u3, @payment_u4, @payment_u5) then gcl74f else 0 end) from gcm10 a with (nolock), #tbl_related_bills b where a.gcl00c = b.compid and a.gcl01c = b.billid group by gcl00c,gcl01c update a set a.cashamt = isnull(b.cashamt, 0) from #tbl_related_bills a, #tbl_part_amt b where a.compid = b.compid and a.billid = b.billid -- 疗程的现金收入 delete #tbl_part_amt insert #tbl_part_amt(compid, billid, cashamt) select gcm00c, gcm01c, sum( case when gcm15c in ('1', '6', 'ZFB', 'WX', @payment_0, @payment_3, @payment_$, @payment_u1, @payment_u2, @payment_u3, @payment_u4, @payment_u5) then gcm16f else 0 end + case when gcm17c in ('1', '6', 'ZFB', 'WX', @payment_0, @payment_3, @payment_$, @payment_u1, @payment_u2, @payment_u3, @payment_u4, @payment_u5) then gcm18f else 0 end + case when gcm19c in ('1', '6', 'ZFB', 'WX', @payment_0, @payment_3, @payment_$, @payment_u1, @payment_u2, @payment_u3, @payment_u4, @payment_u5) then gcm20f else 0 end + case when gcm23c in ('1', '6', 'ZFB', 'WX', @payment_0, @payment_3, @payment_$, @payment_u1, @payment_u2, @payment_u3, @payment_u4, @payment_u5) then gcm24f else 0 end) from gcm11 a with (nolock), #tbl_related_bills b where a.gcm00c = b.compid and a.gcm01c = b.billid group by gcm00c,gcm01c update a set a.cashamt = isnull(a.cashamt, 0) + isnull(b.cashamt, 0) from #tbl_related_bills a, #tbl_part_amt b where a.compid = b.compid and a.billid = b.billid -- 套餐的现金收入 delete #tbl_part_amt insert #tbl_part_amt(compid, billid, cashamt) select gth00c, gth01c, sum( case when gth09c in ('1', '6', 'ZFB', 'WX', @payment_0, @payment_3, @payment_$, @payment_u1, @payment_u2, @payment_u3, @payment_u4, @payment_u5) then gth10f else 0 end + case when gth11c in ('1', '6', 'ZFB', 'WX', @payment_0, @payment_3, @payment_$, @payment_u1, @payment_u2, @payment_u3, @payment_u4, @payment_u5) then gth12f else 0 end + case when gth13c in ('1', '6', 'ZFB', 'WX', @payment_0, @payment_3, @payment_$, @payment_u1, @payment_u2, @payment_u3, @payment_u4, @payment_u5) then gth14f else 0 end + case when gth15c in ('1', '6', 'ZFB', 'WX', @payment_0, @payment_3, @payment_$, @payment_u1, @payment_u2, @payment_u3, @payment_u4, @payment_u5) then gth16f else 0 end) from gtc06 a, #tbl_related_bills b where a.gth00c = b.compid and a.gth01c = b.billid group by gth00c,gth01c update a set a.cashamt = isnull(a.cashamt, 0) + isnull(b.cashamt, 0) from #tbl_related_bills a, #tbl_part_amt b where a.compid = b.compid and a.billid = b.billid -- 将这个卡放入临时表中 insert #intro_net(lvl, src_card, intro_card, card_amt, card_type, is_saved, billid) select @lvl, @current_card, cardid, cashamt, cardtype, isnull(iscomm, 0), billid from #tbl_related_bills insert into #unhandle_card_tmp(card) select cardid from #tbl_related_bills -- 清除已处理的门店 delete #unhandle_card where card = @current_card end else begin set @HasSubRecord = 0 end end select @lvl = @lvl + 1 insert into #unhandle_card(card) select card from #unhandle_card_tmp delete from #unhandle_card_tmp if exists(select 1 from #unhandle_card) set if exists(select 1 from #unhandle_card) set @HasRecord = 1 else set @HasRecord = 0 end drop table #tbl_part_amt drop table #tbl_related_bills end GO /****** Object: StoredProcedure [dbo].[upg_expand_net_ggm01] Script Date: 06/12/2017 06:20:02 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_expand_net_ggm01] ( @compid varchar(10), -- 公司别 @main_card varchar(20), @lvl int , @billNo varchar(30) --单据号 ) as begin if isnull(@main_card, '') = '' return declare @payment_0 varchar(10) declare @payment_3 varchar(10) declare @payment_$ varchar(10) declare @payment_u1 varchar(10) declare @payment_u2 varchar(10) declare @payment_u3 varchar(10) declare @payment_u4 varchar(10) declare @payment_u5 varchar(10) exec upg_isrevenue_payment @compid, @payment_0 output, @payment_3 output, @payment_$ output, @payment_u1 output, @payment_u2 output, @payment_u3 output, @payment_u4 output, @payment_u5 output create table #tbl_related_bills ( compid varchar(20) not null, billid varchar(20) not null, cashamt float null, -- 真实的现金收入 cardid varchar(20) null, -- 所购买的会员卡 cardtype varchar(10) null, -- 介绍会员卡的类别 iscomm int null -- 是否已经提佣 ) create table #tbl_part_amt ( compid varchar(20) not null, billid varchar(20) not null, cashamt float null -- 真实的现金收入 ) declare @cardtype varchar(20) create table #unhandle_card ( card varchar(20) ) create table #unhandle_card_tmp ( card varchar(20) ) insert into #unhandle_card(card) values(@main_card) declare @current_card varchar(20) -- 创建循环变量 declare @HasRecord int set @HasRecord = 1 -- 创建循环变量 declare @HasSubRecord int set @HasSubRecord = 1 while(@HasRecord = 1) begin set @HasSubRecord = 1 while(@HasSubRecord = 1) begin if exists(select 1 from #unhandle_card) begin set @HasSubRecord = 1 select top 1 @current_card = card from #unhandle_card select @cardtype = gca02c from gcm01 with (nolock) where gca00c = gca13d and gca01c = @current_card delete #tbl_related_bills insert #tbl_related_bills(compid, billid, cardid, iscomm, cardtype) select gga00c,gga01c,gga09c,isnull(gga83i,0),@cardtype from ggm01 inner join gcm01 on gga09c = gca04c where gga00c = @compid and gga01c = @billNo and gca31c = @current_card --介绍人 delete #tbl_part_amt insert #tbl_part_amt(compid, billid, cashamt) select gsc00c, gsc01c, sum(case when gsc04c in ('1', '6', 'ZFB', 'WX', @payment_0, @payment_3, @payment_$, @payment_u1, @payment_u2, @payment_u3, @payment_u4, @payment_u5) then gsc05f else 0 end) from gsm03 a with (nolock), #tbl_related_bills b where a.gsc00c = b.compid and a.gsc01c = b.billid and a.gsc02c = 'gx' group by gsc00c,gsc01c update a set a.cashamt = isnull(b.cashamt, 0) from #tbl_related_bills a, #tbl_part_amt b where a.compid = b.compid and a.billid = b.billid -- 将这个卡放入临时表中 insert #intro_net(lvl, src_card, intro_card, card_amt, card_type, is_saved, billid) select @lvl, @current_card, cardid, cashamt, cardtype, isnull(iscomm, 0), billid from #tbl_related_bills insert into #unhandle_card_tmp(card) select cardid from #tbl_related_bills -- 清除已处理的门店 delete #unhandle_card where card = @current_card end else begin set @HasSubRecord = 0 end end select @lvl = @lvl + 1 insert into #unhandle_card(card) select card from #unhandle_card_tmp delete from #unhandle_card_tmp if exists(select 1 from #unhandle_card) set if exists(select 1 from #unhandle_card) set @HasRecord = 1 else set @HasRecord = 0 end drop table #tbl_part_amt drop table #tbl_related_bills end GO /****** Object: StoredProcedure [dbo].[upg_cal_operation_result_month] Script Date: 06/12/2017 06:19:33 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_cal_operation_result_month] ( @compid varchar(10), -- 公司别 @fromdate varchar(10), -- 开始日期 @todate varchar(10) -- 截至日期 ) as begin declare @t_cnt float -- 总客数 declare @t_amt float -- 总业绩 declare @t_f_cnt float -- 指定数量 declare @t_f_amt float -- 指定业绩 declare @t_n_cnt float -- 轮班数量 declare @t_n_amt float -- 轮班业绩 declare @t_old_cnt float -- 老客数量 declare @t_old_amt float -- 老客业绩 declare @t_new_cnt float -- 新客数量 declare @t_new_amt float -- 新客业绩 declare @srv_total_amt float -- 服务项目 declare @srv_total_rawamt float declare @goodsell_cnt float -- 产品销售数量 declare @goodsell_amt float -- 产品销售金额 2013年7月11日11:17:45 declare @goods_total_cnt float -- 产品销售数量 declare @goods_total_amt float declare @goods_total_rawamt float declare @card_total_cnt float declare @card_total_amt float declare @card_total_tamt float declare @card_total_rawamt float declare @srv_mei_total_amt float declare @srv_mei_total_rawamt float declare @srv_other_amt float declare @srv_other_rawamt float declare @card_use_amt float declare @cash_use_amt float declare @credit_use_amt float declare @male_count float declare @female_count float declare @male_cmt float -- 男客业绩 declare @female_cmt float -- 女客业绩 declare @tmp_cnt int declare @tmp_amt float declare @tmp_rawamt float declare @payment_0 varchar(10) declare @payment_3 varchar(10) declare @payment_$ varchar(10) declare @payment_u1 varchar(10) declare @payment_u2 varchar(10) declare @payment_u3 varchar(10) declare @payment_u4 varchar(10) declare @payment_u5 varchar(10) exec upg_isrevenue_payment @compid, @payment_0 output, @payment_3 output, @payment_$ output, @payment_u1 output, @payment_u2 output, @payment_u3 output, @payment_u4 output, @payment_u5 output create table #st_results ( idd int identity not null, type varchar(10) not null, -- 类型 cnt float null, -- 人数 amt float null, -- 折扣后金额 rawamt float null, -- 原价 price float null, -- 折扣后单价 amt_t float null, -- 轮班业绩 amt_f float null, -- 指定业绩 cnt_t float null, -- 轮班数量 cnt_f float null, -- 指定数量 primary key(idd) ) -- 存放所有需要统计的单据 -- 收银单,卡销售单,充值单 create table #st_bills ( idd int identity not null, compid varchar(10) not null, bill_type varchar(10) not null, -- 单据类别 billid varchar(20) not null, -- 单号 srv_type varchar(10) null, -- 点工/轮班 gender int null, -- 性别 ) declare @PK_Sql nvarchar(max) set @PK_Sql = ' alter table #st_bills add constraint PK_#st_bills_' + convert(varchar(10), cast(floor(rand() * 1000000) as int)) + ' primary key nonclustered(idd)' exec sp_executesql @PK_Sql create clustered index pk_st_bills_01 on #st_bills(compid, bill_type, billid) -- 指定的单据 create table #st_bills_fix ( idd int identity not null, compid varchar(10) not null, -- 公司别 billid varchar(20) not null, -- 单号 ) set @PK_Sql = ' alter table #st_bills_fix add constraint PK_#st_bills_fix_' + convert(varchar(10), cast(floor(rand() * 1000000) as int)) + ' primary key nonclustered(idd)' exec sp_executesql @PK_Sql create clustered index idx_st_bills_fix_01 on #st_bills_fix(compid, billid) -- 不指定的单据 create table #st_bills_nofix ( idd int identity not null, compid varchar(10) not null, billid varchar(20) not null, -- 单号 ) set @PK_Sql = ' alter table #st_bills_nofix add constraint PK_#st_bills_nofix_' + convert(varchar(10), cast(floor(rand() * 1000000) as int)) + ' primary key nonclustered(idd)' exec sp_executesql @PK_Sql create clustered index idx_st_bills_nofix_01 on #st_bills_nofix(compid, billid) -- 所有这段时间来得客户 insert #st_bills(compid, bill_type, billid, gender) select gga00c, 'gx', gga01c, isnull(gga31i, 0) from ggm01 with(nolock), gam26 z with(nolock) where gga00c = z.gaz02c and z.gaz01c = @compid and gga80d >= @fromdate and gga80d <= @todate select @female_count = count(*) from #st_bills where gender = 0 and bill_type = 'gx' select @male_count = count(*) from #st_bills where gender = 1 and bill_type = 'gx' -- 卡充值单,卡销售单 insert #st_bills(compid, bill_type, billid) select gna00c, 'gn', gna01c from gnm01 a with (nolock), gam26 z with(nolock) where a.gna00c = z.gaz02c and z.gaz01c = @compid and a.gna80d >= @fromdate and a.gna80d <= @todate insert #st_bills(compid, bill_type, billid) select gcl00c, 'gz', gcl01c from gcm10 with (nolock), gam26 z with(nolock) where gcl00c = z.gaz02c and z.gaz01c = @compid and gcl80d >= @fromdate and gcl80d <= @todate -- 产品外卖数量, 金额 select @goodsell_cnt = sum(ggc05f), @goodsell_amt = sum(ggc11f) from ggm03 a with (nolock), #st_bills b with (nolock), gam26 z with(nolock) where a.ggc00c = b.compid and a.ggc01c = b.billid and b.bill_type = 'gx' and z.gaz01c = @compid and a.ggc00c = z.gaz02c -- 指定的总单据 insert #st_bills_fix(compid, billid) select distinct ggb00c, ggb01c from ggm02 b with (nolock), ggm01 a with (nolock), gam26 z with(nolock) where a.gga00c = b.ggb00c and a.gga01c = b.ggb01c and a.gga00c = z.gaz02c and z.gaz01c = @compid and a.gga80d >= @fromdate and a.gga80d <= @todate and isnull(b.ggb15c, '') = '1' update a set a.srv_type = '1' from #st_bills a, #st_bills_fix b where a.billid = b.billid and a.bill_type = 'gx' and a.compid = b.compid update a set a.srv_type = '2' from #st_bills a where isnull(srv_type, '') = '' and a.bill_type = 'gx' -- 轮班总的单据 insert #st_bills_nofix(compid, billid) select compid, billid from #st_bills a where isnull(srv_type, '') = '2' and bill_type = 'gx' -- 指定总客数 select @t_f_cnt = count(*) from #st_bills where srv_type = '1' -- 指定客业绩 select @t_f_amt = sum(a.ggb11f) from ggm02 a with (nolock), #st_bills_fix b, gam26 z with(nolock) where a.ggb01c = b.billid and a.ggb00c = z.gaz02c and z.gaz01c = @compid and a.ggb00c = b.compid -- 不定客数 select @t_n_cnt = count(*) from #st_bills where srv_type = '2' -- 不定客业绩 select @t_n_amt = sum(a.ggb11f) from ggm02 a with (nolock), #st_bills_nofix b, gam26 z with(nolock) where a.ggb01c = b.billid and a.ggb00c = z.gaz02c and z.gaz01c = @compid and a.ggb00c = b.compid -- 合计总的客数与总的业绩 select @t_cnt = isnull(@t_f_cnt, 0) + isnull(@t_n_cnt, 0), @t_amt = isnull(@t_f_amt, 0) + isnull(@t_n_amt, 0) -- 统计老或新客户的数量及业绩 -- 老客或新客的数量 select @t_old_cnt = sum(isnull(gga14f, 1)) from ggm01 a with (nolock), gam26 z with(nolock), #st_bills b where a.gga00c = z.gaz02c and z.gaz01c = @compid and isnull(a.gga13f, 0) = 1 and a.gga01c = b.billid and b.bill_type = 'gx' and a.gga00c = b.compid select @t_new_cnt = sum(isnull(gga14f, 1)) from ggm01 a with (nolock), gam26 z with(nolock), #st_bills b where a.gga00c = z.gaz02c and z.gaz01c = @compid and isnull(a.gga13f, 0) = 0 and a.gga01c = b.billid and a.gga00c = b.compid and b.bill_type = 'gx' -- 老客或新客的业绩 select @t_old_amt = sum(isnull(ggb11f, 1)) from ggm02 b with (nolock), ggm01 a with (nolock), gam26 z with(nolock), #st_bills c where b.ggb00c = a.gga00c and b.ggb01c = a.gga01c and a.gga00c = z.gaz02c and z.gaz01c = @compid and isnull(a.gga13f, 0) = 1 and a.gga01c = c.billid and c.bill_type = 'gx' and a.gga00c = c.compid select @t_new_amt = sum(isnull(ggb11f, 1)) from ggm02 b with (nolock), ggm01 a with (nolock), gam26 z with(nolock), #st_bills c where b.ggb00c = a.gga00c and b.ggb01c = a.gga01c and a.gga00c = z.gaz02c and z.gaz01c = @compid and isnull(a.gga13f, 0) = 0 and a.gga01c = c.billid and c.bill_type = 'gx' and a.gga00c = c.compid -- 男客女客的业绩 select @male_cmt = sum(isnull(ggb11f, 1)) from ggm02 b with (nolock), ggm01 a with (nolock), gam26 z with(nolock), #st_bills c where b.ggb00c = a.gga00c and b.ggb01c = a.gga01c and a.gga00c = z.gaz02c and z.gaz01c = @compid and a.gga01c = c.billid and c.bill_type = 'gx' and a.gga00c = c.compid and c.gender =1 select @female_cmt = sum(isnull(ggb11f,1)) from ggm02 b with (nolock), ggm01 a with (nolock), gam26 z with(nolock), #st_bills c where b.ggb00c = a.gga00c and b.ggb01c = a.gga01c and a.gga00c = z.gaz02c and z.gaz01c = @compid and a.gga01c = c.billid and c.bill_type = 'gx' and a.gga00c = c.compid and c.gender =0 -- 总的服务金额 select @srv_total_amt = sum(ggb11f), @srv_total_rawamt = sum(ggb08f * isnull(ggb05f, 1)) from ggm02 b with (nolock), #st_bills a, gam26 z with(nolock) where b.ggb00c = z.gaz02c and z.gaz01c = @compid and b.ggb01c = a.billid and a.bill_type = 'gx' and b.ggb00c = a.compid -- 产品销售金额 select @goods_total_cnt = sum(ggc05f), @goods_total_amt = sum(ggc11f), @goods_total_rawamt = sum(ggc08f * isnull(ggc05f, 1)) from ggm03 c with (nolock), #st_bills a, gam26 z with(nolock) where c.ggc00c = z.gaz02c and z.gaz01c = @compid and c.ggc01c = a.billid and a.bill_type = 'gx' and c.ggc00c = a.compid -- 不同统计类别项目的标准金额 insert #st_results(type, cnt, amt, rawamt) select gda13c, cnt = sum(ggb05f), amt = sum(ggb11f), rawamt = sum(ggb08f * ggb05f) from ggm02 a with (nolock), gdm01 b with(nolock), ggm01 c with (nolock), gam26 z with(nolock) where a.ggb00c = b.gda00c and a.ggb03c = b.gda01c and a.ggb00c = z.gaz02c and z.gaz01c = @compid and a.ggb00c = c.gga00c and a.ggb01c = c.gga01c and c.gga80d >= @fromdate and c.gga80d <= @todate and isnull(gda13c, '') <> '' group by gda13c -- 分别统计指定以及轮班的业绩 create table #tbl_turn_fix_amt ( type varchar(20) not null, cnt_t float null, cnt_f float null, amt_t float null, amt_f float null ) -- 轮班的业绩 insert #tbl_turn_fix_amt(type, cnt_t, amt_t) select gda13c, cnt = sum(ggb05f), amt = sum(ggb11f) from ggm02 a with (nolock), gdm01 b with(nolock), ggm01 c with (nolock), gam26 z with(nolock) where a.ggb00c = b.gda00c and a.ggb03c = b.gda01c and a.ggb00c = z.gaz02c and z.gaz01c = @compid and a.ggb00c = c.gga00c and a.ggb01c = c.gga01c and c.gga80d >= @fromdate and c.gga80d <= @todate and isnull(gda13c, '') <> '' and ggb15c = '2' group by gda13c update a set a.cnt_t = b.cnt_t, a.amt_t = b.amt_t from #st_results a, #tbl_turn_fix_amt b where a.type = b.type -- 统计指定的业绩 delete #tbl_turn_fix_amt insert #tbl_turn_fix_amt(type, cnt_f, amt_f) select gda13c, cnt = sum(ggb05f), amt = sum(ggb11f) from ggm02 a with (nolock), gdm01 b with(nolock), ggm01 c with (nolock), gam26 z with(nolock) where a.ggb00c = b.gda00c and a.ggb03c = b.gda01c and a.ggb00c = z.gaz02c and z.gaz01c = @compid and a.ggb00c = c.gga00c and a.ggb01c = c.gga01c and c.gga80d >= @fromdate and c.gga80d <= @todate and isnull(gda13c, '') <> '' and ggb15c = '1' group by gda13c update a set a.cnt_f = b.cnt_f, a.amt_f = b.amt_f from #st_results a, #tbl_turn_fix_amt b where a.type = b.type drop table #tbl_turn_fix_amt -- 美发项目的总金额, 折扣后 select @srv_mei_total_amt = sum(amt), @srv_mei_total_rawamt = sum(rawamt) from #st_results where type >= '1' and type <= '9' select @srv_total_amt = isnull(@srv_total_amt, 0), @srv_mei_total_amt = isnull(@srv_mei_total_amt, 0), @srv_total_rawamt = isnull(@srv_total_rawamt, 0), @srv_mei_total_rawamt = isnull(@srv_mei_total_rawamt, 0) -- 其他金额 = 没有分类的项目 + 产品销售 select @srv_other_amt = @srv_total_amt - @srv_mei_total_amt + @goods_total_amt, @srv_other_rawamt = @srv_total_rawamt - @srv_mei_total_rawamt + @goods_total_rawamt -- 卡销售, 不考虑欠款的情况 select @card_total_cnt = count(*), @card_total_amt = sum(gnb08f), @card_total_rawamt = sum(gnb08f) from gnm02 b with (nolock), #st_bills a, gam26 z with(nolock) where a.billid = b.gnb01c and b.gnb00c = z.gaz02c and z.gaz01c = @compid and a.bill_type = 'gn' and b.gnb00c = a.compid -- 加上充值金额 select @tmp_cnt = count(*), @tmp_amt = sum(gcl07f), @tmp_rawamt = sum(gcl07f) from gcm10 b with (nolock), #st_bills a, gam26 z with(nolock) where a.billid = b.gcl01c and b.gcl00c = z.gaz02c and z.gaz01c = @compid and a.bill_type = 'gz' and b.gcl06i = 0 and b.gcl00c = a.compid select @card_total_cnt = isnull(@card_total_cnt, 0) + isnull(@tmp_cnt, 0), @card_total_amt = isnull(@card_total_amt, 0) + isnull(@tmp_amt, 0), @card_total_rawamt = isnull(@card_total_rawamt, 0) + isnull(@tmp_rawamt, 0) -- 还款, 累计金额, 但不累计次数 select @tmp_amt = sum(gcl07f), @tmp_rawamt = sum(gcl07f) from gcm10 b with (nolock), #st_bills a, gam26 z with(nolock) where a.billid = b.gcl01c and b.gcl00c = z.gaz02c and z.gaz01c = @compid and a.bill_type = 'gz' and b.gcl06i = 6 and b.gcl00c = a.compid select @card_total_amt = isnull(@card_total_amt, 0) + isnull(@tmp_amt, 0), @card_total_rawamt = isnull(@card_total_rawamt, 0) + isnull(@tmp_rawamt, 0) -- 卡中取款,以及欠款 select @tmp_amt = sum(gcl07f), @tmp_rawamt = sum(gcl07f) from gcm10 b with (nolock), #st_bills a, gam26 z with(nolock) where a.billid = b.gcl01c and b.gcl00c = z.gaz02c and z.gaz01c = @compid and a.bill_type = 'gz' and (b.gcl06i = 1 or b.gcl06i = 5) and b.gcl00c = a.compid select @card_total_amt = isnull(@card_total_amt, 0) - isnull(@tmp_amt, 0), @card_total_rawamt = isnull(@card_total_rawamt, 0) - isnull(@tmp_rawamt, 0) -- 卡销售真的收入为实际现金 + 银行卡 -- 上面的计算@card_total_amt为不正确的 select @card_total_tamt = sum(gsc05f) from gsm03 a with (nolock), #st_bills b, gam26 z with(nolock) where a.gsc00c = z.gaz02c and z.gaz01c = @compid and a.gsc01c = b.billid and (a.gsc02c = 'gn' or a.gsc02c = 'gz') and a.gsc04c in ('1', '6', 'ZFB', 'WX', @payment_0, @payment_3, @payment_$, @payment_u1, @payment_u2, @payment_u3, @payment_u4, @payment_u5) and a.gsc02c = b.bill_type and a.gsc00c = b.compid -- 用卡 select @card_use_amt = sum(gsc05f) from gsm03 a with (nolock), #st_bills b, gsm14 c with(nolock), gam26 z with(nolock) where a.gsc00c = z.gaz02c and z.gaz01c = @compid and a.gsc01c = b.billid and (a.gsc02c = 'gx' or a.gsc02c = 'gz') and b.bill_type = a.gsc02c and a.gsc04c = c.gsn01c and isnull(c.gsn02c, '') <> '' and isnull(c.gsn02c, '') <> '999' and a.gsc00c = b.compid -- 现金 select @cash_use_amt = sum(gsc05f) from gsm03 a with (nolock), #st_bills b, gam26 z with(nolock) where a.gsc00c = z.gaz02c and z.gaz01c = @compid and a.gsc01c = b.billid and (a.gsc04c = '1') and a.gsc02c = b.bill_type and a.gsc00c = b.compid -- 银行卡 select @credit_use_amt = sum(gsc05f) from gsm03 a with (nolock), #st_bills b, gam26 z with(nolock) where a.gsc00c = z.gaz02c and z.gaz01c = @compid and a.gsc01c = b.billid and (a.gsc04c = '6') and a.gsc02c = b.bill_type and a.gsc00c = b.compid -- 销售卡(折扣前) insert #st_results(type, cnt, amt) values ('A-00', @card_total_cnt, @card_total_rawamt) -- 其他 insert #st_results(type, cnt, amt) values ('A-01', 0, @srv_other_rawamt) -- 折扣 = 服务原价 + 卡原价 + 产品原价 - () select @srv_total_rawamt = isnull(@srv_total_rawamt, 0), @goods_total_rawamt = isnull(@goods_total_rawamt, 0), @card_total_rawamt = isnull(@card_total_rawamt, 0), @srv_total_amt = isnull(@srv_total_amt, 0), @goods_total_amt = isnull(@goods_total_amt, 0), @card_total_amt = isnull(@card_total_amt, 0) -- 折扣还应该包括 抵用券,经理签单等支付方式 declare @pay_discount_amt float select @pay_discount_amt = 0 select @pay_discount_amt = sum(gsc05f) from gsm03 a with (nolock), #st_bills b, gam26 z with(nolock) where a.gsc00c = z.gaz02c and z.gaz01c = @compid and a.gsc01c = b.billid and (a.gsc04c = '3' or a.gsc04c = '7' or a.gsc04c = '8') and a.gsc02c = b.bill_type and a.gsc00c = b.compid insert #st_results(type, cnt, amt) values('支付折扣', 0, @pay_discount_amt) insert #st_results(type, cnt, amt) values('共标准价', 0, @srv_total_rawamt + @goods_total_rawamt + @card_total_rawamt) insert #st_results(type, cnt, amt) values('共折扣价', 0, @srv_total_amt + @goods_total_amt + @card_total_amt) insert #st_results(type, cnt, amt) values('物原价', 0, @goods_total_rawamt) insert #st_results(type, cnt, amt) values('物折扣价', 0, @goods_total_amt) insert #st_results(type, cnt, amt) values('卡原价', 0, @card_total_rawamt) insert #st_results(type, cnt, amt) values('卡折扣价', 0, @card_total_amt) -- 计算折扣的金额存入 insert #st_results(type, cnt, amt) values ('A-02', 0, (@srv_total_amt + @goods_total_amt + @card_total_amt) - (@srv_total_rawamt + @goods_total_rawamt + @card_total_rawamt) - isnull(@pay_discount_amt, 0)) -- 金额 = 服务金额 + 卡的金额 + 产品销售的金额 insert #st_results(type, cnt, amt) values ('A-03', 0, @srv_total_amt + @goods_total_amt + @card_total_amt-isnull(@pay_discount_amt, 0)) -- 现金 insert #st_results(type, cnt, amt) values ('A-04', 0, @cash_use_amt) -- 银行卡 insert #st_results(type, cnt, amt) values ('A-05', 0, @credit_use_amt) -- 会员卡 insert #st_results(type, cnt, amt) values ('A-06', 0, @card_use_amt) -- 美发 insert #st_results(type, cnt, amt) values ('A-07', 0, @srv_mei_total_amt) -- 销售卡 insert #st_results(type, cnt, amt) values ('A-08', @card_total_cnt, @card_total_tamt) -- 其他 insert #st_results(type, cnt, amt) values ('A-09', 0, @srv_other_amt) -- 指定客户的服务项目业绩 insert #st_results(type, cnt, amt) values ('A-10', @t_f_cnt, @t_f_amt) -- 轮班客户的服务项目业绩 insert #st_results(type, cnt, amt) values ('A-11', @t_n_cnt, @t_n_amt) -- 老客户的服务项目业绩 insert #st_results(type, cnt, amt) values ('A-20', @t_old_cnt, @t_old_amt) -- 新客户的服务项目业绩 insert #st_results(type, cnt, amt) values ('A-21', @t_new_cnt, @t_new_amt) -- 女客 insert #st_results(type, cnt, amt) values ('A-12', @female_count, @female_cmt) -- 男客 insert #st_results(type, cnt, amt) values ('A-13', @male_count, @male_cmt) -- 产品销售业绩及数量(实际价) add by leiji on 2013年7月13日 insert #st_results(type, cnt, amt) values ('A-25', @goodsell_cnt, @goodsell_amt) -- 产品销售业绩及数量(标准价) insert #st_results(type, cnt, amt) values ('A-26', @goodsell_cnt, @goods_total_rawamt) select day = datepart(day, cast(@fromdate as datetime)), idd, isnull(type,'') type, isnull(cnt,0) cnt,isnull(amt,0) amt, isnull(rawamt,0) rawamt, isnull(price,0) price, isnull(amt_t,0) amt_t, isnull(amt_f,0) amt_f, isnull(cnt_t,0) cnt_t,isnull(cnt_f,0) cnt_f from #st_results order by type drop table #st_bills drop table #st_bills_fix drop table #st_bills_nofix drop table #st_results end GO /****** Object: StoredProcedure [dbo].[upg_order_sales_allcomp] Script Date: 06/12/2017 06:20:25 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_order_sales_allcomp] ( @compid varchar(10), -- 公司代码 @fromdate varchar(10), -- 开始日期 @todate varchar(10), -- 截至日期 @orderkind int, -- 1-项目 2-物品 3-疗程 4-卡销售 5-会员卡升级 6-销售 + 充值 7-所有 @orderby int, -- 0-按金额排序 1-按数量排序 @amttype int, -- 0-所有 1-现金 2-银行卡 3-只包括银行卡 + 现金 4-现金 + 信用 + 储值 @datacondition int ) as begin declare @payment_0 varchar(10) declare @payment_3 varchar(10) declare @payment_$ varchar(10) declare @payment_u1 varchar(10), @payment_u2 varchar(10), @payment_u3 varchar(10), @payment_u4 varchar(10), @payment_u5 varchar(10) exec upg_isrevenue_payment @compid, @payment_0 output, @payment_3 output, @payment_$ output, @payment_u1 output, @payment_u2 output, @payment_u3 output, @payment_u4 output, @payment_u5 output create table #tbl_order ( seq int identity not null, kind int not null, -- 类别 1-项目 2-物品 3-疗程 4-卡销售 5-会员卡升级 code varchar(20) not null, -- 代码 nname varchar(100) null, -- 名称 num float null, -- 数量 amt float null, -- 金额 ) declare @PK_Sql nvarchar(max) set @PK_Sql = ' alter table #tbl_order add constraint PK_#tbl_order_' + convert(varchar(10), cast(floor(rand() * 1000000) as int)) + ' PRIMARY KEY(seq)' exec sp_executesql @PK_Sql if (@orderkind = 1 or @orderkind = 7) begin declare @procpay varchar(10) select @procpay = '9' -- 把项目消费 if @amttype = 0 -- 所有支付方式 begin if (@datacondition = 0) -- 所有项目 begin insert #tbl_order(kind, code, nname, num, amt) select 1, gma03c, nname = gda03c, sum(gma05f), sum(gma06f) from gmm01 a, gdm01 b, gam26 z where a.gma00c = b.gda00c and a.gma03c = b.gda01c and a.gma02i = 1 and a.gma00c = z.gaz02c and z.gaz01c = @compid and a.gma01d >= @fromdate and a.gma01d <= @todate and isnull(a.gma04c, '')<>'' group by gma03c, gda03c end else if (@datacondition = 1) -- 不包括疗程项目 begin insert #tbl_order(kind, code, nname, num, amt) select 1, gma03c, nname = gda03c, sum(gma05f), sum(gma06f) from gmm01 a, gdm01 b, gam26 z where a.gma00c = b.gda00c and a.gma03c = b.gda01c and a.gma02i = 1 and a.gma00c = z.gaz02c and z.gaz01c = @compid and a.gma01d >= @fromdate and a.gma01d <= @todate and (a.gma04c <> @procpay) and isnull(a.gma04c, '')<>'' group by gma03c, gda03c end else if (@datacondition = 2) -- 不包括疗程项目与卡销售项目 begin insert #tbl_order(kind, code, nname, num, amt) select 1, gma03c, nname = gda03c, sum(gma05f), sum(gma06f) from gmm01 a, gdm01 b, gam26 z where a.gma00c = b.gda00c and a.gma03c = b.gda01c and a.gma02i = 1 and a.gma00c = z.gaz02c and z.gaz01c = @compid and a.gma01d >= @fromdate and a.gma01d <= @todate and a.gma04c in ('1', '6', 'ZFB', 'WX', @payment_0, @payment_3, @payment_$, @payment_u1, @payment_u2, @payment_u3, @payment_u4, @payment_u5) group by gma03c, gda03c end end else if @amttype = 1 begin insert #tbl_order(kind, code, nname, num, amt) select 1, gma03c, nname = gda03c, sum(gma05f), sum(gma06f) from gmm01 a, gdm01 b, gam26 z where a.gma00c = b.gda00c and a.gma03c = b.gda01c and a.gma02i = 1 and a.gma00c = z.gaz02c and z.gaz01c = @compid and a.gma01d >= @fromdate and a.gma01d <= @todate and a.gma04c = '1' group by gma03c, gda03c end else if @amttype = 2 begin insert #tbl_order(kind, code, nname, num, amt) select 1, gma03c, nname = gda03c, sum(gma05f), sum(gma06f) from gmm01 a, gdm01 b, gam26 z where a.gma00c = b.gda00c and a.gma03c = b.gda01c and a.gma02i = 1 and a.gma00c = z.gaz02c and z.gaz01c = @compid and a.gma01d >= @fromdate and a.gma01d <= @todate and a.gma04c = '6' group by gma03c, gda03c end else if @amttype = 3 begin insert #tbl_order(kind, code, nname, num, amt) select 1, gma03c, nname = gda03c, sum(gma05f), sum(gma06f) from gmm01 a, gdm01 b, gam26 z where a.gma00c = b.gda00c and a.gma03c = b.gda01c and a.gma02i = 1 and a.gma00c = z.gaz02c and z.gaz01c = @compid and a.gma01d >= @fromdate and a.gma01d <= @todate and a.gma04c in ('1', '6', 'ZFB', 'WX', @payment_0, @payment_3, @payment_$, @payment_u1, @payment_u2, @payment_u3, @payment_u4, @payment_u5) group by gma03c, gda03c end else if @amttype = 4 begin insert #tbl_order(kind, code, nname, num, amt) select 1, gma03c, nname = gda03c, sum(gma05f), sum(gma06f) from gmm01 a, gdm01 b, gam26 z where a.gma00c = b.gda00c and a.gma03c = b.gda01c and a.gma02i = 1 and a.gma00c = z.gaz02c and z.gaz01c = @compid and a.gma01d >= @fromdate and a.gma01d <= @todate and a.gma04c in ('1', '6', 'ZFB', 'WX', @payment_0, @payment_3, @payment_$, @payment_u1, @payment_u2, @payment_u3, @payment_u4, @payment_u5) group by gma03c, gda03c end end if (@orderkind = 2 or @orderkind = 7) begin -- 产品销售的情况的统计 if @amttype = 0 begin insert #tbl_order(kind, code, nname, num, amt) select 2, gma03c, nname = gfa03c, sum(gma05f), sum(gma06f) from gmm01 a, gfm01 b, gam26 z where a.gma00c = b.gfa00c and a.gma03c = b.gfa01c and a.gma02i = 2 and a.gma00c = z.gaz02c and z.gaz01c = @compid and a.gma01d >= @fromdate and a.gma01d <= @todate and isnull(a.gma04c, '')<>'' group by gma03c, gfa03c end else if @amttype = 1 begin insert #tbl_order(kind, code, nname, num, amt) select 2, gma03c, nname = gfa03c, sum(gma05f), sum(gma06f) from gmm01 a, gfm01 b, gam26 z where a.gma00c = b.gfa00c and a.gma03c = b.gfa01c and a.gma02i = 2 and a.gma00c = z.gaz02c and z.gaz01c = @compid and a.gma01d >= @fromdate and a.gma01d <= @todate and a.gma04c = '1' group by gma03c, gfa03c end else if @amttype = 2 begin insert #tbl_order(kind, code, nname, num, amt) select 2, gma03c, nname = gfa03c, sum(gma05f), sum(gma06f) from gmm01 a, gfm01 b, gam26 z where a.gma00c = b.gfa00c and a.gma03c = b.gfa01c and a.gma02i = 2 and a.gma00c = z.gaz02c and z.gaz01c = @compid and a.gma01d >= @fromdate and a.gma01d <= @todate and a.gma04c = '6' group by gma03c, gfa03c end else if @amttype = 3 begin insert #tbl_order(kind, code, nname, num, amt) select 2, gma03c, nname = gfa03c, sum(gma05f), sum(gma06f) from gmm01 a, gfm01 b, gam26 z where a.gma00c = b.gfa00c and a.gma03c = b.gfa01c and a.gma02i = 2 and a.gma00c = z.gaz02c and z.gaz01c = @compid and a.gma01d >= @fromdate and a.gma01d <= @todate and a.gma04c in ('1', '6', 'ZFB', 'WX', @payment_0, @payment_3, @payment_$, @payment_u1, @payment_u2, @payment_u3, @payment_u4, @payment_u5) group by gma03c, gfa03c end else if @amttype = 4 begin insert #tbl_order(kind, code, nname, num, amt) select 2, gma03c, nname = gfa03c, sum(gma05f), sum(gma06f) from gmm01 a, gfm01 b, gam26 z where a.gma00c = b.gfa00c and a.gma03c = b.gfa01c and a.gma02i = 2 and a.gma00c = z.gaz02c and z.gaz01c = @compid and a.gma01d >= @fromdate and a.gma01d <= @todate and a.gma04c in ('1', '6', 'ZFB', 'WX', @payment_0, @payment_3, @payment_$, @payment_u1, @payment_u2, @payment_u3, @payment_u4, @payment_u5, '4') group by gma03c, gfa03c end end --疗程的销售情况 if (@orderkind = 3 or @orderkind = 7) begin -- 产品销售的情况的统计 -- 把项目消费 if @amttype = 0 begin create table #tbl_order_1( seq int not null identity, kind int not null, -- 类别 1 项目 2 物品 3疗程 4卡销售 5会员卡升级 code varchar(20) not null, -- 代码 nname varchar(100) null, -- 名称 num float null, -- 数量 amt float null, -- 金额 ) set @PK_Sql = ' alter table #tbl_order_1 add constraint PK_#tbl_order_1_' + convert(varchar(10), cast(floor(rand() * 1000000) as int)) + ' PRIMARY KEY(seq)' exec sp_executesql @PK_Sql insert #tbl_order_1(kind, code, nname, num, amt) select 3, gma03c, nname = gda03c, sum(gma05f), sum(gma06f) from gmm01 a, gdm01 b, gam26 z where a.gma00c = b.gda00c and a.gma03c = b.gda01c and a.gma02i = 3 and a.gma00c = z.gaz02c and z.gaz01c = @compid and a.gma01d >= @fromdate and a.gma01d <= @todate and isnull(a.gma04c, '')<>'' group by gma03c, gda03c --疗程购买疗程,已经统计在上面,为负数了 insert #tbl_order(kind, code, nname, num, amt) select kind, code, nname, sum(num), sum(amt) from #tbl_order_1 group by kind, code, nname drop table #tbl_order_1 end else if @amttype = 1 begin insert #tbl_order(kind, code, nname, num, amt) select 3, gma03c, nname = gda03c, sum(gma05f), sum(gma06f) from gmm01 a, gdm01 b, gam26 z where a.gma00c = b.gda00c and a.gma03c = b.gda01c and a.gma02i = 3 and a.gma00c = z.gaz02c and z.gaz01c = @compid and a.gma01d >= @fromdate and a.gma01d <= @todate and a.gma04c = '1' group by gma03c, gda03c end else if @amttype = 2 begin insert #tbl_order(kind, code, nname, num, amt) select 3, gma03c, nname = gda03c, sum(gma05f), sum(gma06f) from gmm01 a, gdm01 b, gam26 z where a.gma00c = b.gda00c and a.gma03c = b.gda01c and a.gma02i = 3 and a.gma00c = z.gaz02c and z.gaz01c = @compid and a.gma01d >= @fromdate and a.gma01d <= @todate and a.gma04c = '6' group by gma03c, gda03c end else if @amttype = 3 begin insert #tbl_order(kind, code, nname, num, amt) select 3, gma03c, nname = gda03c, sum(gma05f), sum(gma06f) from gmm01 a, gdm01 b, gam26 z where a.gma00c = b.gda00c and a.gma03c = b.gda01c and a.gma02i = 3 and a.gma00c = z.gaz02c and z.gaz01c = @compid and a.gma01d >= @fromdate and a.gma01d <= @todate and a.gma04c in ('1', '6', 'ZFB', 'WX', @payment_0, @payment_3, @payment_$, @payment_u1, @payment_u2, @payment_u3, @payment_u4, @payment_u5) group by gma03c, gda03c end else if @amttype = 4 begin insert #tbl_order(kind, code, nname, num, amt) select 3, gma03c, nname = gda03c, sum(gma05f), sum(gma06f) from gmm01 a, gdm01 b, gam26 z where a.gma00c = b.gda00c and a.gma03c = b.gda01c and a.gma02i = 3 and a.gma00c = z.gaz02c and z.gaz01c = @compid and a.gma01d >= @fromdate and a.gma01d <= @todate and a.gma04c in ('1', '6', 'ZFB', 'WX', @payment_0, @payment_3, @payment_$, @payment_u1, @payment_u2, @payment_u3, @payment_u4, @payment_u5, '4') group by gma03c, gda03c end end --会员卡销售的统计 if (@orderkind = 4 or @orderkind = 6 or @orderkind = 7) begin if @amttype = 0 -- 所有,不分支付方式 begin insert #tbl_order(kind, code, nname, num, amt) select 4, gma03c, nname = gak02c, sum(gma05f), sum(gma06f) from gmm01 a, gam10 b, gam26 z where a.gma00c = b.gak00c and a.gma03c = b.gak01c and a.gma02i = 4 and a.gma00c = z.gaz02c and z.gaz01c = @compid and a.gma01d >= @fromdate and a.gma01d <= @todate and isnull(a.gma04c, '')<>'' group by gma03c, gak02c end else if @amttype = 1 -- 现金 begin insert #tbl_order(kind, code, nname, num, amt) select 4, gma03c, nname = gak02c, sum(gma05f), sum(gma06f) from gmm01 a, gam10 b, gam26 z where a.gma00c = b.gak00c and a.gma03c = b.gak01c and a.gma02i = 4 and a.gma00c = z.gaz02c and z.gaz01c = @compid and a.gma01d >= @fromdate and a.gma01d <= @todate and a.gma04c = '1' group by gma03c, gak02c end else if @amttype = 2 --银行卡 begin insert #tbl_order(kind, code, nname, num, amt) select 4, gma03c, nname = gak02c, sum(gma05f), sum(gma06f) from gmm01 a, gam10 b, gam26 z where a.gma00c = b.gak00c and a.gma03c = b.gak01c and a.gma02i = 4 and a.gma00c = z.gaz02c and z.gaz01c = @compid and a.gma01d >= @fromdate and a.gma01d <= @todate and a.gma04c = '6' group by gma03c, gak02c end else if @amttype = 3 --现金 + 银行卡 begin insert #tbl_order(kind, code, nname, num, amt) select 4, gma03c, nname = gak02c, sum(gma05f), sum(gma06f) from gmm01 a, gam10 b, gam26 z where a.gma00c = b.gak00c and a.gma03c = b.gak01c and a.gma02i = 4 and a.gma00c = z.gaz02c and z.gaz01c = @compid and a.gma01d >= @fromdate and a.gma01d <= @todate and a.gma04c in ('1', '6', 'ZFB', 'WX', @payment_0, @payment_3, @payment_$, @payment_u1, @payment_u2, @payment_u3, @payment_u4, @payment_u5) group by gma03c, gak02c end else if @amttype = 4 --现金 + 银行卡 begin insert #tbl_order(kind, code, nname, num, amt) select 4, gma03c, nname = gak02c, sum(gma05f), sum(gma06f) from gmm01 a, gam10 b, gam26 z where a.gma00c = b.gak00c and a.gma03c = b.gak01c and a.gma02i = 4 and a.gma00c = z.gaz02c and z.gaz01c = @compid and a.gma01d >= @fromdate and a.gma01d <= @todate and a.gma04c in ('1', '6', 'ZFB', 'WX', @payment_0, @payment_3, @payment_$, @payment_u1, @payment_u2, @payment_u3, @payment_u4, @payment_u5, '4') group by gma03c, gak02c end end if (@orderkind = 5 or @orderkind = 6 or @orderkind = 7) begin if @amttype = 0 begin insert #tbl_order(kind, code, nname, num, amt) select 5, gma03c, nname = gak02c, sum(gma05f), sum(gma06f) from gmm01 a, gam10 b, gam26 z where a.gma00c = b.gak00c and a.gma03c = b.gak01c and a.gma02i = 5 and a.gma00c = z.gaz02c and z.gaz01c = @compid and a.gma01d >= @fromdate and a.gma01d <= @todate and isnull(a.gma04c, '')<>'' group by gma03c, gak02c end else if @amttype = 1 begin insert #tbl_order(kind, code, nname, num, amt) select 5, gma03c, nname = gak02c, sum(gma05f), sum(gma06f) from gmm01 a, gam10 b, gam26 z where a.gma00c = b.gak00c and a.gma03c = b.gak01c and a.gma02i = 5 and a.gma00c = z.gaz02c and z.gaz01c = @compid and a.gma01d >= @fromdate and a.gma01d <= @todate and a.gma04c = '1' group by gma03c, gak02c end else if @amttype = 2 begin insert #tbl_order(kind, code, nname, num, amt) select 5, gma03c, nname = gak02c, sum(gma05f), sum(gma06f) from gmm01 a, gam10 b, gam26 z where a.gma00c = b.gak00c and a.gma03c = b.gak01c and a.gma02i = 5 and a.gma00c = z.gaz02c and z.gaz01c = @compid and a.gma01d >= @fromdate and a.gma01d <= @todate and a.gma04c = '6' group by gma03c, gak02c end else if @amttype = 3 begin insert #tbl_order(kind, code, nname, num, amt) select 5, gma03c, nname = gak02c, sum(gma05f), sum(gma06f) from gmm01 a, gam10 b, gam26 z where a.gma00c = b.gak00c and a.gma03c = b.gak01c and a.gma02i = 5 and a.gma00c = z.gaz02c and z.gaz01c = @compid and a.gma01d >= @fromdate and a.gma01d <= @todate and a.gma04c in ('1', '6', 'ZFB', 'WX', @payment_0, @payment_3, @payment_$, @payment_u1, @payment_u2, @payment_u3, @payment_u4, @payment_u5) group by gma03c, gak02c end else if @amttype = 4 begin insert #tbl_order(kind, code, nname, num, amt) select 5, gma03c, nname = gak02c, sum(gma05f), sum(gma06f) from gmm01 a, gam10 b, gam26 z where a.gma00c = b.gak00c and a.gma03c = b.gak01c and a.gma02i = 5 and a.gma00c = z.gaz02c and z.gaz01c = @compid and a.gma01d >= @fromdate and a.gma01d <= @todate and a.gma04c in ('1', '6', 'ZFB', 'WX', @payment_0, @payment_3, @payment_$, @payment_u1, @payment_u2, @payment_u3, @payment_u4, @payment_u5, '4') group by gma03c, gak02c end end -- if @orderby = 0 -- select * from #tbl_order order by amt desc, num desc -- else -- select * from #tbl_order order by num desc, amt desc if @orderby = 0 select seq, isnull(kind,0) kind, isnull(code,'') oldcode, isnull(code,'') newcode, isnull(code,'') code, isnull(nname,'') nname, isnull(num,0) num, isnull(amt,0) amt from #tbl_order order by amt desc, num desc else select seq, isnull(kind,0) kind, isnull(code,'') oldcode, isnull(code,'') newcode, isnull(code,'') code, isnull(nname,'') nname, isnull(num,0) num, isnull(amt,0) amt from #tbl_order order by num desc, amt desc drop table #tbl_order end GO /****** Object: StoredProcedure [dbo].[upg_sales_performace] Script Date: 06/12/2017 06:20:38 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_sales_performace] ( @compid varchar(10), @fromdate varchar(8), -- 开始日期 @todate varchar(8) -- 截至日期 ) as begin declare @incompid varchar(10) select @incompid = @compid declare @payment_0 varchar(10) declare @payment_3 varchar(10) declare @payment_$ varchar(10) declare @payment_u1 varchar(10), @payment_u2 varchar(10), @payment_u3 varchar(10), @payment_u4 varchar(10), @payment_u5 varchar(10) exec upg_isrevenue_payment @compid, @payment_0 output, @payment_3 output, @payment_$ output, @payment_u1 output, @payment_u2 output, @payment_u3 output, @payment_u4 output, @payment_u5 output if @compid = '001' select @compid = '%' create table #perf ( id int identity not null, date varchar(10) not null, cash_srv float null default 0, -- 现金收入 card_sale float null default 0, -- 卡销售或充值 card_srv float null default 0, -- 卡消费 cust_count float null default 0, -- 客流量 goods_sale float null default 0, -- 产品的销售业绩(项目支付) primary key(id) ) -- 服务项目/产品的销售的收入(不包括可能的欠款) insert #perf(date, cash_srv) select gga80d, isnull(sum(isnull(gsc05f, 0)), 0) from gsm03 a, ggm01 b, gam26 z where a.gsc00c = b.gga00c and a.gsc01c = b.gga01c and a.gsc02c = 'gx' and a.gsc04c in ('1', '6', 'ZFB', 'WX', @payment_0, @payment_3, @payment_$, @payment_u1, @payment_u2, @payment_u3, @payment_u4, @payment_u5) and b.gga00c = z.gaz02c and z.gaz01c = @incompid and isnull(gga80d, '')<>'' and isnull(b.gga80d, '') >= @fromdate and isnull(b.gga80d, '') <= @todate group by gga80d insert #perf(date, cash_srv) select gla80d, isnull(sum(isnull(gsc05f, 0)), 0) from gsm03 a with (nolock), glm01 b with (nolock), gam26 z where a.gsc00c = b.gla00c and a.gsc01c = b.gla01c and a.gsc02c = 'gl' and a.gsc04c in ('1', '6', 'ZFB', 'WX', @payment_0, @payment_3, @payment_$, @payment_u1, @payment_u2, @payment_u3, @payment_u4, @payment_u5) and b.gla00c = z.gaz02c and z.gaz01c = @incompid and isnull(b.gla80d, '') >= @fromdate and isnull(b.gla80d, '') <= @todate and isnull(gla80d, '')<>'' group by gla80d insert #perf(date, card_srv) select gga80d, isnull(sum(isnull(gsc05f, 0)), 0) from gsm03 a, ggm01 b, gam26 z where a.gsc00c = b.gga00c and a.gsc01c = b.gga01c and a.gsc02c = 'gx' and (a.gsc04c = '#' or a.gsc04c = '4' or a.gsc04c >= '9') and b.gga00c = z.gaz02c and z.gaz01c = @incompid and isnull(b.gga80d, '') >= @fromdate and isnull(b.gga80d, '') <= @todate and isnull(gga80d, '')<>'' group by gga80d insert #perf(date, card_srv) select gla80d, isnull(sum(isnull(gsc05f, 0)), 0) from gsm03 a with (nolock), glm01 b with (nolock), gam26 z where a.gsc00c = b.gla00c and a.gsc01c = b.gla01c and a.gsc02c = 'gl' and (a.gsc04c = '#' or a.gsc04c = '4' or a.gsc04c >= '9') and b.gla00c = z.gaz02c and z.gaz01c = @incompid and isnull(b.gla80d, '') >= @fromdate and isnull(b.gla80d, '') <= @todate and isnull(gla80d, '')<>'' group by gla80d --产品的销售, 只包括现金与银行卡, 只支持按项目支付 insert #perf(date, goods_sale) select gga80d, isnull(sum(isnull(ggc11f, 0)), 0) from ggm03 a, ggm01 b, gam26 z where a.ggc00c = b.gga00c and a.ggc01c = b.gga01c and a.ggc27c in ('1', '6', 'ZFB', 'WX', @payment_0, @payment_3, @payment_$, @payment_u1, @payment_u2, @payment_u3, @payment_u4, @payment_u5) and b.gga00c = z.gaz02c and z.gaz01c = @incompid and isnull(gga80d, '')<>'' and isnull(b.gga80d, '') >= @fromdate and isnull(b.gga80d, '') <= @todate group by gga80d --卖卡的销售金额(不包括可能的欠款) insert #perf(date, card_sale) select gna80d, isnull(sum(isnull(gsc05f, 0)), 0) from gsm03 a, gnm01 b, gam26 z where a.gsc00c = b.gna00c and a.gsc01c = b.gna01c and a.gsc02c = 'gn' and a.gsc04c in ('1', '6', 'ZFB', 'WX', @payment_0, @payment_3, @payment_$, @payment_u1, @payment_u2, @payment_u3, @payment_u4, @payment_u5) and b.gna00c = z.gaz02c and z.gaz01c = @incompid and isnull(b.gna80d, '') >= @fromdate and isnull(b.gna80d, '') <= @todate and isnull(gna80d, '')<>'' group by gna80d --充值, 只考虑现金 insert #perf(date, card_sale) select gcl80d, isnull(sum(isnull(gsc05f, 0)), 0) from gsm03 a, gcm10 b, gam26 z where a.gsc00c = b.gcl00c and a.gsc01c = b.gcl01c and a.gsc02c = 'gz' and a.gsc04c in ('1', '6', 'ZFB', 'WX', @payment_0, @payment_3, @payment_$, @payment_u1, @payment_u2, @payment_u3, @payment_u4, @payment_u5) and gcl00c = z.gaz02c and z.gaz01c = @incompid and isnull(b.gcl80d, '') >= @fromdate and isnull(b.gcl80d, '') <= @todate and isnull(gcl80d, '')<>'' group by gcl80d insert #perf(date, cust_count) select gga80d, sum(isnull(gga14f, 1)) from ggm01 b, gam26 z where gga00c = z.gaz02c and z.gaz01c = @incompid and isnull(b.gga80d, '') >= @fromdate and isnull(b.gga80d, '') <= @todate and isnull(gga80d, '')<>'' group by gga80d select date, card_sale = isnull(sum(isnull(card_sale, 0)), 0), card_srv = isnull(sum(isnull(card_srv, 0)), 0), cust_count = isnull(sum(isnull(cust_count, 0)), 0), cash_srv = isnull(sum(isnull(cash_srv, 0)), 0), goods_sale = isnull(sum(isnull(goods_sale, 0)), 0) from #perf group by date order by date drop table #perf end GO /****** Object: StoredProcedure [dbo].[upg_custom_consumption_department] Script Date: 06/12/2017 06:19:54 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_custom_consumption_department] ( @compid varchar(10), --会馆编号 @datefrom varchar(8), --起始日期 @dateto varchar(8), --结束日期 @memberidfrom varchar(20), --起始会员卡号 @memberidto varchar(20), --结束会员卡号 @department varchar(30) --部门编号 ) as begin declare @payment_0 varchar(10) declare @payment_3 varchar(10) declare @payment_$ varchar(10) declare @payment_u1 varchar(10), @payment_u2 varchar(10), @payment_u3 varchar(10), @payment_u4 varchar(10), @payment_u5 varchar(10) exec upg_isrevenue_payment @compid, @payment_0 output, @payment_3 output, @payment_$ output, @payment_u1 output, @payment_u2 output, @payment_u3 output, @payment_u4 output, @payment_u5 output create table #consumption --客户消费临时表 ( iid int identity not null, cardid varchar(20) null, --会员卡号 memberid varchar(20) null, --会员编号 membername varchar(40) null, --会员名称 billdate varchar(20) null, --单据日期 ttime varchar(6) null, --单据时间 billno varchar(20) null, --单据编号 consumption float null default 0, --消费金额 billtype varchar(20) null, --单据类别 compid varchar(10) null, --公司别 projectid varchar(20) null, --项目编号 department varchar(30) null, --归属部门 ) declare @PK_Sql nvarchar(max) set @PK_Sql = ' alter table #consumption add constraint PK_#consumption_' + convert(varchar(10), cast(floor(rand() * 1000000) as int)) + ' primary key clustered(iid)' exec sp_executesql @PK_Sql --项目消费(收银) insert #consumption(compid,cardid,memberid,membername,billdate,ttime,billno,consumption, billtype,projectid,department) select gga00c, b.gga09c, b.gga05c, b.gga06c, b.gga02d, b.gga04t, a.gsc01c, a.gsc05f, '项目消费',bb.ggb03c, d.gda02c from gsm03 a with(nolock), ggm01 b with(nolock), ggm02 bb with(nolock), gdm01 d with(nolock), gam26 c with(nolock) where a.gsc00c = b.gga00c and a.gsc01c = b.gga01c and bb.ggb00c = b.gga00c and bb.ggb01c = b.gga01c and bb.ggb00c = d.gda00c and bb.ggb03c = d.gda01c and d.gda02c=@department and a.gsc04c in ('1', '6', 'ZFB', 'WX', @payment_0, @payment_3, @payment_$, @payment_u1, @payment_u2, @payment_u3, @payment_u4, @payment_u5) and a.gsc02c = 'gx' and a.gsc00c = c.gaz02c and c.gaz01c = @compid and b.gga80d >= @datefrom and b.gga80d <= @dateto and (b.gga05c >= @memberidfrom or @memberidfrom = '*') and (b.gga05c <= @memberidto or @memberidto = '*') and b.gga09c not like '散客%' --更新会员基本资料的名称 update a set a.membername = b.gba03c from #consumption a, gbm01 b with(nolock) where a.compid = b.gba00c and a.memberid = b.gba01c select iid,isnull(cardid,'') cardid,isnull(memberid,'') memberid,isnull(membername,'') membername,isnull(billdate,'') billdate, isnull(ttime,'') ttime,isnull(billno,'') billno,isnull(consumption,0) consumption,isnull(compid,'') compid, isnull(billtype,'') billtype,isnull(projectid,'') projectid,isnull(department,'') department from #consumption order by cardid, billtype drop table #consumption end GO /****** Object: StoredProcedure [dbo].[upg_custom_consumption] Script Date: 06/12/2017 06:19:54 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_custom_consumption] ( @compid varchar(10), --会馆编号 @datefrom varchar(8), --起始日期 @dateto varchar(8), --结束日期 @memberidfrom varchar(20), --起始会员卡号 @memberidto varchar(20) --结束会员卡号 ) as begin declare @payment_0 varchar(10) declare @payment_3 varchar(10) declare @payment_$ varchar(10) declare @payment_u1 varchar(10), @payment_u2 varchar(10), @payment_u3 varchar(10), @payment_u4 varchar(10), @payment_u5 varchar(10) exec upg_isrevenue_payment @compid, @payment_0 output, @payment_3 output, @payment_$ output, @payment_u1 output, @payment_u2 output, @payment_u3 output, @payment_u4 output, @payment_u5 output create table #consumption --客户消费临时表 ( iid int identity not null, cardid varchar(20) null, --会员卡号 memberid varchar(20) null, --会员编号 membername varchar(40) null, --会员名称 billdate varchar(20) null, --单据日期 ttime varchar(6) null, --单据时间 billno varchar(20) null, --单据编号 consumption float null default 0, --消费金额 billtype varchar(20) null, --单据类别 compid varchar(10) null, --公司别 mobile varchar(20) null ) declare @PK_Sql nvarchar(max) set @PK_Sql = ' alter table #consumption add constraint PK_#consumption_' + convert(varchar(10), cast(floor(rand() * 1000000) as int)) + ' primary key clustered(iid)' exec sp_executesql @PK_Sql --项目消费(收银) insert #consumption(compid, cardid, memberid, membername, billdate, ttime, billno, consumption, billtype) select gga00c, b.gga09c, b.gga05c, b.gga06c, b.gga02d, b.gga04t, a.gsc01c, a.gsc05f, '项目消费' from gsm03 a with(nolock), ggm01 b with(nolock), gam26 c with(nolock) where a.gsc00c = b.gga00c and a.gsc01c = b.gga01c and a.gsc04c in ('1', '6', 'ZFB', 'WX', @payment_0, @payment_3, @payment_$, @payment_u1, @payment_u2, @payment_u3, @payment_u4, @payment_u5) and a.gsc02c = 'gx' and a.gsc00c = c.gaz02c and c.gaz01c = @compid and b.gga80d >= @datefrom and b.gga80d <= @dateto and (b.gga05c >= @memberidfrom or @memberidfrom = '*') and (b.gga05c <= @memberidto or @memberidto = '*') --会员卡销售 insert #consumption(compid, cardid, memberid, membername, billdate, ttime, billno, consumption, billtype) select gna00c, b.gna13c, b.gna04c, b.gna06c, b.gna02d, b.gna12t, a.gsc01c, a.gsc05f, '会员卡销售' from gsm03 a with(nolock), gnm01 b with(nolock), gam26 c with(nolock) where a.gsc00c = b.gna00c and a.gsc01c = b.gna01c and a.gsc04c in ('1', '6', 'ZFB', 'WX', @payment_0, @payment_3, @payment_$, @payment_u1, @payment_u2, @payment_u3, @payment_u4, @payment_u5) and a.gsc02c = 'gn' and a.gsc00c = c.gaz02c and c.gaz01c = @compid and b.gna80d >= @datefrom and b.gna80d <= @dateto and (b.gna04c >= @memberidfrom or @memberidfrom = '*') and (b.gna04c <= @memberidto or @memberidto = '*') --账户异动 insert #consumption( compid, cardid, memberid, membername, billdate, ttime, billno, consumption, billtype) select gcl00c, b.gcl03c, c.gca04c, '', b.gcl80d, b.gcl56t, a.gsc01c, a.gsc05f, '账户异动' from gsm03 a with(nolock), gcm10 b with(nolock), gcm01 c with(nolock), gam26 e with(nolock) where a.gsc00c = b.gcl00c and a.gsc01c = b.gcl01c and a.gsc04c in ('1', '6', 'ZFB', 'WX', @payment_0, @payment_3, @payment_$, @payment_u1, @payment_u2, @payment_u3, @payment_u4, @payment_u5) and a.gsc02c = 'gz' and a.gsc00c = e.gaz02c and e.gaz01c = @compid and b.gcl80d >= @datefrom and b.gcl80d <= @dateto and b.gcl03c = c.gca01c and b.gcl00c = c.gca00c and (c.gca04c >= @memberidfrom or @memberidfrom = '*') and (c.gca04c <= @memberidto or @memberidto = '*') delete #consumption where cardid like '散客%' --更新会员基本资料的名称 update a set a.membername = b.gba03c, a.mobile = gba08c from #consumption a, gbm01 b with(nolock) where a.compid = b.gba00c and a.memberid = b.gba01c select iid,isnull(cardid,'') cardid,isnull(memberid,'') memberid,isnull(membername,'') membername,isnull(billdate,'') billdate, isnull(ttime,'') ttime,isnull(billno,'') billno,isnull(consumption,0) consumption,isnull(compid,'') compid, isnull(billtype,'') billtype,isnull(mobile,'') mobile from #consumption order by cardid, billtype drop table #consumption end GO /****** Object: StoredProcedure [dbo].[upg_day_total_detail_depart] Script Date: 06/12/2017 06:19:57 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_day_total_detail_depart]( @compid varchar(10), @fromdate varchar(20), @todate varchar(20), @staff varchar(20), --收银人员 @fromdepart varchar(20), --部门 add by lyj 2007-09-06 @todepart varchar(20) ) as begin declare @payment_0 varchar(10) declare @payment_3 varchar(10) declare @payment_$ varchar(10) declare @payment_u1 varchar(10), @payment_u2 varchar(10), @payment_u3 varchar(10), @payment_u4 varchar(10), @payment_u5 varchar(10) exec upg_isrevenue_payment @compid, @payment_0 output, @payment_3 output, @payment_$ output, @payment_u1 output, @payment_u2 output, @payment_u3 output, @payment_u4 output, @payment_u5 output if @staff = '*' or @staff = '' begin select @staff = '%' end create table #tbl_item_group( idd int identity not null, -- 序号 flag int not null, -- 标志 1 项目 2会员卡 3产品 prjtype varchar(20) null, -- 项目类别,产品类别 itemid varchar(20) null, -- 项目代码 产品代码 itemname varchar(60) null, -- 名称 quan float null, -- 数量 amt float null, -- 金额 sort int null --序号 primary key(idd)) create table #tbl_item_group_dept( idd int identity not null, -- 序号 flag int not null, -- 标志 1 项目 2会员卡 3产品 prjtype varchar(20) null, -- 项目类别,产品类别 itemid varchar(20) null, -- 项目代码 产品代码 itemname varchar(60) null, -- 名称 quan float null, -- 数量 amt float null, -- 金额 ) declare @PK_Sql nvarchar(max) set @PK_Sql = ' alter table #tbl_item_group_dept add constraint PK_#tbl_item_group_dept_' + convert(varchar(10), cast(floor(rand() * 1000000) as int)) + ' primary key nonclustered(idd)' exec sp_executesql @PK_Sql create clustered index idx_item_group_dept_02 on #tbl_item_group_dept(flag, itemid) create table #tbl_fill_card( cardid varchar(20) not null, -- 卡号 amt float null, quan float null, primary key(cardid)) create table #tbl_fill_card_detail( cardid varchar(20) not null, -- 卡号 amt float null, quan float null) create table #tbl_fill_card_dept( cardid varchar(20) not null, -- 卡号 amt float null, quan float null) create clustered index idx_tbl_fill_card_dept_01 on #tbl_fill_card_dept(cardid) create table #tbl_fill_card_detail_dept( cardid varchar(20) not null, -- 卡号 amt float null, quan float null) create clustered index idx_fill_card_detail_02 on #tbl_fill_card_detail_dept(cardid) --每个项目的实际金额 insert #tbl_item_group(flag, itemid, quan, amt) select 1, ggb03c, sum(ggb05f), sum(ggb11f) from ggm02 b with (nolock), ggm01 a with (nolock), gam26 z where b.ggb00c = a.gga00c and b.ggb01c = a.gga01c and a.gga00c = z.gaz02c and z.gaz01c = @compid and a.gga80d >= @fromdate and a.gga80d <= @todate and a.gga93c like @staff and (b.ggb81c between @fromdepart and @todepart or @fromdepart = '*') --add by lyj 2007-09-06 group by ggb03c, ggb81c --新增疗程 lucz 2015.10.21 insert #tbl_item_group(flag, itemid, quan, amt) select 4,gch04c,sum(quan),sum(amt) from ( select gch04c, gch05f+gch06f quan,gch07f amt from gcm08 left join gnm01 on gna00c = gch00c and gna01c = gch11c where gch00c in (select gaz02c from gam26 where gaz01c = @compid) and gch10c = 'gn' and gch02d between @fromdate and @todate and (@fromdepart = '*' or gna81c between @fromdepart and @todepart or gna82c between @fromdepart and @todepart or gna83c between @fromdepart and @todepart or gna100c between @fromdepart and @todepart or gna101c between @fromdepart and @todepart or gna102c between @fromdepart and @todepart or gna103c between @fromdepart and @todepart or gna104c between @fromdepart and @todepart or gna105c between @fromdepart and @todepart or gna106c between @fromdepart and @todepart ) union all select gch04c, gch05f+gch06f quan,gch07f amt from gcm08 left join gcm10 on gcl00c = gch00c and gcl01c = gch11c where gch00c in (select gaz02c from gam26 where gaz01c = @compid) and gch10c = 'gz' and gch02d between @fromdate and @todate and (@fromdepart = '*' or gcl81c between @fromdepart and @todepart or gcl82c between @fromdepart and @todepart or gcl83c between @fromdepart and @todepart or gcl100c between @fromdepart and @todepart or gcl101c between @fromdepart and @todepart or gcl102c between @fromdepart and @todepart or gcl103c between @fromdepart and @todepart or gcl104c between @fromdepart and @todepart or gcl105c between @fromdepart and @todepart or gcl106c between @fromdepart and @todepart ) ) as Atable group by gch04c --疗程消费(消耗) insert #tbl_item_group(flag, itemid, quan, amt) select 5,ggb03c,sum(ggb05f),sum(ggb11f) from ggm02 inner join ggm01 on gga00c = ggb00c and gga01c = ggb01c where ggb00c in (select gaz02c from gam26 where gaz01c = @compid) and isnull(ggb47i,-1) <> -1 and gga94d between @fromdate and @todate and (@fromdepart = '*' or ggb81c between @fromdepart and @todepart) group by ggb03c --产品的统计 create table #tbl_item_group_detail( idd int identity not null, -- 序号 flag int not null, -- 标志 1 项目 2会员卡 3产品 prjtype varchar(20) null, -- 项目类别,产品类别 itemid varchar(20) null, -- 项目代码 产品代码 itemname varchar(60) null, -- 名称 quan float null, -- 数量 amt float null, -- 金额 primary key(idd)) --第一销售的部门 insert #tbl_item_group_detail(flag, itemid, quan, amt) select 3, ggc03c, sum(ggc05f*isnull(ggc14f, 0)), sum(ggc11f*isnull(ggc14f, 0)) from ggm03 b with (nolock), ggm01 a with (nolock), gam26 z where b.ggc00c = a.gga00c and b.ggc01c = a.gga01c and a.gga00c = z.gaz02c and z.gaz01c = @compid and a.gga80d >= @fromdate and a.gga80d <= @todate and a.gga93c like @staff and (b.ggc81c between @fromdepart and @todepart or (@fromdepart = '*')) --add by lyj 2007-09-06 group by ggc03c, ggc81c --第二销售的部门 insert #tbl_item_group_detail(flag, itemid, quan, amt) select 3, ggc03c, sum(ggc05f*isnull(ggc16f, 0)), sum(ggc11f*isnull(ggc16f, 0)) from ggm03 b with (nolock), ggm01 a with (nolock), gam26 z where b.ggc00c = a.gga00c and b.ggc01c = a.gga01c and a.gga00c = z.gaz02c and z.gaz01c = @compid and a.gga80d >= @fromdate and a.gga80d <= @todate and a.gga93c like @staff and (b.ggc82c between @fromdepart and @todepart or (@fromdepart = '*')) --add by lyj 2007-09-06 group by ggc03c, ggc82c --第三销售的部门 insert #tbl_item_group_detail(flag, itemid, quan, amt) select 3, ggc03c, sum(ggc05f*isnull(ggc18f, 0)), sum(ggc11f*isnull(ggc18f, 0)) from ggm03 b with (nolock), ggm01 a with (nolock), gam26 z where b.ggc00c = a.gga00c and b.ggc01c = a.gga01c and a.gga00c = z.gaz02c and z.gaz01c = @compid and a.gga80d >= @fromdate and a.gga80d <= @todate and a.gga93c like @staff and (b.ggc83c between @fromdepart and @todepart or (@fromdepart = '*')) --add by lyj 2007-09-06 group by ggc03c, ggc83c insert #tbl_item_group(flag, itemid, quan, amt) select flag, itemid, sum(quan), sum(amt) from #tbl_item_group_detail group by flag, itemid drop table #tbl_item_group_detail update a set a.prjtype = b.gda13c from #tbl_item_group a, gdm01 b where a.itemid = b.gda01c and a.flag = 1 and b.gda00c = @compid update a set a.prjtype = b.gfa24c from #tbl_item_group a, gfm01 b where a.itemid = b.gfa01c and a.flag = 3 and b.gfa00c = @compid --start更新序号 add by lm 2016.01.19 update a set a.sort = b.gsb06i from #tbl_item_group a, gsm02 b where a.prjtype = b.gsb02c and a.flag = 1 and b.gsb00c = @compid and b.gsb01c = 'AJ' update a set a.sort = b.gsb06i from #tbl_item_group a, gsm02 b where a.prjtype = b.gsb02c and a.flag = 3 and b.gsb00c = @compid and b.gsb01c = 'AL' --end --卡的统计 --一段时间内卡的销售 create table #tbl_gnm01_gnm02( gna00c varchar(10) not null, --公司编号 gna01c varchar(20) not null, --销售单号 gna27f float null, --第一分享比率 gna29f float null, --分享比率2 gna31f float null, --分享比率3 gna33f float null, --分享比率4 gna35f float null, --分享比率5 gna37f float null, --分享比率6 gna39f float null, --分享比率7 gna41f float null, --分享比率8 gna43f float null, --分享比率9 gna45f float null, --分享比率10 gna81c varchar(10) null, --部门编号 gna82c varchar(10) null, --部门编号2 gna83c varchar(10) null, --部门编号3 gnb03c varchar(20) not null, --会员卡号 gnb08f float null, --卡销售金额(包括疗程套餐的销售金额) num float null, gna100c varchar(10) null, --部门编号4 Add by maql 20100705 gna101c varchar(10) null, --部门编号5 Add by maql 20100705 gna102c varchar(10) null, --部门编号6 Add by maql 20100705 gna103c varchar(10) null, --部门编号7 Add by maql 20100705 gna104c varchar(10) null, --部门编号8 Add by maql 20100705 gna105c varchar(10) null, --部门编号9 Add by maql 20100705 gna106c varchar(10) null, --部门编号10 Add by maql 20100705 gna67f float null, --第一销售虚业绩 Added by WUXM 2005/11/04 gna69f float null, --第二销售虚业绩 Added by WUXM 2005/11/04 gna71f float null, --第三销售虚业绩 Added by WUXM 2005/11/04 gna86f float null, --第四人员业绩 gna87f float null, --第五人员业绩 gna88f float null, --第六人员业绩 gna89f float null, --第七人员业绩 gna90f float null, --第八人员业绩 gna91f float null, --第九人员业绩 gna92f float null, --第十人员业绩 bill_total_amt float null --业绩合计 ) create clustered index idx_gnm01_gnm02_02 on #tbl_gnm01_gnm02(gna00c, gna01c) insert #tbl_gnm01_gnm02(gna00c, gna01c, gna27f, gna29f, gna31f, gna33f, gna35f, gna37f, gna39f, gna41f, gna43f, gna45f, gna81c, gna82c, gna83c, gnb03c, gnb08f, num, gna100c, gna101c, gna102c, gna103c, gna104c, gna105c, gna106c, gna67f,gna69f,gna71f,gna86f,gna87f,gna88f,gna89f,gna90f,gna91f,gna92f,bill_total_amt ) select gna00c, gna01c, gna27f, gna29f, gna31f, gna33f, gna35f, gna37f, gna39f, gna41f, gna43f, gna45f, gna81c, gna82c, gna83c, gnb03c, gnb08f, 1, gna100c, gna101c, gna102c, gna103c, gna104c, gna105c, gna106c, gna67f,gna69f,gna71f,gna86f,gna87f,gna88f,gna89f,gna90f,gna91f,gna92f, isnull(gna67f,0)+isnull(gna69f,0)+isnull(gna71f,0)+isnull(gna86f,0)+isnull(gna87f,0)+isnull(gna88f,0)+isnull(gna89f,0)+isnull(gna90f,0)+isnull(gna91f,0)+isnull(gna92f,0) from gnm02 b with (nolock), gnm01 a with (nolock), gam26 z where a.gna00c = b.gnb00c and a.gna01c = b.gnb01c and a.gna80d >= @fromdate and a.gna80d <= @todate and a.gna00c = z.gaz02c and z.gaz01c = @compid and a.gna93c like @staff --第1销售的部门 delete #tbl_item_group_dept insert #tbl_item_group_dept(flag, itemid, quan, amt) select 2, gnb03c, sum(num*isnull(gna27f, 0)), isnull(sum( case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gna67f,0)*gsc05f /bill_total_amt end) ,0) from #tbl_gnm01_gnm02 a,gsm03 b where a.gna00c = b.gsc00c and a.gna01c = b.gsc01c and b.gsc02c = 'gn' and (a.gna81c between @fromdepart and @todepart or @fromdepart = '*') --add by lyj 2007-09-06 group by gnb03c, gna81c insert #tbl_item_group_dept(flag, itemid, quan, amt) select 2, gnb03c, sum(num*isnull(gna29f, 0)), isnull(sum( case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gna69f,0)*gsc05f /bill_total_amt end) ,0) from #tbl_gnm01_gnm02 a,gsm03 b where a.gna00c = b.gsc00c and a.gna01c = b.gsc01c and b.gsc02c = 'gn' and (a.gna82c between @fromdepart and @todepart or @fromdepart = '*') --add by lyj 2007-09-06 group by gnb03c, gna82c insert #tbl_item_group_dept(flag, itemid, quan, amt) select 2, gnb03c, sum(num*isnull(gna31f, 0)),isnull(sum( case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gna71f,0)*gsc05f /bill_total_amt end ),0) from #tbl_gnm01_gnm02 a,gsm03 b where a.gna00c = b.gsc00c and a.gna01c = b.gsc01c and b.gsc02c = 'gn' and (a.gna83c between @fromdepart and @todepart or @fromdepart = '*') --add by lyj 2007-09-06 group by gnb03c, gna83c insert #tbl_item_group_dept(flag, itemid, quan, amt) select 2, gnb03c, sum(num*isnull(gna33f, 0)), isnull(sum( case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gna86f,0)*gsc05f /bill_total_amt end ),0) from #tbl_gnm01_gnm02 a,gsm03 b where a.gna00c = b.gsc00c and a.gna01c = b.gsc01c and b.gsc02c = 'gn' and (a.gna100c between @fromdepart and @todepart or @fromdepart = '*') --add by maql 20100707 group by gnb03c, gna100c insert #tbl_item_group_dept(flag, itemid, quan, amt) select 2, gnb03c, sum(num*isnull(gna35f, 0)), isnull(sum( case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gna87f,0)*gsc05f /bill_total_amt end) ,0) from #tbl_gnm01_gnm02 a,gsm03 b where a.gna00c = b.gsc00c and a.gna01c = b.gsc01c and b.gsc02c = 'gn' and (a.gna101c between @fromdepart and @todepart or @fromdepart = '*') --add by maql 20100707 group by gnb03c, gna101c insert #tbl_item_group_dept(flag, itemid, quan, amt) select 2, gnb03c, sum(num*isnull(gna37f, 0)), isnull(sum( case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gna88f,0)*gsc05f /bill_total_amt end) ,0) from #tbl_gnm01_gnm02 a,gsm03 b where a.gna00c = b.gsc00c and a.gna01c = b.gsc01c and b.gsc02c = 'gn' and (a.gna102c between @fromdepart and @todepart or @fromdepart = '*') --add by maql 20100707 group by gnb03c, gna102c insert #tbl_item_group_dept(flag, itemid, quan, amt) select 2, gnb03c, sum(num*isnull(gna39f, 0)), isnull(sum( case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gna89f,0)*gsc05f /bill_total_amt end ),0) from #tbl_gnm01_gnm02 a,gsm03 b where a.gna00c = b.gsc00c and a.gna01c = b.gsc01c and b.gsc02c = 'gn' and (a.gna103c between @fromdepart and @todepart or @fromdepart = '*') --add by maql 20100707 group by gnb03c, gna103c insert #tbl_item_group_dept(flag, itemid, quan, amt) select 2, gnb03c, sum(num*isnull(gna41f, 0)), isnull(sum( case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gna90f,0)*gsc05f /bill_total_amt end ),0) from #tbl_gnm01_gnm02 a,gsm03 b where a.gna00c = b.gsc00c and a.gna01c = b.gsc01c and b.gsc02c = 'gn' and (a.gna104c between @fromdepart and @todepart or @fromdepart = '*') --add by maql 20100707 group by gnb03c, gna104c insert #tbl_item_group_dept(flag, itemid, quan, amt) select 2, gnb03c, sum(num*isnull(gna43f, 0)),isnull(sum( case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gna91f,0)*gsc05f /bill_total_amt end ),0) from #tbl_gnm01_gnm02 a,gsm03 b where a.gna00c = b.gsc00c and a.gna01c = b.gsc01c and b.gsc02c = 'gn' and (a.gna105c between @fromdepart and @todepart or @fromdepart = '*') --add by maql 20100707 group by gnb03c, gna105c insert #tbl_item_group_dept(flag, itemid, quan, amt) select 2, gnb03c, sum(num*isnull(gna45f, 0)), isnull(sum( case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gna92f,0)*gsc05f /bill_total_amt end ),0) from #tbl_gnm01_gnm02 a,gsm03 b where a.gna00c = b.gsc00c and a.gna01c = b.gsc01c and b.gsc02c = 'gn' and (a.gna106c between @fromdepart and @todepart or @fromdepart = '*') --add by maql 20100707 group by gnb03c, gna106c insert #tbl_item_group(flag, itemid, quan, amt) select flag, itemid, sum(quan), sum(amt) from #tbl_item_group_dept group by flag, itemid drop table #tbl_gnm01_gnm02 drop table #tbl_item_group_dept --这段时间内的冲值 create table #tbl_gcm10_buf( gcl00c varchar(10) not null, --门店编号 gcl01c varchar(20) not null, --异动单号 gcl03c varchar(20) null, --会员卡号 gcl07f float null, --异动金额(总收款金额), 含套餐,含疗程 gcl06i int null, --异动类别( 0充值, 1取款 5欠款 6还款) gcl81c varchar(10) null, --部门编号 gcl82c varchar(10) null, --部门编号2 gcl83c varchar(10) null, --部门编号3 gcl16f float null, --第一销售分享比率1 gcl18f float null, --分享比率2 gcl20f float null, --分享比率3 gcl23f float null, --分享比率4 gcl25f float null, --分享比率5 gcl27f float null, --分享比率6 gcl29f float null, --分享比率7 gcl31f float null, --分享比率8 gcl33f float null, --分享比率9 gcl35f float null, --分享比率10 num float null, gcl100c varchar(10) null, --部门编号4 gcl101c varchar(10) null, --部门编号5 gcl102c varchar(10) null, --部门编号6 gcl103c varchar(10) null, --部门编号7 gcl104c varchar(10) null, --部门编号8 gcl105c varchar(10) null, --部门编号9 gcl106c varchar(10) null, --部门编号10 gcl58f float null, gcl60f float null, gcl62f float null, gcl83f float null, gcl84f float null, gcl85f float null, gcl86f float null, gcl87f float null, gcl88f float null, gcl89f float null, bill_total_amt float null --业绩合计 ) create clustered index idx_gcm10_buf on #tbl_gcm10_buf(gcl03c) insert #tbl_gcm10_buf(gcl00c,gcl01c,gcl03c, gcl07f, gcl06i, gcl81c, gcl82c, gcl83c, gcl16f, gcl18f, gcl20f, gcl23f, gcl25f, gcl27f, gcl29f, gcl31f, gcl33f, gcl35f, num, gcl100c, gcl101c, gcl102c, gcl103c, gcl104c, gcl105c, gcl106c, gcl58f,gcl60f,gcl62f,gcl83f,gcl84f,gcl85f,gcl86f,gcl87f,gcl88f,gcl89f, bill_total_amt) select gcl00c,gcl01c, gcl03c, gcl07f, gcl06i, gcl81c, gcl82c, gcl83c, gcl16f, gcl18f, gcl20f, gcl23f, gcl25f, gcl27f, gcl29f, gcl31f, gcl33f, gcl35f, 1, gcl100c, gcl101c, gcl102c, gcl103c, gcl104c, gcl105c, gcl106c, gcl58f,gcl60f,gcl62f,gcl83f,gcl84f,gcl85f,gcl86f,gcl87f,gcl88f,gcl89f, isnull(gcl58f, 0) + isnull(gcl60f, 0) + isnull(gcl62f, 0) + isnull(gcl83f, 0) + isnull(gcl84f, 0) + isnull(gcl85f, 0) + isnull(gcl86f, 0) + isnull(gcl87f, 0) + isnull(gcl88f, 0) + isnull(gcl89f, 0) from gcm10 a with (nolock), gam26 z where gcl00c = z.gaz02c and z.gaz01c = @compid and a.gcl80d >= @fromdate and a.gcl80d <= @todate and gcl93c like @staff and isnull(gcl03c, '')<>'' --第1销售的部门的 insert #tbl_fill_card_detail_dept(cardid, amt, quan) select gcl03c, sum(case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gcl58f,0) * gsc05f/bill_total_amt end), sum(num*isnull(gcl16f, 0)) from #tbl_gcm10_buf a,gsm03 b where a.gcl00c = b.gsc00c and a.gcl01c = b.gsc01c and b.gsc02c = 'gz' and (a.gcl06i = 0 or a.gcl06i = 6) and (a.gcl81c between @fromdepart and @todepart or @fromdepart = '*') --add by lyj 2007-09-06 group by gcl03c, gcl81c --第2销售的部门的 insert #tbl_fill_card_detail_dept(cardid, amt, quan) select gcl03c, sum(case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gcl60f,0) * gsc05f/bill_total_amt end), sum(num*isnull(gcl18f, 0)) from #tbl_gcm10_buf a,gsm03 b where a.gcl00c = b.gsc00c and a.gcl01c = b.gsc01c and b.gsc02c = 'gz' and (gcl06i = 0 or gcl06i = 6) and (a.gcl82c between @fromdepart and @todepart or @fromdepart = '*') --add by lyj 2007-09-06 group by gcl03c, gcl82c --第3销售的部门的 insert #tbl_fill_card_detail_dept(cardid, amt, quan) select gcl03c, sum(case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gcl62f,0) * gsc05f/bill_total_amt end), sum(num*isnull(gcl20f, 0)) from #tbl_gcm10_buf a,gsm03 b where a.gcl00c = b.gsc00c and a.gcl01c = b.gsc01c and b.gsc02c = 'gz' and (gcl06i = 0 or gcl06i = 6) and (a.gcl83c between @fromdepart and @todepart or @fromdepart = '*') --add by lyj 2007-09-06 group by gcl03c, gcl83c --第4销售的部门的 insert #tbl_fill_card_detail_dept(cardid, amt, quan) select gcl03c, sum(case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gcl83f,0) * gsc05f/bill_total_amt end), sum(num*isnull(gcl23f, 0)) from #tbl_gcm10_buf a,gsm03 b where a.gcl00c = b.gsc00c and a.gcl01c = b.gsc01c and b.gsc02c = 'gz' and (gcl06i = 0 or gcl06i = 6) and (a.gcl100c between @fromdepart and @todepart or @fromdepart = '*') --add by maql 20100707 group by gcl03c, gcl100c --第5销售的部门的 insert #tbl_fill_card_detail_dept(cardid, amt, quan) select gcl03c, sum(case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gcl84f,0) * gsc05f/bill_total_amt end), sum(num*isnull(gcl25f, 0)) from #tbl_gcm10_buf a,gsm03 b where a.gcl00c = b.gsc00c and a.gcl01c = b.gsc01c and b.gsc02c = 'gz' and (gcl06i = 0 or gcl06i = 6) and (a.gcl101c between @fromdepart and @todepart or @fromdepart = '*') --add by maql 20100707 group by gcl03c, gcl101c --第6销售的部门的 insert #tbl_fill_card_detail_dept(cardid, amt, quan) select gcl03c, sum(case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gcl85f,0) * gsc05f/bill_total_amt end), sum(num*isnull(gcl27f, 0)) from #tbl_gcm10_buf a,gsm03 b where a.gcl00c = b.gsc00c and a.gcl01c = b.gsc01c and b.gsc02c = 'gz' and (gcl06i = 0 or gcl06i = 6) and (a.gcl102c between @fromdepart and @todepart or @fromdepart = '*') --add by maql 20100707 group by gcl03c, gcl102c --第7销售的部门的 insert #tbl_fill_card_detail_dept(cardid, amt, quan) select gcl03c, sum(case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gcl86f,0) * gsc05f/bill_total_amt end), sum(num*isnull(gcl29f, 0)) from #tbl_gcm10_buf a,gsm03 b where a.gcl00c = b.gsc00c and a.gcl01c = b.gsc01c and b.gsc02c = 'gz' and (gcl06i = 0 or gcl06i = 6) and (a.gcl103c between @fromdepart and @todepart or @fromdepart = '*') --add by maql 20100707 group by gcl03c, gcl103c --第8销售的部门的 insert #tbl_fill_card_detail_dept(cardid, amt, quan) select gcl03c, sum(case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gcl87f,0) * gsc05f/bill_total_amt end), sum(num*isnull(gcl31f, 0)) from #tbl_gcm10_buf a,gsm03 b where a.gcl00c = b.gsc00c and a.gcl01c = b.gsc01c and b.gsc02c = 'gz' and (gcl06i = 0 or gcl06i = 6) and (a.gcl104c between @fromdepart and @todepart or @fromdepart = '*') --add by maql 20100707 group by gcl03c, gcl104c --第9销售的部门的 insert #tbl_fill_card_detail_dept(cardid, amt, quan) select gcl03c,sum(case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gcl88f,0) * gsc05f /bill_total_amt end), sum(num*isnull(gcl33f, 0)) from #tbl_gcm10_buf a,gsm03 b where a.gcl00c = b.gsc00c and a.gcl01c = b.gsc01c and b.gsc02c = 'gz' and (gcl06i = 0 or gcl06i = 6) and (a.gcl105c between @fromdepart and @todepart or @fromdepart = '*') --add by maql 20100707 group by gcl03c, gcl105c --第10销售的部门的 insert #tbl_fill_card_detail_dept(cardid, amt, quan) select gcl03c, sum(case when isnull(bill_total_amt,0) = 0 then 0 else isnull(gcl89f,0) * gsc05f/bill_total_amt end), sum(num*isnull(gcl35f, 0)) from #tbl_gcm10_buf a,gsm03 b where a.gcl00c = b.gsc00c and a.gcl01c = b.gsc01c and b.gsc02c = 'gz' and (gcl06i = 0 or gcl06i = 6) and (a.gcl106c between @fromdepart and @todepart or @fromdepart = '*') --add by maql 20100707 group by gcl03c, gcl106c insert #tbl_fill_card_detail(cardid, amt, quan) select cardid, sum(amt), sum(quan) from #tbl_fill_card_detail_dept a group by cardid drop table #tbl_fill_card_detail_dept --add by lyj 把会员卡变更的金额也应该给加进去 --因为明细分类帐里面有一个记录 --insert #tbl_fill_card_detail(cardid, amt, quan) --select gea07c, sum(gsc05f), 1 --from gem01 with (nolock), gsm03 with (nolock), gam26 z --where gea00c = z.gaz02c --and z.gaz01c = @compid --and gsc00c = gea00c --and gea01c = gsc01c --and gsc02c = 'ge' --and gsc04c in ('1', '6', 'ZFB', 'WX', @payment_0, @payment_3, @payment_$, @payment_u1, @payment_u2, @payment_u3, @payment_u4, @payment_u5) --and gea80d between @fromdate and @todate --and gea21c like @staff --and (gea81c between @fromdepart and @todepart or @fromdepart = '*') --add by lyj 2007-09-06 --group by gea07c --Start竞争转卡业绩 add by lm 2015-10-13 --第一销售 insert #tbl_fill_card_detail(cardid, quan, amt) select gea07c, 1 , isnull(gea27f, 0)*dbo.fun_divide_default(isnull(gsc05f,0),isnull((isnull(gea27f,0)+isnull(gea29f,0)+isnull(gea31f,0)+isnull(gea33f,0)+isnull(gea35f,0)+isnull(gea37f,0)+isnull(gea39f,0)+isnull(gea41f,0)+isnull(gea43f,0)+isnull(gea45f,0)),0) , 0) from gem01 a with (nolock), gsm03 b with(nolock), gam26 z with (nolock) where a.gea00c = b.gsc00c and a.gea01c = b.gsc01c and a.gea00c = z.gaz02c and z.gaz01c = @compid and gsc10d >= @fromdate and gsc10d <= @todate and gsc11c like @staff and b.gsc02c = 'ge' and (isnull(a.gea81c, '') between @fromdepart and @todepart or @fromdepart = '*') --add by lm 2015-10-13 and isnull(gea21c,'') <> '' --第二销售 insert #tbl_fill_card_detail(cardid, quan, amt) select gea07c, 1 , isnull(gea29f, 0)*dbo.fun_divide_default(isnull(gsc05f,0),isnull((isnull(gea27f,0)+isnull(gea29f,0)+isnull(gea31f,0)+isnull(gea33f,0)+isnull(gea35f,0)+isnull(gea37f,0)+isnull(gea39f,0)+isnull(gea41f,0)+isnull(gea43f,0)+isnull(gea45f,0)),0) , 0) from gem01 a with (nolock), gsm03 b with(nolock), gam26 z with (nolock) where a.gea00c = b.gsc00c and a.gea01c = b.gsc01c and a.gea00c = z.gaz02c and z.gaz01c = @compid and gsc10d >= @fromdate and gsc10d <= @todate and gsc11c like @staff and b.gsc02c = 'ge' and (isnull(a.gea82c, '') between @fromdepart and @todepart or @fromdepart = '*') --add by lm 2015-10-13 and isnull(gea28c,'') <> '' --第三销售 insert #tbl_fill_card_detail(cardid, quan, amt) select gea07c, 1 , isnull(gea31f, 0)*dbo.fun_divide_default(isnull(gsc05f,0),isnull((isnull(gea27f,0)+isnull(gea29f,0)+isnull(gea31f,0)+isnull(gea33f,0)+isnull(gea35f,0)+isnull(gea37f,0)+isnull(gea39f,0)+isnull(gea41f,0)+isnull(gea43f,0)+isnull(gea45f,0)),0) , 0) from gem01 a with (nolock), gsm03 b with(nolock), gam26 z with (nolock) where a.gea00c = b.gsc00c and a.gea01c = b.gsc01c and a.gea00c = z.gaz02c and z.gaz01c = @compid and gsc10d >= @fromdate and gsc10d <= @todate and gsc11c like @staff and b.gsc02c = 'ge' and (isnull(a.gea83c, '') between @fromdepart and @todepart or @fromdepart = '*') --add by lm 2015-10-13 and isnull(gea30c,'') <> '' --第四销售 insert #tbl_fill_card_detail(cardid, quan, amt) select gea07c, 1 ,isnull(gea33f, 0)*dbo.fun_divide_default(isnull(gsc05f,0),isnull((isnull(gea27f,0)+isnull(gea29f,0)+isnull(gea31f,0)+isnull(gea33f,0)+isnull(gea35f,0)+isnull(gea37f,0)+isnull(gea39f,0)+isnull(gea41f,0)+isnull(gea43f,0)+isnull(gea45f,0)),0), 0) from gem01 a with (nolock), gsm03 b with(nolock), gam26 z with (nolock) where a.gea00c = b.gsc00c and a.gea01c = b.gsc01c and a.gea00c = z.gaz02c and z.gaz01c = @compid and gsc10d >= @fromdate and gsc10d <= @todate and gsc11c like @staff and b.gsc02c = 'ge' and (isnull(a.gea84c, '') between @fromdepart and @todepart or @fromdepart = '*') --add by lm 2015-10-13 and isnull(gea32c,'') <> '' --第五销售 insert #tbl_fill_card_detail(cardid, quan, amt) select gea07c, 1 ,isnull(gea35f, 0)*dbo.fun_divide_default(isnull(gsc05f,0),isnull((isnull(gea27f,0)+isnull(gea29f,0)+isnull(gea31f,0)+isnull(gea33f,0)+isnull(gea35f,0)+isnull(gea37f,0)+isnull(gea39f,0)+isnull(gea41f,0)+isnull(gea43f,0)+isnull(gea45f,0)),0), 0) from gem01 a with (nolock), gsm03 b with(nolock), gam26 z with (nolock) where a.gea00c = b.gsc00c and a.gea01c = b.gsc01c and a.gea00c = z.gaz02c and z.gaz01c = @compid and gsc10d >= @fromdate and gsc10d <= @todate and gsc11c like @staff and b.gsc02c = 'ge' and (isnull(a.gea85c, '') between @fromdepart and @todepart or @fromdepart = '*') --add by lm 2015-10-13 and isnull(gea34c,'') <> '' --第六销售 insert #tbl_fill_card_detail(cardid, quan, amt) select gea07c, 1 ,isnull(gea37f, 0)*dbo.fun_divide_default(isnull(gsc05f,0),isnull((isnull(gea27f,0)+isnull(gea29f,0)+isnull(gea31f,0)+isnull(gea33f,0)+isnull(gea35f,0)+isnull(gea37f,0)+isnull(gea39f,0)+isnull(gea41f,0)+isnull(gea43f,0)+isnull(gea45f,0)),0), 0) from gem01 a with (nolock),gsm03 b with(nolock), gam26 z with (nolock) where a.gea00c = b.gsc00c and a.gea01c = b.gsc01c and a.gea00c = z.gaz02c and z.gaz01c = @compid and gsc10d >= @fromdate and gsc10d <= @todate and gsc11c like @staff and b.gsc02c = 'ge' and (isnull(a.gea86c, '') between @fromdepart and @todepart or @fromdepart = '*') --add by lm 2015-10-13 and isnull(gea36c,'') <> '' --第七销售 insert #tbl_fill_card_detail(cardid, quan, amt) select gea07c, 1 ,isnull(gea39f, 0)*dbo.fun_divide_default(isnull(gsc05f,0),isnull((isnull(gea27f,0)+isnull(gea29f,0)+isnull(gea31f,0)+isnull(gea33f,0)+isnull(gea35f,0)+isnull(gea37f,0)+isnull(gea39f,0)+isnull(gea41f,0)+isnull(gea43f,0)+isnull(gea45f,0)),0), 0) from gem01 a with (nolock), gsm03 b with(nolock), gam26 z with (nolock) where a.gea00c = b.gsc00c and a.gea01c = b.gsc01c and a.gea00c = z.gaz02c and z.gaz01c = @compid and gsc10d >= @fromdate and gsc10d <= @todate and gsc11c like @staff and b.gsc02c = 'ge' and (isnull(a.gea87c, '') between @fromdepart and @todepart or @fromdepart = '*') --add by lm 2015-10-13 and isnull(gea38c,'') <> '' --第八销售 insert #tbl_fill_card_detail(cardid, quan, amt) select gea07c, 1 ,isnull(gea41f, 0)*dbo.fun_divide_default(isnull(gsc05f,0),isnull((isnull(gea27f,0)+isnull(gea29f,0)+isnull(gea31f,0)+isnull(gea33f,0)+isnull(gea35f,0)+isnull(gea37f,0)+isnull(gea39f,0)+isnull(gea41f,0)+isnull(gea43f,0)+isnull(gea45f,0)),0), 0) from gem01 a with (nolock), gsm03 b with(nolock),gam26 z with (nolock) where a.gea00c = b.gsc00c and a.gea01c = b.gsc01c and a.gea00c = z.gaz02c and z.gaz01c = @compid and gsc10d >= @fromdate and gsc10d <= @todate and gsc11c like @staff and b.gsc02c = 'ge' and (isnull(a.gea88c, '') between @fromdepart and @todepart or @fromdepart = '*') --add by lm 2015-10-13 and isnull(gea40c,'') <> '' --第九销售 insert #tbl_fill_card_detail(cardid, quan, amt) select gea07c, 1 ,isnull(gea43f, 0)*dbo.fun_divide_default(isnull(gsc05f,0),isnull((isnull(gea27f,0)+isnull(gea29f,0)+isnull(gea31f,0)+isnull(gea33f,0)+isnull(gea35f,0)+isnull(gea37f,0)+isnull(gea39f,0)+isnull(gea41f,0)+isnull(gea43f,0)+isnull(gea45f,0)),0), 0) from gem01 a with (nolock), gsm03 b with(nolock),gam26 z with (nolock) where a.gea00c = b.gsc00c and a.gea01c = b.gsc01c and a.gea00c = z.gaz02c and z.gaz01c = @compid and gsc10d >= @fromdate and gsc10d <= @todate and gsc11c like @staff and b.gsc02c = 'ge' and (isnull(a.gea89c, '') between @fromdepart and @todepart or @fromdepart = '*') --add by lm 2015-10-13 and isnull(gea42c,'') <> '' --第十销售 insert #tbl_fill_card_detail(cardid, quan, amt) select gea07c, 1 ,isnull(gea45f, 0)*dbo.fun_divide_default(isnull(gsc05f,0),isnull((isnull(gea27f,0)+isnull(gea29f,0)+isnull(gea31f,0)+isnull(gea33f,0)+isnull(gea35f,0)+isnull(gea37f,0)+isnull(gea39f,0)+isnull(gea41f,0)+isnull(gea43f,0)+isnull(gea45f,0)),0), 0) from gem01 a with (nolock), gsm03 b with(nolock), gam26 z with (nolock) where a.gea00c = b.gsc00c and a.gea01c = b.gsc01c and a.gea00c = z.gaz02c and z.gaz01c = @compid and gsc10d >= @fromdate and gsc10d <= @todate and gsc11c like @staff and b.gsc02c = 'ge' and (isnull(a.gea90c, '') between @fromdepart and @todepart or @fromdepart = '*') --add by lm 2015-10-13 and isnull(gea44c,'') <> '' --End insert #tbl_fill_card(cardid, amt, quan) select cardid, sum(amt), sum(quan) from #tbl_fill_card_detail group by cardid drop table #tbl_fill_card_detail update a set a.amt = a.amt + isnull(b.amt, 0) from #tbl_item_group a, #tbl_fill_card b where a.itemid = b.cardid and a.flag = 2 --有些卡并没有卡销售,也应该加入 delete a from #tbl_fill_card a, #tbl_item_group b where a.cardid = b.itemid and b.flag = 2 insert #tbl_item_group(flag, itemid, quan, amt) select 2, cardid, quan, amt from #tbl_fill_card delete #tbl_fill_card delete #tbl_fill_card_dept -- --欠款部分,需要扣掉 -- insert #tbl_fill_card_dept(cardid, amt, quan) -- select gcl03c, sum(gcl07f*isnull(gcl16f, 0)), 0 -- from #tbl_gcm10_buf a -- where (gcl06i = 1 or gcl06i = 5) -- and (a.gcl81c between @fromdepart and @todepart or @fromdepart = '*') --add by lyj 2007-09-06 -- group by gcl03c, gcl81c -- -- insert #tbl_fill_card_dept(cardid, amt, quan) -- select gcl03c, sum(gcl07f*isnull(gcl18f, 0)), 0 -- from #tbl_gcm10_buf a -- where (gcl06i = 1 or gcl06i = 5) -- and (a.gcl82c between @fromdepart and @todepart or @fromdepart = '*') --add by lyj 2007-09-06 -- group by gcl03c, gcl82c -- -- insert #tbl_fill_card_dept(cardid, amt, quan) -- select gcl03c, sum(gcl07f*isnull(gcl20f, 0)), 0 -- from #tbl_gcm10_buf a -- where (gcl06i = 1 or gcl06i = 5) -- and (a.gcl83c between @fromdepart and @todepart or @fromdepart = '*') --add by lyj 2007-09-06 -- group by gcl03c, gcl83c insert #tbl_fill_card(cardid, amt, quan) select cardid, sum(amt), sum(quan) from #tbl_fill_card_dept group by cardid drop table #tbl_fill_card_dept drop table #tbl_gcm10_buf update a set a.amt = a.amt - isnull(b.amt, 0) from #tbl_item_group a, #tbl_fill_card b where a.itemid = b.cardid and a.flag = 2 --有些卡并没有卡销售,也应该加入 delete a from #tbl_fill_card a, #tbl_item_group b where a.cardid = b.itemid and b.flag = 2 insert #tbl_item_group(flag, itemid, quan, amt) select 2, cardid, quan, 0-amt from #tbl_fill_card update a set a.prjtype = b.gca02c from #tbl_item_group a, gcm01 b where a.itemid = b.gca01c and b.gca00c = @compid and a.flag = 2 --前面是按会员卡来分组统计的 delete #tbl_fill_card insert #tbl_fill_card(cardid, quan, amt) select prjtype, count(prjtype), sum(amt) from #tbl_item_group where flag = 2 and isnull(prjtype, '')<>'' group by prjtype delete #tbl_item_group where flag = 2 --按卡的类别分分组统计 insert #tbl_item_group(flag, prjtype, itemid, quan, amt) select 2, 'card', cardid, quan, amt from #tbl_fill_card update a set a.itemname = b.gda03c from #tbl_item_group a, gdm01 b where a.itemid = b.gda01c and b.gda00c = @compid and a.flag in (1,4,5) --lucz 2015.10.22 update a set a.itemname = b.gfa03c from #tbl_item_group a, gfm01 b where a.itemid = b.gfa01c and b.gfa00c = @compid and a.flag = 3 update a set a.itemname = b.gak02c from #tbl_item_group a, gam10 b where a.itemid = b.gak01c and b.gak00c = @compid and a.flag = 2 select idd, flag, isnull(prjtype,'') prjtype, isnull(itemid,'') itemid, isnull(itemname,'') itemname, isnull(quan,0) quan, isnull(amt,0) amt from #tbl_item_group order by flag, sort, itemid drop table #tbl_item_group drop table #tbl_fill_card end GO /****** Object: StoredProcedure [dbo].[upg_select_member_treatment_consume] Script Date: 06/12/2017 06:20:40 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_select_member_treatment_consume] ( @compid varchar(10),--公司编号 @fromdate_join varchar(8),--入会开始时间 @todate_join varchar(8),--入会结束时间 @fromdate_consume varchar(8),--消费开始日期 @todate_consume varchar(8),--消费结束时间 @cardtype varchar(5),--会员卡类别 @cardfrom varchar(20),--会员卡开始 @cardto varchar(20),--会员卡截止 @totalcashfrom float,--总现金开始 @totalcashto float,--总现金结束 @staff varchar(10),--员工编号 @lastconsumedate varchar(8),--最后消费时间 @totalconsumefrom float,--总消耗开始 @totalconsumeto float, --总消耗截止 @include int --是否包含有疗程的会员 1:包含 0:不包含 ) as begin create table #result ( compId varchar(10) not null,--公司编号 cardId varchar(20) not null,--会员卡号 cardType varchar(5) not null,--卡类型 cardTypeName varchar(40)null,--卡类型名称 mName varchar(300) null,--会员名称 mBirthday1 varchar(8) null,--会员生日 mBirthday2 varchar(10) null,--公历生日 mSex varchar(8) null,--性别 mMobile varchar(20) null,--手机号码 staff varchar(20) null,--负责员工 staffName varchar(20) null,--员工姓名 joinFromDate varchar(8) null,--开始日期 lastConsumeDate varchar(8) null,--最后消费时间 ljBuyAmt float null,--累计疗程购买金额 ljBuyCashAmt float null,--累计疗程现金购买金额 ljBuyStoreAmt float null,--累计疗程储值购买金额 ljCourseConsume float null,--累计疗程消耗金额 ljStoredSellAmt float null,--累计储值金额 ljStoredConsume float null,--累计储值消费 storedUnused float null,--储值账户剩余金额 courseUnused float null,--疗程账户剩余金额 sjCashConsume float null,--实际现金消费 totalConsumeAmt float null,--总消耗额度 toTheStoreCount int null, maxConsume float null,--单笔最高消费 totalConsume float null, totalCash float null, isinclude int null,--是否是疗程会员 ) if isnull(@fromdate_join,'')='' set @fromdate_join = '*' if isnull(@todate_join,'')='' set @todate_join = '*' if isnull(@fromdate_consume,'')='' set @fromdate_consume = '*' if isnull(@todate_consume,'')='' set @todate_consume = '*' if isnull(@cardtype,'')='' set @cardtype = '*' if isnull(@cardfrom,'')='' set @cardfrom = '*' if isnull(@cardto,'')='' set @cardto = '*' if isnull(@staff,'')='' set @staff = '*' if isnull(@lastconsumedate,'')='' set @lastconsumedate = '*' insert into #result(compId,cardId,cardType,cardTypeName,mName,mBirthday1,mBirthday2,mSex,mMobile,staff,staffName,joinFromDate) select gca00c,gca01c,gca02c,gak02c,gba03c,gba17d,gba21c,(case when gba14i = 1 then '男' else '女' end),gba08c,gba10c,haa02c,gca06d from gcm01 with(nolock) LEFT JOIN gbm01 with(nolock) on gca00c = gba00c and gca04c = gba01c LEFT JOIN gam10 with(nolock) on gca00c = gak00c and gca02c = gak01c LEFT JOIN ham01 with(nolock) on gba00c = haa00c and gba10c = haa01c where gca04c NOT LIKE '%散客%' and gca00c = @compid and gca08i in (4,5) and (@fromdate_join = '*' or gca06d >= @fromdate_join) and (@todate_join = '*' or gca06d <= @todate_join) and (@cardtype = '*' or gca02c = @cardtype) and (@cardfrom = '*' or gca01c >= @cardfrom) and (@cardto = '*' or gca01c <= @cardto) and (@staff = '*' or gba10c = @staff) create table #gcm20 -- 会员卡消费历史 ( gct00c varchar(10) not null, --门店编号 gct01i int not null, --序号 gct02c varchar(20) not null, --卡号 gct03c varchar(20) not null, --会员编号 gct04d varchar(8 ) not null, --日期 gct05c varchar(5) null, --异动标志,账户的异动方式 gct06c varchar(10) null, --代码的类别--1-卡销售 2-疗程销售 3-项目 4-产品销售 - 5卡充值 - 6疗程充值 gct07c varchar(20) null, --代码 gct08c varchar(100) null, --名称 gct09f float null, --标准单价 gct10f float null, --折扣单价 gct11f float null, --数量 gct12f float null, --价格 gct13c varchar(10) null, --单据类别 gct14c varchar(20) null, --单据单号 gct15c varchar(20) null, --员工1 gct16c varchar(20) null, --员工2 gct17c varchar(20) null, --员工3 gct18c varchar(20) null, --员工4 gct19t varchar(10) null, --时间 gct20c varchar(5) null, --支付方式 gct21f float null, --项目消费序号 primary key(gct00c,gct01i) ) insert into #gcm20(gct00c,gct01i,gct02c,gct03c,gct04d,gct05c,gct06c,gct07c,gct08c,gct09f,gct10f,gct11f,gct12f,gct13c,gct14c,gct15c,gct16c,gct17c,gct18c,gct19t,gct20c,gct21f) select gct00c,gct01i,gct02c,gct03c,gct04d,gct05c,gct06c,gct07c,gct08c,gct09f,gct10f,gct11f,gct12f,gct13c,gct14c,gct15c,gct16c,gct17c,gct18c,gct19t,gct20c,gct21f from gcm20 with(nolock) where (@fromdate_consume = '*' or gct04d>= @fromdate_consume) and (@todate_consume = '*' or gct04d <= @todate_consume) and (@cardfrom = '*' or gct02c >= @cardfrom) and (@cardto = '*' or gct02c <= @cardfrom) update a set a.lastConsumeDate = b.gct04d from #result a,(select max(gct04d) gct04d,gct02c from #gcm20 where gct13c = 'gx' GROUP BY gct02c) b where a.cardId = b.gct02c create table #gcm04_table ( compid varchar(10) not null, cardid varchar(20) not null, amt float null, billtype varchar(5) null, billid varchar(20) null ) insert into #gcm04_table(compid,cardid,amt,billtype,billid) select gcd00c,gcd01c,gcd06f,gcd07c,gcd08c from gcm04 where gcd02i = '4' and gcd05i = 0 and gcd00c = @compid and (gcd09d >= @fromdate_consume or @fromdate_consume = '*') and (gcd09d <= @todate_consume or @todate_consume = '*') union all select gcd00c,gcd01c,0-gcd06f,gcd07c,gcd08c from gcm04 where gcd02i = '4' and gcd05i = 1 and gcd00c = @compid and (gcd09d >= @fromdate_consume or @fromdate_consume = '*') and (gcd09d <= @todate_consume or @todate_consume = '*') update a set a.ljBuyAmt = isnull(b.amt,0) from #result a,(select cardid,sum(isnull(amt,0)) amt from #gcm04_table group by cardid) b where a.cardId = b.cardid create table #bill_detail ( compid varchar(10) not null, cardid varchar(20) not null, billtype varchar(5) null, billid varchar(20) null, paycode varchar(5) null, payamt float null ) insert into #bill_detail(compid,cardid,billtype,billid,paycode,payamt) select a.gct00c,a.gct02c,a.gct13c,a.gct14c,a.gct20c,isnull(a.gct12f,0) from gcm20 a with(nolock),(select cardid,billtype,billid from #gcm04_table group by cardid,billtype,billid)b where a.gct00c = @compid and a.gct02c = b.cardid and a.gct13c = b.billtype and a.gct14c = b.billid update a set a.ljBuyCashAmt = isnull(amt,0) from #result a,(select cardid,sum(isnull(payamt,0)) amt from #bill_detail where paycode = '1' group by cardid) b where a.cardId = b.cardid update a set a.ljBuyStoreAmt = isnull(amt,0) from #result a,(select cardid,sum(isnull(payamt,0)) amt from #bill_detail where paycode = '4' group by cardid) b where a.cardId = b.cardid update a set a.ljCourseConsume = isnull(b.gct12f,0) from #result a,(select gct02c,sum(isnull(gct12f,0))gct12f from #gcm20 where gct20c = '9' and gct13c = 'gx' group by gct02c) b where a.cardId = b.gct02c delete #gcm04_table insert into #gcm04_table(compid,cardid,amt,billtype,billid) select gcd00c,gcd01c,gcd06f,gcd07c,gcd08c from gcm04 where gcd02i = 2 and gcd05i = 0 and gcd00c = @compid and (gcd09d >= @fromdate_consume or @fromdate_consume = '*') and (gcd09d <= @todate_consume or @todate_consume = '*') union all select gcd00c,gcd01c,0-gcd06f,gcd07c,gcd08c from gcm04 where gcd02i = 2 and gcd05i = 1 and gcd00c = @compid and (gcd09d >= @fromdate_consume or @fromdate_consume = '*') and (gcd09d <= @todate_consume or @todate_consume = '*') update a set a.ljStoredSellAmt = isnull(b.amt,0) from #result a,(select cardid,sum(isnull(amt,0))amt from #gcm04_table group by cardid)b where a.cardId = b.cardid update a set a.ljStoredConsume = isnull(b.gct12f,0) from #result a,(select gct02c,sum(isnull(gct12f,0))gct12f from #gcm20 where gct20c = '4' and gct13c = 'gx' group by gct02c)b where a.cardId = b.gct02c update a set a.storedUnused = isnull(b.gcc06f,0) from #result a,(select gcc01c,isnull(gcc06f, 0)gcc06f from gcm03 where gcc00c = @compid and gcc03i = 2) b where a.cardId = b.gcc01c update a set a.courseUnused = isnull(b.gcc06f,0) from #result a,(select gcc01c,isnull(gcc06f, 0)gcc06f from gcm03 where gcc00c = @compid and gcc03i = 4) b where a.cardId = b.gcc01c update a set a.sjCashConsume = isnull(b.gct12f,0) from #result a,(select gct02c,sum(isnull(gct12f,0))gct12f from #gcm20 where gct20c in ('1', '6', 'wx', 'zfb') group by gct02c )b where a.cardId = b.gct02c update a set a.totalConsumeAmt = isnull(b.gct12f,0) from #result a,(select gct02c,sum(isnull(gct12f,0))gct12f from #gcm20 where gct13c = 'gx' and gct06c in('3','4') group by gct02c )b where a.cardId = b.gct02c create table #bills ( cardid varchar(20) not null, billid varchar(20) not null, amt float null, primary key(cardid,billid) ) declare @payment_0 varchar(10) declare @payment_3 varchar(10) declare @payment_$ varchar(10) declare @payment_u1 varchar(10), @payment_u2 varchar(10), @payment_u3 varchar(10), @payment_u4 varchar(10), @payment_u5 varchar(10) exec upg_isrevenue_payment @compid, @payment_0 output, @payment_3 output, @payment_$ output, @payment_u1 output, @payment_u2 output, @payment_u3 output, @payment_u4 output, @payment_u5 output insert into #bills(cardid,billid,amt) select gct02c,gct14c,sum(case when gct20c not in ('4','9','#') and (gct20c <'A' or gct20c > 'Z') and gct20c in('1', '6', 'ZFB', 'WX', @payment_0, @payment_3, @payment_$, @payment_u1, @payment_u2, @payment_u3, @payment_u4, @payment_u5) then gct12f else 0 end) from #gcm20 where gct13c = 'gx' group by gct02c,gct14c update a set a.toTheStoreCount = b.cnt, a.maxConsume = b.amt from #result a,(select count(*)cnt,max(isnull(amt,0)) amt,cardid from #bills group by cardid) b where a.cardId = b.cardid update a set a.totalConsume = isnull(b.gct12f,0) from #result a,(select gct02c,sum(isnull(gct12f,0)) gct12f from #gcm20 where gct13c = 'gx' group by gct02c)b where a.cardId = b.gct02c update a set a.totalCash = isnull(b.gct12f,0) from #result a,(select gct02c,sum(isnull(gct12f,0)) gct12f from #gcm20 where gct13c = 'gx' and gct20c in ('1', '6', 'zfb', 'wx') group by gct02c)b where a.cardId = b.gct02c update #result set isinclude = 1 from #result where exists(select 1 from gcm06 where gcf00c = @compid and gcf01c = cardId) update #result set isinclude = 0 from #result where isnull(isinclude,0) <> 1 if(@include=0) begin select * from #result where (@lastconsumedate = '*' or lastConsumeDate <= @lastconsumedate) and (isnull(@totalcashfrom,-999) = -999 or isnull(totalCash,0) >= @totalcashfrom) and (isnull(@totalcashto,-999) = -999 or isnull(totalCash,0) <= @totalcashto) and (isnull(@totalconsumefrom,-999) = -999 or isnull(totalConsume,0) >= @totalconsumefrom) and (isnull(@totalconsumeto,-999) = -999 or isnull(totalConsume,0) <= @totalconsumeto) and isinclude = 1 order by cardId end else begin select * from #result where (@lastconsumedate = '*' or lastConsumeDate <= @lastconsumedate) and (isnull(@totalcashfrom,-999) = -999 or isnull(totalCash,0) >= @totalcashfrom) and (isnull(@totalcashto,-999) = -999 or isnull(totalCash,0) <= @totalcashto) and (isnull(@totalconsumefrom,-999) = -999 or isnull(totalConsume,0) >= @totalconsumefrom) and (isnull(@totalconsumeto,-999) = -999 or isnull(totalConsume,0) <= @totalconsumeto) order by cardId end drop table #result drop table #gcm04_table drop table #bill_detail drop table #gcm20 drop table #bills end GO /****** Object: StoredProcedure [dbo].[upg_add_accounts] Script Date: 06/12/2017 06:19:23 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_add_accounts] ( @compid varchar(10), @acctnum int, @i int ) as begin declare @code varchar(10) create table #gsm02 ( gsb00c varchar(10) not null, --公司编号 gsb01c varchar(10) not null, -- 资料类别分类码(1-, 2-消费方式, gsb02c varchar(5) not null, -- 类别字段代码值 gsb03c varchar(40) null, -- 类别字段显示Text gsb04c varchar(40) not null, -- 资料类别分类码描述 gsb05i int null, -- 维护标志(0- user 不可见, 1- user可见但不可维护, 2- user可维护) gsb06i int null, -- 排序标志 0 不用 1, 2, 3, 4, 按次序显示 ) declare @PK_Sql nvarchar(max) set @PK_Sql = ' alter table #gsm02 add constraint PK_#gsm02_' + convert(varchar(10), cast(floor(rand() * 1000000) as int)) + ' primary key clustered(gsb00c, gsb01c, gsb02c)' exec sp_executesql @PK_Sql if @i <= 25 select @code = char(ascii('A') + @i) else if @i <= 51 select @code = 'A' + char(ascii('A') + @i-26) else if @i <= 77 select @code = 'B' + char(ascii('A') + @i-52) else if @i <= 103 select @code = 'C' + char(ascii('A') + @i-78) else select @code = 'D' + char(ascii('A') + @i-103) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i, gsb06i) values(@compid, '3', @code, '账户支付' + convert(varchar(10), @i), '收费方式', 2, 1) insert #gsm02(gsb00c, gsb01c, gsb02c, gsb03c, gsb04c, gsb05i, gsb06i) values(@compid, 'O', convert(varchar(10), 5 + @i), '新增账户' + convert(varchar(10), @i), '账户类别', 2, 1) delete a from #gsm02 a, gsm02 b where a.gsb00c = b.gsb00c and a.gsb01c = b.gsb01c and a.gsb02c = b.gsb02c insert gsm02 select * from #gsm02 drop table #gsm02 exec upg_init_acct2pay end GO /****** Object: StoredProcedure [dbo].[upg_reset_return_flag_card] Script Date: 06/12/2017 06:20:37 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_reset_return_flag_card](@cardid varchar(20)) as begin declare @compid varchar(10) declare @billid varchar(20) declare @billdate varchar(10) update a set a.ggc33d = '', a.ggc34c = '' from ggm03 a, ggm01 b where a.ggc00c = b.gga00c and a.ggc01c = b.gga01c and b.gga09c = @cardid update a set a.ggb41d = '', a.ggb42c = '' from ggm02 a, ggm01 b where a.ggb00c = b.gga00c and a.ggb01c = b.gga01c and b.gga09c = @cardid declare cur_each_gcm10_return cursor for select gcl00c, gcl01c, gcl80d from gcm10 with (nolock) where gcl03c = @cardid and gcl06i = 6 and gcl04c = '888' order by gcl80d open cur_each_gcm10_return fetch cur_each_gcm10_return into @compid, @billid, @billdate while @@fetch_status = 0 begin exec upg_set_return_flag @compid, @billid, @billdate, 1 fetch cur_each_gcm10_return into @compid, @billid, @billdate end close cur_each_gcm10_return deallocate cur_each_gcm10_return end GO /****** Object: StoredProcedure [dbo].[upg_personal_statistics_paymode] Script Date: 06/12/2017 06:20:26 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_personal_statistics_paymode] ( @compid varchar(10), -- 公司别 @fromdate varchar(10), -- 开始日期 @todate varchar(10), -- 截至日期 @fromempl varchar(20), -- 查询开始人员 @toempl varchar(20), -- 查询截至人员 @inc_card int, -- 卡销售 @inc_service int, -- 服务 @inc_goods int, -- 是否包括产品销售 @return_type int, @paymode varchar(20), @spid int ) -- return 数据的类型, 1-明细数据员工业绩统计报表 -- 2-每人一个提成数据, 用于计算员工薪资 -- 3-业绩排行-虚业绩, 4-业绩排行-营业金额 -- 5-业绩排行-实业绩 -- 7-每人按项目分组 6-总提成 as begin exec upg_personal_statistics_paymode_ex @compid, @fromdate, @todate, @fromempl, @toempl, @inc_card, @inc_service, @inc_goods, @return_type, @paymode, '*', @spid end GO /****** Object: StoredProcedure [dbo].[upg_combo_sale_detail] Script Date: 06/12/2017 06:19:42 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_combo_sale_detail] ( @compid varchar(10), @cardid varchar(20) ) as begin declare @cardcompid varchar(10) exec upg_get_card_compid @cardid, @cardcompid output -- 先找出所有的这个卡购买的套餐 create table #tbl_buy_detail ( compid varchar(10) not null, cardid varchar(20) not null, billtype varchar(20) not null, billid varchar(20) not null, billdate varchar(10) null, comboid varchar(20) null, discount float null, amt float null ) create table #tbl_buy_result ( compid varchar(10) not null, cardid varchar(20) not null, billtype varchar(20) not null, billid varchar(20) not null, billdate varchar(10) null, comboid varchar(20) null, amt float null, selected int null, rp_amt float null, item varchar(20) null, discount float null, remain_amt float null ) insert #tbl_buy_detail(compid, cardid, billtype, billid, billdate, comboid, amt, discount) select gna00c, @cardid, 'gn', gna01c, gna02d, gtf02c, gtf05f, gtf07f from gtc05 a, gnm01 b where a.gtf00c = b.gna00c and a.gtf01c = b.gna01c and b.gna13c = @cardid insert #tbl_buy_detail(compid, cardid, billtype, billid, billdate, comboid, amt, discount) select gcl00c, @cardid, 'gz', gcl01c, gcl02d, gth02c, gth05f , gth07f from gtc06 a, gcm10 b where a.gth00c = b.gcl00c and a.gth01c = b.gcl01c and b.gcl03c = @cardid -- 去掉没有套餐剩余的, 只保留有剩余的套餐的购买历史 insert #tbl_buy_result(compid, cardid, billtype, billid, billdate, comboid, amt, remain_amt, item, discount) select distinct compid, cardid, billtype, billid, billdate, comboid, amt, c.gtb04f * b.gte06f * a.discount, b.gte03c, a.discount from #tbl_buy_detail a, gtc04 b, gtc02 c where a.comboid = b.gte02c and c.gtb00c = b.gte00c and c.gtb01c = b.gte02c and c.gtb02c = b.gte03c and b.gte01c = @cardid and b.gte00c = @cardcompid and isnull(gte06f, 0) > 0 and gte03c = gte08c and b.gte10c = a.billtype and b.gte11c = a.billid insert #tbl_buy_result(compid, cardid, billtype, billid, billdate, comboid, amt, remain_amt, item, discount) select distinct compid, cardid, billtype, billid, billdate, comboid, amt, c.gtd04f * b.gtj06f * a.discount, b.gtj03c, a.discount from #tbl_buy_detail a, gtc08 b , gtc03 c where a.comboid = b.gtj02c and c.gtd00c = b.gtj00c and c.gtd01c = b.gtj02c and c.gtd02c = b.gtj03c and b.gtj01c = @cardid and b.gtj00c = @cardcompid and isnull(gtj06f, 0) > 0 and gtj03c = gtj07c and b.gtj10c = a.billtype and b.gtj11c = a.billid delete a from #tbl_buy_detail a, gtc08 b where a.comboid = b.gtj02c and b.gtj01c = @cardid and b.gtj00c = @cardcompid and isnull(gtj06f, 0) > 0 and gtj03c = gtj07c select distinct compid, cardid, comboid, billtype, billid, billdate, discount, amt, sum(remain_amt) remain_amt, selected, rp_amt, nname = b.gta02c, '' discord_amt from #tbl_buy_result a, gtc01 b where a.comboid = b.gta01c and b.gta00c = @cardcompid group by compid, cardid, billtype, billid, billdate, discount, comboid, billtype, billid, billdate, amt, selected, rp_amt, gta02c drop table #tbl_buy_detail drop table #tbl_buy_result end GO /****** Object: StoredProcedure [dbo].[upg_used_cureproc] Script Date: 06/12/2017 06:20:51 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_used_cureproc] ( @compid varchar(10), @cardno varchar(30), @proj varchar(30), @times float, @amount float, @confirm int ) as begin exec upg_used_cureproc_ex @compid, @cardno, @proj, @times, @amount, @confirm, '', '', '', -1 end GO /****** Object: StoredProcedure [dbo].[upg_dispatch_data_gdm01_single] Script Date: 06/12/2017 06:20:00 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_dispatch_data_gdm01_single] ( @compid varchar(10), @gda01c varchar(20) -- 项目编号 ) as begin create table #tbl_control ( compid varchar(10) not null, -- 儿子公司的代码 lvl int null, primary key(compid) ) insert #tbl_control(compid, lvl) select bran_id, control_lvl from b_perf a, basic_data_control b where parent_id = @compid and a.bran_id = b.compid and b.datatype = 'gdm01' and b.control_lvl = 1 -- 创建循环变量 declare @HasRecord int set @HasRecord = 1 declare @currentCompId varchar(10) declare @lvl int while (@HasRecord = 1) begin if exists (select 1 from #tbl_control) begin set @HasRecord = 1 select top 1 @currentCompId = compid, @lvl = lvl from #tbl_control if @lvl = 1 -- 严格按总部的 begin -- 先删除自己的,把总部分发下来 -- 强制模式都是先删除原来的 exec upg_m_dispatch_gdm01_single @compid, @currentCompId, 1, @gda01c insert #tbl_control(compid, lvl) select bran_id, control_lvl from b_perf a, basic_data_control b where parent_id = @currentCompId and a.bran_id = b.compid and b.datatype = 'gdm01' and b.control_lvl = 1 end else if @lvl = 2 begin exec upg_m_dispatch_gdm01_single @compid, @currentCompId, 0, @gda01c insert #tbl_control(compid, lvl) select bran_id, case when (control_lvl <= 2) then 2 else control_lvl end from b_perf a, basic_data_control b where parent_id = @currentCompId and a.bran_id = b.compid and b.datatype = 'gdm01' and b.control_lvl = 1 end -- 清除已处理的门店 delete #tbl_control where compid = @currentCompId end else begin set @HasRecord = 0 end end drop table #tbl_control end GO /****** Object: StoredProcedure [dbo].[upg_dispatch_data_gfm01_single] Script Date: 06/12/2017 06:20:00 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_dispatch_data_gfm01_single] ( @compid varchar(10), @gfa01c varchar(20) -- 产品编号 ) as begin create table #tbl_control ( compid varchar(10) not null, -- 儿子公司的代码 lvl int null, primary key(compid) ) insert #tbl_control(compid, lvl) select bran_id, control_lvl from b_perf a, basic_data_control b where parent_id = @compid and a.bran_id = b.compid and b.datatype = 'gfm01' and (control_lvl = 1 ) -- 创建循环变量 declare @HasRecord int set @HasRecord = 1 declare @currentCompId varchar(10) declare @lvl int while (@HasRecord = 1) begin if exists (select 1 from #tbl_control) begin set @HasRecord = 1 select top 1 @currentCompId = compid, @lvl = lvl from #tbl_control if @lvl = 1 -- 严格按总部的 begin -- 先删除自己的,把总部分发下来 -- 强制模式都是先删除原来的 exec upg_m_dispatch_gfm01_single @compid, @currentCompId, 1, @gfa01c insert #tbl_control(compid, lvl) select bran_id, control_lvl from b_perf a, basic_data_control b where parent_id = @currentCompId and a.bran_id = b.compid and b.datatype = 'gfm01' and (control_lvl = 1 ) end else if @lvl = 2 begin exec upg_m_dispatch_gfm01_single @compid, @currentCompId, 0, @gfa01c insert #tbl_control(compid, lvl) select bran_id, case when (control_lvl <= 2) then 2 else control_lvl end from b_perf a, basic_data_control b where parent_id = @currentCompId and a.bran_id = b.compid and b.datatype = 'gfm01' and (control_lvl = 1 ) end -- 清除已处理的门店 delete #tbl_control where compid = @currentCompId end else begin set @HasRecord = 0 end end drop table #tbl_control end GO /****** Object: StoredProcedure [dbo].[upg_dispatch_data_gtc01_single] Script Date: 06/12/2017 06:20:00 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_dispatch_data_gtc01_single] ( @compid varchar(10), @gta01c varchar(20) -- 套餐编号 ) as begin create table #tbl_control ( compid varchar(10) not null, -- 儿子公司的代码 lvl int null, primary key(compid) ) insert #tbl_control(compid, lvl) select bran_id, control_lvl from b_perf a, basic_data_control b where parent_id = @compid and a.bran_id = b.compid and b.datatype = 'gtc01' and b.control_lvl = 1 -- 创建循环变量 declare @HasRecord int set @HasRecord = 1 declare @currentCompId varchar(10) declare @lvl int while (@HasRecord = 1) begin if exists (select 1 from #tbl_control) begin set @HasRecord = 1 select top 1 @currentCompId = compid, @lvl = lvl from #tbl_control if @lvl = 1 -- 严格按总部的 begin -- 先删除自己的,把总部分发下来 -- 强制模式都是先删除原来的 exec upg_m_dispatch_gtc01_single @compid, @currentCompId, 1, @gta01c insert #tbl_control(compid, lvl) select bran_id, control_lvl from b_perf a, basic_data_control b where parent_id = @currentCompId and a.bran_id = b.compid and b.datatype = 'gtc01' and b.control_lvl = 1 end else if @lvl = 2 begin exec upg_m_dispatch_gtc01_single @compid, @currentCompId, 0, @gta01c insert #tbl_control(compid, lvl) select bran_id, case when (control_lvl <= 2) then 2 else control_lvl end from b_perf a, basic_data_control b where parent_id = @currentCompId and a.bran_id = b.compid and b.datatype = 'gtc01' and b.control_lvl = 1 end -- 清除已处理的门店 delete #tbl_control where compid = @currentCompId end else begin set @HasRecord = 0 end end drop table #tbl_control end GO /****** Object: StoredProcedure [dbo].[upg_new_comp_copy_from_parent] Script Date: 06/12/2017 06:20:24 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_new_comp_copy_from_parent] ( @comp varchar(10), @parent varchar(10) ) as begin begin transaction begin try exec upg_m_dispatch_gsm01 @parent, @comp, '1' exec upg_m_dispatch_gsm02 @parent, @comp, '1' exec upg_m_dispatch_gdm01 @parent, @comp, '1' exec upg_m_dispatch_hem01 @parent, @comp, '1' exec upg_m_dispatch_gfm01 @parent, @comp, '1' exec upg_m_dispatch_gam10 @parent, @comp, '1' exec upg_m_dispatch_gdm06 @parent, @comp, '1' exec upg_m_dispatch_gtc01 @parent, @comp, '1' exec upg_m_dispatch_gbm20 @parent, @comp, '1' exec upg_m_dispatch_gtc01 @parent, @comp, '1' exec upg_m_dispatch_gdm20 @parent, @comp, '1' exec upg_m_dispatch_gam04 @parent, @comp, '1' exec upg_m_dispatch_gzm05 @parent, @comp, '1' exec upg_m_dispatch_hbm01 @parent, @comp, '1' exec upg_m_dispatch_gxm02 @parent, @comp, '1' exec upg_m_dispatch_satisfaction @parent, @comp, '1' exec upg_m_dispatch_gdn01 @parent, @comp, '1' exec upg_m_dispatch_gfm05 @parent, @comp, '1' exec upg_m_dispatch_gsm15 @parent, @comp, '1' exec upg_m_dispatch_personprecept @parent, @comp, '1' exec upg_m_dispatch_accountspendlimit @parent, @comp, '1' exec upg_m_dispatch_gicm11 @parent, @comp, '1' exec upg_m_dispatch_gsm29_1 @parent, @comp, '1' exec upg_m_dispatch_gam28 @parent, @comp, '1' exec upg_m_dispatch_proc_set @parent, @comp, '1' exec upg_m_dispatch_proc_set_perf_comm @parent, @comp, '1' exec upg_m_dispatch_kam01 @parent, @comp, '1' exec upg_m_dispatch_gzm06 @parent, @comp, '1' exec upg_m_dispatch_gzm07 @parent, @comp, '1' exec upg_m_dispatch_appointment_perf_ratio @parent, @comp, '1' end try begin catch if @@TRANCOUNT > 0 begin rollback transaction declare @SP_Parameters varchar(max) set @SP_Parameters = '@comp = ' + isnull(@comp, '') + '@parent = ' + isnull(@parent, '') insert into SP_TranLog (SPName ,SPTime ,SPParameter) values ( 'upg_new_comp_copy_from_parent' ,getdate() ,@SP_Parameters) raiserror ('transaction is failed.', 16, 1) WITH NOWAIT end end catch if @@TRANCOUNT > 0 begin commit transaction end end GO /****** Object: StoredProcedure [dbo].[upg_m_dispatch_tbl_ex] Script Date: 06/12/2017 06:20:23 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_m_dispatch_tbl_ex] ( @fromcompid varchar(10), -- 从哪一个公司发布 @tblname varchar(20), @mode int, -- 1-所有儿子 2-直接的儿子 @mandatory varchar(2) -- 是否强制统一 ) as begin declare @tocompid varchar(10) declare @control_lvl int if @mode = 1 begin -- 复制到其下面的每个公司 declare cur_each_comp cursor for select gaz02c from gam26 where gaz01c = @fromcompid and gaz02c <> @fromcompid end else if @mode = 2 begin -- 复制到直接的儿子 declare cur_each_comp cursor for select bran_id from b_perf where parent_id = @fromcompid end open cur_each_comp fetch cur_each_comp into @tocompid while @@fetch_status = 0 begin -- 判断是否是独立的门店,独立的门店则不要 select @control_lvl = control_lvl from basic_data_control where compid = @tocompid and datatype = @tblname select @control_lvl = isnull(@control_lvl, 1) if @control_lvl = 3 begin print 'no change' end else begin if @tblname = 'gdm01' exec upg_m_dispatch_gdm01 @fromcompid, @tocompid, @mandatory else if @tblname = 'gfm01' exec upg_m_dispatch_gfm01 @fromcompid, @tocompid, @mandatory else if @tblname = 'gsm01' exec upg_m_dispatch_gsm01 @fromcompid, @tocompid, @mandatory else if @tblname = 'gsm02' exec upg_m_dispatch_gsm02 @fromcompid, @tocompid, @mandatory else if @tblname = 'gam10' exec upg_m_dispatch_gam10 @fromcompid, @tocompid, @mandatory else if @tblname = 'gtc01' exec upg_m_dispatch_gtc01 @fromcompid, @tocompid, @mandatory else if @tblname = 'hem01' exec upg_m_dispatch_hem01 @fromcompid, @tocompid, @mandatory else if @tblname = 'gdm06' exec upg_m_dispatch_gdm06 @fromcompid, @tocompid, @mandatory else if @tblname = 'gdm20' exec upg_m_dispatch_gdm20 @fromcompid, @tocompid, @mandatory else if @tblname = 'gfm05' exec upg_m_dispatch_gfm05 @fromcompid, @tocompid, @mandatory else if @tblname = 'gbm20' exec upg_m_dispatch_gbm20 @fromcompid, @tocompid, @mandatory else if @tblname = 'gam04' exec upg_m_dispatch_gam04 @fromcompid, @tocompid, @mandatory else if @tblname = 'gzm05' exec upg_m_dispatch_gzm05 @fromcompid, @tocompid, @mandatory else if @tblname = 'hbm01' exec upg_m_dispatch_hbm01 @fromcompid, @tocompid, @mandatory else if @tblname = 'gxm02' exec upg_m_dispatch_gxm02 @fromcompid, @tocompid, @mandatory else if @tblname = 'gsm15' exec upg_m_dispatch_gsm15 @fromcompid, @tocompid, @mandatory else if @tblname = 'satisfaction' exec upg_m_dispatch_satisfaction @fromcompid, @tocompid, @mandatory else if @tblname = 'gdn01' exec upg_m_dispatch_gdn01 @fromcompid, @tocompid, @mandatory else if @tblname = 'personprecept' exec upg_m_dispatch_personprecept @fromcompid, @tocompid, @mandatory else if @tblname = 'accountspendlimit' exec upg_m_dispatch_accountspendlimit @fromcompid, @tocompid, @mandatory else if @tblname = 'gicm11' exec upg_m_dispatch_gicm11 @fromcompid, @tocompid, @mandatory else if @tblname = 'gsm29_1' exec upg_m_dispatch_gsm29_1 @fromcompid, @tocompid, @mandatory else if @tblname = 'gam28' exec upg_m_dispatch_gam28 @fromcompid, @tocompid, @mandatory else if @tblname = 'proc_set' exec upg_m_dispatch_proc_set @fromcompid, @tocompid, @mandatory else if @tblname = 'proc_set_perf_comm' exec upg_m_dispatch_proc_set_perf_comm @fromcompid, @tocompid, @mandatory else if @tblname = 'appoint_perf_ratio' exec upg_m_dispatch_appointment_perf_ratio @fromcompid, @tocompid, @mandatory else if @tblname = 'pay_classify' exec upg_m_dispatch_pay_classify @fromcompid, @tocompid, @mandatory else if @tblname = 'kam01' exec upg_m_dispatch_kam01 @fromcompid, @tocompid, @mandatory else if @tblname = 'gzm06' exec upg_m_dispatch_gzm06 @fromcompid, @tocompid, @mandatory else if @tblname = 'gzm07' exec upg_m_dispatch_gzm07 @fromcompid, @tocompid, @mandatory end fetch cur_each_comp into @tocompid end close cur_each_comp deallocate cur_each_comp end GO /****** Object: StoredProcedure [dbo].[upg_m_dispatch_table_single] Script Date: 06/12/2017 06:20:22 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_m_dispatch_table_single] ( @fromcompid varchar(10), -- 发布数据的公司 @tblname varchar(20), -- 要发布的table name @tocompid varchar(10), @mandatory int ) as begin if @tblname = 'gsm02' exec upg_m_dispatch_gsm02 @fromcompid, @tocompid, @mandatory else if @tblname = 'gdm01' exec upg_m_dispatch_gdm01 @fromcompid, @tocompid, @mandatory else if @tblname = 'hem01' exec upg_m_dispatch_hem01 @fromcompid, @tocompid, @mandatory else if @tblname = 'gfm01' exec upg_m_dispatch_gfm01 @fromcompid, @tocompid, @mandatory else if @tblname = 'gfm05' exec upg_m_dispatch_gfm05 @fromcompid, @tocompid, @mandatory else if @tblname = 'gsm01' exec upg_m_dispatch_gsm01 @fromcompid, @tocompid, @mandatory else if @tblname = 'gam10' exec upg_m_dispatch_gam10 @fromcompid, @tocompid, @mandatory else if @tblname = 'gdm06' exec upg_m_dispatch_gdm06 @fromcompid, @tocompid, @mandatory else if @tblname = 'gbm20' exec upg_m_dispatch_gbm20 @fromcompid, @tocompid, @mandatory else if @tblname = 'gtc01' exec upg_m_dispatch_gtc01 @fromcompid, @tocompid, @mandatory else if @tblname = 'gdm20' exec upg_m_dispatch_gdm20 @fromcompid, @tocompid, @mandatory else if @tblname = 'gam04' exec upg_m_dispatch_gam04 @fromcompid, @tocompid, @mandatory else if @tblname = 'gzm05' exec upg_m_dispatch_gzm05 @fromcompid, @tocompid, @mandatory else if @tblname = 'hbm01' exec upg_m_dispatch_hbm01 @fromcompid, @tocompid, @mandatory else if @tblname = 'gxm02' exec upg_m_dispatch_gxm02 @fromcompid, @tocompid, @mandatory else if @tblname = 'gsm15' exec upg_m_dispatch_gsm15 @fromcompid, @tocompid, @mandatory else if @tblname = 'satisfaction' exec upg_m_dispatch_satisfaction @fromcompid, @tocompid, @mandatory else if @tblname = 'gdn01' exec upg_m_dispatch_gdn01 @fromcompid, @tocompid, @mandatory else if @tblname = 'personprecept' exec upg_m_dispatch_personprecept @fromcompid, @tocompid, @mandatory else if @tblname = 'accountspendlimit' exec upg_m_dispatch_accountspendlimit @fromcompid, @tocompid, @mandatory else if @tblname = 'gicm11' exec upg_m_dispatch_gicm11 @fromcompid, @tocompid, @mandatory else if @tblname = 'gsm29_1' exec upg_m_dispatch_gsm29_1 @fromcompid, @tocompid, @mandatory else if @tblname = 'gam28' exec upg_m_dispatch_gam28 @fromcompid, @tocompid, @mandatory else if @tblname = 'proc_set' exec upg_m_dispatch_proc_set @fromcompid, @tocompid, @mandatory else if @tblname = 'proc_set_perf_comm' exec upg_m_dispatch_proc_set_perf_comm @fromcompid, @tocompid, @mandatory else if @tblname = 'appoint_perf_ratio' exec upg_m_dispatch_appointment_perf_ratio @fromcompid, @tocompid, @mandatory else if @tblname = 'pay_classify' exec upg_m_dispatch_pay_classify @fromcompid, @tocompid, @mandatory else if @tblname = 'kam01' exec upg_m_dispatch_kam01 @fromcompid, @tocompid, @mandatory else if @tblname = 'gzm06' exec upg_m_dispatch_gzm06 @fromcompid, @tocompid, @mandatory else if @tblname = 'gzm07' exec upg_m_dispatch_gzm07 @fromcompid, @tocompid, @mandatory end GO /****** Object: StoredProcedure [dbo].[upg_change_card_comp] Script Date: 06/12/2017 06:19:38 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_change_card_comp] ( @fromcompid varchar(10), @cardid varchar(20), @tocompid varchar(10) ) as begin --如果不是自己的会员卡,则不允许做任何修改 declare @memid varchar(20) if not exists(select 1 from gcm01 with (nolock) where gca00c = gca13d and gca00c = @fromcompid and gca01c = @cardid) return --更改归属公司的时候,记录当时的卡数据 exec upg_log_copy_gcm0306 @fromcompid, @cardid, '改归属' --把会员编号取到 select @memid = isnull(gca04c, '') from gcm01 with (nolock) where gca00c = gca13d and gca00c = @fromcompid and gca01c = @cardid --把操作历史更新到gcm02中 declare @memo varchar(40) declare @flowno int select @flowno = isnull(max(gcb02f), 0) + 1 from gcm02 where gcb01c = @cardid --取得操作日期与时间 declare @curdate varchar(20), @curtime varchar(20) exec upg_getcurrdate @curdate output, @curtime output select @memo = '从' + @fromcompid + '转移到' + @tocompid insert gcm02(gcb00c, gcb01c, gcb02f, gcb03i, gcb04c, gcb07d, gcb08c) values(@tocompid, @cardid, @flowno, 0, '改归属', @curdate, @memo) delete gcm01 where gca00c = @tocompid and gca01c = @cardid update gcm01 set gca00c = @tocompid, gca13d = @tocompid where gca00c = gca13d and gca00c = @fromcompid and gca01c = @cardid --卡的余额也要调整过来,卡的疗程,卡的套餐,都应该调整到归属公司 delete gcm03 where gcc00c = @tocompid and gcc01c = @cardid update gcm03 set gcc00c = @tocompid where gcc00c = @fromcompid and gcc01c = @cardid delete gcm06 where gcf00c = @tocompid and gcf01c = @cardid update gcm06 set gcf00c = @tocompid where gcf00c = @fromcompid and gcf01c = @cardid delete gtc04 where gte00c = @tocompid and gte01c = @cardid update gtc04 set gte00c = @tocompid where gte00c = @fromcompid and gte01c = @cardid delete gtc08 where gtj00c = @tocompid and gtj01c = @cardid update gtc08 set gtj00c = @tocompid where gtj00c = @fromcompid and gtj01c = @cardid --开帐资料 update gcm12 set gcn00c = @tocompid where gcn00c = @fromcompid and gcn01c = @cardid update gcm16 set gcr00c = @tocompid where gcr00c = @fromcompid and gcr01c = @cardid --会员资料也要更新过来 --先把押金资料转移过来 delete gbm15 where gbl00c<>@fromcompid and gbl01c = @memid update gbm15 set gbl00c = @tocompid where gbl00c = @fromcompid and gbl01c = @memid --先删除目标公司的会员基本资料 --只保留一份会员资料 delete gbm01 where gba00c<>@fromcompid and gba23c = @cardid delete gbm02 where gbb00c<>@fromcompid and gbb01c = @memid delete gbm03 where gbc00c<>@fromcompid and gbc01c = @memid delete gbm04 where gbd00c<>@fromcompid and gbd01c = @memid delete gbm05 where gbe00c<>@fromcompid and gbe01c = @memid delete gbm06 where gbf00c<>@fromcompid and gbf01c = @memid delete gbm07 where gbg00c<>@fromcompid and gbg01c = @memid delete gbm08 where gbh00c<>@fromcompid and gbh01c = @memid delete gbm09 where gbi00c<>@fromcompid and gbi01c = @memid delete gbm13 where gbm00c<>@fromcompid and gbm01c = @memid delete gbm14 where gbn00c<>@fromcompid and gbn01c = @memid update gbm01 set gba00c = @tocompid where gba23c = @cardid and gba00c = @fromcompid update gbm02 set gbb00c = @tocompid where gbb01c = @memid and gbb00c = @fromcompid update gbm03 set gbc00c = @tocompid where gbc01c = @memid and gbc00c = @fromcompid update gbm04 set gbd00c = @tocompid where gbd01c = @memid and gbd00c = @fromcompid update gbm05 set gbe00c = @tocompid where gbe01c = @memid and gbe00c = @fromcompid update gbm06 set gbf00c = @tocompid where gbf01c = @memid and gbf00c = @fromcompid update gbm07 set gbg00c = @tocompid where gbg01c = @memid and gbg00c = @fromcompid update gbm08 set gbh00c = @tocompid where gbh01c = @memid and gbh00c = @fromcompid update gbm09 set gbi00c = @tocompid where gbi01c = @memid and gbi00c = @fromcompid update gbm13 set gbm00c = @tocompid where gbm01c = @memid and gbm00c = @fromcompid update gbm14 set gbn00c = @tocompid where gbn01c = @memid and gbn00c = @fromcompid end GO /****** Object: StoredProcedure [dbo].[upg_cal_fill_card_detail_perf] Script Date: 06/12/2017 06:19:32 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_cal_fill_card_detail_perf] ( @compid varchar(10), -- 公司编号 @billtype varchar(5), -- 单据类别 'gn' = 会员卡销售 'gz' = 会员帐户管理 @billid varchar(20) -- 单号 ) as begin declare @personid varchar(20) declare @code varchar(20) declare @cardtype varchar(20) declare @payamt float declare @times float declare @procid float declare @filltype int declare @share_rate float declare @paycode varchar(20) declare @flag int declare @perf float declare @trueperf float declare @comm float declare @pctimes float declare cur_each_detail cursor for select personid, code, cardtype, payamt, times, filltype, share_rate, paycode, procid, pctimes from card_fill_detail_by_payment where compid = @compid and billtype = @billtype and billid = @billid for update of perf, comm open cur_each_detail fetch cur_each_detail into @personid, @code, @cardtype, @payamt, @times, @filltype, @share_rate, @paycode, @procid, @pctimes while @@fetch_status = 0 begin if @filltype = 1 -- 卡销售或充值 begin if @billtype = 'gn' select @flag = 1 else if @billtype = 'gz' select @flag = 0 end else begin select @flag = @filltype end -- 计算业绩 exec upg_cal_cardfill_perf @compid, @personid, @code, @cardtype, @payamt, @payamt, @times, @pctimes, @procid, @flag, @share_rate, @paycode, @billtype, @billid, @perf output -- 计算提成 exec upg_cal_cardsale_comm @compid, @cardtype, @code, @payamt, @payamt, @perf, @times, @pctimes, @procid, @personid, @flag, @comm output, @trueperf output, @share_rate, @paycode--aad by lm 2016.01.22(加入疗程次数) update card_fill_detail_by_payment set perf = @perf, comm = @comm where current of cur_each_detail set @perf = 0 --add by lm 2016.01.22 set @comm = 0 fetch cur_each_detail into @personid, @code, @cardtype, @payamt, @times, @filltype, @share_rate, @paycode, @procid, @pctimes end close cur_each_detail deallocate cur_each_detail end GO /****** Object: StoredProcedure [dbo].[upg_init_comp] Script Date: 06/12/2017 06:20:13 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_init_comp] ( @compid varchar(10), -- 公司别 @inc_demo int -- 1:需要Demo资料 0:不需要demo资料 ) as begin ---------------------------updated by cyr-------------------------------------- if not exists(select 1 from gam05 where gae01c = @compid) begin insert into gam05(gae01c, gae02c, gae03c, gae15c) values(@compid, 'S3博卡公司', 'S3博卡', '1') exec upg_repair_gym_param @compid exec upg_init_gam10 @compid exec upg_insert_gym_commdata @compid if @inc_demo = 1 begin exec upg_init_demodata @compid end if not exists(select 1 from gam26 where gaz01c = @compid and gaz02c = @compid) begin insert gam26(gaz01c, gaz02c) values(@compid, @compid) if (@compid <> '001') begin insert gam26(gaz01c, gaz02c) values('001', @compid) end end end end GO /****** Object: StoredProcedure [dbo].[upg_total_perf_comp] Script Date: 06/12/2017 06:20:47 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_total_perf_comp] ( @compid varchar(10), -- 公司别 @staff varchar(20), -- 员工编码 @fromdate_1 varchar(8), -- 开始日期 @todate_1 varchar(8), -- 截止日期 @fromdate_2 varchar(8), -- 开始日期 @todate_2 varchar(8), -- 截止日期 @fromdate_3 varchar(8), -- 开始日期 @todate_3 varchar(8) -- 截止日期 ) as begin declare @card_sale float -- 卡销售 declare @card_fill float -- 卡充值 declare @srv_cash float -- 服务项目现金,当人的时候就是业绩 declare @srv_card float -- 服务项目用卡 create table #result ( id int identity not null, during varchar(20) null, fromdate varchar(8) null, todate varchar(8) null, card_sale float null, card_fill float null, srv_cash float null, srv_card float null, primary key(id) ) exec upg_total_perf @compid, @staff, @fromdate_1, @todate_1, @card_sale output, @card_fill output, @srv_cash output, @srv_card output insert #result(during, fromdate, todate, card_sale, card_fill, srv_cash, srv_card) values('区段一', @fromdate_1, @todate_1, @card_sale, @card_fill, @srv_cash, @srv_card) exec upg_total_perf @compid, @staff, @fromdate_2, @todate_2, @card_sale output, @card_fill output, @srv_cash output, @srv_card output insert #result(during, fromdate, todate, card_sale, card_fill, srv_cash, srv_card) values('区段二', @fromdate_2, @todate_2, @card_sale, @card_fill, @srv_cash, @srv_card) exec upg_total_perf @compid, @staff, @fromdate_3, @todate_3, @card_sale output, @card_fill output, @srv_cash output, @srv_card output insert #result(during, fromdate, todate, card_sale, card_fill, srv_cash, srv_card) values('区段三', @fromdate_3, @todate_3, @card_sale, @card_fill, @srv_cash, @srv_card) select id, isnull(during,'') during, isnull(fromdate,'') fromdate, isnull(todate,'') todate, isnull(card_sale,'') card_sale, isnull(card_fill,'') card_fill, isnull(srv_cash,'') srv_cash, isnull(srv_card,'') srv_card from #result drop table #result end GO /****** Object: StoredProcedure [dbo].[upg_ghm01_confirm_out_check] Script Date: 06/12/2017 06:20:11 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_ghm01_confirm_out_check] ( @compid varchar(10), -- 公司编号 @billno varchar(100), -- 出库单号 @billdate varchar(8), -- 单据日期 @storageno varchar(4), -- 仓库编号 @checkman varchar(10), -- 复核人 @currdate varchar(8), -- 复核日期 @curtime varchar(20) -- 复核时间 ) as begin set nocount on -- 当前的事务数 declare @__transaction_count int set @__transaction_count = @@trancount -- try...catch处理 begin try -- a.开启事务,或者设置事务保存点 if @__transaction_count = 0 begin tran else save tran __tran_SavePoint declare @__sp_re int -- update 库存日异动统计表头档 insert gfm03(gfc00c, gfc01c, gfc02c, gfc03c, gfc040c, gfc041c, gfc05c, gfc06i, gfc08i, gfc07c, gfc09t) select gia00c, '2', gia01c, gia03d, '3', gia04c, gia04c, gia07i, gia09i, gia05c, gia08t from gim01 with (nolock) where gia00c = @compid and gia01c = @billno -- update 库存日异动统计明细档 insert gfm04(gfd00c, gfd01c, gfd02c, gfd03f, gfd04c, gfd05f, gfd06f, gfd07c, gfd08c, gfd11c, gfd12f, gfd13f, gfd14i, gfd15c) select gib00c, '2', gib01c, gib02f, gib03c, gib08f, gib11f, @billdate, @storageno, gib06c, isnull(gib07f, 0), isnull(gib10f, 0), gia09i, gia05c from gim02 b with (nolock), gim01 a with (nolock) where gib00c = @compid and gib01c = @billno and isnull(gib08f, 0) <> 0 and a.gia00c = b.gib00c and a.gia01c = b.gib01c update gf set gf.gfb06f = isnull(gf.gfb06f, 0) + isnull(gi.gib08f, 0) from gfm02 gf, gim02 gi where gi.gib00c = @compid and gi.gib01c = @billno and gfb00c = gib00c and gib03c = gfb01c and gfb02c = substring(@billdate, 1, 6) and gfb04c = @storageno and gfb03c = '2' insert into gfm02(gfb00c, gfb01c, gfb02c, gfb03c, gfb04c, gfb06f) select gib00c, gib03c, substring(@billdate, 1, 6), '2', @storageno, isnull(gib08f, 0) from gim02 with (nolock) where gib00c = @compid and gib01c = @billno and gib03c not in (select gfb01c from gfm02 where gfb00c = gib00c and gfb01c = gib03c and gfb02c = substring(@billdate, 1, 6) and gfb03c = '2' and gfb04c = @storageno ) --update 产品出库单 update gim01 set gia93c = @checkman, gia94d = @currdate where gia00c = @compid and gia01c = @billno --begin update ghm03 set ghc05f = isnull(ghc05f, 0) - isnull(gic04f, 0) from ghm03, gim03 where ghc00c = gic00c and ghc01c = gic02c and ghc02c = gic03c and ghc00c = @compid and gic01c = @billno and ghc06c = @storageno --end --记录日志 insert sysoperationlog(userid, program, operation, operationdate, operationtime, origatedate, compid, keyvalue1, keyvalue2, keyvalue3) values(@checkman, 'GWA03I', 'M', @currdate, @curtime, @currdate, @compid, @billno, @storageno, '复核') --出库作业,如果是门店,则需要自动新增门店入库单 --Added by WUXM, 2006/04/23 declare @gia09i int declare @gia05c varchar(20) declare @new_billid varchar(100) declare @sp060f float declare @gia07i int --出库性质 declare @gib12c varchar(20) --供应商代号 add by leiji on 2012年10月15日 select @gia09i = gia09i, @gia05c = gia05c, @gia07i = gia07i, @gib12c = gib12c from gim01, gim02 where gia00c = @compid and gia01c = @billno and gia00c = gib00c and gib01c = gia01c if @gia09i = 2 begin exec @__sp_re = upg_auto_po_from_center @compid, @billno, @billno, @gib12c, @gia07i, @gia05c if @__sp_re <> 0 raiserror('has some error', 16, 1) -- 根据系统设定来决定是否自动复合 select @sp060f = gsa03f from gsm01 with (nolock) where gsa00c = @compid and gsa01c = 'SP060' if @sp060f <> 2 begin select @new_billid = @billno + '(' + @compid + '-' + @gia05c + ')' exec @__sp_re = upg_ghm01_confirm_in_check @gia05c, @new_billid, @billdate, @storageno, @checkman, @currdate, @curtime end end if @__sp_re <> 0 raiserror('has some error', 16, 1) -- 提交事务 -- 有可提交的事务, 并且事务是在当前模块中开启的情况下, 才提交事务 if xact_state() = 1 and @__transaction_count = 0 commit -- 为了防止TRY 中有遗漏的事务处理, 可以在TRY 模拟的结束部分做最终的判断 if @__transaction_count = 0 begin if xact_state() = -1 rollback tran else begin while @@trancount > 0 commit tran end end end try begin catch -- 在CATCH 模块, 应该首先处理事务 if xact_state() <> 0 begin if @__transaction_count = 0 rollback tran -- xact_state 为-1 时, 不能回滚到事务保存点, 这种情况留给外层调用者做统一的事务回滚 -- 通过@@trancount > @__transaction_count 的判断, 即使在TRY 模块中没有设置事务保存点的情况下跳到此步骤, 也不会出错 else if xact_state() = 1 and @@trancount > @__transaction_count rollback tran __tran_SavePoint end declare @SP_Parameters varchar(max) set @SP_Parameters = '@compid = ' + isnull(@compid, '') + '@billno = ' + isnull(@billno, '') + '@billdate = ' + isnull(@billdate, '') + '@storageno = ' + isnull(@storageno, '') + '@checkman = ' + isnull(@checkman, '') + '@currdate = ' + isnull(@currdate, '') + '@curtime = ' + isnull(@curtime, '') insert into SP_TranLog(SPName, SPTime, SPParameter) values('upg_ghm01_confirm_out_check', getdate(), @SP_Parameters) return -1 end catch end GO /****** Object: StoredProcedure [dbo].[upg_reinit_gcm16_nohistory] Script Date: 06/12/2017 06:20:34 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_reinit_gcm16_nohistory] ( @compid varchar(10), -- 公司别 @cardid varchar(20) -- 会员卡号 ) as begin exec upg_log_copy_gcm0306 @compid, @cardid, 'reopen' exec upg_create_card_relation @compid, @cardid declare @totalamt float -- 疗程总余额 declare @procacct int -- 疗程账户 select @procacct = 4 --因为是重新开帐,可能一些是前面已经开帐过的,而且也可能被消费过了。所以需要保持原来的疗程序号不要变 --所以最好是update, 其中另外一些是新输入的,只要insert就可以了,为了方便处理,先把gcm16缓冲一下,到另外一个临时表中 create table #gcm16_buf -- 会员卡开账--疗程 ( gcr00c varchar(10) not null, --公司编号 gcr01c varchar(20) not null, --卡号 gcr02c varchar(20) not null, --项目编号 gcr03f float null, --次数 gcr04f float null, --赠送次数 gcr05f float null, --总次数 gcr06f float null, --已经使用次数 gcr07f float null, --剩余次数 gcr08f float null, --疗程金额 gcr09f float null, --已经使用金额 gcr10f float null, --剩余金额 gcr11d varchar(8) null, --账户开立日 gcr12d varchar(8) null, --账户截至日 gcr13i int null, --疗程间隔 gcr14f float null, --一个月可使用的次数 0--代表不控制 直到这个疗程用完其他float就是一个月可以使用的次数 gcr15c varchar(60) null, --输入的时候的备注 gcr16i int null, --是否已经审核 gcr17i int not null, --价格序号 gcr18f float not null, --折扣 gcr19i int null, --是否停用 2停用 gcr20i int null, --标志 是否前期卡 gcr21i int null, --是否赠送 gcr22i int null, --永久免费 gcr23i int not null, --序号 gcr24d varchar(10) null, --输入日期 gcr25c varchar(20) null, --输入人 gcr26i int null, --是否已经开帐 gcr31i int null, --是否需要开帐 gcr32i int null, -- gcr33c varchar(20) null, --复核人 gcr34c varchar(10) null, --复核日期 gcr35c varchar(10) null, --复核时间 gcr36f float null) --单次费 insert #gcm16_buf(gcr00c, gcr01c, gcr02c, gcr03f, gcr04f, gcr05f, gcr06f, gcr07f, gcr08f, gcr09f, gcr10f, gcr11d, gcr12d, gcr13i, gcr14f, gcr15c, gcr16i, gcr17i, gcr18f, gcr19i, gcr20i, gcr21i, gcr22i, gcr23i, gcr24d, gcr25c, gcr26i, gcr31i, gcr32i, gcr33c, gcr34c, gcr35c, gcr36f) select gcr00c, gcr01c, gcr02c, gcr03f, gcr04f, gcr05f, gcr06f, gcr07f, gcr08f, gcr09f, gcr10f, gcr11d, gcr12d, gcr13i, gcr14f, gcr15c, gcr16i, gcr17i, gcr18f, gcr19i, gcr20i, gcr21i, gcr22i, gcr23i, gcr24d, gcr25c, gcr26i, gcr31i, gcr32i, gcr33c, gcr34c, gcr35c, gcr36f from gcm16 where gcr00c = @compid and gcr01c = @cardid -- 把已经开张的并却已经更新过去的删除 delete b from gcm06 a, #gcm16_buf b where a.gcf00c = @compid and a.gcf01c = @cardid and isnull(a.gcf24i, 0) = b.gcr23i and a.gcf02c = b.gcr02c -- 把剩下的插入到gcm06中 -- 先删除是前期卡,而且开帐的序号,而且也没有消费过 -- 第一次开帐的时候存在,但后来删除了的,特征,有开帐序号,但无法连接到 -- 不考虑在gcm16中删除了 insert gcm06(gcf00c, gcf01c, gcf02c, gcf03f, gcf04f, gcf05f, gcf06f, gcf07f, gcf08f, gcf09f, gcf10f, gcf11d, gcf12d, gcf13i, gcf14f, gcf15c, gcf16i, gcf17i, gcf18f, gcf19i, gcf20i, gcf21i, gcf22i, gcf24i) select gcr00c, gcr01c, gcr02c, gcr03f, gcr04f, gcr05f, gcr06f, gcr07f, gcr08f, gcr09f, gcr10f, gcr11d, gcr12d, gcr13i, gcr14f, gcr15c, gcr16i, gcr17i, gcr18f, isnull(gcr19i, 1), isnull(gcr20i, 1), isnull(gcr21i, 0), isnull(gcr22i, 0), gcr23i from #gcm16_buf where gcr00c = @compid and gcr01c = @cardid order by gcr24d drop table #gcm16_buf update gcm16 set gcr26i = 1 where gcr00c = @compid and gcr01c = @cardid -- 更新gcm03中疗程的余额 select @totalamt = sum(gcf10f) from gcm06 with (nolock) where gcf00c = @compid and gcf01c = @cardid update gcm03 set gcc06f = @totalamt where gcc00c = @compid and gcc01c = @cardid and gcc03i = @procacct end GO /****** Object: StoredProcedure [dbo].[upg_reinit_gcm16] Script Date: 06/12/2017 06:20:34 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_reinit_gcm16]( @compid varchar(10), --公司别 @cardid varchar(20)) -- 会员卡号 as begin --先建立可能丢失的连接 exec upg_log_copy_gcm0306 @compid, @cardid, 'reopen' exec upg_create_card_relation @compid, @cardid declare @totalamt float -- 疗程总余额 declare @procacct int -- 疗程账户 select @procacct = 4 --把已经消费的次数与金额统计出来 create table #tbl_card_proc_used( cardid varchar(20) not null, --卡号 procid varchar(20) not null, --疗程项目 procseq int not null, num float null, amt float null) --可能消费历史不完整, 这个地方统计就出问题了 insert #tbl_card_proc_used(cardid, procid, procseq, num, amt) select gch03c, gch04c, gch16i, sum(gch08f), sum(gch09f) from gcm08 with (nolock) where gch03c = @cardid and (gch10c = 'gx' or gch10c = 'pj') and isnull(gch16i, 0)>0 group by gch03c, gch04c, gch16i --更新两个地方都存在的, 重新覆盖次数以及金额 --关联的疗程重新开账,后面需要考虑这些疗程的消费 update a set gcf03f = gcr03f, gcf04f = gcr04f, gcf05f = gcr05f, gcf06f = gcr06f, gcf07f = gcr07f, gcf08f = gcr08f, gcf09f = gcr09f, gcf10f = gcr10f, gcf21i = gcr21i, gcf15c = gcr15c from gcm06 a, gcm16 b where a.gcf00c = b.gcr00c and a.gcf01c = b.gcr01c and a.gcf02c = b.gcr02c -- 项目 and isnull(a.gcf24i, 0) = b.gcr23i -- 开账序号 and a.gcf00c = @compid and a.gcf01c = @cardid --因为是重新开帐,可能一些是前面已经开帐过的,而且也可能被消费过了。所以需要保持原来的疗程序号不要变 --所以最好是update, 其中另外一些是新输入的,只要insert就可以了,为了方便处理,先把gcm16缓冲一下,到另外一个临时表中 create table #gcm16_buf -- 会员卡开账--疗程 ( gcr00c varchar(10) not null, --公司编号 gcr01c varchar(20) not null, --卡号 gcr02c varchar(20) not null, --项目编号 gcr03f float null, --次数 gcr04f float null, --赠送次数 gcr05f float null, --总次数 gcr06f float null, --已经使用次数 gcr07f float null, --剩余次数 gcr08f float null, --疗程金额 gcr09f float null, --已经使用金额 gcr10f float null, --剩余金额 gcr11d varchar(8) null, --账户开立日 gcr12d varchar(8) null, --账户截至日 gcr13i int null, --疗程间隔 gcr14f float null, --一个月可使用的次数 0--代表不控制 直到这个疗程用完其他float就是一个月可以使用的次数 gcr15c varchar(60) null, --输入的时候的备注 gcr16i int null, --是否已经审核 gcr17i int not null, --价格序号 gcr18f float not null, --折扣 gcr19i int null, --是否停用 2停用 gcr20i int null, --标志 是否前期卡 gcr21i int null, --是否赠送 gcr22i int null, --永久免费 gcr23i int not null, --序号 gcr24d varchar(10) null, --输入日期 gcr25c varchar(20) null, --输入人 gcr26i int null, --是否已经开帐 gcr31i int null, --是否需要开帐 gcr32i int null, -- gcr33c varchar(20) null, --复核人 gcr34c varchar(10) null, --复核日期 gcr35c varchar(10) null, --复核时间 gcr36f float null, --单次费 gcr37i int null) --疗程序号 --备份一下 insert #gcm16_buf(gcr00c, gcr01c, gcr02c, gcr03f, gcr04f, gcr05f, gcr06f, gcr07f, gcr08f, gcr09f, gcr10f, gcr11d, gcr12d, gcr13i, gcr14f, gcr15c, gcr16i, gcr17i, gcr18f, gcr19i, gcr20i, gcr21i, gcr22i, gcr23i, gcr24d, gcr25c, gcr26i, gcr31i, gcr32i, gcr33c, gcr34c, gcr35c, gcr36f, gcr37i) select gcr00c, gcr01c, gcr02c, gcr03f, gcr04f, gcr05f, gcr06f, gcr07f, gcr08f, gcr09f, gcr10f, gcr11d, gcr12d, gcr13i, gcr14f, gcr15c, gcr16i, gcr17i, gcr18f, gcr19i, gcr20i, gcr21i, gcr22i, gcr23i, gcr24d, gcr25c, gcr26i, gcr31i, gcr32i, gcr33c, gcr34c, gcr35c, gcr36f, gcr37i from gcm16 where gcr00c = @compid and gcr01c = @cardid --更新已经存在的纪录, 只要更新剩余次数为开帐中的次数扣掉已经消费的次数 update a set a.gcf06f = isnull(a.gcf06f, 0) + isnull(b.num, 0), gcf07f = isnull(gcf07f, 0)-isnull(num, 0), a.gcf09f = isnull(a.gcf09f, 0) + isnull(b.amt, 0), gcf10f = isnull(gcf10f, 0)-isnull(amt, 0) from gcm06 a, #tbl_card_proc_used b where a.gcf01c = b.cardid and a.gcf23i = b.procseq and a.gcf00c = @compid and a.gcf01c = @cardid and a.gcf02c = b.procid and isnull(gcf24i, 0)>0 --前期卡本来余额为0,扣除pj的金额可能成为负数了,需要修正 update a set gcf10f = 0 from gcm06 a, #tbl_card_proc_used b where a.gcf01c = b.cardid and a.gcf23i = b.procseq and a.gcf00c = @compid and a.gcf01c = @cardid and a.gcf02c = b.procid and isnull(gcf24i, 0)>0 --开账过来的卡 and isnull(gcf20i, 0) = 1 --前期卡 and isnull(gcf10f, 0)<0 --余额为负的 drop table #tbl_card_proc_used --把已经开张的并却已经更新过去的删除 delete b from gcm06 a, #gcm16_buf b where a.gcf00c = @compid and a.gcf01c = @cardid and isnull(a.gcf24i, 0) = b.gcr23i and a.gcf02c = b.gcr02c --把剩下的插入到gcm06中 --先删除是前期卡,而且开帐的序号,而且也没有消费过 --第一次开帐的时候存在,但后来删除了的,特征,有开帐序号,但无法连接到 --不考虑在gcm16中删除了 insert gcm06(gcf00c, gcf01c, gcf02c, gcf03f, gcf04f, gcf05f, gcf06f, gcf07f, gcf08f, gcf09f, gcf10f, gcf11d, gcf12d, gcf13i, gcf14f, gcf15c, gcf16i, gcf17i, gcf18f, gcf19i, gcf20i, gcf21i, gcf22i, gcf24i) select gcr00c, gcr01c, gcr02c, gcr03f, gcr04f, gcr05f, gcr06f, gcr07f, gcr08f, gcr09f, gcr10f, gcr11d, gcr12d, gcr13i, gcr14f, gcr15c, gcr16i, gcr17i, gcr18f, isnull(gcr19i, 1), isnull(gcr20i, 1), isnull(gcr21i, 0), isnull(gcr22i, 0), gcr23i from #gcm16_buf where gcr00c = @compid and gcr01c = @cardid and isnull(gcr26i, 0) = 0 -- 是否已经开账为否 order by gcr24d drop table #gcm16_buf update gcm16 set gcr26i = 1 where gcr00c = @compid and gcr01c = @cardid --更新gcm03中疗程的余额 select @totalamt = sum(gcf10f) from gcm06 with (nolock) where gcf00c = @compid and gcf01c = @cardid update gcm03 set gcc06f = @totalamt where gcc00c = @compid and gcc01c = @cardid and gcc03i = @procacct end GO /****** Object: StoredProcedure [dbo].[upg_cal_work_detail_comm] Script Date: 06/12/2017 06:19:36 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_cal_work_detail_comm] ( @spid int, @compid varchar(10), -- 公司别 @fromdate varchar(10), -- 开始日期 @todate varchar(10), -- 截至日期 @fromempl varchar(20), -- 查询开始人员 @toempl varchar(20) -- 查询截至人员 ) as begin -- 计算实业绩,若可能就计算出提成 declare @total float declare @cardrate float declare @cost float declare @comm float declare @quan float declare @code varchar(20) declare @action_id int declare @flag int declare @trueperf float declare @vperf float declare @stdprice float declare @share_rate float declare @staff varchar(20) declare @sp035 float declare @ex_rate float declare @last_billid varchar(20) declare @last_seq float declare @last_m_comm float declare @last_l_comm float declare @ggm02_seq float declare @billid varchar(20) declare @billtype varchar(20) declare @prj_type varchar(20) -- 会员卡类别,产品类别,项目类别 declare @paycode varchar(20) -- 支付方式 declare @orderdate varchar(8)--账单日期 added by cyr 2012-04-10 declare @sp048 varchar(20) select @sp048 = gsa02c from gsm01 with(nolock) where gsa00c = @compid and gsa01c = 'SP048' --Start 创建临时表 modify by lm 2015-08-05 create table #work_detail ( id int not null,-- 流水好 person_id varchar(20) not null, -- 员工代码 action_id tinyint not null, -- 类别,1-卡销售 2-指定大工,3-轮班大工,4-补工大工, -- 5-指定中工,6-轮班中工 -- 7-指定小工, 8-轮班小工,9-产品销售 10,11-四工 -- 0 卡充值 code varchar(20) not null, --项目代码,或是卡号,产品码 amt float null, --营业金额 quan float null, --数量 cost float null, --成本 amt2 float null, --虚业绩 amt3 float null, --真实业绩 cardrate float null, --转卡比率 --当买卡或充值的时候,这个地方当作疗程账户金额用 stdprice float null, --标准单价 prj_type varchar(20) null, --项目类别 share_rate float null default 1, billid varchar(20) null, --单号 paycode varchar(20) null, --支付代码 ex_rate float null, --实际营业额折算比率 ggm02_seq float null, --ggm02中的序列号,与单号一起来标志一个项目 ) if not exists(select 1 from sys.indexes where name='idx_detail_id') create nonclustered index idx_detail_comm_id on #work_detail(id) insert #work_detail (id, person_id, action_id, code, amt, cost, quan, cardrate, amt2, amt3, stdprice, share_rate, ex_rate, ggm02_seq, billid, prj_type, paycode) select id, person_id, action_id, code, amt, cost, quan, isnull(cardrate, 1), amt2, amt3, stdprice, share_rate, ex_rate, ggm02_seq, billid, prj_type, paycode from #work_detail_ceshi where spid = @spid order by cls_flag, billid, ggm02_seq, action_id desc -- 创建循环变量 declare @HasRecord int declare @idd int set @HasRecord = 1 -- 按店业绩分段计算按天来 while(@HasRecord = 1) begin if exists(select 1 from #work_detail) begin set @HasRecord = 1 select top 1 @idd = id, @staff = person_id, @action_id = action_id, @code = code, @total = amt, @cost = cost, @quan = quan, @cardrate = cardrate, @vperf = amt2, @trueperf = amt3, @stdprice = stdprice, @share_rate = share_rate, @ex_rate = ex_rate, @ggm02_seq = ggm02_seq, @billid = billid, @prj_type = prj_type, @paycode = paycode from #work_detail select @flag = @action_id declare @cardrate2 float --转卡比率 declare @rmbrate float --转现比率 declare @fa_type nvarchar(20) -- 方案类型 --start 计算实业绩折算比率 --modify by lm 2015.12.16 --得到员工解决方案 if((@action_id >= 2 and @action_id <= 8) or (@action_id >= 10 and @action_id <= 11)) begin select top 1 @fa_type = ppt02c from personprecept with(nolock) where ppt00c = @compid and ppt03c = @staff and ppt04i = 1 and ppt02c in (select pre01c from precept where pre00c = @compid and isnull(pre22i, 0) = 0) order by ppt05d desc -- 转卡比率与转现金比率 if (@staff <> '') --modify by lm 2015.12.16 begin select @cardrate2 = hea13f, @rmbrate = hea24f from (select hea13f, hea24f from hem01 with (nolock) where hea00c = @compid and hea01c = @code and hea33c = @fa_type and hea18i = 2 and hea34c = @paycode union all select hea13f, hea24f from hem01 with (nolock) where hea00c = @compid and hea01c = @code and hea33c = @fa_type and hea18i = 2 and isnull(hea34c, '*') = '*' ) A end if (@paycode = '#' or @paycode = '4' or @paycode = ''or @paycode = '9' or (@paycode >= 'A' and @paycode <= 'Z')) begin set @cardrate = @cardrate2 end else if (@paycode = '1' or @paycode = '6') begin set @cardrate = @rmbrate end end --end -- 只有卡需要计算虚业绩 if (@flag = 1 or @flag = 0) begin if (@sp048 <> '1') begin -- 卡销售或产品销售,业绩需要在这里计算 if @flag = 1 select @billtype = 'gn' else if @flag = 0 select @billtype = 'gz' exec upg_statistic_card_goods @compid, @staff, @code, @prj_type, @stdprice, @total, @quan, @flag, @share_rate, @paycode, @billtype, @billid, @vperf output end else begin set @share_rate = 1 update #work_detail_ceshi set share_rate = 1 where id = @idd end end else if (@flag = 2 or @flag = 3 or @flag = 4 or @flag = 5 or @flag = 6 or @flag = 7 or @flag = 8 or @flag = 9) select @billtype = 'gx' -- 得到账单日期 if (@billtype = 'gn') begin select @orderdate = gna02d from gnm01 with(nolock) where gna00c = @compid and gna01c = @billid end else if (@billtype = 'gz') begin select @orderdate = gcl02d from gcm10 with(nolock) where gcl00c = @compid and gcl01c = @billid end else if (@billtype = 'gx') begin select @orderdate = gga02d from ggm01 with(nolock) where gga00c = @compid and gga01c = @billid end if (@flag >= 2 and @flag <= 6) begin if (@billid <> @last_billid or @ggm02_seq <> @last_seq) begin select @last_m_comm = 0, @last_l_comm = 0 end else begin select @last_m_comm = isnull(@last_m_comm, 0), @last_l_comm = isnull(@last_l_comm, 0) end end else begin select @last_m_comm = 0, @last_l_comm = 0 end -- @ex_rate 项目的实际营业额折扣或产品的实际营业额折扣率 if isnull(@cardrate, 0) = 0 set @cardrate = 1 if isnull(@ex_rate, 999) = 999 begin if (@flag = 1 or @flag = 0) select @ex_rate = isnull(gso05f, 1) from gsm15 with(nolock) where gso00c = @compid and gso02c = @paycode else if (@flag = 2 or @flag = 3 or @flag = 4 or @flag = 5 or @flag = 6 or @flag = 7 or @flag = 8 )--项目 select @ex_rate = isnull(gso03i, 1) from gsm15 with(nolock) where gso00c = @compid and gso02c = @paycode else if @flag = 9 -- 产品 select @ex_rate = isnull(gso06f, 1) from gsm15 with(nolock) where gso00c = @compid and gso02c = @paycode update #work_detail_ceshi set ex_rate = @ex_rate where id = @idd end set @comm = 0 if (@flag = 0 or @flag = 1) and @sp048 <> '1' begin -- 卡销售充值 -- 只有自动计算业绩的时候才能这样计算, 如果是手工输入卡销售业绩的情况下, 不能这样计算, 因为那个表为空 exec upg_get_fillbill_comm @compid, @billtype, @billid, @staff, @paycode, @trueperf output, @comm output -- 卡销售, 虚业绩也按后面的计算方式, 更加合理 select @vperf = @trueperf end else begin -- 项目, 产品的提成计算 exec upg_statistic_proj_comm @compid, @code, @prj_type, @stdprice, @total, @vperf, @cardrate, @cost, @quan, @staff, @flag, @comm output, @trueperf output, @ex_rate, @last_m_comm, @last_l_comm, @share_rate, @orderdate, @paycode, @billid end if (@flag = 7 or @flag = 8) -- 小工 begin select @last_l_comm = @comm, @last_billid = @billid, @last_seq = @ggm02_seq end else if (@flag = 5 or @flag = 6) begin select @last_m_comm = @comm, @last_billid = @billid, @last_seq = @ggm02_seq end else begin select @last_m_comm = 0, @last_l_comm = 0, @last_billid = '', @last_seq = -1 end if @flag = 9 -- 产品 begin update #work_detail_ceshi set comm = @comm, amt3 = @trueperf, amt2 = @vperf where id = @idd end else if ( @flag = 1 or @flag = 0) -- 卡销售 begin update #work_detail_ceshi set comm = @comm, amt3 = @trueperf, amt2 = @vperf where id = @idd end else -- 项目 begin update #work_detail_ceshi set comm = @comm * @share_rate, amt3 = @trueperf * @share_rate, amt2 = @vperf * @share_rate where id = @idd end -- 清除已处理的数据 delete #work_detail where id = @idd end else begin set @HasRecord = 0 end end drop table #work_detail --End end GO /****** Object: StoredProcedure [dbo].[upg_personal_statistics_calc] Script Date: 06/12/2017 06:20:26 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_personal_statistics_calc] ( @compid varchar(10), -- 公司别 @fromdate varchar(10), -- 开始日期 @todate varchar(10), -- 截至日期 @fromempl varchar(20), -- 查询开始人员 @toempl varchar(20), -- 查询截至人员 @inc_card int, -- 卡销售 @inc_service int, -- 服务 @inc_goods int, -- 是否包括产品销售 @return_type int, -- 返回类别 @spid int ) -- 返回需要的数据 -- 1: 员工业绩统计表,最明细的数据,如果是累计的情况,没有返回提成的部分 -- 2: 每个员工一个提成数据,用在工资计算的时候,返回只能是一个员工代码 + 提成 -- 3: 员工业绩排行榜,营业额 -- 4: 员工业绩排行榜,虚业绩 -- 5: 员工业绩排行榜,实业绩 -- 6: 总个公司的总提成,用在利润分析表中 -- 7: 单个人按项目分组的数据,用在业绩排行榜中的明细 -- 8, 9, 10: 员工业绩排行对应的3, 4, 5, 但不需要返回结果 as begin declare @shop_vperf float declare @shop_tperf float declare @shop_amt float declare @shop_comm float declare @not_need_return_result int --是否需要返回结果 if (@return_type >= 8 and @return_type <= 10) begin select @return_type = @return_type - 5 select @not_need_return_result = 1 end else begin select @not_need_return_result = 0 end declare @start_time datetime set @start_time = getdate() -- 把需要的数据准备好,一个员工可能的所有服务数据 -- 所有的虚业绩都已经在单据保存的时候就准备好 (改为准备所有员工的单据, 因为有大工存在扣中(小)工提成的计算方式, 若不全部添加, 无法计算小工的提成) delete work_detail where spid = @spid delete workdetail_by_empl where spid = @spid --Start 创建临时表 add by lm 2015-08-05 create table #b_perf ( bran_id varchar(20) not null ) insert #b_perf(bran_id) select gaz02c from gam26 with(nolock) where gaz01c = @compid create table #personprecept ( id int identity(1,1) not null, ppt03c varchar(20) not null ) -- 创建循环变量 declare @HasRecord_inner int declare @idd_inner int declare @ppt03c_inner nvarchar(50) -- 创建循环变量 declare @HasRecord int set @HasRecord = 1 declare @incompid varchar(10) -- 插入旗下所有子门店的营业数据 while(@HasRecord = 1) begin if exists(select 1 from #b_perf) begin set @HasRecord = 1 select top 1 @incompid = bran_id from #b_perf insert into work_detail(person_id, action_id, srvdate, code, name, payway, amt, quan, cost, comm, amt2, amt3, cardrate, stdprice, prj_type, share_rate, cls_flag, gender, billid, paycode, ex_rate, ggm02_seq, room_id, compid, cardid, memid, spid, comp_ascription) select person_id, action_id, srvdate, code, name, payway, amt, quan, cost, comm, amt2, amt3, cardrate, stdprice, prj_type, share_rate, cls_flag, gender, billid, paycode, ex_rate, ggm02_seq, room_id, @compid, cardid, memid, @spid, compid from calc_work_detail with(nolock) where compid = @incompid and srvdate >= @fromdate and srvdate <= @todate delete from #personprecept insert #personprecept(ppt03c) select ppt03c from personprecept with(nolock) where ppt00c = @incompid and ppt02c in (select pre01c from precept with (nolock) where pre03i = 1 and pre00c = ppt00c and isnull(pre22i, 0) = 0) and ((ppt03c >= @fromempl and ppt03c <= @toempl) or @fromempl = '*') set @HasRecord_inner = 1 -- 按店业绩分段计算按天来 while(@HasRecord_inner = 1) begin if exists(select 1 from #personprecept) begin set @HasRecord_inner = 1 select top 1 @ppt03c_inner = ppt03c, @idd_inner = id from #personprecept insert work_detail(spid, person_id, action_id, code, compid, srvdate) values(@spid, @ppt03c_inner, 101, '店总业绩', @compid, '--') insert workdetail_by_empl(spid, person_id, name, compid, fromdate, todate) values(@spid, @ppt03c_inner, '店总业绩', @compid, @fromdate, @todate) -- 清除已处理的数据 delete #personprecept where id = @idd_inner end else begin set @HasRecord_inner = 0 end end -- 清除已处理的数据 delete #b_perf where bran_id = @incompid end else begin set @HasRecord = 0 end end drop table #b_perf drop table #personprecept --End -- 把每个员工这个时间段内的指定与轮班统计出来 added 2013-03-20 exec upg_create_person_stat @compid, @fromdate, @todate if (@return_type = 7) begin -- 按项目代码进行分组统计 exec upg_cal_work_detail_by_item @spid, @compid, @fromdate, @todate, @fromempl, @toempl end else begin -- 按项目类别分组进行分组统计, 按员工项目类别累计来计算每个员工的提成 exec upg_cal_work_detail_by_kind @spid, @compid, @fromdate, @todate, @fromempl, @toempl -- 按员工进行分组统计, 按员工累计来计算每个员工的提成, 注意是一个员工总的业绩(包含项目,产品,卡) -- 按项目,卡,物品销售分别累计计算提成的(不分项目类别,也需要在此计算) exec upg_cal_work_detail_by_empl @spid, @compid, @fromdate, @todate, @fromempl, @toempl, @return_type -- 计算每个员工可能的人头费提成 exec upg_cal_work_detail_by_empl_num @spid, @compid, @fromdate, @todate, @fromempl, @toempl, @return_type -- 计算整个店的总业绩 exec upg_cal_work_detail_by_shop @spid, @compid, @fromdate, @todate, @fromempl, @toempl, @shop_amt output, @shop_vperf output, @shop_tperf output -- 需要计算某些员工的提成,是按照店总业绩来计算的 exec upg_cal_work_detail_by_empl_shop @spid, @compid, @fromdate, @todate, @fromempl, @toempl, @shop_amt, @shop_vperf, @shop_tperf end -- 返回需要的数据 -- 1: 员工业绩统计表,最明细的数据,如果是累计的情况,没有返回提成的部分 -- 2: 每个员工一个提成数据,用在工资计算的时候,返回只能是一个员工代码 + 提成 -- 3: 员工业绩排行榜,营业额 -- 4: 员工业绩排行榜,虚业绩 -- 5: 员工业绩排行榜,实业绩 -- 6: 总个公司的总提成,用在利润分析表中 -- 7: 单个人按项目分组的数据,用在业绩排行榜中的明细 if @return_type = 1 begin -- 根据版本的不同,看是否需要计算房间号 declare @sp056 varchar(20) select @sp056 = gsa02c from gsm01 with(nolock) where gsa00c = @compid and gsa01c = 'SP056' if @sp056 = '4' or @sp056 = '3' begin update a set a.room_id = b.glg04v from work_detail a, glm07 b with(nolock), ggm01 c with(nolock) where a.billid = c.gga01c and c.gga00c = @compid and b.glg00c = @compid and c.gga30c = b.glg01c and a.code = b.glg06c and a.cls_flag = 1 and spid = @spid end -- 把会员号更新正确, 会员编号的位置存放会员姓名 update a set a.memid = b.gba03c from work_detail a, gbm01 b with(nolock) where spid = @spid and a.cardid = b.gba23c and b.gba00c = @compid -- 员工业绩统计表需要的数据 select person_id, comp_ascription, action_id, srvdate, code, name, payway, amt, quan, cost, comm, amt2, amt3, cardrate, stdprice, prj_type, share_rate, cls_flag, gender, billid, paycode, ex_rate, ggm02_seq, room_id, cardid, memid from work_detail with (nolock) where spid = @spid order by person_id, prj_type, action_id, srvdate end else if @return_type = 2 begin select person_id, isnull(comm, 0) from workdetail_by_empl with (nolock) where spid = @spid end else if (@return_type = 3 or @return_type = 4 or @return_type = 5) begin if (@not_need_return_result = 0) begin select id, person_id, name, amt_c, amt_1, amt_12, amt_13, amt_2, amt_21, amt_3, amt_31, amt_4, amt_41, amt_g, amtrate_1, comm, total_srv, total_perf, total_cash, total_cash_c, bonus, total_srv_cash, total_srv_card, num_fix, num_nofix, consume_cost, compid from workdetail_by_empl with (nolock) where spid = @spid order by isnull(amt_c, 0) + isnull(amt_1, 0) + isnull(amt_12, 0) + isnull(amt_13, 0) + isnull(amt_2, 0) + isnull(amt_21, 0) + isnull(amt_3, 0) + isnull(amt_31, 0) + isnull(amt_4, 0) + isnull(amt_41, 0) + isnull(amt_g, 0) desc end end else if (@return_type = 6) begin select sum(comm) from workdetail_by_empl with (nolock) where spid = @spid end else if (@return_type = 7) begin select * from workdetail_by_item with (nolock) where spid = @spid order by cls_flag, code, action_id end end GO /****** Object: StoredProcedure [dbo].[upg_data_recover] Script Date: 06/12/2017 06:19:55 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_data_recover] as begin -- 创建临时表 create table #data_recover_comp ( compId varchar(10) not null ) insert into #data_recover_comp (compId) select gae01c from gam05 with (nolock) -- 创建循环变量 declare @HasRecord int set @HasRecord = 1 declare @currentCompId varchar(10) while (@HasRecord = 1) begin if exists (select 1 from #data_recover_comp) begin set @HasRecord = 1 select top 1 @currentCompId = compId from #data_recover_comp -- 员工的归属门店 exec upg_recover_own_ham01 @currentCompId exec upg_recover_remind_setting @currentCompId -- 清除已处理的门店 delete #data_recover_comp where compId = @currentCompId end else begin set @HasRecord = 0 end end exec upg_recover_gsm02_3 exec upg_recover_gsm15_gso09f_gs010f exec upg_recover_gsm02_3_custom -- 初始化gsm02_3_role表 exec upg_init_gsm02_3_role exec upg_recover_gsm02_3_role_custom exec upg_recover_gim22_did exec upg_recover_gbm01_gba98c exec upg_data_recover_sp064_sp065_2 exec upg_data_recover_gnm03_gcm11_gcm06_gcf24i exec upg_recover_PNIC_Account exec upg_recover_PrintableAccount exec upg_recover_GIA04I_sp2 exec upg_recover_GMA02R_sp123 -- 税率表 exec upg_recover_gsm23 exec upg_recover_pay_category exec upg_recover_pay_classify exec upg_data_recover_gtc09_gtc10 exec upg_data_recover_kam01 delete from gnm03 where isnull(gnc02c, '') = '' drop table #data_recover_comp end GO /****** Object: StoredProcedure [dbo].[upg_batch_cal_compensation] Script Date: 06/12/2017 06:19:29 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_batch_cal_compensation] ( @hca00c varchar(10), -- 公司别 @hca01c varchar(20), -- 发薪单号 @hca91c varchar(10) -- 登录人 ) as begin begin transaction begin try if not exists(select 1 from hcm01 where hca00c = @hca00c and hca01c = @hca01c) begin return end if exists(select 1 from hcm02 where hcb00c = @hca00c and hcb01c = @hca01c) begin return end declare @id int declare @hca93c varchar(10) -- 复核人 declare @hca94d varchar(8) -- 复核日期 select @id = id, @hca93c = hca93c, @hca94d = hca94d from hcm01 where hca00c = @hca00c and hca01c = @hca01c declare @state int select top 1 @state = state from hcm01_operation where hcId = @id order by id desc if isnull(@state, 0) <> 0 or (isnull(@hca93c, '') <> '' and isnull(@hca94d, '') <> '') begin return end set @state = isnull(@state, 0) exec upg_cal_compensation @hca01c, @hca00c insert into hcm01_operation(hcId, operation, operator, state) values(@id, 5, @hca91c, @state) end try begin catch if @@TRANCOUNT > 0 begin rollback transaction declare @SP_Parameters varchar(max) set @SP_Parameters = '@hca00c = ' + isnull(@hca00c, '') + '@hca01c = ' + isnull(@hca01c, '') + '@hca91c = ' + isnull(@hca91c, '') insert into SP_TranLog (SPName ,SPTime ,SPParameter) values ( 'upg_batch_cal_compensation' ,getdate() ,@SP_Parameters) raiserror ('transaction is failed.', 16, 1) WITH NOWAIT end end catch if @@TRANCOUNT > 0 begin commit transaction end end GO /****** Object: StoredProcedure [dbo].[upg_expand_net] Script Date: 06/12/2017 06:20:02 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_expand_net] ( @compid varchar(10), -- 公司别 @main_card varchar(20), -- 介绍人 @flag int, -- 1-显示整个网络结构 2-计算佣金,并存入某个账户 @billNo varchar(30), --单据号 add lucz 2015.11.12 @from int, -- 0 卖卡, 1 充值, 2 消费(收银) add lucz 2015.11.12 @verify int -- 0 反审, 1 审核 ) as begin declare @lvl int declare @billid varchar(20) -- 判断是否有介绍佣金设置 declare @set_rows int select @set_rows = count(*) from gam14 where gal00c = @compid create table #intro_net ( idd int identity not null, card_type varchar(5) not null, -- 会员卡类别 lvl int not null, -- 层级 src_card varchar(20) null, -- 原始客户会员卡 intro_card varchar(20) null, -- 介绍来会员卡 card_amt float null, -- 介绍来卡的金额 ratio float null, -- 提佣比率 amt float null, -- 提佣金额 is_saved int null, -- 是否已经给客户提佣 billid varchar(20) null, -- 单据号 ) declare @PK_Sql nvarchar(max) set @PK_Sql = ' alter table #intro_net add constraint PK_#intro_net_' + convert(varchar(10), cast(floor(rand() * 1000000) as int)) + ' primary key nonclustered(idd)' exec sp_executesql @PK_Sql create clustered index idx_intro_002 on #intro_net(card_type) if isnull(@set_rows, 0) = 0 or isnull(@main_card, '') = '' begin if @flag = 1 begin select * from #intro_net drop table #intro_net end return end select @lvl = 0 if( @from = 0 ) --卖卡 begin exec upg_expand_net_sub @compid, @main_card, @lvl update a set a.ratio = isnull(b.gal03f, 0), a.amt = a.card_amt * isnull(b.gal03f, 0) from #intro_net a, gam14 b where a.card_type = b.gal01c and a.lvl = b.gal02i and b.gal00c = @compid and b.gal04i = 1 end else if(@from = 1) --充值 begin exec upg_expand_net_gcm10 @compid, @main_card, @lvl,@billNo update a set a.ratio = isnull(b.gal03f, 0), a.amt = a.card_amt * isnull(b.gal03f, 0) from #intro_net a, gam14 b where a.card_type = b.gal01c and a.lvl = b.gal02i and b.gal00c = @compid and b.gal05i = 1 end else if(@from = 2) --消费 begin exec upg_expand_net_ggm01 @compid, @main_card, @lvl,@billNo update a set a.ratio = isnull(b.gal03f, 0), a.amt = a.card_amt * isnull(b.gal03f, 0) from #intro_net a, gam14 b where a.card_type = b.gal01c and a.lvl = b.gal02i and b.gal00c = @compid and b.gal06i = 1 end if @flag = 2 --2:计算佣金,并存入某个账户 begin declare @total_comm float declare @type int declare @send_mode varchar(10) declare @cardid varchar(20) declare @comm float declare @curdate varchar(20) declare @curtime varchar(20) exec upg_getcurrdate @curdate output, @curtime output if(@verify = 1) begin select @total_comm = sum(amt) from #intro_net where isnull(is_saved, 0) = 0 end else --反审 begin select @total_comm = sum(amt) from #intro_net end select @total_comm = isnull(@total_comm, 0) select @type = convert(int, gsa02c) from gsm01 where gsa00c = @compid and gsa01c = 'SP042' select @send_mode = gsa02c from gsm01 where gsa00c = @compid and gsa01c = 'SP071' select @type = isnull(@type, 2) -- if (@total_comm > 0) -- begin if @send_mode = '1' or @send_mode = '3' begin if(@verify = 1) begin select @billid = billid from #intro_net where isnull(is_saved, 0) = 0 and isnull(billid, '') <> '' end else begin select @billid = billid from #intro_net where isnull(billid, '') <> '' end exec upg_purse_deduct_ex @compid, @main_card, @curdate, @type, @total_comm, 0, 'gn', @billid, '介绍佣金', @verify end if @send_mode = '2' or @send_mode = '3' begin declare cur_each_card cursor for select intro_card, amt, billid from #intro_net open cur_each_card fetch cur_each_card into @cardid, @comm, @billid while @@fetch_status = 0 begin if isnull(@comm, 0) > 0 begin exec upg_purse_deduct_ex @compid, @cardid, @curdate, @type, @comm, 0, 'gn', @billid, '介绍佣金', @verify end fetch cur_each_card into @cardid, @comm, @billid end close cur_each_card deallocate cur_each_card end --end end else if @flag = 1 begin select * from #intro_net end drop table #intro_net end GO /****** Object: StoredProcedure [dbo].[upg_resetverp_gcm10_gnm01_ceshi] Script Date: 06/12/2017 06:20:38 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_resetverp_gcm10_gnm01_ceshi] ( @compid varchar(10), @fromdate varchar(20), @todate varchar(20) ) as begin set nocount on declare @SP04802 varchar(5) select @SP04802 = gsa02c from gsm01 with(nolock) where gsa00c = @compid and gsa01c = 'SP048' create table #resetverp ( id int identity not null, compid varchar(10) null, billid varchar(20) null, billtype varchar(5) null, ) -- 支付的缓冲表 create table #gsm03_detail ( idd int identity not null, -- 序号 cardid varchar(20) null, -- 会员卡号 compid varchar(10) null, billid varchar(20) null, billtype varchar(10) not null, -- 单别 loop_cnt int not null, code varchar(20) null, -- 代码 卡代码, 套餐代码, 疗程代码 paycode varchar(20) not null, -- 支付代码 payamt float null, -- 支付金额 cardtype varchar(20) null, -- 卡类别 times float null, -- 疗程或套餐的次数 procid float null, -- 疗程的价格序号 pctimes float null, -- 疗程或套餐的实际次数 primary key(idd) ) -- 创建临时表 create table #temp_comp ( compId varchar(10) not null ) insert into #temp_comp(compId) select gaz02c from gam26 with (nolock) where gaz01c = @compid create table #tbl_bill_perf ( idd int identity not null, compid varchar(20) not null, billtype varchar(20) not null, billid varchar(20) not null, person_pos int null, perf float null, primary key(idd) ) create table #gnm01 -- 会员卡销售单 ( gna00c varchar(10) not null, --门店编号 gna01c varchar(20) not null, --销售单号 gna13c varchar(20) null, --取gnm02中第一张卡 gna20c varchar(10) null, --负责业务 gna27f float null, --第一分享比率 gna28c varchar(10) null, --第二卖卡人 gna29f float null, --分享比率 gna30c varchar(10) null, --第三卖卡人 gna31f float null, --分享比率 gna32c varchar(10) null, --第4卖卡人 gna33f float null, --分享比率 gna34c varchar(10) null, --第5卖卡人 gna35f float null, --分享比率 gna36c varchar(10) null, --第6卖卡人 gna37f float null, --分享比率 gna38c varchar(10) null, --第7卖卡人 gna39f float null, --分享比率 gna40c varchar(10) null, --第8卖卡人 gna41f float null, --分享比率 gna42c varchar(10) null, --第9卖卡人 gna43f float null, --分享比率 gna44c varchar(10) null, --第10卖卡人 gna45f float null, --分享比率 gna67f float null, --第一销售虚业绩 Added by WUXM 2005/11/04 gna69f float null, --第二销售虚业绩 Added by WUXM 2005/11/04 gna71f float null, --第三销售虚业绩 Added by WUXM 2005/11/04 gna86f float null, --第四人员业绩 gna87f float null, --第五人员业绩 gna88f float null, --第六人员业绩 gna89f float null, --第七人员业绩 gna90f float null, --第八人员业绩 gna91f float null, --第九人员业绩 gna92f float null, --第十人员业绩 primary key (gna00c,gna01c) ) create table #gcm10 -- 帐户异动单 ( gcl00c varchar(10) not null, --门店编号 gcl01c varchar(20) not null, --异动单号 gcl03c varchar(20) null, --会员卡号 gcl04c varchar(5) null, --帐号类别(1.电子钱包 2.储值帐户) gcl06i int null, --异动类别( 0充值 ,1取款 5欠款 6还款) gcl09c varchar(20) null, --销售人员 gcl15c varchar(5) null, --新的会员卡类别 gcl16f float null, --第一销售分享比率 gcl17c varchar(20) null, --第二销售 gcl18f float null, --分享比率 gcl19c varchar(20) null, --第三负责人 gcl20f float null, --分享比率 gcl22c varchar(20) null, --第4负责人 gcl23f float null, --分享比率 gcl24c varchar(20) null, --第5负责人 gcl25f float null, --分享比率 gcl26c varchar(20) null, --第6负责人 gcl27f float null, --分享比率 gcl28c varchar(20) null, --第7负责人 gcl29f float null, --分享比率 gcl30c varchar(20) null, --第8负责人 gcl31f float null, --分享比率 gcl32c varchar(20) null, --第9负责人 gcl33f float null, --分享比率 gcl34c varchar(20) null, --第10负责人 gcl35f float null, --分享比率 gcl52c varchar(20) null, --升级前会员卡类别 gcl53f float null, --储值应收金额 gcl67c varchar(5) null, --支付方式1 gcl68f float null, --支付方式1金额 gcl69c varchar(5) null, --支付方式2 gcl70f float null, --支付方式2金额 gcl71c varchar(5) null, --支付方式3 gcl72f float null, --支付方式3金额 gcl73c varchar(5) null, --支付方式4 gcl74f float null, --支付方式4金额 gcl58f float null, --第一销售虚业绩 Added by WUXM 2005/11/08 gcl60f float null, --第二销售虚业绩 Added by WUXM 2005/11/08 gcl62f float null, --第三销售虚业绩 Added by WUXM 2005/11/08 gcl83f float null, --第四人员业绩 gcl84f float null, --第五人员业绩 gcl85f float null, --第六人员业绩 gcl86f float null, --第七人员业绩 gcl87f float null, --第八人员业绩 gcl88f float null, --第九人员业绩 gcl89f float null, --第十人员业绩 gcl112i float default 0 null primary key (gcl00c,gcl01c) --debt_mark int default 0 null, ) create table #card_fill_detail_by_payment ( idd int identity not null, -- 序号 compid varchar(10) not null, -- 公司别 billtype varchar(10) not null, -- 单别 billid varchar(20) not null, -- 单号 personid varchar(20) null, -- 员工号 share_rate float null, -- 分享比率 paycode varchar(20) null, -- 支付代码 payamt float null, -- 支付金额 filltype int null, -- 1 充值 2 疗程 3 套餐 cardid varchar(20) null, -- 会员卡号 cardtype varchar(20) null, -- 会员卡类别 code varchar(20) null, -- 代码 会员卡代码 ,疗程代码,套餐代码 times float null, -- 疗程次数或套餐的次数,卡销售=1 procid float null, -- 疗程的价格序号 perf float null, -- 业绩 person_pos int null, -- 第几销售 1,2,3,4 comm float null, -- 提成 pctimes float null, -- 疗程或套餐的实际次数(支付方式拆分) settype int null, -- 业绩方式 setvalue float null, -- 业绩比率 perf_code varchar(20) null, -- 提成方案代码 pay_ratio float null -- 卡销售销售业绩折算比率 primary key(idd) ) create table #card_fill_detail_by_payment_2 ( idd int identity not null, -- 序号 compid varchar(10) not null, -- 公司别 billtype varchar(10) not null, -- 单别 billid varchar(20) not null, -- 单号 personid varchar(20) null, -- 员工号 share_rate float null, -- 分享比率 paycode varchar(20) null, -- 支付代码 payamt float null, -- 支付金额 filltype int null, -- 1 充值 2 疗程 3 套餐 cardid varchar(20) null, -- 会员卡号 cardtype varchar(20) null, -- 会员卡类别 code varchar(20) null, -- 代码 会员卡代码 ,疗程代码,套餐代码 times float null, -- 疗程次数或套餐的次数,卡销售=1 procid float null, -- 疗程的价格序号 perf float null, -- 业绩 person_pos int null, -- 第几销售 1,2,3,4 comm float null, -- 提成 pctimes float null, -- 疗程或套餐的实际次数(支付方式拆分) settype int null, -- 业绩方式 setvalue float null, -- 业绩比率 perf_code varchar(20) null, -- 提成方案代码 pay_ratio float null -- 卡销售销售业绩折算比率 primary key(idd) ) -- 创建循环变量 declare @HasRecord int set @HasRecord = 1 declare @currentCompId varchar(10) while(@HasRecord = 1) begin if exists(select 1 from #temp_comp) begin set @HasRecord = 1 select top 1 @currentCompId = compId from #temp_comp insert #gnm01(gna00c,gna01c,gna13c,gna20c,gna27f,gna28c,gna29f,gna30c,gna31f,gna32c,gna33f, gna34c,gna35f,gna36c,gna37f,gna38c,gna39f,gna40c,gna41f,gna42c,gna43f,gna44c,gna45f, gna67f,gna69f,gna71f,gna86f,gna87f,gna88f,gna89f,gna90f,gna91f,gna92f) select gna00c,gna01c,gna13c,gna20c,gna27f,gna28c,gna29f,gna30c,gna31f,gna32c,gna33f, gna34c,gna35f,gna36c,gna37f,gna38c,gna39f,gna40c,gna41f,gna42c,gna43f,gna44c,gna45f, gna67f,gna69f,gna71f,gna86f,gna87f,gna88f,gna89f,gna90f,gna91f,gna92f from gnm01 with(nolock) where gna00c = @currentCompId and gna80d between @fromdate and @todate insert #gcm10(gcl00c,gcl01c,gcl03c,gcl04c,gcl06i,gcl09c,gcl15c,gcl16f,gcl17c,gcl18f,gcl19c,gcl20f,gcl22c, gcl23f,gcl24c,gcl25f,gcl26c,gcl27f,gcl28c,gcl29f,gcl30c,gcl31f,gcl32c, gcl33f,gcl34c,gcl35f,gcl52c,gcl53f,gcl67c,gcl68f,gcl69c,gcl70f,gcl71c,gcl72f,gcl73c,gcl74f, gcl58f,gcl60f,gcl62f,gcl83f,gcl84f,gcl85f,gcl86f,gcl87f,gcl88f,gcl89f,gcl112i) select gcl00c,gcl01c,gcl03c,gcl04c,gcl06i,gcl09c,gcl15c,gcl16f,gcl17c,gcl18f,gcl19c,gcl20f,gcl22c, gcl23f,gcl24c,gcl25f,gcl26c,gcl27f,gcl28c,gcl29f,gcl30c,gcl31f,gcl32c, gcl33f,gcl34c,gcl35f,gcl52c,gcl53f,gcl67c,gcl68f,gcl69c,gcl70f,gcl71c,gcl72f,gcl73c,gcl74f, gcl58f,gcl60f,gcl62f,gcl83f,gcl84f,gcl85f,gcl86f,gcl87f,gcl88f,gcl89f,gcl112i from gcm10 with(nolock) where gcl00c = @currentCompId and gcl80d between @fromdate and @todate and (gcl06i = 0 or gcl06i = 1 or gcl06i = 6) -- 清除已处理的门店 delete #temp_comp where compId = @currentCompId end else begin set @HasRecord = 0 end end -- --设置欠款疗程/套餐标志 -- update a -- set a.debt_mark = 1 -- from #gcm10 a -- where (gcl06i = 6 and gcl04c = 4) -- -- update a -- set a.debt_mark = 1 -- from #gcm10 a -- where (gcl06i = 6 and gcl04c = 889) --start添加数据 insert #gsm03_detail(compid, billid, billtype, loop_cnt, cardid, code, paycode, payamt, times,cardtype) select gna00c, gna01c, 'gn', 1, gnb03c, gnb03c, gnb15c, isnull(gnb16f, 0),case gnb07f when 0 then 0 else isnull(gnb16f, 0) / isnull(gnb07f, 1) end,gnb02c from gnm02 a with (nolock), #gnm01 b where a.gnb00c = b.gna00c and a.gnb01c = b.gna01c and isnull(gnb15c, '') <> '' insert #gsm03_detail(compid, billid, billtype, loop_cnt, cardid, code, paycode, payamt, times,cardtype) select gna00c, gna01c,'gn', 1, gnb03c, gnb03c, gnb17c, isnull(gnb18f, 0),case gnb07f when 0 then 0 else isnull(gnb18f, 0) / isnull(gnb07f, 1) end,gnb02c from gnm02 a with (nolock), #gnm01 b where a.gnb00c = b.gna00c and a.gnb01c = b.gna01c and isnull(gnb17c, '') <> '' insert #gsm03_detail(compid, billid, billtype, loop_cnt, cardid, code, paycode, payamt, times,cardtype) select gna00c, gna01c, 'gn', 1, gnb03c, gnb03c, gnb19c, isnull(gnb20f, 0), case gnb07f when 0 then 0 else isnull(gnb20f, 0) / isnull(gnb07f, 1) end,gnb02c from gnm02 a with (nolock), #gnm01 b where a.gnb00c = b.gna00c and a.gnb01c = b.gna01c and isnull(gnb19c, '') <> '' insert #gsm03_detail(compid, billid, billtype, loop_cnt, cardid, code, paycode, payamt, times,cardtype) select gna00c, gna01c, 'gn', 1, gnb03c, gnb03c, gnb21c, isnull(gnb22f, 0), case gnb07f when 0 then 0 else isnull(gnb22f, 0) / isnull(gnb07f, 1) end,gnb02c from gnm02 a with (nolock), #gnm01 b where a.gnb00c = b.gna00c and a.gnb01c = b.gna01c and isnull(gnb21c, '') <> '' insert #gsm03_detail(compid, billid, billtype, loop_cnt, cardid, code, paycode, payamt, times, procid, pctimes) select gna00c, gna01c, 'gn', 2, gna13c, gnc02c, gnc15c, isnull(gnc16f, 0), gnc03f, gnc09i, dbo.fun_divide_default(isnull(gnc16f, 0), isnull(gnc16f, 0) + isnull(gnc18f, 0) + isnull(gnc20f, 0) + isnull(gnc24f, 0), 0) from gnm03 a with (nolock), #gnm01 b where a.gnc00c = b.gna00c and a.gnc01c = b.gna01c and isnull(gnc15c, '') <> '' insert #gsm03_detail(compid, billid, billtype, loop_cnt, cardid, code, paycode, payamt, times, procid, pctimes) select gna00c, gna01c, 'gn', 2, gna13c, gnc02c, gnc17c, isnull(gnc18f, 0), gnc03f, gnc09i, dbo.fun_divide_default(isnull(gnc18f, 0), isnull(gnc16f, 0) + isnull(gnc18f, 0) + isnull(gnc20f, 0) + isnull(gnc24f, 0), 0) from gnm03 a with (nolock), #gnm01 b where a.gnc00c = b.gna00c and a.gnc01c = b.gna01c and isnull(gnc17c, '') <> '' insert #gsm03_detail(compid, billid, billtype, loop_cnt, cardid, code, paycode, payamt, times, procid, pctimes) select gna00c, gna01c,'gn', 2, gna13c, gnc02c, gnc19c, isnull(gnc20f, 0), gnc03f, gnc09i, dbo.fun_divide_default(isnull(gnc20f, 0), isnull(gnc16f, 0) + isnull(gnc18f, 0) + isnull(gnc20f, 0) + isnull(gnc24f, 0), 0) from gnm03 a with (nolock), #gnm01 b where a.gnc00c = b.gna00c and a.gnc01c = b.gna01c and isnull(gnc19c, '') <> '' insert #gsm03_detail(compid, billid, billtype, loop_cnt, cardid, code, paycode, payamt, times, procid, pctimes) select gna00c, gna01c, 'gn', 2, gna13c, gnc02c, gnc23c, isnull(gnc24f, 0), gnc03f, gnc09i, dbo.fun_divide_default(isnull(gnc24f, 0), isnull(gnc16f, 0) + isnull(gnc18f, 0) + isnull(gnc20f, 0) + isnull(gnc24f, 0), 0) from gnm03 a with (nolock), #gnm01 b where a.gnc00c = b.gna00c and a.gnc01c = b.gna01c and isnull(gnc23c, '') <> '' -- ----减掉欠款的(统一做现金支付方式算) -- insert #gsm03_detail(compid, billid, billtype, loop_cnt, cardid, code, paycode, payamt, times, procid, pctimes) -- select gna00c, gna01c, 'gn', 2, gna13c, gnc02c, '1', (0-isnull(gnc33f, 0)), gnc03f, gnc09i, dbo.fun_divide_default(isnull(gnc16f, 0), isnull(gnc16f, 0) + isnull(gnc18f, 0) + isnull(gnc20f, 0) + isnull(gnc24f, 0), 0) -- from gnm03 a with (nolock), #gnm01 b -- where a.gnc00c = b.gna00c -- and a.gnc01c = b.gna01c -- and isnull(gnc15c, '') <> '' insert #gsm03_detail(compid, billid, billtype, loop_cnt, cardid, code, paycode, payamt, times, pctimes) select gna00c, gna01c, 'gn', 3, gna13c, gtf02c, gtf09c, isnull(gtf10f, 0), gtf04f, dbo.fun_divide_default(isnull(gtf10f, 0), isnull(gtf10f, 0) + isnull(gtf12f, 0) + isnull(gtf14f, 0) + isnull(gtf16f, 0), 0) from gtc05 a with (nolock), #gnm01 b where a.gtf00c = b.gna00c and a.gtf01c = b.gna01c and isnull(gtf09c, '') <> '' insert #gsm03_detail(compid, billid, billtype, loop_cnt, cardid, code, paycode, payamt, times, pctimes) select gna00c, gna01c, 'gn', 3, gna13c, gtf02c, gtf11c, isnull(gtf12f, 0), gtf04f, dbo.fun_divide_default(isnull(gtf12f, 0), isnull(gtf10f, 0) + isnull(gtf12f, 0) + isnull(gtf14f, 0) + isnull(gtf16f, 0), 0) from gtc05 a with (nolock), #gnm01 b where a.gtf00c = b.gna00c and a.gtf01c = b.gna01c and isnull(gtf11c, '') <> '' insert #gsm03_detail(compid, billid, billtype, loop_cnt, cardid, code, paycode, payamt, times, pctimes) select gna00c, gna01c, 'gn', 3, gna13c, gtf02c, gtf13c, isnull(gtf14f, 0), gtf04f, dbo.fun_divide_default(isnull(gtf14f, 0), isnull(gtf10f, 0) + isnull(gtf12f, 0) + isnull(gtf14f, 0) + isnull(gtf16f, 0), 0) from gtc05 a with (nolock), #gnm01 b where a.gtf00c = b.gna00c and a.gtf01c = b.gna01c and isnull(gtf13c, '') <> '' insert #gsm03_detail(compid, billid, billtype, loop_cnt, cardid, code, paycode, payamt, times, pctimes) select gna00c, gna01c, 'gn', 3, gna13c, gtf02c, gtf15c, isnull(gtf16f, 0), gtf04f, dbo.fun_divide_default(isnull(gtf16f, 0), isnull(gtf10f, 0) + isnull(gtf12f, 0) + isnull(gtf14f, 0) + isnull(gtf16f, 0), 0) from gtc05 a with (nolock), #gnm01 b where a.gtf00c = b.gna00c and a.gtf01c = b.gna01c and isnull(gtf15c, '') <> '' -- -- --减掉欠款的(统一做现金支付方式算) -- insert #gsm03_detail(compid, billid, billtype, loop_cnt, cardid, code, paycode, payamt, times, pctimes) -- select gna00c, gna01c, 'gn', 3, gna13c, gtf02c, '1', (0-isnull(gtf22f, 0)), gtf04f, dbo.fun_divide_default(isnull(gtf10f, 0), isnull(gtf10f, 0) + isnull(gtf12f, 0) + isnull(gtf14f, 0) + isnull(gtf16f, 0), 0) -- from gtc05 a with (nolock), #gnm01 b -- where a.gtf00c = b.gna00c -- and a.gtf01c = b.gna01c -- and isnull(gtf09c, '') <> '' insert #gsm03_detail(compid, billid, billtype, loop_cnt, cardid, code, paycode, payamt, times, cardtype) --add by lm 2015-10-10(cardtype) select gcl00c, gcl01c, 'gz', 1, gcl03c, gcl03c, gcl67c, isnull(gcl68f, 0), isnull(gcl68f, 0) / isnull(gcl53f, 1), isnull(gcl15c,'') from #gcm10 a with (nolock) where isnull(gcl67c, '') <> '' and isnull(gcl112i,0) = 0 insert #gsm03_detail(compid, billid, billtype, loop_cnt, cardid, code, paycode, payamt, times, cardtype)--add by lm 2015-10-10(cardtype) select gcl00c, gcl01c, 'gz', 1, gcl03c, gcl03c, gcl69c, isnull(gcl70f, 0), isnull(gcl70f, 0) / isnull(gcl53f, 1), isnull(gcl15c,'') from #gcm10 a with (nolock) where isnull(gcl69c, '') <> '' and isnull(gcl112i,0) = 0 insert #gsm03_detail(compid, billid, billtype, loop_cnt, cardid, code, paycode, payamt, times, cardtype)--add by lm 2015-10-10(cardtype) select gcl00c, gcl01c, 'gz', 1, gcl03c, gcl03c, gcl71c, isnull(gcl72f, 0), isnull(gcl72f, 0) / isnull(gcl53f, 1), isnull(gcl15c,'') from #gcm10 a with (nolock) where isnull(gcl71c, '') <> '' and isnull(gcl112i,0) = 0 insert #gsm03_detail(compid, billid, billtype, loop_cnt, cardid, code, paycode, payamt, times, cardtype)--add by lm 2015-10-10(cardtype) select gcl00c, gcl01c, 'gz', 1, gcl03c, gcl03c, gcl73c, isnull(gcl74f, 0), isnull(gcl74f, 0) / isnull(gcl53f, 1), isnull(gcl15c,'') from #gcm10 a with (nolock) where isnull(gcl73c, '') <> '' and isnull(gcl112i,0) = 0 insert #gsm03_detail(compid, billid, billtype, loop_cnt, cardid, code, paycode, payamt, times, procid, pctimes) select gcl00c, gcl01c, 'gz', 2, gcl03c, gcm02c, gcm15c, isnull(gcm16f, 0), gcm03f, gcm09i, dbo.fun_divide_default(isnull(gcm16f, 0), isnull(gcm16f, 0) + isnull(gcm18f, 0) + isnull(gcm20f, 0) + isnull(gcm24f, 0), 0) from gcm11 a with (nolock), #gcm10 b where a.gcm00c = b.gcl00c and a.gcm01c = b.gcl01c and isnull(gcm15c, '') <> '' insert #gsm03_detail(compid, billid, billtype, loop_cnt, cardid, code, paycode, payamt, times, procid, pctimes) select gcl00c, gcl01c, 'gz', 2, gcl03c, gcm02c, gcm17c, isnull(gcm18f, 0), gcm03f, gcm09i, dbo.fun_divide_default(isnull(gcm18f, 0), isnull(gcm16f, 0) + isnull(gcm18f, 0) + isnull(gcm20f, 0) + isnull(gcm24f, 0), 0) from gcm11 a with (nolock), #gcm10 b where a.gcm00c = b.gcl00c and a.gcm01c = b.gcl01c and isnull(gcm17c, '') <> '' insert #gsm03_detail(compid, billid, billtype, loop_cnt, cardid, code, paycode, payamt, times, procid, pctimes) select gcl00c, gcl01c, 'gz', 2, gcl03c, gcm02c, gcm19c, isnull(gcm20f, 0), gcm03f, gcm09i, dbo.fun_divide_default(isnull(gcm20f, 0), isnull(gcm16f, 0) + isnull(gcm18f, 0) + isnull(gcm20f, 0) + isnull(gcm24f, 0), 0) from gcm11 a with (nolock), #gcm10 b where a.gcm00c = b.gcl00c and a.gcm01c = b.gcl01c and isnull(gcm19c, '') <> '' insert #gsm03_detail(compid, billid, billtype, loop_cnt, cardid, code, paycode, payamt, times, procid, pctimes) select gcl00c, gcl01c, 'gz', 2, gcl03c, gcm02c, gcm23c, isnull(gcm24f, 0), gcm03f, gcm09i, dbo.fun_divide_default(isnull(gcm24f, 0), isnull(gcm16f, 0) + isnull(gcm18f, 0) + isnull(gcm20f, 0) + isnull(gcm24f, 0), 0) from gcm11 a with (nolock), #gcm10 b where a.gcm00c = b.gcl00c and a.gcm01c = b.gcl01c and isnull(gcm23c, '') <> '' -- --减掉欠款的(统一做现金支付方式算) -- insert #gsm03_detail(compid, billid, billtype, loop_cnt, cardid, code, paycode, payamt, times, procid, pctimes) -- select gcl00c, gcl01c, 'gz', 2, gcl03c, gcm02c, '1', (0-isnull(gcm32f, 0)), gcm03f, gcm09i, dbo.fun_divide_default(isnull(gcm16f, 0), isnull(gcm16f, 0) + isnull(gcm18f, 0) + isnull(gcm20f, 0) + isnull(gcm24f, 0), 0) -- from gcm11 a with (nolock), #gcm10 b -- where a.gcm00c = b.gcl00c -- and a.gcm01c = b.gcl01c -- and isnull(gcm15c, '') <> '' -- and isnull(debt_mark,0) = 0 insert #gsm03_detail(compid, billid, billtype, loop_cnt, cardid, code, paycode, payamt, times, pctimes) select gcl00c, gcl01c, 'gz', 3, gcl03c, gth02c, gth09c, isnull(gth10f, 0), gth04f, dbo.fun_divide_default(isnull(gth10f, 0), isnull(gth10f, 0) + isnull(gth12f, 0) + isnull(gth14f, 0) + isnull(gth16f, 0), 0) from gtc06 a with (nolock), #gcm10 b where a.gth00c = b.gcl00c and a.gth01c = b.gcl01c and isnull(gth09c, '') <> '' insert #gsm03_detail(compid, billid, billtype, loop_cnt, cardid, code, paycode, payamt, times, pctimes) select gcl00c, gcl01c, 'gz', 3, gcl03c, gth02c, gth11c, isnull(gth12f, 0), gth04f, dbo.fun_divide_default(isnull(gth12f, 0), isnull(gth10f, 0) + isnull(gth12f, 0) + isnull(gth14f, 0) + isnull(gth16f, 0), 0) from gtc06 a with (nolock), #gcm10 b where a.gth00c = b.gcl00c and a.gth01c = b.gcl01c and isnull(gth11c, '') <> '' insert #gsm03_detail(compid, billid, billtype, loop_cnt, cardid, code, paycode, payamt, times, pctimes) select gcl00c, gcl01c, 'gz', 3, gcl03c, gth02c, gth13c, isnull(gth14f, 0), gth04f, dbo.fun_divide_default(isnull(gth14f, 0), isnull(gth10f, 0) + isnull(gth12f, 0) + isnull(gth14f, 0) + isnull(gth16f, 0), 0) from gtc06 a with (nolock), #gcm10 b where a.gth00c = b.gcl00c and a.gth01c = b.gcl01c and isnull(gth13c, '') <> '' insert #gsm03_detail(compid, billid, billtype, loop_cnt, cardid, code, paycode, payamt, times, pctimes) select gcl00c, gcl01c, 'gz', 3, gcl03c, gth02c, gth15c, isnull(gth16f, 0), gth04f, dbo.fun_divide_default(isnull(gth16f, 0), isnull(gth10f, 0) + isnull(gth12f, 0) + isnull(gth14f, 0) + isnull(gth16f, 0), 0) from gtc06 a with (nolock), #gcm10 b where a.gth00c = b.gcl00c and a.gth01c = b.gcl01c and isnull(gth15c, '') <> '' -- --减掉欠款的(统一做现金支付方式算) -- insert #gsm03_detail(compid, billid, billtype, loop_cnt, cardid, code, paycode, payamt, times, pctimes) -- select gcl00c, gcl01c, 'gz', 3, gcl03c, gth02c, '1', (0 -isnull(gth23f, 0)), gth04f, dbo.fun_divide_default(isnull(gth10f, 0), isnull(gth10f, 0) + isnull(gth12f, 0) + isnull(gth14f, 0) + isnull(gth16f, 0), 0) -- from gtc06 a with (nolock), #gcm10 b -- where a.gth00c = b.gcl00c -- and a.gth01c = b.gcl01c -- and isnull(gth09c, '') <> '' -- and isnull(debt_mark,0) = 0 --还款 insert #gsm03_detail(compid, billid, billtype, loop_cnt, cardid, code, paycode, payamt, times, procid, pctimes) select gcl00c, gcl01c, 'gz', 2, gcl03c, gcm02c, gsc04c, isnull(gsc05f,0), -- isnull(gcm33f, 0), gcm03f, gcm09i, dbo.fun_divide_default(isnull(gcm24f, 0), isnull(gcm16f, 0) + isnull(gcm18f, 0) + isnull(gcm20f, 0) + isnull(gcm24f, 0), 0) from gcm11 a with (nolock), #gcm10 b, gsm03 c with(nolock) where a.gcm00c = b.gcl00c and a.gcm30c = b.gcl01c and a.gcm00c = c.gsc00c and a.gcm30c = c.gsc01c and c.gsc02c = 'gz' and isnull(a.gcm30c, '') <> '' insert #gsm03_detail(compid, billid, billtype, loop_cnt, cardid, code, paycode, payamt, times, procid, pctimes) select gcl00c, gcl01c, 'gz', 2, gcl03c, gnc02c, gsc04c, isnull(gsc05f,0), --isnull(gnc35f, 0), gnc03f, gnc09i, dbo.fun_divide_default(isnull(gnc24f, 0), isnull(gnc16f, 0) + isnull(gnc18f, 0) + isnull(gnc20f, 0) + isnull(gnc24f, 0), 0) from gnm03 a with (nolock), #gcm10 b, gsm03 c with(nolock) where a.gnc00c = b.gcl00c and a.gnc31c = b.gcl01c and a.gnc00c = c.gsc00c and a.gnc31c = c.gsc01c and c.gsc02c = 'gz' and isnull(a.gnc31c, '') <> '' insert #gsm03_detail(compid, billid, billtype, loop_cnt, cardid, code, paycode, payamt, times, pctimes) select gcl00c, gcl01c, 'gz', 3, gcl03c, gth02c, gsc04c, isnull(gsc05f,0), --isnull(gth25f, 0), gth04f, dbo.fun_divide_default(isnull(gth12f, 0), isnull(gth10f, 0) + isnull(gth12f, 0) + isnull(gth14f, 0) + isnull(gth16f, 0), 0) from gtc06 a with (nolock), #gcm10 b, gsm03 c with(nolock) where a.gth00c = b.gcl00c and a.gth21c = b.gcl01c and a.gth00c = c.gsc00c and a.gth21c = c.gsc01c and c.gsc02c = 'gz' and isnull(a.gth21c, '') <> '' insert #gsm03_detail(compid, billid, billtype, loop_cnt, cardid, code, paycode, payamt, times, pctimes) select gcl00c, gcl01c, 'gz', 3, gcl03c, gtf02c, gsc04c, isnull(gsc05f,0), --isnull(gtf24f, 0), gtf04f, dbo.fun_divide_default(isnull(gtf16f, 0), isnull(gtf10f, 0) + isnull(gtf12f, 0) + isnull(gtf14f, 0) + isnull(gtf16f, 0), 0) from gtc05 a with (nolock), #gcm10 b, gsm03 c with(nolock) where a.gtf00c = b.gcl00c and a.gtf20c = b.gcl01c and a.gtf00c = c.gsc00c and a.gtf20c = c.gsc01c and c.gsc02c = 'gz' and isnull(a.gtf20c, '') <> '' update a set a.cardtype = b.gca02c from #gsm03_detail a, gcm01 b where a.cardid = b.gca01c and b.gca00c = b.gca13d and isnull(a.cardtype,'') = '' and billtype = 'gz'--add by lm 2015-10-10(cardtype) --第一销售人员 insert #card_fill_detail_by_payment(compid, cardtype, cardid, billtype, billid, personid, share_rate, paycode, payamt, filltype, code, times, person_pos, procid, pctimes,perf) select gna00c, cardtype, cardid, 'gn', gna01c, gna20c, isnull(gna27f, 1), paycode, payamt, loop_cnt, code, times, 1, procid, pctimes,gna67f from #gnm01 a with (nolock), #gsm03_detail c where a.gna00c = c.compid and a.gna01c = c.billid and isnull(gna20c, '')<>'' and c.billtype = 'gn' --第二销售人员 insert #card_fill_detail_by_payment(compid, cardtype, cardid, billtype, billid, personid, share_rate, paycode, payamt, filltype, code, times, person_pos, procid, pctimes,perf) select gna00c, cardtype, cardid, 'gn', gna01c, gna28c, isnull(gna29f, 0), paycode, payamt, loop_cnt, code, times, 2, procid, pctimes,gna69f from #gnm01 a with (nolock), #gsm03_detail c where a.gna00c = c.compid and a.gna01c = c.billid and isnull(gna28c, '')<>'' and c.billtype = 'gn' --第三销售人员 insert #card_fill_detail_by_payment(compid, cardtype, cardid, billtype, billid, personid, share_rate, paycode, payamt, filltype, code, times, person_pos, procid, pctimes,perf) select gna00c, cardtype, cardid, 'gn', gna01c, gna30c, isnull(gna31f, 0), paycode, payamt, loop_cnt, code, times, 3, procid, pctimes,gna71f from #gnm01 a with (nolock), #gsm03_detail c where a.gna00c = c.compid and a.gna01c = c.billid and isnull(gna30c, '')<>'' and c.billtype = 'gn' --第四销售人员 insert #card_fill_detail_by_payment(compid, cardtype, cardid, billtype, billid, personid, share_rate, paycode, payamt, filltype, code, times, person_pos, procid, pctimes,perf) select gna00c, cardtype, cardid, 'gn', gna01c, gna32c, isnull(gna33f, 0), paycode, payamt, loop_cnt, code, times, 4, procid, pctimes,gna86f from #gnm01 a with (nolock), #gsm03_detail c where a.gna00c = c.compid and a.gna01c = c.billid and isnull(gna32c, '')<>'' and c.billtype = 'gn' --第五销售人员 insert #card_fill_detail_by_payment(compid, cardtype, cardid, billtype, billid, personid, share_rate, paycode, payamt, filltype, code, times, person_pos, procid, pctimes,perf) select gna00c, cardtype, cardid, 'gn', gna01c, gna34c, isnull(gna35f, 0), paycode, payamt, loop_cnt, code, times, 5, procid, pctimes,gna87f from #gnm01 a with (nolock), #gsm03_detail c where a.gna00c = c.compid and a.gna01c = c.billid and isnull(gna34c, '')<>'' and c.billtype = 'gn' --第六销售人员 insert #card_fill_detail_by_payment(compid, cardtype, cardid, billtype, billid, personid, share_rate, paycode, payamt, filltype, code, times, person_pos, procid, pctimes,perf) select gna00c, cardtype, cardid, 'gn', gna01c, gna36c, isnull(gna37f, 0), paycode, payamt, loop_cnt, code, times, 6, procid, pctimes,gna88f from #gnm01 a with (nolock), #gsm03_detail c where a.gna00c = c.compid and a.gna01c = c.billid and isnull(gna36c, '')<>'' and c.billtype = 'gn' --第七销售人员 insert #card_fill_detail_by_payment(compid, cardtype, cardid, billtype, billid, personid, share_rate, paycode, payamt, filltype, code, times, person_pos, procid, pctimes,perf) select gna00c, cardtype, cardid, 'gn', gna01c, gna38c, isnull(gna39f, 0), paycode, payamt, loop_cnt, code, times, 7, procid, pctimes,gna89f from #gnm01 a with (nolock), #gsm03_detail c where a.gna00c = c.compid and a.gna01c = c.billid and isnull(gna38c, '')<>'' and c.billtype = 'gn' --第八销售人员 insert #card_fill_detail_by_payment(compid, cardtype, cardid, billtype, billid, personid, share_rate, paycode, payamt, filltype, code, times, person_pos, procid, pctimes,perf) select gna00c, cardtype, cardid, 'gn', gna01c, gna40c, isnull(gna41f, 0), paycode, payamt, loop_cnt, code, times, 8, procid, pctimes,gna90f from #gnm01 a with (nolock), #gsm03_detail c where a.gna00c = c.compid and a.gna01c = c.billid and isnull(gna40c, '')<>'' and c.billtype = 'gn' --第九销售人员 insert #card_fill_detail_by_payment(compid, cardtype, cardid, billtype, billid, personid, share_rate, paycode, payamt, filltype, code, times, person_pos, procid, pctimes,perf) select gna00c, cardtype, cardid, 'gn', gna01c, gna42c, isnull(gna43f, 0), paycode, payamt, loop_cnt, code, times, 9, procid, pctimes,gna91f from #gnm01 a with (nolock), #gsm03_detail c where a.gna00c = c.compid and a.gna01c = c.billid and isnull(gna42c, '')<>'' and c.billtype = 'gn' --第十销售人员 insert #card_fill_detail_by_payment(compid, cardtype, cardid, billtype, billid, personid, share_rate, paycode, payamt, filltype, code, times, person_pos, procid, pctimes,perf) select gna00c, cardtype, cardid, 'gn', gna01c, gna44c, isnull(gna45f, 0), paycode, payamt, loop_cnt, code, times, 10, procid, pctimes,gna92f from #gnm01 a with (nolock), #gsm03_detail c where a.gna00c = c.compid and a.gna01c = c.billid and isnull(gna44c, '')<>'' and c.billtype = 'gn' --第一销售人员 insert #card_fill_detail_by_payment(compid, cardtype, cardid, billtype, billid, personid, share_rate, paycode, payamt, filltype, code, times, person_pos, procid, pctimes,perf) select gcl00c, cardtype, cardid, 'gz', gcl01c, gcl09c, isnull(gcl16f, 1), paycode, payamt, loop_cnt, code, times, 1, procid, pctimes,gcl58f from #gcm10 a with (nolock), #gsm03_detail c where a.gcl00c = c.compid and a.gcl01c = c.billid and isnull(gcl09c, '')<>'' and c.billtype = 'gz' --第二销售人员 insert #card_fill_detail_by_payment(compid, cardtype, cardid, billtype, billid, personid, share_rate, paycode, payamt, filltype, code, times, person_pos, procid, pctimes,perf) select gcl00c, cardtype, cardid, 'gz', gcl01c, gcl17c, isnull(gcl18f, 0), paycode, payamt, loop_cnt, code, times, 2, procid, pctimes,gcl60f from #gcm10 a with (nolock), #gsm03_detail c where a.gcl00c = c.compid and a.gcl01c = c.billid and isnull(gcl17c, '')<>'' and c.billtype = 'gz' --第三销售人员 insert #card_fill_detail_by_payment(compid, cardtype, cardid, billtype, billid, personid, share_rate, paycode, payamt, filltype, code, times, person_pos, procid, pctimes,perf) select gcl00c, cardtype, cardid, 'gz', gcl01c, gcl19c, isnull(gcl20f, 0), paycode, payamt, loop_cnt, code, times, 3, procid, pctimes,gcl62f from #gcm10 a with (nolock), #gsm03_detail c where a.gcl00c = c.compid and a.gcl01c = c.billid and isnull(gcl19c, '')<>'' and c.billtype = 'gz' --第四销售人员 insert #card_fill_detail_by_payment(compid, cardtype, cardid, billtype, billid, personid, share_rate, paycode, payamt, filltype, code, times, person_pos, procid, pctimes,perf) select gcl00c, cardtype, cardid, 'gz', gcl01c, gcl22c, isnull(gcl23f, 0), paycode, payamt, loop_cnt, code, times, 4, procid, pctimes,gcl83f from #gcm10 a with (nolock), #gsm03_detail c where a.gcl00c = c.compid and a.gcl01c = c.billid and isnull(gcl22c, '')<>'' and c.billtype = 'gz' --第五销售人员 insert #card_fill_detail_by_payment(compid, cardtype, cardid, billtype, billid, personid, share_rate, paycode, payamt, filltype, code, times, person_pos, procid, pctimes,perf) select gcl00c, cardtype, cardid, 'gz', gcl01c, gcl24c, isnull(gcl25f, 0), paycode, payamt, loop_cnt, code, times, 5, procid, pctimes,gcl84f from #gcm10 a with (nolock), #gsm03_detail c where a.gcl00c = c.compid and a.gcl01c = c.billid and isnull(gcl24c, '')<>'' and c.billtype = 'gz' --第六销售人员 insert #card_fill_detail_by_payment(compid, cardtype, cardid, billtype, billid, personid, share_rate, paycode, payamt, filltype, code, times, person_pos, procid, pctimes,perf) select gcl00c, cardtype, cardid, 'gz', gcl01c, gcl26c, isnull(gcl27f, 0), paycode, payamt, loop_cnt, code, times, 6, procid, pctimes,gcl85f from #gcm10 a with (nolock), #gsm03_detail c where a.gcl00c = c.compid and a.gcl01c = c.billid and isnull(gcl26c, '')<>'' and c.billtype = 'gz' --第七销售人员 insert #card_fill_detail_by_payment(compid, cardtype, cardid, billtype, billid, personid, share_rate, paycode, payamt, filltype, code, times, person_pos, procid, pctimes,perf) select gcl00c, cardtype, cardid, 'gz', gcl01c, gcl28c, isnull(gcl29f, 0), paycode, payamt, loop_cnt, code, times, 7, procid, pctimes,gcl86f from #gcm10 a with (nolock), #gsm03_detail c where a.gcl00c = c.compid and a.gcl01c = c.billid and isnull(gcl28c, '')<>'' and c.billtype = 'gz' --第八销售人员 insert #card_fill_detail_by_payment(compid, cardtype, cardid, billtype, billid, personid, share_rate, paycode, payamt, filltype, code, times, person_pos, procid, pctimes,perf) select gcl00c, cardtype, cardid, 'gz', gcl01c, gcl30c, isnull(gcl31f, 0), paycode, payamt, loop_cnt, code, times, 8, procid, pctimes,gcl87f from #gcm10 a with (nolock), #gsm03_detail c where a.gcl00c = c.compid and a.gcl01c = c.billid and isnull(gcl30c, '')<>'' and c.billtype = 'gz' --第九销售人员 insert #card_fill_detail_by_payment(compid, cardtype, cardid, billtype, billid, personid, share_rate, paycode, payamt, filltype, code, times, person_pos, procid, pctimes,perf) select gcl00c, cardtype, cardid, 'gz', gcl01c, gcl32c, isnull(gcl33f, 0), paycode, payamt, loop_cnt, code, times, 9, procid, pctimes,gcl88f from #gcm10 a with (nolock), #gsm03_detail c where a.gcl00c = c.compid and a.gcl01c = c.billid and isnull(gcl32c, '')<>'' and c.billtype = 'gz' --第十销售人员 insert #card_fill_detail_by_payment(compid, cardtype, cardid, billtype, billid, personid, share_rate, paycode, payamt, filltype, code, times, person_pos, procid, pctimes,perf) select gcl00c, cardtype, cardid, 'gz', gcl01c, gcl34c, isnull(gcl35f, 0), paycode, payamt, loop_cnt, code, times, 10, procid, pctimes,gcl89f from #gcm10 a with (nolock), #gsm03_detail c where a.gcl00c = c.compid and a.gcl01c = c.billid and isnull(gcl34c, '')<>'' and c.billtype = 'gz' --end delete a from card_fill_detail_by_payment a with(nolock), #card_fill_detail_by_payment b where a.billtype = b.billtype and a.billid = b.billid and a.compid = b.compid --更新提成方案代码 update a set a.perf_code = b.ppt02c from #card_fill_detail_by_payment a, personprecept b with(nolock) where a.compid = b.ppt00c and a.personid = b.ppt03c and isnull(ppt04i, 0) = 1 and isnull(ppt11i, 0) = 0 --更新卡销售业绩比率 update a set a.settype = b.car03i, a.setvalue = b.car04f from #card_fill_detail_by_payment a, cardprecept b with(nolock) where a.compid = b.car00c and a.cardtype = b.car01c and a.perf_code = b.car02c and a.billtype = 'gn' and a.filltype = 1 --更新卡充值业绩比率 update a set a.settype = b.car07i, a.setvalue = b.car08f from #card_fill_detail_by_payment a, cardprecept b with(nolock) where a.compid = b.car00c and a.cardtype = b.car01c and a.perf_code = b.car02c and a.billtype = 'gz' and a.filltype = 1 --更新套餐业绩比率 update a set a.settype = b.gta07i, a.setvalue = b.gta08f from #card_fill_detail_by_payment a, gtc01 b with(nolock) where a.compid = b.gta00c and a.code = b.gta01c and a.filltype = 3 --更新卡销售销售业绩折算比率 update a set a.pay_ratio = b.gso05f from #card_fill_detail_by_payment a, gsm15 b with(nolock) where a.compid = b.gso00c and a.paycode = b.gso02c and b.gso01c = '3' declare @type int -- 业绩方式 declare @setvalue float -- 业绩比率 declare @perf_code varchar(20) -- 提成方案代码 declare @pay_ratio float -- 卡销售销售业绩折算比率 declare @personid varchar(20) declare @code varchar(20) declare @cardid varchar(20) declare @cardtype varchar(20) declare @comp varchar(20) declare @person_pos int declare @payamt float declare @times float declare @procid float declare @filltype int declare @share_rate float declare @paycode varchar(20) declare @flag int declare @perf float declare @trueperf float declare @comm float declare @pctimes float declare @billid varchar(20) declare @billtype varchar(5) declare @id int declare @state int set @state = 0 while(@state = 0) begin if exists(select 1 from #card_fill_detail_by_payment) begin select top 1 @id = idd, @personid = personid, @code = code, @cardtype = cardtype, @payamt = payamt, @times = times, @filltype = filltype, @share_rate = share_rate, @paycode = paycode, @procid = procid, @pctimes = pctimes,@cardid = cardid, @person_pos = person_pos, @billtype = billtype, @billid = billid, @comp = compid, @type = settype, @setvalue = setvalue,@perf_code = perf_code, @pay_ratio = pay_ratio,@perf = isnull(perf,0) from #card_fill_detail_by_payment if @filltype = 1 -- 卡销售或充值 begin if @billtype = 'gn' select @flag = 1 else if @billtype = 'gz' select @flag = 0 end else begin select @flag = @filltype end if(@SP04802 <> 1) begin set @perf = 0 -- 计算业绩 exec upg_cal_cardfill_perf_ceshi @comp, @personid, @code, @cardtype, @payamt, @payamt, @times, @pctimes, @procid, @flag, @share_rate, @paycode, @billtype, @billid, @perf output, @type , @setvalue ,@perf_code , @pay_ratio end -- 计算提成 exec upg_cal_cardsale_comm @comp, @cardtype, @code, @payamt, @payamt, @perf, @times,@pctimes, @procid, @personid, @flag, @comm output, @trueperf output, @share_rate, @paycode insert #card_fill_detail_by_payment_2(compid,billtype,billid,personid,share_rate,paycode,payamt,filltype, cardid,cardtype,code,times,procid,perf,person_pos,comm,pctimes) select @comp,@billtype,@billid,@personid,@share_rate,@paycode,@payamt,@filltype, @cardid,@cardtype,@code,@times,@procid,@perf,@person_pos,@comm,@pctimes set @state = 0 set @comp = 0 set @personid = '' set @code = '' set @cardtype = '' set @payamt = 0 set @pctimes = 0 set @procid = '' set @flag = 0 set @share_rate = 0 set @billtype = '' set @billid = '' set @perf = 0 set @type = null set @setvalue = null set @perf_code = '' set @pay_ratio = 0 set @comm = 0 delete from #card_fill_detail_by_payment where idd = @id end else begin set @state = 1 end end --start 如果是手动输入业绩,计算完提成 不需要赋值业绩 if isnull(@SP04802, '') = '1' return -- insert card_fill_detail_by_payment(compid,billtype,billid,personid,share_rate,paycode,payamt,filltype, cardid,cardtype,code,times,procid,perf,person_pos,comm,pctimes) select compid,billtype,billid,personid,share_rate,paycode,payamt,filltype, cardid,cardtype,code,times,procid,perf,person_pos,comm,pctimes from #card_fill_detail_by_payment_2 a with (nolock) insert #tbl_bill_perf(compid, billtype, billid, person_pos, perf) select compid, billtype, billid, person_pos, sum(perf) from #card_fill_detail_by_payment_2 a with (nolock) group by compid, billtype, billid, personid,person_pos update a set a.gna67f = b.perf from gnm01 a with(nolock), #tbl_bill_perf b where a.gna00c = b.compid and a.gna01c = b.billid and b.billtype ='gn' and b.person_pos = 1 -- 第一销售的位置 update a set a.gna69f = b.perf from gnm01 a with(nolock), #tbl_bill_perf b where a.gna00c = b.compid and a.gna01c = b.billid and b.billtype ='gn' and b.person_pos = 2 -- 第2销售的位置 update a set a.gna71f = b.perf from gnm01 a with(nolock), #tbl_bill_perf b where a.gna00c = b.compid and a.gna01c = b.billid and b.billtype ='gn' and b.person_pos = 3 -- 第3销售的位置 update a set a.gna86f = b.perf from gnm01 a with(nolock), #tbl_bill_perf b where a.gna00c = b.compid and a.gna01c = b.billid and b.billtype ='gn' and b.person_pos = 4 -- 第4销售的位置 update a set a.gna87f = b.perf from gnm01 a, #tbl_bill_perf b where a.gna00c = b.compid and a.gna01c = b.billid and b.billtype ='gn' and b.person_pos = 5 -- 第5销售的位置 update a set a.gna88f = b.perf from gnm01 a with(nolock), #tbl_bill_perf b where a.gna00c = b.compid and a.gna01c = b.billid and b.billtype ='gn' and b.person_pos = 6 -- 第6销售的位置 update a set a.gna89f = b.perf from gnm01 a, #tbl_bill_perf b where a.gna00c = b.compid and a.gna01c = b.billid and b.billtype ='gn' and b.person_pos = 7 -- 第7销售的位置 update a set a.gna90f = b.perf from gnm01 a with(nolock), #tbl_bill_perf b where a.gna00c = b.compid and a.gna01c = b.billid and b.billtype ='gn' and b.person_pos = 8 -- 第8销售的位置 update a set a.gna91f = b.perf from gnm01 a, #tbl_bill_perf b where a.gna00c = b.compid and a.gna01c = b.billid and b.billtype ='gn' and b.person_pos = 9 -- 第9销售的位置 update a set a.gna92f = b.perf from gnm01 a with(nolock), #tbl_bill_perf b where a.gna00c = b.compid and a.gna01c = b.billid and b.billtype ='gn' and b.person_pos = 10 -- 第十销售的位置 update a set a.gcl58f = b.perf from gcm10 a, #tbl_bill_perf b where a.gcl00c = b.compid and a.gcl01c = b.billid and b.billtype ='gz' and b.person_pos = 1 -- 第一销售的位置 update a set a.gcl60f = b.perf from gcm10 a with(nolock), #tbl_bill_perf b where a.gcl00c = b.compid and a.gcl01c = b.billid and b.billtype ='gz' and b.person_pos = 2 -- 第2销售的位置 update a set a.gcl62f = b.perf from gcm10 a, #tbl_bill_perf b where a.gcl00c = b.compid and a.gcl01c = b.billid and b.billtype ='gz' and b.person_pos = 3 -- 第3销售的位置 update a set a.gcl83f = b.perf from gcm10 a with(nolock), #tbl_bill_perf b where a.gcl00c = b.compid and a.gcl01c = b.billid and b.billtype ='gz' and b.person_pos = 4 -- 第4销售的位置 update a set a.gcl84f = b.perf from gcm10 a with(nolock), #tbl_bill_perf b where a.gcl00c = b.compid and a.gcl01c = b.billid and b.billtype ='gz' and b.person_pos = 5 -- 第5销售的位置 update a set a.gcl85f = b.perf from gcm10 a with(nolock), #tbl_bill_perf b where a.gcl00c = b.compid and a.gcl01c = b.billid and b.billtype ='gz' and b.person_pos = 6 -- 第6销售的位置 update a set a.gcl86f = b.perf from gcm10 a with(nolock), #tbl_bill_perf b where a.gcl00c = b.compid and a.gcl01c = b.billid and b.billtype ='gz' and b.person_pos = 7 -- 第7销售的位置 update a set a.gcl87f = b.perf from gcm10 a with(nolock), #tbl_bill_perf b where a.gcl00c = b.compid and a.gcl01c = b.billid and b.billtype ='gz' and b.person_pos = 8 -- 第8销售的位置 update a set a.gcl88f = b.perf from gcm10 a, #tbl_bill_perf b where a.gcl00c = b.compid and a.gcl01c = b.billid and b.billtype ='gz' and b.person_pos = 9 -- 第9销售的位置 update a set a.gcl89f = b.perf from gcm10 a with(nolock), #tbl_bill_perf b where a.gcl00c = b.compid and a.gcl01c = b.billid and b.billtype ='gz' and b.person_pos = 10 -- 第十销售的位置 --start --判断充值 是不是 疗程账户还款 -- 疗程账户还款需要加上乘上比率;原因是前面在算疗程还款时,因为多个支付方式导致#tbl_bill_perf表中数据重复 update a set a.gcl58f = b.perf * gcl16f from gcm10 a, #tbl_bill_perf b where a.gcl00c = b.compid and a.gcl01c = b.billid and b.billtype ='gz' and b.person_pos = 1 -- 第一销售的位置 and a.gcl04c = '4' and a.gcl06i = 6 update a set a.gcl60f = b.perf * gcl18f from gcm10 a with(nolock), #tbl_bill_perf b where a.gcl00c = b.compid and a.gcl01c = b.billid and b.billtype ='gz' and b.person_pos = 2 -- 第2销售的位置 and a.gcl04c = '4' and a.gcl06i = 6 update a set a.gcl62f = b.perf * gcl20f from gcm10 a, #tbl_bill_perf b where a.gcl00c = b.compid and a.gcl01c = b.billid and b.billtype ='gz' and b.person_pos = 3 -- 第3销售的位置 and a.gcl04c = '4' and a.gcl06i = 6 update a set a.gcl83f = b.perf* gcl23f from gcm10 a with(nolock), #tbl_bill_perf b where a.gcl00c = b.compid and a.gcl01c = b.billid and b.billtype ='gz' and b.person_pos = 4 -- 第4销售的位置 and a.gcl04c = '4' and a.gcl06i = 6 update a set a.gcl84f = b.perf * gcl25f from gcm10 a with(nolock), #tbl_bill_perf b where a.gcl00c = b.compid and a.gcl01c = b.billid and b.billtype ='gz' and b.person_pos = 5 -- 第5销售的位置 and a.gcl04c = '4' and a.gcl06i = 6 update a set a.gcl85f = b.perf* gcl27f from gcm10 a with(nolock), #tbl_bill_perf b where a.gcl00c = b.compid and a.gcl01c = b.billid and b.billtype ='gz' and b.person_pos = 6 -- 第6销售的位置 and a.gcl04c = '4' and a.gcl06i = 6 update a set a.gcl86f = b.perf * gcl29f from gcm10 a with(nolock), #tbl_bill_perf b where a.gcl00c = b.compid and a.gcl01c = b.billid and b.billtype ='gz' and b.person_pos = 7 -- 第7销售的位置 and a.gcl04c = '4' and a.gcl06i = 6 update a set a.gcl87f = b.perf * gcl31f from gcm10 a with(nolock), #tbl_bill_perf b where a.gcl00c = b.compid and a.gcl01c = b.billid and b.billtype ='gz' and b.person_pos = 8 -- 第8销售的位置 and a.gcl04c = '4' and a.gcl06i = 6 update a set a.gcl88f = b.perf * gcl33f from gcm10 a, #tbl_bill_perf b where a.gcl00c = b.compid and a.gcl01c = b.billid and b.billtype ='gz' and b.person_pos = 9 -- 第9销售的位置 and a.gcl04c = '4' and a.gcl06i = 6 update a set a.gcl89f = b.perf * gcl35f from gcm10 a with(nolock), #tbl_bill_perf b where a.gcl00c = b.compid and a.gcl01c = b.billid and b.billtype ='gz' and b.person_pos = 10 -- 第十销售的位置 and a.gcl04c = '4' and a.gcl06i = 6 --end set nocount off drop table #gsm03_detail drop table #tbl_bill_perf drop table #gnm01 drop table #gcm10 drop table #card_fill_detail_by_payment_2 drop table #card_fill_detail_by_payment drop table #resetverp drop table #temp_comp end GO /****** Object: StoredProcedure [dbo].[upg_recal_bill_discount] Script Date: 06/12/2017 06:20:31 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_recal_bill_discount] ( @compid varchar(10), -- 公司别 @enterid varchar(20) -- 入院单 ) as begin declare @kind varchar(5) -- 类别(1-项目, 2-产品, 3-会员卡) declare @code varchar(5) -- 代码(项目类别,产品类别,会员卡类别 declare @cardid varchar(20) -- 会员卡号 declare @curdate varchar(10) --当前日期 declare @curtime varchar(10) -- 当前时间 declare @accttype int -- 账户 declare @discount float declare @qty float, @stdprice float, @price float, @amt float, @sp055f float, @should_amt float declare @ggm01_accttype int declare @paymode varchar(10) declare @total_amt float, @ggm02_total float, @ggm03_total float select @ggm02_total = 0, @ggm03_total = 0 -- 先确定系统是按哪种支付方式 select @sp055f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP055' --重新计算ggm02的折扣 declare cur_each_ggm02 cursor for select ggb03c, ggb05f, ggb08f, ggb09f, ggb10f, ggb11f, ggb24f, ggb27c, gga09c, gga02d, gga03t, gga27i from ggm02 b, ggm01 a where b.ggb00c = @compid and b.ggb00c = a.gga00c and b.ggb01c = a.gga01c and a.gga30c = @enterid for update of ggb09f, ggb10f, ggb11f, ggb24f, ggb27c open cur_each_ggm02 fetch cur_each_ggm02 into @code, @qty, @stdprice, @discount, @price, @amt, @should_amt, @paymode, @cardid, @curdate, @curtime, @ggm01_accttype while @@fetch_status = 0 begin if (@sp055f = 0) begin --按项目支付 select @accttype = convert(int, gsn02c) from gsm14 where gsn01c = @paymode end else if (@sp055f = 1) begin --按单据支付 select @accttype = @ggm01_accttype end exec upg_get_discount_detail @compid, '1', @code, @cardid, @curdate, @curtime, @accttype, @discount output if @discount<2 begin update ggm02 set ggb09f = @discount, ggb10f = @stdprice*@discount, ggb11f = @stdprice*@discount*@qty, ggb24f = @stdprice*@discount*@qty where current of cur_each_ggm02 end else begin select @discount = 1 update ggm02 set ggb27c = '1' where current of cur_each_ggm02 end select @ggm02_total = @ggm02_total + @stdprice*@discount*@qty fetch cur_each_ggm02 into @code, @qty, @stdprice, @discount, @price, @amt, @should_amt, @paymode, @cardid, @curdate, @curtime, @ggm01_accttype end close cur_each_ggm02 deallocate cur_each_ggm02 --重新计算ggm03的折扣 declare cur_each_ggm03 cursor for select ggc03c, ggc05f, ggc08f, ggc09f, ggc10f, ggc11f, ggc20f, ggc27c, gga09c, gga02d, gga03t, gga27i from ggm03 b, ggm01 a where b.ggc00c = @compid and b.ggc00c = a.gga00c and b.ggc01c = a.gga01c and a.gga30c = @enterid for update of ggc09f, ggc10f, ggc11f, ggc20f, ggc27c open cur_each_ggm03 fetch cur_each_ggm03 into @code, @qty, @stdprice, @discount, @price, @amt, @should_amt, @paymode, @cardid, @curdate, @curtime, @ggm01_accttype while @@fetch_status = 0 begin if (@sp055f = 0) begin --按项目支付 select @accttype = convert(int, gsn02c) from gsm14 where gsn01c = @paymode end else if (@sp055f = 1) begin --按单据支付 select @accttype = @ggm01_accttype end exec upg_get_discount_detail @compid, '2', @code, @cardid, @curdate, @curtime, @accttype, @discount output if @discount<2 begin update ggm03 set ggc09f = @discount, ggc10f = @stdprice*@discount, ggc11f = @stdprice*@discount*@qty, ggc20f = @stdprice*@discount*@qty where current of cur_each_ggm03 end else begin select @discount = 1 update ggm03 set ggc27c = '1' where current of cur_each_ggm03 end select @ggm03_total = @ggm03_total + @stdprice*@discount*@qty fetch cur_each_ggm03 into @code, @qty, @stdprice, @discount, @price, @amt, @should_amt, @paymode, @cardid, @curdate, @curtime, @ggm01_accttype end close cur_each_ggm03 deallocate cur_each_ggm03 --更新ggm01中总的合计以及glm01中总的合计 select @total_amt = isnull(@ggm02_total, 0) + isnull(@ggm03_total, 0) --如果对应多个收银单的时候可能就更新的错误了 create table #sum_bill_amt_2( billid varchar(20) not null, amt float null) create table #sum_bill_amt_3( billid varchar(20) not null, amt float null) insert #sum_bill_amt_2(billid, amt) select ggb01c, sum(ggb11f) from ggm02 b with (nolock), ggm01 a with (nolock) where b.ggb00c = a.gga00c and b.ggb01c = a.gga01c and a.gga00c = @compid and a.gga30c = @enterid group by ggb01c insert #sum_bill_amt_3(billid, amt) select ggc01c, sum(ggc11f) from ggm03 b with (nolock), ggm01 a with (nolock) where b.ggc00c = a.gga00c and b.ggc01c = a.gga01c and a.gga00c = @compid and a.gga30c = @enterid group by ggc01c --可能没有项目, 这个时候, gga16f就没有被重新设定 update a set a.gga16f = 0 from ggm01 a where a.gga00c = @compid and a.gga30c = @enterid update a set a.gga16f = isnull(b.amt, 0) from ggm01 a, #sum_bill_amt_2 b where a.gga00c = @compid and a.gga01c = b.billid update a set a.gga16f = isnull(a.gga16f, 0) + isnull(b.amt, 0) from ggm01 a, #sum_bill_amt_3 b where a.gga00c = @compid and a.gga01c = b.billid drop table #sum_bill_amt_2 drop table #sum_bill_amt_3 --可能是一个入院单对应的几个收银单的钥匙牌号有几个 update glm03 set glc03f = (select sum(isnull(gga16f, 0)) from ggm01 where gga00c = glc00c and gga08c = glc02c and gga30c = glc01c), glc04f = 0, glc06f = 0 where glc00c = @compid and glc01c = @enterid update glm01 set gla11f = @total_amt, gla20f = 0 where gla00c = @compid and gla01c = @enterid end GO /****** Object: StoredProcedure [dbo].[upg_rename_cardid_ex] Script Date: 06/12/2017 06:20:35 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_rename_cardid_ex] ( @oldcardid varchar(20), @newcardid varchar(20), @userid varchar(20), @curdate varchar(10), @curtime varchar(10) ) as begin declare @sp039 varchar(20) select @sp039 = gsa02c from gsm01 where gsa00c = '001' and gsa01c = 'SP039' if @sp039 = '1' begin -- 需要强制一致 exec upg_rename_card @oldcardid, @newcardid, @userid, @curdate, @curtime end else begin -- 只是修改会员卡号 exec upg_rename_card_only @oldcardid, @newcardid, @userid, @curdate, @curtime end end GO /****** Object: StoredProcedure [dbo].[upg_compute_all_perf_produce_ceshi] Script Date: 06/12/2017 06:19:43 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_compute_all_perf_produce_ceshi] ( @compid varchar(10), @fromdate varchar(8), @todate varchar(8) -- 公司别 ) as--WUXM, 2005/07/21 begin set nocount on declare @billid varchar(20) declare @inner_compid varchar(20) declare @ggc02f float declare @cardno varchar(20) declare @ddate varchar(20) declare @cardtype varchar(20) declare @id int declare @sate int set @sate = 0 declare @input_good_perf float declare @card_kind int -- 主要为了识别是否为是否为资格卡 declare @old_good_ex_rate_points float -- 物品积分折算比率 declare @perf_rate_good_gam10 float -- 业绩折算比率,产品 declare @cash_good_rate float declare @staff_1 varchar(20) -- 第一销售 declare @staff_2 varchar(20) -- 第二销售 declare @staff_3 varchar(20) -- 第三销售 declare @good varchar(20) -- 产品代码 declare @standardprice float -- 标准价 declare @price float -- 实际价 declare @amt float -- 项目总价 declare @quan float -- 数量 declare @ratio_1 float -- 第一销售分享比率 declare @ratio_2 float -- 第二销售分享比率 declare @ratio_3 float -- 第三销售分享比率 declare @paymode varchar(20) -- 支付方式, 但按单据支付的时候, 没有此值 declare @cost float -- 最后计算出来的成本 declare @in_cost float declare @in_cost_unit float declare @p_mode int declare @p_setvalue float declare @vmode1 int declare @vsetvalue1 float declare @vmode2 int declare @vsetvalue2 float declare @vmode3 int declare @vsetvalue3 float declare @fa_type1 nvarchar(20) declare @fa_type2 nvarchar(20) declare @fa_type3 nvarchar(20) declare @pos_reduce_rate float -- 银行卡支付的时候需要调整的折算比率 select @input_good_perf = gsa03f from gsm01 with(nolock) where gsa00c = @compid and gsa01c = 'SP135' create table #gfm01 -- 产品资料档 ( gfa00c varchar(10) not null, -- 门店编号 gfa01c varchar(20) not null, -- 产品编号 gfa09f float null, -- 销售单位对应消耗单位数量 gfa22f float null, -- 产品进价(标准单位) gfa25i tinyint null, -- 积分方式 gfa26f float null, -- 积分或比率 gfa19f float null, -- 销售成本(标准单位) ) create table #ggm01_ggm03 ( ggc00c varchar(10) not null, --门店编号 ggc01c varchar(20) not null, --消费单号 gga02d varchar(20) null,--消费日期 gga07c varchar(20) null, --卡类别 gga09c varchar(20) null, --会员卡号 ggc02f float not null, --序号 ggc03c varchar(20) null, --产品代码 ggc05f float null, --数量 ggc08f float null, --标准单价 ggc10f float null, --折让单价 ggc11f float null, --金额 ggc12c varchar(20) null, --产品销售人员 ggc14f float null, --第一分享比率 ggc15c varchar(20) null, --第二人 ggc16f float null, --第二分享比率 ggc17c varchar(20) null, --第三人 ggc18f float null, --第三分享比率 ggc27c varchar(5) null, --产品支付方式 ) insert #ggm01_ggm03(ggc00c, ggc01c, gga02d, ggc02f, ggc03c, ggc08f, ggc10f, ggc11f, gga07c, gga09c, ggc05f, ggc14f, ggc16f, ggc18f, ggc27c, ggc12c, ggc15c, ggc17c) select ggc00c, ggc01c, gga02d, ggc02f, ggc03c, ggc08f, ggc10f, ggc11f, gga07c, gga09c, ggc05f, ggc14f, ggc16f, ggc18f, ggc27c, ggc12c, ggc15c, ggc17c from ggm01 a with (nolock), ggm03 b with(nolock),gam26 z with(nolock) where a.gga00c = z.gaz02c and z.gaz01c = @compid and a.gga00c = b.ggc00c and a.gga01c = b.ggc01c and a.gga02d >= @fromdate and a.gga02d <= @todate create table #ggm03_2 ( ggc00c varchar(20) not null, --门店编号 ggc03c varchar(20) null --产品代码 ) insert #ggm03_2(ggc00c, ggc03c) select distinct ggc00c, ggc03c from #ggm01_ggm03 insert #gfm01(gfa00c, gfa01c, gfa09f, gfa22f, gfa25i, gfa26f, gfa19f) select gfa00c, gfa01c, gfa09f, gfa22f, gfa25i, gfa26f, gfa19f from gfm01 with(nolock) where gfa00c = @compid and gfa01c in (select ggc03c from #ggm03_2 where ggc00c = gfa00c ) create table #ggm03 -- 项目消费--产品明细 ( id int identity not null, ggc00c varchar(20) not null, --门店编号 ggc01c varchar(20) not null, --消费单号 gga02d varchar(20) null,--消费日期 gga07c varchar(20) null, --卡类别 gga09c varchar(20) null, --会员卡号 ggc02f float not null, --序号 ggc03c varchar(20) null, --产品代码 ggc05f float null, --数量 ggc08f float null, --标准单价 ggc10f float null, --折让单价 ggc11f float null, --金额 ggc12c varchar(20) null, --产品销售人员 ggc14f float null, --第一分享比率 ggc15c varchar(20) null, --第二人 ggc16f float null, --第二分享比率 ggc17c varchar(20) null, --第三人 ggc18f float null, --第三分享比率 ggc27c varchar(5) null, --产品支付方式 gak24f float null, --产品实际营业额折算比率 gak03i int null, --大类(1-储值卡, 2- 资格卡,3-计次卡,4-单次卡) gak34f float null, --产品消费积分折算比率 gak46f float null, --业绩这算比率,产品 gca25f float null, --实际营业额折算比率调整率 fa_type1 nvarchar(20) null, fa_type2 nvarchar(20) null, fa_type3 nvarchar(20) null, vmode1 int null, vsetvalue1 float null, vmode2 int null, vsetvalue2 float null, vmode3 int null, vsetvalue3 float null, cost float null, -- 最后计算出来的成 in_cost float null, in_cost_unit float null, p_mode int null, p_setvalue float null, ) insert #ggm03(ggc00c, ggc01c, gga02d, ggc02f, ggc03c, ggc08f, ggc10f, ggc11f, gga07c, gga09c, ggc05f, ggc14f, ggc16f, ggc18f, ggc27c, ggc12c, ggc15c, ggc17c, gak24f, gak03i, gak34f, gak46f, gca25f, cost, in_cost, in_cost_unit, p_mode, p_setvalue) select ggc00c, ggc01c, gga02d, ggc02f, ggc03c, ggc08f, ggc10f, ggc11f, gga07c, gga09c, ggc05f, ggc14f, ggc16f, ggc18f, ggc27c, ggc12c, ggc15c, ggc17c, gak24f, gak03i, gak34f, gak46f, gca25f, gfa19f * ggc05f,gfa22f *ggc05f, gfa22f, gfa25i, gfa26f from #ggm01_ggm03 a with (nolock) left join gam10 b with(nolock) on a.ggc00c = b.gak00c and a.gga07c = b.gak01c left join gcm01 c with(nolock) on a.ggc00c = c.gca01c and gca00c = gca13d left join #gfm01 f on a.ggc00c = f.gfa00c and a.ggc03c = f.gfa01c create table #personprecept_c ( ppt00c varchar(10) not null, -- 公司别 ppt02c varchar(20) not null, -- 方案代码 ppt03c varchar(20) not null, -- 员工代码 ) create table #productprecept ( pro00c varchar(10) not null, -- 公司别 pro01c varchar(20) not null, -- 产品代码 pro02c varchar(20) not null, -- 方案代码 pro03i int null, -- 业绩方式 pro04f float null, -- 业绩比率或金额 pro10c varchar(100) not null, -- 支付方式 ) --员工提成方案 insert #personprecept_c(ppt00c, ppt02c, ppt03c) select ppt00c, ppt02c, ppt03c from personprecept with(nolock) where ppt00c = @compid and ppt04i = 1 and ppt02c in (select pre01c from precept where pre00c = @compid and isnull(pre22i, 0) = 0) insert #productprecept(pro00c, pro01c, pro02c, pro03i, pro04f, pro10c) select pro00c, pro01c, pro02c, pro03i, pro04f, pro10c from productprecept with(nolock) where pro00c = @compid and pro01c in (select ggc03c from #ggm01_ggm03 where ggc00c = pro00c ) update a set a.fa_type1 = ppt02c from #ggm03 a inner join #personprecept_c b on a.ggc00c = b.ppt00c and a.ggc12c = b.ppt03c where isnull(a.ggc12c,'') <> '' update a set a.fa_type2 = ppt02c from #ggm03 a inner join #personprecept_c b on a.ggc00c = b.ppt00c and a.ggc15c = b.ppt03c where isnull(a.ggc15c,'') <> '' update a set a.fa_type3 = ppt02c from #ggm03 a inner join #personprecept_c b on a.ggc00c = b.ppt00c and a.ggc17c = b.ppt03c where isnull(a.ggc17c,'') <> '' update a set a.vmode1 = pro03i, a.vsetvalue1 = pro04f from #ggm03 a inner join #productprecept b on a.ggc00c = b.pro00c and a.ggc03c = b.pro01c and a.fa_type1 = b.pro02c and a.ggc27c = b.pro10c where isnull(a.fa_type1,'') <> '' update a set a.vmode1 = pro03i, a.vsetvalue1 = pro04f from #ggm03 a inner join #productprecept b on a.ggc00c = b.pro00c and a.ggc03c = b.pro01c and a.fa_type1 = b.pro02c where isnull(a.fa_type1,'') <> '' and isnull(pro10c, '*') = '*' and isnull(a.vmode1,'') = '' update a set a.vmode2 = pro03i, a.vsetvalue2 = pro04f from #ggm03 a inner join #productprecept b on a.ggc00c = b.pro00c and a.ggc03c = b.pro01c and a.fa_type2 = b.pro02c and a.ggc27c = b.pro10c where isnull(a.fa_type2,'') <> '' update a set a.vmode2 = pro03i, a.vsetvalue2 = pro04f from #ggm03 a inner join #productprecept b on a.ggc00c = b.pro00c and a.ggc03c = b.pro01c and a.fa_type2 = b.pro02c where isnull(a.fa_type2,'') <> '' and isnull(pro10c, '*') = '*' and isnull(a.vmode2,'') = '' update a set a.vmode3 = pro03i, a.vsetvalue3 = pro04f from #ggm03 a inner join #productprecept b on a.ggc00c = b.pro00c and a.ggc03c = b.pro01c and a.fa_type3 = b.pro02c and a.ggc27c = b.pro10c where isnull(a.fa_type3,'') <> '' update a set a.vmode3 = pro03i, a.vsetvalue3 = pro04f from #ggm03 a inner join #productprecept b on a.ggc00c = b.pro00c and a.ggc03c = b.pro01c and a.fa_type3 = b.pro02c where isnull(a.fa_type3,'') <> '' and isnull(pro10c, '*') = '*' and isnull(a.vmode3,'') = '' while(@sate = 0) begin if exists(select 1 from #ggm03) begin set @sate = 0 select top 1 @id = id, @inner_compid = ggc00c, @billid = ggc01c, @cardno = gga09c, @ddate = gga02d, @cardtype = gga07c, @cash_good_rate = gak24f,@card_kind = gak03i,@old_good_ex_rate_points = gak34f, @perf_rate_good_gam10 = gak46f, @pos_reduce_rate = gca25f, @good = ggc03c, @standardprice = ggc08f, @price = ggc10f, @amt = ggc11f, @quan = ggc05f, @ratio_1 = ggc14f,@ratio_2 = ggc16f, @ratio_3 = ggc18f, @paymode = ggc27c, @staff_1 = ggc12c, @staff_2 = ggc15c, @staff_3 = ggc17c, @fa_type1 = fa_type1,@fa_type2 = fa_type2,@fa_type3 = fa_type3, @vmode1 = vmode1, @vmode2 = vmode2, @vmode3 = vmode3, @vsetvalue1 = vsetvalue1, @vsetvalue2 = vsetvalue2,@vsetvalue3 = vsetvalue3, @cost = cost,@in_cost = in_cost,@in_cost_unit = in_cost_unit, @p_mode = p_mode, @p_setvalue = p_setvalue, @ggc02f = ggc02f from #ggm03 --计算产品购买的成本与虚业绩 exec upg_compute_cost_perf_produce_ceshi @compid, @billid,@cardtype,@cardno, @ddate,@cash_good_rate, @card_kind ,@old_good_ex_rate_points,@perf_rate_good_gam10,@pos_reduce_rate, @good, @standardprice,@price, @amt,@quan,@ratio_1, @ratio_2, @ratio_3, @paymode,@staff_1,@staff_2, @staff_3,@fa_type1,@fa_type2,@fa_type3, @vmode1, @vsetvalue1,@vmode2,@vsetvalue2, @vmode3 ,@vsetvalue3,@cost,@in_cost,@in_cost_unit,@p_mode,@p_setvalue,@input_good_perf, @ggc02f set @billid = '' set @cardtype = '' set @cardno = '' set @ddate = '' set @cash_good_rate = 0 set @card_kind = '' set @old_good_ex_rate_points = 0 set @perf_rate_good_gam10 = 0 set @good = '' set @standardprice = 0 set @price = 0 set @amt = 0 set @quan = 0 set @ratio_1 = 0 set @ratio_2 = 0 set @ratio_3 = 0 set @paymode = '' set @staff_1 = '' set @staff_2 = '' set @staff_3 = '' set @fa_type1 = '' set @fa_type2 = '' set @fa_type3 = '' set @vmode1 = 0 set @vmode2 = 0 set @vmode3 = 0 set @vsetvalue1 = 0 set @vsetvalue2 = 0 set @vsetvalue3 = 0 set @cost = 0 set @in_cost = 0 set @in_cost_unit = 0 set @p_mode = 0 set @p_setvalue = 0 delete from #ggm03 where id = @id end else begin set @sate = 1 end end set nocount off end GO /****** Object: StoredProcedure [dbo].[upg_compute_all_perf_ceshi] Script Date: 06/12/2017 06:19:43 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_compute_all_perf_ceshi] ( @compid varchar(10), @fromdate varchar(8), @todate varchar(8) -- 公司别 ) as--WUXM, 2005/07/21 begin set nocount on declare @custflag varchar(10) select @custflag = gsa02c from gsm01 with (nolock) where gsa00c = @compid and gsa01c = 'SP109' declare @billid varchar(20) declare @inner_compid varchar(20) declare @cardno varchar(20) declare @ddate varchar(20) declare @cardtype varchar(20) declare @id int declare @sate int set @sate = 0 declare @cash_prj_rate_points float declare @perf_rate_prj_gam10 float -- 业绩折算比率,项目 declare @card_kind int -- 主要为了识别是否为是否为资格卡 declare @pos_reduce_rate float -- 银行卡支付的时候需要调整的折算比率 declare @cash_prj_rate float declare @input_project_perf varchar(10) declare @item varchar(20) -- 项目编码 declare @standardprice float -- 标准价 declare @price float -- 实际价 declare @amt float -- 项目总价 declare @quan float -- 数量 declare @mode_1 varchar(20) declare @mode_2 varchar(20) declare @mode_3 varchar(20) declare @mode_4 varchar(20) declare @staff_1 varchar(20) declare @staff_2 varchar(20) declare @staff_3 varchar(20) declare @staff_4 varchar(20) declare @vperf_1 float declare @vperf_2 float declare @vperf_3 float declare @vperf_4 float -- 大,中,小工的虚业绩 declare @input_vperf1 float declare @input_vperf2 float declare @input_vperf3 float declare @input_vperf4 float -- 输入的大,中,小工项目虚业绩 declare @paymode varchar(20) declare @ggm02_seq float declare @fa_type nvarchar(20) -- 方案类型 declare @fa_type2 nvarchar(20) declare @fa_type3 nvarchar(20) declare @fa_type4 nvarchar(20) declare @costtype1 int --计算成本类型 declare @cost_setvalue1 float --设置成本的值 declare @caltperf1_1 int declare @caltperf1_2 int declare @caltperf1_3 int declare @caltperf1_4 int declare @costtype2 int --计算成本类型 declare @cost_setvalue2 float --设置成本的值 declare @caltperf2_1 int declare @caltperf2_2 int declare @caltperf2_3 int declare @caltperf2_4 int declare @costtype3 int --计算成本类型 declare @cost_setvalue3 float --设置成本的值 declare @caltperf3_1 int declare @caltperf3_2 int declare @caltperf3_3 int declare @caltperf3_4 int declare @costtype4 int --计算成本类型 declare @cost_setvalue4 float --设置成本的值 declare @caltperf4_1 int declare @caltperf4_2 int declare @caltperf4_3 int declare @caltperf4_4 int declare @cardrate float --转卡比率 declare @rmbrate float --转现比率 select @input_project_perf = gsa02c from gsm01 with(nolock) where gsa00c = @compid and gsa01c = 'SP076' create table #hem01 -- 项目业绩/提成 ( hea00c varchar(10) not null, -- 公司别 hea01c varchar(20) not null, -- 收款项目代码 hea02i tinyint null, -- 指定大工方式, 1-固定,2-总金额百分,3-余额百分,4-成本百分 hea03f float null, -- 指定大工比率 hea04i tinyint null, -- 指定中工方式 hea05f float null, -- 指定中工比率 hea06i tinyint null, -- 指定小工方式 hea07f float null, -- 指定小工工比率 hea08i tinyint null, -- 轮班大工方式 hea09f float null, -- 轮班大工提成 hea10i tinyint null, -- 补工大工方式 hea11f float null, -- 补工大工提成 hea12f float null, -- 项目成本 hea13f float null, -- 转卡比率 hea14i tinyint null, -- 轮班中工方式 hea15f float null, -- 轮班中工提成 hea16i tinyint null, -- 轮班小工 hea17f float null, -- 轮班小工提成 hea18i int not null, -- 1,业绩; 2-提成 hea19i int null, -- 成本计算方式 -- 1:按实际价格的比率,2-按标准价格的比率,3-固定成本 -- 4: 按消耗产品的成本,5: 按成本最高者(标准价格的比率与消耗成本比) hea20i tinyint null, -- 补工中工方式 hea21f float null, -- 补工中工提成 hea22i tinyint null, -- 补工小工方式 hea23f float null, -- 补工小工提成 hea24f float null, -- 现金转业绩比率(类似转卡比率) hea25i tinyint null, -- 指定四工方式 hea26f float null, -- 指定四工提成 hea27i tinyint null, -- 轮班四工方式 hea28f float null, -- 轮班四工提成 hea29i int null, -- 大工实业绩是否扣成本 hea30i int null, -- 中工实业绩是否扣成本 hea31i int null, -- 小工实业绩是否扣成本 hea32i int null, -- 四工实业绩是否扣成本 hea33c varchar(20) not null, -- 方案代码 hea34c varchar(10) not null -- 支付方式---只对业绩有效 ) create clustered index idx_sent_perf_hem01 on #hem01(hea00c, hea01c, hea18i,hea33c,hea34c) -- insert #hem01(hea00c,hea01c ,hea02i,hea03f ,hea04i,hea05f ,hea06i,hea07f ,hea08i, -- hea09f,hea10i,hea11f ,hea12f,hea13f,hea14i,hea15f,hea16i,hea17f, -- hea18i,hea19i,hea20i,hea21f,hea22i,hea23f,hea24f,hea25i ,hea26f, -- hea27i,hea28f ,hea29i,hea30i,hea31i,hea32i,hea33c,hea34c) -- select hea00c,hea01c ,hea02i,hea03f ,hea04i,hea05f ,hea06i,hea07f ,hea08i, -- hea09f,hea10i,hea11f ,hea12f,hea13f,hea14i,hea15f,hea16i,hea17f, -- hea18i,hea19i,hea20i,hea21f,hea22i,hea23f,hea24f,hea25i ,hea26f, -- hea27i,hea28f ,hea29i,hea30i,hea31i,hea32i,hea33c,hea34c -- FROM hem01 with(nolock) -- where hea00c = @compid -- and hea18i = 2 create table #ggm01_ggm02 ( gga00c varchar(20) not null,--公司编号 gga01c varchar(40) not null,--消费单号 gga09c varchar(20) null,--会员卡号 gga02d varchar(20) null,--消费日期 gga07c varchar(20) null,--卡类别 ggb03c varchar(20) null, --项目代码 ggb08f float null, --标准单价 ggb10f float null, --折让单价 ggb11f float null, --金额 ggb05f float null, --数量 ggb15c varchar(20) null, --大工类型 ggb16c varchar(20) null, --中工类型 ggb19c varchar(20) null, --小工类型 ggb25c varchar(20) null, --四工类型 ggb12c varchar(20) null, --大工代码 ggb13c varchar(20) null, --中工代码 ggb14c varchar(20) null, --小工代码 ggb26c varchar(20) null, --四工代码 ggb27c varchar(5) null, --项目支付方式 ggb02f float not null, --序号 ggb20f float null, --大工虚业绩 ggb21f float null, --中工虚业绩 ggb22f float null, --小工虚业绩 ggb35f float null, --四工虚业绩 ) create table #ggm01 ( id int identity not null, gga00c varchar(20) not null,--公司编号 gga01c varchar(40) not null,--消费单号 gga09c varchar(20) null,--会员卡号 gga02d varchar(20) null,--消费日期 gga07c varchar(20) null,--卡类别 gak03i int null,--会员卡大类 gak33f float null,--项目消费积分折算比率 gak22f float null,--项目消费实际营业额折算比率 gak45f float null,--业绩这算比率,项目 gca25f float null,--实际营业额折算比率调整率 ggb03c varchar(20) null, --项目代码 ggb08f float null, --标准单价 ggb10f float null, --折让单价 ggb11f float null, --金额 ggb05f float null, --数量 ggb15c varchar(20) null, --大工类型 ggb16c varchar(20) null, --中工类型 ggb19c varchar(20) null, --小工类型 ggb25c varchar(20) null, --四工类型 ggb12c varchar(20) null, --大工代码 ggb13c varchar(20) null, --中工代码 ggb14c varchar(20) null, --小工代码 ggb26c varchar(20) null, --四工代码 ggb27c varchar(5) null, --项目支付方式 ggb02f float not null, --序号 ggb20f float null, --大工虚业绩 ggb21f float null, --中工虚业绩 ggb22f float null, --小工虚业绩 ggb35f float null, --四工虚业绩 fa_type nvarchar(20) null, --方案编号 fa_type2 nvarchar(20) null, --方案编号 fa_type3 nvarchar(20) null, --方案编号 fa_type4 nvarchar(20) null, --方案编号 costtype1 int null, --计算成本类型 cost_setvalue1 float null, --设置成本的值 caltperf1_1 int null, caltperf1_2 int null, caltperf1_3 int null, caltperf1_4 int null, costtype2 int null, --计算成本类型 cost_setvalue2 float null, --设置成本的值 caltperf2_1 int null, caltperf2_2 int null, caltperf2_3 int null, caltperf2_4 int null, costtype3 int null, --计算成本类型 cost_setvalue3 float null, --设置成本的值 caltperf3_1 int null, caltperf3_2 int null, caltperf3_3 int null, caltperf3_4 int null, costtype4 int null, --计算成本类型 cost_setvalue4 float null, --设置成本的值 caltperf4_1 int null, caltperf4_2 int null, caltperf4_3 int null, caltperf4_4 int null, cardrate float null, rmbrate float null, ispay float null --扣成本标志 (0,1) ) create clustered index idx_sent_perf_01 on #ggm01(gga00c,gga01c,ggb02f) create table #personprecept ( ppt00c varchar(10) not null, -- 公司别 ppt02c varchar(20) not null, -- 方案代码 ppt03c varchar(20) not null, -- 员工代码 ) create table #ggm02 ( ggb00c varchar(20) not null,--公司编号 ggb03c varchar(20) null, --项目代码 ) --员工提成方案 insert #personprecept(ppt00c, ppt02c, ppt03c) select ppt00c, ppt02c, ppt03c from personprecept where ppt00c = @compid and ppt04i = 1 and ppt02c in (select pre01c from precept where pre00c = @compid and isnull(pre22i, 0) = 0) insert #ggm01_ggm02(gga00c, gga01c, ggb03c,gga07c, ggb08f,gga09c, ggb10f, ggb11f, ggb05f, ggb15c, ggb16c, ggb19c, ggb25c, ggb12c, ggb13c, ggb14c, ggb26c, ggb27c, ggb02f, ggb20f, ggb21f, ggb22f, ggb35f ) select gga00c, gga01c,ggb03c,gga07c, ggb08f, gga09c, ggb10f, ggb11f, ggb05f, isnull(ggb15c, '2'), isnull(ggb16c, '2'), isnull(ggb19c, '2'), isnull(ggb25c, '2'), ggb12c, ggb13c, ggb14c, ggb26c, ggb27c, ggb02f, ggb20f, ggb21f, ggb22f, ggb35f from ggm01 a with(nolock), ggm02 d with (nolock), gam26 z with (nolock) where a.gga00c = z.gaz02c and z.gaz01c = @compid and a.gga00c = d.ggb00c and a.gga01c = d.ggb01c and gga02d >= @fromdate and gga02d <= @todate insert #ggm01(gga00c, gga01c, gak33f, gak45f, gak03i, gak22f, gca25f, ggb03c, ggb08f, ggb10f, ggb11f, ggb05f, ggb15c, ggb16c, ggb19c, ggb25c, ggb12c, ggb13c, ggb14c, ggb26c, ggb27c, ggb02f, ggb20f, ggb21f, ggb22f, ggb35f, gga07c, gga09c ) select gga00c, gga01c, gak33f, gak45f, gak03i, gak22f, gca25f, ggb03c, ggb08f, ggb10f, ggb11f, ggb05f, isnull(ggb15c, '2'), isnull(ggb16c, '2'), isnull(ggb19c, '2'), isnull(ggb25c, '2'), ggb12c, ggb13c, ggb14c, ggb26c, ggb27c, ggb02f, ggb20f, ggb21f, ggb22f, ggb35f, gga07c, gga09c from #ggm01_ggm02 a LEFT JOIN gam10 b with(nolock) on a.gga07c = b.gak01c and a.gga00c = b.gak00c left join gcm01 c with(nolock) on a.gga00c = c.gca00c and a.gga09c = c.gca01c insert #ggm02(ggb00c, ggb03c) select distinct gga00c, ggb03c from #ggm01_ggm02 insert #hem01(hea00c,hea01c,hea33c,hea34c,hea18i, hea19i,hea12f,hea13f,hea24f,hea29i,hea30i,hea31i,hea32i) select hea00c,hea01c,hea33c,hea34c,hea18i, hea19i,hea12f,hea13f,hea24f,hea29i,hea30i,hea31i,hea32i FROM hem01 a with(nolock) where hea00c = @compid and hea18i = 2 and hea01c in (select ggb03c from #ggm02 where ggb00c = hea00c) update a set a.fa_type = ppt02c from #ggm01 a inner join #personprecept b on a.gga00c = b.ppt00c and a.ggb12c = b.ppt03c where isnull(a.ggb12c,'') <> '' update a set a.fa_type2 = ppt02c from #ggm01 a inner join #personprecept b on a.gga00c = b.ppt00c and a.ggb13c = b.ppt03c where isnull(a.ggb13c,'') <> '' update a set a.fa_type3 = ppt02c from #ggm01 a inner join #personprecept b on a.gga00c = b.ppt00c and a.ggb14c = b.ppt03c where isnull(a.ggb14c,'') <> '' update a set a.fa_type4 = ppt02c from #ggm01 a inner join #personprecept b on a.gga00c = b.ppt00c and a.ggb26c = b.ppt03c where isnull(a.ggb26c,'') <> '' update a set a.costtype1 = hea19i, a.cost_setvalue1 = hea12f, a.cardrate = hea13f, a.rmbrate = hea24f, a.caltperf1_1 = isnull(hea29i, 0), a.caltperf1_2 = isnull(hea30i, 0), a.caltperf1_3 = isnull(hea31i, 0), a.caltperf1_4 = isnull(hea32i, 0), a.ispay = 1 from #ggm01 a inner join #hem01 b on a.gga00c = b.hea00c and a.ggb03c = b.hea01c and a.fa_type = b.hea33c and (a.ggb27c = b.hea34c or b.hea34c = '*') where isnull(a.fa_type,'') <> '' update a set a.costtype2 = hea19i, a.cost_setvalue2 = hea12f, a.cardrate = hea13f, a.rmbrate = hea24f, a.caltperf2_1 = isnull(hea29i, 0), a.caltperf2_2 = isnull(hea30i, 0), a.caltperf2_3 = isnull(hea31i, 0), a.caltperf2_4 = isnull(hea32i, 0) , a.ispay = 1 from #ggm01 a inner join #hem01 b on a.gga00c = b.hea00c and a.ggb03c = b.hea01c and a.fa_type2 = b.hea33c and (a.ggb27c = b.hea34c or b.hea34c = '*') where isnull(a.fa_type2,'') <> '' update a set a.costtype3 = hea19i, a.cost_setvalue3 = hea12f, a.cardrate = hea13f, a.rmbrate = hea24f, a.caltperf3_1 = isnull(hea29i, 0), a.caltperf3_2 = isnull(hea30i, 0), a.caltperf3_3 = isnull(hea31i, 0), a.caltperf3_4 = isnull(hea32i, 0), a.ispay = 1 from #ggm01 a inner join #hem01 b on a.gga00c = b.hea00c and a.ggb03c = b.hea01c and a.fa_type3 = b.hea33c and (a.ggb27c = b.hea34c or b.hea34c = '*') where isnull(a.fa_type3,'') <> '' update a set a.costtype4 = hea19i, a.cost_setvalue4 = hea12f, a.cardrate = hea13f, a.rmbrate = hea24f, a.caltperf4_1 = isnull(hea29i, 0), a.caltperf4_2 = isnull(hea30i, 0), a.caltperf4_3 = isnull(hea31i, 0), a.caltperf4_4 = isnull(hea32i, 0), a.ispay = 1 from #ggm01 a inner join #hem01 b on a.gga00c = b.hea00c and a.ggb03c = b.hea01c and a.fa_type4 = b.hea33c and (a.ggb27c = b.hea34c or b.hea34c = '*') where isnull(a.fa_type4,'') <> '' update a set a.costtype1 = hea19i, a.cost_setvalue1 = hea12f, a.cardrate = hea13f, a.rmbrate = hea24f, a.caltperf1_1 = isnull(hea29i, 0), a.caltperf1_2 = isnull(hea30i, 0), a.caltperf1_3 = isnull(hea31i, 0), a.caltperf1_4 = isnull(hea32i, 0) from #ggm01 a inner join #hem01 b on a.gga00c = b.hea00c and a.ggb03c = b.hea01c and a.fa_type = b.hea33c where isnull(a.fa_type,'') <> '' and isnull(hea34c, '*') = '*' and isnull(a.ispay ,0) <> 1 --and isnull(caltperf1_1,'') = '' update a set a.costtype2 = hea19i, a.cost_setvalue2 = hea12f, a.cardrate = hea13f, a.rmbrate = hea24f, a.caltperf2_1 = isnull(hea29i, 0), a.caltperf2_2 = isnull(hea30i, 0), a.caltperf2_3 = isnull(hea31i, 0), a.caltperf2_4 = isnull(hea32i, 0) from #ggm01 a inner join #hem01 b on a.gga00c = b.hea00c and a.ggb03c = b.hea01c and a.fa_type2 = b.hea33c where isnull(a.fa_type2,'') <> '' and isnull(hea34c, '*') = '*' and isnull(a.ispay ,0) <> 1 --and isnull(caltperf2_2,'') = '' update a set a.costtype3 = hea19i, a.cost_setvalue3 = hea12f, a.cardrate = hea13f, a.rmbrate = hea24f, a.caltperf3_1 = isnull(hea29i, 0), a.caltperf3_2 = isnull(hea30i, 0), a.caltperf3_3 = isnull(hea31i, 0), a.caltperf3_4 = isnull(hea32i, 0) from #ggm01 a inner join #hem01 b on a.gga00c = b.hea00c and a.ggb03c = b.hea01c and a.fa_type3 = b.hea33c where isnull(a.fa_type3,'') <> '' and isnull(hea34c, '*') = '*' and isnull(a.ispay ,0) <> 1 --and isnull(caltperf3_2,'') = '' update a set a.costtype4 = hea19i, a.cost_setvalue4 = hea12f, a.cardrate = hea13f, a.rmbrate = hea24f, a.caltperf4_1 = isnull(hea29i, 0), a.caltperf4_2 = isnull(hea30i, 0), a.caltperf4_3 = isnull(hea31i, 0), a.caltperf4_4 = isnull(hea32i, 0) from #ggm01 a inner join #hem01 b on a.gga00c = b.hea00c and a.ggb03c = b.hea01c and a.fa_type4 = b.hea33c where isnull(a.fa_type4,'') <> '' and isnull(hea34c, '*') = '*' and isnull(a.ispay ,0) <> 1 --and isnull(caltperf4_4,'') = '' while(@sate = 0) begin if exists(select 1 from #ggm01) begin set @sate = 0 select top 1 @id = id, @inner_compid = gga00c, @billid = gga01c, @cardno = gga09c, @ddate = gga02d, @cardtype = gga07c, @cash_prj_rate_points = isnull(gak33f, 1), @perf_rate_prj_gam10 = isnull(gak45f, 1), @card_kind = gak03i, @pos_reduce_rate = gca25f, @cash_prj_rate = gak22f, @item = ggb03c,@standardprice = ggb08f, @price = ggb10f,@amt = ggb11f,@quan = ggb05f, @mode_1 = isnull(ggb15c, '2'), @mode_2 = isnull(ggb16c, '2'), @mode_3 = isnull(ggb19c, '2'), @mode_4 = isnull(ggb25c, '2'), @staff_1 = ggb12c, @staff_2 = ggb13c, @staff_3 = ggb14c, @staff_4 = ggb26c, @paymode = ggb27c, @ggm02_seq = ggb02f, @input_vperf1 = ggb20f, @input_vperf2 = ggb21f, @input_vperf3 = ggb22f, @input_vperf4 = ggb35f, @fa_type = fa_type,@fa_type2 = fa_type2,@fa_type3 = fa_type3, @fa_type4 = fa_type4, @costtype1 = costtype1,@cost_setvalue1 = cost_setvalue1,@caltperf1_1 = caltperf1_1,@caltperf1_2 = caltperf1_2,@caltperf1_3 = caltperf1_3,@caltperf1_4 = caltperf1_4, @costtype2 = costtype2,@cost_setvalue2 = cost_setvalue2,@caltperf2_1 = caltperf2_1,@caltperf2_2 = caltperf2_2,@caltperf2_3 = caltperf2_3,@caltperf2_4 = caltperf2_4, @costtype3 = costtype3,@cost_setvalue3 = cost_setvalue3,@caltperf3_1 = caltperf3_1,@caltperf3_2 = caltperf3_2,@caltperf3_3 = caltperf3_3,@caltperf3_4 = caltperf3_4, @costtype4 = costtype4,@cost_setvalue4 = cost_setvalue4,@caltperf4_1 = caltperf4_1,@caltperf4_2 = caltperf4_2,@caltperf4_3 = caltperf4_3,@caltperf4_4 = caltperf4_4, @cardrate = cardrate, @rmbrate = rmbrate from #ggm01 --计算项目购买的成本与虚业绩 exec upg_compute_cost_perf_project_ceshi @compid, @billid, @cardtype,@cardno, @ddate,@cash_prj_rate_points,@perf_rate_prj_gam10, @card_kind,@pos_reduce_rate,@input_project_perf,@cash_prj_rate, @item,@standardprice,@price,@amt,@quan,@mode_1,@mode_2, @mode_3,@mode_4,@staff_1,@staff_2,@staff_3,@staff_4,@paymode, @vperf_1, @vperf_2,@vperf_3,@vperf_4,@ggm02_seq, @input_vperf1,@input_vperf2,@input_vperf3,@input_vperf4, @fa_type,@fa_type2,@fa_type3,@fa_type4, @costtype1,@cost_setvalue1,@caltperf1_1,@caltperf1_2,@caltperf1_3,@caltperf1_4, @costtype2,@cost_setvalue2,@caltperf2_1,@caltperf2_2,@caltperf2_3,@caltperf2_4, @costtype3,@cost_setvalue3,@caltperf3_1,@caltperf3_2,@caltperf3_3,@caltperf3_4, @costtype4,@cost_setvalue4,@caltperf4_1,@caltperf4_2,@caltperf4_3,@caltperf4_4, @cardrate,@rmbrate set @billid = '' set @cardno = '' set @ddate = '' set @cash_prj_rate_points = 0 set @card_kind = '' set @pos_reduce_rate = 0 set @cash_prj_rate = 0 set @item = '' set @standardprice = 0 set @price = 0 set @amt = 0 set @quan = 0 set @mode_1 = '' set @mode_2 = '' set @mode_3 = '' set @mode_4 = '' set @staff_1 = '' set @staff_2 = '' set @staff_3 = '' set @staff_4 = '' set @paymode = '' set @vperf_1 = 0 set @vperf_2 = '' set @vperf_3 = '' set @vperf_4 = '' set @ggm02_seq = 0 set @input_vperf1 = 0 set @input_vperf2 = 0 set @input_vperf3 = '' set @input_vperf4 = '' set @fa_type = '' set @fa_type2 = '' set @fa_type3 = 0 set @fa_type4 = '' set @costtype1 = 0 set @cost_setvalue1 = 0 set @costtype1 = 0 set @cost_setvalue2 = 0 set @costtype3 = 0 set @cost_setvalue3 = 0 set @costtype4 = 0 set @cost_setvalue4 = 0 set @ggm02_seq = 0 set @input_vperf1 = 0 set @input_vperf2 = 0 set @input_vperf3 = '' set @input_vperf4 = '' set @caltperf1_1 = 0 set @caltperf1_2 = 0 set @caltperf1_3 = 0 set @caltperf1_4 = 0 set @caltperf2_1 = 0 set @caltperf2_2 = 0 set @caltperf2_3 = 0 set @caltperf2_4 = 0 set @caltperf3_1 = 0 set @caltperf3_2 = 0 set @caltperf3_3 = 0 set @caltperf3_4 = 0 set @caltperf4_1 = 0 set @caltperf4_2 = 0 set @caltperf4_3 = 0 set @caltperf4_4 = 0 set @cardrate = 0 set @rmbrate = 0 --计算产品购买的成本与虚业绩 -- exec upg_compute_cost_perf_produce @compid, @billid, @cardtype, @cardno, @ddate delete from #ggm01 where id = @id end else begin set @sate = 1 end end -- SELECT @dt2 = getdate() -- print 'sssssssssss' -- SET @Seconds = DATEDIFF( second, @dt1, @dt2); -- print @Seconds drop table #hem01 drop table #ggm01_ggm02 drop table #ggm01 drop table #ggm02 drop table #personprecept set nocount off end GO /****** Object: StoredProcedure [dbo].[upg_add_accounts1] Script Date: 06/12/2017 06:19:23 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_add_accounts1] ( @compid varchar(10), @acctnum int ) as begin declare @ig int declare @i int declare @code varchar(10) set @i = 1 set @ig = 1 set @code = 'B' while @ig <= @acctnum begin while (select count(*) from gsm02 where gsb00c = @compid and gsb01c = '3' and gsb02c = @code) >0 begin set @i = @i + 1 if @i <= 25 select @code = char(ascii('A') + @i) else if @i <= 51 select @code = 'A' + char(ascii('A') + @i-26) else if @i <= 77 select @code = 'B' + char(ascii('A') + @i-52) else if @i <= 103 select @code = 'C' + char(ascii('A') + @i-78) else select @code = 'D' + char(ascii('A') + @i-103) end if @acctnum <= 25 exec upg_add_accounts @compid, 1, @i else if @acctnum <= 51 exec upg_add_accounts @compid, 26, @i else if @acctnum <= 77 exec upg_add_accounts @compid, 52, @i else if @acctnum <= 103 exec upg_add_accounts @compid, 78, @i else exec upg_add_accounts @compid, 104, @i set @ig = @ig + 1 end end GO /****** Object: StoredProcedure [dbo].[upg_cal_sum_comm_mobile] Script Date: 06/12/2017 06:19:34 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_cal_sum_comm_mobile] ( @spid int, @compid varchar(10), -- 公司别 @fromdate varchar(10), -- 开始日期 @todate varchar(10), -- 截至日期 @fromempl varchar(20), -- 查询开始人员 @toempl varchar(20), -- 查询截至人员 @return_type int ) as begin -- 按员工进行分组统计, 按员工累计来计算每个员工的提成, 注意是一个员工总的业绩(包含项目,产品,卡) -- 按项目,卡,物品销售分别累计计算提成的(不分项目类别,也需要在此计算) exec upg_cal_work_detail_by_empl @spid, @compid, @fromdate, @todate, @fromempl, @toempl, @return_type select isnull(comm, 0) totalcomm, isnull(comm_prj, 0) as projcomm, isnull(comm_good, 0) as prodcomm, isnull(comm_card, 0) as cardcomm from workdetail_by_empl where person_id = @fromempl and spid = @spid end GO /****** Object: StoredProcedure [dbo].[upg_cancel_pay] Script Date: 06/12/2017 06:19:37 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_cancel_pay] ( @compid varchar(10), @enterid varchar(20), @billid varchar(20) ) as begin exec upg_unclose_glm01 @compid, @enterid delete gsm03 where gsc00c = @compid and ((gsc01c = @enterid and gsc02c = 'gl') or (gsc01c = @billid and gsc02c = 'gx')) update glm01 set gla20f = 0, gla23i = 0, gla95c = '', gla96d = '', gla80d = '' where gla00c = @compid and gla01c = @enterid update glm03 set glc04f = 0, glc05i = 0, glc06f = 0 where glc00c = @compid and glc01c = @enterid end GO /****** Object: StoredProcedure [dbo].[upg_app_confirm_gqm01] Script Date: 06/12/2017 06:19:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_app_confirm_gqm01] ( @compid varchar(10), @billid varchar(20), @userid varchar(20), -- 经理终端的登录者代码 @confirm int -- 1 审核 0 取消审核 ) as begin declare @curdate varchar(20) declare @time varchar(20) exec upg_getcurrdate @curdate output, @time output exec upg_update_gqm01 @compid, @billid, @userid, @curdate, @confirm end GO /****** Object: StoredProcedure [dbo].[upg_prepare_day_account_scope] Script Date: 06/12/2017 06:20:27 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_prepare_day_account_scope] ( @compid varchar(10), @fromdate varchar(10), @todate varchar(10), @calflag int ) as begin declare @dt_cur_date datetime declare @str_cur_date varchar(20) declare @strtmp varchar(20) set @dt_cur_date = convert(datetime, @fromdate) while (@dt_cur_date <= convert(datetime, @todate)) begin select @strtmp = convert(varchar(10), cast(@dt_cur_date as datetime), 120); select @str_cur_date = substring(@strtmp, 1, 4) + substring(@strtmp, 6, 2) + substring(@strtmp, 9, 2) exec upg_prepare_day_account @compid, @str_cur_date, @calflag select @dt_cur_date = dateadd(day, 1, @dt_cur_date) end end GO /****** Object: StoredProcedure [dbo].[upg_purse_deduct] Script Date: 06/12/2017 06:20:28 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_purse_deduct] ( @compid varchar(10), -- 公司编号 @cardno varchar(20), -- 会员卡号 @date varchar(10), -- 异动日期 @accttype int, -- 账户类型(1-电子钱包 2-储值账户) @consume float, -- 支付金额 @changway int, -- 异动方向(0-充值, 1-取款, 2-消费, 3-转入, 4-转出, 5-欠款 6-欠款返回 7-赠送) @billtype varchar(5), -- 单据类别 @billno varchar(20), -- 单据号 @confirmflag int -- 是否复核的标记(1-复核 0-取消复核) ) as begin declare @__sp_re int exec @__sp_re = upg_purse_deduct_extends @compid, @cardno, @date, @accttype, @consume, @changway, @billtype, @billno, @confirmflag, null return @__sp_re end GO /****** Object: StoredProcedure [dbo].[upg_add_money_new_extends] Script Date: 06/12/2017 06:19:24 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_add_money_new_extends] ( @compid varchar(10), -- 公司别 @cardno varchar(30), -- 卡号 @amount float, -- 金额 @confirm int, -- 是否复核 1 存款复核 0 存款取消复核 3 取款取消复核 2 取款 复核 @type int, -- 账户2:储值 1:电子钱包 @billtype varchar(20), @billid varchar(20), @curdate varchar(8), @proc_seq int ) as--wuxm, 2004/08/14 begin declare @changeway int declare @confirmflag int if @confirm = 1 -- 存款复核 begin select @changeway = 0 -- 存款 select @confirmflag = 1 -- 复核 end else if @confirm = 0 -- 存款取消符合 begin select @changeway = 0 -- 存款 select @confirmflag = 0 -- 取消复核 end else if @confirm = 3 -- 取款,取消复核 begin select @changeway = 1 -- 取款 select @confirmflag = 0 -- 取消复核 end else if @confirm = 2 -- 取款,复核 begin select @changeway = 1 -- 取款 select @confirmflag = 1 -- 复核 end declare @__sp_re int exec @__sp_re = upg_purse_deduct_extends @compid, @cardno, @curdate, @type, @amount, @changeway, @billtype, @billid, @confirmflag, @proc_seq return @__sp_re end GO /****** Object: StoredProcedure [dbo].[upg_compute_cost_perf] Script Date: 06/12/2017 06:19:43 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_compute_cost_perf] ( @compid varchar(10), -- 公司别 @billid varchar(20) -- 收银单号 ) as set nocount on set rowcount 0 set transaction isolation level read uncommitted begin begin try begin transaction declare @cardtype varchar(20) -- 会员卡类别, 消费时候的类别 declare @cardno varchar(20) --会员卡号 declare @ddate varchar(8) --账单日期 -- 获得卡号 消费日期 卡类别 select @cardno = gga09c, @ddate = gga02d, @cardtype = gga07c from ggm01 with(nolock) where gga00c = @compid and gga01c = @billid --计算项目购买的成本与虚业绩 exec upg_compute_cost_perf_project @compid, @billid, @cardtype, @cardno, @ddate --计算产品购买的成本与虚业绩 exec upg_compute_cost_perf_produce @compid, @billid, @cardtype, @cardno, @ddate commit transaction end try begin catch if(@@trancount > 0) begin rollback transaction end end catch end GO /****** Object: StoredProcedure [dbo].[upg_undo_autocreate_inbills] Script Date: 06/12/2017 06:20:48 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_undo_autocreate_inbills] ( @compid varchar(10), -- 公司别(出库) @branid varchar(10), -- 门店号(入库) @outbillid varchar(100), -- 出库单号 @checkman varchar(10), -- 复核人 @currdate varchar(8), -- 复核日期 @curtime varchar(20) -- 复核时间 ) as begin set nocount on -- 当前的事务数 declare @__transaction_count int set @__transaction_count = @@trancount -- try...catch处理 begin try -- a.开启事务,或者设置事务保存点 if @__transaction_count = 0 begin tran else save tran __tran_SavePoint declare @confirmdate varchar(8) declare @inbillid varchar(100) declare @ddate varchar(8) declare @garageid varchar(10) declare @__sp_re int select @confirmdate = gha94d, @inbillid = gha01c, @garageid = gha04c, @ddate = gha03d from ghm01 where gha00c = @branid and gha08c = @outbillid if isnull(@confirmdate, '') = '' begin -- 没有复核过,直接删除 delete ghm01 where gha00c = @branid and gha01c = @inbillid end else begin --先取消复核这个单据,再删除 exec @__sp_re = upg_ghm01_confirm_in_uncheck @branid, @inbillid, @ddate, @garageid, @checkman, @currdate, @curtime delete ghm01 where gha00c = @branid and gha01c = @inbillid end if @__sp_re <> 0 raiserror('has some error', 16, 1) -- 提交事务 -- 有可提交的事务, 并且事务是在当前模块中开启的情况下, 才提交事务 if xact_state() = 1 and @__transaction_count = 0 commit -- 为了防止TRY 中有遗漏的事务处理, 可以在TRY 模拟的结束部分做最终的判断 if @__transaction_count = 0 begin if xact_state() = -1 rollback tran else begin while @@trancount > 0 commit tran end end end try begin catch -- 在CATCH 模块, 应该首先处理事务 if xact_state() <> 0 begin if @__transaction_count = 0 rollback tran -- xact_state 为-1 时, 不能回滚到事务保存点, 这种情况留给外层调用者做统一的事务回滚 -- 通过@@trancount > @__transaction_count 的判断, 即使在TRY 模块中没有设置事务保存点的情况下跳到此步骤, 也不会出错 else if xact_state() = 1 and @@trancount > @__transaction_count rollback tran __tran_SavePoint end declare @SP_Parameters varchar(max) set @SP_Parameters = '@compid = ' + isnull(@compid, '') + '@branid = ' + isnull(@branid, '') + '@outbillid = ' + isnull(@outbillid, '') + '@checkman = ' + isnull(@checkman, '') + '@currdate = ' + isnull(@currdate, '') + '@curtime = ' + isnull(@curtime, '') insert into SP_TranLog(SPName, SPTime, SPParameter) values('upg_undo_autocreate_inbills', getdate(), @SP_Parameters) return -1 end catch end GO /****** Object: StoredProcedure [dbo].[upg_delete_gim20] Script Date: 06/12/2017 06:19:58 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_delete_gim20] ( @compid varchar(20), @billid varchar(20) --收银单号 )--寄存单号 as begin --删除gim20 删除之前先判断对应的产品是否已经领取过 gim20是否已经复核 declare @billno nvarchar(100)--寄存单号 declare @gim06c nvarchar(100)--单据类型 1-寄存 2-领取 3-欠货 declare @gim09d nvarchar(100)--日期 declare @gim07c nvarchar(100)--操作人员 declare @memberid nvarchar(50) declare @flag1 float declare @take_from_id int select @billno = gim01c, @gim06c = gim06c, @gim09d = gim09d, @gim07c = gim07c, @memberid = gim02c from gim20 where gim00c = @compid and gim12c = @billid and (gim11c = '0') if not exists(select 1 from gim22 where gio00c = @compid and gio02c = @billno and isnull(gio09f, 0)>0 and gio01c = @memberid) begin --没有领取过 直接取消复核再删除gim20 exec upg_confirm_gim20 @compid, @gim06c, @billno, 2, @gim07c, @gim09d delete gim20 where gim00c = @compid and gim12c = @billid and (gim11c = '0') end end GO /****** Object: StoredProcedure [dbo].[upg_stat_card_sale_summary] Script Date: 06/12/2017 06:20:43 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_stat_card_sale_summary] ( @incompid varchar(10), -- 公司别 @fromdate varchar(10), -- 开始日期 @todate varchar(10), -- 截至日期 @fromcardtype varchar(10), -- 卡类别 @tocardtype varchar(10), -- 卡类别 @acctype varchar(10), -- 账户类别 Add by Huzy 2011/03/22 @fromcardid varchar(20), -- 开始卡号 @tocardid varchar(20), -- 截至卡号 @include_proc int, -- 是否包括疗程部分 @saletype varchar(2) -- 业务类型 add by liyh '3' - 取款 ) as begin exec upg_stat_card_sale_summary_ex @incompid, @fromdate, @todate, @fromcardtype, @tocardtype, @acctype, @acctype, @fromcardid, @tocardid, @include_proc, @saletype end GO /****** Object: StoredProcedure [dbo].[upg_add_cureproc_ex] Script Date: 06/12/2017 06:19:23 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_add_cureproc_ex] ( @compid varchar(10), -- 门店编号 @cardno varchar(30), -- 会员卡号 @proj varchar(30), -- 项目编号 @buytimes float, -- 购买次数 @sendtimes float, -- 赠送次数 @amount float, -- 疗程金额 @confirm int, -- 1-存款复核 0-存款取消复核,3-取款 取消复核 2-取款 复核 @addtoremain int, -- @start_date varchar(8), -- @end_date varchar(8), -- @billtype varchar(20), -- @billid varchar(20), -- @curdate varchar(8), -- 异动日期 @usetimes float, -- 这个疗程一个月使用的次数 @priceflow int, -- 价格序号 @discount float, -- 折扣 @cardnum float, @issent int, -- 是否为赠送 @isgift int, -- 永久免费 @memo varchar(60), -- @gcf23i int, -- gcm11中的疗程序号 @gcf24i int -- ) as begin if (@start_date > @end_date) begin declare @temp_date varchar(8) set @temp_date = @start_date set @start_date = @end_date set @end_date = @temp_date end declare @curdateIn varchar(8) select @curdateIn = substring(convert(varchar(20), getdate(), 102), 1, 4) + substring(convert(varchar(20), getdate(), 102), 6, 2) + substring(convert(varchar(20), getdate(), 102), 9, 2) -- 根据卡号获取归属公司: declare @ownercompid nvarchar(10) --卡的归属公司 select @ownercompid = min(gca13d) from gcm01 with (nolock) where gca01c = @cardno and gca00c = gca13d if isnull(@start_date, '') = '' begin select @start_date = @curdate end declare @plus_times int set @usetimes = isnull(@usetimes, 0) select @buytimes = isnull(@buytimes, 0), @amount = isnull(@amount, 0), @sendtimes = isnull(@sendtimes, 0) -- 处理gcm08的疗程历史 declare @gch17c varchar(20) -- 支付方式1 declare @gch19c varchar(20) -- 支付方式2 declare @gch21c varchar(20) -- 支付方式3 declare @gch27c varchar(20) -- 支付方式4 declare @gch29c nvarchar(2000) -- 备注 declare @gch18f float -- 支付方式1金额 declare @gch20f float -- 支付方式2金额 declare @gch22f float -- 支付方式3金额 declare @gch28f float -- 支付方式4金额 declare @gcf13i int --疗程间隔 add lucz 2015.09.21 if @confirm = 1 or @confirm = 2 -- 1-充值复核/售卡复核 2-取款复核 begin -- 把疗程中的支付方式也更新过来 -- 把疗程中的支付情况, 更新到gcm08中 if @billtype = 'gn' begin select @gch17c = gnc15c, -- 支付方式1 @gch18f = gnc16f, -- 支付方式1金额 @gch19c = gnc17c, -- 支付方式2 @gch20f = gnc18f, -- 支付方式2金额 @gch21c = gnc19c, -- 支付方式3 @gch22f = gnc20f, -- 支付方式3金额 @gch27c = gnc23c, -- 支付方式4 @gch28f = gnc24f, -- 支付方式4金额 @gcf13i = gnc07i, -- 疗程间隔 add lucz 2015.09.21 @gch29c = gnc13c -- 备注 from gnm03 b with (nolock) where b.gnc00c = @compid and b.gnc29i = @gcf24i end else if @billtype = 'gz' begin select @gch17c = gcm15c, -- 支付方式1 @gch18f = gcm16f, -- 支付方式1金额 @gch19c = gcm17c, -- 支付方式2 @gch20f = gcm18f, -- 支付方式2金额 @gch21c = gcm19c, -- 支付方式3 @gch22f = gcm20f, -- 支付方式3金额 @gch27c = gcm23c, -- 支付方式4 @gch28f = gcm24f, -- 支付方式4金额 @gcf13i = gcm07i, -- 疗程间隔 add lucz 2015.09.21 @gch29c = gcm13c -- 备注 from gcm11 b with (nolock) where b.gcm00c = @compid and b.gcm14i = @gcf24i end end declare @proc_acttype int select @proc_acttype = 4 declare @proc_seq int if (@confirm = 1) --1-充值复核/售卡复核 begin -- 可能是取消复核后的复核,则不能单独的增加疗程,而是需要找到原来的疗程序号 select @proc_seq = 0 if @billtype = 'gz' or @billtype = 'gn' begin select @proc_seq = gcf23i from gcm06 with (nolock) where gcf00c = @ownercompid and gcf01c = @cardno and gcf24i = @gcf24i and gcf23i = @gcf23i -- 如果有,则在上面更新 if @proc_seq > 0 begin update gcm06 set gcf03f = gcf03f + @buytimes, gcf04f = gcf04f + @sendtimes, gcf05f = gcf05f + (@buytimes + @sendtimes), gcf07f = gcf07f + (@buytimes + @sendtimes), gcf08f = gcf08f + @amount, -- 疗程金额 gcf10f = gcf10f + @amount, -- 剩余金额 gcf14f = @usetimes, gcf26c = @gch29c, gcf13i = @gcf13i, gcf28i = 0 where gcf23i = @proc_seq and gcf00c = @ownercompid and gcf01c = @cardno end end -- 如果没有,则需要增加 if @proc_seq = 0 begin insert gcm06(gcf00c, gcf01c, gcf02c, gcf03f, gcf04f, gcf05f, gcf06f, gcf07f, gcf08f, gcf09f, gcf10f, gcf11d, gcf12d, gcf14f, gcf17i, gcf18f, gcf19i, gcf20i, gcf21i, gcf22i, gcf15c, gcf25f, gcf26c, gcf24i,gcf13i) values(@ownercompid, @cardno, @proj, @buytimes, @sendtimes, @buytimes + @sendtimes, 0, @buytimes + @sendtimes, @amount, 0, @amount, @start_date, @end_date, @usetimes, @priceflow, @discount, 1, 0, @issent, isnull(@isgift, 0), @memo, @plus_times, @gch29c, @gcf24i,@gcf13i) set @proc_seq = @@identity end -- 把疗程序号写到原始单据中 exec upg_update_procid_rawbill @compid, @billtype, @gcf24i, @proc_seq -- 将此部分金额也加入充值账户中 if @addtoremain = 1 begin exec upg_add_money_new_extends @compid, @cardno, @amount, @confirm, @proc_acttype, @billtype, @billid, @curdate, @proc_seq end end else if @confirm = 3 --取款 取消复核, 应该加回去 begin -- 寻找的原则是,最少的剩余次数 select @proc_seq = 0 if isnull(@proc_seq, 0) = 0 begin select @proc_seq = gcf23i from gcm06 with (nolock) where gcf00c = @ownercompid and gcf01c = @cardno and gcf23i = @gcf23i order by gcf07f desc end -- 可能根本就找不到那个疗程记录 -- 则需要增加这个疗程记录 if isnull(@proc_seq, 0) = 0 begin insert gcm06(gcf00c, gcf01c, gcf02c, gcf03f, gcf04f, gcf05f, gcf06f, gcf07f, gcf08f, gcf09f, gcf10f, gcf11d, gcf12d, gcf14f, gcf17i, gcf18f, gcf19i, gcf20i, gcf21i, gcf22i, gcf15c, gcf25f, gcf26c,gcf13i) values(@ownercompid, @cardno, @proj, @buytimes, @sendtimes, @buytimes + @sendtimes, 0, @buytimes + @sendtimes, @amount, 0, @amount, @start_date, @end_date, @usetimes, @priceflow, @discount, 1, 0, @issent, isnull(@isgift, 0), @memo, @plus_times, @gch29c,@gcf13i) -- 取序号 set @proc_seq = @@identity end else begin -- 取款取消复核,需要把次数以及金额给加回去 update gcm06 set gcf03f = gcf03f + @buytimes, gcf04f = gcf04f + @sendtimes, gcf05f = gcf05f + (@buytimes + @sendtimes), gcf07f = gcf07f + (@buytimes + @sendtimes), gcf08f = gcf08f + @amount, -- 疗程金额 gcf10f = gcf10f + @amount, -- 疗程余额 gcf14f = @usetimes, gcf26c = @gch29c, --疗程备注 gcf13i = @gcf13i, gcf28i = 0 where gcf23i = @proc_seq and gcf00c = @ownercompid and gcf01c = @cardno end if @addtoremain = 1 begin exec upg_add_money_new_extends @compid, @cardno, @amount, @confirm, @proc_acttype, @billtype, @billid, @curdate, @proc_seq end end else -- 存款取消复核,取款复核 都是在原来的疗程上调整,所以也不需要调整原始单据上的疗程序号 begin set @proc_seq = 0 if isnull(@proc_seq, 0) = 0 begin select @proc_seq = min(gcf23i) from gcm06 with (nolock) where gcf00c = @ownercompid and gcf01c = @cardno and gcf23i = @gcf23i end update gcm06 set gcf03f = gcf03f - @buytimes, gcf04f = gcf04f - @sendtimes, gcf05f = gcf05f - (@buytimes + @sendtimes), gcf07f = gcf07f - (@buytimes + @sendtimes), gcf08f = gcf08f - @amount, gcf10f = gcf10f - @amount, gcf14f = @usetimes, gcf26c = @gch29c --疗程备注 where gcf23i = @proc_seq and gcf00c = @ownercompid and gcf01c = @cardno -- 可能剩余的次数已经变为负数 -- 取消复核,可能被设置为0的疗程给删除掉 update gcm06 set gcf28i = 1 where gcf23i = @proc_seq and gcf00c = @ownercompid and gcf01c = @cardno and isnull(gcf10f, 0) = 0 and isnull(gcf07f, 0) = 0 if @addtoremain = 1 begin exec upg_add_money_new_extends @compid, @cardno, @amount, @confirm, @proc_acttype, @billtype, @billid, @curdate, @proc_seq -- 可能把gcm03中的疗程的账户余额扣为负的,需要修改为0 -- update gcm03 set gcc06f = 0 where gcc00c = @compid and gcc01c = @cardno and gcc03i = @proc_acttype and isnull(gcc06f, 0)<0 end end if @confirm = 1 begin insert gcm08(gch00c, gch02d, gch03c, gch04c, gch05f, gch06f, gch07f, gch08f, gch09f, gch10c, gch11c, gch12i, gch13f, gch14f, gch15f, gch17c, gch18f, gch19c, gch20f, gch21c, gch22f, gch27c, gch28f, gch16i, gch29c) values(@compid, @curdate, @cardno, @proj, @buytimes, @sendtimes, @amount, 0, 0, @billtype, @billid, @priceflow, @discount, @cardnum, dbo.fun_divide_default(@amount, (isnull(@buytimes, 0) + isnull(@sendtimes, 0)), 0), @gch17c, @gch18f, @gch19c, @gch20f, @gch21c, @gch22f, @gch27c, @gch28f, @proc_seq, @gch29c) end else if @confirm = 2 -- 取款复核 begin insert gcm08(gch00c, gch02d, gch03c, gch04c, gch05f, gch06f, gch07f, gch08f, gch09f, gch10c, gch11c, gch12i, gch13f, gch14f, gch15f, gch17c, gch18f, gch19c, gch20f, gch21c, gch22f, gch27c, gch28f, gch16i, gch29c) values(@compid, @curdate, @cardno, @proj, 0-@buytimes, 0-@sendtimes, 0-@amount, 0, 0, @billtype, @billid, @priceflow, @discount, 0-@cardnum, dbo.fun_divide_default(@amount, (isnull(@buytimes, 0) + isnull(@sendtimes, 0)), 0), @gch17c, @gch18f, @gch19c, @gch20f, @gch21c, @gch22f, @gch27c, @gch28f, @proc_seq, @gch29c) end else if @confirm = 0 or @confirm = 3 begin delete gcm08 where gch00c = @compid and gch03c = @cardno and gch04c = @proj and gch10c = @billtype and gch11c = @billid and gch16i = @proc_seq end end GO /****** Object: StoredProcedure [dbo].[upg_create_new_comp] Script Date: 06/12/2017 06:19:53 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_create_new_comp] ( @comp varchar(10) ) as begin begin transaction begin try if exists(select 1 from gam05 where gae01c = @comp) return if isnull(@comp, '') = '' return exec upg_init_comp @comp, 0 exec upg_set_version @comp, 1 -- 美容版本 end try begin catch if @@TRANCOUNT > 0 begin rollback transaction declare @SP_Parameters varchar(max) set @SP_Parameters = '@comp = ' + isnull(@comp, '') insert into SP_TranLog (SPName ,SPTime ,SPParameter) values ( 'upg_create_new_comp' ,getdate() ,@SP_Parameters) raiserror ('transaction is failed.', 16, 1) WITH NOWAIT end end catch if @@TRANCOUNT > 0 begin commit transaction end end GO /****** Object: StoredProcedure [dbo].[upg_add_companies] Script Date: 06/12/2017 06:19:23 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_add_companies] ( @compid varchar(10), @compnum int, @sa02c varchar(10), @sa03f int ) as begin if @compid <> '001' return declare @compcode varchar(10) declare @comp_pre varchar(10) declare @comp_len int set @comp_pre = @sa02c set @comp_len = @sa03f declare @i int select @i = count(gae01c) + 1 from gam05 set @compnum = @compnum + @i while @i < @compnum begin select @compcode = convert(varchar(10), @i) select @compcode = isnull(@comp_pre, '') + replicate('0', @comp_len - len(@compcode)) + @compcode if not exists(select 1 from gam05 where gae01c = @compcode) begin exec upg_init_comp @compcode, 0 exec upg_set_version @compcode, 1 -- 美容版本 exec upg_m_dispatch_gsm01 '001', @compcode, '1' exec upg_m_dispatch_gsm02 '001', @compcode, '1' exec upg_m_dispatch_gdm01 '001', @compcode, '1' exec upg_m_dispatch_hem01 '001', @compcode, '1' exec upg_m_dispatch_gfm01 '001', @compcode, '1' exec upg_m_dispatch_gam10 '001', @compcode, '1' exec upg_m_dispatch_gdm06 '001', @compcode, '1' exec upg_m_dispatch_gtc01 '001', @compcode, '1' exec upg_m_dispatch_gbm20 '001', @compcode, '1' exec upg_m_dispatch_gtc01 '001', @compcode, '1' exec upg_m_dispatch_gdm20 '001', @compcode, '1' exec upg_m_dispatch_gam04 '001', @compcode, '1' exec upg_m_dispatch_gzm05 '001', @compcode, '1' exec upg_m_dispatch_hbm01 '001', @compcode, '1' exec upg_m_dispatch_gxm02 '001', @compcode, '1' exec upg_m_dispatch_satisfaction '001', @compcode, '1' exec upg_m_dispatch_gdn01 '001', @compcode, '1' end select @i = @i + 1 end exec upg_init_control_set--added 20121203 end GO /****** Object: StoredProcedure [dbo].[upg_ghm01_confirm_out_uncheck] Script Date: 06/12/2017 06:20:11 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_ghm01_confirm_out_uncheck] ( @compid varchar(10), -- 公司编号 @billno varchar(100), -- 入库(出库)单号 @billdate varchar(8), -- 单据日期 @storageno varchar(4), -- 仓库编号 @checkman varchar(10), -- 复核人 @currdate varchar(8), -- 复核日期 @curtime varchar(20) -- 复核时间 ) as begin set nocount on -- 当前的事务数 declare @__transaction_count int set @__transaction_count = @@trancount -- try...catch处理 begin try -- a.开启事务,或者设置事务保存点 if @__transaction_count = 0 begin tran else save tran __tran_SavePoint declare @__sp_re int -- update 库存日异动统计表头档 delete from gfm03 where gfc00c = @compid and gfc01c = '2' and gfc02c = @billno -- update 库存日异动统计明细档 delete from gfm04 where gfd00c = @compid and gfd01c = '2' and gfd02c = @billno update gfm02 set gfb06f = isnull(gfb06f, 0) - isnull(gi.gib08f, 0) from gfm02 gf, gim02 gi where gi.gib00c = @compid and gi.gib01c = @billno and gfb00c = gib00c and gib03c = gfb01c and gfb02c = substring(@billdate, 1, 6) and gfb04c = @storageno and gfb03c = '2' -- update 产品出库单 update gim01 set gia93c = '', gia94d = '' where gia00c = @compid and gia01c = @billno update ghm03 set ghc05f = isnull(ghc05f, 0) + isnull(gic04f, 0) from ghm03, gim03 where ghc00c = gic00c and ghc01c = gic02c and ghc02c = gic03c and ghc00c = @compid and gic01c = @billno and ghc06c = @storageno declare @gia05c varchar(10) declare @gia09i tinyint select @gia09i = gia09i, @gia05c = gia05c from gim01 where gia00c = @compid and gia01c = @billno if (@gia09i = 2) begin exec @__sp_re = upg_undo_autocreate_inbills @compid, @gia05c, @billno, @checkman, @currdate, @curtime end if @__sp_re <> 0 raiserror('has some error', 16, 1) delete gim03 where gic00c = @compid and gic01c = @billno --记录日志 insert sysoperationlog(userid, program, operation, operationdate, operationtime, origatedate, compid, keyvalue1, keyvalue2, keyvalue3) values(@checkman, 'GWA03I', 'M', @currdate, @curtime, @currdate, @compid, @billno, @storageno, '取消复核') -- 提交事务 -- 有可提交的事务, 并且事务是在当前模块中开启的情况下, 才提交事务 if xact_state() = 1 and @__transaction_count = 0 commit -- 为了防止TRY 中有遗漏的事务处理, 可以在TRY 模拟的结束部分做最终的判断 if @__transaction_count = 0 begin if xact_state() = -1 rollback tran else begin while @@trancount > 0 commit tran end end end try begin catch -- 在CATCH 模块, 应该首先处理事务 if xact_state() <> 0 begin if @__transaction_count = 0 rollback tran -- xact_state 为-1 时, 不能回滚到事务保存点, 这种情况留给外层调用者做统一的事务回滚 -- 通过@@trancount > @__transaction_count 的判断, 即使在TRY 模块中没有设置事务保存点的情况下跳到此步骤, 也不会出错 else if xact_state() = 1 and @@trancount > @__transaction_count rollback tran __tran_SavePoint end declare @SP_Parameters varchar(max) set @SP_Parameters = '@compid = ' + isnull(@compid, '') + '@billno = ' + isnull(@billno, '') + '@billdate = ' + isnull(@billdate, '') + '@storageno = ' + isnull(@storageno, '') + '@checkman = ' + isnull(@checkman, '') + '@currdate = ' + isnull(@currdate, '') + '@curtime = ' + isnull(@curtime, '') insert into SP_TranLog(SPName, SPTime, SPParameter) values('upg_ghm01_confirm_out_uncheck', getdate(), @SP_Parameters) return -1 end catch end GO /****** Object: StoredProcedure [dbo].[upg_confirm_card_when_pay] Script Date: 06/12/2017 06:19:44 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_confirm_card_when_pay] ( @compid varchar(10), @enterno varchar(20), @cardid varchar(20), @ret int output ) as begin declare @memid varchar(20) declare @cardtype varchar(20) declare @rate float declare @remain float declare @remain_1 float declare @remain_3 float declare @remain_4 float declare @remain_5 float -- 判断这个卡是否已经在gam06等待结帐,如果是,则退出 if exists(select 1 from gam06 where gaf01c = @cardid and gaf07c <> @enterno and gaf00c = @compid) begin select @ret = -1 return end else begin select @ret = 1 end create table #ggm01_billid ( billid varchar(20) ) select @memid = gba01c from gbm01 where gba00c = @compid and gba23c = @cardid -- 可能有多个收银单的情况 insert #ggm01_billid(billid) select gga01c from ggm01 where gga00c = @compid and gga30c = @enterno select @remain = gcc06f from gcm03 with (nolock) where gcc00c = @compid and gcc01c = @cardid and gcc03i = 2 select @remain_1 = gcc06f from gcm03 with (nolock) where gcc00c = @compid and gcc01c = @cardid and gcc03i = 1 select @remain_3 = gcc06f from gcm03 where gcc00c = @compid and gcc01c = @cardid and gcc03i = 3 select @remain_4 = gcc06f from gcm03 where gcc00c = @compid and gcc01c = @cardid and gcc03i = 4 select @remain_5 = gcc06f from gcm03 where gcc00c = @compid and gcc01c = @cardid and gcc03i = 5 update glm01 set gla06c = @cardid, gla16c = @cardid, gla27c = @memid, gla29f = @remain_1, gla30f = @remain, gla31f = @remain_3, gla32f = @remain_4, gla33f = @remain_5 where gla00c = @compid and gla01c = @enterno update glm02 set glb04c = @cardid where glb00c = @compid and glb01c = @enterno -- 如果原来有三个钥匙牌号,这样更新就重复了 declare @gcl03f_amt float select @gcl03f_amt = sum(isnull(glc03f, 0)) from glm03 where glc00c = @compid and glc01c = @enterno delete glm03 where glc00c = @compid and glc01c = @enterno insert glm03(glc00c, glc01c, glc02c, glc03f, glc04f, glc05i, glc06f) values(@compid, @enterno, @cardid, @gcl03f_amt, 0, 0, 0) -- 用卡来支付,先默认用储值账户来支付 update ggm01 set gga08c = @cardid, gga09c = @cardid, gga27i = 2 where gga00c = @compid and gga30c = @enterno delete gam06 where gaf00c = @compid and gaf07c = @enterno declare @curdate varchar(20), @curtime varchar(20) exec upg_getcurrdate @curdate output, @curtime output insert gam06(gaf00c, gaf01c, gaf03c, gaf07c, gaf04c, gaf05c) values(@compid, @cardid, @cardid, @enterno, substring(@curtime, 1, 4), @curdate) -- 更新ggm02, ggm03的支付方式 update a set a.ggb27c = '4' from ggm02 a, #ggm01_billid b where a.ggb00c = @compid and a.ggb01c = b.billid update a set a.ggc27c = '4' from ggm03 a, #ggm01_billid b where a.ggc00c = @compid and a.ggc01c = b.billid drop table #ggm01_billid delete a from gsm03 a, glm01 b where a.gsc00c = b.gla00c and a.gsc01c = b.gla01c and a.gsc02c = 'gl' and b.gla00c = @compid and b.gla01c = @enterno -- 用不同的卡来支付,所以折扣已经发生变化 exec upg_recal_bill_discount @compid, @enterno declare @amt_card_1 float declare @amt_card_2 float declare @amt_cash_1 float declare @amt_cash_2 float select @amt_card_1 = sum(ggb11f) from ggm02 b with (nolock), ggm01 a with (nolock) where a.gga00c = b.ggb00c and a.gga01c = b.ggb01c and a.gga00c = @compid and a.gga30c = @enterno and b.ggb27c = '4' select @amt_cash_1 = sum(ggb11f) from ggm02 b with (nolock), ggm01 a with (nolock) where a.gga00c = b.ggb00c and a.gga01c = b.ggb01c and a.gga00c = @compid and a.gga30c = @enterno and b.ggb27c = '1' select @amt_card_2 = sum(ggc11f) from ggm03 b with (nolock), ggm01 a with (nolock) where a.gga00c = b.ggc00c and a.gga01c = b.ggc01c and a.gga00c = @compid and a.gga30c = @enterno and b.ggc27c = '4' select @amt_cash_2 = sum(ggc11f) from ggm03 b with (nolock), ggm01 a with (nolock) where a.gga00c = b.ggc00c and a.gga01c = b.ggc01c and a.gga00c = @compid and a.gga30c = @enterno and b.ggc27c = '1' if isnull(@amt_cash_1, 0) + isnull(@amt_cash_2, 0) <> 0 begin insert gsm03(gsc00c, gsc01c, gsc02c, gsc03f, gsc04c, gsc05f, gsc08c) values(@compid, @enterno, 'gl', 1, '1', isnull(@amt_cash_1, 0) + isnull(@amt_cash_2, 0), @cardid) end if isnull(@amt_card_1, 0) + isnull(@amt_card_2, 0) <> 0 begin insert gsm03(gsc00c, gsc01c, gsc02c, gsc03f, gsc04c, gsc05f, gsc08c) values(@compid, @enterno, 'gl', 2, '4', isnull(@amt_card_1, 0) + isnull(@amt_card_2, 0), @cardid) end end GO /****** Object: StoredProcedure [dbo].[upg_cal_net_comm] Script Date: 06/12/2017 06:19:32 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_cal_net_comm] ( @compid varchar(20), @main_card varchar(20), @billNo varchar(30), --单据号 add lucz 2015.11.12 @from int, -- 0 卖卡, 1 充值, 2 消费(收银) @verify int -- 0 反审, 1 审核 ) as begin declare @this_card varchar(20) declare @upper_card varchar(20) declare @can_stop int select @this_card = @main_card -- 先计算这个介绍人的提用 exec upg_expand_net @compid, @this_card, 2,@billNo,@from,@verify -- 查找是否这个人是别人的介绍人 select @can_stop = 0 while @can_stop = 0 begin select @upper_card = '' select @upper_card = a.gna09c from gnm01 a with (nolock), gnm02 b with (nolock) where a.gna00c = b.gnb00c and a.gna01c = b.gnb01c and b.gnb03c = @this_card and a.gna00c = @compid if isnull(@upper_card, '') <> '' begin exec upg_expand_net @compid, @upper_card, 2,@billNo,@from,@verify select @this_card = @upper_card end else begin select @can_stop = 1 end end if( @from = 0) --卖卡 begin update b set b.gnb09i = @verify from gnm02 b, gnm01 a where a.gna00c = @compid and a.gna00c = b.gnb00c and a.gna01c = b.gnb01c and a.gna09c = @main_card and isnull(b.gnb09i, 0) = 0 end else if( @from = 1) --充值 begin update gcm10 set gcl111i = @verify from gcm10 inner join gcm01 on gcl03c = gca04c where gcl00c = @compid and gcl01c = @billNo and gca31c = @main_card --介绍人 and gcl06i = 0 end else begin update ggm01 set gga83i = @verify from ggm01 inner join gcm01 on gga09c = gca04c where gga00c = @compid and gga01c = @billNo and gca31c = @main_card --介绍人 end end GO /****** Object: StoredProcedure [dbo].[upg_calc_personal_statistics] Script Date: 06/12/2017 06:19:36 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_calc_personal_statistics] ( @calc_comp varchar(10), -- 计算门店 @fromdate varchar(10), @todate varchar(10) ) as begin declare @spid int set @spid = @@spid --Start 创建临时表 add by lm 2015-08-05 create table #b_perf ( bran_id varchar(20) not null ) insert #b_perf(bran_id) select gaz02c from gam26 where gaz01c = @calc_comp -- 创建循环变量 declare @HasRecord int set @HasRecord = 1 declare @incompid varchar(10) -- 插入旗下所有子门店的营业数据 while(@HasRecord = 1) begin if exists(select 1 from #b_perf) begin set @HasRecord = 1 select top 1 @incompid = bran_id from #b_perf delete from calc_work_detail where compid = @incompid and srvdate >= @fromdate and srvdate <= @todate exec upg_prepare_work_detail @spid, @incompid, @fromdate, @todate, '*', '*', 1, 1, 1, 1 -- 保存原公司别 update work_detail set comp_ascription = compid where spid = @spid -- 清除已处理的数据 delete #b_perf where bran_id = @incompid end else begin set @HasRecord = 0 end end drop table #b_perf -- 将所有门店编号改为总店编号 update a set a.compid = @calc_comp from work_detail a where spid = @spid and compid <> @calc_comp -- 计算实业绩,并尽量计算可能的提成,如果累计的则无法计算出来(计算之后删除表work_detail中除@fromempl与@toempl之外的数据) exec upg_cal_work_detail_comm @spid, @calc_comp, @fromdate, @todate, '*', '*' -- 计算额外的业绩提成 exec upg_cal_work_detail_add_comm @spid, @calc_comp, @fromdate, @todate, '*', '*' insert into calc_work_detail(person_id, action_id, srvdate, code, name, payway, amt, quan, cost, comm, amt2, amt3, cardrate, stdprice, prj_type, share_rate, cls_flag, gender, billid, paycode, ex_rate, ggm02_seq, room_id, compid, cardid, memid) select person_id, action_id, srvdate, code, name, payway, amt, quan, cost, comm, amt2, amt3, cardrate, stdprice, prj_type, share_rate, cls_flag, gender, billid, paycode, ex_rate, ggm02_seq, room_id, comp_ascription, cardid, memid from work_detail with(nolock) where spid = @spid delete from work_detail where spid = @spid return 1 end GO /****** Object: StoredProcedure [dbo].[upg_reduce_acctamt_by_sms] Script Date: 06/12/2017 06:20:34 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_reduce_acctamt_by_sms] ( @compid varchar(10), -- 公司 @amount float) -- 扣除金额 as begin declare @cardid varchar(20) declare @curdate varchar(20) declare @time varchar(20) declare @accttype int -- 账户 exec upg_getcurrdate @curdate output, @time output --判断是否已经标记 if exists(select 1 from sms_fee_control where mmonth = substring(@curdate, 1, 6) and compid = @compid) begin return end --先更新状态 insert sms_fee_control(compid, mmonth, done, caldate) values(@compid, substring(@curdate, 1, 6), 1, @curdate) declare cur_each_card cursor for select gca01c, gak14i from gcm01 a with (nolock), gbm01 b with (nolock), gam10 d with(nolock), gcm03 c with(nolock) where a.gca00c = a.gca13d and a.gca00c = b.gba00c and a.gca04c = b.gba01c and isnull(b.gba04c, '0') = '1' -- 需要发送短信的 -- or isnull(b.gba43c, '0') = '1') -- 是否接收群发短信(1是0否) and len(gba08c) = 11 and gca00c = @compid and a.gca00c = d.gak00c and a.gca02c = d.gak01c and a.gca00c = c.gcc00c and a.gca01c = c.gcc01c and c.gcc03i = d.gak14i and c.gcc06f >= @amount and gca01c not in (select gcd01c from gcm04 with (nolock) where gcd00c = gca00c and gcd07c = 'SMS' and gcd02i = 2 and gcd05i = 1 and gcd09d = @curdate ) open cur_each_card fetch cur_each_card into @cardid, @accttype while @@fetch_status = 0 begin exec upg_purse_deduct @compid, @cardid, @curdate, @accttype, @amount, 1, 'SMS', @curdate, 1 fetch cur_each_card into @cardid, @accttype end close cur_each_card deallocate cur_each_card end GO /****** Object: StoredProcedure [dbo].[upg_check_auto_upgrade] Script Date: 06/12/2017 06:19:39 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_check_auto_upgrade] ( @compid varchar(10), @cardid varchar(20), -- 会员卡 @billtype varchar(20), -- 单据类别 @billid varchar(20), @oldcardtype varchar(10), @todate varchar(10), @ret int output, -- 0 没有动作 1升级到一个高的类别 @newcardtype varchar(10) output, @upgradelvl int output ) as begin declare @lvl int declare @close_flag int -- 得到这个当前会员卡对应的级别 select @lvl = gak15i, @close_flag = gak23i from gam10 with(nolock) where gak00c = @compid and gak01c = @oldcardtype if isnull(@lvl, 0) = 0 return -- 没有定义级别,所以不知道哪些是更高的 if isnull(@close_flag, 0) = 1 return -- 根据卡号获取归属公司: declare @ownercompid nvarchar(10) --卡的归属公司 select @ownercompid = min(gca13d) from gcm01 with(nolock) where gca01c = @cardid and gca00c = gca13d declare @total_sale_card float -- 一个时间范围内总的卡销售额 declare @continue int declare @curlvl int declare @curperiod int -- 天为单位 declare @curcardamt float -- 需要卡销售的金额 declare @curcardtype varchar(10) declare @sum_consume float -- 累计消费金额 declare @one_consume float -- 一次性消费金额 declare @this_consume float declare @total_sum_consume float declare @discount float -- 升级的折扣 select @continue = 1 select @ret = 0 -- 所有更高的级别的卡类别 declare cur_each_gam10 cursor for select gak15i, gak10f, gak11f, gak01c, gak12f, gak13f from gam10 with(nolock) where gak00c = @compid and isnull(gak15i, 0) > @lvl and (isnull(gak11f, 0)>0 or isnull(gak12f, 0)>0 or isnull(gak13f, 0)>0) order by gak15i desc --从最高的开始,检查当前是否满足条件,如果已经满足条件则认为目前卡就应该升级到此类别 open cur_each_gam10 fetch cur_each_gam10 into @curlvl, @curperiod, @curcardamt, @curcardtype, @one_consume, @sum_consume while @@fetch_status = 0 and @continue = 1 begin --计算级别数 select @upgradelvl = isnull(@curlvl, 0) - isnull(@lvl, 0) --取得可能的折扣 exec upg_get_upgrade_discount @compid, @upgradelvl, @discount output --统计这个时间范围内总的卡销售额 if @billtype = 'gz' begin exec upg_cal_total_card_sale @compid, @cardid, @curperiod, @todate, @total_sale_card output end else if @billtype = 'gx' begin if isnull(@one_consume, 0)>0 begin --取得这次的累计消费额,只统计现金与银行卡 select @this_consume = sum(gsc05f) from gsm03 with (nolock) where gsc00c = @compid and gsc01c = @billid and gsc02c = @billtype and (gsc04c = '1' or gsc04c = '6') end if isnull(@sum_consume, 0)>0 begin --计算时间范围内的累计消费额 exec upg_cal_total_sum_consume @compid, @cardid, @curperiod, @todate, @total_sum_consume output end end if (@billtype = 'gz' and isnull(@total_sale_card, 0) >= isnull(@curcardamt, 0)*isnull(@discount, 1) and isnull(@total_sale_card, 0)>0) or (@billtype = 'gx' and isnull(@one_consume, 0)>0 and isnull(@this_consume, 0) >= isnull(@one_consume, 0)) or (@billtype = 'gx' and isnull(@sum_consume, 0)>0 and isnull(@total_sum_consume, 0) >= isnull(@sum_consume, 0)) begin --达到目标,则停止 select @continue = 0 select @newcardtype = @curcardtype select @ret = 1 end else begin fetch cur_each_gam10 into @curlvl, @curperiod, @curcardamt, @curcardtype, @one_consume, @sum_consume end end close cur_each_gam10 deallocate cur_each_gam10 --根据达到的级别,更改卡的类别 if @continue = 0 begin --说明已经达到更高 --更改卡的类别 exec upg_change_one_card_type @compid, @cardid, @oldcardtype, @newcardtype, '', '', '' --升级后的卡类别需要的默认的账户如果在这个卡中不存在则需要增加这个账户 declare @newcardtype_defaultacct int select @newcardtype_defaultacct = gak14i from gam10 where gak00c = @compid and gak01c = @newcardtype if not exists(select 1 from gcm03 with (nolock) where gcc00c = @ownercompid and gcc01c = @cardid and gcc03i = @newcardtype_defaultacct) begin exec upg_purse_deduct @compid, @cardid, @todate, @newcardtype_defaultacct, 0, 0, @billtype, @billid, 1 end exec upg_send_new_gift @compid, @cardid, @newcardtype --必须把变化的历史写到卡的异动历史中 declare @flowno float select @flowno = max(gcb02f) from gcm02 with (nolock) where gcb01c = @cardid select @flowno = isnull(@flowno, 0) + 1 insert gcm02(gcb00c, gcb01c, gcb02f, gcb03i, gcb04c, gcb05i, gcb06i, gcb07d, gcb08c) values(@ownercompid, @cardid, @flowno, 11, @billid, 4, 4, @todate, @oldcardtype + '->' + @newcardtype) end end GO /****** Object: StoredProcedure [dbo].[upg_manage_points] Script Date: 06/12/2017 06:20:23 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_manage_points] ( @compid varchar(10), -- 公司别 @cardno varchar(20), -- 会员卡号 @billtype varchar(20), -- 单据类别 @billid varchar(20), -- 单据号 @billdate varchar(20), -- 单据日期 @amt float, -- 金额, 已经不用,过程中自己计算积分 @confirm int -- 1-复核 0-取消复核 ) as begin declare @start_point varchar(20) select @start_point = gsa02c from gsm01 where gsa00c = @compid and gsa01c = 'SP110' if isnull(@start_point, '') <> '1' return declare @owncomp varchar(10) declare @gca02c varchar(5) -- 卡类别 select @owncomp = gca00c, @gca02c = gca02c from gcm01 with(nolock) where gca00c = gca13d and gca01c = @cardno declare @gak76i int select @gak76i = gak76i from gam10 with(nolock) where gak00c = @owncomp and gak01c = @gca02c if isnull(@gak76i, 0) = 1 return declare @sp055f float select @sp055f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP055' declare @rate float declare @points float declare @sum_prj_points float declare @sum_god_points float declare @sum_points_pay float declare @sum_prjs_amt float declare @sum_gods_amt float declare @pay_prjs float declare @pay_gods float select @sum_prj_points = sum(ggb33f), @sum_prjs_amt = sum(ggb11f) from ggm02 with (nolock) where ggb00c = @compid and ggb01c = @billid select @sum_god_points = sum(ggc28f), @sum_gods_amt = sum(ggc11f) from ggm03 with (nolock) where ggc00c = @compid and ggc01c = @billid select @sum_prj_points = isnull(@sum_prj_points, 0), @sum_god_points = isnull(@sum_god_points, 0), @sum_prjs_amt = isnull(@sum_prjs_amt, 0), @sum_gods_amt = isnull(@sum_gods_amt, 0) if @sp055f = 0 begin -- 按项目支付,累计的积分比较容易计算 select @points = isnull(@sum_prj_points, 0) + isnull(@sum_god_points, 0) end else begin -- 按单据支付 select @sum_points_pay = sum(gsc05f) from gsm03 with (nolock) where gsc00c = @compid and gsc01c = @billid and gsc02c = 'gx' and gsc04c = '7' select @sum_prjs_amt = isnull(@sum_prjs_amt, 0), @sum_gods_amt = isnull(@sum_gods_amt, 0), @sum_points_pay = isnull(@sum_points_pay, 0) if @sum_points_pay >= @sum_prjs_amt begin select @pay_prjs = @sum_prjs_amt select @pay_gods = @sum_points_pay - @sum_prjs_amt end else begin select @pay_prjs = @sum_points_pay select @pay_gods = 0 end declare @part1 float, @part2 float if @sum_prjs_amt <> 0 select @part1 = @sum_prj_points * ((@sum_prjs_amt - @pay_prjs) / @sum_prjs_amt) else select @part1 = 0 if @sum_gods_amt <> 0 select @part2 = @sum_god_points * ((@sum_gods_amt - @pay_gods) / @sum_gods_amt) else select @part2 = 0 select @points = @part1 + @part2 end -- 积分取整 declare @sp037f float select @sp037f = isnull(gsa03f, 0) from gsm01 where gsa00c = @compid and gsa01c = 'SP037' if @sp037f = 0 select @points = convert(decimal(20, 0), @points) else if @sp037f = 1 select @points = convert(decimal(20, 1), @points) else if @sp037f = 2 select @points = convert(decimal(20, 2), @points) else if @sp037f = 3 select @points = convert(decimal(20, 3), @points) else select @points = convert(decimal(20, 0), @points) exec upg_purse_deduct @compid, @cardno, @billdate, 3, @points, 0, 'gx', @billid, @confirm end GO /****** Object: StoredProcedure [dbo].[upg_dispatch_data] Script Date: 06/12/2017 06:20:00 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_dispatch_data] ( @compid varchar(10), @datatype varchar(20) ) as begin create table #tbl_control ( compid varchar(10) not null, -- 儿子公司的代码 lvl int null, primary key(compid) ) insert #tbl_control(compid, lvl) select bran_id, control_lvl from b_perf a, basic_data_control b where parent_id = @compid and a.bran_id = b.compid and b.datatype = @datatype and b.control_lvl = 1 -- 创建循环变量 declare @HasRecord int set @HasRecord = 1 declare @currentCompId varchar(10) declare @lvl int while (@HasRecord = 1) begin if exists (select 1 from #tbl_control) begin set @HasRecord = 1 select top 1 @currentCompId = compid, @lvl = lvl from #tbl_control if @lvl = 1 -- 严格按总部的 begin -- 先删除自己的,把总部分发下来 -- 强制模式都是先删除原来的 exec upg_m_dispatch_table_single @compid, @datatype, @currentCompId, 1 insert #tbl_control(compid, lvl) select bran_id, control_lvl from b_perf a, basic_data_control b where parent_id = @currentCompId and a.bran_id = b.compid and b.datatype = @datatype and b.control_lvl = 1 end else if @lvl = 2 begin exec upg_m_dispatch_table_single @compid, @datatype, @currentCompId, 0 insert #tbl_control(compid, lvl) select bran_id, case when (control_lvl <= 2) then 2 else control_lvl end from b_perf a, basic_data_control b where parent_id = @currentCompId and a.bran_id = b.compid and b.datatype = @datatype and b.control_lvl = 1 end -- 清除已处理的门店 delete #tbl_control where compid = @currentCompId end else begin set @HasRecord = 0 end end drop table #tbl_control end GO /****** Object: StoredProcedure [dbo].[upg_personal_statistics_noreturn] Script Date: 06/12/2017 06:20:26 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_personal_statistics_noreturn] ( @compid varchar(10), -- 公司别 @fromdate varchar(10), -- 开始日期 @todate varchar(10), -- 截至日期 @fromempl varchar(20), -- 查询开始人员 @toempl varchar(20), -- 查询截至人员 @inc_card int, -- 卡销售 @inc_service int, -- 服务 @inc_goods int, -- 是否包括产品销售 @return_type int, -- 返回类别 @spid int ) -- 返回需要的数据 -- 1: 员工业绩统计表,最明细的数据,如果是累计的情况,没有返回提成的部分 -- 2: 每个员工一个提成数据,用在工资计算的时候,返回只能是一个员工代码 + 提成 -- 3: 员工业绩排行榜,营业额 -- 4: 员工业绩排行榜,虚业绩 -- 5: 员工业绩排行榜,实业绩 -- 6: 总个公司的总提成,用在利润分析表中 -- 7: 单个人按项目分组的数据,用在业绩排行榜中的明细 -- 8, 9, 10: 员工业绩排行对应的3, 4, 5, 但不需要返回结果 as begin declare @shop_vperf float declare @shop_tperf float declare @shop_amt float declare @shop_comm float declare @not_need_return_result int --是否需要返回结果 if (@return_type >= 8 and @return_type <= 10) begin select @return_type = @return_type - 5 select @not_need_return_result = 1 end else begin select @not_need_return_result = 0 end -- 把需要的数据准备好,一个员工可能的所有服务数据 -- 所有的虚业绩都已经在单据保存的时候就准备好 (改为准备所有员工的单据, 因为有大工存在扣中(小)工提成的计算方式, 若不全部添加, 无法计算小工的提成) --Start 创建临时表 add by lm 2015-08-05 create table #b_perf ( bran_id varchar(20) not null ) create table #work_detail_ceshi ( id int identity , -- 流水号 person_id varchar(20) not null, -- 员工代码 action_id tinyint not null, -- 类别,1-卡销售 2-指定大工,3-轮班大工,4-补工大工, -- 5-指定中工,6-轮班中工 -- 7-指定小工, 8-轮班小工,9-产品销售 10,11-四工 -- 0 卡充值 srvdate varchar(10) not null, --日期 code varchar(20) not null, --项目代码,或是卡号,产品码 name varchar(200) null, --名称 payway varchar(20) null, --支付方式 amt float null, --营业金额 quan float null, --数量 cost float null, --成本 comm float null, --提成 amt2 float null, --虚业绩 amt3 float null, --真实业绩 cardrate float null, --转卡比率 --当买卡或充值的时候,这个地方当作疗程账户金额用 stdprice float null, --标准单价 prj_type varchar(20) null, --项目类别 share_rate float null default 1, --分享比率 cls_flag int null, -- 1:项目 2:产品 3:卡 gender int null, --性别 0:女 1:男 billid varchar(20) null, --单号 paycode varchar(20) null, --支付代码 ex_rate float null, --实际营业额折算比率 ggm02_seq float null, --ggm02中的序列号,与单号一起来标志一个项目 room_id varchar(20) null, --房间号,只在spa或足浴版本需要 compid varchar(10) null, --公司别 spid int null, --连接号 cardid varchar(20) null, --会员卡号 memid varchar(40) null, --会员编号 comp_ascription varchar(10) null,-- 作业公司 --constraint PK_#work_detail_ceshi primary key nonclustered(id) ) declare @PK_Sql nvarchar(max) set @PK_Sql = ' alter table #work_detail_ceshi add constraint PK_#work_detail_ceshi_' + convert(varchar(10), @spid) + ' PRIMARY KEY nonclustered(id) ' exec sp_executesql @PK_Sql create clustered index work_detail_ceshi_idx_01 on #work_detail_ceshi(person_id) insert #b_perf(bran_id) select gaz02c from gam26 with(nolock) where gaz01c = @compid -- 创建循环变量 declare @HasRecord int set @HasRecord = 1 declare @incompid varchar(10) -- 插入旗下所有子门店的营业数据 while(@HasRecord = 1) begin if exists(select 1 from #b_perf) begin set @HasRecord = 1 select top 1 @incompid = bran_id from #b_perf exec upg_prepare_work_detail @spid, @incompid, @fromdate, @todate, @fromempl, @toempl, @inc_card, @inc_service, @inc_goods, @return_type -- 保存原公司别 update #work_detail_ceshi set comp_ascription = @compid where spid = @spid -- 清除已处理的数据 delete #b_perf where bran_id = @incompid end else begin set @HasRecord = 0 end end drop table #b_perf --End -- 将所有门店编号改为总店编号 update a set a.compid = @compid from #work_detail_ceshi a where spid = @spid and compid <> @compid -- 计算实业绩,并尽量计算可能的提成,如果累计的则无法计算出来(计算之后删除表work_detail中除@fromempl与@toempl之外的数据) exec upg_cal_work_detail_comm @spid, @compid, @fromdate, @todate, @fromempl, @toempl -- 计算额外的业绩提成 exec upg_cal_work_detail_add_comm @spid, @compid, @fromdate, @todate, @fromempl, @toempl --计算竞争转卡业绩,单独计算(业绩:填写的金额为业绩,不走提成方案;提成:业绩乘以卡业绩提成比率) exec upg_cal_work_detail_gem01 @spid, @compid, @fromdate, @todate, @fromempl, @toempl -- 把每个员工这个时间段内的指定与轮班统计出来 added 2013-03-20 exec upg_create_person_stat @compid, @fromdate, @todate if (@return_type = 7) begin -- 按项目代码进行分组统计 exec upg_cal_work_detail_by_item @spid, @compid, @fromdate, @todate, @fromempl, @toempl end else begin -- 按项目类别分组进行分组统计, 按员工项目类别累计来计算每个员工的提成 exec upg_cal_work_detail_by_kind @spid, @compid, @fromdate, @todate, @fromempl, @toempl -- 按员工进行分组统计, 按员工累计来计算每个员工的提成, 注意是一个员工总的业绩(包含项目,产品,卡) -- 按项目,卡,物品销售分别累计计算提成的(不分项目类别,也需要在此计算) exec upg_cal_work_detail_by_empl @spid, @compid, @fromdate, @todate, @fromempl, @toempl, @return_type -- 计算每个员工可能的人头费提成 exec upg_cal_work_detail_by_empl_num @spid, @compid, @fromdate, @todate, @fromempl, @toempl, @return_type -- 计算整个店的总业绩 exec upg_cal_work_detail_by_shop @spid, @compid, @fromdate, @todate, @fromempl, @toempl, @shop_amt output, @shop_vperf output, @shop_tperf output -- 需要计算某些员工的提成,是按照店总业绩来计算的 exec upg_cal_work_detail_by_empl_shop @spid, @compid, @fromdate, @todate, @fromempl, @toempl, @shop_amt, @shop_vperf, @shop_tperf end delete work_detail where spid = @spid insert work_detail (person_id ,action_id ,srvdate,code ,name,payway,amt,quan,cost,comm, amt2,amt3,cardrate,stdprice,prj_type,share_rate,cls_flag,gender,billid, paycode,ex_rate,ggm02_seq,room_id,compid,spid,cardid ,memid,comp_ascription) select person_id ,action_id ,srvdate,code ,name,payway,amt,quan,cost,comm, amt2,amt3,cardrate,stdprice,prj_type,share_rate,cls_flag,gender,billid, paycode,ex_rate,ggm02_seq,room_id,compid,spid,cardid ,memid,comp_ascription from #work_detail_ceshi where spid = @spid -- 返回需要的数据 -- 1: 员工业绩统计表,最明细的数据,如果是累计的情况,没有返回提成的部分 -- 2: 每个员工一个提成数据,用在工资计算的时候,返回只能是一个员工代码 + 提成 -- 3: 员工业绩排行榜,营业额 -- 4: 员工业绩排行榜,虚业绩 -- 5: 员工业绩排行榜,实业绩 -- 6: 总个公司的总提成,用在利润分析表中 -- 7: 单个人按项目分组的数据,用在业绩排行榜中的明细 if @return_type = 1 begin -- 根据版本的不同,看是否需要计算房间号 declare @sp056 varchar(20) select @sp056 = gsa02c from gsm01 with(nolock) where gsa00c = @compid and gsa01c = 'SP056' if @sp056 = '4' or @sp056 = '3' begin update a set a.room_id = b.glg04v from #work_detail_ceshi a, glm07 b with(nolock), ggm01 c with(nolock) where a.billid = c.gga01c and c.gga00c = @compid and b.glg00c = @compid and c.gga30c = b.glg01c and a.code = b.glg06c and a.cls_flag = 1 and spid = @spid end -- 把会员号更新正确, 会员编号的位置存放会员姓名 update a set a.memid = b.gba03c from #work_detail_ceshi a with(nolock), gbm01 b with(nolock) where spid = @spid and a.cardid = b.gba23c and b.gba00c = @compid end drop table #work_detail_ceshi end GO /****** Object: StoredProcedure [dbo].[upg_personal_statistics] Script Date: 06/12/2017 06:20:26 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_personal_statistics] ( @compid varchar(10), -- 公司别 @fromdate varchar(10), -- 开始日期 @todate varchar(10), -- 截至日期 @fromempl varchar(20), -- 查询开始人员 @toempl varchar(20), -- 查询截至人员 @inc_card int, -- 卡销售 @inc_service int, -- 服务 @inc_goods int, -- 是否包括产品销售 @return_type int, -- 返回类别 @spid int ) -- 返回需要的数据 -- 1: 员工业绩统计表,最明细的数据,如果是累计的情况,没有返回提成的部分 -- 2: 每个员工一个提成数据,用在工资计算的时候,返回只能是一个员工代码 + 提成 -- 3: 员工业绩排行榜,营业额 -- 4: 员工业绩排行榜,虚业绩 -- 5: 员工业绩排行榜,实业绩 -- 6: 总个公司的总提成,用在利润分析表中 -- 7: 单个人按项目分组的数据,用在业绩排行榜中的明细 -- 8, 9, 10: 员工业绩排行对应的3, 4, 5, 但不需要返回结果 as begin declare @shop_vperf float declare @shop_tperf float declare @shop_amt float declare @shop_comm float declare @not_need_return_result int --是否需要返回结果 if (@return_type >= 8 and @return_type <= 10) begin select @return_type = @return_type - 5 select @not_need_return_result = 1 end else begin select @not_need_return_result = 0 end -- 把需要的数据准备好,一个员工可能的所有服务数据 -- 所有的虚业绩都已经在单据保存的时候就准备好 (改为准备所有员工的单据, 因为有大工存在扣中(小)工提成的计算方式, 若不全部添加, 无法计算小工的提成) --Start 创建临时表 add by lm 2015-08-05 create table #b_perf ( bran_id varchar(20) not null ) create table #work_detail_ceshi ( id int identity , -- 流水号 person_id varchar(20) not null, -- 员工代码 action_id tinyint not null, -- 类别,1-卡销售 2-指定大工,3-轮班大工,4-补工大工, -- 5-指定中工,6-轮班中工 -- 7-指定小工, 8-轮班小工,9-产品销售 10,11-四工 -- 0 卡充值 srvdate varchar(10) not null, --日期 code varchar(20) not null, --项目代码,或是卡号,产品码 name varchar(200) null, --名称 payway varchar(40) null, --支付方式 amt float null, --营业金额 quan float null, --数量 cost float null, --成本 comm float null, --提成 amt2 float null, --虚业绩 amt3 float null, --真实业绩 cardrate float null, --转卡比率 --当买卡或充值的时候,这个地方当作疗程账户金额用 stdprice float null, --标准单价 prj_type varchar(20) null, --项目类别 share_rate float null default 1, --分享比率 cls_flag int null, -- 1:项目 2:产品 3:卡 gender int null, --性别 0:女 1:男 billid varchar(20) null, --单号 paycode varchar(20) null, --支付代码 ex_rate float null, --实际营业额折算比率 ggm02_seq float null, --ggm02中的序列号,与单号一起来标志一个项目 room_id varchar(20) null, --房间号,只在spa或足浴版本需要 compid varchar(10) null, --公司别 spid int null, --连接号 cardid varchar(20) null, --会员卡号 memid varchar(40) null, --会员编号 comp_ascription varchar(10) null,-- 作业公司 repayment_sign int null --还款标志 --constraint PK_#work_detail_ceshi primary key nonclustered(id) ) declare @PK_Sql nvarchar(max) set @PK_Sql = ' alter table #work_detail_ceshi add constraint PK_#work_detail_ceshi_' + convert(varchar(10), @spid) + ' PRIMARY KEY nonclustered(id) ' exec sp_executesql @PK_Sql create clustered index work_detail_ceshi_idx_01 on #work_detail_ceshi(person_id) insert #b_perf(bran_id) select gaz02c from gam26 with(nolock) where gaz01c = @compid and gaz02c in (select gae01c from gam05) -- 创建循环变量 declare @HasRecord int set @HasRecord = 1 declare @incompid varchar(10) -- 插入旗下所有子门店的营业数据 while(@HasRecord = 1) begin if exists(select 1 from #b_perf) begin set @HasRecord = 1 select top 1 @incompid = bran_id from #b_perf exec upg_prepare_work_detail @spid, @incompid, @fromdate, @todate, @fromempl, @toempl, @inc_card, @inc_service, @inc_goods, @return_type -- 保存原公司别 update #work_detail_ceshi set comp_ascription = @compid where spid = @spid -- 清除已处理的数据 delete #b_perf where bran_id = @incompid end else begin set @HasRecord = 0 end end drop table #b_perf --End -- 将所有门店编号改为总店编号 update a set a.compid = @compid from #work_detail_ceshi a where spid = @spid and compid <> @compid -- 计算实业绩,并尽量计算可能的提成,如果累计的则无法计算出来(计算之后删除表work_detail中除@fromempl与@toempl之外的数据) exec upg_cal_work_detail_comm @spid, @compid, @fromdate, @todate, @fromempl, @toempl -- 计算额外的业绩提成 exec upg_cal_work_detail_add_comm @spid, @compid, @fromdate, @todate, @fromempl, @toempl --计算竞争转卡业绩,单独计算(业绩:填写的金额为业绩,不走提成方案;提成:业绩乘以卡业绩提成比率) exec upg_cal_work_detail_gem01 @spid, @compid, @fromdate, @todate, @fromempl, @toempl,@inc_card -- 把每个员工这个时间段内的指定与轮班统计出来 added 2013-03-20 exec upg_create_person_stat @compid, @fromdate, @todate if (@return_type = 7) begin -- 按项目代码进行分组统计 exec upg_cal_work_detail_by_item @spid, @compid, @fromdate, @todate, @fromempl, @toempl end else begin -- 按项目类别分组进行分组统计, 按员工项目类别累计来计算每个员工的提成 exec upg_cal_work_detail_by_kind @spid, @compid, @fromdate, @todate, @fromempl, @toempl -- 按员工进行分组统计, 按员工累计来计算每个员工的提成, 注意是一个员工总的业绩(包含项目,产品,卡) -- 按项目,卡,物品销售分别累计计算提成的(不分项目类别,也需要在此计算) -- upg_cal_work_detail_by_empl_no_mobile exec upg_cal_work_detail_by_empl_no_mobile @spid, @compid, @fromdate, @todate, @fromempl, @toempl, @return_type -- 计算每个员工可能的人头费提成 exec upg_cal_work_detail_by_empl_num @spid, @compid, @fromdate, @todate, @fromempl, @toempl, @return_type -- 计算整个店的总业绩 exec upg_cal_work_detail_by_shop @spid, @compid, @fromdate, @todate, @fromempl, @toempl, @shop_amt output, @shop_vperf output, @shop_tperf output -- 需要计算某些员工的提成,是按照店总业绩来计算的 exec upg_cal_work_detail_by_empl_shop @spid, @compid, @fromdate, @todate, @fromempl, @toempl, @shop_amt, @shop_vperf, @shop_tperf end delete work_detail where spid = @spid insert work_detail (person_id ,action_id ,srvdate,code ,name,payway,amt,quan,cost,comm, amt2,amt3,cardrate,stdprice,prj_type,share_rate,cls_flag,gender,billid, paycode,ex_rate,ggm02_seq,room_id,compid,spid,cardid ,memid,comp_ascription) select person_id ,action_id ,srvdate,code ,name,payway,amt,quan,cost,comm, amt2,amt3,cardrate,stdprice,prj_type,share_rate,cls_flag,gender,billid, paycode,ex_rate,ggm02_seq,room_id,compid,spid,cardid ,memid,comp_ascription from #work_detail_ceshi where spid = @spid -- 返回需要的数据 -- 1: 员工业绩统计表,最明细的数据,如果是累计的情况,没有返回提成的部分 -- 2: 每个员工一个提成数据,用在工资计算的时候,返回只能是一个员工代码 + 提成 -- 3: 员工业绩排行榜,营业额 -- 4: 员工业绩排行榜,虚业绩 -- 5: 员工业绩排行榜,实业绩 -- 6: 总个公司的总提成,用在利润分析表中 -- 7: 单个人按项目分组的数据,用在业绩排行榜中的明细 if @return_type = 1 begin -- 根据版本的不同,看是否需要计算房间号 declare @sp056 varchar(20) select @sp056 = gsa02c from gsm01 with(nolock) where gsa00c = @compid and gsa01c = 'SP056' if @sp056 = '4' or @sp056 = '3' begin update a set a.room_id = b.glg04v from #work_detail_ceshi a, glm07 b with(nolock), ggm01 c with(nolock) where a.billid = c.gga01c and c.gga00c = @compid and b.glg00c = @compid and c.gga30c = b.glg01c and a.code = b.glg06c and a.cls_flag = 1 and spid = @spid end -- 把会员号更新正确, 会员编号的位置存放会员姓名 update a set a.memid = b.gba03c from #work_detail_ceshi a with(nolock), gbm01 b with(nolock) where spid = @spid and a.cardid = b.gba23c and b.gba00c = @compid -- 员工业绩统计表需要的数据 select person_id, comp_ascription, action_id, srvdate, code, name, payway, amt, quan, cost, comm, amt2, amt3, cardrate, stdprice, prj_type, share_rate, cls_flag, gender, billid, paycode, ex_rate, ggm02_seq, room_id, cardid, memid from #work_detail_ceshi with (nolock) where spid = @spid order by person_id, prj_type, action_id, srvdate end else if @return_type = 2 begin select person_id, isnull(comm, 0) from workdetail_by_empl with (nolock) where spid = @spid end else if (@return_type = 3 or @return_type = 4 or @return_type = 5) begin if (@not_need_return_result = 0) begin select id, person_id, name, amt_c, amt_1, amt_12, amt_13, amt_2, amt_21, amt_3, amt_31, amt_4, amt_41, amt_1_14, amt_1_15, amt_2_14, amt_2_15, amt_3_14, amt_3_15, amt_4_14, amt_4_15, amt_g, amtrate_1, comm, total_srv, total_perf, total_cash, total_cash_c, bonus, total_srv_cash, total_srv_card, num_fix, num_nofix, num_in, num_outer, consume_cost, compid from workdetail_by_empl with (nolock) where spid = @spid order by isnull(amt_c, 0) + isnull(amt_1, 0) + isnull(amt_12, 0) + isnull(amt_13, 0) + isnull(amt_2, 0) + isnull(amt_21, 0) + isnull(amt_3, 0) + isnull(amt_31, 0) + isnull(amt_4, 0) + isnull(amt_41, 0) + isnull(amt_g, 0) desc end end else if (@return_type = 6) begin select sum(comm) from workdetail_by_empl with (nolock) where spid = @spid end else if (@return_type = 7) begin select idd, person_id , -- 员工代码 cls_flag , -- 1:项目 2:产品 3:卡 action_id , code , --项目代码,或是卡号,产品码 isnull(name,'') [name], --名称 isnull(amt,0) amt , --营业金额 isnull(pt_amt,0) pt_amt , --项目总营业额 isnull(rt_amt,0) rt_amt , --营业额占项目总营业额 isnull(amt_f,0) amt_f , --女营业额 isnull(amt_m ,0) amt_m , isnull(quan,0) quan , --数量 isnull(pt_quan,0) pt_quan , --项目总合计 isnull(rt_quan,0) rt_quan , --数量占该项目总合计比率 isnull(quan_f,0) quan_f , isnull(quan_m,0) quan_m , isnull(amt2,0) amt2 , --虚业绩 isnull(pt_amt2,0) pt_amt2, --项目总虚业绩 isnull(rt_amt2,0) rt_amt2 , --虚业绩占项目总虚业绩 isnull(cost,0) cost , --成本 isnull(pt_cost,0) pt_cost , --项目成本 isnull(rt_cost,0) rt_cost, --成本比率 isnull(amt3,0) amt3 , --真实业绩 isnull(pt_amt3,0) pt_amt3 , --项目总真实业绩 isnull(rt_amt3,0) rt_amt3 , --真实业绩占项目总真实业绩 isnull(comm,0) comm , --提成 isnull( pt_comm,0) pt_comm , --项目总提成 isnull(rt_comm,0) rt_comm , --提成占项目总提成 isnull(prj_amt_rt,0) prj_amt_rt, --占这个人所有金额的比率 isnull(compid,'') compid, --公司别 isnull(fromdate,'') fromdate, --开始日期 isnull(todate,'') todate --截止日期 from workdetail_by_item with (nolock) where spid = @spid order by cls_flag, code, action_id end drop table #work_detail_ceshi end GO /****** Object: StoredProcedure [dbo].[upg_compute_all_perf] Script Date: 06/12/2017 06:19:43 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_compute_all_perf] ( @compid varchar(10), @fromdate varchar(8), @todate varchar(8) -- 公司别 ) as--WUXM, 2005/07/21 begin declare @custflag varchar(10) select @custflag = gsa02c from gsm01 where gsa00c = @compid and gsa01c = 'SP109' declare @billid varchar(20) declare @inner_compid varchar(20) declare cur_all_bill cursor for select gga00c, gga01c from ggm01 a, gam26 z where gga00c = z.gaz02c and z.gaz01c = @compid and gga02d >= @fromdate and gga02d <= @todate open cur_all_bill fetch cur_all_bill into @inner_compid, @billid while @@fetch_status = 0 begin exec upg_compute_cost_perf @inner_compid, @billid fetch cur_all_bill into @inner_compid, @billid end close cur_all_bill deallocate cur_all_bill end GO /****** Object: StoredProcedure [dbo].[upg_gcm10_confirm] Script Date: 06/12/2017 06:20:04 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_gcm10_confirm] ( @compid varchar(10), --公司编号 @billno varchar(20), --异动单号/充值单号 @changedate varchar(8), --异动日期 @checkman varchar(10), --复核人 @currdate varchar(8), --复核日期 @flag int, --标志位 (1- 复核, 0- 取消复核) @operateid int, --操作标志(1-存款 0-取款 5-欠款 6-还款) @acctid varchar(5) --账户类别 (1- 电子钱包, 2- 储值账户) ) as begin set nocount on -- 当前的事务数 declare @__transaction_count int set @__transaction_count = @@trancount -- try...catch处理 begin try -- a.开启事务,或者设置事务保存点 if @__transaction_count = 0 begin tran else save tran __tran_SavePoint declare @__sp_re int if @flag = 1 --复核 begin --设置复核标志 exec upg_set_confirm_state @compid, 'gz', @billno, @checkman, @currdate, '' declare @isupgrade int declare @newcardtype varchar(20) declare @oldcardtype varchar(20) declare @cardid varchar(20) declare @newvaliddate varchar(10) -- 新的有效日期 select @isupgrade = gcl14i, @newcardtype = gcl15c, @cardid = gcl03c, @oldcardtype = gcl52c, @newvaliddate = gcl21d from gcm10 with (nolock) where gcl00c = @compid and gcl01c = @billno if isnull(@isupgrade, 0) = 1 begin exec upg_change_one_card_type @compid, @cardid, @oldcardtype, @newcardtype, '', '', '' end else begin --会员卡类别,如果没有升级,则保留原来的会员卡类别 --此类别在计算业绩提成的时候会用到 update a set a.gcl15c = @oldcardtype from gcm10 a where a.gcl01c = @billno and a.gcl00c = @compid end if @operateid = 0 or @operateid = 6 begin update a set gca14i = isnull(gca14i, 0) + isnull(gcl55f, 0) from gcm10 b, gcm01 a where a.gca13d = a.gca00c and b.gcl03c = a.gca01c and a.gca03i = 3 and b.gcl01c = @billno and b.gcl00c = @compid end else begin update a set gca14i = isnull(gca14i, 0) - isnull(gcl55f, 0) from gcm10 b, gcm01 a where a.gca13d = a.gca00c and b.gcl03c = a.gca01c and a.gca03i = 3 and b.gcl01c = @billno and b.gcl00c = @compid end --增加帐务日期 added by WUXM 2005/08/26 exec upg_updategcm10_accountdate @compid, @billno --设置购买疗程中的支付方式 exec upg_create_buyproc_payment @compid, 'gz', @billno --Added by WUXM, 2006/09/18 exec upg_create_gcm20_from_one_bill @compid, 'gz', @billno, 1 --还款标志的设定 if @operateid = 6 and @acctid = '888' begin exec upg_set_return_flag @compid, @billno, @currdate, 1 end --通知正在入院的消费单 exec upg_inform_glm01_when_fill @compid, @billno --调整卡的新的有效日期 -- if isnull(@newvaliddate, '') <> '' and @newvaliddate > @changedate -- begin -- update gcm01 -- set gca07d = @newvaliddate -- where gca01c = @cardid and gca00c = gca13d -- end end else --取消复核 begin update gcm10 set gcl93c = '', gcl94d = '', gcl80d = '' where gcl00c = @compid and gcl01c = @billno and gcl02d = @changedate and gcl04c = @acctid update a set a.gsc10d = '' from gsm03 a where gsc00c = @compid and gsc01c = @billno and gsc02c = 'gz' if @operateid = 0 or @operateid = 6 begin update a set gca14i = isnull(gca14i, 0) - isnull(gcl55f, 0) from gcm10 b, gcm01 a where a.gca13d = a.gca00c and b.gcl03c = a.gca01c and a.gca03i = 3 and b.gcl01c = @billno and b.gcl00c = @compid end else begin update a set gca14i = isnull(gca14i, 0) + isnull(gcl55f, 0) from gcm10 b, gcm01 a where a.gca13d = a.gca00c and b.gcl03c = a.gca01c and a.gca03i = 3 and b.gcl01c = @billno and b.gcl00c = @compid end --Added by WUXM, 2006/09/18 exec upg_create_gcm20_from_one_bill @compid, 'gz', @billno, 0 --如果还款是项目或产品的还款, 则要调整项目或产品的还款标志 --还款标志的设定 if @operateid = 6 and @acctid = '888' begin exec upg_set_return_flag @compid, @billno, @currdate, 0 end --added by cyr 2012/10/10 begin --赠送账户 declare @gcl107c varchar(10) --赠送的账户 declare @gcl108f float --赠送的金额 declare @oricompid nvarchar(20) --卡归属公司 select @gcl107c = gcl107c, @gcl108f = gcl108f, @cardid = gcl03c from gcm10 where gcl00c = @compid and gcl01c = @billno select top 1 @oricompid = gca00c from gcm01 where gca00c = gca13d and gca01c = @cardid if isnull(@gcl107c, '') <> '' begin exec upg_purse_deduct @compid, @cardid, @changedate, @gcl107c, @gcl108f, 7, 'gz', @billno, 0 --赠送账户,金额不应该清空掉 del lucz 2015.11.11 -- update gcm10 -- set gcl107c = '', -- gcl108f = 0 -- where gcl00c = @compid and gcl01c = @billno end end -- 提交事务 -- 有可提交的事务, 并且事务是在当前模块中开启的情况下, 才提交事务 if xact_state() = 1 and @__transaction_count = 0 commit -- 为了防止TRY 中有遗漏的事务处理, 可以在TRY 模拟的结束部分做最终的判断 if @__transaction_count = 0 begin if xact_state() = -1 rollback tran else begin while @@trancount > 0 commit tran end end end try begin catch -- 在CATCH 模块, 应该首先处理事务 if xact_state() <> 0 begin if @__transaction_count = 0 rollback tran -- xact_state 为-1 时, 不能回滚到事务保存点, 这种情况留给外层调用者做统一的事务回滚 -- 通过@@trancount > @__transaction_count 的判断, 即使在TRY 模块中没有设置事务保存点的情况下跳到此步骤, 也不会出错 else if xact_state() = 1 and @@trancount > @__transaction_count rollback tran __tran_SavePoint end return -1 end catch end GO /****** Object: StoredProcedure [dbo].[upg_cal_fill_bill_perf] Script Date: 06/12/2017 06:19:32 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_cal_fill_bill_perf] ( @compid varchar(10), -- 公司编号 @billtype varchar(5), -- 单据类别 'gn' = 会员卡销售 'gz' = 会员帐户管理 @billid varchar(20) -- 单号 ) as begin exec upg_prepare_fill_card_detail @compid, @billtype, @billid exec upg_cal_fill_card_detail_perf @compid, @billtype, @billid create table #tbl_bill_perf ( idd int identity not null, compid varchar(20) not null, billtype varchar(20) not null, billid varchar(20) not null, person_pos int null, perf float null, primary key(idd) ) insert #tbl_bill_perf(compid, billtype, billid, person_pos, perf) select @compid, @billtype, @billid, person_pos, sum(perf) from card_fill_detail_by_payment a with (nolock) where compid = @compid and billtype = @billtype and billid = @billid group by person_pos if @billtype = 'gn' begin update a set a.gna67f = b.perf from gnm01 a, #tbl_bill_perf b where a.gna00c = b.compid and a.gna01c = b.billid and a.gna00c = @compid and a.gna01c = @billid and b.person_pos = 1 -- 第一销售的位置 update a set a.gna69f = b.perf from gnm01 a, #tbl_bill_perf b where a.gna00c = b.compid and a.gna01c = b.billid and a.gna00c = @compid and a.gna01c = @billid and b.person_pos = 2 -- 第2销售的位置 update a set a.gna71f = b.perf from gnm01 a, #tbl_bill_perf b where a.gna00c = b.compid and a.gna01c = b.billid and a.gna00c = @compid and a.gna01c = @billid and b.person_pos = 3 -- 第3销售的位置 update a set a.gna86f = b.perf from gnm01 a, #tbl_bill_perf b where a.gna00c = b.compid and a.gna01c = b.billid and a.gna00c = @compid and a.gna01c = @billid and b.person_pos = 4 -- 第4销售的位置 update a set a.gna87f = b.perf from gnm01 a, #tbl_bill_perf b where a.gna00c = b.compid and a.gna01c = b.billid and a.gna00c = @compid and a.gna01c = @billid and b.person_pos = 5 -- 第5销售的位置 update a set a.gna88f = b.perf from gnm01 a, #tbl_bill_perf b where a.gna00c = b.compid and a.gna01c = b.billid and a.gna00c = @compid and a.gna01c = @billid and b.person_pos = 6 -- 第6销售的位置 update a set a.gna89f = b.perf from gnm01 a, #tbl_bill_perf b where a.gna00c = b.compid and a.gna01c = b.billid and a.gna00c = @compid and a.gna01c = @billid and b.person_pos = 7 -- 第7销售的位置 update a set a.gna90f = b.perf from gnm01 a, #tbl_bill_perf b where a.gna00c = b.compid and a.gna01c = b.billid and a.gna00c = @compid and a.gna01c = @billid and b.person_pos = 8 -- 第8销售的位置 update a set a.gna91f = b.perf from gnm01 a, #tbl_bill_perf b where a.gna00c = b.compid and a.gna01c = b.billid and a.gna00c = @compid and a.gna01c = @billid and b.person_pos = 9 -- 第9销售的位置 update a set a.gna92f = b.perf from gnm01 a, #tbl_bill_perf b where a.gna00c = b.compid and a.gna01c = b.billid and a.gna00c = @compid and a.gna01c = @billid and b.person_pos = 10 -- 第十销售的位置 end else if @billtype = 'gz' begin --判断充值 是不是 疗程账户还款 --疗程账户还款需要加上乘上比率;原因是前面在算疗程还款时,因为多个支付方式导致#tbl_bill_perf表中数据重复 --原因:gcm11表中gcm33f列是总的还款金额,多个支付方式则重复 if exists(select 1 from gcm10 where gcl00c = @compid and gcl01c = @billid and gcl04c = '4' and gcl06i = 6) begin update a set a.gcl58f = b.perf * gcl16f from gcm10 a, #tbl_bill_perf b where a.gcl00c = b.compid and a.gcl01c = b.billid and a.gcl00c = @compid and a.gcl01c = @billid and b.person_pos = 1 -- 第一销售的位置 update a set a.gcl60f = b.perf * gcl18f from gcm10 a, #tbl_bill_perf b where a.gcl00c = b.compid and a.gcl01c = b.billid and a.gcl00c = @compid and a.gcl01c = @billid and b.person_pos = 2 -- 第2销售的位置 update a set a.gcl62f = b.perf * gcl20f from gcm10 a, #tbl_bill_perf b where a.gcl00c = b.compid and a.gcl01c = b.billid and a.gcl00c = @compid and a.gcl01c = @billid and b.person_pos = 3 -- 第3销售的位置 update a set a.gcl83f = b.perf * gcl23f from gcm10 a, #tbl_bill_perf b where a.gcl00c = b.compid and a.gcl01c = b.billid and a.gcl00c = @compid and a.gcl01c = @billid and b.person_pos = 4 -- 第4销售的位置 update a set a.gcl84f = b.perf * gcl25f from gcm10 a, #tbl_bill_perf b where a.gcl00c = b.compid and a.gcl01c = b.billid and a.gcl00c = @compid and a.gcl01c = @billid and b.person_pos = 5 -- 第5销售的位置 update a set a.gcl85f = b.perf * gcl27f from gcm10 a, #tbl_bill_perf b where a.gcl00c = b.compid and a.gcl01c = b.billid and a.gcl00c = @compid and a.gcl01c = @billid and b.person_pos = 6 -- 第6销售的位置 update a set a.gcl86f = b.perf *gcl29f from gcm10 a, #tbl_bill_perf b where a.gcl00c = b.compid and a.gcl01c = b.billid and a.gcl00c = @compid and a.gcl01c = @billid and b.person_pos = 7 -- 第7销售的位置 update a set a.gcl87f = b.perf * gcl31f from gcm10 a, #tbl_bill_perf b where a.gcl00c = b.compid and a.gcl01c = b.billid and a.gcl00c = @compid and a.gcl01c = @billid and b.person_pos = 8 -- 第8销售的位置 update a set a.gcl88f = b.perf * gcl33f from gcm10 a, #tbl_bill_perf b where a.gcl00c = b.compid and a.gcl01c = b.billid and a.gcl00c = @compid and a.gcl01c = @billid and b.person_pos = 9 -- 第9销售的位置 update a set a.gcl89f = b.perf * gcl35f from gcm10 a, #tbl_bill_perf b where a.gcl00c = b.compid and a.gcl01c = b.billid and a.gcl00c = @compid and a.gcl01c = @billid and b.person_pos = 10 -- 第十销售的位置 end else -- 非疗程还款的 begin update a set a.gcl58f = b.perf from gcm10 a, #tbl_bill_perf b where a.gcl00c = b.compid and a.gcl01c = b.billid and a.gcl00c = @compid and a.gcl01c = @billid and b.person_pos = 1 -- 第一销售的位置 update a set a.gcl60f = b.perf from gcm10 a, #tbl_bill_perf b where a.gcl00c = b.compid and a.gcl01c = b.billid and a.gcl00c = @compid and a.gcl01c = @billid and b.person_pos = 2 -- 第2销售的位置 update a set a.gcl62f = b.perf from gcm10 a, #tbl_bill_perf b where a.gcl00c = b.compid and a.gcl01c = b.billid and a.gcl00c = @compid and a.gcl01c = @billid and b.person_pos = 3 -- 第3销售的位置 update a set a.gcl83f = b.perf from gcm10 a, #tbl_bill_perf b where a.gcl00c = b.compid and a.gcl01c = b.billid and a.gcl00c = @compid and a.gcl01c = @billid and b.person_pos = 4 -- 第4销售的位置 update a set a.gcl84f = b.perf from gcm10 a, #tbl_bill_perf b where a.gcl00c = b.compid and a.gcl01c = b.billid and a.gcl00c = @compid and a.gcl01c = @billid and b.person_pos = 5 -- 第5销售的位置 update a set a.gcl85f = b.perf from gcm10 a, #tbl_bill_perf b where a.gcl00c = b.compid and a.gcl01c = b.billid and a.gcl00c = @compid and a.gcl01c = @billid and b.person_pos = 6 -- 第6销售的位置 update a set a.gcl86f = b.perf from gcm10 a, #tbl_bill_perf b where a.gcl00c = b.compid and a.gcl01c = b.billid and a.gcl00c = @compid and a.gcl01c = @billid and b.person_pos = 7 -- 第7销售的位置 update a set a.gcl87f = b.perf from gcm10 a, #tbl_bill_perf b where a.gcl00c = b.compid and a.gcl01c = b.billid and a.gcl00c = @compid and a.gcl01c = @billid and b.person_pos = 8 -- 第8销售的位置 update a set a.gcl88f = b.perf from gcm10 a, #tbl_bill_perf b where a.gcl00c = b.compid and a.gcl01c = b.billid and a.gcl00c = @compid and a.gcl01c = @billid and b.person_pos = 9 -- 第9销售的位置 update a set a.gcl89f = b.perf from gcm10 a, #tbl_bill_perf b where a.gcl00c = b.compid and a.gcl01c = b.billid and a.gcl00c = @compid and a.gcl01c = @billid and b.person_pos = 10 -- 第十销售的位置 end end drop table #tbl_bill_perf end GO /****** Object: StoredProcedure [dbo].[upg_change_paymode_when_out] Script Date: 06/12/2017 06:19:39 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_change_paymode_when_out] ( @compid varchar(10), @enterno varchar(20), @cardid varchar(20), @paymode varchar(10) -- 支付方式 ) as begin declare @accttype_c varchar(10) select @accttype_c = gsn02c from gsm14 where gsn01c = @paymode declare @accttype int if (isnull(@accttype_c, '') = '' or @accttype = '999') begin --用卡但用非卡付,是否有折扣决定于哪种卡了 select @accttype = 0 end else begin select @accttype = convert(int, @accttype_c) end update ggm01 set gga27i = @accttype where gga00c = @compid and gga30c = @enterno exec upg_recal_bill_discount @compid, @enterno end GO /****** Object: StoredProcedure [dbo].[upg_add_money_expend] Script Date: 06/12/2017 06:19:24 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_add_money_expend] ( @compid varchar(10), -- 公司别 @cardno varchar(30), -- 卡号 @amount float, -- 金额 -- 已经扣除了欠款部分 @defict float, -- 欠款金额 @confirm int, -- 是否复核 1-存款复核 0-存款取消复核 3-取款取消复核 2-取款复核 @type int, -- 2:储值 1:电子钱包 @billtype varchar(20), @billid varchar(20) ) as--wuxm, 2004/08/14 begin declare @changeway int declare @curdate varchar(20) declare @totalamt float select @curdate = substring(convert(varchar(20), getdate(), 102), 1, 4) + substring(convert(varchar(20), getdate(), 102), 6, 2) + substring(convert(varchar(20), getdate(), 102), 9, 2) declare @confirmflag int if @confirm = 1 -- 存款复核 begin select @changeway = 0 -- 存款 select @confirmflag = 1 -- 复核 end else if @confirm = 0 -- 存款取消符合 begin select @changeway = 0 -- 存款 select @confirmflag = 0 -- 取消复核 end else if @confirm = 3 -- 取款,取消符合 begin select @changeway = 1 -- 取款 select @confirmflag = 0 -- 取消符合 end else if @confirm = 2 -- 取款,复核 begin select @changeway = 1 -- 取款 select @confirmflag = 1 -- 复核 end select @totalamt = @amount + isnull(@defict, 0) exec upg_purse_deduct @compid, @cardno, @curdate, @type, @totalamt, @changeway, @billtype, @billid, @confirmflag if (@defict > 0) begin if @confirm = 1 select @changeway = 5 -- 欠款 else select @changeway = 6 -- 还款 exec upg_purse_deduct @compid, @cardno, @curdate, @type, @defict, @changeway, @billtype, @billid, 1 end end GO /****** Object: StoredProcedure [dbo].[upg_add_money_new] Script Date: 06/12/2017 06:19:24 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_add_money_new] ( @compid varchar(10), -- 公司别 @cardno varchar(30), -- 卡号 @amount float, -- 金额 @confirm int, -- 是否复核 1 存款复核 0 存款取消复核 3 取款取消复核 2 取款 复核 @type int, -- 账户2:储值 1:电子钱包 @billtype varchar(20), @billid varchar(20), @curdate varchar(8) ) as--wuxm, 2004/08/14 begin declare @changeway int declare @confirmflag int if @confirm = 1 -- 存款复核 begin select @changeway = 0 -- 存款 select @confirmflag = 1 -- 复核 end else if @confirm = 0 -- 存款取消符合 begin select @changeway = 0 -- 存款 select @confirmflag = 0 -- 取消复核 end else if @confirm = 3 -- 取款,取消复核 begin select @changeway = 1 -- 取款 select @confirmflag = 0 -- 取消复核 end else if @confirm = 2 -- 取款,复核 begin select @changeway = 1 -- 取款 select @confirmflag = 1 -- 复核 end declare @__sp_re int exec @__sp_re = upg_purse_deduct @compid, @cardno, @curdate, @type, @amount, @changeway, @billtype, @billid, @confirmflag return @__sp_re end GO /****** Object: StoredProcedure [dbo].[upg_add_money_full_ex] Script Date: 06/12/2017 06:19:24 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_add_money_full_ex] ( @compid varchar(10), -- 公司别 @cardno varchar(30), -- 卡号 @amount float, -- 金额 -- 已经扣除了欠款部分 @defict float, -- 欠款金额 @confirm int, -- 是否复核 1-存款复核 0-存款取消复核 3-取款取消复核 2-取款复核 @type int, -- 2:储值 1:电子钱包 @billtype varchar(10), @billid varchar(20) ) as--wuxm, 2004/08/14 begin declare @changeway int declare @curdate varchar(20) declare @totalamt float select @curdate = substring(convert(varchar(20), getdate(), 102), 1, 4) + substring(convert(varchar(20), getdate(), 102), 6, 2) + substring(convert(varchar(20), getdate(), 102), 9, 2) declare @confirmflag int if @confirm = 1 -- 存款复核 begin select @changeway = 0 -- 存款 select @confirmflag = 1 -- 复核 end else if @confirm = 0 -- 存款取消符合 begin select @changeway = 0 -- 存款 select @confirmflag = 0 -- 取消复核 end else if @confirm = 3 -- 取款,取消符合 begin select @changeway = 1 -- 取款 select @confirmflag = 0 -- 取消符合 end else if @confirm = 2 -- 取款,复核 begin select @changeway = 1 -- 取款 select @confirmflag = 1 -- 复核 end select @totalamt = @amount + isnull(@defict, 0) exec upg_purse_deduct @compid, @cardno, @curdate, @type, @totalamt, @changeway, @billtype, @billid, @confirmflag if (@defict > 0) begin if @confirm = 1 select @changeway = 5 -- 欠款 else select @changeway = 6 -- 还款 exec upg_purse_deduct @compid, @cardno, @curdate, @type, @defict, @changeway, @billtype, @billid, 1 end end GO /****** Object: StoredProcedure [dbo].[upg_add_money] Script Date: 06/12/2017 06:19:23 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_add_money] ( @compid varchar(10), -- 公司别 @cardno varchar(30), -- 卡号 @amount float, -- 金额 @confirm int, -- 是否复核 1-存款复核 0-存款取消复核 3-取款取消复核 2-取款复核 @type int -- 账户 2:储值 1:电子钱包 ) as--wuxm, 2004/08/14 begin declare @changeway int declare @curdate varchar(20) declare @confirmflag int select @curdate = substring(convert(varchar(20), getdate(), 102), 1, 4) + substring(convert(varchar(20), getdate(), 102), 6, 2) + substring(convert(varchar(20), getdate(), 102), 9, 2) if @confirm = 1 -- 存款复核 begin select @changeway = 0 -- 存款 select @confirmflag = 1 -- 复核 end else if @confirm = 0 -- 存款取消符合 begin select @changeway = 0 -- 存款 select @confirmflag = 0 -- 取消复核 end else if @confirm = 3 -- 取款,取消符合 begin select @changeway = 1 -- 取款 select @confirmflag = 0 -- 取消符合 end else if @confirm = 2 -- 取款,复核 begin select @changeway = 1 -- 取款 select @confirmflag = 1 -- 复核 end exec upg_purse_deduct @compid, @cardno, @curdate, @type, @amount, @changeway, '', '', @confirmflag end GO /****** Object: StoredProcedure [dbo].[upg_compute_cost_perf_outbill] Script Date: 06/12/2017 06:19:43 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_compute_cost_perf_outbill] ( @compid varchar(10), -- 公司别 @billid varchar(20) -- 出店单号 ) as begin declare @cashid varchar(20) -- 收银单号 declare cur_each_bill cursor for select glb03c from glm02 where glb00c = @compid and glb01c = @billid open cur_each_bill fetch cur_each_bill into @cashid while @@fetch_status = 0 begin exec upg_compute_cost_perf @compid, @cashid fetch cur_each_bill into @cashid end close cur_each_bill deallocate cur_each_bill end GO /****** Object: StoredProcedure [dbo].[upg_send_promotion] Script Date: 06/12/2017 06:20:40 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_send_promotion]( @compid varchar(10), --公司别 @billtype varchar(5), --单据类别 @billid varchar(20), --单据编号 @curdate varchar(10)) as begin declare @cardid varchar(20) declare @acctid varchar(10) declare @amt float declare @acctid_int int declare cur_each_send_gcm03 cursor for select a.gdj03c, b.gdk04c, b.gdk05f from gdm10 a, gdm11 b where a.gdj00c = b.gdk00c and a.gdj01c = b.gdk01c and a.gdj02c = b.gdk02c and a.gdj04i = b.gdk03i and a.gdj00c = @compid and a.gdj01c = @billtype and a.gdj02c = @billid and isnull(a.gdj05i, 0) = 0 open cur_each_send_gcm03 fetch cur_each_send_gcm03 into @cardid, @acctid, @amt while @@fetch_status = 0 begin select @acctid_int = convert(int, @acctid) exec upg_add_money_new @compid, @cardid, @amt, 1, @acctid_int, @billtype, @billid, @curdate fetch cur_each_send_gcm03 into @cardid, @acctid, @amt end close cur_each_send_gcm03 deallocate cur_each_send_gcm03 --更新赠送标志为已经赠送 update gdm10 set gdj05i = 1 where gdj00c = @compid and gdj01c = @billtype and gdj02c = @billid end GO /****** Object: StoredProcedure [dbo].[upg_exchange_points_tocash] Script Date: 06/12/2017 06:20:02 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_exchange_points_tocash] ( @compid varchar(10), @points float, @cash float, @acctid int, @userid varchar(20), @curdate varchar(20), @curtime varchar(20), @affectedrow int output ) as begin insert sysoperationlog(userid, program, operation, operationdate, operationtime, origatedate, compid, keyvalue1, keyvalue2, keyvalue3, keyvalue4) values(@userid, 'GSB24I', 'M', @curdate, @curtime, @curdate, @compid, @points, @cash, 'points_tocash', @acctid) -- 选择出超过这个积分的所有卡账户,注意只需要处理本公司的卡就可以了 create table #tbl_card_points ( cardid varchar(20) not null, points float null, units int null, reducepoints float null, cash float null, primary key(cardid) ) insert #tbl_card_points(cardid, points) select gcc01c, gcc06f from gcm03 c with (nolock), gcm01 a with (nolock) where c.gcc00c = a.gca00c and c.gcc01c = a.gca01c and a.gca00c = a.gca13d and c.gcc03i = 3 -- 积分 and c.gcc06f >= @points and (a.gca08i = 4 or a.gca08i = 5) -- 折算多少个积分单位 update a set a.units = a.points/@points from #tbl_card_points a update a set a.reducepoints = a.units * @points, a.cash = a.units*@cash from #tbl_card_points a -- 完成计算工作,现在需要处理每个账户,因为需要纪录操作历史所以必须用SP declare @cardid varchar(20) declare @reducepoints float declare @addcash float declare @i int select @i = 0 declare cur_each_card cursor for select cardid, reducepoints, cash from #tbl_card_points open cur_each_card fetch cur_each_card into @cardid, @reducepoints, @addcash while @@fetch_status = 0 begin select @i = @i + 1 exec upg_add_money_new @compid, @cardid, @reducepoints, 0, 3, '', '换积分', @curdate exec upg_add_money_new @compid, @cardid, @addcash, 1, @acctid, '', '换积分', @curdate update a set a.gcc06f = a.gcc06f - @reducepoints from gcm03 a where a.gcc01c = @cardid and a.gcc03i = 3 fetch cur_each_card into @cardid, @reducepoints, @addcash end close cur_each_card deallocate cur_each_card drop table #tbl_card_points select @affectedrow = @i end GO /****** Object: StoredProcedure [dbo].[upg_ipos_create_card] Script Date: 06/12/2017 06:20:17 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_ipos_create_card] ( @compid varchar(10), -- 公司别 @cardtype varchar(20), -- 卡类别 @cardid varchar(20), -- 会员卡号 @billdate varchar(20) -- 开日的日期 ) as begin declare @num float declare @unit varchar(10) declare @entryfee float declare @maxperson int declare @sp025f int declare @type int declare @gak14i int declare @sendflag varchar(10) declare @sp112f float -- 是否自动发送消费短信 select @sp112f = isnull(gsa03f, 0) from gsm01 with(nolock) where gsa00c = @compid and gsa01c = 'SP112' if @sp112f = 1 or @sp112f = 2 or @sp112f = 3 select @sendflag = '1' else select @sendflag = '2' select @type = isnull(gak03i,1), @gak14i = isnull(gak14i, 2) from gam10 where gak00c = @compid and gak01c = @cardtype if not exists(select 1 from gcm01 with(nolock) where gca00c = @compid and gca01c = @cardid and gca08i > 1) begin -- 删除没有销售的卡 -- 删除了前面进入的卡的疗程,是在新建立会员卡基本资料的时候 delete gcm01 where gca00c = @compid and gca01c = @cardid and gca08i = 1 select @num = gak07f, @unit = gak08c, @entryfee = gak12f, @maxperson = gak04i from gam10 where gak00c = @compid and gak01c = @cardtype insert gcm01(gca13d, gca00c, gca01c,gca02c,gca03i,gca04c,gca05d,gca06d,gca07d,gca08i,gca09c,gca10f,gca12c,gca18c,gca14i,gca15i,gca23i,gca24f) values(@compid, @compid, @cardid,@cardtype,@type,'',@billdate,@billdate,'',4,@unit,@num,'','',0,0,@maxperson,@entryfee) -- 如果没有储值账户,则增加一个储值账户 if @gak14i <> 2 begin exec upg_add_money_new @compid, @cardid, 0, 1, 2, '', '开账', @billdate end end end GO /****** Object: StoredProcedure [dbo].[upg_update_point] Script Date: 06/12/2017 06:20:50 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_update_point] ( @compid varchar(10), -- 公司别 @billid varchar(20), -- 销售单号-- 会员卡销售的时候的单号 @modifyno varchar(20), -- 异动单号 @currdate varchar(8), -- 当前日期 @class int, -- 1:销售卡的时候 2:卡异动的时候 @check int -- 0:取消覆核 1:覆核 ) as begin declare @flag int -- 判断系统现在是否为, 是否自动建立客户及开卡 declare @cardclass varchar(5) -- 卡的种类 declare @cardno varchar(20) -- 卡号 declare @cardstdvalues float -- 卡的标准金额 declare @cardfactvalues float -- 卡的实际金额 declare @pointclass int -- 积分种类 declare @pointvalues float -- 积分值 declare @mode int -- 卡销售的时候 异动类别 只处理存款/取款时候的积分 0-存款送积分 1-取款扣积分 declare @updatepointvalues float -- 赠送多少积分 declare @acct_2_id int declare @acct_3_id int declare @acct_2_amt float declare @acct_3_amt float declare @is_buy_again varchar(10) declare @tmpamt float declare @is_send_points varchar(10) declare @trueamt float declare @pay_1 varchar(10) declare @pay_amt_1 float declare @pay_2 varchar(10) declare @pay_amt_2 float declare @pay_3 varchar(10) declare @pay_amt_3 float declare @pay_4 varchar(10) declare @pay_amt_4 float declare @owncompid varchar(10) ---会员卡归属公司别 if @class = 1--会员卡销售 begin declare @first_card varchar(20) select @first_card = gna13c from gnm01 where gna00c = @compid and gna01c = @billid declare find_gnm02 cursor for select gnb02c, gnb03c, isnull(gnb05f, 0), isnull(gnb08f, 0), gnb15c, gnb16f, gnb17c, gnb18f, gnb19c, gnb20f, gnb21c, gnb22f from gnm02 where gnb00c = @compid and gnb01c = @billid open find_gnm02 fetch find_gnm02 into @cardclass, @cardno, @cardstdvalues, @cardfactvalues, @pay_1, @pay_amt_1, @pay_2, @pay_amt_2, @pay_3, @pay_amt_3, @pay_4, @pay_amt_4 while(@@fetch_status = 0) begin select @pointclass = isnull(gak30i, 1), @pointvalues = isnull(gak31i, 0), @acct_2_id = gak35i, @acct_2_amt = gak36f, @acct_3_id = gak37i, @acct_3_amt = gak38f from gam10 where gak00c = @compid and gak01c = @cardclass if (@pointclass = 1) set @updatepointvalues = @pointvalues else if (@pointclass = 2) set @updatepointvalues = @pointvalues * @cardfactvalues / 100.0 else if (@pointclass = 3) set @updatepointvalues = @pointvalues * @cardstdvalues / 100.0 else if (@pointclass = 4) begin if @cardno = @first_card begin exec upg_cal_full_true_card_sale_amt @compid, @billid, @cardno, @trueamt output end else begin exec upg_cal_true_card_sale_amt @compid, @pay_1, @pay_amt_1, @pay_2, @pay_amt_2, @pay_3, @pay_amt_3, @pay_4, @pay_amt_4, @trueamt output end set @updatepointvalues = @pointvalues * @trueamt / 100.0 end if isnull(@acct_2_id, 0) > 0 and isnull(@acct_2_amt, 0) > 0 begin if @acct_2_id = 3 begin set @updatepointvalues = @updatepointvalues + isnull(@acct_2_amt, 0) end else begin exec upg_add_money_new @compid, @cardno, @acct_2_amt, @check, @acct_2_id, 'gn', @billid, @currdate end if exists(select 1 from gnm01_SendAcct where gns00c = @compid and gns01c = @billid and gns02c = 'gn' and gns03c = @acct_2_id) begin delete gnm01_SendAcct where gns00c = @compid and gns01c = @billid and gns02c = 'gn' and gns03c = @acct_2_id end insert into gnm01_SendAcct(gns00c, gns01c, gns02c, gns03c, gns04f) values(@compid, @billid, 'gn', @acct_2_id, @acct_2_amt) end if isnull(@acct_3_id, 0) > 0 and isnull(@acct_3_amt, 0) > 0 begin if @acct_3_id = 3 begin set @updatepointvalues = @updatepointvalues + isnull(@acct_3_amt, 0) end else begin exec upg_add_money_new @compid, @cardno, @acct_3_amt, @check, @acct_3_id, 'gn', @billid, @currdate end if exists(select 1 from gnm01_SendAcct where gns00c = @compid and gns01c = @billid and gns02c = 'gn' and gns03c = @acct_3_id) begin delete gnm01_SendAcct where gns00c = @compid and gns01c = @billid and gns02c = 'gn' and gns03c = @acct_3_id end insert into gnm01_SendAcct(gns00c, gns01c, gns02c, gns03c, gns04f) values(@compid, @billid, 'gn', @acct_3_id, @acct_3_amt) end if @updatepointvalues > 0 exec upg_add_money_new @compid, @cardno, @updatepointvalues, @check, 3, 'gn', @billid, @currdate fetch find_gnm02 into @cardclass, @cardno, @cardstdvalues, @cardfactvalues, @pay_1, @pay_amt_1, @pay_2, @pay_amt_2, @pay_3, @pay_amt_3, @pay_4, @pay_amt_4 end close find_gnm02 deallocate find_gnm02 end if @class = 2 --卡账户管理 begin select @cardfactvalues = isnull(gcl07f, 0), @cardclass = gca02c, @cardno = gcl03c, @mode = gcl06i, @is_buy_again = gcl05c, @is_send_points = gcl54c, @pay_1 = gcl67c, @pay_amt_1 = gcl68f, @pay_2 = gcl69c, @pay_amt_2 = gcl70f, @pay_3 = gcl71c, @pay_amt_3 = gcl72f, @pay_4 = gcl73c, @pay_amt_4 = gcl74f, @owncompid = gca13d from gcm10 a with (nolock), gcm01 b with (nolock) where b.gca01c = a.gcl03c and a.gcl00c = @compid and a.gcl01c = @modifyno select @pointclass = isnull(gak30i, 1), @pointvalues = isnull(gak31i, 0), @acct_2_id = gak35i, @acct_2_amt = gak36f, @acct_3_id = gak37i, @acct_3_amt = gak38f from gam10 where gak00c = @compid and gak01c = @cardclass if @pointclass = 1 set @updatepointvalues = @pointvalues else if @pointclass = 2 or @pointclass = 3 begin set @updatepointvalues = @pointvalues * @cardfactvalues / 100.0 end else if (@pointclass = 4) begin exec upg_cal_full_true_card_charge_amt @compid, @modifyno, @trueamt output set @updatepointvalues = @pointvalues * @trueamt / 100.0 end if @updatepointvalues > 0 and @is_send_points = '1' begin if @mode = 0--存款的时候 begin exec upg_add_money_new @compid, @cardno, @updatepointvalues, @check, 3, 'gz', @modifyno, @currdate end else if @mode = 1 --取款的时候 begin select @tmpamt = -1 * @updatepointvalues exec upg_add_money_new @compid, @cardno, @tmpamt, @check, 3, 'gz', @modifyno, @currdate end end if @is_buy_again = '1' and @mode = 0 begin if isnull(@acct_2_id, 0) > 0 and isnull(@acct_2_amt, 0) > 0 begin exec upg_add_money_new @compid, @cardno, @acct_2_amt, @check, @acct_2_id, 'gz', @modifyno, @currdate end if isnull(@acct_3_id, 0) > 0 and isnull(@acct_3_amt, 0) > 0 begin exec upg_add_money_new @compid, @cardno, @acct_3_amt, @check, @acct_3_id, 'gz', @modifyno, @currdate end end else if @is_buy_again = '1' and @mode = 1 begin if isnull(@acct_2_id, 0) > 0 and isnull(@acct_2_amt, 0) > 0 begin select @tmpamt = 0 - @acct_2_amt exec upg_add_money_new @compid, @cardno, @tmpamt, @check, @acct_2_id, 'gz', @modifyno, @currdate end if isnull(@acct_3_id, 0) > 0 and isnull(@acct_3_amt, 0) > 0 begin select @tmpamt = 0 - @acct_3_amt exec upg_add_money_new @compid, @cardno, @tmpamt, @check, @acct_3_id, 'gz', @modifyno, @currdate end end end end GO /****** Object: StoredProcedure [dbo].[upg_exchange_points_tocash_card] Script Date: 06/12/2017 06:20:02 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_exchange_points_tocash_card] ( @compid varchar(10), @cardid varchar(20), @points float, @cash float, @acctid int, @userid varchar(20), @curdate varchar(20), @curtime varchar(20), @affectedrow int output ) as begin insert sysoperationlog(userid, program, operation, operationdate, operationtime, origatedate, compid, keyvalue1, keyvalue2, keyvalue3, keyvalue4) values(@userid, 'GSB01I', 'M', @curdate, @curtime, @curdate, @compid, @cardid, @points, @cash, @acctid) -- 选择出超过这个积分的所有卡账户,注意只需要处理本公司的卡就可以了 create table #tbl_card_points ( cardid varchar(20) not null, points float null, units int null, reducepoints float null, cash float null, primary key(cardid) ) insert #tbl_card_points(cardid, points) select gcc01c, gcc06f from gcm03 c with (nolock), gcm01 a with (nolock) where c.gcc00c = a.gca00c and c.gcc01c = a.gca01c and a.gca00c = a.gca13d and c.gcc03i = 3 -- 积分 and c.gcc06f >= @points and (a.gca08i = 4 or a.gca08i = 5) and a.gca01c = @cardid -- 折算多少个积分单位 update a set a.units = a.points / @points from #tbl_card_points a update a set a.reducepoints = a.units * @points, a.cash = a.units * @cash from #tbl_card_points a -- 完成计算工作,现在需要处理每个账户,因为需要纪录操作历史所以必须用SP declare @reducepoints float declare @addcash float declare @i int select @i = 0 declare cur_each_card cursor for select reducepoints, cash from #tbl_card_points open cur_each_card fetch cur_each_card into @reducepoints, @addcash while @@fetch_status = 0 begin select @i = @i + 1 exec upg_add_money_new @compid, @cardid, @reducepoints, 2, 3, '', '换积分', @curdate exec upg_add_money_new @compid, @cardid, @addcash, 1, @acctid, '', '换积分', @curdate fetch cur_each_card into @reducepoints, @addcash end close cur_each_card deallocate cur_each_card drop table #tbl_card_points select @affectedrow = @i end GO /****** Object: StoredProcedure [dbo].[upg_init_customers] Script Date: 06/12/2017 06:20:13 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_init_customers] ( @compid varchar(10), -- 公司别 @ddate varchar(20), -- 日期 @cardno varchar(20), -- 卡号 @cardtype varchar(5), -- 卡类别 @cust_name varchar(20), -- 姓名 @mobile varchar(20), -- 手机号码 @birth varchar(10), -- 生日 @gender tinyint, -- 性别 @telno varchar(20), -- 电话 @addr varchar(160), -- 地址 @remain float, -- 储值账户余额 @acct_1_mem varchar(60), -- 储值账户的备注 @fromdate varchar(8), -- 开卡日期 @todate varchar(8), -- 有效日期 @usedtimes float, -- 已经使用次数 @remaintimes float, -- 总次数 @memid varchar(20), -- 会员编号 @points float, -- 积分余额 @acct_p_mem varchar(60), -- 积分备注 @proc_remain float, -- 帐户4余额 @acct_2_mem varchar(60), -- 备注 @remain_3 float, -- 帐户5余额 @acct_3_mem varchar(60), -- 备注 @remain_4 float, -- 帐户6余额 @acct_4_mem varchar(60), -- 备注 @remain_5 float, -- 帐户7余额 @acct_5_mem varchar(60), -- 备注 @remain_6 float, -- 帐户7余额 @acct_6_mem varchar(60), -- 备注 @remain_7 float, -- 帐户8余额 @acct_7_mem varchar(60), -- 备注 @remain_8 float, -- 帐户9余额 @acct_8_mem varchar(60), -- 备注 @psid varchar(30), -- 身份证号码 Add by maql 20100527 @cardmemo varchar(1000) -- 会员卡备注 ) as begin declare @num float declare @unit varchar(10) declare @entryfee float declare @maxperson int declare @sp025f int declare @type int declare @gak14i int declare @sendflag varchar(10) declare @sp112f float select @remain = isnull(@remain, 0) if (isnull(@memid, '') = '') begin select @memid = @cardno end select @sp112f = isnull(gsa03f, 0) from gsm01 with (nolock) where gsa00c = @compid and gsa01c = 'SP112' if @sp112f = 1 or @sp112f = 2 or @sp112f = 3 select @sendflag = '1' else select @sendflag = '2' select @type = isnull(gak03i, 1), @gak14i = isnull(gak14i, 2) from gam10 where gak00c = @compid and gak01c = @cardtype if not exists(select 1 from gcm01 with (nolock) where gca01c = @cardno and gca08i > 1) --gca00c = @compid and begin -- 删除没有销售的卡 -- 删除了前面进入的卡的疗程,是在新建立会员卡基本资料的时候 delete gcm01 where gca01c = @cardno and gca08i = 1 --gca00c = @compid and select @num = gak07f, @unit = gak08c, @entryfee = gak12f, @maxperson = gak04i from gam10 where gak00c = @compid and gak01c = @cardtype insert gcm01(gca13d, gca00c, gca01c, gca02c, gca03i, gca04c, gca05d, gca06d, gca07d, gca08i, gca09c, gca10f, gca12c, gca18c, gca14i, gca15i, gca23i, gca24f, gca27c) values(@compid, @compid, @cardno, @cardtype, @type, @memid, @fromdate, @fromdate, @todate, 4, @unit, @num, '', '', @remaintimes, @usedtimes, @maxperson, @entryfee, @cardmemo) delete gcm05 where gce00c = @compid and gce01c = @cardno and gce02c = @memid insert gcm05(gce00c, gce01c, gce02c, gce03i) values(@compid, @cardno, @memid, 1) -- 增加开帐的异动历史 insert gcm02(gcb00c, gcb01c, gcb02f, gcb03i, gcb06i, gcb07d) values(@compid, @cardno, 1, 0, 4, @ddate) -- 如果没有储值账户,则增加一个储值账户 if @gak14i <> 2 begin exec upg_add_money_new @compid, @cardno, 0, 1, 2, '', '开账', @ddate end exec upg_add_money_new @compid, @cardno, @remain, 1, @gak14i, '', '开账', @ddate if isnull(@points, 0) > 0 begin exec upg_add_money_new @compid, @cardno, @points, 1, 3, '', '开账', @ddate end if isnull(@proc_remain, 0) >= 0 begin exec upg_add_money_new @compid, @cardno, @proc_remain, 1, 4, '', '开账', @ddate end if isnull(@remain_3, 0) > 0 begin exec upg_add_money_new @compid, @cardno, @remain_3, 1, 5, '', '开账', @ddate end if isnull(@remain_4, 0) > 0 begin exec upg_add_money_new @compid, @cardno, @remain_4, 1, 6, '', '开账', @ddate end if isnull(@remain_5, 0) > 0 begin exec upg_add_money_new @compid, @cardno, @remain_5, 1, 7, '', '开账', @ddate end if isnull(@remain_6, 0) > 0 begin exec upg_add_money_new @compid, @cardno, @remain_6, 1, 8, '', '开账', @ddate end if isnull(@remain_7, 0) > 0 begin exec upg_add_money_new @compid, @cardno, @remain_7, 1, 9, '', '开账', @ddate end if isnull(@remain_8, 0) > 0 begin exec upg_add_money_new @compid, @cardno, @remain_8, 1, 10, '', '开账', @ddate end if (isnull(@acct_1_mem, '') <> '') begin update gcm03 set gcc11c = @acct_1_mem where gcc00c = @compid and gcc01c = @cardno and gcc03i = 2 end if (isnull(@acct_p_mem, '') <> '') begin update gcm03 set gcc11c = @acct_p_mem where gcc00c = @compid and gcc01c = @cardno and gcc03i = 3 end if (isnull(@acct_2_mem, '') <> '') begin update gcm03 set gcc11c = @acct_2_mem where gcc00c = @compid and gcc01c = @cardno and gcc03i = 4 end if (isnull(@acct_3_mem, '') <> '') begin update gcm03 set gcc11c = @acct_3_mem where gcc00c = @compid and gcc01c = @cardno and gcc03i = 5 end if (isnull(@acct_4_mem, '') <> '') begin update gcm03 set gcc11c = @acct_4_mem where gcc00c = @compid and gcc01c = @cardno and gcc03i = 6 end if (isnull(@acct_5_mem, '') <> '') begin update gcm03 set gcc11c = @acct_5_mem where gcc00c = @compid and gcc01c = @cardno and gcc03i = 7 end if (isnull(@acct_6_mem, '') <> '') begin update gcm03 set gcc11c = @acct_6_mem where gcc00c = @compid and gcc01c = @cardno and gcc03i = 8 end if (isnull(@acct_7_mem, '') <> '') begin update gcm03 set gcc11c = @acct_7_mem where gcc00c = @compid and gcc01c = @cardno and gcc03i = 9 end if (isnull(@acct_8_mem, '') <> '') begin update gcm03 set gcc11c = @acct_8_mem where gcc00c = @compid and gcc01c = @cardno and gcc03i = 10 end end if not exists(select 1 from gbm01 where gba01c = @memid) --gba00c = @compid and begin insert gbm01(gba00c, gba01c, gba02c, gba03c, gba08c, gba17d, gba14i, gba07c, gba05c, gba23c, gba04c, gba15c, gba16c) values(@compid, @memid, @ddate, @cust_name, @mobile, @birth, @gender, @telno, @addr, @cardno, @sendflag, 1, @psid) end else begin update gbm01 set gba23c = @cardno where gba01c = @memid --gba00c = @compid and end end GO /****** Object: StoredProcedure [dbo].[upg_add_points_tomember] Script Date: 06/12/2017 06:19:24 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_add_points_tomember] ( @compid varchar(10), @cardtype varchar(20), @points float, @userid varchar(20), @curdate varchar(20), @curtime varchar(20), @affectedrow int output ) as begin insert sysoperationlog(userid, program, operation, operationdate, operationtime, origatedate, compid, keyvalue1, keyvalue2, keyvalue3, keyvalue4) values(@userid, 'GSB24I', 'M', @curdate, @curtime, @curdate, @compid, @cardtype, @points, 'add_points_tomember', '主动注入积分') -- 选择出超过这个积分的所有卡账户,注意只需要处理本公司的卡就可以了 create table #tbl_card_points ( compid varchar(10) not null, cardid varchar(20) not null, points float null, units int null, reducepoints float null, cash float null, primary key(cardid) ) insert #tbl_card_points(compid, cardid, points) select gca00c, gca01c, @points from gcm01 a with (nolock), gam26 z where gca00c = z.gaz02c and z.gaz01c = @compid and gca02c = @cardtype and gca00c = gca13d and (a.gca08i = 4 or a.gca08i = 5) -- 完成计算工作,现在需要处理每个账户,因为需要纪录操作历史所以必须用SP declare @cardid varchar(20) declare @addpoints float declare @each_compid varchar(10) declare @i int declare @ret int select @i = 0 declare cur_each_card cursor for select compid, cardid, points from #tbl_card_points open cur_each_card fetch cur_each_card into @each_compid, @cardid, @addpoints while @@fetch_status = 0 begin select @i = @i + 1 exec upg_add_money_new @each_compid, @cardid, @addpoints, 1, 3, '', '送积分', @curdate fetch cur_each_card into @each_compid, @cardid, @addpoints end close cur_each_card deallocate cur_each_card drop table #tbl_card_points select @affectedrow = @i end GO /****** Object: StoredProcedure [dbo].[upg_init_when_start_comp] Script Date: 06/12/2017 06:20:14 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_init_when_start_comp] @compid varchar(10), --公司别 @date varchar(8) --日期 as--WUXM 2004/12/05 begin -- 把批量删除的标志设置正确,可能批量删除的时候导致这个标志不正确,所以需要重新设置 update gsm01 set gsa02c = '' where gsa01c = 'SP001' and gsa00c = @compid declare @maxAQ int select @maxAQ = max(gsb02c) from gsm02 where gsb00c = @compid and gsb01c = 'AQ' set @maxAQ = isnull(@maxAQ, 4) -- 可能没有设置连锁结构设置,系统自动设置 if not exists(select 1 from b_perf) begin insert b_perf(mmonth, bran_id, parent_id, lvl) values('200801', '001', '', 1) insert gam26(gaz01c, gaz02c)---add by cyr 20121223 values('001', '001')---add by cyr 20121223 insert b_perf(mmonth, bran_id, parent_id, lvl) select '200801', gae01c, '001', @maxAQ from gam05 where gae01c <> '001' insert gam26(gaz01c, gaz02c)---add by cyr 20121223 select gae01c, gae01c from gam05 where gae01c not in(select gaz01c from gam26)---add by cyr 20121223 insert gam26(gaz01c, gaz02c)---add by cyr 20121223 select '001', gae01c from gam05 where gae01c not in(select gaz02c from gam26 where gaz01c = '001' )---add by cyr 20121223 end else begin -- 把所有没有加入组织结构树的门店加入 insert b_perf(mmonth, bran_id, parent_id, lvl) select '200801', gae01c, '001', @maxAQ from gam05 where gae01c not in (select bran_id from b_perf) insert gam26(gaz01c, gaz02c)---add by cyr 20121223 select gae01c, gae01c from gam05 where gae01c not in(select gaz01c from gam26)---add by cyr 20121223 insert gam26(gaz01c, gaz02c)---add by cyr 20121223 select '001', gae01c from gam05 where gae01c not in(select gaz02c from gam26 where gaz01c = '001' )---add by cyr 20121223 end -- 清理可能的gam06的垃圾 delete a from gam06 a, glm01 b where a.gaf00c = b.gla00c and a.gaf07c = b.gla01c and datediff(day, b.gla02d, @date) > 3 exec upg_check_card_validate @compid, @date -- 扣除本公司月短信通知费用 declare @need_sms_fee varchar(20) declare @sms_fee float declare @sms_acctid varchar(20) select @need_sms_fee = gsa02c from gsm01 with (nolock) where gsa00c = @compid and gsa01c = 'SP012' if @need_sms_fee = '1' begin -- 需要扣除短信月费用 -- 判断本月扣了吗 if not exists(select 1 from sms_fee_control where mmonth = substring(@date, 1, 6) and compid = @compid) begin select @sms_fee = convert(float, gsa02c) from gsm01 with (nolock) where gsa00c = @compid and gsa01c = 'SP022' select @sms_acctid = gsa02c from gsm01 with (nolock) where gsa00c = @compid and gsa01c = 'SP059' -- 说明没有扣过 exec upg_reduce_acctamt_by_sms @compid, @sms_fee end end -- 设置负债账户, 因为如果没有设置系统会出问题 -- exec upg_init_gsm14_debit_flag end GO /****** Object: StoredProcedure [dbo].[upg_cs_create_and_insert] Script Date: 06/12/2017 06:19:54 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_cs_create_and_insert] ( @comp varchar(10), @parent varchar(10), @level int, @stype int ) as begin begin transaction begin try if not exists(select 1 from gam05 where gae01c = @parent) return if exists(select 1 from gam05 where gae01c = @comp) return if isnull(@comp, '') = '' return exec upg_create_new_comp @comp insert into b_perf(bran_id, parent_id, lvl, stype) values(@comp, @parent, @level, (case when @stype > 0 then @stype else null end)) delete from gam26 where gaz02c = @comp insert gam26(gaz01c, gaz02c) values('001', @comp) if (@parent <> '001') insert gam26(gaz01c, gaz02c) values(@parent, @comp) exec upg_new_comp_copy_from_parent @comp, @parent end try begin catch if @@TRANCOUNT > 0 begin rollback transaction declare @SP_Parameters varchar(max) set @SP_Parameters = '@comp = ' + isnull(@comp, '') + '@parent = ' + isnull(@parent, '') + '@level = ' + convert(varchar(100), isnull(@level, -1)) + '@stype = ' + convert(varchar(100), isnull(@stype, 0)) insert into SP_TranLog (SPName ,SPTime ,SPParameter) values ( 'upg_cs_create_and_insert' ,getdate() ,@SP_Parameters) raiserror ('transaction is failed.', 16, 1) WITH NOWAIT end end catch if @@TRANCOUNT > 0 begin commit transaction end end GO /****** Object: StoredProcedure [dbo].[upg_delete_ggm01] Script Date: 06/12/2017 06:19:58 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_delete_ggm01] ( @compid varchar(10), -- 公司别 @billid varchar(20), -- 收银单号 @cardid varchar(20), -- 会员卡号 @enterno varchar(20), -- 入管单号 @billdate varchar(20), @send_point int -- 是否给积分 ) as begin begin transaction begin try --根据卡号获取归属公司: declare @ownercompid varchar(10) --卡的归属公司 select @ownercompid = min(gca13d) from gcm01 where gca01c = @cardid and gca00c = gca13d declare @incompid varchar(10) select @incompid = @compid declare @sp0403 float select @sp0403 = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP040' if isnull(@cardid, '') = '' or isnull(@billdate, '') = '' or isnull(@enterno, '') = '' begin select @cardid = gga09c, @billdate = gga02d, @enterno = gga30c from ggm01 with (nolock) where gga00c = @compid and gga01c = @billid end if isnull(@cardid, '') = '' set @cardid = '' if isnull(@ownercompid, '') = '' set @ownercompid = '' declare @delete_glm01 int if exists(select 1 from ggm01 with (nolock) where gga00c = @compid and gga01c <> @billid and gga30c = @enterno) select @delete_glm01 = 0 else select @delete_glm01 = 1 -- 取消掉已经生成的gcm20数据 exec upg_create_gcm20_from_one_bill @compid, 'gx', @billid, 0 --根据参数来调整库存 delete a from gfm03 a where gfc00c = @compid and (gfc01c = '3' or gfc01c = '4') and gfc02c = @billid delete a from gfm04 a where gfd00c = @compid and (gfd01c = '3' or gfd01c = '4') and gfd02c = @billid --如果是IC卡同步或异步, 调整卡的余额 --只有已经复核的单据才需要调整回去 if (@sp0403 = 0 or @sp0403 = 1) begin --调整卡余额 insert into T_ggm_delete_error([compid], [billid], [cardid], [errorcode], [step]) values (@compid, @billid, @cardid, @ownercompid, '调整卡余额') declare @gcc06f float declare @gsc05f float select @gcc06f = a.gcc06f, @gsc05f = b.gsc05f from gcm03 a with (nolock), gsm03 b with (nolock), gsm14 c where b.gsc00c = @compid and a.gcc01c = @cardid and a.gcc03i = c.gsn02c and c.gsn01c = b.gsc04c and a.gcc00c = @ownercompid and b.gsc01c = @billid and b.gsc02c = 'gx' insert into T_ggm_delete_error([compid], [billid], [cardid], [errorcode], [step]) values (@compid, @billid, @cardid, @ownercompid, '@gcc06f = ' + (case when @gcc06f is null then 'Is null' else convert(varchar(100), @gcc06f) end ) + '@gsc05f = ' + (case when @gsc05f is null then 'Is null' else convert(varchar(100), @gsc05f) end )) update a set a.gcc06f = a.gcc06f + isnull(b.gsc05f, 0) from gcm03 a, gsm03 b, gsm14 c where b.gsc00c = @compid and a.gcc01c = @cardid and a.gcc03i = c.gsn02c and c.gsn01c = b.gsc04c and a.gcc00c = @ownercompid and b.gsc01c = @billid and b.gsc02c = 'gx' if (@@error <> 0) begin insert into T_ggm_delete_error([compid], [billid], [cardid], [errorcode], [step]) values (@compid, @billid, @cardid, @@error, '调整卡余额失败') end if not exists ( select 1 from gcm03 a with (nolock), gsm03 b with (nolock), gsm14 c where b.gsc00c = @compid and a.gcc01c = @cardid and a.gcc03i = c.gsn02c and c.gsn01c = b.gsc04c and a.gcc00c = @ownercompid and b.gsc01c = @billid and b.gsc02c = 'gx') begin insert into T_ggm_delete_error([compid], [billid], [cardid], [errorcode], [step]) values (@compid, @billid, @cardid, @ownercompid, '调整余额失败!') end end -- 调整卡的积分 -- 先计算消费的金额 if @send_point = 1 begin exec upg_manage_points @compid, @cardid, 'gx', @billid, @billdate, 0, 0 end --可能此收银单有欠款发生,则需要将自动生成的欠款的账户异动单取消复核 --同时在取消复核的时候调整了卡的余额部分 declare @fill_amt float declare @fill_date varchar(12) declare @fill_billid varchar(20) declare @fill_accttype varchar(5) select @fill_amt = gcl07f, @fill_billid = gcl01c, @fill_date = gcl02d, @fill_accttype = gcl04c from gcm10 with (nolock) where gcl00c = @compid and gcl11c = 'gx' and gcl12c = @billid and gcl06i = 5 if isnull(@fill_date, '') <> '' begin exec upg_purse_deduct @compid, @cardid, @fill_date, @fill_accttype, @fill_amt, 5, 'gz', @fill_billid, 0 delete gcm10 where gcl00c = @compid and gcl11c = 'gx' and gcl12c = @billid and gcl06i = 5 end --调整卡的账户异动历史 delete gcm04 where gcd00c = @compid and gcd01c = @cardid and gcd07c = 'gx' and gcd08c = @billid -- 调整已经消费的疗程,应该扣回去 -- 套餐也应该扣回去 declare @procpay varchar(10) select @procpay = '9' --判断是否有疗程支付或套餐支付 declare @paid_proc int -- 1 有疗程支付 0 没有 declare @paid_combo int -- 1 有套餐支付 0 没有 if exists(select 1 from gsm03 with (nolock) where gsc00c = @compid and gsc01c = @billid and gsc02c = 'gx' and gsc04c = @procpay ) begin select @paid_proc = 1 end else select @paid_proc = 0 if exists(select 1 from gsm03 with (nolock) where gsc00c = @compid and gsc01c = @billid and gsc02c = 'gx' and gsc04c = '#' ) begin select @paid_combo = 1 end else select @paid_combo = 0 if @paid_proc = 1 or @paid_combo = 1 begin declare @ggm02_prj varchar(20) declare @ggm02_times float declare @ggm02_amt float declare @paymode varchar(10) declare @procidd int -- 疗程的流水号 declare @maincomboseq int -- 套餐主序号 declare @sp055f float select @sp055f = gsa03f from gsm01 where gsa00c = @incompid and gsa01c = 'SP055' if (@sp055f = 0) begin --按项目支付 declare cur_each_ggm02_1 cursor for select ggb03c, ggb05f, ggb11f, ggb27c, ggb47i, ggb48i from ggm02 a with (nolock) where a.ggb00c = @compid and a.ggb01c = @billid open cur_each_ggm02_1 fetch cur_each_ggm02_1 into @ggm02_prj, @ggm02_times, @ggm02_amt, @paymode, @procidd, @maincomboseq while @@fetch_status = 0 begin if @paymode = @procpay exec upg_used_cureproc_ex @compid, @cardid, @ggm02_prj, @ggm02_times, @ggm02_amt, 0, 'gx', @billid, @billdate, @procidd else if @paymode = '#' exec upg_used_combo @compid, @cardid, @ggm02_prj, @ggm02_times, 0, 'gx', @billid, @billdate, 1, @maincomboseq fetch cur_each_ggm02_1 into @ggm02_prj, @ggm02_times, @ggm02_amt, @paymode, @procidd, @maincomboseq end close cur_each_ggm02_1 deallocate cur_each_ggm02_1 -- 处理可能的套餐中的产品 -- 按项目支付的情况 if @paid_combo = 1 begin declare cur_each_ggm03_1 cursor for select ggc03c, ggc05f, ggc11f, ggc27c, ggc48i from ggm03 a with (nolock) where a.ggc00c = @compid and a.ggc01c = @billid and a.ggc27c = '#' open cur_each_ggm03_1 --借用原来的变量 fetch cur_each_ggm03_1 into @ggm02_prj, @ggm02_times, @ggm02_amt, @paymode, @maincomboseq while @@fetch_status = 0 begin exec upg_used_combo @compid, @cardid, @ggm02_prj, @ggm02_times, 0, 'gx', @billid, @billdate, 2, @maincomboseq fetch cur_each_ggm03_1 into @ggm02_prj, @ggm02_times, @ggm02_amt, @paymode, @maincomboseq end close cur_each_ggm03_1 deallocate cur_each_ggm03_1 end end else begin --按单据支付 --疗程扣回 if @paid_proc = 1 begin declare cur_each_ggm02 cursor for select ggb03c, ggb05f, ggb11f, ggb47i from ggm02 a with (nolock), gcm06 b with (nolock) where a.ggb00c = b.gcf00c and a.ggb03c = b.gcf02c and b.gcf01c = @cardid and a.ggb00c = @compid and a.ggb01c = @billid open cur_each_ggm02 fetch cur_each_ggm02 into @ggm02_prj, @ggm02_times, @ggm02_amt, @procidd while @@fetch_status = 0 begin exec upg_used_cureproc_ex @compid, @cardid, @ggm02_prj, @ggm02_times, @ggm02_amt, 0, 'gx', @billid, @billdate, @procidd fetch cur_each_ggm02 into @ggm02_prj, @ggm02_times, @ggm02_amt, @procidd end close cur_each_ggm02 deallocate cur_each_ggm02 end --套餐的项目扣回 if @paid_combo = 1 begin --套餐项目的回扣 declare cur_each_ggm02_2 cursor for select ggb03c, ggb05f, ggb11f, ggb48i from ggm02 a with (nolock), gtc04 b with (nolock) where a.ggb00c = b.gte00c and a.ggb03c = b.gte03c and b.gte01c = @cardid and a.ggb00c = @compid and a.ggb01c = @billid open cur_each_ggm02_2 fetch cur_each_ggm02_2 into @ggm02_prj, @ggm02_times, @ggm02_amt, @maincomboseq while @@fetch_status = 0 begin exec upg_used_combo @compid, @cardid, @ggm02_prj, @ggm02_times, 0, 'gx', @billid, @billdate, 1, @maincomboseq fetch cur_each_ggm02_2 into @ggm02_prj, @ggm02_times, @ggm02_amt, @maincomboseq end close cur_each_ggm02_2 deallocate cur_each_ggm02_2 --套餐产品的回扣 declare cur_each_ggm03_2 cursor for select ggc03c, ggc05f, ggc11f, ggc48i from ggm03 a with (nolock), gtc08 b with (nolock) where a.ggc00c = b.gtj00c and a.ggc03c = b.gtj03c and b.gtj01c = @cardid and a.ggc00c = @compid and a.ggc01c = @billid open cur_each_ggm03_2 fetch cur_each_ggm03_2 into @ggm02_prj, @ggm02_times, @ggm02_amt, @maincomboseq while @@fetch_status = 0 begin exec upg_used_combo @compid, @cardid, @ggm02_prj, @ggm02_times, 0, 'gx', @billid, @billdate, 2, @maincomboseq fetch cur_each_ggm03_2 into @ggm02_prj, @ggm02_times, @ggm02_amt, @maincomboseq end close cur_each_ggm03_2 deallocate cur_each_ggm03_2 end end end -- 删除收银单的时候,如果购买产品赠送了服务,则需要把免费赠送的服务次数也删除 -- 如果没有消费过,删除没有关系,如果消费过,则不能删除,只能不能再次赠送 exec upg_undo_send_gift_prjs @compid, @billid --可能存在的保证金转付需要删除 declare @prepay_amt float -- 保证金转付的金额 select @prepay_amt = gsc05f from gsm03 with (nolock) where gsc00c = @compid and gsc01c = @billid and gsc02c = 'gx' and gsc04c = '&' if isnull(@prepay_amt, 0) > 0 begin update a set gbl07f = case when isnull(gbl07f, 0) = 0 then 0 else gbl07f - isnull(gbw05f,0) end, gbl09c = '', gbl10c = '', gbl11f = isnull(gbl11f, 0) + isnull(gbw05f,0), gbl12i = 0, gbl18t = '' from gbm15 a,gbm17 b where a.gbl01c = b.gbw01c --会员编号 and a.gbl02f = b.gbw02f --序号 and a.gbl10c = b.gbw03c --单号 and a.gbl09c = b.gbw04c --单据类型 and a.gbl09c = 'gx' and a.gbl10c = @billid and b.gbw00c = @compid --清楚保证金消费明细表 delete gbm17 where gbw00c = @compid and gbw01c = @cardid and gbw04c = 'gx' and gbw03c = @billid end delete ggm01 where gga00c = @compid and gga01c = @billid --删除没有消费的入院单 if @delete_glm01 = 1 begin delete glm01 where gla00c = @compid and gla01c = @enterno end --删除gam06登记入院的客户 delete gam06 where gaf00c = @compid and gaf03c = @cardid and gaf07c = @enterno --add by lyj 2008-04-09 for 伊美娜 delete gcm22 where gcp00c = @compid and gcp01c = @billid end try begin catch if @@TRANCOUNT > 0 begin ROLLBACK transaction declare @SP_Parameters varchar(max) set @SP_Parameters = '@compid = ' + isnull(@compid, '') + '@billid = ' + isnull(@billid, '') + '@cardid = ' + isnull(@cardid, '') + '@enterno = ' + isnull(@enterno, '') + '@billdate = ' + isnull(@billdate, '') + '@send_point = ' + (case when @send_point is null then '' else convert(varchar(100), @send_point) end) insert into SP_TranLog (SPName , SPTime , SPParameter) values ( 'upg_delete_ggm01' , getdate() , @SP_Parameters) end end catch if @@TRANCOUNT > 0 begin commit transaction end end GO /****** Object: StoredProcedure [dbo].[upg_rewrite_verp_ex] Script Date: 06/12/2017 06:20:38 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_rewrite_verp_ex] @compid varchar(10), -- 公司编号 @billid varchar(20), -- 单号 @billtype varchar(5), -- 单据类别 'gn' = 会员卡销售 'gz' = 会员帐户管理 @iconfirm int as begin -- 自己输入业绩则不需要计算 declare @inputperf varchar(20) select @inputperf = gsa02c from gsm01 with (nolock) where gsa01c = 'SP048' and gsa00c = @compid if isnull(@inputperf, '') = '1' return if @iconfirm = 1 begin exec upg_cal_fill_bill_perf @compid, @billtype, @billid end else begin if @billtype = 'gn' begin update gnm01 set gna67f = 0, gna69f = 0, gna71f = 0, gna86f = 0, gna87f = 0, gna88f = 0, gna89f = 0, gna90f = 0, gna91f = 0, gna92f = 0 where gna00c = @compid and gna01c = @billid end else if @billtype = 'gz' begin update gcm10 set gcl58f = 0, gcl60f = 0, gcl62f = 0, gcl83f = 0, gcl84f = 0, gcl85f = 0, gcl86f = 0, gcl87f = 0, gcl88f = 0, gcl89f = 0 where gcl00c = @compid and gcl01c = @billid end end end GO /****** Object: StoredProcedure [dbo].[upg_confirm_cureproc] Script Date: 06/12/2017 06:19:44 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_confirm_cureproc] ( @compid varchar(10), @billtype varchar(10), @billid varchar(20), @confirmdate varchar(20), @cardid varchar(20), @confirmflag int ) as begin declare @proj varchar(30) declare @buytimes float declare @sendtimes float declare @amount float declare @start_date varchar(20) declare @end_date varchar(20) declare @priceflow float declare @discount float declare @cardnum float declare @issent int declare @isgift int declare @gcf24i int declare @gcf23i int declare @memo varchar(60) declare @usetimemonth float --一个月可使用的次数 0--代表不控制 直到这个疗程用完其他float就是一个月可以使用的次数 if @billtype = 'gz' begin declare cur_each_gcm11 cursor for select gcm02c, gcm03f, gcm04f, gcm05f, @confirmdate, gcm06d, gcm09i, gcm10f, gcm11f, 0, gcm12i, gcm13c, gcm25i, gcm14i, gcm08f from gcm11 where gcm00c = @compid and gcm01c = @billid open cur_each_gcm11 fetch cur_each_gcm11 into @proj, @buytimes, @sendtimes, @amount, @start_date, @end_date, @priceflow, @discount, @cardnum, @issent, @isgift, @memo, @gcf23i, @gcf24i, @usetimemonth while @@fetch_status = 0 begin exec upg_add_cureproc_ex @compid, @cardid, @proj, @buytimes, @sendtimes, @amount, @confirmflag, 1, @start_date, @end_date, @billtype, @billid, @confirmdate, @usetimemonth, @priceflow, @discount, @cardnum, @issent, @isgift, @memo, @gcf23i, @gcf24i fetch cur_each_gcm11 into @proj, @buytimes, @sendtimes, @amount, @start_date, @end_date, @priceflow, @discount, @cardnum, @issent, @isgift, @memo, @gcf23i, @gcf24i, @usetimemonth end close cur_each_gcm11 deallocate cur_each_gcm11 end else if @billtype = 'gn' begin declare cur_each_gnm03 cursor for select gnc02c, gnc03f, gnc04f, gnc05f, @confirmdate, gnc06d, gnc09i, gnc10f, gnc11f, 0, gnc12i, gnc13c, gnc25i, gnc29i, gnc08f from gnm03 where gnc00c = @compid and gnc01c = @billid open cur_each_gnm03 fetch cur_each_gnm03 into @proj, @buytimes, @sendtimes, @amount, @start_date, @end_date, @priceflow, @discount, @cardnum, @issent, @isgift, @memo, @gcf23i, @gcf24i, @usetimemonth while @@fetch_status = 0 begin -- 疗程归第一张卡 exec upg_add_cureproc_ex @compid, @cardid, @proj, @buytimes, @sendtimes, @amount, @confirmflag, 1, @start_date, @end_date, 'gn', @billid, @confirmdate, @usetimemonth, @priceflow, @discount, @cardnum, @issent, @isgift, @memo, @gcf23i, @gcf24i fetch cur_each_gnm03 into @proj, @buytimes, @sendtimes, @amount, @start_date, @end_date, @priceflow, @discount, @cardnum, @issent, @isgift, @memo, @gcf23i, @gcf24i, @usetimemonth end close cur_each_gnm03 deallocate cur_each_gnm03 end end GO /****** Object: StoredProcedure [dbo].[upg_trans_card_money] Script Date: 06/12/2017 06:20:47 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_trans_card_money] ( @compid varchar(10), -- 公司别 @fromcardno varchar(30), -- 原来卡号 @tocardno varchar(30), -- 目的卡号 @flag int, -- flag: 1:加钱 2:替换 @billtype varchar(10), @billid varchar(20) ) as begin declare @amt float declare @defict float -- 欠款金额 declare @type int declare @need float declare @memo varchar(512) declare @ownercompid nvarchar(10) --卡的归属公司 select @ownercompid = min(gca13d) from gcm01 where gca01c = @tocardno and gca00c = gca13d if @flag = 2 -- 替换 begin update gcm03 set gcc06f = 0, gcc10f = 0 where gcc00c = @ownercompid and gcc01c = @tocardno and gcc03i = 2 end select @need = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP017' if @need = 1 begin -- 储值帐户 select @type = 2 select @amt = isnull(gcc06f, 0), @defict = isnull(gcc10f, 0), @memo = gcc11c from gcm03 with (nolock) where gcc00c = @ownercompid and gcc01c = @fromcardno and gcc03i = @type select @amt = isnull(@amt, 0), @defict = isnull(@defict, 0) exec upg_add_money_expend @compid, @tocardno, @amt, @defict, 1, @type , @billtype, @billid -- 如果有备注需要把备注拷贝过去 if isnull(@memo, '') <> '' begin update a set a.gcc11c = @memo from gcm03 a where gcc00c = @ownercompid and gcc01c = @tocardno and gcc03i = @type end end update gcm03 set gcc06f = 0, gcc10f = 0 where gcc00c = @ownercompid and gcc01c = @fromcardno and gcc03i = 2 end GO /****** Object: StoredProcedure [dbo].[upg_trans_other_acct_remain] Script Date: 06/12/2017 06:20:47 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_trans_other_acct_remain] ( @compid varchar(10), @oldcardid varchar(20), @oldmode int, -- 0-不动 1-删除 @newcardid varchar(20), @newmode int, -- 0-保留 1-删除 @curdate varchar(10), @billtype varchar(10), @billid varchar(20) ) as begin declare @proc_acct int select @proc_acct = 4 -- 需要覆盖,则新卡需要先被删除 if @newmode = 1 begin delete gcm03 from gcm01 where gcc00c = gca00c and gca00c = gca13d and gca01c = gcc01c and gcc01c = @newcardid and gcc03i <> 2 and gcc03i <> @proc_acct end -- 老卡的都需要转移过去 if @oldmode = 1 and @newmode = 1 -- 老卡需要删除 begin -- 如果老的需要删除,则直接更新卡号就可以了 update gcm03 set gcc01c = @newcardid where gcc01c = @oldcardid and gcc03i <> 2 and gcc03i <> @proc_acct end else -- 老卡不需要删除,则复制过去 begin -- 需要把每个账户的钱给增加进去 declare @amt float declare @def_amt float declare @acctid int declare cur_each_acct cursor for select gcc03i, gcc06f, gcc10f from gcm03 with (nolock), gcm01 with (nolock) where gcc00c = gca00c and gca01c = gcc01c and gca00c = gca13d and gcc01c = @oldcardid and gcc03i <> 2 and gcc03i <> @proc_acct open cur_each_acct fetch cur_each_acct into @acctid, @amt, @def_amt while @@fetch_status = 0 begin exec upg_add_money_expend @compid, @newcardid, @amt, @def_amt, 1, @acctid, @billtype, @billid fetch cur_each_acct into @acctid, @amt, @def_amt end close cur_each_acct deallocate cur_each_acct end if @oldmode = 1 begin delete gcm03 where gcc01c = @oldcardid and gcc03i <> 2 and gcc03i <> @proc_acct end -- 记录操作历史 insert sysoperationlog(userid, program, operation, operationdate, operationtime, origatedate, compid, keyvalue1, keyvalue2, keyvalue3) values('system', 'GSB01I', 'M', @curdate, '', @curdate, @compid, @oldcardid, @newcardid, 'upg_trans_other_acct_remain') end GO /****** Object: StoredProcedure [dbo].[upg_3g_event] Script Date: 06/12/2017 06:19:22 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_3g_event] ( @compid varchar(10), -- 公司别 @cardno varchar(30), -- 卡号 @param1 varchar(30), -- 未用 - 介绍人卡号(申请电子会员卡) @param2 varchar(30), -- 未用 @param3 varchar(30), -- 未用 @param4 varchar(30), -- 未用 @param5 varchar(30), -- 未用 @eventtype int -- 事件类型 1-客户端分享给积分 2-签到给积分 3-申请电子卡填写介绍人送积分,介绍客户申请电子卡送积分 ) as -- 移动终端事件处理 begin declare @amount float declare @billtype varchar(20) declare @billid varchar(20) declare @defict float select @defict = 0 declare @confirm int select @confirm = 1 declare @type int select @type = 3 if @eventtype=9 -- 预约给积分,现在暂时在预约的时候直接给积分,以后需要在来店消费的时候给积分 begin select @billid = 'LIANGLI' select @billtype = '3G' select @amount = gsa03f from gsm01 WITH (NOLOCK) where gsa00c = @compid and gsa01c = 'SPT10' -- 预约设置的积分 if @amount > 0 begin exec upg_add_money_expend @compid, @cardno, @amount, @defict, @confirm, @type, @billtype, @billid end end else if @eventtype = 1 --客户端分享给积分 begin select @billtype = 'SHARE' select @billid = '分享得积分' select @amount = gsa03f from gsm01 with (nolock) where gsa00c = @compid and gsa01c = 'SPT01' if @amount > 0 exec upg_add_money_expend @compid, @cardno, @amount, @defict, @confirm, @type, @billtype, @billid end else if @eventtype = 2 --客户端签到给积分 begin select @billtype = 'SIGN' select @billid = '签到得积分' select @amount = gsa03f from gsm01 with (nolock) where gsa00c = @compid and gsa01c = 'SPT03' if @amount > 0 exec upg_add_money_expend @compid, @cardno, @amount, @defict, @confirm, @type, @billtype, @billid end else if @eventtype = 3 --申请电子卡填写介绍人送积分,介绍客户申请电子卡送积分 begin select @billtype = 'FILL' select @billid = '填写介绍人得积分' --申请电子卡填写介绍人送积分 select @amount = gsa03f from gsm01 with (nolock) where gsa00c = @compid and gsa01c = 'SPT08' if @amount > 0 exec upg_add_money_expend @compid, @cardno, @amount, @defict, @confirm, @type, @billtype, @billid select @billtype = 'RECOM' select @billid = '推荐电子卡得积分' --介绍客户申请电子卡送积分 select @amount = gsa03f from gsm01 with (nolock) where gsa00c = @compid and gsa01c = 'SPT09' if @amount > 0 and isnull(@param1, '') <> '' exec upg_add_money_expend @compid, @param1, @amount, @defict, @confirm, @type, @billtype, @billid -- 更新介绍人 update gbm01 set gba90c = @param1 where gba01c = @cardno end end GO /****** Object: StoredProcedure [dbo].[upg_m_dispatch_table] Script Date: 06/12/2017 06:20:22 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_m_dispatch_table] ( @fromcompid varchar(10), -- 发布数据的公司 @tblname varchar(20) -- 要发布的table name ) as begin exec upg_dispatch_data @fromcompid, @tblname end GO /****** Object: StoredProcedure [dbo].[upg_report_gwm03r_lm1] Script Date: 06/12/2017 06:20:36 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO Create proc [dbo].[upg_report_gwm03r_lm1] ( @companyid varchar(10), --门店 @fromdate varchar(10), --起始日期 @todate varchar(10), --结束日期 @fromyg varchar(30), --起始员工 @toyg varchar(30), --结束员工 @rad int, --选择: -1 全部 0 卡销售 1 卡充值 2 项目现金消费 3 外卖品 @chk int --展示明细: -1 不展示 0 展示 ) As begin declare @spid int select @spid = @@spid delete from work_detail where spid = @spid exec upg_personal_statistics_noreturn @companyid,@fromdate,@todate,@fromyg,@toyg,1,1,1,1,@spid if( @chk = -1 ) begin if( @rad = -1) --全部 begin select isnull(compid,'') compid, isnull(person_id,'') person_id, -- 员工代码 personName = isnull((select haa02c from ham01 where haa00c = compid and haa01c = person_id),''), isnull(sum(amt),0) amt, -- 营业金额 isnull(sum(comm),0) comm, --提成 isnull(sum(amt2),0) amt2, --虚业绩 isnull(sum(amt3),0) amt3, --真实业绩 isnull((case gender when 0 then '女' when 1 then '男' end),'') sex from work_detail with(nolock) where spid = @spid and (amt2 is not null or amt3 is not null) and payway in ( select gsb03c from gsm02 where gsb01c = '3' and gsb00c = compid and gsb02c in ('1','6','WX','ZFB') ) and (compid = @companyid or @companyid = '') and srvdate between @fromdate and @todate and (@fromyg = '' or ( person_id >= @fromyg and person_id <= @toyg )) group by compid,person_id,gender order by person_id end else if( @rad = 0) --卡销售 begin select isnull(compid,'') compid, isnull(person_id,'') person_id, -- 员工代码 personName = isnull((select haa02c from ham01 where haa00c = compid and haa01c = person_id),''), isnull(sum(amt),0) amt, -- 营业金额 isnull(sum(comm),0) comm, --提成 isnull(sum(amt2),0) amt2, --虚业绩 isnull(sum(amt3),0) amt3, --真实业绩 isnull((case gender when 0 then '女' when 1 then '男' end),'') sex from work_detail with(nolock) where action_id = 1 and spid = @spid and (amt2 is not null or amt3 is not null) and payway in ( select gsb03c from gsm02 where gsb01c = '3' and gsb00c = compid and gsb02c in ('1','6','WX','ZFB') ) and (compid = @companyid or @companyid = '') and srvdate between @fromdate and @todate and (@fromyg = '' or ( person_id >= @fromyg and person_id <= @toyg )) group by compid,person_id,gender order by person_id end else if( @rad = 1) --卡充值 begin select isnull(compid,'') compid, isnull(person_id,'') person_id, -- 员工代码 personName = isnull((select haa02c from ham01 where haa00c = compid and haa01c = person_id),''), isnull(sum(amt),0) amt, -- 营业金额 isnull(sum(comm),0) comm, --提成 isnull(sum(amt2),0) amt2, --虚业绩 isnull(sum(amt3),0) amt3, --真实业绩 isnull((case gender when 0 then '女' when 1 then '男' end),'') sex from work_detail with(nolock) where action_id = 0 --卡充值 and spid = @spid and (amt2 is not null or amt3 is not null) and payway in ( select gsb03c from gsm02 where gsb01c = '3' and gsb00c = compid and gsb02c in ('1','6','WX','ZFB') ) and (compid = @companyid or @companyid = '') and srvdate between @fromdate and @todate and (@fromyg = '' or ( person_id >= @fromyg and person_id <= @toyg )) group by compid,person_id,gender order by person_id end else if( @rad = 2) --项目现金消费 begin select isnull(compid,'') compid, isnull(person_id,'') person_id, -- 员工代码 personName = isnull((select haa02c from ham01 where haa00c = compid and haa01c = person_id),''), isnull(sum(amt),0) amt, -- 营业金额 isnull(sum(comm),0) comm, --提成 isnull(sum(amt2),0) amt2, --虚业绩 isnull(sum(amt3),0) amt3, --真实业绩 isnull((case gender when 0 then '女' when 1 then '男' end),'') sex from work_detail with(nolock) where cls_flag = 1 and spid = @spid and (amt2 is not null or amt3 is not null) and payway in ( select gsb03c from gsm02 where gsb01c = '3' and gsb00c = compid and gsb02c in ('1','6','WX','ZFB') ) and (compid = @companyid or @companyid = '') and srvdate between @fromdate and @todate and (@fromyg = '' or ( person_id >= @fromyg and person_id <= @toyg )) group by compid,person_id,gender order by person_id end else if( @rad = 3) --外卖品 begin select isnull(compid,'') compid, isnull(person_id,'') person_id, -- 员工代码 personName = isnull((select haa02c from ham01 where haa00c = compid and haa01c = person_id),''), isnull(sum(amt),0) amt, -- 营业金额 isnull(sum(comm),0) comm, --提成 isnull(sum(amt2),0) amt2, --虚业绩 isnull(sum(amt3),0) amt3, --真实业绩 isnull((case gender when 0 then '女' when 1 then '男' end),'') sex from work_detail with(nolock) where cls_flag = 2 and spid = @spid and (amt2 is not null or amt3 is not null) and payway in ( select gsb03c from gsm02 where gsb01c = '3' and gsb00c = compid and gsb02c in ('1','6','WX','ZFB') ) and (compid = @companyid or @companyid = '') and srvdate between @fromdate and @todate and (@fromyg = '' or ( person_id >= @fromyg and person_id <= @toyg )) group by compid,person_id,gender order by person_id end end else --@chk = 0 begin if( @rad = -1 ) -- 全部 begin select isnull(compid,'') compid, isnull(person_id,'') person_id, -- 员工代码 personName = isnull((select haa02c from ham01 where haa00c = compid and haa01c = person_id),''), isnull(amt,0) amt, -- 营业金额 isnull(comm,0) comm, --提成 isnull(amt2,0) amt2, --虚业绩 isnull(amt3,0) amt3, --真实业绩 isnull((case gender when 0 then '女' when 1 then '男' end),'') sex, isnull(billid,'') billid, -- 单号 isnull(code,'') code, -- 项目代码,或是卡号,产品码 isnull(name,'') name, -- 名称 isnull(quan,0) quan, -- 数量 isnull(memid,'') memid, -- 会员编号 isnull(gba03c,'') gba03c, -- 会员姓名 isnull(gba05c,'') gba05c, -- 联络地址 isnull(gba08c,'') gba08c, -- Mobile No. isnull(gba11c,'') gba11c, -- E-Mail地址 isnull(gba17d,'') gba17d, -- 出生日期 isnull(gba21c,'') gba21c, -- 农历生日 isnull(srvdate,'') srvdate -- 销售日期 from work_detail with(nolock) left join gbm01 on gba00c = compid and gba01c = memid where (amt2 is not null or amt3 is not null) and spid = @spid and payway in ( select gsb03c from gsm02 where gsb01c = '3' and gsb00c = compid and gsb02c in ('1','6','WX','ZFB') ) and (compid = @companyid or @companyid = '') and srvdate between @fromdate and @todate and (@fromyg = '' or ( person_id >= @fromyg and person_id <= @toyg )) order by srvdate end else if( @rad = 0 ) -- 卡销售 begin select isnull(compid,'') compid, isnull(person_id,'') person_id, -- 员工代码 personName = isnull((select haa02c from ham01 where haa00c = compid and haa01c = person_id),''), isnull(amt,0) amt, -- 营业金额 isnull(comm,0) comm, --提成 isnull(amt2,0) amt2, --虚业绩 isnull(amt3,0) amt3, --真实业绩 isnull((case gender when 0 then '女' when 1 then '男' end),'') sex, isnull(billid,'') billid, -- 单号 isnull(code,'') code, -- 项目代码,或是卡号,产品码 isnull(name,'') name, -- 名称 isnull(quan,0) quan, -- 数量 isnull(memid,'') memid, -- 会员编号 isnull(gba03c,'') gba03c, -- 会员姓名 isnull(gba05c,'') gba05c, -- 联络地址 isnull(gba08c,'') gba08c, -- Mobile No. isnull(gba11c,'') gba11c, -- E-Mail地址 isnull(gba17d,'') gba17d, -- 出生日期 isnull(gba21c,'') gba21c, -- 农历生日 isnull(srvdate,'') srvdate -- 销售日期 from work_detail with(nolock) left join gbm01 on gba00c = compid and gba01c = memid where action_id = 1 and spid = @spid and (amt2 is not null or amt3 is not null) and payway in ( select gsb03c from gsm02 where gsb01c = '3' and gsb00c = compid and gsb02c in ('1','6','WX','ZFB') ) and (compid = @companyid or @companyid = '') and srvdate between @fromdate and @todate and (@fromyg = '' or ( person_id >= @fromyg and person_id <= @toyg )) order by srvdate end else if( @rad = 1 ) -- 卡充值 begin select isnull(compid,'') compid, isnull(person_id,'') person_id, -- 员工代码 personName = isnull((select haa02c from ham01 where haa00c = compid and haa01c = person_id),''), isnull(amt,0) amt, -- 营业金额 isnull(comm,0) comm, --提成 isnull(amt2,0) amt2, --虚业绩 isnull(amt3,0) amt3, --真实业绩 isnull((case gender when 0 then '女' when 1 then '男' end),'') sex, isnull(billid,'') billid, -- 单号 isnull(code,'') code, -- 项目代码,或是卡号,产品码 isnull(name,'') name, -- 名称 isnull(quan,0) quan, -- 数量 isnull(memid,'') memid, -- 会员编号 isnull(gba03c,'') gba03c, -- 会员姓名 isnull(gba05c,'') gba05c, -- 联络地址 isnull(gba08c,'') gba08c, -- Mobile No. isnull(gba11c,'') gba11c, -- E-Mail地址 isnull(gba17d,'') gba17d, -- 出生日期 isnull(gba21c,'') gba21c, -- 农历生日 isnull(srvdate,'') srvdate -- 销售日期 from work_detail with(nolock) left join gbm01 on gba00c = compid and gba01c = memid where action_id = 0 and spid = @spid and (amt2 is not null or amt3 is not null) and payway in ( select gsb03c from gsm02 where gsb01c = '3' and gsb00c = compid and gsb02c in ('1','6','WX','ZFB') ) and (compid = @companyid or @companyid = '') and srvdate between @fromdate and @todate and (@fromyg = '' or ( person_id >= @fromyg and person_id <= @toyg )) order by srvdate end else if( @rad = 2 ) -- 项目现金消费 begin select isnull(compid,'') compid, isnull(person_id,'') person_id, -- 员工代码 personName = isnull((select haa02c from ham01 where haa00c = compid and haa01c = person_id),''), isnull(amt,0) amt, -- 营业金额 isnull(comm,0) comm, --提成 isnull(amt2,0) amt2, --虚业绩 isnull(amt3,0) amt3, --真实业绩 isnull((case gender when 0 then '女' when 1 then '男' end),'') sex, isnull(billid,'') billid, -- 单号 isnull(code,'') code, -- 项目代码,或是卡号,产品码 isnull(name,'') name, -- 名称 isnull(quan,0) quan, -- 数量 isnull(memid,'') memid, -- 会员编号 isnull(gba03c,'') gba03c, -- 会员姓名 isnull(gba05c,'') gba05c, -- 联络地址 isnull(gba08c,'') gba08c, -- Mobile No. isnull(gba11c,'') gba11c, -- E-Mail地址 isnull(gba17d,'') gba17d, -- 出生日期 isnull(gba21c,'') gba21c, -- 农历生日 isnull(srvdate,'') srvdate -- 销售日期 from work_detail with(nolock) left join gbm01 on gba00c = compid and gba01c = memid where cls_flag = 1 and spid = @spid and (amt2 is not null or amt3 is not null) and payway in ( select gsb03c from gsm02 where gsb01c = '3' and gsb00c = compid and gsb02c in ('1','6','WX','ZFB') ) and (compid = @companyid or @companyid = '') and srvdate between @fromdate and @todate and (@fromyg = '' or ( person_id >= @fromyg and person_id <= @toyg )) order by srvdate end else if( @rad = 3 ) -- 外卖品 begin select isnull(compid,'') compid, isnull(person_id,'') person_id, -- 员工代码 personName = isnull((select haa02c from ham01 where haa00c = compid and haa01c = person_id),''), isnull(amt,0) amt, -- 营业金额 isnull(comm,0) comm, --提成 isnull(amt2,0) amt2, --虚业绩 isnull(amt3,0) amt3, --真实业绩 isnull((case gender when 0 then '女' when 1 then '男' end),'') sex, isnull(billid,'') billid, -- 单号 isnull(code,'') code, -- 项目代码,或是卡号,产品码 isnull(name,'') name, -- 名称 isnull(quan,0) quan, -- 数量 isnull(memid,'') memid, -- 会员编号 isnull(gba03c,'') gba03c, -- 会员姓名 isnull(gba05c,'') gba05c, -- 联络地址 isnull(gba08c,'') gba08c, -- Mobile No. isnull(gba11c,'') gba11c, -- E-Mail地址 isnull(gba17d,'') gba17d, -- 出生日期 isnull(gba21c,'') gba21c, -- 农历生日 isnull(srvdate,'') srvdate -- 销售日期 from work_detail with(nolock) left join gbm01 on gba00c = compid and gba01c = memid where cls_flag = 2 and spid = @spid and (amt2 is not null or amt3 is not null) and payway in ( select gsb03c from gsm02 where gsb01c = '3' and gsb00c = compid and gsb02c in ('1','6','WX','ZFB') ) and (compid = @companyid or @companyid = '') and srvdate between @fromdate and @todate and (@fromyg = '' or ( person_id >= @fromyg and person_id <= @toyg )) order by srvdate end end end GO /****** Object: StoredProcedure [dbo].[upg_report_gwm03r] Script Date: 06/12/2017 06:20:36 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO Create proc [dbo].[upg_report_gwm03r] ( @companyid varchar(10), --门店 @fromdate varchar(10), --起始日期 @todate varchar(10), --结束日期 @fromyg varchar(30), --起始员工 @toyg varchar(30), --结束员工 @rad int, --选择: -1 全部 0 卡销售 1 卡充值 2 项目现金消费 3 外卖品 @chk int --展示明细: -1 不展示 0 展示 ) As begin declare @spid int select @spid = @@spid delete from work_detail where spid = @spid exec upg_personal_statistics @companyid,@fromdate,@todate,@fromyg,@toyg,1,1,1,1,@spid if( @chk = -1 ) begin if( @rad = -1) --全部 begin select compid, person_id, -- 员工代码 personName = (select haa02c from ham01 where haa00c = compid and haa01c = person_id), sum(amt) amt, -- 营业金额 sum(comm) comm, --提成 sum(amt2) amt2, --虚业绩 sum(amt3) amt3, --真实业绩 case gender when 0 then '女' when 1 then '男' end sex from work_detail with(nolock) where spid = @spid and (amt2 is not null or amt3 is not null) and payway in ( select gsb03c from gsm02 where gsb01c = '3' and gsb00c = compid and gsb02c in ('1','6','WX','ZFB') ) and (compid = @companyid or @companyid = '') and srvdate between @fromdate and @todate and (@fromyg = '' or ( person_id >= @fromyg and person_id <= @toyg )) group by compid,person_id,gender order by person_id end else if( @rad = 0) --卡销售 begin select compid, person_id, -- 员工代码 personName = (select haa02c from ham01 where haa00c = compid and haa01c = person_id), sum(amt) amt, -- 营业金额 sum(comm) comm, --提成 sum(amt2) amt2, --虚业绩 sum(amt3) amt3, --真实业绩 case gender when 0 then '女' when 1 then '男' end sex from work_detail with(nolock) where action_id = 1 and spid = @spid and (amt2 is not null or amt3 is not null) and payway in ( select gsb03c from gsm02 where gsb01c = '3' and gsb00c = compid and gsb02c in ('1','6','WX','ZFB') ) and (compid = @companyid or @companyid = '') and srvdate between @fromdate and @todate and (@fromyg = '' or ( person_id >= @fromyg and person_id <= @toyg )) group by compid,person_id,gender order by person_id end else if( @rad = 1) --卡充值 begin select compid, person_id, -- 员工代码 personName = (select haa02c from ham01 where haa00c = compid and haa01c = person_id), sum(amt) amt, -- 营业金额 sum(comm) comm, --提成 sum(amt2) amt2, --虚业绩 sum(amt3) amt3, --真实业绩 case gender when 0 then '女' when 1 then '男' end sex from work_detail with(nolock) where action_id = 0 --卡充值 and spid = @spid and (amt2 is not null or amt3 is not null) and payway in ( select gsb03c from gsm02 where gsb01c = '3' and gsb00c = compid and gsb02c in ('1','6','WX','ZFB') ) and (compid = @companyid or @companyid = '') and srvdate between @fromdate and @todate and (@fromyg = '' or ( person_id >= @fromyg and person_id <= @toyg )) group by compid,person_id,gender order by person_id end else if( @rad = 2) --项目现金消费 begin select compid, person_id, -- 员工代码 personName = (select haa02c from ham01 where haa00c = compid and haa01c = person_id), sum(amt) amt, -- 营业金额 sum(comm) comm, --提成 sum(amt2) amt2, --虚业绩 sum(amt3) amt3, --真实业绩 case gender when 0 then '女' when 1 then '男' end sex from work_detail with(nolock) where cls_flag = 1 and spid = @spid and (amt2 is not null or amt3 is not null) and payway in ( select gsb03c from gsm02 where gsb01c = '3' and gsb00c = compid and gsb02c in ('1','6','WX','ZFB') ) and (compid = @companyid or @companyid = '') and srvdate between @fromdate and @todate and (@fromyg = '' or ( person_id >= @fromyg and person_id <= @toyg )) group by compid,person_id,gender order by person_id end else if( @rad = 3) --外卖品 begin select compid, person_id, -- 员工代码 personName = (select haa02c from ham01 where haa00c = compid and haa01c = person_id), sum(amt) amt, -- 营业金额 sum(comm) comm, --提成 sum(amt2) amt2, --虚业绩 sum(amt3) amt3, --真实业绩 case gender when 0 then '女' when 1 then '男' end sex from work_detail with(nolock) where cls_flag = 2 and spid = @spid and (amt2 is not null or amt3 is not null) and payway in ( select gsb03c from gsm02 where gsb01c = '3' and gsb00c = compid and gsb02c in ('1','6','WX','ZFB') ) and (compid = @companyid or @companyid = '') and srvdate between @fromdate and @todate and (@fromyg = '' or ( person_id >= @fromyg and person_id <= @toyg )) group by compid,person_id,gender order by person_id end end else --@chk = 0 begin if( @rad = -1 ) -- 全部 begin select compid, person_id, -- 员工代码 personName = (select haa02c from ham01 where haa00c = compid and haa01c = person_id), amt, -- 营业金额 comm, -- 提成 amt2, -- 虚业绩 amt3, -- 真实业绩 case gender when 0 then '女' when 1 then '男' end sex, billid, -- 单号 code, -- 项目代码,或是卡号,产品码 name, -- 名称 quan, -- 数量 memid, -- 会员编号 gba03c, -- 会员姓名 gba05c, -- 联络地址 gba08c, -- Mobile No. gba11c, -- E-Mail地址 gba17d, -- 出生日期 gba21c, -- 农历生日 srvdate -- 销售日期 from work_detail with(nolock) left join gbm01 on gba00c = compid and gba01c = memid where (amt2 is not null or amt3 is not null) and spid = @spid and payway in ( select gsb03c from gsm02 where gsb01c = '3' and gsb00c = compid and gsb02c in ('1','6','WX','ZFB') ) and (compid = @companyid or @companyid = '') and srvdate between @fromdate and @todate and (@fromyg = '' or ( person_id >= @fromyg and person_id <= @toyg )) order by srvdate end else if( @rad = 0 ) -- 卡销售 begin select compid, person_id, -- 员工代码 personName = (select haa02c from ham01 where haa00c = compid and haa01c = person_id), amt, -- 营业金额 comm, -- 提成 amt2, -- 虚业绩 amt3, -- 真实业绩 case gender when 0 then '女' when 1 then '男' end sex, billid, -- 单号 code, -- 项目代码,或是卡号,产品码 name, -- 名称 quan, -- 数量 memid, -- 会员编号 gba03c, -- 会员姓名 gba05c, -- 联络地址 gba08c, -- Mobile No. gba11c, -- E-Mail地址 gba17d, -- 出生日期 gba21c, -- 农历生日 srvdate -- 销售日期 from work_detail with(nolock) left join gbm01 on gba00c = compid and gba01c = memid where action_id = 1 and spid = @spid and (amt2 is not null or amt3 is not null) and payway in ( select gsb03c from gsm02 where gsb01c = '3' and gsb00c = compid and gsb02c in ('1','6','WX','ZFB') ) and (compid = @companyid or @companyid = '') and srvdate between @fromdate and @todate and (@fromyg = '' or ( person_id >= @fromyg and person_id <= @toyg )) order by srvdate end else if( @rad = 1 ) -- 卡充值 begin select compid, person_id, -- 员工代码 personName = (select haa02c from ham01 where haa00c = compid and haa01c = person_id), amt, -- 营业金额 comm, -- 提成 amt2, -- 虚业绩 amt3, -- 真实业绩 case gender when 0 then '女' when 1 then '男' end sex, billid, -- 单号 code, -- 项目代码,或是卡号,产品码 name, -- 名称 quan, -- 数量 memid, -- 会员编号 gba03c, -- 会员姓名 gba05c, -- 联络地址 gba08c, -- Mobile No. gba11c, -- E-Mail地址 gba17d, -- 出生日期 gba21c, -- 农历生日 srvdate -- 销售日期 from work_detail with(nolock) left join gbm01 on gba00c = compid and gba01c = memid where action_id = 0 and spid = @spid and (amt2 is not null or amt3 is not null) and payway in ( select gsb03c from gsm02 where gsb01c = '3' and gsb00c = compid and gsb02c in ('1','6','WX','ZFB') ) and (compid = @companyid or @companyid = '') and srvdate between @fromdate and @todate and (@fromyg = '' or ( person_id >= @fromyg and person_id <= @toyg )) order by srvdate end else if( @rad = 2 ) -- 项目现金消费 begin select compid, person_id, -- 员工代码 personName = (select haa02c from ham01 where haa00c = compid and haa01c = person_id), amt, -- 营业金额 comm, -- 提成 amt2, -- 虚业绩 amt3, -- 真实业绩 case gender when 0 then '女' when 1 then '男' end sex, billid, -- 单号 code, -- 项目代码,或是卡号,产品码 name, -- 名称 quan, -- 数量 memid, -- 会员编号 gba03c, -- 会员姓名 gba05c, -- 联络地址 gba08c, -- Mobile No. gba11c, -- E-Mail地址 gba17d, -- 出生日期 gba21c, -- 农历生日 srvdate -- 销售日期 from work_detail with(nolock) left join gbm01 on gba00c = compid and gba01c = memid where cls_flag = 1 and spid = @spid and (amt2 is not null or amt3 is not null) and payway in ( select gsb03c from gsm02 where gsb01c = '3' and gsb00c = compid and gsb02c in ('1','6','WX','ZFB') ) and (compid = @companyid or @companyid = '') and srvdate between @fromdate and @todate and (@fromyg = '' or ( person_id >= @fromyg and person_id <= @toyg )) order by srvdate end else if( @rad = 3 ) -- 外卖品 begin select compid, person_id, -- 员工代码 personName = (select haa02c from ham01 where haa00c = compid and haa01c = person_id), amt, -- 营业金额 comm, -- 提成 amt2, -- 虚业绩 amt3, -- 真实业绩 case gender when 0 then '女' when 1 then '男' end sex, billid, -- 单号 code, -- 项目代码,或是卡号,产品码 name, -- 名称 quan, -- 数量 memid, -- 会员编号 gba03c, -- 会员姓名 gba05c, -- 联络地址 gba08c, -- Mobile No. gba11c, -- E-Mail地址 gba17d, -- 出生日期 gba21c, -- 农历生日 srvdate -- 销售日期 from work_detail with(nolock) left join gbm01 on gba00c = compid and gba01c = memid where cls_flag = 2 and spid = @spid and (amt2 is not null or amt3 is not null) and payway in ( select gsb03c from gsm02 where gsb01c = '3' and gsb00c = compid and gsb02c in ('1','6','WX','ZFB') ) and (compid = @companyid or @companyid = '') and srvdate between @fromdate and @todate and (@fromyg = '' or ( person_id >= @fromyg and person_id <= @toyg )) order by srvdate end end end GO /****** Object: StoredProcedure [dbo].[upg_ghm01_confirm] Script Date: 06/12/2017 06:20:11 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_ghm01_confirm] ( @compid varchar(10), --公司编号 @billno varchar(100), --入库(出库)单号 @billdate varchar(8), --单据日期 @storageno varchar(4), --仓库编号 @checkman varchar(10), --复核人 @currdate varchar(8), --复核日期 @flag int, --标志位 1 复核 0 取消复核 @operateid int --作业标记 1 入库 2 出库 ) as begin set nocount on -- 当前的事务数 declare @__transaction_count int set @__transaction_count = @@trancount -- try...catch处理 begin try -- a.开启事务,或者设置事务保存点 if @__transaction_count = 0 begin tran else save tran __tran_SavePoint declare @curdate_tmp varchar(20) declare @curtime varchar(20) exec upg_getcurrdate @curdate_tmp output, @curtime output declare @__sp_re int if @operateid = 1 --入库作业 begin if @flag = 1 --复核 begin exec @__sp_re = upg_ghm01_confirm_in_check @compid, @billno, @billdate, @storageno, @checkman, @currdate, @curtime end else if @flag = 0 --取消复核 begin exec @__sp_re = upg_ghm01_confirm_in_uncheck @compid, @billno, @billdate, @storageno, @checkman, @currdate, @curtime end end else if @operateid = 2 --出库作业 begin if @flag = 1 --复核 begin exec @__sp_re = upg_ghm01_confirm_out_check @compid, @billno, @billdate, @storageno, @checkman, @currdate, @curtime end else if @flag = 0 --取消复核 begin exec @__sp_re = upg_ghm01_confirm_out_uncheck @compid, @billno, @billdate, @storageno, @checkman, @currdate, @curtime end end -- 校验系统是否配置月加权 declare @gsa03f float select @gsa03f = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'sp054' if(@gsa03f = 2) begin --复核 重新计算月加平均价 declare @month varchar(10) set @month = substring(@billdate,0,7) exec upg_inoutstock_statistic_gfm08_fh @compid,@month,@billno end if @__sp_re <> 0 raiserror('has some error', 16, 1) -- 提交事务 -- 有可提交的事务, 并且事务是在当前模块中开启的情况下, 才提交事务 if xact_state() = 1 and @__transaction_count = 0 commit -- 为了防止TRY 中有遗漏的事务处理, 可以在TRY 模拟的结束部分做最终的判断 if @__transaction_count = 0 begin if xact_state() = -1 rollback tran else begin while @@trancount > 0 commit tran end end end try begin catch -- 在CATCH 模块, 应该首先处理事务 if xact_state() <> 0 begin if @__transaction_count = 0 rollback tran -- xact_state 为-1 时, 不能回滚到事务保存点, 这种情况留给外层调用者做统一的事务回滚 -- 通过@@trancount > @__transaction_count 的判断, 即使在TRY 模块中没有设置事务保存点的情况下跳到此步骤, 也不会出错 else if xact_state() = 1 and @@trancount > @__transaction_count rollback tran __tran_SavePoint end declare @SP_Parameters varchar(max) set @SP_Parameters = '@compid = ' + isnull(@compid, '') + '@billno = ' + isnull(@billno, '') + '@billdate = ' + isnull(@billdate, '') + '@storageno = ' + isnull(@storageno, '') + '@checkman = ' + isnull(@checkman, '') + '@currdate = ' + isnull(@currdate, '') + '@flag = ' + (case when @flag is null then '' else convert(varchar(100), @flag) end) + '@operateid = ' + (case when @operateid is null then '' else convert(varchar(100), @operateid) end) insert into SP_TranLog(SPName, SPTime, SPParameter) values('upg_ghm01_confirm', getdate(), @SP_Parameters) return -1 end catch end GO /****** Object: StoredProcedure [dbo].[upg_ggm01_after_post_card] Script Date: 06/12/2017 06:20:10 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_ggm01_after_post_card] ( @compid varchar(10), -- 公司 @billno varchar(20), -- 单号 @user varchar(20) -- 复核人 ) as begin declare @orderno varchar(20) --入店单号 declare @cardno varchar(20) --卡号 declare @cardtype varchar(5) --卡类别 declare @date varchar(8) --单据日期 declare @time varchar(6) --单据时间 declare @point int --给积分 declare @sp055_3 int --0 项目支付, 1 单据支付 declare @sp040_3 int --0非IC卡 1IC卡同步 2IC卡异步 3非IC卡异步 declare @havecombopay int --是否有套餐支付 declare @haveprocpay int --是否有疗程支付 declare @procacctype varchar(5) --疗程账户 declare @procpaytype varchar(5) --疗程对应的支付方式 select @sp055_3 = isnull(gsa03f, 0) from gsm01 where gsa00c = @compid and gsa01c = 'SP055' select @sp040_3 = isnull(gsa03f, 0) from gsm01 where gsa00c = @compid and gsa01c = 'SP040' print '结账方式:' + convert(varchar(10), isnull( @sp055_3, 0)) select @procpaytype = '9', @procacctype = '4' print '疗程账户:' + + convert(varchar(10), isnull( @procacctype, 0)) print '疗程支付方式:' + + convert(varchar(10), isnull( @procpaytype, 0)) set @havecombopay = 0 set @haveprocpay = 0 select @cardno = gga09c, @cardtype = gga07c, @date = gga02d, @time = gga04t, @orderno = gga30c, @point = gga15f from ggm01 where gga00c = @compid and gga01c = @billno if @cardno like '散客%' begin return end --1. 扣卡余额 异步的情况不扣 if @sp040_3 not in(2, 3) begin declare @paytype varchar(5) declare @acctype int declare @payamt float --处理欠款可用余额的, 先在扣款前, 准备好前次的可用余额, 需要写入账户历史的 declare @accpayamt float --储值账户支付的金额 declare @enabledamt float declare @gca29f float -- declare @gcc06f float --储值账户余额, declare @gcc10f float --储值账户欠款 select @gcc06f = isnull(gcc06f, 0), @gcc10f = isnull(gcc10f, 0), @gca29f = isnull(gca29f, -1) from gcm03 with (nolock), gcm01 with (nolock) where gcc00c = gca00c and gca00c = gca13d and gca01c = gcc01c and gca01c = @cardno if @gcc10f > 0 --欠款 begin if @gca29f = -1 begin declare @sp140_3 float --控制比率 select @sp140_3 = isnull(gsa03f, 0) from gsm01 where gsa00c = @compid and gsa01c = 'SP140' select @enabledamt = @gcc06f * @sp140_3 end else begin select @enabledamt = @gca29f end end else begin select @enabledamt = @gcc06f end -- end -- --使用表变量循环gsm03, ggm02, ggm03 declare @cnt int --行数 declare @i int --当前行 declare @gsm03 table ( idd int identity not null, gsc04c varchar(5) not null, gsc05f float not null ) insert @gsm03(gsc04c, gsc05f) select isnull(gsc04c, ''), isnull(gsc05f, 0) from gsm03 where gsc00c = @compid and gsc01c = @billno and gsc02c = 'gx' select @cnt = count(1) from @gsm03 select @i = 1 while(@i <= @cnt) begin select @paytype = gsc04c, @payamt = gsc05f from @gsm03 where idd = @i if @paytype = '#' begin set @havecombopay = 1 --设置是否有套餐支付的标志 end else if @paytype = @procpaytype begin set @haveprocpay = 1 --设置是否有疗程支付的标志 end if @paytype = '5' --欠款支付方式 begin declare @fmtstr varchar(20) --充值单号默认原则 declare @gcl01c varchar(20) --生成的新单号 declare @firststaff varchar(20) --第一销售 declare @firstdept varchar(20) --第一部门 select @fmtstr = gsa02c from gsm01 where gsa00c = @compid and gsa01c = 'SPD05' --获取新单号, 类似程序中的AutoKey exec upg_auto_key @compid, @date, 'gcm10', 'gcl00c', 'gcl01c', @fmtstr, @gcl01c output --第一销售和部门 select @firstdept = ggb81c, @firststaff = ggb12c from ggm02 where ggb40i = 1 and ggb00c = @compid and ggb01c = @billno order by ggb02f desc if (isnull(@firstdept, '') = '') begin select @firstdept = ggc81c, @firststaff = ggc12c from ggm03 where ggc32i = 1 and ggc00c = @compid and ggc01c = @billno order by ggc02f desc end --生成欠款单 insert gcm10(gcl00c, gcl01c, gcl02d, gcl03c, gcl04c, gcl05c, gcl06i, gcl07f, gcl53f, gcl08f, gcl09c, gcl10c, gcl11c, gcl12c, gcl91c, gcl92d, gcl64f, gcl65f, gcl56t, gcl15c, gcl81c) values(@compid, @gcl01c, @date, @cardno, '888', '0', 5, @payamt, @payamt, 0, @firststaff, '', 'gx', @billno, @user, @date, @payamt, @payamt, @time, @cardtype, @firstdept) --更新账务日期 exec upg_updategcm10_accountdate @compid, billno --记录对应的账户异动单号 --所产生的欠款单号应该记录在原始的支付明细的欠款纪录中 --就是支付方式为'5'的纪录中 update gsm03 set gsc09c = @gcl01c where gsc00c = @compid and gsc01c = @billno and gsc02c = 'gx' and gsc04c = '5' --写欠款账户余额 exec upg_purse_deduct @compid, @cardno, @date, '888', @payamt, 5, 'gz', @gcl01c, 1 --更新会员卡账户异动单主档的复核人栏位等 exec upg_gcm10_confirm @compid, @gcl01c, @date, @user, @date, 1, 5, @acctype end else begin --获得账户编号 select @acctype = isnull(gsn02c, '') from gsm14 where gsn01c = @paytype if @acctype != '' and @acctype != '999' begin if @acctype = 2 begin select @accpayamt = isnull(@accpayamt, 0) + @payamt end exec upg_purse_deduct @compid, @cardno, @date, @acctype, @payamt, 2, 'gx', @billno, 1 end end select @i = @i + 1 end --更新上次可用余额到账户历史, 更新本次之后的可用余额 update gcm04 set gcd15f = @enabledamt where gcd00c = @compid and gcd01c = @cardno and gcd08c = @billno and gcd07c = 'gx' and gcd05i = 2 and gcd02i = 2 if isnull(@acctype, 0) > 0 begin update gcm01 set gca29f = @enabledamt - @acctype from gcm01 where gca00c = gca13d and gca01c = @cardno end end --2. 计次卡处理 if exists (select 1 from gcm01 where gca00c = gca13d and gca01c = @cardno and gca03i = 3) begin declare @times float select @times = sum(isnull(ggb05f, 0)) from ggm02 where ggb00c = @compid and ggb01c = @billno and ( (@sp055_3 = 0 and (ggb27c = '4' or (ggb27c = '9' and exists (select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP109' and gsa02c = 'QUZI')))) or @sp055_3 != 0) if isnull(@times, 0) > 0 begin update gcm01 set gca15i = isnull(gca15i, 0) + isnull(@times, 0) where gca00c = gca13d and gca01c = @cardno end end --3.扣套餐和疗程 ( 项目支付, 或者 单据支付并且支付方式为套餐疗程的时候 ) if (@sp055_3 = 0 or (@sp055_3 != 0 and (@havecombopay = 1 or @haveprocpay = 1))) begin declare @itemno varchar(20) --项目/产品编号 declare @itemcnt float --次数 declare @itemamt float --金额 declare @procid int --疗程序号 declare @comboid int --套餐序号 declare @itempaytype varchar(5) --支付方式 declare @ggm03 table ( idd int identity not null, ggc03c varchar(20) not null, ggc05f float not null, ggc11f float not null, ggc48i int not null, ggc27c varchar(5) not null ) insert @ggm03(ggc03c, ggc05f, ggc11f, ggc48i, ggc27c) select isnull(ggc03c, ''), isnull(ggc05f, 0), isnull(ggc11f, 0), isnull(ggc48i, 0), isnull(ggc27c, '') from ggm03 where ggc00c = @compid and ggc01c = @billno and isnull(ggc03c, '') != '' select @cnt = count(1) from @ggm03 select @i = 1 while (@i <= @cnt) begin select @itemno = ggc03c, @itemcnt = ggc05f, @itemamt = ggc11f, @comboid = ggc48i, @itempaytype = ggc27c from @ggm03 where idd = @i if ((@sp055_3 = 0 and @itempaytype = '#') or (@sp055_3 = 1 and @havecombopay = 1)) begin exec upg_used_combo @compid, @cardno, @itemno, @itemcnt, 1, 'gx', @billno, @date, 2, @comboid end select @i = @i + 1 end declare @ggm02 table ( idd int identity not null, ggb03c varchar(20) not null, ggb05f float not null, ggb11f float not null, ggb47i int not null, ggb48i int not null, ggb27c varchar(5) not null ) insert @ggm02 (ggb03c, ggb05f, ggb11f, ggb47i, ggb48i, ggb27c) select isnull(ggb03c, ''), isnull(ggb05f, 0), isnull(ggb11f, 0), isnull(ggb47i, 0), isnull(ggb48i, 0), isnull(ggb27c, '') from ggm02 where ggb00c = @compid and ggb01c = @billno and isnull(ggb03c, '') != '' select @cnt = count(1) from @ggm02 select @i = 1 while(@i <= @cnt) begin select @itemno = ggb03c, @itemcnt = ggb05f, @itemamt = ggb11f, @procid = ggb47i, @comboid = ggb48i, @itempaytype = ggb27c from @ggm02 where idd = @i if ((@sp055_3 = 0 and @itempaytype = '#') or (@sp055_3 = 1 and @havecombopay = 1)) begin exec upg_used_combo @compid, @cardno, @itemno, @itemcnt, 1, 'gx', @billno, @date, 1, @comboid end select @i = @i + 1 end select @i = 1 while(@i <= @cnt) begin select @itemno = ggb03c, @itemcnt = ggb05f, @itemamt = ggb11f, @procid = ggb47i, @comboid = ggb48i, @itempaytype = ggb27c from @ggm02 where idd = @i if (@sp055_3 = 0 and @itempaytype = @procpaytype) or (@sp055_3 = 1 and @haveprocpay = 1) begin exec upg_used_cureproc_ex @compid, @cardno, @itemno, @itemcnt, @itemamt, 1, 'gx', @billno, @date, @procid end select @i = @i + 1 end end --4, 处理积分 这里未复核不能处理积分 --5 UpdataConsumneFlag if exists ( select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP040' and gsa03f in (2, 3)) and exists ( select 1 from gsm01 where gsa00c = @compid and gsa01c = 'SP043' and gsa02c != '1') begin exec upg_updatecard_consumestat @compid, @billno, @cardno, 1 end end GO /****** Object: StoredProcedure [dbo].[upg_personal_statistics_allcomp] Script Date: 06/12/2017 06:20:26 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_personal_statistics_allcomp] ( @compid varchar(10), -- 公司别 @fromdate varchar(10), -- 开始日期 @todate varchar(10), -- 截至日期 @fromempl varchar(20), -- 查询开始人员 @toempl varchar(20), -- 查询截至人员 @inc_card int, -- 卡销售 @inc_service int, -- 服务 @inc_goods int, -- 是否包括产品销售 @return_type int, --返回类别 @nsortby int ) as begin declare @each_comp varchar(10) declare @spid int declare @fromspid int declare @tospid int declare @custverflag varchar(20) select @spid = @@spid + 1000 select @fromspid = @spid select @return_type = @return_type + 5 declare cur_each_son cursor for select gaz02c from gam26 where gaz01c = @compid open cur_each_son fetch cur_each_son into @each_comp while @@fetch_status = 0 begin delete workdetail_by_empl where spid = @spid if exists(select 1 from ham01 where haa00c = @each_comp and ((haa01c >= @fromempl and haa01c <= @toempl) or (@fromempl = '*'))) begin exec upg_personal_statistics @each_comp, @fromdate, @todate, @fromempl, @toempl, @inc_card, @inc_service, @inc_goods, @return_type, @spid end select @tospid = @spid select @spid = @spid + 1 fetch cur_each_son into @each_comp end close cur_each_son deallocate cur_each_son create table #workdetail_by_empl_buf( id int identity , -- 流水好 person_id varchar(20) not null, -- 员工代码 name varchar(40) null, --名称 amt_c float null, --卡销售 amt_1 float null, --指定大工 amt_12 float null, --轮班大工 amt_13 float null, --补工大工 amt_2 float null, --指定中工 amt_21 float null, --轮班中工 amt_3 float null, --指定小工 amt_31 float null, --轮班小工 amt_4 float null, --指定四工 amt_41 float null, --轮班四工 amt_g float null, --产品销售金额 amtrate_1 float null, --一个员工指定服务的金额占总的指定金额的比率 comm float null, --提成 total_srv float null, --项目总业绩 total_perf float null, --员工总业绩(服务项目+产品销售) total_cash float null, --员工总现金(现金产品+现金服务), 包括银行卡 total_cash_c float null, --员工总现金(现金产品+现金服务), 包括银行卡, 包括卡 bonus float null, --员工奖罚 total_srv_cash float null, --项目现金支付(现金实做) total_srv_card float null, --项目卡支付(卡实做) num_fix float null, --指定的项目数量 num_nofix float null, --轮班的项目数量 amt_c_cash float null, --卖卡的现金, 包括银行卡 oper_amt float null, -- 营业金额 total_vperf float null, -- 虚业绩 total_tperf float null, -- 实业绩 quan float null, -- 数量 f_oper_amt float null, -- 营业金额(指定) f_total_vperf float null, -- 虚业绩(指定) f_total_tperf float null, -- 实业绩(指定) f_comm float null, -- 佣金(指定) f_quan float null, -- 数量(指定) t_oper_amt float null, -- 营业金额(轮班) t_total_vperf float null, -- 虚业绩(轮班) t_total_tperf float null, -- 实业绩(轮班) t_comm float null, -- 佣金(轮班) t_quan float null, -- 数量(轮班) compid varchar(10) null, --公司别 fromdate varchar(10) null, --开始日期 todate varchar(10) null, --截止日期 spid int null, --连接号 cust_num float null, --人头数 cust_comm float null, --人头数的佣金 consume_cost float null, --员工领用的消耗产品的成本 ) declare @PK_Sql nvarchar(max) set @PK_Sql = ' alter table #workdetail_by_empl_buf add constraint PK_#workdetail_by_empl_buf_' + convert(varchar(10), cast(floor(rand() * 1000000) as int)) + ' PRIMARY KEY NonCLUSTERED(id)' exec sp_executesql @PK_Sql --员工代码以及名字相同则合并 insert #workdetail_by_empl_buf(person_id, name, amt_c, amt_1, amt_12, amt_13, amt_2, amt_21, amt_3, amt_31, amt_4, amt_41, amt_g, amtrate_1, comm, total_srv, total_perf, total_cash, total_cash_c, bonus, total_srv_cash, total_srv_card, num_fix, num_nofix, amt_c_cash, oper_amt, total_vperf, total_tperf, quan, f_oper_amt, f_total_vperf, f_total_tperf, f_comm, f_quan, t_oper_amt, t_total_vperf, t_total_tperf, t_comm, t_quan, cust_num, cust_comm, consume_cost) select person_id, name, sum(amt_c), sum(amt_1), sum(amt_12), sum(amt_13), sum(amt_2), sum(amt_21), sum(amt_3), sum(amt_31), sum(amt_4), sum(amt_41), sum(amt_g), sum(amtrate_1), sum(comm), sum(total_srv), sum(total_perf), sum(total_cash), sum(total_cash_c), sum(bonus), sum(total_srv_cash), sum(total_srv_card), sum(num_fix), sum(num_nofix), sum(amt_c_cash), sum(oper_amt), sum(total_vperf), sum(total_tperf), sum(quan), sum(f_oper_amt), sum(f_total_vperf), sum(f_total_tperf), sum(f_comm), sum(f_quan), sum(t_oper_amt), sum(t_total_vperf), sum(t_total_tperf), sum(t_comm), sum(t_quan), sum(cust_num), sum(cust_comm), sum(consume_cost) from workdetail_by_empl where spid >= @fromspid and spid <= @tospid group by person_id, name if (@nsortby = 0) begin select * from #workdetail_by_empl_buf order by isnull(amt_c, 0) desc end else if (@nsortby = 1) begin select * from #workdetail_by_empl_buf order by isnull(amt_1, 0) + isnull(amt_12, 0) + isnull(amt_13, 0) + isnull(amt_2, 0) + isnull(amt_21, 0) + isnull(amt_3, 0) + isnull(amt_31, 0) + isnull(amt_4, 0) + isnull(amt_41, 0) desc end else if (@nsortby = 2) begin select * from #workdetail_by_empl_buf order by isnull(amt_g, 0) desc end else if (@nsortby = 3) begin select * from #workdetail_by_empl_buf order by isnull(amt_c, 0) + isnull(amt_1, 0) + isnull(amt_12, 0) + isnull(amt_13, 0) + isnull(amt_2, 0) + isnull(amt_21, 0) + isnull(amt_3, 0) + isnull(amt_31, 0) + isnull(amt_4, 0) + isnull(amt_41, 0) + isnull(amt_g, 0) desc end else if (@nsortby = 4) begin select * from #workdetail_by_empl_buf order by total_perf end else if (@nsortby = 5) begin select * from #workdetail_by_empl_buf order by total_cash_c end else if (@nsortby = 6) begin --芳子版本需要修正 --如果选择 主人员服务项目业绩,那么报表中的 项目业绩;也应该不包括这个主人员,作为 中工,小工,四工的服务项目业绩 select @custverflag = gsa02c from gsm01 where gsa00c = @compid and gsa01c = 'SP109' if @custverflag = 'FANGZI' begin update a set a.total_srv = isnull(amt_1, 0) + isnull(amt_12, 0) + isnull(amt_13, 0) from #workdetail_by_empl_buf a end select * from #workdetail_by_empl_buf order by total_srv desc end else if (@nsortby = 7) begin --芳子版本需要修正 --排序标准是其他服务人员总业绩,即 中工,小工,四工服务业绩 + 产品 + 卡销售,那么,总业绩(卡)中,就不要包括大工的 项目服务的业绩了 select @custverflag = gsa02c from gsm01 where gsa00c = @compid and gsa01c = 'SP109' if @custverflag = 'FANGZI' begin update a set a.total_srv = isnull(amt_2, 0) + isnull(amt_21, 0) + isnull(amt_3, 0) + isnull(amt_31, 0) + isnull(amt_4, 0) + isnull(amt_41, 0) from #workdetail_by_empl_buf a end select * from #workdetail_by_empl_buf order by isnull(amt_c, 0) + isnull(total_srv, 0) + isnull(amt_g, 0) desc end else if (@nsortby = 8) --Add by maql 20100629 按员工编号排序 begin select * from #workdetail_by_empl_buf order by person_id end drop table #workdetail_by_empl_buf end GO /****** Object: StoredProcedure [dbo].[upg_emp_yeji_allcomp] Script Date: 06/12/2017 06:20:02 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_emp_yeji_allcomp] ( @compid varchar(10), -- 公司别 @empid varchar(20), -- 查询开始人员 @fromdate varchar(10), -- 开始日期 @todate varchar(10), -- 截至日期 @return_type int --返回类别 ) as begin create table #workdetail_by_empl_buf ( id int identity, -- 流水号 person_id varchar(20) NOT NULL, -- 员工代码 name varchar(40) NULL, --名称 total_vperf float NULL, -- 虚业绩 compid varchar(10) NULL, --公司别 -- constraint PK_workdetail_by_empl_buf PRIMARY KEY NONCLUSTERED(id) ) create clustered index PK_workdetail_by_empl_buf on #workdetail_by_empl_buf(id) create table #workdetail_by_empl_buf_mingci ( mingci int identity, -- 流水号 person_id varchar(20) NOT NULL, -- 员工代码 name varchar(40) NULL, --名称 total_vperf float NULL, -- 虚业绩 compid varchar(10) NULL, --公司别 compname varchar(40) NULL, --公司简称 --constraint PK_workdetail_by_empl_buf_mingci PRIMARY KEY NONCLUSTERED(mingci) ) create clustered index PK_workdetail_by_empl_buf_mingci on #workdetail_by_empl_buf_mingci(mingci) declare @each_comp varchar(10) declare @spid int declare @fromspid int declare @tospid int declare @custverflag varchar(20) select @spid = @@spid+1000 select @fromspid = @spid select @return_type=@return_type + 5 declare cur_each_son cursor for select gaz02c from gam26 where gaz01c=@compid open cur_each_son fetch cur_each_son into @each_comp while @@fetch_status=0 begin if not exists(select 1 from gam26 where gaz01c=@each_comp and gaz02c<>@each_comp) begin exec upg_personal_statistics @each_comp,@fromdate,@todate,'*','*',1,1,1,@return_type,@spid --员工代码以及名字相同则合并 insert #workdetail_by_empl_buf(compid,person_id,name,total_vperf) select compid,person_id,name,sum(total_vperf) from workdetail_by_empl where spid=@spid group by compid,person_id,name end select @tospid = @spid select @spid = @spid + 1 fetch cur_each_son into @each_comp end close cur_each_son deallocate cur_each_son declare @yeji float declare @mingci int select @yeji=total_vperf FROM #workdetail_by_empl_buf with(nolock) where person_id=@empid select @mingci=count(total_vperf) FROM #workdetail_by_empl_buf with(nolock) where total_vperf>=@yeji INSERT #workdetail_by_empl_buf_mingci(compid,person_id,name,total_vperf,compname) SELECT compid,person_id,name,total_vperf,compname=(select top 1 gae03c from gam05 where gae01c=compid) FROM #workdetail_by_empl_buf with(nolock) WHERE total_vperf>0 ORDER BY total_vperf desc SELECT mingci,compid,person_id,name,total_vperf,compname FROM #workdetail_by_empl_buf_mingci with(nolock) WHERE (mingci <= 10 or ( mingci >= @mingci-5 AND mingci <=@mingci+5)) drop table #workdetail_by_empl_buf drop table #workdetail_by_empl_buf_mingci end GO /****** Object: StoredProcedure [dbo].[upg_comm_everyday] Script Date: 06/12/2017 06:19:42 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_comm_everyday] ( @compid varchar(10), -- 公司别 @staffid varchar(20), -- 员工代码 @fromdate varchar(10), -- 开始日期 @todate varchar(10), -- 截至日期 @onlyone int -- 1-只有一条记录 0-每天一条记录 ) as begin create table #tbl_staff_sum ( staffid varchar(20) not null, staffname varchar(20) null, ddate varchar(20) not null, amt_proj float null, -- 项目营业额 amt_good float null, -- 产品营业额 amt_card float null, -- 卡销售营业额 vperf_proj float null, -- 项目虚业绩 vperf_good float null, -- 产品虚业绩 vperf_card float null, -- 卡销售虚业绩 tperf_proj float null, tperf_good float null, tperf_card float null, comm_proj float null, -- 项目提成 comm_good float null, comm_card float null, cust_num float null, -- 客数 male_num float null, female_num float null, primary key(staffid,ddate) ) create table #tbl_buffer ( cls_flag int null, -- 1-项目 2-产品 3-卡 amt float null, vperf float null, tperf float null, comm float null ) create nonclustered index tbl_buffer_idx_01 ON #tbl_buffer(cls_flag) declare @spid int select @spid = @@spid if @onlyone = 1 begin exec upg_personal_statistics @compid, @fromdate, @todate, @staffid, @staffid, 1, 1, 1, 8, @spid -- 合成数据到结果表中 insert #tbl_staff_sum(staffid, staffname, ddate) select @staffid, haa02c, @fromdate from ham01 with (nolock) where haa00c = @compid and haa01c = @staffid insert #tbl_buffer(cls_flag, amt, vperf, tperf, comm) select cls_flag, sum(oper_amt), sum(total_vperf), sum(total_tperf), sum(comm) from workdetail_by_kind with (nolock) where spid = @spid and person_id = @staffid group by cls_flag -- 项目 update a set a.amt_proj = b.amt, a.vperf_proj = b.vperf, a.tperf_proj = b.tperf, a.comm_proj = b.comm from #tbl_staff_sum a, #tbl_buffer b where a.staffid = @staffid and b.cls_flag = 1 -- 产品 update a set a.amt_good = b.amt, a.vperf_good = b.vperf, a.tperf_good = b.tperf, a.comm_good = b.comm from #tbl_staff_sum a, #tbl_buffer b where a.staffid = @staffid and b.cls_flag = 2 -- 会员卡销售 update a set a.amt_card = b.amt, a.vperf_card = b.vperf, a.tperf_card = b.tperf, a.comm_card = b.comm from #tbl_staff_sum a, #tbl_buffer b where a.staffid = @staffid and b.cls_flag = 3 -- 客数 update a set a.cust_num = b.cust_num from #tbl_staff_sum a, workdetail_by_empl b where a.staffid = b.person_id and b.spid = @spid end else begin declare @cur_date datetime declare @strtmp varchar(30) declare @str_date varchar(20) create table #work_detail_everyday( id int identity , -- 流水好 person_id varchar(20) not null, -- 员工代码 srvdate varchar(10) not null, --日期 amt float null, --营业金额 comm float null, --提成 amt2 float null, --虚业绩 amt3 float null, --真实业绩 cls_flag int null, -- 1:项目 2:产品 3:卡 primary key nonclustered(id)) create clustered index work_detail_everyday_idx_01 on #work_detail_everyday(person_id,srvdate,cls_flag) create table #person_cust_num_everyday ( person_id varchar(20) not null, srvdate float not null, cust_num float null, primary key(person_id,srvdate) ) exec upg_personal_statistics @compid, @fromdate, @todate, @staffid, @staffid, 1, 1, 1, 8, @spid insert #work_detail_everyday(person_id,srvdate,amt,amt2,amt3,comm,cls_flag) select person_id,srvdate,sum(amt),sum(amt2),sum(amt3),sum(comm),cls_flag from work_detail with(nolock) where spid = @spid group by person_id,srvdate,cls_flag insert #person_cust_num_everyday(person_id, srvdate, cust_num) select person_id,srvdate,count(distinct billid) from work_detail with (nolock) where spid = @spid and cls_flag = 1 group by person_id,srvdate -- 每天循环计算 set @cur_date= convert(datetime,@fromdate) while (@cur_date <= convert(datetime, @todate)) begin select @strtmp = convert(varchar(10), cast(@cur_date as datetime), 120) select @str_date = substring(@strtmp, 1, 4) + substring(@strtmp, 6, 2) + substring(@strtmp, 9, 2) insert #tbl_staff_sum(staffid, ddate) select distinct person_id, @str_date from #work_detail_everyday select @cur_date = dateadd(day, 1, @cur_date) end --start 赋值 -- 项目 update a set a.amt_proj = b.amt, a.vperf_proj = b.amt2, a.tperf_proj = b.amt3, a.comm_proj = b.comm from #tbl_staff_sum a, #work_detail_everyday b where a.staffid = b.person_id and a.ddate = b.srvdate and b.cls_flag = 1 -- 产品 update a set a.amt_good = b.amt, a.vperf_good = b.amt2, a.tperf_good = b.amt3, a.comm_good = b.comm from #tbl_staff_sum a, #work_detail_everyday b where a.staffid = b.person_id and a.ddate = b.srvdate and b.cls_flag = 2 -- 会员卡销售 update a set a.amt_card = b.amt, a.vperf_card = b.amt2, a.tperf_card = b.amt3, a.comm_card = b.comm from #tbl_staff_sum a, #work_detail_everyday b where a.staffid = b.person_id and a.ddate = b.srvdate and b.cls_flag = 3 -- 客数,以及提成 update a set a.cust_num = b.cust_num from #tbl_staff_sum a, #person_cust_num_everyday b where a.staffid = b.person_id and a.ddate = b.srvdate update a set a.staffname = b.haa02c from #tbl_staff_sum a,ham01 b where a.staffid = b.haa01c and b.haa00c = @compid --end drop table #work_detail_everyday drop table #person_cust_num_everyday end select * from #tbl_staff_sum drop table #tbl_staff_sum drop table #tbl_buffer end GO /****** Object: StoredProcedure [dbo].[upg_confirm_gcm10_whole] Script Date: 06/12/2017 06:19:44 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_confirm_gcm10_whole] ( @compid varchar(10), @billid varchar(20), @confirm varchar(20), @confirmdate varchar(20), @acctid varchar(10), @amt float, @cardid varchar(20), @confirmflag int ) as begin declare @billtype varchar(10) declare @acctid_int int declare @proj varchar(30) declare @buytimes float declare @sendtimes float declare @amount float declare @start_date varchar(20) declare @end_date varchar(20) declare @priceflow float declare @discount float declare @cardnum float declare @issent int declare @isgift int declare @gcf24i int declare @memo varchar(60) declare @usetimemonth float select @billtype = 'gz' select @acctid_int = convert(int, @acctid) exec upg_gcm10_confirm @compid, @billid, @confirmdate, @confirm, @confirmdate, @confirmflag, 0, @acctid declare cur_each_gcm11 cursor for select gcm02c, gcm03f, gcm04f, gcm05f, @confirmdate, gcm06d, gcm09i, gcm10f, gcm11f, 0, gcm12i, gcm13c, gcm14i, gcm08f from gcm11 where gcm00c = @compid and gcm01c = @billid open cur_each_gcm11 fetch cur_each_gcm11 into @proj, @buytimes, @sendtimes, @amount, @start_date, @end_date, @priceflow, @discount, @cardnum, @issent, @isgift, @memo, @gcf24i, @usetimemonth while @@fetch_status = 0 begin exec upg_add_cureproc_ex @compid, @cardid, @proj, @buytimes, @sendtimes, @amount, @confirmflag, 1, @start_date, @end_date, @billtype, @billid, @confirmdate, @usetimemonth, @priceflow, @discount, @cardnum, @issent, @isgift, @memo, -1, @gcf24i fetch cur_each_gcm11 into @proj, @buytimes, @sendtimes, @amount, @start_date, @end_date, @priceflow, @discount, @cardnum, @issent, @isgift, @memo, @gcf24i, @usetimemonth end close cur_each_gcm11 deallocate cur_each_gcm11 exec upg_purse_deduct @compid, @cardid, @confirmdate, @acctid_int, @amt, 0, @billtype, @billid, @confirmflag end GO /****** Object: StoredProcedure [dbo].[upg_reset_proc_acct_remain] Script Date: 06/12/2017 06:20:37 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_reset_proc_acct_remain] ( @compid varchar(10), @cardid varchar(20), @curdate varchar(10), @defamt float, -- 欠款金额 @billtype varchar(10), @billid varchar(20) ) as begin declare @amt float declare @proc_acct int select @proc_acct = 4 declare @cardcompid varchar(10) exec upg_get_card_compid @cardid, @cardcompid output -- 这个金额是包括可能的欠款的 select @amt = sum(gcf10f) from gcm06 with (nolock) where gcf00c = @cardcompid and gcf01c = @cardid --把可能的欠款扣除掉 select @amt = isnull(@amt, 0)-isnull(@defamt, 0) if exists(select 1 from gcm03 with (nolock) where gcc00c = @cardcompid and gcc01c = @cardid and gcc03i = @proc_acct) begin update gcm03 set gcc06f = @amt, gcc10f = isnull(@defamt, 0) where gcc00c = @cardcompid and gcc01c = @cardid and gcc03i = @proc_acct end else begin exec upg_add_money_full_ex @compid, @cardid, @amt, @defamt, 1, @proc_acct, @billtype, @billid end end GO /****** Object: StoredProcedure [dbo].[upg_add_cureproc] Script Date: 06/12/2017 06:19:23 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_add_cureproc] ( @compid varchar(10), @cardno varchar(30), @proj varchar(30), @buytimes float, @sendtimes float, @amount float, @confirm int, @addtoremain int, @start_date varchar(8), @end_date varchar(8)) as begin exec upg_add_cureproc_ex @compid, @cardno, @proj, @buytimes, @sendtimes, @amount, @confirm, @addtoremain, @start_date, @end_date, '', '', '', 0, 0, 0, 1, 0, 0, '', -1 end GO /****** Object: StoredProcedure [dbo].[upg_add_money_ex] Script Date: 06/12/2017 06:19:24 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_add_money_ex] ( @compid varchar(10), -- 公司别 @cardno varchar(30), -- 卡号 @amount float, -- 金额 -- 已经扣除了欠款部分 @defict float, -- 欠款金额 @confirm int, -- 是否复核 1-存款复核 0-存款取消复核 3-取款取消复核 2-取款复核 @type int -- 2:储值 1:电子钱包 ) as--wuxm, 2004/08/14 begin exec upg_add_money_full_ex @compid, @cardno, @amount, @defict, @confirm, @type, '', '' end GO /****** Object: StoredProcedure [dbo].[upg_init_customers_all] Script Date: 06/12/2017 06:20:13 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_init_customers_all] ( @compid varchar(10), @ddate varchar(8) ) as begin declare @cardno varchar(20) declare @cardtype varchar(20) declare @cust_name varchar(20) declare @mobile varchar(20) declare @birth varchar(20) declare @gender int declare @telno varchar(20) declare @addr varchar(80) declare @remain float declare @fromdate varchar(20) declare @todate varchar(20) declare @ttimes float declare @tused float declare @memid varchar(20) declare @points float declare @proc_remain float declare @remain_3 float declare @remain_4 float declare @remain_5 float declare @remain_6 float declare @acct_1_mem varchar(60) -- 储值账户的备注 declare @acct_p_mem varchar(60) -- 积分备注 declare @acct_2_mem varchar(60) -- 2备注 declare @acct_3_mem varchar(60) -- 3备注 declare @acct_4_mem varchar(60) -- 4备注 declare @acct_5_mem varchar(60) -- 5备注 declare @acct_6_mem varchar(60) -- 6备注 declare @psid varchar(30) declare @cardmemo varchar(1000) -- 会员卡备注 --add lucz 2016.02.28 declare @remain_7 float declare @acct_7_mem varchar(60) -- 7备注 declare @remain_8 float declare @acct_8_mem varchar(60) -- 8备注 declare cur_geteach cursor for select gcn17c, gcn01c, gcn02c, gcn03c, gcn04c, gcn05c, gcn07c, gcn08c, gcn12d, -- 1 gcn09f, gcn25c, gcn13d, gcn11i, gcn14i, gcn06i, gcn15c, -- 2 gcn16f, gcn27c, --3 gcn18f, gcn28c, --4 gcn19f, gcn29c, --5 gcn20f, gcn30c, --6 gcn21f, gcn31c, --7 gcn26f, gcn32c, --8 gcn38f,gcn40c, --9 gcn39f,gcn41c, --10 gcn23c, gcn37c from gcm12 where gcn00c = @compid and isnull(gcn10i, 0) = 0 -- 还没有开帐的 and isnull(gcn24i, 0) = 1 -- 需要开帐的 for update of gcn10i open cur_geteach fetch cur_geteach into @compid, @cardno, @cardtype, @cust_name, @mobile, @birth, @telno, @addr, @fromdate, @remain, @acct_1_mem, @todate, @ttimes, @tused, @gender, @memid, @points, @acct_p_mem, @proc_remain, @acct_2_mem, @remain_3, @acct_3_mem, @remain_4, @acct_4_mem, @remain_5, @acct_5_mem, @remain_6, @acct_6_mem, @remain_7, @acct_7_mem, @remain_8, @acct_8_mem, @psid, @cardmemo while @@fetch_status = 0 begin exec upg_init_customers @compid, @ddate, @cardno, @cardtype, @cust_name, @mobile, @birth, @gender, @telno, @addr, @remain, @acct_1_mem, @fromdate, @todate, @tused, @ttimes, @memid, @points, @acct_p_mem, @proc_remain, @acct_2_mem, @remain_3, @acct_3_mem, @remain_4, @acct_4_mem, @remain_5, @acct_5_mem, @remain_6, @acct_6_mem, @remain_7, @acct_7_mem, @remain_8, @acct_8_mem, @psid, @cardmemo update gcm12 set gcn10i = 1 where gcn00c = @compid and isnull(gcn10i, 0) = 0 and isnull(gcn24i, 0) = 1 and gcn01c = @cardno fetch cur_geteach into @compid, @cardno, @cardtype, @cust_name, @mobile, @birth, @telno, @addr, @fromdate, @remain, @acct_1_mem, @todate, @ttimes, @tused, @gender, @memid, @points, @acct_p_mem, @proc_remain, @acct_2_mem, @remain_3, @acct_3_mem, @remain_4, @acct_4_mem, @remain_5, @acct_5_mem, @remain_6, @acct_6_mem, @remain_7, @acct_7_mem, @remain_8, @acct_8_mem, @psid, @cardmemo end close cur_geteach deallocate cur_geteach -- 疗程中可能补输入的资料 exec upg_init_customers_gcm16 @compid end GO /****** Object: StoredProcedure [dbo].[upg_Verify_fex01] Script Date: 06/12/2017 06:20:52 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_Verify_fex01] ( @compId varchar(10), @fea00c varchar(10), @fea01c varchar(20), @fea93c varchar(10), @fea94d varchar(8), @time varchar(6) ) as begin set nocount on declare @__transaction_count int set @__transaction_count = @@trancount begin try if @__transaction_count = 0 begin tran else save tran __tran_upg_purse_deduct declare @fmtstr varchar(20) --充值单号默认原则 -- 修改主表 update fex01 set fea07i = 2, fea93c = @fea93c, fea94d = @fea94d where fea00c = @fea00c and fea01c = @fea01c declare @feb07c varchar(20) --仓库代码 declare cursor_ghm cursor for select feb07c from fex02 where feb06i = 1 group by feb07c open cursor_ghm fetch next from cursor_ghm into @feb07c if (@@fetch_status = 0) begin -- 产品入库 declare @gha01c nvarchar(20) select @fmtstr = gsa02c from gsm01 where gsa00c = @compId and gsa01c = 'SPD20' exec upg_autokey @fea94d, 'ghm01', 'gha01c', @fmtstr, @compId, 'gha00c', @gha01c output insert into ghm01(gha00c, gha01c, gha03d, gha04c, gha05c, gha06c, gha07i, gha08c, gha09t, gha10i, gha11c, gha12c, gha91c, gha92d, gha93c, gha94d) values(@compId, @gha01c, @fea94d, @feb07c, @fea93c, '', 3, '', @time, 2, '', @fea00c, @fea93c, @fea94d, '', '') insert into ghm02(ghb00c, ghb01c, ghb02f, ghb03c, ghb04c, ghb05f, ghb06c, ghb07f, ghb08c, ghb09f, ghb10f, ghb11f, ghb12c, ghb13f, ghb14f, ghb15d) select @compId, @gha01c, feb02f, feb03c, feb04c, feb08f, feb04c, feb08f, 'RMB', 1, feb09f, feb10f, '', feb08f, feb09f, '' from fex02 where feb00c = @fea00c and feb01c = @fea01c and feb06i = 1 and feb07c = @feb07c exec upg_ghm01_confirm @compId, @gha01c, @fea94d, @feb07c, @fea93c, @fea94d, 1, 1 fetch next from cursor_ghm into @feb07c end close cursor_ghm deallocate cursor_ghm if xact_state() = 1 and @__transaction_count = 0 commit if @__transaction_count = 0 begin if xact_state() = -1 rollback tran else begin while @@trancount > 0 commit tran end end end try begin catch if xact_state() <> 0 begin if @__transaction_count = 0 rollback tran else if xact_state() = 1 and @@trancount > @__transaction_count rollback tran __tran_upg_purse_deduct end declare @SP_Parameters varchar(max) set @SP_Parameters = '@compId = ' + isnull(@compId, '') + '@fea00c = ' + isnull(@fea00c, '') + '@fea01c = ' + isnull(@fea01c, '') + '@fea93c = ' + isnull(@fea93c, '') + '@fea94d = ' + isnull(@fea94d, '') + '@time = ' + isnull(@time, '') insert into SP_TranLog(SPName, SPTime, SPParameter) values('upg_Verify_fex01', getdate(), @SP_Parameters) return -1 end catch end GO /****** Object: StoredProcedure [dbo].[upg_Vierfy_lvm01] Script Date: 06/12/2017 06:20:52 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_Vierfy_lvm01] ( @gva00c varchar(10), -- 公司编号 @gva01c varchar(30) -- 收货单号 ) as begin set nocount on declare @__transaction_count int set @__transaction_count = @@trancount begin try if @__transaction_count = 0 begin tran else save tran __tran_upg_purse_deduct declare @fmtstr varchar(30) --充值单号默认原则 declare @gva03c varchar(10) --仓库 declare @gva05c varchar(30) --发货单号 declare @gva06c varchar(30) --采购订单号 declare @gva02d varchar(8) --日期 declare @gva10t varchar(6) --时间 declare @gva93c varchar(10) --操作人 select @gva03c = gva03c, @gva05c = gva05c, @gva06c = gva06c, @gva02d = gva02d, @gva10t = gva10t, @gva93c = gva93c from lvm01 where gva00c = @gva00c and gva01c = @gva01c -- 获取总店编号 declare @companyId varchar(20) --总店 select @companyId = gta00c from ltm01 Where gta01c = @gva05c declare @grm01Status int --订单状态 set @grm01Status = 7 --全部收到 declare @gvb02f float --序号 declare @gvb03c varchar(20) --产品 declare @gvb04f float --收货 declare @gvb05f float --发货 declare @gvb06f float --损坏 declare @gvb07f float --欠货 declare @gvb08f float --订单 declare @gvb13i int --收货来源 0-正常, 1-赠送 declare cursor_gvm cursor for select gvb02f, gvb03c, gvb04f, gvb05f, gvb06f, gvb07f, gvb08f, gvb13i from lvm02 where gvb00c = @gva00c and gvb01c = @gva01c open cursor_gvm fetch next from cursor_gvm into @gvb02f, @gvb03c, @gvb04f, @gvb05f, @gvb06f, @gvb07f, @gvb08f, @gvb13i if (@@fetch_status = 0) begin -- 更新发货明细 (是否少了一个序号的条件???) update ltm02 set gtb09f = @gvb04f where gtb01c = @gva05c and gtb03c = @gvb03c and gtb13i = @gvb13i if (@gvb13i = 0) --正常非赠品情况 begin declare @status int -- 订单状态 set @status = 7 --全部收到 if (@gvb08f - @gvb05f > 0 or @gvb05f - @gvb04f > 0) begin set @status = 6 --部分收到 set @grm01Status = 6 end if (@gvb06f != 0 or @gvb07f != 0) begin set @status = 8 --已经收到需要补货 set @grm01Status = 8 end -- 采购明细表 update lrm02 set grb11f = @gvb05f - @gvb04f, grb10c = @status where grb00c = @gva00c and grb01c = @gva06c and grb03c = @gvb03c -- 欠货 update lrm03 set grc08i = 3, grc10c = @gva01c where grc01c = @gva00c and grc03c = @gvb03c and grc02c in (select grb15c from lrm02 where grb09i = 1 and grb01c = @gva06c and grb03c = @gvb03c and grb00c = @gva00c) end fetch next from cursor_gvm into @gvb02f, @gvb03c, @gvb04f, @gvb05f, @gvb06f, @gvb07f, @gvb08f, @gvb13i end close cursor_gvm deallocate cursor_gvm -- 退货 declare @fex01c nvarchar(30) --退货单号(损坏情况退货) if exists(select 1 from lvm02 where gvb00c = @gva00c and gvb01c = @gva01c and gvb06f > 0) begin select @fmtstr = gsa02c from gsm01 where gsa00c = @gva00c and gsa01c = 'SPD36' exec upg_autokey @gva02d, 'fex01', 'fea01c', @fmtstr, @gva00c, 'fea00c', @fex01c output insert into fex01(fea00c, fea01c, fea03d, fea04t, fea05c, fea06c, fea07i, fea91c, fea92d, fea93c, fea94d) values(@gva00c, @fex01c, @gva02d, @gva10t, @gva03c, @gva93c, 1, '', '', '', '') insert into fex02(feb00c, feb01c, feb02f, feb03c, feb04c, feb05f, feb06i, feb07c, feb08f, feb09f, feb10f) select @gva00c, @fex01c, row_number() over(order by gvb00c), gvb03c, gvb09c, gvb06f, 0, '', 0, 0, 0 from lvm02 where gvb00c = @gva00c and gvb01c = @gva01c and gvb06f > 0 end -- 欠货 if exists(select 1 from lvm02 where gvb00c = @gva00c and gvb01c = @gva01c and gvb07f > 0) begin insert into lrm03(grc00c, grc01c, grc02c, grc03c, grc04f, grc05c, grc06f, grc07i, grc08i, grc09f, grc10c) select @companyId, @gva00c, @gva06c, gvb03c, gvb07f, gvb09c, 0, 0, 1, 0, '' from lvm02 where gvb00c = @gva00c and gvb01c = @gva01c and gvb07f > 0 end -- 采购主表 update lrm01 set gra04c = @grm01Status, gra13c = @gva01c where gra00c = @gva00c and gra01c = @gva06c -- 发货主表 update ltm01 set gta12c = @grm01Status, gta09c = @gva01c where gta01c = @gva05c and gta03c = @gva00c and gta04c = @gva06c -- 产品入库 declare @gha01c nvarchar(30) select @fmtstr = gsa02c from gsm01 where gsa00c = @gva00c and gsa01c = 'SPD20' exec upg_autokey @gva02d, 'ghm01', 'gha01c', @fmtstr, @gva00c, 'gha00c', @gha01c output set @gha01c = @gha01c + '('+ @companyId + '-'+ @gva00c+ ')' insert into ghm01(gha00c, gha01c, gha03d, gha04c, gha05c, gha06c, gha07i, gha08c, gha09t, gha10i, gha11c, gha12c, gha91c, gha92d, gha93c, gha94d) values(@gva00c, @gha01c, @gva02d, @gva03c, @gva93c, '', 1, '', @gva10t, 2, '', @gva01c, @gva93c, @gva02d, '', '') -- 2产品入库1进货成本gfa22f 2标准价gfa11f 3销售成本 gfa19f declare @sp060 varchar(5) select @sp060 = isnull(gsa02c,'1') from gsm01 where gsa00c = @gva00c and gsa01c = 'sp060' insert into ghm02(ghb00c, ghb01c, ghb02f, ghb03c, ghb04c, ghb05f, ghb06c, ghb07f, ghb08c, ghb09f, ghb10f, ghb11f, ghb12c, ghb13f, ghb14f, ghb15d) select @gva00c, @gha01c, row_number() over(order by gvb00c), gvb03c, gvb09c, 0, gvb09c, gvb04f, 'RMB', 1, case @sp060 when '1' then gfa22f when '2' then gfa11f when '3' then gfa19f else gfa22f end,0, '', gvb04f, gvb12f * gvb04f, '' --gvb12f, gvb12f * gvb04f, '', gvb04f, gvb12f * gvb04f, '' from lvm02 inner join gfm01 on gfa00c = @gva00c and gfa01c = gvb03c where gvb00c = @gva00c and gvb01c = @gva01c if(exists(select 1 from ghm02 where ghb00c = @gva00c and ghb01c = @gha01c )) begin update ghm02 set ghb11f = ghb10f * ghb07f where ghb00c = @gva00c and ghb01c = @gha01c end -- 入库审核 exec upg_ghm01_confirm @gva00c, @gha01c, @gva02d, @gva03c, @gva93c, @gva02d, 1, 1 -- update 产品入库单 update ghm01 set gha93c = @gva93c, gha94d = @gva02d where gha00c = @gva00c and gha01c = @gha01c if xact_state() = 1 and @__transaction_count = 0 commit if @__transaction_count = 0 begin if xact_state() = -1 rollback tran else begin while @@trancount > 0 commit tran end end end try begin catch if xact_state() <> 0 begin if @__transaction_count = 0 rollback tran else if xact_state() = 1 and @@trancount > @__transaction_count rollback tran __tran_upg_purse_deduct end declare @SP_Parameters varchar(max) set @SP_Parameters = '@gva00c = ' + isnull(@gva00c, '') + '@gva01c = ' + isnull(@gva01c, '') insert into SP_TranLog(SPName, SPTime, SPParameter) values('upg_Vierfy_lvm01', getdate(), @SP_Parameters) return -1 end catch end GO /****** Object: StoredProcedure [dbo].[upg_3g_init_card_ex] Script Date: 06/12/2017 06:19:22 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_3g_init_card_ex] ( @compid varchar(10), -- 公司别 @cardno varchar(20), -- 卡号 @cardtype varchar(5), -- 卡类别 @phone varchar(20), -- 手机号 @memname varchar(30), -- 会员姓名 @passward varchar(20), -- 初始查询密码 @gender tinyint -- 性别 0-female 1-male ) as -- 申请电子会员卡 begin declare @curdate varchar(20) declare @time varchar(20) exec upg_getcurrdate @curdate output, @time output -- 初始化卡资料 declare @unit varchar(20) declare @period int declare @default_acct int -- 默认的帐户 declare @default_amt float -- 默认的金额 select @period = gak07f, @unit = gak08c, @default_acct = gak14i, @default_amt = gak16f from gam10 with (nolock) where gak00c = @compid and gak01c = @cardtype declare @amount float declare @todate varchar(20) select @amount = gsa03f from gsm01 with (nolock) where gsa00c = @compid and gsa01c = 'SPT02' if isnull(@amount, 0) > 0 begin select @default_acct = 3, @default_amt = @amount end exec upg_cal_next_day @curdate, @unit, @period, @todate output if exists (select 1 from gcm01 where gca01c=@cardno) begin return end exec upg_init_customers @compid, @curdate, @cardno, @cardtype, @memname, @phone, '', @gender, '', '', 0, '', @curdate, @todate, 0, 0, @cardno, 0, '电子卡送积分', 0, '', 0, '', 0, '', 0, '', 0, '',0,'',0,'', '', '' -- 把储值金额充进 exec upg_add_money_expend @compid, @cardno, @default_amt, 0, 1, @default_acct, 'Send', '电子会员卡赠送' -- 初始化查询密码 update gcm01 set gca18c = @passward where gca00c = @compid and gca01c = @cardno declare @proj varchar(30) declare @buytimes float declare @month_times float declare @priceflow int declare @sendtimes float declare cur_each_set cursor for select gav02c, gav03f, gav04f, gav06f, gav07i, gav08f from gam24 where gav00c = @compid and gav01c = @cardtype open cur_each_set fetch cur_each_set into @proj, @buytimes, @amount, @month_times, @priceflow, @sendtimes while @@fetch_status = 0 begin exec upg_add_cureproc_ex @compid, @cardno, @proj, @buytimes, @sendtimes, @amount, 1, 1, @curdate, @todate, '', '电子会员卡赠送', @curdate, @month_times, @priceflow, 1, 1, 1, 0, '电子会员卡赠送', -1, -1 fetch cur_each_set into @proj, @buytimes, @amount, @month_times, @priceflow, @sendtimes end close cur_each_set deallocate cur_each_set end GO /****** Object: StoredProcedure [dbo].[upg_getcard_from_ic] Script Date: 06/12/2017 06:20:09 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_getcard_from_ic] ( @curcomp varchar(10), -- 写在IC卡中的公司别,实际上就是卡的归属公司 @rawcomp varchar(10), @cardid varchar(20), @cardtype varchar(20), @amt float, @defict float ) as begin declare @curdate varchar(20) declare @curtime varchar(20) exec upg_getcurrdate @curdate output, @curtime output declare @state int declare @flowno float -- 判断是否以卡为主 declare @sp050f float select @sp050f = gsa03f from gsm01 where gsa00c = @curcomp and gsa01c = 'SP050' if @sp050f = 2 begin -- 判断这个卡在系统中是否存在以及状态 select @state = 0 select @state = gca08i from gcm01 with (nolock) where gca13d = @curcomp and gca00c = @curcomp and gca01c = @cardid if isnull(@state, 0) = 0 begin exec upg_init_customers @curcomp, @curdate, @cardid, @cardtype, @cardid, 'mobile', 'birth', 0, 'tel', 'addr', @amt, '从IC卡读入', @curdate, '20901231', 0, 0, @cardid, 0, '', 0, '', 0, '', 0, '', 0, '', 0, '',0,'', '', '' end else if @state = 4 or @state = 5 begin -- 需要写一下日志, 否则无法追查 insert sysoperationlog(userid, program, operation, operationdate, operationtime, origatedate, compid, keyvalue1, keyvalue2, keyvalue3, keyvalue4) values('system', 'GSB01I', 'M', @curdate, @curtime, @curdate, @curcomp, @cardid, @amt, '从IC卡强制写入', '以IC卡为准') if exists(select 1 from gcm03 with (nolock) where gcc01c = @cardid and gcc00c = @curcomp and gcc03i = 2) begin update gcm03 set gcc06f = isnull(@amt, 0), gcc10f = @defict where gcc00c = @curcomp and gcc01c = @cardid and (gcc03i = 2 ) end else begin select @flowno = isnull(max(gcc02f), 0) + 1 from gcm03 with (nolock) where gcc00c = @curcomp and gcc01c = @cardid insert gcm03(gcc00c, gcc01c, gcc02f, gcc03i, gcc06f, gcc08c, gcc09d, gcc10f) values(@curcomp, @cardid, @flowno, 2, @amt, '', '', @defict) end end end end GO /****** Object: StoredProcedure [dbo].[upg_verify_lym01] Script Date: 06/12/2017 06:20:52 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_verify_lym01] ( @gya00c varchar(10), -- 公司编号 @gya01c varchar(30), -- 出库单号 @gya93c varchar(10), -- 操作人 @gya94d varchar(8), -- 日期 @gya05t varchar(6) -- 出库时间 ) as begin set nocount on -- 当前的事务数 declare @__transaction_count int set @__transaction_count = @@trancount begin try if @__transaction_count = 0 begin tran else save tran __tran_upg_purse_deduct declare @fmtstr varchar(30) --充值单号默认原则 declare @gya04i int --出库性质(1- 正常出库2- 赠品出库 3-外购产品) declare @gya03c varchar(30) --门店代码 declare @gva07i int --物品来源 declare @sum float --总数量 declare @jine float --总金额 select @gya03c = gya03c, @gya04i = gya04i from lym01 where gya00c = @gya00c and gya01c = @gya01c select @sum = sum(gyb07f), @jine = sum(gyb10f) from lym02 where gyb00c = @gya00c and gyb01c = @gya01c set @gva07i = 2 -- 默认:总部仓 if (@gya04i = 1) -- 外购产品 set @gva07i = 1 -- 供应商 -- 获取收货单号 declare @code varchar(30) select @fmtstr = gsa02c from gsm01 where gsa00c = @gya03c and gsa01c = 'SPD34' exec upg_autokey @gya94d, 'lvm01', 'gva01c', @fmtstr, @gya03c, 'gva00c', @code output -- 更新配发主表 update lym01 set gya06c = @code, gya02d = @gya94d, gya93c = @gya93c, gya94d = @gya94d where gya00c = @gya00c and gya01c = @gya01c select @code = gya06c from lym01 where gya00c = @gya00c and gya01c = @gya01c -- 生成收货主表 insert into lvm01(gva00c, gva01c, gva02d, gva03c, gva04c, gva05c, gva06c, gva07i, gva08c, gva09i, gva10t, gva93c, gva94d) values(@gya03c, @code, @gya94d, '', '', @gya01c, '', @gva07i, '', 0, @gya05t, @gya93c, @gya94d) -- 收货从表 insert into lvm02(gvb00c, gvb01c, gvb02f, gvb03c, gvb04f, gvb05f, gvb06f, gvb07f, gvb08f, gvb09c, gvb10f, gvb11f, gvb12f, gvb13i) select @gya03c, @code, gyb02f, gyb03c, 0, gyb07f, 0, 0, gyb07f, gyb06c, gyb08f, gyb11f, gyb09f, 0 from lym02 where gyb00c = @gya00c and gyb01c = @gya01c -- 产品调拨(走仓库) if @gya04i = 1 begin declare @gra01c varchar(30) select @fmtstr = gsa02c from gsm01 where gsa00c = @gya03c and gsa01c = 'SPD32' exec upg_autokey @gya94d, 'lrm01', 'gra01c', @fmtstr, @gya03c, 'gra00c', @gra01c output -- 采购主表 insert into lrm01(gra00c, gra01c, gra02d, gra03c, gra04c, gra05i, gra06t, gra07f, gra08d, gra09c, gra10d, gra11t, gra12c, gra13c, gra14f, gra15f, gra16c, gra17c, gra18i, gra19c, gra20c, gra21f, gra22f, gra91c, gra92d, gra93c, gra94d, gra95c, gra96d, gra97t) values(@gya03c, @gra01c, @gya94d, @gya93c, '5', 1, @gya05t, @jine, @gya94d, @gya93c, @gya94d, @gya05t, '', @code, @jine, 0, '', '', @gva07i, '', '', @sum, @sum, @gya93c, @gya94d, @gya93c, @gya94d, @gya93c, @gya94d, @gya05t) -- 获取库存量 create table #detail --存放入库的数量(进货单位向标准单位转换) ( goodsno varchar(30) null, --产品编号 goodsname varchar(80) null, --产品名称 garageno varchar(30) null, --仓库编号 garagename varchar(60) null, --仓库名称 goodstype varchar(40) null, --产品类别 price float null, --标准单价 unit varchar(10) null, --单位 quantity float null, --数量 amt float null, --金额 stattype varchar(30) null --统计类别 ) declare @grb03c varchar(30) -- 产品编号 declare @vgfa97c varchar(30) -- 存放仓库 declare detail_cur cursor for select gyb03c, gfa97c from lym02 inner join lym01 on lym01.gya00c = lym02.gyb00c and lym01.gya01c = lym02.gyb01c inner join gfm01 on lym02.gyb03c = gfa01c and lym01.gya03c = gfm01.gfa00c where gyb00c = @gya00c and gyb01c = @gya01c open detail_cur fetch next from detail_cur into @grb03c, @vgfa97c while (@@fetch_status = 0) begin insert into #detail exec upg_stock_statistic @gya03c, @gya94d, @grb03c, @grb03c, @vgfa97c, @vgfa97c, 1, 0, '*', '*', '*', 0 fetch next from detail_cur into @grb03c, @vgfa97c end close detail_cur deallocate detail_cur -- 采购从表 insert into lrm02(grb00c, grb01c, grb02f, grb03c, grb04f, grb05c, grb06f, grb07f, grb08f, grb09i, grb10c, grb11f, grb12f, grb13f, grb14f, grb15c, grb16f, grb17f, grb18f, grb19f, grb20i, grb21c) select @gya03c, @gra01c, gyb02f, gyb03c, gyb07f, gyb06c, gyb09f, gyb10f, gyb07f, 0, 5, 0, 0, #detail.quantity, 0, '', gyb10f, gyb07f, gyb10f, gyb05f - gyb07f, @gva07i, '' from lym02 left join #detail on lym02.gyb03c = #detail.goodsno where gyb00c = @gya00c and gyb01c = @gya01c declare @gae14c varchar(20) -- 门店负责人 declare @gae08c varchar(300) -- 地址 select @gae14c = gae14c, @gae08c = gae08c from gam05 where gae01c = @gya03c -- 获取发货单据号 declare @gta01c nvarchar(30) select @fmtstr = gsa02c from gsm01 where gsa00c = @gya00c and gsa01c = 'SPD33' exec upg_autokey @gya94d, 'ltm01', 'gta01c', @fmtstr, @gya00c, 'gta00c', @gta01c output declare @gfa97c varchar(20) --存放仓库 select @gfa97c = gfa97c from gfm01 where gfa00c = @gya00c and gfa01c in (select gyb03c from lym02 where gyb00c = @gya00c and gyb01c = @gya01c) if (@gfa97c is null or @gfa97c = '') set @gfa97c = '01' -- 发货主表 insert into ltm01(gta00c, gta01c, gta02d, gta03c, gta04c, gta05d, gta06i, gta07c, gta08d, gta09c, gta10c, gta11d, gta12c, gta13c, gta14c, gta15c, gta16t, gta17f, gta18f, gta19c, gta91c, gta92d, gta93c, gta94d) values(@gya00c, @gta01c, @gya94d, @gya03c, @gra01c, @gya94d, 1, @gya93c, @gya94d, @code, @gya93c, @gya94d, '5', @gae08c, @gae14c, @gfa97c, @gya05t, @sum, @jine, '', @gya93c, @gya94d, @gya93c, @gya94d) -- 发货从表 insert into ltm02(gtb00c, gtb01c, gtb02f, gtb03c, gtb04f, gtb05c, gtb06f, gtb07f, gtb08f, gtb09f, gtb10f, gtb11f, gtb12f, gtb13i, gtb14f) select @gya00c, @gta01c, gyb02f, gyb03c, gyb07f, gyb06c, gyb10f, gyb07f, gyb07f, 0, gyb09f, gyb11f, gyb08f, 0, gyb12f from lym02 where gyb00c = @gya00c and gyb01c = @gya01c -- 更新采购主表 update lrm01 set gra12c = @gta01c, gra19c = @gfa97c where gra00c = @gya03c and gra01c = @gra01c -- 更新收货主表 update lvm01 set gva06c = @gra01c where gva00c = @gya03c and gva01c = @Code -- 产品出库 declare @gia01c nvarchar(20) select @fmtstr = gsa02c from gsm01 where gsa00c = @gya00c and gsa01c = 'SPD21' exec upg_autokey @gya94d, 'gim01', 'gia01c', @fmtstr, @gya00c, 'gia00c', @gia01c output insert into gim01(gia00c, gia01c, gia03d, gia04c, gia05c, gia06c, gia07i, gia08t, gia09i, gia10c, gia11c, gia91c, gia92d, gia93c, gia94d) values(@gya00c, @gia01c, @gya94d, @gfa97c, @gya03c, '', 1, @gya05t, 2, '', @gta01c, @gya93c, @gya94d, '', '') --sp058 3产品出库价默认1进货价 gfa22f 2 销售成本 gfa19f 3标准价gfa11f 4 销售成本(门店)+标准价(员工), 5 月加权 declare @sp058 int select @sp058 = isnull(gsa03f,1) from gsm01 where gsa00c = @gya00c and gsa01c = 'sp058' insert into gim02(gib00c, gib01c, gib02f, gib03c, gib04c, gib05f, gib06c, gib07f, gib08f, gib09f, gib10f, gib11f, gib12c) select @gya00c, @gia01c, gyb02f, gyb03c, '', gyb05f, gyb06c, gyb07f, gyb07f, case @sp058 when 1 then gfa22f when 2 then gfa19f when 3 then gfa11f when 5 then (select gfh03f from gfm08 where gfh00c = @gya00c and gfh01c = gyb03c and gfh02c = substring(@gya94d,1,6)) else gfa22f end,0,gyb09f,'' --gyb09f, gyb10f, gyb09f, '' from lym02 inner join gfm01 on gfa00c = @gya00c and gfa01c = gyb03c where gyb00c = @gya00c and gyb01c = @gya01c --更新金额 if(exists(select 1 from gim02 where gib00c = @gya00c and gib01c = @gia01c)) begin update gim02 set gib10f = isnull(gib09f,0) * isnull(gib07f,0) where gib00c = @gya00c and gib01c = @gia01c end -- 出库审核 exec upg_ghm01_confirm @gya00c, @gia01c, @gya94d, @gfa97c, @gya93c, @gya94d, 1, 2 --生成的入库单号 --@billid + '(' + @compid + '-' + @incompid + ')' declare @incompid varchar(30) set @incompid = @gia01c+ '(' + @gya00c + '-' + @gya03c + ')' delete from ghm01 where gha00c = @gya03c and gha01c = @incompid -- 清除临时表 drop table #detail end if xact_state() = 1 and @__transaction_count = 0 commit if @__transaction_count = 0 begin if xact_state() = -1 rollback tran else begin while @@trancount > 0 commit tran end end end try begin catch if xact_state() <> 0 begin if @__transaction_count = 0 rollback tran else if xact_state() = 1 and @@trancount > @__transaction_count rollback tran __tran_upg_purse_deduct end declare @SP_Parameters varchar(max) set @SP_Parameters = '@gya00c = ' + isnull(@gya00c, '') + '@gya01c = ' + isnull(@gya01c, '') + '@gya93c = ' + isnull(@gya93c, '') + '@gya94d = ' + isnull(@gya94d, '') + '@gya05t = ' + isnull(@gya05t, '') insert into SP_TranLog(SPName, SPTime, SPParameter) values('upg_verify_lym01', getdate(), @SP_Parameters) return -1 end catch end GO /****** Object: StoredProcedure [dbo].[upg_handle_point] Script Date: 06/12/2017 06:20:12 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_handle_point] ( @compid varchar(10), -- 公司编号 @billid varchar(20), -- 单据编号 @billtype varchar(5), -- 单据类型 @curdate varchar(8), -- 当前日期 @confirm int -- 1-复核 0-取消复核 ) as begin declare @cardno varchar(20) declare @fillamt float declare @updatepointvalues float declare @pointclass int declare @pointvalues int declare @cardclass varchar(10) if (@billtype = 'gn') begin exec upg_update_point @compid, @billid, null, @curdate, 1, @confirm end else if (@billtype = 'gz') begin exec upg_update_point @compid, null, @billid, @curdate, 2, @confirm end else if (@billtype = 'gx') begin select @cardno = isnull(gga09c, '') from ggm01 where gga00c = @compid and gga01c = @billid exec upg_manage_points @compid, @cardno, 'gx', @billid, @curdate, 0, @confirm end else if (@billtype = 'ge') begin declare @changetype int select @changetype = gea02i from gem01 where gea00c = @compid and gea01c = @billid if (@changetype <> 4 and @changetype <> 12) return select @cardno = isnull(gea07c, '') from gem01 where gea00c = @compid and gea01c = @billid select @cardclass = isnull(gca02c, '') from gcm01 with (nolock) where gca00c = @compid and gca01c = @cardno select @fillamt = isnull(sum(gsc05f), 0) from gsm03 with (nolock) where gsc00c = @compid and gsc01c = @billid and gsc02c = 'ge' and gsc04c in ('1', '2', '6') select top 1 @pointclass = isnull(gya05i, 1), @pointvalues = isnull(gya06i, 0) from gym01 where gya00c = @compid and gya01c = @cardclass and @fillamt >= isnull(gya03f, 0) and @fillamt <= isnull(gya04f, 99999999) if @pointclass = 1 set @updatepointvalues = isnull(@pointvalues, 0) else if @pointclass = 2 or @pointclass = 3 begin set @updatepointvalues = isnull(@pointvalues, 0) * isnull(@fillamt, 0) / 100 end set @updatepointvalues = isnull(@updatepointvalues, 0) if (@confirm = 1) exec upg_purse_deduct @compid, @cardno, @curdate, 3, @updatepointvalues, 0, 'ge', @billid, @confirm else if (@confirm = 0) exec upg_purse_deduct @compid, @cardno, @curdate, 3, @updatepointvalues, 1, 'ge', @billid, @confirm end end GO /****** Object: StoredProcedure [dbo].[upg_ymn_send_1] Script Date: 06/12/2017 06:20:53 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_ymn_send_1]( @compid varchar(10), @billtype varchar(10), @billid varchar(20), @is_send_proc int, -- 赠送疗程换疗程 @send_times_rate float, -- 抵10送多少 @is_send_deps int, -- 是否赠送账户 @acctid varchar(10), -- 赠送账户 @sent_amt_rate float, @enddate varchar(20)) as begin declare @cardid varchar(20) declare @acctid_int int declare @new_billid varchar(20) declare @confirm varchar(20) declare @confirmdate varchar(20), @confirmtime varchar(20) declare @amt float declare @sales1 varchar(20) declare @ret int declare @version varchar(20) select @version = gsa02c from gsm01 where gsa01c = 'SP109' and gsa00c = @compid select @cardid = gcl03c, @confirmdate = gcl02d, @confirm = gcl93c, @sales1 = gcl09c, @confirmtime = gcl56t from gcm10 where gcl00c = @compid and gcl01c = @billid exec upg_check_brother_card_start @compid, @cardid, @ret output select @new_billid = @billid + '-1' --可能以前已经产生过则重新产生 if exists(select 1 from gcm10 where gcl00c = @compid and gcl01c = @new_billid) begin select @amt = gcl65f from gcm10 where gcl00c = @compid and gcl01c = @new_billid exec upg_confirm_gcm10_whole @compid, @new_billid, @confirm, @confirmdate, @acctid, @amt, @cardid, 0 delete gcm10 where gcl00c = @compid and gcl01c = @new_billid end select @acctid_int = convert(int, @acctid) if (@is_send_deps = 1) begin select @amt = sum(hei05f) from hem08 where hei00c = @compid and hei01c = @billid select @amt = round(isnull(@amt, 0)*isnull(@sent_amt_rate, 0), 0) end else begin select @amt = 0 end insert gcm10(gcl00c, gcl01c, gcl02d, gcl03c, gcl04c, gcl06i, gcl07f, gcl09c, gcl11c, gcl12c, gcl91c, gcl92d, gcl14i, gcl56t, gcl57c, gcl64f, gcl65f) values(@compid, @new_billid, @confirmdate, @cardid, @acctid, 0, 0, @sales1, @billtype, @billid, @confirm, @confirmdate, 0, @confirmtime, '促销赠送', @amt, @amt) if @is_send_proc = 1 begin insert gcm11(gcm00c, gcm01c, gcm02c, gcm03f, gcm09i, gcm10f, gcm11f, gcm13c, gcm06d) select @compid, @new_billid, hei04c, round(hei06f*isnull(@send_times_rate, 0), 0), 0, 1, 1, '促销赠送', @enddate from hem08 where hei00c = @compid and hei01c = @billid and isnull(hei06f, 0)>0 insert gcm11(gcm00c, gcm01c, gcm02c, gcm03f, gcm09i, gcm10f, gcm11f, gcm13c, gcm06d) select @compid, @new_billid, hej06c, round(hej08f*isnull(@send_times_rate, 0), 0), 0, 1, 1, '促销赠送', @enddate from hem09 where hej00c = @compid and hej01c = @billid and isnull(hej08f, 0)>0 end exec upg_confirm_gcm10_whole @compid, @new_billid, @confirm, @confirmdate, @acctid, @amt, @cardid, 1 --需要记录sysoperationlog这个记录 insert sysoperationlog(userid, program, operation, operationdate, operationtime, origatedate, compid, keyvalue1, keyvalue2, keyvalue3, keyvalue4) values('system', 'GMC01I', 'A', @confirmdate, @confirmtime, @confirmdate, @compid, @new_billid, @cardid, @send_times_rate, @sent_amt_rate) end GO /****** Object: StoredProcedure [dbo].[upg_trans_proc_and_combo] Script Date: 06/12/2017 06:20:47 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_trans_proc_and_combo] ( @compid varchar(10), @oldcardid varchar(20), @oldmode int, -- 0 不动 1 删除 @newcardid varchar(20), @newmode int, -- 0 保留 1 删除 @curdate varchar(10), @billtype varchar(10), @billid varchar(20) ) as begin --需要覆盖,则新卡需要先被删除 declare @defamt float -- 欠款的金额 declare @proc_acct int --疗程账户 declare @new_proc_def float -- 新卡也可能有欠款 select @proc_acct = 4 select @defamt = isnull(gcc10f, 0) from gcm03 with (nolock), gcm01 with(nolock) where gcc00c = gca00c and gca01c = gca01c and gca00c = gca13d and gcc01c = @oldcardid and gcc03i = @proc_acct select @new_proc_def = isnull(gcc10f, 0) from gcm03 with (nolock), gcm01 with(nolock) where gcc00c = gca00c and gcc01c = gca01c and gca00c = gca13d and gcc01c = @newcardid and gcc03i = @proc_acct if @newmode = 1 begin exec upg_log_copy_gcm0306 @compid, @newcardid, '变更' delete gcm06 where gcf01c = @newcardid delete gtc04 where gte01c = @newcardid delete gtc08 where gtj01c = @newcardid end --老卡的都需要转移过去 if @oldmode = 1 -- 老卡需要删除 begin --如果老的需要删除,则直接更新卡号就可以了 exec upg_log_copy_gcm0306 @compid, @oldcardid, '变更' update gcm06 set gcf01c = @newcardid, gcf00c = @compid where gcf01c = @oldcardid update gtc04 set gte01c = @newcardid, gte00c = @compid where gte01c = @oldcardid update gtc08 set gtj01c = @newcardid, gtj00c = @compid where gtj01c = @oldcardid end else -- 老卡不需要删除,则复制过去 begin declare @newcompid varchar(20) select @newcompid = gca13d from gcm01 where gca01c = @oldcardid insert gcm06(gcf00c, gcf01c, gcf02c, gcf03f, gcf04f, gcf05f, gcf06f, gcf07f, gcf08f, gcf09f, gcf10f, gcf11d, gcf12d, gcf14f, gcf17i, gcf18f, gcf19i, gcf20i, gcf21i, gcf22i, gcf25f) select @newcompid, @newcardid, gcf02c, gcf03f, gcf04f, gcf05f, gcf06f, gcf07f, gcf08f, gcf09f, gcf10f, gcf11d, gcf12d, gcf14f, gcf17i, gcf18f, gcf19i, gcf20i, gcf21i, gcf22i, gcf25f from gcm06 with (nolock) where gcf01c = @oldcardid insert gtc04(gte00c, gte01c, gte02c, gte03c, gte04f, gte05f, gte06f, gte08c) select @newcompid, @newcardid, gte02c, gte03c, gte04f, gte05f, gte06f, gte08c from gtc04 where gte01c = @oldcardid insert gtc08(gtj00c, gtj01c, gtj02c, gtj03c, gtj04f, gtj05f, gtj06f, gtj07c) select @newcompid, @newcardid, gtj02c, gtj03c, gtj04f, gtj05f, gtj06f, gtj07c from gtc08 where gtj01c = @oldcardid end --调整疗程账户的余额, 注意考虑欠款的因数 select @defamt = isnull(@defamt, 0) + isnull(@new_proc_def, 0) exec upg_reset_proc_acct_remain @compid, @newcardid, @curdate, @defamt, @billtype, @billid --记录操作历史 insert sysoperationlog(userid, program, operation, operationdate, operationtime, origatedate, compid, keyvalue1, keyvalue2, keyvalue3) values('system', 'GSB01I', 'M', @curdate, '', @curdate, @compid, @oldcardid, @newcardid, 'upg_trans_proc_and_combo') end GO /****** Object: StoredProcedure [dbo].[upg_Verify_ltm01] Script Date: 06/12/2017 06:20:52 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_Verify_ltm01] ( @gta00c varchar(10), @gta01c varchar(30), @gta93c varchar(10), @gta94d varchar(8), @time varchar(6) ) as begin set nocount on declare @__transaction_count int set @__transaction_count = @@trancount begin try if @__transaction_count = 0 begin tran else save tran __tran_upg_purse_deduct -- 更新发货主表 update ltm01 set gta93c = @gta93c, gta94d = @gta94d, gta06i = 1, gta12c = 5 where gta00c = @gta00c and gta01c = @gta01c declare @fmtstr varchar(30) --充值单号默认原则 declare @compid varchar(10) --门店编码 declare @gta04c varchar(30) --采购订单号 declare @gta15c varchar(20) --总部仓库代码 declare @gta20i int --发货类型 0 为仓库发货,1为供应商发货 declare @gta21c varchar(60) --快递公司名 declare @gta22c varchar(60) -- 快递单号 select @compid = gta03c, @gta04c = gta04c, @gta15c = gta15c, @gta20i = gta20i, @gta21c = gta21c, @gta22c = gta22c from ltm01 where gta00c = @gta00c and gta01c = @gta01c -- 门店收货单号 declare @gva01c nvarchar(20) select @fmtstr = gsa02c from gsm01 where gsa00c = @compid and gsa01c = 'SPD34' --来源 declare @gva07i int declare @gva08c varchar(20) --送货单位编号 declare @gva11c varchar(60) --快递名称 if @gta20i = 1 begin set @gva07i = 4 --快递 set @gva08c = @gta22c set @gva11c = @gta21c end else begin set @gva07i = 0 --总部仓 set @gva08c = @gta15c set @gva11c = '' end exec upg_autokey @gta94d, 'lvm01', 'gva01c', @fmtstr, @compid, 'gva00c', @gva01c output -- 收货主表 insert into lvm01(gva00c, gva01c, gva02d, gva03c, gva04c, gva05c, gva06c, gva07i, gva08c, gva09i, gva10t,gva11c, gva93c, gva94d) values(@compid, @gva01c, '', '', '', @gta01c, @gta04c, @gva07i, @gva08c, 0, @time,@gva11c, '', '') -- 收货明细 0-正常, 1-赠送 insert into lvm02(gvb00c, gvb01c, gvb02f, gvb03c, gvb04f, gvb05f, gvb06f, gvb07f, gvb08f, gvb09c, gvb10f, gvb11f, gvb12f, gvb13i) select @compid, @gva01c, row_number() over(order by gtb02f), gtb03c, 0, gtb08f, 0, 0, gtb07f, gtb05c, gtb12f, gtb11f, gtb10f, gtb13i from ltm02 where gtb00c = @gta00c and gtb01c = @gta01c and gtb08f > 0 -- 修改采购从表状态 :完整发货 update lrm02 set grb10c = 5 where grb00c = @compid and grb01c = @gta04c and grb03c in (select gtb03c from ltm02 where gtb00c = @gta00c and gtb01c = @gta01c and gtb07f = gtb08f) declare @gra04c int select @gra04c = convert(int, gra04c) from lrm01 where gra00c = @compid and gra01c = @gta04c -- 修改采购主表状态 if (isnull(@gra04c, 0) = 2) update lrm01 set gra04c = 4, gra12c = @gta01c where gra00c = @compid and gra01c = @gta04c else update lrm01 set gra04c = 5, gra12c = @gta01c where gra00c = @compid and gra01c = @gta04c -- 欠货 if exists(select 1 from ltm02 where gtb00c = @gta00c and gtb01c = @gta01c and gtb07f > gtb08f) begin insert into lrm03(grc00c, grc01c, grc02c, grc03c, grc04f, grc05c, grc06f, grc07i, grc08i, grc09f, grc10c) select @gta00c, @compid, @gta04c, gtb03c, gtb07f - gtb08f, gtb05c, 0, 0, 1, 0, '' from ltm02 where gtb00c = @gta00c and gtb01c = @gta01c and gtb07f > gtb08f -- 修改采购从表状态 :部分发货 update lrm02 set grb10c = 4 where grb00c = @compid and grb01c = @gta04c and grb03c in (select gtb03c from ltm02 where gtb00c = @gta00c and gtb01c = @gta01c and gtb07f > gtb08f) -- 如果存在部分发货,则修改主表为4 update lrm01 set gra04c = 4, gra12c = @gta01c where gra00c = @compid and gra01c = @gta04c -- 更新发货主表 update ltm01 set gta12c = 4 where gta00c = @gta00c And gta01c = @gta01c end if (@gta20i = 0) --仓库发货则出库 begin declare @gia01c nvarchar(30) select @fmtstr = gsa02c from gsm01 where gsa00c = @compid and gsa01c = 'SPD21' --发货单审核失败BUG add by ydy 2015-10-13 --exec upg_autokey @gta94d, 'gim01', 'gia01c', @fmtstr, @compid, 'gia00c', @gia01c output exec upg_autokey @gta94d, 'gim01', 'gia01c', @fmtstr, @gta00c, 'gia00c', @gia01c output --sp058 3产品出库价默认1进货价 gfa22f 2 销售成本 gfa19f 3标准价gfa11f 4 销售成本(门店)+标准价(员工), 5 月加权 declare @sp058 int select @sp058 = isnull(gsa03f,1) from gsm01 where gsa00c = @gta00c and gsa01c = 'sp058' -- 出库主表 @compid insert into gim01(gia00c, gia01c, gia03d, gia04c, gia05c, gia06c, gia07i, gia08t, gia09i, gia10c, gia11c, gia91c, gia92d, gia93c, gia94d) values(@gta00c, @gia01c, @gta94d, @gta15c, @compid, '', 1, @time, 2, '', @gva01c, @gta93c, @gta94d, '', '') -- 出库从表 insert into gim02(gib00c, gib01c, gib02f, gib03c, gib04c, gib05f, gib06c, gib07f, gib08f, gib09f, gib10f, gib11f, gib12c) select @gta00c, @gia01c, row_number() over(order by gtb02f), gtb03c, '', 0, gtb05c, gtb08f, gtb08f, case @sp058 when 1 then gfa22f when 2 then gfa19f when 3 then gfa11f when 5 then (select gfh03f from gfm08 where gfh00c = @gta00c and gfh01c = gtb03c and gfh02c = substring(@gta94d,1,6)) else gfa22f end,0,gtb11f,'' -- gtb10f, gtb06f, gtb11f, '' from ltm02 inner join gfm01 on gfa00c = @gta00c and gfa01c = gtb03c where gtb00c = @gta00c and gtb01c = @gta01c and gtb08f > 0 --更新金额 if(exists(select 1 from gim02 where gib00c = @gta00c and gib01c = @gia01c)) begin update gim02 set gib10f = isnull(gib09f,0) * isnull(gib07f,0) where gib00c = @gta00c and gib01c = @gia01c end -- 出库审核 exec upg_ghm01_confirm @gta00c, @gia01c, @gta94d, @gta15c, @gta93c, @gta94d, 1, 2 --生成的入库单号 --@billid + '(' + @compid + '-' + @incompid + ')' declare @incompid varchar(30) set @incompid = @gia01c+ '(' + @gta00c + '-' + @compid + ')' delete from ghm01 where gha00c = @compid and gha01c = @incompid delete from gfm04 where gfd00c = @compid and gfd01c = 1 and gfd02c= @incompid --库存日异动 end if xact_state() = 1 and @__transaction_count = 0 commit if @__transaction_count = 0 begin if xact_state() = -1 rollback tran else begin while @@trancount > 0 commit tran end end end try begin catch if xact_state() <> 0 begin if @__transaction_count = 0 rollback tran else if xact_state() = 1 and @@trancount > @__transaction_count rollback tran __tran_upg_purse_deduct end declare @SP_Parameters varchar(max) set @SP_Parameters = '@gta00c = ' + isnull(@gta00c, '') + '@gta01c = ' + isnull(@gta01c, '') + '@gta93c = ' + isnull(@gta93c, '') + '@gta94d = ' + isnull(@gta94d, '') + '@time = ' + isnull(@time, '') insert into SP_TranLog(SPName, SPTime, SPParameter) values('upg_Verify_ltm01', getdate(), @SP_Parameters) return -1 end catch end GO /****** Object: StoredProcedure [dbo].[upg_rewrite_verp] Script Date: 06/12/2017 06:20:38 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_rewrite_verp] @compid varchar(10), -- 公司编号 @billid varchar(20), -- 单号 @billtype varchar(5), -- 单据类别 'gn' = 会员卡销售 'gz' = 会员帐户管理 @iconfirm int as begin exec upg_rewrite_verp_ex @compid, @billid, @billtype, @iconfirm end GO /****** Object: StoredProcedure [dbo].[upg_send_gift_prjs] Script Date: 06/12/2017 06:20:40 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_send_gift_prjs] ( @compid varchar(10), @billid varchar(20) ) as begin declare @cardid varchar(20) select @cardid = gga09c from ggm01 where gga00c = @compid and gga01c = @billid if substring(@cardid, 1, 2) = '散客' begin return end declare @curdate varchar(20) declare @time varchar(20) exec upg_getcurrdate @curdate output, @time output create table #tbl_gift_prjs ( cardid varchar(20) not null, prjid varchar(20) null, times int null, goodid varchar(20) null, -- 购买产品的编号 billid varchar(20) null, -- 收银单号 ggm03seq float null, -- 产品购买流水号 cardnum float null, -- 疗程的卡数 = 产品的数量 procid int null, -- 疗程序号 isreturn int null, -- 是否为退产品,退产品需要退赠送的疗程 validitydate varchar(10) null -- 赠送疗程有效期 ) insert #tbl_gift_prjs(cardid, prjid, times, goodid, billid, ggm03seq, cardnum, validitydate) select gga09c, gfa46c, gfa45i, ggc03c, gga01c, ggc02f, ggc05f, (case when isnull(gfa51c, '') = '1' then Convert(varchar(10), DATEADD(year, gfa50i, getdate()), 120) when isnull(gfa51c, '') = '2' then Convert(varchar(10), DATEADD(month, gfa50i, getdate()), 120) when isnull(gfa51c, '') = '3' then Convert(varchar(10), DATEADD(day, gfa50i, getdate()), 120) when isnull(gfa51c, '') = '4' then Convert(varchar(10), DATEADD(month, gfa50i * 3, getdate()), 120) else '2090-12-31' end) from ggm01 c with (nolock), ggm03 a with (nolock), gfm01 b with (nolock) where c.gga00c = a.ggc00c and c.gga01c = a.ggc01c and a.ggc00c = @compid and a.ggc01c = @billid and a.ggc00c = b.gfa00c and a.ggc03c = b.gfa01c and isnull(b.gfa45i, 0) > 0 and isnull(b.gfa46c, '') <> '' -- 设置退产品标志 update a set a.isreturn = 1 from #tbl_gift_prjs a where isnull(cardnum, 0) < 0 update a set validitydate = substring(isnull(validitydate, ''), 1, 4) + substring(isnull(validitydate, ''), 6, 2) + substring(isnull(validitydate, ''), 9, 2) from #tbl_gift_prjs a -- 根据卡号获取归属公司: declare @ownercompid nvarchar(10) -- 卡的归属公司 select @ownercompid = min(gca13d) from gcm01 with (nolock) where gca01c = @cardid and gca00c = gca13d -- 可能赠送的项目明细,但需要考虑可能这个单据已经赠送的项目 create table #gcm06_buf_sent -- 会员卡--疗程 ( gcf00c varchar(10) not null, -- 公司编号 gcf01c varchar(20) not null, -- 卡号 gcf02c varchar(20) not null, -- 项目编号 gcf03f float null, -- 次数 gcf04f float null, -- 赠送次数 gcf05f float null, -- 总次数 gcf06f float null, -- 已经使用次数 gcf07f float null, -- 剩余次数 gcf08f float null, -- 疗程金额 gcf09f float null, -- 已经使用金额 gcf10f float null, -- 剩余金额 gcf11d varchar(8) null, -- 账户开立日 gcf12d varchar(8) null, -- 账户截至日 gcf13i int null, -- 疗程间隔 gcf14f float null, -- 一个月可使用的次数 0--代表不控制 直到这个疗程用完其他float就是一个月可以使用的次数 gcf15c varchar(60) null, -- 输入的时候的备注 gcf16i int null, -- 是否已经审核 gcf17i int null, -- 价格序号 gcf18f float null, -- 折扣 gcf19i int null, -- 是否停用 2停用 1正常 3买产品赠送项目 gcf20i int null, -- 标志 是否前期卡 gcf21i int null, -- 是否赠送 gcf22i int null, -- 永久免费 gcf23i int null, -- 序号 gcf24i int null, -- 开帐中的序号(gcm16.gcr23i) gcf25f float null -- 虚假次数(只有增加部分) ) create clustered index idx_sent_001 on #gcm06_buf_sent(gcf02c, gcf15c) -- 准备赠送的项目 create table #gcm06_buf_prepare -- 会员卡--疗程 ( gcf00c varchar(10) not null, --公司编号 gcf01c varchar(20) not null, --卡号 gcf02c varchar(20) not null, --项目编号 gcf03f float null, --次数 gcf04f float null, --赠送次数 gcf05f float null, --总次数 gcf06f float null, --已经使用次数 gcf07f float null, --剩余次数 gcf08f float null, --疗程金额 gcf09f float null, --已经使用金额 gcf10f float null, --剩余金额 gcf11d varchar(8) null, --账户开立日 gcf12d varchar(8) null, --账户截至日 gcf13i int null, --疗程间隔 gcf14f float null, --一个月可使用的次数 0--代表不控制 直到这个疗程用完其他float就是一个月可以使用的次数 gcf15c varchar(60) null, --输入的时候的备注 gcf16i int null, --是否已经审核 gcf17i int null, --价格序号 gcf18f float null, --折扣 gcf19i int null, --是否停用 2停用 1正常 3买产品赠送项目 gcf20i int null, --标志 是否前期卡 gcf21i int null, --是否赠送 gcf22i int null, --永久免费 gcf23i int null, --序号 gcf24i int null, --开帐中的序号(gcm16.gcr23i) gcf25f float null --虚假次数(只有增加部分) ) create clustered index idx_prepare_001 on #gcm06_buf_prepare(gcf02c, gcf15c) declare @memo varchar(30) select @memo = @billid + '%' -- 此单据可能已经赠送,并且消费的赠送, 如果只是已经赠送,没有消费,则已经被删除了 insert #gcm06_buf_sent(gcf00c, gcf01c, gcf02c, gcf03f, gcf04f, gcf05f, gcf06f, gcf07f, gcf08f, gcf09f, gcf10f, gcf11d, gcf12d, gcf15c, gcf17i, gcf18f, gcf19i, gcf20i, gcf21i, gcf22i) select gcf00c, gcf01c, gcf02c, gcf03f, gcf04f, gcf05f, gcf06f, gcf07f, gcf08f, gcf09f, gcf10f, gcf11d, gcf12d, gcf15c, gcf17i, gcf18f, gcf19i, gcf20i, gcf21i, gcf22i from gcm06 a with (nolock) where gcf00c = @ownercompid and gcf19i = 3 and gcf15c like @memo and gcf01c = @cardid insert #gcm06_buf_prepare(gcf00c, gcf01c, gcf02c, gcf03f, gcf04f, gcf05f, gcf06f, gcf07f, gcf08f, gcf09f, gcf10f, gcf11d, gcf12d, gcf15c, gcf17i, gcf18f, gcf19i, gcf20i, gcf21i, gcf22i) select @ownercompid, cardid, prjid, 0, convert(decimal(20, 0), times*isnull(cardnum, 1)), convert(decimal(20, 0), times*isnull(cardnum, 1)), 0, convert(decimal(20, 0), times*isnull(cardnum, 1)), 0, 0, 0, @curdate, validitydate, billid + '-' + goodid + '-' + convert(varchar(20), ggm03seq), 0, 1, 3, 0, 1, 0 from #tbl_gift_prjs where isnull(isreturn, 0) = 0 delete a from #gcm06_buf_prepare a, #gcm06_buf_sent b where a.gcf02c = b.gcf02c -- 项目 and a.gcf03f = b.gcf03f and a.gcf04f = b.gcf04f and a.gcf05f = b.gcf05f and substring(a.gcf15c, 1, 19) = substring(b.gcf15c, 1, 19) -- YMN的情况,同一个单据同一个产品 insert gcm06(gcf00c, gcf01c, gcf02c, gcf03f, gcf04f, gcf05f, gcf06f, gcf07f, gcf08f, gcf09f, gcf10f, gcf11d, gcf12d, gcf15c, gcf17i, gcf18f, gcf19i, gcf20i, gcf21i, gcf22i) select gcf00c, gcf01c, gcf02c, gcf03f, gcf04f, gcf05f, gcf06f, gcf07f, gcf08f, gcf09f, gcf10f, gcf11d, gcf12d, gcf15c, gcf17i, gcf18f, gcf19i, gcf20i, gcf21i, gcf22i from #gcm06_buf_prepare drop table #gcm06_buf_sent create table #gcm06_buf -- 会员卡--疗程 ( gcf00c varchar(10) not null, --公司编号 gcf01c varchar(20) not null, --卡号 gcf02c varchar(20) not null, --项目编号 gcf03f float null, --次数 gcf04f float null, --赠送次数 gcf05f float null, --总次数 gcf06f float null, --已经使用次数 gcf07f float null, --剩余次数 gcf08f float null, --疗程金额 gcf09f float null, --已经使用金额 gcf10f float null, --剩余金额 gcf11d varchar(8) null, --账户开立日 gcf12d varchar(8) null, --账户截至日 gcf13i int null, --疗程间隔 gcf14f float null, --一个月可使用的次数 0--代表不控制 直到这个疗程用完其他float就是一个月可以使用的次数 gcf15c varchar(60) null, --输入的时候的备注 gcf16i int null, --是否已经审核 gcf17i int null, --价格序号 gcf18f float null, --折扣 gcf19i int null, --是否停用 2停用 1正常 3买产品赠送项目 gcf20i int null, --标志 是否前期卡 gcf21i int null, --是否赠送 gcf22i int null, --永久免费 gcf23i int null, --序号 gcf24i int null, --开帐中的序号(gcm16.gcr23i) gcf25f float null --虚假次数(只有增加部分) ) create clustered index idx_#gcm06_buf_01 on #gcm06_buf(gcf00c,gcf01c,gcf02c) --退的疗程 insert #gcm06_buf(gcf00c, gcf01c, gcf02c, gcf03f, gcf04f, gcf05f, gcf06f, gcf07f, gcf08f, gcf09f, gcf10f, gcf11d, gcf12d, gcf15c, gcf17i, gcf18f, gcf19i, gcf20i, gcf21i, gcf22i) select @ownercompid, cardid, prjid, 0, convert(decimal(20, 0), times*isnull(cardnum, 1)), convert(decimal(20, 0), times*isnull(cardnum, 1)), 0, convert(decimal(20, 0), times*isnull(cardnum, 1)), 0, 0, 0, @curdate, validitydate, billid + '-' + goodid + '-' + convert(varchar(20), ggm03seq), 0, 1, 3, 0, 1, 0 from #tbl_gift_prjs where isnull(isreturn, 0) = 1 -- 退产品的 --需要把退的疗程给覆盖过去 --把可能对应的序号找出来 update a set a.gcf23i = b.gcf23i from #gcm06_buf a, gcm06 b where a.gcf00c = b.gcf00c and a.gcf01c = b.gcf01c and a.gcf02c = b.gcf02c and 0- a.gcf04f = b.gcf04f and b.gcf19i = 3 --抵扣已经赠送的疗程 update a set a.gcf04f = a.gcf04f + b.gcf04f, a.gcf05f = a.gcf05f + b.gcf05f, a.gcf07f = a.gcf07f + b.gcf07f from gcm06 a, #gcm06_buf b where a.gcf00c = b.gcf00c and a.gcf01c = b.gcf01c and a.gcf02c = b.gcf02c and a.gcf23i = b.gcf23i and a.gcf04f = 0 - b.gcf04f and a.gcf19i = 3 drop table #gcm06_buf update a set a.procid = b.gcf23i from #tbl_gift_prjs a, gcm06 b where a.cardid = b.gcf01c and a.prjid = b.gcf02c and b.gcf00c = @ownercompid and convert(decimal(20, 0), a.times*isnull(a.cardnum, 1)) = b.gcf07f and b.gcf15c = billid + '-' + goodid + '-' + convert(varchar(20), ggm03seq) and isnull(isreturn, 0) = 0 --只要把本次增加到gcm06中的疗程才需要插入到疗程历史中 insert gcm08(gch00c, gch02d, gch03c, gch04c, gch05f, gch06f, gch07f, gch08f, gch09f, gch10c, gch11c, gch12i, gch13f, gch14f, gch15f, gch16i, gch24c, gch25i, gch26f) select @compid, @curdate, cardid, prjid, 0, times, 0, 0, 0, 'gx', @billid, 0, 1, cardnum, 0, procid, goodid, 2, ggm03seq from #tbl_gift_prjs a, #gcm06_buf_prepare b where isnull(isreturn, 0) = 0 and a.cardid = b.gcf01c and a.prjid = b.gcf02c and a.times*isnull(cardnum, 1) = b.gcf04f and b.gcf15c = billid + '-' + goodid + '-' + convert(varchar(20), ggm03seq) drop table #gcm06_buf_prepare drop table #tbl_gift_prjs declare @proc_acct int select @proc_acct = 4 if not exists(select 1 from gcm03 with (nolock) where gcc00c = @ownercompid and gcc01c = @cardid and gcc03i = @proc_acct) begin if isnull(@cardid, '')<>'' begin exec upg_add_money_ex @compid, @cardid, 0, 0, 1, @proc_acct end end end GO /****** Object: StoredProcedure [dbo].[upg_Confirm_CardChange] Script Date: 06/12/2017 06:19:44 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_Confirm_CardChange] ( @compid varchar(10), @billid varchar(20), -- 单号 @changetype int, @currdate varchar(8), @userid varchar(10), @oldcardno varchar(20), -- 会员卡销售的时候,这个参数是卡号 @confirm int, @newcardno varchar(20) -- 开卡的时候,这个栏位是会员编号 ) as-- wuxinming, 2004/12/17 begin declare @ret int if (@confirm = 1) begin if (@changetype = 99) -- 销售 begin -- 设置Confirm标志 exec upg_set_confirm_state @compid, 'gn', @billid, @userid, @currdate, @oldcardno update a set gca08i = 2, gca05d = gna02d, gca19f = gnb07f, gca20c = gna20c, gca11c = @billid, gca21d = gna23d, gca22d = gna24d, gca06d = '', gca07d = '', gca16d = '', gca17d = '' --add by wuxm 2002/11/25 已过期的会员卡可以再销售,更新会员卡资料时需要把原来的开卡等信息置为空 from gcm01 a, gnm01 b, gnm02 c where a.gca00c = a.gca13d and a.gca01c = c.gnb03c and b.gna00c = c.gnb00c and b.gna01c = c.gnb01c and b.gna00c = @compid and b.gna01c = @billid -- 卡销售的时候增加充值账户 -- Added by WUXM, 2006/03/11 -- 卡销售的时候也应该写账户的异动历史,这样更加合理,统计gcm04的时候比较完备 -- 因为在没有销售之前, 卡里有钱, 所以需要先将卡中的钱清空,否则就会多一倍了。 update a set a.gcc06f = 0 from gcm03 a, gnm02 b, gcm01 c where a.gcc00c = c.gca00c and c.gca00c = c.gca13d and a.gcc01c = c.gca01c and a.gcc01c = b.gnb03c and b.gnb00c = @compid and b.gnb01c = @billid and a.gcc03i = b.gnb13i declare @gnm_cardno varchar(20) declare @gnm_amt float declare @gnm_accttype int declare cur_each_gnm02 cursor for select gnb03c, gnb11f, gnb13i from gnm02 where gnb00c = @compid and gnb01c = @billid open cur_each_gnm02 fetch cur_each_gnm02 into @gnm_cardno, @gnm_amt, @gnm_accttype while @@fetch_status = 0 begin exec upg_purse_deduct @compid, @gnm_cardno, @currdate, @gnm_accttype, @gnm_amt, 0, 'gn', @billid, 1 fetch cur_each_gnm02 into @gnm_cardno, @gnm_amt, @gnm_accttype end close cur_each_gnm02 deallocate cur_each_gnm02 -- 添加异动信息 insert into gcm02(gcb00c, gcb01c, gcb02f, gcb03i, gcb04c, gcb05i, gcb06i, gcb07d) select gnb00c, gnb03c, (select isnull(max(gcb02f), 0) + 1 from gcm02 with (nolock) where gcb00c = @compid and gcb01c = gnb03c), 99, gna01c, 1, iState = case gca14i when '1' then 4 else 2 end, gna02d from gnm01 with (nolock), gnm02 with (nolock), gcm01 with(nolock) where gna00c = @compid and gna01c = @billid and gnb00c = gna00c and gnb01c = gna01c and gca00c = gca13d and gca01c = gnb03c exec upg_conv_procamt_card_bill @compid, @billid -- 设置帐务日期 WUXM 2005/08/26 exec upg_updategnm01_accountdate @compid, @billid -- 自动设置疗程购买的支付方式 exec upg_create_buyproc_payment @compid, 'gn', @billid -- Added by WUXM, 2006/09/18 -- 卡销售的时候,写异动历史 exec upg_create_gcm20_from_one_bill @compid, 'gn', @billid, 1 -- 会员卡销售的时候套餐的产品出库 -- 在购买套餐的过程中处理了,这里就不用处理了 end else begin update gem01 set gea95c = @userid, gea96d = @currdate where gea00c = @compid and gea01c = @billid and gea02i = @changetype if (@changetype = 2)--开卡 begin declare @memid varchar(20) -- 会员开卡的时候,过程参数中的最后一个参数为会员编号 select @memid = @newcardno update gcm01 set gca08i = gea06i, gca04c = gea10c, gca06d = gea03d, gca07d = gea09d from gcm01, gem01 where gca00c = gca13d and gca01c = gea07c and gea00c = @compid and gea01c = @billid -- 更新卡的储值账户的有效期 update a set gcc09d = gca07d, gcc08c = isnull(gcc08c, gca06d) from gcm03 a, gcm01 b where a.gcc00c = b.gca00c and a.gcc01c = b.gca01c and a.gcc03i = 2 and b.gca00c = b.gca13d and a.gcc01c = @oldcardno update gbm01 set gba23c = @oldcardno from gbm01 where gba01c = @memid -- 会员开卡的时候,如果会员的负责业务为空,则设置卡的销售人员就是会员的负责业务 update gbm01 set gba10c = gca20c from gbm01, gcm01 where gba00c = gca00c and gba23c = gca01c and gca01c = @oldcardno and isnull(gba10c, '') = '' -- Added by WUXM, end exec upg_updategem01_accountdate @compid, @billid end end if (@changetype = 3)--延期 begin update gcm01 set gca07d = gea11d, gca08i = 5 from gcm01, gem01 where gca00c = gca13d and gca01c = gea04c and gea00c = @compid and gea01c = @billid update gem01 set gea06i = 5 where gea00c = @compid and gea01c = @billid and gea02i = @changetype -- 设置帐务日期 WUXM 2005/08/26 exec upg_updategem01_accountdate @compid, @billid end if (@changetype = 4)--卡种变更 begin declare @oldcard_remain float declare @newcard_remain float declare @oldcard_remain_def float -- 欠款 declare @newcard_remain_def float -- 欠款 declare @pay_fee float -- 更新原卡信息 update gcm01 set gca08i = 6 from gcm01 where gca00c = gca13d and gca01c = @oldcardno -- 更新新卡信息 declare @memid_cor varchar(20) select @memid_cor = min(gba01c) from gbm01 where gba23c = @oldcardno if isnull(@memid_cor, '') = '' begin select @memid_cor = min(gba01c) from gbm01 where gba23c = @newcardno end -- 如果此卡是卡种变更且未开卡,默认把当前卡账户余额清0 -- 若卡为未销售,清除储值账户余额 declare @gca08i int select @gca08i = gca08i from gcm01 where gca01c = @newcardno if(isnull(@gca08i,0) = 1) begin update gcm03 set gcc06f = 0 where gcc01c = @newcardno and gcc03i = 2 end update gcm01 set gca04c = case when isnull(@memid_cor, '') != '' then @memid_cor else gca04c end, gca05d = gea03d, gca06d = gea15d, gca07d = gea16d, gca08i = 4, gca19f = gea20f from gcm01, gem01 where gca00c = gca13d and gca01c = gea07c and gea00c = @compid and gea01c = @billid -- 新卡号加异动档 insert gcm02(gcb00c, gcb01c, gcb02f, gcb03i, gcb04c, gcb05i, gcb06i, gcb07d, gcb08c) select gea00c, gea07c, (select isnull(max(gcb02f), 0) + 1 from gcm02 with (nolock) where gcb01c = @newcardno), gea02i, gea01c, 1, 4, gea03d, gea04c from gem01 where gea00c = @compid and gea01c = @billid -- 如果新卡是没有开的卡, 则新卡是没有会员资料的 if not exists(select 1 from gbm01 where gba00c = @compid and gba23c = @newcardno) begin update gbm01 set gba23c = @newcardno from gbm01 where gba23c = @oldcardno end else begin -- 如果新卡老卡都有会员资料, 则老卡的会员资料必须不与老卡关联 update gbm01 set gba23c = '' from gbm01 where gba23c = @oldcardno end -- 取得老卡的余额, 储值账户 select @oldcard_remain = gcc06f, @oldcard_remain_def = isnull(gcc10f, 0) from gcm03 a with (nolock), gcm01 with(nolock) where a.gcc00c = gca00c and a.gcc01c = gca01c and gcc00c = gca13d and a.gcc01c = @oldcardno and a.gcc03i = 2 -- 取得新卡的余额, 储值账户 select @newcard_remain = gcc06f, @newcard_remain_def = isnull(gcc10f, 0) from gcm03 a with (nolock), gem01 b, gcm01 c where a.gcc00c = c.gca00c and a.gcc01c = c.gca01c and c.gca00c = c.gca13d and a.gcc01c = b.gea07c and b.gea00c = @compid and b.gea01c = @billid and a.gcc03i = 2 and b.gea02i = 4 -- 客户支付的金额 select @pay_fee = sum(gsc05f) from gsm03 where gsc00c = @compid and gsc01c = @billid and gsc02c = 'ge' declare @adjust_amt float declare @adjust_amt_def float declare @isadd varchar(20) select @isadd = gsa02c from gsm01 with (nolock) where gsa00c = @compid and gsa01c = 'SP004' if isnull(@isadd, '') = '1' begin select @adjust_amt = isnull(@oldcard_remain, 0) + isnull(@pay_fee, 0) select @adjust_amt_def = isnull(@oldcard_remain_def, 0) end else begin select @adjust_amt = isnull(@oldcard_remain, 0) - isnull(@newcard_remain, 0) + isnull(@pay_fee, 0) select @adjust_amt_def = isnull(@oldcard_remain_def, 0) - isnull(@newcard_remain_def, 0) end exec upg_add_money_expend @compid, @newcardno, @adjust_amt, @adjust_amt_def, 1, 2, 'ge', @billid update gcm03 set gcc06f = 0 where gcc01c = @oldcardno and gcc03i = 2 -- 转移其他账户的余额 if isnull(@isadd, '') = '1' -- 累加的模式 begin exec upg_trans_other_acct_remain @compid, @oldcardno, 1, @newcardno, 0, @currdate, 'ge', @billid end else -- 替换的模式 begin exec upg_trans_other_acct_remain @compid, @oldcardno, 1, @newcardno, 1, @currdate, 'ge', @billid end -- 将老卡的疗程资料以及套餐转移到新卡上 if isnull(@isadd, '') = '1' --累加的模式 begin exec upg_trans_proc_and_combo @compid, @oldcardno, 1, @newcardno, 0, @currdate, 'ge', @billid end else -- 替换的模式 begin exec upg_trans_proc_and_combo @compid, @oldcardno, 1, @newcardno, 1, @currdate, 'ge', @billid end -- 修正会员卡开张资料中的会员卡,从老卡修改为新卡 -- Modify by liy 2011-03-21 declare @cardid1 varchar(20) declare @cardid2 varchar(20) set @cardid1 = '' set @cardid2 = '' select @cardid1 = gcn01c from gcm12 where gcn01c = @oldcardno select @cardid2 = gcn01c from gcm12 where gcn01c = @newcardno if (isnull(@cardid1, '') = '' or isnull(@cardid2, '') = '') begin update gcm12 set gcn01c = @newcardno where gcn01c = @oldcardno end update gcm16 set gcr01c = @newcardno where gcr01c = @oldcardno update gbm01 set gba23c = @newcardno where gba23c = @oldcardno -- 设置帐务日期 WUXM 2005/08/26 exec upg_updategem01_accountdate @compid, @billid end if (@changetype = 5) -- 会籍保留 begin update gcm01 set gca07d = gea11d, gca16d = gea15d, gca17d = gea16d from gcm01, gem01 where gca00c = gca13d and gca01c = gea04c and gea00c = @compid and gea01c = @billid -- 将异动日期与保留起始日期一致的卡转为保留状态 update gcm01 set gca08i = 10 from gcm01, gem01 where gca00c = gca13d and gca01c = gea04c and gea00c = @compid and gea01c = @billid and gea15d = gea03d -- 设置帐务日期 WUXM 2005/08/26 exec upg_updategem01_accountdate @compid, @billid end if (@changetype = 6) -- 会籍转让 begin update gcm01 set gca04c = gea10c from gcm01, gem01 where gca00c = gca13d and gca01c = gea04c and gea00c = @compid and gea01c = @billid -- 把旧的会员编号中的卡号设置为空 update gbm01 set gba23c = '' from gbm01 where gba23c = @oldcardno -- 设置新的会员编号的卡号 update gbm01 set gba23c = gea04c from gbm01, gem01 where gba01c = gea10c and gea00c = @compid and gea01c = @billid -- 设置帐务日期 WUXM 2005/08/26 exec upg_updategem01_accountdate @compid, @billid end if (@changetype = 7) -- 遗失补卡 begin -- 将老卡的状态改变为挂失 update gcm01 set gca08i = 9 from gcm01, gem01 where gca00c = gca13d and gca01c = gea04c and gea00c = @compid and gea01c = @billid -- 更新新卡的会员信息 update gcm01 set gca04c = (select gca04c from gcm01 where gca00c = gca13d and gca01c = @oldcardno), gca05d = (select gca05d from gcm01 where gca00c = gca13d and gca01c = @oldcardno), gca06d = (select gca06d from gcm01 where gca00c = gca13d and gca01c = @oldcardno), gca07d = (select gca07d from gcm01 where gca00c = gca13d and gca01c = @oldcardno), gca12c = (select gca12c from gcm01 where gca00c = gca13d and gca01c = @oldcardno), --消费密码 gca18c = (select gca18c from gcm01 where gca00c = gca13d and gca01c = @oldcardno), --查询密码 gca08i = 4, gca19f = gea20f from gcm01, gem01 where gca00c = gca13d and gca01c = gea07c and gea00c = @compid and gea01c = @billid insert into gcm02(gcb00c, gcb01c, gcb02f, gcb03i, gcb04c, gcb05i, gcb06i, gcb07d) select gea00c, gea07c, (select isnull(max(gcb02f), 0) + 1 from gcm02 with (nolock) where gcb00c = @compid and gcb01c = @newcardno), gea02i, gea01c, 1, 4, gea03d from gem01 where gea00c = @compid and gea01c = @billid -- 所有分公司的这个会员的卡号都要更新 update gbm01 set gba23c = gea07c from gbm01, gem01 where gea00c = @compid and gba23c = @oldcardno and gea01c = @billid -- 将除储值账户之外的会员卡账户中的钱全部转移到新卡上 exec upg_trans_other_acct_remain @compid, @oldcardno, 1, @newcardno, 1, @currdate, 'ge', @billid -- Added by WUXM, 2004/12/04 -- 将老卡的疗程资料转移到新卡上 exec upg_trans_proc_and_combo @compid, @oldcardno, 1, @newcardno, 1, @currdate, 'ge', @billid -- 转移完疗程之后需要重新统计疗程的余额 -- 设置帐务日期 WUXM 2005/08/26 exec upg_updategem01_accountdate @compid, @billid --begin add lucz 2016.05.24 -- 转移卡的金额 exec upg_trans_card_money @compid, @oldcardno, @newcardno, 2, 'ge', @billid declare @gea08c varchar(20) declare @gea14i int select @gea08c = gea08c, @gea14i = gea14i from gem01 where gea00c = @compid and gea01c = @billid and gea02i = 7 --如果需要更改会员编号,则执行 if(rtrim(ltrim(@gea08c)) = '1') exec upg_correct_memberid @newcardno --是否转移会员卡的历史 if(@gea14i = 1 ) exec upg_trans_history @oldcardno,@newcardno --转移会员卡抵用券 update gam08 set gah10c = @newcardno where gah10c = @oldcardno and gah06i = 0 update gam08 set gah10c = @newcardno,gah16c = @newcardno where gah10c = @oldcardno and gah06i = 1 --end add lucz 2016.05.24 end if (@changetype = 8) -- 退卡 begin update gcm01 set gca08i = 7 from gcm01, gem01 where gca00c = gca13d and gca01c = gea04c and gea00c = @compid and gea01c = @billid update gbm01 set gba23c = '' from gbm01, gcm05 where gba01c = gce02c and gce00c = @compid and gce01c = @oldcardno -- 设置帐务日期 WUXM 2005/08/26 exec upg_updategem01_accountdate @compid, @billid end -- 更新异动历史档 insert into gcm02(gcb00c, gcb01c, gcb02f, gcb03i, gcb04c, gcb05i, gcb06i, gcb07d) select gea00c, gea04c, (select isnull(max(gcb02f), 0) + 1 from gcm02 with (nolock) where gcb00c = @compid and gcb01c = @oldcardno), gea02i, gea01c, gea05i, gea06i, gea03d from gem01 where gea00c = @compid and gea01c = @billid and isnull(gea04c, '') <> '' and isnull(gea04c, '') = @oldcardno if @changetype!= 5 --add by wuxm 2002/06/17 由前台输入支付明细,不需要后台插入 begin if @changetype!= 8 --add by wuxm 2002/08/13 退卡有支付明细, 不需要后台插入支付档 begin insert into gsm03(gsc00c, gsc01c, gsc02c, gsc03f, gsc04c, gsc05f, gsc06c, gsc07f, gsc08c) select gea00c, gea01c, 'ge', (select isnull(max(gsc03f), 0) + 1 from gsm03 where gsc00c = @compid and gsc01c = @billid and gsc02c = 'ge'), '1', gea12f, null, null, gea01c from gem01 where gea00c = @compid and gea01c = @billid and isnull(gea12f, 0) <> 0 and gea02i <> 4 end end end if (@confirm = 2) begin if (@changetype = 99) begin update gnm01 set gna93c = '', gna94d = '', gna80d = '' where gna00c = @compid and gna01c = @billid update gcm01 set gca08i = 1, gca05d = '', gca11c = '', gca20c = '', gca19f = 0, gca21d = '', gca22d = '' from gcm01, gnm02 where gca00c = gca13d and gca01c = gnb03c and gnb00c = @compid and gnb01c = @billid -- 删除充值账户 delete gcm03 from gcm01, gnm02 where gcc00c = gca00c and gca00c = gca13d and gca01c = gcc01c and gca01c = gnb03c and gnb00c = @compid and gnb01c = @billid and isnull(gca24f, 0) <> 0 delete gcm02 where gcb00c = @compid and gcb04c = @billid -- Added by WUXM, 2006/09/18 -- 取消复核的时候 exec upg_create_gcm20_from_one_bill @compid, 'gn', @billid, 0 -- 会员卡销售的时候套餐的产品出库 -- 在购买套餐的过程中处理了,这里就不用处理了 end end end GO /****** Object: StoredProcedure [dbo].[upg_confirm_cardsale] Script Date: 06/12/2017 06:19:44 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_confirm_cardsale] ( @compid varchar(10), @billid varchar(20), @confirmer varchar(20), @confirmdate varchar(20), @curtime varchar(10), @confirm int ) as begin set nocount on -- 当前的事务数 declare @__transaction_count int set @__transaction_count = @@trancount -- try...catch处理 begin try -- a.开启事务,或者设置事务保存点 if @__transaction_count = 0 begin tran else save tran __tran_SavePoint declare @cardid varchar(20) declare @acctid int declare @amt float declare @card_num int declare @homeCompid varchar(10) declare @memid varchar(50) declare @sp112f float select @sp112f = gsa03f from gsm01 with (nolock) where gsa00c = @compid and gsa01c = 'SP112' declare @sendflag varchar(10) if @sp112f = 1 or @sp112f = 2 or @sp112f = 3 select @sendflag = '1' else select @sendflag = '2' create table #tbl_card_sale ( cardid varchar(20) not null, idd int identity not null, memid varchar(20) null, memname varchar(40) null, mobile varchar(20) null, gender int null, birth varchar(20) null, cardtype varchar(10) null, saleamt float null, acctid int null, saveamt float null, firststaff varchar(20) null, todate varchar(20) null, gba16c varchar(20) null, --身份证号 gba05c varchar(500) null, --会员地址 gba42c varchar(50) null, --会员进店渠道 gba04c varchar(1) null, --是否接收消费短信(1是2否) gba43c varchar(1) null, --是否接收群发短信(1是0否) gba06c varchar(50) null, --微信号 primary key(cardid) ) insert #tbl_card_sale(cardid, cardtype, saleamt, saveamt, acctid) select gnb03c, gnb02c, gnb07f, gnb11f, gnb13i from gnm02 b with (nolock) where gnb00c = @compid and gnb01c = @billid select @card_num = @@rowcount update a set a.memid = isnull(b.gna04c, a.cardid), a.mobile = b.gna66c, a.gender = b.gna85i, a.memname = isnull(b.gna06c, a.cardid), a.birth = gna10d, a.gba16c = b.gna107c, a.gba05c = b.gna108c, a.gba42c = b.gna109c, a.gba04c = b.gna110c, a.gba43c = b.gna111c, a.gba06c = b.gna112c from #tbl_card_sale a, gnm01 b where a.idd = 1 and b.gna00c = @compid and b.gna01c = @billid update a set a.firststaff = gna20c from #tbl_card_sale a, gnm01 b where b.gna00c = @compid and b.gna01c = @billid declare @firstcardid varchar(20) -- 取得第一张卡 select @cardid = cardid from #tbl_card_sale where idd = 1 set @firstcardid = @cardid -- 设置Confirm标志 exec upg_set_confirm_state @compid, 'gn', @billid, @confirmer, @confirmdate, @cardid update a set a.memname = a.cardid, a.memid = a.cardid from #tbl_card_sale a where isnull(memname, '') = '' update a set a.memid = a.cardid from #tbl_card_sale a where isnull(memid, '') = '' if @card_num > 1 begin update a set a.memid = a.cardid from #tbl_card_sale a end -- 计算截至日期 declare @unit varchar(20) declare @period float declare @todate varchar(20) declare cur_each_open_card cursor for select gak07f, gak08c from #tbl_card_sale a, gam10 b where a.cardtype = b.gak01c and b.gak00c = @compid for update of todate open cur_each_open_card fetch cur_each_open_card into @period, @unit while @@fetch_status = 0 begin exec upg_cal_next_day @confirmdate, @unit, @period, @todate output update #tbl_card_sale set todate = @todate where current of cur_each_open_card fetch cur_each_open_card into @period, @unit end close cur_each_open_card deallocate cur_each_open_card -- 更改卡的状态 select @homeCompid = gca13d, @memid = b.memid from gcm01 a with (nolock), #tbl_card_sale b where a.gca01c = b.cardid and a.gca00c = a.gca13d update a set a.gca08i = 4, a.gca02c = b.cardtype, a.gca04c = b.memid, a.gca05d = @confirmdate, gca06d = @confirmdate, gca11c = @billid, gca07d = b.todate, gca20c = b.firststaff from gcm01 a, #tbl_card_sale b where a.gca01c = b.cardid -- 账户 delete a from gcm03 a, #tbl_card_sale b where a.gcc01c = b.cardid declare cur_each_card cursor for select cardid, saveamt, acctid from #tbl_card_sale open cur_each_card fetch cur_each_card into @cardid, @amt, @acctid declare @__sp_re int while @@fetch_status = 0 begin exec @__sp_re = upg_add_money_new @compid, @cardid, @amt, @confirm, @acctid, 'gn', @billid, @confirmdate if @__sp_re <> 0 raiserror('has some error', 16, 1) if @acctid <> 2 begin exec @__sp_re = upg_add_money_new @compid, @cardid, 0, @confirm, 2, 'gn', @billid, @confirmdate if @__sp_re <> 0 raiserror('has some error', 16, 1) end fetch cur_each_card into @cardid, @amt, @acctid end close cur_each_card deallocate cur_each_card -- 疗程 -- 自动设置疗程购买的支付方式 -- 因为upg_add_cureproc_ex中需要用到支付方式的分配, 所以必须在之前就分配好 exec upg_create_buyproc_payment @compid, 'gn', @billid declare @proj varchar(30) declare @buytimes float declare @sendtimes float declare @amount float declare @start_date varchar(20) declare @end_date varchar(20) declare @priceflow float declare @discount float declare @cardnum float declare @issent int declare @isgift int declare @memo varchar(60) declare @gcf24i int declare @usetimemonth float declare cur_each_gnm03 cursor for select gnc02c, gnc03f, gnc04f, gnc05f, @confirmdate, gnc06d, gnc09i, gnc10f, gnc11f, 0, gnc12i, gnc13c, gnc29i, gnc08f from gnm03 with (nolock) where gnc00c = @compid and gnc01c = @billid open cur_each_gnm03 fetch cur_each_gnm03 into @proj, @buytimes, @sendtimes, @amount, @start_date, @end_date, @priceflow, @discount, @cardnum, @issent, @isgift, @memo, @gcf24i, @usetimemonth while @@fetch_status = 0 begin -- 疗程归第一张卡 exec upg_add_cureproc_ex @compid, @firstcardid, @proj, @buytimes, @sendtimes, @amount, @confirm, 1, @start_date, @end_date, 'gn', @billid, @confirmdate, @usetimemonth, @priceflow, @discount, @cardnum, @issent, @isgift, @memo, -1, @gcf24i fetch cur_each_gnm03 into @proj, @buytimes, @sendtimes, @amount, @start_date, @end_date, @priceflow, @discount, @cardnum, @issent, @isgift, @memo, @gcf24i, @usetimemonth end close cur_each_gnm03 deallocate cur_each_gnm03 -- 增加了会员基本资料 -- 可能已经存在的会员资料, 则更新 create table #tbl_card_sale_buf ( cardid varchar(20) not null, idd int identity not null, memid varchar(20) null, memname varchar(40) null, mobile varchar(20) null, gender int null, birth varchar(20) null, cardtype varchar(10) null, saleamt float null, acctid int null, saveamt float null, firststaff varchar(20) null, todate varchar(20) null, gba16c varchar(20) null, -- 身份证号 gba05c varchar(500) null, -- 会员地址 gba42c varchar(50) null, -- 会员进店渠道 gba04c varchar(1) null, -- 是否接收消费短信(1是2否) gba43c varchar(1) null, -- 是否接收群发短信(1是0否) gba06c varchar(50) null, -- 微信号 primary key(cardid) ) insert #tbl_card_sale_buf(cardid, memid, memname, mobile, gender, birth, cardtype, saleamt, acctid, saveamt, firststaff, todate, gba16c, gba05c, gba42c, gba04c, gba43c, gba06c) select cardid, memid, memname, mobile, gender, birth, cardtype, saleamt, acctid, saveamt, firststaff, todate, gba16c, gba05c, gba42c, gba04c, gba43c, gba06c from #tbl_card_sale update a set a.gba08c = isnull(b.mobile, a.gba08c), a.gba17d = isnull(b.birth, a.gba17d), a.gba23c = b.cardid, a.gba10c = b.firststaff, a.gba16c = b.gba16c, a.gba05c = b.gba05c, a.gba14i = b.gender, a.gba15c = '1', a.gba42c = b.gba42c, a.gba04c = b.gba04c, a.gba43c = b.gba43c, a.gba06c = b.gba06c from gbm01 a, #tbl_card_sale_buf b where a.gba01c = b.memid delete b from gbm01 a, #tbl_card_sale_buf b where a.gba00c = @compid and a.gba01c = b.memid if not exists(select 1 from gbm01 with (nolock) where gba00c = @homeCompid and gba01c = @memid ) begin insert gbm01(gba00c, gba02c, gba01c, gba03c, gba14i, gba08c, gba17d, gba23c, gba10c, gba16c, gba05c, gba15c, gba42c, gba04c, gba43c, gba06c) select @homeCompid, @confirmdate, memid, memname, gender, mobile, birth, cardid, firststaff, gba16c, gba05c, '1', gba42c, gba04c, gba43c, gba06c from #tbl_card_sale_buf end drop table #tbl_card_sale_buf -- 写业绩 exec upg_rewrite_verp @compid, @billid, 'gn', @confirm -- 添加异动信息 insert into gcm02(gcb00c, gcb01c, gcb02f, gcb03i, gcb04c, gcb05i, gcb06i, gcb07d) select @compid, cardid, (select isnull(max(gcb02f), 0) + 1 from gcm02 with (nolock) where gcb00c = @compid and gcb01c = cardid), 99, @billid, 1, 4, @confirmdate from #tbl_card_sale exec upg_conv_procamt_card_bill @compid, @billid -- 积分的处理 exec upg_update_point @compid, @billid, '', @confirmdate, 1, @confirm -- 设置帐务日期 WUXM 2005/08/26 exec upg_updategnm01_accountdate @compid, @billid -- 卡销售的时候,写异动历史 exec upg_create_gcm20_from_one_bill @compid, 'gn', @billid, 1 --会员卡销售的时候套餐的产品出库 --在购买套餐的过程中处理了,这里就不用处理了 drop table #tbl_card_sale -- 提交事务 -- 有可提交的事务, 并且事务是在当前模块中开启的情况下, 才提交事务 if xact_state() = 1 and @__transaction_count = 0 commit -- 为了防止TRY 中有遗漏的事务处理, 可以在TRY 模拟的结束部分做最终的判断 if @__transaction_count = 0 begin if xact_state() = -1 rollback tran else begin while @@trancount > 0 commit tran end end end try begin catch -- 在CATCH 模块, 应该首先处理事务 if xact_state() <> 0 begin if @__transaction_count = 0 rollback tran -- xact_state 为-1 时, 不能回滚到事务保存点, 这种情况留给外层调用者做统一的事务回滚 -- 通过@@trancount > @__transaction_count 的判断, 即使在TRY 模块中没有设置事务保存点的情况下跳到此步骤, 也不会出错 else if xact_state() = 1 and @@trancount > @__transaction_count rollback tran __tran_SavePoint end declare @SP_Parameters varchar(max) set @SP_Parameters = '@compid = ' + isnull(@compid, '') + '@billid = ' + isnull(@billid, '') + '@confirmer = ' + isnull(@confirmer, '') + '@confirmdate = ' + isnull(@confirmdate, '') + '@curtime = ' + isnull(@curtime, '') + '@confirm = ' + (case when @confirm is null then '' else convert(varchar(100), @confirm) end) insert into SP_TranLog(SPName, SPTime, SPParameter) values('upg_confirm_cardsale', getdate(), @SP_Parameters) return -1 end catch end GO /****** Object: StoredProcedure [dbo].[upg_update_salecard_servant_and_shareratio] Script Date: 06/12/2017 06:20:50 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_update_salecard_servant_and_shareratio] ( @comp varchar(10), -- 公司编号 @bill varchar(20) -- 销售单号 ) as --WUXM 2005/08/16 begin begin transaction begin try -- 更新欠款单据的服务人员及分享比率 update a set gcl09c = gna20c, gcl16f = gna27f, gcl17c = gna28c, gcl18f = gna29f, gcl19c = gna30c, gcl20f = gna31f, gcl22c = gna32c, gcl23f = gna33f, gcl24c = gna34c, gcl25f = gna35f, gcl26c = gna36c, gcl27f = gna37f, gcl28c = gna38c, gcl29f = gna39f, gcl30c = gna40c, gcl31f = gna41f, gcl32c = gna42c, gcl33f = gna43f, gcl34c = gna44c, gcl35f = gna45f, gcl36c = gna46c, gcl37f = gna47f, gcl38c = gna48c, gcl39f = gna49f, gcl40c = gna50c, gcl41f = gna51f, gcl42c = gna52c, gcl43f = gna53f, gcl44c = gna54c, gcl45f = gna55f, gcl46c = gna56c, gcl47f = gna57f, gcl48c = gna58c, gcl49f = gna59f, gcl50c = gna60c, gcl51f = gna61f, gcl52c = gna62c, gcl53f = gna63f, gcl54c = gna64c, gcl55f = gna65f from gcm10 a, gnm01 b where a.gcl00c = b.gna00c and a.gcl12c = b.gna01c and a.gcl11c = 'gn' and a.gcl06i = 5 and b.gna01c = @bill and b.gna00c = @comp -- 重新计算业绩 declare @SP048 varchar(20) select @SP048 = gsa02c from gsm01 with(nolock) where gsa00c = @comp and gsa01c = 'SP048' if (isnull(@SP048, '') <> '1') begin exec upg_rewrite_verp @comp, @bill, 'gn', 1 end -- 重新生成Gcm20 delete gcm20 where gct00c = @comp and gct13c = 'gn' and gct14c = @bill exec upg_create_gcm20_from_one_bill @comp, 'gn', @bill, 1 declare @bill_gcm10 varchar(20) select @bill_gcm10 = gcl01c from gcm10 a where a.gcl00c = @comp and a.gcl12c = @bill and a.gcl11c = 'gn' and a.gcl06i = 5 if (isnull(@bill_gcm10, '') <> '') begin delete gcm20 where gct00c = @comp and gct13c = 'gz' and gct14c = @bill_gcm10 exec upg_create_gcm20_from_one_bill @comp, 'gz', @bill_gcm10, 1 end end try begin catch if @@TRANCOUNT > 0 begin rollback transaction declare @SP_Parameters varchar(max) set @SP_Parameters = '@comp = ' + isnull(@comp, '') + '@bill = ' + isnull(@bill, '') insert into SP_TranLog(SPName, SPTime, SPParameter) values('upg_update_salecard_servant_and_shareratio', getdate(), @SP_Parameters) raiserror ('transaction is failed.', 16, 1) WITH NOWAIT end end catch if @@TRANCOUNT > 0 begin commit transaction end end GO /****** Object: StoredProcedure [dbo].[upg_ipos_confirm_gz] Script Date: 06/12/2017 06:20:17 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_ipos_confirm_gz] ( @compid varchar(10), -- 公司别 @billid varchar(20), -- 充值单号 @cardid varchar(20), -- 会员卡号 @billdate varchar(20), -- 单据日期 @userid varchar(20), -- 操作人 @doflag int -- 1-复核,现在只支持复核 ) as begin -- 设置复核标志等 declare @mode int declare @acctid varchar(10) declare @cardtype varchar(20) -- 会员卡类别 declare @dPureFillAmt float -- 充值金额 declare @staff1 varchar(20),@staff2 varchar(20),@staff3 varchar(20) declare @rate1 float, @rate2 float,@rate3 float declare @dept1 varchar(20),@dept2 varchar(20), @dept3 varchar(20) declare @curdate varchar(20),@curtime varchar(20) exec upg_getcurrdate @curdate output,@curtime output select @mode = gcl06i,@acctid=gcl04c,@dPureFillAmt=gcl65f,@staff1=gcl09c,@rate1=gcl16f,@staff2=gcl17c,@rate2=gcl18f,@staff3=gcl19c,@rate3=gcl20f, @dept1=gcl81c,@dept2=gcl82c,@dept3=gcl83c,@cardtype=gcl52c from gcm10 with (nolock) where gcl00c=@compid and gcl01c=@billid --设置明细的支付方式 --已经处理了不需要重复处理 --exec upg_create_buyproc_payment @compid,'gz',@billid --处理了卡升级,支付方式的填充等 exec upg_gcm10_confirm @compid,@billid,@billdate,@userid,@billdate,1,@mode,@acctid --处理卡的储值 exec upg_purse_deduct_ex @compid,@cardid,@billdate,@acctid,@dPureFillAmt,0,'gz',@billid,'IPOS充值',1 --处理疗程 declare @itemid varchar(20) declare @gcm03f float,@gcm04f float,@gcm05f float,@gcm08f float,@gcm10f float,@gcm11f float declare @gcm22i int,@gcm12i int, @gcm09i int declare @gcm13c varchar(60) declare @gcm06d varchar(20) declare @gcf24i int declare cur_each_gcm11 cursor for select gcm02c, gcm03f, gcm04f, gcm05f, gcm06d, gcm08f, gcm09i, gcm10f, gcm11f, gcm22i, gcm12i, gcm13c, gcm14i from gcm11 with (nolock) where gcm00c = @compid and gcm01c = @billid open cur_each_gcm11 fetch cur_each_gcm11 into @itemid, @gcm03f, @gcm04f, @gcm05f, @gcm06d, @gcm08f, @gcm09i, @gcm10f, @gcm11f, @gcm22i, @gcm12i, @gcm13c, @gcf24i while @@fetch_status = 0 begin exec upg_add_cureproc_ex @compid, @cardid, @itemid, @gcm03f, @gcm04f, @gcm05f, 1, 1, @billdate, @gcm06d, 'gz', @billid, @billdate, @gcm08f, @gcm09i, @gcm10f, @gcm11f, @gcm22i, @gcm12i, @gcm13c, -1, @gcf24i fetch cur_each_gcm11 into @itemid, @gcm03f, @gcm04f, @gcm05f, @gcm06d, @gcm08f, @gcm09i, @gcm10f, @gcm11f, @gcm22i, @gcm12i, @gcm13c, @gcf24i end close cur_each_gcm11 deallocate cur_each_gcm11 --积分的处理 exec upg_update_point @compid,'',@billid,@billdate,2,1 --处理业绩 exec upg_rewrite_verp @compid,@billid,'gz',1 --处理库存 exec upg_refresh_stock @compid,@billdate,@billid,'',1,5 --处理欠款 declare @defamt float -- 欠款金额 select @defamt = 0 select @defamt=isnull(gsc05f,0) from gsm03 with (nolock) where gsc00c=@compid and gsc01c=@billid and gsc02c='gz' and gsc04c='5' if @defamt>0 begin --生成欠款记录 declare @defbillid varchar(20) select @defbillid = @billid+'X' insert gcm10(gcl00c, gcl01c, gcl02d, gcl03c, gcl04c, gcl05c,gcl06i,gcl07f,gcl08f,gcl09c,gcl11c,gcl12c,gcl91c,gcl92d,gcl93c,gcl94d,gcl15c,gcl16f,gcl17c,gcl18f,gcl19c,gcl20f,gcl48c,gcl52c,gcl53f,gcl56t,gcl80d,gcl64f,gcl65f,gcl81c,gcl82c,gcl83c) values (@compid,@defbillid,@billdate,@cardid,@acctid,'0',5,@defamt,0,@staff1,'gz',@billid,@userid,@billdate,@userid,@billdate,@cardtype,@rate1,@staff2,@rate2,@staff3,@rate3,@cardid,@cardtype,@defamt,@curtime,@billdate,@defamt,@defamt,@dept1,@dept2,@dept3) --欠款需要调整帐户的钱 exec upg_purse_deduct @compid,@cardid,@billdate,@acctid,@defamt,5,'gz',@defbillid,1 end --start 增加赠送金额 add by lm 2016.01.12 declare @donate_amt float declare @standfillamt float declare @gze02f_max float declare @donate_acct int declare @acct float select top 1 @standfillamt=gze02f,@donate_amt = gze03f from gzm05 with (nolock) where gze00c = @compid and gze01c = @cardtype and gze02f <= @dPureFillAmt order by gze02f desc if(isnull(@donate_amt,0) > 0 and @standfillamt<>0) begin if(cast(@dPureFillAmt as int) % cast(@standfillamt as int) = 0) begin select @acct = dbo.fun_divide_default(@dPureFillAmt,@standfillamt,0) if(@acct >= 1) begin set @donate_amt = @acct * @donate_amt end end select @donate_acct = gsa02c from gsm01 with (nolock) where gsa01c = 'RP011' and gsa00c = @compid if(isnull(@donate_acct,'') <> '') begin exec upg_purse_deduct @compid, @cardid, @billdate, @donate_acct, @donate_amt, 0, 'gz', @billdate, 1 update a set a.gcl107c = @donate_acct, a.gcl108f = @donate_amt from gcm10 a where a.gcl00c = @compid and a.gcl01c = @billid and a.gcl06i = 0 end end --end end GO /****** Object: StoredProcedure [dbo].[upg_update_rechargecard_servant_and_shareratio] Script Date: 06/12/2017 06:20:50 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_update_rechargecard_servant_and_shareratio] ( @comp varchar(10), -- 公司编号 @bill varchar(20) -- 充值单号 ) as --WUXM 2005/08/16 begin begin transaction begin try -- 更新欠款单据的服务人员及分享比率 update a set gcl09c = b.gcl09c, gcl16f = b.gcl16f, gcl17c = b.gcl17c, gcl18f = b.gcl18f, gcl19c = b.gcl19c, gcl20f = b.gcl20f, gcl22c = b.gcl22c, gcl23f = b.gcl23f, gcl24c = b.gcl24c, gcl25f = b.gcl25f, gcl26c = b.gcl26c, gcl27f = b.gcl27f, gcl28c = b.gcl28c, gcl29f = b.gcl29f, gcl30c = b.gcl30c, gcl31f = b.gcl31f, gcl32c = b.gcl32c, gcl33f = b.gcl33f, gcl34c = b.gcl34c, gcl35f = b.gcl35f, gcl36c = b.gcl36c, gcl37f = b.gcl37f, gcl38c = b.gcl38c, gcl39f = b.gcl39f, gcl40c = b.gcl40c, gcl41f = b.gcl41f, gcl42c = b.gcl42c, gcl43f = b.gcl43f, gcl44c = b.gcl44c, gcl45f = b.gcl45f, gcl46c = b.gcl46c, gcl47f = b.gcl47f, gcl48c = b.gcl48c, gcl49f = b.gcl49f, gcl50c = b.gcl50c, gcl51f = b.gcl51f, gcl52c = b.gcl52c, gcl53f = b.gcl53f, gcl54c = b.gcl54c, gcl55f = b.gcl55f from gcm10 a, gcm10 b where a.gcl00c = b.gcl00c and a.gcl12c = b.gcl01c and a.gcl11c = 'gz' and a.gcl06i = 5 and b.gcl01c = @bill and b.gcl00c = @comp -- 重新计算业绩 declare @SP048 varchar(20) select @SP048 = gsa02c from gsm01 with(nolock) where gsa00c = @comp and gsa01c = 'SP048' if (isnull(@SP048, '') <> '1') begin exec upg_rewrite_verp @comp, @bill, 'gz', 1 end -- 重新生成Gcm20 delete gcm20 where gct00c = @comp and gct13c = 'gz' and gct14c = @bill exec upg_create_gcm20_from_one_bill @comp, 'gz', @bill, 1 declare @bill_gcm10 varchar(20) select @bill_gcm10 = gcl01c from gcm10 a where a.gcl00c = @comp and a.gcl12c = @bill and a.gcl11c = 'gz' and a.gcl06i = 5 if (isnull(@bill_gcm10, '') <> '') begin delete gcm20 where gct00c = @comp and gct13c = 'gz' and gct14c = @bill_gcm10 exec upg_create_gcm20_from_one_bill @comp, 'gz', @bill_gcm10, 1 end end try begin catch if @@TRANCOUNT > 0 begin rollback transaction declare @SP_Parameters varchar(max) set @SP_Parameters = '@comp = ' + isnull(@comp, '') + '@bill = ' + isnull(@bill, '') insert into SP_TranLog(SPName, SPTime, SPParameter) values('upg_update_rechargecard_servant_and_shareratio', getdate(), @SP_Parameters) raiserror ('transaction is failed.', 16, 1) WITH NOWAIT end end catch if @@TRANCOUNT > 0 begin commit transaction end end GO /****** Object: StoredProcedure [dbo].[upg_3g_init_card] Script Date: 06/12/2017 06:19:22 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_3g_init_card] ( @compid varchar(10), -- 公司别 @cardno varchar(20), -- 卡号 @cardtype varchar(5), -- 卡类别 @phone varchar(20), -- 手机号 @memname varchar(30), -- 会员姓名 @passward varchar(20) -- 初始查询密码 ) as -- 申请电子会员卡 begin exec upg_3g_init_card_ex @compid, @cardno, @cardtype, @phone, @memname, @passward, 0 end GO /****** Object: StoredProcedure [dbo].[upg_confirm_ggm01] Script Date: 06/12/2017 06:19:45 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_confirm_ggm01] ( @compid varchar(10), -- 公司编号 @enterno varchar(20), -- 入院单号 @billtype varchar(5), -- 单据类别 @billid varchar(20), -- 单据编号 @keyno varchar(20), -- 钥匙牌号 @userid varchar(10), -- 复核人 @currdate varchar(20), -- 复核日期 @confirm int, -- 是否复核 (1-复核 2-取消复核) @tablekind varchar(2) -- 要更新的table(1.ggm01) ) as--WUXM, 2004/07/10 set nocount on set rowcount 0 set transaction isolation level read uncommitted begin begin try begin transaction -- if @confirm = 1 -- begin -- if not exists(select 1 from glm02 where glb00c = @compid and glb01c = @enterno and glb02c = @billtype and glb03c = @billid) -- begin -- --防止一个收银单挂在两个入院单上 -- delete glm02 -- where glb00c = @compid and glb02c = @billtype and glb03c = @billid -- -- insert glm02(glb00c, glb01c, glb02c, glb03c, glb04c) -- values(@compid, @enterno, @billtype, @billid, @keyno) -- end -- end -- else -- begin -- delete from glm02 -- where glb00c = @compid and glb01c = @enterno and glb02c = @billtype and glb03c = @billid -- end if @confirm = 1 begin update ggm01 set gga93c = @userid, gga94d = @currdate where gga00c = @compid and gga01c = @billid -- Added by WUXM, 2006/09/18 exec upg_create_gcm20_from_one_bill @compid, 'gx', @billid, 1 end else begin update ggm01 set gga93c = '', gga94d = '', gga80d = '' where gga00c = @compid and gga01c = @billid -- Added by WUXM, 2006/09/18 exec upg_create_gcm20_from_one_bill @compid, 'gx', @billid, 0 end -- 直接调整库存 -- Added by WUXM, 2004/07/10 if @billtype = 'gx' begin exec upg_refresh_stock @compid, @currdate, @billid, '01', @confirm, 3 end -- 增加帐务日期 added by WUXM 2005/08/26 exec upg_update_ggm01_date @compid, @billid -- 回写账户类别和余额到glm01 -- exec upg_update_glm01account @compid, @enterno, @billid -- 把欠款的产品设置欠款标志 exec upg_set_deficit_flag @compid, @billid --判断是否启用了购买产品赠送服务的功能 declare @send_srv_when_buy_goods varchar(20) select @send_srv_when_buy_goods = isnull(gsa02c, '') from gsm01 where gsa00c = @compid and gsa01c = 'SP112' if @send_srv_when_buy_goods = '1' begin exec upg_send_gift_prjs @compid, @billid end end try begin catch if @@TRANCOUNT > 0 begin rollback transaction declare @SP_Parameters varchar(max) set @SP_Parameters = '@compid = ' + isnull(@compid, '') + '@enterno = ' + isnull(@enterno, '') + '@billtype = ' + isnull(@billtype, '') + '@billid = ' + isnull(@billid, '') + '@keyno = ' + isnull(@keyno, '') + '@userid = ' + isnull(@userid, '') + '@currdate = ' + isnull(@currdate, '') + '@confirm = ' + (case when @confirm is null then '' else convert(varchar(100), @confirm) end) + '@tablekind = ' + isnull(@tablekind, '') insert into SP_TranLog (SPName ,SPTime ,SPParameter) values ( 'upg_confirm_ggm01' ,getdate() ,@SP_Parameters) raiserror ('transaction is failed.', 16, 1) WITH NOWAIT end end catch if @@TRANCOUNT > 0 begin commit transaction end end GO /****** Object: StoredProcedure [dbo].[upg_resetverp_gcm10_gnm01] Script Date: 06/12/2017 06:20:38 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_resetverp_gcm10_gnm01] ( @compid varchar(10), @fromdate varchar(20), @todate varchar(20) ) as begin declare @SP04802 varchar(5) select @SP04802 = gsa02c from gsm01 where gsa00c = @compid and gsa01c = 'SP048' if (@SP04802 = 1) return; create table #resetverp ( compid varchar(10) null, billid varchar(20) null, billtype varchar(5) null, ) -- 创建临时表 create table #temp_comp ( compId varchar(10) not null ) insert into #temp_comp(compId) select gaz02c from gam26 with (nolock) where gaz01c = @compid -- 创建循环变量 declare @HasRecord int set @HasRecord = 1 declare @currentCompId varchar(10) while(@HasRecord = 1) begin if exists(select 1 from #temp_comp) begin set @HasRecord = 1 select top 1 @currentCompId = compId from #temp_comp insert #resetverp(compid, billid, billtype) select gna00c, gna01c, 'gn' from gnm01 where gna00c = @currentCompId and gna80d between @fromdate and @todate insert #resetverp(compid, billid, billtype) select gcl00c, gcl01c, 'gz' from gcm10 where gcl00c = @compid and gcl80d between @fromdate and @todate and (gcl06i = 0 or gcl06i = 1 or gcl06i = 6) -- 清除已处理的门店 delete #temp_comp where compId = @currentCompId end else begin set @HasRecord = 0 end end drop table #temp_comp declare @billid varchar(20) declare @billtype varchar(5) declare cursor_02 cursor for select compid, billid, billtype from #resetverp open cursor_02 fetch cursor_02 into @currentCompId, @billid, @billtype while @@fetch_status = 0 begin execute upg_rewrite_verp @currentCompId, @billid, @billtype, 1 fetch cursor_02 into @currentCompId, @billid, @billtype end close cursor_02 deallocate cursor_02 drop table #resetverp end GO /****** Object: StoredProcedure [dbo].[upg_ipos_confirm_gx] Script Date: 06/12/2017 06:20:17 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_ipos_confirm_gx] ( @compid varchar(10), -- 公司别 @billid varchar(20), -- 单号 @cardid varchar(20), -- 会员卡号 @billdate varchar(20), -- 单据日期 @userid varchar(20), -- 操作人 @doflag int -- 1-复核,现在只支持复核 ) as begin declare @payment varchar(20) -- 支付方式 declare @acctid int -- 帐户代码 declare @amt float -- 支付今额 declare @enterbillid varchar(20) -- 入院单号 -- 处理单据的复核 -- 先需要往glm01增加一条记录 declare @memid varchar(20) declare @memname varchar(40) declare @curtime varchar(20) declare @totalamt float -- 消费总额 declare @ggm01_acctid int -- ggm01 中记录的帐户代码 declare @gender int -- 客户的性别 declare @isoldcust float -- 是否为老客 select @enterbillid = @billid -- 默认收银单号 select @memid=gga05c,@memname=gga06c,@curtime=gga03t,@totalamt=gga16f,@ggm01_acctid=gga27i,@gender=gga31i,@isoldcust=gga13f from ggm01 with (nolock) where gga00c=@compid and gga01c=@billid delete glm01 where gla00c=@compid and gla01c=@enterbillid --先取得卡当前的余额 declare @amt_2 float,@amt_3 float,@amt_4 float,@amt_5 float,@amt_6 float,@amt_7 float declare @curamt float -- ggm01 中帐户对应的余额 declare @reduceamt_incard float -- 本次销卡金额 exec upg_get_card_acct_remain @cardid,@ggm01_acctid,@curamt output,@amt_2 output,@amt_3 output,@amt_4 output,@amt_5 output,@amt_6 output,@amt_7 output --取得本次销卡的金额 select @reduceamt_incard=gsc05f from gsm03 a with (nolock),gsm14 b where a.gsc00c =@compid and a.gsc02c = 'gx' and a.gsc01c = @billid and a.gsc04c = b.gsn01c and b.gsn02c = convert(varchar(10),@ggm01_acctid) insert glm01(gla00c,gla01c,gla02d,gla03t,gla04t,gla05c,gla06c,gla11f,gla16c,gla23i,gla27c,gla30f,gla31f,gla32f,gla33f,gla34i,gla35f,gla36f,gla91c,gla92d,gla93c,gla94d,gla95c,gla96d,gla80d) values(@compid,@enterbillid,@billdate,@curtime,@curtime,@memname,@cardid,@totalamt,@cardid,1,@memid,@amt_2,@amt_3,@amt_4,@amt_5,@ggm01_acctid,@curamt,@reduceamt_incard,@userid,@billdate,@userid,@billdate,@userid,@billdate,@billdate) --设置ggm01中gga30c的值 update ggm01 set gga30c=@enterbillid where gga00c=@compid and gga01c=@billid --设置明细的支付方式 --exec upg_billpay_2_itempay @compid,@billid exec upg_confirm_ggm01 @compid,@enterbillid,'gx',@billid,@cardid,@userid,@billdate,1,1 declare cur_each_gsm03 cursor for select gsc04c,gsc05f from gsm03 with (nolock) where gsc00c=@compid and gsc02c='gx' and gsc01c=@billid and gsc04c in (select gsn01c from gsm14 where isnull(gsn02c,'')<>'' and gsn02c<>'999') open cur_each_gsm03 fetch cur_each_gsm03 into @payment,@amt while @@fetch_status = 0 begin select @acctid=convert(int,gsn02c) from gsm14 where gsn01c=@payment exec upg_purse_deduct @compid,@cardid,@billdate,@acctid,@amt,2,'gx',@billid,1 fetch cur_each_gsm03 into @payment,@amt end close cur_each_gsm03 deallocate cur_each_gsm03 --判断是有有疗程支付 declare @has_proc int -- 是否有疗程支付 declare @has_combo int -- 是否有套餐支付 declare @itemid varchar(20) -- 项目编码 declare @itemnum float -- 数量 declare @itemamt float -- 金额 declare @combo_idd int -- 套餐需要 declare @proc_payment varchar(20) -- 疗程支付代码 declare @procidd int -- 疗程序号 --取疗程支付对应的支付方式 select @proc_payment = '9' --判断这个收银单是有有疗程支付 if exists(select 1 from gsm03 with (nolock) where gsc00c=@compid and gsc01c=@billid and gsc02c='gx' and gsc04c=@proc_payment) select @has_proc = 1 else select @has_proc = 0 --判断这个收银单是否有套餐支付 if exists(select 1 from gsm03 with (nolock) where gsc00c=@compid and gsc01c=@billid and gsc02c='gx' and gsc04c='#') select @has_combo = 1 else select @has_combo = 0 if @has_proc = 1 begin print '处理疗程' declare cur_each_item cursor for select ggb03c,ggb05f,ggb11f,ggb47i from ggm02 with (nolock) where ggb00c=@compid and ggb01c=@billid and ggb27c = @proc_payment open cur_each_item fetch cur_each_item into @itemid,@itemnum,@itemamt,@procidd while @@fetch_status=0 begin exec upg_used_cureproc_ex @compid,@cardid,@itemid,@itemnum,@itemamt,1,'gx',@billid,@billdate,@procidd fetch cur_each_item into @itemid,@itemnum,@itemamt,@procidd end close cur_each_item deallocate cur_each_item end --套餐支付的处理 --套餐项目支付 if @has_combo = 1 begin declare cur_each_item_combo cursor for select ggb03c,ggb05f,ggb11f,ggb48i from ggm02 with (nolock) where ggb00c=@compid and ggb01c=@billid and ggb27c = '#' open cur_each_item_combo fetch cur_each_item_combo into @itemid,@itemnum,@itemamt,@combo_idd while @@fetch_status=0 begin exec upg_used_combo @compid,@cardid,@itemid,@itemnum,1,'gx',@billid,@billdate,1,@combo_idd fetch cur_each_item_combo into @itemid,@itemnum,@itemamt,@combo_idd end close cur_each_item_combo deallocate cur_each_item_combo end --套餐产品的处理 if @has_combo = 1 begin declare cur_each_item_combo cursor for select ggc03c,ggc05f,ggc11f,ggc48i from ggm03 with (nolock) where ggc00c=@compid and ggc01c=@billid and ggc27c = '#' open cur_each_item_combo fetch cur_each_item_combo into @itemid,@itemnum,@itemamt,@combo_idd while @@fetch_status=0 begin exec upg_used_combo @compid,@cardid,@itemid,@itemnum,1,'gx',@billid,@billdate,2,@combo_idd fetch cur_each_item_combo into @itemid,@itemnum,@itemamt,@combo_idd end close cur_each_item_combo deallocate cur_each_item_combo end -- 处理员工业绩 exec upg_compute_cost_perf @compid,@billid -- 处理积分 exec upg_manage_points @compid, @cardid, 'gx', @billid, @billdate, 0, 1 exec upg_recalc_account @compid,@enterbillid exec upg_close_account_ggm01 @compid, @enterbillid, @cardid, @userid, @billdate, @gender, @isoldcust end GO /****** Object: StoredProcedure [dbo].[upg_ipos_confirm_gn] Script Date: 06/12/2017 06:20:16 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_ipos_confirm_gn] ( @compid varchar(10), -- 公司别 @billid varchar(20), -- 卡销售单号 @cardid varchar(20), -- 会员卡号 @billdate varchar(20), -- 单据日期 @userid varchar(20), -- 操作人 @doflag int -- 1-复核,现在只支持复核 ) as begin declare @curdate varchar(20) declare @curtime varchar(20) exec upg_getcurrdate @curdate output,@curtime output declare @memo varchar(180) declare @cardtype varchar(20) -- 卡类别 declare @acctid int -- 帐户类别 declare @staff1 varchar(20) declare @staff2 varchar(20) declare @staff3 varchar(20) declare @rate1 float declare @rate2 float declare @rate3 float declare @dept1 varchar(20) declare @dept2 varchar(20) declare @dept3 varchar(20) -- 设置销售人员的部门 select @memo = gna07c, @staff1 = gna20c, @rate1 = gna27f, @staff2 = gna28c, @rate2 = gna29f, @staff3 = gna30c, @rate3 = gna31f, @dept1 = gna81c, @dept2 = gna82c, @dept3 = gna83c from gnm01 with(nolock) where gna00c = @compid and gna01c = @billid select @cardtype = gnb02c, @acctid = gnb13i from gnm02 with(nolock) where gnb00c = @compid and gnb01c = @billid -- 生成这个会员卡,卡销售的时候是直接输入的 exec upg_ipos_create_card @compid, @cardtype, @cardid, @billdate -- 设置明细的支付方式 -- 已经处理了,不需要重复处理 -- exec upg_create_buyproc_payment @compid,'gn',@billid -- 设置复核标志 -- 更新卡的状态 -- 设置卡的帐户值 -- 处理了疗程够购买 -- 增加了会员资料 -- 处理了积分 -- 处理了帐户日期 -- 生成了gcm20卡历史 exec upg_confirm_cardsale @compid, @billid, @userid, @billdate, @curtime, 1 -- 处理卡的备注 -- 处理备注 declare @credit_rate float -- 信用卡支付的时候转换比率 select @credit_rate = gsa03f from gsm01 where gsa00c = @compid and gsa01c = 'SP057' update gcm01 set gca02c = @cardtype, gca25f = isnull(@credit_rate, 0), gca27c = isnull(gca27c, '') + isnull(@memo, '') from gcm01 where gca01c = @cardid -- 处理欠款,如果有欠款,需要生成帐户异动单 declare @defamt float -- 欠款金额 declare @dPureFillAmt float -- 充值金额 select @defamt = 0 select @defamt = sum(isnull((case when gsc04c='5' then isnull(gsc05f, 0) else 0 end),0)) , @dPureFillAmt = sum(isnull((case when gsc04c<>'5' then isnull(gsc05f, 0) else 0 end),0) ) from gsm03 with(nolock) where gsc00c = @compid and gsc01c = @billid and gsc02c = 'gn' if @defamt > 0 begin -- 生成欠款记录 declare @defbillid varchar(20) select @defbillid = @billid + 'X' insert gcm10(gcl00c, gcl01c, gcl02d, gcl03c, gcl04c, gcl05c,gcl06i,gcl07f,gcl08f,gcl09c,gcl11c,gcl12c,gcl91c,gcl92d,gcl93c,gcl94d,gcl15c,gcl16f,gcl17c,gcl18f,gcl19c,gcl20f,gcl48c,gcl52c,gcl53f,gcl56t,gcl80d,gcl64f,gcl65f,gcl81c,gcl82c,gcl83c) values (@compid,@defbillid,@billdate,@cardid,@acctid,'0',5,@defamt,0,@staff1,'gn',@billid,@userid,@billdate,@userid,@billdate,@cardtype,@rate1,@staff2,@rate2,@staff3,@rate3,@cardid,@cardtype,@defamt,@curtime,@billdate,@defamt,@defamt,@dept1,@dept2,@dept3) -- 欠款需要调整帐户的钱 exec upg_purse_deduct @compid, @cardid, @billdate, @acctid, @defamt, 5, 'gz', @defbillid, 1 end -- 没有处理套餐 -- 处理可能的库存 exec upg_refresh_stock @compid, @billdate, @billid, '', 1, 4 -- 处理业绩 exec upg_rewrite_verp @compid, @billid, 'gn', 1 --start 增加赠送金额 add by liujie 2016.06.28 declare @donate_amt float declare @standfillamt float declare @gze02f_max float declare @donate_acct int declare @acct float select top 1 @standfillamt=gze02f,@donate_amt = gze03f from gzm05 with (nolock) where gze00c = @compid and gze01c = @cardtype and gze02f <= @dPureFillAmt order by gze02f desc if(isnull(@donate_amt,0) > 0 and @standfillamt<>0) begin if(cast(@dPureFillAmt as int) % cast(@standfillamt as int) = 0) begin select @acct = dbo.fun_divide_default(@dPureFillAmt,@standfillamt,0) if(@acct >= 1) begin set @donate_amt = @acct * @donate_amt end end select @donate_acct = gsa02c from gsm01 with (nolock) where gsa01c = 'SP147' and gsa00c = @compid --select @compid, @cardid, @billdate, @donate_acct, @donate_amt, 0, 'gn', @billdate, 1 if(isnull(@donate_acct,'') <> '') begin exec upg_purse_deduct @compid, @cardid, @billdate, @donate_acct, @donate_amt, 0, 'gn', @billdate, 1 update gnm02 set gnb23c=@donate_acct,gnb24f=@donate_amt where gnb00c=@compid and gnb01c =@billid and gnb03c=@cardid end end --end end GO /****** Object: StoredProcedure [dbo].[upg_accept_appointment] Script Date: 06/12/2017 06:19:22 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_accept_appointment] ( @idd int ) as begin -- 自动产生电子会员卡 declare @mobile varchar(20) declare @cardid varchar(20) declare @memname varchar(20) declare @compid varchar(10) select @mobile = gwa07c, @memname = gwa05c, @compid = gwa00c, @cardid = gwa04c from gwm01 with (nolock) where idd = @idd declare @cardExisted int set @cardExisted = 0 -- 输入会员卡 if substring(@cardid, 1, 2) <> '散客' begin -- 会员卡不存在 if exists (select 1 from gcm01 where gca01c = @cardid and gca00c = gca13d) begin set @cardExisted = 1 end end -- 会员卡不存在, 根据手机找会员, 再找会员卡 if @cardExisted = 0 begin -- 根据手机找到会员 if exists(select 1 from gbm01 with (nolock) where gba08c = @mobile) begin set @cardid = null; select @cardid = gba23c from gbm01 with(nolock), gcm01 with(nolock) where gba08c = @mobile and gba23c = gca01c and gba00c = gca00c and gca00c = gca13d and gba00c = @compid if isnull(@cardid, '') = '' begin select @cardid = gba23c from gbm01 with(nolock), gcm01 with(nolock) where gba08c = @mobile and gba23c = gca01c and gba00c = gca00c and gca00c = gca13d end set @cardExisted = 1 end else -- 根据手机找不到会员 begin set @cardExisted = 0 end end -- 判断是否已经存在会员资料,如果没有则增加 if @cardExisted = 0 begin exec upg_3g_init_card @compid, @mobile, 'ZZ', @mobile, @memname, '' end else begin if isnull(@cardid, '') <> '' begin --已经处理了跨店预约的赠送情况 exec upg_3g_event @compid, @cardid, '', '', '', '', '', 9 end end end GO /****** Object: StoredProcedure [dbo].[upg_calc_execute] Script Date: 06/12/2017 06:19:36 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_calc_execute] ( @calc_type int, -- 计算类型 @calc_comp varchar(10), -- 计算门店 @fromdate varchar(10), @todate varchar(10) ) as begin if @calc_type = 1 begin exec upg_compute_all_perf @calc_comp, @fromdate, @todate return 1 end if @calc_type = 2 begin exec upg_resetverp_gcm10_gnm01 @calc_comp, @fromdate, @todate return 2 end if @calc_type = 3 begin exec upg_compute_all_perf @calc_comp, @fromdate, @todate exec upg_resetverp_gcm10_gnm01 @calc_comp, @fromdate, @todate return 3 end if @calc_type = 4 begin exec upg_calc_personal_statistics @calc_comp, @fromdate, @todate return 4 end if @calc_type = 5 begin declare @calMode int select @calMode = gsa03f from gsm01 with (nolock) where gsa00c = @calc_comp and gsa01c = 'SP115' if @calc_comp = '001' begin exec upg_cal_profit_all @fromdate, @todate, @calMode end else begin exec upg_cal_profit @calc_comp, @fromdate, @todate, @calMode end return 5 end end GO /****** Object: StoredProcedure [dbo].[upg_calc_start_taskstep] Script Date: 06/12/2017 06:19:37 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_calc_start_taskstep] ( @task_id int -- 计划任务id ) as begin declare @start_time datetime set @start_time = getdate() update calc_task set calc_time = @start_time, success = 0 where task_id = @task_id declare @calc_type int -- 计算类型 declare @calc_comp varchar(10) -- 计算门店 declare @fromdate varchar(10) declare @todate varchar(10) select @calc_type = calc_type, @calc_comp = calc_comp, @fromdate = fromdate, @todate = todate from calc_task where task_id = @task_id exec upg_calc_execute @calc_type, @calc_comp, @fromdate, @todate update calc_task set success = 1, consumed = datediff(second, @start_time, getdate()) where task_id = @task_id end GO /****** Object: StoredProcedure [dbo].[upg_calc_instant] Script Date: 06/12/2017 06:19:36 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_calc_instant] ( @compid varchar(10), -- 门店 @calc_type int, -- 计算类型 @calc_comp varchar(10), -- 计算门店 @fromdate varchar(10), @todate varchar(10) ) as begin declare @id int declare @start_time datetime set @start_time = getdate() insert into calc_instant(inst_comp, calc_type, calc_comp, calc_time, fromdate, todate) values(@compid, @calc_type, @calc_comp, @start_time, @fromdate, @todate) set @id = @@identity exec upg_calc_execute @calc_type, @calc_comp, @fromdate, @todate update calc_instant set success = 1, consumed = datediff(second, @start_time, getdate()) where inst_id = @id end GO /****** Object: StoredProcedure [dbo].[upg_handle_appointment] Script Date: 06/12/2017 06:20:12 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_handle_appointment] ( @id int, @accept int, @reason varchar(max), @result int output ) as begin set @accept = isnull(@accept, 1) if @accept <> 1 begin set @accept = 2 end declare @last_status int select @last_status = isnull(status, 1) from gwm01 where idd = @id if @last_status = 0 begin update gwm01 set status = @accept where idd = @id if @accept = 1 exec upg_accept_appointment @id else begin declare @gwa16c varchar(max) select @gwa16c = gwa16c from gwm01 where idd = @id if isnull(@gwa16c, '') = '' set @gwa16c = @reason else if isnull(@reason, '') <> '' set @gwa16c = @gwa16c + ' ' + @reason set @gwa16c = substring( isnull(@gwa16c, ''), 1, 30) update gwm01 set gwa16c = @gwa16c where idd = @id end set @result = 0 end else begin set @result = 1 end end GO /****** Object: StoredProcedure [dbo].[upg_ipos_confirm] Script Date: 06/12/2017 06:20:16 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_ipos_confirm] ( @compid varchar(10), -- 公司别 @billtype varchar(20), -- 单据类别 gx-收银单 gn-卡销售 gz-卡充值 @billid varchar(20), -- 单号 @cardid varchar(20), -- 会员卡号 @billdate varchar(20), -- 单据日期 @userid varchar(20), -- 操作人 @source varchar(20), -- 'IPOS','MALL' @doflag int -- 1-复核,现在只支持复核 ) as begin if @billtype = 'gx' begin exec upg_ipos_confirm_gx @compid, @billid, @cardid, @billdate, @userid, @doflag end else if @billtype = 'gn' begin exec upg_ipos_confirm_gn @compid, @billid, @cardid, @billdate, @userid, @doflag end else if @billtype = 'gz' begin exec upg_ipos_confirm_gz @compid, @billid, @cardid, @billdate, @userid, @doflag end end GO /****** Object: StoredProcedure [dbo].[upg_calc_start_task] Script Date: 06/12/2017 06:19:37 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[upg_calc_start_task] as begin create table #task_id ( id int ) insert into #task_id(id) select task_id from calc_task where success is null order by task_id -- 创建循环变量 declare @HasRecord int set @HasRecord = 1 declare @current_id int while(@HasRecord = 1) begin if exists(select 1 from #task_id) begin set @HasRecord = 1 select top 1 @current_id = id from #task_id exec upg_calc_start_taskstep @current_id -- 清除已处理的门店 delete #task_id where id = @current_id end else begin set @HasRecord = 0 end end drop table #task_id end GO /****** Object: Default [DF__appointme__ratio__369C13AA] Script Date: 06/12/2017 06:16:28 ******/ ALTER TABLE [dbo].[appointment_perf_ratio] ADD DEFAULT ((1)) FOR [ratio] GO /****** Object: Default [DF__b_perf__mmonth__76EBA2E9] Script Date: 06/12/2017 06:16:28 ******/ ALTER TABLE [dbo].[b_perf] ADD DEFAULT ('200801') FOR [mmonth] GO /****** Object: Default [DF__calc_work__share__10416098] Script Date: 06/12/2017 06:16:28 ******/ ALTER TABLE [dbo].[calc_work_detail] ADD DEFAULT ((1)) FOR [share_rate] GO /****** Object: Default [DF__gam03__gac17c__014935CB] Script Date: 06/12/2017 06:16:28 ******/ ALTER TABLE [dbo].[gam03] ADD DEFAULT ('Y') FOR [gac17c] GO /****** Object: Default [DF__gam03__gac18i__023D5A04] Script Date: 06/12/2017 06:16:28 ******/ ALTER TABLE [dbo].[gam03] ADD DEFAULT ((0)) FOR [gac18i] GO /****** Object: Default [DF__gam03__gac19c__03317E3D] Script Date: 06/12/2017 06:16:28 ******/ ALTER TABLE [dbo].[gam03] ADD DEFAULT ('Y') FOR [gac19c] GO /****** Object: Default [DF__gam04__FId__1E5C0C73] Script Date: 06/12/2017 06:16:28 ******/ /****** Object: Default [DF__gam10__gak78i__3691F209] Script Date: 06/12/2017 06:16:28 ******/ ALTER TABLE [dbo].[gam10] ADD DEFAULT ((0)) FOR [gak78i] GO /****** Object: Default [DF__gam10__gak79i__37861642] Script Date: 06/12/2017 06:16:28 ******/ ALTER TABLE [dbo].[gam10] ADD DEFAULT ((0)) FOR [gak79i] GO /****** Object: Default [DF__gam10__gak80i__387A3A7B] Script Date: 06/12/2017 06:16:28 ******/ ALTER TABLE [dbo].[gam10] ADD DEFAULT ((0)) FOR [gak80i] GO /****** Object: Default [DF__gam10__gak82c__0FED2634] Script Date: 06/12/2017 06:16:28 ******/ ALTER TABLE [dbo].[gam10] ADD DEFAULT ((1)) FOR [gak82c] GO /****** Object: Default [DF__gam10__FId__2F869875] Script Date: 06/12/2017 06:16:28 ******/ /****** Object: Default [DF__gam14__gal04i__1ED998B2] Script Date: 06/12/2017 06:16:28 ******/ ALTER TABLE [dbo].[gam14] ADD DEFAULT ((0)) FOR [gal04i] GO /****** Object: Default [DF__gam14__gal05i__1FCDBCEB] Script Date: 06/12/2017 06:16:28 ******/ ALTER TABLE [dbo].[gam14] ADD DEFAULT ((0)) FOR [gal05i] GO /****** Object: Default [DF__gam14__gal06i__20C1E124] Script Date: 06/12/2017 06:16:28 ******/ ALTER TABLE [dbo].[gam14] ADD DEFAULT ((0)) FOR [gal06i] GO /****** Object: Default [DF__gam28__ratio__3C69FB99] Script Date: 06/12/2017 06:16:28 ******/ ALTER TABLE [dbo].[gam28] ADD DEFAULT ((0)) FOR [ratio] GO /****** Object: Default [DF__gam29__gax03i__2818EA29] Script Date: 06/12/2017 06:16:28 ******/ ALTER TABLE [dbo].[gam29] ADD DEFAULT ((0)) FOR [gax03i] GO /****** Object: Default [DF__gbm01__gba41d__3F466844] Script Date: 06/12/2017 06:16:28 ******/ ALTER TABLE [dbo].[gbm01] ADD DEFAULT (getdate()) FOR [gba41d] GO /****** Object: Default [DF__gbm20__gbt18d__60A75C0F] Script Date: 06/12/2017 06:16:28 ******/ ALTER TABLE [dbo].[gbm20] ADD DEFAULT (getdate()) FOR [gbt18d] GO /****** Object: Default [DF__gcm01__FCardType__344B4D92] Script Date: 06/12/2017 06:16:28 ******/ ALTER TABLE [dbo].[gcm01] ADD DEFAULT ((0)) FOR [FCardTypeId] GO /****** Object: Default [DF__gcm03__gcc12f__6B24EA82] Script Date: 06/12/2017 06:16:28 ******/ ALTER TABLE [dbo].[gcm03] ADD DEFAULT ((1)) FOR [gcc12f] GO /****** Object: Default [DF__gcm06__gcf27i__75A278F5] Script Date: 06/12/2017 06:16:28 ******/ ALTER TABLE [dbo].[gcm06] ADD DEFAULT ((0)) FOR [gcf27i] GO /****** Object: Default [DF__gcm06__gcf28i__76969D2E] Script Date: 06/12/2017 06:16:28 ******/ ALTER TABLE [dbo].[gcm06] ADD DEFAULT ((0)) FOR [gcf28i] GO /****** Object: Default [DF__gcm06__FId__381BDE76] Script Date: 06/12/2017 06:16:28 ******/ /****** Object: Default [DF__gcm11__gcm31d__2196D523] Script Date: 06/12/2017 06:16:28 ******/ ALTER TABLE [dbo].[gcm11] ADD DEFAULT ((0)) FOR [gcm31d] GO /****** Object: Default [DF__gcm12__gcn38f__02FC7413] Script Date: 06/12/2017 06:16:28 ******/ ALTER TABLE [dbo].[gcm12] ADD DEFAULT ((0)) FOR [gcn38f] GO /****** Object: Default [DF__gcm12__gcn39f__03F0984C] Script Date: 06/12/2017 06:16:28 ******/ ALTER TABLE [dbo].[gcm12] ADD DEFAULT ((0)) FOR [gcn39f] GO /****** Object: Default [DF__gcm13__gco10i__06CD04F7] Script Date: 06/12/2017 06:16:28 ******/ ALTER TABLE [dbo].[gcm13] ADD DEFAULT ((2)) FOR [gco10i] GO /****** Object: Default [DF__gcm21__gcu10c__1EDB2F60] Script Date: 06/12/2017 06:16:28 ******/ ALTER TABLE [dbo].[gcm21] ADD DEFAULT (CONVERT([varchar](19),getdate(),(120))) FOR [gcu10c] GO /****** Object: Default [DF__gdm01__gda38i__19DFD96B] Script Date: 06/12/2017 06:16:28 ******/ ALTER TABLE [dbo].[gdm01] ADD DEFAULT ((1)) FOR [gda38i] GO /****** Object: Default [DF__gdm01__gda39d__1AD3FDA4] Script Date: 06/12/2017 06:16:28 ******/ ALTER TABLE [dbo].[gdm01] ADD DEFAULT (getdate()) FOR [gda39d] GO /****** Object: Default [DF__gdm01__gda11i__1BC821DD] Script Date: 06/12/2017 06:16:28 ******/ ALTER TABLE [dbo].[gdm01] ADD DEFAULT ((0)) FOR [gda11i] GO /****** Object: Default [DF__gdm01__gda40i__396E5EB4] Script Date: 06/12/2017 06:16:28 ******/ ALTER TABLE [dbo].[gdm01] ADD DEFAULT ((1)) FOR [gda40i] GO /****** Object: Default [DF__gdm01__FId__1F5030AC] Script Date: 06/12/2017 06:16:28 ******/ /****** Object: Default [DF__gdm01__FNo__204454E5] Script Date: 06/12/2017 06:16:28 ******/ /****** Object: Default [DF__gdm06__FId__2AC1E358] Script Date: 06/12/2017 06:16:28 ******/ /****** Object: Default [DF__gdm07__FId__2BB60791] Script Date: 06/12/2017 06:16:28 ******/ /****** Object: Default [DF__gdm07__FMasterId__2CAA2BCA] Script Date: 06/12/2017 06:16:28 ******/ ALTER TABLE [dbo].[gdm07] ADD DEFAULT ((0)) FOR [FMasterId] GO /****** Object: Default [DF__gdm07__FPayTypeI__2D9E5003] Script Date: 06/12/2017 06:16:28 ******/ ALTER TABLE [dbo].[gdm07] ADD DEFAULT ((0)) FOR [FPayTypeId] GO /****** Object: Default [DF__gdm07__FCardType__2E92743C] Script Date: 06/12/2017 06:16:28 ******/ ALTER TABLE [dbo].[gdm07] ADD DEFAULT ((0)) FOR [FCardTypeId] GO /****** Object: Default [DF__gfm01__gfa70i__42E1EEFE] Script Date: 06/12/2017 06:16:28 ******/ ALTER TABLE [dbo].[gfm01] ADD DEFAULT ((0)) FOR [gfa70i] GO /****** Object: Default [DF__gfm01__gfa72i__2EF0D041] Script Date: 06/12/2017 06:16:28 ******/ ALTER TABLE [dbo].[gfm01] ADD DEFAULT ((1)) FOR [gfa72i] GO /****** Object: Default [DF__gfm01__FId__2320C190] Script Date: 06/12/2017 06:16:28 ******/ /****** Object: Default [DF__gfm01__FNo__2414E5C9] Script Date: 06/12/2017 06:16:28 ******/ /****** Object: Default [DF__ggm01__gga82i__531856C7] Script Date: 06/12/2017 06:16:28 ******/ ALTER TABLE [dbo].[ggm01] ADD DEFAULT ((1)) FOR [gga82i] GO /****** Object: Default [DF__ghm01__gha12c__634EBE90] Script Date: 06/12/2017 06:16:28 ******/ ALTER TABLE [dbo].[ghm01] ADD DEFAULT ('') FOR [gha12c] GO /****** Object: Default [DF__gim01__gia11c__69FBBC1F] Script Date: 06/12/2017 06:16:28 ******/ ALTER TABLE [dbo].[gim01] ADD DEFAULT ('') FOR [gia11c] GO /****** Object: Default [DF__gim20__gim15d__72E607DB] Script Date: 06/12/2017 06:16:28 ******/ ALTER TABLE [dbo].[gim20] ADD DEFAULT (getdate()) FOR [gim15d] GO /****** Object: Default [DF__gim22__did__77AABCF8] Script Date: 06/12/2017 06:16:28 ******/ ALTER TABLE [dbo].[gim22] ADD DEFAULT ((0)) FOR [did] GO /****** Object: Default [DF__gnm03__gnc30c__265B8A40] Script Date: 06/12/2017 06:16:28 ******/ ALTER TABLE [dbo].[gnm03] ADD DEFAULT ((0)) FOR [gnc30c] GO /****** Object: Default [DF__gsm02__gsb06i__2610A626] Script Date: 06/12/2017 06:16:28 ******/ ALTER TABLE [dbo].[gsm02] ADD DEFAULT ((1)) FOR [gsb06i] GO /****** Object: Default [DF__gsm02__FId__26F15274] Script Date: 06/12/2017 06:16:28 ******/ /****** Object: Default [DF__gtc04__gte12i__10AB74EC] Script Date: 06/12/2017 06:16:28 ******/ ALTER TABLE [dbo].[gtc04] ADD DEFAULT ((0)) FOR [gte12i] GO /****** Object: Default [DF__gtc05__gtf19c__228AF95C] Script Date: 06/12/2017 06:16:28 ******/ ALTER TABLE [dbo].[gtc05] ADD DEFAULT ((0)) FOR [gtf19c] GO /****** Object: Default [DF__gtc06__gth20c__247341CE] Script Date: 06/12/2017 06:16:28 ******/ ALTER TABLE [dbo].[gtc06] ADD DEFAULT ((0)) FOR [gth20c] GO /****** Object: Default [DF__gtc08__gtj12i__1387E197] Script Date: 06/12/2017 06:16:28 ******/ ALTER TABLE [dbo].[gtc08] ADD DEFAULT ((0)) FOR [gtj12i] GO /****** Object: Default [DF__gwm01__gwa02i__5B78929E] Script Date: 06/12/2017 06:16:28 ******/ ALTER TABLE [dbo].[gwm01] ADD DEFAULT ((0)) FOR [gwa02i] GO /****** Object: Default [DF__gwm01__gwa03i__5C6CB6D7] Script Date: 06/12/2017 06:16:28 ******/ ALTER TABLE [dbo].[gwm01] ADD DEFAULT ((0)) FOR [gwa03i] GO /****** Object: Default [DF__gzm01__gza07i__6E8B6712] Script Date: 06/12/2017 06:16:29 ******/ ALTER TABLE [dbo].[gzm01] ADD DEFAULT ((1)) FOR [gza07i] GO /****** Object: Default [DF__ham01__haa45d__7908F585] Script Date: 06/12/2017 06:16:29 ******/ ALTER TABLE [dbo].[ham01] ADD DEFAULT (getdate()) FOR [haa45d] GO /****** Object: Default [DF__ham01__FId__25FD2E3B] Script Date: 06/12/2017 06:16:29 ******/ /****** Object: Default [DF_hcm01_operation_operateTime] Script Date: 06/12/2017 06:16:29 ******/ ALTER TABLE [dbo].[hcm01_operation] ADD CONSTRAINT [DF_hcm01_operation_operateTime] DEFAULT (getdate()) FOR [operateTime] GO /****** Object: Default [DF__hcm02__hcb07i__22FF2F51] Script Date: 06/12/2017 06:16:29 ******/ ALTER TABLE [dbo].[hcm02] ADD DEFAULT ((0)) FOR [hcb07i] GO /****** Object: Default [DF__hcm02__hcb08i__23F3538A] Script Date: 06/12/2017 06:16:29 ******/ ALTER TABLE [dbo].[hcm02] ADD DEFAULT ((0)) FOR [hcb08i] GO /****** Object: Default [DF_hcm02_operation_operateTime] Script Date: 06/12/2017 06:16:29 ******/ ALTER TABLE [dbo].[hcm02_operation] ADD CONSTRAINT [DF_hcm02_operation_operateTime] DEFAULT (getdate()) FOR [operateTime] GO /****** Object: Default [DF__hdm01__hda18c__2B947552] Script Date: 06/12/2017 06:16:29 ******/ ALTER TABLE [dbo].[hdm01] ADD DEFAULT ('') FOR [hda18c] GO /****** Object: Default [DF__hem01__hea51i__237F1D95] Script Date: 06/12/2017 06:16:29 ******/ ALTER TABLE [dbo].[hem01] ADD DEFAULT ((0)) FOR [hea51i] GO /****** Object: Default [DF__lrm01__gra09c__3D491139] Script Date: 06/12/2017 06:16:29 ******/ ALTER TABLE [dbo].[lrm01] ADD DEFAULT ('') FOR [gra09c] GO /****** Object: Default [DF__lrm01__gra10d__3E3D3572] Script Date: 06/12/2017 06:16:29 ******/ ALTER TABLE [dbo].[lrm01] ADD DEFAULT ('') FOR [gra10d] GO /****** Object: Default [DF__lrm01__gra11t__3F3159AB] Script Date: 06/12/2017 06:16:29 ******/ ALTER TABLE [dbo].[lrm01] ADD DEFAULT ('') FOR [gra11t] GO /****** Object: Default [DF__lrm01__gra12c__40257DE4] Script Date: 06/12/2017 06:16:29 ******/ ALTER TABLE [dbo].[lrm01] ADD DEFAULT ('') FOR [gra12c] GO /****** Object: Default [DF__lrm01__gra13c__4119A21D] Script Date: 06/12/2017 06:16:29 ******/ ALTER TABLE [dbo].[lrm01] ADD DEFAULT ('') FOR [gra13c] GO /****** Object: Default [DF__lrm01__gra14f__420DC656] Script Date: 06/12/2017 06:16:29 ******/ ALTER TABLE [dbo].[lrm01] ADD DEFAULT ((0)) FOR [gra14f] GO /****** Object: Default [DF__lrm01__gra15f__4301EA8F] Script Date: 06/12/2017 06:16:29 ******/ ALTER TABLE [dbo].[lrm01] ADD DEFAULT ((0)) FOR [gra15f] GO /****** Object: Default [DF__lrm01__gra16c__43F60EC8] Script Date: 06/12/2017 06:16:29 ******/ ALTER TABLE [dbo].[lrm01] ADD DEFAULT ('') FOR [gra16c] GO /****** Object: Default [DF__lrm01__gra17c__44EA3301] Script Date: 06/12/2017 06:16:29 ******/ ALTER TABLE [dbo].[lrm01] ADD DEFAULT ('') FOR [gra17c] GO /****** Object: Default [DF__lrm01__gra18i__45DE573A] Script Date: 06/12/2017 06:16:29 ******/ ALTER TABLE [dbo].[lrm01] ADD DEFAULT ((0)) FOR [gra18i] GO /****** Object: Default [DF__lrm01__gra19c__46D27B73] Script Date: 06/12/2017 06:16:29 ******/ ALTER TABLE [dbo].[lrm01] ADD DEFAULT ('') FOR [gra19c] GO /****** Object: Default [DF__lrm01__gra20c__47C69FAC] Script Date: 06/12/2017 06:16:29 ******/ ALTER TABLE [dbo].[lrm01] ADD DEFAULT ('') FOR [gra20c] GO /****** Object: Default [DF__lrm01__gra21f__48BAC3E5] Script Date: 06/12/2017 06:16:29 ******/ ALTER TABLE [dbo].[lrm01] ADD DEFAULT ((0)) FOR [gra21f] GO /****** Object: Default [DF__lrm01__gra22f__49AEE81E] Script Date: 06/12/2017 06:16:29 ******/ ALTER TABLE [dbo].[lrm01] ADD DEFAULT ((0)) FOR [gra22f] GO /****** Object: Default [DF__lrm01__gra23i__4AA30C57] Script Date: 06/12/2017 06:16:29 ******/ ALTER TABLE [dbo].[lrm01] ADD DEFAULT ((0)) FOR [gra23i] GO /****** Object: Default [DF__lrm01__gra93c__4B973090] Script Date: 06/12/2017 06:16:29 ******/ ALTER TABLE [dbo].[lrm01] ADD DEFAULT ('') FOR [gra93c] GO /****** Object: Default [DF__lrm01__gra94d__4C8B54C9] Script Date: 06/12/2017 06:16:29 ******/ ALTER TABLE [dbo].[lrm01] ADD DEFAULT ('') FOR [gra94d] GO /****** Object: Default [DF__lrm01__gra95c__4D7F7902] Script Date: 06/12/2017 06:16:29 ******/ ALTER TABLE [dbo].[lrm01] ADD DEFAULT ('') FOR [gra95c] GO /****** Object: Default [DF__lrm01__gra96d__4E739D3B] Script Date: 06/12/2017 06:16:29 ******/ ALTER TABLE [dbo].[lrm01] ADD DEFAULT ('') FOR [gra96d] GO /****** Object: Default [DF__lrm01__gra97t__4F67C174] Script Date: 06/12/2017 06:16:29 ******/ ALTER TABLE [dbo].[lrm01] ADD DEFAULT ('') FOR [gra97t] GO /****** Object: Default [DF__lrm02__grb20i__52442E1F] Script Date: 06/12/2017 06:16:29 ******/ ALTER TABLE [dbo].[lrm02] ADD DEFAULT ((0)) FOR [grb20i] GO /****** Object: Default [DF__lrm02__grb21c__53385258] Script Date: 06/12/2017 06:16:29 ******/ ALTER TABLE [dbo].[lrm02] ADD DEFAULT ('') FOR [grb21c] GO /****** Object: Default [DF__ltm01__gta07c__5614BF03] Script Date: 06/12/2017 06:16:29 ******/ ALTER TABLE [dbo].[ltm01] ADD DEFAULT ('') FOR [gta07c] GO /****** Object: Default [DF__ltm01__gta08d__5708E33C] Script Date: 06/12/2017 06:16:29 ******/ ALTER TABLE [dbo].[ltm01] ADD DEFAULT ('') FOR [gta08d] GO /****** Object: Default [DF__ltm01__gta09c__57FD0775] Script Date: 06/12/2017 06:16:29 ******/ ALTER TABLE [dbo].[ltm01] ADD DEFAULT ('') FOR [gta09c] GO /****** Object: Default [DF__ltm01__gta13c__58F12BAE] Script Date: 06/12/2017 06:16:29 ******/ ALTER TABLE [dbo].[ltm01] ADD DEFAULT ('') FOR [gta13c] GO /****** Object: Default [DF__ltm01__gta14c__59E54FE7] Script Date: 06/12/2017 06:16:29 ******/ ALTER TABLE [dbo].[ltm01] ADD DEFAULT ('') FOR [gta14c] GO /****** Object: Default [DF__ltm01__gta15c__5AD97420] Script Date: 06/12/2017 06:16:29 ******/ ALTER TABLE [dbo].[ltm01] ADD DEFAULT ('') FOR [gta15c] GO /****** Object: Default [DF__ltm01__gta16t__5BCD9859] Script Date: 06/12/2017 06:16:29 ******/ ALTER TABLE [dbo].[ltm01] ADD DEFAULT ('') FOR [gta16t] GO /****** Object: Default [DF__ltm01__gta17f__5CC1BC92] Script Date: 06/12/2017 06:16:29 ******/ ALTER TABLE [dbo].[ltm01] ADD DEFAULT ('') FOR [gta17f] GO /****** Object: Default [DF__ltm01__gta18f__5DB5E0CB] Script Date: 06/12/2017 06:16:29 ******/ ALTER TABLE [dbo].[ltm01] ADD DEFAULT ('') FOR [gta18f] GO /****** Object: Default [DF__ltm01__gta19c__5EAA0504] Script Date: 06/12/2017 06:16:29 ******/ ALTER TABLE [dbo].[ltm01] ADD DEFAULT ('') FOR [gta19c] GO /****** Object: Default [DF__ltm01__gta20i__5F9E293D] Script Date: 06/12/2017 06:16:29 ******/ ALTER TABLE [dbo].[ltm01] ADD DEFAULT ((0)) FOR [gta20i] GO /****** Object: Default [DF__ltm01__gta93c__60924D76] Script Date: 06/12/2017 06:16:29 ******/ ALTER TABLE [dbo].[ltm01] ADD DEFAULT ('') FOR [gta93c] GO /****** Object: Default [DF__ltm01__gta94d__618671AF] Script Date: 06/12/2017 06:16:29 ******/ ALTER TABLE [dbo].[ltm01] ADD DEFAULT ('') FOR [gta94d] GO /****** Object: Default [DF__ltm02__gtb07f__6462DE5A] Script Date: 06/12/2017 06:16:29 ******/ ALTER TABLE [dbo].[ltm02] ADD DEFAULT ((0)) FOR [gtb07f] GO /****** Object: Default [DF__ltm02__gtb08f__65570293] Script Date: 06/12/2017 06:16:29 ******/ ALTER TABLE [dbo].[ltm02] ADD DEFAULT ((0)) FOR [gtb08f] GO /****** Object: Default [DF__ltm02__gtb09f__664B26CC] Script Date: 06/12/2017 06:16:29 ******/ ALTER TABLE [dbo].[ltm02] ADD DEFAULT ((0)) FOR [gtb09f] GO /****** Object: Default [DF__ltm02__gtb10f__673F4B05] Script Date: 06/12/2017 06:16:29 ******/ ALTER TABLE [dbo].[ltm02] ADD DEFAULT ((0)) FOR [gtb10f] GO /****** Object: Default [DF__ltm02__gtb11f__68336F3E] Script Date: 06/12/2017 06:16:29 ******/ ALTER TABLE [dbo].[ltm02] ADD DEFAULT ((0)) FOR [gtb11f] GO /****** Object: Default [DF__ltm02__gtb12f__69279377] Script Date: 06/12/2017 06:16:29 ******/ ALTER TABLE [dbo].[ltm02] ADD DEFAULT ((0)) FOR [gtb12f] GO /****** Object: Default [DF__ltm02__gtb13i__6A1BB7B0] Script Date: 06/12/2017 06:16:29 ******/ ALTER TABLE [dbo].[ltm02] ADD DEFAULT ((0)) FOR [gtb13i] GO /****** Object: Default [DF__ltm02__gtb14f__6B0FDBE9] Script Date: 06/12/2017 06:16:29 ******/ ALTER TABLE [dbo].[ltm02] ADD DEFAULT ((1)) FOR [gtb14f] GO /****** Object: Default [DF__lvm01__gva02d__6FD49106] Script Date: 06/12/2017 06:16:29 ******/ ALTER TABLE [dbo].[lvm01] ADD DEFAULT ('') FOR [gva02d] GO /****** Object: Default [DF__lvm01__gva04c__70C8B53F] Script Date: 06/12/2017 06:16:29 ******/ ALTER TABLE [dbo].[lvm01] ADD DEFAULT ('') FOR [gva04c] GO /****** Object: Default [DF__lvm01__gva07i__71BCD978] Script Date: 06/12/2017 06:16:29 ******/ ALTER TABLE [dbo].[lvm01] ADD DEFAULT ((0)) FOR [gva07i] GO /****** Object: Default [DF__lvm01__gva08c__72B0FDB1] Script Date: 06/12/2017 06:16:29 ******/ ALTER TABLE [dbo].[lvm01] ADD DEFAULT ('') FOR [gva08c] GO /****** Object: Default [DF__lvm01__gva09i__73A521EA] Script Date: 06/12/2017 06:16:29 ******/ ALTER TABLE [dbo].[lvm01] ADD DEFAULT ((0)) FOR [gva09i] GO /****** Object: Default [DF__lvm01__gva93c__74994623] Script Date: 06/12/2017 06:16:29 ******/ ALTER TABLE [dbo].[lvm01] ADD DEFAULT ('') FOR [gva93c] GO /****** Object: Default [DF__lvm01__gva94d__758D6A5C] Script Date: 06/12/2017 06:16:29 ******/ ALTER TABLE [dbo].[lvm01] ADD DEFAULT ('') FOR [gva94d] GO /****** Object: Default [DF__lvm02__gvb04f__7869D707] Script Date: 06/12/2017 06:16:29 ******/ ALTER TABLE [dbo].[lvm02] ADD DEFAULT ((0)) FOR [gvb04f] GO /****** Object: Default [DF__lvm02__gvb06f__795DFB40] Script Date: 06/12/2017 06:16:29 ******/ ALTER TABLE [dbo].[lvm02] ADD DEFAULT ((0)) FOR [gvb06f] GO /****** Object: Default [DF__lvm02__gvb07f__7A521F79] Script Date: 06/12/2017 06:16:29 ******/ ALTER TABLE [dbo].[lvm02] ADD DEFAULT ((0)) FOR [gvb07f] GO /****** Object: Default [DF__lvm02__gvb12f__7B4643B2] Script Date: 06/12/2017 06:16:29 ******/ ALTER TABLE [dbo].[lvm02] ADD DEFAULT ((0)) FOR [gvb12f] GO /****** Object: Default [DF__lvm02__gvb13i__7C3A67EB] Script Date: 06/12/2017 06:16:29 ******/ ALTER TABLE [dbo].[lvm02] ADD DEFAULT ((0)) FOR [gvb13i] GO /****** Object: Default [DF__lym02__gyb13i__00FF1D08] Script Date: 06/12/2017 06:16:29 ******/ ALTER TABLE [dbo].[lym02] ADD DEFAULT ((0)) FOR [gyb13i] GO /****** Object: Default [DF__member_en__mep05__2EFAF1E2] Script Date: 06/12/2017 06:16:29 ******/ ALTER TABLE [dbo].[member_enter_parameter] ADD DEFAULT ((1)) FOR [mep05i] GO /****** Object: Default [DF__member_en__mep06__2FEF161B] Script Date: 06/12/2017 06:16:29 ******/ ALTER TABLE [dbo].[member_enter_parameter] ADD DEFAULT ((30)) FOR [mep06i] GO /****** Object: Default [DF__member_en__mep07__30E33A54] Script Date: 06/12/2017 06:16:29 ******/ ALTER TABLE [dbo].[member_enter_parameter] ADD DEFAULT ((180)) FOR [mep07i] GO /****** Object: Default [DF__modulestr__versi__50C5FA01] Script Date: 06/12/2017 06:16:29 ******/ ALTER TABLE [dbo].[modulestruct] ADD DEFAULT ('2000.0') FOR [version] GO /****** Object: Default [DF__modulestr__modul__51BA1E3A] Script Date: 06/12/2017 06:16:29 ******/ ALTER TABLE [dbo].[modulestruct] ADD DEFAULT ((0)) FOR [modulegroupid] GO /****** Object: Default [DF__modulestr__modul__52AE4273] Script Date: 06/12/2017 06:16:29 ******/ ALTER TABLE [dbo].[modulestruct] ADD DEFAULT ((0)) FOR [moduletype] GO /****** Object: Default [DF__modulestr__IsRep__53A266AC] Script Date: 06/12/2017 06:16:29 ******/ ALTER TABLE [dbo].[modulestruct] ADD DEFAULT ((0)) FOR [IsReport] GO /****** Object: Default [DF__openpolic__billf__1BE81D6E] Script Date: 06/12/2017 06:16:29 ******/ ALTER TABLE [dbo].[openpolicy_pay_together] ADD DEFAULT ((0)) FOR [billflag] GO /****** Object: Default [DF__personpre__ppt05__3AA1AEB8] Script Date: 06/12/2017 06:16:29 ******/ ALTER TABLE [dbo].[personprecept] ADD DEFAULT (getdate()) FOR [ppt05d] GO /****** Object: Default [DF__personpre__ppt11__3B95D2F1] Script Date: 06/12/2017 06:16:29 ******/ ALTER TABLE [dbo].[personprecept] ADD DEFAULT ((0)) FOR [ppt11i] GO /****** Object: Default [DF__precept__pre15d__36D11DD4] Script Date: 06/12/2017 06:16:29 ******/ ALTER TABLE [dbo].[precept] ADD DEFAULT (getdate()) FOR [pre15d] GO /****** Object: Default [DF__precept__pre22i__37C5420D] Script Date: 06/12/2017 06:16:29 ******/ ALTER TABLE [dbo].[precept] ADD DEFAULT ((0)) FOR [pre22i] GO /****** Object: Default [DF__precept_l__pre10__33F4B129] Script Date: 06/12/2017 06:16:29 ******/ ALTER TABLE [dbo].[precept_log] ADD DEFAULT (getdate()) FOR [pre10d] GO /****** Object: Default [DF__productpr__pro13__20A2B0EA] Script Date: 06/12/2017 06:16:29 ******/ ALTER TABLE [dbo].[productprecept] ADD DEFAULT ((0)) FOR [pro13i] GO /****** Object: Default [DF_pt_operation_operateTime] Script Date: 06/12/2017 06:16:29 ******/ ALTER TABLE [dbo].[pt_operation] ADD CONSTRAINT [DF_pt_operation_operateTime] DEFAULT (getdate()) FOR [operateTime] GO /****** Object: Default [DF__recover_g__gga82__150615B5] Script Date: 06/12/2017 06:16:29 ******/ ALTER TABLE [dbo].[recover_ggm01] ADD DEFAULT ((1)) FOR [gga82i] GO /****** Object: Default [DF__recover_ggm__new__1229A90A] Script Date: 06/12/2017 06:16:29 ******/ ALTER TABLE [dbo].[recover_ggm01_master] ADD DEFAULT ((0)) FOR [new] GO /****** Object: Default [DF__sysstruct__versi__4DE98D56] Script Date: 06/12/2017 06:16:29 ******/ ALTER TABLE [dbo].[sysstruct] ADD DEFAULT ('2000') FOR [version] GO /****** Object: Default [DF__TInventor__FComp__4669FDCD] Script Date: 06/12/2017 06:16:29 ******/ /****** Object: Default [DF__work_deta__share__21D600EE] Script Date: 06/12/2017 06:16:29 ******/ ALTER TABLE [dbo].[work_detail] ADD DEFAULT ((1)) FOR [share_rate] GO