src-db/database/model/functions/COPY_PRODUCT_TEMPLATE.xml
author Juan Pablo Aroztegi <juanpablo.aroztegi@openbravo.com>
Wed, 03 Sep 2008 17:55:37 +0000
changeset 1605 8a0fe0193bef
parent 799 fef2c5e2feb7
permissions -rw-r--r--
Merge r2.5x intro trunk
carlos@0
     1
<?xml version="1.0"?>
adrian@94
     2
  <database name="FUNCTION COPY_PRODUCT_TEMPLATE">
adrian@94
     3
    <function name="COPY_PRODUCT_TEMPLATE" type="NULL">
juanpablo@1605
     4
      <parameter name="pinstance_id" type="VARCHAR" mode="in">
antonio@735
     5
        <default/>
antonio@735
     6
      </parameter>
gorkaion@239
     7
      <body><![CDATA[/*************************************************************************
carlos@0
     8
* The contents of this file are subject to the Openbravo  Public  License
carlos@0
     9
* Version  1.0  (the  "License"),  being   the  Mozilla   Public  License
carlos@0
    10
* Version 1.1  with a permitted attribution clause; you may not  use this
carlos@0
    11
* file except in compliance with the License. You  may  obtain  a copy of
carlos@0
    12
* the License at http://www.openbravo.com/legal/license.html
carlos@0
    13
* Software distributed under the License  is  distributed  on  an "AS IS"
carlos@0
    14
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
carlos@0
    15
* License for the specific  language  governing  rights  and  limitations
carlos@0
    16
* under the License.
carlos@0
    17
* The Original Code is Openbravo ERP.
carlos@0
    18
* The Initial Developer of the Original Code is Openbravo SL
carlos@0
    19
* All portions are Copyright (C) 2001-2006 Openbravo SL
carlos@0
    20
* All Rights Reserved.
carlos@0
    21
* Contributor(s):  ______________________________________.
carlos@0
    22
************************************************************************/
carlos@0
    23
  -- Logistice
carlos@0
    24
  v_ResultStr VARCHAR2(2000):='';
carlos@0
    25
  v_Message VARCHAR2(2000):='';
juanpablo@1605
    26
  v_Record_ID VARCHAR2(32);
carlos@0
    27
  -- Parameter
carlos@0
    28
  TYPE RECORD IS REF CURSOR;
carlos@0
    29
    Cur_Parameter RECORD;
carlos@0
    30
    -- Record Info
juanpablo@1605
    31
    v_Bpartner VARCHAR2(32);
juanpablo@1605
    32
    v_Mpricelist VARCHAR2(32);
juanpablo@1605
    33
    v_Currency VARCHAR2(32);
juanpablo@1605
    34
    v_Warehouse VARCHAR2(32);
carlos@0
    35
    v_SeqNo NUMBER;
juanpablo@1605
    36
    v_CorderId varchar2(32);
juanpablo@1605
    37
    v_Client VARCHAR2(32);
juanpablo@1605
    38
    v_Org VARCHAR2(32);
juanpablo@1605
    39
    v_Createdby VARCHAR2(32);
carlos@0
    40
    v_Dateordered DATE;
carlos@0
    41
    v_BillTo NUMBER;
juanpablo@1605
    42
    v_BpartnerLocation VARCHAR2(32);
juanpablo@1605
    43
    v_Pricelist VARCHAR2(32);
carlos@0
    44
    v_Pricelimit NUMBER;
carlos@0
    45
    v_Pricestd NUMBER;
carlos@0
    46
    v_Linenetamount NUMBER;
juanpablo@1605
    47
    v_Discount VARCHAR2(32);
juanpablo@1605
    48
    v_Tax VARCHAR2(32);
carlos@0
    49
    v_Issotrx CHAR;
carlos@0
    50
    v_NoOfLines NUMBER:=0;
juanpablo@1605
    51
    v_projectID varchar2(32);
carlos@0
    52
    -- Copy
carlos@0
    53
    Cur_ProductTemplate RECORD;
carlos@0
    54
  BEGIN
carlos@0
    55
    --  Update AD_PInstance
carlos@0
    56
    DBMS_OUTPUT.PUT_LINE('Updating PInstance - Processing ' || PInstance_ID) ;
carlos@0
    57
    v_ResultStr:='PInstanceNotFound';
antonio@735
    58
    AD_UPDATE_PINSTANCE(PInstance_ID, NULL, 'Y', NULL, NULL) ;
carlos@0
    59
  BEGIN --BODY
carlos@0
    60
    -- Get Parameters
carlos@0
    61
    v_ResultStr:='ReadingParameters';
carlos@0
    62
    FOR Cur_Parameter IN
carlos@0
    63
      (SELECT i.Record_ID,
carlos@0
    64
        p.ParameterName,
carlos@0
    65
        p.P_String,
carlos@0
    66
        p.P_Number,
carlos@0
    67
        p.P_Date
carlos@0
    68
      FROM AD_PINSTANCE i
carlos@0
    69
      LEFT JOIN AD_PINSTANCE_PARA p
carlos@0
    70
        ON i.AD_PInstance_ID=p.AD_PInstance_ID
carlos@0
    71
      WHERE i.AD_PInstance_ID=PInstance_ID
carlos@0
    72
      ORDER BY p.SeqNo
carlos@0
    73
      )
carlos@0
    74
    LOOP
carlos@0
    75
      v_Record_ID:=Cur_Parameter.Record_ID;
carlos@0
    76
    END LOOP; -- Get Parameter
carlos@0
    77
    DBMS_OUTPUT.PUT_LINE('  Record_ID=' || v_Record_ID) ;
carlos@0
    78
    v_ResultStr:='ReadingOrder';
carlos@0
    79
    SELECT AD_CLIENT_ID, AD_ORG_ID, CREATEDBY, DATEORDERED,
carlos@0
    80
      C_BPARTNER_ID, BILLTO_ID, C_BPARTNER_LOCATION_ID, C_CURRENCY_ID,
carlos@0
    81
      M_WAREHOUSE_ID, M_PRICELIST_ID, C_PROJECT_ID, ISSOTRX
carlos@0
    82
    INTO v_client, v_org, v_Createdby, v_Dateordered,
carlos@0
    83
      v_Bpartner, v_BillTo, v_BpartnerLocation, v_Currency,
carlos@0
    84
      v_Warehouse, v_Mpricelist, v_projectID, v_Issotrx
carlos@0
    85
    FROM C_ORDER
carlos@0
    86
    WHERE C_Order_ID=v_Record_ID;
carlos@0
    87
    SELECT COALESCE(MAX(C_ORDERLINE.line), 10)
carlos@0
    88
    INTO v_SeqNo
carlos@0
    89
    FROM C_ORDERLINE
carlos@0
    90
    WHERE C_ORDERLINE.C_ORDER_ID=v_Record_ID;
carlos@0
    91
    -- Record_ID is the Tab_ID to copy to
carlos@0
    92
    v_ResultStr:='Copying';
carlos@0
    93
    FOR Cur_ProductTemplate IN
carlos@0
    94
      (SELECT M_PRODUCT_TEMPLATE.M_PRODUCT_ID,
carlos@0
    95
        M_PRODUCT_TEMPLATE.QTY,
carlos@0
    96
        M_PRODUCT.C_TaxCategory_Id,
carlos@0
    97
        M_PRODUCT.C_UOM_ID
carlos@0
    98
      FROM M_PRODUCT_TEMPLATE,
carlos@0
    99
        M_PRODUCT
carlos@0
   100
      WHERE M_PRODUCT_TEMPLATE.M_PRODUCT_ID=M_PRODUCT.M_PRODUCT_ID
carlos@0
   101
        AND M_PRODUCT_TEMPLATE.TYPE_TEMPLATE IN(2, 3)
carlos@0
   102
        AND M_PRODUCT_TEMPLATE.C_Bpartner_Id=v_Bpartner
carlos@0
   103
      )
carlos@0
   104
    LOOP
carlos@0
   105
      -- Get prices
carlos@0
   106
      SELECT COALESCE(MAX(M_PRODUCTPRICE.PRICESTD), 0) AS PRICESTD,
carlos@0
   107
        COALESCE(MAX(M_PRODUCTPRICE.PRICELIST), 0) AS PRICELIST,
carlos@0
   108
        COALESCE(MAX(M_PRODUCTPRICE.PRICELIMIT), 0) AS PRICELIMIT
carlos@0
   109
      INTO v_Pricestd,
carlos@0
   110
        v_Pricelist,
carlos@0
   111
        v_Pricelimit
carlos@0
   112
      FROM M_PRICELIST_VERSION,
carlos@0
   113
        M_PRODUCTPRICE
carlos@0
   114
      WHERE M_PRICELIST_VERSION.M_PRICELIST_VERSION_ID=M_PRODUCTPRICE.M_PRICELIST_VERSION_ID
carlos@0
   115
        AND M_PRICELIST_VERSION.VALIDFROM=
carlos@0
   116
        (SELECT MAX(VALIDFROM)
carlos@0
   117
        FROM M_PRICELIST_VERSION
carlos@0
   118
        WHERE ISACTIVE='Y'
carlos@675
   119
          AND VALIDFROM<=now()
carlos@0
   120
          AND M_PRICELIST_ID=v_Mpricelist
carlos@0
   121
        )
carlos@0
   122
        AND M_PRICELIST_VERSION.M_PRICELIST_ID=v_Mpricelist
carlos@0
   123
        AND M_PRODUCTPRICE.M_PRODUCT_ID=Cur_ProductTemplate.M_PRODUCT_ID;
antonio@735
   124
      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) ;
carlos@0
   125
      SELECT C_Gettax(Cur_ProductTemplate.M_PRODUCT_ID, v_Dateordered, v_org, v_Warehouse, v_BillTo, v_BpartnerLocation, v_projectID, v_Issotrx)
carlos@0
   126
      INTO v_tax
carlos@0
   127
      FROM DUAL;
carlos@0
   128
      v_Discount:=(v_Pricelist - v_Pricestd) /v_Pricelist;
carlos@0
   129
      -- Get next no
carlos@0
   130
      Ad_Sequence_Next('C_OrderLine', v_client, v_CorderId) ;
carlos@0
   131
      -- Insert
carlos@0
   132
      INSERT
carlos@0
   133
      INTO C_ORDERLINE
carlos@0
   134
        (
carlos@0
   135
          C_ORDERLINE_ID, AD_CLIENT_ID, AD_ORG_ID, ISACTIVE,
carlos@0
   136
          CREATED, CREATEDBY, UPDATED, UPDATEDBY,
carlos@0
   137
          C_ORDER_ID, LINE, C_BPARTNER_ID, C_BPARTNER_LOCATION_ID,
carlos@0
   138
          DATEORDERED, DATEPROMISED, DATEDELIVERED, DATEINVOICED,
carlos@0
   139
          DESCRIPTION, M_PRODUCT_ID, M_WAREHOUSE_ID, DIRECTSHIP,
carlos@0
   140
          C_UOM_ID, QTYORDERED, QTYRESERVED, QTYDELIVERED,
carlos@0
   141
          QTYINVOICED, M_SHIPPER_ID, C_CURRENCY_ID, PRICELIST,
carlos@0
   142
          PRICEACTUAL, PRICELIMIT, LINENETAMT, DISCOUNT,
carlos@0
   143
          FREIGHTAMT, C_CHARGE_ID, CHARGEAMT, C_TAX_ID,
asier@799
   144
          S_RESOURCEASSIGNMENT_ID, REF_ORDERLINE_ID,
carlos@0
   145
          M_ATTRIBUTESETINSTANCE_ID, ISDESCRIPTION, QUANTITYORDER, M_PRODUCT_UOM_ID,
carlos@0
   146
          PriceStd
carlos@0
   147
        )
carlos@0
   148
        VALUES
carlos@0
   149
        (
carlos@0
   150
          v_CorderId, v_client, v_org, 'Y',
antonio@735
   151
          now(), v_Createdby, now(), v_Createdby,
carlos@0
   152
          v_Record_ID, v_SeqNo, v_Bpartner, v_BpartnerLocation,
carlos@0
   153
          v_Dateordered, NULL, NULL, NULL,
carlos@0
   154
          NULL, Cur_ProductTemplate.M_PRODUCT_ID, v_Warehouse, 'N',
carlos@0
   155
          Cur_ProductTemplate.C_UOM_ID, Cur_ProductTemplate.QTY, 0, 0,
carlos@0
   156
          0, NULL, v_Currency, v_Pricelist,
antonio@735
   157
          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,
carlos@0
   158
          0, NULL, 0, v_tax,
asier@799
   159
          NULL, NULL,
carlos@0
   160
          NULL, 'N', NULL, NULL,
carlos@0
   161
          v_Pricestd
carlos@0
   162
        )
carlos@0
   163
        ;
carlos@0
   164
      -- update translation
carlos@0
   165
      --
carlos@0
   166
      v_SeqNo:=v_SeqNo + 10;
carlos@0
   167
      v_NoOfLines:=v_NoOfLines + 1;
carlos@0
   168
    END LOOP;
carlos@0
   169
    v_Message:='@Copied@=' || v_NoOfLines ;
gorkaion@239
   170
    --<<FINISH_PROCESS>>
carlos@0
   171
    --  Update AD_PInstance
carlos@0
   172
    DBMS_OUTPUT.PUT_LINE('Updating PInstance - Finished ' || v_Message) ;
antonio@735
   173
    AD_UPDATE_PINSTANCE(PInstance_ID, NULL, 'N', 1, v_Message) ;
carlos@0
   174
    RETURN;
carlos@0
   175
  END; --BODY
carlos@0
   176
EXCEPTION
carlos@0
   177
WHEN OTHERS THEN
carlos@0
   178
  v_ResultStr:= '@ERROR=' || SQLERRM;
carlos@0
   179
  DBMS_OUTPUT.PUT_LINE(v_ResultStr) ;
antonio@735
   180
  AD_UPDATE_PINSTANCE(PInstance_ID, NULL, 'N', 0, v_ResultStr) ;
carlos@0
   181
  RETURN;
antonio@735
   182
END COPY_PRODUCT_TEMPLATE
gorkaion@239
   183
]]></body>
adrian@94
   184
    </function>
adrian@94
   185
  </database>