use of org.eevolution.model.MDDOrder in project adempiere by adempiere.
the class DistributionRun method distributionOrders.
// insertDetails
/**************************************************************************
* Create Orders
* @return true if created
*/
private boolean distributionOrders() {
//The Quantity Available is distribute with respect to Distribution Order Demand
if (p_BasedInDamnd) {
int M_Warehouse_ID = 0;
if (p_M_Warehouse_ID <= 0) {
MOrgInfo oi_source = MOrgInfo.get(getCtx(), m_run.getAD_Org_ID(), get_TrxName());
MWarehouse m_source = MWarehouse.get(getCtx(), oi_source.getM_Warehouse_ID());
if (m_source == null)
throw new AdempiereException("Do not exist Defautl Warehouse Source");
M_Warehouse_ID = m_source.getM_Warehouse_ID();
} else
M_Warehouse_ID = p_M_Warehouse_ID;
// For all lines
for (int i = 0; i < m_details.length; i++) {
MDistributionRunDetail detail = m_details[i];
StringBuffer sql = new StringBuffer("SELECT * FROM DD_OrderLine ol INNER JOIN DD_Order o ON (o.DD_Order_ID=ol.DD_Order_ID) INNER JOIN M_Locator l ON (l.M_Locator_ID=ol.M_Locator_ID) ");
//sql.append(" WHERE o.DocStatus IN ('DR','IN') AND o.C_BPartner_ID = ? AND M_Product_ID=? AND l.M_Warehouse_ID=? AND ol.DatePromised BETWEEN ? AND ? ");
sql.append(" WHERE o.DocStatus IN ('DR','IN') AND o.C_BPartner_ID = ? AND M_Product_ID=? AND l.M_Warehouse_ID=? AND ol.DatePromised <=?");
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
pstmt = DB.prepareStatement(sql.toString(), get_TrxName());
pstmt.setInt(1, detail.getC_BPartner_ID());
pstmt.setInt(2, detail.getM_Product_ID());
pstmt.setInt(3, M_Warehouse_ID);
pstmt.setTimestamp(4, p_DatePromised);
//pstmt.setTimestamp(5, p_DatePromised_To);
rs = pstmt.executeQuery();
while (rs.next()) {
// Create Order Line
MDDOrderLine line = new MDDOrderLine(getCtx(), rs, get_TrxName());
line.setM_Product_ID(detail.getM_Product_ID());
line.setConfirmedQty(line.getTargetQty().add(detail.getActualAllocation()));
if (p_M_Warehouse_ID > 0)
line.setDescription(Msg.translate(getCtx(), "PlannedQty"));
else
line.setDescription(m_run.getName());
line.saveEx();
break;
//addLog(0,null, detail.getActualAllocation(), order.getDocumentNo()
// + ": " + bp.getName() + " - " + product.getName());
}
} catch (Exception e) {
log.log(Level.SEVERE, "doIt - " + sql, e);
return false;
} finally {
DB.close(rs, pstmt);
rs = null;
pstmt = null;
}
}
return true;
}
// Get Counter Org/BP
int runAD_Org_ID = m_run.getAD_Org_ID();
if (runAD_Org_ID == 0)
runAD_Org_ID = Env.getAD_Org_ID(getCtx());
MOrg runOrg = MOrg.get(getCtx(), runAD_Org_ID);
int runC_BPartner_ID = runOrg.getLinkedC_BPartner_ID(get_TrxName());
boolean counter = // no single Order
!m_run.isCreateSingleOrder() && // Org linked to BP
runC_BPartner_ID > 0 && // PO
!m_docType.isSOTrx();
MBPartner runBPartner = counter ? new MBPartner(getCtx(), runC_BPartner_ID, get_TrxName()) : null;
if (!counter || runBPartner == null || runBPartner.get_ID() != runC_BPartner_ID)
counter = false;
if (counter)
log.info("RunBP=" + runBPartner + " - " + m_docType);
log.info("Single=" + m_run.isCreateSingleOrder() + " - " + m_docType + ",SO=" + m_docType.isSOTrx());
log.fine("Counter=" + counter + ",C_BPartner_ID=" + runC_BPartner_ID + "," + runBPartner);
//
MBPartner bp = null;
MDDOrder singleOrder = null;
MProduct product = null;
MWarehouse m_source = null;
MLocator m_locator = null;
MWarehouse m_target = null;
MLocator m_locator_to = null;
MWarehouse[] ws = null;
MOrgInfo oi_source = MOrgInfo.get(getCtx(), m_run.getAD_Org_ID(), get_TrxName());
m_source = MWarehouse.get(getCtx(), oi_source.getM_Warehouse_ID());
if (m_source == null)
throw new AdempiereException("Do not exist Defautl Warehouse Source");
m_locator = MLocator.getDefault(m_source);
//get the warehouse in transit
ws = MWarehouse.getInTransitForOrg(getCtx(), m_source.getAD_Org_ID());
if (ws == null)
throw new AdempiereException("Warehouse Intransit do not found");
// Consolidated Single Order
if (m_run.isCreateSingleOrder()) {
bp = new MBPartner(getCtx(), m_run.getC_BPartner_ID(), get_TrxName());
if (bp.get_ID() == 0)
throw new IllegalArgumentException("Business Partner not found - C_BPartner_ID=" + m_run.getC_BPartner_ID());
//
if (!p_IsTest) {
singleOrder = new MDDOrder(getCtx(), 0, get_TrxName());
singleOrder.setC_DocType_ID(m_docType.getC_DocType_ID());
singleOrder.setIsSOTrx(m_docType.isSOTrx());
singleOrder.setBPartner(bp);
if (m_run.getC_BPartner_Location_ID() != 0)
singleOrder.setC_BPartner_Location_ID(m_run.getC_BPartner_Location_ID());
singleOrder.setDateOrdered(m_DateOrdered);
singleOrder.setDatePromised(p_DatePromised);
singleOrder.setM_Warehouse_ID(ws[0].getM_Warehouse_ID());
if (!singleOrder.save()) {
log.log(Level.SEVERE, "Order not saved");
return false;
}
m_counter++;
}
}
int lastC_BPartner_ID = 0;
int lastC_BPartner_Location_ID = 0;
MDDOrder order = null;
// For all lines
for (int i = 0; i < m_details.length; i++) {
MDistributionRunDetail detail = m_details[i];
// Create Order Header
if (m_run.isCreateSingleOrder())
order = singleOrder;
else // New Business Partner
if (lastC_BPartner_ID != detail.getC_BPartner_ID() || lastC_BPartner_Location_ID != detail.getC_BPartner_Location_ID()) {
// finish order
order = null;
}
lastC_BPartner_ID = detail.getC_BPartner_ID();
lastC_BPartner_Location_ID = detail.getC_BPartner_Location_ID();
bp = new MBPartner(getCtx(), detail.getC_BPartner_ID(), get_TrxName());
MOrgInfo oi_target = MOrgInfo.get(getCtx(), bp.getAD_OrgBP_ID_Int(), get_TrxName());
m_target = MWarehouse.get(getCtx(), oi_target.getM_Warehouse_ID());
if (m_target == null)
throw new AdempiereException("Do not exist Default Warehouse Target");
m_locator_to = MLocator.getDefault(m_target);
if (m_locator == null || m_locator_to == null) {
throw new AdempiereException("Do not exist default Locator for Warehouses");
}
if (p_ConsolidateDocument) {
String whereClause = "DocStatus IN ('DR','IN') AND AD_Org_ID=" + bp.getAD_OrgBP_ID_Int() + " AND " + MDDOrder.COLUMNNAME_C_BPartner_ID + "=? AND " + MDDOrder.COLUMNNAME_M_Warehouse_ID + "=? AND " + MDDOrder.COLUMNNAME_DatePromised + "<=? ";
order = new Query(getCtx(), MDDOrder.Table_Name, whereClause, get_TrxName()).setParameters(new Object[] { lastC_BPartner_ID, ws[0].getM_Warehouse_ID(), p_DatePromised }).setOrderBy(MDDOrder.COLUMNNAME_DatePromised + " DESC").first();
}
// New Order
if (order == null) {
if (!p_IsTest) {
order = new MDDOrder(getCtx(), 0, get_TrxName());
order.setAD_Org_ID(bp.getAD_OrgBP_ID_Int());
order.setC_DocType_ID(m_docType.getC_DocType_ID());
order.setIsSOTrx(m_docType.isSOTrx());
// Counter Doc
if (counter && bp.getAD_OrgBP_ID_Int() > 0) {
log.fine("Counter - From_BPOrg=" + bp.getAD_OrgBP_ID_Int() + "-" + bp + ", To_BP=" + runBPartner);
order.setAD_Org_ID(bp.getAD_OrgBP_ID_Int());
if (ws[0].getM_Warehouse_ID() > 0)
order.setM_Warehouse_ID(ws[0].getM_Warehouse_ID());
order.setBPartner(runBPartner);
} else // normal
{
log.fine("From_Org=" + runAD_Org_ID + ", To_BP=" + bp);
order.setAD_Org_ID(bp.getAD_OrgBP_ID_Int());
order.setBPartner(bp);
if (detail.getC_BPartner_Location_ID() != 0)
order.setC_BPartner_Location_ID(detail.getC_BPartner_Location_ID());
}
order.setM_Warehouse_ID(ws[0].getM_Warehouse_ID());
order.setDateOrdered(m_DateOrdered);
order.setDatePromised(p_DatePromised);
order.setIsInDispute(false);
order.setIsInTransit(false);
if (!order.save()) {
log.log(Level.SEVERE, "Order not saved");
return false;
}
}
}
// Line
if (product == null || product.getM_Product_ID() != detail.getM_Product_ID())
product = MProduct.get(getCtx(), detail.getM_Product_ID());
if (p_IsTest) {
addLog(0, null, detail.getActualAllocation(), bp.getName() + " - " + product.getName());
continue;
}
if (p_ConsolidateDocument) {
String sql = "SELECT DD_OrderLine_ID FROM DD_OrderLine ol INNER JOIN DD_Order o ON (o.DD_Order_ID=ol.DD_Order_ID) WHERE o.DocStatus IN ('DR','IN') AND o.C_BPartner_ID = ? AND M_Product_ID=? AND ol.M_Locator_ID=? AND ol.DatePromised <= ?";
int DD_OrderLine_ID = DB.getSQLValueEx(get_TrxName(), sql, new Object[] { detail.getC_BPartner_ID(), product.getM_Product_ID(), m_locator.getM_Locator_ID(), p_DatePromised });
if (DD_OrderLine_ID <= 0) {
MDDOrderLine line = new MDDOrderLine(order);
line.setAD_Org_ID(bp.getAD_OrgBP_ID_Int());
line.setM_Locator_ID(m_locator.getM_Locator_ID());
line.setM_LocatorTo_ID(m_locator_to.getM_Locator_ID());
line.setIsInvoiced(false);
line.setProduct(product);
BigDecimal QtyAllocation = detail.getActualAllocation();
if (QtyAllocation == null)
QtyAllocation = Env.ZERO;
line.setQty(QtyAllocation);
line.setQtyEntered(QtyAllocation);
//line.setTargetQty(detail.getActualAllocation());
line.setTargetQty(Env.ZERO);
String Description = "";
if (m_run.getName() != null)
Description = Description.concat(m_run.getName());
line.setDescription(Description + " " + Msg.translate(getCtx(), "Qty") + " = " + QtyAllocation + " ");
//line.setConfirmedQty(QtyAllocation);
line.saveEx();
} else {
MDDOrderLine line = new MDDOrderLine(getCtx(), DD_OrderLine_ID, get_TrxName());
BigDecimal QtyAllocation = detail.getActualAllocation();
if (QtyAllocation == null)
QtyAllocation = Env.ZERO;
String Description = line.getDescription();
if (Description == null)
Description = "";
if (m_run.getName() != null)
Description = Description.concat(m_run.getName());
line.setDescription(Description + " " + Msg.translate(getCtx(), "Qty") + " = " + QtyAllocation + " ");
line.setQty(line.getQtyEntered().add(QtyAllocation));
//line.setConfirmedQty(line.getConfirmedQty().add( QtyAllocation));
line.saveEx();
}
} else {
// Create Order Line
MDDOrderLine line = new MDDOrderLine(order);
if (counter && bp.getAD_OrgBP_ID_Int() > 0)
// don't overwrite counter doc
;
/*else // normal - optionally overwrite
{
line.setC_BPartner_ID(detail.getC_BPartner_ID());
if (detail.getC_BPartner_Location_ID() != 0)
line.setC_BPartner_Location_ID(detail.getC_BPartner_Location_ID());
}*/
//
line.setAD_Org_ID(bp.getAD_OrgBP_ID_Int());
line.setM_Locator_ID(m_locator.getM_Locator_ID());
line.setM_LocatorTo_ID(m_locator_to.getM_Locator_ID());
line.setIsInvoiced(false);
line.setProduct(product);
line.setQty(detail.getActualAllocation());
line.setQtyEntered(detail.getActualAllocation());
//line.setTargetQty(detail.getActualAllocation());
line.setTargetQty(Env.ZERO);
//line.setConfirmedQty(detail.getActualAllocation());
String Description = "";
if (m_run.getName() != null)
Description = Description.concat(m_run.getName());
line.setDescription(Description + " " + Msg.translate(getCtx(), "Qty") + " = " + detail.getActualAllocation() + " ");
line.saveEx();
}
addLog(0, null, detail.getActualAllocation(), order.getDocumentNo() + ": " + bp.getName() + " - " + product.getName());
}
// finish order
order = null;
return true;
}
use of org.eevolution.model.MDDOrder in project adempiere by adempiere.
the class ReleaseInOutBound method createDistributionOrder.
/**
* get Out Bound Order Lines from Smart Browser
* @return
*/
/*private List <MWMInOutBoundLine> getOutBoundOrderLine()
{
StringBuilder whereClause = new StringBuilder();
whereClause.append("EXISTS (SELECT 1 FROM WM_InOutBound o WHERE o.WM_InOutBound_ID=WM_InOutBoundLine.WM_InOutBound_ID AND o.Processed='N' AND o.DocAction NOT IN ('CO','CL','VO')) AND ");
whereClause.append("EXISTS (SELECT T_Selection_ID FROM T_Selection WHERE T_Selection.AD_PInstance_ID=? AND T_Selection.T_Selection_ID=WM_InOutBoundLine.WM_InOutboundLine_ID)");
return new Query(getCtx(), I_WM_InOutBoundLine.Table_Name, whereClause.toString(), get_TrxName())
.setClient_ID()
.setParameters(getAD_PInstance_ID())
.list();
}*/
/**
* create Distribution Order to performance a Pick List
* @param outBoundOrderLine Out bound Line
* @return Quantity that was not covert for inventory
*/
protected BigDecimal createDistributionOrder(MWMInOutBoundLine outBoundOrderLine) {
WMRuleEngine engineRule = WMRuleEngine.get();
List<MStorage> storageList = engineRule.getStorage(outBoundOrderLine, getWarehouseAreaTypeId(), getWarehouseSectionTypeId());
int shipperId = 0;
BigDecimal qtySupply = BigDecimal.ZERO;
if (storageList != null && storageList.size() > 0) {
//get the warehouse in transit
MWarehouse[] wsts = MWarehouse.getInTransitForOrg(getCtx(), outBoundLocator.getAD_Org_ID());
if (wsts == null || wsts.length == 0)
throw new AdempiereException("@M_Warehouse_ID@ @IsInTransit@ @NotFound@");
//Org Must be linked to BPartner
MOrg org = MOrg.get(getCtx(), outBoundLocator.getAD_Org_ID());
int partnerId = org.getLinkedC_BPartner_ID(get_TrxName());
if (partnerId == 0)
throw new NoBPartnerLinkedforOrgException(org);
MBPartner partner = MBPartner.get(getCtx(), partnerId);
if (orderDistribution == null) {
orderDistribution = new MDDOrder(getCtx(), 0, get_TrxName());
orderDistribution.setAD_Org_ID(outBoundLocator.getAD_Org_ID());
orderDistribution.setC_BPartner_ID(partnerId);
if (getDocumentTypeId() > 0) {
orderDistribution.setC_DocType_ID(getDocumentTypeId());
} else {
orderDistribution.setC_DocType_ID(MDocType.getDocType(X_C_DocType.DOCBASETYPE_DistributionOrder));
}
orderDistribution.setM_Warehouse_ID(wsts[0].get_ID());
if (getDocumentAction() != null)
orderDistribution.setDocAction(getDocumentAction());
else
orderDistribution.setDocAction(X_DD_Order.DOCACTION_Prepare);
MUser[] users = MUser.getOfBPartner(getCtx(), partner.getC_BPartner_ID(), get_TrxName());
if (users == null || users.length == 0)
throw new AdempiereException("@AD_User_ID@ @NotFound@ @Value@ - @C_BPartner_ID@ : " + partner.getValue() + " - " + partner.getName());
orderDistribution.setAD_User_ID(users[0].getAD_User_ID());
orderDistribution.setDateOrdered(getToday());
orderDistribution.setDatePromised(getToday());
orderDistribution.setM_Shipper_ID(shipperId);
orderDistribution.setIsInDispute(false);
orderDistribution.setIsInTransit(false);
orderDistribution.setSalesRep_ID(getAD_User_ID());
orderDistribution.saveEx();
}
storageList.stream().forEach(storage -> {
MDDOrderLine orderLine = new MDDOrderLine(orderDistribution);
orderLine.setM_Locator_ID(storage.getM_Locator_ID());
orderLine.setM_LocatorTo_ID(outBoundLocator.getM_Locator_ID());
orderLine.setC_UOM_ID(outBoundOrderLine.getC_UOM_ID());
orderLine.setM_Product_ID(outBoundOrderLine.getM_Product_ID());
orderLine.setDateOrdered(getToday());
orderLine.setDatePromised(outBoundOrderLine.getPickDate());
orderLine.setWM_InOutBoundLine_ID(outBoundOrderLine.getWM_InOutBoundLine_ID());
orderLine.setIsInvoiced(false);
orderLine.saveEx();
});
} else {
qtySupply = outBoundOrderLine.getQtyToPick().subtract(qtySupply);
}
return qtySupply;
}
use of org.eevolution.model.MDDOrder in project adempiere by adempiere.
the class MRP method createDDOrder.
protected void createDDOrder(int AD_Org_ID, int PP_MRP_ID, MProduct product, BigDecimal QtyPlanned, Timestamp DemandDateStartSchedule, String trxName) throws AdempiereException, SQLException {
//TODO vpj-cd I need to create logic for DRP-040 Shipment Due Action Notice
//Indicates that a shipment for a Order Distribution is due.
// Action should be taken at the source warehouse to ensure that the order is received on time.
//TODO vpj-cd I need to create logic for DRP-050 Shipment Pas Due Action Notice
//Indicates that a shipment for a Order Distribution is past due. You should either delay the orders created the requirement for the product
//or expedite them when the product does arrive.
//Setting DRP Change net Update out the model validator
MPPMRP.setIsRequired(m_product_planning, MPPProductPlanning.COLUMNNAME_IsRequiredDRP, false, trxName);
if (m_product_planning.getDD_NetworkDistribution_ID() == 0) {
//Indicates that the Product Planning Data for this product does not specify a valid network distribution.
createMRPNote("DRP-060", AD_Org_ID, PP_MRP_ID, product, (String) null, null, null, trxName);
}
//TODO: Create functionality for Valid form and Valid To for an Network Distribution
MDDNetworkDistribution network = MDDNetworkDistribution.get(getCtx(), m_product_planning.getDD_NetworkDistribution_ID());
MDDNetworkDistributionLine[] network_lines = network.getLines(m_product_planning.getM_Warehouse_ID());
int M_Shipper_ID = 0;
MDDOrder order = null;
Integer DD_Order_ID = 0;
for (MDDNetworkDistributionLine network_line : network_lines) {
if (network_line.getM_Shipper_ID() == 0) {
String comment = Msg.translate(getCtx(), MDDNetworkDistribution.COLUMNNAME_Name) + " : " + network.getName();
createMRPNote("DRP-030", AD_Org_ID, PP_MRP_ID, product, null, null, comment, trxName);
continue;
}
//get supply source warehouse and locator
MWarehouse source = new MWarehouse(getCtx(), network_line.getM_WarehouseSource_ID(), trxName);
MLocator locator = MLocator.getDefault(source);
if (locator == null || locator.getM_Locator_ID() <= 0) {
String comment = Msg.translate(getCtx(), " @M_Locator_ID@ @Default@ @NotFound@ @To@ ") + source.getName();
createMRPNote("DRP-001", AD_Org_ID, PP_MRP_ID, product, null, null, comment, trxName);
continue;
}
//get supply target warehouse and locator
MWarehouse target = new MWarehouse(getCtx(), network_line.getM_Warehouse_ID(), trxName);
MLocator locator_to = MLocator.getDefault(target);
if (locator_to == null || locator_to.getM_Locator_ID() <= 0) {
String comment = Msg.translate(getCtx(), " @M_Locator_ID@ @Default@ @NotFound@ @To@ ") + source.getName();
createMRPNote("DRP-001", AD_Org_ID, PP_MRP_ID, product, null, null, comment, trxName);
continue;
}
//get the transfer time
BigDecimal transferTime = network_line.getTransferTime();
if (transferTime.compareTo(Env.ZERO) <= 0) {
transferTime = m_product_planning.getTransferTime();
}
if (locator == null || locator_to == null) {
String comment = Msg.translate(getCtx(), MDDNetworkDistributionLine.COLUMNNAME_M_WarehouseSource_ID) + " : " + source.getName();
createMRPNote("DRP-001", AD_Org_ID, PP_MRP_ID, product, null, null, comment, trxName);
continue;
}
//get the warehouse in transit
MWarehouse[] wsts = MWarehouse.getInTransitForOrg(getCtx(), source.getAD_Org_ID());
if (wsts == null || wsts.length == 0) {
String comment = Msg.translate(getCtx(), MOrg.COLUMNNAME_Name) + " : " + MOrg.get(getCtx(), AD_Org_ID).getName();
createMRPNote("DRP-010", AD_Org_ID, PP_MRP_ID, product, null, null, comment, trxName);
continue;
}
if (M_Shipper_ID != network_line.getM_Shipper_ID()) {
//Org Must be linked to BPartner
MOrg org = MOrg.get(getCtx(), locator_to.getAD_Org_ID());
int C_BPartner_ID = org.getLinkedC_BPartner_ID(trxName);
if (C_BPartner_ID == 0) {
String comment = Msg.translate(getCtx(), MOrg.COLUMNNAME_Name) + " : " + MOrg.get(getCtx(), AD_Org_ID).getName();
createMRPNote("DRP-020", AD_Org_ID, PP_MRP_ID, product, null, null, comment, trxName);
continue;
}
MBPartner bp = getBPartner(C_BPartner_ID);
// Try found some order with Shipper , Business Partner and Doc Status = Draft
// Consolidate the demand in a single order for each Shipper , Business Partner , DemandDateStartSchedule
DD_Order_ID = getDDOrder_ID(AD_Org_ID, wsts[0].get_ID(), network_line.getM_Shipper_ID(), bp.getC_BPartner_ID(), TimeUtil.getDay(DemandDateStartSchedule.getTime()), trxName);
if (DD_Order_ID <= 0) {
order = new MDDOrder(getCtx(), 0, trxName);
order.setAD_Org_ID(target.getAD_Org_ID());
order.setC_BPartner_ID(C_BPartner_ID);
order.setAD_User_ID(bp.getPrimaryAD_User_ID());
order.setC_DocType_ID(docTypeDO_ID);
order.setM_Warehouse_ID(wsts[0].get_ID());
order.setDocAction(MDDOrder.DOCACTION_Complete);
order.setDateOrdered(TimeUtil.addDays(DemandDateStartSchedule, (m_product_planning.getDeliveryTime_Promised().add(transferTime)).negate().intValueExact()));
order.setDatePromised(DemandDateStartSchedule);
order.setM_Shipper_ID(network_line.getM_Shipper_ID());
order.setIsInDispute(false);
order.setIsInTransit(false);
order.setSalesRep_ID(m_product_planning.getPlanner_ID());
order.setProcessed(false);
order.setProcessing(false);
order.saveEx();
order.addDescription(Msg.parseTranslation(getCtx(), "@DD_Order_ID@ @DocumentNo@ " + order.getDocumentNo() + " @Generate@ @from@ " + getName()));
order.saveEx();
DD_Order_ID = order.get_ID();
String key = order.getAD_Org_ID() + "#" + order.getM_Warehouse_ID() + "#" + network_line.getM_Shipper_ID() + "#" + C_BPartner_ID + "#" + TimeUtil.getDay(DemandDateStartSchedule.getTime()) + "DR";
dd_order_id_cache.put(key, DD_Order_ID);
} else {
order = new MDDOrder(getCtx(), DD_Order_ID, trxName);
}
M_Shipper_ID = network_line.getM_Shipper_ID();
}
BigDecimal QtyOrdered = QtyPlanned.multiply(network_line.getPercent()).divide(Env.ONEHUNDRED);
MDDOrderLine oline = new MDDOrderLine(getCtx(), 0, trxName);
oline.setDD_Order_ID(order.getDD_Order_ID());
oline.setAD_Org_ID(target.getAD_Org_ID());
oline.setM_Locator_ID(locator.getM_Locator_ID());
oline.setM_LocatorTo_ID(locator_to.getM_Locator_ID());
oline.setM_Product_ID(m_product_planning.getM_Product_ID());
oline.setDateOrdered(order.getDateOrdered());
oline.setDatePromised(DemandDateStartSchedule);
oline.setQtyEntered(QtyOrdered);
oline.setQtyOrdered(QtyOrdered);
oline.setTargetQty(MPPMRP.getQtyReserved(getCtx(), target.getM_Warehouse_ID(), m_product_planning.getM_Product_ID(), DemandDateStartSchedule, trxName));
oline.setIsInvoiced(false);
oline.saveEx();
// Set Correct Dates for Plan
final String whereClause = MPPMRP.COLUMNNAME_DD_OrderLine_ID + "=?";
List<MPPMRP> mrpList = new Query(getCtx(), MPPMRP.Table_Name, whereClause, trxName).setParameters(new Object[] { oline.getDD_OrderLine_ID() }).list();
for (MPPMRP mrp : mrpList) {
mrp.setDateOrdered(getToday());
mrp.setDateOrdered(mrp.getDD_Order().getDateOrdered());
mrp.setDateStartSchedule(mrp.getDateOrdered());
mrp.setDatePromised(DemandDateStartSchedule);
mrp.setDateFinishSchedule(DemandDateStartSchedule);
mrp.saveEx();
if (MPPMRP.TYPEMRP_Supply.equals(mrp.getTypeMRP()))
supplies.put(mrp.get_ID(), mrp.getQty());
}
count_DO += 1;
}
}
use of org.eevolution.model.MDDOrder in project adempiere by adempiere.
the class MRPApproval method createDistributionOrder.
private MDDOrder createDistributionOrder(MPPMRP mrp) {
MLocator locatorFrom = MLocator.get(mrp.getCtx(), getLocatorId());
MLocator locatorTo = MLocator.get(mrp.getCtx(), getLocatorToId());
int docTypeId = MPPMRP.getDocType(getCtx(), MDocType.DOCBASETYPE_DistributionOrder, locatorTo.getAD_Org_ID(), Env.getAD_User_ID(getCtx()), get_TrxName());
MBPartner partner = MBPartner.get(mrp.getCtx(), getBusinessPartnerId());
MDDOrder order = new MDDOrder(getCtx(), 0, get_TrxName());
order.setAD_Org_ID(mrp.getAD_Org_ID());
order.addDescription("Generated by MRP");
order.setC_BPartner_ID(getBusinessPartnerId());
order.setAD_User_ID(partner.getPrimaryAD_User_ID());
order.setC_DocType_ID(docTypeId);
order.setM_Warehouse_ID(getWarehouseinTransitId());
if (getReferenceNo() != null)
order.setPOReference(getReferenceNo());
order.setDocStatus(MDDOrder.DOCSTATUS_Drafted);
order.setDocAction(MDDOrder.DOCACTION_Complete);
order.setDateOrdered(mrp.getDateFinishSchedule());
order.setDatePromised(mrp.getDatePromised());
order.setM_Shipper_ID(getShipperId());
if (getPriority() != null)
order.setPriorityRule(getPriority());
order.setIsInDispute(false);
order.setIsInTransit(false);
order.setSalesRep_ID(mrp.getPlanner_ID());
order.saveEx();
MDDOrderLine orderLine = new MDDOrderLine(getCtx(), 0, get_TrxName());
orderLine.setDD_Order_ID(order.getDD_Order_ID());
orderLine.setAD_Org_ID(locatorTo.getAD_Org_ID());
orderLine.setM_Locator_ID(locatorFrom.getM_Locator_ID());
orderLine.setM_LocatorTo_ID(locatorTo.getM_Locator_ID());
orderLine.setM_Product_ID(mrp.getM_Product_ID());
orderLine.setDateOrdered(order.getDateOrdered());
orderLine.setDatePromised(mrp.getDatePromised());
orderLine.setQtyEntered(getSelectionAsBigDecimal(mrp.getPP_MRP_ID(), "MRP_" + MPPMRP.COLUMNNAME_Qty));
orderLine.setQtyOrdered(getSelectionAsBigDecimal(mrp.getPP_MRP_ID(), "MRP_" + MPPMRP.COLUMNNAME_Qty));
orderLine.setConfirmedQty(getSelectionAsBigDecimal(mrp.getPP_MRP_ID(), "DL_" + MDDOrderLine.COLUMNNAME_ConfirmedQty));
orderLine.setTargetQty(MPPMRP.getQtyReserved(getCtx(), locatorTo.getM_Warehouse_ID(), mrp.getM_Product_ID(), mrp.getDateStartSchedule(), get_TrxName()));
orderLine.setIsInvoiced(false);
orderLine.saveEx();
order.processIt(DocAction.ACTION_Prepare);
order.saveEx();
return order;
}
use of org.eevolution.model.MDDOrder in project adempiere by adempiere.
the class MovementGenerate method generate.
// doIt
/**
* Generate Shipments
* @param pstmt Order Query
* @return info
*/
private String generate(PreparedStatement pstmt) {
MClient client = MClient.get(getCtx());
try {
ResultSet rs = pstmt.executeQuery();
while (// Order
rs.next()) {
MDDOrder order = new MDDOrder(getCtx(), rs, get_TrxName());
// New Header different Shipper, Shipment Location
if (!p_ConsolidateDocument || (m_movement != null && (m_movement.getC_BPartner_Location_ID() != order.getC_BPartner_Location_ID() || m_movement.getM_Shipper_ID() != order.getM_Shipper_ID()))) {
completeMovement();
}
log.fine("check: " + order + " - DeliveryRule=" + order.getDeliveryRule());
//
Timestamp minGuaranteeDate = m_movementDate;
boolean completeOrder = MDDOrder.DELIVERYRULE_CompleteOrder.equals(order.getDeliveryRule());
// OrderLine WHERE
String where = " " + p_M_Warehouse_ID + " IN (SELECT l.M_Warehouse_ID FROM M_Locator l WHERE l.M_Locator_ID=M_Locator_ID) ";
if (p_DatePromised != null)
where += " AND (TRUNC(DatePromised)<=" + DB.TO_DATE(p_DatePromised, true) + " OR DatePromised IS NULL)";
// Exclude Auto Delivery if not Force
if (!MDDOrder.DELIVERYRULE_Force.equals(order.getDeliveryRule()))
where += " AND (DD_OrderLine.M_Product_ID IS NULL" + " OR EXISTS (SELECT * FROM M_Product p " + "WHERE DD_OrderLine.M_Product_ID=p.M_Product_ID" + " AND IsExcludeAutoDelivery='N'))";
// Exclude Unconfirmed
if (!p_IsUnconfirmedInOut)
where += " AND NOT EXISTS (SELECT * FROM M_MovementLine iol" + " INNER JOIN M_Movement io ON (iol.M_Movement_ID=io.M_Movement_ID) " + "WHERE iol.DD_OrderLine_ID=DD_OrderLine.DD_OrderLine_ID AND io.DocStatus IN ('IP','WC'))";
// Deadlock Prevention - Order by M_Product_ID
MDDOrderLine[] lines = order.getLines(where, "M_Product_ID");
for (int i = 0; i < lines.length; i++) {
MDDOrderLine line = lines[i];
MLocator l = new MLocator(getCtx(), line.getM_Locator_ID(), get_TrxName());
if (l.getM_Warehouse_ID() != p_M_Warehouse_ID)
continue;
log.fine("check: " + line);
BigDecimal onHand = Env.ZERO;
//BigDecimal toDeliver = line.getQtyOrdered()
//.subtract(line.getQtyDelivered());
BigDecimal toDeliver = line.getConfirmedQty();
MProduct product = line.getProduct();
// Nothing to Deliver
if (product != null && toDeliver.signum() == 0)
continue;
// or it's a charge - Bug#: 1603966
if (line.getC_Charge_ID() != 0 && toDeliver.signum() == 0)
continue;
// Check / adjust for confirmations
BigDecimal unconfirmedShippedQty = Env.ZERO;
if (p_IsUnconfirmedInOut && product != null && toDeliver.signum() != 0) {
String where2 = "EXISTS (SELECT * FROM M_Movement io WHERE io.M_Movement_ID=M_MovementLine.M_Movement_ID AND io.DocStatus IN ('IP','WC'))";
MMovementLine[] iols = MMovementLine.getOfOrderLine(getCtx(), line.getDD_OrderLine_ID(), where2, null);
for (int j = 0; j < iols.length; j++) unconfirmedShippedQty = unconfirmedShippedQty.add(iols[j].getMovementQty());
String logInfo = "Unconfirmed Qty=" + unconfirmedShippedQty + " - ToDeliver=" + toDeliver + "->";
toDeliver = toDeliver.subtract(unconfirmedShippedQty);
logInfo += toDeliver;
if (toDeliver.signum() < 0) {
toDeliver = Env.ZERO;
logInfo += " (set to 0)";
}
// Adjust On Hand
onHand = onHand.subtract(unconfirmedShippedQty);
log.fine(logInfo);
}
// Comments & lines w/o product & services
if ((product == null || !product.isStocked()) && (// comments
line.getQtyOrdered().signum() == 0 || // lines w/o product
toDeliver.signum() != 0)) {
if (// printed later
!MDDOrder.DELIVERYRULE_CompleteOrder.equals(order.getDeliveryRule()))
createLine(order, line, toDeliver, null, false);
continue;
}
// Stored Product
MProductCategory pc = MProductCategory.get(order.getCtx(), product.getM_Product_Category_ID());
String MMPolicy = pc.getMMPolicy();
if (MMPolicy == null || MMPolicy.length() == 0)
MMPolicy = client.getMMPolicy();
//
MStorage[] storages = getStorages(l.getM_Warehouse_ID(), line.getM_Product_ID(), line.getM_AttributeSetInstance_ID(), product.getM_AttributeSet_ID(), line.getM_AttributeSetInstance_ID() == 0, minGuaranteeDate, MClient.MMPOLICY_FiFo.equals(MMPolicy));
for (int j = 0; j < storages.length; j++) {
MStorage storage = storages[j];
onHand = onHand.add(storage.getQtyOnHand());
}
boolean fullLine = onHand.compareTo(toDeliver) >= 0 || toDeliver.signum() < 0;
// Complete Order
if (completeOrder && !fullLine) {
log.fine("Failed CompleteOrder - OnHand=" + onHand + " (Unconfirmed=" + unconfirmedShippedQty + "), ToDeliver=" + toDeliver + " - " + line);
completeOrder = false;
break;
} else // Complete Line
if (fullLine && MDDOrder.DELIVERYRULE_CompleteLine.equals(order.getDeliveryRule())) {
log.fine("CompleteLine - OnHand=" + onHand + " (Unconfirmed=" + unconfirmedShippedQty + ", ToDeliver=" + toDeliver + " - " + line);
//
createLine(order, line, toDeliver, storages, false);
} else // Availability
if (MDDOrder.DELIVERYRULE_Availability.equals(order.getDeliveryRule()) && (onHand.signum() > 0 || toDeliver.signum() < 0)) {
BigDecimal deliver = toDeliver;
if (deliver.compareTo(onHand) > 0)
deliver = onHand;
log.fine("Available - OnHand=" + onHand + " (Unconfirmed=" + unconfirmedShippedQty + "), ToDeliver=" + toDeliver + ", Delivering=" + deliver + " - " + line);
//
createLine(order, line, deliver, storages, false);
} else // Force
if (MDDOrder.DELIVERYRULE_Force.equals(order.getDeliveryRule())) {
BigDecimal deliver = toDeliver;
log.fine("Force - OnHand=" + onHand + " (Unconfirmed=" + unconfirmedShippedQty + "), ToDeliver=" + toDeliver + ", Delivering=" + deliver + " - " + line);
//
createLine(order, line, deliver, storages, true);
} else // Manual
if (MDDOrder.DELIVERYRULE_Manual.equals(order.getDeliveryRule()))
log.fine("Manual - OnHand=" + onHand + " (Unconfirmed=" + unconfirmedShippedQty + ") - " + line);
else
log.fine("Failed: " + order.getDeliveryRule() + " - OnHand=" + onHand + " (Unconfirmed=" + unconfirmedShippedQty + "), ToDeliver=" + toDeliver + " - " + line);
}
// Complete Order successful
if (completeOrder && MDDOrder.DELIVERYRULE_CompleteOrder.equals(order.getDeliveryRule())) {
for (int i = 0; i < lines.length; i++) {
MDDOrderLine line = lines[i];
MLocator l = new MLocator(getCtx(), line.getM_Locator_ID(), get_TrxName());
if (l.getM_Warehouse_ID() != p_M_Warehouse_ID)
continue;
MProduct product = line.getProduct();
BigDecimal toDeliver = line.getQtyOrdered().subtract(line.getQtyDelivered());
//
MStorage[] storages = null;
if (product != null && product.isStocked()) {
MProductCategory pc = MProductCategory.get(order.getCtx(), product.getM_Product_Category_ID());
String MMPolicy = pc.getMMPolicy();
if (MMPolicy == null || MMPolicy.length() == 0)
MMPolicy = client.getMMPolicy();
//
storages = getStorages(l.getM_Warehouse_ID(), line.getM_Product_ID(), line.getM_AttributeSetInstance_ID(), product.getM_AttributeSet_ID(), line.getM_AttributeSetInstance_ID() == 0, minGuaranteeDate, MClient.MMPOLICY_FiFo.equals(MMPolicy));
}
//
createLine(order, line, toDeliver, storages, false);
}
}
m_line += 1000;
}
// while order
rs.close();
pstmt.close();
pstmt = null;
} catch (Exception e) {
log.log(Level.SEVERE, m_sql, e);
}
try {
if (pstmt != null)
pstmt.close();
pstmt = null;
} catch (Exception e) {
pstmt = null;
}
completeMovement();
return "@Created@ = " + m_created;
}
Aggregations