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