Some procedure and functions have been renamed or deleted due to changes in the used prefixes.
authorGorka Ion Damián <gorkaion.damian@openbravo.com>
Thu, 09 Oct 2008 17:15:25 +0000
changeset 1787 84217f37d788
parent 1786 20694e60728c
child 1788 5d8538e39f1d
Some procedure and functions have been renamed or deleted due to changes in the used prefixes.

Modified functions are: (old name, new name)

ADD_MONTHS2 C_ADD_MONTHS2
AGING_GET_SCOPE C_AGING_GET_SCOPE
BOM_PRICELIMIT M_BOM_PRICELIMIT
BOM_PRICELIST M_BOM_PRICELIST
BOM_PRICESTD M_BOM_PRICESTD
BOM_QTY_AVAILABLE M_BOM_QTY_AVAILABLE
BOM_QTY_ONHAND M_BOM_QTY_ONHAND
BOM_QTY_ORDERED M_BOM_QTY_ORDERED
BOM_QTY_RESERVED M_BOM_QTY_RESERVED
CHANGEPROJECTSTATUSTOORDER C_CHANGEPROJECTSTATUSTOORDER
CHANGEPROJECTSTATUS C_CHANGEPROJECTSTATUS
CLOSEPROJECT C_CLOSEPROJECT
COPY_PRODUCT_TEMPLATE M_COPY_PRODUCT_TEMPLATE
COPY_PROJECT_LINES C_COPY_PROJECT_LINES
DBA_USER_CONST_SEARCHCOND AD_DBA_USER_CONST_SEARCHCOND
DEBT_PAYMENT_BALANCING_REPLACE C_DEBT_PAYMENT_BALANCING_REPLACE
DIVIDE C_DIVIDE
GENERATE_AVERAGE_COSTS M_GENERATE_AVERAGE_COSTS
GENERATEPOFROMPROJECT C_GENERATEPOFROMPROJECT
GENERATESOFROMPROJECTORDER C_GENERATESOFROMPROJECTORDER
GENERATESOFROMPROJECTPHASE C_GENERATESOFROMPROJECTPHASE
GENERATESOFROMPROJECT C_GENERATESOFROMPROJECT
GENERATE_STANDAR_COSTS M_GENERATE_STANDAR_COSTS
GET_PARETO_ABC M_GET_PARETO_ABC
GETPERIODNAME C_GETPERIODNAME
GET_PRICELIST_VERSION M_GET_PRICELIST_VERSION
GET_PRODUCT_COST M_GET_PRODUCT_COST
GET_PRODUCT_PRICE_DATE M_GET_PRODUCT_PRICE_DATE
GET_PRODUCT_PRICE M_GET_PRODUCT_PRICE
GET_PURCHASE_PRICE M_GET_PURCHASE_PRICE
ID_DEVELOPMENT_RANGE deleted
IMPORT_PRODUCT deleted
NUMBER_NULL deleted
TEMPLATE deleted
W_REQUEST_BPARTNER deleted
src-db/database/model/functions/ADD_MONTHS2.xml
src-db/database/model/functions/AD_DBA_USER_CONST_SEARCHCOND.xml
src-db/database/model/functions/AGING_GET_SCOPE.xml
src-db/database/model/functions/BOM_PRICELIMIT.xml
src-db/database/model/functions/BOM_PRICELIST.xml
src-db/database/model/functions/BOM_PRICESTD.xml
src-db/database/model/functions/BOM_QTY_AVAILABLE.xml
src-db/database/model/functions/BOM_QTY_ONHAND.xml
src-db/database/model/functions/BOM_QTY_ORDERED.xml
src-db/database/model/functions/BOM_QTY_RESERVED.xml
src-db/database/model/functions/CHANGEPROJECTSTATUS.xml
src-db/database/model/functions/CHANGEPROJECTSTATUSTOORDER.xml
src-db/database/model/functions/CLOSEPROJECT.xml
src-db/database/model/functions/COPY_PRODUCT_TEMPLATE.xml
src-db/database/model/functions/COPY_PROJECT_LINES.xml
src-db/database/model/functions/C_ADD_MONTHS2.xml
src-db/database/model/functions/C_AGING_GET_SCOPE.xml
src-db/database/model/functions/C_CHANGEPROJECTSTATUS.xml
src-db/database/model/functions/C_CHANGEPROJECTSTATUSTOORDER.xml
src-db/database/model/functions/C_CLOSEPROJECT.xml
src-db/database/model/functions/C_COPY_PROJECT_LINES.xml
src-db/database/model/functions/C_DIVIDE.xml
src-db/database/model/functions/C_GENERATEPOFROMPROJECT.xml
src-db/database/model/functions/C_GENERATESOFROMPROJECT.xml
src-db/database/model/functions/C_GENERATESOFROMPROJECTORDER.xml
src-db/database/model/functions/C_GENERATESOFROMPROJECTPHASE.xml
src-db/database/model/functions/C_GETPERIODNAME.xml
src-db/database/model/functions/C_INVOICE_PERCENTAGE_PAID.xml
src-db/database/model/functions/C_INVOICE_POST.xml
src-db/database/model/functions/C_ORDER_POST1.xml
src-db/database/model/functions/C_PAYMENTDUEDATE.xml
src-db/database/model/functions/DBA_USER_CONST_SEARCHCOND.xml
src-db/database/model/functions/DEBT_PAYMENT_BALANCING_REPLACE.xml
src-db/database/model/functions/DIVIDE.xml
src-db/database/model/functions/GENERATEPOFROMPROJECT.xml
src-db/database/model/functions/GENERATESOFROMPROJECT.xml
src-db/database/model/functions/GENERATESOFROMPROJECTORDER.xml
src-db/database/model/functions/GENERATESOFROMPROJECTPHASE.xml
src-db/database/model/functions/GENERATE_AVERAGE_COSTS.xml
src-db/database/model/functions/GENERATE_STANDAR_COSTS.xml
src-db/database/model/functions/GETPERIODNAME.xml
src-db/database/model/functions/GET_PARETO_ABC.xml
src-db/database/model/functions/GET_PRICELIST_VERSION.xml
src-db/database/model/functions/GET_PRODUCT_COST.xml
src-db/database/model/functions/GET_PRODUCT_PRICE.xml
src-db/database/model/functions/GET_PRODUCT_PRICE_DATE.xml
src-db/database/model/functions/GET_PURCHASE_PRICE.xml
src-db/database/model/functions/ID_DEVELOPMENT_RANGE.xml
src-db/database/model/functions/IMPORT_PRODUCT.xml
src-db/database/model/functions/MA_STANDARD_COST_SEQUENCE.xml
src-db/database/model/functions/MA_WORKEFFORT_VALIDATE.xml
src-db/database/model/functions/M_BOM_PRICELIMIT.xml
src-db/database/model/functions/M_BOM_PRICELIST.xml
src-db/database/model/functions/M_BOM_PRICESTD.xml
src-db/database/model/functions/M_BOM_QTY_AVAILABLE.xml
src-db/database/model/functions/M_BOM_QTY_ONHAND.xml
src-db/database/model/functions/M_BOM_QTY_ORDERED.xml
src-db/database/model/functions/M_BOM_QTY_RESERVED.xml
src-db/database/model/functions/M_COPY_PRODUCT_TEMPLATE.xml
src-db/database/model/functions/M_GENERATE_AVERAGE_COSTS.xml
src-db/database/model/functions/M_GENERATE_STANDARD_COSTS.xml
src-db/database/model/functions/M_GET_PARETO_ABC.xml
src-db/database/model/functions/M_GET_PRICELIST_VERSION.xml
src-db/database/model/functions/M_GET_PRODUCT_COST.xml
src-db/database/model/functions/M_GET_PRODUCT_PRICE.xml
src-db/database/model/functions/M_GET_PRODUCT_PRICE_DATE.xml
src-db/database/model/functions/M_GET_PURCHASE_PRICE.xml
src-db/database/model/functions/M_PRODUCTION_RUN.xml
src-db/database/model/functions/M_REQUISITION_CREATEPO.xml
src-db/database/model/functions/M_UPDATE_PARETO_PRODUCT.xml
src-db/database/model/functions/NUMBER_NULL.xml
src-db/database/model/functions/TEMPLATE.xml
src-db/database/model/functions/W_REQUEST_BPARTNER.xml
src-db/database/sourcedata/AD_PROCESS.xml
src/org/openbravo/erpCommon/ad_actionButton/CopyFromOrder_data.xsql
src/org/openbravo/erpCommon/ad_actionButton/CreateFrom_Invoice_data.xsql
src/org/openbravo/erpCommon/ad_actionButton/CreateFrom_Shipment_data.xsql
src/org/openbravo/erpCommon/ad_actionButton/DropCloseFactAcct_data.xsql
src/org/openbravo/erpCommon/ad_actionButton/DropRegFactAcct_data.xsql
src/org/openbravo/erpCommon/ad_actionButton/ExpenseSOrder_data.xsql
src/org/openbravo/erpCommon/ad_callouts/SE_Expense_Product_data.xsql
src/org/openbravo/erpCommon/ad_callouts/SE_ProjectLine_Value_data.xsql
src/org/openbravo/erpCommon/ad_callouts/SL_RequisitionLine_Product_data.xsql
src/org/openbravo/erpCommon/ad_forms/ProductInfo_data.xsql
src/org/openbravo/erpCommon/ad_forms/RequisitionToOrder_data.xsql
src/org/openbravo/erpCommon/ad_process/ExpenseAPInvoice_data.xsql
src/org/openbravo/erpCommon/ad_process/ProductPrice_data.xsql
src/org/openbravo/erpCommon/ad_reports/ReportAgingBalance_data.xsql
src/org/openbravo/erpCommon/ad_reports/ReportDimensionalAnalysesPDF.java
src/org/openbravo/erpCommon/ad_reports/ReportInvoiceCustomerDimensionalAnalysesComparativeJR.jrxml
src/org/openbravo/erpCommon/ad_reports/ReportInvoiceCustomerDimensionalAnalysesJR_data.xsql
src/org/openbravo/erpCommon/ad_reports/ReportInvoiceCustomerDimensionalAnalysesNoComparativeJR.jrxml
src/org/openbravo/erpCommon/ad_reports/ReportInvoiceCustomerDimensionalAnalyses_data.xsql
src/org/openbravo/erpCommon/ad_reports/ReportInvoiceDiscount_data.xsql
src/org/openbravo/erpCommon/ad_reports/ReportParetoProduct_data.xsql
src/org/openbravo/erpCommon/ad_reports/ReportRefundInvoiceCustomerDimensionalAnalyses.java
src/org/openbravo/erpCommon/ad_reports/ReportRefundInvoiceCustomerDimensionalAnalyses_data.xsql
src/org/openbravo/erpCommon/ad_reports/ReportRefundSalesDimensionalAnalyses.java
src/org/openbravo/erpCommon/ad_reports/ReportRefundSalesDimensionalAnalysesPDF.java
src/org/openbravo/erpCommon/ad_reports/ReportRefundSalesDimensionalAnalyses_data.xsql
src/org/openbravo/erpCommon/ad_reports/ReportValuationStock_data.xsql
src/org/openbravo/erpCommon/info/Product_data.xsql
src/org/openbravo/erpCommon/utility/ErrorTextParserORACLE_data.xsql
src/org/openbravo/erpCommon/utility/ErrorTextParserPOSTGRE_data.xsql
src/org/openbravo/erpCommon/ws/externalSales/ExternalSalesProduct_data.xsql
--- a/src-db/database/model/functions/ADD_MONTHS2.xml	Thu Oct 09 14:13:04 2008 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,46 +0,0 @@
-<?xml version="1.0"?>
-  <database name="FUNCTION ADD_MONTHS2">
-    <function name="ADD_MONTHS2" type="TIMESTAMP">
-      <parameter name="pdate" type="TIMESTAMP" mode="in">
-        <default/>
-      </parameter>
-      <parameter name="pmonths" 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) 2007 Openbravo SL
-* All Rights Reserved.
-* Contributor(s):  ______________________________________.
-************************************************************************/
-/*************************************************************************
-* Description: This function is is similar to oracle's add_months function
-*  In case the DATE is i.e. 28/02/2007 this function will return 28/03/2007
-*  while add_moths returns 31/03/2007
-************************************************************************/
-  pDateAux DATE;
-BEGIN
-  if (last_day(pDate)!=pDate) then
-    return add_months(pdate, pMonths);
-  else
-    pDateAux := add_months(pDate, pMonths);
-    if (to_number(to_char(pDateAux,'DD'))>to_number(to_char(pDate,'DD'))) then
-      return TO_DATE(to_char(pDate,'DD')||to_char(pDateAux,'-MM-YYYY'),'DD-MM-YYYY');
-    else
-      return pDateAux;
-    end if;
-  end if;
-END ADD_MONTHS2
-]]></body>
-    </function>
-  </database>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src-db/database/model/functions/AD_DBA_USER_CONST_SEARCHCOND.xml	Thu Oct 09 17:15:25 2008 +0000
@@ -0,0 +1,41 @@
+<?xml version="1.0"?>
+  <database name="FUNCTION AD_DBA_USER_CONST_SEARCHCOND">
+    <function name="AD_DBA_USER_CONST_SEARCHCOND" type="VARCHAR">
+      <parameter name="p_constraintname" type="VARCHAR" 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):  ______________________________________.
+************************************************************************/
+  v_varchar2 VARCHAR2(32767) ;
+  TYPE RECORD IS REF CURSOR;
+    cur_searchCond RECORD;
+  BEGIN
+    FOR cur_searchCond IN
+      (SELECT SEARCH_CONDITION
+      FROM USER_CONSTRAINTS
+      WHERE CONSTRAINT_NAME=p_constraintName
+      )
+    LOOP
+      v_varchar2 := cur_searchCond.SEARCH_CONDITION;
+      v_varchar2 := TRIM(SUBSTR(v_varchar2, 1, 4000)) ;
+      EXIT;
+    END LOOP;
+    RETURN(v_varchar2) ;
+END AD_DBA_USER_CONST_SEARCHCOND
+]]></body>
+    </function>
+  </database>
--- a/src-db/database/model/functions/AGING_GET_SCOPE.xml	Thu Oct 09 14:13:04 2008 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,58 +0,0 @@
-<?xml version="1.0"?>
-  <database name="FUNCTION AGING_GET_SCOPE">
-    <function name="AGING_GET_SCOPE" type="NUMERIC">
-      <parameter name="pdate" type="TIMESTAMP" mode="in">
-        <default/>
-      </parameter>
-      <parameter name="pcol1" type="NUMERIC" mode="in">
-        <default/>
-      </parameter>
-      <parameter name="pcol2" type="NUMERIC" mode="in">
-        <default/>
-      </parameter>
-      <parameter name="pcol3" type="NUMERIC" mode="in">
-        <default/>
-      </parameter>
-      <parameter name="pcol4" 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) 2008 Openbravo SL
-* All Rights Reserved.
-* Contributor(s):  ______________________________________.
-************************************************************************/
-/*************************************************************************
-* Description: This function is is similar to oracle's add_months function
-*  In case the DATE is i.e. 28/02/2007 this function will return 28/03/2007
-*  while add_moths returns 31/03/2007
-************************************************************************/
-  pDateAux DATE;
-BEGIN
-  if (pDate<now()) then
-    return 0;
-  elsif (pDate<=now()+pCol1) then
-    return 1;
-  elsif (pDate<=now()+pCol2) then
-    return 2;
-  elsif (pDate<=now()+pCol3) then
-    return 3;
-  elsif (pDate<=now()+pCol4) then
-    return 4;
-  else
-    return 5;
-  end if;
-END AGING_GET_SCOPE
-]]></body>
-    </function>
-  </database>
--- a/src-db/database/model/functions/BOM_PRICELIMIT.xml	Thu Oct 09 14:13:04 2008 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,59 +0,0 @@
-<?xml version="1.0"?>
-  <database name="FUNCTION BOM_PRICELIMIT">
-    <function name="BOM_PRICELIMIT" type="NUMERIC">
-      <parameter name="p_product_id" type="VARCHAR" mode="in">
-        <default/>
-      </parameter>
-      <parameter name="p_pricelist_version_id" type="VARCHAR" mode="in">
-        <default/>
-      </parameter>
-      <body><![CDATA[/******************************************************************************
-* The contents of this file are subject to the   Compiere License  Version 1.1
-* ("License"); You may not use this file except in compliance with the License
-* You may obtain a copy of the License at http://www.compiere.org/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                  Compiere  ERP &  Business Solution
-* The Initial Developer of the Original Code is Jorg Janke  and ComPiere, Inc.
-* Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke, parts
-* created by ComPiere are Copyright (C) ComPiere, Inc.;   All Rights Reserved.
-* Contributor(s): Openbravo SL
-* Contributions are Copyright (C) 2001-2006 Openbravo S.L.
-******************************************************************************/
-  v_Price        NUMBER;
-  v_ProductPrice NUMBER;
-  -- Get BOM Product info
-  TYPE RECORD IS REF CURSOR;
-    CUR_BOM RECORD;
-    --
-  BEGIN
-    -- Try to get price from PriceList directly
-    SELECT COALESCE(SUM(PriceLimit), 0)
-    INTO v_Price
-    FROM M_ProductPrice
-    WHERE M_PriceList_Version_ID = p_PriceList_Version_ID
-      AND M_Product_ID = p_Product_ID;
-    -- DBMS_OUTPUT.PUT_LINE('Price=' || v_Price);
-    -- No Price - Check if BOM
-    IF(v_Price = 0) THEN
-      FOR CUR_BOM IN
-        (SELECT b.M_ProductBOM_ID,
-          b.BOMQty,
-          p.IsBOM
-        FROM M_Product_BOM b,
-          M_Product p
-        WHERE b.M_ProductBOM_ID = p.M_Product_ID
-          AND b.M_Product_ID = p_Product_ID
-        )
-      LOOP
-        v_ProductPrice := BOM_PriceLimit(CUR_BOM.M_ProductBOM_ID, p_PriceList_Version_ID) ;
-        v_Price := v_Price +(CUR_BOM.BOMQty * v_ProductPrice) ;
-      END LOOP;
-    END IF;
-    --
-    RETURN v_Price;
-END BOM_PRICELIMIT
-]]></body>
-    </function>
-  </database>
--- a/src-db/database/model/functions/BOM_PRICELIST.xml	Thu Oct 09 14:13:04 2008 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,60 +0,0 @@
-<?xml version="1.0"?>
-  <database name="FUNCTION BOM_PRICELIST">
-    <function name="BOM_PRICELIST" type="NUMERIC">
-      <parameter name="product_id" type="VARCHAR" mode="in">
-        <default/>
-      </parameter>
-      <parameter name="pricelist_version_id" type="VARCHAR" mode="in">
-        <default/>
-      </parameter>
-      <body><![CDATA[/******************************************************************************
-* The contents of this file are subject to the   Compiere License  Version 1.1
-* ("License"); You may not use this file except in compliance with the License
-* You may obtain a copy of the License at http://www.compiere.org/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                  Compiere  ERP &  Business Solution
-* The Initial Developer of the Original Code is Jorg Janke  and ComPiere, Inc.
-* Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke, parts
-* created by ComPiere are Copyright (C) ComPiere, Inc.;   All Rights Reserved.
-* Contributor(s): Openbravo SL
-* Contributions are Copyright (C) 2001-2006 Openbravo S.L.
-******************************************************************************/
-  v_Price        NUMBER;
-  v_ProductPrice NUMBER;
-  -- Get BOM Product info
-  TYPE RECORD IS REF CURSOR;
-    CUR_BOM RECORD;
-    --
-  BEGIN
-    -- Try to get price from pricelist directly
-    SELECT COALESCE(SUM(PriceList), 0)
-    INTO v_Price
-    FROM M_ProductPrice
-    WHERE M_PriceList_Version_ID=PriceList_Version_ID
-      AND M_Product_ID=Product_ID;
-    -- DBMS_OUTPUT.PUT_LINE('Price=' || v_Price);
-    -- No Price - Check if BOM
-    IF(v_Price=0) THEN
-      FOR CUR_BOM IN
-        (SELECT b.M_ProductBOM_ID,
-          b.BOMQty,
-          p.IsBOM
-        FROM M_Product_BOM b,
-          M_Product p
-        WHERE b.M_ProductBOM_ID=p.M_Product_ID
-          AND b.M_Product_ID=Product_ID
-        )
-      LOOP
-        v_ProductPrice:=BOM_PriceList(CUR_BOM.M_ProductBOM_ID, PriceList_Version_ID) ;
-        v_Price:=v_Price +(CUR_BOM.BOMQty * v_ProductPrice) ;
-        -- DBMS_OUTPUT.PUT_LINE('Qry=' || CUR_BOM.BOMQty || ' @ ' || v_ProductPrice || ', Price=' || v_Price);
-      END LOOP; -- BOM
-    END IF;
-    --
-    RETURN v_Price;
-END BOM_PRICELIST
-]]></body>
-    </function>
-  </database>
--- a/src-db/database/model/functions/BOM_PRICESTD.xml	Thu Oct 09 14:13:04 2008 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,60 +0,0 @@
-<?xml version="1.0"?>
-  <database name="FUNCTION BOM_PRICESTD">
-    <function name="BOM_PRICESTD" type="NUMERIC">
-      <parameter name="product_id" type="VARCHAR" mode="in">
-        <default/>
-      </parameter>
-      <parameter name="pricelist_version_id" type="VARCHAR" mode="in">
-        <default/>
-      </parameter>
-      <body><![CDATA[/******************************************************************************
-* The contents of this file are subject to the   Compiere License  Version 1.1
-* ("License"); You may not use this file except in compliance with the License
-* You may obtain a copy of the License at http://www.compiere.org/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                  Compiere  ERP &  Business Solution
-* The Initial Developer of the Original Code is Jorg Janke  and ComPiere, Inc.
-* Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke, parts
-* created by ComPiere are Copyright (C) ComPiere, Inc.;   All Rights Reserved.
-* Contributor(s): Openbravo SL
-* Contributions are Copyright (C) 2001-2006 Openbravo S.L.
-******************************************************************************/
-  v_Price        NUMBER;
-  v_ProductPrice NUMBER;
-  -- Get BOM Product info
-  TYPE RECORD IS REF CURSOR;
-    CUR_BOM RECORD;
-    --
-  BEGIN
-    -- Try to get price from pricelist directly
-    SELECT COALESCE(SUM(PriceStd), 0)
-    INTO v_Price
-    FROM M_ProductPrice
-    WHERE M_PriceList_Version_ID=PriceList_Version_ID
-      AND M_Product_ID=Product_ID;
-    -- DBMS_OUTPUT.PUT_LINE('Price=' || v_Price);
-    -- No Price - Check if BOM
-    IF(v_Price=0) THEN
-      FOR CUR_BOM IN
-        (SELECT b.M_ProductBOM_ID,
-          b.BOMQty,
-          p.IsBOM
-        FROM M_Product_BOM b,
-          M_Product p
-        WHERE b.M_ProductBOM_ID=p.M_Product_ID
-          AND b.M_Product_ID=Product_ID
-        )
-      LOOP
-        v_ProductPrice:=BOM_PriceStd(CUR_BOM.M_ProductBOM_ID, PriceList_Version_ID) ;
-        v_Price:=v_Price +(CUR_BOM.BOMQty * v_ProductPrice) ;
-        -- DBMS_OUTPUT.PUT_LINE('Price=' || v_Price);
-      END LOOP; -- BOM
-    END IF;
-    --
-    RETURN v_Price;
-END BOM_PRICESTD
-]]></body>
-    </function>
-  </database>
--- a/src-db/database/model/functions/BOM_QTY_AVAILABLE.xml	Thu Oct 09 14:13:04 2008 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,40 +0,0 @@
-<?xml version="1.0"?>
-  <database name="FUNCTION BOM_QTY_AVAILABLE">
-    <function name="BOM_QTY_AVAILABLE" type="NUMERIC">
-      <parameter name="p_product_id" type="VARCHAR" mode="in">
-        <default/>
-      </parameter>
-      <parameter name="p_warehouse_id" type="VARCHAR" mode="in">
-        <default/>
-      </parameter>
-      <parameter name="p_locator_id" type="VARCHAR" mode="in">
-        <default><![CDATA[NULL]]></default>
-      </parameter>
-      <body><![CDATA[/*************************************************************************
-* The contents of this file are subject to the Compiere Public
-* License 1.1 ("License"); You may not use this file except in
-* compliance with the License. You may obtain a copy of the License in
-* the legal folder of your Openbravo installation.
-* 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  Compiere  ERP &  Business Solution
-* The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
-* Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke,
-* parts created by ComPiere are Copyright (C) ComPiere, Inc.;
-* All Rights Reserved.
-* Contributor(s): Openbravo SL
-* Contributions are Copyright (C) 2001-2008 Openbravo, S.L.
-*
-* Specifically, this derivative work is based upon the following Compiere
-* file and version.
-*************************************************************************
-* Return quantity available for BOM
-*/
-BEGIN
-  RETURN BOM_Qty_OnHand(p_Product_ID, p_Warehouse_ID, p_Locator_ID) - BOM_Qty_Reserved(p_Product_ID, p_Warehouse_ID, p_Locator_ID) ;
-END BOM_QTY_AVAILABLE
-]]></body>
-    </function>
-  </database>
--- a/src-db/database/model/functions/BOM_QTY_ONHAND.xml	Thu Oct 09 14:13:04 2008 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,150 +0,0 @@
-<?xml version="1.0"?>
-  <database name="FUNCTION BOM_QTY_ONHAND">
-    <function name="BOM_QTY_ONHAND" type="NUMERIC">
-      <parameter name="p_product_id" type="VARCHAR" mode="in">
-        <default/>
-      </parameter>
-      <parameter name="p_warehouse_id" type="VARCHAR" mode="in">
-        <default/>
-      </parameter>
-      <parameter name="p_locator_id" type="VARCHAR" mode="in">
-        <default><![CDATA[NULL]]></default>
-      </parameter>
-      <body><![CDATA[/*************************************************************************
-* The contents of this file are subject to the Compiere Public
-* License 1.1 ("License"); You may not use this file except in
-* compliance with the License. You may obtain a copy of the License in
-* the legal folder of your Openbravo installation.
-* 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  Compiere  ERP &  Business Solution
-* The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
-* Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke,
-* parts created by ComPiere are Copyright (C) ComPiere, Inc.;
-* All Rights Reserved.
-* Contributor(s): Openbravo SL
-* Contributions are Copyright (C) 2001-2008 Openbravo, S.L.
-*
-* Specifically, this derivative work is based upon the following Compiere
-* file and version.
-*************************************************************************
-* Return quantity on hand for BOM
-*/
-    v_myWarehouse_ID VARCHAR2(32);
-    v_Quantity NUMBER:=99999; -- unlimited
-    v_IsBOM CHAR(1) ;
-    v_IsStocked CHAR(1) ;
-    v_ProductType CHAR(1) ;
-    v_ProductQty NUMBER;
-    v_StdPrecision NUMBER;
-    -- Get BOM Product info
-  TYPE RECORD IS REF CURSOR;
-    CUR_BOM RECORD;
-    --
-  BEGIN
-    -- Check Parameters
-    v_myWarehouse_ID:=p_Warehouse_ID;
-    IF(v_myWarehouse_ID IS NULL) THEN
-      IF(p_Locator_ID IS NULL) THEN
-        RETURN 0;
-      ELSE
-        SELECT MAX(M_Warehouse_ID)
-        INTO v_myWarehouse_ID
-        FROM M_LOCATOR
-        WHERE M_Locator_ID=p_Locator_ID;
-      END IF;
-    END IF;
-    IF(v_myWarehouse_ID IS NULL) THEN
-      RETURN 0;
-    END IF;
-    -- DBMS_OUTPUT.PUT_LINE('Warehouse=' || v_myWarehouse_ID);
-    -- Check, if product exists and if it is stocked
-  BEGIN
-    SELECT IsBOM, ProductType, IsStocked
-    INTO v_IsBOM, v_ProductType, v_IsStocked
-    FROM M_PRODUCT
-    WHERE M_Product_ID=p_Product_ID;
-    --
-  EXCEPTION -- not found
-  WHEN OTHERS THEN
-    RETURN 0;
-  END;
-  -- Unimited capacity if no item
-  IF((v_ProductType<>'I' OR(v_IsStocked='N' AND(v_IsBOM='N')))) THEN
-    RETURN v_Quantity;
-    -- Stocked item
-  ELSIF(v_IsStocked='Y') THEN
-    -- Get ProductQty
-    SELECT COALESCE(SUM(QtyOnHand), 0)
-    INTO v_ProductQty
-    FROM M_STORAGE_DETAIL s
-    WHERE M_Product_ID=p_Product_ID  AND EXISTS
-      (SELECT *
-      FROM M_LOCATOR l
-      WHERE s.M_Locator_ID=l.M_Locator_ID  AND l.M_Warehouse_ID=v_myWarehouse_ID
-      )
-      ;
-    --
-    -- DBMS_OUTPUT.PUT_LINE('Qty=' || v_ProductQty);
-    RETURN v_ProductQty;
-  END IF;
-  -- Go though BOM
-  -- DBMS_OUTPUT.PUT_LINE('BOM');
-  FOR CUR_BOM IN
-    (SELECT b.M_ProductBOM_ID, b.BOMQty, p.IsBOM, p.IsStocked, p.ProductType
-    FROM M_PRODUCT_BOM b, M_PRODUCT p
-    WHERE b.M_ProductBOM_ID=p.M_Product_ID  AND b.M_Product_ID=p_Product_ID
-    )
-  LOOP
-    -- Stocked Items "leaf node"
-    IF(CUR_BOM.ProductType='I' AND CUR_BOM.ProductType='I') THEN
-      -- Get v_ProductQty
-      SELECT COALESCE(SUM(QtyOnHand), 0)
-      INTO v_ProductQty
-      FROM M_STORAGE_DETAIL s
-      WHERE M_Product_ID=CUR_BOM.M_ProductBOM_ID  AND EXISTS
-        (SELECT *
-        FROM M_LOCATOR l
-        WHERE s.M_Locator_ID=l.M_Locator_ID  AND l.M_Warehouse_ID=v_myWarehouse_ID
-        )
-        ;
-      -- Get Rounding Precision
-      SELECT COALESCE(MAX(u.StdPrecision), 0)
-      INTO v_StdPrecision
-      FROM C_UOM u, M_PRODUCT p
-      WHERE u.C_UOM_ID=p.C_UOM_ID AND p.M_Product_ID=CUR_BOM.M_ProductBOM_ID;
-      -- How much can we make with this product
-      IF COALESCE(CUR_BOM.BOMQty, 0)=0 THEN
-        v_ProductQty:=0;
-      ELSE
-        v_ProductQty:=ROUND(v_ProductQty/CUR_BOM.BOMQty, v_StdPrecision) ;
-      END IF;
-      -- How much can we make overall
-      IF(v_ProductQty<v_Quantity) THEN
-        v_Quantity:=v_ProductQty;
-      END IF;
-      -- Another BOM
-    ELSIF(CUR_BOM.IsBOM='Y') THEN
-      v_ProductQty:=Bom_Qty_Onhand(CUR_BOM.M_ProductBOM_ID, v_myWarehouse_ID) ;
-      -- How much can we make overall
-      IF(v_ProductQty<v_Quantity) THEN
-        v_Quantity:=v_ProductQty;
-      END IF;
-    END IF;
-  END LOOP; -- BOM
-  IF(v_Quantity>0) THEN
-    -- Get Rounding Precision for Product
-    SELECT COALESCE(MAX(u.StdPrecision), 0)
-    INTO v_StdPrecision
-    FROM C_UOM u, M_PRODUCT p
-    WHERE u.C_UOM_ID=p.C_UOM_ID AND p.M_Product_ID=p_Product_ID;
-    --
-    RETURN ROUND(v_Quantity, v_StdPrecision) ;
-  END IF;
-  RETURN 0;
-END BOM_QTY_ONHAND
-]]></body>
-    </function>
-  </database>
--- a/src-db/database/model/functions/BOM_QTY_ORDERED.xml	Thu Oct 09 14:13:04 2008 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,156 +0,0 @@
-<?xml version="1.0"?>
-  <database name="FUNCTION BOM_QTY_ORDERED">
-    <function name="BOM_QTY_ORDERED" type="NUMERIC">
-      <parameter name="p_product_id" type="VARCHAR" mode="in">
-        <default/>
-      </parameter>
-      <parameter name="p_warehouse_id" type="VARCHAR" mode="in">
-        <default/>
-      </parameter>
-      <parameter name="p_locator_id" type="VARCHAR" mode="in">
-        <default><![CDATA[NULL]]></default>
-      </parameter>
-      <body><![CDATA[/*************************************************************************
-* The contents of this file are subject to the Compiere Public
-* License 1.1 ("License"); You may not use this file except in
-* compliance with the License. You may obtain a copy of the License in
-* the legal folder of your Openbravo installation.
-* 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  Compiere  ERP &  Business Solution
-* The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
-* Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke,
-* parts created by ComPiere are Copyright (C) ComPiere, Inc.;
-* All Rights Reserved.
-* Contributor(s): Openbravo SL
-* Contributions are Copyright (C) 2001-2008 Openbravo, S.L.
-*
-* Specifically, this derivative work is based upon the following Compiere
-* file and version.
-*************************************************************************
-*  Return quantity ordered for BOM
-*/
-  v_Warehouse_ID VARCHAR2(32);
-  v_Quantity NUMBER:= 99999; --  unlimited
-  v_IsBOM        CHAR(1) ;
-  v_IsStocked    CHAR(1) ;
-  v_ProductType  CHAR(1) ;
-  v_ProductQty   NUMBER;
-  v_StdPrecision NUMBER;
-  --  Get BOM Product info
-  TYPE RECORD IS REF CURSOR;
-    CUR_BOM RECORD;
-    --
-  BEGIN
-    --  Check Parameters
-    v_Warehouse_ID := p_Warehouse_ID;
-    IF(v_Warehouse_ID IS NULL) THEN
-      IF(p_Locator_ID IS NULL) THEN
-        RETURN 0;
-      ELSE
-        SELECT MAX(M_Warehouse_ID)
-        INTO v_Warehouse_ID
-        FROM M_Locator
-        WHERE M_Locator_ID = p_Locator_ID;
-      END IF;
-    END IF;
-    IF(v_Warehouse_ID IS NULL) THEN
-      RETURN 0;
-    END IF;
-    --  DBMS_OUTPUT.PUT_LINE('Warehouse=' || v_Warehouse_ID);
-    --  Check, if product exists and if it is stocked
-  BEGIN
-    SELECT IsBOM,
-      ProductType,
-      IsStocked
-    INTO v_IsBOM,
-      v_ProductType,
-      v_IsStocked
-    FROM M_Product
-    WHERE M_Product_ID = p_Product_ID;
-    --
-  EXCEPTION --  not found
-  WHEN OTHERS THEN
-    RETURN 0;
-  END;
-  --  No reservation for non-stocked
-  IF((v_ProductType <> 'I' OR(v_IsStocked = 'N' AND v_IsBOM = 'N'))) THEN
-    RETURN 0;
-    --  Stocked item
-  ELSIF(v_IsStocked = 'Y') THEN
-    --  Get ProductQty
-    SELECT COALESCE(SUM(QtyOrdered), 0)
-    INTO v_ProductQty
-    FROM M_Storage_Pending s
-    WHERE M_Product_ID = p_Product_ID
-      AND M_Warehouse_ID = v_Warehouse_ID;
-    --
-    RETURN v_ProductQty;
-  END IF;
-  --  Go though BOM
-  --  DBMS_OUTPUT.PUT_LINE('BOM');
-  FOR CUR_BOM IN
-    (SELECT b.M_ProductBOM_ID,
-      b.BOMQty,
-      p.IsBOM,
-      p.IsStocked,
-      p.ProductType
-    FROM M_Product_BOM b,
-      M_Product p
-    WHERE b.M_ProductBOM_ID = p.M_Product_ID
-      AND b.M_Product_ID = p_Product_ID
-    )
-  LOOP
-    --  Stocked Items "leaf node"
-    IF(CUR_BOM.ProductType = 'I' AND CUR_BOM.IsStocked = 'Y') THEN
-      --  Get ProductQty
-      SELECT COALESCE(SUM(QtyOrdered), 0)
-      INTO v_ProductQty
-      FROM M_Storage_Pending s
-      WHERE M_Product_ID = CUR_BOM.M_ProductBOM_ID
-        AND M_Warehouse_ID = v_Warehouse_ID;
-      --  Get Rounding Precision
-      SELECT COALESCE(MAX(u.StdPrecision), 0)
-      INTO v_StdPrecision
-      FROM C_UOM u,
-        M_Product p
-      WHERE u.C_UOM_ID = p.C_UOM_ID
-        AND p.M_Product_ID = CUR_BOM.M_ProductBOM_ID;
-      --  How much can we make with this product
-      v_ProductQty := ROUND(v_ProductQty/CUR_BOM.BOMQty, v_StdPrecision) ;
-      --  How much can we make overall
-      IF(v_ProductQty < v_Quantity) THEN
-        v_Quantity := v_ProductQty;
-      END IF;
-      --  Another BOM
-    ELSIF(CUR_BOM.IsBOM = 'Y') THEN
-      v_ProductQty := BOM_Qty_Reserved(CUR_BOM.M_ProductBOM_ID, v_Warehouse_ID) ;
-      --  How much can we make overall
-      IF(v_ProductQty < v_Quantity) THEN
-        v_Quantity := v_ProductQty;
-      END IF;
-    END IF;
-  END LOOP; --  BOM
-  --  Unlimited (e.g. only services)
-  IF(v_Quantity = 99999) THEN
-    RETURN 0;
-  END IF;
-  IF(v_Quantity > 0) THEN
-    --  Get Rounding Precision for Product
-    SELECT COALESCE(MAX(u.StdPrecision), 0)
-    INTO v_StdPrecision
-    FROM C_UOM u,
-      M_Product p
-    WHERE u.C_UOM_ID = p.C_UOM_ID
-      AND p.M_Product_ID = p_Product_ID;
-    --
-    RETURN ROUND(v_Quantity, v_StdPrecision) ;
-  END IF;
-  --
-  RETURN 0;
-END BOM_QTY_ORDERED
-]]></body>
-    </function>
-  </database>
--- a/src-db/database/model/functions/BOM_QTY_RESERVED.xml	Thu Oct 09 14:13:04 2008 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,155 +0,0 @@
-<?xml version="1.0"?>
-  <database name="FUNCTION BOM_QTY_RESERVED">
-    <function name="BOM_QTY_RESERVED" type="NUMERIC">
-      <parameter name="p_product_id" type="VARCHAR" mode="in">
-        <default/>
-      </parameter>
-      <parameter name="p_warehouse_id" type="VARCHAR" mode="in">
-        <default/>
-      </parameter>
-      <parameter name="p_locator_id" type="VARCHAR" mode="in">
-        <default><![CDATA[NULL]]></default>
-      </parameter>
-      <body><![CDATA[/*************************************************************************
-* The contents of this file are subject to the Compiere Public
-* License 1.1 ("License"); You may not use this file except in
-* compliance with the License. You may obtain a copy of the License in
-* the legal folder of your Openbravo installation.
-* 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  Compiere  ERP &  Business Solution
-* The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
-* Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke,
-* parts created by ComPiere are Copyright (C) ComPiere, Inc.;
-* All Rights Reserved.
-* Contributor(s): Openbravo SL
-* Contributions are Copyright (C) 2001-2008 Openbravo, S.L.
-*
-* Specifically, this derivative work is based upon the following Compiere
-* file and version.
-*************************************************************************
-* Return quantity reserved for BOM
-*/
-  v_Warehouse_ID VARCHAR2(32);
-  v_Quantity NUMBER:=99999; -- unlimited
-  v_IsBOM        CHAR(1) ;
-  v_IsStocked    CHAR(1) ;
-  v_ProductType  CHAR(1) ;
-  v_ProductQty   NUMBER;
-  v_StdPrecision NUMBER;
-  -- Get BOM Product info
-  TYPE RECORD IS REF CURSOR;
-    CUR_BOM RECORD;
-    --
-  BEGIN
-    -- Check Parameters
-    v_Warehouse_ID:=p_Warehouse_ID;
-    IF(v_Warehouse_ID IS NULL) THEN
-      IF(p_Locator_ID IS NULL) THEN
-        RETURN 0;
-      ELSE
-        SELECT MAX(M_Warehouse_ID)
-        INTO v_Warehouse_ID
-        FROM M_Locator
-        WHERE M_Locator_ID=p_Locator_ID;
-      END IF;
-    END IF;
-    IF(v_Warehouse_ID IS NULL) THEN
-      RETURN 0;
-    END IF;
-    -- DBMS_OUTPUT.PUT_LINE('Warehouse=' || v_Warehouse_ID);
-    -- Check, if product exists and if it is stocked
-  BEGIN
-    SELECT IsBOM,
-      ProductType,
-      IsStocked
-    INTO v_IsBOM,
-      v_ProductType,
-      v_IsStocked
-    FROM M_Product
-    WHERE M_Product_ID=p_Product_ID;
-    --
-  EXCEPTION -- not found
-  WHEN OTHERS THEN
-    RETURN 0;
-  END;
-  -- No reservation for non-stocked
-  IF((v_ProductType <> 'I' OR(v_IsStocked='N' AND v_IsBOM='N'))) THEN
-    RETURN 0;
-    -- Stocked item
-  ELSIF(v_IsStocked='Y') THEN
-    -- Get ProductQty
-    SELECT COALESCE(SUM(QtyReserved), 0)
-    INTO v_ProductQty
-    FROM M_Storage_Pending s
-    WHERE M_Product_ID=p_Product_ID
-      AND M_Warehouse_ID=v_Warehouse_ID;
-    --
-    RETURN v_ProductQty;
-  END IF;
-  -- Go though BOM
-  -- DBMS_OUTPUT.PUT_LINE('BOM');
-  FOR CUR_BOM IN
-    (SELECT b.M_ProductBOM_ID,
-      b.BOMQty,
-      p.IsBOM,
-      p.IsStocked,
-      p.ProductType
-    FROM M_Product_BOM b,
-      M_Product p
-    WHERE b.M_ProductBOM_ID=p.M_Product_ID
-      AND b.M_Product_ID=p_Product_ID
-    )
-  LOOP
-    -- Stocked Items "leaf node"
-    IF(CUR_BOM.ProductType='I' AND CUR_BOM.IsStocked='Y') THEN
-      -- Get ProductQty
-      SELECT COALESCE(SUM(QtyReserved), 0)
-      INTO v_ProductQty
-      FROM M_Storage_Pending s
-      WHERE M_Product_ID=CUR_BOM.M_ProductBOM_ID
-        AND M_Warehouse_ID=v_Warehouse_ID;
-      -- Get Rounding Precision
-      SELECT COALESCE(MAX(u.StdPrecision), 0)
-      INTO v_StdPrecision
-      FROM C_UOM u,
-        M_Product p
-      WHERE u.C_UOM_ID=p.C_UOM_ID
-        AND p.M_Product_ID=CUR_BOM.M_ProductBOM_ID;
-      -- How much can we make with this product
-      v_ProductQty:=ROUND(v_ProductQty/CUR_BOM.BOMQty, v_StdPrecision) ;
-      -- How much can we make overall
-      IF(v_ProductQty < v_Quantity) THEN
-        v_Quantity:=v_ProductQty;
-      END IF;
-      -- Another BOM
-    ELSIF(CUR_BOM.IsBOM='Y') THEN
-      v_ProductQty:=BOM_Qty_Reserved(CUR_BOM.M_ProductBOM_ID, v_Warehouse_ID) ;
-      -- How much can we make overall
-      IF(v_ProductQty < v_Quantity) THEN
-        v_Quantity:=v_ProductQty;
-      END IF;
-    END IF;
-  END LOOP; -- BOM
-  -- Unlimited (e.g. only services)
-  IF(v_Quantity=99999) THEN
-    RETURN 0;
-  END IF;
-  IF(v_Quantity > 0) THEN
-    -- Get Rounding Precision for Product
-    SELECT COALESCE(MAX(u.StdPrecision), 0)
-    INTO v_StdPrecision
-    FROM C_UOM u,
-      M_Product p
-    WHERE u.C_UOM_ID=p.C_UOM_ID
-      AND p.M_Product_ID=p_Product_ID;
-    --
-    RETURN ROUND(v_Quantity, v_StdPrecision) ;
-  END IF;
-  RETURN 0;
-END BOM_QTY_RESERVED
-]]></body>
-    </function>
-  </database>
--- a/src-db/database/model/functions/CHANGEPROJECTSTATUS.xml	Thu Oct 09 14:13:04 2008 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,134 +0,0 @@
-<?xml version="1.0"?>
-  <database name="FUNCTION CHANGEPROJECTSTATUS">
-    <function name="CHANGEPROJECTSTATUS" type="NULL">
-      <parameter name="p_pinstance_id" type="VARCHAR" 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):  ______________________________________.
-************************************************************************/
-  v_resultstr         VARCHAR2 (2000) := '';
-  v_message           VARCHAR2 (2000) := '';
-  v_result NUMBER:= 1;                      -- 0=failure
-  v_record_id         varchar2(32);
-  v_ad_user_id        varchar2(32);
-
-  TYPE RECORD IS REF CURSOR;
-
-  cur_parameter       RECORD;
-  v_p_projectstatus   CHAR (2);
-  v_projectstatus     CHAR (2);
-  v_created NUMBER:= 0;
-  finish_process      BOOLEAN         := FALSE;
-BEGIN
-  -- Get Parameters
-  v_resultstr := 'ReadingParameters';
-
-  FOR cur_parameter IN (SELECT   i.record_id, i.ad_user_id, p.parametername,
-                                 p.p_string, p.p_number, p.p_date,
-                                 p.p_date_to
-                            FROM ad_pinstance i LEFT JOIN ad_pinstance_para p
-                                 ON i.ad_pinstance_id = p.ad_pinstance_id
-                           WHERE i.ad_pinstance_id = p_pinstance_id
-                        ORDER BY p.seqno)
-  LOOP
-    v_record_id := cur_parameter.record_id;
-    v_ad_user_id := cur_parameter.ad_user_id;
-
-    IF (cur_parameter.parametername = 'ChangeProjectStatus')
-    THEN
-      v_p_projectstatus := cur_parameter.p_string;
-      DBMS_OUTPUT.PUT_LINE('  ChangeProjectStatus=' || v_p_projectstatus);
-    ELSE
-      DBMS_OUTPUT.PUT_LINE('*** Unknown Parameter='|| cur_parameter.parametername);
-    END IF;
-  END LOOP;                                                   -- Get Parameter
-
-  DBMS_OUTPUT.PUT_LINE('  Record_ID=' || v_record_id);
-  -- Check that we have some restrictions
-  v_resultstr := 'CheckRestriction';
-
-  IF (v_p_projectstatus IS NULL)
-  THEN
-    RAISE_APPLICATION_ERROR(-20000, '@Notprojectstatus@');
-  END IF;
-
-  IF (NOT finish_process)
-  THEN
-    v_resultstr := 'Verify status';
-
-    SELECT projectstatus
-      INTO v_projectstatus
-      FROM c_project
-     WHERE c_project_id = v_record_id;
-
-    IF v_projectstatus IS NULL
-    THEN
-      RAISE_APPLICATION_ERROR(-20000, '@Notstatusdefined@');
-    ELSIF (v_projectstatus = 'NF' OR v_projectstatus = 'OP')
-    THEN
-      IF (v_p_projectstatus <> 'OR' AND v_p_projectstatus <> 'OU')
-      THEN
-        RAISE_APPLICATION_ERROR(-20000, '@Invalidprojectstatus@');
-      END IF;
-    ELSIF (v_projectstatus = 'OR')
-    THEN
-      IF (v_p_projectstatus <> 'OC')
-      THEN
-        RAISE_APPLICATION_ERROR(-20000, '@Invalidprojectstatus@');
-      END IF;
-    ELSIF (v_projectstatus = 'OC')
-    THEN
-      RAISE_APPLICATION_ERROR(-20000, '@Projectclose@');
-    END IF;
-  END IF;                                                     --FINISH_PROCESS
-
-  IF (NOT finish_process)
-  THEN
-    IF v_p_projectstatus = 'OR'
-    THEN
-      CHANGEPROJECTSTATUSTOORDER(p_pinstance_id);
-    ELSIF v_p_projectstatus = 'OC'
-    THEN
-      CLOSEPROJECT(p_pinstance_id);
-    ELSE
-      UPDATE c_project
-         SET projectstatus = v_p_projectstatus
-       WHERE c_project_id = v_record_id;
-
-      COMMIT;
-    END IF;
-
-    RETURN;
-  END IF;--FINISH_PROCESS
-
-  --<<FINISH_PROCESS>>
-  --  Update AD_PInstance
-  DBMS_OUTPUT.PUT_LINE('Updating PInstance - Finished ' || v_message);
-  AD_UPDATE_PINSTANCE(p_pinstance_id, NULL, 'N', v_result, v_message);
-  RETURN;
-EXCEPTION
-  WHEN OTHERS
-  THEN
-    v_resultstr:= '@ERROR=' || SQLERRM;
-    DBMS_OUTPUT.PUT_LINE(v_resultstr);
-    ROLLBACK;
-    AD_UPDATE_PINSTANCE(p_pinstance_id, NULL, 'N', 0, v_resultstr);
--- Commented by cromero 19102006 RETURN;
-END CHANGEPROJECTSTATUS
-]]></body>
-    </function>
-  </database>
--- a/src-db/database/model/functions/CHANGEPROJECTSTATUSTOORDER.xml	Thu Oct 09 14:13:04 2008 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,338 +0,0 @@
-<?xml version="1.0"?>
-  <database name="FUNCTION CHANGEPROJECTSTATUSTOORDER">
-    <function name="CHANGEPROJECTSTATUSTOORDER" type="NULL">
-      <parameter name="p_pinstance_id" type="VARCHAR" 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-2008 Openbravo SL
-* All Rights Reserved.
-* Contributor(s):  ______________________________________.
-************************************************************************/
-  v_ResultStr VARCHAR2(2000):='';
-  v_Message VARCHAR2(2000):='';
-  v_Result NUMBER:=1; -- 0=failure
-  v_Record_ID VARCHAR2(32);
-  v_AD_User_ID VARCHAR2(32);
-  v_AD_Client_ID VARCHAR2(32);
-  v_AD_Org_ID VARCHAR2(32);
-  TYPE RECORD IS REF CURSOR;
-    Cur_Parameter RECORD;
-    v_ProjectStatus CHAR(2) ;
-    v_projectcategory   CHAR (2);
-    v_temppricelist     CHAR (1);
-    v_Created NUMBER:=0;
-    v_PriceListVersion NUMBER;
-    v_PriceList NUMBER;
-    v_PriceListVersionPO NUMBER;
-    v_PriceListPO NUMBER;
-    v_C_Currency_ID VARCHAR2(32);
-    v_TotalPhases NUMBER;
-    v_TotalLines NUMBER;
-    v_BPName VARCHAR2(2000):='';
-    v_SupplierName VARCHAR2(2000):='';
-    FINISH_PROCESS BOOLEAN:=false;
-  BEGIN
-    --  Update AD_PInstance
-    DBMS_OUTPUT.PUT_LINE('Updating PInstance - Processing ' || p_PInstance_ID) ;
-    v_ResultStr:='PInstanceNotFound';
-    AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'Y', NULL, NULL) ;
-  BEGIN --BODY
-    -- Get PARAMETERS
-    v_ResultStr:='ReadingParameters';
-    FOR Cur_Parameter IN
-      (SELECT i.Record_ID,
-        i.AD_User_ID,
-        i.AD_Client_ID,
-        i.AD_Org_ID,
-        p.ParameterName,
-        p.P_String,
-        p.P_Number,
-        p.P_Date,
-        p.P_Date_To
-      FROM AD_PInstance i
-      LEFT JOIN AD_PInstance_Para p
-        ON i.AD_PInstance_ID=p.AD_PInstance_ID
-      WHERE i.AD_PInstance_ID=p_PInstance_ID
-      ORDER BY p.SeqNo
-      )
-    LOOP
-      v_Record_ID:=Cur_Parameter.Record_ID;
-      v_AD_User_ID:=Cur_Parameter.AD_User_ID;
-      IF(Cur_Parameter.ParameterName='ChangeProjectStatus') THEN
-        v_ProjectStatus:=Cur_Parameter.P_String;
-        DBMS_OUTPUT.PUT_LINE('  ChangeProjectStatus=' || v_ProjectStatus) ;
-      ELSE
-        DBMS_OUTPUT.PUT_LINE('*** Unknown Parameter=' || Cur_Parameter.ParameterName) ;
-      END IF;
-    END LOOP; -- Get Parameter
-    DBMS_OUTPUT.PUT_LINE('  Record_ID=' || v_Record_ID) ;
-    -- Check that we have some restrictions
-    v_ResultStr:='CheckRestriction';
-    IF(v_ProjectStatus IS NULL) THEN
-      RAISE_APPLICATION_ERROR(-20000, '@Notprojectstatus@');
-    ELSIF v_ProjectStatus<>'OR' THEN
-      RAISE_APPLICATION_ERROR(-20000, '@Invalidprojectstatus@');
-    END IF;
-    IF(NOT FINISH_PROCESS) THEN
-      DECLARE
-        Cur_SO RECORD;
-        Cur_Lines RECORD;
-        Cur_LinesPO RECORD;
-      BEGIN
-        v_ResultStr:='StartLoop';
-        FOR Cur_SO IN
-          (SELECT * FROM C_PROJECT WHERE C_PROJECT_ID=v_Record_ID)
-        LOOP
-          v_AD_Client_ID:=Cur_SO.AD_Client_ID;
-          v_AD_Org_ID:=Cur_SO.AD_Org_ID;
-          IF Cur_SO.C_BPARTNER_ID IS NULL THEN
-            RAISE_APPLICATION_ERROR(-20000, '@NoprojectBusinesspartner@');
-            /* Eliminado a peticion de Huesker
-            ELSIF Cur_SO.AD_USER_ID IS NULL THEN
-            v_Message := 'ContactNotFound';
-            v_Result := 0;
-            FINISH_PROCESS := true;
-            EXIT; */
-          ELSIF Cur_SO.C_BPARTNER_LOCATION_ID IS NULL THEN
-            RAISE_APPLICATION_ERROR(-20000, '@ThebusinessPartner@'||' '||' @ShiptoNotdefined@');
-          ELSIF Cur_SO.BILLTO_ID IS NULL THEN
-            RAISE_APPLICATION_ERROR(-20000, '@ThebusinessPartner@'||' '||' @BillToNotdefined@');
-          ELSIF Cur_SO.C_PAYMENTTERM_ID IS NULL THEN
-            RAISE_APPLICATION_ERROR(-20000, '@ThebusinessPartner@'||' '||' @PaymenttermNotdefined@');
-          ELSIF Cur_SO.C_CURRENCY_ID IS NULL THEN
-            RAISE_APPLICATION_ERROR(-20000, '@ProjectCurrencyNotFound@');
-          ELSIF Cur_SO.M_WAREHOUSE_ID IS NULL THEN
-            RAISE_APPLICATION_ERROR(-20000, '@ProjectWarehouseNotFound@');
-          ELSIF Cur_SO.SALESREP_ID IS NULL THEN
-            RAISE_APPLICATION_ERROR(-20000, '@ProjectSalesRepNotFound@');
-          END IF;
-          /*Added in order to work with Multiphase Project*/
-          IF Cur_SO.PROJECTCATEGORY<>'S' THEN
-            SELECT COUNT(*)
-            INTO v_TotalLines
-            FROM C_PROJECTLINE
-            WHERE C_PROJECT_ID=v_Record_ID;
-            IF v_TotalLines=0 THEN
-              RAISE_APPLICATION_ERROR(-20000, '@NoProjectLines@');
-            END IF;
-          ELSE
-            SELECT COUNT(*)
-            INTO v_TotalPhases
-            FROM C_PROJECTPHASE
-            WHERE C_PROJECT_ID=v_Record_ID;
-            IF v_TotalPhases=0 THEN
-              RAISE_APPLICATION_ERROR(-20000, '@NoProjectPhases@');
-            END IF;
-          END IF;
-          /*In order to create a particular price list for the current project,
-            project must be a service project and Create Temporary Price List option must be checked*/
-          IF (Cur_SO.PROJECTCATEGORY<>'S' AND Cur_SO.CREATETEMPPRICELIST='Y') THEN
-
-            Ad_Sequence_Next('M_PriceList', v_AD_Client_ID, v_PriceList) ;
-            
-            SELECT NAME 
-            INTO v_BPName
-            FROM C_BPARTNER 
-            WHERE C_BPARTNER_ID = Cur_SO.C_BPARTNER_ID; 
-                       
-            INSERT
-            INTO m_pricelist
-              (
-                M_PRICELIST_ID, AD_CLIENT_ID, AD_ORG_ID, ISACTIVE,
-                CREATED, CREATEDBY, UPDATED, UPDATEDBY,
-                NAME, 
-				DESCRIPTION, 
-	        	ISSOPRICELIST, C_Currency_ID
-              )
-              VALUES
-              (
-                v_PriceList, v_AD_Client_ID, v_AD_Org_ID, 'Y',
-                now(), v_AD_User_ID, now(), v_AD_User_ID,
-                (Cur_SO.value || ' - ' || v_BPName), 
-				(Cur_SO.value || ' - ' || v_BPName), 
-				'Y', Cur_SO.C_Currency_ID
-              )
-              ;
-            Ad_Sequence_Next('M_PriceList_Version', v_AD_Client_ID, v_PriceListVersion) ;
-            INSERT
-            INTO M_PRICELIST_VERSION
-              (
-                M_PRICELIST_VERSION_ID, AD_CLIENT_ID, AD_ORG_ID, ISACTIVE,
-                CREATED, CREATEDBY, UPDATED, UPDATEDBY,
-                NAME, 
-				DESCRIPTION, 
-				M_PRICELIST_ID, M_DISCOUNTSCHEMA_ID,
-                VALIDFROM
-              )
-              VALUES
-              (
-                v_PriceListVersion, v_AD_Client_ID, v_AD_Org_ID, 'Y',
-                now(), v_AD_User_ID, now(), v_AD_User_ID,
-                (Cur_SO.value || ' - ' ||v_BPName|| ' - ' ||TRUNC(now(), 'DD')), 
-				(Cur_SO.value || ' - ' ||v_BPName|| ' - ' ||TRUNC(now(), 'DD')), 
-				v_PriceList, (SELECT MIN(M_DISCOUNTSCHEMA_ID) AS ID FROM M_DISCOUNTSCHEMA WHERE isactive='Y'),
-                TRUNC(now(), 'DD')
-              )
-              ;
-            FOR Cur_Lines IN
-              (SELECT * FROM C_PROJECTLINE WHERE C_PROJECT_ID=v_Record_ID)
-            LOOP
-              INSERT
-              INTO M_PRODUCTPRICE
-                (
-                  M_PRICELIST_VERSION_ID, M_PRODUCT_ID, AD_CLIENT_ID, AD_ORG_ID,
-                  ISACTIVE, CREATED, CREATEDBY, UPDATED,
-                  UPDATEDBY, PRICELIST, PRICESTD, PRICELIMIT
-                )
-                VALUES
-                (
-                  v_PriceListVersion, Cur_Lines.M_Product_ID, v_AD_Client_ID, v_AD_Org_ID,
-                  'Y', now(), v_AD_User_ID, now(), v_AD_User_ID,
-                  Cur_Lines.PLANNEDPRICE, Cur_Lines.PLANNEDPRICE, Cur_Lines.PLANNEDPRICE
-                )
-                ;
-            END LOOP;
-            SELECT COUNT(*)
-            INTO v_TotalLines
-            FROM C_PROJECTLINE
-            WHERE C_PROJECT_ID=v_Record_ID
-              AND PLANNEDPOPRICE IS NOT NULL;
-            IF v_TotalLines>0 THEN
-              Ad_Sequence_Next('M_PriceList', v_AD_Client_ID, v_PriceListPO) ;
-              
-              SELECT max(bp.name) 
-			  INTO v_SupplierName
-			  FROM c_project p, c_project_vendor pv, c_bpartner bp 
-			  WHERE p.c_project_id = v_Record_ID 
-			  AND p.c_project_id = pv.c_project_id 
-			  AND pv.c_bpartner_id = bp.c_bpartner_id 
-			  AND pv.isactive = 'Y';
-	      	  IF (v_SupplierName IS NULL) THEN
-			  v_SupplierName := '';
-	     	  END IF;
-              
-	      INSERT
-              INTO m_pricelist
-                (
-                  M_PRICELIST_ID, AD_CLIENT_ID, AD_ORG_ID, ISACTIVE,
-                  CREATED, CREATEDBY, UPDATED, UPDATEDBY,
-                  NAME, 
-	              DESCRIPTION, 
-		          ISSOPRICELIST, C_Currency_ID
-                )
-                VALUES
-                (
-                  v_PriceListPO, v_AD_Client_ID, v_AD_Org_ID, 'Y',
-                  now(), v_AD_User_ID, now(), v_AD_User_ID,
-                  (Cur_SO.value || ' - ' ||v_SupplierName), 
-	              (Cur_SO.value || ' - ' ||v_SupplierName),
-		          'N', Cur_SO.C_Currency_ID
-                )
-                ;
-              Ad_Sequence_Next('M_PriceList_Version', v_AD_Client_ID, v_PriceListVersionPO) ;
-              INSERT
-              INTO M_PRICELIST_VERSION
-                (
-                  M_PRICELIST_VERSION_ID, AD_CLIENT_ID, AD_ORG_ID, ISACTIVE,
-                  CREATED, CREATEDBY, UPDATED, UPDATEDBY, 
-		  		  NAME,
-                  DESCRIPTION, 
-		  		  M_PRICELIST_ID, M_DISCOUNTSCHEMA_ID, 
-		  		  VALIDFROM
-                )
-                VALUES
-                (
-                  v_PriceListVersionPO, v_AD_Client_ID, v_AD_Org_ID, 'Y',
-                  now(), v_AD_User_ID, now(), v_AD_User_ID,
-                  (Cur_SO.value || ' - ' ||v_SupplierName|| ' - ' ||TRUNC(now(), 'DD')), 
-		          (Cur_SO.value || ' - ' ||v_SupplierName||' - ' ||TRUNC(now(), 'DD')), 
-		          v_PriceListPO, (SELECT MIN(M_DISCOUNTSCHEMA_ID) AS ID FROM M_DISCOUNTSCHEMA WHERE isactive='Y'),
-                  TRUNC(now(), 'DD')
-                )
-                ;
-              FOR Cur_LinesPO IN
-                (SELECT *
-                FROM C_PROJECTLINE
-                WHERE C_PROJECT_ID=v_Record_ID
-                  AND PLANNEDPOPRICE IS NOT NULL
-                )
-              LOOP
-                INSERT
-                INTO M_PRODUCTPRICE
-                  (
-                    M_PRICELIST_VERSION_ID, M_PRODUCT_ID, AD_CLIENT_ID, AD_ORG_ID, ISACTIVE,
-                    CREATED, CREATEDBY, UPDATED, UPDATEDBY,
-                    PRICELIST, PRICESTD, PRICELIMIT
-                  )
-                  VALUES
-                  (
-                    v_PriceListVersionPO, Cur_LinesPO.M_Product_ID, v_AD_Client_ID, v_AD_Org_ID,
-                     'Y', now(), v_AD_User_ID, now(),
-                    v_AD_User_ID, Cur_LinesPO.PLANNEDPOPRICE, Cur_LinesPO.PLANNEDPOPRICE, Cur_LinesPO.PLANNEDPOPRICE
-                  )
-                  ;
-              END LOOP;
-            ELSE
-              SELECT MIN(M_PRICELIST_ID)
-              INTO v_PriceListPO
-              FROM M_PRICELIST
-              WHERE ISSOPRICELIST='N'
-                AND ISACTIVE='Y'
-                AND AD_CLIENT_ID=v_AD_Client_ID;
-            END IF;
-          END IF;
-        END LOOP;
-        IF v_PriceListPO IS NOT NULL THEN
-          UPDATE C_PROJECT_VENDOR
-            SET m_pricelist_id=v_PriceListPO
-          WHERE c_project_id=v_Record_ID;
-        END IF;
-
-        /*In order to update the price list of the current project,
-          project must be a service project and Create Temporary Price List option must be checked*/
-        SELECT projectcategory, createtemppricelist
-        INTO v_projectcategory, v_temppricelist
-        FROM c_project
-        WHERE c_project_id = v_Record_ID;
-
-        IF (v_projectcategory<>'S' AND v_temppricelist='Y') THEN
-          UPDATE C_PROJECT
-            SET projectstatus=v_ProjectStatus,
-            m_pricelist_id=v_PriceList
-          WHERE c_project_id=v_Record_ID;
-        ELSE
-          UPDATE C_PROJECT
-            SET projectstatus=v_ProjectStatus
-          WHERE c_project_id=v_Record_ID;
-        END IF;
-      END;
-    END IF; --FINISH_PROCESS
-    --<<FINISH_PROCESS>>
-    --  Update AD_PInstance
-    DBMS_OUTPUT.PUT_LINE('Updating PInstance - Finished ' || v_Message) ;
-    AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', v_Result, v_Message) ;
-    RETURN;
-  END; --BODY
-EXCEPTION
-WHEN OTHERS THEN
-  v_ResultStr:= '@ERROR=' || SQLERRM;
-  DBMS_OUTPUT.PUT_LINE(v_ResultStr) ;
-  ROLLBACK;
-  AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', 0, v_ResultStr) ;
-  RETURN;
-END CHANGEPROJECTSTATUSTOORDER
-]]></body>
-    </function>
-  </database>
--- a/src-db/database/model/functions/CLOSEPROJECT.xml	Thu Oct 09 14:13:04 2008 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,138 +0,0 @@
-<?xml version="1.0"?>
-  <database name="FUNCTION CLOSEPROJECT">
-    <function name="CLOSEPROJECT" type="NULL">
-      <parameter name="p_pinstance_id" type="VARCHAR" 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-2008 Openbravo SL
-* All Rights Reserved.
-* Contributor(s):  ______________________________________.
-************************************************************************/
-  v_ResultStr VARCHAR2(2000):='';
-  v_Message VARCHAR2(2000):='';
-  v_Result NUMBER:=1; -- 0=failure
-  v_p_Record_ID VARCHAR2(32);
-  v_AD_User_ID VARCHAR2(32);
-  TYPE RECORD IS REF CURSOR;
-    Cur_Parameter RECORD;
-    v_ProjectStatus CHAR(2) ;
-    v_projectcategory   CHAR (2);
-    v_temppricelist     CHAR (1);
-    v_Created NUMBER:=0;
-    FINISH_PROCESS BOOLEAN:=false;
-  BEGIN
-    --  Update AD_PInstance
-    DBMS_OUTPUT.PUT_LINE('Updating PInstance - Processing ' || p_PInstance_ID) ;
-    v_ResultStr:='PInstanceNotFound';
-    AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'Y', NULL, NULL) ;
-  BEGIN --BODY
-    -- Get PARAMETERS
-    v_ResultStr:='ReadingParameters';
-    FOR Cur_Parameter IN
-      (SELECT i.Record_ID,
-        i.AD_User_ID,
-        i.AD_Client_ID,
-        i.AD_Org_ID,
-        p.ParameterName,
-        p.P_String,
-        p.P_Number,
-        p.P_Date,
-        p.P_Date_To
-      FROM AD_PInstance i
-      LEFT JOIN AD_PInstance_Para p
-        ON i.AD_PInstance_ID=p.AD_PInstance_ID
-      WHERE i.AD_PInstance_ID=p_PInstance_ID
-      ORDER BY p.SeqNo
-      )
-    LOOP
-      v_p_Record_ID:=Cur_Parameter.Record_ID;
-      v_AD_User_ID:=Cur_Parameter.AD_User_ID;
-      IF(Cur_Parameter.ParameterName='ChangeProjectStatus') THEN
-        v_ProjectStatus:=Cur_Parameter.P_String;
-        DBMS_OUTPUT.PUT_LINE('  ChangeProjectStatus=' || v_ProjectStatus) ;
-      ELSE
-        DBMS_OUTPUT.PUT_LINE('*** Unknown Parameter=' || Cur_Parameter.ParameterName) ;
-      END IF;
-    END LOOP; -- Get Parameter
-    DBMS_OUTPUT.PUT_LINE('  Record_ID=' || v_p_Record_ID) ;
-    -- Check that we have some restrictions
-
-    SELECT projectcategory, createtemppricelist
-    INTO v_projectcategory, v_temppricelist
-    FROM c_project
-    WHERE c_project_id = v_p_Record_ID;
-
-    v_ResultStr:='CheckRestriction';
-    IF(v_ProjectStatus IS NULL) THEN
-      RAISE_APPLICATION_ERROR(-20000, '@Notprojectstatus@');
-    ELSIF v_ProjectStatus<>'OC' THEN
-      RAISE_APPLICATION_ERROR(-20000, '@Invalidprojectstatus@');
-    END IF;
-    IF(NOT FINISH_PROCESS) THEN
-      v_ResultStr:='StartLoop';
-      IF (v_projectcategory<>'S' AND v_temppricelist='Y') THEN
-        UPDATE M_PRICELIST_VERSION
-          SET ISACTIVE='N',
-          updated=now(),
-          updatedby=v_AD_User_ID
-        WHERE M_PRICELIST_ID IN
-          (  SELECT M_PRICELIST_ID FROM C_PROJECT WHERE C_PROJECT_ID=v_p_Record_ID)
-          ;
-        UPDATE M_PRICELIST
-          SET ISACTIVE='N',
-          updated=now(),
-          updatedby=v_AD_User_ID
-        WHERE M_PRICELIST_ID IN
-          (  SELECT M_PRICELIST_ID FROM C_PROJECT WHERE C_PROJECT_ID=v_p_Record_ID)
-          ;
-        UPDATE M_PRICELIST_VERSION
-          SET ISACTIVE='N',
-          updated=now(),
-          updatedby=v_AD_User_ID
-        WHERE M_PRICELIST_ID IN
-          ( SELECT M_PRICELIST_ID FROM C_PROJECT_VENDOR WHERE C_PROJECT_ID=v_p_Record_ID)
-          ;
-        UPDATE M_PRICELIST
-          SET ISACTIVE='N',
-          updated=now(),
-          updatedby=v_AD_User_ID
-        WHERE M_PRICELIST_ID IN
-          (  SELECT M_PRICELIST_ID FROM C_PROJECT_VENDOR WHERE C_PROJECT_ID=v_p_Record_ID)
-          ;
-      END IF;
-      UPDATE C_PROJECT
-        SET projectstatus=v_ProjectStatus,
-        DATEFINISH=now(),
-        updated=now(),
-        updatedby=v_AD_User_ID
-      WHERE c_project_id=v_p_Record_ID;
-    END IF; --FINISH_PROCESS
-    --<<FINISH_PROCESS>>
-    --  Update AD_PInstance
-    DBMS_OUTPUT.PUT_LINE('Updating PInstance - Finished ' || v_Message) ;
-    AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', v_Result, v_Message) ;
-    RETURN;
-  END; --BODY
-EXCEPTION
-WHEN OTHERS THEN
-  v_ResultStr:= '@ERROR=' || SQLERRM;
-  DBMS_OUTPUT.PUT_LINE(v_ResultStr) ;
-  ROLLBACK;
-  AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', 0, v_ResultStr) ;
-  RETURN;
-END CLOSEPROJECT
-]]></body>
-    </function>
-  </database>
--- a/src-db/database/model/functions/COPY_PRODUCT_TEMPLATE.xml	Thu Oct 09 14:13:04 2008 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,185 +0,0 @@
-<?xml version="1.0"?>
-  <database name="FUNCTION COPY_PRODUCT_TEMPLATE">
-    <function name="COPY_PRODUCT_TEMPLATE" type="NULL">
-      <parameter name="pinstance_id" type="VARCHAR" 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 VARCHAR2(32);
-  -- Parameter
-  TYPE RECORD IS REF CURSOR;
-    Cur_Parameter RECORD;
-    -- Record Info
-    v_Bpartner VARCHAR2(32);
-    v_Mpricelist VARCHAR2(32);
-    v_Currency VARCHAR2(32);
-    v_Warehouse VARCHAR2(32);
-    v_SeqNo NUMBER;
-    v_CorderId varchar2(32);
-    v_Client VARCHAR2(32);
-    v_Org VARCHAR2(32);
-    v_Createdby VARCHAR2(32);
-    v_Dateordered DATE;
-    v_BillTo NUMBER;
-    v_BpartnerLocation VARCHAR2(32);
-    v_Pricelist VARCHAR2(32);
-    v_Pricelimit NUMBER;
-    v_Pricestd NUMBER;
-    v_Linenetamount NUMBER;
-    v_Discount VARCHAR2(32);
-    v_Tax VARCHAR2(32);
-    v_Issotrx CHAR;
-    v_NoOfLines NUMBER:=0;
-    v_projectID varchar2(32);
-    -- 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>
--- a/src-db/database/model/functions/COPY_PROJECT_LINES.xml	Thu Oct 09 14:13:04 2008 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,112 +0,0 @@
-<?xml version="1.0"?>
-  <database name="FUNCTION COPY_PROJECT_LINES">
-    <function name="COPY_PROJECT_LINES" type="NULL">
-      <parameter name="pinstance_id" type="VARCHAR" 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 VARCHAR2(32);
-  -- Parameter
-  TYPE RECORD IS REF CURSOR;
-    Cur_Parameter RECORD;
-    -- Parameter Variables
-    v_NoOfLines NUMBER:=0;
-    v_NextNo VARCHAR2(32);
-    -- Copy
-    Cur_ProjectLine 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) ;
-    -- Record_ID is the Tab_ID to copy to
-    v_ResultStr:='Copying';
-    FOR Cur_ProjectLine IN
-      (SELECT C_ProjectLine.*
-      FROM C_ProjectLine,
-        C_Project,
-        C_ProjectProposal
-      WHERE C_Project.C_PROJECT_ID=C_ProjectLine.C_PROJECT_ID
-        AND C_Project.C_PROJECT_ID=C_ProjectProposal.C_PROJECT_ID
-        AND C_ProjectProposal.C_PROJECTPROPOSAL_ID=v_Record_ID
-      )
-    LOOP
-      -- Get next no
-      AD_Sequence_Next('C_ProjectProposalLine', Cur_ProjectLine.AD_Client_ID, v_NextNo) ;
-      -- Insert
-      INSERT
-      INTO C_PROJECTPROPOSALLINE
-        (
-          C_PROJECTPROPOSALLINE_ID, AD_CLIENT_ID, AD_ORG_ID, ISACTIVE,
-          CREATED, CREATEDBY, UPDATED, UPDATEDBY,
-          C_PROJECTPROPOSAL_ID, LINENO, M_PRODUCT_ID, QTY,
-          PRICE, DESCRIPTION, PRODUCT_VALUE, PRODUCT_NAME,
-          PRODUCT_DESCRIPTION
-        )
-        VALUES
-        (
-          v_NextNo, Cur_ProjectLine.AD_Client_ID, Cur_ProjectLine.AD_Org_ID, Cur_ProjectLine.IsActive,
-          now(), '0', now(), '0',
-          v_Record_ID, Cur_ProjectLine.Line, Cur_ProjectLine.M_Product_ID, Cur_ProjectLine.PlannedQty,
-          Cur_ProjectLine.PlannedPrice, Cur_ProjectLine.Description, Cur_ProjectLine.Product_Value, Cur_ProjectLine.Product_Name,
-          Cur_ProjectLine.Product_Description
-        )
-        ;
-      -- update translation
-      --
-      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_PROJECT_LINES
-]]></body>
-    </function>
-  </database>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src-db/database/model/functions/C_ADD_MONTHS2.xml	Thu Oct 09 17:15:25 2008 +0000
@@ -0,0 +1,46 @@
+<?xml version="1.0"?>
+  <database name="FUNCTION C_ADD_MONTHS2">
+    <function name="C_ADD_MONTHS2" type="TIMESTAMP">
+      <parameter name="pdate" type="TIMESTAMP" mode="in">
+        <default/>
+      </parameter>
+      <parameter name="pmonths" 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) 2007 Openbravo SL
+* All Rights Reserved.
+* Contributor(s):  ______________________________________.
+************************************************************************/
+/*************************************************************************
+* Description: This function is is similar to oracle's add_months function
+*  In case the DATE is i.e. 28/02/2007 this function will return 28/03/2007
+*  while add_moths returns 31/03/2007
+************************************************************************/
+  pDateAux DATE;
+BEGIN
+  if (last_day(pDate)!=pDate) then
+    return add_months(pdate, pMonths);
+  else
+    pDateAux := add_months(pDate, pMonths);
+    if (to_number(to_char(pDateAux,'DD'))>to_number(to_char(pDate,'DD'))) then
+      return TO_DATE(to_char(pDate,'DD')||to_char(pDateAux,'-MM-YYYY'),'DD-MM-YYYY');
+    else
+      return pDateAux;
+    end if;
+  end if;
+END C_ADD_MONTHS2
+]]></body>
+    </function>
+  </database>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src-db/database/model/functions/C_AGING_GET_SCOPE.xml	Thu Oct 09 17:15:25 2008 +0000
@@ -0,0 +1,58 @@
+<?xml version="1.0"?>
+  <database name="FUNCTION C_AGING_GET_SCOPE">
+    <function name="C_AGING_GET_SCOPE" type="NUMERIC">
+      <parameter name="pdate" type="TIMESTAMP" mode="in">
+        <default/>
+      </parameter>
+      <parameter name="pcol1" type="NUMERIC" mode="in">
+        <default/>
+      </parameter>
+      <parameter name="pcol2" type="NUMERIC" mode="in">
+        <default/>
+      </parameter>
+      <parameter name="pcol3" type="NUMERIC" mode="in">
+        <default/>
+      </parameter>
+      <parameter name="pcol4" 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) 2008 Openbravo SL
+* All Rights Reserved.
+* Contributor(s):  ______________________________________.
+************************************************************************/
+/*************************************************************************
+* Description: This function is is similar to oracle's add_months function
+*  In case the DATE is i.e. 28/02/2007 this function will return 28/03/2007
+*  while add_moths returns 31/03/2007
+************************************************************************/
+  pDateAux DATE;
+BEGIN
+  if (pDate<now()) then
+    return 0;
+  elsif (pDate<=now()+pCol1) then
+    return 1;
+  elsif (pDate<=now()+pCol2) then
+    return 2;
+  elsif (pDate<=now()+pCol3) then
+    return 3;
+  elsif (pDate<=now()+pCol4) then
+    return 4;
+  else
+    return 5;
+  end if;
+END C_AGING_GET_SCOPE
+]]></body>
+    </function>
+  </database>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src-db/database/model/functions/C_CHANGEPROJECTSTATUS.xml	Thu Oct 09 17:15:25 2008 +0000
@@ -0,0 +1,134 @@
+<?xml version="1.0"?>
+  <database name="FUNCTION C_CHANGEPROJECTSTATUS">
+    <function name="C_CHANGEPROJECTSTATUS" type="NULL">
+      <parameter name="p_pinstance_id" type="VARCHAR" 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):  ______________________________________.
+************************************************************************/
+  v_resultstr         VARCHAR2 (2000) := '';
+  v_message           VARCHAR2 (2000) := '';
+  v_result NUMBER:= 1;                      -- 0=failure
+  v_record_id         varchar2(32);
+  v_ad_user_id        varchar2(32);
+
+  TYPE RECORD IS REF CURSOR;
+
+  cur_parameter       RECORD;
+  v_p_projectstatus   CHAR (2);
+  v_projectstatus     CHAR (2);
+  v_created NUMBER:= 0;
+  finish_process      BOOLEAN         := FALSE;
+BEGIN
+  -- Get Parameters
+  v_resultstr := 'ReadingParameters';
+
+  FOR cur_parameter IN (SELECT   i.record_id, i.ad_user_id, p.parametername,
+                                 p.p_string, p.p_number, p.p_date,
+                                 p.p_date_to
+                            FROM ad_pinstance i LEFT JOIN ad_pinstance_para p
+                                 ON i.ad_pinstance_id = p.ad_pinstance_id
+                           WHERE i.ad_pinstance_id = p_pinstance_id
+                        ORDER BY p.seqno)
+  LOOP
+    v_record_id := cur_parameter.record_id;
+    v_ad_user_id := cur_parameter.ad_user_id;
+
+    IF (cur_parameter.parametername = 'ChangeProjectStatus')
+    THEN
+      v_p_projectstatus := cur_parameter.p_string;
+      DBMS_OUTPUT.PUT_LINE('  ChangeProjectStatus=' || v_p_projectstatus);
+    ELSE
+      DBMS_OUTPUT.PUT_LINE('*** Unknown Parameter='|| cur_parameter.parametername);
+    END IF;
+  END LOOP;                                                   -- Get Parameter
+
+  DBMS_OUTPUT.PUT_LINE('  Record_ID=' || v_record_id);
+  -- Check that we have some restrictions
+  v_resultstr := 'CheckRestriction';
+
+  IF (v_p_projectstatus IS NULL)
+  THEN
+    RAISE_APPLICATION_ERROR(-20000, '@Notprojectstatus@');
+  END IF;
+
+  IF (NOT finish_process)
+  THEN
+    v_resultstr := 'Verify status';
+
+    SELECT projectstatus
+      INTO v_projectstatus
+      FROM c_project
+     WHERE c_project_id = v_record_id;
+
+    IF v_projectstatus IS NULL
+    THEN
+      RAISE_APPLICATION_ERROR(-20000, '@Notstatusdefined@');
+    ELSIF (v_projectstatus = 'NF' OR v_projectstatus = 'OP')
+    THEN
+      IF (v_p_projectstatus <> 'OR' AND v_p_projectstatus <> 'OU')
+      THEN
+        RAISE_APPLICATION_ERROR(-20000, '@Invalidprojectstatus@');
+      END IF;
+    ELSIF (v_projectstatus = 'OR')
+    THEN
+      IF (v_p_projectstatus <> 'OC')
+      THEN
+        RAISE_APPLICATION_ERROR(-20000, '@Invalidprojectstatus@');
+      END IF;
+    ELSIF (v_projectstatus = 'OC')
+    THEN
+      RAISE_APPLICATION_ERROR(-20000, '@Projectclose@');
+    END IF;
+  END IF;                                                     --FINISH_PROCESS
+
+  IF (NOT finish_process)
+  THEN
+    IF v_p_projectstatus = 'OR'
+    THEN
+      C_CHANGEPROJECTSTATUSTOORDER(p_pinstance_id);
+    ELSIF v_p_projectstatus = 'OC'
+    THEN
+      C_CLOSEPROJECT(p_pinstance_id);
+    ELSE
+      UPDATE c_project
+         SET projectstatus = v_p_projectstatus
+       WHERE c_project_id = v_record_id;
+
+      COMMIT;
+    END IF;
+
+    RETURN;
+  END IF;--FINISH_PROCESS
+
+  --<<FINISH_PROCESS>>
+  --  Update AD_PInstance
+  DBMS_OUTPUT.PUT_LINE('Updating PInstance - Finished ' || v_message);
+  AD_UPDATE_PINSTANCE(p_pinstance_id, NULL, 'N', v_result, v_message);
+  RETURN;
+EXCEPTION
+  WHEN OTHERS
+  THEN
+    v_resultstr:= '@ERROR=' || SQLERRM;
+    DBMS_OUTPUT.PUT_LINE(v_resultstr);
+    ROLLBACK;
+    AD_UPDATE_PINSTANCE(p_pinstance_id, NULL, 'N', 0, v_resultstr);
+-- Commented by cromero 19102006 RETURN;
+END C_CHANGEPROJECTSTATUS
+]]></body>
+    </function>
+  </database>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src-db/database/model/functions/C_CHANGEPROJECTSTATUSTOORDER.xml	Thu Oct 09 17:15:25 2008 +0000
@@ -0,0 +1,338 @@
+<?xml version="1.0"?>
+  <database name="FUNCTION C_CHANGEPROJECTSTATUSTOORDER">
+    <function name="C_CHANGEPROJECTSTATUSTOORDER" type="NULL">
+      <parameter name="p_pinstance_id" type="VARCHAR" 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-2008 Openbravo SL
+* All Rights Reserved.
+* Contributor(s):  ______________________________________.
+************************************************************************/
+  v_ResultStr VARCHAR2(2000):='';
+  v_Message VARCHAR2(2000):='';
+  v_Result NUMBER:=1; -- 0=failure
+  v_Record_ID VARCHAR2(32);
+  v_AD_User_ID VARCHAR2(32);
+  v_AD_Client_ID VARCHAR2(32);
+  v_AD_Org_ID VARCHAR2(32);
+  TYPE RECORD IS REF CURSOR;
+    Cur_Parameter RECORD;
+    v_ProjectStatus CHAR(2) ;
+    v_projectcategory   CHAR (2);
+    v_temppricelist     CHAR (1);
+    v_Created NUMBER:=0;
+    v_PriceListVersion NUMBER;
+    v_PriceList NUMBER;
+    v_PriceListVersionPO NUMBER;
+    v_PriceListPO NUMBER;
+    v_C_Currency_ID VARCHAR2(32);
+    v_TotalPhases NUMBER;
+    v_TotalLines NUMBER;
+    v_BPName VARCHAR2(2000):='';
+    v_SupplierName VARCHAR2(2000):='';
+    FINISH_PROCESS BOOLEAN:=false;
+  BEGIN
+    --  Update AD_PInstance
+    DBMS_OUTPUT.PUT_LINE('Updating PInstance - Processing ' || p_PInstance_ID) ;
+    v_ResultStr:='PInstanceNotFound';
+    AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'Y', NULL, NULL) ;
+  BEGIN --BODY
+    -- Get PARAMETERS
+    v_ResultStr:='ReadingParameters';
+    FOR Cur_Parameter IN
+      (SELECT i.Record_ID,
+        i.AD_User_ID,
+        i.AD_Client_ID,
+        i.AD_Org_ID,
+        p.ParameterName,
+        p.P_String,
+        p.P_Number,
+        p.P_Date,
+        p.P_Date_To
+      FROM AD_PInstance i
+      LEFT JOIN AD_PInstance_Para p
+        ON i.AD_PInstance_ID=p.AD_PInstance_ID
+      WHERE i.AD_PInstance_ID=p_PInstance_ID
+      ORDER BY p.SeqNo
+      )
+    LOOP
+      v_Record_ID:=Cur_Parameter.Record_ID;
+      v_AD_User_ID:=Cur_Parameter.AD_User_ID;
+      IF(Cur_Parameter.ParameterName='ChangeProjectStatus') THEN
+        v_ProjectStatus:=Cur_Parameter.P_String;
+        DBMS_OUTPUT.PUT_LINE('  ChangeProjectStatus=' || v_ProjectStatus) ;
+      ELSE
+        DBMS_OUTPUT.PUT_LINE('*** Unknown Parameter=' || Cur_Parameter.ParameterName) ;
+      END IF;
+    END LOOP; -- Get Parameter
+    DBMS_OUTPUT.PUT_LINE('  Record_ID=' || v_Record_ID) ;
+    -- Check that we have some restrictions
+    v_ResultStr:='CheckRestriction';
+    IF(v_ProjectStatus IS NULL) THEN
+      RAISE_APPLICATION_ERROR(-20000, '@Notprojectstatus@');
+    ELSIF v_ProjectStatus<>'OR' THEN
+      RAISE_APPLICATION_ERROR(-20000, '@Invalidprojectstatus@');
+    END IF;
+    IF(NOT FINISH_PROCESS) THEN
+      DECLARE
+        Cur_SO RECORD;
+        Cur_Lines RECORD;
+        Cur_LinesPO RECORD;
+      BEGIN
+        v_ResultStr:='StartLoop';
+        FOR Cur_SO IN
+          (SELECT * FROM C_PROJECT WHERE C_PROJECT_ID=v_Record_ID)
+        LOOP
+          v_AD_Client_ID:=Cur_SO.AD_Client_ID;
+          v_AD_Org_ID:=Cur_SO.AD_Org_ID;
+          IF Cur_SO.C_BPARTNER_ID IS NULL THEN
+            RAISE_APPLICATION_ERROR(-20000, '@NoprojectBusinesspartner@');
+            /* Eliminado a peticion de Huesker
+            ELSIF Cur_SO.AD_USER_ID IS NULL THEN
+            v_Message := 'ContactNotFound';
+            v_Result := 0;
+            FINISH_PROCESS := true;
+            EXIT; */
+          ELSIF Cur_SO.C_BPARTNER_LOCATION_ID IS NULL THEN
+            RAISE_APPLICATION_ERROR(-20000, '@ThebusinessPartner@'||' '||' @ShiptoNotdefined@');
+          ELSIF Cur_SO.BILLTO_ID IS NULL THEN
+            RAISE_APPLICATION_ERROR(-20000, '@ThebusinessPartner@'||' '||' @BillToNotdefined@');
+          ELSIF Cur_SO.C_PAYMENTTERM_ID IS NULL THEN
+            RAISE_APPLICATION_ERROR(-20000, '@ThebusinessPartner@'||' '||' @PaymenttermNotdefined@');
+          ELSIF Cur_SO.C_CURRENCY_ID IS NULL THEN
+            RAISE_APPLICATION_ERROR(-20000, '@ProjectCurrencyNotFound@');
+          ELSIF Cur_SO.M_WAREHOUSE_ID IS NULL THEN
+            RAISE_APPLICATION_ERROR(-20000, '@ProjectWarehouseNotFound@');
+          ELSIF Cur_SO.SALESREP_ID IS NULL THEN
+            RAISE_APPLICATION_ERROR(-20000, '@ProjectSalesRepNotFound@');
+          END IF;
+          /*Added in order to work with Multiphase Project*/
+          IF Cur_SO.PROJECTCATEGORY<>'S' THEN
+            SELECT COUNT(*)
+            INTO v_TotalLines
+            FROM C_PROJECTLINE
+            WHERE C_PROJECT_ID=v_Record_ID;
+            IF v_TotalLines=0 THEN
+              RAISE_APPLICATION_ERROR(-20000, '@NoProjectLines@');
+            END IF;
+          ELSE
+            SELECT COUNT(*)
+            INTO v_TotalPhases
+            FROM C_PROJECTPHASE
+            WHERE C_PROJECT_ID=v_Record_ID;
+            IF v_TotalPhases=0 THEN
+              RAISE_APPLICATION_ERROR(-20000, '@NoProjectPhases@');
+            END IF;
+          END IF;
+          /*In order to create a particular price list for the current project,
+            project must be a service project and Create Temporary Price List option must be checked*/
+          IF (Cur_SO.PROJECTCATEGORY<>'S' AND Cur_SO.CREATETEMPPRICELIST='Y') THEN
+
+            Ad_Sequence_Next('M_PriceList', v_AD_Client_ID, v_PriceList) ;
+            
+            SELECT NAME 
+            INTO v_BPName
+            FROM C_BPARTNER 
+            WHERE C_BPARTNER_ID = Cur_SO.C_BPARTNER_ID; 
+                       
+            INSERT
+            INTO m_pricelist
+              (
+                M_PRICELIST_ID, AD_CLIENT_ID, AD_ORG_ID, ISACTIVE,
+                CREATED, CREATEDBY, UPDATED, UPDATEDBY,
+                NAME, 
+				DESCRIPTION, 
+	        	ISSOPRICELIST, C_Currency_ID
+              )
+              VALUES
+              (
+                v_PriceList, v_AD_Client_ID, v_AD_Org_ID, 'Y',
+                now(), v_AD_User_ID, now(), v_AD_User_ID,
+                (Cur_SO.value || ' - ' || v_BPName), 
+				(Cur_SO.value || ' - ' || v_BPName), 
+				'Y', Cur_SO.C_Currency_ID
+              )
+              ;
+            Ad_Sequence_Next('M_PriceList_Version', v_AD_Client_ID, v_PriceListVersion) ;
+            INSERT
+            INTO M_PRICELIST_VERSION
+              (
+                M_PRICELIST_VERSION_ID, AD_CLIENT_ID, AD_ORG_ID, ISACTIVE,
+                CREATED, CREATEDBY, UPDATED, UPDATEDBY,
+                NAME, 
+				DESCRIPTION, 
+				M_PRICELIST_ID, M_DISCOUNTSCHEMA_ID,
+                VALIDFROM
+              )
+              VALUES
+              (
+                v_PriceListVersion, v_AD_Client_ID, v_AD_Org_ID, 'Y',
+                now(), v_AD_User_ID, now(), v_AD_User_ID,
+                (Cur_SO.value || ' - ' ||v_BPName|| ' - ' ||TRUNC(now(), 'DD')), 
+				(Cur_SO.value || ' - ' ||v_BPName|| ' - ' ||TRUNC(now(), 'DD')), 
+				v_PriceList, (SELECT MIN(M_DISCOUNTSCHEMA_ID) AS ID FROM M_DISCOUNTSCHEMA WHERE isactive='Y'),
+                TRUNC(now(), 'DD')
+              )
+              ;
+            FOR Cur_Lines IN
+              (SELECT * FROM C_PROJECTLINE WHERE C_PROJECT_ID=v_Record_ID)
+            LOOP
+              INSERT
+              INTO M_PRODUCTPRICE
+                (
+                  M_PRICELIST_VERSION_ID, M_PRODUCT_ID, AD_CLIENT_ID, AD_ORG_ID,
+                  ISACTIVE, CREATED, CREATEDBY, UPDATED,
+                  UPDATEDBY, PRICELIST, PRICESTD, PRICELIMIT
+                )
+                VALUES
+                (
+                  v_PriceListVersion, Cur_Lines.M_Product_ID, v_AD_Client_ID, v_AD_Org_ID,
+                  'Y', now(), v_AD_User_ID, now(), v_AD_User_ID,
+                  Cur_Lines.PLANNEDPRICE, Cur_Lines.PLANNEDPRICE, Cur_Lines.PLANNEDPRICE
+                )
+                ;
+            END LOOP;
+            SELECT COUNT(*)
+            INTO v_TotalLines
+            FROM C_PROJECTLINE
+            WHERE C_PROJECT_ID=v_Record_ID
+              AND PLANNEDPOPRICE IS NOT NULL;
+            IF v_TotalLines>0 THEN
+              Ad_Sequence_Next('M_PriceList', v_AD_Client_ID, v_PriceListPO) ;
+              
+              SELECT max(bp.name) 
+			  INTO v_SupplierName
+			  FROM c_project p, c_project_vendor pv, c_bpartner bp 
+			  WHERE p.c_project_id = v_Record_ID 
+			  AND p.c_project_id = pv.c_project_id 
+			  AND pv.c_bpartner_id = bp.c_bpartner_id 
+			  AND pv.isactive = 'Y';
+	      	  IF (v_SupplierName IS NULL) THEN
+			  v_SupplierName := '';
+	     	  END IF;
+              
+	      INSERT
+              INTO m_pricelist
+                (
+                  M_PRICELIST_ID, AD_CLIENT_ID, AD_ORG_ID, ISACTIVE,
+                  CREATED, CREATEDBY, UPDATED, UPDATEDBY,
+                  NAME, 
+	              DESCRIPTION, 
+		          ISSOPRICELIST, C_Currency_ID
+                )
+                VALUES
+                (
+                  v_PriceListPO, v_AD_Client_ID, v_AD_Org_ID, 'Y',
+                  now(), v_AD_User_ID, now(), v_AD_User_ID,
+                  (Cur_SO.value || ' - ' ||v_SupplierName), 
+	              (Cur_SO.value || ' - ' ||v_SupplierName),
+		          'N', Cur_SO.C_Currency_ID
+                )
+                ;
+              Ad_Sequence_Next('M_PriceList_Version', v_AD_Client_ID, v_PriceListVersionPO) ;
+              INSERT
+              INTO M_PRICELIST_VERSION
+                (
+                  M_PRICELIST_VERSION_ID, AD_CLIENT_ID, AD_ORG_ID, ISACTIVE,
+                  CREATED, CREATEDBY, UPDATED, UPDATEDBY, 
+		  		  NAME,
+                  DESCRIPTION, 
+		  		  M_PRICELIST_ID, M_DISCOUNTSCHEMA_ID, 
+		  		  VALIDFROM
+                )
+                VALUES
+                (
+                  v_PriceListVersionPO, v_AD_Client_ID, v_AD_Org_ID, 'Y',
+                  now(), v_AD_User_ID, now(), v_AD_User_ID,
+                  (Cur_SO.value || ' - ' ||v_SupplierName|| ' - ' ||TRUNC(now(), 'DD')), 
+		          (Cur_SO.value || ' - ' ||v_SupplierName||' - ' ||TRUNC(now(), 'DD')), 
+		          v_PriceListPO, (SELECT MIN(M_DISCOUNTSCHEMA_ID) AS ID FROM M_DISCOUNTSCHEMA WHERE isactive='Y'),
+                  TRUNC(now(), 'DD')
+                )
+                ;
+              FOR Cur_LinesPO IN
+                (SELECT *
+                FROM C_PROJECTLINE
+                WHERE C_PROJECT_ID=v_Record_ID
+                  AND PLANNEDPOPRICE IS NOT NULL
+                )
+              LOOP
+                INSERT
+                INTO M_PRODUCTPRICE
+                  (
+                    M_PRICELIST_VERSION_ID, M_PRODUCT_ID, AD_CLIENT_ID, AD_ORG_ID, ISACTIVE,
+                    CREATED, CREATEDBY, UPDATED, UPDATEDBY,
+                    PRICELIST, PRICESTD, PRICELIMIT
+                  )
+                  VALUES
+                  (
+                    v_PriceListVersionPO, Cur_LinesPO.M_Product_ID, v_AD_Client_ID, v_AD_Org_ID,
+                     'Y', now(), v_AD_User_ID, now(),
+                    v_AD_User_ID, Cur_LinesPO.PLANNEDPOPRICE, Cur_LinesPO.PLANNEDPOPRICE, Cur_LinesPO.PLANNEDPOPRICE
+                  )
+                  ;
+              END LOOP;
+            ELSE
+              SELECT MIN(M_PRICELIST_ID)
+              INTO v_PriceListPO
+              FROM M_PRICELIST
+              WHERE ISSOPRICELIST='N'
+                AND ISACTIVE='Y'
+                AND AD_CLIENT_ID=v_AD_Client_ID;
+            END IF;
+          END IF;
+        END LOOP;
+        IF v_PriceListPO IS NOT NULL THEN
+          UPDATE C_PROJECT_VENDOR
+            SET m_pricelist_id=v_PriceListPO
+          WHERE c_project_id=v_Record_ID;
+        END IF;
+
+        /*In order to update the price list of the current project,
+          project must be a service project and Create Temporary Price List option must be checked*/
+        SELECT projectcategory, createtemppricelist
+        INTO v_projectcategory, v_temppricelist
+        FROM c_project
+        WHERE c_project_id = v_Record_ID;
+
+        IF (v_projectcategory<>'S' AND v_temppricelist='Y') THEN
+          UPDATE C_PROJECT
+            SET projectstatus=v_ProjectStatus,
+            m_pricelist_id=v_PriceList
+          WHERE c_project_id=v_Record_ID;
+        ELSE
+          UPDATE C_PROJECT
+            SET projectstatus=v_ProjectStatus
+          WHERE c_project_id=v_Record_ID;
+        END IF;
+      END;
+    END IF; --FINISH_PROCESS
+    --<<FINISH_PROCESS>>
+    --  Update AD_PInstance
+    DBMS_OUTPUT.PUT_LINE('Updating PInstance - Finished ' || v_Message) ;
+    AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', v_Result, v_Message) ;
+    RETURN;
+  END; --BODY
+EXCEPTION
+WHEN OTHERS THEN
+  v_ResultStr:= '@ERROR=' || SQLERRM;
+  DBMS_OUTPUT.PUT_LINE(v_ResultStr) ;
+  ROLLBACK;
+  AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', 0, v_ResultStr) ;
+  RETURN;
+END C_CHANGEPROJECTSTATUSTOORDER
+]]></body>
+    </function>
+  </database>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src-db/database/model/functions/C_CLOSEPROJECT.xml	Thu Oct 09 17:15:25 2008 +0000
@@ -0,0 +1,138 @@
+<?xml version="1.0"?>
+  <database name="FUNCTION C_CLOSEPROJECT">
+    <function name="C_CLOSEPROJECT" type="NULL">
+      <parameter name="p_pinstance_id" type="VARCHAR" 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-2008 Openbravo SL
+* All Rights Reserved.
+* Contributor(s):  ______________________________________.
+************************************************************************/
+  v_ResultStr VARCHAR2(2000):='';
+  v_Message VARCHAR2(2000):='';
+  v_Result NUMBER:=1; -- 0=failure
+  v_p_Record_ID VARCHAR2(32);
+  v_AD_User_ID VARCHAR2(32);
+  TYPE RECORD IS REF CURSOR;
+    Cur_Parameter RECORD;
+    v_ProjectStatus CHAR(2) ;
+    v_projectcategory   CHAR (2);
+    v_temppricelist     CHAR (1);
+    v_Created NUMBER:=0;
+    FINISH_PROCESS BOOLEAN:=false;
+  BEGIN
+    --  Update AD_PInstance
+    DBMS_OUTPUT.PUT_LINE('Updating PInstance - Processing ' || p_PInstance_ID) ;
+    v_ResultStr:='PInstanceNotFound';
+    AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'Y', NULL, NULL) ;
+  BEGIN --BODY
+    -- Get PARAMETERS
+    v_ResultStr:='ReadingParameters';
+    FOR Cur_Parameter IN
+      (SELECT i.Record_ID,
+        i.AD_User_ID,
+        i.AD_Client_ID,
+        i.AD_Org_ID,
+        p.ParameterName,
+        p.P_String,
+        p.P_Number,
+        p.P_Date,
+        p.P_Date_To
+      FROM AD_PInstance i
+      LEFT JOIN AD_PInstance_Para p
+        ON i.AD_PInstance_ID=p.AD_PInstance_ID
+      WHERE i.AD_PInstance_ID=p_PInstance_ID
+      ORDER BY p.SeqNo
+      )
+    LOOP
+      v_p_Record_ID:=Cur_Parameter.Record_ID;
+      v_AD_User_ID:=Cur_Parameter.AD_User_ID;
+      IF(Cur_Parameter.ParameterName='ChangeProjectStatus') THEN
+        v_ProjectStatus:=Cur_Parameter.P_String;
+        DBMS_OUTPUT.PUT_LINE('  ChangeProjectStatus=' || v_ProjectStatus) ;
+      ELSE
+        DBMS_OUTPUT.PUT_LINE('*** Unknown Parameter=' || Cur_Parameter.ParameterName) ;
+      END IF;
+    END LOOP; -- Get Parameter
+    DBMS_OUTPUT.PUT_LINE('  Record_ID=' || v_p_Record_ID) ;
+    -- Check that we have some restrictions
+
+    SELECT projectcategory, createtemppricelist
+    INTO v_projectcategory, v_temppricelist
+    FROM c_project
+    WHERE c_project_id = v_p_Record_ID;
+
+    v_ResultStr:='CheckRestriction';
+    IF(v_ProjectStatus IS NULL) THEN
+      RAISE_APPLICATION_ERROR(-20000, '@Notprojectstatus@');
+    ELSIF v_ProjectStatus<>'OC' THEN
+      RAISE_APPLICATION_ERROR(-20000, '@Invalidprojectstatus@');
+    END IF;
+    IF(NOT FINISH_PROCESS) THEN
+      v_ResultStr:='StartLoop';
+      IF (v_projectcategory<>'S' AND v_temppricelist='Y') THEN
+        UPDATE M_PRICELIST_VERSION
+          SET ISACTIVE='N',
+          updated=now(),
+          updatedby=v_AD_User_ID
+        WHERE M_PRICELIST_ID IN
+          (  SELECT M_PRICELIST_ID FROM C_PROJECT WHERE C_PROJECT_ID=v_p_Record_ID)
+          ;
+        UPDATE M_PRICELIST
+          SET ISACTIVE='N',
+          updated=now(),
+          updatedby=v_AD_User_ID
+        WHERE M_PRICELIST_ID IN
+          (  SELECT M_PRICELIST_ID FROM C_PROJECT WHERE C_PROJECT_ID=v_p_Record_ID)
+          ;
+        UPDATE M_PRICELIST_VERSION
+          SET ISACTIVE='N',
+          updated=now(),
+          updatedby=v_AD_User_ID
+        WHERE M_PRICELIST_ID IN
+          ( SELECT M_PRICELIST_ID FROM C_PROJECT_VENDOR WHERE C_PROJECT_ID=v_p_Record_ID)
+          ;
+        UPDATE M_PRICELIST
+          SET ISACTIVE='N',
+          updated=now(),
+          updatedby=v_AD_User_ID
+        WHERE M_PRICELIST_ID IN
+          (  SELECT M_PRICELIST_ID FROM C_PROJECT_VENDOR WHERE C_PROJECT_ID=v_p_Record_ID)
+          ;
+      END IF;
+      UPDATE C_PROJECT
+        SET projectstatus=v_ProjectStatus,
+        DATEFINISH=now(),
+        updated=now(),
+        updatedby=v_AD_User_ID
+      WHERE c_project_id=v_p_Record_ID;
+    END IF; --FINISH_PROCESS
+    --<<FINISH_PROCESS>>
+    --  Update AD_PInstance
+    DBMS_OUTPUT.PUT_LINE('Updating PInstance - Finished ' || v_Message) ;
+    AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', v_Result, v_Message) ;
+    RETURN;
+  END; --BODY
+EXCEPTION
+WHEN OTHERS THEN
+  v_ResultStr:= '@ERROR=' || SQLERRM;
+  DBMS_OUTPUT.PUT_LINE(v_ResultStr) ;
+  ROLLBACK;
+  AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', 0, v_ResultStr) ;
+  RETURN;
+END C_CLOSEPROJECT
+]]></body>
+    </function>
+  </database>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src-db/database/model/functions/C_COPY_PROJECT_LINES.xml	Thu Oct 09 17:15:25 2008 +0000
@@ -0,0 +1,112 @@
+<?xml version="1.0"?>
+  <database name="FUNCTION C_COPY_PROJECT_LINES">
+    <function name="C_COPY_PROJECT_LINES" type="NULL">
+      <parameter name="pinstance_id" type="VARCHAR" 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 VARCHAR2(32);
+  -- Parameter
+  TYPE RECORD IS REF CURSOR;
+    Cur_Parameter RECORD;
+    -- Parameter Variables
+    v_NoOfLines NUMBER:=0;
+    v_NextNo VARCHAR2(32);
+    -- Copy
+    Cur_ProjectLine 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) ;
+    -- Record_ID is the Tab_ID to copy to
+    v_ResultStr:='Copying';
+    FOR Cur_ProjectLine IN
+      (SELECT C_ProjectLine.*
+      FROM C_ProjectLine,
+        C_Project,
+        C_ProjectProposal
+      WHERE C_Project.C_PROJECT_ID=C_ProjectLine.C_PROJECT_ID
+        AND C_Project.C_PROJECT_ID=C_ProjectProposal.C_PROJECT_ID
+        AND C_ProjectProposal.C_PROJECTPROPOSAL_ID=v_Record_ID
+      )
+    LOOP
+      -- Get next no
+      AD_Sequence_Next('C_ProjectProposalLine', Cur_ProjectLine.AD_Client_ID, v_NextNo) ;
+      -- Insert
+      INSERT
+      INTO C_PROJECTPROPOSALLINE
+        (
+          C_PROJECTPROPOSALLINE_ID, AD_CLIENT_ID, AD_ORG_ID, ISACTIVE,
+          CREATED, CREATEDBY, UPDATED, UPDATEDBY,
+          C_PROJECTPROPOSAL_ID, LINENO, M_PRODUCT_ID, QTY,
+          PRICE, DESCRIPTION, PRODUCT_VALUE, PRODUCT_NAME,
+          PRODUCT_DESCRIPTION
+        )
+        VALUES
+        (
+          v_NextNo, Cur_ProjectLine.AD_Client_ID, Cur_ProjectLine.AD_Org_ID, Cur_ProjectLine.IsActive,
+          now(), '0', now(), '0',
+          v_Record_ID, Cur_ProjectLine.Line, Cur_ProjectLine.M_Product_ID, Cur_ProjectLine.PlannedQty,
+          Cur_ProjectLine.PlannedPrice, Cur_ProjectLine.Description, Cur_ProjectLine.Product_Value, Cur_ProjectLine.Product_Name,
+          Cur_ProjectLine.Product_Description
+        )
+        ;
+      -- update translation
+      --
+      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 C_COPY_PROJECT_LINES
+]]></body>
+    </function>
+  </database>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src-db/database/model/functions/C_DIVIDE.xml	Thu Oct 09 17:15:25 2008 +0000
@@ -0,0 +1,34 @@
+<?xml version="1.0"?>
+  <database name="FUNCTION C_DIVIDE">
+    <function name="C_DIVIDE" type="NUMERIC">
+      <parameter name="p_dividendo" type="NUMERIC" mode="in">
+        <default/>
+      </parameter>
+      <parameter name="p_divisor" 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):  ______________________________________.
+************************************************************************/
+BEGIN
+  RETURN(p_dividendo/p_divisor) ;
+EXCEPTION
+WHEN OTHERS THEN
+  RETURN(NULL) ;
+END C_DIVIDE
+]]></body>
+    </function>
+  </database>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src-db/database/model/functions/C_GENERATEPOFROMPROJECT.xml	Thu Oct 09 17:15:25 2008 +0000
@@ -0,0 +1,354 @@
+<?xml version="1.0"?>
+  <database name="FUNCTION C_GENERATEPOFROMPROJECT">
+    <function name="C_GENERATEPOFROMPROJECT" type="NULL">
+      <parameter name="p_pinstance_id" type="VARCHAR" 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-2008 Openbravo SL
+* All Rights Reserved.
+* Contributor(s):  ______________________________________.
+************************************************************************/
+  v_ResultStr VARCHAR2(2000):='';
+  v_Message VARCHAR2(2000):='';
+  v_Result NUMBER:=1; -- 0=failure
+  v_Record_ID VARCHAR2(32);
+  v_AD_User_ID VARCHAR2(32);
+  TYPE RECORD IS REF CURSOR;
+    Cur_Parameter RECORD;
+    v_C_Order_ID VARCHAR2(32);
+    v_C_OrderLine_ID VARCHAR2(32);
+    v_C_DOCTYPE_ID VARCHAR2(32);
+    v_DocumentNo NVARCHAR2(30) ;
+  BEGIN
+    --  Update AD_PInstance
+    DBMS_OUTPUT.PUT_LINE('Updating PInstance - Processing ' || p_PInstance_ID) ;
+    v_ResultStr:='PInstanceNotFound';
+    AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'Y', NULL, NULL) ;
+  BEGIN --BODY
+    -- Get PARAMETERS
+    v_ResultStr:='ReadingParameters';
+    FOR Cur_Parameter IN
+      (SELECT i.Record_ID,
+        i.AD_User_ID,
+        i.AD_Client_ID,
+        i.AD_Org_ID,
+        p.ParameterName,
+        p.P_String,
+        p.P_Number,
+        p.P_Date,
+        p.P_Date_To
+      FROM AD_PInstance i
+      LEFT JOIN AD_PInstance_Para p
+        ON i.AD_PInstance_ID=p.AD_PInstance_ID
+      WHERE i.AD_PInstance_ID=p_PInstance_ID
+      ORDER BY p.SeqNo
+      )
+    LOOP
+      v_Record_ID:=Cur_Parameter.Record_ID;
+      v_AD_User_ID:=Cur_Parameter.AD_User_ID;
+    END LOOP; -- Get Parameter
+    DBMS_OUTPUT.PUT_LINE('  Record_ID=' || v_Record_ID) ;
+    DECLARE
+      v_BPartner_Location_ID VARCHAR2(32);
+      v_BillTo_ID VARCHAR2(32);
+      v_Size NUMBER;
+      v_UOM NUMBER;
+      v_PriceStd NUMBER;
+      v_PriceList NUMBER;
+      v_PriceLimit NUMBER;
+      v_C_UOM_ID VARCHAR2(32);
+      v_C_Currency_ID VARCHAR2(32);
+      v_PriceActual NUMBER;
+      v_Discount NUMBER;
+      v_Tax_ID VARCHAR2(32);
+      v_M_PriceList_Version_ID VARCHAR2(32);
+
+    TYPE RECORD IS REF CURSOR;
+      Cur_SO RECORD;
+      Cur_SOLINES RECORD;
+    BEGIN
+      v_ResultStr:='StartLoop';
+      FOR Cur_SO IN
+        (SELECT P.C_PROJECT_ID,
+          P.PROJECTSTATUS,
+          V.AD_CLIENT_ID,
+          V.AD_ORG_ID,
+          V.C_BPARTNER_ID,
+          V.C_INCOTERMS_ID,
+          V.C_PROJECT_VENDOR_ID,
+          V.INCOTERMS_DESCRIPTION,
+          V.M_PRICELIST_VERSION_ID,
+          V.M_PRICELIST_ID,
+          P.C_CAMPAIGN_ID,
+          PL.C_CURRENCY_ID,
+	  	  P.C_CURRENCY_ID AS PROJCURRENCY,
+          BP.PAYMENTRULEPO AS PAYMENTRULE,
+          BP.PO_PAYMENTTERM_ID AS C_PAYMENTTERM_ID,
+          P.M_WAREHOUSE_ID,
+          P.POREFERENCE,
+          P.SALESREP_ID,
+          P.CREATETEMPPRICELIST,
+          P.DESCRIPTION
+        FROM C_PROJECT P,
+          C_PROJECT_VENDOR V,
+          C_BPARTNER BP,
+          M_PRICELIST PL
+        WHERE P.C_PROJECT_ID=V.C_PROJECT_ID
+          AND V.C_BPARTNER_ID=BP.C_BPARTNER_ID
+          AND V.M_PRICELIST_ID=PL.M_PRICELIST_ID
+          AND V.ISACTIVE='Y'
+          AND V.C_PROJECT_VENDOR_ID=v_Record_ID
+        )
+      LOOP
+        -- Check that we have some restrictions
+        v_ResultStr:='CheckRestriction';
+        IF(Cur_SO.PROJECTSTATUS IS NULL OR Cur_SO.PROJECTSTATUS<>'OR') THEN
+         RAISE_APPLICATION_ERROR(-20000, '@Invalidprojectstatus@'||'. '||' @ChangeToOrder@'||'.');
+        ELSIF Cur_SO.C_BPARTNER_ID IS NULL THEN
+          RAISE_APPLICATION_ERROR(-20000, '@NoprojectBusinesspartner@');
+        ELSIF Cur_SO.C_PAYMENTTERM_ID IS NULL THEN
+          RAISE_APPLICATION_ERROR(-20000, '@ThebusinessPartner@'||' '||' @PaymenttermNotdefined@'||'.');
+        ELSIF Cur_SO.M_PRICELIST_ID IS NULL THEN
+          RAISE_APPLICATION_ERROR(-20000, '@ThebusinessPartner@'||' '||' @PricelistNotdefined@'||'.');
+        ELSIF Cur_SO.C_CURRENCY_ID IS NULL THEN
+          RAISE_APPLICATION_ERROR(-20000, '@ProjectCurrencyNotFound@'||'.');
+        ELSIF Cur_SO.M_WAREHOUSE_ID IS NULL THEN
+          RAISE_APPLICATION_ERROR(-20000, '@ProjectWarehouseNotFound@'||'.');
+        ELSIF Cur_SO.SALESREP_ID IS NULL THEN
+          RAISE_APPLICATION_ERROR(-20000, '@ProjectSalesRepNotFound@'||'.');
+        END IF;
+
+        v_C_DOCTYPE_ID:=Ad_Get_DocType(Cur_SO.AD_Client_ID, Cur_SO.AD_Org_ID, 'POO') ;
+        AD_Sequence_DocType(v_C_DOCTYPE_ID, Cur_SO.AD_Client_ID, 'Y', v_DocumentNo) ;
+        IF(v_DocumentNo IS NULL) THEN
+          AD_Sequence_Doc('DocumentNo_C_Order', Cur_SO.AD_Client_ID, 'Y', v_DocumentNo) ;
+        END IF;
+
+        -- Get Business Partner Ship Location
+        v_BPartner_Location_ID := C_GetBPLocationID(Cur_SO.C_BPartner_ID, 'S') ;
+        IF (v_BPartner_Location_ID IS NULL) THEN
+          RAISE_APPLICATION_ERROR(-20000, '@ThebusinessPartner@'||' '||' @ShiptoNotdefined@'||'.');
+        END IF;
+
+        -- Get Business Partner Bill Location
+        v_Billto_ID := C_GetBPLocationID(Cur_SO.C_BPartner_ID, 'B') ;
+        IF (v_Billto_ID IS NULL) THEN
+          RAISE_APPLICATION_ERROR(-20000, '@ThebusinessPartner@'||' '||' @BillToNotdefined@'||'.');
+        END IF;
+
+        -- Get next C_Order_ID
+        Ad_Sequence_Next('C_Order', Cur_SO.AD_CLIENT_ID, v_C_Order_ID) ;
+        v_ResultStr:='C_ORDER_ID - ' || v_C_Order_ID;
+
+        INSERT
+        INTO C_ORDER
+          (
+            C_ORDER_ID, AD_CLIENT_ID, AD_ORG_ID, CREATEDBY,
+            UPDATEDBY, ISSOTRX, DOCUMENTNO, DOCSTATUS,
+            DOCACTION, C_DOCTYPE_ID, C_DOCTYPETARGET_ID, DATEORDERED,
+            DATEACCT, C_BPARTNER_ID, C_BPARTNER_LOCATION_ID, ISDISCOUNTPRINTED,
+            C_CURRENCY_ID, PAYMENTRULE, C_PAYMENTTERM_ID, INVOICERULE,
+            DELIVERYRULE, FREIGHTCOSTRULE, DELIVERYVIARULE, PRIORITYRULE,
+            TOTALLINES, GRANDTOTAL, M_WAREHOUSE_ID, M_PRICELIST_ID,
+            ISTAXINCLUDED, POSTED, PROCESSING, SALESREP_ID,
+            BILLTO_ID, POREFERENCE, C_CAMPAIGN_ID, C_PROJECT_ID,
+            AD_USER_ID, COPYFROM, C_INCOTERMS_ID, INCOTERMSDESCRIPTION,
+            DATEPROMISED, DESCRIPTION
+          )
+          VALUES
+          (
+            v_C_Order_ID, Cur_SO.AD_CLIENT_ID, Cur_SO.AD_ORG_ID, v_AD_User_ID,
+            v_AD_User_ID, 'N', v_DocumentNo, 'DR',
+             'CO', '0', v_C_DOCTYPE_ID, TRUNC(now(), 'DD'),
+            TRUNC(now(), 'DD'), Cur_SO.C_BPARTNER_ID, v_BPartner_Location_ID, 'N',
+            Cur_SO.C_CURRENCY_ID, COALESCE(Cur_SO.PAYMENTRULE, 'P'), Cur_SO.C_PAYMENTTERM_ID, 'D',
+             'A', 'I', 'D', '5',
+            0, 0, Cur_SO.M_WAREHOUSE_ID, Cur_SO.M_PRICELIST_ID,
+             'N', 'N', 'N', Cur_SO.SALESREP_ID,
+            v_BillTo_ID, Cur_SO.POREFERENCE, Cur_SO.C_CAMPAIGN_ID, Cur_SO.C_PROJECT_ID,
+            NULL, 'N', Cur_SO.C_INCOTERMS_ID, Cur_SO.INCOTERMS_DESCRIPTION,
+            TRUNC(now(), 'DD'), Cur_SO.DESCRIPTION
+          )
+          ;
+
+        -- Select the price list version that a applies for the price list of the header
+        SELECT M_Get_Pricelist_Version(Cur_SO.M_PriceList_ID, TRUNC(now(), 'DD'))
+        INTO v_M_PriceList_Version_ID
+        FROM DUAL;
+        IF (v_M_PriceList_Version_ID IS NULL) THEN
+          RAISE_APPLICATION_ERROR(-20000, '@PriceListVersionNotFound@'||'.');
+        ELSE
+         -- Select products, quantities, sequence numbers, descriptions and unit prices of the project
+            -- In both cases, if no unit price has been defined for a product,
+            -- price is taken from the price list of the project.
+            -- And if no price is defined in the price list, price is set to 0.
+          FOR Cur_SOLINES IN
+            (
+            SELECT pl.LINE AS SEQNO, pl.PRODUCT_DESCRIPTION AS DESCRIPTION, pl.M_PRODUCT_ID,
+              pl.PLANNEDQTY AS QTY, pl.PLANNEDPOPRICE AS PRICEACTUAL, pl.C_TAX_ID
+            FROM C_PROJECTLINE pl, C_PROJECT_VENDOR  pv
+            WHERE pv.C_PROJECT_VENDOR_ID = v_Record_ID
+              AND pl.C_PROJECT_ID = pv.C_PROJECT_ID
+              AND pl.M_Product_ID IS NOT NULL
+              AND pl.IsActive = 'Y'
+            ORDER BY SEQNO ASC
+            )
+          LOOP
+
+            -- Take StdPrice, PriceList and PriceLimit from Price List Version
+            SELECT COUNT(*)
+            INTO v_Size
+            FROM M_PRODUCT p,M_PRODUCTPRICE pp,M_PRICELIST_VERSION pv,M_PRICELIST pl
+            WHERE p.M_Product_ID=pp.M_Product_ID
+              AND pp.M_PriceList_Version_ID=pv.M_PriceList_Version_ID
+              AND pv.M_PriceList_ID=pl.M_PriceList_ID
+              AND pv.IsActive='Y'
+              AND p.M_Product_ID=Cur_SOLINES.M_PRODUCT_ID
+              AND pv.M_PriceList_Version_ID=v_M_PriceList_Version_ID
+              AND pl.C_Currency_ID=Cur_SO.C_CURRENCY_ID;
+            IF (v_Size>0) THEN
+              SELECT M_BOM_PriceStd(p.M_Product_ID, pv.M_PriceList_Version_ID) AS PriceStd,
+                M_BOM_PriceList(p.M_Product_ID, pv.M_PriceList_Version_ID) AS PriceList,
+                M_BOM_PriceLimit(p.M_Product_ID, pv.M_PriceList_Version_ID) AS PriceLimit,
+                p.C_UOM_ID
+              INTO v_PriceStd,v_PriceList,
+                v_PriceLimit,v_C_UOM_ID
+              FROM M_PRODUCT p,M_PRODUCTPRICE pp,M_PRICELIST_VERSION pv,M_PRICELIST pl
+              WHERE p.M_Product_ID=pp.M_Product_ID
+                AND pp.M_PriceList_Version_ID=pv.M_PriceList_Version_ID
+                AND pv.M_PriceList_ID=pl.M_PriceList_ID
+                AND pv.IsActive='Y'
+                AND p.M_Product_ID=Cur_SOLINES.M_PRODUCT_ID
+                AND pv.M_PriceList_Version_ID=v_M_PriceList_Version_ID
+                AND pl.C_Currency_ID=Cur_SO.C_CURRENCY_ID;
+            ELSE
+              v_PriceStd := NULL;
+              v_PriceList := NULL;
+              v_PriceLimit := NULL;
+              v_C_UOM_ID := NULL;
+            END IF;
+
+            -- Calculate Price Actual
+            IF (Cur_SOLINES.priceactual=0) THEN
+              Cur_SOLINES.priceactual:=NULL;
+            END IF;
+            v_PriceActual := COALESCE(Cur_SOLINES.priceactual,M_Get_Offers_Price(now(), Cur_SO.C_BPARTNER_ID, Cur_SOLINES.M_PRODUCT_ID, v_PriceStd, Cur_SOLINES.Qty, Cur_SO.M_PRICELIST_ID),0);
+
+            IF (v_PriceStd IS NULL) THEN
+             v_PriceStd :=  v_PriceActual ;
+            END IF;
+
+            IF (v_PriceList IS NULL) THEN
+              v_PriceList :=  v_PriceActual ;
+            END IF;
+
+            IF (v_PriceLimit IS NULL) THEN
+              v_PriceLimit :=  v_PriceActual ;
+            END IF;
+
+	        -- Make currency conversion if project header's currency (hence, project lines currency) is different from supplier's price list currency
+            IF (Cur_SO.C_CURRENCY_ID != Cur_SO.PROJCURRENCY) THEN
+              v_PriceActual := COALESCE(C_Currency_Convert(v_PriceActual, Cur_SO.PROJCURRENCY, Cur_SO.C_CURRENCY_ID, now(), NULL, Cur_SO.AD_CLIENT_ID, Cur_SO.AD_ORG_ID),0);
+	          v_PriceStd := COALESCE(C_Currency_Convert(v_PriceStd, Cur_SO.PROJCURRENCY, Cur_SO.C_CURRENCY_ID, now(), NULL, Cur_SO.AD_CLIENT_ID, Cur_SO.AD_ORG_ID),0);
+              v_PriceList := COALESCE(C_Currency_Convert(v_PriceList, Cur_SO.PROJCURRENCY, Cur_SO.C_CURRENCY_ID, now(), NULL, Cur_SO.AD_CLIENT_ID, Cur_SO.AD_ORG_ID),0);
+              v_PriceLimit := COALESCE(C_Currency_Convert(v_PriceLimit, Cur_SO.PROJCURRENCY, Cur_SO.C_CURRENCY_ID, now(), NULL, Cur_SO.AD_CLIENT_ID, Cur_SO.AD_ORG_ID),0);
+	        END IF;
+
+	        -- Calculating the discount
+	        IF (v_PriceList = 0) THEN
+               v_Discount := 0 ;
+            ELSE
+              -- Calculate rounded discount
+              v_Discount :=ROUND((v_PriceList-v_PriceActual) / v_PriceList*100, 2);
+            END IF;
+
+            IF (v_C_UOM_ID IS NULL) THEN
+              SELECT P.C_UOM_ID
+              INTO v_UOM
+              FROM M_PRODUCT P
+              WHERE P.M_PRODUCT_ID=Cur_SOLINES.M_PRODUCT_ID;
+            ELSE
+              v_UOM := v_C_UOM_ID;
+            END IF;
+
+            IF (Cur_SOLINES.C_TAX_ID IS NULL) THEN
+              v_Tax_ID:= C_Gettax(Cur_SOLINES.M_PRODUCT_ID, TRUNC(now(), 'DD'), Cur_SO.AD_ORG_ID, Cur_SO.M_WAREHOUSE_ID, v_BPartner_Location_ID, v_BPartner_Location_ID, Cur_SO.C_PROJECT_ID, 'Y') ;
+            ELSE
+              v_Tax_ID:=Cur_SOLINES.C_TAX_ID;
+            END IF;
+
+            -- Get next C_OrderLine_ID
+            Ad_Sequence_Next('C_OrderLine', Cur_SO.AD_CLIENT_ID, v_C_OrderLine_ID) ;
+            v_ResultStr:='C_OrderLine_ID - ' || v_C_OrderLine_ID;
+
+            INSERT
+            INTO C_ORDERLINE
+              (
+                DateOrdered, M_Warehouse_ID, QtyOrdered, QtyDelivered,
+                QtyReserved, M_Shipper_ID, QtyInvoiced,
+                C_Currency_ID, PriceList, DatePromised, DateDelivered,
+                DateInvoiced, Created, IsActive, Line,
+                C_OrderLine_ID, AD_Client_ID, C_Order_ID, Description,
+                M_Product_ID, C_UOM_ID, DirectShip, CreatedBy,
+                UpdatedBy, FreightAmt, C_Charge_ID, ChargeAmt,
+                Updated, AD_Org_ID, S_ResourceAssignment_ID, C_BPartner_ID,
+                PriceActual,
+                C_Tax_ID, C_BPartner_Location_ID,
+                Discount, PriceLimit, Ref_OrderLine_ID, LineNetAmt,
+                M_AttributeSetInstance_ID, IsDescription, PriceStd
+              )
+              VALUES
+              (
+                TRUNC(now(), 'DD'), Cur_SO.M_WAREHOUSE_ID, Cur_SOLINES.Qty, 0,
+                0, NULL, 0,
+                Cur_SO.C_CURRENCY_ID, v_PriceList, TRUNC(now(), 'DD'), NULL,
+                NULL, now(), 'Y', Cur_SOLINES.SEQNO,
+                v_C_OrderLine_ID, Cur_SO.AD_CLIENT_ID, v_C_Order_ID, Cur_SOLINES.DESCRIPTION,
+                Cur_SOLINES.M_PRODUCT_ID, v_UOM, 'N', v_AD_User_ID,
+                v_AD_User_ID, 0, NULL, 0,
+                now(), Cur_SO.AD_ORG_ID, NULL, Cur_SO.C_BPARTNER_ID,
+                v_PriceActual,
+                v_Tax_ID, v_BPartner_Location_ID,
+                v_Discount, v_PriceLimit, NULL, v_PriceActual * Cur_SOLINES.Qty,
+                NULL, 'N', v_PriceStd
+              )
+              ;
+          END LOOP;
+        END IF;
+
+        IF v_Message IS NOT NULL THEN
+          v_Message:=v_Message || ', ';
+        END IF;
+        v_Message:=v_Message || v_DocumentNo;
+      END LOOP;
+      v_Message:='@DocumentNo@: ' || v_Message;
+    END;
+    --<<FINISH_PROCESS>>
+    --  Update AD_PInstance
+    DBMS_OUTPUT.PUT_LINE('Updating PInstance - Finished ' || v_Message) ;
+    AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', v_Result, v_Message) ;
+    RETURN;
+  END; --BODY
+EXCEPTION
+WHEN OTHERS THEN
+  v_ResultStr:= '@ERROR=' || SQLERRM;
+  DBMS_OUTPUT.PUT_LINE(v_ResultStr) ;
+  ROLLBACK;
+  AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', 0, v_ResultStr) ;
+  RETURN;
+END C_GENERATEPOFROMPROJECT
+]]></body>
+    </function>
+  </database>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src-db/database/model/functions/C_GENERATESOFROMPROJECT.xml	Thu Oct 09 17:15:25 2008 +0000
@@ -0,0 +1,319 @@
+<?xml version="1.0"?>
+  <database name="FUNCTION C_GENERATESOFROMPROJECT">
+    <function name="C_GENERATESOFROMPROJECT" type="NULL">
+      <parameter name="p_pinstance_id" type="VARCHAR" 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-2008 Openbravo SL
+* All Rights Reserved.
+* Contributor(s):  ______________________________________.
+************************************************************************/
+  v_ResultStr VARCHAR2(2000):='';
+  v_Message VARCHAR2(2000):='';
+  v_Result NUMBER:=1; -- 0=failure
+  v_Record_ID VARCHAR2(32);
+  v_AD_User_ID VARCHAR2(32);
+  TYPE RECORD IS REF CURSOR;
+    Cur_Parameter RECORD;
+    v_C_Order_ID VARCHAR2(32);
+    v_C_OrderLine_ID VARCHAR2(32);
+    v_C_DOCTYPE_ID VARCHAR2(32);
+    v_DocumentNo NVARCHAR2(30) ;
+  BEGIN
+    --  Update AD_PInstance
+    DBMS_OUTPUT.PUT_LINE('Updating PInstance - Processing ' || p_PInstance_ID) ;
+    v_ResultStr:='PInstanceNotFound';
+    AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'Y', NULL, NULL) ;
+  BEGIN --BODY
+    -- Get PARAMETERS
+    v_ResultStr:='ReadingParameters';
+    FOR Cur_Parameter IN
+      (SELECT i.Record_ID,
+        i.AD_User_ID,
+        i.AD_Client_ID,
+        i.AD_Org_ID,
+        p.ParameterName,
+        p.P_String,
+        p.P_Number,
+        p.P_Date,
+        p.P_Date_To
+      FROM AD_PInstance i
+      LEFT JOIN AD_PInstance_Para p
+        ON i.AD_PInstance_ID=p.AD_PInstance_ID
+      WHERE i.AD_PInstance_ID=p_PInstance_ID
+      ORDER BY p.SeqNo
+      )
+    LOOP
+      v_Record_ID:=Cur_ParameteR.Record_ID;
+      v_AD_User_ID:=Cur_ParameteR.AD_User_ID;
+    END LOOP; -- Get Parameter
+    DBMS_OUTPUT.PUT_LINE('  Record_ID=' || v_Record_ID) ;
+    DECLARE
+      v_Size NUMBER;
+      v_UOM NUMBER;
+      v_PriceStd NUMBER;
+      v_PriceList NUMBER;
+      v_PriceLimit NUMBER;
+      v_C_UOM_ID VARCHAR2(32);
+      v_C_Currency_ID VARCHAR2(32);
+      v_M_PriceList_ID VARCHAR2(32);
+      v_Billto_ID VARCHAR2(32);
+      v_InvoiceRule CHAR;
+      v_M_PriceList_Version_ID VARCHAR2(32);
+      v_PriceActual NUMBER;
+      v_Discount NUMBER;
+      v_Tax_ID VARCHAR2(32);
+
+    TYPE RECORD IS REF CURSOR;
+      Cur_SO RECORD;
+      Cur_SOLINES RECORD;
+    BEGIN
+      v_ResultStr:='StartLoop';
+      FOR Cur_SO IN
+        (SELECT * FROM C_PROJECT WHERE C_PROJECT_ID=v_Record_ID)
+      LOOP
+        -- Check that we have some restrictions
+        v_ResultStr:='CheckRestriction';
+        IF(Cur_SO.PROJECTSTATUS IS NULL OR Cur_SO.PROJECTSTATUS<>'OR') THEN
+          RAISE_APPLICATION_ERROR(-20000, '@Invalidprojectstatus@');
+        ELSIF Cur_SO.C_BPARTNER_ID IS NULL THEN
+          RAISE_APPLICATION_ERROR(-20000, '@NoprojectBusinesspartner@');
+        ELSIF Cur_SO.C_BPARTNER_LOCATION_ID IS NULL THEN
+          RAISE_APPLICATION_ERROR(-20000, '@ThebusinessPartner@'||' '||' @ShiptoNotdefined@'||'.');
+        ELSIF Cur_SO.BILLTO_ID IS NULL THEN
+          RAISE_APPLICATION_ERROR(-20000, '@ThebusinessPartner@'||' '||' @BillToNotdefined@'||'.');
+        ELSIF Cur_SO.C_PAYMENTTERM_ID IS NULL THEN
+          RAISE_APPLICATION_ERROR(-20000, '@ThebusinessPartner@'||' '||' @PaymenttermNotdefined@'||'.');
+        ELSIF Cur_SO.M_PRICELIST_ID IS NULL THEN
+          RAISE_APPLICATION_ERROR(-20000, '@ThebusinessPartner@'||' '||' @PricelistNotdefined@'||'.');
+        ELSIF Cur_SO.C_CURRENCY_ID IS NULL THEN
+          RAISE_APPLICATION_ERROR(-20000, '@ProjectCurrencyNotFound@'||'.');
+        ELSIF Cur_SO.M_WAREHOUSE_ID IS NULL THEN
+          RAISE_APPLICATION_ERROR(-20000, '@ProjectWarehouseNotFound@'||'.');
+        ELSIF Cur_SO.SALESREP_ID IS NULL THEN
+          RAISE_APPLICATION_ERROR(-20000, '@ProjectSalesRepNotFound@'||'.');
+        END IF;
+
+        -- Get Business Partner Location
+        v_Billto_ID := Cur_SO.BILLTO_ID;
+        IF (v_Billto_ID IS NULL) THEN
+          v_Billto_ID := C_GetBPLocationID(Cur_SO.C_BPartner_ID, 'B') ;
+        END IF;
+
+        SELECT InvoiceRule INTO v_InvoiceRule
+        FROM C_BPARTNER
+        WHERE C_BPartner_ID = Cur_SO.C_BPARTNER_ID;
+        IF (v_InvoiceRule IS NULL) THEN
+           RAISE_APPLICATION_ERROR(-20000, '@ThebusinessPartner@'||' '||' @InvoiceRuleNotdefined@'||'.');
+        END IF;
+
+        v_C_DOCTYPE_ID:=Ad_Get_DocType(Cur_SO.AD_Client_ID, Cur_SO.AD_Org_ID, 'SOO', 'SO') ;
+        AD_Sequence_DocType(v_C_DOCTYPE_ID, Cur_SO.AD_Client_ID, 'Y', v_DocumentNo) ;
+        IF(v_DocumentNo IS NULL) THEN
+          AD_Sequence_Doc('DocumentNo_C_Order', Cur_SO.AD_Client_ID, 'Y', v_DocumentNo) ;
+        END IF;
+
+        -- Get next C_Order_ID
+        Ad_Sequence_Next('C_Order', Cur_SO.AD_CLIENT_ID, v_C_Order_ID) ;
+        v_ResultStr:='C_ORDER_ID - ' || v_C_Order_ID;
+
+        INSERT
+        INTO C_ORDER
+          (
+            C_ORDER_ID, AD_CLIENT_ID, AD_ORG_ID, CREATEDBY,
+            UPDATEDBY, ISSOTRX, DOCUMENTNO, DOCSTATUS,
+            DOCACTION, C_DOCTYPE_ID, C_DOCTYPETARGET_ID, DATEORDERED,
+            DATEACCT, C_BPARTNER_ID, C_BPARTNER_LOCATION_ID, ISDISCOUNTPRINTED,
+            C_CURRENCY_ID, PAYMENTRULE, C_PAYMENTTERM_ID, INVOICERULE,
+            DELIVERYRULE, FREIGHTCOSTRULE, DELIVERYVIARULE, PRIORITYRULE,
+            TOTALLINES, GRANDTOTAL, M_WAREHOUSE_ID, M_PRICELIST_ID,
+            ISTAXINCLUDED, POSTED, PROCESSING, SALESREP_ID,
+            BILLTO_ID, C_CAMPAIGN_ID, C_PROJECT_ID, AD_USER_ID,
+            COPYFROM, DATEPROMISED
+          )
+          VALUES
+          (
+            v_C_Order_ID, Cur_SO.AD_CLIENT_ID, Cur_SO.AD_ORG_ID, v_AD_User_ID,
+            v_AD_User_ID, 'Y', v_DocumentNo, 'DR',
+             'CO', 0, v_C_DOCTYPE_ID, TRUNC(now(), 'DD'),
+            TRUNC(now(), 'DD'), Cur_SO.C_BPARTNER_ID, Cur_SO.C_BPARTNER_LOCATION_ID, 'N',
+            Cur_SO.C_CURRENCY_ID, COALESCE(Cur_SO.PAYMENTRULE, 'P'), Cur_SO.C_PAYMENTTERM_ID,  v_InvoiceRule,
+             'A', 'I', 'D', '5',
+            0, 0, Cur_SO.M_WAREHOUSE_ID, Cur_SO.M_PRICELIST_ID,
+             'N', 'N', 'N', Cur_SO.SALESREP_ID,
+            v_Billto_ID, Cur_SO.C_CAMPAIGN_ID, Cur_SO.C_PROJECT_ID, Cur_SO.AD_User_ID,
+             'N', TRUNC(now(), 'DD')
+          )
+          ;
+
+        -- Select the price list version that a applies for the price list of the header
+        SELECT M_Get_Pricelist_Version(Cur_SO.M_PriceList_ID, TRUNC(now(), 'DD'))
+        INTO v_M_PriceList_Version_ID
+        FROM DUAL;
+        IF (v_M_PriceList_Version_ID IS NULL) THEN
+          RAISE_APPLICATION_ERROR(-20000, '@PriceListVersionNotFound@'||'.');
+        ELSE
+         -- Select products, quantities, sequence numbers, descriptions and unit prices of the project
+            -- In both cases, if no unit price has been defined for a product,
+            -- price is taken from the price list of the project.
+            -- And if no price is defined in the price list, price is set to 0.
+          FOR Cur_SOLINES IN
+            (
+            SELECT pl.LINE AS SEQNO, pl.PRODUCT_DESCRIPTION AS DESCRIPTION, pl.M_PRODUCT_ID,
+              pl.PLANNEDQTY AS QTY, pl.PLANNEDPRICE AS PRICEACTUAL, pl.C_TAX_ID
+            FROM C_PROJECTLINE pl
+            WHERE pl.C_PROJECT_ID = v_Record_ID
+              AND pl.M_Product_ID IS NOT NULL
+              AND pl.IsActive = 'Y'
+            ORDER BY SEQNO ASC
+            )
+          LOOP
+
+            -- Take StdPrice, PriceList and PriceLimit from Price List Version
+            SELECT COUNT(*)
+            INTO v_Size
+            FROM M_PRODUCT p,M_PRODUCTPRICE pp,M_PRICELIST_VERSION pv,M_PRICELIST pl
+            WHERE p.M_Product_ID=pp.M_Product_ID
+              AND pp.M_PriceList_Version_ID=pv.M_PriceList_Version_ID
+              AND pv.M_PriceList_ID=pl.M_PriceList_ID
+              AND pv.IsActive='Y'
+              AND p.M_Product_ID=Cur_SOLINES.M_PRODUCT_ID
+              AND pv.M_PriceList_Version_ID=v_M_PriceList_Version_ID
+              AND pl.C_Currency_ID=Cur_SO.C_CURRENCY_ID;
+            IF (v_Size>0) THEN
+              SELECT M_BOM_PriceStd(p.M_Product_ID, pv.M_PriceList_Version_ID) AS PriceStd,
+                M_BOM_PriceList(p.M_Product_ID, pv.M_PriceList_Version_ID) AS PriceList,
+                M_BOM_PriceLimit(p.M_Product_ID, pv.M_PriceList_Version_ID) AS PriceLimit,
+                p.C_UOM_ID
+              INTO v_PriceStd,v_PriceList,
+                v_PriceLimit,v_C_UOM_ID
+              FROM M_PRODUCT p,M_PRODUCTPRICE pp,M_PRICELIST_VERSION pv,M_PRICELIST pl
+              WHERE p.M_Product_ID=pp.M_Product_ID
+                AND pp.M_PriceList_Version_ID=pv.M_PriceList_Version_ID
+                AND pv.M_PriceList_ID=pl.M_PriceList_ID
+                AND pv.IsActive='Y'
+                AND p.M_Product_ID=Cur_SOLINES.M_PRODUCT_ID
+                AND pv.M_PriceList_Version_ID=v_M_PriceList_Version_ID
+                AND pl.C_Currency_ID=Cur_SO.C_CURRENCY_ID;
+            ELSE
+              v_PriceStd := NULL;
+              v_PriceList := NULL;
+              v_PriceLimit := NULL;
+              v_C_UOM_ID := NULL;
+            END IF;
+
+            -- Calculate Price Actual
+            IF (Cur_SOLINES.priceactual=0) THEN
+              Cur_SOLINES.priceactual:=NULL;
+            END IF;
+            v_PriceActual := COALESCE(Cur_SOLINES.priceactual,M_Get_Offers_Price(now(), Cur_SO.C_BPARTNER_ID, Cur_SOLINES.M_PRODUCT_ID, v_PriceStd, Cur_SOLINES.Qty, Cur_SO.M_PRICELIST_ID),0);
+
+            IF (v_PriceStd IS NULL) THEN
+             v_PriceStd :=  v_PriceActual ;
+            END IF;
+
+            IF (v_PriceList IS NULL) THEN
+              v_PriceList :=  v_PriceActual ;
+            END IF;
+
+            IF (v_PriceLimit IS NULL) THEN
+              v_PriceLimit :=  v_PriceActual ;
+            END IF;
+
+            IF (v_PriceList = 0) THEN
+               v_Discount := 0 ;
+            ELSE
+              -- Calculate rounded discount
+              v_Discount :=ROUND((v_PriceList-v_PriceActual) / v_PriceList*100, 2);
+            END IF;
+
+            IF (v_C_UOM_ID IS NULL) THEN
+              SELECT P.C_UOM_ID
+              INTO v_UOM
+              FROM M_PRODUCT P
+              WHERE P.M_PRODUCT_ID=Cur_SOLINES.M_PRODUCT_ID;
+            ELSE
+              v_UOM := v_C_UOM_ID;
+            END IF;
+
+            IF (Cur_SOLINES.C_TAX_ID IS NULL) THEN
+              v_Tax_ID:= C_Gettax(Cur_SOLINES.M_PRODUCT_ID, TRUNC(now(), 'DD'), Cur_SO.AD_ORG_ID, Cur_SO.M_WAREHOUSE_ID, Cur_SO.C_BPARTNER_LOCATION_ID, Cur_SO.C_BPARTNER_LOCATION_ID, Cur_SO.C_PROJECT_ID, 'Y') ;
+            ELSE
+              v_Tax_ID:=Cur_SOLINES.C_TAX_ID;
+            END IF;
+
+            -- Get next C_OrderLine_ID
+            Ad_Sequence_Next('C_OrderLine', Cur_SO.AD_CLIENT_ID, v_C_OrderLine_ID) ;
+            v_ResultStr:='C_OrderLine_ID - ' || v_C_OrderLine_ID;
+
+            INSERT
+            INTO C_ORDERLINE
+              (
+                DateOrdered, M_Warehouse_ID, QtyOrdered, QtyDelivered,
+                QtyReserved, M_Shipper_ID, QtyInvoiced,
+                C_Currency_ID, PriceList, DatePromised, DateDelivered,
+                DateInvoiced, Created, IsActive, Line,
+                C_OrderLine_ID, AD_Client_ID, C_Order_ID, Description,
+                M_Product_ID, C_UOM_ID, DirectShip, CreatedBy,
+                UpdatedBy, FreightAmt, C_Charge_ID, ChargeAmt,
+                Updated, AD_Org_ID, S_ResourceAssignment_ID, C_BPartner_ID,
+                PriceActual,
+                C_Tax_ID, C_BPartner_Location_ID,
+                Discount, PriceLimit, Ref_OrderLine_ID, LineNetAmt,
+                M_AttributeSetInstance_ID, IsDescription, PriceStd
+              )
+              VALUES
+              (
+                TRUNC(now(), 'DD'), Cur_SO.M_WAREHOUSE_ID, Cur_SOLINES.Qty, 0,
+                0, NULL, 0,
+                Cur_SO.C_CURRENCY_ID, v_PriceList, TRUNC(now(), 'DD'), NULL,
+                NULL, now(), 'Y', Cur_SOLINES.SEQNO,
+                v_C_OrderLine_ID, Cur_SO.AD_CLIENT_ID, v_C_Order_ID, Cur_SOLINES.DESCRIPTION,
+                Cur_SOLINES.M_PRODUCT_ID, v_UOM, 'N', v_AD_User_ID,
+                v_AD_User_ID, 0, NULL, 0,
+                now(), Cur_SO.AD_ORG_ID, NULL, Cur_SO.C_BPARTNER_ID,
+                v_PriceActual,
+                v_Tax_ID, Cur_SO.C_BPARTNER_LOCATION_ID,
+                v_Discount, v_PriceLimit, NULL, v_PriceActual * Cur_SOLINES.Qty,
+                NULL, 'N', v_PriceStd
+              )
+              ;
+          END LOOP;
+        END IF;
+
+        IF v_Message IS NOT NULL THEN
+          v_Message:=v_Message || ', ';
+        END IF;
+        v_Message:=v_Message || v_DocumentNo;
+      END LOOP;
+      v_Message:='@DocumentNo@: ' || v_Message;
+    END;
+    --<<FINISH_PROCESS>>
+    --  Update AD_PInstance
+    DBMS_OUTPUT.PUT_LINE('Updating PInstance - Finished ' || v_Message) ;
+    AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', v_Result, v_Message) ;
+    RETURN;
+  END; --BODY
+EXCEPTION
+WHEN OTHERS THEN
+  v_ResultStr:= '@ERROR=' || SQLERRM;
+  DBMS_OUTPUT.PUT_LINE(v_ResultStr) ;
+  ROLLBACK;
+  AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', 0, v_ResultStr) ;
+  RETURN;
+END C_GENERATESOFROMPROJECT
+]]></body>
+    </function>
+  </database>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src-db/database/model/functions/C_GENERATESOFROMPROJECTORDER.xml	Thu Oct 09 17:15:25 2008 +0000
@@ -0,0 +1,259 @@
+<?xml version="1.0"?>
+  <database name="FUNCTION C_GENERATESOFROMPROJECTORDER">
+    <function name="C_GENERATESOFROMPROJECTORDER" type="NULL">
+      <parameter name="p_pinstance_id" type="VARCHAR" 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):  ______________________________________.
+************************************************************************/
+  v_ResultStr VARCHAR2(2000):='';
+  v_Message VARCHAR2(2000):='';
+  v_Result NUMBER:=1; -- 0=failure
+  v_Record_ID VARCHAR2(32);
+  v_AD_User_ID VARCHAR2(32);
+  v_M_PriceList_ID2 VARCHAR2(32);
+  TYPE RECORD IS REF CURSOR;
+    Cur_Parameter RECORD;
+    v_C_Order_ID VARCHAR2(32);
+    v_C_OrderLine_ID VARCHAR2(32);
+    v_C_DOCTYPE_ID VARCHAR2(32);
+    v_DocumentNo NUMBER;
+  BEGIN
+    --  Update AD_PInstance
+    DBMS_OUTPUT.PUT_LINE('Updating PInstance - Processing ' || p_PInstance_ID) ;
+    v_ResultStr:='PInstanceNotFound';
+    AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'Y', NULL, NULL) ;
+  BEGIN --BODY
+    -- Get PARAMETERS
+    v_ResultStr:='ReadingParameters';
+    FOR Cur_Parameter IN
+      (SELECT i.Record_ID, i.AD_User_ID, i.AD_Client_ID, i.AD_Org_ID, p.ParameterName,
+        p.P_String, p.P_Number, p.P_Date, p.P_Date_To
+      FROM AD_PINSTANCE i
+      LEFT JOIN AD_PINSTANCE_PARA p
+        ON i.AD_PInstance_ID=p.AD_PInstance_ID
+      WHERE i.AD_PInstance_ID=p_PInstance_ID
+      )
+    LOOP
+      v_Record_ID:=Cur_Parameter.Record_ID;
+      v_AD_User_ID:=Cur_Parameter.AD_User_ID;
+    END LOOP; -- Get Parameter
+    DBMS_OUTPUT.PUT_LINE('  Record_ID=' || v_Record_ID) ;
+    DECLARE
+      v_Size NUMBER;
+      v_UOM NUMBER;
+      v_PriceStd NUMBER;
+      v_PriceList NUMBER;
+      v_PriceLimit NUMBER;
+      v_C_UOM_ID VARCHAR2(32);
+      v_C_Currency_ID VARCHAR2(32);
+      v_M_PriceList_ID VARCHAR2(32);
+    TYPE RECORD IS REF CURSOR;
+      Cur_SO RECORD;
+      SOLINES RECORD;
+      FINISH_PROCESS BOOLEAN:=FALSE;
+    BEGIN
+      v_ResultStr:='StartLoop';
+      FOR Cur_SO IN
+        (SELECT * FROM C_PROJECT WHERE C_PROJECT_ID=v_Record_ID)
+      LOOP
+        -- Check that we have some restrictions
+        v_ResultStr:='CheckRestriction';
+        IF(Cur_SO.PROJECTSTATUS IS NULL OR Cur_SO.PROJECTSTATUS<>'OR') THEN
+          RAISE_APPLICATION_ERROR(-20000, '@Invalidprojectstatus@');
+        END IF;
+        IF(NOT FINISH_PROCESS) THEN
+          v_C_Order_ID:=Ad_Sequence_Nextno('C_Order') ;
+          v_ResultStr:='C_ORDER_ID - ' || v_C_Order_ID;
+          v_C_DOCTYPE_ID:=Ad_Get_Doctype(Cur_SO.AD_Client_ID, Cur_SO.AD_Org_ID, 'SOO', 'SO') ;
+          v_ResultStr:='C_DOCTYPE_ID - ' || v_C_DOCTYPE_ID;
+          Ad_Sequence_Doctype(v_C_DOCTYPE_ID, Cur_SO.AD_Client_ID, 'Y', v_DocumentNo) ;
+          IF(v_DocumentNo IS NULL) THEN
+            Ad_Sequence_Doc('DocumentNo_C_Order', Cur_SO.AD_Client_ID, 'Y', v_DocumentNo) ;
+          END IF;
+          SELECT M_PriceList_ID
+        INTO v_M_PriceList_ID2
+        FROM M_PRICELIST_VERSION
+        WHERE M_PriceList_Version_ID=Cur_SO.M_PRICELIST_VERSION_ID;
+        v_ResultStr:='DocumentNo - ' || v_DocumentNo || ' m_pricelist_id - ' || COALESCE(Cur_SO.m_pricelist_id, v_M_Pricelist_ID2);
+          INSERT
+          INTO C_ORDER
+            (
+              C_ORDER_ID, AD_CLIENT_ID, AD_ORG_ID, CREATEDBY,
+              UPDATEDBY, ISSOTRX, DOCUMENTNO, DOCSTATUS,
+              DOCACTION, C_DOCTYPE_ID, C_DOCTYPETARGET_ID, DATEORDERED,
+              DATEACCT, C_BPARTNER_ID, C_BPARTNER_LOCATION_ID, ISDISCOUNTPRINTED,
+              C_CURRENCY_ID, PAYMENTRULE, C_PAYMENTTERM_ID, INVOICERULE,
+              DELIVERYRULE, FREIGHTCOSTRULE, DELIVERYVIARULE, PRIORITYRULE,
+              TOTALLINES, GRANDTOTAL, M_WAREHOUSE_ID, M_PRICELIST_ID,
+              ISTAXINCLUDED, POSTED, PROCESSING, SALESREP_ID,
+              BILLTO_ID, C_CAMPAIGN_ID, C_PROJECT_ID, AD_USER_ID,
+              COPYFROM, DATEPROMISED
+            )
+            VALUES
+            (
+              v_C_Order_ID, Cur_SO.AD_CLIENT_ID, Cur_SO.AD_ORG_ID, v_AD_User_ID,
+              v_AD_User_ID, 'Y', v_DocumentNo, 'DR',
+               'CO', '0', v_C_DOCTYPE_ID, TRUNC(now(), 'DD'),
+              TRUNC(now(), 'DD'), Cur_SO.C_BPARTNER_ID, Cur_SO.C_BPARTNER_LOCATION_ID, 'N',
+              Cur_SO.C_CURRENCY_ID, COALESCE(Cur_SO.PAYMENTRULE, 'P'), Cur_SO.C_PAYMENTTERM_ID, 'D',
+               'A', 'I', 'D', '5',
+              0, 0, Cur_SO.M_WAREHOUSE_ID, COALESCE(Cur_SO.M_PRICELIST_ID, v_M_PriceList_ID2),
+               'N', 'N', 'N', Cur_SO.SALESREP_ID,
+              Cur_SO.BILLTO_ID, Cur_SO.C_CAMPAIGN_ID, Cur_SO.C_PROJECT_ID, Cur_SO.AD_User_ID,
+               'N', TRUNC(now(), 'DD')
+            )
+            ;
+          FOR SOLINES IN
+            (SELECT * FROM C_PROJECTLINE WHERE C_PROJECT_ID=v_Record_ID)
+          LOOP
+            IF(SOLINES.M_PRODUCT_ID IS NOT NULL AND Cur_SO.M_PRICELIST_VERSION_ID IS NOT NULL) THEN
+              v_ResultStr:='SOLINES1 - SO.M_PRICELIST_VERSION_ID - ' || Cur_SO.M_PRICELIST_VERSION_ID || ' - ' || SOLINES.M_PRODUCT_ID;
+              SELECT COUNT(p.C_UOM_ID)
+              INTO v_Size
+              FROM M_PRODUCT p,
+                M_PRODUCTPRICE pp,
+                M_PRICELIST_VERSION pv,
+                M_PRICELIST pl
+              WHERE p.M_Product_ID=pp.M_Product_ID
+                AND pp.M_PriceList_Version_ID=pv.M_PriceList_Version_ID
+                AND pv.M_PriceList_ID=pl.M_PriceList_ID
+                AND pv.IsActive='Y'
+                AND p.M_Product_ID=SOLINES.M_PRODUCT_ID
+                AND pv.M_PriceList_Version_ID=Cur_SO.M_PRICELIST_VERSION_ID;
+              IF(v_Size>0) THEN
+                SELECT M_BOM_PriceStd(p.M_Product_ID, pv.M_PriceList_Version_ID) AS PriceStd,
+                  M_BOM_PriceList(p.M_Product_ID, pv.M_PriceList_Version_ID) AS PriceList,
+                  M_BOM_PriceLimit(p.M_Product_ID, pv.M_PriceList_Version_ID) AS PriceLimit,
+                  p.C_UOM_ID, pl.C_Currency_ID
+                INTO v_PriceStd, v_PriceList, v_PriceLimit, v_C_UOM_ID, v_C_Currency_ID
+                FROM M_PRODUCT p, M_PRODUCTPRICE pp,M_PRICELIST_VERSION pv,M_PRICELIST pl
+                WHERE p.M_Product_ID=pp.M_Product_ID
+                  AND pp.M_PriceList_Version_ID=pv.M_PriceList_Version_ID
+                  AND pv.M_PriceList_ID=pl.M_PriceList_ID
+                  AND pv.IsActive='Y'
+                  AND p.M_Product_ID=SOLINES.M_PRODUCT_ID
+                  AND pv.M_PriceList_Version_ID=Cur_SO.M_PRICELIST_VERSION_ID;
+              END IF;
+            END IF;
+            IF(SOLINES.M_PRODUCT_ID IS NOT NULL AND v_PriceStd IS NULL AND v_PriceList IS NULL AND v_PriceLimit IS NULL AND v_C_UOM_ID IS NULL AND v_C_Currency_ID IS NULL) THEN
+              v_ResultStr:='SOLINES - v_PriceStd - ' || v_PriceStd;
+              IF(Cur_SO.M_PRICELIST_ID IS NULL) THEN
+                SELECT M_PriceList_ID
+                INTO v_M_PriceList_ID
+                FROM M_PRICELIST pl,
+                  M_PRODUCT p
+                WHERE pl.AD_Client_ID=p.AD_Client_ID
+                  AND M_Product_ID=SOLINES.M_PRODUCT_ID
+                ORDER BY IsDefault DESC;
+              END IF;
+              v_ResultStr:='SOLINES2 - v_M_PriceList_ID - ' || v_M_PriceList_ID;
+              SELECT M_BOM_PriceStd(p.M_Product_ID, pv.M_PriceList_Version_ID) AS PriceStd,
+                M_BOM_PriceList(p.M_Product_ID, pv.M_PriceList_Version_ID) AS PriceList,
+                M_BOM_PriceLimit(p.M_Product_ID, pv.M_PriceList_Version_ID) AS PriceLimit,
+                p.C_UOM_ID,pl.C_Currency_ID
+              INTO v_PriceStd,v_PriceList,v_PriceLimit,v_C_UOM_ID,v_C_Currency_ID
+              FROM M_PRODUCT p,M_PRODUCTPRICE pp,M_PRICELIST_VERSION pv,M_PRICELIST pl
+              WHERE p.M_Product_ID=pp.M_Product_ID
+                AND pp.M_PriceList_Version_ID=pv.M_PriceList_Version_ID
+                AND pv.M_PriceList_ID=pl.M_PriceList_ID
+                AND pv.IsActive='Y'
+                AND p.M_Product_ID=SOLINES.M_PRODUCT_ID
+                AND pv.M_PriceList_ID=COALESCE(Cur_SO.M_PRICELIST_ID, v_M_PriceList_ID)
+              ORDER BY pv.ValidFrom DESC;
+              v_ResultStr:='SOLINES - v_PriceStd - ' || v_PriceStd;
+            END IF;
+            IF(SOLINES.M_PRODUCT_ID IS NOT NULL AND v_PriceStd IS NULL AND v_PriceList IS NULL AND v_PriceLimit IS NULL AND v_C_UOM_ID IS NULL AND v_C_Currency_ID IS NULL) THEN
+              SELECT M_BOM_PriceStd(p.M_Product_ID, pv.M_PriceList_Version_ID) AS PriceStd,
+                M_BOM_PriceList(p.M_Product_ID, pv.M_PriceList_Version_ID) AS PriceList,
+                M_BOM_PriceLimit(p.M_Product_ID, pv.M_PriceList_Version_ID) AS PriceLimit,
+                p.C_UOM_ID,pl.C_Currency_ID
+              INTO v_PriceStd,v_PriceList,v_PriceLimit,v_C_UOM_ID,v_C_Currency_ID
+              FROM M_PRODUCT p,M_PRODUCTPRICE pp,M_PRICELIST_VERSION pv,M_PRICELIST bpl,M_PRICELIST pl
+              WHERE p.M_Product_ID=pp.M_Product_ID
+                AND pp.M_PriceList_Version_ID=pv.M_PriceList_Version_ID
+                AND pv.M_PriceList_ID=bpl.M_PriceList_ID
+                AND bpl.M_PriceList_ID=pl.BasePriceList_ID
+                AND pv.IsActive='Y'
+                AND p.M_Product_ID=SOLINES.M_PRODUCT_ID
+                AND pl.M_PriceList_ID=COALESCE(Cur_SO.M_PRICELIST_ID, v_M_PriceList_ID)
+              ORDER BY pv.ValidFrom DESC;
+            END IF;
+            SELECT U.C_UOM_ID
+            INTO v_UOM
+            FROM C_UOM U,M_PRODUCT P
+            WHERE P.C_UOM_ID=U.C_UOM_ID
+              AND M_PRODUCT_ID=SOLINES.M_PRODUCT_ID;
+            v_C_OrderLine_ID:=Ad_Sequence_Nextno('C_OrderLine') ;
+            v_ResultStr:='C_OrderLine_ID - ' || v_C_OrderLine_ID;
+            INSERT
+            INTO C_ORDERLINE
+              (
+                DateOrdered, M_Warehouse_ID, QtyOrdered, QtyDelivered,
+                QtyReserved, M_Shipper_ID, QtyInvoiced,
+                C_Currency_ID, PriceList, DatePromised, DateDelivered,
+                DateInvoiced, Created, IsActive, Line,
+                C_OrderLine_ID, AD_Client_ID, C_Order_ID, Description,
+                M_Product_ID, C_UOM_ID, DirectShip, CreatedBy,
+                UpdatedBy, FreightAmt, C_Charge_ID, ChargeAmt,
+                Updated, AD_Org_ID, S_ResourceAssignment_ID, C_BPartner_ID,
+                PriceActual, C_Tax_ID, C_BPartner_Location_ID,
+                Discount, PriceLimit, Ref_OrderLine_ID, LineNetAmt,
+                M_AttributeSetInstance_ID, IsDescription, PriceStd
+              )
+              VALUES
+              (
+                TRUNC(now(), 'DD'), Cur_SO.M_WAREHOUSE_ID, SOLINES.InvoicedQty, 0,
+                0, NULL, 0,
+                Cur_SO.C_CURRENCY_ID, v_PriceList, TRUNC(now(), 'DD'), NULL,
+                NULL, now(), 'Y', SOLINES.LINE,
+                v_C_OrderLine_ID, Cur_SO.AD_CLIENT_ID, v_C_Order_ID, SOLINES.DESCRIPTION,
+                SOLINES.M_PRODUCT_ID, v_UOM, 'N', v_AD_User_ID,
+                v_AD_User_ID, 0, NULL, 0,
+                now(), Cur_SO.AD_ORG_ID, NULL, Cur_SO.C_BPARTNER_ID,
+                M_Get_Offers_Price(now(), Cur_SO.C_BPARTNER_ID, SOLINES.M_PRODUCT_ID, v_PriceStd, SOLINES.InvoicedQty,  v_M_PriceList_ID),
+    C_Gettax(SOLINES.M_PRODUCT_ID, TRUNC(now(), 'DD'), Cur_SO.AD_ORG_ID, Cur_SO.M_WAREHOUSE_ID, Cur_SO.C_BPARTNER_LOCATION_ID, Cur_SO.C_BPARTNER_LOCATION_ID, Cur_SO.C_PROJECT_ID, 'Y'), Cur_SO.C_BPARTNER_LOCATION_ID,
+                (v_PriceList-v_PriceStd) *100/v_PriceList, v_PriceLimit, NULL, (M_Get_Offers_Price(now(), Cur_SO.C_BPARTNER_ID, SOLINES.M_PRODUCT_ID, v_PriceStd, SOLINES.InvoicedQty,  v_M_PriceList_ID) * SOLINES.InvoicedQty),
+                NULL, 'N', v_PriceStd
+              )
+              ;
+          END LOOP;
+          IF v_Message IS NOT NULL THEN
+            v_Message:=v_Message || ', ';
+          END IF;
+          v_Message:=v_Message || v_DocumentNo;
+        END IF;--FINISH_PROCESS
+      END LOOP;
+      v_Message:='@DocumentNo@: ' || v_Message;
+    END;
+    --<<FINISH_PROCESS>>
+    --  Update AD_PInstance
+    DBMS_OUTPUT.PUT_LINE('Updating PInstance - Finished ' || v_Message) ;
+    AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', v_Result, v_Message) ;
+    RETURN;
+  END; --BODY
+EXCEPTION
+WHEN OTHERS THEN
+  v_ResultStr:= '@ERROR=' || SQLERRM;
+  DBMS_OUTPUT.PUT_LINE(v_ResultStr) ;
+  ROLLBACK;
+  AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', 0, v_ResultStr) ;
+  RETURN;
+END C_GENERATESOFROMPROJECTORDER
+]]></body>
+    </function>
+  </database>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src-db/database/model/functions/C_GENERATESOFROMPROJECTPHASE.xml	Thu Oct 09 17:15:25 2008 +0000
@@ -0,0 +1,323 @@
+<?xml version="1.0"?>
+  <database name="FUNCTION C_GENERATESOFROMPROJECTPHASE">
+    <function name="C_GENERATESOFROMPROJECTPHASE" type="NULL">
+      <parameter name="p_pinstance_id" type="VARCHAR" 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-2008 Openbravo SL
+* All Rights Reserved.
+* Contributor(s):  ______________________________________.
+************************************************************************/
+  v_ResultStr VARCHAR2(2000):='';
+  v_Message VARCHAR2(2000):='';
+  v_Result NUMBER:=1; -- 0=failure
+  v_Record_ID VARCHAR2(32);
+  v_AD_User_ID VARCHAR2(32);
+  TYPE RECORD IS REF CURSOR;
+    Cur_Parameter RECORD;
+    v_C_Order_ID VARCHAR2(32);
+    v_C_OrderLine_ID VARCHAR2(32);
+    v_C_DOCTYPE_ID VARCHAR2(32);
+    v_DocumentNo NVARCHAR2(30) ;
+  BEGIN
+    --  Update AD_PInstance
+    DBMS_OUTPUT.PUT_LINE('Updating PInstance - Processing ' || p_PInstance_ID) ;
+    v_ResultStr:='PInstanceNotFound';
+    AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'Y', NULL, NULL) ;
+  BEGIN --BODY
+    -- Get PARAMETERS
+    v_ResultStr:='ReadingParameters';
+    FOR Cur_Parameter IN
+      (SELECT i.Record_ID, i.AD_User_ID, i.AD_Client_ID, i.AD_Org_ID, p.ParameterName,
+        p.P_String, p.P_Number, p.P_Date, p.P_Date_To
+      FROM AD_PINSTANCE i
+      LEFT JOIN AD_PINSTANCE_PARA p ON i.AD_PInstance_ID=p.AD_PInstance_ID
+      WHERE i.AD_PInstance_ID=p_PInstance_ID
+      ORDER BY p.SeqNo
+      )
+    LOOP
+      v_Record_ID:=Cur_Parameter.Record_ID;
+      v_AD_User_ID:=Cur_Parameter.AD_User_ID;
+    END LOOP; -- Get Parameter
+    DBMS_OUTPUT.PUT_LINE('  Record_ID=' || v_Record_ID) ;
+    DECLARE
+      v_Size NUMBER;
+      v_UOM NUMBER;
+      v_PriceStd NUMBER;
+      v_PriceList NUMBER;
+      v_PriceLimit NUMBER;
+      v_C_UOM_ID VARCHAR2(32);
+      v_C_Currency_ID VARCHAR2(32);
+      v_M_PriceList_ID VARCHAR2(32);
+      v_Billto_ID VARCHAR2(32);
+      v_InvoiceRule CHAR;
+      v_M_PriceList_Version_ID VARCHAR2(32);
+      v_PriceActual NUMBER;
+      v_Discount NUMBER;
+
+    TYPE RECORD IS REF CURSOR;
+      Cur_SO RECORD;
+      Cur_SOLINES RECORD;
+    BEGIN
+      v_ResultStr:='StartLoop';
+      FOR Cur_SO IN
+        (
+        SELECT P.C_BPARTNER_ID, P.C_BPARTNER_LOCATION_ID, P.M_PRICELIST_ID, PL.NAME AS PRICE_LIST_NAME,
+          P.C_CURRENCY_ID, P.PAYMENTRULE, P.C_PAYMENTTERM_ID, P.M_WAREHOUSE_ID, P.SALESREP_ID,
+          P.BILLTO_ID, P.C_CAMPAIGN_ID, P.C_PROJECT_ID, P.AD_USER_ID, P.DESCRIPTION, P.PROJECTSTATUS,
+          PP.NAME AS PHASE_NAME, PP.AD_CLIENT_ID, PP.AD_ORG_ID
+        FROM C_PROJECT P,
+          C_PROJECTPHASE PP,
+          M_PRICELIST PL
+        WHERE P.C_PROJECT_ID=PP.C_PROJECT_ID
+          AND PP.C_PROJECTPHASE_ID=v_Record_ID
+          AND P.M_PRICELIST_ID = PL.M_PRICELIST_ID
+        )
+      LOOP
+        -- Check that we have some restrictions
+        IF (Cur_SO.PROJECTSTATUS IS NULL OR Cur_SO.PROJECTSTATUS <> 'OR') THEN
+          RAISE_APPLICATION_ERROR(-20000, '@Invalidprojectstatus@'||'. '||' @ChangeToOrder@'||'.');
+        ELSIF Cur_SO.C_BPARTNER_ID IS NULL THEN
+          RAISE_APPLICATION_ERROR(-20000, '@NoprojectBusinesspartner@');
+        ELSIF Cur_SO.C_BPARTNER_LOCATION_ID IS NULL THEN
+          RAISE_APPLICATION_ERROR(-20000, '@ThebusinessPartner@'||' '||' @ShiptoNotdefined@'||'.');
+        ELSIF Cur_SO.BILLTO_ID IS NULL THEN
+          RAISE_APPLICATION_ERROR(-20000, '@ThebusinessPartner@'||' '||' @BillToNotdefined@'||'.');
+        ELSIF Cur_SO.C_PAYMENTTERM_ID IS NULL THEN
+          RAISE_APPLICATION_ERROR(-20000, '@ThebusinessPartner@'||' '||' @PaymenttermNotdefined@'||'.');
+        ELSIF Cur_SO.M_PRICELIST_ID IS NULL THEN
+          RAISE_APPLICATION_ERROR(-20000, '@ThebusinessPartner@'||' '||' @PricelistNotdefined@'||'.');
+        ELSIF Cur_SO.C_CURRENCY_ID IS NULL THEN
+          RAISE_APPLICATION_ERROR(-20000, '@ProjectCurrencyNotFound@'||'.');
+        ELSIF Cur_SO.M_WAREHOUSE_ID IS NULL THEN
+          RAISE_APPLICATION_ERROR(-20000, '@ProjectWarehouseNotFound@'||'.');
+        ELSIF Cur_SO.SALESREP_ID IS NULL THEN
+          RAISE_APPLICATION_ERROR(-20000, '@ProjectSalesRepNotFound@'||'.');
+        END IF;
+
+        -- Get Business Partner Location
+        v_Billto_ID := Cur_SO.BILLTO_ID;
+        IF (v_Billto_ID IS NULL) THEN
+          v_Billto_ID := C_GetBPLocationID(Cur_SO.C_BPartner_ID, 'B') ;
+        END IF;
+
+        SELECT InvoiceRule INTO v_InvoiceRule
+        FROM C_BPARTNER
+        WHERE C_BPartner_ID = Cur_SO.C_BPARTNER_ID;
+        IF (v_InvoiceRule IS NULL) THEN
+           RAISE_APPLICATION_ERROR(-20000, '@ThebusinessPartner@'||' '||' @InvoiceRuleNotdefined@'||'.');
+        END IF;
+
+        v_C_DOCTYPE_ID:=Ad_Get_Doctype(Cur_SO.AD_Client_ID, Cur_SO.AD_Org_ID, 'SOO', 'SO') ;
+        v_ResultStr:='C_DOCTYPE_ID - ' || v_C_DOCTYPE_ID;
+        Ad_Sequence_Doctype(v_C_DOCTYPE_ID, Cur_SO.AD_Client_ID, 'Y', v_DocumentNo) ;
+        IF(v_DocumentNo IS NULL) THEN
+          Ad_Sequence_Doc('DocumentNo_C_Order', Cur_SO.AD_Client_ID, 'Y', v_DocumentNo) ;
+        END IF;
+
+        -- Get next C_Order_ID
+        Ad_Sequence_Next('C_Order', Cur_SO.AD_CLIENT_ID, v_C_Order_ID) ;
+        v_ResultStr:='C_ORDER_ID - ' || v_C_Order_ID;
+
+        v_ResultStr:='DocumentNo - ' || v_DocumentNo || ' m_pricelist_id - ' || Cur_SO.M_PriceList_ID;
+        INSERT
+        INTO C_ORDER
+          (
+            C_ORDER_ID, AD_CLIENT_ID, AD_ORG_ID, CREATEDBY,
+            UPDATEDBY, ISSOTRX, DOCUMENTNO, DOCSTATUS,
+            DOCACTION, C_DOCTYPE_ID, C_DOCTYPETARGET_ID, DATEORDERED,
+            DATEACCT, C_BPARTNER_ID, C_BPARTNER_LOCATION_ID, ISDISCOUNTPRINTED,
+            C_CURRENCY_ID, PAYMENTRULE, C_PAYMENTTERM_ID, INVOICERULE,
+            DELIVERYRULE, FREIGHTCOSTRULE, DELIVERYVIARULE, PRIORITYRULE,
+            TOTALLINES, GRANDTOTAL, M_WAREHOUSE_ID, M_PRICELIST_ID,
+            ISTAXINCLUDED, POSTED, PROCESSING, SALESREP_ID,
+            BILLTO_ID, C_CAMPAIGN_ID, C_PROJECT_ID, AD_USER_ID,
+            COPYFROM, DATEPROMISED, DESCRIPTION
+          )
+          VALUES
+          (
+            v_C_Order_ID, Cur_SO.AD_CLIENT_ID, Cur_SO.AD_ORG_ID, v_AD_User_ID,
+            v_AD_User_ID, 'Y', v_DocumentNo, 'DR',
+             'CO', '0', v_C_DOCTYPE_ID, TRUNC(now(), 'DD'),
+            TRUNC(now(), 'DD'), Cur_SO.C_BPARTNER_ID, Cur_SO.C_BPARTNER_LOCATION_ID, 'N',
+            Cur_SO.C_CURRENCY_ID, COALESCE(Cur_SO.PAYMENTRULE, 'P'), Cur_SO.C_PAYMENTTERM_ID, v_InvoiceRule,
+             'A', 'I', 'D', '5',
+            0, 0, Cur_SO.M_WAREHOUSE_ID, Cur_SO.M_PRICELIST_ID,
+             'N', 'N', 'N', Cur_SO.SALESREP_ID,
+            v_Billto_ID, Cur_SO.C_CAMPAIGN_ID, Cur_SO.C_PROJECT_ID, Cur_SO.AD_User_ID,
+             'N', TRUNC(now(), 'DD'), Cur_SO.PHASE_NAME ||' - '|| Cur_SO.DESCRIPTION
+          );
+
+        UPDATE C_PROJECTPHASE
+        SET C_ORDER_ID = v_C_Order_ID
+        WHERE C_PROJECTPHASE_ID = v_Record_ID;
+
+        -- Select the price list version that a applies for the price list of the header
+        SELECT M_Get_Pricelist_Version(Cur_SO.M_PriceList_ID, TRUNC(now(), 'DD'))
+        INTO v_M_PriceList_Version_ID
+        FROM DUAL;
+        IF (v_M_PriceList_Version_ID IS NULL) THEN
+          RAISE_APPLICATION_ERROR(-20000, '@PriceListVersionNotFound@'||'.');
+        ELSE
+         -- Select products, quantities, sequence numbers, descriptions and unit prices of the project phases and tasks
+            -- In both cases, if no unit price has been defined for a product,
+            -- price is taken from the price list of the project.
+            -- And if no price is defined in the price list, price is set to 0.
+          FOR Cur_SOLINES IN
+            (
+            SELECT 'phase' AS PROV, pp.SEQNO, pp.NAME AS LINE_NAME, pp.DESCRIPTION, pr.NAME AS PRODUCT_NAME, pp.M_PRODUCT_ID, pp.QTY, pp.PRICEACTUAL
+            FROM C_PROJECTPHASE pp, M_PRODUCT pr
+            WHERE pp.C_PROJECTPHASE_ID = v_Record_ID
+              AND pp.M_PRODUCT_ID = pr.M_PRODUCT_ID
+              AND pp.M_Product_ID IS NOT NULL
+              AND pp.IsActive = 'Y'
+            UNION
+            SELECT 'task' as PROV, pt.SEQNO, pt.NAME AS LINE_NAME, pt.DESCRIPTION, pr.NAME AS PRODUCT_NAME, pt.M_PRODUCT_ID, pt.QTY, pt.PRICEACTUAL
+            FROM C_PROJECTTASK pt, C_PROJECTPHASE pp, M_PRODUCT pr
+            WHERE pp.C_PROJECTPHASE_ID = v_Record_ID
+              AND pp.C_PROJECTPHASE_ID = pt.C_PROJECTPHASE_ID
+              AND pt.M_PRODUCT_ID = pr.M_PRODUCT_ID
+              AND pt.M_Product_ID IS NOT NULL
+              AND pt.IsActive = 'Y'
+            ORDER BY PROV ASC, SEQNO ASC
+            )
+          LOOP
+
+            -- Take StdPrice, PriceList and PriceLimit from Price List Version
+            SELECT COUNT(*)
+            INTO v_Size
+            FROM M_PRODUCT p,M_PRODUCTPRICE pp,M_PRICELIST_VERSION pv,M_PRICELIST pl
+            WHERE p.M_Product_ID=pp.M_Product_ID
+              AND pp.M_PriceList_Version_ID=pv.M_PriceList_Version_ID
+              AND pv.M_PriceList_ID=pl.M_PriceList_ID
+              AND pv.IsActive='Y'
+              AND p.M_Product_ID=Cur_SOLINES.M_PRODUCT_ID
+              AND pv.M_PriceList_Version_ID=v_M_PriceList_Version_ID
+              AND pl.C_Currency_ID=Cur_SO.C_CURRENCY_ID;
+            IF (v_Size>0) THEN
+              SELECT M_BOM_PriceStd(p.M_Product_ID, pv.M_PriceList_Version_ID) AS PriceStd,
+                M_BOM_PriceList(p.M_Product_ID, pv.M_PriceList_Version_ID) AS PriceList,
+                M_BOM_PriceLimit(p.M_Product_ID, pv.M_PriceList_Version_ID) AS PriceLimit,
+                p.C_UOM_ID
+              INTO v_PriceStd,v_PriceList,
+                v_PriceLimit,v_C_UOM_ID
+              FROM M_PRODUCT p,M_PRODUCTPRICE pp,M_PRICELIST_VERSION pv,M_PRICELIST pl
+              WHERE p.M_Product_ID=pp.M_Product_ID
+                AND pp.M_PriceList_Version_ID=pv.M_PriceList_Version_ID
+                AND pv.M_PriceList_ID=pl.M_PriceList_ID
+                AND pv.IsActive='Y'
+                AND p.M_Product_ID=Cur_SOLINES.M_PRODUCT_ID
+                AND pv.M_PriceList_Version_ID=v_M_PriceList_Version_ID
+                AND pl.C_Currency_ID=Cur_SO.C_CURRENCY_ID;
+            ELSE
+              v_PriceStd := NULL;
+              v_PriceList := NULL;
+              v_PriceLimit := NULL;
+              v_C_UOM_ID := NULL;
+            END IF;
+
+            -- Calculate Price Actual
+            v_PriceActual := COALESCE(Cur_SOLINES.priceactual,M_Get_Offers_Price(now(), Cur_SO.C_BPARTNER_ID, Cur_SOLINES.M_PRODUCT_ID, v_PriceStd, Cur_SOLINES.Qty, Cur_SO.M_PRICELIST_ID),0);
+
+            IF (v_PriceStd IS NULL) THEN
+             v_PriceStd :=  v_PriceActual ;
+            END IF;
+
+            IF (v_PriceList IS NULL) THEN
+              v_PriceList :=  v_PriceActual ;
+            END IF;
+
+             IF (v_PriceLimit IS NULL) THEN
+              v_PriceLimit :=  v_PriceActual ;
+            END IF;
+
+            IF (v_PriceList = 0) THEN
+               v_Discount := 0 ;
+            ELSE
+              -- Calculate rounded discount
+              v_Discount :=ROUND((v_PriceList-v_PriceActual) / v_PriceList*100, 2);
+            END IF;
+
+            IF (v_C_UOM_ID IS NULL) THEN
+              SELECT P.C_UOM_ID
+              INTO v_UOM
+              FROM M_PRODUCT P
+              WHERE P.M_PRODUCT_ID=Cur_SOLINES.M_PRODUCT_ID;
+            ELSE
+              v_UOM := v_C_UOM_ID;
+            END IF;
+
+            -- Get next C_OrderLine_ID
+            Ad_Sequence_Next('C_OrderLine', Cur_SO.AD_CLIENT_ID, v_C_OrderLine_ID) ;
+            v_ResultStr:='C_OrderLine_ID - ' || v_C_OrderLine_ID;
+
+            INSERT
+            INTO C_ORDERLINE
+              (
+                DateOrdered, M_Warehouse_ID, QtyOrdered, QtyDelivered,
+                QtyReserved, M_Shipper_ID, QtyInvoiced,
+                C_Currency_ID, PriceList, DatePromised, DateDelivered,
+                DateInvoiced, Created, IsActive, Line,
+                C_OrderLine_ID, AD_Client_ID, C_Order_ID, Description,
+                M_Product_ID, C_UOM_ID, DirectShip, CreatedBy,
+                UpdatedBy, FreightAmt, C_Charge_ID, ChargeAmt,
+                Updated, AD_Org_ID, S_ResourceAssignment_ID, C_BPartner_ID,
+                PriceActual,
+                C_Tax_ID, C_BPartner_Location_ID,
+                Discount, PriceLimit, Ref_OrderLine_ID, LineNetAmt,
+                M_AttributeSetInstance_ID, IsDescription, PriceStd
+              )
+              VALUES
+              (
+                TRUNC(now(), 'DD'), Cur_SO.M_WAREHOUSE_ID, Cur_SOLINES.Qty, 0,
+                0, NULL, 0,
+                Cur_SO.C_CURRENCY_ID, v_PriceList, TRUNC(now(), 'DD'), NULL,
+                NULL, now(), 'Y', Cur_SOLINES.SEQNO,
+                v_C_OrderLine_ID, Cur_SO.AD_CLIENT_ID, v_C_Order_ID, Cur_SOLINES.LINE_NAME || ' - ' || Cur_SOLINES.DESCRIPTION,
+                Cur_SOLINES.M_PRODUCT_ID, v_UOM, 'N', v_AD_User_ID,
+                v_AD_User_ID, 0, NULL, 0,
+                now(), Cur_SO.AD_ORG_ID, NULL, Cur_SO.C_BPARTNER_ID,
+                v_PriceActual,
+                C_Gettax(Cur_SOLINES.M_PRODUCT_ID, TRUNC(now(), 'DD'), Cur_SO.AD_ORG_ID, Cur_SO.M_WAREHOUSE_ID, Cur_SO.C_BPARTNER_LOCATION_ID, Cur_SO.C_BPARTNER_LOCATION_ID, Cur_SO.C_PROJECT_ID, 'Y'), Cur_SO.C_BPARTNER_LOCATION_ID,
+                v_Discount, v_PriceLimit, NULL, v_PriceActual * Cur_SOLINES.Qty,
+                NULL, 'N', v_PriceStd
+              )
+              ;
+          END LOOP;
+        END IF;
+        IF v_Message IS NOT NULL THEN
+          v_Message:=v_Message || ', ';
+        END IF;
+        v_Message:=v_Message || v_DocumentNo;
+      END LOOP;
+      v_Message:='@DocumentNo@: ' || v_Message;
+    END;
+    --<<FINISH_PROCESS>>
+    --  Update AD_PInstance
+    DBMS_OUTPUT.PUT_LINE('Updating PInstance - Finished ' || v_Message) ;
+    AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', v_Result, v_Message) ;
+    RETURN;
+  END; --BODY
+EXCEPTION
+WHEN OTHERS THEN
+  v_ResultStr:= '@ERROR=' || SQLERRM;
+  DBMS_OUTPUT.PUT_LINE(v_ResultStr) ;
+  ROLLBACK;
+  AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', 0, v_ResultStr) ;
+  RETURN;
+END C_GENERATESOFROMPROJECTPHASE
+]]></body>
+    </function>
+  </database>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src-db/database/model/functions/C_GETPERIODNAME.xml	Thu Oct 09 17:15:25 2008 +0000
@@ -0,0 +1,38 @@
+<?xml version="1.0"?>
+  <database name="FUNCTION C_GETPERIODNAME">
+    <function name="C_GETPERIODNAME" type="NVARCHAR">
+      <parameter name="p_periodno" type="NUMERIC" mode="in">
+        <default/>
+      </parameter>
+      <parameter name="p_year_id" type="VARCHAR" 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):  ______________________________________.
+************************************************************************/
+  v_Period NVARCHAR2(60) ;
+  --
+BEGIN
+  SELECT NAME
+  INTO v_Period
+  FROM C_PERIOD
+  WHERE C_YEAR_ID=p_Year_ID
+    AND periodno=p_PeriodNo;
+  RETURN v_Period;
+END C_GETPERIODNAME
+]]></body>
+    </function>
+  </database>
--- a/src-db/database/model/functions/C_INVOICE_PERCENTAGE_PAID.xml	Thu Oct 09 14:13:04 2008 +0000
+++ b/src-db/database/model/functions/C_INVOICE_PERCENTAGE_PAID.xml	Thu Oct 09 17:15:25 2008 +0000
@@ -26,7 +26,7 @@
       C_Currency_Convert(DB.amount,DB.C_Currency_ID,I.C_Currency_ID,
       now(), null, I.AD_Client_ID, I.AD_Org_ID))/
   (CASE SUM(C_Currency_Convert(DB.amount,DB.C_Currency_ID,I.C_Currency_ID,
-      now(), null, I.AD_Client_ID, I.AD_Org_ID)) WHEN 0 THEN NUMBER_NULL() ELSE SUM(C_Currency_Convert(DB.amount,DB.C_Currency_ID,I.C_Currency_ID,
+      now(), null, I.AD_Client_ID, I.AD_Org_ID)) WHEN 0 THEN to_number(NULL) ELSE SUM(C_Currency_Convert(DB.amount,DB.C_Currency_ID,I.C_Currency_ID,
       now(), null, I.AD_Client_ID, I.AD_Org_ID)) END) INTO v_percentaje
   FROM C_DEBT_PAYMENT DB, C_Invoice I
   WHERE DB.C_Invoice_ID = I.C_Invoice_ID
--- a/src-db/database/model/functions/C_INVOICE_POST.xml	Thu Oct 09 14:13:04 2008 +0000
+++ b/src-db/database/model/functions/C_INVOICE_POST.xml	Thu Oct 09 17:15:25 2008 +0000
@@ -884,13 +884,13 @@
                 CUR_BOM.Description,
                 CUR_BOM.M_ProductBOM_ID, CUR_BOM.C_UOM_ID,
                 CUR_BOM_Line.QtyInvoiced*CUR_BOM.BOMQty,
-                Bom_Pricelist(CUR_BOM.M_ProductBOM_ID, PriceList_Version_ID),M_GET_OFFER_PRICE(v_Offer_ID, Bom_Pricestd(CUR_BOM.M_ProductBOM_ID, PriceList_Version_ID)td),
-                Bom_Pricelimit(CUR_BOM.M_ProductBOM_ID, PriceList_Version_ID),
-                M_GET_OFFER_PRICE(v_Offer_ID, Bom_Pricestd(CUR_BOM.M_ProductBOM_ID, PriceList_Version_ID)) * CUR_BOM_Line.QtyInvoiced*CUR_BOM.BOMQty,
+                M_BOM_PriceList(CUR_BOM.M_ProductBOM_ID, PriceList_Version_ID),M_GET_OFFER_PRICE(v_Offer_ID, M_BOM_PriceStd(CUR_BOM.M_ProductBOM_ID, PriceList_Version_ID)td),
+                M_BOM_PriceLimit(CUR_BOM.M_ProductBOM_ID, PriceList_Version_ID),
+                M_GET_OFFER_PRICE(v_Offer_ID, M_BOM_PriceStd(CUR_BOM.M_ProductBOM_ID, PriceList_Version_ID)) * CUR_BOM_Line.QtyInvoiced*CUR_BOM.BOMQty,
                 CUR_BOM_Line.C_Charge_ID,v_ChargeAmt,
                 CUR_BOM_Line.C_Tax_ID);
                 */
-                -- Bom_Pricestd(CUR_BOM.M_ProductBOM_ID, PriceList_Version_ID), v_Offer_ID);
+                -- M_BOM_PriceStd(CUR_BOM.M_ProductBOM_ID, PriceList_Version_ID), v_Offer_ID);
                 -- One Time variables
                 INSERT
                 INTO C_INVOICELINE
@@ -908,11 +908,11 @@
                     v_NextNo, CUR_BOM_Line.AD_Client_ID, CUR_BOM_Line.AD_Org_ID, CUR_BOM_Line.IsActive,
                     now(), v_UpdatedBy, now(), v_UpdatedBy,
                     CUR_BOM_Line.C_Invoice_ID, v_Line, CUR_BOM.Description, CUR_BOM.M_ProductBOM_ID,
-                    CUR_BOM.C_UOM_ID, CUR_BOM_Line.QtyInvoiced*CUR_BOM.BOMQty, Bom_Pricelist(CUR_BOM.M_ProductBOM_ID, v_PriceList_Version_ID),
-                    M_Get_Offers_Price(v_DateInvoiced, v_BPartner_ID, CUR_BOM.M_ProductBOM_ID, Bom_Pricestd(CUR_BOM.M_ProductBOM_ID, v_PriceList_Version_ID),CUR_BOM_Line.QtyInvoiced*CUR_BOM.BOMQty, v_PriceList_ID),
-                    Bom_Pricelimit(CUR_BOM.M_ProductBOM_ID, v_PriceList_Version_ID),
-                    M_Get_Offers_Price(v_DateInvoiced, v_BPartner_ID, CUR_BOM.M_ProductBOM_ID, Bom_Pricestd(CUR_BOM.M_ProductBOM_ID, v_PriceList_Version_ID),CUR_BOM_Line.QtyInvoiced*CUR_BOM.BOMQty, v_PriceList_ID) * CUR_BOM_Line.QtyInvoiced*CUR_BOM.BOMQty, CUR_BOM_Line.C_Charge_ID, v_ChargeAmt,
-                    CUR_BOM_Line.C_Tax_ID, Bom_Pricestd(CUR_BOM.M_ProductBOM_ID, v_PriceList_Version_ID)
+                    CUR_BOM.C_UOM_ID, CUR_BOM_Line.QtyInvoiced*CUR_BOM.BOMQty, M_BOM_PriceList(CUR_BOM.M_ProductBOM_ID, v_PriceList_Version_ID),
+                    M_Get_Offers_Price(v_DateInvoiced, v_BPartner_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),
+                    M_BOM_PriceLimit(CUR_BOM.M_ProductBOM_ID, v_PriceList_Version_ID),
+                    M_Get_Offers_Price(v_DateInvoiced, v_BPartner_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) * CUR_BOM_Line.QtyInvoiced*CUR_BOM.BOMQty, CUR_BOM_Line.C_Charge_ID, v_ChargeAmt,
+                    CUR_BOM_Line.C_Tax_ID, M_BOM_PriceStd(CUR_BOM.M_ProductBOM_ID, v_PriceList_Version_ID)
                   )
                   ;
                 v_ChargeAmt:=0;
--- a/src-db/database/model/functions/C_ORDER_POST1.xml	Thu Oct 09 14:13:04 2008 +0000
+++ b/src-db/database/model/functions/C_ORDER_POST1.xml	Thu Oct 09 17:15:25 2008 +0000
@@ -723,11 +723,11 @@
               CUR_BOM.Description, CUR_BOM.M_ProductBOM_ID, CUR_BOM_Line.M_Warehouse_ID, CUR_BOM_Line.DirectShip,
               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,
               CUR_BOM_Line.QtyInvoiced*CUR_BOM.BOMQty, CUR_BOM_Line.M_Shipper_ID, CUR_BOM_Line.C_Currency_ID,
-     Bom_Pricelist(CUR_BOM.M_ProductBOM_ID, v_PriceList_Version_ID),
-              M_Get_Offers_Price(CUR_BOM_Line.DateOrdered, v_CBPartner_ID, CUR_BOM.M_ProductBOM_ID, Bom_Pricestd(CUR_BOM.M_ProductBOM_ID, v_PriceList_Version_ID), CUR_BOM_Line.QtyInvoiced*CUR_BOM.BOMQty, v_PriceList_ID), Bom_Pricelimit(CUR_BOM.M_ProductBOM_ID, v_PriceList_Version_ID),
-     M_Get_Offers_Price(CUR_BOM_Line.DateOrdered, v_CBPartner_ID, CUR_BOM.M_ProductBOM_ID, Bom_Pricestd(CUR_BOM.M_ProductBOM_ID, v_PriceList_Version_ID), CUR_BOM_Line.QtyInvoiced*CUR_BOM.BOMQty, v_PriceList_ID) * CUR_BOM_Line.QtyOrdered*CUR_BOM.BOMQty, CUR_BOM_Line.Discount,
+     M_BOM_PriceList(CUR_BOM.M_ProductBOM_ID, v_PriceList_Version_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), M_BOM_PriceLimit(CUR_BOM.M_ProductBOM_ID, v_PriceList_Version_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) * CUR_BOM_Line.QtyOrdered*CUR_BOM.BOMQty, CUR_BOM_Line.Discount,
               v_FreightAmt, CUR_BOM_Line.C_Charge_ID, v_ChargeAmt, CUR_BOM_Line.C_Tax_ID,
-              CUR_BOM_Line.QuantityOrder, CUR_BOM_Line.M_Product_UOM_ID, Bom_Pricestd(CUR_BOM.M_ProductBOM_ID, v_PriceList_Version_ID)
+              CUR_BOM_Line.QuantityOrder, CUR_BOM_Line.M_Product_UOM_ID, M_BOM_PriceStd(CUR_BOM.M_ProductBOM_ID, v_PriceList_Version_ID)
             )
             ; -- no Lot/SerNo for BOMs
           -- One Time variables
--- a/src-db/database/model/functions/C_PAYMENTDUEDATE.xml	Thu Oct 09 14:13:04 2008 +0000
+++ b/src-db/database/model/functions/C_PAYMENTDUEDATE.xml	Thu Oct 09 17:15:25 2008 +0000
@@ -84,7 +84,7 @@
   IF(v_fixMonthDay1 IS NOT NULL) THEN
     --First aply offset (days and months)
     v_docDate := p_DocDate + COALESCE(p_netDays, 0) ;
-    v_docDate := ADD_MONTHS2(v_docDate, COALESCE(p_fixMonthOffset, 0)) ;
+    v_docDate := C_ADD_MONTHS2(v_docDate, COALESCE(p_fixMonthOffset, 0)) ;
     IF((v_fixMonthDay2 < TO_NUMBER(TO_CHAR(TRUNC(v_docDate), 'dd'))) AND(v_fixMonthDay3 >= TO_NUMBER(TO_CHAR(TRUNC(v_docDate), 'dd')))) THEN
       v_FixMonthDay := v_fixMonthDay3;
     ELSIF((v_fixMonthDay1 < TO_NUMBER(TO_CHAR(TRUNC(v_docDate), 'dd'))) AND(v_fixMonthDay2 >= TO_NUMBER(TO_CHAR(TRUNC(v_docDate), 'dd')))) THEN
--- a/src-db/database/model/functions/DBA_USER_CONST_SEARCHCOND.xml	Thu Oct 09 14:13:04 2008 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,41 +0,0 @@
-<?xml version="1.0"?>
-  <database name="FUNCTION DBA_USER_CONST_SEARCHCOND">
-    <function name="DBA_USER_CONST_SEARCHCOND" type="VARCHAR">
-      <parameter name="p_constraintname" type="VARCHAR" 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):  ______________________________________.
-************************************************************************/
-  v_varchar2 VARCHAR2(32767) ;
-  TYPE RECORD IS REF CURSOR;
-    cur_searchCond RECORD;
-  BEGIN
-    FOR cur_searchCond IN
-      (SELECT SEARCH_CONDITION
-      FROM USER_CONSTRAINTS
-      WHERE CONSTRAINT_NAME=p_constraintName
-      )
-    LOOP
-      v_varchar2 := cur_searchCond.SEARCH_CONDITION;
-      v_varchar2 := TRIM(SUBSTR(v_varchar2, 1, 4000)) ;
-      EXIT;
-    END LOOP;
-    RETURN(v_varchar2) ;
-END DBA_USER_CONST_SEARCHCOND
-]]></body>
-    </function>
-  </database>
--- a/src-db/database/model/functions/DEBT_PAYMENT_BALANCING_REPLACE.xml	Thu Oct 09 14:13:04 2008 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,191 +0,0 @@
-<?xml version="1.0"?>
-  <database name="FUNCTION DEBT_PAYMENT_BALANCING_REPLACE">
-    <function name="DEBT_PAYMENT_BALANCING_REPLACE" type="NULL">
-      <parameter name="p_pinstance_id" type="VARCHAR" 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_Result NUMBER:=1; -- 0=failure
-  v_Record_ID VARCHAR2(32);
-  v_AD_User_ID VARCHAR2(32);
-  TYPE RECORD IS REF CURSOR;
-    Cur_DebtPaymentBalancing RECORD;
-    v_C_Debt_Payment_ID VARCHAR2(32) ;
-    v_C_Debt_Payment_Balancing VARCHAR2(32) ;
-    v_AmountCredit NUMBER(10) ;
-    v_AmountDebit NUMBER(10) ;
-    v_Amount NUMBER(10) ;
-    v_IsDirectPosting CHAR(1) ;
-    v_Posted CHAR(1) ;
-    v_IsReceipt CHAR(1) ;
-    v_Processed CHAR(1) ;
-    FINISH_PROCESS BOOLEAN:=false;
-  BEGIN
-    --  Update AD_PInstance
-    DBMS_OUTPUT.PUT_LINE('Updating PInstance - Processing ' || p_PInstance_ID) ;
-    v_ResultStr:='PInstanceNotFound';
-    AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'Y', NULL, NULL) ;
-  BEGIN --BODY
-    SELECT i.Record_ID,
-      i.AD_User_ID
-    INTO v_Record_ID,
-      v_AD_User_ID
-    FROM AD_PINSTANCE i
-    WHERE i.AD_PInstance_ID=p_PInstance_ID;
-    DBMS_OUTPUT.PUT_LINE('  Record_ID=' || v_Record_ID) ;
-    SELECT C_DEBT_PAYMENT.C_DEBT_PAYMENT_ID,
-      SUM(C_DEBT_PAYMENT_BAL_REPLACE.AMOUNTDEBIT),
-      SUM(C_DEBT_PAYMENT_BAL_REPLACE.AMOUNTCREDIT),
-      C_DEBT_PAYMENT.ISRECEIPT
-    INTO v_C_Debt_Payment_ID,
-      v_AmountDebit,
-      v_AmountCredit,
-      v_IsReceipt
-    FROM C_DEBT_PAYMENT_BAL_REPLACE,
-      C_DEBT_PAYMENT
-    WHERE C_DEBT_PAYMENT.C_DEBT_PAYMENT_ID=C_DEBT_PAYMENT_BAL_REPLACE.C_DEBT_PAYMENT_ID
-     and C_DEBT_PAYMENT.C_DEBT_PAYMENT_id = (select c_debt_payment_id
-                                               from C_DEBT_PAYMENT_BAL_REPLACE
-                                              where C_DEBT_PAYMENT_BAL_REPLACE_ID=v_Record_ID)
-    GROUP BY C_DEBT_PAYMENT.C_DEBT_PAYMENT_ID,
-      C_DEBT_PAYMENT.ISRECEIPT;
-    SELECT POSTED,
-      PROCESSED
-    INTO v_Posted,
-      v_Processed
-    FROM C_SETTLEMENT
-    WHERE C_SETTLEMENT_ID=
-      (SELECT C_SETTLEMENT_GENERATE_ID
-      FROM C_DEBT_PAYMENT
-      WHERE C_DEBT_PAYMENT_ID=v_C_Debt_Payment_ID
-      )
-      ;
-    IF(v_Posted='Y') THEN
-      RAISE_APPLICATION_ERROR(-20000, '@SettlementDocumentPosted@') ;
-    ELSE
-      SELECT ISDIRECTPOSTING
-      INTO v_IsDirectPosting
-      FROM C_DEBT_PAYMENT
-      WHERE C_DEBT_PAYMENT_ID=v_C_Debt_Payment_ID;
-      IF(v_IsDirectPosting='N') THEN
-        SELECT COALESCE(MAX(POSTED), 'N')
-        INTO v_Posted
-        FROM C_SETTLEMENT
-        WHERE C_SETTLEMENT_ID=
-          ( SELECT C_SETTLEMENT_CANCEL_ID
-          FROM C_DEBT_PAYMENT
-          WHERE C_DEBT_PAYMENT_ID=v_C_Debt_Payment_ID
-          )
-          ;
-        IF(v_Posted='Y') THEN
-          RAISE_APPLICATION_ERROR(-20000, '@SettlementCancelPosted@') ;
-        END IF;
-      END IF;
-    END IF;
-    IF(NOT FINISH_PROCESS) THEN
-      IF(v_IsReceipt='N') THEN
-        v_Amount:=v_AmountDebit - v_AmountCredit;
-      ELSE
-        v_Amount:=v_AmountCredit - v_AmountDebit;
-      END IF;
-      SELECT AMOUNT
-      INTO v_AmountDebit
-      FROM C_DEBT_PAYMENT
-      WHERE C_DEBT_PAYMENT_ID=v_C_Debt_Payment_ID;
-      v_Amount:=v_Amount - v_AmountDebit;
-      IF(v_Amount=0) THEN
-        IF(v_Processed='Y') THEN
-          UPDATE C_SETTLEMENT
-            SET PROCESSED='N'
-          WHERE C_SETTLEMENT_ID=
-            (SELECT C_SETTLEMENT_GENERATE_ID
-            FROM C_DEBT_PAYMENT
-            WHERE C_DEBT_PAYMENT_ID=v_C_Debt_Payment_ID
-            )
-            ;
-        END IF;
-        DELETE
-        FROM C_DEBT_PAYMENT_BALANCING
-        WHERE C_DEBT_PAYMENT_ID=v_C_Debt_Payment_ID;
-        FOR Cur_DebtPaymentBalancing in
-          (SELECT AMOUNTDEBIT,
-            AMOUNTCREDIT,
-            C_DEBT_PAYMENT_ID,
-            C_GLITEM_ID,
-            ISACTIVE,
-            CREATEDBY,
-            UPDATEDBY,
-            AD_CLIENT_ID,
-            AD_ORG_ID
-          FROM C_DEBT_PAYMENT_BAL_REPLACE
-          WHERE C_DEBT_PAYMENT_ID=v_C_Debt_Payment_ID
-          )
-        LOOP
-          Ad_Sequence_Next('C_Debt_Payment_Balancing', v_C_Debt_Payment_ID, v_C_Debt_Payment_Balancing) ;
-          INSERT
-          INTO C_DEBT_PAYMENT_BALANCING
-            (
-              C_DEBT_PAYMENT_BALANCING_ID, AD_CLIENT_ID, AD_ORG_ID, ISACTIVE,
-              CREATED, CREATEDBY, UPDATED, UPDATEDBY,
-              AMOUNTDEBIT, AMOUNTCREDIT, C_GLITEM_ID, C_DEBT_PAYMENT_ID
-            )
-            VALUES
-            (
-              v_C_Debt_Payment_Balancing, Cur_DebtPaymentBalancing.AD_CLIENT_ID, Cur_DebtPaymentBalancing.AD_ORG_ID, Cur_DebtPaymentBalancing.ISACTIVE,
-              now(), Cur_DebtPaymentBalancing.CREATEDBY, now(), Cur_DebtPaymentBalancing.UPDATEDBY,
-              Cur_DebtPaymentBalancing.AMOUNTDEBIT, Cur_DebtPaymentBalancing.AMOUNTCREDIT, Cur_DebtPaymentBalancing.C_GLITEM_ID, Cur_DebtPaymentBalancing.C_DEBT_PAYMENT_ID
-            )
-            ;
-        END LOOP;
-        DELETE
-        FROM C_DEBT_PAYMENT_BAL_REPLACE
-        WHERE C_DEBT_PAYMENT_ID=v_C_Debt_Payment_ID;
-        IF(v_Processed='Y') THEN
-          UPDATE C_SETTLEMENT
-            SET PROCESSED='Y'
-          WHERE C_SETTLEMENT_ID=
-            (SELECT C_SETTLEMENT_GENERATE_ID
-            FROM C_DEBT_PAYMENT
-            WHERE C_DEBT_PAYMENT_ID=v_C_Debt_Payment_ID
-            )
-            ;
-        END IF;
-      ELSE
-        RAISE_APPLICATION_ERROR(-20000, '@ProcessFailed@') ;
-      END IF;
-    END IF; --FINISH_PROCESS
-    --<<FINISH_PROCESS>>
-    --  Update AD_PInstance
-    DBMS_OUTPUT.PUT_LINE('Updating PInstance - Finished ' || v_Message) ;
-    AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', v_Result, v_Message) ;
-    RETURN;
-  END; --BODY
-EXCEPTION
-WHEN OTHERS THEN
-  v_ResultStr:= '@ERROR=' || SQLERRM;
-  DBMS_OUTPUT.PUT_LINE(v_ResultStr) ;
-  ROLLBACK;
-  AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', 0, v_ResultStr) ;
-  RETURN;
-END DEBT_PAYMENT_BALANCING_REPLACE
-]]></body>
-    </function>
-  </database>
--- a/src-db/database/model/functions/DIVIDE.xml	Thu Oct 09 14:13:04 2008 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,34 +0,0 @@
-<?xml version="1.0"?>
-  <database name="FUNCTION DIVIDE">
-    <function name="DIVIDE" type="NUMERIC">
-      <parameter name="p_dividendo" type="NUMERIC" mode="in">
-        <default/>
-      </parameter>
-      <parameter name="p_divisor" 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):  ______________________________________.
-************************************************************************/
-BEGIN
-  RETURN(p_dividendo/p_divisor) ;
-EXCEPTION
-WHEN OTHERS THEN
-  RETURN(NULL) ;
-END DIVIDE
-]]></body>
-    </function>
-  </database>
--- a/src-db/database/model/functions/GENERATEPOFROMPROJECT.xml	Thu Oct 09 14:13:04 2008 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,354 +0,0 @@
-<?xml version="1.0"?>
-  <database name="FUNCTION GENERATEPOFROMPROJECT">
-    <function name="GENERATEPOFROMPROJECT" type="NULL">
-      <parameter name="p_pinstance_id" type="VARCHAR" 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-2008 Openbravo SL
-* All Rights Reserved.
-* Contributor(s):  ______________________________________.
-************************************************************************/
-  v_ResultStr VARCHAR2(2000):='';
-  v_Message VARCHAR2(2000):='';
-  v_Result NUMBER:=1; -- 0=failure
-  v_Record_ID VARCHAR2(32);
-  v_AD_User_ID VARCHAR2(32);
-  TYPE RECORD IS REF CURSOR;
-    Cur_Parameter RECORD;
-    v_C_Order_ID VARCHAR2(32);
-    v_C_OrderLine_ID VARCHAR2(32);
-    v_C_DOCTYPE_ID VARCHAR2(32);
-    v_DocumentNo NVARCHAR2(30) ;
-  BEGIN
-    --  Update AD_PInstance
-    DBMS_OUTPUT.PUT_LINE('Updating PInstance - Processing ' || p_PInstance_ID) ;
-    v_ResultStr:='PInstanceNotFound';
-    AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'Y', NULL, NULL) ;
-  BEGIN --BODY
-    -- Get PARAMETERS
-    v_ResultStr:='ReadingParameters';
-    FOR Cur_Parameter IN
-      (SELECT i.Record_ID,
-        i.AD_User_ID,
-        i.AD_Client_ID,
-        i.AD_Org_ID,
-        p.ParameterName,
-        p.P_String,
-        p.P_Number,
-        p.P_Date,
-        p.P_Date_To
-      FROM AD_PInstance i
-      LEFT JOIN AD_PInstance_Para p
-        ON i.AD_PInstance_ID=p.AD_PInstance_ID
-      WHERE i.AD_PInstance_ID=p_PInstance_ID
-      ORDER BY p.SeqNo
-      )
-    LOOP
-      v_Record_ID:=Cur_Parameter.Record_ID;
-      v_AD_User_ID:=Cur_Parameter.AD_User_ID;
-    END LOOP; -- Get Parameter
-    DBMS_OUTPUT.PUT_LINE('  Record_ID=' || v_Record_ID) ;
-    DECLARE
-      v_BPartner_Location_ID VARCHAR2(32);
-      v_BillTo_ID VARCHAR2(32);
-      v_Size NUMBER;
-      v_UOM NUMBER;
-      v_PriceStd NUMBER;
-      v_PriceList NUMBER;
-      v_PriceLimit NUMBER;
-      v_C_UOM_ID VARCHAR2(32);
-      v_C_Currency_ID VARCHAR2(32);
-      v_PriceActual NUMBER;
-      v_Discount NUMBER;
-      v_Tax_ID VARCHAR2(32);
-      v_M_PriceList_Version_ID VARCHAR2(32);
-
-    TYPE RECORD IS REF CURSOR;
-      Cur_SO RECORD;
-      Cur_SOLINES RECORD;
-    BEGIN
-      v_ResultStr:='StartLoop';
-      FOR Cur_SO IN
-        (SELECT P.C_PROJECT_ID,
-          P.PROJECTSTATUS,
-          V.AD_CLIENT_ID,
-          V.AD_ORG_ID,
-          V.C_BPARTNER_ID,
-          V.C_INCOTERMS_ID,
-          V.C_PROJECT_VENDOR_ID,
-          V.INCOTERMS_DESCRIPTION,
-          V.M_PRICELIST_VERSION_ID,
-          V.M_PRICELIST_ID,
-          P.C_CAMPAIGN_ID,
-          PL.C_CURRENCY_ID,
-	  	  P.C_CURRENCY_ID AS PROJCURRENCY,
-          BP.PAYMENTRULEPO AS PAYMENTRULE,
-          BP.PO_PAYMENTTERM_ID AS C_PAYMENTTERM_ID,
-          P.M_WAREHOUSE_ID,
-          P.POREFERENCE,
-          P.SALESREP_ID,
-          P.CREATETEMPPRICELIST,
-          P.DESCRIPTION
-        FROM C_PROJECT P,
-          C_PROJECT_VENDOR V,
-          C_BPARTNER BP,
-          M_PRICELIST PL
-        WHERE P.C_PROJECT_ID=V.C_PROJECT_ID
-          AND V.C_BPARTNER_ID=BP.C_BPARTNER_ID
-          AND V.M_PRICELIST_ID=PL.M_PRICELIST_ID
-          AND V.ISACTIVE='Y'
-          AND V.C_PROJECT_VENDOR_ID=v_Record_ID
-        )
-      LOOP
-        -- Check that we have some restrictions
-        v_ResultStr:='CheckRestriction';
-        IF(Cur_SO.PROJECTSTATUS IS NULL OR Cur_SO.PROJECTSTATUS<>'OR') THEN
-         RAISE_APPLICATION_ERROR(-20000, '@Invalidprojectstatus@'||'. '||' @ChangeToOrder@'||'.');
-        ELSIF Cur_SO.C_BPARTNER_ID IS NULL THEN
-          RAISE_APPLICATION_ERROR(-20000, '@NoprojectBusinesspartner@');
-        ELSIF Cur_SO.C_PAYMENTTERM_ID IS NULL THEN
-          RAISE_APPLICATION_ERROR(-20000, '@ThebusinessPartner@'||' '||' @PaymenttermNotdefined@'||'.');
-        ELSIF Cur_SO.M_PRICELIST_ID IS NULL THEN
-          RAISE_APPLICATION_ERROR(-20000, '@ThebusinessPartner@'||' '||' @PricelistNotdefined@'||'.');
-        ELSIF Cur_SO.C_CURRENCY_ID IS NULL THEN
-          RAISE_APPLICATION_ERROR(-20000, '@ProjectCurrencyNotFound@'||'.');
-        ELSIF Cur_SO.M_WAREHOUSE_ID IS NULL THEN
-          RAISE_APPLICATION_ERROR(-20000, '@ProjectWarehouseNotFound@'||'.');
-        ELSIF Cur_SO.SALESREP_ID IS NULL THEN
-          RAISE_APPLICATION_ERROR(-20000, '@ProjectSalesRepNotFound@'||'.');
-        END IF;
-
-        v_C_DOCTYPE_ID:=Ad_Get_DocType(Cur_SO.AD_Client_ID, Cur_SO.AD_Org_ID, 'POO') ;
-        AD_Sequence_DocType(v_C_DOCTYPE_ID, Cur_SO.AD_Client_ID, 'Y', v_DocumentNo) ;
-        IF(v_DocumentNo IS NULL) THEN
-          AD_Sequence_Doc('DocumentNo_C_Order', Cur_SO.AD_Client_ID, 'Y', v_DocumentNo) ;
-        END IF;
-
-        -- Get Business Partner Ship Location
-        v_BPartner_Location_ID := C_GetBPLocationID(Cur_SO.C_BPartner_ID, 'S') ;
-        IF (v_BPartner_Location_ID IS NULL) THEN
-          RAISE_APPLICATION_ERROR(-20000, '@ThebusinessPartner@'||' '||' @ShiptoNotdefined@'||'.');
-        END IF;
-
-        -- Get Business Partner Bill Location
-        v_Billto_ID := C_GetBPLocationID(Cur_SO.C_BPartner_ID, 'B') ;
-        IF (v_Billto_ID IS NULL) THEN
-          RAISE_APPLICATION_ERROR(-20000, '@ThebusinessPartner@'||' '||' @BillToNotdefined@'||'.');
-        END IF;
-
-        -- Get next C_Order_ID
-        Ad_Sequence_Next('C_Order', Cur_SO.AD_CLIENT_ID, v_C_Order_ID) ;
-        v_ResultStr:='C_ORDER_ID - ' || v_C_Order_ID;
-
-        INSERT
-        INTO C_ORDER
-          (
-            C_ORDER_ID, AD_CLIENT_ID, AD_ORG_ID, CREATEDBY,
-            UPDATEDBY, ISSOTRX, DOCUMENTNO, DOCSTATUS,
-            DOCACTION, C_DOCTYPE_ID, C_DOCTYPETARGET_ID, DATEORDERED,
-            DATEACCT, C_BPARTNER_ID, C_BPARTNER_LOCATION_ID, ISDISCOUNTPRINTED,
-            C_CURRENCY_ID, PAYMENTRULE, C_PAYMENTTERM_ID, INVOICERULE,
-            DELIVERYRULE, FREIGHTCOSTRULE, DELIVERYVIARULE, PRIORITYRULE,
-            TOTALLINES, GRANDTOTAL, M_WAREHOUSE_ID, M_PRICELIST_ID,
-            ISTAXINCLUDED, POSTED, PROCESSING, SALESREP_ID,
-            BILLTO_ID, POREFERENCE, C_CAMPAIGN_ID, C_PROJECT_ID,
-            AD_USER_ID, COPYFROM, C_INCOTERMS_ID, INCOTERMSDESCRIPTION,
-            DATEPROMISED, DESCRIPTION
-          )
-          VALUES
-          (
-            v_C_Order_ID, Cur_SO.AD_CLIENT_ID, Cur_SO.AD_ORG_ID, v_AD_User_ID,
-            v_AD_User_ID, 'N', v_DocumentNo, 'DR',
-             'CO', '0', v_C_DOCTYPE_ID, TRUNC(now(), 'DD'),
-            TRUNC(now(), 'DD'), Cur_SO.C_BPARTNER_ID, v_BPartner_Location_ID, 'N',
-            Cur_SO.C_CURRENCY_ID, COALESCE(Cur_SO.PAYMENTRULE, 'P'), Cur_SO.C_PAYMENTTERM_ID, 'D',
-             'A', 'I', 'D', '5',
-            0, 0, Cur_SO.M_WAREHOUSE_ID, Cur_SO.M_PRICELIST_ID,
-             'N', 'N', 'N', Cur_SO.SALESREP_ID,
-            v_BillTo_ID, Cur_SO.POREFERENCE, Cur_SO.C_CAMPAIGN_ID, Cur_SO.C_PROJECT_ID,
-            NULL, 'N', Cur_SO.C_INCOTERMS_ID, Cur_SO.INCOTERMS_DESCRIPTION,
-            TRUNC(now(), 'DD'), Cur_SO.DESCRIPTION
-          )
-          ;
-
-        -- Select the price list version that a applies for the price list of the header
-        SELECT Get_Pricelist_Version(Cur_SO.M_PriceList_ID, TRUNC(now(), 'DD'))
-        INTO v_M_PriceList_Version_ID
-        FROM DUAL;
-        IF (v_M_PriceList_Version_ID IS NULL) THEN
-          RAISE_APPLICATION_ERROR(-20000, '@PriceListVersionNotFound@'||'.');
-        ELSE
-         -- Select products, quantities, sequence numbers, descriptions and unit prices of the project
-            -- In both cases, if no unit price has been defined for a product,
-            -- price is taken from the price list of the project.
-            -- And if no price is defined in the price list, price is set to 0.
-          FOR Cur_SOLINES IN
-            (
-            SELECT pl.LINE AS SEQNO, pl.PRODUCT_DESCRIPTION AS DESCRIPTION, pl.M_PRODUCT_ID,
-              pl.PLANNEDQTY AS QTY, pl.PLANNEDPOPRICE AS PRICEACTUAL, pl.C_TAX_ID
-            FROM C_PROJECTLINE pl, C_PROJECT_VENDOR  pv
-            WHERE pv.C_PROJECT_VENDOR_ID = v_Record_ID
-              AND pl.C_PROJECT_ID = pv.C_PROJECT_ID
-              AND pl.M_Product_ID IS NOT NULL
-              AND pl.IsActive = 'Y'
-            ORDER BY SEQNO ASC
-            )
-          LOOP
-
-            -- Take StdPrice, PriceList and PriceLimit from Price List Version
-            SELECT COUNT(*)
-            INTO v_Size
-            FROM M_PRODUCT p,M_PRODUCTPRICE pp,M_PRICELIST_VERSION pv,M_PRICELIST pl
-            WHERE p.M_Product_ID=pp.M_Product_ID
-              AND pp.M_PriceList_Version_ID=pv.M_PriceList_Version_ID
-              AND pv.M_PriceList_ID=pl.M_PriceList_ID
-              AND pv.IsActive='Y'
-              AND p.M_Product_ID=Cur_SOLINES.M_PRODUCT_ID
-              AND pv.M_PriceList_Version_ID=v_M_PriceList_Version_ID
-              AND pl.C_Currency_ID=Cur_SO.C_CURRENCY_ID;
-            IF (v_Size>0) THEN
-              SELECT Bom_Pricestd(p.M_Product_ID, pv.M_PriceList_Version_ID) AS PriceStd,
-                Bom_Pricelist(p.M_Product_ID, pv.M_PriceList_Version_ID) AS PriceList,
-                Bom_Pricelimit(p.M_Product_ID, pv.M_PriceList_Version_ID) AS PriceLimit,
-                p.C_UOM_ID
-              INTO v_PriceStd,v_PriceList,
-                v_PriceLimit,v_C_UOM_ID
-              FROM M_PRODUCT p,M_PRODUCTPRICE pp,M_PRICELIST_VERSION pv,M_PRICELIST pl
-              WHERE p.M_Product_ID=pp.M_Product_ID
-                AND pp.M_PriceList_Version_ID=pv.M_PriceList_Version_ID
-                AND pv.M_PriceList_ID=pl.M_PriceList_ID
-                AND pv.IsActive='Y'
-                AND p.M_Product_ID=Cur_SOLINES.M_PRODUCT_ID
-                AND pv.M_PriceList_Version_ID=v_M_PriceList_Version_ID
-                AND pl.C_Currency_ID=Cur_SO.C_CURRENCY_ID;
-            ELSE
-              v_PriceStd := NULL;
-              v_PriceList := NULL;
-              v_PriceLimit := NULL;
-              v_C_UOM_ID := NULL;
-            END IF;
-
-            -- Calculate Price Actual
-            IF (Cur_SOLINES.priceactual=0) THEN
-              Cur_SOLINES.priceactual:=NULL;
-            END IF;
-            v_PriceActual := COALESCE(Cur_SOLINES.priceactual,M_Get_Offers_Price(now(), Cur_SO.C_BPARTNER_ID, Cur_SOLINES.M_PRODUCT_ID, v_PriceStd, Cur_SOLINES.Qty, Cur_SO.M_PRICELIST_ID),0);
-
-            IF (v_PriceStd IS NULL) THEN
-             v_PriceStd :=  v_PriceActual ;
-            END IF;
-
-            IF (v_PriceList IS NULL) THEN
-              v_PriceList :=  v_PriceActual ;
-            END IF;
-
-            IF (v_PriceLimit IS NULL) THEN
-              v_PriceLimit :=  v_PriceActual ;
-            END IF;
-
-	        -- Make currency conversion if project header's currency (hence, project lines currency) is different from supplier's price list currency
-            IF (Cur_SO.C_CURRENCY_ID != Cur_SO.PROJCURRENCY) THEN
-              v_PriceActual := COALESCE(C_Currency_Convert(v_PriceActual, Cur_SO.PROJCURRENCY, Cur_SO.C_CURRENCY_ID, now(), NULL, Cur_SO.AD_CLIENT_ID, Cur_SO.AD_ORG_ID),0);
-	          v_PriceStd := COALESCE(C_Currency_Convert(v_PriceStd, Cur_SO.PROJCURRENCY, Cur_SO.C_CURRENCY_ID, now(), NULL, Cur_SO.AD_CLIENT_ID, Cur_SO.AD_ORG_ID),0);
-              v_PriceList := COALESCE(C_Currency_Convert(v_PriceList, Cur_SO.PROJCURRENCY, Cur_SO.C_CURRENCY_ID, now(), NULL, Cur_SO.AD_CLIENT_ID, Cur_SO.AD_ORG_ID),0);
-              v_PriceLimit := COALESCE(C_Currency_Convert(v_PriceLimit, Cur_SO.PROJCURRENCY, Cur_SO.C_CURRENCY_ID, now(), NULL, Cur_SO.AD_CLIENT_ID, Cur_SO.AD_ORG_ID),0);
-	        END IF;
-
-	        -- Calculating the discount
-	        IF (v_PriceList = 0) THEN
-               v_Discount := 0 ;
-            ELSE
-              -- Calculate rounded discount
-              v_Discount :=ROUND((v_PriceList-v_PriceActual) / v_PriceList*100, 2);
-            END IF;
-
-            IF (v_C_UOM_ID IS NULL) THEN
-              SELECT P.C_UOM_ID
-              INTO v_UOM
-              FROM M_PRODUCT P
-              WHERE P.M_PRODUCT_ID=Cur_SOLINES.M_PRODUCT_ID;
-            ELSE
-              v_UOM := v_C_UOM_ID;
-            END IF;
-
-            IF (Cur_SOLINES.C_TAX_ID IS NULL) THEN
-              v_Tax_ID:= C_Gettax(Cur_SOLINES.M_PRODUCT_ID, TRUNC(now(), 'DD'), Cur_SO.AD_ORG_ID, Cur_SO.M_WAREHOUSE_ID, v_BPartner_Location_ID, v_BPartner_Location_ID, Cur_SO.C_PROJECT_ID, 'Y') ;
-            ELSE
-              v_Tax_ID:=Cur_SOLINES.C_TAX_ID;
-            END IF;
-
-            -- Get next C_OrderLine_ID
-            Ad_Sequence_Next('C_OrderLine', Cur_SO.AD_CLIENT_ID, v_C_OrderLine_ID) ;
-            v_ResultStr:='C_OrderLine_ID - ' || v_C_OrderLine_ID;
-
-            INSERT
-            INTO C_ORDERLINE
-              (
-                DateOrdered, M_Warehouse_ID, QtyOrdered, QtyDelivered,
-                QtyReserved, M_Shipper_ID, QtyInvoiced,
-                C_Currency_ID, PriceList, DatePromised, DateDelivered,
-                DateInvoiced, Created, IsActive, Line,
-                C_OrderLine_ID, AD_Client_ID, C_Order_ID, Description,
-                M_Product_ID, C_UOM_ID, DirectShip, CreatedBy,
-                UpdatedBy, FreightAmt, C_Charge_ID, ChargeAmt,
-                Updated, AD_Org_ID, S_ResourceAssignment_ID, C_BPartner_ID,
-                PriceActual,
-                C_Tax_ID, C_BPartner_Location_ID,
-                Discount, PriceLimit, Ref_OrderLine_ID, LineNetAmt,
-                M_AttributeSetInstance_ID, IsDescription, PriceStd
-              )
-              VALUES
-              (
-                TRUNC(now(), 'DD'), Cur_SO.M_WAREHOUSE_ID, Cur_SOLINES.Qty, 0,
-                0, NULL, 0,
-                Cur_SO.C_CURRENCY_ID, v_PriceList, TRUNC(now(), 'DD'), NULL,
-                NULL, now(), 'Y', Cur_SOLINES.SEQNO,
-                v_C_OrderLine_ID, Cur_SO.AD_CLIENT_ID, v_C_Order_ID, Cur_SOLINES.DESCRIPTION,
-                Cur_SOLINES.M_PRODUCT_ID, v_UOM, 'N', v_AD_User_ID,
-                v_AD_User_ID, 0, NULL, 0,
-                now(), Cur_SO.AD_ORG_ID, NULL, Cur_SO.C_BPARTNER_ID,
-                v_PriceActual,
-                v_Tax_ID, v_BPartner_Location_ID,
-                v_Discount, v_PriceLimit, NULL, v_PriceActual * Cur_SOLINES.Qty,
-                NULL, 'N', v_PriceStd
-              )
-              ;
-          END LOOP;
-        END IF;
-
-        IF v_Message IS NOT NULL THEN
-          v_Message:=v_Message || ', ';
-        END IF;
-        v_Message:=v_Message || v_DocumentNo;
-      END LOOP;
-      v_Message:='@DocumentNo@: ' || v_Message;
-    END;
-    --<<FINISH_PROCESS>>
-    --  Update AD_PInstance
-    DBMS_OUTPUT.PUT_LINE('Updating PInstance - Finished ' || v_Message) ;
-    AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', v_Result, v_Message) ;
-    RETURN;
-  END; --BODY
-EXCEPTION
-WHEN OTHERS THEN
-  v_ResultStr:= '@ERROR=' || SQLERRM;
-  DBMS_OUTPUT.PUT_LINE(v_ResultStr) ;
-  ROLLBACK;
-  AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', 0, v_ResultStr) ;
-  RETURN;
-END GENERATEPOFROMPROJECT
-]]></body>
-    </function>
-  </database>
--- a/src-db/database/model/functions/GENERATESOFROMPROJECT.xml	Thu Oct 09 14:13:04 2008 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,319 +0,0 @@
-<?xml version="1.0"?>
-  <database name="FUNCTION GENERATESOFROMPROJECT">
-    <function name="GENERATESOFROMPROJECT" type="NULL">
-      <parameter name="p_pinstance_id" type="VARCHAR" 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-2008 Openbravo SL
-* All Rights Reserved.
-* Contributor(s):  ______________________________________.
-************************************************************************/
-  v_ResultStr VARCHAR2(2000):='';
-  v_Message VARCHAR2(2000):='';
-  v_Result NUMBER:=1; -- 0=failure
-  v_Record_ID VARCHAR2(32);
-  v_AD_User_ID VARCHAR2(32);
-  TYPE RECORD IS REF CURSOR;
-    Cur_Parameter RECORD;
-    v_C_Order_ID VARCHAR2(32);
-    v_C_OrderLine_ID VARCHAR2(32);
-    v_C_DOCTYPE_ID VARCHAR2(32);
-    v_DocumentNo NVARCHAR2(30) ;
-  BEGIN
-    --  Update AD_PInstance
-    DBMS_OUTPUT.PUT_LINE('Updating PInstance - Processing ' || p_PInstance_ID) ;
-    v_ResultStr:='PInstanceNotFound';
-    AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'Y', NULL, NULL) ;
-  BEGIN --BODY
-    -- Get PARAMETERS
-    v_ResultStr:='ReadingParameters';
-    FOR Cur_Parameter IN
-      (SELECT i.Record_ID,
-        i.AD_User_ID,
-        i.AD_Client_ID,
-        i.AD_Org_ID,
-        p.ParameterName,
-        p.P_String,
-        p.P_Number,
-        p.P_Date,
-        p.P_Date_To
-      FROM AD_PInstance i
-      LEFT JOIN AD_PInstance_Para p
-        ON i.AD_PInstance_ID=p.AD_PInstance_ID
-      WHERE i.AD_PInstance_ID=p_PInstance_ID
-      ORDER BY p.SeqNo
-      )
-    LOOP
-      v_Record_ID:=Cur_ParameteR.Record_ID;
-      v_AD_User_ID:=Cur_ParameteR.AD_User_ID;
-    END LOOP; -- Get Parameter
-    DBMS_OUTPUT.PUT_LINE('  Record_ID=' || v_Record_ID) ;
-    DECLARE
-      v_Size NUMBER;
-      v_UOM NUMBER;
-      v_PriceStd NUMBER;
-      v_PriceList NUMBER;
-      v_PriceLimit NUMBER;
-      v_C_UOM_ID VARCHAR2(32);
-      v_C_Currency_ID VARCHAR2(32);
-      v_M_PriceList_ID VARCHAR2(32);
-      v_Billto_ID VARCHAR2(32);
-      v_InvoiceRule CHAR;
-      v_M_PriceList_Version_ID VARCHAR2(32);
-      v_PriceActual NUMBER;
-      v_Discount NUMBER;
-      v_Tax_ID VARCHAR2(32);
-
-    TYPE RECORD IS REF CURSOR;
-      Cur_SO RECORD;
-      Cur_SOLINES RECORD;
-    BEGIN
-      v_ResultStr:='StartLoop';
-      FOR Cur_SO IN
-        (SELECT * FROM C_PROJECT WHERE C_PROJECT_ID=v_Record_ID)
-      LOOP
-        -- Check that we have some restrictions
-        v_ResultStr:='CheckRestriction';
-        IF(Cur_SO.PROJECTSTATUS IS NULL OR Cur_SO.PROJECTSTATUS<>'OR') THEN
-          RAISE_APPLICATION_ERROR(-20000, '@Invalidprojectstatus@');
-        ELSIF Cur_SO.C_BPARTNER_ID IS NULL THEN
-          RAISE_APPLICATION_ERROR(-20000, '@NoprojectBusinesspartner@');
-        ELSIF Cur_SO.C_BPARTNER_LOCATION_ID IS NULL THEN
-          RAISE_APPLICATION_ERROR(-20000, '@ThebusinessPartner@'||' '||' @ShiptoNotdefined@'||'.');
-        ELSIF Cur_SO.BILLTO_ID IS NULL THEN
-          RAISE_APPLICATION_ERROR(-20000, '@ThebusinessPartner@'||' '||' @BillToNotdefined@'||'.');
-        ELSIF Cur_SO.C_PAYMENTTERM_ID IS NULL THEN
-          RAISE_APPLICATION_ERROR(-20000, '@ThebusinessPartner@'||' '||' @PaymenttermNotdefined@'||'.');
-        ELSIF Cur_SO.M_PRICELIST_ID IS NULL THEN
-          RAISE_APPLICATION_ERROR(-20000, '@ThebusinessPartner@'||' '||' @PricelistNotdefined@'||'.');
-        ELSIF Cur_SO.C_CURRENCY_ID IS NULL THEN
-          RAISE_APPLICATION_ERROR(-20000, '@ProjectCurrencyNotFound@'||'.');
-        ELSIF Cur_SO.M_WAREHOUSE_ID IS NULL THEN
-          RAISE_APPLICATION_ERROR(-20000, '@ProjectWarehouseNotFound@'||'.');
-        ELSIF Cur_SO.SALESREP_ID IS NULL THEN
-          RAISE_APPLICATION_ERROR(-20000, '@ProjectSalesRepNotFound@'||'.');
-        END IF;
-
-        -- Get Business Partner Location
-        v_Billto_ID := Cur_SO.BILLTO_ID;
-        IF (v_Billto_ID IS NULL) THEN
-          v_Billto_ID := C_GetBPLocationID(Cur_SO.C_BPartner_ID, 'B') ;
-        END IF;
-
-        SELECT InvoiceRule INTO v_InvoiceRule
-        FROM C_BPARTNER
-        WHERE C_BPartner_ID = Cur_SO.C_BPARTNER_ID;
-        IF (v_InvoiceRule IS NULL) THEN
-           RAISE_APPLICATION_ERROR(-20000, '@ThebusinessPartner@'||' '||' @InvoiceRuleNotdefined@'||'.');
-        END IF;
-
-        v_C_DOCTYPE_ID:=Ad_Get_DocType(Cur_SO.AD_Client_ID, Cur_SO.AD_Org_ID, 'SOO', 'SO') ;
-        AD_Sequence_DocType(v_C_DOCTYPE_ID, Cur_SO.AD_Client_ID, 'Y', v_DocumentNo) ;
-        IF(v_DocumentNo IS NULL) THEN
-          AD_Sequence_Doc('DocumentNo_C_Order', Cur_SO.AD_Client_ID, 'Y', v_DocumentNo) ;
-        END IF;
-
-        -- Get next C_Order_ID
-        Ad_Sequence_Next('C_Order', Cur_SO.AD_CLIENT_ID, v_C_Order_ID) ;
-        v_ResultStr:='C_ORDER_ID - ' || v_C_Order_ID;
-
-        INSERT
-        INTO C_ORDER
-          (
-            C_ORDER_ID, AD_CLIENT_ID, AD_ORG_ID, CREATEDBY,
-            UPDATEDBY, ISSOTRX, DOCUMENTNO, DOCSTATUS,
-            DOCACTION, C_DOCTYPE_ID, C_DOCTYPETARGET_ID, DATEORDERED,
-            DATEACCT, C_BPARTNER_ID, C_BPARTNER_LOCATION_ID, ISDISCOUNTPRINTED,
-            C_CURRENCY_ID, PAYMENTRULE, C_PAYMENTTERM_ID, INVOICERULE,
-            DELIVERYRULE, FREIGHTCOSTRULE, DELIVERYVIARULE, PRIORITYRULE,
-            TOTALLINES, GRANDTOTAL, M_WAREHOUSE_ID, M_PRICELIST_ID,
-            ISTAXINCLUDED, POSTED, PROCESSING, SALESREP_ID,
-            BILLTO_ID, C_CAMPAIGN_ID, C_PROJECT_ID, AD_USER_ID,
-            COPYFROM, DATEPROMISED
-          )
-          VALUES
-          (
-            v_C_Order_ID, Cur_SO.AD_CLIENT_ID, Cur_SO.AD_ORG_ID, v_AD_User_ID,
-            v_AD_User_ID, 'Y', v_DocumentNo, 'DR',
-             'CO', 0, v_C_DOCTYPE_ID, TRUNC(now(), 'DD'),
-            TRUNC(now(), 'DD'), Cur_SO.C_BPARTNER_ID, Cur_SO.C_BPARTNER_LOCATION_ID, 'N',
-            Cur_SO.C_CURRENCY_ID, COALESCE(Cur_SO.PAYMENTRULE, 'P'), Cur_SO.C_PAYMENTTERM_ID,  v_InvoiceRule,
-             'A', 'I', 'D', '5',
-            0, 0, Cur_SO.M_WAREHOUSE_ID, Cur_SO.M_PRICELIST_ID,
-             'N', 'N', 'N', Cur_SO.SALESREP_ID,
-            v_Billto_ID, Cur_SO.C_CAMPAIGN_ID, Cur_SO.C_PROJECT_ID, Cur_SO.AD_User_ID,
-             'N', TRUNC(now(), 'DD')
-          )
-          ;
-
-        -- Select the price list version that a applies for the price list of the header
-        SELECT Get_Pricelist_Version(Cur_SO.M_PriceList_ID, TRUNC(now(), 'DD'))
-        INTO v_M_PriceList_Version_ID
-        FROM DUAL;
-        IF (v_M_PriceList_Version_ID IS NULL) THEN
-          RAISE_APPLICATION_ERROR(-20000, '@PriceListVersionNotFound@'||'.');
-        ELSE
-         -- Select products, quantities, sequence numbers, descriptions and unit prices of the project
-            -- In both cases, if no unit price has been defined for a product,
-            -- price is taken from the price list of the project.
-            -- And if no price is defined in the price list, price is set to 0.
-          FOR Cur_SOLINES IN
-            (
-            SELECT pl.LINE AS SEQNO, pl.PRODUCT_DESCRIPTION AS DESCRIPTION, pl.M_PRODUCT_ID,
-              pl.PLANNEDQTY AS QTY, pl.PLANNEDPRICE AS PRICEACTUAL, pl.C_TAX_ID
-            FROM C_PROJECTLINE pl
-            WHERE pl.C_PROJECT_ID = v_Record_ID
-              AND pl.M_Product_ID IS NOT NULL
-              AND pl.IsActive = 'Y'
-            ORDER BY SEQNO ASC
-            )
-          LOOP
-
-            -- Take StdPrice, PriceList and PriceLimit from Price List Version
-            SELECT COUNT(*)
-            INTO v_Size
-            FROM M_PRODUCT p,M_PRODUCTPRICE pp,M_PRICELIST_VERSION pv,M_PRICELIST pl
-            WHERE p.M_Product_ID=pp.M_Product_ID
-              AND pp.M_PriceList_Version_ID=pv.M_PriceList_Version_ID
-              AND pv.M_PriceList_ID=pl.M_PriceList_ID
-              AND pv.IsActive='Y'
-              AND p.M_Product_ID=Cur_SOLINES.M_PRODUCT_ID
-              AND pv.M_PriceList_Version_ID=v_M_PriceList_Version_ID
-              AND pl.C_Currency_ID=Cur_SO.C_CURRENCY_ID;
-            IF (v_Size>0) THEN
-              SELECT Bom_Pricestd(p.M_Product_ID, pv.M_PriceList_Version_ID) AS PriceStd,
-                Bom_Pricelist(p.M_Product_ID, pv.M_PriceList_Version_ID) AS PriceList,
-                Bom_Pricelimit(p.M_Product_ID, pv.M_PriceList_Version_ID) AS PriceLimit,
-                p.C_UOM_ID
-              INTO v_PriceStd,v_PriceList,
-                v_PriceLimit,v_C_UOM_ID
-              FROM M_PRODUCT p,M_PRODUCTPRICE pp,M_PRICELIST_VERSION pv,M_PRICELIST pl
-              WHERE p.M_Product_ID=pp.M_Product_ID
-                AND pp.M_PriceList_Version_ID=pv.M_PriceList_Version_ID
-                AND pv.M_PriceList_ID=pl.M_PriceList_ID
-                AND pv.IsActive='Y'
-                AND p.M_Product_ID=Cur_SOLINES.M_PRODUCT_ID
-                AND pv.M_PriceList_Version_ID=v_M_PriceList_Version_ID
-                AND pl.C_Currency_ID=Cur_SO.C_CURRENCY_ID;
-            ELSE
-              v_PriceStd := NULL;
-              v_PriceList := NULL;
-              v_PriceLimit := NULL;
-              v_C_UOM_ID := NULL;
-            END IF;
-
-            -- Calculate Price Actual
-            IF (Cur_SOLINES.priceactual=0) THEN
-              Cur_SOLINES.priceactual:=NULL;
-            END IF;
-            v_PriceActual := COALESCE(Cur_SOLINES.priceactual,M_Get_Offers_Price(now(), Cur_SO.C_BPARTNER_ID, Cur_SOLINES.M_PRODUCT_ID, v_PriceStd, Cur_SOLINES.Qty, Cur_SO.M_PRICELIST_ID),0);
-
-            IF (v_PriceStd IS NULL) THEN
-             v_PriceStd :=  v_PriceActual ;
-            END IF;
-
-            IF (v_PriceList IS NULL) THEN
-              v_PriceList :=  v_PriceActual ;
-            END IF;
-
-            IF (v_PriceLimit IS NULL) THEN
-              v_PriceLimit :=  v_PriceActual ;
-            END IF;
-
-            IF (v_PriceList = 0) THEN
-               v_Discount := 0 ;
-            ELSE
-              -- Calculate rounded discount
-              v_Discount :=ROUND((v_PriceList-v_PriceActual) / v_PriceList*100, 2);
-            END IF;
-
-            IF (v_C_UOM_ID IS NULL) THEN
-              SELECT P.C_UOM_ID
-              INTO v_UOM
-              FROM M_PRODUCT P
-              WHERE P.M_PRODUCT_ID=Cur_SOLINES.M_PRODUCT_ID;
-            ELSE
-              v_UOM := v_C_UOM_ID;
-            END IF;
-
-            IF (Cur_SOLINES.C_TAX_ID IS NULL) THEN
-              v_Tax_ID:= C_Gettax(Cur_SOLINES.M_PRODUCT_ID, TRUNC(now(), 'DD'), Cur_SO.AD_ORG_ID, Cur_SO.M_WAREHOUSE_ID, Cur_SO.C_BPARTNER_LOCATION_ID, Cur_SO.C_BPARTNER_LOCATION_ID, Cur_SO.C_PROJECT_ID, 'Y') ;
-            ELSE
-              v_Tax_ID:=Cur_SOLINES.C_TAX_ID;
-            END IF;
-
-            -- Get next C_OrderLine_ID
-            Ad_Sequence_Next('C_OrderLine', Cur_SO.AD_CLIENT_ID, v_C_OrderLine_ID) ;
-            v_ResultStr:='C_OrderLine_ID - ' || v_C_OrderLine_ID;
-
-            INSERT
-            INTO C_ORDERLINE
-              (
-                DateOrdered, M_Warehouse_ID, QtyOrdered, QtyDelivered,
-                QtyReserved, M_Shipper_ID, QtyInvoiced,
-                C_Currency_ID, PriceList, DatePromised, DateDelivered,
-                DateInvoiced, Created, IsActive, Line,
-                C_OrderLine_ID, AD_Client_ID, C_Order_ID, Description,
-                M_Product_ID, C_UOM_ID, DirectShip, CreatedBy,
-                UpdatedBy, FreightAmt, C_Charge_ID, ChargeAmt,
-                Updated, AD_Org_ID, S_ResourceAssignment_ID, C_BPartner_ID,
-                PriceActual,
-                C_Tax_ID, C_BPartner_Location_ID,
-                Discount, PriceLimit, Ref_OrderLine_ID, LineNetAmt,
-                M_AttributeSetInstance_ID, IsDescription, PriceStd
-              )
-              VALUES
-              (
-                TRUNC(now(), 'DD'), Cur_SO.M_WAREHOUSE_ID, Cur_SOLINES.Qty, 0,
-                0, NULL, 0,
-                Cur_SO.C_CURRENCY_ID, v_PriceList, TRUNC(now(), 'DD'), NULL,
-                NULL, now(), 'Y', Cur_SOLINES.SEQNO,
-                v_C_OrderLine_ID, Cur_SO.AD_CLIENT_ID, v_C_Order_ID, Cur_SOLINES.DESCRIPTION,
-                Cur_SOLINES.M_PRODUCT_ID, v_UOM, 'N', v_AD_User_ID,
-                v_AD_User_ID, 0, NULL, 0,
-                now(), Cur_SO.AD_ORG_ID, NULL, Cur_SO.C_BPARTNER_ID,
-                v_PriceActual,
-                v_Tax_ID, Cur_SO.C_BPARTNER_LOCATION_ID,
-                v_Discount, v_PriceLimit, NULL, v_PriceActual * Cur_SOLINES.Qty,
-                NULL, 'N', v_PriceStd
-              )
-              ;
-          END LOOP;
-        END IF;
-
-        IF v_Message IS NOT NULL THEN
-          v_Message:=v_Message || ', ';
-        END IF;
-        v_Message:=v_Message || v_DocumentNo;
-      END LOOP;
-      v_Message:='@DocumentNo@: ' || v_Message;
-    END;
-    --<<FINISH_PROCESS>>
-    --  Update AD_PInstance
-    DBMS_OUTPUT.PUT_LINE('Updating PInstance - Finished ' || v_Message) ;
-    AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', v_Result, v_Message) ;
-    RETURN;
-  END; --BODY
-EXCEPTION
-WHEN OTHERS THEN
-  v_ResultStr:= '@ERROR=' || SQLERRM;
-  DBMS_OUTPUT.PUT_LINE(v_ResultStr) ;
-  ROLLBACK;
-  AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', 0, v_ResultStr) ;
-  RETURN;
-END GENERATESOFROMPROJECT
-]]></body>
-    </function>
-  </database>
--- a/src-db/database/model/functions/GENERATESOFROMPROJECTORDER.xml	Thu Oct 09 14:13:04 2008 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,259 +0,0 @@
-<?xml version="1.0"?>
-  <database name="FUNCTION GENERATESOFROMPROJECTORDER">
-    <function name="GENERATESOFROMPROJECTORDER" type="NULL">
-      <parameter name="p_pinstance_id" type="VARCHAR" 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):  ______________________________________.
-************************************************************************/
-  v_ResultStr VARCHAR2(2000):='';
-  v_Message VARCHAR2(2000):='';
-  v_Result NUMBER:=1; -- 0=failure
-  v_Record_ID VARCHAR2(32);
-  v_AD_User_ID VARCHAR2(32);
-  v_M_PriceList_ID2 VARCHAR2(32);
-  TYPE RECORD IS REF CURSOR;
-    Cur_Parameter RECORD;
-    v_C_Order_ID VARCHAR2(32);
-    v_C_OrderLine_ID VARCHAR2(32);
-    v_C_DOCTYPE_ID VARCHAR2(32);
-    v_DocumentNo NUMBER;
-  BEGIN
-    --  Update AD_PInstance
-    DBMS_OUTPUT.PUT_LINE('Updating PInstance - Processing ' || p_PInstance_ID) ;
-    v_ResultStr:='PInstanceNotFound';
-    AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'Y', NULL, NULL) ;
-  BEGIN --BODY
-    -- Get PARAMETERS
-    v_ResultStr:='ReadingParameters';
-    FOR Cur_Parameter IN
-      (SELECT i.Record_ID, i.AD_User_ID, i.AD_Client_ID, i.AD_Org_ID, p.ParameterName,
-        p.P_String, p.P_Number, p.P_Date, p.P_Date_To
-      FROM AD_PINSTANCE i
-      LEFT JOIN AD_PINSTANCE_PARA p
-        ON i.AD_PInstance_ID=p.AD_PInstance_ID
-      WHERE i.AD_PInstance_ID=p_PInstance_ID
-      )
-    LOOP
-      v_Record_ID:=Cur_Parameter.Record_ID;
-      v_AD_User_ID:=Cur_Parameter.AD_User_ID;
-    END LOOP; -- Get Parameter
-    DBMS_OUTPUT.PUT_LINE('  Record_ID=' || v_Record_ID) ;
-    DECLARE
-      v_Size NUMBER;
-      v_UOM NUMBER;
-      v_PriceStd NUMBER;
-      v_PriceList NUMBER;
-      v_PriceLimit NUMBER;
-      v_C_UOM_ID VARCHAR2(32);
-      v_C_Currency_ID VARCHAR2(32);
-      v_M_PriceList_ID VARCHAR2(32);
-    TYPE RECORD IS REF CURSOR;
-      Cur_SO RECORD;
-      SOLINES RECORD;
-      FINISH_PROCESS BOOLEAN:=FALSE;
-    BEGIN
-      v_ResultStr:='StartLoop';
-      FOR Cur_SO IN
-        (SELECT * FROM C_PROJECT WHERE C_PROJECT_ID=v_Record_ID)
-      LOOP
-        -- Check that we have some restrictions
-        v_ResultStr:='CheckRestriction';
-        IF(Cur_SO.PROJECTSTATUS IS NULL OR Cur_SO.PROJECTSTATUS<>'OR') THEN
-          RAISE_APPLICATION_ERROR(-20000, '@Invalidprojectstatus@');
-        END IF;
-        IF(NOT FINISH_PROCESS) THEN
-          v_C_Order_ID:=Ad_Sequence_Nextno('C_Order') ;
-          v_ResultStr:='C_ORDER_ID - ' || v_C_Order_ID;
-          v_C_DOCTYPE_ID:=Ad_Get_Doctype(Cur_SO.AD_Client_ID, Cur_SO.AD_Org_ID, 'SOO', 'SO') ;
-          v_ResultStr:='C_DOCTYPE_ID - ' || v_C_DOCTYPE_ID;
-          Ad_Sequence_Doctype(v_C_DOCTYPE_ID, Cur_SO.AD_Client_ID, 'Y', v_DocumentNo) ;
-          IF(v_DocumentNo IS NULL) THEN
-            Ad_Sequence_Doc('DocumentNo_C_Order', Cur_SO.AD_Client_ID, 'Y', v_DocumentNo) ;
-          END IF;
-          SELECT M_PriceList_ID
-        INTO v_M_PriceList_ID2
-        FROM M_PRICELIST_VERSION
-        WHERE M_PriceList_Version_ID=Cur_SO.M_PRICELIST_VERSION_ID;
-        v_ResultStr:='DocumentNo - ' || v_DocumentNo || ' m_pricelist_id - ' || COALESCE(Cur_SO.m_pricelist_id, v_M_Pricelist_ID2);
-          INSERT
-          INTO C_ORDER
-            (
-              C_ORDER_ID, AD_CLIENT_ID, AD_ORG_ID, CREATEDBY,
-              UPDATEDBY, ISSOTRX, DOCUMENTNO, DOCSTATUS,
-              DOCACTION, C_DOCTYPE_ID, C_DOCTYPETARGET_ID, DATEORDERED,
-              DATEACCT, C_BPARTNER_ID, C_BPARTNER_LOCATION_ID, ISDISCOUNTPRINTED,
-              C_CURRENCY_ID, PAYMENTRULE, C_PAYMENTTERM_ID, INVOICERULE,
-              DELIVERYRULE, FREIGHTCOSTRULE, DELIVERYVIARULE, PRIORITYRULE,
-              TOTALLINES, GRANDTOTAL, M_WAREHOUSE_ID, M_PRICELIST_ID,
-              ISTAXINCLUDED, POSTED, PROCESSING, SALESREP_ID,
-              BILLTO_ID, C_CAMPAIGN_ID, C_PROJECT_ID, AD_USER_ID,
-              COPYFROM, DATEPROMISED
-            )
-            VALUES
-            (
-              v_C_Order_ID, Cur_SO.AD_CLIENT_ID, Cur_SO.AD_ORG_ID, v_AD_User_ID,
-              v_AD_User_ID, 'Y', v_DocumentNo, 'DR',
-               'CO', '0', v_C_DOCTYPE_ID, TRUNC(now(), 'DD'),
-              TRUNC(now(), 'DD'), Cur_SO.C_BPARTNER_ID, Cur_SO.C_BPARTNER_LOCATION_ID, 'N',
-              Cur_SO.C_CURRENCY_ID, COALESCE(Cur_SO.PAYMENTRULE, 'P'), Cur_SO.C_PAYMENTTERM_ID, 'D',
-               'A', 'I', 'D', '5',
-              0, 0, Cur_SO.M_WAREHOUSE_ID, COALESCE(Cur_SO.M_PRICELIST_ID, v_M_PriceList_ID2),
-               'N', 'N', 'N', Cur_SO.SALESREP_ID,
-              Cur_SO.BILLTO_ID, Cur_SO.C_CAMPAIGN_ID, Cur_SO.C_PROJECT_ID, Cur_SO.AD_User_ID,
-               'N', TRUNC(now(), 'DD')
-            )
-            ;
-          FOR SOLINES IN
-            (SELECT * FROM C_PROJECTLINE WHERE C_PROJECT_ID=v_Record_ID)
-          LOOP
-            IF(SOLINES.M_PRODUCT_ID IS NOT NULL AND Cur_SO.M_PRICELIST_VERSION_ID IS NOT NULL) THEN
-              v_ResultStr:='SOLINES1 - SO.M_PRICELIST_VERSION_ID - ' || Cur_SO.M_PRICELIST_VERSION_ID || ' - ' || SOLINES.M_PRODUCT_ID;
-              SELECT COUNT(p.C_UOM_ID)
-              INTO v_Size
-              FROM M_PRODUCT p,
-                M_PRODUCTPRICE pp,
-                M_PRICELIST_VERSION pv,
-                M_PRICELIST pl
-              WHERE p.M_Product_ID=pp.M_Product_ID
-                AND pp.M_PriceList_Version_ID=pv.M_PriceList_Version_ID
-                AND pv.M_PriceList_ID=pl.M_PriceList_ID
-                AND pv.IsActive='Y'
-                AND p.M_Product_ID=SOLINES.M_PRODUCT_ID
-                AND pv.M_PriceList_Version_ID=Cur_SO.M_PRICELIST_VERSION_ID;
-              IF(v_Size>0) THEN
-                SELECT Bom_Pricestd(p.M_Product_ID, pv.M_PriceList_Version_ID) AS PriceStd,
-                  Bom_Pricelist(p.M_Product_ID, pv.M_PriceList_Version_ID) AS PriceList,
-                  Bom_Pricelimit(p.M_Product_ID, pv.M_PriceList_Version_ID) AS PriceLimit,
-                  p.C_UOM_ID, pl.C_Currency_ID
-                INTO v_PriceStd, v_PriceList, v_PriceLimit, v_C_UOM_ID, v_C_Currency_ID
-                FROM M_PRODUCT p, M_PRODUCTPRICE pp,M_PRICELIST_VERSION pv,M_PRICELIST pl
-                WHERE p.M_Product_ID=pp.M_Product_ID
-                  AND pp.M_PriceList_Version_ID=pv.M_PriceList_Version_ID
-                  AND pv.M_PriceList_ID=pl.M_PriceList_ID
-                  AND pv.IsActive='Y'
-                  AND p.M_Product_ID=SOLINES.M_PRODUCT_ID
-                  AND pv.M_PriceList_Version_ID=Cur_SO.M_PRICELIST_VERSION_ID;
-              END IF;
-            END IF;
-            IF(SOLINES.M_PRODUCT_ID IS NOT NULL AND v_PriceStd IS NULL AND v_PriceList IS NULL AND v_PriceLimit IS NULL AND v_C_UOM_ID IS NULL AND v_C_Currency_ID IS NULL) THEN
-              v_ResultStr:='SOLINES - v_PriceStd - ' || v_PriceStd;
-              IF(Cur_SO.M_PRICELIST_ID IS NULL) THEN
-                SELECT M_PriceList_ID
-                INTO v_M_PriceList_ID
-                FROM M_PRICELIST pl,
-                  M_PRODUCT p
-                WHERE pl.AD_Client_ID=p.AD_Client_ID
-                  AND M_Product_ID=SOLINES.M_PRODUCT_ID
-                ORDER BY IsDefault DESC;
-              END IF;
-              v_ResultStr:='SOLINES2 - v_M_PriceList_ID - ' || v_M_PriceList_ID;
-              SELECT Bom_Pricestd(p.M_Product_ID, pv.M_PriceList_Version_ID) AS PriceStd,
-                Bom_Pricelist(p.M_Product_ID, pv.M_PriceList_Version_ID) AS PriceList,
-                Bom_Pricelimit(p.M_Product_ID, pv.M_PriceList_Version_ID) AS PriceLimit,
-                p.C_UOM_ID,pl.C_Currency_ID
-              INTO v_PriceStd,v_PriceList,v_PriceLimit,v_C_UOM_ID,v_C_Currency_ID
-              FROM M_PRODUCT p,M_PRODUCTPRICE pp,M_PRICELIST_VERSION pv,M_PRICELIST pl
-              WHERE p.M_Product_ID=pp.M_Product_ID
-                AND pp.M_PriceList_Version_ID=pv.M_PriceList_Version_ID
-                AND pv.M_PriceList_ID=pl.M_PriceList_ID
-                AND pv.IsActive='Y'
-                AND p.M_Product_ID=SOLINES.M_PRODUCT_ID
-                AND pv.M_PriceList_ID=COALESCE(Cur_SO.M_PRICELIST_ID, v_M_PriceList_ID)
-              ORDER BY pv.ValidFrom DESC;
-              v_ResultStr:='SOLINES - v_PriceStd - ' || v_PriceStd;
-            END IF;
-            IF(SOLINES.M_PRODUCT_ID IS NOT NULL AND v_PriceStd IS NULL AND v_PriceList IS NULL AND v_PriceLimit IS NULL AND v_C_UOM_ID IS NULL AND v_C_Currency_ID IS NULL) THEN
-              SELECT Bom_Pricestd(p.M_Product_ID, pv.M_PriceList_Version_ID) AS PriceStd,
-                Bom_Pricelist(p.M_Product_ID, pv.M_PriceList_Version_ID) AS PriceList,
-                Bom_Pricelimit(p.M_Product_ID, pv.M_PriceList_Version_ID) AS PriceLimit,
-                p.C_UOM_ID,pl.C_Currency_ID
-              INTO v_PriceStd,v_PriceList,v_PriceLimit,v_C_UOM_ID,v_C_Currency_ID
-              FROM M_PRODUCT p,M_PRODUCTPRICE pp,M_PRICELIST_VERSION pv,M_PRICELIST bpl,M_PRICELIST pl
-              WHERE p.M_Product_ID=pp.M_Product_ID
-                AND pp.M_PriceList_Version_ID=pv.M_PriceList_Version_ID
-                AND pv.M_PriceList_ID=bpl.M_PriceList_ID
-                AND bpl.M_PriceList_ID=pl.BasePriceList_ID
-                AND pv.IsActive='Y'
-                AND p.M_Product_ID=SOLINES.M_PRODUCT_ID
-                AND pl.M_PriceList_ID=COALESCE(Cur_SO.M_PRICELIST_ID, v_M_PriceList_ID)
-              ORDER BY pv.ValidFrom DESC;
-            END IF;
-            SELECT U.C_UOM_ID
-            INTO v_UOM
-            FROM C_UOM U,M_PRODUCT P
-            WHERE P.C_UOM_ID=U.C_UOM_ID
-              AND M_PRODUCT_ID=SOLINES.M_PRODUCT_ID;
-            v_C_OrderLine_ID:=Ad_Sequence_Nextno('C_OrderLine') ;
-            v_ResultStr:='C_OrderLine_ID - ' || v_C_OrderLine_ID;
-            INSERT
-            INTO C_ORDERLINE
-              (
-                DateOrdered, M_Warehouse_ID, QtyOrdered, QtyDelivered,
-                QtyReserved, M_Shipper_ID, QtyInvoiced,
-                C_Currency_ID, PriceList, DatePromised, DateDelivered,
-                DateInvoiced, Created, IsActive, Line,
-                C_OrderLine_ID, AD_Client_ID, C_Order_ID, Description,
-                M_Product_ID, C_UOM_ID, DirectShip, CreatedBy,
-                UpdatedBy, FreightAmt, C_Charge_ID, ChargeAmt,
-                Updated, AD_Org_ID, S_ResourceAssignment_ID, C_BPartner_ID,
-                PriceActual, C_Tax_ID, C_BPartner_Location_ID,
-                Discount, PriceLimit, Ref_OrderLine_ID, LineNetAmt,
-                M_AttributeSetInstance_ID, IsDescription, PriceStd
-              )
-              VALUES
-              (
-                TRUNC(now(), 'DD'), Cur_SO.M_WAREHOUSE_ID, SOLINES.InvoicedQty, 0,
-                0, NULL, 0,
-                Cur_SO.C_CURRENCY_ID, v_PriceList, TRUNC(now(), 'DD'), NULL,
-                NULL, now(), 'Y', SOLINES.LINE,
-                v_C_OrderLine_ID, Cur_SO.AD_CLIENT_ID, v_C_Order_ID, SOLINES.DESCRIPTION,
-                SOLINES.M_PRODUCT_ID, v_UOM, 'N', v_AD_User_ID,
-                v_AD_User_ID, 0, NULL, 0,
-                now(), Cur_SO.AD_ORG_ID, NULL, Cur_SO.C_BPARTNER_ID,
-                M_Get_Offers_Price(now(), Cur_SO.C_BPARTNER_ID, SOLINES.M_PRODUCT_ID, v_PriceStd, SOLINES.InvoicedQty,  v_M_PriceList_ID),
-    C_Gettax(SOLINES.M_PRODUCT_ID, TRUNC(now(), 'DD'), Cur_SO.AD_ORG_ID, Cur_SO.M_WAREHOUSE_ID, Cur_SO.C_BPARTNER_LOCATION_ID, Cur_SO.C_BPARTNER_LOCATION_ID, Cur_SO.C_PROJECT_ID, 'Y'), Cur_SO.C_BPARTNER_LOCATION_ID,
-                (v_PriceList-v_PriceStd) *100/v_PriceList, v_PriceLimit, NULL, (M_Get_Offers_Price(now(), Cur_SO.C_BPARTNER_ID, SOLINES.M_PRODUCT_ID, v_PriceStd, SOLINES.InvoicedQty,  v_M_PriceList_ID) * SOLINES.InvoicedQty),
-                NULL, 'N', v_PriceStd
-              )
-              ;
-          END LOOP;
-          IF v_Message IS NOT NULL THEN
-            v_Message:=v_Message || ', ';
-          END IF;
-          v_Message:=v_Message || v_DocumentNo;
-        END IF;--FINISH_PROCESS
-      END LOOP;
-      v_Message:='@DocumentNo@: ' || v_Message;
-    END;
-    --<<FINISH_PROCESS>>
-    --  Update AD_PInstance
-    DBMS_OUTPUT.PUT_LINE('Updating PInstance - Finished ' || v_Message) ;
-    AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', v_Result, v_Message) ;
-    RETURN;
-  END; --BODY
-EXCEPTION
-WHEN OTHERS THEN
-  v_ResultStr:= '@ERROR=' || SQLERRM;
-  DBMS_OUTPUT.PUT_LINE(v_ResultStr) ;
-  ROLLBACK;
-  AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', 0, v_ResultStr) ;
-  RETURN;
-END GENERATESOFROMPROJECTORDER
-]]></body>
-    </function>
-  </database>
--- a/src-db/database/model/functions/GENERATESOFROMPROJECTPHASE.xml	Thu Oct 09 14:13:04 2008 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,323 +0,0 @@
-<?xml version="1.0"?>
-  <database name="FUNCTION GENERATESOFROMPROJECTPHASE">
-    <function name="GENERATESOFROMPROJECTPHASE" type="NULL">
-      <parameter name="p_pinstance_id" type="VARCHAR" 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-2008 Openbravo SL
-* All Rights Reserved.
-* Contributor(s):  ______________________________________.
-************************************************************************/
-  v_ResultStr VARCHAR2(2000):='';
-  v_Message VARCHAR2(2000):='';
-  v_Result NUMBER:=1; -- 0=failure
-  v_Record_ID VARCHAR2(32);
-  v_AD_User_ID VARCHAR2(32);
-  TYPE RECORD IS REF CURSOR;
-    Cur_Parameter RECORD;
-    v_C_Order_ID VARCHAR2(32);
-    v_C_OrderLine_ID VARCHAR2(32);
-    v_C_DOCTYPE_ID VARCHAR2(32);
-    v_DocumentNo NVARCHAR2(30) ;
-  BEGIN
-    --  Update AD_PInstance
-    DBMS_OUTPUT.PUT_LINE('Updating PInstance - Processing ' || p_PInstance_ID) ;
-    v_ResultStr:='PInstanceNotFound';
-    AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'Y', NULL, NULL) ;
-  BEGIN --BODY
-    -- Get PARAMETERS
-    v_ResultStr:='ReadingParameters';
-    FOR Cur_Parameter IN
-      (SELECT i.Record_ID, i.AD_User_ID, i.AD_Client_ID, i.AD_Org_ID, p.ParameterName,
-        p.P_String, p.P_Number, p.P_Date, p.P_Date_To
-      FROM AD_PINSTANCE i
-      LEFT JOIN AD_PINSTANCE_PARA p ON i.AD_PInstance_ID=p.AD_PInstance_ID
-      WHERE i.AD_PInstance_ID=p_PInstance_ID
-      ORDER BY p.SeqNo
-      )
-    LOOP
-      v_Record_ID:=Cur_Parameter.Record_ID;
-      v_AD_User_ID:=Cur_Parameter.AD_User_ID;
-    END LOOP; -- Get Parameter
-    DBMS_OUTPUT.PUT_LINE('  Record_ID=' || v_Record_ID) ;
-    DECLARE
-      v_Size NUMBER;
-      v_UOM NUMBER;
-      v_PriceStd NUMBER;
-      v_PriceList NUMBER;
-      v_PriceLimit NUMBER;
-      v_C_UOM_ID VARCHAR2(32);
-      v_C_Currency_ID VARCHAR2(32);
-      v_M_PriceList_ID VARCHAR2(32);
-      v_Billto_ID VARCHAR2(32);
-      v_InvoiceRule CHAR;
-      v_M_PriceList_Version_ID VARCHAR2(32);
-      v_PriceActual NUMBER;
-      v_Discount NUMBER;
-
-    TYPE RECORD IS REF CURSOR;
-      Cur_SO RECORD;
-      Cur_SOLINES RECORD;
-    BEGIN
-      v_ResultStr:='StartLoop';
-      FOR Cur_SO IN
-        (
-        SELECT P.C_BPARTNER_ID, P.C_BPARTNER_LOCATION_ID, P.M_PRICELIST_ID, PL.NAME AS PRICE_LIST_NAME,
-          P.C_CURRENCY_ID, P.PAYMENTRULE, P.C_PAYMENTTERM_ID, P.M_WAREHOUSE_ID, P.SALESREP_ID,
-          P.BILLTO_ID, P.C_CAMPAIGN_ID, P.C_PROJECT_ID, P.AD_USER_ID, P.DESCRIPTION, P.PROJECTSTATUS,
-          PP.NAME AS PHASE_NAME, PP.AD_CLIENT_ID, PP.AD_ORG_ID
-        FROM C_PROJECT P,
-          C_PROJECTPHASE PP,
-          M_PRICELIST PL
-        WHERE P.C_PROJECT_ID=PP.C_PROJECT_ID
-          AND PP.C_PROJECTPHASE_ID=v_Record_ID
-          AND P.M_PRICELIST_ID = PL.M_PRICELIST_ID
-        )
-      LOOP
-        -- Check that we have some restrictions
-        IF (Cur_SO.PROJECTSTATUS IS NULL OR Cur_SO.PROJECTSTATUS <> 'OR') THEN
-          RAISE_APPLICATION_ERROR(-20000, '@Invalidprojectstatus@'||'. '||' @ChangeToOrder@'||'.');
-        ELSIF Cur_SO.C_BPARTNER_ID IS NULL THEN
-          RAISE_APPLICATION_ERROR(-20000, '@NoprojectBusinesspartner@');
-        ELSIF Cur_SO.C_BPARTNER_LOCATION_ID IS NULL THEN
-          RAISE_APPLICATION_ERROR(-20000, '@ThebusinessPartner@'||' '||' @ShiptoNotdefined@'||'.');
-        ELSIF Cur_SO.BILLTO_ID IS NULL THEN
-          RAISE_APPLICATION_ERROR(-20000, '@ThebusinessPartner@'||' '||' @BillToNotdefined@'||'.');
-        ELSIF Cur_SO.C_PAYMENTTERM_ID IS NULL THEN
-          RAISE_APPLICATION_ERROR(-20000, '@ThebusinessPartner@'||' '||' @PaymenttermNotdefined@'||'.');
-        ELSIF Cur_SO.M_PRICELIST_ID IS NULL THEN
-          RAISE_APPLICATION_ERROR(-20000, '@ThebusinessPartner@'||' '||' @PricelistNotdefined@'||'.');
-        ELSIF Cur_SO.C_CURRENCY_ID IS NULL THEN
-          RAISE_APPLICATION_ERROR(-20000, '@ProjectCurrencyNotFound@'||'.');
-        ELSIF Cur_SO.M_WAREHOUSE_ID IS NULL THEN
-          RAISE_APPLICATION_ERROR(-20000, '@ProjectWarehouseNotFound@'||'.');
-        ELSIF Cur_SO.SALESREP_ID IS NULL THEN
-          RAISE_APPLICATION_ERROR(-20000, '@ProjectSalesRepNotFound@'||'.');
-        END IF;
-
-        -- Get Business Partner Location
-        v_Billto_ID := Cur_SO.BILLTO_ID;
-        IF (v_Billto_ID IS NULL) THEN
-          v_Billto_ID := C_GetBPLocationID(Cur_SO.C_BPartner_ID, 'B') ;
-        END IF;
-
-        SELECT InvoiceRule INTO v_InvoiceRule
-        FROM C_BPARTNER
-        WHERE C_BPartner_ID = Cur_SO.C_BPARTNER_ID;
-        IF (v_InvoiceRule IS NULL) THEN
-           RAISE_APPLICATION_ERROR(-20000, '@ThebusinessPartner@'||' '||' @InvoiceRuleNotdefined@'||'.');
-        END IF;
-
-        v_C_DOCTYPE_ID:=Ad_Get_Doctype(Cur_SO.AD_Client_ID, Cur_SO.AD_Org_ID, 'SOO', 'SO') ;
-        v_ResultStr:='C_DOCTYPE_ID - ' || v_C_DOCTYPE_ID;
-        Ad_Sequence_Doctype(v_C_DOCTYPE_ID, Cur_SO.AD_Client_ID, 'Y', v_DocumentNo) ;
-        IF(v_DocumentNo IS NULL) THEN
-          Ad_Sequence_Doc('DocumentNo_C_Order', Cur_SO.AD_Client_ID, 'Y', v_DocumentNo) ;
-        END IF;
-
-        -- Get next C_Order_ID
-        Ad_Sequence_Next('C_Order', Cur_SO.AD_CLIENT_ID, v_C_Order_ID) ;
-        v_ResultStr:='C_ORDER_ID - ' || v_C_Order_ID;
-
-        v_ResultStr:='DocumentNo - ' || v_DocumentNo || ' m_pricelist_id - ' || Cur_SO.M_PriceList_ID;
-        INSERT
-        INTO C_ORDER
-          (
-            C_ORDER_ID, AD_CLIENT_ID, AD_ORG_ID, CREATEDBY,
-            UPDATEDBY, ISSOTRX, DOCUMENTNO, DOCSTATUS,
-            DOCACTION, C_DOCTYPE_ID, C_DOCTYPETARGET_ID, DATEORDERED,
-            DATEACCT, C_BPARTNER_ID, C_BPARTNER_LOCATION_ID, ISDISCOUNTPRINTED,
-            C_CURRENCY_ID, PAYMENTRULE, C_PAYMENTTERM_ID, INVOICERULE,
-            DELIVERYRULE, FREIGHTCOSTRULE, DELIVERYVIARULE, PRIORITYRULE,
-            TOTALLINES, GRANDTOTAL, M_WAREHOUSE_ID, M_PRICELIST_ID,
-            ISTAXINCLUDED, POSTED, PROCESSING, SALESREP_ID,
-            BILLTO_ID, C_CAMPAIGN_ID, C_PROJECT_ID, AD_USER_ID,
-            COPYFROM, DATEPROMISED, DESCRIPTION
-          )
-          VALUES
-          (
-            v_C_Order_ID, Cur_SO.AD_CLIENT_ID, Cur_SO.AD_ORG_ID, v_AD_User_ID,
-            v_AD_User_ID, 'Y', v_DocumentNo, 'DR',
-             'CO', '0', v_C_DOCTYPE_ID, TRUNC(now(), 'DD'),
-            TRUNC(now(), 'DD'), Cur_SO.C_BPARTNER_ID, Cur_SO.C_BPARTNER_LOCATION_ID, 'N',
-            Cur_SO.C_CURRENCY_ID, COALESCE(Cur_SO.PAYMENTRULE, 'P'), Cur_SO.C_PAYMENTTERM_ID, v_InvoiceRule,
-             'A', 'I', 'D', '5',
-            0, 0, Cur_SO.M_WAREHOUSE_ID, Cur_SO.M_PRICELIST_ID,
-             'N', 'N', 'N', Cur_SO.SALESREP_ID,
-            v_Billto_ID, Cur_SO.C_CAMPAIGN_ID, Cur_SO.C_PROJECT_ID, Cur_SO.AD_User_ID,
-             'N', TRUNC(now(), 'DD'), Cur_SO.PHASE_NAME ||' - '|| Cur_SO.DESCRIPTION
-          );
-
-        UPDATE C_PROJECTPHASE
-        SET C_ORDER_ID = v_C_Order_ID
-        WHERE C_PROJECTPHASE_ID = v_Record_ID;
-
-        -- Select the price list version that a applies for the price list of the header
-        SELECT Get_Pricelist_Version(Cur_SO.M_PriceList_ID, TRUNC(now(), 'DD'))
-        INTO v_M_PriceList_Version_ID
-        FROM DUAL;
-        IF (v_M_PriceList_Version_ID IS NULL) THEN
-          RAISE_APPLICATION_ERROR(-20000, '@PriceListVersionNotFound@'||'.');
-        ELSE
-         -- Select products, quantities, sequence numbers, descriptions and unit prices of the project phases and tasks
-            -- In both cases, if no unit price has been defined for a product,
-            -- price is taken from the price list of the project.
-            -- And if no price is defined in the price list, price is set to 0.
-          FOR Cur_SOLINES IN
-            (
-            SELECT 'phase' AS PROV, pp.SEQNO, pp.NAME AS LINE_NAME, pp.DESCRIPTION, pr.NAME AS PRODUCT_NAME, pp.M_PRODUCT_ID, pp.QTY, pp.PRICEACTUAL
-            FROM C_PROJECTPHASE pp, M_PRODUCT pr
-            WHERE pp.C_PROJECTPHASE_ID = v_Record_ID
-              AND pp.M_PRODUCT_ID = pr.M_PRODUCT_ID
-              AND pp.M_Product_ID IS NOT NULL
-              AND pp.IsActive = 'Y'
-            UNION
-            SELECT 'task' as PROV, pt.SEQNO, pt.NAME AS LINE_NAME, pt.DESCRIPTION, pr.NAME AS PRODUCT_NAME, pt.M_PRODUCT_ID, pt.QTY, pt.PRICEACTUAL
-            FROM C_PROJECTTASK pt, C_PROJECTPHASE pp, M_PRODUCT pr
-            WHERE pp.C_PROJECTPHASE_ID = v_Record_ID
-              AND pp.C_PROJECTPHASE_ID = pt.C_PROJECTPHASE_ID
-              AND pt.M_PRODUCT_ID = pr.M_PRODUCT_ID
-              AND pt.M_Product_ID IS NOT NULL
-              AND pt.IsActive = 'Y'
-            ORDER BY PROV ASC, SEQNO ASC
-            )
-          LOOP
-
-            -- Take StdPrice, PriceList and PriceLimit from Price List Version
-            SELECT COUNT(*)
-            INTO v_Size
-            FROM M_PRODUCT p,M_PRODUCTPRICE pp,M_PRICELIST_VERSION pv,M_PRICELIST pl
-            WHERE p.M_Product_ID=pp.M_Product_ID
-              AND pp.M_PriceList_Version_ID=pv.M_PriceList_Version_ID
-              AND pv.M_PriceList_ID=pl.M_PriceList_ID
-              AND pv.IsActive='Y'
-              AND p.M_Product_ID=Cur_SOLINES.M_PRODUCT_ID
-              AND pv.M_PriceList_Version_ID=v_M_PriceList_Version_ID
-              AND pl.C_Currency_ID=Cur_SO.C_CURRENCY_ID;
-            IF (v_Size>0) THEN
-              SELECT Bom_Pricestd(p.M_Product_ID, pv.M_PriceList_Version_ID) AS PriceStd,
-                Bom_Pricelist(p.M_Product_ID, pv.M_PriceList_Version_ID) AS PriceList,
-                Bom_Pricelimit(p.M_Product_ID, pv.M_PriceList_Version_ID) AS PriceLimit,
-                p.C_UOM_ID
-              INTO v_PriceStd,v_PriceList,
-                v_PriceLimit,v_C_UOM_ID
-              FROM M_PRODUCT p,M_PRODUCTPRICE pp,M_PRICELIST_VERSION pv,M_PRICELIST pl
-              WHERE p.M_Product_ID=pp.M_Product_ID
-                AND pp.M_PriceList_Version_ID=pv.M_PriceList_Version_ID
-                AND pv.M_PriceList_ID=pl.M_PriceList_ID
-                AND pv.IsActive='Y'
-                AND p.M_Product_ID=Cur_SOLINES.M_PRODUCT_ID
-                AND pv.M_PriceList_Version_ID=v_M_PriceList_Version_ID
-                AND pl.C_Currency_ID=Cur_SO.C_CURRENCY_ID;
-            ELSE
-              v_PriceStd := NULL;
-              v_PriceList := NULL;
-              v_PriceLimit := NULL;
-              v_C_UOM_ID := NULL;
-            END IF;
-
-            -- Calculate Price Actual
-            v_PriceActual := COALESCE(Cur_SOLINES.priceactual,M_Get_Offers_Price(now(), Cur_SO.C_BPARTNER_ID, Cur_SOLINES.M_PRODUCT_ID, v_PriceStd, Cur_SOLINES.Qty, Cur_SO.M_PRICELIST_ID),0);
-
-            IF (v_PriceStd IS NULL) THEN
-             v_PriceStd :=  v_PriceActual ;
-            END IF;
-
-            IF (v_PriceList IS NULL) THEN
-              v_PriceList :=  v_PriceActual ;
-            END IF;
-
-             IF (v_PriceLimit IS NULL) THEN
-              v_PriceLimit :=  v_PriceActual ;
-            END IF;
-
-            IF (v_PriceList = 0) THEN
-               v_Discount := 0 ;
-            ELSE
-              -- Calculate rounded discount
-              v_Discount :=ROUND((v_PriceList-v_PriceActual) / v_PriceList*100, 2);
-            END IF;
-
-            IF (v_C_UOM_ID IS NULL) THEN
-              SELECT P.C_UOM_ID
-              INTO v_UOM
-              FROM M_PRODUCT P
-              WHERE P.M_PRODUCT_ID=Cur_SOLINES.M_PRODUCT_ID;
-            ELSE
-              v_UOM := v_C_UOM_ID;
-            END IF;
-
-            -- Get next C_OrderLine_ID
-            Ad_Sequence_Next('C_OrderLine', Cur_SO.AD_CLIENT_ID, v_C_OrderLine_ID) ;
-            v_ResultStr:='C_OrderLine_ID - ' || v_C_OrderLine_ID;
-
-            INSERT
-            INTO C_ORDERLINE
-              (
-                DateOrdered, M_Warehouse_ID, QtyOrdered, QtyDelivered,
-                QtyReserved, M_Shipper_ID, QtyInvoiced,
-                C_Currency_ID, PriceList, DatePromised, DateDelivered,
-                DateInvoiced, Created, IsActive, Line,
-                C_OrderLine_ID, AD_Client_ID, C_Order_ID, Description,
-                M_Product_ID, C_UOM_ID, DirectShip, CreatedBy,
-                UpdatedBy, FreightAmt, C_Charge_ID, ChargeAmt,
-                Updated, AD_Org_ID, S_ResourceAssignment_ID, C_BPartner_ID,
-                PriceActual,
-                C_Tax_ID, C_BPartner_Location_ID,
-                Discount, PriceLimit, Ref_OrderLine_ID, LineNetAmt,
-                M_AttributeSetInstance_ID, IsDescription, PriceStd
-              )
-              VALUES
-              (
-                TRUNC(now(), 'DD'), Cur_SO.M_WAREHOUSE_ID, Cur_SOLINES.Qty, 0,
-                0, NULL, 0,
-                Cur_SO.C_CURRENCY_ID, v_PriceList, TRUNC(now(), 'DD'), NULL,
-                NULL, now(), 'Y', Cur_SOLINES.SEQNO,
-                v_C_OrderLine_ID, Cur_SO.AD_CLIENT_ID, v_C_Order_ID, Cur_SOLINES.LINE_NAME || ' - ' || Cur_SOLINES.DESCRIPTION,
-                Cur_SOLINES.M_PRODUCT_ID, v_UOM, 'N', v_AD_User_ID,
-                v_AD_User_ID, 0, NULL, 0,
-                now(), Cur_SO.AD_ORG_ID, NULL, Cur_SO.C_BPARTNER_ID,
-                v_PriceActual,
-                C_Gettax(Cur_SOLINES.M_PRODUCT_ID, TRUNC(now(), 'DD'), Cur_SO.AD_ORG_ID, Cur_SO.M_WAREHOUSE_ID, Cur_SO.C_BPARTNER_LOCATION_ID, Cur_SO.C_BPARTNER_LOCATION_ID, Cur_SO.C_PROJECT_ID, 'Y'), Cur_SO.C_BPARTNER_LOCATION_ID,
-                v_Discount, v_PriceLimit, NULL, v_PriceActual * Cur_SOLINES.Qty,
-                NULL, 'N', v_PriceStd
-              )
-              ;
-          END LOOP;
-        END IF;
-        IF v_Message IS NOT NULL THEN
-          v_Message:=v_Message || ', ';
-        END IF;
-        v_Message:=v_Message || v_DocumentNo;
-      END LOOP;
-      v_Message:='@DocumentNo@: ' || v_Message;
-    END;
-    --<<FINISH_PROCESS>>
-    --  Update AD_PInstance
-    DBMS_OUTPUT.PUT_LINE('Updating PInstance - Finished ' || v_Message) ;
-    AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', v_Result, v_Message) ;
-    RETURN;
-  END; --BODY
-EXCEPTION
-WHEN OTHERS THEN
-  v_ResultStr:= '@ERROR=' || SQLERRM;
-  DBMS_OUTPUT.PUT_LINE(v_ResultStr) ;
-  ROLLBACK;
-  AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', 0, v_ResultStr) ;
-  RETURN;
-END GENERATESOFROMPROJECTPHASE
-]]></body>
-    </function>
-  </database>
--- a/src-db/database/model/functions/GENERATE_AVERAGE_COSTS.xml	Thu Oct 09 14:13:04 2008 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,226 +0,0 @@
-<?xml version="1.0"?>
-  <database name="FUNCTION GENERATE_AVERAGE_COSTS">
-    <function name="GENERATE_AVERAGE_COSTS" type="NULL">
-      <parameter name="p_pinstance_id" type="VARCHAR" 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-2008 Openbravo SL
-* All Rights Reserved.
-* Contributor(s):  ______________________________________.
-************************************************************************/
-  -- Logistice
-  v_ResultStr VARCHAR2(2000):='';
-  v_Result NUMBER;
-  v_Message VARCHAR2(2000):='';
-  v_Record_ID VARCHAR2(32);
-  v_Date DATE;
-  v_Qty NUMBER;
-  v_Cost NUMBER;
-  v_User VARCHAR2(32);
-  v_Price NUMBER;
-  v_DateOld DATE;
-  v_CumQty NUMBER;
-  v_Processed CHAR(1) ;
-  v_count NUMBER;
-  v_Deleted NUMBER;
-  v_ProductOld VARCHAR2(32);
-  -- Parameter
-  TYPE RECORD IS REF CURSOR;
-    Cur_Parameter RECORD;
-    Cur_InOutLine RECORD;
-    Cur_ProdCost RECORD;
-    Cur_PriceList RECORD;
-    v_rowcount NUMBER;
-  BEGIN
-    v_Qty:=0;
-    v_Cost:=0;
-    v_Price:=0;
-    v_CumQty:=0;
-    v_ProductOld:='0';
-    --  Update AD_PInstance
-    DBMS_OUTPUT.PUT_LINE('Updating PInstance - Processing ' || p_PInstance_ID) ;
-    v_ResultStr:='PInstanceNotFound';
-    AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'Y', NULL, NULL) ;
-  BEGIN --BODY
-    --  Get Parameters
-    v_ResultStr:='ReadingParameters';
-    FOR Cur_Parameter IN
-      (SELECT i.Record_ID,
-        i.AD_User_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=p_PInstance_ID
-      ORDER BY p.SeqNo
-      )
-    LOOP
-      v_Record_ID:=Cur_Parameter.Record_ID;
-      v_User:=Cur_Parameter.AD_User_ID;
-    END LOOP; --  Get Parameter
-    DELETE
-    FROM M_COSTING
-    WHERE ISPERMANENT='N'
-      AND ISMANUAL='N'
-      AND COSTTYPE='AV'
-      AND ISPRODUCTION = 'N';
-    v_rowcount:=SQL%ROWCOUNT;
-    v_Deleted:=v_Deleted + v_rowcount;
-    v_rowcount:=SQL%ROWCOUNT;
-    DBMS_OUTPUT.PUT_LINE('  deleted=' || v_rowcount) ;
-    v_DateOld:=TO_DATE('01-01-1900', 'DD-MM-YYYY') ;
-    FOR Cur_InOutLine IN
-      (SELECT M_INOUTLINE.M_INOUTLINE_ID, M_INOUTLINE.MOVEMENTQTY, M_PRODUCT.M_PRODUCT_ID, M_INOUT.MOVEMENTDATE,
-        M_INOUTLINE.AD_CLIENT_ID, M_INOUTLINE.AD_ORG_ID, M_INOUT.C_BPARTNER_ID
-      FROM M_INOUT, M_INOUTLINE, M_PRODUCT
-      WHERE M_INOUT.M_INOUT_ID=M_INOUTLINE.M_INOUT_ID
-        AND M_INOUTLINE.M_PRODUCT_ID=M_PRODUCT.M_PRODUCT_ID
-        AND M_PRODUCT.COSTTYPE='AV'
-        AND M_INOUT.ISSOTRX='N'
-        AND M_INOUT.PROCESSED='Y'
-        AND M_INOUTLINE.MOVEMENTQTY>0
-        AND M_INOUT.DOCSTATUS <> 'DR' AND M_INOUT.DOCSTATUS <> 'RE'
-        AND NOT EXISTS
-        (SELECT 1 FROM M_COSTING WHERE M_INOUTLINE_ID=M_INOUTLINE.M_INOUTLINE_ID)
-      ORDER BY M_PRODUCT.M_PRODUCT_ID, M_INOUT.MOVEMENTDATE, M_INOUTLINE.MOVEMENTQTY DESC
-      )
-    LOOP
-      v_ResultStr:='new albaran:   '||Cur_InOutLine.M_INOUTLINE_ID;
-      IF(Cur_InOutLine.MOVEMENTDATE<>v_DateOld OR Cur_InOutLine.M_PRODUCT_ID<>v_ProductOld) THEN
-        v_CumQty:=0;
-      END IF;
-      SELECT COUNT(*)
-      INTO v_Count
-      FROM M_COSTING
-      WHERE M_PRODUCT_ID=Cur_InOutLine.M_PRODUCT_ID;
-      IF(v_Count>0) THEN
-        FOR Cur_ProdCost IN
-          (SELECT COST, DATETO, M_COSTING_ID
-          FROM M_COSTING
-          WHERE M_PRODUCT_ID=Cur_InOutLine.M_PRODUCT_ID
-            AND DATEFROM <= Cur_InOutLine.MOVEMENTDATE
-          ORDER BY DATETO DESC, M_COSTING_ID
-          )
-        LOOP
-          v_Cost:=Cur_ProdCost.COST;
-          v_Date:=Cur_ProdCost.DATETO;
-          UPDATE M_COSTING
-          SET DATETO = Cur_InOutLine.MOVEMENTDATE
-          WHERE M_COSTING_ID = Cur_ProdCost.M_COSTING_ID;
-          EXIT;
-        END LOOP;
-      ELSE
-        v_Cost:=NULL;
-        v_Date := TO_DATE('31-12-9999', 'DD-MM-YYYY');
-      END IF;
-      IF(v_Cost IS NOT NULL) THEN
-        SELECT COALESCE(SUM(MOVEMENTQTY), 0)
-        INTO v_Qty
-        FROM M_TRANSACTION
-        WHERE M_PRODUCT_ID=Cur_InOutLine.M_PRODUCT_ID
-          AND MOVEMENTDATE<Cur_InOutLine.MOVEMENTDATE;
-      ELSE
-        v_Qty:=0;
-      END IF;
-      v_ResultStr:='invoice albaran:   '||Cur_InOutLine.M_INOUTLINE_ID;
-      SELECT CASE WHEN SUM(QTYINVOICED) <=0 THEN 0 ELSE SUM(C_INVOICELINE.QTYINVOICED*C_INVOICELINE.PRICEACTUAL) /SUM(QTYINVOICED) END,
-             CASE WHEN SUM(QTYINVOICED) <=0 THEN 0 ELSE COUNT(C_INVOICELINE.C_INVOICELINE_ID) END
-      INTO v_Price, v_Count
-      FROM M_MATCHINV, C_INVOICELINE
-      WHERE M_MATCHINV.C_INVOICELINE_ID=C_INVOICELINE.C_INVOICELINE_ID
-        AND M_MATCHINV.M_INOUTLINE_ID=Cur_InOutLine.M_INOUTLINE_ID
-        AND QTYINVOICED<>0;
-      IF(v_Count=0) THEN
-        v_ResultStr:='order albaran:   '||Cur_InOutLine.M_INOUTLINE_ID;
-        SELECT CASE WHEN SUM(C_ORDERLINE.QTYORDERED) <=0 THEN 0 ELSE SUM(C_ORDERLINE.QTYORDERED*C_ORDERLINE.PRICEACTUAL) /SUM(C_ORDERLINE.QTYORDERED) END,
-               CASE WHEN SUM(C_ORDERLINE.QTYORDERED) <=0 THEN 0 ELSE COUNT(C_ORDERLINE.C_ORDERLINE_ID) END
-        INTO v_Price, v_Count
-        FROM M_MATCHPO, C_ORDERLINE
-        WHERE M_MATCHPO.C_ORDERLINE_ID=C_ORDERLINE.C_ORDERLINE_ID
-          AND M_MATCHPO.C_INVOICELINE_ID IS NULL
-          AND M_MATCHPO.M_INOUTLINE_ID=Cur_InOutLine.M_INOUTLINE_ID
-          AND C_ORDERLINE.QTYORDERED<>0;
-
-      END IF;
-      IF(v_Count=0) THEN
-        FOR Cur_PriceList IN
-          (SELECT M_PRODUCTPRICE.PRICESTD
-          FROM M_PRICELIST_VERSION, M_PRODUCTPRICE
-          WHERE M_PRICELIST_VERSION.M_PRICELIST_ID=
-            (SELECT po_pricelist_id
-            FROM C_BPARTNER
-            WHERE c_bpartner_id=Cur_InOutLine.C_BPARTNER_ID
-            )
-            AND M_PRICELIST_VERSION.M_PRICELIST_VERSION_ID=M_PRODUCTPRICE.M_PRICELIST_VERSION_ID
-            AND M_PRICELIST_VERSION.ISACTIVE='Y'
-            AND M_PRODUCTPRICE.M_PRODUCT_ID=Cur_InOutLine.M_PRODUCT_ID
-            AND M_PRICELIST_VERSION.VALIDFROM<Cur_InOutLine.MOVEMENTDATE
-          ORDER BY M_PRICELIST_VERSION.VALIDFROM DESC
-          )
-        LOOP
-          v_Price:=Cur_PriceList.PRICESTD;
-          EXIT;
-        END LOOP;
-      END IF;
-      v_ResultStr:='insert costing:   '||Cur_InOutLine.M_INOUTLINE_ID;
-      INSERT
-      INTO M_COSTING
-        (
-          M_COSTING_ID, CREATED, CREATEDBY, UPDATED,
-          UPDATEDBY, AD_CLIENT_ID, AD_ORG_ID, M_PRODUCT_ID,
-          DATETO, DATEFROM, ISMANUAL, M_INOUTLINE_ID,
-          C_INVOICELINE_ID, QTY, PRICE, CUMQTY,
-          COST, COSTTYPE, ISPERMANENT
-        )
-        VALUES
-        (
-          Ad_Sequence_Nextno('M_Costing'), now(), v_User, now(),
-          v_User, Cur_InOutLine.AD_CLIENT_ID, Cur_InOutLine.AD_ORG_ID, Cur_InOutLine.M_PRODUCT_ID,
-          v_Date, Cur_InOutLine.MOVEMENTDATE, 'N', Cur_InOutLine.M_INOUTLINE_ID,
-          NULL, Cur_InOutLine.MOVEMENTQTY, v_Price, COALESCE(v_Qty, 0) + Cur_InOutLine.MOVEMENTQTY + v_CumQty,(
-          CASE
-            WHEN v_Qty+v_CumQty<0 THEN v_Price
-            WHEN (v_Qty+Cur_InOutLine.MOVEMENTQTY+v_CumQty) = 0 THEN 0
-            ELSE (((v_Qty+v_CumQty)*COALESCE(v_Cost, v_Price)) +(Cur_InOutLine.MOVEMENTQTY *v_Price)) /(v_Qty+Cur_InOutLine.MOVEMENTQTY+v_CumQty) END),
-          'AV', 'N'
-        )
-        ;
-      IF(Cur_InOutLine.MOVEMENTDATE=v_DateOld AND Cur_InOutLine.M_PRODUCT_ID=v_ProductOld) THEN
-        v_CumQty:=v_CumQty + Cur_InOutLine.MOVEMENTQTY;
-      END IF;
-      v_DateOld:=Cur_InOutLine.MOVEMENTDATE;
-      v_ProductOld:=Cur_InOutLine.M_PRODUCT_ID;
-    END LOOP;
-    --<<FINISH_PROCESS>>
-    --  Update AD_PInstance
-    DBMS_OUTPUT.PUT_LINE('Updating PInstance - Finished ' || v_Message);
-    AD_UPDATE_PINSTANCE(p_PInstance_ID, v_User, 'N', 1, v_Message);
-    RETURN;
-  END; --BODY
-EXCEPTION
-WHEN OTHERS THEN
-  ROLLBACK;
-  DBMS_OUTPUT.PUT_LINE(v_ResultStr);
-  v_ResultStr:= '@ERROR=' || SQLERRM;
-  DBMS_OUTPUT.PUT_LINE(v_ResultStr);
-  AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', 0, v_ResultStr);
-  RETURN;
-END GENERATE_AVERAGE_COSTS
-]]></body>
-    </function>
-  </database>
--- a/src-db/database/model/functions/GENERATE_STANDAR_COSTS.xml	Thu Oct 09 14:13:04 2008 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,134 +0,0 @@
-<?xml version="1.0"?>
-  <database name="FUNCTION GENERATE_STANDAR_COSTS">
-    <function name="GENERATE_STANDAR_COSTS" type="NULL">
-      <parameter name="p_pinstance_id" type="VARCHAR" mode="in">
-        <default/>
-      </parameter>
-      <parameter name="p_date" type="TIMESTAMP" mode="in">
-        <default/>
-      </parameter>
-      <parameter name="p_products" type="CHAR" 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_Result NUMBER;
-  v_Message VARCHAR2(2000):='';
-  v_Record_ID VARCHAR2(32);
-  v_User VARCHAR2(32);
-  v_Deleted NUMBER;
-  -- Parameter
-  TYPE RECORD IS REF CURSOR;
-    Cur_Parameter RECORD;
-    Cur_PriceList record;
-    v_rowcount NUMBER;
-  BEGIN
-    --  Update AD_PInstance
-    DBMS_OUTPUT.PUT_LINE('Updating PInstance - Processing ' || p_PInstance_ID) ;
-    v_ResultStr:='PInstanceNotFound';
-    AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'Y', NULL, NULL) ;
-  BEGIN --BODY
-    --  Get Parameters
-    v_ResultStr:='ReadingParameters';
-    FOR Cur_Parameter in
-      (SELECT i.Record_ID,
-        i.AD_User_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=p_PInstance_ID
-      ORDER BY p.SeqNo
-      )
-    LOOP
-      v_Record_ID:=Cur_Parameter.Record_ID;
-      v_User:=Cur_Parameter.AD_User_ID;
-    END LOOP; --  Get Parameter
-    DELETE
-    FROM M_COSTING
-    WHERE ISPERMANENT='N'
-      AND ISMANUAL='N'
-      AND COSTTYPE='ST';
-    v_rowcount:=SQL%ROWCOUNT;
-    v_Deleted:=v_Deleted + v_rowcount;
-    DBMS_OUTPUT.PUT_LINE('  deleted=' || v_rowcount) ;
-    FOR Cur_PriceList in
-      (SELECT COALESCE(M_PRODUCT.COSTSTD, 0) AS COSTSTD,
-        M_PRODUCT.M_PRODUCT_ID,
-        M_PRODUCT.AD_CLIENT_ID,
-        M_PRODUCT.AD_ORG_ID
-      FROM M_PRODUCT
-      WHERE M_PRODUCT_ID IN
-        (SELECT M_PRODUCT_ID FROM M_PRODUCT  WHERE COSTTYPE='ST')
-        AND M_PRODUCT_ID IN(p_Products)
-        AND NOT EXISTS
-        (SELECT 1
-        FROM M_COSTING
-        WHERE COSTTYPE='ST'
-          AND M_PRODUCT_ID=M_PRODUCT.M_PRODUCT_ID
-          AND DATEFROM=p_Date
-          AND PRICE=M_PRODUCT.COSTSTD
-          AND COST=M_PRODUCT.COSTSTD
-        )
-      )
-    LOOP
-      UPDATE M_COSTING
-        SET DATETO=p_Date
-      WHERE DATETO=TO_DATE('31-12-9999', 'DD-MM-YYYY')
-        AND M_PRODUCT_ID=Cur_PriceList.M_PRODUCT_ID
-        AND COSTTYPE='ST';
-      INSERT
-      INTO M_COSTING
-        (
-          M_COSTING_ID, CREATED, CREATEDBY, UPDATED,
-          UPDATEDBY, AD_CLIENT_ID, AD_ORG_ID, M_PRODUCT_ID,
-          DATETO, DATEFROM, ISMANUAL, M_INOUTLINE_ID,
-          C_INVOICELINE_ID, QTY, PRICE, CUMQTY,
-          COST, COSTTYPE, ISPERMANENT
-        )
-        VALUES
-        (
-          Ad_Sequence_Nextno('M_Costing'), now(), v_User, now(),
-          v_User, Cur_PriceList.AD_CLIENT_ID, Cur_PriceList.AD_ORG_ID, Cur_PriceList.M_PRODUCT_ID,
-          TO_DATE('31-12-9999', 'DD-MM-YYYY'), p_Date, 'N', NULL,
-          NULL, 0, Cur_PriceList.COSTSTD, 0,
-          Cur_PriceList.COSTSTD, 'ST', 'N'
-        )
-        ;
-    END LOOP;
-    --<<FINISH_PROCESS>>
-    --  Update AD_PInstance
-    DBMS_OUTPUT.PUT_LINE('Updating PInstance - Finished ' || v_Message) ;
-    AD_UPDATE_PINSTANCE(p_PInstance_ID, v_User, 'N', 1, v_Message) ;
-    RETURN;
-  END; --BODY
-EXCEPTION
-WHEN OTHERS THEN
-  ROLLBACK;
-  v_ResultStr:= '@ERROR=' || SQLERRM;
-  DBMS_OUTPUT.PUT_LINE(v_ResultStr) ;
-  AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', 0, v_ResultStr) ;
-  RETURN;
-END GENERATE_STANDAR_COSTS
-]]></body>
-    </function>
-  </database>
--- a/src-db/database/model/functions/GETPERIODNAME.xml	Thu Oct 09 14:13:04 2008 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,38 +0,0 @@
-<?xml version="1.0"?>
-  <database name="FUNCTION GETPERIODNAME">
-    <function name="GETPERIODNAME" type="NVARCHAR">
-      <parameter name="p_periodno" type="NUMERIC" mode="in">
-        <default/>
-      </parameter>
-      <parameter name="p_year_id" type="VARCHAR" 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):  ______________________________________.
-************************************************************************/
-  v_Period NVARCHAR2(60) ;
-  --
-BEGIN
-  SELECT NAME
-  INTO v_Period
-  FROM C_PERIOD
-  WHERE C_YEAR_ID=p_Year_ID
-    AND periodno=p_PeriodNo;
-  RETURN v_Period;
-END GETPERIODNAME
-]]></body>
-    </function>
-  </database>
--- a/src-db/database/model/functions/GET_PARETO_ABC.xml	Thu Oct 09 14:13:04 2008 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,94 +0,0 @@
-<?xml version="1.0"?>
-  <database name="FUNCTION GET_PARETO_ABC">
-    <function name="GET_PARETO_ABC" type="VARCHAR">
-      <parameter name="p_warehouse_id" type="VARCHAR" mode="in">
-        <default/>
-      </parameter>
-      <parameter name="p_org_id" type="VARCHAR" mode="in">
-        <default/>
-      </parameter>
-      <parameter name="p_client_id" type="VARCHAR" mode="in">
-        <default/>
-      </parameter>
-      <parameter name="p_percentageactual" 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-2008 Openbravo SL
-* All Rights Reserved.
-* Contributor(s):  ______________________________________.
-************************************************************************/
-
-  v_orgList VARCHAR2(4000) ;
-  VARaCUM NUMBER:=0;
-  VARaCUMB NUMBER:=0;
-  v_limitA NUMBER:=80;
-  v_limitB NUMBER:=15;
-  varComprobadoA NUMBER:=0;
-  varComprobadoB NUMBER:=0;
-  TYPE RECORD IS REF CURSOR;
-  Cur_Cursor RECORD;
-BEGIN
-  varAcum :=0;
-  varComprobadoA := 0;
-  for Cur_Cursor in (
-   SELECT
-  100*(GET_PRODUCT_COST(PR.M_PRODUCT_ID,TO_DATE(now()),'AV')/ (select SUM(GET_PRODUCT_COST(PR1.M_PRODUCT_ID,TO_DATE(now()),'AV'))
-                                                       from M_WAREHOUSE MW1
-                                                       LEFT JOIN M_LOCATOR ML1 ON ML1.M_WAREHOUSE_ID=MW1.M_WAREHOUSE_ID
-                                                       LEFT JOIN M_STORAGE_DETAIL MSD1 ON ML1.M_LOCATOR_ID=MSD1.M_LOCATOR_ID
-                                                       LEFT JOIN M_PRODUCT PR1 ON MSD1.M_PRODUCT_ID=PR1.M_PRODUCT_ID
-                                                       WHERE (p_warehouse_ID IS NULL OR MW1.M_WAREHOUSE_ID=p_warehouse_ID)
-                                                       AND (p_org_ID IS NULL OR MW1.AD_ORG_ID=p_org_ID)
-                                                       AND (p_client_id IS NULL OR MW1.AD_CLIENT_ID=p_client_id) 
-                                                       AND MSD1.QTYONHAND>0) ) as PERCENTAGE
-  FROM
-   M_WAREHOUSE MW
-     LEFT JOIN M_LOCATOR ML ON ML.M_WAREHOUSE_ID=MW.M_WAREHOUSE_ID
-     LEFT JOIN M_STORAGE_DETAIL MSD ON ML.M_LOCATOR_ID=MSD.M_LOCATOR_ID
-     LEFT JOIN M_PRODUCT PR ON MSD.M_PRODUCT_ID=PR.M_PRODUCT_ID
-  WHERE (p_warehouse_ID IS NULL OR MW.M_WAREHOUSE_ID=p_warehouse_ID)
-     AND (p_org_ID IS NULL OR MW.AD_ORG_ID=p_org_ID)
-     AND (p_client_id IS NULL OR MW.AD_CLIENT_ID=p_client_id)
-     AND MSD.QTYONHAND>0
-     AND GET_PRODUCT_COST(PR.M_PRODUCT_ID,TO_DATE(now()),'AV') IS NOT NULL
-     AND GET_PRODUCT_COST(PR.M_PRODUCT_ID,TO_DATE(now()),'AV') <> 0
-  ORDER BY PERCENTAGE DESC) loop
-  varAcum := varAcum+Cur_Cursor.percentage;
-   if(varComprobadoA=0)then
-     if (varAcum>=v_limitA) then
-       if (p_percentageactual>=Cur_Cursor.percentage) then
-	     return 'A';
-       else
-         varComprobadoA:=-1;
-         varAcumB:=-1*Cur_Cursor.percentage;
-       end if;
-     end if;
-   end if;
-
-  if (varComprobadoA=-1 and varComprobadoB=0) then
-  varAcumB:=varAcumB+Cur_Cursor.percentage;
-   if (varAcumB>=v_limitB) then
-     if (p_percentageactual>=Cur_Cursor.percentage) then return 'B';
-	 else varComprobadoB:=-1;
-     end if;
-   end if;
-  end if;
-
- end loop;
- return 'C';
-END GET_PARETO_ABC
-]]></body>
-    </function>
-  </database>
--- a/src-db/database/model/functions/GET_PRICELIST_VERSION.xml	Thu Oct 09 14:13:04 2008 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,48 +0,0 @@
-<?xml version="1.0"?>
-  <database name="FUNCTION GET_PRICELIST_VERSION">
-    <function name="GET_PRICELIST_VERSION" type="VARCHAR">
-      <parameter name="p_pricelist_id" type="VARCHAR" mode="in">
-        <default/>
-      </parameter>
-      <parameter name="p_date" type="TIMESTAMP" 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):  ______________________________________.
-************************************************************************/
-  v_M_PriceList_Version_ID VARCHAR2(32);
-  TYPE RECORD IS REF CURSOR;
-    Cur_Pricelist_Version RECORD;
-  BEGIN
-    FOR Cur_Pricelist_Version IN
-      (SELECT PLV.M_PRICELIST_VERSION_ID AS PRICELISTVERSION
-      FROM M_PRICELIST PL,
-        M_PRICELIST_VERSION PLV
-      WHERE PL.M_PRICELIST_ID = PLV.M_PRICELIST_ID
-        AND PLV.ISACTIVE = 'Y'
-        AND PL.M_PRICELIST_ID = p_Pricelist_Id
-        AND PLV.VALIDFROM <= p_Date
-      ORDER BY PLV.VALIDFROM DESC
-      )
-    LOOP
-      v_M_PriceList_Version_ID := Cur_Pricelist_Version.PRICELISTVERSION;
-      EXIT;
-    END LOOP;
-    RETURN v_M_PriceList_Version_ID;
-END GET_PRICELIST_VERSION
-]]></body>
-    </function>
-  </database>
--- a/src-db/database/model/functions/GET_PRODUCT_COST.xml	Thu Oct 09 14:13:04 2008 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,77 +0,0 @@
-<?xml version="1.0"?>
-  <database name="FUNCTION GET_PRODUCT_COST">
-    <function name="GET_PRODUCT_COST" type="NUMERIC">
-      <parameter name="p_product_id" type="VARCHAR" mode="in">
-        <default/>
-      </parameter>
-      <parameter name="p_movementdate" type="TIMESTAMP" mode="in">
-        <default/>
-      </parameter>
-      <parameter name="p_costtype" type="CHAR" 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):  ______________________________________.
-************************************************************************/
-  v_Cost     NUMBER;
-  v_Count    NUMBER;
-  v_CostType CHAR(2) ;
-  TYPE RECORD IS REF CURSOR;
-    Cur_Cost RECORD;
-  BEGIN
-    v_CostType := p_CostType;
-    IF(p_Product_ID IS NULL) THEN
-      RETURN 0;
-    END IF;
-    IF(p_MovementDate IS NULL) THEN
-      RETURN 0;
-    END IF;
-    /*IF (v_CostType IS NULL) THEN
-    SELECT  COSTTYPE INTO v_CostType
-    FROM M_PRODUCT
-    WHERE M_PRODUCT_ID=p_Product_ID;
-    END IF;*/
-    SELECT COUNT(*)
-    INTO v_Count
-    FROM M_COSTING
-    WHERE p_MovementDate BETWEEN DATEFROM AND DATETO
-      AND M_PRODUCT_ID = p_Product_ID
-      AND(v_CostType IS NULL
-      OR v_CostType = COSTTYPE) ;
-    IF(v_Count = 0) THEN
-      RETURN 0;
-    ELSE
-      FOR Cur_Cost IN
-        (SELECT COST
-        FROM M_COSTING
-        WHERE DATEFROM < p_MovementDate
-          AND DATETO >= p_MovementDate
-          AND M_PRODUCT_ID = p_Product_ID
-          AND(v_CostType IS NULL
-          OR v_CostType = COSTTYPE)
-        ORDER BY DATEFROM,
-          DATETO DESC
-        )
-      LOOP
-        v_Cost := Cur_Cost.COST;
-        EXIT;
-      END LOOP;
-      RETURN v_Cost;
-    END IF;
-END GET_PRODUCT_COST
-]]></body>
-    </function>
-  </database>
--- a/src-db/database/model/functions/GET_PRODUCT_PRICE.xml	Thu Oct 09 14:13:04 2008 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,69 +0,0 @@
-<?xml version="1.0"?>
-  <database name="FUNCTION GET_PRODUCT_PRICE">
-    <function name="GET_PRODUCT_PRICE" type="NUMERIC">
-      <parameter name="p_ad_client_id" type="VARCHAR" mode="in">
-        <default/>
-      </parameter>
-      <parameter name="p_m_product_id" type="VARCHAR" 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):  ______________________________________.
-************************************************************************/
-  v_M_PriceList_Version_ID VARCHAR2(32);
-  v_Price                  NUMBER;
-BEGIN
-  SELECT M_PRICELIST_VERSION_ID
-  INTO v_M_PriceList_Version_ID
-  FROM M_PRICELIST_VERSION
-  WHERE M_PRICELIST_ID IN
-    (SELECT M_PRICELIST_ID
-    FROM M_PRICELIST
-    WHERE ISDEFAULT = 'Y'
-      AND AD_CLIENT_ID = p_AD_Client_ID
-      AND ISACTIVE = 'Y'
-    )
-    AND VALIDFROM =
-    (SELECT max(VALIDFROM)
-    FROM M_PRICELIST_VERSION
-    WHERE M_PRICELIST_ID IN
-      (SELECT M_PRICELIST_ID
-      FROM M_PRICELIST
-      WHERE ISDEFAULT = 'Y'
-        AND AD_CLIENT_ID = p_AD_Client_ID
-        AND ISACTIVE = 'Y'
-      )
-      AND VALIDFROM <= now()
-    )
-    ;
-  IF(v_M_PriceList_Version_ID IS NULL) THEN
-    RETURN 0;
-  ELSE
-    SELECT COALESCE(PRICESTD, 0)
-    INTO v_Price
-    FROM M_PRODUCTPRICE
-    WHERE M_PRICELIST_VERSION_ID = v_M_PriceList_Version_ID
-      AND AD_CLIENT_ID = p_AD_Client_ID
-      AND M_PRODUCT_ID = p_M_Product_ID;
-  END IF;
-  IF v_Price IS NULL THEN
-    v_Price := 0;
-  END IF;
-  RETURN v_Price;
-END GET_PRODUCT_PRICE
-]]></body>
-    </function>
-  </database>
--- a/src-db/database/model/functions/GET_PRODUCT_PRICE_DATE.xml	Thu Oct 09 14:13:04 2008 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,80 +0,0 @@
-<?xml version="1.0"?>
-  <database name="FUNCTION GET_PRODUCT_PRICE_DATE">
-    <function name="GET_PRODUCT_PRICE_DATE" type="NUMERIC">
-      <parameter name="p_ad_client_id" type="VARCHAR" mode="in">
-        <default/>
-      </parameter>
-      <parameter name="p_m_product_id" type="VARCHAR" mode="in">
-        <default/>
-      </parameter>
-      <parameter name="p_date" type="TIMESTAMP" 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) 2007 Openbravo SL
-* All Rights Reserved.
-* Contributor(s):  ______________________________________.
-************************************************************************/
-  v_M_PriceList_Version_ID VARCHAR2(32);
-  v_Price                  NUMBER;
-BEGIN
-  SELECT MAX(M_PRICELIST_VERSION_ID)
-  INTO v_M_PriceList_Version_ID
-  FROM M_PRICELIST_VERSION
-  WHERE M_PRICELIST_ID IN
-    (SELECT M_PRICELIST_ID
-    FROM M_PRICELIST
-    WHERE ISDEFAULT = 'Y'
-      AND AD_CLIENT_ID = p_AD_Client_ID
-      AND ISACTIVE = 'Y'
-    )
-    AND VALIDFROM =
-    (SELECT max(VALIDFROM)
-    FROM M_PRICELIST_VERSION
-    WHERE M_PRICELIST_ID IN
-      (SELECT M_PRICELIST_ID
-      FROM M_PRICELIST
-      WHERE ISDEFAULT = 'Y'
-        AND AD_CLIENT_ID = p_AD_Client_ID
-        AND ISACTIVE = 'Y'
-      )
-      AND TO_DATE(VALIDFROM) <= TO_DATE(COALESCE(p_Date, now()))
-      AND EXISTS(SELECT 1
-                 FROM M_PRODUCTPRICE
-                 WHERE M_PRICELIST_VERSION_ID = M_PRICELIST_VERSION.M_PRICELIST_VERSION_ID
-                   AND M_PRODUCT_ID = p_M_Product_ID)
-    )
-    AND EXISTS(SELECT 1
-                 FROM M_PRODUCTPRICE
-                 WHERE M_PRICELIST_VERSION_ID = M_PRICELIST_VERSION.M_PRICELIST_VERSION_ID
-                   AND M_PRODUCT_ID = p_M_Product_ID)
-    ;
-  IF(v_M_PriceList_Version_ID IS NULL) THEN
-    RETURN 0;
-  ELSE
-    SELECT COALESCE(PRICESTD, 0)
-    INTO v_Price
-    FROM M_PRODUCTPRICE
-    WHERE M_PRICELIST_VERSION_ID = v_M_PriceList_Version_ID
-      AND AD_CLIENT_ID = p_AD_Client_ID
-      AND M_PRODUCT_ID = p_M_Product_ID;
-  END IF;
-  IF v_Price IS NULL THEN
-    v_Price := 0;
-  END IF;
-  RETURN v_Price;
-END GET_PRODUCT_PRICE_DATE
-]]></body>
-    </function>
-  </database>
--- a/src-db/database/model/functions/GET_PURCHASE_PRICE.xml	Thu Oct 09 14:13:04 2008 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,92 +0,0 @@
-<?xml version="1.0"?>
-  <database name="FUNCTION GET_PURCHASE_PRICE">
-    <function name="GET_PURCHASE_PRICE" type="NUMERIC">
-      <parameter name="p_product_id" type="VARCHAR" 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):  ______________________________________.
-************************************************************************/
-  v_Price NUMBER;
-  TYPE RECORD IS REF CURSOR;
-    Cur_Orderline RECORD;
-    Cur_Invoiceline RECORD;
-    Cur_Pricelist_Version RECORD;
-  BEGIN
-    FOR Cur_Invoiceline IN
-      (SELECT COALESCE(C_INVOICELINE.PRICEACTUAL, 0) AS Price
-      FROM C_INVOICELINE,
-        C_INVOICE
-      WHERE C_INVOICELINE.C_INVOICE_ID = C_INVOICE.C_INVOICE_ID
-        AND C_INVOICE.ISSOTRX = 'N'
-        AND C_INVOICE.DOCSTATUS = 'CO'
-        AND C_INVOICE.PROCESSED = 'Y'
-        AND C_INVOICELINE.M_PRODUCT_ID = p_Product_ID
-      ORDER BY C_INVOICE.DATEINVOICED DESC
-      )
-    LOOP
-      v_Price := Cur_Invoiceline.PRICE;
-      EXIT;
-    END LOOP;
-    IF(v_Price IS NULL) THEN
-      FOR Cur_Orderline IN
-        (SELECT COALESCE(C_ORDERLINE.PRICEACTUAL, 0) AS Price
-        FROM C_ORDERLINE,
-          C_ORDER
-        WHERE C_ORDERLINE.C_ORDER_ID = C_ORDER.C_ORDER_ID
-          AND C_ORDER.ISSOTRX = 'N'
-          AND C_ORDER.DOCSTATUS = 'CO'
-          AND C_ORDER.PROCESSED = 'Y'
-          AND C_ORDERLINE.M_PRODUCT_ID = p_Product_ID
-        ORDER BY C_ORDER.DATEORDERED DESC
-        )
-      LOOP
-        v_Price := Cur_Orderline.PRICE;
-        EXIT;
-      END LOOP;
-    END IF;
-    IF(v_Price IS NULL) THEN
-      FOR Cur_Pricelist_Version IN
-        (SELECT COALESCE(M_PRODUCTPRICE.PRICESTD, 0) AS Price
-        FROM M_PRODUCTPRICE,
-          M_PRICELIST_VERSION,
-          M_PRICELIST
-        WHERE M_PRODUCTPRICE.M_PRICELIST_VERSION_ID = M_PRICELIST_VERSION.M_PRICELIST_VERSION_ID
-          AND M_PRICELIST_VERSION.M_PRICELIST_ID = M_PRICELIST.M_PRICELIST_ID
-          AND M_PRICELIST.ISSOPRICELIST = 'N'  --  AND M_PRICELIST_VERSION.ISACTIVE= 'Y'
-          AND M_PRICELIST_VERSION.VALIDFROM < now()
-          AND M_PRODUCTPRICE.M_PRODUCT_ID = p_Product_ID
-        ORDER BY M_PRICELIST_VERSION.VALIDFROM DESC
-        )
-      LOOP
-        v_Price := Cur_Pricelist_Version.PRICE;
-        EXIT;
-      END LOOP;
-    END IF;
-    IF(v_Price IS NULL) THEN
-      SELECT COALESCE(M_PRODUCT_PO.PRICEPO, 0)
-      INTO v_Price
-      FROM M_PRODUCT_PO
-      WHERE M_PRODUCT_PO.M_PRODUCT_ID = p_Product_ID;
-    END IF;
-    IF(v_Price IS NULL) THEN
-      v_Price := 0;
-    END IF;
-    RETURN v_Price;
-END GET_PURCHASE_PRICE
-]]></body>
-    </function>
-  </database>
--- a/src-db/database/model/functions/ID_DEVELOPMENT_RANGE.xml	Thu Oct 09 14:13:04 2008 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,32 +0,0 @@
-<?xml version="1.0"?>
-  <database name="FUNCTION ID_DEVELOPMENT_RANGE">
-    <function name="ID_DEVELOPMENT_RANGE" type="NUMERIC">
-      <parameter name="theid" 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) 2008 Openbravo SL
-* All Rights Reserved.
-* Contributor(s):  ______________________________________.
-************************************************************************/
- BEGIN
-   IF (theID>=0 AND theID<=99999) OR (theID>=800000 AND theID<=999999) OR (theID>=1000000000) THEN
-     RETURN 0;
-   ELSIF  (theID>=100000 AND theID<=799999) OR (theID>=1000000 AND theID<=999999999) THEN
-     RETURN 1;
-   END IF;
-END ID_DEVELOPMENT_RANGE
-]]></body>
-    </function>
-  </database>
--- a/src-db/database/model/functions/IMPORT_PRODUCT.xml	Thu Oct 09 14:13:04 2008 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,38 +0,0 @@
-<?xml version="1.0"?>
-  <database name="FUNCTION IMPORT_PRODUCT">
-    <function name="IMPORT_PRODUCT" type="NULL">
-      <parameter name="pinstance_id" type="VARCHAR" mode="in">
-        <default/>
-      </parameter>
-      <body><![CDATA[/*************************************************************************
-  * The contents of this file are subject to the Compiere Public
-  * License 1.1 ("License"); You may not use this file except in
-  * compliance with the License. You may obtain a copy of the License in
-  * the legal folder of your Openbravo installation.
-  * 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  Compiere  ERP &  Business Solution
-  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
-  * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke,
-  * parts created by ComPiere are Copyright (C) ComPiere, Inc.;
-  * All Rights Reserved.
-  * Contributor(s): Openbravo SL
-  * Contributions are Copyright (C) 2001-2008 Openbravo, S.L.
-  *
-  * Specifically, this derivative work is based upon the following Compiere
-  * file and version.
-  *************************************************************************
-  * $Id: Import_Product.sql,v 1.6 2003/05/26 22:31:16 jjanke Exp $
-  ***
-  * Title: Import Products
-  * Description:
-  *  Replaced by Java Class.
-  ************************************************************************/
-BEGIN
-  NULL;
-END IMPORT_PRODUCT
-]]></body>
-    </function>
-  </database>
--- a/src-db/database/model/functions/MA_STANDARD_COST_SEQUENCE.xml	Thu Oct 09 14:13:04 2008 +0000
+++ b/src-db/database/model/functions/MA_STANDARD_COST_SEQUENCE.xml	Thu Oct 09 17:15:25 2008 +0000
@@ -164,7 +164,7 @@
 
     -- Purchased raw material
     IF (v_No_Cost) THEN
-      SELECT COALESCE(MAX(bl.Price),GET_PRODUCT_PRICE_DATE(MAX(p.AD_CLIENT_ID), MAX(p.M_PRODUCT_ID), p_CalcDate)) INTO v_Cost
+      SELECT COALESCE(MAX(bl.Price),M_GET_PRODUCT_PRICE_DATE(MAX(p.AD_CLIENT_ID), MAX(p.M_PRODUCT_ID), p_CalcDate)) INTO v_Cost
       FROM M_Product p LEFT JOIN C_BudgetLine bl ON p.M_Product_ID = bl.M_Product_ID
                                                     AND bl.C_Budget_ID = p_Budget_ID
       WHERE p.M_Product_ID = Cur_UsedProducts.M_Product_ID
--- a/src-db/database/model/functions/MA_WORKEFFORT_VALIDATE.xml	Thu Oct 09 14:13:04 2008 +0000
+++ b/src-db/database/model/functions/MA_WORKEFFORT_VALIDATE.xml	Thu Oct 09 17:15:25 2008 +0000
@@ -204,7 +204,7 @@
       LOOP
         -- M_ProductionLine_ID, AD_Client_ID, AD_Org_ID, MovementDate, M_Product_ID, MovementQty, M_Locator_ID
         -- DBMS_OUTPUT.PUT_LINE('ProductionLine=' || CUR_PL_Post.M_ProductionLine_ID);
-        -- DBMS_OUTPUT.PUT_LINE('  Qty=' || CUR_PL_Post.MovementQty || ', OnHand=' || BOM_Qty_OnHand(CUR_PL_Post.M_Product_ID, NULL, CUR_PL_Post.M_Locator_ID));
+        -- DBMS_OUTPUT.PUT_LINE('  Qty=' || CUR_PL_Post.MovementQty || ', OnHand=' || M_BOM_Qty_OnHand(CUR_PL_Post.M_Product_ID, NULL, CUR_PL_Post.M_Locator_ID));
         -- Check Stock levels for reductions
         -- Check, if product exists and if it is stocked
         -- Unlimited capacity if no item
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src-db/database/model/functions/M_BOM_PRICELIMIT.xml	Thu Oct 09 17:15:25 2008 +0000
@@ -0,0 +1,59 @@
+<?xml version="1.0"?>
+  <database name="FUNCTION M_BOM_PRICELIMIT">
+    <function name="M_BOM_PRICELIMIT" type="NUMERIC">
+      <parameter name="p_product_id" type="VARCHAR" mode="in">
+        <default/>
+      </parameter>
+      <parameter name="p_pricelist_version_id" type="VARCHAR" mode="in">
+        <default/>
+      </parameter>
+      <body><![CDATA[/******************************************************************************
+* The contents of this file are subject to the   Compiere License  Version 1.1
+* ("License"); You may not use this file except in compliance with the License
+* You may obtain a copy of the License at http://www.compiere.org/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                  Compiere  ERP &  Business Solution
+* The Initial Developer of the Original Code is Jorg Janke  and ComPiere, Inc.
+* Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke, parts
+* created by ComPiere are Copyright (C) ComPiere, Inc.;   All Rights Reserved.
+* Contributor(s): Openbravo SL
+* Contributions are Copyright (C) 2001-2006 Openbravo S.L.
+******************************************************************************/
+  v_Price        NUMBER;
+  v_ProductPrice NUMBER;
+  -- Get BOM Product info
+  TYPE RECORD IS REF CURSOR;
+    CUR_BOM RECORD;
+    --
+  BEGIN
+    -- Try to get price from PriceList directly
+    SELECT COALESCE(SUM(PriceLimit), 0)
+    INTO v_Price
+    FROM M_ProductPrice
+    WHERE M_PriceList_Version_ID = p_PriceList_Version_ID
+      AND M_Product_ID = p_Product_ID;
+    -- DBMS_OUTPUT.PUT_LINE('Price=' || v_Price);
+    -- No Price - Check if BOM
+    IF(v_Price = 0) THEN
+      FOR CUR_BOM IN
+        (SELECT b.M_ProductBOM_ID,
+          b.BOMQty,
+          p.IsBOM
+        FROM M_Product_BOM b,
+          M_Product p
+        WHERE b.M_ProductBOM_ID = p.M_Product_ID
+          AND b.M_Product_ID = p_Product_ID
+        )
+      LOOP
+        v_ProductPrice := M_BOM_PriceLimit(CUR_BOM.M_ProductBOM_ID, p_PriceList_Version_ID) ;
+        v_Price := v_Price +(CUR_BOM.BOMQty * v_ProductPrice) ;
+      END LOOP;
+    END IF;
+    --
+    RETURN v_Price;
+END M_BOM_PRICELIMIT
+]]></body>
+    </function>
+  </database>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src-db/database/model/functions/M_BOM_PRICELIST.xml	Thu Oct 09 17:15:25 2008 +0000
@@ -0,0 +1,60 @@
+<?xml version="1.0"?>
+  <database name="FUNCTION M_BOM_PRICELIST">
+    <function name="M_BOM_PRICELIST" type="NUMERIC">
+      <parameter name="product_id" type="VARCHAR" mode="in">
+        <default/>
+      </parameter>
+      <parameter name="pricelist_version_id" type="VARCHAR" mode="in">
+        <default/>
+      </parameter>
+      <body><![CDATA[/******************************************************************************
+* The contents of this file are subject to the   Compiere License  Version 1.1
+* ("License"); You may not use this file except in compliance with the License
+* You may obtain a copy of the License at http://www.compiere.org/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                  Compiere  ERP &  Business Solution
+* The Initial Developer of the Original Code is Jorg Janke  and ComPiere, Inc.
+* Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke, parts
+* created by ComPiere are Copyright (C) ComPiere, Inc.;   All Rights Reserved.
+* Contributor(s): Openbravo SL
+* Contributions are Copyright (C) 2001-2006 Openbravo S.L.
+******************************************************************************/
+  v_Price        NUMBER;
+  v_ProductPrice NUMBER;
+  -- Get BOM Product info
+  TYPE RECORD IS REF CURSOR;
+    CUR_BOM RECORD;
+    --
+  BEGIN
+    -- Try to get price from pricelist directly
+    SELECT COALESCE(SUM(PriceList), 0)
+    INTO v_Price
+    FROM M_ProductPrice
+    WHERE M_PriceList_Version_ID=PriceList_Version_ID
+      AND M_Product_ID=Product_ID;
+    -- DBMS_OUTPUT.PUT_LINE('Price=' || v_Price);
+    -- No Price - Check if BOM
+    IF(v_Price=0) THEN
+      FOR CUR_BOM IN
+        (SELECT b.M_ProductBOM_ID,
+          b.BOMQty,
+          p.IsBOM
+        FROM M_Product_BOM b,
+          M_Product p
+        WHERE b.M_ProductBOM_ID=p.M_Product_ID
+          AND b.M_Product_ID=Product_ID
+        )
+      LOOP
+        v_ProductPrice:=M_BOM_PriceList(CUR_BOM.M_ProductBOM_ID, PriceList_Version_ID) ;
+        v_Price:=v_Price +(CUR_BOM.BOMQty * v_ProductPrice) ;
+        -- DBMS_OUTPUT.PUT_LINE('Qry=' || CUR_BOM.BOMQty || ' @ ' || v_ProductPrice || ', Price=' || v_Price);
+      END LOOP; -- BOM
+    END IF;
+    --
+    RETURN v_Price;
+END M_BOM_PRICELIST
+]]></body>
+    </function>
+  </database>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src-db/database/model/functions/M_BOM_PRICESTD.xml	Thu Oct 09 17:15:25 2008 +0000
@@ -0,0 +1,60 @@
+<?xml version="1.0"?>
+  <database name="FUNCTION M_BOM_PRICESTD">
+    <function name="M_BOM_PRICESTD" type="NUMERIC">
+      <parameter name="product_id" type="VARCHAR" mode="in">
+        <default/>
+      </parameter>
+      <parameter name="pricelist_version_id" type="VARCHAR" mode="in">
+        <default/>
+      </parameter>
+      <body><![CDATA[/******************************************************************************
+* The contents of this file are subject to the   Compiere License  Version 1.1
+* ("License"); You may not use this file except in compliance with the License
+* You may obtain a copy of the License at http://www.compiere.org/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                  Compiere  ERP &  Business Solution
+* The Initial Developer of the Original Code is Jorg Janke  and ComPiere, Inc.
+* Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke, parts
+* created by ComPiere are Copyright (C) ComPiere, Inc.;   All Rights Reserved.
+* Contributor(s): Openbravo SL
+* Contributions are Copyright (C) 2001-2006 Openbravo S.L.
+******************************************************************************/
+  v_Price        NUMBER;
+  v_ProductPrice NUMBER;
+  -- Get BOM Product info
+  TYPE RECORD IS REF CURSOR;
+    CUR_BOM RECORD;
+    --
+  BEGIN
+    -- Try to get price from pricelist directly
+    SELECT COALESCE(SUM(PriceStd), 0)
+    INTO v_Price
+    FROM M_ProductPrice
+    WHERE M_PriceList_Version_ID=PriceList_Version_ID
+      AND M_Product_ID=Product_ID;
+    -- DBMS_OUTPUT.PUT_LINE('Price=' || v_Price);
+    -- No Price - Check if BOM
+    IF(v_Price=0) THEN
+      FOR CUR_BOM IN
+        (SELECT b.M_ProductBOM_ID,
+          b.BOMQty,
+          p.IsBOM
+        FROM M_Product_BOM b,
+          M_Product p
+        WHERE b.M_ProductBOM_ID=p.M_Product_ID
+          AND b.M_Product_ID=Product_ID
+        )
+      LOOP
+        v_ProductPrice:=M_BOM_PriceStd(CUR_BOM.M_ProductBOM_ID, PriceList_Version_ID) ;
+        v_Price:=v_Price +(CUR_BOM.BOMQty * v_ProductPrice) ;
+        -- DBMS_OUTPUT.PUT_LINE('Price=' || v_Price);
+      END LOOP; -- BOM
+    END IF;
+    --
+    RETURN v_Price;
+END M_BOM_PRICESTD
+]]></body>
+    </function>
+  </database>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src-db/database/model/functions/M_BOM_QTY_AVAILABLE.xml	Thu Oct 09 17:15:25 2008 +0000
@@ -0,0 +1,40 @@
+<?xml version="1.0"?>
+  <database name="FUNCTION M_BOM_QTY_AVAILABLE">
+    <function name="M_BOM_QTY_AVAILABLE" type="NUMERIC">
+      <parameter name="p_product_id" type="VARCHAR" mode="in">
+        <default/>
+      </parameter>
+      <parameter name="p_warehouse_id" type="VARCHAR" mode="in">
+        <default/>
+      </parameter>
+      <parameter name="p_locator_id" type="VARCHAR" mode="in">
+        <default><![CDATA[NULL]]></default>
+      </parameter>
+      <body><![CDATA[/*************************************************************************
+* The contents of this file are subject to the Compiere Public
+* License 1.1 ("License"); You may not use this file except in
+* compliance with the License. You may obtain a copy of the License in
+* the legal folder of your Openbravo installation.
+* 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  Compiere  ERP &  Business Solution
+* The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
+* Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke,
+* parts created by ComPiere are Copyright (C) ComPiere, Inc.;
+* All Rights Reserved.
+* Contributor(s): Openbravo SL
+* Contributions are Copyright (C) 2001-2008 Openbravo, S.L.
+*
+* Specifically, this derivative work is based upon the following Compiere
+* file and version.
+*************************************************************************
+* Return quantity available for BOM
+*/
+BEGIN
+  RETURN M_BOM_Qty_OnHand(p_Product_ID, p_Warehouse_ID, p_Locator_ID) - M_BOM_Qty_Reserved(p_Product_ID, p_Warehouse_ID, p_Locator_ID) ;
+END M_BOM_QTY_AVAILABLE
+]]></body>
+    </function>
+  </database>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src-db/database/model/functions/M_BOM_QTY_ONHAND.xml	Thu Oct 09 17:15:25 2008 +0000
@@ -0,0 +1,150 @@
+<?xml version="1.0"?>
+  <database name="FUNCTION M_BOM_QTY_ONHAND">
+    <function name="M_BOM_QTY_ONHAND" type="NUMERIC">
+      <parameter name="p_product_id" type="VARCHAR" mode="in">
+        <default/>
+      </parameter>
+      <parameter name="p_warehouse_id" type="VARCHAR" mode="in">
+        <default/>
+      </parameter>
+      <parameter name="p_locator_id" type="VARCHAR" mode="in">
+        <default><![CDATA[NULL]]></default>
+      </parameter>
+      <body><![CDATA[/*************************************************************************
+* The contents of this file are subject to the Compiere Public
+* License 1.1 ("License"); You may not use this file except in
+* compliance with the License. You may obtain a copy of the License in
+* the legal folder of your Openbravo installation.
+* 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  Compiere  ERP &  Business Solution
+* The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
+* Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke,
+* parts created by ComPiere are Copyright (C) ComPiere, Inc.;
+* All Rights Reserved.
+* Contributor(s): Openbravo SL
+* Contributions are Copyright (C) 2001-2008 Openbravo, S.L.
+*
+* Specifically, this derivative work is based upon the following Compiere
+* file and version.
+*************************************************************************
+* Return quantity on hand for BOM
+*/
+    v_myWarehouse_ID VARCHAR2(32);
+    v_Quantity NUMBER:=99999; -- unlimited
+    v_IsBOM CHAR(1) ;
+    v_IsStocked CHAR(1) ;
+    v_ProductType CHAR(1) ;
+    v_ProductQty NUMBER;
+    v_StdPrecision NUMBER;
+    -- Get BOM Product info
+  TYPE RECORD IS REF CURSOR;
+    CUR_BOM RECORD;
+    --
+  BEGIN
+    -- Check Parameters
+    v_myWarehouse_ID:=p_Warehouse_ID;
+    IF(v_myWarehouse_ID IS NULL) THEN
+      IF(p_Locator_ID IS NULL) THEN
+        RETURN 0;
+      ELSE
+        SELECT MAX(M_Warehouse_ID)
+        INTO v_myWarehouse_ID
+        FROM M_LOCATOR
+        WHERE M_Locator_ID=p_Locator_ID;
+      END IF;
+    END IF;
+    IF(v_myWarehouse_ID IS NULL) THEN
+      RETURN 0;
+    END IF;
+    -- DBMS_OUTPUT.PUT_LINE('Warehouse=' || v_myWarehouse_ID);
+    -- Check, if product exists and if it is stocked
+  BEGIN
+    SELECT IsBOM, ProductType, IsStocked
+    INTO v_IsBOM, v_ProductType, v_IsStocked
+    FROM M_PRODUCT
+    WHERE M_Product_ID=p_Product_ID;
+    --
+  EXCEPTION -- not found
+  WHEN OTHERS THEN
+    RETURN 0;
+  END;
+  -- Unimited capacity if no item
+  IF((v_ProductType<>'I' OR(v_IsStocked='N' AND(v_IsBOM='N')))) THEN
+    RETURN v_Quantity;
+    -- Stocked item
+  ELSIF(v_IsStocked='Y') THEN
+    -- Get ProductQty
+    SELECT COALESCE(SUM(QtyOnHand), 0)
+    INTO v_ProductQty
+    FROM M_STORAGE_DETAIL s
+    WHERE M_Product_ID=p_Product_ID  AND EXISTS
+      (SELECT *
+      FROM M_LOCATOR l
+      WHERE s.M_Locator_ID=l.M_Locator_ID  AND l.M_Warehouse_ID=v_myWarehouse_ID
+      )
+      ;
+    --
+    -- DBMS_OUTPUT.PUT_LINE('Qty=' || v_ProductQty);
+    RETURN v_ProductQty;
+  END IF;
+  -- Go though BOM
+  -- DBMS_OUTPUT.PUT_LINE('BOM');
+  FOR CUR_BOM IN
+    (SELECT b.M_ProductBOM_ID, b.BOMQty, p.IsBOM, p.IsStocked, p.ProductType
+    FROM M_PRODUCT_BOM b, M_PRODUCT p
+    WHERE b.M_ProductBOM_ID=p.M_Product_ID  AND b.M_Product_ID=p_Product_ID
+    )
+  LOOP
+    -- Stocked Items "leaf node"
+    IF(CUR_BOM.ProductType='I' AND CUR_BOM.ProductType='I') THEN
+      -- Get v_ProductQty
+      SELECT COALESCE(SUM(QtyOnHand), 0)
+      INTO v_ProductQty
+      FROM M_STORAGE_DETAIL s
+      WHERE M_Product_ID=CUR_BOM.M_ProductBOM_ID  AND EXISTS
+        (SELECT *
+        FROM M_LOCATOR l
+        WHERE s.M_Locator_ID=l.M_Locator_ID  AND l.M_Warehouse_ID=v_myWarehouse_ID
+        )
+        ;
+      -- Get Rounding Precision
+      SELECT COALESCE(MAX(u.StdPrecision), 0)
+      INTO v_StdPrecision
+      FROM C_UOM u, M_PRODUCT p
+      WHERE u.C_UOM_ID=p.C_UOM_ID AND p.M_Product_ID=CUR_BOM.M_ProductBOM_ID;
+      -- How much can we make with this product
+      IF COALESCE(CUR_BOM.BOMQty, 0)=0 THEN
+        v_ProductQty:=0;
+      ELSE
+        v_ProductQty:=ROUND(v_ProductQty/CUR_BOM.BOMQty, v_StdPrecision) ;
+      END IF;
+      -- How much can we make overall
+      IF(v_ProductQty<v_Quantity) THEN
+        v_Quantity:=v_ProductQty;
+      END IF;
+      -- Another BOM
+    ELSIF(CUR_BOM.IsBOM='Y') THEN
+      v_ProductQty:=M_BOM_Qty_OnHand(CUR_BOM.M_ProductBOM_ID, v_myWarehouse_ID) ;
+      -- How much can we make overall
+      IF(v_ProductQty<v_Quantity) THEN
+        v_Quantity:=v_ProductQty;
+      END IF;
+    END IF;
+  END LOOP; -- BOM
+  IF(v_Quantity>0) THEN
+    -- Get Rounding Precision for Product
+    SELECT COALESCE(MAX(u.StdPrecision), 0)
+    INTO v_StdPrecision
+    FROM C_UOM u, M_PRODUCT p
+    WHERE u.C_UOM_ID=p.C_UOM_ID AND p.M_Product_ID=p_Product_ID;
+    --
+    RETURN ROUND(v_Quantity, v_StdPrecision) ;
+  END IF;
+  RETURN 0;
+END M_BOM_QTY_ONHAND
+]]></body>
+    </function>
+  </database>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src-db/database/model/functions/M_BOM_QTY_ORDERED.xml	Thu Oct 09 17:15:25 2008 +0000
@@ -0,0 +1,156 @@
+<?xml version="1.0"?>
+  <database name="FUNCTION M_BOM_QTY_ORDERED">
+    <function name="M_BOM_QTY_ORDERED" type="NUMERIC">
+      <parameter name="p_product_id" type="VARCHAR" mode="in">
+        <default/>
+      </parameter>
+      <parameter name="p_warehouse_id" type="VARCHAR" mode="in">
+        <default/>
+      </parameter>
+      <parameter name="p_locator_id" type="VARCHAR" mode="in">
+        <default><![CDATA[NULL]]></default>
+      </parameter>
+      <body><![CDATA[/*************************************************************************
+* The contents of this file are subject to the Compiere Public
+* License 1.1 ("License"); You may not use this file except in
+* compliance with the License. You may obtain a copy of the License in
+* the legal folder of your Openbravo installation.
+* 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  Compiere  ERP &  Business Solution
+* The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
+* Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke,
+* parts created by ComPiere are Copyright (C) ComPiere, Inc.;
+* All Rights Reserved.
+* Contributor(s): Openbravo SL
+* Contributions are Copyright (C) 2001-2008 Openbravo, S.L.
+*
+* Specifically, this derivative work is based upon the following Compiere
+* file and version.
+*************************************************************************
+*  Return quantity ordered for BOM
+*/
+  v_Warehouse_ID VARCHAR2(32);
+  v_Quantity NUMBER:= 99999; --  unlimited
+  v_IsBOM        CHAR(1) ;
+  v_IsStocked    CHAR(1) ;
+  v_ProductType  CHAR(1) ;
+  v_ProductQty   NUMBER;
+  v_StdPrecision NUMBER;
+  --  Get BOM Product info
+  TYPE RECORD IS REF CURSOR;
+    CUR_BOM RECORD;
+    --
+  BEGIN
+    --  Check Parameters
+    v_Warehouse_ID := p_Warehouse_ID;
+    IF(v_Warehouse_ID IS NULL) THEN
+      IF(p_Locator_ID IS NULL) THEN
+        RETURN 0;
+      ELSE
+        SELECT MAX(M_Warehouse_ID)
+        INTO v_Warehouse_ID
+        FROM M_Locator
+        WHERE M_Locator_ID = p_Locator_ID;
+      END IF;
+    END IF;
+    IF(v_Warehouse_ID IS NULL) THEN
+      RETURN 0;
+    END IF;
+    --  DBMS_OUTPUT.PUT_LINE('Warehouse=' || v_Warehouse_ID);
+    --  Check, if product exists and if it is stocked
+  BEGIN
+    SELECT IsBOM,
+      ProductType,
+      IsStocked
+    INTO v_IsBOM,
+      v_ProductType,
+      v_IsStocked
+    FROM M_Product
+    WHERE M_Product_ID = p_Product_ID;
+    --
+  EXCEPTION --  not found
+  WHEN OTHERS THEN
+    RETURN 0;
+  END;
+  --  No reservation for non-stocked
+  IF((v_ProductType <> 'I' OR(v_IsStocked = 'N' AND v_IsBOM = 'N'))) THEN
+    RETURN 0;
+    --  Stocked item
+  ELSIF(v_IsStocked = 'Y') THEN
+    --  Get ProductQty
+    SELECT COALESCE(SUM(QtyOrdered), 0)
+    INTO v_ProductQty
+    FROM M_Storage_Pending s
+    WHERE M_Product_ID = p_Product_ID
+      AND M_Warehouse_ID = v_Warehouse_ID;
+    --
+    RETURN v_ProductQty;
+  END IF;
+  --  Go though BOM
+  --  DBMS_OUTPUT.PUT_LINE('BOM');
+  FOR CUR_BOM IN
+    (SELECT b.M_ProductBOM_ID,
+      b.BOMQty,
+      p.IsBOM,
+      p.IsStocked,
+      p.ProductType
+    FROM M_Product_BOM b,
+      M_Product p
+    WHERE b.M_ProductBOM_ID = p.M_Product_ID
+      AND b.M_Product_ID = p_Product_ID
+    )
+  LOOP
+    --  Stocked Items "leaf node"
+    IF(CUR_BOM.ProductType = 'I' AND CUR_BOM.IsStocked = 'Y') THEN
+      --  Get ProductQty
+      SELECT COALESCE(SUM(QtyOrdered), 0)
+      INTO v_ProductQty
+      FROM M_Storage_Pending s
+      WHERE M_Product_ID = CUR_BOM.M_ProductBOM_ID
+        AND M_Warehouse_ID = v_Warehouse_ID;
+      --  Get Rounding Precision
+      SELECT COALESCE(MAX(u.StdPrecision), 0)
+      INTO v_StdPrecision
+      FROM C_UOM u,
+        M_Product p
+      WHERE u.C_UOM_ID = p.C_UOM_ID
+        AND p.M_Product_ID = CUR_BOM.M_ProductBOM_ID;
+      --  How much can we make with this product
+      v_ProductQty := ROUND(v_ProductQty/CUR_BOM.BOMQty, v_StdPrecision) ;
+      --  How much can we make overall
+      IF(v_ProductQty < v_Quantity) THEN
+        v_Quantity := v_ProductQty;
+      END IF;
+      --  Another BOM
+    ELSIF(CUR_BOM.IsBOM = 'Y') THEN
+      v_ProductQty := M_BOM_Qty_Reserved(CUR_BOM.M_ProductBOM_ID, v_Warehouse_ID) ;
+      --  How much can we make overall
+      IF(v_ProductQty < v_Quantity) THEN
+        v_Quantity := v_ProductQty;
+      END IF;
+    END IF;
+  END LOOP; --  BOM
+  --  Unlimited (e.g. only services)
+  IF(v_Quantity = 99999) THEN
+    RETURN 0;
+  END IF;
+  IF(v_Quantity > 0) THEN
+    --  Get Rounding Precision for Product
+    SELECT COALESCE(MAX(u.StdPrecision), 0)
+    INTO v_StdPrecision
+    FROM C_UOM u,
+      M_Product p
+    WHERE u.C_UOM_ID = p.C_UOM_ID
+      AND p.M_Product_ID = p_Product_ID;
+    --
+    RETURN ROUND(v_Quantity, v_StdPrecision) ;
+  END IF;
+  --
+  RETURN 0;
+END M_BOM_QTY_ORDERED
+]]></body>
+    </function>
+  </database>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src-db/database/model/functions/M_BOM_QTY_RESERVED.xml	Thu Oct 09 17:15:25 2008 +0000
@@ -0,0 +1,155 @@
+<?xml version="1.0"?>
+  <database name="FUNCTION M_BOM_QTY_RESERVED">
+    <function name="M_BOM_QTY_RESERVED" type="NUMERIC">
+      <parameter name="p_product_id" type="VARCHAR" mode="in">
+        <default/>
+      </parameter>
+      <parameter name="p_warehouse_id" type="VARCHAR" mode="in">
+        <default/>
+      </parameter>
+      <parameter name="p_locator_id" type="VARCHAR" mode="in">
+        <default><![CDATA[NULL]]></default>
+      </parameter>
+      <body><![CDATA[/*************************************************************************
+* The contents of this file are subject to the Compiere Public
+* License 1.1 ("License"); You may not use this file except in
+* compliance with the License. You may obtain a copy of the License in
+* the legal folder of your Openbravo installation.
+* 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  Compiere  ERP &  Business Solution
+* The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
+* Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke,
+* parts created by ComPiere are Copyright (C) ComPiere, Inc.;
+* All Rights Reserved.
+* Contributor(s): Openbravo SL
+* Contributions are Copyright (C) 2001-2008 Openbravo, S.L.
+*
+* Specifically, this derivative work is based upon the following Compiere
+* file and version.
+*************************************************************************
+* Return quantity reserved for BOM
+*/
+  v_Warehouse_ID VARCHAR2(32);
+  v_Quantity NUMBER:=99999; -- unlimited
+  v_IsBOM        CHAR(1) ;
+  v_IsStocked    CHAR(1) ;
+  v_ProductType  CHAR(1) ;
+  v_ProductQty   NUMBER;
+  v_StdPrecision NUMBER;
+  -- Get BOM Product info
+  TYPE RECORD IS REF CURSOR;
+    CUR_BOM RECORD;
+    --
+  BEGIN
+    -- Check Parameters
+    v_Warehouse_ID:=p_Warehouse_ID;
+    IF(v_Warehouse_ID IS NULL) THEN
+      IF(p_Locator_ID IS NULL) THEN
+        RETURN 0;
+      ELSE
+        SELECT MAX(M_Warehouse_ID)
+        INTO v_Warehouse_ID
+        FROM M_Locator
+        WHERE M_Locator_ID=p_Locator_ID;
+      END IF;
+    END IF;
+    IF(v_Warehouse_ID IS NULL) THEN
+      RETURN 0;
+    END IF;
+    -- DBMS_OUTPUT.PUT_LINE('Warehouse=' || v_Warehouse_ID);
+    -- Check, if product exists and if it is stocked
+  BEGIN
+    SELECT IsBOM,
+      ProductType,
+      IsStocked
+    INTO v_IsBOM,
+      v_ProductType,
+      v_IsStocked
+    FROM M_Product
+    WHERE M_Product_ID=p_Product_ID;
+    --
+  EXCEPTION -- not found
+  WHEN OTHERS THEN
+    RETURN 0;
+  END;
+  -- No reservation for non-stocked
+  IF((v_ProductType <> 'I' OR(v_IsStocked='N' AND v_IsBOM='N'))) THEN
+    RETURN 0;
+    -- Stocked item
+  ELSIF(v_IsStocked='Y') THEN
+    -- Get ProductQty
+    SELECT COALESCE(SUM(QtyReserved), 0)
+    INTO v_ProductQty
+    FROM M_Storage_Pending s
+    WHERE M_Product_ID=p_Product_ID
+      AND M_Warehouse_ID=v_Warehouse_ID;
+    --
+    RETURN v_ProductQty;
+  END IF;
+  -- Go though BOM
+  -- DBMS_OUTPUT.PUT_LINE('BOM');
+  FOR CUR_BOM IN
+    (SELECT b.M_ProductBOM_ID,
+      b.BOMQty,
+      p.IsBOM,
+      p.IsStocked,
+      p.ProductType
+    FROM M_Product_BOM b,
+      M_Product p
+    WHERE b.M_ProductBOM_ID=p.M_Product_ID
+      AND b.M_Product_ID=p_Product_ID
+    )
+  LOOP
+    -- Stocked Items "leaf node"
+    IF(CUR_BOM.ProductType='I' AND CUR_BOM.IsStocked='Y') THEN
+      -- Get ProductQty
+      SELECT COALESCE(SUM(QtyReserved), 0)
+      INTO v_ProductQty
+      FROM M_Storage_Pending s
+      WHERE M_Product_ID=CUR_BOM.M_ProductBOM_ID
+        AND M_Warehouse_ID=v_Warehouse_ID;
+      -- Get Rounding Precision
+      SELECT COALESCE(MAX(u.StdPrecision), 0)
+      INTO v_StdPrecision
+      FROM C_UOM u,
+        M_Product p
+      WHERE u.C_UOM_ID=p.C_UOM_ID
+        AND p.M_Product_ID=CUR_BOM.M_ProductBOM_ID;
+      -- How much can we make with this product
+      v_ProductQty:=ROUND(v_ProductQty/CUR_BOM.BOMQty, v_StdPrecision) ;
+      -- How much can we make overall
+      IF(v_ProductQty < v_Quantity) THEN
+        v_Quantity:=v_ProductQty;
+      END IF;
+      -- Another BOM
+    ELSIF(CUR_BOM.IsBOM='Y') THEN
+      v_ProductQty:=M_BOM_Qty_Reserved(CUR_BOM.M_ProductBOM_ID, v_Warehouse_ID) ;
+      -- How much can we make overall
+      IF(v_ProductQty < v_Quantity) THEN
+        v_Quantity:=v_ProductQty;
+      END IF;
+    END IF;
+  END LOOP; -- BOM
+  -- Unlimited (e.g. only services)
+  IF(v_Quantity=99999) THEN
+    RETURN 0;
+  END IF;
+  IF(v_Quantity > 0) THEN
+    -- Get Rounding Precision for Product
+    SELECT COALESCE(MAX(u.StdPrecision), 0)
+    INTO v_StdPrecision
+    FROM C_UOM u,
+      M_Product p
+    WHERE u.C_UOM_ID=p.C_UOM_ID
+      AND p.M_Product_ID=p_Product_ID;
+    --
+    RETURN ROUND(v_Quantity, v_StdPrecision) ;
+  END IF;
+  RETURN 0;
+END M_BOM_QTY_RESERVED
+]]></body>
+    </function>
+  </database>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src-db/database/model/functions/M_COPY_PRODUCT_TEMPLATE.xml	Thu Oct 09 17:15:25 2008 +0000
@@ -0,0 +1,185 @@
+<?xml version="1.0"?>
+  <database name="FUNCTION M_COPY_PRODUCT_TEMPLATE">
+    <function name="M_COPY_PRODUCT_TEMPLATE" type="NULL">
+      <parameter name="pinstance_id" type="VARCHAR" 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 VARCHAR2(32);
+  -- Parameter
+  TYPE RECORD IS REF CURSOR;
+    Cur_Parameter RECORD;
+    -- Record Info
+    v_Bpartner VARCHAR2(32);
+    v_Mpricelist VARCHAR2(32);
+    v_Currency VARCHAR2(32);
+    v_Warehouse VARCHAR2(32);
+    v_SeqNo NUMBER;
+    v_CorderId varchar2(32);
+    v_Client VARCHAR2(32);
+    v_Org VARCHAR2(32);
+    v_Createdby VARCHAR2(32);
+    v_Dateordered DATE;
+    v_BillTo NUMBER;
+    v_BpartnerLocation VARCHAR2(32);
+    v_Pricelist VARCHAR2(32);
+    v_Pricelimit NUMBER;
+    v_Pricestd NUMBER;
+    v_Linenetamount NUMBER;
+    v_Discount VARCHAR2(32);
+    v_Tax VARCHAR2(32);
+    v_Issotrx CHAR;
+    v_NoOfLines NUMBER:=0;
+    v_projectID varchar2(32);
+    -- 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 M_COPY_PRODUCT_TEMPLATE
+]]></body>
+    </function>
+  </database>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src-db/database/model/functions/M_GENERATE_AVERAGE_COSTS.xml	Thu Oct 09 17:15:25 2008 +0000
@@ -0,0 +1,226 @@
+<?xml version="1.0"?>
+  <database name="FUNCTION M_GENERATE_AVERAGE_COSTS">
+    <function name="M_GENERATE_AVERAGE_COSTS" type="NULL">
+      <parameter name="p_pinstance_id" type="VARCHAR" 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-2008 Openbravo SL
+* All Rights Reserved.
+* Contributor(s):  ______________________________________.
+************************************************************************/
+  -- Logistice
+  v_ResultStr VARCHAR2(2000):='';
+  v_Result NUMBER;
+  v_Message VARCHAR2(2000):='';
+  v_Record_ID VARCHAR2(32);
+  v_Date DATE;
+  v_Qty NUMBER;
+  v_Cost NUMBER;
+  v_User VARCHAR2(32);
+  v_Price NUMBER;
+  v_DateOld DATE;
+  v_CumQty NUMBER;
+  v_Processed CHAR(1) ;
+  v_count NUMBER;
+  v_Deleted NUMBER;
+  v_ProductOld VARCHAR2(32);
+  -- Parameter
+  TYPE RECORD IS REF CURSOR;
+    Cur_Parameter RECORD;
+    Cur_InOutLine RECORD;
+    Cur_ProdCost RECORD;
+    Cur_PriceList RECORD;
+    v_rowcount NUMBER;
+  BEGIN
+    v_Qty:=0;
+    v_Cost:=0;
+    v_Price:=0;
+    v_CumQty:=0;
+    v_ProductOld:='0';
+    --  Update AD_PInstance
+    DBMS_OUTPUT.PUT_LINE('Updating PInstance - Processing ' || p_PInstance_ID) ;
+    v_ResultStr:='PInstanceNotFound';
+    AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'Y', NULL, NULL) ;
+  BEGIN --BODY
+    --  Get Parameters
+    v_ResultStr:='ReadingParameters';
+    FOR Cur_Parameter IN
+      (SELECT i.Record_ID,
+        i.AD_User_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=p_PInstance_ID
+      ORDER BY p.SeqNo
+      )
+    LOOP
+      v_Record_ID:=Cur_Parameter.Record_ID;
+      v_User:=Cur_Parameter.AD_User_ID;
+    END LOOP; --  Get Parameter
+    DELETE
+    FROM M_COSTING
+    WHERE ISPERMANENT='N'
+      AND ISMANUAL='N'
+      AND COSTTYPE='AV'
+      AND ISPRODUCTION = 'N';
+    v_rowcount:=SQL%ROWCOUNT;
+    v_Deleted:=v_Deleted + v_rowcount;
+    v_rowcount:=SQL%ROWCOUNT;
+    DBMS_OUTPUT.PUT_LINE('  deleted=' || v_rowcount) ;
+    v_DateOld:=TO_DATE('01-01-1900', 'DD-MM-YYYY') ;
+    FOR Cur_InOutLine IN
+      (SELECT M_INOUTLINE.M_INOUTLINE_ID, M_INOUTLINE.MOVEMENTQTY, M_PRODUCT.M_PRODUCT_ID, M_INOUT.MOVEMENTDATE,
+        M_INOUTLINE.AD_CLIENT_ID, M_INOUTLINE.AD_ORG_ID, M_INOUT.C_BPARTNER_ID
+      FROM M_INOUT, M_INOUTLINE, M_PRODUCT
+      WHERE M_INOUT.M_INOUT_ID=M_INOUTLINE.M_INOUT_ID
+        AND M_INOUTLINE.M_PRODUCT_ID=M_PRODUCT.M_PRODUCT_ID
+        AND M_PRODUCT.COSTTYPE='AV'
+        AND M_INOUT.ISSOTRX='N'
+        AND M_INOUT.PROCESSED='Y'
+        AND M_INOUTLINE.MOVEMENTQTY>0
+        AND M_INOUT.DOCSTATUS <> 'DR' AND M_INOUT.DOCSTATUS <> 'RE'
+        AND NOT EXISTS
+        (SELECT 1 FROM M_COSTING WHERE M_INOUTLINE_ID=M_INOUTLINE.M_INOUTLINE_ID)
+      ORDER BY M_PRODUCT.M_PRODUCT_ID, M_INOUT.MOVEMENTDATE, M_INOUTLINE.MOVEMENTQTY DESC
+      )
+    LOOP
+      v_ResultStr:='new albaran:   '||Cur_InOutLine.M_INOUTLINE_ID;
+      IF(Cur_InOutLine.MOVEMENTDATE<>v_DateOld OR Cur_InOutLine.M_PRODUCT_ID<>v_ProductOld) THEN
+        v_CumQty:=0;
+      END IF;
+      SELECT COUNT(*)
+      INTO v_Count
+      FROM M_COSTING
+      WHERE M_PRODUCT_ID=Cur_InOutLine.M_PRODUCT_ID;
+      IF(v_Count>0) THEN
+        FOR Cur_ProdCost IN
+          (SELECT COST, DATETO, M_COSTING_ID
+          FROM M_COSTING
+          WHERE M_PRODUCT_ID=Cur_InOutLine.M_PRODUCT_ID
+            AND DATEFROM <= Cur_InOutLine.MOVEMENTDATE
+          ORDER BY DATETO DESC, M_COSTING_ID
+          )
+        LOOP
+          v_Cost:=Cur_ProdCost.COST;
+          v_Date:=Cur_ProdCost.DATETO;
+          UPDATE M_COSTING
+          SET DATETO = Cur_InOutLine.MOVEMENTDATE
+          WHERE M_COSTING_ID = Cur_ProdCost.M_COSTING_ID;
+          EXIT;
+        END LOOP;
+      ELSE
+        v_Cost:=NULL;
+        v_Date := TO_DATE('31-12-9999', 'DD-MM-YYYY');
+      END IF;
+      IF(v_Cost IS NOT NULL) THEN
+        SELECT COALESCE(SUM(MOVEMENTQTY), 0)
+        INTO v_Qty
+        FROM M_TRANSACTION
+        WHERE M_PRODUCT_ID=Cur_InOutLine.M_PRODUCT_ID
+          AND MOVEMENTDATE<Cur_InOutLine.MOVEMENTDATE;
+      ELSE
+        v_Qty:=0;
+      END IF;
+      v_ResultStr:='invoice albaran:   '||Cur_InOutLine.M_INOUTLINE_ID;
+      SELECT CASE WHEN SUM(QTYINVOICED) <=0 THEN 0 ELSE SUM(C_INVOICELINE.QTYINVOICED*C_INVOICELINE.PRICEACTUAL) /SUM(QTYINVOICED) END,
+             CASE WHEN SUM(QTYINVOICED) <=0 THEN 0 ELSE COUNT(C_INVOICELINE.C_INVOICELINE_ID) END
+      INTO v_Price, v_Count
+      FROM M_MATCHINV, C_INVOICELINE
+      WHERE M_MATCHINV.C_INVOICELINE_ID=C_INVOICELINE.C_INVOICELINE_ID
+        AND M_MATCHINV.M_INOUTLINE_ID=Cur_InOutLine.M_INOUTLINE_ID
+        AND QTYINVOICED<>0;
+      IF(v_Count=0) THEN
+        v_ResultStr:='order albaran:   '||Cur_InOutLine.M_INOUTLINE_ID;
+        SELECT CASE WHEN SUM(C_ORDERLINE.QTYORDERED) <=0 THEN 0 ELSE SUM(C_ORDERLINE.QTYORDERED*C_ORDERLINE.PRICEACTUAL) /SUM(C_ORDERLINE.QTYORDERED) END,
+               CASE WHEN SUM(C_ORDERLINE.QTYORDERED) <=0 THEN 0 ELSE COUNT(C_ORDERLINE.C_ORDERLINE_ID) END
+        INTO v_Price, v_Count
+        FROM M_MATCHPO, C_ORDERLINE
+        WHERE M_MATCHPO.C_ORDERLINE_ID=C_ORDERLINE.C_ORDERLINE_ID
+          AND M_MATCHPO.C_INVOICELINE_ID IS NULL
+          AND M_MATCHPO.M_INOUTLINE_ID=Cur_InOutLine.M_INOUTLINE_ID
+          AND C_ORDERLINE.QTYORDERED<>0;
+
+      END IF;
+      IF(v_Count=0) THEN
+        FOR Cur_PriceList IN
+          (SELECT M_PRODUCTPRICE.PRICESTD
+          FROM M_PRICELIST_VERSION, M_PRODUCTPRICE
+          WHERE M_PRICELIST_VERSION.M_PRICELIST_ID=
+            (SELECT po_pricelist_id
+            FROM C_BPARTNER
+            WHERE c_bpartner_id=Cur_InOutLine.C_BPARTNER_ID
+            )
+            AND M_PRICELIST_VERSION.M_PRICELIST_VERSION_ID=M_PRODUCTPRICE.M_PRICELIST_VERSION_ID
+            AND M_PRICELIST_VERSION.ISACTIVE='Y'
+            AND M_PRODUCTPRICE.M_PRODUCT_ID=Cur_InOutLine.M_PRODUCT_ID
+            AND M_PRICELIST_VERSION.VALIDFROM<Cur_InOutLine.MOVEMENTDATE
+          ORDER BY M_PRICELIST_VERSION.VALIDFROM DESC
+          )
+        LOOP
+          v_Price:=Cur_PriceList.PRICESTD;
+          EXIT;
+        END LOOP;
+      END IF;
+      v_ResultStr:='insert costing:   '||Cur_InOutLine.M_INOUTLINE_ID;
+      INSERT
+      INTO M_COSTING
+        (
+          M_COSTING_ID, CREATED, CREATEDBY, UPDATED,
+          UPDATEDBY, AD_CLIENT_ID, AD_ORG_ID, M_PRODUCT_ID,
+          DATETO, DATEFROM, ISMANUAL, M_INOUTLINE_ID,
+          C_INVOICELINE_ID, QTY, PRICE, CUMQTY,
+          COST, COSTTYPE, ISPERMANENT
+        )
+        VALUES
+        (
+          Ad_Sequence_Nextno('M_Costing'), now(), v_User, now(),
+          v_User, Cur_InOutLine.AD_CLIENT_ID, Cur_InOutLine.AD_ORG_ID, Cur_InOutLine.M_PRODUCT_ID,
+          v_Date, Cur_InOutLine.MOVEMENTDATE, 'N', Cur_InOutLine.M_INOUTLINE_ID,
+          NULL, Cur_InOutLine.MOVEMENTQTY, v_Price, COALESCE(v_Qty, 0) + Cur_InOutLine.MOVEMENTQTY + v_CumQty,(
+          CASE
+            WHEN v_Qty+v_CumQty<0 THEN v_Price
+            WHEN (v_Qty+Cur_InOutLine.MOVEMENTQTY+v_CumQty) = 0 THEN 0
+            ELSE (((v_Qty+v_CumQty)*COALESCE(v_Cost, v_Price)) +(Cur_InOutLine.MOVEMENTQTY *v_Price)) /(v_Qty+Cur_InOutLine.MOVEMENTQTY+v_CumQty) END),
+          'AV', 'N'
+        )
+        ;
+      IF(Cur_InOutLine.MOVEMENTDATE=v_DateOld AND Cur_InOutLine.M_PRODUCT_ID=v_ProductOld) THEN
+        v_CumQty:=v_CumQty + Cur_InOutLine.MOVEMENTQTY;
+      END IF;
+      v_DateOld:=Cur_InOutLine.MOVEMENTDATE;
+      v_ProductOld:=Cur_InOutLine.M_PRODUCT_ID;
+    END LOOP;
+    --<<FINISH_PROCESS>>
+    --  Update AD_PInstance
+    DBMS_OUTPUT.PUT_LINE('Updating PInstance - Finished ' || v_Message);
+    AD_UPDATE_PINSTANCE(p_PInstance_ID, v_User, 'N', 1, v_Message);
+    RETURN;
+  END; --BODY
+EXCEPTION
+WHEN OTHERS THEN
+  ROLLBACK;
+  DBMS_OUTPUT.PUT_LINE(v_ResultStr);
+  v_ResultStr:= '@ERROR=' || SQLERRM;
+  DBMS_OUTPUT.PUT_LINE(v_ResultStr);
+  AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', 0, v_ResultStr);
+  RETURN;
+END M_GENERATE_AVERAGE_COSTS
+]]></body>
+    </function>
+  </database>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src-db/database/model/functions/M_GENERATE_STANDARD_COSTS.xml	Thu Oct 09 17:15:25 2008 +0000
@@ -0,0 +1,134 @@
+<?xml version="1.0"?>
+  <database name="FUNCTION M_GENERATE_STANDARD_COSTS">
+    <function name="M_GENERATE_STANDARD_COSTS" type="NULL">
+      <parameter name="p_pinstance_id" type="VARCHAR" mode="in">
+        <default/>
+      </parameter>
+      <parameter name="p_date" type="TIMESTAMP" mode="in">
+        <default/>
+      </parameter>
+      <parameter name="p_products" type="CHAR" 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_Result NUMBER;
+  v_Message VARCHAR2(2000):='';
+  v_Record_ID VARCHAR2(32);
+  v_User VARCHAR2(32);
+  v_Deleted NUMBER;
+  -- Parameter
+  TYPE RECORD IS REF CURSOR;
+    Cur_Parameter RECORD;
+    Cur_PriceList record;
+    v_rowcount NUMBER;
+  BEGIN
+    --  Update AD_PInstance
+    DBMS_OUTPUT.PUT_LINE('Updating PInstance - Processing ' || p_PInstance_ID) ;
+    v_ResultStr:='PInstanceNotFound';
+    AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'Y', NULL, NULL) ;
+  BEGIN --BODY
+    --  Get Parameters
+    v_ResultStr:='ReadingParameters';
+    FOR Cur_Parameter in
+      (SELECT i.Record_ID,
+        i.AD_User_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=p_PInstance_ID
+      ORDER BY p.SeqNo
+      )
+    LOOP
+      v_Record_ID:=Cur_Parameter.Record_ID;
+      v_User:=Cur_Parameter.AD_User_ID;
+    END LOOP; --  Get Parameter
+    DELETE
+    FROM M_COSTING
+    WHERE ISPERMANENT='N'
+      AND ISMANUAL='N'
+      AND COSTTYPE='ST';
+    v_rowcount:=SQL%ROWCOUNT;
+    v_Deleted:=v_Deleted + v_rowcount;
+    DBMS_OUTPUT.PUT_LINE('  deleted=' || v_rowcount) ;
+    FOR Cur_PriceList in
+      (SELECT COALESCE(M_PRODUCT.COSTSTD, 0) AS COSTSTD,
+        M_PRODUCT.M_PRODUCT_ID,
+        M_PRODUCT.AD_CLIENT_ID,
+        M_PRODUCT.AD_ORG_ID
+      FROM M_PRODUCT
+      WHERE M_PRODUCT_ID IN
+        (SELECT M_PRODUCT_ID FROM M_PRODUCT  WHERE COSTTYPE='ST')
+        AND M_PRODUCT_ID IN(p_Products)
+        AND NOT EXISTS
+        (SELECT 1
+        FROM M_COSTING
+        WHERE COSTTYPE='ST'
+          AND M_PRODUCT_ID=M_PRODUCT.M_PRODUCT_ID
+          AND DATEFROM=p_Date
+          AND PRICE=M_PRODUCT.COSTSTD
+          AND COST=M_PRODUCT.COSTSTD
+        )
+      )
+    LOOP
+      UPDATE M_COSTING
+        SET DATETO=p_Date
+      WHERE DATETO=TO_DATE('31-12-9999', 'DD-MM-YYYY')
+        AND M_PRODUCT_ID=Cur_PriceList.M_PRODUCT_ID
+        AND COSTTYPE='ST';
+      INSERT
+      INTO M_COSTING
+        (
+          M_COSTING_ID, CREATED, CREATEDBY, UPDATED,
+          UPDATEDBY, AD_CLIENT_ID, AD_ORG_ID, M_PRODUCT_ID,
+          DATETO, DATEFROM, ISMANUAL, M_INOUTLINE_ID,
+          C_INVOICELINE_ID, QTY, PRICE, CUMQTY,
+          COST, COSTTYPE, ISPERMANENT
+        )
+        VALUES
+        (
+          Ad_Sequence_Nextno('M_Costing'), now(), v_User, now(),
+          v_User, Cur_PriceList.AD_CLIENT_ID, Cur_PriceList.AD_ORG_ID, Cur_PriceList.M_PRODUCT_ID,
+          TO_DATE('31-12-9999', 'DD-MM-YYYY'), p_Date, 'N', NULL,
+          NULL, 0, Cur_PriceList.COSTSTD, 0,
+          Cur_PriceList.COSTSTD, 'ST', 'N'
+        )
+        ;
+    END LOOP;
+    --<<FINISH_PROCESS>>
+    --  Update AD_PInstance
+    DBMS_OUTPUT.PUT_LINE('Updating PInstance - Finished ' || v_Message) ;
+    AD_UPDATE_PINSTANCE(p_PInstance_ID, v_User, 'N', 1, v_Message) ;
+    RETURN;
+  END; --BODY
+EXCEPTION
+WHEN OTHERS THEN
+  ROLLBACK;
+  v_ResultStr:= '@ERROR=' || SQLERRM;
+  DBMS_OUTPUT.PUT_LINE(v_ResultStr) ;
+  AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', 0, v_ResultStr) ;
+  RETURN;
+END M_GENERATE_STANDARD_COSTS
+]]></body>
+    </function>
+  </database>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src-db/database/model/functions/M_GET_PARETO_ABC.xml	Thu Oct 09 17:15:25 2008 +0000
@@ -0,0 +1,94 @@
+<?xml version="1.0"?>
+  <database name="FUNCTION M_GET_PARETO_ABC">
+    <function name="M_GET_PARETO_ABC" type="VARCHAR">
+      <parameter name="p_warehouse_id" type="VARCHAR" mode="in">
+        <default/>
+      </parameter>
+      <parameter name="p_org_id" type="VARCHAR" mode="in">
+        <default/>
+      </parameter>
+      <parameter name="p_client_id" type="VARCHAR" mode="in">
+        <default/>
+      </parameter>
+      <parameter name="p_percentageactual" 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-2008 Openbravo SL
+* All Rights Reserved.
+* Contributor(s):  ______________________________________.
+************************************************************************/
+
+  v_orgList VARCHAR2(4000) ;
+  VARaCUM NUMBER:=0;
+  VARaCUMB NUMBER:=0;
+  v_limitA NUMBER:=80;
+  v_limitB NUMBER:=15;
+  varComprobadoA NUMBER:=0;
+  varComprobadoB NUMBER:=0;
+  TYPE RECORD IS REF CURSOR;
+  Cur_Cursor RECORD;
+BEGIN
+  varAcum :=0;
+  varComprobadoA := 0;
+  for Cur_Cursor in (
+   SELECT
+  100*(M_GET_PRODUCT_COST(PR.M_PRODUCT_ID,TO_DATE(now()),'AV')/ (select SUM(M_GET_PRODUCT_COST(PR1.M_PRODUCT_ID,TO_DATE(now()),'AV'))
+                                                       from M_WAREHOUSE MW1
+                                                       LEFT JOIN M_LOCATOR ML1 ON ML1.M_WAREHOUSE_ID=MW1.M_WAREHOUSE_ID
+                                                       LEFT JOIN M_STORAGE_DETAIL MSD1 ON ML1.M_LOCATOR_ID=MSD1.M_LOCATOR_ID
+                                                       LEFT JOIN M_PRODUCT PR1 ON MSD1.M_PRODUCT_ID=PR1.M_PRODUCT_ID
+                                                       WHERE (p_warehouse_ID IS NULL OR MW1.M_WAREHOUSE_ID=p_warehouse_ID)
+                                                       AND (p_org_ID IS NULL OR MW1.AD_ORG_ID=p_org_ID)
+                                                       AND (p_client_id IS NULL OR MW1.AD_CLIENT_ID=p_client_id) 
+                                                       AND MSD1.QTYONHAND>0) ) as PERCENTAGE
+  FROM
+   M_WAREHOUSE MW
+     LEFT JOIN M_LOCATOR ML ON ML.M_WAREHOUSE_ID=MW.M_WAREHOUSE_ID
+     LEFT JOIN M_STORAGE_DETAIL MSD ON ML.M_LOCATOR_ID=MSD.M_LOCATOR_ID
+     LEFT JOIN M_PRODUCT PR ON MSD.M_PRODUCT_ID=PR.M_PRODUCT_ID
+  WHERE (p_warehouse_ID IS NULL OR MW.M_WAREHOUSE_ID=p_warehouse_ID)
+     AND (p_org_ID IS NULL OR MW.AD_ORG_ID=p_org_ID)
+     AND (p_client_id IS NULL OR MW.AD_CLIENT_ID=p_client_id)
+     AND MSD.QTYONHAND>0
+     AND M_GET_PRODUCT_COST(PR.M_PRODUCT_ID,TO_DATE(now()),'AV') IS NOT NULL
+     AND M_GET_PRODUCT_COST(PR.M_PRODUCT_ID,TO_DATE(now()),'AV') <> 0
+  ORDER BY PERCENTAGE DESC) loop
+  varAcum := varAcum+Cur_Cursor.percentage;
+   if(varComprobadoA=0)then
+     if (varAcum>=v_limitA) then
+       if (p_percentageactual>=Cur_Cursor.percentage) then
+	     return 'A';
+       else
+         varComprobadoA:=-1;
+         varAcumB:=-1*Cur_Cursor.percentage;
+       end if;
+     end if;
+   end if;
+
+  if (varComprobadoA=-1 and varComprobadoB=0) then
+  varAcumB:=varAcumB+Cur_Cursor.percentage;
+   if (varAcumB>=v_limitB) then
+     if (p_percentageactual>=Cur_Cursor.percentage) then return 'B';
+	 else varComprobadoB:=-1;
+     end if;
+   end if;
+  end if;
+
+ end loop;
+ return 'C';
+END M_GET_PARETO_ABC
+]]></body>
+    </function>
+  </database>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src-db/database/model/functions/M_GET_PRICELIST_VERSION.xml	Thu Oct 09 17:15:25 2008 +0000
@@ -0,0 +1,48 @@
+<?xml version="1.0"?>
+  <database name="FUNCTION M_GET_PRICELIST_VERSION">
+    <function name="M_GET_PRICELIST_VERSION" type="VARCHAR">
+      <parameter name="p_pricelist_id" type="VARCHAR" mode="in">
+        <default/>
+      </parameter>
+      <parameter name="p_date" type="TIMESTAMP" 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):  ______________________________________.
+************************************************************************/
+  v_M_PriceList_Version_ID VARCHAR2(32);
+  TYPE RECORD IS REF CURSOR;
+    Cur_Pricelist_Version RECORD;
+  BEGIN
+    FOR Cur_Pricelist_Version IN
+      (SELECT PLV.M_PRICELIST_VERSION_ID AS PRICELISTVERSION
+      FROM M_PRICELIST PL,
+        M_PRICELIST_VERSION PLV
+      WHERE PL.M_PRICELIST_ID = PLV.M_PRICELIST_ID
+        AND PLV.ISACTIVE = 'Y'
+        AND PL.M_PRICELIST_ID = p_Pricelist_Id
+        AND PLV.VALIDFROM <= p_Date
+      ORDER BY PLV.VALIDFROM DESC
+      )
+    LOOP
+      v_M_PriceList_Version_ID := Cur_Pricelist_Version.PRICELISTVERSION;
+      EXIT;
+    END LOOP;
+    RETURN v_M_PriceList_Version_ID;
+END M_GET_PRICELIST_VERSION
+]]></body>
+    </function>
+  </database>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src-db/database/model/functions/M_GET_PRODUCT_COST.xml	Thu Oct 09 17:15:25 2008 +0000
@@ -0,0 +1,77 @@
+<?xml version="1.0"?>
+  <database name="FUNCTION M_GET_PRODUCT_COST">
+    <function name="M_GET_PRODUCT_COST" type="NUMERIC">
+      <parameter name="p_product_id" type="VARCHAR" mode="in">
+        <default/>
+      </parameter>
+      <parameter name="p_movementdate" type="TIMESTAMP" mode="in">
+        <default/>
+      </parameter>
+      <parameter name="p_costtype" type="CHAR" 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):  ______________________________________.
+************************************************************************/
+  v_Cost     NUMBER;
+  v_Count    NUMBER;
+  v_CostType CHAR(2) ;
+  TYPE RECORD IS REF CURSOR;
+    Cur_Cost RECORD;
+  BEGIN
+    v_CostType := p_CostType;
+    IF(p_Product_ID IS NULL) THEN
+      RETURN 0;
+    END IF;
+    IF(p_MovementDate IS NULL) THEN
+      RETURN 0;
+    END IF;
+    /*IF (v_CostType IS NULL) THEN
+    SELECT  COSTTYPE INTO v_CostType
+    FROM M_PRODUCT
+    WHERE M_PRODUCT_ID=p_Product_ID;
+    END IF;*/
+    SELECT COUNT(*)
+    INTO v_Count
+    FROM M_COSTING
+    WHERE p_MovementDate BETWEEN DATEFROM AND DATETO
+      AND M_PRODUCT_ID = p_Product_ID
+      AND(v_CostType IS NULL
+      OR v_CostType = COSTTYPE) ;
+    IF(v_Count = 0) THEN
+      RETURN 0;
+    ELSE
+      FOR Cur_Cost IN
+        (SELECT COST
+        FROM M_COSTING
+        WHERE DATEFROM < p_MovementDate
+          AND DATETO >= p_MovementDate
+          AND M_PRODUCT_ID = p_Product_ID
+          AND(v_CostType IS NULL
+          OR v_CostType = COSTTYPE)
+        ORDER BY DATEFROM,
+          DATETO DESC
+        )
+      LOOP
+        v_Cost := Cur_Cost.COST;
+        EXIT;
+      END LOOP;
+      RETURN v_Cost;
+    END IF;
+END M_GET_PRODUCT_COST
+]]></body>
+    </function>
+  </database>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src-db/database/model/functions/M_GET_PRODUCT_PRICE.xml	Thu Oct 09 17:15:25 2008 +0000
@@ -0,0 +1,69 @@
+<?xml version="1.0"?>
+  <database name="FUNCTION M_GET_PRODUCT_PRICE">
+    <function name="M_GET_PRODUCT_PRICE" type="NUMERIC">
+      <parameter name="p_ad_client_id" type="VARCHAR" mode="in">
+        <default/>
+      </parameter>
+      <parameter name="p_m_product_id" type="VARCHAR" 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):  ______________________________________.
+************************************************************************/
+  v_M_PriceList_Version_ID VARCHAR2(32);
+  v_Price                  NUMBER;
+BEGIN
+  SELECT M_PRICELIST_VERSION_ID
+  INTO v_M_PriceList_Version_ID
+  FROM M_PRICELIST_VERSION
+  WHERE M_PRICELIST_ID IN
+    (SELECT M_PRICELIST_ID
+    FROM M_PRICELIST
+    WHERE ISDEFAULT = 'Y'
+      AND AD_CLIENT_ID = p_AD_Client_ID
+      AND ISACTIVE = 'Y'
+    )
+    AND VALIDFROM =
+    (SELECT max(VALIDFROM)
+    FROM M_PRICELIST_VERSION
+    WHERE M_PRICELIST_ID IN
+      (SELECT M_PRICELIST_ID
+      FROM M_PRICELIST
+      WHERE ISDEFAULT = 'Y'
+        AND AD_CLIENT_ID = p_AD_Client_ID
+        AND ISACTIVE = 'Y'
+      )
+      AND VALIDFROM <= now()
+    )
+    ;
+  IF(v_M_PriceList_Version_ID IS NULL) THEN
+    RETURN 0;
+  ELSE
+    SELECT COALESCE(PRICESTD, 0)
+    INTO v_Price
+    FROM M_PRODUCTPRICE
+    WHERE M_PRICELIST_VERSION_ID = v_M_PriceList_Version_ID
+      AND AD_CLIENT_ID = p_AD_Client_ID
+      AND M_PRODUCT_ID = p_M_Product_ID;
+  END IF;
+  IF v_Price IS NULL THEN
+    v_Price := 0;
+  END IF;
+  RETURN v_Price;
+END M_GET_PRODUCT_PRICE
+]]></body>
+    </function>
+  </database>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src-db/database/model/functions/M_GET_PRODUCT_PRICE_DATE.xml	Thu Oct 09 17:15:25 2008 +0000
@@ -0,0 +1,80 @@
+<?xml version="1.0"?>
+  <database name="FUNCTION M_GET_PRODUCT_PRICE_DATE">
+    <function name="M_GET_PRODUCT_PRICE_DATE" type="NUMERIC">
+      <parameter name="p_ad_client_id" type="VARCHAR" mode="in">
+        <default/>
+      </parameter>
+      <parameter name="p_m_product_id" type="VARCHAR" mode="in">
+        <default/>
+      </parameter>
+      <parameter name="p_date" type="TIMESTAMP" 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) 2007 Openbravo SL
+* All Rights Reserved.
+* Contributor(s):  ______________________________________.
+************************************************************************/
+  v_M_PriceList_Version_ID VARCHAR2(32);
+  v_Price                  NUMBER;
+BEGIN
+  SELECT MAX(M_PRICELIST_VERSION_ID)
+  INTO v_M_PriceList_Version_ID
+  FROM M_PRICELIST_VERSION
+  WHERE M_PRICELIST_ID IN
+    (SELECT M_PRICELIST_ID
+    FROM M_PRICELIST
+    WHERE ISDEFAULT = 'Y'
+      AND AD_CLIENT_ID = p_AD_Client_ID
+      AND ISACTIVE = 'Y'
+    )
+    AND VALIDFROM =
+    (SELECT max(VALIDFROM)
+    FROM M_PRICELIST_VERSION
+    WHERE M_PRICELIST_ID IN
+      (SELECT M_PRICELIST_ID
+      FROM M_PRICELIST
+      WHERE ISDEFAULT = 'Y'
+        AND AD_CLIENT_ID = p_AD_Client_ID
+        AND ISACTIVE = 'Y'
+      )
+      AND TO_DATE(VALIDFROM) <= TO_DATE(COALESCE(p_Date, now()))
+      AND EXISTS(SELECT 1
+                 FROM M_PRODUCTPRICE
+                 WHERE M_PRICELIST_VERSION_ID = M_PRICELIST_VERSION.M_PRICELIST_VERSION_ID
+                   AND M_PRODUCT_ID = p_M_Product_ID)
+    )
+    AND EXISTS(SELECT 1
+                 FROM M_PRODUCTPRICE
+                 WHERE M_PRICELIST_VERSION_ID = M_PRICELIST_VERSION.M_PRICELIST_VERSION_ID
+                   AND M_PRODUCT_ID = p_M_Product_ID)
+    ;
+  IF(v_M_PriceList_Version_ID IS NULL) THEN
+    RETURN 0;
+  ELSE
+    SELECT COALESCE(PRICESTD, 0)
+    INTO v_Price
+    FROM M_PRODUCTPRICE
+    WHERE M_PRICELIST_VERSION_ID = v_M_PriceList_Version_ID
+      AND AD_CLIENT_ID = p_AD_Client_ID
+      AND M_PRODUCT_ID = p_M_Product_ID;
+  END IF;
+  IF v_Price IS NULL THEN
+    v_Price := 0;
+  END IF;
+  RETURN v_Price;
+END M_GET_PRODUCT_PRICE_DATE
+]]></body>
+    </function>
+  </database>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src-db/database/model/functions/M_GET_PURCHASE_PRICE.xml	Thu Oct 09 17:15:25 2008 +0000
@@ -0,0 +1,92 @@
+<?xml version="1.0"?>
+  <database name="FUNCTION M_GET_PURCHASE_PRICE">
+    <function name="M_GET_PURCHASE_PRICE" type="NUMERIC">
+      <parameter name="p_product_id" type="VARCHAR" 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):  ______________________________________.
+************************************************************************/
+  v_Price NUMBER;
+  TYPE RECORD IS REF CURSOR;
+    Cur_Orderline RECORD;
+    Cur_Invoiceline RECORD;
+    Cur_Pricelist_Version RECORD;
+  BEGIN
+    FOR Cur_Invoiceline IN
+      (SELECT COALESCE(C_INVOICELINE.PRICEACTUAL, 0) AS Price
+      FROM C_INVOICELINE,
+        C_INVOICE
+      WHERE C_INVOICELINE.C_INVOICE_ID = C_INVOICE.C_INVOICE_ID
+        AND C_INVOICE.ISSOTRX = 'N'
+        AND C_INVOICE.DOCSTATUS = 'CO'
+        AND C_INVOICE.PROCESSED = 'Y'
+        AND C_INVOICELINE.M_PRODUCT_ID = p_Product_ID
+      ORDER BY C_INVOICE.DATEINVOICED DESC
+      )
+    LOOP
+      v_Price := Cur_Invoiceline.PRICE;
+      EXIT;
+    END LOOP;
+    IF(v_Price IS NULL) THEN
+      FOR Cur_Orderline IN
+        (SELECT COALESCE(C_ORDERLINE.PRICEACTUAL, 0) AS Price
+        FROM C_ORDERLINE,
+          C_ORDER
+        WHERE C_ORDERLINE.C_ORDER_ID = C_ORDER.C_ORDER_ID
+          AND C_ORDER.ISSOTRX = 'N'
+          AND C_ORDER.DOCSTATUS = 'CO'
+          AND C_ORDER.PROCESSED = 'Y'
+          AND C_ORDERLINE.M_PRODUCT_ID = p_Product_ID
+        ORDER BY C_ORDER.DATEORDERED DESC
+        )
+      LOOP
+        v_Price := Cur_Orderline.PRICE;
+        EXIT;
+      END LOOP;
+    END IF;
+    IF(v_Price IS NULL) THEN
+      FOR Cur_Pricelist_Version IN
+        (SELECT COALESCE(M_PRODUCTPRICE.PRICESTD, 0) AS Price
+        FROM M_PRODUCTPRICE,
+          M_PRICELIST_VERSION,
+          M_PRICELIST
+        WHERE M_PRODUCTPRICE.M_PRICELIST_VERSION_ID = M_PRICELIST_VERSION.M_PRICELIST_VERSION_ID
+          AND M_PRICELIST_VERSION.M_PRICELIST_ID = M_PRICELIST.M_PRICELIST_ID
+          AND M_PRICELIST.ISSOPRICELIST = 'N'  --  AND M_PRICELIST_VERSION.ISACTIVE= 'Y'
+          AND M_PRICELIST_VERSION.VALIDFROM < now()
+          AND M_PRODUCTPRICE.M_PRODUCT_ID = p_Product_ID
+        ORDER BY M_PRICELIST_VERSION.VALIDFROM DESC
+        )
+      LOOP
+        v_Price := Cur_Pricelist_Version.PRICE;
+        EXIT;
+      END LOOP;
+    END IF;
+    IF(v_Price IS NULL) THEN
+      SELECT COALESCE(M_PRODUCT_PO.PRICEPO, 0)
+      INTO v_Price
+      FROM M_PRODUCT_PO
+      WHERE M_PRODUCT_PO.M_PRODUCT_ID = p_Product_ID;
+    END IF;
+    IF(v_Price IS NULL) THEN
+      v_Price := 0;
+    END IF;
+    RETURN v_Price;
+END M_GET_PURCHASE_PRICE
+]]></body>
+    </function>
+  </database>
--- a/src-db/database/model/functions/M_PRODUCTION_RUN.xml	Thu Oct 09 14:13:04 2008 +0000
+++ b/src-db/database/model/functions/M_PRODUCTION_RUN.xml	Thu Oct 09 17:15:25 2008 +0000
@@ -449,9 +449,9 @@
             LOOP
               -- M_ProductionLine_ID, AD_Client_ID, AD_Org_ID, MovementDate, M_Product_ID, MovementQty, M_Locator_ID
               -- DBMS_OUTPUT.PUT_LINE('ProductionLine=' || pl.M_ProductionLine_ID);
-              -- DBMS_OUTPUT.PUT_LINE('  Qty=' || pl.MovementQty || ', OnHand=' || BOM_Qty_OnHand(pl.M_Product_ID, NULL, pl.M_Locator_ID));
+              -- DBMS_OUTPUT.PUT_LINE('  Qty=' || pl.MovementQty || ', OnHand=' || M_BOM_Qty_OnHand(pl.M_Product_ID, NULL, pl.M_Locator_ID));
               -- Check Stock levels for reductions
-              IF(CUR_PL_Post.MovementQty<0 AND MustBeStocked<>'N'  AND Bom_Qty_Onhand(CUR_PL_Post.M_Product_ID, NULL, CUR_PL_Post.M_Locator_ID) +CUR_PL_Post.MovementQty<0) THEN
+              IF(CUR_PL_Post.MovementQty<0 AND MustBeStocked<>'N'  AND M_BOM_Qty_OnHand(CUR_PL_Post.M_Product_ID, NULL, CUR_PL_Post.M_Locator_ID) +CUR_PL_Post.MovementQty<0) THEN
                 ROLLBACK;
                 SELECT '@NotEnoughStocked@: ' || Name
                 INTO v_Message
--- a/src-db/database/model/functions/M_REQUISITION_CREATEPO.xml	Thu Oct 09 14:13:04 2008 +0000
+++ b/src-db/database/model/functions/M_REQUISITION_CREATEPO.xml	Thu Oct 09 17:15:25 2008 +0000
@@ -223,7 +223,7 @@
              COALESCE(MAX(M_PRODUCTPRICE.PRICELIMIT), Cur_Lines.PRICEACTUAL) AS PRICELIMIT,
              COALESCE(MAX(M_PRODUCTPRICE.PRICESTD), Cur_Lines.PRICEACTUAL) AS PRICESTD
          FROM M_PRODUCTPRICE
-         WHERE M_PRICELIST_VERSION_ID = GET_PRICELIST_VERSION(Cur_Lines.PRICELIST_ID, p_OrderDate)
+         WHERE M_PRICELIST_VERSION_ID = M_GET_PRICELIST_VERSION(Cur_Lines.PRICELIST_ID, p_OrderDate)
            AND M_PRODUCT_ID = Cur_Lines.M_PRODUCT_ID) pp, (SELECT PRICEPRECISION FROM C_CURRENCY WHERE C_CURRENCY_ID = v_Currency_ID) CUR;
 
       IF (v_PriceActual IS NULL) THEN
--- a/src-db/database/model/functions/M_UPDATE_PARETO_PRODUCT.xml	Thu Oct 09 14:13:04 2008 +0000
+++ b/src-db/database/model/functions/M_UPDATE_PARETO_PRODUCT.xml	Thu Oct 09 17:15:25 2008 +0000
@@ -79,7 +79,7 @@
 
   for Cur_Cursor in (
      SELECT ORGID, SEARCHKEY, NAME, COST, PERCENTAGE,
-     GET_PARETO_ABC(v_warehouse_ID, v_org_ID, v_client_ID, PERCENTAGE) AS ISABC,
+     M_GET_PARETO_ABC(v_warehouse_ID, v_org_ID, v_client_ID, PERCENTAGE) AS ISABC,
      AD_ORG_ID, AD_CLIENT_ID, M_PRODUCT_ID
      FROM
      (
@@ -87,8 +87,8 @@
       AD_ORG.NAME AS ORGID,
       PR.VALUE AS SEARCHKEY,
       PR.NAME,
-      GET_PRODUCT_COST(PR.M_PRODUCT_ID,TO_DATE(now()),'AV') AS COST,
-      100*(GET_PRODUCT_COST(PR.M_PRODUCT_ID,TO_DATE(now()),'AV')/ (SELECT SUM(GET_PRODUCT_COST(PR1.M_PRODUCT_ID,TO_DATE(now()),'AV'))
+      M_GET_PRODUCT_COST(PR.M_PRODUCT_ID,TO_DATE(now()),'AV') AS COST,
+      100*(M_GET_PRODUCT_COST(PR.M_PRODUCT_ID,TO_DATE(now()),'AV')/ (SELECT SUM(M_GET_PRODUCT_COST(PR1.M_PRODUCT_ID,TO_DATE(now()),'AV'))
                                                            from M_WAREHOUSE MW1
                                                            LEFT JOIN M_LOCATOR ML1 ON ML1.M_WAREHOUSE_ID=MW1.M_WAREHOUSE_ID
                                                            LEFT JOIN M_STORAGE_DETAIL MSD1 ON ML1.M_LOCATOR_ID=MSD1.M_LOCATOR_ID
@@ -110,8 +110,8 @@
        AND (v_org_ID IS NULL OR MW.AD_ORG_ID = v_org_ID)
        AND (v_client_ID IS NULL OR MW.AD_CLIENT_ID = v_client_ID)
        AND MSD.QTYONHAND>0
-       AND GET_PRODUCT_COST(PR.M_PRODUCT_ID,TO_DATE(now()),'AV') IS NOT NULL
-       AND GET_PRODUCT_COST(PR.M_PRODUCT_ID,TO_DATE(now()),'AV') <> 0
+       AND M_GET_PRODUCT_COST(PR.M_PRODUCT_ID,TO_DATE(now()),'AV') IS NOT NULL
+       AND M_GET_PRODUCT_COST(PR.M_PRODUCT_ID,TO_DATE(now()),'AV') <> 0
        AND MSD.AD_ORG_ID = AD_ORG.AD_ORG_ID
      GROUP BY AD_ORG.NAME ,PR.VALUE, PR.NAME, MW.AD_ORG_ID, MW.AD_CLIENT_ID, PR.M_PRODUCT_ID
      ORDER BY PERCENTAGE DESC) BBB) loop
--- a/src-db/database/model/functions/NUMBER_NULL.xml	Thu Oct 09 14:13:04 2008 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,25 +0,0 @@
-<?xml version="1.0"?>
-  <database name="FUNCTION NUMBER_NULL">
-    <function name="NUMBER_NULL" type="NUMERIC">
-      <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):  ______________________________________.
-************************************************************************/
-BEGIN
-  RETURN(Null) ;
-END NUMBER_NULL
-]]></body>
-    </function>
-  </database>
--- a/src-db/database/model/functions/TEMPLATE.xml	Thu Oct 09 14:13:04 2008 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,100 +0,0 @@
-<?xml version="1.0"?>
-  <database name="FUNCTION TEMPLATE">
-    <function name="TEMPLATE" type="NULL">
-      <parameter name="p_pinstance_id" type="VARCHAR" mode="in">
-        <default/>
-      </parameter>
-      <body><![CDATA[/*************************************************************************
-  * The contents of this file are subject to the Compiere Public
-  * License 1.1 ("License"); You may not use this file except in
-  * compliance with the License. You may obtain a copy of the License in
-  * the legal folder of your Openbravo installation.
-  * 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  Compiere  ERP &  Business Solution
-  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
-  * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke,
-  * parts created by ComPiere are Copyright (C) ComPiere, Inc.;
-  * All Rights Reserved.
-  * Contributor(s): Openbravo SL
-  * Contributions are Copyright (C) 2001-2008 Openbravo, S.L.
-  *
-  * Specifically, this derivative work is based upon the following Compiere
-  * file and version.
-  *************************************************************************
-  * $Id: Template.sql,v 1.9 2003/02/18 03:33:22 jjanke Exp $
-  ***
-  * Title:
-  * Description:
-  ************************************************************************/
-  -- Logistice
-  v_ResultStr VARCHAR2(2000):='';
-  v_Message VARCHAR2(2000):='';
-  v_Result NUMBER:=1; -- 0=failure
-  v_Record_ID VARCHAR2(32);
-  v_AD_User_ID VARCHAR2(32);
-  v_PInstance_Log_ID VARCHAR2(32);
-  v_Client_ID VARCHAR2(32);
-  -- Parameter
-  TYPE RECORD IS REF CURSOR;
-    Cur_Parameter RECORD;
-    -- Parameter Variables
-    rowcount NUMBER;
-  BEGIN
-    --  Update AD_PInstance
-    DBMS_OUTPUT.PUT_LINE('Updating PInstance - Processing ' || p_PInstance_ID) ;
-    v_ResultStr:='PInstanceNotFound';
-    AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'Y', NULL, NULL) ;
-  BEGIN --BODY
-    -- Get Parameters
-    v_ResultStr:='ReadingParameters';
-    FOR Cur_Parameter IN
-      (SELECT i.Record_ID,
-        i.AD_User_ID,
-        p.ParameterName,
-        p.P_String,
-        p.P_Number,
-        p.P_Date,
-        p.AD_CLIENT_ID
-      FROM AD_PInstance i
-      LEFT JOIN AD_PInstance_Para p
-        ON i.AD_PInstance_ID=p.AD_PInstance_ID
-      WHERE i.AD_PInstance_ID=p_PInstance_ID
-      ORDER BY p.SeqNo
-      )
-    LOOP
-      v_Record_ID:=Cur_Parameter.Record_ID;
-      v_AD_User_ID:=Cur_Parameter.AD_User_ID;
-      v_Client_ID:=Cur_Parameter.AD_CLIENT_ID;
-      -- IF (p.ParameterName = 'xx') THEN
-      --  p_xx := p.P_String;
-      --  DBMS_OUTPUT.PUT_LINE('  xx=' || p_xx);
-      -- ELSE
-      --  DBMS_OUTPUT.PUT_LINE('*** Unknown Parameter=' || p.ParameterName);
-      --  END IF;
-    END LOOP; -- Get Parameter
-    DBMS_OUTPUT.PUT_LINE('  Record_ID=' || v_Record_ID) ;
-   
-    -- Summary info rowcount
-    rowcount:=SQL%ROWCOUNT;
-    DBMS_OUTPUT.PUT_LINE('  Count=' || rowcount);
-    v_Message:='@Totals@ @Created@ @Updated@';
-    --<<FINISH_PROCESS>>
-    --  Update AD_PInstance
-    DBMS_OUTPUT.PUT_LINE('Updating PInstance - Finished ' || v_Message) ;
-    AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', v_Result, v_Message) ;
-    RETURN;
-  END; --BODY
-EXCEPTION
-WHEN OTHERS THEN
-   v_ResultStr:= '@ERROR=' || SQLERRM;
-  DBMS_OUTPUT.PUT_LINE(v_ResultStr) ;
-  ROLLBACK