|
|
|
|
@ -10,200 +10,139 @@ import org.adempiere.base.event.IEventTopics;
|
|
|
|
|
import org.adempiere.exceptions.AdempiereException;
|
|
|
|
|
import org.compiere.model.MAttributeSet;
|
|
|
|
|
import org.compiere.model.MAttributeSetInstance;
|
|
|
|
|
import org.compiere.model.MDocType;
|
|
|
|
|
import org.compiere.model.MInOut;
|
|
|
|
|
import org.compiere.model.MInOutLine;
|
|
|
|
|
import org.compiere.model.MInvoice;
|
|
|
|
|
import org.compiere.model.MInvoiceLine;
|
|
|
|
|
import org.compiere.model.MOrder;
|
|
|
|
|
import org.compiere.model.MOrderLine;
|
|
|
|
|
import org.compiere.model.MStorageOnHand;
|
|
|
|
|
import org.compiere.model.MWarehouse;
|
|
|
|
|
import org.compiere.model.PO;
|
|
|
|
|
import org.compiere.model.Query;
|
|
|
|
|
import org.compiere.process.DocAction;
|
|
|
|
|
import org.compiere.util.DB;
|
|
|
|
|
import org.osgi.service.event.Event;
|
|
|
|
|
|
|
|
|
|
import andromedia.midsuit.model.X_C_OrderLineMA;
|
|
|
|
|
import andromedia.midsuit.model.X_MID_UploadTPBLine;
|
|
|
|
|
|
|
|
|
|
public class MID_OrderValidator {
|
|
|
|
|
final static String EMPTY_STRING = "";
|
|
|
|
|
static String sql = "";
|
|
|
|
|
|
|
|
|
|
public static String executeEvent(Event e, PO po) {
|
|
|
|
|
MOrder Order = (MOrder) po;
|
|
|
|
|
|
|
|
|
|
// if (e.getTopic().equals(IEventTopics.DOC_AFTER_COMPLETE))
|
|
|
|
|
// return afterComplete(Order);
|
|
|
|
|
|
|
|
|
|
if (e.getTopic().equals(IEventTopics.DOC_BEFORE_COMPLETE))
|
|
|
|
|
return beforeComplete(Order);
|
|
|
|
|
else if (e.getTopic().equals(IEventTopics.PO_BEFORE_CHANGE))
|
|
|
|
|
return beforeChange(Order);
|
|
|
|
|
else if (e.getTopic().equals(IEventTopics.DOC_BEFORE_PREPARE))
|
|
|
|
|
return beforePrepare(Order);
|
|
|
|
|
return "";
|
|
|
|
|
return beforePrepare(Order);
|
|
|
|
|
else if (e.getTopic().equals(IEventTopics.DOC_BEFORE_VOID))
|
|
|
|
|
return beforeVoid(Order);
|
|
|
|
|
return EMPTY_STRING;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static String beforeVoid(MOrder order) {
|
|
|
|
|
|
|
|
|
|
if (!order.isSOTrx() && order.get_ValueAsInt("MID_AJUDocumentType_ID") > 0) {
|
|
|
|
|
for (MOrderLine line : order.getLines()) {
|
|
|
|
|
line.set_ValueNoCheck("M_Requisition_ID", null);
|
|
|
|
|
line.set_ValueNoCheck("M_RequisitionLine_ID", null);
|
|
|
|
|
line.saveEx();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sql = "UPDATE C_OrderLine SET NoAju = null WHERE C_Order_ID = ?";
|
|
|
|
|
DB.executeUpdateEx(sql, new Object[] { order.get_ID() }, order.get_TrxName());
|
|
|
|
|
|
|
|
|
|
sql = "UPDATE C_Order SET NoAju1 = null, RegisterDate = null, RegisterNo = null, MID_AjuDocumentType_ID = null WHERE C_Order_ID =?";
|
|
|
|
|
DB.executeUpdateEx(sql, new Object[] { order.get_ID() }, order.get_TrxName());
|
|
|
|
|
|
|
|
|
|
sql = "SELECT MID_UploadTPBLine_ID FROM MID_UploadTPBLine WHERE C_Order_ID =?";
|
|
|
|
|
int tpbID = DB.getSQLValue(order.get_TrxName(), sql, new Object[] { order.get_ID() });
|
|
|
|
|
if (tpbID > 0) {
|
|
|
|
|
X_MID_UploadTPBLine tpbLine = new X_MID_UploadTPBLine(order.getCtx(), tpbID, order.get_TrxName());
|
|
|
|
|
tpbLine.setProcessed(false);
|
|
|
|
|
tpbLine.setC_Order_ID(0);
|
|
|
|
|
tpbLine.saveEx();
|
|
|
|
|
sql = "UPDATE MID_UploadTPB SET Processed='N' WHERE MID_UploadTPB_ID =?";
|
|
|
|
|
DB.executeUpdateEx(sql, new Object[] { tpbLine.getMID_UploadTPB_ID() }, order.get_TrxName());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
return EMPTY_STRING;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static String beforePrepare(MOrder order) {
|
|
|
|
|
for(MOrderLine line : order.getLines()) {
|
|
|
|
|
int countAttLines = new Query(order.getCtx(), X_C_OrderLineMA.Table_Name, "C_OrderLine_ID =?", order.get_TrxName())
|
|
|
|
|
.setParameters(new Object[] { line.getC_OrderLine_ID() })
|
|
|
|
|
.setOnlyActiveRecords(true)
|
|
|
|
|
.count();
|
|
|
|
|
if(countAttLines>0){
|
|
|
|
|
BigDecimal totalQtyOrdered = DB.getSQLValueBD(order.get_TrxName(), "SELECT COALESCE(SUM(MovementQty),0) FROM C_OrderLineMA WHERE C_OrderLine_ID =?", new Object[] { line.getC_OrderLine_ID() });
|
|
|
|
|
if(line.getQtyOrdered().compareTo(totalQtyOrdered)!=0)
|
|
|
|
|
throw new AdempiereException("Cek kembali jumlah lot untuk produk "+line.getM_Product().getName());
|
|
|
|
|
for (MOrderLine line : order.getLines()) {
|
|
|
|
|
int countAttLines = new Query(order.getCtx(), X_C_OrderLineMA.Table_Name, "C_OrderLine_ID =?",
|
|
|
|
|
order.get_TrxName()).setParameters(new Object[] { line.getC_OrderLine_ID() })
|
|
|
|
|
.setOnlyActiveRecords(true).count();
|
|
|
|
|
if (countAttLines > 0) {
|
|
|
|
|
BigDecimal totalQtyOrdered = DB.getSQLValueBD(order.get_TrxName(),
|
|
|
|
|
"SELECT COALESCE(SUM(MovementQty),0) FROM C_OrderLineMA WHERE C_OrderLine_ID =?",
|
|
|
|
|
new Object[] { line.getC_OrderLine_ID() });
|
|
|
|
|
if (line.getQtyOrdered().compareTo(totalQtyOrdered) != 0)
|
|
|
|
|
throw new AdempiereException(
|
|
|
|
|
"Cek kembali jumlah lot untuk produk " + line.getM_Product().getName());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (order.isSOTrx()) return "";
|
|
|
|
|
if (order.isSOTrx())
|
|
|
|
|
return EMPTY_STRING;
|
|
|
|
|
MOrderLine[] lines = order.getLines();
|
|
|
|
|
if(order.get_ValueAsInt("MID_AJUDocumentType_ID")==0)
|
|
|
|
|
return "";
|
|
|
|
|
if (order.get_ValueAsInt("MID_AJUDocumentType_ID") == 0)
|
|
|
|
|
return EMPTY_STRING;
|
|
|
|
|
|
|
|
|
|
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
|
|
|
|
|
Timestamp regDate = (Timestamp) order.get_Value("RegisterDate");
|
|
|
|
|
|
|
|
|
|
int M_AttributeSetInstance_ID = DB.getSQLValue(order.get_TrxName(), "SELECT M_AttributeSetInstance_ID FROM M_AttributeSetInstance WHERE SerNo=?", new Object[] { order.get_ValueAsString("RegisterNo")+"-"+dateFormat.format(new Date(regDate.getTime())) });
|
|
|
|
|
int M_AttributeSet_ID = DB.getSQLValue(order.get_TrxName(), "SELECT M_AttributeSet_ID FROM M_AttributeSet WHERE IsActive=? AND AD_Client_ID =? ", new Object[] { true, order.getAD_Client_ID() });
|
|
|
|
|
|
|
|
|
|
int M_AttributeSetInstance_ID = DB.getSQLValue(order.get_TrxName(),
|
|
|
|
|
"SELECT M_AttributeSetInstance_ID FROM M_AttributeSetInstance WHERE SerNo=?", new Object[] {
|
|
|
|
|
order.get_ValueAsString("RegisterNo") + "-" + dateFormat.format(new Date(regDate.getTime())) });
|
|
|
|
|
int M_AttributeSet_ID = DB.getSQLValue(order.get_TrxName(),
|
|
|
|
|
"SELECT M_AttributeSet_ID FROM M_AttributeSet WHERE IsActive=? AND AD_Client_ID =? ",
|
|
|
|
|
new Object[] { true, order.getAD_Client_ID() });
|
|
|
|
|
MAttributeSet aSet = new MAttributeSet(order.getCtx(), M_AttributeSet_ID, order.get_TrxName());
|
|
|
|
|
if(M_AttributeSetInstance_ID <=0) {
|
|
|
|
|
if (M_AttributeSetInstance_ID <= 0) {
|
|
|
|
|
MAttributeSetInstance ASI = new MAttributeSetInstance(order.getCtx(), 0, order.get_TrxName());
|
|
|
|
|
ASI.setSerNo(order.get_ValueAsString("RegisterNo")+"-"+dateFormat.format(new Date(regDate.getTime())));
|
|
|
|
|
ASI.setSerNo(order.get_ValueAsString("RegisterNo") + "-" + dateFormat.format(new Date(regDate.getTime())));
|
|
|
|
|
ASI.setMAttributeSet(aSet);
|
|
|
|
|
|
|
|
|
|
ASI.setDescription();
|
|
|
|
|
ASI.saveEx();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
M_AttributeSetInstance_ID = ASI.get_ID();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for(MOrderLine line : lines) {
|
|
|
|
|
|
|
|
|
|
for (MOrderLine line : lines) {
|
|
|
|
|
String noAju = line.get_ValueAsString("NoAju");
|
|
|
|
|
if(order.get_ValueAsInt("MID_AJUDocumentType_ID")>0 && noAju==null)
|
|
|
|
|
if (order.get_ValueAsInt("MID_AJUDocumentType_ID") > 0 && noAju == null)
|
|
|
|
|
return "Silahkan isi dokumen AJU terlebih dahulu !!!";
|
|
|
|
|
List<X_C_OrderLineMA> attLines = new Query(order.getCtx(), X_C_OrderLineMA.Table_Name, "C_OrderLine_ID =?", order.get_TrxName())
|
|
|
|
|
.setParameters(new Object[] { line.getC_OrderLine_ID() })
|
|
|
|
|
.setOnlyActiveRecords(true)
|
|
|
|
|
.list();
|
|
|
|
|
if(attLines.size()<=0) {
|
|
|
|
|
line.setM_AttributeSetInstance_ID(M_AttributeSetInstance_ID);
|
|
|
|
|
line.saveEx();
|
|
|
|
|
}else
|
|
|
|
|
for(X_C_OrderLineMA attLine : attLines) {
|
|
|
|
|
attLine.setM_AttributeSetInstance_ID(M_AttributeSetInstance_ID);
|
|
|
|
|
attLine.saveEx();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static String afterComplete(MOrder Order){
|
|
|
|
|
if (Order.isSOTrx()) return "";
|
|
|
|
|
MDocType docTypeOfOrder = (MDocType) Order.getC_DocType();
|
|
|
|
|
if(docTypeOfOrder.get_ValueAsBoolean("isPOS")){
|
|
|
|
|
//Create Shipment
|
|
|
|
|
MInOut Shipment = new MInOut(Order, docTypeOfOrder.getC_DocTypeShipment_ID(), Order.getDateOrdered());
|
|
|
|
|
Shipment.saveEx();
|
|
|
|
|
|
|
|
|
|
for(MOrderLine OrderLine : Order.getLines()){
|
|
|
|
|
MInOutLine ShipmentLine = new MInOutLine(Shipment);
|
|
|
|
|
// Qty = Ordered - Delivered
|
|
|
|
|
BigDecimal MovementQty = OrderLine.getQtyOrdered().subtract(OrderLine.getQtyDelivered());
|
|
|
|
|
// Location
|
|
|
|
|
int M_Locator_ID = MStorageOnHand.getM_Locator_ID (OrderLine.getM_Warehouse_ID(),
|
|
|
|
|
OrderLine.getM_Product_ID(), OrderLine.getM_AttributeSetInstance_ID(),
|
|
|
|
|
MovementQty, OrderLine.get_TrxName());
|
|
|
|
|
if (M_Locator_ID == 0) // Get default Location
|
|
|
|
|
{
|
|
|
|
|
MWarehouse wh = MWarehouse.get(OrderLine.getCtx(), OrderLine.getM_Warehouse_ID());
|
|
|
|
|
M_Locator_ID = wh.getDefaultLocator().getM_Locator_ID();
|
|
|
|
|
List<X_C_OrderLineMA> attLines = new Query(order.getCtx(), X_C_OrderLineMA.Table_Name, "C_OrderLine_ID =?",
|
|
|
|
|
order.get_TrxName()).setParameters(new Object[] { line.getC_OrderLine_ID() })
|
|
|
|
|
.setOnlyActiveRecords(true).list();
|
|
|
|
|
if (attLines.size() <= 0) {
|
|
|
|
|
line.setM_AttributeSetInstance_ID(M_AttributeSetInstance_ID);
|
|
|
|
|
line.saveEx();
|
|
|
|
|
} else
|
|
|
|
|
for (X_C_OrderLineMA attLine : attLines) {
|
|
|
|
|
attLine.setM_AttributeSetInstance_ID(M_AttributeSetInstance_ID);
|
|
|
|
|
attLine.saveEx();
|
|
|
|
|
}
|
|
|
|
|
//
|
|
|
|
|
ShipmentLine.setOrderLine(OrderLine, M_Locator_ID, MovementQty);
|
|
|
|
|
ShipmentLine.setQty(MovementQty);
|
|
|
|
|
if (OrderLine.getQtyEntered().compareTo(OrderLine.getQtyOrdered()) != 0)
|
|
|
|
|
OrderLine.setQtyEntered(MovementQty
|
|
|
|
|
.multiply(OrderLine.getQtyEntered())
|
|
|
|
|
.divide(OrderLine.getQtyOrdered(), 6, BigDecimal.ROUND_HALF_UP));
|
|
|
|
|
ShipmentLine.saveEx();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!Shipment.processIt(DocAction.ACTION_Complete))
|
|
|
|
|
throw new AdempiereException("Failed when processing document - " + Shipment.getProcessMsg());
|
|
|
|
|
Shipment.saveEx();
|
|
|
|
|
|
|
|
|
|
//Create Invoice
|
|
|
|
|
MInvoice invoice = new MInvoice (Order, docTypeOfOrder.getC_DocTypeInvoice_ID(), Order.getDateOrdered());
|
|
|
|
|
invoice.setPaymentRule(Order.getPaymentRule());
|
|
|
|
|
invoice.saveEx();
|
|
|
|
|
|
|
|
|
|
// If we have a Shipment - use that as a base
|
|
|
|
|
if (Shipment != null)
|
|
|
|
|
{
|
|
|
|
|
MInOutLine[] sLines = Shipment.getLines(true);
|
|
|
|
|
for (int i = 0; i < sLines.length; i++)
|
|
|
|
|
{
|
|
|
|
|
MInOutLine sLine = sLines[i];
|
|
|
|
|
//
|
|
|
|
|
MInvoiceLine iLine = new MInvoiceLine(invoice);
|
|
|
|
|
iLine.setShipLine(sLine);
|
|
|
|
|
// Qty = Delivered
|
|
|
|
|
if (sLine.sameOrderLineUOM())
|
|
|
|
|
iLine.setQtyEntered(sLine.getQtyEntered());
|
|
|
|
|
else
|
|
|
|
|
iLine.setQtyEntered(sLine.getMovementQty());
|
|
|
|
|
iLine.setQtyInvoiced(sLine.getMovementQty());
|
|
|
|
|
iLine.saveEx();
|
|
|
|
|
sLine.setIsInvoiced(true);
|
|
|
|
|
sLine.saveEx();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!invoice.processIt(DocAction.ACTION_Complete))
|
|
|
|
|
throw new AdempiereException("Failed when processing document - " + invoice.getProcessMsg());
|
|
|
|
|
invoice.saveEx();
|
|
|
|
|
|
|
|
|
|
Order.setC_CashLine_ID(invoice.getC_CashLine_ID());
|
|
|
|
|
Order.saveEx();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// MPayment payment = new MPayment(Order.getCtx(), 0, Order.get_TrxName());
|
|
|
|
|
// payment.setAD_Org_ID(Order.getAD_Org_ID());
|
|
|
|
|
//
|
|
|
|
|
// payment.setTenderType(MPayment.TENDERTYPE_Cash);
|
|
|
|
|
// payment.setC_BankAccount_ID(ba.getC_BankAccount_ID());
|
|
|
|
|
// payment.setIsPrepayment(false);
|
|
|
|
|
//
|
|
|
|
|
// payment.setDateAcct(Order.getDateAcct());
|
|
|
|
|
// payment.setDateTrx(Order.getDateOrdered());
|
|
|
|
|
// //
|
|
|
|
|
// payment.setC_BPartner_ID(Order.getC_BPartner_ID());
|
|
|
|
|
// payment.setC_Invoice_ID(invoice.getC_Invoice_ID());
|
|
|
|
|
// payment.setC_DocType_ID(119);
|
|
|
|
|
// payment.setC_Currency_ID(Order.getC_Currency_ID());
|
|
|
|
|
//
|
|
|
|
|
// payment.setPayAmt(invoice.getGrandTotal());
|
|
|
|
|
// // Save payment
|
|
|
|
|
// payment.saveEx();
|
|
|
|
|
// payment.processIt(DocAction.ACTION_Complete);
|
|
|
|
|
// payment.saveEx();
|
|
|
|
|
}
|
|
|
|
|
return "";
|
|
|
|
|
return EMPTY_STRING;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static String beforeChange(MOrder order) {
|
|
|
|
|
if (order.isSOTrx()) return "";
|
|
|
|
|
if(order.get_ValueAsString("NoAju1").equals("")) {
|
|
|
|
|
DB.executeUpdateEx("UPDATE C_OrderLine SET NoAju = ? WHERE C_Order_ID = ?",
|
|
|
|
|
if (order.isSOTrx())
|
|
|
|
|
return EMPTY_STRING;
|
|
|
|
|
if (order.get_ValueAsString("NoAju1").equals("")) {
|
|
|
|
|
DB.executeUpdateEx("UPDATE C_OrderLine SET NoAju = ? WHERE C_Order_ID = ?",
|
|
|
|
|
new Object[] { "", order.getC_Order_ID() }, order.get_TrxName());
|
|
|
|
|
}
|
|
|
|
|
else if(order.is_ValueChanged("NoAju1")) {
|
|
|
|
|
} else if (order.is_ValueChanged("NoAju1")) {
|
|
|
|
|
String noAju = order.get_ValueAsString("NoAju1");
|
|
|
|
|
DB.executeUpdateEx("UPDATE C_OrderLine SET NoAju = ? WHERE C_Order_ID = ?",
|
|
|
|
|
DB.executeUpdateEx("UPDATE C_OrderLine SET NoAju = ? WHERE C_Order_ID = ?",
|
|
|
|
|
new Object[] { noAju, order.getC_Order_ID() }, order.get_TrxName());
|
|
|
|
|
}
|
|
|
|
|
return "";
|
|
|
|
|
return EMPTY_STRING;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static String beforeComplete(MOrder Order) {
|
|
|
|
|
return "";
|
|
|
|
|
return EMPTY_STRING;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|