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