src-db/database/model/functions/COPY_PRODUCT_TEMPLATE.xml
author Asier Lostalé <asier.lostale@openbravo.com>
Mon, 05 May 2008 06:59:24 +0000
changeset 799 fef2c5e2feb7
parent 756 ae11e4610537
child 1605 8a0fe0193bef
permissions -rw-r--r--
Merged cleanup branch (r3931) with trunk
<?xml version="1.0"?>
  <database name="FUNCTION COPY_PRODUCT_TEMPLATE">
    <function name="COPY_PRODUCT_TEMPLATE" type="NULL">
      <parameter name="pinstance_id" type="NUMERIC" mode="in">
        <default/>
      </parameter>
      <body><![CDATA[/*************************************************************************
* The contents of this file are subject to the Openbravo  Public  License
* Version  1.0  (the  "License"),  being   the  Mozilla   Public  License
* Version 1.1  with a permitted attribution clause; you may not  use this
* file except in compliance with the License. You  may  obtain  a copy of
* the License at http://www.openbravo.com/legal/license.html
* Software distributed under the License  is  distributed  on  an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific  language  governing  rights  and  limitations
* under the License.
* The Original Code is Openbravo ERP.
* The Initial Developer of the Original Code is Openbravo SL
* All portions are Copyright (C) 2001-2006 Openbravo SL
* All Rights Reserved.
* Contributor(s):  ______________________________________.
************************************************************************/
  -- Logistice
  v_ResultStr VARCHAR2(2000):='';
  v_Message VARCHAR2(2000):='';
  v_Record_ID NUMBER;
  -- Parameter
  TYPE RECORD IS REF CURSOR;
    Cur_Parameter RECORD;
    -- Record Info
    v_Bpartner NUMBER;
    v_Mpricelist NUMBER;
    v_Currency NUMBER;
    v_Warehouse NUMBER;
    v_SeqNo NUMBER;
    v_CorderId NUMBER;
    v_Client NUMBER;
    v_Org NUMBER;
    v_Createdby NUMBER;
    v_Dateordered DATE;
    v_BillTo NUMBER;
    v_BpartnerLocation NUMBER;
    v_Pricelist NUMBER;
    v_Pricelimit NUMBER;
    v_Pricestd NUMBER;
    v_Linenetamount NUMBER;
    v_Discount NUMBER;
    v_Tax NUMBER;
    v_Issotrx CHAR;
    v_NoOfLines NUMBER:=0;
    v_projectID NUMBER;
    -- Copy
    Cur_ProductTemplate RECORD;
  BEGIN
    --  Update AD_PInstance
    DBMS_OUTPUT.PUT_LINE('Updating PInstance - Processing ' || PInstance_ID) ;
    v_ResultStr:='PInstanceNotFound';
    AD_UPDATE_PINSTANCE(PInstance_ID, NULL, 'Y', NULL, NULL) ;
  BEGIN --BODY
    -- Get Parameters
    v_ResultStr:='ReadingParameters';
    FOR Cur_Parameter IN
      (SELECT i.Record_ID,
        p.ParameterName,
        p.P_String,
        p.P_Number,
        p.P_Date
      FROM AD_PINSTANCE i
      LEFT JOIN AD_PINSTANCE_PARA p
        ON i.AD_PInstance_ID=p.AD_PInstance_ID
      WHERE i.AD_PInstance_ID=PInstance_ID
      ORDER BY p.SeqNo
      )
    LOOP
      v_Record_ID:=Cur_Parameter.Record_ID;
    END LOOP; -- Get Parameter
    DBMS_OUTPUT.PUT_LINE('  Record_ID=' || v_Record_ID) ;
    v_ResultStr:='ReadingOrder';
    SELECT AD_CLIENT_ID, AD_ORG_ID, CREATEDBY, DATEORDERED,
      C_BPARTNER_ID, BILLTO_ID, C_BPARTNER_LOCATION_ID, C_CURRENCY_ID,
      M_WAREHOUSE_ID, M_PRICELIST_ID, C_PROJECT_ID, ISSOTRX
    INTO v_client, v_org, v_Createdby, v_Dateordered,
      v_Bpartner, v_BillTo, v_BpartnerLocation, v_Currency,
      v_Warehouse, v_Mpricelist, v_projectID, v_Issotrx
    FROM C_ORDER
    WHERE C_Order_ID=v_Record_ID;
    SELECT COALESCE(MAX(C_ORDERLINE.line), 10)
    INTO v_SeqNo
    FROM C_ORDERLINE
    WHERE C_ORDERLINE.C_ORDER_ID=v_Record_ID;
    -- Record_ID is the Tab_ID to copy to
    v_ResultStr:='Copying';
    FOR Cur_ProductTemplate IN
      (SELECT M_PRODUCT_TEMPLATE.M_PRODUCT_ID,
        M_PRODUCT_TEMPLATE.QTY,
        M_PRODUCT.C_TaxCategory_Id,
        M_PRODUCT.C_UOM_ID
      FROM M_PRODUCT_TEMPLATE,
        M_PRODUCT
      WHERE M_PRODUCT_TEMPLATE.M_PRODUCT_ID=M_PRODUCT.M_PRODUCT_ID
        AND M_PRODUCT_TEMPLATE.TYPE_TEMPLATE IN(2, 3)
        AND M_PRODUCT_TEMPLATE.C_Bpartner_Id=v_Bpartner
      )
    LOOP
      -- Get prices
      SELECT COALESCE(MAX(M_PRODUCTPRICE.PRICESTD), 0) AS PRICESTD,
        COALESCE(MAX(M_PRODUCTPRICE.PRICELIST), 0) AS PRICELIST,
        COALESCE(MAX(M_PRODUCTPRICE.PRICELIMIT), 0) AS PRICELIMIT
      INTO v_Pricestd,
        v_Pricelist,
        v_Pricelimit
      FROM M_PRICELIST_VERSION,
        M_PRODUCTPRICE
      WHERE M_PRICELIST_VERSION.M_PRICELIST_VERSION_ID=M_PRODUCTPRICE.M_PRICELIST_VERSION_ID
        AND M_PRICELIST_VERSION.VALIDFROM=
        (SELECT MAX(VALIDFROM)
        FROM M_PRICELIST_VERSION
        WHERE ISACTIVE='Y'
          AND VALIDFROM<=now()
          AND M_PRICELIST_ID=v_Mpricelist
        )
        AND M_PRICELIST_VERSION.M_PRICELIST_ID=v_Mpricelist
        AND M_PRODUCTPRICE.M_PRODUCT_ID=Cur_ProductTemplate.M_PRODUCT_ID;
      v_Linenetamount:=C_Currency_Round(M_Get_Offers_Price(now(), v_Bpartner, Cur_ProductTemplate.M_Product_ID, v_Pricestd,Cur_ProductTemplate.QTY,v_Mpricelist ) *Cur_ProductTemplate.QTY, v_Currency, NULL) ;
      SELECT C_Gettax(Cur_ProductTemplate.M_PRODUCT_ID, v_Dateordered, v_org, v_Warehouse, v_BillTo, v_BpartnerLocation, v_projectID, v_Issotrx)
      INTO v_tax
      FROM DUAL;
      v_Discount:=(v_Pricelist - v_Pricestd) /v_Pricelist;
      -- Get next no
      Ad_Sequence_Next('C_OrderLine', v_client, v_CorderId) ;
      -- Insert
      INSERT
      INTO C_ORDERLINE
        (
          C_ORDERLINE_ID, AD_CLIENT_ID, AD_ORG_ID, ISACTIVE,
          CREATED, CREATEDBY, UPDATED, UPDATEDBY,
          C_ORDER_ID, LINE, C_BPARTNER_ID, C_BPARTNER_LOCATION_ID,
          DATEORDERED, DATEPROMISED, DATEDELIVERED, DATEINVOICED,
          DESCRIPTION, M_PRODUCT_ID, M_WAREHOUSE_ID, DIRECTSHIP,
          C_UOM_ID, QTYORDERED, QTYRESERVED, QTYDELIVERED,
          QTYINVOICED, M_SHIPPER_ID, C_CURRENCY_ID, PRICELIST,
          PRICEACTUAL, PRICELIMIT, LINENETAMT, DISCOUNT,
          FREIGHTAMT, C_CHARGE_ID, CHARGEAMT, C_TAX_ID,
          S_RESOURCEASSIGNMENT_ID, REF_ORDERLINE_ID,
          M_ATTRIBUTESETINSTANCE_ID, ISDESCRIPTION, QUANTITYORDER, M_PRODUCT_UOM_ID,
          PriceStd
        )
        VALUES
        (
          v_CorderId, v_client, v_org, 'Y',
          now(), v_Createdby, now(), v_Createdby,
          v_Record_ID, v_SeqNo, v_Bpartner, v_BpartnerLocation,
          v_Dateordered, NULL, NULL, NULL,
          NULL, Cur_ProductTemplate.M_PRODUCT_ID, v_Warehouse, 'N',
          Cur_ProductTemplate.C_UOM_ID, Cur_ProductTemplate.QTY, 0, 0,
          0, NULL, v_Currency, v_Pricelist,
          M_Get_Offers_Price(now(), v_Bpartner, Cur_ProductTemplate.M_Product_ID, v_Pricestd, Cur_ProductTemplate.QTY, v_Mpricelist), v_Pricelimit, v_Linenetamount, v_Discount,
          0, NULL, 0, v_tax,
          NULL, NULL,
          NULL, 'N', NULL, NULL,
          v_Pricestd
        )
        ;
      -- update translation
      --
      v_SeqNo:=v_SeqNo + 10;
      v_NoOfLines:=v_NoOfLines + 1;
    END LOOP;
    v_Message:='@Copied@=' || v_NoOfLines ;
    --<<FINISH_PROCESS>>
    --  Update AD_PInstance
    DBMS_OUTPUT.PUT_LINE('Updating PInstance - Finished ' || v_Message) ;
    AD_UPDATE_PINSTANCE(PInstance_ID, NULL, 'N', 1, v_Message) ;
    RETURN;
  END; --BODY
EXCEPTION
WHEN OTHERS THEN
  v_ResultStr:= '@ERROR=' || SQLERRM;
  DBMS_OUTPUT.PUT_LINE(v_ResultStr) ;
  AD_UPDATE_PINSTANCE(PInstance_ID, NULL, 'N', 0, v_ResultStr) ;
  RETURN;
END COPY_PRODUCT_TEMPLATE
]]></body>
    </function>
  </database>