create or replace procedure correct_deliveryhistory(fp_processbusinessunitid in deliveryhistory.processbusinessunit_id%type,
                                                    fp_deliverydate          in deliveryhistory.deliverydate%type,
                                                    fp_systemuserid          in systemuser.systemuser_id%type default 0) is

  gv_userexception exception;
  gv_message        abslog.message%type;
  gv_distribpointid distributionpoint.distributionpoint_id%type;
  gv_departmentid   department.department_id%type;
  gv_customerid     customer.customer_id%type;
  gv_currencyid     customer.currency_id%type;

  procedure write_log(lfp_message  in abslog.message%type,
                      lfp_priority in abslog.priority%type default 1) is
  begin
    /*
    Write message in ABSLOG with these priorities
    1 = Message
    2 = Warning
    3 = Error
    4 = Stack trace
    5 = Debug info
    */
    abslogwrite.abslog_write(5, 'correct_deliveryhistory', null, lfp_priority, lfp_message,
                             fp_systemuserid);
    dbms_output.put_line(lfp_message);
  end write_log;

  procedure get_cust_dept_for_distrid(lfp_distributionpointid in distributionpoint.distributionpoint_id%type) is
  begin
    --get_cust_dept_for_distrid
    select dp.department_id, t.customer_id, c.currency_id
    into   gv_departmentid, gv_customerid, gv_currencyid
    from   distributionpoint dp, department t, customer c
    where  dp.distributionpoint_id = lfp_distributionpointid
    and    dp.department_id = t.department_id
    and    t.customer_id = c.customer_id;
  end get_cust_dept_for_distrid;

  procedure get_client_info(lfp_clientid in client.client_id%type) is
  begin
    --get_client_info
    select cl.customer_id, nvl(dr.department_id, tcp.department_id),
           nvl(dr.distributionpoint_id, cp.distributionpoint_id), c.currency_id
    into   gv_customerid, gv_departmentid, gv_distribpointid, gv_currencyid
    from   client cl
    left   outer join clientresident cr
    on     (cr.client_id = cl.client_id)
    left   outer join departmentroom dr
    on     (cr.departmentroom_id = dr.departmentroom_id)
    left   outer join clientprivateperson cp
    on     (cp.client_id = cl.client_id)
    left   outer join distributionpoint dpcp
    on     (cp.distributionpoint_id = dpcp.distributionpoint_id)
    left   outer join department tcp
    on     (dpcp.department_id = tcp.department_id), customer c
    where  cl.client_id = lfp_clientid
    and    cl.customer_id = c.customer_id;
  end get_client_info;

  function get_new_deliveryhistoryid return deliveryhistory.deliveryhistory_id%type is
    lv_new_deliveryhistoryid deliveryhistory.deliveryhistory_id%type;
  begin
    --get_new_deliveryhistoryid
    select seq_deliveryhistory.nextval into lv_new_deliveryhistoryid from dual;
    return lv_new_deliveryhistoryid;
  end get_new_deliveryhistoryid;

  procedure check_parameters is
    lv_bucode businessunit.code%type;
  begin
    --initialize
    gv_message := 'check_parameters';
    --fp_processbusinessunit_id
    gv_message := 'check_parameters parameter fp_processbusinessunit_id: ' ||
                  nvl(to_char(fp_processbusinessunitid), 'NULL');
    select bu.code
    into   lv_bucode
    from   businessunit bu
    where  bu.businessunit_id = fp_processbusinessunitid;
    --fp_deliverydate
    if fp_deliverydate is null then
      begin
        gv_message := 'check_parameters parameter fp_deliverydate is null this is not allowed';
        raise gv_userexception;
      end;
    end if;
    --fp_systemuserid
    if fp_systemuserid is null then
      begin
        gv_message := 'check_parameters parameter fp_systemuserid  is null this is not allowed';
      end;
    end if;
  end check_parameters;

  procedure delete_old_records is
  begin
    --delete_old_records
    gv_message := 'delete_old_records';
    delete from deliveryhistory dh
    where  dh.deliverydate = fp_deliverydate
    and    dh.processbusinessunit_id = fp_processbusinessunitid;
  end delete_old_records;

  procedure insert_packingnote is
    lv_rec_deliveryhistory deliveryhistory%rowtype;
  begin
    --insert_packingnote
    gv_message := 'insert_packingnote';
    for x in (select pk.distributionpoint_id, pkl.product_id, pkl.sizedefinition_id,
                     pkl.finishingmethod_id, pk.processbusinessunit_id,
                     nvl(sum(pkl.standardamount), 0) standardamount,
                     nvl(sum(pkl.variationamount), 0) variationamount,
                     nvl(sum(pkl.deliveredamount), 0) deliveredamount,
                     nvl(sum(pkl.tobedeliveredamount), 0) tobedeliveredamount,
                     max(pk.invoicebusinessunit_id) invoicebusinessunit_id,
                     max(p.parentproduct_id) parentproduct_id,
                     case
                       when sum((select count(1)
                                from   packingnotelinecontent pklc
                                where  pklc.packingnoteline_id = pkl.packingnoteline_id)) > 0 then
                        3
                       else
                        1
                     end packindication
              from   packingnote pk, packingnoteline pkl, product p
              where  pk.processbusinessunit_id = fp_processbusinessunitid
              and    pk.deliverydate = fp_deliverydate
              and    pk.packingnote_id = pkl.packingnote_id
              and    pkl.product_id = p.product_id
              and    pk.status_id = (select st.status_id
                                     from   status st
                                     where  st.statustype = 'PN'
                                     and    st.code = '30')
              group  by pk.distributionpoint_id, pkl.product_id, pkl.sizedefinition_id,
                        pkl.finishingmethod_id, pk.processbusinessunit_id) loop
      begin
        get_cust_dept_for_distrid(x.distributionpoint_id);
        --fill data
        lv_rec_deliveryhistory.deliveryhistory_id        := get_new_deliveryhistoryid;
        lv_rec_deliveryhistory.processbusinessunit_id    := x.processbusinessunit_id;
        lv_rec_deliveryhistory.invoicebusinessunit_id    := x.invoicebusinessunit_id;
        lv_rec_deliveryhistory.deliverydate              := fp_deliverydate;
        lv_rec_deliveryhistory.week                      := getisoweek(fp_deliverydate);
        lv_rec_deliveryhistory.year                      := getyearofweek(fp_deliverydate);
        lv_rec_deliveryhistory.daynumber                 := getabsdaynumber(fp_deliverydate);
        lv_rec_deliveryhistory.customer_link_id          := gv_customerid;
        lv_rec_deliveryhistory.department_link_id        := gv_departmentid;
        lv_rec_deliveryhistory.distributionpoint_link_id := x.distributionpoint_id;
        lv_rec_deliveryhistory.product_link_id           := x.product_id;
        lv_rec_deliveryhistory.sizedefinition_link_id    := x.sizedefinition_id;
        lv_rec_deliveryhistory.finishingmethod_id        := x.finishingmethod_id;
        lv_rec_deliveryhistory.amountordered             := x.standardamount + x.variationamount;
        lv_rec_deliveryhistory.amountdelivered           := x.deliveredamount;
        lv_rec_deliveryhistory.amountstandard            := x.standardamount;
        lv_rec_deliveryhistory.amountshortage := case
                                                   when (x.tobedeliveredamount - x.deliveredamount < 0 or
                                                        x.tobedeliveredamount = 0) then
                                                    0
                                                   else
                                                    x.tobedeliveredamount - x.deliveredamount
                                                 end;
        lv_rec_deliveryhistory.currency_id               := gv_currencyid;
        lv_rec_deliveryhistory.price                     := 0;
        lv_rec_deliveryhistory.discount                  := 0;
        lv_rec_deliveryhistory.internalprice             := 0;
        lv_rec_deliveryhistory.pieceweight               := 0;
        lv_rec_deliveryhistory.packindication            := x.packindication;
        lv_rec_deliveryhistory.parentproduct_link_id     := x.parentproduct_id;
        lv_rec_deliveryhistory.parentpackproduct_link_id := x.product_id;
        lv_rec_deliveryhistory.systemuser_id             := fp_systemuserid;
        lv_rec_deliveryhistory.timestamp                 := sysdate;
        --insert
        insert into deliveryhistory values lv_rec_deliveryhistory;
      exception
        when dup_val_on_index then
          begin
            update deliveryhistory dh
            set    dh.amountdelivered = dh.amountdelivered + lv_rec_deliveryhistory.amountdelivered,
                   dh.amountshortage = dh.amountshortage + lv_rec_deliveryhistory.amountshortage,
                   dh.amountordered = dh.amountordered + lv_rec_deliveryhistory.amountordered,
                   dh.amountstandard = dh.amountstandard + lv_rec_deliveryhistory.amountstandard
            where  dh.deliverydate = lv_rec_deliveryhistory.deliverydate
            and    dh.customer_link_id = lv_rec_deliveryhistory.customer_link_id
            and    dh.department_link_id = lv_rec_deliveryhistory.department_link_id
            and    dh.distributionpoint_link_id = lv_rec_deliveryhistory.distributionpoint_link_id
            and    dh.product_link_id = lv_rec_deliveryhistory.product_link_id
            and    dh.sizedefinition_link_id = lv_rec_deliveryhistory.sizedefinition_link_id
            and    dh.finishingmethod_id = lv_rec_deliveryhistory.finishingmethod_id
            and    dh.processbusinessunit_id = lv_rec_deliveryhistory.processbusinessunit_id
            and    dh.parentpackproduct_link_id = lv_rec_deliveryhistory.parentpackproduct_link_id
            and    dh.packindication = lv_rec_deliveryhistory.packindication;
          end;
      end;
    end loop; --x packingnote
  end insert_packingnote;

  procedure insert_packingnotecontent is
    lv_rec_deliveryhistory deliveryhistory%rowtype;
  begin
    --insert_packingnotecontent
    gv_message := 'insert_packingnotecontent';
    --plc.quantity is the pack quantity so not the total!
    for y in (select pk.distributionpoint_id, plc.product_id, plc.sizedefinition_id,
                     plc.finishingmethod_id, pk.processbusinessunit_id,
                     nvl(sum((pkl.standardamount + pkl.variationamount) * plc.quantity), 0) amountordered,
                     nvl(sum(pkl.deliveredamount * plc.quantity), 0) amountdelivered,
                     nvl(sum(pkl.tobedeliveredamount * plc.quantity), 0) tobedeliveredamount,
                     nvl(sum(pkl.standardamount * plc.quantity), 0) amountstandard,
                     max(pk.invoicebusinessunit_id) invoicebusinessunit_id,
                     max(p.parentproduct_id) parentproduct_id, 2 packindication
              from   packingnote pk, packingnoteline pkl, packingnotelinecontent plc, product p
              where  pk.processbusinessunit_id = fp_processbusinessunitid
              and    pk.deliverydate = fp_deliverydate
              and    pk.packingnote_id = pkl.packingnote_id
              and    pkl.packingnoteline_id = plc.packingnoteline_id
              and    plc.product_id = p.product_id
              and    pk.status_id = (select st.status_id
                                     from   status st
                                     where  st.statustype = 'PN'
                                     and    st.code = '30')
              group  by pk.distributionpoint_id, plc.product_id, plc.sizedefinition_id,
                        plc.finishingmethod_id, pk.processbusinessunit_id) loop
      begin
        get_cust_dept_for_distrid(y.distributionpoint_id);
        --fill data
        lv_rec_deliveryhistory.deliveryhistory_id        := get_new_deliveryhistoryid;
        lv_rec_deliveryhistory.processbusinessunit_id    := y.processbusinessunit_id;
        lv_rec_deliveryhistory.invoicebusinessunit_id    := y.invoicebusinessunit_id;
        lv_rec_deliveryhistory.deliverydate              := fp_deliverydate;
        lv_rec_deliveryhistory.week                      := getisoweek(fp_deliverydate);
        lv_rec_deliveryhistory.year                      := getyearofweek(fp_deliverydate);
        lv_rec_deliveryhistory.daynumber                 := getabsdaynumber(fp_deliverydate);
        lv_rec_deliveryhistory.customer_link_id          := gv_customerid;
        lv_rec_deliveryhistory.department_link_id        := gv_departmentid;
        lv_rec_deliveryhistory.distributionpoint_link_id := y.distributionpoint_id;
        lv_rec_deliveryhistory.product_link_id           := y.product_id;
        lv_rec_deliveryhistory.sizedefinition_link_id    := y.sizedefinition_id;
        lv_rec_deliveryhistory.finishingmethod_id        := y.finishingmethod_id;
        lv_rec_deliveryhistory.amountordered             := y.amountordered;
        lv_rec_deliveryhistory.amountdelivered           := y.amountdelivered;
        lv_rec_deliveryhistory.amountstandard            := y.amountstandard;
        lv_rec_deliveryhistory.amountshortage := case
                                                   when (y.tobedeliveredamount - y.amountdelivered < 0 or
                                                        y.tobedeliveredamount = 0) then
                                                    0
                                                   else
                                                    y.tobedeliveredamount - y.amountdelivered
                                                 end;
        lv_rec_deliveryhistory.currency_id               := gv_currencyid;
        lv_rec_deliveryhistory.price                     := 0;
        lv_rec_deliveryhistory.discount                  := 0;
        lv_rec_deliveryhistory.internalprice             := 0;
        lv_rec_deliveryhistory.pieceweight               := 0;
        lv_rec_deliveryhistory.packindication            := 2;
        lv_rec_deliveryhistory.parentproduct_link_id     := y.parentproduct_id;
        lv_rec_deliveryhistory.parentpackproduct_link_id := y.product_id;
        lv_rec_deliveryhistory.systemuser_id             := fp_systemuserid;
        lv_rec_deliveryhistory.timestamp                 := sysdate;
        --insert
        insert into deliveryhistory values lv_rec_deliveryhistory;
      exception
        when dup_val_on_index then
          begin
            update deliveryhistory dh
            set    dh.amountdelivered = dh.amountdelivered + lv_rec_deliveryhistory.amountdelivered,
                   dh.amountshortage = dh.amountshortage + lv_rec_deliveryhistory.amountshortage,
                   dh.amountordered = dh.amountordered + lv_rec_deliveryhistory.amountordered,
                   dh.amountstandard = dh.amountstandard + lv_rec_deliveryhistory.amountstandard
            where  dh.deliverydate = lv_rec_deliveryhistory.deliverydate
            and    dh.customer_link_id = lv_rec_deliveryhistory.customer_link_id
            and    dh.department_link_id = lv_rec_deliveryhistory.department_link_id
            and    dh.distributionpoint_link_id = lv_rec_deliveryhistory.distributionpoint_link_id
            and    dh.product_link_id = lv_rec_deliveryhistory.product_link_id
            and    dh.sizedefinition_link_id = lv_rec_deliveryhistory.sizedefinition_link_id
            and    dh.finishingmethod_id = lv_rec_deliveryhistory.finishingmethod_id
            and    dh.processbusinessunit_id = lv_rec_deliveryhistory.processbusinessunit_id
            and    dh.parentpackproduct_link_id = lv_rec_deliveryhistory.parentpackproduct_link_id
            and    dh.packindication = lv_rec_deliveryhistory.packindication;
          end;
      end;
    end loop; --y packingnote content
  end insert_packingnotecontent;

  procedure insert_clientnote is
    lv_rec_deliveryhistory deliveryhistory%rowtype;
  begin
    --insert_clientnote
    gv_message := 'insert_clientnote';
    for z in (select cld.client_link_id, cld.processbusinessunit_id,
                     max(cldl.invoicebusinessunit_id) invoicebusinessunit_id,
                     cldl.product_link_id product_id, cldl.sizedefinition_link_id sizedefinition_id,
                     cldl.finishingmethod_id, max(p.parentproduct_id) parentproduct_id,
                     nvl(sum(cldl.qtyscannedout + cldl.qtycountedout), 0) amountdelivered
              from   clientdeliverynote cld, clientdeliverynoteline cldl, batch bc, product p
              where  cld.clientdeliverynote_id = cldl.clientdeliverinote_id
              and    cld.batch_id = bc.batch_id
              and    bc.status = 4 -- closed
              and    cld.deliverydate = fp_deliverydate
              and    cld.processbusinessunit_id = fp_processbusinessunitid
              and    cldl.product_link_id = p.product_id
              group  by cld.client_link_id, cldl.product_link_id, cldl.sizedefinition_link_id,
                        cldl.finishingmethod_id, cld.processbusinessunit_id) loop
      begin
        get_client_info(z.client_link_id);
        --fill data
        lv_rec_deliveryhistory.deliveryhistory_id        := get_new_deliveryhistoryid;
        lv_rec_deliveryhistory.processbusinessunit_id    := z.processbusinessunit_id;
        lv_rec_deliveryhistory.invoicebusinessunit_id    := z.invoicebusinessunit_id;
        lv_rec_deliveryhistory.deliverydate              := fp_deliverydate;
        lv_rec_deliveryhistory.week                      := getisoweek(fp_deliverydate);
        lv_rec_deliveryhistory.year                      := getyearofweek(fp_deliverydate);
        lv_rec_deliveryhistory.daynumber                 := getabsdaynumber(fp_deliverydate);
        lv_rec_deliveryhistory.customer_link_id          := gv_customerid;
        lv_rec_deliveryhistory.department_link_id        := gv_departmentid;
        lv_rec_deliveryhistory.distributionpoint_link_id := gv_distribpointid;
        lv_rec_deliveryhistory.product_link_id           := z.product_id;
        lv_rec_deliveryhistory.sizedefinition_link_id    := z.sizedefinition_id;
        lv_rec_deliveryhistory.finishingmethod_id        := z.finishingmethod_id;
        lv_rec_deliveryhistory.amountordered             := 0;
        lv_rec_deliveryhistory.amountdelivered           := z.amountdelivered;
        lv_rec_deliveryhistory.amountstandard            := 0;
        lv_rec_deliveryhistory.amountshortage            := 0;
        lv_rec_deliveryhistory.currency_id               := gv_currencyid;
        lv_rec_deliveryhistory.price                     := 0;
        lv_rec_deliveryhistory.discount                  := 0;
        lv_rec_deliveryhistory.internalprice             := 0;
        lv_rec_deliveryhistory.pieceweight               := 0;
        lv_rec_deliveryhistory.packindication            := 1;
        lv_rec_deliveryhistory.parentproduct_link_id     := z.parentproduct_id;
        lv_rec_deliveryhistory.parentpackproduct_link_id := z.product_id;
        lv_rec_deliveryhistory.systemuser_id             := fp_systemuserid;
        lv_rec_deliveryhistory.timestamp                 := sysdate;
        --insert
        insert into deliveryhistory values lv_rec_deliveryhistory;
      exception
        when dup_val_on_index then
          begin
            update deliveryhistory dh
            set    dh.amountdelivered = dh.amountdelivered + lv_rec_deliveryhistory.amountdelivered,
                   dh.amountshortage = dh.amountshortage + lv_rec_deliveryhistory.amountshortage,
                   dh.amountordered = dh.amountordered + lv_rec_deliveryhistory.amountordered,
                   dh.amountstandard = dh.amountstandard + lv_rec_deliveryhistory.amountstandard
            where  dh.deliverydate = lv_rec_deliveryhistory.deliverydate
            and    dh.customer_link_id = lv_rec_deliveryhistory.customer_link_id
            and    dh.department_link_id = lv_rec_deliveryhistory.department_link_id
            and    dh.distributionpoint_link_id = lv_rec_deliveryhistory.distributionpoint_link_id
            and    dh.product_link_id = lv_rec_deliveryhistory.product_link_id
            and    dh.sizedefinition_link_id = lv_rec_deliveryhistory.sizedefinition_link_id
            and    dh.finishingmethod_id = lv_rec_deliveryhistory.finishingmethod_id
            and    dh.processbusinessunit_id = lv_rec_deliveryhistory.processbusinessunit_id
            and    dh.parentpackproduct_link_id = lv_rec_deliveryhistory.parentpackproduct_link_id
            and    dh.packindication = lv_rec_deliveryhistory.packindication;
          end;
      end;
    end loop; --z clientdelivery not
  end insert_clientnote;


  procedure insert_allocationtransaction is
    lv_rec_deliveryhistory deliveryhistory%rowtype;
  begin
    --insert_allocationtransaction
    gv_message := 'insert_allocationtransaction';
    for t in (select c.customer_id,
                     d.department_id,
                     c.currency_id,
                     atr.product_link_id,
                     atr.sizedefinition_link_id,
                     atr.finishingmethod_id,
                     atr.todistributionpoint_link_id,
                     atr.fromdistributionpoint_link_id,
                     max(p.parentproduct_id) parentproduct_id,
                     sum(case
                           when atr.transactiontype = 1 then atr.stocked
                           when atr.transactiontype = 2 then atr.emergency
                           when atr.transactiontype = 3 then (-1) * atr.returned
                         end) delAmount
                from allocationtransaction atr,
                     product               p,
                     distributionpoint     dp,
                     department            d,
                     customer              c
               where atr.todistributionpoint_link_id = dp.distributionpoint_id
                 and dp.department_id = d.department_id
                 and d.customer_id = c.customer_id
                 and atr.product_link_id = p.product_id
                 and c.defaultbusinessunit_id = fp_processbusinessunitid
                 and atr.allocationdate = fp_deliverydate
               group by c.customer_id,
                        d.department_id,
                        c.currency_id,
                        atr.todistributionpoint_link_id,
                        atr.product_link_id,
                        atr.sizedefinition_link_id,
                        atr.finishingmethod_id,
                        atr.todistributionpoint_link_id,
                        atr.fromdistributionpoint_link_id) loop
      --fill data
      lv_rec_deliveryhistory.processbusinessunit_id    := fp_processbusinessunitid;
      lv_rec_deliveryhistory.invoicebusinessunit_id    := fp_processbusinessunitid;
      lv_rec_deliveryhistory.deliverydate              := fp_deliverydate;
      lv_rec_deliveryhistory.week                      := getisoweek(fp_deliverydate);
      lv_rec_deliveryhistory.year                      := getyearofweek(fp_deliverydate);
      lv_rec_deliveryhistory.daynumber                 := getabsdaynumber(fp_deliverydate);
      lv_rec_deliveryhistory.customer_link_id          := t.customer_id;
      lv_rec_deliveryhistory.department_link_id        := t.department_id;
      lv_rec_deliveryhistory.product_link_id           := t.product_link_id;
      lv_rec_deliveryhistory.sizedefinition_link_id    := t.sizedefinition_link_id;
      lv_rec_deliveryhistory.finishingmethod_id        := t.finishingmethod_id;
      lv_rec_deliveryhistory.amountordered             := 0;
      lv_rec_deliveryhistory.amountstandard            := 0;
      lv_rec_deliveryhistory.amountshortage            := 0;
      lv_rec_deliveryhistory.currency_id               := t.currency_id;
      lv_rec_deliveryhistory.price                     := 0;
      lv_rec_deliveryhistory.discount                  := 0;
      lv_rec_deliveryhistory.internalprice             := 0;
      lv_rec_deliveryhistory.pieceweight               := 0;
      lv_rec_deliveryhistory.packindication            := 1;
      lv_rec_deliveryhistory.parentproduct_link_id     := t.parentproduct_id;
      lv_rec_deliveryhistory.parentpackproduct_link_id := t.product_link_id;
      lv_rec_deliveryhistory.systemuser_id             := fp_systemuserid;
      lv_rec_deliveryhistory.timestamp                 := sysdate;
      
      for i in 1..2 loop
        if (i = 1) then -- insert positive DH for toDistributionPoint
           lv_rec_deliveryhistory.deliveryhistory_id        := get_new_deliveryhistoryid;
           lv_rec_deliveryhistory.distributionpoint_link_id := t.todistributionpoint_link_id;
           lv_rec_deliveryhistory.amountdelivered           := t.delamount;         
        else -- insert negative DH for fromDistributionPoint
           lv_rec_deliveryhistory.deliveryhistory_id        := get_new_deliveryhistoryid;
           lv_rec_deliveryhistory.distributionpoint_link_id := t.fromdistributionpoint_link_id;
           lv_rec_deliveryhistory.amountdelivered           := (-1) * t.delamount;        
        end if;
        begin
          --insert
          insert into deliveryhistory values lv_rec_deliveryhistory;
        exception
          when dup_val_on_index then
            begin
              update deliveryhistory dh
              set    dh.amountdelivered = dh.amountdelivered + lv_rec_deliveryhistory.amountdelivered,
                     dh.amountshortage = dh.amountshortage + lv_rec_deliveryhistory.amountshortage,
                     dh.amountordered = dh.amountordered + lv_rec_deliveryhistory.amountordered,
                     dh.amountstandard = dh.amountstandard + lv_rec_deliveryhistory.amountstandard
              where  dh.deliverydate = lv_rec_deliveryhistory.deliverydate
              and    dh.customer_link_id = lv_rec_deliveryhistory.customer_link_id
              and    dh.department_link_id = lv_rec_deliveryhistory.department_link_id
              and    dh.distributionpoint_link_id = lv_rec_deliveryhistory.distributionpoint_link_id
              and    dh.product_link_id = lv_rec_deliveryhistory.product_link_id
              and    dh.sizedefinition_link_id = lv_rec_deliveryhistory.sizedefinition_link_id
              and    dh.finishingmethod_id = lv_rec_deliveryhistory.finishingmethod_id
              and    dh.processbusinessunit_id = lv_rec_deliveryhistory.processbusinessunit_id
              and    dh.parentpackproduct_link_id = lv_rec_deliveryhistory.parentpackproduct_link_id
              and    dh.packindication = lv_rec_deliveryhistory.packindication;
            exception
              when others then
                raise;
            end;
        end;
      end loop; -- to/from distributionpoint
    end loop; -- t allocationtransaction
  end insert_allocationtransaction;
  
begin
  check_parameters;
  delete_old_records;
  insert_packingnote;
  insert_packingnotecontent;
  insert_clientnote;
  insert_allocationtransaction;
  commit;
  write_log('ran succesfully');
exception
  when gv_userexception then
    begin
      write_log(gv_message, 3);
    end;
  when others then
    begin
      rollback;
      gv_message := gv_message || ' ' || sqlerrm;
      write_log(gv_message, 3);
    end;
end correct_deliveryhistory;
/
