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