예제 #1
0
 /**
  * Get Total Product Costs from Cost Detail or from Current Cost
  *
  * @param as accounting schema
  * @param AD_Org_ID trx org
  * @param zeroCostsOK zero/no costs are OK
  * @param whereClause null are OK
  * @return costs
  */
 public BigDecimal getProductCosts(
     MAcctSchema as, int AD_Org_ID, boolean zeroCostsOK, String whereClause) {
   if (whereClause != null
       && !as.getCostingMethod().equals(MAcctSchema.COSTINGMETHOD_StandardCosting)) {
     MCostDetail cd =
         MCostDetail.get(
             Env.getCtx(),
             whereClause,
             get_ID(),
             getM_AttributeSetInstance_ID(),
             as.getC_AcctSchema_ID(),
             p_po.get_TrxName());
     if (cd != null) return cd.getAmt();
   }
   return getProductCosts(as, AD_Org_ID, zeroCostsOK);
 } //  getProductCosts
예제 #2
0
  /**
   * Create Facts (the accounting logic) for MMP.
   *
   * <pre>
   *  Production
   *      Inventory       DR      CR
   *  </pre>
   *
   * @param as account schema
   * @return Fact
   */
  public ArrayList<Fact> createFacts(MAcctSchema as) {
    //  create Fact Header
    Fact fact = new Fact(this, as, Fact.POST_Actual);
    setC_Currency_ID(as.getC_Currency_ID());

    //  Line pointer
    FactLine fl = null;
    X_M_Production prod = (X_M_Production) getPO();
    for (int i = 0; i < p_lines.length; i++) {
      DocLine line = p_lines[i];
      //	Calculate Costs
      BigDecimal costs = null;

      // MZ Goodwill
      // if Production CostDetail exist then get Cost from Cost Detail
      MCostDetail cd =
          MCostDetail.get(
              as.getCtx(),
              "M_ProductionLine_ID=?",
              line.get_ID(),
              line.getM_AttributeSetInstance_ID(),
              as.getC_AcctSchema_ID(),
              getTrxName());
      if (cd != null) {
        costs = cd.getAmt();
      } else {
        costs = line.getProductCosts(as, line.getAD_Org_ID(), false);
      }
      if (line.isProductionBOM()) {
        X_M_ProductionLine endProLine = (X_M_ProductionLine) line.getPO();
        Object parentEndPro =
            prod.isUseProductionPlan()
                ? endProLine.getM_ProductionPlan_ID()
                : endProLine.getM_Production_ID();

        //	Get BOM Cost - Sum of individual lines
        BigDecimal bomCost = Env.ZERO;
        for (int ii = 0; ii < p_lines.length; ii++) {
          DocLine line0 = p_lines[ii];
          X_M_ProductionLine bomProLine = (X_M_ProductionLine) line0.getPO();
          Object parentBomPro =
              prod.isUseProductionPlan()
                  ? bomProLine.getM_ProductionPlan_ID()
                  : bomProLine.getM_Production_ID();

          if (!parentBomPro.equals(parentEndPro)) continue;
          if (!line0.isProductionBOM()) {
            // get cost of children
            MCostDetail cd0 =
                MCostDetail.get(
                    as.getCtx(),
                    "M_ProductionLine_ID=?",
                    line0.get_ID(),
                    line0.getM_AttributeSetInstance_ID(),
                    as.getC_AcctSchema_ID(),
                    getTrxName());
            BigDecimal costs0;
            if (cd0 != null) {
              costs0 = cd0.getAmt();
            } else {
              costs0 = line0.getProductCosts(as, line0.getAD_Org_ID(), false);
            }
            bomCost = bomCost.add(costs0.setScale(2, BigDecimal.ROUND_HALF_UP));
          }
        }

        BigDecimal qtyProduced =
            manipulateQtyProduced(mQtyProduced, endProLine, prod.isUseProductionPlan(), null);
        if (line.getQty().compareTo(qtyProduced) != 0) {
          BigDecimal factor = line.getQty().divide(qtyProduced, 12, BigDecimal.ROUND_HALF_UP);
          bomCost = bomCost.multiply(factor).setScale(2, BigDecimal.ROUND_HALF_UP);
        }
        int precision = as.getStdPrecision();
        BigDecimal variance =
            (costs.setScale(precision, BigDecimal.ROUND_HALF_UP)).subtract(bomCost.negate());
        // only post variance if it's not zero
        if (variance.signum() != 0) {
          // post variance
          fl =
              fact.createLine(
                  line,
                  line.getAccount(ProductCost.ACCTTYPE_P_RateVariance, as),
                  as.getC_Currency_ID(),
                  variance.negate());
          if (fl == null) {
            p_Error = "Couldn't post variance " + line.getLine() + " - " + line;
            return null;
          }
          fl.setQty(Env.ZERO);
        }
        // costs = bomCost.negate();
      }
      // end MZ

      //  Inventory       DR      CR
      fl =
          fact.createLine(
              line,
              line.getAccount(ProductCost.ACCTTYPE_P_Asset, as),
              as.getC_Currency_ID(),
              costs);
      if (fl == null) {
        p_Error = "No Costs for Line " + line.getLine() + " - " + line;
        return null;
      }
      fl.setM_Locator_ID(line.getM_Locator_ID());
      fl.setQty(line.getQty());

      //	Cost Detail
      String description = line.getDescription();
      if (description == null) description = "";
      if (line.isProductionBOM()) description += "(*)";
      if (!MCostDetail.createProduction(
          as,
          line.getAD_Org_ID(),
          line.getM_Product_ID(),
          line.getM_AttributeSetInstance_ID(),
          line.get_ID(),
          0,
          costs,
          line.getQty(),
          description,
          getTrxName())) {
        p_Error = "Failed to create cost detail record";
        return null;
      }
    }
    //
    ArrayList<Fact> facts = new ArrayList<Fact>();
    facts.add(fact);
    return facts;
  } //  createFact