src-db/database/model/functions/M_INOUT_CREATE.xml
author Asier Lostalé <asier.lostale@openbravo.com>
Mon, 26 May 2008 11:24:39 +0000
changeset 1027 2e9052d7de0e
parent 799 fef2c5e2feb7
child 1178 9d1c8b3fd305
permissions -rw-r--r--
fixed bug 0000399: Remove insertions in AD_PInstance_Log table
carlos@0
     1
<?xml version="1.0"?>
adrian@94
     2
  <database name="FUNCTION M_INOUT_CREATE">
adrian@94
     3
    <function name="M_INOUT_CREATE" type="NULL">
antonio@735
     4
      <parameter name="p_pinstance_id" type="NUMERIC" mode="in">
antonio@735
     5
        <default/>
antonio@735
     6
      </parameter>
antonio@735
     7
      <parameter name="p_inout_id" type="NUMERIC" mode="out">
antonio@735
     8
        <default/>
antonio@735
     9
      </parameter>
antonio@735
    10
      <parameter name="p_order_id" type="NUMERIC" mode="in">
antonio@735
    11
        <default><![CDATA[NULL]]></default>
antonio@735
    12
      </parameter>
antonio@735
    13
      <parameter name="p_invoice_id" type="NUMERIC" mode="in">
antonio@735
    14
        <default><![CDATA[NULL]]></default>
antonio@735
    15
      </parameter>
antonio@735
    16
      <parameter name="p_forcedelivery" type="CHAR" mode="in">
antonio@735
    17
        <default><![CDATA[N]]></default>
antonio@735
    18
      </parameter>
antonio@735
    19
      <parameter name="p_locator_id" type="NUMERIC" mode="in">
antonio@735
    20
        <default><![CDATA[NULL]]></default>
antonio@735
    21
      </parameter>
gorkaion@239
    22
      <body><![CDATA[/*************************************************************************
juanpablo@771
    23
  * The contents of this file are subject to the Compiere Public
juanpablo@771
    24
  * License 1.1 ("License"); You may not use this file except in
juanpablo@771
    25
  * compliance with the License. You may obtain a copy of the License in
juanpablo@771
    26
  * the legal folder of your Openbravo installation.
carlos@0
    27
  * Software distributed under the License is distributed on an
carlos@0
    28
  * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
carlos@0
    29
  * implied. See the License for the specific language governing rights
carlos@0
    30
  * and limitations under the License.
juanpablo@778
    31
  * The Original Code is  Compiere  ERP &  Business Solution
juanpablo@771
    32
  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
carlos@0
    33
  * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke,
carlos@0
    34
  * parts created by ComPiere are Copyright (C) ComPiere, Inc.;
carlos@0
    35
  * All Rights Reserved.
carlos@0
    36
  * Contributor(s): Openbravo SL
juanpablo@785
    37
  * Contributions are Copyright (C) 2001-2008 Openbravo, S.L.
juanpablo@771
    38
  *
juanpablo@771
    39
  * Specifically, this derivative work is based upon the following Compiere
juanpablo@771
    40
  * file and version.
carlos@0
    41
  *************************************************************************
carlos@0
    42
  * $Id: M_InOut_Create.sql,v 1.17 2003/09/05 04:58:06 jjanke Exp $
carlos@0
    43
  ***
carlos@0
    44
  * Title: Create Shipment from Order
carlos@0
    45
  * Description:
carlos@0
    46
  * Order Loop goes though all open orders, where we would need to ship something
carlos@0
    47
  *  if forced or if there is a line to ship
carlos@0
    48
  *   create InOut document header
carlos@0
    49
  *   for all qualifying order lines
carlos@0
    50
  *    check every locator availability and if qty available
carlos@0
    51
  *     create InOut line
carlos@0
    52
  *
carlos@0
    53
  * Order and reservation is updated when posting
carlos@0
    54
  * as there should not be a delay between creating + posting it
carlos@0
    55
  *
carlos@0
    56
  * For each Warehouse create lines (with exception if Direct Ship's),
carlos@0
    57
  * create also lines for non-stocked, ad_hoc products or comments
carlos@0
    58
  ************************************************************************/
carlos@0
    59
  -- Logistice
carlos@0
    60
  v_ResultStr VARCHAR2(2000):='';
carlos@0
    61
  v_Message VARCHAR2(4000):='';
carlos@0
    62
  -- Parameter
carlos@0
    63
  TYPE RECORD IS REF CURSOR;
carlos@0
    64
    Cur_Parameter RECORD;
carlos@0
    65
    -- Record Info
carlos@0
    66
    v_AD_Org_ID NUMBER;
asier@799
    67
    v_AD_Client_ID NUMBER;
carlos@0
    68
    v_Record_ID NUMBER:=NULL;
carlos@0
    69
    v_Selection VARCHAR2(1):='N';
asier@799
    70
    v_DBA_ErrorLog_ID NUMBER;
carlos@0
    71
    -- Orders to process  - one per warehouse
carlos@0
    72
    Cur_Order RECORD;
carlos@0
    73
    -- Order Lines per Warehouse
carlos@0
    74
    CURSOR Cur_OrderLine (Order_ID NUMBER, Warehouse_ID NUMBER, IsForced CHAR) IS
carlos@0
    75
    SELECT *
carlos@0
    76
    FROM C_ORDERLINE l
carlos@0
    77
    WHERE C_Order_ID=Order_ID -- Parameter
carlos@0
    78
      AND
carlos@0
    79
      M_Warehouse_ID=Warehouse_ID -- Parameter
carlos@0
    80
      -- Incomplete lines and comments
gorkaion@239
    81
      AND(QtyOrdered<>QtyDelivered OR(QtyOrdered=0 AND
carlos@0
    82
      M_Product_ID IS NULL)) -- Don't include Direct Ship's
carlos@0
    83
      AND
carlos@0
    84
      DirectShip='N' AND -- We ship it anyway
carlos@0
    85
      (IsForced = 'Y' -- Parameter
carlos@0
    86
      OR -- we have it on stock
carlos@0
    87
      (EXISTS  (SELECT 1
carlos@0
    88
    FROM M_STORAGE_DETAIL s,
carlos@0
    89
      M_LOCATOR loc,
carlos@0
    90
      C_ORDER o
carlos@0
    91
    WHERE s.M_Product_ID=l.M_Product_ID AND
carlos@0
    92
      s.M_Locator_ID=loc.M_Locator_ID AND
carlos@0
    93
      loc.M_Warehouse_ID=l.M_Warehouse_ID AND(s.M_AttributeSetInstance_ID=COALESCE(l.M_AttributeSetInstance_ID, 0) OR
carlos@0
    94
      l.M_AttributeSetInstance_ID IS NULL) AND
carlos@0
    95
      l.C_Order_ID=o.C_Order_ID -- Delivery Rule: (L)ine, (O)rder
carlos@0
    96
    GROUP BY o.DeliveryRule
carlos@0
    97
    HAVING((o.DeliveryRule IN('L', 'O') AND
gorkaion@239
    98
      SUM(s.QtyOnHand)>=l.QtyOrdered-l.QtyDelivered) -- Delivery Rule: (A)vailability
carlos@0
    99
      OR
gorkaion@239
   100
      SUM(s.QtyOnHand)>0) ) --  Delivery Rule (R)eceipt ** NOT HANDELED **
carlos@0
   101
      -- Service
carlos@0
   102
      OR
carlos@0
   103
      EXISTS
carlos@0
   104
      (SELECT *
carlos@0
   105
      FROM M_PRODUCT p
carlos@0
   106
      WHERE l.M_Product_ID=p.M_Product_ID AND(p.IsStocked='N' OR
gorkaion@239
   107
        p.ProductType<>'I')
carlos@0
   108
      )
carlos@0
   109
      -- Comment + AdHoc
carlos@0
   110
      OR
carlos@0
   111
      l.M_Product_ID IS NULL)) --
carlos@0
   112
    ORDER BY M_Product_ID;
carlos@0
   113
    ol Cur_OrderLine%ROWTYPE;
carlos@0
   114
    CURSOR Cur_Storage (p_Product NUMBER, p_Warehouse NUMBER, p_UOM NUMBER, p_Product_UOM NUMBER, p_Attribute NUMBER) IS
carlos@0
   115
    SELECT T.C_UOM_ID,
carlos@0
   116
      T.M_LOCATOR_ID,
carlos@0
   117
      T.M_ATTRIBUTESETINSTANCE_ID,
carlos@0
   118
      T.M_PRODUCT_UOM_ID,
carlos@0
   119
      COALESCE(T.QtyOnHand, 0) AS QtyOnHand,
carlos@0
   120
      T.QtyOrderOnHand AS QtyOrder
carlos@0
   121
    FROM M_STORAGE_DETAIL T,
carlos@0
   122
      M_LOCATOR L
carlos@0
   123
    WHERE T.M_PRODUCT_ID=p_Product AND
carlos@0
   124
      T.M_LOCATOR_ID=L.M_LOCATOR_ID AND
carlos@0
   125
      L.M_WAREHOUSE_ID=p_Warehouse AND
carlos@0
   126
      T.C_UOM_ID=p_UOM AND
carlos@0
   127
      COALESCE(T.M_PRODUCT_UOM_ID, 0)=COALESCE(p_Product_UOM, 0) AND
carlos@0
   128
      COALESCE(T.M_ATTRIBUTESETINSTANCE_ID, 0)=COALESCE(p_Attribute, COALESCE(T.M_ATTRIBUTESETINSTANCE_ID, 0)) AND
gorkaion@239
   129
      COALESCE(T.QtyOnHand, 0)<>0
carlos@0
   130
    ORDER BY L.PRIORITYNO,
carlos@0
   131
      T.M_ATTRIBUTESETINSTANCE_ID;
carlos@0
   132
    v_storage Cur_Storage%ROWTYPE;
carlos@0
   133
    --
carlos@0
   134
    v_NextNo NUMBER;
carlos@0
   135
    v_DocType_ID NUMBER;
carlos@0
   136
    v_DocumentNo VARCHAR2(40) ;
carlos@0
   137
    v_Qty NUMBER;
carlos@0
   138
    --MODIFIED BY F.IRIAZABAL
carlos@0
   139
    v_QtyOrder NUMBER;
carlos@0
   140
    v_ProductUOM NUMBER;
carlos@0
   141
    v_ActualQty NUMBER;
carlos@0
   142
    v_QtyAux NUMBER;
carlos@0
   143
    --
carlos@0
   144
    v_lines NUMBER:=0;
carlos@0
   145
    v_count NUMBER:=0;
carlos@0
   146
    CREATE_FROM_INVOICE BOOLEAN:=false;
carlos@0
   147
    Next_Line BOOLEAN:=false;
carlos@0
   148
    Next_Order BOOLEAN:=false;
carlos@0
   149
    NEXT_PRODUCT BOOLEAN:=false;
carlos@0
   150
    FINISH_PROCESS BOOLEAN:=false;
carlos@0
   151
    Cur_Storage_ISOPEN BOOLEAN:=false;
carlos@0
   152
    Cur_OrderLine_ISOPEN BOOLEAN:=false;
carlos@0
   153
  BEGIN
carlos@0
   154
    -- Process Parameters
carlos@0
   155
    IF(p_PInstance_ID IS NOT NULL) THEN
asier@1027
   156
      --  Chec  k for serial execution
carlos@0
   157
      DBMS_OUTPUT.PUT_LINE('Updating PInstance - Processing ' || p_PInstance_ID) ;
carlos@0
   158
      SELECT COUNT(*)
carlos@0
   159
      INTO v_count
carlos@0
   160
      FROM AD_PINSTANCE
carlos@0
   161
      WHERE AD_PROCESS_ID IN(SELECT AD_PROCESS_ID FROM AD_PINSTANCE WHERE AD_PInstance_ID=p_PInstance_ID)
carlos@0
   162
        AND IsProcessing='Y'
gorkaion@239
   163
        AND AD_PInstance_ID<>p_PInstance_ID;
gorkaion@239
   164
      IF(v_count>0) THEN
carlos@0
   165
        RAISE_APPLICATION_ERROR(-20000, '@SerialProcessStillRunning@') ;
carlos@0
   166
      END IF;
carlos@0
   167
      --  Update AD_PInstance
carlos@0
   168
      DBMS_OUTPUT.PUT_LINE('M_InOut_Create - Processing ' || p_PInstance_ID) ;
carlos@0
   169
      v_ResultStr:='PInstanceNotFound';
carlos@0
   170
      AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'Y', NULL, NULL) ;
carlos@0
   171
      -- Get Parameters
carlos@0
   172
      v_ResultStr:='ReadingParameters';
carlos@0
   173
      FOR Cur_Parameter IN
asier@799
   174
        (SELECT i.Record_ID, p.ParameterName, p.P_String, p.P_Number, p.P_Date, p.AD_CLIENT_ID
carlos@0
   175
        FROM AD_PInstance i LEFT JOIN AD_PInstance_Para p ON i.AD_PInstance_ID=p.AD_PInstance_ID
carlos@0
   176
        WHERE i.AD_PInstance_ID=p_PInstance_ID
carlos@0
   177
        ORDER BY p.SeqNo) LOOP
carlos@0
   178
        IF(Cur_Parameter.ParameterName='AD_Org_ID') THEN
carlos@0
   179
          v_AD_Org_ID:=Cur_Parameter.P_Number;
carlos@0
   180
          DBMS_OUTPUT.PUT_LINE('  AD_Org_ID=' || v_AD_Org_ID) ;
carlos@0
   181
        ELSIF(Cur_Parameter.ParameterName='Selection') THEN
carlos@0
   182
          v_Selection:=Cur_Parameter.P_String;
carlos@0
   183
          DBMS_OUTPUT.PUT_LINE('  Selection=' || v_Selection) ;
carlos@0
   184
        ELSE
carlos@0
   185
          DBMS_OUTPUT.PUT_LINE('*** Unknown Parameter=' || Cur_Parameter.ParameterName) ;
carlos@0
   186
        END IF;
asier@799
   187
        v_AD_Client_ID:=Cur_Parameter.AD_CLIENT_ID;
carlos@0
   188
        v_Record_ID:=Cur_Parameter.Record_ID;
carlos@0
   189
      END LOOP; -- Get Parameter
carlos@0
   190
      DBMS_OUTPUT.PUT_LINE('  v_Record_ID=' || v_Record_ID) ;
carlos@0
   191
    ELSIF(p_Invoice_ID IS NOT NULL) THEN
carlos@0
   192
      CREATE_FROM_INVOICE:=true;
carlos@0
   193
    ELSE
carlos@0
   194
      v_Record_ID:=p_Order_ID;
gorkaion@239
   195
      DBMS_OUTPUT.PUT_LINE('--<<M_InOut_Create>> Order_ID=' || v_Record_ID);
carlos@0
   196
    END IF;
carlos@0
   197
    BEGIN --BODY
carlos@0
   198
      IF(NOT CREATE_FROM_INVOICE) THEN
carlos@0
   199
        -- May be NULL or 0
carlos@0
   200
        IF(v_Record_ID IS NULL) THEN
carlos@0
   201
          v_Record_ID:=0;
carlos@0
   202
        END IF;
carlos@0
   203
        IF(v_Selection IS NULL) THEN
carlos@0
   204
          v_Selection:='N';
carlos@0
   205
        END IF;
carlos@0
   206
      END IF;--CREATE_FROM_INVOICE
carlos@0
   207
      IF(NOT CREATE_FROM_INVOICE) THEN
carlos@0
   208
        /**************************************************************************
carlos@0
   209
        * Order Loop goes though all open orders, where we would need to ship something
carlos@0
   210
        * (if v_Record_ID = 0)
carlos@0
   211
        *************************************************************************/
carlos@0
   212
        FOR Cur_Order IN
carlos@0
   213
          (SELECT o.AD_Client_ID, o.AD_Org_ID, o.C_Order_ID, o.IsSOTrx,
carlos@0
   214
             o.Description, o.DocumentNo, o.C_DocType_ID, o.C_BPartner_ID,
carlos@0
   215
             o.C_BPartner_Location_ID, o.AD_User_ID, l.M_Warehouse_ID, o.POReference,
carlos@0
   216
             o.DateOrdered, o.DeliveryRule, o.FreightCostRule, o.FreightAmt,
carlos@0
   217
             o.C_Project_ID, o.C_Activity_ID, o.C_Campaign_ID, o.AD_OrgTrx_ID,
carlos@0
   218
             o.User1_ID, o.User2_ID, o.DeliveryViaRule, o.M_Shipper_ID,
antonio@737
   219
             o.C_Charge_ID, o.ChargeAmt, o.PriorityRule, o.SALESREP_ID,
rafael@385
   220
             o.DELIVERY_LOCATION_ID -- Added by Rafa Roda to include delivery location
carlos@0
   221
           FROM C_ORDER o INNER JOIN C_ORDERLINE l ON(o.C_Order_ID=l.C_Order_ID) --  Orders are IP  or CO if Standard Orders
carlos@0
   222
           WHERE(o.DocStatus='IP' OR(o.DocStatus='CO'
carlos@0
   223
                                     AND o.IsDelivered='N'
carlos@0
   224
                                     AND v_Record_ID=0)) -- Sales Orders Only and not Offers
carlos@0
   225
                 AND o.IsSOTrx='Y'
carlos@0
   226
                 AND o.C_DocType_ID IN (SELECT C_DocType_ID
carlos@0
   227
                                        FROM C_DOCTYPE
carlos@0
   228
                                        WHERE DocBaseType='SOO'
carlos@0
   229
                                        AND DocSubTypeSO NOT IN('ON', 'OB')
carlos@0
   230
                 )
carlos@0
   231
            -- Manually Selected
carlos@0
   232
            AND((v_Selection='Y'
carlos@0
   233
                AND o.IsSelected='Y')
gorkaion@239
   234
            OR(v_Selection<>'Y' -- Specific or individual organization
carlos@0
   235
               AND(v_AD_Org_ID IS NULL OR
carlos@0
   236
                   v_AD_Org_ID=o.AD_Org_ID) -- Specific order or all open orders
carlos@0
   237
               AND(o.C_Order_ID=v_Record_ID -- Parameter
carlos@0
   238
                   OR(v_Record_ID=0
carlos@0
   239
                      AND EXISTS (SELECT *
carlos@0
   240
                                  FROM C_ORDERLINE ll
carlos@0
   241
                                  WHERE o.C_Order_ID=ll.C_Order_ID
gorkaion@239
   242
                                  AND ll.QtyOrdered<>ll.QtyDelivered
carlos@0
   243
                                  AND ll.DirectShip='N'
carlos@0
   244
                                  AND ll.M_Product_ID IS NOT NULL
carlos@0
   245
              )))))
carlos@0
   246
           GROUP BY o.AD_Client_ID, o.AD_Org_ID, o.C_Order_ID, o.IsSOTrx,
carlos@0
   247
             o.Description, o.DocumentNo, o.C_DocType_ID, o.C_BPartner_ID,
carlos@0
   248
             o.C_BPartner_Location_ID, o.AD_User_ID, l.M_Warehouse_ID, o.POReference,
carlos@0
   249
             o.DateOrdered, o.DeliveryRule, o.FreightCostRule, o.FreightAmt,
carlos@0
   250
             o.C_Project_ID, o.C_Activity_ID, o.C_Campaign_ID, o.AD_OrgTrx_ID,
carlos@0
   251
             o.User1_ID, o.User2_ID, o.DeliveryViaRule, o.M_Shipper_ID,
rafael@385
   252
             o.C_Charge_ID, o.ChargeAmt, o.PriorityRule, o.SALESREP_ID,
rafael@385
   253
             o.DELIVERY_LOCATION_ID -- Added by Rafa Roda to include delivery location
carlos@0
   254
           ORDER BY o.PriorityRule
carlos@0
   255
          ) LOOP
carlos@0
   256
          DBMS_OUTPUT.PUT_LINE('Order ' || Cur_Order.DocumentNo || '/' || Cur_Order.C_Order_ID || ', Wh=' || Cur_Order.M_Warehouse_ID || ', Force=' || p_ForceDelivery || ', Delivery=' || Cur_Order.DeliveryRule) ;
carlos@0
   257
          -- Delivery Rules
carlos@0
   258
          --  (A)vailability
carlos@0
   259
          --  Complete (L)ine
carlos@0
   260
          --  Complete (O)rder
carlos@0
   261
          --  After (R)eceipt
carlos@0
   262
          -- (A)vailability (L)ine   -- Do we have something to ship ?
carlos@0
   263
          IF(Cur_Order.DeliveryRule IN('A', 'L', 'R', 'O')) THEN
carlos@0
   264
            v_ResultStr:='CheckSomethingToShip';
carlos@0
   265
            OPEN Cur_OrderLine(Cur_Order.C_Order_ID, Cur_Order.M_Warehouse_ID, p_ForceDelivery) ;
carlos@0
   266
            Cur_OrderLine_ISOPEN:=true;
carlos@0
   267
            v_ResultStr:='Fetching';
carlos@0
   268
            FETCH Cur_OrderLine INTO ol;
carlos@0
   269
            IF(Cur_OrderLine%NOTFOUND) THEN
carlos@0
   270
              DBMS_OUTPUT.PUT_LINE('  -no lines-') ;
carlos@0
   271
              Next_Order:=true;
carlos@0
   272
            END IF;
carlos@0
   273
          ELSE
carlos@0
   274
            DBMS_OUTPUT.PUT_LINE('** DeliveryRule=' || Cur_Order.DeliveryRule || ' not implemented') ;
carlos@0
   275
            Next_Order:=true;
carlos@0
   276
          END IF;
carlos@0
   277
          IF(NOT Next_Order) THEN
carlos@0
   278
            /**
carlos@0
   279
            * Create Order Header
carlos@0
   280
            *  if forced or if there is a line to ship
carlos@0
   281
            */
carlos@0
   282
            v_ResultStr:='CreateInOut-S';
carlos@0
   283
            SAVEPOINT sp_new_shipment;
carlos@0
   284
            v_lines:=10;
carlos@0
   285
            --
carlos@0
   286
            Ad_Sequence_Next('M_InOut', Cur_Order.C_Order_ID, p_InOut_ID) ;
carlos@0
   287
            v_ResultStr:='CreateInOut DocType=' || Cur_Order.C_DocType_ID;
carlos@0
   288
            -- Get Shipment Doc Number
carlos@0
   289
            SELECT C_DocTypeShipment_ID INTO v_DocType_ID
carlos@0
   290
            FROM C_DOCTYPE
carlos@0
   291
            WHERE C_DocType_ID=Cur_Order.C_DocType_ID;
carlos@0
   292
            Ad_Sequence_Doctype(v_DocType_ID, Cur_Order.C_Order_ID, 'Y', v_DocumentNo) ;
carlos@0
   293
            IF(v_DocumentNo IS NULL) THEN
carlos@0
   294
              Ad_Sequence_Doc('DocumentNo_M_InOut', Cur_Order.AD_Client_ID, 'Y', v_DocumentNo) ;
carlos@0
   295
            END IF;
antonio@735
   296
            --RAISE_APPLICATION_ERROR(-20100,'el numero documento es:'||COALESCE(v_DocumentNo,0)||' El doctypeId es: '||COALESCE(v_DocType_ID,0)||' El cOrderId es: '||COALESCE(Cur_Order.C_Order_ID,0));
carlos@0
   297
            --
carlos@0
   298
            DBMS_OUTPUT.PUT_LINE('  InOut_ID=' || p_InOut_ID || ', DocumentNo=' || v_DocumentNo || ', Ship_DocType_ID=' || v_DocType_ID || ', Order_DocType_ID=' || Cur_Order.C_DocType_ID) ;
carlos@0
   299
            v_ResultStr:='InsertInOut ' || p_InOut_ID;
carlos@0
   300
            INSERT INTO M_INOUT
carlos@0
   301
              (M_InOut_ID, C_Order_ID, IsSOTrx, AD_Client_ID,
carlos@0
   302
              AD_Org_ID, IsActive, Created, CreatedBy,
carlos@0
   303
              Updated, UpdatedBy, DocumentNo, C_DocType_ID,
carlos@0
   304
              Description, IsPrinted, MovementType, MovementDate,
carlos@0
   305
              DateAcct, C_BPartner_ID, C_BPartner_Location_ID, AD_User_ID,
carlos@0
   306
              M_Warehouse_ID, POReference, DateOrdered, DeliveryRule,
carlos@0
   307
              FreightCostRule, FreightAmt, C_Project_ID, C_Activity_ID,
carlos@0
   308
              C_Campaign_ID, AD_OrgTrx_ID, User1_ID, User2_ID,
carlos@0
   309
              DeliveryViaRule, M_Shipper_ID, C_Charge_ID, ChargeAmt,
carlos@0
   310
              PriorityRule, DocStatus, DocAction, Processing,
antonio@737
   311
              Processed, SALESREP_ID,
rafael@385
   312
              DELIVERY_LOCATION_ID) -- Added by Rafa Roda to include delivery location
carlos@0
   313
            VALUES
carlos@0
   314
              (p_InOut_ID, Cur_Order.C_Order_ID, Cur_Order.IsSOTrx, Cur_Order.AD_Client_ID,
carlos@0
   315
              Cur_Order.AD_Org_ID, 'Y', now(), 0,
carlos@0
   316
              now(), 0, v_DocumentNo, v_DocType_ID,
carlos@0
   317
              Cur_Order.Description, 'N', 'C-', now(),
carlos@0
   318
              now(), Cur_Order.C_BPartner_ID, Cur_Order.C_BPartner_Location_ID, Cur_Order.AD_User_ID,
carlos@0
   319
              Cur_Order.M_Warehouse_ID, Cur_Order.POReference, Cur_Order.DateOrdered, Cur_Order.DeliveryRule,
carlos@0
   320
              Cur_Order.FreightCostRule, Cur_Order.FreightAmt, Cur_Order.C_Project_ID, Cur_Order.C_Activity_ID,
carlos@0
   321
              Cur_Order.C_Campaign_ID, Cur_Order.AD_OrgTrx_ID, Cur_Order.User1_ID, Cur_Order.User2_ID,
carlos@0
   322
              Cur_Order.DeliveryViaRule, Cur_Order.M_Shipper_ID, Cur_Order.C_Charge_ID, Cur_Order.ChargeAmt,
carlos@0
   323
              Cur_Order.PriorityRule, 'DR', 'CO', 'N',
antonio@737
   324
              'N', Cur_Order.SALESREP_ID,
rafael@385
   325
              Cur_Order.DELIVERY_LOCATION_ID); -- Added by Rafa Roda to include delivery location
carlos@0
   326
          END IF;--Next_Order
carlos@0
   327
          IF(NOT Next_Order) THEN
carlos@0
   328
            /**
carlos@0
   329
            * Create InOut Lines
carlos@0
   330
            * for all qualifying order lines
carlos@0
   331
            */
carlos@0
   332
            DECLARE
carlos@0
   333
              v_Product_old NUMBER;
carlos@0
   334
              v_Warehouse_old NUMBER;
carlos@0
   335
              v_UOM_old NUMBER;
carlos@0
   336
              v_Attribute_old NUMBER;
carlos@0
   337
              v_Product_UOM_old NUMBER;
carlos@0
   338
              v_QtyStorage NUMBER:=0;
carlos@0
   339
            BEGIN
carlos@0
   340
              LOOP
carlos@0
   341
                -- Check Availability
carlos@0
   342
                v_Qty:=ol.QtyOrdered - ol.QtyDelivered;
carlos@0
   343
                --MODIFIED BY F.IRIAZABAL
carlos@0
   344
                v_QtyOrder:=0;
carlos@0
   345
                IF(ol.M_Product_UOM_ID IS NOT NULL) THEN
carlos@0
   346
                  SELECT M_PRODUCT_UOM.C_UOM_ID INTO v_ProductUOM
carlos@0
   347
                  FROM M_PRODUCT_UOM, C_UOM
carlos@0
   348
                  WHERE M_PRODUCT_UOM.C_UOM_ID=C_UOM.C_UOM_ID
carlos@0
   349
                    AND M_Product_UOM_ID=ol.M_Product_UOM_ID;
carlos@0
   350
                  IF(ol.QtyDelivered=0) THEN
carlos@0
   351
                    v_QtyOrder:=ol.QuantityOrder;
carlos@0
   352
                  ELSIF(v_ProductUOM IS NOT NULL) THEN
carlos@0
   353
                    v_QtyOrder:=ol.QuantityOrder*v_Qty/ol.QtyOrdered;
carlos@0
   354
                    --v_QtyOrder:=C_Uom_Convert(v_Qty, ol.C_UOM_ID, v_ProductUOM, 'Y') ;
carlos@0
   355
                  ELSE
carlos@0
   356
                    v_QtyOrder:=NULL;
carlos@0
   357
                  END IF;
carlos@0
   358
                ELSE
carlos@0
   359
                  v_QtyOrder:=NULL;
carlos@0
   360
                END IF;
carlos@0
   361
                -- Skip zero product lines (comments are zero qty)
carlos@0
   362
                IF(v_Qty=0 AND ol.M_Product_ID IS NOT NULL) THEN
carlos@0
   363
                  Next_Line:=true;
carlos@0
   364
                END IF;
carlos@0
   365
                IF(NOT Next_Line) THEN
carlos@0
   366
                  --
carlos@0
   367
                  DECLARE
carlos@0
   368
                    v_LocatorQty NUMBER;
carlos@0
   369
                    v_Locator_ID NUMBER(10) ;
carlos@0
   370
                    v_IsStocked NUMBER:=0;
carlos@0
   371
                  BEGIN
carlos@0
   372
                    -- Is it a standard stocked product?
carlos@0
   373
                    IF(ol.M_Product_ID IS NOT NULL) THEN
carlos@0
   374
                      SELECT COUNT(*) INTO v_IsStocked
carlos@0
   375
                      FROM M_PRODUCT
carlos@0
   376
                      WHERE M_Product_ID=ol.M_Product_ID
carlos@0
   377
                        AND IsStocked='Y'
carlos@0
   378
                        AND ProductType='I';
carlos@0
   379
                    END IF;
carlos@0
   380
                    -- Item is stocked - check availability
gorkaion@239
   381
                    IF(v_IsStocked<>0) THEN
carlos@0
   382
                      -- check every locator availability and if qty available
carlos@0
   383
                      --  create InOut line, update storage reservation + qty
carlos@0
   384
                      IF(NOT Cur_Storage_ISOPEN)
gorkaion@239
   385
                        OR(v_Product_old<>ol.M_Product_ID
gorkaion@239
   386
                           OR v_Warehouse_old<>ol.M_Warehouse_ID
gorkaion@239
   387
                           OR v_UOM_old<>ol.C_UOM_ID
gorkaion@239
   388
                           OR COALESCE(v_Attribute_old, 0)<>COALESCE(ol.M_AttributeSetInstance_ID, 0)
gorkaion@239
   389
                           OR COALESCE(v_Product_UOM_old, 0)<>COALESCE(ol.M_Product_UOM_ID, 0)) THEN
carlos@0
   390
                        v_Product_old:=ol.M_Product_ID;
carlos@0
   391
                        v_Warehouse_old:=ol.M_Warehouse_ID;
carlos@0
   392
                        v_UOM_old:=ol.C_UOM_ID;
carlos@0
   393
                        v_Attribute_old:=ol.M_AttributeSetInstance_ID;
carlos@0
   394
                        v_Product_UOM_old:=ol.M_Product_UOM_ID;
carlos@0
   395
                        IF(Cur_Storage_ISOPEN) THEN
carlos@0
   396
                          CLOSE Cur_Storage;
carlos@0
   397
                          Cur_Storage_ISOPEN:=false;
carlos@0
   398
                        END IF;
carlos@0
   399
                        OPEN Cur_Storage(v_Product_old, v_Warehouse_old, v_UOM_old, v_Product_UOM_old, v_Attribute_old) ;
carlos@0
   400
                        Cur_Storage_ISOPEN:=true;
carlos@0
   401
                        FETCH Cur_Storage INTO v_storage;
carlos@0
   402
                        IF Cur_Storage%NOTFOUND THEN
carlos@0
   403
                          NEXT_PRODUCT:=true;
carlos@0
   404
                        END IF;
carlos@0
   405
                        IF(NOT NEXT_PRODUCT) THEN
carlos@0
   406
                          v_QtyStorage:=v_storage.QtyOnHand;
carlos@0
   407
                        END IF;--NEXT_PRODUCT
carlos@0
   408
                      END IF;
carlos@0
   409
                      IF(NOT NEXT_PRODUCT) THEN
carlos@0
   410
                        LOOP
carlos@0
   411
                          -- How much do we deliver from here?
carlos@0
   412
                          v_ResultStr:='Deliver';
gorkaion@239
   413
                          IF(v_Qty>v_QtyStorage) THEN
carlos@0
   414
                            v_LocatorQty:=v_QtyStorage;
carlos@0
   415
                          ELSE
carlos@0
   416
                            v_LocatorQty:=v_Qty;
carlos@0
   417
                            v_QtyStorage:=v_QtyStorage-v_Qty;
carlos@0
   418
                          END IF;
carlos@0
   419
                          v_ActualQty:=NULL;
carlos@0
   420
                          IF v_storage.M_Product_UOM_ID IS NOT NULL THEN
carlos@0
   421
                            v_ActualQty:=v_storage.QtyOrder*(v_LocatorQty/v_storage.QtyOnHand) ;
carlos@0
   422
                          END IF;
gorkaion@239
   423
                          IF(v_LocatorQty<>0) THEN
carlos@0
   424
                            -- Create InOut Line
carlos@0
   425
                            Ad_Sequence_Next('M_InOutLine', Cur_Order.C_Order_ID, v_NextNo) ;
carlos@0
   426
                            --
carlos@0
   427
                            DBMS_OUTPUT.PUT_LINE('    Line ' || ol.Line || '  Qty=' || v_LocatorQty) ;
carlos@0
   428
                            v_ResultStr:='CreateInOutLine';
carlos@0
   429
                            INSERT INTO M_INOUTLINE
carlos@0
   430
                              (M_InOutLine_ID, Line, M_InOut_ID, C_OrderLine_ID,
carlos@0
   431
                              AD_Client_ID, AD_Org_ID, IsActive, Created,
carlos@0
   432
                              CreatedBy, Updated, UpdatedBy, M_Product_ID,
carlos@0
   433
                              C_UOM_ID, M_Locator_ID, MovementQty, Description,
asier@799
   434
                              IsInvoiced,
carlos@0
   435
                              --MODIFIED BY F.IRIAZABAL
carlos@0
   436
                              QuantityOrder, M_Product_Uom_ID, M_AttributeSetInstance_ID)
carlos@0
   437
                            VALUES
carlos@0
   438
                              (v_NextNo, v_lines, p_InOut_ID, ol.C_OrderLine_ID,
carlos@0
   439
                              ol.AD_Client_ID, ol.AD_Org_ID, 'Y', now(),
carlos@0
   440
                              0, now(), 0, ol.M_Product_ID,
carlos@0
   441
                              v_storage.C_UOM_ID, v_storage.M_Locator_ID, v_LocatorQty, ol.Description,
asier@799
   442
                              'N',
carlos@0
   443
                              --MODIFIED BY F.IRIAZABAL
carlos@0
   444
                              v_ActualQty, v_storage.M_PRODUCT_UOM_ID, v_storage.M_AttributeSetInstance_ID);
carlos@0
   445
                            v_lines:=v_lines + 10;
carlos@0
   446
                            DBMS_OUTPUT.PUT_LINE('v_lines' || v_lines) ;
carlos@0
   447
                          END IF;
carlos@0
   448
                          v_Qty:=v_Qty - v_LocatorQty;
carlos@0
   449
                          --MODIFIED BY F.IRIAZABAL
carlos@0
   450
                          v_QtyOrder:=v_QtyOrder - v_ActualQty;
carlos@0
   451
                          EXIT WHEN v_Qty=0;
carlos@0
   452
                          v_QtyStorage:=0;
carlos@0
   453
                          v_ResultStr:='FetchingData';
carlos@0
   454
                          FETCH Cur_Storage INTO v_storage;
carlos@0
   455
                          EXIT WHEN Cur_Storage%NOTFOUND;
carlos@0
   456
                          v_QtyStorage:=v_storage.QtyOnHand;
carlos@0
   457
                        END LOOP; -- Storage
carlos@0
   458
                      END IF;--NEXT_PRODUCT
gorkaion@239
   459
                      --<<NEXT_PRODUCT>>
carlos@0
   460
                      NEXT_PRODUCT:=false;
gorkaion@239
   461
                      IF(v_Qty<>0 AND p_ForceDelivery='Y') THEN
carlos@0
   462
                        -- Create InOut Line
carlos@0
   463
                        Ad_Sequence_Next('M_InOutLine', Cur_Order.C_Order_ID, v_NextNo) ;
carlos@0
   464
                        IF(v_ProductUOM IS NOT NULL) THEN
carlos@0
   465
                          v_ActualQty:=ol.QuantityOrder*v_Qty/ol.QtyOrdered;
carlos@0
   466
                          --v_ActualQty:=C_Uom_Convert(v_Qty, ol.C_UOM_ID, v_ProductUOM, 'Y') ;
carlos@0
   467
                        ELSE
carlos@0
   468
                          v_ActualQty:=NULL;
carlos@0
   469
                        END IF;
carlos@0
   470
                        SELECT MIN(M_LOCATOR_ID) INTO v_Locator_ID
carlos@0
   471
                        FROM M_LOCATOR
carlos@0
   472
                        WHERE M_WAREHOUSE_ID=ol.M_Warehouse_ID;
carlos@0
   473
                        --
carlos@0
   474
                        DBMS_OUTPUT.PUT_LINE('    Line ' || ol.Line || '  Qty=' || v_LocatorQty) ;
carlos@0
   475
                        v_ResultStr:='CreateInOutLine';
carlos@0
   476
                        INSERT INTO M_INOUTLINE
carlos@0
   477
                          (M_InOutLine_ID, Line, M_InOut_ID, C_OrderLine_ID,
carlos@0
   478
                          AD_Client_ID, AD_Org_ID, IsActive, Created,
carlos@0
   479
                          CreatedBy, Updated, UpdatedBy, M_Product_ID,
carlos@0
   480
                          C_UOM_ID, M_Locator_ID, MovementQty, Description,
asier@799
   481
                          IsInvoiced,
carlos@0
   482
                          --MODIFIED BY F.IRIAZABAL   ,
carlos@0
   483
                          QuantityOrder, M_Product_Uom_ID, M_AttributeSetInstance_ID)
carlos@0
   484
                        VALUES
carlos@0
   485
                          (v_NextNo, v_lines, p_InOut_ID, ol.C_OrderLine_ID,
carlos@0
   486
                          ol.AD_Client_ID, ol.AD_Org_ID, 'Y', now(),
carlos@0
   487
                          0, now(), 0, ol.M_Product_ID,
carlos@0
   488
                          ol.C_UOM_ID, v_Locator_ID, v_Qty, ol.Description,
asier@799
   489
                          'N',
carlos@0
   490
                          --MODIFIED BY F.IRIAZABAL
carlos@0
   491
                          v_ActualQty, ol.M_PRODUCT_UOM_ID, ol.M_AttributeSetInstance_ID);
carlos@0
   492
                      END IF;
carlos@0
   493
                      -- Copy Ad-hoc lines, Comments OR Service Items
carlos@0
   494
                    ELSE
carlos@0
   495
                      -- Create InOut Line
carlos@0
   496
                      Ad_Sequence_Next('M_InOutLine', Cur_Order.C_Order_ID, v_NextNo) ;
carlos@0
   497
                      --
carlos@0
   498
                      DBMS_OUTPUT.PUT_LINE('    Line* ' || ol.Line || '  Qty=' || v_Qty) ;
carlos@0
   499
                      v_ResultStr:='CreateInOutLine*2 '||ol.c_order_id||'-'||ol.line||'-'||ol.m_product_id;
carlos@0
   500
                      INSERT INTO M_INOUTLINE
carlos@0
   501
                        (M_InOutLine_ID, Line, M_InOut_ID, C_OrderLine_ID,
carlos@0
   502
                        AD_Client_ID, AD_Org_ID, IsActive, Created,
carlos@0
   503
                        CreatedBy, Updated, UpdatedBy, M_Product_ID,
carlos@0
   504
                        C_UOM_ID, M_Locator_ID, MovementQty, Description,
asier@799
   505
                        IsInvoiced,
carlos@0
   506
                        --MODIFIED BY F.IRIAZABAL
carlos@0
   507
                        QuantityOrder, M_Product_Uom_ID)
carlos@0
   508
                      VALUES
carlos@0
   509
                        (v_NextNo, v_lines, p_InOut_ID, ol.C_OrderLine_ID,
carlos@0
   510
                        ol.AD_Client_ID, ol.AD_Org_ID, 'Y', now(),
carlos@0
   511
                        0, now(), 0, ol.M_Product_ID,
carlos@0
   512
                        ol.C_UOM_ID, NULL, v_Qty, ol.Description,
asier@799
   513
                        'N',
carlos@0
   514
                        --MODIFIED BY F.IRIAZABAL
carlos@0
   515
                        v_QtyOrder, ol.M_PRODUCT_UOM_ID);
carlos@0
   516
                      v_lines:=v_lines + 10;
carlos@0
   517
                    END IF;
carlos@0
   518
                  END;
carlos@0
   519
                END IF;--Next_Line
gorkaion@239
   520
                --<<Next_Line>>
carlos@0
   521
                Next_Line:=false;
carlos@0
   522
                FETCH Cur_OrderLine INTO ol;
carlos@0
   523
                EXIT WHEN Cur_OrderLine%NOTFOUND;
carlos@0
   524
              END LOOP; -- Order Line Loop
carlos@0
   525
            END;--END DECLARE VARIABLES FOR ORDER LINE LOOP
carlos@0
   526
          END IF;--Next_Order
carlos@0
   527
          IF(NOT Next_Order) THEN
carlos@0
   528
            IF(v_lines=0) THEN
carlos@0
   529
              DBMS_OUTPUT.PUT_LINE('v_lines!!!!!!!!!!!!!!!!!' || v_lines) ;
carlos@0
   530
              ROLLBACK TO SAVEPOINT sp_new_shipment;
carlos@0
   531
            ELSE
gorkaion@239
   532
              v_Message:=v_Message || '@OrderDocumentno@ ' || Cur_Order.DocumentNo || ' -> @InoutDocumentno@ ' || v_DocumentNo || '; ';
asier@1027
   533
              
carlos@0
   534
              -- Post Shipment
antonio@735
   535
              M_INOUT_POST(NULL, p_InOut_ID) ;
carlos@0
   536
            END IF;
carlos@0
   537
          END IF;--Next_Order
gorkaion@239
   538
          --<<Next_Order>>
carlos@0
   539
          Next_Order:=false;
carlos@0
   540
          IF(Cur_Storage_ISOPEN) THEN
carlos@0
   541
            CLOSE Cur_Storage;
carlos@0
   542
            Cur_Storage_ISOPEN:=false;
carlos@0
   543
          END IF;
carlos@0
   544
          IF(Cur_OrderLine_ISOPEN) THEN
carlos@0
   545
            CLOSE Cur_OrderLine;
carlos@0
   546
            Cur_OrderLine_ISOPEN:=false;
carlos@0
   547
          END IF;
carlos@0
   548
        END LOOP; -- Order Header Loop
carlos@0
   549
        FINISH_PROCESS:=true;
carlos@0
   550
      END IF;--CREATE_FROM_INVOICE
carlos@0
   551
      IF(NOT FINISH_PROCESS) THEN
carlos@0
   552
        /**************************************************************************
carlos@0
   553
        * Create Shipment from Invoice
carlos@0
   554
        *************************************************************************/
gorkaion@239
   555
        --<<CREATE_FROM_INVOICE>>
carlos@0
   556
        CREATE_FROM_INVOICE:=false;
carlos@0
   557
        DECLARE
carlos@0
   558
          Cur_Invoice RECORD;
carlos@0
   559
          Cur_Lines RECORD;
carlos@0
   560
          v_Warehouse_ID NUMBER;
carlos@0
   561
          v_IsDocNoControlled CHAR(1) ;
carlos@0
   562
        BEGIN
carlos@0
   563
          FOR Cur_Invoice IN
carlos@0
   564
            (SELECT * FROM C_INVOICE WHERE C_Invoice_ID=p_Invoice_ID) LOOP
carlos@0
   565
            v_ResultStr:='CreateInOut';
carlos@0
   566
            -- Get Warehouse from Locator
carlos@0
   567
            SELECT M_Warehouse_ID INTO v_Warehouse_ID
carlos@0
   568
            FROM M_LOCATOR
carlos@0
   569
            WHERE M_Locator_ID=p_Locator_ID;
carlos@0
   570
            -- Get Shipment Doc Number
carlos@0
   571
            v_DocType_ID:=Ad_Get_DocType(Cur_Invoice.AD_Client_ID, Cur_Invoice.AD_Org_ID, 'MMR') ;
carlos@0
   572
            SELECT IsDocNoControlled INTO v_IsDocNoControlled
carlos@0
   573
            FROM C_DOCTYPE
carlos@0
   574
            WHERE C_DocType_ID=v_DocType_ID;
carlos@0
   575
            IF(v_IsDocNoControlled='Y') THEN
carlos@0
   576
              Ad_Sequence_Doctype(v_DocType_ID, Cur_Invoice.C_Invoice_ID, 'Y', v_DocumentNo) ;
carlos@0
   577
              IF(v_DocumentNo IS NULL) THEN
carlos@0
   578
                Ad_Sequence_Doc('DocumentNo_M_InOut', Cur_Invoice.AD_Client_ID, 'Y', v_DocumentNo) ;
carlos@0
   579
              END IF;
carlos@0
   580
            ELSE
carlos@0
   581
              v_DocumentNo:=Cur_Invoice.DocumentNo;
carlos@0
   582
            END IF;
carlos@0
   583
            Ad_Sequence_Next('M_InOut', Cur_Invoice.C_Invoice_ID, p_InOut_ID) ;
carlos@0
   584
            --
carlos@0
   585
            DBMS_OUTPUT.PUT_LINE('  InOut_ID=' || p_InOut_ID || ' DocumentNo=' || v_DocumentNo) ;
carlos@0
   586
            v_ResultStr:='InsertInOut ' || p_InOut_ID;
carlos@0
   587
            INSERT INTO M_INOUT
carlos@0
   588
              (M_InOut_ID, C_Order_ID, IsSOTrx, C_Invoice_ID,
carlos@0
   589
              AD_Client_ID, AD_Org_ID, IsActive, Created,
carlos@0
   590
              CreatedBy, Updated, UpdatedBy, DocumentNo,
carlos@0
   591
              C_DocType_ID, Description, IsPrinted, MovementType,
carlos@0
   592
              MovementDate, DateAcct, C_BPartner_ID, C_BPartner_Location_ID,
carlos@0
   593
              AD_User_ID, M_Warehouse_ID, POReference, DateOrdered,
carlos@0
   594
              DeliveryRule,
carlos@0
   595
              FreightCostRule,
carlos@0
   596
              FreightAmt,
carlos@0
   597
              DeliveryViaRule, M_Shipper_ID, C_Charge_ID, ChargeAmt,
carlos@0
   598
              PriorityRule,
carlos@0
   599
              DocStatus, DocAction, Processing, Processed
carlos@0
   600
              )
carlos@0
   601
            VALUES
carlos@0
   602
              (p_InOut_ID, NULL, Cur_Invoice.IsSoTrx, Cur_Invoice.C_Invoice_ID,
carlos@0
   603
              Cur_Invoice.AD_Client_ID, Cur_Invoice.AD_Org_ID, 'Y', now(),
carlos@0
   604
              0, now(), 0, v_DocumentNo,
carlos@0
   605
              v_DocType_ID, Cur_Invoice.Description, 'N', 'V+',
carlos@0
   606
              Cur_Invoice.DateInvoiced, Cur_Invoice.DateInvoiced, Cur_Invoice.C_BPartner_ID, Cur_Invoice.C_BPartner_Location_ID,
carlos@0
   607
              Cur_Invoice.AD_User_ID, v_Warehouse_ID, Cur_Invoice.POReference, Cur_Invoice.DateInvoiced,
carlos@0
   608
              'A', -- Available
carlos@0
   609
              'I',
carlos@0
   610
              0, -- Freight included
carlos@0
   611
              'P', NULL, Cur_Invoice.C_Charge_ID, Cur_Invoice.ChargeAmt,
carlos@0
   612
              '3', -- PickUp, High Priority
carlos@0
   613
              'DR', 'CO', 'N', 'N'
carlos@0
   614
              );
carlos@0
   615
            --
asier@1027
   616
           
carlos@0
   617
            -- Lines
carlos@0
   618
            FOR Cur_Lines IN
carlos@0
   619
              (SELECT *
carlos@0
   620
              FROM C_INVOICELINE
carlos@0
   621
              WHERE C_Invoice_ID=p_Invoice_ID
carlos@0
   622
              AND M_Product_ID IS NOT NULL) LOOP
carlos@0
   623
              Ad_Sequence_Next('M_InOutLine', Cur_Invoice.C_Invoice_ID, v_NextNo) ;
carlos@0
   624
              DBMS_OUTPUT.PUT_LINE('    Line* ' || Cur_Lines.Line) ;
carlos@0
   625
              v_ResultStr:='CreateInOutLine*3';
carlos@0
   626
              INSERT INTO M_INOUTLINE
carlos@0
   627
                (M_InOutLine_ID, Line, M_InOut_ID, C_OrderLine_ID,
carlos@0
   628
                AD_Client_ID, AD_Org_ID, IsActive, Created,
carlos@0
   629
                CreatedBy, Updated, UpdatedBy, M_Product_ID,
carlos@0
   630
                C_UOM_ID, M_Locator_ID, MovementQty, Description,
asier@799
   631
                IsInvoiced,
carlos@0
   632
                --MODIFIED BY F.IRIAZABAL
carlos@0
   633
                QuantityOrder, M_Product_Uom_ID
carlos@0
   634
                )
carlos@0
   635
              VALUES
carlos@0
   636
                (v_NextNo, Cur_Lines.Line, p_InOut_ID, Cur_Lines.C_OrderLine_ID,
carlos@0
   637
                Cur_Lines.AD_Client_ID, Cur_Lines.AD_Org_ID, 'Y', now(),
carlos@0
   638
                0, now(), 0, Cur_Lines.M_Product_ID,
carlos@0
   639
                Cur_Lines.C_UOM_ID, p_Locator_ID, Cur_Lines.QtyInvoiced, Cur_Lines.Description,
asier@799
   640
                'Y',
carlos@0
   641
                --MODIFIED BY F.IRIAZABAL
carlos@0
   642
                Cur_Lines.QuantityOrder, Cur_Lines.M_Product_Uom_ID
carlos@0
   643
                );
carlos@0
   644
              UPDATE C_INVOICELINE
carlos@0
   645
              SET M_INOUTLINE_ID=v_NextNo
carlos@0
   646
              WHERE C_INVOICELINE_ID=Cur_Lines.C_INVOICELINE_ID;
carlos@0
   647
            END LOOP; -- InvoiceLines
carlos@0
   648
          END LOOP; -- Invoices
carlos@0
   649
          -- Post Shipment
antonio@735
   650
          M_INOUT_POST(NULL, p_InOut_ID) ;
carlos@0
   651
		  --ADDED BY P.SAROBE
carlos@0
   652
		  SELECT documentno
carlos@0
   653
		  INTO v_DocumentNo
carlos@0
   654
		  FROM 	 M_INOUT
carlos@0
   655
		  WHERE m_inout_id=p_InOut_ID;
carlos@0
   656
		  v_Message:='@InoutDocumentno@ ' || v_DocumentNo || ' @beenCreated@';
carlos@0
   657
        END;
carlos@0
   658
      END IF;--FINISH_PROCESS
carlos@0
   659
      /*************************************************************************/
gorkaion@239
   660
      --<<FINISH_PROCESS>>
carlos@0
   661
      IF(p_PInstance_ID IS NOT NULL) THEN
carlos@0
   662
        --  Update AD_PInstance
carlos@0
   663
        DBMS_OUTPUT.PUT_LINE('Updating PInstance - Finished ' || v_Message) ;
carlos@0
   664
        AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', 1, v_Message) ;
carlos@0
   665
      ELSE
gorkaion@239
   666
        DBMS_OUTPUT.PUT_LINE('--<<M_InOut_Create finished>> ' || v_Message);
carlos@0
   667
      END IF;
carlos@0
   668
      --
carlos@0
   669
      RETURN;
carlos@0
   670
    END; --BODY
carlos@0
   671
EXCEPTION
carlos@0
   672
WHEN OTHERS THEN
carlos@0
   673
  v_ResultStr:= '@ERROR=' || SQLERRM;
carlos@0
   674
  DBMS_OUTPUT.PUT_LINE(v_ResultStr) ;
carlos@0
   675
  IF(p_PInstance_ID IS NOT NULL) THEN
carlos@0
   676
    ROLLBACK;
carlos@0
   677
    AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', 0, v_ResultStr) ;
carlos@0
   678
  ELSE
carlos@0
   679
    DECLARE
carlos@0
   680
      v_Code NUMBER:=SQLCODE;
carlos@0
   681
    BEGIN
asier@799
   682
    AD_SEQUENCE_NEXT('DBA_ERRORLOG', v_AD_Client_ID, v_DBA_ErrorLog_ID);
carlos@0
   683
    INSERT
carlos@0
   684
    INTO DBA_ERRORLOG
carlos@0
   685
      (
carlos@0
   686
        DBA_ErrorLog_ID,
carlos@0
   687
        Created,
carlos@0
   688
        Code,
carlos@0
   689
        Msg,
carlos@0
   690
        Info
carlos@0
   691
      )
carlos@0
   692
      VALUES
carlos@0
   693
      (
asier@799
   694
        v_DBA_ErrorLog_ID,
carlos@0
   695
        now(),
carlos@0
   696
        v_Code,
carlos@0
   697
        v_ResultStr,
carlos@0
   698
        'C_Invoice_Post'
carlos@0
   699
      )
carlos@0
   700
      ;
carlos@0
   701
    RAISE;
carlos@0
   702
  END;
carlos@0
   703
END IF;
carlos@0
   704
p_InOut_ID:=0; -- Error Indicator
carlos@0
   705
RETURN;
antonio@735
   706
END M_INOUT_CREATE
gorkaion@239
   707
]]></body>
adrian@94
   708
    </function>
adrian@94
   709
  </database>