src-db/database/model/functions/M_INOUT_POST.xml
author David Miguelez <david.miguelez@openbravo.com>
Fri, 21 Aug 2015 13:33:27 +0200
changeset 27385 1d9e795478ba
parent 27384 f8dc8ea21e39
child 27386 6db6e5448fd2
permissions -rw-r--r--
Related to Issue 30556. Code Review Changes.
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
alvaro@26193
    25
  * Contributions are Copyright (C) 2001-2015 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;
atul@21884
    60
  v_DateDelivered C_OrderLine.DateDelivered%TYPE;
victor@3065
    61
  v_isacctle AD_OrgType.IsAcctLegalEntity%TYPE;
victor@3065
    62
  v_org_bule_id AD_Org.AD_Org_ID%TYPE;
carlos@0
    63
  -- Parameter
carlos@0
    64
  TYPE RECORD IS REF CURSOR;
carlos@0
    65
    Cur_Parameter RECORD;
carlos@0
    66
    --
carlos@0
    67
    Cur_InOut RECORD;
carlos@0
    68
    Cur_InOutLine RECORD;
gorkaion@3170
    69
    Cur_Order RECORD;
pandeeswari@21778
    70
    Cur_OrderLine RECORD;
gaurav@22036
    71
    Cur_Lines RECORD;
alvaro@27102
    72
    Cur_Reservation RECORD;
carlos@0
    73
    --
carlos@0
    74
    v_Result NUMBER:=1;
juanpablo@1605
    75
    v_AD_Org_ID VARCHAR2(32);
alvaro@27384
    76
    v_Locator_Org_ID VARCHAR2(32);
pandeeswari@21373
    77
    v_Warehouse_Org VARCHAR2(32);
juanpablo@1605
    78
    v_AD_Client_ID VARCHAR2(32);
juanpablo@1605
    79
    v_NextNo VARCHAR2(32);
carlos@0
    80
    v_Qty NUMBER;
carlos@0
    81
    v_QtyPO NUMBER;
carlos@0
    82
    v_QtySO NUMBER;
carlos@0
    83
    v_QuantityOrder NUMBER;
carlos@0
    84
    v_QuantityOrderPO NUMBER;
carlos@0
    85
    v_QuantityOrderSO NUMBER;
carlos@0
    86
    v_RDocumentNo VARCHAR2(40) ;
juanpablo@1605
    87
    v_RInOut_ID VARCHAR2(32);
carlos@0
    88
    v_IsStocked NUMBER;
juanpablo@1605
    89
    v_DoctypeReversed_ID VARCHAR2(32);
carlos@0
    90
    --MODIFIED BY F.IRIAZABAL
carlos@0
    91
    v_QtyOrder NUMBER;
carlos@0
    92
    v_ProductUOM NUMBER;
asier@2586
    93
    v_BreakDown VARCHAR2(60) ;
carlos@0
    94
    v_ActualQty NUMBER;
carlos@0
    95
    v_QtyAux NUMBER;
carlos@0
    96
    v_Count NUMBER:=0;
carlos@0
    97
    v_Line VARCHAR2(10) ;
gorkaion@3170
    98
    v_OrderID_old VARCHAR2(32);
harikrishnan@7945
    99
    Cur_MILines RECORD;
carlos@0
   100
    FINISH_PROCESS BOOLEAN:=false;
harikrishnan@5580
   101
    v_Aux NUMBER;
pandeeswari@21340
   102
    v_isSoTrx CHAR(1);
ioritz@17861
   103
    v_ProductName M_Product.name%TYPE;
gorkaion@18652
   104
    v_reservation_id    VARCHAR2(32);
pandeeswari@21353
   105
    v_M_Warehouse_ID    VARCHAR2(32);
mikel@19131
   106
    v_voidmovementdate M_Inout.MovementDate%TYPE;
mikel@19131
   107
    v_voiddate_acct M_Inout.DateAcct%TYPE;
david@19194
   108
    v_bpartner_blocked VARCHAR2(1):='N';
david@19194
   109
    v_goods_blocked VARCHAR2(1):='N';
sandra@20002
   110
    v_bpartner_name c_bpartner.name%TYPE;
david@19194
   111
    v_DocAction VARCHAR2(60);
gaurav@21852
   112
    v_voiddoccount NUMBER:=0;
mikel@19131
   113
david@22931
   114
    v_penqty NUMBER;
david@22931
   115
    v_qtysumorders NUMBER;
david@22931
   116
    v_released  NUMBER;
fernando@25185
   117
    
fernando@25185
   118
    v_bp_isactive c_bpartner.isactive%Type;
atul@25018
   119
    v_IsQtyVariable M_Product.IsQuantityVariable%TYPE;
atul@25116
   120
    v_IsReversedDoc CHAR(1);
atul@25018
   121
alvaro@27102
   122
    v_uuid VARCHAR2(32) := get_uuid();
alvaro@27102
   123
    v_qtyordered NUMBER;
alvaro@27102
   124
carlos@0
   125
  BEGIN
david@22931
   126
  
carlos@0
   127
    IF(p_PInstance_ID IS NOT NULL) THEN
carlos@0
   128
      --  Update AD_PInstance
carlos@0
   129
      DBMS_OUTPUT.PUT_LINE('Updating PInstance - Processing ' || p_PInstance_ID) ;
carlos@0
   130
      v_ResultStr:='PInstanceNotFound';
carlos@0
   131
      AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'Y', NULL, NULL) ;
carlos@0
   132
      -- Get Parameters
carlos@0
   133
      v_ResultStr:='ReadingParameters';
carlos@0
   134
      FOR Cur_Parameter IN
carlos@0
   135
        (SELECT i.Record_ID,
carlos@0
   136
          i.AD_User_ID,
carlos@0
   137
          p.ParameterName,
carlos@0
   138
          p.P_String,
carlos@0
   139
          p.P_Number,
carlos@0
   140
          p.P_Date
carlos@0
   141
        FROM AD_PInstance i
carlos@0
   142
        LEFT JOIN AD_PInstance_Para p
carlos@0
   143
          ON i.AD_PInstance_ID=p.AD_PInstance_ID
carlos@0
   144
        WHERE i.AD_PInstance_ID=p_PInstance_ID
carlos@0
   145
        ORDER BY p.SeqNo
carlos@0
   146
        )
carlos@0
   147
      LOOP
carlos@0
   148
        v_Record_ID:=Cur_Parameter.Record_ID;
carlos@0
   149
        v_User:=Cur_Parameter.AD_User_ID;
mikel@19131
   150
        IF (Cur_Parameter.parametername = 'voidedDocumentDate') THEN
mikel@19131
   151
          v_voidmovementdate := TO_DATE(Cur_Parameter.p_string, 'YYYY-MM-DD');
mikel@19131
   152
        ELSIF (Cur_Parameter.parametername = 'voidedDocumentAcctDate') THEN
mikel@19131
   153
          v_voiddate_acct := TO_DATE(Cur_Parameter.p_string, 'YYYY-MM-DD');
mikel@19131
   154
        END IF;
carlos@0
   155
      END LOOP; -- Get Parameter
carlos@0
   156
      DBMS_OUTPUT.PUT_LINE('  Record_ID=' || v_Record_ID) ;
carlos@0
   157
    ELSE
gorkaion@239
   158
      DBMS_OUTPUT.PUT_LINE('--<<M_InOut_Post>>') ;
carlos@0
   159
      v_Record_ID:=p_InOut_ID;
harikrishnan@7227
   160
      SELECT count(*),updatedby
harikrishnan@7227
   161
        INTO v_Count,v_User
rafael@5825
   162
        FROM M_InOut
harikrishnan@7227
   163
        WHERE M_InOut_ID=v_Record_ID
harikrishnan@7227
   164
        GROUP BY updatedby;
rafael@5825
   165
      IF v_Count=0 THEN
rafael@5825
   166
        FINISH_PROCESS:=true;
rafael@5825
   167
      END IF;
carlos@0
   168
    END IF;
fernando@25185
   169
    
carlos@0
   170
  BEGIN --BODY
rafael@5825
   171
  	IF(NOT FINISH_PROCESS) THEN
harikrishnan@7227
   172
  	  v_PUser:=v_User;
gaurav@21852
   173
      SELECT io.AD_Client_ID, io.AD_Org_ID, io.CreatedBy, io.C_DocType_ID, io.DateAcct, dt.isreturn, io.m_warehouse_id, io.issotrx,docaction
gaurav@21852
   174
        INTO v_AD_Client_ID, v_AD_Org_ID, v_User, v_DocType_ID, v_DateAcct, v_isreturndoctype, v_M_Warehouse_ID, v_isSoTrx,v_DocAction
gorkaion@14547
   175
        FROM M_InOut io JOIN c_doctype dt ON io.c_doctype_id = dt.c_doctype_id
gorkaion@14547
   176
        WHERE io.M_InOut_ID=v_Record_ID;
harikrishnan@7227
   177
        IF(v_PUser IS NOT NULL) THEN
harikrishnan@7227
   178
        	v_User:=v_PUser;
harikrishnan@7227
   179
        END IF;
carlos@0
   180
      SELECT count(*)
carlos@0
   181
      INTO v_Count
rafael@5825
   182
      FROM AD_CLIENTINFO
rafael@5825
   183
      WHERE AD_CLIENT_ID=v_AD_Client_ID
rafael@5825
   184
        AND CHECKINOUTORG='Y';
naiara@19828
   185
      IF (v_Count>0) THEN
rafael@5825
   186
        v_ResultStr:='CheckingRestrictions - M_INOUT ORG IS IN C_BPARTNER ORG TREE';
rafael@5825
   187
        SELECT count(*)
rafael@5825
   188
        INTO v_Count
rafael@5825
   189
        FROM M_InOut m,
rafael@5825
   190
          C_BPartner bp
rafael@5825
   191
        WHERE m.M_InOut_ID=v_Record_ID
rafael@5825
   192
          AND m.C_BPARTNER_ID=bp.C_BPARTNER_ID
rafael@5825
   193
          AND AD_IsOrgIncluded(m.AD_ORG_ID, bp.AD_ORG_ID, bp.AD_CLIENT_ID)=-1;
rafael@5825
   194
        IF v_Count>0 THEN
rafael@5825
   195
          RAISE_APPLICATION_ERROR(-20000, '@NotCorrectOrgBpartnerInout@') ;
rafael@5825
   196
        END IF;
carlos@0
   197
      END IF;
gaurav@22036
   198
       Declare
gaurav@22036
   199
       v_Message_product VARCHAR(2000) :='';
gaurav@22036
   200
       Begin
gaurav@22036
   201
          FOR Cur_Lines IN (
gaurav@22036
   202
                             SELECT M.line
gaurav@22036
   203
                             FROM M_InOut I,
gaurav@22036
   204
                                  M_InOutLine M
gaurav@22036
   205
                             WHERE I.M_InOut_ID=M.M_InOut_ID
gaurav@22036
   206
                               AND I.M_INOUT_ID=v_Record_ID
gaurav@22036
   207
                               AND (M.M_PRODUCT_ID IS NULL AND M.MOVEMENTQTY <> 0)
gaurav@22036
   208
                               ORDER BY M.line
gaurav@22036
   209
                            ) LOOP
gaurav@22036
   210
               v_Message_product:=v_Message_product||Cur_Lines.line||', ';
gaurav@22036
   211
          END LOOP;
gaurav@22036
   212
          if v_Message_product != '' then
gaurav@22036
   213
             RAISE_APPLICATION_ERROR(-20000, '@Inline@ '||v_Message_product||' '||'@ProductNullAndMovementQtyGreaterZero@') ;
gaurav@22036
   214
          end if ;
gaurav@22036
   215
	     End ;
atul@23544
   216
	     Declare
atul@23544
   217
       v_Message_Qty VARCHAR(2000) :='';
atul@23544
   218
	     BEGIN
atul@23544
   219
					FOR Cur_Lines IN (
atul@23544
   220
                             SELECT M.line
atul@23544
   221
                             FROM M_InOut I,
atul@23544
   222
                                  M_InOutLine M
atul@23544
   223
                             WHERE I.M_InOut_ID=M.M_InOut_ID
atul@23544
   224
                               AND I.M_INOUT_ID=v_Record_ID
atul@23544
   225
                               AND (M.M_PRODUCT_ID IS NOT NULL AND M.MOVEMENTQTY = 0)
atul@23544
   226
                               ORDER BY M.line
atul@23544
   227
                            ) LOOP
atul@23544
   228
          v_Message_Qty:=v_Message_Qty||Cur_Lines.line||', ';
atul@23544
   229
          END LOOP;
atul@23544
   230
          IF v_Message_Qty != '' THEN
atul@23544
   231
             RAISE_APPLICATION_ERROR(-20000, '@Inline@ '||v_Message_Qty||' '||'@ProductNotNullAndMovementQtyZero@') ;
atul@23544
   232
          END IF;
atul@23544
   233
	     End ;
atul@25116
   234
      SELECT CASE WHEN COALESCE(instr(M_INOUT.Description,'*R*:'),0) = 0 THEN 'N' ELSE 'Y' END
atul@25116
   235
            INTO v_IsReversedDoc
atul@25116
   236
            FROM M_INOUT
atul@25116
   237
            WHERE M_INOUT.M_INOUT_id = v_Record_ID;
atul@25116
   238
      -- Skip MovementQtyCheck when it is reversed document
atul@25116
   239
      if(v_isreturndoctype = 'N' AND v_isSoTrx = 'Y' and v_DocAction<>'RC' AND v_IsReversedDoc='N') then
pandeeswari@21778
   240
        v_message := null;
pandeeswari@21778
   241
        for Cur_OrderLine in (
atul@25018
   242
          select c_orderline_id, line, m_product_id
pandeeswari@21778
   243
          from m_inoutline
pandeeswari@21778
   244
          where m_inout_id = v_Record_ID
gaurav@22036
   245
          and c_orderline_id is not null
pandeeswari@21778
   246
          order by line
pandeeswari@21778
   247
        ) loop
atul@25116
   248
          select COALESCE(sum(ABS(movementqty)), 0)
pandeeswari@21778
   249
          into v_qty
pandeeswari@21778
   250
          from m_inoutline
pandeeswari@21778
   251
          where m_inout_id = v_Record_ID
pandeeswari@21778
   252
          and c_orderline_id = Cur_OrderLine.c_orderline_id;
pandeeswari@21778
   253
atul@25116
   254
          select ABS(qtyordered), (coalesce(ABS(qtydelivered), 0) + v_qty)
pandeeswari@21778
   255
          into v_QuantityOrder, v_qty
pandeeswari@21778
   256
          from c_orderline
pandeeswari@21778
   257
          where c_orderline_id = Cur_OrderLine.c_orderline_id;
pandeeswari@21778
   258
atul@25018
   259
          SELECT IsQuantityVariable INTO v_IsQtyVariable
atul@25018
   260
          FROM M_Product
atul@25018
   261
          WHERE M_Product_ID = Cur_OrderLine.M_Product_ID;
atul@25018
   262
atul@25018
   263
          IF(v_IsQtyVariable='N') THEN
atul@25018
   264
            if(v_QuantityOrder < v_qty) then
atul@25018
   265
              if v_message is null THEN
atul@25018
   266
                v_message := Cur_OrderLine.line;
atul@25018
   267
              ELSE
atul@25018
   268
                v_message := v_message || ', ' || Cur_OrderLine.line;
atul@25018
   269
              END IF;
atul@25018
   270
            end if;
atul@25018
   271
          END IF;
pandeeswari@21778
   272
        end loop;
pandeeswari@21778
   273
        if v_message is not null then
pandeeswari@21778
   274
          RAISE_APPLICATION_ERROR(-20000, '@MovementQtyCheck@'||' '||'@Inlines@'||' '||v_message);
pandeeswari@21778
   275
        end if;
pandeeswari@21778
   276
      end if;
fernando@25185
   277
    
fernando@25185
   278
    
fernando@25185
   279
    /* Check active business partner*/
fernando@25185
   280
      select bp.isactive into v_bp_isactive
fernando@25185
   281
      from m_inout io 
fernando@25185
   282
      left join c_bpartner bp on io.C_BPARTNER_ID = bp.C_BPARTNER_ID
fernando@25185
   283
      where io.M_INOUT_ID = v_Record_ID;
fernando@25185
   284
fernando@25185
   285
      IF(v_bp_isactive = 'N') THEN
fernando@25185
   286
        RAISE_APPLICATION_ERROR(-20000, '@InActiveBusinessPartner@');
fernando@25185
   287
      END IF;
pandeeswari@21778
   288
pandeeswari@21353
   289
      --Check whether warehouse belongs to the organization.
pandeeswari@21353
   290
      SELECT count(AD_ORG_ID)
pandeeswari@21353
   291
      INTO v_count
pandeeswari@21353
   292
      FROM AD_Org_Warehouse
pandeeswari@21353
   293
      WHERE M_Warehouse_ID=v_M_Warehouse_ID
pandeeswari@21353
   294
      AND AD_Org_ID = v_AD_Org_ID;
pandeeswari@21353
   295
pandeeswari@21340
   296
      IF v_count = 0 AND v_isSoTrx = 'Y' THEN
pandeeswari@21353
   297
        RAISE_APPLICATION_ERROR(-20000,'@WrongWarehouse@');
pandeeswari@21353
   298
      END IF;
pandeeswari@21353
   299
pandeeswari@21373
   300
      SELECT AD_Org_ID
pandeeswari@21373
   301
      INTO v_Warehouse_Org
pandeeswari@21373
   302
      FROM M_Warehouse
pandeeswari@21373
   303
      WHERE M_Warehouse_ID = v_M_Warehouse_ID;
pandeeswari@21373
   304
pandeeswari@21373
   305
      IF(ad_org_isinnaturaltree(v_Warehouse_Org, v_AD_Org_ID, v_AD_Client_ID) = 'N' AND v_isSoTrx = 'N') THEN
pandeeswari@21373
   306
        RAISE_APPLICATION_ERROR(-20000,'@WrongWarehouse@');
pandeeswari@21373
   307
      END IF;
pandeeswari@21373
   308
david@19194
   309
      SELECT CASE WHEN (m.ISSOTRX='Y') THEN customer_blocking ELSE vendor_blocking END, CASE WHEN (m.ISSOTRX='Y') 
david@19194
   310
      THEN so_goods_blocking ELSE po_goods_blocking END, name, DocAction
david@19194
   311
      INTO v_bpartner_blocked, v_goods_blocked, v_bpartner_name, v_DocAction
david@19194
   312
      FROM M_InOut m, C_BPartner bp
david@19194
   313
      WHERE m.c_bpartner_id = bp.c_bpartner_id
david@19194
   314
      AND m.M_InOut_ID=v_Record_ID
david@19194
   315
      AND m.C_BPARTNER_ID=bp.C_BPARTNER_ID;
david@19194
   316
      
david@19194
   317
      IF (v_DocAction = 'CO' AND v_bpartner_blocked = 'Y' AND v_goods_blocked = 'Y' AND v_isreturndoctype='N') THEN
david@19194
   318
        RAISE_APPLICATION_ERROR(-20000, '@ThebusinessPartner@'||' '|| v_bpartner_name ||' '||'@BusinessPartnerBlocked@');
david@19194
   319
      END IF;
david@19194
   320
      
rafael@5825
   321
     v_ResultStr:='CheckingRestrictions';
rafael@5825
   322
     SELECT COUNT(*)
rafael@5825
   323
     INTO v_Count
rafael@5825
   324
     FROM C_DocType,
rafael@5825
   325
          M_InOut M
rafael@5825
   326
     WHERE M_Inout_ID = v_Record_ID
rafael@5825
   327
       AND C_DocType.DocBaseType IN ('MMR', 'MMS')
rafael@5825
   328
      AND C_DocType.IsSOTrx=M.IsSOTrx
rafael@5825
   329
      AND AD_ISORGINCLUDED(m.AD_Org_ID,C_DocType.AD_Org_ID, m.AD_Client_ID) <> -1
rafael@5825
   330
       AND M.C_DOCTYPE_ID=C_DocType.C_DOCTYPE_ID;
naiara@19828
   331
        IF (v_Count=0) THEN
rafael@5825
   332
          RAISE_APPLICATION_ERROR(-20000, '@NotCorrectOrgDoctypeShipment@') ;
rafael@5825
   333
        END IF;
rafael@5825
   334
        SELECT COUNT(*), MAX(M.line)
rafael@5825
   335
        INTO v_Count, v_line
rafael@5825
   336
        FROM M_InOutLine M,
rafael@5825
   337
          M_Product P
rafael@5825
   338
        WHERE M.M_PRODUCT_ID=P.M_PRODUCT_ID
rafael@5825
   339
          AND P.M_ATTRIBUTESET_ID IS NOT NULL
rafael@6091
   340
          AND (P.ATTRSETVALUETYPE IS NULL OR P.ATTRSETVALUETYPE <> 'F')
rafael@6104
   341
          AND (SELECT ISONEATTRSETVALREQUIRED FROM M_ATTRIBUTESET WHERE M_ATTRIBUTESET_ID = P.M_ATTRIBUTESET_ID) = 'Y'
rafael@5825
   342
          AND COALESCE(M.M_ATTRIBUTESETINSTANCE_ID, '0') = '0'
rafael@5825
   343
          AND M.M_INOUT_ID=v_Record_ID;
rafael@5825
   344
        IF v_Count<>0 THEN
rafael@6091
   345
          RAISE_APPLICATION_ERROR(-20000, '@Inline@'||' '||v_line||' '||'@productWithoutAttributeSet@') ;
rafael@5825
   346
        END IF;
rafael@5825
   347
        SELECT COUNT(*), MAX(M.line)
rafael@5825
   348
        INTO v_Count, v_Line
rafael@5825
   349
        FROM M_InOut I,
rafael@5825
   350
          M_InOutLine M,
rafael@5825
   351
          M_AttributeSetInstance P
rafael@5825
   352
        WHERE I.M_InOut_ID=M.M_InOut_ID
rafael@5825
   353
          AND M.M_AttributeSetInstance_ID=P.M_AttributeSetInstance_ID
rafael@5825
   354
          AND P.ISLOCKED='Y'
rafael@5825
   355
          AND I.ISSOTRX='Y'
rafael@5825
   356
          AND I.M_INOUT_ID=v_Record_ID;
naiara@19828
   357
        IF (v_Count<>0) THEN
rafael@5825
   358
          RAISE_APPLICATION_ERROR(-20000, '@Inline@'||v_line||' '||'@lockedProduct@') ;
rafael@5825
   359
        END IF;
rafael@5825
   360
      -- check inout line instance location
rafael@5825
   361
        SELECT COUNT(*), MAX(M.line)
rafael@5825
   362
        INTO v_Count, v_Line
rafael@5825
   363
        FROM M_InOutLine M,
rafael@5825
   364
          M_Product P
rafael@5825
   365
        WHERE M.M_InOut_ID=v_Record_ID
rafael@5825
   366
          AND M.M_Locator_ID IS NULL
rafael@5825
   367
          AND p.m_product_id = m.m_product_id
rafael@5825
   368
          AND p.isstocked = 'Y'
rafael@5825
   369
          AND p.producttype = 'I';
naiara@19828
   370
        IF (v_Count <> 0) THEN
rafael@5825
   371
          RAISE_APPLICATION_ERROR(-20000, '@Inline@'||v_line||' '||'@InoutLineWithoutLocator@') ;
rafael@5825
   372
        END IF;	  
naiara@19828
   373
      --check if bom non-stockable is exploded
naiara@19828
   374
        SELECT COUNT(*), MAX(M.line)
naiara@19828
   375
        INTO v_Count, v_Line
naiara@19828
   376
        FROM M_InOutLine M,    
naiara@19828
   377
          M_Product P
naiara@19828
   378
        WHERE M.M_InOut_ID=v_Record_ID
naiara@19828
   379
          AND P.isBOM='Y' 
naiara@19828
   380
          AND P.isstocked='N'
naiara@19828
   381
          AND M.explode='N'
naiara@19828
   382
          AND p.m_product_id = m.m_product_id;
naiara@19828
   383
        IF (v_Count <> 0) THEN
naiara@19828
   384
          RAISE_APPLICATION_ERROR(-20000, '@Inline@'||v_line||' '||'@InoutLineNotExploded@') ;
naiara@19828
   385
        END IF;
gorkaion@14547
   386
    --Check negative quantities on return inouts
gorkaion@14547
   387
    IF (v_isreturndoctype = 'Y') THEN
gorkaion@14547
   388
      SELECT count(*) INTO v_count
gorkaion@14547
   389
      FROM m_inoutline iol JOIN c_orderline ol ON iol.c_orderline_id = ol.c_orderline_id
gorkaion@14547
   390
      WHERE iol.m_inout_id = v_record_id
gorkaion@14547
   391
        AND iol.movementqty > 0
ioritz@15868
   392
        AND canceled_inoutline_id IS NULL
gorkaion@14547
   393
        AND ol.c_order_discount_id IS NULL;
gorkaion@14547
   394
      IF (v_Count <> 0) THEN
gorkaion@14547
   395
        RAISE_APPLICATION_ERROR(-20000, '@ReturnInOutNegativeQty@');
gorkaion@14547
   396
      END IF;
gorkaion@14547
   397
    END IF;
gorkaion@20976
   398
      SELECT count(*) INTO v_count
gorkaion@20976
   399
      FROM dual
gorkaion@20976
   400
      WHERE EXISTS (
gorkaion@20976
   401
          SELECT 1
gorkaion@20976
   402
          FROM m_inoutline il JOIN m_product p ON il.m_product_id = p.m_product_id
gorkaion@20976
   403
          WHERE il.m_inout_id = v_record_id
gorkaion@20976
   404
            AND p.isgeneric = 'Y');
gorkaion@20944
   405
      IF (v_count > 0) THEN
gorkaion@20976
   406
        SELECT max(p.name) INTO v_productname
gorkaion@20976
   407
        FROM m_inoutline il JOIN m_product p ON il.m_product_id = p.m_product_id
gorkaion@20976
   408
        WHERE il.m_inout_id = v_record_id
gorkaion@20976
   409
          AND p.isgeneric = 'Y';
gorkaion@20944
   410
        RAISE_APPLICATION_ERROR(-20000, '@CannotUseGenericProduct@ ' || v_productName);
gorkaion@20944
   411
      END IF;
gorkaion@20944
   412
      
rafael@5825
   413
        -- Process Shipments
pandeeswari@19202
   414
      SELECT COUNT(*) INTO v_Aux
pandeeswari@19202
   415
      FROM M_InOutLine
pandeeswari@19202
   416
      WHERE M_InOut_ID = v_Record_ID;
pandeeswari@19202
   417
pandeeswari@19202
   418
      IF v_Aux > 0 THEN  
pandeeswari@19202
   419
        SELECT COUNT(*)
sandra@19203
   420
        INTO v_Count
pandeeswari@19202
   421
        FROM M_INOUT IO, M_INOUTLINE IOL
pandeeswari@19202
   422
        WHERE IO.M_INOUT_ID = IOL.M_INOUT_ID
eduardo@19387
   423
        AND AD_ISORGINCLUDED(IOL.AD_Org_ID, IO.AD_Org_ID, IO.AD_Client_ID) = -1
sandra@19203
   424
        AND IO.M_INOUT_ID = v_Record_ID;
eduardo@19387
   425
        IF (v_Count>0) THEN
sandra@19203
   426
          RAISE_APPLICATION_ERROR(-20000, '@NotCorrectOrgLines@') ;
pandeeswari@19202
   427
        END IF;
pandeeswari@18808
   428
      END IF;
rafael@5825
   429
      
rafael@5825
   430
      -- Check the header belongs to a organization where transactions are posible and ready to use
rafael@5825
   431
      SELECT AD_Org.IsReady, Ad_OrgType.IsTransactionsAllowed
rafael@5825
   432
      INTO v_is_ready, v_is_tr_allow
rafael@5825
   433
      FROM M_INOUT, AD_Org, AD_OrgType
rafael@5825
   434
      WHERE AD_Org.AD_Org_ID=M_INOUT.AD_Org_ID
rafael@5825
   435
      AND AD_Org.AD_OrgType_ID=AD_OrgType.AD_OrgType_ID
rafael@5825
   436
      AND M_INOUT.M_INOUT_ID=v_Record_ID;
rafael@5825
   437
      IF (v_is_ready='N') THEN
rafael@5825
   438
        RAISE_APPLICATION_ERROR(-20000, '@OrgHeaderNotReady@');
carlos@0
   439
      END IF;
rafael@5825
   440
      IF (v_is_tr_allow='N') THEN
rafael@5825
   441
        RAISE_APPLICATION_ERROR(-20000, '@OrgHeaderNotTransAllowed@');
carlos@0
   442
      END IF;
rafael@5825
   443
        
rafael@5825
   444
      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
   445
      IF (v_is_included=-1) THEN
rafael@5825
   446
        RAISE_APPLICATION_ERROR(-20000, '@LinesAndHeaderDifferentLEorBU@');
carlos@0
   447
      END IF;
asier@1929
   448
      
rafael@5825
   449
      -- Check the period control is opened (only if it is legal entity with accounting)
rafael@5825
   450
      -- Gets the BU or LE of the document
rafael@5825
   451
      SELECT AD_GET_DOC_LE_BU('M_INOUT', v_Record_ID, 'M_INOUT_ID', 'LE')
rafael@5825
   452
      INTO v_org_bule_id
victor@3065
   453
      FROM DUAL;
victor@3065
   454
      
rafael@5825
   455
      SELECT AD_OrgType.IsAcctLegalEntity
rafael@5825
   456
      INTO v_isacctle
rafael@5825
   457
      FROM AD_OrgType, AD_Org
rafael@5825
   458
      WHERE AD_Org.AD_OrgType_ID = AD_OrgType.AD_OrgType_ID
rafael@5825
   459
      AND AD_Org.AD_Org_ID=v_org_bule_id;
rafael@5825
   460
      
rafael@5825
   461
      IF (v_isacctle='Y') THEN    
rafael@5825
   462
        SELECT C_CHK_OPEN_PERIOD(v_AD_Org_ID, v_DateAcct, NULL, v_DocType_ID) 
rafael@5825
   463
        INTO v_available_period
rafael@5825
   464
        FROM DUAL;
javier@20594
   465
rafael@5825
   466
        IF (v_available_period<>1) THEN
javier@20594
   467
          IF (v_docAction <> 'RC') THEN
javier@20594
   468
             RAISE_APPLICATION_ERROR(-20000, '@PeriodNotAvailable@');
javier@20594
   469
          END IF;
rafael@5825
   470
        END IF;
rafael@5825
   471
      END IF;  
rafael@5825
   472
  
rafael@5825
   473
        FOR Cur_InOut IN
rafael@5825
   474
          (SELECT *
rafael@5825
   475
          FROM M_INOUT
rafael@5825
   476
          WHERE(M_InOut_ID=v_Record_ID
rafael@5825
   477
            OR(v_Record_ID IS NULL
rafael@5825
   478
            AND DocAction='CO'))
rafael@5825
   479
            AND IsActive='Y'  FOR UPDATE
rafael@5825
   480
          )
rafael@5825
   481
        LOOP
rafael@5825
   482
          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
   483
          v_ResultStr:='HeaderLoop';
rafael@5825
   484
          /**
rafael@5825
   485
          * Shipment not processed
rafael@5825
   486
          */
rafael@5825
   487
          IF(Cur_InOut.Processed='N' AND Cur_InOut.DocStatus='DR' AND Cur_InOut.DocAction='CO') THEN
rafael@5825
   488
            -- For all active shipment lines
rafael@5825
   489
            v_ResultStr:='HeaderLoop-1';
pandeeswari@19202
   490
            
rafael@5825
   491
        IF v_Aux=0 THEN
rafael@5825
   492
        RAISE_APPLICATION_ERROR(-20000, '@ReceiptWithoutLines@');
rafael@5825
   493
        END IF;
carlos@0
   494
          FOR Cur_InOutLine IN
carlos@0
   495
            (SELECT *
carlos@0
   496
            FROM M_INOUTLINE
carlos@0
   497
            WHERE M_InOut_ID=Cur_InOut.M_InOut_ID
carlos@0
   498
              AND IsActive='Y'  FOR UPDATE
carlos@0
   499
            )
carlos@0
   500
          LOOP
adrian@170
   501
            -- Incomming or Outgoing :1:2
carlos@0
   502
            v_Qty:=Cur_InOutLine.MovementQty;
carlos@0
   503
            v_QuantityOrder:=Cur_InOutLine.QuantityOrder;
carlos@0
   504
            IF(SUBSTR(Cur_InOut.MovementType, 2)='-') THEN
carlos@0
   505
              v_Qty:=- Cur_InOutLine.MovementQty;
carlos@0
   506
              v_QuantityOrder:=-Cur_InOutLine.QuantityOrder;
carlos@0
   507
            END IF;
carlos@0
   508
            IF(Cur_InOut.IsSOTrx='N') THEN
carlos@0
   509
              v_QtySO:=0;
carlos@0
   510
              v_QtyPO:=Cur_InOutLine.MovementQty;
carlos@0
   511
              v_QuantityOrderSO:=0;
carlos@0
   512
              v_QuantityOrderPO:=Cur_InOutLine.QuantityOrder;
carlos@0
   513
            ELSE
carlos@0
   514
              v_QtySO:=Cur_InOutLine.MovementQty;
carlos@0
   515
              v_QtyPO:=0;
carlos@0
   516
              v_QuantityOrderSO:=Cur_InOutLine.QuantityOrder;
carlos@0
   517
              v_QuantityOrderPO:=0;
carlos@0
   518
            END IF;
carlos@0
   519
            -- UOM Conversion
adrian@170
   520
            -- Is it a standard stocked product:3
carlos@0
   521
            SELECT COUNT(*)
carlos@0
   522
            INTO v_IsStocked
carlos@0
   523
            FROM M_PRODUCT
carlos@0
   524
            WHERE M_Product_ID=Cur_InOutLine.M_Product_ID
carlos@0
   525
              AND IsStocked='Y'
carlos@0
   526
              AND ProductType='I';
carlos@0
   527
            -- Create Transaction for stocked product
harikrishnan@8015
   528
            IF(Cur_InOutLine.M_Product_ID IS NOT NULL AND v_IsStocked=1 AND Cur_InOutLine.IsDescription <> 'Y') THEN
gorkaion@18652
   529
              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
   530
                -- Manage reservations.
gorkaion@18652
   531
                SELECT count(*), max(m_reservation_id)
gorkaion@18652
   532
                  INTO v_aux, v_reservation_id
gorkaion@18652
   533
                FROM m_reservation
david@22931
   534
                WHERE c_orderline_id = cur_inoutline.c_orderline_id
david@22931
   535
                AND res_status NOT IN ('DR', 'CL');
gorkaion@18652
   536
                IF (v_aux > 1) THEN
gorkaion@18652
   537
                  RAISE_APPLICATION_ERROR(-20000, '@SOLineWithMoreThanOneOpenReservation@');
gorkaion@18652
   538
                ELSIF (v_aux = 1) THEN
gorkaion@18652
   539
                  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
   540
                END IF;
gorkaion@18706
   541
              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
   542
                -- Undo reservation
gorkaion@18706
   543
                DECLARE
gorkaion@18706
   544
                  cur_released_stock RECORD;
gorkaion@18706
   545
                  v_qtyaux NUMBER;
gorkaion@18706
   546
                  v_undoqty NUMBER;
gorkaion@18706
   547
                BEGIN
gorkaion@18706
   548
                  SELECT count(*), max(m_reservation_id)
gorkaion@18706
   549
                    INTO v_aux, v_reservation_id
gorkaion@18706
   550
                  FROM m_reservation
david@22931
   551
                  WHERE c_orderline_id = cur_inoutline.c_orderline_id
david@22931
   552
                  AND res_status NOT IN ('DR', 'CL');
gorkaion@18706
   553
                  IF (v_aux > 1) THEN
gorkaion@18706
   554
                    RAISE_APPLICATION_ERROR(-20000, '@SOLineWithMoreThanOneOpenReservation@');
gorkaion@18706
   555
                  ELSIF (v_aux = 1) THEN
gorkaion@18706
   556
                    v_qtyaux := v_qty;
gorkaion@18706
   557
                    FOR cur_released_stock IN (
gorkaion@18706
   558
                        SELECT m_reservation_stock_id, quantity, releasedqty
gorkaion@18706
   559
                        FROM m_reservation_stock
gorkaion@18706
   560
                        WHERE m_locator_id = cur_inoutline.m_locator_id
gorkaion@18706
   561
                          AND COALESCE(m_attributesetinstance_id, '0') = COALESCE(cur_inoutline.m_attributesetinstance_id, '0')
gorkaion@18706
   562
                          AND m_reservation_id = v_reservation_id
gorkaion@18706
   563
                          AND COALESCE(releasedqty, 0) > 0
gorkaion@18706
   564
                        ORDER BY CASE isallocated WHEN 'N' THEN 0 ELSE 1 END
gorkaion@18706
   565
                    ) LOOP
gorkaion@18706
   566
                      v_undoqty := LEAST(v_qtyaux, cur_released_stock.releasedqty);
gorkaion@18706
   567
                      UPDATE m_reservation_stock
gorkaion@18706
   568
                      SET releasedqty = releasedqty - v_undoqty
gorkaion@18706
   569
                      WHERE m_reservation_stock_id = cur_released_stock.m_reservation_stock_id;
gorkaion@18706
   570
                      v_qtyaux := v_qtyaux - v_undoqty;
gorkaion@18706
   571
                    END LOOP;
gorkaion@18706
   572
                  END IF;
gorkaion@18706
   573
                END;
gorkaion@19377
   574
              ELSIF (cur_inout.issotrx = 'N' AND cur_inoutline.canceled_inoutline_id IS NULL) THEN
gorkaion@18681
   575
                -- Manage pre-reserves
gorkaion@18681
   576
                DECLARE
gorkaion@18681
   577
                  cur_reserve_stock RECORD;
gorkaion@18681
   578
                  v_pendingqty NUMBER;
gorkaion@18681
   579
                  v_qtyaux NUMBER;
gorkaion@18681
   580
                  v_res_stock_id VARCHAR2(32);
gorkaion@18681
   581
                BEGIN
gorkaion@18681
   582
                  v_pendingqty := v_qty;
gorkaion@18681
   583
                  FOR cur_reserve_stock IN (
gorkaion@18681
   584
                      SELECT rs.*
gorkaion@18681
   585
                      FROM m_reservation_stock rs JOIN m_reservation r ON rs.m_reservation_id = r.m_reservation_id
gorkaion@18681
   586
                      WHERE rs.c_orderline_id = cur_inoutline.c_orderline_id
gorkaion@18681
   587
                        AND rs.quantity <> COALESCE(rs.releasedqty, 0)
gorkaion@18681
   588
                        AND rs.m_locator_id IS NULL
gorkaion@19377
   589
                        AND r.res_status NOT IN ('DR', 'CL')
gorkaion@18681
   590
                  ) LOOP
gorkaion@18681
   591
                    v_qtyaux := LEAST(cur_reserve_stock.quantity - COALESCE(cur_reserve_stock.releasedqty, 0), v_pendingqty);
gorkaion@18681
   592
                    -- Check if exists a reserved stock for the same orderline, attributes and locator in the reservation
gorkaion@18681
   593
                    SELECT count(*), max(m_reservation_stock_id) INTO v_aux, v_res_stock_id
gorkaion@18681
   594
                    FROM m_reservation_stock
gorkaion@19377
   595
                    WHERE c_orderline_id = cur_inoutline.c_orderline_id
gorkaion@19377
   596
                      AND m_locator_id = cur_inoutline.m_locator_id
gorkaion@19377
   597
                      AND m_reservation_id = cur_reserve_stock.m_reservation_id
david@22931
   598
                      AND isallocated = cur_reserve_stock.isallocated
gorkaion@19377
   599
                      AND COALESCE(m_attributesetinstance_id, '0') = COALESCE(Cur_InOutLine.M_AttributeSetInstance_ID, '0');
gorkaion@19377
   600
                    -- Update existing prereserved stock to decrease reserved qty
gorkaion@18681
   601
                    UPDATE m_reservation_stock
gorkaion@18681
   602
                    SET quantity = quantity - v_qtyaux
gorkaion@18681
   603
                    WHERE m_reservation_stock_id = cur_reserve_stock.m_reservation_stock_id;
gorkaion@19377
   604
                    -- Insert or update reserved stock by same quantity
gorkaion@18681
   605
                    IF (v_aux > 0) THEN
gorkaion@18681
   606
                      UPDATE m_reservation_stock
gorkaion@18681
   607
                      SET quantity = quantity + v_qtyaux
gorkaion@18681
   608
                      WHERE m_reservation_stock_id = v_res_stock_id;
gorkaion@18681
   609
                    ELSE
gorkaion@18681
   610
                      INSERT INTO m_reservation_stock(
gorkaion@18681
   611
                        m_reservation_stock_id, ad_client_id, ad_org_id, isactive,
gorkaion@18681
   612
                        created, createdby, updated, updatedby,
gorkaion@18681
   613
                        m_reservation_id, m_attributesetinstance_id, m_locator_id, c_orderline_id,
gorkaion@18681
   614
                        quantity, releasedqty, isallocated
gorkaion@18681
   615
                      ) VALUES (
gorkaion@18681
   616
                        get_uuid(), cur_reserve_stock.ad_client_id, cur_reserve_stock.ad_org_id, 'Y',
gorkaion@18681
   617
                        now(), v_user, now(), v_user,
eduardo@26192
   618
                        cur_reserve_stock.m_reservation_id, coalesce(cur_inoutline.m_attributesetinstance_id,'0'), cur_inoutline.m_locator_id, cur_inoutline.c_orderline_id,
david@22931
   619
                        v_qtyaux, 0, cur_reserve_stock.isallocated
gorkaion@18681
   620
                      );
gorkaion@18681
   621
                    END IF;
gorkaion@18681
   622
                    v_pendingqty := v_pendingqty - v_qtyaux;
gorkaion@18681
   623
                    IF (v_pendingqty <= 0) THEN
gorkaion@18681
   624
                      EXIT;
gorkaion@18681
   625
                    END IF;
gorkaion@18681
   626
                  END LOOP;
gorkaion@18681
   627
                  DELETE FROM m_reservation_stock
gorkaion@18681
   628
                  WHERE c_orderline_id = cur_inoutline.c_orderline_id
gorkaion@18681
   629
                    AND quantity = 0
gorkaion@18681
   630
                    AND COALESCE(releasedqty, 0) = 0;
gorkaion@18681
   631
                END;
gorkaion@19377
   632
              ELSIF (cur_inout.issotrx = 'N' AND cur_inoutline.canceled_inoutline_id IS NOT NULL AND v_qty < 0) THEN
gorkaion@19377
   633
                -- Revert to pre-reservations
david@22931
   634
atul@24291
   635
		BEGIN
david@22931
   636
                  select  sum(iol.movementqty)
david@22931
   637
                  into v_qtysumorders
david@22931
   638
                  from m_inoutline iol
david@22931
   639
                  WHERE  iol.c_orderline_id=cur_inoutline.c_orderline_id
david@22931
   640
                  and iol.m_locator_id=cur_inoutline.m_locator_id;
david@22931
   641
david@22931
   642
                  select rs.quantity
david@22931
   643
                  into v_released
david@22931
   644
                  from m_reservation_stock rs
david@22931
   645
                  where c_orderline_id=cur_inoutline.c_orderline_id
david@22931
   646
                  and rs.m_locator_id=cur_inoutline.m_locator_id;
david@22931
   647
atul@24291
   648
                EXCEPTION
atul@24291
   649
                  WHEN NO_DATA_FOUND THEN
atul@24291
   650
                  v_qtysumorders:=0;
atul@24291
   651
                  v_released:=0;
atul@24291
   652
                END;
david@22931
   653
                  v_penqty := -v_qty - ((v_qtysumorders+(-cur_inoutline.movementqty))-v_released);
david@22931
   654
gorkaion@19377
   655
                DECLARE
gorkaion@19377
   656
                  cur_reserve_stock     RECORD;
gorkaion@19377
   657
                  v_pendingqty          NUMBER;
gorkaion@19377
   658
                  v_qtyaux              NUMBER;
gorkaion@19377
   659
                  v_res_stock_id        VARCHAR2(32);
gorkaion@19380
   660
                  v_aux_released NUMBER:= 0;
gorkaion@19377
   661
                BEGIN
david@22931
   662
                  v_pendingqty:=v_penqty;
gorkaion@19377
   663
                  FOR cur_reserve_stock IN (
gorkaion@19377
   664
                      SELECT rs.quantity, COALESCE(rs.releasedqty,0) AS releasedqty, rs.m_reservation_stock_id, rs.m_reservation_id,
david@22931
   665
                          rs.ad_org_id, rs.ad_client_id, rs.isallocated
gorkaion@19377
   666
                      FROM m_reservation_stock rs JOIN m_reservation r ON rs.m_reservation_id = r.m_reservation_id
gorkaion@19377
   667
                      WHERE rs.c_orderline_id = cur_inoutline.c_orderline_id
gorkaion@19377
   668
                        AND rs.m_locator_id = cur_inoutline.m_locator_id
gorkaion@19377
   669
                        AND r.res_status NOT IN ('DR', 'CL')
gorkaion@19377
   670
                  ) LOOP
gorkaion@19377
   671
                    v_qtyaux := LEAST((cur_reserve_stock.quantity - COALESCE(cur_reserve_stock.releasedqty, 0)), v_pendingqty);
gorkaion@19377
   672
                    v_aux_released := v_aux_released + COALESCE(cur_reserve_stock.releasedqty, 0);
gorkaion@20270
   673
                    IF (cur_reserve_stock.quantity <> COALESCE(cur_reserve_stock.releasedqty, 0)) THEN
gorkaion@20270
   674
                      -- Check if exists a prereservation for the same orderline, attributes and locator in the reservation
gorkaion@20270
   675
                      SELECT count(*), max(m_reservation_stock_id) INTO v_aux, v_res_stock_id
gorkaion@20270
   676
                      FROM m_reservation_stock
gorkaion@20270
   677
                      WHERE c_orderline_id = cur_inoutline.c_orderline_id
gorkaion@20270
   678
                        AND m_locator_id IS NULL
gorkaion@20270
   679
                        AND m_reservation_id = cur_reserve_stock.m_reservation_id;
gorkaion@20270
   680
                      -- Update existing prereserved stock to decrease reserved qty
david@22931
   681
gorkaion@19377
   682
                      UPDATE m_reservation_stock
gorkaion@20270
   683
                      SET quantity = quantity - v_qtyaux
gorkaion@20270
   684
                      WHERE m_reservation_stock_id = cur_reserve_stock.m_reservation_stock_id;
gorkaion@20270
   685
                      -- Insert or update reserved stock by same quantity
gorkaion@20270
   686
                      IF (v_aux > 0) THEN
gorkaion@20270
   687
                        UPDATE m_reservation_stock
gorkaion@20270
   688
                        SET quantity = quantity + v_qtyaux
gorkaion@20270
   689
                        WHERE m_reservation_stock_id = v_res_stock_id;
gorkaion@20270
   690
                      ELSE
gorkaion@20270
   691
                        INSERT INTO m_reservation_stock (
gorkaion@20270
   692
                          m_reservation_stock_id, ad_client_id, ad_org_id, isactive,
gorkaion@20270
   693
                          created, createdby, updated, updatedby,
gorkaion@20270
   694
                          m_reservation_id, m_attributesetinstance_id, m_locator_id, c_orderline_id,
gorkaion@20270
   695
                          quantity, releasedqty, isallocated
gorkaion@20270
   696
                        ) VALUES (
gorkaion@20270
   697
                          get_uuid(), cur_reserve_stock.ad_client_id, cur_reserve_stock.ad_org_id, 'Y',
gorkaion@20270
   698
                          now(), v_user, now(), v_user,
gorkaion@20270
   699
                          cur_reserve_stock.m_reservation_id, '0', NULL, cur_inoutline.c_orderline_id,
david@22931
   700
                          v_qtyaux, 0, cur_reserve_stock.isallocated
gorkaion@20270
   701
                        );
gorkaion@20270
   702
                      END IF;
gorkaion@20270
   703
                      v_pendingqty := v_pendingqty - v_qtyaux;
gorkaion@20270
   704
                      IF (v_pendingqty <= 0) THEN
gorkaion@20270
   705
                        EXIT;
gorkaion@20270
   706
                      END IF;
gorkaion@19377
   707
                    END IF;
gorkaion@19377
   708
                  END LOOP;
gorkaion@19377
   709
                  IF (v_pendingqty > 0 AND v_aux_released > 0) THEN
gorkaion@19377
   710
                    -- Not all quantity has been reverted to pre-reservation having released quantity.
gorkaion@19377
   711
                    RAISE_APPLICATION_ERROR(-20000, '@ReceiptVoidReleasedQtyFound@');
gorkaion@19377
   712
                  END IF;
gorkaion@19377
   713
                  DELETE FROM m_reservation_stock
gorkaion@19377
   714
                  WHERE c_orderline_id = cur_inoutline.c_orderline_id
gorkaion@19377
   715
                    AND quantity = 0
gorkaion@19377
   716
                    AND COALESCE(releasedqty, 0) = 0;
gorkaion@19377
   717
                END;
david@22931
   718
gorkaion@18652
   719
              END IF;
david@22931
   720
carlos@0
   721
              v_ResultStr:='CreateTransaction';
carlos@0
   722
              Ad_Sequence_Next('M_Transaction', Cur_InOutLine.AD_Org_ID, v_NextNo) ;
carlos@0
   723
              INSERT
carlos@0
   724
              INTO M_TRANSACTION
carlos@0
   725
                (
carlos@0
   726
                  M_Transaction_ID, M_InOutLine_ID, AD_Client_ID, AD_Org_ID,
carlos@0
   727
                  IsActive, Created, CreatedBy, Updated,
carlos@0
   728
                  UpdatedBy, MovementType, M_Locator_ID, M_Product_ID,
carlos@0
   729
                  M_AttributeSetInstance_ID, MovementDate, MovementQty, M_Product_UOM_ID,
carlos@0
   730
                  QuantityOrder, C_UOM_ID
carlos@0
   731
                )
carlos@0
   732
                VALUES
carlos@0
   733
                (
carlos@0
   734
                  v_NextNo, Cur_InOutLine.M_InOutLine_ID, Cur_InOutLine.AD_Client_ID, Cur_InOutLine.AD_Org_ID,
harikrishnan@7227
   735
                   'Y', now(), v_User, now(),
harikrishnan@7227
   736
                  v_User, Cur_InOut.MovementType, Cur_InOutLine.M_Locator_ID, Cur_InOutLine.M_Product_ID,
juanpablo@1605
   737
                  COALESCE(Cur_InOutLine.M_AttributeSetInstance_ID, '0'), Cur_InOut.MovementDate, v_Qty, Cur_InOutLine.M_Product_UOM_ID,
carlos@0
   738
                  v_QuantityOrder, Cur_InOutLine.C_UOM_ID
carlos@0
   739
                )
carlos@0
   740
                ;
carlos@0
   741
            END IF;
carlos@0
   742
            -- Create Asset
carlos@0
   743
            IF(Cur_InOutLine.M_Product_ID IS NOT NULL AND Cur_InOut.IsSOTrx='Y') THEN
antonio@735
   744
              A_ASSET_CREATE(NULL, Cur_InOutLine.M_InOutLine_ID) ;
carlos@0
   745
            END IF;
carlos@0
   746
            v_ResultStr:='UpdateOrderLine';
carlos@0
   747
            IF(Cur_InOutLine.C_OrderLine_ID IS NOT NULL) THEN
atul@21879
   748
              if(Cur_InOut.ISSOTRX='Y') THEN
atul@21879
   749
                -- Sets DateDelivered with the recent shipment date
atul@21879
   750
                -- of the shipment/s done for the orderline.
atul@21879
   751
                SELECT MAX(M.MOVEMENTDATE) INTO v_DateDelivered
atul@21879
   752
                FROM M_INOUTLINE ML, M_INOUT M
atul@21879
   753
                WHERE ML.C_OrderLine_ID = Cur_InOutLine.C_OrderLine_ID
atul@21879
   754
                AND ML.M_INOUT_ID = M.M_INOUT_ID
atul@21879
   755
                AND M.DOCSTATUS='CO';
atul@21879
   756
              ELSE
atul@21879
   757
                v_DateDelivered := null;
atul@21879
   758
              END IF;
atul@21879
   759
atul@21879
   760
              IF(v_QtySO > 0) THEN
atul@21879
   761
                IF(v_DateDelivered IS NULL OR v_DateDelivered < Cur_InOut.MovementDate ) THEN
atul@21879
   762
                  v_DateDelivered:=Cur_InOut.MovementDate;
atul@21879
   763
                END IF;
atul@21879
   764
              END IF;
atul@21879
   765
              
carlos@0
   766
              -- stocked product
carlos@0
   767
              IF(Cur_InOutLine.M_Product_ID IS NOT NULL AND v_IsStocked=1) THEN
carlos@0
   768
                -- Update OrderLine (if C-, Qty is negative)
javier@21256
   769
                SELECT DOCSTATUS into v_DocStatus
javier@21256
   770
                FROM C_ORDER
javier@21256
   771
                WHERE C_ORDER_ID = (SELECT C_ORDER_ID
javier@21256
   772
                                    FROM C_ORDERLINE 
javier@21256
   773
                                    WHERE C_ORDERLINE_ID=Cur_InOutLine.C_OrderLine_ID);
javier@21256
   774
                IF (v_DocStatus = 'DR') THEN
javier@21256
   775
                  UPDATE C_ORDERLINE
javier@21256
   776
                    SET QtyDelivered=QtyDelivered + v_QtySO,
atul@21879
   777
                    DATEDELIVERED=(CASE WHEN (QtyDelivered + v_QtySO) > 0 THEN v_DateDelivered ELSE NULL END),
javier@21256
   778
                    Updated=now(),
javier@21256
   779
                    UpdatedBy=v_User
javier@21256
   780
                  WHERE C_OrderLine_ID=Cur_InOutLine.C_OrderLine_ID;
javier@21256
   781
                ELSE 
javier@21256
   782
                  UPDATE C_ORDERLINE
javier@21256
   783
                    SET QtyReserved=QtyReserved - v_QtyPO - v_QtySO,
atul@21879
   784
                    DATEDELIVERED=(CASE WHEN (QtyReserved - v_QtyPO - v_QtySO) > 0 THEN v_DateDelivered ELSE NULL END),
javier@21256
   785
                    QtyDelivered=QtyDelivered + v_QtySO,
javier@21256
   786
                    Updated=now(),
javier@21256
   787
                    UpdatedBy=v_User
javier@21256
   788
                   WHERE C_OrderLine_ID=Cur_InOutLine.C_OrderLine_ID;
javier@21256
   789
                END IF;
carlos@0
   790
                -- Products not stocked
carlos@0
   791
              ELSE
carlos@0
   792
                -- Update OrderLine (if C-, Qty is negative)
carlos@0
   793
                UPDATE C_ORDERLINE
carlos@0
   794
                  SET QtyDelivered=QtyDelivered + v_QtySO,
atul@21879
   795
                  DATEDELIVERED=(CASE WHEN (QtyDelivered + v_QtySO) > 0 THEN v_DateDelivered ELSE NULL END),
harikrishnan@7227
   796
                  Updated=now(),
harikrishnan@7227
   797
                  UpdatedBy=v_User
carlos@0
   798
                WHERE C_OrderLine_ID=Cur_InOutLine.C_OrderLine_ID;
carlos@0
   799
              END IF;
carlos@0
   800
            END IF;
carlos@0
   801
            IF(Cur_InOutLine.M_Product_ID IS NOT NULL AND v_IsStocked=1) THEN
alvaro@27384
   802
              SELECT AD_Org_ID INTO v_Locator_Org_ID FROM M_Locator L WHERE M_Locator_ID = Cur_InOutLine.M_Locator_ID;
david@27385
   803
              M_Check_Stock(Cur_InOutLine.M_Product_ID, v_AD_Client_ID, Cur_InOutLine.AD_Org_ID, v_Result, v_Message) ;
carlos@0
   804
              IF v_Result=0 THEN
ioritz@16496
   805
                SELECT name INTO v_ProductName FROM M_Product WHERE M_Product_id = Cur_InOutLine.M_Product_ID;
ioritz@16496
   806
			    RAISE_APPLICATION_ERROR(-20000, v_Message||' '||'@line@'||' '||Cur_InOutLine.line||', '||'@Product@'||' '||v_ProductName) ;
carlos@0
   807
              END IF;
carlos@0
   808
            END IF;
carlos@0
   809
          END LOOP; -- For all InOut Lines
carlos@0
   810
          /*******************
carlos@0
   811
          * PO Matching
carlos@0
   812
          ******************/
carlos@0
   813
          IF(Cur_InOut.IsSOTrx='N') THEN
carlos@0
   814
            DECLARE
carlos@0
   815
              Cur_SLines RECORD;
carlos@0
   816
              Cur_ILines RECORD;
carlos@0
   817
              v_Qty NUMBER;
juanpablo@1605
   818
              v_MatchPO_ID VARCHAR2(32) ;
juanpablo@1605
   819
              v_MatchInv_ID VARCHAR2(32) ;
carlos@0
   820
            BEGIN
carlos@0
   821
              v_ResultStr:='MatchPO';
carlos@0
   822
              FOR Cur_SLines IN
carlos@0
   823
                (SELECT sl.AD_Client_ID,
carlos@0
   824
                  sl.AD_Org_ID,
carlos@0
   825
                  ol.C_OrderLine_ID,
carlos@0
   826
                  sl.M_InOutLine_ID,
carlos@0
   827
                  sl.M_Product_ID,
carlos@0
   828
                  sl.M_AttributeSetInstance_ID,
carlos@0
   829
                  sl.MovementQty,
carlos@0
   830
                  ol.QtyOrdered
carlos@0
   831
                FROM M_INOUTLINE sl,
carlos@0
   832
                  C_ORDERLINE ol
carlos@0
   833
                WHERE sl.C_OrderLine_ID=ol.C_OrderLine_ID
carlos@0
   834
                  AND sl.M_Product_ID=ol.M_Product_ID  --    AND   sl.M_AttributeSetInstance_ID=ol.M_AttributeSetInstance_ID
carlos@0
   835
                  AND sl.M_InOut_ID=Cur_InOut.M_InOut_ID
carlos@0
   836
                )
david@19206
   837
              LOOP          
david@19010
   838
mikel@19201
   839
                v_Qty:=Cur_SLines.MovementQty;
mikel@19201
   840
                --IF (ABS(Cur_SLines.MovementQty) > ABS(Cur_SLines.QtyOrdered)) THEN
mikel@19201
   841
                -- v_Qty := Cur_SLines.QtyOrdered;
mikel@19201
   842
                --END IF;
david@19010
   843
mikel@19201
   844
                  Ad_Sequence_Next('M_MatchPO', Cur_SLines.AD_Org_ID, v_MatchPO_ID) ;
mikel@19201
   845
                  -- The min qty. Modified by Ismael Ciordia
mikel@19201
   846
                  v_ResultStr:='InsertMatchPO ' || v_MatchPO_ID;
mikel@19201
   847
                  INSERT
mikel@19201
   848
                  INTO M_MATCHPO
mikel@19201
   849
                    (
mikel@19201
   850
                      M_MatchPO_ID, AD_Client_ID, AD_Org_ID, IsActive,
mikel@19201
   851
                      Created, CreatedBy, Updated, UpdatedBy,
mikel@19201
   852
                      M_InOutLine_ID, C_OrderLine_ID, M_Product_ID, DateTrx,
mikel@19201
   853
                      Qty, Processing, Processed, Posted
mikel@19201
   854
                    )
mikel@19201
   855
                    VALUES
mikel@19201
   856
                    (
mikel@19201
   857
                      v_MatchPO_ID, Cur_SLines.AD_Client_ID, Cur_SLines.AD_Org_ID, 'Y',
mikel@19201
   858
                      now(), v_User, now(), v_User,
mikel@19201
   859
                      Cur_SLines.M_InOutLine_ID, Cur_SLines.C_OrderLine_ID, Cur_SLines.M_Product_ID, now(),
mikel@19201
   860
                      v_Qty, 'N', 'Y', 'N'
mikel@19201
   861
                    )
mikel@19201
   862
                  ;
mikel@19198
   863
                  
carlos@0
   864
              END LOOP;
carlos@0
   865
              v_ResultStr:='MatchInv';
carlos@0
   866
              FOR Cur_ILines IN
carlos@0
   867
                (SELECT sl.AD_Client_ID,
carlos@0
   868
                  sl.AD_Org_ID,
carlos@0
   869
                  il.C_InvoiceLine_ID,
carlos@0
   870
                  sl.M_InOutLine_ID,
carlos@0
   871
                  sl.M_Product_ID,
carlos@0
   872
                  sl.M_AttributeSetInstance_ID,
carlos@0
   873
                  sl.MovementQty,
david@7233
   874
                  il.QTYINVOICED,
david@7233
   875
                  i.DateAcct
carlos@0
   876
                FROM M_INOUTLINE sl,
david@7233
   877
                  C_INVOICE i,
carlos@0
   878
                  C_INVOICELINE il
carlos@0
   879
                WHERE sl.M_InOutLine_ID=il.M_InOutLine_ID
carlos@0
   880
                  AND sl.M_InOut_ID=Cur_InOut.M_InOut_ID
david@7233
   881
                  AND i.C_INVOICE_ID = il.C_INVOICE_ID
carlos@0
   882
                )
carlos@0
   883
              LOOP
carlos@0
   884
                Ad_Sequence_Next('M_MatchInv', Cur_ILines.AD_Org_ID, v_MatchInv_ID) ;
carlos@0
   885
                -- The min qty. Modified by Ismael Ciordia
carlos@0
   886
                v_Qty:=Cur_ILines.MovementQty;
gorkaion@239
   887
                --IF (ABS(Cur_ILines.MovementQty) > ABS(Cur_ILines.QtyInvoiced)) THEN
carlos@0
   888
                -- v_Qty := Cur_ILines.QtyInvoiced;
carlos@0
   889
                --END IF;
carlos@0
   890
                v_ResultStr:='InsertMatchPO ' || v_MatchPO_ID;
carlos@0
   891
                INSERT
carlos@0
   892
                INTO M_MATCHINV
carlos@0
   893
                  (
carlos@0
   894
                    M_MATCHINV_ID, AD_CLIENT_ID, AD_ORG_ID, ISACTIVE,
carlos@0
   895
                    CREATED, CREATEDBY, UPDATED, UPDATEDBY,
carlos@0
   896
                    M_INOUTLINE_ID, C_INVOICELINE_ID, M_PRODUCT_ID, DATETRX,
carlos@0
   897
                    QTY, PROCESSING, PROCESSED, POSTED
carlos@0
   898
                  )
carlos@0
   899
                  VALUES
carlos@0
   900
                  (
carlos@0
   901
                    v_MatchInv_ID, Cur_ILines.AD_Client_ID, Cur_ILines.AD_Org_ID, 'Y',
harikrishnan@7227
   902
                    now(), v_User, now(), v_User,
david@7233
   903
                    Cur_ILines.M_InOutLine_ID, Cur_ILines.C_InvoiceLine_ID, Cur_ILines.M_Product_ID, Cur_ILines.DateAcct,
carlos@0
   904
                    v_Qty, 'N', 'Y', 'N'
carlos@0
   905
                  )
carlos@0
   906
                  ;
carlos@0
   907
              END LOOP;
gorkaion@24928
   908
              -- Set check price difference to true in case the receipt line has a related invoice.
gorkaion@24928
   909
              UPDATE M_TRANSACTION
gorkaion@24928
   910
              SET checkpricedifference = 'Y'
gorkaion@24928
   911
              WHERE M_INOUTLINE_ID IN (SELECT DISTINCT il.m_inoutline_id
gorkaion@24928
   912
                                       FROM m_inoutline il
gorkaion@24928
   913
                                          JOIN m_matchinv mi ON il.m_inoutline_id = mi.m_inoutline_id
gorkaion@24928
   914
                                       WHERE il.m_inout_id = Cur_InOut.M_InOut_ID);
carlos@0
   915
            END;
gorkaion@3170
   916
          ELSE
gaurav@21852
   917
            --Void document is created automatically from main document . 
gaurav@21852
   918
            --during completion of void document , we have to skip delivery rule for void document .
gaurav@21852
   919
            select COALESCE(instr(M_INOUT.Description,'*R*:'),0) 
gaurav@21852
   920
            into v_voiddoccount 
gaurav@21852
   921
            from M_INOUT
gaurav@21852
   922
            where M_INOUT.M_INOUT_id =v_Record_ID;
gaurav@21852
   923
            if v_voiddoccount = 0 then
gaurav@21852
   924
              v_ResultStr:='Check delivery rule for sales orders';
gaurav@21852
   925
              v_Message_aux:='';
gaurav@21852
   926
              v_orderid_old:='0';
gaurav@21852
   927
              FOR Cur_Order IN 
gaurav@21852
   928
                (SELECT c_order.deliveryrule, m_inoutline.line, c_order.c_order_id,
gaurav@21852
   929
                        c_order.documentno, c_orderline.line as orderline
gaurav@21852
   930
                 FROM M_InOutLine, C_Orderline, C_Order
gaurav@21852
   931
                 WHERE M_Inoutline.c_orderline_id = c_orderline.c_orderline_id
gaurav@21852
   932
                   AND c_orderline.c_order_id = c_order.c_order_id
gaurav@21852
   933
                   AND m_inoutline.m_inout_id = cur_inout.m_inout_id
gaurav@21852
   934
                   AND ((c_order.deliveryrule = 'O'
gaurav@21852
   935
                        AND EXISTS (SELECT 1 FROM C_OrderLine ol
gaurav@21852
   936
                                    WHERE ol.C_Order_ID = C_order.c_order_id
gaurav@21852
   937
                                      and ol.qtyordered > ol.qtydelivered ))
gaurav@21852
   938
                        OR (c_order.deliveryrule = 'L' 
gaurav@21852
   939
                            AND c_orderline.qtyordered > c_orderline.qtydelivered))
gaurav@21852
   940
                 ORDER BY c_order.c_order_id, c_orderline.line) LOOP
gaurav@21852
   941
                --Order lines not completely delivered with delivery rule O or L
gaurav@21852
   942
                IF (v_orderid_old <> cur_order.c_order_id OR cur_order.deliveryrule <> 'O' ) THEN
gaurav@21852
   943
                  v_Message_aux := COALESCE(v_Message_aux,'') || '@Shipment@' || ' ' || cur_inout.documentno;
gaurav@21852
   944
                  v_Message_aux := v_Message_aux || ' ' || '@line@' || ' ' || cur_order.line || ': ';
gaurav@21852
   945
                  v_Message_aux := v_Message_aux || '@SalesOrderDocumentno@' || cur_order.documentno;
gaurav@21852
   946
                  IF (cur_order.deliveryrule = 'O') THEN
gaurav@21852
   947
                    v_Message_aux := v_Message_aux || ' ' || '@notCompleteDeliveryRuleOrder@' || '<br>';
gaurav@21852
   948
                  ELSE
gaurav@21852
   949
                    v_Message_aux := v_Message_aux || ' ' || '@line@' || ' ' || cur_order.orderline;
gaurav@21852
   950
                    v_Message_aux := v_Message_aux || ' ' || '@notCompleteDeliveryRuleLine@' || '<br>';
gaurav@21852
   951
                  END IF;
gorkaion@3170
   952
                END IF;
gaurav@21852
   953
                v_orderid_old := cur_order.c_order_id;
gaurav@21852
   954
              END LOOP;
gaurav@21852
   955
              IF (v_Message_aux IS NOT NULL AND v_Message_aux <> '') THEN
gaurav@21852
   956
                RAISE_APPLICATION_ERROR(-20000, v_Message_aux);
gorkaion@3170
   957
              END IF;
gorkaion@3170
   958
            END IF;
carlos@0
   959
          END IF;
carlos@0
   960
          -- Close Shipment
carlos@0
   961
          v_ResultStr:='CloseShipment';
carlos@0
   962
          UPDATE M_INOUT
carlos@0
   963
            SET Processed='Y',
carlos@0
   964
            DocStatus='CO',
carlos@0
   965
            DocAction='--',
mikel@19150
   966
            Process_Goods_Java='--',
harikrishnan@7227
   967
            Updated=now(),
harikrishnan@7227
   968
            UpdatedBy=v_User
carlos@0
   969
          WHERE M_INOUT.M_INOUT_ID=Cur_InOut.M_INOUT_ID;
carlos@0
   970
          --
asier@1027
   971
          
asier@1027
   972
         
carlos@0
   973
          -- Not Processed + Complete --
carlos@0
   974
          /**
carlos@0
   975
          * Reverse Correction
carlos@0
   976
          */
carlos@0
   977
        ELSIF(Cur_InOut.DocStatus='CO' AND Cur_InOut.DocAction='RC') THEN
javier@17645
   978
          --Check if the m_inoutlines has an invoice lines related. In this case is not possible to void the m_inout.
javier@17645
   979
	  SELECT COUNT(*)
javier@17645
   980
          INTO v_count
javier@17645
   981
          FROM M_INOUTLINE MIOL 
javier@17645
   982
              JOIN C_INVOICELINE CIL ON MIOL.M_INOUTLINE_ID=CIL.M_INOUTLINE_ID 
javier@17645
   983
              JOIN C_INVOICE CI ON CI.C_INVOICE_ID=CIL.C_INVOICE_ID
javier@17645
   984
          WHERE M_INOUT_ID=Cur_InOut.m_inout_id
javier@17645
   985
          AND CI.DOCSTATUS <> 'VO';
javier@17645
   986
          IF (v_count <> 0) THEN
javier@17645
   987
	     RAISE_APPLICATION_ERROR(-20000,'@VoidShipmentWithRelatedInvoice@');
javier@17645
   988
          END IF;
gorkaion@5377
   989
          --Check that there isn't any line with an invoice if the order's 
gorkaion@5377
   990
          --invoice rule is after delivery
gorkaion@5377
   991
          select count(*), max(line) into v_count, v_line
gorkaion@5377
   992
          from (
gorkaion@5377
   993
          SELECT m_inoutline.m_inoutline_id, m_inoutline.line
gorkaion@5377
   994
          from m_inoutline, c_order, c_orderline, c_invoiceline, m_inout, c_invoice
gorkaion@5377
   995
          where m_inoutline.c_orderline_id = c_orderline.c_orderline_id
gorkaion@5377
   996
            and c_orderline.c_order_id = c_order.c_order_id
gorkaion@5377
   997
            and c_orderline.c_orderline_id = c_invoiceline.c_orderline_id
gorkaion@5377
   998
            and m_inoutline.m_inout_id = m_inout.m_inout_id
gorkaion@5377
   999
            and c_invoiceline.c_invoice_id = c_invoice.c_invoice_id
gorkaion@5377
  1000
            and m_inout.m_inout_id = Cur_InOut.m_inout_id
gorkaion@5377
  1001
            and m_inout.issotrx = 'Y'
gorkaion@5377
  1002
            and c_order.invoicerule in ('D', 'O', 'S')
gorkaion@5377
  1003
            and c_invoice.processed='Y'
gorkaion@5377
  1004
          group by m_inoutline.m_inoutline_id, m_inoutline.line
rafael@5506
  1005
          having sum(c_invoiceline.qtyinvoiced) <> 0
gorkaion@5377
  1006
          ) a;
gorkaion@5377
  1007
          IF (v_count > 0 ) THEN
gorkaion@5377
  1008
            v_Message := '@InoutDocumentno@' || ': ' || Cur_InOut.DocumentNo || ' ' || '@line@' || ': ' || v_line || '. ';
gorkaion@5377
  1009
            v_Message := v_Message || '@VoidShipmentInvoiced@';
gorkaion@5377
  1010
            RAISE_APPLICATION_ERROR(-20000, v_Message);
gorkaion@5377
  1011
          END IF;
carlos@0
  1012
          v_ResultStr:='CreateInOut';
carlos@0
  1013
          SELECT COALESCE(C_DOCTYPE_REVERSED_ID, C_DOCTYPE_ID)
carlos@0
  1014
          INTO v_DoctypeReversed_ID
carlos@0
  1015
          FROM C_DOCTYPE
carlos@0
  1016
          WHERE C_DOCTYPE_ID=Cur_InOut.C_DocType_ID;
carlos@0
  1017
          Ad_Sequence_Next('M_InOut', Cur_InOut.M_InOut_ID, v_RInOut_ID) ; -- Get RInOut_ID
carlos@0
  1018
          Ad_Sequence_Doctype(v_DoctypeReversed_ID, Cur_InOut.M_InOut_ID, 'Y', v_RDocumentNo) ; -- Get RDocumentNo
carlos@0
  1019
          IF(v_RDocumentNo IS NULL) THEN
carlos@0
  1020
            AD_Sequence_Doc('DocumentNo_M_InOut', Cur_InOut.AD_Client_ID, 'Y', v_RDocumentNo) ;
carlos@0
  1021
          END IF;
carlos@0
  1022
          -- Indicate that it is invoiced (i.e. not printed on invoices)
carlos@0
  1023
          v_ResultStr:='SetInvoiced';
harikrishnan@7227
  1024
          UPDATE M_INOUTLINE  SET IsInvoiced='Y',Updated=now(),UpdatedBy=v_User  WHERE M_InOut_ID=Cur_InOut.M_InOut_ID;
carlos@0
  1025
          --
carlos@0
  1026
          DBMS_OUTPUT.PUT_LINE('Reverse InOut_ID=' || v_RInOut_ID || ' DocumentNo=' || v_RDocumentNo) ;
carlos@0
  1027
          v_ResultStr:='InsertInOut Reverse ' || v_RInOut_ID;
carlos@0
  1028
          INSERT
carlos@0
  1029
          INTO M_INOUT
carlos@0
  1030
            (
carlos@0
  1031
              M_InOut_ID, C_Order_ID, IsSOTrx, AD_Client_ID,
carlos@0
  1032
              AD_Org_ID, IsActive, Created, CreatedBy,
carlos@0
  1033
              Updated, UpdatedBy, DocumentNo, C_DocType_ID,
carlos@0
  1034
              Description, IsPrinted, MovementType, MovementDate,
carlos@0
  1035
              DateAcct, C_BPartner_ID, C_BPartner_Location_ID, AD_User_ID,
carlos@0
  1036
              M_Warehouse_ID, POReference, DateOrdered, DeliveryRule,
carlos@0
  1037
              FreightCostRule, FreightAmt, C_Project_ID, C_Activity_ID,
carlos@0
  1038
              C_Campaign_ID, AD_OrgTrx_ID, User1_ID, User2_ID,
eduardo@18857
  1039
              C_Costcenter_ID, A_Asset_ID,
carlos@0
  1040
              DeliveryViaRule, M_Shipper_ID, C_Charge_ID, ChargeAmt,
carlos@0
  1041
              PriorityRule, DocStatus, DocAction, Processing,
pandeeswari@22934
  1042
              Processed, ISLOGISTIC, salesrep_id, Process_Goods_Java,
pandeeswari@22934
  1043
              calculate_freight, m_freightcategory_id, freight_currency_id 
carlos@0
  1044
            )
carlos@0
  1045
            VALUES
carlos@0
  1046
            (
carlos@0
  1047
              v_RInOut_ID, Cur_InOut.C_Order_ID, Cur_InOut.IsSOTrx, Cur_InOut.AD_Client_ID,
harikrishnan@7227
  1048
              Cur_InOut.AD_Org_ID, 'Y', now(), v_User,
harikrishnan@7227
  1049
              now(), v_User, v_RDocumentNo, v_DoctypeReversed_ID,
unai@15382
  1050
               '(*R*: ' || Cur_InOut.DocumentNo || ') ' || COALESCE(TO_CHAR(Cur_InOut.Description), ''), 'N', Cur_InOut.MovementType, Cur_InOut.MovementDate,
carlos@0
  1051
              Cur_InOut.DateAcct, Cur_InOut.C_BPartner_ID, Cur_InOut.C_BPartner_Location_ID, Cur_InOut.AD_User_ID,
carlos@0
  1052
              Cur_InOut.M_Warehouse_ID, Cur_InOut.POReference, Cur_InOut.DateOrdered, Cur_InOut.DeliveryRule,
carlos@0
  1053
              Cur_InOut.FreightCostRule, Cur_InOut.FreightAmt * -1, Cur_InOut.C_Project_ID, Cur_InOut.C_Activity_ID,
carlos@0
  1054
              Cur_InOut.C_Campaign_ID, Cur_InOut.AD_OrgTrx_ID, Cur_InOut.User1_ID, Cur_InOut.User2_ID,
eduardo@18857
  1055
              Cur_InOut.C_Costcenter_ID, Cur_InOut.A_Asset_ID,
carlos@0
  1056
              Cur_InOut.DeliveryViaRule, Cur_InOut.M_Shipper_ID, Cur_InOut.C_Charge_ID, Cur_InOut.ChargeAmt * -1,
carlos@0
  1057
              Cur_InOut.PriorityRule, 'DR', 'CO', 'N',
pandeeswari@22934
  1058
               'N', Cur_InOut.islogistic, Cur_InOut.salesrep_id, 'CO',
pandeeswari@22934
  1059
              Cur_InOut.calculate_freight, Cur_InOut.m_freightcategory_id, Cur_InOut.freight_currency_id 
carlos@0
  1060
            )
carlos@0
  1061
            ;
carlos@0
  1062
          v_ResultStr:='InsertInOutLine';
carlos@0
  1063
          FOR Cur_InOutLine IN
carlos@0
  1064
            (SELECT *
carlos@0
  1065
            FROM M_INOUTLINE
carlos@0
  1066
            WHERE M_InOut_ID=Cur_InOut.M_InOut_ID
carlos@0
  1067
              AND IsActive='Y'  FOR UPDATE
carlos@0
  1068
            )
carlos@0
  1069
          LOOP
carlos@0
  1070
            -- Create InOut Line
carlos@0
  1071
            Ad_Sequence_Next('M_InOutLine', Cur_InOut.M_InOut_ID, v_NextNo) ;
carlos@0
  1072
            v_ResultStr:='CreateInOutLine';
carlos@0
  1073
            INSERT
carlos@0
  1074
            INTO M_INOUTLINE
carlos@0
  1075
              (
carlos@0
  1076
                M_InOutLine_ID, Line, M_InOut_ID, C_OrderLine_ID,
carlos@0
  1077
                AD_Client_ID, AD_Org_ID, IsActive, Created,
carlos@0
  1078
                CreatedBy, Updated, UpdatedBy, M_Product_ID,
carlos@0
  1079
                M_AttributeSetInstance_ID, C_UOM_ID, M_Locator_ID, MovementQty,
asier@799
  1080
                Description, IsInvoiced,  --MODIFIED BY F.IRIAZABAL
gorkaion@15487
  1081
                QuantityOrder, M_Product_UOM_ID, IsDescription,
eduardo@18857
  1082
                canceled_inoutline_id, A_Asset_ID, C_Project_ID, C_BPartner_ID,
naiara@19828
  1083
                User1_ID, User2_ID, C_CostCenter_ID, 
naiara@19828
  1084
                explode
carlos@0
  1085
              )
carlos@0
  1086
              VALUES
carlos@0
  1087
              (
carlos@0
  1088
                v_NextNo, Cur_InOutLine.Line, v_RInOut_ID, Cur_InOutLine.C_OrderLine_ID,
carlos@0
  1089
                Cur_InOut.AD_Client_ID, Cur_InOut.AD_Org_ID, 'Y', now(),
harikrishnan@7227
  1090
                v_User, now(), v_User, Cur_InOutLine.M_Product_ID,
carlos@0
  1091
                Cur_InOutLine.M_AttributeSetInstance_ID, Cur_InOutLine.C_UOM_ID, Cur_InOutLine.M_Locator_ID, Cur_InOutLine.MovementQty * -1,
unai@15382
  1092
                 '*R*: ' || COALESCE(TO_CHAR(Cur_InOutLine.Description), ''), Cur_InOutLine.IsInvoiced, --MODIFIED BY F.IRIAZABAL
gorkaion@15487
  1093
                Cur_InOutLine.QuantityOrder * -1, Cur_InOutLine.M_PRODUCT_UOM_ID, Cur_InOutLine.IsDescription,
eduardo@18857
  1094
                Cur_InOutLine.M_InOutLine_ID, Cur_InOutLine.A_Asset_ID, Cur_InOutLine.C_Project_ID, Cur_InOutLine.C_BPartner_ID,
naiara@19828
  1095
                Cur_InOutLine.User1_ID, Cur_InOutLine.User2_ID, Cur_InOutLine.C_CostCenter_ID, 
naiara@19828
  1096
                Cur_InOutLine.explode 
carlos@0
  1097
              )
carlos@0
  1098
              ;
pandeeswari@22049
  1099
pandeeswari@22049
  1100
            -- Create InOut acctounting dimension
pandeeswari@22049
  1101
            v_ResultStr:='CreateInOutLineAcctDimension';
pandeeswari@22049
  1102
            INSERT
pandeeswari@22049
  1103
            INTO M_INOUTLINE_ACCTDIMENSION
pandeeswari@22049
  1104
              (
pandeeswari@22049
  1105
                M_InOutLine_Acctdimension_ID, M_InOutLine_ID, Quantity,
pandeeswari@22049
  1106
                AD_Client_ID, AD_Org_ID, IsActive, Created,
pandeeswari@22049
  1107
                CreatedBy, Updated, UpdatedBy, M_Product_ID, C_BPartner_ID,
pandeeswari@22049
  1108
                C_Project_ID, C_Campaign_ID, C_Activity_ID, A_Asset_ID,
pandeeswari@22049
  1109
                User1_ID, User2_ID, C_CostCenter_ID
pandeeswari@22049
  1110
              )
pandeeswari@22052
  1111
              SELECT
pandeeswari@22052
  1112
                get_uuid(), v_NextNo, Quantity * -1,
pandeeswari@22052
  1113
                AD_Client_ID, AD_Org_ID, 'Y', now(),
pandeeswari@22052
  1114
                v_User, now(), v_User, M_Product_ID, C_BPartner_ID,
pandeeswari@22052
  1115
                C_Project_ID, C_Campaign_ID, C_Activity_ID , A_Asset_ID,
pandeeswari@22052
  1116
                User1_ID, User2_ID, C_CostCenter_ID
pandeeswari@22052
  1117
              FROM M_INOUTLINE_ACCTDIMENSION where M_INOUTLINE_ID=Cur_InOutLine.M_INOUTLINE_ID
pandeeswari@22052
  1118
              and IsActive = 'Y';
pandeeswari@22052
  1119
david@7235
  1120
            INSERT INTO M_MATCHINV
david@7235
  1121
              (M_MATCHINV_ID, AD_CLIENT_ID, AD_ORG_ID, ISACTIVE, CREATED, CREATEDBY, UPDATED, UPDATEDBY,
david@7235
  1122
              M_INOUTLINE_ID, C_INVOICELINE_ID, M_PRODUCT_ID, DATETRX, QTY, PROCESSING, PROCESSED, POSTED)
david@7235
  1123
            SELECT
david@7235
  1124
              GET_UUID(), MI.AD_CLIENT_ID, MI.AD_ORG_ID, MI.ISACTIVE, now(), '0', now(), '0',
david@7240
  1125
              v_NextNo, MI.C_INVOICELINE_ID, MI.M_PRODUCT_ID, MI.DATETRX, -MI.QTY, 'N', 'Y', 'N'
david@7235
  1126
            FROM M_MATCHINV MI
david@7235
  1127
            WHERE MI.M_INOUTLINE_ID = Cur_InOutLine.M_InOutLine_ID;
gorkaion@19377
  1128
carlos@0
  1129
          END LOOP;
carlos@0
  1130
          -- Close Order
carlos@0
  1131
          v_ResultStr:='CloseInOut';
carlos@0
  1132
          UPDATE M_INOUT
carlos@0
  1133
            SET Description=COALESCE(TO_CHAR(Description), '') || ' (*R*=' || v_RDocumentNo || ')',
carlos@0
  1134
            Processed='Y',
ainhoa@2454
  1135
            DocStatus='VO', -- it IS reversed
carlos@0
  1136
            DocAction='--',
mikel@19150
  1137
            Process_Goods_Java='--',
carlos@0
  1138
            Updated=now(),
carlos@0
  1139
            UpdatedBy=v_User
carlos@0
  1140
          WHERE M_INOUT.M_INOUT_ID=Cur_InOut.M_INOUT_ID;
unai@15382
  1141
unai@15382
  1142
	   FOR Cur_InOutLine IN
unai@15382
  1143
            (SELECT *
unai@15382
  1144
            FROM M_INOUTLINE
unai@15382
  1145
            WHERE M_InOut_ID=Cur_InOut.M_InOut_ID
unai@15382
  1146
              AND IsActive='Y'  FOR UPDATE
unai@15382
  1147
            )
unai@15382
  1148
	  LOOP
unai@15382
  1149
            UPDATE M_INOUTLINE
unai@15382
  1150
              SET Description=COALESCE(TO_CHAR(Cur_InOutLine.Description), '') || ' : *R*',
unai@15382
  1151
              Updated=now(),
unai@15382
  1152
              UpdatedBy=v_User
unai@15382
  1153
            WHERE M_INOUTLINE.M_INOUTLINE_ID=Cur_InOutLine.M_INOUTLINE_ID;
alvaro@27102
  1154
alvaro@27102
  1155
	    -- Create new reservation to replace the closed related one
alvaro@27102
  1156
	    FOR Cur_Reservation IN
alvaro@27102
  1157
              (SELECT *
alvaro@27102
  1158
              FROM M_RESERVATION
alvaro@27102
  1159
              WHERE M_RESERVATION.C_OrderLine_ID=Cur_InOutLine.C_OrderLine_ID
alvaro@27102
  1160
              AND IsActive='Y')
alvaro@27102
  1161
	    LOOP
alvaro@27102
  1162
	      IF (Cur_Reservation.Res_Status = 'CL') THEN
alvaro@27102
  1163
david@27103
  1164
		-- Get the ordered quantity from the order line
alvaro@27102
  1165
	        SELECT qtyordered
alvaro@27102
  1166
	        INTO v_qtyordered
alvaro@27102
  1167
	        FROM C_ORDERLINE
alvaro@27102
  1168
	        WHERE C_OrderLine_ID=Cur_Reservation.C_OrderLine_ID;
alvaro@27102
  1169
alvaro@27102
  1170
	        -- Create a new reservation with the ordered quantity
alvaro@27102
  1171
	        INSERT INTO M_RESERVATION (
alvaro@27102
  1172
		  m_reservation_id, ad_client_id, ad_org_id, isactive,
alvaro@27102
  1173
		  created, createdby, updated, updatedby,
david@27103
  1174
		  m_product_id, quantity, c_uom_id, c_orderline_id,
alvaro@27102
  1175
		  ad_user_id, c_bpartner_id, m_warehouse_id, m_attributesetinstance_id, m_locator_id,
alvaro@27102
  1176
		  res_status, res_process, managereservation_pe, reservedgoodmnt_pe
alvaro@27102
  1177
	        ) VALUES (
alvaro@27102
  1178
		  v_uuid, Cur_Reservation.ad_client_id, Cur_Reservation.ad_org_id, 'Y',
alvaro@27102
  1179
		  now(), v_user, now(), v_user,
david@27103
  1180
		  Cur_Reservation.m_product_id, v_qtyordered, Cur_Reservation.c_uom_id, Cur_Reservation.c_orderline_id,
david@27103
  1181
		  Cur_Reservation.ad_user_id, Cur_Reservation.c_bpartner_id, Cur_Reservation.m_warehouse_id,
david@27103
  1182
		  Cur_Reservation.m_attributesetinstance_id, Cur_Reservation.m_locator_id, 'DR', 'PR',
alvaro@27102
  1183
		  Cur_Reservation.managereservation_pe, Cur_Reservation.reservedgoodmnt_pe
alvaro@27102
  1184
	        );
alvaro@27102
  1185
david@27103
  1186
           -- Add a reservation stock with the ordered quantity
david@27103
  1187
      	   INSERT INTO M_RESERVATION_STOCK (
david@27103
  1188
     	      m_reservation_stock_id, ad_client_id, ad_org_id, isactive,
david@27103
  1189
   	        created, createdby, updated, updatedby,
david@27103
  1190
		        m_reservation_id, c_orderline_id, quantity, isallocated,
david@27103
  1191
		        m_locator_id, m_attributesetinstance_id
david@27103
  1192
	        )
david@27103
  1193
          SELECT get_uuid(), ad_client_id, ad_org_id, 'Y',
david@27103
  1194
            now(), v_user, now(), v_user,
david@27103
  1195
            v_uuid, c_orderline_id, v_qtyordered, isallocated,
david@27103
  1196
            m_locator_id, m_attributesetinstance_id
david@27103
  1197
          FROM M_RESERVATION_STOCK
david@27103
  1198
          WHERE M_RESERVATION_STOCK.M_Reservation_ID=Cur_Reservation.M_Reservation_ID
david@27103
  1199
          AND IsActive='Y';
david@27103
  1200
david@27103
  1201
	        -- Process the reservation
alvaro@27102
  1202
	        M_RESERVATION_POST(null, v_uuid, 'PR', v_user);
david@27103
  1203
alvaro@27102
  1204
	      END IF;
alvaro@27102
  1205
	    END LOOP;
david@27103
  1206
unai@15382
  1207
          END LOOP;
unai@15382
  1208
          
carlos@0
  1209
          -- Post Reversal
carlos@0
  1210
          v_ResultStr:='PostReversal';
mikel@19131
  1211
          -- Update reversal goods dates
mikel@19131
  1212
          IF (v_voidmovementdate IS NOT NULL) THEN
mikel@19131
  1213
            UPDATE M_INOUT SET MovementDate = v_voidmovementdate WHERE M_INOUT_ID = v_RInOut_ID;
mikel@19131
  1214
          END IF;
mikel@19131
  1215
          IF (v_voiddate_acct IS NOT NULL) THEN
mikel@19131
  1216
            UPDATE M_INOUT SET DateAcct = v_voiddate_acct WHERE M_INOUT_ID = v_RInOut_ID;
mikel@19131
  1217
          END IF;
antonio@735
  1218
          M_INOUT_POST(NULL, v_RInOut_ID) ;
carlos@0
  1219
          -- Indicate as Reversal Transaction
carlos@0
  1220
          v_ResultStr:='IndicateReversal';
carlos@0
  1221
          UPDATE M_INOUT
carlos@0
  1222
            SET Updated=now(),
carlos@0
  1223
            UpdatedBy=v_User,
ainhoa@2454
  1224
            DocStatus='VO' -- the reversal transaction
carlos@0
  1225
          WHERE M_InOut_ID=v_RInOut_ID;
miguel@24913
  1226
miguel@24913
  1227
          -- transactions related with original inout and with voided inout will be mark as is cost permanent
miguel@24913
  1228
          UPDATE M_TRANSACTION TRX
miguel@24913
  1229
          SET ISCOSTPERMANENT='Y'
miguel@24913
  1230
          WHERE TRX.M_INOUTLINE_ID IN (SELECT M_INOUTLINE_ID 
miguel@24913
  1231
                                       FROM M_INOUTLINE
miguel@24913
  1232
                                       WHERE (M_INOUT_ID = v_RInOut_ID 
miguel@24913
  1233
                                              OR M_INOUT_ID = Cur_InOut.m_inout_id));
carlos@0
  1234
        END IF; -- ReverseCorrection
adrianromero@7702
  1235
adrianromero@7702
  1236
        --M_Inout_Post - Finish_Process Extension Point
adrianromero@7702
  1237
        --Extension point at the end of the M_Inout_Post. It has 5 available parameters Record_ID, DocAction, User, Message and Result
adrianromero@7702
  1238
        SELECT count(*) INTO v_count
adrianromero@7702
  1239
        FROM DUAL
adrianromero@7702
  1240
        where exists (select 1 from ad_ep_procedures where ad_extension_points_id = '5A7C6972321E42C2A5A8E9D6D73E6A7C');
adrianromero@7702
  1241
        IF (v_count=1) THEN
adrianromero@7702
  1242
          DECLARE
adrianromero@7702
  1243
            v_ep_instance VARCHAR2(32);
adrianromero@7702
  1244
            v_extension_point_id VARCHAR2(32) := '5A7C6972321E42C2A5A8E9D6D73E6A7C';
adrianromero@7702
  1245
          BEGIN
adrianromero@7702
  1246
            v_ep_instance := get_uuid();
adrianromero@7702
  1247
            AD_EP_INSTANCE_PARA_INSERT(v_ep_instance, v_extension_point_id, 'Record_ID',
adrianromero@7702
  1248
              v_record_id, NULL, NULL, NULL, NULL, NULL, NULL);
adrianromero@7702
  1249
            AD_EP_INSTANCE_PARA_INSERT(v_ep_instance, v_extension_point_id, 'DocAction',
adrianromero@7702
  1250
              Cur_InOut.DocAction, NULL, NULL, NULL, NULL, NULL, NULL);
adrianromero@7702
  1251
            AD_EP_INSTANCE_PARA_INSERT(v_ep_instance, v_extension_point_id, 'User',
adrianromero@7702
  1252
              v_User, NULL, NULL, NULL, NULL, NULL, NULL);
adrianromero@7702
  1253
            AD_EP_INSTANCE_PARA_INSERT(v_ep_instance, v_extension_point_id, 'Message',
adrianromero@7702
  1254
              NULL, NULL, NULL, NULL, NULL, NULL, v_Message);
adrianromero@7702
  1255
            AD_EP_INSTANCE_PARA_INSERT(v_ep_instance, v_extension_point_id, 'Result',
adrianromero@7702
  1256
              NULL, NULL, v_Result, NULL, NULL, NULL, NULL);
adrianromero@7702
  1257
            AD_EXTENSION_POINT_HANDLER(v_ep_instance, v_extension_point_id);
adrianromero@7702
  1258
            SELECT p_number INTO v_Result
adrianromero@7702
  1259
            FROM ad_ep_instance_para
adrianromero@7702
  1260
            WHERE ad_ep_instance_id = v_ep_instance
adrianromero@7702
  1261
              AND parametername LIKE 'Result';
adrianromero@7702
  1262
            SELECT p_text INTO v_Message
adrianromero@7702
  1263
            FROM ad_ep_instance_para
adrianromero@7702
  1264
            WHERE ad_ep_instance_id = v_ep_instance
adrianromero@7702
  1265
              AND parametername LIKE 'Message';
adrianromero@7702
  1266
adrianromero@7702
  1267
            DELETE FROM ad_ep_instance_para
adrianromero@7702
  1268
            WHERE ad_ep_instance_id = v_ep_instance;
adrianromero@7702
  1269
          END;
adrianromero@7702
  1270
        END IF;
adrianromero@7702
  1271
carlos@0
  1272
      END LOOP; -- InOut Header
carlos@0
  1273
      /**
carlos@0
  1274
      * Transaction End
carlos@0
  1275
      */
carlos@0
  1276
      v_ResultStr:='Fini';
carlos@0
  1277
    END IF; --FINISH_PROCESS
gorkaion@239
  1278
    --<<FINISH_PROCESS>>
carlos@0
  1279
    IF(p_PInstance_ID IS NOT NULL) THEN
carlos@0
  1280
      --  Update AD_PInstance
carlos@0
  1281
      DBMS_OUTPUT.PUT_LINE('Updating PInstance - Finished ' || v_Message) ;
carlos@0
  1282
      AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', v_Result, v_Message) ;
carlos@0
  1283
    ELSE
gorkaion@239
  1284
      DBMS_OUTPUT.PUT_LINE('--<<M_InOut_Post finished>>') ;
carlos@0
  1285
    END IF;
carlos@0
  1286
    RETURN;
carlos@0
  1287
  END; --BODY
carlos@0
  1288
EXCEPTION
carlos@0
  1289
WHEN OTHERS THEN
carlos@0
  1290
  v_ResultStr:= '@ERROR=' || SQLERRM;
carlos@0
  1291
  DBMS_OUTPUT.PUT_LINE(v_ResultStr) ;
carlos@0
  1292
  IF(p_PInstance_ID IS NOT NULL) THEN
carlos@0
  1293
    ROLLBACK;
carlos@0
  1294
    AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', 0, v_ResultStr) ;
carlos@0
  1295
  ELSE
carlos@0
  1296
    RAISE;
carlos@0
  1297
  END IF;
carlos@0
  1298
  RETURN;
antonio@735
  1299
END M_INOUT_POST
juanpablo@3490
  1300
]]></body>
juanpablo@3490
  1301
    </function>
juanpablo@3490
  1302
  </database>