use of org.compiere.model.MTransaction 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;
}
}
use of org.compiere.model.MTransaction in project adempiere by adempiere.
the class GenerateCostDetail method generateCostDetail.
public void generateCostDetail() {
//Generate Costdetail
KeyNamePair[] transactions = getTransactionIdsByDateAcct();
// System.out.println("Transaction to process : " + transactions.length);
Integer process = 0;
Integer productId = 0;
boolean processNewProduct = true;
Trx dbTransaction = null;
try {
//Process transaction
for (KeyNamePair keyNamePair : transactions) {
int transactionId = keyNamePair.getKey();
int transactionProductId = new Integer(keyNamePair.getName());
//Detected a new product
if (productId != transactionProductId) {
//commit last transaction by product
if (dbTransaction != null) {
dbTransaction.commit(true);
dbTransaction.close();
}
productId = transactionProductId;
processNewProduct = true;
//Create new transaction for this product
dbTransaction = Trx.get(productId.toString(), true);
}
MTransaction transaction = new MTransaction(getCtx(), transactionId, dbTransaction.getTrxName());
// for each Account Schema
for (MAcctSchema accountSchema : acctSchemas) {
// for each Cost Type
for (MCostType costType : costTypes) {
// for each Cost Element
for (MCostElement costElement : costElements) {
if (processNewProduct) {
applyCriteria(accountSchema.getC_AcctSchema_ID(), costType.getM_CostType_ID(), costElement.getM_CostElement_ID(), productId, getAccountDate(), getAccountDateTo());
deleteCostDetail(dbTransaction.getTrxName());
resetCostDimension(costType.getCostingMethod(), dbTransaction.getTrxName());
generateCostCollectorNotTransaction(accountSchema, costType, productId, dbTransaction.getTrxName());
processNewProduct = false;
}
generateCostDetail(accountSchema, costType, costElement, transaction);
}
}
}
process++;
//System.out.println("Transaction : " + transactionId + " Transaction Type :"+ transaction.getMovementType() + " record ..." + process);
}
if (dbTransaction != null) {
dbTransaction.commit(true);
dbTransaction.close();
dbTransaction = null;
}
} catch (Exception e) {
if (dbTransaction != null) {
dbTransaction.rollback();
dbTransaction.close();
dbTransaction = null;
e.printStackTrace();
addLog(e.getMessage());
}
} finally {
if (dbTransaction != null) {
dbTransaction.commit();
dbTransaction.close();
dbTransaction = null;
}
}
}
use of org.compiere.model.MTransaction in project adempiere by adempiere.
the class InventoryTestException method assertMTransaction.
private void assertMTransaction(MMDocument doc, String trxName) {
final MLocator locator = InventoryUtil.getCreateLocator(-1, doc.LocatorValue, doc.LocatorValue);
final MProduct product = InventoryUtil.getCreateProduct(doc);
final int M_ASI_ID;
if (Util.isEmpty(doc.ASI, true)) {
M_ASI_ID = -1;
} else {
M_ASI_ID = doc.scenario.getM_ASI_ID(doc.ASI);
}
//
ArrayList<Object> params = new ArrayList<Object>();
String whereClause = MTransaction.COLUMNNAME_M_Product_ID + "=?" + " AND " + MTransaction.COLUMNNAME_M_Locator_ID + "=?";
params.add(product.get_ID());
params.add(locator.get_ID());
if (M_ASI_ID > 0) {
whereClause += " AND " + MTransaction.COLUMNNAME_M_AttributeSetInstance_ID + "=?";
params.add(M_ASI_ID);
}
//
MTransaction trx = new Query(getCtx(), MTransaction.Table_Name, whereClause, trxName).setParameters(params).setOrderBy(// fetch lasts first
MTransaction.COLUMNNAME_M_Transaction_ID + " DESC").first();
//
assertNotNull("No MTransaction found", trx);
assertEquals("MTransaction.MovementQty not match", doc.Qty, trx.getMovementQty());
if (doc.Date != null) {
assertEquals("MTransaction.MovementDate not match", doc.Date, trx.getMovementDate());
}
//
// TODO: check MCostDetail...
}
use of org.compiere.model.MTransaction in project adempiere by adempiere.
the class CostEngine method createCostDetail.
/**
* Create Cost Detail
* @param accountSchema Account Schema
* @param transaction Material Transaction
* @param model IDocumentLine
* @param costType Cost Type
* @param costElement Cost Element
*/
public void createCostDetail(MAcctSchema accountSchema, MCostType costType, MCostElement costElement, MTransaction transaction, IDocumentLine model, boolean force) {
if (!force)
return;
BigDecimal costThisLevel = Env.ZERO;
BigDecimal costLowLevel = Env.ZERO;
String costingLevel = MProduct.get(transaction.getCtx(), transaction.getM_Product_ID()).getCostingLevel(accountSchema, transaction.getAD_Org_ID());
// adjustment for Average PO Costing method
if (model instanceof MMatchInv && MCostType.COSTINGMETHOD_AveragePO.equals(costType.getCostingMethod()))
return;
// adjustment for Average PO Costing method
if (model instanceof MMatchPO && MCostType.COSTINGMETHOD_AverageInvoice.equals(costType.getCostingMethod()))
return;
if (model instanceof MLandedCostAllocation) {
MLandedCostAllocation allocation = (MLandedCostAllocation) model;
costThisLevel = convertCostToSchemaCurrency(accountSchema, model, model.getPriceActualCurrency());
}
MCost cost = MCost.validateCostForCostType(accountSchema, costType, costElement, transaction.getM_Product_ID(), transaction.getAD_Org_ID(), transaction.getM_Warehouse_ID(), transaction.getM_AttributeSetInstance_ID(), transaction.get_TrxName());
// get the cost for positive transaction
if ((MCostElement.COSTELEMENTTYPE_Material.equals(costElement.getCostElementType()) || MCostElement.COSTELEMENTTYPE_LandedCost.equals(costElement.getCostElementType())) && transaction.getMovementType().contains("+") && !MCostType.COSTINGMETHOD_StandardCosting.equals(costType.getCostingMethod())) {
if (model instanceof MMovementLine || model instanceof MInventoryLine || (model instanceof MInOutLine && MTransaction.MOVEMENTTYPE_CustomerReturns.equals(transaction.getMovementType()))) {
costThisLevel = getCostThisLevel(accountSchema, costType, costElement, transaction, model, costingLevel);
if (model instanceof MInventoryLine) {
MInventoryLine inventoryLine = (MInventoryLine) model;
// try get cost from physical inventory
if (costThisLevel.signum() == 0 && MCostElement.COSTELEMENTTYPE_Material.equals(costElement.getCostElementType())) {
// Use the current cost only for Physical Inventory
if (inventoryLine.getQtyInternalUse().signum() == 0 && inventoryLine.getCurrentCostPrice() != null && inventoryLine.getCurrentCostPrice().signum() > 0) {
costThisLevel = convertCostToSchemaCurrency(accountSchema, model, model.getPriceActualCurrency());
}
if (costThisLevel.signum() == 0)
costThisLevel = getCostThisLevel(accountSchema, costType, costElement, transaction, model, costingLevel);
}
costLowLevel = getCostLowLevel(accountSchema, costType, costElement, transaction, model, costingLevel);
// try get cost low level from physical inventory
if (costLowLevel.signum() == 0 && MCostElement.COSTELEMENTTYPE_Material.equals(costElement.getCostElementType())) {
// Use the current cost only for Physical Inventory
if (inventoryLine.getQtyInternalUse().signum() == 0 && inventoryLine.getCurrentCostPriceLL() != null && inventoryLine.getCurrentCostPriceLL().signum() > 0) {
costLowLevel = convertCostToSchemaCurrency(accountSchema, model, inventoryLine.getCurrentCostPriceLL());
}
if (costLowLevel.signum() == 0)
costLowLevel = getCostLowLevel(accountSchema, costType, costElement, transaction, model, costingLevel);
}
}
//Get cost from movement from if it > that zero replace cost This Level
if (model instanceof MMovementLine) {
MTransaction transactionFrom = MTransaction.getByDocumentLine(model, MTransaction.MOVEMENTTYPE_MovementFrom);
BigDecimal costMovementFrom = getCostThisLevel(accountSchema, costType, costElement, transactionFrom == null ? transaction : transactionFrom, model, costingLevel);
if (costMovementFrom.signum() > 0)
costThisLevel = costMovementFrom;
BigDecimal costMovementFromLL = getCostLowLevel(accountSchema, costType, costElement, transactionFrom == null ? transaction : transactionFrom, model, costingLevel);
if (costMovementFromLL.signum() > 0)
costLowLevel = costMovementFromLL;
}
} else if (MCostElement.COSTELEMENTTYPE_Material.equals(costElement.getCostElementType())) {
costThisLevel = convertCostToSchemaCurrency(accountSchema, model, model.getPriceActualCurrency());
}
}
if (!MCostType.COSTINGMETHOD_StandardCosting.equals(costType.getCostingMethod())) {
if (model instanceof MPPCostCollector) {
MPPCostCollector costCollector = (MPPCostCollector) model;
if (MPPCostCollector.COSTCOLLECTORTYPE_MaterialReceipt.equals(costCollector.getCostCollectorType())) {
// get Actual Cost for Cost Type and Cost Element
costThisLevel = getCostThisLevel(accountSchema, costType, costElement, transaction, model, costingLevel);
costLowLevel = CostEngine.getParentActualCostByCostType(accountSchema, costType.getM_CostType_ID(), costElement.getM_CostElement_ID(), costCollector);
}
}
if (model instanceof MProductionLine) {
MProductionLine productionLine = (MProductionLine) model;
if (productionLine.isParent())
costThisLevel = CostEngine.getParentActualCostByCostType(accountSchema, costType, costElement, productionLine.getM_Production());
if (costThisLevel.signum() == 0)
costThisLevel = cost.getCurrentCostPrice();
if (costThisLevel.signum() == 0 && MCostElement.COSTELEMENTTYPE_Material.equals(costElement.getCostElementType()))
costThisLevel = getSeedCost(transaction.getCtx(), transaction.getM_Product_ID(), transaction.get_TrxName());
// Material Receipt for Production light
if (productionLine.isParent()) {
// if the product is purchase then no use low level
if (!productionLine.getM_Product().isPurchased()) {
costLowLevel = costThisLevel;
costThisLevel = Env.ZERO;
}
} else if (productionLine.getMovementQty().signum() < 0)
costLowLevel = Env.ZERO;
}
} else if (MCostType.COSTINGMETHOD_StandardCosting.equals(costType.getCostingMethod())) {
costThisLevel = cost.getCurrentCostPrice();
costLowLevel = cost.getCurrentCostPriceLL();
//Define Cost Inventory Line
if (model instanceof MInventoryLine) {
MInventoryLine inventoryLine = (MInventoryLine) model;
//Define Current Cost Level
if (costThisLevel.signum() == 0 && MCostElement.COSTELEMENTTYPE_Material.equals(costElement.getCostElementType())) {
// Use the current cost only for Physical Inventory
if (inventoryLine.getQtyInternalUse().signum() == 0 && inventoryLine.getCurrentCostPrice() != null && inventoryLine.getCurrentCostPrice().signum() > 0) {
costThisLevel = convertCostToSchemaCurrency(accountSchema, model, model.getPriceActualCurrency());
cost.setCurrentCostPrice(costThisLevel);
cost.saveEx();
}
if (costThisLevel.signum() == 0)
costThisLevel = getCostThisLevel(accountSchema, costType, costElement, transaction, model, costingLevel);
}
//Define Current Cost Low Level
if (costLowLevel.signum() == 0 && MCostElement.COSTELEMENTTYPE_Material.equals(costElement.getCostElementType())) {
// Use the cost only for Physical Inventory
if (inventoryLine.getQtyInternalUse().signum() == 0 && inventoryLine.getCurrentCostPriceLL() != null && inventoryLine.getCurrentCostPriceLL().signum() > 0) {
costLowLevel = convertCostToSchemaCurrency(accountSchema, model, inventoryLine.getCurrentCostPriceLL());
cost.setCurrentCostPriceLL(costLowLevel);
cost.saveEx();
}
if (costLowLevel.signum() == 0)
costLowLevel = getCostLowLevel(accountSchema, costType, costElement, transaction, model, costingLevel);
}
}
if (model instanceof MMovementLine) {
MTransaction transactionFrom = MTransaction.getByDocumentLine(model, MTransaction.MOVEMENTTYPE_MovementFrom);
BigDecimal costMovementFrom = getCostThisLevel(accountSchema, costType, costElement, transactionFrom == null ? transaction : transactionFrom, model, costingLevel);
if (costThisLevel.signum() == 0 && MCostElement.COSTELEMENTTYPE_Material.equals(costElement.getCostElementType())) {
if (costMovementFrom.signum() > 0)
costThisLevel = costMovementFrom;
}
if (costLowLevel.signum() == 0 && MCostElement.COSTELEMENTTYPE_Material.equals(costElement.getCostElementType())) {
BigDecimal costMovementFromLL = getCostLowLevel(accountSchema, costType, costElement, transactionFrom == null ? transaction : transactionFrom, model, costingLevel);
if (costMovementFromLL.signum() > 0)
costLowLevel = costMovementFromLL;
}
}
if (costThisLevel.signum() == 0 && MCostElement.COSTELEMENTTYPE_Material.equals(costElement.getCostElementType())) {
costThisLevel = getSeedCost(transaction.getCtx(), transaction.getM_Product_ID(), transaction.get_TrxName());
if (costThisLevel.signum() == 0)
if (model instanceof MInOutLine && !model.isSOTrx()) {
costThisLevel = convertCostToSchemaCurrency(accountSchema, model, model.getPriceActualCurrency());
}
if (costThisLevel.signum() != 0) {
cost.setCurrentCostPrice(costThisLevel);
cost.saveEx();
}
}
if (costLowLevel.signum() != 0) {
cost.setCurrentCostPriceLL(costLowLevel);
cost.saveEx();
}
}
final ICostingMethod method = CostingMethodFactory.get().getCostingMethod(costType.getCostingMethod());
method.setCostingMethod(accountSchema, transaction, model, cost, costThisLevel, costLowLevel, model.isSOTrx());
method.process();
}
use of org.compiere.model.MTransaction in project adempiere by adempiere.
the class FifoLifoCostingMethod method get.
public static MTransaction get(MCostDetail cd) {
final String whereClause = I_M_Transaction.COLUMNNAME_M_Product_ID + "=? AND " + I_M_Transaction.COLUMNNAME_M_Transaction_ID + "=? AND " + I_M_Transaction.COLUMNNAME_MovementQty + "=?";
MTransaction trx = new Query(cd.getCtx(), MTransaction.Table_Name, whereClause, cd.get_TrxName()).setClient_ID().setParameters(cd.getM_Product_ID(), cd.getM_Transaction_ID(), cd.getQty()).firstOnly();
return trx;
}
Aggregations