博主写的Route收费代码,随笔记录下.

CREATE OR REPLACE PACKAGE BODY TMS.tms_intf_outbound
/* Version 1.8.0 */
AS
   --The procedue is to charge  drop off items which routes are finished and not be charged yet.
   --The parameter p_bol is used when we want to charge items with a specific route no for test purpose, it's not used in a real case yet.
   PROCEDURE charge_ground_freight (p_bol NUMBER := NULL)
   IS
   BEGIN
      DECLARE
         v_now   DATE := SYSDATE;
      BEGIN
         --Set routes which are to be process , the current time will be used to as a filter.
         --Since we are not always get routes to be charged at every hour,so we can return if we did not get any candidate routes..
         UPDATE   tms_route_header
            SET   charged_date = v_now
          WHERE       end_of_day IS NOT NULL
                  AND charged_date IS NULL
                  AND (p_bol IS NULL OR bill_of_lading_id = p_bol);

         IF sql%NOTFOUND
         THEN
            RETURN;
         END IF;

         --Insert all details to temp table which are similiar to tms_stop_detail.
         --The reason we do not filter pick up or exception records etc. at the sql since we need to pass those data by move_to_intf_for_wms, so we did not delete them in this sql, we just set their process stetus to processed later.
         INSERT INTO tms.tms_temp_sd_update (detail_row,
                                             charge_status,
                                             gl_seq_nbr,
                                             item_id,
                                             carrier_cd,
                                             next_detail_id,
                                             weight,
                                             task_type,
                                             delete_flag,
                                             stop_id,
                                             truck_route_instance_id,
                                             shipping_unit_id,
                                             host_created_dt,
                                             package_type,
                                             exception_code,
                                             stop_detail_id)
            SELECT   dtl.ROWID,
                     'IN-PROCESS',
                     dtl.gl_seq_nbr,
                     dtl.item_id,
                     dtl.carrier_cd,
                     dtl.next_detail_id,
                     dtl.weight,
                     dtl.task_type,
                     dtl.delete_flag,
                     DECODE (NVL (dtl.destination_id, 0),
                             --In case , tms_ws update destination_id to 0 .
                             0, dtl.stop_id,
                             dtl.destination_id),
                     dtl.truck_route_instance_id,
                     dtl.shipping_unit_id,
                     dtl.host_created_dt,
                     dtl.package_type,
                     dtl.exception_code,
                     dtl.stop_detail_id
              FROM   tms_stop_detail dtl, tms_route_header hdr
             WHERE   NVL (dtl.status, 'O') != 'O'
                     AND hdr.truck_route_instance_id =
                           dtl.truck_route_instance_id
                     AND hdr.end_of_day IS NOT NULL
                     AND dtl.part_dt > SYSDATE - 21
                     AND hdr.charged_date = v_now;

         --Inserting pallet contents based on sr freight since those contents may not creat when created or downloaded.
         INSERT INTO tms.tms_pallet_content (stop_detail_id,
                                             business_unit,
                                             business_unit_dest,
                                             ship_dt,
                                             item_id,
                                             qty,
                                             uom,
                                             control,
                                             weight,
                                             uom_wt,
                                             carrier_cd,
                                             request_id,
                                             intf_dt,
                                             prcs_instance,
                                             request_type,
                                             gl_sequence_number,
                                             pallet_container_id,
                                             request_ship_to_cd)
            SELECT   tmp.stop_detail_id,
                     sr.business_unit,
                     sr.business_unit_dest,
                     sr.ship_dt,
                     sr.item_id,
                     sr.qty,
                     sr.uom,
                     sr.control,
                     sr.weight,
                     sr.uom_wt,
                     sr.carrier_cd,
                     sr.request_id,
                     sr.intf_dt,
                     sr.prcs_instance,
                     sr.request_type,
                     sr.gl_sequence_number,
                     sr.pallet_container_id,
                     sr.request_ship_to_cd
              FROM   tms.tms_route_header thdr,
                     tms.tms_location loc,
                     tms.tms_temp_sd_update tmp,
                     tms.tms_location stoploc,
                     tms.wms_intf_sr_freight sr,
                     tms_route_stop tsp
             WHERE   thdr.truck_route_instance_id =
                        tmp.truck_route_instance_id
                     AND thdr.dispatch_location_id = loc.location_id
                     AND tmp.package_type != 'LOOSE'
                     AND tmp.stop_id = tsp.stop_id
                     AND tsp.location_id = stoploc.location_id
                     AND sr.business_unit = loc.business_unit
                     AND sr.business_unit_dest = stoploc.business_unit
                     AND sr.ship_dt > tmp.host_created_dt - 7
                     AND tmp.shipping_unit_id IS NOT NULL
                     AND tmp.shipping_unit_id = sr.pallet_container_id;

         --Exclude any details which next_detail_id are  existed , those items should not be charged since they are not the terminal.
         -- Exclude any weight more than 40000 pounds because it is wrong.
         -- Exclude anything which is not a drop off because we only charge for drops.
         -- Exclude any item that has been removed from the trailer.
         -- Exclude anything not delivered by FTRK or BGEX.
         UPDATE   tms_temp_sd_update dtl
            SET   dtl.charge_status = 'PROCESSED'
          WHERE   dtl.charge_status = 'IN-PROCESS'
                  AND (   dtl.next_detail_id IS NOT NULL
                       OR exception_code IS NOT NULL
                       OR dtl.weight > 40000
                       OR dtl.task_type <> 'DROP'
                       OR dtl.delete_flag = 'Y'
                       OR carrier_cd NOT IN ('FTRK', 'BGEX'));

         -- Exclude any delivery which is not to a store.
         UPDATE   tms_temp_sd_update dtl
            SET   dtl.charge_status = 'PROCESSED'
          WHERE   dtl.charge_status = 'IN-PROCESS'
                  AND EXISTS
                        (SELECT   NULL
                           FROM   tms_route_stop stp, tms_location loc
                          WHERE       stp.stop_id = dtl.stop_id
                                  AND loc.location_id = stp.location_id
                                  AND loc.ibu_group <> 'BRANCH');

         -- Exclude any items which are sent from pack center.
         UPDATE   tms_temp_sd_update dtl
            SET   dtl.charge_status = 'PROCESSED'
          WHERE   dtl.charge_status = 'IN-PROCESS'
                  AND EXISTS
                        (SELECT   NULL
                           FROM   tms_route_header hdr, tms_location loc
                          WHERE   hdr.truck_route_instance_id =
                                     dtl.truck_route_instance_id
                                  AND loc.location_id =
                                        hdr.dispatch_location_id
                                  AND loc.ibu_group = 'PACK_CNTR');

         --The tms_freight_table is used to record the charge details for every items we can use on reports and traces , inclduing BGEX,FTRK,BGND type.
         --There will have some records without charge in this table, since we need to record CSP and Exclusion items, so we did not exlude them out. This is a big diffrence then before.
         -- Create every row for each item in tms_stop_detail that ready for a charge for BGEX,FTRK.
         --TODO: round for temp,we can make sure this match old version since old version only care weight as integer .
         INSERT INTO tms_freight_detail (original_rowid,
                                         business_unit,
                                         ship_dt,
                                         carrier_cd,
                                         weight,
                                         charged_weight,
                                         hub,
                                         shipping_unit_id,
                                         update_cd,
                                         stop_detail_id)
            SELECT   shp.detail_row,
                     shp.business_unit,
                     shp.ship_date,
                     shp.carrier_cd,
                     weight,
                     DECODE (SIGN (shp.net_weight), -1, 0, shp.net_weight)
                        net_weight,
                     shp.hub_business_unit,
                     shp.shipping_unit_id,
                     'READY',
                     shp.stop_detail_id
              FROM   (SELECT   dtl.detail_row,
                               loc.business_unit,
                               ROUND (weight) weight,
                               dtl.shipping_unit_id,
                               TRUNC (hdr.end_of_day) ship_date,
                               loch.business_unit hub_business_unit,
                               NVL (dtl.carrier_cd, 'FTRK') carrier_cd,
                               ROUND(NVL (dtl.weight, 0)
                                     - NVL (
                                          (SELECT   SUM (tpc.weight)
                                                       csp_weight
                                             FROM   tms_pallet_content tpc
                                            WHERE   EXISTS
                                                       (SELECT   1
                                                          FROM   tms_frgt_bu_mvw buf,
                                                                 tms_frgt_itm_mvw itmf
                                                         WHERE   itmf.inv_item_id =
                                                                    tpc.item_id
                                                                 AND buf.business_unit =
                                                                       loc.business_unit
                                                                 AND buf.fas_freight_number =
                                                                       itmf.fas_freight_number)
                                                    AND tpc.stop_detail_id =
                                                          dtl.stop_detail_id
                                                    AND tpc.business_unit_dest =
                                                          loc.business_unit
                                                    AND tpc.business_unit =
                                                          loch.business_unit
                                                    AND tpc.pallet_container_id =
                                                          dtl.shipping_unit_id),
                                          0
                                       ))
                                  net_weight,
                               dtl.stop_detail_id
                        FROM   tms_temp_sd_update dtl,
                               tms_route_stop stp,
                               tms_location loc,
                               tms_route_header hdr,
                               tms_location loch
                       WHERE   stp.stop_id = dtl.stop_id
                               AND loc.location_id = stp.location_id
                               AND hdr.truck_route_instance_id =
                                     dtl.truck_route_instance_id
                               AND loch.location_id =
                                     hdr.dispatch_location_id
                               AND dtl.charge_status = 'IN-PROCESS') shp;

         --CSP items that we need to exclude. but we need those items in freight detail table.
         --This is purposed mainly for LOOSE types .
         UPDATE   tms_freight_detail tfd
            SET   charged_weight = 0
          WHERE   update_cd = 'READY'
                  AND EXISTS
                        (SELECT   1
                           FROM   tms_temp_sd_update dtl,
                                  tms_frgt_itm_mvw itmf,
                                  tms_route_stop stp,
                                  tms_location loc,
                                  tms_frgt_bu_mvw buf
                          WHERE       dtl.charge_status = 'IN-PROCESS'
                                  AND dtl.detail_row = tfd.original_rowid
                                  AND itmf.inv_item_id = dtl.item_id
                                  AND stp.stop_id = dtl.stop_id
                                  AND loc.location_id = stp.location_id
                                  AND buf.business_unit = loc.business_unit
                                  AND buf.fas_freight_number =
                                        itmf.fas_freight_number);

         --Set the  weight and charge informations for items.
         --We are simplify the process by using a new MV tms_charge_rate.
         --SHorten the SQL, the previous SQL is duplicate and mass.
         UPDATE   tms_freight_detail wgt
            SET
                  (business_unit_gl,
                  deptid,
                  charged_cost,
                  charged_freight,
                  currency_cd,
                  charge_per_pound,
                  cur_exchng_rt
                  ) =
                     (SELECT   tbrm.business_unit_gl,
                               tbrm.business_unit_7,
                               ROUND (
                                  chargerate_per_pound * wgt.charged_weight,
                                  4
                               ),
                               ROUND (
                                  chargerate_per_pound * wgt.charged_weight,
                                  4
                               ),
                               tbrm.base_currency,
                               tbrm.charge_per_pound,
                               tbrm.cur_exchng_rt
                        FROM   tms.tms_charge_rate tbrm
                       WHERE   tbrm.business_unit = wgt.business_unit
                               AND tbrm.carrier_cd = wgt.carrier_cd)
          WHERE   wgt.update_cd = 'READY';

         --Exclusion will not be charged in freight detail tables .
         UPDATE   tms_freight_detail tfd
            SET   charged_freight = 0
          WHERE   update_cd = 'READY'
                  AND EXISTS
                        (SELECT   1
                           FROM   tms_temp_sd_update dtl,
                                  tms_route_header hdr,
                                  tms_route_stop stp,
                                  tms_location loc,
                                  tms.tms_charge_exclusion exc
                          WHERE   dtl.detail_row = tfd.original_rowid
                                  AND dtl.charge_status = 'IN-PROCESS'
                                  AND hdr.truck_route_instance_id =
                                        dtl.truck_route_instance_id
                                  AND stp.stop_id = dtl.stop_id
                                  AND loc.location_id = stp.location_id
                                  AND exc.business_unit = loc.business_unit
                                  AND TRUNC (
                                        hdr.scheduled_departure_date - 0.25
                                     ) BETWEEN exc.start_dt
                                           AND  exc.end_dt);

         --Remove useless data.
         --TODO: Send email;
         DELETE FROM   tms_freight_detail
               WHERE   update_cd = 'READY' AND charged_cost IS NULL;

         --Like CSP or Exclusion items will not be considered to next step. The reason
         --why we did not update charge_status in tms_freight_detail above since we need to update tms_temp_sd_update table satus here.
         UPDATE   tms_temp_sd_update dtl
            SET   dtl.charge_status = 'PROCESSED'
          WHERE   EXISTS
                     (SELECT   NULL
                        FROM   tms_freight_detail chg
                       WHERE       dtl.detail_row = chg.original_rowid
                               AND charged_freight = 0
                               AND update_cd = 'READY');

         UPDATE   tms_freight_detail
            SET   update_cd = 'PROCESSED'
          WHERE   charged_freight = 0 AND update_cd = 'READY';

         -- Group data which will need to be put into ps_fas_gl_blugbl.
         INSERT INTO tms.tms_temp_gl_blugbl (business_unit_gl,
                                             deptid,
                                             ship_date,
                                             seq_nbr,
                                             account,
                                             fas_blue_globl_amt,
                                             currency_cd,
                                             descr30,
                                             hub,
                                             carrier_cd,
                                             business_unit,
                                             charge_per_pound,
                                             cur_exchng_rt)
            SELECT   business_unit_gl,
                     deptid,
                     ship_dt,
                     tms_bg_gl_charge.NEXTVAL,
                     acct,
                     sum_money,
                     currency_cd,
                     description,
                     hub,
                     carrier_cd,
                     business_unit,
                     charge_per_pound,
                     cur_exchng_rt
              FROM   (  SELECT   business_unit_gl,
                                 deptid,
                                 ship_dt,
                                 DECODE (wgt.carrier_cd,
                                         'FTRK',
                                         '6706',
                                         'BGEX',
                                         '6707')
                                    acct,
                                 ROUND (SUM (charged_freight), 2) sum_money,
                                 currency_cd,
                                    TO_CHAR (wgt.ship_dt, 'mm/dd')
                                 || ' '
                                 || wgt.hub
                                 || ' delivery'
                                 || SUBSTR (
                                       TO_CHAR (SUM (charged_weight), '99,999'),
                                       2
                                    )
                                 || ' lbs'
                                    description,
                                 hub,
                                 carrier_cd,
                                 business_unit,
                                 charge_per_pound,
                                 cur_exchng_rt
                          FROM   tms_freight_detail wgt
                         WHERE   update_cd = 'READY'
                      GROUP BY   business_unit_gl,
                                 deptid,
                                 ship_dt,
                                 carrier_cd,
                                 currency_cd,
                                 hub,
                                 carrier_cd,
                                 business_unit,
                                 charge_per_pound,
                                 cur_exchng_rt);

         DELETE FROM   tms_temp_gl_blugbl
               WHERE   fas_blue_globl_amt = 0;

         -- Put data into GL staging table.
         INSERT INTO ps_fas_gl_blugbl (business_unit_gl,
                                       deptid,
                                       ship_date,
                                       seq_nbr,
                                       account,
                                       fas_blue_globl_amt,
                                       currency_cd,
                                       descr30,
                                       oprid_entered_by,
                                       gl_distrib_status,
                                       process_instance)
            SELECT   business_unit_gl,
                     deptid,
                     ship_date,
                     seq_nbr,
                     account,
                     fas_blue_globl_amt,
                     currency_cd,
                     descr30,
                     'WMSLOAD',
                     'N',
                     0
              FROM   tms_temp_gl_blugbl;

         -- Record gl sequence number in associated tables that we already charged in peoplesoft tables.

         UPDATE   tms_freight_detail tfd
            SET   tfd.gl_seq_nbr =
                     (SELECT   gl.seq_nbr
                        FROM   tms_temp_gl_blugbl gl
                       WHERE       gl.business_unit = tfd.business_unit
                               AND gl.ship_date = tfd.ship_dt
                               AND gl.carrier_cd = tfd.carrier_cd
                               AND gl.hub = tfd.hub
                               AND tfd.update_cd = 'READY')
          WHERE   tfd.update_cd = 'READY';

         MERGE INTO   tms_stop_detail dtl
              USING   (SELECT   tfd.original_rowid, tfd.gl_seq_nbr
                         FROM   tms_freight_detail tfd
                        WHERE   tfd.update_cd = 'READY') tmp
                 ON   (dtl.ROWID = tmp.original_rowid)
         WHEN MATCHED
         THEN
            UPDATE SET dtl.gl_seq_nbr = tmp.gl_seq_nbr where dtl.part_dt > SYSDATE - 21;

         UPDATE   tms_freight_detail
            SET   update_cd = NVL2 (gl_seq_nbr, 'PROCESSED', 'FAILED')
          WHERE   update_cd = 'READY';

         COMMIT;
      END;
   END charge_ground_freight;

   -----------------------------------------------------------------

   --The big purpose to rewrite this function is to provide BGND details to tms_freight_detail.
   --Peoplesoft did not care items, only how much moneys from which hub, branch and ship date.
   PROCEDURE charge_bgnd_freight
   IS
   BEGIN
      BEGIN
         --If one item is delivered more than once a day, it should only be charged once, so rn=1 is added.
         --At the end of this procedure, all data before today will be marked as processed, so only one row will have
         --charge detail, and the other rows will just be marked as processed
         --Also, check if the business_unit is correct DC.
         --Check if the business_unit_dest is not a vendor store.
         INSERT INTO tms_freight_detail (original_rowid,
                                         business_unit,
                                         ship_dt,
                                         hub,
                                         weight,
                                         carrier_cd,
                                         shipping_unit_id,
                                         update_cd,
                                         deptid)
            SELECT   update_row,
                     business_unit_dest,
                     ship_date,
                     hub5,
                     weight,
                     carrier_cd,
                     location_id,
                     'READY',
                     business_unit_7
              FROM   (SELECT /*+ index(wms_intf_sr_freight WMS_INTF_SR_FREIGHT_IDX1) */
                            sr .ROWID update_row,
                               sr.business_unit_dest,
                               TRUNC (sr.ship_dt - .25) ship_date,
                               sr.business_unit hub5,
                               sr.weight,
                               sr.carrier_cd,
                               sr.location_id,
                               hubstores.business_unit_7,
                               ROW_NUMBER ()
                                  OVER (PARTITION BY sr.location_id
                                        ORDER BY sr.ship_dt)
                                  rn
                        FROM   wms_intf_sr_freight sr,
                               tms_location hubs,
                               tms_location hubstores
                       WHERE   sr.business_unit = hubs.business_unit
                               AND hubstores.business_unit =
                                     sr.business_unit_dest
                               AND hubstores.location_type != 'VENDOR'
                               AND hubs.ibu_group = 'DIST_CNTR'
                               AND sr.carrier_cd = 'BGND'
                               AND sr.intf_status_cd = 'READY'
                               AND sr.ship_dt < TRUNC (SYSDATE - .25) + .25)
             WHERE   rn = 1;

         --The tms_temp_gl_blugbl table is used to store rate, total money informations for each items in the source above.
         --The temp table is used for prepare  data to ps_fas_gl_blugbl, at the same time , it will be used to update tms_freight_detail.
         --THe weight in this table is a sum weight for same branch,hub, ship date.
         --The exclusion is caculated since we need to know the rate,weight information as well.
         --Insert into tms_temp_gl_blugbl table which sum the total money by business_unit.
         --tms_carrier_program and tms_carrier_rate are used to determine which rates those items are.
         --The inner view rates is used to do a rate transtion to USD.
         --The inner view weight_shipped is used to get a list of shipped items with row seq , particularly the weight is a sum weight for every items. that's why we had a field piece_weight.
         INSERT INTO tms_temp_gl_blugbl (business_unit_gl,
                                         deptid,
                                         ship_date,
                                         fas_blue_globl_amt,
                                         currency_cd,
                                         hub,
                                         account,
                                         descr30,
                                         carrier_cd,
                                         business_unit,
                                         charge_per_pound,
                                         weight,
                                         cur_exchng_rt,
                                         detail_row,
                                         row_seq,
                                         is_exclude,
                                         caculate_weight)
            WITH rates
                   AS (SELECT   DISTINCT cur_exchng_rt, base_currency
                         FROM   tms_charge_rate),
                stores
                   AS (SELECT   business_unit,
                                business_unit_gl,
                                base_currency,
                                NVL (service_hub, '') servicing_hub
                         FROM   tms_location
                        WHERE   ibu_group = 'BRANCH'
                                AND business_unit IS NOT NULL),
                shipped
                   AS (SELECT   original_rowid update_row,
                                business_unit business_unit_dest,
                                ship_dt ship_date,
                                hub hub5,
                                weight,
                                carrier_cd,
                                deptid
                         FROM   tms_freight_detail
                        WHERE   carrier_cd = 'BGND' AND update_cd = 'READY'),
                weight_shipped
                   AS (SELECT   update_row,
                                business_unit_dest,
                                ship_date,
                                hub5,
                                deptid,
                                s.carrier_cd,
                                stores.business_unit_gl,
                                stores.base_currency,
                                stores.servicing_hub,
                                CEIL(SUM(weight)
                                        OVER (
                                           PARTITION BY business_unit_dest,
                                                        ship_date,
                                                        hub5
                                        ))
                                   weight,
                                SUM(DECODE (weight, 0, 1, weight))
                                   OVER (
                                      PARTITION BY business_unit_dest,
                                                   ship_date,
                                                   hub5
                                   )
                                   caculate_weight,
                                ROW_NUMBER ()
                                   OVER (
                                      PARTITION BY business_unit_dest,
                                                   ship_date,
                                                   hub5
                                      ORDER BY update_row
                                   )
                                   first_row
                         FROM   shipped s, stores
                        WHERE   s.business_unit_dest = stores.business_unit),
                default_program AS (SELECT   carrier_cd, program_id
                                      FROM   tms.tms_carrier_program
                                     WHERE   to_dc = 'DEFLT'),
                exclusion
                   AS (SELECT   DISTINCT
                                w.ship_date,
                                w.business_unit_dest business_unit
                         FROM   weight_shipped w, tms.tms_charge_exclusion x
                        WHERE   w.business_unit_dest = x.business_unit
                                AND w.ship_date BETWEEN x.start_dt
                                                    AND  x.end_dt),
                chargeable_line
                   AS (SELECT                               /*+ leading (f) */
                             ws .business_unit_gl,
                                ws.deptid,
                                ws.ship_date,
                                hub5,
                                ws.weight,
                                ws.caculate_weight,
                                ws.carrier_cd,
                                (SELECT   cur_exchng_rt
                                   FROM   rates
                                  WHERE   base_currency = ws.base_currency)
                                   rate_mult,
                                ws.base_currency,
                                business_unit_dest,
                                update_row,
                                first_row,
                                NVL (p.program_id, pd.program_id) program_id,
                                NVL2 (x.ship_date, 'Y', 'N') is_exclude
                         FROM   weight_shipped ws,
                                tms.tms_carrier_program p,
                                default_program pd,
                                exclusion x
                        WHERE       ws.carrier_cd = p.carrier_cd(+)
                                AND ws.servicing_hub = p.to_dc(+)
                                AND ws.carrier_cd = pd.carrier_cd(+)
                                AND ws.ship_date = x.ship_date(+)
                                AND ws.business_unit_dest =
                                      x.business_unit(+)),
                max_rate
                   AS (  SELECT   program_id,
                                  MAX (charge) / MAX (weight) top_rate
                           FROM   tms.tms_carrier_rate
                       GROUP BY   program_id)
              SELECT                                        /*+ leading (f) */
                    f  .business_unit_gl,
                       f.deptid,
                       f.ship_date,
                       ROUND (
                          NVL (r.charge,
                               ROUND (f.weight * max_rate.top_rate, 2))
                          * rate_mult,
                          2
                       ),
                       f.base_currency,
                       f.hub5,
                       6708,
                          TO_CHAR (f.ship_date + 1, 'mm/dd')
                       || ' '
                       || f.hub5
                       || ' '
                       || f.carrier_cd
                       || SUBSTR (TO_CHAR (f.weight, '99,999'), 2)
                       || ' lbs',
                       f.carrier_cd,
                       f.business_unit_dest,
                       ROUND (
                          NVL (r.charge,
                               ROUND (f.weight * max_rate.top_rate, 2))
                          * rate_mult,
                          2
                       )
                       / DECODE (f.weight, 0, 1, f.weight),
                       f.weight,
                       f.rate_mult,
                       f.update_row,
                       f.first_row,
                       is_exclude,
                       f.caculate_weight
                FROM   chargeable_line f, tms.tms_carrier_rate r, max_rate
               WHERE       f.weight = r.weight(+)
                       AND f.program_id = r.program_id(+)
                       AND f.program_id = max_rate.program_id
            ORDER BY   deptid,
                       ship_date,
                       hub5,
                       first_row;

         --Caculate weight for each item which comes from tms_freight_detail.
         --Connect by original rowid which is a ROWID type.
         UPDATE   tms_temp_gl_blugbl blg
            SET   blg.piece_weight =
                     NVL (
                        (SELECT   tfd.weight
                           FROM   tms_freight_detail tfd
                          WHERE   tfd.original_rowid = blg.detail_row
                                  AND ROWNUM <= 1),
                        0
                     );

         --Clear items which not get any chargement ,including exclusion items.
         DELETE FROM   tms_temp_gl_blugbl
               WHERE   fas_blue_globl_amt = 0;

         --Update seq_nbr column by the first row_seq, since the weight is a total value,
         --we do not need to consider other row_seq.
         UPDATE   tms_temp_gl_blugbl
            SET   seq_nbr = tms_bg_gl_charge.NEXTVAL
          WHERE   row_seq = 1 AND is_exclude = 'N';

         --Set other row_seq value items by row_seq which is 1.
         --The reason we set the seq_nbr for every item since we will update them back to tms_freight_detail.
         UPDATE   tms_temp_gl_blugbl a
            SET   seq_nbr =
                     (SELECT   b.seq_nbr
                        FROM   tms_temp_gl_blugbl b
                       WHERE       a.business_unit = b.business_unit
                               AND a.ship_date = b.ship_date
                               AND a.hub = b.hub
                               AND b.row_seq = 1
                               AND is_exclude = 'N')
          WHERE   a.row_seq != 1 AND is_exclude = 'N';

         --Insert into peoplesoft interface table based on the temp gl table .
         INSERT INTO ps_fas_gl_blugbl (business_unit_gl,
                                       seq_nbr,
                                       deptid,
                                       ship_date,
                                       account,
                                       fas_blue_globl_amt,
                                       currency_cd,
                                       descr30,
                                       oprid_entered_by,
                                       gl_distrib_status,
                                       process_instance)
            SELECT   business_unit_gl,
                     seq_nbr,
                     deptid,
                     ship_date,
                     account,
                     fas_blue_globl_amt,
                     currency_cd,
                     descr30,
                     'WMSLOAD',
                     'N',
                     0
              FROM   tms_temp_gl_blugbl
             WHERE   row_seq = 1 AND is_exclude = 'N';

         --Update the freight detail data by tms_temp_gl_blugbl which already be charged.
         UPDATE   tms_freight_detail t
            SET
                  (update_cd,
                  currency_cd,
                  charge_per_pound,
                  charged_cost,
                  charged_freight,
                  charged_weight,
                  cur_exchng_rt,
                  gl_seq_nbr,
                  business_unit_gl
                  ) =
                     (SELECT   'PROCESSED',
                               b.currency_cd b_currency_cd,
                               b.charge_per_pound b_charge_per_pound,
                               NVL (
                                  b.fas_blue_globl_amt
                                  * (DECODE (b.piece_weight,
                                             0, 1,
                                             b.piece_weight)
                                     / b.caculate_weight),
                                  0
                               )
                                  b_charged_cost,
                               --charged_freight
                               DECODE (
                                  is_exclude,
                                  'N',
                                  NVL (
                                     b.fas_blue_globl_amt
                                     * (DECODE (b.piece_weight,
                                                0, 1,
                                                b.piece_weight)
                                        / b.caculate_weight),
                                     0
                                  ),
                                  0
                               ),
                               t.weight b_weight,
                               b.cur_exchng_rt rate_mult,
                               b.seq_nbr,
                               business_unit_gl
                        FROM   tms_temp_gl_blugbl b
                       WHERE   b.detail_row = t.original_rowid)
          WHERE       t.update_cd = 'READY'
                  AND t.carrier_cd = 'BGND'
                  AND EXISTS (SELECT   1
                                FROM   tms_temp_gl_blugbl b
                               WHERE   b.detail_row = t.original_rowid);

         --Mark those items in sr freight table which had been already charged.
         UPDATE   wms_intf_sr_freight sr
            SET   intf_status_cd = 'PROCESSED',
                  gl_sequence_number =
                     (SELECT   seq_nbr
                        FROM   tms_temp_gl_blugbl t
                       WHERE   sr.ROWID = t.detail_row)
          WHERE   EXISTS (SELECT   1
                            FROM   tms_temp_gl_blugbl t
                           WHERE   sr.ROWID = t.detail_row);

         --Mark all expired items to be charged.
         UPDATE   wms_intf_sr_freight
            SET   intf_status_cd = 'PROCESSED'
          WHERE       carrier_cd = 'BGND'
                  AND intf_status_cd = 'READY'
                  AND ship_dt < TRUNC (SYSDATE - .25) + .25;

         --We charge even the weight is 0, if we had data like this(without gl_seq_nbr) , it would not be considered.
         -- DELETE FROM   tms_freight_detail
         --     WHERE   weight = 0 AND gl_seq_nbr IS NULL;

         --Those items which are not processed will be in failure status.
         UPDATE   tms_freight_detail
            SET   update_cd = 'FAILED'
          WHERE   update_cd = 'READY' AND carrier_cd = 'BGND';

         COMMIT;
      END;
   END charge_bgnd_freight;

   PROCEDURE credit_back
   IS
      v_cancel   VARCHAR2 (9) := 'IN-CREDIT';
   BEGIN
      --Clear temp table
      DELETE FROM   tms.tms_temp_gl_blugbl;

      --Insert creditback value.
      --Because tms_freight_detail is a history table, and charge, cancel charge, credit back may happen
      --any time, so sum should be used to be sure that the total freight is zero
      --The charged_cost should always be zero when crediting back
      INSERT INTO tms_freight_detail (business_unit,
                                      ship_dt,
                                      shipping_unit_id,
                                      carrier_cd,
                                      weight,
                                      charged_weight,
                                      charged_cost,
                                      charged_freight,
                                      update_cd,
                                      original_rowid,
                                      stop_detail_id,
                                      charge_per_pound,
                                      currency_cd,
                                      hub,
                                      cur_exchng_rt,
                                      business_unit_gl,
                                      deptid,
                                      operate_type)
         WITH branch_list AS (SELECT   DISTINCT business_unit
                                FROM   tms.tms_charge_exclusion
                               WHERE   start_dt = TRUNC (SYSDATE, 'MONTH'))
         SELECT   *
           FROM   (  SELECT   tfd.business_unit,
                              tfd.ship_dt,
                              tfd.shipping_unit_id,
                              tfd.carrier_cd,
                              tfd.weight,
                              tfd.charged_weight,
                              0 charged_cost,
                              -1 * SUM (tfd.charged_freight) charged_freight,
                              'IN-CREDIT' update_cd,
                              tfd.original_rowid,
                              tfd.stop_detail_id,
                              tfd.charge_per_pound,
                              tfd.currency_cd,
                              tfd.hub,
                              tfd.cur_exchng_rt,
                              tfd.business_unit_gl,
                              tfd.deptid,
                              'Credit Back' operate_type
                       FROM   tms_freight_detail tfd, branch_list branch
                      WHERE   tfd.business_unit = branch.business_unit
                              AND TRUNC (tfd.ship_dt, 'MONTH') =
                                    TRUNC (SYSDATE, 'MONTH')
                   GROUP BY   tfd.business_unit,
                              tfd.ship_dt,
                              tfd.shipping_unit_id,
                              tfd.carrier_cd,
                              tfd.weight,
                              tfd.charged_weight,
                              tfd.original_rowid,
                              tfd.stop_detail_id,
                              tfd.charge_per_pound,
                              tfd.currency_cd,
                              tfd.hub,
                              tfd.cur_exchng_rt,
                              tfd.business_unit_gl,
                              tfd.deptid)
          WHERE   charged_freight < 0;

      --Insert credit value to temp table
      INSERT INTO tms.tms_temp_gl_blugbl (business_unit_gl,
                                          deptid,
                                          ship_date,
                                          seq_nbr,
                                          account,
                                          fas_blue_globl_amt,
                                          currency_cd,
                                          descr30,
                                          hub,
                                          carrier_cd,
                                          business_unit,
                                          charge_per_pound,
                                          cur_exchng_rt)
         SELECT   business_unit_gl,
                  deptid,
                  ship_dt,
                  tms_bg_gl_charge.NEXTVAL,
                  acct,
                  sum_money,
                  currency_cd,
                  description,
                  hub,
                  carrier_cd,
                  business_unit,
                  charge_per_pound,
                  cur_exchng_rt
           FROM   (  SELECT   business_unit_gl,
                              deptid,
                              ship_dt,
                              DECODE (wgt.carrier_cd,
                                      'FTRK',
                                      '6706',
                                      'BGEX',
                                      '6707',
                                      'BGND',
                                      '6708')
                                 acct,
                              ROUND (SUM (charged_freight), 2) sum_money,
                              currency_cd,
                              'Blue Global Credit' description,
                              hub,
                              carrier_cd,
                              business_unit,
                              charge_per_pound,
                              cur_exchng_rt
                       FROM   tms_freight_detail wgt
                      WHERE   update_cd = v_cancel
                   GROUP BY   business_unit_gl,
                              deptid,
                              ship_dt,
                              carrier_cd,
                              currency_cd,
                              hub,
                              carrier_cd,
                              business_unit,
                              charge_per_pound,
                              cur_exchng_rt);

      --Insert to PeopleSoft table
      INSERT INTO ps_fas_gl_blugbl (business_unit_gl,
                                    deptid,
                                    ship_date,
                                    seq_nbr,
                                    account,
                                    fas_blue_globl_amt,
                                    currency_cd,
                                    descr30,
                                    oprid_entered_by,
                                    gl_distrib_status,
                                    process_instance)
         SELECT   business_unit_gl,
                  deptid,
                  ship_date,
                  seq_nbr,
                  account,
                  fas_blue_globl_amt,
                  currency_cd,
                  descr30,
                  'WMSLOAD' oprid_entered_by,
                  'N' gl_distrib_status,
                  0
           FROM   tms_temp_gl_blugbl;

      --Update gl_seq_nbr to tms_freight_detail
      UPDATE   tms_freight_detail tfd
         SET   tfd.gl_seq_nbr =
                  (SELECT   dtl.seq_nbr
                     FROM   tms_temp_gl_blugbl dtl
                    WHERE       tfd.business_unit_gl = dtl.business_unit_gl
                            AND tfd.deptid = dtl.deptid
                            AND tfd.ship_dt = dtl.ship_date
                            AND tfd.carrier_cd = dtl.carrier_cd
                            AND tfd.currency_cd = dtl.currency_cd
                            AND tfd.hub = dtl.hub
                            AND tfd.carrier_cd = dtl.carrier_cd
                            AND tfd.business_unit = dtl.business_unit
                            AND tfd.charge_per_pound = dtl.charge_per_pound
                            AND tfd.cur_exchng_rt = dtl.cur_exchng_rt)
       WHERE   tfd.update_cd = v_cancel;

      --Remove those gl number which in tms_stop_detail.
      UPDATE   tms_stop_detail dtl
         SET   dtl.gl_seq_nbr = NULL
       WHERE   EXISTS
                  (SELECT   NULL
                     FROM   tms_freight_detail tfd
                    WHERE   dtl.stop_detail_id = tfd.stop_detail_id
                            AND tfd.update_cd = v_cancel);

      --Update tms_freight_detail status
      UPDATE   tms_freight_detail
         SET   update_cd = NVL2 (gl_seq_nbr, 'PROCESSED', 'FAILED')
       WHERE   update_cd = v_cancel;

      COMMIT;
   EXCEPTION
      WHEN OTHERS
      THEN
         ROLLBACK;
   END;

   --Function to found new region which appeard in tms_location , but not in tms_region_schedule.
   --We need to set the region schedule ,otherwise, it will use default schedule 0 and associated rate.
   PROCEDURE return_new_region
   IS
      v_count          NUMBER := 0;
      v_region_descr   VARCHAR2 (200);
      db_name_v        VARCHAR2 (10);
   BEGIN
      SELECT   DBMS_STANDARD.database_name INTO db_name_v FROM DUAL;

      FOR region_cur
      IN (  SELECT   region, COUNT ( * ) num
              FROM   (SELECT   DISTINCT loc.region
                        FROM   tms_location loc, tms_region_schedule sched
                       WHERE       loc.region = sched.region_id(+)
                               AND sched.region_id IS NULL
                               AND loc.business_unit IS NOT NULL
                               AND loc.location_type = 'BRANCH'
                               AND loc.region IS NOT NULL)
          GROUP BY   region)
      LOOP
         IF region_cur.num > 0
         THEN
            v_count := v_count + 1;

            IF v_count = 1
            THEN
               v_region_descr := region_cur.region;
            ELSIF v_count > 1
            THEN
               v_region_descr := v_region_descr || ' ' || region_cur.region;
            END IF;
         END IF;
      END LOOP;

      IF (v_region_descr IS NOT NULL)
      THEN
         SYSTEM.dba_maint.mailusers (
            'RGN',
            'TMS_Blue_Global Alert System - ' || db_name_v,
            'New region(s) exist, need to be added to table TMS_REGION_SCHEDULE on '
            || db_name_v,
               'System: '
            || db_name_v
            || CHR (10)
            || CHR (10)
            || 'New regions need to be created:'
            || CHR (10)
            || CHR (10)
            || v_region_descr
            || CHR (10)
            || CHR (10)
            || 'Action: Please add regions listed above into table tms.TMS_REGION_SCHEDULE with effective date.

Thanks.'
         );

         INSERT INTO tms_email_alert (alert_from)
           VALUES   ('return new region');
      END IF;

      COMMIT;
   END return_new_region;

   PROCEDURE move_to_intf_for_wms
   IS
   BEGIN
      --Insert into interface table by tms_temp_sd_update which had all the data been processed.
      INSERT INTO tms.tms_intf_outbound_item_status (stop_detail_id,
                                                     campus_id,
                                                     task_type,
                                                     shipping_unit_id,
                                                     scan_dt,
                                                     exception,
                                                     intf_status_cd)
         SELECT   tms_sd.stop_detail_id,
                  loc.location_id,
                  tms_sd.task_type,
                  tms_sd.shipping_unit_id,
                  tms_sd.scan_dt,
                  NVL2 (tms_sd.exception_code, 'T', 'F'),
                  --T means exception, F means no exception
                  'READY'
           FROM   tms_stop_detail tms_sd,
                  tms_route_stop tms_rs,
                  tms_location loc,
                  tms_temp_sd_update ttsd
          WHERE       tms_sd.stop_id = tms_rs.stop_id
                  AND tms_rs.location_id = loc.location_id
                  AND tms_sd.po_id IS NULL
                  AND ttsd.detail_row = tms_sd.ROWID;
   END move_to_intf_for_wms;

   PROCEDURE charge_freight
   IS
   BEGIN
      BEGIN
         tms.tms_intf_inbound.complete_branch_pickup_route;
      EXCEPTION
         WHEN OTHERS
         THEN
            NULL;
      END;

      --Run every day between 5:50 and 6:50. Those works did not need to run every hour.
      IF (TO_CHAR (SYSDATE, 'HH24MI') >= '0550'
          AND TO_CHAR (SYSDATE, 'HH24MI') < '0650')
      THEN
         --Only refresh view at 6:00AM one for one day
         EXECUTE IMMEDIATE 'truncate table tms_frgt_itm_mvw';

         dbms_mview.refresh ('tms_frgt_itm_mvw');

         EXECUTE IMMEDIATE 'truncate table tms_frgt_bu_mvw';

         dbms_mview.refresh ('tms_frgt_bu_mvw');

         return_new_region;

         charge_bgnd_freight;
      END IF;

      --Function to charge BGEX and FTRK.
      charge_ground_freight;
      --Export route data to wms which already processed .
      move_to_intf_for_wms;
      COMMIT;
   END charge_freight;
END tms_intf_outbound;
/

魔兽就是毒瘤,大家千万不要玩。
原文地址:https://www.cnblogs.com/tracy/p/1712531.html