use of org.compiere.model.MProduct in project adempiere by adempiere.
the class MPPOrderBOMLine method reserveStock.
/**
* Reserve Inventory for this BOM Line
*/
protected void reserveStock() {
final int header_M_Warehouse_ID = getParent().getM_Warehouse_ID();
// Check/set WH/Org
if (// enforce WH
header_M_Warehouse_ID != 0) {
if (header_M_Warehouse_ID != getM_Warehouse_ID())
setM_Warehouse_ID(header_M_Warehouse_ID);
if (getAD_Org_ID() != getAD_Org_ID())
setAD_Org_ID(getAD_Org_ID());
}
//
final BigDecimal target = getQtyRequired();
final BigDecimal difference = target.subtract(getQtyReserved()).subtract(getQtyDelivered());
log.fine("Line=" + getLine() + " - Target=" + target + ",Difference=" + difference + " - Requiered=" + getQtyRequired() + ",Reserved=" + getQtyReserved() + ",Delivered=" + getQtyDelivered());
if (difference.signum() == 0) {
return;
}
// Check Product - Stocked and Item
MProduct product = getM_Product();
if (!product.isStocked()) {
return;
}
BigDecimal reserved = difference;
int M_Locator_ID = getM_Locator_ID(reserved);
// Update Storage
if (!MStorage.add(getCtx(), getM_Warehouse_ID(), M_Locator_ID, getM_Product_ID(), getM_AttributeSetInstance_ID(), getM_AttributeSetInstance_ID(), Env.ZERO, reserved, Env.ZERO, get_TrxName())) {
throw new AdempiereException();
}
// update line
setQtyReserved(getQtyReserved().add(difference));
}
use of org.compiere.model.MProduct in project adempiere by adempiere.
the class InfoProductPanel method onEvent.
// isUnconfirmed
public void onEvent(Event e) {
if (!p_loadedOK)
return;
Component component = e.getTarget();
if (component != null) {
// Handle product info specific fields here
if (component.equals(confirmPanel.getButton(ConfirmPanel.A_PATTRIBUTE))) {
// Find the ASI used by the product on the lead row
MProduct mp = MProduct.get(Env.getCtx(), m_M_Product_ID);
// Set title and parameters for the PattributeInstance window
String title = "";
int wh_id = 0;
if (isValidVObject(fWarehouse_ID)) {
title = mp.getName() + " - " + fWarehouse_ID.getDisplay();
wh_id = ((Integer) (fWarehouse_ID.getValue())).intValue();
}
// Get the business partner from the context - it may be different than the Vendor
int bp_id = 0;
String s_bp_id = Env.getContext(Env.getCtx(), p_WindowNo, p_TabNo, "C_BPartner_ID", false);
if (s_bp_id != null && s_bp_id.length() != 0 && (new Integer(s_bp_id).intValue() > 0))
bp_id = new Integer(s_bp_id).intValue();
// Display the window
InfoPAttributeInstancePanel pai = new InfoPAttributeInstancePanel(this, title, wh_id, 0, p_table.getLeadRowKey(), bp_id);
if (!pai.wasCancelled()) {
// Get the results and update the fASI criteria field
m_M_AttributeSetInstance_ID = pai.getM_AttributeSetInstance_ID();
m_M_Locator_ID = pai.getM_Locator_ID();
if (m_M_AttributeSetInstance_ID > 0)
fASI_ID.setValue(m_M_AttributeSetInstance_ID);
else
// No instance
fASI_ID.setValue(0);
}
// looking around.
if (// If the results are saved, we can save now - an ASI is product specific
p_saveResults && m_M_AttributeSetInstance_ID != -1 && !pai.wasCancelled()) {
dispose(p_saveResults);
return;
}
return;
} else if (component instanceof Combobox) {
if (e.getName().equals("onChange")) {
// perform field-specific changes
if (component.equals(fWarehouse_ID.getComponent())) {
if (!isValidVObject(fWarehouse_ID)) {
// Disable the stock button
checkOnlyStock.setSelected(false);
checkOnlyStock.setEnabled(false);
} else
checkOnlyStock.setEnabled(true);
}
}
} else if (component instanceof Checkbox) {
Checkbox cb = (Checkbox) component;
// ShowDetail check box
if (cb.getName() != null && cb.getName().equals("ShowDetail")) {
// Refresh only the ATP tab
refreshAtpTab();
return;
}
} else if (// a tab in the ATP panel is selected
component instanceof Tab) {
if (detailTabBox.getSelectedIndex() == 5) {
checkShowDetail.setEnabled(true);
} else {
checkShowDetail.setEnabled(false);
}
refresh();
return;
}
}
//
super.onEvent(e);
}
use of org.compiere.model.MProduct 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;
}
use of org.compiere.model.MProduct in project adempiere by adempiere.
the class Doc_Production method createFacts.
// getBalance
/**
* 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 factLine = null;
BigDecimal total = Env.ZERO;
DocLine parentProductionLine = null;
for (DocLine line : p_lines) {
MProduct product = line.getProduct();
if (line.isProductionBOM())
parentProductionLine = line;
if (X_M_Product.PRODUCTTYPE_Item.equals(product.getProductType())) {
BigDecimal totalCosts = Env.ZERO;
BigDecimal qty = line.getQty();
BigDecimal costTransaction = Env.ZERO;
for (MCostDetail cost : line.getCostDetail(as, false)) {
if (!MCostDetail.existsCost(cost))
continue;
costTransaction = MCostDetail.getTotalCost(cost, as);
totalCosts = totalCosts.add(costTransaction);
}
if (qty.signum() < 0)
totalCosts = totalCosts.negate();
total = total.add(totalCosts);
factLine = fact.createLine(line, line.getAccount(ProductCost.ACCTTYPE_P_Asset, as), line.getAccount(ProductCost.ACCTTYPE_P_Asset, as), as.getC_Currency_ID(), totalCosts);
factLine.setM_Product_ID(line.getM_Product_ID());
factLine.setM_Locator_ID(line.getM_LocatorTo_ID());
factLine.setDescription("");
factLine.saveEx();
} else if (!X_M_Product.PRODUCTTYPE_Item.equals(product.getProductType())) {
BigDecimal totalCosts = Env.ZERO;
MAccount acct = null;
for (MCostElement costElement : MCostElement.getCostElement(line.getCtx(), line.getTrxName())) {
if (MCostElement.COSTELEMENTTYPE_BurdenMOverhead.equals(costElement.getCostElementType()))
acct = line.getAccount(ProductCost.ACCTTYPE_P_Burden, as);
else if (MCostElement.COSTELEMENTTYPE_OutsideProcessing.equals(costElement.getCostElementType()))
acct = line.getAccount(ProductCost.ACCTTYPE_P_OutsideProcessing, as);
else if (MCostElement.COSTELEMENTTYPE_Overhead.equals(costElement.getCostElementType()))
acct = line.getAccount(ProductCost.ACCTTYPE_P_Overhead, as);
else if (MCostElement.COSTELEMENTTYPE_Resource.equals(costElement.getCostElementType()))
acct = line.getAccount(ProductCost.ACCTTYPE_P_Labor, as);
else
acct = line.getAccount(ProductCost.ACCTTYPE_P_OutsideProcessing, as);
int warehouseId = DB.getSQLValue(line.getTrxName(), "SELECT M_Warehouse_ID from M_Locator WHERE M_Locator_ID=?", line.getM_Locator_ID());
BigDecimal costTransaction = Env.ZERO;
MCost costDimension = MCost.validateCostForCostType(as, (MCostType) as.getM_CostType(), costElement, product.getM_Product_ID(), line.getAD_Org_ID(), warehouseId, line.getM_AttributeSetInstance_ID(), line.getTrxName());
if (costDimension != null)
costTransaction = costDimension.getCurrentCostPrice().add(costDimension.getCurrentCostPriceLL());
totalCosts = totalCosts.add(costTransaction.multiply(line.getQty()));
}
factLine = fact.createLine(line, acct, acct, as.getC_Currency_ID(), totalCosts);
factLine.setM_Product_ID(line.getM_Product_ID());
factLine.setM_Locator_ID(line.getM_LocatorTo_ID());
if (m_DocStatus.equals(MProduction.DOCSTATUS_Reversed) && m_Reversal_ID != 0 && line.getReversalLine_ID() != 0) {
// Set AmtAcctDr from Original Phys.Inventory
if (!factLine.updateReverseLine(MProduction.Table_ID, m_Reversal_ID, line.getReversalLine_ID(), line.getQty(), Env.ONE)) {
p_Error = "Original Physical Inventory not posted yet";
return null;
}
}
factLine.setDescription("");
factLine.saveEx();
total = total.add(totalCosts);
}
}
// When total no is zero then create an adjustment cost , can happen when of resource rate was changes from original cost finish good
if (total.signum() != 0) {
factLine = fact.createLine(parentProductionLine, parentProductionLine.getAccount(ProductCost.ACCTTYPE_P_CostAdjustment, as), as.getC_Currency_ID(), total);
factLine.setM_Product_ID(parentProductionLine.getM_Product_ID());
factLine.setM_Locator_ID(parentProductionLine.getM_LocatorTo_ID());
factLine.setDescription(" Adjustment Cost");
factLine.saveEx();
}
ArrayList<Fact> facts = new ArrayList<Fact>();
facts.add(fact);
return facts;
}
use of org.compiere.model.MProduct in project adempiere by adempiere.
the class Doc_MatchPO method createFacts.
// getBalance
/**
* Create Facts (the accounting logic) for
* MXP.
* <pre>
* Product PPV <difference>
* PPV_Offset <difference>
* </pre>
* @param as accounting schema
* @return Fact
*/
public ArrayList<Fact> createFacts(MAcctSchema as) {
ArrayList<Fact> facts = new ArrayList<Fact>();
//
if (// Nothing to do if no Product
getM_Product_ID() == 0 || getQty().signum() == 0 || // No posting if not matched to Shipment
m_M_InOutLine_ID == 0) {
log.fine("No Product/Qty - M_Product_ID=" + getM_Product_ID() + ",Qty=" + getQty());
return facts;
}
// create Fact Header
Fact fact = new Fact(this, as, Fact.POST_Actual);
setC_Currency_ID(as.getC_Currency_ID());
boolean isInterOrg = isInterOrg(as);
// Purchase Order Line
BigDecimal poCost = m_oLine.getPriceCost();
if (poCost == null || poCost.signum() == 0)
poCost = m_oLine.getPriceActual();
MInOutLine receiptLine = new MInOutLine(getCtx(), m_M_InOutLine_ID, getTrxName());
MInOut inOut = receiptLine.getParent();
boolean isReturnTrx = inOut.getMovementType().equals(X_M_InOut.MOVEMENTTYPE_VendorReturns);
// calculate po cost
// Delivered so far
poCost = poCost.multiply(getQty());
// Different currency
if (m_oLine.getC_Currency_ID() != as.getC_Currency_ID()) {
MOrder order = m_oLine.getParent();
Timestamp dateAcct = order.getDateAcct();
BigDecimal rate = MConversionRate.getRate(order.getC_Currency_ID(), as.getC_Currency_ID(), dateAcct, order.getC_ConversionType_ID(), m_oLine.getAD_Client_ID(), m_oLine.getAD_Org_ID());
if (rate == null) {
p_Error = "Purchase Order not convertible - " + as.getName();
return null;
}
poCost = poCost.multiply(rate);
if (poCost.scale() > as.getCostingPrecision())
poCost = poCost.setScale(as.getCostingPrecision(), BigDecimal.ROUND_HALF_UP);
}
// Calculate PPV for standard costing
//get standard cost and also make sure cost for other costing method is updated
MProduct product = MProduct.get(getCtx(), getM_Product_ID());
MCostType ct = MCostType.get(as, getM_Product_ID(), getAD_Org_ID());
String costingMethod = ct.getCostingMethod();
MInOutLine ioLine = new MInOutLine(getCtx(), m_M_InOutLine_ID, getTrxName());
BigDecimal costs = Env.ZERO;
for (MTransaction transaction : MTransaction.getByInOutLine(ioLine)) {
MCostElement element = MCostElement.getByMaterialCostElementType(transaction);
MCostDetail cd = MCostDetail.getByTransaction(ioLine, transaction, as.getC_AcctSchema_ID(), ct.getM_CostType_ID(), element.getM_CostElement_ID());
if (cd != null)
costs = costs.add(MCostDetail.getTotalCost(cd, as));
}
if (MCostType.COSTINGMETHOD_StandardCosting.equals(costingMethod)) {
// No Costs yet - no PPV
if (costs == null || costs.signum() == 0) {
// TODO make these a translatable message
p_Error = "No Standard Cost information was found for " + product.getName() + ". Please add a standard cost for material and repost this document.";
log.log(Level.SEVERE, p_Error);
return null;
}
// Difference
BigDecimal difference = poCost.subtract(costs.setScale(as.getCostingPrecision(), BigDecimal.ROUND_HALF_UP));
// Nothing to post
if (difference.signum() == 0) {
log.log(Level.FINE, "No Cost Difference for M_Product_ID=" + getM_Product_ID());
return facts;
}
// Product PPV
FactLine cr = fact.createLine(null, m_pc.getAccount(ProductCost.ACCTTYPE_P_PPV, as), as.getC_Currency_ID(), isReturnTrx ? difference.negate() : difference);
if (cr != null) {
cr.setQty(isReturnTrx ? getQty().negate() : getQty());
cr.setC_BPartner_ID(m_oLine.getC_BPartner_ID());
cr.setC_Activity_ID(m_oLine.getC_Activity_ID());
cr.setC_Campaign_ID(m_oLine.getC_Campaign_ID());
cr.setC_Project_ID(m_oLine.getC_Project_ID());
cr.setC_ProjectPhase_ID(m_oLine.getC_ProjectPhase_ID());
cr.setC_ProjectTask_ID(m_oLine.getC_ProjectTask_ID());
cr.setC_UOM_ID(m_oLine.getC_UOM_ID());
cr.setUser1_ID(m_oLine.getUser1_ID());
cr.setUser2_ID(m_oLine.getUser2_ID());
cr.setUser3_ID(m_oLine.getUser3_ID());
cr.setUser4_ID(m_oLine.getUser4_ID());
}
// PPV Offset
FactLine dr = fact.createLine(null, getAccount(Doc.ACCTTYPE_PPVOffset, as), as.getC_Currency_ID(), isReturnTrx ? difference : difference.negate());
if (dr != null) {
dr.setQty(isReturnTrx ? getQty() : getQty().negate());
dr.setC_BPartner_ID(m_oLine.getC_BPartner_ID());
dr.setC_Activity_ID(m_oLine.getC_Activity_ID());
dr.setC_Campaign_ID(m_oLine.getC_Campaign_ID());
dr.setC_Project_ID(m_oLine.getC_Project_ID());
dr.setC_ProjectPhase_ID(m_oLine.getC_ProjectPhase_ID());
dr.setC_ProjectTask_ID(m_oLine.getC_ProjectTask_ID());
dr.setC_UOM_ID(m_oLine.getC_UOM_ID());
dr.setUser1_ID(m_oLine.getUser1_ID());
dr.setUser2_ID(m_oLine.getUser2_ID());
dr.setUser3_ID(m_oLine.getUser3_ID());
dr.setUser4_ID(m_oLine.getUser4_ID());
}
// Avoid usage of clearing accounts
// If both accounts Purchase Price Variance and Purchase Price Variance Offset are equal
// then remove the posting
// PPV
MAccount acct_db = dr.getAccount();
// PPV Offset
MAccount acct_cr = cr.getAccount();
if ((!as.isPostIfClearingEqual()) && acct_db.equals(acct_cr) && (!isInterOrg)) {
BigDecimal debit = dr.getAmtSourceDr();
BigDecimal credit = cr.getAmtSourceCr();
if (debit.compareTo(credit) == 0) {
fact.remove(dr);
fact.remove(cr);
}
}
// End Avoid usage of clearing accounts
//
facts.add(fact);
return facts;
} else {
return facts;
}
}
Aggregations