use of org.compiere.model.MAcctSchema in project adempiere by adempiere.
the class CostBillOfMaterial method createLines.
/**
* createLines
*
* @param bom
* @param bomLine
*/
private void createLines(MAcctSchema accountSchema, MPPProductBOM bom, MPPProductBOMLine bomLine) {
MProduct product;
BigDecimal qty;
if (bomLine != null) {
product = MProduct.get(getCtx(), bomLine.getM_Product_ID());
qty = bomLine.getQty();
} else if (bom != null) {
product = MProduct.get(getCtx(), bom.getM_Product_ID());
qty = Env.ONE;
} else {
throw new AdempiereException("@NotFound@ @PP_Product_BOM_ID@");
}
//for (MCostElement costElement : getCostElements())
getCostElements().stream().filter(costElement -> costElement != null).forEach(costElement -> {
X_T_BOMLine reportBOMLine = new X_T_BOMLine(getCtx(), 0, get_TrxName());
reportBOMLine.setAD_Org_ID(getOrganizationId());
reportBOMLine.setM_Warehouse_ID(getWarehouseId());
reportBOMLine.setSel_Product_ID(getProductId());
reportBOMLine.setImplosion(isImplosion);
reportBOMLine.setC_AcctSchema_ID(getAccountingSchemaId());
reportBOMLine.setM_CostType_ID(getCostTypeId());
reportBOMLine.setCostingMethod(getCostingMethod());
reportBOMLine.setAD_PInstance_ID(getAD_PInstance_ID());
reportBOMLine.setM_CostElement_ID(costElement.get_ID());
reportBOMLine.setM_Product_ID(product.get_ID());
reportBOMLine.setM_Warehouse_ID(getWarehouseId());
reportBOMLine.setQtyBOM(qty);
reportBOMLine.setSeqNo(seqNo);
reportBOMLine.setLevelNo(levelNo);
reportBOMLine.setLevels(LEVELS.substring(0, levelNo) + levelNo);
BigDecimal currentCostPrice = Env.ZERO;
BigDecimal currentCostPriceLL = Env.ZERO;
BigDecimal futureCostPrice = Env.ZERO;
BigDecimal futureCostPriceLL = Env.ZERO;
final CostEngine engine = CostEngineFactory.getCostEngine(getAD_Client_ID());
List<MCost> costs = MCost.getByElement(product, accountSchema, getCostTypeId(), getOrganizationId(), getWarehouseId(), 0, costElement.getM_CostElement_ID());
boolean isCostFrozen = false;
for (MCost cost : costs) {
currentCostPrice = currentCostPrice.add(cost.getCurrentCostPrice());
currentCostPriceLL = currentCostPriceLL.add(cost.getCurrentCostPriceLL());
futureCostPrice = futureCostPrice.add(cost.getFutureCostPrice());
futureCostPriceLL = futureCostPriceLL.add(cost.getFutureCostPriceLL());
isCostFrozen = cost.isCostFrozen();
}
reportBOMLine.setCurrentCostPrice(currentCostPrice);
reportBOMLine.setCurrentCostPriceLL(currentCostPriceLL);
reportBOMLine.setFutureCostPrice(currentCostPrice);
reportBOMLine.setFutureCostPriceLL(currentCostPriceLL);
reportBOMLine.setIsCostFrozen(isCostFrozen);
if (bomLine != null) {
reportBOMLine.setPP_Product_BOM_ID(bomLine.getPP_Product_BOM_ID());
reportBOMLine.setPP_Product_BOMLine_ID(bomLine.getPP_Product_BOMLine_ID());
} else if (bom != null) {
reportBOMLine.setPP_Product_BOM_ID(bom.getPP_Product_BOM_ID());
}
reportBOMLine.saveEx();
seqNo++;
});
}
use of org.compiere.model.MAcctSchema in project adempiere by adempiere.
the class CostBillOfMaterial method doIt.
/**
* Execute process
*
* @return
* @throws Exception
*/
protected String doIt() throws Exception {
MAcctSchema accountSchema = MAcctSchema.get(getCtx(), getAccountingSchemaId());
explodeProduct(accountSchema, getProductId(), false);
return "@Ok@";
}
use of org.compiere.model.MAcctSchema in project adempiere by adempiere.
the class RollupBillOfMaterial method doIt.
// prepare
/**
* Generate Calculate Cost
* @return info
* @throws Exception
*/
protected String doIt() throws Exception {
//Get account schema
MAcctSchema acctSchema = MAcctSchema.get(getCtx(), getAccountingSchemaId());
//Get cost type
MCostType costType = MCostType.get(getCtx(), getCostTypeId());
final List<MCostElement> costElements = getCostElementId() > 0 ? Arrays.asList(MCostElement.get(getCtx(), getCostElementId())) : MCostElement.getCostElement(getCtx(), get_TrxName());
//Get max low level
int maxLowLevel = MPPMRP.getMaxLowLevel(getCtx(), get_TrxName());
// Cost Roll-up for all levels
for (int lowLevel = maxLowLevel; lowLevel >= 0; lowLevel--) {
//Iterate product based in parameters
Arrays.stream(getProductIds(lowLevel)).filter(productId -> productId > 0).forEach(productId -> {
MProduct product = MProduct.get(getCtx(), productId);
I_PP_Product_Planning productPlanning = MPPProductPlanning.find(getCtx(), getOrganizationId(), getWarehouseId(), getResourcePlantId(), productId, get_TrxName());
int bomId = 0;
if (productPlanning != null)
bomId = productPlanning.getPP_Product_BOM_ID();
else
createNotice(product, Msg.parseTranslation(getCtx(), "@NotFound@ @PP_Product_Planning_ID@"));
if (bomId <= 0)
bomId = MPPProductBOM.getBOMSearchKey(product);
MPPProductBOM bom = MPPProductBOM.get(getCtx(), bomId);
if (bom == null)
createNotice(product, Msg.parseTranslation(getCtx(), "@NotFound@ @PP_Product_BOM_ID@"));
Trx.run(new TrxRunnable() {
MAcctSchema acctSchema;
MCostType costType;
MProduct product;
MPPProductBOM bom;
public TrxRunnable setParameters(MAcctSchema acctSchema, MCostType costType, MProduct product, MPPProductBOM bom) {
this.acctSchema = acctSchema;
this.costType = costType;
this.product = product;
this.bom = bom;
return this;
}
public void run(String trxName) {
costElements.stream().filter(costElement -> costElement != null).forEach(costElement -> {
rollup(acctSchema, costType, costElement, product, bom, trxName);
});
}
}.setParameters(acctSchema, costType, product, bom));
});
// Products List
}
// for Low Lever
return "@OK@";
}
use of org.compiere.model.MAcctSchema in project adempiere by adempiere.
the class Doc method get.
// get
/**
* Create Posting document
* @param ass accounting schema
* @param AD_Table_ID Table ID of Documents
* @param rs ResultSet
* @param trxName transaction name
* @return Document
* @throws AdempiereUserError
*/
public static Doc get(MAcctSchema[] ass, int AD_Table_ID, ResultSet rs, String trxName) throws AdempiereUserError {
Doc doc = null;
/* Classname of the Doc class follows this convention:
* if the prefix (letters before the first underscore _) is 1 character, then the class is Doc_TableWithoutPrefixWithoutUnderscores
* otherwise Doc_WholeTableWithoutUnderscores
* i.e. following this query
SELECT t.ad_table_id, tablename,
CASE
WHEN instr(tablename, '_') = 2
THEN 'Doc_' || substr(tablename, 3)
WHEN instr(tablename, '_') > 2
THEN 'Doc_' ||
ELSE ''
REPLACE
(
tablename, '_', ''
)
END AS classname
FROM ad_table t, ad_column C
WHERE t.ad_table_id = C.ad_table_id AND
C.columnname = 'Posted' AND
isview = 'N'
ORDER BY 1
* This is:
* 224 GL_Journal Doc_GLJournal
* 259 C_Order Doc_Order
* 318 C_Invoice Doc_Invoice
* 319 M_InOut Doc_InOut
* 321 M_Inventory Doc_Inventory
* 323 M_Movement Doc_Movement
* 325 M_Production Doc_Production
* 335 C_Payment Doc_Payment
* 392 C_BankStatement Doc_BankStatement
* 407 C_Cash Doc_Cash
* 472 M_MatchInv Doc_MatchInv
* 473 M_MatchPO Doc_MatchPO
* 623 C_ProjectIssue Doc_ProjectIssue
* 702 M_Requisition Doc_Requisition
* 735 C_AllocationHdr Doc_AllocationHdr
* 53027 PP_Order Doc_PPOrder
* 53035 PP_Cost_Collector Doc_PPCostCollector
* 53037 DD_Order Doc_DDOrder
* 53092 HR_Process Doc_HRProcess
*/
String tableName = MTable.getTableName(Env.getCtx(), AD_Table_ID);
String packageName = "org.compiere.acct";
String className = null;
int firstUnderscore = tableName.indexOf("_");
if (firstUnderscore == 1)
className = packageName + ".Doc_" + tableName.substring(2).replaceAll("_", "");
else
className = packageName + ".Doc_" + tableName.replaceAll("_", "");
try {
Class<?> cClass = Class.forName(className);
Constructor<?> cnstr = cClass.getConstructor(new Class[] { MAcctSchema[].class, ResultSet.class, String.class });
doc = (Doc) cnstr.newInstance(ass, rs, trxName);
} catch (Exception e) {
s_log.log(Level.SEVERE, "Doc Class invalid: " + className + " (" + e.toString() + ")");
throw new AdempiereUserError("Doc Class invalid: " + className + " (" + e.toString() + ")");
}
if (doc == null)
s_log.log(Level.SEVERE, "Unknown AD_Table_ID=" + AD_Table_ID);
return doc;
}
use of org.compiere.model.MAcctSchema in project adempiere by adempiere.
the class Doc_Invoice method landedCost.
// createFactCash
/**
* Create Landed Cost accounting & Cost lines
* @param as accounting schema
* @param fact fact
* @param line document line
* @param isDebit DR entry (normal api)
* @return true if landed costs were created
*/
private boolean landedCost(MAcctSchema as, Fact fact, DocLine line, boolean isDebit) {
int invoiceLineId = line.get_ID();
MLandedCostAllocation[] landedCostAllocations = MLandedCostAllocation.getOfInvoiceLine(getCtx(), invoiceLineId, getTrxName());
if (landedCostAllocations.length == 0)
return false;
BigDecimal totalBase = Arrays.stream(landedCostAllocations).map(MLandedCostAllocation::getBase).reduce(BigDecimal.ZERO, BigDecimal::add);
// Create New
MInvoiceLine invoiceLine = new MInvoiceLine(getCtx(), invoiceLineId, getTrxName());
Arrays.stream(landedCostAllocations).filter(// only cost allocation with base > 0
landedCostAllocation -> landedCostAllocation.getBase().signum() != 0).forEach(landedCostAllocation -> {
BigDecimal percent = landedCostAllocation.getBase().divide(totalBase, BigDecimal.ROUND_HALF_UP);
String desc = invoiceLine.getDescription();
if (desc == null)
desc = percent + "%";
else
desc += " - " + percent + "%";
if (line.getDescription() != null)
desc += " - " + line.getDescription();
ProductCost productCost = new ProductCost(Env.getCtx(), landedCostAllocation.getM_Product_ID(), landedCostAllocation.getM_AttributeSetInstance_ID(), getTrxName());
BigDecimal debitAmount = BigDecimal.ZERO;
BigDecimal creditAmount = BigDecimal.ZERO;
;
FactLine factLine = null;
MCostType costType = MCostType.get(as, landedCostAllocation.getM_Product_ID(), landedCostAllocation.getAD_Org_ID());
if (MCostType.COSTINGMETHOD_AverageInvoice.equals(costType.getCostingMethod())) {
BigDecimal assetAmount = Optional.ofNullable(MCostDetail.getByDocLineLandedCost(landedCostAllocation, as.getC_AcctSchema_ID(), costType.get_ID())).orElse(BigDecimal.ZERO);
BigDecimal costAdjustment = landedCostAllocation.getAmt().subtract(assetAmount);
if (assetAmount.signum() != 0) {
if (isDebit)
debitAmount = assetAmount;
else
creditAmount = assetAmount;
factLine = fact.createLine(line, productCost.getAccount(ProductCost.ACCTTYPE_P_Asset, as), as.getC_Currency_ID(), debitAmount, creditAmount);
factLine.setDescription(desc + " " + landedCostAllocation.getM_CostElement().getName());
factLine.setM_Product_ID(landedCostAllocation.getM_Product_ID());
}
if (costAdjustment.signum() != 0) {
if (isDebit)
debitAmount = costAdjustment;
else
creditAmount = costAdjustment;
factLine = fact.createLine(line, productCost.getAccount(ProductCost.ACCTTYPE_P_CostAdjustment, as), getC_Currency_ID(), debitAmount, creditAmount);
}
} else {
factLine = fact.createLine(line, productCost.getAccount(ProductCost.ACCTTYPE_P_CostAdjustment, as), getC_Currency_ID(), debitAmount, creditAmount);
}
factLine.setDescription(desc + " " + landedCostAllocation.getM_CostElement().getName());
factLine.setM_Product_ID(landedCostAllocation.getM_Product_ID());
});
log.config("Created #" + landedCostAllocations.length);
return true;
}
Aggregations