Fixes Issue 27363:Exists a case where currency is not set to business partner
authorAtul Gaware <atul.gaware@openbravo.com>
Tue, 19 Aug 2014 15:28:48 +0530
changeset 24282 723fbb6fcb2a
parent 24281 1411ac29976f
child 24283 2b6fe7d2babe
Fixes Issue 27363:Exists a case where currency is not set to business partner
and could create big problems.
src-util/modulescript/build/classes/org/openbravo/modulescript/InitializeBPCurrency.class
src-util/modulescript/build/classes/org/openbravo/modulescript/InitializeBPCurrencyData.class
src-util/modulescript/build/javasqlc/src/org/openbravo/modulescript/InitializeBPCurrencyData.java
src-util/modulescript/src/org/openbravo/modulescript/InitializeBPCurrency.java
src-util/modulescript/src/org/openbravo/modulescript/InitializeBPCurrency_data.xsql
Binary file src-util/modulescript/build/classes/org/openbravo/modulescript/InitializeBPCurrency.class has changed
Binary file src-util/modulescript/build/classes/org/openbravo/modulescript/InitializeBPCurrencyData.class has changed
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src-util/modulescript/build/javasqlc/src/org/openbravo/modulescript/InitializeBPCurrencyData.java	Tue Aug 19 15:28:48 2014 +0530
@@ -0,0 +1,260 @@
+//Sqlc generated V1.O00-1
+package org.openbravo.modulescript;
+
+import java.sql.*;
+
+import org.apache.log4j.Logger;
+
+import javax.servlet.ServletException;
+
+import org.openbravo.data.FieldProvider;
+import org.openbravo.database.ConnectionProvider;
+import org.openbravo.data.UtilSql;
+import java.util.*;
+
+class InitializeBPCurrencyData implements FieldProvider {
+static Logger log4j = Logger.getLogger(InitializeBPCurrencyData.class);
+  private String InitRecordNumber="0";
+  public String bpCurrencyId;
+
+  public String getInitRecordNumber() {
+    return InitRecordNumber;
+  }
+
+  public String getField(String fieldName) {
+    if (fieldName.equalsIgnoreCase("bp_currency_id") || fieldName.equals("bpCurrencyId"))
+      return bpCurrencyId;
+   else {
+     log4j.debug("Field does not exist: " + fieldName);
+     return null;
+   }
+ }
+
+  public static InitializeBPCurrencyData[] select(ConnectionProvider connectionProvider)    throws ServletException {
+    return select(connectionProvider, 0, 0);
+  }
+
+  public static InitializeBPCurrencyData[] select(ConnectionProvider connectionProvider, int firstRegister, int numberRegisters)    throws ServletException {
+    String strSql = "";
+    strSql = strSql + 
+      "      SELECT '' AS BP_Currency_ID" +
+      "      FROM DUAL";
+
+    ResultSet result;
+    Vector<java.lang.Object> vector = new Vector<java.lang.Object>(0);
+    PreparedStatement st = null;
+
+    try {
+    st = connectionProvider.getPreparedStatement(strSql);
+
+      result = st.executeQuery();
+      long countRecord = 0;
+      long countRecordSkip = 1;
+      boolean continueResult = true;
+      while(countRecordSkip < firstRegister && continueResult) {
+        continueResult = result.next();
+        countRecordSkip++;
+      }
+      while(continueResult && result.next()) {
+        countRecord++;
+        InitializeBPCurrencyData objectInitializeBPCurrencyData = new InitializeBPCurrencyData();
+        objectInitializeBPCurrencyData.bpCurrencyId = UtilSql.getValue(result, "bp_currency_id");
+        objectInitializeBPCurrencyData.InitRecordNumber = Integer.toString(firstRegister);
+        vector.addElement(objectInitializeBPCurrencyData);
+        if (countRecord >= numberRegisters && numberRegisters != 0) {
+          continueResult = false;
+        }
+      }
+      result.close();
+    } catch(SQLException e){
+      log4j.error("SQL error in query: " + strSql + "Exception:"+ e);
+      throw new ServletException("@CODE=" + Integer.toString(e.getErrorCode()) + "@" + e.getMessage());
+    } catch(Exception ex){
+      log4j.error("Exception in query: " + strSql + "Exception:"+ ex);
+      throw new ServletException("@CODE=@" + ex.getMessage());
+    } finally {
+      try {
+        connectionProvider.releasePreparedStatement(st);
+      } catch(Exception ignore){
+        ignore.printStackTrace();
+      }
+    }
+    InitializeBPCurrencyData objectInitializeBPCurrencyData[] = new InitializeBPCurrencyData[vector.size()];
+    vector.copyInto(objectInitializeBPCurrencyData);
+    return(objectInitializeBPCurrencyData);
+  }
+
+  public static int initializeCurrency(ConnectionProvider connectionProvider)    throws ServletException {
+    String strSql = "";
+    strSql = strSql + 
+      "        UPDATE C_Bpartner bp" +
+      " 		SET BP_Currency_ID = (" +
+      " 		                     SELECT C_Currency_ID" +
+      " 		                     FROM M_Pricelist" +
+      " 		                     WHERE M_Pricelist_ID=(case when bp.iscustomer='Y' and bp.m_pricelist_id is not null then bp.M_Pricelist_ID" +
+      " 		                                                when bp.isvendor='Y' and bp.po_pricelist_id is not null then  bp.PO_Pricelist_ID" +
+      " 		                                                end)" +
+      " 		                     )" +
+      " 		WHERE bp.BP_Currency_ID IS NULL" +
+      " 		AND ((bp.IsCustomer = 'Y' and bp.m_pricelist_id is not null) OR " +
+      " 		     (bp.IsVendor = 'Y' and bp.po_pricelist_id is not null))";
+
+    int updateCount = 0;
+    PreparedStatement st = null;
+
+    try {
+    st = connectionProvider.getPreparedStatement(strSql);
+
+      updateCount = st.executeUpdate();
+    } catch(SQLException e){
+      log4j.error("SQL error in query: " + strSql + "Exception:"+ e);
+      throw new ServletException("@CODE=" + Integer.toString(e.getErrorCode()) + "@" + e.getMessage());
+    } catch(Exception ex){
+      log4j.error("Exception in query: " + strSql + "Exception:"+ ex);
+      throw new ServletException("@CODE=@" + ex.getMessage());
+    } finally {
+      try {
+        connectionProvider.releasePreparedStatement(st);
+      } catch(Exception ignore){
+        ignore.printStackTrace();
+      }
+    }
+    return(updateCount);
+  }
+
+  public static boolean isExecuted(ConnectionProvider connectionProvider)    throws ServletException {
+    String strSql = "";
+    strSql = strSql + 
+      "        SELECT count(*) as exist" +
+      "        FROM DUAL" +
+      "        WHERE EXISTS (SELECT 1 FROM ad_preference" +
+      "                      WHERE attribute = 'InitializeBPCurrency')";
+
+    ResultSet result;
+    boolean boolReturn = false;
+    PreparedStatement st = null;
+
+    try {
+    st = connectionProvider.getPreparedStatement(strSql);
+
+      result = st.executeQuery();
+      if(result.next()) {
+        boolReturn = !UtilSql.getValue(result, "exist").equals("0");
+      }
+      result.close();
+    } catch(SQLException e){
+      log4j.error("SQL error in query: " + strSql + "Exception:"+ e);
+      throw new ServletException("@CODE=" + Integer.toString(e.getErrorCode()) + "@" + e.getMessage());
+    } catch(Exception ex){
+      log4j.error("Exception in query: " + strSql + "Exception:"+ ex);
+      throw new ServletException("@CODE=@" + ex.getMessage());
+    } finally {
+      try {
+        connectionProvider.releasePreparedStatement(st);
+      } catch(Exception ignore){
+        ignore.printStackTrace();
+      }
+    }
+    return(boolReturn);
+  }
+
+  public static int createPreference(ConnectionProvider connectionProvider)    throws ServletException {
+    String strSql = "";
+    strSql = strSql + 
+      "           INSERT INTO ad_preference (" +
+      "           ad_preference_id, ad_client_id, ad_org_id, isactive," +
+      "           createdby, created, updatedby, updated,attribute" +
+      "           ) VALUES (" +
+      "           get_uuid(), '0', '0', 'Y', '0', NOW(), '0', NOW(),'InitializeBPCurrency')";
+
+    int updateCount = 0;
+    PreparedStatement st = null;
+
+    try {
+    st = connectionProvider.getPreparedStatement(strSql);
+
+      updateCount = st.executeUpdate();
+    } catch(SQLException e){
+      log4j.error("SQL error in query: " + strSql + "Exception:"+ e);
+      throw new ServletException("@CODE=" + Integer.toString(e.getErrorCode()) + "@" + e.getMessage());
+    } catch(Exception ex){
+      log4j.error("Exception in query: " + strSql + "Exception:"+ ex);
+      throw new ServletException("@CODE=@" + ex.getMessage());
+    } finally {
+      try {
+        connectionProvider.releasePreparedStatement(st);
+      } catch(Exception ignore){
+        ignore.printStackTrace();
+      }
+    }
+    return(updateCount);
+  }
+
+  public static boolean isUpdated(ConnectionProvider connectionProvider)    throws ServletException {
+    String strSql = "";
+    strSql = strSql + 
+      "        SELECT count(*) as exist" +
+      "        FROM DUAL" +
+      "        WHERE EXISTS (SELECT 1 FROM ad_preference" +
+      "                      WHERE attribute = 'UpdateBPCurrency')";
+
+    ResultSet result;
+    boolean boolReturn = false;
+    PreparedStatement st = null;
+
+    try {
+    st = connectionProvider.getPreparedStatement(strSql);
+
+      result = st.executeQuery();
+      if(result.next()) {
+        boolReturn = !UtilSql.getValue(result, "exist").equals("0");
+      }
+      result.close();
+    } catch(SQLException e){
+      log4j.error("SQL error in query: " + strSql + "Exception:"+ e);
+      throw new ServletException("@CODE=" + Integer.toString(e.getErrorCode()) + "@" + e.getMessage());
+    } catch(Exception ex){
+      log4j.error("Exception in query: " + strSql + "Exception:"+ ex);
+      throw new ServletException("@CODE=@" + ex.getMessage());
+    } finally {
+      try {
+        connectionProvider.releasePreparedStatement(st);
+      } catch(Exception ignore){
+        ignore.printStackTrace();
+      }
+    }
+    return(boolReturn);
+  }
+
+  public static int createUpdatePreference(ConnectionProvider connectionProvider)    throws ServletException {
+    String strSql = "";
+    strSql = strSql + 
+      "           INSERT INTO ad_preference (" +
+      "           ad_preference_id, ad_client_id, ad_org_id, isactive," +
+      "           createdby, created, updatedby, updated,attribute" +
+      "           ) VALUES (" +
+      "           get_uuid(), '0', '0', 'Y', '0', NOW(), '0', NOW(),'UpdateBPCurrency')";
+
+    int updateCount = 0;
+    PreparedStatement st = null;
+
+    try {
+    st = connectionProvider.getPreparedStatement(strSql);
+
+      updateCount = st.executeUpdate();
+    } catch(SQLException e){
+      log4j.error("SQL error in query: " + strSql + "Exception:"+ e);
+      throw new ServletException("@CODE=" + Integer.toString(e.getErrorCode()) + "@" + e.getMessage());
+    } catch(Exception ex){
+      log4j.error("Exception in query: " + strSql + "Exception:"+ ex);
+      throw new ServletException("@CODE=@" + ex.getMessage());
+    } finally {
+      try {
+        connectionProvider.releasePreparedStatement(st);
+      } catch(Exception ignore){
+        ignore.printStackTrace();
+      }
+    }
+    return(updateCount);
+  }
+}
--- a/src-util/modulescript/src/org/openbravo/modulescript/InitializeBPCurrency.java	Tue Aug 19 17:43:47 2014 +0000
+++ b/src-util/modulescript/src/org/openbravo/modulescript/InitializeBPCurrency.java	Tue Aug 19 15:28:48 2014 +0530
@@ -36,6 +36,13 @@
       if (!isExecuted){
         InitializeBPCurrencyData.initializeCurrency(cp);
         InitializeBPCurrencyData.createPreference(cp);
+        InitializeBPCurrencyData.createUpdatePreference(cp);
+      } else {
+        boolean isUpdated = InitializeBPCurrencyData.isUpdated(cp);
+        if(!isUpdated) {
+          InitializeBPCurrencyData.initializeCurrency(cp);
+          InitializeBPCurrencyData.createUpdatePreference(cp);
+        }
       }
     } catch (Exception e) {
       handleError(e);
--- a/src-util/modulescript/src/org/openbravo/modulescript/InitializeBPCurrency_data.xsql	Tue Aug 19 17:43:47 2014 +0000
+++ b/src-util/modulescript/src/org/openbravo/modulescript/InitializeBPCurrency_data.xsql	Tue Aug 19 15:28:48 2014 +0530
@@ -34,8 +34,8 @@
  		SET BP_Currency_ID = (
  		                     SELECT C_Currency_ID
  		                     FROM M_Pricelist
- 		                     WHERE M_Pricelist_ID=(case when bp.iscustomer='Y' then bp.M_Pricelist_ID
- 		                                                when bp.isvendor='Y' then  bp.PO_Pricelist_ID
+ 		                     WHERE M_Pricelist_ID=(case when bp.iscustomer='Y' and bp.m_pricelist_id is not null then bp.M_Pricelist_ID
+ 		                                                when bp.isvendor='Y' and bp.po_pricelist_id is not null then  bp.PO_Pricelist_ID
  		                                                end)
  		                     )
  		WHERE bp.BP_Currency_ID IS NULL
@@ -67,4 +67,27 @@
          ]]>
         </Sql>
    </SqlMethod>
+   <SqlMethod name="isUpdated" type="preparedStatement" return="boolean">
+    <SqlMethodComment></SqlMethodComment>
+    <Sql>
+      <![CDATA[
+        SELECT count(*) as exist
+        FROM DUAL
+        WHERE EXISTS (SELECT 1 FROM ad_preference
+                      WHERE attribute = 'UpdateBPCurrency')
+      ]]>
+    </Sql>
+  </SqlMethod>
+   <SqlMethod name="createUpdatePreference" type="preparedStatement" return="rowcount">
+      <SqlMethodComment></SqlMethodComment>
+       <Sql> 
+       <![CDATA[ 
+           INSERT INTO ad_preference (
+           ad_preference_id, ad_client_id, ad_org_id, isactive,
+           createdby, created, updatedby, updated,attribute
+           ) VALUES (
+           get_uuid(), '0', '0', 'Y', '0', NOW(), '0', NOW(),'UpdateBPCurrency')
+         ]]>
+        </Sql>
+   </SqlMethod>
 </SqlClass>
\ No newline at end of file