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