use of org.compiere.model.MAccount 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.MAccount in project adempiere by adempiere.
the class ExpenseTypesFromAccounts method doIt.
@Override
protected String doIt() throws Exception {
// Fetch price list
MPriceList priceList = new MPriceList(getCtx(), m_priceListId, get_TrxName());
// Get current client id from price list since I for some reason can't read it from
// context.
m_clientId = priceList.getAD_Client_ID();
// Get active price list version
MPriceListVersion pv = priceList.getPriceListVersion(null);
if (pv == null)
throw new Exception("Pricelist " + priceList.getName() + " has no default version.");
MProduct product;
// Read all existing applicable products into memory for quick comparison.
List<MProduct> products = new Query(getCtx(), I_M_Product.Table_Name, "ProductType=?", get_TrxName()).setParameters(MProduct.PRODUCTTYPE_ExpenseType).list();
Map<String, MProduct> productMap = new TreeMap<String, MProduct>();
for (Iterator<MProduct> it = products.iterator(); it.hasNext(); ) {
product = it.next();
productMap.put(product.getValue(), product);
}
// Read all existing valid combinations comparison
MAccount validComb;
List<MAccount> validCombs = new Query(getCtx(), I_C_ValidCombination.Table_Name, "C_AcctSchema_ID=? and AD_Client_ID=? and AD_Org_ID=0", get_TrxName()).setParameters(m_acctSchemaId, m_clientId).list();
Map<Integer, MAccount> validCombMap = new TreeMap<Integer, MAccount>();
for (Iterator<MAccount> it = validCombs.iterator(); it.hasNext(); ) {
validComb = it.next();
validCombMap.put(validComb.getAccount_ID(), validComb);
}
// Read all accounttypes that fit the given criteria.
List<MElementValue> result = new Query(getCtx(), I_C_ElementValue.Table_Name, "AccountType=? and isSummary='N' and Value>=? and Value<=? and AD_Client_ID=?", get_TrxName()).setParameters(MElementValue.ACCOUNTTYPE_Expense, m_startElement, m_endElement, m_clientId).list();
MElementValue elem;
MProductPrice priceRec;
X_M_Product_Acct productAcct;
String expenseItemValue;
BigDecimal zero = Env.ZERO;
int addCount = 0;
int skipCount = 0;
for (Iterator<MElementValue> it = result.iterator(); it.hasNext(); ) {
elem = it.next();
expenseItemValue = m_productValuePrefix + elem.getValue() + m_productValueSuffix;
// See if a product with this key already exists
product = productMap.get(expenseItemValue);
if (product == null) {
// Create a new product from the account element
product = new MProduct(getCtx(), 0, get_TrxName());
product.set_ValueOfColumn("AD_Client_ID", Integer.valueOf(m_clientId));
product.setValue(expenseItemValue);
product.setName(elem.getName());
product.setDescription(elem.getDescription());
product.setIsActive(true);
product.setProductType(MProduct.PRODUCTTYPE_ExpenseType);
product.setM_Product_Category_ID(m_productCategoryId);
product.setC_UOM_ID(m_uomId);
product.setC_TaxCategory_ID(m_taxCategoryId);
product.setIsStocked(false);
product.setIsPurchased(true);
product.setIsSold(false);
// Save the product
product.saveEx(get_TrxName());
// Add a zero product price to the price list so it shows up in the price list
priceRec = new MProductPrice(getCtx(), pv.get_ID(), product.get_ID(), get_TrxName());
priceRec.set_ValueOfColumn("AD_Client_ID", Integer.valueOf(m_clientId));
priceRec.setPrices(zero, zero, zero);
priceRec.saveEx(get_TrxName());
// Set the revenue and expense accounting of the product to the given account element
// Get the valid combination
validComb = validCombMap.get(elem.getC_ElementValue_ID());
if (validComb == null) {
// Create new valid combination
validComb = new MAccount(getCtx(), 0, get_TrxName());
validComb.set_ValueOfColumn("AD_Client_ID", Integer.valueOf(m_clientId));
validComb.setAD_Org_ID(0);
validComb.setAlias(elem.getValue());
validComb.setAccount_ID(elem.get_ID());
validComb.setC_AcctSchema_ID(m_acctSchemaId);
validComb.saveEx(get_TrxName());
}
// TODO: It might be needed to make the accounting more specific, but the purpose
// of the process now is to create general accounts so this is intentional.
productAcct = new Query(getCtx(), I_M_Product_Acct.Table_Name, "M_Product_ID=? and C_AcctSchema_ID=?", get_TrxName()).setParameters(product.get_ID(), m_acctSchemaId).first();
productAcct.setP_Expense_Acct(validComb.get_ID());
productAcct.setP_Revenue_Acct(validComb.get_ID());
productAcct.saveEx(get_TrxName());
addCount++;
} else {
skipCount++;
}
}
String returnStr = addCount + " products added.";
if (skipCount > 0)
returnStr += " " + skipCount + " products skipped.";
return (returnStr);
}
use of org.compiere.model.MAccount in project adempiere by adempiere.
the class Charge method createCharge.
// createElementValue
/**
* Create Charge and account entries for primary Account Schema.
*
* @param name charge name
* @param elementValueId element value identifier
* @return charge identifier, or 0 if no charge created.
*/
protected int createCharge(String name, int elementValueId) {
MCharge charge;
MAccount account;
log.config(name + " - ");
// Charge
charge = new MCharge(Env.getCtx(), 0, null);
charge.setName(name);
charge.setC_TaxCategory_ID(m_C_TaxCategory_ID);
if (!charge.save()) {
log.log(Level.SEVERE, name + " not created");
return 0;
}
refreshAccountSchema();
if (!isAccountSchemaValid()) {
return 0;
}
// Target Account
account = getAccount(elementValueId, charge);
if (account == null) {
return 0;
}
updateAccount(charge, account);
return charge.getC_Charge_ID();
}
use of org.compiere.model.MAccount in project adempiere by adempiere.
the class Doc_AssetReval method createFacts.
public ArrayList<Fact> createFacts(MAcctSchema as) {
MAssetAcct assetAcct = getAssetAcct();
MAssetReval assetRe = getAssetReval();
ArrayList<Fact> facts = new ArrayList<Fact>();
Fact fact = new Fact(this, as, assetAcct.getPostingType());
facts.add(fact);
MAccount dr = MAccount.get(getCtx(), assetAcct.getA_Asset_Acct());
MAccount cr = MAccount.get(getCtx(), assetAcct.getA_Reval_Cost_Offset_Acct());
FactUtil.createSimpleOperation(fact, null, dr, cr, as.getC_Currency_ID(), assetRe.getA_Asset_Cost_Change().subtract(assetRe.getA_Asset_Cost()), false);
MAccount drd = MAccount.get(getCtx(), assetAcct.getA_Reval_Cost_Offset_Acct());
MAccount crd = MAccount.get(getCtx(), assetAcct.getA_Accumdepreciation_Acct());
FactUtil.createSimpleOperation(fact, null, drd, crd, as.getC_Currency_ID(), assetRe.getA_Change_Acumulated_Depr().subtract(assetRe.getA_Accumulated_Depr()), false);
return facts;
}
use of org.compiere.model.MAccount in project adempiere by adempiere.
the class Doc_AssetAddition method getP_Asset_Acct.
private MAccount getP_Asset_Acct(MAcctSchema as) {
MAssetAddition assetAdd = getAssetAddition();
// Source Account
MAccount pAssetAcct = null;
if (MAssetAddition.A_SOURCETYPE_Project.equals(assetAdd.getA_SourceType())) {
I_C_Project prj = assetAdd.getC_Project();
return getProjectAcct(prj, as);
} else if (MAssetAddition.A_SOURCETYPE_Manual.equals(assetAdd.getA_SourceType()) && // backward compatibility: only if charge defined; if not fallback to product account
getC_Charge_ID() > 0) {
pAssetAcct = MCharge.getAccount(getC_Charge_ID(), as, null);
return pAssetAcct;
} else if (MAssetAddition.A_SOURCETYPE_Invoice.equals(assetAdd.getA_SourceType()) && assetAdd.getC_InvoiceLine().getC_Project_ID() > 0) {
I_C_Project prj = assetAdd.getC_InvoiceLine().getC_Project();
return getProjectAcct(prj, as);
} else {
pAssetAcct = getP_Expense_Acct(assetAdd.getM_Product_ID(), as);
}
//
return pAssetAcct;
}
Aggregations