public void build(String headerText, Fact fact) {

    if (fact instanceof FactData) {
      FactData factData = (FactData) fact;
      widget.setWidget(0, ++col, new SmallLabel("[" + factData.getName() + "]"));
    } else {
      col++;
    }

    widget.setWidget(0, 0, new ClickableLabel(headerText, createAddFieldButton(fact)));

    // Sets row name and delete button.
    for (final Field field : fact.getFieldData()) {
      // Avoid duplicate field rows, only one for each name.
      if (rowIndexByFieldName.doesNotContain(field.getName())) {
        newRow(fact, field.getName());
      }

      // Sets row data
      int fieldRowIndex = rowIndexByFieldName.getRowIndex(field.getName());
      widget.setWidget(fieldRowIndex, col, editableCell(field, fact, fact.getType()));
    }

    if (fact instanceof FactData) {
      DeleteFactColumnButton deleteFactColumnButton = new DeleteFactColumnButton((FactData) fact);

      widget.setWidget(rowIndexByFieldName.amountOrRows() + 1, col, deleteFactColumnButton);
    }
  }
Beispiel #2
0
 public ArrayList<Fact> getRows() {
   ArrayList<Fact> filteredFact = new ArrayList<Fact>();
   for (Fact fact : rows) {
     if (!fact.isNull()) {
       filteredFact.add(fact);
     }
   }
   return filteredFact;
   //        return rows;
 }
  /**
   * Create Facts (the accounting logic) for PJI
   *
   * <pre>
   *  Issue
   *      ProjectWIP      DR
   *      Inventory               CR
   *  </pre>
   *
   * Project Account is either Asset or WIP depending on Project Type
   *
   * @param as accounting 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());

    MProject project = new MProject(getCtx(), m_issue.getC_Project_ID(), getTrxName());
    String ProjectCategory = project.getProjectCategory();
    MProduct product = MProduct.get(getCtx(), m_issue.getM_Product_ID());

    //  Line pointers
    FactLine dr = null;
    FactLine cr = null;

    //  Issue Cost
    BigDecimal costs = null;
    BigDecimal total = Env.ZERO;
    if (m_issue.getM_InOutLine_ID() != 0) costs = getPOCost(as);
    else if (m_issue.getS_TimeExpenseLine_ID() != 0) costs = getLaborCost(as);
    if (costs == null) // 	standard Product Costs
    {
      for (MCostDetail cost : m_line.getCostDetail(as)) {
        if (!MCostDetail.existsCost(cost)) continue;

        costs = MCostDetail.getTotalCost(cost, as);
        total = total.add(costs);
      }
    }

    if (total == null || total.signum() == 0) {
      p_Error = "Resubmit - No Costs for " + product.getName();
      log.log(Level.WARNING, p_Error);
      return null;
    }

    //  Project         DR
    int acctType = ACCTTYPE_ProjectWIP;
    if (MProject.PROJECTCATEGORY_AssetProject.equals(ProjectCategory))
      acctType = ACCTTYPE_ProjectAsset;
    dr = fact.createLine(m_line, getAccount(acctType, as), as.getC_Currency_ID(), costs, null);
    dr.setQty(m_line.getQty().negate());

    //  Inventory               CR
    acctType = ProductCost.ACCTTYPE_P_Asset;
    if (product.isService()) acctType = ProductCost.ACCTTYPE_P_Expense;
    cr =
        fact.createLine(
            m_line, m_line.getAccount(acctType, as), as.getC_Currency_ID(), null, costs);
    cr.setM_Locator_ID(m_line.getM_Locator_ID());
    cr.setLocationFromLocator(m_line.getM_Locator_ID(), true); // from Loc
    //
    ArrayList<Fact> facts = new ArrayList<Fact>();
    facts.add(fact);
    return facts;
  } //  createFact
Beispiel #4
0
  private TaskData doInBackgroundUpdateFact(TaskData[] params) {

    // Save the fact
    Deck deck = params[0].getDeck();
    Card editCard = params[0].getCard();
    Fact editFact = editCard.fact;
    editFact.toDb();
    LinkedList<Card> saveCards = editFact.getUpdatedRelatedCards();

    Iterator<Card> iter = saveCards.iterator();
    while (iter.hasNext()) {
      Card modifyCard = iter.next();
      deck.updateCard(modifyCard);
    }
    // Find all cards based on this fact and update them with the updateCard method.

    publishProgress(new TaskData(deck.getCurrentCard()));

    return null;
  }
  /**
   * Create Facts (the accounting logic) for CMB.
   *
   * <pre>
   *      BankAsset       DR      CR  (Statement)
   *      BankInTransit   DR      CR              (Payment)
   *      Charge          DR          (Charge)
   *      Interest        DR      CR  (Interest)
   * </pre>
   *
   * @param as accounting schema
   * @return Fact
   */
  @Override
  public List<Fact> createFacts(final MAcctSchema as) {
    // create Fact Header
    final Fact fact = new Fact(this, as, Fact.POST_Actual);
    // boolean isInterOrg = isInterOrg(as);

    // Header -- there may be different currency amounts

    final int AD_Org_ID = getBank_Org_ID(); // Bank Account Org
    // Lines
    for (int i = 0; i < p_lines.length; i++) {
      final DocLine_BankStatement line = (DocLine_BankStatement) p_lines[i];
      final int C_BPartner_ID = line.getC_BPartner_ID();

      // Avoid usage of clearing accounts
      // If both accounts BankAsset and BankInTransit are equal
      // then remove the posting

      MAccount acct_bank_asset = getAccount(Doc.ACCTTYPE_BankAsset, as);
      MAccount acct_bank_in_transit = getAccount(Doc.ACCTTYPE_BankInTransit, as);

      // if ((!as.isPostIfClearingEqual()) && acct_bank_asset.equals(acct_bank_in_transit) &&
      // (!isInterOrg)) {
      // don't validate interorg on banks for this - normally banks are balanced by orgs
      if ((!as.isPostIfClearingEqual()) && acct_bank_asset.equals(acct_bank_in_transit)) {
        // Not using clearing accounts
        // just post the difference (if any)

        BigDecimal amt_stmt_minus_trx = line.getStmtAmt().subtract(line.getTrxAmt());
        if (amt_stmt_minus_trx.compareTo(Env.ZERO) != 0) {

          // BankAsset DR CR (Statement minus Payment)
          final FactLine fl =
              fact.createLine(line, acct_bank_asset, line.getC_Currency_ID(), amt_stmt_minus_trx);
          if (fl != null && AD_Org_ID != 0) fl.setAD_Org_ID(AD_Org_ID);
          if (fl != null && C_BPartner_ID != 0) fl.setC_BPartner_ID(C_BPartner_ID);
        }
      }
      // Normal Adempiere behavior -- unchanged if using clearing accounts
      else {
        // BankAsset DR CR (Statement)
        {
          FactLine fl =
              fact.createLine(line, acct_bank_asset, line.getC_Currency_ID(), line.getStmtAmt());
          if (fl != null && AD_Org_ID != 0) fl.setAD_Org_ID(AD_Org_ID);
          if (fl != null && C_BPartner_ID != 0) fl.setC_BPartner_ID(C_BPartner_ID);
        }

        //
        // BankInTransit DR CR (Payment)
        {
          final List<I_C_BankStatementLine_Ref> lineReferences = line.getReferences();
          if (lineReferences.isEmpty()) {
            final FactLine fl =
                fact.createLine(
                    line, acct_bank_in_transit, line.getC_Currency_ID(), line.getTrxAmt().negate());
            if (fl != null) {
              if (C_BPartner_ID != 0) fl.setC_BPartner_ID(C_BPartner_ID);
              if (AD_Org_ID != 0) fl.setAD_Org_ID(AD_Org_ID);
              else fl.setAD_Org_ID(line.getAD_Org_ID(true)); // from payment
            }
          } else {
            for (final I_C_BankStatementLine_Ref lineRef : lineReferences) {
              final FactLine fl =
                  fact.createLine(
                      line,
                      acct_bank_in_transit,
                      lineRef.getC_Currency_ID(),
                      lineRef.getTrxAmt().negate());
              if (fl != null) {
                fl.setSubLine_ID(lineRef.getC_BankStatementLine_Ref_ID());
                fl.setC_BPartner_ID(lineRef.getC_BPartner_ID());
                if (AD_Org_ID != 0) fl.setAD_Org_ID(AD_Org_ID);
                else fl.setAD_Org_ID(line.getAD_Org_ID(lineRef.getC_Payment())); // from payment
              }
            }
          }
        }
      }

      //
      // Charge DR (Charge)
      {
        final FactLine fl;
        if (line.getChargeAmt().compareTo(Env.ZERO) > 0) {
          fl =
              fact.createLine(
                  line,
                  line.getChargeAccount(as, line.getChargeAmt().negate()),
                  line.getC_Currency_ID(),
                  null,
                  line.getChargeAmt());
        } else {
          fl =
              fact.createLine(
                  line,
                  line.getChargeAccount(as, line.getChargeAmt().negate()),
                  line.getC_Currency_ID(),
                  line.getChargeAmt().negate(),
                  null);
        }
        if (fl != null && C_BPartner_ID != 0) fl.setC_BPartner_ID(C_BPartner_ID);
      }

      //
      // Interest DR CR (Interest)
      {
        final FactLine fl;
        if (line.getInterestAmt().signum() < 0) {
          fl =
              fact.createLine(
                  line,
                  getAccount(Doc.ACCTTYPE_InterestExp, as),
                  getAccount(Doc.ACCTTYPE_InterestExp, as),
                  line.getC_Currency_ID(),
                  line.getInterestAmt().negate());
        } else {
          fl =
              fact.createLine(
                  line,
                  getAccount(Doc.ACCTTYPE_InterestRev, as),
                  getAccount(Doc.ACCTTYPE_InterestRev, as),
                  line.getC_Currency_ID(),
                  line.getInterestAmt().negate());
        }
        if (fl != null && C_BPartner_ID != 0) fl.setC_BPartner_ID(C_BPartner_ID);
      }

      //
      // fact.createTaxCorrection();
    }
    //
    final ArrayList<Fact> facts = new ArrayList<Fact>();
    facts.add(fact);
    return facts;
  } // createFact
  /**
   * Create Facts (the accounting logic) for MMS, MMR.
   *
   * <pre>
   *  Shipment
   *      CoGS            DR
   *      Inventory               CR
   *  Shipment of Project Issue
   *      CoGS            DR
   *      Project                 CR
   *  Receipt
   *      Inventory       DR
   *      NotInvoicedReceipt      CR
   * </pre>
   *
   * @param as accounting schema
   * @return Fact
   */
  public Fact createFact(
      AcctSchema as, ConnectionProvider conn, Connection con, VariablesSecureApp vars)
      throws ServletException {
    // Select specific definition
    String strClassname =
        AcctServerData.selectTemplateDoc(conn, as.m_C_AcctSchema_ID, DocumentType);
    if (StringUtils.isEmpty(strClassname)) {
      strClassname = AcctServerData.selectTemplate(conn, as.m_C_AcctSchema_ID, AD_Table_ID);
    } else {
      try {
        DocCostAdjustmentTemplate newTemplate =
            (DocCostAdjustmentTemplate) Class.forName(strClassname).newInstance();
        return newTemplate.createFact(this, as, conn, con, vars);
      } catch (Exception e) {
        log4j.error("Error while creating new instance for DocCostAdjustmentTemplate - ", e);
      }
    }
    C_Currency_ID = as.getC_Currency_ID();
    // create Fact Header
    Fact fact = new Fact(this, as, Fact.POST_Actual);
    String Fact_Acct_Group_ID = SequenceIdData.getUUID();
    String amtDebit = "0";
    String amtCredit = "0";

    // Lines
    for (int i = 0; p_lines != null && i < p_lines.length; i++) {
      DocLine_CostAdjustment line = (DocLine_CostAdjustment) p_lines[i];
      String transactionType = line.getTransactionType();

      BigDecimal amount = new BigDecimal(line.getAmount());
      ProductInfo p = new ProductInfo(line.m_M_Product_ID, conn);

      log4jDocCostAdjustment.debug(
          "antes del creteline, line.getAmount(): "
              + line.getAmount()
              + " - TransactionType: "
              + transactionType);
      if (transactionType.equals(DocLine_CostAdjustment.TRXTYPE_SHIPMENT)) {
        // Cogs DR
        // Inventory Asset CR
        log4jDocCostAdjustment.debug(
            "********** DocCostAdjustment - factAcct - account - "
                + p.getAccount(ProductInfo.ACCTTYPE_P_Cogs, as, conn).C_ValidCombination_ID);

        if (line.isTransactionNegative()) {
          amtDebit = "";
          amtCredit = amount.toPlainString();
        } else {
          amtDebit = amount.toPlainString();
          amtCredit = "";
        }
        fact.createLine(
            line,
            p.getAccount(ProductInfo.ACCTTYPE_P_Cogs, as, conn),
            line.m_C_Currency_ID,
            amtDebit,
            amtCredit,
            Fact_Acct_Group_ID,
            nextSeqNo(SeqNo),
            DocumentType,
            line.m_DateAcct,
            null,
            conn);
        fact.createLine(
            line,
            p.getAccount(ProductInfo.ACCTTYPE_P_Asset, as, conn),
            line.m_C_Currency_ID,
            amtCredit,
            amtDebit,
            Fact_Acct_Group_ID,
            nextSeqNo(SeqNo),
            DocumentType,
            line.m_DateAcct,
            null,
            conn);
      } else if (transactionType.equals(DocLine_CostAdjustment.TRXTYPE_RECEIPT)) {
        Account acct = null;
        // Inventory Asset DR
        if (line.getIsSource() && ("PDC").equals(line.getSourceProcess())) { // Price Diff
          // Correction
          // Invoice Price Variance CR
          acct = p.getAccount(ProductInfo.ACCTTYPE_P_IPV, as, conn);
        } else if (line.getIsSource() && ("LC").equals(line.getSourceProcess())) {
          throw new IllegalStateException(OBMessageUtils.messageBD("LCNotAccounting"));
        } else {
          // Product Exp CR
          acct =
              getAccountByWarehouse(
                  AcctServer.ACCTTYPE_InvDifferences, as, line.getWarehouseId(), conn);
        }
        log4jDocCostAdjustment.debug(
            "********** DocCostAdjustment - factAcct - account - "
                + p.getAccount(ProductInfo.ACCTTYPE_P_Expense, as, conn).C_ValidCombination_ID);
        if (line.isTransactionNegative()) {
          amtDebit = amount.toPlainString();
          amtCredit = "";
        } else {
          amtDebit = "";
          amtCredit = amount.toPlainString();
        }
        fact.createLine(
            line,
            acct,
            line.m_C_Currency_ID,
            amtDebit,
            amtCredit,
            Fact_Acct_Group_ID,
            nextSeqNo(SeqNo),
            DocumentType,
            line.m_DateAcct,
            null,
            conn);
        fact.createLine(
            line,
            p.getAccount(ProductInfo.ACCTTYPE_P_Asset, as, conn),
            line.m_C_Currency_ID,
            amtCredit,
            amtDebit,
            Fact_Acct_Group_ID,
            nextSeqNo(SeqNo),
            DocumentType,
            line.m_DateAcct,
            null,
            conn);
      } else if (transactionType.equals(DocLine_CostAdjustment.TRXTYPE_INVENTORY)) {
        // Inventory Asset DR
        // Inventory Adjustment CR
        log4jDocCostAdjustment.debug(
            "********** DocCostAdjustment - factAcct - account - "
                + getAccountByWarehouse(
                        AcctServer.ACCTTYPE_InvDifferences, as, line.getWarehouseId(), conn)
                    .C_ValidCombination_ID);
        if (line.isTransactionNegative()) {
          amtDebit = amount.toPlainString();
          amtCredit = "";
        } else {
          amtDebit = "";
          amtCredit = amount.toPlainString();
        }
        fact.createLine(
            line,
            getAccountByWarehouse(
                AcctServer.ACCTTYPE_InvDifferences, as, line.getWarehouseId(), conn),
            line.m_C_Currency_ID,
            amtDebit,
            amtCredit,
            Fact_Acct_Group_ID,
            nextSeqNo(SeqNo),
            DocumentType,
            line.m_DateAcct,
            null,
            conn);
        fact.createLine(
            line,
            p.getAccount(ProductInfo.ACCTTYPE_P_Asset, as, conn),
            line.m_C_Currency_ID,
            amtCredit,
            amtDebit,
            Fact_Acct_Group_ID,
            nextSeqNo(SeqNo),
            DocumentType,
            line.m_DateAcct,
            null,
            conn);
      } else if (transactionType.equals(DocLine_CostAdjustment.TRXTYPE_INTERNALMOVEMENTFROM)) {
        // Inventory Asset DR
        // Inventory Adjustment CR
        M_Warehouse_ID = line.getWarehouseId();
        log4jDocCostAdjustment.debug(
            "********** DocCostAdjustment - factAcct - account - "
                + getAccountByWarehouse(
                        AcctServer.ACCTTYPE_InvDifferences, as, line.getWarehouseId(), conn)
                    .C_ValidCombination_ID);
        if (line.isTransactionNegative()) {
          amtDebit = amount.negate().toPlainString();
          amtCredit = "";
        } else {
          amtDebit = "";
          amtCredit = amount.negate().toPlainString();
        }
        fact.createLine(
            line,
            p.getAccount(ProductInfo.ACCTTYPE_P_Asset, as, conn),
            line.m_C_Currency_ID,
            amtDebit,
            amtCredit,
            Fact_Acct_Group_ID,
            nextSeqNo(SeqNo),
            DocumentType,
            line.m_DateAcct,
            null,
            conn);

        fact.createLine(
            line,
            getAccountByWarehouse(
                AcctServer.ACCTTYPE_InvDifferences, as, line.getWarehouseId(), conn),
            line.m_C_Currency_ID,
            amtCredit,
            amtDebit,
            Fact_Acct_Group_ID,
            nextSeqNo(SeqNo),
            DocumentType,
            line.m_DateAcct,
            null,
            conn);
      } else if (transactionType.equals(DocLine_CostAdjustment.TRXTYPE_INTERNALMOVEMENTTO)) {
        // Inventory Asset DR
        // Inventory Adjustment CR
        M_Warehouse_ID = line.getWarehouseId();
        log4jDocCostAdjustment.debug(
            "********** DocCostAdjustment - factAcct - account - "
                + getAccountByWarehouse(
                        AcctServer.ACCTTYPE_InvDifferences, as, line.getWarehouseId(), conn)
                    .C_ValidCombination_ID);
        if (line.isTransactionNegative()) {
          amtDebit = amount.toPlainString();
          amtCredit = "";
        } else {
          amtDebit = "";
          amtCredit = amount.toPlainString();
        }
        fact.createLine(
            line,
            getAccountByWarehouse(
                AcctServer.ACCTTYPE_InvDifferences, as, line.getWarehouseId(), conn),
            line.m_C_Currency_ID,
            amtDebit,
            amtCredit,
            Fact_Acct_Group_ID,
            nextSeqNo(SeqNo),
            DocumentType,
            line.m_DateAcct,
            null,
            conn);
        fact.createLine(
            line,
            p.getAccount(ProductInfo.ACCTTYPE_P_Asset, as, conn),
            line.m_C_Currency_ID,
            amtCredit,
            amtDebit,
            Fact_Acct_Group_ID,
            nextSeqNo(SeqNo),
            DocumentType,
            line.m_DateAcct,
            null,
            conn);
      } else if (transactionType.equals(DocLine_CostAdjustment.TRXTYPE_INTERNALCONSUMPTION)) {
        // Inventory Asset DR
        // Inventory Adjustment CR
        M_Warehouse_ID = line.getWarehouseId();
        log4jDocCostAdjustment.debug(
            "********** DocCostAdjustment - factAcct - account - "
                + getAccountByWarehouse(
                        AcctServer.ACCTTYPE_InvDifferences, as, line.getWarehouseId(), conn)
                    .C_ValidCombination_ID);
        if (line.isTransactionNegative()) {
          amtDebit = amount.toPlainString();
          amtCredit = "";
        } else {
          amtDebit = "";
          amtCredit = amount.toPlainString();
        }
        fact.createLine(
            line,
            p.getAccount(ProductInfo.ACCTTYPE_P_Asset, as, conn),
            line.m_C_Currency_ID,
            amtDebit,
            amtCredit,
            Fact_Acct_Group_ID,
            nextSeqNo(SeqNo),
            DocumentType,
            line.m_DateAcct,
            null,
            conn);

        fact.createLine(
            line,
            getAccountByWarehouse(
                AcctServer.ACCTTYPE_InvDifferences, as, line.getWarehouseId(), conn),
            line.m_C_Currency_ID,
            amtCredit,
            amtDebit,
            Fact_Acct_Group_ID,
            nextSeqNo(SeqNo),
            DocumentType,
            line.m_DateAcct,
            null,
            conn);
      } else if (transactionType.equals(DocLine_CostAdjustment.TRXTYPE_BOM)) {
        // Inventory Asset DR
        // Inventory Adjustment CR
        M_Warehouse_ID = line.getWarehouseId();
        log4jDocCostAdjustment.debug(
            "********** DocCostAdjustment - factAcct - account - "
                + getAccountByWarehouse(
                        AcctServer.ACCTTYPE_InvDifferences, as, line.getWarehouseId(), conn)
                    .C_ValidCombination_ID);
        if (line.isTransactionNegative()) {
          amtDebit = amount.toPlainString();
          amtCredit = "";
        } else {
          amtDebit = "";
          amtCredit = amount.toPlainString();
        }
        fact.createLine(
            line,
            getAccountByWarehouse(
                AcctServer.ACCTTYPE_InvDifferences, as, line.getWarehouseId(), conn),
            line.m_C_Currency_ID,
            amtDebit,
            amtCredit,
            Fact_Acct_Group_ID,
            nextSeqNo(SeqNo),
            DocumentType,
            line.m_DateAcct,
            null,
            conn);

        fact.createLine(
            line,
            p.getAccount(ProductInfo.ACCTTYPE_P_Asset, as, conn),
            line.m_C_Currency_ID,
            amtCredit,
            amtDebit,
            Fact_Acct_Group_ID,
            nextSeqNo(SeqNo),
            DocumentType,
            line.m_DateAcct,
            null,
            conn);
      } else if (transactionType.equals(DocLine_CostAdjustment.TRXTYPE_MANUFACTURING)) {
        // Inventory Asset DR
        // Inventory Adjustment CR
        M_Warehouse_ID = line.getWarehouseId();
        log4jDocCostAdjustment.debug(
            "********** DocCostAdjustment - factAcct - account - "
                + getAccountByWarehouse(
                        AcctServer.ACCTTYPE_InvDifferences, as, line.getWarehouseId(), conn)
                    .C_ValidCombination_ID);
        if (line.isTransactionNegative()) {
          amtDebit = amount.toPlainString();
          amtCredit = "";
        } else {
          amtDebit = "";
          amtCredit = amount.toPlainString();
        }
        fact.createLine(
            line,
            getAccountByWarehouse(
                AcctServer.ACCTTYPE_InvDifferences, as, line.getWarehouseId(), conn),
            line.m_C_Currency_ID,
            amtDebit,
            amtCredit,
            Fact_Acct_Group_ID,
            nextSeqNo(SeqNo),
            DocumentType,
            line.m_DateAcct,
            null,
            conn);

        fact.createLine(
            line,
            p.getAccount(ProductInfo.ACCTTYPE_P_Asset, as, conn),
            line.m_C_Currency_ID,
            amtCredit,
            amtDebit,
            Fact_Acct_Group_ID,
            nextSeqNo(SeqNo),
            DocumentType,
            line.m_DateAcct,
            null,
            conn);
      }
    } // lines

    SeqNo = "0";
    return fact;
  } // createFact
  /**
   * 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
  @Override
  public ArrayList<Fact> createFacts(MAcctSchema as) {
    Fact fact = new Fact(this, as, Fact.POST_Actual);
    String sql =
        "SELECT m.HR_Concept_id, MAX(c.Name), SUM(m.Amount), MAX(c.AccountSign), MAX(CA.IsBalancing), e.AD_Org_ID, d.C_Activity_ID" // 1,2,3,4,5,6,7
            + " FROM HR_Movement m"
            + " INNER JOIN HR_Concept_Acct ca ON (ca.HR_Concept_ID=m.HR_Concept_ID AND ca.IsActive = 'Y')"
            + " INNER JOIN HR_Concept      c  ON (c.HR_Concept_ID=m.HR_Concept_ID AND c.IsActive = 'Y')"
            + " INNER JOIN C_BPartner      bp ON (bp.C_BPartner_ID = m.C_BPartner_ID)"
            + " INNER JOIN HR_Employee	 e  ON (bp.C_BPartner_ID=e.C_BPartner_ID)"
            + " INNER JOIN HR_Department   d  ON (d.HR_Department_ID=e.HR_Department_ID)"
            + " WHERE m.HR_Process_ID=? AND (m.Qty <> 0 OR m.Amount <> 0) AND c.AccountSign != 'N' AND ca.IsBalancing != 'Y'"
            + " GROUP BY m.HR_Concept_ID,e.AD_Org_ID,d.C_Activity_ID"
            + " ORDER BY e.AD_Org_ID,d.C_Activity_ID";

    ResultSet rs = null;
    PreparedStatement pstmt = null;
    try {
      pstmt = DB.prepareStatement(sql, process.get_TrxName());
      pstmt.setInt(1, process.getHR_Process_ID());
      rs = pstmt.executeQuery();
      while (rs.next()) {
        int HR_Concept_ID = rs.getInt(1);
        BigDecimal sumAmount = rs.getBigDecimal(3);
        // round amount according to currency
        sumAmount = sumAmount.setScale(as.getStdPrecision(), BigDecimal.ROUND_HALF_UP);
        String AccountSign = rs.getString(4);
        int AD_OrgTrx_ID = rs.getInt(6);
        int C_Activity_ID = rs.getInt(7);
        //
        if (AccountSign != null
            && AccountSign.length() > 0
            && (AccountSign.equals("D") || AccountSign.equals("C"))) {
          // HR_Expense_Acct    DR
          // HR_Revenue_Acct    CR
          MAccount accountBPD =
              MAccount.get(
                  getCtx(), getAccountBalancing(as.getC_AcctSchema_ID(), HR_Concept_ID, "D"));
          FactLine debit =
              fact.createLine(null, accountBPD, as.getC_Currency_ID(), sumAmount, null);
          debit.setAD_OrgTrx_ID(AD_OrgTrx_ID);
          debit.setC_Activity_ID(C_Activity_ID);
          debit.saveEx();
          MAccount accountBPC =
              MAccount.get(
                  getCtx(), this.getAccountBalancing(as.getC_AcctSchema_ID(), HR_Concept_ID, "C"));
          FactLine credit =
              fact.createLine(null, accountBPC, as.getC_Currency_ID(), null, sumAmount);
          credit.setAD_OrgTrx_ID(AD_OrgTrx_ID);
          credit.setC_Activity_ID(C_Activity_ID);
          credit.saveEx();
        }
      }
    } catch (Exception e) {
      log.log(Level.SEVERE, sql, e);
      p_Error = e.getLocalizedMessage();
      return null;
    } finally {
      DB.close(rs, pstmt);
      pstmt = null;
      rs = null;
    }

    ArrayList<Fact> facts = new ArrayList<Fact>();
    facts.add(fact);
    return facts;
  }
Beispiel #9
0
 public FeedResult addItem(Fact item) {
   if (!item.isNull()) {
     rows.add(item);
   }
   return this;
 }