src-db/database/model/functions/M_INOUT_POST.xml
author Atul Gaware <atul.gaware@openbravo.com>
Tue, 14 Oct 2014 15:12:25 +0530
changeset 25219 8c58f8ee82a3
parent 25185 2352f43d4f0f
child 25286 d7cf69d29673
permissions -rw-r--r--
Fixes Issue 27752:Cannot ship more than the ordered quantity when Is Quantity
Variable checkbox is selected for the product

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