details:   https://code.openbravo.com/erp/devel/pi/rev/e5b78d273dd8
changeset: 33090:e5b78d273dd8
user:      Nono Carballo <nonofce <at> gmail.com>
date:      Thu Nov 30 18:02:35 2017 +0100
summary:   Fixes issue 37344: If same product is consumed and produced in same 
BOM
Production, produced transaction cost is calculated first

Before calculating the cost of a BOMPart transaction, cost of BOMProduction
transactions for the same product in previous production plans are calculated.

This fix is a contribution of Support Team.

details:   https://code.openbravo.com/erp/devel/pi/rev/1fde3c15b1a6
changeset: 33091:1fde3c15b1a6
user:      David Miguelez <david.miguelez <at> openbravo.com>
date:      Thu Nov 30 18:31:38 2017 +0100
summary:   Related to Issue 37344. Code Review changes.

Moved query logic into it's own method.

diffstat:

 src/org/openbravo/costing/CostingAlgorithm.java |  47 +++++++++++++++++++++++-
 1 files changed, 44 insertions(+), 3 deletions(-)

diffs (82 lines):

diff -r 4206e68ea96b -r 1fde3c15b1a6 
src/org/openbravo/costing/CostingAlgorithm.java
--- a/src/org/openbravo/costing/CostingAlgorithm.java   Thu Nov 30 10:44:40 
2017 +0100
+++ b/src/org/openbravo/costing/CostingAlgorithm.java   Thu Nov 30 18:31:38 
2017 +0100
@@ -31,6 +31,7 @@
 import org.openbravo.costing.CostingServer.TrxType;
 import org.openbravo.dal.core.OBContext;
 import org.openbravo.dal.service.OBDal;
+import org.openbravo.dal.service.OBQuery;
 import org.openbravo.erpCommon.utility.OBDateUtils;
 import org.openbravo.financial.FinancialUtils;
 import org.openbravo.model.common.businesspartner.BusinessPartner;
@@ -41,6 +42,7 @@
 import org.openbravo.model.materialmgmt.cost.CostingRule;
 import org.openbravo.model.materialmgmt.transaction.MaterialTransaction;
 import org.openbravo.model.materialmgmt.transaction.ProductionLine;
+import org.openbravo.model.materialmgmt.transaction.ProductionPlan;
 import org.openbravo.model.materialmgmt.transaction.ProductionTransaction;
 import org.openbravo.model.pricing.pricelist.PriceList;
 import org.openbravo.model.pricing.pricelist.ProductPrice;
@@ -59,9 +61,9 @@
    * Initializes the instance of the CostingAlgorith with the 
MaterialTransaction that is being to
    * be calculated and the cost dimensions values in case they have to be used.
    * 
-   * It initializes several values: <list><li>Organization, it's used the 
Legal Entity dimension. If
-   * this is null Asterisk organization is used. <li>Currency, it takes the 
currency defined for the
-   * Organization. If this is null it uses the currency defined for the 
Client. <li>Transaction
+   * It initializes several values: <list> <li>Organization, it's used the 
Legal Entity dimension.
+   * If this is null Asterisk organization is used. <li>Currency, it takes the 
currency defined for
+   * the Organization. If this is null it uses the currency defined for the 
Client. <li>Transaction
    * Type, it calculates its type. </list>
    * 
    * @param costingServer
@@ -474,10 +476,49 @@
    * @return BigDecimal object representing the total cost amount of the 
transaction.
    */
   protected BigDecimal getBOMPartCost() {
+    // Check if the BOM Part is produced in a previous production plan of the 
same Production. If
+    // that is the case ensure that its cost is already calculated.
+    calculateWIPBOMCost();
     return getOutgoingTransactionCost();
   }
 
   /**
+   * It searches transactions of current consumed BOM Product Part where it is 
produced in the same
+   * BOM Production and are still not calculated. Then it calculates the cost 
of each of them.
+   */
+  private void calculateWIPBOMCost() {
+    for (MaterialTransaction wipBOMtrx : getPendingWIPBOMTransactions()) {
+      log4j
+          .debug("BOM Part produced in previous Production Plan detected. 
Calculating its cost. TrxId: "
+              + wipBOMtrx.getId());
+      CostingServer transactionCost = new CostingServer(wipBOMtrx);
+      transactionCost.process();
+    }
+  }
+
+  private List<MaterialTransaction> getPendingWIPBOMTransactions() {
+    StringBuilder where = new StringBuilder();
+    where.append(" as trx ");
+    where.append("  join trx." + MaterialTransaction.PROPERTY_PRODUCTIONLINE + 
" as pl ");
+    where.append("  join pl." + ProductionLine.PROPERTY_PRODUCTIONPLAN + " as 
pp");
+    where.append(" where pp." + ProductionPlan.PROPERTY_LINENO + " < :line");
+    where.append("   and pp." + ProductionPlan.PROPERTY_PRODUCTION + " = 
:production");
+    where.append("   and pl." + ProductionLine.PROPERTY_PRODUCT + " = 
:product");
+    where.append("   and pl." + ProductionLine.PROPERTY_MOVEMENTQUANTITY + " > 
0");
+    where.append("   and trx." + MaterialTransaction.PROPERTY_ISCOSTCALCULATED 
+ " = false");
+
+    OBQuery<MaterialTransaction> pendingWIPBOMs = 
OBDal.getInstance().createQuery(
+        MaterialTransaction.class, where.toString());
+
+    ProductionPlan productionPlan = 
transaction.getProductionLine().getProductionPlan();
+    pendingWIPBOMs.setNamedParameter("line", productionPlan.getLineNo());
+    pendingWIPBOMs.setNamedParameter("production", 
productionPlan.getProduction());
+    pendingWIPBOMs.setNamedParameter("product", transaction.getProduct());
+
+    return pendingWIPBOMs.list();
+  }
+
+  /**
    * Calculates the cost of a produced BOM product. Its cost is the sum of the 
used products
    * transactions costs. If these has not been calculated yet they are 
calculated.
    * 

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Openbravo-commits mailing list
Openbravo-commits@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/openbravo-commits

Reply via email to