src-db/database/model/functions/M_INOUT_POST.xml
author Gorka Ion Damián <gorkaion.damian@openbravo.com>
Wed, 01 Feb 2012 12:25:42 +0100
changeset 15487 f663c413f0b0
parent 15382 33cb80a95949
child 15570 33175c6c534f
permissions -rw-r--r--
Related to issue 19755.Relate canceled inoutlines with originals.
juanpablo@3490
     1
<?xml version="1.0"?>
juanpablo@3490
     2
  <database name="FUNCTION M_INOUT_POST">
juanpablo@3490
     3
    <function name="M_INOUT_POST" type="NULL">
juanpablo@3490
     4
      <parameter name="p_pinstance_id" type="VARCHAR" mode="in">
juanpablo@3490
     5
        <default/>
juanpablo@3490
     6
      </parameter>
juanpablo@3490
     7
      <parameter name="p_inout_id" type="VARCHAR" mode="in">
juanpablo@3490
     8
        <default/>
juanpablo@3490
     9
      </parameter>
gorkaion@239
    10
      <body><![CDATA[/*************************************************************************
juanpablo@771
    11
  * The contents of this file are subject to the Compiere Public
juanpablo@771
    12
  * License 1.1 ("License"); You may not use this file except in
juanpablo@771
    13
  * compliance with the License. You may obtain a copy of the License in
juanpablo@771
    14
  * the legal folder of your Openbravo installation.
carlos@0
    15
  * Software distributed under the License is distributed on an
carlos@0
    16
  * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
carlos@0
    17
  * implied. See the License for the specific language governing rights
carlos@0
    18
  * and limitations under the License.
juanpablo@778
    19
  * The Original Code is  Compiere  ERP &  Business Solution
juanpablo@771
    20
  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
carlos@0
    21
  * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke,
carlos@0
    22
  * parts created by ComPiere are Copyright (C) ComPiere, Inc.;
carlos@0
    23
  * All Rights Reserved.
ggi@6702
    24
  * Contributor(s): Openbravo SLU
ggi@6702
    25
  * Contributions are Copyright (C) 2001-2010 Openbravo, S.L.U.
juanpablo@771
    26
  *
juanpablo@771
    27
  * Specifically, this derivative work is based upon the following Compiere
juanpablo@771
    28
  * file and version.
carlos@0
    29
  *************************************************************************
carlos@0
    30
  * $Id: M_InOut_Post.sql,v 1.8 2003/09/05 04:58:06 jjanke Exp $
carlos@0
    31
  ***
carlos@0
    32
  * Title: Post M_InOut_ID
carlos@0
    33
  * Description:
carlos@0
    34
  *  Action: COmplete
carlos@0
    35
  *  - Create Transaction
carlos@0
    36
  *    (only stocked products)
carlos@0
    37
  *  - Update Inventory (QtyReserved, QtyOnHand)
carlos@0
    38
  *    (only stocked products)
carlos@0
    39
  *  - Update OrderLine (QtyDelivered)
carlos@0
    40
  *
carlos@0
    41
  *  Action: Reverse Correction
carlos@0
    42
  *  - Create Header and lines with negative Quantities (and header amounts)
carlos@0
    43
  *  - Post it
carlos@0
    44
  ************************************************************************/
carlos@0
    45
  -- Logistice
carlos@0
    46
  v_ResultStr VARCHAR2(2000):='';
carlos@0
    47
  v_Message VARCHAR2(2000):='';
juanpablo@1605
    48
  v_Record_ID VARCHAR2(32);
juanpablo@1605
    49
  v_User VARCHAR2(32);
harikrishnan@7227
    50
  v_PUser VARCHAR2(32);
asier@1929
    51
  v_is_included NUMBER:=0;
asier@1929
    52
  v_DocType_ID VARCHAR2(32);
gorkaion@14547
    53
  v_isreturndoctype CHAR(1);
asier@1929
    54
  v_available_period NUMBER:=0;
asier@1929
    55
  v_is_ready AD_Org.IsReady%TYPE;
asier@1929
    56
  v_is_tr_allow AD_OrgType.IsTransactionsAllowed%TYPE;
asier@1929
    57
  v_DateAcct DATE;
victor@3065
    58
  v_isacctle AD_OrgType.IsAcctLegalEntity%TYPE;
victor@3065
    59
  v_org_bule_id AD_Org.AD_Org_ID%TYPE;
carlos@0
    60
  -- Parameter
carlos@0
    61
  TYPE RECORD IS REF CURSOR;
carlos@0
    62
    Cur_Parameter RECORD;
carlos@0
    63
    --
carlos@0
    64
    Cur_InOut RECORD;
carlos@0
    65
    Cur_InOutLine RECORD;
gorkaion@3170
    66
    Cur_Order RECORD;
carlos@0
    67
    --
carlos@0
    68
    v_Result NUMBER:=1;
juanpablo@1605
    69
    v_AD_Org_ID VARCHAR2(32);
juanpablo@1605
    70
    v_AD_Client_ID VARCHAR2(32);
juanpablo@1605
    71
    v_NextNo VARCHAR2(32);
carlos@0
    72
    v_Qty NUMBER;
carlos@0
    73
    v_QtyPO NUMBER;
carlos@0
    74
    v_QtySO NUMBER;
carlos@0
    75
    v_QuantityOrder NUMBER;
carlos@0
    76
    v_QuantityOrderPO NUMBER;
carlos@0
    77
    v_QuantityOrderSO NUMBER;
carlos@0
    78
    v_RDocumentNo VARCHAR2(40) ;
juanpablo@1605
    79
    v_RInOut_ID VARCHAR2(32);
carlos@0
    80
    v_IsStocked NUMBER;
juanpablo@1605
    81
    v_DoctypeReversed_ID VARCHAR2(32);
carlos@0
    82
    --MODIFIED BY F.IRIAZABAL
carlos@0
    83
    v_QtyOrder NUMBER;
carlos@0
    84
    v_ProductUOM NUMBER;
asier@2586
    85
    v_BreakDown VARCHAR2(60) ;
carlos@0
    86
    v_ActualQty NUMBER;
carlos@0
    87
    v_QtyAux NUMBER;
carlos@0
    88
    v_Count NUMBER:=0;
carlos@0
    89
    v_Line VARCHAR2(10) ;
gorkaion@3170
    90
    v_OrderID_old VARCHAR2(32);
harikrishnan@7945
    91
    Cur_MILines RECORD;
carlos@0
    92
    FINISH_PROCESS BOOLEAN:=false;
harikrishnan@5580
    93
    v_Aux NUMBER;
carlos@0
    94
  BEGIN
carlos@0
    95
    IF(p_PInstance_ID IS NOT NULL) THEN
carlos@0
    96
      --  Update AD_PInstance
carlos@0
    97
      DBMS_OUTPUT.PUT_LINE('Updating PInstance - Processing ' || p_PInstance_ID) ;
carlos@0
    98
      v_ResultStr:='PInstanceNotFound';
carlos@0
    99
      AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'Y', NULL, NULL) ;
carlos@0
   100
      -- Get Parameters
carlos@0
   101
      v_ResultStr:='ReadingParameters';
carlos@0
   102
      FOR Cur_Parameter IN
carlos@0
   103
        (SELECT i.Record_ID,
carlos@0
   104
          i.AD_User_ID,
carlos@0
   105
          p.ParameterName,
carlos@0
   106
          p.P_String,
carlos@0
   107
          p.P_Number,
carlos@0
   108
          p.P_Date
carlos@0
   109
        FROM AD_PInstance i
carlos@0
   110
        LEFT JOIN AD_PInstance_Para p
carlos@0
   111
          ON i.AD_PInstance_ID=p.AD_PInstance_ID
carlos@0
   112
        WHERE i.AD_PInstance_ID=p_PInstance_ID
carlos@0
   113
        ORDER BY p.SeqNo
carlos@0
   114
        )
carlos@0
   115
      LOOP
carlos@0
   116
        v_Record_ID:=Cur_Parameter.Record_ID;
carlos@0
   117
        v_User:=Cur_Parameter.AD_User_ID;
carlos@0
   118
      END LOOP; -- Get Parameter
carlos@0
   119
      DBMS_OUTPUT.PUT_LINE('  Record_ID=' || v_Record_ID) ;
carlos@0
   120
    ELSE
gorkaion@239
   121
      DBMS_OUTPUT.PUT_LINE('--<<M_InOut_Post>>') ;
carlos@0
   122
      v_Record_ID:=p_InOut_ID;
harikrishnan@7227
   123
      SELECT count(*),updatedby
harikrishnan@7227
   124
        INTO v_Count,v_User
rafael@5825
   125
        FROM M_InOut
harikrishnan@7227
   126
        WHERE M_InOut_ID=v_Record_ID
harikrishnan@7227
   127
        GROUP BY updatedby;
rafael@5825
   128
      IF v_Count=0 THEN
rafael@5825
   129
        FINISH_PROCESS:=true;
rafael@5825
   130
      END IF;
carlos@0
   131
    END IF;
carlos@0
   132
  BEGIN --BODY
rafael@5825
   133
  	IF(NOT FINISH_PROCESS) THEN
harikrishnan@7227
   134
  	  v_PUser:=v_User;
gorkaion@14547
   135
      SELECT io.AD_Client_ID, io.AD_Org_ID, io.CreatedBy, io.C_DocType_ID, io.DateAcct, dt.isreturn
gorkaion@14547
   136
        INTO v_AD_Client_ID, v_AD_Org_ID, v_User, v_DocType_ID, v_DateAcct, v_isreturndoctype
gorkaion@14547
   137
        FROM M_InOut io JOIN c_doctype dt ON io.c_doctype_id = dt.c_doctype_id
gorkaion@14547
   138
        WHERE io.M_InOut_ID=v_Record_ID;
harikrishnan@7227
   139
        IF(v_PUser IS NOT NULL) THEN
harikrishnan@7227
   140
        	v_User:=v_PUser;
harikrishnan@7227
   141
        END IF;
carlos@0
   142
      SELECT count(*)
carlos@0
   143
      INTO v_Count
rafael@5825
   144
      FROM AD_CLIENTINFO
rafael@5825
   145
      WHERE AD_CLIENT_ID=v_AD_Client_ID
rafael@5825
   146
        AND CHECKINOUTORG='Y';
gorkaion@239
   147
      IF v_Count>0 THEN
rafael@5825
   148
        v_ResultStr:='CheckingRestrictions - M_INOUT ORG IS IN C_BPARTNER ORG TREE';
rafael@5825
   149
        SELECT count(*)
rafael@5825
   150
        INTO v_Count
rafael@5825
   151
        FROM M_InOut m,
rafael@5825
   152
          C_BPartner bp
rafael@5825
   153
        WHERE m.M_InOut_ID=v_Record_ID
rafael@5825
   154
          AND m.C_BPARTNER_ID=bp.C_BPARTNER_ID
rafael@5825
   155
          AND AD_IsOrgIncluded(m.AD_ORG_ID, bp.AD_ORG_ID, bp.AD_CLIENT_ID)=-1;
rafael@5825
   156
        IF v_Count>0 THEN
rafael@5825
   157
          RAISE_APPLICATION_ERROR(-20000, '@NotCorrectOrgBpartnerInout@') ;
rafael@5825
   158
        END IF;
carlos@0
   159
      END IF;
rafael@5825
   160
      
rafael@5825
   161
     v_ResultStr:='CheckingRestrictions';
rafael@5825
   162
     SELECT COUNT(*)
rafael@5825
   163
     INTO v_Count
rafael@5825
   164
     FROM C_DocType,
rafael@5825
   165
          M_InOut M
rafael@5825
   166
     WHERE M_Inout_ID = v_Record_ID
rafael@5825
   167
       AND C_DocType.DocBaseType IN ('MMR', 'MMS')
rafael@5825
   168
      AND C_DocType.IsSOTrx=M.IsSOTrx
rafael@5825
   169
      AND AD_ISORGINCLUDED(m.AD_Org_ID,C_DocType.AD_Org_ID, m.AD_Client_ID) <> -1
rafael@5825
   170
       AND M.C_DOCTYPE_ID=C_DocType.C_DOCTYPE_ID;
rafael@5825
   171
        IF v_Count=0 THEN
rafael@5825
   172
          RAISE_APPLICATION_ERROR(-20000, '@NotCorrectOrgDoctypeShipment@') ;
rafael@5825
   173
        END IF;
rafael@5825
   174
        SELECT COUNT(*), MAX(M.line)
rafael@5825
   175
        INTO v_Count, v_line
rafael@5825
   176
        FROM M_InOutLine M,
rafael@5825
   177
          M_Product P
rafael@5825
   178
        WHERE M.M_PRODUCT_ID=P.M_PRODUCT_ID
rafael@5825
   179
          AND P.M_ATTRIBUTESET_ID IS NOT NULL
rafael@6091
   180
          AND (P.ATTRSETVALUETYPE IS NULL OR P.ATTRSETVALUETYPE <> 'F')
rafael@6104
   181
          AND (SELECT ISONEATTRSETVALREQUIRED FROM M_ATTRIBUTESET WHERE M_ATTRIBUTESET_ID = P.M_ATTRIBUTESET_ID) = 'Y'
rafael@5825
   182
          AND COALESCE(M.M_ATTRIBUTESETINSTANCE_ID, '0') = '0'
rafael@5825
   183
          AND M.M_INOUT_ID=v_Record_ID;
rafael@5825
   184
        IF v_Count<>0 THEN
rafael@6091
   185
          RAISE_APPLICATION_ERROR(-20000, '@Inline@'||' '||v_line||' '||'@productWithoutAttributeSet@') ;
rafael@5825
   186
        END IF;
rafael@5825
   187
        SELECT COUNT(*), MAX(M.line)
rafael@5825
   188
        INTO v_Count, v_Line
rafael@5825
   189
        FROM M_InOut I,
rafael@5825
   190
          M_InOutLine M,
rafael@5825
   191
          M_AttributeSetInstance P
rafael@5825
   192
        WHERE I.M_InOut_ID=M.M_InOut_ID
rafael@5825
   193
          AND M.M_AttributeSetInstance_ID=P.M_AttributeSetInstance_ID
rafael@5825
   194
          AND P.ISLOCKED='Y'
rafael@5825
   195
          AND I.ISSOTRX='Y'
rafael@5825
   196
          AND I.M_INOUT_ID=v_Record_ID;
rafael@5825
   197
        IF v_Count<>0 THEN
rafael@5825
   198
          RAISE_APPLICATION_ERROR(-20000, '@Inline@'||v_line||' '||'@lockedProduct@') ;
rafael@5825
   199
        END IF;
rafael@5825
   200
      -- check inout line instance location
rafael@5825
   201
        SELECT COUNT(*), MAX(M.line)
rafael@5825
   202
        INTO v_Count, v_Line
rafael@5825
   203
        FROM M_InOutLine M,
rafael@5825
   204
          M_Product P
rafael@5825
   205
        WHERE M.M_InOut_ID=v_Record_ID
rafael@5825
   206
          AND M.M_Locator_ID IS NULL
rafael@5825
   207
          AND p.m_product_id = m.m_product_id
rafael@5825
   208
          AND p.isstocked = 'Y'
rafael@5825
   209
          AND p.producttype = 'I';
rafael@5825
   210
        IF v_Count <> 0 THEN
rafael@5825
   211
          RAISE_APPLICATION_ERROR(-20000, '@Inline@'||v_line||' '||'@InoutLineWithoutLocator@') ;
rafael@5825
   212
        END IF;	  
rafael@5825
   213
      
gorkaion@14547
   214
    --Check negative quantities on return inouts
gorkaion@14547
   215
    IF (v_isreturndoctype = 'Y') THEN
gorkaion@14547
   216
      SELECT count(*) INTO v_count
gorkaion@14547
   217
      FROM m_inoutline iol JOIN c_orderline ol ON iol.c_orderline_id = ol.c_orderline_id
gorkaion@14547
   218
      WHERE iol.m_inout_id = v_record_id
gorkaion@14547
   219
        AND iol.movementqty > 0
gorkaion@14547
   220
        AND ol.c_order_discount_id IS NULL;
gorkaion@14547
   221
      IF (v_Count <> 0) THEN
gorkaion@14547
   222
        RAISE_APPLICATION_ERROR(-20000, '@ReturnInOutNegativeQty@');
gorkaion@14547
   223
      END IF;
gorkaion@14547
   224
    END IF;
rafael@5825
   225
        -- Process Shipments
rafael@5825
   226
  
rafael@5825
   227
     -- Set org lines like the header
rafael@5825
   228
       UPDATE M_INOUTLINE
harikrishnan@7227
   229
        SET AD_ORG_ID = (SELECT AD_ORG_ID FROM M_INOUT WHERE M_INOUT_ID = v_Record_ID),
harikrishnan@7227
   230
        Updated=now(),
harikrishnan@7227
   231
        UpdatedBy=v_User
rafael@5825
   232
      WHERE M_INOUT_ID = v_Record_ID;
rafael@5825
   233
      
rafael@5825
   234
      -- Check the header belongs to a organization where transactions are posible and ready to use
rafael@5825
   235
      SELECT AD_Org.IsReady, Ad_OrgType.IsTransactionsAllowed
rafael@5825
   236
      INTO v_is_ready, v_is_tr_allow
rafael@5825
   237
      FROM M_INOUT, AD_Org, AD_OrgType
rafael@5825
   238
      WHERE AD_Org.AD_Org_ID=M_INOUT.AD_Org_ID
rafael@5825
   239
      AND AD_Org.AD_OrgType_ID=AD_OrgType.AD_OrgType_ID
rafael@5825
   240
      AND M_INOUT.M_INOUT_ID=v_Record_ID;
rafael@5825
   241
      IF (v_is_ready='N') THEN
rafael@5825
   242
        RAISE_APPLICATION_ERROR(-20000, '@OrgHeaderNotReady@');
carlos@0
   243
      END IF;
rafael@5825
   244
      IF (v_is_tr_allow='N') THEN
rafael@5825
   245
        RAISE_APPLICATION_ERROR(-20000, '@OrgHeaderNotTransAllowed@');
carlos@0
   246
      END IF;
rafael@5825
   247
        
rafael@5825
   248
      SELECT AD_ORG_CHK_DOCUMENTS('M_INOUT', 'M_INOUTLINE', v_Record_ID, 'M_INOUT_ID', 'M_INOUT_ID') INTO v_is_included FROM dual;
rafael@5825
   249
      IF (v_is_included=-1) THEN
rafael@5825
   250
        RAISE_APPLICATION_ERROR(-20000, '@LinesAndHeaderDifferentLEorBU@');
carlos@0
   251
      END IF;
asier@1929
   252
      
rafael@5825
   253
      -- Check the period control is opened (only if it is legal entity with accounting)
rafael@5825
   254
      -- Gets the BU or LE of the document
rafael@5825
   255
      SELECT AD_GET_DOC_LE_BU('M_INOUT', v_Record_ID, 'M_INOUT_ID', 'LE')
rafael@5825
   256
      INTO v_org_bule_id
victor@3065
   257
      FROM DUAL;
victor@3065
   258
      
rafael@5825
   259
      SELECT AD_OrgType.IsAcctLegalEntity
rafael@5825
   260
      INTO v_isacctle
rafael@5825
   261
      FROM AD_OrgType, AD_Org
rafael@5825
   262
      WHERE AD_Org.AD_OrgType_ID = AD_OrgType.AD_OrgType_ID
rafael@5825
   263
      AND AD_Org.AD_Org_ID=v_org_bule_id;
rafael@5825
   264
      
rafael@5825
   265
      IF (v_isacctle='Y') THEN    
rafael@5825
   266
        SELECT C_CHK_OPEN_PERIOD(v_AD_Org_ID, v_DateAcct, NULL, v_DocType_ID) 
rafael@5825
   267
        INTO v_available_period
rafael@5825
   268
        FROM DUAL;
rafael@5825
   269
        
rafael@5825
   270
        IF (v_available_period<>1) THEN
rafael@5825
   271
          RAISE_APPLICATION_ERROR(-20000, '@PeriodNotAvailable@');
rafael@5825
   272
        END IF;
rafael@5825
   273
      END IF;  
rafael@5825
   274
  
rafael@5825
   275
        FOR Cur_InOut IN
rafael@5825
   276
          (SELECT *
rafael@5825
   277
          FROM M_INOUT
rafael@5825
   278
          WHERE(M_InOut_ID=v_Record_ID
rafael@5825
   279
            OR(v_Record_ID IS NULL
rafael@5825
   280
            AND DocAction='CO'))
rafael@5825
   281
            AND IsActive='Y'  FOR UPDATE
rafael@5825
   282
          )
rafael@5825
   283
        LOOP
rafael@5825
   284
          DBMS_OUTPUT.PUT_LINE('Shipment_ID=' || Cur_InOut.M_InOut_ID || ', Doc=' || Cur_InOut.DocumentNo || ', Status=' || Cur_InOut.DocStatus || ', Action=' || Cur_InOut.DocAction) ;
rafael@5825
   285
          v_ResultStr:='HeaderLoop';
rafael@5825
   286
          /**
rafael@5825
   287
          * Shipment not processed
rafael@5825
   288
          */
rafael@5825
   289
          IF(Cur_InOut.Processed='N' AND Cur_InOut.DocStatus='DR' AND Cur_InOut.DocAction='CO') THEN
rafael@5825
   290
            -- For all active shipment lines
rafael@5825
   291
            v_ResultStr:='HeaderLoop-1';
rafael@5825
   292
            SELECT COUNT(*) INTO v_Aux
rafael@5825
   293
        FROM M_InOutLine
rafael@5825
   294
        WHERE M_InOut_ID = v_Record_ID;
rafael@5825
   295
        IF v_Aux=0 THEN
rafael@5825
   296
        RAISE_APPLICATION_ERROR(-20000, '@ReceiptWithoutLines@');
rafael@5825
   297
        END IF;
carlos@0
   298
          FOR Cur_InOutLine IN
carlos@0
   299
            (SELECT *
carlos@0
   300
            FROM M_INOUTLINE
carlos@0
   301
            WHERE M_InOut_ID=Cur_InOut.M_InOut_ID
carlos@0
   302
              AND IsActive='Y'  FOR UPDATE
carlos@0
   303
            )
carlos@0
   304
          LOOP
adrian@170
   305
            -- Incomming or Outgoing :1:2
carlos@0
   306
            v_Qty:=Cur_InOutLine.MovementQty;
carlos@0
   307
            v_QuantityOrder:=Cur_InOutLine.QuantityOrder;
carlos@0
   308
            IF(SUBSTR(Cur_InOut.MovementType, 2)='-') THEN
carlos@0
   309
              v_Qty:=- Cur_InOutLine.MovementQty;
carlos@0
   310
              v_QuantityOrder:=-Cur_InOutLine.QuantityOrder;
carlos@0
   311
            END IF;
carlos@0
   312
            IF(Cur_InOut.IsSOTrx='N') THEN
carlos@0
   313
              v_QtySO:=0;
carlos@0
   314
              v_QtyPO:=Cur_InOutLine.MovementQty;
carlos@0
   315
              v_QuantityOrderSO:=0;
carlos@0
   316
              v_QuantityOrderPO:=Cur_InOutLine.QuantityOrder;
carlos@0
   317
            ELSE
carlos@0
   318
              v_QtySO:=Cur_InOutLine.MovementQty;
carlos@0
   319
              v_QtyPO:=0;
carlos@0
   320
              v_QuantityOrderSO:=Cur_InOutLine.QuantityOrder;
carlos@0
   321
              v_QuantityOrderPO:=0;
carlos@0
   322
            END IF;
carlos@0
   323
            -- UOM Conversion
adrian@170
   324
            -- Is it a standard stocked product:3
carlos@0
   325
            SELECT COUNT(*)
carlos@0
   326
            INTO v_IsStocked
carlos@0
   327
            FROM M_PRODUCT
carlos@0
   328
            WHERE M_Product_ID=Cur_InOutLine.M_Product_ID
carlos@0
   329
              AND IsStocked='Y'
carlos@0
   330
              AND ProductType='I';
carlos@0
   331
            -- Create Transaction for stocked product
harikrishnan@8015
   332
            IF(Cur_InOutLine.M_Product_ID IS NOT NULL AND v_IsStocked=1 AND Cur_InOutLine.IsDescription <> 'Y') THEN
carlos@0
   333
              v_ResultStr:='CreateTransaction';
carlos@0
   334
              Ad_Sequence_Next('M_Transaction', Cur_InOutLine.AD_Org_ID, v_NextNo) ;
carlos@0
   335
              INSERT
carlos@0
   336
              INTO M_TRANSACTION
carlos@0
   337
                (
carlos@0
   338
                  M_Transaction_ID, M_InOutLine_ID, AD_Client_ID, AD_Org_ID,
carlos@0
   339
                  IsActive, Created, CreatedBy, Updated,
carlos@0
   340
                  UpdatedBy, MovementType, M_Locator_ID, M_Product_ID,
carlos@0
   341
                  M_AttributeSetInstance_ID, MovementDate, MovementQty, M_Product_UOM_ID,
carlos@0
   342
                  QuantityOrder, C_UOM_ID
carlos@0
   343
                )
carlos@0
   344
                VALUES
carlos@0
   345
                (
carlos@0
   346
                  v_NextNo, Cur_InOutLine.M_InOutLine_ID, Cur_InOutLine.AD_Client_ID, Cur_InOutLine.AD_Org_ID,
harikrishnan@7227
   347
                   'Y', now(), v_User, now(),
harikrishnan@7227
   348
                  v_User, Cur_InOut.MovementType, Cur_InOutLine.M_Locator_ID, Cur_InOutLine.M_Product_ID,
juanpablo@1605
   349
                  COALESCE(Cur_InOutLine.M_AttributeSetInstance_ID, '0'), Cur_InOut.MovementDate, v_Qty, Cur_InOutLine.M_Product_UOM_ID,
carlos@0
   350
                  v_QuantityOrder, Cur_InOutLine.C_UOM_ID
carlos@0
   351
                )
carlos@0
   352
                ;
carlos@0
   353
            END IF;
carlos@0
   354
            -- Create Asset
carlos@0
   355
            IF(Cur_InOutLine.M_Product_ID IS NOT NULL AND Cur_InOut.IsSOTrx='Y') THEN
antonio@735
   356
              A_ASSET_CREATE(NULL, Cur_InOutLine.M_InOutLine_ID) ;
carlos@0
   357
            END IF;
carlos@0
   358
            v_ResultStr:='UpdateOrderLine';
carlos@0
   359
            IF(Cur_InOutLine.C_OrderLine_ID IS NOT NULL) THEN
carlos@0
   360
              -- stocked product
carlos@0
   361
              IF(Cur_InOutLine.M_Product_ID IS NOT NULL AND v_IsStocked=1) THEN
carlos@0
   362
                -- Update OrderLine (if C-, Qty is negative)
carlos@0
   363
                UPDATE C_ORDERLINE
carlos@0
   364
                  SET QtyReserved=QtyReserved - v_QtyPO - v_QtySO,
carlos@0
   365
                  QtyDelivered=QtyDelivered + v_QtySO,
harikrishnan@7227
   366
                  Updated=now(),
harikrishnan@7227
   367
                  UpdatedBy=v_User
carlos@0
   368
                WHERE C_OrderLine_ID=Cur_InOutLine.C_OrderLine_ID;
carlos@0
   369
                -- Products not stocked
carlos@0
   370
              ELSE
carlos@0
   371
                -- Update OrderLine (if C-, Qty is negative)
carlos@0
   372
                UPDATE C_ORDERLINE
carlos@0
   373
                  SET QtyDelivered=QtyDelivered + v_QtySO,
harikrishnan@7227
   374
                  Updated=now(),
harikrishnan@7227
   375
                  UpdatedBy=v_User
carlos@0
   376
                WHERE C_OrderLine_ID=Cur_InOutLine.C_OrderLine_ID;
carlos@0
   377
              END IF;
carlos@0
   378
            END IF;
carlos@0
   379
            IF(Cur_InOutLine.M_Product_ID IS NOT NULL AND v_IsStocked=1) THEN
carlos@0
   380
              M_Check_Stock(Cur_InOutLine.M_Product_ID, v_AD_Client_ID, v_AD_Org_ID, v_Result, v_Message) ;
carlos@0
   381
              IF v_Result=0 THEN
carlos@0
   382
			    RAISE_APPLICATION_ERROR(-20000, v_Message||' '||'@line@'||' '||Cur_InOutLine.line) ;
carlos@0
   383
              END IF;
carlos@0
   384
            END IF;
carlos@0
   385
          END LOOP; -- For all InOut Lines
carlos@0
   386
          /*******************
carlos@0
   387
          * PO Matching
carlos@0
   388
          ******************/
carlos@0
   389
          IF(Cur_InOut.IsSOTrx='N') THEN
carlos@0
   390
            DECLARE
carlos@0
   391
              Cur_SLines RECORD;
carlos@0
   392
              Cur_ILines RECORD;
carlos@0
   393
              v_Qty NUMBER;
juanpablo@1605
   394
              v_MatchPO_ID VARCHAR2(32) ;
juanpablo@1605
   395
              v_MatchInv_ID VARCHAR2(32) ;
carlos@0
   396
            BEGIN
carlos@0
   397
              v_ResultStr:='MatchPO';
carlos@0
   398
              FOR Cur_SLines IN
carlos@0
   399
                (SELECT sl.AD_Client_ID,
carlos@0
   400
                  sl.AD_Org_ID,
carlos@0
   401
                  ol.C_OrderLine_ID,
carlos@0
   402
                  sl.M_InOutLine_ID,
carlos@0
   403
                  sl.M_Product_ID,
carlos@0
   404
                  sl.M_AttributeSetInstance_ID,
carlos@0
   405
                  sl.MovementQty,
carlos@0
   406
                  ol.QtyOrdered
carlos@0
   407
                FROM M_INOUTLINE sl,
carlos@0
   408
                  C_ORDERLINE ol
carlos@0
   409
                WHERE sl.C_OrderLine_ID=ol.C_OrderLine_ID
carlos@0
   410
                  AND sl.M_Product_ID=ol.M_Product_ID  --    AND   sl.M_AttributeSetInstance_ID=ol.M_AttributeSetInstance_ID
carlos@0
   411
                  AND sl.M_InOut_ID=Cur_InOut.M_InOut_ID
carlos@0
   412
                )
carlos@0
   413
              LOOP
carlos@0
   414
                Ad_Sequence_Next('M_MatchPO', Cur_SLines.AD_Org_ID, v_MatchPO_ID) ;
carlos@0
   415
                -- The min qty. Modified by Ismael Ciordia
carlos@0
   416
                v_Qty:=Cur_SLines.MovementQty;
gorkaion@239
   417
                --IF (ABS(Cur_SLines.MovementQty) > ABS(Cur_SLines.QtyOrdered)) THEN
carlos@0
   418
                -- v_Qty := Cur_SLines.QtyOrdered;
carlos@0
   419
                --END IF;
carlos@0
   420
                v_ResultStr:='InsertMatchPO ' || v_MatchPO_ID;
carlos@0
   421
                INSERT
carlos@0
   422
                INTO M_MATCHPO
carlos@0
   423
                  (
carlos@0
   424
                    M_MatchPO_ID, AD_Client_ID, AD_Org_ID, IsActive,
carlos@0
   425
                    Created, CreatedBy, Updated, UpdatedBy,
carlos@0
   426
                    M_InOutLine_ID, C_OrderLine_ID, M_Product_ID, DateTrx,
carlos@0
   427
                    Qty, Processing, Processed, Posted
carlos@0
   428
                  )
carlos@0
   429
                  VALUES
carlos@0
   430
                  (
carlos@0
   431
                    v_MatchPO_ID, Cur_SLines.AD_Client_ID, Cur_SLines.AD_Org_ID, 'Y',
harikrishnan@7227
   432
                    now(), v_User, now(), v_User,
carlos@0
   433
                    Cur_SLines.M_InOutLine_ID, Cur_SLines.C_OrderLine_ID, Cur_SLines.M_Product_ID, now(),
carlos@0
   434
                    v_Qty, 'N', 'Y', 'N'
carlos@0
   435
                  )
carlos@0
   436
                  ;
carlos@0
   437
              END LOOP;
carlos@0
   438
              v_ResultStr:='MatchInv';
carlos@0
   439
              FOR Cur_ILines IN
carlos@0
   440
                (SELECT sl.AD_Client_ID,
carlos@0
   441
                  sl.AD_Org_ID,
carlos@0
   442
                  il.C_InvoiceLine_ID,
carlos@0
   443
                  sl.M_InOutLine_ID,
carlos@0
   444
                  sl.M_Product_ID,
carlos@0
   445
                  sl.M_AttributeSetInstance_ID,
carlos@0
   446
                  sl.MovementQty,
david@7233
   447
                  il.QTYINVOICED,
david@7233
   448
                  i.DateAcct
carlos@0
   449
                FROM M_INOUTLINE sl,
david@7233
   450
                  C_INVOICE i,
carlos@0
   451
                  C_INVOICELINE il
carlos@0
   452
                WHERE sl.M_InOutLine_ID=il.M_InOutLine_ID
carlos@0
   453
                  AND sl.M_InOut_ID=Cur_InOut.M_InOut_ID
david@7233
   454
                  AND i.C_INVOICE_ID = il.C_INVOICE_ID
carlos@0
   455
                )
carlos@0
   456
              LOOP
carlos@0
   457
                Ad_Sequence_Next('M_MatchInv', Cur_ILines.AD_Org_ID, v_MatchInv_ID) ;
carlos@0
   458
                -- The min qty. Modified by Ismael Ciordia
carlos@0
   459
                v_Qty:=Cur_ILines.MovementQty;
gorkaion@239
   460
                --IF (ABS(Cur_ILines.MovementQty) > ABS(Cur_ILines.QtyInvoiced)) THEN
carlos@0
   461
                -- v_Qty := Cur_ILines.QtyInvoiced;
carlos@0
   462
                --END IF;
carlos@0
   463
                v_ResultStr:='InsertMatchPO ' || v_MatchPO_ID;
carlos@0
   464
                INSERT
carlos@0
   465
                INTO M_MATCHINV
carlos@0
   466
                  (
carlos@0
   467
                    M_MATCHINV_ID, AD_CLIENT_ID, AD_ORG_ID, ISACTIVE,
carlos@0
   468
                    CREATED, CREATEDBY, UPDATED, UPDATEDBY,
carlos@0
   469
                    M_INOUTLINE_ID, C_INVOICELINE_ID, M_PRODUCT_ID, DATETRX,
carlos@0
   470
                    QTY, PROCESSING, PROCESSED, POSTED
carlos@0
   471
                  )
carlos@0
   472
                  VALUES
carlos@0
   473
                  (
carlos@0
   474
                    v_MatchInv_ID, Cur_ILines.AD_Client_ID, Cur_ILines.AD_Org_ID, 'Y',
harikrishnan@7227
   475
                    now(), v_User, now(), v_User,
david@7233
   476
                    Cur_ILines.M_InOutLine_ID, Cur_ILines.C_InvoiceLine_ID, Cur_ILines.M_Product_ID, Cur_ILines.DateAcct,
carlos@0
   477
                    v_Qty, 'N', 'Y', 'N'
carlos@0
   478
                  )
carlos@0
   479
                  ;
carlos@0
   480
              END LOOP;
carlos@0
   481
            END;
gorkaion@3170
   482
          ELSE
gorkaion@3170
   483
            v_ResultStr:='Check delivery rule for sales orders';
gorkaion@3170
   484
            v_Message:='';
gorkaion@3170
   485
            v_orderid_old:='0';
gorkaion@3170
   486
            FOR Cur_Order IN 
gorkaion@3170
   487
              (SELECT c_order.deliveryrule, m_inoutline.line, c_order.c_order_id,
gorkaion@3170
   488
                      c_order.documentno, c_orderline.line as orderline
gorkaion@3170
   489
               FROM M_InOutLine, C_Orderline, C_Order
gorkaion@3170
   490
               WHERE M_Inoutline.c_orderline_id = c_orderline.c_orderline_id
gorkaion@3170
   491
                 AND c_orderline.c_order_id = c_order.c_order_id
gorkaion@3170
   492
                 AND m_inoutline.m_inout_id = cur_inout.m_inout_id
gorkaion@3170
   493
                 AND ((c_order.deliveryrule = 'O'
gorkaion@3170
   494
                      AND EXISTS (SELECT 1 FROM C_OrderLine ol
gorkaion@3170
   495
                                  WHERE ol.C_Order_ID = C_order.c_order_id
gorkaion@3170
   496
                                    and ol.qtyordered > ol.qtydelivered ))
gorkaion@3170
   497
                      OR (c_order.deliveryrule = 'L' 
gorkaion@3170
   498
                          AND c_orderline.qtyordered > c_orderline.qtydelivered))
gorkaion@3170
   499
               ORDER BY c_order.c_order_id, c_orderline.line) LOOP
gorkaion@3170
   500
              --Order lines not completely delivered with delivery rule O or L
gorkaion@3170
   501
              IF (v_orderid_old <> cur_order.c_order_id OR cur_order.deliveryrule <> 'O' ) THEN
gorkaion@3170
   502
                v_Message := COALESCE(v_Message,'') || '@Shipment@' || ' ' || cur_inout.documentno;
gorkaion@3170
   503
                v_Message := v_Message || ' ' || '@line@' || ' ' || cur_order.line || ': ';
gorkaion@3170
   504
                v_Message := v_Message || '@SalesOrderDocumentno@' || cur_order.documentno;
gorkaion@3170
   505
                IF (cur_order.deliveryrule = 'O') THEN
gorkaion@3170
   506
                  v_Message := v_Message || ' ' || '@notCompleteDeliveryRuleOrder@' || '<br>';
gorkaion@3170
   507
                ELSE
gorkaion@3170
   508
                  v_Message := v_Message || ' ' || '@line@' || ' ' || cur_order.orderline;
gorkaion@3170
   509
                  v_Message := v_Message || ' ' || '@notCompleteDeliveryRuleLine@' || '<br>';
gorkaion@3170
   510
                END IF;
gorkaion@3170
   511
              END IF;
gorkaion@3170
   512
              v_orderid_old := cur_order.c_order_id;
gorkaion@3170
   513
            END LOOP;
rafael@3205
   514
            IF (v_Message IS NOT NULL AND v_Message <> '') THEN
gorkaion@3170
   515
              RAISE_APPLICATION_ERROR(-20000, v_message);
gorkaion@3170
   516
            END IF;
carlos@0
   517
          END IF;
carlos@0
   518
          -- Close Shipment
harikrishnan@7945
   519
          --Entry in Match Invoices
harikrishnan@7945
   520
          FOR Cur_MILines IN
harikrishnan@7945
   521
                (SELECT sl.AD_Client_ID,
harikrishnan@7945
   522
                  sl.AD_Org_ID,
harikrishnan@7945
   523
                  il.C_InvoiceLine_ID,
harikrishnan@7945
   524
                  sl.M_InOutLine_ID,
harikrishnan@7945
   525
                  sl.M_Product_ID,
harikrishnan@7945
   526
                  sl.M_AttributeSetInstance_ID,
harikrishnan@7945
   527
                  sl.MovementQty,
harikrishnan@7945
   528
                  il.QTYINVOICED,
harikrishnan@8015
   529
                  i.DateAcct,
harikrishnan@8015
   530
                  il.IsDescription
harikrishnan@7945
   531
                FROM M_INOUTLINE sl,
harikrishnan@7945
   532
                  C_INVOICE i,
harikrishnan@7945
   533
                  C_INVOICELINE il
harikrishnan@7945
   534
                WHERE sl.M_InOutLine_ID=il.M_InOutLine_ID
harikrishnan@7945
   535
                  AND sl.M_InOut_ID=Cur_InOut.M_InOut_ID
harikrishnan@7945
   536
                  AND i.C_INVOICE_ID = il.C_INVOICE_ID
harikrishnan@7945
   537
                )
harikrishnan@7945
   538
              LOOP
harikrishnan@7945
   539
                --Ad_Sequence_Next('M_MatchInv', Cur_MILines.AD_Org_ID, v_MatchInv_ID) ;
harikrishnan@7945
   540
                -- The min qty. Modified by Ismael Ciordia
harikrishnan@7945
   541
                v_Qty:=Cur_MILines.MovementQty;
harikrishnan@7945
   542
              --  v_ResultStr:='InsertMatchPO ' || v_MatchPO_ID;
harikrishnan@7945
   543
                INSERT
harikrishnan@7945
   544
                INTO M_MATCHINV
harikrishnan@7945
   545
                  (
harikrishnan@7945
   546
                    M_MATCHINV_ID, AD_CLIENT_ID, AD_ORG_ID, ISACTIVE,
harikrishnan@7945
   547
                    CREATED, CREATEDBY, UPDATED, UPDATEDBY,
harikrishnan@7945
   548
                    M_INOUTLINE_ID, C_INVOICELINE_ID, M_PRODUCT_ID, DATETRX,
harikrishnan@7945
   549
                    QTY, PROCESSING, PROCESSED, POSTED
harikrishnan@7945
   550
                  )
harikrishnan@7945
   551
                  VALUES
harikrishnan@7945
   552
                  (
harikrishnan@7945
   553
                    GET_UUID(), Cur_MILines.AD_Client_ID, Cur_MILines.AD_Org_ID, 'Y',
harikrishnan@7945
   554
                    now(), v_User, now(), v_User,
harikrishnan@7945
   555
                    Cur_MILines.M_InOutLine_ID, Cur_MILines.C_InvoiceLine_ID, Cur_MILines.M_Product_ID, Cur_MILines.DateAcct,
harikrishnan@7945
   556
                    v_Qty, 'N', 'Y', 'N'
harikrishnan@7945
   557
                  )
harikrishnan@7945
   558
                  ;
harikrishnan@7945
   559
              END LOOP;
carlos@0
   560
          v_ResultStr:='CloseShipment';
carlos@0
   561
          UPDATE M_INOUT
carlos@0
   562
            SET Processed='Y',
carlos@0
   563
            DocStatus='CO',
carlos@0
   564
            DocAction='--',
harikrishnan@7227
   565
            Updated=now(),
harikrishnan@7227
   566
            UpdatedBy=v_User
carlos@0
   567
          WHERE M_INOUT.M_INOUT_ID=Cur_InOut.M_INOUT_ID;
carlos@0
   568
          --
asier@1027
   569
          
asier@1027
   570
         
carlos@0
   571
          -- Not Processed + Complete --
carlos@0
   572
          /**
carlos@0
   573
          * Reverse Correction
carlos@0
   574
          */
carlos@0
   575
        ELSIF(Cur_InOut.DocStatus='CO' AND Cur_InOut.DocAction='RC') THEN
gorkaion@5377
   576
          --Check that there isn't any line with an invoice if the order's 
gorkaion@5377
   577
          --invoice rule is after delivery
gorkaion@5377
   578
          select count(*), max(line) into v_count, v_line
gorkaion@5377
   579
          from (
gorkaion@5377
   580
          SELECT m_inoutline.m_inoutline_id, m_inoutline.line
gorkaion@5377
   581
          from m_inoutline, c_order, c_orderline, c_invoiceline, m_inout, c_invoice
gorkaion@5377
   582
          where m_inoutline.c_orderline_id = c_orderline.c_orderline_id
gorkaion@5377
   583
            and c_orderline.c_order_id = c_order.c_order_id
gorkaion@5377
   584
            and c_orderline.c_orderline_id = c_invoiceline.c_orderline_id
gorkaion@5377
   585
            and m_inoutline.m_inout_id = m_inout.m_inout_id
gorkaion@5377
   586
            and c_invoiceline.c_invoice_id = c_invoice.c_invoice_id
gorkaion@5377
   587
            and m_inout.m_inout_id = Cur_InOut.m_inout_id
gorkaion@5377
   588
            and m_inout.issotrx = 'Y'
gorkaion@5377
   589
            and c_order.invoicerule in ('D', 'O', 'S')
gorkaion@5377
   590
            and c_invoice.processed='Y'
gorkaion@5377
   591
          group by m_inoutline.m_inoutline_id, m_inoutline.line
rafael@5506
   592
          having sum(c_invoiceline.qtyinvoiced) <> 0
gorkaion@5377
   593
          ) a;
gorkaion@5377
   594
          IF (v_count > 0 ) THEN
gorkaion@5377
   595
            v_Message := '@InoutDocumentno@' || ': ' || Cur_InOut.DocumentNo || ' ' || '@line@' || ': ' || v_line || '. ';
gorkaion@5377
   596
            v_Message := v_Message || '@VoidShipmentInvoiced@';
gorkaion@5377
   597
            RAISE_APPLICATION_ERROR(-20000, v_Message);
gorkaion@5377
   598
          END IF;
carlos@0
   599
          v_ResultStr:='CreateInOut';
carlos@0
   600
          SELECT COALESCE(C_DOCTYPE_REVERSED_ID, C_DOCTYPE_ID)
carlos@0
   601
          INTO v_DoctypeReversed_ID
carlos@0
   602
          FROM C_DOCTYPE
carlos@0
   603
          WHERE C_DOCTYPE_ID=Cur_InOut.C_DocType_ID;
carlos@0
   604
          Ad_Sequence_Next('M_InOut', Cur_InOut.M_InOut_ID, v_RInOut_ID) ; -- Get RInOut_ID
carlos@0
   605
          Ad_Sequence_Doctype(v_DoctypeReversed_ID, Cur_InOut.M_InOut_ID, 'Y', v_RDocumentNo) ; -- Get RDocumentNo
carlos@0
   606
          IF(v_RDocumentNo IS NULL) THEN
carlos@0
   607
            AD_Sequence_Doc('DocumentNo_M_InOut', Cur_InOut.AD_Client_ID, 'Y', v_RDocumentNo) ;
carlos@0
   608
          END IF;
carlos@0
   609
          -- Indicate that it is invoiced (i.e. not printed on invoices)
carlos@0
   610
          v_ResultStr:='SetInvoiced';
harikrishnan@7227
   611
          UPDATE M_INOUTLINE  SET IsInvoiced='Y',Updated=now(),UpdatedBy=v_User  WHERE M_InOut_ID=Cur_InOut.M_InOut_ID;
carlos@0
   612
          --
carlos@0
   613
          DBMS_OUTPUT.PUT_LINE('Reverse InOut_ID=' || v_RInOut_ID || ' DocumentNo=' || v_RDocumentNo) ;
carlos@0
   614
          v_ResultStr:='InsertInOut Reverse ' || v_RInOut_ID;
carlos@0
   615
          INSERT
carlos@0
   616
          INTO M_INOUT
carlos@0
   617
            (
carlos@0
   618
              M_InOut_ID, C_Order_ID, IsSOTrx, AD_Client_ID,
carlos@0
   619
              AD_Org_ID, IsActive, Created, CreatedBy,
carlos@0
   620
              Updated, UpdatedBy, DocumentNo, C_DocType_ID,
carlos@0
   621
              Description, IsPrinted, MovementType, MovementDate,
carlos@0
   622
              DateAcct, C_BPartner_ID, C_BPartner_Location_ID, AD_User_ID,
carlos@0
   623
              M_Warehouse_ID, POReference, DateOrdered, DeliveryRule,
carlos@0
   624
              FreightCostRule, FreightAmt, C_Project_ID, C_Activity_ID,
carlos@0
   625
              C_Campaign_ID, AD_OrgTrx_ID, User1_ID, User2_ID,
carlos@0
   626
              DeliveryViaRule, M_Shipper_ID, C_Charge_ID, ChargeAmt,
carlos@0
   627
              PriorityRule, DocStatus, DocAction, Processing,
carlos@0
   628
              Processed, ISLOGISTIC, salesrep_id
carlos@0
   629
            )
carlos@0
   630
            VALUES
carlos@0
   631
            (
carlos@0
   632
              v_RInOut_ID, Cur_InOut.C_Order_ID, Cur_InOut.IsSOTrx, Cur_InOut.AD_Client_ID,
harikrishnan@7227
   633
              Cur_InOut.AD_Org_ID, 'Y', now(), v_User,
harikrishnan@7227
   634
              now(), v_User, v_RDocumentNo, v_DoctypeReversed_ID,
unai@15382
   635
               '(*R*: ' || Cur_InOut.DocumentNo || ') ' || COALESCE(TO_CHAR(Cur_InOut.Description), ''), 'N', Cur_InOut.MovementType, Cur_InOut.MovementDate,
carlos@0
   636
              Cur_InOut.DateAcct, Cur_InOut.C_BPartner_ID, Cur_InOut.C_BPartner_Location_ID, Cur_InOut.AD_User_ID,
carlos@0
   637
              Cur_InOut.M_Warehouse_ID, Cur_InOut.POReference, Cur_InOut.DateOrdered, Cur_InOut.DeliveryRule,
carlos@0
   638
              Cur_InOut.FreightCostRule, Cur_InOut.FreightAmt * -1, Cur_InOut.C_Project_ID, Cur_InOut.C_Activity_ID,
carlos@0
   639
              Cur_InOut.C_Campaign_ID, Cur_InOut.AD_OrgTrx_ID, Cur_InOut.User1_ID, Cur_InOut.User2_ID,
carlos@0
   640
              Cur_InOut.DeliveryViaRule, Cur_InOut.M_Shipper_ID, Cur_InOut.C_Charge_ID, Cur_InOut.ChargeAmt * -1,
carlos@0
   641
              Cur_InOut.PriorityRule, 'DR', 'CO', 'N',
carlos@0
   642
               'N', Cur_InOut.islogistic, Cur_InOut.salesrep_id
carlos@0
   643
            )
carlos@0
   644
            ;
carlos@0
   645
          v_ResultStr:='InsertInOutLine';
carlos@0
   646
          FOR Cur_InOutLine IN
carlos@0
   647
            (SELECT *
carlos@0
   648
            FROM M_INOUTLINE
carlos@0
   649
            WHERE M_InOut_ID=Cur_InOut.M_InOut_ID
carlos@0
   650
              AND IsActive='Y'  FOR UPDATE
carlos@0
   651
            )
carlos@0
   652
          LOOP
carlos@0
   653
            -- Create InOut Line
carlos@0
   654
            Ad_Sequence_Next('M_InOutLine', Cur_InOut.M_InOut_ID, v_NextNo) ;
carlos@0
   655
            v_ResultStr:='CreateInOutLine';
carlos@0
   656
            INSERT
carlos@0
   657
            INTO M_INOUTLINE
carlos@0
   658
              (
carlos@0
   659
                M_InOutLine_ID, Line, M_InOut_ID, C_OrderLine_ID,
carlos@0
   660
                AD_Client_ID, AD_Org_ID, IsActive, Created,
carlos@0
   661
                CreatedBy, Updated, UpdatedBy, M_Product_ID,
carlos@0
   662
                M_AttributeSetInstance_ID, C_UOM_ID, M_Locator_ID, MovementQty,
asier@799
   663
                Description, IsInvoiced,  --MODIFIED BY F.IRIAZABAL
gorkaion@15487
   664
                QuantityOrder, M_Product_UOM_ID, IsDescription,
gorkaion@15487
   665
                canceled_inoutline_id
carlos@0
   666
              )
carlos@0
   667
              VALUES
carlos@0
   668
              (
carlos@0
   669
                v_NextNo, Cur_InOutLine.Line, v_RInOut_ID, Cur_InOutLine.C_OrderLine_ID,
carlos@0
   670
                Cur_InOut.AD_Client_ID, Cur_InOut.AD_Org_ID, 'Y', now(),
harikrishnan@7227
   671
                v_User, now(), v_User, Cur_InOutLine.M_Product_ID,
carlos@0
   672
                Cur_InOutLine.M_AttributeSetInstance_ID, Cur_InOutLine.C_UOM_ID, Cur_InOutLine.M_Locator_ID, Cur_InOutLine.MovementQty * -1,
unai@15382
   673
                 '*R*: ' || COALESCE(TO_CHAR(Cur_InOutLine.Description), ''), Cur_InOutLine.IsInvoiced, --MODIFIED BY F.IRIAZABAL
gorkaion@15487
   674
                Cur_InOutLine.QuantityOrder * -1, Cur_InOutLine.M_PRODUCT_UOM_ID, Cur_InOutLine.IsDescription,
gorkaion@15487
   675
                Cur_InOutLine.M_InOutLine_ID
carlos@0
   676
              )
carlos@0
   677
              ;
david@7235
   678
            INSERT INTO M_MATCHINV
david@7235
   679
              (M_MATCHINV_ID, AD_CLIENT_ID, AD_ORG_ID, ISACTIVE, CREATED, CREATEDBY, UPDATED, UPDATEDBY,
david@7235
   680
              M_INOUTLINE_ID, C_INVOICELINE_ID, M_PRODUCT_ID, DATETRX, QTY, PROCESSING, PROCESSED, POSTED)
david@7235
   681
            SELECT
david@7235
   682
              GET_UUID(), MI.AD_CLIENT_ID, MI.AD_ORG_ID, MI.ISACTIVE, now(), '0', now(), '0',
david@7240
   683
              v_NextNo, MI.C_INVOICELINE_ID, MI.M_PRODUCT_ID, MI.DATETRX, -MI.QTY, 'N', 'Y', 'N'
david@7235
   684
            FROM M_MATCHINV MI
david@7235
   685
            WHERE MI.M_INOUTLINE_ID = Cur_InOutLine.M_InOutLine_ID;
carlos@0
   686
          END LOOP;
carlos@0
   687
          -- Close Order
carlos@0
   688
          v_ResultStr:='CloseInOut';
carlos@0
   689
          UPDATE M_INOUT
carlos@0
   690
            SET Description=COALESCE(TO_CHAR(Description), '') || ' (*R*=' || v_RDocumentNo || ')',
carlos@0
   691
            Processed='Y',
ainhoa@2454
   692
            DocStatus='VO', -- it IS reversed
carlos@0
   693
            DocAction='--',
carlos@0
   694
            Updated=now(),
carlos@0
   695
            UpdatedBy=v_User
carlos@0
   696
          WHERE M_INOUT.M_INOUT_ID=Cur_InOut.M_INOUT_ID;
unai@15382
   697
unai@15382
   698
	   FOR Cur_InOutLine IN
unai@15382
   699
            (SELECT *
unai@15382
   700
            FROM M_INOUTLINE
unai@15382
   701
            WHERE M_InOut_ID=Cur_InOut.M_InOut_ID
unai@15382
   702
              AND IsActive='Y'  FOR UPDATE
unai@15382
   703
            )
unai@15382
   704
	  LOOP
unai@15382
   705
            UPDATE M_INOUTLINE
unai@15382
   706
              SET Description=COALESCE(TO_CHAR(Cur_InOutLine.Description), '') || ' : *R*',
unai@15382
   707
              Updated=now(),
unai@15382
   708
              UpdatedBy=v_User
unai@15382
   709
            WHERE M_INOUTLINE.M_INOUTLINE_ID=Cur_InOutLine.M_INOUTLINE_ID;
unai@15382
   710
          END LOOP;
unai@15382
   711
          
carlos@0
   712
          -- Post Reversal
carlos@0
   713
          v_ResultStr:='PostReversal';
antonio@735
   714
          M_INOUT_POST(NULL, v_RInOut_ID) ;
carlos@0
   715
          -- Indicate as Reversal Transaction
carlos@0
   716
          v_ResultStr:='IndicateReversal';
carlos@0
   717
          UPDATE M_INOUT
carlos@0
   718
            SET Updated=now(),
carlos@0
   719
            UpdatedBy=v_User,
ainhoa@2454
   720
            DocStatus='VO' -- the reversal transaction
carlos@0
   721
          WHERE M_InOut_ID=v_RInOut_ID;
carlos@0
   722
        END IF; -- ReverseCorrection
adrianromero@7702
   723
adrianromero@7702
   724
        --M_Inout_Post - Finish_Process Extension Point
adrianromero@7702
   725
        --Extension point at the end of the M_Inout_Post. It has 5 available parameters Record_ID, DocAction, User, Message and Result
adrianromero@7702
   726
        SELECT count(*) INTO v_count
adrianromero@7702
   727
        FROM DUAL
adrianromero@7702
   728
        where exists (select 1 from ad_ep_procedures where ad_extension_points_id = '5A7C6972321E42C2A5A8E9D6D73E6A7C');
adrianromero@7702
   729
        IF (v_count=1) THEN
adrianromero@7702
   730
          DECLARE
adrianromero@7702
   731
            v_ep_instance VARCHAR2(32);
adrianromero@7702
   732
            v_extension_point_id VARCHAR2(32) := '5A7C6972321E42C2A5A8E9D6D73E6A7C';
adrianromero@7702
   733
          BEGIN
adrianromero@7702
   734
            v_ep_instance := get_uuid();
adrianromero@7702
   735
            AD_EP_INSTANCE_PARA_INSERT(v_ep_instance, v_extension_point_id, 'Record_ID',
adrianromero@7702
   736
              v_record_id, NULL, NULL, NULL, NULL, NULL, NULL);
adrianromero@7702
   737
            AD_EP_INSTANCE_PARA_INSERT(v_ep_instance, v_extension_point_id, 'DocAction',
adrianromero@7702
   738
              Cur_InOut.DocAction, NULL, NULL, NULL, NULL, NULL, NULL);
adrianromero@7702
   739
            AD_EP_INSTANCE_PARA_INSERT(v_ep_instance, v_extension_point_id, 'User',
adrianromero@7702
   740
              v_User, NULL, NULL, NULL, NULL, NULL, NULL);
adrianromero@7702
   741
            AD_EP_INSTANCE_PARA_INSERT(v_ep_instance, v_extension_point_id, 'Message',
adrianromero@7702
   742
              NULL, NULL, NULL, NULL, NULL, NULL, v_Message);
adrianromero@7702
   743
            AD_EP_INSTANCE_PARA_INSERT(v_ep_instance, v_extension_point_id, 'Result',
adrianromero@7702
   744
              NULL, NULL, v_Result, NULL, NULL, NULL, NULL);
adrianromero@7702
   745
            AD_EXTENSION_POINT_HANDLER(v_ep_instance, v_extension_point_id);
adrianromero@7702
   746
            SELECT p_number INTO v_Result
adrianromero@7702
   747
            FROM ad_ep_instance_para
adrianromero@7702
   748
            WHERE ad_ep_instance_id = v_ep_instance
adrianromero@7702
   749
              AND parametername LIKE 'Result';
adrianromero@7702
   750
            SELECT p_text INTO v_Message
adrianromero@7702
   751
            FROM ad_ep_instance_para
adrianromero@7702
   752
            WHERE ad_ep_instance_id = v_ep_instance
adrianromero@7702
   753
              AND parametername LIKE 'Message';
adrianromero@7702
   754
adrianromero@7702
   755
            DELETE FROM ad_ep_instance_para
adrianromero@7702
   756
            WHERE ad_ep_instance_id = v_ep_instance;
adrianromero@7702
   757
          END;
adrianromero@7702
   758
        END IF;
adrianromero@7702
   759
carlos@0
   760
      END LOOP; -- InOut Header
carlos@0
   761
      /**
carlos@0
   762
      * Transaction End
carlos@0
   763
      */
carlos@0
   764
      v_ResultStr:='Fini';
carlos@0
   765
    END IF; --FINISH_PROCESS
gorkaion@239
   766
    --<<FINISH_PROCESS>>
carlos@0
   767
    IF(p_PInstance_ID IS NOT NULL) THEN
carlos@0
   768
      --  Update AD_PInstance
carlos@0
   769
      DBMS_OUTPUT.PUT_LINE('Updating PInstance - Finished ' || v_Message) ;
carlos@0
   770
      AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', v_Result, v_Message) ;
carlos@0
   771
    ELSE
gorkaion@239
   772
      DBMS_OUTPUT.PUT_LINE('--<<M_InOut_Post finished>>') ;
carlos@0
   773
    END IF;
carlos@0
   774
    RETURN;
carlos@0
   775
  END; --BODY
carlos@0
   776
EXCEPTION
carlos@0
   777
WHEN OTHERS THEN
carlos@0
   778
  v_ResultStr:= '@ERROR=' || SQLERRM;
carlos@0
   779
  DBMS_OUTPUT.PUT_LINE(v_ResultStr) ;
carlos@0
   780
  IF(p_PInstance_ID IS NOT NULL) THEN
carlos@0
   781
    ROLLBACK;
carlos@0
   782
    AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', 0, v_ResultStr) ;
carlos@0
   783
  ELSE
carlos@0
   784
    RAISE;
carlos@0
   785
  END IF;
carlos@0
   786
  RETURN;
antonio@735
   787
END M_INOUT_POST
juanpablo@3490
   788
]]></body>
juanpablo@3490
   789
    </function>
juanpablo@3490
   790
  </database>