MInvoice.java

Index Score
org.compiere.model
Compiere

View: Reasons, Metrics, Source Code

These are the metrics that contribute to the Enerjy Score for this file, ranked by impact. So the metrics listed at the top influence the score to a greater extent that the metrics listed at the bottom.

MetricDescription
EXEC_COMMENTSComments in executable code
LINE_COMMENTNumber of line comments
EXITSProcedure exits
JAVA0034JAVA0034 Missing braces in if statement
COMPARISONSNumber of comparison operators
CYCLOMATICCyclomatic complexity
LOGICAL_LINESNumber of statements
OPERATORSNumber of operators
ELOCEffective lines of code
PROGRAM_LENGTHHalstead program length
LOCLines of code
OPERANDSNumber of operands
SIZESize of the file in bytes
LINESNumber of lines in the source file
UNIQUE_OPERANDSNumber of unique operands
PROGRAM_VOCABHalstead program vocabulary
DECL_COMMENTSComments in declarations
JAVA0145JAVA0145 Tab character used in source file
COMMENTSComment lines
RETURNSNumber of return points from functions
BLOCKSNumber of blocks
LOOPSNumber of loops
DOC_COMMENTNumber of javadoc comment lines
INTERFACE_COMPLEXITYInterface complexity
JAVA0270JAVA0270 Use Java 5.0 enhanced for loop construct to iterate over all elements in an array
FUNCTIONSNumber of function declarations
PARAMSNumber of formal parameter declarations
JAVA0166JAVA0166 Generic exception caught
JAVA0174JAVA0174 Assigned local variable never used
JAVA0160JAVA0160 Method does not throw specified exception
JAVA0173JAVA0173 Unused method parameter
WHITESPACENumber of whitespace lines
JAVA0109JAVA0109 Incorrect javadoc: no parameter 'parameter'
JAVA0036JAVA0036 Missing braces in while statement
JAVA0111JAVA0111 Incorrect javadoc: @return tag for void method
JAVA0115JAVA0115 Incorrect javadoc: no @throws or @exception tag for 'exception'
JAVA0108JAVA0108 Incorrect javadoc: no @param tag for 'parameter'
JAVA0285JAVA0285 Dereference of potentially null variable
UNIQUE_OPERATORSNumber of unique operators
JAVA0259JAVA0259 Return of collection/array field
JAVA0008JAVA0008 Empty catch block
NEST_DEPTHMaximum nesting depth
JAVA0094JAVA0094 Field hides a superclass field
PROGRAM_VOLUMEHalstead program volume
JAVA0136JAVA0136 N methods defined in class (maximum: M)
JAVA0035JAVA0035 Missing braces in for statement
JAVA0126JAVA0126 Method declares unchecked exception in throws
JAVA0110JAVA0110 Incorrect javadoc: no @return tag
JAVA0064JAVA0064 N variations of identifier name (maximum: M)
JAVA0117JAVA0117 Missing javadoc: method 'method'
JAVA0003JAVA0003 Minimize use of on-demand (.*) imports
/****************************************************************************** * Product: Compiere ERP & CRM Smart Business Solution * * Copyright (C) 1999-2007 ComPiere, Inc. All Rights Reserved. * * This program is free software, you can redistribute it and/or modify it * * under the terms version 2 of the GNU General Public License as published * * by the Free Software Foundation. This program is distributed in the hope * * that it will be useful, but WITHOUT ANY WARRANTY, without even the implied * * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * * See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along * * with this program, if not, write to the Free Software Foundation, Inc., * * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. * * For the text or an alternative of this public license, you may reach us * * ComPiere, Inc., 3600 Bridge Parkway #102, Redwood City, CA 94065, USA * * or via info@compiere.org or http://www.compiere.org/license.html * *****************************************************************************/ package org.compiere.model; import java.io.*; import java.math.*; import java.sql.*; import java.util.*; import java.util.logging.*; import org.compiere.api.*; import org.compiere.framework.*; import org.compiere.print.*; import org.compiere.process.*; import org.compiere.util.*; /** * Invoice Model. * Please do not set DocStatus and C_DocType_ID directly. * They are set in the process() method. * Use DocAction and C_DocTypeTarget_ID instead. * * @author Jorg Janke * @version $Id: MInvoice.java,v 1.2 2006/07/30 00:51:02 jjanke Exp $ */ public class MInvoice extends X_C_Invoice implements DocAction { /** * */ private static final long serialVersionUID = 1L; /** * Get Payments Of BPartner * @param ctx context * @param C_BPartner_ID id * @param trxName transaction * @return array */ public static MInvoice[] getOfBPartner (Ctx ctx, int C_BPartner_ID, String trxName) { ArrayList<MInvoice> list = new ArrayList<MInvoice>(); String sql = "SELECT * FROM C_Invoice WHERE C_BPartner_ID=?"; PreparedStatement pstmt = null; try { pstmt = DB.prepareStatement(sql, trxName); pstmt.setInt(1, C_BPartner_ID); ResultSet rs = pstmt.executeQuery(); while (rs.next()) list.add(new MInvoice(ctx,rs, trxName)); rs.close(); pstmt.close(); pstmt = null; } catch (Exception e) { s_log.log(Level.SEVERE, sql, e); } try { if (pstmt != null) pstmt.close(); pstmt = null; } catch (Exception e) { pstmt = null; } // MInvoice[] retValue = new MInvoice[list.size()]; list.toArray(retValue); return retValue; } // getOfBPartner /** * Create new Invoice by copying * @param from invoice * @param dateDoc date of the document date * @param C_DocTypeTarget_ID target doc type * @param isSOTrx sales order * @param counter create counter links * @param trxName trx * @param setOrder set Order links * @return Invoice */ public static MInvoice copyFrom (MInvoice from, Timestamp dateDoc, int C_DocTypeTarget_ID, boolean counter, String trxName, boolean setOrder) { MInvoice to = new MInvoice (from.getCtx(), 0, null); to.set_TrxName(trxName); PO.copyValues (from, to, from.getAD_Client_ID(), from.getAD_Org_ID()); to.set_ValueNoCheck ("C_Invoice_ID", I_ZERO); to.set_ValueNoCheck ("DocumentNo", null); // to.setDocStatus (DOCSTATUS_Drafted); // Draft to.setDocAction(DOCACTION_Complete); // to.setC_DocType_ID(0); to.setC_DocTypeTarget_ID (C_DocTypeTarget_ID, true); // to.setDateInvoiced (dateDoc); to.setDateAcct (dateDoc); to.setDatePrinted(null); to.setIsPrinted (false); // to.setIsApproved (false); to.setC_Payment_ID(0); to.setC_CashLine_ID(0); to.setIsPaid (false); to.setIsInDispute(false); // // Amounts are updated by trigger when adding lines to.setGrandTotal(Env.ZERO); to.setTotalLines(Env.ZERO); // to.setIsTransferred (false); to.setPosted (false); to.setProcessed (false); // delete references to.setIsSelfService(false); if (!setOrder) to.setC_Order_ID(0); if (counter) { to.setRef_Invoice_ID(from.getC_Invoice_ID()); // Try to find Order link if (from.getC_Order_ID() != 0) { MOrder peer = new MOrder (from.getCtx(), from.getC_Order_ID(), from.get_TrxName()); if (peer.getRef_Order_ID() != 0) to.setC_Order_ID(peer.getRef_Order_ID()); } } else to.setRef_Invoice_ID(0); if (!to.save(trxName)) throw new IllegalStateException("Could not create Invoice"); if (counter) from.setRef_Invoice_ID(to.getC_Invoice_ID()); // Lines if (to.copyLinesFrom(from, counter, setOrder) == 0) throw new IllegalStateException("Could not create Invoice Lines"); return to; } // copyFrom /** * Get PDF File Name * @param documentDir directory * @param C_Invoice_ID invoice * @return file name */ public static String getPDFFileName (String documentDir, int C_Invoice_ID) { StringBuffer sb = new StringBuffer (documentDir); if (sb.length() == 0) sb.append("."); if (!sb.toString().endsWith(File.separator)) sb.append(File.separator); sb.append("C_Invoice_ID_") .append(C_Invoice_ID) .append(".pdf"); return sb.toString(); } // getPDFFileName /** * Get MInvoice from Cache * @param ctx context * @param C_Invoice_ID id * @return MInvoice */ public static MInvoice get (Ctx ctx, int C_Invoice_ID) { Integer key = new Integer (C_Invoice_ID); MInvoice retValue = (MInvoice) s_cache.get (ctx, key); if (retValue != null) return retValue; retValue = new MInvoice (ctx, C_Invoice_ID, null); if (retValue.get_ID () != 0) s_cache.put (key, retValue); return retValue; } // get /** Cache */ private static CCache<Integer,MInvoice> s_cache = new CCache<Integer,MInvoice>("C_Invoice", 20, 2); // 2 minutes /************************************************************************** * Invoice Constructor * @param ctx context * @param C_Invoice_ID invoice or 0 for new * @param trxName trx name */ public MInvoice (Ctx ctx, int C_Invoice_ID, String trxName) { super (ctx, C_Invoice_ID, trxName); if (C_Invoice_ID == 0) { setDocStatus (DOCSTATUS_Drafted); // Draft setDocAction (DOCACTION_Complete); // setPaymentRule(PAYMENTRULE_OnCredit); // Payment Terms setDateInvoiced (new Timestamp (System.currentTimeMillis ())); setDateAcct (new Timestamp (System.currentTimeMillis ())); // setChargeAmt (Env.ZERO); setTotalLines (Env.ZERO); setGrandTotal (Env.ZERO); // setIsSOTrx (true); setIsTaxIncluded (false); setIsApproved (false); setIsDiscountPrinted (false); setIsPaid (false); setSendEMail (false); setIsPrinted (false); setIsTransferred (false); setIsSelfService(false); setIsPayScheduleValid(false); setIsInDispute(false); setPosted(false); setIsReturnTrx(false); super.setProcessed (false); setProcessing(false); } } // MInvoice /** * Load Constructor * @param ctx context * @param rs result set record * @param trxName transaction */ public MInvoice (Ctx ctx, ResultSet rs, String trxName) { super(ctx, rs, trxName); } // MInvoice /** * Create Invoice from Order * @param order order * @param C_DocTypeTarget_ID target document type * @param invoiceDate date or null */ public MInvoice (MOrder order, int C_DocTypeTarget_ID, Timestamp invoiceDate) { this (order.getCtx(), 0, order.get_TrxName()); setClientOrg(order); setOrder(order); // set base settings // if (C_DocTypeTarget_ID == 0) C_DocTypeTarget_ID = DB.getSQLValue(null, "SELECT C_DocTypeInvoice_ID FROM C_DocType WHERE C_DocType_ID=?", order.getC_DocType_ID()); setC_DocTypeTarget_ID(C_DocTypeTarget_ID, true); if (invoiceDate != null) setDateInvoiced(invoiceDate); setDateAcct(getDateInvoiced()); // setSalesRep_ID(order.getSalesRep_ID()); // setC_BPartner_ID(order.getBill_BPartner_ID()); setC_BPartner_Location_ID(order.getBill_Location_ID()); setAD_User_ID(order.getBill_User_ID()); } // MInvoice /** * Create Invoice from Shipment * @param ship shipment * @param invoiceDate date or null */ public MInvoice (MInOut ship, Timestamp invoiceDate) { this (ship.getCtx(), 0, ship.get_TrxName()); setClientOrg(ship); setShipment(ship); // set base settings // setC_DocTypeTarget_ID(); if (invoiceDate != null) setDateInvoiced(invoiceDate); setDateAcct(getDateInvoiced()); // setSalesRep_ID(ship.getSalesRep_ID()); setAD_User_ID(ship.getAD_User_ID()); } // MInvoice /** * Create Invoice from Batch Line * @param batch batch * @param line batch line */ public MInvoice (MInvoiceBatch batch, MInvoiceBatchLine line) { this (line.getCtx(), 0, line.get_TrxName()); setClientOrg(line); setDocumentNo(line.getDocumentNo()); // setIsSOTrx(batch.isSOTrx()); MBPartner bp = new MBPartner (line.getCtx(), line.getC_BPartner_ID(), line.get_TrxName()); setBPartner(bp); // defaults // setIsTaxIncluded(line.isTaxIncluded()); // May conflict with default price list setC_Currency_ID(batch.getC_Currency_ID()); setC_ConversionType_ID(batch.getC_ConversionType_ID()); // // setPaymentRule(order.getPaymentRule()); // setC_PaymentTerm_ID(order.getC_PaymentTerm_ID()); // setPOReference(""); setDescription(batch.getDescription()); // setDateOrdered(order.getDateOrdered()); // setAD_OrgTrx_ID(line.getAD_OrgTrx_ID()); setC_Project_ID(line.getC_Project_ID()); // setC_Campaign_ID(line.getC_Campaign_ID()); setC_Activity_ID(line.getC_Activity_ID()); setUser1_ID(line.getUser1_ID()); setUser2_ID(line.getUser2_ID()); // setC_DocTypeTarget_ID(line.getC_DocType_ID(), true); setDateInvoiced(line.getDateInvoiced()); setDateAcct(line.getDateAcct()); // setSalesRep_ID(batch.getSalesRep_ID()); // setC_BPartner_ID(line.getC_BPartner_ID()); setC_BPartner_Location_ID(line.getC_BPartner_Location_ID()); setAD_User_ID(line.getAD_User_ID()); } // MInvoice /** Open Amount */ private BigDecimal m_openAmt = null; /** Invoice Lines */ private MInvoiceLine[] m_lines; /** Invoice Taxes */ private MInvoiceTax[] m_taxes; /** Logger */ private static CLogger s_log = CLogger.getCLogger(MInvoice.class); /** * Overwrite Client/Org if required * @param AD_Client_ID client * @param AD_Org_ID org */ public void setClientOrg (int AD_Client_ID, int AD_Org_ID) { super.setClientOrg(AD_Client_ID, AD_Org_ID); } // setClientOrg /** * Set Business Partner Defaults & Details * @param bp business partner */ public void setBPartner (MBPartner bp) { if (bp == null) return; setC_BPartner_ID(bp.getC_BPartner_ID()); // Set Defaults int ii = 0; if (isSOTrx()) ii = bp.getC_PaymentTerm_ID(); else ii = bp.getPO_PaymentTerm_ID(); if (ii != 0) setC_PaymentTerm_ID(ii); // if (isSOTrx()) ii = bp.getM_PriceList_ID(); else ii = bp.getPO_PriceList_ID(); if (ii != 0) setM_PriceList_ID(ii); // String ss = null; if (isSOTrx()) ss = bp.getPaymentRule(); else ss = bp.getPaymentRulePO(); if (ss != null) setPaymentRule(ss); // Set Locations MBPartnerLocation[] locs = bp.getLocations(false); if (locs != null) { for (int i = 0; i < locs.length; i++) { if ((locs[i].isBillTo() && isSOTrx()) || (locs[i].isPayFrom() && !isSOTrx())) setC_BPartner_Location_ID(locs[i].getC_BPartner_Location_ID()); } // set to first if (getC_BPartner_Location_ID() == 0 && locs.length > 0) setC_BPartner_Location_ID(locs[0].getC_BPartner_Location_ID()); } if (getC_BPartner_Location_ID() == 0) log.log(Level.SEVERE, "Has no To Address: " + bp); // Set Contact MUser[] contacts = bp.getContacts(false); if (contacts != null && contacts.length > 0) // get first User setAD_User_ID(contacts[0].getAD_User_ID()); // setC_Project_ID(0); } // setBPartner /** * Set Order References * @param order order */ public void setOrder (MOrder order) { if (order == null) return; setC_Order_ID(order.getC_Order_ID()); setIsSOTrx(order.isSOTrx()); setIsDiscountPrinted(order.isDiscountPrinted()); setIsSelfService(order.isSelfService()); setSendEMail(order.isSendEMail()); // setM_PriceList_ID(order.getM_PriceList_ID()); setIsTaxIncluded(order.isTaxIncluded()); setC_Currency_ID(order.getC_Currency_ID()); setC_ConversionType_ID(order.getC_ConversionType_ID()); // setPaymentRule(order.getPaymentRule()); setC_PaymentTerm_ID(order.getC_PaymentTerm_ID()); setPOReference(order.getPOReference()); setDescription(order.getDescription()); setDateOrdered(order.getDateOrdered()); setC_BP_BankAccount_ID(order.getC_BP_BankAccount_ID()); // setAD_OrgTrx_ID(order.getAD_OrgTrx_ID()); setC_Project_ID(order.getC_Project_ID()); setC_Campaign_ID(order.getC_Campaign_ID()); setC_Activity_ID(order.getC_Activity_ID()); setUser1_ID(order.getUser1_ID()); setUser2_ID(order.getUser2_ID()); } // setOrder /** * Set Shipment References * @param ship shipment */ public void setShipment (MInOut ship) { if (ship == null) return; setIsSOTrx(ship.isSOTrx()); // MBPartner bp = new MBPartner (getCtx(), ship.getC_BPartner_ID(), null); setBPartner (bp); // setSendEMail(ship.isSendEMail()); // setPOReference(ship.getPOReference()); setDescription(ship.getDescription()); setDateOrdered(ship.getDateOrdered()); // setAD_OrgTrx_ID(ship.getAD_OrgTrx_ID()); setC_Project_ID(ship.getC_Project_ID()); setC_Campaign_ID(ship.getC_Campaign_ID()); setC_Activity_ID(ship.getC_Activity_ID()); setUser1_ID(ship.getUser1_ID()); setUser2_ID(ship.getUser2_ID()); // if (ship.getC_Order_ID() != 0) { setC_Order_ID(ship.getC_Order_ID()); MOrder order = new MOrder (getCtx(), ship.getC_Order_ID(), get_TrxName()); setIsDiscountPrinted(order.isDiscountPrinted()); setM_PriceList_ID(order.getM_PriceList_ID()); setIsTaxIncluded(order.isTaxIncluded()); setC_Currency_ID(order.getC_Currency_ID()); setC_ConversionType_ID(order.getC_ConversionType_ID()); setPaymentRule(order.getPaymentRule()); setC_PaymentTerm_ID(order.getC_PaymentTerm_ID()); // MDocType dt = MDocType.get(getCtx(), order.getC_DocType_ID()); if (dt.getC_DocTypeInvoice_ID() != 0) setC_DocTypeTarget_ID(dt.getC_DocTypeInvoice_ID(), true); // Overwrite Invoice Address setC_BPartner_Location_ID(order.getBill_Location_ID()); } } // setShipment /** * Set Target Document Type * @param DocBaseType doc type MDocBaseType.DOCBASETYPE_ */ public void setC_DocTypeTarget_ID (String DocBaseType) { String sql = "SELECT C_DocType_ID FROM C_DocType " + "WHERE AD_Client_ID=? AND DocBaseType=?" + " AND IsActive='Y' " + "ORDER BY IsDefault DESC"; int C_DocType_ID = DB.getSQLValue(null, sql, getAD_Client_ID(), DocBaseType); if (C_DocType_ID <= 0) log.log(Level.SEVERE, "Not found for AC_Client_ID=" + getAD_Client_ID() + " - " + DocBaseType); else { log.fine(DocBaseType); setC_DocTypeTarget_ID (C_DocType_ID); boolean isSOTrx = MDocBaseType.DOCBASETYPE_ARInvoice.equals(DocBaseType) || MDocBaseType.DOCBASETYPE_ARCreditMemo.equals(DocBaseType); setIsSOTrx (isSOTrx); boolean isReturnTrx = MDocBaseType.DOCBASETYPE_ARCreditMemo.equals(DocBaseType) || MDocBaseType.DOCBASETYPE_APCreditMemo.equals(DocBaseType); setIsReturnTrx(isReturnTrx); } } // setC_DocTypeTarget_ID /** * Set Target Document Type. * Based on SO flag AP/AP Invoice */ public void setC_DocTypeTarget_ID () { if (getC_DocTypeTarget_ID() > 0) return; if (isSOTrx()) setC_DocTypeTarget_ID(MDocBaseType.DOCBASETYPE_ARInvoice); else setC_DocTypeTarget_ID(MDocBaseType.DOCBASETYPE_APInvoice); } // setC_DocTypeTarget_ID /** * Set Target Document Type * @param C_DocTypeTarget_ID id * @param setReturnTrx if true set ReturnTrx and SOTrx */ public void setC_DocTypeTarget_ID(int C_DocTypeTarget_ID, boolean setReturnTrx) { super.setC_DocTypeTarget_ID(C_DocTypeTarget_ID); if (setReturnTrx) { MDocType dt = MDocType.get(getCtx(), C_DocTypeTarget_ID); setIsSOTrx(dt.isSOTrx()); setIsReturnTrx(dt.isReturnTrx()); } } // setC_DocTypeTarget_ID /** * Get Grand Total * @param creditMemoAdjusted adjusted for CM (negative) * @return grand total */ public BigDecimal getGrandTotal (boolean creditMemoAdjusted) { if (!creditMemoAdjusted) return super.getGrandTotal(); // BigDecimal amt = getGrandTotal(); if (isCreditMemo()) return amt.negate(); return amt; } // getGrandTotal /** * Get Invoice Lines of Invoice * @param whereClause starting with AND * @return lines */ private MInvoiceLine[] getLines (String whereClause) { ArrayList<MInvoiceLine> list = new ArrayList<MInvoiceLine>(); String sql = "SELECT * FROM C_InvoiceLine WHERE C_Invoice_ID=? "; if (whereClause != null) sql += whereClause; sql += " ORDER BY Line"; PreparedStatement pstmt = null; try { pstmt = DB.prepareStatement(sql, get_TrxName()); pstmt.setInt(1, getC_Invoice_ID()); ResultSet rs = pstmt.executeQuery(); while (rs.next()) { MInvoiceLine il = new MInvoiceLine(getCtx(), rs, get_TrxName()); il.setInvoice(this); list.add(il); } rs.close(); pstmt.close(); pstmt = null; } catch (Exception e) { log.log(Level.SEVERE, "getLines", e); } finally { try { if (pstmt != null) pstmt.close (); } catch (Exception e) {} pstmt = null; } // MInvoiceLine[] lines = new MInvoiceLine[list.size()]; list.toArray(lines); return lines; } // getLines /** * Get Invoice Lines * @param requery * @return lines */ public MInvoiceLine[] getLines (boolean requery) { if (m_lines == null || m_lines.length == 0 || requery) m_lines = getLines(null); return m_lines; } // getLines /** * Get Lines of Invoice * @return lines */ public MInvoiceLine[] getLines() { return getLines(false); } // getLines /** * Renumber Lines * @param step start and step */ public void renumberLines (int step) { int number = step; MInvoiceLine[] lines = getLines(false); for (int i = 0; i < lines.length; i++) { MInvoiceLine line = lines[i]; line.setLine(number); line.save(); number += step; } m_lines = null; } // renumberLines /** * Copy Lines From other Invoice. * @param otherInvoice invoice * @param counter create counter links * @param setOrder set order links * @return number of lines copied */ public int copyLinesFrom (MInvoice otherInvoice, boolean counter, boolean setOrder) { if (isProcessed() || isPosted() || otherInvoice == null) return 0; MInvoiceLine[] fromLines = otherInvoice.getLines(false); int count = 0; for (int i = 0; i < fromLines.length; i++) { MInvoiceLine line = new MInvoiceLine (getCtx(), 0, get_TrxName()); MInvoiceLine fromLine = fromLines[i]; if (counter) // header PO.copyValues (fromLine, line, getAD_Client_ID(), getAD_Org_ID()); else PO.copyValues (fromLine, line, fromLine.getAD_Client_ID(), fromLine.getAD_Org_ID()); line.setC_Invoice_ID(getC_Invoice_ID()); line.setInvoice(this); line.set_ValueNoCheck ("C_InvoiceLine_ID", I_ZERO); // new // Reset if (!setOrder) line.setC_OrderLine_ID(0); line.setRef_InvoiceLine_ID(0); line.setM_InOutLine_ID(0); line.setA_Asset_ID(0); line.setM_AttributeSetInstance_ID(0); line.setS_ResourceAssignment_ID(0); // New Tax if (getC_BPartner_ID() != otherInvoice.getC_BPartner_ID()) line.setTax(); // recalculate // if (counter) { line.setRef_InvoiceLine_ID(fromLine.getC_InvoiceLine_ID()); if (fromLine.getC_OrderLine_ID() != 0) { MOrderLine peer = new MOrderLine (getCtx(), fromLine.getC_OrderLine_ID(), get_TrxName()); if (peer.getRef_OrderLine_ID() != 0) line.setC_OrderLine_ID(peer.getRef_OrderLine_ID()); } line.setM_InOutLine_ID(0); if (fromLine.getM_InOutLine_ID() != 0) { MInOutLine peer = new MInOutLine (getCtx(), fromLine.getM_InOutLine_ID(), get_TrxName()); if (peer.getRef_InOutLine_ID() != 0) line.setM_InOutLine_ID(peer.getRef_InOutLine_ID()); } } // line.setProcessed(false); if (line.save(get_TrxName())) count++; // Cross Link if (counter) { fromLine.setRef_InvoiceLine_ID(line.getC_InvoiceLine_ID()); fromLine.save(get_TrxName()); } } if (fromLines.length != count) log.log(Level.SEVERE, "Line difference - From=" + fromLines.length + " <> Saved=" + count); return count; } // copyLinesFrom /** Reversal Flag */ private boolean m_reversal = false; /** * Set Reversal * @param reversal reversal */ private void setReversal(boolean reversal) { m_reversal = reversal; } // setReversal /** * Is Reversal * @return reversal */ private boolean isReversal() { return m_reversal; } // isReversal /** * Get Taxes * @param requery requery * @return array of taxes */ public MInvoiceTax[] getTaxes (boolean requery) { if (m_taxes != null && !requery) return m_taxes; String sql = "SELECT * FROM C_InvoiceTax WHERE C_Invoice_ID=?"; ArrayList<MInvoiceTax> list = new ArrayList<MInvoiceTax>(); PreparedStatement pstmt = null; try { pstmt = DB.prepareStatement (sql, get_TrxName()); pstmt.setInt (1, getC_Invoice_ID()); ResultSet rs = pstmt.executeQuery (); while (rs.next ()) list.add(new MInvoiceTax(getCtx(), rs, get_TrxName())); rs.close (); pstmt.close (); pstmt = null; } catch (Exception e) { log.log(Level.SEVERE, "getTaxes", e); } try { if (pstmt != null) pstmt.close (); pstmt = null; } catch (Exception e) { pstmt = null; } m_taxes = new MInvoiceTax[list.size ()]; list.toArray (m_taxes); return m_taxes; } // getTaxes /** * Add to Description * @param description text */ public void addDescription (String description) { String desc = getDescription(); if (desc == null) setDescription(description); else setDescription(desc + " | " + description); } // addDescription /** * Is it a Credit Memo? * @return true if CM */ public boolean isCreditMemo() { MDocType dt = MDocType.get(getCtx(), getC_DocType_ID()==0 ? getC_DocTypeTarget_ID() : getC_DocType_ID()); return MDocBaseType.DOCBASETYPE_APCreditMemo.equals(dt.getDocBaseType()) || MDocBaseType.DOCBASETYPE_ARCreditMemo.equals(dt.getDocBaseType()); } // isCreditMemo /** * 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_Invoice_ID=" + getC_Invoice_ID(); int noLine = DB.executeUpdate("UPDATE C_InvoiceLine " + set, get_TrxName()); int noTax = DB.executeUpdate("UPDATE C_InvoiceTax " + set, get_TrxName()); m_lines = null; m_taxes = null; log.fine(processed + " - Lines=" + noLine + ", Tax=" + noTax); } // setProcessed /** * Validate Invoice Pay Schedule * @return pay schedule is valid */ public boolean validatePaySchedule() { MInvoicePaySchedule[] schedule = MInvoicePaySchedule.getInvoicePaySchedule (getCtx(), getC_Invoice_ID(), 0, get_TrxName()); log.fine("#" + schedule.length); if (schedule.length == 0) { setIsPayScheduleValid(false); return false; } // Add up due amounts BigDecimal total = Env.ZERO; for (int i = 0; i < schedule.length; i++) { schedule[i].setParent(this); BigDecimal due = schedule[i].getDueAmt(); if (due != null) total = total.add(due); } boolean valid = getGrandTotal().compareTo(total) == 0; setIsPayScheduleValid(valid); // Update Schedule Lines for (int i = 0; i < schedule.length; i++) { if (schedule[i].isValid() != valid) { schedule[i].setIsValid(valid); schedule[i].save(get_TrxName()); } } return valid; } // validatePaySchedule /************************************************************************** * Before Save * @param newRecord new * @return true */ protected boolean beforeSave (boolean newRecord) { // 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)); // Price List if (getM_PriceList_ID() == 0) { int ii = getCtx().getContextAsInt( "#M_PriceList_ID"); if (ii != 0) setM_PriceList_ID(ii); else { String sql = "SELECT M_PriceList_ID FROM M_PriceList WHERE AD_Client_ID=? AND IsDefault='Y'"; ii = DB.getSQLValue (null, sql, getAD_Client_ID()); if (ii != 0) setM_PriceList_ID (ii); } } boolean validPLV = false; // Verify that price list has a valid version for the date String sql1 = "SELECT 1 " + "FROM M_PriceList pl,M_PriceList_Version plv " + "WHERE pl.M_PriceList_ID=plv.M_PriceList_ID" + " AND pl.M_PriceList_ID=? " // 1 + " AND plv.ValidFrom <=? " // 2 + " AND pl.IsActive='Y' " + " AND plv.IsActive='Y' "; // Use newest price list - may not be future try { PreparedStatement pstmt = DB.prepareStatement(sql1, null); pstmt.setInt(1, getM_PriceList_ID()); pstmt.setTimestamp(2, getDateInvoiced()); ResultSet rs = pstmt.executeQuery(); if (rs.next()) validPLV = true; rs.close(); pstmt.close(); } catch (SQLException e) { log.log(Level.SEVERE, sql1, e); return false; } if(!validPLV) { log.saveError("Error", Msg.getMsg(getCtx(), "PriceListVersionNotFound")); return false; } // 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(getCtx().getContextAsInt( "#C_Currency_ID")); } // Sales Rep if (getSalesRep_ID() == 0) { int ii = getCtx().getContextAsInt( "#SalesRep_ID"); if (ii != 0) setSalesRep_ID (ii); } // Document Type if (getC_DocType_ID() == 0) setC_DocType_ID (0); // make sure it's set to 0 if (getC_DocTypeTarget_ID() == 0) setC_DocTypeTarget_ID(isSOTrx() ? MDocBaseType.DOCBASETYPE_ARInvoice : MDocBaseType.DOCBASETYPE_APInvoice); // Payment Term if (getC_PaymentTerm_ID() == 0) { int ii = getCtx().getContextAsInt( "#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); } } // BPartner Active if (newRecord || is_ValueChanged("C_BPartner_ID")) { MBPartner bp = MBPartner.get(getCtx(), getC_BPartner_ID()); if (!bp.isActive()) { log.saveWarning("NotActive", Msg.getMsg(getCtx(), "C_BPartner_ID")); return false; } } return true; } // beforeSave /** * Before Delete * @return true if it can be deleted */ protected boolean beforeDelete () { if (getC_Order_ID() != 0) { log.saveError("Error", Msg.getMsg(getCtx(), "CannotDelete")); return false; } return true; } // beforeDelete /** * String Representation * @return info */ public String toString () { StringBuffer sb = new StringBuffer ("MInvoice[") .append(get_ID()).append("-").append(getDocumentNo()) .append(",GrandTotal=").append(getGrandTotal()); if (m_lines != null) sb.append(" (#").append(m_lines.length).append(")"); sb.append ("]"); return sb.toString (); } // toString /** * Get Document Info * @return document info (untranslated) */ public String getDocumentInfo() { MDocType dt = MDocType.get(getCtx(), getC_DocType_ID()); return dt.getName() + " " + getDocumentNo(); } // getDocumentInfo /** * After Save * @param newRecord new * @param success success * @return success */ protected boolean afterSave (boolean newRecord, boolean success) { if (!success || newRecord) return success; if (is_ValueChanged("AD_Org_ID")) { String sql = "UPDATE C_InvoiceLine ol" + " SET AD_Org_ID =" + "(SELECT AD_Org_ID" + " FROM C_Invoice o WHERE ol.C_Invoice_ID=o.C_Invoice_ID) " + "WHERE C_Invoice_ID=" + getC_Invoice_ID(); int no = DB.executeUpdate(sql, get_TrxName()); log.fine("Lines -> #" + no); } return true; } // afterSave /** * Set Price List (and Currency) when valid * @param M_PriceList_ID price list */ public void setM_PriceList_ID (int M_PriceList_ID) { String sql = "SELECT M_PriceList_ID, C_Currency_ID " + "FROM M_PriceList WHERE M_PriceList_ID=?"; PreparedStatement pstmt = null; try { pstmt = DB.prepareStatement(sql, null); pstmt.setInt(1, M_PriceList_ID); ResultSet rs = pstmt.executeQuery(); if (rs.next()) { super.setM_PriceList_ID (rs.getInt(1)); setC_Currency_ID (rs.getInt(2)); } rs.close(); pstmt.close(); pstmt = null; } catch (Exception e) { log.log(Level.SEVERE, "setM_PriceList_ID", e); } finally { try { if (pstmt != null) pstmt.close (); } catch (Exception e) {} pstmt = null; } } // setM_PriceList_ID /** * Get Allocated Amt in Invoice Currency * @return pos/neg amount or null */ public BigDecimal getAllocatedAmt () { BigDecimal retValue = null; String sql = "SELECT SUM(currencyConvert(al.Amount+al.DiscountAmt+al.WriteOffAmt," + "ah.C_Currency_ID, i.C_Currency_ID,ah.DateTrx,COALESCE(i.C_ConversionType_ID,0), al.AD_Client_ID,al.AD_Org_ID)) " //jz + "FROM C_AllocationLine al" + " INNER JOIN C_AllocationHdr ah ON (al.C_AllocationHdr_ID=ah.C_AllocationHdr_ID)" + " INNER JOIN C_Invoice i ON (al.C_Invoice_ID=i.C_Invoice_ID) " + "WHERE al.C_Invoice_ID=?" + " AND ah.IsActive='Y' AND al.IsActive='Y'"; PreparedStatement pstmt = null; try { pstmt = DB.prepareStatement(sql, get_TrxName()); pstmt.setInt(1, getC_Invoice_ID()); ResultSet rs = pstmt.executeQuery(); if (rs.next()) retValue = rs.getBigDecimal(1); rs.close(); pstmt.close(); pstmt = null; } catch (Exception e) { log.log(Level.SEVERE, sql, e); } try { if (pstmt != null) pstmt.close(); pstmt = null; } catch (Exception e) { pstmt = null; } // log.fine("getAllocatedAmt - " + retValue); // ? ROUND(NVL(v_AllocatedAmt,0), 2); return retValue; } // getAllocatedAmt /** * Test Allocation (and set paid flag) * @return true if updated */ public boolean testAllocation() { BigDecimal alloc = getAllocatedAmt(); // absolute if (alloc == null) alloc = Env.ZERO; BigDecimal total = getGrandTotal(); if (!isSOTrx()) total = total.negate(); if (isCreditMemo()) total = total.negate(); boolean test = total.compareTo(alloc) == 0; boolean change = test != isPaid(); if (change) setIsPaid(test); log.fine("Paid=" + test + " (" + alloc + "=" + total + ")"); return change; } // testAllocation /** * Set Paid Flag for invoices * @param ctx context * @param C_BPartner_ID if 0 all * @param trxName transaction */ public static void setIsPaid (Ctx ctx, int C_BPartner_ID, String trxName) { int counter = 0; String sql = "SELECT * FROM C_Invoice " + "WHERE IsPaid='N' AND DocStatus IN ('CO','CL')"; if (C_BPartner_ID > 1) sql += " AND C_BPartner_ID=?"; else sql += " AND AD_Client_ID=" + ctx.getAD_Client_ID(); PreparedStatement pstmt = null; try { pstmt = DB.prepareStatement (sql, trxName); if (C_BPartner_ID > 1) pstmt.setInt (1, C_BPartner_ID); ResultSet rs = pstmt.executeQuery (); while (rs.next ()) { MInvoice invoice = new MInvoice(ctx, rs, trxName); if (invoice.testAllocation()) if (invoice.save()) counter++; } rs.close (); pstmt.close (); pstmt = null; } catch (Exception e) { s_log.log(Level.SEVERE, sql, e); } try { if (pstmt != null) pstmt.close (); pstmt = null; } catch (Exception e) { pstmt = null; } s_log.config("#" + counter); /**/ } // setIsPaid /** * Get Open Amount. * Used by web interface * @return Open Amt */ public BigDecimal getOpenAmt () { return getOpenAmt (true, null); } // getOpenAmt /** * Get Open Amount * @param creditMemoAdjusted adjusted for CM (negative) * @param paymentDate ignored Payment Date * @return Open Amt */ public BigDecimal getOpenAmt (boolean creditMemoAdjusted, Timestamp paymentDate) { if (isPaid()) return Env.ZERO; // if (m_openAmt == null) { m_openAmt = getGrandTotal(); if (paymentDate != null) { // Payment Discount // Payment Schedule } BigDecimal allocated = getAllocatedAmt(); if (allocated != null) { if (!isSOTrx()) allocated = allocated.negate(); m_openAmt = m_openAmt.subtract(allocated); } } // if (!creditMemoAdjusted) return m_openAmt; if (isCreditMemo()) return m_openAmt.negate(); return m_openAmt; } // getOpenAmt /** * Get Document Status * @return Document Status Clear Text */ public String getDocStatusName() { return MRefList.getListName(getCtx(), 131, getDocStatus()); } // getDocStatusName /************************************************************************** * Create PDF * @return File or null */ public File createPDF () { try { File temp = File.createTempFile(get_TableName()+get_ID()+"_", ".pdf"); return createPDF (temp); } catch (Exception e) { log.severe("Could not create PDF - " + e.getMessage()); } return null; } // getPDF /** * Create PDF file * @param file output file * @return file if success */ public File createPDF (File file) { ReportEngine re = ReportEngine.get (getCtx(), ReportEngine.INVOICE, getC_Invoice_ID()); if (re == null) return null; return re.getPDF(file); } // createPDF /** * Get PDF File Name * @param documentDir directory * @return file name */ public String getPDFFileName (String documentDir) { return getPDFFileName (documentDir, getC_Invoice_ID()); } // getPDFFileName /** * Get ISO Code of Currency * @return Currency ISO */ public String getCurrencyISO() { return MCurrency.getISO_Code (getCtx(), getC_Currency_ID()); } // getCurrencyISO /** * Get Currency Precision * @return precision */ public int getPrecision() { return MCurrency.getStdPrecision(getCtx(), getC_Currency_ID()); } // getPrecision /************************************************************************** * 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()); } // process /** 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("invalidateIt - " + 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.DOCTIMING_BEFORE_PREPARE); if (m_processMsg != null) return DocAction.STATUS_Invalid; MDocType dt = MDocType.get(getCtx(), getC_DocTypeTarget_ID()); setIsReturnTrx(dt.isReturnTrx()); setIsSOTrx(dt.isSOTrx()); // Lines MInvoiceLine[] lines = getLines(true); if (lines.length == 0) { m_processMsg = "@NoLines@"; return DocAction.STATUS_Invalid; } // Std Period open? m_processMsg = MPeriod.isOpen(this, lines, getDateAcct(), dt.getDocBaseType()); if (m_processMsg != null) return DocAction.STATUS_Invalid; // No Cash Book if (PAYMENTRULE_Cash.equals(getPaymentRule()) && MCashBook.get(getCtx(), getAD_Org_ID(), getC_Currency_ID()) == null) { m_processMsg = "@NoCashBook@"; return DocAction.STATUS_Invalid; } // Convert/Check DocType if (getC_DocType_ID() != getC_DocTypeTarget_ID() ) setC_DocType_ID(getC_DocTypeTarget_ID()); if (getC_DocType_ID() == 0) { m_processMsg = "No Document Type"; return DocAction.STATUS_Invalid; } explodeBOM(); if (!calculateTaxTotal()) // setTotals { m_processMsg = "Error calculating Tax"; return DocAction.STATUS_Invalid; } createPaySchedule(); // Credit Status if (isSOTrx() && !isReversal()) { 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; } } // Landed Costs if (!isSOTrx()) { for (int i = 0; i < lines.length; i++) { MInvoiceLine line = lines[i]; String error = line.allocateLandedCosts(); if (error != null && error.length() > 0) { m_processMsg = error; return DocAction.STATUS_Invalid; } } } // Add up Amounts m_justPrepared = true; if (!DOCACTION_Complete.equals(getDocAction())) setDocAction(DOCACTION_Complete); return DocAction.STATUS_InProgress; } // prepareIt /** * Explode non stocked BOM. */ private void explodeBOM () { String where = "AND IsActive='Y' AND EXISTS " + "(SELECT * FROM M_Product p WHERE C_InvoiceLine.M_Product_ID=p.M_Product_ID" + " AND p.IsBOM='Y' AND p.IsVerified='Y' AND p.IsStocked='N')"; // String sql = "SELECT COUNT(*) FROM C_InvoiceLine " + "WHERE C_Invoice_ID=? " + where; int count = DB.getSQLValue(get_TrxName(), sql, getC_Invoice_ID()); while (count != 0) { renumberLines (1000); // max 999 bom items // Order Lines with non-stocked BOMs MInvoiceLine[] lines = getLines (where); for (int i = 0; i < lines.length; i++) { MInvoiceLine line = lines[i]; MProduct product = MProduct.get (getCtx(), line.getM_Product_ID()); log.fine(product.getName()); // New Lines int lineNo = line.getLine (); MBOMProduct[] boms = MBOMProduct.getBOMLines (product); for (int j = 0; j < boms.length; j++) { MBOMProduct bom = boms[j]; MInvoiceLine newLine = new MInvoiceLine (this); newLine.setLine (++lineNo); newLine.setM_Product_ID (bom.getComponent().getM_Product_ID(), bom.getComponent().getC_UOM_ID()); newLine.setQty (line.getQtyInvoiced().multiply( bom.getBOMQty ())); // Invoiced/Entered if (bom.getDescription () != null) newLine.setDescription (bom.getDescription ()); // newLine.setPrice (); newLine.save (get_TrxName()); } // Convert into Comment Line line.setM_Product_ID (0); line.setM_AttributeSetInstance_ID (0); line.setPriceEntered (Env.ZERO); line.setPriceActual (Env.ZERO); line.setPriceLimit (Env.ZERO); line.setPriceList (Env.ZERO); line.setLineNetAmt (Env.ZERO); // String description = product.getName (); if (product.getDescription () != null) description += " " + product.getDescription (); if (line.getDescription () != null) description += " " + line.getDescription (); line.setDescription (description); line.save (get_TrxName()); } // for all lines with BOM m_lines = null; count = DB.getSQLValue (get_TrxName(), sql, getC_Invoice_ID ()); renumberLines (10); } // while count != 0 } // explodeBOM /** * Calculate Tax and Total * @return true if calculated */ private boolean calculateTaxTotal() { log.fine(""); // Delete Taxes DB.executeUpdate("DELETE FROM C_InvoiceTax WHERE C_Invoice_ID=" + getC_Invoice_ID(), get_TrxName()); m_taxes = null; // Lines BigDecimal totalLines = Env.ZERO; ArrayList<Integer> taxList = new ArrayList<Integer>(); MInvoiceLine[] lines = getLines(false); for (int i = 0; i < lines.length; i++) { MInvoiceLine line = lines[i]; /** Sync ownership for SO if (isSOTrx() && line.getAD_Org_ID() != getAD_Org_ID()) { line.setAD_Org_ID(getAD_Org_ID()); line.save(); } **/ Integer taxID = new Integer(line.getC_Tax_ID()); if (!taxList.contains(taxID)) { MInvoiceTax iTax = MInvoiceTax.get (line, getPrecision(), false, get_TrxName()); // current Tax if (iTax != null) { iTax.setIsTaxIncluded(isTaxIncluded()); if (!iTax.calculateTaxFromLines()) return false; if (!iTax.save()) return false; taxList.add(taxID); } } totalLines = totalLines.add(line.getLineNetAmt()); } // Taxes BigDecimal grandTotal = totalLines; MInvoiceTax[] taxes = getTaxes(true); for (int i = 0; i < taxes.length; i++) { MInvoiceTax iTax = taxes[i]; MTax tax = iTax.getTax(); if (tax.isSummary()) { MTax[] cTaxes = tax.getChildTaxes(false); // Multiple taxes for (int j = 0; j < cTaxes.length; j++) { MTax cTax = cTaxes[j]; BigDecimal taxAmt = cTax.calculateTax(iTax.getTaxBaseAmt(), isTaxIncluded(), getPrecision()); // MInvoiceTax newITax = new MInvoiceTax(getCtx(), 0, get_TrxName()); newITax.setClientOrg(this); newITax.setC_Invoice_ID(getC_Invoice_ID()); newITax.setC_Tax_ID(cTax.getC_Tax_ID()); newITax.setPrecision(getPrecision()); newITax.setIsTaxIncluded(isTaxIncluded()); newITax.setTaxBaseAmt(iTax.getTaxBaseAmt()); newITax.setTaxAmt(taxAmt); if (!newITax.save(get_TrxName())) return false; // if (!isTaxIncluded()) grandTotal = grandTotal.add(taxAmt); } if (!iTax.delete(true, get_TrxName())) return false; } else { if (!isTaxIncluded()) grandTotal = grandTotal.add(iTax.getTaxAmt()); } } // setTotalLines(totalLines); setGrandTotal(grandTotal); return true; } // calculateTaxTotal /** * (Re) Create Pay Schedule * @return true if valid schedule */ private boolean createPaySchedule() { if (getC_PaymentTerm_ID() == 0) return false; MPaymentTerm pt = new MPaymentTerm(getCtx(), getC_PaymentTerm_ID(), null); log.fine(pt.toString()); return pt.apply(this); // calls validate pay schedule } // createPaySchedule /** * Approve Document * @return true if success */ public boolean approveIt() { log.info(toString()); setIsApproved(true); return true; } // approveIt /** * Reject Approval * @return true if success */ public boolean rejectIt() { log.info(toString()); setIsApproved(false); return true; } // rejectIt /** * Complete Document * @return new status (Complete, In Progress, Invalid, Waiting ..) */ public String completeIt() { // Re-Check if (!m_justPrepared) { String status = prepareIt(); if (!DocAction.STATUS_InProgress.equals(status)) return status; } // Implicit Approval if (!isApproved()) approveIt(); log.info(toString()); StringBuffer info = new StringBuffer(); // Create Cash if (PAYMENTRULE_Cash.equals(getPaymentRule())) { MCash cash = MCash.get (getCtx(), getAD_Org_ID(), getDateInvoiced(), getC_Currency_ID(), get_TrxName()); if (cash == null || cash.get_ID() == 0) { m_processMsg = "@NoCashBook@"; return DocAction.STATUS_Invalid; } MCashLine cl = new MCashLine (cash); cl.setInvoice(this); if (!cl.save(get_TrxName())) { m_processMsg = "Could not save Cash Journal Line"; return DocAction.STATUS_Invalid; } 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()) { MInOutLine receiptLine = new MInOutLine (getCtx(),line.getM_InOutLine_ID(), get_TrxName()); BigDecimal matchQty = line.getQtyInvoiced(); if (receiptLine.getMovementQty().compareTo(matchQty) < 0) matchQty = receiptLine.getMovementQty(); 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++; } // Lead/Request line.createLeadRequest(this); } // 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(), getC_ConversionType_ID(), getAD_Client_ID(), getAD_Org_ID()); if (invAmt == null) { m_processMsg = "Could not convert C_Currency_ID=" + getC_Currency_ID() + " to base C_Currency_ID=" + MClient.get(getCtx()).getC_Currency_ID(); return DocAction.STATUS_Invalid; } // 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()); if (amt == null) { m_processMsg = "Could not convert C_Currency_ID=" + getC_Currency_ID() + " to Project C_Currency_ID=" + C_CurrencyTo_ID; return DocAction.STATUS_Invalid; } 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.DOCTIMING_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 * @return counter invoice */ 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(get_TrxName()); //jz 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, null); 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, true, get_TrxName(), true); MDocType dt = MDocType.get(getCtx(), C_DocTypeTarget_ID); if (!dt.isDocNoControlled()) counter.setDocumentNo(getDocumentNo()); // copy if manual // 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.setProcessing(false); 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()); // MInvoiceLine[] iLines = getLines(false); m_processMsg = MPeriod.isOpen(this, iLines, getDateAcct(), dt.getDocBaseType()); if (m_processMsg != null) return false; // Don't touch allocation for cash as that is handled in CashJournal boolean isCash = PAYMENTRULE_Cash.equals(getPaymentRule()); if (!isCash) { 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(), 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.setProcessing (false); reversal.setDocStatus(DOCSTATUS_Reversed); reversal.setDocAction(DOCACTION_None); reversal.save(get_TrxName()); m_processMsg = reversal.getDocumentNo(); // addDescription("(" + reversal.getDocumentNo() + "<-)"); // Clean up Reversed (this) 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()); // Reconciliation 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 if (!isCash) { 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.setAD_Org_ID(getAD_Org_ID()); 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.setAD_Org_ID(reversal.getAD_Org_ID()); rLine.setC_Invoice_ID(reversal.getC_Invoice_ID()); rLine.save(); // Process It if (alloc.processIt(DocAction.ACTION_Complete)) alloc.save(); } } // notCash // Explicitly save for balance calc. save(); // Update BP Balance MBPartner bp = new MBPartner (getCtx(), getC_BPartner_ID(), get_TrxName()); bp.setTotalOpenBalance(); bp.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 /** * Set Price List - Callout * @param oldM_PriceList_ID old value * @param newM_PriceList_ID new value * @param windowNo window * @throws Exception */ @UICallout public void setM_PriceList_ID (String oldM_PriceList_ID, String newM_PriceList_ID, int windowNo) throws Exception { if (newM_PriceList_ID == null || newM_PriceList_ID.length() == 0) return; int M_PriceList_ID = Integer.parseInt(newM_PriceList_ID); if (M_PriceList_ID == 0) return; setPriceListVersion(windowNo); } // setM_PriceList_ID /** * * @param oldC_PaymentTerm_ID * @param newC_PaymentTerm_ID * @param windowNo * @throws Exception */ @UICallout public void setC_PaymentTerm_ID( String oldC_PaymentTerm_ID, String newC_PaymentTerm_ID, int windowNo ) throws Exception { if( newC_PaymentTerm_ID == null || newC_PaymentTerm_ID.length() == 0 ) return; int C_PaymentTerm_ID = Integer.parseInt( newC_PaymentTerm_ID ); int C_Invoice_ID = getC_Invoice_ID(); if ( C_PaymentTerm_ID == 0 || C_Invoice_ID == 0) // not saved yet return; // MPaymentTerm pt = new MPaymentTerm (getCtx(), C_PaymentTerm_ID, null); if (pt.get_ID() == 0) { addError( Msg.getMsg( getCtx(), "PaymentTerm not found" ) ); } boolean valid = pt.apply (C_Invoice_ID); setIsPayScheduleValid( valid ); return; } /** * Invoice Header - DocType. * - PaymentRule * - temporary Document * Context: * - DocSubTypeSO * - HasCharges * - (re-sets Business Partner info of required) * @param ctx context * @param WindowNo window no * @param mTab tab * @param mField field * @param value value * @return null or error message */ @UICallout public void setC_DocTypeTarget_ID (String oldC_DocTypeTarget_ID, String newC_DocTypeTarget_ID, int WindowNo) throws Exception { if (Util.isEmpty(newC_DocTypeTarget_ID)) return; int C_DocTypeTarget_ID = convertToInt(newC_DocTypeTarget_ID); if (C_DocTypeTarget_ID == 0) return; // Re-Create new DocNo, if there is a doc number already // and the existing source used a different Sequence number String oldDocNo = getDocumentNo(); boolean newDocNo = (oldDocNo == null); if (!newDocNo && oldDocNo.startsWith("<") && oldDocNo.endsWith(">")) newDocNo = true; int oldC_DocType_ID = getC_DocType_ID(); if (oldC_DocType_ID == 0 && !Util.isEmpty(oldC_DocTypeTarget_ID)) oldC_DocType_ID = convertToInt(oldC_DocTypeTarget_ID); String sql = "SELECT d.HasCharges,'N',d.IsDocNoControlled," + "s.CurrentNext, d.DocBaseType, s.CurrentNextSys, s.AD_Sequence_ID " + "FROM C_DocType d " + "LEFT OUTER JOIN AD_Sequence s ON (d.DocNoSequence_ID=s.AD_Sequence_ID) " + "WHERE C_DocType_ID=?"; // 1 try { int AD_Sequence_ID = 0; // Get old AD_SeqNo for comparison if (!newDocNo && oldC_DocType_ID != 0) { PreparedStatement pstmt = DB.prepareStatement(sql, null); pstmt.setInt(1, oldC_DocType_ID); ResultSet rs = pstmt.executeQuery(); if (rs.next()) AD_Sequence_ID = rs.getInt(7); rs.close(); pstmt.close(); } PreparedStatement pstmt = DB.prepareStatement(sql, null); pstmt.setInt(1, C_DocTypeTarget_ID); ResultSet rs = pstmt.executeQuery(); if (rs.next()) { // Charges - Set Context setContext( WindowNo, "HasCharges", rs.getString(1)); // DocumentNo if (rs.getString(3).equals("Y")) // IsDocNoControlled { if (!newDocNo && AD_Sequence_ID != rs.getInt(7)) newDocNo = true; if (newDocNo) if (Ini.isPropertyBool(Ini.P_COMPIERESYS) && getCtx().getAD_Client_ID() < 1000000) setDocumentNo("<" + rs.getString(6) + ">"); else setDocumentNo("<" + rs.getString(4) + ">"); } // DocBaseType - Set Context String s = rs.getString(5); setContext( WindowNo, "DocBaseType", s); // AP Check & AR Credit Memo if (s.startsWith("AP")) setPaymentRule( "S"); // Check else if (s.endsWith("C")) setPaymentRule("P"); // OnCredit } rs.close(); pstmt.close(); } catch (SQLException e) { log.log(Level.SEVERE, sql, e); } return; } // docType /** * Invoice Header- BPartner. * - M_PriceList_ID (+ Context) * - C_BPartner_Location_ID * - AD_User_ID * - POReference * - SO_Description * - IsDiscountPrinted * - PaymentRule * - C_PaymentTerm_ID * @param ctx context * @param WindowNo window no * @param mTab tab * @param mField field * @param value value * @return null or error message */ @UICallout public void setC_BPartner_ID (String oldC_BPartner_ID, String newC_BPartner_ID, int WindowNo) throws Exception { if( newC_BPartner_ID == null || newC_BPartner_ID.length() == 0 ) return; Integer C_BPartner_ID = Integer.parseInt( newC_BPartner_ID ); if (C_BPartner_ID == null || C_BPartner_ID.intValue() == 0) return; String sql = "SELECT p.AD_Language,p.C_PaymentTerm_ID," + " COALESCE(p.M_PriceList_ID,g.M_PriceList_ID) AS M_PriceList_ID, p.PaymentRule,p.POReference," + " p.SO_Description,p.IsDiscountPrinted," + " p.SO_CreditLimit, p.SO_CreditLimit-p.SO_CreditUsed AS CreditAvailable," + " l.C_BPartner_Location_ID,c.AD_User_ID," + " COALESCE(p.PO_PriceList_ID,g.PO_PriceList_ID) AS PO_PriceList_ID, p.PaymentRulePO,p.PO_PaymentTerm_ID " + "FROM C_BPartner p" + " INNER JOIN C_BP_Group g ON (p.C_BP_Group_ID=g.C_BP_Group_ID)" + " LEFT OUTER JOIN C_BPartner_Location l ON (p.C_BPartner_ID=l.C_BPartner_ID AND l.IsBillTo='Y' AND l.IsActive='Y')" + " LEFT OUTER JOIN AD_User c ON (p.C_BPartner_ID=c.C_BPartner_ID) " + "WHERE p.C_BPartner_ID=? AND p.IsActive='Y'"; // #1 boolean IsSOTrx = isSOTrx(); try { PreparedStatement pstmt = DB.prepareStatement(sql, null); pstmt.setInt(1, C_BPartner_ID.intValue()); ResultSet rs = pstmt.executeQuery(); // if (rs.next()) { // PriceList & IsTaxIncluded & Currency Integer ii = new Integer(rs.getInt(IsSOTrx ? "M_PriceList_ID" : "PO_PriceList_ID")); if (!rs.wasNull()) setM_PriceList_ID( ii); else { // get default PriceList int i = getCtx().getContextAsInt( "#M_PriceList_ID"); if (i != 0) setM_PriceList_ID( new Integer(i)); } // PaymentRule String s = rs.getString(IsSOTrx ? "PaymentRule" : "PaymentRulePO"); if (s != null && s.length() != 0) { if (getCtx().getContext( WindowNo, "DocBaseType").endsWith("C")) // Credits are Payment Term s = "P"; else if (IsSOTrx && (s.equals("S") || s.equals("U"))) // No Check/Transfer for SO_Trx s = "P"; // Payment Term setPaymentRule( s); } // Payment Term ii = new Integer(rs.getInt(IsSOTrx ? "C_PaymentTerm_ID" : "PO_PaymentTerm_ID")); if (!rs.wasNull()) setC_PaymentTerm_ID( ii); // Location int locID = rs.getInt("C_BPartner_Location_ID"); // overwritten by InfoBP selection - works only if InfoWindow // was used otherwise creates error (uses last value, may belong to differnt BP) if (C_BPartner_ID.toString().equals(getCtx().getContext( Env.WINDOW_INFO, Env.TAB_INFO, "C_BPartner_ID"))) { String loc = getCtx().getContext( Env.WINDOW_INFO, Env.TAB_INFO, "C_BPartner_Location_ID"); if (loc.length() > 0) locID = Integer.parseInt(loc); } if(locID == 0) p_changeVO.addChangedValue("C_BPartner_Location_ID", (String)null); else setC_BPartner_Location_ID(locID); // Contact - overwritten by InfoBP selection int contID = rs.getInt("AD_User_ID"); if (C_BPartner_ID.toString().equals(getCtx().getContext( Env.WINDOW_INFO, Env.TAB_INFO, "C_BPartner_ID"))) { String cont = getCtx().getContext( Env.WINDOW_INFO, Env.TAB_INFO, "AD_User_ID"); if (cont.length() > 0) contID = Integer.parseInt(cont); } setAD_User_ID( contID ); // CreditAvailable if (IsSOTrx) { double CreditLimit = rs.getDouble("SO_CreditLimit"); if (CreditLimit != 0) { double CreditAvailable = rs.getDouble("CreditAvailable"); if (!rs.wasNull() && CreditAvailable < 0) { String msg = Msg.getMsg(getCtx(), "CreditLimitOver", DisplayType.getNumberFormat(DisplayType.Amount).format(CreditAvailable)); addError(msg); } } } // PO Reference s = rs.getString("POReference"); if (s != null && s.length() != 0) setPOReference( s); else setPOReference( null); // SO Description s = rs.getString("SO_Description"); if (s != null && s.trim().length() != 0) setDescription( s); // IsDiscountPrinted s = rs.getString("IsDiscountPrinted"); setIsDiscountPrinted("Y".equals(s)); // setC_Project_ID(0); } rs.close(); pstmt.close(); } catch (SQLException e) { log.log(Level.SEVERE, "bPartner", e); } } // bPartner /** * Set DateInvoiced - Callout * @param oldDateInvoiced old * @param newDateInvoiced new * @param windowNo window no */ @UICallout public void setDateInvoiced (String oldDateInvoiced , String newDateInvoiced , int windowNo) throws Exception { if (newDateInvoiced == null || newDateInvoiced.length() == 0) return; Timestamp dateInvoiced = PO.convertToTimestamp(newDateInvoiced ); if (dateInvoiced == null) return; setDateInvoiced (dateInvoiced ); setPriceListVersion(windowNo); } // setDateOrdered public void setPriceListVersion(int windowNo) { int M_PriceList_ID = getM_PriceList_ID(); if (M_PriceList_ID == 0) return; Timestamp invoiceDate = getDateInvoiced(); if(invoiceDate == null) return; String sql = "SELECT pl.IsTaxIncluded,pl.EnforcePriceLimit,pl.C_Currency_ID,c.StdPrecision," + "plv.M_PriceList_Version_ID,plv.ValidFrom " + "FROM M_PriceList pl,C_Currency c,M_PriceList_Version plv " + "WHERE pl.C_Currency_ID=c.C_Currency_ID" + " AND pl.M_PriceList_ID=plv.M_PriceList_ID" + " AND pl.M_PriceList_ID=? " // 1 + " AND plv.ValidFrom <=? " // 2 + " AND plv.IsActive='Y' " + "ORDER BY plv.ValidFrom DESC"; // Use newest price list - may not be future try { PreparedStatement pstmt = DB.prepareStatement(sql, null); pstmt.setInt(1, M_PriceList_ID); pstmt.setTimestamp(2, invoiceDate); ResultSet rs = pstmt.executeQuery(); if (rs.next()) { // Tax Included setIsTaxIncluded("Y".equals(rs.getString(1))); // Price Limit Enforce if (p_changeVO != null) p_changeVO.setContext(getCtx(), windowNo, "EnforcePriceLimit", rs.getString(2)); // Currency Integer ii = new Integer(rs.getInt(3)); setC_Currency_ID(ii); // PriceList Version if (p_changeVO != null) p_changeVO.setContext(getCtx(), windowNo, "M_PriceList_Version_ID", rs.getInt(5)); } rs.close(); pstmt.close(); } catch (SQLException e) { log.log(Level.SEVERE, sql, e); } } } // MInvoice

The table below shows all metrics for MInvoice.java.

MetricValueDescription
BLOCKS221.00Number of blocks
BLOCK_COMMENT 1.00Number of block comment lines
COMMENTS572.00Comment lines
COMMENT_DENSITY 0.38Comment density
COMPARISONS339.00Number of comparison operators
CYCLOMATIC400.00Cyclomatic complexity
DECL_COMMENTS82.00Comments in declarations
DOC_COMMENT400.00Number of javadoc comment lines
ELOC1517.00Effective lines of code
EXEC_COMMENTS163.00Comments in executable code
EXITS427.00Procedure exits
FUNCTIONS72.00Number of function declarations
HALSTEAD_DIFFICULTY101.90Halstead difficulty
HALSTEAD_EFFORT 0.00Halstead effort
INTERFACE_COMPLEXITY221.00Interface complexity
JAVA0001 0.00JAVA0001 Package name does not contain only lower case letters
JAVA0002 0.00JAVA0002 Package name does not begin with a top level domain name or country code
JAVA0003 1.00JAVA0003 Minimize use of on-demand (.*) imports
JAVA0004 0.00JAVA0004 Unnecessary import from java.lang
JAVA0005 0.00JAVA0005 Imports not in specified order
JAVA0006 0.00JAVA0006 Empty finally block
JAVA0007 0.00JAVA0007 Should not declare public field
JAVA0008 2.00JAVA0008 Empty catch block
JAVA0009 0.00JAVA0009 Protected member in final class
JAVA0010 0.00JAVA0010 Non-instantiable class does not contain a non-private static member
JAVA0011 0.00JAVA0011 Abstract class does not contain an abstract method
JAVA0012 0.00JAVA0012 Non-constructor method with same name as declaring class
JAVA0013 0.00JAVA0013 Non-blank final field is not static
JAVA0014 0.00JAVA0014 Class with only static members has non-private constructor
JAVA0015 0.00JAVA0015 Package class contains public nested type
JAVA0016 0.00JAVA0016 Abstract class contains public constructor
JAVA0017 0.00JAVA0017 Class name does not have required form
JAVA0018 0.00JAVA0018 Method name does not have required form
JAVA0019 0.00JAVA0019 Interface name does not have required form
JAVA0020 0.00JAVA0020 Field name does not have required form
JAVA0021 0.00JAVA0021 Interface method name does not have required form
JAVA0022 0.00JAVA0022 Static final field name does not have required form
JAVA0023 0.00JAVA0023 Empty finalize method
JAVA0024 0.00JAVA0024 Empty class
JAVA0025 0.00JAVA0025 Method override is empty
JAVA0026 0.00JAVA0026 Finalize method with parameters
JAVA0029 0.00JAVA0029 Private method not used
JAVA0030 0.00JAVA0030 Private field not used
JAVA0031 0.00JAVA0031 Case statement not properly closed
JAVA0032 0.00JAVA0032 Switch statement missing default
JAVA0033 0.00JAVA0033 default: not last case in switch statement
JAVA0034167.00JAVA0034 Missing braces in if statement
JAVA0035 1.00JAVA0035 Missing braces in for statement
JAVA0036 2.00JAVA0036 Missing braces in while statement
JAVA0038 0.00JAVA0038 Non-case label in switch statement
JAVA0039 0.00JAVA0039 Break statement with label
JAVA0040 0.00JAVA0040 Switch statement contains N cases (maximum: M)
JAVA0041 0.00JAVA0041 Nested synchronized block
JAVA0042 0.00JAVA0042 Empty synchronized statement
JAVA0043 0.00JAVA0043 Inner class does not use outer class
JAVA0044 0.00JAVA0044 Serializable class with no instance variables
JAVA0045 0.00JAVA0045 Serializable class with only transient fields
JAVA0046 0.00JAVA0046 Name of class not derived from Exception ends with 'Exception'
JAVA0047 0.00JAVA0047 Serializable class derives from invalid base class
JAVA0048 0.00JAVA0048 Name of class derived from Exception does not end with 'Exception'
JAVA0049 1.00JAVA0049 Nested block at depth N (maximum: M)
JAVA0050 0.00JAVA0050 Class derives from java.lang.Error
JAVA0051 0.00JAVA0051 Class derives from java.lang.RuntimeException
JAVA0052 0.00JAVA0052 Class derives from java.lang.Throwable
JAVA0053 0.00JAVA0053 Unused label
JAVA0054 0.00JAVA0054 Inheritance depth N exceeds maximum M
JAVA0055 0.00JAVA0055 Class should be interface
JAVA0056 0.00JAVA0056 Unnecessary abstract modifier for interface or annotation
JAVA0057 0.00JAVA0057 Unnecessary default constructor
JAVA0058 0.00JAVA0058 Constructor calls super()
JAVA0059 1.00JAVA0059 Method override only calls super()
JAVA0061 0.00JAVA0061 Inaccessible member in anonymous class
JAVA0062 0.00JAVA0062 Public class missing public member or protected constructor
JAVA0063 0.00JAVA0063 Identifier name should not contain '$'
JAVA0064 2.00JAVA0064 N variations of identifier name (maximum: M)
JAVA0065 0.00JAVA0065 Unnecessary final modifier for method in final class
JAVA0066 0.00JAVA0066 Unnecessary modifier for interface nested type
JAVA0067 0.00JAVA0067 Array descriptor on identifier name
JAVA0068 0.00JAVA0068 Modifiers not declared in recommended order
JAVA0071 0.00JAVA0071 Strings compared with ==
JAVA0073 0.00JAVA0073 Integer division in floating-point context
JAVA0074 0.00JAVA0074 Use of Object.notify()
JAVA0075 0.00JAVA0075 Method parameter hides field
JAVA0076 2.00JAVA0076 Use of magic number
JAVA0077 0.00JAVA0077 Private field not used in declaring class
JAVA0078 1.00JAVA0078 Floating point values compared with ==
JAVA0079 0.00JAVA0079 Use of instance to reference static member
JAVA0080 0.00JAVA0080 Import declaration not used
JAVA0081 0.00JAVA0081 Boolean literal in comparison
JAVA0082 1.00JAVA0082 Unnecessary widening cast
JAVA0083 0.00JAVA0083 Unnecessary instanceof test
JAVA0084 0.00JAVA0084 Should use compound assignment operator
JAVA0085 0.00JAVA0085 Use of sun.* class
JAVA0087 0.00JAVA0087 Use of Thread.sleep()
JAVA0089 0.00JAVA0089 Use of restricted package
JAVA0092 0.00JAVA0092 Use of restricted type
JAVA0093 0.00JAVA0093 Redundant assignment
JAVA0094 1.00JAVA0094 Field hides a superclass field
JAVA0095 0.00JAVA0095 Uninitialized private field
JAVA0096 0.00JAVA0096 Field in nested class hides outer field
JAVA0098 0.00JAVA0098 Minimize use of implicit field initializers
JAVA0100 0.00JAVA0100 Class contains N non-final fields (maximum: M)
JAVA0101 0.00JAVA0101 Unnecessary modifier for field in interface
JAVA0102 0.00JAVA0102 Last statement in finalize() not super.finalize()
JAVA0103 0.00JAVA0103 Explicit call to finalize()
JAVA0104 0.00JAVA0104 finalize() only calls super.finalize()
JAVA0105 0.00JAVA0105 Duplicate import declaration
JAVA0106 0.00JAVA0106 Unnecessary import from current package
JAVA0108 4.00JAVA0108 Incorrect javadoc: no @param tag for 'parameter'
JAVA0109 9.00JAVA0109 Incorrect javadoc: no parameter 'parameter'
JAVA0110 0.00JAVA0110 Incorrect javadoc: no @return tag
JAVA0111 2.00JAVA0111 Incorrect javadoc: @return tag for void method
JAVA0112 0.00JAVA0112 Incorrect javadoc: no exception 'exception' in throws
JAVA0113 0.00JAVA0113 Incorrect javadoc: no @author tag
JAVA0114 0.00JAVA0114 Incorrect javadoc: no @version tag
JAVA0115 3.00JAVA0115 Incorrect javadoc: no @throws or @exception tag for 'exception'
JAVA0116 0.00JAVA0116 Missing javadoc: field 'field'
JAVA0117 1.00JAVA0117 Missing javadoc: method 'method'
JAVA0118 0.00JAVA0118 Missing javadoc: type 'type'
JAVA0119 0.00JAVA0119 Control variable changed within body of for loop
JAVA0123 0.00JAVA0123 Use all three components of for loop
JAVA0125 0.00JAVA0125 Continue statement with label
JAVA0126 0.00JAVA0126 Method declares unchecked exception in throws
JAVA0128 0.00JAVA0128 Public constructor in non-public class
JAVA0130 0.00JAVA0130 Non-static method does not use instance fields
JAVA0131 0.00JAVA0131 Compatible method does not override base
JAVA0132 0.00JAVA0132 Method overload with compatible signature
JAVA0133 0.00JAVA0133 Non-synchronized method overrides synchronized method
JAVA0135 0.00JAVA0135 Only one of Object.equals and Object.hashCode defined: missing 'method'
JAVA0136 1.00JAVA0136 N methods defined in class (maximum: M)
JAVA0137 0.00JAVA0137 Non-abstract class missing constructor
JAVA0138 1.00JAVA0138 N parameters defined for method (maximum: M)
JAVA0139 0.00JAVA0139 Definition of main other than public static void main(java.lang.String[])
JAVA0141 0.00JAVA0141 Unnecessary modifier for method in interface
JAVA0143 0.00JAVA0143 Synchronized method
JAVA0144 1.00JAVA0144 Line exceeds maximum M characters
JAVA01456646.00JAVA0145 Tab character used in source file
JAVA0150 0.00JAVA0150 java.lang.Error (or subclass) thrown
JAVA0153 0.00JAVA0153 Inefficient conversion of integer to string
JAVA0159 0.00JAVA0159 Inefficient conversion of string to integer
JAVA0160 5.00JAVA0160 Method does not throw specified exception
JAVA0161 0.00JAVA0161 Conditional wait() not in loop
JAVA0163 0.00JAVA0163 Empty statement
JAVA0165 0.00JAVA0165 Conflicting return statement in finally block
JAVA016613.00JAVA0166 Generic exception caught
JAVA0167 0.00JAVA0167 ThreadDeath not rethrown
JAVA0169 0.00JAVA0169 Unnecessary catch block: exception 'exception'
JAVA0170 0.00JAVA0170 Caught exception not derived from java.lang.Exception
JAVA0171 0.00JAVA0171 Unused local variable
JAVA0173 5.00JAVA0173 Unused method parameter
JAVA017410.00JAVA0174 Assigned local variable never used
JAVA0175 0.00JAVA0175 Successive assignment to variable
JAVA0176 0.00JAVA0176 Local variable name does not have required form
JAVA0177 0.00JAVA0177 Variable declaration missing initializer
JAVA0179 0.00JAVA0179 Local variable hides visible field
JAVA0233 0.00JAVA0233 Definition of serialVersionUID other than 'private static final long serialVersionUID'
JAVA0234 0.00JAVA0234 Class is Serializable but does not define serialVersionUID
JAVA0235 0.00JAVA0235 Class defines serialVersionUID but does not implement Serializable
JAVA0236 0.00JAVA0236 Attempt to clone an object which does not implement Cloneable
JAVA0237 1.00JAVA0237 Class implements Cloneable but does not have public clone method
JAVA0238 0.00JAVA0238 Clone method does not call super.clone()
JAVA0239 0.00JAVA0239 Class declares 'readObject' or 'writeObject' but does not implement Serializable
JAVA0240 0.00JAVA0240 Serializable class which declares readObject or writeObject but not both
JAVA0241 0.00JAVA0241 'readObject' or 'writeObject' should be declared private in Serializable class
JAVA0242 0.00JAVA0242 Transient field in non-Serializable class
JAVA0243 0.00JAVA0243 'readResolve' or 'writeReplace' should be declared private or protected
JAVA0244 0.00JAVA0244 Field or method name in subclass differs only by case from inherited field or method
JAVA0245 0.00JAVA0245 JUnit TestCase with non-trivial constructor
JAVA0246 0.00JAVA0246 JUnit assertXXX statement missing message parameter
JAVA0247 0.00JAVA0247 JUnit 'setUp()' and 'tearDown()' should call super method
JAVA0248 0.00JAVA0248 JUnit method 'setUp' or 'tearDown' with incorrect signature
JAVA0249 0.00JAVA0249 JUnit TestCase 'suite()' should be declared static
JAVA0250 0.00JAVA0250 JUnit TestCase declares testXXX method with incorrect signature
JAVA0251 0.00JAVA0251 Use '%n' for line breaks in printf/format for platform independence
JAVA0252 0.00JAVA0252 'enum' is a Java 1.5 reserved word
JAVA0253 0.00JAVA0253 Not all enum constants consumed in switch statement
JAVA0254 0.00JAVA0254 Use enhanced for loop construct instead of Iterator
JAVA0255 0.00JAVA0255 Result of method invocation not used
JAVA0256 0.00JAVA0256 Assignment of external collection/array to field
JAVA0257 0.00JAVA0257 Use of 'Constant Interface' anti-pattern
JAVA0258 0.00JAVA0258 Implement Iterable for foreach compatibility
JAVA0259 3.00JAVA0259 Return of collection/array field
JAVA0260 0.00JAVA0260 Use 'enum' instead of Enumerated Type pattern
JAVA0261 0.00JAVA0261 Use specialized Enum collection types
JAVA0262 0.00JAVA0262 Use of char in integer context
JAVA0263 0.00JAVA0263 Long literal ends with 'l' instead of 'L'
JAVA0264 0.00JAVA0264 Integer math in long context - check for overflow
JAVA0265 0.00JAVA0265 Use of Throwable.printStackTrace()
JAVA0266 0.00JAVA0266 Use of System.out
JAVA0267 0.00JAVA0267 Use of System.err
JAVA0269 0.00JAVA0269 Contents of StringBuffer never used
JAVA027019.00JAVA0270 Use Java 5.0 enhanced for loop construct to iterate over all elements in an array
JAVA0271 0.00JAVA0271 Minimize use of on-demand (.*) static imports
JAVA0272 0.00JAVA0272 Thread.run() called
JAVA0273 0.00JAVA0273 Non-final derivative of Thread calls start() in constructor
JAVA0274 0.00JAVA0274 Serializable class has a synchronized readObject()
JAVA0275 0.00JAVA0275 Serializable class has a synchronized writeObject() and no other synchronized methods
JAVA0276 0.00JAVA0276 Unnecessary use of String constructor
JAVA0277 0.00JAVA0277 Iterator.next() implementation does not throw NoSuchElementException
JAVA0278 0.00JAVA0278 Unnecessary use of Boolean constructor
JAVA0279 0.00JAVA0279 Serialization method readObject or readObjectNoData calls an overridable method
JAVA0280 0.00JAVA0280 IllegalMonitorStateException caught
JAVA0281 0.00JAVA0281 Iterator.next() not called in loop
JAVA0282 0.00JAVA0282 Call to Iterator.next() in loop which does not test Iterator.hasNext()
JAVA0283 0.00JAVA0283 Control variable not updated in loop body
JAVA0284 0.00JAVA0284 Explicit garbage collection
JAVA0285 2.00JAVA0285 Dereference of potentially null variable
JAVA0286 0.00JAVA0286 Dereference of null variable
JAVA0287 0.00JAVA0287 Unnecessary null check
JAVA0288 0.00JAVA0288 Inconsistent null check
LINES2638.00Number of lines in the source file
LINE_COMMENT171.00Number of line comments
LOC1877.00Lines of code
LOGICAL_LINES970.00Number of statements
LOOPS24.00Number of loops
NEST_DEPTH 6.00Maximum nesting depth
OPERANDS4076.00Number of operands
OPERATORS8153.00Number of operators
PARAMS73.00Number of formal parameter declarations
PROGRAM_LENGTH12229.00Halstead program length
PROGRAM_VOCAB1176.00Halstead program vocabulary
PROGRAM_VOLUME 0.00Halstead program volume
RETURNS148.00Number of return points from functions
SIZE75151.00Size of the file in bytes
UNIQUE_OPERANDS1120.00Number of unique operands
UNIQUE_OPERATORS56.00Number of unique operators
WHITESPACE189.00Number of whitespace lines