use of org.compiere.model.MInOutLine in project adempiere by adempiere.
the class ModelValidator method docValidate.
public String docValidate(PO po, int timing) {
log.info(po.get_TableName() + " Timing: " + timing);
String result = null;
// TABLE C_Invoice
String tableName = po.get_TableName();
if (tableName.equals(MInvoice.Table_Name)) {
// Invoice - Validate Fixed Assets Invoice (LRO)
if (timing == TIMING_AFTER_PREPARE) {
MInvoice invoice = (MInvoice) po;
validateFixedAssetsInvoice_LRO(invoice);
}
if (timing == TIMING_AFTER_COMPLETE) {
MInvoice mi = (MInvoice) po;
if (mi.isSOTrx()) {
MInvoiceLine[] mils = mi.getLines();
for (MInvoiceLine mil : mils) {
if (mil.isA_CreateAsset() && !mil.isA_Processed()) {
MAssetDisposed.createAssetDisposed(mil);
}
}
}
}
if (timing == TIMING_AFTER_VOID) {
MInvoice invoice = (MInvoice) po;
String error = afterVoid(invoice);
if (error != null)
return error;
}
if (timing == TIMING_BEFORE_REVERSECORRECT) {
MInvoice invoice = (MInvoice) po;
String error = beforeReverseCorrect(invoice);
if (error != null)
return error;
}
}
if (tableName.equals(MInOut.Table_Name)) {
if (timing == TIMING_AFTER_COMPLETE) {
MInOut inOut = (MInOut) po;
for (MInOutLine inOutLine : inOut.getLines()) {
MProduct product = inOutLine.getProduct();
// Create Asset for SO
if (product != null && inOut.isSOTrx() && product.isCreateAsset() && !product.getM_Product_Category().getA_Asset_Group().isFixedAsset() && inOutLine.getMovementQty().signum() > 0 && !inOut.isReversal()) {
log.fine("Asset");
//info.append("@A_Asset_ID@: ");
int noAssets = inOutLine.getMovementQty().intValue();
if (!product.isOneAssetPerUOM())
noAssets = 1;
for (int i = 0; i < noAssets; i++) {
//if (i > 0)
// info.append(" - ");
int deliveryCount = i + 1;
if (!product.isOneAssetPerUOM())
deliveryCount = 0;
MAsset asset = new MAsset(inOut, inOutLine, deliveryCount);
if (!asset.save(inOut.get_TrxName())) {
//return DocAction.STATUS_Invalid;
throw new IllegalStateException("Could not create Asset");
}
//info.append(asset.getValue());
}
}
}
// Asset
}
if (timing == TIMING_AFTER_REVERSECORRECT) {
MInOut inOut = (MInOut) po;
I_M_InOut inOutReversal = inOut.getReversal();
for (MInOutLine inOutLine : inOut.getLines()) {
// De-Activate Asset
MAsset asset = MAsset.getFromShipment(inOut.getCtx(), inOutLine.getM_InOutLine_ID(), inOut.get_TrxName());
if (asset != null) {
asset.setIsActive(false);
asset.setDescription(asset.getDescription() + " (" + inOutReversal.getDocumentNo() + " #" + inOutLine.getLine() + "<-)");
asset.saveEx();
}
}
}
}
return result;
}
use of org.compiere.model.MInOutLine in project adempiere by adempiere.
the class InOutGenerate method generate.
// doIt
/**
* Generate Shipments
* @param pstmt Order Query
* @return info
*/
private String generate(PreparedStatement pstmt) {
try {
ResultSet rs = pstmt.executeQuery();
while (// Order
rs.next()) {
MOrder order = new MOrder(getCtx(), rs, get_TrxName());
// New Header different Shipper, Shipment Location
if (!p_ConsolidateDocument || (m_shipment != null && (m_shipment.getC_BPartner_Location_ID() != order.getC_BPartner_Location_ID() || m_shipment.getM_Shipper_ID() != order.getM_Shipper_ID())))
completeShipment();
log.fine("check: " + order + " - DeliveryRule=" + order.getDeliveryRule());
//
Timestamp minGuaranteeDate = m_movementDate;
boolean completeOrder = MOrder.DELIVERYRULE_CompleteOrder.equals(order.getDeliveryRule());
// OrderLine WHERE
String where = " AND M_Warehouse_ID=" + p_M_Warehouse_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 (!MOrder.DELIVERYRULE_Force.equals(order.getDeliveryRule()))
where += " AND (C_OrderLine.M_Product_ID IS NULL" + " OR EXISTS (SELECT * FROM M_Product p " + "WHERE C_OrderLine.M_Product_ID=p.M_Product_ID" + " AND IsExcludeAutoDelivery='N'))";
// Exclude Unconfirmed
if (!p_IsUnconfirmedInOut)
where += " AND NOT EXISTS (SELECT * FROM M_InOutLine iol" + " INNER JOIN M_InOut io ON (iol.M_InOut_ID=io.M_InOut_ID) " + "WHERE iol.C_OrderLine_ID=C_OrderLine.C_OrderLine_ID AND io.DocStatus IN ('IP','WC'))";
// Deadlock Prevention - Order by M_Product_ID
MOrderLine[] lines = order.getLines(where, "C_BPartner_Location_ID, M_Product_ID");
for (int i = 0; i < lines.length; i++) {
MOrderLine line = lines[i];
if (line.getM_Warehouse_ID() != p_M_Warehouse_ID)
continue;
log.fine("check: " + line);
BigDecimal onHand = Env.ZERO;
BigDecimal toDeliver = line.getQtyOrdered().subtract(line.getQtyDelivered());
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_InOut io WHERE io.M_InOut_ID=M_InOutLine.M_InOut_ID AND io.DocStatus IN ('IP','WC'))";
MInOutLine[] iols = MInOutLine.getOfOrderLine(getCtx(), line.getC_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
!MOrder.DELIVERYRULE_CompleteOrder.equals(order.getDeliveryRule()))
createLine(order, line, toDeliver, null, false);
continue;
}
// Stored Product
String MMPolicy = product.getMMPolicy();
MStorage[] storages = getStorages(line.getM_Warehouse_ID(), line.getM_Product_ID(), line.getM_AttributeSetInstance_ID(), 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 && MOrder.DELIVERYRULE_CompleteLine.equals(order.getDeliveryRule())) {
log.fine("CompleteLine - OnHand=" + onHand + " (Unconfirmed=" + unconfirmedShippedQty + ", ToDeliver=" + toDeliver + " - " + line);
//
createLine(order, line, toDeliver, storages, false);
} else // Availability
if (MOrder.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 (MOrder.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 (MOrder.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 && MOrder.DELIVERYRULE_CompleteOrder.equals(order.getDeliveryRule())) {
for (int i = 0; i < lines.length; i++) {
MOrderLine line = lines[i];
if (line.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()) {
String MMPolicy = product.getMMPolicy();
storages = getStorages(line.getM_Warehouse_ID(), line.getM_Product_ID(), line.getM_AttributeSetInstance_ID(), 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;
}
completeShipment();
return "@Created@ = " + m_created;
}
use of org.compiere.model.MInOutLine in project adempiere by adempiere.
the class InvoiceCreateFrom method doIt.
@Override
protected String doIt() throws Exception {
// Valid Record Identifier
if (getRecord_ID() == 0)
return "";
// Get Shipment
MInvoice invoice = new MInvoice(getCtx(), getRecord_ID(), get_TrxName());
AtomicInteger referenceId = new AtomicInteger(0);
AtomicInteger created = new AtomicInteger(0);
List<Integer> recordIds = getSelectionKeys();
String createFromType = recordIds.size() > 0 ? getSelectionAsString(recordIds.get(0), "CF_CreateFromType") : null;
log.fine("CreateFromType=" + createFromType);
if (createFromType == null || createFromType.length() == 0)
throw new AdempiereException("@CreateFromType@ @NotFound@");
// Loop
recordIds.stream().forEach(key -> {
int productId = getSelectionAsInt(key, "CF_M_Product_ID");
int chargeId = getSelectionAsInt(key, "CF_C_Charge_ID");
int uomId = getSelectionAsInt(key, "CF_C_UOM_ID");
BigDecimal qtyEntered = getSelectionAsBigDecimal(key, "CF_QtyEntered");
log.fine("Line QtyEntered=" + qtyEntered + ", Product=" + productId + ", CreateFromType=" + createFromType + ", Key=" + key);
MInvoiceLine invoiceLine = new MInvoiceLine(invoice);
BigDecimal qtyInvoiced = null;
int precision = 2;
if (productId > 0) {
MProduct product = MProduct.get(Env.getCtx(), productId);
if (product != null) {
invoiceLine.setM_Product_ID(product.getM_Product_ID(), uomId);
precision = product.getUOMPrecision();
if (product.getC_UOM_ID() != uomId) {
qtyEntered = qtyEntered.setScale(precision, BigDecimal.ROUND_HALF_DOWN);
qtyInvoiced = MUOMConversion.convertProductFrom(Env.getCtx(), productId, uomId, qtyEntered);
}
}
} else if (chargeId != 0) {
invoiceLine.setC_Charge_ID(chargeId);
}
qtyEntered = qtyEntered.setScale(precision, BigDecimal.ROUND_HALF_DOWN);
if (qtyInvoiced == null)
qtyInvoiced = qtyEntered;
invoiceLine.setQty(qtyEntered);
invoiceLine.setQtyInvoiced(qtyInvoiced);
if (createFromType.equals(ORDER)) {
MOrderLine orderLine = new MOrderLine(getCtx(), key, get_TrxName());
referenceId.set(orderLine.getC_Order_ID());
String whereClause = "EXISTS (SELECT 1 " + "FROM M_InOut io " + "WHERE io.M_InOut_ID = M_InOutLine.M_InOut_ID " + "AND io.DocStatus IN ('CO','CL'))";
MInOutLine[] inOutLines = MInOutLine.getOfOrderLine(Env.getCtx(), key, whereClause, get_TrxName());
log.fine("Receipt Lines with OrderLine = #" + inOutLines.length);
final BigDecimal qty = qtyEntered;
MInOutLine inOutLine = Arrays.stream(inOutLines).filter(ioLine -> ioLine != null && ioLine.getQtyEntered().compareTo(qty) == 0).findFirst().orElse(inOutLines.length > 0 ? inOutLines[0] : null);
if (inOutLine != null)
invoiceLine.setShipLine(inOutLine);
else
invoiceLine.setOrderLine(orderLine);
} else if (createFromType.equals(INVOICE)) {
MInvoiceLine fromLine = new MInvoiceLine(getCtx(), key, get_TrxName());
referenceId.set(invoiceLine.getParent().getC_Invoice_ID());
PO.copyValues(fromLine, invoiceLine);
invoiceLine.setC_Invoice_ID(invoiceLine.getParent().getC_Invoice_ID());
invoiceLine.setAD_Org_ID(fromLine.getAD_Org_ID());
invoiceLine.setC_OrderLine_ID(0);
invoiceLine.setRef_InvoiceLine_ID(0);
invoiceLine.setM_InOutLine_ID(0);
invoiceLine.setA_Asset_ID(0);
invoiceLine.setM_AttributeSetInstance_ID(0);
invoiceLine.setS_ResourceAssignment_ID(0);
if (invoiceLine.getParent().getC_BPartner_ID() != fromLine.getC_Invoice().getC_BPartner_ID())
invoiceLine.setTax();
invoiceLine.setProcessed(false);
} else if (createFromType.equals(RMA)) {
MRMALine rmaLine = new MRMALine(getCtx(), key, get_TrxName());
referenceId.set(rmaLine.getM_RMA_ID());
invoiceLine.setRMALine(rmaLine);
} else if (createFromType.equals(RECEIPT)) {
MInOutLine inOutLine = new MInOutLine(getCtx(), key, get_TrxName());
referenceId.set(inOutLine.getM_InOut_ID());
invoiceLine.setShipLine(inOutLine);
}
invoiceLine.saveEx();
if (createFromType.equals(INVOICE)) {
MInvoiceLine fromLine = new MInvoiceLine(getCtx(), key, get_TrxName());
invoiceLine.copyLandedCostFrom(fromLine);
invoiceLine.allocateLandedCosts();
}
created.updateAndGet(createNo -> createNo + 1);
});
// Add reference to Order / Invoice / RMA
addReference(invoice, createFromType, referenceId.get());
return "@Created@ " + created.get();
}
use of org.compiere.model.MInOutLine in project adempiere by adempiere.
the class LiberoValidator method docValidate.
// modelChange
public String docValidate(PO po, int timing) {
log.info(po.get_TableName() + " Timing: " + timing);
if (po instanceof MOrder && timing == TIMING_BEFORE_COMPLETE) {
MOrder order = (MOrder) po;
MPPMRP.C_Order(order);
}
if (po instanceof MInOut && timing == TIMING_AFTER_COMPLETE) {
MInOut inout = (MInOut) po;
if (inout.isSOTrx()) {
for (MInOutLine outline : inout.getLines()) {
updateMPPOrder(outline);
}
} else //Purchase Receipt
{
for (MInOutLine line : inout.getLines()) {
final String whereClause = "C_OrderLine_ID=? AND PP_Cost_Collector_ID IS NOT NULL";
Collection<MOrderLine> olines = new Query(po.getCtx(), MOrderLine.Table_Name, whereClause, po.get_TrxName()).setParameters(new Object[] { line.getC_OrderLine_ID() }).list();
for (MOrderLine oline : olines) {
if (oline.getQtyOrdered().compareTo(oline.getQtyDelivered()) >= 0) {
MPPCostCollector cc = new MPPCostCollector(po.getCtx(), oline.getPP_Cost_Collector_ID(), po.get_TrxName());
String docStatus = cc.completeIt();
cc.setDocStatus(docStatus);
cc.setDocAction(MPPCostCollector.DOCACTION_Close);
cc.saveEx();
return null;
}
}
}
}
} else // Update Distribution Order Line
if (po instanceof MMovement && timing == TIMING_AFTER_COMPLETE) {
MMovement move = (MMovement) po;
for (MMovementLine line : move.getLines(false)) {
if (line.getDD_OrderLine_ID() > 0) {
MDDOrderLine oline = new MDDOrderLine(line.getCtx(), line.getDD_OrderLine_ID(), po.get_TrxName());
MLocator locator_to = MLocator.get(line.getCtx(), line.getM_LocatorTo_ID());
MWarehouse warehouse = MWarehouse.get(line.getCtx(), locator_to.getM_Warehouse_ID());
if (warehouse.isInTransit()) {
oline.setQtyInTransit(oline.getQtyInTransit().add(line.getMovementQty()));
oline.setConfirmedQty(Env.ZERO);
} else {
oline.setQtyInTransit(oline.getQtyInTransit().subtract(line.getMovementQty()));
oline.setQtyDelivered(oline.getQtyDelivered().add(line.getMovementQty()));
}
oline.saveEx();
}
}
if (move.getDD_Order_ID() > 0) {
MDDOrder order = new MDDOrder(move.getCtx(), move.getDD_Order_ID(), move.get_TrxName());
order.setIsInTransit(isInTransting(order));
order.reserveStock(order.getLines(true, null));
order.saveEx();
}
}
return null;
}
use of org.compiere.model.MInOutLine in project adempiere by adempiere.
the class Doc_MatchPO method loadDocumentDetails.
/**
* Load Specific Document Details
* @return error message or null
*/
protected String loadDocumentDetails() {
setC_Currency_ID(Doc.NO_CURRENCY);
MMatchPO matchPO = (MMatchPO) getPO();
setDateDoc(matchPO.getDateTrx());
//
m_M_AttributeSetInstance_ID = matchPO.getM_AttributeSetInstance_ID();
setQty(matchPO.getQty());
//
m_C_OrderLine_ID = matchPO.getC_OrderLine_ID();
m_oLine = new MOrderLine(getCtx(), m_C_OrderLine_ID, getTrxName());
//
m_M_InOutLine_ID = matchPO.getM_InOutLine_ID();
m_ioLine = new MInOutLine(getCtx(), m_M_InOutLine_ID, getTrxName());
m_C_InvoiceLine_ID = matchPO.getC_InvoiceLine_ID();
//
m_pc = new ProductCost(Env.getCtx(), getM_Product_ID(), m_M_AttributeSetInstance_ID, getTrxName());
m_pc.setQty(getQty());
return null;
}
Aggregations