vstokesjr

GenLCLExportDailyStats

Dec 28th, 2011
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 14.96 KB | None | 0 0
  1. -- --------------------------------------------------------------------------------
  2. -- Routine DDL
  3. -- --------------------------------------------------------------------------------
  4. DELIMITER $$
  5. DROP PROCEDURE IF EXISTS `GenLCLExportDailyStats` $$
  6. CREATE PROCEDURE `GenLCLExportDailyStats`( IN pSailDate INT(11) )
  7. MODIFIES SQL DATA
  8. MAIN: BEGIN
  9.     -- Server SQL Logging declarations.
  10.     DECLARE mSLS VARCHAR(64) DEFAULT "GenLCLExportDailyStats";
  11.     DECLARE mSLH VARCHAR(256) DEFAULT "";
  12.     DECLARE mSLT VARCHAR(128) DEFAULT UUID();
  13.     DECLARE mDEBUG BOOL DEFAULT FALSE;
  14.  
  15.     DECLARE mSailDate DATE DEFAULT NULL;
  16.  
  17.     DECLARE mTotalCBM DECIMAL(10,2) DEFAULT 0.00;
  18.     DECLARE mTotalKGS DECIMAL(10,2) DEFAULT 0.00;
  19.     DECLARE mTotalAPCost DECIMAL(10,2) DEFAULT 0.00;
  20.     DECLARE mTotalBiblesCost DECIMAL(10,2) DEFAULT 0.00;
  21.     DECLARE mTotalInvoice DECIMAL(10,2) DEFAULT 0.00;
  22.     DECLARE mTotalGenExp DECIMAL(10,2) DEFAULT 0.00;
  23.     DECLARE mTotalUnitVY DECIMAL(10,2) DEFAULT 0.00;
  24.     DECLARE mTotalChgs DECIMAL(10,2) DEFAULT 0.00;
  25.     DECLARE mTotalCns DECIMAL(10,2) DEFAULT 0.00;
  26.  
  27.     DECLARE mBLTerm VARCHAR(2) DEFAULT "";
  28.     DECLARE mPort VARCHAR(3) DEFAULT "";
  29.     DECLARE mDRTerm VARCHAR(2) DEFAULT "";
  30.     DECLARE mDRNum VARCHAR(6) DEFAULT "";
  31.     DECLARE mGLTerm VARCHAR(3) DEFAULT "";
  32.     DECLARE mCntVoy VARCHAR(4) DEFAULT "";
  33.     DECLARE mBLFdes VARCHAR(3) DEFAULT "";
  34.  
  35.     DECLARE mTRecs INT(11) DEFAULT 0;
  36.  
  37.     -- SQL Error Handler Variables
  38.     -- These must appear BEFORE cursor declarations.
  39.     DECLARE mRecordNotFound BOOL DEFAULT FALSE;
  40.     DECLARE mCursorEOL BOOL DEFAULT FALSE;
  41.  
  42.     -- SQL CURSOR declarations begin here.
  43.  
  44.     DECLARE mCursorA CURSOR FOR
  45.         SELECT
  46.             blterm, port, drterm, drnum, glterm, cntvoy, blfdes,
  47.             (
  48.                 IF ( ChgsChgCdeInGLNumb( chg01, "3440" ), amt01, 0.00 ) +
  49.                 IF ( ChgsChgCdeInGLNumb( chg02, "3440" ), amt02, 0.00 ) +
  50.                 IF ( ChgsChgCdeInGLNumb( chg03, "3440" ), amt03, 0.00 ) +
  51.                 IF ( ChgsChgCdeInGLNumb( chg04, "3440" ), amt04, 0.00 ) +
  52.                 IF ( ChgsChgCdeInGLNumb( chg05, "3440" ), amt05, 0.00 ) +
  53.                 IF ( ChgsChgCdeInGLNumb( chg06, "3440" ), amt06, 0.00 ) +
  54.                 IF ( ChgsChgCdeInGLNumb( chg07, "3440" ), amt07, 0.00 ) +
  55.                 IF ( ChgsChgCdeInGLNumb( chg08, "3440" ), amt08, 0.00 ) +
  56.                 IF ( ChgsChgCdeInGLNumb( chg09, "3440" ), amt09, 0.00 ) +
  57.                 IF ( ChgsChgCdeInGLNumb( chg10, "3440" ), amt10, 0.00 ) +
  58.                 IF ( ChgsChgCdeInGLNumb( chg11, "3440" ), amt11, 0.00 ) +
  59.                 IF ( ChgsChgCdeInGLNumb( chg12, "3440" ), amt12, 0.00 )
  60.             ) AS 'AMT',
  61.             IF( engmet = 'M', ( msr01 + msr02 + msr03 + msr04 + msr05 + msr06 ) / 100, ( msr01 + msr02 + msr03 + msr04 + msr05 + msr06 ) / 35.314 ) AS 'CBM',
  62.             IF( engmet = 'M', ( wgt01 + wgt02 + wgt03 + wgt04 + wgt05 + wgt06 ) / 100, ( wgt01 + wgt02 + wgt03 + wgt04 + wgt05 + wgt06 ) / 2.2046 ) AS 'KGS'
  63.         FROM histry
  64.         WHERE saildt = pSailDate
  65.         AND airocn='O';
  66.  
  67.     DECLARE mCursorB CURSOR FOR
  68.         SELECT DISTINCT port, glterm, cntvoy, blfdes
  69.         FROM histry
  70.         WHERE saildt = pSailDate
  71.         AND airocn='O';
  72.  
  73.     DECLARE mCursorC CURSOR FOR
  74.         SELECT DISTINCT glterm, PORT, blfdes, cntvoy
  75.         FROM histry
  76.         WHERE saildt = pSailDate
  77.         AND airocn='O';
  78.  
  79.     -- SQL CURSOR declarations end here.
  80.  
  81.     -- SQL Error Handlers
  82.     -- These must appear AFTER cursor declarations.
  83.     DECLARE CONTINUE HANDLER FOR NOT FOUND SET mRecordNotFound = TRUE;
  84.     DECLARE CONTINUE HANDLER FOR SQLSTATE "02000" SET mCursorEOL = TRUE;
  85.  
  86.     -- ****************************************************************
  87.     -- VALIDATION
  88.  
  89.     -- Set the SQL Server Log Header.
  90.     SET mSLH = CONCAT( ": pSailDate (", pSailDate, "): " );
  91.  
  92.     IF ISNULL( pSailDate ) OR pSailDate < 1 THEN
  93.         CALL LogSQLEntry( 1, mSLS, mSLT, CONCAT( "VALIDATION", mSLH, "Stopped; pSailDate invalid." ) );
  94.         LEAVE MAIN;
  95.     END IF;
  96.  
  97.     SET mSailDate = DATE_FORMAT( pSailDate, "%Y%m%d" );
  98.  
  99.     IF ISNULL( mSailDate ) THEN
  100.         CALL LogSQLEntry( 1, mSLS, mSLT, CONCAT( "VALIDATION", mSLH, "Stopped; pSailDate invalid." ) );
  101.         LEAVE MAIN;
  102.     END IF;
  103.  
  104.     -- VALIDATION
  105.     -- ****************************************************************
  106.  
  107.     -- Debug Entry
  108.     IF mDEBUG = TRUE THEN
  109.         CALL LogSQLEntry( 1, mSLS, mSLT, CONCAT( "DEBUG", mSLH, "Begins." ) );
  110.     END IF;
  111.  
  112.     -- Make sure the table is empty for the selected date
  113.   -- because this procedure regens all the data for entire date!
  114.     START TRANSACTION;
  115.     DELETE FROM ExportDailyStats WHERE SailDate = mSailDate;
  116.   COMMIT;
  117.  
  118.     -- *******************************************************
  119.     -- Loop A
  120.     -- Charges and Costs by BL
  121.  
  122.     START TRANSACTION;
  123.  
  124.     OPEN mCursorA;
  125.  
  126.     -- Debug Entry
  127.     IF mDEBUG = TRUE THEN
  128.         CALL LogSQLEntry( 1, mSLS, mSLT, CONCAT( "DEBUG: Cursor opened, starting loop A." ) );
  129.     END IF;
  130.  
  131.     LoopA: LOOP
  132.  
  133.         -- Reset
  134.         SET mTotalCBM = 0.00;
  135.         SET mTotalKGS = 0.00;
  136.         SET mTotalAPCost = 0.00;
  137.         SET mTotalInvoice = 0.00;
  138.         SET mTotalBiblesCost = 0.00;
  139.         SET mTotalChgs = 0.00;
  140.         SET mTotalCns = 0.00;
  141.  
  142.         SET mBLTerm = "";
  143.         SET mPort = "";
  144.         SET mDRTerm = "";
  145.         SET mDRNum = "";
  146.         SET mGLTerm = "";
  147.         SET mCntVoy = "";
  148.         SET mBLFdes = "";
  149.  
  150.         -- Get next record from the table.
  151.         SET mCursorEOL = FALSE;
  152.         FETCH mCursorA INTO  mBLTerm, mPort, mDRTerm, mDRNum, mGLTerm, mCntVoy, mBLFdes, mTotalChgs, mTotalCBM, mTotalKGS;
  153.  
  154.         IF mCursorEOL = TRUE THEN
  155.             LEAVE LoopA;
  156.         END IF;
  157.  
  158.         -- Get the total Correction Notices associated with this BL.
  159.         -- Only get the LATEST entry (may be more than one entry but the latest is the final).
  160.         SELECT
  161.             IF ( ChgsChgCdeInGLNumb( ccd01, "3440" ), cna01, 0.00 ) +
  162.             IF ( ChgsChgCdeInGLNumb( ccd02, "3440" ), cna02, 0.00 ) +
  163.             IF ( ChgsChgCdeInGLNumb( ccd03, "3440" ), cna03, 0.00 ) +
  164.             IF ( ChgsChgCdeInGLNumb( ccd04, "3440" ), cna04, 0.00 ) +
  165.             IF ( ChgsChgCdeInGLNumb( ccd05, "3440" ), cna05, 0.00 ) +
  166.             IF ( ChgsChgCdeInGLNumb( ccd06, "3440" ), cna06, 0.00 ) +
  167.             IF ( ChgsChgCdeInGLNumb( ccd07, "3440" ), cna07, 0.00 ) +
  168.             IF ( ChgsChgCdeInGLNumb( ccd08, "3440" ), cna08, 0.00 ) +
  169.             IF ( ChgsChgCdeInGLNumb( ccd09, "3440" ), cna09, 0.00 ) +
  170.             IF ( ChgsChgCdeInGLNumb( ccd10, "3440" ), cna10, 0.00 ) +
  171.             IF ( ChgsChgCdeInGLNumb( ccd11, "3440" ), cna11, 0.00 ) +
  172.             IF ( ChgsChgCdeInGLNumb( ccd12, "3440" ), cna12, 0.00 )
  173.         INTO mTotalCns
  174.         FROM cnoths
  175.         WHERE blkey = CONCAT( mBLTerm, mPort, mDRTerm, mDRNum )
  176.         ORDER BY cndate DESC
  177.         LIMIT 1;
  178.  
  179.         -- Get the Type 4 COSTS associated with this BL.
  180.         SELECT SUM( apcost.amount ) INTO mTotalAPCost
  181.         FROM bldr, apcost
  182.         WHERE bldr.blterm = mBLTerm
  183.         AND bldr.PORT = mPort
  184.         AND bldr.drterm = mDRTerm
  185.         AND bldr.drnum = mDRNum
  186.         AND apcost.type = '4'
  187.         AND apcost.askfld = CONCAT( bldr.dr1, bldr.dr2 )
  188.         AND apcost.gltrml = mGLTerm
  189.         AND ( FclCodFclCodInGLNumb( apcost.cstcde, "3403" ) = TRUE
  190.         OR FclCodFclCodInGLNumb( apcost.cstcde, "3492" ) = TRUE
  191.         OR FclCodFclCodInGLNumb( apcost.cstcde, "4009" ) = TRUE
  192.         OR FclCodFclCodInGLNumb( apcost.cstcde, "4010" ) = TRUE
  193.         OR FclCodFclCodInGLNumb( apcost.cstcde, "4030" ) = TRUE
  194.         OR FclCodFclCodInGLNumb( apcost.cstcde, "4041" ) = TRUE
  195.         OR FclCodFclCodInGLNumb( apcost.cstcde, "4130" ) = TRUE );
  196.  
  197.         -- Cleanup Results
  198.         IF ISNULL( mTotalCBM ) = TRUE THEN
  199.             SET mTotalCBM = 0.00;
  200.         END IF;
  201.  
  202.         IF ISNULL( mTotalKGS ) = TRUE THEN
  203.             SET mTotalKGS = 0.00;
  204.         END IF;
  205.  
  206.         IF ISNULL( mTotalAPCost ) = TRUE THEN
  207.             SET mTotalAPCost = 0.00;
  208.         END IF;
  209.  
  210.         IF ISNULL( mTotalInvoice ) = TRUE THEN
  211.             SET mTotalInvoice = 0.00;
  212.         END IF;
  213.  
  214.         IF ISNULL( mTotalBiblesCost ) = TRUE THEN
  215.             SET mTotalBiblesCost = 0.00;
  216.         END IF;
  217.  
  218.         IF ISNULL( mTotalChgs ) = TRUE THEN
  219.             SET mTotalChgs = 0.00;
  220.         END IF;
  221.  
  222.         IF ISNULL( mTotalCns ) = TRUE THEN
  223.             SET mTotalCns = 0.00;
  224.         END IF;
  225.  
  226.         -- Update temporary table.
  227.         INSERT INTO ExportDailyStats SET
  228.       SailDate = mSailDate,
  229.             LoadTerm = mGLTerm,
  230.             UnLoadTerm = mPort,
  231.             DestTerm = mBLFdes,
  232.             TotalCBM = mTotalCBM,
  233.             TotalKGS = mTotalKGS,
  234.             TotalAPCost4 = mTotalAPCost,
  235.             TotalChgs = mTotalChgs,
  236.             TotalCns = mTotalCns,
  237.             TotalBLs = 1,
  238.       RecLastUpdated = NULL
  239.         ON DUPLICATE KEY UPDATE
  240.             TotalCBM = TotalCBM + mTotalCBM,
  241.             TotalKGS = TotalKGS + mTotalKGS,
  242.             TotalAPCost4 = TotalAPCost4 + mTotalAPCost,
  243.             TotalChgs = TotalChgs + mTotalChgs,
  244.             TotalCns = TotalCns + mTotalCns,
  245.             TotalBLs = TotalBLs + 1,
  246.       RecLastUpdated = NULL;
  247.  
  248.     END LOOP LoopA;
  249.  
  250.     -- Cleanup
  251.     CLOSE mCursorA;
  252.  
  253.     COMMIT;
  254.  
  255.     -- *******************************************************
  256.     -- Loop B
  257.     -- Costs by VOYAGE (Voyages consists of multiple BLs!)
  258.  
  259.     START TRANSACTION;
  260.  
  261.     OPEN mCursorB;
  262.  
  263.     -- Debug Entry
  264.     IF mDEBUG = TRUE THEN
  265.         CALL LogSQLEntry( 1, mSLS, mSLT, CONCAT( "DEBUG: Cursor opened, starting loop B." ) );
  266.     END IF;
  267.  
  268.     LoopB: LOOP
  269.  
  270.         -- Reset
  271.         SET mTotalCBM = 0.00;
  272.         SET mTotalKGS = 0.00;
  273.         SET mTotalAPCost = 0.00;
  274.         SET mTotalBiblesCost = 0.00;
  275.         SET mTotalChgs = 0.00;
  276.         SET mTotalCns = 0.00;
  277.         SET mTotalInvoice = 0.00;
  278.         SET mTotalUnitVY = 0.00;
  279.  
  280.         SET mBLTerm = "";
  281.         SET mPort = "";
  282.         SET mDRTerm = "";
  283.         SET mDRNum = "";
  284.         SET mGLTerm = "";
  285.         SET mCntVoy = "";
  286.         SET mBLFdes = "";
  287.  
  288.         SET mTRecs = 0;
  289.  
  290.         -- Get next record from the table.
  291.         SET mCursorEOL = FALSE;
  292.         FETCH mCursorB INTO  mPort, mGLTerm, mCntVoy, mBLFdes;
  293.  
  294.         IF mCursorEOL = TRUE THEN
  295.             LEAVE LoopB;
  296.         END IF;
  297.  
  298.         -- Get the Type 5 COSTS associated with this Voyage.
  299.         SELECT SUM( amount ) INTO mTotalAPCost
  300.         FROM apcost
  301.         WHERE type = '5'
  302.         AND askfld LIKE CONCAT( mGLTerm, mPort, mCntVoy, "%" )
  303.         AND gltrml = mGLTerm
  304.         AND ( FclCodFclCodInGLNumb( cstcde, "3403" ) = TRUE
  305.         OR  FclCodFclCodInGLNumb( cstcde, "3492" ) = TRUE
  306.         OR FclCodFclCodInGLNumb( cstcde, "4009" ) = TRUE
  307.         OR FclCodFclCodInGLNumb( cstcde, "4010" ) = TRUE
  308.         OR FclCodFclCodInGLNumb( cstcde, "4030" ) = TRUE
  309.         OR FclCodFclCodInGLNumb( cstcde, "4041" ) = TRUE
  310.         OR FclCodFclCodInGLNumb( cstcde, "4130" ) = TRUE );
  311.  
  312.         IF ISNULL( mTotalAPCost ) = TRUE THEN
  313.             SET mTotalAPCost = 0.00;
  314.         END IF;
  315.  
  316.         SELECT IF( SUM( trlcbm ) != 0.00, SUM( trlcbm ), SUM( trlcft ) / 35.314 )
  317.     INTO mTotalUnitVY
  318.         FROM unitvy
  319.         WHERE voynum = CONCAT( mGLTerm, mPort, mCntVoy );
  320.  
  321.         IF ISNULL( mTotalUnitVY ) THEN
  322.             SET mTotalUnitVY = 0.00;
  323.         END IF;
  324.  
  325.         -- Get the Bibles associated with this Voyage.
  326.         SELECT amnt01 + amnt02 + amnt03 + amnt04 + amnt05 + amnt06 + amnt07 + amnt08 + amnt09 + amnt10 + amnt11 + amnt12
  327.         INTO mTotalBiblesCost
  328.         FROM bibles
  329.         WHERE trmnum =  mGLTerm
  330.         AND prtnum = mPort
  331.         AND voynum = mCntVoy;
  332.  
  333.         IF ISNULL( mTotalBiblesCost ) = TRUE THEN
  334.             SET mTotalBiblesCost = 0.00;
  335.         END IF;
  336.  
  337.         -- Update temporary table - with TotalUnitVY.
  338.         UPDATE ExportDailyStats SET
  339.             TotalAPCost5 = TotalAPCost5 + mTotalAPCost,
  340.             TotalUnitVY = TotalUnitVY + mTotalUnitVY,
  341.       TotalBiblesCost = TotalBiblesCost + mTotalBiblesCost,
  342.       RecLastUpdated = NULL
  343.         WHERE SailDate = mSailDate
  344.     AND LoadTerm = mGLTerm
  345.         AND UnloadTerm = mPort
  346.         AND DestTerm = mBLFdes;
  347.  
  348.     END LOOP LoopB;
  349.  
  350.     -- Cleanup
  351.     CLOSE mCursorB;
  352.  
  353.     -- *******************************************************
  354.     -- Loop C
  355.     -- Invoices by Voyage Destination
  356.  
  357.     OPEN mCursorC;
  358.  
  359.     -- Debug Entry
  360.     IF mDEBUG = TRUE THEN
  361.         CALL LogSQLEntry( 1, mSLS, mSLT, CONCAT( "DEBUG: Cursor opened, starting loop C." ) );
  362.     END IF;
  363.  
  364.     LoopC: LOOP
  365.  
  366.         -- Reset
  367.         SET mTotalInvoice = 0.00;
  368.  
  369.         SET mBLTerm = "";
  370.         SET mPort = "";
  371.         SET mDRTerm = "";
  372.         SET mDRNum = "";
  373.         SET mGLTerm = "";
  374.         SET mCntVoy = "";
  375.         SET mBLFdes = "";
  376.  
  377.         SET mTRecs = 0;
  378.  
  379.         -- Get next record from the table.
  380.         SET mCursorEOL = FALSE;
  381.         FETCH mCursorC INTO mGLTerm, mPort, mBLFdes, mCntVoy;
  382.  
  383.         IF mCursorEOL = TRUE THEN
  384.             LEAVE LoopC;
  385.         END IF;
  386.  
  387.         -- Get the INVOICE charges associated with this BL.
  388.         SELECT SUM( `InvAmts` ) INTO mTotalInvoice
  389.         FROM
  390.         (
  391.             SELECT DISTINCT invoic.invnum,
  392.             (
  393.                 IF ( ChgsChgCdeInGLNumb( invoic.chg01, "3492" ), invoic.amt01, 0.00 ) +
  394.                 IF ( ChgsChgCdeInGLNumb( invoic.chg01, "4030" ), invoic.amt01, 0.00 ) +
  395.                 IF ( ChgsChgCdeInGLNumb( invoic.chg02, "3492" ), invoic.amt02, 0.00 ) +
  396.                 IF ( ChgsChgCdeInGLNumb( invoic.chg02, "4030" ), invoic.amt02, 0.00 ) +
  397.                 IF ( ChgsChgCdeInGLNumb( invoic.chg03, "3492" ), invoic.amt03, 0.00 ) +
  398.                 IF ( ChgsChgCdeInGLNumb( invoic.chg03, "4030" ), invoic.amt03, 0.00 ) +
  399.                 IF ( ChgsChgCdeInGLNumb( invoic.chg04, "3492" ), invoic.amt04, 0.00 ) +
  400.                 IF ( ChgsChgCdeInGLNumb( invoic.chg04, "4030" ), invoic.amt04, 0.00 ) +
  401.                 IF ( ChgsChgCdeInGLNumb( invoic.chg05, "3492" ), invoic.amt05, 0.00 ) +
  402.                 IF ( ChgsChgCdeInGLNumb( invoic.chg05, "4030" ), invoic.amt05, 0.00 ) +
  403.                 IF ( ChgsChgCdeInGLNumb( invoic.chg06, "3492" ), invoic.amt06, 0.00 ) +
  404.                 IF ( ChgsChgCdeInGLNumb( invoic.chg06, "4030" ), invoic.amt06, 0.00 ) +
  405.                 IF ( ChgsChgCdeInGLNumb( invoic.chg07, "3492" ), invoic.amt07, 0.00 ) +
  406.                 IF ( ChgsChgCdeInGLNumb( invoic.chg07, "4030" ), invoic.amt07, 0.00 ) +
  407.                 IF ( ChgsChgCdeInGLNumb( invoic.chg08, "3492" ), invoic.amt08, 0.00 ) +
  408.                 IF ( ChgsChgCdeInGLNumb( invoic.chg08, "4030" ), invoic.amt08, 0.00 ) +
  409.                 IF ( ChgsChgCdeInGLNumb( invoic.chg09, "3492" ), invoic.amt09, 0.00 ) +
  410.                 IF ( ChgsChgCdeInGLNumb( invoic.chg09, "4030" ), invoic.amt09, 0.00 ) +
  411.                 IF ( ChgsChgCdeInGLNumb( invoic.chg10, "3492" ), invoic.amt10, 0.00 ) +
  412.                 IF ( ChgsChgCdeInGLNumb( invoic.chg10, "4030" ), invoic.amt10, 0.00 ) +
  413.                 IF ( ChgsChgCdeInGLNumb( invoic.chg11, "3492" ), invoic.amt11, 0.00 ) +
  414.                 IF ( ChgsChgCdeInGLNumb( invoic.chg11, "4030" ), invoic.amt11, 0.00 ) +
  415.                 IF ( ChgsChgCdeInGLNumb( invoic.chg12, "3492" ), invoic.amt12, 0.00 ) +
  416.                 IF ( ChgsChgCdeInGLNumb( invoic.chg12, "4030" ), invoic.amt12, 0.00 )
  417.             ) AS `InvAmts`
  418.             FROM histry, bldr, invdrs, invoic
  419.             WHERE histry.saildt = pSailDate
  420.             AND histry.port = mPort
  421.             AND histry.glterm = mGLTerm
  422.             AND histry.airocn = 'O'
  423.             AND histry.cntvoy = mCntVoy
  424.             AND bldr.blterm = histry.`blterm`
  425.             AND bldr.PORT = histry.`port`
  426.             AND bldr.drterm = histry.`drterm`
  427.             AND bldr.drnum = histry.`drnum`
  428.             AND invdrs.drcnum = CONCAT( bldr.dr1, bldr.dr2 )
  429.             AND invoic.cmpnum = invdrs.cmpnum
  430.             AND invoic.trmnum = invdrs.trmnum
  431.             AND invoic.invnum = invdrs.invnum
  432.             AND invoic.cntnum = invdrs.cntnum
  433.         ) AS `SumAllInvoices`;
  434.  
  435.         IF ISNULL( mTotalInvoice ) = FALSE OR mTotalInvoice != 0.00 THEN
  436.             -- Update temporary table - with TotalUnitVY.
  437.             UPDATE ExportDailyStats SET
  438.                 TotalInvoice = TotalInvoice + mTotalInvoice,
  439.               RecLastUpdated = NULL
  440.       WHERE SailDate = mSailDate
  441.       AND LoadTerm = mGLTerm
  442.       AND UnloadTerm = mPort
  443.       AND DestTerm = mBLFdes;
  444.         END IF;
  445.  
  446.     END LOOP LoopC;
  447.  
  448.     -- Cleanup
  449.     CLOSE mCursorC;
  450.  
  451.  
  452.     -- *******************************************************
  453.     -- General Expenses
  454.     -- NOT CORRECT
  455.     UPDATE ExportDailyStats, genexp SET
  456.         TotalGenExp = TotalGenExp +
  457.             ( amnt01 + amnt02 + amnt03 + amnt04 + amnt05 + amnt06 +
  458.             amnt07 + amnt08 + amnt09 + amnt10 + amnt11 + amnt12 ),
  459.     RecLastUpdated = NULL
  460.     WHERE SailDate = mSailDate
  461.   AND LoadTerm = genexp.trmnum
  462.     AND UnloadTerm = genexp.prtnum
  463.     AND genexp.geyear = "0000"
  464.     AND genexp.gemnth = "00";
  465.  
  466.     COMMIT;
  467.  
  468.     -- Debug Entry
  469.     IF mDEBUG = TRUE THEN
  470.         CALL LogSQLEntry( 1, mSLS, mSLT, "DEBUG: Ends normally." );
  471.     END IF;
  472.  
  473. END MAIN $$
Add Comment
Please, Sign In to add comment