/***********************************************/ /*** WASTEMAN 2G SYBASE DATABASE UPDATE FILE ***/ /***********************************************/ /* Example Script To Add a New Field To An Existing Table IF NOT EXISTS( SELECT 1 FROM syscolumn JOIN systable WHERE table_name = '#TABLE_NAME' AND column_name = '#NEW_COLUMN_NAME') THEN ALTER TABLE "DBA".#TABLE_NAME ADD #NEW_COLUMN_NAME INTEGER DEFAULT 0; --Passthrough the update to the remote sites (if in a replicated environment) IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; ALTER TABLE "DBA".#TABLE_NAME ADD #NEW_COLUMN_NAME INTEGER DEFAULT 0; PASSTHROUGH STOP; END IF; END IF; */ /* Example Script To Update a trigger, procedure or view on first change only IF NOT EXISTS( SELECT 1 FROM "DBA".SM_Version_Control WHERE Entity_Name = '#UNIQUE_IDENTIFIER' AND Database_ID = (SELECT db_property('GlobalDbID'))) THEN #ENTER_TRIGGER_DEFINITION_HERE INSERT "DBA".SM_Version_Control(Entity_Name, Version_No, Database_ID) VALUES('#UNIQUE_IDENTIFIER', 1, (SELECT db_property('GlobalDBId'))); --Passthrough the update to the remote sites (if in a replicated environment) IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; #ENTER_TRIGGER_DEFINITION_HERE INSERT "DBA".SM_Version_Control(Entity_Name, Version_No, Database_ID) VALUES('#UNIQUE_IDENTIFIER', 1, (SELECT db_property('GlobalDBId'))); PASSTHROUGH STOP; END IF; END IF; */ /* Example Script To Update a trigger, procedure or view on subsequent changes IF EXISTS( SELECT 1 FROM "DBA".SM_Version_Control WHERE Entity_Name = '#UNIQUE_IDENTIFIER' AND Database_ID = (SELECT db_property('GlobalDbID')) AND Version_No < #VERSION_NUMBER) THEN #ENTER_TRIGGER_DEFINITION_HERE --Update version control UPDATE "DBA".SM_Version_Control SET Version_No = #VERSION_NUMBER WHERE Entity_Name = '#UNIQUE_IDENTIFIER' AND Database_ID = (SELECT db_property('GlobalDbID')); --Passthrough the update to the remote sites (if in a replicated environment) IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; #ENTER_TRIGGER_DEFINITION_HERE --Update version control UPDATE "DBA".SM_Version_Control SET Version_No = #VERSION_NUMBER WHERE Entity_Name = '#UNIQUE_IDENTIFIER' AND Database_ID = (SELECT db_property('GlobalDbID')); PASSTHROUGH STOP; END IF; END IF; */ /***********************************************/ /* Create a backup of the database first in C:\W2G_Backup */ /***********************************************/ BACKUP DATABASE DIRECTORY 'C:\\W2G_Backup'; --Backup the remote database, if in a replicated environment IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; /* NOTE "ONLY" keyword prevents the update from being applied at the consolidated node */ BACKUP DATABASE DIRECTORY 'C:\\W2G_Backup'; PASSTHROUGH STOP; END IF; /********************************************************************/ /* 29/06/2006 - Fix bug in WM_DuplicateProductRate causing invalid */ /* authorisation records to be inserted */ /********************************************************************/ IF NOT EXISTS( SELECT 1 FROM "DBA".SM_Version_Control WHERE Entity_Name = 'WM_DuplicateProductRate' AND Database_ID = (SELECT db_property('GlobalDbID'))) THEN ALTER PROCEDURE "DBA".WM_DuplicateProductRate(IN @RateRangeNo Entity_No, IN @NewDesc VARCHAR(20), OUT @NewRangeNo Entity_No, IN @NewIsDefault Boolean DEFAULT 'N', IN @NewEffectiveDate Entity_Date DEFAULT NULL, IN @NewEndEffectiveDate Entity_Date DEFAULT NULL, IN @DupAuthorisations Boolean DEFAULT 'Y') BEGIN /***************************************************************************/ /* WM_DuplicateProductRate */ /* */ /* This proc will create a duplicate product rate record with the */ /* specified Description (@NewDesc), Is_Default value (@NewIsDefault), */ /* Valid_Start_Date (@NewEffectiveDate) and Valid_End_Date */ /* (@NewEndEffectiveDate). */ /* If @DupAuthorisation = 'Y' the product rate authorisations will also */ /* be duplicated. */ /***************************************************************************/ DECLARE @ErrMsg VARCHAR(100); DECLARE @DefIsoLevel INT; --Validate inputs IF @RateRangeNo IS NULL THEN RAISERROR 30000 'You must specify a Rate Range No'; RETURN; END IF; IF @NewDesc IS NULL THEN RAISERROR 30000 'You must specify a descriptor for the new Product Rate'; RETURN; END IF; IF NOT EXISTS( SELECT 1 FROM "DBA".WM_Product_Rate WHERE Range_No = @RateRangeNo) THEN SET @ErrMsg = 'No Product Rate found for Range No ' || RTRIM(CAST(@RateRangeNo AS CHAR)); RAISERROR 30000 @ErrMsg; RETURN; END IF; SET @NewRangeNo = NULL; --Save current iso level SELECT connection_property('ISOLATION_LEVEL') INTO @DefIsoLevel; --Use Read Committed isolation level SET TEMPORARY OPTION ISOLATION_LEVEL = 1; --Duplicate the product rate record INSERT "DBA".WM_Product_Rate (Product_No, Rate_No, "Description", "Value", Net, Is_Default, Charge_Type, Is_Operator_Selected, Is_Auto_Range, Cost_Rule_No, Minimum_Cost, Min_Gross_Allowed, Max_Gross_Allowed, Valid_Start_Date, Valid_End_Date, User_Rate_Field1, User_Rate_Field2, User_Rate_Field3, User_Rate_Field4, Sub_Stream_2_No, Memo, Available) SELECT Product_No, Rate_No, @NewDesc, "Value", Net, @NewIsDefault, Charge_Type, Is_Operator_Selected, Is_Auto_Range, Cost_Rule_No, Minimum_Cost, Min_Gross_Allowed, Max_Gross_Allowed, @NewEffectiveDate, @NewEndEffectiveDate, User_Rate_Field1, User_Rate_Field2, User_Rate_Field3, User_Rate_Field4, Sub_Stream_2_No, Memo, Available FROM "DBA".WM_Product_Rate WHERE Range_No = @RateRangeNo; SET @NewRangeNo = @@identity; --Duplicate product rate authorisations if required IF @DupAuthorisations = 'Y' THEN --Get the Product Rate -> ??? authorisation keys CREATE TABLE #AuthConfigHeaders( Auth_Config_No INT PRIMARY KEY, Authing_Class_ID INT, Authed_Class_ID INT); INSERT #AuthConfigHeaders (Auth_Config_No, Authing_Class_ID, Authed_Class_ID) SELECT Authorisation_Config_No, Authorising_Class_ID, Authorised_Class_ID FROM "DBA".SM_Authorisation_Config WHERE Authorising_Class_ID = 3007 --Product Rate AND Authorised_Process = 3002; --Calc Product Charge Process INSERT "DBA".SM_Authorisation_Item (Authorisation_Config_No, Authorising_Class_Key, Authorised_Class_Key) SELECT Authorisation_Config_No, @NewRangeNo, Authorised_Class_Key FROM "DBA".SM_Authorisation_Item WHERE Authorisation_Config_No IN (SELECT Auth_Config_No FROM #AuthConfigHeaders) AND Authorising_Class_Key = RTRIM(CAST(@RateRangeNo AS CHAR)); DROP TABLE #AuthConfigHeaders; END IF; --Commit transaction COMMIT; --Restore previous iso level IF @DefIsoLevel = 0 THEN SET TEMPORARY OPTION ISOLATION_LEVEL = 0; ELSEIF @DefIsoLevel = 1 THEN SET TEMPORARY OPTION ISOLATION_LEVEL = 1; ELSEIF @DefIsoLevel = 2 THEN SET TEMPORARY OPTION ISOLATION_LEVEL = 2; ELSEIF @DefIsoLevel = 3 THEN SET TEMPORARY OPTION ISOLATION_LEVEL = 3; END IF; END; --Add version control rec INSERT "DBA".SM_Version_Control(Entity_Name, Database_ID, Version_No) VALUES('WM_DuplicateProductRate', (SELECT db_property( 'GlobalDBId' )), 1); IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; ALTER PROCEDURE "DBA".WM_DuplicateProductRate(IN @RateRangeNo Entity_No, IN @NewDesc VARCHAR(20), OUT @NewRangeNo Entity_No, IN @NewIsDefault Boolean DEFAULT 'N', IN @NewEffectiveDate Entity_Date DEFAULT NULL, IN @NewEndEffectiveDate Entity_Date DEFAULT NULL, IN @DupAuthorisations Boolean DEFAULT 'Y') BEGIN /***************************************************************************/ /* WM_DuplicateProductRate */ /* */ /* This proc will create a duplicate product rate record with the */ /* specified Description (@NewDesc), Is_Default value (@NewIsDefault), */ /* Valid_Start_Date (@NewEffectiveDate) and Valid_End_Date */ /* (@NewEndEffectiveDate). */ /* If @DupAuthorisation = 'Y' the product rate authorisations will also */ /* be duplicated. */ /***************************************************************************/ DECLARE @ErrMsg VARCHAR(100); DECLARE @DefIsoLevel INT; --Validate inputs IF @RateRangeNo IS NULL THEN RAISERROR 30000 'You must specify a Rate Range No'; RETURN; END IF; IF @NewDesc IS NULL THEN RAISERROR 30000 'You must specify a descriptor for the new Product Rate'; RETURN; END IF; IF NOT EXISTS( SELECT 1 FROM "DBA".WM_Product_Rate WHERE Range_No = @RateRangeNo) THEN SET @ErrMsg = 'No Product Rate found for Range No ' || RTRIM(CAST(@RateRangeNo AS CHAR)); RAISERROR 30000 @ErrMsg; RETURN; END IF; SET @NewRangeNo = NULL; --Save current iso level SELECT connection_property('ISOLATION_LEVEL') INTO @DefIsoLevel; --Use Read Committed isolation level SET TEMPORARY OPTION ISOLATION_LEVEL = 1; --Duplicate the product rate record INSERT "DBA".WM_Product_Rate (Product_No, Rate_No, "Description", "Value", Net, Is_Default, Charge_Type, Is_Operator_Selected, Is_Auto_Range, Cost_Rule_No, Minimum_Cost, Min_Gross_Allowed, Max_Gross_Allowed, Valid_Start_Date, Valid_End_Date, User_Rate_Field1, User_Rate_Field2, User_Rate_Field3, User_Rate_Field4, Sub_Stream_2_No, Memo, Available) SELECT Product_No, Rate_No, @NewDesc, "Value", Net, @NewIsDefault, Charge_Type, Is_Operator_Selected, Is_Auto_Range, Cost_Rule_No, Minimum_Cost, Min_Gross_Allowed, Max_Gross_Allowed, @NewEffectiveDate, @NewEndEffectiveDate, User_Rate_Field1, User_Rate_Field2, User_Rate_Field3, User_Rate_Field4, Sub_Stream_2_No, Memo, Available FROM "DBA".WM_Product_Rate WHERE Range_No = @RateRangeNo; SET @NewRangeNo = @@identity; --Duplicate product rate authorisations if required IF @DupAuthorisations = 'Y' THEN --Get the Product Rate -> ??? authorisation keys CREATE TABLE #AuthConfigHeaders( Auth_Config_No INT PRIMARY KEY, Authing_Class_ID INT, Authed_Class_ID INT); INSERT #AuthConfigHeaders (Auth_Config_No, Authing_Class_ID, Authed_Class_ID) SELECT Authorisation_Config_No, Authorising_Class_ID, Authorised_Class_ID FROM "DBA".SM_Authorisation_Config WHERE Authorising_Class_ID = 3007 --Product Rate AND Authorised_Process = 3002; --Calc Product Charge Process INSERT "DBA".SM_Authorisation_Item (Authorisation_Config_No, Authorising_Class_Key, Authorised_Class_Key) SELECT Authorisation_Config_No, @NewRangeNo, Authorised_Class_Key FROM "DBA".SM_Authorisation_Item WHERE Authorisation_Config_No IN (SELECT Auth_Config_No FROM #AuthConfigHeaders) AND Authorising_Class_Key = RTRIM(CAST(@RateRangeNo AS CHAR)); DROP TABLE #AuthConfigHeaders; END IF; --Commit transaction COMMIT; --Restore previous iso level IF @DefIsoLevel = 0 THEN SET TEMPORARY OPTION ISOLATION_LEVEL = 0; ELSEIF @DefIsoLevel = 1 THEN SET TEMPORARY OPTION ISOLATION_LEVEL = 1; ELSEIF @DefIsoLevel = 2 THEN SET TEMPORARY OPTION ISOLATION_LEVEL = 2; ELSEIF @DefIsoLevel = 3 THEN SET TEMPORARY OPTION ISOLATION_LEVEL = 3; END IF; END; --Add version control rec INSERT "DBA".SM_Version_Control(Entity_Name, Database_ID, Version_No) VALUES('WM_DuplicateProductRate', (SELECT db_property( 'GlobalDBId' )), 1); PASSTHROUGH STOP; END IF; END IF; /********************************************************************/ /* 29/06/2006 - Add Is_GST_Exempt column to WM_Customer */ /********************************************************************/ IF NOT EXISTS( SELECT 1 FROM syscolumn JOIN systable WHERE table_name = 'WM_Customer' AND column_name = 'Is_GST_Exempt') THEN ALTER TABLE "DBA".WM_Customer ADD Is_GST_Exempt Boolean NOT NULL DEFAULT 'N'; IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; ALTER TABLE "DBA".WM_Customer ADD Is_GST_Exempt Boolean NOT NULL DEFAULT 'N'; PASSTHROUGH STOP; END IF; END IF; /********************************************************************/ /* 29/06/2006 - Modify WM_GetProductCharges to return Is_GST flag from Rate record */ /********************************************************************/ IF EXISTS( SELECT 1 FROM "DBA".SM_Version_Control WHERE Entity_Name = 'WM_GetProductCharges' AND Database_ID = (SELECT db_property('GlobalDbID')) AND Version_No < 7) THEN ALTER PROCEDURE "DBA".WM_GetProductCharges(IN @ProductNo Entity_No, IN @TranDate Entity_Date, IN @SpecificChargeNo Entity_No, IN @SpecificChargeType CHAR(1), IN @CheckAuthorisations Boolean, IN @AllowUnavailable Boolean DEFAULT 'N') RESULT( Product_No Entity_No, Rate_No Entity_No, Range_No Entity_No, Description varchar(20), Value numeric(10,4), Net Weight, Is_Default Boolean, Charge_Type Prod_Cost_Method, Is_Operator_Selected Boolean, Is_Auto_Range Boolean, Cost_Rule_No Entity_No, Minimum_Cost numeric(10,4), Min_Gross_Allowed Weight, Max_Gross_Allowed Weight, Valid_Start_Date Entity_Date, Valid_End_Date Entity_Date, User_Rate_Field1 numeric(10,4), User_Rate_Field2 numeric(10,4), User_Rate_Field3 numeric(10,4), User_Rate_Field4 numeric(10,4), Sub_Stream_2_No integer, Memo Entity_Comment, Available Boolean, Type char(1), Name Entity_Name, Is_GST Boolean, Source_Rate_No Entity_No ) BEGIN DECLARE LOCAL TEMPORARY TABLE @ChargeAuthHeaders( Authorisation_Config_No INTEGER, Authorised_Class_ID INTEGER, Table_Name CHAR(128)); --Declare local temp table to hold result set while processing DECLARE LOCAL TEMPORARY TABLE @ChargeInfo( Range_No INTEGER, Rate_No INTEGER, Rate_Type CHAR(1), Description VARCHAR(20), Authorised_Class_ID INT, Authorised_Class_Key VARCHAR(50), Table_Name CHAR(128), Available CHAR(1)); DECLARE @AuthCust Entity_No; DECLARE @AuthSite Entity_No; INSERT @ChargeAuthHeaders (Authorisation_Config_No, Authorised_Class_ID, Table_Name) SELECT AC.Authorisation_Config_No, AC.Authorised_Class_ID, OC.Table_Name FROM "DBA".SM_Authorisation_Config AC LEFT OUTER JOIN "DBA".SM_Object_Class OC ON (OC.Object_Class = AC.Authorised_Class_ID) --may not exist WHERE AC.Authorising_Class_ID = 3007 --product rate AND AC.Authorised_Process = 3002; --charge process IF @@ERROR <> 0 THEN RETURN; END IF; /* NOTE: The @AllowUnavailable flag will change filtering of Charge-type rates based on authorisations, validity dates and the "Available" flag. */ /* When set to 'Y', the procedure will return rates irrespective of authorisations, validity date settings or "Available" flag. */ /* When set to 'N', the procedure will return only rates that are authorised (optionally), flagged as "Available" and are valid for the supplied @TranDate. */ /* %MG NOTE 05/06/2006 - Rates of type other than 'C' are still subject to validity date checks when @AllowUnavailable = 'Y', to prevent multiples of the same rate from being returned */ IF @SpecificChargeNo IS NULL THEN -- Select all rates for the product. IF (@CheckAuthorisations = 'Y') AND (@AllowUnavailable = 'N') THEN -- Create a "shortlist" of available rates based on Available flag, Validity dates and Authorised entities INSERT @ChargeInfo (Range_No, Rate_No, Rate_Type, Description, Authorised_Class_ID, Authorised_Class_Key, Table_Name, Available) SELECT PR.Range_No, PR.Rate_No, R.Type, PR.Description, CAH.Authorised_Class_ID, AI.Authorised_Class_Key, CAH.Table_Name, PR.Available FROM "DBA".WM_Product_Rate PR INNER JOIN "DBA".WM_Rate R ON (R.Rate_No = PR.Rate_No) LEFT OUTER JOIN "DBA".SM_Authorisation_Item AI ON ((AI.Authorising_Class_Key = CAST(PR.Range_No AS CHAR)) AND (AI.Authorisation_Config_No IN (SELECT Authorisation_Config_No FROM @ChargeAuthHeaders))) LEFT OUTER JOIN @ChargeAuthHeaders CAH ON (CAH.Authorisation_Config_No = AI.Authorisation_Config_No) WHERE (PR.Product_No = @ProductNo) AND ((DATEDIFF(Day, PR.Valid_Start_Date, @TranDate) >= 0) OR (PR.Valid_Start_Date IS NULL)) AND ((DATEDIFF(Day, @TranDate, PR.Valid_End_Date) >= 0) OR (PR.Valid_End_Date IS NULL)) AND (PR.Available = 'Y') ELSE -- Not checking authorisations - just return the result set. -- Must be in order by net in descending order for DetermineBestRate to work correctly. SELECT PR.Product_No, PR.Rate_No, PR.Range_No, PR.Description, PR.Value, PR.Net, PR.Is_Default, PR.Charge_Type, PR.Is_Operator_Selected, PR.Is_Auto_Range, PR.Cost_Rule_No, PR.Minimum_Cost, PR.Min_Gross_Allowed, PR.Max_Gross_Allowed, PR.Valid_Start_Date, PR.Valid_End_Date, PR.User_Rate_Field1, PR.User_Rate_Field2, PR.User_Rate_Field3, PR.User_Rate_Field4, PR.Sub_Stream_2_No, PR.Memo, PR.Available, R.Type, R.Name, R.Is_GST, R.Rate_No AS Source_Rate_No FROM "DBA".WM_Product_Rate PR, "DBA".WM_Rate R WHERE PR.Rate_No = R.Rate_No AND PR.Product_No = @ProductNo AND ((DATEDIFF(Day, PR.Valid_Start_Date, @TranDate) >= 0) OR (PR.Valid_Start_Date IS NULL) OR ((@AllowUnavailable = 'Y') AND (R.Type = 'C'))) AND ((DATEDIFF(Day, @TranDate, PR.Valid_End_Date) >= 0) OR (PR.Valid_End_Date IS NULL) OR ((@AllowUnavailable = 'Y') AND (R.Type = 'C'))) AND ((PR.Available = 'Y') OR (@AllowUnavailable = 'Y')) ORDER BY PR.Net DESC END IF; ELSE -- Select specific rate. IF (@CheckAuthorisations = 'Y') AND (@AllowUnavailable = 'N') THEN -- Create a "shortlist" of available rates based on Available flag, Validity dates and Authorised entities -- If the specified rate has the auto range flag then return the auto range group that includes the rate. IF EXISTS( SELECT 1 FROM "DBA".WM_Product_Rate WHERE Product_No = @ProductNo AND ((Range_No = @SpecificChargeNo) AND (Is_Auto_Range = 'Y'))) THEN INSERT @ChargeInfo (Range_No, Rate_No, Rate_Type, Description, Authorised_Class_ID, Authorised_Class_Key, Table_Name, Available) SELECT PR.Range_No, PR.Rate_No, R.Type, PR.Description, CAH.Authorised_Class_ID, AI.Authorised_Class_Key, CAH.Table_Name, PR.Available FROM "DBA".WM_Product_Rate PR INNER JOIN "DBA".WM_Rate R ON (R.Rate_No = PR.Rate_No) LEFT OUTER JOIN "DBA".SM_Authorisation_Item AI ON ((AI.Authorising_Class_Key = CAST(PR.Range_No AS CHAR)) AND (AI.Authorisation_Config_No IN (SELECT Authorisation_Config_No FROM @ChargeAuthHeaders))) LEFT OUTER JOIN @ChargeAuthHeaders CAH ON (CAH.Authorisation_Config_No = AI.Authorisation_Config_No) WHERE (PR.Product_No = @ProductNo) AND (((R.Type = 'C') AND (PR.Is_Auto_Range = 'Y')) OR (R.Type <> 'C')) AND ((DATEDIFF(Day, PR.Valid_Start_Date, @TranDate) >= 0) OR (PR.Valid_Start_Date IS NULL)) AND ((DATEDIFF(Day, @TranDate, PR.Valid_End_Date) >= 0) OR (PR.Valid_End_Date IS NULL)) AND (PR.Available = 'Y') ELSE INSERT @ChargeInfo (Range_No, Rate_No, Rate_Type, Description, Authorised_Class_ID, Authorised_Class_Key, Table_Name, Available) SELECT PR.Range_No, PR.Rate_No, R.Type, PR.Description, CAH.Authorised_Class_ID, AI.Authorised_Class_Key, CAH.Table_Name, PR.Available FROM "DBA".WM_Product_Rate PR INNER JOIN "DBA".WM_Rate R ON (R.Rate_No = PR.Rate_No) LEFT OUTER JOIN "DBA".SM_Authorisation_Item AI ON ((AI.Authorising_Class_Key = CAST(PR.Range_No AS CHAR)) AND (AI.Authorisation_Config_No IN (SELECT Authorisation_Config_No FROM @ChargeAuthHeaders))) LEFT OUTER JOIN @ChargeAuthHeaders CAH ON (CAH.Authorisation_Config_No = AI.Authorisation_Config_No) WHERE (PR.Product_No = @ProductNo) AND ((PR.Range_No = @SpecificChargeNo) OR (R.Type <> 'C')) AND ((DATEDIFF(Day, PR.Valid_Start_Date, @TranDate) >= 0) OR (PR.Valid_Start_Date IS NULL)) AND ((DATEDIFF(Day, @TranDate, PR.Valid_End_Date) >= 0) OR (PR.Valid_End_Date IS NULL)) AND (PR.Available = 'Y') END IF; ELSE -- Not checking authorisations - just return the result set. -- Must be in order by net in descending order for DetermineBestRate to work correctly. -- If the specified rate has the auto range flag then return the auto range group that includes the rate. IF EXISTS( SELECT 1 FROM "DBA".WM_Product_Rate PR, "DBA".WM_Rate R WHERE PR.Rate_No = R.Rate_No AND PR.Product_No = @ProductNo AND ((PR.Range_No = @SpecificChargeNo) AND (PR.Is_Auto_Range = 'Y'))) THEN SELECT PR.Product_No, PR.Rate_No, PR.Range_No, PR.Description, PR.Value, PR.Net, PR.Is_Default, PR.Charge_Type, PR.Is_Operator_Selected, PR.Is_Auto_Range, PR.Cost_Rule_No, PR.Minimum_Cost, PR.Min_Gross_Allowed, PR.Max_Gross_Allowed, PR.Valid_Start_Date, PR.Valid_End_Date, PR.User_Rate_Field1, PR.User_Rate_Field2, PR.User_Rate_Field3, PR.User_Rate_Field4, PR.Sub_Stream_2_No, PR.Memo, PR.Available, R.Type, R.Name, R.Is_GST, R.Rate_No AS Source_Rate_No FROM "DBA".WM_Product_Rate PR, "DBA".WM_Rate R WHERE PR.Rate_No = R.Rate_No AND PR.Product_No = @ProductNo AND ((R.Type = 'C' AND PR.Is_Auto_Range = 'Y') OR (R.Type <> 'C')) AND ((DATEDIFF(Day, PR.Valid_Start_Date, @TranDate) >= 0) OR (PR.Valid_Start_Date IS NULL) OR ((@AllowUnavailable = 'Y') AND (R.Type = 'C'))) AND ((DATEDIFF(Day, @TranDate, PR.Valid_End_Date) >= 0) OR (PR.Valid_End_Date IS NULL) OR ((@AllowUnavailable = 'Y') AND (R.Type = 'C'))) AND ((PR.Available = 'Y') OR (@AllowUnavailable = 'Y')) ORDER BY PR.Net DESC ELSE SELECT PR.Product_No, PR.Rate_No, PR.Range_No, PR.Description, PR.Value, PR.Net, PR.Is_Default, PR.Charge_Type, PR.Is_Operator_Selected, PR.Is_Auto_Range, PR.Cost_Rule_No, PR.Minimum_Cost, PR.Min_Gross_Allowed, PR.Max_Gross_Allowed, PR.Valid_Start_Date, PR.Valid_End_Date, PR.User_Rate_Field1, PR.User_Rate_Field2, PR.User_Rate_Field3, PR.User_Rate_Field4, PR.Sub_Stream_2_No, PR.Memo, PR.Available, R.Type, R.Name, R.Is_GST, R.Rate_No AS Source_Rate_No FROM "DBA".WM_Product_Rate PR, "DBA".WM_Rate R WHERE PR.Rate_No = R.Rate_No AND PR.Product_No = @ProductNo AND ((PR.Range_No = @SpecificChargeNo) OR (R.Type <> 'C')) AND ((DATEDIFF(Day, PR.Valid_Start_Date, @TranDate) >= 0) OR (PR.Valid_Start_Date IS NULL) OR ((@AllowUnavailable = 'Y') AND (R.Type = 'C'))) AND ((DATEDIFF(Day, @TranDate, PR.Valid_End_Date) >= 0) OR (PR.Valid_End_Date IS NULL) OR ((@AllowUnavailable = 'Y') AND (R.Type = 'C'))) AND ((PR.Available = 'Y') OR (@AllowUnavailable = 'Y')) END IF; END IF; END IF; /* If authorisation service and relevant relationships are enabled, and authorisation entities */ /* are supplied, filter the rate list using the supplied entities */ IF (@CheckAuthorisations = 'Y') AND (@AllowUnavailable = 'N') THEN -- Get Cust and/or Site authorised SELECT Authed_Class_Key INTO @AuthCust FROM #AuthedItems WHERE Authed_Class_ID = 3001; SELECT Authed_Class_Key INTO @AuthSite FROM #AuthedItems WHERE Authed_Class_ID = 0100; -- If site specified, delete all charges that authorise sites other than that specified, -- and do not also authorise the site. -- If site not specified, ASSUME THAT SITE AUTHORISATION IS NOT ENABLED, i.e. do NOT check site authorisations. -- It is the caller's responsibility to supply a site number if that authorisation is to be checked. -- NOTE: If supplied site no is -1, treat as unknown, i.e. only charges authorising all sites will be returned IF (@AuthSite IS NOT NULL) OR (@AuthSite = '-1') THEN DELETE @ChargeInfo WHERE Range_No IN (SELECT Range_No FROM @ChargeInfo WHERE Authorised_Class_ID = 0100 AND Authorised_Class_Key <> @AuthSite AND Rate_Type = 'C') AND Range_No NOT IN (SELECT Range_No FROM @ChargeInfo WHERE Authorised_Class_ID = 0100 AND Authorised_Class_Key = @AuthSite AND Rate_Type = 'C') END IF; -- If customer specified and one or more charges in the group explicitly authorise that customer, -- delete all charges that do NOT explicitly authorise the customer. (This includes charges that do not authorise any customers.) -- If customer specified and none of the charges explicitly authorise that customer, -- delete all charges that explictly authorise any customer. This will leave only those charges that do not explicitly authorise -- any customers. -- If customer not specified, ASSUME THAT CUSTOMER AUTHORISATION IS NOT ENABLED, i.e. do NOT check customer authorisations. -- It is the caller's responsibility to supply a customer number if that authorisation is to be checked. -- NOTE: If supplied customer no is -1, treat as unknown, i.e. only charges not authorising a customer will be returned IF (@AuthCust IS NOT NULL) OR (@AuthCust = '-1') THEN IF EXISTS( SELECT 1 FROM @ChargeInfo WHERE Authorised_Class_ID = 3001 AND Authorised_Class_Key = @AuthCust AND Rate_Type = 'C') THEN --Delete all charges that do not explicitly authorise this customer DELETE @ChargeInfo WHERE Rate_Type = 'C' AND Range_No NOT IN (SELECT Range_No FROM @ChargeInfo WHERE Authorised_Class_ID = 3001 AND Authorised_Class_Key = @AuthCust AND Rate_Type = 'C') ELSE --Delete all charges that explicitly authorise any customer DELETE @ChargeInfo WHERE Range_No IN (SELECT Range_No FROM @ChargeInfo WHERE Authorised_Class_ID = 3001 AND Rate_Type = 'C') END IF; END IF; -- RETURN THE RESULT SET -- NOTE: Must be in order by Net in descending order for DetermineBestRate to work correctly. SELECT PR.Product_No, PR.Rate_No, PR.Range_No, PR.Description, PR.Value, PR.Net, PR.Is_Default, PR.Charge_Type, PR.Is_Operator_Selected, PR.Is_Auto_Range, PR.Cost_Rule_No, PR.Minimum_Cost, PR.Min_Gross_Allowed, PR.Max_Gross_Allowed, PR.Valid_Start_Date, PR.Valid_End_Date, PR.User_Rate_Field1, PR.User_Rate_Field2, PR.User_Rate_Field3, PR.User_Rate_Field4, PR.Sub_Stream_2_No, PR.Memo, PR.Available, R.Type, R.Name, R.Is_GST, R.Rate_No AS Source_Rate_No FROM "DBA".WM_Product_Rate PR, "DBA".WM_Rate R WHERE (PR.Rate_No = R.Rate_No) AND (PR.Range_No IN (SELECT Range_No FROM @ChargeInfo)) ORDER BY PR.Net DESC, PR.Description; END IF; END; IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; /* NOTE "ONLY" keyword prevents the update from being applied at the consolidated node */ ALTER PROCEDURE "DBA".WM_GetProductCharges(IN @ProductNo Entity_No, IN @TranDate Entity_Date, IN @SpecificChargeNo Entity_No, IN @SpecificChargeType CHAR(1), IN @CheckAuthorisations Boolean, IN @AllowUnavailable Boolean DEFAULT 'N') RESULT( Product_No Entity_No, Rate_No Entity_No, Range_No Entity_No, Description varchar(20), Value numeric(10,4), Net Weight, Is_Default Boolean, Charge_Type Prod_Cost_Method, Is_Operator_Selected Boolean, Is_Auto_Range Boolean, Cost_Rule_No Entity_No, Minimum_Cost numeric(10,4), Min_Gross_Allowed Weight, Max_Gross_Allowed Weight, Valid_Start_Date Entity_Date, Valid_End_Date Entity_Date, User_Rate_Field1 numeric(10,4), User_Rate_Field2 numeric(10,4), User_Rate_Field3 numeric(10,4), User_Rate_Field4 numeric(10,4), Sub_Stream_2_No integer, Memo Entity_Comment, Available Boolean, Type char(1), Name Entity_Name, Is_GST Boolean, Source_Rate_No Entity_No ) BEGIN DECLARE LOCAL TEMPORARY TABLE @ChargeAuthHeaders( Authorisation_Config_No INTEGER, Authorised_Class_ID INTEGER, Table_Name CHAR(128)); --Declare local temp table to hold result set while processing DECLARE LOCAL TEMPORARY TABLE @ChargeInfo( Range_No INTEGER, Rate_No INTEGER, Rate_Type CHAR(1), Description VARCHAR(20), Authorised_Class_ID INT, Authorised_Class_Key VARCHAR(50), Table_Name CHAR(128), Available CHAR(1)); DECLARE @AuthCust Entity_No; DECLARE @AuthSite Entity_No; INSERT @ChargeAuthHeaders (Authorisation_Config_No, Authorised_Class_ID, Table_Name) SELECT AC.Authorisation_Config_No, AC.Authorised_Class_ID, OC.Table_Name FROM "DBA".SM_Authorisation_Config AC LEFT OUTER JOIN "DBA".SM_Object_Class OC ON (OC.Object_Class = AC.Authorised_Class_ID) --may not exist WHERE AC.Authorising_Class_ID = 3007 --product rate AND AC.Authorised_Process = 3002; --charge process IF @@ERROR <> 0 THEN RETURN; END IF; /* NOTE: The @AllowUnavailable flag will change filtering of Charge-type rates based on authorisations, validity dates and the "Available" flag. */ /* When set to 'Y', the procedure will return rates irrespective of authorisations, validity date settings or "Available" flag. */ /* When set to 'N', the procedure will return only rates that are authorised (optionally), flagged as "Available" and are valid for the supplied @TranDate. */ /* %MG NOTE 05/06/2006 - Rates of type other than 'C' are still subject to validity date checks when @AllowUnavailable = 'Y', to prevent multiples of the same rate from being returned */ IF @SpecificChargeNo IS NULL THEN -- Select all rates for the product. IF (@CheckAuthorisations = 'Y') AND (@AllowUnavailable = 'N') THEN -- Create a "shortlist" of available rates based on Available flag, Validity dates and Authorised entities INSERT @ChargeInfo (Range_No, Rate_No, Rate_Type, Description, Authorised_Class_ID, Authorised_Class_Key, Table_Name, Available) SELECT PR.Range_No, PR.Rate_No, R.Type, PR.Description, CAH.Authorised_Class_ID, AI.Authorised_Class_Key, CAH.Table_Name, PR.Available FROM "DBA".WM_Product_Rate PR INNER JOIN "DBA".WM_Rate R ON (R.Rate_No = PR.Rate_No) LEFT OUTER JOIN "DBA".SM_Authorisation_Item AI ON ((AI.Authorising_Class_Key = CAST(PR.Range_No AS CHAR)) AND (AI.Authorisation_Config_No IN (SELECT Authorisation_Config_No FROM @ChargeAuthHeaders))) LEFT OUTER JOIN @ChargeAuthHeaders CAH ON (CAH.Authorisation_Config_No = AI.Authorisation_Config_No) WHERE (PR.Product_No = @ProductNo) AND ((DATEDIFF(Day, PR.Valid_Start_Date, @TranDate) >= 0) OR (PR.Valid_Start_Date IS NULL)) AND ((DATEDIFF(Day, @TranDate, PR.Valid_End_Date) >= 0) OR (PR.Valid_End_Date IS NULL)) AND (PR.Available = 'Y') ELSE -- Not checking authorisations - just return the result set. -- Must be in order by net in descending order for DetermineBestRate to work correctly. SELECT PR.Product_No, PR.Rate_No, PR.Range_No, PR.Description, PR.Value, PR.Net, PR.Is_Default, PR.Charge_Type, PR.Is_Operator_Selected, PR.Is_Auto_Range, PR.Cost_Rule_No, PR.Minimum_Cost, PR.Min_Gross_Allowed, PR.Max_Gross_Allowed, PR.Valid_Start_Date, PR.Valid_End_Date, PR.User_Rate_Field1, PR.User_Rate_Field2, PR.User_Rate_Field3, PR.User_Rate_Field4, PR.Sub_Stream_2_No, PR.Memo, PR.Available, R.Type, R.Name, R.Is_GST, R.Rate_No AS Source_Rate_No FROM "DBA".WM_Product_Rate PR, "DBA".WM_Rate R WHERE PR.Rate_No = R.Rate_No AND PR.Product_No = @ProductNo AND ((DATEDIFF(Day, PR.Valid_Start_Date, @TranDate) >= 0) OR (PR.Valid_Start_Date IS NULL) OR ((@AllowUnavailable = 'Y') AND (R.Type = 'C'))) AND ((DATEDIFF(Day, @TranDate, PR.Valid_End_Date) >= 0) OR (PR.Valid_End_Date IS NULL) OR ((@AllowUnavailable = 'Y') AND (R.Type = 'C'))) AND ((PR.Available = 'Y') OR (@AllowUnavailable = 'Y')) ORDER BY PR.Net DESC END IF; ELSE -- Select specific rate. IF (@CheckAuthorisations = 'Y') AND (@AllowUnavailable = 'N') THEN -- Create a "shortlist" of available rates based on Available flag, Validity dates and Authorised entities -- If the specified rate has the auto range flag then return the auto range group that includes the rate. IF EXISTS( SELECT 1 FROM "DBA".WM_Product_Rate WHERE Product_No = @ProductNo AND ((Range_No = @SpecificChargeNo) AND (Is_Auto_Range = 'Y'))) THEN INSERT @ChargeInfo (Range_No, Rate_No, Rate_Type, Description, Authorised_Class_ID, Authorised_Class_Key, Table_Name, Available) SELECT PR.Range_No, PR.Rate_No, R.Type, PR.Description, CAH.Authorised_Class_ID, AI.Authorised_Class_Key, CAH.Table_Name, PR.Available FROM "DBA".WM_Product_Rate PR INNER JOIN "DBA".WM_Rate R ON (R.Rate_No = PR.Rate_No) LEFT OUTER JOIN "DBA".SM_Authorisation_Item AI ON ((AI.Authorising_Class_Key = CAST(PR.Range_No AS CHAR)) AND (AI.Authorisation_Config_No IN (SELECT Authorisation_Config_No FROM @ChargeAuthHeaders))) LEFT OUTER JOIN @ChargeAuthHeaders CAH ON (CAH.Authorisation_Config_No = AI.Authorisation_Config_No) WHERE (PR.Product_No = @ProductNo) AND (((R.Type = 'C') AND (PR.Is_Auto_Range = 'Y')) OR (R.Type <> 'C')) AND ((DATEDIFF(Day, PR.Valid_Start_Date, @TranDate) >= 0) OR (PR.Valid_Start_Date IS NULL)) AND ((DATEDIFF(Day, @TranDate, PR.Valid_End_Date) >= 0) OR (PR.Valid_End_Date IS NULL)) AND (PR.Available = 'Y') ELSE INSERT @ChargeInfo (Range_No, Rate_No, Rate_Type, Description, Authorised_Class_ID, Authorised_Class_Key, Table_Name, Available) SELECT PR.Range_No, PR.Rate_No, R.Type, PR.Description, CAH.Authorised_Class_ID, AI.Authorised_Class_Key, CAH.Table_Name, PR.Available FROM "DBA".WM_Product_Rate PR INNER JOIN "DBA".WM_Rate R ON (R.Rate_No = PR.Rate_No) LEFT OUTER JOIN "DBA".SM_Authorisation_Item AI ON ((AI.Authorising_Class_Key = CAST(PR.Range_No AS CHAR)) AND (AI.Authorisation_Config_No IN (SELECT Authorisation_Config_No FROM @ChargeAuthHeaders))) LEFT OUTER JOIN @ChargeAuthHeaders CAH ON (CAH.Authorisation_Config_No = AI.Authorisation_Config_No) WHERE (PR.Product_No = @ProductNo) AND ((PR.Range_No = @SpecificChargeNo) OR (R.Type <> 'C')) AND ((DATEDIFF(Day, PR.Valid_Start_Date, @TranDate) >= 0) OR (PR.Valid_Start_Date IS NULL)) AND ((DATEDIFF(Day, @TranDate, PR.Valid_End_Date) >= 0) OR (PR.Valid_End_Date IS NULL)) AND (PR.Available = 'Y') END IF; ELSE -- Not checking authorisations - just return the result set. -- Must be in order by net in descending order for DetermineBestRate to work correctly. -- If the specified rate has the auto range flag then return the auto range group that includes the rate. IF EXISTS( SELECT 1 FROM "DBA".WM_Product_Rate PR, "DBA".WM_Rate R WHERE PR.Rate_No = R.Rate_No AND PR.Product_No = @ProductNo AND ((PR.Range_No = @SpecificChargeNo) AND (PR.Is_Auto_Range = 'Y'))) THEN SELECT PR.Product_No, PR.Rate_No, PR.Range_No, PR.Description, PR.Value, PR.Net, PR.Is_Default, PR.Charge_Type, PR.Is_Operator_Selected, PR.Is_Auto_Range, PR.Cost_Rule_No, PR.Minimum_Cost, PR.Min_Gross_Allowed, PR.Max_Gross_Allowed, PR.Valid_Start_Date, PR.Valid_End_Date, PR.User_Rate_Field1, PR.User_Rate_Field2, PR.User_Rate_Field3, PR.User_Rate_Field4, PR.Sub_Stream_2_No, PR.Memo, PR.Available, R.Type, R.Name, R.Is_GST, R.Rate_No AS Source_Rate_No FROM "DBA".WM_Product_Rate PR, "DBA".WM_Rate R WHERE PR.Rate_No = R.Rate_No AND PR.Product_No = @ProductNo AND ((R.Type = 'C' AND PR.Is_Auto_Range = 'Y') OR (R.Type <> 'C')) AND ((DATEDIFF(Day, PR.Valid_Start_Date, @TranDate) >= 0) OR (PR.Valid_Start_Date IS NULL) OR ((@AllowUnavailable = 'Y') AND (R.Type = 'C'))) AND ((DATEDIFF(Day, @TranDate, PR.Valid_End_Date) >= 0) OR (PR.Valid_End_Date IS NULL) OR ((@AllowUnavailable = 'Y') AND (R.Type = 'C'))) AND ((PR.Available = 'Y') OR (@AllowUnavailable = 'Y')) ORDER BY PR.Net DESC ELSE SELECT PR.Product_No, PR.Rate_No, PR.Range_No, PR.Description, PR.Value, PR.Net, PR.Is_Default, PR.Charge_Type, PR.Is_Operator_Selected, PR.Is_Auto_Range, PR.Cost_Rule_No, PR.Minimum_Cost, PR.Min_Gross_Allowed, PR.Max_Gross_Allowed, PR.Valid_Start_Date, PR.Valid_End_Date, PR.User_Rate_Field1, PR.User_Rate_Field2, PR.User_Rate_Field3, PR.User_Rate_Field4, PR.Sub_Stream_2_No, PR.Memo, PR.Available, R.Type, R.Name, R.Is_GST, R.Rate_No AS Source_Rate_No FROM "DBA".WM_Product_Rate PR, "DBA".WM_Rate R WHERE PR.Rate_No = R.Rate_No AND PR.Product_No = @ProductNo AND ((PR.Range_No = @SpecificChargeNo) OR (R.Type <> 'C')) AND ((DATEDIFF(Day, PR.Valid_Start_Date, @TranDate) >= 0) OR (PR.Valid_Start_Date IS NULL) OR ((@AllowUnavailable = 'Y') AND (R.Type = 'C'))) AND ((DATEDIFF(Day, @TranDate, PR.Valid_End_Date) >= 0) OR (PR.Valid_End_Date IS NULL) OR ((@AllowUnavailable = 'Y') AND (R.Type = 'C'))) AND ((PR.Available = 'Y') OR (@AllowUnavailable = 'Y')) END IF; END IF; END IF; /* If authorisation service and relevant relationships are enabled, and authorisation entities */ /* are supplied, filter the rate list using the supplied entities */ IF (@CheckAuthorisations = 'Y') AND (@AllowUnavailable = 'N') THEN -- Get Cust and/or Site authorised SELECT Authed_Class_Key INTO @AuthCust FROM #AuthedItems WHERE Authed_Class_ID = 3001; SELECT Authed_Class_Key INTO @AuthSite FROM #AuthedItems WHERE Authed_Class_ID = 0100; -- If site specified, delete all charges that authorise sites other than that specified, -- and do not also authorise the site. -- If site not specified, ASSUME THAT SITE AUTHORISATION IS NOT ENABLED, i.e. do NOT check site authorisations. -- It is the caller's responsibility to supply a site number if that authorisation is to be checked. -- NOTE: If supplied site no is -1, treat as unknown, i.e. only charges authorising all sites will be returned IF (@AuthSite IS NOT NULL) OR (@AuthSite = '-1') THEN DELETE @ChargeInfo WHERE Range_No IN (SELECT Range_No FROM @ChargeInfo WHERE Authorised_Class_ID = 0100 AND Authorised_Class_Key <> @AuthSite AND Rate_Type = 'C') AND Range_No NOT IN (SELECT Range_No FROM @ChargeInfo WHERE Authorised_Class_ID = 0100 AND Authorised_Class_Key = @AuthSite AND Rate_Type = 'C') END IF; -- If customer specified and one or more charges in the group explicitly authorise that customer, -- delete all charges that do NOT explicitly authorise the customer. (This includes charges that do not authorise any customers.) -- If customer specified and none of the charges explicitly authorise that customer, -- delete all charges that explictly authorise any customer. This will leave only those charges that do not explicitly authorise -- any customers. -- If customer not specified, ASSUME THAT CUSTOMER AUTHORISATION IS NOT ENABLED, i.e. do NOT check customer authorisations. -- It is the caller's responsibility to supply a customer number if that authorisation is to be checked. -- NOTE: If supplied customer no is -1, treat as unknown, i.e. only charges not authorising a customer will be returned IF (@AuthCust IS NOT NULL) OR (@AuthCust = '-1') THEN IF EXISTS( SELECT 1 FROM @ChargeInfo WHERE Authorised_Class_ID = 3001 AND Authorised_Class_Key = @AuthCust AND Rate_Type = 'C') THEN --Delete all charges that do not explicitly authorise this customer DELETE @ChargeInfo WHERE Rate_Type = 'C' AND Range_No NOT IN (SELECT Range_No FROM @ChargeInfo WHERE Authorised_Class_ID = 3001 AND Authorised_Class_Key = @AuthCust AND Rate_Type = 'C') ELSE --Delete all charges that explicitly authorise any customer DELETE @ChargeInfo WHERE Range_No IN (SELECT Range_No FROM @ChargeInfo WHERE Authorised_Class_ID = 3001 AND Rate_Type = 'C') END IF; END IF; -- RETURN THE RESULT SET -- NOTE: Must be in order by Net in descending order for DetermineBestRate to work correctly. SELECT PR.Product_No, PR.Rate_No, PR.Range_No, PR.Description, PR.Value, PR.Net, PR.Is_Default, PR.Charge_Type, PR.Is_Operator_Selected, PR.Is_Auto_Range, PR.Cost_Rule_No, PR.Minimum_Cost, PR.Min_Gross_Allowed, PR.Max_Gross_Allowed, PR.Valid_Start_Date, PR.Valid_End_Date, PR.User_Rate_Field1, PR.User_Rate_Field2, PR.User_Rate_Field3, PR.User_Rate_Field4, PR.Sub_Stream_2_No, PR.Memo, PR.Available, R.Type, R.Name, R.Is_GST, R.Rate_No AS Source_Rate_No FROM "DBA".WM_Product_Rate PR, "DBA".WM_Rate R WHERE (PR.Rate_No = R.Rate_No) AND (PR.Range_No IN (SELECT Range_No FROM @ChargeInfo)) ORDER BY PR.Net DESC, PR.Description; END IF; END; --Update remote version control UPDATE "DBA".SM_Version_Control SET Version_No = 7 WHERE Entity_Name = 'WM_GetProductCharges' AND Database_ID = (SELECT db_property('GlobalDbID')); PASSTHROUGH STOP; END IF; --Update consolidated version control UPDATE "DBA".SM_Version_Control SET Version_No = 7 WHERE Entity_Name = 'WM_GetProductCharges' AND Database_ID = (SELECT db_property('GlobalDbID')); END IF; /********************************************************************/ /* 29/06/2006 - Modify result set of WM_GetProductChargesAuth as */ /* Is_GST is now returned by WM_GetProductCharges */ /********************************************************************/ IF EXISTS( SELECT 1 FROM "DBA".SM_Version_Control WHERE Entity_Name = 'WM_GetProductChargesAuth' AND Database_ID = (SELECT db_property('GlobalDbID')) AND Version_No < 2) THEN ALTER PROCEDURE "DBA".WM_GetProductChargesAuth(IN @ProductNo Entity_No, IN @TranDate Entity_Date, IN @SpecificChargeNo Entity_No, IN @SpecificChargeType CHAR(1), IN @SiteNo Entity_No, IN @CustomerNo Entity_No, IN @AllowUnavailable Boolean DEFAULT 'N') RESULT( Product_No Entity_No, Rate_No Entity_No, Range_No Entity_No, Description varchar(20), Value numeric(10,4), Net Weight, Is_Default Boolean, Charge_Type Prod_Cost_Method, Is_Operator_Selected Boolean, Is_Auto_Range Boolean, Cost_Rule_No Entity_No, Minimum_Cost numeric(10,4), Min_Gross_Allowed Weight, Max_Gross_Allowed Weight, Valid_Start_Date Entity_Date, Valid_End_Date Entity_Date, User_Rate_Field1 numeric(10,4), User_Rate_Field2 numeric(10,4), User_Rate_Field3 numeric(10,4), User_Rate_Field4 numeric(10,4), Sub_Stream_2_No integer, Memo Entity_Comment, Available Boolean, Type char(1), Name Entity_Name, Is_GST Boolean, Source_Rate_No Entity_No ) BEGIN DECLARE LOCAL TEMPORARY TABLE #AuthedItems( Authed_Class_ID INT, Authed_Class_Key VARCHAR(50)); --Transaction Value: Site IF @SiteNo IS NOT NULL THEN INSERT #AuthedItems(Authed_Class_ID, Authed_Class_Key) VALUES('0100', CAST(@SiteNo AS CHAR)); END IF; --Transaction Value: Customer IF @CustomerNo IS NOT NULL THEN INSERT #AuthedItems(Authed_Class_ID, Authed_Class_Key) VALUES('3001', CAST(@CustomerNo AS CHAR)); END IF; CALL "DBA".WM_GetProductCharges (@ProductNo, @TranDate, @SpecificChargeNo, @SpecificChargeType, 'Y', @AllowUnavailable); DROP TABLE #AuthedItems; END; IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; /* NOTE "ONLY" keyword prevents the update from being applied at the consolidated node */ ALTER PROCEDURE "DBA".WM_GetProductChargesAuth(IN @ProductNo Entity_No, IN @TranDate Entity_Date, IN @SpecificChargeNo Entity_No, IN @SpecificChargeType CHAR(1), IN @SiteNo Entity_No, IN @CustomerNo Entity_No, IN @AllowUnavailable Boolean DEFAULT 'N') RESULT( Product_No Entity_No, Rate_No Entity_No, Range_No Entity_No, Description varchar(20), Value numeric(10,4), Net Weight, Is_Default Boolean, Charge_Type Prod_Cost_Method, Is_Operator_Selected Boolean, Is_Auto_Range Boolean, Cost_Rule_No Entity_No, Minimum_Cost numeric(10,4), Min_Gross_Allowed Weight, Max_Gross_Allowed Weight, Valid_Start_Date Entity_Date, Valid_End_Date Entity_Date, User_Rate_Field1 numeric(10,4), User_Rate_Field2 numeric(10,4), User_Rate_Field3 numeric(10,4), User_Rate_Field4 numeric(10,4), Sub_Stream_2_No integer, Memo Entity_Comment, Available Boolean, Type char(1), Name Entity_Name, Is_GST Boolean, Source_Rate_No Entity_No ) BEGIN DECLARE LOCAL TEMPORARY TABLE #AuthedItems( Authed_Class_ID INT, Authed_Class_Key VARCHAR(50)); --Transaction Value: Site IF @SiteNo IS NOT NULL THEN INSERT #AuthedItems(Authed_Class_ID, Authed_Class_Key) VALUES('0100', CAST(@SiteNo AS CHAR)); END IF; --Transaction Value: Customer IF @CustomerNo IS NOT NULL THEN INSERT #AuthedItems(Authed_Class_ID, Authed_Class_Key) VALUES('3001', CAST(@CustomerNo AS CHAR)); END IF; CALL "DBA".WM_GetProductCharges (@ProductNo, @TranDate, @SpecificChargeNo, @SpecificChargeType, 'Y', @AllowUnavailable); DROP TABLE #AuthedItems; END; --Update remote version control UPDATE "DBA".SM_Version_Control SET Version_No = 2 WHERE Entity_Name = 'WM_GetProductChargesAuth' AND Database_ID = (SELECT db_property('GlobalDbID')); PASSTHROUGH STOP; END IF; --Update consolidated version control UPDATE "DBA".SM_Version_Control SET Version_No = 2 WHERE Entity_Name = 'WM_GetProductChargesAuth' AND Database_ID = (SELECT db_property('GlobalDbID')); END IF; /********************************************************************/ /* 29/06/2006 - Modify WM_MD_GetRatesForExport to return Is_GST flag from Rate record */ /********************************************************************/ IF NOT EXISTS( SELECT 1 FROM "DBA".SM_Version_Control WHERE Entity_Name = 'WM_MD_GetRatesForExport' AND Database_ID = (SELECT db_property('GlobalDbID'))) THEN ALTER PROCEDURE "DBA".WM_MD_GetRatesForExport(IN @ProductNo Entity_No, IN @DeviceSiteNo Entity_No, IN @ExportDate DATE DEFAULT CURRENT DATE) RESULT( Product_No Entity_No, Rate_No Entity_No, Range_No Entity_No, Description varchar(20), Value numeric(10,4), Net Weight, Is_Default Boolean, Charge_Type Prod_Cost_Method, Is_Operator_Selected Boolean, Is_Auto_Range Boolean, Cost_Rule_No Entity_No, Minimum_Cost numeric(10,4), Min_Gross_Allowed Weight, Max_Gross_Allowed Weight, User_Rate_Field1 numeric(10,4), User_Rate_Field2 numeric(10,4), User_Rate_Field3 numeric(10,4), User_Rate_Field4 numeric(10,4), Type char(1), Name Entity_Name, Is_GST Boolean, Source_Rate_No Entity_No ) BEGIN DECLARE LOCAL TEMPORARY TABLE @ChargeAuthHeaders( Authorisation_Config_No INTEGER, Authorised_Class_ID INTEGER, Table_Name CHAR(128)); --Declare local temp table to hold result set while processing DECLARE LOCAL TEMPORARY TABLE @ChargeInfo( Range_No INTEGER, Rate_No INTEGER, Rate_Type CHAR(1), Description VARCHAR(20), Authorised_Class_ID INT, Authorised_Class_Key VARCHAR(50), Table_Name CHAR(128), Available CHAR(1)); IF @ProductNo IS NULL THEN RAISERROR 30000 'Product No not supplied'; RETURN; END IF; INSERT @ChargeAuthHeaders (Authorisation_Config_No, Authorised_Class_ID, Table_Name) SELECT AC.Authorisation_Config_No, AC.Authorised_Class_ID, OC.Table_Name FROM "DBA".SM_Authorisation_Config AC LEFT OUTER JOIN "DBA".SM_Object_Class OC ON (OC.Object_Class = AC.Authorised_Class_ID) --may not exist WHERE AC.Authorising_Class_ID = 3007 --product rate AND AC.Authorised_Process = 3002 --charge process AND AC.Is_Enabled = 'Y'; IF @@ERROR <> 0 THEN RETURN; END IF; -- Create a "shortlist" of available rates based on Available flag, Validity dates and Authorised entities INSERT @ChargeInfo (Range_No, Rate_No, Rate_Type, Description, Authorised_Class_ID, Authorised_Class_Key, Table_Name, Available) SELECT PR.Range_No, PR.Rate_No, R.Type, PR.Description, CAH.Authorised_Class_ID, AI.Authorised_Class_Key, CAH.Table_Name, PR.Available FROM "DBA".WM_Product_Rate PR INNER JOIN "DBA".WM_Rate R ON (R.Rate_No = PR.Rate_No) LEFT OUTER JOIN "DBA".SM_Authorisation_Item AI ON ((AI.Authorising_Class_Key = CAST(PR.Range_No AS CHAR)) AND (AI.Authorisation_Config_No IN (SELECT Authorisation_Config_No FROM @ChargeAuthHeaders))) LEFT OUTER JOIN @ChargeAuthHeaders CAH ON (CAH.Authorisation_Config_No = AI.Authorisation_Config_No) WHERE (PR.Product_No = @ProductNo) AND (((R.Type = 'C') AND (PR.Charge_Type IN ('N','C'))) --Non-weighed types only OR (R.Type <> 'C')) AND ((DATEDIFF(Day, PR.Valid_Start_Date, @ExportDate) >= 0) OR (PR.Valid_Start_Date IS NULL)) AND ((DATEDIFF(Day, @ExportDate, PR.Valid_End_Date) >= 0) OR (PR.Valid_End_Date IS NULL)) AND (PR.Available = 'Y'); --If Device Site No is specified, delete all charges that authorise sites other than that specified, and do not also authorise the site IF @DeviceSiteNo IS NOT NULL THEN DELETE @ChargeInfo WHERE Range_No IN (SELECT Range_No FROM @ChargeInfo WHERE Authorised_Class_ID = 0100 AND Authorised_Class_Key <> @DeviceSiteNo AND Rate_Type = 'C') AND Range_No NOT IN (SELECT Range_No FROM @ChargeInfo WHERE Authorised_Class_ID = 0100 AND Authorised_Class_Key = @DeviceSiteNo AND Rate_Type = 'C'); END IF; --Delete all charges that explicitly authorise a customer --Customer-specific rates will be excluded from the export to the Mobile Device DELETE @ChargeInfo WHERE Range_No IN (SELECT Range_No FROM @ChargeInfo WHERE Authorised_Class_ID = 3001 AND Rate_Type = 'C'); -- RETURN THE RESULT SET SELECT PR.Product_No, PR.Rate_No, PR.Range_No, PR.Description, PR.Value, PR.Net, PR.Is_Default, PR.Charge_Type, PR.Is_Operator_Selected, PR.Is_Auto_Range, PR.Cost_Rule_No, PR.Minimum_Cost, PR.Min_Gross_Allowed, PR.Max_Gross_Allowed, PR.User_Rate_Field1, PR.User_Rate_Field2, PR.User_Rate_Field3, PR.User_Rate_Field4, R.Type, R.Name, R.Is_GST, R.Rate_No AS Source_Rate_No FROM "DBA".WM_Product_Rate PR INNER JOIN "DBA".WM_Rate R ON (R.Rate_No = PR.Rate_No) WHERE Range_No IN (SELECT Range_No FROM @ChargeInfo); END; IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; /* NOTE "ONLY" keyword prevents the update from being applied at the consolidated node */ ALTER PROCEDURE "DBA".WM_MD_GetRatesForExport(IN @ProductNo Entity_No, IN @DeviceSiteNo Entity_No, IN @ExportDate DATE DEFAULT CURRENT DATE) RESULT( Product_No Entity_No, Rate_No Entity_No, Range_No Entity_No, Description varchar(20), Value numeric(10,4), Net Weight, Is_Default Boolean, Charge_Type Prod_Cost_Method, Is_Operator_Selected Boolean, Is_Auto_Range Boolean, Cost_Rule_No Entity_No, Minimum_Cost numeric(10,4), Min_Gross_Allowed Weight, Max_Gross_Allowed Weight, User_Rate_Field1 numeric(10,4), User_Rate_Field2 numeric(10,4), User_Rate_Field3 numeric(10,4), User_Rate_Field4 numeric(10,4), Type char(1), Name Entity_Name, Is_GST Boolean, Source_Rate_No Entity_No ) BEGIN DECLARE LOCAL TEMPORARY TABLE @ChargeAuthHeaders( Authorisation_Config_No INTEGER, Authorised_Class_ID INTEGER, Table_Name CHAR(128)); --Declare local temp table to hold result set while processing DECLARE LOCAL TEMPORARY TABLE @ChargeInfo( Range_No INTEGER, Rate_No INTEGER, Rate_Type CHAR(1), Description VARCHAR(20), Authorised_Class_ID INT, Authorised_Class_Key VARCHAR(50), Table_Name CHAR(128), Available CHAR(1)); IF @ProductNo IS NULL THEN RAISERROR 30000 'Product No not supplied'; RETURN; END IF; INSERT @ChargeAuthHeaders (Authorisation_Config_No, Authorised_Class_ID, Table_Name) SELECT AC.Authorisation_Config_No, AC.Authorised_Class_ID, OC.Table_Name FROM "DBA".SM_Authorisation_Config AC LEFT OUTER JOIN "DBA".SM_Object_Class OC ON (OC.Object_Class = AC.Authorised_Class_ID) --may not exist WHERE AC.Authorising_Class_ID = 3007 --product rate AND AC.Authorised_Process = 3002 --charge process AND AC.Is_Enabled = 'Y'; IF @@ERROR <> 0 THEN RETURN; END IF; -- Create a "shortlist" of available rates based on Available flag, Validity dates and Authorised entities INSERT @ChargeInfo (Range_No, Rate_No, Rate_Type, Description, Authorised_Class_ID, Authorised_Class_Key, Table_Name, Available) SELECT PR.Range_No, PR.Rate_No, R.Type, PR.Description, CAH.Authorised_Class_ID, AI.Authorised_Class_Key, CAH.Table_Name, PR.Available FROM "DBA".WM_Product_Rate PR INNER JOIN "DBA".WM_Rate R ON (R.Rate_No = PR.Rate_No) LEFT OUTER JOIN "DBA".SM_Authorisation_Item AI ON ((AI.Authorising_Class_Key = CAST(PR.Range_No AS CHAR)) AND (AI.Authorisation_Config_No IN (SELECT Authorisation_Config_No FROM @ChargeAuthHeaders))) LEFT OUTER JOIN @ChargeAuthHeaders CAH ON (CAH.Authorisation_Config_No = AI.Authorisation_Config_No) WHERE (PR.Product_No = @ProductNo) AND (((R.Type = 'C') AND (PR.Charge_Type IN ('N','C'))) --Non-weighed types only OR (R.Type <> 'C')) AND ((DATEDIFF(Day, PR.Valid_Start_Date, @ExportDate) >= 0) OR (PR.Valid_Start_Date IS NULL)) AND ((DATEDIFF(Day, @ExportDate, PR.Valid_End_Date) >= 0) OR (PR.Valid_End_Date IS NULL)) AND (PR.Available = 'Y'); --If Device Site No is specified, delete all charges that authorise sites other than that specified, and do not also authorise the site IF @DeviceSiteNo IS NOT NULL THEN DELETE @ChargeInfo WHERE Range_No IN (SELECT Range_No FROM @ChargeInfo WHERE Authorised_Class_ID = 0100 AND Authorised_Class_Key <> @DeviceSiteNo AND Rate_Type = 'C') AND Range_No NOT IN (SELECT Range_No FROM @ChargeInfo WHERE Authorised_Class_ID = 0100 AND Authorised_Class_Key = @DeviceSiteNo AND Rate_Type = 'C'); END IF; --Delete all charges that explicitly authorise a customer --Customer-specific rates will be excluded from the export to the Mobile Device DELETE @ChargeInfo WHERE Range_No IN (SELECT Range_No FROM @ChargeInfo WHERE Authorised_Class_ID = 3001 AND Rate_Type = 'C'); -- RETURN THE RESULT SET SELECT PR.Product_No, PR.Rate_No, PR.Range_No, PR.Description, PR.Value, PR.Net, PR.Is_Default, PR.Charge_Type, PR.Is_Operator_Selected, PR.Is_Auto_Range, PR.Cost_Rule_No, PR.Minimum_Cost, PR.Min_Gross_Allowed, PR.Max_Gross_Allowed, PR.User_Rate_Field1, PR.User_Rate_Field2, PR.User_Rate_Field3, PR.User_Rate_Field4, R.Type, R.Name, R.Is_GST, R.Rate_No AS Source_Rate_No FROM "DBA".WM_Product_Rate PR INNER JOIN "DBA".WM_Rate R ON (R.Rate_No = PR.Rate_No) WHERE Range_No IN (SELECT Range_No FROM @ChargeInfo); END; --Add remote version control rec INSERT "DBA".SM_Version_Control(Entity_Name, Database_ID, Version_No) VALUES('WM_MD_GetRatesForExport', (SELECT db_property( 'GlobalDBId' )), 1); PASSTHROUGH STOP; END IF; --Add consolidated version control rec INSERT "DBA".SM_Version_Control(Entity_Name, Database_ID, Version_No) VALUES('WM_MD_GetRatesForExport', (SELECT db_property( 'GlobalDBId' )), 1); END IF; /********************************************************************/ /* 06/07/2006 - Modify CreateTranReversal proc to (1) return new */ /* tran details using OUT params (2) Raise exceptions when errors */ /* occur (3) Copy over Carrier_No, DateTime_In, DateTime_Out and */ /* Group_Gn fields from transaction header, and copy over tran exceptions */ /********************************************************************/ IF EXISTS( SELECT 1 FROM "DBA".SM_Version_Control WHERE Entity_Name = 'CreateTranReversal' AND Database_ID = (SELECT db_property('GlobalDbID')) AND Version_No < 7) THEN ALTER PROCEDURE "DBA".CreateTranReversal(IN @OriginalTranNo Large_Entity_No, IN @DockPrefix CHAR(3), IN @DockSuffix CHAR(3), IN @DockNo INTEGER, IN @DockVersion INTEGER, IN @CreatedAt Entity_No, IN @CreatedBy Operator_Code, IN @ShiftNo Entity_No, IN @Reason Entity_Comment, OUT @NewTranNo Large_Entity_No, OUT @NewDocketDesc VARCHAR(20)) BEGIN DECLARE @TotalPaid NUMERIC(8,2); DECLARE @OldDocketDesc VARCHAR(20); DECLARE LOCAL TEMPORARY TABLE Map_Tran_Items( oldTranItem Large_Entity_No, newTranitem Large_Entity_No, PRIMARY KEY (oldTranItem, newTranItem)); /* Raise error if the transaction is incomplete. */ /* Only completed transactions can be reversed with this procedure */ IF EXISTS( SELECT 1 FROM "DBA".WM_Transaction_Header WHERE Transaction_No = @OriginalTranNo AND Is_Completed = 'N') THEN RAISERROR 30000 'Cannot reverse an Incomplete Transaction'; RETURN; END IF; /* Raise error if the transaction to be reversed has already been reversed, */ /* or is a reversal itself */ IF EXISTS( SELECT 1 FROM "DBA".WM_Transaction_Header WHERE Transaction_No = @OriginalTranNo AND (Original_Tran_No IS NOT NULL OR Reversal_Tran_No IS NOT NULL)) THEN RAISERROR 30000 'Cannot reverse a Transaction that has already been reversed or is a reversal itself'; RETURN; END IF; --Get the docket number of the reversed tran (used when adding tran exception) SELECT Docket INTO @OldDocketDesc FROM "DBA".WM_Transaction_Header WHERE Transaction_No = @OriginalTranNo; --Insert the reversal transaction INSERT "DBA".WM_Transaction_Header( Transaction_No, Vehicle_No, Vehicle_Code, Customer_No, Carrier_No, Job_No, EPA_Approval_No, Docket_No_Prefix, Docket_No, Docket_No_Suffix, Docket_No_Version, Date_Complete, Time_Complete, DateTime_In, DateTime_Out, Created_At, Created_By, Shift_No, Gross, Tare, Net, Gross_Date, Gross_Time, Tare_Date, Tare_Time, Order_no, Transaction_Amount, Amount_Paid, Is_Completed, Is_Stored_Tare, Is_Collection_Vehicle, Memo, Print_Counter, Original_Tran_No, Custom_Data1, Replication_Site_Flag, Group_G0, Group_G1, Group_G2, Group_G3, Group_G4) SELECT NULL, Vehicle_No, Vehicle_Code, Customer_No, Carrier_No, Job_No, EPA_Approval_No, @DockPrefix, @DockNo, @DockSuffix, @DockVersion, CURRENT DATE, CURRENT TIME, DateTime_In, DateTime_Out, Created_At, @CreatedBy, @ShiftNo, -Gross, -Tare, -Net, Gross_Date, Gross_Time, Tare_Date, Tare_Time, Order_no, 0, /* note must use zero initially until items are inserted */ 0, /* note must use zero initially until allocations are inserted */ Is_Completed, Is_Stored_Tare, Is_Collection_Vehicle, Memo, 0, Transaction_No, /* Save reversed transaction no as "Original Transaction No" */ Custom_Data1, Replication_Site_Flag, Group_G0, Group_G1, Group_G2, Group_G3, Group_G4 FROM "DBA".WM_Transaction_Header WHERE Transaction_No = @OriginalTranNo; SELECT @@identity INTO @NewTranNo; /* Get the new transaction number */ --Get the new docket no in same format as computed column SELECT Docket INTO @NewDocketDesc FROM "DBA".WM_Transaction_Header WHERE Transaction_No = @NewTranNo; --Set the link to the new transaction on the old transaction UPDATE "DBA".WM_Transaction_Header SET Reversal_Tran_No = @NewTranNo WHERE Transaction_No = @OriginalTranNo; INSERT "DBA".WM_Transaction_Item( Transaction_No, Transaction_Item_No, Area_No, Product_No, Waste_Stream_No, Sub_Stream_1_No, Sub_Stream_2_No, Sub_Stream_3_No, Gross, Tare, Net, Cubic_Metres, Quantity, LGA_code, Cost_Rule_No, Rate_Range_No, Item_Charge, Cost_Method, EPA_Levy_Exempt) SELECT @NewTranNo, NULL, Area_No, Product_No, Waste_Stream_No, Sub_Stream_1_No, Sub_Stream_2_No, Sub_Stream_3_No, -Gross, -Tare, -Net, -Cubic_Metres, -Quantity, LGA_code, Cost_Rule_No, Rate_Range_No, 0, /* note must use zero initially until rates are inserted */ Cost_Method, EPA_Levy_Exempt FROM "DBA".WM_Transaction_Item WHERE Transaction_No = @OriginalTranNo ORDER BY Transaction_No, Transaction_Item_No; CALL "DBA".MapTranItems(@OriginalTranNo, @NewTranNo); INSERT "DBA".WM_Transaction_Item_Rate( Transaction_No, Transaction_Item_No, Rate_No, Rate, Value) SELECT @NewTranNo, newTranItem, Rate_No, Rate, -Value FROM "DBA".WM_Transaction_Item_Rate, Map_Tran_Items WHERE (Transaction_No = @OriginalTranNo) AND (Transaction_Item_No = oldTranItem) ORDER BY Transaction_No, Transaction_Item_No; INSERT "DBA".WM_Transaction_Attach( Transaction_No, Attachment_No, Tare, Quantity) SELECT @NewTranNo, Attachment_No, -Tare, Quantity FROM "DBA".WM_Transaction_Attach WHERE Transaction_No = @OriginalTranNo; INSERT "DBA".WM_Transaction_Exception( Transaction_No, Exception_No, Exception_Comment, Exception_Date, Exception_Time, Operator_Code) SELECT @NewTranNo, Exception_No, Exception_Comment, Exception_Date, Exception_Time, Operator_Code FROM "DBA".WM_Transaction_Exception WHERE Transaction_No = @OriginalTranNo; /* Now all Items and Rates are inserted, can set the Transaction Charges on the reversal tran */ UPDATE "DBA".WM_Transaction_Item New_Item SET Item_Charge = (SELECT -Item_Charge FROM "DBA".WM_Transaction_Item Old_Item INNER JOIN Map_Tran_Items ON (Transaction_Item_No = Map_Tran_Items.OldTranItem) WHERE Transaction_No = @OriginalTranNo AND Map_Tran_Items.NewTranItem = New_Item.Transaction_Item_No) WHERE New_Item.Transaction_No = @NewTranNo; UPDATE "DBA".WM_Transaction_Header SET Transaction_Amount = (SELECT -Transaction_Amount FROM "DBA".WM_Transaction_Header WHERE Transaction_No = @OriginalTranNo) WHERE Transaction_No = @NewTranNo; /* Add relevant exceptions to the transaction and its reversal */ INSERT "DBA".WM_Transaction_Exception(Transaction_No, Operator_Code, Exception_No, Exception_Comment) VALUES(@OriginalTranNo, @CreatedBy, (SELECT Exception_No FROM "DBA".WM_Exceptions WHERE Code = 'R'), 'Reason: ' || @Reason || '. Reversed by Docket ' || @NewDocketDesc); /* 'R' = reversed transaction */ INSERT "DBA".WM_Transaction_Exception(Transaction_No, Operator_Code, Exception_No, Exception_Comment) VALUES(@NewTranNo, @CreatedBy, (SELECT Exception_No FROM "DBA".WM_Exceptions WHERE Code = 'S'), 'Reason: ' || @Reason || '. Reversal of Docket ' || @OldDocketDesc); /* 'S' = transaction reversal */ END; IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; /* NOTE "ONLY" keyword prevents the update from being applied at the consolidated node */ ALTER PROCEDURE "DBA".CreateTranReversal(IN @OriginalTranNo Large_Entity_No, IN @DockPrefix CHAR(3), IN @DockSuffix CHAR(3), IN @DockNo INTEGER, IN @DockVersion INTEGER, IN @CreatedAt Entity_No, IN @CreatedBy Operator_Code, IN @ShiftNo Entity_No, IN @Reason Entity_Comment, OUT @NewTranNo Large_Entity_No, OUT @NewDocketDesc VARCHAR(20)) BEGIN DECLARE @TotalPaid NUMERIC(8,2); DECLARE @OldDocketDesc VARCHAR(20); DECLARE LOCAL TEMPORARY TABLE Map_Tran_Items( oldTranItem Large_Entity_No, newTranitem Large_Entity_No, PRIMARY KEY (oldTranItem, newTranItem)); /* Raise error if the transaction is incomplete. */ /* Only completed transactions can be reversed with this procedure */ IF EXISTS( SELECT 1 FROM "DBA".WM_Transaction_Header WHERE Transaction_No = @OriginalTranNo AND Is_Completed = 'N') THEN RAISERROR 30000 'Cannot reverse an Incomplete Transaction'; RETURN; END IF; /* Raise error if the transaction to be reversed has already been reversed, */ /* or is a reversal itself */ IF EXISTS( SELECT 1 FROM "DBA".WM_Transaction_Header WHERE Transaction_No = @OriginalTranNo AND (Original_Tran_No IS NOT NULL OR Reversal_Tran_No IS NOT NULL)) THEN RAISERROR 30000 'Cannot reverse a Transaction that has already been reversed or is a reversal itself'; RETURN; END IF; --Get the docket number of the reversed tran (used when adding tran exception) SELECT Docket INTO @OldDocketDesc FROM "DBA".WM_Transaction_Header WHERE Transaction_No = @OriginalTranNo; --Insert the reversal transaction INSERT "DBA".WM_Transaction_Header( Transaction_No, Vehicle_No, Vehicle_Code, Customer_No, Carrier_No, Job_No, EPA_Approval_No, Docket_No_Prefix, Docket_No, Docket_No_Suffix, Docket_No_Version, Date_Complete, Time_Complete, DateTime_In, DateTime_Out, Created_At, Created_By, Shift_No, Gross, Tare, Net, Gross_Date, Gross_Time, Tare_Date, Tare_Time, Order_no, Transaction_Amount, Amount_Paid, Is_Completed, Is_Stored_Tare, Is_Collection_Vehicle, Memo, Print_Counter, Original_Tran_No, Custom_Data1, Replication_Site_Flag, Group_G0, Group_G1, Group_G2, Group_G3, Group_G4) SELECT NULL, Vehicle_No, Vehicle_Code, Customer_No, Carrier_No, Job_No, EPA_Approval_No, @DockPrefix, @DockNo, @DockSuffix, @DockVersion, CURRENT DATE, CURRENT TIME, DateTime_In, DateTime_Out, Created_At, @CreatedBy, @ShiftNo, -Gross, -Tare, -Net, Gross_Date, Gross_Time, Tare_Date, Tare_Time, Order_no, 0, /* note must use zero initially until items are inserted */ 0, /* note must use zero initially until allocations are inserted */ Is_Completed, Is_Stored_Tare, Is_Collection_Vehicle, Memo, 0, Transaction_No, /* Save reversed transaction no as "Original Transaction No" */ Custom_Data1, Replication_Site_Flag, Group_G0, Group_G1, Group_G2, Group_G3, Group_G4 FROM "DBA".WM_Transaction_Header WHERE Transaction_No = @OriginalTranNo; SELECT @@identity INTO @NewTranNo; /* Get the new transaction number */ --Get the new docket no in same format as computed column SELECT Docket INTO @NewDocketDesc FROM "DBA".WM_Transaction_Header WHERE Transaction_No = @NewTranNo; --Set the link to the new transaction on the old transaction UPDATE "DBA".WM_Transaction_Header SET Reversal_Tran_No = @NewTranNo WHERE Transaction_No = @OriginalTranNo; INSERT "DBA".WM_Transaction_Item( Transaction_No, Transaction_Item_No, Area_No, Product_No, Waste_Stream_No, Sub_Stream_1_No, Sub_Stream_2_No, Sub_Stream_3_No, Gross, Tare, Net, Cubic_Metres, Quantity, LGA_code, Cost_Rule_No, Rate_Range_No, Item_Charge, Cost_Method, EPA_Levy_Exempt) SELECT @NewTranNo, NULL, Area_No, Product_No, Waste_Stream_No, Sub_Stream_1_No, Sub_Stream_2_No, Sub_Stream_3_No, -Gross, -Tare, -Net, -Cubic_Metres, -Quantity, LGA_code, Cost_Rule_No, Rate_Range_No, 0, /* note must use zero initially until rates are inserted */ Cost_Method, EPA_Levy_Exempt FROM "DBA".WM_Transaction_Item WHERE Transaction_No = @OriginalTranNo ORDER BY Transaction_No, Transaction_Item_No; CALL "DBA".MapTranItems(@OriginalTranNo, @NewTranNo); INSERT "DBA".WM_Transaction_Item_Rate( Transaction_No, Transaction_Item_No, Rate_No, Rate, Value) SELECT @NewTranNo, newTranItem, Rate_No, Rate, -Value FROM "DBA".WM_Transaction_Item_Rate, Map_Tran_Items WHERE (Transaction_No = @OriginalTranNo) AND (Transaction_Item_No = oldTranItem) ORDER BY Transaction_No, Transaction_Item_No; INSERT "DBA".WM_Transaction_Attach( Transaction_No, Attachment_No, Tare, Quantity) SELECT @NewTranNo, Attachment_No, -Tare, Quantity FROM "DBA".WM_Transaction_Attach WHERE Transaction_No = @OriginalTranNo; INSERT "DBA".WM_Transaction_Exception( Transaction_No, Exception_No, Exception_Comment, Exception_Date, Exception_Time, Operator_Code) SELECT @NewTranNo, Exception_No, Exception_Comment, Exception_Date, Exception_Time, Operator_Code FROM "DBA".WM_Transaction_Exception WHERE Transaction_No = @OriginalTranNo; /* Now all Items and Rates are inserted, can set the Transaction Charges on the reversal tran */ UPDATE "DBA".WM_Transaction_Item New_Item SET Item_Charge = (SELECT -Item_Charge FROM "DBA".WM_Transaction_Item Old_Item INNER JOIN Map_Tran_Items ON (Transaction_Item_No = Map_Tran_Items.OldTranItem) WHERE Transaction_No = @OriginalTranNo AND Map_Tran_Items.NewTranItem = New_Item.Transaction_Item_No) WHERE New_Item.Transaction_No = @NewTranNo; UPDATE "DBA".WM_Transaction_Header SET Transaction_Amount = (SELECT -Transaction_Amount FROM "DBA".WM_Transaction_Header WHERE Transaction_No = @OriginalTranNo) WHERE Transaction_No = @NewTranNo; /* Add relevant exceptions to the transaction and its reversal */ INSERT "DBA".WM_Transaction_Exception(Transaction_No, Operator_Code, Exception_No, Exception_Comment) VALUES(@OriginalTranNo, @CreatedBy, (SELECT Exception_No FROM "DBA".WM_Exceptions WHERE Code = 'R'), 'Reason: ' || @Reason || '. Reversed by Docket ' || @NewDocketDesc); /* 'R' = reversed transaction */ INSERT "DBA".WM_Transaction_Exception(Transaction_No, Operator_Code, Exception_No, Exception_Comment) VALUES(@NewTranNo, @CreatedBy, (SELECT Exception_No FROM "DBA".WM_Exceptions WHERE Code = 'S'), 'Reason: ' || @Reason || '. Reversal of Docket ' || @OldDocketDesc); /* 'S' = transaction reversal */ END; --Update remote version control UPDATE "DBA".SM_Version_Control SET Version_No = 7 WHERE Entity_Name = 'CreateTranReversal' AND Database_ID = (SELECT db_property('GlobalDbID')); PASSTHROUGH STOP; END IF; --Update consolidated version control UPDATE "DBA".SM_Version_Control SET Version_No = 7 WHERE Entity_Name = 'CreateTranReversal' AND Database_ID = (SELECT db_property('GlobalDbID')); END IF; /********************************************************************/ /* 06/07/2006 - Modify ai_WM_Transaction_Item and au_WM_Transaction_Item */ /* so that the triggers do not attempt to set progress totals negative */ /********************************************************************/ IF EXISTS( SELECT 1 FROM "DBA".SM_Version_Control WHERE Entity_Name = 'ai_WM_Transaction_Item' AND Database_ID = (SELECT db_property('GlobalDbID')) AND Version_No < 3) THEN ALTER TRIGGER ai_WM_Transaction_Item AFTER INSERT ON "DBA".WM_Transaction_Item REFERENCING NEW AS New_Rec FOR EACH ROW BEGIN DECLARE @Total NUMERIC(10,4); DECLARE @ProductDirection integer; DECLARE @JobNo integer; DECLARE @PeriodEnd date; DECLARE @PeriodInterval integer; /* Check that the Item_Charge equals the sum of the Rates */ SELECT SUM(Value) INTO @Total FROM WM_Transaction_Item_Rate WHERE Transaction_No = new_Rec.Transaction_No AND Transaction_Item_No = new_Rec.Transaction_Item_No; IF @Total IS NULL THEN SET @Total = 0; END IF; IF @Total <> new_Rec.Item_Charge THEN RAISERROR 30000 'Total of Transaction Item Rates does not equal Item total'; END IF; /* If Product is a normal (i.e. not a Blended) product, update the Area Tonnage */ /* and Cubic Metre totals from the Item. */ /* If Product is a Blended Product, add records to the WM_Tran_Prod_Component table. */ /* Triggers on WM_Tran_Prod_Component will update the relevant Area records */ IF NOT EXISTS( SELECT 1 FROM "DBA".WM_Product WHERE Product_No = new_Rec.Product_No AND Is_Blended_Product = 'Y') THEN set @ProductDirection=(select(case Incoming_Product when 'Y' then 1 when 'N' then(0-1) end) from WM_Product where WM_Product.Product_No = New_Rec.Product_No); update WM_Area set Tonnes = Tonnes + (New_Rec.Net * @ProductDirection), Cubic_Metres = Cubic_Metres + (New_Rec.Cubic_Metres * @ProductDirection) where WM_Area.Area_No = new_Rec.Area_No; update WM_Area set Start_Date = current date where WM_Area.Area_No = new_Rec.Area_No and Start_Date is null; update WM_Area set First_Use_Date = current date where WM_Area.Area_No = new_Rec.Area_No and First_Use_Date is null; ELSE INSERT "DBA".WM_Tran_Prod_Component (Transaction_No, Transaction_Item_No, Sub_Product_No, Percentage_Of_Blend, Sub_Prod_Net, Sub_Prod_Cubic_Metres) SELECT new_Rec.Transaction_No, new_Rec.Transaction_Item_No, Sub_Product_No, Percentage_Of_Blend, new_Rec.Net * (Percentage_Of_Blend / 100), new_Rec.Cubic_Metres * (Percentage_Of_Blend / 100) FROM "DBA".WM_Product_Component WHERE Product_No = new_Rec.Product_No; END IF; /* Update Job product tonnages */ set @JobNo = (select Job_No from WM_Transaction_Header where WM_Transaction_Header.Transaction_No = New_Rec.Transaction_No); update WM_Job_Auth_Product set Period_Progress_Mass = 0, Period_Progress_CM = 0 where WM_Job_Auth_Product.Job_No = @JobNo and WM_Job_Auth_Product.Product_No = New_Rec.Product_No and Period_End < current date; set @PeriodEnd = (select Period_End from WM_Job_Auth_Product where WM_Job_Auth_Product.Job_No = @JobNo and WM_Job_Auth_Product.Product_No = New_Rec.Product_No); set @PeriodInterval = (select Period_Interval from WM_Job_Auth_Product where WM_Job_Auth_Product.Job_No = @JobNo and WM_Job_Auth_Product.Product_No = New_Rec.Product_No); if @PeriodEnd is null then set @PeriodEnd = current date end if; if @PeriodInterval > 0 then while @PeriodEnd < current date loop set @PeriodEnd = @PeriodEnd + @PeriodInterval end loop end if; --UPDATE progress totals --Note that when reversing a transaction, New_Rec.Net may be negative, so do not allow totals to be less than zero UPDATE WM_Job_Auth_Product SET Job_Progress_Mass = GREATER(Job_Progress_Mass + New_Rec.Net, 0), Period_Progress_Mass = GREATER(Period_Progress_Mass + New_Rec.Net, 0), Job_Progress_CM = GREATER(Job_Progress_CM + New_Rec.Cubic_Metres, 0), Period_Progress_CM = GREATER(Period_Progress_CM + New_Rec.Cubic_Metres, 0), Period_End = @PeriodEnd WHERE WM_Job_Auth_Product.Job_No = @JobNo AND WM_Job_Auth_Product.Product_No = New_Rec.Product_No; END; IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; /* NOTE "ONLY" keyword prevents the update from being applied at the consolidated node */ ALTER TRIGGER ai_WM_Transaction_Item AFTER INSERT ON "DBA".WM_Transaction_Item REFERENCING NEW AS New_Rec FOR EACH ROW BEGIN DECLARE @Total NUMERIC(10,4); DECLARE @ProductDirection integer; DECLARE @JobNo integer; DECLARE @PeriodEnd date; DECLARE @PeriodInterval integer; /* Check that the Item_Charge equals the sum of the Rates */ SELECT SUM(Value) INTO @Total FROM WM_Transaction_Item_Rate WHERE Transaction_No = new_Rec.Transaction_No AND Transaction_Item_No = new_Rec.Transaction_Item_No; IF @Total IS NULL THEN SET @Total = 0; END IF; IF @Total <> new_Rec.Item_Charge THEN RAISERROR 30000 'Total of Transaction Item Rates does not equal Item total'; END IF; /* If Product is a normal (i.e. not a Blended) product, update the Area Tonnage */ /* and Cubic Metre totals from the Item. */ /* If Product is a Blended Product, add records to the WM_Tran_Prod_Component table. */ /* Triggers on WM_Tran_Prod_Component will update the relevant Area records */ IF NOT EXISTS( SELECT 1 FROM "DBA".WM_Product WHERE Product_No = new_Rec.Product_No AND Is_Blended_Product = 'Y') THEN set @ProductDirection=(select(case Incoming_Product when 'Y' then 1 when 'N' then(0-1) end) from WM_Product where WM_Product.Product_No = New_Rec.Product_No); update WM_Area set Tonnes = Tonnes + (New_Rec.Net * @ProductDirection), Cubic_Metres = Cubic_Metres + (New_Rec.Cubic_Metres * @ProductDirection) where WM_Area.Area_No = new_Rec.Area_No; update WM_Area set Start_Date = current date where WM_Area.Area_No = new_Rec.Area_No and Start_Date is null; update WM_Area set First_Use_Date = current date where WM_Area.Area_No = new_Rec.Area_No and First_Use_Date is null; ELSE INSERT "DBA".WM_Tran_Prod_Component (Transaction_No, Transaction_Item_No, Sub_Product_No, Percentage_Of_Blend, Sub_Prod_Net, Sub_Prod_Cubic_Metres) SELECT new_Rec.Transaction_No, new_Rec.Transaction_Item_No, Sub_Product_No, Percentage_Of_Blend, new_Rec.Net * (Percentage_Of_Blend / 100), new_Rec.Cubic_Metres * (Percentage_Of_Blend / 100) FROM "DBA".WM_Product_Component WHERE Product_No = new_Rec.Product_No; END IF; /* Update Job product tonnages */ set @JobNo = (select Job_No from WM_Transaction_Header where WM_Transaction_Header.Transaction_No = New_Rec.Transaction_No); update WM_Job_Auth_Product set Period_Progress_Mass = 0, Period_Progress_CM = 0 where WM_Job_Auth_Product.Job_No = @JobNo and WM_Job_Auth_Product.Product_No = New_Rec.Product_No and Period_End < current date; set @PeriodEnd = (select Period_End from WM_Job_Auth_Product where WM_Job_Auth_Product.Job_No = @JobNo and WM_Job_Auth_Product.Product_No = New_Rec.Product_No); set @PeriodInterval = (select Period_Interval from WM_Job_Auth_Product where WM_Job_Auth_Product.Job_No = @JobNo and WM_Job_Auth_Product.Product_No = New_Rec.Product_No); if @PeriodEnd is null then set @PeriodEnd = current date end if; if @PeriodInterval > 0 then while @PeriodEnd < current date loop set @PeriodEnd = @PeriodEnd + @PeriodInterval end loop end if; --UPDATE progress totals --Note that when reversing a transaction, New_Rec.Net may be negative, so do not allow totals to be less than zero UPDATE WM_Job_Auth_Product SET Job_Progress_Mass = GREATER(Job_Progress_Mass + New_Rec.Net, 0), Period_Progress_Mass = GREATER(Period_Progress_Mass + New_Rec.Net, 0), Job_Progress_CM = GREATER(Job_Progress_CM + New_Rec.Cubic_Metres, 0), Period_Progress_CM = GREATER(Period_Progress_CM + New_Rec.Cubic_Metres, 0), Period_End = @PeriodEnd WHERE WM_Job_Auth_Product.Job_No = @JobNo AND WM_Job_Auth_Product.Product_No = New_Rec.Product_No; END; --Update remote version control UPDATE "DBA".SM_Version_Control SET Version_No = 3 WHERE Entity_Name = 'ai_WM_Transaction_Item' AND Database_ID = (SELECT db_property('GlobalDbID')); PASSTHROUGH STOP; END IF; --Update consolidated version control UPDATE "DBA".SM_Version_Control SET Version_No = 3 WHERE Entity_Name = 'ai_WM_Transaction_Item' AND Database_ID = (SELECT db_property('GlobalDbID')); END IF; IF EXISTS( SELECT 1 FROM "DBA".SM_Version_Control WHERE Entity_Name = 'au_WM_Transaction_Item' AND Database_ID = (SELECT db_property('GlobalDbID')) AND Version_No < 5) THEN ALTER TRIGGER au_WM_Transaction_Item AFTER UPDATE OF Product_No, Area_No, Net, Cubic_Metres ON "DBA".WM_Transaction_Item REFERENCING OLD AS Old_Rec NEW AS New_Rec FOR EACH ROW BEGIN DECLARE @ProductDirection integer; DECLARE @OldJobNo integer; DECLARE @NewJobNo integer; DECLARE @PeriodEnd date; DECLARE @PeriodInterval integer; IF UPDATE(Product_No) THEN /* Product has changed. Delete existing records from the detail table. */ /* If the new product is also a Blended Product, copy its records into the table */ DELETE "DBA".WM_Tran_Prod_Component WHERE Transaction_No = new_Rec.Transaction_No AND Transaction_Item_No = new_Rec.Transaction_Item_No; IF EXISTS( SELECT 1 FROM "DBA".WM_Product WHERE Product_No = new_Rec.Product_No AND Is_Blended_Product = 'Y') THEN INSERT "DBA".WM_Tran_Prod_Component (Transaction_No, Transaction_Item_No, Sub_Product_No, Percentage_Of_Blend, Sub_Prod_Net, Sub_Prod_Cubic_Metres) SELECT new_Rec.Transaction_No, new_Rec.Transaction_Item_No, Sub_Product_No, Percentage_Of_Blend, new_Rec.Net * (Percentage_Of_Blend / 100), new_Rec.Cubic_Metres * (Percentage_Of_Blend / 100) FROM "DBA".WM_Product_Component WHERE Product_No = new_Rec.Product_No; END IF; END IF; IF UPDATE(Net) OR UPDATE(Area_No) THEN IF NOT EXISTS( SELECT 1 FROM "DBA".WM_Product WHERE Product_No = new_Rec.Product_No AND Is_Blended_Product = 'Y') THEN set @ProductDirection = (select(case Incoming_Product when 'Y' then 1 when 'N' then(0-1) end) from WM_Product where WM_Product.Product_No = New_Rec.Product_No); update WM_Area set Tonnes = Tonnes - (Old_Rec.Net * @ProductDirection) where WM_Area.Area_No = old_Rec.Area_No; update WM_Area set Tonnes = Tonnes + (New_Rec.Net * @ProductDirection) where WM_Area.Area_No = new_Rec.Area_No; update WM_Area set Start_Date = current date where WM_Area.Area_No = new_Rec.Area_No and Start_Date is null; update WM_Area set First_Use_Date = current date where WM_Area.Area_No = new_Rec.Area_No and First_Use_Date is null; ELSE /* Update Net in the Component table based on the Percentage there */ UPDATE "DBA".WM_Tran_Prod_Component SET Sub_Prod_Net = new_Rec.Net * (Percentage_Of_Blend / 100) WHERE Transaction_No = new_Rec.Transaction_No AND Transaction_Item_No = new_Rec.Transaction_Item_No; END IF; /* Update job product tonnages */ set @OldJobNo = (select Job_No from WM_Transaction_Header where WM_Transaction_Header.Transaction_No = Old_Rec.Transaction_No); set @NewJobNo = (select Job_No from WM_Transaction_Header where WM_Transaction_Header.Transaction_No = New_Rec.Transaction_No); update WM_Job_Auth_Product set Job_Progress_Mass = GREATER(Job_Progress_Mass - Old_Rec.Net, 0), --NOTE: This will set the total to ZERO if it would be negative Period_Progress_Mass = GREATER(Period_Progress_Mass - Old_Rec.Net, 0) where WM_Job_Auth_Product.Job_No = @OldJobNo and WM_Job_Auth_Product.Product_No = Old_Rec.Product_No; update WM_Job_Auth_Product set Period_Progress_Mass = 0 where WM_Job_Auth_Product.Job_No = @NewJobNo and WM_Job_Auth_Product.Product_No = New_Rec.Product_No and Period_End < current date; set @PeriodEnd = (select Period_End from WM_Job_Auth_Product where WM_Job_Auth_Product.Job_No = @NewJobNo and WM_Job_Auth_Product.Product_No = New_Rec.Product_No); set @PeriodInterval = (select Period_Interval from WM_Job_Auth_Product where WM_Job_Auth_Product.Job_No = @NewJobNo and WM_Job_Auth_Product.Product_No = New_Rec.Product_No); if @PeriodEnd is null then set @PeriodEnd = current date end if; if @PeriodInterval > 0 then while @PeriodEnd < current date loop set @PeriodEnd = @PeriodEnd + @PeriodInterval end loop end if; update WM_Job_Auth_Product set Job_Progress_Mass = GREATER(Job_Progress_Mass + New_Rec.Net, 0), Period_Progress_Mass = GREATER(Period_Progress_Mass + New_Rec.Net, 0), Period_End = @PeriodEnd where WM_Job_Auth_Product.Job_No = @NewJobNo and WM_Job_Auth_Product.Product_No = New_Rec.Product_No; END IF; IF UPDATE(Cubic_Metres) OR UPDATE(Area_No) THEN IF NOT EXISTS( SELECT 1 FROM "DBA".WM_Product WHERE Product_No = new_Rec.Product_No AND Is_Blended_Product = 'Y') THEN set @ProductDirection = (select(case Incoming_Product when 'Y' then 1 when 'N' then(0-1) end) from WM_Product where WM_Product.Product_No = New_Rec.Product_No); update WM_Area set Cubic_Metres = Cubic_Metres - (Old_Rec.Cubic_Metres * @ProductDirection) where WM_Area.Area_No = old_Rec.Area_No; update WM_Area set Cubic_Metres = Cubic_Metres + (New_Rec.Cubic_Metres * @ProductDirection) where WM_Area.Area_No = new_Rec.Area_No; update WM_Area set Start_Date = current date where WM_Area.Area_No = new_Rec.Area_No and Start_Date is null; update WM_Area set First_Use_Date = current date where WM_Area.Area_No = new_Rec.Area_No and First_Use_Date is null; ELSE /* Update Cubic Metres in the Component table based on the Percentage there */ UPDATE "DBA".WM_Tran_Prod_Component SET Sub_Prod_Cubic_Metres = new_Rec.Cubic_Metres * (Percentage_Of_Blend / 100) WHERE Transaction_No = new_Rec.Transaction_No AND Transaction_Item_No = new_Rec.Transaction_Item_No; END IF; /* Update Job product cubic metres */ set @OldJobNo = (select Job_No from WM_Transaction_Header where WM_Transaction_Header.Transaction_No = Old_Rec.Transaction_No); set @NewJobNo = (select Job_No from WM_Transaction_Header where WM_Transaction_Header.Transaction_No = New_Rec.Transaction_No); update WM_Job_Auth_Product set Job_Progress_CM = GREATER(Job_Progress_CM - Old_Rec.Cubic_Metres, 0), --NOTE: This will set the total to ZERO if it would be negative Period_Progress_CM = GREATER(Period_Progress_CM - Old_Rec.Cubic_Metres, 0) where WM_Job_Auth_Product.Job_No = @OldJobNo and WM_Job_Auth_Product.Product_No = Old_Rec.Product_No; update WM_Job_Auth_Product set Period_Progress_CM = 0 where WM_Job_Auth_Product.Job_No = @NewJobNo and WM_Job_Auth_Product.Product_No = New_Rec.Product_No and Period_End < current date; set @PeriodEnd = (select Period_End from WM_Job_Auth_Product where WM_Job_Auth_Product.Job_No = @NewJobNo and WM_Job_Auth_Product.Product_No = New_Rec.Product_No); set @PeriodInterval = (select Period_Interval from WM_Job_Auth_Product where WM_Job_Auth_Product.Job_No = @NewJobNo and WM_Job_Auth_Product.Product_No = New_Rec.Product_No); if @PeriodEnd is null then set @PeriodEnd = current date end if; if @PeriodInterval > 0 then while @PeriodEnd < current date loop set @PeriodEnd = @PeriodEnd + @PeriodInterval end loop end if; update WM_Job_Auth_Product set Job_Progress_CM = GREATER(Job_Progress_CM + New_Rec.Cubic_Metres, 0), Period_Progress_CM = GREATER(Period_Progress_CM + New_Rec.Cubic_Metres, 0), Period_End = @PeriodEnd where WM_Job_Auth_Product.Job_No = @NewJobNo and WM_Job_Auth_Product.Product_No = New_Rec.Product_No; END IF; END; IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; /* NOTE "ONLY" keyword prevents the update from being applied at the consolidated node */ ALTER TRIGGER au_WM_Transaction_Item AFTER UPDATE OF Product_No, Area_No, Net, Cubic_Metres ON "DBA".WM_Transaction_Item REFERENCING OLD AS Old_Rec NEW AS New_Rec FOR EACH ROW BEGIN DECLARE @ProductDirection integer; DECLARE @OldJobNo integer; DECLARE @NewJobNo integer; DECLARE @PeriodEnd date; DECLARE @PeriodInterval integer; IF UPDATE(Product_No) THEN /* Product has changed. Delete existing records from the detail table. */ /* If the new product is also a Blended Product, copy its records into the table */ DELETE "DBA".WM_Tran_Prod_Component WHERE Transaction_No = new_Rec.Transaction_No AND Transaction_Item_No = new_Rec.Transaction_Item_No; IF EXISTS( SELECT 1 FROM "DBA".WM_Product WHERE Product_No = new_Rec.Product_No AND Is_Blended_Product = 'Y') THEN INSERT "DBA".WM_Tran_Prod_Component (Transaction_No, Transaction_Item_No, Sub_Product_No, Percentage_Of_Blend, Sub_Prod_Net, Sub_Prod_Cubic_Metres) SELECT new_Rec.Transaction_No, new_Rec.Transaction_Item_No, Sub_Product_No, Percentage_Of_Blend, new_Rec.Net * (Percentage_Of_Blend / 100), new_Rec.Cubic_Metres * (Percentage_Of_Blend / 100) FROM "DBA".WM_Product_Component WHERE Product_No = new_Rec.Product_No; END IF; END IF; IF UPDATE(Net) OR UPDATE(Area_No) THEN IF NOT EXISTS( SELECT 1 FROM "DBA".WM_Product WHERE Product_No = new_Rec.Product_No AND Is_Blended_Product = 'Y') THEN set @ProductDirection = (select(case Incoming_Product when 'Y' then 1 when 'N' then(0-1) end) from WM_Product where WM_Product.Product_No = New_Rec.Product_No); update WM_Area set Tonnes = Tonnes - (Old_Rec.Net * @ProductDirection) where WM_Area.Area_No = old_Rec.Area_No; update WM_Area set Tonnes = Tonnes + (New_Rec.Net * @ProductDirection) where WM_Area.Area_No = new_Rec.Area_No; update WM_Area set Start_Date = current date where WM_Area.Area_No = new_Rec.Area_No and Start_Date is null; update WM_Area set First_Use_Date = current date where WM_Area.Area_No = new_Rec.Area_No and First_Use_Date is null; ELSE /* Update Net in the Component table based on the Percentage there */ UPDATE "DBA".WM_Tran_Prod_Component SET Sub_Prod_Net = new_Rec.Net * (Percentage_Of_Blend / 100) WHERE Transaction_No = new_Rec.Transaction_No AND Transaction_Item_No = new_Rec.Transaction_Item_No; END IF; /* Update job product tonnages */ set @OldJobNo = (select Job_No from WM_Transaction_Header where WM_Transaction_Header.Transaction_No = Old_Rec.Transaction_No); set @NewJobNo = (select Job_No from WM_Transaction_Header where WM_Transaction_Header.Transaction_No = New_Rec.Transaction_No); update WM_Job_Auth_Product set Job_Progress_Mass = GREATER(Job_Progress_Mass - Old_Rec.Net, 0), --NOTE: This will set the total to ZERO if it would be negative Period_Progress_Mass = GREATER(Period_Progress_Mass - Old_Rec.Net, 0) where WM_Job_Auth_Product.Job_No = @OldJobNo and WM_Job_Auth_Product.Product_No = Old_Rec.Product_No; update WM_Job_Auth_Product set Period_Progress_Mass = 0 where WM_Job_Auth_Product.Job_No = @NewJobNo and WM_Job_Auth_Product.Product_No = New_Rec.Product_No and Period_End < current date; set @PeriodEnd = (select Period_End from WM_Job_Auth_Product where WM_Job_Auth_Product.Job_No = @NewJobNo and WM_Job_Auth_Product.Product_No = New_Rec.Product_No); set @PeriodInterval = (select Period_Interval from WM_Job_Auth_Product where WM_Job_Auth_Product.Job_No = @NewJobNo and WM_Job_Auth_Product.Product_No = New_Rec.Product_No); if @PeriodEnd is null then set @PeriodEnd = current date end if; if @PeriodInterval > 0 then while @PeriodEnd < current date loop set @PeriodEnd = @PeriodEnd + @PeriodInterval end loop end if; update WM_Job_Auth_Product set Job_Progress_Mass = GREATER(Job_Progress_Mass + New_Rec.Net, 0), Period_Progress_Mass = GREATER(Period_Progress_Mass + New_Rec.Net, 0), Period_End = @PeriodEnd where WM_Job_Auth_Product.Job_No = @NewJobNo and WM_Job_Auth_Product.Product_No = New_Rec.Product_No; END IF; IF UPDATE(Cubic_Metres) OR UPDATE(Area_No) THEN IF NOT EXISTS( SELECT 1 FROM "DBA".WM_Product WHERE Product_No = new_Rec.Product_No AND Is_Blended_Product = 'Y') THEN set @ProductDirection = (select(case Incoming_Product when 'Y' then 1 when 'N' then(0-1) end) from WM_Product where WM_Product.Product_No = New_Rec.Product_No); update WM_Area set Cubic_Metres = Cubic_Metres - (Old_Rec.Cubic_Metres * @ProductDirection) where WM_Area.Area_No = old_Rec.Area_No; update WM_Area set Cubic_Metres = Cubic_Metres + (New_Rec.Cubic_Metres * @ProductDirection) where WM_Area.Area_No = new_Rec.Area_No; update WM_Area set Start_Date = current date where WM_Area.Area_No = new_Rec.Area_No and Start_Date is null; update WM_Area set First_Use_Date = current date where WM_Area.Area_No = new_Rec.Area_No and First_Use_Date is null; ELSE /* Update Cubic Metres in the Component table based on the Percentage there */ UPDATE "DBA".WM_Tran_Prod_Component SET Sub_Prod_Cubic_Metres = new_Rec.Cubic_Metres * (Percentage_Of_Blend / 100) WHERE Transaction_No = new_Rec.Transaction_No AND Transaction_Item_No = new_Rec.Transaction_Item_No; END IF; /* Update Job product cubic metres */ set @OldJobNo = (select Job_No from WM_Transaction_Header where WM_Transaction_Header.Transaction_No = Old_Rec.Transaction_No); set @NewJobNo = (select Job_No from WM_Transaction_Header where WM_Transaction_Header.Transaction_No = New_Rec.Transaction_No); update WM_Job_Auth_Product set Job_Progress_CM = GREATER(Job_Progress_CM - Old_Rec.Cubic_Metres, 0), --NOTE: This will set the total to ZERO if it would be negative Period_Progress_CM = GREATER(Period_Progress_CM - Old_Rec.Cubic_Metres, 0) where WM_Job_Auth_Product.Job_No = @OldJobNo and WM_Job_Auth_Product.Product_No = Old_Rec.Product_No; update WM_Job_Auth_Product set Period_Progress_CM = 0 where WM_Job_Auth_Product.Job_No = @NewJobNo and WM_Job_Auth_Product.Product_No = New_Rec.Product_No and Period_End < current date; set @PeriodEnd = (select Period_End from WM_Job_Auth_Product where WM_Job_Auth_Product.Job_No = @NewJobNo and WM_Job_Auth_Product.Product_No = New_Rec.Product_No); set @PeriodInterval = (select Period_Interval from WM_Job_Auth_Product where WM_Job_Auth_Product.Job_No = @NewJobNo and WM_Job_Auth_Product.Product_No = New_Rec.Product_No); if @PeriodEnd is null then set @PeriodEnd = current date end if; if @PeriodInterval > 0 then while @PeriodEnd < current date loop set @PeriodEnd = @PeriodEnd + @PeriodInterval end loop end if; update WM_Job_Auth_Product set Job_Progress_CM = GREATER(Job_Progress_CM + New_Rec.Cubic_Metres, 0), Period_Progress_CM = GREATER(Period_Progress_CM + New_Rec.Cubic_Metres, 0), Period_End = @PeriodEnd where WM_Job_Auth_Product.Job_No = @NewJobNo and WM_Job_Auth_Product.Product_No = New_Rec.Product_No; END IF; END; --Update remote version control UPDATE "DBA".SM_Version_Control SET Version_No = 5 WHERE Entity_Name = 'au_WM_Transaction_Item' AND Database_ID = (SELECT db_property('GlobalDbID')); PASSTHROUGH STOP; END IF; --Update consolidated version control UPDATE "DBA".SM_Version_Control SET Version_No = 5 WHERE Entity_Name = 'au_WM_Transaction_Item' AND Database_ID = (SELECT db_property('GlobalDbID')); END IF; /***********************************************/ /* 22/09/2006 - Add EPA reporting table WM_EPA_RPTM_Sec2_WasteReceived */ /* NOTE: No passthrough is required as tables are not replicated */ /***********************************************/ IF NOT EXISTS( SELECT 1 FROM systable WHERE table_name = 'WM_EPA_RPTM_Sec2_WasteReceived') THEN create table DBA.WM_EPA_RPTM_Sec2_WasteReceived ( Section char(5) not null, Description varchar(80) not null, Count_1 integer, Count_2 integer, Quantity numeric(15,4), Waste_Facility varchar(40), Display_Order integer ); alter table DBA.WM_EPA_RPTM_Sec2_WasteReceived add constraint PK_WM_EPA_RPTM_SEC2_WASTERECEI primary key (Section, Description); END IF; /***********************************************/ /* 22/09/2006 - Add EPA reporting table WM_EPA_RPTM_Sec3_Exemptions */ /* NOTE: No passthrough is required as tables are not replicated */ /***********************************************/ IF NOT EXISTS( SELECT 1 FROM systable WHERE table_name = 'WM_EPA_RPTM_Sec3_Exemptions') THEN create table DBA.WM_EPA_RPTM_Sec3_Exemptions ( Section char(5) not null, Description varchar(80) not null, Waste_Stream char(10) not null, Quantity numeric(15,4), Approval_No char(15), Organisation varchar(40) ); alter table DBA.WM_EPA_RPTM_Sec3_Exemptions add constraint PK_WM_EPA_RPTM_SEC3_EXEMPTIONS primary key (Section, Description, Waste_Stream); END IF; /***********************************************/ /* 22/09/2006 - Add new EPA reporting fields to table WM_EPA_RPTM_Report */ /* NOTE: No passthrough is required as tables are not replicated */ /***********************************************/ IF NOT EXISTS( SELECT 1 FROM syscolumn JOIN systable WHERE table_name = 'WM_EPA_RPTM_Report' AND column_name = 'Contribution_Rate') THEN ALTER TABLE DBA.WM_EPA_RPTM_Report ADD Sec2A_A_Total NUMERIC(15,4); ALTER TABLE DBA.WM_EPA_RPTM_Report ADD Sec2B_B_Total NUMERIC(15,4); ALTER TABLE DBA.WM_EPA_RPTM_Report ADD Sec2C_C_Total NUMERIC(15,4); ALTER TABLE DBA.WM_EPA_RPTM_Report ADD Sec2D_D_Total NUMERIC(15,4); ALTER TABLE DBA.WM_EPA_RPTM_Report ADD Sec3A_E_Total NUMERIC(15,4); ALTER TABLE DBA.WM_EPA_RPTM_Report ADD Sec3B_F_Total NUMERIC(15,4); ALTER TABLE DBA.WM_EPA_RPTM_Report ADD Contribution_Rate NUMERIC(10,2); END IF; /*********************************************************************/ /* 13/10/2006 - Add Charge Override exception to the set */ /* NOTE: No passthrough is required as the INSERT will replicate normally */ /*********************************************************************/ IF NOT EXISTS( SELECT 1 FROM "DBA".WM_Exceptions WHERE Code = 'C') THEN INSERT "DBA".WM_Exceptions (Code, Name, Memo) VALUES ('C', 'Charge Override', 'The operator has entered an arbitrary charge rate for one or more products on the transaction'); END IF; /*********************************************************************/ /* 16/10/2006 - Add Rate_Override_Option to WM_Product_Rate */ /*********************************************************************/ IF NOT EXISTS( SELECT 1 FROM syscolumn JOIN systable WHERE table_name = 'WM_Product_Rate' AND column_name = 'Rate_Override_Option') THEN ALTER TABLE "DBA".WM_Product_Rate ADD Rate_Override_Option CHAR(1) NOT NULL DEFAULT 'N' CONSTRAINT CKC_RATE_OVERRIDE_OPT_WM_PRODU CHECK (Rate_Override_Option IN ('N','P','A')); IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; /* NOTE "ONLY" keyword prevents the update from being applied at the consolidated node */ ALTER TABLE "DBA".WM_Product_Rate ADD Rate_Override_Option CHAR(1) NOT NULL DEFAULT 'N' CONSTRAINT CKC_RATE_OVERRIDE_OPT_WM_PRODU CHECK (Rate_Override_Option IN ('N','P','A')); PASSTHROUGH STOP; END IF; END IF; /*********************************************************************/ /* 16/10/2006 - Update WM_DuplicateProductRate to copy over the new field */ /*********************************************************************/ IF EXISTS( SELECT 1 FROM "DBA".SM_Version_Control WHERE Entity_Name = 'WM_DuplicateProductRate' AND Database_ID = (SELECT db_property('GlobalDbID')) AND Version_No < 2) THEN ALTER PROCEDURE "DBA".WM_DuplicateProductRate(IN @RateRangeNo Entity_No, IN @NewDesc VARCHAR(20), OUT @NewRangeNo Entity_No, IN @NewIsDefault Boolean DEFAULT 'N', IN @NewEffectiveDate Entity_Date DEFAULT NULL, IN @NewEndEffectiveDate Entity_Date DEFAULT NULL, IN @DupAuthorisations Boolean DEFAULT 'Y') BEGIN /***************************************************************************/ /* WM_DuplicateProductRate */ /* */ /* This proc will create a duplicate product rate record with the */ /* specified Description (@NewDesc), Is_Default value (@NewIsDefault), */ /* Valid_Start_Date (@NewEffectiveDate) and Valid_End_Date */ /* (@NewEndEffectiveDate). */ /* If @DupAuthorisation = 'Y' the product rate authorisations will also */ /* be duplicated. */ /***************************************************************************/ DECLARE @ErrMsg VARCHAR(100); DECLARE @DefIsoLevel INT; --Validate inputs IF @RateRangeNo IS NULL THEN RAISERROR 30000 'You must specify a Rate Range No'; RETURN; END IF; IF @NewDesc IS NULL THEN RAISERROR 30000 'You must specify a descriptor for the new Product Rate'; RETURN; END IF; IF NOT EXISTS( SELECT 1 FROM "DBA".WM_Product_Rate WHERE Range_No = @RateRangeNo) THEN SET @ErrMsg = 'No Product Rate found for Range No ' || RTRIM(CAST(@RateRangeNo AS CHAR)); RAISERROR 30000 @ErrMsg; RETURN; END IF; SET @NewRangeNo = NULL; --Save current iso level SELECT connection_property('ISOLATION_LEVEL') INTO @DefIsoLevel; --Use Read Committed isolation level SET TEMPORARY OPTION ISOLATION_LEVEL = 1; --Duplicate the product rate record INSERT "DBA".WM_Product_Rate (Product_No, Rate_No, "Description", "Value", Net, Is_Default, Charge_Type, Is_Operator_Selected, Is_Auto_Range, Cost_Rule_No, Minimum_Cost, Min_Gross_Allowed, Max_Gross_Allowed, Valid_Start_Date, Valid_End_Date, User_Rate_Field1, User_Rate_Field2, User_Rate_Field3, User_Rate_Field4, Sub_Stream_2_No, Rate_Override_Option, Memo, Available) SELECT Product_No, Rate_No, @NewDesc, "Value", Net, @NewIsDefault, Charge_Type, Is_Operator_Selected, Is_Auto_Range, Cost_Rule_No, Minimum_Cost, Min_Gross_Allowed, Max_Gross_Allowed, @NewEffectiveDate, @NewEndEffectiveDate, User_Rate_Field1, User_Rate_Field2, User_Rate_Field3, User_Rate_Field4, Sub_Stream_2_No, Rate_Override_Option, Memo, Available FROM "DBA".WM_Product_Rate WHERE Range_No = @RateRangeNo; SET @NewRangeNo = @@identity; --Duplicate product rate authorisations if required IF @DupAuthorisations = 'Y' THEN --Get the Product Rate -> ??? authorisation keys CREATE TABLE #AuthConfigHeaders( Auth_Config_No INT PRIMARY KEY, Authing_Class_ID INT, Authed_Class_ID INT); INSERT #AuthConfigHeaders (Auth_Config_No, Authing_Class_ID, Authed_Class_ID) SELECT Authorisation_Config_No, Authorising_Class_ID, Authorised_Class_ID FROM "DBA".SM_Authorisation_Config WHERE Authorising_Class_ID = 3007 --Product Rate AND Authorised_Process = 3002; --Calc Product Charge Process INSERT "DBA".SM_Authorisation_Item (Authorisation_Config_No, Authorising_Class_Key, Authorised_Class_Key) SELECT Authorisation_Config_No, @NewRangeNo, Authorised_Class_Key FROM "DBA".SM_Authorisation_Item WHERE Authorisation_Config_No IN (SELECT Auth_Config_No FROM #AuthConfigHeaders) AND Authorising_Class_Key = RTRIM(CAST(@RateRangeNo AS CHAR)); DROP TABLE #AuthConfigHeaders; END IF; --Commit transaction COMMIT; --Restore previous iso level IF @DefIsoLevel = 0 THEN SET TEMPORARY OPTION ISOLATION_LEVEL = 0; ELSEIF @DefIsoLevel = 1 THEN SET TEMPORARY OPTION ISOLATION_LEVEL = 1; ELSEIF @DefIsoLevel = 2 THEN SET TEMPORARY OPTION ISOLATION_LEVEL = 2; ELSEIF @DefIsoLevel = 3 THEN SET TEMPORARY OPTION ISOLATION_LEVEL = 3; END IF; END; IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; /* NOTE "ONLY" keyword prevents the update from being applied at the consolidated node */ ALTER PROCEDURE "DBA".WM_DuplicateProductRate(IN @RateRangeNo Entity_No, IN @NewDesc VARCHAR(20), OUT @NewRangeNo Entity_No, IN @NewIsDefault Boolean DEFAULT 'N', IN @NewEffectiveDate Entity_Date DEFAULT NULL, IN @NewEndEffectiveDate Entity_Date DEFAULT NULL, IN @DupAuthorisations Boolean DEFAULT 'Y') BEGIN /***************************************************************************/ /* WM_DuplicateProductRate */ /* */ /* This proc will create a duplicate product rate record with the */ /* specified Description (@NewDesc), Is_Default value (@NewIsDefault), */ /* Valid_Start_Date (@NewEffectiveDate) and Valid_End_Date */ /* (@NewEndEffectiveDate). */ /* If @DupAuthorisation = 'Y' the product rate authorisations will also */ /* be duplicated. */ /***************************************************************************/ DECLARE @ErrMsg VARCHAR(100); DECLARE @DefIsoLevel INT; --Validate inputs IF @RateRangeNo IS NULL THEN RAISERROR 30000 'You must specify a Rate Range No'; RETURN; END IF; IF @NewDesc IS NULL THEN RAISERROR 30000 'You must specify a descriptor for the new Product Rate'; RETURN; END IF; IF NOT EXISTS( SELECT 1 FROM "DBA".WM_Product_Rate WHERE Range_No = @RateRangeNo) THEN SET @ErrMsg = 'No Product Rate found for Range No ' || RTRIM(CAST(@RateRangeNo AS CHAR)); RAISERROR 30000 @ErrMsg; RETURN; END IF; SET @NewRangeNo = NULL; --Save current iso level SELECT connection_property('ISOLATION_LEVEL') INTO @DefIsoLevel; --Use Read Committed isolation level SET TEMPORARY OPTION ISOLATION_LEVEL = 1; --Duplicate the product rate record INSERT "DBA".WM_Product_Rate (Product_No, Rate_No, "Description", "Value", Net, Is_Default, Charge_Type, Is_Operator_Selected, Is_Auto_Range, Cost_Rule_No, Minimum_Cost, Min_Gross_Allowed, Max_Gross_Allowed, Valid_Start_Date, Valid_End_Date, User_Rate_Field1, User_Rate_Field2, User_Rate_Field3, User_Rate_Field4, Sub_Stream_2_No, Rate_Override_Option, Memo, Available) SELECT Product_No, Rate_No, @NewDesc, "Value", Net, @NewIsDefault, Charge_Type, Is_Operator_Selected, Is_Auto_Range, Cost_Rule_No, Minimum_Cost, Min_Gross_Allowed, Max_Gross_Allowed, @NewEffectiveDate, @NewEndEffectiveDate, User_Rate_Field1, User_Rate_Field2, User_Rate_Field3, User_Rate_Field4, Sub_Stream_2_No, Rate_Override_Option, Memo, Available FROM "DBA".WM_Product_Rate WHERE Range_No = @RateRangeNo; SET @NewRangeNo = @@identity; --Duplicate product rate authorisations if required IF @DupAuthorisations = 'Y' THEN --Get the Product Rate -> ??? authorisation keys CREATE TABLE #AuthConfigHeaders( Auth_Config_No INT PRIMARY KEY, Authing_Class_ID INT, Authed_Class_ID INT); INSERT #AuthConfigHeaders (Auth_Config_No, Authing_Class_ID, Authed_Class_ID) SELECT Authorisation_Config_No, Authorising_Class_ID, Authorised_Class_ID FROM "DBA".SM_Authorisation_Config WHERE Authorising_Class_ID = 3007 --Product Rate AND Authorised_Process = 3002; --Calc Product Charge Process INSERT "DBA".SM_Authorisation_Item (Authorisation_Config_No, Authorising_Class_Key, Authorised_Class_Key) SELECT Authorisation_Config_No, @NewRangeNo, Authorised_Class_Key FROM "DBA".SM_Authorisation_Item WHERE Authorisation_Config_No IN (SELECT Auth_Config_No FROM #AuthConfigHeaders) AND Authorising_Class_Key = RTRIM(CAST(@RateRangeNo AS CHAR)); DROP TABLE #AuthConfigHeaders; END IF; --Commit transaction COMMIT; --Restore previous iso level IF @DefIsoLevel = 0 THEN SET TEMPORARY OPTION ISOLATION_LEVEL = 0; ELSEIF @DefIsoLevel = 1 THEN SET TEMPORARY OPTION ISOLATION_LEVEL = 1; ELSEIF @DefIsoLevel = 2 THEN SET TEMPORARY OPTION ISOLATION_LEVEL = 2; ELSEIF @DefIsoLevel = 3 THEN SET TEMPORARY OPTION ISOLATION_LEVEL = 3; END IF; END; --Update remote version control UPDATE "DBA".SM_Version_Control SET Version_No = 2 WHERE Entity_Name = 'WM_DuplicateProductRate' AND Database_ID = (SELECT db_property('GlobalDbID')); PASSTHROUGH STOP; END IF; --Update consolidated version control UPDATE "DBA".SM_Version_Control SET Version_No = 2 WHERE Entity_Name = 'WM_DuplicateProductRate' AND Database_ID = (SELECT db_property('GlobalDbID')); END IF; /*********************************************************************/ /* 16/10/2006 - Update WM_GetProductCharges return the new field */ /*********************************************************************/ IF EXISTS( SELECT 1 FROM "DBA".SM_Version_Control WHERE Entity_Name = 'WM_GetProductCharges' AND Database_ID = (SELECT db_property('GlobalDbID')) AND Version_No < 8) THEN ALTER PROCEDURE "DBA".WM_GetProductCharges(IN @ProductNo Entity_No, IN @TranDate Entity_Date, IN @SpecificChargeNo Entity_No, IN @SpecificChargeType CHAR(1), IN @CheckAuthorisations Boolean, IN @AllowUnavailable Boolean DEFAULT 'N') RESULT( Product_No Entity_No, Rate_No Entity_No, Range_No Entity_No, Description varchar(20), Value numeric(10,4), Net Weight, Is_Default Boolean, Charge_Type Prod_Cost_Method, Is_Operator_Selected Boolean, Is_Auto_Range Boolean, Cost_Rule_No Entity_No, Minimum_Cost numeric(10,4), Min_Gross_Allowed Weight, Max_Gross_Allowed Weight, Valid_Start_Date Entity_Date, Valid_End_Date Entity_Date, User_Rate_Field1 numeric(10,4), User_Rate_Field2 numeric(10,4), User_Rate_Field3 numeric(10,4), User_Rate_Field4 numeric(10,4), Sub_Stream_2_No integer, Rate_Override_Option CHAR(1), Memo Entity_Comment, Available Boolean, Type char(1), Name Entity_Name, Is_GST Boolean, Source_Rate_No Entity_No ) BEGIN DECLARE LOCAL TEMPORARY TABLE @ChargeAuthHeaders( Authorisation_Config_No INTEGER, Authorised_Class_ID INTEGER, Table_Name CHAR(128)); --Declare local temp table to hold result set while processing DECLARE LOCAL TEMPORARY TABLE @ChargeInfo( Range_No INTEGER, Rate_No INTEGER, Rate_Type CHAR(1), Description VARCHAR(20), Authorised_Class_ID INT, Authorised_Class_Key VARCHAR(50), Table_Name CHAR(128), Available CHAR(1)); DECLARE @AuthCust Entity_No; DECLARE @AuthSite Entity_No; INSERT @ChargeAuthHeaders (Authorisation_Config_No, Authorised_Class_ID, Table_Name) SELECT AC.Authorisation_Config_No, AC.Authorised_Class_ID, OC.Table_Name FROM "DBA".SM_Authorisation_Config AC LEFT OUTER JOIN "DBA".SM_Object_Class OC ON (OC.Object_Class = AC.Authorised_Class_ID) --may not exist WHERE AC.Authorising_Class_ID = 3007 --product rate AND AC.Authorised_Process = 3002; --charge process IF @@ERROR <> 0 THEN RETURN; END IF; /* NOTE: The @AllowUnavailable flag will change filtering of Charge-type rates based on authorisations, validity dates and the "Available" flag. */ /* When set to 'Y', the procedure will return rates irrespective of authorisations, validity date settings or "Available" flag. */ /* When set to 'N', the procedure will return only rates that are authorised (optionally), flagged as "Available" and are valid for the supplied @TranDate. */ /* %MG NOTE 05/06/2006 - Rates of type other than 'C' are still subject to validity date checks when @AllowUnavailable = 'Y', to prevent multiples of the same rate from being returned */ IF @SpecificChargeNo IS NULL THEN -- Select all rates for the product. IF (@CheckAuthorisations = 'Y') AND (@AllowUnavailable = 'N') THEN -- Create a "shortlist" of available rates based on Available flag, Validity dates and Authorised entities INSERT @ChargeInfo (Range_No, Rate_No, Rate_Type, Description, Authorised_Class_ID, Authorised_Class_Key, Table_Name, Available) SELECT PR.Range_No, PR.Rate_No, R.Type, PR.Description, CAH.Authorised_Class_ID, AI.Authorised_Class_Key, CAH.Table_Name, PR.Available FROM "DBA".WM_Product_Rate PR INNER JOIN "DBA".WM_Rate R ON (R.Rate_No = PR.Rate_No) LEFT OUTER JOIN "DBA".SM_Authorisation_Item AI ON ((AI.Authorising_Class_Key = CAST(PR.Range_No AS CHAR)) AND (AI.Authorisation_Config_No IN (SELECT Authorisation_Config_No FROM @ChargeAuthHeaders))) LEFT OUTER JOIN @ChargeAuthHeaders CAH ON (CAH.Authorisation_Config_No = AI.Authorisation_Config_No) WHERE (PR.Product_No = @ProductNo) AND ((DATEDIFF(Day, PR.Valid_Start_Date, @TranDate) >= 0) OR (PR.Valid_Start_Date IS NULL)) AND ((DATEDIFF(Day, @TranDate, PR.Valid_End_Date) >= 0) OR (PR.Valid_End_Date IS NULL)) AND (PR.Available = 'Y') ELSE -- Not checking authorisations - just return the result set. -- Must be in order by net in descending order for DetermineBestRate to work correctly. SELECT PR.Product_No, PR.Rate_No, PR.Range_No, PR.Description, PR.Value, PR.Net, PR.Is_Default, PR.Charge_Type, PR.Is_Operator_Selected, PR.Is_Auto_Range, PR.Cost_Rule_No, PR.Minimum_Cost, PR.Min_Gross_Allowed, PR.Max_Gross_Allowed, PR.Valid_Start_Date, PR.Valid_End_Date, PR.User_Rate_Field1, PR.User_Rate_Field2, PR.User_Rate_Field3, PR.User_Rate_Field4, PR.Sub_Stream_2_No, PR.Rate_Override_Option, PR.Memo, PR.Available, R.Type, R.Name, R.Is_GST, R.Rate_No AS Source_Rate_No FROM "DBA".WM_Product_Rate PR, "DBA".WM_Rate R WHERE PR.Rate_No = R.Rate_No AND PR.Product_No = @ProductNo AND ((DATEDIFF(Day, PR.Valid_Start_Date, @TranDate) >= 0) OR (PR.Valid_Start_Date IS NULL) OR ((@AllowUnavailable = 'Y') AND (R.Type = 'C'))) AND ((DATEDIFF(Day, @TranDate, PR.Valid_End_Date) >= 0) OR (PR.Valid_End_Date IS NULL) OR ((@AllowUnavailable = 'Y') AND (R.Type = 'C'))) AND ((PR.Available = 'Y') OR (@AllowUnavailable = 'Y')) ORDER BY PR.Net DESC END IF; ELSE -- Select specific rate. IF (@CheckAuthorisations = 'Y') AND (@AllowUnavailable = 'N') THEN -- Create a "shortlist" of available rates based on Available flag, Validity dates and Authorised entities -- If the specified rate has the auto range flag then return the auto range group that includes the rate. IF EXISTS( SELECT 1 FROM "DBA".WM_Product_Rate WHERE Product_No = @ProductNo AND ((Range_No = @SpecificChargeNo) AND (Is_Auto_Range = 'Y'))) THEN INSERT @ChargeInfo (Range_No, Rate_No, Rate_Type, Description, Authorised_Class_ID, Authorised_Class_Key, Table_Name, Available) SELECT PR.Range_No, PR.Rate_No, R.Type, PR.Description, CAH.Authorised_Class_ID, AI.Authorised_Class_Key, CAH.Table_Name, PR.Available FROM "DBA".WM_Product_Rate PR INNER JOIN "DBA".WM_Rate R ON (R.Rate_No = PR.Rate_No) LEFT OUTER JOIN "DBA".SM_Authorisation_Item AI ON ((AI.Authorising_Class_Key = CAST(PR.Range_No AS CHAR)) AND (AI.Authorisation_Config_No IN (SELECT Authorisation_Config_No FROM @ChargeAuthHeaders))) LEFT OUTER JOIN @ChargeAuthHeaders CAH ON (CAH.Authorisation_Config_No = AI.Authorisation_Config_No) WHERE (PR.Product_No = @ProductNo) AND (((R.Type = 'C') AND (PR.Is_Auto_Range = 'Y')) OR (R.Type <> 'C')) AND ((DATEDIFF(Day, PR.Valid_Start_Date, @TranDate) >= 0) OR (PR.Valid_Start_Date IS NULL)) AND ((DATEDIFF(Day, @TranDate, PR.Valid_End_Date) >= 0) OR (PR.Valid_End_Date IS NULL)) AND (PR.Available = 'Y') ELSE INSERT @ChargeInfo (Range_No, Rate_No, Rate_Type, Description, Authorised_Class_ID, Authorised_Class_Key, Table_Name, Available) SELECT PR.Range_No, PR.Rate_No, R.Type, PR.Description, CAH.Authorised_Class_ID, AI.Authorised_Class_Key, CAH.Table_Name, PR.Available FROM "DBA".WM_Product_Rate PR INNER JOIN "DBA".WM_Rate R ON (R.Rate_No = PR.Rate_No) LEFT OUTER JOIN "DBA".SM_Authorisation_Item AI ON ((AI.Authorising_Class_Key = CAST(PR.Range_No AS CHAR)) AND (AI.Authorisation_Config_No IN (SELECT Authorisation_Config_No FROM @ChargeAuthHeaders))) LEFT OUTER JOIN @ChargeAuthHeaders CAH ON (CAH.Authorisation_Config_No = AI.Authorisation_Config_No) WHERE (PR.Product_No = @ProductNo) AND ((PR.Range_No = @SpecificChargeNo) OR (R.Type <> 'C')) AND ((DATEDIFF(Day, PR.Valid_Start_Date, @TranDate) >= 0) OR (PR.Valid_Start_Date IS NULL)) AND ((DATEDIFF(Day, @TranDate, PR.Valid_End_Date) >= 0) OR (PR.Valid_End_Date IS NULL)) AND (PR.Available = 'Y') END IF; ELSE -- Not checking authorisations - just return the result set. -- Must be in order by net in descending order for DetermineBestRate to work correctly. -- If the specified rate has the auto range flag then return the auto range group that includes the rate. IF EXISTS( SELECT 1 FROM "DBA".WM_Product_Rate PR, "DBA".WM_Rate R WHERE PR.Rate_No = R.Rate_No AND PR.Product_No = @ProductNo AND ((PR.Range_No = @SpecificChargeNo) AND (PR.Is_Auto_Range = 'Y'))) THEN SELECT PR.Product_No, PR.Rate_No, PR.Range_No, PR.Description, PR.Value, PR.Net, PR.Is_Default, PR.Charge_Type, PR.Is_Operator_Selected, PR.Is_Auto_Range, PR.Cost_Rule_No, PR.Minimum_Cost, PR.Min_Gross_Allowed, PR.Max_Gross_Allowed, PR.Valid_Start_Date, PR.Valid_End_Date, PR.User_Rate_Field1, PR.User_Rate_Field2, PR.User_Rate_Field3, PR.User_Rate_Field4, PR.Sub_Stream_2_No, PR.Rate_Override_Option, PR.Memo, PR.Available, R.Type, R.Name, R.Is_GST, R.Rate_No AS Source_Rate_No FROM "DBA".WM_Product_Rate PR, "DBA".WM_Rate R WHERE PR.Rate_No = R.Rate_No AND PR.Product_No = @ProductNo AND ((R.Type = 'C' AND PR.Is_Auto_Range = 'Y') OR (R.Type <> 'C')) AND ((DATEDIFF(Day, PR.Valid_Start_Date, @TranDate) >= 0) OR (PR.Valid_Start_Date IS NULL) OR ((@AllowUnavailable = 'Y') AND (R.Type = 'C'))) AND ((DATEDIFF(Day, @TranDate, PR.Valid_End_Date) >= 0) OR (PR.Valid_End_Date IS NULL) OR ((@AllowUnavailable = 'Y') AND (R.Type = 'C'))) AND ((PR.Available = 'Y') OR (@AllowUnavailable = 'Y')) ORDER BY PR.Net DESC ELSE SELECT PR.Product_No, PR.Rate_No, PR.Range_No, PR.Description, PR.Value, PR.Net, PR.Is_Default, PR.Charge_Type, PR.Is_Operator_Selected, PR.Is_Auto_Range, PR.Cost_Rule_No, PR.Minimum_Cost, PR.Min_Gross_Allowed, PR.Max_Gross_Allowed, PR.Valid_Start_Date, PR.Valid_End_Date, PR.User_Rate_Field1, PR.User_Rate_Field2, PR.User_Rate_Field3, PR.User_Rate_Field4, PR.Sub_Stream_2_No, PR.Rate_Override_Option, PR.Memo, PR.Available, R.Type, R.Name, R.Is_GST, R.Rate_No AS Source_Rate_No FROM "DBA".WM_Product_Rate PR, "DBA".WM_Rate R WHERE PR.Rate_No = R.Rate_No AND PR.Product_No = @ProductNo AND ((PR.Range_No = @SpecificChargeNo) OR (R.Type <> 'C')) AND ((DATEDIFF(Day, PR.Valid_Start_Date, @TranDate) >= 0) OR (PR.Valid_Start_Date IS NULL) OR ((@AllowUnavailable = 'Y') AND (R.Type = 'C'))) AND ((DATEDIFF(Day, @TranDate, PR.Valid_End_Date) >= 0) OR (PR.Valid_End_Date IS NULL) OR ((@AllowUnavailable = 'Y') AND (R.Type = 'C'))) AND ((PR.Available = 'Y') OR (@AllowUnavailable = 'Y')) END IF; END IF; END IF; /* If authorisation service and relevant relationships are enabled, and authorisation entities */ /* are supplied, filter the rate list using the supplied entities */ IF (@CheckAuthorisations = 'Y') AND (@AllowUnavailable = 'N') THEN -- Get Cust and/or Site authorised SELECT Authed_Class_Key INTO @AuthCust FROM #AuthedItems WHERE Authed_Class_ID = 3001; SELECT Authed_Class_Key INTO @AuthSite FROM #AuthedItems WHERE Authed_Class_ID = 0100; -- If site specified, delete all charges that authorise sites other than that specified, -- and do not also authorise the site. -- If site not specified, ASSUME THAT SITE AUTHORISATION IS NOT ENABLED, i.e. do NOT check site authorisations. -- It is the caller's responsibility to supply a site number if that authorisation is to be checked. -- NOTE: If supplied site no is -1, treat as unknown, i.e. only charges authorising all sites will be returned IF (@AuthSite IS NOT NULL) OR (@AuthSite = '-1') THEN DELETE @ChargeInfo WHERE Range_No IN (SELECT Range_No FROM @ChargeInfo WHERE Authorised_Class_ID = 0100 AND Authorised_Class_Key <> @AuthSite AND Rate_Type = 'C') AND Range_No NOT IN (SELECT Range_No FROM @ChargeInfo WHERE Authorised_Class_ID = 0100 AND Authorised_Class_Key = @AuthSite AND Rate_Type = 'C') END IF; -- If customer specified and one or more charges in the group explicitly authorise that customer, -- delete all charges that do NOT explicitly authorise the customer. (This includes charges that do not authorise any customers.) -- If customer specified and none of the charges explicitly authorise that customer, -- delete all charges that explictly authorise any customer. This will leave only those charges that do not explicitly authorise -- any customers. -- If customer not specified, ASSUME THAT CUSTOMER AUTHORISATION IS NOT ENABLED, i.e. do NOT check customer authorisations. -- It is the caller's responsibility to supply a customer number if that authorisation is to be checked. -- NOTE: If supplied customer no is -1, treat as unknown, i.e. only charges not authorising a customer will be returned IF (@AuthCust IS NOT NULL) OR (@AuthCust = '-1') THEN IF EXISTS( SELECT 1 FROM @ChargeInfo WHERE Authorised_Class_ID = 3001 AND Authorised_Class_Key = @AuthCust AND Rate_Type = 'C') THEN --Delete all charges that do not explicitly authorise this customer DELETE @ChargeInfo WHERE Rate_Type = 'C' AND Range_No NOT IN (SELECT Range_No FROM @ChargeInfo WHERE Authorised_Class_ID = 3001 AND Authorised_Class_Key = @AuthCust AND Rate_Type = 'C') ELSE --Delete all charges that explicitly authorise any customer DELETE @ChargeInfo WHERE Range_No IN (SELECT Range_No FROM @ChargeInfo WHERE Authorised_Class_ID = 3001 AND Rate_Type = 'C') END IF; END IF; -- RETURN THE RESULT SET -- NOTE: Must be in order by Net in descending order for DetermineBestRate to work correctly. SELECT PR.Product_No, PR.Rate_No, PR.Range_No, PR.Description, PR.Value, PR.Net, PR.Is_Default, PR.Charge_Type, PR.Is_Operator_Selected, PR.Is_Auto_Range, PR.Cost_Rule_No, PR.Minimum_Cost, PR.Min_Gross_Allowed, PR.Max_Gross_Allowed, PR.Valid_Start_Date, PR.Valid_End_Date, PR.User_Rate_Field1, PR.User_Rate_Field2, PR.User_Rate_Field3, PR.User_Rate_Field4, PR.Sub_Stream_2_No, PR.Rate_Override_Option, PR.Memo, PR.Available, R.Type, R.Name, R.Is_GST, R.Rate_No AS Source_Rate_No FROM "DBA".WM_Product_Rate PR, "DBA".WM_Rate R WHERE (PR.Rate_No = R.Rate_No) AND (PR.Range_No IN (SELECT Range_No FROM @ChargeInfo)) ORDER BY PR.Net DESC, PR.Description; END IF; END; IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; /* NOTE "ONLY" keyword prevents the update from being applied at the consolidated node */ ALTER PROCEDURE "DBA".WM_GetProductCharges(IN @ProductNo Entity_No, IN @TranDate Entity_Date, IN @SpecificChargeNo Entity_No, IN @SpecificChargeType CHAR(1), IN @CheckAuthorisations Boolean, IN @AllowUnavailable Boolean DEFAULT 'N') RESULT( Product_No Entity_No, Rate_No Entity_No, Range_No Entity_No, Description varchar(20), Value numeric(10,4), Net Weight, Is_Default Boolean, Charge_Type Prod_Cost_Method, Is_Operator_Selected Boolean, Is_Auto_Range Boolean, Cost_Rule_No Entity_No, Minimum_Cost numeric(10,4), Min_Gross_Allowed Weight, Max_Gross_Allowed Weight, Valid_Start_Date Entity_Date, Valid_End_Date Entity_Date, User_Rate_Field1 numeric(10,4), User_Rate_Field2 numeric(10,4), User_Rate_Field3 numeric(10,4), User_Rate_Field4 numeric(10,4), Sub_Stream_2_No integer, Rate_Override_Option CHAR(1), Memo Entity_Comment, Available Boolean, Type char(1), Name Entity_Name, Is_GST Boolean, Source_Rate_No Entity_No ) BEGIN DECLARE LOCAL TEMPORARY TABLE @ChargeAuthHeaders( Authorisation_Config_No INTEGER, Authorised_Class_ID INTEGER, Table_Name CHAR(128)); --Declare local temp table to hold result set while processing DECLARE LOCAL TEMPORARY TABLE @ChargeInfo( Range_No INTEGER, Rate_No INTEGER, Rate_Type CHAR(1), Description VARCHAR(20), Authorised_Class_ID INT, Authorised_Class_Key VARCHAR(50), Table_Name CHAR(128), Available CHAR(1)); DECLARE @AuthCust Entity_No; DECLARE @AuthSite Entity_No; INSERT @ChargeAuthHeaders (Authorisation_Config_No, Authorised_Class_ID, Table_Name) SELECT AC.Authorisation_Config_No, AC.Authorised_Class_ID, OC.Table_Name FROM "DBA".SM_Authorisation_Config AC LEFT OUTER JOIN "DBA".SM_Object_Class OC ON (OC.Object_Class = AC.Authorised_Class_ID) --may not exist WHERE AC.Authorising_Class_ID = 3007 --product rate AND AC.Authorised_Process = 3002; --charge process IF @@ERROR <> 0 THEN RETURN; END IF; /* NOTE: The @AllowUnavailable flag will change filtering of Charge-type rates based on authorisations, validity dates and the "Available" flag. */ /* When set to 'Y', the procedure will return rates irrespective of authorisations, validity date settings or "Available" flag. */ /* When set to 'N', the procedure will return only rates that are authorised (optionally), flagged as "Available" and are valid for the supplied @TranDate. */ /* %MG NOTE 05/06/2006 - Rates of type other than 'C' are still subject to validity date checks when @AllowUnavailable = 'Y', to prevent multiples of the same rate from being returned */ IF @SpecificChargeNo IS NULL THEN -- Select all rates for the product. IF (@CheckAuthorisations = 'Y') AND (@AllowUnavailable = 'N') THEN -- Create a "shortlist" of available rates based on Available flag, Validity dates and Authorised entities INSERT @ChargeInfo (Range_No, Rate_No, Rate_Type, Description, Authorised_Class_ID, Authorised_Class_Key, Table_Name, Available) SELECT PR.Range_No, PR.Rate_No, R.Type, PR.Description, CAH.Authorised_Class_ID, AI.Authorised_Class_Key, CAH.Table_Name, PR.Available FROM "DBA".WM_Product_Rate PR INNER JOIN "DBA".WM_Rate R ON (R.Rate_No = PR.Rate_No) LEFT OUTER JOIN "DBA".SM_Authorisation_Item AI ON ((AI.Authorising_Class_Key = CAST(PR.Range_No AS CHAR)) AND (AI.Authorisation_Config_No IN (SELECT Authorisation_Config_No FROM @ChargeAuthHeaders))) LEFT OUTER JOIN @ChargeAuthHeaders CAH ON (CAH.Authorisation_Config_No = AI.Authorisation_Config_No) WHERE (PR.Product_No = @ProductNo) AND ((DATEDIFF(Day, PR.Valid_Start_Date, @TranDate) >= 0) OR (PR.Valid_Start_Date IS NULL)) AND ((DATEDIFF(Day, @TranDate, PR.Valid_End_Date) >= 0) OR (PR.Valid_End_Date IS NULL)) AND (PR.Available = 'Y') ELSE -- Not checking authorisations - just return the result set. -- Must be in order by net in descending order for DetermineBestRate to work correctly. SELECT PR.Product_No, PR.Rate_No, PR.Range_No, PR.Description, PR.Value, PR.Net, PR.Is_Default, PR.Charge_Type, PR.Is_Operator_Selected, PR.Is_Auto_Range, PR.Cost_Rule_No, PR.Minimum_Cost, PR.Min_Gross_Allowed, PR.Max_Gross_Allowed, PR.Valid_Start_Date, PR.Valid_End_Date, PR.User_Rate_Field1, PR.User_Rate_Field2, PR.User_Rate_Field3, PR.User_Rate_Field4, PR.Sub_Stream_2_No, PR.Rate_Override_Option, PR.Memo, PR.Available, R.Type, R.Name, R.Is_GST, R.Rate_No AS Source_Rate_No FROM "DBA".WM_Product_Rate PR, "DBA".WM_Rate R WHERE PR.Rate_No = R.Rate_No AND PR.Product_No = @ProductNo AND ((DATEDIFF(Day, PR.Valid_Start_Date, @TranDate) >= 0) OR (PR.Valid_Start_Date IS NULL) OR ((@AllowUnavailable = 'Y') AND (R.Type = 'C'))) AND ((DATEDIFF(Day, @TranDate, PR.Valid_End_Date) >= 0) OR (PR.Valid_End_Date IS NULL) OR ((@AllowUnavailable = 'Y') AND (R.Type = 'C'))) AND ((PR.Available = 'Y') OR (@AllowUnavailable = 'Y')) ORDER BY PR.Net DESC END IF; ELSE -- Select specific rate. IF (@CheckAuthorisations = 'Y') AND (@AllowUnavailable = 'N') THEN -- Create a "shortlist" of available rates based on Available flag, Validity dates and Authorised entities -- If the specified rate has the auto range flag then return the auto range group that includes the rate. IF EXISTS( SELECT 1 FROM "DBA".WM_Product_Rate WHERE Product_No = @ProductNo AND ((Range_No = @SpecificChargeNo) AND (Is_Auto_Range = 'Y'))) THEN INSERT @ChargeInfo (Range_No, Rate_No, Rate_Type, Description, Authorised_Class_ID, Authorised_Class_Key, Table_Name, Available) SELECT PR.Range_No, PR.Rate_No, R.Type, PR.Description, CAH.Authorised_Class_ID, AI.Authorised_Class_Key, CAH.Table_Name, PR.Available FROM "DBA".WM_Product_Rate PR INNER JOIN "DBA".WM_Rate R ON (R.Rate_No = PR.Rate_No) LEFT OUTER JOIN "DBA".SM_Authorisation_Item AI ON ((AI.Authorising_Class_Key = CAST(PR.Range_No AS CHAR)) AND (AI.Authorisation_Config_No IN (SELECT Authorisation_Config_No FROM @ChargeAuthHeaders))) LEFT OUTER JOIN @ChargeAuthHeaders CAH ON (CAH.Authorisation_Config_No = AI.Authorisation_Config_No) WHERE (PR.Product_No = @ProductNo) AND (((R.Type = 'C') AND (PR.Is_Auto_Range = 'Y')) OR (R.Type <> 'C')) AND ((DATEDIFF(Day, PR.Valid_Start_Date, @TranDate) >= 0) OR (PR.Valid_Start_Date IS NULL)) AND ((DATEDIFF(Day, @TranDate, PR.Valid_End_Date) >= 0) OR (PR.Valid_End_Date IS NULL)) AND (PR.Available = 'Y') ELSE INSERT @ChargeInfo (Range_No, Rate_No, Rate_Type, Description, Authorised_Class_ID, Authorised_Class_Key, Table_Name, Available) SELECT PR.Range_No, PR.Rate_No, R.Type, PR.Description, CAH.Authorised_Class_ID, AI.Authorised_Class_Key, CAH.Table_Name, PR.Available FROM "DBA".WM_Product_Rate PR INNER JOIN "DBA".WM_Rate R ON (R.Rate_No = PR.Rate_No) LEFT OUTER JOIN "DBA".SM_Authorisation_Item AI ON ((AI.Authorising_Class_Key = CAST(PR.Range_No AS CHAR)) AND (AI.Authorisation_Config_No IN (SELECT Authorisation_Config_No FROM @ChargeAuthHeaders))) LEFT OUTER JOIN @ChargeAuthHeaders CAH ON (CAH.Authorisation_Config_No = AI.Authorisation_Config_No) WHERE (PR.Product_No = @ProductNo) AND ((PR.Range_No = @SpecificChargeNo) OR (R.Type <> 'C')) AND ((DATEDIFF(Day, PR.Valid_Start_Date, @TranDate) >= 0) OR (PR.Valid_Start_Date IS NULL)) AND ((DATEDIFF(Day, @TranDate, PR.Valid_End_Date) >= 0) OR (PR.Valid_End_Date IS NULL)) AND (PR.Available = 'Y') END IF; ELSE -- Not checking authorisations - just return the result set. -- Must be in order by net in descending order for DetermineBestRate to work correctly. -- If the specified rate has the auto range flag then return the auto range group that includes the rate. IF EXISTS( SELECT 1 FROM "DBA".WM_Product_Rate PR, "DBA".WM_Rate R WHERE PR.Rate_No = R.Rate_No AND PR.Product_No = @ProductNo AND ((PR.Range_No = @SpecificChargeNo) AND (PR.Is_Auto_Range = 'Y'))) THEN SELECT PR.Product_No, PR.Rate_No, PR.Range_No, PR.Description, PR.Value, PR.Net, PR.Is_Default, PR.Charge_Type, PR.Is_Operator_Selected, PR.Is_Auto_Range, PR.Cost_Rule_No, PR.Minimum_Cost, PR.Min_Gross_Allowed, PR.Max_Gross_Allowed, PR.Valid_Start_Date, PR.Valid_End_Date, PR.User_Rate_Field1, PR.User_Rate_Field2, PR.User_Rate_Field3, PR.User_Rate_Field4, PR.Sub_Stream_2_No, PR.Rate_Override_Option, PR.Memo, PR.Available, R.Type, R.Name, R.Is_GST, R.Rate_No AS Source_Rate_No FROM "DBA".WM_Product_Rate PR, "DBA".WM_Rate R WHERE PR.Rate_No = R.Rate_No AND PR.Product_No = @ProductNo AND ((R.Type = 'C' AND PR.Is_Auto_Range = 'Y') OR (R.Type <> 'C')) AND ((DATEDIFF(Day, PR.Valid_Start_Date, @TranDate) >= 0) OR (PR.Valid_Start_Date IS NULL) OR ((@AllowUnavailable = 'Y') AND (R.Type = 'C'))) AND ((DATEDIFF(Day, @TranDate, PR.Valid_End_Date) >= 0) OR (PR.Valid_End_Date IS NULL) OR ((@AllowUnavailable = 'Y') AND (R.Type = 'C'))) AND ((PR.Available = 'Y') OR (@AllowUnavailable = 'Y')) ORDER BY PR.Net DESC ELSE SELECT PR.Product_No, PR.Rate_No, PR.Range_No, PR.Description, PR.Value, PR.Net, PR.Is_Default, PR.Charge_Type, PR.Is_Operator_Selected, PR.Is_Auto_Range, PR.Cost_Rule_No, PR.Minimum_Cost, PR.Min_Gross_Allowed, PR.Max_Gross_Allowed, PR.Valid_Start_Date, PR.Valid_End_Date, PR.User_Rate_Field1, PR.User_Rate_Field2, PR.User_Rate_Field3, PR.User_Rate_Field4, PR.Sub_Stream_2_No, PR.Rate_Override_Option, PR.Memo, PR.Available, R.Type, R.Name, R.Is_GST, R.Rate_No AS Source_Rate_No FROM "DBA".WM_Product_Rate PR, "DBA".WM_Rate R WHERE PR.Rate_No = R.Rate_No AND PR.Product_No = @ProductNo AND ((PR.Range_No = @SpecificChargeNo) OR (R.Type <> 'C')) AND ((DATEDIFF(Day, PR.Valid_Start_Date, @TranDate) >= 0) OR (PR.Valid_Start_Date IS NULL) OR ((@AllowUnavailable = 'Y') AND (R.Type = 'C'))) AND ((DATEDIFF(Day, @TranDate, PR.Valid_End_Date) >= 0) OR (PR.Valid_End_Date IS NULL) OR ((@AllowUnavailable = 'Y') AND (R.Type = 'C'))) AND ((PR.Available = 'Y') OR (@AllowUnavailable = 'Y')) END IF; END IF; END IF; /* If authorisation service and relevant relationships are enabled, and authorisation entities */ /* are supplied, filter the rate list using the supplied entities */ IF (@CheckAuthorisations = 'Y') AND (@AllowUnavailable = 'N') THEN -- Get Cust and/or Site authorised SELECT Authed_Class_Key INTO @AuthCust FROM #AuthedItems WHERE Authed_Class_ID = 3001; SELECT Authed_Class_Key INTO @AuthSite FROM #AuthedItems WHERE Authed_Class_ID = 0100; -- If site specified, delete all charges that authorise sites other than that specified, -- and do not also authorise the site. -- If site not specified, ASSUME THAT SITE AUTHORISATION IS NOT ENABLED, i.e. do NOT check site authorisations. -- It is the caller's responsibility to supply a site number if that authorisation is to be checked. -- NOTE: If supplied site no is -1, treat as unknown, i.e. only charges authorising all sites will be returned IF (@AuthSite IS NOT NULL) OR (@AuthSite = '-1') THEN DELETE @ChargeInfo WHERE Range_No IN (SELECT Range_No FROM @ChargeInfo WHERE Authorised_Class_ID = 0100 AND Authorised_Class_Key <> @AuthSite AND Rate_Type = 'C') AND Range_No NOT IN (SELECT Range_No FROM @ChargeInfo WHERE Authorised_Class_ID = 0100 AND Authorised_Class_Key = @AuthSite AND Rate_Type = 'C') END IF; -- If customer specified and one or more charges in the group explicitly authorise that customer, -- delete all charges that do NOT explicitly authorise the customer. (This includes charges that do not authorise any customers.) -- If customer specified and none of the charges explicitly authorise that customer, -- delete all charges that explictly authorise any customer. This will leave only those charges that do not explicitly authorise -- any customers. -- If customer not specified, ASSUME THAT CUSTOMER AUTHORISATION IS NOT ENABLED, i.e. do NOT check customer authorisations. -- It is the caller's responsibility to supply a customer number if that authorisation is to be checked. -- NOTE: If supplied customer no is -1, treat as unknown, i.e. only charges not authorising a customer will be returned IF (@AuthCust IS NOT NULL) OR (@AuthCust = '-1') THEN IF EXISTS( SELECT 1 FROM @ChargeInfo WHERE Authorised_Class_ID = 3001 AND Authorised_Class_Key = @AuthCust AND Rate_Type = 'C') THEN --Delete all charges that do not explicitly authorise this customer DELETE @ChargeInfo WHERE Rate_Type = 'C' AND Range_No NOT IN (SELECT Range_No FROM @ChargeInfo WHERE Authorised_Class_ID = 3001 AND Authorised_Class_Key = @AuthCust AND Rate_Type = 'C') ELSE --Delete all charges that explicitly authorise any customer DELETE @ChargeInfo WHERE Range_No IN (SELECT Range_No FROM @ChargeInfo WHERE Authorised_Class_ID = 3001 AND Rate_Type = 'C') END IF; END IF; -- RETURN THE RESULT SET -- NOTE: Must be in order by Net in descending order for DetermineBestRate to work correctly. SELECT PR.Product_No, PR.Rate_No, PR.Range_No, PR.Description, PR.Value, PR.Net, PR.Is_Default, PR.Charge_Type, PR.Is_Operator_Selected, PR.Is_Auto_Range, PR.Cost_Rule_No, PR.Minimum_Cost, PR.Min_Gross_Allowed, PR.Max_Gross_Allowed, PR.Valid_Start_Date, PR.Valid_End_Date, PR.User_Rate_Field1, PR.User_Rate_Field2, PR.User_Rate_Field3, PR.User_Rate_Field4, PR.Sub_Stream_2_No, PR.Rate_Override_Option, PR.Memo, PR.Available, R.Type, R.Name, R.Is_GST, R.Rate_No AS Source_Rate_No FROM "DBA".WM_Product_Rate PR, "DBA".WM_Rate R WHERE (PR.Rate_No = R.Rate_No) AND (PR.Range_No IN (SELECT Range_No FROM @ChargeInfo)) ORDER BY PR.Net DESC, PR.Description; END IF; END; --Update remote version control UPDATE "DBA".SM_Version_Control SET Version_No = 8 WHERE Entity_Name = 'WM_GetProductCharges' AND Database_ID = (SELECT db_property('GlobalDbID')); PASSTHROUGH STOP; END IF; --Update consolidated version control UPDATE "DBA".SM_Version_Control SET Version_No = 8 WHERE Entity_Name = 'WM_GetProductCharges' AND Database_ID = (SELECT db_property('GlobalDbID')); END IF; /********************************************************************/ /* 16/10/2006 - Modify result set of WM_GetProductChargesAuth as */ /* Rate_Override_Option is now returned by WM_GetProductCharges */ /********************************************************************/ IF EXISTS( SELECT 1 FROM "DBA".SM_Version_Control WHERE Entity_Name = 'WM_GetProductChargesAuth' AND Database_ID = (SELECT db_property('GlobalDbID')) AND Version_No < 3) THEN ALTER PROCEDURE "DBA".WM_GetProductChargesAuth(IN @ProductNo Entity_No, IN @TranDate Entity_Date, IN @SpecificChargeNo Entity_No, IN @SpecificChargeType CHAR(1), IN @SiteNo Entity_No, IN @CustomerNo Entity_No, IN @AllowUnavailable Boolean DEFAULT 'N') RESULT( Product_No Entity_No, Rate_No Entity_No, Range_No Entity_No, Description varchar(20), Value numeric(10,4), Net Weight, Is_Default Boolean, Charge_Type Prod_Cost_Method, Is_Operator_Selected Boolean, Is_Auto_Range Boolean, Cost_Rule_No Entity_No, Minimum_Cost numeric(10,4), Min_Gross_Allowed Weight, Max_Gross_Allowed Weight, Valid_Start_Date Entity_Date, Valid_End_Date Entity_Date, User_Rate_Field1 numeric(10,4), User_Rate_Field2 numeric(10,4), User_Rate_Field3 numeric(10,4), User_Rate_Field4 numeric(10,4), Sub_Stream_2_No integer, Rate_Override_Option CHAR(1), Memo Entity_Comment, Available Boolean, Type char(1), Name Entity_Name, Is_GST Boolean, Source_Rate_No Entity_No ) BEGIN DECLARE LOCAL TEMPORARY TABLE #AuthedItems( Authed_Class_ID INT, Authed_Class_Key VARCHAR(50)); --Transaction Value: Site IF @SiteNo IS NOT NULL THEN INSERT #AuthedItems(Authed_Class_ID, Authed_Class_Key) VALUES('0100', CAST(@SiteNo AS CHAR)); END IF; --Transaction Value: Customer IF @CustomerNo IS NOT NULL THEN INSERT #AuthedItems(Authed_Class_ID, Authed_Class_Key) VALUES('3001', CAST(@CustomerNo AS CHAR)); END IF; CALL "DBA".WM_GetProductCharges (@ProductNo, @TranDate, @SpecificChargeNo, @SpecificChargeType, 'Y', @AllowUnavailable); DROP TABLE #AuthedItems; END; IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; /* NOTE "ONLY" keyword prevents the update from being applied at the consolidated node */ ALTER PROCEDURE "DBA".WM_GetProductChargesAuth(IN @ProductNo Entity_No, IN @TranDate Entity_Date, IN @SpecificChargeNo Entity_No, IN @SpecificChargeType CHAR(1), IN @SiteNo Entity_No, IN @CustomerNo Entity_No, IN @AllowUnavailable Boolean DEFAULT 'N') RESULT( Product_No Entity_No, Rate_No Entity_No, Range_No Entity_No, Description varchar(20), Value numeric(10,4), Net Weight, Is_Default Boolean, Charge_Type Prod_Cost_Method, Is_Operator_Selected Boolean, Is_Auto_Range Boolean, Cost_Rule_No Entity_No, Minimum_Cost numeric(10,4), Min_Gross_Allowed Weight, Max_Gross_Allowed Weight, Valid_Start_Date Entity_Date, Valid_End_Date Entity_Date, User_Rate_Field1 numeric(10,4), User_Rate_Field2 numeric(10,4), User_Rate_Field3 numeric(10,4), User_Rate_Field4 numeric(10,4), Sub_Stream_2_No integer, Rate_Override_Option CHAR(1), Memo Entity_Comment, Available Boolean, Type char(1), Name Entity_Name, Is_GST Boolean, Source_Rate_No Entity_No ) BEGIN DECLARE LOCAL TEMPORARY TABLE #AuthedItems( Authed_Class_ID INT, Authed_Class_Key VARCHAR(50)); --Transaction Value: Site IF @SiteNo IS NOT NULL THEN INSERT #AuthedItems(Authed_Class_ID, Authed_Class_Key) VALUES('0100', CAST(@SiteNo AS CHAR)); END IF; --Transaction Value: Customer IF @CustomerNo IS NOT NULL THEN INSERT #AuthedItems(Authed_Class_ID, Authed_Class_Key) VALUES('3001', CAST(@CustomerNo AS CHAR)); END IF; CALL "DBA".WM_GetProductCharges (@ProductNo, @TranDate, @SpecificChargeNo, @SpecificChargeType, 'Y', @AllowUnavailable); DROP TABLE #AuthedItems; END; --Update remote version control UPDATE "DBA".SM_Version_Control SET Version_No = 3 WHERE Entity_Name = 'WM_GetProductChargesAuth' AND Database_ID = (SELECT db_property('GlobalDbID')); PASSTHROUGH STOP; END IF; --Update consolidated version control UPDATE "DBA".SM_Version_Control SET Version_No = 3 WHERE Entity_Name = 'WM_GetProductChargesAuth' AND Database_ID = (SELECT db_property('GlobalDbID')); END IF; /********************************************************************/ /* 17/10/2006 - Add column DCS_Description to table WM_Job */ /********************************************************************/ IF NOT EXISTS( SELECT 1 FROM syscolumn JOIN systable WHERE table_name = 'WM_Job' AND column_name = 'DCS_Description') THEN ALTER TABLE "DBA".WM_Job ADD DCS_Description Entity_Code; --Passthrough the update to the remote sites (if in a replicated environment) IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; ALTER TABLE "DBA".WM_Job ADD DCS_Description Entity_Code; PASSTHROUGH STOP; END IF; END IF; /********************************************************************/ /* 17/10/2006 - Add column DCS_Description to table WM_Product */ /********************************************************************/ IF NOT EXISTS( SELECT 1 FROM syscolumn JOIN systable WHERE table_name = 'WM_Product' AND column_name = 'DCS_Description') THEN ALTER TABLE "DBA".WM_Product ADD DCS_Description Entity_Code; --Passthrough the update to the remote sites (if in a replicated environment) IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; ALTER TABLE "DBA".WM_Product ADD DCS_Description Entity_Code; PASSTHROUGH STOP; END IF; END IF; /********************************************************************/ /* 18/10/2006 - Add column Original_Rate_Range_No to table WM_Transaction_Item */ /********************************************************************/ IF NOT EXISTS( SELECT 1 FROM syscolumn JOIN systable WHERE table_name = 'WM_Transaction_Item' AND column_name = 'Original_Rate_Range_No') THEN ALTER TABLE "DBA".WM_Transaction_Item ADD Original_Rate_Range_No Entity_No NULL; alter table "DBA".WM_Transaction_Item add constraint FK_WM_TRANITEM_REF_WM_PRODRATE2 foreign key (Original_Rate_Range_No) references "DBA".WM_Product_Rate (Range_No) on update restrict on delete restrict; --Passthrough the update to the remote sites (if in a replicated environment) IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_Site_Data') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_Site_Data; ALTER TABLE "DBA".WM_Transaction_Item ADD Original_Rate_Range_No Entity_No NULL; alter table "DBA".WM_Transaction_Item add constraint FK_WM_TRANITEM_REF_WM_PRODRATE2 foreign key (Original_Rate_Range_No) references "DBA".WM_Product_Rate (Range_No) on update restrict on delete restrict; PASSTHROUGH STOP; END IF; END IF; --Add to WM_Transaction_Item_A IF NOT EXISTS( SELECT 1 FROM syscolumn JOIN systable WHERE table_name = 'WM_Transaction_Item_A' AND column_name = 'Original_Rate_Range_No') THEN ALTER TABLE "DBA".WM_Transaction_Item_A ADD Original_Rate_Range_No Entity_No NULL; alter table "DBA".WM_Transaction_Item_A add constraint FK_WM_TRANITEM_REF_WM_PRODRATE_A2 foreign key (Original_Rate_Range_No) references "DBA".WM_Product_Rate (Range_No) on update restrict on delete restrict; --Passthrough the update to the remote sites (if in a replicated environment) IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_Site_Data') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_Site_Data; ALTER TABLE "DBA".WM_Transaction_Item_A ADD Original_Rate_Range_No Entity_No NULL; alter table "DBA".WM_Transaction_Item_A add constraint FK_WM_TRANITEM_REF_WM_PRODRATE_A2 foreign key (Original_Rate_Range_No) references "DBA".WM_Product_Rate (Range_No) on update restrict on delete restrict; PASSTHROUGH STOP; END IF; END IF; --Update the delete trigger IF EXISTS( SELECT 1 FROM "DBA".SM_Version_Control WHERE Entity_Name = 'bd_WM_Transaction_Item' AND Database_ID = (SELECT db_property('GlobalDbID')) AND Version_No < 3) THEN ALTER TRIGGER bd_WM_Transaction_Item BEFORE DELETE ORDER 1 ON "DBA".WM_Transaction_Item REFERENCING OLD AS Old_Rec FOR EACH ROW BEGIN /* Don't attempt to move the record across if the Foreign Key doesn't exist */ IF EXISTS (SELECT 1 FROM WM_Transaction_Header_A WHERE Transaction_No = old_Rec.Transaction_No) THEN INSERT WM_Transaction_Item_A (Transaction_No, Transaction_Item_No, Area_No, Product_No, Waste_Stream_No, Sub_Stream_1_No, Sub_Stream_2_No, Sub_Stream_3_No, Gross, Tare, Net, Cubic_Metres, Quantity, LGA_code, Cost_Rule_No, Rate_Range_No, Cost_Method, Item_Charge, EPA_Levy_Exempt, Original_Rate_Range_No) VALUES (old_Rec.Transaction_No, old_Rec.Transaction_Item_No, old_Rec.Area_No, old_Rec.Product_No, old_Rec.Waste_Stream_No, old_Rec.Sub_Stream_1_No, old_Rec.Sub_Stream_2_No, old_Rec.Sub_Stream_3_No, old_Rec.Gross, old_Rec.Tare, old_Rec.Net, old_Rec.Cubic_Metres, old_Rec.Quantity, old_Rec.LGA_code, old_Rec.Cost_Rule_No, old_Rec.Rate_Range_No, old_Rec.Cost_Method, old_Rec.Item_Charge, old_Rec.EPA_Levy_Exempt, old_Rec.Original_Rate_Range_No) END IF; END; IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_Site_Data') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_Site_Data; ALTER TRIGGER bd_WM_Transaction_Item BEFORE DELETE ORDER 1 ON "DBA".WM_Transaction_Item REFERENCING OLD AS Old_Rec FOR EACH ROW BEGIN /* Don't attempt to move the record across if the Foreign Key doesn't exist */ IF EXISTS (SELECT 1 FROM WM_Transaction_Header_A WHERE Transaction_No = old_Rec.Transaction_No) THEN INSERT WM_Transaction_Item_A (Transaction_No, Transaction_Item_No, Area_No, Product_No, Waste_Stream_No, Sub_Stream_1_No, Sub_Stream_2_No, Sub_Stream_3_No, Gross, Tare, Net, Cubic_Metres, Quantity, LGA_code, Cost_Rule_No, Rate_Range_No, Cost_Method, Item_Charge, EPA_Levy_Exempt, Original_Rate_Range_No) VALUES (old_Rec.Transaction_No, old_Rec.Transaction_Item_No, old_Rec.Area_No, old_Rec.Product_No, old_Rec.Waste_Stream_No, old_Rec.Sub_Stream_1_No, old_Rec.Sub_Stream_2_No, old_Rec.Sub_Stream_3_No, old_Rec.Gross, old_Rec.Tare, old_Rec.Net, old_Rec.Cubic_Metres, old_Rec.Quantity, old_Rec.LGA_code, old_Rec.Cost_Rule_No, old_Rec.Rate_Range_No, old_Rec.Cost_Method, old_Rec.Item_Charge, old_Rec.EPA_Levy_Exempt, old_Rec.Original_Rate_Range_No) END IF; END; --Update remote version control UPDATE "DBA".SM_Version_Control SET Version_No = 3 WHERE Entity_Name = 'bd_WM_Transaction_Item' AND Database_ID = (SELECT db_property('GlobalDbID')); PASSTHROUGH STOP; END IF; --Update consolidated version control UPDATE "DBA".SM_Version_Control SET Version_No = 3 WHERE Entity_Name = 'bd_WM_Transaction_Item' AND Database_ID = (SELECT db_property('GlobalDbID')); END IF; /********************************************************************/ /* 18/10/2006 - Add stored procedure to return Incomplete transaction details */ /********************************************************************/ IF NOT EXISTS( SELECT 1 FROM sysprocedure WHERE proc_name = 'WM_BaseFrame_IncompleteTran') THEN CREATE PROCEDURE "DBA".WM_BaseFrame_IncompleteTran(IN @TranNo Large_Entity_No, IN @ProductChargeRateNo Entity_No, IN @ChargeOverrideExceptionCode Entity_Code) RESULT( Transaction_No Large_Entity_No, Docket_No INTEGER, Docket_No_Prefix CHAR(3), Docket_No_Suffix CHAR(3), Docket_No_Version INTEGER, Vehicle_No Entity_No, Vehicle_Code Entity_Code, Customer_No Entity_No, Job_No Entity_No, Carrier_No Entity_No, Gross Weight, Gross_Date Entity_Date, Gross_Time Entity_Time, Memo Entity_Comment, Date_Complete Entity_Date, Time_Complete Entity_Time, Created_At Entity_No, Shift_No Entity_No, Created_By Operator_Code, Is_Fully_Paid Boolean, Amount_Paid NUMERIC(8,2), DateTime_In Entity_DateTime, DateTime_Out Entity_DateTime, Custom_Data1 Entity_Code, Order_No VARCHAR(30), Transaction_Item_No Large_Entity_No, Product_No Entity_No, Area_No Entity_No, Is_Mandatory Boolean, Job_Prod_Charge_Type CHAR(1), Waste_Stream_No Entity_No, Sub_Stream_1_No Entity_No, SS1_Name Entity_Name, LGA_Code Entity_Code, Rate_Range_No Entity_No, Cost_Method Prod_Cost_Method, Quantity NUMERIC(19,6), Item_Net Weight, Item_CM NUMERIC(19,6), Sub_Stream_2_No Entity_No, Sub_Stream_3_No Entity_No, Has_Override Boolean, Override_User Operator_Code, Override_Comment Entity_Comment, Charge_Rate NUMERIC(10,4), Original_Rate_Range_No Entity_No ) BEGIN SELECT H.Transaction_No, H.Docket_No, H.Docket_No_Prefix, H.Docket_No_Suffix, H.Docket_No_Version, H.Vehicle_No, H.Vehicle_Code, H.Customer_No, H.Job_No, H.Carrier_No, H.Gross, H.Gross_Date, H.Gross_Time, H.Memo, H.Date_Complete, H.Time_Complete, H.Created_At, H.Shift_No, H.Created_By, H.Is_Fully_Paid, H.Amount_Paid, H.DateTime_In, H.DateTime_Out, H.Custom_Data1, H.Order_No, I.Transaction_Item_No, I.Product_No, I.Area_No, JAP.Is_Mandatory, JAP.Charge_Type AS Job_Prod_Charge_Type, I.Waste_Stream_No, I.Sub_Stream_1_No, E.Name AS SS1_Name, I.LGA_Code, I.Rate_Range_No, I.Cost_Method, I.Quantity, I.Net AS Item_Net, I.Cubic_Metres AS Item_CM, I.Sub_Stream_2_No, I.Sub_Stream_3_No, (CASE A.Code WHEN @ChargeOverrideExceptionCode THEN 'Y' ELSE 'N' END) AS Has_Override, A.Operator_Code AS Override_User, A.Exception_Comment AS Override_Comment, B.Rate AS Charge_Rate, I.Original_Rate_Range_No FROM WM_Transaction_Header H INNER JOIN WM_Transaction_Item I ON (I.Transaction_No = H.Transaction_No) LEFT OUTER JOIN WM_Job_Auth_Product JAP ON ((JAP.Job_No = H.Job_No) AND (JAP.Product_No = I.Product_No)) LEFT OUTER JOIN WM_Waste_Sub_Stream_1 E ON (E.Sub_Stream_1_No = I.Sub_Stream_1_No) LEFT OUTER JOIN ( SELECT TEx.Transaction_No, Ex.Code, TEx.Operator_Code, TEx.Exception_Comment FROM WM_Transaction_Exception TEx INNER JOIN WM_Exceptions Ex ON (Ex.Exception_No = TEx.Exception_No) ) A ON (A.Transaction_No = H.Transaction_No) AND (A.Code = @ChargeOverrideExceptionCode) LEFT OUTER JOIN ( SELECT Transaction_No, Transaction_Item_No, Rate, Rate_No FROM WM_Transaction_Item_Rate ) B ON (B.Transaction_No = I.Transaction_No) AND (B.Transaction_Item_No = I.Transaction_Item_No) AND (B.Rate_No = @ProductChargeRateNo) WHERE H.Transaction_No = @TranNo AND Is_Completed = 'N'; END; IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; CREATE PROCEDURE "DBA".WM_BaseFrame_IncompleteTran(IN @TranNo Large_Entity_No, IN @ProductChargeRateNo Entity_No, IN @ChargeOverrideExceptionCode Entity_Code) RESULT( Transaction_No Large_Entity_No, Docket_No INTEGER, Docket_No_Prefix CHAR(3), Docket_No_Suffix CHAR(3), Docket_No_Version INTEGER, Vehicle_No Entity_No, Vehicle_Code Entity_Code, Customer_No Entity_No, Job_No Entity_No, Carrier_No Entity_No, Gross Weight, Gross_Date Entity_Date, Gross_Time Entity_Time, Memo Entity_Comment, Date_Complete Entity_Date, Time_Complete Entity_Time, Created_At Entity_No, Shift_No Entity_No, Created_By Operator_Code, Is_Fully_Paid Boolean, Amount_Paid NUMERIC(8,2), DateTime_In Entity_DateTime, DateTime_Out Entity_DateTime, Custom_Data1 Entity_Code, Order_No VARCHAR(30), Transaction_Item_No Large_Entity_No, Product_No Entity_No, Area_No Entity_No, Is_Mandatory Boolean, Job_Prod_Charge_Type CHAR(1), Waste_Stream_No Entity_No, Sub_Stream_1_No Entity_No, SS1_Name Entity_Name, LGA_Code Entity_Code, Rate_Range_No Entity_No, Cost_Method Prod_Cost_Method, Quantity NUMERIC(19,6), Item_Net Weight, Item_CM NUMERIC(19,6), Sub_Stream_2_No Entity_No, Sub_Stream_3_No Entity_No, Has_Override Boolean, Override_User Operator_Code, Override_Comment Entity_Comment, Charge_Rate NUMERIC(10,4), Original_Rate_Range_No Entity_No ) BEGIN SELECT H.Transaction_No, H.Docket_No, H.Docket_No_Prefix, H.Docket_No_Suffix, H.Docket_No_Version, H.Vehicle_No, H.Vehicle_Code, H.Customer_No, H.Job_No, H.Carrier_No, H.Gross, H.Gross_Date, H.Gross_Time, H.Memo, H.Date_Complete, H.Time_Complete, H.Created_At, H.Shift_No, H.Created_By, H.Is_Fully_Paid, H.Amount_Paid, H.DateTime_In, H.DateTime_Out, H.Custom_Data1, H.Order_No, I.Transaction_Item_No, I.Product_No, I.Area_No, JAP.Is_Mandatory, JAP.Charge_Type AS Job_Prod_Charge_Type, I.Waste_Stream_No, I.Sub_Stream_1_No, E.Name AS SS1_Name, I.LGA_Code, I.Rate_Range_No, I.Cost_Method, I.Quantity, I.Net AS Item_Net, I.Cubic_Metres AS Item_CM, I.Sub_Stream_2_No, I.Sub_Stream_3_No, (CASE A.Code WHEN @ChargeOverrideExceptionCode THEN 'Y' ELSE 'N' END) AS Has_Override, A.Operator_Code AS Override_User, A.Exception_Comment AS Override_Comment, B.Rate AS Charge_Rate, I.Original_Rate_Range_No FROM WM_Transaction_Header H INNER JOIN WM_Transaction_Item I ON (I.Transaction_No = H.Transaction_No) LEFT OUTER JOIN WM_Job_Auth_Product JAP ON ((JAP.Job_No = H.Job_No) AND (JAP.Product_No = I.Product_No)) LEFT OUTER JOIN WM_Waste_Sub_Stream_1 E ON (E.Sub_Stream_1_No = I.Sub_Stream_1_No) LEFT OUTER JOIN ( SELECT TEx.Transaction_No, Ex.Code, TEx.Operator_Code, TEx.Exception_Comment FROM WM_Transaction_Exception TEx INNER JOIN WM_Exceptions Ex ON (Ex.Exception_No = TEx.Exception_No) ) A ON (A.Transaction_No = H.Transaction_No) AND (A.Code = @ChargeOverrideExceptionCode) LEFT OUTER JOIN ( SELECT Transaction_No, Transaction_Item_No, Rate, Rate_No FROM WM_Transaction_Item_Rate ) B ON (B.Transaction_No = I.Transaction_No) AND (B.Transaction_Item_No = I.Transaction_Item_No) AND (B.Rate_No = @ProductChargeRateNo) WHERE H.Transaction_No = @TranNo AND Is_Completed = 'N'; END; PASSTHROUGH STOP; END IF; END IF; /********************************************************************/ /* 19/10/2006 - Add stored procedure to return Completed transaction details */ /********************************************************************/ IF NOT EXISTS( SELECT 1 FROM sysprocedure WHERE proc_name = 'WM_BaseFrame_CompletedTran') THEN CREATE PROCEDURE "DBA".WM_BaseFrame_CompletedTran(IN @TranNo Large_Entity_No, IN @ProductChargeRateNo Entity_No, IN @ChargeOverrideExceptionCode Entity_Code) RESULT( Transaction_No Large_Entity_No, Docket_No INTEGER, Docket_No_Prefix CHAR(3), Docket_No_Suffix CHAR(3), Docket_No_Version INTEGER, Vehicle_No Entity_No, Vehicle_Code Entity_Code, Customer_No Entity_No, Job_No Entity_No, Carrier_No Entity_No, Gross Weight, Gross_Date Entity_Date, Gross_Time Entity_Time, Tare Weight, Tare_Date Entity_Date, Tare_Time Entity_Time, Net Weight, Memo Entity_Comment, Date_Complete Entity_Date, Time_Complete Entity_Time, Created_At Entity_No, Shift_No Entity_No, Created_By Operator_Code, Is_Fully_Paid Boolean, Amount_Paid NUMERIC(8,2), Is_Stored_Tare Boolean, DateTime_In Entity_DateTime, DateTime_Out Entity_DateTime, Custom_Data1 Entity_Code, Order_No VARCHAR(30), Reversal_Tran_No Large_Entity_No, Original_Tran_No Large_Entity_No, Transaction_Item_No Large_Entity_No, Product_No Entity_No, Area_No Entity_No, Waste_Stream_No Entity_No, Sub_Stream_1_No Entity_No, SS1_Name Entity_Name, LGA_Code Entity_Code, Rate_Range_No Entity_No, Cost_Method Prod_Cost_Method, Cost_Rule_No Entity_No, Item_Charge NUMERIC(8,2), Quantity NUMERIC(19,6), Item_Net Weight, Item_CM NUMERIC(19,6), Sub_Stream_2_No Entity_No, Sub_Stream_3_No Entity_No, Has_Override Boolean, Override_User Operator_Code, Override_Comment Entity_Comment, Charge_Rate NUMERIC(10,4), Original_Rate_Range_No Entity_No ) BEGIN SELECT H.Transaction_No, H.Docket_No, H.Docket_No_Prefix, H.Docket_No_Suffix, H.Docket_No_Version, H.Vehicle_No, H.Vehicle_Code, H.Customer_No, H.Job_No, H.Carrier_No, H.Gross, H.Gross_Date, H.Gross_Time, H.Tare, H.Tare_Date, H.Tare_Time, H.Net, H.Memo, H.Date_Complete, H.Time_Complete, H.Created_At, H.Shift_No, H.Created_By, H.Is_Fully_Paid, H.Amount_Paid, H.Is_Stored_Tare, H.DateTime_In, H.DateTime_Out, H.Custom_Data1, H.Order_No, H.Reversal_Tran_No, H.Original_Tran_No, I.Transaction_Item_No, I.Product_No, I.Area_No, I.Waste_Stream_No, I.Sub_Stream_1_No, E.Name AS SS1_Name, I.LGA_Code, I.Rate_Range_No, I.Cost_Method, I.Cost_Rule_No, I.Item_Charge, I.Quantity, I.Net AS Item_Net, I.Cubic_Metres AS Item_CM, I.Sub_Stream_2_No, I.Sub_Stream_3_No, (CASE A.Code WHEN @ChargeOverrideExceptionCode THEN 'Y' ELSE 'N' END) AS Has_Override, A.Operator_Code AS Override_User, A.Exception_Comment AS Override_Comment, B.Rate AS Charge_Rate, I.Original_Rate_Range_No FROM WM_Transaction_Header H INNER JOIN WM_Transaction_Item I ON (I.Transaction_No = H.Transaction_No) LEFT OUTER JOIN WM_Waste_Sub_Stream_1 E ON (E.Sub_Stream_1_No = I.Sub_Stream_1_No) LEFT OUTER JOIN ( SELECT TEx.Transaction_No, Ex.Code, TEx.Operator_Code, TEx.Exception_Comment FROM WM_Transaction_Exception TEx INNER JOIN WM_Exceptions Ex ON (Ex.Exception_No = TEx.Exception_No) ) A ON (A.Transaction_No = H.Transaction_No) AND (A.Code = @ChargeOverrideExceptionCode) LEFT OUTER JOIN ( SELECT Transaction_No, Transaction_Item_No, Rate, Rate_No FROM WM_Transaction_Item_Rate ) B ON (B.Transaction_No = I.Transaction_No) AND (B.Transaction_Item_No = I.Transaction_Item_No) AND (B.Rate_No = @ProductChargeRateNo) WHERE H.Transaction_No = @TranNo AND Is_Completed = 'Y'; END; IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; CREATE PROCEDURE "DBA".WM_BaseFrame_CompletedTran(IN @TranNo Large_Entity_No, IN @ProductChargeRateNo Entity_No, IN @ChargeOverrideExceptionCode Entity_Code) RESULT( Transaction_No Large_Entity_No, Docket_No INTEGER, Docket_No_Prefix CHAR(3), Docket_No_Suffix CHAR(3), Docket_No_Version INTEGER, Vehicle_No Entity_No, Vehicle_Code Entity_Code, Customer_No Entity_No, Job_No Entity_No, Carrier_No Entity_No, Gross Weight, Gross_Date Entity_Date, Gross_Time Entity_Time, Tare Weight, Tare_Date Entity_Date, Tare_Time Entity_Time, Net Weight, Memo Entity_Comment, Date_Complete Entity_Date, Time_Complete Entity_Time, Created_At Entity_No, Shift_No Entity_No, Created_By Operator_Code, Is_Fully_Paid Boolean, Amount_Paid NUMERIC(8,2), Is_Stored_Tare Boolean, DateTime_In Entity_DateTime, DateTime_Out Entity_DateTime, Custom_Data1 Entity_Code, Order_No VARCHAR(30), Reversal_Tran_No Large_Entity_No, Original_Tran_No Large_Entity_No, Transaction_Item_No Large_Entity_No, Product_No Entity_No, Area_No Entity_No, Waste_Stream_No Entity_No, Sub_Stream_1_No Entity_No, SS1_Name Entity_Name, LGA_Code Entity_Code, Rate_Range_No Entity_No, Cost_Method Prod_Cost_Method, Cost_Rule_No Entity_No, Item_Charge NUMERIC(8,2), Quantity NUMERIC(19,6), Item_Net Weight, Item_CM NUMERIC(19,6), Sub_Stream_2_No Entity_No, Sub_Stream_3_No Entity_No, Has_Override Boolean, Override_User Operator_Code, Override_Comment Entity_Comment, Charge_Rate NUMERIC(10,4), Original_Rate_Range_No Entity_No ) BEGIN SELECT H.Transaction_No, H.Docket_No, H.Docket_No_Prefix, H.Docket_No_Suffix, H.Docket_No_Version, H.Vehicle_No, H.Vehicle_Code, H.Customer_No, H.Job_No, H.Carrier_No, H.Gross, H.Gross_Date, H.Gross_Time, H.Tare, H.Tare_Date, H.Tare_Time, H.Net, H.Memo, H.Date_Complete, H.Time_Complete, H.Created_At, H.Shift_No, H.Created_By, H.Is_Fully_Paid, H.Amount_Paid, H.Is_Stored_Tare, H.DateTime_In, H.DateTime_Out, H.Custom_Data1, H.Order_No, H.Reversal_Tran_No, H.Original_Tran_No, I.Transaction_Item_No, I.Product_No, I.Area_No, I.Waste_Stream_No, I.Sub_Stream_1_No, E.Name AS SS1_Name, I.LGA_Code, I.Rate_Range_No, I.Cost_Method, I.Cost_Rule_No, I.Item_Charge, I.Quantity, I.Net AS Item_Net, I.Cubic_Metres AS Item_CM, I.Sub_Stream_2_No, I.Sub_Stream_3_No, (CASE A.Code WHEN @ChargeOverrideExceptionCode THEN 'Y' ELSE 'N' END) AS Has_Override, A.Operator_Code AS Override_User, A.Exception_Comment AS Override_Comment, B.Rate AS Charge_Rate, I.Original_Rate_Range_No FROM WM_Transaction_Header H INNER JOIN WM_Transaction_Item I ON (I.Transaction_No = H.Transaction_No) LEFT OUTER JOIN WM_Waste_Sub_Stream_1 E ON (E.Sub_Stream_1_No = I.Sub_Stream_1_No) LEFT OUTER JOIN ( SELECT TEx.Transaction_No, Ex.Code, TEx.Operator_Code, TEx.Exception_Comment FROM WM_Transaction_Exception TEx INNER JOIN WM_Exceptions Ex ON (Ex.Exception_No = TEx.Exception_No) ) A ON (A.Transaction_No = H.Transaction_No) AND (A.Code = @ChargeOverrideExceptionCode) LEFT OUTER JOIN ( SELECT Transaction_No, Transaction_Item_No, Rate, Rate_No FROM WM_Transaction_Item_Rate ) B ON (B.Transaction_No = I.Transaction_No) AND (B.Transaction_Item_No = I.Transaction_Item_No) AND (B.Rate_No = @ProductChargeRateNo) WHERE H.Transaction_No = @TranNo AND Is_Completed = 'Y'; END; PASSTHROUGH STOP; END IF; END IF; /********************************************************************/ /* 2/11/2006 - Add trigger to prevent One-Touch Product Rates from being */ /* changed to a weighed charge type that will not be exported */ /********************************************************************/ IF NOT EXISTS( SELECT 1 FROM systrigger WHERE trigger_name = 'au_WM_Product_Rate_2') THEN CREATE TRIGGER au_WM_Product_Rate_2 AFTER UPDATE ORDER 2 ON "DBA".WM_Product_Rate REFERENCING NEW AS inserted FOR EACH STATEMENT BEGIN --NOTE: This trigger assumes that Range_No will never be changed IF EXISTS( SELECT 1 FROM inserted ins INNER JOIN "DBA".WM_MD_OneTouch_Tran T ON (T.Rate_Range_No = ins.Range_No) WHERE ins.Charge_Type NOT IN ('N','C')) THEN RAISERROR 30000 'Cannot change Charge Type to a non-weighed type when Product Rate is being used in a One-Touch Transaction.'; END IF; END; IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; CREATE TRIGGER au_WM_Product_Rate_2 AFTER UPDATE ORDER 2 ON "DBA".WM_Product_Rate REFERENCING NEW AS inserted FOR EACH STATEMENT BEGIN --NOTE: This trigger assumes that Range_No will never be changed IF EXISTS( SELECT 1 FROM inserted ins INNER JOIN "DBA".WM_MD_OneTouch_Tran T ON (T.Rate_Range_No = ins.Range_No) WHERE ins.Charge_Type NOT IN ('N','C')) THEN RAISERROR 30000 'Cannot change Charge Type to a non-weighed type when Product Rate is being used in a One-Touch Transaction.'; END IF; END; PASSTHROUGH STOP; END IF; END IF; /********************************************************************/ /* 3/11/2006 - Passthrough Standard Modules change to remove WasteMan 2G-specific */ /* commands from trigger on SM_Action_Schedules. Add new WasteMan 2G-specific */ /* trigger to do the job instead */ /********************************************************************/ IF EXISTS( SELECT 1 FROM "DBA".SM_Version_Control WHERE Entity_Name = 'PT-bu_SM_Action_Schedules' AND Database_ID = (SELECT db_property('GlobalDbID')) AND Version_No < 2) THEN IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_Site_Data') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_Site_Data; ALTER TRIGGER bu_SM_Action_Schedules BEFORE UPDATE ON "DBA".SM_Action_Schedules REFERENCING NEW AS new_Row FOR EACH ROW BEGIN SET new_Row.DateTime_Modified = GetDate(); SET new_Row.User_Modified = (SELECT SM_UC_GetConnectionAppUserID()); END; --Update version control rec (remote) UPDATE "DBA".SM_Version_Control SET Version_No = 2 WHERE Entity_Name = 'PT-bu_SM_Action_Schedules' AND Database_ID = (SELECT db_property('GlobalDbID')); PASSTHROUGH STOP; END IF; --Update version control rec (consolidated) UPDATE "DBA".SM_Version_Control SET Version_No = 2 WHERE Entity_Name = 'PT-bu_SM_Action_Schedules' AND Database_ID = (SELECT db_property('GlobalDbID')); END IF; --Add second trigger to SM_Action_Schedules table to update the publication IF NOT EXISTS( SELECT 1 FROM systrigger WHERE trigger_name = 'bu_SM_Action_Schedules_2') THEN CREATE TRIGGER bu_SM_Action_Schedules_2 BEFORE UPDATE ORDER 2 ON "DBA".SM_Action_Schedules REFERENCING NEW AS new_Row FOR EACH ROW BEGIN IF new_Row.Replication_Site_No IS NULL THEN SET new_Row.Replication_Site_No = new_Row.Site_No; IF EXISTS(SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_Site_Data') THEN UPDATE SM_Action_Schedule_Params PUBLICATION WM_Pub_Site_Data SUBSCRIBE BY new_Row.Replication_Site_No WHERE Schedule_No = new_Row.Schedule_No; END IF; END IF; IF UPDATE(Replication_Site_No) AND EXISTS(SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_Site_Data') THEN UPDATE SM_Action_Schedule_Params PUBLICATION WM_Pub_Site_Data SUBSCRIBE BY new_Row.Replication_Site_No WHERE Schedule_No = new_Row.Schedule_No; END IF; END; IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_Site_Data') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_Site_Data; CREATE TRIGGER bu_SM_Action_Schedules_2 BEFORE UPDATE ORDER 2 ON "DBA".SM_Action_Schedules REFERENCING NEW AS new_Row FOR EACH ROW BEGIN IF new_Row.Replication_Site_No IS NULL THEN SET new_Row.Replication_Site_No = new_Row.Site_No; IF EXISTS(SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_Site_Data') THEN UPDATE SM_Action_Schedule_Params PUBLICATION WM_Pub_Site_Data SUBSCRIBE BY new_Row.Replication_Site_No WHERE Schedule_No = new_Row.Schedule_No; END IF; END IF; IF UPDATE(Replication_Site_No) AND EXISTS(SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_Site_Data') THEN UPDATE SM_Action_Schedule_Params PUBLICATION WM_Pub_Site_Data SUBSCRIBE BY new_Row.Replication_Site_No WHERE Schedule_No = new_Row.Schedule_No; END IF; END; PASSTHROUGH STOP; END IF; END IF; /********************************************************************/ /* 3/11/2006 - Passthrough Standard Modules change to add Replication_Site_Flag */ /* to SM_Enterprise_Config_Header */ /********************************************************************/ IF EXISTS( SELECT 1 FROM syscolumn JOIN systable WHERE table_name = 'SM_Enterprise_Config_Header' AND column_name = 'Replication_Site_Flag') AND EXISTS( SELECT 1 FROM "DBA".SM_Version_Control WHERE Entity_Name = 'Passthrough-SM_Enterprise_Config_Header' AND Database_ID = (SELECT db_property('GlobalDbID')) AND Version_No < 5) THEN IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_Site_Data') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_Site_Data; ALTER TABLE "DBA".SM_Enterprise_Config_Header ADD Replication_Site_Flag Rep_Site_No NULL; --Add index on field CREATE INDEX IDX_Replication ON "DBA".SM_Enterprise_Config_Header (Replication_Site_Flag ASC); --Update version control rec (remote) UPDATE "DBA".SM_Version_Control SET Version_No = 5 WHERE Entity_Name = 'Passthrough-SM_Enterprise_Config_Header' AND Database_ID = (SELECT db_property('GlobalDbID')); PASSTHROUGH STOP; END IF; --Update version control rec (consolidated) UPDATE "DBA".SM_Version_Control SET Version_No = 5 WHERE Entity_Name = 'Passthrough-SM_Enterprise_Config_Header' AND Database_ID = (SELECT db_property('GlobalDbID')); END IF; /********************************************************************/ /* 15/11/2006 - Passthrough Standard Modules change to add View_Version field to SM_List_Form_View */ /********************************************************************/ IF EXISTS( SELECT 1 FROM syscolumn JOIN systable WHERE table_name = 'SM_List_Form_View' AND column_name = 'View_Version') AND EXISTS( SELECT 1 FROM "DBA".SM_Version_Control WHERE Entity_Name = 'PT_SM_List_Form_View' AND Database_ID = (SELECT db_property('GlobalDbID')) AND Version_No < 4) THEN IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; ALTER TABLE "DBA".SM_List_Form_View ADD VIEW_VERSION UNSIGNED INT NULL DEFAULT 0; --Update version control rec (remote) UPDATE "DBA".SM_Version_Control SET Version_No = 4 WHERE Entity_Name = 'PT_SM_List_Form_View' AND Database_ID = (SELECT db_property('GlobalDbID')); PASSTHROUGH STOP; END IF; --Update version control rec (consolidated) UPDATE "DBA".SM_Version_Control SET Version_No = 4 WHERE Entity_Name = 'PT_SM_List_Form_View' AND Database_ID = (SELECT db_property('GlobalDbID')); END IF; /********************************************************************/ /* 16/11/2006 - Passthrough Standard Module changes to add start and end */ /* stored procedures for List View Test */ /********************************************************************/ IF EXISTS( SELECT 1 FROM sysprocedure WHERE proc_name = 'SM_ListViewTestStart') AND NOT EXISTS( SELECT 1 FROM "DBA".SM_Version_Control WHERE Entity_Name = 'PT-SM_ListViewTestStart' AND Database_ID = (SELECT db_property('GlobalDbID'))) THEN IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; CREATE PROCEDURE "DBA".SM_ListViewTestStart(IN @ViewID CHAR(4)) BEGIN --NOTE 1: Use value of '1' as it will (99% of cases) not cause any errors when used as either -- a string, integer or float --NOTE 2: A maximum of 20 parameters is supported CREATE VARIABLE @Param1 VARCHAR(50); SET @Param1 = '1'; CREATE VARIABLE @Param2 VARCHAR(50); SET @Param2 = '1'; CREATE VARIABLE @Param3 VARCHAR(50); SET @Param3 = '1'; CREATE VARIABLE @Param4 VARCHAR(50); SET @Param4 = '1'; CREATE VARIABLE @Param5 VARCHAR(50); SET @Param5 = '1'; CREATE VARIABLE @Param6 VARCHAR(50); SET @Param6 = '1'; CREATE VARIABLE @Param7 VARCHAR(50); SET @Param7 = '1'; CREATE VARIABLE @Param8 VARCHAR(50); SET @Param8 = '1'; CREATE VARIABLE @Param9 VARCHAR(50); SET @Param9 = '1'; CREATE VARIABLE @Param10 VARCHAR(50); SET @Param10 = '1'; CREATE VARIABLE @Param11 VARCHAR(50); SET @Param11 = '1'; CREATE VARIABLE @Param12 VARCHAR(50); SET @Param12 = '1'; CREATE VARIABLE @Param13 VARCHAR(50); SET @Param13 = '1'; CREATE VARIABLE @Param14 VARCHAR(50); SET @Param14 = '1'; CREATE VARIABLE @Param15 VARCHAR(50); SET @Param15 = '1'; CREATE VARIABLE @Param16 VARCHAR(50); SET @Param16 = '1'; CREATE VARIABLE @Param17 VARCHAR(50); SET @Param17 = '1'; CREATE VARIABLE @Param18 VARCHAR(50); SET @Param18 = '1'; CREATE VARIABLE @Param19 VARCHAR(50); SET @Param19 = '1'; CREATE VARIABLE @Param20 VARCHAR(50); SET @Param20 = '1'; END; --Insert version control (remote) INSERT "DBA".SM_Version_Control(Entity_Name, Database_ID, Version_No) VALUES('PT-SM_ListViewTestStart', (SELECT db_property( 'GlobalDBId' )), 1); PASSTHROUGH STOP; END IF; --Insert version control (consolidated) INSERT "DBA".SM_Version_Control(Entity_Name, Database_ID, Version_No) VALUES('PT-SM_ListViewTestStart', (SELECT db_property( 'GlobalDBId' )), 1); END IF; IF EXISTS( SELECT 1 FROM sysprocedure WHERE proc_name = 'SM_ListViewTestEnd') AND NOT EXISTS( SELECT 1 FROM "DBA".SM_Version_Control WHERE Entity_Name = 'PT-SM_ListViewTestEnd' AND Database_ID = (SELECT db_property('GlobalDbID'))) THEN IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; CREATE PROCEDURE "DBA".SM_ListViewTestEnd(IN @ViewID CHAR(4)) BEGIN DROP VARIABLE @Param1; DROP VARIABLE @Param2; DROP VARIABLE @Param3; DROP VARIABLE @Param4; DROP VARIABLE @Param5; DROP VARIABLE @Param6; DROP VARIABLE @Param7; DROP VARIABLE @Param8; DROP VARIABLE @Param9; DROP VARIABLE @Param10; DROP VARIABLE @Param11; DROP VARIABLE @Param12; DROP VARIABLE @Param13; DROP VARIABLE @Param14; DROP VARIABLE @Param15; DROP VARIABLE @Param16; DROP VARIABLE @Param17; DROP VARIABLE @Param18; DROP VARIABLE @Param19; DROP VARIABLE @Param20; END; --Insert version control (remote) INSERT "DBA".SM_Version_Control(Entity_Name, Database_ID, Version_No) VALUES('PT-SM_ListViewTestEnd', (SELECT db_property( 'GlobalDBId' )), 1); PASSTHROUGH STOP; END IF; --Insert version control (consolidated) INSERT "DBA".SM_Version_Control(Entity_Name, Database_ID, Version_No) VALUES('PT-SM_ListViewTestEnd', (SELECT db_property( 'GlobalDBId' )), 1); END IF; /********************************************************************/ /* 19/12/2006 - Passthrough standard module changes to add a Refresh_Login_Time */ /* column to SM_Operator */ /********************************************************************/ IF EXISTS( SELECT 1 FROM syscolumn JOIN systable WHERE table_name = 'SM_Operator' AND column_name = 'Refresh_Login_Time') AND EXISTS( SELECT 1 FROM "DBA".SM_Version_Control WHERE Entity_Name = 'Passthrough-SM_Operator' AND Database_ID = (SELECT db_property('GlobalDbID')) AND Version_No < 4) THEN IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; ALTER TABLE "DBA".SM_Operator ADD Refresh_Login_Time UNSIGNED SMALLINT NULL DEFAULT 0; --Update version control rec UPDATE "DBA".SM_Version_Control SET Version_No = 4 WHERE Entity_Name = 'Passthrough-SM_Operator' AND Database_ID = (SELECT db_property('GlobalDbID')); PASSTHROUGH STOP; END IF; --Update version control rec UPDATE "DBA".SM_Version_Control SET Version_No = 4 WHERE Entity_Name = 'Passthrough-SM_Operator' AND Database_ID = (SELECT db_property('GlobalDbID')); END IF; /********************************************************************/ /* 23/01/2007 - Passthrough Standard Modules change to add new fields */ /* to SM_Print_Template_Item */ /* NOTE: These are added as NULL-permitting to prevent DB errors */ /* when run on < 9.0.2 DB server version */ /********************************************************************/ IF EXISTS( SELECT 1 FROM syscolumn JOIN systable WHERE table_name = 'SM_Print_Template_Item' AND column_name = 'Expandable') AND EXISTS( SELECT 1 FROM syscolumn JOIN systable WHERE table_name = 'SM_Print_Template_Item' AND column_name = 'Max_Expand_Lines') AND NOT EXISTS( SELECT 1 FROM "DBA".SM_Version_Control WHERE Entity_Name = 'PT-SM_Print_Template_Item' AND Database_ID = (SELECT db_property('GlobalDbID'))) THEN IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; ALTER TABLE "DBA".SM_Print_Template_Item ADD Expandable Boolean NULL DEFAULT 'N'; ALTER TABLE "DBA".SM_Print_Template_Item ADD Max_Expand_Lines SMALLINT NULL DEFAULT 0; --Add version control rec (remote) INSERT "DBA".SM_Version_Control(Entity_Name, Database_ID, Version_No) VALUES('PT-SM_Print_Template_Item', (SELECT db_property( 'GlobalDBId' )), 1); PASSTHROUGH STOP; END IF; --Add version control rec (consolidated) INSERT "DBA".SM_Version_Control(Entity_Name, Database_ID, Version_No) VALUES('PT-SM_Print_Template_Item', (SELECT db_property( 'GlobalDBId' )), 1); END IF; /*********************************************************************/ /* 23/01/2007 - Passthrough standard module changes to update */ /* PrintService_TemplateDets to return new fields */ /*********************************************************************/ IF EXISTS( SELECT 1 FROM "DBA".SM_Version_Control WHERE Entity_Name = 'PrintService_TemplateDets' AND Database_ID = (SELECT db_property('GlobalDbID'))) AND NOT EXISTS( SELECT 1 FROM "DBA".SM_Version_Control WHERE Entity_Name = 'PT-PrintService_TemplateDets' AND Database_ID = (SELECT db_property('GlobalDbID'))) THEN IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; ALTER PROCEDURE "DBA".PrintService_TemplateDets(IN @TemplateNo Entity_No) RESULT( Template_Title VARCHAR(25), Template_Description Gen_Large_Description, Page_Width UNSIGNED INT, Page_Length UNSIGNED INT, Template_Section UNSIGNED INT, Visible Boolean, Height_In_Lines UNSIGNED INT, Template_Line_No Entity_No, Line_No UNSIGNED INT, Template_Item_No Entity_No, Width UNSIGNED INT, Column_No UNSIGNED INT, Print_Item_Type INTEGER, Print_Item_Value VARCHAR(80), Alignment UNSIGNED INT, Bold Boolean, Underline Boolean, Italic Boolean, Double_Width Boolean, Special_Format Boolean, Expandable Boolean, Max_Expand_Lines SMALLINT) BEGIN SELECT t.Template_Title, t.Template_Description, t.Page_Width, t.Page_Length, s.Template_Section, s.Visible, s.Height_In_Lines, l.Template_Line_No, l.Line_No, i.Template_Item_No, i.Width, i.Column_No, i.Print_Item_Type, i.Print_Item_Value, i.Alignment, i.Bold, i.Underline, i.Italic, i.Double_Width, i.Special_Format, i.Expandable, i.Max_Expand_Lines FROM SM_Print_Template t LEFT OUTER JOIN SM_Print_Template_Section s LEFT OUTER JOIN SM_Print_Template_Line l LEFT OUTER JOIN SM_Print_Template_Item i WHERE t.Template_No = @TemplateNo ORDER BY s.Template_Section, l.Line_No, i.Column_No; END; --Add version control rec (remote) INSERT "DBA".SM_Version_Control(Entity_Name, Database_ID, Version_No) VALUES('PT-PrintService_TemplateDets', (SELECT db_property( 'GlobalDBId' )), 1); PASSTHROUGH STOP; END IF; --Add version control rec (consolidated) INSERT "DBA".SM_Version_Control(Entity_Name, Database_ID, Version_No) VALUES('PT-PrintService_TemplateDets', (SELECT db_property( 'GlobalDBId' )), 1); END IF; /**************************************************************/ /* 31/01/2007 - Add period fields to select for procedure BaseFrame_PrintDockJob */ /*************************************************************/ IF NOT EXISTS( SELECT 1 FROM "DBA".SM_Version_Control WHERE Entity_Name = 'BaseFrame_PrintDockJob' AND Database_ID = (SELECT db_property('GlobalDbID'))) THEN ALTER PROCEDURE "DBA"."BaseFrame_PrintDockJob"(IN @JobNo Entity_No, @ProductNo Entity_No) RESULT( Job_No Entity_No, Product_No Entity_No, Job_Target_Type char(1), Job_Progress Numeric(8,2), Job_Target Numeric(8,2), Period_Target_Type char(1), Period_Progress Numeric(8,2), Period_Target Numeric(8,2), Period_Interval Numeric(8,2), Period_End Entity_Date) BEGIN SELECT Job_No, Product_No, Job_Target_Type, (if Job_Target_Type = 'M' then Job_Progress_Mass else Job_Progress_CM endif) as Job_Progress, (if Job_Target_Type = 'M' then Job_Target else 0 endif) as Job_Target, Period_Target_Type, (if Period_Target_Type = 'M' then Period_Progress_Mass else Period_Progress_CM endif) as Period_Progress, (if Period_Target_Type = 'M' then Period_Target else 0 endif) as Period_Target, Period_Interval, Period_End FROM "DBA".WM_Job_Auth_Product WHERE Job_No = @JobNo AND Product_No = @ProductNo; END; --Passthrough the update to the remote sites (if in a replicated environment) IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; /* NOTE "ONLY" keyword prevents the update from being applied at the consolidated node */ ALTER PROCEDURE "DBA"."BaseFrame_PrintDockJob"(IN @JobNo Entity_No, @ProductNo Entity_No) RESULT( Job_No Entity_No, Product_No Entity_No, Job_Target_Type char(1), Job_Progress Numeric(8,2), Job_Target Numeric(8,2), Period_Target_Type char(1), Period_Progress Numeric(8,2), Period_Target Numeric(8,2), Period_Interval Numeric(8,2), Period_End Entity_Date) BEGIN SELECT Job_No, Product_No, Job_Target_Type, (if Job_Target_Type = 'M' then Job_Progress_Mass else Job_Progress_CM endif) as Job_Progress, (if Job_Target_Type = 'M' then Job_Target else 0 endif) as Job_Target, Period_Target_Type, (if Period_Target_Type = 'M' then Period_Progress_Mass else Period_Progress_CM endif) as Period_Progress, (if Period_Target_Type = 'M' then Period_Target else 0 endif) as Period_Target, Period_Interval, Period_End FROM "DBA".WM_Job_Auth_Product WHERE Job_No = @JobNo AND Product_No = @ProductNo; END; --Update version control INSERT "DBA".SM_Version_Control(Entity_Name, Version_No, Database_ID) VALUES('BaseFrame_PrintDockJob', 1, (SELECT db_property('GlobalDBId'))); PASSTHROUGH STOP; END IF; --Update version control INSERT "DBA".SM_Version_Control(Entity_Name, Version_No, Database_ID) VALUES('BaseFrame_PrintDockJob', 1, (SELECT db_property('GlobalDBId'))); END IF; /*********************************************************************/ /* 19/07/2007 - Add Grouping_Code domain if it doesn't exist */ /* 16/08/2007 - Addition now added to Standard Modules, must be merely */ /* passed through here if the version control entry is flagged */ /*********************************************************************/ IF EXISTS( SELECT 1 FROM sysusertype WHERE type_name = 'Grouping_Code') AND EXISTS( SELECT 1 FROM "DBA".SM_Version_Control WHERE Entity_Name = 'Grouping_Code Add' AND Version_No = 1) THEN IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; CREATE DOMAIN Grouping_Code AS VARCHAR(100); PASSTHROUGH STOP; END IF; --Increment version number to prevent script from being re-run UPDATE "DBA".SM_Version_Control SET Version_No = 2 WHERE Entity_Name = 'Grouping_Code Add'; END IF; /*********************************************************************/ /* 19/02/2007 - Add WM_Temp_Account table. */ /* System extension DISABLE_TEMP_ACCOUNTS is installed as a global */ /* extension to suppress the Temp Account functionality (retaining */ /* Open Cheques) until explicitly enabled by removing the extension */ /*********************************************************************/ IF NOT EXISTS( SELECT 1 FROM systable WHERE table_name = 'WM_Temp_Account' AND table_type = 'BASE') THEN BEGIN DECLARE @LocalSiteNo Entity_No; CREATE TABLE "DBA".WM_Temp_Account ( Temp_Account_No Identity_No not null, Customer_No Entity_No not null, Vehicle_Code Entity_Code, Site_No Entity_No not null, Shift_No Entity_No not null, Operator_Reference varchar(40), Credit_Limit numeric(8,2) not null default 0, Expiry_Date Entity_Date, Memo Entity_Comment_Text, Customer_Name Entity_Name, Customer_ABN ABN, Customer_Address1 Entity_Address, Customer_Address2 Entity_Address, Customer_Address3 Entity_Address, Customer_State Australian_State constraint CKC_CUSTOMER_STATE_WM_TEMP_ check (Customer_State is null or ( Customer_State in ('NSW','VIC','SA','WA','TAS','QLD','NT','ACT') )), Customer_PostCode Postcode, Cheque_No Cheque_No, Cheque_BSB Cheque_BSB, Cheque_Account Cheque_Account, Cheque_Bank Cheque_Bank, Cheque_Drawer Cheque_Drawer, Cheque_Drawer_License Cheque_Drawer_License, Display_Priority smallint, Available Boolean not null constraint CKC_AVAILABLE_WM_TEMP_ check (Available in ('Y','N')), DateTime_Created Entity_DateTime_Def_Current not null, DateTime_Modified Entity_DateTime_Def_Current not null, User_Created varchar(50), User_Modified varchar(50), Group_G0 Grouping_Code, Group_G1 Grouping_Code, Group_G2 Grouping_Code, Group_G3 Grouping_Code, Group_G4 Grouping_Code ); alter table "DBA".WM_Temp_Account add constraint PK_WM_TEMP_ACCOUNT primary key (Temp_Account_No); alter table "DBA".WM_Temp_Account add constraint FK_WM_TEMPACC_REF_WM_CUSTOMER foreign key (Customer_No) references "DBA".WM_Customer (Customer_No) on update restrict on delete restrict; alter table "DBA".WM_Temp_Account add constraint FK_WM_TEMPACC_REF_WM_SHIFT foreign key (Shift_No) references "DBA".WM_Shift (Shift_No) on update restrict on delete restrict; alter table "DBA".WM_Temp_Account add constraint FK_WM_TEMPACC_REF_SM_SITE foreign key (Site_No) references "DBA".SM_Site (Site_No) on update restrict on delete restrict; --Add system extension SELECT @LocalSiteNo = SM_GetLocalSiteNo(); CALL SM_EntCfgSetItemValue(NULL, NULL, 'SRV2005:SystemExtensions:Global', 'DISABLE_TEMP_ACCOUNTS', 'Y', NULL, @LocalSiteNo); --Passthrough the update to the remote sites (if in a replicated environment) IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN --Add the table to the publication ALTER PUBLICATION "DBA".WM_Pub_All_Nodes ADD TABLE "DBA".WM_Temp_Account; PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; create table "DBA".WM_Temp_Account ( Temp_Account_No Identity_No not null, Customer_No Entity_No not null, Vehicle_Code Entity_Code, Site_No Entity_No not null, Shift_No Entity_No not null, Operator_Reference varchar(40), Credit_Limit numeric(8,2) not null default 0, Expiry_Date Entity_Date, Memo Entity_Comment_Text, Customer_Name Entity_Name, Customer_ABN ABN, Customer_Address1 Entity_Address, Customer_Address2 Entity_Address, Customer_Address3 Entity_Address, Customer_State Australian_State constraint CKC_CUSTOMER_STATE_WM_TEMP_ check (Customer_State is null or ( Customer_State in ('NSW','VIC','SA','WA','TAS','QLD','NT','ACT') )), Customer_PostCode Postcode, Cheque_No Cheque_No, Cheque_BSB Cheque_BSB, Cheque_Account Cheque_Account, Cheque_Bank Cheque_Bank, Cheque_Drawer Cheque_Drawer, Cheque_Drawer_License Cheque_Drawer_License, Display_Priority smallint, Available Boolean not null constraint CKC_AVAILABLE_WM_TEMP_ check (Available in ('Y','N')), DateTime_Created Entity_DateTime_Def_Current not null, DateTime_Modified Entity_DateTime_Def_Current not null, User_Created varchar(50), User_Modified varchar(50), Group_G0 Grouping_Code, Group_G1 Grouping_Code, Group_G2 Grouping_Code, Group_G3 Grouping_Code, Group_G4 Grouping_Code ); alter table "DBA".WM_Temp_Account add constraint PK_WM_TEMP_ACCOUNT primary key (Temp_Account_No); alter table "DBA".WM_Temp_Account add constraint FK_WM_TEMPACC_REF_WM_CUSTOMER foreign key (Customer_No) references "DBA".WM_Customer (Customer_No) on update restrict on delete restrict; alter table "DBA".WM_Temp_Account add constraint FK_WM_TEMPACC_REF_WM_SHIFT foreign key (Shift_No) references "DBA".WM_Shift (Shift_No) on update restrict on delete restrict; alter table "DBA".WM_Temp_Account add constraint FK_WM_TEMPACC_REF_SM_SITE foreign key (Site_No) references "DBA".SM_Site (Site_No) on update restrict on delete restrict; --Add the table to the publication ALTER PUBLICATION "DBA".WM_Pub_All_Nodes ADD TABLE "DBA".WM_Temp_Account; PASSTHROUGH STOP; END IF; END; END IF; IF NOT EXISTS( SELECT 1 FROM systrigger WHERE trigger_name = 'bi_WM_Temp_Account') THEN CREATE TRIGGER bi_WM_Temp_Account BEFORE INSERT ON "DBA".WM_Temp_Account REFERENCING NEW AS new_Row FOR EACH ROW BEGIN SET new_Row.User_Created = (SELECT SM_UC_GetConnectionAppUserID()); SET new_Row.User_Modified = (SELECT SM_UC_GetConnectionAppUserID()); END; --Passthrough the update to the remote sites (if in a replicated environment) IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; CREATE TRIGGER bi_WM_Temp_Account BEFORE INSERT ON "DBA".WM_Temp_Account REFERENCING NEW AS new_Row FOR EACH ROW BEGIN SET new_Row.User_Created = (SELECT SM_UC_GetConnectionAppUserID()); SET new_Row.User_Modified = (SELECT SM_UC_GetConnectionAppUserID()); END; PASSTHROUGH STOP; END IF; END IF; IF NOT EXISTS( SELECT 1 FROM systrigger WHERE trigger_name = 'bu_WM_Temp_Account') THEN CREATE TRIGGER bu_WM_Temp_Account BEFORE UPDATE ON "DBA".WM_Temp_Account REFERENCING NEW AS new_Row FOR EACH ROW BEGIN SET new_Row.DateTime_Modified = GetDate(); SET new_Row.User_Modified = (SELECT SM_UC_GetConnectionAppUserID()); END; --Passthrough the update to the remote sites (if in a replicated environment) IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; CREATE TRIGGER bu_WM_Temp_Account BEFORE UPDATE ON "DBA".WM_Temp_Account REFERENCING NEW AS new_Row FOR EACH ROW BEGIN SET new_Row.DateTime_Modified = GetDate(); SET new_Row.User_Modified = (SELECT SM_UC_GetConnectionAppUserID()); END; PASSTHROUGH STOP; END IF; END IF; /********************************************************************/ /* 05/02/2007 - Changes to WM_Payment required for integrated EFTPOS*/ /* and revised payment processing. */ /* System extension LEGACY_UNDERPAYMENT_FUNCTION is installed as a */ /* global extension to suppress the new underpayment functionality */ /* until explicitly enabled by removing the extension */ /********************************************************************/ --Add new fields IF NOT EXISTS( SELECT 1 FROM syscolumn JOIN systable WHERE table_name = 'WM_Payment' AND column_name = 'Refund_Payment_No') THEN BEGIN DECLARE @LocalSiteNo Entity_No; ALTER TABLE "DBA".WM_Payment ADD Refund_Payment_No Large_Entity_No NULL; --Add system extension SELECT @LocalSiteNo = SM_GetLocalSiteNo(); CALL SM_EntCfgSetItemValue(NULL, NULL, 'SRV2005:SystemExtensions:Global', 'LEGACY_UNDERPAYMENT_FUNCTION', 'Y', NULL, @LocalSiteNo); --Passthrough the update to the remote sites (if in a replicated environment) IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_Site_Data') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_Site_Data; ALTER TABLE "DBA".WM_Payment ADD Refund_Payment_No Large_Entity_No NULL; PASSTHROUGH STOP; END IF; END; END IF; IF NOT EXISTS( SELECT 1 FROM syscolumn JOIN systable WHERE table_name = 'WM_Payment' AND column_name = 'Tran_No_If_Deposit') THEN ALTER TABLE "DBA".WM_Payment ADD Tran_No_If_Deposit Large_Entity_No NULL; --Passthrough the update to the remote sites (if in a replicated environment) IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_Site_Data') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_Site_Data; ALTER TABLE "DBA".WM_Payment ADD Tran_No_If_Deposit Large_Entity_No NULL; PASSTHROUGH STOP; END IF; END IF; IF NOT EXISTS( SELECT 1 FROM syscolumn JOIN systable WHERE table_name = 'WM_Payment' AND column_name = 'Site_Workstation_No') THEN ALTER TABLE "DBA".WM_Payment ADD Site_Workstation_No Entity_No NULL; --Passthrough the update to the remote sites (if in a replicated environment) IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_Site_Data') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_Site_Data; ALTER TABLE "DBA".WM_Payment ADD Site_Workstation_No Entity_No NULL; PASSTHROUGH STOP; END IF; END IF; IF NOT EXISTS( SELECT 1 FROM syscolumn JOIN systable WHERE table_name = 'WM_Payment' AND column_name = 'Temp_Account_No') THEN ALTER TABLE "DBA".WM_Payment ADD Temp_Account_No Entity_No NULL; --Passthrough the update to the remote sites (if in a replicated environment) IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_Site_Data') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_Site_Data; ALTER TABLE "DBA".WM_Payment ADD Temp_Account_No Entity_No NULL; PASSTHROUGH STOP; END IF; END IF; IF NOT EXISTS( SELECT 1 FROM syscolumn JOIN systable WHERE table_name = 'WM_Payment' AND column_name = 'Eftpos_Account_Type') THEN ALTER TABLE "DBA".WM_Payment ADD Eftpos_Account_Type VARCHAR(15); --Passthrough the update to the remote sites (if in a replicated environment) IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_Site_Data') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_Site_Data; ALTER TABLE "DBA".WM_Payment ADD Eftpos_Account_Type VARCHAR(15); PASSTHROUGH STOP; END IF; END IF; IF NOT EXISTS( SELECT 1 FROM syscolumn JOIN systable WHERE table_name = 'WM_Payment' AND column_name = 'Eftpos_Transaction_Ref') THEN ALTER TABLE "DBA".WM_Payment ADD Eftpos_Transaction_Ref VARCHAR(16); --Passthrough the update to the remote sites (if in a replicated environment) IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_Site_Data') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_Site_Data; ALTER TABLE "DBA".WM_Payment ADD Eftpos_Transaction_Ref VARCHAR(16); PASSTHROUGH STOP; END IF; END IF; IF NOT EXISTS( SELECT 1 FROM syscolumn JOIN systable WHERE table_name = 'WM_Payment' AND column_name = 'Eftpos_Terminal_ID') THEN ALTER TABLE "DBA".WM_Payment ADD Eftpos_Terminal_ID VARCHAR(20); --Passthrough the update to the remote sites (if in a replicated environment) IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_Site_Data') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_Site_Data; ALTER TABLE "DBA".WM_Payment ADD Eftpos_Terminal_ID VARCHAR(20); PASSTHROUGH STOP; END IF; END IF; IF NOT EXISTS( SELECT 1 FROM syscolumn JOIN systable WHERE table_name = 'WM_Payment' AND column_name = 'Eftpos_Audit_No') THEN ALTER TABLE "DBA".WM_Payment ADD Eftpos_Audit_No VARCHAR(8); --Passthrough the update to the remote sites (if in a replicated environment) IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_Site_Data') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_Site_Data; ALTER TABLE "DBA".WM_Payment ADD Eftpos_Audit_No VARCHAR(8); PASSTHROUGH STOP; END IF; END IF; IF NOT EXISTS( SELECT 1 FROM syscolumn JOIN systable WHERE table_name = 'WM_Payment' AND column_name = 'Eftpos_Receipt_String') THEN ALTER TABLE "DBA".WM_Payment ADD Eftpos_Receipt_String VARCHAR(2048); --Passthrough the update to the remote sites (if in a replicated environment) IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_Site_Data') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_Site_Data; ALTER TABLE "DBA".WM_Payment ADD Eftpos_Receipt_String VARCHAR(2048); PASSTHROUGH STOP; END IF; END IF; IF NOT EXISTS( SELECT 1 FROM syscolumn JOIN systable WHERE table_name = 'WM_Payment' AND column_name = 'Eftpos_Cashout_Amount') THEN --NOTE: Field is added as NULL-permitting to prevent DB errors when run on < 9.0.2 DB server version ALTER TABLE "DBA".WM_Payment ADD Eftpos_Cashout_Amount NUMERIC(8,2) NULL DEFAULT 0; --Passthrough the update to the remote sites (if in a replicated environment) IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_Site_Data') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_Site_Data; ALTER TABLE "DBA".WM_Payment ADD Eftpos_Cashout_Amount NUMERIC(8,2) NULL DEFAULT 0; PASSTHROUGH STOP; END IF; END IF; IF NOT EXISTS( SELECT 1 FROM syscolumn JOIN systable WHERE table_name = 'WM_Payment' AND column_name = 'Workstation_Screen_Name') THEN ALTER TABLE "DBA".WM_Payment ADD Workstation_Screen_Name VARCHAR(50) NULL; --Passthrough the update to the remote sites (if in a replicated environment) IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_Site_Data') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_Site_Data; ALTER TABLE "DBA".WM_Payment ADD Workstation_Screen_Name VARCHAR(50) NULL; PASSTHROUGH STOP; END IF; END IF; --Add new index IF NOT EXISTS( SELECT 1 FROM sysindex i INNER JOIN systable t on (t.table_id = i.table_id) WHERE t.table_name = 'WM_Payment' AND i.index_name = 'IDX_Eftpos_Tran_Ref') THEN CREATE INDEX IDX_Eftpos_Tran_Ref ON "DBA".WM_Payment (Eftpos_Transaction_Ref ASC); --Passthrough the update to the remote sites (if in a replicated environment) IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_Site_Data') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_Site_Data; CREATE INDEX IDX_Eftpos_Tran_Ref ON "DBA".WM_Payment (Eftpos_Transaction_Ref ASC); PASSTHROUGH STOP; END IF; END IF; --Add foreign keys IF NOT EXISTS( SELECT 1 FROM sys.sysforeignkey WHERE role = 'FK_WM_PAYMENT_REF_WM_PAYMENT2') THEN ALTER TABLE "DBA".WM_Payment ADD CONSTRAINT FK_WM_PAYMENT_REF_WM_PAYMENT2 FOREIGN KEY (Refund_Payment_No) REFERENCES "DBA".WM_Payment (Payment_No) ON UPDATE RESTRICT ON DELETE RESTRICT; --Passthrough the update to the remote sites (if in a replicated environment) IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_Site_Data') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_Site_Data; ALTER TABLE "DBA".WM_Payment ADD CONSTRAINT FK_WM_PAYMENT_REF_WM_PAYMENT2 FOREIGN KEY (Refund_Payment_No) REFERENCES "DBA".WM_Payment (Payment_No) ON UPDATE RESTRICT ON DELETE RESTRICT; PASSTHROUGH STOP; END IF; END IF; IF NOT EXISTS( SELECT 1 FROM sys.sysforeignkey WHERE role = 'FK_WM_PAYMENT_REF_WM_TEMPACC') THEN alter table "DBA".WM_Payment add constraint FK_WM_PAYMENT_REF_WM_TEMPACC foreign key (Temp_Account_No) references "DBA".WM_Temp_Account (Temp_Account_No) on update restrict on delete restrict; --Passthrough the update to the remote sites (if in a replicated environment) IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_Site_Data') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_Site_Data; alter table "DBA".WM_Payment add constraint FK_WM_PAYMENT_REF_WM_TEMPACC foreign key (Temp_Account_No) references "DBA".WM_Temp_Account (Temp_Account_No) on update restrict on delete restrict; PASSTHROUGH STOP; END IF; END IF; IF NOT EXISTS( SELECT 1 FROM sys.sysforeignkey WHERE role = 'FK_WM_PAY_REF_DEPOSIT') THEN alter table "DBA".WM_Payment add constraint FK_WM_PAY_REF_DEPOSIT foreign key (Tran_No_If_Deposit) references "DBA".WM_Transaction_Header (Transaction_No) on update restrict on delete restrict; --Passthrough the update to the remote sites (if in a replicated environment) IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_Site_Data') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_Site_Data; alter table "DBA".WM_Payment add constraint FK_WM_PAY_REF_DEPOSIT foreign key (Tran_No_If_Deposit) references "DBA".WM_Transaction_Header (Transaction_No) on update restrict on delete restrict; PASSTHROUGH STOP; END IF; END IF; IF NOT EXISTS( SELECT 1 FROM sys.sysforeignkey WHERE role = 'FK_WM_PAYMENT_REF_SM_SITE_WKS') THEN alter table "DBA".WM_Payment add constraint FK_WM_PAYMENT_REF_SM_SITE_WKS foreign key (Site_Workstation_No) references "DBA".SM_Site_Workstation (Site_Workstation_No) on update restrict on delete restrict; --Passthrough the update to the remote sites (if in a replicated environment) IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_Site_Data') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_Site_Data; alter table "DBA".WM_Payment add constraint FK_WM_PAYMENT_REF_SM_SITE_WKS foreign key (Site_Workstation_No) references "DBA".SM_Site_Workstation (Site_Workstation_No) on update restrict on delete restrict; PASSTHROUGH STOP; END IF; END IF; --Update table constraint to validate changes to Refund_Payment_No and to allow 'D'eposit payment type IF NOT EXISTS( SELECT 1 FROM "DBA".SM_Version_Control WHERE Entity_Name = 'CKT_WM_PAYMENT' AND Database_ID = (SELECT db_property('GlobalDbID'))) THEN --Ensure constraint name is correct. Must use a stored procedure as compound statements are not replicated in PASSTHROUGH mode --Stored procedure must exist at consolidated for CALL to work correctly in PASSTHROUGH mode --NOTE: If Named Constraints are not supported in the database the ALTER TABLE statement will FAIL and the payment's table -- constraint will need to be updated manually at the consolidated and any remote nodes IF EXISTS( SELECT 1 FROM systable WHERE table_name = 'sysconstraint') THEN CREATE PROCEDURE "DBA".WM_tmp_Rename_Payment_Constraint() BEGIN DECLARE @ConstName VARCHAR(128); SELECT c.constraint_name INTO @ConstName FROM sysconstraint c INNER JOIN systable t on (t.table_id = c.table_id) WHERE t.table_name = 'WM_Payment' AND c.constraint_type = 'T'; EXECUTE IMMEDIATE 'ALTER TABLE "DBA".WM_Payment RENAME CONSTRAINT ' || @ConstName || ' TO CKT_WM_PAYMENT'; END; CALL "DBA".WM_tmp_Rename_Payment_Constraint; END IF; ALTER TABLE "DBA".WM_Payment ALTER CONSTRAINT CKT_WM_PAYMENT CHECK ( ( ((Amount_Paid >= 0) AND (Amount_Allocated >= 0)) OR ((Amount_Paid <= 0) AND (Amount_Allocated <= 0)) ) AND (Abs(Amount_Allocated) <= Abs(Amount_Paid)) AND ( (Locked_By IS NULL) OR ((Locked_By IS NOT NULL) AND (Lock_Expiry_Time IS NOT NULL)) ) AND ((Amount_Allocated = Amount_Paid) OR (Refund_Payment_No IS NULL)) AND ((Refund_Payment_No IS NULL) OR (Reference_Payment_No IS NULL)) ); --Passthrough the update to the remote sites (if in a replicated environment) IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_Site_Data') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_Site_Data; --Ensure constraint name is correct. Must use a stored procedure as compound statements are not replicated in PASSTHROUGH mode CREATE PROCEDURE "DBA".WM_tmp_Rename_Payment_Constraint() BEGIN DECLARE @ConstName VARCHAR(128); SELECT c.constraint_name INTO @ConstName FROM sysconstraint c INNER JOIN systable t on (t.table_id = c.table_id) WHERE t.table_name = 'WM_Payment' AND c.constraint_type = 'T'; EXECUTE IMMEDIATE 'ALTER TABLE "DBA".WM_Payment RENAME CONSTRAINT ' || @ConstName || ' TO CKT_WM_PAYMENT'; END; CALL "DBA".WM_tmp_Rename_Payment_Constraint; DROP PROCEDURE "DBA".WM_tmp_Rename_Payment_Constraint; ALTER TABLE "DBA".WM_Payment ALTER CONSTRAINT CKT_WM_PAYMENT CHECK ( ( ((Amount_Paid >= 0) AND (Amount_Allocated >= 0)) OR ((Amount_Paid <= 0) AND (Amount_Allocated <= 0)) ) AND (Abs(Amount_Allocated) <= Abs(Amount_Paid)) AND ( (Locked_By IS NULL) OR ((Locked_By IS NOT NULL) AND (Lock_Expiry_Time IS NOT NULL)) ) AND ((Amount_Allocated = Amount_Paid) OR (Refund_Payment_No IS NULL)) AND ((Refund_Payment_No IS NULL) OR (Reference_Payment_No IS NULL)) ); --Add version control rec INSERT "DBA".SM_Version_Control(Entity_Name, Database_ID, Version_No) VALUES('CKT_WM_PAYMENT', (SELECT db_property('GlobalDbID')), 1); PASSTHROUGH STOP; END IF; --Drop temporary stored procedure DROP PROCEDURE "DBA".WM_tmp_Rename_Payment_Constraint; --Add version control rec INSERT "DBA".SM_Version_Control(Entity_Name, Database_ID, Version_No) VALUES('CKT_WM_PAYMENT', (SELECT db_property('GlobalDbID')), 1); END IF; IF NOT EXISTS( SELECT 1 FROM "DBA".SM_Version_Control WHERE Entity_Name = 'CKC_PAYMENT_TYPE_WM_PAYME' AND Database_ID = (SELECT db_property('GlobalDbID'))) THEN IF EXISTS( SELECT 1 FROM systable WHERE table_name = 'sysconstraint') THEN --Named constraints supported, update by constraint name if exists otherwise update without constraint name IF EXISTS( SELECT 1 FROM sysconstraint WHERE constraint_name = 'CKC_PAYMENT_TYPE_WM_PAYME') THEN ALTER TABLE "DBA".WM_Payment MODIFY Payment_Type CONSTRAINT CKC_PAYMENT_TYPE_WM_PAYME CHECK (Payment_Type is null or ( Payment_Type in ('P','R','A','D') )); --Passthrough the update to the remote sites (if in a replicated environment) IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_Site_Data') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_Site_Data; ALTER TABLE "DBA".WM_Payment MODIFY Payment_Type CONSTRAINT CKC_PAYMENT_TYPE_WM_PAYME CHECK (Payment_Type is null or ( Payment_Type in ('P','R','A','D') )); --Add version control rec INSERT "DBA".SM_Version_Control(Entity_Name, Database_ID, Version_No) VALUES('CKC_PAYMENT_TYPE_WM_PAYME', (SELECT db_property('GlobalDbID')), 1); PASSTHROUGH STOP; END IF; --Add version control rec (consolidated) INSERT "DBA".SM_Version_Control(Entity_Name, Database_ID, Version_No) VALUES('CKC_PAYMENT_TYPE_WM_PAYME', (SELECT db_property('GlobalDbID')), 1); ELSE ALTER TABLE "DBA".WM_Payment MODIFY Payment_Type CHECK (Payment_Type is null or ( Payment_Type in ('P','R','A','D') )); --Passthrough the update to the remote sites (if in a replicated environment) IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_Site_Data') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_Site_Data; ALTER TABLE "DBA".WM_Payment MODIFY Payment_Type CHECK (Payment_Type is null or ( Payment_Type in ('P','R','A','D') )); --Add version control rec INSERT "DBA".SM_Version_Control(Entity_Name, Database_ID, Version_No) VALUES('CKC_PAYMENT_TYPE_WM_PAYME', (SELECT db_property('GlobalDbID')), 1); PASSTHROUGH STOP; END IF; --Add version control rec (consolidated) INSERT "DBA".SM_Version_Control(Entity_Name, Database_ID, Version_No) VALUES('CKC_PAYMENT_TYPE_WM_PAYME', (SELECT db_property('GlobalDbID')), 1); END IF; ELSE --Named constraints not supported, modify check constraint without name ALTER TABLE "DBA".WM_Payment MODIFY Payment_Type CHECK (Payment_Type is null or ( Payment_Type in ('P','R','A','D') )); --Passthrough the update to the remote sites (if in a replicated environment) IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_Site_Data') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_Site_Data; ALTER TABLE "DBA".WM_Payment MODIFY Payment_Type CHECK (Payment_Type is null or ( Payment_Type in ('P','R','A','D') )); --Add version control rec INSERT "DBA".SM_Version_Control(Entity_Name, Database_ID, Version_No) VALUES('CKC_PAYMENT_TYPE_WM_PAYME', (SELECT db_property('GlobalDbID')), 1); PASSTHROUGH STOP; END IF; --Add version control rec (consolidated) INSERT "DBA".SM_Version_Control(Entity_Name, Database_ID, Version_No) VALUES('CKC_PAYMENT_TYPE_WM_PAYME', (SELECT db_property('GlobalDbID')), 1); END IF; END IF; --Update triggers IF NOT EXISTS( SELECT 1 FROM systrigger WHERE trigger_name = 'bd_WM_Payment') THEN CREATE TRIGGER bd_WM_Payment BEFORE DELETE ON "DBA".WM_Payment REFERENCING OLD AS old_Rec FOR EACH ROW BEGIN --Don't permit integrated EFTPOS payments to be deleted as they have been processed at the bank --Allow this during replication so that transactions can be aged off IF (CURRENT REMOTE USER IS NULL) AND (old_Rec.Eftpos_Transaction_Ref IS NOT NULL) THEN RAISERROR 30000 'Cannot delete an EFTPOS payment after it has been processed by the bank'; END IF; END; --Passthrough the update to the remote sites (if in a replicated environment) IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_Site_Data') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_Site_Data; CREATE TRIGGER bd_WM_Payment BEFORE DELETE ON "DBA".WM_Payment REFERENCING OLD AS old_Rec FOR EACH ROW BEGIN --Don't permit integrated EFTPOS payments to be deleted as they have been processed at the bank --Allow this during replication so that transactions can be aged off IF (CURRENT REMOTE USER IS NULL) AND (old_Rec.Eftpos_Transaction_Ref IS NOT NULL) THEN RAISERROR 30000 'Cannot delete an EFTPOS payment after it has been processed by the bank'; END IF; END; PASSTHROUGH STOP; END IF; END IF; IF EXISTS( SELECT 1 FROM "DBA".SM_Version_Control WHERE Entity_Name = 'ai_WM_Payment' AND Database_ID = (SELECT db_property('GlobalDbID')) AND Version_No < 3) THEN ALTER TRIGGER ai_WM_Payment AFTER INSERT ON "DBA".WM_Payment REFERENCING NEW AS inserted FOR EACH STATEMENT BEGIN IF EXISTS( SELECT 1 FROM inserted P LEFT OUTER JOIN WM_Transaction_Payment TP ON (TP.Payment_No = P.Payment_No) WHERE P.Reference_Payment_No IS NULL AND (P.Amount_Allocated <> 0) AND (P.Amount_Allocated <> P.Amount_Paid) -- Allow insertion of payments without allocations if they are fully paid AND TP.Payment_No IS NULL) THEN RAISERROR 30000 'Total of Transaction Payment Allocations does not equal Payment Allocated Total'; END IF; --Refund_Payment_No must reference a payment of type 'R' (i.e. refund) IF EXISTS( SELECT 1 FROM inserted ins INNER JOIN WM_Payment P ON (P.Payment_No = ins.Refund_Payment_No) WHERE P.Payment_Type <> 'R') THEN RAISERROR 30000 'Refund_Payment_No must reference a Refund Payment.'; END IF; --Cannot insert a reversal of an integrated EFTPOS payment IF EXISTS( SELECT 1 FROM inserted ins INNER JOIN WM_Payment P ON (P.Payment_No = ins.Reference_Payment_No) WHERE P.Eftpos_Transaction_Ref IS NOT NULL) THEN RAISERROR 30000 'Cannot reverse an integrated EFTPOS payment. A refund must be issued.'; END IF; END; --Passthrough the update to the remote sites (if in a replicated environment) IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_Site_Data') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_Site_Data; ALTER TRIGGER ai_WM_Payment AFTER INSERT ON "DBA".WM_Payment REFERENCING NEW AS inserted FOR EACH STATEMENT BEGIN IF EXISTS( SELECT 1 FROM inserted P LEFT OUTER JOIN WM_Transaction_Payment TP ON (TP.Payment_No = P.Payment_No) WHERE P.Reference_Payment_No IS NULL AND (P.Amount_Allocated <> 0) AND (P.Amount_Allocated <> P.Amount_Paid) -- Allow insertion of payments without allocations if they are fully paid AND TP.Payment_No IS NULL) THEN RAISERROR 30000 'Total of Transaction Payment Allocations does not equal Payment Allocated Total'; END IF; --Refund_Payment_No must reference a payment of type 'R' (i.e. refund) IF EXISTS( SELECT 1 FROM inserted ins INNER JOIN WM_Payment P ON (P.Payment_No = ins.Refund_Payment_No) WHERE P.Payment_Type <> 'R') THEN RAISERROR 30000 'Refund_Payment_No must reference a Refund Payment.'; END IF; --Cannot insert a reversal of an integrated EFTPOS payment IF EXISTS( SELECT 1 FROM inserted ins INNER JOIN WM_Payment P ON (P.Payment_No = ins.Reference_Payment_No) WHERE P.Eftpos_Transaction_Ref IS NOT NULL) THEN RAISERROR 30000 'Cannot reverse an integrated EFTPOS payment. A refund must be issued.'; END IF; END; --Update remote version control UPDATE "DBA".SM_Version_Control SET Version_No = 3 WHERE Entity_Name = 'ai_WM_Payment' AND Database_ID = (SELECT db_property('GlobalDbID')); PASSTHROUGH STOP; END IF; --Update consolidated version control UPDATE "DBA".SM_Version_Control SET Version_No = 3 WHERE Entity_Name = 'ai_WM_Payment' AND Database_ID = (SELECT db_property('GlobalDbID')); END IF; IF EXISTS( SELECT 1 FROM "DBA".SM_Version_Control WHERE Entity_Name = 'au_WM_Payment' AND Database_ID = (SELECT db_property('GlobalDbID')) AND Version_No < 5) THEN ALTER TRIGGER au_WM_Payment AFTER UPDATE ON "DBA".WM_Payment REFERENCING NEW AS inserted OLD AS deleted FOR EACH STATEMENT BEGIN DECLARE @PaymentTotal NUMERIC(8,2); --Refund_Payment_No must reference a payment of type 'R' (i.e. refund) IF UPDATE(Refund_Payment_No) AND EXISTS( SELECT 1 FROM inserted ins INNER JOIN WM_Payment P ON (P.Payment_No = ins.Refund_Payment_No) WHERE P.Payment_Type <> 'R') THEN RAISERROR 30000 'Refund_Payment_No must reference a Refund Payment.'; END IF; --Cannot create a reversal of an integrated EFTPOS payment IF UPDATE(Reference_Payment_No) AND EXISTS( SELECT 1 FROM inserted ins INNER JOIN WM_Payment P ON (P.Payment_No = ins.Reference_Payment_No) WHERE P.Eftpos_Transaction_Ref IS NOT NULL) THEN RAISERROR 30000 'Cannot reverse an integrated EFTPOS payment. A refund must be issued.'; END IF; --Amount Allocated must equal the sum of the Transaction Payment allocations -- UNLESS a reversal of the payment exists OR the payment is a reversal itself -- OR the Amount Allocated is set equal to the Amount Paid and no allocations exist -- OR the payment is linked to a Refund via the Refund_Payment_No field -- OR the payment is a linked Refund --If payment is a linked Refund, the Amount Allocated must equal the sum of the Amount Paid -- on all referencing payments (this includes referencing Refunds), less the total of payment -- allocations connected with referencing payments (and Refunds) --NOTE: Allow the total of allocations to differ from the payment Amount_Allocated during replication. -- This is necessary to permit the reconciliation of update conflicts that occur when payments are -- allocated at multiple sites between replication operations. This is intended to be a temporary state only -- and should be resolved by the time replication is complete. The view Payment_Allocation_Errors is examined at -- the conclusion of the replication process and if any exist, an error is logged. IF (UPDATE(Amount_Allocated) OR UPDATE(Reference_Payment_No) OR UPDATE(Refund_Payment_No) OR UPDATE(Amount_Paid)) AND CURRENT REMOTE USER IS NULL THEN IF EXISTS( SELECT 1 FROM inserted ins LEFT OUTER JOIN WM_Transaction_Payment TP ON (TP.Payment_No = ins.Payment_No) WHERE (ins.Reference_Payment_No IS NULL) --not a reversal AND (NOT EXISTS(SELECT 1 FROM WM_Payment P1 WHERE P1.Reference_Payment_No = ins.Payment_No)) --not reversed AND (Refund_Payment_No IS NULL) --does not have a linked Refund AND (NOT EXISTS(SELECT 1 FROM WM_Payment P2 WHERE P2.Refund_Payment_No = ins.Payment_No)) --not a linked Refund GROUP BY ins.Payment_No, ins.Amount_Paid, ins.Amount_Allocated HAVING ((SUM(TP.Amount) IS NOT NULL) AND (SUM(TP.Amount) <> ins.Amount_Allocated)) OR ((SUM(TP.Amount) IS NULL) AND (ins.Amount_Allocated <> 0) AND (ins.Amount_Allocated <> ins.Amount_Paid))) THEN RAISERROR 30000 'Total of Transaction Payment Allocations does not equal Payment Allocated Total.'; END IF; --Validate linked Refund payments (if present) IF EXISTS( SELECT 1 FROM inserted ins INNER JOIN WM_Payment P ON (P.Refund_Payment_No = ins.Payment_No)) THEN SELECT ISNULL(SUM(P.Amount_Paid), 0) INTO @PaymentTotal FROM inserted ins INNER JOIN WM_Payment P ON (P.Refund_Payment_No = ins.Payment_No); IF EXISTS( SELECT 1 FROM inserted ins INNER JOIN WM_Payment P ON (P.Refund_Payment_No = ins.Payment_No) LEFT OUTER JOIN WM_Transaction_Payment TP ON (TP.Payment_No = P.Payment_No) GROUP BY ins.Payment_No, ins.Amount_Allocated HAVING (-1 * ins.Amount_Allocated) <> (@PaymentTotal - SUM(ISNULL(TP.Amount, 0)))) THEN RAISERROR 30000 'Amount Allocated on linked Refund must equal Payment total less Payment allocations.'; END IF; END IF; END IF; --Set all Transactions' Replication_Site_Flag's to ZERO if the payment's flag is ZERO --NOTE: This query assumes that Payment_No will never be modified IF UPDATE(Replication_Site_Flag) AND EXISTS( SELECT 1 FROM WM_Transaction_Payment TP INNER JOIN inserted ins ON (ins.Payment_No = TP.Payment_No) INNER JOIN deleted del ON (del.Payment_No = ins.Payment_No) WHERE (ins.Replication_Site_Flag = '0') AND (del.Replication_Site_Flag <> '0')) THEN UPDATE WM_Transaction_Header SET Replication_Site_Flag = '0' WHERE Transaction_No IN (SELECT TP.Transaction_No FROM WM_Transaction_Payment TP INNER JOIN inserted ins ON (ins.Payment_No = TP.Payment_No) INNER JOIN deleted del ON (del.Payment_No = ins.Payment_No) WHERE (ins.Replication_Site_Flag = '0') AND (del.Replication_Site_Flag <> '0')); END IF; --Set Replication_Site_Flag to zero on all if the flags differ between the payments and the refund payment --This is necessary to ensure the linked refund stays with its payments --Trigger on WM_Payment will set associated transaction header flags to zero IF UPDATE(Replication_Site_Flag) AND EXISTS( SELECT 1 FROM WM_Payment P WHERE ((P.Refund_Payment_No IS NOT NULL) OR EXISTS( SELECT 1 FROM WM_Payment RP WHERE RP.Refund_Payment_No = P.Payment_No)) AND ((P.Payment_No IN (SELECT Payment_No FROM inserted)) OR (P.Payment_No IN (SELECT Refund_Payment_No FROM inserted)) OR (P.Refund_Payment_No IN (SELECT Payment_No FROM inserted)) OR (P.Refund_Payment_No IN (SELECT Refund_Payment_No FROM inserted))) GROUP BY ISNULL(P.Refund_Payment_No, P.Payment_No) HAVING COUNT(DISTINCT P.Replication_Site_Flag) > 1) THEN UPDATE WM_Payment SET Replication_Site_Flag = '0' WHERE ((Payment_No IN ( SELECT ISNULL(P.Refund_Payment_No, P.Payment_No) AS Refund_Pay_No FROM WM_Payment P WHERE ((P.Refund_Payment_No IS NOT NULL) OR EXISTS( SELECT 1 FROM WM_Payment RP WHERE RP.Refund_Payment_No = P.Payment_No)) AND ((P.Payment_No IN (SELECT Payment_No FROM inserted)) OR (P.Payment_No IN (SELECT Refund_Payment_No FROM inserted)) OR (P.Refund_Payment_No IN (SELECT Payment_No FROM inserted)) OR (P.Refund_Payment_No IN (SELECT Refund_Payment_No FROM inserted))) GROUP BY ISNULL(P.Refund_Payment_No, P.Payment_No) HAVING COUNT(DISTINCT P.Replication_Site_Flag) > 1)) --Linked Refund OR (Refund_Payment_No IN ( SELECT ISNULL(P.Refund_Payment_No, P.Payment_No) AS Refund_Pay_No FROM WM_Payment P WHERE ((P.Refund_Payment_No IS NOT NULL) OR EXISTS( SELECT 1 FROM WM_Payment RP WHERE RP.Refund_Payment_No = P.Payment_No)) AND ((P.Payment_No IN (SELECT Payment_No FROM inserted)) OR (P.Payment_No IN (SELECT Refund_Payment_No FROM inserted)) OR (P.Refund_Payment_No IN (SELECT Payment_No FROM inserted)) OR (P.Refund_Payment_No IN (SELECT Refund_Payment_No FROM inserted))) GROUP BY ISNULL(P.Refund_Payment_No, P.Payment_No) HAVING COUNT(DISTINCT P.Replication_Site_Flag) > 1))) --Payments referencing the Linked Refund AND (Replication_Site_Flag <> '0'); END IF; END; --Passthrough the update to the remote sites (if in a replicated environment) IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_Site_Data') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_Site_Data; ALTER TRIGGER au_WM_Payment AFTER UPDATE ON "DBA".WM_Payment REFERENCING NEW AS inserted OLD AS deleted FOR EACH STATEMENT BEGIN DECLARE @PaymentTotal NUMERIC(8,2); --Refund_Payment_No must reference a payment of type 'R' (i.e. refund) IF UPDATE(Refund_Payment_No) AND EXISTS( SELECT 1 FROM inserted ins INNER JOIN WM_Payment P ON (P.Payment_No = ins.Refund_Payment_No) WHERE P.Payment_Type <> 'R') THEN RAISERROR 30000 'Refund_Payment_No must reference a Refund Payment.'; END IF; --Cannot create a reversal of an integrated EFTPOS payment IF UPDATE(Reference_Payment_No) AND EXISTS( SELECT 1 FROM inserted ins INNER JOIN WM_Payment P ON (P.Payment_No = ins.Reference_Payment_No) WHERE P.Eftpos_Transaction_Ref IS NOT NULL) THEN RAISERROR 30000 'Cannot reverse an integrated EFTPOS payment. A refund must be issued.'; END IF; --Amount Allocated must equal the sum of the Transaction Payment allocations -- UNLESS a reversal of the payment exists OR the payment is a reversal itself -- OR the Amount Allocated is set equal to the Amount Paid and no allocations exist -- OR the payment is linked to a Refund via the Refund_Payment_No field -- OR the payment is a linked Refund --If payment is a linked Refund, the Amount Allocated must equal the sum of the Amount Paid -- on all referencing payments (this includes referencing Refunds), less the total of payment -- allocations connected with referencing payments (and Refunds) --NOTE: Allow the total of allocations to differ from the payment Amount_Allocated during replication. -- This is necessary to permit the reconciliation of update conflicts that occur when payments are -- allocated at multiple sites between replication operations. This is intended to be a temporary state only -- and should be resolved by the time replication is complete. The view Payment_Allocation_Errors is examined at -- the conclusion of the replication process and if any exist, an error is logged. IF (UPDATE(Amount_Allocated) OR UPDATE(Reference_Payment_No) OR UPDATE(Refund_Payment_No) OR UPDATE(Amount_Paid)) AND CURRENT REMOTE USER IS NULL THEN IF EXISTS( SELECT 1 FROM inserted ins LEFT OUTER JOIN WM_Transaction_Payment TP ON (TP.Payment_No = ins.Payment_No) WHERE (ins.Reference_Payment_No IS NULL) --not a reversal AND (NOT EXISTS(SELECT 1 FROM WM_Payment P1 WHERE P1.Reference_Payment_No = ins.Payment_No)) --not reversed AND (Refund_Payment_No IS NULL) --does not have a linked Refund AND (NOT EXISTS(SELECT 1 FROM WM_Payment P2 WHERE P2.Refund_Payment_No = ins.Payment_No)) --not a linked Refund GROUP BY ins.Payment_No, ins.Amount_Paid, ins.Amount_Allocated HAVING ((SUM(TP.Amount) IS NOT NULL) AND (SUM(TP.Amount) <> ins.Amount_Allocated)) OR ((SUM(TP.Amount) IS NULL) AND (ins.Amount_Allocated <> 0) AND (ins.Amount_Allocated <> ins.Amount_Paid))) THEN RAISERROR 30000 'Total of Transaction Payment Allocations does not equal Payment Allocated Total.'; END IF; --Validate linked Refund payments (if present) IF EXISTS( SELECT 1 FROM inserted ins INNER JOIN WM_Payment P ON (P.Refund_Payment_No = ins.Payment_No)) THEN SELECT ISNULL(SUM(P.Amount_Paid), 0) INTO @PaymentTotal FROM inserted ins INNER JOIN WM_Payment P ON (P.Refund_Payment_No = ins.Payment_No); IF EXISTS( SELECT 1 FROM inserted ins INNER JOIN WM_Payment P ON (P.Refund_Payment_No = ins.Payment_No) LEFT OUTER JOIN WM_Transaction_Payment TP ON (TP.Payment_No = P.Payment_No) GROUP BY ins.Payment_No, ins.Amount_Allocated HAVING (-1 * ins.Amount_Allocated) <> (@PaymentTotal - SUM(ISNULL(TP.Amount, 0)))) THEN RAISERROR 30000 'Amount Allocated on linked Refund must equal Payment total less Payment allocations.'; END IF; END IF; END IF; --Set all Transactions' Replication_Site_Flag's to ZERO if the payment's flag is ZERO --NOTE: This query assumes that Payment_No will never be modified IF UPDATE(Replication_Site_Flag) AND EXISTS( SELECT 1 FROM WM_Transaction_Payment TP INNER JOIN inserted ins ON (ins.Payment_No = TP.Payment_No) INNER JOIN deleted del ON (del.Payment_No = ins.Payment_No) WHERE (ins.Replication_Site_Flag = '0') AND (del.Replication_Site_Flag <> '0')) THEN UPDATE WM_Transaction_Header SET Replication_Site_Flag = '0' WHERE Transaction_No IN (SELECT TP.Transaction_No FROM WM_Transaction_Payment TP INNER JOIN inserted ins ON (ins.Payment_No = TP.Payment_No) INNER JOIN deleted del ON (del.Payment_No = ins.Payment_No) WHERE (ins.Replication_Site_Flag = '0') AND (del.Replication_Site_Flag <> '0')); END IF; --Set Replication_Site_Flag to zero on all if the flags differ between the payments and the refund payment --This is necessary to ensure the linked refund stays with its payments --Trigger on WM_Payment will set associated transaction header flags to zero IF UPDATE(Replication_Site_Flag) AND EXISTS( SELECT 1 FROM WM_Payment P WHERE ((P.Refund_Payment_No IS NOT NULL) OR EXISTS( SELECT 1 FROM WM_Payment RP WHERE RP.Refund_Payment_No = P.Payment_No)) AND ((P.Payment_No IN (SELECT Payment_No FROM inserted)) OR (P.Payment_No IN (SELECT Refund_Payment_No FROM inserted)) OR (P.Refund_Payment_No IN (SELECT Payment_No FROM inserted)) OR (P.Refund_Payment_No IN (SELECT Refund_Payment_No FROM inserted))) GROUP BY ISNULL(P.Refund_Payment_No, P.Payment_No) HAVING COUNT(DISTINCT P.Replication_Site_Flag) > 1) THEN UPDATE WM_Payment SET Replication_Site_Flag = '0' WHERE ((Payment_No IN ( SELECT ISNULL(P.Refund_Payment_No, P.Payment_No) AS Refund_Pay_No FROM WM_Payment P WHERE ((P.Refund_Payment_No IS NOT NULL) OR EXISTS( SELECT 1 FROM WM_Payment RP WHERE RP.Refund_Payment_No = P.Payment_No)) AND ((P.Payment_No IN (SELECT Payment_No FROM inserted)) OR (P.Payment_No IN (SELECT Refund_Payment_No FROM inserted)) OR (P.Refund_Payment_No IN (SELECT Payment_No FROM inserted)) OR (P.Refund_Payment_No IN (SELECT Refund_Payment_No FROM inserted))) GROUP BY ISNULL(P.Refund_Payment_No, P.Payment_No) HAVING COUNT(DISTINCT P.Replication_Site_Flag) > 1)) --Linked Refund OR (Refund_Payment_No IN ( SELECT ISNULL(P.Refund_Payment_No, P.Payment_No) AS Refund_Pay_No FROM WM_Payment P WHERE ((P.Refund_Payment_No IS NOT NULL) OR EXISTS( SELECT 1 FROM WM_Payment RP WHERE RP.Refund_Payment_No = P.Payment_No)) AND ((P.Payment_No IN (SELECT Payment_No FROM inserted)) OR (P.Payment_No IN (SELECT Refund_Payment_No FROM inserted)) OR (P.Refund_Payment_No IN (SELECT Payment_No FROM inserted)) OR (P.Refund_Payment_No IN (SELECT Refund_Payment_No FROM inserted))) GROUP BY ISNULL(P.Refund_Payment_No, P.Payment_No) HAVING COUNT(DISTINCT P.Replication_Site_Flag) > 1))) --Payments referencing the Linked Refund AND (Replication_Site_Flag <> '0'); END IF; END; --Update remote version control UPDATE "DBA".SM_Version_Control SET Version_No = 5 WHERE Entity_Name = 'au_WM_Payment' AND Database_ID = (SELECT db_property('GlobalDbID')); PASSTHROUGH STOP; END IF; --Update consolidated version control UPDATE "DBA".SM_Version_Control SET Version_No = 5 WHERE Entity_Name = 'au_WM_Payment' AND Database_ID = (SELECT db_property('GlobalDbID')); END IF; /********************************************************************/ /* 19/02/2007 - Changes to WM_Cash_Drawer_Log required for integrated EFTPOS */ /********************************************************************/ --Add new fields IF NOT EXISTS( SELECT 1 FROM syscolumn JOIN systable WHERE table_name = 'WM_Cash_Drawer_Log' AND column_name = 'Eftpos_Card_No') THEN ALTER TABLE "DBA".WM_Cash_Drawer_Log ADD Eftpos_Card_No varchar(32); --Passthrough the update to the remote sites (if in a replicated environment) IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_Site_Data') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_Site_Data; ALTER TABLE "DBA".WM_Cash_Drawer_Log ADD Eftpos_Card_No varchar(32); PASSTHROUGH STOP; END IF; END IF; IF NOT EXISTS( SELECT 1 FROM syscolumn JOIN systable WHERE table_name = 'WM_Cash_Drawer_Log' AND column_name = 'Eftpos_Card_Type') THEN ALTER TABLE "DBA".WM_Cash_Drawer_Log ADD Eftpos_Card_Type varchar(32); --Passthrough the update to the remote sites (if in a replicated environment) IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_Site_Data') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_Site_Data; ALTER TABLE "DBA".WM_Cash_Drawer_Log ADD Eftpos_Card_Type varchar(32); PASSTHROUGH STOP; END IF; END IF; IF NOT EXISTS( SELECT 1 FROM syscolumn JOIN systable WHERE table_name = 'WM_Cash_Drawer_Log' AND column_name = 'Eftpos_Expiry_Date') THEN ALTER TABLE "DBA".WM_Cash_Drawer_Log ADD Eftpos_Expiry_Date Entity_Date NULL; --Passthrough the update to the remote sites (if in a replicated environment) IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_Site_Data') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_Site_Data; ALTER TABLE "DBA".WM_Cash_Drawer_Log ADD Eftpos_Expiry_Date Entity_Date NULL; PASSTHROUGH STOP; END IF; END IF; IF NOT EXISTS( SELECT 1 FROM syscolumn JOIN systable WHERE table_name = 'WM_Cash_Drawer_Log' AND column_name = 'Eftpos_Authorisation') THEN ALTER TABLE "DBA".WM_Cash_Drawer_Log ADD Eftpos_Authorisation varchar(32); --Passthrough the update to the remote sites (if in a replicated environment) IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_Site_Data') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_Site_Data; ALTER TABLE "DBA".WM_Cash_Drawer_Log ADD Eftpos_Authorisation varchar(32); PASSTHROUGH STOP; END IF; END IF; IF NOT EXISTS( SELECT 1 FROM syscolumn JOIN systable WHERE table_name = 'WM_Cash_Drawer_Log' AND column_name = 'Eftpos_Account_Type') THEN ALTER TABLE "DBA".WM_Cash_Drawer_Log ADD Eftpos_Account_Type varchar(15); --Passthrough the update to the remote sites (if in a replicated environment) IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_Site_Data') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_Site_Data; ALTER TABLE "DBA".WM_Cash_Drawer_Log ADD Eftpos_Account_Type varchar(15); PASSTHROUGH STOP; END IF; END IF; IF NOT EXISTS( SELECT 1 FROM syscolumn JOIN systable WHERE table_name = 'WM_Cash_Drawer_Log' AND column_name = 'Eftpos_Transaction_Ref') THEN ALTER TABLE "DBA".WM_Cash_Drawer_Log ADD Eftpos_Transaction_Ref varchar(16); --Passthrough the update to the remote sites (if in a replicated environment) IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_Site_Data') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_Site_Data; ALTER TABLE "DBA".WM_Cash_Drawer_Log ADD Eftpos_Transaction_Ref varchar(16); PASSTHROUGH STOP; END IF; END IF; IF NOT EXISTS( SELECT 1 FROM syscolumn JOIN systable WHERE table_name = 'WM_Cash_Drawer_Log' AND column_name = 'Eftpos_Terminal_ID') THEN ALTER TABLE "DBA".WM_Cash_Drawer_Log ADD Eftpos_Terminal_ID varchar(20); --Passthrough the update to the remote sites (if in a replicated environment) IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_Site_Data') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_Site_Data; ALTER TABLE "DBA".WM_Cash_Drawer_Log ADD Eftpos_Terminal_ID varchar(20); PASSTHROUGH STOP; END IF; END IF; IF NOT EXISTS( SELECT 1 FROM syscolumn JOIN systable WHERE table_name = 'WM_Cash_Drawer_Log' AND column_name = 'Eftpos_Audit_No') THEN ALTER TABLE "DBA".WM_Cash_Drawer_Log ADD Eftpos_Audit_No varchar(8); --Passthrough the update to the remote sites (if in a replicated environment) IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_Site_Data') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_Site_Data; ALTER TABLE "DBA".WM_Cash_Drawer_Log ADD Eftpos_Audit_No varchar(8); PASSTHROUGH STOP; END IF; END IF; IF NOT EXISTS( SELECT 1 FROM syscolumn JOIN systable WHERE table_name = 'WM_Cash_Drawer_Log' AND column_name = 'Eftpos_Receipt_String') THEN ALTER TABLE "DBA".WM_Cash_Drawer_Log ADD Eftpos_Receipt_String varchar(2048); --Passthrough the update to the remote sites (if in a replicated environment) IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_Site_Data') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_Site_Data; ALTER TABLE "DBA".WM_Cash_Drawer_Log ADD Eftpos_Receipt_String varchar(2048); PASSTHROUGH STOP; END IF; END IF; --Add new index IF NOT EXISTS( SELECT 1 FROM sysindex i INNER JOIN systable t on (t.table_id = i.table_id) WHERE t.table_name = 'WM_Cash_Drawer_Log' AND i.index_name = 'IDX_Eftpos_Tran_Ref') THEN CREATE INDEX IDX_Eftpos_Tran_Ref ON "DBA".WM_Cash_Drawer_Log (Eftpos_Transaction_Ref ASC); --Passthrough the update to the remote sites (if in a replicated environment) IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_Site_Data') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_Site_Data; CREATE INDEX IDX_Eftpos_Tran_Ref ON "DBA".WM_Cash_Drawer_Log (Eftpos_Transaction_Ref ASC); PASSTHROUGH STOP; END IF; END IF; --Add "Receipt" type to Cash_Drawer_Log_Type field IF NOT EXISTS( SELECT 1 FROM "DBA".SM_Version_Control WHERE Entity_Name = 'CKC_CASH_DRAWER_LOG_T_WM_CASH_' AND Database_ID = (SELECT db_property('GlobalDbID'))) THEN IF EXISTS( SELECT 1 FROM systable WHERE table_name = 'sysconstraint') THEN --Named constraints supported, update by constraint name if exists otherwise update without constraint name IF EXISTS( SELECT 1 FROM sysconstraint WHERE constraint_name = 'CKC_CASH_DRAWER_LOG_T_WM_CASH_') THEN ALTER TABLE "DBA".WM_Cash_Drawer_Log MODIFY Cash_Drawer_Log_Type CONSTRAINT CKC_CASH_DRAWER_LOG_T_WM_CASH_ CHECK (Cash_Drawer_Log_Type in ('O','P','R')); --Passthrough the update to the remote sites (if in a replicated environment) IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_Site_Data') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_Site_Data; ALTER TABLE "DBA".WM_Cash_Drawer_Log MODIFY Cash_Drawer_Log_Type CONSTRAINT CKC_CASH_DRAWER_LOG_T_WM_CASH_ CHECK (Cash_Drawer_Log_Type in ('O','P','R')); --Add version control rec INSERT "DBA".SM_Version_Control(Entity_Name, Database_ID, Version_No) VALUES('CKC_CASH_DRAWER_LOG_T_WM_CASH_', (SELECT db_property('GlobalDbID')), 1); PASSTHROUGH STOP; END IF; --Add version control rec (consolidated) INSERT "DBA".SM_Version_Control(Entity_Name, Database_ID, Version_No) VALUES('CKC_CASH_DRAWER_LOG_T_WM_CASH_', (SELECT db_property('GlobalDbID')), 1); ELSE ALTER TABLE "DBA".WM_Cash_Drawer_Log MODIFY Cash_Drawer_Log_Type CHECK (Cash_Drawer_Log_Type in ('O','P','R')); --Passthrough the update to the remote sites (if in a replicated environment) IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_Site_Data') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_Site_Data; ALTER TABLE "DBA".WM_Cash_Drawer_Log MODIFY Cash_Drawer_Log_Type CHECK (Cash_Drawer_Log_Type in ('O','P','R')); --Add version control rec INSERT "DBA".SM_Version_Control(Entity_Name, Database_ID, Version_No) VALUES('CKC_CASH_DRAWER_LOG_T_WM_CASH_', (SELECT db_property('GlobalDbID')), 1); PASSTHROUGH STOP; END IF; --Add version control rec (consolidated) INSERT "DBA".SM_Version_Control(Entity_Name, Database_ID, Version_No) VALUES('CKC_CASH_DRAWER_LOG_T_WM_CASH_', (SELECT db_property('GlobalDbID')), 1); END IF; ELSE --Named constraints not supported, modify check constraint without name ALTER TABLE "DBA".WM_Cash_Drawer_Log MODIFY Cash_Drawer_Log_Type CHECK (Cash_Drawer_Log_Type in ('O','P','R')); --Passthrough the update to the remote sites (if in a replicated environment) IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_Site_Data') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_Site_Data; ALTER TABLE "DBA".WM_Cash_Drawer_Log MODIFY Cash_Drawer_Log_Type CHECK (Cash_Drawer_Log_Type in ('O','P','R')); --Add version control rec INSERT "DBA".SM_Version_Control(Entity_Name, Database_ID, Version_No) VALUES('CKC_CASH_DRAWER_LOG_T_WM_CASH_', (SELECT db_property('GlobalDbID')), 1); PASSTHROUGH STOP; END IF; --Add version control rec (consolidated) INSERT "DBA".SM_Version_Control(Entity_Name, Database_ID, Version_No) VALUES('CKC_CASH_DRAWER_LOG_T_WM_CASH_', (SELECT db_property('GlobalDbID')), 1); END IF; END IF; --Add table constraint to validate changes to Cash_Drawer_Log_Type --Add version control if constraint already exists IF EXISTS( SELECT 1 FROM systable WHERE table_name = 'sysconstraint') THEN IF EXISTS( SELECT 1 FROM sysconstraint WHERE constraint_name = 'CKT_WM_CASH_DRAWER_LOG') AND NOT EXISTS( SELECT 1 FROM "DBA".SM_Version_Control WHERE Entity_Name = 'CKT_WM_CASH_DRAWER_LOG' AND Database_ID = (SELECT db_property('GlobalDbID'))) THEN IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_Site_Data') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_Site_Data; INSERT "DBA".SM_Version_Control(Entity_Name, Database_ID, Version_No) VALUES('CKT_WM_CASH_DRAWER_LOG', (SELECT db_property('GlobalDBId')), 1); PASSTHROUGH STOP; END IF; INSERT "DBA".SM_Version_Control(Entity_Name, Database_ID, Version_No) VALUES('CKT_WM_CASH_DRAWER_LOG', (SELECT db_property('GlobalDBId')), 1); END IF; END IF; IF NOT EXISTS( SELECT 1 FROM "DBA".SM_Version_Control WHERE Entity_Name = 'CKT_WM_CASH_DRAWER_LOG' AND Database_ID = (SELECT db_property('GlobalDbID'))) THEN ALTER TABLE "DBA".WM_Cash_Drawer_Log ADD CONSTRAINT CKT_WM_CASH_DRAWER_LOG CHECK ( ((Cash_Drawer_Log_Type = 'O') AND (Amount >= 0)) OR ((Cash_Drawer_Log_Type = 'P') AND (Amount < 0)) OR ((Cash_Drawer_Log_Type = 'R') AND (Amount > 0)) ); --Passthrough the update to the remote sites (if in a replicated environment) IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_Site_Data') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_Site_Data; ALTER TABLE "DBA".WM_Cash_Drawer_Log ADD CONSTRAINT CKT_WM_CASH_DRAWER_LOG CHECK ( ((Cash_Drawer_Log_Type = 'O') AND (Amount >= 0)) OR ((Cash_Drawer_Log_Type = 'P') AND (Amount < 0)) OR ((Cash_Drawer_Log_Type = 'R') AND (Amount > 0)) ); --Add version control INSERT "DBA".SM_Version_Control(Entity_Name, Database_ID, Version_No) VALUES('CKT_WM_CASH_DRAWER_LOG', (SELECT db_property('GlobalDBId')), 1); PASSTHROUGH STOP; END IF; --Add version control INSERT "DBA".SM_Version_Control(Entity_Name, Database_ID, Version_No) VALUES('CKT_WM_CASH_DRAWER_LOG', (SELECT db_property('GlobalDBId')), 1); END IF; --Add new trigger IF NOT EXISTS( SELECT 1 FROM systrigger WHERE trigger_name = 'ad_WM_Cash_Drawer_Log') THEN CREATE TRIGGER ad_WM_Cash_Drawer_Log AFTER DELETE ON "DBA".WM_Cash_Drawer_Log REFERENCING OLD AS deleted FOR EACH STATEMENT BEGIN --Don't permit integrated EFTPOS payments to be deleted as they have been -- processed at the bank --Allow during replication to allow records to be aged off IF (CURRENT REMOTE USER IS NULL) AND EXISTS( SELECT 1 FROM deleted del INNER JOIN WM_Payment_Method M ON (M.Pay_Method_No = del.Pay_Method_No) WHERE (M.Prompt = 'E') --Eftpos payout AND (del.Eftpos_Transaction_Ref IS NOT NULL)) THEN RAISERROR 30000 'Cannot delete an EFTPOS payout after it has been processed by the bank.'; END IF; END; --Passthrough the update to the remote sites (if in a replicated environment) IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_Site_Data') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_Site_Data; CREATE TRIGGER ad_WM_Cash_Drawer_Log AFTER DELETE ON "DBA".WM_Cash_Drawer_Log REFERENCING OLD AS deleted FOR EACH STATEMENT BEGIN --Don't permit integrated EFTPOS payments to be deleted as they have been -- processed at the bank --Allow during replication to allow records to be aged off IF (CURRENT REMOTE USER IS NULL) AND EXISTS( SELECT 1 FROM deleted del INNER JOIN WM_Payment_Method M ON (M.Pay_Method_No = del.Pay_Method_No) WHERE (M.Prompt = 'E') --Eftpos payout AND (del.Eftpos_Transaction_Ref IS NOT NULL)) THEN RAISERROR 30000 'Cannot delete an EFTPOS payout after it has been processed by the bank.'; END IF; END; PASSTHROUGH STOP; END IF; END IF; /********************************************************************/ /* 19/02/2007 - Changes to WM_Payment_Method required for integrated EFTPOS */ /********************************************************************/ --Add new fields - these allow NULLs here to allow the script to run on < 9.0.2 version database IF NOT EXISTS( SELECT 1 FROM syscolumn JOIN systable WHERE table_name = 'WM_Payment_Method' AND column_name = 'Allow_Deposit') THEN ALTER TABLE "DBA".WM_Payment_Method ADD Allow_Deposit Boolean NULL DEFAULT 'Y'; --Passthrough the update to the remote sites (if in a replicated environment) IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; ALTER TABLE "DBA".WM_Payment_Method ADD Allow_Deposit Boolean NULL DEFAULT 'Y'; PASSTHROUGH STOP; END IF; END IF; IF NOT EXISTS( SELECT 1 FROM syscolumn JOIN systable WHERE table_name = 'WM_Payment_Method' AND column_name = 'Allow_Refund') THEN ALTER TABLE "DBA".WM_Payment_Method ADD Allow_Refund Boolean NULL DEFAULT 'Y'; --Passthrough the update to the remote sites (if in a replicated environment) IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; ALTER TABLE "DBA".WM_Payment_Method ADD Allow_Refund Boolean NULL DEFAULT 'Y'; PASSTHROUGH STOP; END IF; END IF; IF NOT EXISTS( SELECT 1 FROM syscolumn JOIN systable WHERE table_name = 'WM_Payment_Method' AND column_name = 'Eftpos_Surcharge_Percent') THEN ALTER TABLE "DBA".WM_Payment_Method ADD Eftpos_Surcharge_Percent NUMERIC(6,2) NULL DEFAULT 0; --Passthrough the update to the remote sites (if in a replicated environment) IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; ALTER TABLE "DBA".WM_Payment_Method ADD Eftpos_Surcharge_Percent NUMERIC(6,2) NULL DEFAULT 0; PASSTHROUGH STOP; END IF; END IF; /********************************************************************/ /* 07/03/2007 - Add stored proc to reverse a payment */ /********************************************************************/ IF NOT EXISTS( SELECT 1 FROM sysprocedure WHERE proc_name = 'WM_CreatePaymentReversal') THEN CREATE PROCEDURE "DBA".WM_CreatePaymentReversal(IN @PaymentNo Large_Entity_No, IN @CreatedWorkstation Entity_No, IN @CreatedUser Operator_Code, IN @CreatedShift Entity_No, IN @CreatedDateTime TIMESTAMP, OUT @NewPaymentNo Large_Entity_No) BEGIN ATOMIC --Check that the payment exists IF NOT EXISTS( SELECT 1 FROM WM_Payment WHERE Payment_No = @PaymentNo) THEN RAISERROR 50001 'Payment No does not exist'; RETURN; END IF; --Check that the payment has not already been reversed, and is not a reversal itself IF EXISTS( SELECT 1 FROM WM_Payment WHERE ((Payment_No = @PaymentNo) AND (Reference_Payment_No IS NOT NULL)) --Specified payment is a reversal OR (Reference_Payment_No = @PaymentNo)) THEN --Specified payment has already been reversed RAISERROR 50001 'Cannot reverse a Payment that has already been reversed or is a reversal itself'; RETURN; END IF; --Can't reverse a payment that has been refunded IF EXISTS( SELECT 1 FROM WM_Payment WHERE (Payment_No = @PaymentNo) AND (Refund_Payment_No IS NOT NULL)) THEN RAISERROR 50001 'Cannot reverse a Payment that has been Refunded.'; RETURN; END IF; --Can't reverse a payment that is a linked Refund IF EXISTS( SELECT 1 FROM WM_Payment WHERE Refund_Payment_No = @PaymentNo) THEN RAISERROR 50001 'Cannot reverse a Payment that is a linked Refund.'; RETURN; END IF; --Cannot reverse payment when payment allocation records exist, unless the amount allocated = 0 --Note that a payment may exist, fully allocated, without any allocation records --Also a payment linked to a reversed transaction will also have links to the transaction's reversal, -- but the net Amount Allocated will be = 0 IF EXISTS( SELECT 1 FROM WM_Transaction_Payment WHERE Payment_No = @PaymentNo) AND NOT EXISTS( SELECT 1 FROM WM_Payment WHERE Payment_No = @PaymentNo AND Amount_Allocated = 0) THEN RAISERROR 50001 'Cannot reverse a Payment that has been allocated to one or more Transactions'; RETURN; END IF; --Add the new record INSERT WM_Payment( Pay_Method_No, Shift_No, Customer_No, Payment_Type, Vehicle_Code, Reference_Payment_No, Refund_Payment_No, Tran_No_If_Deposit, Payment_Date, Payment_Time, Operator, Site_No, Site_Workstation_No, Temp_Account_No, Workstation_Screen_Name, Amount_Paid, Amount_Allocated, Is_Fully_Allocated, Debit_Account, Round_Account, EPA_Account, GL_Account, Memo, Customer_Name, Customer_ABN, Customer_Address1, Customer_Address2, Customer_Address3, Customer_State, Customer_PostCode, Cheque_No, Cheque_BSB, Cheque_Account, Cheque_Bank, Cheque_Drawer, Cheque_Drawer_License, Eftpos_Card_No, Eftpos_Card_Type, Eftpos_Expiry_Date, Eftpos_Authorisation, Eftpos_Transaction_Ref, Eftpos_Account_Type, Eftpos_Terminal_ID, Eftpos_Audit_No, Eftpos_Receipt_String, Eftpos_Cashout_Amount, Generic_Field1, Generic_Field2, Generic_Field3, Replication_Site_Flag) SELECT Pay_Method_No, @CreatedShift, Customer_No, Payment_Type, Vehicle_Code, Payment_No, --Store reference to the original payment in the reversal NULL, --Refund_Payment_No not relevant Tran_No_If_Deposit, DATEFORMAT(@CreatedDateTime, 'yyyy-mm-dd'), DATEFORMAT(@CreatedDateTime, 'hh:mm:ss'), @CreatedUser, Site_No, --NOTE: Retain site number so payments stay together during replication @CreatedWorkstation, Temp_Account_No, '', --Workstation screen name not relevant for reversals -Amount_Paid, -Amount_Allocated, Is_Fully_Allocated, Debit_Account, Round_Account, EPA_Account, GL_Account, Memo, Customer_Name, Customer_ABN, Customer_Address1, Customer_Address2, Customer_Address3, Customer_State, Customer_PostCode, Cheque_No, Cheque_BSB, Cheque_Account, Cheque_Bank, Cheque_Drawer, Cheque_Drawer_License, Eftpos_Card_No, Eftpos_Card_Type, Eftpos_Expiry_Date, Eftpos_Authorisation, Eftpos_Transaction_Ref, Eftpos_Account_Type, Eftpos_Terminal_ID, Eftpos_Audit_No, Eftpos_Receipt_String, Eftpos_Cashout_Amount, Generic_Field1, Generic_Field2, Generic_Field3, Replication_Site_Flag --NOTE: Keep the original payment and its reversal together when replicating FROM WM_Payment WHERE Payment_No = @PaymentNo; --Get the new payment number SET @NewPaymentNo = @@identity; --Set the Amount Allocated field to the Amount Paid on both the Original Payment and the Reversal UPDATE WM_Payment SET Amount_Allocated = Amount_Paid, Is_Fully_Allocated = 'Y' WHERE Payment_No = @PaymentNo; UPDATE WM_Payment SET Amount_Allocated = Amount_Paid, Is_Fully_Allocated = 'Y' WHERE Payment_No = @NewPaymentNo; END; --Passthrough the update to the remote sites (if in a replicated environment) IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; CREATE PROCEDURE "DBA".WM_CreatePaymentReversal(IN @PaymentNo Large_Entity_No, IN @CreatedWorkstation Entity_No, IN @CreatedUser Operator_Code, IN @CreatedShift Entity_No, IN @CreatedDateTime TIMESTAMP, OUT @NewPaymentNo Large_Entity_No) BEGIN ATOMIC --Check that the payment exists IF NOT EXISTS( SELECT 1 FROM WM_Payment WHERE Payment_No = @PaymentNo) THEN RAISERROR 50001 'Payment No does not exist'; RETURN; END IF; --Check that the payment has not already been reversed, and is not a reversal itself IF EXISTS( SELECT 1 FROM WM_Payment WHERE ((Payment_No = @PaymentNo) AND (Reference_Payment_No IS NOT NULL)) --Specified payment is a reversal OR (Reference_Payment_No = @PaymentNo)) THEN --Specified payment has already been reversed RAISERROR 50001 'Cannot reverse a Payment that has already been reversed or is a reversal itself'; RETURN; END IF; --Can't reverse a payment that has been refunded IF EXISTS( SELECT 1 FROM WM_Payment WHERE (Payment_No = @PaymentNo) AND (Refund_Payment_No IS NOT NULL)) THEN RAISERROR 50001 'Cannot reverse a Payment that has been Refunded.'; RETURN; END IF; --Can't reverse a payment that is a linked Refund IF EXISTS( SELECT 1 FROM WM_Payment WHERE Refund_Payment_No = @PaymentNo) THEN RAISERROR 50001 'Cannot reverse a Payment that is a linked Refund.'; RETURN; END IF; --Cannot reverse payment when payment allocation records exist, unless the amount allocated = 0 --Note that a payment may exist, fully allocated, without any allocation records --Also a payment linked to a reversed transaction will also have links to the transaction's reversal, -- but the net Amount Allocated will be = 0 IF EXISTS( SELECT 1 FROM WM_Transaction_Payment WHERE Payment_No = @PaymentNo) AND NOT EXISTS( SELECT 1 FROM WM_Payment WHERE Payment_No = @PaymentNo AND Amount_Allocated = 0) THEN RAISERROR 50001 'Cannot reverse a Payment that has been allocated to one or more Transactions'; RETURN; END IF; --Add the new record INSERT WM_Payment( Pay_Method_No, Shift_No, Customer_No, Payment_Type, Vehicle_Code, Reference_Payment_No, Refund_Payment_No, Tran_No_If_Deposit, Payment_Date, Payment_Time, Operator, Site_No, Site_Workstation_No, Temp_Account_No, Workstation_Screen_Name, Amount_Paid, Amount_Allocated, Is_Fully_Allocated, Debit_Account, Round_Account, EPA_Account, GL_Account, Memo, Customer_Name, Customer_ABN, Customer_Address1, Customer_Address2, Customer_Address3, Customer_State, Customer_PostCode, Cheque_No, Cheque_BSB, Cheque_Account, Cheque_Bank, Cheque_Drawer, Cheque_Drawer_License, Eftpos_Card_No, Eftpos_Card_Type, Eftpos_Expiry_Date, Eftpos_Authorisation, Eftpos_Transaction_Ref, Eftpos_Account_Type, Eftpos_Terminal_ID, Eftpos_Audit_No, Eftpos_Receipt_String, Eftpos_Cashout_Amount, Generic_Field1, Generic_Field2, Generic_Field3, Replication_Site_Flag) SELECT Pay_Method_No, @CreatedShift, Customer_No, Payment_Type, Vehicle_Code, Payment_No, --Store reference to the original payment in the reversal NULL, --Refund_Payment_No not relevant Tran_No_If_Deposit, DATEFORMAT(@CreatedDateTime, 'yyyy-mm-dd'), DATEFORMAT(@CreatedDateTime, 'hh:mm:ss'), @CreatedUser, Site_No, --NOTE: Retain site number so payments stay together during replication @CreatedWorkstation, Temp_Account_No, '', --Workstation screen name not relevant for reversals -Amount_Paid, -Amount_Allocated, Is_Fully_Allocated, Debit_Account, Round_Account, EPA_Account, GL_Account, Memo, Customer_Name, Customer_ABN, Customer_Address1, Customer_Address2, Customer_Address3, Customer_State, Customer_PostCode, Cheque_No, Cheque_BSB, Cheque_Account, Cheque_Bank, Cheque_Drawer, Cheque_Drawer_License, Eftpos_Card_No, Eftpos_Card_Type, Eftpos_Expiry_Date, Eftpos_Authorisation, Eftpos_Transaction_Ref, Eftpos_Account_Type, Eftpos_Terminal_ID, Eftpos_Audit_No, Eftpos_Receipt_String, Eftpos_Cashout_Amount, Generic_Field1, Generic_Field2, Generic_Field3, Replication_Site_Flag --NOTE: Keep the original payment and its reversal together when replicating FROM WM_Payment WHERE Payment_No = @PaymentNo; --Get the new payment number SET @NewPaymentNo = @@identity; --Set the Amount Allocated field to the Amount Paid on both the Original Payment and the Reversal UPDATE WM_Payment SET Amount_Allocated = Amount_Paid, Is_Fully_Allocated = 'Y' WHERE Payment_No = @PaymentNo; UPDATE WM_Payment SET Amount_Allocated = Amount_Paid, Is_Fully_Allocated = 'Y' WHERE Payment_No = @NewPaymentNo; END; PASSTHROUGH STOP; END IF; END IF; /********************************************************************/ /* 07/03/2007 - Add stored proc to clear deposit-related flags on a payment */ /********************************************************************/ IF NOT EXISTS( SELECT 1 FROM sysprocedure WHERE proc_name = 'WM_ClearDepositFlags') THEN CREATE PROCEDURE "DBA".WM_ClearDepositFlags(IN @TranNo Large_Entity_No, IN @NewAvailable Boolean DEFAULT NULL) BEGIN /********************************************************************/ /* Clear Tran_No_If_Deposit field and set Payment_Type = 'P' */ /* for all Deposit payments applying to the nominated Transaction */ /********************************************************************/ IF @NewAvailable IS NOT NULL THEN UPDATE WM_Payment SET Tran_No_If_Deposit = NULL, Payment_Type = 'P', Available = @NewAvailable WHERE Tran_No_If_Deposit = @TranNo; ELSE UPDATE WM_Payment SET Tran_No_If_Deposit = NULL, Payment_Type = 'P' WHERE Tran_No_If_Deposit = @TranNo; END IF; END; --Passthrough the update to the remote sites (if in a replicated environment) IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; CREATE PROCEDURE "DBA".WM_ClearDepositFlags(IN @TranNo Large_Entity_No, IN @NewAvailable Boolean DEFAULT NULL) BEGIN /********************************************************************/ /* Clear Tran_No_If_Deposit field and set Payment_Type = 'P' */ /* for all Deposit payments applying to the nominated Transaction */ /********************************************************************/ IF @NewAvailable IS NOT NULL THEN UPDATE WM_Payment SET Tran_No_If_Deposit = NULL, Payment_Type = 'P', Available = @NewAvailable WHERE Tran_No_If_Deposit = @TranNo; ELSE UPDATE WM_Payment SET Tran_No_If_Deposit = NULL, Payment_Type = 'P' WHERE Tran_No_If_Deposit = @TranNo; END IF; END; PASSTHROUGH STOP; END IF; END IF; /********************************************************************/ /* 07/03/2007 - Update CreateTranReversal proc to also reverse payment */ /* allocation records, and rename it to WM_CreateTranReversal. */ /* Drop ReverseAllocations and MapTranItems procs as they are now obsolete */ /********************************************************************/ --Drop old procedure name and version control entries IF EXISTS( SELECT 1 FROM sysprocedure WHERE proc_name = 'CreateTranReversal') THEN DROP PROCEDURE "DBA".CreateTranReversal; DELETE "DBA".SM_Version_Control WHERE Entity_Name = 'CreateTranReversal'; --Passthrough the update to the remote sites (if in a replicated environment) IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; DROP PROCEDURE "DBA".CreateTranReversal; --DELETE will replicate normally so no need to passthrough PASSTHROUGH STOP; END IF; END IF; --Add updated procedure with new name IF NOT EXISTS( SELECT 1 FROM sysprocedure WHERE proc_name = 'WM_CreateTranReversal') THEN CREATE PROCEDURE "DBA".WM_CreateTranReversal(IN @OriginalTranNo Large_Entity_No, IN @DockPrefix VARCHAR(3), IN @DockSuffix VARCHAR(3), IN @DockNo INTEGER, IN @DockVersion INTEGER, IN @CreatedAt Entity_No, IN @CreatedBy Operator_Code, IN @ShiftNo Entity_No, IN @Reason Entity_Comment, OUT @NewTranNo Large_Entity_No, OUT @NewDocketDesc VARCHAR(20)) BEGIN ATOMIC DECLARE @OldItemNo Large_Entity_No; DECLARE @NewItemNo Large_Entity_No; DECLARE @OldDocketDesc VARCHAR(20); DECLARE Item_Cursor CURSOR FOR SELECT Transaction_Item_No FROM WM_Transaction_Item WHERE Transaction_No = @OriginalTranNo; --Check that the transaction exists IF NOT EXISTS( SELECT 1 FROM WM_Transaction_Header WHERE Transaction_No = @OriginalTranNo) THEN RAISERROR 50001 'Transaction No does not exist'; RETURN; END IF; --Raise error if the transaction is incomplete. --Only completed transactions can be reversed with this procedure IF EXISTS( SELECT 1 FROM WM_Transaction_Header WHERE Transaction_No = @OriginalTranNo AND Is_Completed = 'N') THEN RAISERROR 50001 'Cannot reverse an Incomplete Transaction'; RETURN; END IF; --Raise error if the transaction to be reversed has already been reversed, or is a reversal itself IF EXISTS( SELECT 1 FROM WM_Transaction_Header WHERE Transaction_No = @OriginalTranNo AND (Original_Tran_No IS NOT NULL OR Reversal_Tran_No IS NOT NULL)) THEN RAISERROR 50001 'Cannot reverse a Transaction that has already been reversed or is a reversal itself'; RETURN; END IF; --Get the docket number of the reversed tran (used when adding tran exception) SELECT Docket INTO @OldDocketDesc FROM WM_Transaction_Header WHERE Transaction_No = @OriginalTranNo; --Insert the reversal transaction INSERT WM_Transaction_Header( Vehicle_No, Vehicle_Code, Customer_No, Carrier_No, Job_No, EPA_Approval_No, Docket_No_Prefix, Docket_No, Docket_No_Suffix, Docket_No_Version, Date_Complete, Time_Complete, DateTime_In, DateTime_Out, Created_At, Created_By, Shift_No, Gross, Tare, Net, Gross_Date, Gross_Time, Tare_Date, Tare_Time, Order_no, Transaction_Amount, Amount_Paid, Is_Completed, Is_Stored_Tare, Is_Collection_Vehicle, Memo, Print_Counter, Original_Tran_No, Custom_Data1, Replication_Site_Flag, Group_G0, Group_G1, Group_G2, Group_G3, Group_G4) SELECT Vehicle_No, Vehicle_Code, Customer_No, Carrier_No, Job_No, EPA_Approval_No, @DockPrefix, @DockNo, @DockSuffix, @DockVersion, CURRENT DATE, CURRENT TIME, DateTime_In, DateTime_Out, Created_At, @CreatedBy, @ShiftNo, -Gross, -Tare, -Net, Gross_Date, Gross_Time, Tare_Date, Tare_Time, Order_no, 0, /* note must use zero initially until items are inserted */ 0, /* note must use zero initially until allocations are inserted */ Is_Completed, Is_Stored_Tare, Is_Collection_Vehicle, Memo, 0, Transaction_No, /* Save reversed transaction no as "Original Transaction No" */ Custom_Data1, Replication_Site_Flag, Group_G0, Group_G1, Group_G2, Group_G3, Group_G4 FROM WM_Transaction_Header WHERE Transaction_No = @OriginalTranNo; --Get the new transaction number SET @NewTranNo = @@identity; --Get the new docket no in same format as computed column SELECT Docket INTO @NewDocketDesc FROM WM_Transaction_Header WHERE Transaction_No = @NewTranNo; --Set the link to the new transaction on the old transaction UPDATE WM_Transaction_Header SET Reversal_Tran_No = @NewTranNo WHERE Transaction_No = @OriginalTranNo; --Reverse the tran attachments INSERT WM_Transaction_Attach( Transaction_No, Attachment_No, Tare, Quantity) SELECT @NewTranNo, Attachment_No, -Tare, Quantity FROM WM_Transaction_Attach WHERE Transaction_No = @OriginalTranNo; --Reverse the tran exceptions INSERT WM_Transaction_Exception( Transaction_No, Exception_No, Exception_Comment, Exception_Date, Exception_Time, Operator_Code) SELECT @NewTranNo, Exception_No, Exception_Comment, Exception_Date, Exception_Time, Operator_Code FROM WM_Transaction_Exception WHERE Transaction_No = @OriginalTranNo; --Reverse the tran image links INSERT WM_Transaction_Image( Transaction_No, Image_No) SELECT @NewTranNo, Image_No FROM WM_Transaction_Image WHERE Transaction_No = @OriginalTranNo; --Reverse the Items and Rates OPEN Item_Cursor; lp: LOOP FETCH NEXT Item_Cursor INTO @OldItemNo; IF SQLCODE <> 0 THEN LEAVE lp END IF; --Add the reversal item INSERT WM_Transaction_Item( Transaction_No, Area_No, Product_No, Waste_Stream_No, Sub_Stream_1_No, Sub_Stream_2_No, Sub_Stream_3_No, Gross, Tare, Net, Cubic_Metres, Quantity, LGA_code, Cost_Rule_No, Rate_Range_No, Item_Charge, Cost_Method, EPA_Levy_Exempt) SELECT @NewTranNo, Area_No, Product_No, Waste_Stream_No, Sub_Stream_1_No, Sub_Stream_2_No, Sub_Stream_3_No, -Gross, -Tare, -Net, -Cubic_Metres, -Quantity, LGA_code, Cost_Rule_No, Rate_Range_No, 0, /* note must use zero initially until rates are inserted */ Cost_Method, EPA_Levy_Exempt FROM WM_Transaction_Item WHERE Transaction_Item_No = @OldItemNo; -- NOTE Transaction_Item_No is an identity field, so can be referenced on its own --Get the new item no SET @NewItemNo = @@identity; --Insert the rates for the new item INSERT WM_Transaction_Item_Rate( Transaction_No, Transaction_Item_No, Rate_No, Rate, Value) SELECT @NewTranNo, @NewItemNo, Rate_No, Rate, -Value FROM WM_Transaction_Item_Rate WHERE Transaction_No = @OriginalTranNo AND Transaction_Item_No = @OldItemNo; --Update Item charge fields now that rates have been inserted --Amount_Paid field will be updated when the allocations are reversed UPDATE WM_Transaction_Item SET Item_Charge = (SELECT -Item_Charge FROM WM_Transaction_Item WHERE Transaction_Item_No = @OldItemNo) WHERE Transaction_Item_No = @NewItemNo; END LOOP; CLOSE Item_Cursor; --Now all Items and Rates are inserted, can set the Transaction Charges on the reversal tran UPDATE WM_Transaction_Header SET Transaction_Amount = (SELECT -Transaction_Amount FROM WM_Transaction_Header WHERE Transaction_No = @OriginalTranNo) WHERE Transaction_No = @NewTranNo; --Now reverse the payment allocations to the transaction --The same payments are used, but the allocation amounts are negated INSERT WM_Transaction_Payment( Payment_No, Transaction_No, Shift_No, Allocation_Date, Amount) SELECT Payment_No, @NewTranNo, @ShiftNo, CURRENT TIMESTAMP, -Amount FROM WM_Transaction_Payment WHERE Transaction_No = @OriginalTranNo; --Update the payment records' Amount_Paid fields CREATE TABLE #PayTotals( Payment_No BIGINT PRIMARY KEY, Total_Allocated NUMERIC(10,2)); INSERT #PayTotals(Payment_No, Total_Allocated) SELECT Payment_No, ISNULL(SUM(Amount), 0) FROM WM_Transaction_Payment WHERE Payment_No IN (SELECT Payment_No FROM WM_Transaction_Payment WHERE Transaction_No = @OriginalTranNo) GROUP BY Payment_No; UPDATE WM_Payment SET Amount_Allocated = PT.Total_Allocated, Is_Fully_Allocated = (CASE WHEN PT.Total_Allocated = Amount_Paid THEN 'Y' ELSE 'N' END) FROM WM_Payment P, #PayTotals PT WHERE P.Payment_No = PT.Payment_No; --Update transaction Amount Paid field now that allocations have been inserted UPDATE WM_Transaction_Header SET Amount_Paid = (SELECT -Amount_Paid FROM WM_Transaction_Header WHERE Transaction_No = @OriginalTranNo) WHERE Transaction_No = @NewTranNo; --Add relevant exceptions to the transaction and its reversal --'R' to the reversed transaction, 'S' to the new transaction INSERT WM_Transaction_Exception( Transaction_No, Exception_No, Exception_Comment, Operator_Code) SELECT @OriginalTranNo, Exception_No, 'Reason: ' || @Reason || ', reversed by Docket ' || @NewDocketDesc, @CreatedBy FROM WM_Exceptions WHERE Code = 'R'; INSERT WM_Transaction_Exception( Transaction_No, Exception_No, Exception_Comment, Operator_Code) SELECT @NewTranNo, Exception_No, 'Reason: ' || @Reason || ', reversal of Docket ' || @OldDocketDesc, @CreatedBy FROM WM_Exceptions WHERE Code = 'S'; END; END IF; --Passthrough the update to the remote sites (if in a replicated environment) --This is done in a separate statement due to DBMS issues when DECLARE CURSOR is used twice in the same IF statement IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') AND EXISTS( SELECT 1 FROM sysprocedure WHERE proc_name = 'WM_CreateTranReversal') AND NOT EXISTS( SELECT 1 FROM "DBA".SM_Version_Control WHERE Entity_Name = 'WM_CreateTranReversal' AND Database_ID = (SELECT db_property('GlobalDbID'))) THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; CREATE PROCEDURE "DBA".WM_CreateTranReversal(IN @OriginalTranNo Large_Entity_No, IN @DockPrefix VARCHAR(3), IN @DockSuffix VARCHAR(3), IN @DockNo INTEGER, IN @DockVersion INTEGER, IN @CreatedAt Entity_No, IN @CreatedBy Operator_Code, IN @ShiftNo Entity_No, IN @Reason Entity_Comment, OUT @NewTranNo Large_Entity_No, OUT @NewDocketDesc VARCHAR(20)) BEGIN ATOMIC DECLARE @OldItemNo Large_Entity_No; DECLARE @NewItemNo Large_Entity_No; DECLARE @OldDocketDesc VARCHAR(20); DECLARE Item_Cursor CURSOR FOR SELECT Transaction_Item_No FROM WM_Transaction_Item WHERE Transaction_No = @OriginalTranNo; --Check that the transaction exists IF NOT EXISTS( SELECT 1 FROM WM_Transaction_Header WHERE Transaction_No = @OriginalTranNo) THEN RAISERROR 50001 'Transaction No does not exist'; RETURN; END IF; --Raise error if the transaction is incomplete. --Only completed transactions can be reversed with this procedure IF EXISTS( SELECT 1 FROM WM_Transaction_Header WHERE Transaction_No = @OriginalTranNo AND Is_Completed = 'N') THEN RAISERROR 50001 'Cannot reverse an Incomplete Transaction'; RETURN; END IF; --Raise error if the transaction to be reversed has already been reversed, or is a reversal itself IF EXISTS( SELECT 1 FROM WM_Transaction_Header WHERE Transaction_No = @OriginalTranNo AND (Original_Tran_No IS NOT NULL OR Reversal_Tran_No IS NOT NULL)) THEN RAISERROR 50001 'Cannot reverse a Transaction that has already been reversed or is a reversal itself'; RETURN; END IF; --Get the docket number of the reversed tran (used when adding tran exception) SELECT Docket INTO @OldDocketDesc FROM WM_Transaction_Header WHERE Transaction_No = @OriginalTranNo; --Insert the reversal transaction INSERT WM_Transaction_Header( Vehicle_No, Vehicle_Code, Customer_No, Carrier_No, Job_No, EPA_Approval_No, Docket_No_Prefix, Docket_No, Docket_No_Suffix, Docket_No_Version, Date_Complete, Time_Complete, DateTime_In, DateTime_Out, Created_At, Created_By, Shift_No, Gross, Tare, Net, Gross_Date, Gross_Time, Tare_Date, Tare_Time, Order_no, Transaction_Amount, Amount_Paid, Is_Completed, Is_Stored_Tare, Is_Collection_Vehicle, Memo, Print_Counter, Original_Tran_No, Custom_Data1, Replication_Site_Flag, Group_G0, Group_G1, Group_G2, Group_G3, Group_G4) SELECT Vehicle_No, Vehicle_Code, Customer_No, Carrier_No, Job_No, EPA_Approval_No, @DockPrefix, @DockNo, @DockSuffix, @DockVersion, CURRENT DATE, CURRENT TIME, DateTime_In, DateTime_Out, Created_At, @CreatedBy, @ShiftNo, -Gross, -Tare, -Net, Gross_Date, Gross_Time, Tare_Date, Tare_Time, Order_no, 0, /* note must use zero initially until items are inserted */ 0, /* note must use zero initially until allocations are inserted */ Is_Completed, Is_Stored_Tare, Is_Collection_Vehicle, Memo, 0, Transaction_No, /* Save reversed transaction no as "Original Transaction No" */ Custom_Data1, Replication_Site_Flag, Group_G0, Group_G1, Group_G2, Group_G3, Group_G4 FROM WM_Transaction_Header WHERE Transaction_No = @OriginalTranNo; --Get the new transaction number SET @NewTranNo = @@identity; --Get the new docket no in same format as computed column SELECT Docket INTO @NewDocketDesc FROM WM_Transaction_Header WHERE Transaction_No = @NewTranNo; --Set the link to the new transaction on the old transaction UPDATE WM_Transaction_Header SET Reversal_Tran_No = @NewTranNo WHERE Transaction_No = @OriginalTranNo; --Reverse the tran attachments INSERT WM_Transaction_Attach( Transaction_No, Attachment_No, Tare, Quantity) SELECT @NewTranNo, Attachment_No, -Tare, Quantity FROM WM_Transaction_Attach WHERE Transaction_No = @OriginalTranNo; --Reverse the tran exceptions INSERT WM_Transaction_Exception( Transaction_No, Exception_No, Exception_Comment, Exception_Date, Exception_Time, Operator_Code) SELECT @NewTranNo, Exception_No, Exception_Comment, Exception_Date, Exception_Time, Operator_Code FROM WM_Transaction_Exception WHERE Transaction_No = @OriginalTranNo; --Reverse the tran image links INSERT WM_Transaction_Image( Transaction_No, Image_No) SELECT @NewTranNo, Image_No FROM WM_Transaction_Image WHERE Transaction_No = @OriginalTranNo; --Reverse the Items and Rates OPEN Item_Cursor; lp: LOOP FETCH NEXT Item_Cursor INTO @OldItemNo; IF SQLCODE <> 0 THEN LEAVE lp END IF; --Add the reversal item INSERT WM_Transaction_Item( Transaction_No, Area_No, Product_No, Waste_Stream_No, Sub_Stream_1_No, Sub_Stream_2_No, Sub_Stream_3_No, Gross, Tare, Net, Cubic_Metres, Quantity, LGA_code, Cost_Rule_No, Rate_Range_No, Item_Charge, Cost_Method, EPA_Levy_Exempt) SELECT @NewTranNo, Area_No, Product_No, Waste_Stream_No, Sub_Stream_1_No, Sub_Stream_2_No, Sub_Stream_3_No, -Gross, -Tare, -Net, -Cubic_Metres, -Quantity, LGA_code, Cost_Rule_No, Rate_Range_No, 0, /* note must use zero initially until rates are inserted */ Cost_Method, EPA_Levy_Exempt FROM WM_Transaction_Item WHERE Transaction_Item_No = @OldItemNo; -- NOTE Transaction_Item_No is an identity field, so can be referenced on its own --Get the new item no SET @NewItemNo = @@identity; --Insert the rates for the new item INSERT WM_Transaction_Item_Rate( Transaction_No, Transaction_Item_No, Rate_No, Rate, Value) SELECT @NewTranNo, @NewItemNo, Rate_No, Rate, -Value FROM WM_Transaction_Item_Rate WHERE Transaction_No = @OriginalTranNo AND Transaction_Item_No = @OldItemNo; --Update Item charge fields now that rates have been inserted --Amount_Paid field will be updated when the allocations are reversed UPDATE WM_Transaction_Item SET Item_Charge = (SELECT -Item_Charge FROM WM_Transaction_Item WHERE Transaction_Item_No = @OldItemNo) WHERE Transaction_Item_No = @NewItemNo; END LOOP; CLOSE Item_Cursor; --Now all Items and Rates are inserted, can set the Transaction Charges on the reversal tran UPDATE WM_Transaction_Header SET Transaction_Amount = (SELECT -Transaction_Amount FROM WM_Transaction_Header WHERE Transaction_No = @OriginalTranNo) WHERE Transaction_No = @NewTranNo; --Now reverse the payment allocations to the transaction --The same payments are used, but the allocation amounts are negated INSERT WM_Transaction_Payment( Payment_No, Transaction_No, Shift_No, Allocation_Date, Amount) SELECT Payment_No, @NewTranNo, @ShiftNo, CURRENT TIMESTAMP, -Amount FROM WM_Transaction_Payment WHERE Transaction_No = @OriginalTranNo; --Update the payment records' Amount_Paid fields CREATE TABLE #PayTotals( Payment_No BIGINT PRIMARY KEY, Total_Allocated NUMERIC(10,2)); INSERT #PayTotals(Payment_No, Total_Allocated) SELECT Payment_No, ISNULL(SUM(Amount), 0) FROM WM_Transaction_Payment WHERE Payment_No IN (SELECT Payment_No FROM WM_Transaction_Payment WHERE Transaction_No = @OriginalTranNo) GROUP BY Payment_No; UPDATE WM_Payment SET Amount_Allocated = PT.Total_Allocated, Is_Fully_Allocated = (CASE WHEN PT.Total_Allocated = Amount_Paid THEN 'Y' ELSE 'N' END) FROM WM_Payment P, #PayTotals PT WHERE P.Payment_No = PT.Payment_No; --Update transaction Amount Paid field now that allocations have been inserted UPDATE WM_Transaction_Header SET Amount_Paid = (SELECT -Amount_Paid FROM WM_Transaction_Header WHERE Transaction_No = @OriginalTranNo) WHERE Transaction_No = @NewTranNo; --Add relevant exceptions to the transaction and its reversal --'R' to the reversed transaction, 'S' to the new transaction INSERT WM_Transaction_Exception( Transaction_No, Exception_No, Exception_Comment, Operator_Code) SELECT @OriginalTranNo, Exception_No, 'Reason: ' || @Reason || ', reversed by Docket ' || @NewDocketDesc, @CreatedBy FROM WM_Exceptions WHERE Code = 'R'; INSERT WM_Transaction_Exception( Transaction_No, Exception_No, Exception_Comment, Operator_Code) SELECT @NewTranNo, Exception_No, 'Reason: ' || @Reason || ', reversal of Docket ' || @OldDocketDesc, @CreatedBy FROM WM_Exceptions WHERE Code = 'S'; END; INSERT "DBA".SM_Version_Control(Entity_Name, Database_ID, Version_No) VALUES('WM_CreateTranReversal', (SELECT db_property('GlobalDbID')), 1); PASSTHROUGH STOP; INSERT "DBA".SM_Version_Control(Entity_Name, Database_ID, Version_No) VALUES('WM_CreateTranReversal', (SELECT db_property('GlobalDbID')), 1); END IF; --Drop ReverseAllocations proc IF EXISTS( SELECT 1 FROM sysprocedure WHERE proc_name = 'ReverseAllocations') THEN DROP PROCEDURE "DBA".ReverseAllocations; --Passthrough the update to the remote sites (if in a replicated environment) IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; DROP PROCEDURE "DBA".ReverseAllocations; PASSTHROUGH STOP; END IF; END IF; --Drop MapTranItems proc IF EXISTS( SELECT 1 FROM sysprocedure WHERE proc_name = 'MapTranItems') THEN DROP PROCEDURE "DBA".MapTranItems; --Passthrough the update to the remote sites (if in a replicated environment) IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; DROP PROCEDURE "DBA".MapTranItems; PASSTHROUGH STOP; END IF; END IF; /********************************************************************/ /* 07/03/2007 - Add stored proc to update EFT payment details after */ /* EFT transaction approved */ /********************************************************************/ IF NOT EXISTS( SELECT 1 FROM sysprocedure WHERE proc_name = 'WM_UpdateEftPayment') THEN CREATE PROCEDURE "DBA".WM_UpdateEftPayment(IN @PaymentNo Large_Entity_No, IN @CardNo VARCHAR(32), IN @CardType VARCHAR(32), IN @ExpiryDate Entity_Date, IN @Authorisation VARCHAR(32), IN @AccountType VARCHAR(15), IN @TranRef VARCHAR(16), IN @TerminalID VARCHAR(20), IN @AuditNo VARCHAR(8), IN @ReceiptString VARCHAR(2048), IN @CashoutAmount NUMERIC(8,2), IN @CashPayMethod Entity_No) BEGIN ATOMIC --@PaymentNo, @TranRef, @TerminalID required as a minimum IF @PaymentNo IS NULL THEN RAISERROR 50001 'You must supply a Payment No'; RETURN; END IF; IF ISNULL(@TranRef, '') = '' THEN RAISERROR 50001 'You must supply the EFTPOS Transaction Reference'; RETURN; END IF; IF ISNULL(@TerminalID, '') = '' THEN RAISERROR 50001 'You must supply the EFTPOS Terminal ID'; RETURN; END IF; --Payment must exist ! and must be an EFTPOS payment for the nominated terminal IF NOT EXISTS( SELECT 1 FROM WM_Payment P INNER JOIN WM_Payment_Method M ON (M.Pay_Method_No = P.Pay_Method_No) WHERE (P.Payment_No = @PaymentNo) AND (P.Eftpos_Terminal_ID = @TerminalID) AND (M.Prompt = 'E')) THEN RAISERROR 50001 'Payment No %1! not found, not an EFTPOS payment or does not belong to Terminal ID %2!', @PaymentNo, @TerminalID; RETURN; END IF; UPDATE WM_Payment SET Eftpos_Card_No = @CardNo, Eftpos_Card_Type = @CardType, Eftpos_Expiry_Date = @ExpiryDate, Eftpos_Authorisation = @Authorisation, Eftpos_Account_Type = @AccountType, Eftpos_Transaction_Ref = @TranRef, Eftpos_Audit_No = @AuditNo, Eftpos_Receipt_String = @ReceiptString, Eftpos_Cashout_Amount = @CashoutAmount, Available = 'Y' WHERE Payment_No = @PaymentNo; --If cashout was nominated, must add a Payout entry to the Cash Drawer Log. --Log entry will contain the @TranRef as a cross-reference, but no other EFT details --Use the Station_ID from the payment Shift IF @CashoutAmount > 0 THEN INSERT WM_Cash_Drawer_Log (Cash_Drawer_Log_Type, Log_Date, Log_Time, Site_No, Shift_No, Station_ID, Operator, Amount, Pay_Method_No, Eftpos_Transaction_Ref, Replication_Site_No) SELECT 'P', P.Payment_Date, P.Payment_Time, P.Site_No, P.Shift_No, S.Station_ID, P.Operator, (-1 * @CashoutAmount), @CashPayMethod, @TranRef, RTRIM(CAST(P.Site_No AS CHAR)) FROM WM_Payment P INNER JOIN WM_Shift S ON (S.Shift_No = P.Shift_No) WHERE P.Payment_No = @PaymentNo; END IF; END; --Passthrough the update to the remote sites (if in a replicated environment) IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; CREATE PROCEDURE "DBA".WM_UpdateEftPayment(IN @PaymentNo Large_Entity_No, IN @CardNo VARCHAR(32), IN @CardType VARCHAR(32), IN @ExpiryDate Entity_Date, IN @Authorisation VARCHAR(32), IN @AccountType VARCHAR(15), IN @TranRef VARCHAR(16), IN @TerminalID VARCHAR(20), IN @AuditNo VARCHAR(8), IN @ReceiptString VARCHAR(2048), IN @CashoutAmount NUMERIC(8,2), IN @CashPayMethod Entity_No) BEGIN ATOMIC --@PaymentNo, @TranRef, @TerminalID required as a minimum IF @PaymentNo IS NULL THEN RAISERROR 50001 'You must supply a Payment No'; RETURN; END IF; IF ISNULL(@TranRef, '') = '' THEN RAISERROR 50001 'You must supply the EFTPOS Transaction Reference'; RETURN; END IF; IF ISNULL(@TerminalID, '') = '' THEN RAISERROR 50001 'You must supply the EFTPOS Terminal ID'; RETURN; END IF; --Payment must exist ! and must be an EFTPOS payment for the nominated terminal IF NOT EXISTS( SELECT 1 FROM WM_Payment P INNER JOIN WM_Payment_Method M ON (M.Pay_Method_No = P.Pay_Method_No) WHERE (P.Payment_No = @PaymentNo) AND (P.Eftpos_Terminal_ID = @TerminalID) AND (M.Prompt = 'E')) THEN RAISERROR 50001 'Payment No %1! not found, not an EFTPOS payment or does not belong to Terminal ID %2!', @PaymentNo, @TerminalID; RETURN; END IF; UPDATE WM_Payment SET Eftpos_Card_No = @CardNo, Eftpos_Card_Type = @CardType, Eftpos_Expiry_Date = @ExpiryDate, Eftpos_Authorisation = @Authorisation, Eftpos_Account_Type = @AccountType, Eftpos_Transaction_Ref = @TranRef, Eftpos_Audit_No = @AuditNo, Eftpos_Receipt_String = @ReceiptString, Eftpos_Cashout_Amount = @CashoutAmount, Available = 'Y' WHERE Payment_No = @PaymentNo; --If cashout was nominated, must add a Payout entry to the Cash Drawer Log. --Log entry will contain the @TranRef as a cross-reference, but no other EFT details --Use the Station_ID from the payment Shift IF @CashoutAmount > 0 THEN INSERT WM_Cash_Drawer_Log (Cash_Drawer_Log_Type, Log_Date, Log_Time, Site_No, Shift_No, Station_ID, Operator, Amount, Pay_Method_No, Eftpos_Transaction_Ref, Replication_Site_No) SELECT 'P', P.Payment_Date, P.Payment_Time, P.Site_No, P.Shift_No, S.Station_ID, P.Operator, (-1 * @CashoutAmount), @CashPayMethod, @TranRef, RTRIM(CAST(P.Site_No AS CHAR)) FROM WM_Payment P INNER JOIN WM_Shift S ON (S.Shift_No = P.Shift_No) WHERE P.Payment_No = @PaymentNo; END IF; END; PASSTHROUGH STOP; END IF; END IF; /*********************************************************************/ /* 07/03/2007 - Add stored procedure to Link a Refund Payment */ /*********************************************************************/ IF NOT EXISTS( SELECT 1 FROM sysprocedure WHERE proc_name = 'WM_LinkRefundPayment') THEN CREATE PROCEDURE "DBA".WM_LinkRefundPayment(IN @RefundPaymentNo Large_Entity_No, IN @PaymentsCSV VARCHAR(300)) BEGIN ATOMIC DECLARE @TotalRefund NUMERIC(8,2); DECLARE @NumsTemp VARCHAR(500); DECLARE @RepFlagValueCnt INTEGER; --Must supply input variables IF @RefundPaymentNo IS NULL THEN RAISERROR 50001 'You must specify the Refund Payment No'; RETURN; END IF; IF ISNULL(@PaymentsCSV, '') = '' THEN RAISERROR 50001 'You must specify the Payments to link the Refund to'; RETURN; END IF; --Refund payment must exist IF NOT EXISTS( SELECT 1 FROM WM_Payment WHERE Payment_No = @RefundPaymentNo) THEN RAISERROR 50001 'Refund Payment %1! not found', @RefundPaymentNo; RETURN; END IF; --Create a table containing the payment numbers supplied CREATE TABLE #PaymentList(Payment_No BIGINT); SET @NumsTemp = @PaymentsCSV; WHILE LTRIM(@NumsTemp) <> '' LOOP IF CHARINDEX(',', @NumsTemp) > 0 THEN INSERT #PaymentList(Payment_No) VALUES(CONVERT(BIGINT, LTRIM(LEFT(@NumsTemp, CHARINDEX(',', @NumsTemp)-1)))); SET @NumsTemp = LTRIM(STUFF(@NumsTemp, 1, CHARINDEX(',', @NumsTemp), NULL)); ELSE INSERT #PaymentList(Payment_No) VALUES(CONVERT(BIGINT, LTRIM(@NumsTemp))); SET @NumsTemp = ''; END IF; END LOOP; --Amount to be refunded is the sum of the available amount on the payments / refunds nominated SELECT ISNULL(SUM(Amount_Paid), 0) - ISNULL(SUM(Amount_Allocated), 0) INTO @TotalRefund FROM WM_Payment WHERE Payment_No IN (SELECT Payment_No FROM #PaymentList); --Refund remaining amount available on each payment / refund and link the refund payment nominated UPDATE WM_Payment SET Amount_Allocated = Amount_Paid, Is_Fully_Allocated = 'Y', Refund_Payment_No = @RefundPaymentNo WHERE Payment_No IN (SELECT Payment_No FROM #PaymentList); --Set Amount_Allocated on Refund payment = total amount refunded --Set Is_Fully_Allocated = 'Y' when necessary UPDATE WM_Payment SET Amount_Allocated = Amount_Allocated - @TotalRefund, --NOTE @TotalRefund will be +ve Is_Fully_Allocated = (CASE WHEN (Amount_Allocated - @TotalRefund) = Amount_Paid THEN 'Y' ELSE 'N' END) WHERE Payment_No = @RefundPaymentNo; --Set Replication_Site_Flag to zero on all if the flags differ between the payments and the refund payment --This is necessary to ensure the linked refund stays with its payments --Trigger on WM_Payment will set associated transaction header flags to zero SELECT COUNT(DISTINCT Replication_Site_Flag) INTO @RepFlagValueCnt FROM WM_Payment WHERE (Payment_No = @RefundPaymentNo) OR (Payment_No IN (SELECT Payment_No FROM #PaymentList)); IF @RepFlagValueCnt > 1 THEN UPDATE WM_Payment SET Replication_Site_Flag = '0' WHERE ((Payment_No = @RefundPaymentNo) OR (Payment_No IN (SELECT Payment_No FROM #PaymentList))) AND (Replication_Site_Flag <> '0'); END IF; END; --Passthrough the update to the remote sites (if in a replicated environment) IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; CREATE PROCEDURE "DBA".WM_LinkRefundPayment(IN @RefundPaymentNo Large_Entity_No, IN @PaymentsCSV VARCHAR(300)) BEGIN ATOMIC DECLARE @TotalRefund NUMERIC(8,2); DECLARE @NumsTemp VARCHAR(500); DECLARE @RepFlagValueCnt INTEGER; --Must supply input variables IF @RefundPaymentNo IS NULL THEN RAISERROR 50001 'You must specify the Refund Payment No'; RETURN; END IF; IF ISNULL(@PaymentsCSV, '') = '' THEN RAISERROR 50001 'You must specify the Payments to link the Refund to'; RETURN; END IF; --Refund payment must exist IF NOT EXISTS( SELECT 1 FROM WM_Payment WHERE Payment_No = @RefundPaymentNo) THEN RAISERROR 50001 'Refund Payment %1! not found', @RefundPaymentNo; RETURN; END IF; --Create a table containing the payment numbers supplied CREATE TABLE #PaymentList(Payment_No BIGINT); SET @NumsTemp = @PaymentsCSV; WHILE LTRIM(@NumsTemp) <> '' LOOP IF CHARINDEX(',', @NumsTemp) > 0 THEN INSERT #PaymentList(Payment_No) VALUES(CONVERT(BIGINT, LTRIM(LEFT(@NumsTemp, CHARINDEX(',', @NumsTemp)-1)))); SET @NumsTemp = LTRIM(STUFF(@NumsTemp, 1, CHARINDEX(',', @NumsTemp), NULL)); ELSE INSERT #PaymentList(Payment_No) VALUES(CONVERT(BIGINT, LTRIM(@NumsTemp))); SET @NumsTemp = ''; END IF; END LOOP; --Amount to be refunded is the sum of the available amount on the payments / refunds nominated SELECT ISNULL(SUM(Amount_Paid), 0) - ISNULL(SUM(Amount_Allocated), 0) INTO @TotalRefund FROM WM_Payment WHERE Payment_No IN (SELECT Payment_No FROM #PaymentList); --Refund remaining amount available on each payment / refund and link the refund payment nominated UPDATE WM_Payment SET Amount_Allocated = Amount_Paid, Is_Fully_Allocated = 'Y', Refund_Payment_No = @RefundPaymentNo WHERE Payment_No IN (SELECT Payment_No FROM #PaymentList); --Set Amount_Allocated on Refund payment = total amount refunded --Set Is_Fully_Allocated = 'Y' when necessary UPDATE WM_Payment SET Amount_Allocated = Amount_Allocated - @TotalRefund, --NOTE @TotalRefund will be +ve Is_Fully_Allocated = (CASE WHEN (Amount_Allocated - @TotalRefund) = Amount_Paid THEN 'Y' ELSE 'N' END) WHERE Payment_No = @RefundPaymentNo; --Set Replication_Site_Flag to zero on all if the flags differ between the payments and the refund payment --This is necessary to ensure the linked refund stays with its payments --Trigger on WM_Payment will set associated transaction header flags to zero SELECT COUNT(DISTINCT Replication_Site_Flag) INTO @RepFlagValueCnt FROM WM_Payment WHERE (Payment_No = @RefundPaymentNo) OR (Payment_No IN (SELECT Payment_No FROM #PaymentList)); IF @RepFlagValueCnt > 1 THEN UPDATE WM_Payment SET Replication_Site_Flag = '0' WHERE ((Payment_No = @RefundPaymentNo) OR (Payment_No IN (SELECT Payment_No FROM #PaymentList))) AND (Replication_Site_Flag <> '0'); END IF; END; PASSTHROUGH STOP; END IF; END IF; /*********************************************************************/ /* 07/03/2007 - Add stored procedure to return a list of payments for Refund */ /*********************************************************************/ IF NOT EXISTS( SELECT 1 FROM sysprocedure WHERE proc_name = 'WM_RefundablePayments') THEN CREATE PROCEDURE "DBA".WM_RefundablePayments(IN @VehCode Entity_Code, IN @CustNo Entity_No, IN @SiteNo Entity_No) RESULT( Payment_No Large_Entity_No, Payment_Date Entity_Date, Payment_Time Entity_Time, Customer Entity_Name, Vehicle Entity_Code, Pay_Type VARCHAR(10), Site Entity_Code, Method Entity_Name, Payment_Amount NUMERIC(8,2), Amount_Available NUMERIC(8,2), Pay_Method_No Entity_No, Pay_Method_Prompt CHAR(1), Customer_No Entity_No, Cust_Default_Pay_Method_No Entity_No, ABN ABN, Delivery_Address1 Entity_Address, Delivery_Address2 Entity_Address, Delivery_Address3 Entity_Address, Delivery_State Australian_State, Delivery_PostCode Postcode) BEGIN --@CustNo must be supplied. Others can be NULL IF @CustNo IS NULL THEN RAISERROR 50001 'Customer No. must be specified.'; RETURN; END IF; SELECT P.Payment_No, P.Payment_Date, P.Payment_Time, C.Name AS Customer, P.Vehicle_Code AS Vehicle, (CASE P.Payment_Type WHEN 'D' THEN 'Deposit' WHEN 'P' THEN 'Payment' WHEN 'R' THEN 'Refund' WHEN 'A' THEN 'Pre-Pay' ELSE P.Payment_Type END) AS Pay_Type, S.Site_Code AS Site, M.Method, P.Amount_Paid AS Payment_Amount, (P.Amount_Paid - P.Amount_Allocated) AS Amount_Available, P.Pay_Method_No, M.Prompt AS Pay_Method_Prompt, P.Customer_No, C.Default_Pay_Method_No AS Cust_Default_Pay_Method_No, C.ABN, C.Delivery_Address1, C.Delivery_Address2, C.Delivery_Address3, C.Delivery_State, C.Delivery_PostCode FROM WM_Payment P INNER JOIN WM_Payment_Method M ON (M.Pay_Method_No = P.Pay_Method_No) INNER JOIN WM_Customer C ON (C.Customer_No = P.Customer_No) INNER JOIN SM_Site S ON (S.Site_No = P.Site_No) LEFT OUTER JOIN WM_Payment Rev ON (Rev.Reference_Payment_No = P.Payment_No) WHERE (P.Customer_No = @CustNo) --applies to the nominated customer AND ((P.Vehicle_Code = @VehCode) OR (@VehCode IS NULL) OR (P.Vehicle_Code IS NULL)) --...and to the nominated vehicle (if supplied) - if specified on the payment AND ((P.Site_No = @SiteNo) OR (@SiteNo IS NULL)) --entered at the nominated site (or no site number supplied) AND (P.Is_Fully_Allocated = 'N') --not fully allocated (regardless of Available flag) AND (P.Tran_No_If_Deposit IS NULL) --not linked to an Incomplete transaction as a Deposit AND (M.Include_On_Cash_Summary = 'Y') --cash summary-type pay method (not Account etc.) AND (P.Payment_Type <> 'R') --not a Refund (linked or otherwise) AND (P.Refund_Payment_No IS NULL) --has not been refunded AND (P.Reference_Payment_No IS NULL) --is not a reversal AND (Rev.Payment_No IS NULL) --has not been reversed ORDER BY P.Payment_Date, P.Payment_Time, P.Payment_No; END; --Passthrough the update to the remote sites (if in a replicated environment) IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; CREATE PROCEDURE "DBA".WM_RefundablePayments(IN @VehCode Entity_Code, IN @CustNo Entity_No, IN @SiteNo Entity_No) RESULT( Payment_No Large_Entity_No, Payment_Date Entity_Date, Payment_Time Entity_Time, Customer Entity_Name, Vehicle Entity_Code, Pay_Type VARCHAR(10), Site Entity_Code, Method Entity_Name, Payment_Amount NUMERIC(8,2), Amount_Available NUMERIC(8,2), Pay_Method_No Entity_No, Pay_Method_Prompt CHAR(1), Customer_No Entity_No, Cust_Default_Pay_Method_No Entity_No, ABN ABN, Delivery_Address1 Entity_Address, Delivery_Address2 Entity_Address, Delivery_Address3 Entity_Address, Delivery_State Australian_State, Delivery_PostCode Postcode) BEGIN --@CustNo must be supplied. Others can be NULL IF @CustNo IS NULL THEN RAISERROR 50001 'Customer No. must be specified.'; RETURN; END IF; SELECT P.Payment_No, P.Payment_Date, P.Payment_Time, C.Name AS Customer, P.Vehicle_Code AS Vehicle, (CASE P.Payment_Type WHEN 'D' THEN 'Deposit' WHEN 'P' THEN 'Payment' WHEN 'R' THEN 'Refund' WHEN 'A' THEN 'Pre-Pay' ELSE P.Payment_Type END) AS Pay_Type, S.Site_Code AS Site, M.Method, P.Amount_Paid AS Payment_Amount, (P.Amount_Paid - P.Amount_Allocated) AS Amount_Available, P.Pay_Method_No, M.Prompt AS Pay_Method_Prompt, P.Customer_No, C.Default_Pay_Method_No AS Cust_Default_Pay_Method_No, C.ABN, C.Delivery_Address1, C.Delivery_Address2, C.Delivery_Address3, C.Delivery_State, C.Delivery_PostCode FROM WM_Payment P INNER JOIN WM_Payment_Method M ON (M.Pay_Method_No = P.Pay_Method_No) INNER JOIN WM_Customer C ON (C.Customer_No = P.Customer_No) INNER JOIN SM_Site S ON (S.Site_No = P.Site_No) LEFT OUTER JOIN WM_Payment Rev ON (Rev.Reference_Payment_No = P.Payment_No) WHERE (P.Customer_No = @CustNo) --applies to the nominated customer AND ((P.Vehicle_Code = @VehCode) OR (@VehCode IS NULL) OR (P.Vehicle_Code IS NULL)) --...and to the nominated vehicle (if supplied) - if specified on the payment AND ((P.Site_No = @SiteNo) OR (@SiteNo IS NULL)) --entered at the nominated site (or no site number supplied) AND (P.Is_Fully_Allocated = 'N') --not fully allocated (regardless of Available flag) AND (P.Tran_No_If_Deposit IS NULL) --not linked to an Incomplete transaction as a Deposit AND (M.Include_On_Cash_Summary = 'Y') --cash summary-type pay method (not Account etc.) AND (P.Payment_Type <> 'R') --not a Refund (linked or otherwise) AND (P.Refund_Payment_No IS NULL) --has not been refunded AND (P.Reference_Payment_No IS NULL) --is not a reversal AND (Rev.Payment_No IS NULL) --has not been reversed ORDER BY P.Payment_Date, P.Payment_Time, P.Payment_No; END; PASSTHROUGH STOP; END IF; END IF; /*********************************************************************/ /* 07/03/2007 - Update stored procedures following removal of Is_Deposit */ /* flag from WM_Payment */ /* VisualFrame_PaymentGrid has been modified to use NOLOCK table hint */ /* to prevent blocking from occurring while transaction in progress */ /*********************************************************************/ IF EXISTS( SELECT 1 FROM "DBA".SM_Version_Control WHERE Entity_Name = 'VisualFrame_PaymentGrid' AND Database_ID = (SELECT db_property('GlobalDbID')) AND Version_No < 5) THEN ALTER PROCEDURE "DBA".VisualFrame_PaymentGrid(IN @TranNo BIGINT, IN @VehCode Entity_Code, IN @CustNo Entity_No, IN @SiteNo Entity_No) RESULT (Payment_No Large_Entity_No, Payment_Date DATE, Payment_Time TIME, Operator Operator_Code, Method Entity_Name, Cheque_No Cheque_No, Amount_Paid NUMERIC(8,2), Amount_Available NUMERIC(8,2), Transaction_No Large_Entity_No, Is_Fully_Allocated Boolean) BEGIN SELECT P.Payment_No, P.Payment_Date, P.Payment_Time, P.Operator, M.Method, P.Cheque_No, P.Amount_Paid, (P.Amount_Paid - P.Amount_Allocated) AS Amount_Available, NULL AS Transaction_No, P.Is_Fully_Allocated FROM "DBA".WM_Payment P WITH (NOLOCK) INNER JOIN "DBA".WM_Payment_Method M WITH (NOLOCK) on (P.Pay_Method_No = M.Pay_Method_No) WHERE ((P.Is_Fully_Allocated = 'N' AND P.Customer_No IS NULL AND P.Vehicle_Code = @VehCode) OR (P.Is_Fully_Allocated = 'N' AND P.Vehicle_Code IS NULL AND P.Customer_No = @CustNo) OR (P.Is_Fully_Allocated = 'N' AND P.Vehicle_Code = @VehCode AND P.Customer_No = @CustNo)) AND ((Locked_By IS NULL) OR (Locked_By = @SiteNo) OR (Lock_Expiry_Time <= CURRENT TIMESTAMP)) AND (P.Available = 'Y') UNION SELECT P.Payment_No, P.Payment_Date, P.Payment_Time, P.Operator, M.Method, P.Cheque_No, P.Amount_Paid, (P.Amount_Paid - P.Amount_Allocated) AS Amount_Available, T.Transaction_No, P.Is_Fully_Allocated FROM "DBA".WM_Transaction_Payment T WITH (NOLOCK) INNER JOIN "DBA".WM_Payment P WITH (NOLOCK) on (T.Payment_No = P.Payment_No) INNER JOIN "DBA".WM_Payment_Method M WITH (NOLOCK) on (P.Pay_Method_No = M.Pay_Method_No) WHERE (T.Transaction_No = @TranNo) AND (P.Is_Fully_Allocated = 'Y'); END; --Passthrough the update to the remote sites (if in a replicated environment) IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; ALTER PROCEDURE "DBA".VisualFrame_PaymentGrid(IN @TranNo BIGINT, IN @VehCode Entity_Code, IN @CustNo Entity_No, IN @SiteNo Entity_No) RESULT (Payment_No Large_Entity_No, Payment_Date DATE, Payment_Time TIME, Operator Operator_Code, Method Entity_Name, Cheque_No Cheque_No, Amount_Paid NUMERIC(8,2), Amount_Available NUMERIC(8,2), Transaction_No Large_Entity_No, Is_Fully_Allocated Boolean) BEGIN SELECT P.Payment_No, P.Payment_Date, P.Payment_Time, P.Operator, M.Method, P.Cheque_No, P.Amount_Paid, (P.Amount_Paid - P.Amount_Allocated) AS Amount_Available, NULL AS Transaction_No, P.Is_Fully_Allocated FROM "DBA".WM_Payment P WITH (NOLOCK) INNER JOIN "DBA".WM_Payment_Method M WITH (NOLOCK) on (P.Pay_Method_No = M.Pay_Method_No) WHERE ((P.Is_Fully_Allocated = 'N' AND P.Customer_No IS NULL AND P.Vehicle_Code = @VehCode) OR (P.Is_Fully_Allocated = 'N' AND P.Vehicle_Code IS NULL AND P.Customer_No = @CustNo) OR (P.Is_Fully_Allocated = 'N' AND P.Vehicle_Code = @VehCode AND P.Customer_No = @CustNo)) AND ((Locked_By IS NULL) OR (Locked_By = @SiteNo) OR (Lock_Expiry_Time <= CURRENT TIMESTAMP)) AND (P.Available = 'Y') UNION SELECT P.Payment_No, P.Payment_Date, P.Payment_Time, P.Operator, M.Method, P.Cheque_No, P.Amount_Paid, (P.Amount_Paid - P.Amount_Allocated) AS Amount_Available, T.Transaction_No, P.Is_Fully_Allocated FROM "DBA".WM_Transaction_Payment T WITH (NOLOCK) INNER JOIN "DBA".WM_Payment P WITH (NOLOCK) on (T.Payment_No = P.Payment_No) INNER JOIN "DBA".WM_Payment_Method M WITH (NOLOCK) on (P.Pay_Method_No = M.Pay_Method_No) WHERE (T.Transaction_No = @TranNo) AND (P.Is_Fully_Allocated = 'Y'); END; --Update remote version control UPDATE "DBA".SM_Version_Control SET Version_No = 5 WHERE Entity_Name = 'VisualFrame_PaymentGrid' AND Database_ID = (SELECT db_property('GlobalDbID')); PASSTHROUGH STOP; END IF; --Update consolidated version control UPDATE "DBA".SM_Version_Control SET Version_No = 5 WHERE Entity_Name = 'VisualFrame_PaymentGrid' AND Database_ID = (SELECT db_property('GlobalDbID')); END IF; /*********************************************************************/ /* 07/03/2007 - Add stored procedures required for Temp Accounts */ /*********************************************************************/ IF NOT EXISTS( SELECT 1 FROM sysprocedure WHERE proc_name = 'WM_BaseFrame_PrintTempAccHdr') THEN CREATE PROCEDURE "DBA".WM_BaseFrame_PrintTempAccHdr(IN @TempAccountNo Entity_No) RESULT( Temp_Account_No Entity_No, Site_No Entity_No, Site_Code Entity_Code, Operator_Reference VARCHAR(40), DateTime_Created Entity_DateTime, Memo Entity_Comment_Text, Credit_Limit NUMERIC(8,2), Expiry_Date Entity_Date, Vehicle_Code Entity_Code, Customer_No Entity_No, Customer_Code Entity_Code, Temp_Customer_Name Entity_Name, Temp_Customer_ABN ABN, Temp_Customer_Address1 Entity_Address, Temp_Customer_Address2 Entity_Address, Temp_Customer_Address3 Entity_Address, Temp_Customer_State Australian_State, Temp_Customer_Postcode Postcode, Temp_Cheque_No Cheque_No, Temp_Cheque_Account Cheque_Account, Temp_Cheque_Drawer Cheque_Drawer, Temp_Cheque_Drawer_License Cheque_Drawer_License, Temp_Cheque_Bank Cheque_Bank, Temp_Cheque_BSB Cheque_BSB) BEGIN SELECT TA.Temp_Account_No, TA.Site_No, S.Site_Code AS Site_Code, TA.Operator_Reference, TA.DateTime_Created, TA.Memo, TA.Credit_Limit, TA.Expiry_Date, TA.Vehicle_Code, C.Customer_No, C.Code AS Customer_Code, TA.Customer_Name AS Temp_Customer_Name, TA.Customer_ABN AS Temp_Customer_ABN, TA.Customer_Address1 AS Temp_Customer_Address1, TA.Customer_Address2 AS Temp_Customer_Address2, TA.Customer_Address3 AS Temp_Customer_Address3, TA.Customer_State AS Temp_Customer_State, TA.Customer_Postcode AS Temp_Customer_Postcode, TA.Cheque_No AS Temp_Cheque_No, TA.Cheque_Account AS Temp_Cheque_Account, TA.Cheque_Drawer AS Temp_Cheque_Drawer, TA.Cheque_Drawer_License AS Temp_Cheque_Drawer_License, TA.Cheque_Bank AS Temp_Cheque_Bank, TA.Cheque_BSB AS Temp_Cheque_BSB FROM WM_Temp_Account TA INNER JOIN WM_Customer C ON (C.Customer_No = TA.Customer_No) INNER JOIN SM_Site S ON (S.Site_No = TA.Site_No) WHERE TA.Temp_Account_No = @TempAccountNo; END; --Passthrough the update to the remote sites (if in a replicated environment) IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; CREATE PROCEDURE "DBA".WM_BaseFrame_PrintTempAccHdr(IN @TempAccountNo Entity_No) RESULT( Temp_Account_No Entity_No, Site_No Entity_No, Site_Code Entity_Code, Operator_Reference VARCHAR(40), DateTime_Created Entity_DateTime, Memo Entity_Comment_Text, Credit_Limit NUMERIC(8,2), Expiry_Date Entity_Date, Vehicle_Code Entity_Code, Customer_No Entity_No, Customer_Code Entity_Code, Temp_Customer_Name Entity_Name, Temp_Customer_ABN ABN, Temp_Customer_Address1 Entity_Address, Temp_Customer_Address2 Entity_Address, Temp_Customer_Address3 Entity_Address, Temp_Customer_State Australian_State, Temp_Customer_Postcode Postcode, Temp_Cheque_No Cheque_No, Temp_Cheque_Account Cheque_Account, Temp_Cheque_Drawer Cheque_Drawer, Temp_Cheque_Drawer_License Cheque_Drawer_License, Temp_Cheque_Bank Cheque_Bank, Temp_Cheque_BSB Cheque_BSB) BEGIN SELECT TA.Temp_Account_No, TA.Site_No, S.Site_Code AS Site_Code, TA.Operator_Reference, TA.DateTime_Created, TA.Memo, TA.Credit_Limit, TA.Expiry_Date, TA.Vehicle_Code, C.Customer_No, C.Code AS Customer_Code, TA.Customer_Name AS Temp_Customer_Name, TA.Customer_ABN AS Temp_Customer_ABN, TA.Customer_Address1 AS Temp_Customer_Address1, TA.Customer_Address2 AS Temp_Customer_Address2, TA.Customer_Address3 AS Temp_Customer_Address3, TA.Customer_State AS Temp_Customer_State, TA.Customer_Postcode AS Temp_Customer_Postcode, TA.Cheque_No AS Temp_Cheque_No, TA.Cheque_Account AS Temp_Cheque_Account, TA.Cheque_Drawer AS Temp_Cheque_Drawer, TA.Cheque_Drawer_License AS Temp_Cheque_Drawer_License, TA.Cheque_Bank AS Temp_Cheque_Bank, TA.Cheque_BSB AS Temp_Cheque_BSB FROM WM_Temp_Account TA INNER JOIN WM_Customer C ON (C.Customer_No = TA.Customer_No) INNER JOIN SM_Site S ON (S.Site_No = TA.Site_No) WHERE TA.Temp_Account_No = @TempAccountNo; END; PASSTHROUGH STOP; END IF; END IF; IF NOT EXISTS( SELECT 1 FROM sysprocedure WHERE proc_name = 'WM_BaseFrame_PrintTempAccItem') THEN CREATE PROCEDURE "DBA".WM_BaseFrame_PrintTempAccItem(IN @TempAccountNo Entity_No) RESULT( Payment_Date Entity_Date, Payment_Time Entity_Time, Amount_Paid NUMERIC(8,2), Amount_Allocated NUMERIC(8,2), Vehicle_Code Entity_Code, Docket VARCHAR(20), Product Entity_Name, Operator Operator_Code, Memo Entity_Comment) BEGIN SELECT P.Payment_Date, P.Payment_Time, P.Amount_Paid, P.Amount_Allocated, P.Vehicle_Code, H.Docket, PR.Name AS Product, P.Operator, P.Memo FROM WM_Payment P LEFT OUTER JOIN WM_Transaction_Payment TP ON (TP.Payment_No = P.Payment_No) LEFT OUTER JOIN WM_Transaction_Header H ON (H.Transaction_No = TP.Transaction_No) LEFT OUTER JOIN WM_Transaction_Item I ON (I.Transaction_No = H.Transaction_No) LEFT OUTER JOIN WM_Product PR ON (PR.Product_No = I.Product_No) WHERE P.Temp_Account_No = @TempAccountNo AND P.Reference_Payment_No IS NULL -- i.e. Not a Reversal AND NOT EXISTS(SELECT 1 FROM WM_Payment P1 WHERE P1.Reference_Payment_No = P.Payment_No) -- i.e. Has not been reversed ORDER BY P.Payment_Date, P.Payment_Time, P.Payment_No; END; --Passthrough the update to the remote sites (if in a replicated environment) IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; CREATE PROCEDURE "DBA".WM_BaseFrame_PrintTempAccItem(IN @TempAccountNo Entity_No) RESULT( Payment_Date Entity_Date, Payment_Time Entity_Time, Amount_Paid NUMERIC(8,2), Amount_Allocated NUMERIC(8,2), Vehicle_Code Entity_Code, Docket VARCHAR(20), Product Entity_Name, Operator Operator_Code, Memo Entity_Comment) BEGIN SELECT P.Payment_Date, P.Payment_Time, P.Amount_Paid, P.Amount_Allocated, P.Vehicle_Code, H.Docket, PR.Name AS Product, P.Operator, P.Memo FROM WM_Payment P LEFT OUTER JOIN WM_Transaction_Payment TP ON (TP.Payment_No = P.Payment_No) LEFT OUTER JOIN WM_Transaction_Header H ON (H.Transaction_No = TP.Transaction_No) LEFT OUTER JOIN WM_Transaction_Item I ON (I.Transaction_No = H.Transaction_No) LEFT OUTER JOIN WM_Product PR ON (PR.Product_No = I.Product_No) WHERE P.Temp_Account_No = @TempAccountNo AND P.Reference_Payment_No IS NULL -- i.e. Not a Reversal AND NOT EXISTS(SELECT 1 FROM WM_Payment P1 WHERE P1.Reference_Payment_No = P.Payment_No) -- i.e. Has not been reversed ORDER BY P.Payment_Date, P.Payment_Time, P.Payment_No; END; PASSTHROUGH STOP; END IF; END IF; IF NOT EXISTS( SELECT 1 FROM sysprocedure WHERE proc_name = 'WM_BaseFrame_PrintTempAccTotals') THEN CREATE PROCEDURE "DBA".WM_BaseFrame_PrintTempAccTotals(IN @TempAccountNo Entity_No) RESULT( Payment_Count INTEGER, Amount_Paid NUMERIC(8,2), Amount_Allocated NUMERIC(8,2)) BEGIN SELECT COUNT(P.Temp_Account_No) AS Payment_Count, SUM(P.Amount_Paid) AS Amount_Paid, SUM(P.Amount_Allocated) AS Amount_Allocated FROM WM_Payment P LEFT OUTER JOIN WM_Transaction_Payment TP ON (TP.Payment_No = P.Payment_No) WHERE P.Temp_Account_No = @TempAccountNo AND P.Reference_Payment_No IS NULL -- i.e. Not a Reversal AND NOT EXISTS(SELECT 1 FROM WM_Payment P1 WHERE P1.Reference_Payment_No = P.Payment_No) -- i.e. Has not been reversed GROUP BY P.Temp_Account_No; END; --Passthrough the update to the remote sites (if in a replicated environment) IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; CREATE PROCEDURE "DBA".WM_BaseFrame_PrintTempAccTotals(IN @TempAccountNo Entity_No) RESULT( Payment_Count INTEGER, Amount_Paid NUMERIC(8,2), Amount_Allocated NUMERIC(8,2)) BEGIN SELECT COUNT(P.Temp_Account_No) AS Payment_Count, SUM(P.Amount_Paid) AS Amount_Paid, SUM(P.Amount_Allocated) AS Amount_Allocated FROM WM_Payment P LEFT OUTER JOIN WM_Transaction_Payment TP ON (TP.Payment_No = P.Payment_No) WHERE P.Temp_Account_No = @TempAccountNo AND P.Reference_Payment_No IS NULL -- i.e. Not a Reversal AND NOT EXISTS(SELECT 1 FROM WM_Payment P1 WHERE P1.Reference_Payment_No = P.Payment_No) -- i.e. Has not been reversed GROUP BY P.Temp_Account_No; END; PASSTHROUGH STOP; END IF; END IF; IF NOT EXISTS( SELECT 1 FROM sysprocedure WHERE proc_name = 'WM_BuildTempAccTranItemList') THEN CREATE PROCEDURE "DBA".WM_BuildTempAccTranItemList(IN @TempAccountNo Entity_No) BEGIN IF EXISTS( SELECT 1 FROM systable WHERE table_name = 'TempAccountTranItems') THEN DROP TABLE TempAccountTranItems; END IF; CREATE GLOBAL TEMPORARY TABLE TempAccountTranItems( Transaction_Item_No BIGINT PRIMARY KEY); INSERT TempAccountTranItems SELECT DISTINCT I.Transaction_Item_No --NOTE Transaction_Item_No is an IDENTITY column and so can be used to refer to the transaction item record FROM WM_Transaction_Item I INNER JOIN WM_Transaction_Header H ON (H.Transaction_No = I.Transaction_No) INNER JOIN WM_Transaction_Payment TP ON (TP.Transaction_No = H.Transaction_No) INNER JOIN WM_Payment P ON (P.Payment_No = TP.Payment_No) WHERE P.Temp_Account_No = @TempAccountNo; END; --Passthrough the update to the remote sites (if in a replicated environment) IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; CREATE PROCEDURE "DBA".WM_BuildTempAccTranItemList(IN @TempAccountNo Entity_No) BEGIN IF EXISTS( SELECT 1 FROM systable WHERE table_name = 'TempAccountTranItems') THEN DROP TABLE TempAccountTranItems; END IF; CREATE GLOBAL TEMPORARY TABLE TempAccountTranItems( Transaction_Item_No BIGINT PRIMARY KEY); INSERT TempAccountTranItems SELECT DISTINCT I.Transaction_Item_No --NOTE Transaction_Item_No is an IDENTITY column and so can be used to refer to the transaction item record FROM WM_Transaction_Item I INNER JOIN WM_Transaction_Header H ON (H.Transaction_No = I.Transaction_No) INNER JOIN WM_Transaction_Payment TP ON (TP.Transaction_No = H.Transaction_No) INNER JOIN WM_Payment P ON (P.Payment_No = TP.Payment_No) WHERE P.Temp_Account_No = @TempAccountNo; END; PASSTHROUGH STOP; END IF; END IF; IF NOT EXISTS( SELECT 1 FROM sysprocedure WHERE proc_name = 'WM_VisualFrame_TempAccountGrid') THEN CREATE PROCEDURE "DBA".WM_VisualFrame_TempAccountGrid(IN @VehCode Entity_Code, IN @CustomerNo Entity_No, IN @SiteNo Entity_No) RESULT( Temp_Account_No Entity_No, Ref_No VARCHAR(40), Created Entity_DateTime, Payments INTEGER, Total NUMERIC(8,2)) BEGIN SELECT TA.Temp_Account_No, TA.Operator_Reference AS Ref_No, TA.DateTime_Created AS Created, COUNT(P.Payment_No) AS Payments, SUM(P.Amount_Paid) AS Total FROM WM_Payment P INNER JOIN WM_Temp_Account TA ON (TA.Temp_Account_No = P.Temp_Account_No) INNER JOIN WM_Customer C ON (C.Customer_No = TA.Customer_No) WHERE TA.Customer_No = @CustomerNo AND TA.Site_No = @SiteNo AND ((C.Is_Debtor = 'Y') OR ((C.Is_Debtor = 'N') AND (P.Vehicle_Code = @VehCode))) AND P.Reference_Payment_No IS NULL -- i.e. Not a Reversal AND NOT EXISTS(SELECT 1 FROM WM_Payment P1 WHERE P1.Reference_Payment_No = P.Payment_No) -- i.e. Has not been reversed GROUP BY TA.Temp_Account_No, TA.Operator_Reference, TA.DateTime_Created; END; --Passthrough the update to the remote sites (if in a replicated environment) IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; CREATE PROCEDURE "DBA".WM_VisualFrame_TempAccountGrid(IN @VehCode Entity_Code, IN @CustomerNo Entity_No, IN @SiteNo Entity_No) RESULT( Temp_Account_No Entity_No, Ref_No VARCHAR(40), Created Entity_DateTime, Payments INTEGER, Total NUMERIC(8,2)) BEGIN SELECT TA.Temp_Account_No, TA.Operator_Reference AS Ref_No, TA.DateTime_Created AS Created, COUNT(P.Payment_No) AS Payments, SUM(P.Amount_Paid) AS Total FROM WM_Payment P INNER JOIN WM_Temp_Account TA ON (TA.Temp_Account_No = P.Temp_Account_No) INNER JOIN WM_Customer C ON (C.Customer_No = TA.Customer_No) WHERE TA.Customer_No = @CustomerNo AND TA.Site_No = @SiteNo AND ((C.Is_Debtor = 'Y') OR ((C.Is_Debtor = 'N') AND (P.Vehicle_Code = @VehCode))) AND P.Reference_Payment_No IS NULL -- i.e. Not a Reversal AND NOT EXISTS(SELECT 1 FROM WM_Payment P1 WHERE P1.Reference_Payment_No = P.Payment_No) -- i.e. Has not been reversed GROUP BY TA.Temp_Account_No, TA.Operator_Reference, TA.DateTime_Created; END; PASSTHROUGH STOP; END IF; END IF; /*********************************************************************/ /* 07/03/2007 - Add views required for Temp Accounts */ /*********************************************************************/ IF NOT EXISTS( SELECT 1 FROM systable WHERE table_name = 'Default_View_0011' AND table_type = 'VIEW') THEN CREATE VIEW "DBA".Default_View_0011 AS SELECT Code, "Name", Alias, Delivery_Address1 AS Address_1, Delivery_Address2 AS Address_2, Delivery_Address3 AS Address_3, Delivery_State AS State, Delivery_Postcode AS Postcode, Customer_No FROM "DBA".WM_Customer WHERE Available = 'Y' AND RTRIM(CAST(Customer_No AS CHAR)) IN ( --NOTE: User is not permitted to modify the authorisation parameters so they can be assumed to be constant --Is_Authorising_Relationship = 'Y', Items_Must_Exist = 'Y' SELECT AI.Authorising_Class_Key FROM "DBA".SM_Authorisation_Item AI INNER JOIN "DBA".WM_Payment_Method M ON (RTRIM(CAST(M.Pay_Method_No AS CHAR)) = AI.Authorised_Class_Key) INNER JOIN "DBA".SM_Authorisation_Config AC ON (AC.Authorisation_Config_No = AI.Authorisation_Config_No) WHERE (AC.Authorising_Class_ID = 3001) --Customer AND (AC.Authorised_Class_ID = 3009) --Payment Method AND (AC.Authorised_Process = 3001) --Payment process AND (AC.Is_Enabled = 'Y') --Relationship Enabled AND (M.Prompt = 'O') --Payment Method is Temp Account prompt type ); --Passthrough the update to the remote sites (if in a replicated environment) IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; CREATE VIEW "DBA".Default_View_0011 AS SELECT Code, "Name", Alias, Delivery_Address1 AS Address_1, Delivery_Address2 AS Address_2, Delivery_Address3 AS Address_3, Delivery_State AS State, Delivery_Postcode AS Postcode, Customer_No FROM "DBA".WM_Customer WHERE Available = 'Y' AND RTRIM(CAST(Customer_No AS CHAR)) IN ( --NOTE: User is not permitted to modify the authorisation parameters so they can be assumed to be constant --Is_Authorising_Relationship = 'Y', Items_Must_Exist = 'Y' SELECT AI.Authorising_Class_Key FROM "DBA".SM_Authorisation_Item AI INNER JOIN "DBA".WM_Payment_Method M ON (RTRIM(CAST(M.Pay_Method_No AS CHAR)) = AI.Authorised_Class_Key) INNER JOIN "DBA".SM_Authorisation_Config AC ON (AC.Authorisation_Config_No = AI.Authorisation_Config_No) WHERE (AC.Authorising_Class_ID = 3001) --Customer AND (AC.Authorised_Class_ID = 3009) --Payment Method AND (AC.Authorised_Process = 3001) --Payment process AND (AC.Is_Enabled = 'Y') --Relationship Enabled AND (M.Prompt = 'O') --Payment Method is Temp Account prompt type ); PASSTHROUGH STOP; END IF; END IF; IF NOT EXISTS( SELECT 1 FROM systable WHERE table_name = 'Default_View_0012' AND table_type = 'VIEW') THEN CREATE VIEW "DBA".Default_View_0012 AS SELECT V.Code, V.Alias, CA."Name" AS Carrier, VT."Description" AS Vehicle_Type, Tare = (CASE WHEN V.Use_Stored_Tare = 'Y' THEN V.Tare ELSE NULL END), Max_Gross = (CASE WHEN V.Gross_Max > 0 THEN V.Gross_Max ELSE NULL END), DC."Name" AS Default_Customer, VG."Name" AS Vehicle_Group, V.Vehicle_No FROM "DBA".WM_Vehicle V LEFT OUTER JOIN "DBA".WM_Carrier CA ON (CA.Carrier_No = V.Carrier_No) LEFT OUTER JOIN "DBA".WM_Customer DC ON (DC.Customer_No = V.Last_Customer) LEFT OUTER JOIN "DBA".WM_Vehicle_Group VG ON (VG.Vehicle_Group_No = V.Vehicle_Group_No) LEFT OUTER JOIN "DBA".SM_Vehicle_Type VT ON (VT.Vehicle_Type_No = V.Vehicle_Type_No) WHERE V.Available = 'Y'; --Passthrough the update to the remote sites (if in a replicated environment) IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; CREATE VIEW "DBA".Default_View_0012 AS SELECT V.Code, V.Alias, CA."Name" AS Carrier, VT."Description" AS Vehicle_Type, Tare = (CASE WHEN V.Use_Stored_Tare = 'Y' THEN V.Tare ELSE NULL END), Max_Gross = (CASE WHEN V.Gross_Max > 0 THEN V.Gross_Max ELSE NULL END), DC."Name" AS Default_Customer, VG."Name" AS Vehicle_Group, V.Vehicle_No FROM "DBA".WM_Vehicle V LEFT OUTER JOIN "DBA".WM_Carrier CA ON (CA.Carrier_No = V.Carrier_No) LEFT OUTER JOIN "DBA".WM_Customer DC ON (DC.Customer_No = V.Last_Customer) LEFT OUTER JOIN "DBA".WM_Vehicle_Group VG ON (VG.Vehicle_Group_No = V.Vehicle_Group_No) LEFT OUTER JOIN "DBA".SM_Vehicle_Type VT ON (VT.Vehicle_Type_No = V.Vehicle_Type_No) WHERE V.Available = 'Y'; PASSTHROUGH STOP; END IF; END IF; /*********************************************************************/ /* 07/03/2007 - Add / modify stored procedures required for enhanced payment processing */ /*********************************************************************/ IF NOT EXISTS( SELECT 1 FROM sysprocedure WHERE proc_name = 'WM_AvailablePayments') THEN CREATE PROCEDURE "DBA".WM_AvailablePayments(IN @VehCode Entity_Code, IN @CustNo Entity_No, IN @SiteNo Entity_No, IN @SiteWorkstationNo Entity_No, IN @WorkstationScreenName VARCHAR(50), IN @SpecificPayments VARCHAR(50)) RESULT( Row_No INTEGER, Payment_No BIGINT, Pay_Method_No INTEGER, Pay_Method_Desc VARCHAR(50), Customer_No INTEGER, Operator VARCHAR(20), Site_No INTEGER, Workstation_No INTEGER, Shift_No INTEGER, Payment_Type CHAR(1), Vehicle_Code VARCHAR(32), Reference_Payment_No BIGINT, Temp_Account_No INTEGER, Payment_Date TIMESTAMP, Payment_Time TIMESTAMP, Amount_Paid NUMERIC(8,2), Amount_Allocated NUMERIC(8,2), Amount_Available NUMERIC(8,2), Is_Fully_Allocated CHAR(1), Workstation_Screen_Name VARCHAR(50)) BEGIN DECLARE @ProcRes INTEGER; DECLARE @ErrMsg VARCHAR(80); DECLARE @NumsTemp VARCHAR(500); --Create table to hold result set CREATE TABLE #PaymentTable( Row_No INTEGER DEFAULT AUTOINCREMENT PRIMARY KEY, Payment_No BIGINT, Pay_Method_No INTEGER, Pay_Method_Desc VARCHAR(50), Customer_No INTEGER, Operator VARCHAR(20), Site_No INTEGER, Workstation_No INTEGER, Shift_No INTEGER, Payment_Type CHAR(1), Vehicle_Code VARCHAR(32), Reference_Payment_No BIGINT, Temp_Account_No INTEGER, Payment_Date TIMESTAMP, Payment_Time TIMESTAMP, Amount_Paid NUMERIC(8,2), Amount_Allocated NUMERIC(8,2), Amount_Available NUMERIC(8,2), Is_Fully_Allocated CHAR(1), Workstation_Screen_Name VARCHAR(50)); /* Give preference to the specific payments if supplied. This is usually either: (1) a list of payments previously used to pay off the transaction. These will exist if the transaction is an edited transaction (2) one or more deposit payments entered at the start of a transaction and needing to be allocated to the transaction The usual criteria re Customer and Vehicle is used. Payments that specify a customer and vehicle appear in the list before those that do not. The oldest payments are returned before the more recent */ IF LTRIM(@SpecificPayments) <> '' THEN --Create a table containing the payment numbers supplied CREATE TABLE #PaymentList(Payment_No BIGINT); SET @NumsTemp = @SpecificPayments; WHILE LTRIM(@NumsTemp) <> '' LOOP IF CHARINDEX(',', @NumsTemp) > 0 THEN INSERT #PaymentList(Payment_No) VALUES(CONVERT(BIGINT, LTRIM(LEFT(@NumsTemp, CHARINDEX(',', @NumsTemp)-1)))); SET @NumsTemp = LTRIM(STUFF(@NumsTemp, 1, CHARINDEX(',', @NumsTemp), NULL)); ELSE INSERT #PaymentList(Payment_No) VALUES(CONVERT(BIGINT, LTRIM(@NumsTemp))); SET @NumsTemp = ''; END IF; END LOOP; INSERT #PaymentTable (Payment_No, Pay_Method_No, Pay_Method_Desc, Customer_No, Operator, Site_No, Workstation_No, Shift_No, Payment_Type, Vehicle_Code, Reference_Payment_No, Temp_Account_No, Payment_Date, Payment_Time, Amount_Paid, Amount_Allocated, Amount_Available, Is_Fully_Allocated, Workstation_Screen_Name) SELECT P.Payment_No, P.Pay_Method_No, M.Method, P.Customer_No, P.Operator, P.Site_No, P.Site_Workstation_No, P.Shift_No, P.Payment_Type, P.Vehicle_Code, P.Reference_Payment_No, P.Temp_Account_No, P.Payment_Date, P.Payment_Time, P.Amount_Paid, P.Amount_Allocated, P.Amount_Paid - P.Amount_Allocated, P.Is_Fully_Allocated, P.Workstation_Screen_Name FROM WM_Payment P INNER JOIN WM_Payment_Method M ON (M.Pay_Method_No = P.Pay_Method_No) --WHERE payment is not fully allocated WHERE P.Is_Fully_Allocated = 'N' --AND payment is not assigned to a customer or truck AND ((P.Customer_No IS NULL AND P.Vehicle_Code Is NULL) --OR payment not assigned to customer but is assigned to truck } OR (P.Customer_No IS NULL AND P.Vehicle_Code = @VehCode) --OR payment not assigned to truck but is assigned to customer } OR (P.Vehicle_Code IS NULL AND P.Customer_No = @CustNo) --OR payment is assigned to both truck and customer } OR (P.Vehicle_Code = @VehCode AND P.Customer_No = @CustNo)) --AND in the payment list nominated AND P.Payment_No IN (SELECT Payment_No FROM #PaymentList) /*Payment Order: (1) Vehicle Code non-NULL & Customer No non-NULL (oldest first, non-advance payments before advance payments) (2) Vehicle Code non-NULL & Customer No NULL (oldest first, non-advance payments before advance payments) (3) Vehicle Code NULL & Customer No non-NULL (oldest first, non-advance payments before advance payments) (4) Vehicle Code & Customer No both NULL (oldest first, non-advance payments before advance payments) they will sort to the end, but in ORDER BY ... ASC, they will sort to the BEGINNING */ ORDER BY P.Vehicle_Code DESC, P.Customer_No DESC, (CASE P.Payment_Type WHEN 'A' THEN 2 ELSE 1 END), P.Payment_Date, P.Payment_Time, P.Payment_No; END IF; --Normal result set --First get all available payments for the workstation and tab name INSERT #PaymentTable (Payment_No, Pay_Method_No, Pay_Method_Desc, Customer_No, Operator, Site_No, Workstation_No, Shift_No, Payment_Type, Vehicle_Code, Reference_Payment_No, Temp_Account_No, Payment_Date, Payment_Time, Amount_Paid, Amount_Allocated, Amount_Available, Is_Fully_Allocated, Workstation_Screen_Name) SELECT P.Payment_No, P.Pay_Method_No, M.Method, P.Customer_No, P.Operator, P.Site_No, P.Site_Workstation_No, P.Shift_No, P.Payment_Type, P.Vehicle_Code, P.Reference_Payment_No, P.Temp_Account_No, P.Payment_Date, P.Payment_Time, P.Amount_Paid, P.Amount_Allocated, P.Amount_Paid - P.Amount_Allocated, P.Is_Fully_Allocated, P.Workstation_Screen_Name FROM WM_Payment P INNER JOIN WM_Payment_Method M ON (M.Pay_Method_No = P.Pay_Method_No) --WHERE payment is not fully allocated WHERE P.Is_Fully_Allocated = 'N' --AND payment is Available AND P.Available = 'Y' --AND payment is NOT a deposit already allocated to a transaction AND P.Tran_No_If_Deposit IS NULL --AND payment is not assigned to a customer or truck AND ((P.Customer_No IS NULL AND P.Vehicle_Code Is NULL) --OR payment not assigned to customer but is assigned to truck } OR (P.Customer_No IS NULL AND P.Vehicle_Code = @VehCode) --OR payment not assigned to truck but is assigned to customer } OR (P.Vehicle_Code IS NULL AND P.Customer_No = @CustNo) --OR payment is assigned to both truck and customer } OR (P.Vehicle_Code = @VehCode AND P.Customer_No = @CustNo)) --AND belongs to the site specified (if specified) AND ((P.Site_No = @SiteNo) OR (@SiteNo IS NULL)) --AND payment belongs to this workstation and screen (tab) name AND P.Site_Workstation_No = @SiteWorkstationNo AND P.Workstation_Screen_Name = @WorkstationScreenName --AND payment not already added to list AND P.Payment_No NOT IN (SELECT Payment_No FROM #PaymentTable) --Get the most-recently-added payments first, non-advance payments before advance payments ORDER BY (CASE P.Payment_Type WHEN 'A' THEN 2 ELSE 1 END), P.Payment_Date DESC, P.Payment_Time DESC; --Now get all available payments NOT entered at the weigh station and tab INSERT #PaymentTable (Payment_No, Pay_Method_No, Pay_Method_Desc, Customer_No, Operator, Site_No, Workstation_No, Shift_No, Payment_Type, Vehicle_Code, Reference_Payment_No, Temp_Account_No, Payment_Date, Payment_Time, Amount_Paid, Amount_Allocated, Amount_Available, Is_Fully_Allocated, Workstation_Screen_Name) SELECT P.Payment_No, P.Pay_Method_No, M.Method, P.Customer_No, P.Operator, P.Site_No, P.Site_Workstation_No, P.Shift_No, P.Payment_Type, P.Vehicle_Code, P.Reference_Payment_No, P.Temp_Account_No, P.Payment_Date, P.Payment_Time, P.Amount_Paid, P.Amount_Allocated, P.Amount_Paid - P.Amount_Allocated, P.Is_Fully_Allocated, P.Workstation_Screen_Name FROM WM_Payment P INNER JOIN WM_Payment_Method M ON (M.Pay_Method_No = P.Pay_Method_No) LEFT OUTER JOIN WM_Customer C ON (C.Customer_No = P.Customer_No) --WHERE payment is not fully allocated WHERE P.Is_Fully_Allocated = 'N' --AND payment is Available AND P.Available = 'Y' --AND payment is NOT a deposit already allocated to a transaction AND P.Tran_No_If_Deposit IS NULL --AND payment is not assigned to a customer or truck AND ((P.Customer_No IS NULL AND P.Vehicle_Code Is NULL) --OR payment not assigned to customer but is assigned to truck } OR (P.Customer_No IS NULL AND P.Vehicle_Code = @VehCode) --OR payment not assigned to truck but is assigned to customer } OR (P.Vehicle_Code IS NULL AND P.Customer_No = @CustNo) --OR payment is assigned to both truck and customer } OR (P.Vehicle_Code = @VehCode AND P.Customer_No = @CustNo)) --AND belongs to the site specified (if specified) AND ((P.Site_No = @SiteNo) OR (@SiteNo IS NULL)) --AND payment does NOT belong to this weigh station and screen (tab) name AND ((P.Site_Workstation_No <> @SiteWorkstationNo) OR (P.Workstation_Screen_Name <> @WorkstationScreenName) OR (P.Workstation_Screen_Name IS NULL)) --AND payment not already added to list AND P.Payment_No NOT IN (SELECT Payment_No FROM #PaymentTable) /*Payment Order: (1) Vehicle Code & Customer No both NULL (oldest first) (2) Vehicle Code NULL & Customer No non-NULL (oldest first) (3) Vehicle Code non-NULL & Customer No NULL (oldest first) (4) Vehicle Code non-NULL & Customer No non-NULL (oldest first) they will sort to the beginning, but in ORDER BY ... DESC, they will sort to the END */ ORDER BY P.Vehicle_Code, P.Customer_No, (CASE P.Payment_Type WHEN 'A' THEN 2 ELSE 1 END), P.Payment_Date, P.Payment_Time, P.Payment_No; --Return results SELECT * FROM #PaymentTable ORDER BY Row_No; END; --Passthrough the update to the remote sites (if in a replicated environment) IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; CREATE PROCEDURE "DBA".WM_AvailablePayments(IN @VehCode Entity_Code, IN @CustNo Entity_No, IN @SiteNo Entity_No, IN @SiteWorkstationNo Entity_No, IN @WorkstationScreenName VARCHAR(50), IN @SpecificPayments VARCHAR(50)) RESULT( Row_No INTEGER, Payment_No BIGINT, Pay_Method_No INTEGER, Pay_Method_Desc VARCHAR(50), Customer_No INTEGER, Operator VARCHAR(20), Site_No INTEGER, Workstation_No INTEGER, Shift_No INTEGER, Payment_Type CHAR(1), Vehicle_Code VARCHAR(32), Reference_Payment_No BIGINT, Temp_Account_No INTEGER, Payment_Date TIMESTAMP, Payment_Time TIMESTAMP, Amount_Paid NUMERIC(8,2), Amount_Allocated NUMERIC(8,2), Amount_Available NUMERIC(8,2), Is_Fully_Allocated CHAR(1), Workstation_Screen_Name VARCHAR(50)) BEGIN DECLARE @ProcRes INTEGER; DECLARE @ErrMsg VARCHAR(80); DECLARE @NumsTemp VARCHAR(500); --Create table to hold result set CREATE TABLE #PaymentTable( Row_No INTEGER DEFAULT AUTOINCREMENT PRIMARY KEY, Payment_No BIGINT, Pay_Method_No INTEGER, Pay_Method_Desc VARCHAR(50), Customer_No INTEGER, Operator VARCHAR(20), Site_No INTEGER, Workstation_No INTEGER, Shift_No INTEGER, Payment_Type CHAR(1), Vehicle_Code VARCHAR(32), Reference_Payment_No BIGINT, Temp_Account_No INTEGER, Payment_Date TIMESTAMP, Payment_Time TIMESTAMP, Amount_Paid NUMERIC(8,2), Amount_Allocated NUMERIC(8,2), Amount_Available NUMERIC(8,2), Is_Fully_Allocated CHAR(1), Workstation_Screen_Name VARCHAR(50)); /* Give preference to the specific payments if supplied. This is usually either: (1) a list of payments previously used to pay off the transaction. These will exist if the transaction is an edited transaction (2) one or more deposit payments entered at the start of a transaction and needing to be allocated to the transaction The usual criteria re Customer and Vehicle is used. Payments that specify a customer and vehicle appear in the list before those that do not. The oldest payments are returned before the more recent */ IF LTRIM(@SpecificPayments) <> '' THEN --Create a table containing the payment numbers supplied CREATE TABLE #PaymentList(Payment_No BIGINT); SET @NumsTemp = @SpecificPayments; WHILE LTRIM(@NumsTemp) <> '' LOOP IF CHARINDEX(',', @NumsTemp) > 0 THEN INSERT #PaymentList(Payment_No) VALUES(CONVERT(BIGINT, LTRIM(LEFT(@NumsTemp, CHARINDEX(',', @NumsTemp)-1)))); SET @NumsTemp = LTRIM(STUFF(@NumsTemp, 1, CHARINDEX(',', @NumsTemp), NULL)); ELSE INSERT #PaymentList(Payment_No) VALUES(CONVERT(BIGINT, LTRIM(@NumsTemp))); SET @NumsTemp = ''; END IF; END LOOP; INSERT #PaymentTable (Payment_No, Pay_Method_No, Pay_Method_Desc, Customer_No, Operator, Site_No, Workstation_No, Shift_No, Payment_Type, Vehicle_Code, Reference_Payment_No, Temp_Account_No, Payment_Date, Payment_Time, Amount_Paid, Amount_Allocated, Amount_Available, Is_Fully_Allocated, Workstation_Screen_Name) SELECT P.Payment_No, P.Pay_Method_No, M.Method, P.Customer_No, P.Operator, P.Site_No, P.Site_Workstation_No, P.Shift_No, P.Payment_Type, P.Vehicle_Code, P.Reference_Payment_No, P.Temp_Account_No, P.Payment_Date, P.Payment_Time, P.Amount_Paid, P.Amount_Allocated, P.Amount_Paid - P.Amount_Allocated, P.Is_Fully_Allocated, P.Workstation_Screen_Name FROM WM_Payment P INNER JOIN WM_Payment_Method M ON (M.Pay_Method_No = P.Pay_Method_No) --WHERE payment is not fully allocated WHERE P.Is_Fully_Allocated = 'N' --AND payment is not assigned to a customer or truck AND ((P.Customer_No IS NULL AND P.Vehicle_Code Is NULL) --OR payment not assigned to customer but is assigned to truck } OR (P.Customer_No IS NULL AND P.Vehicle_Code = @VehCode) --OR payment not assigned to truck but is assigned to customer } OR (P.Vehicle_Code IS NULL AND P.Customer_No = @CustNo) --OR payment is assigned to both truck and customer } OR (P.Vehicle_Code = @VehCode AND P.Customer_No = @CustNo)) --AND in the payment list nominated AND P.Payment_No IN (SELECT Payment_No FROM #PaymentList) /*Payment Order: (1) Vehicle Code non-NULL & Customer No non-NULL (oldest first, non-advance payments before advance payments) (2) Vehicle Code non-NULL & Customer No NULL (oldest first, non-advance payments before advance payments) (3) Vehicle Code NULL & Customer No non-NULL (oldest first, non-advance payments before advance payments) (4) Vehicle Code & Customer No both NULL (oldest first, non-advance payments before advance payments) they will sort to the end, but in ORDER BY ... ASC, they will sort to the BEGINNING */ ORDER BY P.Vehicle_Code DESC, P.Customer_No DESC, (CASE P.Payment_Type WHEN 'A' THEN 2 ELSE 1 END), P.Payment_Date, P.Payment_Time, P.Payment_No; END IF; --Normal result set --First get all available payments for the workstation and tab name INSERT #PaymentTable (Payment_No, Pay_Method_No, Pay_Method_Desc, Customer_No, Operator, Site_No, Workstation_No, Shift_No, Payment_Type, Vehicle_Code, Reference_Payment_No, Temp_Account_No, Payment_Date, Payment_Time, Amount_Paid, Amount_Allocated, Amount_Available, Is_Fully_Allocated, Workstation_Screen_Name) SELECT P.Payment_No, P.Pay_Method_No, M.Method, P.Customer_No, P.Operator, P.Site_No, P.Site_Workstation_No, P.Shift_No, P.Payment_Type, P.Vehicle_Code, P.Reference_Payment_No, P.Temp_Account_No, P.Payment_Date, P.Payment_Time, P.Amount_Paid, P.Amount_Allocated, P.Amount_Paid - P.Amount_Allocated, P.Is_Fully_Allocated, P.Workstation_Screen_Name FROM WM_Payment P INNER JOIN WM_Payment_Method M ON (M.Pay_Method_No = P.Pay_Method_No) --WHERE payment is not fully allocated WHERE P.Is_Fully_Allocated = 'N' --AND payment is Available AND P.Available = 'Y' --AND payment is NOT a deposit already allocated to a transaction AND P.Tran_No_If_Deposit IS NULL --AND payment is not assigned to a customer or truck AND ((P.Customer_No IS NULL AND P.Vehicle_Code Is NULL) --OR payment not assigned to customer but is assigned to truck } OR (P.Customer_No IS NULL AND P.Vehicle_Code = @VehCode) --OR payment not assigned to truck but is assigned to customer } OR (P.Vehicle_Code IS NULL AND P.Customer_No = @CustNo) --OR payment is assigned to both truck and customer } OR (P.Vehicle_Code = @VehCode AND P.Customer_No = @CustNo)) --AND belongs to the site specified (if specified) AND ((P.Site_No = @SiteNo) OR (@SiteNo IS NULL)) --AND payment belongs to this workstation and screen (tab) name AND P.Site_Workstation_No = @SiteWorkstationNo AND P.Workstation_Screen_Name = @WorkstationScreenName --AND payment not already added to list AND P.Payment_No NOT IN (SELECT Payment_No FROM #PaymentTable) --Get the most-recently-added payments first, non-advance payments before advance payments ORDER BY (CASE P.Payment_Type WHEN 'A' THEN 2 ELSE 1 END), P.Payment_Date DESC, P.Payment_Time DESC; --Now get all available payments NOT entered at the weigh station and tab INSERT #PaymentTable (Payment_No, Pay_Method_No, Pay_Method_Desc, Customer_No, Operator, Site_No, Workstation_No, Shift_No, Payment_Type, Vehicle_Code, Reference_Payment_No, Temp_Account_No, Payment_Date, Payment_Time, Amount_Paid, Amount_Allocated, Amount_Available, Is_Fully_Allocated, Workstation_Screen_Name) SELECT P.Payment_No, P.Pay_Method_No, M.Method, P.Customer_No, P.Operator, P.Site_No, P.Site_Workstation_No, P.Shift_No, P.Payment_Type, P.Vehicle_Code, P.Reference_Payment_No, P.Temp_Account_No, P.Payment_Date, P.Payment_Time, P.Amount_Paid, P.Amount_Allocated, P.Amount_Paid - P.Amount_Allocated, P.Is_Fully_Allocated, P.Workstation_Screen_Name FROM WM_Payment P INNER JOIN WM_Payment_Method M ON (M.Pay_Method_No = P.Pay_Method_No) LEFT OUTER JOIN WM_Customer C ON (C.Customer_No = P.Customer_No) --WHERE payment is not fully allocated WHERE P.Is_Fully_Allocated = 'N' --AND payment is Available AND P.Available = 'Y' --AND payment is NOT a deposit already allocated to a transaction AND P.Tran_No_If_Deposit IS NULL --AND payment is not assigned to a customer or truck AND ((P.Customer_No IS NULL AND P.Vehicle_Code Is NULL) --OR payment not assigned to customer but is assigned to truck } OR (P.Customer_No IS NULL AND P.Vehicle_Code = @VehCode) --OR payment not assigned to truck but is assigned to customer } OR (P.Vehicle_Code IS NULL AND P.Customer_No = @CustNo) --OR payment is assigned to both truck and customer } OR (P.Vehicle_Code = @VehCode AND P.Customer_No = @CustNo)) --AND belongs to the site specified (if specified) AND ((P.Site_No = @SiteNo) OR (@SiteNo IS NULL)) --AND payment does NOT belong to this weigh station and screen (tab) name AND ((P.Site_Workstation_No <> @SiteWorkstationNo) OR (P.Workstation_Screen_Name <> @WorkstationScreenName) OR (P.Workstation_Screen_Name IS NULL)) --AND payment not already added to list AND P.Payment_No NOT IN (SELECT Payment_No FROM #PaymentTable) /*Payment Order: (1) Vehicle Code & Customer No both NULL (oldest first) (2) Vehicle Code NULL & Customer No non-NULL (oldest first) (3) Vehicle Code non-NULL & Customer No NULL (oldest first) (4) Vehicle Code non-NULL & Customer No non-NULL (oldest first) they will sort to the beginning, but in ORDER BY ... DESC, they will sort to the END */ ORDER BY P.Vehicle_Code, P.Customer_No, (CASE P.Payment_Type WHEN 'A' THEN 2 ELSE 1 END), P.Payment_Date, P.Payment_Time, P.Payment_No; --Return results SELECT * FROM #PaymentTable ORDER BY Row_No; END; PASSTHROUGH STOP; END IF; END IF; IF NOT EXISTS( SELECT 1 FROM sysprocedure WHERE proc_name = 'WM_Underpayment_List_2') THEN CREATE PROCEDURE "DBA".WM_Underpayment_List_2(IN @CustNo Entity_No, IN @VehCode Entity_Code) BEGIN INSERT #ShortPayments (Transaction_No, Date_Complete, Time_Complete, Site, Site_Desc, Vehicle_Code, Outstanding, Docket, Product, Customer_No, Customer, Is_Debtor, ABN, Delivery_Address1, Delivery_Address2, Delivery_Address3, Delivery_State, Delivery_Postcode, Default_Pay_Method_No, Prod_Name) SELECT A.*, Prod.Name FROM ( SELECT H.Transaction_No, H.Date_Complete, H.Time_Complete, S.Site_Code AS Site, S.Description AS Site_Desc, H.Vehicle_Code, (H.Transaction_Amount - H.Amount_Paid) AS Outstanding, H.Docket, MIN(P.Code) AS Product, H.Customer_No, C.Name AS Customer, C.Is_Debtor, C.ABN, C.Delivery_Address1, C.Delivery_Address2, C.Delivery_Address3, C.Delivery_State, C.Delivery_Postcode, C.Default_Pay_Method_No FROM WM_Transaction_Header H INNER JOIN WM_Customer C ON (C.Customer_No = H.Customer_No) INNER JOIN SM_Site S ON (S.Site_No = H.Created_At) INNER JOIN WM_Transaction_Item I ON (I.Transaction_No = H.Transaction_No) INNER JOIN WM_Product P ON (P.Product_No = I.Product_No) WHERE (H.Is_Fully_Paid = 'N') AND (H.Is_Completed = 'Y') AND ((C.Is_Debtor = 'Y' AND H.Customer_No = @CustNo) OR (C.Is_Debtor = 'N' AND H.Customer_No = @CustNo AND H.Vehicle_Code = @VehCode)) GROUP BY H.Transaction_No, H.Date_Complete, H.Time_Complete, S.Site_Code, S.Description, H.Vehicle_Code, (H.Transaction_Amount - H.Amount_Paid), H.Docket, H.Customer_No, C.Name, C.Is_Debtor, C.ABN, C.Delivery_Address1, C.Delivery_Address2, C.Delivery_Address3, C.Delivery_State, C.Delivery_Postcode, C.Default_Pay_Method_No ORDER BY H.Date_Complete, H.Time_Complete, H.Transaction_No ) A INNER JOIN WM_Product Prod ON (Prod.Code = A.Product); END; --Passthrough the update to the remote sites (if in a replicated environment) IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; CREATE PROCEDURE "DBA".WM_Underpayment_List_2(IN @CustNo Entity_No, IN @VehCode Entity_Code) BEGIN INSERT #ShortPayments (Transaction_No, Date_Complete, Time_Complete, Site, Site_Desc, Vehicle_Code, Outstanding, Docket, Product, Customer_No, Customer, Is_Debtor, ABN, Delivery_Address1, Delivery_Address2, Delivery_Address3, Delivery_State, Delivery_Postcode, Default_Pay_Method_No, Prod_Name) SELECT A.*, Prod.Name FROM ( SELECT H.Transaction_No, H.Date_Complete, H.Time_Complete, S.Site_Code AS Site, S.Description AS Site_Desc, H.Vehicle_Code, (H.Transaction_Amount - H.Amount_Paid) AS Outstanding, H.Docket, MIN(P.Code) AS Product, H.Customer_No, C.Name AS Customer, C.Is_Debtor, C.ABN, C.Delivery_Address1, C.Delivery_Address2, C.Delivery_Address3, C.Delivery_State, C.Delivery_Postcode, C.Default_Pay_Method_No FROM WM_Transaction_Header H INNER JOIN WM_Customer C ON (C.Customer_No = H.Customer_No) INNER JOIN SM_Site S ON (S.Site_No = H.Created_At) INNER JOIN WM_Transaction_Item I ON (I.Transaction_No = H.Transaction_No) INNER JOIN WM_Product P ON (P.Product_No = I.Product_No) WHERE (H.Is_Fully_Paid = 'N') AND (H.Is_Completed = 'Y') AND ((C.Is_Debtor = 'Y' AND H.Customer_No = @CustNo) OR (C.Is_Debtor = 'N' AND H.Customer_No = @CustNo AND H.Vehicle_Code = @VehCode)) GROUP BY H.Transaction_No, H.Date_Complete, H.Time_Complete, S.Site_Code, S.Description, H.Vehicle_Code, (H.Transaction_Amount - H.Amount_Paid), H.Docket, H.Customer_No, C.Name, C.Is_Debtor, C.ABN, C.Delivery_Address1, C.Delivery_Address2, C.Delivery_Address3, C.Delivery_State, C.Delivery_Postcode, C.Default_Pay_Method_No ORDER BY H.Date_Complete, H.Time_Complete, H.Transaction_No ) A INNER JOIN WM_Product Prod ON (Prod.Code = A.Product); END; PASSTHROUGH STOP; END IF; END IF; /*********************************************************************/ /* 20/03/2007 - Passthrough standard module change to add stored procedure SM_GetNextEftposRef */ /*********************************************************************/ IF EXISTS( SELECT 1 FROM sysprocedure WHERE proc_name = 'SM_GetNextEftposRef') AND NOT EXISTS( SELECT 1 FROM "DBA".SM_Version_Control WHERE Entity_Name = 'PT-SM_GetNextEftposRef' AND Database_ID = (SELECT db_property('GlobalDbID'))) THEN --Passthrough the update to the remote sites (if in a replicated environment) IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; CREATE PROCEDURE "DBA".SM_GetNextEftposRef(IN @SiteNo Entity_No, IN @SiteCode Entity_Code, OUT @EftposRef VARCHAR(16), OUT @NextRefNo VARCHAR(8)) BEGIN ATOMIC DECLARE @localSiteNo Entity_No; DECLARE @refNo VARCHAR(8); IF (@SiteNo IS NULL) OR (ISNULL(@SiteCode, '') = '') THEN RAISERROR 50001 'Site Number and Code must be specified.'; RETURN; END IF; SET @localSiteNo = SM_GetLocalSiteNo(); --Get current ref no. or 1 if doesn't exist SET @refNo = ISNULL(SM_EntCfgGetItemValue(@SiteNo, NULL, 'SRV2023:Tran Ref', 'Next Tran Ref No'), 1); --Write @refNo + 1 back to enterprise config --SM_EntCfgSetItemValue proc will create the header and/or item if necessary IF ISNUMERIC(@refNo) = 1 THEN SET @refNo = TRUNCNUM(CAST(RTRIM(@refNo) AS FLOAT), 0); --remove decimals if returned SET @NextRefNo = RTRIM(CAST((CAST(@refNo AS INT) + 1) AS CHAR)); ELSE RAISERROR 50001 'Invalid value found for Next Tran Ref No: %1!', @refNo; RETURN; END IF; CALL SM_EntCfgSetItemValue(@SiteNo, NULL, 'SRV2023:Tran Ref', 'Next Tran Ref No', @NextRefNo, 2023, @localSiteNo); --Return the eftpos reference no. SET @EftposRef = LEFT(@SiteCode, 8) || REPLICATE('0', 8 - LENGTH(@refNo)) || @refNo; END; --Add version control rec (remote) INSERT "DBA".SM_Version_Control(Entity_Name, Database_ID, Version_No) VALUES('PT-SM_GetNextEftposRef', (SELECT db_property('GlobalDbID')), 1); PASSTHROUGH STOP; END IF; --Add version control rec (consolidated) INSERT "DBA".SM_Version_Control(Entity_Name, Database_ID, Version_No) VALUES('PT-SM_GetNextEftposRef', (SELECT db_property('GlobalDbID')), 1); END IF; /*********************************************************************/ /* 21/03/2007 - Update complete tran procedure to return the amount */ /* of transaction paid by advance payments */ /*********************************************************************/ IF NOT EXISTS( SELECT 1 FROM "DBA".SM_Version_Control WHERE Entity_Name = 'WM_BaseFrame_CompletedTran' AND Database_ID = (SELECT db_property('GlobalDbID'))) THEN ALTER PROCEDURE "DBA".WM_BaseFrame_CompletedTran(IN @TranNo Large_Entity_No, IN @ProductChargeRateNo Entity_No, IN @ChargeOverrideExceptionCode Entity_Code) RESULT( Transaction_No Large_Entity_No, Docket_No INTEGER, Docket_No_Prefix CHAR(3), Docket_No_Suffix CHAR(3), Docket_No_Version INTEGER, Vehicle_No Entity_No, Vehicle_Code Entity_Code, Customer_No Entity_No, Job_No Entity_No, Carrier_No Entity_No, Gross Weight, Gross_Date Entity_Date, Gross_Time Entity_Time, Tare Weight, Tare_Date Entity_Date, Tare_Time Entity_Time, Net Weight, Memo Entity_Comment, Date_Complete Entity_Date, Time_Complete Entity_Time, Created_At Entity_No, Shift_No Entity_No, Created_By Operator_Code, Is_Fully_Paid Boolean, Amount_Paid NUMERIC(8,2), Amount_Advance_Paid NUMERIC(8,2), Is_Stored_Tare Boolean, DateTime_In Entity_DateTime, DateTime_Out Entity_DateTime, Custom_Data1 Entity_Code, Order_No VARCHAR(30), Reversal_Tran_No Large_Entity_No, Original_Tran_No Large_Entity_No, Transaction_Item_No Large_Entity_No, Product_No Entity_No, Area_No Entity_No, Waste_Stream_No Entity_No, Sub_Stream_1_No Entity_No, SS1_Name Entity_Name, LGA_Code Entity_Code, Rate_Range_No Entity_No, Cost_Method Prod_Cost_Method, Cost_Rule_No Entity_No, Item_Charge NUMERIC(8,2), Quantity NUMERIC(19,6), Item_Net Weight, Item_CM NUMERIC(19,6), Sub_Stream_2_No Entity_No, Sub_Stream_3_No Entity_No, Has_Override Boolean, Override_User Operator_Code, Override_Comment Entity_Comment, Charge_Rate NUMERIC(10,4), Original_Rate_Range_No Entity_No ) BEGIN SELECT H.Transaction_No, H.Docket_No, H.Docket_No_Prefix, H.Docket_No_Suffix, H.Docket_No_Version, H.Vehicle_No, H.Vehicle_Code, H.Customer_No, H.Job_No, H.Carrier_No, H.Gross, H.Gross_Date, H.Gross_Time, H.Tare, H.Tare_Date, H.Tare_Time, H.Net, H.Memo, H.Date_Complete, H.Time_Complete, H.Created_At, H.Shift_No, H.Created_By, H.Is_Fully_Paid, H.Amount_Paid, ISNULL(C.Advance_Pay_Total, 0), H.Is_Stored_Tare, H.DateTime_In, H.DateTime_Out, H.Custom_Data1, H.Order_No, H.Reversal_Tran_No, H.Original_Tran_No, I.Transaction_Item_No, I.Product_No, I.Area_No, I.Waste_Stream_No, I.Sub_Stream_1_No, E.Name AS SS1_Name, I.LGA_Code, I.Rate_Range_No, I.Cost_Method, I.Cost_Rule_No, I.Item_Charge, I.Quantity, I.Net AS Item_Net, I.Cubic_Metres AS Item_CM, I.Sub_Stream_2_No, I.Sub_Stream_3_No, (CASE A.Code WHEN @ChargeOverrideExceptionCode THEN 'Y' ELSE 'N' END) AS Has_Override, A.Operator_Code AS Override_User, A.Exception_Comment AS Override_Comment, B.Rate AS Charge_Rate, I.Original_Rate_Range_No FROM WM_Transaction_Header H INNER JOIN WM_Transaction_Item I ON (I.Transaction_No = H.Transaction_No) LEFT OUTER JOIN WM_Waste_Sub_Stream_1 E ON (E.Sub_Stream_1_No = I.Sub_Stream_1_No) LEFT OUTER JOIN ( SELECT TEx.Transaction_No, Ex.Code, TEx.Operator_Code, TEx.Exception_Comment FROM WM_Transaction_Exception TEx INNER JOIN WM_Exceptions Ex ON (Ex.Exception_No = TEx.Exception_No) ) A ON (A.Transaction_No = H.Transaction_No) AND (A.Code = @ChargeOverrideExceptionCode) LEFT OUTER JOIN ( SELECT Transaction_No, Transaction_Item_No, Rate, Rate_No FROM WM_Transaction_Item_Rate ) B ON (B.Transaction_No = I.Transaction_No) AND (B.Transaction_Item_No = I.Transaction_Item_No) AND (B.Rate_No = @ProductChargeRateNo) LEFT OUTER JOIN ( SELECT TP.Transaction_No, SUM(TP.Amount) AS Advance_Pay_Total FROM WM_Transaction_Payment TP INNER JOIN WM_Payment P ON (P.Payment_No = TP.Payment_No) WHERE P.Payment_Type = 'A' GROUP BY TP.Transaction_No ) C ON (C.Transaction_No = H.Transaction_No) WHERE H.Transaction_No = @TranNo AND Is_Completed = 'Y'; END; --Passthrough the update to the remote sites (if in a replicated environment) IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; ALTER PROCEDURE "DBA".WM_BaseFrame_CompletedTran(IN @TranNo Large_Entity_No, IN @ProductChargeRateNo Entity_No, IN @ChargeOverrideExceptionCode Entity_Code) RESULT( Transaction_No Large_Entity_No, Docket_No INTEGER, Docket_No_Prefix CHAR(3), Docket_No_Suffix CHAR(3), Docket_No_Version INTEGER, Vehicle_No Entity_No, Vehicle_Code Entity_Code, Customer_No Entity_No, Job_No Entity_No, Carrier_No Entity_No, Gross Weight, Gross_Date Entity_Date, Gross_Time Entity_Time, Tare Weight, Tare_Date Entity_Date, Tare_Time Entity_Time, Net Weight, Memo Entity_Comment, Date_Complete Entity_Date, Time_Complete Entity_Time, Created_At Entity_No, Shift_No Entity_No, Created_By Operator_Code, Is_Fully_Paid Boolean, Amount_Paid NUMERIC(8,2), Amount_Advance_Paid NUMERIC(8,2), Is_Stored_Tare Boolean, DateTime_In Entity_DateTime, DateTime_Out Entity_DateTime, Custom_Data1 Entity_Code, Order_No VARCHAR(30), Reversal_Tran_No Large_Entity_No, Original_Tran_No Large_Entity_No, Transaction_Item_No Large_Entity_No, Product_No Entity_No, Area_No Entity_No, Waste_Stream_No Entity_No, Sub_Stream_1_No Entity_No, SS1_Name Entity_Name, LGA_Code Entity_Code, Rate_Range_No Entity_No, Cost_Method Prod_Cost_Method, Cost_Rule_No Entity_No, Item_Charge NUMERIC(8,2), Quantity NUMERIC(19,6), Item_Net Weight, Item_CM NUMERIC(19,6), Sub_Stream_2_No Entity_No, Sub_Stream_3_No Entity_No, Has_Override Boolean, Override_User Operator_Code, Override_Comment Entity_Comment, Charge_Rate NUMERIC(10,4), Original_Rate_Range_No Entity_No ) BEGIN SELECT H.Transaction_No, H.Docket_No, H.Docket_No_Prefix, H.Docket_No_Suffix, H.Docket_No_Version, H.Vehicle_No, H.Vehicle_Code, H.Customer_No, H.Job_No, H.Carrier_No, H.Gross, H.Gross_Date, H.Gross_Time, H.Tare, H.Tare_Date, H.Tare_Time, H.Net, H.Memo, H.Date_Complete, H.Time_Complete, H.Created_At, H.Shift_No, H.Created_By, H.Is_Fully_Paid, H.Amount_Paid, ISNULL(C.Advance_Pay_Total, 0), H.Is_Stored_Tare, H.DateTime_In, H.DateTime_Out, H.Custom_Data1, H.Order_No, H.Reversal_Tran_No, H.Original_Tran_No, I.Transaction_Item_No, I.Product_No, I.Area_No, I.Waste_Stream_No, I.Sub_Stream_1_No, E.Name AS SS1_Name, I.LGA_Code, I.Rate_Range_No, I.Cost_Method, I.Cost_Rule_No, I.Item_Charge, I.Quantity, I.Net AS Item_Net, I.Cubic_Metres AS Item_CM, I.Sub_Stream_2_No, I.Sub_Stream_3_No, (CASE A.Code WHEN @ChargeOverrideExceptionCode THEN 'Y' ELSE 'N' END) AS Has_Override, A.Operator_Code AS Override_User, A.Exception_Comment AS Override_Comment, B.Rate AS Charge_Rate, I.Original_Rate_Range_No FROM WM_Transaction_Header H INNER JOIN WM_Transaction_Item I ON (I.Transaction_No = H.Transaction_No) LEFT OUTER JOIN WM_Waste_Sub_Stream_1 E ON (E.Sub_Stream_1_No = I.Sub_Stream_1_No) LEFT OUTER JOIN ( SELECT TEx.Transaction_No, Ex.Code, TEx.Operator_Code, TEx.Exception_Comment FROM WM_Transaction_Exception TEx INNER JOIN WM_Exceptions Ex ON (Ex.Exception_No = TEx.Exception_No) ) A ON (A.Transaction_No = H.Transaction_No) AND (A.Code = @ChargeOverrideExceptionCode) LEFT OUTER JOIN ( SELECT Transaction_No, Transaction_Item_No, Rate, Rate_No FROM WM_Transaction_Item_Rate ) B ON (B.Transaction_No = I.Transaction_No) AND (B.Transaction_Item_No = I.Transaction_Item_No) AND (B.Rate_No = @ProductChargeRateNo) LEFT OUTER JOIN ( SELECT TP.Transaction_No, SUM(TP.Amount) AS Advance_Pay_Total FROM WM_Transaction_Payment TP INNER JOIN WM_Payment P ON (P.Payment_No = TP.Payment_No) WHERE P.Payment_Type = 'A' GROUP BY TP.Transaction_No ) C ON (C.Transaction_No = H.Transaction_No) WHERE H.Transaction_No = @TranNo AND Is_Completed = 'Y'; END; --Add version control rec (remote) INSERT "DBA".SM_Version_Control(Entity_Name, Database_ID, Version_No) VALUES('WM_BaseFrame_CompletedTran', (SELECT db_property('GlobalDbID')), 1); PASSTHROUGH STOP; END IF; --Add version control rec (consolidated) INSERT "DBA".SM_Version_Control(Entity_Name, Database_ID, Version_No) VALUES('WM_BaseFrame_CompletedTran', (SELECT db_property('GlobalDbID')), 1); END IF; /*********************************************************************/ /* 27/03/2007 - Add foreign key on Job_No field on WM_Transaction_Header */ /* and WM_Transaction_Header_A tables */ /* NOTE: This may FAIL if transactions exist that reference jobs that */ /* no longer EXIST. If this happens, the issue will need to be resolved */ /* before adding the foreign key */ /*********************************************************************/ IF NOT EXISTS( SELECT 1 FROM sys.sysforeignkey WHERE role = 'FK_WM_TRAN_REF_WM_JOB') THEN ALTER TABLE "DBA".WM_Transaction_Header ADD CONSTRAINT FK_WM_TRAN_REF_WM_JOB FOREIGN KEY (Job_No) REFERENCES "DBA".WM_Job (Job_No) ON UPDATE RESTRICT ON DELETE RESTRICT; --Passthrough the update to the remote sites (if in a replicated environment) IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_Site_Data') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_Site_Data; ALTER TABLE "DBA".WM_Transaction_Header ADD CONSTRAINT FK_WM_TRAN_REF_WM_JOB FOREIGN KEY (Job_No) REFERENCES "DBA".WM_Job (Job_No) ON UPDATE RESTRICT ON DELETE RESTRICT; PASSTHROUGH STOP; END IF; END IF; IF NOT EXISTS( SELECT 1 FROM sys.sysforeignkey WHERE role = 'FK_WM_TRAN_A_REF_WM_JOB') THEN ALTER TABLE "DBA".WM_Transaction_Header_A ADD CONSTRAINT FK_WM_TRAN_A_REF_WM_JOB FOREIGN KEY (Job_No) REFERENCES "DBA".WM_Job (Job_No) ON UPDATE RESTRICT ON DELETE RESTRICT; --Passthrough the update to the remote sites (if in a replicated environment) IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_Site_Data') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_Site_Data; ALTER TABLE "DBA".WM_Transaction_Header_A ADD CONSTRAINT FK_WM_TRAN_A_REF_WM_JOB FOREIGN KEY (Job_No) REFERENCES "DBA".WM_Job (Job_No) ON UPDATE RESTRICT ON DELETE RESTRICT; PASSTHROUGH STOP; END IF; END IF; /***********************************************/ /* 28/03/2007 - Add group fields to WM_Payment_Method table */ /***********************************************/ IF NOT EXISTS( SELECT 1 FROM syscolumn JOIN systable WHERE table_name = 'WM_Payment_Method' AND column_name = 'Group_G0') THEN ALTER TABLE "DBA".WM_Payment_Method ADD Group_G0 Grouping_Code; ALTER TABLE "DBA".WM_Payment_Method ADD Group_G1 Grouping_Code; ALTER TABLE "DBA".WM_Payment_Method ADD Group_G2 Grouping_Code; ALTER TABLE "DBA".WM_Payment_Method ADD Group_G3 Grouping_Code; ALTER TABLE "DBA".WM_Payment_Method ADD Group_G4 Grouping_Code; --Passthrough the update to the remote sites (if in a replicated environment) IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; ALTER TABLE "DBA".WM_Payment_Method ADD Group_G0 Grouping_Code; ALTER TABLE "DBA".WM_Payment_Method ADD Group_G1 Grouping_Code; ALTER TABLE "DBA".WM_Payment_Method ADD Group_G2 Grouping_Code; ALTER TABLE "DBA".WM_Payment_Method ADD Group_G3 Grouping_Code; ALTER TABLE "DBA".WM_Payment_Method ADD Group_G4 Grouping_Code; PASSTHROUGH STOP; END IF; END IF; /***********************************************/ /* 28/03/2007 - Update proc to clear EPA tables to clear the new EPA tables */ /***********************************************/ IF NOT EXISTS( SELECT 1 FROM "DBA".SM_Version_Control WHERE Entity_Name = 'Clear_EPA_RPTM_Tables' AND Database_ID = (SELECT db_property('GlobalDbID'))) THEN ALTER PROCEDURE "DBA"."Clear_EPA_RPTM_Tables"() BEGIN TRUNCATE TABLE WM_EPA_RPTM_AshDets; TRUNCATE TABLE WM_EPA_RPTM_BldDets; TRUNCATE TABLE WM_EPA_RPTM_BldOther; TRUNCATE TABLE WM_EPA_RPTM_ComDets; TRUNCATE TABLE WM_EPA_RPTM_ComOther; TRUNCATE TABLE WM_EPA_RPTM_CtyDets; TRUNCATE TABLE WM_EPA_RPTM_GVBldDets; TRUNCATE TABLE WM_EPA_RPTM_GVComDets; TRUNCATE TABLE WM_EPA_RPTM_GVMunDets; TRUNCATE TABLE WM_EPA_RPTM_MunDets; TRUNCATE TABLE WM_EPA_RPTM_Report; TRUNCATE TABLE WM_EPA_RPTM_SegDets; TRUNCATE TABLE WM_EPA_RPTM_WFacDets; TRUNCATE TABLE WM_EPA_RPTM_WFacDetsDetail; TRUNCATE TABLE WM_EPA_RPTM_Sec2_WasteReceived; TRUNCATE TABLE WM_EPA_RPTM_Sec3_Exemptions; END; INSERT "DBA".SM_Version_Control(Entity_Name, Version_No, Database_ID) VALUES('Clear_EPA_RPTM_Tables', 1, (SELECT db_property('GlobalDBId'))); --Passthrough the update to the remote sites (if in a replicated environment) IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; ALTER PROCEDURE "DBA"."Clear_EPA_RPTM_Tables"() BEGIN TRUNCATE TABLE WM_EPA_RPTM_AshDets; TRUNCATE TABLE WM_EPA_RPTM_BldDets; TRUNCATE TABLE WM_EPA_RPTM_BldOther; TRUNCATE TABLE WM_EPA_RPTM_ComDets; TRUNCATE TABLE WM_EPA_RPTM_ComOther; TRUNCATE TABLE WM_EPA_RPTM_CtyDets; TRUNCATE TABLE WM_EPA_RPTM_GVBldDets; TRUNCATE TABLE WM_EPA_RPTM_GVComDets; TRUNCATE TABLE WM_EPA_RPTM_GVMunDets; TRUNCATE TABLE WM_EPA_RPTM_MunDets; TRUNCATE TABLE WM_EPA_RPTM_Report; TRUNCATE TABLE WM_EPA_RPTM_SegDets; TRUNCATE TABLE WM_EPA_RPTM_WFacDets; TRUNCATE TABLE WM_EPA_RPTM_WFacDetsDetail; TRUNCATE TABLE WM_EPA_RPTM_Sec2_WasteReceived; TRUNCATE TABLE WM_EPA_RPTM_Sec3_Exemptions; END; INSERT "DBA".SM_Version_Control(Entity_Name, Version_No, Database_ID) VALUES('Clear_EPA_RPTM_Tables', 1, (SELECT db_property('GlobalDBId'))); PASSTHROUGH STOP; END IF; END IF; /*********************************************************************/ /* 28/03/2007 - Default Job Target and Period Target Enforcement to NONE */ /*********************************************************************/ IF EXISTS( SELECT 1 FROM syscolumn JOIN systable WHERE table_name = 'WM_Job_Auth_Product' AND column_name = 'Job_Target_Enforcement' AND "default" = '''E''') THEN ALTER TABLE "DBA".WM_Job_Auth_Product MODIFY Job_Target_Enforcement DEFAULT 'N'; IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; ALTER TABLE "DBA".WM_Job_Auth_Product MODIFY Job_Target_Enforcement DEFAULT 'N'; PASSTHROUGH STOP; END IF; END IF; IF EXISTS( SELECT 1 FROM syscolumn JOIN systable WHERE table_name = 'WM_Job_Auth_Product' AND column_name = 'Period_Target_Enforcement' AND "default" = '''E''') THEN ALTER TABLE "DBA".WM_Job_Auth_Product MODIFY Period_Target_Enforcement DEFAULT 'N'; IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; ALTER TABLE "DBA".WM_Job_Auth_Product MODIFY Period_Target_Enforcement DEFAULT 'N'; PASSTHROUGH STOP; END IF; END IF; /*********************************************************************/ /* 28/03/2007 - Update Default Views 216 (customer) and 217 (product) */ /* to include alias */ /*********************************************************************/ IF EXISTS( SELECT 1 FROM "DBA".SM_Version_Control WHERE Entity_Name = 'Default_View_0216' AND Database_ID = (SELECT db_property( 'GlobalDBId' )) AND Version_No < 5) THEN ALTER PROCEDURE "DBA".Default_View_0216() BEGIN --Build temp table of authorised customers CREATE TABLE #AuthList(Customer_No INT); CALL "DBA".WM_BuildTranLookupList('Customer', '#AuthList'); SELECT C.Code, C.Name, C.Alias, M.Method as Payment_Method, C.Contact, C.Phone1, C.Is_Barred, C.Is_Stop_Credit, C.Is_Debtor, C.Customer_No FROM "DBA".WM_Customer C LEFT OUTER JOIN "DBA".WM_Payment_Method M ON (M.Pay_Method_No = C.Default_Pay_Method_No) WHERE (C.Available = 'Y') AND (Customer_No IN (SELECT Customer_No FROM #AuthList)); END; --Update version control UPDATE "DBA".SM_Version_Control SET Version_No = 5 WHERE Entity_Name = 'Default_View_0216' AND Database_ID = (SELECT db_property( 'GlobalDBId' )); --Passthrough the update to the remote sites (if in a replicated environment) IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; ALTER PROCEDURE "DBA".Default_View_0216() BEGIN --Build temp table of authorised customers CREATE TABLE #AuthList(Customer_No INT); CALL "DBA".WM_BuildTranLookupList('Customer', '#AuthList'); SELECT C.Code, C.Name, C.Alias, M.Method as Payment_Method, C.Contact, C.Phone1, C.Is_Barred, C.Is_Stop_Credit, C.Is_Debtor, C.Customer_No FROM "DBA".WM_Customer C LEFT OUTER JOIN "DBA".WM_Payment_Method M ON (M.Pay_Method_No = C.Default_Pay_Method_No) WHERE (C.Available = 'Y') AND (Customer_No IN (SELECT Customer_No FROM #AuthList)); END; --Update version control UPDATE "DBA".SM_Version_Control SET Version_No = 5 WHERE Entity_Name = 'Default_View_0216' AND Database_ID = (SELECT db_property( 'GlobalDBId' )); PASSTHROUGH STOP; END IF; END IF; IF EXISTS( SELECT 1 FROM "DBA".SM_Version_Control WHERE Entity_Name = 'Default_View_0217' AND Database_ID = (SELECT db_property( 'GlobalDBId' )) AND Version_No < 5) THEN ALTER PROCEDURE "DBA".Default_View_0217() BEGIN --Build temp table of authorised products CREATE TABLE #AuthList(Product_No INT); CALL "DBA".WM_BuildTranLookupList('Product', '#AuthList'); SELECT P.Code, P.Name, P.Alias, T.Name AS Product_Type, A.Name AS Area_Name, P.Product_No FROM "DBA".WM_Product P LEFT OUTER JOIN "DBA".WM_Product_Type T ON (P.Product_Type_No = T.Product_Type_No) LEFT OUTER JOIN "DBA".WM_Area A ON (P.Area_No = A.Area_No) WHERE (P.Available = 'Y') AND (Product_No IN (SELECT Product_No FROM #AuthList)); END; --Update version control UPDATE "DBA".SM_Version_Control SET Version_No = 5 WHERE Entity_Name = 'Default_View_0217' AND Database_ID = (SELECT db_property( 'GlobalDBId' )); --Passthrough the update to the remote sites (if in a replicated environment) IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; ALTER PROCEDURE "DBA".Default_View_0217() BEGIN --Build temp table of authorised products CREATE TABLE #AuthList(Product_No INT); CALL "DBA".WM_BuildTranLookupList('Product', '#AuthList'); SELECT P.Code, P.Name, P.Alias, T.Name AS Product_Type, A.Name AS Area_Name, P.Product_No FROM "DBA".WM_Product P LEFT OUTER JOIN "DBA".WM_Product_Type T ON (P.Product_Type_No = T.Product_Type_No) LEFT OUTER JOIN "DBA".WM_Area A ON (P.Area_No = A.Area_No) WHERE (P.Available = 'Y') AND (Product_No IN (SELECT Product_No FROM #AuthList)); END; --Update version control UPDATE "DBA".SM_Version_Control SET Version_No = 5 WHERE Entity_Name = 'Default_View_0217' AND Database_ID = (SELECT db_property( 'GlobalDBId' )); PASSTHROUGH STOP; END IF; END IF; /*********************************************************************/ /* 29/03/2007 - Add triggers on WM_Job to prevent duplicate job codes */ /* from being able to be entered */ /*********************************************************************/ IF NOT EXISTS( SELECT 1 FROM systrigger WHERE trigger_name = 'bi_WM_Job') THEN CREATE TRIGGER bi_WM_Job BEFORE INSERT ON "DBA".WM_Job REFERENCING NEW AS new_Rec FOR EACH ROW BEGIN /* Permit duplicates entered via SQL Remote but bomb out if anyone else enters */ IF CURRENT REMOTE USER IS NULL AND EXISTS(SELECT 1 FROM WM_Job WHERE Code = new_Rec.Code) THEN RAISERROR 30000 'Job Code already exists'; END IF; END; --Passthrough the update to the remote sites (if in a replicated environment) IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; CREATE TRIGGER bi_WM_Job BEFORE INSERT ON "DBA".WM_Job REFERENCING NEW AS new_Rec FOR EACH ROW BEGIN /* Permit duplicates entered via SQL Remote but bomb out if anyone else enters */ IF CURRENT REMOTE USER IS NULL AND EXISTS(SELECT 1 FROM WM_Job WHERE Code = new_Rec.Code) THEN RAISERROR 30000 'Job Code already exists'; END IF; END; PASSTHROUGH STOP; END IF; END IF; IF NOT EXISTS( SELECT 1 FROM systrigger WHERE trigger_name = 'bu_WM_Job') THEN CREATE TRIGGER bu_WM_Job BEFORE UPDATE ON "DBA".WM_Job REFERENCING OLD AS old_Rec NEW AS new_Rec FOR EACH ROW BEGIN IF UPDATE(Code) THEN /* Permit duplicates entered via SQL Remote but bomb out if anyone else enters */ IF CURRENT REMOTE USER IS NULL AND EXISTS(SELECT 1 FROM WM_Job WHERE Code = new_Rec.Code AND Job_No <> new_Rec.Job_No) THEN RAISERROR 30000 'Job Code already exists'; END IF; END IF; END; --Passthrough the update to the remote sites (if in a replicated environment) IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; CREATE TRIGGER bu_WM_Job BEFORE UPDATE ON "DBA".WM_Job REFERENCING OLD AS old_Rec NEW AS new_Rec FOR EACH ROW BEGIN IF UPDATE(Code) THEN /* Permit duplicates entered via SQL Remote but bomb out if anyone else enters */ IF CURRENT REMOTE USER IS NULL AND EXISTS(SELECT 1 FROM WM_Job WHERE Code = new_Rec.Code AND Job_No <> new_Rec.Job_No) THEN RAISERROR 30000 'Job Code already exists'; END IF; END IF; END; PASSTHROUGH STOP; END IF; END IF; /*********************************************************************/ /* 13/04/2007 - Add more fields to Accounts transaction import procedures */ /*********************************************************************/ IF NOT EXISTS( SELECT 1 FROM "DBA".SM_Version_Control WHERE Entity_Name = 'PT-WM_Accounts_Tran_Import_Data' AND Database_ID = (SELECT db_property('GlobalDbID'))) THEN ALTER PROCEDURE "DBA"."WM_Accounts_Tran_Import_Data"(in @StartDate date default null,in @EndDate date default null) result (Transaction_No Large_Entity_No,Docket varchar(20),Date_Complete date,Time_Complete time,Vehicle_Code Entity_Code,Customer_No Entity_No, Customer_Code Entity_Code,Customer_Name Entity_Name,Customer_Alias varchar(40),Cust_Debtor_Account Entity_Account,Cust_GL_Account Entity_Account, Gross Weight,Tare Weight,Net Weight,Transaction_Amount numeric(8,2),Transaction_Item_No Large_Entity_No,Product_Code Entity_Code, Product_Name Entity_Name,Product_GL_Account Entity_Account,Product_Rate varchar(20),Item_Net Weight,Item_Cubic_Metres numeric(19,6), Item_Quantity numeric(19,6),Item_Cost_Method Prod_Cost_Method,Item_Amount numeric(8,2),IR_Rate numeric(10,4),IR_Value numeric(10,4), IR_Rate_No Entity_No,IR_Rate_Code Entity_Code,IR_Rate_Type char(1),IR_Rate_Is_GST Boolean,IR_Rate_GL_Account Entity_Account,Is_Deleted char(1), Date_Deleted date,Time_Deleted time,Deleted_By Operator_Code,Reversal_Tran_No Large_Entity_No,Original_Tran_No Large_Entity_No, Original_Docket varchar(20),Original_Docket_Date date,Site_No Entity_No,Site_Code Entity_Code,Job_No Entity_No,Job_Code Entity_Code, Order_No varchar(40)) begin select H.Transaction_No, H.Docket, H.Date_Complete, H.Time_Complete, H.Vehicle_Code, H.Customer_No, C.Code as Customer_Code, C.Name as Customer_Name, C.Alias as Customer_Alias, C.Debtor_Account as Cust_Debtor_Account, C.GL_Account as Cust_GL_Account, H.Gross, H.Tare, H.Net, H.Transaction_Amount, I.Transaction_Item_No, P.Code as Product_Code, P.Name as Product_Name, P.GL_Account as Product_GL_Account, PR.Description as Product_Rate, I.Net as Item_Net, I.Cubic_Metres as Item_Cubic_Metres, I.Quantity as Item_Quantity, I.Cost_Method as Item_Cost_Method, I.Item_Charge as Item_Amount, IR.Rate as IR_Rate, IR.Value as IR_Value, IR.Rate_No as IR_Rate_No, R.Code as IR_Rate_Code, R.Type as IR_Rate_Type, R.Is_GST as IR_Rate_Is_GST, R.GL_Account_Code as IR_Rate_GL_Account,'N' as Is_Deleted, H.Date_Deleted, H.Time_Deleted, H.Deleted_By, H.Reversal_Tran_No, H.Original_Tran_No, (case when H.Original_Tran_No is not null then OH.Docket else null end) as Original_Docket, (case when H.Original_Tran_No is not null then OH.Date_Complete else null end) as Original_Docket_Date, S.Site_No, S.Site_Code, J.Job_No, J.Code as Job_Code, J.Order_No from DBA.WM_Transaction_Header as H left outer join DBA.WM_Customer as C on (C.Customer_No = H.Customer_No) left outer join DBA.WM_Transaction_Header as OH on (H.Original_Tran_No = OH.Transaction_No) join DBA.WM_Transaction_Item as I on (I.Transaction_No = H.Transaction_No) join DBA.WM_Product as P on (P.Product_No = I.Product_No) join DBA.WM_Area as A on (A.Area_No = I.Area_No) left outer join DBA.WM_Product_Rate as PR on (PR.Range_No = I.Rate_Range_No) join DBA.WM_Transaction_Item_Rate as IR on ((IR.Transaction_No = I.Transaction_No) and (IR.Transaction_Item_No = I.Transaction_Item_No)) join DBA.WM_Rate as R on (R.Rate_No = IR.Rate_No) join DBA.SM_Site as S on (S.Site_No = H.Created_At) left outer join DBA.WM_Job as J on (J.Job_No = H.Job_No) where H.Is_Completed = 'Y' and H.Accounts_Import_Flag is null and ((C.Debtor_Account is not null) and(C.Debtor_Account <> '')) and ((R.GL_Account_Code is not null) or(R.Is_GST = 'Y')) and (((@StartDate is not null) and(H.Date_Complete >= @StartDate)) or(@StartDate is null)) and (((@EndDate is not null) and(H.Date_Complete <= @EndDate)) or(@EndDate is null)) union all select H.Transaction_No, H.Docket, H.Date_Complete, H.Time_Complete, H.Vehicle_Code, H.Customer_No, C.Code as Customer_Code, C.Name as Customer_Name, C.Alias as Customer_Alias, C.Debtor_Account as Cust_Debtor_Account, C.GL_Account as Cust_GL_Account, H.Gross, H.Tare, H.Net, H.Transaction_Amount, I.Transaction_Item_No, P.Code as Product_Code, P.Name as Product_Name, P.GL_Account as Product_GL_Account, PR.Description as Product_Rate, I.Net as Item_Net, I.Cubic_Metres as Item_Cubic_Metres, I.Quantity as Item_Quantity, I.Cost_Method as Item_Cost_Method, I.Item_Charge as Item_Amount, IR.Rate as IR_Rate, IR.Value as IR_Value, IR.Rate_No as IR_Rate_No, R.Code as IR_Rate_Code, R.Type as IR_Rate_Type, R.Is_GST as IR_Rate_Is_GST, R.GL_Account_Code as IR_Rate_GL_Account,'Y' as Is_Deleted, H.Date_Deleted, H.Time_Deleted, H.Deleted_By, H.Reversal_Tran_No, H.Original_Tran_No, null as Original_Docket, null as Original_Docket_Date, S.Site_No, S.Site_Code, J.Job_No, J.Code as Job_Code, J.Order_No from DBA.WM_Transaction_Header_A as H left outer join DBA.WM_Customer as C on (C.Customer_No = H.Customer_No) join DBA.WM_Transaction_Item_A as I on (I.Transaction_No = H.Transaction_No) join DBA.WM_Product as P on (P.Product_No = I.Product_No) join DBA.WM_Area as A on (A.Area_No = I.Area_No) left outer join DBA.WM_Product_Rate as PR on (PR.Range_No = I.Rate_Range_No) join DBA.WM_Transaction_Item_Rate_A as IR on ((IR.Transaction_No = I.Transaction_No) and (IR.Transaction_Item_No = I.Transaction_Item_No)) join DBA.WM_Rate as R on (R.Rate_No = IR.Rate_No) join DBA.SM_Site as S on (S.Site_No = H.Created_At) left outer join DBA.WM_Job as J on (J.Job_No = H.Job_No) where H.Is_Completed = 'Y' and H.Accounts_Import_Flag is null and ((C.Debtor_Account is not null) and(C.Debtor_Account <> '')) and ((R.GL_Account_Code is not null) or(R.Is_GST = 'Y')) and (((@StartDate is not null) and(H.Date_Complete >= @StartDate)) or(@StartDate is null)) and (((@EndDate is not null) and(H.Date_Complete <= @EndDate)) or(@EndDate is null)) order by 2 asc,16 asc,27 asc; /* Docket, Transaction_Item_No, Rate_No */ end; --Passthrough the update to the remote sites (if in a replicated environment) IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; ALTER PROCEDURE "DBA"."WM_Accounts_Tran_Import_Data"(in @StartDate date default null,in @EndDate date default null) result (Transaction_No Large_Entity_No,Docket varchar(20),Date_Complete date,Time_Complete time,Vehicle_Code Entity_Code,Customer_No Entity_No, Customer_Code Entity_Code,Customer_Name Entity_Name,Customer_Alias varchar(40),Cust_Debtor_Account Entity_Account,Cust_GL_Account Entity_Account, Gross Weight,Tare Weight,Net Weight,Transaction_Amount numeric(8,2),Transaction_Item_No Large_Entity_No,Product_Code Entity_Code, Product_Name Entity_Name,Product_GL_Account Entity_Account,Product_Rate varchar(20),Item_Net Weight,Item_Cubic_Metres numeric(19,6), Item_Quantity numeric(19,6),Item_Cost_Method Prod_Cost_Method,Item_Amount numeric(8,2),IR_Rate numeric(10,4),IR_Value numeric(10,4), IR_Rate_No Entity_No,IR_Rate_Code Entity_Code,IR_Rate_Type char(1),IR_Rate_Is_GST Boolean,IR_Rate_GL_Account Entity_Account,Is_Deleted char(1), Date_Deleted date,Time_Deleted time,Deleted_By Operator_Code,Reversal_Tran_No Large_Entity_No,Original_Tran_No Large_Entity_No, Original_Docket varchar(20),Original_Docket_Date date,Site_No Entity_No,Site_Code Entity_Code,Job_No Entity_No,Job_Code Entity_Code, Order_No varchar(40)) begin select H.Transaction_No, H.Docket, H.Date_Complete, H.Time_Complete, H.Vehicle_Code, H.Customer_No, C.Code as Customer_Code, C.Name as Customer_Name, C.Alias as Customer_Alias, C.Debtor_Account as Cust_Debtor_Account, C.GL_Account as Cust_GL_Account, H.Gross, H.Tare, H.Net, H.Transaction_Amount, I.Transaction_Item_No, P.Code as Product_Code, P.Name as Product_Name, P.GL_Account as Product_GL_Account, PR.Description as Product_Rate, I.Net as Item_Net, I.Cubic_Metres as Item_Cubic_Metres, I.Quantity as Item_Quantity, I.Cost_Method as Item_Cost_Method, I.Item_Charge as Item_Amount, IR.Rate as IR_Rate, IR.Value as IR_Value, IR.Rate_No as IR_Rate_No, R.Code as IR_Rate_Code, R.Type as IR_Rate_Type, R.Is_GST as IR_Rate_Is_GST, R.GL_Account_Code as IR_Rate_GL_Account,'N' as Is_Deleted, H.Date_Deleted, H.Time_Deleted, H.Deleted_By, H.Reversal_Tran_No, H.Original_Tran_No, (case when H.Original_Tran_No is not null then OH.Docket else null end) as Original_Docket, (case when H.Original_Tran_No is not null then OH.Date_Complete else null end) as Original_Docket_Date, S.Site_No, S.Site_Code, J.Job_No, J.Code as Job_Code, J.Order_No from DBA.WM_Transaction_Header as H left outer join DBA.WM_Customer as C on (C.Customer_No = H.Customer_No) left outer join DBA.WM_Transaction_Header as OH on (H.Original_Tran_No = OH.Transaction_No) join DBA.WM_Transaction_Item as I on (I.Transaction_No = H.Transaction_No) join DBA.WM_Product as P on (P.Product_No = I.Product_No) join DBA.WM_Area as A on (A.Area_No = I.Area_No) left outer join DBA.WM_Product_Rate as PR on (PR.Range_No = I.Rate_Range_No) join DBA.WM_Transaction_Item_Rate as IR on ((IR.Transaction_No = I.Transaction_No) and (IR.Transaction_Item_No = I.Transaction_Item_No)) join DBA.WM_Rate as R on (R.Rate_No = IR.Rate_No) join DBA.SM_Site as S on (S.Site_No = H.Created_At) left outer join DBA.WM_Job as J on (J.Job_No = H.Job_No) where H.Is_Completed = 'Y' and H.Accounts_Import_Flag is null and ((C.Debtor_Account is not null) and(C.Debtor_Account <> '')) and ((R.GL_Account_Code is not null) or(R.Is_GST = 'Y')) and (((@StartDate is not null) and(H.Date_Complete >= @StartDate)) or(@StartDate is null)) and (((@EndDate is not null) and(H.Date_Complete <= @EndDate)) or(@EndDate is null)) union all select H.Transaction_No, H.Docket, H.Date_Complete, H.Time_Complete, H.Vehicle_Code, H.Customer_No, C.Code as Customer_Code, C.Name as Customer_Name, C.Alias as Customer_Alias, C.Debtor_Account as Cust_Debtor_Account, C.GL_Account as Cust_GL_Account, H.Gross, H.Tare, H.Net, H.Transaction_Amount, I.Transaction_Item_No, P.Code as Product_Code, P.Name as Product_Name, P.GL_Account as Product_GL_Account, PR.Description as Product_Rate, I.Net as Item_Net, I.Cubic_Metres as Item_Cubic_Metres, I.Quantity as Item_Quantity, I.Cost_Method as Item_Cost_Method, I.Item_Charge as Item_Amount, IR.Rate as IR_Rate, IR.Value as IR_Value, IR.Rate_No as IR_Rate_No, R.Code as IR_Rate_Code, R.Type as IR_Rate_Type, R.Is_GST as IR_Rate_Is_GST, R.GL_Account_Code as IR_Rate_GL_Account,'Y' as Is_Deleted, H.Date_Deleted, H.Time_Deleted, H.Deleted_By, H.Reversal_Tran_No, H.Original_Tran_No, null as Original_Docket, null as Original_Docket_Date, S.Site_No, S.Site_Code, J.Job_No, J.Code as Job_Code, J.Order_No from DBA.WM_Transaction_Header_A as H left outer join DBA.WM_Customer as C on (C.Customer_No = H.Customer_No) join DBA.WM_Transaction_Item_A as I on (I.Transaction_No = H.Transaction_No) join DBA.WM_Product as P on (P.Product_No = I.Product_No) join DBA.WM_Area as A on (A.Area_No = I.Area_No) left outer join DBA.WM_Product_Rate as PR on (PR.Range_No = I.Rate_Range_No) join DBA.WM_Transaction_Item_Rate_A as IR on ((IR.Transaction_No = I.Transaction_No) and (IR.Transaction_Item_No = I.Transaction_Item_No)) join DBA.WM_Rate as R on (R.Rate_No = IR.Rate_No) join DBA.SM_Site as S on (S.Site_No = H.Created_At) left outer join DBA.WM_Job as J on (J.Job_No = H.Job_No) where H.Is_Completed = 'Y' and H.Accounts_Import_Flag is null and ((C.Debtor_Account is not null) and(C.Debtor_Account <> '')) and ((R.GL_Account_Code is not null) or(R.Is_GST = 'Y')) and (((@StartDate is not null) and(H.Date_Complete >= @StartDate)) or(@StartDate is null)) and (((@EndDate is not null) and(H.Date_Complete <= @EndDate)) or(@EndDate is null)) order by 2 asc,16 asc,27 asc; /* Docket, Transaction_Item_No, Rate_No */ end; INSERT "DBA".SM_Version_Control(Entity_Name, Version_No, Database_ID) VALUES('PT-WM_Accounts_Tran_Import_Data', 1, (SELECT db_property('GlobalDBId'))); PASSTHROUGH STOP; END IF; INSERT "DBA".SM_Version_Control(Entity_Name, Version_No, Database_ID) VALUES('PT-WM_Accounts_Tran_Import_Data', 1, (SELECT db_property('GlobalDBId'))); END IF; IF NOT EXISTS( SELECT 1 FROM "DBA".SM_Version_Control WHERE Entity_Name = 'PT-WM_Save_Accounts_Tran_Import_Data' AND Database_ID = (SELECT db_property('GlobalDbID'))) THEN ALTER PROCEDURE "DBA"."WM_Save_Accounts_Tran_Import_Data"(in @StartDate date default null,in @EndDate date default null) begin /* Create summary table to hold results, if doesn''t already exist */ if not exists(select 1 from systable where table_name = 'WMS_Accounts_Tran_Import_Data' and table_type = 'BASE') then create table DBA.WMS_Accounts_Tran_Import_Data( Transaction_No Large_Entity_No null, Docket varchar(20) not null, Date_Complete date null, Time_Complete time null, Vehicle_Code Entity_Code null, Customer_No Entity_No null, Customer_Code Entity_Code null, Customer_Name Entity_Name null, Customer_Alias varchar(40) null, Cust_Debtor_Account Entity_Account null, Cust_GL_Account Entity_Account null, Gross Weight null, Tare Weight null, Net Weight null, Transaction_Amount numeric(8,2) null, Transaction_Item_No Large_Entity_No not null, Product_Code Entity_Code null, Product_Name Entity_Name null, Product_GL_Account Entity_Account null, Product_Rate varchar(20) null, Item_Net Weight null, Item_Cubic_Metres numeric(19,6) null, Item_Quantity numeric(19,6) null, Item_Cost_Method Prod_Cost_Method null, Item_Amount numeric(8,2) null, IR_Rate numeric(10,4) null, IR_Value numeric(10,4) null, IR_Rate_No Entity_No not null, IR_Rate_Code Entity_Code null, IR_Rate_Type char(1) null, IR_Rate_Is_GST Boolean null, IR_Rate_GL_Account Entity_Account null, Is_Deleted char(1) null, Date_Deleted date null, Time_Deleted time null, Deleted_By Operator_Code null, Reversal_Tran_No Large_Entity_No null, Original_Tran_No Large_Entity_No null, Original_Docket varchar(20) null, Original_Docket_Date date null, Site_No Entity_No null, Site_Code Entity_Code null, Job_No Entity_No null, Job_Code Entity_Code null, Order_No varchar(40) null, primary key( Docket, Transaction_Item_No,IR_Rate_No), ); create index IDX_Docket on DBA.WMS_Accounts_Tran_Import_Data(Docket asc); create index IDX_Date on DBA.WMS_Accounts_Tran_Import_Data(Date_Complete asc); create index IDX_Cust_Debtor_Account on DBA.WMS_Accounts_Tran_Import_Data(Cust_Debtor_Account asc) else /* Clear summary table */ delete from DBA.WMS_Accounts_Tran_Import_Data end if; /* NOTE This statement should mirror that used in procedure WM_Accounts_Tran_Import_Data */ insert into DBA.WMS_Accounts_Tran_Import_Data select H.Transaction_No, H.Docket, H.Date_Complete, H.Time_Complete, H.Vehicle_Code, H.Customer_No, C.Code as Customer_Code, C.Name as Customer_Name, C.Alias as Customer_Alias, C.Debtor_Account as Cust_Debtor_Account, C.GL_Account as Cust_GL_Account, H.Gross, H.Tare, H.Net, H.Transaction_Amount, I.Transaction_Item_No, P.Code as Product_Code, P.Name as Product_Name, P.GL_Account as Product_GL_Account, PR.Description as Product_Rate, I.Net as Item_Net, I.Cubic_Metres as Item_Cubic_Metres, I.Quantity as Item_Quantity, I.Cost_Method as Item_Cost_Method, I.Item_Charge as Item_Amount, IR.Rate as IR_Rate, IR.Value as IR_Value, IR.Rate_No as IR_Rate_No, R.Code as IR_Rate_Code, R.Type as IR_Rate_Type, R.Is_GST as IR_Rate_Is_GST, R.GL_Account_Code as IR_Rate_GL_Account,'N' as Is_Deleted, H.Date_Deleted, H.Time_Deleted, H.Deleted_By, H.Reversal_Tran_No, H.Original_Tran_No, (case when H.Original_Tran_No is not null then OH.Docket else null end) as Original_Docket, (case when H.Original_Tran_No is not null then OH.Date_Complete else null end) as Original_Docket_Date, S.Site_No, S.Site_Code, J.Job_No, J.Code as Job_Code, J.Order_No from DBA.WM_Transaction_Header as H left outer join DBA.WM_Customer as C on(C.Customer_No = H.Customer_No) left outer join DBA.WM_Transaction_Header as OH on(H.Original_Tran_No = OH.Transaction_No) join DBA.WM_Transaction_Item as I on(I.Transaction_No = H.Transaction_No) join DBA.WM_Product as P on(P.Product_No = I.Product_No) join DBA.WM_Area as A on(A.Area_No = I.Area_No) left outer join DBA.WM_Product_Rate as PR on(PR.Range_No = I.Rate_Range_No) join DBA.WM_Transaction_Item_Rate as IR on((IR.Transaction_No = I.Transaction_No) and(IR.Transaction_Item_No = I.Transaction_Item_No)) join DBA.WM_Rate as R on(R.Rate_No = IR.Rate_No) join DBA.SM_Site as S on (S.Site_No = H.Created_At) left outer join DBA.WM_Job as J on (J.Job_No = H.Job_No) where H.Is_Completed = 'Y' and H.Accounts_Import_Flag is null and ((C.Debtor_Account is not null) and(C.Debtor_Account <> '')) and ((R.GL_Account_Code is not null) or(R.Is_GST = 'Y')) and (((@StartDate is not null) and(H.Date_Complete >= @StartDate)) or(@StartDate is null)) and (((@EndDate is not null) and(H.Date_Complete <= @EndDate)) or(@EndDate is null)) union all select H.Transaction_No, H.Docket, H.Date_Complete, H.Time_Complete, H.Vehicle_Code, H.Customer_No, C.Code as Customer_Code, C.Name as Customer_Name, C.Alias as Customer_Alias, C.Debtor_Account as Cust_Debtor_Account, C.GL_Account as Cust_GL_Account, H.Gross, H.Tare, H.Net, H.Transaction_Amount, I.Transaction_Item_No, P.Code as Product_Code, P.Name as Product_Name, P.GL_Account as Product_GL_Account, PR.Description as Product_Rate, I.Net as Item_Net, I.Cubic_Metres as Item_Cubic_Metres, I.Quantity as Item_Quantity, I.Cost_Method as Item_Cost_Method, I.Item_Charge as Item_Amount, IR.Rate as IR_Rate, IR.Value as IR_Value, IR.Rate_No as IR_Rate_No, R.Code as IR_Rate_Code, R.Type as IR_Rate_Type, R.Is_GST as IR_Rate_Is_GST, R.GL_Account_Code as IR_Rate_GL_Account,'Y' as Is_Deleted, H.Date_Deleted, H.Time_Deleted, H.Deleted_By, H.Reversal_Tran_No, H.Original_Tran_No, null as Original_Docket, null as Original_Docket_Date, S.Site_No, S.Site_Code, J.Job_No, J.Code as Job_Code, J.Order_No from DBA.WM_Transaction_Header_A as H left outer join DBA.WM_Customer as C on(C.Customer_No = H.Customer_No) join DBA.WM_Transaction_Item_A as I on(I.Transaction_No = H.Transaction_No) join DBA.WM_Product as P on(P.Product_No = I.Product_No) join DBA.WM_Area as A on(A.Area_No = I.Area_No) left outer join DBA.WM_Product_Rate as PR on(PR.Range_No = I.Rate_Range_No) join DBA.WM_Transaction_Item_Rate_A as IR on((IR.Transaction_No = I.Transaction_No) and(IR.Transaction_Item_No = I.Transaction_Item_No)) join DBA.WM_Rate as R on(R.Rate_No = IR.Rate_No) join DBA.SM_Site as S on (S.Site_No = H.Created_At) left outer join DBA.WM_Job as J on (J.Job_No = H.Job_No) where H.Is_Completed = 'Y' and H.Accounts_Import_Flag is null and ((C.Debtor_Account is not null) and(C.Debtor_Account <> '')) and ((R.GL_Account_Code is not null) or(R.Is_GST = 'Y')) and (((@StartDate is not null) and(H.Date_Complete >= @StartDate)) or(@StartDate is null)) and (((@EndDate is not null) and(H.Date_Complete <= @EndDate)) or(@EndDate is null)); end; --Passthrough the update to the remote sites (if in a replicated environment) IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; ALTER PROCEDURE "DBA"."WM_Save_Accounts_Tran_Import_Data"(in @StartDate date default null,in @EndDate date default null) begin /* Create summary table to hold results, if doesn''t already exist */ if not exists(select 1 from systable where table_name = 'WMS_Accounts_Tran_Import_Data' and table_type = 'BASE') then create table DBA.WMS_Accounts_Tran_Import_Data( Transaction_No Large_Entity_No null, Docket varchar(20) not null, Date_Complete date null, Time_Complete time null, Vehicle_Code Entity_Code null, Customer_No Entity_No null, Customer_Code Entity_Code null, Customer_Name Entity_Name null, Customer_Alias varchar(40) null, Cust_Debtor_Account Entity_Account null, Cust_GL_Account Entity_Account null, Gross Weight null, Tare Weight null, Net Weight null, Transaction_Amount numeric(8,2) null, Transaction_Item_No Large_Entity_No not null, Product_Code Entity_Code null, Product_Name Entity_Name null, Product_GL_Account Entity_Account null, Product_Rate varchar(20) null, Item_Net Weight null, Item_Cubic_Metres numeric(19,6) null, Item_Quantity numeric(19,6) null, Item_Cost_Method Prod_Cost_Method null, Item_Amount numeric(8,2) null, IR_Rate numeric(10,4) null, IR_Value numeric(10,4) null, IR_Rate_No Entity_No not null, IR_Rate_Code Entity_Code null, IR_Rate_Type char(1) null, IR_Rate_Is_GST Boolean null, IR_Rate_GL_Account Entity_Account null, Is_Deleted char(1) null, Date_Deleted date null, Time_Deleted time null, Deleted_By Operator_Code null, Reversal_Tran_No Large_Entity_No null, Original_Tran_No Large_Entity_No null, Original_Docket varchar(20) null, Original_Docket_Date date null, Site_No Entity_No null, Site_Code Entity_Code null, Job_No Entity_No null, Job_Code Entity_Code null, Order_No varchar(40) null, primary key( Docket, Transaction_Item_No,IR_Rate_No), ); create index IDX_Docket on DBA.WMS_Accounts_Tran_Import_Data(Docket asc); create index IDX_Date on DBA.WMS_Accounts_Tran_Import_Data(Date_Complete asc); create index IDX_Cust_Debtor_Account on DBA.WMS_Accounts_Tran_Import_Data(Cust_Debtor_Account asc) else /* Clear summary table */ delete from DBA.WMS_Accounts_Tran_Import_Data end if; /* NOTE This statement should mirror that used in procedure WM_Accounts_Tran_Import_Data */ insert into DBA.WMS_Accounts_Tran_Import_Data select H.Transaction_No, H.Docket, H.Date_Complete, H.Time_Complete, H.Vehicle_Code, H.Customer_No, C.Code as Customer_Code, C.Name as Customer_Name, C.Alias as Customer_Alias, C.Debtor_Account as Cust_Debtor_Account, C.GL_Account as Cust_GL_Account, H.Gross, H.Tare, H.Net, H.Transaction_Amount, I.Transaction_Item_No, P.Code as Product_Code, P.Name as Product_Name, P.GL_Account as Product_GL_Account, PR.Description as Product_Rate, I.Net as Item_Net, I.Cubic_Metres as Item_Cubic_Metres, I.Quantity as Item_Quantity, I.Cost_Method as Item_Cost_Method, I.Item_Charge as Item_Amount, IR.Rate as IR_Rate, IR.Value as IR_Value, IR.Rate_No as IR_Rate_No, R.Code as IR_Rate_Code, R.Type as IR_Rate_Type, R.Is_GST as IR_Rate_Is_GST, R.GL_Account_Code as IR_Rate_GL_Account,'N' as Is_Deleted, H.Date_Deleted, H.Time_Deleted, H.Deleted_By, H.Reversal_Tran_No, H.Original_Tran_No, (case when H.Original_Tran_No is not null then OH.Docket else null end) as Original_Docket, (case when H.Original_Tran_No is not null then OH.Date_Complete else null end) as Original_Docket_Date, S.Site_No, S.Site_Code, J.Job_No, J.Code as Job_Code, J.Order_No from DBA.WM_Transaction_Header as H left outer join DBA.WM_Customer as C on(C.Customer_No = H.Customer_No) left outer join DBA.WM_Transaction_Header as OH on(H.Original_Tran_No = OH.Transaction_No) join DBA.WM_Transaction_Item as I on(I.Transaction_No = H.Transaction_No) join DBA.WM_Product as P on(P.Product_No = I.Product_No) join DBA.WM_Area as A on(A.Area_No = I.Area_No) left outer join DBA.WM_Product_Rate as PR on(PR.Range_No = I.Rate_Range_No) join DBA.WM_Transaction_Item_Rate as IR on((IR.Transaction_No = I.Transaction_No) and(IR.Transaction_Item_No = I.Transaction_Item_No)) join DBA.WM_Rate as R on(R.Rate_No = IR.Rate_No) join DBA.SM_Site as S on (S.Site_No = H.Created_At) left outer join DBA.WM_Job as J on (J.Job_No = H.Job_No) where H.Is_Completed = 'Y' and H.Accounts_Import_Flag is null and ((C.Debtor_Account is not null) and(C.Debtor_Account <> '')) and ((R.GL_Account_Code is not null) or(R.Is_GST = 'Y')) and (((@StartDate is not null) and(H.Date_Complete >= @StartDate)) or(@StartDate is null)) and (((@EndDate is not null) and(H.Date_Complete <= @EndDate)) or(@EndDate is null)) union all select H.Transaction_No, H.Docket, H.Date_Complete, H.Time_Complete, H.Vehicle_Code, H.Customer_No, C.Code as Customer_Code, C.Name as Customer_Name, C.Alias as Customer_Alias, C.Debtor_Account as Cust_Debtor_Account, C.GL_Account as Cust_GL_Account, H.Gross, H.Tare, H.Net, H.Transaction_Amount, I.Transaction_Item_No, P.Code as Product_Code, P.Name as Product_Name, P.GL_Account as Product_GL_Account, PR.Description as Product_Rate, I.Net as Item_Net, I.Cubic_Metres as Item_Cubic_Metres, I.Quantity as Item_Quantity, I.Cost_Method as Item_Cost_Method, I.Item_Charge as Item_Amount, IR.Rate as IR_Rate, IR.Value as IR_Value, IR.Rate_No as IR_Rate_No, R.Code as IR_Rate_Code, R.Type as IR_Rate_Type, R.Is_GST as IR_Rate_Is_GST, R.GL_Account_Code as IR_Rate_GL_Account,'Y' as Is_Deleted, H.Date_Deleted, H.Time_Deleted, H.Deleted_By, H.Reversal_Tran_No, H.Original_Tran_No, null as Original_Docket, null as Original_Docket_Date, S.Site_No, S.Site_Code, J.Job_No, J.Code as Job_Code, J.Order_No from DBA.WM_Transaction_Header_A as H left outer join DBA.WM_Customer as C on(C.Customer_No = H.Customer_No) join DBA.WM_Transaction_Item_A as I on(I.Transaction_No = H.Transaction_No) join DBA.WM_Product as P on(P.Product_No = I.Product_No) join DBA.WM_Area as A on(A.Area_No = I.Area_No) left outer join DBA.WM_Product_Rate as PR on(PR.Range_No = I.Rate_Range_No) join DBA.WM_Transaction_Item_Rate_A as IR on((IR.Transaction_No = I.Transaction_No) and(IR.Transaction_Item_No = I.Transaction_Item_No)) join DBA.WM_Rate as R on(R.Rate_No = IR.Rate_No) join DBA.SM_Site as S on (S.Site_No = H.Created_At) left outer join DBA.WM_Job as J on (J.Job_No = H.Job_No) where H.Is_Completed = 'Y' and H.Accounts_Import_Flag is null and ((C.Debtor_Account is not null) and(C.Debtor_Account <> '')) and ((R.GL_Account_Code is not null) or(R.Is_GST = 'Y')) and (((@StartDate is not null) and(H.Date_Complete >= @StartDate)) or(@StartDate is null)) and (((@EndDate is not null) and(H.Date_Complete <= @EndDate)) or(@EndDate is null)); end; INSERT "DBA".SM_Version_Control(Entity_Name, Version_No, Database_ID) VALUES('PT-WM_Save_Accounts_Tran_Import_Data', 1, (SELECT db_property('GlobalDBId'))); PASSTHROUGH STOP; END IF; INSERT "DBA".SM_Version_Control(Entity_Name, Version_No, Database_ID) VALUES('PT-WM_Save_Accounts_Tran_Import_Data', 1, (SELECT db_property('GlobalDBId'))); END IF; /*********************************************************************/ /* 19/04/2007 - Change WM_Validation_Code.Validation_Code_No to a Large_Identity_No */ /*********************************************************************/ IF EXISTS( SELECT 1 FROM syscolumn C JOIN systable JOIN sysusertype UT on UT.Type_id = C.user_type WHERE table_name = 'WM_Validation_Code' AND column_name = 'Validation_Code_No' AND type_name <> 'Large_Identity_No') THEN ALTER TABLE "DBA".WM_Validation_Code DROP PRIMARY KEY; DROP INDEX "DBA".WM_Validation_Code.IDX_Customer; ALTER TABLE "DBA".WM_Validation_Code MODIFY Validation_Code_No Large_Identity_No; CREATE INDEX "IDX_Customer" ON "DBA".WM_Validation_Code (Validation_Code_No ASC, Customer_No ASC); ALTER TABLE "DBA".WM_Validation_Code ADD CONSTRAINT PK_WM_Validation_Code PRIMARY KEY (Customer_No, Validation_Code_No, Reference_Code); IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; ALTER TABLE "DBA".WM_Validation_Code DROP PRIMARY KEY; DROP INDEX "DBA".WM_Validation_Code.IDX_Customer; ALTER TABLE "DBA".WM_Validation_Code MODIFY Validation_Code_No Large_Identity_No; CREATE INDEX "IDX_Customer" ON "DBA".WM_Validation_Code (Validation_Code_No ASC, Customer_No ASC); ALTER TABLE "DBA".WM_Validation_Code ADD CONSTRAINT PK_WM_Validation_Code PRIMARY KEY (Customer_No, Validation_Code_No, Reference_Code); PASSTHROUGH STOP; END IF; END IF; /*********************************************************************/ /* 23/04/2007 - Remove NOT NULL constraint from WM_Validation_Code.Alias */ /*********************************************************************/ IF EXISTS( SELECT 1 FROM syscolumn JOIN systable WHERE table_name = 'WM_Validation_Code' AND column_name = 'Alias' AND syscolumn.nulls = 'N') THEN ALTER TABLE "DBA".WM_Validation_Code MODIFY Alias NULL; IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; ALTER TABLE "DBA".WM_Validation_Code MODIFY Alias NULL; PASSTHROUGH STOP; END IF; END IF; /*********************************************************************/ /* 4/05/2007 - Update WM_VisualFrame_TempAccountGrid to return better info */ /*********************************************************************/ IF NOT EXISTS( SELECT 1 FROM "DBA".SM_Version_Control WHERE Entity_Name = 'WM_VisualFrame_TempAccountGrid' AND Database_ID = (SELECT db_property('GlobalDbID'))) THEN ALTER PROCEDURE "DBA".WM_VisualFrame_TempAccountGrid(IN @VehicleCode Entity_Code, IN @CustomerNo Entity_No) RESULT( Temp_Account_No Entity_No, Customer Entity_Name, Ref_No VARCHAR(40), Created Entity_DateTime, Payments INTEGER, Total NUMERIC(8,2)) BEGIN --Vehicle Code must be specified as a minimum IF @VehicleCode IS NULL THEN RAISERROR 30000 'Vehicle Code must be specified'; END IF; SELECT TA.Temp_Account_No, C.Name AS Customer, TA.Operator_Reference AS Ref_No, TA.DateTime_Created AS Created, ISNULL(PC.Payments, 0) AS Payments, ISNULL(PC.Total, 0) AS Total FROM WM_Temp_Account TA INNER JOIN WM_Customer C ON (C.Customer_No = TA.Customer_No) LEFT OUTER JOIN ( SELECT TApc.Temp_Account_No, COUNT(Ppc.Payment_No) AS Payments, SUM(Ppc.Amount_Paid) AS Total FROM WM_Temp_Account TApc INNER JOIN WM_Customer Cpc ON (Cpc.Customer_No = TApc.Customer_No) INNER JOIN WM_Payment Ppc ON (TApc.Temp_Account_No = Ppc.Temp_Account_No) WHERE (TApc.Available = 'Y') --show only Available Temp Accounts AND (TApc.Site_No = (SELECT SM_GetLocalSiteNo())) --only Temp Accounts for this site AND ((TApc.Customer_No = @CustomerNo) OR ((@CustomerNo IS NULL) AND (TApc.Vehicle_Code = @VehicleCode))) AND ((TApc.Vehicle_Code = @VehicleCode) OR ((@CustomerNo IS NOT NULL) AND (Cpc.Is_Debtor = 'Y'))) --Eliminate reversed payments and reversals as they cause a misleading payment count AND (Ppc.Reference_Payment_No IS NULL) -- i.e. Not a Reversal AND (NOT EXISTS(SELECT 1 FROM WM_Payment P1pc WHERE P1pc.Reference_Payment_No = Ppc.Payment_No)) -- i.e. Has not been reversed GROUP BY TApc.Temp_Account_No ) PC ON (PC.Temp_Account_No = TA.Temp_Account_No) WHERE (TA.Available = 'Y') --show only Available Temp Accounts AND (TA.Site_No = (SELECT SM_GetLocalSiteNo())) --only Temp Accounts for this site --If @CustomerNo is NULL, can show only Temp Accounts that nominate the vehicle --If @CustomerNo is not NULL, show all Temp Accounts for the Customer if it is a Debtor, -- otherwise show just the Temp Accounts for the Customer and Vehicle AND ((TA.Customer_No = @CustomerNo) OR ((@CustomerNo IS NULL) AND (TA.Vehicle_Code = @VehicleCode))) AND ((TA.Vehicle_Code = @VehicleCode) OR ((@CustomerNo IS NOT NULL) AND (C.Is_Debtor = 'Y'))) ORDER BY TA.DateTime_Created; END; IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; ALTER PROCEDURE "DBA".WM_VisualFrame_TempAccountGrid(IN @VehicleCode Entity_Code, IN @CustomerNo Entity_No) RESULT( Temp_Account_No Entity_No, Customer Entity_Name, Ref_No VARCHAR(40), Created Entity_DateTime, Payments INTEGER, Total NUMERIC(8,2)) BEGIN --Vehicle Code must be specified as a minimum IF @VehicleCode IS NULL THEN RAISERROR 30000 'Vehicle Code must be specified'; END IF; SELECT TA.Temp_Account_No, C.Name AS Customer, TA.Operator_Reference AS Ref_No, TA.DateTime_Created AS Created, ISNULL(PC.Payments, 0) AS Payments, ISNULL(PC.Total, 0) AS Total FROM WM_Temp_Account TA INNER JOIN WM_Customer C ON (C.Customer_No = TA.Customer_No) LEFT OUTER JOIN ( SELECT TApc.Temp_Account_No, COUNT(Ppc.Payment_No) AS Payments, SUM(Ppc.Amount_Paid) AS Total FROM WM_Temp_Account TApc INNER JOIN WM_Customer Cpc ON (Cpc.Customer_No = TApc.Customer_No) INNER JOIN WM_Payment Ppc ON (TApc.Temp_Account_No = Ppc.Temp_Account_No) WHERE (TApc.Available = 'Y') --show only Available Temp Accounts AND (TApc.Site_No = (SELECT SM_GetLocalSiteNo())) --only Temp Accounts for this site AND ((TApc.Customer_No = @CustomerNo) OR ((@CustomerNo IS NULL) AND (TApc.Vehicle_Code = @VehicleCode))) AND ((TApc.Vehicle_Code = @VehicleCode) OR ((@CustomerNo IS NOT NULL) AND (Cpc.Is_Debtor = 'Y'))) --Eliminate reversed payments and reversals as they cause a misleading payment count AND (Ppc.Reference_Payment_No IS NULL) -- i.e. Not a Reversal AND (NOT EXISTS(SELECT 1 FROM WM_Payment P1pc WHERE P1pc.Reference_Payment_No = Ppc.Payment_No)) -- i.e. Has not been reversed GROUP BY TApc.Temp_Account_No ) PC ON (PC.Temp_Account_No = TA.Temp_Account_No) WHERE (TA.Available = 'Y') --show only Available Temp Accounts AND (TA.Site_No = (SELECT SM_GetLocalSiteNo())) --only Temp Accounts for this site --If @CustomerNo is NULL, can show only Temp Accounts that nominate the vehicle --If @CustomerNo is not NULL, show all Temp Accounts for the Customer if it is a Debtor, -- otherwise show just the Temp Accounts for the Customer and Vehicle AND ((TA.Customer_No = @CustomerNo) OR ((@CustomerNo IS NULL) AND (TA.Vehicle_Code = @VehicleCode))) AND ((TA.Vehicle_Code = @VehicleCode) OR ((@CustomerNo IS NOT NULL) AND (C.Is_Debtor = 'Y'))) ORDER BY TA.DateTime_Created; END; --Add version control rec INSERT "DBA".SM_Version_Control(Entity_Name, Database_ID, Version_No) VALUES('WM_VisualFrame_TempAccountGrid', (SELECT db_property('GlobalDBId')), 1); PASSTHROUGH STOP; END IF; --Add version control rec INSERT "DBA".SM_Version_Control(Entity_Name, Database_ID, Version_No) VALUES('WM_VisualFrame_TempAccountGrid', (SELECT db_property('GlobalDBId')), 1); END IF; /*********************************************************************/ /* 4/05/2007 - Add Temp Account list view */ /*********************************************************************/ IF NOT EXISTS( SELECT 1 FROM systable WHERE table_name = 'Default_View_0019' AND table_type = 'VIEW') THEN CREATE VIEW "DBA".Default_View_0019 AS SELECT TA.Operator_Reference AS Account_Ref, TA.DateTime_Created AS Created, TA.Customer_Name, TA.Vehicle_Code, TA.Cheque_No, ISNULL(COUNT(P.Payment_No), 0) AS Payments, ISNULL(SUM(P.Amount_Paid), 0) AS Total, TA.Credit_Limit, TA.Expiry_Date, TA.Temp_Account_No FROM "DBA".WM_Temp_Account TA INNER JOIN "DBA".WM_Customer C ON (C.Customer_No = TA.Customer_No) INNER JOIN "DBA".SM_Site S ON (S.Site_No = TA.Site_No) INNER JOIN "DBA".WM_Shift SH ON (SH.Shift_No = TA.Shift_No) LEFT OUTER JOIN "DBA".WM_Payment P ON (TA.Temp_Account_No = P.Temp_Account_No) WHERE (TA.Available = 'Y') --only Available Temp Accounts AND (TA.Site_No = (SELECT "DBA".SM_GetLocalSiteNo())) --only Temp Accounts for this site --Eliminate reversed payments and reversals as they cause a misleading payment count AND (P.Reference_Payment_No IS NULL) -- i.e. Not a Reversal AND (NOT EXISTS(SELECT 1 FROM "DBA".WM_Payment P1 WHERE P1.Reference_Payment_No = P.Payment_No)) -- i.e. Has not been reversed --Eliminate refunded payments and linked refunds as they cause a misleading payment count AND (P.Refund_Payment_No IS NULL) -- i.e. has not been refunded AND (NOT EXISTS(SELECT 1 FROM "DBA".WM_Payment P1 WHERE P1.Refund_Payment_No = P.Payment_No)) -- i.e. is not a refund GROUP BY S.Site_Code, SH.Station_ID, TA.Temp_Account_No, TA.Operator_Reference, TA.DateTime_Created, TA.Vehicle_Code, TA.Customer_Name, TA.Cheque_No, TA.Credit_Limit, TA.Expiry_Date; IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; CREATE VIEW "DBA".Default_View_0019 AS SELECT TA.Operator_Reference AS Account_Ref, TA.DateTime_Created AS Created, TA.Customer_Name, TA.Vehicle_Code, TA.Cheque_No, ISNULL(COUNT(P.Payment_No), 0) AS Payments, ISNULL(SUM(P.Amount_Paid), 0) AS Total, TA.Credit_Limit, TA.Expiry_Date, TA.Temp_Account_No FROM "DBA".WM_Temp_Account TA INNER JOIN "DBA".WM_Customer C ON (C.Customer_No = TA.Customer_No) INNER JOIN "DBA".SM_Site S ON (S.Site_No = TA.Site_No) INNER JOIN "DBA".WM_Shift SH ON (SH.Shift_No = TA.Shift_No) LEFT OUTER JOIN "DBA".WM_Payment P ON (TA.Temp_Account_No = P.Temp_Account_No) WHERE (TA.Available = 'Y') --only Available Temp Accounts AND (TA.Site_No = (SELECT "DBA".SM_GetLocalSiteNo())) --only Temp Accounts for this site --Eliminate reversed payments and reversals as they cause a misleading payment count AND (P.Reference_Payment_No IS NULL) -- i.e. Not a Reversal AND (NOT EXISTS(SELECT 1 FROM "DBA".WM_Payment P1 WHERE P1.Reference_Payment_No = P.Payment_No)) -- i.e. Has not been reversed --Eliminate refunded payments and linked refunds as they cause a misleading payment count AND (P.Refund_Payment_No IS NULL) -- i.e. has not been refunded AND (NOT EXISTS(SELECT 1 FROM "DBA".WM_Payment P1 WHERE P1.Refund_Payment_No = P.Payment_No)) -- i.e. is not a refund GROUP BY S.Site_Code, SH.Station_ID, TA.Temp_Account_No, TA.Operator_Reference, TA.DateTime_Created, TA.Vehicle_Code, TA.Customer_Name, TA.Cheque_No, TA.Credit_Limit, TA.Expiry_Date; PASSTHROUGH STOP; END IF; END IF; /*********************************************************************/ /* 18/05/2007 - Add proc to return a Cheque List for the cash summary */ /*********************************************************************/ IF NOT EXISTS( SELECT 1 FROM sysprocedure WHERE proc_name = 'WM_BaseFrame_CashSummaryChqList') THEN CREATE PROCEDURE "DBA".WM_BaseFrame_CashSummaryChqList(IN @ShiftNo Entity_No) RESULT( Cheque_No Cheque_No, Customer_Name Entity_Name, Cheque_BSB Cheque_BSB, Cheque_Account Cheque_Account, Cheque_Total NUMERIC(8,2)) BEGIN SELECT Cheque_No, Customer_Name, Cheque_BSB, Cheque_Account, SUM(Amount_Paid) AS Cheque_Total FROM WM_Payment WHERE (Shift_No = @ShiftNo) AND (RTRIM(ISNULL(Cheque_No, '')) <> '') GROUP BY Cheque_No, Customer_Name, Cheque_BSB, Cheque_Account ORDER BY Cheque_No, Cheque_BSB, Cheque_Account; END; IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; CREATE PROCEDURE "DBA".WM_BaseFrame_CashSummaryChqList(IN @ShiftNo Entity_No) RESULT( Cheque_No Cheque_No, Customer_Name Entity_Name, Cheque_BSB Cheque_BSB, Cheque_Account Cheque_Account, Cheque_Total NUMERIC(8,2)) BEGIN SELECT Cheque_No, Customer_Name, Cheque_BSB, Cheque_Account, SUM(Amount_Paid) AS Cheque_Total FROM WM_Payment WHERE (Shift_No = @ShiftNo) AND (RTRIM(ISNULL(Cheque_No, '')) <> '') GROUP BY Cheque_No, Customer_Name, Cheque_BSB, Cheque_Account ORDER BY Cheque_No, Cheque_BSB, Cheque_Account; END; PASSTHROUGH STOP; END IF; END IF; /*********************************************************************/ /* 21/05/2007 - Update WM_BaseFrame_PrintTempAccTotals proc to return better info */ /*********************************************************************/ IF NOT EXISTS( SELECT 1 FROM "DBA".SM_Version_Control WHERE Entity_Name = 'WM_BaseFrame_PrintTempAccTotals' AND Database_ID = (SELECT db_property('GlobalDbID'))) THEN ALTER PROCEDURE "DBA".WM_BaseFrame_PrintTempAccTotals(IN @TempAccountNo Entity_No) RESULT( Operator_Reference VARCHAR(40), Credit_Limit NUMERIC(8,2), Expiry_Date Entity_Date, Payment_Count INTEGER, Amount_Paid NUMERIC(8,2), Amount_Allocated NUMERIC(8,2), Amount_Unallocated NUMERIC(8,2)) BEGIN SELECT T.Operator_Reference, T.Credit_Limit, T.Expiry_Date, COUNT(P.Temp_Account_No) AS Payment_Count, SUM(P.Amount_Paid) AS Amount_Paid, SUM(P.Amount_Allocated) AS Amount_Allocated, (SUM(P.Amount_Paid) - SUM(P.Amount_Allocated)) AS Amount_Unallocated FROM WM_Payment P INNER JOIN WM_Temp_Account T ON (T.Temp_Account_No = P.Temp_Account_No) WHERE P.Temp_Account_No = @TempAccountNo AND P.Reference_Payment_No IS NULL -- i.e. Not a Reversal AND NOT EXISTS(SELECT 1 FROM WM_Payment P1 WHERE P1.Reference_Payment_No = P.Payment_No) -- i.e. Has not been reversed GROUP BY P.Temp_Account_No, T.Operator_Reference, T.Credit_Limit, T.Expiry_Date; END; IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; ALTER PROCEDURE "DBA".WM_BaseFrame_PrintTempAccTotals(IN @TempAccountNo Entity_No) RESULT( Operator_Reference VARCHAR(40), Credit_Limit NUMERIC(8,2), Expiry_Date Entity_Date, Payment_Count INTEGER, Amount_Paid NUMERIC(8,2), Amount_Allocated NUMERIC(8,2), Amount_Unallocated NUMERIC(8,2)) BEGIN SELECT T.Operator_Reference, T.Credit_Limit, T.Expiry_Date, COUNT(P.Temp_Account_No) AS Payment_Count, SUM(P.Amount_Paid) AS Amount_Paid, SUM(P.Amount_Allocated) AS Amount_Allocated, (SUM(P.Amount_Paid) - SUM(P.Amount_Allocated)) AS Amount_Unallocated FROM WM_Payment P INNER JOIN WM_Temp_Account T ON (T.Temp_Account_No = P.Temp_Account_No) WHERE P.Temp_Account_No = @TempAccountNo AND P.Reference_Payment_No IS NULL -- i.e. Not a Reversal AND NOT EXISTS(SELECT 1 FROM WM_Payment P1 WHERE P1.Reference_Payment_No = P.Payment_No) -- i.e. Has not been reversed GROUP BY P.Temp_Account_No, T.Operator_Reference, T.Credit_Limit, T.Expiry_Date; END; INSERT "DBA".SM_Version_Control(Entity_Name, Database_ID, Version_No) VALUES('WM_BaseFrame_PrintTempAccTotals', (SELECT db_property('GlobalDBId')), 1); PASSTHROUGH STOP; END IF; INSERT "DBA".SM_Version_Control(Entity_Name, Database_ID, Version_No) VALUES('WM_BaseFrame_PrintTempAccTotals', (SELECT db_property('GlobalDBId')), 1); END IF; /*********************************************************************/ /* 21/05/2007 - Update WM_BaseFrame_PrintTempAccItem proc to return better info */ /*********************************************************************/ IF NOT EXISTS( SELECT 1 FROM "DBA".SM_Version_Control WHERE Entity_Name = 'WM_BaseFrame_PrintTempAccItem' AND Database_ID = (SELECT db_property('GlobalDbID'))) THEN ALTER PROCEDURE "DBA".WM_BaseFrame_PrintTempAccItem(IN @TempAccountNo Entity_No) RESULT( Payment_Date Entity_Date, Payment_Time Entity_Time, Payment_No Large_Entity_No, Amount_Paid NUMERIC(8,2), Amount_Allocated NUMERIC(8,2), Tran_Docket VARCHAR(20), Tran_Vehicle_Code Entity_Code, Tran_Operator Operator_Code, Tran_Amount_Allocated NUMERIC(8,2)) BEGIN SELECT P.Payment_Date, P.Payment_Time, P.Payment_No, P.Amount_Paid, P.Amount_Allocated, H.Docket, H.Vehicle_Code, --must use tran vehicle as payment vehicle will be NULL if customer is a debtor H.Created_By, SUM(TP.Amount) AS Tran_Allocated FROM WM_Payment P LEFT OUTER JOIN WM_Transaction_Payment TP ON (TP.Payment_No = P.Payment_No) LEFT OUTER JOIN WM_Transaction_Header H ON (H.Transaction_No = TP.Transaction_No) WHERE P.Temp_Account_No = @TempAccountNo AND P.Reference_Payment_No IS NULL -- i.e. Not a Reversal AND NOT EXISTS(SELECT 1 FROM WM_Payment P1 WHERE P1.Reference_Payment_No = P.Payment_No) -- i.e. Has not been reversed GROUP BY P.Payment_Date, P.Payment_Time, P.Payment_No, P.Amount_Paid, P.Amount_Allocated, H.Docket, H.Vehicle_Code, H.Created_By ORDER BY P.Payment_Date, P.Payment_Time, P.Payment_No, H.Docket; END; IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; ALTER PROCEDURE "DBA".WM_BaseFrame_PrintTempAccItem(IN @TempAccountNo Entity_No) RESULT( Payment_Date Entity_Date, Payment_Time Entity_Time, Payment_No Large_Entity_No, Amount_Paid NUMERIC(8,2), Amount_Allocated NUMERIC(8,2), Tran_Docket VARCHAR(20), Tran_Vehicle_Code Entity_Code, Tran_Operator Operator_Code, Tran_Amount_Allocated NUMERIC(8,2)) BEGIN SELECT P.Payment_Date, P.Payment_Time, P.Payment_No, P.Amount_Paid, P.Amount_Allocated, H.Docket, H.Vehicle_Code, --must use tran vehicle as payment vehicle will be NULL if customer is a debtor H.Created_By, SUM(TP.Amount) AS Tran_Allocated FROM WM_Payment P LEFT OUTER JOIN WM_Transaction_Payment TP ON (TP.Payment_No = P.Payment_No) LEFT OUTER JOIN WM_Transaction_Header H ON (H.Transaction_No = TP.Transaction_No) WHERE P.Temp_Account_No = @TempAccountNo AND P.Reference_Payment_No IS NULL -- i.e. Not a Reversal AND NOT EXISTS(SELECT 1 FROM WM_Payment P1 WHERE P1.Reference_Payment_No = P.Payment_No) -- i.e. Has not been reversed GROUP BY P.Payment_Date, P.Payment_Time, P.Payment_No, P.Amount_Paid, P.Amount_Allocated, H.Docket, H.Vehicle_Code, H.Created_By ORDER BY P.Payment_Date, P.Payment_Time, P.Payment_No, H.Docket; END; INSERT "DBA".SM_Version_Control(Entity_Name, Database_ID, Version_No) VALUES('WM_BaseFrame_PrintTempAccItem', (SELECT db_property('GlobalDBId')), 1); PASSTHROUGH STOP; END IF; INSERT "DBA".SM_Version_Control(Entity_Name, Database_ID, Version_No) VALUES('WM_BaseFrame_PrintTempAccItem', (SELECT db_property('GlobalDBId')), 1); END IF; /*********************************************************************/ /* 21/05/2007 - Add proc to return a Short Payment List for the cash summary */ /*********************************************************************/ IF NOT EXISTS( SELECT 1 FROM sysprocedure WHERE proc_name = 'WM_BaseFrame_CashSummShortPayList') THEN CREATE PROCEDURE "DBA".WM_BaseFrame_CashSummShortPayList(IN @ShiftNo Entity_No) RESULT( Transaction_No Large_Entity_No, Docket VARCHAR(20), Date_Complete Entity_Date, Time_Complete Entity_Time, Vehicle_Code Entity_Code, Customer_Code Entity_Code, Customer_Name Entity_Name, Operator Operator_Code, Transaction_Amount NUMERIC(8,2), Amount_Paid NUMERIC(8,2)) BEGIN SELECT H.Transaction_No, H.Docket, H.Date_Complete, H.Time_Complete, H.Vehicle_Code, C.Code AS Customer_Code, C.Name AS Customer_Name, H.Created_By AS Operator, H.Transaction_Amount, H.Amount_Paid FROM WM_Transaction_Header H INNER JOIN WM_Customer C ON (C.Customer_No = H.Customer_No) WHERE H.Shift_No = @ShiftNo AND H.Is_Completed = 'Y' AND H.Is_Fully_Paid = 'N' END; IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; CREATE PROCEDURE "DBA".WM_BaseFrame_CashSummShortPayList(IN @ShiftNo Entity_No) RESULT( Transaction_No Large_Entity_No, Docket VARCHAR(20), Date_Complete Entity_Date, Time_Complete Entity_Time, Vehicle_Code Entity_Code, Customer_Code Entity_Code, Customer_Name Entity_Name, Operator Operator_Code, Transaction_Amount NUMERIC(8,2), Amount_Paid NUMERIC(8,2)) BEGIN SELECT H.Transaction_No, H.Docket, H.Date_Complete, H.Time_Complete, H.Vehicle_Code, C.Code AS Customer_Code, C.Name AS Customer_Name, H.Created_By AS Operator, H.Transaction_Amount, H.Amount_Paid FROM WM_Transaction_Header H INNER JOIN WM_Customer C ON (C.Customer_No = H.Customer_No) WHERE H.Shift_No = @ShiftNo AND H.Is_Completed = 'Y' AND H.Is_Fully_Paid = 'N' END; PASSTHROUGH STOP; END IF; END IF; /*********************************************************************/ /* 21/05/2007 - Add proc to return a Unallocated Payment List for the cash summary */ /*********************************************************************/ IF NOT EXISTS( SELECT 1 FROM sysprocedure WHERE proc_name = 'WM_BaseFrame_CashSummUnallocPayList') THEN CREATE PROCEDURE "DBA".WM_BaseFrame_CashSummUnallocPayList(IN @ShiftNo Entity_No) RESULT( Payment_No Large_Entity_No, Payment_Date Entity_Date, Payment_Time Entity_Time, Customer_Code Entity_Code, Customer_Name Entity_Name, Vehicle_Code Entity_Code, Pay_Method Entity_Name, Amount_Paid NUMERIC(8,2), Amount_Allocated NUMERIC(8,2)) BEGIN SELECT P.Payment_No, P.Payment_Date, P.Payment_Time, C.Code AS Customer_Code, C.Name AS Customer_Name, P.Vehicle_Code, M.Method AS Pay_Method, P.Amount_Paid, P.Amount_Allocated FROM WM_Payment P INNER JOIN WM_Customer C ON (C.Customer_No = P.Customer_No) INNER JOIN WM_Payment_Method M ON (M.Pay_Method_No = P.Pay_Method_No) WHERE P.Shift_No = @ShiftNo AND M.Include_On_Cash_Summary = 'Y' AND P.Is_Fully_Allocated = 'N'; END; IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; CREATE PROCEDURE "DBA".WM_BaseFrame_CashSummUnallocPayList(IN @ShiftNo Entity_No) RESULT( Payment_No Large_Entity_No, Payment_Date Entity_Date, Payment_Time Entity_Time, Customer_Code Entity_Code, Customer_Name Entity_Name, Vehicle_Code Entity_Code, Pay_Method Entity_Name, Amount_Paid NUMERIC(8,2), Amount_Allocated NUMERIC(8,2)) BEGIN SELECT P.Payment_No, P.Payment_Date, P.Payment_Time, C.Code AS Customer_Code, C.Name AS Customer_Name, P.Vehicle_Code, M.Method AS Pay_Method, P.Amount_Paid, P.Amount_Allocated FROM WM_Payment P INNER JOIN WM_Customer C ON (C.Customer_No = P.Customer_No) INNER JOIN WM_Payment_Method M ON (M.Pay_Method_No = P.Pay_Method_No) WHERE P.Shift_No = @ShiftNo AND M.Include_On_Cash_Summary = 'Y' AND P.Is_Fully_Allocated = 'N'; END; PASSTHROUGH STOP; END IF; END IF; /*********************************************************************/ /* 22/05/2007 - Add WM_Temp_Account_Defaults table */ /*********************************************************************/ IF NOT EXISTS( SELECT 1 FROM systable WHERE table_name = 'WM_Temp_Account_Defaults' AND table_type = 'BASE') THEN BEGIN CREATE TABLE "DBA".WM_Temp_Account_Defaults ( Temp_Account_Default_No Large_Identity_No not null, Vehicle_Code Entity_Code not null, Site_No Entity_No not null, Customer_Name Entity_Name, Customer_ABN ABN, Customer_Address1 Entity_Address, Customer_Address2 Entity_Address, Customer_Address3 Entity_Address, Customer_State Australian_State constraint CKC_CUSTOMER_STATE_WM_TEMP_ check (Customer_State is null or ( Customer_State in ('NSW','VIC','SA','WA','TAS','QLD','NT','ACT') )), Customer_PostCode Postcode, Cheque_BSB Cheque_BSB, Cheque_Account Cheque_Account, Cheque_Bank Cheque_Bank, Cheque_Drawer Cheque_Drawer, Cheque_Drawer_License Cheque_Drawer_License, Available Boolean not null constraint CKC_AVAILABLE_WM_TEMP_ check (Available in ('Y','N')), DateTime_Created Entity_DateTime_Def_Current not null, DateTime_Modified Entity_DateTime_Def_Current not null, User_Created varchar(50), User_Modified varchar(50), constraint PK_WM_TEMP_ACCOUNT_DEFAULTS primary key (Temp_Account_Default_No) ); alter table "DBA".WM_Temp_Account_Defaults add constraint AK_KEY_2_WM_TEMP_ACC_DEF unique (Vehicle_Code, Site_No); alter table "DBA".WM_Temp_Account_Defaults add constraint FK_WM_TEMPACC_DEF_REF_SM_SITE foreign key (Site_No) references "DBA".SM_Site (Site_No) on update restrict on delete cascade; CREATE TRIGGER bi_WM_Temp_Account_Defaults BEFORE INSERT ON "DBA".WM_Temp_Account_Defaults REFERENCING NEW AS new_Row FOR EACH ROW BEGIN SET new_Row.User_Created = (SELECT SM_UC_GetConnectionAppUserID()); SET new_Row.User_Modified = (SELECT SM_UC_GetConnectionAppUserID()); END; CREATE TRIGGER bu_WM_Temp_Account_Defaults BEFORE UPDATE ON "DBA".WM_Temp_Account_Defaults REFERENCING NEW AS new_Row FOR EACH ROW BEGIN SET new_Row.DateTime_Modified = GetDate(); SET new_Row.User_Modified = (SELECT SM_UC_GetConnectionAppUserID()); END; --Passthrough the update to the remote sites (if in a replicated environment) IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN --Add the table to the publication ALTER PUBLICATION "DBA".WM_Pub_All_Nodes ADD TABLE "DBA".WM_Temp_Account_Defaults; PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; CREATE TABLE "DBA".WM_Temp_Account_Defaults ( Temp_Account_Default_No Large_Identity_No not null, Vehicle_Code Entity_Code not null, Site_No Entity_No not null, Customer_Name Entity_Name, Customer_ABN ABN, Customer_Address1 Entity_Address, Customer_Address2 Entity_Address, Customer_Address3 Entity_Address, Customer_State Australian_State constraint CKC_CUSTOMER_STATE_WM_TEMP_ check (Customer_State is null or ( Customer_State in ('NSW','VIC','SA','WA','TAS','QLD','NT','ACT') )), Customer_PostCode Postcode, Cheque_BSB Cheque_BSB, Cheque_Account Cheque_Account, Cheque_Bank Cheque_Bank, Cheque_Drawer Cheque_Drawer, Cheque_Drawer_License Cheque_Drawer_License, Available Boolean not null constraint CKC_AVAILABLE_WM_TEMP_ check (Available in ('Y','N')), DateTime_Created Entity_DateTime_Def_Current not null, DateTime_Modified Entity_DateTime_Def_Current not null, User_Created varchar(50), User_Modified varchar(50), constraint PK_WM_TEMP_ACCOUNT_DEFAULTS primary key (Temp_Account_Default_No) ); alter table "DBA".WM_Temp_Account_Defaults add constraint AK_KEY_2_WM_TEMP_ACC_DEF unique (Vehicle_Code, Site_No); alter table "DBA".WM_Temp_Account_Defaults add constraint FK_WM_TEMPACC_DEF_REF_SM_SITE foreign key (Site_No) references "DBA".SM_Site (Site_No) on update restrict on delete cascade; CREATE TRIGGER bi_WM_Temp_Account_Defaults BEFORE INSERT ON "DBA".WM_Temp_Account_Defaults REFERENCING NEW AS new_Row FOR EACH ROW BEGIN SET new_Row.User_Created = (SELECT SM_UC_GetConnectionAppUserID()); SET new_Row.User_Modified = (SELECT SM_UC_GetConnectionAppUserID()); END; CREATE TRIGGER bu_WM_Temp_Account_Defaults BEFORE UPDATE ON "DBA".WM_Temp_Account_Defaults REFERENCING NEW AS new_Row FOR EACH ROW BEGIN SET new_Row.DateTime_Modified = GetDate(); SET new_Row.User_Modified = (SELECT SM_UC_GetConnectionAppUserID()); END; --Add the table to the publication ALTER PUBLICATION "DBA".WM_Pub_All_Nodes ADD TABLE "DBA".WM_Temp_Account_Defaults; PASSTHROUGH STOP; END IF; END; END IF; /*********************************************************************/ /* 23/05/2007 - Add email configuration fields to WM_Mobile_Device */ /*********************************************************************/ IF NOT EXISTS( SELECT 1 FROM syscolumn JOIN systable WHERE table_name = 'WM_Mobile_Device' AND column_name = 'Email_Account_Name') THEN ALTER TABLE "DBA".WM_Mobile_Device ADD Email_Account_Name VARCHAR(50); IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; ALTER TABLE "DBA".WM_Mobile_Device ADD Email_Account_Name VARCHAR(50); PASSTHROUGH STOP; END IF; END IF; IF NOT EXISTS( SELECT 1 FROM syscolumn JOIN systable WHERE table_name = 'WM_Mobile_Device' AND column_name = 'Email_Recipient_Address') THEN ALTER TABLE "DBA".WM_Mobile_Device ADD Email_Recipient_Address VARCHAR(250); IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; ALTER TABLE "DBA".WM_Mobile_Device ADD Email_Recipient_Address VARCHAR(250); PASSTHROUGH STOP; END IF; END IF; IF NOT EXISTS( SELECT 1 FROM syscolumn JOIN systable WHERE table_name = 'WM_Mobile_Device' AND column_name = 'Email_Subject_Line') THEN ALTER TABLE "DBA".WM_Mobile_Device ADD Email_Subject_Line VARCHAR(100); IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; ALTER TABLE "DBA".WM_Mobile_Device ADD Email_Subject_Line VARCHAR(100); PASSTHROUGH STOP; END IF; --Add the default subject line to existing devices (update will replicate normally) UPDATE "DBA".WM_Mobile_Device SET Email_Subject_Line = 'WasteMan 2G Mobile Transactions' WHERE ISNULL(Email_Subject_Line, '') = ''; END IF; IF NOT EXISTS( SELECT 1 FROM syscolumn JOIN systable WHERE table_name = 'WM_Mobile_Device' AND column_name = 'Email_Purge_Days') THEN ALTER TABLE "DBA".WM_Mobile_Device ADD Email_Purge_Days SMALLINT; IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; ALTER TABLE "DBA".WM_Mobile_Device ADD Email_Purge_Days SMALLINT; PASSTHROUGH STOP; END IF; --Add the default value to existing devices (update will replicate normally) UPDATE "DBA".WM_Mobile_Device SET Email_Purge_Days = 14 WHERE Email_Purge_Days IS NULL; END IF; /*********************************************************************/ /* 25/05/2007 - Add field to Job Auth Product to allow products to */ /* be hidden on DCS */ /*********************************************************************/ IF NOT EXISTS( SELECT 1 FROM syscolumn JOIN systable WHERE table_name = 'WM_Job_Auth_Product' AND column_name = 'Display_On_Dcs') THEN --Field is NOT NULL in schema but is added as NULL-permitting to support DBMS < 9.0.2 ALTER TABLE "DBA".WM_Job_Auth_Product ADD Display_On_Dcs Boolean NULL DEFAULT 'Y'; IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; ALTER TABLE "DBA".WM_Job_Auth_Product ADD Display_On_Dcs Boolean NULL DEFAULT 'Y'; PASSTHROUGH STOP; END IF; END IF; /*********************************************************************/ /* 28/05/2007 - Update proc used to summarise Temp Account details for closing */ /* NOTE: Name has been changed, old is dropped, new added */ /*********************************************************************/ IF EXISTS( SELECT 1 FROM sysprocedure WHERE proc_name = 'WM_BuildTempAccTranItemList') THEN DROP PROCEDURE "DBA".WM_BuildTempAccTranItemList; IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; DROP PROCEDURE "DBA".WM_BuildTempAccTranItemList; PASSTHROUGH STOP; END IF; END IF; IF NOT EXISTS( SELECT 1 FROM sysprocedure WHERE proc_name = 'WM_TempAccountPayments') THEN CREATE PROCEDURE "DBA".WM_TempAccountPayments(IN @TempAccountNo Entity_No) RESULT( Payment_Date Entity_Date, Payment_Time Entity_Time, Payment_No Large_Entity_No, Amount_Paid NUMERIC(8,2), Amount_Allocated NUMERIC(8,2), Transaction_No Large_Entity_No, Tran_Docket VARCHAR(20), Tran_Vehicle_Code Entity_Code, Tran_Operator Operator_Code, Tran_Amount_Allocated NUMERIC(8,2)) BEGIN SELECT P.Payment_Date, P.Payment_Time, P.Payment_No, P.Amount_Paid, P.Amount_Allocated, H.Transaction_No, H.Docket, H.Vehicle_Code, --must use tran vehicle as payment vehicle will be NULL if customer is a debtor H.Created_By, SUM(TP.Amount) FROM WM_Payment P --NOTE: Only concerned with payments allocated to transactions so use an INNER join INNER JOIN WM_Transaction_Payment TP ON (TP.Payment_No = P.Payment_No) INNER JOIN WM_Transaction_Header H ON (H.Transaction_No = TP.Transaction_No) WHERE P.Temp_Account_No = @TempAccountNo AND P.Reference_Payment_No IS NULL -- i.e. Not a Reversal AND NOT EXISTS(SELECT 1 FROM WM_Payment P1 WHERE P1.Reference_Payment_No = P.Payment_No) -- i.e. Has not been reversed GROUP BY P.Payment_Date, P.Payment_Time, P.Payment_No, P.Amount_Paid, P.Amount_Allocated, H.Transaction_No, H.Docket, H.Vehicle_Code, H.Created_By ORDER BY P.Payment_Date, P.Payment_Time, P.Payment_No, H.Docket END; IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; CREATE PROCEDURE "DBA".WM_TempAccountPayments(IN @TempAccountNo Entity_No) RESULT( Payment_Date Entity_Date, Payment_Time Entity_Time, Payment_No Large_Entity_No, Amount_Paid NUMERIC(8,2), Amount_Allocated NUMERIC(8,2), Transaction_No Large_Entity_No, Tran_Docket VARCHAR(20), Tran_Vehicle_Code Entity_Code, Tran_Operator Operator_Code, Tran_Amount_Allocated NUMERIC(8,2)) BEGIN SELECT P.Payment_Date, P.Payment_Time, P.Payment_No, P.Amount_Paid, P.Amount_Allocated, H.Transaction_No, H.Docket, H.Vehicle_Code, --must use tran vehicle as payment vehicle will be NULL if customer is a debtor H.Created_By, SUM(TP.Amount) FROM WM_Payment P --NOTE: Only concerned with payments allocated to transactions so use an INNER join INNER JOIN WM_Transaction_Payment TP ON (TP.Payment_No = P.Payment_No) INNER JOIN WM_Transaction_Header H ON (H.Transaction_No = TP.Transaction_No) WHERE P.Temp_Account_No = @TempAccountNo AND P.Reference_Payment_No IS NULL -- i.e. Not a Reversal AND NOT EXISTS(SELECT 1 FROM WM_Payment P1 WHERE P1.Reference_Payment_No = P.Payment_No) -- i.e. Has not been reversed GROUP BY P.Payment_Date, P.Payment_Time, P.Payment_No, P.Amount_Paid, P.Amount_Allocated, H.Transaction_No, H.Docket, H.Vehicle_Code, H.Created_By ORDER BY P.Payment_Date, P.Payment_Time, P.Payment_No, H.Docket END; PASSTHROUGH STOP; END IF; END IF; /*********************************************************************/ /* 28/05/2007 - Add stored proc to delete temp account and its payments */ /*********************************************************************/ IF NOT EXISTS( SELECT 1 FROM sysprocedure WHERE proc_name = 'WM_DropTempAccount') THEN CREATE PROCEDURE "DBA".WM_DropTempAccount(IN @TempAccountNo Entity_No) BEGIN DECLARE LOCAL TEMPORARY TABLE @TranAllocations( Transaction_No UNSIGNED BIGINT, Total_Allocated NUMERIC(12,2)); --NOTE: No transaction is started by this procedure --It must be run from within a transaction to prevent partial changes from being made --Get total allocated to each transaction from temp account payments INSERT @TranAllocations (Transaction_No, Total_Allocated) SELECT H.Transaction_No, SUM(TP.Amount) FROM WM_Transaction_Header H INNER JOIN WM_Transaction_Payment TP ON (TP.Transaction_No = H.Transaction_No) INNER JOIN WM_Payment P ON (P.Payment_No = TP.Payment_No) WHERE P.Temp_Account_No = @TempAccountNo GROUP BY H.Transaction_No; --Drop payment allocations DELETE WM_Transaction_Payment FROM WM_Transaction_Payment TP INNER JOIN WM_Payment P ON (P.Payment_No = TP.Payment_No) WHERE P.Temp_Account_No = @TempAccountNo; --Adjust paid amounts on deallocated transactions UPDATE WM_Transaction_Header SET Amount_Paid = Amount_Paid - A.Total_Allocated, Is_Fully_Paid = 'N' FROM WM_Transaction_Header TH INNER JOIN @TranAllocations A ON (A.Transaction_No = TH.Transaction_No); --Drop payments DELETE WM_Payment WHERE Temp_Account_No = @TempAccountNo; --Drop temp account DELETE WM_Temp_Account WHERE Temp_Account_No = @TempAccountNo; END; IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; CREATE PROCEDURE "DBA".WM_DropTempAccount(IN @TempAccountNo Entity_No) BEGIN DECLARE LOCAL TEMPORARY TABLE @TranAllocations( Transaction_No UNSIGNED BIGINT, Total_Allocated NUMERIC(12,2)); --NOTE: No transaction is started by this procedure --It must be run from within a transaction to prevent partial changes from being made --Get total allocated to each transaction from temp account payments INSERT @TranAllocations (Transaction_No, Total_Allocated) SELECT H.Transaction_No, SUM(TP.Amount) FROM WM_Transaction_Header H INNER JOIN WM_Transaction_Payment TP ON (TP.Transaction_No = H.Transaction_No) INNER JOIN WM_Payment P ON (P.Payment_No = TP.Payment_No) WHERE P.Temp_Account_No = @TempAccountNo GROUP BY H.Transaction_No; --Drop payment allocations DELETE WM_Transaction_Payment FROM WM_Transaction_Payment TP INNER JOIN WM_Payment P ON (P.Payment_No = TP.Payment_No) WHERE P.Temp_Account_No = @TempAccountNo; --Adjust paid amounts on deallocated transactions UPDATE WM_Transaction_Header SET Amount_Paid = Amount_Paid - A.Total_Allocated, Is_Fully_Paid = 'N' FROM WM_Transaction_Header TH INNER JOIN @TranAllocations A ON (A.Transaction_No = TH.Transaction_No); --Drop payments DELETE WM_Payment WHERE Temp_Account_No = @TempAccountNo; --Drop temp account DELETE WM_Temp_Account WHERE Temp_Account_No = @TempAccountNo; END; PASSTHROUGH STOP; END IF; END IF; /*********************************************************************/ /* 30/05/2007 - Add stored proc to return whether or not a transaction */ /* is paid by Temp Account payments */ /*********************************************************************/ IF NOT EXISTS( SELECT 1 FROM sysprocedure WHERE proc_name = 'WM_TranIsTempAccount') THEN CREATE PROCEDURE "DBA".WM_TranIsTempAccount(IN @TransactionNo Large_Entity_No) RESULT( Transaction_No Large_Entity_No, Is_Temp_Account Boolean) BEGIN IF EXISTS( SELECT 1 FROM WM_Transaction_Payment TP INNER JOIN WM_Payment P ON (P.Payment_No = TP.Payment_No) WHERE (TP.Transaction_No = @TransactionNo) AND (P.Temp_Account_No IS NOT NULL)) THEN SELECT @TransactionNo, 'Y'; ELSE SELECT @TransactionNo, 'N'; END IF; END; IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; CREATE PROCEDURE "DBA".WM_TranIsTempAccount(IN @TransactionNo Large_Entity_No) RESULT( Transaction_No Large_Entity_No, Is_Temp_Account Boolean) BEGIN IF EXISTS( SELECT 1 FROM WM_Transaction_Payment TP INNER JOIN WM_Payment P ON (P.Payment_No = TP.Payment_No) WHERE (TP.Transaction_No = @TransactionNo) AND (P.Temp_Account_No IS NOT NULL)) THEN SELECT @TransactionNo, 'Y'; ELSE SELECT @TransactionNo, 'N'; END IF; END; PASSTHROUGH STOP; END IF; END IF; /*********************************************************************/ /* 30/05/2007 - Prevent reversal of transactions paid by Temp Account */ /*********************************************************************/ IF EXISTS( SELECT 1 FROM "DBA".SM_Version_Control WHERE Entity_Name = 'WM_CreateTranReversal' AND Database_ID = (SELECT db_property('GlobalDbID')) AND Version_No < 2) THEN ALTER PROCEDURE "DBA".WM_CreateTranReversal(IN @OriginalTranNo Large_Entity_No, IN @DockPrefix VARCHAR(3), IN @DockSuffix VARCHAR(3), IN @DockNo INTEGER, IN @DockVersion INTEGER, IN @CreatedAt Entity_No, IN @CreatedBy Operator_Code, IN @ShiftNo Entity_No, IN @Reason Entity_Comment, OUT @NewTranNo Large_Entity_No, OUT @NewDocketDesc VARCHAR(20)) BEGIN ATOMIC DECLARE @OldItemNo Large_Entity_No; DECLARE @NewItemNo Large_Entity_No; DECLARE @OldDocketDesc VARCHAR(20); DECLARE Item_Cursor CURSOR FOR SELECT Transaction_Item_No FROM WM_Transaction_Item WHERE Transaction_No = @OriginalTranNo; --Check that the transaction exists IF NOT EXISTS( SELECT 1 FROM WM_Transaction_Header WHERE Transaction_No = @OriginalTranNo) THEN RAISERROR 50001 'Transaction No does not exist'; RETURN; END IF; --Raise error if the transaction is incomplete. --Only completed transactions can be reversed with this procedure IF EXISTS( SELECT 1 FROM WM_Transaction_Header WHERE Transaction_No = @OriginalTranNo AND Is_Completed = 'N') THEN RAISERROR 50001 'Cannot reverse an Incomplete Transaction'; RETURN; END IF; --Raise error if the transaction is a Temp Account transaction IF EXISTS( SELECT 1 FROM WM_Transaction_Payment TP INNER JOIN WM_Payment P ON (P.Payment_No = TP.Payment_No) WHERE (TP.Transaction_No = @OriginalTranNo) AND (P.Temp_Account_No IS NOT NULL)) THEN RAISERROR 50001 'Cannot reverse a Temp Account Transaction'; RETURN; END IF; --Raise error if the transaction to be reversed has already been reversed, or is a reversal itself IF EXISTS( SELECT 1 FROM WM_Transaction_Header WHERE Transaction_No = @OriginalTranNo AND (Original_Tran_No IS NOT NULL OR Reversal_Tran_No IS NOT NULL)) THEN RAISERROR 50001 'Cannot reverse a Transaction that has already been reversed or is a reversal itself'; RETURN; END IF; --Get the docket number of the reversed tran (used when adding tran exception) SELECT Docket INTO @OldDocketDesc FROM WM_Transaction_Header WHERE Transaction_No = @OriginalTranNo; --Insert the reversal transaction INSERT WM_Transaction_Header( Vehicle_No, Vehicle_Code, Customer_No, Carrier_No, Job_No, EPA_Approval_No, Docket_No_Prefix, Docket_No, Docket_No_Suffix, Docket_No_Version, Date_Complete, Time_Complete, DateTime_In, DateTime_Out, Created_At, Created_By, Shift_No, Gross, Tare, Net, Gross_Date, Gross_Time, Tare_Date, Tare_Time, Order_no, Transaction_Amount, Amount_Paid, Is_Completed, Is_Stored_Tare, Is_Collection_Vehicle, Memo, Print_Counter, Original_Tran_No, Custom_Data1, Replication_Site_Flag, Group_G0, Group_G1, Group_G2, Group_G3, Group_G4) SELECT Vehicle_No, Vehicle_Code, Customer_No, Carrier_No, Job_No, EPA_Approval_No, @DockPrefix, @DockNo, @DockSuffix, @DockVersion, CURRENT DATE, CURRENT TIME, DateTime_In, DateTime_Out, Created_At, @CreatedBy, @ShiftNo, -Gross, -Tare, -Net, Gross_Date, Gross_Time, Tare_Date, Tare_Time, Order_no, 0, /* note must use zero initially until items are inserted */ 0, /* note must use zero initially until allocations are inserted */ Is_Completed, Is_Stored_Tare, Is_Collection_Vehicle, Memo, 0, Transaction_No, /* Save reversed transaction no as "Original Transaction No" */ Custom_Data1, Replication_Site_Flag, Group_G0, Group_G1, Group_G2, Group_G3, Group_G4 FROM WM_Transaction_Header WHERE Transaction_No = @OriginalTranNo; --Get the new transaction number SET @NewTranNo = @@identity; --Get the new docket no in same format as computed column SELECT Docket INTO @NewDocketDesc FROM WM_Transaction_Header WHERE Transaction_No = @NewTranNo; --Set the link to the new transaction on the old transaction UPDATE WM_Transaction_Header SET Reversal_Tran_No = @NewTranNo WHERE Transaction_No = @OriginalTranNo; --Reverse the tran attachments INSERT WM_Transaction_Attach( Transaction_No, Attachment_No, Tare, Quantity) SELECT @NewTranNo, Attachment_No, -Tare, Quantity FROM WM_Transaction_Attach WHERE Transaction_No = @OriginalTranNo; --Reverse the tran exceptions INSERT WM_Transaction_Exception( Transaction_No, Exception_No, Exception_Comment, Exception_Date, Exception_Time, Operator_Code) SELECT @NewTranNo, Exception_No, Exception_Comment, Exception_Date, Exception_Time, Operator_Code FROM WM_Transaction_Exception WHERE Transaction_No = @OriginalTranNo; --Reverse the tran image links INSERT WM_Transaction_Image( Transaction_No, Image_No) SELECT @NewTranNo, Image_No FROM WM_Transaction_Image WHERE Transaction_No = @OriginalTranNo; --Reverse the Items and Rates OPEN Item_Cursor; lp: LOOP FETCH NEXT Item_Cursor INTO @OldItemNo; IF SQLCODE <> 0 THEN LEAVE lp END IF; --Add the reversal item INSERT WM_Transaction_Item( Transaction_No, Area_No, Product_No, Waste_Stream_No, Sub_Stream_1_No, Sub_Stream_2_No, Sub_Stream_3_No, Gross, Tare, Net, Cubic_Metres, Quantity, LGA_code, Cost_Rule_No, Rate_Range_No, Item_Charge, Cost_Method, EPA_Levy_Exempt) SELECT @NewTranNo, Area_No, Product_No, Waste_Stream_No, Sub_Stream_1_No, Sub_Stream_2_No, Sub_Stream_3_No, -Gross, -Tare, -Net, -Cubic_Metres, -Quantity, LGA_code, Cost_Rule_No, Rate_Range_No, 0, /* note must use zero initially until rates are inserted */ Cost_Method, EPA_Levy_Exempt FROM WM_Transaction_Item WHERE Transaction_Item_No = @OldItemNo; -- NOTE Transaction_Item_No is an identity field, so can be referenced on its own --Get the new item no SET @NewItemNo = @@identity; --Insert the rates for the new item INSERT WM_Transaction_Item_Rate( Transaction_No, Transaction_Item_No, Rate_No, Rate, Value) SELECT @NewTranNo, @NewItemNo, Rate_No, Rate, -Value FROM WM_Transaction_Item_Rate WHERE Transaction_No = @OriginalTranNo AND Transaction_Item_No = @OldItemNo; --Update Item charge fields now that rates have been inserted --Amount_Paid field will be updated when the allocations are reversed UPDATE WM_Transaction_Item SET Item_Charge = (SELECT -Item_Charge FROM WM_Transaction_Item WHERE Transaction_Item_No = @OldItemNo) WHERE Transaction_Item_No = @NewItemNo; END LOOP; CLOSE Item_Cursor; --Now all Items and Rates are inserted, can set the Transaction Charges on the reversal tran UPDATE WM_Transaction_Header SET Transaction_Amount = (SELECT -Transaction_Amount FROM WM_Transaction_Header WHERE Transaction_No = @OriginalTranNo) WHERE Transaction_No = @NewTranNo; --Now reverse the payment allocations to the transaction --The same payments are used, but the allocation amounts are negated INSERT WM_Transaction_Payment( Payment_No, Transaction_No, Shift_No, Allocation_Date, Amount) SELECT Payment_No, @NewTranNo, @ShiftNo, CURRENT TIMESTAMP, -Amount FROM WM_Transaction_Payment WHERE Transaction_No = @OriginalTranNo; --Update the payment records' Amount_Paid fields CREATE TABLE #PayTotals( Payment_No BIGINT PRIMARY KEY, Total_Allocated NUMERIC(10,2)); INSERT #PayTotals(Payment_No, Total_Allocated) SELECT Payment_No, ISNULL(SUM(Amount), 0) FROM WM_Transaction_Payment WHERE Payment_No IN (SELECT Payment_No FROM WM_Transaction_Payment WHERE Transaction_No = @OriginalTranNo) GROUP BY Payment_No; UPDATE WM_Payment SET Amount_Allocated = PT.Total_Allocated, Is_Fully_Allocated = (CASE WHEN PT.Total_Allocated = Amount_Paid THEN 'Y' ELSE 'N' END) FROM WM_Payment P, #PayTotals PT WHERE P.Payment_No = PT.Payment_No; --Update transaction Amount Paid field now that allocations have been inserted UPDATE WM_Transaction_Header SET Amount_Paid = (SELECT -Amount_Paid FROM WM_Transaction_Header WHERE Transaction_No = @OriginalTranNo) WHERE Transaction_No = @NewTranNo; --Add relevant exceptions to the transaction and its reversal --'R' to the reversed transaction, 'S' to the new transaction INSERT WM_Transaction_Exception( Transaction_No, Exception_No, Exception_Comment, Operator_Code) SELECT @OriginalTranNo, Exception_No, 'Reason: ' || @Reason || ', reversed by Docket ' || @NewDocketDesc, @CreatedBy FROM WM_Exceptions WHERE Code = 'R'; INSERT WM_Transaction_Exception( Transaction_No, Exception_No, Exception_Comment, Operator_Code) SELECT @NewTranNo, Exception_No, 'Reason: ' || @Reason || ', reversal of Docket ' || @OldDocketDesc, @CreatedBy FROM WM_Exceptions WHERE Code = 'S'; END; UPDATE "DBA".SM_Version_Control SET Version_No = 2 WHERE Entity_Name = 'WM_CreateTranReversal' AND Database_ID = (SELECT db_property('GlobalDbID')); END IF; IF EXISTS( SELECT 1 FROM "DBA".SM_Version_Control WHERE Entity_Name = 'WM_CreateTranReversal' AND Database_ID = (SELECT db_property('GlobalDbID')) AND Version_No < 3) THEN IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; ALTER PROCEDURE "DBA".WM_CreateTranReversal(IN @OriginalTranNo Large_Entity_No, IN @DockPrefix VARCHAR(3), IN @DockSuffix VARCHAR(3), IN @DockNo INTEGER, IN @DockVersion INTEGER, IN @CreatedAt Entity_No, IN @CreatedBy Operator_Code, IN @ShiftNo Entity_No, IN @Reason Entity_Comment, OUT @NewTranNo Large_Entity_No, OUT @NewDocketDesc VARCHAR(20)) BEGIN ATOMIC DECLARE @OldItemNo Large_Entity_No; DECLARE @NewItemNo Large_Entity_No; DECLARE @OldDocketDesc VARCHAR(20); DECLARE Item_Cursor CURSOR FOR SELECT Transaction_Item_No FROM WM_Transaction_Item WHERE Transaction_No = @OriginalTranNo; --Check that the transaction exists IF NOT EXISTS( SELECT 1 FROM WM_Transaction_Header WHERE Transaction_No = @OriginalTranNo) THEN RAISERROR 50001 'Transaction No does not exist'; RETURN; END IF; --Raise error if the transaction is incomplete. --Only completed transactions can be reversed with this procedure IF EXISTS( SELECT 1 FROM WM_Transaction_Header WHERE Transaction_No = @OriginalTranNo AND Is_Completed = 'N') THEN RAISERROR 50001 'Cannot reverse an Incomplete Transaction'; RETURN; END IF; --Raise error if the transaction is a Temp Account transaction IF EXISTS( SELECT 1 FROM WM_Transaction_Payment TP INNER JOIN WM_Payment P ON (P.Payment_No = TP.Payment_No) WHERE (TP.Transaction_No = @OriginalTranNo) AND (P.Temp_Account_No IS NOT NULL)) THEN RAISERROR 50001 'Cannot reverse a Temp Account Transaction'; RETURN; END IF; --Raise error if the transaction to be reversed has already been reversed, or is a reversal itself IF EXISTS( SELECT 1 FROM WM_Transaction_Header WHERE Transaction_No = @OriginalTranNo AND (Original_Tran_No IS NOT NULL OR Reversal_Tran_No IS NOT NULL)) THEN RAISERROR 50001 'Cannot reverse a Transaction that has already been reversed or is a reversal itself'; RETURN; END IF; --Get the docket number of the reversed tran (used when adding tran exception) SELECT Docket INTO @OldDocketDesc FROM WM_Transaction_Header WHERE Transaction_No = @OriginalTranNo; --Insert the reversal transaction INSERT WM_Transaction_Header( Vehicle_No, Vehicle_Code, Customer_No, Carrier_No, Job_No, EPA_Approval_No, Docket_No_Prefix, Docket_No, Docket_No_Suffix, Docket_No_Version, Date_Complete, Time_Complete, DateTime_In, DateTime_Out, Created_At, Created_By, Shift_No, Gross, Tare, Net, Gross_Date, Gross_Time, Tare_Date, Tare_Time, Order_no, Transaction_Amount, Amount_Paid, Is_Completed, Is_Stored_Tare, Is_Collection_Vehicle, Memo, Print_Counter, Original_Tran_No, Custom_Data1, Replication_Site_Flag, Group_G0, Group_G1, Group_G2, Group_G3, Group_G4) SELECT Vehicle_No, Vehicle_Code, Customer_No, Carrier_No, Job_No, EPA_Approval_No, @DockPrefix, @DockNo, @DockSuffix, @DockVersion, CURRENT DATE, CURRENT TIME, DateTime_In, DateTime_Out, Created_At, @CreatedBy, @ShiftNo, -Gross, -Tare, -Net, Gross_Date, Gross_Time, Tare_Date, Tare_Time, Order_no, 0, /* note must use zero initially until items are inserted */ 0, /* note must use zero initially until allocations are inserted */ Is_Completed, Is_Stored_Tare, Is_Collection_Vehicle, Memo, 0, Transaction_No, /* Save reversed transaction no as "Original Transaction No" */ Custom_Data1, Replication_Site_Flag, Group_G0, Group_G1, Group_G2, Group_G3, Group_G4 FROM WM_Transaction_Header WHERE Transaction_No = @OriginalTranNo; --Get the new transaction number SET @NewTranNo = @@identity; --Get the new docket no in same format as computed column SELECT Docket INTO @NewDocketDesc FROM WM_Transaction_Header WHERE Transaction_No = @NewTranNo; --Set the link to the new transaction on the old transaction UPDATE WM_Transaction_Header SET Reversal_Tran_No = @NewTranNo WHERE Transaction_No = @OriginalTranNo; --Reverse the tran attachments INSERT WM_Transaction_Attach( Transaction_No, Attachment_No, Tare, Quantity) SELECT @NewTranNo, Attachment_No, -Tare, Quantity FROM WM_Transaction_Attach WHERE Transaction_No = @OriginalTranNo; --Reverse the tran exceptions INSERT WM_Transaction_Exception( Transaction_No, Exception_No, Exception_Comment, Exception_Date, Exception_Time, Operator_Code) SELECT @NewTranNo, Exception_No, Exception_Comment, Exception_Date, Exception_Time, Operator_Code FROM WM_Transaction_Exception WHERE Transaction_No = @OriginalTranNo; --Reverse the tran image links INSERT WM_Transaction_Image( Transaction_No, Image_No) SELECT @NewTranNo, Image_No FROM WM_Transaction_Image WHERE Transaction_No = @OriginalTranNo; --Reverse the Items and Rates OPEN Item_Cursor; lp: LOOP FETCH NEXT Item_Cursor INTO @OldItemNo; IF SQLCODE <> 0 THEN LEAVE lp END IF; --Add the reversal item INSERT WM_Transaction_Item( Transaction_No, Area_No, Product_No, Waste_Stream_No, Sub_Stream_1_No, Sub_Stream_2_No, Sub_Stream_3_No, Gross, Tare, Net, Cubic_Metres, Quantity, LGA_code, Cost_Rule_No, Rate_Range_No, Item_Charge, Cost_Method, EPA_Levy_Exempt) SELECT @NewTranNo, Area_No, Product_No, Waste_Stream_No, Sub_Stream_1_No, Sub_Stream_2_No, Sub_Stream_3_No, -Gross, -Tare, -Net, -Cubic_Metres, -Quantity, LGA_code, Cost_Rule_No, Rate_Range_No, 0, /* note must use zero initially until rates are inserted */ Cost_Method, EPA_Levy_Exempt FROM WM_Transaction_Item WHERE Transaction_Item_No = @OldItemNo; -- NOTE Transaction_Item_No is an identity field, so can be referenced on its own --Get the new item no SET @NewItemNo = @@identity; --Insert the rates for the new item INSERT WM_Transaction_Item_Rate( Transaction_No, Transaction_Item_No, Rate_No, Rate, Value) SELECT @NewTranNo, @NewItemNo, Rate_No, Rate, -Value FROM WM_Transaction_Item_Rate WHERE Transaction_No = @OriginalTranNo AND Transaction_Item_No = @OldItemNo; --Update Item charge fields now that rates have been inserted --Amount_Paid field will be updated when the allocations are reversed UPDATE WM_Transaction_Item SET Item_Charge = (SELECT -Item_Charge FROM WM_Transaction_Item WHERE Transaction_Item_No = @OldItemNo) WHERE Transaction_Item_No = @NewItemNo; END LOOP; CLOSE Item_Cursor; --Now all Items and Rates are inserted, can set the Transaction Charges on the reversal tran UPDATE WM_Transaction_Header SET Transaction_Amount = (SELECT -Transaction_Amount FROM WM_Transaction_Header WHERE Transaction_No = @OriginalTranNo) WHERE Transaction_No = @NewTranNo; --Now reverse the payment allocations to the transaction --The same payments are used, but the allocation amounts are negated INSERT WM_Transaction_Payment( Payment_No, Transaction_No, Shift_No, Allocation_Date, Amount) SELECT Payment_No, @NewTranNo, @ShiftNo, CURRENT TIMESTAMP, -Amount FROM WM_Transaction_Payment WHERE Transaction_No = @OriginalTranNo; --Update the payment records' Amount_Paid fields CREATE TABLE #PayTotals( Payment_No BIGINT PRIMARY KEY, Total_Allocated NUMERIC(10,2)); INSERT #PayTotals(Payment_No, Total_Allocated) SELECT Payment_No, ISNULL(SUM(Amount), 0) FROM WM_Transaction_Payment WHERE Payment_No IN (SELECT Payment_No FROM WM_Transaction_Payment WHERE Transaction_No = @OriginalTranNo) GROUP BY Payment_No; UPDATE WM_Payment SET Amount_Allocated = PT.Total_Allocated, Is_Fully_Allocated = (CASE WHEN PT.Total_Allocated = Amount_Paid THEN 'Y' ELSE 'N' END) FROM WM_Payment P, #PayTotals PT WHERE P.Payment_No = PT.Payment_No; --Update transaction Amount Paid field now that allocations have been inserted UPDATE WM_Transaction_Header SET Amount_Paid = (SELECT -Amount_Paid FROM WM_Transaction_Header WHERE Transaction_No = @OriginalTranNo) WHERE Transaction_No = @NewTranNo; --Add relevant exceptions to the transaction and its reversal --'R' to the reversed transaction, 'S' to the new transaction INSERT WM_Transaction_Exception( Transaction_No, Exception_No, Exception_Comment, Operator_Code) SELECT @OriginalTranNo, Exception_No, 'Reason: ' || @Reason || ', reversed by Docket ' || @NewDocketDesc, @CreatedBy FROM WM_Exceptions WHERE Code = 'R'; INSERT WM_Transaction_Exception( Transaction_No, Exception_No, Exception_Comment, Operator_Code) SELECT @NewTranNo, Exception_No, 'Reason: ' || @Reason || ', reversal of Docket ' || @OldDocketDesc, @CreatedBy FROM WM_Exceptions WHERE Code = 'S'; END; UPDATE "DBA".SM_Version_Control SET Version_No = 3 WHERE Entity_Name = 'WM_CreateTranReversal' AND Database_ID = (SELECT db_property('GlobalDbID')); PASSTHROUGH STOP; END IF; UPDATE "DBA".SM_Version_Control SET Version_No = 3 WHERE Entity_Name = 'WM_CreateTranReversal' AND Database_ID = (SELECT db_property('GlobalDbID')); END IF; /*********************************************************************/ /* 31/05/2007 - Changes to Default_View_0019 so that all Temp Accounts */ /* are shown including those where all transactions have been deleted */ /*********************************************************************/ IF NOT EXISTS( SELECT 1 FROM "DBA".SM_Version_Control WHERE Entity_Name = 'Default_View_0019' AND Database_ID = (SELECT db_property('GlobalDbID'))) THEN ALTER VIEW "DBA".Default_View_0019 AS SELECT TA.Operator_Reference AS Account_Ref, TA.DateTime_Created AS Created, TA.Customer_Name, TA.Vehicle_Code, TA.Cheque_No, ISNULL(PC.Payments, 0) AS Payments, ISNULL(PC.Total, 0) AS Total, TA.Credit_Limit, TA.Expiry_Date, TA.Temp_Account_No FROM "DBA".WM_Temp_Account TA LEFT OUTER JOIN ( SELECT TApc.Temp_Account_No, COUNT(Ppc.Payment_No) AS Payments, SUM(Ppc.Amount_Paid) AS Total FROM "DBA".WM_Temp_Account TApc LEFT OUTER JOIN "DBA".WM_Payment Ppc ON (TApc.Temp_Account_No = Ppc.Temp_Account_No) WHERE (TApc.Available = 'Y') --only Available Temp Accounts AND (TApc.Site_No = (SELECT "DBA".SM_GetLocalSiteNo())) --only Temp Accounts for this site --Eliminate reversed payments and reversals as they cause a misleading payment count AND (Ppc.Reference_Payment_No IS NULL) -- i.e. Not a Reversal AND (NOT EXISTS(SELECT 1 FROM "DBA".WM_Payment P1pc WHERE P1pc.Reference_Payment_No = Ppc.Payment_No)) -- i.e. Has not been reversed GROUP BY TApc.Temp_Account_No ) PC ON (PC.Temp_Account_No = TA.Temp_Account_No) WHERE (TA.Available = 'Y') AND (TA.Site_No = (SELECT "DBA".SM_GetLocalSiteNo())); IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; ALTER VIEW "DBA".Default_View_0019 AS SELECT TA.Operator_Reference AS Account_Ref, TA.DateTime_Created AS Created, TA.Customer_Name, TA.Vehicle_Code, TA.Cheque_No, ISNULL(PC.Payments, 0) AS Payments, ISNULL(PC.Total, 0) AS Total, TA.Credit_Limit, TA.Expiry_Date, TA.Temp_Account_No FROM "DBA".WM_Temp_Account TA LEFT OUTER JOIN ( SELECT TApc.Temp_Account_No, COUNT(Ppc.Payment_No) AS Payments, SUM(Ppc.Amount_Paid) AS Total FROM "DBA".WM_Temp_Account TApc LEFT OUTER JOIN "DBA".WM_Payment Ppc ON (TApc.Temp_Account_No = Ppc.Temp_Account_No) WHERE (TApc.Available = 'Y') --only Available Temp Accounts AND (TApc.Site_No = (SELECT "DBA".SM_GetLocalSiteNo())) --only Temp Accounts for this site --Eliminate reversed payments and reversals as they cause a misleading payment count AND (Ppc.Reference_Payment_No IS NULL) -- i.e. Not a Reversal AND (NOT EXISTS(SELECT 1 FROM "DBA".WM_Payment P1pc WHERE P1pc.Reference_Payment_No = Ppc.Payment_No)) -- i.e. Has not been reversed GROUP BY TApc.Temp_Account_No ) PC ON (PC.Temp_Account_No = TA.Temp_Account_No) WHERE (TA.Available = 'Y') AND (TA.Site_No = (SELECT "DBA".SM_GetLocalSiteNo())); INSERT "DBA".SM_Version_Control(Entity_Name, Database_ID, Version_No) VALUES('Default_View_0019', (SELECT db_property('GlobalDBId')), 1); PASSTHROUGH STOP; END IF; INSERT "DBA".SM_Version_Control(Entity_Name, Database_ID, Version_No) VALUES('Default_View_0019', (SELECT db_property('GlobalDBId')), 1); END IF; /*********************************************************************/ /* 31/05/2007 - Alter procedure WM_LinkRefundPayment to raise exception */ /* if attempting to link a Refund to a Temp Account payment. (Temp Account */ /* payments must be reversed as Refunds are not supported.) */ /*********************************************************************/ IF NOT EXISTS( SELECT 1 FROM "DBA".SM_Version_Control WHERE Entity_Name = 'WM_LinkRefundPayment' AND Database_ID = (SELECT db_property('GlobalDbID'))) THEN ALTER PROCEDURE "DBA".WM_LinkRefundPayment(IN @RefundPaymentNo Large_Entity_No, IN @PaymentsCSV VARCHAR(300)) BEGIN ATOMIC DECLARE @TotalRefund NUMERIC(8,2); DECLARE @NumsTemp VARCHAR(500); DECLARE @RepFlagValueCnt INTEGER; --Must supply input variables IF @RefundPaymentNo IS NULL THEN RAISERROR 50001 'You must specify the Refund Payment No'; RETURN; END IF; IF ISNULL(@PaymentsCSV, '') = '' THEN RAISERROR 50001 'You must specify the Payments to link the Refund to'; RETURN; END IF; --Refund payment must exist IF NOT EXISTS( SELECT 1 FROM WM_Payment WHERE Payment_No = @RefundPaymentNo) THEN RAISERROR 50001 'Refund Payment %1! not found', @RefundPaymentNo; RETURN; END IF; --Create a table containing the payment numbers supplied CREATE TABLE #PaymentList(Payment_No BIGINT); SET @NumsTemp = @PaymentsCSV; WHILE LTRIM(@NumsTemp) <> '' LOOP IF CHARINDEX(',', @NumsTemp) > 0 THEN INSERT #PaymentList(Payment_No) VALUES(CONVERT(BIGINT, LTRIM(LEFT(@NumsTemp, CHARINDEX(',', @NumsTemp)-1)))); SET @NumsTemp = LTRIM(STUFF(@NumsTemp, 1, CHARINDEX(',', @NumsTemp), NULL)); ELSE INSERT #PaymentList(Payment_No) VALUES(CONVERT(BIGINT, LTRIM(@NumsTemp))); SET @NumsTemp = ''; END IF; END LOOP; --Raise error if any payments are Temp Account --Refunds on Temp Accounts are not supported IF EXISTS( SELECT 1 FROM #PaymentList PL INNER JOIN WM_Payment P ON (P.Payment_No = PL.Payment_No) WHERE P.Temp_Account_No IS NOT NULL) THEN RAISERROR 50001 'Cannot Refund a Temporary Account payment'; RETURN; END IF; --Amount to be refunded is the sum of the available amount on the payments / refunds nominated SELECT ISNULL(SUM(Amount_Paid), 0) - ISNULL(SUM(Amount_Allocated), 0) INTO @TotalRefund FROM WM_Payment WHERE Payment_No IN (SELECT Payment_No FROM #PaymentList); --Refund remaining amount available on each payment / refund and link the refund payment nominated UPDATE WM_Payment SET Amount_Allocated = Amount_Paid, Is_Fully_Allocated = 'Y', Refund_Payment_No = @RefundPaymentNo WHERE Payment_No IN (SELECT Payment_No FROM #PaymentList); --Set Amount_Allocated on Refund payment = total amount refunded --Set Is_Fully_Allocated = 'Y' when necessary UPDATE WM_Payment SET Amount_Allocated = Amount_Allocated - @TotalRefund, --NOTE @TotalRefund will be +ve Is_Fully_Allocated = (CASE WHEN (Amount_Allocated - @TotalRefund) = Amount_Paid THEN 'Y' ELSE 'N' END) WHERE Payment_No = @RefundPaymentNo; --Set Replication_Site_Flag to zero on all if the flags differ between the payments and the refund payment --This is necessary to ensure the linked refund stays with its payments --Trigger on WM_Payment will set associated transaction header flags to zero SELECT COUNT(DISTINCT Replication_Site_Flag) INTO @RepFlagValueCnt FROM WM_Payment WHERE (Payment_No = @RefundPaymentNo) OR (Payment_No IN (SELECT Payment_No FROM #PaymentList)); IF @RepFlagValueCnt > 1 THEN UPDATE WM_Payment SET Replication_Site_Flag = '0' WHERE ((Payment_No = @RefundPaymentNo) OR (Payment_No IN (SELECT Payment_No FROM #PaymentList))) AND (Replication_Site_Flag <> '0'); END IF; END; IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; ALTER PROCEDURE "DBA".WM_LinkRefundPayment(IN @RefundPaymentNo Large_Entity_No, IN @PaymentsCSV VARCHAR(300)) BEGIN ATOMIC DECLARE @TotalRefund NUMERIC(8,2); DECLARE @NumsTemp VARCHAR(500); DECLARE @RepFlagValueCnt INTEGER; --Must supply input variables IF @RefundPaymentNo IS NULL THEN RAISERROR 50001 'You must specify the Refund Payment No'; RETURN; END IF; IF ISNULL(@PaymentsCSV, '') = '' THEN RAISERROR 50001 'You must specify the Payments to link the Refund to'; RETURN; END IF; --Refund payment must exist IF NOT EXISTS( SELECT 1 FROM WM_Payment WHERE Payment_No = @RefundPaymentNo) THEN RAISERROR 50001 'Refund Payment %1! not found', @RefundPaymentNo; RETURN; END IF; --Create a table containing the payment numbers supplied CREATE TABLE #PaymentList(Payment_No BIGINT); SET @NumsTemp = @PaymentsCSV; WHILE LTRIM(@NumsTemp) <> '' LOOP IF CHARINDEX(',', @NumsTemp) > 0 THEN INSERT #PaymentList(Payment_No) VALUES(CONVERT(BIGINT, LTRIM(LEFT(@NumsTemp, CHARINDEX(',', @NumsTemp)-1)))); SET @NumsTemp = LTRIM(STUFF(@NumsTemp, 1, CHARINDEX(',', @NumsTemp), NULL)); ELSE INSERT #PaymentList(Payment_No) VALUES(CONVERT(BIGINT, LTRIM(@NumsTemp))); SET @NumsTemp = ''; END IF; END LOOP; --Raise error if any payments are Temp Account --Refunds on Temp Accounts are not supported IF EXISTS( SELECT 1 FROM #PaymentList PL INNER JOIN WM_Payment P ON (P.Payment_No = PL.Payment_No) WHERE P.Temp_Account_No IS NOT NULL) THEN RAISERROR 50001 'Cannot Refund a Temporary Account payment'; RETURN; END IF; --Amount to be refunded is the sum of the available amount on the payments / refunds nominated SELECT ISNULL(SUM(Amount_Paid), 0) - ISNULL(SUM(Amount_Allocated), 0) INTO @TotalRefund FROM WM_Payment WHERE Payment_No IN (SELECT Payment_No FROM #PaymentList); --Refund remaining amount available on each payment / refund and link the refund payment nominated UPDATE WM_Payment SET Amount_Allocated = Amount_Paid, Is_Fully_Allocated = 'Y', Refund_Payment_No = @RefundPaymentNo WHERE Payment_No IN (SELECT Payment_No FROM #PaymentList); --Set Amount_Allocated on Refund payment = total amount refunded --Set Is_Fully_Allocated = 'Y' when necessary UPDATE WM_Payment SET Amount_Allocated = Amount_Allocated - @TotalRefund, --NOTE @TotalRefund will be +ve Is_Fully_Allocated = (CASE WHEN (Amount_Allocated - @TotalRefund) = Amount_Paid THEN 'Y' ELSE 'N' END) WHERE Payment_No = @RefundPaymentNo; --Set Replication_Site_Flag to zero on all if the flags differ between the payments and the refund payment --This is necessary to ensure the linked refund stays with its payments --Trigger on WM_Payment will set associated transaction header flags to zero SELECT COUNT(DISTINCT Replication_Site_Flag) INTO @RepFlagValueCnt FROM WM_Payment WHERE (Payment_No = @RefundPaymentNo) OR (Payment_No IN (SELECT Payment_No FROM #PaymentList)); IF @RepFlagValueCnt > 1 THEN UPDATE WM_Payment SET Replication_Site_Flag = '0' WHERE ((Payment_No = @RefundPaymentNo) OR (Payment_No IN (SELECT Payment_No FROM #PaymentList))) AND (Replication_Site_Flag <> '0'); END IF; END; INSERT "DBA".SM_Version_Control(Entity_Name, Database_ID, Version_No) VALUES('WM_LinkRefundPayment', (SELECT db_property('GlobalDBId')), 1); PASSTHROUGH STOP; END IF; INSERT "DBA".SM_Version_Control(Entity_Name, Database_ID, Version_No) VALUES('WM_LinkRefundPayment', (SELECT db_property('GlobalDBId')), 1); END IF; /*********************************************************************/ /* 12/06/2007 - Alter procedure WM_BaseFrame_CompletedTran to improve performance */ /*********************************************************************/ IF EXISTS( SELECT 1 FROM "DBA".SM_Version_Control WHERE Entity_Name = 'WM_BaseFrame_CompletedTran' AND Database_ID = (SELECT db_property('GlobalDbID')) AND Version_No < 2) THEN ALTER PROCEDURE "DBA".WM_BaseFrame_CompletedTran(IN @TranNo Large_Entity_No, IN @ProductChargeRateNo Entity_No, IN @ChargeOverrideExceptionCode Entity_Code) RESULT( Transaction_No Large_Entity_No, Docket_No INTEGER, Docket_No_Prefix CHAR(3), Docket_No_Suffix CHAR(3), Docket_No_Version INTEGER, Vehicle_No Entity_No, Vehicle_Code Entity_Code, Customer_No Entity_No, Job_No Entity_No, Carrier_No Entity_No, Gross Weight, Gross_Date Entity_Date, Gross_Time Entity_Time, Tare Weight, Tare_Date Entity_Date, Tare_Time Entity_Time, Net Weight, Memo Entity_Comment, Date_Complete Entity_Date, Time_Complete Entity_Time, Created_At Entity_No, Shift_No Entity_No, Created_By Operator_Code, Is_Fully_Paid Boolean, Amount_Paid NUMERIC(8,2), Is_Stored_Tare Boolean, DateTime_In Entity_DateTime, DateTime_Out Entity_DateTime, Custom_Data1 Entity_Code, Order_No VARCHAR(30), Reversal_Tran_No Large_Entity_No, Original_Tran_No Large_Entity_No, Transaction_Item_No Large_Entity_No, Product_No Entity_No, Area_No Entity_No, Waste_Stream_No Entity_No, Sub_Stream_1_No Entity_No, SS1_Name Entity_Name, LGA_Code Entity_Code, Rate_Range_No Entity_No, Cost_Method Prod_Cost_Method, Cost_Rule_No Entity_No, Item_Charge NUMERIC(8,2), Quantity NUMERIC(19,6), Item_Net Weight, Item_CM NUMERIC(19,6), Sub_Stream_2_No Entity_No, Sub_Stream_3_No Entity_No, Has_Override Boolean, Override_User Operator_Code, Override_Comment Entity_Comment, Charge_Rate NUMERIC(10,4), Original_Rate_Range_No Entity_No, Amount_Advance_Paid NUMERIC(8,2) ) BEGIN SELECT H.Transaction_No, H.Docket_No, H.Docket_No_Prefix, H.Docket_No_Suffix, H.Docket_No_Version, H.Vehicle_No, H.Vehicle_Code, H.Customer_No, H.Job_No, H.Carrier_No, H.Gross, H.Gross_Date, H.Gross_Time, H.Tare, H.Tare_Date, H.Tare_Time, H.Net, H.Memo, H.Date_Complete, H.Time_Complete, H.Created_At, H.Shift_No, H.Created_By, H.Is_Fully_Paid, H.Amount_Paid, H.Is_Stored_Tare, H.DateTime_In, H.DateTime_Out, H.Custom_Data1, H.Order_No, H.Reversal_Tran_No, H.Original_Tran_No, I.Transaction_Item_No, I.Product_No, I.Area_No, I.Waste_Stream_No, I.Sub_Stream_1_No, E.Name AS SS1_Name, I.LGA_Code, I.Rate_Range_No, I.Cost_Method, I.Cost_Rule_No, I.Item_Charge, I.Quantity, I.Net AS Item_Net, I.Cubic_Metres AS Item_CM, I.Sub_Stream_2_No, I.Sub_Stream_3_No, (CASE A.Code WHEN @ChargeOverrideExceptionCode THEN 'Y' ELSE 'N' END) AS Has_Override, A.Operator_Code AS Override_User, A.Exception_Comment AS Override_Comment, B.Rate AS Charge_Rate, I.Original_Rate_Range_No, ISNULL(SUM(X.Amount), 0) AS Advance_Pay_Total FROM WM_Transaction_Header H INNER JOIN WM_Transaction_Item I ON (I.Transaction_No = H.Transaction_No) LEFT OUTER JOIN ( SELECT Transaction_No, Amount FROM WM_Transaction_Payment TP INNER JOIN WM_Payment P ON ((P.Payment_No = TP.Payment_No) AND (P.Payment_Type = 'A')) ) X ON (X.Transaction_No = H.Transaction_No) LEFT OUTER JOIN WM_Waste_Sub_Stream_1 E ON (E.Sub_Stream_1_No = I.Sub_Stream_1_No) LEFT OUTER JOIN ( SELECT TEx.Transaction_No, Ex.Code, TEx.Operator_Code, TEx.Exception_Comment FROM WM_Transaction_Exception TEx INNER JOIN WM_Exceptions Ex ON (Ex.Exception_No = TEx.Exception_No) ) A ON (A.Transaction_No = H.Transaction_No) AND (A.Code = @ChargeOverrideExceptionCode) LEFT OUTER JOIN ( SELECT Transaction_No, Transaction_Item_No, Rate, Rate_No FROM WM_Transaction_Item_Rate ) B ON (B.Transaction_No = I.Transaction_No) AND (B.Transaction_Item_No = I.Transaction_Item_No) AND (B.Rate_No = @ProductChargeRateNo) WHERE H.Transaction_No = @TranNo AND Is_Completed = 'Y' GROUP BY H.Transaction_No, H.Docket_No, H.Docket_No_Prefix, H.Docket_No_Suffix, H.Docket_No_Version, H.Vehicle_No, H.Vehicle_Code, H.Customer_No, H.Job_No, H.Carrier_No, H.Gross, H.Gross_Date, H.Gross_Time, H.Tare, H.Tare_Date, H.Tare_Time, H.Net, H.Memo, H.Date_Complete, H.Time_Complete, H.Created_At, H.Shift_No, H.Created_By, H.Is_Fully_Paid, H.Amount_Paid, H.Is_Stored_Tare, H.DateTime_In, H.DateTime_Out, H.Custom_Data1, H.Order_No, H.Reversal_Tran_No, H.Original_Tran_No, I.Transaction_Item_No, I.Product_No, I.Area_No, I.Waste_Stream_No, I.Sub_Stream_1_No, E.Name, I.LGA_Code, I.Rate_Range_No, I.Cost_Method, I.Cost_Rule_No, I.Item_Charge, I.Quantity, I.Net, I.Cubic_Metres, I.Sub_Stream_2_No, I.Sub_Stream_3_No, (CASE A.Code WHEN @ChargeOverrideExceptionCode THEN 'Y' ELSE 'N' END), A.Operator_Code, A.Exception_Comment, B.Rate, I.Original_Rate_Range_No END; IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; ALTER PROCEDURE "DBA".WM_BaseFrame_CompletedTran(IN @TranNo Large_Entity_No, IN @ProductChargeRateNo Entity_No, IN @ChargeOverrideExceptionCode Entity_Code) RESULT( Transaction_No Large_Entity_No, Docket_No INTEGER, Docket_No_Prefix CHAR(3), Docket_No_Suffix CHAR(3), Docket_No_Version INTEGER, Vehicle_No Entity_No, Vehicle_Code Entity_Code, Customer_No Entity_No, Job_No Entity_No, Carrier_No Entity_No, Gross Weight, Gross_Date Entity_Date, Gross_Time Entity_Time, Tare Weight, Tare_Date Entity_Date, Tare_Time Entity_Time, Net Weight, Memo Entity_Comment, Date_Complete Entity_Date, Time_Complete Entity_Time, Created_At Entity_No, Shift_No Entity_No, Created_By Operator_Code, Is_Fully_Paid Boolean, Amount_Paid NUMERIC(8,2), Is_Stored_Tare Boolean, DateTime_In Entity_DateTime, DateTime_Out Entity_DateTime, Custom_Data1 Entity_Code, Order_No VARCHAR(30), Reversal_Tran_No Large_Entity_No, Original_Tran_No Large_Entity_No, Transaction_Item_No Large_Entity_No, Product_No Entity_No, Area_No Entity_No, Waste_Stream_No Entity_No, Sub_Stream_1_No Entity_No, SS1_Name Entity_Name, LGA_Code Entity_Code, Rate_Range_No Entity_No, Cost_Method Prod_Cost_Method, Cost_Rule_No Entity_No, Item_Charge NUMERIC(8,2), Quantity NUMERIC(19,6), Item_Net Weight, Item_CM NUMERIC(19,6), Sub_Stream_2_No Entity_No, Sub_Stream_3_No Entity_No, Has_Override Boolean, Override_User Operator_Code, Override_Comment Entity_Comment, Charge_Rate NUMERIC(10,4), Original_Rate_Range_No Entity_No, Amount_Advance_Paid NUMERIC(8,2) ) BEGIN SELECT H.Transaction_No, H.Docket_No, H.Docket_No_Prefix, H.Docket_No_Suffix, H.Docket_No_Version, H.Vehicle_No, H.Vehicle_Code, H.Customer_No, H.Job_No, H.Carrier_No, H.Gross, H.Gross_Date, H.Gross_Time, H.Tare, H.Tare_Date, H.Tare_Time, H.Net, H.Memo, H.Date_Complete, H.Time_Complete, H.Created_At, H.Shift_No, H.Created_By, H.Is_Fully_Paid, H.Amount_Paid, H.Is_Stored_Tare, H.DateTime_In, H.DateTime_Out, H.Custom_Data1, H.Order_No, H.Reversal_Tran_No, H.Original_Tran_No, I.Transaction_Item_No, I.Product_No, I.Area_No, I.Waste_Stream_No, I.Sub_Stream_1_No, E.Name AS SS1_Name, I.LGA_Code, I.Rate_Range_No, I.Cost_Method, I.Cost_Rule_No, I.Item_Charge, I.Quantity, I.Net AS Item_Net, I.Cubic_Metres AS Item_CM, I.Sub_Stream_2_No, I.Sub_Stream_3_No, (CASE A.Code WHEN @ChargeOverrideExceptionCode THEN 'Y' ELSE 'N' END) AS Has_Override, A.Operator_Code AS Override_User, A.Exception_Comment AS Override_Comment, B.Rate AS Charge_Rate, I.Original_Rate_Range_No, ISNULL(SUM(X.Amount), 0) AS Advance_Pay_Total FROM WM_Transaction_Header H INNER JOIN WM_Transaction_Item I ON (I.Transaction_No = H.Transaction_No) LEFT OUTER JOIN ( SELECT Transaction_No, Amount FROM WM_Transaction_Payment TP INNER JOIN WM_Payment P ON ((P.Payment_No = TP.Payment_No) AND (P.Payment_Type = 'A')) ) X ON (X.Transaction_No = H.Transaction_No) LEFT OUTER JOIN WM_Waste_Sub_Stream_1 E ON (E.Sub_Stream_1_No = I.Sub_Stream_1_No) LEFT OUTER JOIN ( SELECT TEx.Transaction_No, Ex.Code, TEx.Operator_Code, TEx.Exception_Comment FROM WM_Transaction_Exception TEx INNER JOIN WM_Exceptions Ex ON (Ex.Exception_No = TEx.Exception_No) ) A ON (A.Transaction_No = H.Transaction_No) AND (A.Code = @ChargeOverrideExceptionCode) LEFT OUTER JOIN ( SELECT Transaction_No, Transaction_Item_No, Rate, Rate_No FROM WM_Transaction_Item_Rate ) B ON (B.Transaction_No = I.Transaction_No) AND (B.Transaction_Item_No = I.Transaction_Item_No) AND (B.Rate_No = @ProductChargeRateNo) WHERE H.Transaction_No = @TranNo AND Is_Completed = 'Y' GROUP BY H.Transaction_No, H.Docket_No, H.Docket_No_Prefix, H.Docket_No_Suffix, H.Docket_No_Version, H.Vehicle_No, H.Vehicle_Code, H.Customer_No, H.Job_No, H.Carrier_No, H.Gross, H.Gross_Date, H.Gross_Time, H.Tare, H.Tare_Date, H.Tare_Time, H.Net, H.Memo, H.Date_Complete, H.Time_Complete, H.Created_At, H.Shift_No, H.Created_By, H.Is_Fully_Paid, H.Amount_Paid, H.Is_Stored_Tare, H.DateTime_In, H.DateTime_Out, H.Custom_Data1, H.Order_No, H.Reversal_Tran_No, H.Original_Tran_No, I.Transaction_Item_No, I.Product_No, I.Area_No, I.Waste_Stream_No, I.Sub_Stream_1_No, E.Name, I.LGA_Code, I.Rate_Range_No, I.Cost_Method, I.Cost_Rule_No, I.Item_Charge, I.Quantity, I.Net, I.Cubic_Metres, I.Sub_Stream_2_No, I.Sub_Stream_3_No, (CASE A.Code WHEN @ChargeOverrideExceptionCode THEN 'Y' ELSE 'N' END), A.Operator_Code, A.Exception_Comment, B.Rate, I.Original_Rate_Range_No END; UPDATE "DBA".SM_Version_Control SET Version_No = 2 WHERE Entity_Name = 'WM_BaseFrame_CompletedTran' AND Database_ID = (SELECT db_property('GlobalDbID')); PASSTHROUGH STOP; END IF; UPDATE "DBA".SM_Version_Control SET Version_No = 2 WHERE Entity_Name = 'WM_BaseFrame_CompletedTran' AND Database_ID = (SELECT db_property('GlobalDbID')); END IF; /*********************************************************************/ /* 12/06/2007 - Alter WM_Payment AFTER UPDATE trigger to improve performance */ /*********************************************************************/ IF EXISTS( SELECT 1 FROM "DBA".SM_Version_Control WHERE Entity_Name = 'au_WM_Payment' AND Database_ID = (SELECT db_property('GlobalDbID')) AND Version_No < 6) THEN ALTER TRIGGER au_WM_Payment AFTER UPDATE ON "DBA".WM_Payment REFERENCING NEW AS inserted OLD AS deleted FOR EACH STATEMENT BEGIN DECLARE @PaymentTotal NUMERIC(8,2); --Refund_Payment_No must reference a payment of type 'R' (i.e. refund) IF UPDATE(Refund_Payment_No) AND EXISTS( SELECT 1 FROM inserted ins INNER JOIN WM_Payment P ON (P.Payment_No = ins.Refund_Payment_No) WHERE P.Payment_Type <> 'R') THEN RAISERROR 30000 'Refund_Payment_No must reference a Refund Payment.'; END IF; --Cannot create a reversal of an integrated EFTPOS payment IF UPDATE(Reference_Payment_No) AND EXISTS( SELECT 1 FROM inserted ins INNER JOIN WM_Payment P ON (P.Payment_No = ins.Reference_Payment_No) WHERE P.Eftpos_Transaction_Ref IS NOT NULL) THEN RAISERROR 30000 'Cannot reverse an integrated EFTPOS payment. A refund must be issued.'; END IF; --Amount Allocated must equal the sum of the Transaction Payment allocations -- UNLESS a reversal of the payment exists OR the payment is a reversal itself -- OR the Amount Allocated is set equal to the Amount Paid and no allocations exist -- OR the payment is linked to a Refund via the Refund_Payment_No field -- OR the payment is a linked Refund --If payment is a linked Refund, the Amount Allocated must equal the sum of the Amount Paid -- on all referencing payments (this includes referencing Refunds), less the total of payment -- allocations connected with referencing payments (and Refunds) --NOTE: Allow the total of allocations to differ from the payment Amount_Allocated during replication. -- This is necessary to permit the reconciliation of update conflicts that occur when payments are -- allocated at multiple sites between replication operations. This is intended to be a temporary state only -- and should be resolved by the time replication is complete. The view Payment_Allocation_Errors is examined at -- the conclusion of the replication process and if any exist, an error is logged. IF (UPDATE(Amount_Allocated) OR UPDATE(Reference_Payment_No) OR UPDATE(Refund_Payment_No) OR UPDATE(Amount_Paid)) AND CURRENT REMOTE USER IS NULL THEN IF EXISTS( SELECT 1 FROM inserted ins LEFT OUTER JOIN WM_Transaction_Payment TP ON (TP.Payment_No = ins.Payment_No) WHERE (ins.Reference_Payment_No IS NULL) --not a reversal AND (NOT EXISTS(SELECT 1 FROM WM_Payment P1 WHERE P1.Reference_Payment_No = ins.Payment_No)) --not reversed AND (Refund_Payment_No IS NULL) --does not have a linked Refund AND (NOT EXISTS(SELECT 1 FROM WM_Payment P2 WHERE P2.Refund_Payment_No = ins.Payment_No)) --not a linked Refund GROUP BY ins.Payment_No, ins.Amount_Paid, ins.Amount_Allocated HAVING ((SUM(TP.Amount) IS NOT NULL) AND (SUM(TP.Amount) <> ins.Amount_Allocated)) OR ((SUM(TP.Amount) IS NULL) AND (ins.Amount_Allocated <> 0) AND (ins.Amount_Allocated <> ins.Amount_Paid))) THEN RAISERROR 30000 'Total of Transaction Payment Allocations does not equal Payment Allocated Total.'; END IF; --Validate linked Refund payments (if present) IF EXISTS( SELECT 1 FROM inserted ins INNER JOIN WM_Payment P ON (P.Refund_Payment_No = ins.Payment_No)) THEN SELECT ISNULL(SUM(P.Amount_Paid), 0) INTO @PaymentTotal FROM inserted ins INNER JOIN WM_Payment P ON (P.Refund_Payment_No = ins.Payment_No); IF EXISTS( SELECT 1 FROM inserted ins INNER JOIN WM_Payment P ON (P.Refund_Payment_No = ins.Payment_No) LEFT OUTER JOIN WM_Transaction_Payment TP ON (TP.Payment_No = P.Payment_No) GROUP BY ins.Payment_No, ins.Amount_Allocated HAVING (-1 * ins.Amount_Allocated) <> (@PaymentTotal - SUM(ISNULL(TP.Amount, 0)))) THEN RAISERROR 30000 'Amount Allocated on linked Refund must equal Payment total less Payment allocations.'; END IF; END IF; END IF; --Set all Transactions' Replication_Site_Flag's to ZERO if the payment's flag is ZERO --NOTE: This query assumes that Payment_No will never be modified IF UPDATE(Replication_Site_Flag) AND EXISTS( SELECT 1 FROM WM_Transaction_Payment TP INNER JOIN inserted ins ON (ins.Payment_No = TP.Payment_No) INNER JOIN deleted del ON (del.Payment_No = ins.Payment_No) WHERE (ins.Replication_Site_Flag = '0') AND (del.Replication_Site_Flag <> '0')) THEN UPDATE WM_Transaction_Header SET Replication_Site_Flag = '0' WHERE Transaction_No IN (SELECT TP.Transaction_No FROM WM_Transaction_Payment TP INNER JOIN inserted ins ON (ins.Payment_No = TP.Payment_No) INNER JOIN deleted del ON (del.Payment_No = ins.Payment_No) WHERE (ins.Replication_Site_Flag = '0') AND (del.Replication_Site_Flag <> '0')); END IF; --Set Replication_Site_Flag to zero on all if the flags differ between the payments and the refund payment --This is necessary to ensure the linked refund stays with its payments --Trigger on WM_Payment will set associated transaction header flags to zero --%MG NOTE 6/06/2007 - Sybase appears to do a "complete boolean eval" so secondary IF clause -- has been moved inside the IF UPDATE clause. Otherwise, it executes each time which adds -- considerable time to the transaction save e.g. at GCCC, 3.5 seconds to do a single payment -- update, and there are two or more per transactions IF UPDATE(Replication_Site_Flag) THEN IF EXISTS( SELECT 1 FROM WM_Payment P WHERE ((P.Refund_Payment_No IS NOT NULL) OR EXISTS( SELECT 1 FROM WM_Payment RP WHERE RP.Refund_Payment_No = P.Payment_No)) AND ((P.Payment_No IN (SELECT Payment_No FROM inserted)) OR (P.Payment_No IN (SELECT Refund_Payment_No FROM inserted)) OR (P.Refund_Payment_No IN (SELECT Payment_No FROM inserted)) OR (P.Refund_Payment_No IN (SELECT Refund_Payment_No FROM inserted))) GROUP BY ISNULL(P.Refund_Payment_No, P.Payment_No) HAVING COUNT(DISTINCT P.Replication_Site_Flag) > 1) THEN UPDATE WM_Payment SET Replication_Site_Flag = '0' WHERE ((Payment_No IN ( SELECT ISNULL(P.Refund_Payment_No, P.Payment_No) AS Refund_Pay_No FROM WM_Payment P WHERE ((P.Refund_Payment_No IS NOT NULL) OR EXISTS( SELECT 1 FROM WM_Payment RP WHERE RP.Refund_Payment_No = P.Payment_No)) AND ((P.Payment_No IN (SELECT Payment_No FROM inserted)) OR (P.Payment_No IN (SELECT Refund_Payment_No FROM inserted)) OR (P.Refund_Payment_No IN (SELECT Payment_No FROM inserted)) OR (P.Refund_Payment_No IN (SELECT Refund_Payment_No FROM inserted))) GROUP BY ISNULL(P.Refund_Payment_No, P.Payment_No) HAVING COUNT(DISTINCT P.Replication_Site_Flag) > 1)) --Linked Refund OR (Refund_Payment_No IN ( SELECT ISNULL(P.Refund_Payment_No, P.Payment_No) AS Refund_Pay_No FROM WM_Payment P WHERE ((P.Refund_Payment_No IS NOT NULL) OR EXISTS( SELECT 1 FROM WM_Payment RP WHERE RP.Refund_Payment_No = P.Payment_No)) AND ((P.Payment_No IN (SELECT Payment_No FROM inserted)) OR (P.Payment_No IN (SELECT Refund_Payment_No FROM inserted)) OR (P.Refund_Payment_No IN (SELECT Payment_No FROM inserted)) OR (P.Refund_Payment_No IN (SELECT Refund_Payment_No FROM inserted))) GROUP BY ISNULL(P.Refund_Payment_No, P.Payment_No) HAVING COUNT(DISTINCT P.Replication_Site_Flag) > 1))) --Payments referencing the Linked Refund AND (Replication_Site_Flag <> '0'); END IF; END IF; END; IF EXISTS( SELECT 1 FROM syspublication WHERE publication_name = 'WM_Pub_All_Nodes') THEN PASSTHROUGH ONLY FOR SUBSCRIPTION TO WM_Pub_All_Nodes; ALTER TRIGGER au_WM_Payment AFTER UPDATE ON "DBA".WM_Payment REFERENCING NEW AS inserted OLD AS deleted FOR EACH STATEMENT BEGIN DECLARE @PaymentTotal NUMERIC(8,2); --Refund_Payment_No must reference a payment of type 'R' (i.e. refund) IF UPDATE(Refund_Payment_No) AND EXISTS( SELECT 1 FROM inserted ins INNER JOIN WM_Payment P ON (P.Payment_No = ins.Refund_Payment_No) WHERE P.Payment_Type <> 'R') THEN RAISERROR 30000 'Refund_Payment_No must reference a Refund Payment.'; END IF; --Cannot create a reversal of an integrated EFTPOS payment IF UPDATE(Reference_Payment_No) AND EXISTS( SELECT 1 FROM inserted ins INNER JOIN WM_Payment P ON (P.Payment_No = ins.Reference_Payment_No) WHERE P.Eftpos_Transaction_Ref IS NOT NULL) THEN RAISERROR 30000 'Cannot reverse an integrated EFTPOS payment. A refund must be issued.'; END IF; --Amount Allocated must equal the sum of the Transaction Payment allocations -- UNLESS a reversal of the payment exists OR the payment is a reversal itself -- OR the Amount Allocated is set equal to the Amount Paid and no allocations exist -- OR the payment is linked to a Refund via the Refund_Payment_No field -- OR the payment is a linked Refund --If payment is a linked Refund, the Amount Allocated must equal the sum of the Amount Paid -- on all referencing payments (this includes referencing Refunds), less the total of payment -- allocations connected with referencing payments (and Refunds) --NOTE: Allow the total of allocations to differ from the payment Amount_Allocated during replication. -- This is necessary to permit the reconciliation of update conflicts that occur when payments are -- allocated at multiple sites between replication operations. This is intended to be a temporary state only -- and should be resolved by the time replication is complete. The view Payment_Allocation_Errors is examined at -- the conclusion of the replication process and if any exist, an error is logged. IF (UPDATE(Amount_Allocated) OR UPDATE(Reference_Payment_No) OR UPDATE(Refund_Payment_No) OR UPDATE(Amount_Paid)) AND CURRENT REMOTE USER IS NULL THEN IF EXISTS( SELECT 1 FROM inserted ins LEFT OUTER JOIN WM_Transaction_Payment TP ON (TP.Payment_No = ins.Payment_No) WHERE (ins.Reference_Payment_No IS NULL) --not a reversal AND (NOT EXISTS(SELECT 1 FROM WM_Payment P1 WHERE P1.Reference_Payment_No = ins.Payment_No)) --not reversed AND (Refund_Payment_No IS NULL) --does not have a linked Refund AND (NOT EXISTS(SELECT 1 FROM WM_Payment P2 WHERE P2.Refund_Payment_No = ins.Payment_No)) --not a linked Refund GROUP BY ins.Payment_No, ins.Amount_Paid, ins.Amount_Allocated HAVING ((SUM(TP.Amount) IS NOT NULL) AND (SUM(TP.Amount) <> ins.Amount_Allocated)) OR ((SUM(TP.Amount) IS NULL) AND (ins.Amount_Allocated <> 0) AND (ins.Amount_Allocated <> ins.Amount_Paid))) THEN RAISERROR 30000 'Total of Transaction Payment Allocations does not equal Payment Allocated Total.'; END IF; --Validate linked Refund payments (if present) IF EXISTS( SELECT 1 FROM inserted ins INNER JOIN WM_Payment P ON (P.Refund_Payment_No = ins.Payment_No)) THEN SELECT ISNULL(SUM(P.Amount_Paid), 0) INTO @PaymentTotal FROM inserted ins INNER JOIN WM_Payment P ON (P.Refund_Payment_No = ins.Payment_No); IF EXISTS( SELECT 1 FROM inserted ins INNER JOIN WM_Payment P ON (P.Refund_Payment_No = ins.Payment_No) LEFT OUTER JOIN WM_Transaction_Payment TP ON (TP.Payment_No = P.Payment_No) GROUP BY ins.Payment_No, ins.Amount_Allocated HAVING (-1 * ins.Amount_Allocated) <> (@PaymentTotal - SUM(ISNULL(TP.Amount, 0)))) THEN RAISERROR 30000 'Amount Allocated on linked Refund must equal Payment total less Payment allocations.'; END IF; END IF; END IF; --Set all Transactions' Replication_Site_Flag's to ZERO if the payment's flag is ZERO --NOTE: This query assumes that Payment_No will never be modified IF UPDATE(Replication_Site_Flag) AND EXISTS( SELECT 1 FROM WM_Transaction_Payment TP INNER JOIN inserted ins ON (ins.Payment_No = TP.Payment_No) INNER JOIN deleted del ON (del.Payment_No = ins.Payment_No) WHERE (ins.Replication_Site_Flag = '0') AND (del.Replication_Site_Flag <> '0')) THEN UPDATE WM_Transaction_Header SET Replication_Site_Flag = '0' WHERE Transaction_No IN (SELECT TP.Transaction_No FROM WM_Transaction_Payment TP INNER JOIN inserted ins ON (ins.Payment_No = TP.Payment_No) INNER JOIN deleted del ON (del.Payment_No = ins.Payment_No) WHERE (ins.Replication_Site_Flag = '0') AND (del.Replication_Site_Flag <> '0')); END IF; --Set Replication_Site_Flag to zero on all if the flags differ between the payments and the refund payment --This is necessary to ensure the linked refund stays with its payments --Trigger on WM_Payment will set associated transaction header flags to zero --%MG NOTE 6/06/2007 - Sybase appears to do a "complete boolean eval" so secondary IF clause -- has been moved inside the IF UPDATE clause. Otherwise, it executes each time which adds -- considerable time to the transaction save e.g. at GCCC, 3.5 seconds to do a single payment -- update, and there are two or more per transactions IF UPDATE(Replication_Site_Flag) THEN IF EXISTS( SELECT 1 FROM WM_Payment P WHERE ((P.Refund_Payment_No IS NOT NULL) OR EXISTS( SELECT 1 FROM WM_Payment RP WHERE RP.Refund_Payment_No = P.Payment_No)) AND ((P.Payment_No IN (SELECT Payment_No FROM inserted)) OR (P.Payment_No IN (SELECT Refund_Payment_No FROM inserted)) OR (P.Refund_Payment_No IN (SELECT Payment_No FROM inserted)) OR (P.Refund_Payment_No IN (SELECT Refund_Payment_No FROM inserted))) GROUP BY ISNULL(P.Refund_Payment_No, P.Payment_No) HAVING COUNT(DISTINCT P.Replication_Site_Flag) > 1) THEN UPDATE WM_Payment SET Replication_Site_Flag = '0' WHERE ((Payment_No IN ( SELECT ISNULL(P.Refund_Payment_No, P.Payment_No) AS Refund_Pay_No FROM WM_Payment P WHERE ((P.Refund_Payment_No IS NOT NULL) OR EXISTS( SELECT 1 FROM WM_Payment RP WHERE RP.Refund_Payment_No = P.Payment_No)) AND ((P.Payment_No IN (SELECT Payment_No FROM inserted)) OR (P.Payment_No IN (SELECT Refund_Payment_No FROM inserted)) OR (P.Refund_Payment_No IN (SELECT Payment_No FROM inserted)) OR (P.Refund_Payment_No IN (SELECT Refund_Payment_No FROM inserted))) GROUP BY ISNULL(P.Refund_Payment_No, P.Payment_No) HAVING COUNT(DISTINCT P.Replication_Site_Flag) > 1)) --Linked Refund OR (Refund_Payment_No IN ( SELECT ISNULL(P.Refund_Payment_No, P.Payment_No) AS Refund_Pay_No FROM WM_Payment P WHERE ((P.Refund_Payment_No IS NOT NULL) OR EXISTS( SELECT 1 FROM WM_Payment RP WHERE RP.Refund_Payment_No = P.Payment_No)) AND ((P.Payment_No IN (SELECT Payment_No FROM inserted)) OR (P.Payment_No IN (SELECT Refund_Payment_No FROM inserted)) OR (P.Refund_Payment_No IN (SELECT Payment_No FROM inserted)) OR (P.Refund_Payment_No IN (SELECT Refund_Payment_No FROM inserted))) GROU