Search in sources :

Example 6 with MCostDetail

use of org.compiere.model.MCostDetail in project adempiere by adempiere.

the class FifoLifoCostingMethod method processCostDetail.

//	need this for reversal documents
public void processCostDetail(MCostDetail costDetail) {
    boolean addition = costDetail.getQty().signum() > 0;
    MAcctSchema as = MAcctSchema.get(costDetail.getCtx(), costDetail.getC_AcctSchema_ID());
    int precision = as.getCostingPrecision();
    MProduct product = MProduct.get(costDetail.getCtx(), costDetail.getM_Product_ID());
    BigDecimal price = costDetail.getAmt();
    if (costDetail.getQty().signum() != 0)
        price = costDetail.getAmt().divide(costDetail.getQty(), precision, BigDecimal.ROUND_HALF_UP);
    int AD_Org_ID = costDetail.getAD_Org_ID();
    int M_ASI_ID = costDetail.getM_AttributeSetInstance_ID();
    if (costDetail.getC_OrderLine_ID() != 0 && !(model.getReversalLine_ID() > 0)) {
        log.finer("Inv - FiFo/LiFo - amt=" + costDetail.getAmt() + ", qty=" + costDetail.getQty() + " [NOTHING TO DO]");
    } else if (//	AR Shipment Detail Record
    costDetail.getM_InOutLine_ID() != 0 || costDetail.getM_MovementLine_ID() != 0 || costDetail.getM_InventoryLine_ID() != 0 || costDetail.getM_ProductionLine_ID() != 0 || costDetail.getC_ProjectIssue_ID() != 0 || costDetail.getPP_Cost_Collector_ID() != 0 || costDetail.getC_LandedCostAllocation_ID() != 0) {
        if (addition) {
            MCostQueue.add(product, M_ASI_ID, as, AD_Org_ID, costDetail.getM_CostElement_ID(), costDetail.getAmt(), costDetail.getQty(), precision, (MCostDetail) costDetail, costDetail.get_TrxName());
        } else {
            BigDecimal amtQueue = MCostQueue.adjustQty(dimension, costDetail.getQty().negate(), costDetail.getDateAcct(), costDetail.get_TrxName());
            // outgoing amt should be negative
            amtQueue = amtQueue.negate();
            if (costDetail.getAmt().compareTo(amtQueue) != 0) {
                BigDecimal priceQueue = Env.ZERO;
                if (costDetail.getQty().signum() != 0)
                    priceQueue = amtQueue.divide(costDetail.getQty(), precision, BigDecimal.ROUND_HALF_UP);
                log.warning("Amt not match " + this + ": price=" + price + ", priceQueue=" + priceQueue + " [ADJUSTED]");
                // FIXME: teo_sarca: should not happen
                if ("Y".equals(Env.getContext(costDetail.getCtx(), "#M_CostDetail_CorrectAmt"))) {
                    costDetail.setAmt(amtQueue);
                    costDetail.setAmt(amtQueue);
                    costDetail.setPrice(priceQueue);
                } else {
                    throw new AdempiereException("Amt not match " + this + ": price=" + price + ", priceQueue=" + priceQueue);
                }
            }
        }
        costDetail.setCumulatedQty(dimension.getCumulatedQty());
        costDetail.setCumulatedAmt(dimension.getCumulatedQty());
        costDetail.setCumulatedAmtLL(getNewAccumulatedAmountLowerLevel(lastCostDetail));
        costDetail.setCurrentCostPrice(dimension.getCurrentCostPrice());
        updateCurrentCost(costDetail);
        costDetail.saveEx();
        this.costDetail = costDetail;
    }
}
Also used : MAcctSchema(org.compiere.model.MAcctSchema) MProduct(org.compiere.model.MProduct) AdempiereException(org.adempiere.exceptions.AdempiereException) MCostDetail(org.compiere.model.MCostDetail) BigDecimal(java.math.BigDecimal)

Example 7 with MCostDetail

use of org.compiere.model.MCostDetail in project adempiere by adempiere.

the class FifoLifoCostingMethod method adjustementQueue.

public void adjustementQueue(MCostDetail costDetail) {
    final List<MCostDetail> cds;
    if (costDetail.getCostAdjustmentDate() != null) {
        cds = MCostDetail.getAfterDate(costDetail, costingLevel);
    } else
        cds = MCostDetail.getAfterDate(costDetail, costingLevel);
    List<Object> list = new ArrayList<Object>();
    for (MCostDetail cd : cds) {
        if (cd == null)
            throw new AdempiereException("Error do not exist adjustment");
        MCostQueue cq = MCostQueue.getQueueForAdjustment(cd, dimension, model.get_TrxName());
        MTransaction trx = get(cd);
        //second condition - delayed transaction
        if ((!(cq.getCurrentQty().compareTo(Env.ZERO) == 0) && (trx.getMovementType().equals("C-") || trx.getMovementType().equals("I+") || trx.getMovementType().equals("I-")) && cd.getCostAdjustmentDate() != null) || ((trx.getMovementType().equals("C-") || trx.getMovementType().equals("I+") || trx.getMovementType().equals("I-")) && transaction.getMovementType().endsWith("+"))) {
            cq.addCurrentQty(cd.getQty().negate());
            cq.saveEx();
            cd.setProcessed(false);
            cd.setAmt(cd.getQty().multiply(costThisLevel.add(costLowLevel)));
            cd.saveEx();
            list.add(cd);
        } else if (trx.getMovementType().equals("V+") && costDetail.getCostAdjustmentDate() != null) {
            cd.setProcessed(false);
            cd.setAmt(amount);
            cd.saveEx();
            cq.setCurrentCostPrice(cd.getAmt().divide(cd.getQty()));
            cq.saveEx();
            break;
        } else if (trx.getMovementType().equals("M+") || trx.getMovementType().equals("M-")) {
            MTransaction trxTo;
            if (trx.getMovementType().equals("M+"))
                trxTo = getPrevious(trx);
            else
                trxTo = getNext(trx);
            cd.setProcessed(false);
            if (CostDimension.isSameCostDimension(accountSchema, trx, trxTo)) {
                cd.setAmt(cd.getQty().multiply(costThisLevel.add(costLowLevel)));
                cd.saveEx();
            } else {
                cq.addCurrentQty(cd.getQty().negate());
                cd.setAmt(cd.getQty().multiply(costThisLevel.add(costLowLevel)));
                cd.saveEx();
                if (trx.getMovementType().equals("M+"))
                    cq.setCurrentCostPrice(cd.getAmt().divide(cd.getQty()));
                if (cq.getCurrentCostPrice().compareTo(Env.ZERO) == 0)
                    cq.setCurrentCostPrice(cd.getCurrentCostPrice());
                cq.saveEx();
                list.add(cd);
            }
        } else
            continue;
    }
    for (MCostDetail cd : list.toArray(new MCostDetail[list.size()])) {
        processCostDetail(cd);
    }
}
Also used : AdempiereException(org.adempiere.exceptions.AdempiereException) ArrayList(java.util.ArrayList) MTransaction(org.compiere.model.MTransaction) MCostQueue(org.compiere.model.MCostQueue) MCostDetail(org.compiere.model.MCostDetail)

Example 8 with MCostDetail

use of org.compiere.model.MCostDetail in project adempiere by adempiere.

the class CostEngine method getCostLowLevel.

/**
	 * get cost this level
	 * @param accountSchema
	 * @param costType
	 * @param costElement
	 * @param transaction
	 * @param model
	 * @param costingLevel
	 * @return
	 */
public static BigDecimal getCostLowLevel(MAcctSchema accountSchema, I_M_CostType costType, I_M_CostElement costElement, MTransaction transaction, IDocumentLine model, String costingLevel) {
    BigDecimal costLowLevel = Env.ZERO;
    MCostDetail lastCostDetail = MCostDetail.getLastTransaction(model, transaction, accountSchema.getC_AcctSchema_ID(), costType.getM_CostType_ID(), costElement.getM_CostElement_ID(), model.getDateAcct(), costingLevel);
    if (lastCostDetail != null) {
        // (Total Cost transaction + cost adjustments) divide by transaction quantity
        if (lastCostDetail.getQty().signum() != 0) {
            costLowLevel = lastCostDetail.getCostAmtLL().add(lastCostDetail.getCostAdjustmentLL()).divide(lastCostDetail.getQty(), accountSchema.getCostingPrecision(), BigDecimal.ROUND_HALF_UP).abs();
        } else // (Total Cost Transaction + cost adjustments + accumulate cost) divide between on hand quantity
        if (lastCostDetail.getCumulatedQty().add(lastCostDetail.getQty()).signum() != 0) {
            costLowLevel = lastCostDetail.getCostAmtLL().add(lastCostDetail.getCostAdjustmentLL()).add(lastCostDetail.getCumulatedAmtLL()).divide(lastCostDetail.getCumulatedQty().add(lastCostDetail.getQty()), accountSchema.getCostingPrecision(), BigDecimal.ROUND_HALF_UP).abs();
            return costLowLevel;
        } else // (Total Cost transaction + cost adjustments + Cumulated amount) divide by On hand Quantity
        if (lastCostDetail.getCumulatedQty().signum() != 0) {
            costLowLevel = lastCostDetail.getCumulatedAmtLL().divide(lastCostDetail.getCumulatedQty(), accountSchema.getCostingPrecision(), BigDecimal.ROUND_HALF_UP).abs();
            return costLowLevel;
        }
    }
    return costLowLevel;
}
Also used : MCostDetail(org.compiere.model.MCostDetail) BigDecimal(java.math.BigDecimal)

Example 9 with MCostDetail

use of org.compiere.model.MCostDetail in project adempiere by adempiere.

the class AveragePOCostingMethod method createCostDetail.

private void createCostDetail() {
    //Validate if model have a reverses and processing of reverse
    if (model.getReversalLine_ID() > 0 && costDetail == null) {
        createReversalCostDetail();
        return;
    } else if (model.getReversalLine_ID() > 0)
        return;
    int seqNo = lastCostDetail.getSeqNo() + 10;
    // adjustment
    if (transaction.getM_Transaction_ID() != lastCostDetail.getM_Transaction_ID() && costDetail == null || adjustCost.add(adjustCostLowerLevel).signum() != 0 && costDetail == null) {
        // if exist adjustment cost for Landed Cost Allocation or Match Inv then set the movement qty to zero
        if ((adjustCost.add(adjustCostLowerLevel).signum() != 0 && costDetail != null) || (model instanceof MLandedCostAllocation || model instanceof MMatchPO))
            movementQuantity = Env.ZERO;
        // create new cost detail
        costDetail = new MCostDetail(transaction, accountSchema.getC_AcctSchema_ID(), dimension.getM_CostType_ID(), dimension.getM_CostElement_ID(), currentCostPrice.multiply(movementQuantity).abs(), currentCostPriceLowerLevel.multiply(movementQuantity).abs(), movementQuantity, transaction.get_TrxName());
        // set account date for this cost detail
        costDetail.setDateAcct(dateAccounting);
        costDetail.setSeqNo(seqNo);
        // set transaction id
        if (transaction != null)
            costDetail.setM_Transaction_ID(transaction.getM_Transaction_ID());
        // set if transaction is sales order type or not
        if (isSalesTransaction != null)
            costDetail.setIsSOTrx(isSalesTransaction);
        else
            costDetail.setIsSOTrx(model.isSOTrx());
        if (adjustCost.signum() != 0 || adjustCostLowerLevel.signum() != 0) {
            String description = costDetail.getDescription() != null ? costDetail.getDescription() : "";
            // update adjustment cost this level
            if (adjustCost.signum() != 0) {
                costDetail.setCostAdjustmentDate(model.getDateAcct());
                costDetail.setCostAdjustment(adjustCost);
                costDetail.setCostAmt(BigDecimal.ZERO);
                costDetail.setAmt(costDetail.getAmt().add(costDetail.getCostAdjustment()));
                costDetail.setDescription(description + " Adjust Cost:" + adjustCost);
            }
            // update adjustment cost lower level
            if (adjustCostLowerLevel.signum() != 0) {
                description = costDetail.getDescription() != null ? costDetail.getDescription() : "";
                costDetail.setCostAdjustmentDateLL(model.getDateAcct());
                costDetail.setCostAdjustmentLL(adjustCostLowerLevel);
                costDetail.setCostAmtLL(BigDecimal.ZERO);
                costDetail.setAmt(costDetail.getCostAmtLL().add(costDetail.getCostAdjustmentLL()));
                costDetail.setDescription(description + " Adjust Cost LL:" + adjustCost);
            }
        }
        updateAmountCost();
        return;
    }
}
Also used : MLandedCostAllocation(org.compiere.model.MLandedCostAllocation) MMatchPO(org.compiere.model.MMatchPO) MCostDetail(org.compiere.model.MCostDetail)

Example 10 with MCostDetail

use of org.compiere.model.MCostDetail in project adempiere by adempiere.

the class Doc_InOut method createFacts.

//  getBalance
/**
	 *  Create Facts (the accounting logic) for
	 *  MMS, MMR.
	 *  <pre>
	 *  Shipment
	 *      CoGS (RevOrg)   DR
	 *      Inventory               CR
	 *  Shipment of Project Issue
	 *      CoGS            DR
	 *      Project                 CR
	 *  Receipt
	 *      Inventory       DR
	 *      NotInvoicedReceipt      CR
	 *  </pre>
	 *  @param as accounting schema
	 *  @return Fact
	 */
public ArrayList<Fact> createFacts(MAcctSchema as) {
    //
    ArrayList<Fact> facts = new ArrayList<Fact>();
    //  create Fact Header
    Fact fact = new Fact(this, as, Fact.POST_Actual);
    setC_Currency_ID(as.getC_Currency_ID());
    //  Line pointers
    FactLine dr = null;
    FactLine cr = null;
    //  *** Sales - Shipment
    if (getDocumentType().equals(DOCTYPE_MatShipment) && isSOTrx()) {
        BigDecimal total = Env.ZERO;
        for (int i = 0; i < p_lines.length; i++) {
            DocLine line = p_lines[i];
            BigDecimal costs = null;
            MProduct product = line.getProduct();
            for (MCostDetail cost : line.getCostDetail(as, false)) {
                if (!MCostDetail.existsCost(cost))
                    continue;
                costs = MCostDetail.getTotalCost(cost, as);
                total = total.add(costs);
                //get costing method for product
                String description = cost.getM_CostElement().getName() + " " + cost.getM_CostType().getName();
                //  CoGS            DR
                dr = fact.createLine(line, line.getAccount(ProductCost.ACCTTYPE_P_Cogs, as), as.getC_Currency_ID(), costs, null);
                if (dr == null) {
                    p_Error = "FactLine DR not created: " + line;
                    log.log(Level.WARNING, p_Error);
                    return null;
                }
                dr.setM_Locator_ID(line.getM_Locator_ID());
                //  from Loc
                dr.setLocationFromLocator(line.getM_Locator_ID(), true);
                //  to Loc
                dr.setLocationFromBPartner(getC_BPartner_Location_ID(), false);
                //	Revenue X-Org
                dr.setAD_Org_ID(line.getOrder_Org_ID());
                dr.setM_Product_ID(cost.getM_Product_ID());
                dr.setQty(cost.getQty().negate());
                dr.addDescription(description);
                if (m_DocStatus.equals(MInOut.DOCSTATUS_Reversed) && m_Reversal_ID != 0 && line.getReversalLine_ID() != 0) {
                    //	Set AmtAcctDr from Original Shipment/Receipt
                    if (!dr.updateReverseLine(MInOut.Table_ID, m_Reversal_ID, line.getReversalLine_ID(), cost.getQty().negate(), Env.ONE.negate())) {
                        p_Error = "Original Shipment/Receipt not posted yet";
                        return null;
                    }
                }
                //  Inventory               CR
                cr = fact.createLine(line, line.getAccount(ProductCost.ACCTTYPE_P_Asset, as), as.getC_Currency_ID(), null, costs);
                if (cr == null) {
                    p_Error = "FactLine CR not created: " + line;
                    log.log(Level.WARNING, p_Error);
                    return null;
                }
                cr.setM_Locator_ID(line.getM_Locator_ID());
                // from Loc
                cr.setLocationFromLocator(line.getM_Locator_ID(), true);
                // to Loc
                cr.setLocationFromBPartner(getC_BPartner_Location_ID(), false);
                cr.addDescription(description);
                cr.setM_Product_ID(cost.getM_Product_ID());
                cr.setQty(cost.getQty());
                if (m_DocStatus.equals(MInOut.DOCSTATUS_Reversed) && m_Reversal_ID != 0 && line.getReversalLine_ID() != 0) {
                    //	Set AmtAcctCr from Original Shipment/Receipt
                    if (!cr.updateReverseLine(MInOut.Table_ID, m_Reversal_ID, line.getReversalLine_ID(), cost.getQty(), Env.ONE.negate())) {
                        p_Error = "Original Shipment/Receipt not posted yet";
                        return null;
                    }
                    //get original cost
                    costs = cr.getAcctBalance();
                }
            }
            // costing elements
            if (//	zero costs OK
            total == null || total.signum() == 0) {
            /*if (product.isStocked())
					{
						p_Error = "No Costs for " + line.getProduct().getName();
						log.log(Level.WARNING, p_Error);
						return null;
					}
					else	//	ignore service
						continue;
					*/
            }
        }
        /** Commitment release										****/
        if (as.isAccrual() && as.isCreateSOCommitment()) {
            for (int i = 0; i < p_lines.length; i++) {
                DocLine line = p_lines[i];
                BigDecimal multiplier = Env.ONE.negate();
                if (m_Reversal_ID != 0 && m_Reversal_ID < get_ID())
                    multiplier = multiplier.negate();
                Fact factcomm = Doc_Order.getCommitmentSalesRelease(as, this, line.getQty(), line.get_ID(), multiplier);
                if (factcomm != null)
                    facts.add(factcomm);
            }
        }
    //	Commitment
    } else //	  *** Sales - Return
    if (getDocumentType().equals(DOCTYPE_MatReceipt) && isSOTrx()) {
        BigDecimal total = Env.ZERO;
        for (int i = 0; i < p_lines.length; i++) {
            DocLine line = p_lines[i];
            BigDecimal costs = null;
            MProduct product = line.getProduct();
            for (MCostDetail cost : line.getCostDetail(as, false)) {
                if (!MCostDetail.existsCost(cost))
                    continue;
                costs = MCostDetail.getTotalCost(cost, as);
                total = total.add(costs);
                String description = cost.getM_CostElement().getName() + " " + cost.getM_CostType().getName();
                //  Inventory               DR
                dr = fact.createLine(line, line.getAccount(ProductCost.ACCTTYPE_P_Asset, as), as.getC_Currency_ID(), costs, null);
                if (dr == null) {
                    p_Error = "FactLine DR not created: " + line;
                    log.log(Level.WARNING, p_Error);
                    return null;
                }
                dr.setM_Locator_ID(line.getM_Locator_ID());
                // from Loc
                dr.setLocationFromLocator(line.getM_Locator_ID(), true);
                // to Loc
                dr.setLocationFromBPartner(getC_BPartner_Location_ID(), false);
                dr.addDescription(description);
                dr.setM_Product_ID(cost.getM_Product_ID());
                dr.setQty(cost.getQty());
                if (m_DocStatus.equals(MInOut.DOCSTATUS_Reversed) && m_Reversal_ID != 0 && line.getReversalLine_ID() != 0) {
                    //	Set AmtAcctDr from Original Shipment/Receipt
                    if (!dr.updateReverseLine(MInOut.Table_ID, m_Reversal_ID, line.getReversalLine_ID(), cost.getQty(), Env.ONE.negate())) {
                        p_Error = "Original Shipment/Receipt not posted yet";
                        return null;
                    }
                    //get original cost
                    costs = dr.getAcctBalance();
                }
                //  CoGS            CR
                cr = fact.createLine(line, line.getAccount(ProductCost.ACCTTYPE_P_Cogs, as), as.getC_Currency_ID(), null, costs);
                if (cr == null) {
                    p_Error = "FactLine CR not created: " + line;
                    log.log(Level.WARNING, p_Error);
                    return null;
                }
                cr.setM_Locator_ID(line.getM_Locator_ID());
                //  from Loc
                cr.setLocationFromLocator(line.getM_Locator_ID(), true);
                //  to Loc
                cr.setLocationFromBPartner(getC_BPartner_Location_ID(), false);
                //	Revenue X-Org
                cr.setAD_Org_ID(line.getOrder_Org_ID());
                cr.setM_Product_ID(cost.getM_Product_ID());
                cr.setQty(cost.getQty().negate());
                cr.addDescription(description);
                if (m_DocStatus.equals(MInOut.DOCSTATUS_Reversed) && m_Reversal_ID != 0 && line.getReversalLine_ID() != 0) {
                    //	Set AmtAcctCr from Original Shipment/Receipt
                    if (!cr.updateReverseLine(MInOut.Table_ID, m_Reversal_ID, line.getReversalLine_ID(), cost.getQty().negate(), Env.ONE.negate())) {
                        p_Error = "Original Shipment/Receipt not posted yet";
                        return null;
                    }
                }
            }
            if (//	zero costs OK
            total == null || total.signum() == 0) {
            /*if (product.isStocked())
					{
						p_Error = "No Costs for " + line.getProduct().getName();
						log.log(Level.WARNING, p_Error);
						return null;
					}
					else	//	ignore service
						continue;
					*/
            }
        }
    //	for all lines
    } else //  *** Purchasing - Receipt
    if (getDocumentType().equals(DOCTYPE_MatReceipt) && !isSOTrx()) {
        BigDecimal total = Env.ZERO;
        for (int i = 0; i < p_lines.length; i++) {
            int C_Currency_ID = as.getC_Currency_ID();
            //
            DocLine line = p_lines[i];
            BigDecimal costs = null;
            MProduct product = line.getProduct();
            for (MCostDetail cost : line.getCostDetail(as, true)) {
                if (!MCostDetail.existsCost(cost))
                    continue;
                costs = MCostDetail.getTotalCost(cost, as);
                total = total.add(costs);
                String description = cost.getM_CostElement().getName() + " " + cost.getM_CostType().getName();
                //  Inventory/Asset			DR
                MAccount assets = line.getAccount(ProductCost.ACCTTYPE_P_Asset, as);
                if (product.isService()) {
                    //if the line is a Outside Processing then DR WIP
                    if (line.getPP_Cost_Collector_ID() > 0)
                        assets = line.getAccount(ProductCost.ACCTTYPE_P_WorkInProcess, as);
                    else
                        assets = line.getAccount(ProductCost.ACCTTYPE_P_Expense, as);
                }
                dr = fact.createLine(line, assets, C_Currency_ID, costs, null);
                dr.addDescription(description);
                //
                if (dr == null) {
                    p_Error = "DR not created: " + line;
                    log.log(Level.WARNING, p_Error);
                    return null;
                }
                dr.setM_Locator_ID(line.getM_Locator_ID());
                // from Loc
                dr.setLocationFromBPartner(getC_BPartner_Location_ID(), true);
                // to Loc
                dr.setLocationFromLocator(line.getM_Locator_ID(), false);
                dr.setM_Product_ID(cost.getM_Product_ID());
                dr.setQty(cost.getQty());
                if (m_DocStatus.equals(MInOut.DOCSTATUS_Reversed) && m_Reversal_ID != 0 && line.getReversalLine_ID() != 0) {
                    //	Set AmtAcctDr from Original Shipment/Receipt
                    if (!dr.updateReverseLine(MInOut.Table_ID, m_Reversal_ID, line.getReversalLine_ID(), cost.getQty(), Env.ONE.negate())) {
                        p_Error = "Original Receipt not posted yet";
                        return null;
                    }
                }
                cr = fact.createLine(line, getAccount(Doc.ACCTTYPE_NotInvoicedReceipts, as), C_Currency_ID, null, costs);
                cr.addDescription(description);
                //
                if (cr == null) {
                    p_Error = "CR not created: " + line;
                    log.log(Level.WARNING, p_Error);
                    return null;
                }
                cr.setM_Locator_ID(line.getM_Locator_ID());
                //  from Loc
                cr.setLocationFromBPartner(getC_BPartner_Location_ID(), true);
                //  to Loc
                cr.setLocationFromLocator(line.getM_Locator_ID(), false);
                cr.setM_Product_ID(cost.getM_Product_ID());
                cr.setQty(cost.getQty().negate());
                if (m_DocStatus.equals(MInOut.DOCSTATUS_Reversed) && m_Reversal_ID != 0 && line.getReversalLine_ID() != 0) {
                    //	Set AmtAcctCr from Original Shipment/Receipt
                    if (!cr.updateReverseLine(MInOut.Table_ID, m_Reversal_ID, line.getReversalLine_ID(), cost.getQty().negate(), Env.ONE.negate())) {
                        p_Error = "Original Receipt not posted yet";
                        return null;
                    }
                }
                cost.setProcessed(true);
                cost.saveEx();
            }
        /*if (total == null || total.signum() == 0)
					{
						p_Error = "Resubmit - No Costs for " + product.getName();
						log.log(Level.WARNING, p_Error);
						return null;
					}*/
        }
    } else //	  *** Purchasing - return
    if (getDocumentType().equals(DOCTYPE_MatShipment) && !isSOTrx()) {
        BigDecimal total = Env.ZERO;
        for (int i = 0; i < p_lines.length; i++) {
            int C_Currency_ID = as.getC_Currency_ID();
            //
            DocLine line = p_lines[i];
            BigDecimal costs = null;
            MProduct product = line.getProduct();
            for (MCostDetail cost : line.getCostDetail(as, true)) {
                if (!MCostDetail.existsCost(cost))
                    continue;
                costs = MCostDetail.getTotalCost(cost, as);
                total = total.add(costs);
                String description = cost.getM_CostElement().getName() + " " + cost.getM_CostType().getName();
                dr = fact.createLine(line, getAccount(Doc.ACCTTYPE_NotInvoicedReceipts, as), C_Currency_ID, costs, null);
                dr.addDescription(description);
                //
                if (dr == null) {
                    p_Error = "CR not created: " + line;
                    log.log(Level.WARNING, p_Error);
                    return null;
                }
                dr.setM_Locator_ID(line.getM_Locator_ID());
                //  from Loc
                dr.setLocationFromBPartner(getC_BPartner_Location_ID(), true);
                //  to Loc
                dr.setLocationFromLocator(line.getM_Locator_ID(), false);
                dr.setM_Product_ID(cost.getM_Product_ID());
                dr.setQty(cost.getQty().negate());
                if (m_DocStatus.equals(MInOut.DOCSTATUS_Reversed) && m_Reversal_ID != 0 && line.getReversalLine_ID() != 0) {
                    //	Set AmtAcctDr from Original Shipment/Receipt
                    if (!dr.updateReverseLine(MInOut.Table_ID, m_Reversal_ID, line.getReversalLine_ID(), cost.getQty().negate(), Env.ONE.negate())) {
                        p_Error = "Original Receipt not posted yet";
                        return null;
                    }
                }
                //  Inventory/Asset			CR
                MAccount assets = line.getAccount(ProductCost.ACCTTYPE_P_Asset, as);
                if (product.isService())
                    assets = line.getAccount(ProductCost.ACCTTYPE_P_Expense, as);
                cr = fact.createLine(line, assets, C_Currency_ID, null, costs);
                //
                if (cr == null) {
                    p_Error = "DR not created: " + line;
                    log.log(Level.WARNING, p_Error);
                    return null;
                }
                cr.setM_Locator_ID(line.getM_Locator_ID());
                // from Loc
                cr.setLocationFromBPartner(getC_BPartner_Location_ID(), true);
                // to Loc
                cr.setLocationFromLocator(line.getM_Locator_ID(), false);
                cr.addDescription(description);
                cr.setM_Product_ID(cost.getM_Product_ID());
                cr.setQty(cost.getQty());
                if (m_DocStatus.equals(MInOut.DOCSTATUS_Reversed) && m_Reversal_ID != 0 && line.getReversalLine_ID() != 0) {
                    //	Set AmtAcctCr from Original Shipment/Receipt
                    if (!cr.updateReverseLine(MInOut.Table_ID, m_Reversal_ID, line.getReversalLine_ID(), cost.getQty(), Env.ONE.negate())) {
                        p_Error = "Original Receipt not posted yet";
                        return null;
                    }
                }
            }
        /*if (total == null || total.signum() == 0)
				{
					p_Error = "Resubmit - No Costs for " + product.getName();
					log.log(Level.WARNING, p_Error);
					return null;
				}*/
        }
    } else //	Purchasing Return
    {
        p_Error = "DocumentType unknown: " + getDocumentType();
        log.log(Level.SEVERE, p_Error);
        return null;
    }
    //
    facts.add(fact);
    return facts;
}
Also used : MProduct(org.compiere.model.MProduct) MAccount(org.compiere.model.MAccount) ArrayList(java.util.ArrayList) MCostDetail(org.compiere.model.MCostDetail) BigDecimal(java.math.BigDecimal)

Aggregations

MCostDetail (org.compiere.model.MCostDetail)27 BigDecimal (java.math.BigDecimal)15 ArrayList (java.util.ArrayList)9 MProduct (org.compiere.model.MProduct)9 MAccount (org.compiere.model.MAccount)8 MCostElement (org.compiere.model.MCostElement)5 MLandedCostAllocation (org.compiere.model.MLandedCostAllocation)4 MTransaction (org.compiere.model.MTransaction)4 AdempiereException (org.adempiere.exceptions.AdempiereException)3 MCostQueue (org.compiere.model.MCostQueue)3 MMatchInv (org.compiere.model.MMatchInv)3 MMatchPO (org.compiere.model.MMatchPO)3 Timestamp (java.sql.Timestamp)2 MCostType (org.compiere.model.MCostType)2 Date (java.sql.Date)1 StandardCostingMethod (org.adempiere.engine.StandardCostingMethod)1 I_C_OrderLine (org.compiere.model.I_C_OrderLine)1 I_M_CostElement (org.compiere.model.I_M_CostElement)1 I_M_InOutLine (org.compiere.model.I_M_InOutLine)1 MAcctSchema (org.compiere.model.MAcctSchema)1