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