src-db/database/model/functions/M_EXPLODEBOMNOTSTOCK.xml
author Atul Gaware <atul.gaware@openbravo.com>
Tue, 01 Apr 2014 19:21:48 +0530
changeset 22852 8403924dc79d
parent 21926 8d694f6a34a3
child 25697 11a4bc84965f
permissions -rw-r--r--
Fixes Issue 25932 In Sales Order wrong Total Gross Amt for non stocked BOM
naiara@19828
     1
<?xml version="1.0"?>
naiara@19828
     2
  <database name="FUNCTION M_EXPLODEBOMNOTSTOCK">
naiara@19828
     3
    <function name="M_EXPLODEBOMNOTSTOCK" type="NULL">
naiara@19828
     4
      <parameter name="p_pinstance_id" type="VARCHAR" mode="in">
naiara@19828
     5
        <default/>
naiara@19828
     6
      </parameter>
naiara@19828
     7
      <parameter name="p_orderline_id" type="VARCHAR" mode="in">
naiara@19828
     8
        <default><![CDATA[null]]></default>
naiara@19828
     9
      </parameter>
naiara@19828
    10
      <body><![CDATA[/*************************************************************************
naiara@19828
    11
* The contents of this file are subject to the Openbravo  Public  License
naiara@19828
    12
* Version  1.1  (the  "License"),  being   the  Mozilla   Public  License
naiara@19828
    13
* Version 1.1  with a permitted attribution clause; you may not  use this
naiara@19828
    14
* file except in compliance with the License. You  may  obtain  a copy of
naiara@19828
    15
* the License at http://www.openbravo.com/legal/license.html
naiara@19828
    16
* Software distributed under the License  is  distributed  on  an "AS IS"
naiara@19828
    17
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
naiara@19828
    18
* License for the specific  language  governing  rights  and  limitations
naiara@19828
    19
* under the License.
naiara@19828
    20
* The Original Code is Openbravo ERP.
naiara@19828
    21
* The Initial Developer of the Original Code is Openbravo SLU
atul@22852
    22
* All portions are Copyright (C) 2013-2014 Openbravo SLU
naiara@19828
    23
* All Rights Reserved.
naiara@19828
    24
* Contributor(s):  ______________________________________.
naiara@19828
    25
************************************************************************/
naiara@19828
    26
  -- Logistice
naiara@19828
    27
  v_explode CHAR(1);
naiara@19828
    28
  v_orderID VARCHAR2(32);
naiara@19828
    29
  v_CBPartner_ID VARCHAR2(32);
naiara@19828
    30
  v_PriceList_ID VARCHAR2(32);
naiara@19828
    31
  v_PriceList_Version_ID VARCHAR2(32);
naiara@19828
    32
  v_Record_ID VARCHAR2(32);
javier@21919
    33
  v_C_Tax_ID VARCHAR2(32) ;
naiara@19828
    34
  TYPE RECORD IS REF CURSOR;
naiara@19828
    35
  Cur_Parameter RECORD;
naiara@19828
    36
  Cur_MPriceListVersion RECORD;
naiara@19828
    37
  CUR_BOM_Line RECORD;
naiara@19828
    38
  CUR_BOM RECORD;
naiara@19828
    39
  v_Line NUMBER:=0;
naiara@19828
    40
  v_ResultStr VARCHAR2(2000):='';
naiara@19828
    41
  v_result NUMBER:= 1;  
atul@22852
    42
  v_Precision NUMBER;
atul@22852
    43
  v_gross_unit_price NUMBER;
atul@22852
    44
  v_line_gross_amount NUMBER;
atul@22852
    45
  v_price_actual NUMBER;
atul@22852
    46
  v_gross_price_list NUMBER;
atul@22852
    47
  v_line_net_amt NUMBER;
atul@22852
    48
  v_istaxincluded CHAR(1);
atul@22852
    49
  
naiara@19828
    50
  BEGIN
naiara@19828
    51
  IF (p_PInstance_ID IS NOT NULL) THEN
naiara@19828
    52
      --  Update AD_PInstance
naiara@19828
    53
      DBMS_OUTPUT.PUT_LINE('Updating PInstance - Processing ' || p_PInstance_ID) ;
naiara@19828
    54
      v_ResultStr:='PInstanceNotFound';
naiara@19828
    55
      AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'Y', NULL, NULL) ;
naiara@19828
    56
      -- Get Parameters
naiara@19828
    57
      v_ResultStr:='ReadingParameters';
naiara@19828
    58
      FOR Cur_Parameter IN
naiara@19828
    59
        (SELECT i.Record_ID, i.AD_User_ID, p.ParameterName, p.P_String,
naiara@19828
    60
          p.P_Number, p.P_Date
naiara@19828
    61
        FROM AD_PINSTANCE i
naiara@19828
    62
        LEFT JOIN AD_PINSTANCE_PARA p
naiara@19828
    63
          ON i.AD_PInstance_ID=p.AD_PInstance_ID
naiara@19828
    64
        WHERE i.AD_PInstance_ID=p_PInstance_ID
naiara@19828
    65
        ORDER BY p.SeqNo
naiara@19828
    66
        )
naiara@19828
    67
      LOOP
naiara@19828
    68
        v_Record_ID:=Cur_Parameter.Record_ID;
naiara@19828
    69
      END LOOP; -- Get Parameter
naiara@19828
    70
  ELSE
naiara@19828
    71
      v_Record_ID:=p_Orderline_ID;
naiara@19828
    72
  END IF;
naiara@19828
    73
    DBMS_OUTPUT.PUT_LINE('  Record_ID=' || v_Record_ID) ;
naiara@19828
    74
  BEGIN --BODY
naiara@19828
    75
     
naiara@19828
    76
    SELECT C_ORDER.c_order_id, C_ORDER.c_bpartner_id, c_order.M_PriceList_ID, c_orderline.explode
naiara@19828
    77
      INTO v_orderID, v_CBPartner_ID, v_PriceList_ID, v_explode
naiara@19828
    78
    FROM C_ORDER JOIN m_pricelist ON c_order.m_pricelist_id = m_pricelist.m_pricelist_id JOIN c_orderline ON c_order.c_order_id = c_orderline.c_order_id
naiara@19828
    79
    WHERE c_orderline.c_orderline_id=v_Record_ID  FOR UPDATE;
naiara@19828
    80
naiara@19828
    81
    IF (v_explode = 'Y') THEN
naiara@19828
    82
      v_ResultStr:='@ProdExploded@';
naiara@19828
    83
      RAISE_APPLICATION_ERROR(-20000, '@ProdExploded@') ;
naiara@19828
    84
    END IF;
naiara@19828
    85
    
naiara@19828
    86
    v_ResultStr:='Exploding';
naiara@19828
    87
naiara@19828
    88
    -- Get Price List Version
naiara@19828
    89
    FOR Cur_MPriceListVersion IN
atul@22852
    90
      (SELECT v.M_PriceList_Version_ID AS PriceList_Version_ID, o.M_PriceList_ID,
atul@22852
    91
      p.IsTaxIncluded
atul@22852
    92
       FROM M_PRICELIST_VERSION v, C_ORDER o, M_PriceList p
naiara@19828
    93
       WHERE v.M_PriceList_ID=o.M_PriceList_ID
atul@22852
    94
         AND v.M_PriceList_ID = p.M_PriceList_ID
naiara@19828
    95
         AND v.ValidFrom<=o.DateOrdered
naiara@19828
    96
         AND v.IsActive='Y'
naiara@19828
    97
         AND o.C_Order_ID=v_orderID
naiara@19828
    98
       ORDER BY v.ValidFrom DESC
naiara@19828
    99
      )
naiara@19828
   100
    LOOP
naiara@19828
   101
      v_PriceList_Version_ID:=Cur_MPriceListVersion.PriceList_Version_ID;
atul@22852
   102
      v_IsTaxIncluded:=Cur_MPriceListVersion.IsTaxIncluded;
naiara@19828
   103
      EXIT;
naiara@19828
   104
    END LOOP;
naiara@19828
   105
    -- Replace Lines
naiara@19828
   106
    v_ResultStr:='Inserting lines';
javier@21926
   107
    For Cur_Bom_Line In
javier@21926
   108
      (SELECT l.*,o.M_Warehouse_ID as ord_m_warehouse_id,o.C_BPartner_Location_ID as ord_C_BPartner_Location_ID,o.issotrx
javier@21919
   109
       FROM C_ORDERLINE l,c_order o
naiara@19828
   110
       WHERE l.C_Orderline_ID=v_Record_ID
javier@21919
   111
         AND l.c_order_id=o.c_order_id
javier@21919
   112
         AND l.IsActive='Y'
naiara@19828
   113
         AND EXISTS
naiara@19828
   114
           (SELECT *
naiara@19828
   115
            FROM M_PRODUCT p
naiara@19828
   116
            WHERE l.M_Product_ID=p.M_Product_ID
naiara@19828
   117
              AND p.IsBOM='Y'
naiara@19828
   118
              AND p.IsStocked='N'
naiara@19828
   119
            )
naiara@19828
   120
       ORDER BY l.Line  FOR UPDATE
naiara@19828
   121
       )
naiara@19828
   122
    LOOP
naiara@19828
   123
      v_Line:=CUR_BOM_Line.Line;
naiara@19828
   124
      -- Create New Lines
naiara@19828
   125
      FOR CUR_BOM IN
naiara@19828
   126
        (SELECT b.M_ProductBOM_ID, p.C_UOM_ID, b.BOMQty, b.Description
naiara@19828
   127
         FROM M_PRODUCT_BOM b
naiara@19828
   128
              INNER JOIN M_PRODUCT p ON (b.M_ProductBOM_ID=p.M_Product_ID)
naiara@19828
   129
         WHERE b.M_Product_ID=CUR_BOM_Line.M_Product_ID
naiara@19828
   130
           AND b.isactive='Y'
naiara@19828
   131
         ORDER BY Line
naiara@19828
   132
         )
naiara@19828
   133
      LOOP
javier@21926
   134
        V_C_Tax_Id:=C_Gettax(Cur_Bom.M_Productbom_Id, Cur_Bom_Line.Dateordered, 
javier@21926
   135
        Cur_Bom_Line.Ad_Org_Id, Cur_Bom_Line.Ord_M_Warehouse_Id, Cur_Bom_Line.Ord_C_Bpartner_Location_Id, 
javier@21926
   136
        CUR_BOM_Line.ord_C_BPartner_Location_ID, null, CUR_BOM_Line.issotrx) ;
javier@21919
   137
atul@22852
   138
        SELECT PricePrecision INTO v_Precision FROM C_Currency WHERE
atul@22852
   139
        C_Currency_ID = CUR_BOM_Line.C_Currency_ID;
atul@22852
   140
atul@22852
   141
        IF(v_IsTaxIncluded='Y') THEN
atul@22852
   142
					v_gross_unit_price:=M_Get_Offers_Price(CUR_BOM_Line.DateOrdered, v_CBPartner_ID, CUR_BOM.M_ProductBOM_ID, M_BOM_PriceStd(CUR_BOM.M_ProductBOM_ID, v_PriceList_Version_ID), CUR_BOM_Line.QtyInvoiced*CUR_BOM.BOMQty, v_PriceList_ID);
atul@22852
   143
					v_gross_price_list:=M_Get_Offers_Price(CUR_BOM_Line.DateOrdered, v_CBPartner_ID, CUR_BOM.M_ProductBOM_ID, M_BOM_PriceList(CUR_BOM.M_ProductBOM_ID, v_PriceList_Version_ID), CUR_BOM_Line.QtyInvoiced*CUR_BOM.BOMQty, v_PriceList_ID);
atul@22852
   144
					v_line_gross_amount:=ROUND(CUR_BOM_Line.QtyOrdered*CUR_BOM.BOMQty*v_gross_unit_price, v_Precision);
atul@22852
   145
					v_price_actual:=ROUND(C_GET_NET_PRICE_FROM_GROSS(V_C_Tax_Id,  M_Get_Offers_Price(CUR_BOM_Line.DateOrdered, v_CBPartner_ID, CUR_BOM.M_ProductBOM_ID, M_BOM_PriceStd(CUR_BOM.M_ProductBOM_ID, v_PriceList_Version_ID), CUR_BOM_Line.QtyInvoiced*CUR_BOM.BOMQty, v_PriceList_ID), 0, v_Precision, 1),v_Precision);
atul@22852
   146
					v_line_net_amt:=ROUND(CUR_BOM_Line.QtyOrdered*CUR_BOM.BOMQty*v_price_actual, v_Precision);
atul@22852
   147
				ELSE
atul@22852
   148
					v_gross_unit_price:=0;
atul@22852
   149
					v_line_gross_amount:=0;
atul@22852
   150
					v_gross_price_list:=0;
atul@22852
   151
					v_price_actual:=M_Get_Offers_Price(CUR_BOM_Line.DateOrdered, v_CBPartner_ID, CUR_BOM.M_ProductBOM_ID, M_BOM_PriceStd(CUR_BOM.M_ProductBOM_ID, v_PriceList_Version_ID), CUR_BOM_Line.QtyInvoiced*CUR_BOM.BOMQty, v_PriceList_ID);
atul@22852
   152
					v_line_net_amt:=ROUND(CUR_BOM_Line.QtyOrdered*CUR_BOM.BOMQty*v_gross_unit_price, v_Precision);
atul@22852
   153
				END IF;
atul@22852
   154
naiara@19828
   155
        v_Line:=v_Line + 1;
naiara@19828
   156
        INSERT
naiara@19828
   157
        INTO C_ORDERLINE
naiara@19828
   158
          (
naiara@19828
   159
            C_OrderLine_ID, AD_Client_ID, AD_Org_ID, IsActive,
naiara@19828
   160
            Created, CreatedBy, Updated, UpdatedBy,
naiara@19828
   161
            C_Order_ID, Line, C_BPartner_ID, C_BPartner_Location_ID,
naiara@19828
   162
            DateOrdered, DatePromised, DateDelivered, DateInvoiced,
naiara@19828
   163
            Description, M_Product_ID, M_Warehouse_ID, DirectShip,
naiara@19828
   164
            C_UOM_ID, QtyOrdered, QtyReserved, QtyDelivered,
naiara@19828
   165
            QtyInvoiced, M_Shipper_ID, C_Currency_ID,
naiara@19828
   166
            PriceList,
naiara@19828
   167
            PriceActual, PriceLimit, LineNetAmt, Discount,
naiara@19828
   168
            FreightAmt, C_Charge_ID, ChargeAmt, C_Tax_ID,
naiara@19828
   169
            QuantityOrder, M_Product_Uom_ID, PriceStd,
naiara@19828
   170
            c_project_id, a_asset_id, c_costcenter_id,
atul@22852
   171
            user1_id, user2_id, bom_parent_id,
atul@22852
   172
            gross_unit_price, grosspricelist, line_gross_amount
naiara@19828
   173
          )
naiara@19828
   174
        VALUES
naiara@19828
   175
          (
naiara@19828
   176
            get_uuid(), CUR_BOM_Line.AD_Client_ID, CUR_BOM_Line.AD_Org_ID, CUR_BOM_Line.IsActive,
naiara@19828
   177
            now(), '0', now(), '0',
naiara@19828
   178
            CUR_BOM_Line.C_Order_ID, v_Line, CUR_BOM_Line.C_BPartner_ID, CUR_BOM_Line.C_BPartner_Location_ID,
naiara@19828
   179
            CUR_BOM_Line.DateOrdered, CUR_BOM_Line.DatePromised, CUR_BOM_Line.DateDelivered, CUR_BOM_Line.DateInvoiced,
naiara@19828
   180
            CUR_BOM.Description, CUR_BOM.M_ProductBOM_ID, CUR_BOM_Line.M_Warehouse_ID, CUR_BOM_Line.DirectShip,
naiara@19828
   181
            CUR_BOM.C_UOM_ID, CUR_BOM_Line.QtyOrdered*CUR_BOM.BOMQty, CUR_BOM_Line.QtyReserved*CUR_BOM.BOMQty, CUR_BOM_Line.QtyDelivered*CUR_BOM.BOMQty,
naiara@19828
   182
            CUR_BOM_Line.QtyInvoiced*CUR_BOM.BOMQty, CUR_BOM_Line.M_Shipper_ID, CUR_BOM_Line.C_Currency_ID,
naiara@19828
   183
            M_BOM_PriceList(CUR_BOM.M_ProductBOM_ID, v_PriceList_Version_ID),
atul@22852
   184
            v_price_actual, M_BOM_PriceLimit(CUR_BOM.M_ProductBOM_ID, v_PriceList_Version_ID),
atul@22852
   185
            v_line_net_amt, CUR_BOM_Line.Discount,
javier@21919
   186
            CUR_BOM_Line.FreightAmt, CUR_BOM_Line.C_Charge_ID, CUR_BOM_Line.ChargeAmt, coalesce(v_C_Tax_ID,CUR_BOM_Line.c_tax_id),
naiara@19828
   187
            CUR_BOM_Line.QuantityOrder, CUR_BOM_Line.M_Product_UOM_ID, M_BOM_PriceStd(CUR_BOM.M_ProductBOM_ID, v_PriceList_Version_ID),
naiara@19828
   188
            CUR_BOM_Line.c_project_id, CUR_BOM_Line.a_asset_id, CUR_BOM_Line.c_costcenter_id,
atul@22852
   189
            CUR_BOM_Line.user1_id, CUR_BOM_Line.user2_id, v_Record_ID,
atul@22852
   190
            v_gross_unit_price, v_gross_price_list, v_line_gross_amount
naiara@19828
   191
          );
naiara@19828
   192
      END LOOP; -- Create New Lines
naiara@19828
   193
    END LOOP; -- Replace Lines
naiara@19828
   194
    UPDATE c_orderline set explode='Y' where c_orderline_id = v_Record_ID;
naiara@19828
   195
    
naiara@19828
   196
    IF (p_PInstance_ID IS NOT NULL) THEN
naiara@19828
   197
      DBMS_OUTPUT.PUT_LINE('Updating PInstance - Finished ');
naiara@19828
   198
      AD_UPDATE_PINSTANCE(p_pinstance_id, NULL, 'N', v_result, '');
naiara@19828
   199
    END IF;
naiara@19828
   200
   
naiara@19828
   201
  END; --BODY
naiara@19828
   202
EXCEPTION
naiara@19828
   203
WHEN OTHERS THEN
naiara@19828
   204
 DBMS_OUTPUT.PUT_LINE(v_ResultStr) ;
naiara@19828
   205
 v_ResultStr:= '@ERROR=' || SQLERRM;
naiara@19828
   206
  DBMS_OUTPUT.PUT_LINE(v_ResultStr) ;
naiara@19828
   207
  IF(p_PInstance_ID IS NOT NULL) THEN
naiara@19828
   208
    ROLLBACK;
naiara@19828
   209
    AD_UPDATE_PINSTANCE(p_pinstance_id, NULL, 'N', 0, v_resultstr);
naiara@19828
   210
   END IF;
naiara@19828
   211
END M_EXPLODEBOMNOTSTOCK
naiara@19828
   212
]]></body>
naiara@19828
   213
    </function>
naiara@19828
   214
  </database>