Fixes issue 21557: When an Instance does not need to migrate to the
authorDavid Miguelez <david.miguelez@openbravo.com>
Thu, 13 Sep 2012 13:27:42 +0200
changeset 17938 a8cb5918bad6
parent 17937 af015ec644dd
child 17939 3e0e98fb9275
Fixes issue 21557: When an Instance does not need to migrate to the
new Costing Engine, but the Preference has not been created, create
it.
src/org/openbravo/costing/CostingRuleProcess.java
src/org/openbravo/costing/CostingStatus.java
--- a/src/org/openbravo/costing/CostingRuleProcess.java	Thu Sep 13 12:53:17 2012 +0200
+++ b/src/org/openbravo/costing/CostingRuleProcess.java	Thu Sep 13 13:27:42 2012 +0200
@@ -151,20 +151,11 @@
   }
 
   private void migrationCheck() {
-    if (isCostingMigrationNeeded() && !CostingStatus.getInstance().isMigrated()) {
+    if (!CostingStatus.getInstance().isMigrated()) {
       throw new OBException("@CostMigrationNotDone@");
     }
   }
 
-  private boolean isCostingMigrationNeeded() {
-    OBQuery<org.openbravo.model.materialmgmt.cost.Costing> costingQry = OBDal.getInstance()
-        .createQuery(org.openbravo.model.materialmgmt.cost.Costing.class, "");
-    costingQry.setFilterOnReadableClients(false);
-    costingQry.setFilterOnReadableOrganization(false);
-
-    return costingQry.count() > 0;
-  }
-
   private boolean existsPreviousRule(CostingRule rule) {
     StringBuffer where = new StringBuffer();
     where.append(" as cr");
--- a/src/org/openbravo/costing/CostingStatus.java	Thu Sep 13 12:53:17 2012 +0200
+++ b/src/org/openbravo/costing/CostingStatus.java	Thu Sep 13 13:27:42 2012 +0200
@@ -26,6 +26,7 @@
 import org.openbravo.model.ad.domain.Preference;
 import org.openbravo.model.ad.system.Client;
 import org.openbravo.model.common.enterprise.Organization;
+import org.openbravo.model.materialmgmt.cost.CostingRule;
 
 public class CostingStatus implements OBSingleton {
   private static CostingStatus instance;
@@ -51,6 +52,23 @@
         crQry.setFilterOnReadableOrganization(false);
         crQry.setMaxResult(1);
         isMigrated = crQry.uniqueResult() != null;
+
+        if (!isMigrated) {
+          OBQuery<org.openbravo.model.materialmgmt.cost.Costing> costingQry = OBDal.getInstance()
+              .createQuery(org.openbravo.model.materialmgmt.cost.Costing.class, "");
+          costingQry.setFilterOnReadableClients(false);
+          costingQry.setFilterOnReadableOrganization(false);
+
+          OBQuery<CostingRule> cRuleQry = OBDal.getInstance().createQuery(CostingRule.class,
+              CostingRule.PROPERTY_VALIDATED + " = true");
+          cRuleQry.setFilterOnReadableClients(false);
+          cRuleQry.setFilterOnReadableOrganization(false);
+
+          if (costingQry.count() == 0 || cRuleQry.count() > 0) {
+            setMigrated();
+          }
+        }
+
       } finally {
         OBContext.restorePreviousMode();
       }
@@ -59,7 +77,7 @@
   }
 
   public void setMigrated() {
-    if (isMigrated()) {
+    if (isMigrated) {
       return;
     }
     OBContext.setAdminMode(false);