[Upgrade]
OldVersion=2.3.0
NewVersion=3.2.0
StepCount=230
noTranSteps=219,220
[Step1]
Line1=IF OBJECT_ID('st_pd_CardStatusRep') IS NOT NULL DROP PROCEDURE st_pd_CardStatusRep
[Step2]
Line1=CREATE PROCEDURE st_pd_CardStatusRep
Line2=   @UserNo   VARCHAR(40),   --用戶編號
Line3=   @DeptID   VARCHAR(8000), --出納員編號
Line4=   @DeptName VARCHAR(8000), --出納員名稱
Line5=   @DateBgn  DATETIME,      --統計起始日期
Line6=   @DateEnd  DATETIME       --統計終止日期
Line7=AS
Line8=-- DECLARE @DeptID VARCHAR(8000),@DeptName VARCHAR(8000),@DateBgn DATETIME,@DateEnd DATETIME,@UserNo VARCHAR(40)
Line9=-- SELECT @DeptID='AllDepartments',@DeptName='所有部門', @DateBgn='2005-01-01',@DateEnd=GETDATE(),@UserNo='Admin'
Line10= DECLARE @SDeptID VARCHAR(20),@Pos INT
Line11=   DECLARE @sBgnDate VARCHAR(16), @sEndDate VARCHAR(16)
Line12=   SET @sBgnDate = '' +  CONVERT(CHAR(16), @DateBgn, 20)  + ''
Line13=   SET @sEndDate = '' +  CONVERT(CHAR(16), @DateEnd, 20)  + ''
Line14=  -- SET @DateEnd=DATEADD(second, -1, @DateEnd + 1)
Line15=   IF @DeptID <> 'AllDepartments' --選擇的不是所有部門
Line16=   BEGIN   
Line17=      --1.取得選擇的部門并插入到臨時表#SelectDept中
Line18=      IF OBJECT_ID('tempdb..#SelectDept') IS NOT NULL DROP TABLE #SelectDept
Line19=      SELECT Dept_ID INTO #SelectDept FROM dbo.ST_Department WHERE 1=0
Line20=      WHILE (@DeptID <> '' )
Line21=      BEGIN
Line22=         SET @Pos = CHARINDEX(',', @DeptID)
Line23=         IF @Pos = 0
Line24=         BEGIN
Line25=            SET @SDeptID=@DeptID
Line26=            SET @DeptID=''
Line27=         END
Line28=         ELSE
Line29=         BEGIN
Line30=            SET @SDeptID=SUBSTRING(@DeptID, 1, @Pos - 1)
Line31=            SET @DeptID=SUBSTRING(@DeptID, @Pos + 1, 8000)
Line32=         END
Line33=         INSERT INTO #SelectDept(Dept_ID) VALUES(@SDeptID)
Line34=      END
Line35=      --2.查詢符合條件的記錄
Line36=      SELECT @DeptName[Select_Dept],@DateBgn[Date_Bgn],@DateEnd[Date_End],Dept_Name,COUNT(*)[Card_Count],
Line37=             SUM(CASE WHEN Card_State=0 THEN 1 ELSE 0 END)[Using_Count],
Line38=             SUM(CASE WHEN Card_State=1 THEN 1 ELSE 0 END)[Loss_Count],
Line39=             SUM(CASE WHEN Card_State=2 THEN 1 ELSE 0 END)[Logout_Count]
Line40=        FROM (SELECT Person_ID,Card_State,Card_NewDate FROM dbo.ST_Card
Line41=               UNION ALL SELECT Person_ID,2[Card_Sate],Del_Date[Card_NewDate] FROM dbo.ST_DeleteCard
Line42=             ) A
Line43=        LEFT JOIN dbo.ST_Person B ON B.Person_ID=A.Person_ID
Line44=        LEFT JOIN dbo.ST_Department C ON C.Dept_ID=B.Dept_ID
Line45=        WHERE CONVERT(CHAR(16),  A.Card_NewDate, 20)  >=  @sBgnDate and 
Line46=	          CONVERT(CHAR(16),  A.Card_NewDate, 20) <=  @sEndDate 
Line47=         AND C.Is_Del=0 AND C.Dept_ID IN (SELECT Dept_ID FROM #SelectDept)
Line48=       GROUP BY C.Dept_Name
Line49=   END
Line50=   ELSE --選擇的是所有部門
Line51=   BEGIN
Line52=      SELECT @DeptName[Select_Dept],@sBgnDate[Date_Bgn],@sEndDate[Date_End],Dept_Name,COUNT(*)[Card_Count],
Line53=             SUM(CASE WHEN Card_State=0 THEN 1 ELSE 0 END)[Using_Count],
Line54=             SUM(CASE WHEN Card_State=1 THEN 1 ELSE 0 END)[Loss_Count],
Line55=             SUM(CASE WHEN Card_State=2 THEN 1 ELSE 0 END)[Logout_Count]
Line56=        FROM (SELECT Person_ID,Card_State,Card_NewDate FROM dbo.ST_Card
Line57=               UNION ALL SELECT Person_ID,2[Card_Sate],Del_Date[Card_NewDate] FROM dbo.ST_DeleteCard
Line58=             ) A
Line59=        LEFT JOIN dbo.ST_Person B ON B.Person_ID=A.Person_ID
Line60=        LEFT JOIN dbo.ST_Department C ON C.Dept_ID=B.Dept_ID
Line61=        WHERE CONVERT(CHAR(16),  A.Card_NewDate, 20)  >=  @sBgnDate and 
Line62=	          CONVERT(CHAR(16),  A.Card_NewDate, 20) <=  @sEndDate 
Line63=         AND C.Is_Del=0 AND C.Dept_ID IN (SELECT Dept_ID FROM ST_DeptRight WHERE Check_State=0 AND User_No=@UserNo)
Line64=       GROUP BY C.Dept_Name
Line65=   END 
[Step3]
Line1=UPDATE ST_SysParam SET Param_Name='0:計次;1:計份;2:鍵值;3:金額;4:菜譜;6:自定義單價',Param_Value='0;1;2;3;4;6' WHERE Param_Code='MODE_SF' 
Line2=IF NOT EXISTS(SELECT Menu_ID FROM dbo.ST_MenuInfo WHERE Menu_ID=191)
Line3=   INSERT INTO dbo.ST_MenuInfo(Menu_ID,System_No,Menu_No,Menu_Name,Menu_Hint,Para_ID,Call_Mode,Form_Name,ShortKey,
Line4=                               Menu_Index,Image_Index,Separator,InToolBar,Remark)
Line5=   SELECT 191,'ST','ST_0416','注銷人員明細表','注銷人員明細表',24,1,'TfrmDelDetail',NULL,16,-1,0,0,NULL
[Step4]
System=SK
Line1=IF OBJECT_ID('dbo.SK_CardType') IS NULL
Line2=CREATE TABLE dbo.SK_CardType(
Line3=	Type_No       varchar(3) NOT NULL,	
Line4=	Discount_Info varchar(100) NULL,
Line5=  Max_Fund	     money NOT NULL DEFAULT 99999,
Line6=	Max_DayFund   money NOT NULL DEFAULT 99999,
Line7=	LimitTimes    int NOT NULL DEFAULT 0,
Line8=	Discount      int NOT NULL DEFAULT 100,
Line9=	Remark        varchar(40) NULL,
Line10=  PRIMARY KEY (Type_No ASC)
Line11=)
[Step5]
System=SK
Line1=IF OBJECT_ID('dbo.SK_DiscountInfo') IS NULL
Line2=CREATE TABLE dbo.SK_DiscountInfo(
Line3=	Discount_No   varchar(20) NOT NULL,
Line4=	Discount_Name varchar(40) NOT NULL,
Line5=	Week_Info     tinyint NOT NULL,
Line6=	Time_Bgn1     varchar(5) NULL,
Line7=	Time_End1     varchar(5) NULL,
Line8=	Time_Bgn2     varchar(5) NULL,
Line9=	Time_End2     varchar(5) NULL,
Line10=	Time_Bgn3     varchar(5) NULL,
Line11=	Time_End3     varchar(5) NULL,
Line12=	Remark        varchar(40) NULL,
Line13=	Week_Value    varchar(20) NULL,
Line14=  PRIMARY KEY (Discount_No ASC)
Line15=)
[Step6]
System=SK
Line1=IF OBJECT_ID('dbo.SK_SysParam') IS NULL
Line2=CREATE TABLE dbo.SK_SysParam(
Line3=       Param_ID    int IDENTITY,
Line4=       Param_Code  varchar(20) NOT NULL,
Line5=       Param_Name  varchar(40) NOT NULL,
Line6=       Param_Value varchar(100) NOT NULL,
Line7=       PRIMARY KEY (Param_ID ASC)
Line8=)
[Step7]
System=SK
Line1=INSERT INTO dbo.ST_MenuInfo(Menu_ID,System_No,Menu_No,Menu_Name,Menu_Hint,Para_ID,Call_Mode,Form_Name,ShortKey,
Line2=                        Menu_Index,Image_Index,Separator,InToolBar,Remark)
Line3=SELECT * FROM (SELECT 201[Menu_ID],'SK'[System_No],'SK_0400'[Menu_No],'系統參數'[Menu_Name],'系統參數'[Menu_Hint],143[Para_ID],2[Call_Mode],
Line4=                      'TfrmSKSysParams'[Form_Name],NULL[ShortKey],0[Menu_Index],3[Image_Index],1[Separator],0[InToolBar],NULL[Remark]
Line5=                UNION ALL SELECT 202,'SK','SK_10','業務功能',NULL,-1,NULL,NULL,NULL,2,0,0,0,NULL
Line6=                UNION ALL SELECT 203,'SK','SK_1001','卡類設置','卡類設置',202,2,'TfrmSKCardType',NULL,1,4,0,0,NULL
Line7=                UNION ALL SELECT 204,'SK','SK_1002','終端機設置','終端機設置',202,2,'TfrmSKMocSet',NULL,2,5,1,0,NULL
Line8=                UNION ALL SELECT 205,'SK','SK_1003','采集記錄','采集記錄',202,2,'TfrmSKDownload',NULL,3,9,1,0,NULL
Line9=                UNION ALL SELECT 206,'SK','SK_1004','統計報表',NULL,202,9,NULL,NULL,3,0,0,0,NULL
Line10=                UNION ALL SELECT 207,'SK','SK_100401','消費明細表','消費明細表',206,1,'TfrmSKConsume',NULL,1,20,1,0,NULL
Line11=                UNION ALL SELECT 208,'SK','SK_100402','人員消費匯總表','人員消費匯總表',206,1,'TfrmSKPsnTotal',NULL,2,19,0,0,NULL
Line12=                UNION ALL SELECT 209,'SK','SK_100403','機器消費匯總表','機器消費匯總表',206,1,'TfrmSKMocTotal',NULL,3,19,0,0,NULL
Line13=                UNION ALL SELECT 210,'SK','SK_100404','用途消費匯總表','用途消費匯總表',206,1,'TfrmSKUseTotal',NULL,4,19,0,0,NULL
Line14=                UNION ALL SELECT 211,'SK','SK_1005','打折時段設置','打折時段設置',202,2,'TfrmDiscountInfo',NULL,2,5,1,0,NULL
Line15=							) A
Line16= WHERE A.Menu_ID NOT IN (SELECT Menu_ID FROM dbo.ST_MenuInfo)
[Step8]
System=SK
Line1=INSERT INTO dbo.SK_SysParam(Param_Code,Param_Name,Param_Value)
Line2=SELECT * FROM (SELECT 'MocType'[Param_Code],'硬件類型'[Param_Name],'1'[Param_Value]
Line3=                UNION ALL SELECT 'COM','通訊串口','COM1'
Line4=                UNION ALL SELECT 'BaudRate','通訊波特率','9600'
Line5=                UNION ALL SELECT 'MOCUse','水控機用途','0-娛樂;1-開水;2-洗浴;3-機房;4-洗衣;5-用電'
Line6=                UNION ALL SELECT 'MOCUnit','水控機計量單位','0-秒;1-分;2-小時;3-天;4-次;5-脈沖'
Line7=                UNION ALL SELECT 'MOCMode','水控機計費模式','1-計時;2-計次;3-計流量'
Line8=              ) A
Line9=WHERE A.Param_Code NOT IN (SELECT Param_Code FROM dbo.SK_SysParam)
[Step9]
System=SK
Line1=IF OBJECT_ID('SK_vi_Download') IS NOT NULL
Line2=   DROP VIEW SK_vi_Download
[Step10]
System=SK
Line1=CREATE VIEW dbo.SK_vi_Download
Line2=AS
Line3=   SELECT Person_No,Person_Name,Card_No,Consume_Time,Consume_Fund,
Line4=          CASE WHEN A.Consume_Mode=1 THEN '計時' WHEN A.Consume_Mode=2 THEN '計次' ELSE '計量' END [Consume_Mode],
Line5=          Consume_Copy,Consume_TimeLen,Consume_Capacity,A.MOC_No,MOC_Name,Card_Balance
Line6=     FROM XF_Consume A
Line7=     LEFT JOIN ST_Person B ON B.Person_ID=A.Person_ID
Line8=     LEFT JOIN ST_Moc C ON C.MOC_No=A.MOC_No
Line9=    WHERE A.System_No='SK'
[Step11]
Line1=IF COL_LENGTH('ST_CardType', 'Is_OnlySubsidy') IS NULL
Line2=   ALTER TABLE ST_CardType ADD Is_OnlySubsidy BIT DEFAULT 0
[Step12]
Line1=IF COL_LENGTH('ST_Moc', 'Download_Status') IS NULL
Line2=   ALTER TABLE ST_Moc ADD Download_Status TINYINT DEFAULT 0
[Step13]
Line1=IF COL_LENGTH('ST_Moc', 'Download_Time') IS NULL
Line2=   ALTER TABLE ST_Moc ADD Download_Time DATETIME
[Step14]
Line1=UPDATE ST_CardType SET Is_OnlySubsidy=0 WHERE Is_OnlySubsidy IS NULL
[Step15]
Line1=IF OBJECT_ID('dbo.st_pd_PersonalBalanceRep') IS NOT NULL
Line2=   DROP PROCEDURE dbo.st_pd_PersonalBalanceRep
[Step16]
Line1=CREATE PROCEDURE st_pd_PersonalBalanceRep
Line2=   @DeptNo  VARCHAR(40),      --部門編號
Line3=   @UserNo  VARCHAR(40),       --用戶編號
Line4=   @IsDel    BIT             --只否顯示注銷人員 
Line5=AS
Line6=DECLARE   @AccountDate Varchar(10)   --財務結算日期
Line7=IF @IsDel =0
Line8=BEGIN   
Line9=   SELECT @AccountDate=Param_Value FROM dbo.ST_SysParam Where Param_Code = 'SettleDate'
Line10=   SELECT C.Person_No,C.Person_Name,B.Dept_No,B.Dept_Name,ISNULL(Card_Balance,0)[Card_Balance],Card_No,
Line11=          ISNULL(Supply_Fund,0)[Supply_Fund],ISNULL(Consume_Fund,0)[Consume_Fund],
Line12=          ISNULL(G.Subsidy_Fund,0)[Subsidy_Fund],ISNULL(Take_Fund,0)[Take_Fund],
Line13=          ISNULL(Transfer_Fund,0)[Transfer_Fund],
Line14=          ISNULL(Supply_Fund,0)+ISNULL(G.Subsidy_Fund,0)-(ISNULL(Consume_Fund,0)+ISNULL(Take_Fund,0)+ISNULL(Transfer_Fund,0)+ISNULL(Card_Balance+ISNULL(M.Back_Fund,0),0))[Mny_Balance]
Line15=     FROM ST_Card A
Line16=     LEFT JOIN ST_Person C ON C.Person_ID=A.Person_ID
Line17=     LEFT JOIN ST_Department B ON B.Dept_ID=C.Dept_ID
Line18=     --0.計算銷卡的退金額
Line19=     LEFT JOIN (SELECT ROUND(ISNULL(SUM(Back_Fund), 0), 2)[Back_Fund],Person_ID 
Line20=                FROM ST_DeleteCard 
Line21=                GROUP BY Person_ID
Line22=                ) M ON M.Person_ID=C.Person_ID
Line23=     --1.計算存款金額
Line24=     LEFT JOIN (SELECT SUM(Supply_Fund)[Supply_Fund],Person_ID 
Line25=                 FROM  (SELECT SUM(Supply_Fund)[Supply_Fund],Person_ID 
Line26=                        FROM ST_SupplyFund WHERE Supply_Date >= @AccountDate
Line27=                        GROUP BY Person_ID
Line28=            				Union All
Line29=            		      SELECT Supply_Fund,Person_ID 
Line30=                        FROM ST_PersonAccount 
Line31=                       ) A
Line32=                GROUP BY Person_ID
Line33=                ) D ON D.Person_ID=C.Person_ID
Line34=     --2.計算消費金額
Line35=     LEFT JOIN (SELECT SUM(Consume_Fund)[Consume_Fund],Person_ID
Line36=                 FROM (SELECT SUM(Consume_Fund)[Consume_Fund],Person_ID 
Line37=                       FROM XF_Consume WHERE Consume_Time >= @AccountDate + ' 23:59:59'
Line38=                       GROUP BY Person_ID
Line39=                       Union All
Line40=                       SELECT (EM_ConsumeFund + XF_ConsumeFund)[Consume_Fund],Person_ID 
Line41=                       FROM ST_PersonAccount
Line42=                       ) A
Line43=               GROUP BY Person_ID
Line44=          ) E ON E.Person_ID=C.Person_ID
Line45=     --3.計算補貼金額
Line46=     LEFT JOIN (SELECT SUM(Subsidy_Fund)[Subsidy_Fund],Person_ID
Line47=                FROM (SELECT SUM(Subsidy_Fund)[Subsidy_Fund],Person_ID 
Line48=                       FROM ST_Subsidy  WHERE Subsidy_Date >= @AccountDate
Line49=                       GROUP BY Person_ID
Line50=      					Union All
Line51=                      SELECT Subsidy_Fund,Person_ID 
Line52=                      FROM ST_PersonAccount
Line53=                     ) A
Line54=                 GROUP BY Person_ID
Line55=               ) G ON G.Person_ID=C.Person_ID
Line56=     --4.計算取款金額
Line57=     LEFT JOIN (SELECT SUM(Take_Fund)[Take_Fund],Person_ID 
Line58=                FROM (SELECT SUM(Take_Fund)[Take_Fund],Person_ID
Line59=                      FROM ST_TakeFund  WHERE Take_Date >= @AccountDate
Line60=                      GROUP BY Person_ID					
Line61=                    Union All
Line62=                      SELECT Take_Fund,Person_ID 
Line63=                      FROM ST_PersonAccount
Line64=                     ) A
Line65=                 GROUP BY Person_ID
Line66=               ) H ON H.Person_ID=C.Person_ID
Line67=      --6.計算轉帳金額
Line68=     LEFT JOIN (SELECT SUM(Transfer_Fund)[Transfer_Fund],Person_ID 
Line69=                FROM (SELECT SUM(Transfer_Fund)[Transfer_Fund],Person_ID
Line70=                      FROM ST_TransferAccount WHERE Transfer_Date >= @AccountDate
Line71=                      GROUP BY Person_ID
Line72=                    Union All
Line73=                      SELECT Transfer_Fund,Person_ID 
Line74=                      FROM ST_PersonAccount
Line75=                      ) A
Line76=                GROUP BY Person_ID
Line77=               ) N ON N.Person_ID=C.Person_ID
Line78=   WHERE B.Dept_No LIKE @DeptNo+'%' AND B.Dept_ID IN (SELECT Dept_ID FROM ST_DeptRight WHERE Check_State=0 AND User_No=@UserNo)
Line79=   ORDER BY B.Dept_No,C.Person_No
Line80=END
Line81=ELSE
Line82=BEGIN
Line83=   SELECT @AccountDate=Param_Value FROM dbo.ST_SysParam Where Param_Code = 'SettleDate'
Line84=   SELECT A.Person_No,A.Person_Name,B.Dept_No,B.Dept_Name,ISNULL(Card_Balance,0)[Card_Balance],Card_No,
Line85=          ISNULL(Supply_Fund,0)[Supply_Fund],ISNULL(Consume_Fund,0)[Consume_Fund],
Line86=          ISNULL(G.Subsidy_Fund,0)[Subsidy_Fund],ISNULL(Take_Fund,0)[Take_Fund],
Line87=          ISNULL(Transfer_Fund,0)[Transfer_Fund],
Line88=          ISNULL(Supply_Fund,0)+ISNULL(G.Subsidy_Fund,0)-(ISNULL(Consume_Fund,0)+ISNULL(Take_Fund,0)+ISNULL(Transfer_Fund,0)+ISNULL(Card_Balance+ISNULL(M.Back_Fund,0),0))[Mny_Balance]
Line89=     FROM ST_Person A
Line90=     LEFT JOIN  ST_Card C ON A.Person_ID=C.Person_ID
Line91=     LEFT JOIN ST_Department B ON A.Dept_ID=B.Dept_ID
Line92=     --0.計算銷卡的退金額
Line93=     LEFT JOIN (SELECT ROUND(ISNULL(SUM(Back_Fund), 0), 2)[Back_Fund],Person_ID 
Line94=                FROM ST_DeleteCard 
Line95=                GROUP BY Person_ID
Line96=                ) M ON M.Person_ID=C.Person_ID
Line97=     --1.計算存款金額
Line98=     LEFT JOIN (SELECT SUM(Supply_Fund)[Supply_Fund],Person_ID 
Line99=                 FROM  (SELECT SUM(Supply_Fund)[Supply_Fund],Person_ID 
Line100=                        FROM ST_SupplyFund WHERE Supply_Date >= @AccountDate
Line101=                        GROUP BY Person_ID
Line102=            				Union All
Line103=            		      SELECT Supply_Fund,Person_ID 
Line104=                        FROM ST_PersonAccount 
Line105=                       ) A
Line106=                GROUP BY Person_ID
Line107=                ) D ON D.Person_ID=C.Person_ID
Line108=     --2.計算消費金額
Line109=     LEFT JOIN (SELECT SUM(Consume_Fund)[Consume_Fund],Person_ID
Line110=                 FROM (SELECT SUM(Consume_Fund)[Consume_Fund],Person_ID 
Line111=                       FROM XF_Consume WHERE Consume_Time >= @AccountDate + ' 23:59:59'
Line112=                       GROUP BY Person_ID
Line113=                       Union All
Line114=                       SELECT (EM_ConsumeFund + XF_ConsumeFund)[Consume_Fund],Person_ID 
Line115=                       FROM ST_PersonAccount
Line116=                       ) A
Line117=               GROUP BY Person_ID
Line118=          ) E ON E.Person_ID=C.Person_ID
Line119=     --3.計算補貼金額
Line120=     LEFT JOIN (SELECT SUM(Subsidy_Fund)[Subsidy_Fund],Person_ID
Line121=                FROM (SELECT SUM(Subsidy_Fund)[Subsidy_Fund],Person_ID 
Line122=                       FROM ST_Subsidy  WHERE Subsidy_Date >= @AccountDate
Line123=                       GROUP BY Person_ID
Line124=      					Union All
Line125=                      SELECT Subsidy_Fund,Person_ID 
Line126=                      FROM ST_PersonAccount
Line127=                     ) A
Line128=                 GROUP BY Person_ID
Line129=               ) G ON G.Person_ID=C.Person_ID
Line130=     --4.計算取款金額
Line131=     LEFT JOIN (SELECT SUM(Take_Fund)[Take_Fund],Person_ID 
Line132=                FROM (SELECT SUM(Take_Fund)[Take_Fund],Person_ID
Line133=                      FROM ST_TakeFund  WHERE Take_Date >= @AccountDate
Line134=                      GROUP BY Person_ID					
Line135=                    Union All
Line136=                      SELECT Take_Fund,Person_ID 
Line137=                      FROM ST_PersonAccount
Line138=                     ) A
Line139=                 GROUP BY Person_ID
Line140=               ) H ON H.Person_ID=C.Person_ID
Line141=      --6.計算轉帳金額
Line142=     LEFT JOIN (SELECT SUM(Transfer_Fund)[Transfer_Fund],Person_ID 
Line143=                FROM (SELECT SUM(Transfer_Fund)[Transfer_Fund],Person_ID
Line144=                      FROM ST_TransferAccount WHERE Transfer_Date >= @AccountDate
Line145=                      GROUP BY Person_ID
Line146=                    Union All
Line147=                      SELECT Transfer_Fund,Person_ID 
Line148=                      FROM ST_PersonAccount
Line149=                      ) A
Line150=                GROUP BY Person_ID
Line151=               ) N ON N.Person_ID=C.Person_ID
Line152=   WHERE B.Dept_No LIKE @DeptNo+'%' AND B.Dept_ID IN (SELECT Dept_ID FROM ST_DeptRight WHERE Check_State=0 AND User_No=@UserNo)
Line153=   ORDER BY B.Dept_No,A.Person_No
Line154=END
[Step17]
System=SF
Line1=INSERT INTO dbo.ST_MenuInfo(Menu_ID,System_No,Menu_No,Menu_Name,Menu_Hint,Para_ID,Call_Mode,Form_Name,ShortKey,
Line2=                        Menu_Index,Image_Index,Separator,InToolBar,Remark)
Line3=SELECT * FROM (SELECT 80[Menu_ID],'SF'[System_No],'SF_05'[Menu_No],'系統管理'[Menu_Name],NULL[Menu_Hint],-1[Para_ID],NULL[Call_Mode],
Line4=                      NULL[Form_Name],NULL[ShortKey],'1'[Menu_Index],NULL[Image_Index],NULL[Separator],0[InToolBar],NULL[Remark]                                                            
Line5=                UNION ALL SELECT 193,'SF','SF_0405','食堂餐別匯總表','食堂餐別匯總表',97,'1','TfrmHallTotalRep',NULL,'5',13,'0',0,NULL
Line6=                UNION ALL SELECT 194,'SF','SF_0406','機台餐別匯總表','機台餐別匯總表',97,'1','TfrmMocHallTotalRep',NULL,'6',13,'0',0,NULL
Line7=                UNION ALL SELECT 195,'SF','SF_0407','人員機台匯總表','人員機台匯總表',97,'1','TfrmPsnMocRpt',NULL,'7',13,'0',0,NULL 
Line8=		      UNION ALL SELECT 196,'SF','SF_0408','品種消費匯總表(商品)','品種消費匯總表(商品)',97,'1','TfrmWareSumRpt',NULL,'8',63,'0',0,NULL
Line9=							) A
Line10= WHERE A.Menu_ID NOT IN (SELECT Menu_ID FROM dbo.ST_MenuInfo)
[Step18]
System=KQ
Line1=/****考勤****/
Line2=-- =============================================
Line3=-- 考勤月報
Line4=-- =============================================
Line5=IF OBJECT_ID('dbo.KQ_Monthly') IS NOT NULL
Line6= IF COL_LENGTH('KQ_Monthly','L') IS NULL
Line7=   ALTER TABLE dbo.KQ_Monthly ADD 
Line8=       H                   smallint NULL,
Line9=       I                    smallint NULL,
Line10=       J                    smallint NULL,
Line11=       K                    smallint NULL,
Line12=       L                    smallint NULL
[Step19]
System=KQ
Line1=-- =============================================
Line2=-- 考勤月報歷史表
Line3=-- =============================================
Line4=IF OBJECT_ID('dbo.KQ_MonthlyHistory') IS NOT NULL
Line5= IF COL_LENGTH('KQ_MonthlyHistory','L') IS NULL 
Line6=   ALTER TABLE dbo.KQ_MonthlyHistory ADD 
Line7=       H                   smallint NULL,
Line8=       I                    smallint NULL,
Line9=       J                    smallint NULL,
Line10=       K                    smallint NULL,
Line11=       L                    smallint NULL
[Step20]
System=KQ
Line1=-- =============================================
Line2=-- 考勤年報
Line3=-- =============================================
Line4=IF OBJECT_ID('dbo.KQ_Yearly') IS NOT NULL
Line5=  IF COL_LENGTH('KQ_Yearly','L') IS NULL 
Line6=   ALTER TABLE dbo.KQ_Yearly ADD 
Line7=       H                   smallint NULL,
Line8=       I                    smallint NULL,
Line9=       J                    smallint NULL,
Line10=       K                    smallint NULL,
Line11=       L                    smallint NULL
[Step21]

[Step22]

[Step23]
System=SK
Line1=IF OBJECT_ID('SK_Pd_PsnTotal') IS NOT NULL
Line2=   DROP PROCEDURE SK_Pd_PsnTotal
[Step24]
System=SK
Line1=-----水控消費匯總查詢
Line2=CREATE PROCEDURE dbo.SK_Pd_PsnTotal
Line3=@DateBgn VARCHAR(20),		--起始日期
Line4=@DateEnd VARCHAR(20),		--結束日期
Line5=@Moc_No VARCHAR(3000),		--機器編號
Line6=@Dept VARCHAR(20),			--部門條件
Line7=@Type integer,             --匯總類別
Line8=@UseType VARCHAR(20),      --用途分類
Line9=@isDept bit
Line10=AS
Line11=DECLARE @sql1 VARCHAR(3000), @sql2 VARCHAR(3000),@sql3 VARCHAR(3000)
Line12=SET   @sql2 = '  And Consume_Time >= ' + @DateBgn + ' And  Consume_Time <=' + @DateEnd
Line13=IF @Type = 1   --部門+時間匯總
Line14=BEGIN
Line15=IF @Dept = ''    --不選擇部門,只按日期范圍
Line16=BEGIN
Line17=   Select c.Person_No, c.Person_Name ,d.Dept_Name, c.TotalTimeLen, c.TotalCopy, c.TotalCapacity, c.TotalFund From(
Line18=   (Select a.*, b.Person_No,b.Person_Name,b.Dept_ID From (
Line19=   Select Person_ID, sum(Consume_TimeLen) as TotalTimeLen, sum(Consume_Copy) as TotalCopy, 
Line20=   sum(Consume_Capacity) as TotalCapacity, sum(Consume_Fund) as TotalFund
Line21=   From XF_Consume Where System_No = 'SK'   And Consume_Time >=  @DateBgn  And  Consume_Time <= @DateEnd  
Line22=   Group By Person_ID) a 
Line23=   Left Join ST_Person b On a.Person_Id = b.Person_Id) c
Line24=   Left Join ST_Department d on c.Dept_ID = d.Dept_ID)
Line25=END
Line26=ELSE   -- 	帶部門或人員
Line27=BEGIN
Line28=if  @isDept = 1               --匯總部門
Line29=BEGIN
Line30=   Select d.Person_No, d.Person_Name, d.Dept_Name,
Line31=   sum(c.Consume_TimeLen) as TotalTimeLen,sum(c.Consume_Copy) as TotalCopy,sum(c.Consume_Capacity) as TotalCapacity,
Line32=   sum(c.Consume_Fund) as TotalFund
Line33=   From 
Line34=   (SELECT a.Person_id,a.Person_no,a.Person_name,b.Dept_Name FROM 
Line35=   ST_Person a  LEFT JOIN  ST_Department b ON a.Dept_id = b.Dept_id  WHERE  b.Dept_no  LIKE @Dept) d
Line36=   Left Join XF_Consume c  
Line37=   ON c.Person_Id = d.Person_id WHERE c.System_No = 'SK' 
Line38=   And c.Consume_Time >=  @DateBgn  And c. Consume_Time <= @DateEnd  
Line39=   Group by d.Person_ID,d.Person_No,d.Person_name,d.Dept_Name
Line40=END
Line41=ELSE              
Line42=BEGIN               ---------單個人員匯總
Line43=   Select c.Person_No, c.Person_Name, d.Dept_Name, c.TotalTimeLen ,c.TotalCopy, c.TotalCapacity,c.TotalFund
Line44=   From 
Line45=   (Select b.Person_No,Person_Name,b.Dept_ID, sum(a.Consume_TimeLen) as TotalTimeLen,sum(a.Consume_Copy) as TotalCopy,sum(a.Consume_Capacity) as TotalCapacity,
Line46=   sum(a.Consume_Fund) as TotalFund From
Line47=   XF_Consume a   Left Join ST_Person b
Line48=   ON a.Person_Id = b.Person_id WHERE a.System_No = 'SK' 
Line49=   And a.Consume_Time >=  @DateBgn  And a. Consume_Time <= @DateEnd   And a.Person_ID = Convert(integer,@Dept)
Line50=   Group by a.Person_Id,b.Person_No,b.Person_name,b.Dept_ID) c 
Line51=   Left Join ST_Department d ON c.Dept_ID = d.Dept_ID
Line52=END
Line53=END
Line54=END
Line55=ELSE   -----------------------------------------按用途或機號
Line56=BEGIN
Line57=IF @Dept = ''    --不選擇部門,只按日期范圍
Line58=BEGIN
Line59=   --匯總機號
Line60=   IF @Moc_No = ''
Line61=      SET @Sql3 =''
Line62=   ELSE
Line63=      SET @sql3 = ' And  Moc_No in (' + @Moc_No + ')'
Line64=   SET  @Sql1 ='
Line65=   Select a.Moc_No, ISNULL(b.Moc_Name, '+ '''''' + ') as Moc_Name, a.TotalTimelen, a.TotalCopy, a.TotalCapacity, a.TotalFund   From (
Line66=   Select Moc_No,  sum(Consume_TimeLen) as TotalTimelen, sum(Consume_Copy) as TotalCopy, 
Line67=   sum(Consume_Capacity) as TotalCapacity, sum(Consume_Fund) as TotalFund
Line68=   From XF_Consume Where System_No = ' + '''SK'''   + @Sql2 + @Sql3      
Line69=    + ' Group by Moc_No) a
Line70=    Left Join  ST_Moc b ON a.Moc_No = b.Moc_No' 
Line71=IF @Type = 3       --按用途
Line72=BEGIN
Line73=   IF @UseType = ''
Line74=     SET @Sql2 = ''
Line75=   ELSE
Line76=     SET @Sql2 =' Where  x.KQ_UseType  IN (' + @UseType + ')'
Line77=   SET @Sql1='
Line78=   Select z.*,
Line79=   ( Case When x.KQ_UseType = 0 Then ' + '''娛樂''' + 
Line80=   ' When x.KQ_UseType = 1 Then ' +  '''開水'''  + 
Line81=   'When x.KQ_UseType = 2 Then ' +  '''洗浴''' +
Line82=   ' When x.KQ_UseType = 3 Then ' + '''機房'''  +
Line83=   'When x.KQ_UseType = 4 Then ' + '''洗衣'''  + 
Line84=   'When x.KQ_UseType = 5 Then ' + '''用電''' + 
Line85=   'ELSE '  + '''其它''' + ' End) as  MocUse  From (' + @Sql1 + ') z Left Join ST_Moc x ON z.Moc_No=x.Moc_No' + @Sql2
Line86=   --執行按用途匯總
Line87=   SET @Sql1='
Line88=   Select  MocUse, Sum(TotalTimeLen) as TotalTimeLen, Sum(TotalCopy) as TotalCopy, Sum(TotalCapacity) as TotalCapacity, 
Line89=   Sum(TotalFund) as TotalFund   From (' + @Sql1   + ') y  Group by  y.MocUse'
Line90=END
Line91=Exec (@Sql1)
Line92=END
Line93=ELSE   ------------------------帶部門的機號或用途匯總
Line94=BEGIN
Line95=   IF @Moc_No = ''
Line96=      SET @Sql3 =''
Line97=   ELSE
Line98=      SET  @sql3 = '  And  Moc_No in (' + @Moc_No + ')'
Line99=SET @Sql1 ='
Line100=   Select e.Moc_No,f.Moc_Name, e.TotalTimeLen,e.TotalCopy,e.TotalCapacity,.e.TotalFund From
Line101=   (Select c.Moc_No, 
Line102=   sum(c.Consume_TimeLen) as TotalTimeLen,sum(c.Consume_Copy) as TotalCopy,sum(c.Consume_Capacity) as TotalCapacity,
Line103=   sum(c.Consume_Fund) as TotalFund
Line104=   From 
Line105=   (SELECT a.Person_id FROM 
Line106=   ST_Person a  LEFT JOIN  ST_Department b ON a.Dept_id = b.Dept_id  WHERE  b.Dept_no  LIKE  ''' + @Dept + ''') d
Line107=   Left Join XF_Consume c  
Line108=   ON c.Person_Id = d.Person_id WHERE c.System_No ='  + '''SK''' +  @sql2 + @sql3
Line109=   + '
Line110=   Group by  c.Moc_No) e Left Join ST_Moc f ON e.Moc_No = f.Moc_No' 
Line111=IF @Type = 3        --按用途
Line112=BEGIN
Line113=   IF @UseType = ''
Line114=     SET @Sql2 = ''
Line115=   ELSE
Line116=     SET @Sql2 = '  Where  x.KQ_UseType  IN (' + @UseType + ')'
Line117=   SET @Sql1='
Line118=   Select z.*,
Line119=   ( Case When x.KQ_UseType = 0 Then ' + '''娛樂''' + 
Line120=   ' When x.KQ_UseType = 1 Then ' +  '''開水'''  + 
Line121=   'When x.KQ_UseType = 2 Then ' +  '''洗浴''' +
Line122=   ' When x.KQ_UseType = 3 Then ' + '''機房'''  +
Line123=   'When x.KQ_UseType = 4 Then ' + '''洗衣'''  + 
Line124=   'When x.KQ_UseType = 5 Then ' + '''用電''' + 
Line125=   'ELSE '  + '''其它''' + ' End) as MocUse  From (' + @Sql1 + ') z Left Join ST_Moc x ON z.Moc_No=x.Moc_No' + @Sql2
Line126=--執行按用途匯總
Line127=   SET @Sql1='
Line128=   Select  MocUse, Sum(TotalTimeLen) as TotalTimeLen, Sum(TotalCopy) as TotalCopy, Sum(TotalCapacity) as TotalCapacity, 
Line129=   Sum(TotalFund) as TotalFund   From (' + @Sql1   + ') y  Group by  y.MocUse'
Line130=END
Line131=Exec (@Sql1)  
Line132=END
Line133=END
[Step25]
System=SK
Line1=IF OBJECT_ID('SK_pd_Consume') IS NOT NULL
Line2=   DROP PROCEDURE SK_pd_Consume
[Step26]
System=SK
Line1=-----水控消費明細查詢
Line2=CREATE PROCEDURE dbo.SK_pd_Consume
Line3=@DateBgn VARCHAR(20),		--起始日期
Line4=@DateEnd VARCHAR(20),		--結束日期
Line5=@Moc_No VARCHAR(3000),		--機器編號
Line6=@Dept VARCHAR(20),			--部門條件
Line7=@isDept bit
Line8=AS
Line9=DECLARE @sql1 VARCHAR(3000), @sql2 VARCHAR(3000),@sql3 VARCHAR(3000)
Line10=DECLARE @BgnDate VARCHAR(10),@EndDate VARCHAR(10)
Line11=IF @Dept = ''    --不選擇部門
Line12=BEGIN
Line13=SET @sql2 = '  And Consume_Time >= ' + @DateBgn + ' And  Consume_Time <=' + @DateEnd
Line14=IF  @Moc_No = ''
Line15=   SET  @sql3 =''
Line16=ELSE
Line17=   SET  @sql3 = '  And Moc_No in (' + @Moc_No + ')'
Line18=SET @sql1='
Line19=   Select e.*,  (CASE WHEN  f.KQ_usetype  = 0 THEN   ' + '''娛樂''' + '  
Line20=   WHEN  f.KQ_usetype  = 1  THEN   ' + '''開水''' +  '  
Line21=   WHEN  f.KQ_usetype  = 2  THEN   ' + '''洗浴''' +'  
Line22=   WHEN  f.KQ_usetype  = 3  THEN   ' + '''機房''' +'
Line23=   WHEN  f.KQ_usetype  = 4  THEN   ' + '''洗衣''' +'
Line24=   WHEN  f.KQ_usetype  = 5  THEN   ' + '''用電''' +
Line25=   ' ELSE '  + '''其它'''  + ' END) as UseType From (
Line26=   Select c.*,d.Dept_Name From
Line27=   (Select a.*,b.Person_No,b.Person_Name,b.Dept_ID From
Line28=   (
Line29=   (Select Person_ID,
Line30=   (CASE WHEN  Consume_Mode = 1 THEN   ' + '''計時''' + '  
Line31=      WHEN  Consume_Mode = 2 THEN   ' + '''計次''' + 
Line32=    ' ELSE '  + '''計流量'''  + ' END) as Consume_Mode,
Line33=   Consume_Time,Consume_Fund,Moc_No,
Line34=   (CASE WHEN  Consume_Mode = 1 THEN Consume_TimeLen 
Line35=      WHEN  Consume_Mode = 2 THEN Consume_Copy 
Line36=      ELSE Consume_Capacity END) as Consume_Num
Line37=   From XF_Consume Where System_No = ' + '''SK''' + 
Line38=   @sql2 + @Sql3 +
Line39=   '  ) a
Line40=   Left Join ST_Person b On a.Person_ID = b.Person_ID) 
Line41=   ) c 
Line42=   Left Join ST_Department d on c.dept_ID = d.dept_ID' + 
Line43='   ' ) e Left Join ST_Moc f ON e.Moc_No = f.Moc_No''
Line44=Exec (@Sql1)
Line45=END
Line46=ELSE                --選擇了部門
Line47=BEGIN
Line48=IF @isDept = 1 
Line49=BEGIN
Line50=IF  @Moc_No = ''
Line51=   SET  @sql3 =''
Line52=ELSE
Line53=   SET  @sql3 = '  And c.Moc_No in (' + @Moc_No + ')'
Line54=SET @Sql1='
Line55=   Select e.*,  (CASE WHEN  f.KQ_usetype  = 0 THEN   ' + '''娛樂''' + '  
Line56=   WHEN  f.KQ_usetype  = 1  THEN   ' + '''開水''' +  '  
Line57=   WHEN  f.KQ_usetype  = 2  THEN   ' + '''洗浴''' +'  
Line58=   WHEN  f.KQ_usetype  = 3  THEN   ' + '''機房''' +'
Line59=   WHEN  f.KQ_usetype  = 4  THEN   ' + '''洗衣''' +'
Line60=   WHEN  f.KQ_usetype  = 5  THEN   ' + '''用電''' +
Line61=   ' ELSE '  + '''其它'''  + ' END) as UseType From (
Line62=   SELECT  d.Dept_name, c.Person_id, d.Person_no,d.Person_name,
Line63=   (CASE WHEN  c.Consume_Mode = 1 THEN   ' + '''計時''' + '  
Line64=   WHEN  c.Consume_Mode = 2 THEN   ' + '''計次''' + 
Line65=   ' ELSE '  + '''計流量'''  + ' END) as Consume_Mode,
Line66=   c.Consume_Time,c.Consume_Fund,c.Moc_No,
Line67=   (CASE WHEN  c.Consume_Mode = 1 THEN Consume_TimeLen 
Line68=   WHEN  c.Consume_Mode = 2 THEN Consume_Copy 
Line69=   ELSE Consume_Capacity END) as Consume_Num
Line70=   FROM 
Line71=   (SELECT a.Person_id,a.Person_no,a.Person_name,b.Dept_Name FROM 
Line72=   ST_Person a  LEFT JOIN  ST_Department b ON a.Dept_id = b.Dept_id  WHERE  b.Dept_no  LIKE '
Line73=   +  @dept  +  ') d
Line74=   Left Join XF_Consume c  
Line75=   ON c.Person_Id = d.Person_id WHERE c.system_No = ' + '''SK'''  +
Line76=   '  And c.Consume_Time >= ' + @DateBgn + ' And  c.Consume_Time <=' + @DateEnd + @Sql3 +
Line77='   ' ) e Left Join ST_Moc f ON e.Moc_No = f.Moc_No''
Line78=Exec (@sql1)
Line79=END
Line80=ELSE            --查詢單個人員
Line81=BEGIN
Line82=SET @sql2 = '  And Consume_Time >= ' + @DateBgn + ' And  Consume_Time <=' + @DateEnd
Line83=IF  @Moc_No = ''
Line84=   SET  @sql3 =''
Line85=ELSE
Line86=   SET  @sql3 = '  And Moc_No in (' + @Moc_No + ')'
Line87=SET @sql1='
Line88=   Select e.*,  (CASE WHEN  f.KQ_usetype  = 0 THEN   ' + '''娛樂''' + '  
Line89=   WHEN  f.KQ_usetype  = 1  THEN   ' + '''開水''' +  '  
Line90=   WHEN  f.KQ_usetype  = 2  THEN   ' + '''洗浴''' +'  
Line91=   WHEN  f.KQ_usetype  = 3  THEN   ' + '''機房''' +'
Line92=   WHEN  f.KQ_usetype  = 4  THEN   ' + '''洗衣''' +
Line93=   ' ELSE '  + '''用電'''  + ' END) as UseType From (
Line94=   Select c.*,d.Dept_Name From
Line95=   (Select a.*,b.Person_No,b.Person_Name,b.Dept_ID From
Line96=   (
Line97=   (Select Person_ID,
Line98=   (CASE WHEN  Consume_Mode = 1 THEN   ' + '''計時''' + '  
Line99=   WHEN  Consume_Mode = 2 THEN   ' + '''計次''' + 
Line100=   ' ELSE '  + '''計流量'''  + ' END) as Consume_Mode,
Line101=   Consume_Time,Consume_Fund,Moc_No,
Line102=   (CASE WHEN  Consume_Mode = 1 THEN Consume_TimeLen 
Line103=   WHEN  Consume_Mode = 2 THEN Consume_Copy 
Line104=   ELSE Consume_Capacity END) as Consume_Num
Line105=   From XF_Consume Where System_No = ' + '''SK''' + ' And Person_ID =' + @Dept +
Line106=   @sql2  + 
Line107=   @sql3  + 
Line108=   '  ) a
Line109=   Left Join ST_Person b On a.Person_ID = b.Person_ID) 
Line110=   ) c 
Line111=   Left Join ST_Department d on c.dept_ID = d.dept_ID' + 
Line112='   ' ) e Left Join ST_Moc f ON e.Moc_No = f.Moc_No'   '
Line113=Exec(@Sql1)
Line114=END
Line115=ENd
[Step27]
System=KQ
Line1=-- =============================================
Line2=-- 批量處理人員的請假登記
Line3=-- =============================================
Line4=IF EXISTS(SELECT name FROM sysobjects WHERE name = 'KQ_pd_BatchUpdateLeave' AND type = 'P')
Line5=    DROP PROCEDURE dbo.KQ_pd_BatchUpdateLeave
[Step28]
System=KQ
Line1=CREATE PROCEDURE dbo.KQ_pd_BatchUpdateLeave
Line2=  @TblName VARCHAR(30),--表名KQ_Leave或者KQ_LeaveHistory
Line3=  @SelPersonTmp VARCHAR(30),--部門樹中所選擇的人員
Line4=  @StartDate DATETIME,--開始日期（請假日期）
Line5=  @EndDate DATETIME,--結束日期
Line6=  @Kind_No VARCHAR(30),--假種編號
Line7=  @Leave_Hour Real,--請假時長
Line8=  @Leave_Sect Real,--請假時段
Line9=  @IsOutLeave int,--是否段外
Line10=  @User_No VARCHAR(20),--操作員
Line11=  @Remark VARCHAR(300),
Line12=  @Row Int output --返回成功記錄數
Line13=AS
Line14=DECLARE  @SQL Varchar(8000);
Line15=DECLARE  @LeaveDate DATETIME
Line16=DECLARE  @lsLeaveDate VARCHAR(10)
Line17=DECLARE  @W VARCHAR(2),@D VARCHAR(2)
Line18=  SET @SQL=''
Line19=  SET @ROW=0
Line20=  SET @LeaveDate=@StartDate 
Line21=  WHILE @LeaveDate <= @EndDate --循環處理
Line22=  BEGIN
Line23=    SET @lsLeaveDate= CONVERT(VARCHAR,@LeaveDate,120)
Line24=    SET @D=SUBSTRING(CONVERT(Varchar,@LeaveDate,126),9,2) 
Line25=    SET @W= CASE WHEN DATEPART(dw ,@LeaveDate)-1=0 THEN 7 ELSE  DATEPART(dw ,@LeaveDate)-1 End --取得星期
Line26=    SET @SQL = ' INSERT INTO  '   +  @TblName+'  '+
Line27=              '(Person_ID,Kind_No,Leave_Date,Leave_Hour,IsOutLeave,Leave_Sect,Remark,User_No,Update_Date)'+
Line28=              ' SELECT A.Person_ID,'''+@Kind_No +''' AS Kind_No ,''' +
Line29=              @lsLeaveDate + ''' AS Leave_Date, ' + CONVERT(VARCHAR,@Leave_Hour) + ' AS Leave_Hour,' +
Line30=              CONVERT(VARCHAR,@IsOutLeave)  + ' AS IsOutLeave,' + CONVERT(VARCHAR,@Leave_Sect)+ ' AS Leave_Sect,''' +
Line31=              @Remark + ''' AS Remark,''' +@User_No +''' AS User_No,' +
Line32=              +'Getdate() AS Update_Date '+
Line33=              ' FROM '+ @SelPersonTmp+' A '+
Line34=              ' LEFT JOIN  '  +  @TblName +' B ON A.Person_ID=B.Person_ID'+
Line35=              ' AND Leave_Date='''+@lsLeaveDate+''' AND Kind_No='''+@Kind_No+''''+
Line36=              ' WHERE EXISTS(SELECT Person_ID FROM ( '+
Line37=              ' SELECT Person_ID,(CASE WHEN Rest_Mode=1 THEN '+
Line38=              '              (CASE WHEN SUBSTRING(Rest_Days,CHARINDEX('''+@W+''',Rest_Days)-1,1)<>''.'' AND  '+ 
Line39=                             'CHARINDEX('''+@W+''',Rest_Days)>0 '+
Line40=                             ' THEN 1 ElSE 0 END) '+
Line41=                                ' ELSE CHARINDEX('''+@D+''',Rest_Days) END) AS Rest '+
Line42=              ' FROM dbo.KQ_Person )C WHERE C.Person_ID=A.Person_ID AND C.Rest=0 '+
Line43=                           ' )'+
Line44='              ' AND B.Kind_NO IS NULL''
Line45=   EXEC(@SQL);
Line46=   SET @ROW=@ROW+@@ROWCOUNT
Line47=   SET @LeaveDate=@LeaveDate+1--日期加1
Line48=  end;
[Step29]
System=KQ
Line1=-- =============================================
Line2=-- 更新人員的班次調整
Line3=-- =============================================
Line4=IF EXISTS(SELECT name FROM sysobjects WHERE name = 'KQ_pd_UpdateAdjustClass' AND type = 'P')
Line5=    DROP PROCEDURE dbo.KQ_pd_UpdateAdjustClass
[Step30]
System=KQ
Line1=CREATE PROCEDURE dbo.KQ_pd_UpdateAdjustClass
Line2=  @AClassCode VARCHAR(30),--轉化為班次代號之后的班次臨時表名
Line3=  @FAdjustEnd VARCHAR(30),--最后經處理的臨時表
Line4=  @FAdjustClassTmp VARCHAR(30),--在界面上所操作的當前臨時表
Line5=  @KQ_Adjustclass VARCHAR(30),--班次調整表名（KQ_AdjustClass或者KQ_AdjustClassHistory）
Line6=  @StartDate DATETIME,--表格中開始日期
Line7=  @User_No VARCHAR(20),
Line8=  @Remark VARCHAR(200)
Line9=AS
Line10=DECLARE  @SQL Varchar(8000);
Line11=DECLARE  @SQL_D Varchar(8000);
Line12=DECLARE  @I  Int,@FieldValue Varchar(100),@Field Varchar(20);
Line13=DECLARE  @DATE DateTime
Line14=  SET @I=1 ;
Line15=  SET @SQL=''
Line16=  WHILE  @I< 32
Line17=  BEGIN
Line18=    SET @DATE=@StartDate+@I-1;
Line19=    SET @FieldValue =CONVERT(Varchar(10),@StartDate+@I-1,126);
Line20=    SET @Field=CAST('D'+CAST(@i as Varchar)as Varchar)
Line21=    IF @SQL = ''
Line22=     SET @SQL = 'SELECT Person_ID,'''+CAST(@FieldValue AS VARCHAR)+''' AS Adjust_Date, '+
Line23=                ' A.Class_No,'+ @User_No +' AS User_NO,GetDate() AS Update_Date,'+@Remark +' AS Remark '+
Line24=                ' FROM '+ @FAdjustClassTmp +
Line25=                ' LEFT JOIN '+@AClassCode +'  A on A.Class_Code='+@Field+
Line26='                ' WHERE ISNull('+@Field+','''')<>''''''
Line27=    ELSE
Line28=     SET @SQL = @SQL + ' UNION ALL '+
Line29=             'SELECT Person_ID,'''+CAST(@FieldValue AS VARCHAR)+''' AS Adjust_Date,'+
Line30=                ' A.Class_No,'+ @User_No +' AS User_NO,GetDate() AS Update_Date,'+@Remark +' AS Remark '+
Line31=                ' FROM '+ @FAdjustClassTmp +
Line32=                ' LEFT JOIN '+@AClassCode +' A on A.Class_Code='+@Field+
Line33='                 ' WHERE ISNull('+@Field+','''')<>''''''
Line34=    SET @I=@I+1;
Line35=  END
Line36=-- 
Line37=  SET @SQL='INSERT INTO '+@FAdjustEnd +'(Person_ID,Adjust_Date,Class_No,User_NO,Update_Date,Remark)'+
Line38='           ' SELECT Person_ID,Adjust_Date,Class_No,User_NO,Update_Date,Remark  FROM ('+ @SQL +')A''
Line39=  EXEC(@SQL)
Line40=--刪除不在臨時表的記錄
Line41=  SET @SQL ='DELETE  '+ @KQ_Adjustclass +
Line42=                ' WHERE EXISTS (SELECT Person_ID FROM '+ @FAdjustClassTmp +' A '+
Line43=                               ' WHERE A.Person_ID='+@KQ_Adjustclass+'.Person_ID)'+
Line44=                ' AND NOT EXISTS(SELECT Person_ID FROM '+@FAdjustEnd+' B '+
Line45=                               ' WHERE B.Person_ID='+@KQ_Adjustclass+'.Person_ID '+
Line46=                               ' AND B.ADjust_Date='+@KQ_Adjustclass+'.Adjust_Date)';
Line47=  EXEC(@SQL)
Line48=--修改班次日期相同的記錄
Line49=  SET @SQL ='UPDATE '+ @KQ_Adjustclass +' SET Class_No=A.Class_No,Remark=A.Remark,User_NO=A.User_No'+ 
Line50=            ',Update_Date=A.Update_Date FROM '+@FAdjustEnd+' A  WHERE '+@KQ_AdjustClass+'.Person_ID=A.Person_ID '+
Line51='           ' AND '+@KQ_AdjustClass+'.Adjust_Date=A.Adjust_Date ''
Line52=  EXEC(@SQL)
Line53=--插入臨時表存在，但KQ_AdjustClass中不存在的記錄
Line54=  SET @SQL ='INSERT INTO '+ @KQ_Adjustclass +'(Person_ID,Adjust_Date,Class_No,User_NO,Update_Date,Remark)'+
Line55=          '  SELECT A.Person_ID,CONVERT(Varchar(10),A.Adjust_Date,126),'+
Line56=          '  A.Class_No,A.User_NO,A.Update_Date,A.Remark From '+ @FAdjustEnd+' A  ' +
Line57=          '  LEFT JOIN '+ @KQ_Adjustclass +' B On B.Person_ID=A.Person_ID and B.Adjust_Date=A.Adjust_Date '+
Line58=          '  WHERE ISNULL(B.Class_No,'''')=''''';
Line59=  EXEC(@SQL)
[Step31]
Line1=IF OBJECT_ID('dbo.XF_Subsidy') IS NULL
Line2=  CREATE TABLE dbo.XF_Subsidy (
Line3=    ID_Key int IDENTITY(1, 1),
Line4=    Person_ID INT,
Line5=    Card_No VARCHAR(20),
Line6=    Apply_Time DATETIME,
Line7=    Subsidy_Fund MONEY,
Line8=    Apply_State TINYINT,
Line9=    Moc_No VARCHAR(6),
Line10=    PRIMARY KEY (ID_Key ASC)
Line11=  )
[Step32]
System=MJ
Line1=IF OBJECT_ID('MJ_fn_Card10To8') IS NOT NULL
Line2=   DROP FUNCTION MJ_fn_Card10To8
[Step33]
System=MJ
Line1=CREATE FUNCTION dbo.MJ_fn_Card10To8(@CardNo VARCHAR(12))
Line2=RETURNS VARCHAR(12)
Line3=AS
Line4=BEGIN
Line5=   DECLARE @Result VARCHAR(20),@Converted BIT
Line6=   SELECT @Converted=CAST(Param_Value AS BIT) FROM MJ_SysParam WHERE Param_Code='CARDTOWIGAND26'
Line7=   IF (@Converted = 1)
Line8=   BEGIN
Line9=      IF (@CardNo IS NOT NULL) AND (@CardNo <> '')
Line10=      BEGIN
Line11=         DECLARE @b2 TINYINT, @b1 TINYINT, @b0 TINYINT, @Card BIGINT
Line12=             SET @Card=CAST(@CardNo AS BIGINT)
Line13=             SET @Card=@Card % (256*256*256)
Line14=             SET @b2=@Card / (256*256)
Line15=             SET @Card=@Card % (256*256)
Line16=             SET @b1=@Card / 256
Line17=             SET @b0=@Card % 256
Line18=             SET @Card=@b2 * 100000 + @b1 * 256 + @b0
Line19=         SET @Result=CAST(@Card AS VARCHAR)
Line20=      END
Line21=      ELSE
Line22=         SET @Result=NULL
Line23=   END
Line24=   ELSE
Line25=      SET @Result=@CardNo
Line26=   RETURN @Result
Line27=END
[Step34]
System=SF;LH
Line1=/**2008-01-24**/
Line2=/**售飯**/
Line3=IF OBJECT_ID('dbo.XF_Subsidy') IS NOT NULL
Line4=  IF COL_LENGTH('XF_Subsidy', 'Card_Balance') IS NULL
Line5=    ALTER TABLE XF_Subsidy ADD Card_Balance MONEY NULL DEFAULT 0
[Step35]
Line1=IF EXISTS (SELECT name FROM sysindexes WHERE name = 'IX_XF_Subsidy')
Line2=   DROP INDEX XF_Subsidy.IX_XF_Subsidy
[Step36]
System=SF;LH
Line1=CREATE NONCLUSTERED INDEX IX_XF_Subsidy ON dbo.XF_Subsidy
Line2=	(
Line3=	Card_No,
Line4=	Apply_Time
Line5=	) ON [PRIMARY]
[Step37]
Line1=IF NOT EXISTS(SELECT Param_Code FROM ST_SysParam WHERE Param_Code='SubsidyInterval')
Line2=   INSERT INTO ST_SysParam(Param_Code,Param_Name,Param_Value)
Line3=   VALUES('SubsidyInterval','SubsidyInterval','60')
[Step38]
Line1=-- JenanWang 2008.1.23 支持二次消費餐別單價
Line2=-- HasSencondPrice 值為1表示存在二次餐別單價,同時將SF_Repast表中的Remark作為二次單價
Line3=IF NOT EXISTS(SELECT Param_Code FROM ST_SysParam WHERE Param_Code='HasSencondPrice')
Line4=   INSERT INTO ST_SysParam(Param_Code,Param_Name,Param_Value)
Line5=   VALUES('HasSencondPrice','HasSencondPrice','0')
[Step39]
System=KQ
Line1=/**考勤**/
Line2=IF COL_LENGTH('KQ_Person', 'IsOverTimeAsWorkTime') IS NULL
Line3=  ALTER TABLE KQ_Person ADD 
Line4=       IsHolidayAsWorkTime  bit NULL DEFAULT 0,
Line5=       IsOverTimeAsWorkTime  bit NULL DEFAULT 0
[Step40]
System=KQ
Line1=-- =============================================
Line2=-- 視圖：考勤人員考勤信息與核心平台部門、人員信息關聯
Line3=-- 此視圖作為以下異常情況所關聯的視圖
Line4=-- =============================================
Line5=IF OBJECT_ID('dbo.V_KQPerson') IS NOT NULL DROP VIEW dbo.V_KQPerson
[Step41]
System=KQ
Line1=CREATE VIEW dbo.V_KQPerson
Line2=AS
Line3=SELECT A.*,
Line4=      B.Person_No, 
Line5=      B.Person_Name, 
Line6=      B.Dept_ID, 
Line7=      B.Card_No, 
Line8=      C.Dept_No, 
Line9=      C.Dept_Name
Line10=FROM dbo.KQ_Person A LEFT OUTER JOIN
Line11=      dbo.ST_Person B ON B.Person_ID = A.Person_ID LEFT OUTER JOIN
Line12=      dbo.ST_Department C ON C.Dept_ID = B.Dept_ID
[Step42]
System=KQ
Line1=-- =============================================
Line2=-- 視圖：考勤人員考勤信息與人員考勤信息關聯
Line3=-- =============================================
Line4=IF OBJECT_ID('dbo.V_KQAttendInfo') IS NOT NULL DROP VIEW dbo.V_KQAttendInfo
[Step43]
System=KQ
Line1=CREATE VIEW dbo.V_KQAttendInfo
Line2=AS
Line3=SELECT A.*, 
Line4=      D.Class_Name, 
Line5=      B.Person_Name, 
Line6=      B.Person_No, 
Line7=      B.Card_No, 
Line8=      C.Dept_Name, 
Line9=      C.Dept_No, 
Line10=      C.Dept_ID
Line11=FROM dbo.KQ_Person A
Line12=INNER JOIN dbo.ST_Person B ON A.Person_ID = B.Person_ID 
Line13=INNER JOIN dbo.ST_Department C ON B.Dept_ID = C.Dept_ID 
Line14=LEFT OUTER JOIN dbo.V_KQClassInfo D ON A.Class_No = D.Class_No
Line15=WHERE (A.Person_State = 0)
[Step44]
System=MJ
Line1=/**門禁**/
Line2=IF COL_LENGTH('MJ_ControlInfo ', 'Update_Time') IS NULL
Line3=   ALTER TABLE MJ_ControlInfo ADD Update_Time DATETIME NULL
[Step45]
System=SK
Line1=--水控補貼表
Line2=IF OBJECT_ID('dbo.SK_SubsidyRecord') IS NULL
Line3=CREATE TABLE dbo.SK_SubsidyRecord(
Line4=	Person_No     varchar(20) NOT NULL,
Line5=        SubsidyFund   money NOT NULL,
Line6=	SubsidyDate   DateTime NOT NULL,
Line7=        Moc_No        varchar(50) NOT NULL,
Line8=        Card_No       varchar(50) NOT NULL
Line9=)
[Step46]
System=SK
Line1=--1.水控子系統初始菜單表
Line2=INSERT INTO dbo.ST_MenuInfo(Menu_ID,System_No,Menu_No,Menu_Name,Menu_Hint,Para_ID,Call_Mode,Form_Name,ShortKey,
Line3=                        Menu_Index,Image_Index,Separator,InToolBar,Remark)
Line4=SELECT * FROM (SELECT 143[Menu_ID],'SK'[System_No],'SK_04'[Menu_No],'系統管理'[Menu_Name],NULL[Menu_Hint],-1[Para_ID],NULL[Call_Mode],
Line5=                      NULL[Form_Name],NULL[ShortKey],'1'[Menu_Index],NULL[Image_Index],NULL[Separator],0[InToolBar],NULL[Remark]
Line6=                UNION ALL SELECT 213,'SK','SK_0102','水控補貼機','水控補貼機',137,1,'TfrmSKSubsidyMocSet',NULL,1,3,'0',0,NULL
Line7=							) A
Line8= WHERE A.Menu_ID NOT IN (SELECT Menu_ID FROM dbo.ST_MenuInfo)
[Step47]
System=SK
Line1=--新版水控機所需要使用的菜單
Line2=INSERT INTO dbo.ST_MenuInfo(Menu_ID,System_No,Menu_No,Menu_Name,Menu_Hint,Para_ID,Call_Mode,Form_Name,ShortKey,
Line3=                        Menu_Index,Image_Index,Separator,InToolBar,Remark)
Line4=SELECT * FROM (SELECT 201[Menu_ID],'SK'[System_No],'SK_0400'[Menu_No],'系統參數'[Menu_Name],'系統參數'[Menu_Hint],143[Para_ID],2[Call_Mode],
Line5=                      'TfrmSKSysParams'[Form_Name],NULL[ShortKey],0[Menu_Index],3[Image_Index],1[Separator],0[InToolBar],NULL[Remark]
Line6=                UNION ALL SELECT 212,'SK','SK_1006','水控補貼機','水控補貼機',202,1,'TfrmSKSubsidyMocSet',Null,2,3,1,0,NULL
Line7=							) A
Line8= WHERE A.Menu_ID NOT IN (SELECT Menu_ID FROM dbo.ST_MenuInfo)
[Step48]
System=SF;LH
Line1=INSERT INTO dbo.ST_MenuInfo(Menu_ID,System_No,Menu_No,Menu_Name,Menu_Hint,Para_ID,Call_Mode,Form_Name,ShortKey,
Line2=                        Menu_Index,Image_Index,Separator,InToolBar,Remark)
Line3=SELECT * FROM (SELECT 30[Menu_ID],'ST'[System_No],'ST_0406'[Menu_No],'卡狀態統計表'[Menu_Name],'卡狀態統計表'[Menu_Hint],24[Para_ID],
Line4=                      2[Call_Mode],'TfrmCardStatusRep'[Form_Name],NULL[ShortKey],7[Menu_Index],26[Image_Index],1[Separator],0[InToolBar],NULL[Remark]
Line5=                      UNION ALL SELECT 48,'ST','ST_0417','補貼領取明細','補貼領取明細',  24,1,'TfrmTakeSubsidy',NULL,13,30,0,0,NULL
Line6=              ) A
Line7= WHERE A.Menu_ID NOT IN (SELECT Menu_ID FROM dbo.ST_MenuInfo)
[Step49]
Line1=/**2008-03-20**/
Line2=IF COL_LENGTH('ST_Person','Card_SN') IS NULL 
Line3=  ALTER TABLE ST_Person ADD Card_SN varchar(12)
[Step50]
System=MJ
Line1=IF NOT EXISTS(Select * from MJ_sysParam where Param_Code='ISUSECARD_SN')
Line2=  INSERT INTO  MJ_sysParam(Param_Code,Param_name,Param_Value)
Line3=   VALUES('ISUSECARD_SN','是否用IC卡序列號做門禁卡號','0')
[Step51]
System=MJ
Line1=IF OBJECT_ID('MJ_pd_SynchronizePerson') IS NOT NULL
Line2=   DROP PROCEDURE MJ_pd_SynchronizePerson
[Step52]
System=MJ
Line1=CREATE PROCEDURE MJ_pd_SynchronizePerson
Line2=   (@SystemNo VARCHAR(20),@UserNo VARCHAR(40))--子系統編號
Line3=AS
Line4=   DECLARE @Synchronize TINYINT --是否需要同步標志
Line5=   DECLARE @IsUseCardSN TINYINT --是否使用IC卡SN作為卡號
Line6=	--1.檢查是否需要同步
Line7=   SELECT @Synchronize=Update_State1 FROM ST_SubSystem WHERE System_No=@SystemNo
Line8=   IF @Synchronize=0 
Line9=   BEGIN --需要同步
Line10=      --2.設置重新獲得使用門禁子系統權限的人員的標志
Line11=      UPDATE MJ_Person SET Person_State=0 WHERE Person_State=1
Line12=         AND EXISTS(SELECT Person_ID FROM ST_SubSysRight WHERE Person_ID=MJ_Person.Person_ID AND System_No=@SystemNo) --子系統權限
Line13=         AND EXISTS(SELECT Person_ID FROM ST_Card WHERE Person_ID=MJ_Person.Person_ID) --有卡號人員
Line14=      --3.設置沒有權限使用門禁子系統或在核心平台已經被注銷的人員的標志
Line15=      UPDATE MJ_Person SET Person_State=1 WHERE Person_State=0
Line16=         AND (NOT EXISTS(SELECT Person_ID FROM ST_SubSysRight WHERE Person_ID=MJ_Person.Person_ID AND System_No=@SystemNo) --子系統權限
Line17=          OR EXISTS(SELECT Person_ID FROM ST_Person WHERE Person_ID=MJ_Person.Person_ID AND Is_Del=1)
Line18=          OR NOT EXISTS(SELECT Person_ID FROM ST_Card WHERE Person_ID=MJ_Person.Person_ID)) --無卡號人員
Line19=      --4.插入有使用門禁子系統且門禁信息不存在的人員
Line20=      SELECT @IsUseCardSN=Param_Value FROM MJ_sysParam WHERE Param_Code='ISUSECARD_SN'
Line21=      IF @IsUseCardSN IS NULL SET @IsUseCardSN=0
Line22=      --
Line23=      IF @IsUseCardSN = 0 --使用卡號
Line24=      BEGIN
Line25=         INSERT INTO MJ_Person(Person_ID,Door_Pwd,Person_State,User_No,Update_Date,Acs_Card)
Line26=         SELECT A.Person_ID,'111111',0,@UserNo,GETDATE(),dbo.MJ_fn_Card10To8(A.Card_No) FROM ST_Person A
Line27=           LEFT JOIN MJ_Person B ON B.Person_ID=A.Person_ID
Line28=          WHERE Is_Del=0 AND B.Person_State IS NULL
Line29=            AND EXISTS(SELECT Person_ID FROM ST_SubSysRight WHERE Person_ID=A.Person_ID AND System_No=@SystemNo) --子系統權限
Line30=            AND EXISTS(SELECT Person_ID FROM ST_Card WHERE Person_ID=A.Person_ID) --有卡號人員
Line31=         --4.1轉換變更卡號的卡號
Line32=         UPDATE MJ_Person SET Acs_Card=dbo.MJ_fn_Card10To8(A.Card_No)
Line33=           FROM ST_Person A WHERE MJ_Person.Person_ID=A.Person_ID AND ISNULL(MJ_Person.Acs_Card,0)<>dbo.MJ_fn_Card10To8(A.Card_No)
Line34=     END
Line35=     ELSE  --使用SN
Line36=     BEGIN
Line37=        INSERT INTO MJ_Person(Person_ID,Door_Pwd,Person_State,User_No,Update_Date,Acs_Card)
Line38=        SELECT A.Person_ID,'111111',0,@UserNo,GETDATE(),dbo.MJ_fn_Card10To8(ISNULL(A.Card_SN,0)) FROM ST_Person A
Line39=          LEFT JOIN MJ_Person B ON B.Person_ID=A.Person_ID
Line40=         WHERE Is_Del=0 AND B.Person_State IS NULL
Line41=           AND EXISTS(SELECT Person_ID FROM ST_SubSysRight WHERE Person_ID=A.Person_ID AND System_No=@SystemNo) --子系統權限
Line42=           AND EXISTS(SELECT Person_ID FROM ST_Card WHERE Person_ID=A.Person_ID) --有卡號人員
Line43=        --4.1轉換變更卡號的卡號
Line44=        UPDATE MJ_Person SET Acs_Card=dbo.MJ_fn_Card10To8(ISNULL(A.Card_SN,0))
Line45=          FROM ST_Person A WHERE MJ_Person.Person_ID=A.Person_ID AND ISNULL(MJ_Person.Acs_Card,0)<>dbo.MJ_fn_Card10To8(ISNULL(A.Card_SN,0)) 
Line46=     END
Line47=      --5.更改核心平台同步成功的標志
Line48=      UPDATE ST_SubSystem SET Update_State1=1 WHERE System_No=@SystemNo      
Line49=   END
Line50=   SELECT @Synchronize
[Step53]
System=SF;LH
Line1=IF OBJECT_ID('XF_CollectLog') IS NULL
Line2=   CREATE TABLE XF_CollectLog(Id_Key int IDENTITY(1,1),Log_DateTime DateTime,Log_Msg varchar(255))  
[Step54]
Line1=/**添加一個用戶自定義菜單表**/
Line2=IF OBJECT_ID('dbo.ST_SelfMenu') IS NULL
Line3=  CREATE TABLE dbo.ST_SelfMenu (
Line4=          User_No              varchar(20) NOT NULL,
Line5=          Menu_ID              int NOT NULL,
Line6=          PRIMARY KEY (User_No ASC, Menu_ID ASC)
Line7=  )
[Step55]
Line1=/**增加一個'當前菜單'字段**/
Line2=IF COL_LENGTH('ST_UserInfo ', 'Current_Menu') IS NULL
Line3=  ALTER TABLE ST_UserInfo ADD Current_Menu varchar(3) NULL DEFAULT 'ST'
[Step56]
System=MJ
Line1=/**2008-04-24 **/
Line2=IF OBJECT_ID('dbo.V_MJPersonInfo') IS NOT NULL DROP VIEW dbo.V_MJPersonInfo
[Step57]
System=MJ
Line1=/**人員門禁信息的視圖 **/ 
Line2=CREATE VIEW dbo.V_MJPersonInfo AS
Line3= SELECT A.Person_ID,
Line4=  P.Person_No,
Line5=  P.Person_Name,
Line6=  P.Card_No,
Line7=  P.OnWork_Date,
Line8=  A.Door_Pwd,
Line9=  A.Person_State,
Line10=  P.Dept_ID,
Line11=  D.Dept_No,
Line12=  D.Dept_Name
Line13= FROM dbo.MJ_Person A
Line14= LEFT JOIN dbo.ST_Person P ON A.Person_ID = P.Person_ID
Line15= INNER JOIN dbo.ST_Department D ON P.Dept_ID = D.Dept_ID
Line16= WHERE (A.Person_State = 0)
[Step58]
System=MJ
Line1=/**正常刷卡記錄報表的視圖**/
Line2=IF OBJECT_ID('dbo.V_MJNormalRecord') IS NOT NULL DROP VIEW dbo.V_MJNormalRecord
[Step59]
System=MJ
Line1=/**/
Line2=CREATE VIEW dbo.V_MJNormalRecord AS
Line3= SELECT N.BrushCard_Time, N.Ctrl_No, C.Ctrl_Name, N.Door_No, D.Door_Name,
Line4=  N.Card_No, P.Person_No, P.Person_Name, N.Event_Code, f.Event_Name, N.Is_InDoor,g.Dept_No,g.Dept_Name
Line5=  FROM dbo.MJ_NormalRecord N
Line6=  LEFT OUTER JOIN dbo.MJ_Controller C ON N.Ctrl_No = C.Ctrl_No
Line7=  LEFT OUTER JOIN dbo.MJ_Door D ON N.Ctrl_No = D.Ctrl_No AND N.Door_No = D.Door_No
Line8=  LEFT OUTER JOIN dbo.ST_Person P ON N.Person_ID = P.Person_ID
Line9=  LEFT OUTER JOIN dbo.MJ_EventInfo f ON N.Event_Code = f.Event_Code
Line10=  LEFT OUTER JOIN dbo.ST_DepartMent g on P.Dept_ID = g.Dept_ID
[Step60]
System=MJ
Line1=/**異常記錄報表的視圖**/
Line2=IF OBJECT_ID('dbo.V_MJAbnormRecord') IS NOT NULL DROP VIEW dbo.V_MJAbnormRecord
[Step61]
System=MJ
Line1=/**/
Line2=CREATE VIEW dbo.V_MJAbnormRecord AS
Line3= SELECT A.BrushCard_Time, A.Ctrl_No, C.Ctrl_Name, A.Door_No, D.Door_Name, A.Card_No,
Line4=  P.Person_No, P.Person_Name, f.Event_Name, A.Event_Code, A.Is_InDoor,g.Dept_No,g.Dept_Name
Line5=  FROM dbo.MJ_AbnormRecord A
Line6=  LEFT OUTER JOIN dbo.MJ_Controller C ON A.Ctrl_No = C.Ctrl_No
Line7=  LEFT OUTER JOIN dbo.MJ_Door D ON A.Ctrl_No = D.Ctrl_No And A.Door_No = D.Door_No
Line8=  LEFT OUTER JOIN dbo.ST_Person P ON A.Card_No = P.Card_No
Line9=  LEFT OUTER JOIN dbo.MJ_EventInfo f ON A.Event_Code = f.Event_Code
Line10=  LEFT OUTER JOIN dbo.ST_DepartMent g on P.Dept_ID = g.Dept_ID
[Step62]
Line1=SET QUOTED_IDENTIFIER OFF 
[Step63]
Line1=SET ANSI_NULLS ON 
[Step64]
Line1=/*2008-05-26 IC卡操作卡庫不平衡的問題*/
Line2=IF COL_LENGTH('ST_Card', 'Update_Time') IS NULL
Line3=  ALTER TABLE ST_Card ADD Update_Time DATETIME Default Getdate()
[Step65]
Line1=IF COL_LENGTH('ST_Card', 'IC_Balance') IS NULL
Line2=  ALTER TABLE ST_Card ADD IC_Balance MONEY
[Step66]
Line1=UPDATE ST_Card SET Update_Time=GETDATE(),IC_Balance=Card_Balance -Subsidy_Fund
Line2= WHERE Update_Time IS NULL
[Step67]

[Step68]

[Step69]
Line1=IF OBJECT_ID('XF_pd_CardSupplySmall') IS NOT NULL
Line2=   DROP PROCEDURE XF_pd_CardSupplySmall
[Step70]
Line1=CREATE PROCEDURE dbo.XF_pd_CardSupplySmall
Line2=   (@PersonID INT,        --人員標識號
Line3=    @SupplyFund MONEY,    --充值金額
Line4=    @Balance MONEY,       --轉帳后的余額
Line5=    @CardNo VARCHAR(40),  --操作人員卡號
Line6=    @SupplyDate DATETIME, --充值日期
Line7=    @MocNo VARCHAR(20)    --終端機編號
Line8=   )
Line9=AS  
Line10=   --0.更新IC卡余額 
Line11=   UPDATE ST_Card 
Line12=      SET Update_Time=@SupplyDate,IC_Balance=@Balance
Line13=    WHERE Person_ID=@PersonID AND Update_Time<@SupplyDate AND DATEDIFF(day,Update_Time,@SupplyDate)<90
Line14=   DECLARE @UserNo VARCHAR(40) --充值操作員編號
Line15=   --1.取操作人員編號
Line16=   SELECT @UserNo=User_No FROM ST_UserInfo WHERE Card_No=@CardNo
Line17=   --2.插入充值存款記錄
Line18=   INSERT ST_SupplyFund(Person_ID,Supply_Date,Supply_Fund,User_No,Card_Balance,Data_Type,Moc_No,Remark)
Line19=   SELECT @PersonID,@SupplyDate,@SupplyFund,@UserNo,0,1,@MocNo,'SupplySmall'
Line20=   --3.插入轉帳記錄
Line21=   INSERT ST_TransferAccount(Person_ID,Transfer_Date,Transfer_Fund,User_No,Card_Balance,Moc_No,Remark)
Line22=   SELECT @PersonID,@SupplyDate,@SupplyFund,@UserNo,0,@MocNo,'SupplySmall'
[Step71]
Line1=IF OBJECT_ID('XF_pd_TransferAccount') IS NOT NULL
Line2=   DROP PROCEDURE XF_pd_TransferAccount
[Step72]
Line1=CREATE PROCEDURE dbo.XF_pd_TransferAccount
Line2=   (@PersonID INT,         --人員標識號
Line3=    @TransferFund MONEY,   --轉賬金額
Line4=    @CardNo VARCHAR(40),   --操作人員卡號
Line5=    @Balance MONEY,        --轉帳前的余額
Line6=    @MocNo VARCHAR(20),    --終端機編號
Line7=    @Date DATETIME         --轉帳時間
Line8=   )
Line9=AS   
Line10=   --0.更新IC卡余額
Line11=   UPDATE ST_Card 
Line12=      SET Update_Time=@Date,IC_Balance=@Balance
Line13=    WHERE Person_ID=@PersonID AND Update_Time<@Date AND DATEDIFF(day,Update_Time,@Date)<90
Line14=   DECLARE @UserNo VARCHAR(40) --轉帳操作員編號
Line15=   --1.取操作人員編號
Line16=   SELECT @UserNo=User_No FROM ST_UserInfo WHERE Card_No=@CardNo
Line17=   IF @UserNo IS NULL SET @UserNo=@CardNo
Line18=   --2.插入轉帳記錄
Line19=  INSERT ST_TransferAccount(Person_ID,Transfer_Date,Transfer_Fund,User_No,Card_Balance,Moc_No,Remark)
Line20=  SELECT @PersonID,@Date,@TransferFund,@UserNo,@Balance+@TransferFund,@MocNo,'終端轉帳'
Line21=  --3.更新余額
Line22=  UPDATE ST_Card SET Card_Balance=Card_Balance-@TransferFund WHERE Person_ID=@PersonID
[Step73]
Line1=IF OBJECT_ID('XF_pd_CardTakeFund') IS NOT NULL
Line2=   DROP PROCEDURE XF_pd_CardTakeFund
[Step74]
Line1=CREATE PROCEDURE dbo.XF_pd_CardTakeFund
Line2=   (@PersonID INT,        --人員標識號
Line3=    @TakeFund MONEY,      --取款金額
Line4=    @Balance MONEY,       --取款前的余額
Line5=    @CardNo VARCHAR(40),  --操作人員卡號
Line6=    @TakeDate DATETIME,   --取款日期
Line7=    @MocNo VARCHAR(20)   --終端機編號
Line8=   )
Line9=AS   
Line10=   --0.更新IC卡余額
Line11=   UPDATE ST_Card 
Line12=      SET Update_Time=@TakeDate,IC_Balance=@Balance
Line13=    WHERE Person_ID=@PersonID AND Update_Time<@TakeDate AND DATEDIFF(day,Update_Time,@TakeDate)<90
Line14=   DECLARE @UserNo VARCHAR(40) --充值操作員編號
Line15=   --1.取操作人員編號
Line16=   SELECT @UserNo=User_No FROM ST_UserInfo WHERE Card_No=@CardNo
Line17=   IF @UserNo IS NULL SET @UserNo=@CardNo
Line18=    --2.插入取款記錄
Line19=   INSERT ST_TakeFund(Person_ID,Take_Date,Take_Fund,User_No,Card_Balance,Moc_No,Remark)
Line20=   SELECT @PersonID,@TakeDate,@TakeFund,@UserNo,@Balance,@MocNo,'MocTakeFund' 
Line21=   --3.更新余額和補貼金額
Line22=   UPDATE ST_Card SET Card_Balance=Card_Balance-@TakeFund WHERE Person_ID=@PersonID 
[Step75]
Line1=IF OBJECT_ID('XF_pd_CardSupply') IS NOT NULL
Line2=   DROP PROCEDURE XF_pd_CardSupply
[Step76]
Line1=CREATE PROCEDURE dbo.XF_pd_CardSupply
Line2=   (@PersonID INT,        --人員標識號
Line3=    @SupplyFund MONEY,    --充值金額
Line4=    @FundOld MONEY,       --原充值金額
Line5=    @CardNo VARCHAR(40),  --操作人員卡號
Line6=    @Balance MONEY,       --充值前的余額
Line7=    @IDKey INT,           --被糾正的記錄ID
Line8=    @SupplyDate DATETIME, --充值日期
Line9=    @MocNo VARCHAR(20),   --終端機編號
Line10=    @IsCorrect BIT        --是否是糾錯記錄 
Line11=   )
Line12=AS   
Line13=   --0.更新IC卡余額
Line14=   UPDATE ST_Card 
Line15=      SET Update_Time=@SupplyDate,IC_Balance=@Balance
Line16=    WHERE Person_ID=@PersonID AND Update_Time<@SupplyDate AND DATEDIFF(day,Update_Time,@SupplyDate)<90
Line17=   DECLARE @UserNo VARCHAR(40) --充值操作員編號
Line18=   --1.取操作人員編號
Line19=   SELECT @UserNo=User_No FROM ST_UserInfo WHERE Card_No=@CardNo
Line20=   IF @UserNo IS NULL SET @UserNo=@CardNo
Line21=   IF @IsCorrect = 1 --充值糾錯
Line22=   BEGIN
Line23=      --2.插入充值存款記錄
Line24=      INSERT ST_SupplyFund(Person_ID,Supply_Date,Supply_Fund,User_No,Card_Balance,Data_Type,Remark)
Line25=      SELECT @PersonID,@SupplyDate,-@FundOld,@UserNo,@Balance-@SupplyFund+@FundOld,2,'充值糾錯'
Line26=      UNION ALL SELECT @PersonID,@SupplyDate,@SupplyFund,@UserNo,@Balance-@SupplyFund,2,'糾錯充值'
Line27=      --3.更新余額和補貼金額
Line28=      UPDATE ST_Card SET Card_Balance=Card_Balance+@SupplyFund-@FundOld WHERE Person_ID=@PersonID
Line29=      --4.標識記錄已經糾錯過
Line30=      UPDATE ST_SupplyFund SET Data_Type=2 WHERE ID_KEY=@IDKey
Line31=   END
Line32=   ELSE
Line33=   BEGIN  
Line34=      --2.插入充值存款記錄
Line35=      INSERT ST_SupplyFund(Person_ID,Supply_Date,Supply_Fund,User_No,Card_Balance,Data_Type,Moc_No,Remark)
Line36=      SELECT @PersonID,@SupplyDate,@SupplyFund,@UserNo,@Balance-@SupplyFund,1,@MocNo,'終端充值' 
Line37=      --3.更新余額和補貼金額
Line38=      UPDATE ST_Card SET Card_Balance=Card_Balance+@SupplyFund WHERE Person_ID=@PersonID
Line39=   END
[Step77]
Line1=IF OBJECT_ID('XF_pd_CardTimeConsume') IS NOT NULL
Line2=   DROP PROCEDURE XF_pd_CardTimeConsume
[Step78]
Line1=CREATE PROCEDURE dbo.XF_pd_CardTimeConsume
Line2=   (@PersonID INT,         --人員標識號
Line3=    @CardNo VARCHAR(20),   --卡號
Line4=    @SystemNo VARCHAR(20), --子系統編號
Line5=    @ConsumeMode TINYINT,  --消費模式
Line6=    @ConsumeTime DATETIME, --消費時間
Line7=    @Balance MONEY,        --卡余額(消費后的余額)
Line8=    @DBBalance MONEY,      --庫余額(消費后的余額)
Line9=    @BeforeFund MONEY,    --打折前的消費金額
Line10=    @ConsumeFund MONEY,    --打折后的消費金額
Line11=    @TimeLen INT,          --消費時長
Line12=    @MocNo VARCHAR(20),    --終端機編號
Line13=    @Options TINYINT,      --數據選項(0-計時開始、1-計時結束、2-計時結束并且超時糾錯)
Line14=    @SubsidyFund MONEY     --本次發放補貼金額
Line15=   )
Line16=AS   
Line17=   --0.更新IC卡余額
Line18=   UPDATE ST_Card 
Line19=      SET Update_Time=@ConsumeTime,IC_Balance=@Balance
Line20=    WHERE Person_ID=@PersonID AND Update_Time<@ConsumeTime AND DATEDIFF(day,Update_Time,@ConsumeTime)<90
Line21=   DECLARE @DataType TINYINT,@Fund MONEY --數據類型,預扣默認消費額
Line22=   IF @Options > 0 --計時結束
Line23=   BEGIN
Line24=      --1.1.刪除時長消費刷卡記錄
Line25=      DELETE FROM LH_TimeBrushCard WHERE Card_No=@CardNo
Line26=      --1.2.刪除預存消費記錄
Line27=      IF @Options <> 2 --不是超時糾錯
Line28=      BEGIN
Line29=         SELECT @Fund=Consume_Fund FROM (SELECT TOP 1 Consume_Fund FROM XF_ConsumeTemp
Line30=                                          WHERE Person_ID=@PersonID AND Data_Type=7 AND Is_Commited=0) A
Line31=         IF @Fund IS NULL --消費未結算時長記錄不緩沖表中
Line32=         BEGIN
Line33=            SELECT @Fund=Consume_Fund FROM (SELECT TOP 1 Consume_Fund FROM XF_Consume WHERE Person_ID=@PersonID AND Data_Type=7) A
Line34=            DELETE FROM XF_Consume WHERE Person_ID=@PersonID AND Data_Type=7
Line35=         END
Line36=         DELETE FROM XF_ConsumeTemp WHERE Person_ID=@PersonID AND Data_Type=7
Line37=         SET @DataType=0
Line38=      END
Line39=      ELSE --超時糾錯
Line40=      BEGIN
Line41=         UPDATE XF_ConsumeTemp SET Data_Type=0 WHERE Person_ID=@PersonID AND Data_Type=7
Line42=         UPDATE XF_Consume SET Data_Type=0 WHERE Person_ID=@PersonID AND Data_Type=7 --如果未在消費緩沖表中就必須到消費表更新記錄
Line43=      END
Line44=   END
Line45=   ELSE --計時開始
Line46=   BEGIN
Line47=      --1.插入時長消費刷卡記錄
Line48=      INSERT INTO LH_TimeBrushCard(Card_No,Brush_Time)
Line49=      VALUES(@CardNo,@ConsumeTime)
Line50=      SET @DataType=7
Line51=      SET @Fund=0
Line52=   END
Line53=   IF @Options < 2 --非超時糾錯
Line54=   BEGIN
Line55=      --2.更新余額和補貼金額
Line56=      UPDATE ST_Card 
Line57=         SET @DBBalance=Card_Balance+@Fund-@ConsumeFund,Card_Balance=@DBBalance
Line58=       WHERE Person_ID=@PersonID      
Line59=      --3.插入消費記錄
Line60=      INSERT INTO XF_ConsumeTemp(Person_ID,System_No,Consume_Mode,Consume_Time,Before_Fund,Consume_Fund,Consume_Copy,
Line61=                                 Consume_TimeLen,Consume_Capacity,Repast_No,Ware_ID,Moc_No,COM_No,Card_Balance,
Line62=                                 DB_Balance,Card_SmallAccount,Data_Type,Is_Commited) 
Line63=      SELECT @PersonID,@SystemNo,@ConsumeMode,@ConsumeTime,@BeforeFund,@ConsumeFund,NULL,@TimeLen,NULL,NULL,
Line64=             NULL,@MocNo,NULL,@Balance,@DBBalance,0,@DataType,0 --新消費記錄
Line65=   END
[Step79]
system=MJ
Line1= IF OBJECT_ID('dbo.MJ_Solution') IS NULL
Line2=	CREATE TABLE dbo.MJ_Solution (
Line3=	Solution_ID int NOT NULL,
Line4=	Solution_No varchar(40) NOT NULL,
Line5=	Solution_Name varchar(40) NOT NULL,
Line6=	Ctrl_No varchar(4) NOT NULL,
Line7=	Door_No int NOT NULL,
Line8=	PsnTime_No int NULL,
Line9=	PRIMARY KEY (Solution_ID ASC, Ctrl_No ASC, Door_No ASC) 
Line10=	)
[Step80]
system=MJ
Line1=	INSERT INTO dbo.ST_MenuInfo(Menu_ID,System_No,Menu_No,Menu_Name,Menu_Hint,Para_ID,Call_Mode,Form_Name,ShortKey,
Line2=                        Menu_Index,Image_Index,Separator,InToolBar,Remark)
Line3=  SELECT 200,'MJ','MJ_0205','進出權限方案管理','進去權限方案管理',153,'2','TfrmSolution',NULL,'5',43,'0',0,NULL
Line4=    WHERE not Exists(SELECT Menu_ID FROM dbo.ST_MenuInfo where Menu_ID=200)
[Step81]
system=MJ
Line1=--最多供100個用戶使用權限
Line2=IF COL_LENGTH('MJ_Controller', 'Users_No') IS NULL
Line3=  ALTER TABLE MJ_Controller ADD Users_No VARCHAR(2100) DEFAULT ''
[Step82]
Line1=ALTER VIEW dbo.V_STPersonCard
Line2=AS
Line3=SELECT dbo.ST_Person.Person_ID,
Line4=dbo.ST_Person.Dept_ID,
Line5=dbo.ST_Person.Card_No,
Line6=dbo.ST_Person.Person_No,
Line7=dbo.ST_Person.Person_Name,
Line8=dbo.ST_Department.Dept_Name,
Line9=dbo.ST_Card.Type_No,
Line10=dbo.ST_Card.Card_Password,
Line11=dbo.ST_Card.Card_NewDate,
Line12=dbo.ST_Card.Card_ValidDate,
Line13=dbo.ST_Card.Card_State,
Line14=dbo.ST_Card.Card_SmallBalance,
Line15=dbo.ST_Card.Card_Balance,
Line16=dbo.ST_Card.Subsidy_Fund,
Line17=dbo.ST_CardType.Type_Name,
Line18=dbo.ST_Department.Dept_No,
Line19=dbo.ST_CardType.Small_Discount,
Line20=dbo.ST_CardType.Max_IcFunds,
Line21=dbo.ST_Person.OffWork_Date
Line22=FROM dbo.ST_Person LEFT OUTER JOIN
Line23=dbo.ST_Department ON
Line24=dbo.ST_Person.Dept_ID = dbo.ST_Department.Dept_ID LEFT OUTER JOIN
Line25=dbo.ST_Card ON
Line26=dbo.ST_Person.Person_ID = dbo.ST_Card.Person_ID LEFT OUTER JOIN
Line27=dbo.ST_CardType ON dbo.ST_Card.Type_No = dbo.ST_CardType.Type_No
Line28=WHERE (dbo.ST_Person.Is_Del = 0)
[Step83]
system=MJ
Line1=INSERT INTO MJ_EventInfo(Event_Type,Event_Code,Event_Name,Record_Color,Info_Style)
Line2= SELECT A.* FROM
Line3=   (SELECT 0 [Event_Type],4[Event_Code],'非普通模式開門'[Event_Name],2[Record_Color],'S1'[Info_Style]     
Line4=   ) A
Line5= WHERE NOT EXISTS(SELECT Event_Code FROM MJ_EventInfo 
Line6=                   WHERE MJ_EventInfo.Event_Type=A.Event_Type AND MJ_EventInfo.Event_Code=A.Event_Code)
[Step84]
system=MJ
Line1=UPDATE MJ_EventInfo SET Record_Color=2,Info_Style='S3' WHERE Event_Type=4
[Step85]
system=MJ
Line1=IF OBJECT_ID('MJ_pd_SaveControlInfo') IS NOT NULL
Line2=   DROP PROCEDURE MJ_pd_SaveControlInfo
[Step86]
system=MJ
Line1=CREATE PROCEDURE dbo.MJ_pd_SaveControlInfo
Line2=   @CardNo    VARCHAR(20),
Line3=   @OccurTime DATETIME,
Line4=   @CtrlNo    VARCHAR(20),
Line5=   @DoorNo    INT,
Line6=   @EventCode TINYINT,
Line7=   @DoorFlag  VARCHAR(20),
Line8=   @EventType TINYINT
Line9=AS
Line10=   UPDATE MJ_SysParam SET Param_Value=@CardNo WHERE Param_Code='Last_READCARD' -- 記錄讀到卡號
Line11=   DECLARE @PersonID INT,@Description VARCHAR(40),@RecordColor TINYINT,@InfoStyle VARCHAR(20)
Line12=   DECLARE @RunInfo VARCHAR(80),@AlarmInfo VARCHAR(80),@StateInfo VARCHAR(40),@AcsCard VARCHAR(20)
Line13=   DECLARE @PersonName VARCHAR(40),@DeptName VARCHAR(40),@EventName VARCHAR(20)
Line14=       SET @PersonID=-1
Line15=       SET @PersonName='      '
Line16=       SET @DeptName='      '
Line17=   --
Line18=   SET @AcsCard=@CardNo
Line19=   SELECT @PersonID=A.Person_ID,@PersonName=Person_Name,@DeptName=Dept_Name,@CardNo=A.Card_No
Line20=     FROM MJ_Person C
Line21=     LEFT JOIN ST_Person A ON A.Person_ID=C.Person_ID
Line22=     LEFT JOIN ST_Department B ON B.Dept_ID=A.Dept_ID
Line23=    WHERE Acs_Card=@AcsCard
Line24=   IF @CardNo IS NULL SET @CardNo=@AcsCard
Line25=   SELECT @Description=Door_Name FROM MJ_Door WHERE Ctrl_No=@CtrlNo AND Door_No=@DoorNo
Line26=   --處理事件
Line27=   SELECT @RecordColor=Record_Color,@InfoStyle=Info_Style,@EventName=Event_Name FROM MJ_EventInfo
Line28=    WHERE Event_Type=@EventType AND Event_Code=@EventCode
Line29=   IF @EventType=1 SET @EventName=CAST(@EventCode AS VARCHAR)+':'+@EventName           
Line30=   IF @InfoStyle='S1' 
Line31=   BEGIN
Line32=      SET @RunInfo=@CardNo+'--'+@PersonName+'--'+@DeptName+'--允許通過--時間:'+CONVERT(VARCHAR(8),@OccurTime,108)
Line33=      SET @StateInfo='允許通過'
Line34=   END
Line35=   ELSE IF @InfoStyle='S2' 
Line36=   BEGIN
Line37=      SET @RunInfo='['+@EventName+']--時間:'+CONVERT(VARCHAR(19),@OccurTime,121)
Line38=      SET @StateInfo=@EventName
Line39=   END
Line40=   ELSE IF @InfoStyle='S3' 
Line41=   BEGIN
Line42=      IF @EventCode<>26 
Line43=         SET @RunInfo='[報警]--'+@EventName+'--時間:'+CONVERT(VARCHAR(19),@OccurTime,121)
Line44=      ELSE
Line45=      BEGIN
Line46=         SET @RunInfo='[報警]--'+@EventName+'-不足人數'+@CardNo+'人'+'--時間:'+CONVERT(VARCHAR(19),@OccurTime,121)
Line47=         SET @CardNo=''
Line48=      END
Line49=      SET @StateInfo=@EventName
Line50=   END
Line51=   ELSE IF @InfoStyle='S4' 
Line52=   BEGIN
Line53=      SET @RunInfo='['+@EventName+']'+@CardNo+'--'+@PersonName+'--'+@DeptName+'--允許通過--時間:'+CONVERT(VARCHAR(8),@OccurTime,108)
Line54=      SET @StateInfo='允許通過-'+@EventName
Line55=   END
Line56=   ELSE IF @InfoStyle='S5' 
Line57=   BEGIN
Line58=      SET @RunInfo=@CardNo+'--'+@PersonName+'--'+@DeptName+'--禁止通過-'+@EventName+'--時間:'+CONVERT(VARCHAR(8),@OccurTime,108)
Line59=      SET @StateInfo='禁止通過-'+@EventName
Line60=   END              
Line61=   IF @RunInfo IS NOT NULL
Line62=   BEGIN
Line63=      IF @RecordColor=4 SET @AlarmInfo=@Description+@EventName+'，知道了'
Line64=      SET @Description=@Description+@DoorFlag
Line65=      --更新記錄
Line66=      IF @CardNo='0' SET @CardNo=''
Line67=      IF NOT EXISTS(SELECT Ctrl_No FROM MJ_ControlInfo WHERE Ctrl_No=@CtrlNo AND Door_No=@DoorNo)
Line68=      BEGIN
Line69=         INSERT INTO MJ_ControlInfo(Person_ID,Card_No,Ctrl_No,Door_No,[Description],Run_Time,Run_Info,Alarm_Info,Door_State,Record_Color,State_Info,Update_Time)
Line70=         VALUES(@PersonID,@CardNo,@CtrlNo,@DoorNo,@Description,@OccurTime,@RunInfo,@AlarmInfo,0,@RecordColor,@StateInfo,GETDATE())
Line71=      END
Line72=      ELSE
Line73=      BEGIN
Line74=         UPDATE MJ_ControlInfo 
Line75=            SET Person_ID=@PersonID,Card_No=@CardNo,Run_Time=@OccurTime,Run_Info=@RunInfo,
Line76=                Alarm_Info=@AlarmInfo,Record_Color=@RecordColor,[Description]=@Description,
Line77=                State_Info=@StateInfo,Update_Time=GETDATE()
Line78=          WHERE Ctrl_No=@CtrlNo AND Door_No=@DoorNo
Line79=      END
Line80=   END
[Step87]
system=MJ
Line1=IF OBJECT_ID('MJ_vi_ChangeHistory') IS NOT NULL
Line2=   DROP VIEW MJ_vi_ChangeHistory
[Step88]
system=MJ
Line1=CREATE VIEW MJ_vi_ChangeHistory
Line2=AS
Line3=   SELECT C.Acs_Card [Card_No],Door_No,PsnTime_No,Door_Pwd,Card_NewDate,
Line4=          CASE WHEN A.Change_State<>6 THEN Card_ValidDate ELSE '2000-01-01' END [Card_ValidDate],
Line5=          A.Change_Value,A.Change_State,A.Change_Date,Change_Ctrl,Ctrl_No,FirstCardOpen
Line6=     FROM MJ_ChangeHistory A
Line7=     LEFT JOIN MJ_PsnDoorRight B ON B.Person_ID=A.Person_ID
Line8=     LEFT JOIN ST_Person E ON E.Person_ID=A.Person_ID
Line9=     LEFT JOIN MJ_Person C ON C.Person_ID=A.Person_ID
Line10=     LEFT JOIN ST_Card D ON D.Person_ID=A.Person_ID
Line11=--  WHERE C.Person_State=0 AND E.Is_Del=0
[Step89]
system=MJ
Line1=-- 會議管理
Line2=--更新表結構
Line3=IF COL_LENGTH('MJ_DOOR', 'MeetingMode') IS NULL
Line4=  ALTER TABLE MJ_DOOR ADD MeetingMode Int NULL Default 0
[Step90]
system=MJ
Line1=IF OBJECT_ID('dbo.MJ_MeetingInfo') IS NULL
Line2=	CREATE TABLE dbo.MJ_MeetingInfo (
Line3=	       ID_Key             int IDENTITY(1,1) NOT NULL,
Line4=	       Ctrl_No            varchar(20) NOT NULL,
Line5=	       Door_No            varchar(20) NOT NULL,
Line6=	       Bgn_Date           varchar(10) NULL,
Line7=	       Bgn_Time           varchar(5) NULL,
Line8=	       End_Date           varchar(10) NULL,
Line9=	       End_Time           varchar(5) NULL,
Line10=	       Remark             varchar(40) NULL,
Line11=	       PRIMARY KEY (ID_Key ASC)
Line12=	)
[Step91]
system=MJ
Line1=IF OBJECT_ID('dbo.MJ_MeetingPerson') IS NULL
Line2=	CREATE TABLE dbo.MJ_MeetingPerson (
Line3=	       ID_Key             int IDENTITY(1,1) NOT NULL,
Line4=	       Ctrl_No            varchar(20) NOT NULL,
Line5=	       Door_No            varchar(20) NOT NULL,
Line6=	       Person_ID          int,
Line7=	       PRIMARY KEY (ID_Key ASC)
Line8=	)
[Step92]
system=MJ
Line1=--增加觸發器
Line2=IF EXISTS (SELECT name FROM sysobjects WHERE name = 'MJ_tg_DoorMeetingMode' AND type = 'TR')
Line3=   DROP TRIGGER MJ_tg_DoorMeetingMode
[Step93]
system=MJ
Line1=CREATE TRIGGER dbo.MJ_tg_DoorMeetingMode
Line2=    ON MJ_Door
Line3=FOR UPDATE,INSERT,DELETE
Line4=AS 
Line5=   DECLARE @IsDelete BIT,@IsInsert BIT,@IsUpdate BIT,@IsMeetingMode BIT
Line6=   IF EXISTS(SELECT Ctrl_No FROM DELETED)  SET @IsDelete=1 ELSE SET @IsDelete=0
Line7=   IF EXISTS(SELECT Ctrl_No FROM INSERTED) SET @IsInsert=1 ELSE SET @IsInsert=0
Line8=   IF @IsInsert=1 AND @IsDelete=1 
Line9=      SELECT @IsUpdate=1,@IsDelete=0,@IsInsert=0
Line10=   ELSE
Line11=      SET @IsUpdate=0
Line12=   IF @IsUpdate=1 AND UPDATE(MeetingMode)
Line13=      SELECT @IsMeetingMode=MeetingMode FROM INSERTED
Line14=   IF (@IsInsert=1 AND EXISTS(SELECT MeetingMode FROM INSERTED WHERE MeetingMode=1)) OR (@IsUpdate=1 AND @IsMeetingMode=1)
Line15=   BEGIN
Line16=      IF NOT EXISTS(SELECT ID_Key FROM MJ_MeetingInfo WHERE Ctrl_No IN (SELECT Ctrl_No FROM INSERTED) AND Door_No IN (SELECT Door_No FROM INSERTED))
Line17=         INSERT INTO MJ_MeetingInfo(Ctrl_No,Door_No,Bgn_Date,Bgn_Time,End_Date,End_Time) 
Line18=           SELECT Ctrl_No,Door_No,CONVERT(varchar(10), getdate(), 120 ),'00:00',CONVERT(varchar(10), getdate(), 120),'23:59' FROM INSERTED
Line19=   END
[Step94]
system=MJ
Line1=IF OBJECT_ID('dbo.MJ_pd_GenDotAndDoor') IS NOT NULL
Line2=   DROP PROCEDURE dbo.MJ_pd_GenDotAndDoor
[Step95]
system=MJ
Line1=CREATE PROCEDURE dbo.MJ_pd_GenDotAndDoor
Line2=@ctrl_no varchar(4),
Line3=@is_Extend int,
Line4=@Ctrl_Type int
Line5=AS
Line6=DECLARE @ResType int, @IdxNo int, @ResNumber int,@ResName varchar(20),@IsExtend bit
Line7=DECLARE  tmp_resparam  CURSOR FOR SELECT  Res_Type, Idx_No, Res_Name, Res_Number,Is_Extend FROM  MJ_Resparam  
Line8=   where Ctrl_Type=@Ctrl_Type and (Is_Extend=0 or Is_Extend = @Is_Extend) order by Res_Type ,Idx_No
Line9=OPEN tmp_resparam
Line10=IF @@cursor_rows > 0
Line11=BEGIN
Line12=  FETCH NEXT FROM tmp_resparam into @ResType,  @IdxNo, @ResName,  @ResNumber,@IsExtend
Line13=  WHILE @@fetch_status = 0
Line14=  BEGIN
Line15=    Set @IdxNo=1
Line16=    WHILE @ResNumber >0    
Line17=    BEGIN
Line18=      ---生成門--
Line19=      if @ResType=0 
Line20=	INSERT  INTO MJ_Door (Ctrl_No, Door_No, Door_Name,DoorTime_No)  
Line21=	  VALUES(@ctrl_no,convert(varchar,@IdxNo), @ResName+'('+convert(varchar,@IdxNo)+')',1)
Line22=      else
Line23=       ---生成點--
Line24=        INSERT INTO MJ_CtrlDot (Ctrl_No, Dot_No, Dot_Name,  Dot_Type, Is_Input,Is_Extend) 
Line25=          VALUES(@ctrl_no, Convert(varchar,@ResType)+'_'+ Convert(varchar,@IdxNo),@ResName+'('+convert(varchar,@IdxNo)+')', 
Line26=                 @ResType, (Case when @ResType=1 then 1 else 0 end),@IsExtend)
Line27=      SELECT @ResNumber = @ResNumber - 1
Line28=      SELECT @IdxNo = @IdxNo + 1
Line29=    END
Line30=    FETCH NEXT FROM tmp_resparam into @ResType,  @IdxNo, @ResName,  @ResNumber,@IsExtend
Line31=  END
Line32=END
Line33=CLOSE tmp_resparam
Line34=DEALLOCATE tmp_resparam
[Step96]
system=MJ
Line1=--門禁系統初始化菜單
Line2=INSERT INTO dbo.ST_MenuInfo(Menu_ID,System_No,Menu_No,Menu_Name,Menu_Hint,Para_ID,Call_Mode,Form_Name,ShortKey,
Line3=                        Menu_Index,Image_Index,Separator,InToolBar,Remark)
Line4=SELECT * FROM (SELECT 450[Menu_ID],'MJ'[System_No],'MJ_0408'[Menu_No],'會議管理'[Menu_Name],'會議管理'[Menu_Hint],158[Para_ID],2[Call_Mode],
Line5=                      'TfrmMjMeeting'[Form_Name],NULL[ShortKey],8[Menu_Index],-1[Image_Index],0[Separator],0[InToolBar],NULL[Remark]
Line6=               UNION ALL SELECT 451,'MJ','MJ_0305','刷卡添加用戶','刷卡添加用戶',148,2,'TfrmBrushCardPerson',NULL,6,-1,0,0,NULL
Line7=               UNION ALL SELECT 452,'MJ','MJ_0306','門權限設置','門權限設置',148,'1','TfrmAscDoor',NULL,'7',45,'1',0,NULL       
Line8=							) A
Line9= WHERE A.Menu_ID NOT IN (SELECT Menu_ID FROM dbo.ST_MenuInfo)
[Step97]
system=MJ
Line1=--1.初始化功能擴展數據
Line2=INSERT INTO dbo.MJ_ExtendFunc(Menu_No,Menu_Name,Is_Menu,Is_Use)
Line3=SELECT A.* FROM (SELECT 'MJ_0408'[Menu_No],'會議管理'[Menu_Name],0[Is_Menu],0[Is_Use]
Line4=							) A
Line5=WHERE A.Menu_No NOT IN(SELECT Menu_No FROM dbo.MJ_ExtendFunc)
[Step98]
system=MJ
Line1=--2.增加讀到卡號的參數
Line2=INSERT INTO dbo.MJ_SysParam(Param_Code,Param_Name,Param_Value)
Line3=SELECT A.* FROM (SELECT 'LAST_READCARD'[Param_Code],'最后一次門禁讀頭讀到卡號'[Param_Name],'0'[Param_Value]								
Line4=							) A
Line5=WHERE A.Param_Code NOT IN(SELECT Param_Code FROM dbo.MJ_SysParam)
[Step99]
System=SF
Line1=/*售飯子系統初始菜單表 增加卡類餐別匯總表*/
Line2=INSERT INTO dbo.ST_MenuInfo(Menu_ID,System_No,Menu_No,Menu_Name,Menu_Hint,Para_ID,Call_Mode,Form_Name,ShortKey,
Line3=                        Menu_Index,Image_Index,Separator,InToolBar,Remark)
Line4= SELECT 400,'SF','SF_0409','卡類餐別匯總表','卡類餐別匯總表',97,'1','TfrmCardTypeRepastRpt',NULL,'1',-1,'0',0,NULL
Line5=   WHERE NOT Exists(SELECT Menu_ID FROM dbo.ST_MenuInfo where Menu_ID=400)
[Step100]
System=KQ
Line1=--6.考勤日報增加系統權限值列表
Line2=--6.1 日報中
Line3=IF NOT EXISTS(SELECT Right_ID FROM ST_RightValue WHERE Right_ID='1' AND Form_Name='KQ_0601')
Line4=   INSERT INTO ST_RightValue(Right_ID,Form_Name,Right_Name)
Line5=   VALUES('1','KQ_0601','班次調整')
Line6=ELSE
Line7=   UPDATE ST_RightValue SET Right_Name='班次調整'
Line8=    WHERE Right_ID='1' AND Form_Name='KQ_0601'
[Step101]
System=KQ
Line1=IF NOT EXISTS(SELECT Right_ID FROM ST_RightValue WHERE Right_ID='2' AND Form_Name='KQ_0601')
Line2=   INSERT INTO ST_RightValue(Right_ID,Form_Name,Right_Name)
Line3=   VALUES('2','KQ_0601','請假登記')
Line4=ELSE
Line5=   UPDATE ST_RightValue SET Right_Name='請假登記'
Line6=    WHERE Right_ID='2' AND Form_Name='KQ_0601'
[Step102]
System=KQ
Line1=IF NOT EXISTS(SELECT Right_ID FROM ST_RightValue WHERE Right_ID='3' AND Form_Name='KQ_0601')
Line2=   INSERT INTO ST_RightValue(Right_ID,Form_Name,Right_Name)
Line3=   VALUES('3','KQ_0601','增補刷卡')
Line4=ELSE
Line5=   UPDATE ST_RightValue SET Right_Name='增補刷卡'
Line6=    WHERE Right_ID='3' AND Form_Name='KQ_0601'
[Step103]
System=KQ
Line1=IF NOT EXISTS(SELECT Right_ID FROM ST_RightValue WHERE Right_ID='4' AND Form_Name='KQ_0601')
Line2=   INSERT INTO ST_RightValue(Right_ID,Form_Name,Right_Name)
Line3=   VALUES('4','KQ_0601','調休補班')
Line4=ELSE
Line5=   UPDATE ST_RightValue SET Right_Name='調休補班'
Line6=    WHERE Right_ID='4' AND Form_Name='KQ_0601'
[Step104]
System=KQ
Line1=UPDATE ST_RightInfo SET Right_Values='01234'
Line2= WHERE Form_Name='KQ_0601'
[Step105]
System=KQ
Line1=--7.默認管理員組角色權限為所有
Line2=/*模塊插入功能已經由menuinfo上的觸發器完成了*/
Line3=/*如果有增加權限項目必須執行以下*/
Line4=UPDATE dbo.ST_RoleRight SET Right_Values=dbo.ST_RightInfo.Right_Values
Line5=  FROM dbo.ST_RightInfo
Line6= WHERE dbo.ST_RoleRight.Menu_ID=dbo.ST_RightInfo.Menu_ID AND dbo.ST_RoleRight.Role_No='Administrators'
[Step106]
Line1=IF OBJECT_ID('MJ_fn_GetBrushCardTime') IS NOT NULL
Line2=   DROP FUNCTION MJ_fn_GetBrushCardTime
[Step107]
Line1=CREATE FUNCTION dbo.MJ_fn_GetBrushCardTime(
Line2=   @PersonID INT,
Line3=   @First BIT,
Line4=   @DateBgn DATETIME,
Line5=   @DateEnd DATETIME
Line6=)
Line7=RETURNS BIGINT
Line8=AS
Line9=BEGIN
Line10=   DECLARE @BrushCardTime DATETIME,@IDKEY BIGINT
Line11=   --取出時間段的數據
Line12=   SELECT @BrushCardTime=(CASE WHEN @First=1 THEN MIN(BrushCard_Time) ELSE MAX(BrushCard_Time) END)
Line13=     FROM MJ_NormalRecord
Line14=    WHERE Person_ID=@PersonID AND BrushCard_Time>=@DateBgn AND BrushCard_Time<=@DateEnd
Line15=   --取出時間相同的唯一記錄
Line16=   IF @BrushCardTime IS NOT NULL 
Line17=      SELECT @IDKEY=MAX(ID_KEY) FROM MJ_NormalRecord WHERE Person_ID=@PersonID AND BrushCard_Time=@BrushCardTime
Line18=   RETURN @IDKEY
Line19=END
[Step108]
Line1=/**不能消費則售飯機顯示18**/
Line2=IF not Exists(Select * from ST_SysParam where Param_Code='ConsumeHasSubsidy')
Line3=  INSERT INTO ST_SysParam(Param_Code,Param_name,Param_Value)
Line4=   Values('ConsumeHasSubsidy','ConsumeHasSubsidy','0')
[Step109]
System=KQ
Line1=IF OBJECT_ID('dbo.KQ_ModifyMonthly') IS NULL
Line2=   CREATE TABLE dbo.KQ_ModifyMonthly (
Line3=       ID_KEY               int IDENTITY,
Line4=       Person_ID            int NULL,
Line5=       MustWork_Hour        float NULL,
Line6=       MustWork_Days        float NULL,
Line7=       FactWork_Hour        float NULL,
Line8=       FactWork_Days        float NULL,
Line9=       AbsentWork_Hour      float NULL,
Line10=       AbsentWork_Days      float NULL,
Line11=       Overtime_Hour        float NULL,
Line12=       Overtime_Days        float NULL,
Line13=       Overrest_Hour        float NULL,
Line14=       Overrest_Days        float NULL,
Line15=       Overholiday_Hour     float NULL,
Line16=       Overholiday_Days     float NULL,
Line17=       Late1_Minute         float NULL,
Line18=       Late1_Times          tinyint NULL,
Line19=       Late2_Minute         float NULL,
Line20=       Late2_Times          tinyint NULL,
Line21=       Late3_Minute         float NULL,
Line22=       Late3_Times          tinyint NULL,
Line23=       Early1_Minute        float NULL,
Line24=       Early1_Times         tinyint NULL,
Line25=       Early2_Minute        float NULL,
Line26=       Early2_Times         tinyint NULL,
Line27=       Early3_Minute        float NULL,
Line28=       Early3_Times         tinyint NULL,
Line29=       TotalLate_Minute     float NULL,
Line30=       TotalLate_Times      tinyint NULL,
Line31=       TotalEarly_Minute    float NULL,
Line32=       TotalEarly_Times     tinyint NULL,
Line33=       J101_Hour            float NULL,
Line34=       J101_Days            float NULL,
Line35=       J102_Hour            float NULL,
Line36=       J102_Days            float NULL,
Line37=       J103_Hour            float NULL,
Line38=       J103_Days            float NULL,
Line39=       J104_Hour            float NULL,
Line40=       J104_Days            float NULL,
Line41=       J105_Hour            float NULL,
Line42=       J105_Days            float NULL,
Line43=       J106_Hour            float NULL,
Line44=       J106_Days            float NULL,
Line45=       J107_Hour            float NULL,
Line46=       J107_Days            float NULL,
Line47=       J108_Hour            float NULL,
Line48=       J108_Days            float NULL,
Line49=       J109_Hour            float NULL,
Line50=       J109_Days            float NULL,
Line51=       J110_Hour            float NULL,
Line52=       J110_Days            float NULL,
Line53=       J111_Hour            float NULL,
Line54=       J111_Days            float NULL,
Line55=       J112_Hour            float NULL,
Line56=       J112_Days            float NULL,
Line57=       J113_Hour            float NULL,
Line58=       J113_Days            float NULL,
Line59=       J114_Hour            float NULL,
Line60=       J114_Days            float NULL,
Line61=       J115_Hour            float NULL,
Line62=       J115_Days            float NULL,
Line63=       J116_Hour            float NULL,
Line64=       J116_Days            float NULL,
Line65=       J117_Hour            float NULL,
Line66=       J117_Days            float NULL,
Line67=       J118_Hour            float NULL,
Line68=       J118_Days            float NULL,
Line69=       TotalLeave_Hour      float NULL,
Line70=       TotalLeave_Days      float NULL,
Line71=       A                    smallint NULL,
Line72=       B                    smallint NULL,
Line73=       C                    smallint NULL,
Line74=       D                    smallint NULL,
Line75=       E                    smallint NULL,
Line76=       F                    smallint NULL,
Line77=       G                    smallint NULL,
Line78=       H                    smallint NULL,
Line79=       I                    smallint NULL,
Line80=       J                    smallint NULL,
Line81=       K                    smallint NULL,
Line82=       L                    smallint NULL, 
Line83=       Account_Code         varchar(20) NULL,
Line84=       PRIMARY KEY (ID_KEY ASC)
Line85=)
[Step110]
System=KQ
Line1=IF OBJECT_ID('dbo.KQ_ModifyMonthlyHistory') IS NULL
Line2=   CREATE TABLE dbo.KQ_ModifyMonthlyHistory (
Line3=       ID_KEY               int IDENTITY,
Line4=       Person_ID            int NULL,
Line5=       MustWork_Hour        float NULL,
Line6=       MustWork_Days        float NULL,
Line7=       FactWork_Hour        float NULL,
Line8=       FactWork_Days        float NULL,
Line9=       AbsentWork_Hour      float NULL,
Line10=       AbsentWork_Days      float NULL,
Line11=       Overtime_Hour        float NULL,
Line12=       Overtime_Days        float NULL,
Line13=       Overrest_Hour        float NULL,
Line14=       Overrest_Days        float NULL,
Line15=       Overholiday_Hour     float NULL,
Line16=       Overholiday_Days     float NULL,
Line17=       Late1_Minute         float NULL,
Line18=       Late1_Times          tinyint NULL,
Line19=       Late2_Minute         float NULL,
Line20=       Late2_Times          tinyint NULL,
Line21=       Late3_Minute         float NULL,
Line22=       Late3_Times          tinyint NULL,
Line23=       Early1_Minute        float NULL,
Line24=       Early1_Times         tinyint NULL,
Line25=       Early2_Minute        float NULL,
Line26=       Early2_Times         tinyint NULL,
Line27=       Early3_Minute        float NULL,
Line28=       Early3_Times         tinyint NULL,
Line29=       TotalLate_Minute     float NULL,
Line30=       TotalLate_Times      tinyint NULL,
Line31=       TotalEarly_Minute    float NULL,
Line32=       TotalEarly_Times     tinyint NULL,
Line33=       J101_Hour            float NULL,
Line34=       J101_Days            float NULL,
Line35=       J102_Hour            float NULL,
Line36=       J102_Days            float NULL,
Line37=       J103_Hour            float NULL,
Line38=       J103_Days            float NULL,
Line39=       J104_Hour            float NULL,
Line40=       J104_Days            float NULL,
Line41=       J105_Hour            float NULL,
Line42=       J105_Days            float NULL,
Line43=       J106_Hour            float NULL,
Line44=       J106_Days            float NULL,
Line45=       J107_Hour            float NULL,
Line46=       J107_Days            float NULL,
Line47=       J108_Hour            float NULL,
Line48=       J108_Days            float NULL,
Line49=       J109_Hour            float NULL,
Line50=       J109_Days            float NULL,
Line51=       J110_Hour            float NULL,
Line52=       J110_Days            float NULL,
Line53=       J111_Hour            float NULL,
Line54=       J111_Days            float NULL,
Line55=       J112_Hour            float NULL,
Line56=       J112_Days            float NULL,
Line57=       J113_Hour            float NULL,
Line58=       J113_Days            float NULL,
Line59=       J114_Hour            float NULL,
Line60=       J114_Days            float NULL,
Line61=       J115_Hour            float NULL,
Line62=       J115_Days            float NULL,
Line63=       J116_Hour            float NULL,
Line64=       J116_Days            float NULL,
Line65=       J117_Hour            float NULL,
Line66=       J117_Days            float NULL,
Line67=       J118_Hour            float NULL,
Line68=       J118_Days            float NULL,
Line69=       TotalLeave_Hour      float NULL,
Line70=       TotalLeave_Days      float NULL,
Line71=       A                    smallint NULL,
Line72=       B                    smallint NULL,
Line73=       C                    smallint NULL,
Line74=       D                    smallint NULL,
Line75=       E                    smallint NULL,
Line76=       F                    smallint NULL,
Line77=       G                    smallint NULL,
Line78=       H                    smallint NULL,
Line79=       I                    smallint NULL,
Line80=       J                    smallint NULL,
Line81=       K                    smallint NULL,
Line82=       L                    smallint NULL, 
Line83=       Account_Code         varchar(20) NULL,
Line84=       PRIMARY KEY (ID_KEY ASC)
Line85=)
[Step111]
System=KQ
Line1=INSERT INTO dbo.KQ_TransferTables(Table_Name,Date_Field,Transfer_Index,Delete_Index)
Line2= SELECT * FROM (SELECT 'KQ_AdjustClass'[Table_Name],'Adjust_Date'[Date_Field],3[Transfer_Index],10[Delete_Index]
Line3=								UNION ALL SELECT 'KQ_ModifyMonthly','Account_Code',12,1
Line4=							) A
Line5=WHERE A.Table_Name NOT IN(SELECT Table_Name FROM dbo.KQ_TransferTables)
[Step112]
Line1=/*一卡通產品化升級菜單*/
Line2=Update ST_menuInfo Set Form_Name='TfrmDownloadKQ' where System_No='KQ' and Form_Name='TfrmDownload'
Line3=Update ST_menuInfo Set Form_Name='TfrmDownloadDC' where System_No='DC' and Form_Name='TfrmDownload'
Line4=Update ST_menuInfo Set Form_Name='TfrmMocSetDC' where System_No='DC' and Form_Name='TfrmMocSet'
Line5=Update ST_menuInfo Set Form_Name='TfrmMocSetSF' where System_No='SF' and Form_Name='TfrmMocSet'
Line6=Update ST_menuInfo Set Menu_No='KQ_0101', image_index=1 where Menu_No='KQ_01' and Para_ID>0
[Step113]
Line1=UPDATE ST_Person set Card_No='' where Card_No is NULL
[Step114]
Line1=IF NOT EXISTS(SELECT *  FROM SYSCOLUMNS WHERE  NAME='Card_No' AND ID=OBJECT_ID('ST_Person')  AND  CDEFAULT<>0)
Line2=BEGIN
Line3=  ALTER Table ST_Person ALTER COLUMN Card_No varchar(12) Not NULL 
Line4=  ALTER TABLE ST_Person ADD CONSTRAINT [Card_NoDefault] DEFAULT ('') FOR [Card_No]
Line5=END
[Step115]
Line1=if not exists (select * from dbo.sysindexes where Name='IX_ST_Person_Card_No')
Line2= CREATE  INDEX [IX_ST_Person_Card_No] ON [dbo].[ST_Person]([Card_No]) ON [PRIMARY]
[Step116]
Line1=IF NOT EXISTS (SELECT name FROM sysindexes WHERE name = 'ConsumeTemp_Person_Idx')
Line2=   CREATE INDEX ConsumeTemp_Person_Idx
Line3=       ON XF_ConsumeTemp (Person_ID,Consume_Time,System_No)
Line4=/**/
Line5=IF NOT EXISTS(SELECT name FROM sysindexes WHERE name = 'Consume_Person_Idx')
Line6=   CREATE INDEX Consume_Person_Idx
Line7=       ON XF_Consume (Person_ID,Consume_Time)
[Step117]
Line1=--1.重領補貼修改說明
Line2=if not Exists(select 1 from syscolumns where id=(select id from sysobjects where name='ST_CARD') and name='Subsidy_Flag')  
Line3=begin
Line4=  alter table ST_CARD add Subsidy_Flag tinyint default 0
Line5=  exec ('update ST_CARD set Subsidy_Flag=0')
Line6=end
[Step118]
System=KQ
Line1=--2.初始化考勤參數﹔
Line2=INSERT INTO dbo.KQ_SysParam(Param_Code,Param_Name,Param_Value)
Line3=SELECT * FROM (SELECT 'Is_Auditing'[Param_Code],'是否啟用審核流程'[Param_Name], '0'[Param_Value]) A
Line4=WHERE A.Param_Code NOT IN(SELECT Param_Code FROM dbo.KQ_SysParam)
[Step119]
System=KQ
Line1=--3.考勤標識
Line2=INSERT INTO dbo.ST_Dictionary(Table_Name,Table_ChsName,Dict_Id,Dict_Value,Is_SysCol)
Line3=SELECT * FROM (SELECT 'BM_KAOQIN'[Table_Name],'考勤標識'[Table_ChsName],'1'[Dict_Id],'正常={}'[Dict_Value],1[Is_SysCol]
Line4=                UNION ALL SELECT 'BM_KAOQIN','考勤標識','2','曠工={曠}',1
Line5=                UNION ALL SELECT 'BM_KAOQIN','考勤標識','3','遲到={<}',1
Line6=                UNION ALL SELECT 'BM_KAOQIN','考勤標識','4','早退={>}',1
Line7=                UNION ALL SELECT 'BM_KAOQIN','考勤標識','5','加班={+}',1
Line8=                UNION ALL SELECT 'BM_KAOQIN','考勤標識','6','請假={假}',1
Line9=              ) A
Line10= WHERE A.Dict_ID NOT IN (SELECT Dict_ID FROM dbo.ST_Dictionary WHERE Table_Name='BM_KAOQIN')
[Step120]
Line1=--1.門禁增加存儲過程
Line2=IF OBJECT_ID('MJ_pd_LoadPersonIn') IS NOT NULL
Line3=   DROP PROCEDURE MJ_pd_LoadPersonIn
[Step121]
System=MJ
Line1=CREATE PROCEDURE MJ_pd_LoadPersonIn
Line2=@UserNo  VARCHAR(20),
Line3=@CtrlNo  VARCHAR(20),
Line4=@DoorNo  VARCHAR(20)
Line5=AS
Line6=BEGIN
Line7=	SELECT a.Person_Id,a.Door_No,a.Ctrl_No,d.Ctrl_Name,b.Person_No,b.Person_Name,  
Line8=	b.Dept_ID,c.Dept_No,c.Dept_Name,b.Card_No,e.PsnTime_Name FROM Mj_psndoorright a 
Line9=	LEFT join st_person b ON a.Person_id = b.Person_ID  LEFT join st_department c
Line10=	ON b.dept_id = c.dept_id  LEFT join mj_Controller d ON a.Ctrl_No = d.Ctrl_No 
Line11=	LEFT join Mj_PsnInOutTime e ON a.PsnTime_No = e.PsnTime_No  
Line12=	WHERE (a.ctrl_no in (SELECT DISTINCT @CtrlNo FROM mj_door)) and 
Line13=	 (a.Door_No in (SELECT DISTINCT @DoorNo FROM mj_door)) and 
Line14=	b.Dept_ID in (SELECT A.Dept_ID FROM ST_DeptRight B  LEFT JOIN ST_Department
Line15=	 A ON B.Dept_ID=A.Dept_ID  WHERE B.User_No= @UserNo)  Order By c.Dept_No
Line16=END
[Step122]

[Step123]

[Step124]

[Step125]

[Step126]

[Step127]

[Step128]
Line1=--5.訂餐系統
Line2=IF OBJECT_ID('SF_Order_pd_UnUseDetailQry') IS NOT NULL
Line3=   DROP PROCEDURE SF_Order_pd_UnUseDetailQry
[Step129]
Line1=------未用餐數據明細查詢
Line2=CREATE     PROCEDURE dbo.SF_Order_pd_UnUseDetailQry
Line3=@qryType INT,			--查詢類別
Line4=@Dept   VARCHAR(20),		--部門條件或員工編號 ,根據查詢類別來確定
Line5=@Repast VARCHAR(500),		--餐別條件
Line6=@Bdate DATETIME,		--起始日期
Line7=@Edate DATETIME			--結束日期
Line8=AS
Line9=set nocount on
Line10=declare @BgnDate varchar(12)
Line11=declare @EndDate varchar(12)
Line12=set @BgnDate=convert(varchar(10),@BDate,120)
Line13=set @EndDate=convert(varchar(10),@EDate,120)
Line14=TRUNCATE TABLE SF_OrderUnUseData
Line15=/*創建臨時表*/
Line16=IF object_ID('tempdb..#TempOderDate') is null
Line17=CREATE TABLE #TempOderDate([Person_Id] [int] NULL,[DS_Date] [varchar] (20) NULL,[Repast_No] [varchar] (10) NULL)
Line18=ELSE
Line19=TRUNCATE TABLE #TempOderDate
Line20=IF object_ID('tempdb..#TempConsume') is null
Line21=CREATE TABLE #TempConsume([Person_Id] [int] NULL ,[DS_Date] [varchar] (20) NULL,[Repast_No] [varchar] (10) NULL)
Line22=ELSE
Line23=TRUNCATE TABLE #TempConsume
Line24=/*訂餐數據*/
Line25=IF @qryType=1
Line26=INSERT INTO #TempOderDate(Person_Id, DS_Date, Repast_no)
Line27=SELECT SF_OrderData.Person_id, Order_Date, Repast_no
Line28=FROM ST_Department INNER JOIN
Line29=ST_Person ON
Line30=ST_Department.Dept_ID = ST_Person.Dept_ID RIGHT OUTER JOIN
Line31=SF_OrderData ON
Line32=ST_Person.Person_ID = SF_OrderData.Person_Id
Line33=WHERE  Dept_No Like @Dept
Line34=AND   charindex(Repast_no, @Repast) > 0
Line35=AND   Order_date <= @EndDate
Line36=AND   Order_date >= @BgnDate
Line37=ELSE
Line38=INSERT INTO #TempOderDate(Person_Id, DS_Date, Repast_no)
Line39=SELECT SF_OrderData.Person_id, Order_Date, Repast_no
Line40=FROM SF_OrderData LEFT JOIN
Line41=ST_Person ON
Line42=ST_Person.Person_ID = SF_OrderData.Person_Id
Line43=WHERE  Person_No =@Dept
Line44=AND   charindex(Repast_no, @Repast) > 0
Line45=AND   Order_date <= @EndDate
Line46=AND   Order_date >= @BgnDate
Line47=/*用餐數據*/
Line48=INSERT INTO #TempConsume(Person_Id, DS_Date, Repast_no)
Line49=SELECT Person_id, CONVERT(varchar(10),Consume_Time,120), Repast_no
Line50=FROM XF_Consume X
Line51=where Exists (Select 1 from #TempOderDate where #TempOderDate.Person_Id =X.Person_Id)
Line52=AND   Consume_Time <= @EndDate + ' 23:59:59'
Line53=AND   Consume_Time >= @BgnDate + ' 00:00:00'
Line54=/*訂餐未用餐數據*/
Line55=INSERT INTO SF_OrderUnUseData(Person_Id , Order_Date , Repast_No)
Line56=SELECT A.Person_Id , A.DS_Date , A. Repast_No FROM #TempOderDate A
Line57=WHERE not Exists(Select 1 from #TempConsume B
Line58=where A.DS_date=B.DS_date
Line59=and   A.Repast_No=B.repast_No
Line60=and   A.person_id=B.Person_id and charindex(Repast_no, @Repast) > 0)
Line61=/**/
Line62=Drop table #TempOderDate
Line63=Drop table #TempConsume
[Step130]
Line1=--6.訂餐系統
Line2=IF OBJECT_ID('SF_Order_pd_UnUseTotalQry') IS NOT NULL
Line3=   DROP PROCEDURE SF_Order_pd_UnUseTotalQry
[Step131]
Line1=------未用餐數據匯總查詢
Line2=CREATE        PROCEDURE dbo.SF_Order_pd_UnUseTotalQry
Line3=@qryType INT,				--查詢類別
Line4=@Dept   VARCHAR(20),			--部門條件或員工編號 ,根據查詢類別來確定
Line5=@Repast VARCHAR(500),			--餐別條件
Line6=@Bdate DATETIME ,			--起始日期
Line7=@Edate DATETIME		                --結束日期
Line8=AS
Line9=SET NOCOUNT ON
Line10=DECLARE @str VARCHAR(5)
Line11=DECLARE @BgnDate VARCHAR(12)
Line12=DECLARE @EndDate VARCHAR(12)
Line13=DECLARE @SQL  VARCHAR(1000)
Line14=DECLARE @i  INT
Line15=DECLARE @j  INT
Line16=SET @BgnDate=CONVERT(VARCHAR(10),@BDate,120)
Line17=SET @EndDate=CONVERT(VARCHAR(10),@EDate,120)
Line18=/*創建 人員餐別表*/
Line19=IF object_ID('SF_OrderUnUseDataTotal') is not null
Line20=DROP table SF_OrderUnUseDataTotal
Line21=SET @SQL=' Create Table SF_OrderUnUseDataTotal([Person_Id] [int] null'
Line22=SET @j=1
Line23=WHILE @j < LEN(@Repast)
Line24=BEGIN
Line25=  SET @i=CHARINDEX(',',@Repast,@j)
Line26=  IF @i>@j
Line27=  BEGIN
Line28=    SET @str=SUBSTRING(@Repast,@j,@i-@j)
Line29=    SET @j=@i+1
Line30=  END
Line31=  ELSE
Line32=  BEGIN
Line33=    SET @str=SUBSTRING(@Repast,@j,LEN(@Repast)-@j+1)
Line34=    SET @j=500
Line35=  END
Line36=  IF LEN(@Str)>0
Line37=    SET @SQL=@SQL+' , [R'+@str+'] [INT] DEFAULT 0'
Line38=END
Line39=SET @SQL=@SQL+')'
Line40=EXEC(@SQL)
Line41=/**插入未用餐人員***/
Line42=IF @qryType=0
Line43=  INSERT INTO SF_OrderUnUseDataTotal([Person_Id])
Line44=  SELECT SF_OrderUnUseData.Person_Id
Line45=  FROM ST_Department INNER JOIN
Line46=  ST_Person ON
Line47=  ST_Department.Dept_ID = ST_Person.Dept_ID RIGHT OUTER JOIN
Line48=  SF_OrderUnUseData ON
Line49=  ST_Person.Person_ID = SF_OrderUnUseData.Person_Id
Line50=  WHERE ST_Department.Dept_No like @Dept
Line51=  and   CHARINDEX(Repast_No,@Repast)>0
Line52=  and   Order_Date <= @EndDate
Line53=  and   Order_Date >= @BgnDate
Line54=  GROUP BY SF_OrderUnUseData.Person_Id
Line55=ELSE
Line56=  INSERT INTO SF_OrderUnUseDataTotal([Person_Id]) Values(0)
Line57=/****匯總******/
Line58=SET @j=1
Line59=WHILE @j < len(@Repast)
Line60=BEGIN
Line61=  SET @i=CHARINDEX(',',@Repast,@j)
Line62=  IF @i>@j
Line63=  BEGIN
Line64=    SET @str=SUBSTRING(@Repast,@j,@i-@j)
Line65=    SET @j=@i+1
Line66=  END
Line67=  ELSE
Line68=  BEGIN
Line69=    SET @str=SUBSTRING(@Repast,@j,LEN(@Repast)-@j+1)
Line70=    SET @j=500
Line71=  end
Line72=  IF Len(@Str)>0
Line73=  BEGIN
Line74=  IF @qryType=0
Line75=    SET @SQL='Update SF_OrderUnUseDataTotal set R'+@str+' = '
Line76=    +' (Select Count(*) from SF_OrderUnUseData '
Line77=    +'  where SF_OrderUnUseData.Repast_No = '    + CHAR(39) + @str + CHAR(39)
Line78=    +'  and   SF_OrderUnUseData.Order_Date <= '  + CHAR(39) + @EndDate + CHAR(39)
Line79=    +'  and   SF_OrderUnUseData.Order_Date >= '  + CHAR(39) + @BgnDate + CHAR(39)
Line80=    +'  and   SF_OrderUnUseData.Person_Id = SF_OrderUnUseDataTotal.Person_Id '
Line81=    +' )'
Line82=  IF @qryType=1
Line83=    SET @SQL='Update SF_OrderUnUseDataTotal set R'+@str+' = '
Line84=    +' (Select Count(*) FROM ST_Department INNER JOIN
Line85=    ST_Person ON
Line86=    ST_Department.Dept_ID = ST_Person.Dept_ID RIGHT OUTER JOIN
Line87=    SF_OrderUnUseData ON
Line88=    ST_Person.Person_ID = SF_OrderUnUseData.Person_Id'
Line89=    +'  where ST_Department.Dept_No like '     + CHAR(39) + @Dept + CHAR(39)
Line90=    +'  and   SF_OrderUnUseData.Repast_No = '  + CHAR(39) + @str + CHAR(39)
Line91=    +'  and   SF_OrderUnUseData.Order_Date <= '+ CHAR(39) + @EndDate + CHAR(39)
Line92=    +'  and   SF_OrderUnUseData.Order_Date >= '+ CHAR(39) + @BgnDate + CHAR(39)
Line93=    +' )'
Line94=    +' where Person_Id = 0 '
Line95=  EXEC(@SQL)
Line96=END
Line97=end
[Step132]
Line1=/*創建XF_Consume表Person_ID索引*/
Line2=if not exists(select * from sysindexes where name='IX_XF_Consume_Person_ID')
Line3=    CREATE  INDEX [IX_XF_Consume_Person_ID] ON [dbo].[XF_Consume]([Person_ID]) ON [PRIMARY]
[Step133]
Line1=/*創建XF_ConsumeHistory表Person_ID索引*/
Line2=if not exists(select * from sysindexes where name='IX_XF_ConsumeHistory_Person_ID')
Line3=    CREATE  INDEX [IX_XF_ConsumeHistory_Person_ID] ON [dbo].[XF_ConsumeHistory]([Person_ID]) ON [PRIMARY]
[Step134]
Line1=--個人充值明細報表
Line2=IF OBJECT_ID('ST_pd_SupplyFundList') IS NOT NULL DROP PROCEDURE ST_pd_SupplyFundList
[Step135]
Line1=/*******************************************/
Line2=/* 類型：報表                              */
Line3=/* 模塊來源：核心平台                      */
Line4=/* 報表名稱：個人充值明細報表          */
Line5=/*                                         */
Line6=/*******************************************/
Line7=CREATE PROCEDURE dbo.ST_pd_SupplyFundList
Line8=(@DateBgn VarChar(40),      --起始日期
Line9=@DateEnd VarChar(40),      --結束日期
Line10=@PersonID Int,             --人員標識號
Line11=@DeptNo VarChar(256) = '', --部門編號
Line12=@UserNo VarChar(40) = ''   --操作員編號
Line13=)
Line14=AS
Line15=Declare @sBgnDate VarChar(16), @sEndDate VarChar(16)
Line16=Set @sBgnDate = '' +  Convert(Char(16), @DateBgn, 20)  + ''
Line17=Set @sEndDate = '' +  Convert(Char(16), @DateEnd, 20)  + ''
Line18=Begin
Line19=    If @UserNo='' --按人員查詢
Line20=        Select A.*,B.Is_Del,B.Card_No,B.Person_Name,B.Person_No,C.Dept_No,C.Dept_Name
Line21=        From (
Line22=             /*獲取取款記錄表的數據*/
Line23=              Select * From ST_SupplyFund
Line24=               Where Person_ID = @PersonID AND Convert(Char(16), Supply_Date, 20) >=  @sBgnDate  and  Convert(Char(16), Supply_Date, 20) <=  @sEndDate
Line25=              Union All
Line26=              Select * From ST_SupplyFundHistory
Line27=               Where Person_ID = @PersonID AND Convert(Char(16), Supply_Date, 20) >=  @sBgnDate  and  Convert(Char(16), Supply_Date, 20) <=  @sEndDate
Line28=              ) A
Line29=        Left Join ST_Person B On B.Person_ID=A.Person_ID
Line30=        Left Join ST_Department C On C.Dept_ID=B.Dept_ID
Line31=    Else
Line32=        Select A.*,B.Is_Del,B.Card_No,B.Person_Name,B.Person_No,C.Dept_No,C.Dept_Name
Line33=        From (
Line34=             /*獲取取款記錄表的數據*/
Line35=              Select * From ST_SupplyFund
Line36=               Where  Convert(Char(16),  Supply_Date, 20)  >=  @sBgnDate  and  Convert(Char(16), Supply_Date, 20) <=  @sEndDate
Line37=              Union All
Line38=              Select * From ST_SupplyFundHistory
Line39=               Where  Convert(Char(16),  Supply_Date, 20)  >=  @sBgnDate  and  Convert(Char(16), Supply_Date, 20) <=  @sEndDate
Line40=              ) A
Line41=        Left Join ST_Person B On B.Person_ID=A.Person_ID
Line42=        Left Join ST_Department C On C.Dept_ID=B.Dept_ID
Line43=       /**跟據部門及部門所涉及的用戶顯示權限進行數據過慮*/
Line44=         Where  C.Dept_No LIKE @DeptNo+'%'
Line45=           AND Exists (Select Dept_ID From ST_DeptRight Where C.Dept_ID =ST_DeptRight.Dept_ID
Line46=            AND User_No=@UserNo AND Check_State=0)
Line47=END
[Step136]
Line1=-- 報表名稱：個人取款明細報表                  
Line2=IF OBJECT_ID('ST_pd_TakeFundList') IS NOT NULL DROP PROCEDURE ST_pd_TakeFundList
[Step137]
Line1=/*******************************************/
Line2=/* 類型：報表                              */
Line3=/* 模塊來源：核心平台                      */
Line4=/* 報表名稱：個人取款明細報表                  */
Line5=/*                                         */
Line6=/*******************************************/
Line7=CREATE  PROCEDURE dbo.ST_pd_TakeFundList
Line8=(@DateBgn VarChar(40),      --起始日期
Line9=@DateEnd VarChar(40),      --結束日期
Line10=@PersonID INT,             --人員標識號
Line11=@DeptNo VarChar(256) = '', --部門編號
Line12=@UserNo VarChar(40) = ''   --操作員編號
Line13=)
Line14=AS
Line15=Declare @sBgnDate VarChar(16), @sEndDate VarChar(16)
Line16=SET @sBgnDate = '' +  Convert(Char(16), @DateBgn, 20)  + ''
Line17=SET @sEndDate = '' +  Convert(Char(16), @DateEnd, 20)  + ''
Line18=Begin
Line19=    If @UserNo='' --按人員查詢
Line20=            Select A.*,B.Is_Del,B.Card_No,B.Person_Name,B.Person_No,C.Dept_No,C.Dept_Name
Line21=            From (
Line22=             /*獲取小取款記錄表的數據*/
Line23=                  Select * From ST_TakeFund
Line24=                   Where Person_ID=@PersonID AND
Line25=                    Convert(Char(16),  Take_Date, 20)  >=  @sBgnDate  and  Convert(Char(16), Take_Date, 20) <=  @sEndDate
Line26=                  Union All
Line27=                  Select * From ST_TakeFundHistory
Line28=                   Where Person_ID=@PersonID AND Convert(Char(16),  Take_Date, 20)  >=  @sBgnDate  and  Convert(Char(16), Take_Date, 20) <=  @sEndDate
Line29=                 ) A
Line30=            Left Join ST_Person B On B.Person_ID=A.Person_ID
Line31=            Left Join ST_Department C On C.Dept_ID=B.Dept_ID
Line32=    ELSE
Line33=            Select A.*,B.Is_Del,B.Card_No,B.Person_Name,B.Person_No,C.Dept_No,C.Dept_Name
Line34=            From (
Line35=             /*獲取小取款記錄表的數據*/
Line36=                  Select * From ST_TakeFund
Line37=                   Where Convert(Char(16),  Take_Date, 20)  >=  @sBgnDate  and  Convert(Char(16), Take_Date, 20) <=  @sEndDate
Line38=                  Union All
Line39=                  Select * From ST_TakeFundHistory
Line40=                   Where Convert(Char(16),  Take_Date, 20)  >=  @sBgnDate  and  Convert(Char(16), Take_Date, 20) <=  @sEndDate
Line41=                 ) A
Line42=            Left Join ST_Person B On B.Person_ID=A.Person_ID
Line43=            Left Join ST_Department C On C.Dept_ID=B.Dept_ID
Line44=       /**跟據部門及部門所涉及的用戶顯示權限進行數據過慮*/
Line45=             Where  C.Dept_No LIKE @DeptNo+'%'
Line46=              AND EXISTS (Select Dept_ID From ST_DeptRight Where C.Dept_ID =ST_DeptRight.Dept_ID
Line47=               AND User_No=@UserNo AND Check_State=0)
Line48=END
[Step138]
Line1=--個人轉賬明細報表
Line2=IF OBJECT_ID('ST_pd_TransferAccountList') IS NOT NULL DROP PROCEDURE ST_pd_TransferAccountList
[Step139]
Line1=/*******************************************/
Line2=/* 類型：報表                              */
Line3=/* 模塊來源：核心平台                      */
Line4=/* 報表名稱：個人轉賬明細報表                  */
Line5=/*                                         */
Line6=/*******************************************/
Line7=CREATE PROCEDURE dbo.ST_pd_TransferAccountList
Line8=(@DateBgn VarChar(40),      --起始日期
Line9=@DateEnd VarChar(40),      --結束日期
Line10=@PersonID INT,             --人員標識號
Line11=@DeptNo VarChar(256) = '', --部門編號
Line12=@UserNo VarChar(40) = ''   --操作員編號
Line13=)
Line14=AS
Line15=Declare @sBgnDate VarChar(16), @sEndDate VarChar(16)
Line16=Set @sBgnDate = '' +  Convert(Char(16), @DateBgn, 20)  + ''
Line17=Set @sEndDate = '' +  Convert(Char(16), @DateEnd, 20)  + ''
Line18=Begin
Line19=    If @UserNo='' --按人員查詢
Line20=        Select A.*,B.Is_Del,B.Card_No,B.Person_Name,B.Person_No,C.Dept_No,C.Dept_Name
Line21=        From (
Line22=             /*獲取小金額轉賬表的數據*/
Line23=              Select * From ST_TransferAccount
Line24=               Where Person_ID=@PersonID AND  Convert(Char(16),  Transfer_Date, 20)  >=  @sBgnDate  and  Convert(Char(16), Transfer_Date, 20) <=  @sEndDate
Line25=              Union All
Line26=              Select * From ST_TransferAccountHistory
Line27=               Where Person_ID=@PersonID AND Convert(Char(16),  Transfer_Date, 20)  >=  @sBgnDate  and  Convert(Char(16), Transfer_Date, 20) <=  @sEndDate
Line28=              ) A
Line29=        Left JOin ST_Person B On B.Person_ID=A.Person_ID
Line30=        Left JOin ST_Department C On C.Dept_ID=B.Dept_ID
Line31=    Else
Line32=        Select A.*,B.Is_Del,B.Card_No,B.Person_Name,B.Person_No,C.Dept_No,C.Dept_Name
Line33=        From (
Line34=             /*獲取小金額轉賬表的數據*/
Line35=              Select * From ST_TransferAccount
Line36=               Where Convert(Char(16),  Transfer_Date, 20)  >=  @sBgnDate  and  Convert(Char(16), Transfer_Date, 20) <=  @sEndDate
Line37=              Union All
Line38=              Select * From ST_TransferAccountHistory
Line39=               Where Convert(Char(16),  Transfer_Date, 20)  >=  @sBgnDate  and  Convert(Char(16), Transfer_Date, 20) <=  @sEndDate
Line40=             ) A
Line41=        Left JOin ST_Person B On B.Person_ID=A.Person_ID
Line42=        Left JOin ST_Department C On C.Dept_ID=B.Dept_ID
Line43=       /**跟據部門及部門所涉及的用戶顯示權限進行數據過慮*/
Line44=         Where  C.Dept_No LIKE @DeptNo+'%'
Line45=          AND EXISTS (Select Dept_ID From ST_DeptRight Where C.Dept_ID =ST_DeptRight.Dept_ID
Line46=           AND User_No=@UserNo AND Check_State=0)
Line47=END
[Step140]
Line1=--注銷人員明細
Line2=IF OBJECT_ID('st_pd_DelDetail') IS NOT NULL DROP PROCEDURE st_pd_DelDetail
[Step141]
Line1=/*******************************************/
Line2=/* 類型：報表                              */
Line3=/* 模塊來源：核心平台                      */
Line4=/* 報表名稱：注銷人員明細                  */
Line5=/*                                         */
Line6=/*******************************************/
Line7=CREATE PROCEDURE st_pd_DelDetail
Line8=@UserNo NVarChar(40), --用戶編號
Line9=@aValue NVarChar(20),--部門樹選擇傳遞進來的值
Line10=@DelDateBgn NVarChar(20),--開始日期
Line11=@DelDateEnd NVarChar(20), --結束日期
Line12=@IsDept  Int       --是否選擇的是部門
Line13=AS
Line14=Begin
Line15=  IF @IsDept = 1  --判斷是否是部門
Line16=Begin
Line17=Select a.Dept_No,a.Dept_Name,a.PersOn_No,a.PersOn_Name,a.Card_No,b.* From 
Line18=(
Line19=  Select Card_No,IS_Del,PersOn_No,Dept_No,PersOn_Name,Dept_Name,PersOn_ID From V_STPersOn  
Line20=  Where Exists(
Line21=		Select Dept_ID From ST_DeptRight  
Line22=		Where Dept_ID=V_STPersOn.Dept_ID AND Check_State=0    and User_No = @UserNo
Line23=               ) AND Is_Del=1 AND (Dept_No like @aValue + '%')
Line24=) a 
Line25=Left JOIN ST_DeleteCard b On a.PersOn_ID = b.PersOn_ID 
Line26=Where COnvert(Char(16),b.Del_Date,20) >=@DelDateBgn
Line27=AND   COnvert(Char(16),b.Del_Date,20) <=@DelDateEnd
Line28=End
Line29=End
[Step142]

[Step143]

[Step144]
Line1=--個人換用新卡明細報表
Line2=IF OBJECT_ID('ST_pd_AlterCardList') IS NOT NULL DROP PROCEDURE ST_pd_AlterCardList
[Step145]
Line1=/*******************************************/
Line2=/* 類型：報表                              */
Line3=/* 模塊來源：核心平台                      */
Line4=/* 報表名稱：個人換用新卡明細報表          */
Line5=/*                                         */
Line6=/*******************************************/
Line7=CREATE PROCEDURE dbo.ST_pd_AlterCardList
Line8=(
Line9=    @AlterType TinyInt,        --更改類型
Line10=    @DateBgn VarChar(40),      --起始日期
Line11=    @DateEnd VarChar(40),      --結束日期
Line12=    @PersonID Int,             --人員標識號
Line13=    @DeptNo VarChar(256) = '', --部門編號
Line14=    @UserNo VarChar(40) = ''   --操作員編號
Line15=)
Line16=AS
Line17=Declare @sBgnDate VarChar(16), @sEndDate VarChar(16)
Line18=Set @sBgnDate = '' +  Convert(Char(16), @DateBgn, 20)  + ''
Line19=Set @sEndDate = '' +  Convert(Char(16), @DateEnd, 20)  + ''
Line20=    If @UserNo='' --按人員查詢
Line21=        Select A.*,B.Is_Del,B.Person_Name,B.Person_No,C.Dept_No,C.Dept_Name
Line22=        From (
Line23=              /*獲取卡變更表的數據*/
Line24=              Select * From ST_AlterCard
Line25=               Where Person_ID=@PersonID AND Alter_Type=@AlterType
Line26=                AND Convert(Char(16),  Alter_Date, 20)  >=  @sBgnDate  and  Convert(Char(16), Alter_Date, 20) <=  @sEndDate
Line27=              Union ALL
Line28=              Select * From ST_AlterCardHistory
Line29=               Where Person_ID=@PersonID AND Alter_Type=@AlterType
Line30=                AND Convert(Char(16),  Alter_Date, 20)  >=  @sBgnDate  and  Convert(Char(16), Alter_Date, 20) <=  @sEndDate
Line31=              ) A
Line32=        Left Join ST_Person B ON B.Person_ID=A.Person_ID
Line33=        Left Join ST_Department C ON C.Dept_ID=B.Dept_ID
Line34=    Else
Line35=        Select A.*,B.Is_Del,B.Person_Name,B.Person_No,C.Dept_No,C.Dept_Name
Line36=        From (
Line37=              /*獲取卡變更表的數據*/
Line38=              Select * From ST_AlterCard
Line39=               Where Alter_Type=@AlterType AND Convert(Char(16),Alter_Date, 20)  >=  @sBgnDate  and  Convert(Char(16), Alter_Date, 20) <=  @sEndDate
Line40=              Union ALL
Line41=              Select * From ST_AlterCardHistory
Line42=               Where Alter_Type=@AlterType AND Convert(Char(16),Alter_Date, 20)  >=  @sBgnDate  and  Convert(Char(16), Alter_Date, 20) <=  @sEndDate
Line43=              ) A
Line44=        Left Join ST_Person B ON B.Person_ID=A.Person_ID
Line45=        Left Join ST_Department C ON C.Dept_ID=B.Dept_ID
Line46=       /**跟據部門及部門所涉及的用戶顯示權限進行數據過慮*/
Line47=         Where  C.Dept_No LIKE @DeptNo+'%'
Line48=          AND EXISTS (Select Dept_ID From ST_DeptRight Where C.Dept_ID =ST_DeptRight.Dept_ID
Line49=           AND User_No=@UserNo AND Check_State=0)
[Step146]
Line1=--個人發放補貼明細報表 
Line2=IF OBJECT_ID('ST_pd_SubsidyList') IS NOT NULL DROP PROCEDURE ST_pd_SubsidyList
[Step147]
Line1=/*******************************************/
Line2=/* 類型：報表                              */
Line3=/* 模塊來源：核心平台                      */
Line4=/* 報表名稱：個人發放補貼明細報表          */
Line5=/*                                         */
Line6=/*******************************************/
Line7=CREATE PROCEDURE dbo.ST_pd_SubsidyList
Line8=(
Line9=    @DateBgn VarChar(40),      --起始日期
Line10=    @DateEnd VarChar(40),      --結束日期
Line11=    @PersonID Int,             --人員標識號
Line12=    @DeptNo VarChar(256) = '', --部門編號
Line13=    @UserNo VarChar(40) = ''   --操作員編號
Line14=)
Line15=AS
Line16=Declare @sBgnDate VarChar(16), @sEndDate VarChar(16)
Line17=Set @sBgnDate = '' +  Convert(Char(16), @DateBgn, 20)  + ''
Line18=Set @sEndDate = '' +  Convert(Char(16), @DateEnd, 20)  + ''
Line19=    If @UserNo='' --按人員查詢
Line20=        Select A.*,B.Is_Del,B.Person_Name,B.Person_No,C.Dept_No,C.Dept_Name,Card_No
Line21=        From (
Line22=             /*獲取補貼記錄表的數據*/
Line23=              Select * From ST_Subsidy
Line24=               Where Person_ID = @PersonID AND Convert(Char(16), Subsidy_Date, 20) >=  @sBgnDate  and  Convert(Char(16), Subsidy_Date, 20) <=  @sEndDate
Line25=              Union ALL
Line26=              Select * From ST_SubsidyHistory
Line27=               Where Person_ID = @PersonID AND Convert(Char(16), Subsidy_Date, 20) >=  @sBgnDate  and  Convert(Char(16), Subsidy_Date, 20) <=  @sEndDate
Line28=              ) A
Line29=        Left Join ST_Person B On B.Person_ID=A.Person_ID
Line30=        Left Join ST_Department C On C.Dept_ID=B.Dept_ID
Line31=    Else
Line32=        Select A.*,B.Is_Del,B.Person_Name,B.Person_No,C.Dept_No,C.Dept_Name,Card_No
Line33=        From (
Line34=             /*獲取補貼記錄表的數據*/
Line35=              Select * From ST_Subsidy
Line36=               Where Convert(Char(16), Subsidy_Date, 20)  >=  @sBgnDate  and  Convert(Char(16), Subsidy_Date, 20) <=  @sEndDate
Line37=              Union ALL
Line38=              Select * From ST_SubsidyHistory
Line39=               Where  Convert(Char(16), Subsidy_Date, 20) >=  @sBgnDate  and  Convert(Char(16), Subsidy_Date, 20) <=  @sEndDate
Line40=              ) A
Line41=        Left Join ST_Person B On B.Person_ID=A.Person_ID
Line42=        Left Join ST_Department C On C.Dept_ID=B.Dept_ID
Line43=       /**跟據部門及部門所涉及的用戶顯示權限進行數據過慮*/
Line44=         Where  C.Dept_No LIKE @DeptNo+'%'
Line45=          AND EXISTS (Select Dept_ID From ST_DeptRight Where C.Dept_ID =ST_DeptRight.Dept_ID
Line46=           AND User_No=@UserNo AND Check_State=0)
[Step148]
Line1=--處理假寫卡
Line2=IF OBJECT_ID('XF_pd_ConsumeMatchingAll') IS NOT NULL DROP PROCEDURE XF_pd_ConsumeMatchingAll
[Step149]
Line1=/*********************************************/
Line2=/* 類型：業務計算                            */
Line3=/* 模塊來源：核心平台                        */
Line4=/* 過程名稱：處理假寫卡                      */
Line5=/*                                           */
Line6=/*********************************************/
Line7=CREATE PROCEDURE dbo.XF_pd_ConsumeMatchingAll
Line8=AS
Line9=Declare @PersonID INT
Line10=--1.獲取所有有假寫卡記錄的人員
Line11=Declare persons_cursor Cursor Local Forward_only Static Read_only for 
Line12=    Select Distinct Person_ID From XF_ConsumeFake Where Data_Type=8
Line13=/*formerly：  Declare persons_cursor Cursor Local  for 
Line14=   Select Person_ID From XF_ConsumeFake Where Data_Type=8 Group By Person_ID */
Line15=Open persons_cursor
Line16=Fetch Next From persons_cursor Into @PersonID
Line17=While @@Fetch_STATUS = 0
Line18=BEGIN
Line19=   /*處理指定人員的假寫卡記錄*/
Line20=    Execute XF_pd_ConsumeMatchingPerson @PersonID
Line21=    Fetch Next From persons_cursor Into @PersonID
Line22=End
Line23=Close persons_cursor
Line24=DealLocate persons_cursor
[Step150]
Line1=--處理假寫卡內套過程
Line2=IF OBJECT_ID('XF_pd_ConsumeMatchingPerson') IS NOT NULL DROP PROCEDURE XF_pd_ConsumeMatchingPerson
[Step151]
Line1=/*********************************************/
Line2=/* 類型：業務計算                            */
Line3=/* 模塊來源：核心平台                        */
Line4=/* 過程名稱：處理假寫卡內套過程              */
Line5=/*                                           */
Line6=/*********************************************/
Line7=CREATE PROCEDURE dbo.XF_pd_ConSumeMatchingPerson
Line8=(
Line9=@PersonID INT
Line10=)
Line11=AS
Line12=Declare @ConSumeTimeBgn DateTime,/*消費開始日期*/
Line13=             @ConSumeTimeEnd DateTime,/*消費結事日期*/
Line14=             @CardBalance Money,/*主帳戶余額*/
Line15=             @ICBalance Money,/*卡余額存*/
Line16=             @DBBalance Money,/*庫余額*/
Line17=             @BeForeBalance Money,/*消費金額*/
Line18=             @IDKey INT,/*關鍵字段*/ 
Line19=             @Result Tinyint,
Line20=             @OtherFund Money,/*發放金額*/
Line21=             @ConSumeFund Money/*消費金額*/
Line22=/*1.取得卡余額*/
Line23=Select @ICBalance=Card_Balance 
Line24=From (
Line25=    Select Top 1 Card_Balance
Line26=    From (
Line27=        Select Person_ID,Max(ConSume_Time)[ConSume_Time] From XF_ConSume
Line28=         Where Person_ID=@PersonID Group BY Person_ID) B
Line29=    Left Join (Select Card_Balance,ConSume_Time From XF_ConSume Where Person_ID=@PersonID) A On B.ConSume_Time=A.ConSume_Time
Line30=) C
Line31=/*2.取得庫余額*/
Line32=Select @DBBalance=Card_Balance
Line33=     From St_Card Where Person_ID=@PersonID
Line34=/*或者該人員已經被注銷*/
Line35=IF (@ICBalance = @DBBalance) OR (@DBBalance IS NULL) 
Line36=Begin
Line37=    Update XF_ConSumeFake Set Data_Type=82,Matching_Time=GetDate() Where Person_ID=@PersonID AND Data_Type=8 --更新數據標識為無效
Line38=    Return --如果卡庫平衡就不處理
Line39=End
Line40=/*3.處理假寫卡記錄*/
Line41=Declare conSumes_Cursor Cursor Local Forward_only Static Read_only for 
Line42=    Select ConSume_Time,ConSume_Fund,Card_Balance,ID_Key From XF_ConSumeFake
Line43=    Where Person_ID=@PersonID AND Data_Type=8
Line44=    Order By ConSume_Time Desc
Line45=Open conSumes_Cursor
Line46=Fetch Next From conSumes_Cursor Into @ConSumeTimeBgn,@ConSumeFund,@CardBalance,@IDKey
Line47=While @@FETCH_STATUS = 0
Line48=Begin
Line49=    /*如果卡庫平衡，就不處理相應操作*/
Line50=    IF (@ICBalance = @DBBalance)
Line51=    Begin
Line52=        Update XF_ConSumeFake Set Data_Type=82,Matching_Time=GetDate() Where Person_ID=@PersonID AND Data_Type=8 --更新數據標識為無效
Line53=        Break
Line54=    End
Line55=   /*獲取消費金額*/
Line56=    Select Top 1 @BeForeBalance=ConSume_Fund+Card_Balance,@ConSumeTimeEnd=ConSume_Time From XF_ConSume
Line57=         Where Person_ID=@PersonID AND ConSume_Time>@ConSumeTimeBgn Order By ConSume_Time
Line58=     /*初始化為不處理*/
Line59=    Set @Result = 83
Line60=    /*是否找到下一條消費記錄*/
Line61=    IF (@BeForeBalance IS NOT NULL) 
Line62=    Begin
Line63=        /*假寫卡是否可以匹配得上*/
Line64=        IF (@BeForeBalance <> @CardBalance) 
Line65=        Begin
Line66=          /*充值金額*/
Line67=            Select @OtherFund=IsNull(Sum(Supply_Fund),0) From ST_SupplyFund
Line68=                Where Person_ID=@PersonID AND (Supply_Date>=@ConSumeTimeBgn AND Supply_Date<=@ConSumeTimeEnd)
Line69=         /*取款金額*/
Line70=            Select @OtherFund=@OtherFund-IsNull(Sum(Take_Fund),0) From ST_TakeFund
Line71=                Where Person_ID=@PersonID AND (Take_Date>=@ConSumeTimeBgn AND Take_Date<=@ConSumeTimeEnd)
Line72=         /*轉帳金額*/
Line73=            Select @OtherFund=@OtherFund-IsNull(Sum(Transfer_Fund),0) From ST_TransferAccount
Line74=                Where Person_ID=@PersonID AND (Transfer_Date>=@ConSumeTimeBgn AND Transfer_Date<=@ConSumeTimeEnd)
Line75=           /*出納處理后的假寫卡是否可以匹配得上*/
Line76=            If (@BeForeBalance = @CardBalance+@OtherFund) 
Line77=             /*假定卡有效*/
Line78=                Set @Result = 81
Line79=            Else If (@BeForeBalance > @CardBalance) 
Line80=            /*假定卡無效*/
Line81=                Set @Result = 82
Line82=        End
Line83=        Else
Line84=        -- JenanWang 2008.1.8 修正
Line85=        Begin
Line86=            Select Top 1 @BeForeBalance=Card_Balance From XF_ConSume
Line87=                Where Person_ID=@PersonID AND ConSume_Time<@ConSumeTimeBgn Order By ConSume_Time Desc
Line88=           /*假寫卡是否為重復記錄*/
Line89=            If (@BeForeBalance <> @CardBalance) 
Line90=                /*假寫有效*/
Line91=                Set @Result = 81 
Line92=            Else
Line93=                /*假定卡無效*/
Line94=                Set @Result = 82 
Line95=        End
Line96=        /*假寫卡記錄需要處理*/
Line97=        IF (@Result < 83) 
Line98=        Begin
Line99=            /*更新數據標識*/
Line100=            Update XF_ConSumeFake Set Data_Type=@Result,Matching_Time=GetDate() Where ID_Key=@IDKey 
Line101=            /*假寫卡有效 */
Line102=            IF (@Result = 81) 
Line103=            Begin
Line104=               /*插入消費記錄*/
Line105=                Insert Into XF_ConSume(Person_ID,System_No,ConSume_Mode,ConSume_Time,BeFore_Fund,ConSume_Fund,ConSume_Copy,
Line106=                    ConSume_TimeLen,ConSume_Capacity,Repast_No,Ware_ID,Moc_No,COM_No,Card_Balance,
Line107=                        DB_Balance,Card_SmallAccount,Data_Type,Download_Time)
Line108=                Select Person_ID,System_No,ConSume_Mode,ConSume_Time,BeFore_Fund,ConSume_Fund,ConSume_Copy,
Line109=                    ConSume_TimeLen,ConSume_Capacity,Repast_No,Ware_ID,Moc_No,COM_No,Card_Balance,
Line110=                        DB_Balance,Card_SmallAccount,0,Download_Time
Line111=                From XF_ConSumeFake Where ID_Key=@IDKey
Line112=               /*更新卡號金額表中的余額*/
Line113=                Update ST_Card Set Card_Balance=Card_Balance-@ConSumeFund Where Person_ID=@PersonID 
Line114=                /*更新@DBBalance值*/
Line115=                Set @DBBalance=@DBBalance-@ConSumeFund 
Line116=            End
Line117=        End
Line118=    End
Line119=    Fetch Next From conSumes_Cursor Into @ConSumeTimeBgn,@ConSumeFund,@CardBalance,@IDKey
Line120=End
Line121=CLOSE conSumes_Cursor
Line122=DEALLOCATE conSumes_Cursor
[Step152]
Line1=--個人綜合報表  
Line2=IF OBJECT_ID('st_pd_PersonalRep') IS NOT NULL DROP PROCEDURE st_pd_PersonalRep
[Step153]
Line1=/*******************************************/
Line2=/* 類型：報表                              */
Line3=/* 模塊來源：核心平台                      */
Line4=/* 報表名稱：個人綜合報表                  */
Line5=/*                                         */
Line6=/*******************************************/
Line7=CREATE  PROCEDURE st_pd_PersonalRep
Line8=@DeptNo  VarChar(40),    --部門編號
Line9=@UserNo  VarChar(40),    --用戶編號
Line10=@DateBgn DateTime,       --統計起始日期
Line11=@DateEnd DateTime,        --統計終止日期
Line12=@IsDel   Bit             --是否顯示注銷人員
Line13=AS
Line14=Declare @sBgnDate VarChar(16), @sEndDate VarChar(16)
Line15=Set @sBgnDate = '' +  Convert(Char(16), @DateBgn, 20)  + ''
Line16=Set @sEndDate = '' +  Convert(Char(16), @DateEnd, 20)  + ''
Line17=IF Object_ID('MyTemp..#temp1') Is NOT NULL
Line18=    Drop Table #temp1
Line19=Create Table #temp1 (Person_ID int)
Line20=--是否顯示注銷人員
Line21=IF @IsDel=0
Line22=Begin
Line23=    /*統計發生業務的人員*/
Line24=    Insert  Into  #temp1(Person_ID)
Line25=        Select C.Person_ID
Line26=        From ST_Card A
Line27=        Left Join ST_Person C ON C.Person_ID=A.Person_ID
Line28=        Left Join ST_Department B ON B.Dept_ID=C.Dept_ID
Line29=        Where B.Dept_No LIKE @DeptNo+'%' AND B.Dept_ID IN (Select Dept_ID From ST_DeptRight Where Check_State=0 AND User_No=@UserNo)
Line30=        Order By B.Dept_No,C.Person_No
Line31=    /*查詢相關信息*/
Line32=    Select C.Person_No,C.Person_Name,B.Dept_No,B.Dept_Name,Card_Balance,C.Card_No,
Line33=        IsNull(Supply_Fund,0) as [Supply_Fund],IsNull(ConSume_Fund,0) as [ConSume_Fund],
Line34=        IsNull(D.Subsidy_Fund,0) as [Subsidy_Fund],IsNull(Take_Fund,0) as [Take_Fund],
Line35=        IsNull(Transfer_Fund,0) as [Transfer_Fund],
Line36=        IsNull(Supply_Fund,0)+IsNull(D.Subsidy_Fund,0)-(IsNull(ConSume_Fund,0)+IsNull(Take_Fund,0)+IsNull(Transfer_Fund,0)+Card_Balance)[Mny_Balance]
Line37=      From ST_Card A
Line38=      Left Join ST_Person C ON C.Person_ID=A.Person_ID
Line39=      Left Join ST_Department B ON B.Dept_ID=C.Dept_ID
Line40=      Left Join (
Line41=      /*統計發生金額*/
Line42=       Select Person_ID,Sum(Supply_Fund) Supply_Fund,Sum(ConSume_Fund) ConSume_Fund,Sum(Subsidy_Fund) Subsidy_Fund,Sum(Take_Fund) Take_Fund,Sum(Transfer_Fund) Transfer_Fund
Line43=       From
Line44=       (
Line45=      /*1.累計存款金額(當前的數據)*/                                                                                   
Line46=          Select Person_ID,Sum(Supply_Fund) as Supply_Fund/*存款金額*/,0 as ConSume_Fund/*消費金額*/ ,0 as Subsidy_Fund  /*補貼金額*/,0 as Take_Fund/*取款金額*/  ,0 as Transfer_Fund /*轉帳金額*/ 
Line47=          From ST_SupplyFund
Line48=          Where Convert(Char(16), Supply_Date, 20)  >=  @sBgnDate and
Line49=              Convert(Char(16), Supply_Date, 20) <=  @sEndDate
Line50=              and Person_ID in (Select Distinct Person_ID From #temp1)
Line51=          Group By Person_ID
Line52=          Union All
Line53=      /*累計存款金額(歷史的數據)*/
Line54=          Select Person_ID,Sum(Supply_Fund) as Supply_Fund,0,0,0,0
Line55=          From ST_SupplyFundHistory
Line56=          Where Convert(Char(16), Supply_Date, 20)  >=  @sBgnDate and
Line57=              Convert(Char(16), Supply_Date, 20) <=  @sEndDate
Line58=              and Person_ID in (Select Distinct Person_ID From #temp1) 
Line59=              Group By Person_ID
Line60=          Union All
Line61=      /*2.累計消費金額(當前的數據)*/
Line62=          Select Person_ID,0,Sum(ConSume_Fund)[ConSume_Fund],0,0,0
Line63=          From XF_ConSume
Line64=          Where Convert(Char(16), ConSume_Time, 20)  >=  @sBgnDate and
Line65=              Convert(Char(16), ConSume_Time, 20) <=  @sEndDate
Line66=              and Person_ID in (Select Distinct Person_ID From #temp1) 
Line67=          Group By Person_ID
Line68=          Union All
Line69=      /*累計消費金額(歷史的數據)*/
Line70=          Select Person_ID,0,Sum(ConSume_Fund)[ConSume_Fund],0,0,0
Line71=          From XF_ConSumeHistory
Line72=          Where Convert(Char(16), ConSume_Time, 20)  >=  @sBgnDate and
Line73=              Convert(Char(16), ConSume_Time, 20) <=  @sEndDate
Line74=              and Person_ID in (Select Distinct Person_ID From #temp1)
Line75=              Group By Person_ID
Line76=          Union All
Line77=      /*3.累計補貼金額(當前的數據)*/
Line78=          Select Person_ID,0,0,Sum(Subsidy_Fund)[Subsidy_Fund],0,0
Line79=          From ST_Subsidy
Line80=          Where Convert(Char(16), Subsidy_Date, 20)  >=  @sBgnDate and
Line81=              Convert(Char(16), Subsidy_Date, 20) <=  @sEndDate
Line82=              and Person_ID in (Select Distinct Person_ID From #temp1) 
Line83=              Group By Person_ID
Line84=          Union All
Line85=      /*累計補貼金額(歷史的數據)*/
Line86=          Select Person_ID,0,0,Sum(Subsidy_Fund)[Subsidy_Fund],0,0
Line87=          From ST_SubsidyHistory
Line88=          Where Convert(Char(16), Subsidy_Date, 20)  >=  @sBgnDate and
Line89=              Convert(Char(16), Subsidy_Date, 20) <=  @sEndDate
Line90=              and Person_ID in (Select Distinct Person_ID From #temp1)
Line91=              Group By Person_ID
Line92=          Union All
Line93=      /*4.累計取款金額(當前的數據)*/
Line94=          Select Person_ID,0,0,0,Sum(Take_Fund)[Take_Fund],0
Line95=          From ST_TakeFund
Line96=          Where Convert(Char(16), Take_Date, 20)  >=  @sBgnDate and
Line97=             Convert(Char(16), Take_Date, 20) <=  @sEndDate
Line98=             and Person_ID in (Select Distinct Person_ID From #temp1) 
Line99=             Group By Person_ID
Line100=          Union All
Line101=      /*累計取款金額(歷史的數據)*/
Line102=          Select Person_ID,0,0,0,Sum(Take_Fund)[Take_Fund],0
Line103=          From ST_TakeFundHistory
Line104=          Where Convert(Char(16), Take_Date, 20)  >=  @sBgnDate and
Line105=             Convert(Char(16), Take_Date, 20) <=  @sEndDate
Line106=             and Person_ID in (Select Distinct Person_ID From #temp1)
Line107=             Group By Person_ID
Line108=          Union All
Line109=      /*5.累計轉帳金額(當前的數據)*/
Line110=          Select Person_ID,0,0,0,0,Sum(Transfer_Fund)[Transfer_Fund]
Line111=          From ST_TransferAccount
Line112=          Where Convert(Char(16), Transfer_Date, 20)  >=  @sBgnDate and
Line113=              Convert(Char(16), Transfer_Date, 20) <=  @sEndDate
Line114=              and Person_ID in (Select Distinct Person_ID From #temp1) 
Line115=              Group By Person_ID
Line116=          Union All
Line117=      /*累計轉帳金額(歷史的數據)*/
Line118=          Select Person_ID,0,0,0,0,Sum(Transfer_Fund)[Transfer_Fund]
Line119=          From ST_TransferAccountHistory
Line120=          Where Convert(Char(16), Transfer_Date, 20)  >=  @sBgnDate and
Line121=              Convert(Char(16), Transfer_Date, 20) <=  @sEndDate
Line122=              and Person_ID in (Select Distinct Person_ID From #temp1)
Line123=              Group By Person_ID
Line124=    ) ss  Group By Person_ID) D  on A.Person_ID=D.Person_ID
Line125=    Where B.Dept_No LIKE @DeptNo+'%' AND B.Dept_ID IN (Select Dept_ID From ST_DeptRight Where Check_State=0 AND User_No=@UserNo)
Line126=        Order By B.Dept_No,C.Person_No
Line127=END
Line128=ELSE
Line129=Begin
Line130=    /*統計發生業務的人員*/
Line131=    Insert  Into  #temp1(Person_ID)
Line132=        Select C.Person_ID
Line133=        From ST_Person C 
Line134=        Left Join ST_Card A ON C.Person_ID=A.Person_ID
Line135=        Left Join ST_Department B ON B.Dept_ID=C.Dept_ID
Line136=        Where B.Dept_No LIKE @DeptNo+'%' AND B.Dept_ID IN (Select Dept_ID From ST_DeptRight Where Check_State=0 AND User_No=@UserNo)
Line137=    /*查詢相關信息*/
Line138=    Select A.Person_No,A.Person_Name,B.Dept_No,B.Dept_Name,IsNull(Card_Balance,0)[Card_Balance],Card_No,A.IS_DEL,
Line139=        IsNull(Supply_Fund,0)[Supply_Fund],IsNull(ConSume_Fund,0)[ConSume_Fund],
Line140=        IsNull(I.Subsidy_Fund,0)[Subsidy_Fund],IsNull(Take_Fund,0)[Take_Fund],
Line141=        IsNull(Transfer_Fund,0)[Transfer_Fund],
Line142=        IsNull(Supply_Fund,0)+IsNull(I.Subsidy_Fund,0)-(IsNull(ConSume_Fund,0)+IsNull(Take_Fund,0)+IsNull(Transfer_Fund,0)+Card_Balance)[Mny_Balance]
Line143=      From ST_Person A
Line144=      Left Join ST_Card C ON  A.Person_ID=C.Person_ID
Line145=      Left Join ST_Department B ON A.Dept_ID=B.Dept_ID
Line146=      Left Join (
Line147=      /*統計發生金額*/
Line148=       Select Person_ID,Sum(Supply_Fund) Supply_Fund,Sum(ConSume_Fund) ConSume_Fund,Sum(Subsidy_Fund) Subsidy_Fund,Sum(Take_Fund) Take_Fund,Sum(Transfer_Fund) Transfer_Fund
Line149=       From
Line150=       (
Line151=      /*1.累計存款金額(當前的數據)*/
Line152=          Select Person_ID,Sum(Supply_Fund)[Supply_Fund],0 as ConSume_Fund,0 as Subsidy_Fund,0 as Take_Fund,0 as Transfer_Fund
Line153=          From ST_SupplyFund
Line154=          Where Convert(Char(16), Supply_Date, 20)  >=  @sBgnDate and
Line155=              Convert(Char(16), Supply_Date, 20) <=  @sEndDate
Line156=              and Person_ID in (Select Distinct Person_ID From #temp1)
Line157=          Group By Person_ID
Line158=          Union All
Line159=      /*累計存款金額(歷史的數據)*/
Line160=          Select Person_ID,Sum(Supply_Fund)[Supply_Fund],0,0,0,0
Line161=          From ST_SupplyFundHistory
Line162=          Where Convert(Char(16), Supply_Date, 20)  >=  @sBgnDate and
Line163=              Convert(Char(16), Supply_Date, 20) <=  @sEndDate
Line164=              and Person_ID in (Select Distinct Person_ID From #temp1)
Line165=          Group By Person_ID
Line166=          Union All
Line167=      /*2.累計消費金額(當前的數據)*/
Line168=          Select  Person_ID,0,Sum(ConSume_Fund)[ConSume_Fund],0,0,0
Line169=          From XF_ConSume
Line170=          Where Convert(Char(16), ConSume_Time, 20)  >=  @sBgnDate and
Line171=              Convert(Char(16), ConSume_Time, 20) <=  @sEndDate
Line172=              and Person_ID in (Select Distinct Person_ID From #temp1) 
Line173=          Group By Person_ID
Line174=          Union All
Line175=      /*累計消費金額(歷史的數據)*/
Line176=          Select Person_ID,0,Sum(ConSume_Fund)[ConSume_Fund],0,0,0
Line177=          From XF_ConSumeHistory
Line178=          Where Convert(Char(16), ConSume_Time, 20)  >=  @sBgnDate and
Line179=             Convert(Char(16), ConSume_Time, 20) <=  @sEndDate
Line180=              and Person_ID in (Select Distinct Person_ID From #temp1)
Line181=          Group By Person_ID
Line182=          Union All
Line183=      /*3.累計補貼金額(當前的數據)*/
Line184=          Select Person_ID,0,0,Sum(Subsidy_Fund)[Subsidy_Fund],0,0
Line185=          From ST_Subsidy
Line186=          Where Convert(Char(16), Subsidy_Date, 20)  >=  @sBgnDate and
Line187=             Convert(Char(16), Subsidy_Date, 20) <=  @sEndDate
Line188=              and Person_ID in (Select Distinct Person_ID From #temp1)
Line189=          Group By Person_ID
Line190=          Union All
Line191=      /*累計補貼金額(歷史的數據)*/
Line192=          Select Person_ID,0,0,Sum(Subsidy_Fund)[Subsidy_Fund],0,0
Line193=          From ST_SubsidyHistory
Line194=          Where Convert(Char(16), Subsidy_Date, 20)  >=  @sBgnDate and
Line195=              Convert(Char(16), Subsidy_Date, 20) <=  @sEndDate
Line196=              and Person_ID in (Select Distinct Person_ID From #temp1) 
Line197=          Group By Person_ID
Line198=          Union All
Line199=      /*4.累計取款金額(當前的數據)*/
Line200=          Select Person_ID,0,0,0,Sum(Take_Fund)[Take_Fund],0
Line201=          From ST_TakeFund
Line202=          Where Convert(Char(16), Take_Date, 20)  >=  @sBgnDate and
Line203=              Convert(Char(16), Take_Date, 20) <=  @sEndDate
Line204=              and Person_ID in (Select Distinct Person_ID From #temp1) 
Line205=          Group By Person_ID
Line206=          Union All
Line207=      /*累計取款金額(歷史的數據)*/
Line208=          Select Person_ID,0,0,0,Sum(Take_Fund)[Take_Fund],0
Line209=          From ST_TakeFundHistory
Line210=          Where Convert(Char(16), Take_Date, 20)  >=  @sBgnDate and
Line211=              Convert(Char(16), Take_Date, 20) <=  @sEndDate
Line212=              and Person_ID in (Select Distinct Person_ID From #temp1) 
Line213=          Group By Person_ID
Line214=          Union All
Line215=      /*5.累計轉帳金額(當前的數據)*/
Line216=          Select Person_ID,0,0,0,0,Sum(Transfer_Fund)[Transfer_Fund]
Line217=          From ST_TransferAccount
Line218=          Where Convert(Char(16), Transfer_Date, 20)  >=  @sBgnDate and
Line219=              Convert(Char(16), Transfer_Date, 20) <=  @sEndDate
Line220=              and Person_ID in (Select Distinct Person_ID From #temp1)
Line221=          Group By Person_ID
Line222=          Union All
Line223=      /*累計轉帳金額(歷史的數據)*/
Line224=          Select Person_ID,0,0,0,0,Sum(Transfer_Fund)[Transfer_Fund]
Line225=          From ST_TransferAccountHistory
Line226=          Where Convert(Char(16), Transfer_Date, 20)  >=  @sBgnDate and
Line227=              Convert(Char(16), Transfer_Date, 20) <=  @sEndDate
Line228=              and Person_ID in (Select Distinct Person_ID From #temp1)
Line229=          Group By Person_ID
Line230=    ) ss  Group By Person_ID) I  On A.Person_ID=I.Person_ID
Line231=    Where B.Dept_No LIKE @DeptNo+'%' AND B.Dept_ID IN (Select Dept_ID From ST_DeptRight Where Check_State=0 AND User_No=@UserNo)
Line232=    Order By B.Dept_No,A.Person_No
Line233=End
Line234=Drop Table #temp1
[Step154]
Line1=--系統平衡表 
Line2=IF OBJECT_ID('st_pd_SysBalanceRep') IS NOT NULL DROP PROCEDURE st_pd_SysBalanceRep
[Step155]
Line1=/*******************************************/
Line2=/* 類型：報表                              */
Line3=/* 模塊來源：核心平台                      */
Line4=/* 報表名稱：系統平衡表                    */
Line5=/*                                         */
Line6=/*******************************************/
Line7=CREATE PROCEDURE dbo.st_pd_SysBalanceRep
Line8=AS
Line9=    Declare @CardCharge Money, @CardForegift Money  --開戶卡費用、開戶卡押金
Line10=    Declare @SupplyFund Money                      --總存款金額
Line11=    Declare @SubsidyFund Money                     --總補貼金額
Line12=    Declare @AlterFee Money                        --總補卡費用
Line13=    Declare @TakeFund Money                        --總取款金額
Line14=    Declare @BackFund Money, @BackForegift Money   --銷卡退金額、銷卡退押金
Line15=    Declare @TransferFund Money                    --小金額轉賬總金額
Line16=    Declare @CardBalance Money                     --總賬戶余額
Line17=    Declare @Fund Money                            --總消費金額
Line18=    Declare @Money Money                           --平衡差額
Line19=/*1.取得開戶卡費用、開戶卡押金、總賬戶余額*/
Line20=Select @CardCharge=Round(IsNull(Sum(Card_Charge),0), 2), @CardForegift=Round(IsNull(Sum(Card_Foregift),0), 2),
Line21=    @CardBalance=Round(IsNull(Sum(Card_Balance),0), 2)
Line22=From dbo.ST_Card
Line23=Select @CardCharge=@CardCharge+Round(IsNull(Sum(Card_Charge), 0), 2),
Line24=    @CardForegift=@CardForegift+Round(IsNull(Sum(Back_Foregift),0), 2)
Line25=From dbo.ST_DeleteCard
Line26=Select @CardCharge=@CardCharge+Round(IsNull(Sum(Card_Charge), 0), 2),
Line27=    @CardForegift=@CardForegift+Round(IsNull(Sum(Back_Foregift),0), 2)
Line28=From dbo.ST_PersonAccount
Line29=/*2.總存款金額*/
Line30=Select @SupplyFund=Round(IsNull(Sum(Supply_Fund),0), 2) From dbo.ST_SupplyFund
Line31=Select @SupplyFund=@SupplyFund+Round(IsNull(Sum(Supply_Fund),0), 2) From dbo.ST_PersonAccount
Line32=/*3.總補貼金額*/
Line33=Select @SubsidyFund=Round(IsNull(Sum(Subsidy_Fund),0), 2) From dbo.ST_Subsidy
Line34=Select @SubsidyFund=@SubsidyFund+Round(IsNull(Sum(Subsidy_Fund),0), 2) From dbo.ST_PersonAccount
Line35=/*4.總補卡費用*/
Line36=Select @AlterFee=Round(IsNull(Sum(Alter_Fee),0), 2) From dbo.ST_AlterCard
Line37=Select @AlterFee=@AlterFee+Round(IsNull(Sum(AlterCard_Fund),0), 2) From dbo.ST_PersonAccount
Line38=/*5.總取款金額*/
Line39=Select @TakeFund=Round(IsNull(Sum(Take_Fund),0), 2) From dbo.ST_TakeFund
Line40=Select @TakeFund=@TakeFund+Round(IsNull(Sum(Take_Fund),0), 2) From dbo.ST_PersonAccount
Line41=/*6.銷卡退金額、銷卡退押金*/
Line42=Select @BackFund=Round(IsNull(Sum(Back_Fund),0), 2), @BackForegift=Round(IsNull(Sum(Back_Foregift),0), 2)
Line43=From dbo.ST_DeleteCard
Line44=Select @BackFund=@BackFund+Round(IsNull(Sum(Back_Fund),0), 2), @BackForegift=@BackForegift+Round(IsNull(Sum(Back_Foregift),0), 2)
Line45=From dbo.ST_PersonAccount
Line46=/*7.小金額轉賬總金額*/
Line47=Select @TransferFund=Round(IsNull(Sum(Transfer_Fund),0), 2) From dbo.ST_TransferAccount
Line48=Select @TransferFund=@TransferFund+Round(IsNull(Sum(Transfer_Fund),0), 2) From dbo.ST_PersonAccount
Line49=/*8.總消費金額*/
Line50=Select @Fund=Round(IsNull(Sum(ConSume_Fund),0), 2) From dbo.XF_ConSume
Line51=Select @Fund=@Fund+Round(IsNull(Sum(EM_ConSumeFund),0), 2) From dbo.ST_PersonAccount
Line52=Select @Fund=@Fund+Round(IsNull(Sum(XF_ConSumeFund),0), 2) From dbo.ST_PersonAccount
Line53=/*9.平衡差額*/
Line54=SET @Money=Round((@SupplyFund+@SubsidyFund)-(@Fund+@CardBalance+@TakeFund+@BackFund+@TransferFund), 2)
Line55=Select GetDate()  as [Balance_Time], @CardCharge as [Card_Charge], @CardForegift as [Card_Foregift], @SupplyFund as [Supply_AllFund], @SubsidyFund as [Subsidy_AllFund],
Line56=@AlterFee as [Alter_Fee], @TakeFund as [Take_AllFund], @BackFund as [Back_Fund], @BackForegift as [Back_Foregift], @TransferFund as [Transfer_AllFund],
Line57=@Money as [System_Balance], @CardBalance as [Card_AllBalance],@Fund as [ConSume_AllFund],@CardForegift-@BackForegift as [Remain_AllForegift]
[Step156]
Line1=--綜合報表
Line2=IF OBJECT_ID('st_pd_PersonalRepAll') IS NOT NULL DROP PROCEDURE st_pd_PersonalRepAll
[Step157]
Line1=/*******************************************/
Line2=/* 類型：報表                              */
Line3=/* 模塊來源：核心平台                      */
Line4=/* 報表名稱：個人綜合報表                  */
Line5=/*                                         */
Line6=/*******************************************/
Line7=CREATE  PROCEDURE st_pd_PersonalRepAll
Line8=@DeptNo  VarChar(40),    --部門編號
Line9=@UserNo  VarChar(40),    --用戶編號
Line10=@DateBgn DateTime,       --統計起始日期
Line11=@DateEnd DateTime,        --統計終止日期
Line12=@IsDel   Bit             --是否顯示注銷人員
Line13=AS
Line14=Declare @sBgnDate VarChar(16), @sEndDate VarChar(16)
Line15=Set @sBgnDate = '' +  Convert(Char(16), @DateBgn, 20)  + ''
Line16=Set @sEndDate = '' +  Convert(Char(16), @DateEnd, 20)  + ''
Line17=--是否顯示注銷人員
Line18=IF @IsDel=0
Line19=Begin
Line20=    /*查詢相關信息*/
Line21=    Select C.Person_No,C.Person_Name,B.Dept_No,B.Dept_Name,Card_Balance,C.Card_No,
Line22=        IsNull(Supply_Fund,0) as [Supply_Fund],IsNull(ConSume_Fund,0) as [ConSume_Fund],
Line23=        IsNull(D.Subsidy_Fund,0) as [Subsidy_Fund],IsNull(Take_Fund,0) as [Take_Fund],
Line24=        IsNull(Transfer_Fund,0) as [Transfer_Fund],
Line25=        IsNull(Supply_Fund,0)+IsNull(D.Subsidy_Fund,0)-(IsNull(ConSume_Fund,0)+IsNull(Take_Fund,0)+IsNull(Transfer_Fund,0)+Card_Balance)[Mny_Balance]
Line26=      From ST_Card A
Line27=      Left Join ST_Person C ON C.Person_ID=A.Person_ID
Line28=      Left Join ST_Department B ON B.Dept_ID=C.Dept_ID
Line29=      Left Join (
Line30=      /*統計發生金額*/
Line31=       Select Person_ID,Sum(Supply_Fund) Supply_Fund,Sum(ConSume_Fund) ConSume_Fund,Sum(Subsidy_Fund) Subsidy_Fund,Sum(Take_Fund) Take_Fund,Sum(Transfer_Fund) Transfer_Fund
Line32=       From
Line33=       (
Line34=      /*1.累計存款金額(當前的數據)*/                                                                                   
Line35=          Select Person_ID,Sum(Supply_Fund) as Supply_Fund/*存款金額*/,0 as ConSume_Fund/*消費金額*/ ,0 as Subsidy_Fund  /*補貼金額*/,0 as Take_Fund/*取款金額*/  ,0 as Transfer_Fund /*轉帳金額*/ 
Line36=          From ST_SupplyFund
Line37=          Where Convert(Char(16), Supply_Date, 20)  >=  @sBgnDate and
Line38=              Convert(Char(16), Supply_Date, 20) <=  @sEndDate
Line39=          Group By Person_ID
Line40=          Union All
Line41=      /*累計存款金額(歷史的數據)*/
Line42=          Select Person_ID,Sum(Supply_Fund) as Supply_Fund,0,0,0,0
Line43=          From ST_SupplyFundHistory
Line44=          Where Convert(Char(16), Supply_Date, 20)  >=  @sBgnDate and
Line45=              Convert(Char(16), Supply_Date, 20) <=  @sEndDate
Line46=              Group By Person_ID
Line47=          Union All
Line48=      /*2.累計消費金額(當前的數據)*/
Line49=          Select Person_ID,0,Sum(ConSume_Fund)[ConSume_Fund],0,0,0
Line50=          From XF_ConSume
Line51=          Where Convert(Char(16), ConSume_Time, 20)  >=  @sBgnDate and
Line52=              Convert(Char(16), ConSume_Time, 20) <=  @sEndDate
Line53=          Group By Person_ID
Line54=          Union All
Line55=      /*累計消費金額(歷史的數據)*/
Line56=          Select Person_ID,0,Sum(ConSume_Fund)[ConSume_Fund],0,0,0
Line57=          From XF_ConSumeHistory
Line58=          Where Convert(Char(16), ConSume_Time, 20)  >=  @sBgnDate and
Line59=              Convert(Char(16), ConSume_Time, 20) <=  @sEndDate
Line60=              Group By Person_ID
Line61=          Union All
Line62=      /*3.累計補貼金額(當前的數據)*/
Line63=          Select Person_ID,0,0,Sum(Subsidy_Fund)[Subsidy_Fund],0,0
Line64=          From ST_Subsidy
Line65=          Where Convert(Char(16), Subsidy_Date, 20)  >=  @sBgnDate and
Line66=              Convert(Char(16), Subsidy_Date, 20) <=  @sEndDate
Line67=              Group By Person_ID
Line68=          Union All
Line69=      /*累計補貼金額(歷史的數據)*/
Line70=          Select Person_ID,0,0,Sum(Subsidy_Fund)[Subsidy_Fund],0,0
Line71=          From ST_SubsidyHistory
Line72=          Where Convert(Char(16), Subsidy_Date, 20)  >=  @sBgnDate and
Line73=              Convert(Char(16), Subsidy_Date, 20) <=  @sEndDate
Line74=              Group By Person_ID
Line75=          Union All
Line76=      /*4.累計取款金額(當前的數據)*/
Line77=          Select Person_ID,0,0,0,Sum(Take_Fund)[Take_Fund],0
Line78=          From ST_TakeFund
Line79=          Where Convert(Char(16), Take_Date, 20)  >=  @sBgnDate and
Line80=             Convert(Char(16), Take_Date, 20) <=  @sEndDate
Line81=             Group By Person_ID
Line82=          Union All
Line83=      /*累計取款金額(歷史的數據)*/
Line84=          Select Person_ID,0,0,0,Sum(Take_Fund)[Take_Fund],0
Line85=          From ST_TakeFundHistory
Line86=          Where Convert(Char(16), Take_Date, 20)  >=  @sBgnDate and
Line87=             Convert(Char(16), Take_Date, 20) <=  @sEndDate
Line88=             Group By Person_ID
Line89=          Union All
Line90=      /*5.累計轉帳金額(當前的數據)*/
Line91=          Select Person_ID,0,0,0,0,Sum(Transfer_Fund)[Transfer_Fund]
Line92=          From ST_TransferAccount
Line93=          Where Convert(Char(16), Transfer_Date, 20)  >=  @sBgnDate and
Line94=              Convert(Char(16), Transfer_Date, 20) <=  @sEndDate
Line95=              Group By Person_ID
Line96=          Union All
Line97=      /*累計轉帳金額(歷史的數據)*/
Line98=          Select Person_ID,0,0,0,0,Sum(Transfer_Fund)[Transfer_Fund]
Line99=          From ST_TransferAccountHistory
Line100=          Where Convert(Char(16), Transfer_Date, 20)  >=  @sBgnDate and
Line101=              Convert(Char(16), Transfer_Date, 20) <=  @sEndDate
Line102=              Group By Person_ID
Line103=    ) ss  Group By Person_ID) D  on A.Person_ID=D.Person_ID
Line104=    /*過慮數據*/
Line105=    Where B.Dept_No LIKE @DeptNo+'%' AND B.Dept_ID IN (Select Dept_ID From ST_DeptRight Where Check_State=0 AND User_No=@UserNo)
Line106=        Order By B.Dept_No,C.Person_No  
Line107=END
Line108=ELSE
Line109=Begin
Line110=      /*統計發生業務的人員
Line111=    Insert  Into  #temp1(Person_ID)
Line112=        Select C.Person_ID
Line113=        From ST_Card A
Line114=        Left Join ST_Person C ON C.Person_ID=A.Person_ID
Line115=        Left Join ST_Department B ON B.Dept_ID=C.Dept_ID
Line116=        Where B.Dept_No LIKE @DeptNo+'%' AND B.Dept_ID IN (Select Dept_ID From ST_DeptRight Where Check_State=0 AND User_No=@UserNo)*/
Line117=    /*查詢相關信息*/
Line118=    Select A.Person_No,A.Person_Name,B.Dept_No,B.Dept_Name,IsNull(Card_Balance,0)[Card_Balance],Card_No,A.IS_DEL,
Line119=        IsNull(Supply_Fund,0)[Supply_Fund],IsNull(ConSume_Fund,0)[ConSume_Fund],
Line120=        IsNull(I.Subsidy_Fund,0)[Subsidy_Fund],IsNull(Take_Fund,0)[Take_Fund],
Line121=        IsNull(Transfer_Fund,0)[Transfer_Fund],
Line122=        IsNull(Supply_Fund,0)+IsNull(I.Subsidy_Fund,0)-(IsNull(ConSume_Fund,0)+IsNull(Take_Fund,0)+IsNull(Transfer_Fund,0)+Card_Balance)[Mny_Balance]
Line123=      From ST_Person A
Line124=      Left Join ST_Card C ON  A.Person_ID=C.Person_ID
Line125=      Left Join ST_Department B ON A.Dept_ID=B.Dept_ID
Line126=      Left Join (
Line127=      /*統計發生金額*/
Line128=       Select Person_ID,Sum(Supply_Fund) Supply_Fund,Sum(ConSume_Fund) ConSume_Fund,Sum(Subsidy_Fund) Subsidy_Fund,Sum(Take_Fund) Take_Fund,Sum(Transfer_Fund) Transfer_Fund
Line129=       From
Line130=       (
Line131=      /*1.累計存款金額(當前的數據)*/
Line132=          Select Person_ID,Sum(Supply_Fund)[Supply_Fund],0 as ConSume_Fund,0 as Subsidy_Fund,0 as Take_Fund,0 as Transfer_Fund
Line133=          From ST_SupplyFund
Line134=          Where Convert(Char(16), Supply_Date, 20)  >=  @sBgnDate and
Line135=              Convert(Char(16), Supply_Date, 20) <=  @sEndDate
Line136=          Group By Person_ID
Line137=          Union All
Line138=      /*累計存款金額(歷史的數據)*/
Line139=          Select Person_ID,Sum(Supply_Fund)[Supply_Fund],0,0,0,0
Line140=          From ST_SupplyFundHistory
Line141=          Where Convert(Char(16), Supply_Date, 20)  >=  @sBgnDate and
Line142=              Convert(Char(16), Supply_Date, 20) <=  @sEndDate
Line143=          Group By Person_ID
Line144=          Union All
Line145=      /*2.累計消費金額(當前的數據)*/
Line146=          Select  Person_ID,0,Sum(ConSume_Fund)[ConSume_Fund],0,0,0
Line147=          From XF_ConSume
Line148=          Where Convert(Char(16), ConSume_Time, 20)  >=  @sBgnDate and
Line149=              Convert(Char(16), ConSume_Time, 20) <=  @sEndDate
Line150=          Group By Person_ID
Line151=          Union All
Line152=      /*累計消費金額(歷史的數據)*/
Line153=          Select Person_ID,0,Sum(ConSume_Fund)[ConSume_Fund],0,0,0
Line154=          From XF_ConSumeHistory
Line155=          Where Convert(Char(16), ConSume_Time, 20)  >=  @sBgnDate and
Line156=             Convert(Char(16), ConSume_Time, 20) <=  @sEndDate
Line157=          Group By Person_ID
Line158=          Union All
Line159=      /*3.累計補貼金額(當前的數據)*/
Line160=          Select Person_ID,0,0,Sum(Subsidy_Fund)[Subsidy_Fund],0,0
Line161=          From ST_Subsidy
Line162=          Where Convert(Char(16), Subsidy_Date, 20)  >=  @sBgnDate and
Line163=             Convert(Char(16), Subsidy_Date, 20) <=  @sEndDate
Line164=          Group By Person_ID
Line165=          Union All
Line166=      /*累計補貼金額(歷史的數據)*/
Line167=          Select Person_ID,0,0,Sum(Subsidy_Fund)[Subsidy_Fund],0,0
Line168=          From ST_SubsidyHistory
Line169=          Where Convert(Char(16), Subsidy_Date, 20)  >=  @sBgnDate and
Line170=              Convert(Char(16), Subsidy_Date, 20) <=  @sEndDate
Line171=          Group By Person_ID
Line172=          Union All
Line173=      /*4.累計取款金額(當前的數據)*/
Line174=          Select Person_ID,0,0,0,Sum(Take_Fund)[Take_Fund],0
Line175=          From ST_TakeFund
Line176=          Where Convert(Char(16), Take_Date, 20)  >=  @sBgnDate and
Line177=              Convert(Char(16), Take_Date, 20) <=  @sEndDate
Line178=          Group By Person_ID
Line179=          Union All
Line180=      /*累計取款金額(歷史的數據)*/
Line181=          Select Person_ID,0,0,0,Sum(Take_Fund)[Take_Fund],0
Line182=          From ST_TakeFundHistory
Line183=          Where Convert(Char(16), Take_Date, 20)  >=  @sBgnDate and
Line184=              Convert(Char(16), Take_Date, 20) <=  @sEndDate
Line185=          Group By Person_ID
Line186=          Union All
Line187=      /*5.累計轉帳金額(當前的數據)*/
Line188=          Select Person_ID,0,0,0,0,Sum(Transfer_Fund)[Transfer_Fund]
Line189=          From ST_TransferAccount
Line190=          Where Convert(Char(16), Transfer_Date, 20)  >=  @sBgnDate and
Line191=              Convert(Char(16), Transfer_Date, 20) <=  @sEndDate
Line192=          Group By Person_ID
Line193=          Union All
Line194=      /*累計轉帳金額(歷史的數據)*/
Line195=          Select Person_ID,0,0,0,0,Sum(Transfer_Fund)[Transfer_Fund]
Line196=          From ST_TransferAccountHistory
Line197=          Where Convert(Char(16), Transfer_Date, 20)  >=  @sBgnDate and
Line198=              Convert(Char(16), Transfer_Date, 20) <=  @sEndDate
Line199=          Group By Person_ID
Line200=    ) ss  Group By Person_ID) I  On A.Person_ID=I.Person_ID
Line201=    Where B.Dept_No LIKE @DeptNo+'%' AND B.Dept_ID IN (Select Dept_ID From ST_DeptRight Where Check_State=0 AND User_No=@UserNo)
Line202=    Order By B.Dept_No,A.Person_No
Line203=End
[Step158]
Line1=IF OBJECT_ID('st_pd_AccountRep') IS NOT NULL DROP PROCEDURE st_pd_AccountRep
[Step159]
Line1=CREATE PROCEDURE dbo.st_pd_AccountRep
Line2=   @UserNo VARCHAR(8000),   --出納員編號
Line3=   @UserName VARCHAR(8000), --出納員名稱
Line4=   @DateBgn DATETIME,       --統計起始日期
Line5=   @DateEnd DATETIME        --統計終止日期
Line6=AS
Line7=-- DECLARE @UserNo VARCHAR(8000),@UserName VARCHAR(8000),@DateBgn DATETIME,@DateEnd DATETIME
Line8=-- SELECT @UserNo='AllUsers',@UserName='所有出納員', @DateBgn='2005-01-01',@DateEnd=GETDATE()
Line9=   DECLARE @sBgnDate VARCHAR(16), @sEndDate VARCHAR(16)
Line10=   SET @sBgnDate = '' +  CONVERT(CHAR(16), @DateBgn, 20)  + ''
Line11=   SET @sEndDate = '' + CONVERT(CHAR(16), @DateEnd, 20)  + ''
Line12=   DECLARE @CardCount INT, @CardCharge MONEY, @CardForegif MONEY                  --取得開戶次數、開戶卡費用、開戶卡押金
Line13=   DECLARE @SupplyCount INT, @SupplyFund MONEY                                    --存款次數、總存款金額
Line14=   DECLARE @SubsidyCount INT, @SubsidyFund MONEY                                  --補貼次數、總補貼金額
Line15=   DECLARE @AlterCount INT, @AlterFee MONEY                                       --補卡次數、總補卡費用
Line16=   DECLARE @ChangeCount INT                                                       --換卡次數
Line17=   DECLARE @TakeCount INT, @TakeFund MONEY                                        --取款次數、總取款金額
Line18=   DECLARE @BackCount INT, @BackFund MONEY, @BackForegift MONEY,@SmallFund MONEY  --銷卡次數、銷卡退主帳戶金額、銷卡退押金、銷卡退小帳戶金額
Line19=   DECLARE @TransferCount INT, @TransferFund MONEY                                --小金額轉賬次數、小金額轉賬總金額
Line20=   DECLARE @Count INT                                                             --總計次數
Line21=   DECLARE @Money MONEY                                                           --應繳金額
Line22=   DECLARE @SUserNo VARCHAR(20),@Pos INT
Line23=   --v3.1 peihb add 2008-11-03
Line24=   DECLARE @AllConsumeMoney MONEY, @AllConsumeCount INT				--總消費金額，總消費次數	
Line25=  -- SET @DateEnd=DATEADD(second, -1, @DateEnd + 1)
Line26=   IF @UserNo='AllUsers'
Line27=   BEGIN
Line28=      --1.取得開戶次數、開戶卡費用、開戶卡押金(加上歷史記錄)
Line29=      SELECT @CardCount=COUNT(*), @CardCharge=ROUND(ISNULL(SUM(Card_Charge),0), 2), @CardForegif=ROUND(ISNULL(SUM(Card_Foregift),0), 2)
Line30=        FROM dbo.ST_Card WHERE CONVERT(CHAR(16), Card_NewDate, 20) >=  @sBgnDate AND CONVERT (CHAR(16), Card_NewDate, 20) <= @sEndDate
Line31=      SELECT @CardCount=@CardCount+COUNT(*), @CardCharge=@CardCharge+ROUND(ISNULL(SUM(Card_Charge),0), 2),
Line32=             @CardForegif=@CardForegif+ROUND(ISNULL(SUM(Back_Foregift),0), 2)
Line33=        FROM dbo.ST_DeleteCard WHERE  CONVERT(CHAR(16), New_Date, 20) >=  @sBgnDate AND CONVERT (CHAR(16), New_Date, 20) <= @sEndDate
Line34=      SELECT @CardCount=@CardCount+COUNT(*), @CardCharge=@CardCharge+ROUND(ISNULL(SUM(Card_Charge),0), 2),
Line35=             @CardForegif=@CardForegif+ROUND(ISNULL(SUM(Back_Foregift),0), 2)
Line36=        FROM dbo.ST_DeleteCardHistory WHERE CONVERT(CHAR(16), New_Date, 20) >=  @sBgnDate AND CONVERT (CHAR(16), New_Date, 20) <= @sEndDate
Line37=      --2.存款次數、總存款金額(加上歷史記錄)
Line38=      SELECT @SupplyCount=COUNT(*), @SupplyFund=ROUND(ISNULL(SUM(Supply_Fund),0), 2)
Line39=        FROM dbo.ST_SupplyFund WHERE  CONVERT(CHAR(16), Supply_Date, 20) >=  @sBgnDate AND CONVERT (CHAR(16), Supply_Date, 20) <= @sEndDate
Line40=      SELECT @SupplyCount=@SupplyCount + COUNT(*), @SupplyFund=@SupplyFund + ROUND(ISNULL(SUM(Supply_Fund),0), 2)
Line41=        FROM dbo.ST_SupplyFundHistory WHERE  CONVERT(CHAR(16), Supply_Date, 20) >=  @sBgnDate AND CONVERT (CHAR(16), Supply_Date, 20) <= @sEndDate
Line42=      --3.補貼次數、總補貼金額(加上歷史記錄)
Line43=      SELECT @SubsidyCount=COUNT(*), @SubsidyFund=ROUND(ISNULL(SUM(Subsidy_Fund),0), 2)
Line44=        FROM dbo.ST_Subsidy WHERE CONVERT(CHAR(16), Subsidy_Date, 20) >=  @sBgnDate AND CONVERT (CHAR(16), Subsidy_Date, 20) <= @sEndDate
Line45=      SELECT @SubsidyCount=@SubsidyCount + COUNT(*), @SubsidyFund=@SubsidyFund + ROUND(ISNULL(SUM(Subsidy_Fund),0), 2)
Line46=        FROM dbo.ST_SubsidyHistory WHERE CONVERT(CHAR(16), Subsidy_Date, 20) >=  @sBgnDate AND CONVERT (CHAR(16), Subsidy_Date, 20) <= @sEndDate
Line47=      --4.補卡次數、總補卡費用(加上歷史記錄)
Line48=      SELECT @AlterCount=COUNT(*), @AlterFee=ROUND(ISNULL(SUM(Alter_Fee),0), 2)
Line49=        FROM dbo.ST_AlterCard WHERE Alter_Type=1 AND  CONVERT(CHAR(16), Alter_Date, 20) >=  @sBgnDate AND CONVERT (CHAR(16), Alter_Date, 20) <= @sEndDate
Line50=      SELECT @AlterCount=@AlterCount + COUNT(*), @AlterFee=@AlterFee + ROUND(ISNULL(SUM(Alter_Fee),0), 2)
Line51=        FROM dbo.ST_AlterCardHistory WHERE Alter_Type=1 AND  CONVERT(CHAR(16), Alter_Date, 20) >=  @sBgnDate AND CONVERT (CHAR(16), Alter_Date, 20) <= @sEndDate
Line52=      --4.1.換卡次數(加上歷史記錄)
Line53=      SELECT @ChangeCount=COUNT(*)
Line54=        FROM dbo.ST_AlterCard WHERE Alter_Type=0 AND  CONVERT(CHAR(16), Alter_Date, 20) >=  @sBgnDate AND CONVERT (CHAR(16), Alter_Date, 20) <= @sEndDate
Line55=      SELECT @ChangeCount=@ChangeCount + COUNT(*)
Line56=        FROM dbo.ST_AlterCardHistory WHERE Alter_Type=0 AND  CONVERT(CHAR(16), Alter_Date, 20) >=  @sBgnDate AND CONVERT (CHAR(16), Alter_Date, 20) <= @sEndDate
Line57=      --5.取款次數、總取款金額(加上歷史記錄)
Line58=      SELECT @TakeCount=COUNT(*), @TakeFund=ROUND(ISNULL(SUM(Take_Fund),0), 2)
Line59=        FROM dbo.ST_TakeFund WHERE  CONVERT(CHAR(16), Take_Date, 20) >=  @sBgnDate AND CONVERT (CHAR(16), Take_Date, 20) <= @sEndDate
Line60=      SELECT @TakeCount=@TakeCount + COUNT(*), @TakeFund=@TakeFund + ROUND(ISNULL(SUM(Take_Fund),0), 2)
Line61=        FROM dbo.ST_TakeFundHistory WHERE   CONVERT(CHAR(16), Take_Date, 20) >=  @sBgnDate AND CONVERT (CHAR(16), Take_Date, 20) <= @sEndDate
Line62=      --6.銷卡次數、銷卡退金額、銷卡退押金(加上歷史記錄)
Line63=      SELECT @BackCount=COUNT(*), @BackFund=ROUND(ISNULL(SUM(Back_Fund),0), 2),@SmallFund=ROUND(ISNULL(SUM(Back_SmallFund),0), 2), 
Line64=             @BackForegift=ROUND(ISNULL(SUM(Back_Foregift),0), 2)
Line65=        FROM dbo.ST_DeleteCard WHERE  CONVERT(CHAR(16), Del_Date, 20) >=  @sBgnDate AND CONVERT (CHAR(16), Del_Date, 20) <= @sEndDate
Line66=      SELECT @BackCount=@BackCount + COUNT(*), @BackFund=@BackFund + ROUND(ISNULL(SUM(Back_Fund),0), 2),@SmallFund=@SmallFund + ROUND(ISNULL(SUM(Back_SmallFund),0), 2), 
Line67=             @BackForegift=@BackForegift + ROUND(ISNULL(SUM(Back_Foregift),0), 2)
Line68=        FROM dbo.ST_DeleteCardHistory WHERE  CONVERT(CHAR(16), Del_Date, 20) >=  @sBgnDate AND CONVERT (CHAR(16), Del_Date, 20) <= @sEndDate
Line69=      --7.小金額轉賬次數、小金額轉賬總金額(加上歷史記錄)
Line70=      SELECT @TransferCount=COUNT(*), @TransferFund=ROUND(ISNULL(SUM(Transfer_Fund),0), 2)
Line71=        FROM dbo.ST_TransferAccount WHERE CONVERT(CHAR(16), Transfer_Date, 20) >=  @sBgnDate AND CONVERT (CHAR(16), Transfer_Date, 20) <= @sEndDate
Line72=      SELECT @TransferCount=@TransferCount + COUNT(*), @TransferFund=@TransferFund + ROUND(ISNULL(SUM(Transfer_Fund),0), 2)
Line73=        FROM dbo.ST_TransferAccountHistory WHERE  CONVERT(CHAR(16), Transfer_Date, 20) >=  @sBgnDate AND CONVERT (CHAR(16), Transfer_Date, 20) <= @sEndDate
Line74=   END
Line75=   ELSE --不是所有的出納員
Line76=   BEGIN
Line77=      --0.取得選中的出納員
Line78=      IF OBJECT_ID('tempdb..#SelectPerson') IS NOT NULL DROP TABLE #SelectPerson
Line79=      SELECT User_No INTO #SelectPerson FROM ST_UserInfo WHERE 1=0
Line80=      WHILE (@UserNo <> '' )
Line81=      BEGIN
Line82=         SET @Pos = CHARINDEX(',', @UserNo)
Line83=         IF @Pos = 0
Line84=         BEGIN
Line85=            SET @SUserNo=@UserNo
Line86=            SET @UserNo=''
Line87=         END
Line88=         ELSE
Line89=         BEGIN
Line90=            SET @SUserNo=SUBSTRING(@UserNo, 1, @Pos - 1)
Line91=            SET @UserNo=SUBSTRING(@UserNo,@Pos + 1, 8000)
Line92=         END
Line93=         INSERT INTO #SelectPerson(User_No) VALUES(@SUserNo)
Line94=      END
Line95=      --1.取得開戶次數、開戶卡費用、開戶卡押金(加上歷史記錄)
Line96=      SELECT @CardCount=COUNT(*), @CardCharge=ROUND(ISNULL(SUM(Card_Charge),0), 2), @CardForegif=ROUND(ISNULL(SUM(Card_Foregift),0), 2)
Line97=        FROM dbo.ST_Card  WHERE CONVERT(CHAR(16), Card_NewDate, 20) >=  @sBgnDate AND CONVERT (CHAR(16), Card_NewDate, 20) <= @sEndDate AND User_No IN (SELECT User_No FROM #SelectPerson)
Line98=      SELECT @CardCount=@CardCount+COUNT(*), @CardCharge=@CardCharge+ROUND(ISNULL(SUM(Card_Charge),0), 2),
Line99=             @CardForegif=@CardForegif+ROUND(ISNULL(SUM(Back_Foregift),0), 2)
Line100=        FROM dbo.ST_DeleteCard WHERE CONVERT(CHAR(16), New_Date, 20) >=  @sBgnDate AND CONVERT (CHAR(16), New_Date, 20) <= @sEndDate
Line101=              AND New_User_No IN (SELECT User_No FROM #SelectPerson)
Line102=        SELECT @CardCount=@CardCount+COUNT(*), @CardCharge=@CardCharge+ROUND(ISNULL(SUM(Card_Charge),0), 2),
Line103=             @CardForegif=@CardForegif+ROUND(ISNULL(SUM(Back_Foregift),0), 2)
Line104=        FROM dbo.ST_DeleteCardHistory  WHERE CONVERT(CHAR(16), New_Date, 20) >=  @sBgnDate AND CONVERT (CHAR(16), New_Date, 20) <= @sEndDate
Line105=             AND New_User_No IN (SELECT User_No FROM #SelectPerson)
Line106=      --2.存款次數、總存款金額(加上歷史記錄)
Line107=      SELECT @SupplyCount=COUNT(*), @SupplyFund=ROUND(ISNULL(SUM(Supply_Fund),0), 2)
Line108=        FROM dbo.ST_SupplyFund WHERE CONVERT(CHAR(16), Supply_Date, 20) >=  @sBgnDate AND CONVERT (CHAR(16), Supply_Date, 20) <= @sEndDate
Line109=             AND User_No IN (SELECT User_No FROM #SelectPerson)  
Line110=      SELECT @SupplyCount=@SupplyCount + COUNT(*), @SupplyFund=@SupplyFund + ROUND(ISNULL(SUM(Supply_Fund),0), 2)
Line111=        FROM dbo.ST_SupplyFundHistory WHERE CONVERT(CHAR(16), Supply_Date, 20) >=  @sBgnDate AND CONVERT (CHAR(16), Supply_Date, 20) <= @sEndDate
Line112=             AND User_No IN (SELECT User_No FROM #SelectPerson)  
Line113=      --3.補貼次數、總補貼金額(加上歷史記錄)
Line114=      SELECT @SubsidyCount=COUNT(*), @SubsidyFund=ROUND(ISNULL(SUM(Subsidy_Fund),0), 2)
Line115=        FROM dbo.ST_Subsidy WHERE CONVERT(CHAR(16), Subsidy_Date, 20) >=  @sBgnDate AND CONVERT (CHAR(16), Subsidy_Date, 20) <= @sEndDate
Line116=              AND User_No IN (SELECT User_No FROM #SelectPerson)
Line117=      SELECT @SubsidyCount=@SubsidyCount + COUNT(*), @SubsidyFund=@SubsidyFund + ROUND(ISNULL(SUM(Subsidy_Fund),0), 2)
Line118=        FROM dbo.ST_SubsidyHistory WHERE  CONVERT(CHAR(16), Subsidy_Date, 20) >=  @sBgnDate AND CONVERT (CHAR(16), Subsidy_Date, 20) <= @sEndDate
Line119=             AND User_No IN (SELECT User_No FROM #SelectPerson)
Line120=      --4.補卡次數、總補卡費用(加上歷史記錄)
Line121=      SELECT @AlterCount=COUNT(*), @AlterFee=ROUND(ISNULL(SUM(Alter_Fee),0), 2)
Line122=        FROM dbo.ST_AlterCard WHERE Alter_Type=1 AND  CONVERT(CHAR(16), Alter_Date, 20) >=  @sBgnDate AND CONVERT (CHAR(16), Alter_Date, 20) <= @sEndDate
Line123=             AND User_No IN (SELECT User_No FROM #SelectPerson)
Line124=      SELECT @AlterCount=@AlterCount + COUNT(*), @AlterFee=@AlterFee + ROUND(ISNULL(SUM(Alter_Fee),0), 2)
Line125=        FROM dbo.ST_AlterCardHistory  WHERE Alter_Type=1 AND 
Line126=             CONVERT(CHAR(16), Alter_Date, 20) >=  @sBgnDate AND CONVERT (CHAR(16), Alter_Date, 20) <= @sEndDate
Line127=             AND User_No IN (SELECT User_No FROM #SelectPerson)
Line128=      --4.1.換卡次數(加上歷史記錄)
Line129=      SELECT @ChangeCount=COUNT(*)
Line130=        FROM dbo.ST_AlterCard WHERE Alter_Type=0 AND  CONVERT(CHAR(16), Alter_Date, 20) >=  @sBgnDate AND CONVERT (CHAR(16), Alter_Date, 20) <= @sEndDate 
Line131=        AND User_No IN (SELECT User_No FROM #SelectPerson)
Line132=      SELECT @ChangeCount=@ChangeCount + COUNT(*)
Line133=        FROM dbo.ST_AlterCardHistory WHERE Alter_Type=0 AND  CONVERT(CHAR(16), Alter_Date, 20) >=  @sBgnDate AND CONVERT (CHAR(16), Alter_Date, 20) <= @sEndDate 
Line134=        AND User_No IN (SELECT User_No FROM #SelectPerson)
Line135=      --5.取款次數、總取款金額(加上歷史記錄)
Line136=      SELECT @TakeCount=COUNT(*), @TakeFund=ROUND(ISNULL(SUM(Take_Fund),0), 2)
Line137=        FROM dbo.ST_TakeFund WHERE  CONVERT(CHAR(16), Take_Date, 20) >=  @sBgnDate AND CONVERT (CHAR(16), Take_Date, 20) <= @sEndDate
Line138=             AND User_No IN (SELECT User_No FROM #SelectPerson)
Line139=      SELECT @TakeCount=@TakeCount + COUNT(*), @TakeFund=@TakeFund + ROUND(ISNULL(SUM(Take_Fund),0), 2)
Line140=        FROM dbo.ST_TakeFundHistory WHERE   CONVERT(CHAR(16), Take_Date, 20) >=  @sBgnDate AND CONVERT (CHAR(16), Take_Date, 20) <= @sEndDate
Line141=             AND User_No IN (SELECT User_No FROM #SelectPerson)
Line142=      --6.銷卡次數、銷卡退金額、銷卡退押金(加上歷史記錄)
Line143=      SELECT @BackCount=COUNT(*), @BackFund=ROUND(ISNULL(SUM(Back_Fund),0), 2), @BackForegift=ROUND(ISNULL(SUM(Back_Foregift),0), 2),
Line144=             @SmallFund=ROUND(ISNULL(SUM(Back_SmallFund),0), 2)
Line145=        FROM dbo.ST_DeleteCard WHERE  CONVERT(CHAR(16), Del_Date, 20) >=  @sBgnDate AND CONVERT (CHAR(16), Del_Date, 20) <= @sEndDate
Line146= 	AND User_No IN (SELECT User_No FROM #SelectPerson)
Line147=      SELECT @BackCount=@BackCount + COUNT(*), @BackFund=@BackFund + ROUND(ISNULL(SUM(Back_Fund),0), 2), @BackForegift=@BackForegift + ROUND(ISNULL(SUM(Back_Foregift),0), 2),
Line148=             @SmallFund=@SmallFund + ROUND(ISNULL(SUM(Back_SmallFund),0), 2)
Line149=        FROM dbo.ST_DeleteCardHistory WHERE  CONVERT(CHAR(16), Del_Date, 20) >=  @sBgnDate AND CONVERT (CHAR(16), Del_Date, 20) <= @sEndDate
Line150=	 AND User_No IN (SELECT User_No FROM #SelectPerson)
Line151=      --7.小金額轉賬次數、小金額轉賬總金額(加上歷史記錄)
Line152=      SELECT @TransferCount=COUNT(*), @TransferFund=ROUND(ISNULL(SUM(Transfer_Fund),0), 2)
Line153=        FROM dbo.ST_TransferAccount WHERE  CONVERT(CHAR(16), Transfer_Date, 20) >=  @sBgnDate AND CONVERT (CHAR(16), Transfer_Date, 20) <= @sEndDate
Line154=	AND User_No IN (SELECT User_No FROM #SelectPerson)   
Line155=      SELECT @TransferCount=@TransferCount + COUNT(*), @TransferFund=@TransferFund + ROUND(ISNULL(SUM(Transfer_Fund),0), 2)
Line156=        FROM dbo.ST_TransferAccountHistory WHERE  CONVERT(CHAR(16), Transfer_Date, 20) >=  @sBgnDate AND CONVERT (CHAR(16), Transfer_Date, 20) <= @sEndDate
Line157=	AND User_No IN (SELECT User_No FROM #SelectPerson)   
Line158=   END
Line159=   --8.總計次數
Line160=   SET @Count=@CardCount+@SupplyCount+@SubsidyCount+@AlterCount+@TakeCount+@BackCount+@TransferCount
Line161=   --9.應繳金額
Line162=   SET @Money=ROUND(@CardCharge+@CardForegif+@SupplyFund+@AlterFee-@TakeFund-@BackFund-@BackForegift-@SmallFund, 2)
Line163=   --10.總消費金額、總消費次數(加上歷史記錄)
Line164=   SELECT @AllConsumeCount=COUNT(*), @AllConsumeMoney=ROUND(ISNULL(SUM(Consume_Fund),0), 2)
Line165=   FROM dbo.XF_Consume WHERE  CONVERT(CHAR(16), Consume_Time, 20) >=  @sBgnDate AND CONVERT (CHAR(16), Consume_Time, 20)   <= @sEndDate
Line166=   SELECT @AllConsumeCount=@AllConsumeCount + COUNT(*), @AllConsumeMoney=@AllConsumeMoney + ROUND(ISNULL(SUM(Consume_Fund),0), 2)
Line167=   FROM dbo.XF_ConsumeHistory WHERE  CONVERT(CHAR(16), Consume_Time, 20) >=  @sBgnDate AND CONVERT (CHAR(16), Consume_Time, 20) <= @sEndDate
Line168=   SELECT @CardCount[Card_Count], @SupplyCount[Supply_Count], @SubsidyCount[Subsidy_Count], @AlterCount[Alter_Count],
Line169=	@TakeCount[Take_Count], @BackCount[Back_Count], @TransferCount[Transfer_Count],@ChangeCount[Change_Count],
Line170=	@CardCharge[Card_Charge], @CardForegif[Card_Foregif], @SupplyFund[Supply_Fund], @SubsidyFund[Subsidy_Fund],@SmallFund[Small_Fund],
Line171=	@AlterFee[Alter_Fee], @TakeFund[Take_Fund], @BackFund[Back_Fund], @BackForegift[Back_Foregift], @TransferFund[Transfer_Fund],
Line172=	@Count[Count], @Money[Mny_Sum], @sBgnDate[Date_Bgn], @sEndDate[Date_End], @UserName[User_Name],
Line173=	@AllConsumeCount[AllConsumeCount],@AllConsumeMoney[AllConsumeMoney]
[Step160]
Line1=IF OBJECT_ID('KQ_pd_UpdateAdjustClass') IS NOT NULL DROP PROCEDURE KQ_pd_UpdateAdjustClass
[Step161]
Line1=CREATE PROCEDURE dbo.KQ_pd_UpdateAdjustClass
Line2=  @AClassCode VARCHAR(30),--轉化為班次代號之后的班次臨時表名
Line3=  @FAdjustEnd VARCHAR(30),--最后經處理的臨時表
Line4=  @FAdjustClassTmp VARCHAR(30),--在界面上所操作的當前臨時表
Line5=  @KQ_Adjustclass VARCHAR(30),--班次調整表名（KQ_AdjustClass或者KQ_AdjustClassHistory）
Line6=  @StartDate DATETIME,--表格中開始日期
Line7=  @User_No VARCHAR(20),
Line8=  @Remark VARCHAR(200)
Line9=AS
Line10=DECLARE  @SQL Varchar(8000);
Line11=DECLARE  @SQL_D Varchar(8000);
Line12=DECLARE  @I  Int,@FieldValue Varchar(100),@Field Varchar(20);
Line13=DECLARE  @DATE DateTime
Line14=  SET @I=1 ;
Line15=  SET @SQL=''
Line16=  WHILE  @I< 32
Line17=  BEGIN
Line18=    SET @DATE=@StartDate+@I-1;
Line19=    SET @FieldValue =CONVERT(Varchar(10),@StartDate+@I-1,126);
Line20=    SET @Field=CAST('D'+CAST(@i as Varchar)as Varchar)
Line21=    IF @SQL = ''
Line22=     SET @SQL = 'SELECT Person_ID,'''+CAST(@FieldValue AS VARCHAR)+''' AS Adjust_Date, '+
Line23=                ' A.Class_No,'+ @User_No +' AS User_NO,GetDate() AS Update_Date,'+@Remark +' AS Remark '+
Line24=                ' FROM '+ @FAdjustClassTmp +
Line25=                ' LEFT JOIN '+@AClassCode +'  A on A.Class_Code='+@Field+
Line26='                ' WHERE ISNull('+@Field+','''')<>''''''
Line27=    ELSE
Line28=     SET @SQL = @SQL + ' UNION ALL '+
Line29=             'SELECT Person_ID,'''+CAST(@FieldValue AS VARCHAR)+''' AS Adjust_Date,'+
Line30=                ' A.Class_No,'+ @User_No +' AS User_NO,GetDate() AS Update_Date,'+@Remark +' AS Remark '+
Line31=                ' FROM '+ @FAdjustClassTmp +
Line32=                ' LEFT JOIN '+@AClassCode +' A on A.Class_Code='+@Field+
Line33='                 ' WHERE ISNull('+@Field+','''')<>''''''
Line34=    SET @I=@I+1;
Line35=  END
Line36=-- 
Line37=  SET @SQL='INSERT INTO '+@FAdjustEnd +'(Person_ID,Adjust_Date,Class_No,User_NO,Update_Date,Remark)'+
Line38='           ' SELECT Person_ID,Adjust_Date,Class_No,User_NO,Update_Date,Remark  FROM ('+ @SQL +')A''
Line39=  EXEC(@SQL)
Line40=--刪除不在臨時表的記錄
Line41=  SET @SQL ='DELETE  '+ @KQ_Adjustclass +
Line42=                ' WHERE EXISTS (SELECT Person_ID FROM '+ @FAdjustClassTmp +' A '+
Line43=                               ' WHERE A.Person_ID='+@KQ_Adjustclass+'.Person_ID)'+
Line44=                ' AND NOT EXISTS(SELECT Person_ID FROM '+@FAdjustEnd+' B '+
Line45=                               ' WHERE B.Person_ID='+@KQ_Adjustclass+'.Person_ID '+
Line46=                               ' AND B.ADjust_Date='+@KQ_Adjustclass+'.Adjust_Date)';
Line47= -- EXEC(@SQL)
Line48=--修改班次日期相同的記錄
Line49=  SET @SQL ='UPDATE '+ @KQ_Adjustclass +' SET Class_No=A.Class_No,Remark=A.Remark,User_NO=A.User_No'+ 
Line50=            ',Update_Date=A.Update_Date FROM '+@FAdjustEnd+' A  WHERE '+@KQ_AdjustClass+'.Person_ID=A.Person_ID '+
Line51='           ' AND '+@KQ_AdjustClass+'.Adjust_Date=A.Adjust_Date ''
Line52=  EXEC(@SQL)
Line53=--插入臨時表存在，但KQ_AdjustClass中不存在的記錄
Line54=  SET @SQL ='INSERT INTO '+ @KQ_Adjustclass +'(Person_ID,Adjust_Date,Class_No,User_NO,Update_Date,Remark)'+
Line55=          '  SELECT A.Person_ID,CONVERT(Varchar(10),A.Adjust_Date,126),'+
Line56=          '  A.Class_No,A.User_NO,A.Update_Date,A.Remark From '+ @FAdjustEnd+' A  ' +
Line57=          '  LEFT JOIN '+ @KQ_Adjustclass +' B On B.Person_ID=A.Person_ID and B.Adjust_Date=A.Adjust_Date '+
Line58=          '  WHERE ISNULL(B.Class_No,'''')=''''';
Line59=  EXEC(@SQL)
[Step162]
Line1=INSERT INTO dbo.ST_MenuInfo(Menu_ID,System_No,Menu_No,Menu_Name,Menu_Hint,Para_ID,Call_Mode,Form_Name,ShortKey,
Line2=                        Menu_Index,Image_Index,Separator,InToolBar,Remark)
Line3=SELECT * FROM (SELECT 49[Menu_ID],'ST'[System_No],'ST_0418'[Menu_No],'未領取補貼明細'[Menu_Name],'未領取補貼明細'[Menu_Hint],24[Para_ID],
Line4=                      1[Call_Mode],'TfrmUnTakeSubsidy'[Form_Name],NULL[ShortKey],14[Menu_Index],25[Image_Index],0[Separator],0[InToolBar],NULL[Remark]
Line5=              ) A
Line6= WHERE A.Menu_ID NOT IN (SELECT Menu_ID FROM dbo.ST_MenuInfo)
[Step163]
Line1=--卡類押金費用報表
Line2=IF OBJECT_ID('st_pd_CardForegiftCharge') IS NOT NULL DROP PROCEDURE st_pd_CardForegiftCharge
[Step164]
Line1=/*******************************************/
Line2=/* 類型：報表                              */
Line3=/* 模塊來源：核心平台                      */
Line4=/* 報表名稱：卡類押金費用報表              */
Line5=/*                                         */
Line6=/*******************************************/
Line7=Create PROCEDURE st_pd_CardForegiftCharge
Line8=@UserNo   VARCHAR(8000),
Line9=@sDateBgn NVARCHAR(20),
Line10=@SDateEnd NVARCHAR(20)
Line11=AS
Line12=DECLARE @SUserNo VARCHAR(20)
Line13=DECLARE @Pos INT
Line14=SET @SUserNo = ''
Line15=SET @Pos = 0
Line16=IF @UserNo = 'AllUsers'
Line17=BEGIN
Line18=  SELECT C.Type_No,C.Type_Name,Sum(Card_Foregift) Card_Foregift,Sum(Card_Charge) Card_Charge,SUM(Alter_Fee) Alter_Fee FROM ST_Card
Line19=  A LEFT JOIN ST_AlterCard B ON A.Person_ID = B.Person_ID LEFT JOIN ST_CardType C On A.Type_No = C.Type_No 
Line20=  WHERE Card_NewDate >= @sDateBgn AND Card_NewDate <= @sDateEnd GROUP BY C.Type_NO,C.Type_Name Order By C.Type_No
Line21=END
Line22=ELSE
Line23=BEGIN
Line24=  --0.取得選中的出納員
Line25=  IF OBJECT_ID('tempdb..#SelectPerson') IS NOT NULL DROP TABLE #SelectPerson
Line26=  SELECT User_No INTO #SelectPerson FROM ST_UserInfo WHERE 1=0
Line27=  WHILE (@UserNo <> '' )
Line28=  BEGIN
Line29=    SET @Pos = CHARINDEX(',', @UserNo)
Line30=    IF @Pos = 0
Line31=    BEGIN
Line32=      SET @SUserNo=@UserNo
Line33=      SET @UserNo=''
Line34=    END
Line35=    ELSE  
Line36=    BEGIN
Line37=      SET @SUserNo=SUBSTRING(@UserNo, 1, @Pos - 1)
Line38=      SET @UserNo=SUBSTRING(@UserNo,@Pos + 1, 8000)
Line39=    END
Line40=   INSERT INTO #SelectPerson(User_No) VALUES(@SUserNo)
Line41=  END
Line42=  --1.取數據
Line43=  SELECT C.Type_No,C.Type_Name,Sum(Card_Foregift) Card_Foregift,Sum(Card_Charge) Card_Charge,SUM(Alter_Fee) Alter_Fee FROM ST_Card
Line44=  A LEFT JOIN ST_AlterCard B ON A.Person_ID = B.Person_ID LEFT JOIN ST_CardType C On A.Type_No = C.Type_No 
Line45=  WHERE Card_NewDate >= @sDateBgn AND Card_NewDate <= @sDateEnd AND 
Line46=  A.User_No in (SELECT User_No FROM #SelectPerson) GROUP BY C.Type_NO,C.Type_Name Order By C.Type_No
Line47=END
[Step165]
Line1=INSERT INTO dbo.ST_MenuInfo(Menu_ID,System_No,Menu_No,Menu_Name,Menu_Hint,Para_ID,Call_Mode,Form_Name,ShortKey,
Line2=                        Menu_Index,Image_Index,Separator,InToolBar,Remark)
Line3=SELECT * FROM (SELECT 101[Menu_ID],'LH'[System_No],'LH_06'[Menu_No],'系統管理'[Menu_Name],NULL[Menu_Hint],-1[Para_ID],NULL[Call_Mode],
Line4=                      NULL[Form_Name],NULL[ShortKey],'1'[Menu_Index],NULL[Image_Index],NULL[Separator],0[InToolBar],NULL[Remark]
Line5=                UNION ALL SELECT 131,'LH','LH_0409','部門消費綜合表','部門消費綜合表',122,'1','TfrmDeptSumRpt',NULL,'1',17,'0',0,NULL
Line6=) A WHERE A.Menu_ID NOT IN (SELECT Menu_ID FROM dbo.ST_MenuInfo)
[Step166]
Line1=--靈活消費商品匯總表（機器）
Line2=IF OBJECT_ID('XF_Pd_RptWareSum') IS NOT NULL DROP PROCEDURE XF_Pd_RptWareSum  
[Step167]
Line1=/*******************************************/
Line2=/* 類型：報表                              */
Line3=/* 模塊來源：                              */
Line4=/* 報表名稱：                              */
Line5=/*                                         */
Line6=/*******************************************/
Line7=Create PROCEDURE XF_Pd_RptWareSum  
Line8=@DateBgn Datetime,
Line9=@DateEnd Datetime
Line10=AS
Line11=Select c.*,d.Moc_Name
Line12= From (
Line13=Select a.*,b.Ware_Name From (
Line14=Select sum(Consume_Copy) as Capacity , count(*) as Copy,sum(Consume_Fund) as Fund,
Line15=Ware_ID,Moc_No From xf_consume
Line16=Where System_No ='LH' And Consume_Time >= @DateBgn And Consume_Time <= @DateEnd And Consume_mode = '4'
Line17=Group By Moc_No,Ware_ID  --WITH ROLLUP
Line18=) a right Join
Line19=XF_WareInfo b ON a.Ware_ID = b.Ware_ID where b.system_no = 'lh'
Line20=) c Left Join
Line21=St_Moc d On c.Moc_No=d.Moc_No  Where d.System_No = 'LH' Order By c.Moc_No,c.Ware_ID
[Step168]

[Step169]

[Step170]
Line1=IF NOT EXISTS(SELECT Param_Code FROM ST_SysParam WHERE Param_Code='Repeat.BeginTime')   
Line2=   INSERT INTO ST_SysParam(Param_Code,Param_Name,Param_Value)
Line3=   SELECT 'Repeat.BeginTime','Repeat.BeginTime',CONVERT(VARCHAR(50), ISNULL(MAX(Consume_Time),GETDATE()), 121) 
Line4=     FROM XF_Consume
[Step171]
Line1=IF NOT EXISTS(SELECT Param_Code FROM ST_SysParam WHERE Param_Code='Fake.BeginTime')
Line2=   INSERT INTO ST_SysParam(Param_Code,Param_Name,Param_Value)
Line3=   SELECT 'Fake.BeginTime','Fake.BeginTime',CONVERT(VARCHAR(50), ISNULL(MAX(Consume_Time),GETDATE()), 121) 
Line4=     FROM XF_Consume
[Step172]
Line1=IF OBJECT_ID('XF_pd_CleanupFakeConsume') IS NOT NULL
Line2=   DROP PROCEDURE XF_pd_CleanupFakeConsume
[Step173]
Line1=CREATE PROCEDURE dbo.XF_pd_CleanupFakeConsume
Line2=AS
Line3=   --Get begin time
Line4=   DECLARE @BeginTime DATETIME
Line5=   SELECT @BeginTime=CAST(Param_Value AS DATETIME) FROM ST_SysParam WHERE Param_Code='Fake.BeginTime'
Line6=   --1.SELECT
Line7=   DECLARE @Table TABLE(ID_KEY BIGINT, Person_ID INT,Consume_Time DATETIME,Consume_Fund MONEY,
Line8=     Card_Balance MONEY,DB_Balance MONEY,Moc_No VARCHAR(20),COM_No VARCHAR(20))
Line9=   DECLARE @FakeConsume TABLE(ID_KEY BIGINT,Person_ID INT,Consume_Fund MONEY)
Line10=   DECLARE @Interval TINYINT
Line11=   SET @Interval=3
Line12=   INSERT INTO @Table
Line13=   SELECT ID_KEY,A.Person_ID,Consume_Time,A.Consume_Fund,A.Card_Balance,DB_Balance,A.Moc_No,COM_No
Line14=     FROM (SELECT Person_ID,Consume_Fund,Card_Balance,Moc_No
Line15=             FROM (SELECT ID_KEY FROM XF_Consume WHERE Consume_Time>@BeginTime) A1
Line16=             LEFT JOIN XF_Consume B1 ON B1.ID_KEY=A1.ID_KEY
Line17=            WHERE Data_Type=0 AND Consume_Fund<>0
Line18=            GROUP BY Person_ID,Consume_Fund,Card_Balance,Moc_No
Line19=           HAVING COUNT(*)>1
Line20=          ) B,XF_Consume A
Line21=    WHERE Consume_Time>@BeginTime AND A.Person_ID=B.Person_ID AND A.Consume_Fund=B.Consume_Fund
Line22=      AND A.Card_Balance=B.Card_Balance AND A.Moc_No=B.Moc_No
Line23=    ORDER BY A.Person_ID,Consume_Time
Line24=   INSERT INTO @FakeConsume
Line25=   SELECT ID_Key,Person_ID,Consume_Fund
Line26=     FROM (SELECT ID_Key,Person_ID,Consume_Fund,Consume_Time,
Line27=                  (SELECT TOP 1 Consume_Time FROM @Table WHERE Person_ID=A.Person_ID AND Consume_Fund=A.Consume_Fund
Line28=                      AND Card_Balance=A.Card_Balance AND Moc_No=a.Moc_No AND Consume_Time<A.Consume_Time 
Line29=                    ORDER BY Consume_Time DESC
Line30=                  ) dDateEnd
Line31=             FROM @Table A
Line32=          ) B
Line33=    WHERE dDateEnd IS NOT NULL AND DATEDIFF(s, dDateEnd, Consume_Time)<=@Interval
Line34=   --2.Backup
Line35=   INSERT INTO XF_ConsumeCleanup(Person_ID,System_No,Consume_Mode,Consume_Time,Before_Fund,Consume_Fund,
Line36=      Consume_Copy,Consume_TimeLen,Consume_Capacity,Repast_No,Ware_ID,MOC_No,COM_No,Card_Balance,DB_Balance,
Line37=      Card_SmallAccount,Data_Type,Remark,Download_Time)
Line38=   SELECT C.Person_ID,System_No,Consume_Mode,C.Consume_Time,Before_Fund,C.Consume_Fund,
Line39=          Consume_Copy,Consume_TimeLen,Consume_Capacity,Repast_No,Ware_ID,MOC_No,COM_No,Card_Balance,DB_Balance,
Line40=          Card_SmallAccount,Data_Type,Remark,Download_Time
Line41=     FROM XF_Consume C,@FakeConsume D 
Line42=    WHERE D.ID_Key=C.ID_Key
Line43=   --3.UPDATE
Line44=   UPDATE ST_Card SET Card_Balance=Card_Balance+C.Consume_Fund
Line45=     FROM (SELECT Person_ID,SUM(Consume_Fund)[Consume_Fund]
Line46=             FROM @FakeConsume GROUP BY Person_ID
Line47=          ) C
Line48=    WHERE ST_Card.Person_ID=C.Person_ID
Line49=   --4.DELETE
Line50=   DELETE XF_Consume FROM @FakeConsume C WHERE C.ID_Key=XF_Consume.ID_Key 
Line51=   --Update begin time
Line52=   UPDATE ST_SysParam SET Param_Value=CONVERT(VARCHAR(50), A.Consume_Time, 121)
Line53=     FROM (SELECT ISNULL(MAX(Consume_Time),GETDATE()) [Consume_Time]
Line54=             FROM XF_Consume
Line55=          ) A
Line56=    WHERE Param_Code='Fake.BeginTime'
[Step174]
Line1=IF OBJECT_ID('XF_pd_CleanupRepeatConsume') IS NOT NULL
Line2=   DROP PROCEDURE XF_pd_CleanupRepeatConsume
[Step175]
Line1=CREATE  PROCEDURE dbo.XF_pd_CleanupRepeatConsume
Line2=AS
Line3=    --Get begin time
Line4=   DECLARE @BeginTime DATETIME
Line5=   SELECT @BeginTime=CAST(Param_Value AS DATETIME) FROM ST_SysParam WHERE Param_Code='Repeat.BeginTime'
Line6=   IF EXISTS(SELECT * FROM ST_CardInfo WHERE UPPER(SUBSTRING(Card_Type,1,2))='IC')
Line7=   BEGIN  
Line8=   	DECLARE @ConsumeTable TABLE(Person_ID INT,Consume_Time DATETIME,Consume_Fund MONEY,
Line9=   	Card_Balance MONEY,Data_Type TINYINT,ID_Key BIGINT)
Line10=      /**/
Line11=   	INSERT INTO @ConsumeTable
Line12=      SELECT Person_ID,Consume_Time,0,Card_Balance,0,MIN(B1.ID_Key)[ID_Key]
Line13=        FROM (SELECT ID_KEY FROM XF_Consume WITH (HOLDLOCK) WHERE Consume_Time>@BeginTime) A1 
Line14=        LEFT JOIN XF_Consume B1 ON B1.ID_Key=A1.ID_Key
Line15=       WHERE Data_Type=0 
Line16=       GROUP BY Person_ID,Consume_Time,Card_Balance HAVING COUNT(*)>1
Line17=   	--1.Backup
Line18=   	--1.1.Consume
Line19=   	INSERT INTO XF_ConsumeCleanup(Person_ID,System_No,Consume_Mode,Consume_Time,Before_Fund,Consume_Fund,
Line20=   	            Consume_Copy,Consume_TimeLen,Consume_Capacity,Repast_No,Ware_ID,MOC_No,COM_No,Card_Balance,DB_Balance,
Line21=               	Card_SmallAccount,Data_Type,Remark,Download_Time)
Line22=   	SELECT A.Person_ID,System_No,Consume_Mode,A.Consume_Time,Before_Fund,A.Consume_Fund,Consume_Copy,Consume_TimeLen,
Line23=             Consume_Capacity,Repast_No,Ware_ID,MOC_No,COM_No,A.Card_Balance,DB_Balance,Card_SmallAccount,A.Data_Type,
Line24=             Remark,Download_Time
Line25=     	  FROM @ConsumeTable B,XF_Consume A
Line26=   	 WHERE A.Consume_Time>@BeginTime AND A.ID_Key<>B.ID_Key AND A.Person_ID=B.Person_ID AND A.Consume_Time=B.Consume_Time
Line27=   	   AND A.Card_Balance=B.Card_Balance AND A.Data_Type=0   	
Line28=   	--2.Update
Line29=   	UPDATE ST_Card SET Card_Balance=Card_Balance+C.Consume_Fund
Line30=   	  FROM (SELECT SUM(A.Consume_Fund)[Consume_Fund],A.Person_ID FROM XF_Consume A,
Line31=   	          (SELECT * FROM @ConsumeTable WHERE Data_Type=0) B
Line32=   	         WHERE A.Consume_Time>@BeginTime AND A.Data_Type=0 AND A.Person_ID=B.Person_ID
Line33=                 AND A.Consume_Time=B.Consume_Time AND A.Card_Balance=B.Card_Balance AND A.ID_Key<>B.ID_Key
Line34=   	         GROUP BY A.Person_ID
Line35=   	       ) C
Line36=   	 WHERE ST_Card.Person_ID=C.Person_ID
Line37=   	--3.Delete
Line38=   	DELETE XF_Consume
Line39=   	  FROM (SELECT * FROM @ConsumeTable WHERE Data_Type=0) A
Line40=       WHERE A.Consume_Time>@BeginTime AND A.Data_Type=0 AND A.Person_ID=XF_Consume.Person_ID 
Line41=         AND A.Consume_Time=XF_Consume.Consume_Time
Line42=      	AND A.Card_Balance=XF_Consume.Card_Balance AND A.ID_Key<>XF_Consume.ID_Key
Line43=   END
Line44=   ELSE
Line45=   BEGIN
Line46=      /*取出存在重復消費人員的名單和一個有用的記錄的Id_key*/
Line47=   	if OBJECT_ID('tempDB..#RP')<>0 drop table #RP
Line48=      /**/ 
Line49=      SELECT MIN(B1.ID_Key) AS ID_Key,Person_ID,Consume_Time,Consume_Fund 
Line50=        INTO #RP
Line51=        FROM (SELECT ID_KEY FROM XF_Consume WHERE Consume_Time>@BeginTime) A1 
Line52=        LEFT JOIN XF_Consume B1 ON B1.ID_Key=A1.ID_Key
Line53=       GROUP BY Person_ID,Consume_Time,Consume_Fund  HAVING COUNT(*)>1
Line54=       /**/
Line55=   	if @@ROWCOUNT > 0 
Line56=   	begin
Line57=   		/*將重復的記錄取出來*/
Line58=   		if OBJECT_ID('tempDB..#RP1')<>0 drop table #RP1
Line59=   		select A.* into #RP1 from XF_Consume A , #RP B  
Line60=   		  where A.person_ID=B.person_ID and A.consume_Time=B.Consume_Time and A.consume_fund=B.consume_fund 
Line61=                     and A.id_Key<>B.id_key
Line62=   		/*合計重復的金額*/
Line63=   		if OBJECT_ID('tempDB..#RP2')<>0 drop table #RP2
Line64=   		select person_ID,Sum(consume_fund) as Funds Into #RP2 from #RP1
Line65=   		  group by person_ID
Line66=   		/*將重復消費的金額返回數據庫*/
Line67=   		update ST_Card set Card_Balance=ST_Card.Card_Balance +#RP2.Funds from #RP2 
Line68=   		  where ST_Card.person_ID=#RP2.person_ID
Line69=   		/*保存并刪除重復的記錄*/
Line70=   		insert into xf_consumecleanup
Line71=                    (person_id,system_no,consume_mode,consume_time,before_fund,consume_fund,
Line72=   		  consume_copy,consume_timelen,consume_capacity,repast_no,ware_id,moc_no,com_no,card_balance,db_balance,
Line73=   		  card_smallaccount,data_type,remark,download_time)
Line74=   		select 
Line75=                     person_id,system_no,consume_mode,consume_time,before_fund,consume_fund,
Line76=   		  consume_copy,consume_timelen,consume_capacity,repast_no,ware_id,moc_no,com_no,card_balance,db_balance,
Line77=   		  card_smallaccount,data_type,remark,download_time
Line78=   		from #RP1
Line79=   		Delete from XF_Consume where id_key in (select id_Key from #RP1)
Line80=   		/*刪除臨時表*/
Line81=   		drop table #RP1
Line82=   		drop table #RP2
Line83=   	end
Line84=   	drop table #RP  
Line85=   END
Line86=   --Update begin time
Line87=   UPDATE ST_SysParam SET Param_Value=CONVERT(VARCHAR(50), A.Consume_Time, 121)
Line88=     FROM (SELECT ISNULL(MAX(Consume_Time),GETDATE()) [Consume_Time]
Line89=             FROM XF_Consume
Line90=          ) A
Line91=    WHERE Param_Code='Repeat.BeginTime'
[Step176]
Line1=IF OBJECT_ID('XF_pd_CardConsume') IS NOT NULL
Line2=   DROP PROCEDURE XF_pd_CardConsume
[Step177]
Line1=CREATE PROCEDURE dbo.XF_pd_CardConsume
Line2=   (@PersonID INT,         --人員標識號
Line3=    @CardNo VARCHAR(20),   --卡號
Line4=    @SystemNo VARCHAR(20), --子系統編號
Line5=    @ConsumeMode TINYINT,  --消費模式
Line6=    @ConsumeTime DATETIME, --消費時間
Line7=    @Balance MONEY,        --卡余額(本次消費后的余額)
Line8=    @DBBalance MONEY,      --庫余額(本次消費后的余額)
Line9=    @BeforeFund MONEY,     --打折前的消費金額
Line10=    @ConsumeFund MONEY,    --打折后的消費金額
Line11=    @FundOld MONEY,        --原消費金額
Line12=    @Copies INT,           --消費份數
Line13=    @Quantity INT,         --消費數量
Line14=    @TimeLen INT,          --消費時長
Line15=    @RepastNo VARCHAR(20), --餐別編號
Line16=    @WareID VARCHAR(20),   --商品編號
Line17=    @MocNo VARCHAR(20),    --終端機編號
Line18=    @HardNo VARCHAR(20),   --硬件編號
Line19=    @IDKey INT,            --糾錯記錄的ID_KEY
Line20=    @Options TINYINT,      --記錄選項(0表示消費,1表示糾錯,2表示脫機,3表示有卡無人的脫機記錄,
Line21=                           --        4表示聯機假寫卡記錄,5表示脫機糾錯記錄,6表示脫機假寫卡記錄)
Line22=    @SubsidyFund MONEY     --本次發放補貼金額
Line23=   )
Line24=AS   
Line25=   --0.更新IC卡余額
Line26=   UPDATE ST_Card 
Line27=      SET Update_Time=@ConsumeTime,IC_Balance=@Balance
Line28=    WHERE Person_ID=@PersonID AND Update_Time<=@ConsumeTime AND DATEDIFF(day,Update_Time,@ConsumeTime)<90
Line29=   --Update begin time
Line30=   UPDATE ST_SysParam SET Param_Value=CONVERT(VARCHAR(50), @ConsumeTime, 121)
Line31=    WHERE Param_Code='Fake.BeginTime' AND CAST(Param_Value AS DATETIME)>@ConsumeTime
Line32=   --
Line33=   UPDATE ST_SysParam SET Param_Value=CONVERT(VARCHAR(50), @ConsumeTime, 121)
Line34=    WHERE Param_Code='Repeat.BeginTime' AND CAST(Param_Value AS DATETIME)>@ConsumeTime
Line35=   --
Line36=   DECLARE @DataType TINYINT   
Line37=   IF @Options = 1 --消費糾錯
Line38=   BEGIN
Line39=      --1.更新余額和補貼金額
Line40=      UPDATE ST_Card 
Line41=         SET @DBBalance=Card_Balance-@ConsumeFund+@FundOld,Card_Balance=@DBBalance
Line42=       WHERE Person_ID=@PersonID
Line43=      --2.插入消費記錄
Line44=      INSERT INTO XF_ConsumeTemp(Person_ID,System_No,Consume_Mode,Consume_Time,Before_Fund,Consume_Fund,Consume_Copy,
Line45=                                 Consume_TimeLen,Consume_Capacity,Repast_No,Ware_ID,Moc_No,COM_No,Card_Balance,
Line46=                                 DB_Balance,Card_SmallAccount,Data_Type,Is_Commited) 
Line47=      SELECT @PersonID,@SystemNo,@ConsumeMode,GETDATE(),-Before_Fund,-Consume_Fund,-Consume_Copy,-Consume_TimeLen,-Consume_Capacity,@RepastNo,
Line48=             Ware_ID,@MocNo,@HardNo,@Balance,@DBBalance,0,2,0 FROM XF_ConsumeTemp WHERE ID_KEY=@IDKEY --糾錯記錄
Line49=      UNION ALL SELECT @PersonID,@SystemNo,@ConsumeMode,GETDATE(),@BeforeFund,@ConsumeFund,@Copies,@TimeLen,@Quantity,@RepastNo,
Line50=                       @WareID,@MocNo,@HardNo,@Balance,@DBBalance,0,2,0 --新消費記錄
Line51=      --3.更新糾錯標識
Line52=      UPDATE XF_ConsumeTemp SET Data_Type=2 WHERE ID_KEY=@IDKey
Line53=      --4.更新已提交的糾錯標識
Line54=      UPDATE XF_Consume SET Data_Type=2 
Line55=        FROM (SELECT Source_ID FROM XF_ConsumeTemp WHERE ID_KEY=@IDKey AND Is_Commited=1) A
Line56=       WHERE ID_KEY=A.Source_ID
Line57=   END
Line58=   ELSE IF @Options <> 3 --正常消費或假寫卡或脫機消費、糾錯
Line59=   BEGIN
Line60=      IF (@Options = 0) OR (@Options = 4) SET @ConsumeTime=GETDATE() --非脫機記錄取數據庫的當前時間
Line61=      IF (@Options = 0) OR (@Options = 2) SET @DataType=0 --聯機消費或脫機消費
Line62=      ELSE IF @Options = 5 SET @DataType=2 --脫機糾錯
Line63=      ELSE SET @DataType=8 --假寫卡
Line64=      IF @DataType<>8 --非假寫卡記錄
Line65=      BEGIN         
Line66=         IF (@Options <> 5) AND (@Options <> 2) --非脫機記錄
Line67=         BEGIN
Line68=            --1.更新余額和補貼金額
Line69=            UPDATE ST_Card 
Line70=               SET @DBBalance=Card_Balance-@ConsumeFund,Card_Balance=@DBBalance
Line71=             WHERE Person_ID=@PersonID
Line72=            --2.插入消費記錄
Line73=            INSERT INTO XF_ConsumeTemp(Person_ID,System_No,Consume_Mode,Consume_Time,Before_Fund,Consume_Fund,Consume_Copy,
Line74=                                       Consume_TimeLen,Consume_Capacity,Repast_No,Ware_ID,Moc_No,COM_No,Card_Balance,
Line75=                                       DB_Balance,Card_SmallAccount,Data_Type,Is_Commited)
Line76=            SELECT @PersonID,@SystemNo,@ConsumeMode,@ConsumeTime,@BeforeFund,@ConsumeFund,@Copies,@TimeLen,@Quantity,@RepastNo,
Line77=                   @WareID,@MocNo,@HardNo,@Balance,@DBBalance,0,@DataType,0
Line78=         END
Line79=         ELSE  --脫機記錄
Line80=         BEGIN 
Line81=            --1.更新余額和補貼金額
Line82=            UPDATE ST_Card 
Line83=               SET @DBBalance=Card_Balance-@ConsumeFund,Card_Balance=@DBBalance
Line84=             WHERE Person_ID=@PersonID
Line85=            --2.插入消費記錄
Line86=            INSERT INTO XF_ConsumeTemp(Person_ID,System_No,Consume_Mode,Consume_Time,Before_Fund,Consume_Fund,Consume_Copy,
Line87=                                       Consume_TimeLen,Consume_Capacity,Repast_No,Ware_ID,Moc_No,COM_No,Card_Balance,
Line88=                                       DB_Balance,Card_SmallAccount,Data_Type,Is_Commited,Download_Time)
Line89=            SELECT @PersonID,@SystemNo,@ConsumeMode,@ConsumeTime,@BeforeFund,@ConsumeFund,@Copies,@TimeLen,@Quantity,@RepastNo,
Line90=                   @WareID,@MocNo,@HardNo,@Balance,@DBBalance,0,@DataType,0,GETDATE()
Line91=         END
Line92=      END
Line93=      ELSE --假寫卡記錄
Line94=      BEGIN
Line95=         --2.插入消費記錄
Line96=         IF (@Options = 4) --非脫機記錄
Line97=         BEGIN
Line98=            INSERT INTO XF_ConsumeFake(Person_ID,System_No,Consume_Mode,Consume_Time,Before_Fund,Consume_Fund,Consume_Copy,
Line99=                                       Consume_TimeLen,Consume_Capacity,Repast_No,Ware_ID,Moc_No,COM_No,Card_Balance,
Line100=                                       DB_Balance,Card_SmallAccount,Data_Type)
Line101=            SELECT @PersonID,@SystemNo,@ConsumeMode,@ConsumeTime,@BeforeFund,@ConsumeFund,@Copies,@TimeLen,@Quantity,@RepastNo,
Line102=                   @WareID,@MocNo,@HardNo,@Balance,@DBBalance,0,@DataType
Line103=         END
Line104=         ELSE  --脫機記錄
Line105=         BEGIN 
Line106=            INSERT INTO XF_ConsumeFake(Person_ID,System_No,Consume_Mode,Consume_Time,Before_Fund,Consume_Fund,Consume_Copy,
Line107=                                       Consume_TimeLen,Consume_Capacity,Repast_No,Ware_ID,Moc_No,COM_No,Card_Balance,
Line108=                                       DB_Balance,Card_SmallAccount,Data_Type,Download_Time)
Line109=            SELECT @PersonID,@SystemNo,@ConsumeMode,@ConsumeTime,@BeforeFund,@ConsumeFund,@Copies,@TimeLen,@Quantity,@RepastNo,
Line110=                  @WareID,@MocNo,@HardNo,@Balance,@DBBalance,0,@DataType,GETDATE()
Line111=         END
Line112=      END
Line113=   END
Line114=   ELSE
Line115=   BEGIN
Line116=      SET @DBBalance=0
Line117=      --1.插入有卡無人消費記錄
Line118=      INSERT INTO ST_NoPersonCard(Card_No,System_No,Consume_Mode,Consume_Time,Consume_Fund,Consume_Copy,
Line119=                                  Consume_TimeLen,Consume_Capacity,Repast_No,Ware_ID,Moc_No,COM_No,Card_Balance,
Line120=                                  DB_Balance,Card_SmallAccount,Data_Type,Download_Date)
Line121=      SELECT @CardNo,@SystemNo,@ConsumeMode,@ConsumeTime,@ConsumeFund,@Copies,@TimeLen,@Quantity,@RepastNo,
Line122=             @WareID,@MocNo,@HardNo,@DBBalance,@Balance,0,0,GETDATE()
Line123=   END
[Step178]
Line1=IF OBJECT_ID('XF_pd_PrepareConsumeCache') IS NOT NULL
Line2=   DROP PROCEDURE XF_pd_PrepareConsumeCache
[Step179]
Line1=CREATE PROCEDURE dbo.XF_pd_PrepareConsumeCache
Line2=AS   
Line3=   --Data_Type(0.硬件消費、1.硬件糾錯、2.軟件消費、3.軟件糾錯(負記錄)、4.軟件糾錯(正記錄)、7.未結算時長消費記錄、8.假寫卡記錄)
Line4=   --1.清空消費緩存表
Line5=   DECLARE @UnCommitCount INT
Line6=   SELECT @UnCommitCount=COUNT(*) FROM XF_ConsumeTemp WHERE Is_Commited=0 --取未提交的消費記錄數
Line7=   IF (@UnCommitCount = 0) --如果消費記錄已經全部提交
Line8=      TRUNCATE TABLE XF_ConsumeTemp --清除數據表
Line9=   ELSE
Line10=      DELETE XF_ConsumeTemp WHERE Is_Commited=1
Line11=   --2.取當天的所有消費記錄
Line12=   DECLARE @Date VARCHAR(10),@MinIDKey BIGINT
Line13=       SET @Date=CONVERT(VARCHAR(10), GETDATE(),121)
Line14=   SELECT @MinIDKey=ID_Key FROM XF_Consume WHERE Consume_Time>=@Date+' 00:00:00' AND Consume_Time<=@Date+' 23:59:59'
Line15=    ORDER BY ID_Key DESC
Line16=   IF @MinIDKey IS NULL SET @MinIDKey=0 
Line17=   INSERT INTO XF_ConsumeTemp(Person_ID,System_No,Consume_Mode,Consume_Time,Before_Fund,Consume_Fund,Consume_Copy,
Line18=                              Consume_TimeLen,Consume_Capacity,Repast_No,Ware_ID,Moc_No,COM_No,Card_Balance,
Line19=                              DB_Balance,Card_SmallAccount,Data_Type,Is_Commited,Source_ID)
Line20=   SELECT Person_ID,System_No,Consume_Mode,Consume_Time,Before_Fund,Consume_Fund,Consume_Copy,
Line21=          Consume_TimeLen,Consume_Capacity,Repast_No,Ware_ID,Moc_No,COM_No,Card_Balance,
Line22=          DB_Balance,Card_SmallAccount,Data_Type,1,B1.ID_KEY
Line23=     FROM (SELECT ID_KEY FROM XF_Consume WHERE Consume_Time>=@Date+' 00:00:00' AND Consume_Time<=@Date+' 23:59:59') A1 
Line24=     LEFT JOIN (SELECT * FROM XF_Consume WHERE ID_Key>=@MinIDKey) B1 ON B1.ID_Key=A1.ID_Key
Line25=    WHERE Data_Type=0 OR Data_Type=2
[Step180]
Line1=--補貼領取明細
Line2=IF OBJECT_ID('st_pd_TakeSubsidyDetail') IS NOT NULL DROP PROCEDURE st_pd_TakeSubsidyDetail
[Step181]
Line1=/*******************************************************/
Line2=/* 類型：報表                                           */
Line3=/* 模塊來源：核心平台                                    */
Line4=/* 報表名稱：補貼領取明細                                */
Line5=/*修改說明：                                            */
Line6=/*2008-11-11  新增補貼狀態過濾                          */
Line7=/*******************************************************/
Line8=CREATE PROCEDURE st_pd_TakeSubsidyDetail
Line9=@Dept_No NVarChar(20), --過濾條件
Line10=@DateTimeBegin NVarChar(20),--開始日期
Line11=@DateTimeEnd NVarChar(20),--結束日期
Line12=@ApplyState Int  --0表示領取失敗，1表示領取成功
Line13=AS
Line14=Declare @SQL NVarChar(4000);
Line15=Declare @ApplyStateSQL NVarChar(200);
Line16=Declare @OrderSQL NVarChar(200);
Line17=Set @SQL = ' ';
Line18=Set @ApplyStateSQL = ' ';
Line19=Set @OrderSQL = ' ';
Line20=Begin
Line21=Set @SQL = 'SELECT a.person_id,c.person_no,a.card_no,a.apply_time,a.subsidy_fund,a.apply_state,d.dept_no, '+
Line22=    ' a.moc_no,a.card_balance,c.person_name,b.subsidy_fund bSubsidy_fund FROM xf_subsidy a '+
Line23=     ' LEFT join st_card b ON a.person_id = b.person_id ' +
Line24=      ' LEFT join st_person c ON b.person_id = c.person_id '+
Line25=       ' LEFT join st_department d ON c.dept_id = d.dept_id '+
Line26=        ' Where d.dept_no like  ''' + @Dept_No + '''' +
Line27=         ' and  Apply_time >= ''' + @DateTimeBegin + '''' +
Line28=          ' and Apply_time  <= ''' + @DateTimeEnd + '''';
Line29=Set @ApplyStateSQL = ' and (a.apply_state =0) ';
Line30=Set @OrderSQL = ' order by d.Dept_No, a.Person_id, a.Apply_Time';
Line31=If @ApplyState = 0
Line32=    Set @SQL = @SQL + @ApplyStateSQL + @OrderSQL;
Line33=Else
Line34=    Set @SQL = @SQL + @OrderSQL;
Line35=Exec(@SQL)
Line36=END
[Step182]
System=MJ
Line1=--0.門禁系統初始化菜單
Line2=INSERT INTO dbo.ST_MenuInfo(Menu_ID,System_No,Menu_No,Menu_Name,Menu_Hint,Para_ID,Call_Mode,Form_Name,ShortKey,
Line3=                        Menu_Index,Image_Index,Separator,InToolBar,Remark)
Line4=SELECT * FROM (SELECT 145[Menu_ID],'MJ'[System_No],'MJ_01'[Menu_No],'系統管理'[Menu_Name],NULL[Menu_Hint],-1[Para_ID],NULL[Call_Mode],
Line5=                      NULL[Form_Name],NULL[ShortKey],'1'[Menu_Index],-1[Image_Index],NULL[Separator],0[InToolBar],NULL[Remark]
Line6= UNION ALL SELECT 650,'MJ','MJ_0409','DVS聯動拍照','DVS聯動拍照',158,1,'TfrmDVSMJScroll',NULL,9,-1,0,0,NULL
Line7=							) A
Line8= WHERE A.Menu_ID NOT IN (SELECT Menu_ID FROM dbo.ST_MenuInfo)
[Step183]
System=MJ
Line1=--1.初始化功能擴展數據
Line2=INSERT INTO dbo.MJ_ExtendFunc(Menu_No,Menu_Name,Is_Menu,Is_Use)
Line3=SELECT A.* FROM (SELECT 'Is_APB'[Menu_No],'防尾隨'[Menu_Name],1[Is_Menu],0[Is_Use]
Line4=                        UNION ALL SELECT 'MJ_0409','DVS聯動拍照',0,0
Line5=							) A
Line6=WHERE A.Menu_No NOT IN(SELECT Menu_No FROM dbo.MJ_ExtendFunc)
[Step184]
System=MJ
Line1=--3.MJ_Door表增加一個字段用于登記DVS的IP
Line2=IF COL_LENGTH('MJ_Door', 'IP') IS NULL 
Line3= Alter Table MJ_Door Add IP VARCHAR(20) NULL
[Step185]
System=MJ
Line1=--4.初始化功能擴展數據
Line2=INSERT INTO dbo.MJ_SysParam(Param_Code,Param_Name,Param_Value)
Line3=SELECT A.* FROM (SELECT 'PHOTO_DOORIN'[Param_Code],'進門是否顯示照片'[Param_Name],'1'[Param_Value]
Line4=			UNION ALL SELECT 'DVSPhotoStoreDir','DVS聯動拍照圖片存儲位置',''	
Line5=							) A
Line6=WHERE A.Param_Code NOT IN(SELECT Param_Code FROM dbo.MJ_SysParam)
[Step186]
System=SK
Line1=--初始化水控參數
Line2=INSERT INTO dbo.SK_SysParam(Param_Code,Param_Name,Param_Value)
Line3=SELECT * FROM (SELECT 'MocType'[Param_Code],'硬件類型'[Param_Name],'1'[Param_Value]
Line4=                UNION ALL SELECT 'CommMode','通訊方式','0'
Line5=              ) A
Line6=WHERE A.Param_Code NOT IN (SELECT Param_Code FROM dbo.SK_SysParam)

[Step187]
System=DC
Line1=IF OBJECT_ID('SF_fn_IsOrderMeal') IS NOT NULL
Line2=  DROP FUNCTION SF_fn_IsOrderMeal 
[Step188]
Line1=CREATE FUNCTION dbo.SF_fn_IsOrderMeal 
Line2=	(@PersonID INT, 
Line3=    @RepastNo VARCHAR(20),
Line4=    @Date DATETIME)
Line5=RETURNS BIT
Line6=AS
Line7=BEGIN
Line8=   DECLARE @Return BIT,@CurDate VARCHAR(10),@TimeBgn VARCHAR(5),@TimeEnd VARCHAR(5),@CurTime VARCHAR(5)
Line9=   SET @CurTime=CONVERT(VARCHAR(5),@Date,108)  
Line10=   SELECT @TimeBgn=Bgn_Time,@TimeEnd=End_Time FROM SF_Repast
Line11=    WHERE Repast_No=@RepastNo
Line12=   --跨天餐別在零晨消費,訂餐日期應為前一天
Line13=   IF (@TimeBgn>@TimeEnd) AND (@CurTime<=@TimeEnd)
Line14=      SET @CurDate=CONVERT(VARCHAR(10),@Date-1,120)
Line15=   ELSE
Line16=      SET @CurDate=CONVERT(VARCHAR(10),@Date,120)   
Line17=   IF EXISTS(SELECT Person_ID FROM SF_OrderData 
Line18=              WHERE Person_ID=@PersonID AND Order_Date=@CurDate 
Line19=                AND Repast_No=@RepastNo
Line20=            )      
Line21=      SET @Return = 1
Line22=   ELSE
Line23=      SET @Return = 0
Line24=   RETURN @Return
Line25=END
[Step189]
System=DC
Line1=IF OBJECT_ID('dbo.SF_Order_pd_OrderDetailQry_R') IS NOT NULL DROP PROCEDURE SF_Order_pd_OrderDetailQry_R
[Step190]
Line1=--訂餐數據明細
Line2=CREATE  PROCEDURE dbo.SF_Order_pd_OrderDetailQry_R
Line3=@qryType INT,				--查詢類別
Line4=@Dept VARCHAR(20),			--部門條件或員工編號 ,根據查詢類別來確定
Line5=@Repast VARCHAR(500),			--餐別條件
Line6=@DateBgn DATETIME,			--起始日期
Line7=@DateEnd DATETIME			--結束日期
Line8=AS
Line9=set nocount on
Line10=Declare @BgnDate varchar(10),@EndDate varchar(10)
Line11=SET @BgnDate = CONVERT(CHAR(10),  @DateBgn, 120)
Line12=SET @EndDate = CONVERT(CHAR(10),  @DateEnd, 120)
Line13=/*訂餐數據*/
Line14=IF @qryType=1 
Line15= 	/** 按部門 + 日期 + 餐別**/
Line16=	SELECT ST_Department.Dept_Name,ST_Person.Person_No,ST_Person.Person_Name,
Line17=	O.Order_Date,O.Order_Time,O.Repast_No,R.Repast_Name,O.Moc_No_DC[Moc_No_DC] 
Line18=	FROM DC_OrderData O
Line19=	LEFT JOIN SF_Repast R On O.Repast_No=R.Repast_No
Line20=	LEFT OUTER JOIN ST_Department 
Line21=	INNER JOIN ST_Person ON ST_Department.Dept_ID = ST_Person.Dept_ID ON O.Person_ID = ST_Person.Person_ID
Line22=	WHERE --(Data_Type=0 OR Data_Type=1) 
Line23=	 Dept_No Like @Dept
Line24=	AND charindex(O.Repast_no, @Repast) > 0
Line25=	AND Order_date <= @EndDate
Line26=	AND Order_date >= @BgnDate
Line27=	ORDER BY ST_Department.Dept_No,ST_Person.Person_No,O.Order_Date
Line28=ELSE  
Line29=        /** 按人員 + 日期 + 餐別**/
Line30=	SELECT ST_Department.Dept_Name,ST_Person.Person_No,ST_Person.Person_Name,
Line31=	O.Order_Date,O.Order_Time,O.Repast_No,R.Repast_Name,O.Moc_No_DC[Moc_No_DC] 
Line32=	FROM DC_OrderData O
Line33=	LEFT JOIN SF_Repast R On O.Repast_No=R.Repast_No
Line34=	LEFT OUTER JOIN ST_Department 
Line35=	INNER JOIN ST_Person ON ST_Department.Dept_ID = ST_Person.Dept_ID ON O.Person_ID = ST_Person.Person_ID
Line36=	WHERE --(Data_Type=0 OR Data_Type=1) 
Line37=	 ST_Person.Person_No =@Dept
Line38=	AND charindex(O.Repast_no, @Repast) > 0
Line39=	AND Order_date <= @EndDate
Line40=	AND Order_date >= @BgnDate
Line41=	ORDER BY ST_Department.Dept_No,ST_Person.Person_No,O.Order_Date
[Step191]
Line1=IF OBJECT_ID('dbo.SF_Order_pd_OrderTotalQry_R') IS NOT NULL DROP PROCEDURE SF_Order_pd_OrderTotalQry_R
[Step192]
System=DC
Line1=--訂餐數據匯總過程：
Line2=CREATE PROCEDURE  dbo.SF_Order_pd_OrderTotalQry_R
Line3=@TotalType INT,
Line4=@Dept VARCHAR(20),
Line5=@Repast VARCHAR(500),
Line6=@BDate DATETIME,
Line7=@EDate DATETIME
Line8=AS
Line9=set nocount on
Line10=Declare @str   varchar(5),@BgnDate varchar(12),@EndDate varchar(12),@SQL   varchar(1000)
Line11=Declare @i  int, @j  int
Line12=set @BgnDate=convert(varchar(10),@BDate,120)
Line13=set @EndDate=convert(varchar(10),@EDate,120)
Line14=/*創建 人員餐別表*/
Line15=IF object_ID('SF_OrderDataTotal') is not null
Line16=DROP table SF_OrderDataTotal
Line17=SELECT @SQL=
Line18=(case @TotalType
Line19=when 0 then    
Line20=/*按部門匯總*/' Create Table SF_OrderDataTotal([Dept_No] [varchar] (20) null '
Line21=when 1 then    
Line22=/*按人員匯總*/' Create Table SF_OrderDataTotal([Person_Id] [int] null '
Line23=when 2 then    
Line24=/*按食堂匯總*/' Create Table SF_OrderDataTotal([Area_No] [varchar] (10) null '
Line25=when 3 then    
Line26=/*按餐別*/' Create Table SF_OrderDataTotal([Person_Id] [int] null '
Line27=end)
Line28=set @j=1
Line29=while @j < len(@Repast)
Line30=begin
Line31=set @i=charindex(',',@Repast,@j)
Line32=if @i>@j
Line33=begin
Line34=set @str=substring(@Repast,@j,@i-@j)
Line35=set @j=@i+1
Line36=end
Line37=else
Line38=begin
Line39=set @str=substring(@Repast,@j,Len(@Repast)-@j+1)
Line40=set @j=500
Line41=end
Line42=if Len(@Str)>0
Line43=set @SQL=@SQL+' , [R'+@str+'] [int] default 0'
Line44=end
Line45=set @SQL=@SQL+')' 
Line46=Exec(@SQL)
Line47=/**收集訂餐數據**/
Line48=IF object_ID('tempdb..#TempOrderData') is not null
Line49=DROP table #TempOrderData
Line50=SELECT ST_Department.Dept_No, ST_Person.Person_No,
Line51=DC_OrderData.Person_ID, DC_OrderData.Order_date,
Line52=DC_OrderData.Repast_No, ST_MOC.Area_No, XF_Area.Area_Name
Line53=INTO  #TempOrderData
Line54=FROM ST_MOC RIGHT OUTER JOIN
Line55=DC_OrderData ON
Line56=ST_MOC.Moc_No = DC_OrderData.Moc_No_DC
Line57=Left JOIN XF_Area ON ST_MOC.Area_No = XF_Area.Area_No 
Line58=LEFT OUTER JOIN ST_Department INNER JOIN ST_Person ON ST_Department.Dept_ID = ST_Person.Dept_ID ON
Line59=DC_OrderData.Person_ID = ST_Person.Person_ID
Line60=WHERE  ST_Department.Dept_No like @Dept
Line61=AND   charindex(Repast_no, @Repast) > 0
Line62=AND   Order_date <= @EndDate
Line63=AND   Order_date >= @BgnDate
Line64=/**插入記錄**/
Line65=SELECT @SQL=
Line66=(case @TotalType
Line67=when 0 then    
Line68=/*按部門匯總*/' INSERT INTO SF_OrderDataTotal(Dept_No) '
Line69=+'  select Dept_No from #TempOrderData Group by Dept_No'
Line70=when 1 then    
Line71=/*按人員匯總*/' INSERT INTO SF_OrderDataTotal(Person_Id) '
Line72=+'  select Person_Id from #TempOrderData Group by Person_Id'
Line73=when 2 then    
Line74=/*按食堂匯總*/' INSERT INTO SF_OrderDataTotal(Area_No) '
Line75=+'  select Area_No from #TempOrderData Group by Area_No'
Line76=when 3 then    
Line77=/* 按餐別*/' INSERT INTO SF_OrderDataTotal(Person_Id) Values(0)'
Line78=end)
Line79=EXEC(@SQL)
Line80=/**匯總**/
Line81=set @j=1
Line82=while @j < len(@Repast)
Line83=begin
Line84=set @i=charindex(',',@Repast,@j)
Line85=if @i>@j
Line86=begin
Line87=set @str=substring(@Repast,@j,@i-@j)
Line88=set @j=@i+1
Line89=end
Line90=else
Line91=begin
Line92=set @str=substring(@Repast,@j,Len(@Repast)-@j+1)
Line93=set @j=500
Line94=end
Line95=if Len(@Str)>0
Line96=begin
Line97=SELECT @SQL=
Line98=(case @TotalType
Line99=when 0 then    
Line100=/*按部門匯總*/'Update SF_OrderDataTotal set R'+@str+' = '
Line101=+' (Select Count(*) from #TempOrderData '
Line102=+'  where #TempOrderData.Dept_No = SF_OrderDataTotal.Dept_No'
Line103=+'  and   #TempOrderData.Repast_No = '+char(39)+@str+char(39)
Line104=+')'
Line105=when 1 then    
Line106=/*按人員匯總*/'Update SF_OrderDataTotal set R'+@str+' = '
Line107=+' (Select Count(*) from #TempOrderData '
Line108=+'  where #TempOrderData.Person_Id = SF_OrderDataTotal.Person_Id'
Line109=+'  and   #TempOrderData.Repast_No = '+char(39)+@str+char(39)
Line110=+')'
Line111=when 2 then    
Line112=/*按食堂匯總*/'Update SF_OrderDataTotal set R'+@str+' = '
Line113=+' (Select Count(*) from #TempOrderData '
Line114=+'  where #TempOrderData.Area_No = SF_OrderDataTotal.Area_No '
Line115=+'  and   #TempOrderData.Repast_No = '+char(39)+@str+char(39)
Line116=+')'
Line117=when 3 then    
Line118=/*按餐別*/'Update SF_OrderDataTotal set R'+@str+' = '
Line119=+' (Select Count(*) from #TempOrderData '
Line120=+'  where #TempOrderData.Repast_No = '+char(39)+@str+char(39)
Line121=+')'
Line122=end)
Line123=EXEC(@SQL)
Line124=end
Line125=end
Line126=/**返回**/
Line127=SELECT @SQL=
Line128=(case @TotalType
Line129=when 0 then    
Line130=/*按部門匯總*/'Select B.Dept_Name, A.* from SF_OrderDataTotal A left join ST_Department B on A.Dept_No=B.Dept_No
Line131=ORDER BY B.Dept_No'
Line132=when 1 then    
Line133=/*按人員匯總*/'SELECT ST_Department.Dept_Name , ST_Department.Dept_No,
Line134=ST_Person.Person_Name, ST_Person.Person_No,
Line135=SF_OrderDataTotal.*
Line136=FROM ST_Department INNER JOIN
Line137=ST_Person ON
Line138=ST_Department.Dept_ID = ST_Person.Dept_ID RIGHT OUTER JOIN
Line139=SF_OrderDataTotal ON
Line140=ST_Person.Person_ID = SF_OrderDataTotal.Person_Id
Line141=ORDER BY ST_Department.dept_No,ST_Person.Person_No'
Line142=when 2 then    
Line143=/*按食堂匯總*/'Select B.Area_Name, A.* from SF_OrderDataTotal A left join XF_Area B on A.Area_No=B.Area_No'
Line144=when 3 then    
Line145=/*按餐別*/'Select * from SF_OrderDataTotal'
Line146=end)
Line147=EXEC(@SQL)
[Step193]
Line1=IF OBJECT_ID('dbo.SF_Order_pd_UnUseDetailQry_R') IS NOT NULL DROP PROCEDURE SF_Order_pd_UnUseDetailQry_R
[Step194]
System=DC
Line1=--訂餐未用餐明細
Line2=CREATE  PROCEDURE dbo.SF_Order_pd_UnUseDetailQry_R
Line3=@qryType INT,				--查詢類別
Line4=@Dept VARCHAR(20),			--部門條件或員工編號 ,根據查詢類別來確定
Line5=@Repast VARCHAR(500),			--餐別條件
Line6=@DateBgn DATETIME,			--起始日期
Line7=@DateEnd DATETIME			--結束日期
Line8=AS
Line9=Declare @BgnDate varchar(10),@EndDate varchar(10)
Line10=SET @BgnDate = CONVERT(CHAR(10),  @DateBgn, 120)
Line11=SET @EndDate = CONVERT(CHAR(10),  @DateEnd, 120)
Line12=/*訂餐數據*/
Line13=IF @qryType=1 
Line14= 	/** 按部門 + 日期 + 餐別**/
Line15=	SELECT ST_Department.Dept_Name,ST_Person.Person_No,ST_Person.Person_Name,
Line16=	O.Order_Date,O.Order_Time,O.Repast_No,R.Repast_Name,O.Moc_No_DC[Moc_No_DC] 
Line17=	FROM DC_OrderData O
Line18=	LEFT JOIN SF_Repast R On O.Repast_No=R.Repast_No
Line19=	LEFT OUTER JOIN ST_Department 
Line20=	INNER JOIN ST_Person ON ST_Department.Dept_ID = ST_Person.Dept_ID ON O.Person_ID = ST_Person.Person_ID
Line21=	WHERE Data_Type=0
Line22=	AND Dept_No Like @Dept
Line23=	AND charindex(O.Repast_no, @Repast) > 0
Line24=	AND Order_date <= @EndDate
Line25=	AND Order_date >= @BgnDate
Line26=	ORDER BY ST_Department.Dept_No,ST_Person.Person_No,O.Order_Date
Line27=ELSE  
Line28=        /** 按人員 + 日期 + 餐別**/
Line29=	SELECT ST_Department.Dept_Name,ST_Person.Person_No,ST_Person.Person_Name,
Line30=	O.Order_Date,O.Order_Time,O.Repast_No,R.Repast_Name,O.Moc_No_DC[Moc_No_DC] 
Line31=	FROM DC_OrderData O
Line32=	LEFT JOIN SF_Repast R On O.Repast_No=R.Repast_No
Line33=	LEFT OUTER JOIN ST_Department 
Line34=	INNER JOIN ST_Person ON ST_Department.Dept_ID = ST_Person.Dept_ID ON O.Person_ID = ST_Person.Person_ID
Line35=	WHERE Data_Type=0
Line36=	AND ST_Person.Person_No =@Dept
Line37=	AND charindex(O.Repast_no, @Repast) > 0
Line38=	AND Order_date <= @EndDate
Line39=	AND Order_date >= @BgnDate
Line40=	ORDER BY ST_Department.Dept_No,ST_Person.Person_No,O.Order_Date
[Step195]
Line1=IF OBJECT_ID('dbo.SF_Order_pd_UnUseTotalQry_R') IS NOT NULL DROP PROCEDURE SF_Order_pd_UnUseTotalQry_R
[Step196]
System=DC
Line1=------訂餐未用餐數據匯總查詢
Line2=CREATE PROCEDURE dbo.SF_Order_pd_UnUseTotalQry_R
Line3=@qryType INT,				--查詢類別
Line4=@Dept   VARCHAR(20),			--部門條件或員工編號 ,根據查詢類別來確定
Line5=@Repast VARCHAR(500),			--餐別條件
Line6=@Bdate DATETIME ,			--起始日期
Line7=@Edate DATETIME		                --結束日期
Line8=AS
Line9=SET NOCOUNT ON
Line10=DECLARE @str VARCHAR(5)
Line11=DECLARE @BgnDate VARCHAR(12)
Line12=DECLARE @EndDate VARCHAR(12)
Line13=DECLARE @SQL  VARCHAR(1000)
Line14=DECLARE @i  INT
Line15=DECLARE @j  INT
Line16=SET @BgnDate=CONVERT(VARCHAR(10),@BDate,120)
Line17=SET @EndDate=CONVERT(VARCHAR(10),@EDate,120)
Line18=/*創建 人員餐別表*/
Line19=IF object_ID('SF_OrderUnUseDataTotal') is not null DROP table SF_OrderUnUseDataTotal
Line20=SET @SQL=' Create Table SF_OrderUnUseDataTotal([Person_Id] [int] null'
Line21=SET @j=1
Line22=WHILE @j < LEN(@Repast)
Line23=BEGIN
Line24=	SET @i=CHARINDEX(',',@Repast,@j)
Line25=	IF @i>@j
Line26=	BEGIN
Line27=		SET @str=SUBSTRING(@Repast,@j,@i-@j)
Line28=		SET @j=@i+1
Line29=	END
Line30=	ELSE
Line31=	BEGIN
Line32=		SET @str=SUBSTRING(@Repast,@j,LEN(@Repast)-@j+1)
Line33=		SET @j=500
Line34=	END
Line35=	IF LEN(@Str)>0
Line36=	SET @SQL=@SQL+' , [R'+@str+'] [INT] DEFAULT 0'
Line37=END
Line38=SET @SQL=@SQL+')'
Line39=EXEC(@SQL)
Line40=/**插入訂餐未用餐人員***/
Line41=IF @qryType=0
Line42=INSERT INTO SF_OrderUnUseDataTotal([Person_Id])
Line43=SELECT DC_OrderData.Person_Id
Line44=FROM ST_Department INNER JOIN
Line45=ST_Person ON
Line46=ST_Department.Dept_ID = ST_Person.Dept_ID RIGHT OUTER JOIN
Line47=DC_OrderData ON
Line48=ST_Person.Person_ID = DC_OrderData.Person_Id
Line49=WHERE DC_OrderData.Data_type=0 --已訂未用餐
Line50=and ST_Department.Dept_No like @Dept
Line51=and CHARINDEX(Repast_No,@Repast)>0
Line52=and Order_Date <= @EndDate
Line53=and Order_Date >= @BgnDate
Line54=GROUP BY DC_OrderData.Person_Id
Line55=ELSE
Line56=INSERT INTO SF_OrderUnUseDataTotal([Person_Id]) Values(0)
Line57=/****匯總******/
Line58=SET @j=1
Line59=WHILE @j < len(@Repast)
Line60=BEGIN
Line61=	SET @i=CHARINDEX(',',@Repast,@j)
Line62=	IF @i>@j
Line63=	BEGIN
Line64=		SET @str=SUBSTRING(@Repast,@j,@i-@j)
Line65=		SET @j=@i+1
Line66=	END
Line67=	ELSE
Line68=	BEGIN
Line69=		SET @str=SUBSTRING(@Repast,@j,LEN(@Repast)-@j+1)
Line70=		SET @j=500
Line71=	END
Line72=	IF Len(@Str)>0
Line73=	BEGIN
Line74=		IF @qryType=0
Line75=		SET @SQL='Update SF_OrderUnUseDataTotal set R'+@str+' = '
Line76=		+' (Select Count(*) from DC_OrderData '
Line77=		+'  where DC_OrderData.Data_Type=0 and DC_OrderData.Repast_No = '    + CHAR(39) + @str + CHAR(39)
Line78=		+'  and   DC_OrderData.Order_Date <= '  + CHAR(39) + @EndDate + CHAR(39)
Line79=		+'  and   DC_OrderData.Order_Date >= '  + CHAR(39) + @BgnDate + CHAR(39)
Line80=		+'  and   DC_OrderData.Person_Id = SF_OrderUnUseDataTotal.Person_Id '
Line81=		+' )'
Line82=		IF @qryType=1
Line83=		SET @SQL='Update SF_OrderUnUseDataTotal set R'+@str+' = '
Line84=		+' (Select Count(*) FROM ST_Department INNER JOIN
Line85=		ST_Person ON
Line86=		ST_Department.Dept_ID = ST_Person.Dept_ID RIGHT OUTER JOIN
Line87=		DC_OrderData ON
Line88=		ST_Person.Person_ID = DC_OrderData.Person_Id'
Line89=		+'  where DC_OrderData.Data_Type=0 and ST_Department.Dept_No like '     + CHAR(39) + @Dept + CHAR(39)
Line90=		+'  and   DC_OrderData.Repast_No = '  + CHAR(39) + @str + CHAR(39)
Line91=		+'  and   DC_OrderData.Order_Date <= '+ CHAR(39) + @EndDate + CHAR(39)
Line92=		+'  and   DC_OrderData.Order_Date >= '+ CHAR(39) + @BgnDate + CHAR(39)
Line93=		+' )'
Line94=		+' where Person_Id = 0 '
Line95=		EXEC(@SQL)
Line96=	END
Line97=END
Line98=SELECT ST_Department.Dept_Name, ST_Department.Dept_No,
Line99=       	ST_Person.Person_Name, ST_Person.Person_No,SF_OrderUnUseDataTotal.*
Line100=	FROM ST_Department
Line101=	INNER JOIN ST_Person ON  ST_Department.Dept_ID = ST_Person.Dept_ID 
Line102=	RIGHT OUTER JOIN SF_OrderUnUseDataTotal ON ST_Person.Person_ID = SF_OrderUnUseDataTotal.Person_Id
Line103=	ORDER BY ST_Department.dept_No,ST_Person.Person_No
[Step197]
Line1=IF OBJECT_ID('XF_pd_CardOrder') IS NOT NULL
Line2=   DROP PROCEDURE XF_pd_CardOrder
[Step198]
System=DC
Line1=--2009.11.5 實時訂餐系統增加
Line2=CREATE PROCEDURE dbo.XF_pd_CardOrder
Line3=   (@OrderDate   DATETIME,    --訂餐日期
Line4=    @PersonID    INT,         --人員標識號
Line5=    @CardNo      VARCHAR(20), --卡號
Line6=    @SystemNo    VARCHAR(20), --子系統編號
Line7=    @ConsumeMode TINYINT,     --消費模式
Line8=    @ConsumeTime DATETIME,    --消費時間
Line9=    @Balance     MONEY,       --卡余額(本次消費后的余額)
Line10=    @DBBalance   MONEY,       --庫余額(本次消費后的余額)
Line11=    @BeforeFund  MONEY,       --打折前的消費金額
Line12=    @ConsumeFund MONEY,       --打折后的消費金額
Line13=    @FundOld     MONEY,       --原消費金額
Line14=    @Copies      INT,         --消費份數
Line15=    @Quantity    INT,         --消費數量(此過程沒用)
Line16=    @TimeLen     INT,         --消費時長(此過程沒用)
Line17=    @RepastNo    VARCHAR(20), --餐別編號
Line18=    @WareID      VARCHAR(20), --商品編號(此過程沒用)
Line19=    @MocNo       VARCHAR(20), --終端機編號
Line20=    @HardNo      VARCHAR(20), --硬件編號
Line21=    @IDKey       INT,         --糾錯記錄的ID_KEY(此過程沒用)
Line22=    @Options     TINYINT,     --記錄選項(0表示消費,1表示糾錯,2表示脫機,3表示有卡無人的脫機記錄,
Line23=                              --        4表示聯機假寫卡記錄,5表示脫機糾錯記錄,6表示脫機假寫卡記錄)
Line24=    @SubsidyFund MONEY        --本次發放補貼金額(此過程沒用)
Line25=   )
Line26=AS   
Line27=   --0.更新IC卡余額
Line28=   UPDATE ST_Card 
Line29=      SET Update_Time=@ConsumeTime,IC_Balance=@Balance
Line30=    WHERE Person_ID=@PersonID AND Update_Time<=@ConsumeTime AND DATEDIFF(day,Update_Time,@ConsumeTime)<90
Line31=   --Update begin time
Line32=   UPDATE ST_SysParam SET Param_Value=CONVERT(VARCHAR(50), @ConsumeTime, 121)
Line33=    WHERE Param_Code='Fake.BeginTime' AND CAST(Param_Value AS DATETIME)>@ConsumeTime
Line34=   --
Line35=   UPDATE ST_SysParam SET Param_Value=CONVERT(VARCHAR(50), @ConsumeTime, 121)
Line36=    WHERE Param_Code='Repeat.BeginTime' AND CAST(Param_Value AS DATETIME)>@ConsumeTime
Line37=   --
Line38=   DECLARE @DataType TINYINT   
Line39=   IF @Options <> 3 --正常消費或假寫卡或脫機消費
Line40=   BEGIN
Line41=      IF (@Options = 0) OR (@Options = 4) SET @ConsumeTime=GETDATE() --非脫機記錄取數據庫的當前時間
Line42=      IF (@Options = 0) OR (@Options = 2) SET @DataType=0 --聯機消費或脫機消費
Line43=      ELSE SET @DataType=8 --假寫卡
Line44=      IF @DataType=8 RETURN --假寫卡記錄,暫時不處理    
Line45=      DECLARE @DownloadTime DATETIME,@IDKEYDC INT,@OrderTime DATETIME,@DataTypeDC INT
Line46=      IF (@Options = 5) OR (@Options = 2) --脫機記錄
Line47=         SET @DownloadTime=GETDATE()
Line48=      --1.更新余額和補貼金額
Line49=      UPDATE ST_Card 
Line50=         SET @DBBalance=Card_Balance-@ConsumeFund,Card_Balance=@DBBalance
Line51=       WHERE Person_ID=@PersonID
Line52=      --處理已用餐但未訂餐的情況
Line53=      IF @DownloadTime IS NOT NULL --脫機情況
Line54=      BEGIN
Line55=         --判斷訂餐記錄是否存在
Line56=         SELECT @IDKEYDC=ID_KEY,@OrderTime=Consume_Time,@DataTypeDC=Data_Type FROM DC_OrderData 
Line57=          WHERE Person_ID=@PersonID AND Order_Date=CONVERT(VARCHAR(10),@OrderDate,121)
Line58=            AND Repast_No=@RepastNo
Line59=         --訂餐記錄如果存在就更新信息 
Line60=         IF @IDKEYDC IS NOT NULL
Line61=         BEGIN
Line62=            IF @DataTypeDC<>2 RETURN --如果是重復的訂餐記錄就不處理
Line63=            --1.更新訂餐表
Line64=            UPDATE DC_OrderData SET Order_Time=@ConsumeTime,Moc_No_DC=@MocNo,Repast_No=@RepastNo,Data_Type=1
Line65=             WHERE ID_KEY=@IDKEYDC
Line66=            --2.更新消費臨時表中的記錄
Line67=            UPDATE XF_ConsumeTemp 
Line68=               SET @IDKEYDC=Is_Commited, 
Line69=                   Consume_Fund=@ConsumeFund,Before_Fund=@BeforeFund,Card_Balance=@Balance,
Line70=                   DB_Balance=@DBBalance,Consume_Copy=@Copies 
Line71=             WHERE Person_ID=@PersonID AND Consume_Time=@OrderTime
Line72=            --3.更新消費表中的記錄
Line73=            IF @IDKEYDC<>0 --當前消費記錄已經提交時,需要再次更新消費表
Line74=            BEGIN
Line75=               UPDATE XF_Consume 
Line76=                  SET Consume_Fund=@ConsumeFund,Before_Fund=@BeforeFund,Card_Balance=@Balance,
Line77=                      DB_Balance=@DBBalance,Consume_Copy=@Copies 
Line78=                WHERE Person_ID=@PersonID AND Consume_Time=@OrderTime
Line79=            END
Line80=            RETURN --訂餐記錄已存在就不用插入新的記錄 
Line81=         END
Line82=      END
Line83=      --結束
Line84=      --2.插入消費記錄
Line85=      INSERT INTO XF_ConsumeTemp(Person_ID,System_No,Consume_Mode,Consume_Time,Before_Fund,Consume_Fund,Consume_Copy,
Line86=                                 Consume_TimeLen,Consume_Capacity,Repast_No,Ware_ID,Moc_No,COM_No,Card_Balance,
Line87=                                 DB_Balance,Card_SmallAccount,Data_Type,Is_Commited,Download_Time)
Line88=      SELECT @PersonID,@SystemNo,@ConsumeMode,@ConsumeTime,@BeforeFund,@ConsumeFund,@Copies,0,0,@RepastNo,
Line89=             -1,@MocNo,@HardNo,@Balance,@DBBalance,0,@DataType,0,@DownloadTime
Line90=      --3.插入訂餐記錄
Line91=      INSERT INTO DC_OrderData(Person_ID,Order_Date,Order_Time,Repast_No,Moc_No_DC,Data_Type,Download_Time)
Line92=      SELECT @PersonID,CONVERT(VARCHAR(10),@OrderDate,121),@ConsumeTime,@RepastNo,@MocNo,0,@DownloadTime
Line93=   END
Line94=   ELSE
Line95=   BEGIN
Line96=      SET @DBBalance=0
Line97=      --1.插入有卡無人消費記錄
Line98=      INSERT INTO ST_NoPersonCard(Card_No,System_No,Consume_Mode,Consume_Time,Consume_Fund,Consume_Copy,
Line99=                                  Consume_TimeLen,Consume_Capacity,Repast_No,Ware_ID,Moc_No,COM_No,Card_Balance,
Line100=                                  DB_Balance,Card_SmallAccount,Data_Type,Download_Date)
Line101=      SELECT @CardNo,@SystemNo,@ConsumeMode,@ConsumeTime,@ConsumeFund,@Copies,0,0,@RepastNo,
Line102=             -1,@MocNo,@HardNo,@DBBalance,@Balance,0,0,GETDATE()
Line103=   END
[Step199]
Line1=IF OBJECT_ID('XF_pd_CardUseMeal') IS NOT NULL
Line2=   DROP PROCEDURE XF_pd_CardUseMeal
[Step200]
System=DC
Line1=CREATE PROCEDURE dbo.XF_pd_CardUseMeal
Line2=   (@IsCheckOrder BIT,         --是否需要檢查訂餐表
Line3=    @PersonID     INT,         --人員標識號
Line4=    @CardNo       VARCHAR(20), --卡號
Line5=    @SystemNo     VARCHAR(20), --子系統編號
Line6=    @ConsumeMode  TINYINT,     --消費模式
Line7=    @ConsumeTime  DATETIME,    --消費時間
Line8=    @Balance      MONEY,       --卡余額(本次消費后的余額)
Line9=    @DBBalance    MONEY,       --庫余額(本次消費后的余額)
Line10=    @BeforeFund   MONEY,       --打折前的消費金額(此過程沒用)
Line11=    @ConsumeFund  MONEY,       --打折后的消費金額(此過程沒用)
Line12=    @FundOld      MONEY,       --原消費金額(此過程沒用)
Line13=    @Copies       INT,         --消費份數(此過程沒用)
Line14=    @Quantity     INT,         --消費數量(此過程沒用)
Line15=    @TimeLen      INT,         --消費時長(此過程沒用)
Line16=    @RepastNo     VARCHAR(20), --餐別編號
Line17=    @WareID       VARCHAR(20), --商品編號(此過程沒用)
Line18=    @MocNo        VARCHAR(20), --終端機編號
Line19=    @HardNo       VARCHAR(20), --硬件編號
Line20=    @IDKey        INT,         --糾錯記錄的ID_KEY(此過程沒用)
Line21=    @Options      TINYINT,     --記錄選項(0表示消費,1表示糾錯,2表示脫機,3表示有卡無人的脫機記錄,
Line22=                               --        4表示聯機假寫卡記錄,5表示脫機糾錯記錄,6表示脫機假寫卡記錄)
Line23=    @SubsidyFund  MONEY        --本次發放補貼金額(此過程沒用)
Line24=   )
Line25=AS      
Line26=   IF @Options = 3 RETURN --不處理有卡無人的情況
Line27=   DECLARE @DataType TINYINT,@DownloadTime DATETIME,@OrderTime DATETIME,@IDKEYDC INT,@DataTypeDC INT   
Line28=   --正常消費或假寫卡或脫機消費   
Line29=   IF (@Options = 0) OR (@Options = 4) SET @ConsumeTime=GETDATE() --非脫機記錄取數據庫的當前時間
Line30=   IF (@Options = 0) OR (@Options = 2) SET @DataType=0 --聯機消費或脫機消費
Line31=   ELSE SET @DataType=8 --假寫卡
Line32=   IF @DataType=8 RETURN --不處理假寫卡的情況
Line33=   --非假寫卡記錄   
Line34=   IF (@Options = 5) OR (@Options = 2) --脫機記錄
Line35=      SET @DownloadTime=GETDATE()
Line36=   SELECT @OrderTime=Order_Time,@IDKEYDC=ID_KEY,@DataTypeDC=Data_Type FROM DC_OrderData 
Line37=    WHERE Person_ID=@PersonID AND Order_Date=CONVERT(VARCHAR(10),@ConsumeTime,121)
Line38=      AND Repast_No=@RepastNo
Line39=   --有訂餐記錄存在
Line40=   IF @OrderTime IS NOT NULL
Line41=   BEGIN
Line42=      IF @DataTypeDC<>0 RETURN --如果是重復的訂餐消費記錄就不處理
Line43=      --1.更新訂餐表
Line44=      UPDATE DC_OrderData SET Consume_Time=@ConsumeTime,Moc_No_SF=@MocNo,Data_Type=1
Line45=       WHERE ID_KEY=@IDKEYDC
Line46=      --2.更新消費臨時表中的記錄
Line47=      UPDATE XF_ConsumeTemp 
Line48=         SET @IDKEYDC=Is_Commited, 
Line49=             Consume_Time=@ConsumeTime,Moc_No=@MocNo,Download_Time=@DownloadTime,COM_No=@HardNo         
Line50=       WHERE Person_ID=@PersonID AND Consume_Time=@OrderTime
Line51=      --3.更新消費表中的記錄
Line52=      IF @IDKEYDC<>0 --當前消費記錄已經提交時,需要再次更新消費表
Line53=      BEGIN
Line54=         UPDATE XF_Consume 
Line55=            SET Consume_Time=@ConsumeTime,Moc_No=@MocNo,Download_Time=@DownloadTime,COM_No=@HardNo
Line56=          WHERE Person_ID=@PersonID AND Consume_Time=@OrderTime
Line57=      END
Line58=   END
Line59=   ELSE IF @IsCheckOrder=1 --有用餐記錄沒有訂餐記錄需要補訂餐記錄
Line60=   BEGIN
Line61=      --1.插入消費記錄
Line62=      INSERT INTO XF_ConsumeTemp(Person_ID,System_No,Consume_Mode,Consume_Time,Before_Fund,Consume_Fund,Consume_Copy,
Line63=                                 Consume_TimeLen,Consume_Capacity,Repast_No,Ware_ID,Moc_No,COM_No,Card_Balance,
Line64=                                 DB_Balance,Card_SmallAccount,Data_Type,Is_Commited,Download_Time)
Line65=      SELECT @PersonID,@SystemNo,@ConsumeMode,@ConsumeTime,0,0,0,0,0,@RepastNo,
Line66=             -1,@MocNo,@HardNo,@Balance,@DBBalance,0,@DataType,0,@DownloadTime
Line67=      --2.插入訂餐記錄
Line68=      INSERT INTO DC_OrderData(Person_ID,Order_Date,Order_Time,Repast_No,Moc_No_DC,Data_Type,Download_Time,Consume_Time,Moc_No_SF)
Line69=      SELECT @PersonID,CONVERT(VARCHAR(10),@ConsumeTime,121),@ConsumeTime,@RepastNo,@MocNo,2,@DownloadTime,@ConsumeTime,@MocNo
Line70=   END
[Step201]
Line1=IF OBJECT_ID('SF_fn_HasOrderData') IS NOT NULL
Line2=  DROP FUNCTION SF_fn_HasOrderData 
[Step202]
System=DC
Line1=CREATE FUNCTION dbo.SF_fn_HasOrderData 
Line2=	(@PersonID INT, 
Line3=    @RepastNo VARCHAR(20),
Line4=    @Date DATETIME)
Line5=RETURNS BIT
Line6=AS
Line7=BEGIN
Line8=   DECLARE @Return BIT,@CurDate VARCHAR(10)
Line9=   SET @CurDate=CONVERT(VARCHAR(10),@Date,121)   
Line10=   IF EXISTS(SELECT Person_ID FROM DC_OrderData 
Line11=              WHERE Person_ID=@PersonID AND Order_Date=@CurDate 
Line12=                AND Repast_No=@RepastNo AND Data_Type<>1
Line13=            )      
Line14=      SET @Return = 1
Line15=   ELSE
Line16=      SET @Return = 0
Line17=   RETURN @Return
Line18=END
[Step203]
Line1=IF OBJECT_ID('XF_pd_CancelOrder') IS NOT NULL
Line2=   DROP PROCEDURE XF_pd_CancelOrder
[Step204]
System=DC
Line1=--撤銷訂餐
Line2=CREATE  PROCEDURE dbo.XF_pd_CancelOrder
Line3=   (@OrderDate   DATETIME,    --訂餐日期
Line4=    @PersonID    INT,         --人員標識號
Line5=    @CardNo      VARCHAR(20), --卡號
Line6=    @SystemNo    VARCHAR(20), --子系統編號
Line7=    @Balance     MONEY,       --卡余額(本次消費后的余額)
Line8=    @DBBalance   MONEY,       --庫余額(本次消費后的余額)
Line9=    @BeforeFund  MONEY,       --打折前的消費金額
Line10=    @ConsumeFund MONEY,       --打折后的消費金額
Line11=    @Copies      INT,         --消費份數
Line12=    @RepastNo    VARCHAR(20), --餐別編號
Line13=    @HardNo      VARCHAR(20), --硬件編號
Line14=    @MocNo       VARCHAR(20), --終端機編號
Line15=    @UserName    Varchar(30)  --操作人員
Line16=   )
Line17=AS   
Line18=   DECLARE @Is_Commited int,@IDKEYDC INT
Line19=   --0.更新IC卡余額
Line20=   UPDATE ST_Card SET Update_Time=GETDATE(),IC_Balance=@Balance,Card_Balance=@Balance WHERE Person_ID=@PersonID 
Line21=   --1.插入訂餐日志記錄
Line22=     INSERT INTO DC_OrderDataLog(Person_ID,Order_Date,Order_Time,Repast_No,Moc_No_DC,
Line23=				Data_Type,Download_Time,Consume_Time,Moc_No_SF,Remark,
Line24=				Card_Balance,DB_Balance,Consume_Fund,Del_Date,User_Name
Line25=                           ) 
Line26=	SELECT Person_ID,Order_Date,Order_Time,Repast_No,Moc_No_DC,
Line27=	       Data_Type,Download_Time,Consume_Time,Moc_No_SF,Remark,
Line28=	       @Balance,@DBBalance,@ConsumeFund,GETDATE(),@UserName 
Line29=              FROM DC_OrderData 
Line30=	      WHERE Person_ID=@PersonID AND Order_Date=@OrderDate AND Repast_No=@RepastNo      
Line31=   --2.刪除訂餐記錄
Line32=     DELETE DC_OrderData WHERE Person_ID=@PersonID AND Order_Date=@OrderDate 
Line33=            AND Repast_No=@RepastNo AND Data_Type=0
Line34=    SELECT TOP 1 @IDKEYDC=ID_KEY,@Is_Commited=Is_Commited FROM XF_ConsumeTemp 
Line35=        WHERE Person_ID=@PersonID AND CONVERT(VARCHAR(10), Consume_Time, 121)=@OrderDate AND Repast_No=@RepastNo 
Line36=    --訂餐記錄如果存在就更新信息 
Line37=    IF @Is_Commited IS NOT NULL
Line38=    BEGIN
Line39=      --3.插入消費記錄
Line40=       DELETE XF_ConsumeTemp WHERE ID_KEY=@IDKEYDC
Line41=       IF @Is_Commited = 1 
Line42=       BEGIN
Line43=         --4.插入消費記錄
Line44=         DELETE XF_Consume WHERE Person_ID=@PersonID AND CONVERT(VARCHAR(10), Consume_Time, 121)=@OrderDate AND Repast_No=@RepastNo 
Line45=       END
Line46=    END  
[Step205]
System=DC
Line1=/*2009.11.5 實時訂餐系統增加*/
Line2=IF OBJECT_ID('DC_Repast') IS NULL
Line3=   CREATE TABLE DC_Repast(
Line4=      Repast_No  VARCHAR(3) NOT NULL,
Line5=      Bgn_Time   VARCHAR(5) NOT NULL,
Line6=      End_Time   VARCHAR(5) NOT NULL,
Line7=      Order_Days TINYINT NOT NULL DEFAULT 0,
Line8=      Can_Order  BIT NOT NULL DEFAULT 1,
Line9=      PRIMARY KEY (Repast_No ASC)
Line10=   )
[Step206]
System=DC
Line1=IF OBJECT_ID('DC_OrderData') IS NULL
Line2=   CREATE TABLE DC_OrderData (
Line3=      ID_KEY        INT IDENTITY(1,1) NOT NULL,
Line4=      Person_ID     INT NOT NULL,
Line5=      Order_Date    VARCHAR(10) NOT NULL,
Line6=      Order_Time    DATETIME NOT NULL,
Line7=      Repast_No     VARCHAR(3) NOT NULL,
Line8=      Moc_No_DC     VARCHAR(6) NOT NULL,
Line9=      Data_Type     TINYINT NOT NULL DEFAULT 0,
Line10=      Download_Time DATETIME NULL,
Line11=      Consume_Time  DATETIME NULL, 
Line12=      Moc_No_SF     VARCHAR(6) NULL,
Line13=      Remark        VARCHAR(40) NULL,
Line14=      PRIMARY KEY (ID_KEY ASC)
Line15=   ) 
[Step207]
System=DC
Line1=IF NOT EXISTS (SELECT name FROM sysindexes WHERE name = 'IX_DC_OrderData')  
Line2=   CREATE NONCLUSTERED INDEX IX_DC_OrderData ON dbo.DC_OrderData
Line3=	   (
Line4=	   Person_ID,
Line5=	   Order_Date,
Line6=	   Repast_No
Line7=	   ) ON [PRIMARY]
[Step208]
System=DC
Line1=IF OBJECT_ID('DC_OrderDataLog') IS NULL
Line2=   CREATE TABLE DC_OrderDataLog (
Line3=      ID_KEY        INT IDENTITY(1,1) NOT NULL,
Line4=      Person_ID     INT NOT NULL,
Line5=      Order_Date    VARCHAR(10) NOT NULL,
Line6=      Order_Time    DATETIME NOT NULL,
Line7=      Repast_No     VARCHAR(3) NOT NULL,
Line8=      Moc_No_DC     VARCHAR(6) NOT NULL,
Line9=      Data_Type     TINYINT NOT NULL DEFAULT 0,
Line10=      Download_Time DATETIME NULL,
Line11=      Consume_Time  DATETIME NULL, 
Line12=      Moc_No_SF     VARCHAR(6) NULL,
Line13=      Remark        VARCHAR(40) NULL,
Line14=      Card_Balance  MONEY NOT NULL,
Line15=      DB_Balance    MONEY NOT NULL,
Line16=      Consume_Fund  MONEY NOT NULL,
Line17=      Del_Date	    DATETIME NOT NULL,
Line18=      User_Name     VARCHAR(20) NOT NULL,
Line19=      PRIMARY KEY (ID_KEY ASC)
Line20=   ) 
[Step209]
System=DC
Line1=--初始化實時訂餐的菜單
Line2=INSERT INTO dbo.ST_MenuInfo(Menu_ID,System_No,Menu_No,Menu_Name,Menu_Hint,Para_ID,Call_Mode,Form_Name,ShortKey,
Line3=                        Menu_Index,Image_Index,Separator,InToolBar,Remark)
Line4=SELECT * FROM (SELECT 600[Menu_ID],'DC'[System_No],'DC_10'[Menu_No],'系統管理'[Menu_Name],NULL[Menu_Hint],-1[Para_ID],NULL[Call_Mode],
Line5=                      NULL[Form_Name],NULL[ShortKey],1[Menu_Index],NULL[Image_Index],NULL[Separator],0[InToolBar],NULL[Remark]
Line6=                UNION ALL SELECT 601,'DC','DC_1001','系統參數設置','系統參數設置',600,2,'TfrmSysparamSet_R',NULL,1,65,1,0,NULL
Line7=                UNION ALL SELECT 602,'DC','DC_1002','訂餐機設置','訂餐機設置',600,2,'TfrmMocSetDC_R',NULL,2,65,0,0,NULL
Line8=                UNION ALL SELECT 603,'DC','DC_1003','訂餐時間設置','訂餐時間設置',600,2,'TfrmRepast_R',NULL,3,66,1,0,NULL
Line9=                UNION ALL SELECT 604,'DC','DC_1004','退出系統','退出系統',600,5,'Exit',NULL,4,0,0,0,NULL
Line10=                UNION ALL SELECT 605,'DC','DC_11','訂餐管理',NULL,-1,NULL,NULL,NULL,2,-1,NULL,0,NULL
Line11=                UNION ALL SELECT 606,'DC','DC_1101','訂餐機控制台','訂餐機控制台',605,1,'TfrmMocStateDC',NULL,1,67,1,0,NULL
Line12=                UNION ALL SELECT 607,'DC','DC_1102','手工訂餐','手工訂餐',605,1,'TfrmSelfOrder_R',NULL,2,68,'0',0,NULL
Line13=                UNION ALL SELECT 608,'DC','DC_1103','撤消訂餐','撤消訂餐',605,1,'TfrmDeleteOrder_R',NULL,3,69,'0',0,NULL
Line14=                UNION ALL SELECT 609,'DC','DC_12','訂餐報表',NULL,-1,NULL,NULL,NULL,3,-1,NULL,0,NULL
Line15=                UNION ALL SELECT 610,'DC','DC_1201','訂餐數據明細','訂餐數據明細報表',609,1,'TfrmOrderData_R',NULL,1,70,0,0,NULL
Line16=                UNION ALL SELECT 611,'DC','DC_1202','訂餐數據匯總','訂餐數據匯總報表',609,1,'TfrmOrderTotal_R',NULL,2,71,1,0,NULL
Line17=                UNION ALL SELECT 612,'DC','DC_1203','未用餐明細','未用餐明細報表',609,1,'TfrmUnuseData_R',NULL,3,-1,0,0,NULL
Line18=                UNION ALL SELECT 613,'DC','DC_1204','未用餐匯總','未用餐匯總報表',609,1,'TfrmUnuseTotal_R',NULL,4,-1,0,0,NULL
Line19=                UNION ALL SELECT 614,'DC','DC_13','系統維護',NULL,-1,NULL,NULL,NULL,4,-1,NULL,0,NULL
Line20=                UNION ALL SELECT 615,'DC','DC_1301','過期數據清理','過期數據清理',614,2,'TfrmClearData',NULL,1,-1,0,0,NULL
Line21=                UNION ALL SELECT 599,'DC','DC_0100','系統參數設置','系統參數設置',175,2,'TfrmSysparamSet_R',NULL,0,-1,1,0,NULL
Line22=              ) A
Line23= WHERE A.Menu_ID NOT IN (SELECT Menu_ID FROM dbo.ST_MenuInfo)
[Step210]
System=DC
Line1=--2.增加系統權限值列表
Line2=--2.1增加每個模塊的默認權限值
Line3=--3.增加角色權限默認管理員組
Line4=IF NOT EXISTS(SELECT Param_Code FROM ST_SysParam WHERE Param_Code='DC.Solution')   
Line5=   INSERT INTO ST_SysParam(Param_Code,Param_Name,Param_Value)
Line6=   SELECT 'DC.Solution','DC.Solution','0'
[Step211]
System=DC
Line1=IF NOT EXISTS(SELECT Param_Code FROM ST_SysParam WHERE Param_Code='DC.IsUseMOCAsMDC')   
Line2=   INSERT INTO ST_SysParam(Param_Code,Param_Name,Param_Value)
Line3=   SELECT 'DC.IsUseMOCAsMDC','DC.IsUseMOCAsMDC','0'
[Step212]
Line1=IF COL_LENGTH('ST_CardInfo', 'Card_Spec') IS NULL
Line2=   ALTER TABLE ST_CardInfo ADD Card_Spec SMALLINT NOT NULL DEFAULT 0
[Step213]
Line1=IF COL_LENGTH('ST_CardInfo', 'Init_Code') IS NULL
Line2=   ALTER TABLE ST_CardInfo ADD Init_Code VARCHAR(100)
[Step214]
System=KQ
Line1=/*下載表增加 圖片文件名 字段*/ 
Line2=IF COL_LENGTH('KQ_Download','FileName') IS NULL
Line3=  ALTER TABLE KQ_Download ADD FileName VARCHAR(128)
[Step215]
System=KQ
Line1=/*下載表增加 異常標志 字段*/ 
Line2=IF COL_LENGTH('KQ_Download','Is_Falsity') IS NULL
Line3=  ALTER TABLE KQ_Download ADD Is_Falsity BIT DEFAULT 0
[Step216]
Line1=IF EXISTS(SELECT name FROM sysobjects WHERE name = 'SQLPageData' AND type = 'P')
Line2=  DROP PROCEDURE dbo.SQLPageData
[Step217]
System=KQ
Line1=--1.考勤子系統初始菜單表
Line2=INSERT INTO dbo.ST_MenuInfo(Menu_ID,System_No,Menu_No,Menu_Name,Menu_Hint,Para_ID,Call_Mode,Form_Name,ShortKey,
Line3=                        Menu_Index,Image_Index,Separator,InToolBar,Remark)
Line4=SELECT * FROM (SELECT 50[Menu_ID],'KQ'[System_No],'KQ_0101'[Menu_No],'賬套管理'[Menu_Name],NULL[Menu_Hint],79[Para_ID],2[Call_Mode],
Line5=                      'TfrmAccountMana'[Form_Name],NULL[ShortKey],1[Menu_Index],33[Image_Index],1[Separator],0[InToolBar],NULL[Remark]
Line6=			UNION ALL SELECT 79,'KQ','KQ_01','系統管理',NULL,-1,NULL,NULL,NULL,'1',NULL,NULL,0,NULL
Line7=			UNION ALL SELECT 700,'KQ','KQ_07','拍照考勤機',NULL,-1,'1',NULL,NULL,'3',-1,'0',0,NULL
Line8=			UNION ALL SELECT 701,'KQ','KQ_0701','照片對比','照片對比',700,'1','TfrmPhotoContrast',NULL,'1',39,'0',0,NULL
Line9=			UNION ALL SELECT 702,'KQ','KQ_0702','實時顯示','實時顯示',700,'2','TfrmSetRealInfoKQ',NULL,'2',40,'0',0,NULL
Line10=			UNION ALL SELECT 703,'KQ','KQ_0703','配置參數','配置參數',700,'2','TfrmSetHardInfoKQ',NULL,'2',40,'0',0,NULL
Line11=							) A
Line12= WHERE A.Menu_ID NOT IN (SELECT Menu_ID FROM dbo.ST_MenuInfo)
[Step218]
System=KQ
Line1=/**/
Line2=/*數據分頁存儲過程*/
Line3=/**/
Line4=Create PROCEDURE SQLPageData
Line5=  @ASql          VARCHAR(8000),
Line6=  @APageNo       INT, --頁號
Line7=  @APageSize     INT, --每頁記錄數
Line8=  @AOnceMoreSize INT, --每頁多取下頁的前N條
Line9=  @APageCount    INT OUTPUT,
Line10=  @ARecordCount  INT OUTPUT
Line11=AS
Line12=BEGIN
Line13=  DECLARE @t int
Line14=  EXEC(@ASql)  
Line15=  SELECT @ARecordCount = @@rowcount
Line16=  IF (@ARecordCount <= @APageSize)
Line17=    SELECT @APageCount=1
Line18=  ELSE 
Line19=     SELECT @APageCount=CEILING((@ARecordCount - @AOnceMoreSize)/(@APageSize*1.0-@AOnceMoreSize))
Line20=  IF (@APageNo < 0)  SELECT @APageNo = @APageCount + @APageNo + 1
Line21=  SELECT @t = CASE  @APageNo
Line22=    WHEN 0   THEN @APageSize * -1
Line23=    WHEN  1  THEN 0
Line24=    ELSE (@APageNo-1)*(@APageSize-@AOnceMoreSize)
Line25=    END 
Line26=  SELECT @ASql= @ASql + ' SELECT * FROM #tmpData WHERE ID  BETWEEN ' + CONVERT(VARCHAR(10),@t+1) +
Line27=                        ' AND ' + CONVERT(VARCHAR(10), @t+@APageSize) + ' ORDER BY ID ' +
Line28='                        ' DROP TABLE #tmpData''
Line29=  EXEC(@ASql)  
Line30= END
Line31=SET QUOTED_IDENTIFIER OFF
[Step219]
System=KQ
Line1=--創建數據庫
Line2=IF NOT EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name = N'STPhoto')
Line3=  CREATE DATABASE [STPhoto]  
[Step220]
System=KQ
Line1=/*刷卡圖片表*/ 
Line2=IF OBJECT_ID('STPHOTO.dbo.ST_Photo') IS NULL
Line3=   CREATE TABLE STPHOTO.dbo.ST_Photo (
Line4=          FileName  varchar(128) NULL,
Line5=          Photo     image NULL
Line6=   )
[Step221]
Line1=IF NOT EXISTS(SELECT Param_Code FROM ST_SysParam WHERE Param_Code='IsShowTakeAll')
Line2=   INSERT INTO ST_SysParam(Param_Code,Param_Name,Param_Value)
Line3=   SELECT 'IsShowTakeAll','顯示“取出全部人員的卡余額”','1'

[Step222]
Line1=IF OBJECT_ID('XF_pd_GetCardTypeRepast') IS NOT NULL
Line2=   DROP PROCEDURE XF_pd_GetCardTypeRepast 
[Step223]
Line1=CREATE  Procedure XF_pd_GetCardTypeRepast 
Line2=(
Line3=@ConsumeTimeEnd VarChar(20),
Line4=@ConsumeTimeBng VarChar(20)
Line5=)
Line6=AS
Line7=DECLARE @SQL VARCHAR(2000),
Line8=        @RepastNo VARCHAR(10)
Line9=DECLARE repast_cursor CURSOR FOR
Line10=--
Line11=SELECT Repast_No FROM SF_Repast
Line12=OPEN repast_cursor
Line13=--
Line14=FETCH NEXT FROM repast_cursor
Line15=INTO @RepastNo
Line16=--
Line17=    SET @SQL='SELECT C.Type_No,[Type_Name],'
Line18=    WHILE @@FETCH_STATUS = 0
Line19=    BEGIN
Line20=        SET @SQL=@SQL+'SUM(CASE WHEN Repast_No='''+ @RepastNo + '''' +
Line21=        ' THEN CountAll ELSE 0 END) [Count' + @RepastNo +'],' +
Line22=        'SUM(CASE WHEN Repast_No=''' + @RepastNo + '''' +
Line23='        ' THEN FundsAll ELSE 0 END) [Funds' + @RepastNo + '],''
Line24=        FETCH NEXT FROM repast_cursor
Line25=        INTO @RepastNo
Line26=    END
Line27=--
Line28=CLOSE repast_cursor
Line29=DEALLOCATE repast_cursor
Line30=SET @SQL=@SQL+'SUM(CountAll)[CountAll],SUM(FundsAll) [FundsAll]' +
Line31='  FROM  ' +
Line32='  (SELECT Type_No,Repast_No,SUM(Consume_Copy)[CountAll],SUM(Consume_Fund) [FundsAll]' +
Line33='  FROM ST_Card A' +
Line34='  LEFT JOIN XF_Consume B ON B.Person_ID=A.Person_ID' +
Line35='  WHERE Consume_Time >=''' + @ConsumeTimeBng + ''' and Consume_Time <= '''+ @ConsumeTimeEnd + '''' +
Line36='   AND B.SysTem_No='''+'SF'''+
Line37='  GROUP BY Type_No,Repast_No ' +
Line38='  Union All ' + 
Line39='  SELECT Type_No,Repast_No,SUM(Consume_Copy)[CountAll],SUM(Consume_Fund) [FundsAll]' +
Line40='  FROM ST_Card A' +
Line41='  LEFT JOIN XF_ConsumeHistory B ON B.Person_ID=A.Person_ID' +
Line42='  WHERE Consume_Time >=''' + @ConsumeTimeBng + ''' and Consume_Time <= '''+ @ConsumeTimeEnd + '''' +
Line43='   AND B.SysTem_No='''+'SF'''+
Line44='  GROUP BY Type_No,Repast_No ' +
Line45='  ) C' +
Line46='  LEFT JOIN ST_CardType D ON D.Type_No=C.Type_No' +
Line47='    '  GROUP BY  C.Type_No,[Type_Name]''
Line48=EXEC(@SQL)
[Step224]
Line1=IF OBJECT_ID('SF_pd_CanBeiHuiZongBeg') IS NOT NULL
Line2=   DROP PROCEDURE SF_pd_CanBeiHuiZongBeg
[Step225]
Line1=CREATE procedure SF_pd_CanBeiHuiZongBeg
Line2=  @Is_Check bit,
Line3=  @BegDateTime Varchar(255),
Line4=  @EndDateTime Varchar(255),
Line5=  @BegDate Varchar(255)
Line6=as 
Line7=begin
Line8=  Declare @rec Varchar(50),    @ref Varchar(50),   
Line9=               @RepastCount Varchar(50),   @RepastFunds Varchar(50),
Line10=              @Repast_No Varchar(3),   @SQLCreate NVarchar(4000)
Line11=  Set @rec='rec'  set @ref='ref'
Line12=  set @RepastCount='RepastCount'  set @RepastFunds='RepastFunds'
Line13=  /*過慮數據臨時表*/
Line14=  if Object_Id('Tempdb..##Temp_Consume') is not Null 
Line15=      Drop Table ##Temp_Consume
Line16=   Create Table  ##Temp_Consume (
Line17=	                     ID_KEY bigint IDENTITY (1, 1) NOT NULL ,
Line18=	                     Person_ID  int NOT NULL ,
Line19=	                     Consume_Time  datetime NOT NULL ,
Line20=	                     Consume_Fund money NOT NULL ,
Line21=	                     Consume_Copy int NULL ,
Line22=                                   Repast_No  varchar (3) COLLATE Chinese_PRC_CI_AS NULL
Line23=     ) On  [PRIMARY]
Line24=     Alter Table  ##Temp_Consume  With NoCheck Add  Primary Key   Clustered    ( ID_Key)  ON [PRIMARY]
Line25=     Alter Table  ##Temp_Consume With NoCheck Add Constraint DF_Temp_Consume Default (getdate()) For Consume_Time
Line26=     Create  Index IX_Temp_Consume_Person_ID ON  ##Temp_Consume(Person_ID, Consume_Time) ON [PRIMARY]
Line27=     Insert Into ##Temp_Consume(Person_ID,Consume_Time,Consume_Fund,Consume_Copy,Repast_No)
Line28=           Select Person_ID,Consume_Time,Consume_Fund,Consume_Copy,Repast_No From XF_Consume
Line29=           Where  (CONVERT(CHAR(16),  Consume_Time, 20) >= @BegDateTime) And  (CONVERT(CHAR(16),  Consume_Time, 20) <= @EndDateTime)
Line30=                  AND System_No='SF'   
Line31=     UNION ALL
Line32=           Select Person_ID,Consume_Time,Consume_Fund,Consume_Copy,Repast_No From XF_ConsumeHistory
Line33=           Where  (CONVERT(CHAR(16),  Consume_Time, 20) >= @BegDateTime) And  (CONVERT(CHAR(16),  Consume_Time, 20) <= @EndDateTime)
Line34=                  AND System_No='SF'   
Line35=  /*創建部門信息臨時表*/
Line36=  if Object_Id('Tempdb..##SF_PsnRepastRpt') is not Null 
Line37=     Drop Table ##SF_PsnRepastRpt
Line38=  Create Table ##SF_PsnRepastRpt
Line39=              ( Person_ID int ,
Line40=                Person_No varChar(20) Not Null,
Line41=                Person_Name varchar(20) Null,
Line42=                Dept_No varchar(30) Null,
Line43=                Dept_Name varchar(30) Null)
Line44=  if @Is_Check=0 
Line45=     Insert Into ##SF_PsnRepastRpt (Person_ID,Person_No,Person_Name,Dept_No,Dept_Name)
Line46=         Select dbo.ST_Person.Person_ID,dbo.ST_Person.Person_No,dbo.ST_Person.Person_Name,dbo.ST_Department.Dept_No,
Line47=             dbo.ST_Department.Dept_Name From dbo.ST_Person,dbo.ST_Department 
Line48=                /*JenanWang 2006.12.12 不統計注銷人員*/
Line49=             Where ST_Person.Is_Del=0 AND dbo.ST_Person.Dept_ID=dbo.ST_Department.Dept_ID 
Line50=               /*peihb 2008-12-26 過濾在本系統中不可使用人員*/
Line51=              and dbo.ST_Person.Person_ID in (select Person_ID from ST_SubSysRight where system_no='SF')
Line52=  else
Line53=      Insert into ##SF_PsnRepastRpt (Person_ID,Person_No,Person_Name,Dept_No,Dept_Name)
Line54=          Select dbo.ST_Person.Person_ID,dbo.ST_Person.Person_No,dbo.ST_Person.Person_Name,dbo.ST_Department.Dept_No,
Line55=              dbo.ST_Department.Dept_Name From dbo.ST_Person,dbo.ST_Department 
Line56=              Where dbo.ST_Person.Dept_ID=dbo.ST_Department.Dept_ID 
Line57=              and dbo.ST_Person.Person_ID in (select Person_ID from ST_SubSysRight where system_no='SF')
Line58=  /*匯總的臨時表*/
Line59=  if Object_Id('Tempdb..##SF_RepastSum') is not Null 
Line60=     Drop Table ##SF_RepastSum
Line61=    /*創建返回結果集*/
Line62=  if Object_Id('Tempdb..##SF_PsnDayRpt') is not Null 
Line63=     Drop Table ##SF_PsnDayRpt
Line64=  Select  @SQLCreate=''
Line65=  Declare Repast_cur2 Cursor Local Forward_only Static Read_only For
Line66=      Select  Repast_No  From SF_Repast Order By Repast_No
Line67=  Open Repast_cur2
Line68=  Fetch Next From Repast_cur2 Into @Repast_No
Line69=  While (@@fetch_status=0)
Line70=  Begin
Line71=    Select @SQLCreate = @SQLCreate+@RepastCount+@Repast_No+' int NULL Default 0 ,'+@RepastFunds+@Repast_No+' money NULL Default 0, ';
Line72=    Fetch Next From Repast_cur2 Into @Repast_No
Line73=  End;
Line74=  Close Repast_cur2
Line75=  Deallocate Repast_cur2
Line76=  Select @SQLCreate  = ' Create Table ##SF_PsnDayRpt(Consume_time varchar(10) Not NULL, '+
Line77=                                       @SQLCreate+'RepastCountSum money NULL Default 0.00,RepastFundsSum money NULL Default 0.00,'+
Line78=                                       ' Person_No varChar(20) Not Null,'+
Line79=                                        ' Person_Name varchar(20) Null,Dept_No varchar(30) Null,Dept_Name varchar(30) Null )';
Line80=  Exec sp_executesql @SQLCreate;
Line81=end;

[Step226]
System=KQ
Line1=/*下載表增加 圖片文件名 字段*/ 
Line2=IF COL_LENGTH('KQ_DownloadHistory','FileName') IS NULL
Line3=  ALTER TABLE KQ_DownloadHistory ADD FileName VARCHAR(128)
[Step227]
System=KQ
Line1=/*下載表增加 異常標志 字段*/ 
Line2=IF COL_LENGTH('KQ_DownloadHistory','Is_Falsity') IS NULL
Line3=  ALTER TABLE KQ_DownloadHistory ADD Is_Falsity BIT DEFAULT 0


[Step228]
Line1=IF OBJECT_ID('SF_pd_CanBeiHuiZongCal') IS NOT NULL
Line2=   DROP PROCEDURE SF_pd_CanBeiHuiZongCal
[Step229]
System=SF
Line1=CREATE procedure SF_pd_CanBeiHuiZongCal
Line2=  @Is_Check bit,
Line3=  @BegDateTime Varchar(255),
Line4=  @EndDateTime Varchar(255),
Line5=  @BegDate Varchar(255)
Line6=as 
Line7=begin
Line8=  Declare @SelGroup NVarchar(4000),  @sField  Varchar(2000),
Line9=          @sValue  Varchar(2000),      @Display  Varchar(2000),
Line10=          @DisplaySum1 Varchar(2000),    @DisPlaySum2 Varchar(2000),
Line11=          @Repast_No Varchar(255),   @Bgn_Time Varchar(255),
Line12=          @End_Time Varchar(255),     @sBegDate Varchar(255),
Line13=          @sEndDate varchar(255),     @rec Varchar(50),
Line14=          @ref Varchar(50),     @RepastCount Varchar(50),  
Line15=          @RepastFunds Varchar(50),    @SqlStr Varchar(2000),
Line16=          @sField1 Varchar(2000)
Line17=  Set @rec='rec'  set @ref='ref'
Line18=  set @RepastCount='RepastCount'  set @RepastFunds='RepastFunds'
Line19=  /*匯總的臨時表*/
Line20=  if Object_Id('Tempdb..##SF_RepastSum') is not Null 
Line21=     Drop Table ##SF_RepastSum
Line22=  Set @DisplaySum1 = '';  Set @Display = '';  Set @DisPlaySum2 = '';  Set @SelGroup = '';  Set @sField = '';  Set @sValue = '';  Set @sField1='';
Line23=  /*獲取參別信息*/
Line24=  Declare Repast_cur2 Cursor Local Forward_only Static Read_only For
Line25=      Select  Repast_No,Bgn_Time,End_Time  From dbo.SF_Repast Order By Repast_No
Line26=  Open Repast_cur2
Line27=  Fetch Next From Repast_cur2 Into @Repast_No,@Bgn_Time,@End_Time
Line28=  While (@@fetch_status=0)
Line29=  Begin
Line30=    Select @sBegDate=@BegDate+' '+@Bgn_Time
Line31=    If @Bgn_Time <= @End_Time
Line32=      Select @sEndDate=@BegDate+' '+@End_Time
Line33=    Else
Line34=      Select @sEndDate=convert(char(10),Cast(@BegDate as DateTime)+1,20)+' '+@End_Time
Line35=    /*餐別起始時間和結束時間都為零點時表示不統計該餐別，匯總時直接sum(0) as RepastField  peihb 2010-04-16*/
Line36=    IF @Bgn_Time='00:00' AND @End_Time='00:00'
Line37=    BEGIN
Line38=      Select @sField= @sField+','+@Rec+@Repast_No+','+@Ref+@Repast_No;
Line39=      Select @sField1=@sField1+','+@RepastCount+@Repast_No+','+@RepastFunds+@Repast_No;
Line40=      Select @sValue = @sValue +',0,0.00'	
Line41=      Select @Display = @Display+',sum(isnull(0,0)) '+@Rec+@Repast_No+',sum(isnull(0,0)) '+@Ref+@Repast_No;
Line42=      Select @DisplaySum1 = @DisplaySum1+'+sum(isnull(0,0))';
Line43=      Select @DisPlaySum2 = @DisPlaySum2+'+sum(isnull(0,0))';	
Line44=    END 
Line45=    ELSE
Line46=    BEGIN
Line47=      Select @sField= @sField+','+@Rec+@Repast_No+','+@Ref+@Repast_No;
Line48=      Select @sField1=@sField1+','+@RepastCount+@Repast_No+','+@RepastFunds+@Repast_No;
Line49=      Select @sValue = @sValue +',0,0.00'
Line50=      Select @Display = @Display+',sum(isnull('+@Rec+@Repast_No+',0)) '+@Rec+@Repast_No+',sum(isnull('+@Ref+@Repast_No+',0)) '+@Ref+@Repast_No;
Line51=      Select @DisplaySum1 = @DisplaySum1+'+sum(isnull('+@Rec+@Repast_No+',0))';
Line52=      Select @DisPlaySum2 = @DisPlaySum2+'+sum(isnull('+@Ref+@Repast_No+',0))';	
Line53=    END	
Line54=    Select @SelGroup = @SelGroup+',Case When (CONVERT(CHAR(16),Consume_Time, 20) Between '''+@sBegDate+''' And '''+@sEndDate+''') then Consume_Copy end as Rec'+@Repast_No+
Line55=                                ',Case When (CONVERT(CHAR(16),Consume_Time, 20) Between '''+@sBegDate+''' And '''+@sEndDate+''') then Consume_Fund end as Ref'+@Repast_No;
Line56=    /*返回臨時數據集*/
Line57=    Fetch Next From Repast_cur2 Into @Repast_No,@Bgn_Time,@End_Time
Line58=  End;
Line59=  Close Repast_cur2
Line60=  Deallocate Repast_cur2
Line61=  /**/
Line62=  Set @Display = SubString(@Display,2,len(@Display))
Line63=  Set @DisplaySum1 = SubString(@DisplaySum1,2,len(@DisplaySum1))+' RepastCountSum'
Line64=  Set @DisPlaySum2 = SubString(@DisPlaySum2,2,len(@DisPlaySum2))+' RepastFundsSum'
Line65=  Set @SelGroup = SubString(@SelGroup,2,len(@SelGroup))
Line66=  Select @SelGroup = 'Select Person_ID,'+@Display+','+@DisplaySum1+','+@DisPlaySum2+' Into ##SF_RepastSum'+
Line67=                    ' From (Select Person_ID,'+@SelGroup+' from ##Temp_Consume) bb Group By Person_ID';
Line68=  Exec sp_executesql   @SelGroup
Line69=  Delete ##Temp_Consume where   (CONVERT(CHAR(16),  Consume_Time, 20) >= @BegDate+' '+' 00:00' ) And  (CONVERT(CHAR(16),  Consume_Time, 20) <= @BegDate+' '+' 23:59')
Line70= /*插入職員信息*/
Line71=  Select @SelGroup=' Insert into ##SF_RepastSum(Person_ID'+@sField+',RepastCountSum,RepastFundsSum)
Line72=                    Select Person_ID'+@sValue+',0,0.00 From ##SF_PsnRepastRpt where Person_ID not in (select Person_ID from ##SF_RepastSum)'
Line73=  Exec sp_executesql @SelGroup
Line74=  /**/
Line75=  Select  @SelGroup  = ' Insert Into ##SF_PsnDayRpt(Consume_time,Person_No,Person_Name,Dept_No,Dept_Name'+@sField1+',RepastCountSum,RepastFundsSum) '
Line76=                                     +'  Select '''+@BegDate+''',P.Person_No, P.Person_Name,P.Dept_No,P.Dept_Name'+@sField+',S.RepastCountSum,S.RepastFundsSum From ##SF_PsnRepastRpt P,##SF_RepastSum S Where P.Person_ID = S.Person_ID'
Line77=  Exec sp_executesql @SelGroup
Line78=end
[Step230]
Line1=UPDATE ST_SubSystem SET DBVersion='3.2.0' WHERE System_No='LH' AND IsInitTable=1
Line2=UPDATE ST_SubSystem SET DBVersion='3.2.0' WHERE System_No='SF' AND IsInitTable=1
Line3=UPDATE ST_SubSystem SET DBVersion='3.2.0' WHERE System_No='KQ' AND IsInitTable=1
Line4=UPDATE ST_SubSystem SET DBVersion='3.2.0' WHERE System_No='ST' AND IsInitTable=1
Line5=UPDATE ST_SubSystem SET DBVersion='3.2.0' WHERE System_No='SK' AND IsInitTable=1
Line6=UPDATE ST_SubSystem SET DBVersion='3.2.0' WHERE System_No='MJ' AND IsInitTable=1
Line7=DELETE FROM ST_MenuInfo WHERE Menu_ID=134
