📄 morder.java
字号:
} // getCurrencyISO
/**
* Get Currency Precision
* @return precision
*/
public int getPrecision()
{
return MCurrency.getStdPrecision(getCtx(), getC_Currency_ID());
} // getPrecision
/**
* Get Document Status
* @return Document Status Clear Text
*/
public String getDocStatusName()
{
return MRefList.getListName(getCtx(), 131, getDocStatus());
} // getDocStatusName
/**
* Set DocAction
* @param DocAction doc action
*/
public void setDocAction (String DocAction)
{
setDocAction (DocAction, false);
} // setDocAction
/**
* Set DocAction
* @param DocAction doc oction
* @param forceCreation force creation
*/
public void setDocAction (String DocAction, boolean forceCreation)
{
super.setDocAction (DocAction);
m_forceCreation = forceCreation;
} // setDocAction
/**
* Set Processed.
* Propergate to Lines/Taxes
* @param processed processed
*/
public void setProcessed (boolean processed)
{
super.setProcessed (processed);
if (get_ID() == 0)
return;
String set = "SET Processed='"
+ (processed ? "Y" : "N")
+ "' WHERE C_Order_ID=" + getC_Order_ID();
int noLine = DB.executeUpdate("UPDATE C_OrderLine " + set, get_TrxName());
int noTax = DB.executeUpdate("UPDATE C_OrderTax " + set, get_TrxName());
m_lines = null;
m_taxes = null;
log.fine("setProcessed - " + processed + " - Lines=" + noLine + ", Tax=" + noTax);
} // setProcessed
/**************************************************************************
* Before Save
* @param newRecord new
* @return save
*/
protected boolean beforeSave (boolean newRecord)
{
// Client/Org Check
if (getAD_Org_ID() == 0)
{
int context_AD_Org_ID = Env.getAD_Org_ID(getCtx());
if (context_AD_Org_ID != 0)
{
setAD_Org_ID(context_AD_Org_ID);
log.warning("Changed Org to Context=" + context_AD_Org_ID);
}
}
if (getAD_Client_ID() == 0)
{
m_processMsg = "AD_Client_ID = 0";
return false;
}
// New Record Doc Type - make sure DocType set to 0
if (newRecord && getC_DocType_ID() == 0)
setC_DocType_ID (0);
// Default Warehouse
if (getM_Warehouse_ID() == 0)
{
int ii = Env.getContextAsInt(getCtx(), "#M_Warehouse_ID");
if (ii != 0)
setM_Warehouse_ID(ii);
else
{
log.saveError("FillMandatory", Msg.getElement(getCtx(), "M_Warehouse_ID"));
return false;
}
}
// Warehouse Org
if (newRecord
|| is_ValueChanged("AD_Org_ID") || is_ValueChanged("M_Warehouse_ID"))
{
MWarehouse wh = MWarehouse.get(getCtx(), getM_Warehouse_ID());
if (wh.getAD_Org_ID() != getAD_Org_ID())
log.saveWarning("WarehouseOrgConflict", "");
}
// Reservations in Warehouse
if (!newRecord && is_ValueChanged("M_Warehouse_ID"))
{
MOrderLine[] lines = getLines(false,null);
for (int i = 0; i < lines.length; i++)
{
if (!lines[i].canChangeWarehouse())
return false;
}
}
// No Partner Info - set Template
if (getC_BPartner_ID() == 0)
setBPartner(MBPartner.getTemplate(getCtx(), getAD_Client_ID()));
if (getC_BPartner_Location_ID() == 0)
setBPartner(new MBPartner(getCtx(), getC_BPartner_ID(), null));
// No Bill - get from Ship
if (getBill_BPartner_ID() == 0)
{
setBill_BPartner_ID(getC_BPartner_ID());
setBill_Location_ID(getC_BPartner_Location_ID());
}
if (getBill_Location_ID() == 0)
setBill_Location_ID(getC_BPartner_Location_ID());
// Default Price List
if (getM_PriceList_ID() == 0)
{
int ii = DB.getSQLValue(null,
"SELECT M_PriceList_ID FROM M_PriceList "
+ "WHERE AD_Client_ID=? AND IsSOPriceList=? "
+ "ORDER BY IsDefault DESC", getAD_Client_ID(), isSOTrx() ? "Y" : "N");
if (ii != 0)
setM_PriceList_ID (ii);
}
// Default Currency
if (getC_Currency_ID() == 0)
{
String sql = "SELECT C_Currency_ID FROM M_PriceList WHERE M_PriceList_ID=?";
int ii = DB.getSQLValue (null, sql, getM_PriceList_ID());
if (ii != 0)
setC_Currency_ID (ii);
else
setC_Currency_ID(Env.getContextAsInt(getCtx(), "#C_Currency_ID"));
}
// Default Sales Rep
if (getSalesRep_ID() == 0)
{
int ii = Env.getContextAsInt(getCtx(), "#SalesRep_ID");
if (ii != 0)
setSalesRep_ID (ii);
}
// Default Document Type
if (getC_DocTypeTarget_ID() == 0)
setC_DocTypeTarget_ID(DocSubTypeSO_Standard);
// Default Payment Term
if (getC_PaymentTerm_ID() == 0)
{
int ii = Env.getContextAsInt(getCtx(), "#C_PaymentTerm_ID");
if (ii != 0)
setC_PaymentTerm_ID(ii);
else
{
String sql = "SELECT C_PaymentTerm_ID FROM C_PaymentTerm WHERE AD_Client_ID=? AND IsDefault='Y'";
ii = DB.getSQLValue(null, sql, getAD_Client_ID());
if (ii != 0)
setC_PaymentTerm_ID (ii);
}
}
return true;
} // beforeSave
/**
* After Save
* @param newRecord new
* @param success success
*/
protected boolean afterSave (boolean newRecord, boolean success)
{
if (success) {
//
String sql = "UPDATE C_Order"
+ " SET SourceRecord_ID ="
+ getC_Order_ID()
+ " WHERE C_Order_ID=" + getC_Order_ID();
int no = DB.executeUpdate(sql, get_TrxName());
}
if (!success || newRecord)
return success;
// Propagate Description changes
if (is_ValueChanged("Description") || is_ValueChanged("POReference"))
{
String sql = "UPDATE C_Invoice i"
+ " SET (Description,POReference)="
+ "(SELECT Description,POReference "
+ "FROM C_Order o WHERE i.C_Order_ID=o.C_Order_ID) "
+ "WHERE DocStatus NOT IN ('RE','CL') AND C_Order_ID=" + getC_Order_ID();
int no = DB.executeUpdate(sql, get_TrxName());
log.fine("Description -> #" + no);
}
// Propagate Changes of Payment Info to existing (not reversed/closed) invoices
if (is_ValueChanged("PaymentRule") || is_ValueChanged("C_PaymentTerm_ID")
|| is_ValueChanged("DateAcct") || is_ValueChanged("C_Payment_ID")
|| is_ValueChanged("C_CashLine_ID"))
{
String sql = "UPDATE C_Invoice i "
+ "SET (PaymentRule,C_PaymentTerm_ID,DateAcct,C_Payment_ID,C_CashLine_ID)="
+ "(SELECT PaymentRule,C_PaymentTerm_ID,DateAcct,C_Payment_ID,C_CashLine_ID "
+ "FROM C_Order o WHERE i.C_Order_ID=o.C_Order_ID)"
+ "WHERE DocStatus NOT IN ('RE','CL') AND C_Order_ID=" + getC_Order_ID();
// Don't touch Closed/Reversed entries
int no = DB.executeUpdate(sql, get_TrxName());
log.fine("Payment -> #" + no);
}
// Sync Lines
afterSaveSync("AD_Org_ID");
afterSaveSync("C_BPartner_ID");
afterSaveSync("C_BPartner_Location_ID");
afterSaveSync("DateOrdered");
afterSaveSync("DatePromised");
afterSaveSync("M_Warehouse_ID");
afterSaveSync("M_Shipper_ID");
afterSaveSync("C_Currency_ID");
//
return true;
} // afterSave
private void afterSaveSync (String columnName)
{
if (is_ValueChanged(columnName))
{
String sql = "UPDATE C_OrderLine ol"
+ " SET " + columnName + " ="
+ "(SELECT " + columnName
+ " FROM C_Order o WHERE ol.C_Order_ID=o.C_Order_ID) "
+ "WHERE C_Order_ID=" + getC_Order_ID();
int no = DB.executeUpdate(sql, get_TrxName());
log.fine(columnName + " Lines -> #" + no);
}
} // afterSaveSync
/**
* Before Delete
* @return true of it can be deleted
*/
protected boolean beforeDelete ()
{
if (isProcessed())
return false;
getLines();
for (int i = 0; i < m_lines.length; i++)
{
if (!m_lines[i].beforeDelete())
return false;
}
return true;
} // beforeDelete
/**************************************************************************
* Process document
* @param processAction document action
* @return true if performed
*/
public boolean processIt (String processAction)
{
m_processMsg = null;
DocumentEngine engine = new DocumentEngine (this, getDocStatus());
return engine.processIt (processAction, getDocAction());
} // processIt
/** Process Message */
private String m_processMsg = null;
/** Just Prepared Flag */
private boolean m_justPrepared = false;
/**
* Unlock Document.
* @return true if success
*/
public boolean unlockIt()
{
log.info("unlockIt - " + toString());
setProcessing(false);
return true;
} // unlockIt
/**
* Invalidate Document
* @return true if success
*/
public boolean invalidateIt()
{
log.info(toString());
setDocAction(DOCACTION_Prepare);
return true;
} // invalidateIt
/**************************************************************************
* Prepare Document
* @return new status (In Progress or Invalid)
*/
public String prepareIt()
{
log.info(toString());
m_processMsg = ModelValidationEngine.get().fireDocValidate(this, ModelValidator.TIMING_BEFORE_PREPARE);
if (m_processMsg != null)
return DocAction.STATUS_Invalid;
MDocType dt = MDocType.get(getCtx(), getC_DocTypeTarget_ID());
// Std Period open?
if (!MPeriod.isOpen(getCtx(), getDateAcct(), dt.getDocBaseType()))
{
m_processMsg = "@PeriodClosed@";
return DocAction.STATUS_Invalid;
}
// Lines
MOrderLine[] lines = getLines(true, "M_Product_ID");
if (lines.length == 0)
{
m_processMsg = "@NoLines@";
return DocAction.STATUS_Invalid;
}
// Convert DocType to Target
if (getC_DocType_ID() != getC_DocTypeTarget_ID() )
{
// Cannot change Std to anything else if different warehouses
if (getC_DocType_ID() != 0)
{
MDocType dtOld = MDocType.get(getCtx(), getC_DocType_ID());
if (MDocType.DOCSUBTYPESO_StandardOrder.equals(dtOld.getDocSubTypeSO()) // From SO
&& !MDocType.DOCSUBTYPESO_StandardOrder.equals(dt.getDocSubTypeSO())) // To !SO
{
for (int i = 0; i < lines.length; i++)
{
if (lines[i].getM_Warehouse_ID() != getM_Warehouse_ID())
{
log.warning("different Warehouse " + lines[i]);
m_processMsg = "@CannotChangeDocType@";
return DocAction.STATUS_Invalid;
}
}
}
}
// New or in Progress/Invalid
if (DOCSTATUS_Drafted.equals(getDocStatus())
|| DOCSTATUS_InProgress.equals(getDocStatus())
|| DOCSTATUS_Invalid.equals(getDocStatus())
|| getC_DocType_ID() == 0)
{
setC_DocType_ID(getC_DocTypeTarget_ID());
}
else // convert only if offer
{
if (dt.isOffer())
setC_DocType_ID(getC_DocTypeTarget_ID());
else
{
m_processMsg = "@CannotChangeDocType@";
return DocAction.STATUS_Invalid;
}
}
} // convert DocType
// Mandatory Product Attribute Set Instance
String mandatoryType = "='Y'"; // IN ('Y','S')
String sql = "SELECT COUNT(*) "
+ "FROM C_OrderLine ol"
+ " INNER JOIN M_Product p ON (ol.M_Product_ID=p.M_Product_ID)"
+ " INNER JOIN M_AttributeSet pas ON (p.M_AttributeSet_ID=pas.M_AttributeSet_ID) "
+ "WHERE pas.MandatoryType" + mandatoryType
+ " AND ol.M_AttributeSetInstance_ID IS NULL"
+ " AND ol.C_Order_ID=?";
int no = DB.getSQLValue(get_TrxName(), sql, getC_Order_ID());
if (no != 0)
{
m_processMsg = "@LinesWithoutProductAttribute@ (" + no + ")";
return DocAction.STATUS_Invalid;
}
// Lines
explodeBOM();
if (!reserveStock(dt, lines))
{
m_processMsg = "Cannot reserve Stock";
return DocAction.STATUS_Invalid;
}
if (!calculateTaxTotal())
{
m_processMsg = "Error calculating tax";
return DocAction.STATUS_Invalid;
}
// Credit Check
if (isSOTrx())
{
MBPartner bp = new MBPartner (getCtx(), getC_BPartner_ID(), null);
if (MBPartner.SOCREDITSTATUS_CreditStop.equals(bp.getSOCreditStatus()))
{
m_processMsg = "@BPartnerCreditStop@ - @TotalOpenBalance@="
+ bp.getTotalOpenBalance()
+ ", @SO_CreditLimit@=" + bp.getSO_CreditLimit();
return DocAction.STATUS_Invalid;
}
if (MBPartner.SOCREDITSTATUS_CreditHold.equals(bp.getSOCreditStatus()))
{
m_processMsg = "@BPartnerCreditHold@ - @TotalOpenBalance@="
+ bp.getTotalOpenBalance()
+ ", @SO_CreditLimit@=" + bp.getSO_CreditLimit();
return DocAction.STATUS_Invalid;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -