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