📄 minvoice.java
字号:
}
info.append("@C_Cash_ID@: " + cash.getName() + " #" + cl.getLine());
setC_CashLine_ID(cl.getC_CashLine_ID());
} // CashBook
// Update Order & Match
int matchInv = 0;
int matchPO = 0;
MInvoiceLine[] lines = getLines(false);
for (int i = 0; i < lines.length; i++)
{
MInvoiceLine line = lines[i];
// Update Order Line
MOrderLine ol = null;
if (line.getC_OrderLine_ID() != 0)
{
if (isSOTrx()
|| line.getM_Product_ID() == 0)
{
ol = new MOrderLine (getCtx(), line.getC_OrderLine_ID(), get_TrxName());
if (line.getQtyInvoiced() != null)
ol.setQtyInvoiced(ol.getQtyInvoiced().add(line.getQtyInvoiced()));
if (!ol.save(get_TrxName()))
{
m_processMsg = "Could not update Order Line";
return DocAction.STATUS_Invalid;
}
}
// Order Invoiced Qty updated via Matching Inv-PO
else if (!isSOTrx()
&& line.getM_Product_ID() != 0
&& !isReversal())
{
// MatchPO is created also from MInOut when Invoice exists before Shipment
BigDecimal matchQty = line.getQtyInvoiced();
MMatchPO po = MMatchPO.create (line, null,
getDateInvoiced(), matchQty);
if (!po.save(get_TrxName()))
{
m_processMsg = "Could not create PO Matching";
return DocAction.STATUS_Invalid;
}
else
matchPO++;
}
}
// Matching - Inv-Shipment
if (!isSOTrx()
&& line.getM_InOutLine_ID() != 0
&& line.getM_Product_ID() != 0
&& !isReversal())
{
BigDecimal matchQty = line.getQtyInvoiced();
MMatchInv inv = new MMatchInv(line, getDateInvoiced(), matchQty);
if (!inv.save(get_TrxName()))
{
m_processMsg = "Could not create Invoice Matching";
return DocAction.STATUS_Invalid;
}
else
matchInv++;
}
} // for all lines
if (matchInv > 0)
info.append(" @M_MatchInv_ID@#").append(matchInv).append(" ");
if (matchPO > 0)
info.append(" @M_MatchPO_ID@#").append(matchPO).append(" ");
// Update BP Statistics
MBPartner bp = new MBPartner (getCtx(), getC_BPartner_ID(), get_TrxName());
// Update total revenue and balance / credit limit (reversed on AllocationLine.processIt)
BigDecimal invAmt = MConversionRate.convertBase(getCtx(), getGrandTotal(true), // CM adjusted
getC_Currency_ID(), getDateAcct(), 0, getAD_Client_ID(), getAD_Org_ID());
// Total Balance
BigDecimal newBalance = bp.getTotalOpenBalance(false);
if (newBalance == null)
newBalance = Env.ZERO;
if (isSOTrx())
{
newBalance = newBalance.add(invAmt);
//
if (bp.getFirstSale() == null)
bp.setFirstSale(getDateInvoiced());
BigDecimal newLifeAmt = bp.getActualLifeTimeValue();
if (newLifeAmt == null)
newLifeAmt = invAmt;
else
newLifeAmt = newLifeAmt.add(invAmt);
BigDecimal newCreditAmt = bp.getSO_CreditUsed();
if (newCreditAmt == null)
newCreditAmt = invAmt;
else
newCreditAmt = newCreditAmt.add(invAmt);
//
log.fine("GrandTotal=" + getGrandTotal(true) + "(" + invAmt
+ ") BP Life=" + bp.getActualLifeTimeValue() + "->" + newLifeAmt
+ ", Credit=" + bp.getSO_CreditUsed() + "->" + newCreditAmt
+ ", Balance=" + bp.getTotalOpenBalance(false) + " -> " + newBalance);
bp.setActualLifeTimeValue(newLifeAmt);
bp.setSO_CreditUsed(newCreditAmt);
} // SO
else
{
newBalance = newBalance.subtract(invAmt);
log.fine("GrandTotal=" + getGrandTotal(true) + "(" + invAmt
+ ") Balance=" + bp.getTotalOpenBalance(false) + " -> " + newBalance);
}
bp.setTotalOpenBalance(newBalance);
bp.setSOCreditStatus();
if (!bp.save(get_TrxName()))
{
m_processMsg = "Could not update Business Partner";
return DocAction.STATUS_Invalid;
}
// User - Last Result/Contact
if (getAD_User_ID() != 0)
{
MUser user = new MUser (getCtx(), getAD_User_ID(), get_TrxName());
user.setLastContact(new Timestamp(System.currentTimeMillis()));
user.setLastResult(Msg.translate(getCtx(), "C_Invoice_ID") + ": " + getDocumentNo());
if (!user.save(get_TrxName()))
{
m_processMsg = "Could not update Business Partner User";
return DocAction.STATUS_Invalid;
}
} // user
// Update Project
if (isSOTrx() && getC_Project_ID() != 0)
{
MProject project = new MProject (getCtx(), getC_Project_ID(), get_TrxName());
BigDecimal amt = getGrandTotal(true);
int C_CurrencyTo_ID = project.getC_Currency_ID();
if (C_CurrencyTo_ID != getC_Currency_ID())
amt = MConversionRate.convert(getCtx(), amt, getC_Currency_ID(), C_CurrencyTo_ID,
getDateAcct(), 0, getAD_Client_ID(), getAD_Org_ID());
BigDecimal newAmt = project.getInvoicedAmt();
if (newAmt == null)
newAmt = amt;
else
newAmt = newAmt.add(amt);
log.fine("GrandTotal=" + getGrandTotal(true) + "(" + amt
+ ") Project " + project.getName()
+ " - Invoiced=" + project.getInvoicedAmt() + "->" + newAmt);
project.setInvoicedAmt(newAmt);
if (!project.save(get_TrxName()))
{
m_processMsg = "Could not update Project";
return DocAction.STATUS_Invalid;
}
} // project
// User Validation
String valid = ModelValidationEngine.get().fireDocValidate(this, ModelValidator.TIMING_AFTER_COMPLETE);
if (valid != null)
{
m_processMsg = valid;
return DocAction.STATUS_Invalid;
}
// Counter Documents
MInvoice counter = createCounterDoc();
if (counter != null)
info.append(" - @CounterDoc@: @C_Invoice_ID@=").append(counter.getDocumentNo());
m_processMsg = info.toString().trim();
setProcessed(true);
setDocAction(DOCACTION_Close);
return DocAction.STATUS_Completed;
} // completeIt
/**
* Create Counter Document
*/
private MInvoice createCounterDoc()
{
// Is this a counter doc ?
if (getRef_Invoice_ID() != 0)
return null;
// Org Must be linked to BPartner
MOrg org = MOrg.get(getCtx(), getAD_Org_ID());
int counterC_BPartner_ID = org.getLinkedC_BPartner_ID();
if (counterC_BPartner_ID == 0)
return null;
// Business Partner needs to be linked to Org
MBPartner bp = new MBPartner (getCtx(), getC_BPartner_ID(), null);
int counterAD_Org_ID = bp.getAD_OrgBP_ID_Int();
if (counterAD_Org_ID == 0)
return null;
MBPartner counterBP = new MBPartner (getCtx(), counterC_BPartner_ID, null);
MOrgInfo counterOrgInfo = MOrgInfo.get(getCtx(), counterAD_Org_ID);
log.info("Counter BP=" + counterBP.getName());
// Document Type
int C_DocTypeTarget_ID = 0;
MDocTypeCounter counterDT = MDocTypeCounter.getCounterDocType(getCtx(), getC_DocType_ID());
if (counterDT != null)
{
log.fine(counterDT.toString());
if (!counterDT.isCreateCounter() || !counterDT.isValid())
return null;
C_DocTypeTarget_ID = counterDT.getCounter_C_DocType_ID();
}
else // indirect
{
C_DocTypeTarget_ID = MDocTypeCounter.getCounterDocType_ID(getCtx(), getC_DocType_ID());
log.fine("Indirect C_DocTypeTarget_ID=" + C_DocTypeTarget_ID);
if (C_DocTypeTarget_ID <= 0)
return null;
}
// Deep Copy
MInvoice counter = copyFrom(this, getDateInvoiced(),
C_DocTypeTarget_ID, !isSOTrx(), true, get_TrxName(), true);
//
counter.setAD_Org_ID(counterAD_Org_ID);
// counter.setM_Warehouse_ID(counterOrgInfo.getM_Warehouse_ID());
//
counter.setBPartner(counterBP);
// Refernces (Should not be required
counter.setSalesRep_ID(getSalesRep_ID());
counter.save(get_TrxName());
// Update copied lines
MInvoiceLine[] counterLines = counter.getLines(true);
for (int i = 0; i < counterLines.length; i++)
{
MInvoiceLine counterLine = counterLines[i];
counterLine.setClientOrg(counter);
counterLine.setInvoice(counter); // copies header values (BP, etc.)
counterLine.setPrice();
counterLine.setTax();
//
counterLine.save(get_TrxName());
}
log.fine(counter.toString());
// Document Action
if (counterDT != null)
{
if (counterDT.getDocAction() != null)
{
counter.setDocAction(counterDT.getDocAction());
counter.processIt(counterDT.getDocAction());
counter.save(get_TrxName());
}
}
return counter;
} // createCounterDoc
/**
* Void Document.
* @return true if success
*/
public boolean voidIt()
{
log.info(toString());
if (DOCSTATUS_Closed.equals(getDocStatus())
|| DOCSTATUS_Reversed.equals(getDocStatus())
|| DOCSTATUS_Voided.equals(getDocStatus()))
{
m_processMsg = "Document Closed: " + getDocStatus();
setDocAction(DOCACTION_None);
return false;
}
// Not Processed
if (DOCSTATUS_Drafted.equals(getDocStatus())
|| DOCSTATUS_Invalid.equals(getDocStatus())
|| DOCSTATUS_InProgress.equals(getDocStatus())
|| DOCSTATUS_Approved.equals(getDocStatus())
|| DOCSTATUS_NotApproved.equals(getDocStatus()) )
{
// Set lines to 0
MInvoiceLine[] lines = getLines(false);
for (int i = 0; i < lines.length; i++)
{
MInvoiceLine line = lines[i];
BigDecimal old = line.getQtyInvoiced();
if (old.compareTo(Env.ZERO) != 0)
{
line.setQty(Env.ZERO);
line.setTaxAmt(Env.ZERO);
line.setLineNetAmt(Env.ZERO);
line.setLineTotalAmt(Env.ZERO);
line.addDescription(Msg.getMsg(getCtx(), "Voided") + " (" + old + ")");
// Unlink Shipment
if (line.getM_InOutLine_ID() != 0)
{
MInOutLine ioLine = new MInOutLine(getCtx(), line.getM_InOutLine_ID(), get_TrxName());
ioLine.setIsInvoiced(false);
ioLine.save(get_TrxName());
line.setM_InOutLine_ID(0);
}
line.save(get_TrxName());
}
}
addDescription(Msg.getMsg(getCtx(), "Voided"));
setIsPaid(true);
setC_Payment_ID(0);
}
else
{
return reverseCorrectIt();
}
setProcessed(true);
setDocAction(DOCACTION_None);
return true;
} // voidIt
/**
* Close Document.
* @return true if success
*/
public boolean closeIt()
{
log.info(toString());
setProcessed(true);
setDocAction(DOCACTION_None);
return true;
} // closeIt
/**
* Reverse Correction - same date
* @return true if success
*/
public boolean reverseCorrectIt()
{
log.info(toString());
MDocType dt = MDocType.get(getCtx(), getC_DocType_ID());
if (!MPeriod.isOpen(getCtx(), getDateAcct(), dt.getDocBaseType()))
{
m_processMsg = "@PeriodClosed@";
return false;
}
//
MAllocationHdr[] allocations = MAllocationHdr.getOfInvoice(getCtx(),
getC_Invoice_ID(), get_TrxName());
for (int i = 0; i < allocations.length; i++)
{
allocations[i].setDocAction(DocAction.ACTION_Reverse_Correct);
allocations[i].reverseCorrectIt();
allocations[i].save(get_TrxName());
}
// Reverse/Delete Matching
if (!isSOTrx())
{
MMatchInv[] mInv = MMatchInv.getInvoice(getCtx(), getC_Invoice_ID(), get_TrxName());
for (int i = 0; i < mInv.length; i++)
mInv[i].delete(true);
MMatchPO[] mPO = MMatchPO.getInvoice(getCtx(), getC_Invoice_ID(), get_TrxName());
for (int i = 0; i < mPO.length; i++)
{
if (mPO[i].getM_InOutLine_ID() == 0)
mPO[i].delete(true);
else
{
mPO[i].setC_InvoiceLine_ID(null);
mPO[i].save(get_TrxName());
}
}
}
//
load(get_TrxName()); // reload allocation reversal info
// Deep Copy
MInvoice reversal = copyFrom (this, getDateInvoiced(),
getC_DocType_ID(), isSOTrx(), false, get_TrxName(), true);
if (reversal == null)
{
m_processMsg = "Could not create Invoice Reversal";
return false;
}
reversal.setReversal(true);
// Reverse Line Qty
MInvoiceLine[] rLines = reversal.getLines(false);
for (int i = 0; i < rLines.length; i++)
{
MInvoiceLine rLine = rLines[i];
rLine.setQtyEntered(rLine.getQtyEntered().negate());
rLine.setQtyInvoiced(rLine.getQtyInvoiced().negate());
rLine.setLineNetAmt(rLine.getLineNetAmt().negate());
if (rLine.getTaxAmt() != null && rLine.getTaxAmt().compareTo(Env.ZERO) != 0)
rLine.setTaxAmt(rLine.getTaxAmt().negate());
if (rLine.getLineTotalAmt() != null && rLine.getLineTotalAmt().compareTo(Env.ZERO) != 0)
rLine.setLineTotalAmt(rLine.getLineTotalAmt().negate());
if (!rLine.save(get_TrxName()))
{
m_processMsg = "Could not correct Invoice Reversal Line";
return false;
}
}
reversal.setC_Order_ID(getC_Order_ID());
reversal.addDescription("{->" + getDocumentNo() + ")");
//
if (!reversal.processIt(DocAction.ACTION_Complete))
{
m_processMsg = "Reversal ERROR: " + reversal.getProcessMsg();
return false;
}
reversal.setC_Payment_ID(0);
reversal.setIsPaid(true);
reversal.closeIt();
reversal.setDocStatus(DOCSTATUS_Reversed);
reversal.setDocAction(DOCACTION_None);
reversal.save(get_TrxName());
m_processMsg = reversal.getDocumentNo();
//
addDescription("(" + reversal.getDocumentNo() + "<-)");
// Clean up Reversed (this)
MInvoiceLine[] iLines = getLines(false);
for (int i = 0; i < iLines.length; i++)
{
MInvoiceLine iLine = iLines[i];
if (iLine.getM_InOutLine_ID() != 0)
{
MInOutLine ioLine = new MInOutLine(getCtx(), iLine.getM_InOutLine_ID(), get_TrxName());
ioLine.setIsInvoiced(false);
ioLine.save(get_TrxName());
// Reconsiliation
iLine.setM_InOutLine_ID(0);
iLine.save(get_TrxName());
}
}
setProcessed(true);
setDocStatus(DOCSTATUS_Reversed); // may come from void
setDocAction(DOCACTION_None);
setC_Payment_ID(0);
setIsPaid(true);
// Create Allocation
MAllocationHdr alloc = new MAllocationHdr(getCtx(), false, getDateAcct(),
getC_Currency_ID(),
Msg.translate(getCtx(), "C_Invoice_ID") + ": " + getDocumentNo() + "/" + reversal.getDocumentNo(),
get_TrxName());
alloc.setAD_Org_ID(getAD_Org_ID());
if (alloc.save())
{
// Amount
BigDecimal gt = getGrandTotal(true);
if (!isSOTrx())
gt = gt.negate();
// Orig Line
MAllocationLine aLine = new MAllocationLine (alloc, gt,
Env.ZERO, Env.ZERO, Env.ZERO);
aLine.setC_Invoice_ID(getC_Invoice_ID());
aLine.save();
// Reversal Line
MAllocationLine rLine = new MAllocationLine (alloc, gt.negate(),
Env.ZERO, Env.ZERO, Env.ZERO);
rLine.setC_Invoice_ID(reversal.getC_Invoice_ID());
rLine.save();
// Process It
if (alloc.processIt(DocAction.ACTION_Complete))
alloc.save();
}
return true;
} // reverseCorrectIt
/**
* Reverse Accrual - none
* @return false
*/
public boolean reverseAccrualIt()
{
log.info(toString());
return false;
} // reverseAccrualIt
/**
* Re-activate
* @return false
*/
public boolean reActivateIt()
{
log.info(toString());
return false;
} // reActivateIt
/*************************************************************************
* Get Summary
* @return Summary of Document
*/
public String getSummary()
{
StringBuffer sb = new StringBuffer();
sb.append(getDocumentNo());
// : Grand Total = 123.00 (#1)
sb.append(": ").
append(Msg.translate(getCtx(),"GrandTotal")).append("=").append(getGrandTotal())
.append(" (#").append(getLines(false).length).append(")");
// - Description
if (getDescription() != null && getDescription().length() > 0)
sb.append(" - ").append(getDescription());
return sb.toString();
} // getSummary
/**
* Get Process Message
* @return clear text error message
*/
public String getProcessMsg()
{
return m_processMsg;
} // getProcessMsg
/**
* Get Document Owner (Responsible)
* @return AD_User_ID
*/
public int getDoc_User_ID()
{
return getSalesRep_ID();
} // getDoc_User_ID
/**
* Get Document Approval Amount
* @return amount
*/
public BigDecimal getApprovalAmt()
{
return getGrandTotal();
} // getApprovalAmt
} // MInvoice
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -