use of org.eevolution.model.MDDOrder in project adempiere by adempiere.
the class MovementGenerate method generate.
/**
* Generate Shipments
* @param distributionOrders Orders
* @return info
*/
private String generate(List<MDDOrder> distributionOrders) {
distributionOrders.stream().filter(order -> order != null).forEach(order -> {
getCurrentMovement().filter(movement -> movement != null || !isConsolidatetooneDocument()).filter(movement -> movement.getC_BPartner_Location_ID() != order.getC_BPartner_Location_ID() || movement.getM_Shipper_ID() != order.getM_Shipper_ID()).ifPresent(movement -> completeMovement());
log.fine("check: " + order + " - DeliveryRule=" + order.getDeliveryRule());
Timestamp minGuaranteeDate = getMovementDate();
StringBuilder where = new StringBuilder(" 1=1 ");
if (getWarehouseId() > 0)
where.append(" AND ").append(getWarehouseId()).append(" IN (SELECT l.M_Warehouse_ID FROM M_Locator l WHERE l.M_Locator_ID=M_Locator_ID) ");
if (getDatePromised() != null)
where.append(" AND (TRUNC(DatePromised)<=").append(DB.TO_DATE(getDatePromised(), true)).append(" OR DatePromised IS NULL)");
if (!MDDOrder.DELIVERYRULE_Force.equals(order.getDeliveryRule()))
where.append(" AND (DD_OrderLine.M_Product_ID IS NULL").append(" OR EXISTS (SELECT * FROM M_Product p ").append("WHERE DD_OrderLine.M_Product_ID=p.M_Product_ID").append(" AND IsExcludeAutoDelivery='N'))");
if (!isOrderswithunconfirmedShipments())
where.append(" AND NOT EXISTS (SELECT * FROM M_MovementLine iol").append(" INNER JOIN M_Movement io ON (iol.M_Movement_ID=io.M_Movement_ID) ").append("WHERE iol.DD_OrderLine_ID=DD_OrderLine.DD_OrderLine_ID AND io.DocStatus IN ('IP','WC'))");
List<MDDOrderLine> orderLines = order.getLines(where.toString(), "M_Product_ID");
orderLines.stream().filter(orderLine -> orderLine.getM_Product_ID() > 0 || orderLine.getC_Charge_ID() > 0).filter(orderLine -> orderLine.getConfirmedQty().signum() != 0).forEach(orderLine -> {
MLocator locator = new MLocator(getCtx(), orderLine.getM_Locator_ID(), get_TrxName());
if (getWarehouseId() > 0 && locator.getM_Warehouse_ID() != getWarehouseId())
return;
log.fine("check: " + orderLine);
BigDecimal onHand = Env.ZERO;
BigDecimal toDeliver = orderLine.getConfirmedQty();
MProduct product = orderLine.getProduct();
BigDecimal unconfirmedShippedQty = Env.ZERO;
if (isOrderswithunconfirmedShipments() && product != null && toDeliver.signum() != 0) {
String whereLine = "EXISTS (SELECT * FROM M_Movement io WHERE io.M_Movement_ID=M_MovementLine.M_Movement_ID AND io.DocStatus IN ('IP','WC'))";
MMovementLine[] movementLines = MMovementLine.getOfOrderLine(getCtx(), orderLine.getDD_OrderLine_ID(), whereLine, null);
unconfirmedShippedQty = Arrays.asList(movementLines).stream().map(MMovementLine::getMovementQty).reduce(BigDecimal.ZERO, BigDecimal::add);
String logInfo = "Unconfirmed Qty=" + unconfirmedShippedQty + " - ToDeliver=" + orderLine.getConfirmedQty() + "->";
toDeliver = orderLine.getConfirmedQty().subtract(unconfirmedShippedQty);
logInfo += toDeliver;
if (toDeliver.signum() < 0) {
toDeliver = Env.ZERO;
logInfo += " (set to 0)";
}
onHand = onHand.subtract(unconfirmedShippedQty);
log.fine(logInfo);
}
if ((product == null || !product.isStocked()) && (orderLine.getQtyOrdered().signum() == 0 || toDeliver.signum() != 0)) {
if (!MDDOrder.DELIVERYRULE_CompleteOrder.equals(order.getDeliveryRule()))
createLine(order, orderLine, toDeliver, null, false);
return;
}
String policyMaterialIssue = getPolicyIssue(product);
MStorage[] storages = getStorages(locator.getM_Warehouse_ID(), orderLine.getM_Product_ID(), orderLine.getM_AttributeSetInstance_ID(), product.getM_AttributeSet_ID(), orderLine.getM_AttributeSetInstance_ID() == 0, minGuaranteeDate, MClient.MMPOLICY_FiFo.equals(policyMaterialIssue));
onHand = Arrays.asList(storages).stream().map(MStorage::getQtyOnHand).reduce(BigDecimal.ZERO, BigDecimal::add);
boolean fullLine = onHand.compareTo(toDeliver) >= 0 || toDeliver.signum() < 0;
boolean completeOrder = MDDOrder.DELIVERYRULE_CompleteOrder.equals(order.getDeliveryRule());
if (completeOrder && !fullLine) {
log.fine("Failed CompleteOrder - OnHand=" + onHand + " (Unconfirmed=" + unconfirmedShippedQty + "), ToDeliver=" + toDeliver + " - " + orderLine);
return;
} else if (fullLine && MDDOrder.DELIVERYRULE_CompleteLine.equals(order.getDeliveryRule())) {
log.fine("CompleteLine - OnHand=" + onHand + " (Unconfirmed=" + unconfirmedShippedQty + ", ToDeliver=" + toDeliver + " - " + orderLine);
createLine(order, orderLine, toDeliver, storages, false);
} else if (MDDOrder.DELIVERYRULE_Availability.equals(order.getDeliveryRule()) && (onHand.signum() > 0 || orderLine.getConfirmedQty().signum() < 0)) {
BigDecimal deliver = orderLine.getConfirmedQty();
log.fine("Available - OnHand=" + onHand + " (Unconfirmed=" + unconfirmedShippedQty + "), ToDeliver=" + toDeliver + ", Delivering=" + deliver + " - " + orderLine);
if (deliver.compareTo(onHand) > 0)
deliver = onHand;
createLine(order, orderLine, deliver, storages, false);
} else if (MDDOrder.DELIVERYRULE_Force.equals(order.getDeliveryRule())) {
BigDecimal deliver = toDeliver;
log.fine("Force - OnHand=" + onHand + " (Unconfirmed=" + unconfirmedShippedQty + "), ToDeliver=" + toDeliver + ", Delivering=" + deliver + " - " + orderLine);
createLine(order, orderLine, deliver, storages, true);
} else if (MDDOrder.DELIVERYRULE_Manual.equals(order.getDeliveryRule()))
log.fine("Manual - OnHand=" + onHand + " (Unconfirmed=" + unconfirmedShippedQty + ") - " + orderLine);
else
log.fine("Failed: " + order.getDeliveryRule() + " - OnHand=" + onHand + " (Unconfirmed=" + unconfirmedShippedQty + "), ToDeliver=" + toDeliver + " - " + orderLine);
});
boolean completeOrder = MDDOrder.DELIVERYRULE_CompleteOrder.equals(order.getDeliveryRule());
if (completeOrder && MDDOrder.DELIVERYRULE_CompleteOrder.equals(order.getDeliveryRule())) {
for (MDDOrderLine orderLine : orderLines) {
MLocator l = new MLocator(getCtx(), orderLine.getM_Locator_ID(), get_TrxName());
if (getWarehouseId() > 0 && l.getM_Warehouse_ID() != getWarehouseId())
continue;
MProduct product = orderLine.getProduct();
BigDecimal toDeliver = orderLine.getQtyOrdered().subtract(orderLine.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 = getClient().getMMPolicy();
storages = getStorages(l.getM_Warehouse_ID(), orderLine.getM_Product_ID(), orderLine.getM_AttributeSetInstance_ID(), product.getM_AttributeSet_ID(), orderLine.getM_AttributeSetInstance_ID() == 0, minGuaranteeDate, MClient.MMPOLICY_FiFo.equals(MMPolicy));
}
createLine(order, orderLine, toDeliver, storages, false);
}
}
lineNo += 1000;
completeMovement();
});
return "@Created@ = " + createdCount;
}
use of org.eevolution.model.MDDOrder in project adempiere by adempiere.
the class VOrderDistributionReceipt method generateMovements.
// saveSelection
/**************************************************************************
* Generate Shipments
*/
private void generateMovements() {
log.info("DD_Order_ID=" + m_DD_Order_ID);
log.info("MovementDate" + m_MovementDate);
String trxName = Trx.createTrxName("IOG");
//trx needs to be committed too
Trx trx = Trx.get(trxName, true);
// prevents from being called twice
m_selectionActive = false;
statusBar.setStatusLine(Msg.translate(Env.getCtx(), "M_Movement_ID"));
statusBar.setStatusDB(String.valueOf(selection.size()));
if (selection.size() <= 0)
return;
Properties m_ctx = Env.getCtx();
Timestamp MovementDate = (Timestamp) m_MovementDate;
MDDOrder order = new MDDOrder(m_ctx, Integer.parseInt(m_DD_Order_ID.toString()), trxName);
MMovement movement = new MMovement(m_ctx, 0, trxName);
movement.setDD_Order_ID(order.getDD_Order_ID());
movement.setAD_User_ID(order.getAD_User_ID());
movement.setPOReference(order.getPOReference());
movement.setReversal_ID(0);
movement.setM_Shipper_ID(order.getM_Shipper_ID());
movement.setDescription(order.getDescription());
movement.setC_BPartner_ID(order.getC_BPartner_ID());
movement.setC_BPartner_Location_ID(order.getC_BPartner_Location_ID());
movement.setAD_Org_ID(order.getAD_Org_ID());
movement.setAD_OrgTrx_ID(order.getAD_OrgTrx_ID());
movement.setAD_User_ID(order.getAD_User_ID());
movement.setC_Activity_ID(order.getC_Activity_ID());
movement.setC_Campaign_ID(order.getC_Campaign_ID());
movement.setC_Project_ID(order.getC_Project_ID());
movement.setMovementDate(MovementDate);
movement.setDeliveryRule(order.getDeliveryRule());
movement.setDeliveryViaRule(order.getDeliveryViaRule());
movement.setDocAction(MMovement.ACTION_Prepare);
movement.setDocStatus(MMovement.DOCSTATUS_Drafted);
//Look the document type for the organization
int docTypeDO_ID = getDocType(MDocType.DOCBASETYPE_MaterialMovement, order.getAD_Org_ID());
if (docTypeDO_ID > 0)
movement.setC_DocType_ID(docTypeDO_ID);
movement.saveEx();
for (int i = 0; i < selection.size(); i++) {
int DD_OrderLine_ID = selection.get(i);
MDDOrderLine oline = new MDDOrderLine(m_ctx, DD_OrderLine_ID, trxName);
MMovementLine line = new MMovementLine(movement);
line.setM_Product_ID(oline.getM_Product_ID());
BigDecimal QtyDeliver = (BigDecimal) miniTable.getValueAt(i, 1);
if (QtyDeliver == null | QtyDeliver.compareTo(oline.getQtyInTransit()) > 0)
throw new AdempiereException("Error in Qty");
line.setOrderLine(oline, QtyDeliver, true);
line.saveEx();
}
movement.setDocAction(MMovement.DOCACTION_Close);
movement.setDocStatus(movement.completeIt());
movement.saveEx();
trx.commit();
generateMovements_complete(movement);
//
}
use of org.eevolution.model.MDDOrder in project adempiere by adempiere.
the class ReplenishReport method createDO.
// Create Inventory Movements
/**
* Create Distribution Order
*/
private void createDO() throws Exception {
int noMoves = 0;
String info = "";
//
MClient client = null;
MDDOrder order = null;
int M_Warehouse_ID = 0;
int M_WarehouseSource_ID = 0;
MWarehouse whSource = null;
MWarehouse wh = null;
X_T_Replenish[] replenishs = getReplenishDO("M_WarehouseSource_ID IS NOT NULL");
for (X_T_Replenish replenish : replenishs) {
if (whSource == null || whSource.getM_WarehouseSource_ID() != replenish.getM_WarehouseSource_ID())
whSource = MWarehouse.get(getCtx(), replenish.getM_WarehouseSource_ID());
if (wh == null || wh.getM_Warehouse_ID() != replenish.getM_Warehouse_ID())
wh = MWarehouse.get(getCtx(), replenish.getM_Warehouse_ID());
if (client == null || client.getAD_Client_ID() != whSource.getAD_Client_ID())
client = MClient.get(getCtx(), whSource.getAD_Client_ID());
//
if (order == null || M_WarehouseSource_ID != replenish.getM_WarehouseSource_ID() || M_Warehouse_ID != replenish.getM_Warehouse_ID()) {
M_WarehouseSource_ID = replenish.getM_WarehouseSource_ID();
M_Warehouse_ID = replenish.getM_Warehouse_ID();
order = new MDDOrder(getCtx(), 0, get_TrxName());
order.setC_DocType_ID(p_C_DocType_ID);
order.setDescription(Msg.getMsg(getCtx(), "Replenishment") + ": " + whSource.getName() + "->" + wh.getName());
// Set Org
order.setAD_Org_ID(whSource.getAD_Org_ID());
// Set Org Trx
MOrg orgTrx = MOrg.get(getCtx(), wh.getAD_Org_ID());
order.setAD_OrgTrx_ID(orgTrx.getAD_Org_ID());
int C_BPartner_ID = orgTrx.getLinkedC_BPartner_ID(get_TrxName());
if (C_BPartner_ID == 0)
throw new AdempiereUserError(Msg.translate(getCtx(), "C_BPartner_ID") + " @FillMandatory@ ");
MBPartner bp = new MBPartner(getCtx(), C_BPartner_ID, get_TrxName());
// Set BPartner Link to Org
order.setBPartner(bp);
order.setDateOrdered(new Timestamp(System.currentTimeMillis()));
//order.setDatePromised(DatePromised);
order.setDeliveryRule(MDDOrder.DELIVERYRULE_Availability);
order.setDeliveryViaRule(MDDOrder.DELIVERYVIARULE_Delivery);
order.setPriorityRule(MDDOrder.PRIORITYRULE_Medium);
order.setIsInDispute(false);
order.setIsApproved(false);
order.setIsDropShip(false);
order.setIsDelivered(false);
order.setIsInTransit(false);
order.setIsPrinted(false);
order.setIsSelected(false);
order.setIsSOTrx(false);
// Warehouse in Transit
MWarehouse[] whsInTransit = MWarehouse.getForOrg(getCtx(), whSource.getAD_Org_ID());
for (MWarehouse whInTransit : whsInTransit) {
if (whInTransit.isInTransit())
order.setM_Warehouse_ID(whInTransit.getM_Warehouse_ID());
}
if (order.getM_Warehouse_ID() == 0)
throw new AdempiereUserError("Warehouse inTransit is @FillMandatory@ ");
if (!order.save())
return;
log.fine(order.toString());
noMoves++;
info += " - " + order.getDocumentNo();
}
// To
int M_LocatorTo_ID = wh.getDefaultLocator().getM_Locator_ID();
int M_Locator_ID = whSource.getDefaultLocator().getM_Locator_ID();
if (M_LocatorTo_ID == 0 || M_Locator_ID == 0)
throw new AdempiereUserError(Msg.translate(getCtx(), "M_Locator_ID") + " @FillMandatory@ ");
// From: Look-up Storage
/*MProduct product = MProduct.get(getCtx(), replenish.getM_Product_ID());
MProductCategory pc = MProductCategory.get(getCtx(), product.getM_Product_Category_ID());
String MMPolicy = pc.getMMPolicy();
if (MMPolicy == null || MMPolicy.length() == 0)
MMPolicy = client.getMMPolicy();
//
MStorage[] storages = MStorage.getWarehouse(getCtx(),
whSource.getM_Warehouse_ID(), replenish.getM_Product_ID(), 0, 0,
true, null,
MClient.MMPOLICY_FiFo.equals(MMPolicy), get_TrxName());
BigDecimal target = replenish.getQtyToOrder();
for (int j = 0; j < storages.length; j++)
{
MStorage storage = storages[j];
if (storage.getQtyOnHand().signum() <= 0)
continue;
BigDecimal moveQty = target;
if (storage.getQtyOnHand().compareTo(moveQty) < 0)
moveQty = storage.getQtyOnHand();
//
MDDOrderLine line = new MDDOrderLine(order);
line.setM_Product_ID(replenish.getM_Product_ID());
line.setQtyEntered(moveQty);
if (replenish.getQtyToOrder().compareTo(moveQty) != 0)
line.setDescription("Total: " + replenish.getQtyToOrder());
line.setM_Locator_ID(storage.getM_Locator_ID()); // from
line.setM_AttributeSetInstance_ID(storage.getM_AttributeSetInstance_ID());
line.setM_LocatorTo_ID(M_LocatorTo_ID); // to
line.setM_AttributeSetInstanceTo_ID(storage.getM_AttributeSetInstance_ID());
line.setIsInvoiced(false);
line.saveEx();
//
target = target.subtract(moveQty);
if (target.signum() == 0)
break;
}*/
MDDOrderLine line = new MDDOrderLine(order);
line.setM_Product_ID(replenish.getM_Product_ID());
line.setQty(replenish.getQtyToOrder());
if (replenish.getQtyToOrder().compareTo(replenish.getQtyToOrder()) != 0)
line.setDescription("Total: " + replenish.getQtyToOrder());
// from
line.setM_Locator_ID(M_Locator_ID);
line.setM_AttributeSetInstance_ID(0);
// to
line.setM_LocatorTo_ID(M_LocatorTo_ID);
line.setM_AttributeSetInstanceTo_ID(0);
line.setIsInvoiced(false);
line.saveEx();
}
if (replenishs.length == 0) {
m_info = "No Source Warehouse";
log.warning(m_info);
} else {
m_info = "#" + noMoves + info;
log.info(m_info);
}
}
use of org.eevolution.model.MDDOrder in project adempiere by adempiere.
the class Doc_DDOrder method loadDocumentDetails.
//Doc Cost Collector
/**
* Load Document Details
* @return error message or null
*/
protected String loadDocumentDetails() {
MDDOrder order = (MDDOrder) getPO();
setDateDoc(order.getDateOrdered());
return STATUS_Posted;
}
use of org.eevolution.model.MDDOrder in project adempiere by adempiere.
the class OrderDistributionReceipt method generate.
// saveSelection
/**************************************************************************
* Generate Movements
*/
public String generate(MiniTable miniTable, IStatusBar statusBar, String docActionSelected) {
String info = "";
log.info("DD_Order_ID=" + m_DD_Order_ID);
log.info("MovementDate" + m_MovementDate);
String trxName = Trx.createTrxName("MVG");
Trx trx = Trx.get(trxName, true);
// prevents from being called twice
setSelectionActive(false);
statusBar.setStatusLine(Msg.translate(Env.getCtx(), "M_Movement_ID"));
statusBar.setStatusDB(String.valueOf(getSelection().size()));
Properties m_ctx = Env.getCtx();
Timestamp movementDate = (Timestamp) m_MovementDate;
MDDOrder order = new MDDOrder(m_ctx, Integer.parseInt(m_DD_Order_ID.toString()), trxName);
MMovement movement = new MMovement(order, movementDate);
movement.saveEx();
ArrayList<Integer> ids = getSelection();
int i = 0;
for (int DD_OrderLine_ID : ids) {
MDDOrderLine oline = new MDDOrderLine(m_ctx, DD_OrderLine_ID, trxName);
MMovementLine line = new MMovementLine(movement);
line.setM_Product_ID(oline.getM_Product_ID());
BigDecimal QtyDeliver = (BigDecimal) miniTable.getValueAt(i, 1);
if (QtyDeliver == null | QtyDeliver.compareTo(oline.getQtyInTransit()) > 0)
throw new AdempiereException("Error in Qty");
line.setOrderLine(oline, QtyDeliver, true);
line.saveEx();
i++;
}
// Fails if there is a confirmation
if (!movement.processIt(MMovement.ACTION_Complete))
log.warning("Failed: " + movement);
movement.setDocStatus(MMovement.DOCACTION_Complete);
movement.setDocAction(MMovement.ACTION_Close);
movement.saveEx();
return info;
}
Aggregations