use of org.compiere.util.TrxRunnable in project adempiere by adempiere.
the class VCreateFromDialog method actionPerformed.
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals(ConfirmPanel.A_OK)) {
try {
Trx.run(new TrxRunnable() {
public void run(String trxName) {
if (save(trxName)) {
dispose();
}
}
});
} catch (Exception ex) {
ADialog.error(windowNo, this, "Error", ex.getLocalizedMessage());
}
} else // Cancel
if (e.getActionCommand().equals(ConfirmPanel.A_CANCEL)) {
dispose();
} else // Trifon
if (e.getActionCommand().equals(SELECT_ALL)) {
dataTable.selectAll();
dataTable.matchCheckWithSelectedRows();
info();
}
}
use of org.compiere.util.TrxRunnable in project adempiere by adempiere.
the class RollupWorkflow method doIt.
// prepare
protected String doIt() throws Exception {
//Get account schema
MAcctSchema accountSchema = MAcctSchema.get(getCtx(), getAccountingSchemaId());
//Get cost type
MCostType costType = MCostType.get(getCtx(), getCostTypeId());
//Get cost element to process
final List<MCostElement> costElements = getCostElementId() > 0 ? Arrays.asList(MCostElement.get(getCtx(), getCostElementId())) : MCostElement.getCostElement(getCtx(), get_TrxName());
routingService = RoutingServiceFactory.get().getRoutingService(getAD_Client_ID());
//Iterate product ids based on parameters
Arrays.stream(getProductIds()).filter(productId -> productId > 0).forEach(productId -> {
MProduct product = MProduct.get(getCtx(), productId);
log.info("Product: " + product);
int workflowId = 0;
MPPProductPlanning productPlanning = null;
if (workflowId <= 0)
workflowId = MWorkflow.getWorkflowSearchKey(product);
if (workflowId <= 0) {
productPlanning = MPPProductPlanning.find(getCtx(), getOrganizationId(), getWarehouseId(), getResourcePlantId(), product.get_ID(), get_TrxName());
if (productPlanning != null)
workflowId = productPlanning.getAD_Workflow_ID();
else
createNotice(product, "@NotFound@ @PP_Product_Planning_ID@");
}
if (workflowId <= 0)
createNotice(product, "@NotFound@ @AD_Workflow_ID@");
else {
Trx.run(new TrxRunnable() {
MAcctSchema accountSchema;
MCostType costType;
MProduct product;
MPPProductPlanning productPlanning;
int workflowId;
public TrxRunnable setParameters(MAcctSchema accountSchema, MCostType costType, MProduct product, MPPProductPlanning productPlanning, int workflowId) {
this.accountSchema = accountSchema;
this.costType = costType;
this.product = product;
this.productPlanning = productPlanning;
this.workflowId = workflowId;
return this;
}
public void run(String trxName) {
MWorkflow workflow = new MWorkflow(getCtx(), workflowId, trxName);
costElements.stream().filter(costElement -> costElement != null && CostEngine.isActivityControlElement(costElement)).forEach(costElement -> {
rollup(accountSchema, costType, costElement, product, workflow, trxName);
});
if (productPlanning != null) {
productPlanning.load(trxName);
productPlanning.setYield(workflow.getYield());
productPlanning.saveEx();
}
}
}.setParameters(accountSchema, costType, product, productPlanning, workflowId));
}
});
return "@OK@";
}
use of org.compiere.util.TrxRunnable 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.util.TrxRunnable in project adempiere by adempiere.
the class MRP method runMRP.
/**************************************************************************
* Calculate plan
* @param AD_Client_ID Client ID
* @param AD_Org_ID Organization ID
* @param M_Warehuse_ID Warehouse ID
* @throws SQLException
*/
protected String runMRP(int AD_Client_ID, int AD_Org_ID, int S_Resource_ID, int M_Warehouse_ID) throws SQLException {
Trx.run(new TrxRunnable() {
private int AD_Client_ID, AD_Org_ID, S_Resource_ID, M_Warehouse_ID;
public TrxRunnable setParameters(int AD_Client_ID, int AD_Org_ID, int S_Resource_ID, int M_Warehouse_ID) {
this.AD_Client_ID = AD_Client_ID;
this.AD_Org_ID = AD_Org_ID;
this.S_Resource_ID = S_Resource_ID;
this.M_Warehouse_ID = M_Warehouse_ID;
return this;
}
public void run(String trxName) {
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
MProduct product = null;
int BeforePP_MRP_ID = 0;
Timestamp BeforeDateStartSchedule = null;
Timestamp POQDateStartSchedule = null;
int lowlevel = MPPMRP.getMaxLowLevel(getCtx(), trxName);
log.info("Low Level Is :" + lowlevel);
// Calculate MRP for all levels
for (int level = 0; level <= lowlevel; level++) {
log.info("Current Level Is :" + level);
StringBuilder sql = new StringBuilder();
sql.append("SELECT mrp.M_Product_ID, mrp.LowLevel, mrp.Qty, mrp.DatePromised").append(",mrp.TypeMRP, mrp.OrderType, mrp.DateOrdered, mrp.M_Warehouse_ID").append(",mrp.PP_MRP_ID, mrp.DateStartSchedule, mrp.DateFinishSchedule").append(" FROM RV_PP_MRP mrp WHERE 1=1 ").append(getSQLWhere("mrp", AD_Client_ID, AD_Org_ID, M_Warehouse_ID, null, null, level, MPPMRP.TYPEMRP_Demand, Planning_Horizon)).append(" ORDER BY mrp.M_Product_ID , mrp.DatePromised");
pstmt = DB.prepareStatement(sql.toString(), trxName);
DB.setParameters(pstmt, parameters);
rs = pstmt.executeQuery();
log.info("Records " + rs.getFetchSize() + " to process for Low Code:" + level);
while (rs.next()) {
final int PP_MRP_ID = rs.getInt(MPPMRP.COLUMNNAME_PP_MRP_ID);
final String TypeMRP = rs.getString(MPPMRP.COLUMNNAME_TypeMRP);
final String OrderType = rs.getString(MPPMRP.COLUMNNAME_OrderType);
final Timestamp DatePromised = rs.getTimestamp(MPPMRP.COLUMNNAME_DateStartSchedule);
final BigDecimal Qty = rs.getBigDecimal(MPPMRP.COLUMNNAME_Qty);
final int M_Product_ID = rs.getInt(MPPMRP.COLUMNNAME_M_Product_ID);
// if demand is forecast and promised date less than or equal to today, ignore this QtyGrossReq
if (MPPMRP.TYPEMRP_Demand.equals(TypeMRP) && MPPMRP.ORDERTYPE_Forecast.equals(OrderType) && DatePromised.compareTo(getToday()) <= 0) {
continue;
}
// New Product
if (product == null || product.get_ID() != M_Product_ID) {
// If exist QtyGrossReqs of last Demand verify/calculate plan
if (QtyGrossReqs.signum() != 0) {
if (product == null) {
throw new IllegalStateException("MRP Internal Error: QtyGrossReqs=" + QtyGrossReqs + " and we do not have previous demand defined");
}
if (X_PP_Product_Planning.ORDER_POLICY_PeriodOrderQuantity.equals(m_product_planning.getOrder_Policy()) && POQDateStartSchedule.compareTo(Planning_Horizon) < 0) {
BeforeDateStartSchedule = POQDateStartSchedule;
calculatePlan(AD_Client_ID, AD_Org_ID, M_Warehouse_ID, BeforePP_MRP_ID, product, BeforeDateStartSchedule, trxName);
} else if (X_PP_Product_Planning.ORDER_POLICY_Lot_For_Lot.equals(m_product_planning.getOrder_Policy()) && BeforeDateStartSchedule.compareTo(Planning_Horizon) <= 0) {
// TODO: Q: when we have this situation because on LFL we balance the Demand imediately
// so we do not cumullate it?
calculatePlan(AD_Client_ID, AD_Org_ID, M_Warehouse_ID, BeforePP_MRP_ID, product, BeforeDateStartSchedule, trxName);
}
// Discard QtyGrossReqs because:
// * was already balanced by calculatePlan
// * is out of Planning Horizon
QtyGrossReqs = Env.ZERO;
}
//Setting MRP Change net Update out the model validator and out transaction
if (m_product_planning != null)
MPPMRP.setIsRequired(m_product_planning, MPPProductPlanning.COLUMNNAME_IsRequiredMRP, false, trxName);
// Load Product & define Product Data Planning
product = MProduct.get(getCtx(), M_Product_ID);
log.info("Calculte Plan to this Product:" + product);
setProduct(AD_Client_ID, AD_Org_ID, S_Resource_ID, M_Warehouse_ID, product, PP_MRP_ID, trxName);
// If No Product Planning found, go to next MRP record
if (m_product_planning == null)
continue;
if (X_PP_Product_Planning.ORDER_POLICY_PeriodOrderQuantity.equals(m_product_planning.getOrder_Policy())) {
POQDateStartSchedule = null;
}
}
// new product
demands.put(PP_MRP_ID, Qty);
// If No Product Planning found, go to next MRP record
if (m_product_planning == null)
continue;
int daysPOQ = m_product_planning.getOrder_Period().intValueExact() - 1;
//first DatePromised.compareTo for ORDER_POLICY_PeriodOrderQuantity
if (X_PP_Product_Planning.ORDER_POLICY_PeriodOrderQuantity.equals(m_product_planning.getOrder_Policy()) && (DatePromisedTo != null && DatePromised.compareTo(DatePromisedTo) > 0)) {
calculatePlan(AD_Client_ID, AD_Org_ID, M_Warehouse_ID, PP_MRP_ID, product, DatePromisedFrom, trxName);
DatePromisedFrom = DatePromised;
DatePromisedTo = TimeUtil.addDays(DatePromised, daysPOQ < 0 ? 0 : daysPOQ);
POQDateStartSchedule = DatePromised;
} else if (POQDateStartSchedule == null) {
DatePromisedFrom = DatePromised;
DatePromisedTo = TimeUtil.addDays(DatePromised, daysPOQ < 0 ? 0 : daysPOQ);
POQDateStartSchedule = DatePromised;
}
//Indicates that a demand order is past due.
if (DatePromised.compareTo(getToday()) < 0) {
String comment = Msg.translate(getCtx(), MPPOrder.COLUMNNAME_DatePromised) + " : " + DatePromised;
createMRPNote("MRP-150", AD_Org_ID, PP_MRP_ID, product, MPPMRP.getDocumentNo(PP_MRP_ID), Qty, comment, trxName);
}
BeforePP_MRP_ID = PP_MRP_ID;
if (X_PP_Product_Planning.ORDER_POLICY_PeriodOrderQuantity.equals(m_product_planning.getOrder_Policy())) {
// Verify if is DatePromised < DatePromisedTo then Accumulation QtyGrossReqs
if (DatePromisedTo != null && DatePromised.compareTo(DatePromisedTo) <= 0) {
QtyGrossReqs = QtyGrossReqs.add(Qty);
log.info("Accumulation QtyGrossReqs:" + QtyGrossReqs);
log.info("DatePromised:" + DatePromised);
log.info("DatePromisedTo:" + DatePromisedTo);
Trx.get(trxName, true).commit(true);
continue;
}
} else // If Order_Policy = LoteForLote then always create new range for next period and put QtyGrossReqs
if (X_PP_Product_Planning.ORDER_POLICY_Lot_For_Lot.equals(m_product_planning.getOrder_Policy())) {
QtyGrossReqs = QtyGrossReqs.add(Qty);
BeforeDateStartSchedule = DatePromised;
calculatePlan(AD_Client_ID, AD_Org_ID, M_Warehouse_ID, PP_MRP_ID, product, BeforeDateStartSchedule, trxName);
Trx.get(trxName, true).commit(true);
continue;
}
}
// If exist QtyGrossReq of last Demand after finish while verify plan
if (QtyGrossReqs.signum() != 0 && product != null) {
if (X_PP_Product_Planning.ORDER_POLICY_PeriodOrderQuantity.equals(m_product_planning.getOrder_Policy()) && POQDateStartSchedule.compareTo(Planning_Horizon) < 0) {
BeforeDateStartSchedule = POQDateStartSchedule;
calculatePlan(AD_Client_ID, AD_Org_ID, M_Warehouse_ID, BeforePP_MRP_ID, product, BeforeDateStartSchedule, trxName);
} else if (X_PP_Product_Planning.ORDER_POLICY_Lot_For_Lot.equals(m_product_planning.getOrder_Policy()) && BeforeDateStartSchedule.compareTo(Planning_Horizon) <= 0) {
calculatePlan(AD_Client_ID, AD_Org_ID, M_Warehouse_ID, BeforePP_MRP_ID, product, BeforeDateStartSchedule, trxName);
}
} else if (product != null) {
//Create Action Notice if exist supply
getNetRequirements(AD_Client_ID, AD_Org_ID, M_Warehouse_ID, product, null, trxName);
}
createMRPPegging(trxName);
Trx.get(trxName, true).commit(true);
DB.close(rs, pstmt);
}
// end for
}// try
catch (SQLException ex) {
throw new DBException(ex);
} finally {
DB.close(rs, pstmt);
rs = null;
pstmt = null;
}
}
}.setParameters(AD_Client_ID, AD_Org_ID, S_Resource_ID, M_Warehouse_ID));
return "ok";
}
use of org.compiere.util.TrxRunnable in project adempiere by adempiere.
the class WPayment method saveChanges.
/**************************************************************************
* Save Changes
* @return true, if Window can exit
*/
private boolean saveChanges() {
// BF [ 1920179 ] perform the save in a trx's context.
final boolean[] success = new boolean[] { false };
final TrxRunnable r = new TrxRunnable() {
public void run(String trxName) {
success[0] = saveChangesInTrx(trxName);
}
};
try {
Trx.run(r);
} catch (Throwable e) {
success[0] = false;
FDialog.error(m_WindowNo, this, "PaymentError", e.getLocalizedMessage());
}
if (m_cashLine != null)
m_cashLine.set_TrxName(null);
if (m_mPayment != null)
m_mPayment.set_TrxName(null);
if (m_mPaymentOriginal != null)
m_mPayment.set_TrxName(null);
return success[0];
}
Aggregations