src-db/database/model/functions/M_INOUT_POST.xml
author Sandra Huguet <sandra.huguet@openbravo.com>
Fri, 15 Mar 2013 12:45:46 +0100
changeset 20002 3c9887b82ed4
parent 19828 2e4dccb63f7d
child 20053 d6a7bf46c54f
permissions -rw-r--r--
Fixed bug 23277 Change length to v_bpartner_name
Change length to v_bpartner_name in C_INVOICE_POST,
C_ORDER_POST1, M_INOUT_POST, M_REQUISITION_POST
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
sandra@19203
    25
  * Contributions are Copyright (C) 2001-2013 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):='';
gorkaion@18652
    48
  v_Message_aux VARCHAR2(2000):='';
juanpablo@1605
    49
  v_Record_ID VARCHAR2(32);
juanpablo@1605
    50
  v_User VARCHAR2(32);
harikrishnan@7227
    51
  v_PUser VARCHAR2(32);
asier@1929
    52
  v_is_included NUMBER:=0;
asier@1929
    53
  v_DocType_ID VARCHAR2(32);
gorkaion@14547
    54
  v_isreturndoctype CHAR(1);
asier@1929
    55
  v_available_period NUMBER:=0;
asier@1929
    56
  v_is_ready AD_Org.IsReady%TYPE;
asier@1929
    57
  v_is_tr_allow AD_OrgType.IsTransactionsAllowed%TYPE;
asier@1929
    58
  v_DateAcct DATE;
victor@3065
    59
  v_isacctle AD_OrgType.IsAcctLegalEntity%TYPE;
victor@3065
    60
  v_org_bule_id AD_Org.AD_Org_ID%TYPE;
carlos@0
    61
  -- Parameter
carlos@0
    62
  TYPE RECORD IS REF CURSOR;
carlos@0
    63
    Cur_Parameter RECORD;
carlos@0
    64
    --
carlos@0
    65
    Cur_InOut RECORD;
carlos@0
    66
    Cur_InOutLine RECORD;
gorkaion@3170
    67
    Cur_Order RECORD;
carlos@0
    68
    --
carlos@0
    69
    v_Result NUMBER:=1;
juanpablo@1605
    70
    v_AD_Org_ID VARCHAR2(32);
juanpablo@1605
    71
    v_AD_Client_ID VARCHAR2(32);
juanpablo@1605
    72
    v_NextNo VARCHAR2(32);
carlos@0
    73
    v_Qty NUMBER;
carlos@0
    74
    v_QtyPO NUMBER;
carlos@0
    75
    v_QtySO NUMBER;
carlos@0
    76
    v_QuantityOrder NUMBER;
carlos@0
    77
    v_QuantityOrderPO NUMBER;
carlos@0
    78
    v_QuantityOrderSO NUMBER;
carlos@0
    79
    v_RDocumentNo VARCHAR2(40) ;
juanpablo@1605
    80
    v_RInOut_ID VARCHAR2(32);
carlos@0
    81
    v_IsStocked NUMBER;
juanpablo@1605
    82
    v_DoctypeReversed_ID VARCHAR2(32);
carlos@0
    83
    --MODIFIED BY F.IRIAZABAL
carlos@0
    84
    v_QtyOrder NUMBER;
carlos@0
    85
    v_ProductUOM NUMBER;
asier@2586
    86
    v_BreakDown VARCHAR2(60) ;
carlos@0
    87
    v_ActualQty NUMBER;
carlos@0
    88
    v_QtyAux NUMBER;
carlos@0
    89
    v_Count NUMBER:=0;
carlos@0
    90
    v_Line VARCHAR2(10) ;
gorkaion@3170
    91
    v_OrderID_old VARCHAR2(32);
harikrishnan@7945
    92
    Cur_MILines RECORD;
carlos@0
    93
    FINISH_PROCESS BOOLEAN:=false;
harikrishnan@5580
    94
    v_Aux NUMBER;
ioritz@17861
    95
    v_ProductName M_Product.name%TYPE;
gorkaion@18652
    96
    v_reservation_id    VARCHAR2(32);
mikel@19131
    97
    v_voidmovementdate M_Inout.MovementDate%TYPE;
mikel@19131
    98
    v_voiddate_acct M_Inout.DateAcct%TYPE;
david@19194
    99
    v_bpartner_blocked VARCHAR2(1):='N';
david@19194
   100
    v_goods_blocked VARCHAR2(1):='N';
sandra@20002
   101
    v_bpartner_name c_bpartner.name%TYPE;
david@19194
   102
    v_DocAction VARCHAR2(60);
mikel@19131
   103
carlos@0
   104
  BEGIN
carlos@0
   105
    IF(p_PInstance_ID IS NOT NULL) THEN
carlos@0
   106
      --  Update AD_PInstance
carlos@0
   107
      DBMS_OUTPUT.PUT_LINE('Updating PInstance - Processing ' || p_PInstance_ID) ;
carlos@0
   108
      v_ResultStr:='PInstanceNotFound';
carlos@0
   109
      AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'Y', NULL, NULL) ;
carlos@0
   110
      -- Get Parameters
carlos@0
   111
      v_ResultStr:='ReadingParameters';
carlos@0
   112
      FOR Cur_Parameter IN
carlos@0
   113
        (SELECT i.Record_ID,
carlos@0
   114
          i.AD_User_ID,
carlos@0
   115
          p.ParameterName,
carlos@0
   116
          p.P_String,
carlos@0
   117
          p.P_Number,
carlos@0
   118
          p.P_Date
carlos@0
   119
        FROM AD_PInstance i
carlos@0
   120
        LEFT JOIN AD_PInstance_Para p
carlos@0
   121
          ON i.AD_PInstance_ID=p.AD_PInstance_ID
carlos@0
   122
        WHERE i.AD_PInstance_ID=p_PInstance_ID
carlos@0
   123
        ORDER BY p.SeqNo
carlos@0
   124
        )
carlos@0
   125
      LOOP
carlos@0
   126
        v_Record_ID:=Cur_Parameter.Record_ID;
carlos@0
   127
        v_User:=Cur_Parameter.AD_User_ID;
mikel@19131
   128
        IF (Cur_Parameter.parametername = 'voidedDocumentDate') THEN
mikel@19131
   129
          v_voidmovementdate := TO_DATE(Cur_Parameter.p_string, 'YYYY-MM-DD');
mikel@19131
   130
        ELSIF (Cur_Parameter.parametername = 'voidedDocumentAcctDate') THEN
mikel@19131
   131
          v_voiddate_acct := TO_DATE(Cur_Parameter.p_string, 'YYYY-MM-DD');
mikel@19131
   132
        END IF;
carlos@0
   133
      END LOOP; -- Get Parameter
carlos@0
   134
      DBMS_OUTPUT.PUT_LINE('  Record_ID=' || v_Record_ID) ;
carlos@0
   135
    ELSE
gorkaion@239
   136
      DBMS_OUTPUT.PUT_LINE('--<<M_InOut_Post>>') ;
carlos@0
   137
      v_Record_ID:=p_InOut_ID;
harikrishnan@7227
   138
      SELECT count(*),updatedby
harikrishnan@7227
   139
        INTO v_Count,v_User
rafael@5825
   140
        FROM M_InOut
harikrishnan@7227
   141
        WHERE M_InOut_ID=v_Record_ID
harikrishnan@7227
   142
        GROUP BY updatedby;
rafael@5825
   143
      IF v_Count=0 THEN
rafael@5825
   144
        FINISH_PROCESS:=true;
rafael@5825
   145
      END IF;
carlos@0
   146
    END IF;
carlos@0
   147
  BEGIN --BODY
rafael@5825
   148
  	IF(NOT FINISH_PROCESS) THEN
harikrishnan@7227
   149
  	  v_PUser:=v_User;
gorkaion@14547
   150
      SELECT io.AD_Client_ID, io.AD_Org_ID, io.CreatedBy, io.C_DocType_ID, io.DateAcct, dt.isreturn
gorkaion@14547
   151
        INTO v_AD_Client_ID, v_AD_Org_ID, v_User, v_DocType_ID, v_DateAcct, v_isreturndoctype
gorkaion@14547
   152
        FROM M_InOut io JOIN c_doctype dt ON io.c_doctype_id = dt.c_doctype_id
gorkaion@14547
   153
        WHERE io.M_InOut_ID=v_Record_ID;
harikrishnan@7227
   154
        IF(v_PUser IS NOT NULL) THEN
harikrishnan@7227
   155
        	v_User:=v_PUser;
harikrishnan@7227
   156
        END IF;
carlos@0
   157
      SELECT count(*)
carlos@0
   158
      INTO v_Count
rafael@5825
   159
      FROM AD_CLIENTINFO
rafael@5825
   160
      WHERE AD_CLIENT_ID=v_AD_Client_ID
rafael@5825
   161
        AND CHECKINOUTORG='Y';
naiara@19828
   162
      IF (v_Count>0) THEN
rafael@5825
   163
        v_ResultStr:='CheckingRestrictions - M_INOUT ORG IS IN C_BPARTNER ORG TREE';
rafael@5825
   164
        SELECT count(*)
rafael@5825
   165
        INTO v_Count
rafael@5825
   166
        FROM M_InOut m,
rafael@5825
   167
          C_BPartner bp
rafael@5825
   168
        WHERE m.M_InOut_ID=v_Record_ID
rafael@5825
   169
          AND m.C_BPARTNER_ID=bp.C_BPARTNER_ID
rafael@5825
   170
          AND AD_IsOrgIncluded(m.AD_ORG_ID, bp.AD_ORG_ID, bp.AD_CLIENT_ID)=-1;
rafael@5825
   171
        IF v_Count>0 THEN
rafael@5825
   172
          RAISE_APPLICATION_ERROR(-20000, '@NotCorrectOrgBpartnerInout@') ;
rafael@5825
   173
        END IF;
carlos@0
   174
      END IF;
rafael@5825
   175
      
david@19194
   176
      SELECT CASE WHEN (m.ISSOTRX='Y') THEN customer_blocking ELSE vendor_blocking END, CASE WHEN (m.ISSOTRX='Y') 
david@19194
   177
      THEN so_goods_blocking ELSE po_goods_blocking END, name, DocAction
david@19194
   178
      INTO v_bpartner_blocked, v_goods_blocked, v_bpartner_name, v_DocAction
david@19194
   179
      FROM M_InOut m, C_BPartner bp
david@19194
   180
      WHERE m.c_bpartner_id = bp.c_bpartner_id
david@19194
   181
      AND m.M_InOut_ID=v_Record_ID
david@19194
   182
      AND m.C_BPARTNER_ID=bp.C_BPARTNER_ID;
david@19194
   183
      
david@19194
   184
      IF (v_DocAction = 'CO' AND v_bpartner_blocked = 'Y' AND v_goods_blocked = 'Y' AND v_isreturndoctype='N') THEN
david@19194
   185
        RAISE_APPLICATION_ERROR(-20000, '@ThebusinessPartner@'||' '|| v_bpartner_name ||' '||'@BusinessPartnerBlocked@');
david@19194
   186
      END IF;
david@19194
   187
      
rafael@5825
   188
     v_ResultStr:='CheckingRestrictions';
rafael@5825
   189
     SELECT COUNT(*)
rafael@5825
   190
     INTO v_Count
rafael@5825
   191
     FROM C_DocType,
rafael@5825
   192
          M_InOut M
rafael@5825
   193
     WHERE M_Inout_ID = v_Record_ID
rafael@5825
   194
       AND C_DocType.DocBaseType IN ('MMR', 'MMS')
rafael@5825
   195
      AND C_DocType.IsSOTrx=M.IsSOTrx
rafael@5825
   196
      AND AD_ISORGINCLUDED(m.AD_Org_ID,C_DocType.AD_Org_ID, m.AD_Client_ID) <> -1
rafael@5825
   197
       AND M.C_DOCTYPE_ID=C_DocType.C_DOCTYPE_ID;
naiara@19828
   198
        IF (v_Count=0) THEN
rafael@5825
   199
          RAISE_APPLICATION_ERROR(-20000, '@NotCorrectOrgDoctypeShipment@') ;
rafael@5825
   200
        END IF;
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_PRODUCT_ID=P.M_PRODUCT_ID
rafael@5825
   206
          AND P.M_ATTRIBUTESET_ID IS NOT NULL
rafael@6091
   207
          AND (P.ATTRSETVALUETYPE IS NULL OR P.ATTRSETVALUETYPE <> 'F')
rafael@6104
   208
          AND (SELECT ISONEATTRSETVALREQUIRED FROM M_ATTRIBUTESET WHERE M_ATTRIBUTESET_ID = P.M_ATTRIBUTESET_ID) = 'Y'
rafael@5825
   209
          AND COALESCE(M.M_ATTRIBUTESETINSTANCE_ID, '0') = '0'
rafael@5825
   210
          AND M.M_INOUT_ID=v_Record_ID;
rafael@5825
   211
        IF v_Count<>0 THEN
rafael@6091
   212
          RAISE_APPLICATION_ERROR(-20000, '@Inline@'||' '||v_line||' '||'@productWithoutAttributeSet@') ;
rafael@5825
   213
        END IF;
rafael@5825
   214
        SELECT COUNT(*), MAX(M.line)
rafael@5825
   215
        INTO v_Count, v_Line
rafael@5825
   216
        FROM M_InOut I,
rafael@5825
   217
          M_InOutLine M,
rafael@5825
   218
          M_AttributeSetInstance P
rafael@5825
   219
        WHERE I.M_InOut_ID=M.M_InOut_ID
rafael@5825
   220
          AND M.M_AttributeSetInstance_ID=P.M_AttributeSetInstance_ID
rafael@5825
   221
          AND P.ISLOCKED='Y'
rafael@5825
   222
          AND I.ISSOTRX='Y'
rafael@5825
   223
          AND I.M_INOUT_ID=v_Record_ID;
naiara@19828
   224
        IF (v_Count<>0) THEN
rafael@5825
   225
          RAISE_APPLICATION_ERROR(-20000, '@Inline@'||v_line||' '||'@lockedProduct@') ;
rafael@5825
   226
        END IF;
rafael@5825
   227
      -- check inout line instance location
rafael@5825
   228
        SELECT COUNT(*), MAX(M.line)
rafael@5825
   229
        INTO v_Count, v_Line
rafael@5825
   230
        FROM M_InOutLine M,
rafael@5825
   231
          M_Product P
rafael@5825
   232
        WHERE M.M_InOut_ID=v_Record_ID
rafael@5825
   233
          AND M.M_Locator_ID IS NULL
rafael@5825
   234
          AND p.m_product_id = m.m_product_id
rafael@5825
   235
          AND p.isstocked = 'Y'
rafael@5825
   236
          AND p.producttype = 'I';
naiara@19828
   237
        IF (v_Count <> 0) THEN
rafael@5825
   238
          RAISE_APPLICATION_ERROR(-20000, '@Inline@'||v_line||' '||'@InoutLineWithoutLocator@') ;
rafael@5825
   239
        END IF;	  
naiara@19828
   240
      --check if bom non-stockable is exploded
naiara@19828
   241
        SELECT COUNT(*), MAX(M.line)
naiara@19828
   242
        INTO v_Count, v_Line
naiara@19828
   243
        FROM M_InOutLine M,    
naiara@19828
   244
          M_Product P
naiara@19828
   245
        WHERE M.M_InOut_ID=v_Record_ID
naiara@19828
   246
          AND P.isBOM='Y' 
naiara@19828
   247
          AND P.isstocked='N'
naiara@19828
   248
          AND M.explode='N'
naiara@19828
   249
          AND p.m_product_id = m.m_product_id;
naiara@19828
   250
        IF (v_Count <> 0) THEN
naiara@19828
   251
          RAISE_APPLICATION_ERROR(-20000, '@Inline@'||v_line||' '||'@InoutLineNotExploded@') ;
naiara@19828
   252
        END IF;
gorkaion@14547
   253
    --Check negative quantities on return inouts
gorkaion@14547
   254
    IF (v_isreturndoctype = 'Y') THEN
gorkaion@14547
   255
      SELECT count(*) INTO v_count
gorkaion@14547
   256
      FROM m_inoutline iol JOIN c_orderline ol ON iol.c_orderline_id = ol.c_orderline_id
gorkaion@14547
   257
      WHERE iol.m_inout_id = v_record_id
gorkaion@14547
   258
        AND iol.movementqty > 0
ioritz@15868
   259
        AND canceled_inoutline_id IS NULL
gorkaion@14547
   260
        AND ol.c_order_discount_id IS NULL;
gorkaion@14547
   261
      IF (v_Count <> 0) THEN
gorkaion@14547
   262
        RAISE_APPLICATION_ERROR(-20000, '@ReturnInOutNegativeQty@');
gorkaion@14547
   263
      END IF;
gorkaion@14547
   264
    END IF;
rafael@5825
   265
        -- Process Shipments
pandeeswari@19202
   266
      SELECT COUNT(*) INTO v_Aux
pandeeswari@19202
   267
      FROM M_InOutLine
pandeeswari@19202
   268
      WHERE M_InOut_ID = v_Record_ID;
pandeeswari@19202
   269
pandeeswari@19202
   270
      IF v_Aux > 0 THEN  
pandeeswari@19202
   271
        SELECT COUNT(*)
sandra@19203
   272
        INTO v_Count
pandeeswari@19202
   273
        FROM M_INOUT IO, M_INOUTLINE IOL
pandeeswari@19202
   274
        WHERE IO.M_INOUT_ID = IOL.M_INOUT_ID
eduardo@19387
   275
        AND AD_ISORGINCLUDED(IOL.AD_Org_ID, IO.AD_Org_ID, IO.AD_Client_ID) = -1
sandra@19203
   276
        AND IO.M_INOUT_ID = v_Record_ID;
eduardo@19387
   277
        IF (v_Count>0) THEN
sandra@19203
   278
          RAISE_APPLICATION_ERROR(-20000, '@NotCorrectOrgLines@') ;
pandeeswari@19202
   279
        END IF;
pandeeswari@18808
   280
      END IF;
rafael@5825
   281
      
rafael@5825
   282
      -- Check the header belongs to a organization where transactions are posible and ready to use
rafael@5825
   283
      SELECT AD_Org.IsReady, Ad_OrgType.IsTransactionsAllowed
rafael@5825
   284
      INTO v_is_ready, v_is_tr_allow
rafael@5825
   285
      FROM M_INOUT, AD_Org, AD_OrgType
rafael@5825
   286
      WHERE AD_Org.AD_Org_ID=M_INOUT.AD_Org_ID
rafael@5825
   287
      AND AD_Org.AD_OrgType_ID=AD_OrgType.AD_OrgType_ID
rafael@5825
   288
      AND M_INOUT.M_INOUT_ID=v_Record_ID;
rafael@5825
   289
      IF (v_is_ready='N') THEN
rafael@5825
   290
        RAISE_APPLICATION_ERROR(-20000, '@OrgHeaderNotReady@');
carlos@0
   291
      END IF;
rafael@5825
   292
      IF (v_is_tr_allow='N') THEN
rafael@5825
   293
        RAISE_APPLICATION_ERROR(-20000, '@OrgHeaderNotTransAllowed@');
carlos@0
   294
      END IF;
rafael@5825
   295
        
rafael@5825
   296
      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
   297
      IF (v_is_included=-1) THEN
rafael@5825
   298
        RAISE_APPLICATION_ERROR(-20000, '@LinesAndHeaderDifferentLEorBU@');
carlos@0
   299
      END IF;
asier@1929
   300
      
rafael@5825
   301
      -- Check the period control is opened (only if it is legal entity with accounting)
rafael@5825
   302
      -- Gets the BU or LE of the document
rafael@5825
   303
      SELECT AD_GET_DOC_LE_BU('M_INOUT', v_Record_ID, 'M_INOUT_ID', 'LE')
rafael@5825
   304
      INTO v_org_bule_id
victor@3065
   305
      FROM DUAL;
victor@3065
   306
      
rafael@5825
   307
      SELECT AD_OrgType.IsAcctLegalEntity
rafael@5825
   308
      INTO v_isacctle
rafael@5825
   309
      FROM AD_OrgType, AD_Org
rafael@5825
   310
      WHERE AD_Org.AD_OrgType_ID = AD_OrgType.AD_OrgType_ID
rafael@5825
   311
      AND AD_Org.AD_Org_ID=v_org_bule_id;
rafael@5825
   312
      
rafael@5825
   313
      IF (v_isacctle='Y') THEN    
rafael@5825
   314
        SELECT C_CHK_OPEN_PERIOD(v_AD_Org_ID, v_DateAcct, NULL, v_DocType_ID) 
rafael@5825
   315
        INTO v_available_period
rafael@5825
   316
        FROM DUAL;
rafael@5825
   317
        
rafael@5825
   318
        IF (v_available_period<>1) THEN
rafael@5825
   319
          RAISE_APPLICATION_ERROR(-20000, '@PeriodNotAvailable@');
rafael@5825
   320
        END IF;
rafael@5825
   321
      END IF;  
rafael@5825
   322
  
rafael@5825
   323
        FOR Cur_InOut IN
rafael@5825
   324
          (SELECT *
rafael@5825
   325
          FROM M_INOUT
rafael@5825
   326
          WHERE(M_InOut_ID=v_Record_ID
rafael@5825
   327
            OR(v_Record_ID IS NULL
rafael@5825
   328
            AND DocAction='CO'))
rafael@5825
   329
            AND IsActive='Y'  FOR UPDATE
rafael@5825
   330
          )
rafael@5825
   331
        LOOP
rafael@5825
   332
          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
   333
          v_ResultStr:='HeaderLoop';
rafael@5825
   334
          /**
rafael@5825
   335
          * Shipment not processed
rafael@5825
   336
          */
rafael@5825
   337
          IF(Cur_InOut.Processed='N' AND Cur_InOut.DocStatus='DR' AND Cur_InOut.DocAction='CO') THEN
rafael@5825
   338
            -- For all active shipment lines
rafael@5825
   339
            v_ResultStr:='HeaderLoop-1';
pandeeswari@19202
   340
            
rafael@5825
   341
        IF v_Aux=0 THEN
rafael@5825
   342
        RAISE_APPLICATION_ERROR(-20000, '@ReceiptWithoutLines@');
rafael@5825
   343
        END IF;
carlos@0
   344
          FOR Cur_InOutLine IN
carlos@0
   345
            (SELECT *
carlos@0
   346
            FROM M_INOUTLINE
carlos@0
   347
            WHERE M_InOut_ID=Cur_InOut.M_InOut_ID
carlos@0
   348
              AND IsActive='Y'  FOR UPDATE
carlos@0
   349
            )
carlos@0
   350
          LOOP
adrian@170
   351
            -- Incomming or Outgoing :1:2
carlos@0
   352
            v_Qty:=Cur_InOutLine.MovementQty;
carlos@0
   353
            v_QuantityOrder:=Cur_InOutLine.QuantityOrder;
carlos@0
   354
            IF(SUBSTR(Cur_InOut.MovementType, 2)='-') THEN
carlos@0
   355
              v_Qty:=- Cur_InOutLine.MovementQty;
carlos@0
   356
              v_QuantityOrder:=-Cur_InOutLine.QuantityOrder;
carlos@0
   357
            END IF;
carlos@0
   358
            IF(Cur_InOut.IsSOTrx='N') THEN
carlos@0
   359
              v_QtySO:=0;
carlos@0
   360
              v_QtyPO:=Cur_InOutLine.MovementQty;
carlos@0
   361
              v_QuantityOrderSO:=0;
carlos@0
   362
              v_QuantityOrderPO:=Cur_InOutLine.QuantityOrder;
carlos@0
   363
            ELSE
carlos@0
   364
              v_QtySO:=Cur_InOutLine.MovementQty;
carlos@0
   365
              v_QtyPO:=0;
carlos@0
   366
              v_QuantityOrderSO:=Cur_InOutLine.QuantityOrder;
carlos@0
   367
              v_QuantityOrderPO:=0;
carlos@0
   368
            END IF;
carlos@0
   369
            -- UOM Conversion
adrian@170
   370
            -- Is it a standard stocked product:3
carlos@0
   371
            SELECT COUNT(*)
carlos@0
   372
            INTO v_IsStocked
carlos@0
   373
            FROM M_PRODUCT
carlos@0
   374
            WHERE M_Product_ID=Cur_InOutLine.M_Product_ID
carlos@0
   375
              AND IsStocked='Y'
carlos@0
   376
              AND ProductType='I';
carlos@0
   377
            -- Create Transaction for stocked product
harikrishnan@8015
   378
            IF(Cur_InOutLine.M_Product_ID IS NOT NULL AND v_IsStocked=1 AND Cur_InOutLine.IsDescription <> 'Y') THEN
gorkaion@18652
   379
              IF (cur_inout.issotrx = 'Y' AND cur_inoutline.c_orderline_id IS NOT NULL AND v_qty < 0 AND cur_inoutline.canceled_inoutline_id IS NULL) THEN
gorkaion@18652
   380
                -- Manage reservations.
gorkaion@18652
   381
                SELECT count(*), max(m_reservation_id)
gorkaion@18652
   382
                  INTO v_aux, v_reservation_id
gorkaion@18652
   383
                FROM m_reservation
gorkaion@18706
   384
                WHERE c_orderline_id = cur_inoutline.c_orderline_id;
gorkaion@18652
   385
                IF (v_aux > 1) THEN
gorkaion@18652
   386
                  RAISE_APPLICATION_ERROR(-20000, '@SOLineWithMoreThanOneOpenReservation@');
gorkaion@18652
   387
                ELSIF (v_aux = 1) THEN
gorkaion@18652
   388
                  M_RESERVATION_CONSUMPTION(v_reservation_id, cur_inoutline.m_locator_id, cur_inoutline.m_attributesetinstance_id, cur_inoutline.movementqty, v_user, v_result, v_message);
gorkaion@18652
   389
                END IF;
gorkaion@18706
   390
              ELSIF (cur_inout.issotrx = 'Y' AND cur_inoutline.c_orderline_id IS NOT NULL AND v_qty > 0 AND cur_inoutline.canceled_inoutline_id IS NOT NULL) THEN
gorkaion@18706
   391
                -- Undo reservation
gorkaion@18706
   392
                DECLARE
gorkaion@18706
   393
                  cur_released_stock RECORD;
gorkaion@18706
   394
                  v_qtyaux NUMBER;
gorkaion@18706
   395
                  v_undoqty NUMBER;
gorkaion@18706
   396
                BEGIN
gorkaion@18706
   397
                  SELECT count(*), max(m_reservation_id)
gorkaion@18706
   398
                    INTO v_aux, v_reservation_id
gorkaion@18706
   399
                  FROM m_reservation
gorkaion@18706
   400
                  WHERE c_orderline_id = cur_inoutline.c_orderline_id;
gorkaion@18706
   401
                  IF (v_aux > 1) THEN
gorkaion@18706
   402
                    RAISE_APPLICATION_ERROR(-20000, '@SOLineWithMoreThanOneOpenReservation@');
gorkaion@18706
   403
                  ELSIF (v_aux = 1) THEN
gorkaion@18706
   404
                    v_qtyaux := v_qty;
gorkaion@18706
   405
                    FOR cur_released_stock IN (
gorkaion@18706
   406
                        SELECT m_reservation_stock_id, quantity, releasedqty
gorkaion@18706
   407
                        FROM m_reservation_stock
gorkaion@18706
   408
                        WHERE m_locator_id = cur_inoutline.m_locator_id
gorkaion@18706
   409
                          AND COALESCE(m_attributesetinstance_id, '0') = COALESCE(cur_inoutline.m_attributesetinstance_id, '0')
gorkaion@18706
   410
                          AND m_reservation_id = v_reservation_id
gorkaion@18706
   411
                          AND COALESCE(releasedqty, 0) > 0
gorkaion@18706
   412
                        ORDER BY CASE isallocated WHEN 'N' THEN 0 ELSE 1 END
gorkaion@18706
   413
                    ) LOOP
gorkaion@18706
   414
                      v_undoqty := LEAST(v_qtyaux, cur_released_stock.releasedqty);
gorkaion@18706
   415
                      UPDATE m_reservation_stock
gorkaion@18706
   416
                      SET releasedqty = releasedqty - v_undoqty
gorkaion@18706
   417
                      WHERE m_reservation_stock_id = cur_released_stock.m_reservation_stock_id;
gorkaion@18706
   418
                      v_qtyaux := v_qtyaux - v_undoqty;
gorkaion@18706
   419
                    END LOOP;
gorkaion@18706
   420
                  END IF;
gorkaion@18706
   421
                END;
gorkaion@19377
   422
              ELSIF (cur_inout.issotrx = 'N' AND cur_inoutline.canceled_inoutline_id IS NULL) THEN
gorkaion@18681
   423
                -- Manage pre-reserves
gorkaion@18681
   424
                DECLARE
gorkaion@18681
   425
                  cur_reserve_stock RECORD;
gorkaion@18681
   426
                  v_pendingqty NUMBER;
gorkaion@18681
   427
                  v_qtyaux NUMBER;
gorkaion@18681
   428
                  v_res_stock_id VARCHAR2(32);
gorkaion@18681
   429
                BEGIN
gorkaion@18681
   430
                  v_pendingqty := v_qty;
gorkaion@18681
   431
                  FOR cur_reserve_stock IN (
gorkaion@18681
   432
                      SELECT rs.*
gorkaion@18681
   433
                      FROM m_reservation_stock rs JOIN m_reservation r ON rs.m_reservation_id = r.m_reservation_id
gorkaion@18681
   434
                      WHERE rs.c_orderline_id = cur_inoutline.c_orderline_id
gorkaion@18681
   435
                        AND rs.quantity <> COALESCE(rs.releasedqty, 0)
gorkaion@18681
   436
                        AND rs.m_locator_id IS NULL
gorkaion@19377
   437
                        AND r.res_status NOT IN ('DR', 'CL')
gorkaion@18681
   438
                  ) LOOP
gorkaion@18681
   439
                    v_qtyaux := LEAST(cur_reserve_stock.quantity - COALESCE(cur_reserve_stock.releasedqty, 0), v_pendingqty);
gorkaion@18681
   440
                    -- Check if exists a reserved stock for the same orderline, attributes and locator in the reservation
gorkaion@18681
   441
                    SELECT count(*), max(m_reservation_stock_id) INTO v_aux, v_res_stock_id
gorkaion@18681
   442
                    FROM m_reservation_stock
gorkaion@19377
   443
                    WHERE c_orderline_id = cur_inoutline.c_orderline_id
gorkaion@19377
   444
                      AND m_locator_id = cur_inoutline.m_locator_id
gorkaion@19377
   445
                      AND m_reservation_id = cur_reserve_stock.m_reservation_id
gorkaion@19377
   446
                      AND isallocated = 'Y'
gorkaion@19377
   447
                      AND COALESCE(m_attributesetinstance_id, '0') = COALESCE(Cur_InOutLine.M_AttributeSetInstance_ID, '0');
gorkaion@19377
   448
                    -- Update existing prereserved stock to decrease reserved qty
gorkaion@18681
   449
                    UPDATE m_reservation_stock
gorkaion@18681
   450
                    SET quantity = quantity - v_qtyaux
gorkaion@18681
   451
                    WHERE m_reservation_stock_id = cur_reserve_stock.m_reservation_stock_id;
gorkaion@19377
   452
                    -- Insert or update reserved stock by same quantity
gorkaion@18681
   453
                    IF (v_aux > 0) THEN
gorkaion@18681
   454
                      UPDATE m_reservation_stock
gorkaion@18681
   455
                      SET quantity = quantity + v_qtyaux
gorkaion@18681
   456
                      WHERE m_reservation_stock_id = v_res_stock_id;
gorkaion@18681
   457
                    ELSE
gorkaion@18681
   458
                      INSERT INTO m_reservation_stock(
gorkaion@18681
   459
                        m_reservation_stock_id, ad_client_id, ad_org_id, isactive,
gorkaion@18681
   460
                        created, createdby, updated, updatedby,
gorkaion@18681
   461
                        m_reservation_id, m_attributesetinstance_id, m_locator_id, c_orderline_id,
gorkaion@18681
   462
                        quantity, releasedqty, isallocated
gorkaion@18681
   463
                      ) VALUES (
gorkaion@18681
   464
                        get_uuid(), cur_reserve_stock.ad_client_id, cur_reserve_stock.ad_org_id, 'Y',
gorkaion@18681
   465
                        now(), v_user, now(), v_user,
gorkaion@18681
   466
                        cur_reserve_stock.m_reservation_id, cur_inoutline.m_attributesetinstance_id, cur_inoutline.m_locator_id, cur_inoutline.c_orderline_id,
gorkaion@18683
   467
                        v_qtyaux, 0, 'Y'
gorkaion@18681
   468
                      );
gorkaion@18681
   469
                    END IF;
gorkaion@18681
   470
                    v_pendingqty := v_pendingqty - v_qtyaux;
gorkaion@18681
   471
                    IF (v_pendingqty <= 0) THEN
gorkaion@18681
   472
                      EXIT;
gorkaion@18681
   473
                    END IF;
gorkaion@18681
   474
                  END LOOP;
gorkaion@18681
   475
                  DELETE FROM m_reservation_stock
gorkaion@18681
   476
                  WHERE c_orderline_id = cur_inoutline.c_orderline_id
gorkaion@18681
   477
                    AND quantity = 0
gorkaion@18681
   478
                    AND COALESCE(releasedqty, 0) = 0;
gorkaion@18681
   479
                END;
gorkaion@19377
   480
              ELSIF (cur_inout.issotrx = 'N' AND cur_inoutline.canceled_inoutline_id IS NOT NULL AND v_qty < 0) THEN
gorkaion@19377
   481
                -- Revert to pre-reservations
gorkaion@19377
   482
                DECLARE
gorkaion@19377
   483
                  cur_reserve_stock     RECORD;
gorkaion@19377
   484
                  v_pendingqty          NUMBER;
gorkaion@19377
   485
                  v_qtyaux              NUMBER;
gorkaion@19377
   486
                  v_res_stock_id        VARCHAR2(32);
gorkaion@19380
   487
                  v_aux_released NUMBER:= 0;
gorkaion@19377
   488
                BEGIN
gorkaion@19377
   489
                  v_pendingqty := -v_qty;
gorkaion@19377
   490
                  FOR cur_reserve_stock IN (
gorkaion@19377
   491
                      SELECT rs.quantity, COALESCE(rs.releasedqty,0) AS releasedqty, rs.m_reservation_stock_id, rs.m_reservation_id,
gorkaion@19377
   492
                          rs.ad_org_id, rs.ad_client_id
gorkaion@19377
   493
                      FROM m_reservation_stock rs JOIN m_reservation r ON rs.m_reservation_id = r.m_reservation_id
gorkaion@19377
   494
                      WHERE rs.c_orderline_id = cur_inoutline.c_orderline_id
gorkaion@19377
   495
                        AND rs.m_locator_id = cur_inoutline.m_locator_id
gorkaion@19377
   496
                        AND r.res_status NOT IN ('DR', 'CL')
gorkaion@19377
   497
                  ) LOOP
gorkaion@19377
   498
                    v_qtyaux := LEAST((cur_reserve_stock.quantity - COALESCE(cur_reserve_stock.releasedqty, 0)), v_pendingqty);
gorkaion@19377
   499
                    v_aux_released := v_aux_released + COALESCE(cur_reserve_stock.releasedqty, 0);
gorkaion@19377
   500
                    IF (cur_reserve_stock.quantity = COALESCE(cur_reserve_stock.releasedqty, 0)) THEN
gorkaion@19377
   501
                      -- Reservation is completely released do not convert to pre-reserve.
gorkaion@19377
   502
                      CONTINUE;
gorkaion@19377
   503
                    END IF;
gorkaion@19377
   504
                    -- Check if exists a prereservation for the same orderline, attributes and locator in the reservation
gorkaion@19377
   505
                    SELECT count(*), max(m_reservation_stock_id) INTO v_aux, v_res_stock_id
gorkaion@19377
   506
                    FROM m_reservation_stock
gorkaion@19377
   507
                    WHERE c_orderline_id = cur_inoutline.c_orderline_id
gorkaion@19377
   508
                      AND m_locator_id IS NULL
gorkaion@19377
   509
                      AND m_reservation_id = cur_reserve_stock.m_reservation_id;
gorkaion@19377
   510
                    -- Update existing prereserved stock to decrease reserved qty
gorkaion@19377
   511
                    UPDATE m_reservation_stock
gorkaion@19377
   512
                    SET quantity = quantity - v_qtyaux
gorkaion@19377
   513
                    WHERE m_reservation_stock_id = cur_reserve_stock.m_reservation_stock_id;
gorkaion@19377
   514
                    -- Insert or update reserved stock by same quantity
gorkaion@19377
   515
                    IF (v_aux > 0) THEN
gorkaion@19377
   516
                      UPDATE m_reservation_stock
gorkaion@19377
   517
                      SET quantity = quantity + v_qtyaux
gorkaion@19377
   518
                      WHERE m_reservation_stock_id = v_res_stock_id;
gorkaion@19377
   519
                    ELSE
gorkaion@19377
   520
                      INSERT INTO m_reservation_stock (
gorkaion@19377
   521
                        m_reservation_stock_id, ad_client_id, ad_org_id, isactive,
gorkaion@19377
   522
                        created, createdby, updated, updatedby,
gorkaion@19377
   523
                        m_reservation_id, m_attributesetinstance_id, m_locator_id, c_orderline_id,
gorkaion@19377
   524
                        quantity, releasedqty, isallocated
gorkaion@19377
   525
                      ) VALUES (
gorkaion@19377
   526
                        get_uuid(), cur_reserve_stock.ad_client_id, cur_reserve_stock.ad_org_id, 'Y',
gorkaion@19377
   527
                        now(), v_user, now(), v_user,
gorkaion@19377
   528
                        cur_reserve_stock.m_reservation_id, '0', NULL, cur_inoutline.c_orderline_id,
gorkaion@19377
   529
                        v_qtyaux, 0, 'Y'
gorkaion@19377
   530
                      );
gorkaion@19377
   531
                    END IF;
gorkaion@19377
   532
                    v_pendingqty := v_pendingqty - v_qtyaux;
gorkaion@19377
   533
                    IF (v_pendingqty <= 0) THEN
gorkaion@19377
   534
                      EXIT;
gorkaion@19377
   535
                    END IF;
gorkaion@19377
   536
                  END LOOP;
gorkaion@19377
   537
                  IF (v_pendingqty > 0 AND v_aux_released > 0) THEN
gorkaion@19377
   538
                    -- Not all quantity has been reverted to pre-reservation having released quantity.
gorkaion@19377
   539
                    RAISE_APPLICATION_ERROR(-20000, '@ReceiptVoidReleasedQtyFound@');
gorkaion@19377
   540
                  END IF;
gorkaion@19377
   541
                  DELETE FROM m_reservation_stock
gorkaion@19377
   542
                  WHERE c_orderline_id = cur_inoutline.c_orderline_id
gorkaion@19377
   543
                    AND quantity = 0
gorkaion@19377
   544
                    AND COALESCE(releasedqty, 0) = 0;
gorkaion@19377
   545
                END;
gorkaion@18652
   546
              END IF;
gorkaion@18652
   547
              
carlos@0
   548
              v_ResultStr:='CreateTransaction';
carlos@0
   549
              Ad_Sequence_Next('M_Transaction', Cur_InOutLine.AD_Org_ID, v_NextNo) ;
carlos@0
   550
              INSERT
carlos@0
   551
              INTO M_TRANSACTION
carlos@0
   552
                (
carlos@0
   553
                  M_Transaction_ID, M_InOutLine_ID, AD_Client_ID, AD_Org_ID,
carlos@0
   554
                  IsActive, Created, CreatedBy, Updated,
carlos@0
   555
                  UpdatedBy, MovementType, M_Locator_ID, M_Product_ID,
carlos@0
   556
                  M_AttributeSetInstance_ID, MovementDate, MovementQty, M_Product_UOM_ID,
carlos@0
   557
                  QuantityOrder, C_UOM_ID
carlos@0
   558
                )
carlos@0
   559
                VALUES
carlos@0
   560
                (
carlos@0
   561
                  v_NextNo, Cur_InOutLine.M_InOutLine_ID, Cur_InOutLine.AD_Client_ID, Cur_InOutLine.AD_Org_ID,
harikrishnan@7227
   562
                   'Y', now(), v_User, now(),
harikrishnan@7227
   563
                  v_User, Cur_InOut.MovementType, Cur_InOutLine.M_Locator_ID, Cur_InOutLine.M_Product_ID,
juanpablo@1605
   564
                  COALESCE(Cur_InOutLine.M_AttributeSetInstance_ID, '0'), Cur_InOut.MovementDate, v_Qty, Cur_InOutLine.M_Product_UOM_ID,
carlos@0
   565
                  v_QuantityOrder, Cur_InOutLine.C_UOM_ID
carlos@0
   566
                )
carlos@0
   567
                ;
carlos@0
   568
            END IF;
carlos@0
   569
            -- Create Asset
carlos@0
   570
            IF(Cur_InOutLine.M_Product_ID IS NOT NULL AND Cur_InOut.IsSOTrx='Y') THEN
antonio@735
   571
              A_ASSET_CREATE(NULL, Cur_InOutLine.M_InOutLine_ID) ;
carlos@0
   572
            END IF;
carlos@0
   573
            v_ResultStr:='UpdateOrderLine';
carlos@0
   574
            IF(Cur_InOutLine.C_OrderLine_ID IS NOT NULL) THEN
carlos@0
   575
              -- stocked product
carlos@0
   576
              IF(Cur_InOutLine.M_Product_ID IS NOT NULL AND v_IsStocked=1) THEN
carlos@0
   577
                -- Update OrderLine (if C-, Qty is negative)
carlos@0
   578
                UPDATE C_ORDERLINE
carlos@0
   579
                  SET QtyReserved=QtyReserved - v_QtyPO - v_QtySO,
carlos@0
   580
                  QtyDelivered=QtyDelivered + v_QtySO,
harikrishnan@7227
   581
                  Updated=now(),
harikrishnan@7227
   582
                  UpdatedBy=v_User
carlos@0
   583
                WHERE C_OrderLine_ID=Cur_InOutLine.C_OrderLine_ID;
carlos@0
   584
                -- Products not stocked
carlos@0
   585
              ELSE
carlos@0
   586
                -- Update OrderLine (if C-, Qty is negative)
carlos@0
   587
                UPDATE C_ORDERLINE
carlos@0
   588
                  SET QtyDelivered=QtyDelivered + v_QtySO,
harikrishnan@7227
   589
                  Updated=now(),
harikrishnan@7227
   590
                  UpdatedBy=v_User
carlos@0
   591
                WHERE C_OrderLine_ID=Cur_InOutLine.C_OrderLine_ID;
carlos@0
   592
              END IF;
carlos@0
   593
            END IF;
carlos@0
   594
            IF(Cur_InOutLine.M_Product_ID IS NOT NULL AND v_IsStocked=1) THEN
carlos@0
   595
              M_Check_Stock(Cur_InOutLine.M_Product_ID, v_AD_Client_ID, v_AD_Org_ID, v_Result, v_Message) ;
carlos@0
   596
              IF v_Result=0 THEN
ioritz@16496
   597
                SELECT name INTO v_ProductName FROM M_Product WHERE M_Product_id = Cur_InOutLine.M_Product_ID;
ioritz@16496
   598
			    RAISE_APPLICATION_ERROR(-20000, v_Message||' '||'@line@'||' '||Cur_InOutLine.line||', '||'@Product@'||' '||v_ProductName) ;
carlos@0
   599
              END IF;
carlos@0
   600
            END IF;
carlos@0
   601
          END LOOP; -- For all InOut Lines
carlos@0
   602
          /*******************
carlos@0
   603
          * PO Matching
carlos@0
   604
          ******************/
carlos@0
   605
          IF(Cur_InOut.IsSOTrx='N') THEN
carlos@0
   606
            DECLARE
carlos@0
   607
              Cur_SLines RECORD;
carlos@0
   608
              Cur_ILines RECORD;
carlos@0
   609
              v_Qty NUMBER;
juanpablo@1605
   610
              v_MatchPO_ID VARCHAR2(32) ;
juanpablo@1605
   611
              v_MatchInv_ID VARCHAR2(32) ;
carlos@0
   612
            BEGIN
carlos@0
   613
              v_ResultStr:='MatchPO';
carlos@0
   614
              FOR Cur_SLines IN
carlos@0
   615
                (SELECT sl.AD_Client_ID,
carlos@0
   616
                  sl.AD_Org_ID,
carlos@0
   617
                  ol.C_OrderLine_ID,
carlos@0
   618
                  sl.M_InOutLine_ID,
carlos@0
   619
                  sl.M_Product_ID,
carlos@0
   620
                  sl.M_AttributeSetInstance_ID,
carlos@0
   621
                  sl.MovementQty,
carlos@0
   622
                  ol.QtyOrdered
carlos@0
   623
                FROM M_INOUTLINE sl,
carlos@0
   624
                  C_ORDERLINE ol
carlos@0
   625
                WHERE sl.C_OrderLine_ID=ol.C_OrderLine_ID
carlos@0
   626
                  AND sl.M_Product_ID=ol.M_Product_ID  --    AND   sl.M_AttributeSetInstance_ID=ol.M_AttributeSetInstance_ID
carlos@0
   627
                  AND sl.M_InOut_ID=Cur_InOut.M_InOut_ID
carlos@0
   628
                )
david@19206
   629
              LOOP          
david@19010
   630
mikel@19201
   631
                v_Qty:=Cur_SLines.MovementQty;
mikel@19201
   632
                --IF (ABS(Cur_SLines.MovementQty) > ABS(Cur_SLines.QtyOrdered)) THEN
mikel@19201
   633
                -- v_Qty := Cur_SLines.QtyOrdered;
mikel@19201
   634
                --END IF;
david@19010
   635
mikel@19201
   636
                  Ad_Sequence_Next('M_MatchPO', Cur_SLines.AD_Org_ID, v_MatchPO_ID) ;
mikel@19201
   637
                  -- The min qty. Modified by Ismael Ciordia
mikel@19201
   638
                  v_ResultStr:='InsertMatchPO ' || v_MatchPO_ID;
mikel@19201
   639
                  INSERT
mikel@19201
   640
                  INTO M_MATCHPO
mikel@19201
   641
                    (
mikel@19201
   642
                      M_MatchPO_ID, AD_Client_ID, AD_Org_ID, IsActive,
mikel@19201
   643
                      Created, CreatedBy, Updated, UpdatedBy,
mikel@19201
   644
                      M_InOutLine_ID, C_OrderLine_ID, M_Product_ID, DateTrx,
mikel@19201
   645
                      Qty, Processing, Processed, Posted
mikel@19201
   646
                    )
mikel@19201
   647
                    VALUES
mikel@19201
   648
                    (
mikel@19201
   649
                      v_MatchPO_ID, Cur_SLines.AD_Client_ID, Cur_SLines.AD_Org_ID, 'Y',
mikel@19201
   650
                      now(), v_User, now(), v_User,
mikel@19201
   651
                      Cur_SLines.M_InOutLine_ID, Cur_SLines.C_OrderLine_ID, Cur_SLines.M_Product_ID, now(),
mikel@19201
   652
                      v_Qty, 'N', 'Y', 'N'
mikel@19201
   653
                    )
mikel@19201
   654
                  ;
mikel@19198
   655
                  
carlos@0
   656
              END LOOP;
carlos@0
   657
              v_ResultStr:='MatchInv';
carlos@0
   658
              FOR Cur_ILines IN
carlos@0
   659
                (SELECT sl.AD_Client_ID,
carlos@0
   660
                  sl.AD_Org_ID,
carlos@0
   661
                  il.C_InvoiceLine_ID,
carlos@0
   662
                  sl.M_InOutLine_ID,
carlos@0
   663
                  sl.M_Product_ID,
carlos@0
   664
                  sl.M_AttributeSetInstance_ID,
carlos@0
   665
                  sl.MovementQty,
david@7233
   666
                  il.QTYINVOICED,
david@7233
   667
                  i.DateAcct
carlos@0
   668
                FROM M_INOUTLINE sl,
david@7233
   669
                  C_INVOICE i,
carlos@0
   670
                  C_INVOICELINE il
carlos@0
   671
                WHERE sl.M_InOutLine_ID=il.M_InOutLine_ID
carlos@0
   672
                  AND sl.M_InOut_ID=Cur_InOut.M_InOut_ID
david@7233
   673
                  AND i.C_INVOICE_ID = il.C_INVOICE_ID
carlos@0
   674
                )
carlos@0
   675
              LOOP
carlos@0
   676
                Ad_Sequence_Next('M_MatchInv', Cur_ILines.AD_Org_ID, v_MatchInv_ID) ;
carlos@0
   677
                -- The min qty. Modified by Ismael Ciordia
carlos@0
   678
                v_Qty:=Cur_ILines.MovementQty;
gorkaion@239
   679
                --IF (ABS(Cur_ILines.MovementQty) > ABS(Cur_ILines.QtyInvoiced)) THEN
carlos@0
   680
                -- v_Qty := Cur_ILines.QtyInvoiced;
carlos@0
   681
                --END IF;
carlos@0
   682
                v_ResultStr:='InsertMatchPO ' || v_MatchPO_ID;
carlos@0
   683
                INSERT
carlos@0
   684
                INTO M_MATCHINV
carlos@0
   685
                  (
carlos@0
   686
                    M_MATCHINV_ID, AD_CLIENT_ID, AD_ORG_ID, ISACTIVE,
carlos@0
   687
                    CREATED, CREATEDBY, UPDATED, UPDATEDBY,
carlos@0
   688
                    M_INOUTLINE_ID, C_INVOICELINE_ID, M_PRODUCT_ID, DATETRX,
carlos@0
   689
                    QTY, PROCESSING, PROCESSED, POSTED
carlos@0
   690
                  )
carlos@0
   691
                  VALUES
carlos@0
   692
                  (
carlos@0
   693
                    v_MatchInv_ID, Cur_ILines.AD_Client_ID, Cur_ILines.AD_Org_ID, 'Y',
harikrishnan@7227
   694
                    now(), v_User, now(), v_User,
david@7233
   695
                    Cur_ILines.M_InOutLine_ID, Cur_ILines.C_InvoiceLine_ID, Cur_ILines.M_Product_ID, Cur_ILines.DateAcct,
carlos@0
   696
                    v_Qty, 'N', 'Y', 'N'
carlos@0
   697
                  )
carlos@0
   698
                  ;
carlos@0
   699
              END LOOP;
carlos@0
   700
            END;
gorkaion@3170
   701
          ELSE
gorkaion@3170
   702
            v_ResultStr:='Check delivery rule for sales orders';
gorkaion@18652
   703
            v_Message_aux:='';
gorkaion@3170
   704
            v_orderid_old:='0';
gorkaion@3170
   705
            FOR Cur_Order IN 
gorkaion@3170
   706
              (SELECT c_order.deliveryrule, m_inoutline.line, c_order.c_order_id,
gorkaion@3170
   707
                      c_order.documentno, c_orderline.line as orderline
gorkaion@3170
   708
               FROM M_InOutLine, C_Orderline, C_Order
gorkaion@3170
   709
               WHERE M_Inoutline.c_orderline_id = c_orderline.c_orderline_id
gorkaion@3170
   710
                 AND c_orderline.c_order_id = c_order.c_order_id
gorkaion@3170
   711
                 AND m_inoutline.m_inout_id = cur_inout.m_inout_id
gorkaion@3170
   712
                 AND ((c_order.deliveryrule = 'O'
gorkaion@3170
   713
                      AND EXISTS (SELECT 1 FROM C_OrderLine ol
gorkaion@3170
   714
                                  WHERE ol.C_Order_ID = C_order.c_order_id
gorkaion@3170
   715
                                    and ol.qtyordered > ol.qtydelivered ))
gorkaion@3170
   716
                      OR (c_order.deliveryrule = 'L' 
gorkaion@3170
   717
                          AND c_orderline.qtyordered > c_orderline.qtydelivered))
gorkaion@3170
   718
               ORDER BY c_order.c_order_id, c_orderline.line) LOOP
gorkaion@3170
   719
              --Order lines not completely delivered with delivery rule O or L
gorkaion@3170
   720
              IF (v_orderid_old <> cur_order.c_order_id OR cur_order.deliveryrule <> 'O' ) THEN
gorkaion@18652
   721
                v_Message_aux := COALESCE(v_Message_aux,'') || '@Shipment@' || ' ' || cur_inout.documentno;
gorkaion@18652
   722
                v_Message_aux := v_Message_aux || ' ' || '@line@' || ' ' || cur_order.line || ': ';
gorkaion@18652
   723
                v_Message_aux := v_Message_aux || '@SalesOrderDocumentno@' || cur_order.documentno;
gorkaion@3170
   724
                IF (cur_order.deliveryrule = 'O') THEN
gorkaion@18652
   725
                  v_Message_aux := v_Message_aux || ' ' || '@notCompleteDeliveryRuleOrder@' || '<br>';
gorkaion@3170
   726
                ELSE
gorkaion@18652
   727
                  v_Message_aux := v_Message_aux || ' ' || '@line@' || ' ' || cur_order.orderline;
gorkaion@18652
   728
                  v_Message_aux := v_Message_aux || ' ' || '@notCompleteDeliveryRuleLine@' || '<br>';
gorkaion@3170
   729
                END IF;
gorkaion@3170
   730
              END IF;
gorkaion@3170
   731
              v_orderid_old := cur_order.c_order_id;
gorkaion@3170
   732
            END LOOP;
gorkaion@18652
   733
            IF (v_Message_aux IS NOT NULL AND v_Message_aux <> '') THEN
gorkaion@18652
   734
              RAISE_APPLICATION_ERROR(-20000, v_Message_aux);
gorkaion@3170
   735
            END IF;
carlos@0
   736
          END IF;
carlos@0
   737
          -- Close Shipment
carlos@0
   738
          v_ResultStr:='CloseShipment';
carlos@0
   739
          UPDATE M_INOUT
carlos@0
   740
            SET Processed='Y',
carlos@0
   741
            DocStatus='CO',
carlos@0
   742
            DocAction='--',
mikel@19150
   743
            Process_Goods_Java='--',
harikrishnan@7227
   744
            Updated=now(),
harikrishnan@7227
   745
            UpdatedBy=v_User
carlos@0
   746
          WHERE M_INOUT.M_INOUT_ID=Cur_InOut.M_INOUT_ID;
carlos@0
   747
          --
asier@1027
   748
          
asier@1027
   749
         
carlos@0
   750
          -- Not Processed + Complete --
carlos@0
   751
          /**
carlos@0
   752
          * Reverse Correction
carlos@0
   753
          */
carlos@0
   754
        ELSIF(Cur_InOut.DocStatus='CO' AND Cur_InOut.DocAction='RC') THEN
javier@17645
   755
          --Check if the m_inoutlines has an invoice lines related. In this case is not possible to void the m_inout.
javier@17645
   756
	  SELECT COUNT(*)
javier@17645
   757
          INTO v_count
javier@17645
   758
          FROM M_INOUTLINE MIOL 
javier@17645
   759
              JOIN C_INVOICELINE CIL ON MIOL.M_INOUTLINE_ID=CIL.M_INOUTLINE_ID 
javier@17645
   760
              JOIN C_INVOICE CI ON CI.C_INVOICE_ID=CIL.C_INVOICE_ID
javier@17645
   761
          WHERE M_INOUT_ID=Cur_InOut.m_inout_id
javier@17645
   762
          AND CI.DOCSTATUS <> 'VO';
javier@17645
   763
          IF (v_count <> 0) THEN
javier@17645
   764
	     RAISE_APPLICATION_ERROR(-20000,'@VoidShipmentWithRelatedInvoice@');
javier@17645
   765
          END IF;
gorkaion@5377
   766
          --Check that there isn't any line with an invoice if the order's 
gorkaion@5377
   767
          --invoice rule is after delivery
gorkaion@5377
   768
          select count(*), max(line) into v_count, v_line
gorkaion@5377
   769
          from (
gorkaion@5377
   770
          SELECT m_inoutline.m_inoutline_id, m_inoutline.line
gorkaion@5377
   771
          from m_inoutline, c_order, c_orderline, c_invoiceline, m_inout, c_invoice
gorkaion@5377
   772
          where m_inoutline.c_orderline_id = c_orderline.c_orderline_id
gorkaion@5377
   773
            and c_orderline.c_order_id = c_order.c_order_id
gorkaion@5377
   774
            and c_orderline.c_orderline_id = c_invoiceline.c_orderline_id
gorkaion@5377
   775
            and m_inoutline.m_inout_id = m_inout.m_inout_id
gorkaion@5377
   776
            and c_invoiceline.c_invoice_id = c_invoice.c_invoice_id
gorkaion@5377
   777
            and m_inout.m_inout_id = Cur_InOut.m_inout_id
gorkaion@5377
   778
            and m_inout.issotrx = 'Y'
gorkaion@5377
   779
            and c_order.invoicerule in ('D', 'O', 'S')
gorkaion@5377
   780
            and c_invoice.processed='Y'
gorkaion@5377
   781
          group by m_inoutline.m_inoutline_id, m_inoutline.line
rafael@5506
   782
          having sum(c_invoiceline.qtyinvoiced) <> 0
gorkaion@5377
   783
          ) a;
gorkaion@5377
   784
          IF (v_count > 0 ) THEN
gorkaion@5377
   785
            v_Message := '@InoutDocumentno@' || ': ' || Cur_InOut.DocumentNo || ' ' || '@line@' || ': ' || v_line || '. ';
gorkaion@5377
   786
            v_Message := v_Message || '@VoidShipmentInvoiced@';
gorkaion@5377
   787
            RAISE_APPLICATION_ERROR(-20000, v_Message);
gorkaion@5377
   788
          END IF;
carlos@0
   789
          v_ResultStr:='CreateInOut';
carlos@0
   790
          SELECT COALESCE(C_DOCTYPE_REVERSED_ID, C_DOCTYPE_ID)
carlos@0
   791
          INTO v_DoctypeReversed_ID
carlos@0
   792
          FROM C_DOCTYPE
carlos@0
   793
          WHERE C_DOCTYPE_ID=Cur_InOut.C_DocType_ID;
carlos@0
   794
          Ad_Sequence_Next('M_InOut', Cur_InOut.M_InOut_ID, v_RInOut_ID) ; -- Get RInOut_ID
carlos@0
   795
          Ad_Sequence_Doctype(v_DoctypeReversed_ID, Cur_InOut.M_InOut_ID, 'Y', v_RDocumentNo) ; -- Get RDocumentNo
carlos@0
   796
          IF(v_RDocumentNo IS NULL) THEN
carlos@0
   797
            AD_Sequence_Doc('DocumentNo_M_InOut', Cur_InOut.AD_Client_ID, 'Y', v_RDocumentNo) ;
carlos@0
   798
          END IF;
carlos@0
   799
          -- Indicate that it is invoiced (i.e. not printed on invoices)
carlos@0
   800
          v_ResultStr:='SetInvoiced';
harikrishnan@7227
   801
          UPDATE M_INOUTLINE  SET IsInvoiced='Y',Updated=now(),UpdatedBy=v_User  WHERE M_InOut_ID=Cur_InOut.M_InOut_ID;
carlos@0
   802
          --
carlos@0
   803
          DBMS_OUTPUT.PUT_LINE('Reverse InOut_ID=' || v_RInOut_ID || ' DocumentNo=' || v_RDocumentNo) ;
carlos@0
   804
          v_ResultStr:='InsertInOut Reverse ' || v_RInOut_ID;
carlos@0
   805
          INSERT
carlos@0
   806
          INTO M_INOUT
carlos@0
   807
            (
carlos@0
   808
              M_InOut_ID, C_Order_ID, IsSOTrx, AD_Client_ID,
carlos@0
   809
              AD_Org_ID, IsActive, Created, CreatedBy,
carlos@0
   810
              Updated, UpdatedBy, DocumentNo, C_DocType_ID,
carlos@0
   811
              Description, IsPrinted, MovementType, MovementDate,
carlos@0
   812
              DateAcct, C_BPartner_ID, C_BPartner_Location_ID, AD_User_ID,
carlos@0
   813
              M_Warehouse_ID, POReference, DateOrdered, DeliveryRule,
carlos@0
   814
              FreightCostRule, FreightAmt, C_Project_ID, C_Activity_ID,
carlos@0
   815
              C_Campaign_ID, AD_OrgTrx_ID, User1_ID, User2_ID,
eduardo@18857
   816
              C_Costcenter_ID, A_Asset_ID,
carlos@0
   817
              DeliveryViaRule, M_Shipper_ID, C_Charge_ID, ChargeAmt,
carlos@0
   818
              PriorityRule, DocStatus, DocAction, Processing,
mikel@19150
   819
              Processed, ISLOGISTIC, salesrep_id, Process_Goods_Java
carlos@0
   820
            )
carlos@0
   821
            VALUES
carlos@0
   822
            (
carlos@0
   823
              v_RInOut_ID, Cur_InOut.C_Order_ID, Cur_InOut.IsSOTrx, Cur_InOut.AD_Client_ID,
harikrishnan@7227
   824
              Cur_InOut.AD_Org_ID, 'Y', now(), v_User,
harikrishnan@7227
   825
              now(), v_User, v_RDocumentNo, v_DoctypeReversed_ID,
unai@15382
   826
               '(*R*: ' || Cur_InOut.DocumentNo || ') ' || COALESCE(TO_CHAR(Cur_InOut.Description), ''), 'N', Cur_InOut.MovementType, Cur_InOut.MovementDate,
carlos@0
   827
              Cur_InOut.DateAcct, Cur_InOut.C_BPartner_ID, Cur_InOut.C_BPartner_Location_ID, Cur_InOut.AD_User_ID,
carlos@0
   828
              Cur_InOut.M_Warehouse_ID, Cur_InOut.POReference, Cur_InOut.DateOrdered, Cur_InOut.DeliveryRule,
carlos@0
   829
              Cur_InOut.FreightCostRule, Cur_InOut.FreightAmt * -1, Cur_InOut.C_Project_ID, Cur_InOut.C_Activity_ID,
carlos@0
   830
              Cur_InOut.C_Campaign_ID, Cur_InOut.AD_OrgTrx_ID, Cur_InOut.User1_ID, Cur_InOut.User2_ID,
eduardo@18857
   831
              Cur_InOut.C_Costcenter_ID, Cur_InOut.A_Asset_ID,
carlos@0
   832
              Cur_InOut.DeliveryViaRule, Cur_InOut.M_Shipper_ID, Cur_InOut.C_Charge_ID, Cur_InOut.ChargeAmt * -1,
carlos@0
   833
              Cur_InOut.PriorityRule, 'DR', 'CO', 'N',
mikel@19150
   834
               'N', Cur_InOut.islogistic, Cur_InOut.salesrep_id, 'CO'
carlos@0
   835
            )
carlos@0
   836
            ;
carlos@0
   837
          v_ResultStr:='InsertInOutLine';
carlos@0
   838
          FOR Cur_InOutLine IN
carlos@0
   839
            (SELECT *
carlos@0
   840
            FROM M_INOUTLINE
carlos@0
   841
            WHERE M_InOut_ID=Cur_InOut.M_InOut_ID
carlos@0
   842
              AND IsActive='Y'  FOR UPDATE
carlos@0
   843
            )
carlos@0
   844
          LOOP
carlos@0
   845
            -- Create InOut Line
carlos@0
   846
            Ad_Sequence_Next('M_InOutLine', Cur_InOut.M_InOut_ID, v_NextNo) ;
carlos@0
   847
            v_ResultStr:='CreateInOutLine';
carlos@0
   848
            INSERT
carlos@0
   849
            INTO M_INOUTLINE
carlos@0
   850
              (
carlos@0
   851
                M_InOutLine_ID, Line, M_InOut_ID, C_OrderLine_ID,
carlos@0
   852
                AD_Client_ID, AD_Org_ID, IsActive, Created,
carlos@0
   853
                CreatedBy, Updated, UpdatedBy, M_Product_ID,
carlos@0
   854
                M_AttributeSetInstance_ID, C_UOM_ID, M_Locator_ID, MovementQty,
asier@799
   855
                Description, IsInvoiced,  --MODIFIED BY F.IRIAZABAL
gorkaion@15487
   856
                QuantityOrder, M_Product_UOM_ID, IsDescription,
eduardo@18857
   857
                canceled_inoutline_id, A_Asset_ID, C_Project_ID, C_BPartner_ID,
naiara@19828
   858
                User1_ID, User2_ID, C_CostCenter_ID, 
naiara@19828
   859
                explode
carlos@0
   860
              )
carlos@0
   861
              VALUES
carlos@0
   862
              (
carlos@0
   863
                v_NextNo, Cur_InOutLine.Line, v_RInOut_ID, Cur_InOutLine.C_OrderLine_ID,
carlos@0
   864
                Cur_InOut.AD_Client_ID, Cur_InOut.AD_Org_ID, 'Y', now(),
harikrishnan@7227
   865
                v_User, now(), v_User, Cur_InOutLine.M_Product_ID,
carlos@0
   866
                Cur_InOutLine.M_AttributeSetInstance_ID, Cur_InOutLine.C_UOM_ID, Cur_InOutLine.M_Locator_ID, Cur_InOutLine.MovementQty * -1,
unai@15382
   867
                 '*R*: ' || COALESCE(TO_CHAR(Cur_InOutLine.Description), ''), Cur_InOutLine.IsInvoiced, --MODIFIED BY F.IRIAZABAL
gorkaion@15487
   868
                Cur_InOutLine.QuantityOrder * -1, Cur_InOutLine.M_PRODUCT_UOM_ID, Cur_InOutLine.IsDescription,
eduardo@18857
   869
                Cur_InOutLine.M_InOutLine_ID, Cur_InOutLine.A_Asset_ID, Cur_InOutLine.C_Project_ID, Cur_InOutLine.C_BPartner_ID,
naiara@19828
   870
                Cur_InOutLine.User1_ID, Cur_InOutLine.User2_ID, Cur_InOutLine.C_CostCenter_ID, 
naiara@19828
   871
                Cur_InOutLine.explode 
carlos@0
   872
              )
carlos@0
   873
              ;
david@7235
   874
            INSERT INTO M_MATCHINV
david@7235
   875
              (M_MATCHINV_ID, AD_CLIENT_ID, AD_ORG_ID, ISACTIVE, CREATED, CREATEDBY, UPDATED, UPDATEDBY,
david@7235
   876
              M_INOUTLINE_ID, C_INVOICELINE_ID, M_PRODUCT_ID, DATETRX, QTY, PROCESSING, PROCESSED, POSTED)
david@7235
   877
            SELECT
david@7235
   878
              GET_UUID(), MI.AD_CLIENT_ID, MI.AD_ORG_ID, MI.ISACTIVE, now(), '0', now(), '0',
david@7240
   879
              v_NextNo, MI.C_INVOICELINE_ID, MI.M_PRODUCT_ID, MI.DATETRX, -MI.QTY, 'N', 'Y', 'N'
david@7235
   880
            FROM M_MATCHINV MI
david@7235
   881
            WHERE MI.M_INOUTLINE_ID = Cur_InOutLine.M_InOutLine_ID;
gorkaion@19377
   882
carlos@0
   883
          END LOOP;
carlos@0
   884
          -- Close Order
carlos@0
   885
          v_ResultStr:='CloseInOut';
carlos@0
   886
          UPDATE M_INOUT
carlos@0
   887
            SET Description=COALESCE(TO_CHAR(Description), '') || ' (*R*=' || v_RDocumentNo || ')',
carlos@0
   888
            Processed='Y',
ainhoa@2454
   889
            DocStatus='VO', -- it IS reversed
carlos@0
   890
            DocAction='--',
mikel@19150
   891
            Process_Goods_Java='--',
carlos@0
   892
            Updated=now(),
carlos@0
   893
            UpdatedBy=v_User
carlos@0
   894
          WHERE M_INOUT.M_INOUT_ID=Cur_InOut.M_INOUT_ID;
unai@15382
   895
unai@15382
   896
	   FOR Cur_InOutLine IN
unai@15382
   897
            (SELECT *
unai@15382
   898
            FROM M_INOUTLINE
unai@15382
   899
            WHERE M_InOut_ID=Cur_InOut.M_InOut_ID
unai@15382
   900
              AND IsActive='Y'  FOR UPDATE
unai@15382
   901
            )
unai@15382
   902
	  LOOP
unai@15382
   903
            UPDATE M_INOUTLINE
unai@15382
   904
              SET Description=COALESCE(TO_CHAR(Cur_InOutLine.Description), '') || ' : *R*',
unai@15382
   905
              Updated=now(),
unai@15382
   906
              UpdatedBy=v_User
unai@15382
   907
            WHERE M_INOUTLINE.M_INOUTLINE_ID=Cur_InOutLine.M_INOUTLINE_ID;
unai@15382
   908
          END LOOP;
unai@15382
   909
          
carlos@0
   910
          -- Post Reversal
carlos@0
   911
          v_ResultStr:='PostReversal';
mikel@19131
   912
          -- Update reversal goods dates
mikel@19131
   913
          IF (v_voidmovementdate IS NOT NULL) THEN
mikel@19131
   914
            UPDATE M_INOUT SET MovementDate = v_voidmovementdate WHERE M_INOUT_ID = v_RInOut_ID;
mikel@19131
   915
          END IF;
mikel@19131
   916
          IF (v_voiddate_acct IS NOT NULL) THEN
mikel@19131
   917
            UPDATE M_INOUT SET DateAcct = v_voiddate_acct WHERE M_INOUT_ID = v_RInOut_ID;
mikel@19131
   918
          END IF;
antonio@735
   919
          M_INOUT_POST(NULL, v_RInOut_ID) ;
carlos@0
   920
          -- Indicate as Reversal Transaction
carlos@0
   921
          v_ResultStr:='IndicateReversal';
carlos@0
   922
          UPDATE M_INOUT
carlos@0
   923
            SET Updated=now(),
carlos@0
   924
            UpdatedBy=v_User,
ainhoa@2454
   925
            DocStatus='VO' -- the reversal transaction
carlos@0
   926
          WHERE M_InOut_ID=v_RInOut_ID;
carlos@0
   927
        END IF; -- ReverseCorrection
adrianromero@7702
   928
adrianromero@7702
   929
        --M_Inout_Post - Finish_Process Extension Point
adrianromero@7702
   930
        --Extension point at the end of the M_Inout_Post. It has 5 available parameters Record_ID, DocAction, User, Message and Result
adrianromero@7702
   931
        SELECT count(*) INTO v_count
adrianromero@7702
   932
        FROM DUAL
adrianromero@7702
   933
        where exists (select 1 from ad_ep_procedures where ad_extension_points_id = '5A7C6972321E42C2A5A8E9D6D73E6A7C');
adrianromero@7702
   934
        IF (v_count=1) THEN
adrianromero@7702
   935
          DECLARE
adrianromero@7702
   936
            v_ep_instance VARCHAR2(32);
adrianromero@7702
   937
            v_extension_point_id VARCHAR2(32) := '5A7C6972321E42C2A5A8E9D6D73E6A7C';
adrianromero@7702
   938
          BEGIN
adrianromero@7702
   939
            v_ep_instance := get_uuid();
adrianromero@7702
   940
            AD_EP_INSTANCE_PARA_INSERT(v_ep_instance, v_extension_point_id, 'Record_ID',
adrianromero@7702
   941
              v_record_id, NULL, NULL, NULL, NULL, NULL, NULL);
adrianromero@7702
   942
            AD_EP_INSTANCE_PARA_INSERT(v_ep_instance, v_extension_point_id, 'DocAction',
adrianromero@7702
   943
              Cur_InOut.DocAction, NULL, NULL, NULL, NULL, NULL, NULL);
adrianromero@7702
   944
            AD_EP_INSTANCE_PARA_INSERT(v_ep_instance, v_extension_point_id, 'User',
adrianromero@7702
   945
              v_User, NULL, NULL, NULL, NULL, NULL, NULL);
adrianromero@7702
   946
            AD_EP_INSTANCE_PARA_INSERT(v_ep_instance, v_extension_point_id, 'Message',
adrianromero@7702
   947
              NULL, NULL, NULL, NULL, NULL, NULL, v_Message);
adrianromero@7702
   948
            AD_EP_INSTANCE_PARA_INSERT(v_ep_instance, v_extension_point_id, 'Result',
adrianromero@7702
   949
              NULL, NULL, v_Result, NULL, NULL, NULL, NULL);
adrianromero@7702
   950
            AD_EXTENSION_POINT_HANDLER(v_ep_instance, v_extension_point_id);
adrianromero@7702
   951
            SELECT p_number INTO v_Result
adrianromero@7702
   952
            FROM ad_ep_instance_para
adrianromero@7702
   953
            WHERE ad_ep_instance_id = v_ep_instance
adrianromero@7702
   954
              AND parametername LIKE 'Result';
adrianromero@7702
   955
            SELECT p_text INTO v_Message
adrianromero@7702
   956
            FROM ad_ep_instance_para
adrianromero@7702
   957
            WHERE ad_ep_instance_id = v_ep_instance
adrianromero@7702
   958
              AND parametername LIKE 'Message';
adrianromero@7702
   959
adrianromero@7702
   960
            DELETE FROM ad_ep_instance_para
adrianromero@7702
   961
            WHERE ad_ep_instance_id = v_ep_instance;
adrianromero@7702
   962
          END;
adrianromero@7702
   963
        END IF;
adrianromero@7702
   964
carlos@0
   965
      END LOOP; -- InOut Header
carlos@0
   966
      /**
carlos@0
   967
      * Transaction End
carlos@0
   968
      */
carlos@0
   969
      v_ResultStr:='Fini';
carlos@0
   970
    END IF; --FINISH_PROCESS
gorkaion@239
   971
    --<<FINISH_PROCESS>>
carlos@0
   972
    IF(p_PInstance_ID IS NOT NULL) THEN
carlos@0
   973
      --  Update AD_PInstance
carlos@0
   974
      DBMS_OUTPUT.PUT_LINE('Updating PInstance - Finished ' || v_Message) ;
carlos@0
   975
      AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', v_Result, v_Message) ;
carlos@0
   976
    ELSE
gorkaion@239
   977
      DBMS_OUTPUT.PUT_LINE('--<<M_InOut_Post finished>>') ;
carlos@0
   978
    END IF;
carlos@0
   979
    RETURN;
carlos@0
   980
  END; --BODY
carlos@0
   981
EXCEPTION
carlos@0
   982
WHEN OTHERS THEN
carlos@0
   983
  v_ResultStr:= '@ERROR=' || SQLERRM;
carlos@0
   984
  DBMS_OUTPUT.PUT_LINE(v_ResultStr) ;
carlos@0
   985
  IF(p_PInstance_ID IS NOT NULL) THEN
carlos@0
   986
    ROLLBACK;
carlos@0
   987
    AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', 0, v_ResultStr) ;
carlos@0
   988
  ELSE
carlos@0
   989
    RAISE;
carlos@0
   990
  END IF;
carlos@0
   991
  RETURN;
antonio@735
   992
END M_INOUT_POST
juanpablo@3490
   993
]]></body>
juanpablo@3490
   994
    </function>
juanpablo@3490
   995
  </database>